Prevent crash when switchpanel is not initialised.
[wmaker-crm.git] / src / startup.c
blob94f88aa0745dd779bde76181c8be838cd25f0352
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 "funcs.h"
60 #include "dock.h"
61 #include "workspace.h"
62 #include "keybind.h"
63 #include "framewin.h"
64 #include "session.h"
65 #include "defaults.h"
66 #include "properties.h"
67 #include "dialog.h"
68 #include "wmspec.h"
69 #ifdef XDND
70 #include "xdnd.h"
71 #endif
73 #include "xutil.h"
75 /* for SunOS */
76 #ifndef SA_RESTART
77 # define SA_RESTART 0
78 #endif
80 /* Just in case, for weirdo systems */
81 #ifndef SA_NODEFER
82 # define SA_NODEFER 0
83 #endif
85 /****** Global Variables ******/
86 extern WPreferences wPreferences;
87 extern WDDomain *WDWindowMaker;
88 extern WDDomain *WDRootMenu;
89 extern WDDomain *WDWindowAttributes;
90 extern WShortKey wKeyBindings[WKBD_LAST];
91 extern int wScreenCount;
93 #ifdef SHAPE
94 extern Bool wShapeSupported;
95 extern int wShapeEventBase;
96 #endif
98 #ifdef KEEP_XKB_LOCK_STATUS
99 extern Bool wXkbSupported;
100 extern int wXkbEventBase;
101 #endif
103 /* contexts */
104 extern XContext wWinContext;
105 extern XContext wAppWinContext;
106 extern XContext wStackContext;
107 extern XContext wVEdgeContext;
109 /* atoms */
110 extern Atom _XA_WM_STATE;
111 extern Atom _XA_WM_CHANGE_STATE;
112 extern Atom _XA_WM_PROTOCOLS;
113 extern Atom _XA_WM_TAKE_FOCUS;
114 extern Atom _XA_WM_DELETE_WINDOW;
115 extern Atom _XA_WM_SAVE_YOURSELF;
116 extern Atom _XA_WM_CLIENT_LEADER;
117 extern Atom _XA_WM_COLORMAP_WINDOWS;
118 extern Atom _XA_WM_COLORMAP_NOTIFY;
119 extern Atom _XA_GNUSTEP_WM_ATTR;
120 extern Atom _XA_WINDOWMAKER_MENU;
121 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
122 extern Atom _XA_WINDOWMAKER_STATE;
123 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
124 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
125 extern Atom _XA_WINDOWMAKER_COMMAND;
126 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
127 extern Atom _XA_WINDOWMAKER_ICON_TILE;
128 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
129 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
130 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
132 /* cursors */
133 extern Cursor wCursor[WCUR_LAST];
135 #ifndef HAVE_INOTIFY
136 /* special flags */
137 extern char WDelayedActionSet;
138 #endif
140 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
142 /***** Local *****/
143 static WScreen **wScreen = NULL;
144 static unsigned int _NumLockMask = 0;
145 static unsigned int _ScrollLockMask = 0;
146 static void manageAllWindows(WScreen * scr, int crashed);
148 static int catchXError(Display * dpy, XErrorEvent * error)
150 char buffer[MAXLINE];
152 /* ignore some errors */
153 if (error->resourceid != None
154 && ((error->error_code == BadDrawable && error->request_code == X_GetGeometry)
155 || (error->error_code == BadMatch && (error->request_code == X_SetInputFocus))
156 || (error->error_code == BadWindow)
158 && (error->request_code == X_GetWindowAttributes
159 || error->request_code == X_SetInputFocus
160 || error->request_code == X_ChangeWindowAttributes
161 || error->request_code == X_GetProperty
162 || error->request_code == X_ChangeProperty
163 || error->request_code == X_QueryTree
164 || error->request_code == X_GrabButton
165 || error->request_code == X_UngrabButton
166 || error->request_code == X_SendEvent
167 || error->request_code == X_ConfigureWindow))
169 || (error->request_code == X_InstallColormap))) {
170 return 0;
172 FormatXError(dpy, error, buffer, MAXLINE);
173 wwarning(_("internal X error: %s"), buffer);
174 return -1;
178 *----------------------------------------------------------------------
179 * handleXIO-
180 * Handle X shutdowns and other stuff.
181 *----------------------------------------------------------------------
183 static int handleXIO(Display * xio_dpy)
185 dpy = NULL;
186 Exit(0);
187 return 0;
190 #ifndef HAVE_INOTIFY
192 *----------------------------------------------------------------------
193 * delayedAction-
194 * Action to be executed after the signal() handler is exited.
195 *----------------------------------------------------------------------
197 static void delayedAction(void *cdata)
199 if (WDelayedActionSet == 0)
200 return;
202 WDelayedActionSet--;
204 * Make the event dispatcher do whatever it needs to do,
205 * including handling zombie processes, restart and exit
206 * signals.
208 DispatchEvent(NULL);
210 #endif
213 *----------------------------------------------------------------------
214 * handleExitSig--
215 * User generated exit signal handler.
216 *----------------------------------------------------------------------
218 static RETSIGTYPE handleExitSig(int sig)
220 sigset_t sigs;
222 sigfillset(&sigs);
223 sigprocmask(SIG_BLOCK, &sigs, NULL);
225 if (sig == SIGUSR1) {
226 wwarning("got signal %i - restarting", sig);
227 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
228 } else if (sig == SIGUSR2) {
229 wwarning("got signal %i - rereading defaults", sig);
230 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
231 } else if (sig == SIGTERM || sig == SIGINT || sig == SIGHUP) {
232 wwarning("got signal %i - exiting...", sig);
233 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
236 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
237 DispatchEvent(NULL); /* Dispatch events imediately. */
240 /* Dummy signal handler */
241 static void dummyHandler(int sig)
246 *----------------------------------------------------------------------
247 * handleSig--
248 * general signal handler. Exits the program gently.
249 *----------------------------------------------------------------------
251 static RETSIGTYPE handleSig(int sig)
253 wfatal("got signal %i", sig);
255 /* Setting the signal behaviour back to default and then reraising the
256 * signal is a cleaner way to make program exit and core dump than calling
257 * abort(), since it correctly returns from the signal handler and sets
258 * the flags accordingly. -Dan
260 if (sig == SIGSEGV || sig == SIGFPE || sig == SIGBUS || sig == SIGILL || sig == SIGABRT) {
261 signal(sig, SIG_DFL);
262 kill(getpid(), sig);
263 return;
266 wAbort(0);
269 static RETSIGTYPE buryChild(int foo)
271 pid_t pid;
272 int status;
273 int save_errno = errno;
274 sigset_t sigs;
276 sigfillset(&sigs);
277 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
278 sigprocmask(SIG_BLOCK, &sigs, NULL);
280 /* R.I.P. */
281 /* If 2 or more kids exit in a small time window, before this handler gets
282 * the chance to get invoked, the SIGCHLD signals will be merged and only
283 * one SIGCHLD signal will be sent to us. We use a while loop to get all
284 * exited child status because we can't count on the number of SIGCHLD
285 * signals to know exactly how many kids have exited. -Dan
287 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || (pid < 0 && errno == EINTR)) {
288 NotifyDeadProcess(pid, WEXITSTATUS(status));
291 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
293 errno = save_errno;
296 static void getOffendingModifiers(void)
298 int i;
299 XModifierKeymap *modmap;
300 KeyCode nlock, slock;
301 static int mask_table[8] = {
302 ShiftMask, LockMask, ControlMask, Mod1Mask,
303 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
306 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
307 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
310 * Find out the masks for the NumLock and ScrollLock modifiers,
311 * so that we can bind the grabs for when they are enabled too.
313 modmap = XGetModifierMapping(dpy);
315 if (modmap != NULL && modmap->max_keypermod > 0) {
316 for (i = 0; i < 8 * modmap->max_keypermod; i++) {
317 if (modmap->modifiermap[i] == nlock && nlock != 0)
318 _NumLockMask = mask_table[i / modmap->max_keypermod];
319 else if (modmap->modifiermap[i] == slock && slock != 0)
320 _ScrollLockMask = mask_table[i / modmap->max_keypermod];
324 if (modmap)
325 XFreeModifiermap(modmap);
328 #ifdef NUMLOCK_HACK
329 void
330 wHackedGrabKey(int keycode, unsigned int modifiers,
331 Window grab_window, Bool owner_events, int pointer_mode, int keyboard_mode)
333 if (modifiers == AnyModifier)
334 return;
336 /* grab all combinations of the modifier with CapsLock, NumLock and
337 * ScrollLock. How much memory/CPU does such a monstrosity consume
338 * in the server?
340 if (_NumLockMask)
341 XGrabKey(dpy, keycode, modifiers | _NumLockMask,
342 grab_window, owner_events, pointer_mode, keyboard_mode);
343 if (_ScrollLockMask)
344 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask,
345 grab_window, owner_events, pointer_mode, keyboard_mode);
346 if (_NumLockMask && _ScrollLockMask)
347 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask,
348 grab_window, owner_events, pointer_mode, keyboard_mode);
349 if (_NumLockMask)
350 XGrabKey(dpy, keycode, modifiers | _NumLockMask | LockMask,
351 grab_window, owner_events, pointer_mode, keyboard_mode);
352 if (_ScrollLockMask)
353 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask | LockMask,
354 grab_window, owner_events, pointer_mode, keyboard_mode);
355 if (_NumLockMask && _ScrollLockMask)
356 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask | LockMask,
357 grab_window, owner_events, pointer_mode, keyboard_mode);
358 /* phew, I guess that's all, right? */
360 #endif
362 void
363 wHackedGrabButton(unsigned int button, unsigned int modifiers,
364 Window grab_window, Bool owner_events,
365 unsigned int event_mask, int pointer_mode, int keyboard_mode, Window confine_to, Cursor cursor)
367 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
368 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
370 if (modifiers == AnyModifier)
371 return;
373 XGrabButton(dpy, button, modifiers | LockMask, grab_window, owner_events,
374 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
376 #ifdef NUMLOCK_HACK
377 /* same as above, but for mouse buttons */
378 if (_NumLockMask)
379 XGrabButton(dpy, button, modifiers | _NumLockMask,
380 grab_window, owner_events, event_mask, pointer_mode,
381 keyboard_mode, confine_to, cursor);
382 if (_ScrollLockMask)
383 XGrabButton(dpy, button, modifiers | _ScrollLockMask,
384 grab_window, owner_events, event_mask, pointer_mode,
385 keyboard_mode, confine_to, cursor);
386 if (_NumLockMask && _ScrollLockMask)
387 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask,
388 grab_window, owner_events, event_mask, pointer_mode,
389 keyboard_mode, confine_to, cursor);
390 if (_NumLockMask)
391 XGrabButton(dpy, button, modifiers | _NumLockMask | LockMask,
392 grab_window, owner_events, event_mask, pointer_mode,
393 keyboard_mode, confine_to, cursor);
394 if (_ScrollLockMask)
395 XGrabButton(dpy, button, modifiers | _ScrollLockMask | LockMask,
396 grab_window, owner_events, event_mask, pointer_mode,
397 keyboard_mode, confine_to, cursor);
398 if (_NumLockMask && _ScrollLockMask)
399 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask | LockMask,
400 grab_window, owner_events, event_mask, pointer_mode,
401 keyboard_mode, confine_to, cursor);
402 #endif /* NUMLOCK_HACK */
405 WScreen *wScreenWithNumber(int i)
407 assert(i < wScreenCount);
409 return wScreen[i];
412 WScreen *wScreenForRootWindow(Window window)
414 int i;
416 if (wScreenCount == 1)
417 return wScreen[0];
419 /* Since the number of heads will probably be small (normally 2),
420 * it should be faster to use this than a hash table, because
421 * of the overhead. */
422 for (i = 0; i < wScreenCount; i++)
423 if (wScreen[i]->root_win == window)
424 return wScreen[i];
426 return wScreenForWindow(window);
429 WScreen *wScreenForWindow(Window window)
431 XWindowAttributes attr;
433 if (wScreenCount == 1)
434 return wScreen[0];
436 if (XGetWindowAttributes(dpy, window, &attr))
437 return wScreenForRootWindow(attr.root);
439 return NULL;
442 static char *atomNames[] = {
443 "WM_STATE",
444 "WM_CHANGE_STATE",
445 "WM_PROTOCOLS",
446 "WM_TAKE_FOCUS",
447 "WM_DELETE_WINDOW",
448 "WM_SAVE_YOURSELF",
449 "WM_CLIENT_LEADER",
450 "WM_COLORMAP_WINDOWS",
451 "WM_COLORMAP_NOTIFY",
453 "_WINDOWMAKER_MENU",
454 "_WINDOWMAKER_STATE",
455 "_WINDOWMAKER_WM_PROTOCOLS",
456 "_WINDOWMAKER_WM_FUNCTION",
457 "_WINDOWMAKER_NOTICEBOARD",
458 "_WINDOWMAKER_COMMAND",
459 "_WINDOWMAKER_ICON_SIZE",
460 "_WINDOWMAKER_ICON_TILE",
462 GNUSTEP_WM_ATTR_NAME,
463 GNUSTEP_WM_MINIATURIZE_WINDOW,
464 GNUSTEP_TITLEBAR_STATE,
466 "WM_IGNORE_FOCUS_EVENTS"
470 *----------------------------------------------------------
471 * StartUp--
472 * starts the window manager and setup global data.
473 * Called from main() at startup.
475 * Side effects:
476 * global data declared in main.c is initialized
477 *----------------------------------------------------------
479 void StartUp(Bool defaultScreenOnly)
481 struct sigaction sig_action;
482 int j, max;
483 #ifdef HAVE_XRANDR
484 int dummy;
485 #endif
486 Atom atom[sizeof(atomNames) / sizeof(atomNames[0])];
489 * Ignore CapsLock in modifiers
491 ValidModMask = 0xff & ~LockMask;
493 getOffendingModifiers();
495 * Ignore NumLock and ScrollLock too
497 ValidModMask &= ~(_NumLockMask | _ScrollLockMask);
499 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
501 wWinContext = XUniqueContext();
502 wAppWinContext = XUniqueContext();
503 wStackContext = XUniqueContext();
504 wVEdgeContext = XUniqueContext();
506 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False); */
508 #ifdef HAVE_XINTERNATOMS
509 XInternAtoms(dpy, atomNames, sizeof(atomNames) / sizeof(atomNames[0]), False, atom);
510 #else
513 int i;
514 for (i = 0; i < sizeof(atomNames) / sizeof(atomNames[0]); i++)
515 atom[i] = XInternAtom(dpy, atomNames[i], False);
517 #endif
519 _XA_WM_STATE = atom[0];
520 _XA_WM_CHANGE_STATE = atom[1];
521 _XA_WM_PROTOCOLS = atom[2];
522 _XA_WM_TAKE_FOCUS = atom[3];
523 _XA_WM_DELETE_WINDOW = atom[4];
524 _XA_WM_SAVE_YOURSELF = atom[5];
525 _XA_WM_CLIENT_LEADER = atom[6];
526 _XA_WM_COLORMAP_WINDOWS = atom[7];
527 _XA_WM_COLORMAP_NOTIFY = atom[8];
529 _XA_WINDOWMAKER_MENU = atom[9];
530 _XA_WINDOWMAKER_STATE = atom[10];
531 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
532 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
533 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
534 _XA_WINDOWMAKER_COMMAND = atom[14];
535 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
536 _XA_WINDOWMAKER_ICON_TILE = atom[16];
538 _XA_GNUSTEP_WM_ATTR = atom[17];
539 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
540 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
542 _XA_WM_IGNORE_FOCUS_EVENTS = atom[20];
544 #ifdef XDND
545 wXDNDInitializeAtoms();
546 #endif
548 /* cursors */
549 wCursor[WCUR_NORMAL] = None; /* inherit from root */
550 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
551 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
552 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
553 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
554 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
555 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
556 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
557 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
558 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
559 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
560 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
561 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
562 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name??? */
563 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
565 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
566 GC gc = XCreateGC(dpy, cur, 0, NULL);
567 XColor black;
568 memset(&black, 0, sizeof(XColor));
569 XSetForeground(dpy, gc, 0);
570 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
571 XFreeGC(dpy, gc);
572 wCursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
573 XFreePixmap(dpy, cur);
576 #ifndef HAVE_INOTIFY
577 /* signal handler stuff that gets called when a signal is caught */
578 WMAddPersistentTimerHandler(500, delayedAction, NULL);
579 #endif
581 /* emergency exit... */
582 sig_action.sa_handler = handleSig;
583 sigemptyset(&sig_action.sa_mask);
585 sig_action.sa_flags = SA_RESTART;
586 sigaction(SIGQUIT, &sig_action, NULL);
587 /* instead of catching these, we let the default handler abort the
588 * program. The new monitor process will take appropriate action
589 * when it detects the crash.
590 sigaction(SIGSEGV, &sig_action, NULL);
591 sigaction(SIGBUS, &sig_action, NULL);
592 sigaction(SIGFPE, &sig_action, NULL);
593 sigaction(SIGABRT, &sig_action, NULL);
596 sig_action.sa_handler = handleExitSig;
598 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
599 * immediately. -Dan */
600 sig_action.sa_flags = SA_RESTART;
601 sigaction(SIGTERM, &sig_action, NULL);
602 sigaction(SIGINT, &sig_action, NULL);
603 sigaction(SIGHUP, &sig_action, NULL);
604 sigaction(SIGUSR1, &sig_action, NULL);
605 sigaction(SIGUSR2, &sig_action, NULL);
607 /* ignore dead pipe */
608 /* Because POSIX mandates that only signal with handlers are reset
609 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
610 * to children. Hence the dummy handler.
611 * Philippe Troin <phil@fifi.org>
613 sig_action.sa_handler = &dummyHandler;
614 sig_action.sa_flags = SA_RESTART;
615 sigaction(SIGPIPE, &sig_action, NULL);
617 /* handle dead children */
618 sig_action.sa_handler = buryChild;
619 sig_action.sa_flags = SA_NOCLDSTOP | SA_RESTART;
620 sigaction(SIGCHLD, &sig_action, NULL);
622 /* Now we unblock all signals, that may have been blocked by the parent
623 * who exec()-ed us. This can happen for example if Window Maker crashes
624 * and restarts itself or another window manager from the signal handler.
625 * In this case, the new proccess inherits the blocked signal mask and
626 * will no longer react to that signal, until unblocked.
627 * This is because the signal handler of the proccess who crashed (parent)
628 * didn't return, and the signal remained blocked. -Dan
630 sigfillset(&sig_action.sa_mask);
631 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
633 /* handle X shutdowns a such */
634 XSetIOErrorHandler(handleXIO);
636 /* set hook for out event dispatcher in WINGs event dispatcher */
637 WMHookEventHandler(DispatchEvent);
639 /* initialize defaults stuff */
640 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
641 if (!WDWindowMaker->dictionary)
642 wwarning(_("could not read domain \"%s\" from defaults database"), "WindowMaker");
644 /* read defaults that don't change until a restart and are
645 * screen independent */
646 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
648 /* check sanity of some values */
649 if (wPreferences.icon_size < 16) {
650 wwarning(_("icon size is configured to %i, but it's too small. Using 16 instead"),
651 wPreferences.icon_size);
652 wPreferences.icon_size = 16;
655 /* init other domains */
656 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
657 if (!WDRootMenu->dictionary)
658 wwarning(_("could not read domain \"%s\" from defaults database"), "WMRootMenu");
660 wDefaultsMergeGlobalMenus(WDRootMenu);
662 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
663 if (!WDWindowAttributes->dictionary)
664 wwarning(_("could not read domain \"%s\" from defaults database"), "WMWindowAttributes");
666 XSetErrorHandler((XErrorHandler) catchXError);
668 #ifdef SHAPE
669 /* ignore j */
670 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
671 #endif
673 #ifdef HAVE_XRANDR
674 has_randr = XRRQueryExtension(dpy, &randr_event_base, &dummy);
675 #endif
677 #ifdef KEEP_XKB_LOCK_STATUS
678 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
679 if (wPreferences.modelock && !wXkbSupported) {
680 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
681 wPreferences.modelock = 0;
683 #endif
685 if (defaultScreenOnly)
686 max = 1;
687 else
688 max = ScreenCount(dpy);
690 wScreen = wmalloc(sizeof(WScreen *) * max);
692 wScreenCount = 0;
694 /* manage the screens */
695 for (j = 0; j < max; j++) {
696 if (defaultScreenOnly || max == 1) {
697 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
698 if (!wScreen[wScreenCount]) {
699 wfatal(_("it seems that there is already a window manager running"));
700 Exit(1);
702 } else {
703 wScreen[wScreenCount] = wScreenInit(j);
704 if (!wScreen[wScreenCount]) {
705 wwarning(_("could not manage screen %i"), j);
706 continue;
709 wScreenCount++;
712 InitializeSwitchMenu();
714 /* initialize/restore state for the screens */
715 for (j = 0; j < wScreenCount; j++) {
716 int lastDesktop;
718 lastDesktop = wNETWMGetCurrentDesktopFromHint(wScreen[j]);
720 wScreenRestoreState(wScreen[j]);
722 /* manage all windows that were already here before us */
723 if (!wPreferences.flags.nodock && wScreen[j]->dock)
724 wScreen[j]->last_dock = wScreen[j]->dock;
726 manageAllWindows(wScreen[j], wPreferences.flags.restarting == 2);
728 /* restore saved menus */
729 wMenuRestoreState(wScreen[j]);
731 /* If we're not restarting, restore session */
732 if (wPreferences.flags.restarting == 0 && !wPreferences.flags.norestore)
733 wSessionRestoreState(wScreen[j]);
735 if (!wPreferences.flags.noautolaunch) {
736 /* auto-launch apps */
737 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
738 wScreen[j]->last_dock = wScreen[j]->dock;
739 wDockDoAutoLaunch(wScreen[j]->dock, 0);
741 /* auto-launch apps in clip */
742 if (!wPreferences.flags.noclip) {
743 int i;
744 for (i = 0; i < wScreen[j]->workspace_count; i++) {
745 if (wScreen[j]->workspaces[i]->clip) {
746 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
747 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
751 /* auto-launch apps in drawers */
752 if (!wPreferences.flags.nodrawer) {
753 WDrawerChain *dc;
754 for (dc = wScreen[j]->drawers; dc; dc = dc->next) {
755 wScreen[j]->last_dock = dc->adrawer;
756 wDockDoAutoLaunch(dc->adrawer, 0);
761 /* go to workspace where we were before restart */
762 if (lastDesktop >= 0)
763 wWorkspaceForceChange(wScreen[j], lastDesktop);
764 else
765 wSessionRestoreLastWorkspace(wScreen[j]);
768 if (wScreenCount == 0) {
769 wfatal(_("could not manage any screen"));
770 Exit(1);
773 #ifndef HAVE_INOTIFY
774 /* setup defaults file polling */
775 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates)
776 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
777 #endif
781 static Bool windowInList(Window window, Window * list, int count)
783 for (; count >= 0; count--) {
784 if (window == list[count])
785 return True;
787 return False;
791 *-----------------------------------------------------------------------
792 * manageAllWindows--
793 * Manages all windows in the screen.
795 * Notes:
796 * Called when the wm is being started.
797 * No events can be processed while the windows are being
798 * reparented/managed.
799 *-----------------------------------------------------------------------
801 static void manageAllWindows(WScreen * scr, int crashRecovery)
803 Window root, parent;
804 Window *children;
805 unsigned int nchildren;
806 unsigned int i, j;
807 WWindow *wwin;
809 XGrabServer(dpy);
810 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
812 scr->flags.startup = 1;
814 /* first remove all icon windows */
815 for (i = 0; i < nchildren; i++) {
816 XWMHints *wmhints;
818 if (children[i] == None)
819 continue;
821 wmhints = XGetWMHints(dpy, children[i]);
822 if (wmhints && (wmhints->flags & IconWindowHint)) {
823 for (j = 0; j < nchildren; j++) {
824 if (children[j] == wmhints->icon_window) {
825 XFree(wmhints);
826 wmhints = NULL;
827 children[j] = None;
828 break;
832 if (wmhints) {
833 XFree(wmhints);
837 for (i = 0; i < nchildren; i++) {
838 if (children[i] == None)
839 continue;
841 wwin = wManageWindow(scr, children[i]);
842 if (wwin) {
843 /* apply states got from WSavedState */
844 /* shaded + minimized is not restored correctly */
845 if (wwin->flags.shaded) {
846 wwin->flags.shaded = 0;
847 wShadeWindow(wwin);
849 if (wwin->flags.miniaturized
850 && (wwin->transient_for == None
851 || wwin->transient_for == scr->root_win
852 || !windowInList(wwin->transient_for, children, nchildren))) {
854 wwin->flags.skip_next_animation = 1;
855 wwin->flags.miniaturized = 0;
856 wIconifyWindow(wwin);
857 } else {
858 wClientSetState(wwin, NormalState, None);
860 if (crashRecovery) {
861 int border;
863 border = (!HAS_BORDER(wwin) ? 0 : scr->frame_border_width);
865 wWindowMove(wwin, wwin->frame_x - border,
866 wwin->frame_y - border -
867 (wwin->frame->titlebar ? wwin->frame->titlebar->height : 0));
871 XUngrabServer(dpy);
873 /* hide apps */
874 wwin = scr->focused_window;
875 while (wwin) {
876 if (wwin->flags.hidden) {
877 WApplication *wapp = wApplicationOf(wwin->main_window);
879 if (wapp) {
880 wwin->flags.hidden = 0;
881 wHideApplication(wapp);
882 } else {
883 wwin->flags.hidden = 0;
886 wwin = wwin->prev;
889 XFree(children);
890 scr->flags.startup = 0;
891 scr->flags.startup2 = 1;
893 while (XPending(dpy)) {
894 XEvent ev;
895 WMNextEvent(dpy, &ev);
896 WMHandleEvent(&ev);
898 scr->last_workspace = 0;
899 wWorkspaceForceChange(scr, 0);
900 if (!wPreferences.flags.noclip)
901 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
902 scr->flags.startup2 = 0;