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