wmaker: Moved global var with list of cursors to the preferences variable
[wmaker-crm.git] / src / startup.c
blobf8e8bbc21fa2a57124ca821b20b4d203fc9360c4
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 WDDomain *WDWindowMaker;
89 extern WDDomain *WDRootMenu;
90 extern WDDomain *WDWindowAttributes;
91 extern WShortKey wKeyBindings[WKBD_LAST];
92 extern int wScreenCount;
94 #ifdef SHAPE
95 extern Bool wShapeSupported;
96 extern int wShapeEventBase;
97 #endif
99 #ifdef KEEP_XKB_LOCK_STATUS
100 extern Bool wXkbSupported;
101 extern int wXkbEventBase;
102 #endif
104 /* contexts */
105 extern XContext wWinContext;
106 extern XContext wAppWinContext;
107 extern XContext wStackContext;
108 extern XContext wVEdgeContext;
110 /* atoms */
111 extern Atom _XA_WM_STATE;
112 extern Atom _XA_WM_CHANGE_STATE;
113 extern Atom _XA_WM_PROTOCOLS;
114 extern Atom _XA_WM_TAKE_FOCUS;
115 extern Atom _XA_WM_DELETE_WINDOW;
116 extern Atom _XA_WM_SAVE_YOURSELF;
117 extern Atom _XA_WM_CLIENT_LEADER;
118 extern Atom _XA_WM_COLORMAP_WINDOWS;
119 extern Atom _XA_WM_COLORMAP_NOTIFY;
120 extern Atom _XA_GNUSTEP_WM_ATTR;
121 extern Atom _XA_WINDOWMAKER_MENU;
122 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
123 extern Atom _XA_WINDOWMAKER_STATE;
124 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
125 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
126 extern Atom _XA_WINDOWMAKER_COMMAND;
127 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
128 extern Atom _XA_WINDOWMAKER_ICON_TILE;
129 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
130 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
131 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
133 #ifndef HAVE_INOTIFY
134 /* special flags */
135 extern char WDelayedActionSet;
136 #endif
138 /***** Local *****/
139 static WScreen **wScreen = NULL;
140 static unsigned int _NumLockMask = 0;
141 static unsigned int _ScrollLockMask = 0;
142 static void manageAllWindows(WScreen * scr, int crashed);
144 static int catchXError(Display * dpy, XErrorEvent * error)
146 char buffer[MAXLINE];
148 /* ignore some errors */
149 if (error->resourceid != None
150 && ((error->error_code == BadDrawable && error->request_code == X_GetGeometry)
151 || (error->error_code == BadMatch && (error->request_code == X_SetInputFocus))
152 || (error->error_code == BadWindow)
154 && (error->request_code == X_GetWindowAttributes
155 || error->request_code == X_SetInputFocus
156 || error->request_code == X_ChangeWindowAttributes
157 || error->request_code == X_GetProperty
158 || error->request_code == X_ChangeProperty
159 || error->request_code == X_QueryTree
160 || error->request_code == X_GrabButton
161 || error->request_code == X_UngrabButton
162 || error->request_code == X_SendEvent
163 || error->request_code == X_ConfigureWindow))
165 || (error->request_code == X_InstallColormap))) {
166 return 0;
168 FormatXError(dpy, error, buffer, MAXLINE);
169 wwarning(_("internal X error: %s"), buffer);
170 return -1;
174 *----------------------------------------------------------------------
175 * handleXIO-
176 * Handle X shutdowns and other stuff.
177 *----------------------------------------------------------------------
179 static int handleXIO(Display * xio_dpy)
181 dpy = NULL;
182 Exit(0);
183 return 0;
186 #ifndef HAVE_INOTIFY
188 *----------------------------------------------------------------------
189 * delayedAction-
190 * Action to be executed after the signal() handler is exited.
191 *----------------------------------------------------------------------
193 static void delayedAction(void *cdata)
195 if (WDelayedActionSet == 0)
196 return;
198 WDelayedActionSet--;
200 * Make the event dispatcher do whatever it needs to do,
201 * including handling zombie processes, restart and exit
202 * signals.
204 DispatchEvent(NULL);
206 #endif
209 *----------------------------------------------------------------------
210 * handleExitSig--
211 * User generated exit signal handler.
212 *----------------------------------------------------------------------
214 static RETSIGTYPE handleExitSig(int sig)
216 sigset_t sigs;
218 sigfillset(&sigs);
219 sigprocmask(SIG_BLOCK, &sigs, NULL);
221 if (sig == SIGUSR1) {
222 wwarning("got signal %i - restarting", sig);
223 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
224 } else if (sig == SIGUSR2) {
225 wwarning("got signal %i - rereading defaults", sig);
226 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
227 } else if (sig == SIGTERM || sig == SIGINT || sig == SIGHUP) {
228 wwarning("got signal %i - exiting...", sig);
229 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
232 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
233 DispatchEvent(NULL); /* Dispatch events imediately. */
236 /* Dummy signal handler */
237 static void dummyHandler(int sig)
242 *----------------------------------------------------------------------
243 * handleSig--
244 * general signal handler. Exits the program gently.
245 *----------------------------------------------------------------------
247 static RETSIGTYPE handleSig(int sig)
249 wfatal("got signal %i", sig);
251 /* Setting the signal behaviour back to default and then reraising the
252 * signal is a cleaner way to make program exit and core dump than calling
253 * abort(), since it correctly returns from the signal handler and sets
254 * the flags accordingly. -Dan
256 if (sig == SIGSEGV || sig == SIGFPE || sig == SIGBUS || sig == SIGILL || sig == SIGABRT) {
257 signal(sig, SIG_DFL);
258 kill(getpid(), sig);
259 return;
262 wAbort(0);
265 static RETSIGTYPE buryChild(int foo)
267 pid_t pid;
268 int status;
269 int save_errno = errno;
270 sigset_t sigs;
272 sigfillset(&sigs);
273 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
274 sigprocmask(SIG_BLOCK, &sigs, NULL);
276 /* R.I.P. */
277 /* If 2 or more kids exit in a small time window, before this handler gets
278 * the chance to get invoked, the SIGCHLD signals will be merged and only
279 * one SIGCHLD signal will be sent to us. We use a while loop to get all
280 * exited child status because we can't count on the number of SIGCHLD
281 * signals to know exactly how many kids have exited. -Dan
283 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || (pid < 0 && errno == EINTR)) {
284 NotifyDeadProcess(pid, WEXITSTATUS(status));
287 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
289 errno = save_errno;
292 static void getOffendingModifiers(void)
294 int i;
295 XModifierKeymap *modmap;
296 KeyCode nlock, slock;
297 static int mask_table[8] = {
298 ShiftMask, LockMask, ControlMask, Mod1Mask,
299 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
302 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
303 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
306 * Find out the masks for the NumLock and ScrollLock modifiers,
307 * so that we can bind the grabs for when they are enabled too.
309 modmap = XGetModifierMapping(dpy);
311 if (modmap != NULL && modmap->max_keypermod > 0) {
312 for (i = 0; i < 8 * modmap->max_keypermod; i++) {
313 if (modmap->modifiermap[i] == nlock && nlock != 0)
314 _NumLockMask = mask_table[i / modmap->max_keypermod];
315 else if (modmap->modifiermap[i] == slock && slock != 0)
316 _ScrollLockMask = mask_table[i / modmap->max_keypermod];
320 if (modmap)
321 XFreeModifiermap(modmap);
324 #ifdef NUMLOCK_HACK
325 void
326 wHackedGrabKey(int keycode, unsigned int modifiers,
327 Window grab_window, Bool owner_events, int pointer_mode, int keyboard_mode)
329 if (modifiers == AnyModifier)
330 return;
332 /* grab all combinations of the modifier with CapsLock, NumLock and
333 * ScrollLock. How much memory/CPU does such a monstrosity consume
334 * in the server?
336 if (_NumLockMask)
337 XGrabKey(dpy, keycode, modifiers | _NumLockMask,
338 grab_window, owner_events, pointer_mode, keyboard_mode);
339 if (_ScrollLockMask)
340 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask,
341 grab_window, owner_events, pointer_mode, keyboard_mode);
342 if (_NumLockMask && _ScrollLockMask)
343 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask,
344 grab_window, owner_events, pointer_mode, keyboard_mode);
345 if (_NumLockMask)
346 XGrabKey(dpy, keycode, modifiers | _NumLockMask | LockMask,
347 grab_window, owner_events, pointer_mode, keyboard_mode);
348 if (_ScrollLockMask)
349 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask | LockMask,
350 grab_window, owner_events, pointer_mode, keyboard_mode);
351 if (_NumLockMask && _ScrollLockMask)
352 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask | LockMask,
353 grab_window, owner_events, pointer_mode, keyboard_mode);
354 /* phew, I guess that's all, right? */
356 #endif
358 void
359 wHackedGrabButton(unsigned int button, unsigned int modifiers,
360 Window grab_window, Bool owner_events,
361 unsigned int event_mask, int pointer_mode, int keyboard_mode, Window confine_to, Cursor cursor)
363 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
364 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
366 if (modifiers == AnyModifier)
367 return;
369 XGrabButton(dpy, button, modifiers | LockMask, grab_window, owner_events,
370 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
372 #ifdef NUMLOCK_HACK
373 /* same as above, but for mouse buttons */
374 if (_NumLockMask)
375 XGrabButton(dpy, button, modifiers | _NumLockMask,
376 grab_window, owner_events, event_mask, pointer_mode,
377 keyboard_mode, confine_to, cursor);
378 if (_ScrollLockMask)
379 XGrabButton(dpy, button, modifiers | _ScrollLockMask,
380 grab_window, owner_events, event_mask, pointer_mode,
381 keyboard_mode, confine_to, cursor);
382 if (_NumLockMask && _ScrollLockMask)
383 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask,
384 grab_window, owner_events, event_mask, pointer_mode,
385 keyboard_mode, confine_to, cursor);
386 if (_NumLockMask)
387 XGrabButton(dpy, button, modifiers | _NumLockMask | LockMask,
388 grab_window, owner_events, event_mask, pointer_mode,
389 keyboard_mode, confine_to, cursor);
390 if (_ScrollLockMask)
391 XGrabButton(dpy, button, modifiers | _ScrollLockMask | LockMask,
392 grab_window, owner_events, event_mask, pointer_mode,
393 keyboard_mode, confine_to, cursor);
394 if (_NumLockMask && _ScrollLockMask)
395 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask | LockMask,
396 grab_window, owner_events, event_mask, pointer_mode,
397 keyboard_mode, confine_to, cursor);
398 #endif /* NUMLOCK_HACK */
401 WScreen *wScreenWithNumber(int i)
403 assert(i < wScreenCount);
405 return wScreen[i];
408 WScreen *wScreenForRootWindow(Window window)
410 int i;
412 if (wScreenCount == 1)
413 return wScreen[0];
415 /* Since the number of heads will probably be small (normally 2),
416 * it should be faster to use this than a hash table, because
417 * of the overhead. */
418 for (i = 0; i < wScreenCount; i++)
419 if (wScreen[i]->root_win == window)
420 return wScreen[i];
422 return wScreenForWindow(window);
425 WScreen *wScreenForWindow(Window window)
427 XWindowAttributes attr;
429 if (wScreenCount == 1)
430 return wScreen[0];
432 if (XGetWindowAttributes(dpy, window, &attr))
433 return wScreenForRootWindow(attr.root);
435 return NULL;
438 static char *atomNames[] = {
439 "WM_STATE",
440 "WM_CHANGE_STATE",
441 "WM_PROTOCOLS",
442 "WM_TAKE_FOCUS",
443 "WM_DELETE_WINDOW",
444 "WM_SAVE_YOURSELF",
445 "WM_CLIENT_LEADER",
446 "WM_COLORMAP_WINDOWS",
447 "WM_COLORMAP_NOTIFY",
449 "_WINDOWMAKER_MENU",
450 "_WINDOWMAKER_STATE",
451 "_WINDOWMAKER_WM_PROTOCOLS",
452 "_WINDOWMAKER_WM_FUNCTION",
453 "_WINDOWMAKER_NOTICEBOARD",
454 "_WINDOWMAKER_COMMAND",
455 "_WINDOWMAKER_ICON_SIZE",
456 "_WINDOWMAKER_ICON_TILE",
458 GNUSTEP_WM_ATTR_NAME,
459 GNUSTEP_WM_MINIATURIZE_WINDOW,
460 GNUSTEP_TITLEBAR_STATE,
462 "WM_IGNORE_FOCUS_EVENTS"
466 *----------------------------------------------------------
467 * StartUp--
468 * starts the window manager and setup global data.
469 * Called from main() at startup.
471 * Side effects:
472 * global data declared in main.c is initialized
473 *----------------------------------------------------------
475 void StartUp(Bool defaultScreenOnly)
477 struct sigaction sig_action;
478 int i, j, max;
479 char **formats;
480 #ifdef HAVE_XRANDR
481 int dummy;
482 #endif
483 Atom atom[sizeof(atomNames) / sizeof(atomNames[0])];
486 * Ignore CapsLock in modifiers
488 ValidModMask = 0xff & ~LockMask;
490 getOffendingModifiers();
492 * Ignore NumLock and ScrollLock too
494 ValidModMask &= ~(_NumLockMask | _ScrollLockMask);
496 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
498 wWinContext = XUniqueContext();
499 wAppWinContext = XUniqueContext();
500 wStackContext = XUniqueContext();
501 wVEdgeContext = XUniqueContext();
503 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False); */
505 #ifdef HAVE_XINTERNATOMS
506 XInternAtoms(dpy, atomNames, sizeof(atomNames) / sizeof(atomNames[0]), False, atom);
507 #else
510 int i;
511 for (i = 0; i < sizeof(atomNames) / sizeof(atomNames[0]); i++)
512 atom[i] = XInternAtom(dpy, atomNames[i], False);
514 #endif
516 _XA_WM_STATE = atom[0];
517 _XA_WM_CHANGE_STATE = atom[1];
518 _XA_WM_PROTOCOLS = atom[2];
519 _XA_WM_TAKE_FOCUS = atom[3];
520 _XA_WM_DELETE_WINDOW = atom[4];
521 _XA_WM_SAVE_YOURSELF = atom[5];
522 _XA_WM_CLIENT_LEADER = atom[6];
523 _XA_WM_COLORMAP_WINDOWS = atom[7];
524 _XA_WM_COLORMAP_NOTIFY = atom[8];
526 _XA_WINDOWMAKER_MENU = atom[9];
527 _XA_WINDOWMAKER_STATE = atom[10];
528 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
529 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
530 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
531 _XA_WINDOWMAKER_COMMAND = atom[14];
532 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
533 _XA_WINDOWMAKER_ICON_TILE = atom[16];
535 _XA_GNUSTEP_WM_ATTR = atom[17];
536 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
537 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
539 _XA_WM_IGNORE_FOCUS_EVENTS = atom[20];
541 #ifdef XDND
542 wXDNDInitializeAtoms();
543 #endif
545 /* cursors */
546 wPreferences.cursor[WCUR_NORMAL] = None; /* inherit from root */
547 wPreferences.cursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
548 wPreferences.cursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
549 wPreferences.cursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
550 wPreferences.cursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
551 wPreferences.cursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
552 wPreferences.cursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
553 wPreferences.cursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
554 wPreferences.cursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
555 wPreferences.cursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
556 wPreferences.cursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
557 wPreferences.cursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
558 wPreferences.cursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
559 wPreferences.cursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name??? */
560 wPreferences.cursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
562 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
563 GC gc = XCreateGC(dpy, cur, 0, NULL);
564 XColor black;
565 memset(&black, 0, sizeof(XColor));
566 XSetForeground(dpy, gc, 0);
567 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
568 XFreeGC(dpy, gc);
569 wPreferences.cursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
570 XFreePixmap(dpy, cur);
573 #ifndef HAVE_INOTIFY
574 /* signal handler stuff that gets called when a signal is caught */
575 WMAddPersistentTimerHandler(500, delayedAction, NULL);
576 #endif
578 /* emergency exit... */
579 sig_action.sa_handler = handleSig;
580 sigemptyset(&sig_action.sa_mask);
582 sig_action.sa_flags = SA_RESTART;
583 sigaction(SIGQUIT, &sig_action, NULL);
584 /* instead of catching these, we let the default handler abort the
585 * program. The new monitor process will take appropriate action
586 * when it detects the crash.
587 sigaction(SIGSEGV, &sig_action, NULL);
588 sigaction(SIGBUS, &sig_action, NULL);
589 sigaction(SIGFPE, &sig_action, NULL);
590 sigaction(SIGABRT, &sig_action, NULL);
593 sig_action.sa_handler = handleExitSig;
595 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
596 * immediately. -Dan */
597 sig_action.sa_flags = SA_RESTART;
598 sigaction(SIGTERM, &sig_action, NULL);
599 sigaction(SIGINT, &sig_action, NULL);
600 sigaction(SIGHUP, &sig_action, NULL);
601 sigaction(SIGUSR1, &sig_action, NULL);
602 sigaction(SIGUSR2, &sig_action, NULL);
604 /* ignore dead pipe */
605 /* Because POSIX mandates that only signal with handlers are reset
606 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
607 * to children. Hence the dummy handler.
608 * Philippe Troin <phil@fifi.org>
610 sig_action.sa_handler = &dummyHandler;
611 sig_action.sa_flags = SA_RESTART;
612 sigaction(SIGPIPE, &sig_action, NULL);
614 /* handle dead children */
615 sig_action.sa_handler = buryChild;
616 sig_action.sa_flags = SA_NOCLDSTOP | SA_RESTART;
617 sigaction(SIGCHLD, &sig_action, NULL);
619 /* Now we unblock all signals, that may have been blocked by the parent
620 * who exec()-ed us. This can happen for example if Window Maker crashes
621 * and restarts itself or another window manager from the signal handler.
622 * In this case, the new proccess inherits the blocked signal mask and
623 * will no longer react to that signal, until unblocked.
624 * This is because the signal handler of the proccess who crashed (parent)
625 * didn't return, and the signal remained blocked. -Dan
627 sigfillset(&sig_action.sa_mask);
628 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
630 /* handle X shutdowns a such */
631 XSetIOErrorHandler(handleXIO);
633 /* set hook for out event dispatcher in WINGs event dispatcher */
634 WMHookEventHandler(DispatchEvent);
636 /* initialize defaults stuff */
637 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
638 if (!WDWindowMaker->dictionary)
639 wwarning(_("could not read domain \"%s\" from defaults database"), "WindowMaker");
641 /* read defaults that don't change until a restart and are
642 * screen independent */
643 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
645 /* check sanity of some values */
646 if (wPreferences.icon_size < 16) {
647 wwarning(_("icon size is configured to %i, but it's too small. Using 16 instead"),
648 wPreferences.icon_size);
649 wPreferences.icon_size = 16;
652 /* init other domains */
653 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
654 if (!WDRootMenu->dictionary)
655 wwarning(_("could not read domain \"%s\" from defaults database"), "WMRootMenu");
657 wDefaultsMergeGlobalMenus(WDRootMenu);
659 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
660 if (!WDWindowAttributes->dictionary)
661 wwarning(_("could not read domain \"%s\" from defaults database"), "WMWindowAttributes");
663 XSetErrorHandler((XErrorHandler) catchXError);
665 #ifdef SHAPE
666 /* ignore j */
667 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
668 #endif
670 #ifdef HAVE_XRANDR
671 has_randr = XRRQueryExtension(dpy, &randr_event_base, &dummy);
672 #endif
674 #ifdef KEEP_XKB_LOCK_STATUS
675 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
676 if (wPreferences.modelock && !wXkbSupported) {
677 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
678 wPreferences.modelock = 0;
680 #endif
682 if (defaultScreenOnly)
683 max = 1;
684 else
685 max = ScreenCount(dpy);
687 wScreen = wmalloc(sizeof(WScreen *) * max);
689 wScreenCount = 0;
691 /* Check if TIFF images are supported */
692 formats = RSupportedFileFormats();
693 if (formats) {
694 for (i = 0; formats[i] != NULL; i++) {
695 if (strcmp(formats[i], "TIFF") == 0) {
696 wPreferences.supports_tiff = 1;
697 break;
702 /* manage the screens */
703 for (j = 0; j < max; j++) {
704 if (defaultScreenOnly || max == 1) {
705 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
706 if (!wScreen[wScreenCount]) {
707 wfatal(_("it seems that there is already a window manager running"));
708 Exit(1);
710 } else {
711 wScreen[wScreenCount] = wScreenInit(j);
712 if (!wScreen[wScreenCount]) {
713 wwarning(_("could not manage screen %i"), j);
714 continue;
717 wScreenCount++;
720 InitializeSwitchMenu();
722 /* initialize/restore state for the screens */
723 for (j = 0; j < wScreenCount; j++) {
724 int lastDesktop;
726 lastDesktop = wNETWMGetCurrentDesktopFromHint(wScreen[j]);
728 wScreenRestoreState(wScreen[j]);
730 /* manage all windows that were already here before us */
731 if (!wPreferences.flags.nodock && wScreen[j]->dock)
732 wScreen[j]->last_dock = wScreen[j]->dock;
734 manageAllWindows(wScreen[j], wPreferences.flags.restarting == 2);
736 /* restore saved menus */
737 wMenuRestoreState(wScreen[j]);
739 /* If we're not restarting, restore session */
740 if (wPreferences.flags.restarting == 0 && !wPreferences.flags.norestore)
741 wSessionRestoreState(wScreen[j]);
743 if (!wPreferences.flags.noautolaunch) {
744 /* auto-launch apps */
745 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
746 wScreen[j]->last_dock = wScreen[j]->dock;
747 wDockDoAutoLaunch(wScreen[j]->dock, 0);
749 /* auto-launch apps in clip */
750 if (!wPreferences.flags.noclip) {
751 int i;
752 for (i = 0; i < wScreen[j]->workspace_count; i++) {
753 if (wScreen[j]->workspaces[i]->clip) {
754 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
755 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
759 /* auto-launch apps in drawers */
760 if (!wPreferences.flags.nodrawer) {
761 WDrawerChain *dc;
762 for (dc = wScreen[j]->drawers; dc; dc = dc->next) {
763 wScreen[j]->last_dock = dc->adrawer;
764 wDockDoAutoLaunch(dc->adrawer, 0);
769 /* go to workspace where we were before restart */
770 if (lastDesktop >= 0)
771 wWorkspaceForceChange(wScreen[j], lastDesktop);
772 else
773 wSessionRestoreLastWorkspace(wScreen[j]);
776 if (wScreenCount == 0) {
777 wfatal(_("could not manage any screen"));
778 Exit(1);
781 #ifndef HAVE_INOTIFY
782 /* setup defaults file polling */
783 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates)
784 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
785 #endif
789 static Bool windowInList(Window window, Window * list, int count)
791 for (; count >= 0; count--) {
792 if (window == list[count])
793 return True;
795 return False;
799 *-----------------------------------------------------------------------
800 * manageAllWindows--
801 * Manages all windows in the screen.
803 * Notes:
804 * Called when the wm is being started.
805 * No events can be processed while the windows are being
806 * reparented/managed.
807 *-----------------------------------------------------------------------
809 static void manageAllWindows(WScreen * scr, int crashRecovery)
811 Window root, parent;
812 Window *children;
813 unsigned int nchildren;
814 unsigned int i, j;
815 WWindow *wwin;
817 XGrabServer(dpy);
818 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
820 scr->flags.startup = 1;
822 /* first remove all icon windows */
823 for (i = 0; i < nchildren; i++) {
824 XWMHints *wmhints;
826 if (children[i] == None)
827 continue;
829 wmhints = XGetWMHints(dpy, children[i]);
830 if (wmhints && (wmhints->flags & IconWindowHint)) {
831 for (j = 0; j < nchildren; j++) {
832 if (children[j] == wmhints->icon_window) {
833 XFree(wmhints);
834 wmhints = NULL;
835 children[j] = None;
836 break;
840 if (wmhints) {
841 XFree(wmhints);
845 for (i = 0; i < nchildren; i++) {
846 if (children[i] == None)
847 continue;
849 wwin = wManageWindow(scr, children[i]);
850 if (wwin) {
851 /* apply states got from WSavedState */
852 /* shaded + minimized is not restored correctly */
853 if (wwin->flags.shaded) {
854 wwin->flags.shaded = 0;
855 wShadeWindow(wwin);
857 if (wwin->flags.miniaturized
858 && (wwin->transient_for == None
859 || wwin->transient_for == scr->root_win
860 || !windowInList(wwin->transient_for, children, nchildren))) {
862 wwin->flags.skip_next_animation = 1;
863 wwin->flags.miniaturized = 0;
864 wIconifyWindow(wwin);
865 } else {
866 wClientSetState(wwin, NormalState, None);
868 if (crashRecovery) {
869 int border;
871 border = (!HAS_BORDER(wwin) ? 0 : scr->frame_border_width);
873 wWindowMove(wwin, wwin->frame_x - border,
874 wwin->frame_y - border -
875 (wwin->frame->titlebar ? wwin->frame->titlebar->height : 0));
879 XUngrabServer(dpy);
881 /* hide apps */
882 wwin = scr->focused_window;
883 while (wwin) {
884 if (wwin->flags.hidden) {
885 WApplication *wapp = wApplicationOf(wwin->main_window);
887 if (wapp) {
888 wwin->flags.hidden = 0;
889 wHideApplication(wapp);
890 } else {
891 wwin->flags.hidden = 0;
894 wwin = wwin->prev;
897 XFree(children);
898 scr->flags.startup = 0;
899 scr->flags.startup2 = 1;
901 while (XPending(dpy)) {
902 XEvent ev;
903 WMNextEvent(dpy, &ev);
904 WMHandleEvent(&ev);
906 scr->last_workspace = 0;
907 wWorkspaceForceChange(scr, 0);
908 if (!wPreferences.flags.noclip)
909 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
910 scr->flags.startup2 = 0;