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