Fix enable-modelock warnings
[wmaker-crm.git] / src / startup.c
blob6ef3580eb1449aa5b8afeb62a3699d195aacfeae
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <sys/wait.h>
32 #ifdef __FreeBSD__
33 #include <sys/signal.h>
34 #endif
36 #include <X11/Xlib.h>
37 #include <X11/Xresource.h>
38 #include <X11/Xutil.h>
39 #include <X11/cursorfont.h>
40 #include <X11/Xproto.h>
41 #include <X11/keysym.h>
42 #ifdef SHAPE
43 #include <X11/extensions/shape.h>
44 #endif
45 #ifdef KEEP_XKB_LOCK_STATUS
46 #include <X11/XKBlib.h>
47 #endif
49 #include "WindowMaker.h"
50 #include "GNUstep.h"
51 #include "texture.h"
52 #include "screen.h"
53 #include "window.h"
54 #include "actions.h"
55 #include "client.h"
56 #include "funcs.h"
57 #include "dock.h"
58 #include "workspace.h"
59 #include "keybind.h"
60 #include "framewin.h"
61 #include "session.h"
62 #include "defaults.h"
63 #include "properties.h"
64 #include "dialog.h"
65 #include "wmspec.h"
66 #ifdef XDND
67 #include "xdnd.h"
68 #endif
70 #include "xutil.h"
72 /* for SunOS */
73 #ifndef SA_RESTART
74 # define SA_RESTART 0
75 #endif
77 /* Just in case, for weirdo systems */
78 #ifndef SA_NODEFER
79 # define SA_NODEFER 0
80 #endif
82 /****** Global Variables ******/
84 extern WPreferences wPreferences;
86 extern WDDomain *WDWindowMaker;
87 extern WDDomain *WDRootMenu;
88 extern WDDomain *WDWindowAttributes;
90 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;
121 extern Atom _XA_GNUSTEP_WM_ATTR;
123 extern Atom _XA_WINDOWMAKER_MENU;
124 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
125 extern Atom _XA_WINDOWMAKER_STATE;
126 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
127 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
128 extern Atom _XA_WINDOWMAKER_COMMAND;
129 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
130 extern Atom _XA_WINDOWMAKER_ICON_TILE;
132 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
133 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
135 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
137 /* cursors */
138 extern Cursor wCursor[WCUR_LAST];
140 #ifndef HAVE_INOTIFY
141 /* special flags */
142 extern char WDelayedActionSet;
143 #endif
145 /***** Local *****/
147 static WScreen **wScreen = NULL;
149 static unsigned int _NumLockMask = 0;
150 static unsigned int _ScrollLockMask = 0;
152 static void manageAllWindows(WScreen * scr, int crashed);
154 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
156 static int catchXError(Display * dpy, XErrorEvent * error)
158 char buffer[MAXLINE];
160 /* ignore some errors */
161 if (error->resourceid != None
162 && ((error->error_code == BadDrawable && error->request_code == X_GetGeometry)
163 || (error->error_code == BadMatch && (error->request_code == X_SetInputFocus))
164 || (error->error_code == BadWindow)
166 && (error->request_code == X_GetWindowAttributes
167 || error->request_code == X_SetInputFocus
168 || error->request_code == X_ChangeWindowAttributes
169 || error->request_code == X_GetProperty
170 || error->request_code == X_ChangeProperty
171 || error->request_code == X_QueryTree
172 || error->request_code == X_GrabButton
173 || error->request_code == X_UngrabButton
174 || error->request_code == X_SendEvent
175 || error->request_code == X_ConfigureWindow))
177 || (error->request_code == X_InstallColormap))) {
178 return 0;
180 FormatXError(dpy, error, buffer, MAXLINE);
181 wwarning(_("internal X error: %s\n"), buffer);
182 return -1;
186 *----------------------------------------------------------------------
187 * handleXIO-
188 * Handle X shutdowns and other stuff.
189 *----------------------------------------------------------------------
191 static int handleXIO(Display * xio_dpy)
193 dpy = NULL;
194 Exit(0);
195 return 0;
198 #ifndef HAVE_INOTIFY
200 *----------------------------------------------------------------------
201 * delayedAction-
202 * Action to be executed after the signal() handler is exited.
203 *----------------------------------------------------------------------
205 static void delayedAction(void *cdata)
207 if (WDelayedActionSet == 0)
208 return;
210 WDelayedActionSet--;
212 * Make the event dispatcher do whatever it needs to do,
213 * including handling zombie processes, restart and exit
214 * signals.
216 DispatchEvent(NULL);
218 #endif
221 *----------------------------------------------------------------------
222 * handleExitSig--
223 * User generated exit signal handler.
224 *----------------------------------------------------------------------
226 static RETSIGTYPE handleExitSig(int sig)
228 sigset_t sigs;
230 sigfillset(&sigs);
231 sigprocmask(SIG_BLOCK, &sigs, NULL);
233 if (sig == SIGUSR1) {
234 wwarning("got signal %i - restarting\n", sig);
235 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
236 } else if (sig == SIGUSR2) {
237 wwarning("got signal %i - rereading defaults\n", sig);
238 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
239 } else if (sig == SIGTERM || sig == SIGINT || sig == SIGHUP) {
240 wwarning("got signal %i - exiting...\n", sig);
241 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
244 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
245 DispatchEvent(NULL); /* Dispatch events imediately. */
248 /* Dummy signal handler */
249 static void dummyHandler(int sig)
254 *----------------------------------------------------------------------
255 * handleSig--
256 * general signal handler. Exits the program gently.
257 *----------------------------------------------------------------------
259 static RETSIGTYPE handleSig(int sig)
261 wfatal("got signal %i\n", sig);
263 /* Setting the signal behaviour back to default and then reraising the
264 * signal is a cleaner way to make program exit and core dump than calling
265 * abort(), since it correctly returns from the signal handler and sets
266 * the flags accordingly. -Dan
268 if (sig == SIGSEGV || sig == SIGFPE || sig == SIGBUS || sig == SIGILL || sig == SIGABRT) {
269 signal(sig, SIG_DFL);
270 kill(getpid(), sig);
271 return;
274 wAbort(0);
277 static RETSIGTYPE buryChild(int foo)
279 pid_t pid;
280 int status;
281 int save_errno = errno;
282 sigset_t sigs;
284 sigfillset(&sigs);
285 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
286 sigprocmask(SIG_BLOCK, &sigs, NULL);
288 /* R.I.P. */
289 /* If 2 or more kids exit in a small time window, before this handler gets
290 * the chance to get invoked, the SIGCHLD signals will be merged and only
291 * one SIGCHLD signal will be sent to us. We use a while loop to get all
292 * exited child status because we can't count on the number of SIGCHLD
293 * signals to know exactly how many kids have exited. -Dan
295 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || (pid < 0 && errno == EINTR)) {
296 NotifyDeadProcess(pid, WEXITSTATUS(status));
299 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
301 errno = save_errno;
304 static void getOffendingModifiers()
306 int i;
307 XModifierKeymap *modmap;
308 KeyCode nlock, slock;
309 static int mask_table[8] = {
310 ShiftMask, LockMask, ControlMask, Mod1Mask,
311 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
314 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
315 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
318 * Find out the masks for the NumLock and ScrollLock modifiers,
319 * so that we can bind the grabs for when they are enabled too.
321 modmap = XGetModifierMapping(dpy);
323 if (modmap != NULL && modmap->max_keypermod > 0) {
324 for (i = 0; i < 8 * modmap->max_keypermod; i++) {
325 if (modmap->modifiermap[i] == nlock && nlock != 0)
326 _NumLockMask = mask_table[i / modmap->max_keypermod];
327 else if (modmap->modifiermap[i] == slock && slock != 0)
328 _ScrollLockMask = mask_table[i / modmap->max_keypermod];
332 if (modmap)
333 XFreeModifiermap(modmap);
336 #ifdef NUMLOCK_HACK
337 void
338 wHackedGrabKey(int keycode, unsigned int modifiers,
339 Window grab_window, Bool owner_events, int pointer_mode, int keyboard_mode)
341 if (modifiers == AnyModifier)
342 return;
344 /* grab all combinations of the modifier with CapsLock, NumLock and
345 * ScrollLock. How much memory/CPU does such a monstrosity consume
346 * in the server?
348 if (_NumLockMask)
349 XGrabKey(dpy, keycode, modifiers | _NumLockMask,
350 grab_window, owner_events, pointer_mode, keyboard_mode);
351 if (_ScrollLockMask)
352 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask,
353 grab_window, owner_events, pointer_mode, keyboard_mode);
354 if (_NumLockMask && _ScrollLockMask)
355 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask,
356 grab_window, owner_events, pointer_mode, keyboard_mode);
357 if (_NumLockMask)
358 XGrabKey(dpy, keycode, modifiers | _NumLockMask | LockMask,
359 grab_window, owner_events, pointer_mode, keyboard_mode);
360 if (_ScrollLockMask)
361 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask | LockMask,
362 grab_window, owner_events, pointer_mode, keyboard_mode);
363 if (_NumLockMask && _ScrollLockMask)
364 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask | LockMask,
365 grab_window, owner_events, pointer_mode, keyboard_mode);
366 /* phew, I guess that's all, right? */
368 #endif
370 void
371 wHackedGrabButton(unsigned int button, unsigned int modifiers,
372 Window grab_window, Bool owner_events,
373 unsigned int event_mask, int pointer_mode, int keyboard_mode, Window confine_to, Cursor cursor)
375 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
376 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
378 if (modifiers == AnyModifier)
379 return;
381 XGrabButton(dpy, button, modifiers | LockMask, grab_window, owner_events,
382 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
384 #ifdef NUMLOCK_HACK
385 /* same as above, but for mouse buttons */
386 if (_NumLockMask)
387 XGrabButton(dpy, button, modifiers | _NumLockMask,
388 grab_window, owner_events, event_mask, pointer_mode,
389 keyboard_mode, confine_to, cursor);
390 if (_ScrollLockMask)
391 XGrabButton(dpy, button, modifiers | _ScrollLockMask,
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,
396 grab_window, owner_events, event_mask, pointer_mode,
397 keyboard_mode, confine_to, cursor);
398 if (_NumLockMask)
399 XGrabButton(dpy, button, modifiers | _NumLockMask | LockMask,
400 grab_window, owner_events, event_mask, pointer_mode,
401 keyboard_mode, confine_to, cursor);
402 if (_ScrollLockMask)
403 XGrabButton(dpy, button, modifiers | _ScrollLockMask | LockMask,
404 grab_window, owner_events, event_mask, pointer_mode,
405 keyboard_mode, confine_to, cursor);
406 if (_NumLockMask && _ScrollLockMask)
407 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask | LockMask,
408 grab_window, owner_events, event_mask, pointer_mode,
409 keyboard_mode, confine_to, cursor);
410 #endif /* NUMLOCK_HACK */
413 WScreen *wScreenWithNumber(int i)
415 assert(i < wScreenCount);
417 return wScreen[i];
420 WScreen *wScreenForRootWindow(Window window)
422 int i;
424 if (wScreenCount == 1)
425 return wScreen[0];
428 * Since the number of heads will probably be small (normally 2),
429 * it should be faster to use this than a hash table, because
430 * of the overhead.
432 for (i = 0; i < wScreenCount; i++) {
433 if (wScreen[i]->root_win == window) {
434 return wScreen[i];
438 return wScreenForWindow(window);
441 WScreen *wScreenSearchForRootWindow(Window window)
443 int i;
445 if (wScreenCount == 1)
446 return wScreen[0];
449 * Since the number of heads will probably be small (normally 2),
450 * it should be faster to use this than a hash table, because
451 * of the overhead.
453 for (i = 0; i < wScreenCount; i++) {
454 if (wScreen[i]->root_win == window) {
455 return wScreen[i];
459 return wScreenForWindow(window);
462 WScreen *wScreenForWindow(Window window)
464 XWindowAttributes attr;
466 if (wScreenCount == 1)
467 return wScreen[0];
469 if (XGetWindowAttributes(dpy, window, &attr)) {
470 return wScreenForRootWindow(attr.root);
472 return NULL;
475 static char *atomNames[] = {
476 "WM_STATE",
477 "WM_CHANGE_STATE",
478 "WM_PROTOCOLS",
479 "WM_TAKE_FOCUS",
480 "WM_DELETE_WINDOW",
481 "WM_SAVE_YOURSELF",
482 "WM_CLIENT_LEADER",
483 "WM_COLORMAP_WINDOWS",
484 "WM_COLORMAP_NOTIFY",
486 "_WINDOWMAKER_MENU",
487 "_WINDOWMAKER_STATE",
488 "_WINDOWMAKER_WM_PROTOCOLS",
489 "_WINDOWMAKER_WM_FUNCTION",
490 "_WINDOWMAKER_NOTICEBOARD",
491 "_WINDOWMAKER_COMMAND",
492 "_WINDOWMAKER_ICON_SIZE",
493 "_WINDOWMAKER_ICON_TILE",
495 GNUSTEP_WM_ATTR_NAME,
496 GNUSTEP_WM_MINIATURIZE_WINDOW,
497 GNUSTEP_TITLEBAR_STATE,
499 "WM_IGNORE_FOCUS_EVENTS"
503 *----------------------------------------------------------
504 * StartUp--
505 * starts the window manager and setup global data.
506 * Called from main() at startup.
508 * Side effects:
509 * global data declared in main.c is initialized
510 *----------------------------------------------------------
512 void StartUp(Bool defaultScreenOnly)
514 struct sigaction sig_action;
515 int j, max;
516 Atom atom[sizeof(atomNames) / sizeof(char *)];
519 * Ignore CapsLock in modifiers
521 ValidModMask = 0xff & ~LockMask;
523 getOffendingModifiers();
525 * Ignore NumLock and ScrollLock too
527 ValidModMask &= ~(_NumLockMask | _ScrollLockMask);
529 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
531 wWinContext = XUniqueContext();
532 wAppWinContext = XUniqueContext();
533 wStackContext = XUniqueContext();
534 wVEdgeContext = XUniqueContext();
536 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False); */
538 #ifdef HAVE_XINTERNATOMS
539 XInternAtoms(dpy, atomNames, sizeof(atomNames) / sizeof(char *), False, atom);
540 #else
543 int i;
544 for (i = 0; i < sizeof(atomNames) / sizeof(char *); i++) {
545 atom[i] = XInternAtom(dpy, atomNames[i], False);
548 #endif
550 _XA_WM_STATE = atom[0];
551 _XA_WM_CHANGE_STATE = atom[1];
552 _XA_WM_PROTOCOLS = atom[2];
553 _XA_WM_TAKE_FOCUS = atom[3];
554 _XA_WM_DELETE_WINDOW = atom[4];
555 _XA_WM_SAVE_YOURSELF = atom[5];
556 _XA_WM_CLIENT_LEADER = atom[6];
557 _XA_WM_COLORMAP_WINDOWS = atom[7];
558 _XA_WM_COLORMAP_NOTIFY = atom[8];
560 _XA_WINDOWMAKER_MENU = atom[9];
561 _XA_WINDOWMAKER_STATE = atom[10];
562 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
563 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
564 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
565 _XA_WINDOWMAKER_COMMAND = atom[14];
566 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
567 _XA_WINDOWMAKER_ICON_TILE = atom[16];
569 _XA_GNUSTEP_WM_ATTR = atom[17];
570 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
571 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
573 _XA_WM_IGNORE_FOCUS_EVENTS = atom[20];
575 #ifdef XDND
576 wXDNDInitializeAtoms();
577 #endif
579 /* cursors */
580 wCursor[WCUR_NORMAL] = None; /* inherit from root */
581 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
582 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
583 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
584 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
585 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
586 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
587 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
588 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
589 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
590 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
591 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
592 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
593 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name??? */
594 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
596 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
597 GC gc = XCreateGC(dpy, cur, 0, NULL);
598 XColor black;
599 memset(&black, 0, sizeof(XColor));
600 XSetForeground(dpy, gc, 0);
601 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
602 XFreeGC(dpy, gc);
603 wCursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
604 XFreePixmap(dpy, cur);
607 #ifndef HAVE_INOTIFY
608 /* signal handler stuff that gets called when a signal is caught */
609 WMAddPersistentTimerHandler(500, delayedAction, NULL);
610 #endif
612 /* emergency exit... */
613 sig_action.sa_handler = handleSig;
614 sigemptyset(&sig_action.sa_mask);
616 sig_action.sa_flags = SA_RESTART;
617 sigaction(SIGQUIT, &sig_action, NULL);
618 /* instead of catching these, we let the default handler abort the
619 * program. The new monitor process will take appropriate action
620 * when it detects the crash.
621 sigaction(SIGSEGV, &sig_action, NULL);
622 sigaction(SIGBUS, &sig_action, NULL);
623 sigaction(SIGFPE, &sig_action, NULL);
624 sigaction(SIGABRT, &sig_action, NULL);
627 sig_action.sa_handler = handleExitSig;
629 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
630 * immediately. -Dan */
631 sig_action.sa_flags = SA_RESTART;
632 sigaction(SIGTERM, &sig_action, NULL);
633 sigaction(SIGINT, &sig_action, NULL);
634 sigaction(SIGHUP, &sig_action, NULL);
635 sigaction(SIGUSR1, &sig_action, NULL);
636 sigaction(SIGUSR2, &sig_action, NULL);
638 /* ignore dead pipe */
639 /* Because POSIX mandates that only signal with handlers are reset
640 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
641 * to children. Hence the dummy handler.
642 * Philippe Troin <phil@fifi.org>
644 sig_action.sa_handler = &dummyHandler;
645 sig_action.sa_flags = SA_RESTART;
646 sigaction(SIGPIPE, &sig_action, NULL);
648 /* handle dead children */
649 sig_action.sa_handler = buryChild;
650 sig_action.sa_flags = SA_NOCLDSTOP | SA_RESTART;
651 sigaction(SIGCHLD, &sig_action, NULL);
653 /* Now we unblock all signals, that may have been blocked by the parent
654 * who exec()-ed us. This can happen for example if Window Maker crashes
655 * and restarts itself or another window manager from the signal handler.
656 * In this case, the new proccess inherits the blocked signal mask and
657 * will no longer react to that signal, until unblocked.
658 * This is because the signal handler of the proccess who crashed (parent)
659 * didn't return, and the signal remained blocked. -Dan
661 sigfillset(&sig_action.sa_mask);
662 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
664 /* handle X shutdowns a such */
665 XSetIOErrorHandler(handleXIO);
667 /* set hook for out event dispatcher in WINGs event dispatcher */
668 WMHookEventHandler(DispatchEvent);
670 /* initialize defaults stuff */
671 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
672 if (!WDWindowMaker->dictionary) {
673 wwarning(_("could not read domain \"%s\" from defaults database"), "WindowMaker");
676 /* read defaults that don't change until a restart and are
677 * screen independent */
678 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
680 /* check sanity of some values */
681 if (wPreferences.icon_size < 16) {
682 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
683 wPreferences.icon_size);
684 wPreferences.icon_size = 16;
687 /* init other domains */
688 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
689 if (!WDRootMenu->dictionary) {
690 wwarning(_("could not read domain \"%s\" from defaults database"), "WMRootMenu");
692 wDefaultsMergeGlobalMenus(WDRootMenu);
694 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
695 if (!WDWindowAttributes->dictionary) {
696 wwarning(_("could not read domain \"%s\" from defaults database"), "WMWindowAttributes");
699 XSetErrorHandler((XErrorHandler) catchXError);
701 #ifdef SHAPE
702 /* ignore j */
703 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
704 #endif
706 #ifdef KEEP_XKB_LOCK_STATUS
707 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
708 if (wPreferences.modelock && !wXkbSupported) {
709 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
710 wPreferences.modelock = 0;
712 #endif
714 if (defaultScreenOnly) {
715 max = 1;
716 } else {
717 max = ScreenCount(dpy);
719 wScreen = wmalloc(sizeof(WScreen *) * max);
721 wScreenCount = 0;
723 /* manage the screens */
724 for (j = 0; j < max; j++) {
725 if (defaultScreenOnly || max == 1) {
726 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
727 if (!wScreen[wScreenCount]) {
728 wfatal(_("it seems that there is already a window manager running"));
729 Exit(1);
731 } else {
732 wScreen[wScreenCount] = wScreenInit(j);
733 if (!wScreen[wScreenCount]) {
734 wwarning(_("could not manage screen %i"), j);
735 continue;
738 wScreenCount++;
741 InitializeSwitchMenu();
743 /* initialize/restore state for the screens */
744 for (j = 0; j < wScreenCount; j++) {
745 int lastDesktop;
747 lastDesktop = wNETWMGetCurrentDesktopFromHint(wScreen[j]);
749 wScreenRestoreState(wScreen[j]);
751 /* manage all windows that were already here before us */
752 if (!wPreferences.flags.nodock && wScreen[j]->dock)
753 wScreen[j]->last_dock = wScreen[j]->dock;
755 manageAllWindows(wScreen[j], wPreferences.flags.restarting == 2);
757 /* restore saved menus */
758 wMenuRestoreState(wScreen[j]);
760 /* If we're not restarting, restore session */
761 if (wPreferences.flags.restarting == 0 && !wPreferences.flags.norestore)
762 wSessionRestoreState(wScreen[j]);
764 if (!wPreferences.flags.noautolaunch) {
765 /* auto-launch apps */
766 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
767 wScreen[j]->last_dock = wScreen[j]->dock;
768 wDockDoAutoLaunch(wScreen[j]->dock, 0);
770 /* auto-launch apps in clip */
771 if (!wPreferences.flags.noclip) {
772 int i;
773 for (i = 0; i < wScreen[j]->workspace_count; i++) {
774 if (wScreen[j]->workspaces[i]->clip) {
775 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
776 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
782 /* go to workspace where we were before restart */
783 if (lastDesktop >= 0) {
784 wWorkspaceForceChange(wScreen[j], lastDesktop);
785 } else {
786 wSessionRestoreLastWorkspace(wScreen[j]);
790 if (wScreenCount == 0) {
791 wfatal(_("could not manage any screen"));
792 Exit(1);
795 #ifndef HAVE_INOTIFY
796 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates) {
797 /* setup defaults file polling */
798 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
800 #endif
804 static Bool windowInList(Window window, Window * list, int count)
806 for (; count >= 0; count--) {
807 if (window == list[count])
808 return True;
810 return False;
814 *-----------------------------------------------------------------------
815 * manageAllWindows--
816 * Manages all windows in the screen.
818 * Notes:
819 * Called when the wm is being started.
820 * No events can be processed while the windows are being
821 * reparented/managed.
822 *-----------------------------------------------------------------------
824 static void manageAllWindows(WScreen * scr, int crashRecovery)
826 Window root, parent;
827 Window *children;
828 unsigned int nchildren;
829 unsigned int i, j;
830 WWindow *wwin;
832 XGrabServer(dpy);
833 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
835 scr->flags.startup = 1;
837 /* first remove all icon windows */
838 for (i = 0; i < nchildren; i++) {
839 XWMHints *wmhints;
841 if (children[i] == None)
842 continue;
844 wmhints = XGetWMHints(dpy, children[i]);
845 if (wmhints && (wmhints->flags & IconWindowHint)) {
846 for (j = 0; j < nchildren; j++) {
847 if (children[j] == wmhints->icon_window) {
848 XFree(wmhints);
849 wmhints = NULL;
850 children[j] = None;
851 break;
855 if (wmhints) {
856 XFree(wmhints);
860 for (i = 0; i < nchildren; i++) {
861 if (children[i] == None)
862 continue;
864 wwin = wManageWindow(scr, children[i]);
865 if (wwin) {
866 /* apply states got from WSavedState */
867 /* shaded + minimized is not restored correctly */
868 if (wwin->flags.shaded) {
869 wwin->flags.shaded = 0;
870 wShadeWindow(wwin);
872 if (wwin->flags.miniaturized
873 && (wwin->transient_for == None
874 || wwin->transient_for == scr->root_win
875 || !windowInList(wwin->transient_for, children, nchildren))) {
877 wwin->flags.skip_next_animation = 1;
878 wwin->flags.miniaturized = 0;
879 wIconifyWindow(wwin);
880 } else {
881 wClientSetState(wwin, NormalState, None);
883 if (crashRecovery) {
884 int border;
886 border = (!HAS_BORDER(wwin) ? 0 : FRAME_BORDER_WIDTH);
888 wWindowMove(wwin, wwin->frame_x - border,
889 wwin->frame_y - border -
890 (wwin->frame->titlebar ? wwin->frame->titlebar->height : 0));
894 XUngrabServer(dpy);
896 /* hide apps */
897 wwin = scr->focused_window;
898 while (wwin) {
899 if (wwin->flags.hidden) {
900 WApplication *wapp = wApplicationOf(wwin->main_window);
902 if (wapp) {
903 wwin->flags.hidden = 0;
904 wHideApplication(wapp);
905 } else {
906 wwin->flags.hidden = 0;
909 wwin = wwin->prev;
912 XFree(children);
913 scr->flags.startup = 0;
914 scr->flags.startup2 = 1;
916 while (XPending(dpy)) {
917 XEvent ev;
918 WMNextEvent(dpy, &ev);
919 WMHandleEvent(&ev);
921 wWorkspaceForceChange(scr, 0);
922 if (!wPreferences.flags.noclip)
923 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
924 scr->flags.startup2 = 0;