wmaker: Reduce wakeups to zero
[wmaker-crm.git] / src / startup.c
blob12d001defd8c32ac7eab07399cb973a8b30cb06e
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/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
45 #include "WindowMaker.h"
46 #include "GNUstep.h"
47 #include "texture.h"
48 #include "screen.h"
49 #include "window.h"
50 #include "actions.h"
51 #include "client.h"
52 #include "funcs.h"
53 #include "dock.h"
54 #include "workspace.h"
55 #include "keybind.h"
56 #include "framewin.h"
57 #include "session.h"
58 #include "defaults.h"
59 #include "properties.h"
60 #include "dialog.h"
61 #include "wmspec.h"
62 #ifdef XDND
63 #include "xdnd.h"
64 #endif
66 #include "xutil.h"
68 #if 0
69 #ifdef SYS_SIGLIST_DECLARED
70 extern const char * const sys_siglist[];
71 #endif
72 #endif
74 /* for SunOS */
75 #ifndef SA_RESTART
76 # define SA_RESTART 0
77 #endif
79 /* Just in case, for weirdo systems */
80 #ifndef SA_NODEFER
81 # define SA_NODEFER 0
82 #endif
84 /****** Global Variables ******/
86 extern WPreferences wPreferences;
88 extern WDDomain *WDWindowMaker;
89 extern WDDomain *WDRootMenu;
90 extern WDDomain *WDWindowAttributes;
92 extern WShortKey wKeyBindings[WKBD_LAST];
94 extern int wScreenCount;
97 #ifdef SHAPE
98 extern Bool wShapeSupported;
99 extern int wShapeEventBase;
100 #endif
102 #ifdef KEEP_XKB_LOCK_STATUS
103 extern Bool wXkbSupported;
104 extern int wXkbEventBase;
105 #endif
107 /* contexts */
108 extern XContext wWinContext;
109 extern XContext wAppWinContext;
110 extern XContext wStackContext;
111 extern XContext wVEdgeContext;
113 /* atoms */
114 extern Atom _XA_WM_STATE;
115 extern Atom _XA_WM_CHANGE_STATE;
116 extern Atom _XA_WM_PROTOCOLS;
117 extern Atom _XA_WM_TAKE_FOCUS;
118 extern Atom _XA_WM_DELETE_WINDOW;
119 extern Atom _XA_WM_SAVE_YOURSELF;
120 extern Atom _XA_WM_CLIENT_LEADER;
121 extern Atom _XA_WM_COLORMAP_WINDOWS;
122 extern Atom _XA_WM_COLORMAP_NOTIFY;
124 extern Atom _XA_GNUSTEP_WM_ATTR;
126 extern Atom _XA_WINDOWMAKER_MENU;
127 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
128 extern Atom _XA_WINDOWMAKER_STATE;
129 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
130 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
131 extern Atom _XA_WINDOWMAKER_COMMAND;
132 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
133 extern Atom _XA_WINDOWMAKER_ICON_TILE;
135 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
136 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
139 /* cursors */
140 extern Cursor wCursor[WCUR_LAST];
142 /* special flags */
143 extern char WDelayedActionSet;
145 /***** Local *****/
147 static WScreen **wScreen = NULL;
150 static unsigned int _NumLockMask = 0;
151 static unsigned int _ScrollLockMask = 0;
155 static void manageAllWindows(WScreen *scr, int crashed);
157 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
160 static int
161 catchXError(Display *dpy, XErrorEvent *error)
163 char buffer[MAXLINE];
165 /* ignore some errors */
166 if (error->resourceid != None
167 && ((error->error_code == BadDrawable
168 && error->request_code == X_GetGeometry)
169 || (error->error_code == BadMatch
170 && (error->request_code == X_SetInputFocus))
171 || (error->error_code == BadWindow)
173 && (error->request_code == X_GetWindowAttributes
174 || error->request_code == X_SetInputFocus
175 || error->request_code == X_ChangeWindowAttributes
176 || error->request_code == X_GetProperty
177 || error->request_code == X_ChangeProperty
178 || error->request_code == X_QueryTree
179 || error->request_code == X_GrabButton
180 || error->request_code == X_UngrabButton
181 || error->request_code == X_SendEvent
182 || error->request_code == X_ConfigureWindow))
184 || (error->request_code == X_InstallColormap))) {
185 #ifndef DEBUG
187 return 0;
188 #else
189 printf("got X error %x %x %x\n", error->request_code,
190 error->error_code, (unsigned)error->resourceid);
191 return 0;
192 #endif
194 FormatXError(dpy, error, buffer, MAXLINE);
195 wwarning(_("internal X error: %s\n"), buffer);
196 return -1;
201 *----------------------------------------------------------------------
202 * handleXIO-
203 * Handle X shutdowns and other stuff.
204 *----------------------------------------------------------------------
206 static int
207 handleXIO(Display *xio_dpy)
209 dpy = NULL;
210 Exit(0);
211 return 0;
216 *----------------------------------------------------------------------
217 * delayedAction-
218 * Action to be executed after the signal() handler is exited.
219 *----------------------------------------------------------------------
221 static void
222 delayedAction(void *cdata)
224 if (WDelayedActionSet == 0) {
225 return;
227 WDelayedActionSet--;
229 * Make the event dispatcher do whatever it needs to do,
230 * including handling zombie processes, restart and exit
231 * signals.
233 DispatchEvent(NULL);
238 *----------------------------------------------------------------------
239 * handleExitSig--
240 * User generated exit signal handler.
241 *----------------------------------------------------------------------
243 static RETSIGTYPE
244 handleExitSig(int sig)
246 sigset_t sigs;
248 sigfillset(&sigs);
249 sigprocmask(SIG_BLOCK, &sigs, NULL);
251 if (sig == SIGUSR1) {
252 #ifdef SYS_SIGLIST_DECLARED
253 wwarning("got signal %i (%s) - restarting\n", sig, sys_siglist[sig]);
254 #else
255 wwarning("got signal %i - restarting\n", sig);
256 #endif
258 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
259 /* setup idle handler, so that this will be handled when
260 * the select() is returned becaused of the signal, even if
261 * there are no X events in the queue */
262 WDelayedActionSet++;
263 } else if (sig == SIGUSR2) {
264 #ifdef SYS_SIGLIST_DECLARED
265 wwarning("got signal %i (%s) - rereading defaults\n", sig, sys_siglist[sig]);
266 #else
267 wwarning("got signal %i - rereading defaults\n", sig);
268 #endif
270 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
271 /* setup idle handler, so that this will be handled when
272 * the select() is returned becaused of the signal, even if
273 * there are no X events in the queue */
274 WDelayedActionSet++;
275 } else if (sig==SIGTERM || sig==SIGINT || sig==SIGHUP) {
276 #ifdef SYS_SIGLIST_DECLARED
277 wwarning("got signal %i (%s) - exiting...\n", sig, sys_siglist[sig]);
278 #else
279 wwarning("got signal %i - exiting...\n", sig);
280 #endif
282 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
284 WDelayedActionSet++;
287 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
290 /* Dummy signal handler */
291 static void
292 dummyHandler(int sig)
298 *----------------------------------------------------------------------
299 * handleSig--
300 * general signal handler. Exits the program gently.
301 *----------------------------------------------------------------------
303 static RETSIGTYPE
304 handleSig(int sig)
306 #ifdef SYS_SIGLIST_DECLARED
307 wfatal("got signal %i (%s)\n", sig, sys_siglist[sig]);
308 #else
309 wfatal("got signal %i\n", sig);
310 #endif
312 /* Setting the signal behaviour back to default and then reraising the
313 * signal is a cleaner way to make program exit and core dump than calling
314 * abort(), since it correctly returns from the signal handler and sets
315 * the flags accordingly. -Dan
317 if (sig==SIGSEGV || sig==SIGFPE || sig==SIGBUS || sig==SIGILL
318 || sig==SIGABRT) {
319 signal(sig, SIG_DFL);
320 kill(getpid(), sig);
321 return;
324 wAbort(0);
328 static RETSIGTYPE
329 buryChild(int foo)
331 pid_t pid;
332 int status;
333 int save_errno = errno;
334 sigset_t sigs;
336 sigfillset(&sigs);
337 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
338 sigprocmask(SIG_BLOCK, &sigs, NULL);
340 /* R.I.P. */
341 /* If 2 or more kids exit in a small time window, before this handler gets
342 * the chance to get invoked, the SIGCHLD signals will be merged and only
343 * one SIGCHLD signal will be sent to us. We use a while loop to get all
344 * exited child status because we can't count on the number of SIGCHLD
345 * signals to know exactly how many kids have exited. -Dan
347 while ((pid=waitpid(-1, &status, WNOHANG))>0 || (pid<0 && errno==EINTR)) {
348 NotifyDeadProcess(pid, WEXITSTATUS(status));
351 WDelayedActionSet++;
353 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
355 errno = save_errno;
359 static void
360 getOffendingModifiers()
362 int i;
363 XModifierKeymap *modmap;
364 KeyCode nlock, slock;
365 static int mask_table[8] = {
366 ShiftMask,LockMask,ControlMask,Mod1Mask,
367 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
370 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
371 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
374 * Find out the masks for the NumLock and ScrollLock modifiers,
375 * so that we can bind the grabs for when they are enabled too.
377 modmap = XGetModifierMapping(dpy);
379 if (modmap!=NULL && modmap->max_keypermod>0) {
380 for (i=0; i<8*modmap->max_keypermod; i++) {
381 if (modmap->modifiermap[i]==nlock && nlock!=0)
382 _NumLockMask = mask_table[i/modmap->max_keypermod];
383 else if (modmap->modifiermap[i]==slock && slock!=0)
384 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
388 if (modmap)
389 XFreeModifiermap(modmap);
394 #ifdef NUMLOCK_HACK
395 void
396 wHackedGrabKey(int keycode, unsigned int modifiers,
397 Window grab_window, Bool owner_events, int pointer_mode,
398 int keyboard_mode)
400 if (modifiers == AnyModifier)
401 return;
403 /* grab all combinations of the modifier with CapsLock, NumLock and
404 * ScrollLock. How much memory/CPU does such a monstrosity consume
405 * in the server?
407 if (_NumLockMask)
408 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
409 grab_window, owner_events, pointer_mode, keyboard_mode);
410 if (_ScrollLockMask)
411 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
412 grab_window, owner_events, pointer_mode, keyboard_mode);
413 if (_NumLockMask && _ScrollLockMask)
414 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
415 grab_window, owner_events, pointer_mode, keyboard_mode);
416 if (_NumLockMask)
417 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
418 grab_window, owner_events, pointer_mode, keyboard_mode);
419 if (_ScrollLockMask)
420 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
421 grab_window, owner_events, pointer_mode, keyboard_mode);
422 if (_NumLockMask && _ScrollLockMask)
423 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
424 grab_window, owner_events, pointer_mode, keyboard_mode);
425 /* phew, I guess that's all, right? */
427 #endif
429 void
430 wHackedGrabButton(unsigned int button, unsigned int modifiers,
431 Window grab_window, Bool owner_events,
432 unsigned int event_mask, int pointer_mode,
433 int keyboard_mode, Window confine_to, Cursor cursor)
435 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
436 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
438 if (modifiers==AnyModifier)
439 return;
441 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
442 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
444 #ifdef NUMLOCK_HACK
445 /* same as above, but for mouse buttons */
446 if (_NumLockMask)
447 XGrabButton(dpy, button, modifiers|_NumLockMask,
448 grab_window, owner_events, event_mask, pointer_mode,
449 keyboard_mode, confine_to, cursor);
450 if (_ScrollLockMask)
451 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
452 grab_window, owner_events, event_mask, pointer_mode,
453 keyboard_mode, confine_to, cursor);
454 if (_NumLockMask && _ScrollLockMask)
455 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
456 grab_window, owner_events, event_mask, pointer_mode,
457 keyboard_mode, confine_to, cursor);
458 if (_NumLockMask)
459 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
460 grab_window, owner_events, event_mask, pointer_mode,
461 keyboard_mode, confine_to, cursor);
462 if (_ScrollLockMask)
463 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
464 grab_window, owner_events, event_mask, pointer_mode,
465 keyboard_mode, confine_to, cursor);
466 if (_NumLockMask && _ScrollLockMask)
467 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
468 grab_window, owner_events, event_mask, pointer_mode,
469 keyboard_mode, confine_to, cursor);
470 #endif /* NUMLOCK_HACK */
473 #ifdef notused
474 void
475 wHackedUngrabButton(unsigned int button, unsigned int modifiers,
476 Window grab_window)
478 XUngrabButton(dpy, button, modifiers|_NumLockMask,
479 grab_window);
480 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
481 grab_window);
482 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
483 grab_window);
484 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
485 grab_window);
486 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
487 grab_window);
488 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
489 grab_window);
491 #endif
495 WScreen*
496 wScreenWithNumber(int i)
498 assert(i < wScreenCount);
500 return wScreen[i];
504 WScreen*
505 wScreenForRootWindow(Window window)
507 int i;
509 if (wScreenCount==1)
510 return wScreen[0];
513 * Since the number of heads will probably be small (normally 2),
514 * it should be faster to use this than a hash table, because
515 * of the overhead.
517 for (i=0; i<wScreenCount; i++) {
518 if (wScreen[i]->root_win == window) {
519 return wScreen[i];
523 return wScreenForWindow(window);
527 WScreen*
528 wScreenSearchForRootWindow(Window window)
530 int i;
532 if (wScreenCount==1)
533 return wScreen[0];
536 * Since the number of heads will probably be small (normally 2),
537 * it should be faster to use this than a hash table, because
538 * of the overhead.
540 for (i=0; i<wScreenCount; i++) {
541 if (wScreen[i]->root_win == window) {
542 return wScreen[i];
546 return wScreenForWindow(window);
550 WScreen*
551 wScreenForWindow(Window window)
553 XWindowAttributes attr;
555 if (wScreenCount==1)
556 return wScreen[0];
558 if (XGetWindowAttributes(dpy, window, &attr)) {
559 return wScreenForRootWindow(attr.root);
561 return NULL;
565 static char *atomNames[] = {
566 "WM_STATE",
567 "WM_CHANGE_STATE",
568 "WM_PROTOCOLS",
569 "WM_TAKE_FOCUS",
570 "WM_DELETE_WINDOW",
571 "WM_SAVE_YOURSELF",
572 "WM_CLIENT_LEADER",
573 "WM_COLORMAP_WINDOWS",
574 "WM_COLORMAP_NOTIFY",
576 "_WINDOWMAKER_MENU",
577 "_WINDOWMAKER_STATE",
578 "_WINDOWMAKER_WM_PROTOCOLS",
579 "_WINDOWMAKER_WM_FUNCTION",
580 "_WINDOWMAKER_NOTICEBOARD",
581 "_WINDOWMAKER_COMMAND",
582 "_WINDOWMAKER_ICON_SIZE",
583 "_WINDOWMAKER_ICON_TILE",
585 GNUSTEP_WM_ATTR_NAME,
586 GNUSTEP_WM_MINIATURIZE_WINDOW,
587 GNUSTEP_TITLEBAR_STATE
592 *----------------------------------------------------------
593 * StartUp--
594 * starts the window manager and setup global data.
595 * Called from main() at startup.
597 * Side effects:
598 * global data declared in main.c is initialized
599 *----------------------------------------------------------
601 void
602 StartUp(Bool defaultScreenOnly)
604 struct sigaction sig_action;
605 int j, max;
606 Atom atom[sizeof(atomNames)/sizeof(char*)];
609 * Ignore CapsLock in modifiers
611 ValidModMask = 0xff & ~LockMask;
613 getOffendingModifiers();
615 * Ignore NumLock and ScrollLock too
617 ValidModMask &= ~(_NumLockMask|_ScrollLockMask);
620 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
622 wWinContext = XUniqueContext();
623 wAppWinContext = XUniqueContext();
624 wStackContext = XUniqueContext();
625 wVEdgeContext = XUniqueContext();
627 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
629 #ifdef HAVE_XINTERNATOMS
630 XInternAtoms(dpy, atomNames, sizeof(atomNames)/sizeof(char*),
631 False, atom);
632 #else
635 int i;
636 for (i = 0; i < sizeof(atomNames)/sizeof(char*); i++) {
637 atom[i] = XInternAtom(dpy, atomNames[i], False);
640 #endif
642 _XA_WM_STATE = atom[0];
643 _XA_WM_CHANGE_STATE = atom[1];
644 _XA_WM_PROTOCOLS = atom[2];
645 _XA_WM_TAKE_FOCUS = atom[3];
646 _XA_WM_DELETE_WINDOW = atom[4];
647 _XA_WM_SAVE_YOURSELF = atom[5];
648 _XA_WM_CLIENT_LEADER = atom[6];
649 _XA_WM_COLORMAP_WINDOWS = atom[7];
650 _XA_WM_COLORMAP_NOTIFY = atom[8];
652 _XA_WINDOWMAKER_MENU = atom[9];
653 _XA_WINDOWMAKER_STATE = atom[10];
654 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
655 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
656 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
657 _XA_WINDOWMAKER_COMMAND = atom[14];
658 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
659 _XA_WINDOWMAKER_ICON_TILE = atom[16];
661 _XA_GNUSTEP_WM_ATTR = atom[17];
662 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
663 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
665 #ifdef XDND
666 wXDNDInitializeAtoms();
667 #endif
670 /* cursors */
671 wCursor[WCUR_NORMAL] = None; /* inherit from root */
672 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
673 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
674 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
675 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
676 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
677 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
678 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
679 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
680 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
681 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
682 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
683 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
684 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
685 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
687 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
688 GC gc = XCreateGC(dpy, cur, 0, NULL);
689 XColor black;
690 memset(&black, 0, sizeof(XColor));
691 XSetForeground(dpy, gc, 0);
692 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
693 XFreeGC(dpy, gc);
694 wCursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
695 XFreePixmap(dpy, cur);
699 /* emergency exit... */
700 sig_action.sa_handler = handleSig;
701 sigemptyset(&sig_action.sa_mask);
703 sig_action.sa_flags = SA_RESTART;
704 sigaction(SIGQUIT, &sig_action, NULL);
705 /* instead of catching these, we let the default handler abort the
706 * program. The new monitor process will take appropriate action
707 * when it detects the crash.
708 sigaction(SIGSEGV, &sig_action, NULL);
709 sigaction(SIGBUS, &sig_action, NULL);
710 sigaction(SIGFPE, &sig_action, NULL);
711 sigaction(SIGABRT, &sig_action, NULL);
714 sig_action.sa_handler = handleExitSig;
716 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
717 * immediately. -Dan */
718 sig_action.sa_flags = SA_RESTART;
719 sigaction(SIGTERM, &sig_action, NULL);
720 sigaction(SIGINT, &sig_action, NULL);
721 sigaction(SIGHUP, &sig_action, NULL);
722 sigaction(SIGUSR1, &sig_action, NULL);
723 sigaction(SIGUSR2, &sig_action, NULL);
725 /* ignore dead pipe */
726 /* Because POSIX mandates that only signal with handlers are reset
727 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
728 * to children. Hence the dummy handler.
729 * Philippe Troin <phil@fifi.org>
731 sig_action.sa_handler = &dummyHandler;
732 sig_action.sa_flags = SA_RESTART;
733 sigaction(SIGPIPE, &sig_action, NULL);
735 /* handle dead children */
736 sig_action.sa_handler = buryChild;
737 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
738 sigaction(SIGCHLD, &sig_action, NULL);
740 /* Now we unblock all signals, that may have been blocked by the parent
741 * who exec()-ed us. This can happen for example if Window Maker crashes
742 * and restarts itself or another window manager from the signal handler.
743 * In this case, the new proccess inherits the blocked signal mask and
744 * will no longer react to that signal, until unblocked.
745 * This is because the signal handler of the proccess who crashed (parent)
746 * didn't return, and the signal remained blocked. -Dan
748 sigfillset(&sig_action.sa_mask);
749 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
751 /* handle X shutdowns a such */
752 XSetIOErrorHandler(handleXIO);
754 /* set hook for out event dispatcher in WINGs event dispatcher */
755 WMHookEventHandler(DispatchEvent);
757 /* initialize defaults stuff */
758 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
759 if (!WDWindowMaker->dictionary) {
760 wwarning(_("could not read domain \"%s\" from defaults database"),
761 "WindowMaker");
764 /* read defaults that don't change until a restart and are
765 * screen independent */
766 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
768 /* check sanity of some values */
769 if (wPreferences.icon_size < 16) {
770 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
771 wPreferences.icon_size);
772 wPreferences.icon_size = 16;
775 /* init other domains */
776 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
777 if (!WDRootMenu->dictionary) {
778 wwarning(_("could not read domain \"%s\" from defaults database"),
779 "WMRootMenu");
781 wDefaultsMergeGlobalMenus(WDRootMenu);
783 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
784 if (!WDWindowAttributes->dictionary) {
785 wwarning(_("could not read domain \"%s\" from defaults database"),
786 "WMWindowAttributes");
789 XSetErrorHandler((XErrorHandler)catchXError);
791 #ifdef SHAPE
792 /* ignore j */
793 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
794 #endif
796 #ifdef KEEP_XKB_LOCK_STATUS
797 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
798 if(wPreferences.modelock && !wXkbSupported) {
799 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
800 wPreferences.modelock = 0;
802 #endif
804 if (defaultScreenOnly) {
805 max = 1;
806 } else {
807 max = ScreenCount(dpy);
809 wScreen = wmalloc(sizeof(WScreen*)*max);
811 wScreenCount = 0;
813 /* manage the screens */
814 for (j = 0; j < max; j++) {
815 if (defaultScreenOnly || max==1) {
816 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
817 if (!wScreen[wScreenCount]) {
818 wfatal(_("it seems that there is already a window manager running"));
819 Exit(1);
821 } else {
822 wScreen[wScreenCount] = wScreenInit(j);
823 if (!wScreen[wScreenCount]) {
824 wwarning(_("could not manage screen %i"), j);
825 continue;
828 wScreenCount++;
831 #ifndef LITE
832 InitializeSwitchMenu();
833 #endif
835 /* initialize/restore state for the screens */
836 for (j = 0; j < wScreenCount; j++) {
837 int lastDesktop;
839 lastDesktop= wNETWMGetCurrentDesktopFromHint(wScreen[j]);
841 wScreenRestoreState(wScreen[j]);
843 /* manage all windows that were already here before us */
844 if (!wPreferences.flags.nodock && wScreen[j]->dock)
845 wScreen[j]->last_dock = wScreen[j]->dock;
847 manageAllWindows(wScreen[j], wPreferences.flags.restarting==2);
849 /* restore saved menus */
850 wMenuRestoreState(wScreen[j]);
852 /* If we're not restarting, restore session */
853 if (wPreferences.flags.restarting==0 && !wPreferences.flags.norestore)
854 wSessionRestoreState(wScreen[j]);
856 if (!wPreferences.flags.noautolaunch) {
857 /* auto-launch apps */
858 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
859 wScreen[j]->last_dock = wScreen[j]->dock;
860 wDockDoAutoLaunch(wScreen[j]->dock, 0);
862 /* auto-launch apps in clip */
863 if (!wPreferences.flags.noclip) {
864 int i;
865 for(i=0; i<wScreen[j]->workspace_count; i++) {
866 if (wScreen[j]->workspaces[i]->clip) {
867 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
868 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
874 /* go to workspace where we were before restart */
875 if (lastDesktop >= 0) {
876 wWorkspaceForceChange(wScreen[j], lastDesktop);
877 } else {
878 wSessionRestoreLastWorkspace(wScreen[j]);
882 if (wScreenCount == 0) {
883 wfatal(_("could not manage any screen"));
884 Exit(1);
892 static Bool
893 windowInList(Window window, Window *list, int count)
895 for (; count>=0; count--) {
896 if (window == list[count])
897 return True;
899 return False;
903 *-----------------------------------------------------------------------
904 * manageAllWindows--
905 * Manages all windows in the screen.
907 * Notes:
908 * Called when the wm is being started.
909 * No events can be processed while the windows are being
910 * reparented/managed.
911 *-----------------------------------------------------------------------
913 static void
914 manageAllWindows(WScreen *scr, int crashRecovery)
916 Window root, parent;
917 Window *children;
918 unsigned int nchildren;
919 unsigned int i, j;
920 WWindow *wwin;
922 XGrabServer(dpy);
923 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
925 scr->flags.startup = 1;
927 /* first remove all icon windows */
928 for (i = 0; i < nchildren; i++) {
929 XWMHints *wmhints;
931 if (children[i]==None)
932 continue;
934 wmhints = XGetWMHints(dpy, children[i]);
935 if (wmhints && (wmhints->flags & IconWindowHint)) {
936 for (j = 0; j < nchildren; j++) {
937 if (children[j] == wmhints->icon_window) {
938 XFree(wmhints);
939 wmhints = NULL;
940 children[j] = None;
941 break;
945 if (wmhints) {
946 XFree(wmhints);
951 for (i = 0; i < nchildren; i++) {
952 if (children[i] == None)
953 continue;
955 wwin = wManageWindow(scr, children[i]);
956 if (wwin) {
957 /* apply states got from WSavedState */
958 /* shaded + minimized is not restored correctly */
959 if (wwin->flags.shaded) {
960 wwin->flags.shaded = 0;
961 wShadeWindow(wwin);
963 if (wwin->flags.miniaturized
964 && (wwin->transient_for == None
965 || wwin->transient_for == scr->root_win
966 || !windowInList(wwin->transient_for, children,
967 nchildren))) {
969 wwin->flags.skip_next_animation = 1;
970 wwin->flags.miniaturized = 0;
971 wIconifyWindow(wwin);
972 } else {
973 wClientSetState(wwin, NormalState, None);
975 if (crashRecovery) {
976 int border;
978 border = (!HAS_BORDER(wwin) ? 0 : FRAME_BORDER_WIDTH);
980 wWindowMove(wwin, wwin->frame_x - border,
981 wwin->frame_y - border -
982 (wwin->frame->titlebar ?
983 wwin->frame->titlebar->height : 0));
987 XUngrabServer(dpy);
989 /* hide apps */
990 wwin = scr->focused_window;
991 while (wwin) {
992 if (wwin->flags.hidden) {
993 WApplication *wapp = wApplicationOf(wwin->main_window);
995 if (wapp) {
996 wwin->flags.hidden = 0;
997 wHideApplication(wapp);
998 } else {
999 wwin->flags.hidden = 0;
1002 wwin = wwin->prev;
1005 XFree(children);
1006 scr->flags.startup = 0;
1007 scr->flags.startup2 = 1;
1009 while (XPending(dpy)) {
1010 XEvent ev;
1011 WMNextEvent(dpy, &ev);
1012 WMHandleEvent(&ev);
1014 wWorkspaceForceChange(scr, 0);
1015 if (!wPreferences.flags.noclip)
1016 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1017 scr->flags.startup2 = 0;