Remove LITE config option
[wmaker-crm.git] / src / startup.c
blob013845e8d1b675c6015d8d95a338529e27ca5a65
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 #if 0
70 #ifdef SYS_SIGLIST_DECLARED
71 extern const char *const sys_siglist[];
72 #endif
73 #endif
75 /* for SunOS */
76 #ifndef SA_RESTART
77 # define SA_RESTART 0
78 #endif
80 /* Just in case, for weirdo systems */
81 #ifndef SA_NODEFER
82 # define SA_NODEFER 0
83 #endif
85 /****** Global Variables ******/
87 extern WPreferences wPreferences;
89 extern WDDomain *WDWindowMaker;
90 extern WDDomain *WDRootMenu;
91 extern WDDomain *WDWindowAttributes;
93 extern WShortKey wKeyBindings[WKBD_LAST];
95 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;
138 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
140 /* cursors */
141 extern Cursor wCursor[WCUR_LAST];
143 /* special flags */
144 /*extern char WDelayedActionSet;*/
146 /***** Local *****/
148 static WScreen **wScreen = NULL;
150 static unsigned int _NumLockMask = 0;
151 static unsigned int _ScrollLockMask = 0;
153 static void manageAllWindows(WScreen * scr, int crashed);
155 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
157 static int catchXError(Display * dpy, XErrorEvent * error)
159 char buffer[MAXLINE];
161 /* ignore some errors */
162 if (error->resourceid != None
163 && ((error->error_code == BadDrawable && error->request_code == X_GetGeometry)
164 || (error->error_code == BadMatch && (error->request_code == X_SetInputFocus))
165 || (error->error_code == BadWindow)
167 && (error->request_code == X_GetWindowAttributes
168 || error->request_code == X_SetInputFocus
169 || error->request_code == X_ChangeWindowAttributes
170 || error->request_code == X_GetProperty
171 || error->request_code == X_ChangeProperty
172 || error->request_code == X_QueryTree
173 || error->request_code == X_GrabButton
174 || error->request_code == X_UngrabButton
175 || error->request_code == X_SendEvent
176 || error->request_code == X_ConfigureWindow))
178 || (error->request_code == X_InstallColormap))) {
179 #ifndef DEBUG
181 return 0;
182 #else
183 printf("got X error %x %x %x\n", error->request_code,
184 error->error_code, (unsigned)error->resourceid);
185 return 0;
186 #endif
188 FormatXError(dpy, error, buffer, MAXLINE);
189 wwarning(_("internal X error: %s\n"), buffer);
190 return -1;
194 *----------------------------------------------------------------------
195 * handleXIO-
196 * Handle X shutdowns and other stuff.
197 *----------------------------------------------------------------------
199 static int handleXIO(Display * xio_dpy)
201 dpy = NULL;
202 Exit(0);
203 return 0;
207 *----------------------------------------------------------------------
208 * delayedAction-
209 * Action to be executed after the signal() handler is exited.
210 * This was called every 500ms to 'clean up' signals. Not used now.
211 *----------------------------------------------------------------------
213 #ifdef notused
214 static void delayedAction(void *cdata)
216 if (WDelayedActionSet == 0) {
217 return;
219 WDelayedActionSet--;
221 * Make the event dispatcher do whatever it needs to do,
222 * including handling zombie processes, restart and exit
223 * signals.
225 DispatchEvent(NULL);
227 #endif
230 *----------------------------------------------------------------------
231 * handleExitSig--
232 * User generated exit signal handler.
233 *----------------------------------------------------------------------
235 static RETSIGTYPE handleExitSig(int sig)
237 sigset_t sigs;
239 sigfillset(&sigs);
240 sigprocmask(SIG_BLOCK, &sigs, NULL);
242 if (sig == SIGUSR1) {
243 #ifdef SYS_SIGLIST_DECLARED
244 wwarning("got signal %i (%s) - restarting\n", sig, sys_siglist[sig]);
245 #else
246 wwarning("got signal %i - restarting\n", sig);
247 #endif
249 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
251 } else if (sig == SIGUSR2) {
252 #ifdef SYS_SIGLIST_DECLARED
253 wwarning("got signal %i (%s) - rereading defaults\n", sig, sys_siglist[sig]);
254 #else
255 wwarning("got signal %i - rereading defaults\n", sig);
256 #endif
258 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
260 } else if (sig == SIGTERM || sig == SIGINT || sig == SIGHUP) {
261 #ifdef SYS_SIGLIST_DECLARED
262 wwarning("got signal %i (%s) - exiting...\n", sig, sys_siglist[sig]);
263 #else
264 wwarning("got signal %i - exiting...\n", sig);
265 #endif
267 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
271 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
272 DispatchEvent(NULL); /* Dispatch events imediately. */
275 /* Dummy signal handler */
276 static void dummyHandler(int sig)
281 *----------------------------------------------------------------------
282 * handleSig--
283 * general signal handler. Exits the program gently.
284 *----------------------------------------------------------------------
286 static RETSIGTYPE handleSig(int sig)
288 #ifdef SYS_SIGLIST_DECLARED
289 wfatal("got signal %i (%s)\n", sig, sys_siglist[sig]);
290 #else
291 wfatal("got signal %i\n", sig);
292 #endif
294 /* Setting the signal behaviour back to default and then reraising the
295 * signal is a cleaner way to make program exit and core dump than calling
296 * abort(), since it correctly returns from the signal handler and sets
297 * the flags accordingly. -Dan
299 if (sig == SIGSEGV || sig == SIGFPE || sig == SIGBUS || sig == SIGILL || sig == SIGABRT) {
300 signal(sig, SIG_DFL);
301 kill(getpid(), sig);
302 return;
305 wAbort(0);
308 static RETSIGTYPE buryChild(int foo)
310 pid_t pid;
311 int status;
312 int save_errno = errno;
313 sigset_t sigs;
315 sigfillset(&sigs);
316 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
317 sigprocmask(SIG_BLOCK, &sigs, NULL);
319 /* R.I.P. */
320 /* If 2 or more kids exit in a small time window, before this handler gets
321 * the chance to get invoked, the SIGCHLD signals will be merged and only
322 * one SIGCHLD signal will be sent to us. We use a while loop to get all
323 * exited child status because we can't count on the number of SIGCHLD
324 * signals to know exactly how many kids have exited. -Dan
326 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || (pid < 0 && errno == EINTR)) {
327 NotifyDeadProcess(pid, WEXITSTATUS(status));
330 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
332 errno = save_errno;
335 static void getOffendingModifiers()
337 int i;
338 XModifierKeymap *modmap;
339 KeyCode nlock, slock;
340 static int mask_table[8] = {
341 ShiftMask, LockMask, ControlMask, Mod1Mask,
342 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
345 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
346 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
349 * Find out the masks for the NumLock and ScrollLock modifiers,
350 * so that we can bind the grabs for when they are enabled too.
352 modmap = XGetModifierMapping(dpy);
354 if (modmap != NULL && modmap->max_keypermod > 0) {
355 for (i = 0; i < 8 * modmap->max_keypermod; i++) {
356 if (modmap->modifiermap[i] == nlock && nlock != 0)
357 _NumLockMask = mask_table[i / modmap->max_keypermod];
358 else if (modmap->modifiermap[i] == slock && slock != 0)
359 _ScrollLockMask = mask_table[i / modmap->max_keypermod];
363 if (modmap)
364 XFreeModifiermap(modmap);
367 #ifdef NUMLOCK_HACK
368 void
369 wHackedGrabKey(int keycode, unsigned int modifiers,
370 Window grab_window, Bool owner_events, int pointer_mode, int keyboard_mode)
372 if (modifiers == AnyModifier)
373 return;
375 /* grab all combinations of the modifier with CapsLock, NumLock and
376 * ScrollLock. How much memory/CPU does such a monstrosity consume
377 * in the server?
379 if (_NumLockMask)
380 XGrabKey(dpy, keycode, modifiers | _NumLockMask,
381 grab_window, owner_events, pointer_mode, keyboard_mode);
382 if (_ScrollLockMask)
383 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask,
384 grab_window, owner_events, pointer_mode, keyboard_mode);
385 if (_NumLockMask && _ScrollLockMask)
386 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask,
387 grab_window, owner_events, pointer_mode, keyboard_mode);
388 if (_NumLockMask)
389 XGrabKey(dpy, keycode, modifiers | _NumLockMask | LockMask,
390 grab_window, owner_events, pointer_mode, keyboard_mode);
391 if (_ScrollLockMask)
392 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask | LockMask,
393 grab_window, owner_events, pointer_mode, keyboard_mode);
394 if (_NumLockMask && _ScrollLockMask)
395 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask | LockMask,
396 grab_window, owner_events, pointer_mode, keyboard_mode);
397 /* phew, I guess that's all, right? */
399 #endif
401 void
402 wHackedGrabButton(unsigned int button, unsigned int modifiers,
403 Window grab_window, Bool owner_events,
404 unsigned int event_mask, int pointer_mode, int keyboard_mode, Window confine_to, Cursor cursor)
406 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
407 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
409 if (modifiers == AnyModifier)
410 return;
412 XGrabButton(dpy, button, modifiers | LockMask, grab_window, owner_events,
413 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
415 #ifdef NUMLOCK_HACK
416 /* same as above, but for mouse buttons */
417 if (_NumLockMask)
418 XGrabButton(dpy, button, modifiers | _NumLockMask,
419 grab_window, owner_events, event_mask, pointer_mode,
420 keyboard_mode, confine_to, cursor);
421 if (_ScrollLockMask)
422 XGrabButton(dpy, button, modifiers | _ScrollLockMask,
423 grab_window, owner_events, event_mask, pointer_mode,
424 keyboard_mode, confine_to, cursor);
425 if (_NumLockMask && _ScrollLockMask)
426 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask,
427 grab_window, owner_events, event_mask, pointer_mode,
428 keyboard_mode, confine_to, cursor);
429 if (_NumLockMask)
430 XGrabButton(dpy, button, modifiers | _NumLockMask | LockMask,
431 grab_window, owner_events, event_mask, pointer_mode,
432 keyboard_mode, confine_to, cursor);
433 if (_ScrollLockMask)
434 XGrabButton(dpy, button, modifiers | _ScrollLockMask | LockMask,
435 grab_window, owner_events, event_mask, pointer_mode,
436 keyboard_mode, confine_to, cursor);
437 if (_NumLockMask && _ScrollLockMask)
438 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask | LockMask,
439 grab_window, owner_events, event_mask, pointer_mode,
440 keyboard_mode, confine_to, cursor);
441 #endif /* NUMLOCK_HACK */
444 #ifdef notused
445 void wHackedUngrabButton(unsigned int button, unsigned int modifiers, Window grab_window)
447 XUngrabButton(dpy, button, modifiers | _NumLockMask, grab_window);
448 XUngrabButton(dpy, button, modifiers | _ScrollLockMask, grab_window);
449 XUngrabButton(dpy, button, modifiers | _NumLockMask | _ScrollLockMask, grab_window);
450 XUngrabButton(dpy, button, modifiers | _NumLockMask | LockMask, grab_window);
451 XUngrabButton(dpy, button, modifiers | _ScrollLockMask | LockMask, grab_window);
452 XUngrabButton(dpy, button, modifiers | _NumLockMask | _ScrollLockMask | LockMask, grab_window);
454 #endif
456 WScreen *wScreenWithNumber(int i)
458 assert(i < wScreenCount);
460 return wScreen[i];
463 WScreen *wScreenForRootWindow(Window window)
465 int i;
467 if (wScreenCount == 1)
468 return wScreen[0];
471 * Since the number of heads will probably be small (normally 2),
472 * it should be faster to use this than a hash table, because
473 * of the overhead.
475 for (i = 0; i < wScreenCount; i++) {
476 if (wScreen[i]->root_win == window) {
477 return wScreen[i];
481 return wScreenForWindow(window);
484 WScreen *wScreenSearchForRootWindow(Window window)
486 int i;
488 if (wScreenCount == 1)
489 return wScreen[0];
492 * Since the number of heads will probably be small (normally 2),
493 * it should be faster to use this than a hash table, because
494 * of the overhead.
496 for (i = 0; i < wScreenCount; i++) {
497 if (wScreen[i]->root_win == window) {
498 return wScreen[i];
502 return wScreenForWindow(window);
505 WScreen *wScreenForWindow(Window window)
507 XWindowAttributes attr;
509 if (wScreenCount == 1)
510 return wScreen[0];
512 if (XGetWindowAttributes(dpy, window, &attr)) {
513 return wScreenForRootWindow(attr.root);
515 return NULL;
518 static char *atomNames[] = {
519 "WM_STATE",
520 "WM_CHANGE_STATE",
521 "WM_PROTOCOLS",
522 "WM_TAKE_FOCUS",
523 "WM_DELETE_WINDOW",
524 "WM_SAVE_YOURSELF",
525 "WM_CLIENT_LEADER",
526 "WM_COLORMAP_WINDOWS",
527 "WM_COLORMAP_NOTIFY",
529 "_WINDOWMAKER_MENU",
530 "_WINDOWMAKER_STATE",
531 "_WINDOWMAKER_WM_PROTOCOLS",
532 "_WINDOWMAKER_WM_FUNCTION",
533 "_WINDOWMAKER_NOTICEBOARD",
534 "_WINDOWMAKER_COMMAND",
535 "_WINDOWMAKER_ICON_SIZE",
536 "_WINDOWMAKER_ICON_TILE",
538 GNUSTEP_WM_ATTR_NAME,
539 GNUSTEP_WM_MINIATURIZE_WINDOW,
540 GNUSTEP_TITLEBAR_STATE,
542 "WM_IGNORE_FOCUS_EVENTS"
546 *----------------------------------------------------------
547 * StartUp--
548 * starts the window manager and setup global data.
549 * Called from main() at startup.
551 * Side effects:
552 * global data declared in main.c is initialized
553 *----------------------------------------------------------
555 void StartUp(Bool defaultScreenOnly)
557 struct sigaction sig_action;
558 int j, max;
559 Atom atom[sizeof(atomNames) / sizeof(char *)];
562 * Ignore CapsLock in modifiers
564 ValidModMask = 0xff & ~LockMask;
566 getOffendingModifiers();
568 * Ignore NumLock and ScrollLock too
570 ValidModMask &= ~(_NumLockMask | _ScrollLockMask);
572 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
574 wWinContext = XUniqueContext();
575 wAppWinContext = XUniqueContext();
576 wStackContext = XUniqueContext();
577 wVEdgeContext = XUniqueContext();
579 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False); */
581 #ifdef HAVE_XINTERNATOMS
582 XInternAtoms(dpy, atomNames, sizeof(atomNames) / sizeof(char *), False, atom);
583 #else
586 int i;
587 for (i = 0; i < sizeof(atomNames) / sizeof(char *); i++) {
588 atom[i] = XInternAtom(dpy, atomNames[i], False);
591 #endif
593 _XA_WM_STATE = atom[0];
594 _XA_WM_CHANGE_STATE = atom[1];
595 _XA_WM_PROTOCOLS = atom[2];
596 _XA_WM_TAKE_FOCUS = atom[3];
597 _XA_WM_DELETE_WINDOW = atom[4];
598 _XA_WM_SAVE_YOURSELF = atom[5];
599 _XA_WM_CLIENT_LEADER = atom[6];
600 _XA_WM_COLORMAP_WINDOWS = atom[7];
601 _XA_WM_COLORMAP_NOTIFY = atom[8];
603 _XA_WINDOWMAKER_MENU = atom[9];
604 _XA_WINDOWMAKER_STATE = atom[10];
605 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
606 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
607 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
608 _XA_WINDOWMAKER_COMMAND = atom[14];
609 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
610 _XA_WINDOWMAKER_ICON_TILE = atom[16];
612 _XA_GNUSTEP_WM_ATTR = atom[17];
613 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
614 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
616 _XA_WM_IGNORE_FOCUS_EVENTS = atom[20];
618 #ifdef XDND
619 wXDNDInitializeAtoms();
620 #endif
622 /* cursors */
623 wCursor[WCUR_NORMAL] = None; /* inherit from root */
624 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
625 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
626 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
627 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
628 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
629 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
630 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
631 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
632 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
633 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
634 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
635 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
636 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name??? */
637 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
639 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
640 GC gc = XCreateGC(dpy, cur, 0, NULL);
641 XColor black;
642 memset(&black, 0, sizeof(XColor));
643 XSetForeground(dpy, gc, 0);
644 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
645 XFreeGC(dpy, gc);
646 wCursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
647 XFreePixmap(dpy, cur);
650 /* emergency exit... */
651 sig_action.sa_handler = handleSig;
652 sigemptyset(&sig_action.sa_mask);
654 sig_action.sa_flags = SA_RESTART;
655 sigaction(SIGQUIT, &sig_action, NULL);
656 /* instead of catching these, we let the default handler abort the
657 * program. The new monitor process will take appropriate action
658 * when it detects the crash.
659 sigaction(SIGSEGV, &sig_action, NULL);
660 sigaction(SIGBUS, &sig_action, NULL);
661 sigaction(SIGFPE, &sig_action, NULL);
662 sigaction(SIGABRT, &sig_action, NULL);
665 sig_action.sa_handler = handleExitSig;
667 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
668 * immediately. -Dan */
669 sig_action.sa_flags = SA_RESTART;
670 sigaction(SIGTERM, &sig_action, NULL);
671 sigaction(SIGINT, &sig_action, NULL);
672 sigaction(SIGHUP, &sig_action, NULL);
673 sigaction(SIGUSR1, &sig_action, NULL);
674 sigaction(SIGUSR2, &sig_action, NULL);
676 /* ignore dead pipe */
677 /* Because POSIX mandates that only signal with handlers are reset
678 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
679 * to children. Hence the dummy handler.
680 * Philippe Troin <phil@fifi.org>
682 sig_action.sa_handler = &dummyHandler;
683 sig_action.sa_flags = SA_RESTART;
684 sigaction(SIGPIPE, &sig_action, NULL);
686 /* handle dead children */
687 sig_action.sa_handler = buryChild;
688 sig_action.sa_flags = SA_NOCLDSTOP | SA_RESTART;
689 sigaction(SIGCHLD, &sig_action, NULL);
691 /* Now we unblock all signals, that may have been blocked by the parent
692 * who exec()-ed us. This can happen for example if Window Maker crashes
693 * and restarts itself or another window manager from the signal handler.
694 * In this case, the new proccess inherits the blocked signal mask and
695 * will no longer react to that signal, until unblocked.
696 * This is because the signal handler of the proccess who crashed (parent)
697 * didn't return, and the signal remained blocked. -Dan
699 sigfillset(&sig_action.sa_mask);
700 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
702 /* handle X shutdowns a such */
703 XSetIOErrorHandler(handleXIO);
705 /* set hook for out event dispatcher in WINGs event dispatcher */
706 WMHookEventHandler(DispatchEvent);
708 /* initialize defaults stuff */
709 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
710 if (!WDWindowMaker->dictionary) {
711 wwarning(_("could not read domain \"%s\" from defaults database"), "WindowMaker");
714 /* read defaults that don't change until a restart and are
715 * screen independent */
716 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
718 /* check sanity of some values */
719 if (wPreferences.icon_size < 16) {
720 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
721 wPreferences.icon_size);
722 wPreferences.icon_size = 16;
725 /* init other domains */
726 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
727 if (!WDRootMenu->dictionary) {
728 wwarning(_("could not read domain \"%s\" from defaults database"), "WMRootMenu");
730 wDefaultsMergeGlobalMenus(WDRootMenu);
732 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
733 if (!WDWindowAttributes->dictionary) {
734 wwarning(_("could not read domain \"%s\" from defaults database"), "WMWindowAttributes");
737 XSetErrorHandler((XErrorHandler) catchXError);
739 #ifdef SHAPE
740 /* ignore j */
741 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
742 #endif
744 #ifdef KEEP_XKB_LOCK_STATUS
745 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
746 if (wPreferences.modelock && !wXkbSupported) {
747 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
748 wPreferences.modelock = 0;
750 #endif
752 if (defaultScreenOnly) {
753 max = 1;
754 } else {
755 max = ScreenCount(dpy);
757 wScreen = wmalloc(sizeof(WScreen *) * max);
759 wScreenCount = 0;
761 /* manage the screens */
762 for (j = 0; j < max; j++) {
763 if (defaultScreenOnly || max == 1) {
764 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
765 if (!wScreen[wScreenCount]) {
766 wfatal(_("it seems that there is already a window manager running"));
767 Exit(1);
769 } else {
770 wScreen[wScreenCount] = wScreenInit(j);
771 if (!wScreen[wScreenCount]) {
772 wwarning(_("could not manage screen %i"), j);
773 continue;
776 wScreenCount++;
779 InitializeSwitchMenu();
781 /* initialize/restore state for the screens */
782 for (j = 0; j < wScreenCount; j++) {
783 int lastDesktop;
785 lastDesktop = wNETWMGetCurrentDesktopFromHint(wScreen[j]);
787 wScreenRestoreState(wScreen[j]);
789 /* manage all windows that were already here before us */
790 if (!wPreferences.flags.nodock && wScreen[j]->dock)
791 wScreen[j]->last_dock = wScreen[j]->dock;
793 manageAllWindows(wScreen[j], wPreferences.flags.restarting == 2);
795 /* restore saved menus */
796 wMenuRestoreState(wScreen[j]);
798 /* If we're not restarting, restore session */
799 if (wPreferences.flags.restarting == 0 && !wPreferences.flags.norestore)
800 wSessionRestoreState(wScreen[j]);
802 if (!wPreferences.flags.noautolaunch) {
803 /* auto-launch apps */
804 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
805 wScreen[j]->last_dock = wScreen[j]->dock;
806 wDockDoAutoLaunch(wScreen[j]->dock, 0);
808 /* auto-launch apps in clip */
809 if (!wPreferences.flags.noclip) {
810 int i;
811 for (i = 0; i < wScreen[j]->workspace_count; i++) {
812 if (wScreen[j]->workspaces[i]->clip) {
813 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
814 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
820 /* go to workspace where we were before restart */
821 if (lastDesktop >= 0) {
822 wWorkspaceForceChange(wScreen[j], lastDesktop);
823 } else {
824 wSessionRestoreLastWorkspace(wScreen[j]);
828 if (wScreenCount == 0) {
829 wfatal(_("could not manage any screen"));
830 Exit(1);
835 static Bool windowInList(Window window, Window * list, int count)
837 for (; count >= 0; count--) {
838 if (window == list[count])
839 return True;
841 return False;
845 *-----------------------------------------------------------------------
846 * manageAllWindows--
847 * Manages all windows in the screen.
849 * Notes:
850 * Called when the wm is being started.
851 * No events can be processed while the windows are being
852 * reparented/managed.
853 *-----------------------------------------------------------------------
855 static void manageAllWindows(WScreen * scr, int crashRecovery)
857 Window root, parent;
858 Window *children;
859 unsigned int nchildren;
860 unsigned int i, j;
861 WWindow *wwin;
863 XGrabServer(dpy);
864 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
866 scr->flags.startup = 1;
868 /* first remove all icon windows */
869 for (i = 0; i < nchildren; i++) {
870 XWMHints *wmhints;
872 if (children[i] == None)
873 continue;
875 wmhints = XGetWMHints(dpy, children[i]);
876 if (wmhints && (wmhints->flags & IconWindowHint)) {
877 for (j = 0; j < nchildren; j++) {
878 if (children[j] == wmhints->icon_window) {
879 XFree(wmhints);
880 wmhints = NULL;
881 children[j] = None;
882 break;
886 if (wmhints) {
887 XFree(wmhints);
891 for (i = 0; i < nchildren; i++) {
892 if (children[i] == None)
893 continue;
895 wwin = wManageWindow(scr, children[i]);
896 if (wwin) {
897 /* apply states got from WSavedState */
898 /* shaded + minimized is not restored correctly */
899 if (wwin->flags.shaded) {
900 wwin->flags.shaded = 0;
901 wShadeWindow(wwin);
903 if (wwin->flags.miniaturized
904 && (wwin->transient_for == None
905 || wwin->transient_for == scr->root_win
906 || !windowInList(wwin->transient_for, children, nchildren))) {
908 wwin->flags.skip_next_animation = 1;
909 wwin->flags.miniaturized = 0;
910 wIconifyWindow(wwin);
911 } else {
912 wClientSetState(wwin, NormalState, None);
914 if (crashRecovery) {
915 int border;
917 border = (!HAS_BORDER(wwin) ? 0 : FRAME_BORDER_WIDTH);
919 wWindowMove(wwin, wwin->frame_x - border,
920 wwin->frame_y - border -
921 (wwin->frame->titlebar ? wwin->frame->titlebar->height : 0));
925 XUngrabServer(dpy);
927 /* hide apps */
928 wwin = scr->focused_window;
929 while (wwin) {
930 if (wwin->flags.hidden) {
931 WApplication *wapp = wApplicationOf(wwin->main_window);
933 if (wapp) {
934 wwin->flags.hidden = 0;
935 wHideApplication(wapp);
936 } else {
937 wwin->flags.hidden = 0;
940 wwin = wwin->prev;
943 XFree(children);
944 scr->flags.startup = 0;
945 scr->flags.startup2 = 1;
947 while (XPending(dpy)) {
948 XEvent ev;
949 WMNextEvent(dpy, &ev);
950 WMHandleEvent(&ev);
952 wWorkspaceForceChange(scr, 0);
953 if (!wPreferences.flags.noclip)
954 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
955 scr->flags.startup2 = 0;