Small fix for wmaker nightly build script 2
[wmaker-crm.git] / src / startup.c
blobcf6b0111681efd59d082c2b272bd7128545db529
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
46 #include "WindowMaker.h"
47 #include "GNUstep.h"
48 #include "texture.h"
49 #include "screen.h"
50 #include "window.h"
51 #include "actions.h"
52 #include "client.h"
53 #include "funcs.h"
54 #include "dock.h"
55 #include "workspace.h"
56 #include "keybind.h"
57 #include "framewin.h"
58 #include "session.h"
59 #include "defaults.h"
60 #include "properties.h"
61 #include "dialog.h"
62 #include "wmspec.h"
63 #ifdef XDND
64 #include "xdnd.h"
65 #endif
67 #include "xutil.h"
69 /* for SunOS */
70 #ifndef SA_RESTART
71 # define SA_RESTART 0
72 #endif
74 /* Just in case, for weirdo systems */
75 #ifndef SA_NODEFER
76 # define SA_NODEFER 0
77 #endif
79 /****** Global Variables ******/
81 extern WPreferences wPreferences;
83 extern WDDomain *WDWindowMaker;
84 extern WDDomain *WDRootMenu;
85 extern WDDomain *WDWindowAttributes;
87 extern WShortKey wKeyBindings[WKBD_LAST];
89 extern int wScreenCount;
91 #ifdef SHAPE
92 extern Bool wShapeSupported;
93 extern int wShapeEventBase;
94 #endif
96 #ifdef KEEP_XKB_LOCK_STATUS
97 extern Bool wXkbSupported;
98 extern int wXkbEventBase;
99 #endif
101 /* contexts */
102 extern XContext wWinContext;
103 extern XContext wAppWinContext;
104 extern XContext wStackContext;
105 extern XContext wVEdgeContext;
107 /* atoms */
108 extern Atom _XA_WM_STATE;
109 extern Atom _XA_WM_CHANGE_STATE;
110 extern Atom _XA_WM_PROTOCOLS;
111 extern Atom _XA_WM_TAKE_FOCUS;
112 extern Atom _XA_WM_DELETE_WINDOW;
113 extern Atom _XA_WM_SAVE_YOURSELF;
114 extern Atom _XA_WM_CLIENT_LEADER;
115 extern Atom _XA_WM_COLORMAP_WINDOWS;
116 extern Atom _XA_WM_COLORMAP_NOTIFY;
118 extern Atom _XA_GNUSTEP_WM_ATTR;
120 extern Atom _XA_WINDOWMAKER_MENU;
121 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
122 extern Atom _XA_WINDOWMAKER_STATE;
123 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
124 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
125 extern Atom _XA_WINDOWMAKER_COMMAND;
126 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
127 extern Atom _XA_WINDOWMAKER_ICON_TILE;
129 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
130 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
132 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
134 /* cursors */
135 extern Cursor wCursor[WCUR_LAST];
137 /* special flags */
138 /*extern char WDelayedActionSet;*/
140 /***** Local *****/
142 static WScreen **wScreen = NULL;
144 static unsigned int _NumLockMask = 0;
145 static unsigned int _ScrollLockMask = 0;
147 static void manageAllWindows(WScreen * scr, int crashed);
149 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
151 static int catchXError(Display * dpy, XErrorEvent * error)
153 char buffer[MAXLINE];
155 /* ignore some errors */
156 if (error->resourceid != None
157 && ((error->error_code == BadDrawable && error->request_code == X_GetGeometry)
158 || (error->error_code == BadMatch && (error->request_code == X_SetInputFocus))
159 || (error->error_code == BadWindow)
161 && (error->request_code == X_GetWindowAttributes
162 || error->request_code == X_SetInputFocus
163 || error->request_code == X_ChangeWindowAttributes
164 || error->request_code == X_GetProperty
165 || error->request_code == X_ChangeProperty
166 || error->request_code == X_QueryTree
167 || error->request_code == X_GrabButton
168 || error->request_code == X_UngrabButton
169 || error->request_code == X_SendEvent
170 || error->request_code == X_ConfigureWindow))
172 || (error->request_code == X_InstallColormap))) {
173 #ifndef DEBUG
175 return 0;
176 #else
177 printf("got X error %x %x %x\n", error->request_code,
178 error->error_code, (unsigned)error->resourceid);
179 return 0;
180 #endif
182 FormatXError(dpy, error, buffer, MAXLINE);
183 wwarning(_("internal X error: %s\n"), buffer);
184 return -1;
188 *----------------------------------------------------------------------
189 * handleXIO-
190 * Handle X shutdowns and other stuff.
191 *----------------------------------------------------------------------
193 static int handleXIO(Display * xio_dpy)
195 dpy = NULL;
196 Exit(0);
197 return 0;
201 *----------------------------------------------------------------------
202 * delayedAction-
203 * Action to be executed after the signal() handler is exited.
204 * This was called every 500ms to 'clean up' signals. Not used now.
205 *----------------------------------------------------------------------
207 #ifdef notused
208 static void delayedAction(void *cdata)
210 if (WDelayedActionSet == 0) {
211 return;
213 WDelayedActionSet--;
215 * Make the event dispatcher do whatever it needs to do,
216 * including handling zombie processes, restart and exit
217 * signals.
219 DispatchEvent(NULL);
221 #endif
224 *----------------------------------------------------------------------
225 * handleExitSig--
226 * User generated exit signal handler.
227 *----------------------------------------------------------------------
229 static RETSIGTYPE handleExitSig(int sig)
231 sigset_t sigs;
233 sigfillset(&sigs);
234 sigprocmask(SIG_BLOCK, &sigs, NULL);
236 if (sig == SIGUSR1) {
237 wwarning("got signal %i - restarting\n", sig);
238 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
239 } else if (sig == SIGUSR2) {
240 wwarning("got signal %i - rereading defaults\n", sig);
241 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
242 } else if (sig == SIGTERM || sig == SIGINT || sig == SIGHUP) {
243 wwarning("got signal %i - exiting...\n", sig);
244 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
247 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
248 DispatchEvent(NULL); /* Dispatch events imediately. */
251 /* Dummy signal handler */
252 static void dummyHandler(int sig)
257 *----------------------------------------------------------------------
258 * handleSig--
259 * general signal handler. Exits the program gently.
260 *----------------------------------------------------------------------
262 static RETSIGTYPE handleSig(int sig)
264 wfatal("got signal %i\n", sig);
266 /* Setting the signal behaviour back to default and then reraising the
267 * signal is a cleaner way to make program exit and core dump than calling
268 * abort(), since it correctly returns from the signal handler and sets
269 * the flags accordingly. -Dan
271 if (sig == SIGSEGV || sig == SIGFPE || sig == SIGBUS || sig == SIGILL || sig == SIGABRT) {
272 signal(sig, SIG_DFL);
273 kill(getpid(), sig);
274 return;
277 wAbort(0);
280 static RETSIGTYPE buryChild(int foo)
282 pid_t pid;
283 int status;
284 int save_errno = errno;
285 sigset_t sigs;
287 sigfillset(&sigs);
288 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
289 sigprocmask(SIG_BLOCK, &sigs, NULL);
291 /* R.I.P. */
292 /* If 2 or more kids exit in a small time window, before this handler gets
293 * the chance to get invoked, the SIGCHLD signals will be merged and only
294 * one SIGCHLD signal will be sent to us. We use a while loop to get all
295 * exited child status because we can't count on the number of SIGCHLD
296 * signals to know exactly how many kids have exited. -Dan
298 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || (pid < 0 && errno == EINTR)) {
299 NotifyDeadProcess(pid, WEXITSTATUS(status));
302 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
304 errno = save_errno;
307 static void getOffendingModifiers()
309 int i;
310 XModifierKeymap *modmap;
311 KeyCode nlock, slock;
312 static int mask_table[8] = {
313 ShiftMask, LockMask, ControlMask, Mod1Mask,
314 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
317 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
318 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
321 * Find out the masks for the NumLock and ScrollLock modifiers,
322 * so that we can bind the grabs for when they are enabled too.
324 modmap = XGetModifierMapping(dpy);
326 if (modmap != NULL && modmap->max_keypermod > 0) {
327 for (i = 0; i < 8 * modmap->max_keypermod; i++) {
328 if (modmap->modifiermap[i] == nlock && nlock != 0)
329 _NumLockMask = mask_table[i / modmap->max_keypermod];
330 else if (modmap->modifiermap[i] == slock && slock != 0)
331 _ScrollLockMask = mask_table[i / modmap->max_keypermod];
335 if (modmap)
336 XFreeModifiermap(modmap);
339 #ifdef NUMLOCK_HACK
340 void
341 wHackedGrabKey(int keycode, unsigned int modifiers,
342 Window grab_window, Bool owner_events, int pointer_mode, int keyboard_mode)
344 if (modifiers == AnyModifier)
345 return;
347 /* grab all combinations of the modifier with CapsLock, NumLock and
348 * ScrollLock. How much memory/CPU does such a monstrosity consume
349 * in the server?
351 if (_NumLockMask)
352 XGrabKey(dpy, keycode, modifiers | _NumLockMask,
353 grab_window, owner_events, pointer_mode, keyboard_mode);
354 if (_ScrollLockMask)
355 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask,
356 grab_window, owner_events, pointer_mode, keyboard_mode);
357 if (_NumLockMask && _ScrollLockMask)
358 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask,
359 grab_window, owner_events, pointer_mode, keyboard_mode);
360 if (_NumLockMask)
361 XGrabKey(dpy, keycode, modifiers | _NumLockMask | LockMask,
362 grab_window, owner_events, pointer_mode, keyboard_mode);
363 if (_ScrollLockMask)
364 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask | LockMask,
365 grab_window, owner_events, pointer_mode, keyboard_mode);
366 if (_NumLockMask && _ScrollLockMask)
367 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask | LockMask,
368 grab_window, owner_events, pointer_mode, keyboard_mode);
369 /* phew, I guess that's all, right? */
371 #endif
373 void
374 wHackedGrabButton(unsigned int button, unsigned int modifiers,
375 Window grab_window, Bool owner_events,
376 unsigned int event_mask, int pointer_mode, int keyboard_mode, Window confine_to, Cursor cursor)
378 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
379 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
381 if (modifiers == AnyModifier)
382 return;
384 XGrabButton(dpy, button, modifiers | LockMask, grab_window, owner_events,
385 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
387 #ifdef NUMLOCK_HACK
388 /* same as above, but for mouse buttons */
389 if (_NumLockMask)
390 XGrabButton(dpy, button, modifiers | _NumLockMask,
391 grab_window, owner_events, event_mask, pointer_mode,
392 keyboard_mode, confine_to, cursor);
393 if (_ScrollLockMask)
394 XGrabButton(dpy, button, modifiers | _ScrollLockMask,
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,
399 grab_window, owner_events, event_mask, pointer_mode,
400 keyboard_mode, confine_to, cursor);
401 if (_NumLockMask)
402 XGrabButton(dpy, button, modifiers | _NumLockMask | LockMask,
403 grab_window, owner_events, event_mask, pointer_mode,
404 keyboard_mode, confine_to, cursor);
405 if (_ScrollLockMask)
406 XGrabButton(dpy, button, modifiers | _ScrollLockMask | LockMask,
407 grab_window, owner_events, event_mask, pointer_mode,
408 keyboard_mode, confine_to, cursor);
409 if (_NumLockMask && _ScrollLockMask)
410 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask | LockMask,
411 grab_window, owner_events, event_mask, pointer_mode,
412 keyboard_mode, confine_to, cursor);
413 #endif /* NUMLOCK_HACK */
416 WScreen *wScreenWithNumber(int i)
418 assert(i < wScreenCount);
420 return wScreen[i];
423 WScreen *wScreenForRootWindow(Window window)
425 int i;
427 if (wScreenCount == 1)
428 return wScreen[0];
431 * Since the number of heads will probably be small (normally 2),
432 * it should be faster to use this than a hash table, because
433 * of the overhead.
435 for (i = 0; i < wScreenCount; i++) {
436 if (wScreen[i]->root_win == window) {
437 return wScreen[i];
441 return wScreenForWindow(window);
444 WScreen *wScreenSearchForRootWindow(Window window)
446 int i;
448 if (wScreenCount == 1)
449 return wScreen[0];
452 * Since the number of heads will probably be small (normally 2),
453 * it should be faster to use this than a hash table, because
454 * of the overhead.
456 for (i = 0; i < wScreenCount; i++) {
457 if (wScreen[i]->root_win == window) {
458 return wScreen[i];
462 return wScreenForWindow(window);
465 WScreen *wScreenForWindow(Window window)
467 XWindowAttributes attr;
469 if (wScreenCount == 1)
470 return wScreen[0];
472 if (XGetWindowAttributes(dpy, window, &attr)) {
473 return wScreenForRootWindow(attr.root);
475 return NULL;
478 static char *atomNames[] = {
479 "WM_STATE",
480 "WM_CHANGE_STATE",
481 "WM_PROTOCOLS",
482 "WM_TAKE_FOCUS",
483 "WM_DELETE_WINDOW",
484 "WM_SAVE_YOURSELF",
485 "WM_CLIENT_LEADER",
486 "WM_COLORMAP_WINDOWS",
487 "WM_COLORMAP_NOTIFY",
489 "_WINDOWMAKER_MENU",
490 "_WINDOWMAKER_STATE",
491 "_WINDOWMAKER_WM_PROTOCOLS",
492 "_WINDOWMAKER_WM_FUNCTION",
493 "_WINDOWMAKER_NOTICEBOARD",
494 "_WINDOWMAKER_COMMAND",
495 "_WINDOWMAKER_ICON_SIZE",
496 "_WINDOWMAKER_ICON_TILE",
498 GNUSTEP_WM_ATTR_NAME,
499 GNUSTEP_WM_MINIATURIZE_WINDOW,
500 GNUSTEP_TITLEBAR_STATE,
502 "WM_IGNORE_FOCUS_EVENTS"
506 *----------------------------------------------------------
507 * StartUp--
508 * starts the window manager and setup global data.
509 * Called from main() at startup.
511 * Side effects:
512 * global data declared in main.c is initialized
513 *----------------------------------------------------------
515 void StartUp(Bool defaultScreenOnly)
517 struct sigaction sig_action;
518 int j, max;
519 Atom atom[sizeof(atomNames) / sizeof(char *)];
522 * Ignore CapsLock in modifiers
524 ValidModMask = 0xff & ~LockMask;
526 getOffendingModifiers();
528 * Ignore NumLock and ScrollLock too
530 ValidModMask &= ~(_NumLockMask | _ScrollLockMask);
532 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
534 wWinContext = XUniqueContext();
535 wAppWinContext = XUniqueContext();
536 wStackContext = XUniqueContext();
537 wVEdgeContext = XUniqueContext();
539 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False); */
541 #ifdef HAVE_XINTERNATOMS
542 XInternAtoms(dpy, atomNames, sizeof(atomNames) / sizeof(char *), False, atom);
543 #else
546 int i;
547 for (i = 0; i < sizeof(atomNames) / sizeof(char *); i++) {
548 atom[i] = XInternAtom(dpy, atomNames[i], False);
551 #endif
553 _XA_WM_STATE = atom[0];
554 _XA_WM_CHANGE_STATE = atom[1];
555 _XA_WM_PROTOCOLS = atom[2];
556 _XA_WM_TAKE_FOCUS = atom[3];
557 _XA_WM_DELETE_WINDOW = atom[4];
558 _XA_WM_SAVE_YOURSELF = atom[5];
559 _XA_WM_CLIENT_LEADER = atom[6];
560 _XA_WM_COLORMAP_WINDOWS = atom[7];
561 _XA_WM_COLORMAP_NOTIFY = atom[8];
563 _XA_WINDOWMAKER_MENU = atom[9];
564 _XA_WINDOWMAKER_STATE = atom[10];
565 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
566 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
567 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
568 _XA_WINDOWMAKER_COMMAND = atom[14];
569 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
570 _XA_WINDOWMAKER_ICON_TILE = atom[16];
572 _XA_GNUSTEP_WM_ATTR = atom[17];
573 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
574 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
576 _XA_WM_IGNORE_FOCUS_EVENTS = atom[20];
578 #ifdef XDND
579 wXDNDInitializeAtoms();
580 #endif
582 /* cursors */
583 wCursor[WCUR_NORMAL] = None; /* inherit from root */
584 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
585 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
586 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
587 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
588 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
589 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
590 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
591 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
592 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
593 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
594 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
595 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
596 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name??? */
597 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
599 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
600 GC gc = XCreateGC(dpy, cur, 0, NULL);
601 XColor black;
602 memset(&black, 0, sizeof(XColor));
603 XSetForeground(dpy, gc, 0);
604 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
605 XFreeGC(dpy, gc);
606 wCursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
607 XFreePixmap(dpy, cur);
610 /* emergency exit... */
611 sig_action.sa_handler = handleSig;
612 sigemptyset(&sig_action.sa_mask);
614 sig_action.sa_flags = SA_RESTART;
615 sigaction(SIGQUIT, &sig_action, NULL);
616 /* instead of catching these, we let the default handler abort the
617 * program. The new monitor process will take appropriate action
618 * when it detects the crash.
619 sigaction(SIGSEGV, &sig_action, NULL);
620 sigaction(SIGBUS, &sig_action, NULL);
621 sigaction(SIGFPE, &sig_action, NULL);
622 sigaction(SIGABRT, &sig_action, NULL);
625 sig_action.sa_handler = handleExitSig;
627 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
628 * immediately. -Dan */
629 sig_action.sa_flags = SA_RESTART;
630 sigaction(SIGTERM, &sig_action, NULL);
631 sigaction(SIGINT, &sig_action, NULL);
632 sigaction(SIGHUP, &sig_action, NULL);
633 sigaction(SIGUSR1, &sig_action, NULL);
634 sigaction(SIGUSR2, &sig_action, NULL);
636 /* ignore dead pipe */
637 /* Because POSIX mandates that only signal with handlers are reset
638 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
639 * to children. Hence the dummy handler.
640 * Philippe Troin <phil@fifi.org>
642 sig_action.sa_handler = &dummyHandler;
643 sig_action.sa_flags = SA_RESTART;
644 sigaction(SIGPIPE, &sig_action, NULL);
646 /* handle dead children */
647 sig_action.sa_handler = buryChild;
648 sig_action.sa_flags = SA_NOCLDSTOP | SA_RESTART;
649 sigaction(SIGCHLD, &sig_action, NULL);
651 /* Now we unblock all signals, that may have been blocked by the parent
652 * who exec()-ed us. This can happen for example if Window Maker crashes
653 * and restarts itself or another window manager from the signal handler.
654 * In this case, the new proccess inherits the blocked signal mask and
655 * will no longer react to that signal, until unblocked.
656 * This is because the signal handler of the proccess who crashed (parent)
657 * didn't return, and the signal remained blocked. -Dan
659 sigfillset(&sig_action.sa_mask);
660 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
662 /* handle X shutdowns a such */
663 XSetIOErrorHandler(handleXIO);
665 /* set hook for out event dispatcher in WINGs event dispatcher */
666 WMHookEventHandler(DispatchEvent);
668 /* initialize defaults stuff */
669 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
670 if (!WDWindowMaker->dictionary) {
671 wwarning(_("could not read domain \"%s\" from defaults database"), "WindowMaker");
674 /* read defaults that don't change until a restart and are
675 * screen independent */
676 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
678 /* check sanity of some values */
679 if (wPreferences.icon_size < 16) {
680 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
681 wPreferences.icon_size);
682 wPreferences.icon_size = 16;
685 /* init other domains */
686 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
687 if (!WDRootMenu->dictionary) {
688 wwarning(_("could not read domain \"%s\" from defaults database"), "WMRootMenu");
690 wDefaultsMergeGlobalMenus(WDRootMenu);
692 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
693 if (!WDWindowAttributes->dictionary) {
694 wwarning(_("could not read domain \"%s\" from defaults database"), "WMWindowAttributes");
697 XSetErrorHandler((XErrorHandler) catchXError);
699 #ifdef SHAPE
700 /* ignore j */
701 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
702 #endif
704 #ifdef KEEP_XKB_LOCK_STATUS
705 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
706 if (wPreferences.modelock && !wXkbSupported) {
707 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
708 wPreferences.modelock = 0;
710 #endif
712 if (defaultScreenOnly) {
713 max = 1;
714 } else {
715 max = ScreenCount(dpy);
717 wScreen = wmalloc(sizeof(WScreen *) * max);
719 wScreenCount = 0;
721 /* manage the screens */
722 for (j = 0; j < max; j++) {
723 if (defaultScreenOnly || max == 1) {
724 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
725 if (!wScreen[wScreenCount]) {
726 wfatal(_("it seems that there is already a window manager running"));
727 Exit(1);
729 } else {
730 wScreen[wScreenCount] = wScreenInit(j);
731 if (!wScreen[wScreenCount]) {
732 wwarning(_("could not manage screen %i"), j);
733 continue;
736 wScreenCount++;
739 InitializeSwitchMenu();
741 /* initialize/restore state for the screens */
742 for (j = 0; j < wScreenCount; j++) {
743 int lastDesktop;
745 lastDesktop = wNETWMGetCurrentDesktopFromHint(wScreen[j]);
747 wScreenRestoreState(wScreen[j]);
749 /* manage all windows that were already here before us */
750 if (!wPreferences.flags.nodock && wScreen[j]->dock)
751 wScreen[j]->last_dock = wScreen[j]->dock;
753 manageAllWindows(wScreen[j], wPreferences.flags.restarting == 2);
755 /* restore saved menus */
756 wMenuRestoreState(wScreen[j]);
758 /* If we're not restarting, restore session */
759 if (wPreferences.flags.restarting == 0 && !wPreferences.flags.norestore)
760 wSessionRestoreState(wScreen[j]);
762 if (!wPreferences.flags.noautolaunch) {
763 /* auto-launch apps */
764 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
765 wScreen[j]->last_dock = wScreen[j]->dock;
766 wDockDoAutoLaunch(wScreen[j]->dock, 0);
768 /* auto-launch apps in clip */
769 if (!wPreferences.flags.noclip) {
770 int i;
771 for (i = 0; i < wScreen[j]->workspace_count; i++) {
772 if (wScreen[j]->workspaces[i]->clip) {
773 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
774 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
780 /* go to workspace where we were before restart */
781 if (lastDesktop >= 0) {
782 wWorkspaceForceChange(wScreen[j], lastDesktop);
783 } else {
784 wSessionRestoreLastWorkspace(wScreen[j]);
788 if (wScreenCount == 0) {
789 wfatal(_("could not manage any screen"));
790 Exit(1);
795 static Bool windowInList(Window window, Window * list, int count)
797 for (; count >= 0; count--) {
798 if (window == list[count])
799 return True;
801 return False;
805 *-----------------------------------------------------------------------
806 * manageAllWindows--
807 * Manages all windows in the screen.
809 * Notes:
810 * Called when the wm is being started.
811 * No events can be processed while the windows are being
812 * reparented/managed.
813 *-----------------------------------------------------------------------
815 static void manageAllWindows(WScreen * scr, int crashRecovery)
817 Window root, parent;
818 Window *children;
819 unsigned int nchildren;
820 unsigned int i, j;
821 WWindow *wwin;
823 XGrabServer(dpy);
824 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
826 scr->flags.startup = 1;
828 /* first remove all icon windows */
829 for (i = 0; i < nchildren; i++) {
830 XWMHints *wmhints;
832 if (children[i] == None)
833 continue;
835 wmhints = XGetWMHints(dpy, children[i]);
836 if (wmhints && (wmhints->flags & IconWindowHint)) {
837 for (j = 0; j < nchildren; j++) {
838 if (children[j] == wmhints->icon_window) {
839 XFree(wmhints);
840 wmhints = NULL;
841 children[j] = None;
842 break;
846 if (wmhints) {
847 XFree(wmhints);
851 for (i = 0; i < nchildren; i++) {
852 if (children[i] == None)
853 continue;
855 wwin = wManageWindow(scr, children[i]);
856 if (wwin) {
857 /* apply states got from WSavedState */
858 /* shaded + minimized is not restored correctly */
859 if (wwin->flags.shaded) {
860 wwin->flags.shaded = 0;
861 wShadeWindow(wwin);
863 if (wwin->flags.miniaturized
864 && (wwin->transient_for == None
865 || wwin->transient_for == scr->root_win
866 || !windowInList(wwin->transient_for, children, nchildren))) {
868 wwin->flags.skip_next_animation = 1;
869 wwin->flags.miniaturized = 0;
870 wIconifyWindow(wwin);
871 } else {
872 wClientSetState(wwin, NormalState, None);
874 if (crashRecovery) {
875 int border;
877 border = (!HAS_BORDER(wwin) ? 0 : FRAME_BORDER_WIDTH);
879 wWindowMove(wwin, wwin->frame_x - border,
880 wwin->frame_y - border -
881 (wwin->frame->titlebar ? wwin->frame->titlebar->height : 0));
885 XUngrabServer(dpy);
887 /* hide apps */
888 wwin = scr->focused_window;
889 while (wwin) {
890 if (wwin->flags.hidden) {
891 WApplication *wapp = wApplicationOf(wwin->main_window);
893 if (wapp) {
894 wwin->flags.hidden = 0;
895 wHideApplication(wapp);
896 } else {
897 wwin->flags.hidden = 0;
900 wwin = wwin->prev;
903 XFree(children);
904 scr->flags.startup = 0;
905 scr->flags.startup2 = 1;
907 while (XPending(dpy)) {
908 XEvent ev;
909 WMNextEvent(dpy, &ev);
910 WMHandleEvent(&ev);
912 wWorkspaceForceChange(scr, 0);
913 if (!wPreferences.flags.noclip)
914 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
915 scr->flags.startup2 = 0;