Fix gcc warnings
[wmaker-crm.git] / src / startup.c
blob9f75b4abeef9a5187712b27a77af19269ff6954e
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;
98 #ifdef SHAPE
99 extern Bool wShapeSupported;
100 extern int wShapeEventBase;
101 #endif
103 #ifdef KEEP_XKB_LOCK_STATUS
104 extern Bool wXkbSupported;
105 extern int wXkbEventBase;
106 #endif
108 /* contexts */
109 extern XContext wWinContext;
110 extern XContext wAppWinContext;
111 extern XContext wStackContext;
112 extern XContext wVEdgeContext;
114 /* atoms */
115 extern Atom _XA_WM_STATE;
116 extern Atom _XA_WM_CHANGE_STATE;
117 extern Atom _XA_WM_PROTOCOLS;
118 extern Atom _XA_WM_TAKE_FOCUS;
119 extern Atom _XA_WM_DELETE_WINDOW;
120 extern Atom _XA_WM_SAVE_YOURSELF;
121 extern Atom _XA_WM_CLIENT_LEADER;
122 extern Atom _XA_WM_COLORMAP_WINDOWS;
123 extern Atom _XA_WM_COLORMAP_NOTIFY;
125 extern Atom _XA_GNUSTEP_WM_ATTR;
127 extern Atom _XA_WINDOWMAKER_MENU;
128 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
129 extern Atom _XA_WINDOWMAKER_STATE;
130 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
131 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
132 extern Atom _XA_WINDOWMAKER_COMMAND;
133 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
134 extern Atom _XA_WINDOWMAKER_ICON_TILE;
136 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
137 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
140 /* cursors */
141 extern Cursor wCursor[WCUR_LAST];
143 /* special flags */
144 /*extern char WDelayedActionSet;*/
146 /***** Local *****/
148 static WScreen **wScreen = NULL;
151 static unsigned int _NumLockMask = 0;
152 static unsigned int _ScrollLockMask = 0;
156 static void manageAllWindows(WScreen *scr, int crashed);
158 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
161 static int
162 catchXError(Display *dpy, XErrorEvent *error)
164 char buffer[MAXLINE];
166 /* ignore some errors */
167 if (error->resourceid != None
168 && ((error->error_code == BadDrawable
169 && error->request_code == X_GetGeometry)
170 || (error->error_code == BadMatch
171 && (error->request_code == X_SetInputFocus))
172 || (error->error_code == BadWindow)
174 && (error->request_code == X_GetWindowAttributes
175 || error->request_code == X_SetInputFocus
176 || error->request_code == X_ChangeWindowAttributes
177 || error->request_code == X_GetProperty
178 || error->request_code == X_ChangeProperty
179 || error->request_code == X_QueryTree
180 || error->request_code == X_GrabButton
181 || error->request_code == X_UngrabButton
182 || error->request_code == X_SendEvent
183 || error->request_code == X_ConfigureWindow))
185 || (error->request_code == X_InstallColormap))) {
186 #ifndef DEBUG
188 return 0;
189 #else
190 printf("got X error %x %x %x\n", error->request_code,
191 error->error_code, (unsigned)error->resourceid);
192 return 0;
193 #endif
195 FormatXError(dpy, error, buffer, MAXLINE);
196 wwarning(_("internal X error: %s\n"), buffer);
197 return -1;
202 *----------------------------------------------------------------------
203 * handleXIO-
204 * Handle X shutdowns and other stuff.
205 *----------------------------------------------------------------------
207 static int
208 handleXIO(Display *xio_dpy)
210 dpy = NULL;
211 Exit(0);
212 return 0;
217 *----------------------------------------------------------------------
218 * delayedAction-
219 * Action to be executed after the signal() handler is exited.
220 * This was called every 500ms to 'clean up' signals. Not used now.
221 *----------------------------------------------------------------------
223 #ifdef notused
224 static void
225 delayedAction(void *cdata)
227 if (WDelayedActionSet == 0) {
228 return;
230 WDelayedActionSet--;
232 * Make the event dispatcher do whatever it needs to do,
233 * including handling zombie processes, restart and exit
234 * signals.
236 DispatchEvent(NULL);
238 #endif
241 *----------------------------------------------------------------------
242 * handleExitSig--
243 * User generated exit signal handler.
244 *----------------------------------------------------------------------
246 static RETSIGTYPE
247 handleExitSig(int sig)
249 sigset_t sigs;
251 sigfillset(&sigs);
252 sigprocmask(SIG_BLOCK, &sigs, NULL);
254 if (sig == SIGUSR1) {
255 #ifdef SYS_SIGLIST_DECLARED
256 wwarning("got signal %i (%s) - restarting\n", sig, sys_siglist[sig]);
257 #else
258 wwarning("got signal %i - restarting\n", sig);
259 #endif
261 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
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);
272 } else if (sig==SIGTERM || sig==SIGINT || sig==SIGHUP) {
273 #ifdef SYS_SIGLIST_DECLARED
274 wwarning("got signal %i (%s) - exiting...\n", sig, sys_siglist[sig]);
275 #else
276 wwarning("got signal %i - exiting...\n", sig);
277 #endif
279 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
283 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
284 DispatchEvent(NULL); /* Dispatch events imediately.*/
287 /* Dummy signal handler */
288 static void
289 dummyHandler(int sig)
295 *----------------------------------------------------------------------
296 * handleSig--
297 * general signal handler. Exits the program gently.
298 *----------------------------------------------------------------------
300 static RETSIGTYPE
301 handleSig(int sig)
303 #ifdef SYS_SIGLIST_DECLARED
304 wfatal("got signal %i (%s)\n", sig, sys_siglist[sig]);
305 #else
306 wfatal("got signal %i\n", sig);
307 #endif
309 /* Setting the signal behaviour back to default and then reraising the
310 * signal is a cleaner way to make program exit and core dump than calling
311 * abort(), since it correctly returns from the signal handler and sets
312 * the flags accordingly. -Dan
314 if (sig==SIGSEGV || sig==SIGFPE || sig==SIGBUS || sig==SIGILL
315 || sig==SIGABRT) {
316 signal(sig, SIG_DFL);
317 kill(getpid(), sig);
318 return;
321 wAbort(0);
325 static RETSIGTYPE
326 buryChild(int foo)
328 pid_t pid;
329 int status;
330 int save_errno = errno;
331 sigset_t sigs;
333 sigfillset(&sigs);
334 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
335 sigprocmask(SIG_BLOCK, &sigs, NULL);
337 /* R.I.P. */
338 /* If 2 or more kids exit in a small time window, before this handler gets
339 * the chance to get invoked, the SIGCHLD signals will be merged and only
340 * one SIGCHLD signal will be sent to us. We use a while loop to get all
341 * exited child status because we can't count on the number of SIGCHLD
342 * signals to know exactly how many kids have exited. -Dan
344 while ((pid=waitpid(-1, &status, WNOHANG))>0 || (pid<0 && errno==EINTR)) {
345 NotifyDeadProcess(pid, WEXITSTATUS(status));
348 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
350 errno = save_errno;
354 static void
355 getOffendingModifiers()
357 int i;
358 XModifierKeymap *modmap;
359 KeyCode nlock, slock;
360 static int mask_table[8] = {
361 ShiftMask,LockMask,ControlMask,Mod1Mask,
362 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
365 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
366 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
369 * Find out the masks for the NumLock and ScrollLock modifiers,
370 * so that we can bind the grabs for when they are enabled too.
372 modmap = XGetModifierMapping(dpy);
374 if (modmap!=NULL && modmap->max_keypermod>0) {
375 for (i=0; i<8*modmap->max_keypermod; i++) {
376 if (modmap->modifiermap[i]==nlock && nlock!=0)
377 _NumLockMask = mask_table[i/modmap->max_keypermod];
378 else if (modmap->modifiermap[i]==slock && slock!=0)
379 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
383 if (modmap)
384 XFreeModifiermap(modmap);
389 #ifdef NUMLOCK_HACK
390 void
391 wHackedGrabKey(int keycode, unsigned int modifiers,
392 Window grab_window, Bool owner_events, int pointer_mode,
393 int keyboard_mode)
395 if (modifiers == AnyModifier)
396 return;
398 /* grab all combinations of the modifier with CapsLock, NumLock and
399 * ScrollLock. How much memory/CPU does such a monstrosity consume
400 * in the server?
402 if (_NumLockMask)
403 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
404 grab_window, owner_events, pointer_mode, keyboard_mode);
405 if (_ScrollLockMask)
406 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
407 grab_window, owner_events, pointer_mode, keyboard_mode);
408 if (_NumLockMask && _ScrollLockMask)
409 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
410 grab_window, owner_events, pointer_mode, keyboard_mode);
411 if (_NumLockMask)
412 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
413 grab_window, owner_events, pointer_mode, keyboard_mode);
414 if (_ScrollLockMask)
415 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
416 grab_window, owner_events, pointer_mode, keyboard_mode);
417 if (_NumLockMask && _ScrollLockMask)
418 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
419 grab_window, owner_events, pointer_mode, keyboard_mode);
420 /* phew, I guess that's all, right? */
422 #endif
424 void
425 wHackedGrabButton(unsigned int button, unsigned int modifiers,
426 Window grab_window, Bool owner_events,
427 unsigned int event_mask, int pointer_mode,
428 int keyboard_mode, Window confine_to, Cursor cursor)
430 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
431 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
433 if (modifiers==AnyModifier)
434 return;
436 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
437 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
439 #ifdef NUMLOCK_HACK
440 /* same as above, but for mouse buttons */
441 if (_NumLockMask)
442 XGrabButton(dpy, button, modifiers|_NumLockMask,
443 grab_window, owner_events, event_mask, pointer_mode,
444 keyboard_mode, confine_to, cursor);
445 if (_ScrollLockMask)
446 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
447 grab_window, owner_events, event_mask, pointer_mode,
448 keyboard_mode, confine_to, cursor);
449 if (_NumLockMask && _ScrollLockMask)
450 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
451 grab_window, owner_events, event_mask, pointer_mode,
452 keyboard_mode, confine_to, cursor);
453 if (_NumLockMask)
454 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
455 grab_window, owner_events, event_mask, pointer_mode,
456 keyboard_mode, confine_to, cursor);
457 if (_ScrollLockMask)
458 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
459 grab_window, owner_events, event_mask, pointer_mode,
460 keyboard_mode, confine_to, cursor);
461 if (_NumLockMask && _ScrollLockMask)
462 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
463 grab_window, owner_events, event_mask, pointer_mode,
464 keyboard_mode, confine_to, cursor);
465 #endif /* NUMLOCK_HACK */
468 #ifdef notused
469 void
470 wHackedUngrabButton(unsigned int button, unsigned int modifiers,
471 Window grab_window)
473 XUngrabButton(dpy, button, modifiers|_NumLockMask,
474 grab_window);
475 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
476 grab_window);
477 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
478 grab_window);
479 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
480 grab_window);
481 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
482 grab_window);
483 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
484 grab_window);
486 #endif
490 WScreen*
491 wScreenWithNumber(int i)
493 assert(i < wScreenCount);
495 return wScreen[i];
499 WScreen*
500 wScreenForRootWindow(Window window)
502 int i;
504 if (wScreenCount==1)
505 return wScreen[0];
508 * Since the number of heads will probably be small (normally 2),
509 * it should be faster to use this than a hash table, because
510 * of the overhead.
512 for (i=0; i<wScreenCount; i++) {
513 if (wScreen[i]->root_win == window) {
514 return wScreen[i];
518 return wScreenForWindow(window);
522 WScreen*
523 wScreenSearchForRootWindow(Window window)
525 int i;
527 if (wScreenCount==1)
528 return wScreen[0];
531 * Since the number of heads will probably be small (normally 2),
532 * it should be faster to use this than a hash table, because
533 * of the overhead.
535 for (i=0; i<wScreenCount; i++) {
536 if (wScreen[i]->root_win == window) {
537 return wScreen[i];
541 return wScreenForWindow(window);
545 WScreen*
546 wScreenForWindow(Window window)
548 XWindowAttributes attr;
550 if (wScreenCount==1)
551 return wScreen[0];
553 if (XGetWindowAttributes(dpy, window, &attr)) {
554 return wScreenForRootWindow(attr.root);
556 return NULL;
560 static char *atomNames[] = {
561 "WM_STATE",
562 "WM_CHANGE_STATE",
563 "WM_PROTOCOLS",
564 "WM_TAKE_FOCUS",
565 "WM_DELETE_WINDOW",
566 "WM_SAVE_YOURSELF",
567 "WM_CLIENT_LEADER",
568 "WM_COLORMAP_WINDOWS",
569 "WM_COLORMAP_NOTIFY",
571 "_WINDOWMAKER_MENU",
572 "_WINDOWMAKER_STATE",
573 "_WINDOWMAKER_WM_PROTOCOLS",
574 "_WINDOWMAKER_WM_FUNCTION",
575 "_WINDOWMAKER_NOTICEBOARD",
576 "_WINDOWMAKER_COMMAND",
577 "_WINDOWMAKER_ICON_SIZE",
578 "_WINDOWMAKER_ICON_TILE",
580 GNUSTEP_WM_ATTR_NAME,
581 GNUSTEP_WM_MINIATURIZE_WINDOW,
582 GNUSTEP_TITLEBAR_STATE
587 *----------------------------------------------------------
588 * StartUp--
589 * starts the window manager and setup global data.
590 * Called from main() at startup.
592 * Side effects:
593 * global data declared in main.c is initialized
594 *----------------------------------------------------------
596 void
597 StartUp(Bool defaultScreenOnly)
599 struct sigaction sig_action;
600 int j, max;
601 Atom atom[sizeof(atomNames)/sizeof(char*)];
604 * Ignore CapsLock in modifiers
606 ValidModMask = 0xff & ~LockMask;
608 getOffendingModifiers();
610 * Ignore NumLock and ScrollLock too
612 ValidModMask &= ~(_NumLockMask|_ScrollLockMask);
615 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
617 wWinContext = XUniqueContext();
618 wAppWinContext = XUniqueContext();
619 wStackContext = XUniqueContext();
620 wVEdgeContext = XUniqueContext();
622 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
624 #ifdef HAVE_XINTERNATOMS
625 XInternAtoms(dpy, atomNames, sizeof(atomNames)/sizeof(char*),
626 False, atom);
627 #else
630 int i;
631 for (i = 0; i < sizeof(atomNames)/sizeof(char*); i++) {
632 atom[i] = XInternAtom(dpy, atomNames[i], False);
635 #endif
637 _XA_WM_STATE = atom[0];
638 _XA_WM_CHANGE_STATE = atom[1];
639 _XA_WM_PROTOCOLS = atom[2];
640 _XA_WM_TAKE_FOCUS = atom[3];
641 _XA_WM_DELETE_WINDOW = atom[4];
642 _XA_WM_SAVE_YOURSELF = atom[5];
643 _XA_WM_CLIENT_LEADER = atom[6];
644 _XA_WM_COLORMAP_WINDOWS = atom[7];
645 _XA_WM_COLORMAP_NOTIFY = atom[8];
647 _XA_WINDOWMAKER_MENU = atom[9];
648 _XA_WINDOWMAKER_STATE = atom[10];
649 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
650 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
651 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
652 _XA_WINDOWMAKER_COMMAND = atom[14];
653 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
654 _XA_WINDOWMAKER_ICON_TILE = atom[16];
656 _XA_GNUSTEP_WM_ATTR = atom[17];
657 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
658 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
660 #ifdef XDND
661 wXDNDInitializeAtoms();
662 #endif
665 /* cursors */
666 wCursor[WCUR_NORMAL] = None; /* inherit from root */
667 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
668 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
669 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
670 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
671 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
672 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
673 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
674 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
675 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
676 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
677 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
678 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
679 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
680 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
682 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
683 GC gc = XCreateGC(dpy, cur, 0, NULL);
684 XColor black;
685 memset(&black, 0, sizeof(XColor));
686 XSetForeground(dpy, gc, 0);
687 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
688 XFreeGC(dpy, gc);
689 wCursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
690 XFreePixmap(dpy, cur);
694 /* emergency exit... */
695 sig_action.sa_handler = handleSig;
696 sigemptyset(&sig_action.sa_mask);
698 sig_action.sa_flags = SA_RESTART;
699 sigaction(SIGQUIT, &sig_action, NULL);
700 /* instead of catching these, we let the default handler abort the
701 * program. The new monitor process will take appropriate action
702 * when it detects the crash.
703 sigaction(SIGSEGV, &sig_action, NULL);
704 sigaction(SIGBUS, &sig_action, NULL);
705 sigaction(SIGFPE, &sig_action, NULL);
706 sigaction(SIGABRT, &sig_action, NULL);
709 sig_action.sa_handler = handleExitSig;
711 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
712 * immediately. -Dan */
713 sig_action.sa_flags = SA_RESTART;
714 sigaction(SIGTERM, &sig_action, NULL);
715 sigaction(SIGINT, &sig_action, NULL);
716 sigaction(SIGHUP, &sig_action, NULL);
717 sigaction(SIGUSR1, &sig_action, NULL);
718 sigaction(SIGUSR2, &sig_action, NULL);
720 /* ignore dead pipe */
721 /* Because POSIX mandates that only signal with handlers are reset
722 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
723 * to children. Hence the dummy handler.
724 * Philippe Troin <phil@fifi.org>
726 sig_action.sa_handler = &dummyHandler;
727 sig_action.sa_flags = SA_RESTART;
728 sigaction(SIGPIPE, &sig_action, NULL);
730 /* handle dead children */
731 sig_action.sa_handler = buryChild;
732 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
733 sigaction(SIGCHLD, &sig_action, NULL);
735 /* Now we unblock all signals, that may have been blocked by the parent
736 * who exec()-ed us. This can happen for example if Window Maker crashes
737 * and restarts itself or another window manager from the signal handler.
738 * In this case, the new proccess inherits the blocked signal mask and
739 * will no longer react to that signal, until unblocked.
740 * This is because the signal handler of the proccess who crashed (parent)
741 * didn't return, and the signal remained blocked. -Dan
743 sigfillset(&sig_action.sa_mask);
744 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
746 /* handle X shutdowns a such */
747 XSetIOErrorHandler(handleXIO);
749 /* set hook for out event dispatcher in WINGs event dispatcher */
750 WMHookEventHandler(DispatchEvent);
752 /* initialize defaults stuff */
753 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
754 if (!WDWindowMaker->dictionary) {
755 wwarning(_("could not read domain \"%s\" from defaults database"),
756 "WindowMaker");
759 /* read defaults that don't change until a restart and are
760 * screen independent */
761 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
763 /* check sanity of some values */
764 if (wPreferences.icon_size < 16) {
765 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
766 wPreferences.icon_size);
767 wPreferences.icon_size = 16;
770 /* init other domains */
771 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
772 if (!WDRootMenu->dictionary) {
773 wwarning(_("could not read domain \"%s\" from defaults database"),
774 "WMRootMenu");
776 wDefaultsMergeGlobalMenus(WDRootMenu);
778 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
779 if (!WDWindowAttributes->dictionary) {
780 wwarning(_("could not read domain \"%s\" from defaults database"),
781 "WMWindowAttributes");
784 XSetErrorHandler((XErrorHandler)catchXError);
786 #ifdef SHAPE
787 /* ignore j */
788 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
789 #endif
791 #ifdef KEEP_XKB_LOCK_STATUS
792 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
793 if(wPreferences.modelock && !wXkbSupported) {
794 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
795 wPreferences.modelock = 0;
797 #endif
799 if (defaultScreenOnly) {
800 max = 1;
801 } else {
802 max = ScreenCount(dpy);
804 wScreen = wmalloc(sizeof(WScreen*)*max);
806 wScreenCount = 0;
808 /* manage the screens */
809 for (j = 0; j < max; j++) {
810 if (defaultScreenOnly || max==1) {
811 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
812 if (!wScreen[wScreenCount]) {
813 wfatal(_("it seems that there is already a window manager running"));
814 Exit(1);
816 } else {
817 wScreen[wScreenCount] = wScreenInit(j);
818 if (!wScreen[wScreenCount]) {
819 wwarning(_("could not manage screen %i"), j);
820 continue;
823 wScreenCount++;
826 #ifndef LITE
827 InitializeSwitchMenu();
828 #endif
830 /* initialize/restore state for the screens */
831 for (j = 0; j < wScreenCount; j++) {
832 int lastDesktop;
834 lastDesktop= wNETWMGetCurrentDesktopFromHint(wScreen[j]);
836 wScreenRestoreState(wScreen[j]);
838 /* manage all windows that were already here before us */
839 if (!wPreferences.flags.nodock && wScreen[j]->dock)
840 wScreen[j]->last_dock = wScreen[j]->dock;
842 manageAllWindows(wScreen[j], wPreferences.flags.restarting==2);
844 /* restore saved menus */
845 wMenuRestoreState(wScreen[j]);
847 /* If we're not restarting, restore session */
848 if (wPreferences.flags.restarting==0 && !wPreferences.flags.norestore)
849 wSessionRestoreState(wScreen[j]);
851 if (!wPreferences.flags.noautolaunch) {
852 /* auto-launch apps */
853 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
854 wScreen[j]->last_dock = wScreen[j]->dock;
855 wDockDoAutoLaunch(wScreen[j]->dock, 0);
857 /* auto-launch apps in clip */
858 if (!wPreferences.flags.noclip) {
859 int i;
860 for(i=0; i<wScreen[j]->workspace_count; i++) {
861 if (wScreen[j]->workspaces[i]->clip) {
862 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
863 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
869 /* go to workspace where we were before restart */
870 if (lastDesktop >= 0) {
871 wWorkspaceForceChange(wScreen[j], lastDesktop);
872 } else {
873 wSessionRestoreLastWorkspace(wScreen[j]);
877 if (wScreenCount == 0) {
878 wfatal(_("could not manage any screen"));
879 Exit(1);
887 static Bool
888 windowInList(Window window, Window *list, int count)
890 for (; count>=0; count--) {
891 if (window == list[count])
892 return True;
894 return False;
898 *-----------------------------------------------------------------------
899 * manageAllWindows--
900 * Manages all windows in the screen.
902 * Notes:
903 * Called when the wm is being started.
904 * No events can be processed while the windows are being
905 * reparented/managed.
906 *-----------------------------------------------------------------------
908 static void
909 manageAllWindows(WScreen *scr, int crashRecovery)
911 Window root, parent;
912 Window *children;
913 unsigned int nchildren;
914 unsigned int i, j;
915 WWindow *wwin;
917 XGrabServer(dpy);
918 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
920 scr->flags.startup = 1;
922 /* first remove all icon windows */
923 for (i = 0; i < nchildren; i++) {
924 XWMHints *wmhints;
926 if (children[i]==None)
927 continue;
929 wmhints = XGetWMHints(dpy, children[i]);
930 if (wmhints && (wmhints->flags & IconWindowHint)) {
931 for (j = 0; j < nchildren; j++) {
932 if (children[j] == wmhints->icon_window) {
933 XFree(wmhints);
934 wmhints = NULL;
935 children[j] = None;
936 break;
940 if (wmhints) {
941 XFree(wmhints);
946 for (i = 0; i < nchildren; i++) {
947 if (children[i] == None)
948 continue;
950 wwin = wManageWindow(scr, children[i]);
951 if (wwin) {
952 /* apply states got from WSavedState */
953 /* shaded + minimized is not restored correctly */
954 if (wwin->flags.shaded) {
955 wwin->flags.shaded = 0;
956 wShadeWindow(wwin);
958 if (wwin->flags.miniaturized
959 && (wwin->transient_for == None
960 || wwin->transient_for == scr->root_win
961 || !windowInList(wwin->transient_for, children,
962 nchildren))) {
964 wwin->flags.skip_next_animation = 1;
965 wwin->flags.miniaturized = 0;
966 wIconifyWindow(wwin);
967 } else {
968 wClientSetState(wwin, NormalState, None);
970 if (crashRecovery) {
971 int border;
973 border = (!HAS_BORDER(wwin) ? 0 : FRAME_BORDER_WIDTH);
975 wWindowMove(wwin, wwin->frame_x - border,
976 wwin->frame_y - border -
977 (wwin->frame->titlebar ?
978 wwin->frame->titlebar->height : 0));
982 XUngrabServer(dpy);
984 /* hide apps */
985 wwin = scr->focused_window;
986 while (wwin) {
987 if (wwin->flags.hidden) {
988 WApplication *wapp = wApplicationOf(wwin->main_window);
990 if (wapp) {
991 wwin->flags.hidden = 0;
992 wHideApplication(wapp);
993 } else {
994 wwin->flags.hidden = 0;
997 wwin = wwin->prev;
1000 XFree(children);
1001 scr->flags.startup = 0;
1002 scr->flags.startup2 = 1;
1004 while (XPending(dpy)) {
1005 XEvent ev;
1006 WMNextEvent(dpy, &ev);
1007 WMHandleEvent(&ev);
1009 wWorkspaceForceChange(scr, 0);
1010 if (!wPreferences.flags.noclip)
1011 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1012 scr->flags.startup2 = 0;