]> git.draconx.ca Git - rrace.git/blob - src/motif.c
f9366f7f30b4e2ac1ca5539df03cace02c273f12
[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         "*game.height: 371", // 365 + 2*shadowThickness
37         "*game.width: 498",
38
39         "*game.XmFrame.shadowThickness: 3",
40         "*game.XmFrame.shadowType: shadow_in",
41         "*goalArea.leftOffset: 1",
42         "*gameCanvas.background: #202020",
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         const struct option *opt;
71
72         print_usage(stdout);
73
74         putchar('\n');
75         puts("Options:");
76         for (opt = lopts; opt->name; opt++) {
77                 struct lopt_help help;
78                 int w;
79
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, PROGNAME, 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 struct app_state state;
131
132 static void proc_exit(Widget w, XEvent *e, String *argv, Cardinal *argc)
133 {
134         XtAppSetExitFlag(XtWidgetToApplicationContext(w));
135 }
136
137 static void proc_new_game(Widget w, XEvent *e, String *argv, Cardinal *argc)
138 {
139         game_reset(&state.board);
140         x11_redraw_goal(&state);
141         x11_redraw_game(&state);
142 }
143
144 static const XtActionsRec menu_actions[] = {
145         { "gameNew", proc_new_game },
146         { "gameExit", proc_exit }
147 };
148
149 static XtAppContext app_initialize(int argc, char **argv)
150 {
151         XtAppContext app;
152         Widget shell;
153
154         if (argc > 0)
155                 progname = argv[0];
156
157         shell = early_setup(&app, argc, argv);
158         XtAppAddActions(app, (void *)menu_actions, XtNumber(menu_actions));
159         ui_initialize(&state, shell);
160         x11_initialize(&state, XtScreen(shell));
161         game_reset(&state.board);
162         XtRealizeWidget(shell);
163
164         return app;
165 }
166
167 int main(int argc, char **argv)
168 {
169         XtAppMainLoop(app_initialize(argc, argv));
170         return 0;
171 }