2 * X11 GUI for slide puzzle game
3 * Copyright © 2022-2023 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
34 #define TIMER_UPDATE_MS 33
36 #define SPLIT_NUMERATOR 75
37 #define SPLIT_DENOMINATOR 100
39 #define MIN(a, b) ((a) < (b) ? (a) : (b))
41 #define PROGNAME "rrace"
42 static const char *progname = PROGNAME;
43 static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
45 static char * const default_resources[] = {
47 "*game.height: 371", // 365 + 2*shadowThickness
50 "*game.XmFrame.shadowThickness: 3",
51 "*game.XmFrame.shadowType: shadow_in",
52 "*goalArea.leftOffset: 1",
54 "*gameNew.accelerator: Ctrl<Key>N",
55 "*gameNew.acceleratorText: Ctrl+N",
56 "*gameExit.accelerator: Ctrl<Key>Q",
57 "*gameExit.acceleratorText: Ctrl+Q",
59 "*aboutDialog*pixmapTextPadding: 10",
64 static void print_version(void)
66 version_print_head("rrace-motif", stdout);
67 puts("License GPLv3+: GNU GPL version 3 or any later version");
68 puts("This is free software: you are free to change and redistribute it.");
69 puts("There is NO WARRANTY, to the extent permitted by law.");
72 static void print_usage(FILE *f)
74 fprintf(f, "Usage: %s [options]\n", progname);
76 fprintf(f, "Try %s --help for more information.\n", progname);
79 static void print_help(void)
81 struct lopt_help help = {0};
82 const struct option *opt;
88 for (opt = lopts; opt->name; opt++) {
89 if (!lopt_get_help(opt, &help))
91 help_print_option(opt, help.arg, help.desc, 20);
95 printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
98 static Widget early_setup(XtAppContext *app, int argc, char **argv)
103 /* Check for --help/--version early (before X connection) */
105 while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
116 shell = XtOpenApplication(app, PACKAGE_TARNAME, NULL, 0,
117 &argc, argv, (String *)default_resources,
118 sessionShellWidgetClass, NULL, 0);
121 while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
130 fprintf(stderr, "%s: excess command-line arguments.\n",
139 static void timer_tick(void *data, XtIntervalId *id)
141 struct app_state *state = data;
144 if (state->board.x > 4) {
146 state->flags &= ~FLAG_TIMER_RUNNING;
150 ui_timer_update(state, game_elapsed(&state->board));
151 app = XtWidgetToApplicationContext(state->game);
152 XtAppAddTimeOut(app, TIMER_UPDATE_MS, timer_tick, state);
155 static void do_input_move(struct app_state *state, int x, int y)
159 if ((mask = game_do_move(&state->board, x, y)) != 0) {
160 if (game_check_goal(&state->board) == 0) {
161 int_fast32_t ms = game_finish(&state->board);
164 ui_timer_update(state, ms);
166 sec = ms / 1000, ms %= 1000;
167 min = sec / 60, sec %= 60;
168 printf("You won! Time was %u:%.2u.%.3u\n",
169 min, sec, (unsigned)ms);
174 x11_redraw_game(state, mask);
178 static void set_view_goal(struct app_state *state, int view_goal)
180 state->flags &= ~FLAG_VIEW_GOAL_ON_GAME;
181 state->flags |= view_goal ? FLAG_VIEW_GOAL_ON_GAME : 0;
183 x11_redraw_game(state, game_check_goal(&state->board));
186 static void game_input(Widget w, void *data, void *cb_data)
188 XmDrawingAreaCallbackStruct *cbs = cb_data;
189 XButtonEvent *b = &cbs->event->xbutton;
190 struct app_state *state = data;
192 switch (cbs->event->type) {
196 if (b->state & Button3Mask)
199 do_input_move(state, b->x / state->game_tile_sz,
200 b->y / state->game_tile_sz);
203 set_view_goal(state, 1);
210 set_view_goal(state, 0);
216 static struct app_state state;
218 static Widget get_shell(Widget w)
222 for (shell = w; !XtIsWMShell(shell);)
223 shell = XtParent(shell);
228 static void proc_exit(Widget w, XEvent *e, String *argv, Cardinal *argc)
230 XtAppSetExitFlag(XtWidgetToApplicationContext(w));
233 static void proc_new_game(Widget w, XEvent *e, String *argv, Cardinal *argc)
237 game_reset(&state.board);
239 x11_redraw_goal(&state, -1, get_shell(w));
240 x11_redraw_game(&state, -1);
242 game_begin(&state.board);
243 if (!(state.flags & FLAG_TIMER_RUNNING)) {
244 state.flags |= FLAG_TIMER_RUNNING;
245 timer_tick(&state, 0);
249 static void proc_about(Widget w, XEvent *e, String *argv, Cardinal *argc)
251 ui_show_about(&state, get_shell(w));
254 static void proc_resize(Widget form, XEvent *e, String *argv, Cardinal *argc)
256 Widget game = XtParent(state.game);
257 Widget goal = XtParent(state.goal);
258 Dimension w, h, gamesz, gameborder, goalsz, goalborder;
261 XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
262 XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
263 XtVaGetValues(goal, XmNshadowThickness, &goalborder,
267 gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
268 state.game_tile_sz = (gamesz - 2*gameborder) / 5;
269 gamesz = 5*state.game_tile_sz + 2*gameborder;
271 goalsz = MIN(gamesz*3/5, w - gamesz - gap);
272 state.goal_tile_sz = (goalsz - 2*goalborder) / 3;
273 goalsz = 3*state.goal_tile_sz + 2*goalborder;
275 x = (w - gamesz - goalsz - gap) / 2;
278 y = (h - gamesz) / 2;
281 XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL);
282 XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL);
283 XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
286 static const XtActionsRec app_actions[] = {
287 { "gameNew", proc_new_game },
288 { "gameExit", proc_exit },
289 { "helpAbout", proc_about },
291 { "ResizeGameArea", proc_resize }
294 static XtAppContext app_initialize(int argc, char **argv)
303 shell = early_setup(&app, argc, argv);
304 XtAppAddActions(app, (void *)app_actions, XtNumber(app_actions));
305 ui_initialize(&state, shell);
306 x11_initialize(&state, shell);
308 XtAddCallback(state.game, XmNinputCallback, game_input, &state);
310 /* Begin with the game in winning state */
311 game_reset(&state.board);
312 for (i = 0; i < 3; i++)
313 state.board.game[i] = state.board.goal[i] << GOAL_SHIFT;
314 game_finish(&state.board);
316 state.flags = ewmh_probe_wm_icon(shell);
317 XtRealizeWidget(shell);
319 x11_redraw_goal(&state, 0, shell);
324 int main(int argc, char **argv)
326 setlocale(LC_ALL, "");
327 XtAppMainLoop(app_initialize(argc, argv));