]> git.draconx.ca Git - rrace.git/blob - src/motif.c
7396c0b7b7fdca7e156b45274b020bde1137443d
[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 <getopt.h>
23
24 #include <Xm/XmAll.h>
25
26 #include "help.h"
27 #include "motif.h"
28 #include "motifopt.h"
29 #include "game.h"
30
31 #define TIMER_UPDATE_MS 33
32
33 #define PROGNAME "rrace"
34 static const char *progname = PROGNAME;
35 static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
36
37 static char * const default_resources[] = {
38         "*title: RRace",
39         "*game.height: 371", // 365 + 2*shadowThickness
40         "*game.width: 498",
41
42         "*game.XmFrame.shadowThickness: 3",
43         "*game.XmFrame.shadowType: shadow_in",
44         "*goalArea.leftOffset: 1",
45
46         "*gameNew.accelerator: Ctrl<Key>N",
47         "*gameNew.acceleratorText: Ctrl+N",
48         "*gameExit.accelerator: Ctrl<Key>Q",
49         "*gameExit.acceleratorText: Ctrl+Q",
50
51         NULL
52 };
53
54 static void print_version(void)
55 {
56         printf("%s %s\n", PROGNAME, PACKAGE_VERSION);
57         printf("Copyright (C) 2022 Nick Bowler\n");
58         puts("License GPLv3+: GNU GPL version 3 or any later version");
59         puts("This is free software: you are free to change and redistribute it.");
60         puts("There is NO WARRANTY, to the extent permitted by law.");
61 }
62
63 static void print_usage(FILE *f)
64 {
65         fprintf(f, "Usage: %s [options]\n", progname);
66         if (f != stdout)
67                 fprintf(f, "Try %s --help for more information.\n", progname);
68 }
69
70 static void print_help(void)
71 {
72         struct lopt_help help = {0};
73         const struct option *opt;
74
75         print_usage(stdout);
76
77         putchar('\n');
78         puts("Options:");
79         for (opt = lopts; opt->name; opt++) {
80                 if (!lopt_get_help(opt, &help))
81                         continue;
82                 help_print_option(opt, help.arg, help.desc, 20);
83         }
84         putchar('\n');
85
86         printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
87 }
88
89 static Widget early_setup(XtAppContext *app, int argc, char **argv)
90 {
91         Widget shell;
92         int opt;
93
94         /* Check for --help/--version early (before X connection) */
95         opterr = 0;
96         while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
97                 switch (opt) {
98                 case LOPT_VERSION:
99                         print_version();
100                         exit(EXIT_SUCCESS);
101                 case LOPT_HELP:
102                         print_help();
103                         exit(EXIT_SUCCESS);
104                 }
105         }
106
107         shell = XtOpenApplication(app, PACKAGE_TARNAME, NULL, 0,
108                                   &argc, argv, (String *)default_resources,
109                                   sessionShellWidgetClass, NULL, 0);
110
111         opterr = optind = 1;
112         while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
113                 switch (opt) {
114                 default:
115                         print_usage(stderr);
116                         exit(EXIT_FAILURE);
117                 }
118         }
119
120         if (argv[optind]) {
121                 fprintf(stderr, "%s: excess command-line arguments.\n",
122                                 progname);
123                 print_usage(stderr);
124                 exit(EXIT_FAILURE);
125         }
126
127         return shell;
128 }
129
130 static void timer_tick(void *data, XtIntervalId *id)
131 {
132         struct app_state *state = data;
133         XtAppContext app;
134
135         if (state->board.x > 4) {
136                 /* Game is over */
137                 state->timer_tick = 0;
138                 return;
139         }
140
141         app = XtWidgetToApplicationContext(state->timer);
142         ui_timer_update(state, game_elapsed(&state->board));
143         state->timer_tick = XtAppAddTimeOut(app, TIMER_UPDATE_MS,
144                                             timer_tick, state);
145 }
146
147 static void do_input_move(struct app_state *state, int x, int y)
148 {
149         uint_fast32_t mask;
150
151         if ((mask = game_do_move(&state->board, x, y)) != 0) {
152                 if (game_check_goal(&state->board) == 0) {
153                         int_fast32_t ms = game_finish(&state->board);
154                         unsigned min, sec;
155
156                         ui_timer_update(state, ms);
157
158                         sec = ms / 1000, ms %= 1000;
159                         min = sec / 60, sec %= 60;
160                         printf("You won!  Time was %u:%.2u.%.3u\n",
161                                min, sec, (unsigned)ms);
162
163                         mask |= ~GOAL_MASK;
164                 }
165
166                 x11_redraw_game(state, mask);
167         }
168 }
169
170 static void set_view_goal(struct app_state *state, int view_goal)
171 {
172         state->view_goal_on_game = view_goal;
173         x11_redraw_game(state, game_check_goal(&state->board));
174 }
175
176 static void game_input(Widget w, void *data, void *cb_data)
177 {
178         XmDrawingAreaCallbackStruct *cbs = cb_data;
179         XButtonEvent *b = &cbs->event->xbutton;
180         struct app_state *state = data;
181         Dimension width, height;
182
183         switch (cbs->event->type) {
184         case ButtonPress:
185                 switch (b->button) {
186                 case Button1:
187                         if (b->state & Button3Mask)
188                                 break;
189
190                         XtVaGetValues(w, XmNwidth, &width,
191                                          XmNheight, &height,
192                                          (char *)NULL);
193
194                         do_input_move(state, b->x / (width / 5),
195                                              b->y / (height / 5));
196                         break;
197                 case Button3:
198                         set_view_goal(state, 1);
199                         break;
200                 }
201                 break;
202         case ButtonRelease:
203                 switch (b->button) {
204                 case Button3:
205                         set_view_goal(state, 0);
206                         break;
207                 }
208         }
209 }
210
211 static struct app_state state;
212
213 static void proc_exit(Widget w, XEvent *e, String *argv, Cardinal *argc)
214 {
215         XtAppSetExitFlag(XtWidgetToApplicationContext(w));
216 }
217
218 static void proc_new_game(Widget w, XEvent *e, String *argv, Cardinal *argc)
219 {
220         Widget shell;
221
222         for (shell = w; !XtIsWMShell(shell);)
223                 shell = XtParent(shell);
224
225         game_reset(&state.board);
226
227         x11_redraw_goal(&state, -1);
228         x11_redraw_game(&state, -1);
229         x11_redraw_icon(&state, shell);
230
231         if (!state.timer_tick) {
232                 XtAppContext app = XtWidgetToApplicationContext(w);
233                 state.timer_tick = XtAppAddTimeOut(app, TIMER_UPDATE_MS,
234                                                    timer_tick, &state);
235         }
236
237         game_begin(&state.board);
238 }
239
240 static const XtActionsRec menu_actions[] = {
241         { "gameNew", proc_new_game },
242         { "gameExit", proc_exit }
243 };
244
245 static XtAppContext app_initialize(int argc, char **argv)
246 {
247         XtAppContext app;
248         Widget shell;
249         int i;
250
251         if (argc > 0)
252                 progname = argv[0];
253
254         shell = early_setup(&app, argc, argv);
255         XtAppAddActions(app, (void *)menu_actions, XtNumber(menu_actions));
256         ui_initialize(&state, shell);
257         x11_initialize(&state, shell);
258
259         XtAddCallback(state.game, XmNinputCallback,  game_input, &state);
260
261         /* Begin with the game in winning state */
262         game_reset(&state.board);
263         for (i = 0; i < 3; i++)
264                 state.board.game[i] = state.board.goal[i] << GOAL_SHIFT;
265         game_finish(&state.board);
266
267         state.use_ewmh_icons = ewmh_probe_wm_icon(shell);
268         XtRealizeWidget(shell);
269
270         x11_redraw_icon(&state, shell);
271
272         return app;
273 }
274
275 int main(int argc, char **argv)
276 {
277         XtAppMainLoop(app_initialize(argc, argv));
278         return 0;
279 }