Added option to 'configure' to control debug information for compilation
[wmaker-crm.git] / src / startup.c
blobac7baa497f30c62ff65c3b6a1092b0c9d598ecc8
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 Dan Pascu
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <sys/wait.h>
31 #ifdef __FreeBSD__
32 #include <sys/signal.h>
33 #endif
35 #include <X11/Xlib.h>
36 #include <X11/Xresource.h>
37 #include <X11/Xutil.h>
38 #include <X11/cursorfont.h>
39 #include <X11/Xproto.h>
40 #include <X11/keysym.h>
41 #ifdef SHAPE
42 #include <X11/extensions/shape.h>
43 #endif
44 #ifdef KEEP_XKB_LOCK_STATUS
45 #include <X11/XKBlib.h>
46 #endif
47 #ifdef HAVE_XRANDR
48 #include <X11/extensions/Xrandr.h>
49 #endif
51 #include "WindowMaker.h"
52 #include "GNUstep.h"
53 #include "texture.h"
54 #include "screen.h"
55 #include "window.h"
56 #include "actions.h"
57 #include "client.h"
58 #include "main.h"
59 #include "dock.h"
60 #include "workspace.h"
61 #include "keybind.h"
62 #include "framewin.h"
63 #include "session.h"
64 #include "defaults.h"
65 #include "properties.h"
66 #include "dialog.h"
67 #include "wmspec.h"
68 #ifdef XDND
69 #include "xdnd.h"
70 #endif
72 #include "xutil.h"
74 /* for SunOS */
75 #ifndef SA_RESTART
76 # define SA_RESTART 0
77 #endif
79 /* Just in case, for weirdo systems */
80 #ifndef SA_NODEFER
81 # define SA_NODEFER 0
82 #endif
84 /****** Global Variables ******/
85 extern WPreferences wPreferences;
86 extern WDDomain *WDWindowMaker;
87 extern WDDomain *WDRootMenu;
88 extern WDDomain *WDWindowAttributes;
89 extern WShortKey wKeyBindings[WKBD_LAST];
90 extern int wScreenCount;
92 #ifdef SHAPE
93 extern Bool wShapeSupported;
94 extern int wShapeEventBase;
95 #endif
97 #ifdef KEEP_XKB_LOCK_STATUS
98 extern Bool wXkbSupported;
99 extern int wXkbEventBase;
100 #endif
102 #ifdef HAVE_XRANDR
103 extern Bool has_randr;
104 extern int randr_event_base;
105 #endif
107 /* contexts */
108 extern XContext wWinContext;
109 extern XContext wAppWinContext;
110 extern XContext wStackContext;
111 extern XContext wVEdgeContext;
113 /* atoms */
114 extern Atom _XA_WM_STATE;
115 extern Atom _XA_WM_CHANGE_STATE;
116 extern Atom _XA_WM_PROTOCOLS;
117 extern Atom _XA_WM_TAKE_FOCUS;
118 extern Atom _XA_WM_DELETE_WINDOW;
119 extern Atom _XA_WM_SAVE_YOURSELF;
120 extern Atom _XA_WM_CLIENT_LEADER;
121 extern Atom _XA_WM_COLORMAP_WINDOWS;
122 extern Atom _XA_WM_COLORMAP_NOTIFY;
123 extern Atom _XA_GNUSTEP_WM_ATTR;
124 extern Atom _XA_WINDOWMAKER_MENU;
125 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
126 extern Atom _XA_WINDOWMAKER_STATE;
127 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
128 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
129 extern Atom _XA_WINDOWMAKER_COMMAND;
130 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
131 extern Atom _XA_WINDOWMAKER_ICON_TILE;
132 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
133 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
134 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
136 /* cursors */
137 extern Cursor wCursor[WCUR_LAST];
139 #ifndef HAVE_INOTIFY
140 /* special flags */
141 extern char WDelayedActionSet;
142 #endif
144 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
146 /***** Local *****/
147 static WScreen **wScreen = NULL;
148 static unsigned int _NumLockMask = 0;
149 static unsigned int _ScrollLockMask = 0;
150 static void manageAllWindows(WScreen * scr, int crashed);
152 static int catchXError(Display * dpy, XErrorEvent * error)
154 char buffer[MAXLINE];
156 /* ignore some errors */
157 if (error->resourceid != None
158 && ((error->error_code == BadDrawable && error->request_code == X_GetGeometry)
159 || (error->error_code == BadMatch && (error->request_code == X_SetInputFocus))
160 || (error->error_code == BadWindow)
162 && (error->request_code == X_GetWindowAttributes
163 || error->request_code == X_SetInputFocus
164 || error->request_code == X_ChangeWindowAttributes
165 || error->request_code == X_GetProperty
166 || error->request_code == X_ChangeProperty
167 || error->request_code == X_QueryTree
168 || error->request_code == X_GrabButton
169 || error->request_code == X_UngrabButton
170 || error->request_code == X_SendEvent
171 || error->request_code == X_ConfigureWindow))
173 || (error->request_code == X_InstallColormap))) {
174 return 0;
176 FormatXError(dpy, error, buffer, MAXLINE);
177 wwarning(_("internal X error: %s"), buffer);
178 return -1;
182 *----------------------------------------------------------------------
183 * handleXIO-
184 * Handle X shutdowns and other stuff.
185 *----------------------------------------------------------------------
187 static int handleXIO(Display * xio_dpy)
189 dpy = NULL;
190 Exit(0);
191 return 0;
194 #ifndef HAVE_INOTIFY
196 *----------------------------------------------------------------------
197 * delayedAction-
198 * Action to be executed after the signal() handler is exited.
199 *----------------------------------------------------------------------
201 static void delayedAction(void *cdata)
203 if (WDelayedActionSet == 0)
204 return;
206 WDelayedActionSet--;
208 * Make the event dispatcher do whatever it needs to do,
209 * including handling zombie processes, restart and exit
210 * signals.
212 DispatchEvent(NULL);
214 #endif
217 *----------------------------------------------------------------------
218 * handleExitSig--
219 * User generated exit signal handler.
220 *----------------------------------------------------------------------
222 static RETSIGTYPE handleExitSig(int sig)
224 sigset_t sigs;
226 sigfillset(&sigs);
227 sigprocmask(SIG_BLOCK, &sigs, NULL);
229 if (sig == SIGUSR1) {
230 wwarning("got signal %i - restarting", sig);
231 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
232 } else if (sig == SIGUSR2) {
233 wwarning("got signal %i - rereading defaults", sig);
234 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
235 } else if (sig == SIGTERM || sig == SIGINT || sig == SIGHUP) {
236 wwarning("got signal %i - exiting...", sig);
237 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
240 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
241 DispatchEvent(NULL); /* Dispatch events imediately. */
244 /* Dummy signal handler */
245 static void dummyHandler(int sig)
250 *----------------------------------------------------------------------
251 * handleSig--
252 * general signal handler. Exits the program gently.
253 *----------------------------------------------------------------------
255 static RETSIGTYPE handleSig(int sig)
257 wfatal("got signal %i", sig);
259 /* Setting the signal behaviour back to default and then reraising the
260 * signal is a cleaner way to make program exit and core dump than calling
261 * abort(), since it correctly returns from the signal handler and sets
262 * the flags accordingly. -Dan
264 if (sig == SIGSEGV || sig == SIGFPE || sig == SIGBUS || sig == SIGILL || sig == SIGABRT) {
265 signal(sig, SIG_DFL);
266 kill(getpid(), sig);
267 return;
270 wAbort(0);
273 static RETSIGTYPE buryChild(int foo)
275 pid_t pid;
276 int status;
277 int save_errno = errno;
278 sigset_t sigs;
280 sigfillset(&sigs);
281 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
282 sigprocmask(SIG_BLOCK, &sigs, NULL);
284 /* R.I.P. */
285 /* If 2 or more kids exit in a small time window, before this handler gets
286 * the chance to get invoked, the SIGCHLD signals will be merged and only
287 * one SIGCHLD signal will be sent to us. We use a while loop to get all
288 * exited child status because we can't count on the number of SIGCHLD
289 * signals to know exactly how many kids have exited. -Dan
291 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || (pid < 0 && errno == EINTR)) {
292 NotifyDeadProcess(pid, WEXITSTATUS(status));
295 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
297 errno = save_errno;
300 static void getOffendingModifiers(void)
302 int i;
303 XModifierKeymap *modmap;
304 KeyCode nlock, slock;
305 static int mask_table[8] = {
306 ShiftMask, LockMask, ControlMask, Mod1Mask,
307 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
310 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
311 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
314 * Find out the masks for the NumLock and ScrollLock modifiers,
315 * so that we can bind the grabs for when they are enabled too.
317 modmap = XGetModifierMapping(dpy);
319 if (modmap != NULL && modmap->max_keypermod > 0) {
320 for (i = 0; i < 8 * modmap->max_keypermod; i++) {
321 if (modmap->modifiermap[i] == nlock && nlock != 0)
322 _NumLockMask = mask_table[i / modmap->max_keypermod];
323 else if (modmap->modifiermap[i] == slock && slock != 0)
324 _ScrollLockMask = mask_table[i / modmap->max_keypermod];
328 if (modmap)
329 XFreeModifiermap(modmap);
332 #ifdef NUMLOCK_HACK
333 void
334 wHackedGrabKey(int keycode, unsigned int modifiers,
335 Window grab_window, Bool owner_events, int pointer_mode, int keyboard_mode)
337 if (modifiers == AnyModifier)
338 return;
340 /* grab all combinations of the modifier with CapsLock, NumLock and
341 * ScrollLock. How much memory/CPU does such a monstrosity consume
342 * in the server?
344 if (_NumLockMask)
345 XGrabKey(dpy, keycode, modifiers | _NumLockMask,
346 grab_window, owner_events, pointer_mode, keyboard_mode);
347 if (_ScrollLockMask)
348 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask,
349 grab_window, owner_events, pointer_mode, keyboard_mode);
350 if (_NumLockMask && _ScrollLockMask)
351 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask,
352 grab_window, owner_events, pointer_mode, keyboard_mode);
353 if (_NumLockMask)
354 XGrabKey(dpy, keycode, modifiers | _NumLockMask | LockMask,
355 grab_window, owner_events, pointer_mode, keyboard_mode);
356 if (_ScrollLockMask)
357 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask | LockMask,
358 grab_window, owner_events, pointer_mode, keyboard_mode);
359 if (_NumLockMask && _ScrollLockMask)
360 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask | LockMask,
361 grab_window, owner_events, pointer_mode, keyboard_mode);
362 /* phew, I guess that's all, right? */
364 #endif
366 void
367 wHackedGrabButton(unsigned int button, unsigned int modifiers,
368 Window grab_window, Bool owner_events,
369 unsigned int event_mask, int pointer_mode, int keyboard_mode, Window confine_to, Cursor cursor)
371 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
372 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
374 if (modifiers == AnyModifier)
375 return;
377 XGrabButton(dpy, button, modifiers | LockMask, grab_window, owner_events,
378 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
380 #ifdef NUMLOCK_HACK
381 /* same as above, but for mouse buttons */
382 if (_NumLockMask)
383 XGrabButton(dpy, button, modifiers | _NumLockMask,
384 grab_window, owner_events, event_mask, pointer_mode,
385 keyboard_mode, confine_to, cursor);
386 if (_ScrollLockMask)
387 XGrabButton(dpy, button, modifiers | _ScrollLockMask,
388 grab_window, owner_events, event_mask, pointer_mode,
389 keyboard_mode, confine_to, cursor);
390 if (_NumLockMask && _ScrollLockMask)
391 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask,
392 grab_window, owner_events, event_mask, pointer_mode,
393 keyboard_mode, confine_to, cursor);
394 if (_NumLockMask)
395 XGrabButton(dpy, button, modifiers | _NumLockMask | LockMask,
396 grab_window, owner_events, event_mask, pointer_mode,
397 keyboard_mode, confine_to, cursor);
398 if (_ScrollLockMask)
399 XGrabButton(dpy, button, modifiers | _ScrollLockMask | LockMask,
400 grab_window, owner_events, event_mask, pointer_mode,
401 keyboard_mode, confine_to, cursor);
402 if (_NumLockMask && _ScrollLockMask)
403 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask | LockMask,
404 grab_window, owner_events, event_mask, pointer_mode,
405 keyboard_mode, confine_to, cursor);
406 #endif /* NUMLOCK_HACK */
409 WScreen *wScreenWithNumber(int i)
411 assert(i < wScreenCount);
413 return wScreen[i];
416 WScreen *wScreenForRootWindow(Window window)
418 int i;
420 if (wScreenCount == 1)
421 return wScreen[0];
424 * Since the number of heads will probably be small (normally 2),
425 * it should be faster to use this than a hash table, because
426 * of the overhead.
428 for (i = 0; i < wScreenCount; i++) {
429 if (wScreen[i]->root_win == window) {
430 return wScreen[i];
434 return wScreenForWindow(window);
437 WScreen *wScreenSearchForRootWindow(Window window)
439 int i;
441 if (wScreenCount == 1)
442 return wScreen[0];
445 * Since the number of heads will probably be small (normally 2),
446 * it should be faster to use this than a hash table, because
447 * of the overhead.
449 for (i = 0; i < wScreenCount; i++) {
450 if (wScreen[i]->root_win == window) {
451 return wScreen[i];
455 return wScreenForWindow(window);
458 WScreen *wScreenForWindow(Window window)
460 XWindowAttributes attr;
462 if (wScreenCount == 1)
463 return wScreen[0];
465 if (XGetWindowAttributes(dpy, window, &attr)) {
466 return wScreenForRootWindow(attr.root);
468 return NULL;
471 static char *atomNames[] = {
472 "WM_STATE",
473 "WM_CHANGE_STATE",
474 "WM_PROTOCOLS",
475 "WM_TAKE_FOCUS",
476 "WM_DELETE_WINDOW",
477 "WM_SAVE_YOURSELF",
478 "WM_CLIENT_LEADER",
479 "WM_COLORMAP_WINDOWS",
480 "WM_COLORMAP_NOTIFY",
482 "_WINDOWMAKER_MENU",
483 "_WINDOWMAKER_STATE",
484 "_WINDOWMAKER_WM_PROTOCOLS",
485 "_WINDOWMAKER_WM_FUNCTION",
486 "_WINDOWMAKER_NOTICEBOARD",
487 "_WINDOWMAKER_COMMAND",
488 "_WINDOWMAKER_ICON_SIZE",
489 "_WINDOWMAKER_ICON_TILE",
491 GNUSTEP_WM_ATTR_NAME,
492 GNUSTEP_WM_MINIATURIZE_WINDOW,
493 GNUSTEP_TITLEBAR_STATE,
495 "WM_IGNORE_FOCUS_EVENTS"
499 *----------------------------------------------------------
500 * StartUp--
501 * starts the window manager and setup global data.
502 * Called from main() at startup.
504 * Side effects:
505 * global data declared in main.c is initialized
506 *----------------------------------------------------------
508 void StartUp(Bool defaultScreenOnly)
510 struct sigaction sig_action;
511 int j, max;
512 #ifdef HAVE_XRANDR
513 int dummy;
514 #endif
515 Atom atom[sizeof(atomNames) / sizeof(char *)];
518 * Ignore CapsLock in modifiers
520 ValidModMask = 0xff & ~LockMask;
522 getOffendingModifiers();
524 * Ignore NumLock and ScrollLock too
526 ValidModMask &= ~(_NumLockMask | _ScrollLockMask);
528 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
530 wWinContext = XUniqueContext();
531 wAppWinContext = XUniqueContext();
532 wStackContext = XUniqueContext();
533 wVEdgeContext = XUniqueContext();
535 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False); */
537 #ifdef HAVE_XINTERNATOMS
538 XInternAtoms(dpy, atomNames, sizeof(atomNames) / sizeof(char *), False, atom);
539 #else
542 int i;
543 for (i = 0; i < sizeof(atomNames) / sizeof(char *); i++)
544 atom[i] = XInternAtom(dpy, atomNames[i], False);
546 #endif
548 _XA_WM_STATE = atom[0];
549 _XA_WM_CHANGE_STATE = atom[1];
550 _XA_WM_PROTOCOLS = atom[2];
551 _XA_WM_TAKE_FOCUS = atom[3];
552 _XA_WM_DELETE_WINDOW = atom[4];
553 _XA_WM_SAVE_YOURSELF = atom[5];
554 _XA_WM_CLIENT_LEADER = atom[6];
555 _XA_WM_COLORMAP_WINDOWS = atom[7];
556 _XA_WM_COLORMAP_NOTIFY = atom[8];
558 _XA_WINDOWMAKER_MENU = atom[9];
559 _XA_WINDOWMAKER_STATE = atom[10];
560 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
561 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
562 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
563 _XA_WINDOWMAKER_COMMAND = atom[14];
564 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
565 _XA_WINDOWMAKER_ICON_TILE = atom[16];
567 _XA_GNUSTEP_WM_ATTR = atom[17];
568 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
569 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
571 _XA_WM_IGNORE_FOCUS_EVENTS = atom[20];
573 #ifdef XDND
574 wXDNDInitializeAtoms();
575 #endif
577 /* cursors */
578 wCursor[WCUR_NORMAL] = None; /* inherit from root */
579 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
580 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
581 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
582 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
583 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
584 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
585 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
586 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
587 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
588 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
589 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
590 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
591 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name??? */
592 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
594 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
595 GC gc = XCreateGC(dpy, cur, 0, NULL);
596 XColor black;
597 memset(&black, 0, sizeof(XColor));
598 XSetForeground(dpy, gc, 0);
599 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
600 XFreeGC(dpy, gc);
601 wCursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
602 XFreePixmap(dpy, cur);
605 #ifndef HAVE_INOTIFY
606 /* signal handler stuff that gets called when a signal is caught */
607 WMAddPersistentTimerHandler(500, delayedAction, NULL);
608 #endif
610 /* emergency exit... */
611 sig_action.sa_handler = handleSig;
612 sigemptyset(&sig_action.sa_mask);
614 sig_action.sa_flags = SA_RESTART;
615 sigaction(SIGQUIT, &sig_action, NULL);
616 /* instead of catching these, we let the default handler abort the
617 * program. The new monitor process will take appropriate action
618 * when it detects the crash.
619 sigaction(SIGSEGV, &sig_action, NULL);
620 sigaction(SIGBUS, &sig_action, NULL);
621 sigaction(SIGFPE, &sig_action, NULL);
622 sigaction(SIGABRT, &sig_action, NULL);
625 sig_action.sa_handler = handleExitSig;
627 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
628 * immediately. -Dan */
629 sig_action.sa_flags = SA_RESTART;
630 sigaction(SIGTERM, &sig_action, NULL);
631 sigaction(SIGINT, &sig_action, NULL);
632 sigaction(SIGHUP, &sig_action, NULL);
633 sigaction(SIGUSR1, &sig_action, NULL);
634 sigaction(SIGUSR2, &sig_action, NULL);
636 /* ignore dead pipe */
637 /* Because POSIX mandates that only signal with handlers are reset
638 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
639 * to children. Hence the dummy handler.
640 * Philippe Troin <phil@fifi.org>
642 sig_action.sa_handler = &dummyHandler;
643 sig_action.sa_flags = SA_RESTART;
644 sigaction(SIGPIPE, &sig_action, NULL);
646 /* handle dead children */
647 sig_action.sa_handler = buryChild;
648 sig_action.sa_flags = SA_NOCLDSTOP | SA_RESTART;
649 sigaction(SIGCHLD, &sig_action, NULL);
651 /* Now we unblock all signals, that may have been blocked by the parent
652 * who exec()-ed us. This can happen for example if Window Maker crashes
653 * and restarts itself or another window manager from the signal handler.
654 * In this case, the new proccess inherits the blocked signal mask and
655 * will no longer react to that signal, until unblocked.
656 * This is because the signal handler of the proccess who crashed (parent)
657 * didn't return, and the signal remained blocked. -Dan
659 sigfillset(&sig_action.sa_mask);
660 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
662 /* handle X shutdowns a such */
663 XSetIOErrorHandler(handleXIO);
665 /* set hook for out event dispatcher in WINGs event dispatcher */
666 WMHookEventHandler(DispatchEvent);
668 /* initialize defaults stuff */
669 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
670 if (!WDWindowMaker->dictionary)
671 wwarning(_("could not read domain \"%s\" from defaults database"), "WindowMaker");
673 /* read defaults that don't change until a restart and are
674 * screen independent */
675 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
677 /* check sanity of some values */
678 if (wPreferences.icon_size < 16) {
679 wwarning(_("icon size is configured to %i, but it's too small. Using 16 instead"),
680 wPreferences.icon_size);
681 wPreferences.icon_size = 16;
684 /* init other domains */
685 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
686 if (!WDRootMenu->dictionary)
687 wwarning(_("could not read domain \"%s\" from defaults database"), "WMRootMenu");
689 wDefaultsMergeGlobalMenus(WDRootMenu);
691 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
692 if (!WDWindowAttributes->dictionary)
693 wwarning(_("could not read domain \"%s\" from defaults database"), "WMWindowAttributes");
695 XSetErrorHandler((XErrorHandler) catchXError);
697 #ifdef SHAPE
698 /* ignore j */
699 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
700 #endif
702 #ifdef HAVE_XRANDR
703 has_randr = XRRQueryExtension(dpy, &randr_event_base, &dummy);
704 #endif
706 #ifdef KEEP_XKB_LOCK_STATUS
707 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
708 if (wPreferences.modelock && !wXkbSupported) {
709 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
710 wPreferences.modelock = 0;
712 #endif
714 if (defaultScreenOnly)
715 max = 1;
716 else
717 max = ScreenCount(dpy);
719 wScreen = wmalloc(sizeof(WScreen *) * max);
721 wScreenCount = 0;
723 /* manage the screens */
724 for (j = 0; j < max; j++) {
725 if (defaultScreenOnly || max == 1) {
726 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
727 if (!wScreen[wScreenCount]) {
728 wfatal(_("it seems that there is already a window manager running"));
729 Exit(1);
731 } else {
732 wScreen[wScreenCount] = wScreenInit(j);
733 if (!wScreen[wScreenCount]) {
734 wwarning(_("could not manage screen %i"), j);
735 continue;
738 wScreenCount++;
741 InitializeSwitchMenu();
743 /* initialize/restore state for the screens */
744 for (j = 0; j < wScreenCount; j++) {
745 int lastDesktop;
747 lastDesktop = wNETWMGetCurrentDesktopFromHint(wScreen[j]);
749 wScreenRestoreState(wScreen[j]);
751 /* manage all windows that were already here before us */
752 if (!wPreferences.flags.nodock && wScreen[j]->dock)
753 wScreen[j]->last_dock = wScreen[j]->dock;
755 manageAllWindows(wScreen[j], wPreferences.flags.restarting == 2);
757 /* restore saved menus */
758 wMenuRestoreState(wScreen[j]);
760 /* If we're not restarting, restore session */
761 if (wPreferences.flags.restarting == 0 && !wPreferences.flags.norestore)
762 wSessionRestoreState(wScreen[j]);
764 if (!wPreferences.flags.noautolaunch) {
765 /* auto-launch apps */
766 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
767 wScreen[j]->last_dock = wScreen[j]->dock;
768 wDockDoAutoLaunch(wScreen[j]->dock, 0);
770 /* auto-launch apps in clip */
771 if (!wPreferences.flags.noclip) {
772 int i;
773 for (i = 0; i < wScreen[j]->workspace_count; i++) {
774 if (wScreen[j]->workspaces[i]->clip) {
775 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
776 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
782 /* go to workspace where we were before restart */
783 if (lastDesktop >= 0)
784 wWorkspaceForceChange(wScreen[j], lastDesktop);
785 else
786 wSessionRestoreLastWorkspace(wScreen[j]);
789 if (wScreenCount == 0) {
790 wfatal(_("could not manage any screen"));
791 Exit(1);
794 #ifndef HAVE_INOTIFY
795 /* setup defaults file polling */
796 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates)
797 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
798 #endif
802 static Bool windowInList(Window window, Window * list, int count)
804 for (; count >= 0; count--) {
805 if (window == list[count])
806 return True;
808 return False;
812 *-----------------------------------------------------------------------
813 * manageAllWindows--
814 * Manages all windows in the screen.
816 * Notes:
817 * Called when the wm is being started.
818 * No events can be processed while the windows are being
819 * reparented/managed.
820 *-----------------------------------------------------------------------
822 static void manageAllWindows(WScreen * scr, int crashRecovery)
824 Window root, parent;
825 Window *children;
826 unsigned int nchildren;
827 unsigned int i, j;
828 WWindow *wwin;
830 XGrabServer(dpy);
831 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
833 scr->flags.startup = 1;
835 /* first remove all icon windows */
836 for (i = 0; i < nchildren; i++) {
837 XWMHints *wmhints;
839 if (children[i] == None)
840 continue;
842 wmhints = XGetWMHints(dpy, children[i]);
843 if (wmhints && (wmhints->flags & IconWindowHint)) {
844 for (j = 0; j < nchildren; j++) {
845 if (children[j] == wmhints->icon_window) {
846 XFree(wmhints);
847 wmhints = NULL;
848 children[j] = None;
849 break;
853 if (wmhints) {
854 XFree(wmhints);
858 for (i = 0; i < nchildren; i++) {
859 if (children[i] == None)
860 continue;
862 wwin = wManageWindow(scr, children[i]);
863 if (wwin) {
864 /* apply states got from WSavedState */
865 /* shaded + minimized is not restored correctly */
866 if (wwin->flags.shaded) {
867 wwin->flags.shaded = 0;
868 wShadeWindow(wwin);
870 if (wwin->flags.miniaturized
871 && (wwin->transient_for == None
872 || wwin->transient_for == scr->root_win
873 || !windowInList(wwin->transient_for, children, nchildren))) {
875 wwin->flags.skip_next_animation = 1;
876 wwin->flags.miniaturized = 0;
877 wIconifyWindow(wwin);
878 } else {
879 wClientSetState(wwin, NormalState, None);
881 if (crashRecovery) {
882 int border;
884 border = (!HAS_BORDER(wwin) ? 0 : FRAME_BORDER_WIDTH);
886 wWindowMove(wwin, wwin->frame_x - border,
887 wwin->frame_y - border -
888 (wwin->frame->titlebar ? wwin->frame->titlebar->height : 0));
892 XUngrabServer(dpy);
894 /* hide apps */
895 wwin = scr->focused_window;
896 while (wwin) {
897 if (wwin->flags.hidden) {
898 WApplication *wapp = wApplicationOf(wwin->main_window);
900 if (wapp) {
901 wwin->flags.hidden = 0;
902 wHideApplication(wapp);
903 } else {
904 wwin->flags.hidden = 0;
907 wwin = wwin->prev;
910 XFree(children);
911 scr->flags.startup = 0;
912 scr->flags.startup2 = 1;
914 while (XPending(dpy)) {
915 XEvent ev;
916 WMNextEvent(dpy, &ev);
917 WMHandleEvent(&ev);
919 wWorkspaceForceChange(scr, 0);
920 if (!wPreferences.flags.noclip)
921 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
922 scr->flags.startup2 = 0;