]> git.draconx.ca Git - rrace.git/blob - src/motif.c
c4ea62dbbd25b5d5e4a4b7d6ceec50a3d8f4e40d
[rrace.git] / src / motif.c
1 /*
2  * X11 GUI for slide puzzle game
3  * Copyright © 2022 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <locale.h>
23 #include <getopt.h>
24
25 #include <Xm/XmAll.h>
26
27 #include "help.h"
28 #include "motif.h"
29 #include "ewmhicon.h"
30 #include "motifopt.h"
31 #include "game.h"
32 #include "version.h"
33
34 #define TIMER_UPDATE_MS 33
35
36 #define SPLIT_NUMERATOR    75
37 #define SPLIT_DENOMINATOR 100
38
39 #define MIN(a, b) ((a) < (b) ? (a) : (b))
40
41 #define PROGNAME "rrace"
42 static const char *progname = PROGNAME;
43 static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
44
45 static char * const default_resources[] = {
46         "*title: RRace",
47         "*game.height: 371", // 365 + 2*shadowThickness
48         "*game.width: 498",
49
50         "*game.XmFrame.shadowThickness: 3",
51         "*game.XmFrame.shadowType: shadow_in",
52         "*goalArea.leftOffset: 1",
53
54         "*gameNew.accelerator: Ctrl<Key>N",
55         "*gameNew.acceleratorText: Ctrl+N",
56         "*gameExit.accelerator: Ctrl<Key>Q",
57         "*gameExit.acceleratorText: Ctrl+Q",
58
59         "*aboutDialog*pixmapTextPadding: 10",
60
61         NULL
62 };
63
64 static void print_version(void)
65 {
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.");
70 }
71
72 static void print_usage(FILE *f)
73 {
74         fprintf(f, "Usage: %s [options]\n", progname);
75         if (f != stdout)
76                 fprintf(f, "Try %s --help for more information.\n", progname);
77 }
78
79 static void print_help(void)
80 {
81         struct lopt_help help = {0};
82         const struct option *opt;
83
84         print_usage(stdout);
85
86         putchar('\n');
87         puts("Options:");
88         for (opt = lopts; opt->name; opt++) {
89                 if (!lopt_get_help(opt, &help))
90                         continue;
91                 help_print_option(opt, help.arg, help.desc, 20);
92         }
93         putchar('\n');
94
95         printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
96 }
97
98 static Widget early_setup(XtAppContext *app, int argc, char **argv)
99 {
100         Widget shell;
101         int opt;
102
103         /* Check for --help/--version early (before X connection) */
104         opterr = 0;
105         while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
106                 switch (opt) {
107                 case LOPT_VERSION:
108                         print_version();
109                         exit(EXIT_SUCCESS);
110                 case LOPT_HELP:
111                         print_help();
112                         exit(EXIT_SUCCESS);
113                 }
114         }
115
116         shell = XtOpenApplication(app, PACKAGE_TARNAME, NULL, 0,
117                                   &argc, argv, (String *)default_resources,
118                                   sessionShellWidgetClass, NULL, 0);
119
120         opterr = optind = 1;
121         while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
122                 switch (opt) {
123                 default:
124                         print_usage(stderr);
125                         exit(EXIT_FAILURE);
126                 }
127         }
128
129         if (argv[optind]) {
130                 fprintf(stderr, "%s: excess command-line arguments.\n",
131                                 progname);
132                 print_usage(stderr);
133                 exit(EXIT_FAILURE);
134         }
135
136         return shell;
137 }
138
139 static void timer_tick(void *data, XtIntervalId *id)
140 {
141         struct app_state *state = data;
142         XtAppContext app;
143
144         if (state->board.x > 4) {
145                 /* Game is over */
146                 state->timer_tick = 0;
147                 return;
148         }
149
150         app = XtWidgetToApplicationContext(state->game);
151         ui_timer_update(state, game_elapsed(&state->board));
152         state->timer_tick = XtAppAddTimeOut(app, TIMER_UPDATE_MS,
153                                             timer_tick, state);
154 }
155
156 static void do_input_move(struct app_state *state, int x, int y)
157 {
158         uint_fast32_t mask;
159
160         if ((mask = game_do_move(&state->board, x, y)) != 0) {
161                 if (game_check_goal(&state->board) == 0) {
162                         int_fast32_t ms = game_finish(&state->board);
163                         unsigned min, sec;
164
165                         ui_timer_update(state, ms);
166
167                         sec = ms / 1000, ms %= 1000;
168                         min = sec / 60, sec %= 60;
169                         printf("You won!  Time was %u:%.2u.%.3u\n",
170                                min, sec, (unsigned)ms);
171
172                         mask |= ~GOAL_MASK;
173                 }
174
175                 x11_redraw_game(state, mask);
176         }
177 }
178
179 static void set_view_goal(struct app_state *state, int view_goal)
180 {
181         state->view_goal_on_game = view_goal;
182         x11_redraw_game(state, game_check_goal(&state->board));
183 }
184
185 static void game_input(Widget w, void *data, void *cb_data)
186 {
187         XmDrawingAreaCallbackStruct *cbs = cb_data;
188         XButtonEvent *b = &cbs->event->xbutton;
189         struct app_state *state = data;
190
191         switch (cbs->event->type) {
192         case ButtonPress:
193                 switch (b->button) {
194                 case Button1:
195                         if (b->state & Button3Mask)
196                                 break;
197
198                         do_input_move(state, b->x / state->game_tile_sz,
199                                              b->y / state->game_tile_sz);
200                         break;
201                 case Button3:
202                         set_view_goal(state, 1);
203                         break;
204                 }
205                 break;
206         case ButtonRelease:
207                 switch (b->button) {
208                 case Button3:
209                         set_view_goal(state, 0);
210                         break;
211                 }
212         }
213 }
214
215 static struct app_state state;
216
217 static Widget get_shell(Widget w)
218 {
219         Widget shell;
220
221         for (shell = w; !XtIsWMShell(shell);)
222                 shell = XtParent(shell);
223
224         return shell;
225 }
226
227 static void proc_exit(Widget w, XEvent *e, String *argv, Cardinal *argc)
228 {
229         XtAppSetExitFlag(XtWidgetToApplicationContext(w));
230 }
231
232 static void proc_new_game(Widget w, XEvent *e, String *argv, Cardinal *argc)
233 {
234         game_reset(&state.board);
235
236         x11_redraw_goal(&state, -1, get_shell(w));
237         x11_redraw_game(&state, -1);
238
239         if (!state.timer_tick) {
240                 XtAppContext app = XtWidgetToApplicationContext(w);
241                 state.timer_tick = XtAppAddTimeOut(app, TIMER_UPDATE_MS,
242                                                    timer_tick, &state);
243         }
244
245         game_begin(&state.board);
246 }
247
248 static void proc_about(Widget w, XEvent *e, String *argv, Cardinal *argc)
249 {
250         ui_show_about(&state, get_shell(w));
251 }
252
253 static void proc_resize(Widget form, XEvent *e, String *argv, Cardinal *argc)
254 {
255         Widget game = XtParent(state.game);
256         Widget goal = XtParent(state.goal);
257         Dimension w, h, gamesz, gameborder, goalsz, goalborder;
258         int x, y, gap;
259
260         XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
261         XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
262         XtVaGetValues(goal, XmNshadowThickness, &goalborder,
263                             XmNleftOffset, &gap,
264                             (char *)NULL);
265
266         gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
267         state.game_tile_sz = (gamesz - 2*gameborder) / 5;
268         gamesz = 5*state.game_tile_sz + 2*gameborder;
269
270         goalsz = MIN(gamesz*3/5, w - gamesz - gap);
271         state.goal_tile_sz = (goalsz - 2*goalborder) / 3;
272         goalsz = 3*state.goal_tile_sz + 2*goalborder;
273
274         x = (w - gamesz - goalsz - gap) / 2;
275         if (x < 2) x = 0;
276
277         y = (h - gamesz) / 2;
278         if (y < 3) y = 0;
279
280         XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL);
281         XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL);
282         XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
283 }
284
285 static const XtActionsRec app_actions[] = {
286         { "gameNew", proc_new_game },
287         { "gameExit", proc_exit },
288         { "helpAbout", proc_about },
289
290         { "ResizeGameArea", proc_resize }
291 };
292
293 static XtAppContext app_initialize(int argc, char **argv)
294 {
295         XtAppContext app;
296         Widget shell;
297         int i;
298
299         if (argc > 0)
300                 progname = argv[0];
301
302         shell = early_setup(&app, argc, argv);
303         XtAppAddActions(app, (void *)app_actions, XtNumber(app_actions));
304         ui_initialize(&state, shell);
305         x11_initialize(&state, shell);
306
307         XtAddCallback(state.game, XmNinputCallback, game_input, &state);
308
309         /* Begin with the game in winning state */
310         game_reset(&state.board);
311         for (i = 0; i < 3; i++)
312                 state.board.game[i] = state.board.goal[i] << GOAL_SHIFT;
313         game_finish(&state.board);
314
315         state.use_ewmh_icons = ewmh_probe_wm_icon(shell);
316         XtRealizeWidget(shell);
317
318         x11_redraw_goal(&state, 0, shell);
319
320         return app;
321 }
322
323 int main(int argc, char **argv)
324 {
325         setlocale(LC_ALL, "");
326         XtAppMainLoop(app_initialize(argc, argv));
327         return 0;
328 }