Factorize duplicate run command code
[wmaker-crm.git] / WPrefs.app / main.c
blobd2dd77d9e128e8995da26e4e542dc21b9b4a6662
1 /*
2 * WPrefs - Window Maker Preferences Program
4 * Copyright (c) 1998-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "config.h"
23 #include "WPrefs.h"
25 #include <assert.h>
27 #include <X11/Xlocale.h>
28 #include <X11/XKBlib.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
33 #ifdef HAVE_STDNORETURN
34 #include <stdnoreturn.h>
35 #endif
37 char *NOptionValueChanged = "NOptionValueChanged";
38 Bool xext_xkb_supported = False;
41 #define MAX_DEATHS 64
43 struct {
44 pid_t pid;
45 void *data;
46 void (*handler) (void *);
47 } DeadHandlers[MAX_DEATHS];
49 static pid_t DeadChildren[MAX_DEATHS];
50 static int DeadChildrenCount = 0;
52 static noreturn void wAbort(Bool foo)
54 /* Parameter not used, but tell the compiler that it is ok */
55 (void) foo;
57 exit(1);
60 static void print_help(const char *progname)
62 printf(_("usage: %s [options]\n"), progname);
63 puts(_("options:"));
64 puts(_(" -display <display> display to be used"));
65 puts(_(" --version print version number and exit"));
66 puts(_(" --help print this message and exit"));
69 void AddDeadChildHandler(pid_t pid, void (*handler) (void *), void *data)
71 int i;
73 for (i = 0; i < MAX_DEATHS; i++) {
74 if (DeadHandlers[i].pid == 0) {
75 DeadHandlers[i].pid = pid;
76 DeadHandlers[i].handler = handler;
77 DeadHandlers[i].data = data;
78 break;
81 assert(i != MAX_DEATHS);
84 int main(int argc, char **argv)
86 Display *dpy;
87 WMScreen *scr;
88 char *path;
89 int i;
90 char *display_name = "";
92 wsetabort(wAbort);
94 memset(DeadHandlers, 0, sizeof(DeadHandlers));
96 WMInitializeApplication("WPrefs", &argc, argv);
98 WMSetResourcePath(RESOURCE_PATH);
99 path = WMPathForResourceOfType("WPrefs.tiff", NULL);
100 if (!path) {
101 /* maybe it is run directly from the source directory */
102 WMSetResourcePath(".");
103 path = WMPathForResourceOfType("WPrefs.tiff", NULL);
104 if (!path) {
105 WMSetResourcePath("..");
108 if (path) {
109 wfree(path);
112 if (argc > 1) {
113 for (i = 1; i < argc; i++) {
114 if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
115 printf("WPrefs (Window Maker) %s\n", VERSION);
116 exit(0);
117 } else if (strcmp(argv[i], "-display") == 0) {
118 i++;
119 if (i >= argc) {
120 wwarning(_("too few arguments for %s"), argv[i - 1]);
121 exit(0);
123 display_name = argv[i];
124 } else {
125 print_help(argv[0]);
126 exit(0);
131 setlocale(LC_ALL, "");
133 #ifdef I18N
134 if (getenv("NLSPATH"))
135 bindtextdomain("WPrefs", getenv("NLSPATH"));
136 else
137 bindtextdomain("WPrefs", LOCALEDIR);
138 bind_textdomain_codeset("WPrefs", "UTF-8");
139 textdomain("WPrefs");
141 if (!XSupportsLocale()) {
142 wwarning(_("X server does not support locale"));
144 if (XSetLocaleModifiers("") == NULL) {
145 wwarning(_("cannot set locale modifiers"));
147 #endif
149 dpy = XOpenDisplay(display_name);
150 if (!dpy) {
151 wfatal(_("could not open display %s"), XDisplayName(display_name));
152 exit(0);
154 scr = WMCreateScreen(dpy, DefaultScreen(dpy));
155 if (!scr) {
156 wfatal(_("could not initialize application"));
157 exit(0);
160 xext_xkb_supported = XkbQueryExtension(dpy, NULL, NULL, NULL, NULL, NULL);
162 WMPLSetCaseSensitive(False);
164 Initialize(scr);
166 while (1) {
167 XEvent event;
169 WMNextEvent(dpy, &event);
171 while (DeadChildrenCount-- > 0) {
172 int i;
174 for (i = 0; i < MAX_DEATHS; i++) {
175 if (DeadChildren[i] == DeadHandlers[i].pid) {
176 (*DeadHandlers[i].handler) (DeadHandlers[i].data);
177 DeadHandlers[i].pid = 0;
182 WMHandleEvent(&event);