+static void timer_tick(void *data, XtIntervalId *id)
+{
+ struct app_state *state = data;
+ XtAppContext app;
+
+ if (state->board.x > 4) {
+ /* Game is over */
+ state->flags &= ~FLAG_TIMER_RUNNING;
+ return;
+ }
+
+ ui_timer_update(state, game_elapsed(&state->board));
+ app = XtWidgetToApplicationContext(state->game);
+ XtAppAddTimeOut(app, TIMER_UPDATE_MS, timer_tick, state);
+}
+
+static void do_input_move(struct app_state *state, int x, int y)
+{
+ uint_fast32_t mask;
+
+ if ((mask = game_do_move(&state->board, x, y)) != 0) {
+ if (game_check_goal(&state->board) == 0) {
+ int_fast32_t ms = game_finish(&state->board);
+ unsigned min, sec;
+
+ ui_timer_update(state, ms);
+
+ sec = ms / 1000, ms %= 1000;
+ min = sec / 60, sec %= 60;
+ printf("You won! Time was %u:%.2u.%.3u\n",
+ min, sec, (unsigned)ms);
+
+ mask |= ~GOAL_MASK;
+ }
+
+ x11_redraw_game(state, mask);
+ }
+}
+
+static void set_view_goal(struct app_state *state, int view_goal)
+{
+ state->flags &= ~FLAG_VIEW_GOAL_ON_GAME;
+ state->flags |= view_goal ? FLAG_VIEW_GOAL_ON_GAME : 0;
+
+ x11_redraw_game(state, game_check_goal(&state->board));
+}
+
+static void game_input(Widget w, void *data, void *cb_data)
+{
+ XmDrawingAreaCallbackStruct *cbs = cb_data;
+ XButtonEvent *b = &cbs->event->xbutton;
+ struct app_state *state = data;
+
+ switch (cbs->event->type) {
+ case ButtonPress:
+ switch (b->button) {
+ case Button1:
+ if (b->state & Button3Mask)
+ break;
+
+ do_input_move(state, b->x / state->game_tile_sz,
+ b->y / state->game_tile_sz);
+ break;
+ case Button3:
+ set_view_goal(state, 1);
+ break;
+ }
+ break;
+ case ButtonRelease:
+ switch (b->button) {
+ case Button3:
+ set_view_goal(state, 0);
+ break;
+ }
+ }
+}
+
+static struct app_state state;
+
+static Widget get_shell(Widget w)
+{
+ Widget shell;
+
+ for (shell = w; !XtIsWMShell(shell);)
+ shell = XtParent(shell);
+
+ return shell;
+}
+
+static void proc_exit(Widget w, XEvent *e, String *argv, Cardinal *argc)
+{
+ XtAppSetExitFlag(XtWidgetToApplicationContext(w));
+}
+
+static void proc_new_game(Widget w, XEvent *e, String *argv, Cardinal *argc)
+{
+ XtAppContext app;
+
+ game_reset(&state.board);
+
+ x11_redraw_goal(&state, -1, get_shell(w));
+ x11_redraw_game(&state, -1);
+
+ game_begin(&state.board);
+ if (!(state.flags & FLAG_TIMER_RUNNING)) {
+ state.flags |= FLAG_TIMER_RUNNING;
+ timer_tick(&state, 0);
+ }
+}
+
+static void proc_about(Widget w, XEvent *e, String *argv, Cardinal *argc)
+{
+ ui_show_about(&state, get_shell(w));
+}
+
+static void proc_resize(Widget form, XEvent *e, String *argv, Cardinal *argc)
+{
+ Widget game = XtParent(state.game);
+ Widget goal = XtParent(state.goal);
+ Dimension w, h, gamesz, gameborder, goalsz, goalborder;
+ int x, y, gap;
+
+ XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
+ XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
+ XtVaGetValues(goal, XmNshadowThickness, &goalborder,
+ XmNleftOffset, &gap,
+ (char *)NULL);
+
+ gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
+ state.game_tile_sz = (gamesz - 2*gameborder) / 5;
+ gamesz = 5*state.game_tile_sz + 2*gameborder;
+
+ goalsz = MIN(gamesz*3/5, w - gamesz - gap);
+ state.goal_tile_sz = (goalsz - 2*goalborder) / 3;
+ goalsz = 3*state.goal_tile_sz + 2*goalborder;
+
+ x = (w - gamesz - goalsz - gap) / 2;
+ if (x < 2) x = 0;
+
+ y = (h - gamesz) / 2;
+ if (y < 3) y = 0;
+
+ XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL);
+ XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL);
+ XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
+}
+
+static const XtActionsRec app_actions[] = {
+ { "gameNew", proc_new_game },
+ { "gameExit", proc_exit },
+ { "helpAbout", proc_about },
+
+ { "ResizeGameArea", proc_resize }
+};
+