Bug fixes for 0.20.3 pre-release 2
[wmaker-crm.git] / src / main.c
blob419ca1f1e073ddf0c23f19a774b86164a8f4fda2
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 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.
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
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.
22 #include "wconfig.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
36 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
37 * and if X_LOCALE is defined, X's locale emulating functions will be used.
38 * See Xlocale.h for more information.
40 #include <X11/Xlocale.h>
42 #include "WindowMaker.h"
43 #include "window.h"
44 #include "funcs.h"
45 #include "menu.h"
46 #include "keybind.h"
47 #include "xmodifier.h"
48 #include "defaults.h"
50 #include <proplist.h>
53 /****** Global Variables ******/
55 /* general info */
57 Display *dpy;
59 char *ProgName;
61 unsigned int ValidModMask = 0xff;
63 /* locale to use. NULL==POSIX or C */
64 char *Locale=NULL;
67 int wScreenCount=0;
69 WPreferences wPreferences;
72 proplist_t wDomainName;
73 proplist_t wAttributeDomainName;
75 WShortKey wKeyBindings[WKBD_LAST];
77 /* defaults domains */
78 WDDomain *WDWindowMaker = NULL;
79 WDDomain *WDRootMenu = NULL;
80 WDDomain *WDWindowAttributes = NULL;
83 /* XContexts */
84 XContext wWinContext;
85 XContext wAppWinContext;
86 XContext wStackContext;
88 /* Atoms */
89 Atom _XA_WM_STATE;
90 Atom _XA_WM_CHANGE_STATE;
91 Atom _XA_WM_PROTOCOLS;
92 Atom _XA_WM_TAKE_FOCUS;
93 Atom _XA_WM_DELETE_WINDOW;
94 Atom _XA_WM_SAVE_YOURSELF;
95 Atom _XA_WM_CLIENT_LEADER;
96 Atom _XA_WM_COLORMAP_WINDOWS;
97 Atom _XA_WM_COLORMAP_NOTIFY;
99 Atom _XA_GNUSTEP_WM_ATTR;
100 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
101 Atom _XA_GNUSTEP_WM_RESIZEBAR;
103 #ifdef MWM_HINTS
104 /* MWM support */
105 Atom _XA_MOTIF_WM_HINTS;
106 #endif
108 Atom _XA_WINDOWMAKER_MENU;
109 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
110 Atom _XA_WINDOWMAKER_STATE;
112 Atom _XA_WINDOWMAKER_WM_FUNCTION;
114 #ifdef OFFIX_DND
115 Atom _XA_DND_PROTOCOL;
116 Atom _XA_DND_SELECTION;
117 #endif
118 #ifdef XDE_DND
119 Atom _XA_XDE_REQUEST;
120 Atom _XA_XDE_ENTER;
121 Atom _XA_XDE_LEAVE;
122 Atom _XA_XDE_DATA_AVAILABLE;
123 Atom _XDE_FILETYPE;
124 Atom _XDE_URLTYPE;
125 #endif
128 /* cursors */
129 Cursor wCursor[WCUR_LAST];
131 /* last event timestamp for XSetInputFocus */
132 Time LastTimestamp;
133 /* timestamp on the last time we did XSetInputFocus() */
134 Time LastFocusChange;
136 #ifdef SHAPE
137 Bool wShapeSupported;
138 int wShapeEventBase;
139 #endif
142 /* special flags */
143 char WProgramState = WSTATE_NORMAL;
144 char WDelayedActionSet = 0;
146 /* temporary stuff */
147 int wVisualID = -1;
150 /******** End Global Variables *****/
152 static char *DisplayName = NULL;
153 static char **Arguments;
154 static int ArgCount;
156 extern void EventLoop();
157 extern void StartUp();
161 void
162 Restart(char *manager)
164 char *prog=NULL;
165 char *argv[MAX_RESTART_ARGS];
166 int i;
168 if (manager && manager[0]!=0) {
169 prog = argv[0] = strtok(manager, " ");
170 for (i=1; i<MAX_RESTART_ARGS; i++) {
171 argv[i]=strtok(NULL, " ");
172 if (argv[i]==NULL) {
173 break;
178 XCloseDisplay(dpy);
179 if (!prog)
180 execvp(Arguments[0], Arguments);
181 else {
182 execvp(prog, argv);
183 /* fallback */
184 execv(Arguments[0], Arguments);
186 wsyserror(_("could not exec window manager"));
187 wfatal(_("Restart failed!!!"));
188 exit(-1);
193 void
194 SetupEnvironment(WScreen *scr)
196 char *tmp, *ptr;
197 char buf[16];
199 if (wScreenCount > 1) {
200 tmp = wmalloc(strlen(DisplayName)+64);
201 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
202 ptr = strchr(strchr(tmp, ':'), '.');
203 if (ptr)
204 *ptr = 0;
205 sprintf(buf, ".%i", scr->screen);
206 strcat(tmp, buf);
207 putenv(tmp);
209 tmp = wmalloc(60);
210 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
211 scr->rcontext->attribs->colors_per_channel);
212 putenv(tmp);
217 *---------------------------------------------------------------------
218 * wAbort--
219 * Do a major cleanup and exit the program
221 *----------------------------------------------------------------------
223 void
224 wAbort(Bool dumpCore)
226 int i;
227 WScreen *scr;
229 for (i=0; i<wScreenCount; i++) {
230 scr = wScreenWithNumber(i);
231 if (scr)
232 RestoreDesktop(scr);
234 printf(_("%s aborted.\n"), ProgName);
235 if (dumpCore)
236 abort();
237 else
238 exit(1);
242 void
243 print_help()
245 printf(_("usage: %s [-options]\n"), ProgName);
246 puts(_("options:"));
247 #ifdef USECPP
248 puts(_(" -nocpp disable preprocessing of configuration files"));
249 #endif
250 puts(_(" -nodock do not open the application Dock"));
251 puts(_(" -noclip do not open the workspace Clip"));
253 puts(_(" -locale locale locale to use"));
255 puts(_(" -visualid visualid visual id of visual to use"));
256 puts(_(" -display host:dpy display to use"));
257 puts(_(" -version print version and exit"));
262 void
263 check_defaults()
265 char *path;
267 path = wdefaultspathfordomain("");
268 if (access(path, R_OK)!=0) {
269 wfatal(_("could not find user GNUstep directory (%s).\n"
270 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
271 path);
272 exit(1);
275 free(path);
280 static void
281 execInitScript()
283 char *file;
285 file = wfindfile(DEF_CONFIG_PATHS, DEF_INIT_SCRIPT);
286 if (file) {
287 if (fork()==0) {
288 close(ConnectionNumber(dpy));
290 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
291 wsyserror(_("%s:could not execute initialization script"), file);
292 exit(1);
294 free(file);
299 void
300 ExecExitScript()
302 char *file;
304 file = wfindfile(DEF_CONFIG_PATHS, DEF_EXIT_SCRIPT);
305 if (file) {
306 if (fork()==0) {
307 close(ConnectionNumber(dpy));
309 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
310 wsyserror(_("%s:could not execute exit script"), file);
311 exit(1);
313 free(file);
320 main(int argc, char **argv)
322 int i, restart=0;
323 Bool multiHead = True;
324 char *str;
325 int d, s;
327 ArgCount = argc;
328 Arguments = argv;
330 WMInitializeApplication("WindowMaker", &argc, argv);
333 ProgName = strrchr(argv[0],'/');
334 if (!ProgName)
335 ProgName = argv[0];
336 else
337 ProgName++;
340 /* check existence of Defaults DB directory */
341 check_defaults();
343 restart = 0;
345 memset(&wPreferences, 0, sizeof(WPreferences));
347 if (argc>1) {
348 for (i=1; i<argc; i++) {
349 #ifdef USECPP
350 if (strcmp(argv[i], "-nocpp")==0) {
351 wPreferences.flags.nocpp=1;
352 } else
353 #endif
354 if (strcmp(argv[i], "-nodock")==0) {
355 wPreferences.flags.nodock=1;
356 } else if (strcmp(argv[i], "-noclip")==0) {
357 wPreferences.flags.noclip=1;
358 } else if (strcmp(argv[i], "-version")==0) {
359 printf("Window Maker %s\n", VERSION);
360 exit(0);
361 } else if (strcmp(argv[i], "-global_defaults_path")==0) {
362 printf("%s/Defaults/WindowMaker", PKGDATADIR);
363 exit(0);
364 } else if (strcmp(argv[i], "-locale")==0) {
365 i++;
366 if (i>=argc) {
367 wwarning(_("too few arguments for %s"), argv[i-1]);
368 exit(0);
370 Locale = argv[i];
371 } else if (strcmp(argv[i], "-display")==0) {
372 i++;
373 if (i>=argc) {
374 wwarning(_("too few arguments for %s"), argv[i-1]);
375 exit(0);
377 DisplayName = argv[i];
378 } else if (strcmp(argv[i], "-visualid")==0) {
379 i++;
380 if (i>=argc) {
381 wwarning(_("too few arguments for %s"), argv[i-1]);
382 exit(0);
384 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
385 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
386 exit(0);
388 } else {
389 print_help();
390 exit(0);
394 #if 0
395 tmp = getenv("LANG");
396 if (tmp) {
397 if (setlocale(LC_ALL,"") == NULL) {
398 wwarning("cannot set locale %s", tmp);
399 wwarning("falling back to C locale");
400 setlocale(LC_ALL,"C");
401 Locale = NULL;
402 } else {
403 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
404 Locale = NULL;
405 else
406 Locale = tmp;
408 } else {
409 Locale = NULL;
411 #endif
412 if (!Locale) {
413 Locale = getenv("LANG");
415 setlocale(LC_ALL, Locale);
416 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
417 Locale = NULL;
418 #ifdef I18N
419 if (getenv("NLSPATH"))
420 bindtextdomain("WindowMaker", getenv("NLSPATH"));
421 else
422 bindtextdomain("WindowMaker", NLSDIR);
423 textdomain("WindowMaker");
425 if (!XSupportsLocale()) {
426 wwarning(_("X server does not support locale"));
428 if (XSetLocaleModifiers("") == NULL) {
429 wwarning(_("cannot set locale modifiers"));
431 #endif
433 if (Locale) {
434 char *ptr;
436 Locale = wstrdup(Locale);
437 ptr = strchr(Locale, '.');
438 if (ptr)
439 *ptr = 0;
443 /* open display */
444 dpy = XOpenDisplay(DisplayName);
445 if (dpy == NULL) {
446 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
447 exit(1);
450 /* check if the user specified a complete display name (with screen).
451 * If so, only manage the specified screen */
452 if (DisplayName)
453 str = strchr(DisplayName, ':');
454 else
455 str = NULL;
456 if (!str)
457 str = "";
458 if (sscanf(str, "%i.%i", &d, &s)==2)
459 multiHead = False;
461 DisplayName = XDisplayName(DisplayName);
462 str = wmalloc(strlen(DisplayName)+64);
463 sprintf(str, "DISPLAY=%s", DisplayName);
464 putenv(str);
466 #ifdef DEBUG
467 XSynchronize(dpy, True);
468 #endif
470 wXModifierInitialize();
472 #ifdef SOUNDS
473 wSoundInitialize();
474 #endif
475 StartUp(!multiHead);
477 execInitScript();
479 EventLoop();
480 return -1;