Some changes that try to address the zombie proccesses some people seem
[wmaker-crm.git] / src / startup.c
blob3d400a16bf667c5af7192e9c0627019592f84626
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 * Copyright (c) 1999 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 <signal.h>
29 #include <sys/wait.h>
30 #ifdef __FreeBSD__
31 #include <sys/signal.h>
32 #endif
34 #include <X11/Xlib.h>
35 #include <X11/Xutil.h>
36 #include <X11/Intrinsic.h>
37 #include <X11/cursorfont.h>
38 #include <X11/Xproto.h>
39 #include <X11/keysym.h>
40 #ifdef SHAPE
41 #include <X11/extensions/shape.h>
42 #endif
44 #include "WindowMaker.h"
45 #include "GNUstep.h"
46 #include "texture.h"
47 #include "screen.h"
48 #include "window.h"
49 #include "actions.h"
50 #include "client.h"
51 #include "funcs.h"
52 #include "dock.h"
53 #include "workspace.h"
54 #include "keybind.h"
55 #include "framewin.h"
56 #include "session.h"
57 #include "defaults.h"
58 #include "properties.h"
59 #include "dialog.h"
60 #ifdef XDND
61 #include "xdnd.h"
62 #endif
64 #include "xutil.h"
66 #ifdef KWM_HINTS
67 #include "kwm.h"
68 #endif
70 #if 0
71 #ifdef SYS_SIGLIST_DECLARED
72 extern const char * const sys_siglist[];
73 #endif
74 #endif
76 /* for SunOS */
77 #ifndef SA_RESTART
78 # define SA_RESTART 0
79 #endif
81 /* Just in case, for weirdo systems */
82 #ifndef SA_NODEFER
83 # define SA_NODEFER 0
84 #endif
86 /****** Global Variables ******/
88 extern WPreferences wPreferences;
90 extern WDDomain *WDWindowMaker;
91 extern WDDomain *WDRootMenu;
92 extern WDDomain *WDWindowAttributes;
94 extern WShortKey wKeyBindings[WKBD_LAST];
96 extern int wScreenCount;
99 #ifdef SHAPE
100 extern Bool wShapeSupported;
101 extern int wShapeEventBase;
102 #endif
104 #ifdef KEEP_XKB_LOCK_STATUS
105 extern Bool wXkbSupported;
106 extern int wXkbEventBase;
107 #endif
109 /* contexts */
110 extern XContext wWinContext;
111 extern XContext wAppWinContext;
112 extern XContext wStackContext;
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;
139 #ifdef OFFIX_DND
140 extern Atom _XA_DND_PROTOCOL;
141 extern Atom _XA_DND_SELECTION;
142 #endif
145 /* cursors */
146 extern Cursor wCursor[WCUR_LAST];
148 /* special flags */
149 extern char WDelayedActionSet;
151 /***** Local *****/
153 static WScreen **wScreen = NULL;
156 static unsigned int _NumLockMask = 0;
157 static unsigned int _ScrollLockMask = 0;
161 static void manageAllWindows();
163 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
166 static int
167 catchXError(Display *dpy, XErrorEvent *error)
169 char buffer[MAXLINE];
171 /* ignore some errors */
172 if (error->resourceid != None
173 && ((error->error_code == BadDrawable
174 && error->request_code == X_GetGeometry)
175 || (error->error_code == BadMatch
176 && (error->request_code == X_SetInputFocus))
177 || (error->error_code == BadWindow)
179 && (error->request_code == X_GetWindowAttributes
180 || error->request_code == X_SetInputFocus
181 || error->request_code == X_ChangeWindowAttributes
182 || error->request_code == X_GetProperty
183 || error->request_code == X_ChangeProperty
184 || error->request_code == X_QueryTree
185 || error->request_code == X_GrabButton
186 || error->request_code == X_UngrabButton
187 || error->request_code == X_SendEvent
188 || error->request_code == X_ConfigureWindow))
190 || (error->request_code == X_InstallColormap))) {
191 #ifndef DEBUG
193 return 0;
194 #else
195 printf("got X error %x %x %x\n", error->request_code,
196 error->error_code, (unsigned)error->resourceid);
197 return 0;
198 #endif
200 FormatXError(dpy, error, buffer, MAXLINE);
201 wwarning(_("internal X error: %s\n"), buffer);
202 return -1;
207 *----------------------------------------------------------------------
208 * handleXIO-
209 * Handle X shutdowns and other stuff.
210 *----------------------------------------------------------------------
212 static int
213 handleXIO(Display *xio_dpy)
215 dpy = NULL;
216 Exit(0);
217 return 0;
222 *----------------------------------------------------------------------
223 * delayedAction-
224 * Action to be executed after the signal() handler is exited.
225 *----------------------------------------------------------------------
227 static void
228 delayedAction(void *cdata)
230 WDelayedActionSet = 0;
232 * Make the event dispatcher do whatever it needs to do,
233 * including handling zombie processes, restart and exit
234 * signals.
236 DispatchEvent(NULL);
242 *----------------------------------------------------------------------
243 * handleSig--
244 * general signal handler. Exits the program gently.
245 *----------------------------------------------------------------------
247 static RETSIGTYPE
248 handleSig(int sig)
250 static int already_crashed = 0;
251 int dumpcore = 0;
252 #ifndef NO_EMERGENCY_AUTORESTART
253 int crashAction;
254 char *argv[2];
256 argv[1] = NULL;
257 #endif
260 * No functions that potentially do Xlib calls should be called from
261 * here. Xlib calls are not reentrant so the integrity of Xlib is
262 * not guaranteed if a Xlib call is made from a signal handler.
264 if (sig == SIGUSR1) {
265 #ifdef SYS_SIGLIST_DECLARED
266 wwarning(_("got signal %i (%s) - restarting\n"), sig, sys_siglist[sig]);
267 #else
268 wwarning(_("got signal %i - restarting\n"), sig);
269 #endif
271 WCHANGE_STATE(WSTATE_NEED_RESTART);
272 /* setup idle handler, so that this will be handled when
273 * the select() is returned becaused of the signal, even if
274 * there are no X events in the queue */
275 if (!WDelayedActionSet) {
276 WDelayedActionSet = 1;
277 WMAddIdleHandler(delayedAction, NULL);
279 return;
280 } else if (sig == SIGTERM || sig == SIGHUP) {
281 #ifdef SYS_SIGLIST_DECLARED
282 wwarning(_("got signal %i (%s) - exiting...\n"), sig, sys_siglist[sig]);
283 #else
284 wwarning(_("got signal %i - exiting...\n"), sig);
285 #endif
287 WCHANGE_STATE(WSTATE_NEED_EXIT);
289 if (!WDelayedActionSet) {
290 WDelayedActionSet = 1;
291 WMAddIdleHandler(delayedAction, NULL);
293 return;
296 #ifdef SYS_SIGLIST_DECLARED
297 wfatal(_("got signal %i (%s)\n"), sig, sys_siglist[sig]);
298 #else
299 wfatal(_("got signal %i\n"), sig);
300 #endif
302 /* Setting the signal behaviour back to default and then reraising the
303 * signal is a cleaner way to make program exit and core dump than calling
304 * abort(), since it correctly returns from the signal handler and sets
305 * the flags accordingly. -Dan
307 if (sig==SIGSEGV || sig==SIGFPE || sig==SIGBUS || sig==SIGILL
308 || sig==SIGABRT) {
309 if (already_crashed) {
310 wfatal(_("crashed while trying to do some post-crash cleanup. Aborting immediatelly."));
311 signal(sig, SIG_DFL);
312 kill(getpid(), sig);
313 return;
315 already_crashed = 1;
317 dumpcore = 1;
319 #ifndef NO_EMERGENCY_AUTORESTART
320 /* Close the X connection and open a new one. This is to avoid messing
321 * Xlib because we call to Xlib functions in a signal handler.
323 if (dpy)
324 XCloseDisplay(dpy);
325 dpy = XOpenDisplay("");
326 if (dpy) {
327 XGrabServer(dpy);
328 crashAction = wShowCrashingDialogPanel(sig);
329 XCloseDisplay(dpy);
330 dpy = NULL;
331 } else {
332 wsyserror(_("cannot open connection for crashing dialog panel. Aborting."));
333 crashAction = WMAbort;
336 if (crashAction == WMAbort) {
337 signal(sig, SIG_DFL);
338 kill(getpid(), sig);
339 return;
342 if (crashAction == WMRestart) {
343 /* we try to restart Window Maker */
344 wmessage(_("trying to restart Window Maker..."));
345 Restart(NULL, False);
346 /* fallback to alternate window manager then */
349 wmessage(_("trying to start alternate window manager..."));
351 Restart(FALLBACK_WINDOWMANAGER, False);
352 Restart("fvwm", False);
353 Restart("twm", False);
354 wfatal(_("failed to start alternate window manager. Aborting."));
355 #else
356 wfatal(_("a fatal error has occured, probably due to a bug. "
357 "Please fill the included BUGFORM and report it."));
358 #endif /* !NO_EMERGENCY_AUTORESTART */
360 signal(sig, SIG_DFL);
361 kill(getpid(), sig);
362 return;
366 wAbort(dumpcore);
370 static RETSIGTYPE
371 ignoreSig(int signal)
373 return;
377 static RETSIGTYPE
378 buryChild(int foo)
380 pid_t pid;
381 int status;
383 /* R.I.P. */
384 /* SIGCHLD's are blocked while we are in this signal handler, and if 2 or
385 * more kids exit while we handle the exit of one child in this handler
386 * (ie, about at the same time), we will miss the SIGCHLD signals the
387 * other kids will send and will be left with zombies. Using a while loop
388 * tries to avoid this (or at least minimize the probability), by calling
389 * waitpid until there are no more exited kids that respond.
390 * I think there is still a small probability that a a SIGCHLD signal can
391 * arrive after we finished the while loop, but before we return.
392 * Passing SA_NODEFER to the SIGCHLD handler doesn't seem to help at all,
393 * though tha man page sais that using this flag should allow receival of
394 * other SIGCHLD signals while processing one. Why is this? -Dan
396 * There seem to be SA_NOCLDWAIT, but I'm not sure its available on all
397 * platforms, and its possible that even if it will prevent zombie
398 * generation, it may also prevent us from receiving the exit status
399 * of our kids (I'm not sure, because the man page doesn't state this
400 * clearly). -Dan
402 * Maybe the best way would be to forget about SA_NODEFER and do it the
403 * old way: unblock SIGCHLD immediately after we entered this handler,
404 * then call waitpid in a while loop to get the exit status of all kids
405 * that exited while SIGCHLD was blocked and we missed the signal.
406 * This way there is no way we can miss any kid, because the while loop
407 * takes care of signals that were missed before we reenabled SIGCHLD
408 * and after enabling the signal, we we start getting them even if we
409 * are in the signal handler itself. Only need to check if it doesn't
410 * have any problem with reentrancy, but I don't think so. -Dan
412 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
413 NotifyDeadProcess(pid, WEXITSTATUS(status));
415 * Make sure that the kid will be buried even if there are
416 * no events in the X event queue
418 if (!WDelayedActionSet) {
419 WDelayedActionSet = 1;
420 WMAddIdleHandler(delayedAction, NULL);
426 static int
427 getWorkspaceState(Window root, WWorkspaceState **state)
429 Atom type_ret;
430 int fmt_ret;
431 unsigned long nitems_ret;
432 unsigned long bytes_after_ret;
433 CARD32 *data;
435 if (XGetWindowProperty(dpy, root, _XA_WINDOWMAKER_STATE, 0, 2,
436 True, _XA_WINDOWMAKER_STATE,
437 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
438 (unsigned char **)&data)!=Success || !data)
439 return 0;
441 *state = malloc(sizeof(WWorkspaceState));
442 if (*state) {
443 (*state)->flags = data[0];
444 (*state)->workspace = data[1];
446 XFree(data);
448 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
449 return 1;
450 else
451 return 0;
455 static void
456 getOffendingModifiers()
458 int i;
459 XModifierKeymap *modmap;
460 KeyCode nlock, slock;
461 static int mask_table[8] = {
462 ShiftMask,LockMask,ControlMask,Mod1Mask,
463 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
466 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
467 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
470 * Find out the masks for the NumLock and ScrollLock modifiers,
471 * so that we can bind the grabs for when they are enabled too.
473 modmap = XGetModifierMapping(dpy);
475 if (modmap!=NULL && modmap->max_keypermod>0) {
476 for (i=0; i<8*modmap->max_keypermod; i++) {
477 if (modmap->modifiermap[i]==nlock && nlock!=0)
478 _NumLockMask = mask_table[i/modmap->max_keypermod];
479 else if (modmap->modifiermap[i]==slock && slock!=0)
480 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
484 if (modmap)
485 XFreeModifiermap(modmap);
490 #ifdef NUMLOCK_HACK
491 void
492 wHackedGrabKey(int keycode, unsigned int modifiers,
493 Window grab_window, Bool owner_events, int pointer_mode,
494 int keyboard_mode)
496 if (modifiers == AnyModifier)
497 return;
499 /* grab all combinations of the modifier with CapsLock, NumLock and
500 * ScrollLock. How much memory/CPU does such a monstrosity consume
501 * in the server?
503 if (_NumLockMask)
504 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
505 grab_window, owner_events, pointer_mode, keyboard_mode);
506 if (_ScrollLockMask)
507 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
508 grab_window, owner_events, pointer_mode, keyboard_mode);
509 if (_NumLockMask && _ScrollLockMask)
510 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
511 grab_window, owner_events, pointer_mode, keyboard_mode);
512 if (_NumLockMask)
513 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
514 grab_window, owner_events, pointer_mode, keyboard_mode);
515 if (_ScrollLockMask)
516 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
517 grab_window, owner_events, pointer_mode, keyboard_mode);
518 if (_NumLockMask && _ScrollLockMask)
519 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
520 grab_window, owner_events, pointer_mode, keyboard_mode);
521 /* phew, I guess that's all, right? */
523 #endif
525 void
526 wHackedGrabButton(unsigned int button, unsigned int modifiers,
527 Window grab_window, Bool owner_events,
528 unsigned int event_mask, int pointer_mode,
529 int keyboard_mode, Window confine_to, Cursor cursor)
531 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
532 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
534 if (modifiers==AnyModifier)
535 return;
537 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
538 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
540 #ifdef NUMLOCK_HACK
541 /* same as above, but for mouse buttons */
542 if (_NumLockMask)
543 XGrabButton(dpy, button, modifiers|_NumLockMask,
544 grab_window, owner_events, event_mask, pointer_mode,
545 keyboard_mode, confine_to, cursor);
546 if (_ScrollLockMask)
547 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
548 grab_window, owner_events, event_mask, pointer_mode,
549 keyboard_mode, confine_to, cursor);
550 if (_NumLockMask && _ScrollLockMask)
551 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
552 grab_window, owner_events, event_mask, pointer_mode,
553 keyboard_mode, confine_to, cursor);
554 if (_NumLockMask)
555 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
556 grab_window, owner_events, event_mask, pointer_mode,
557 keyboard_mode, confine_to, cursor);
558 if (_ScrollLockMask)
559 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
560 grab_window, owner_events, event_mask, pointer_mode,
561 keyboard_mode, confine_to, cursor);
562 if (_NumLockMask && _ScrollLockMask)
563 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
564 grab_window, owner_events, event_mask, pointer_mode,
565 keyboard_mode, confine_to, cursor);
566 #endif /* NUMLOCK_HACK */
569 #ifdef notused
570 void
571 wHackedUngrabButton(unsigned int button, unsigned int modifiers,
572 Window grab_window)
574 XUngrabButton(dpy, button, modifiers|_NumLockMask,
575 grab_window);
576 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
577 grab_window);
578 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
579 grab_window);
580 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
581 grab_window);
582 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
583 grab_window);
584 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
585 grab_window);
587 #endif
591 WScreen*
592 wScreenWithNumber(int i)
594 assert(i < wScreenCount);
596 return wScreen[i];
600 WScreen*
601 wScreenForRootWindow(Window window)
603 int i;
605 if (wScreenCount==1)
606 return wScreen[0];
609 * Since the number of heads will probably be small (normally 2),
610 * it should be faster to use this than a hash table, because
611 * of the overhead.
613 for (i=0; i<wScreenCount; i++) {
614 if (wScreen[i]->root_win == window) {
615 return wScreen[i];
619 assert("bad_root_window" && 0);
621 return NULL;
625 WScreen*
626 wScreenSearchForRootWindow(Window window)
628 int i;
630 if (wScreenCount==1)
631 return wScreen[0];
634 * Since the number of heads will probably be small (normally 2),
635 * it should be faster to use this than a hash table, because
636 * of the overhead.
638 for (i=0; i<wScreenCount; i++) {
639 if (wScreen[i]->root_win == window) {
640 return wScreen[i];
644 return NULL;
648 WScreen*
649 wScreenForWindow(Window window)
651 XWindowAttributes attr;
653 if (wScreenCount==1)
654 return wScreen[0];
656 if (XGetWindowAttributes(dpy, window, &attr)) {
657 return wScreenForRootWindow(attr.root);
659 return NULL;
663 static char *atomNames[] = {
664 "WM_STATE",
665 "WM_CHANGE_STATE",
666 "WM_PROTOCOLS",
667 "WM_TAKE_FOCUS",
668 "WM_DELETE_WINDOW",
669 "WM_SAVE_YOURSELF",
670 "WM_CLIENT_LEADER",
671 "WM_COLORMAP_WINDOWS",
672 "WM_COLORMAP_NOTIFY",
673 GNUSTEP_WM_ATTR_NAME,
674 "_WINDOWMAKER_MENU",
675 "_WINDOWMAKER_STATE",
676 "_WINDOWMAKER_WM_PROTOCOLS",
677 GNUSTEP_WM_MINIATURIZE_WINDOW,
678 "_WINDOWMAKER_WM_FUNCTION",
679 "_WINDOWMAKER_NOTICEBOARD",
680 "_WINDOWMAKER_COMMAND",
681 "_WINDOWMAKER_ICON_SIZE",
682 "_WINDOWMAKER_ICON_TILE",
683 GNUSTEP_TITLEBAR_STATE
688 *----------------------------------------------------------
689 * StartUp--
690 * starts the window manager and setup global data.
691 * Called from main() at startup.
693 * Side effects:
694 * global data declared in main.c is initialized
695 *----------------------------------------------------------
697 void
698 StartUp(Bool defaultScreenOnly)
700 WWorkspaceState *ws_state;
701 struct sigaction sig_action;
702 int j, max;
703 Atom atom[sizeof(atomNames)/sizeof(char*)];
706 * Ignore CapsLock in modifiers
708 ValidModMask = 0xff & ~LockMask;
710 getOffendingModifiers();
712 * Ignore NumLock and ScrollLock too
714 ValidModMask &= ~(_NumLockMask|_ScrollLockMask);
717 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
719 wWinContext = XUniqueContext();
720 wAppWinContext = XUniqueContext();
721 wStackContext = XUniqueContext();
723 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
725 #ifdef HAVE_XINTERNATOMS
726 XInternAtoms(dpy, atomNames, sizeof(atomNames)/sizeof(char*),
727 False, atom);
728 #else
731 int i;
732 for (i = 0; i < sizeof(atomNames)/sizeof(char*); i++) {
733 atom[i] = XInternAtom(dpy, atomNames[i], False);
736 #endif
738 _XA_WM_STATE = atom[0];
739 _XA_WM_CHANGE_STATE = atom[1];
740 _XA_WM_PROTOCOLS = atom[2];
741 _XA_WM_TAKE_FOCUS = atom[3];
742 _XA_WM_DELETE_WINDOW = atom[4];
743 _XA_WM_SAVE_YOURSELF = atom[5];
744 _XA_WM_CLIENT_LEADER = atom[6];
745 _XA_WM_COLORMAP_WINDOWS = atom[7];
746 _XA_WM_COLORMAP_NOTIFY = atom[8];
748 _XA_GNUSTEP_WM_ATTR = atom[9];
750 _XA_WINDOWMAKER_MENU = atom[10];
751 _XA_WINDOWMAKER_STATE = atom[11];
753 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[12];
755 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[13];
757 _XA_WINDOWMAKER_WM_FUNCTION = atom[14];
759 _XA_WINDOWMAKER_NOTICEBOARD = atom[15];
761 _XA_WINDOWMAKER_COMMAND = atom[16];
763 _XA_WINDOWMAKER_ICON_SIZE = atom[17];
764 _XA_WINDOWMAKER_ICON_TILE = atom[18];
766 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
768 #ifdef OFFIX_DND
769 _XA_DND_SELECTION = XInternAtom(dpy, "DndSelection", False);
770 _XA_DND_PROTOCOL = XInternAtom(dpy, "DndProtocol", False);
771 #endif
772 #ifdef XDND
773 wXDNDInitializeAtoms();
774 #endif
777 /* cursors */
778 #ifdef DEFINABLE_CURSOR
779 wCursor[WCUR_NORMAL] = None;
780 #else
781 wCursor[WCUR_NORMAL] = XCreateFontCursor(dpy, XC_left_ptr);
782 #endif
783 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
784 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
785 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
786 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
787 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
788 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
789 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
790 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
791 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
792 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
793 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
794 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
795 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
796 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
798 /* emergency exit... */
799 sig_action.sa_handler = handleSig;
800 sigemptyset(&sig_action.sa_mask);
802 /* Here we don't care about SA_RESTART since these signals will close
803 * wmaker anyway.
804 * -Dan */
805 sig_action.sa_flags = 0;
806 sigaction(SIGINT, &sig_action, NULL);
807 sigaction(SIGTERM, &sig_action, NULL);
808 sigaction(SIGHUP, &sig_action, NULL);
809 sigaction(SIGQUIT, &sig_action, NULL);
810 sigaction(SIGSEGV, &sig_action, NULL);
811 sigaction(SIGBUS, &sig_action, NULL);
812 sigaction(SIGFPE, &sig_action, NULL);
813 sigaction(SIGABRT, &sig_action, NULL);
815 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
816 * immediately.
817 * -Dan */
818 sig_action.sa_flags = SA_RESTART;
819 sigaction(SIGUSR1, &sig_action, NULL);
821 /* ignore dead pipe */
822 sig_action.sa_handler = ignoreSig;
823 sig_action.sa_flags = SA_RESTART;
824 sigaction(SIGPIPE, &sig_action, NULL);
826 /* handle dead children */
827 /* Using SA_NODEFER doesn't seem to have any effect. Why? -Dan */
828 sig_action.sa_handler = buryChild;
829 sig_action.sa_flags = SA_NODEFER|SA_NOCLDSTOP|SA_RESTART;
830 sigaction(SIGCHLD, &sig_action, NULL);
832 /* Now we unblock all signals, that may have been blocked by the parent
833 * who exec()-ed us. This can happen for example if Window Maker crashes
834 * and restarts itself or another window manager from the signal handler.
835 * In this case, the new proccess inherits the blocked signal mask and
836 * will no longer react to that signal, until unblocked.
837 * This is because the signal handler of the proccess who crashed (parent)
838 * didn't return, and the signal remained blocked. -Dan
840 sigfillset(&sig_action.sa_mask);
841 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
843 /* handle X shutdowns a such */
844 XSetIOErrorHandler(handleXIO);
846 /* set hook for out event dispatcher in WINGs event dispatcher */
847 WMHookEventHandler(DispatchEvent);
849 /* initialize defaults stuff */
850 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
851 if (!WDWindowMaker->dictionary) {
852 wwarning(_("could not read domain \"%s\" from defaults database"),
853 "WindowMaker");
856 /* read defaults that don't change until a restart and are
857 * screen independent */
858 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
860 /* check sanity of some values */
861 if (wPreferences.icon_size < 16) {
862 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
863 wPreferences.icon_size);
864 wPreferences.icon_size = 16;
867 /* init other domains */
868 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
869 if (!WDRootMenu->dictionary) {
870 wwarning(_("could not read domain \"%s\" from defaults database"),
871 "WMRootMenu");
874 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
875 if (!WDWindowAttributes->dictionary) {
876 wwarning(_("could not read domain \"%s\" from defaults database"),
877 "WMWindowAttributes");
880 XSetErrorHandler((XErrorHandler)catchXError);
882 #ifdef SHAPE
883 /* ignore j */
884 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
885 #endif
887 #ifdef KEEP_XKB_LOCK_STATUS
888 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
889 if(wPreferences.modelock && !wXkbSupported) {
890 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
891 wPreferences.modelock = 0;
893 #endif
895 if (defaultScreenOnly) {
896 max = 1;
897 } else {
898 max = ScreenCount(dpy);
900 wScreen = wmalloc(sizeof(WScreen*)*max);
902 wScreenCount = 0;
904 /* manage the screens */
905 for (j = 0; j < max; j++) {
906 if (defaultScreenOnly || max==1) {
907 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
908 if (!wScreen[wScreenCount]) {
909 wfatal(_("it seems that there is already a window manager running"));
910 Exit(1);
912 } else {
913 wScreen[wScreenCount] = wScreenInit(j);
914 if (!wScreen[wScreenCount]) {
915 wwarning(_("could not manage screen %i"), j);
916 continue;
919 wScreenCount++;
922 /* initialize/restore state for the screens */
923 for (j = 0; j < wScreenCount; j++) {
924 /* restore workspace state */
925 if (!getWorkspaceState(wScreen[j]->root_win, &ws_state)) {
926 ws_state = NULL;
929 wScreenRestoreState(wScreen[j]);
931 /* manage all windows that were already here before us */
932 if (!wPreferences.flags.nodock && wScreen[j]->dock)
933 wScreen[j]->last_dock = wScreen[j]->dock;
935 manageAllWindows(wScreen[j]);
937 /* restore saved menus */
938 wMenuRestoreState(wScreen[j]);
940 /* If we're not restarting restore session */
941 if (ws_state == NULL && !wPreferences.flags.norestore)
942 wSessionRestoreState(wScreen[j]);
944 if (!wPreferences.flags.noautolaunch) {
945 /* auto-launch apps */
946 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
947 wScreen[j]->last_dock = wScreen[j]->dock;
948 wDockDoAutoLaunch(wScreen[j]->dock, 0);
950 /* auto-launch apps in clip */
951 if (!wPreferences.flags.noclip) {
952 int i;
953 for(i=0; i<wScreen[j]->workspace_count; i++) {
954 if (wScreen[j]->workspaces[i]->clip) {
955 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
956 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
962 /* go to workspace where we were before restart */
963 if (ws_state) {
964 wWorkspaceForceChange(wScreen[j], ws_state->workspace);
965 wfree(ws_state);
966 } else {
967 wSessionRestoreLastWorkspace(wScreen[j]);
970 #ifdef KWM_HINTS
971 wKWMSetInitializedHint(wScreen[j]);
972 #endif
975 if (wScreenCount == 0) {
976 wfatal(_("could not manage any screen"));
977 Exit(1);
980 if (!wPreferences.flags.noupdates) {
981 /* setup defaults file polling */
982 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
989 static Bool
990 windowInList(Window window, Window *list, int count)
992 for (; count>=0; count--) {
993 if (window == list[count])
994 return True;
996 return False;
1000 *-----------------------------------------------------------------------
1001 * manageAllWindows--
1002 * Manages all windows in the screen.
1004 * Notes:
1005 * Called when the wm is being started.
1006 * No events can be processed while the windows are being
1007 * reparented/managed.
1008 *-----------------------------------------------------------------------
1010 static void
1011 manageAllWindows(WScreen *scr)
1013 Window root, parent;
1014 Window *children;
1015 unsigned int nchildren;
1016 unsigned int i, j;
1017 WWindow *wwin;
1019 XGrabServer(dpy);
1020 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
1022 scr->flags.startup = 1;
1024 /* first remove all icon windows */
1025 for (i = 0; i < nchildren; i++) {
1026 XWMHints *wmhints;
1028 if (children[i]==None)
1029 continue;
1031 wmhints = XGetWMHints(dpy, children[i]);
1032 if (wmhints && (wmhints->flags & IconWindowHint)) {
1033 for (j = 0; j < nchildren; j++) {
1034 if (children[j] == wmhints->icon_window) {
1035 XFree(wmhints);
1036 wmhints = NULL;
1037 children[j] = None;
1038 break;
1042 if (wmhints) {
1043 XFree(wmhints);
1048 for (i = 0; i < nchildren; i++) {
1049 if (children[i] == None)
1050 continue;
1052 #ifdef KWM_HINTS
1053 wKWMCheckModule(scr, children[i]);
1054 #endif
1055 wwin = wManageWindow(scr, children[i]);
1056 if (wwin) {
1057 /* apply states got from WSavedState */
1058 /* shaded + minimized is not restored correctly */
1059 if (wwin->flags.shaded) {
1060 wwin->flags.shaded = 0;
1061 wShadeWindow(wwin);
1063 if (wwin->flags.miniaturized
1064 && (wwin->transient_for == None
1065 || wwin->transient_for == scr->root_win
1066 || !windowInList(wwin->transient_for, children,
1067 nchildren))) {
1069 wwin->flags.skip_next_animation = 1;
1070 wwin->flags.miniaturized = 0;
1071 wIconifyWindow(wwin);
1072 } else {
1073 wClientSetState(wwin, NormalState, None);
1077 XUngrabServer(dpy);
1079 /* hide apps */
1080 wwin = scr->focused_window;
1081 while (wwin) {
1082 if (wwin->flags.hidden) {
1083 WApplication *wapp = wApplicationOf(wwin->main_window);
1085 if (wapp) {
1086 wwin->flags.hidden = 0;
1087 wHideApplication(wapp);
1088 } else {
1089 wwin->flags.hidden = 0;
1092 wwin = wwin->prev;
1095 XFree(children);
1096 scr->flags.startup = 0;
1097 scr->flags.startup2 = 1;
1099 while (XPending(dpy)) {
1100 XEvent ev;
1101 WMNextEvent(dpy, &ev);
1102 WMHandleEvent(&ev);
1104 wWorkspaceForceChange(scr, 0);
1105 if (!wPreferences.flags.noclip)
1106 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1107 scr->flags.startup2 = 0;