2 * X11 GUI for slide puzzle game
3 * Copyright © 2022 Nick Bowler
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.
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.
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/>.
31 #define PROGNAME "rrace"
32 static const char *progname = PROGNAME;
33 static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
35 static char * const default_resources[] = {
37 "*game.height: 371", // 365 + 2*shadowThickness
40 "*game.XmFrame.shadowThickness: 3",
41 "*game.XmFrame.shadowType: shadow_in",
42 "*goalArea.leftOffset: 1",
43 "*gameCanvas.background: #202020",
45 "*gameNew.accelerator: Ctrl<Key>N",
46 "*gameNew.acceleratorText: Ctrl+N",
47 "*gameExit.accelerator: Ctrl<Key>Q",
48 "*gameExit.acceleratorText: Ctrl+Q",
53 static void print_version(void)
55 printf("%s %s\n", PROGNAME, PACKAGE_VERSION);
56 printf("Copyright (C) 2022 Nick Bowler\n");
57 puts("License GPLv3+: GNU GPL version 3 or any later version");
58 puts("This is free software: you are free to change and redistribute it.");
59 puts("There is NO WARRANTY, to the extent permitted by law.");
62 static void print_usage(FILE *f)
64 fprintf(f, "Usage: %s [options]\n", progname);
66 fprintf(f, "Try %s --help for more information.\n", progname);
69 static void print_help(void)
71 const struct option *opt;
77 for (opt = lopts; opt->name; opt++) {
78 struct lopt_help help;
80 if (!lopt_get_help(opt, &help))
82 help_print_option(opt, help.arg, help.desc, 20);
86 printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
89 static Widget early_setup(XtAppContext *app, int argc, char **argv)
94 /* Check for --help/--version early (before X connection) */
96 while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
107 shell = XtOpenApplication(app, PROGNAME, NULL, 0,
108 &argc, argv, (String *)default_resources,
109 sessionShellWidgetClass, NULL, 0);
112 while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
121 fprintf(stderr, "%s: excess command-line arguments.\n",
130 static struct app_state state;
132 static void proc_exit(Widget w, XEvent *e, String *argv, Cardinal *argc)
134 XtAppSetExitFlag(XtWidgetToApplicationContext(w));
137 static void proc_new_game(Widget w, XEvent *e, String *argv, Cardinal *argc)
141 for (shell = w; !XtIsWMShell(shell);)
142 shell = XtParent(shell);
144 game_reset(&state.board);
145 x11_redraw_goal(&state, -1);
146 x11_redraw_game(&state, -1);
147 x11_redraw_icon(&state, shell);
150 static const XtActionsRec menu_actions[] = {
151 { "gameNew", proc_new_game },
152 { "gameExit", proc_exit }
155 static XtAppContext app_initialize(int argc, char **argv)
163 shell = early_setup(&app, argc, argv);
164 XtAppAddActions(app, (void *)menu_actions, XtNumber(menu_actions));
165 ui_initialize(&state, shell);
166 x11_initialize(&state, shell);
167 game_reset(&state.board);
168 state.use_ewmh_icons = ewmh_probe_wm_icon(shell);
169 XtRealizeWidget(shell);
171 x11_redraw_icon(&state, shell);
176 int main(int argc, char **argv)
178 XtAppMainLoop(app_initialize(argc, argv));