Change to the linux kernel coding style
[wmaker-crm.git] / WPrefs.app / main.c
1 /*
2 * WPrefs - Window Maker Preferences Program
3 *
4 * Copyright (c) 1998-2003 Alfredo K. Kojima
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
20 */
21
22 #include "WPrefs.h"
23
24 #include <assert.h>
25
26 #include <X11/Xlocale.h>
27
28 #include <sys/wait.h>
29 #include <unistd.h>
30
31 char *NOptionValueChanged = "NOptionValueChanged";
32
33 extern void Initialize(WMScreen * scr);
34
35 #define MAX_DEATHS 64
36
37 struct {
38 pid_t pid;
39 void *data;
40 void (*handler) (void *);
41 } DeadHandlers[MAX_DEATHS];
42
43 static pid_t DeadChildren[MAX_DEATHS];
44 static int DeadChildrenCount = 0;
45
46 void wAbort(Bool foo)
47 {
48 exit(1);
49 }
50
51 static void print_help(char *progname)
52 {
53 printf(_("usage: %s [options]\n"), progname);
54 puts(_("options:"));
55 puts(_(" -display <display> display to be used"));
56 puts(_(" --version print version number and exit"));
57 puts(_(" --help print this message and exit"));
58 }
59
60 #if 0
61 static RETSIGTYPE handleDeadChild(int sig)
62 {
63 pid_t pid;
64 int status;
65
66 pid = waitpid(-1, &status, WNOHANG);
67 if (pid > 0) {
68 DeadChildren[DeadChildrenCount++] = pid;
69 }
70 }
71 #endif
72
73 void AddDeadChildHandler(pid_t pid, void (*handler) (void *), void *data)
74 {
75 int i;
76
77 for (i = 0; i < MAX_DEATHS; i++) {
78 if (DeadHandlers[i].pid == 0) {
79 DeadHandlers[i].pid = pid;
80 DeadHandlers[i].handler = handler;
81 DeadHandlers[i].data = data;
82 break;
83 }
84 }
85 assert(i != MAX_DEATHS);
86 }
87
88 int main(int argc, char **argv)
89 {
90 Display *dpy;
91 WMScreen *scr;
92 char *locale, *path;
93 int i;
94 char *display_name = "";
95
96 wsetabort(wAbort);
97
98 memset(DeadHandlers, 0, sizeof(DeadHandlers));
99
100 WMInitializeApplication("WPrefs", &argc, argv);
101
102 WMSetResourcePath(RESOURCE_PATH);
103 path = WMPathForResourceOfType("WPrefs.tiff", NULL);
104 if (!path) {
105 /* maybe it is run directly from the source directory */
106 WMSetResourcePath(".");
107 path = WMPathForResourceOfType("WPrefs.tiff", NULL);
108 if (!path) {
109 WMSetResourcePath("..");
110 }
111 }
112 if (path) {
113 wfree(path);
114 }
115
116 if (argc > 1) {
117 for (i = 1; i < argc; i++) {
118 if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
119 printf("WPrefs (Window Maker) %s\n", VERSION);
120 exit(0);
121 } else if (strcmp(argv[i], "-display") == 0) {
122 i++;
123 if (i >= argc) {
124 wwarning(_("too few arguments for %s"), argv[i - 1]);
125 exit(0);
126 }
127 display_name = argv[i];
128 } else {
129 print_help(argv[0]);
130 exit(0);
131 }
132 }
133 }
134
135 locale = getenv("LANG");
136 setlocale(LC_ALL, "");
137
138 #ifdef I18N
139 if (getenv("NLSPATH"))
140 bindtextdomain("WPrefs", getenv("NLSPATH"));
141 else
142 bindtextdomain("WPrefs", LOCALEDIR);
143 bind_textdomain_codeset("WPrefs", "UTF-8");
144 textdomain("WPrefs");
145
146 if (!XSupportsLocale()) {
147 wwarning(_("X server does not support locale"));
148 }
149 if (XSetLocaleModifiers("") == NULL) {
150 wwarning(_("cannot set locale modifiers"));
151 }
152 #endif
153
154 dpy = XOpenDisplay(display_name);
155 if (!dpy) {
156 wfatal(_("could not open display %s"), XDisplayName(display_name));
157 exit(0);
158 }
159 #if 0
160 XSynchronize(dpy, 1);
161 #endif
162 scr = WMCreateScreen(dpy, DefaultScreen(dpy));
163 if (!scr) {
164 wfatal(_("could not initialize application"));
165 exit(0);
166 }
167
168 WMPLSetCaseSensitive(False);
169
170 Initialize(scr);
171
172 while (1) {
173 XEvent event;
174
175 WMNextEvent(dpy, &event);
176
177 while (DeadChildrenCount-- > 0) {
178 int i;
179
180 for (i = 0; i < MAX_DEATHS; i++) {
181 if (DeadChildren[i] == DeadHandlers[i].pid) {
182 (*DeadHandlers[i].handler) (DeadHandlers[i].data);
183 DeadHandlers[i].pid = 0;
184 }
185 }
186 }
187
188 WMHandleEvent(&event);
189 }
190 }