fixed some signal handling bugs
[wmaker-crm.git] / src / startup.c
blob224ae4a644d437333d5dd18cb154248900e495f7
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 <errno.h>
29 #include <signal.h>
30 #include <sys/wait.h>
31 #ifdef __FreeBSD__
32 #include <sys/signal.h>
33 #endif
35 #include <X11/Xlib.h>
36 #include <X11/Xutil.h>
37 #include <X11/Intrinsic.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 #ifdef XDND
62 #include "xdnd.h"
63 #endif
65 #include "xutil.h"
67 #ifdef KWM_HINTS
68 #include "kwm.h"
69 #endif
71 #if 0
72 #ifdef SYS_SIGLIST_DECLARED
73 extern const char * const sys_siglist[];
74 #endif
75 #endif
77 /* for SunOS */
78 #ifndef SA_RESTART
79 # define SA_RESTART 0
80 #endif
82 /* Just in case, for weirdo systems */
83 #ifndef SA_NODEFER
84 # define SA_NODEFER 0
85 #endif
87 /****** Global Variables ******/
89 extern WPreferences wPreferences;
91 extern WDDomain *WDWindowMaker;
92 extern WDDomain *WDRootMenu;
93 extern WDDomain *WDWindowAttributes;
95 extern WShortKey wKeyBindings[WKBD_LAST];
97 extern int wScreenCount;
100 #ifdef SHAPE
101 extern Bool wShapeSupported;
102 extern int wShapeEventBase;
103 #endif
105 #ifdef KEEP_XKB_LOCK_STATUS
106 extern Bool wXkbSupported;
107 extern int wXkbEventBase;
108 #endif
110 /* contexts */
111 extern XContext wWinContext;
112 extern XContext wAppWinContext;
113 extern XContext wStackContext;
115 /* atoms */
116 extern Atom _XA_WM_STATE;
117 extern Atom _XA_WM_CHANGE_STATE;
118 extern Atom _XA_WM_PROTOCOLS;
119 extern Atom _XA_WM_TAKE_FOCUS;
120 extern Atom _XA_WM_DELETE_WINDOW;
121 extern Atom _XA_WM_SAVE_YOURSELF;
122 extern Atom _XA_WM_CLIENT_LEADER;
123 extern Atom _XA_WM_COLORMAP_WINDOWS;
124 extern Atom _XA_WM_COLORMAP_NOTIFY;
126 extern Atom _XA_GNUSTEP_WM_ATTR;
128 extern Atom _XA_WINDOWMAKER_MENU;
129 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
130 extern Atom _XA_WINDOWMAKER_STATE;
131 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
132 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
133 extern Atom _XA_WINDOWMAKER_COMMAND;
134 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
135 extern Atom _XA_WINDOWMAKER_ICON_TILE;
137 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
138 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
140 #ifdef OFFIX_DND
141 extern Atom _XA_DND_PROTOCOL;
142 extern Atom _XA_DND_SELECTION;
143 #endif
146 /* cursors */
147 extern Cursor wCursor[WCUR_LAST];
149 /* special flags */
150 extern char WDelayedActionSet;
152 /***** Local *****/
154 static WScreen **wScreen = NULL;
157 static unsigned int _NumLockMask = 0;
158 static unsigned int _ScrollLockMask = 0;
162 static void manageAllWindows();
164 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
167 static int
168 catchXError(Display *dpy, XErrorEvent *error)
170 char buffer[MAXLINE];
172 /* ignore some errors */
173 if (error->resourceid != None
174 && ((error->error_code == BadDrawable
175 && error->request_code == X_GetGeometry)
176 || (error->error_code == BadMatch
177 && (error->request_code == X_SetInputFocus))
178 || (error->error_code == BadWindow)
180 && (error->request_code == X_GetWindowAttributes
181 || error->request_code == X_SetInputFocus
182 || error->request_code == X_ChangeWindowAttributes
183 || error->request_code == X_GetProperty
184 || error->request_code == X_ChangeProperty
185 || error->request_code == X_QueryTree
186 || error->request_code == X_GrabButton
187 || error->request_code == X_UngrabButton
188 || error->request_code == X_SendEvent
189 || error->request_code == X_ConfigureWindow))
191 || (error->request_code == X_InstallColormap))) {
192 #ifndef DEBUG
194 return 0;
195 #else
196 printf("got X error %x %x %x\n", error->request_code,
197 error->error_code, (unsigned)error->resourceid);
198 return 0;
199 #endif
201 FormatXError(dpy, error, buffer, MAXLINE);
202 wwarning(_("internal X error: %s\n"), buffer);
203 return -1;
208 *----------------------------------------------------------------------
209 * handleXIO-
210 * Handle X shutdowns and other stuff.
211 *----------------------------------------------------------------------
213 static int
214 handleXIO(Display *xio_dpy)
216 dpy = NULL;
217 Exit(0);
218 return 0;
223 *----------------------------------------------------------------------
224 * delayedAction-
225 * Action to be executed after the signal() handler is exited.
226 *----------------------------------------------------------------------
228 static void
229 delayedAction(void *cdata)
231 if (WDelatedActionSet == 0)
232 return;
233 WDelayedActionSet = 0;
235 * Make the event dispatcher do whatever it needs to do,
236 * including handling zombie processes, restart and exit
237 * signals.
239 DispatchEvent(NULL);
244 *----------------------------------------------------------------------
245 * handleExitSig--
246 * User generated exit signal handler.
247 *----------------------------------------------------------------------
249 static RETSIGTYPE
250 handleExitSig(int sig)
252 sigset_t sigs;
254 sigfillset(&sigs);
255 sigprocmask(SIG_BLOCK, &sigs, NULL);
257 if (sig == SIGUSR1) {
258 #ifdef SYS_SIGLIST_DECLARED
259 wwarning(_("got signal %i (%s) - restarting\n"), sig, sys_siglist[sig]);
260 #else
261 wwarning(_("got signal %i - restarting\n"), sig);
262 #endif
264 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
265 /* setup idle handler, so that this will be handled when
266 * the select() is returned becaused of the signal, even if
267 * there are no X events in the queue */
268 WDelayedActionSet = 1;
269 } else if (sig == SIGUSR2) {
270 #ifdef SYS_SIGLIST_DECLARED
271 wwarning(_("got signal %i (%s) - rereading defaults\n"), sig, sys_siglist[sig]);
272 #else
273 wwarning(_("got signal %i - rereading defaults\n"), sig);
274 #endif
276 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
277 /* setup idle handler, so that this will be handled when
278 * the select() is returned becaused of the signal, even if
279 * there are no X events in the queue */
280 WDelayedActionSet = 1;
281 } else if (sig == SIGINT || sig == SIGHUP) {
282 #ifdef SYS_SIGLIST_DECLARED
283 wwarning(_("got signal %i (%s) - exiting...\n"), sig, sys_siglist[sig]);
284 #else
285 wwarning(_("got signal %i - exiting...\n"), sig);
286 #endif
288 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
290 WDelayedActionSet = 1;
293 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
297 *----------------------------------------------------------------------
298 * handleSig--
299 * general signal handler. Exits the program gently.
300 *----------------------------------------------------------------------
302 static RETSIGTYPE
303 handleSig(int sig)
305 static int already_crashed = 0;
306 int dumpcore = 0;
307 #ifndef NO_EMERGENCY_AUTORESTART
308 int crashAction;
309 char *argv[2];
311 argv[1] = NULL;
312 #endif
315 * No functions that potentially do Xlib calls should be called from
316 * here. Xlib calls are not reentrant so the integrity of Xlib is
317 * not guaranteed if a Xlib call is made from a signal handler.
320 #ifdef SYS_SIGLIST_DECLARED
321 wfatal(_("got signal %i (%s)\n"), sig, sys_siglist[sig]);
322 #else
323 wfatal(_("got signal %i\n"), sig);
324 #endif
326 /* Setting the signal behaviour back to default and then reraising the
327 * signal is a cleaner way to make program exit and core dump than calling
328 * abort(), since it correctly returns from the signal handler and sets
329 * the flags accordingly. -Dan
331 if (sig==SIGSEGV || sig==SIGFPE || sig==SIGBUS || sig==SIGILL
332 || sig==SIGABRT) {
333 if (already_crashed) {
334 wfatal(_("crashed while trying to do some post-crash cleanup. Aborting immediatelly."));
335 signal(sig, SIG_DFL);
336 kill(getpid(), sig);
337 return;
339 already_crashed = 1;
341 dumpcore = 1;
344 * Yeah, we shouldn't do this, but it's already crashed anyway :P
347 #ifndef NO_EMERGENCY_AUTORESTART
348 /* Close the X connection and open a new one. This is to avoid messing
349 * Xlib because we call to Xlib functions in a signal handler.
351 if (dpy)
352 XCloseDisplay(dpy);
353 dpy = XOpenDisplay("");
354 if (dpy) {
355 XGrabServer(dpy);
356 crashAction = wShowCrashingDialogPanel(sig);
357 XCloseDisplay(dpy);
358 dpy = NULL;
359 } else {
360 wsyserror(_("cannot open connection for crashing dialog panel. Aborting."));
361 crashAction = WMAbort;
364 if (crashAction == WMAbort) {
365 signal(sig, SIG_DFL);
366 kill(getpid(), sig);
367 return;
370 if (crashAction == WMRestart) {
371 /* we try to restart Window Maker */
372 wmessage(_("trying to restart Window Maker..."));
373 Restart(NULL, False);
374 /* fallback to alternate window manager then */
377 wmessage(_("trying to start alternate window manager..."));
379 Restart(FALLBACK_WINDOWMANAGER, False);
380 Restart("fvwm", False);
381 Restart("twm", False);
382 wfatal(_("failed to start alternate window manager. Aborting."));
383 #else
384 wfatal(_("a fatal error has occured, probably due to a bug. "
385 "Please fill the included BUGFORM and report it."));
386 #endif /* !NO_EMERGENCY_AUTORESTART */
388 signal(sig, SIG_DFL);
389 kill(getpid(), sig);
390 return;
394 wAbort(dumpcore);
398 static RETSIGTYPE
399 buryChild(int foo)
401 pid_t pid;
402 int status;
403 int save_errno = errno;
404 sigset_t sigs;
406 sigfillset(&sigs);
407 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
408 sigprocmask(SIG_BLOCK, &sigs, NULL);
410 /* R.I.P. */
411 /* If 2 or more kids exit in a small time window, before this handler gets
412 * the chance to get invoked, the SIGCHLD signals will be merged and only
413 * one SIGCHLD signal will be sent to us. We use a while loop to get all
414 * exited child status because we can't count on the number of SIGCHLD
415 * signals to know exactly how many kids have exited. -Dan
417 while ((pid=waitpid(-1, &status, WNOHANG))>0 || (pid<0 && errno==EINTR)) {
418 NotifyDeadProcess(pid, WEXITSTATUS(status));
421 WDelayedActionSet = 1;
423 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
425 errno = save_errno;
429 static int
430 getWorkspaceState(Window root, WWorkspaceState **state)
432 Atom type_ret;
433 int fmt_ret;
434 unsigned long nitems_ret;
435 unsigned long bytes_after_ret;
436 CARD32 *data;
438 if (XGetWindowProperty(dpy, root, _XA_WINDOWMAKER_STATE, 0, 2,
439 True, _XA_WINDOWMAKER_STATE,
440 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
441 (unsigned char **)&data)!=Success || !data)
442 return 0;
444 *state = malloc(sizeof(WWorkspaceState));
445 if (*state) {
446 (*state)->flags = data[0];
447 (*state)->workspace = data[1];
449 XFree(data);
451 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
452 return 1;
453 else
454 return 0;
458 static void
459 getOffendingModifiers()
461 int i;
462 XModifierKeymap *modmap;
463 KeyCode nlock, slock;
464 static int mask_table[8] = {
465 ShiftMask,LockMask,ControlMask,Mod1Mask,
466 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
469 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
470 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
473 * Find out the masks for the NumLock and ScrollLock modifiers,
474 * so that we can bind the grabs for when they are enabled too.
476 modmap = XGetModifierMapping(dpy);
478 if (modmap!=NULL && modmap->max_keypermod>0) {
479 for (i=0; i<8*modmap->max_keypermod; i++) {
480 if (modmap->modifiermap[i]==nlock && nlock!=0)
481 _NumLockMask = mask_table[i/modmap->max_keypermod];
482 else if (modmap->modifiermap[i]==slock && slock!=0)
483 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
487 if (modmap)
488 XFreeModifiermap(modmap);
493 #ifdef NUMLOCK_HACK
494 void
495 wHackedGrabKey(int keycode, unsigned int modifiers,
496 Window grab_window, Bool owner_events, int pointer_mode,
497 int keyboard_mode)
499 if (modifiers == AnyModifier)
500 return;
502 /* grab all combinations of the modifier with CapsLock, NumLock and
503 * ScrollLock. How much memory/CPU does such a monstrosity consume
504 * in the server?
506 if (_NumLockMask)
507 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
508 grab_window, owner_events, pointer_mode, keyboard_mode);
509 if (_ScrollLockMask)
510 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
511 grab_window, owner_events, pointer_mode, keyboard_mode);
512 if (_NumLockMask && _ScrollLockMask)
513 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
514 grab_window, owner_events, pointer_mode, keyboard_mode);
515 if (_NumLockMask)
516 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
517 grab_window, owner_events, pointer_mode, keyboard_mode);
518 if (_ScrollLockMask)
519 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
520 grab_window, owner_events, pointer_mode, keyboard_mode);
521 if (_NumLockMask && _ScrollLockMask)
522 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
523 grab_window, owner_events, pointer_mode, keyboard_mode);
524 /* phew, I guess that's all, right? */
526 #endif
528 void
529 wHackedGrabButton(unsigned int button, unsigned int modifiers,
530 Window grab_window, Bool owner_events,
531 unsigned int event_mask, int pointer_mode,
532 int keyboard_mode, Window confine_to, Cursor cursor)
534 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
535 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
537 if (modifiers==AnyModifier)
538 return;
540 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
541 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
543 #ifdef NUMLOCK_HACK
544 /* same as above, but for mouse buttons */
545 if (_NumLockMask)
546 XGrabButton(dpy, button, modifiers|_NumLockMask,
547 grab_window, owner_events, event_mask, pointer_mode,
548 keyboard_mode, confine_to, cursor);
549 if (_ScrollLockMask)
550 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
551 grab_window, owner_events, event_mask, pointer_mode,
552 keyboard_mode, confine_to, cursor);
553 if (_NumLockMask && _ScrollLockMask)
554 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
555 grab_window, owner_events, event_mask, pointer_mode,
556 keyboard_mode, confine_to, cursor);
557 if (_NumLockMask)
558 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
559 grab_window, owner_events, event_mask, pointer_mode,
560 keyboard_mode, confine_to, cursor);
561 if (_ScrollLockMask)
562 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
563 grab_window, owner_events, event_mask, pointer_mode,
564 keyboard_mode, confine_to, cursor);
565 if (_NumLockMask && _ScrollLockMask)
566 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
567 grab_window, owner_events, event_mask, pointer_mode,
568 keyboard_mode, confine_to, cursor);
569 #endif /* NUMLOCK_HACK */
572 #ifdef notused
573 void
574 wHackedUngrabButton(unsigned int button, unsigned int modifiers,
575 Window grab_window)
577 XUngrabButton(dpy, button, modifiers|_NumLockMask,
578 grab_window);
579 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
580 grab_window);
581 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
582 grab_window);
583 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
584 grab_window);
585 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
586 grab_window);
587 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
588 grab_window);
590 #endif
594 WScreen*
595 wScreenWithNumber(int i)
597 assert(i < wScreenCount);
599 return wScreen[i];
603 WScreen*
604 wScreenForRootWindow(Window window)
606 int i;
608 if (wScreenCount==1)
609 return wScreen[0];
612 * Since the number of heads will probably be small (normally 2),
613 * it should be faster to use this than a hash table, because
614 * of the overhead.
616 for (i=0; i<wScreenCount; i++) {
617 if (wScreen[i]->root_win == window) {
618 return wScreen[i];
622 assert("bad_root_window" && 0);
624 return NULL;
628 WScreen*
629 wScreenSearchForRootWindow(Window window)
631 int i;
633 if (wScreenCount==1)
634 return wScreen[0];
637 * Since the number of heads will probably be small (normally 2),
638 * it should be faster to use this than a hash table, because
639 * of the overhead.
641 for (i=0; i<wScreenCount; i++) {
642 if (wScreen[i]->root_win == window) {
643 return wScreen[i];
647 return NULL;
651 WScreen*
652 wScreenForWindow(Window window)
654 XWindowAttributes attr;
656 if (wScreenCount==1)
657 return wScreen[0];
659 if (XGetWindowAttributes(dpy, window, &attr)) {
660 return wScreenForRootWindow(attr.root);
662 return NULL;
666 static char *atomNames[] = {
667 "WM_STATE",
668 "WM_CHANGE_STATE",
669 "WM_PROTOCOLS",
670 "WM_TAKE_FOCUS",
671 "WM_DELETE_WINDOW",
672 "WM_SAVE_YOURSELF",
673 "WM_CLIENT_LEADER",
674 "WM_COLORMAP_WINDOWS",
675 "WM_COLORMAP_NOTIFY",
676 GNUSTEP_WM_ATTR_NAME,
677 "_WINDOWMAKER_MENU",
678 "_WINDOWMAKER_STATE",
679 "_WINDOWMAKER_WM_PROTOCOLS",
680 GNUSTEP_WM_MINIATURIZE_WINDOW,
681 "_WINDOWMAKER_WM_FUNCTION",
682 "_WINDOWMAKER_NOTICEBOARD",
683 "_WINDOWMAKER_COMMAND",
684 "_WINDOWMAKER_ICON_SIZE",
685 "_WINDOWMAKER_ICON_TILE",
686 GNUSTEP_TITLEBAR_STATE
691 *----------------------------------------------------------
692 * StartUp--
693 * starts the window manager and setup global data.
694 * Called from main() at startup.
696 * Side effects:
697 * global data declared in main.c is initialized
698 *----------------------------------------------------------
700 void
701 StartUp(Bool defaultScreenOnly)
703 WWorkspaceState *ws_state;
704 struct sigaction sig_action;
705 int j, max;
706 Atom atom[sizeof(atomNames)/sizeof(char*)];
709 * Ignore CapsLock in modifiers
711 ValidModMask = 0xff & ~LockMask;
713 getOffendingModifiers();
715 * Ignore NumLock and ScrollLock too
717 ValidModMask &= ~(_NumLockMask|_ScrollLockMask);
720 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
722 wWinContext = XUniqueContext();
723 wAppWinContext = XUniqueContext();
724 wStackContext = XUniqueContext();
726 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
728 #ifdef HAVE_XINTERNATOMS
729 XInternAtoms(dpy, atomNames, sizeof(atomNames)/sizeof(char*),
730 False, atom);
731 #else
734 int i;
735 for (i = 0; i < sizeof(atomNames)/sizeof(char*); i++) {
736 atom[i] = XInternAtom(dpy, atomNames[i], False);
739 #endif
741 _XA_WM_STATE = atom[0];
742 _XA_WM_CHANGE_STATE = atom[1];
743 _XA_WM_PROTOCOLS = atom[2];
744 _XA_WM_TAKE_FOCUS = atom[3];
745 _XA_WM_DELETE_WINDOW = atom[4];
746 _XA_WM_SAVE_YOURSELF = atom[5];
747 _XA_WM_CLIENT_LEADER = atom[6];
748 _XA_WM_COLORMAP_WINDOWS = atom[7];
749 _XA_WM_COLORMAP_NOTIFY = atom[8];
751 _XA_GNUSTEP_WM_ATTR = atom[9];
753 _XA_WINDOWMAKER_MENU = atom[10];
754 _XA_WINDOWMAKER_STATE = atom[11];
756 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[12];
758 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[13];
760 _XA_WINDOWMAKER_WM_FUNCTION = atom[14];
762 _XA_WINDOWMAKER_NOTICEBOARD = atom[15];
764 _XA_WINDOWMAKER_COMMAND = atom[16];
766 _XA_WINDOWMAKER_ICON_SIZE = atom[17];
767 _XA_WINDOWMAKER_ICON_TILE = atom[18];
769 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
771 #ifdef OFFIX_DND
772 _XA_DND_SELECTION = XInternAtom(dpy, "DndSelection", False);
773 _XA_DND_PROTOCOL = XInternAtom(dpy, "DndProtocol", False);
774 #endif
775 #ifdef XDND
776 wXDNDInitializeAtoms();
777 #endif
780 /* cursors */
781 #ifdef DEFINABLE_CURSOR
782 wCursor[WCUR_NORMAL] = None;
783 #else
784 wCursor[WCUR_NORMAL] = XCreateFontCursor(dpy, XC_left_ptr);
785 #endif
786 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
787 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
788 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
789 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
790 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
791 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
792 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
793 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
794 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
795 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
796 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
797 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
798 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
799 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
801 /* signal handler stuff that gets called when a signal is caught */
802 WMAddEternalTimerHandler(500, delayedAction, NULL);
804 /* emergency exit... */
805 sig_action.sa_handler = handleSig;
806 sigemptyset(&sig_action.sa_mask);
808 sig_action.sa_flags = SA_RESTART;
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 sig_action.sa_handler = handleExitSig;
817 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
818 * immediately.
819 * -Dan */
820 sig_action.sa_flags = SA_RESTART;
821 /* sigaction(SIGTERM, &sig_action, NULL);*/
822 sigaction(SIGINT, &sig_action, NULL);
823 sigaction(SIGHUP, &sig_action, NULL);
824 sigaction(SIGUSR1, &sig_action, NULL);
825 sigaction(SIGUSR2, &sig_action, NULL);
827 /* ignore dead pipe */
828 sig_action.sa_handler = SIG_IGN;
829 sig_action.sa_flags = SA_RESTART;
830 sigaction(SIGPIPE, &sig_action, NULL);
832 /* handle dead children */
833 sig_action.sa_handler = buryChild;
834 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
835 sigaction(SIGCHLD, &sig_action, NULL);
837 /* Now we unblock all signals, that may have been blocked by the parent
838 * who exec()-ed us. This can happen for example if Window Maker crashes
839 * and restarts itself or another window manager from the signal handler.
840 * In this case, the new proccess inherits the blocked signal mask and
841 * will no longer react to that signal, until unblocked.
842 * This is because the signal handler of the proccess who crashed (parent)
843 * didn't return, and the signal remained blocked. -Dan
845 sigfillset(&sig_action.sa_mask);
846 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
848 /* handle X shutdowns a such */
849 XSetIOErrorHandler(handleXIO);
851 /* set hook for out event dispatcher in WINGs event dispatcher */
852 WMHookEventHandler(DispatchEvent);
854 /* initialize defaults stuff */
855 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
856 if (!WDWindowMaker->dictionary) {
857 wwarning(_("could not read domain \"%s\" from defaults database"),
858 "WindowMaker");
861 /* read defaults that don't change until a restart and are
862 * screen independent */
863 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
865 /* check sanity of some values */
866 if (wPreferences.icon_size < 16) {
867 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
868 wPreferences.icon_size);
869 wPreferences.icon_size = 16;
872 /* init other domains */
873 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
874 if (!WDRootMenu->dictionary) {
875 wwarning(_("could not read domain \"%s\" from defaults database"),
876 "WMRootMenu");
879 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
880 if (!WDWindowAttributes->dictionary) {
881 wwarning(_("could not read domain \"%s\" from defaults database"),
882 "WMWindowAttributes");
885 XSetErrorHandler((XErrorHandler)catchXError);
887 #ifdef SHAPE
888 /* ignore j */
889 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
890 #endif
892 #ifdef KEEP_XKB_LOCK_STATUS
893 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
894 if(wPreferences.modelock && !wXkbSupported) {
895 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
896 wPreferences.modelock = 0;
898 #endif
900 if (defaultScreenOnly) {
901 max = 1;
902 } else {
903 max = ScreenCount(dpy);
905 wScreen = wmalloc(sizeof(WScreen*)*max);
907 wScreenCount = 0;
909 /* manage the screens */
910 for (j = 0; j < max; j++) {
911 if (defaultScreenOnly || max==1) {
912 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
913 if (!wScreen[wScreenCount]) {
914 wfatal(_("it seems that there is already a window manager running"));
915 Exit(1);
917 } else {
918 wScreen[wScreenCount] = wScreenInit(j);
919 if (!wScreen[wScreenCount]) {
920 wwarning(_("could not manage screen %i"), j);
921 continue;
924 wScreenCount++;
927 /* initialize/restore state for the screens */
928 for (j = 0; j < wScreenCount; j++) {
929 /* restore workspace state */
930 if (!getWorkspaceState(wScreen[j]->root_win, &ws_state)) {
931 ws_state = NULL;
934 wScreenRestoreState(wScreen[j]);
936 /* manage all windows that were already here before us */
937 if (!wPreferences.flags.nodock && wScreen[j]->dock)
938 wScreen[j]->last_dock = wScreen[j]->dock;
940 manageAllWindows(wScreen[j]);
942 /* restore saved menus */
943 wMenuRestoreState(wScreen[j]);
945 /* If we're not restarting restore session */
946 if (ws_state == NULL && !wPreferences.flags.norestore)
947 wSessionRestoreState(wScreen[j]);
949 if (!wPreferences.flags.noautolaunch) {
950 /* auto-launch apps */
951 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
952 wScreen[j]->last_dock = wScreen[j]->dock;
953 wDockDoAutoLaunch(wScreen[j]->dock, 0);
955 /* auto-launch apps in clip */
956 if (!wPreferences.flags.noclip) {
957 int i;
958 for(i=0; i<wScreen[j]->workspace_count; i++) {
959 if (wScreen[j]->workspaces[i]->clip) {
960 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
961 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
967 /* go to workspace where we were before restart */
968 if (ws_state) {
969 wWorkspaceForceChange(wScreen[j], ws_state->workspace);
970 wfree(ws_state);
971 } else {
972 wSessionRestoreLastWorkspace(wScreen[j]);
975 #ifdef KWM_HINTS
976 wKWMSetInitializedHint(wScreen[j]);
977 #endif
980 if (wScreenCount == 0) {
981 wfatal(_("could not manage any screen"));
982 Exit(1);
985 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates) {
986 /* setup defaults file polling */
987 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
994 static Bool
995 windowInList(Window window, Window *list, int count)
997 for (; count>=0; count--) {
998 if (window == list[count])
999 return True;
1001 return False;
1005 *-----------------------------------------------------------------------
1006 * manageAllWindows--
1007 * Manages all windows in the screen.
1009 * Notes:
1010 * Called when the wm is being started.
1011 * No events can be processed while the windows are being
1012 * reparented/managed.
1013 *-----------------------------------------------------------------------
1015 static void
1016 manageAllWindows(WScreen *scr)
1018 Window root, parent;
1019 Window *children;
1020 unsigned int nchildren;
1021 unsigned int i, j;
1022 WWindow *wwin;
1024 XGrabServer(dpy);
1025 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
1027 scr->flags.startup = 1;
1029 /* first remove all icon windows */
1030 for (i = 0; i < nchildren; i++) {
1031 XWMHints *wmhints;
1033 if (children[i]==None)
1034 continue;
1036 wmhints = XGetWMHints(dpy, children[i]);
1037 if (wmhints && (wmhints->flags & IconWindowHint)) {
1038 for (j = 0; j < nchildren; j++) {
1039 if (children[j] == wmhints->icon_window) {
1040 XFree(wmhints);
1041 wmhints = NULL;
1042 children[j] = None;
1043 break;
1047 if (wmhints) {
1048 XFree(wmhints);
1053 for (i = 0; i < nchildren; i++) {
1054 if (children[i] == None)
1055 continue;
1057 #ifdef KWM_HINTS
1058 wKWMCheckModule(scr, children[i]);
1059 #endif
1060 wwin = wManageWindow(scr, children[i]);
1061 if (wwin) {
1062 /* apply states got from WSavedState */
1063 /* shaded + minimized is not restored correctly */
1064 if (wwin->flags.shaded) {
1065 wwin->flags.shaded = 0;
1066 wShadeWindow(wwin);
1068 if (wwin->flags.miniaturized
1069 && (wwin->transient_for == None
1070 || wwin->transient_for == scr->root_win
1071 || !windowInList(wwin->transient_for, children,
1072 nchildren))) {
1074 wwin->flags.skip_next_animation = 1;
1075 wwin->flags.miniaturized = 0;
1076 wIconifyWindow(wwin);
1077 } else {
1078 wClientSetState(wwin, NormalState, None);
1082 XUngrabServer(dpy);
1084 /* hide apps */
1085 wwin = scr->focused_window;
1086 while (wwin) {
1087 if (wwin->flags.hidden) {
1088 WApplication *wapp = wApplicationOf(wwin->main_window);
1090 if (wapp) {
1091 wwin->flags.hidden = 0;
1092 wHideApplication(wapp);
1093 } else {
1094 wwin->flags.hidden = 0;
1097 wwin = wwin->prev;
1100 XFree(children);
1101 scr->flags.startup = 0;
1102 scr->flags.startup2 = 1;
1104 while (XPending(dpy)) {
1105 XEvent ev;
1106 WMNextEvent(dpy, &ev);
1107 WMHandleEvent(&ev);
1109 wWorkspaceForceChange(scr, 0);
1110 if (!wPreferences.flags.noclip)
1111 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1112 scr->flags.startup2 = 0;