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