]> git.draconx.ca Git - rrace.git/blob - src/motif.c
Initial commit
[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 "options.h"
29
30 #define PROGNAME "rrace"
31 static const char *progname = PROGNAME;
32 static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
33
34 static char * const default_resources[] = {
35         PROGNAME "*game.height: 371", // 365 + 2*shadowThickness
36         PROGNAME "*game.width: 498",
37
38         PROGNAME "*game.XmFrame.shadowThickness: 3",
39         PROGNAME "*game.XmFrame.shadowType: shadow_in",
40         PROGNAME "*goalArea.leftOffset: 1",
41
42         NULL
43 };
44
45 static void print_version(void)
46 {
47         printf("%s %s\n", PROGNAME, PACKAGE_VERSION);
48         printf("Copyright (C) 2022 Nick Bowler\n");
49         puts("License GPLv3+: GNU GPL version 3 or any later version");
50         puts("This is free software: you are free to change and redistribute it.");
51         puts("There is NO WARRANTY, to the extent permitted by law.");
52 }
53
54 static void print_usage(FILE *f)
55 {
56         fprintf(f, "Usage: %s [options]\n", progname);
57         if (f != stdout)
58                 fprintf(f, "Try %s --help for more information.\n", progname);
59 }
60
61 static void print_help(void)
62 {
63         const struct option *opt;
64
65         print_usage(stdout);
66
67         putchar('\n');
68         puts("Options:");
69         for (opt = lopts; opt->name; opt++) {
70                 struct lopt_help help;
71                 int w;
72
73                 if (!lopt_get_help(opt, &help))
74                         continue;
75                 help_print_option(opt, help.arg, help.desc, 20);
76         }
77         putchar('\n');
78
79         printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
80 }
81
82 static Widget early_setup(XtAppContext *app, int argc, char **argv)
83 {
84         Widget shell;
85         int opt;
86
87         /* Check for --help/--version early (before X connection) */
88         opterr = 0;
89         while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
90                 switch (opt) {
91                 case LOPT_VERSION:
92                         print_version();
93                         exit(EXIT_SUCCESS);
94                 case LOPT_HELP:
95                         print_help();
96                         exit(EXIT_SUCCESS);
97                 }
98         }
99
100         shell = XtOpenApplication(app, PROGNAME, NULL, 0,
101                                   &argc, argv, (String *)default_resources,
102                                   sessionShellWidgetClass, NULL, 0);
103
104         opterr = optind = 1;
105         while ((opt = getopt_long_only(argc, argv, "", lopts, NULL)) != -1) {
106                 switch (opt) {
107                 default:
108                         print_usage(stderr);
109                         exit(EXIT_FAILURE);
110                 }
111         }
112
113         if (argv[optind]) {
114                 fprintf(stderr, "%s: excess command-line arguments.\n",
115                                 progname);
116                 print_usage(stderr);
117                 exit(EXIT_FAILURE);
118         }
119
120         return shell;
121 }
122
123 static XtAppContext app_initialize(int argc, char **argv)
124 {
125         XtAppContext app;
126         Widget shell;
127
128         if (argc > 0)
129                 progname = argv[0];
130
131         shell = early_setup(&app, argc, argv);
132         ui_initialize(shell);
133         XtRealizeWidget(shell);
134
135         return app;
136 }
137
138 int main(int argc, char **argv)
139 {
140         XtAppMainLoop(app_initialize(argc, argv));
141         return 0;
142 }