Merge branch 'wmdrawer' into next
[wmaker-crm.git] / src / startup.c
blobd309d98b88e8a2748fe0e82a316ef9e6d56e94d4
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 /* contexts */
103 extern XContext wWinContext;
104 extern XContext wAppWinContext;
105 extern XContext wStackContext;
106 extern XContext wVEdgeContext;
108 /* atoms */
109 extern Atom _XA_WM_STATE;
110 extern Atom _XA_WM_CHANGE_STATE;
111 extern Atom _XA_WM_PROTOCOLS;
112 extern Atom _XA_WM_TAKE_FOCUS;
113 extern Atom _XA_WM_DELETE_WINDOW;
114 extern Atom _XA_WM_SAVE_YOURSELF;
115 extern Atom _XA_WM_CLIENT_LEADER;
116 extern Atom _XA_WM_COLORMAP_WINDOWS;
117 extern Atom _XA_WM_COLORMAP_NOTIFY;
118 extern Atom _XA_GNUSTEP_WM_ATTR;
119 extern Atom _XA_WINDOWMAKER_MENU;
120 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
121 extern Atom _XA_WINDOWMAKER_STATE;
122 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
123 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
124 extern Atom _XA_WINDOWMAKER_COMMAND;
125 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
126 extern Atom _XA_WINDOWMAKER_ICON_TILE;
127 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
128 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
129 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
131 /* cursors */
132 extern Cursor wCursor[WCUR_LAST];
134 #ifndef HAVE_INOTIFY
135 /* special flags */
136 extern char WDelayedActionSet;
137 #endif
139 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
141 /***** Local *****/
142 static WScreen **wScreen = NULL;
143 static unsigned int _NumLockMask = 0;
144 static unsigned int _ScrollLockMask = 0;
145 static void manageAllWindows(WScreen * scr, int crashed);
147 static int catchXError(Display * dpy, XErrorEvent * error)
149 char buffer[MAXLINE];
151 /* ignore some errors */
152 if (error->resourceid != None
153 && ((error->error_code == BadDrawable && error->request_code == X_GetGeometry)
154 || (error->error_code == BadMatch && (error->request_code == X_SetInputFocus))
155 || (error->error_code == BadWindow)
157 && (error->request_code == X_GetWindowAttributes
158 || error->request_code == X_SetInputFocus
159 || error->request_code == X_ChangeWindowAttributes
160 || error->request_code == X_GetProperty
161 || error->request_code == X_ChangeProperty
162 || error->request_code == X_QueryTree
163 || error->request_code == X_GrabButton
164 || error->request_code == X_UngrabButton
165 || error->request_code == X_SendEvent
166 || error->request_code == X_ConfigureWindow))
168 || (error->request_code == X_InstallColormap))) {
169 return 0;
171 FormatXError(dpy, error, buffer, MAXLINE);
172 wwarning(_("internal X error: %s"), buffer);
173 return -1;
177 *----------------------------------------------------------------------
178 * handleXIO-
179 * Handle X shutdowns and other stuff.
180 *----------------------------------------------------------------------
182 static int handleXIO(Display * xio_dpy)
184 dpy = NULL;
185 Exit(0);
186 return 0;
189 #ifndef HAVE_INOTIFY
191 *----------------------------------------------------------------------
192 * delayedAction-
193 * Action to be executed after the signal() handler is exited.
194 *----------------------------------------------------------------------
196 static void delayedAction(void *cdata)
198 if (WDelayedActionSet == 0)
199 return;
201 WDelayedActionSet--;
203 * Make the event dispatcher do whatever it needs to do,
204 * including handling zombie processes, restart and exit
205 * signals.
207 DispatchEvent(NULL);
209 #endif
212 *----------------------------------------------------------------------
213 * handleExitSig--
214 * User generated exit signal handler.
215 *----------------------------------------------------------------------
217 static RETSIGTYPE handleExitSig(int sig)
219 sigset_t sigs;
221 sigfillset(&sigs);
222 sigprocmask(SIG_BLOCK, &sigs, NULL);
224 if (sig == SIGUSR1) {
225 wwarning("got signal %i - restarting", sig);
226 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
227 } else if (sig == SIGUSR2) {
228 wwarning("got signal %i - rereading defaults", sig);
229 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
230 } else if (sig == SIGTERM || sig == SIGINT || sig == SIGHUP) {
231 wwarning("got signal %i - exiting...", sig);
232 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
235 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
236 DispatchEvent(NULL); /* Dispatch events imediately. */
239 /* Dummy signal handler */
240 static void dummyHandler(int sig)
245 *----------------------------------------------------------------------
246 * handleSig--
247 * general signal handler. Exits the program gently.
248 *----------------------------------------------------------------------
250 static RETSIGTYPE handleSig(int sig)
252 wfatal("got signal %i", sig);
254 /* Setting the signal behaviour back to default and then reraising the
255 * signal is a cleaner way to make program exit and core dump than calling
256 * abort(), since it correctly returns from the signal handler and sets
257 * the flags accordingly. -Dan
259 if (sig == SIGSEGV || sig == SIGFPE || sig == SIGBUS || sig == SIGILL || sig == SIGABRT) {
260 signal(sig, SIG_DFL);
261 kill(getpid(), sig);
262 return;
265 wAbort(0);
268 static RETSIGTYPE buryChild(int foo)
270 pid_t pid;
271 int status;
272 int save_errno = errno;
273 sigset_t sigs;
275 sigfillset(&sigs);
276 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
277 sigprocmask(SIG_BLOCK, &sigs, NULL);
279 /* R.I.P. */
280 /* If 2 or more kids exit in a small time window, before this handler gets
281 * the chance to get invoked, the SIGCHLD signals will be merged and only
282 * one SIGCHLD signal will be sent to us. We use a while loop to get all
283 * exited child status because we can't count on the number of SIGCHLD
284 * signals to know exactly how many kids have exited. -Dan
286 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || (pid < 0 && errno == EINTR)) {
287 NotifyDeadProcess(pid, WEXITSTATUS(status));
290 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
292 errno = save_errno;
295 static void getOffendingModifiers(void)
297 int i;
298 XModifierKeymap *modmap;
299 KeyCode nlock, slock;
300 static int mask_table[8] = {
301 ShiftMask, LockMask, ControlMask, Mod1Mask,
302 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
305 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
306 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
309 * Find out the masks for the NumLock and ScrollLock modifiers,
310 * so that we can bind the grabs for when they are enabled too.
312 modmap = XGetModifierMapping(dpy);
314 if (modmap != NULL && modmap->max_keypermod > 0) {
315 for (i = 0; i < 8 * modmap->max_keypermod; i++) {
316 if (modmap->modifiermap[i] == nlock && nlock != 0)
317 _NumLockMask = mask_table[i / modmap->max_keypermod];
318 else if (modmap->modifiermap[i] == slock && slock != 0)
319 _ScrollLockMask = mask_table[i / modmap->max_keypermod];
323 if (modmap)
324 XFreeModifiermap(modmap);
327 #ifdef NUMLOCK_HACK
328 void
329 wHackedGrabKey(int keycode, unsigned int modifiers,
330 Window grab_window, Bool owner_events, int pointer_mode, int keyboard_mode)
332 if (modifiers == AnyModifier)
333 return;
335 /* grab all combinations of the modifier with CapsLock, NumLock and
336 * ScrollLock. How much memory/CPU does such a monstrosity consume
337 * in the server?
339 if (_NumLockMask)
340 XGrabKey(dpy, keycode, modifiers | _NumLockMask,
341 grab_window, owner_events, pointer_mode, keyboard_mode);
342 if (_ScrollLockMask)
343 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask,
344 grab_window, owner_events, pointer_mode, keyboard_mode);
345 if (_NumLockMask && _ScrollLockMask)
346 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask,
347 grab_window, owner_events, pointer_mode, keyboard_mode);
348 if (_NumLockMask)
349 XGrabKey(dpy, keycode, modifiers | _NumLockMask | LockMask,
350 grab_window, owner_events, pointer_mode, keyboard_mode);
351 if (_ScrollLockMask)
352 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask | LockMask,
353 grab_window, owner_events, pointer_mode, keyboard_mode);
354 if (_NumLockMask && _ScrollLockMask)
355 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask | LockMask,
356 grab_window, owner_events, pointer_mode, keyboard_mode);
357 /* phew, I guess that's all, right? */
359 #endif
361 void
362 wHackedGrabButton(unsigned int button, unsigned int modifiers,
363 Window grab_window, Bool owner_events,
364 unsigned int event_mask, int pointer_mode, int keyboard_mode, Window confine_to, Cursor cursor)
366 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
367 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
369 if (modifiers == AnyModifier)
370 return;
372 XGrabButton(dpy, button, modifiers | LockMask, grab_window, owner_events,
373 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
375 #ifdef NUMLOCK_HACK
376 /* same as above, but for mouse buttons */
377 if (_NumLockMask)
378 XGrabButton(dpy, button, modifiers | _NumLockMask,
379 grab_window, owner_events, event_mask, pointer_mode,
380 keyboard_mode, confine_to, cursor);
381 if (_ScrollLockMask)
382 XGrabButton(dpy, button, modifiers | _ScrollLockMask,
383 grab_window, owner_events, event_mask, pointer_mode,
384 keyboard_mode, confine_to, cursor);
385 if (_NumLockMask && _ScrollLockMask)
386 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask,
387 grab_window, owner_events, event_mask, pointer_mode,
388 keyboard_mode, confine_to, cursor);
389 if (_NumLockMask)
390 XGrabButton(dpy, button, modifiers | _NumLockMask | LockMask,
391 grab_window, owner_events, event_mask, pointer_mode,
392 keyboard_mode, confine_to, cursor);
393 if (_ScrollLockMask)
394 XGrabButton(dpy, button, modifiers | _ScrollLockMask | LockMask,
395 grab_window, owner_events, event_mask, pointer_mode,
396 keyboard_mode, confine_to, cursor);
397 if (_NumLockMask && _ScrollLockMask)
398 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask | LockMask,
399 grab_window, owner_events, event_mask, pointer_mode,
400 keyboard_mode, confine_to, cursor);
401 #endif /* NUMLOCK_HACK */
404 WScreen *wScreenWithNumber(int i)
406 assert(i < wScreenCount);
408 return wScreen[i];
411 WScreen *wScreenForRootWindow(Window window)
413 int i;
415 if (wScreenCount == 1)
416 return wScreen[0];
418 /* Since the number of heads will probably be small (normally 2),
419 * it should be faster to use this than a hash table, because
420 * of the overhead. */
421 for (i = 0; i < wScreenCount; i++)
422 if (wScreen[i]->root_win == window)
423 return wScreen[i];
425 return wScreenForWindow(window);
428 WScreen *wScreenForWindow(Window window)
430 XWindowAttributes attr;
432 if (wScreenCount == 1)
433 return wScreen[0];
435 if (XGetWindowAttributes(dpy, window, &attr))
436 return wScreenForRootWindow(attr.root);
438 return NULL;
441 static char *atomNames[] = {
442 "WM_STATE",
443 "WM_CHANGE_STATE",
444 "WM_PROTOCOLS",
445 "WM_TAKE_FOCUS",
446 "WM_DELETE_WINDOW",
447 "WM_SAVE_YOURSELF",
448 "WM_CLIENT_LEADER",
449 "WM_COLORMAP_WINDOWS",
450 "WM_COLORMAP_NOTIFY",
452 "_WINDOWMAKER_MENU",
453 "_WINDOWMAKER_STATE",
454 "_WINDOWMAKER_WM_PROTOCOLS",
455 "_WINDOWMAKER_WM_FUNCTION",
456 "_WINDOWMAKER_NOTICEBOARD",
457 "_WINDOWMAKER_COMMAND",
458 "_WINDOWMAKER_ICON_SIZE",
459 "_WINDOWMAKER_ICON_TILE",
461 GNUSTEP_WM_ATTR_NAME,
462 GNUSTEP_WM_MINIATURIZE_WINDOW,
463 GNUSTEP_TITLEBAR_STATE,
465 "WM_IGNORE_FOCUS_EVENTS"
469 *----------------------------------------------------------
470 * StartUp--
471 * starts the window manager and setup global data.
472 * Called from main() at startup.
474 * Side effects:
475 * global data declared in main.c is initialized
476 *----------------------------------------------------------
478 void StartUp(Bool defaultScreenOnly)
480 struct sigaction sig_action;
481 int j, max;
482 #ifdef HAVE_XRANDR
483 int dummy;
484 #endif
485 Atom atom[sizeof(atomNames) / sizeof(char *)];
488 * Ignore CapsLock in modifiers
490 ValidModMask = 0xff & ~LockMask;
492 getOffendingModifiers();
494 * Ignore NumLock and ScrollLock too
496 ValidModMask &= ~(_NumLockMask | _ScrollLockMask);
498 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
500 wWinContext = XUniqueContext();
501 wAppWinContext = XUniqueContext();
502 wStackContext = XUniqueContext();
503 wVEdgeContext = XUniqueContext();
505 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False); */
507 #ifdef HAVE_XINTERNATOMS
508 XInternAtoms(dpy, atomNames, sizeof(atomNames) / sizeof(char *), False, atom);
509 #else
512 int i;
513 for (i = 0; i < sizeof(atomNames) / sizeof(char *); i++)
514 atom[i] = XInternAtom(dpy, atomNames[i], False);
516 #endif
518 _XA_WM_STATE = atom[0];
519 _XA_WM_CHANGE_STATE = atom[1];
520 _XA_WM_PROTOCOLS = atom[2];
521 _XA_WM_TAKE_FOCUS = atom[3];
522 _XA_WM_DELETE_WINDOW = atom[4];
523 _XA_WM_SAVE_YOURSELF = atom[5];
524 _XA_WM_CLIENT_LEADER = atom[6];
525 _XA_WM_COLORMAP_WINDOWS = atom[7];
526 _XA_WM_COLORMAP_NOTIFY = atom[8];
528 _XA_WINDOWMAKER_MENU = atom[9];
529 _XA_WINDOWMAKER_STATE = atom[10];
530 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
531 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
532 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
533 _XA_WINDOWMAKER_COMMAND = atom[14];
534 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
535 _XA_WINDOWMAKER_ICON_TILE = atom[16];
537 _XA_GNUSTEP_WM_ATTR = atom[17];
538 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
539 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
541 _XA_WM_IGNORE_FOCUS_EVENTS = atom[20];
543 #ifdef XDND
544 wXDNDInitializeAtoms();
545 #endif
547 /* cursors */
548 wCursor[WCUR_NORMAL] = None; /* inherit from root */
549 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
550 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
551 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
552 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
553 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
554 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
555 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
556 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
557 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
558 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
559 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
560 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
561 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name??? */
562 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
564 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
565 GC gc = XCreateGC(dpy, cur, 0, NULL);
566 XColor black;
567 memset(&black, 0, sizeof(XColor));
568 XSetForeground(dpy, gc, 0);
569 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
570 XFreeGC(dpy, gc);
571 wCursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
572 XFreePixmap(dpy, cur);
575 #ifndef HAVE_INOTIFY
576 /* signal handler stuff that gets called when a signal is caught */
577 WMAddPersistentTimerHandler(500, delayedAction, NULL);
578 #endif
580 /* emergency exit... */
581 sig_action.sa_handler = handleSig;
582 sigemptyset(&sig_action.sa_mask);
584 sig_action.sa_flags = SA_RESTART;
585 sigaction(SIGQUIT, &sig_action, NULL);
586 /* instead of catching these, we let the default handler abort the
587 * program. The new monitor process will take appropriate action
588 * when it detects the crash.
589 sigaction(SIGSEGV, &sig_action, NULL);
590 sigaction(SIGBUS, &sig_action, NULL);
591 sigaction(SIGFPE, &sig_action, NULL);
592 sigaction(SIGABRT, &sig_action, NULL);
595 sig_action.sa_handler = handleExitSig;
597 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
598 * immediately. -Dan */
599 sig_action.sa_flags = SA_RESTART;
600 sigaction(SIGTERM, &sig_action, NULL);
601 sigaction(SIGINT, &sig_action, NULL);
602 sigaction(SIGHUP, &sig_action, NULL);
603 sigaction(SIGUSR1, &sig_action, NULL);
604 sigaction(SIGUSR2, &sig_action, NULL);
606 /* ignore dead pipe */
607 /* Because POSIX mandates that only signal with handlers are reset
608 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
609 * to children. Hence the dummy handler.
610 * Philippe Troin <phil@fifi.org>
612 sig_action.sa_handler = &dummyHandler;
613 sig_action.sa_flags = SA_RESTART;
614 sigaction(SIGPIPE, &sig_action, NULL);
616 /* handle dead children */
617 sig_action.sa_handler = buryChild;
618 sig_action.sa_flags = SA_NOCLDSTOP | SA_RESTART;
619 sigaction(SIGCHLD, &sig_action, NULL);
621 /* Now we unblock all signals, that may have been blocked by the parent
622 * who exec()-ed us. This can happen for example if Window Maker crashes
623 * and restarts itself or another window manager from the signal handler.
624 * In this case, the new proccess inherits the blocked signal mask and
625 * will no longer react to that signal, until unblocked.
626 * This is because the signal handler of the proccess who crashed (parent)
627 * didn't return, and the signal remained blocked. -Dan
629 sigfillset(&sig_action.sa_mask);
630 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
632 /* handle X shutdowns a such */
633 XSetIOErrorHandler(handleXIO);
635 /* set hook for out event dispatcher in WINGs event dispatcher */
636 WMHookEventHandler(DispatchEvent);
638 /* initialize defaults stuff */
639 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
640 if (!WDWindowMaker->dictionary)
641 wwarning(_("could not read domain \"%s\" from defaults database"), "WindowMaker");
643 /* read defaults that don't change until a restart and are
644 * screen independent */
645 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
647 /* check sanity of some values */
648 if (wPreferences.icon_size < 16) {
649 wwarning(_("icon size is configured to %i, but it's too small. Using 16 instead"),
650 wPreferences.icon_size);
651 wPreferences.icon_size = 16;
654 /* init other domains */
655 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
656 if (!WDRootMenu->dictionary)
657 wwarning(_("could not read domain \"%s\" from defaults database"), "WMRootMenu");
659 wDefaultsMergeGlobalMenus(WDRootMenu);
661 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
662 if (!WDWindowAttributes->dictionary)
663 wwarning(_("could not read domain \"%s\" from defaults database"), "WMWindowAttributes");
665 XSetErrorHandler((XErrorHandler) catchXError);
667 #ifdef SHAPE
668 /* ignore j */
669 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
670 #endif
672 #ifdef HAVE_XRANDR
673 has_randr = XRRQueryExtension(dpy, &randr_event_base, &dummy);
674 #endif
676 #ifdef KEEP_XKB_LOCK_STATUS
677 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
678 if (wPreferences.modelock && !wXkbSupported) {
679 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
680 wPreferences.modelock = 0;
682 #endif
684 if (defaultScreenOnly)
685 max = 1;
686 else
687 max = ScreenCount(dpy);
689 wScreen = wmalloc(sizeof(WScreen *) * max);
691 wScreenCount = 0;
693 /* manage the screens */
694 for (j = 0; j < max; j++) {
695 if (defaultScreenOnly || max == 1) {
696 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
697 if (!wScreen[wScreenCount]) {
698 wfatal(_("it seems that there is already a window manager running"));
699 Exit(1);
701 } else {
702 wScreen[wScreenCount] = wScreenInit(j);
703 if (!wScreen[wScreenCount]) {
704 wwarning(_("could not manage screen %i"), j);
705 continue;
708 wScreenCount++;
711 InitializeSwitchMenu();
713 /* initialize/restore state for the screens */
714 for (j = 0; j < wScreenCount; j++) {
715 int lastDesktop;
717 lastDesktop = wNETWMGetCurrentDesktopFromHint(wScreen[j]);
719 wScreenRestoreState(wScreen[j]);
721 /* manage all windows that were already here before us */
722 if (!wPreferences.flags.nodock && wScreen[j]->dock)
723 wScreen[j]->last_dock = wScreen[j]->dock;
725 manageAllWindows(wScreen[j], wPreferences.flags.restarting == 2);
727 /* restore saved menus */
728 wMenuRestoreState(wScreen[j]);
730 /* If we're not restarting, restore session */
731 if (wPreferences.flags.restarting == 0 && !wPreferences.flags.norestore)
732 wSessionRestoreState(wScreen[j]);
734 if (!wPreferences.flags.noautolaunch) {
735 /* auto-launch apps */
736 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
737 wScreen[j]->last_dock = wScreen[j]->dock;
738 wDockDoAutoLaunch(wScreen[j]->dock, 0);
740 /* auto-launch apps in clip */
741 if (!wPreferences.flags.noclip) {
742 int i;
743 for (i = 0; i < wScreen[j]->workspace_count; i++) {
744 if (wScreen[j]->workspaces[i]->clip) {
745 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
746 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
750 /* auto-launch apps in drawers */
751 if (!wPreferences.flags.nodrawer) {
752 WDrawerChain *dc;
753 for (dc = wScreen[j]->drawers; dc; dc = dc->next) {
754 wScreen[j]->last_dock = dc->adrawer;
755 wDockDoAutoLaunch(dc->adrawer, 0);
760 /* go to workspace where we were before restart */
761 if (lastDesktop >= 0)
762 wWorkspaceForceChange(wScreen[j], lastDesktop);
763 else
764 wSessionRestoreLastWorkspace(wScreen[j]);
767 if (wScreenCount == 0) {
768 wfatal(_("could not manage any screen"));
769 Exit(1);
772 #ifndef HAVE_INOTIFY
773 /* setup defaults file polling */
774 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates)
775 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
776 #endif
780 static Bool windowInList(Window window, Window * list, int count)
782 for (; count >= 0; count--) {
783 if (window == list[count])
784 return True;
786 return False;
790 *-----------------------------------------------------------------------
791 * manageAllWindows--
792 * Manages all windows in the screen.
794 * Notes:
795 * Called when the wm is being started.
796 * No events can be processed while the windows are being
797 * reparented/managed.
798 *-----------------------------------------------------------------------
800 static void manageAllWindows(WScreen * scr, int crashRecovery)
802 Window root, parent;
803 Window *children;
804 unsigned int nchildren;
805 unsigned int i, j;
806 WWindow *wwin;
808 XGrabServer(dpy);
809 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
811 scr->flags.startup = 1;
813 /* first remove all icon windows */
814 for (i = 0; i < nchildren; i++) {
815 XWMHints *wmhints;
817 if (children[i] == None)
818 continue;
820 wmhints = XGetWMHints(dpy, children[i]);
821 if (wmhints && (wmhints->flags & IconWindowHint)) {
822 for (j = 0; j < nchildren; j++) {
823 if (children[j] == wmhints->icon_window) {
824 XFree(wmhints);
825 wmhints = NULL;
826 children[j] = None;
827 break;
831 if (wmhints) {
832 XFree(wmhints);
836 for (i = 0; i < nchildren; i++) {
837 if (children[i] == None)
838 continue;
840 wwin = wManageWindow(scr, children[i]);
841 if (wwin) {
842 /* apply states got from WSavedState */
843 /* shaded + minimized is not restored correctly */
844 if (wwin->flags.shaded) {
845 wwin->flags.shaded = 0;
846 wShadeWindow(wwin);
848 if (wwin->flags.miniaturized
849 && (wwin->transient_for == None
850 || wwin->transient_for == scr->root_win
851 || !windowInList(wwin->transient_for, children, nchildren))) {
853 wwin->flags.skip_next_animation = 1;
854 wwin->flags.miniaturized = 0;
855 wIconifyWindow(wwin);
856 } else {
857 wClientSetState(wwin, NormalState, None);
859 if (crashRecovery) {
860 int border;
862 border = (!HAS_BORDER(wwin) ? 0 : scr->frame_border_width);
864 wWindowMove(wwin, wwin->frame_x - border,
865 wwin->frame_y - border -
866 (wwin->frame->titlebar ? wwin->frame->titlebar->height : 0));
870 XUngrabServer(dpy);
872 /* hide apps */
873 wwin = scr->focused_window;
874 while (wwin) {
875 if (wwin->flags.hidden) {
876 WApplication *wapp = wApplicationOf(wwin->main_window);
878 if (wapp) {
879 wwin->flags.hidden = 0;
880 wHideApplication(wapp);
881 } else {
882 wwin->flags.hidden = 0;
885 wwin = wwin->prev;
888 XFree(children);
889 scr->flags.startup = 0;
890 scr->flags.startup2 = 1;
892 while (XPending(dpy)) {
893 XEvent ev;
894 WMNextEvent(dpy, &ev);
895 WMHandleEvent(&ev);
897 scr->last_workspace = 0;
898 wWorkspaceForceChange(scr, 0);
899 if (!wPreferences.flags.noclip)
900 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
901 scr->flags.startup2 = 0;