wmaker: Changed math on floating point by integer operation
[wmaker-crm.git] / src / startup.c
blob0175e7cb15c3abce9f75cc7c0867f1e3ed63dd04
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 "startup.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 #include "event.h"
70 #include "switchmenu.h"
71 #ifdef XDND
72 #include "xdnd.h"
73 #endif
75 #include "xutil.h"
77 /* for SunOS */
78 #ifndef SA_RESTART
79 # define SA_RESTART 0
80 #endif
82 /* Just in case, for weirdo systems */
83 #ifndef SA_NODEFER
84 # define SA_NODEFER 0
85 #endif
87 /****** Global Variables ******/
88 extern WPreferences wPreferences;
89 extern WDDomain *WDWindowMaker;
90 extern WDDomain *WDRootMenu;
91 extern WDDomain *WDWindowAttributes;
92 extern WShortKey wKeyBindings[WKBD_LAST];
93 extern int wScreenCount;
95 #ifdef SHAPE
96 extern Bool wShapeSupported;
97 extern int wShapeEventBase;
98 #endif
100 #ifdef KEEP_XKB_LOCK_STATUS
101 extern Bool wXkbSupported;
102 extern int wXkbEventBase;
103 #endif
105 /* contexts */
106 extern XContext wWinContext;
107 extern XContext wAppWinContext;
108 extern XContext wStackContext;
109 extern XContext wVEdgeContext;
111 /* atoms */
112 extern Atom _XA_WM_STATE;
113 extern Atom _XA_WM_CHANGE_STATE;
114 extern Atom _XA_WM_PROTOCOLS;
115 extern Atom _XA_WM_TAKE_FOCUS;
116 extern Atom _XA_WM_DELETE_WINDOW;
117 extern Atom _XA_WM_SAVE_YOURSELF;
118 extern Atom _XA_WM_CLIENT_LEADER;
119 extern Atom _XA_WM_COLORMAP_WINDOWS;
120 extern Atom _XA_WM_COLORMAP_NOTIFY;
121 extern Atom _XA_GNUSTEP_WM_ATTR;
122 extern Atom _XA_WINDOWMAKER_MENU;
123 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
124 extern Atom _XA_WINDOWMAKER_STATE;
125 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
126 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
127 extern Atom _XA_WINDOWMAKER_COMMAND;
128 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
129 extern Atom _XA_WINDOWMAKER_ICON_TILE;
130 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
131 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
132 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
134 /* cursors */
135 extern Cursor wCursor[WCUR_LAST];
137 #ifndef HAVE_INOTIFY
138 /* special flags */
139 extern char WDelayedActionSet;
140 #endif
142 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
144 /***** Local *****/
145 static WScreen **wScreen = NULL;
146 static unsigned int _NumLockMask = 0;
147 static unsigned int _ScrollLockMask = 0;
148 static void manageAllWindows(WScreen * scr, int crashed);
150 static int catchXError(Display * dpy, XErrorEvent * error)
152 char buffer[MAXLINE];
154 /* ignore some errors */
155 if (error->resourceid != None
156 && ((error->error_code == BadDrawable && error->request_code == X_GetGeometry)
157 || (error->error_code == BadMatch && (error->request_code == X_SetInputFocus))
158 || (error->error_code == BadWindow)
160 && (error->request_code == X_GetWindowAttributes
161 || error->request_code == X_SetInputFocus
162 || error->request_code == X_ChangeWindowAttributes
163 || error->request_code == X_GetProperty
164 || error->request_code == X_ChangeProperty
165 || error->request_code == X_QueryTree
166 || error->request_code == X_GrabButton
167 || error->request_code == X_UngrabButton
168 || error->request_code == X_SendEvent
169 || error->request_code == X_ConfigureWindow))
171 || (error->request_code == X_InstallColormap))) {
172 return 0;
174 FormatXError(dpy, error, buffer, MAXLINE);
175 wwarning(_("internal X error: %s"), buffer);
176 return -1;
180 *----------------------------------------------------------------------
181 * handleXIO-
182 * Handle X shutdowns and other stuff.
183 *----------------------------------------------------------------------
185 static int handleXIO(Display * xio_dpy)
187 dpy = NULL;
188 Exit(0);
189 return 0;
192 #ifndef HAVE_INOTIFY
194 *----------------------------------------------------------------------
195 * delayedAction-
196 * Action to be executed after the signal() handler is exited.
197 *----------------------------------------------------------------------
199 static void delayedAction(void *cdata)
201 if (WDelayedActionSet == 0)
202 return;
204 WDelayedActionSet--;
206 * Make the event dispatcher do whatever it needs to do,
207 * including handling zombie processes, restart and exit
208 * signals.
210 DispatchEvent(NULL);
212 #endif
215 *----------------------------------------------------------------------
216 * handleExitSig--
217 * User generated exit signal handler.
218 *----------------------------------------------------------------------
220 static RETSIGTYPE handleExitSig(int sig)
222 sigset_t sigs;
224 sigfillset(&sigs);
225 sigprocmask(SIG_BLOCK, &sigs, NULL);
227 if (sig == SIGUSR1) {
228 wwarning("got signal %i - restarting", sig);
229 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
230 } else if (sig == SIGUSR2) {
231 wwarning("got signal %i - rereading defaults", sig);
232 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
233 } else if (sig == SIGTERM || sig == SIGINT || sig == SIGHUP) {
234 wwarning("got signal %i - exiting...", sig);
235 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
238 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
239 DispatchEvent(NULL); /* Dispatch events imediately. */
242 /* Dummy signal handler */
243 static void dummyHandler(int sig)
248 *----------------------------------------------------------------------
249 * handleSig--
250 * general signal handler. Exits the program gently.
251 *----------------------------------------------------------------------
253 static RETSIGTYPE handleSig(int sig)
255 wfatal("got signal %i", sig);
257 /* Setting the signal behaviour back to default and then reraising the
258 * signal is a cleaner way to make program exit and core dump than calling
259 * abort(), since it correctly returns from the signal handler and sets
260 * the flags accordingly. -Dan
262 if (sig == SIGSEGV || sig == SIGFPE || sig == SIGBUS || sig == SIGILL || sig == SIGABRT) {
263 signal(sig, SIG_DFL);
264 kill(getpid(), sig);
265 return;
268 wAbort(0);
271 static RETSIGTYPE buryChild(int foo)
273 pid_t pid;
274 int status;
275 int save_errno = errno;
276 sigset_t sigs;
278 sigfillset(&sigs);
279 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
280 sigprocmask(SIG_BLOCK, &sigs, NULL);
282 /* R.I.P. */
283 /* If 2 or more kids exit in a small time window, before this handler gets
284 * the chance to get invoked, the SIGCHLD signals will be merged and only
285 * one SIGCHLD signal will be sent to us. We use a while loop to get all
286 * exited child status because we can't count on the number of SIGCHLD
287 * signals to know exactly how many kids have exited. -Dan
289 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || (pid < 0 && errno == EINTR)) {
290 NotifyDeadProcess(pid, WEXITSTATUS(status));
293 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
295 errno = save_errno;
298 static void getOffendingModifiers(void)
300 int i;
301 XModifierKeymap *modmap;
302 KeyCode nlock, slock;
303 static int mask_table[8] = {
304 ShiftMask, LockMask, ControlMask, Mod1Mask,
305 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
308 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
309 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
312 * Find out the masks for the NumLock and ScrollLock modifiers,
313 * so that we can bind the grabs for when they are enabled too.
315 modmap = XGetModifierMapping(dpy);
317 if (modmap != NULL && modmap->max_keypermod > 0) {
318 for (i = 0; i < 8 * modmap->max_keypermod; i++) {
319 if (modmap->modifiermap[i] == nlock && nlock != 0)
320 _NumLockMask = mask_table[i / modmap->max_keypermod];
321 else if (modmap->modifiermap[i] == slock && slock != 0)
322 _ScrollLockMask = mask_table[i / modmap->max_keypermod];
326 if (modmap)
327 XFreeModifiermap(modmap);
330 #ifdef NUMLOCK_HACK
331 void
332 wHackedGrabKey(int keycode, unsigned int modifiers,
333 Window grab_window, Bool owner_events, int pointer_mode, int keyboard_mode)
335 if (modifiers == AnyModifier)
336 return;
338 /* grab all combinations of the modifier with CapsLock, NumLock and
339 * ScrollLock. How much memory/CPU does such a monstrosity consume
340 * in the server?
342 if (_NumLockMask)
343 XGrabKey(dpy, keycode, modifiers | _NumLockMask,
344 grab_window, owner_events, pointer_mode, keyboard_mode);
345 if (_ScrollLockMask)
346 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask,
347 grab_window, owner_events, pointer_mode, keyboard_mode);
348 if (_NumLockMask && _ScrollLockMask)
349 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask,
350 grab_window, owner_events, pointer_mode, keyboard_mode);
351 if (_NumLockMask)
352 XGrabKey(dpy, keycode, modifiers | _NumLockMask | LockMask,
353 grab_window, owner_events, pointer_mode, keyboard_mode);
354 if (_ScrollLockMask)
355 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask | LockMask,
356 grab_window, owner_events, pointer_mode, keyboard_mode);
357 if (_NumLockMask && _ScrollLockMask)
358 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask | LockMask,
359 grab_window, owner_events, pointer_mode, keyboard_mode);
360 /* phew, I guess that's all, right? */
362 #endif
364 void
365 wHackedGrabButton(unsigned int button, unsigned int modifiers,
366 Window grab_window, Bool owner_events,
367 unsigned int event_mask, int pointer_mode, int keyboard_mode, Window confine_to, Cursor cursor)
369 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
370 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
372 if (modifiers == AnyModifier)
373 return;
375 XGrabButton(dpy, button, modifiers | LockMask, grab_window, owner_events,
376 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
378 #ifdef NUMLOCK_HACK
379 /* same as above, but for mouse buttons */
380 if (_NumLockMask)
381 XGrabButton(dpy, button, modifiers | _NumLockMask,
382 grab_window, owner_events, event_mask, pointer_mode,
383 keyboard_mode, confine_to, cursor);
384 if (_ScrollLockMask)
385 XGrabButton(dpy, button, modifiers | _ScrollLockMask,
386 grab_window, owner_events, event_mask, pointer_mode,
387 keyboard_mode, confine_to, cursor);
388 if (_NumLockMask && _ScrollLockMask)
389 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask,
390 grab_window, owner_events, event_mask, pointer_mode,
391 keyboard_mode, confine_to, cursor);
392 if (_NumLockMask)
393 XGrabButton(dpy, button, modifiers | _NumLockMask | LockMask,
394 grab_window, owner_events, event_mask, pointer_mode,
395 keyboard_mode, confine_to, cursor);
396 if (_ScrollLockMask)
397 XGrabButton(dpy, button, modifiers | _ScrollLockMask | LockMask,
398 grab_window, owner_events, event_mask, pointer_mode,
399 keyboard_mode, confine_to, cursor);
400 if (_NumLockMask && _ScrollLockMask)
401 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask | LockMask,
402 grab_window, owner_events, event_mask, pointer_mode,
403 keyboard_mode, confine_to, cursor);
404 #endif /* NUMLOCK_HACK */
407 WScreen *wScreenWithNumber(int i)
409 assert(i < wScreenCount);
411 return wScreen[i];
414 WScreen *wScreenForRootWindow(Window window)
416 int i;
418 if (wScreenCount == 1)
419 return wScreen[0];
421 /* Since the number of heads will probably be small (normally 2),
422 * it should be faster to use this than a hash table, because
423 * of the overhead. */
424 for (i = 0; i < wScreenCount; i++)
425 if (wScreen[i]->root_win == window)
426 return wScreen[i];
428 return wScreenForWindow(window);
431 WScreen *wScreenForWindow(Window window)
433 XWindowAttributes attr;
435 if (wScreenCount == 1)
436 return wScreen[0];
438 if (XGetWindowAttributes(dpy, window, &attr))
439 return wScreenForRootWindow(attr.root);
441 return NULL;
444 static char *atomNames[] = {
445 "WM_STATE",
446 "WM_CHANGE_STATE",
447 "WM_PROTOCOLS",
448 "WM_TAKE_FOCUS",
449 "WM_DELETE_WINDOW",
450 "WM_SAVE_YOURSELF",
451 "WM_CLIENT_LEADER",
452 "WM_COLORMAP_WINDOWS",
453 "WM_COLORMAP_NOTIFY",
455 "_WINDOWMAKER_MENU",
456 "_WINDOWMAKER_STATE",
457 "_WINDOWMAKER_WM_PROTOCOLS",
458 "_WINDOWMAKER_WM_FUNCTION",
459 "_WINDOWMAKER_NOTICEBOARD",
460 "_WINDOWMAKER_COMMAND",
461 "_WINDOWMAKER_ICON_SIZE",
462 "_WINDOWMAKER_ICON_TILE",
464 GNUSTEP_WM_ATTR_NAME,
465 GNUSTEP_WM_MINIATURIZE_WINDOW,
466 GNUSTEP_TITLEBAR_STATE,
468 "WM_IGNORE_FOCUS_EVENTS"
472 *----------------------------------------------------------
473 * StartUp--
474 * starts the window manager and setup global data.
475 * Called from main() at startup.
477 * Side effects:
478 * global data declared in main.c is initialized
479 *----------------------------------------------------------
481 void StartUp(Bool defaultScreenOnly)
483 struct sigaction sig_action;
484 int j, max;
485 #ifdef HAVE_XRANDR
486 int dummy;
487 #endif
488 Atom atom[sizeof(atomNames) / sizeof(atomNames[0])];
491 * Ignore CapsLock in modifiers
493 ValidModMask = 0xff & ~LockMask;
495 getOffendingModifiers();
497 * Ignore NumLock and ScrollLock too
499 ValidModMask &= ~(_NumLockMask | _ScrollLockMask);
501 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
503 wWinContext = XUniqueContext();
504 wAppWinContext = XUniqueContext();
505 wStackContext = XUniqueContext();
506 wVEdgeContext = XUniqueContext();
508 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False); */
510 #ifdef HAVE_XINTERNATOMS
511 XInternAtoms(dpy, atomNames, sizeof(atomNames) / sizeof(atomNames[0]), False, atom);
512 #else
515 int i;
516 for (i = 0; i < sizeof(atomNames) / sizeof(atomNames[0]); i++)
517 atom[i] = XInternAtom(dpy, atomNames[i], False);
519 #endif
521 _XA_WM_STATE = atom[0];
522 _XA_WM_CHANGE_STATE = atom[1];
523 _XA_WM_PROTOCOLS = atom[2];
524 _XA_WM_TAKE_FOCUS = atom[3];
525 _XA_WM_DELETE_WINDOW = atom[4];
526 _XA_WM_SAVE_YOURSELF = atom[5];
527 _XA_WM_CLIENT_LEADER = atom[6];
528 _XA_WM_COLORMAP_WINDOWS = atom[7];
529 _XA_WM_COLORMAP_NOTIFY = atom[8];
531 _XA_WINDOWMAKER_MENU = atom[9];
532 _XA_WINDOWMAKER_STATE = atom[10];
533 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
534 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
535 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
536 _XA_WINDOWMAKER_COMMAND = atom[14];
537 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
538 _XA_WINDOWMAKER_ICON_TILE = atom[16];
540 _XA_GNUSTEP_WM_ATTR = atom[17];
541 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
542 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
544 _XA_WM_IGNORE_FOCUS_EVENTS = atom[20];
546 #ifdef XDND
547 wXDNDInitializeAtoms();
548 #endif
550 /* cursors */
551 wCursor[WCUR_NORMAL] = None; /* inherit from root */
552 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
553 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
554 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
555 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
556 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
557 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
558 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
559 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
560 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
561 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
562 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
563 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
564 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name??? */
565 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
567 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
568 GC gc = XCreateGC(dpy, cur, 0, NULL);
569 XColor black;
570 memset(&black, 0, sizeof(XColor));
571 XSetForeground(dpy, gc, 0);
572 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
573 XFreeGC(dpy, gc);
574 wCursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
575 XFreePixmap(dpy, cur);
578 #ifndef HAVE_INOTIFY
579 /* signal handler stuff that gets called when a signal is caught */
580 WMAddPersistentTimerHandler(500, delayedAction, NULL);
581 #endif
583 /* emergency exit... */
584 sig_action.sa_handler = handleSig;
585 sigemptyset(&sig_action.sa_mask);
587 sig_action.sa_flags = SA_RESTART;
588 sigaction(SIGQUIT, &sig_action, NULL);
589 /* instead of catching these, we let the default handler abort the
590 * program. The new monitor process will take appropriate action
591 * when it detects the crash.
592 sigaction(SIGSEGV, &sig_action, NULL);
593 sigaction(SIGBUS, &sig_action, NULL);
594 sigaction(SIGFPE, &sig_action, NULL);
595 sigaction(SIGABRT, &sig_action, NULL);
598 sig_action.sa_handler = handleExitSig;
600 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
601 * immediately. -Dan */
602 sig_action.sa_flags = SA_RESTART;
603 sigaction(SIGTERM, &sig_action, NULL);
604 sigaction(SIGINT, &sig_action, NULL);
605 sigaction(SIGHUP, &sig_action, NULL);
606 sigaction(SIGUSR1, &sig_action, NULL);
607 sigaction(SIGUSR2, &sig_action, NULL);
609 /* ignore dead pipe */
610 /* Because POSIX mandates that only signal with handlers are reset
611 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
612 * to children. Hence the dummy handler.
613 * Philippe Troin <phil@fifi.org>
615 sig_action.sa_handler = &dummyHandler;
616 sig_action.sa_flags = SA_RESTART;
617 sigaction(SIGPIPE, &sig_action, NULL);
619 /* handle dead children */
620 sig_action.sa_handler = buryChild;
621 sig_action.sa_flags = SA_NOCLDSTOP | SA_RESTART;
622 sigaction(SIGCHLD, &sig_action, NULL);
624 /* Now we unblock all signals, that may have been blocked by the parent
625 * who exec()-ed us. This can happen for example if Window Maker crashes
626 * and restarts itself or another window manager from the signal handler.
627 * In this case, the new proccess inherits the blocked signal mask and
628 * will no longer react to that signal, until unblocked.
629 * This is because the signal handler of the proccess who crashed (parent)
630 * didn't return, and the signal remained blocked. -Dan
632 sigfillset(&sig_action.sa_mask);
633 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
635 /* handle X shutdowns a such */
636 XSetIOErrorHandler(handleXIO);
638 /* set hook for out event dispatcher in WINGs event dispatcher */
639 WMHookEventHandler(DispatchEvent);
641 /* initialize defaults stuff */
642 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
643 if (!WDWindowMaker->dictionary)
644 wwarning(_("could not read domain \"%s\" from defaults database"), "WindowMaker");
646 /* read defaults that don't change until a restart and are
647 * screen independent */
648 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
650 /* check sanity of some values */
651 if (wPreferences.icon_size < 16) {
652 wwarning(_("icon size is configured to %i, but it's too small. Using 16 instead"),
653 wPreferences.icon_size);
654 wPreferences.icon_size = 16;
657 /* init other domains */
658 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
659 if (!WDRootMenu->dictionary)
660 wwarning(_("could not read domain \"%s\" from defaults database"), "WMRootMenu");
662 wDefaultsMergeGlobalMenus(WDRootMenu);
664 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
665 if (!WDWindowAttributes->dictionary)
666 wwarning(_("could not read domain \"%s\" from defaults database"), "WMWindowAttributes");
668 XSetErrorHandler((XErrorHandler) catchXError);
670 #ifdef SHAPE
671 /* ignore j */
672 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
673 #endif
675 #ifdef HAVE_XRANDR
676 has_randr = XRRQueryExtension(dpy, &randr_event_base, &dummy);
677 #endif
679 #ifdef KEEP_XKB_LOCK_STATUS
680 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
681 if (wPreferences.modelock && !wXkbSupported) {
682 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
683 wPreferences.modelock = 0;
685 #endif
687 if (defaultScreenOnly)
688 max = 1;
689 else
690 max = ScreenCount(dpy);
692 wScreen = wmalloc(sizeof(WScreen *) * max);
694 wScreenCount = 0;
696 /* manage the screens */
697 for (j = 0; j < max; j++) {
698 if (defaultScreenOnly || max == 1) {
699 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
700 if (!wScreen[wScreenCount]) {
701 wfatal(_("it seems that there is already a window manager running"));
702 Exit(1);
704 } else {
705 wScreen[wScreenCount] = wScreenInit(j);
706 if (!wScreen[wScreenCount]) {
707 wwarning(_("could not manage screen %i"), j);
708 continue;
711 wScreenCount++;
714 InitializeSwitchMenu();
716 /* initialize/restore state for the screens */
717 for (j = 0; j < wScreenCount; j++) {
718 int lastDesktop;
720 lastDesktop = wNETWMGetCurrentDesktopFromHint(wScreen[j]);
722 wScreenRestoreState(wScreen[j]);
724 /* manage all windows that were already here before us */
725 if (!wPreferences.flags.nodock && wScreen[j]->dock)
726 wScreen[j]->last_dock = wScreen[j]->dock;
728 manageAllWindows(wScreen[j], wPreferences.flags.restarting == 2);
730 /* restore saved menus */
731 wMenuRestoreState(wScreen[j]);
733 /* If we're not restarting, restore session */
734 if (wPreferences.flags.restarting == 0 && !wPreferences.flags.norestore)
735 wSessionRestoreState(wScreen[j]);
737 if (!wPreferences.flags.noautolaunch) {
738 /* auto-launch apps */
739 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
740 wScreen[j]->last_dock = wScreen[j]->dock;
741 wDockDoAutoLaunch(wScreen[j]->dock, 0);
743 /* auto-launch apps in clip */
744 if (!wPreferences.flags.noclip) {
745 int i;
746 for (i = 0; i < wScreen[j]->workspace_count; i++) {
747 if (wScreen[j]->workspaces[i]->clip) {
748 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
749 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
753 /* auto-launch apps in drawers */
754 if (!wPreferences.flags.nodrawer) {
755 WDrawerChain *dc;
756 for (dc = wScreen[j]->drawers; dc; dc = dc->next) {
757 wScreen[j]->last_dock = dc->adrawer;
758 wDockDoAutoLaunch(dc->adrawer, 0);
763 /* go to workspace where we were before restart */
764 if (lastDesktop >= 0)
765 wWorkspaceForceChange(wScreen[j], lastDesktop);
766 else
767 wSessionRestoreLastWorkspace(wScreen[j]);
770 if (wScreenCount == 0) {
771 wfatal(_("could not manage any screen"));
772 Exit(1);
775 #ifndef HAVE_INOTIFY
776 /* setup defaults file polling */
777 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates)
778 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
779 #endif
783 static Bool windowInList(Window window, Window * list, int count)
785 for (; count >= 0; count--) {
786 if (window == list[count])
787 return True;
789 return False;
793 *-----------------------------------------------------------------------
794 * manageAllWindows--
795 * Manages all windows in the screen.
797 * Notes:
798 * Called when the wm is being started.
799 * No events can be processed while the windows are being
800 * reparented/managed.
801 *-----------------------------------------------------------------------
803 static void manageAllWindows(WScreen * scr, int crashRecovery)
805 Window root, parent;
806 Window *children;
807 unsigned int nchildren;
808 unsigned int i, j;
809 WWindow *wwin;
811 XGrabServer(dpy);
812 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
814 scr->flags.startup = 1;
816 /* first remove all icon windows */
817 for (i = 0; i < nchildren; i++) {
818 XWMHints *wmhints;
820 if (children[i] == None)
821 continue;
823 wmhints = XGetWMHints(dpy, children[i]);
824 if (wmhints && (wmhints->flags & IconWindowHint)) {
825 for (j = 0; j < nchildren; j++) {
826 if (children[j] == wmhints->icon_window) {
827 XFree(wmhints);
828 wmhints = NULL;
829 children[j] = None;
830 break;
834 if (wmhints) {
835 XFree(wmhints);
839 for (i = 0; i < nchildren; i++) {
840 if (children[i] == None)
841 continue;
843 wwin = wManageWindow(scr, children[i]);
844 if (wwin) {
845 /* apply states got from WSavedState */
846 /* shaded + minimized is not restored correctly */
847 if (wwin->flags.shaded) {
848 wwin->flags.shaded = 0;
849 wShadeWindow(wwin);
851 if (wwin->flags.miniaturized
852 && (wwin->transient_for == None
853 || wwin->transient_for == scr->root_win
854 || !windowInList(wwin->transient_for, children, nchildren))) {
856 wwin->flags.skip_next_animation = 1;
857 wwin->flags.miniaturized = 0;
858 wIconifyWindow(wwin);
859 } else {
860 wClientSetState(wwin, NormalState, None);
862 if (crashRecovery) {
863 int border;
865 border = (!HAS_BORDER(wwin) ? 0 : scr->frame_border_width);
867 wWindowMove(wwin, wwin->frame_x - border,
868 wwin->frame_y - border -
869 (wwin->frame->titlebar ? wwin->frame->titlebar->height : 0));
873 XUngrabServer(dpy);
875 /* hide apps */
876 wwin = scr->focused_window;
877 while (wwin) {
878 if (wwin->flags.hidden) {
879 WApplication *wapp = wApplicationOf(wwin->main_window);
881 if (wapp) {
882 wwin->flags.hidden = 0;
883 wHideApplication(wapp);
884 } else {
885 wwin->flags.hidden = 0;
888 wwin = wwin->prev;
891 XFree(children);
892 scr->flags.startup = 0;
893 scr->flags.startup2 = 1;
895 while (XPending(dpy)) {
896 XEvent ev;
897 WMNextEvent(dpy, &ev);
898 WMHandleEvent(&ev);
900 scr->last_workspace = 0;
901 wWorkspaceForceChange(scr, 0);
902 if (!wPreferences.flags.noclip)
903 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
904 scr->flags.startup2 = 0;