- SIGTERM is now handled and saves the internal state before exiting (like
[wmaker-crm.git] / src / startup.c
blob9c6e4fdfc8dc5ffb644ef81e5148102e9e3c0258
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(WScreen *scr, int crashed);
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 (WDelayedActionSet == 0) {
232 return;
234 WDelayedActionSet--;
236 * Make the event dispatcher do whatever it needs to do,
237 * including handling zombie processes, restart and exit
238 * signals.
240 DispatchEvent(NULL);
245 *----------------------------------------------------------------------
246 * handleExitSig--
247 * User generated exit signal handler.
248 *----------------------------------------------------------------------
250 static RETSIGTYPE
251 handleExitSig(int sig)
253 sigset_t sigs;
255 sigfillset(&sigs);
256 sigprocmask(SIG_BLOCK, &sigs, NULL);
258 if (sig == SIGUSR1) {
259 #ifdef SYS_SIGLIST_DECLARED
260 wwarning(_("got signal %i (%s) - restarting\n"), sig, sys_siglist[sig]);
261 #else
262 wwarning(_("got signal %i - restarting\n"), sig);
263 #endif
265 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
266 /* setup idle handler, so that this will be handled when
267 * the select() is returned becaused of the signal, even if
268 * there are no X events in the queue */
269 WDelayedActionSet++;
270 } else if (sig == SIGUSR2) {
271 #ifdef SYS_SIGLIST_DECLARED
272 wwarning(_("got signal %i (%s) - rereading defaults\n"), sig, sys_siglist[sig]);
273 #else
274 wwarning(_("got signal %i - rereading defaults\n"), sig);
275 #endif
277 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
278 /* setup idle handler, so that this will be handled when
279 * the select() is returned becaused of the signal, even if
280 * there are no X events in the queue */
281 WDelayedActionSet++;
282 } else if (sig==SIGTERM || sig==SIGINT || sig==SIGHUP) {
283 #ifdef SYS_SIGLIST_DECLARED
284 wwarning(_("got signal %i (%s) - exiting...\n"), sig, sys_siglist[sig]);
285 #else
286 wwarning(_("got signal %i - exiting...\n"), sig);
287 #endif
289 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
291 WDelayedActionSet++;
294 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
297 /* Dummy signal handler */
298 static void
299 dummyHandler(int sig)
305 *----------------------------------------------------------------------
306 * handleSig--
307 * general signal handler. Exits the program gently.
308 *----------------------------------------------------------------------
310 static RETSIGTYPE
311 handleSig(int sig)
313 static int already_crashed = 0;
314 int dumpcore = 0;
315 #ifndef NO_EMERGENCY_AUTORESTART
316 int crashAction;
317 char *argv[2];
319 argv[1] = NULL;
320 #endif
323 * No functions that potentially do Xlib calls should be called from
324 * here. Xlib calls are not reentrant so the integrity of Xlib is
325 * not guaranteed if a Xlib call is made from a signal handler.
328 #ifdef SYS_SIGLIST_DECLARED
329 wfatal(_("got signal %i (%s)\n"), sig, sys_siglist[sig]);
330 #else
331 wfatal(_("got signal %i\n"), sig);
332 #endif
334 /* Setting the signal behaviour back to default and then reraising the
335 * signal is a cleaner way to make program exit and core dump than calling
336 * abort(), since it correctly returns from the signal handler and sets
337 * the flags accordingly. -Dan
339 if (sig==SIGSEGV || sig==SIGFPE || sig==SIGBUS || sig==SIGILL
340 || sig==SIGABRT) {
341 if (already_crashed) {
342 wfatal(_("crashed while trying to do some post-crash cleanup. Aborting immediatelly."));
343 signal(sig, SIG_DFL);
344 kill(getpid(), sig);
345 return;
347 already_crashed = 1;
349 dumpcore = 1;
352 * Yeah, we shouldn't do this, but it's already crashed anyway :P
355 #ifndef NO_EMERGENCY_AUTORESTART
356 /* Close the X connection and open a new one. This is to avoid messing
357 * Xlib because we call to Xlib functions in a signal handler.
359 if (dpy)
360 XCloseDisplay(dpy);
361 dpy = XOpenDisplay("");
362 if (dpy) {
363 CARD32 data[2];
364 WWindow *wwin;
365 int i;
367 XGrabServer(dpy);
368 crashAction = wShowCrashingDialogPanel(sig);
371 * Save current workspace, so can go back to it if restarting.
372 * Also add hint that we crashed, so that windows will be placed
373 * correctly upon restart.
375 for (i=0; i<wScreenCount; i++) {
376 data[0] = wScreen[i]->current_workspace; /* workspace number */
377 data[1] = WFLAGS_CRASHED; /* signal that we crashed */
379 XChangeProperty(dpy, wScreen[i]->root_win,
380 _XA_WINDOWMAKER_STATE, _XA_WINDOWMAKER_STATE,
381 32, PropModeReplace, (unsigned char*) data, 2);
383 /* save state of windows */
384 wwin = wScreen[i]->focused_window;
385 while (wwin) {
386 wWindowSaveState(wwin);
387 wwin = wwin->prev;
392 XCloseDisplay(dpy);
393 dpy = NULL;
394 } else {
395 wsyserror(_("cannot open connection for crashing dialog panel. Aborting."));
396 crashAction = WMAbort;
399 if (crashAction == WMAbort) {
400 signal(sig, SIG_DFL);
401 kill(getpid(), sig);
402 return;
405 if (crashAction == WMRestart) {
406 /* we try to restart Window Maker */
407 wmessage(_("trying to restart Window Maker..."));
408 Restart(NULL, False);
409 /* fallback to alternate window manager then */
412 wmessage(_("trying to start alternate window manager..."));
414 Restart(FALLBACK_WINDOWMANAGER, False);
415 Restart("fvwm", False);
416 Restart("twm", False);
417 wfatal(_("failed to start alternate window manager. Aborting."));
418 #else
419 wfatal(_("a fatal error has occured, probably due to a bug. "
420 "Please fill the included BUGFORM and report it."));
421 #endif /* !NO_EMERGENCY_AUTORESTART */
423 signal(sig, SIG_DFL);
424 kill(getpid(), sig);
425 return;
429 wAbort(dumpcore);
433 static RETSIGTYPE
434 buryChild(int foo)
436 pid_t pid;
437 int status;
438 int save_errno = errno;
439 sigset_t sigs;
441 sigfillset(&sigs);
442 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
443 sigprocmask(SIG_BLOCK, &sigs, NULL);
445 /* R.I.P. */
446 /* If 2 or more kids exit in a small time window, before this handler gets
447 * the chance to get invoked, the SIGCHLD signals will be merged and only
448 * one SIGCHLD signal will be sent to us. We use a while loop to get all
449 * exited child status because we can't count on the number of SIGCHLD
450 * signals to know exactly how many kids have exited. -Dan
452 while ((pid=waitpid(-1, &status, WNOHANG))>0 || (pid<0 && errno==EINTR)) {
453 NotifyDeadProcess(pid, WEXITSTATUS(status));
456 WDelayedActionSet++;
458 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
460 errno = save_errno;
464 static int
465 getWorkspaceState(Window root, WWorkspaceState **state)
467 Atom type_ret;
468 int fmt_ret;
469 unsigned long nitems_ret;
470 unsigned long bytes_after_ret;
471 CARD32 *data;
473 if (XGetWindowProperty(dpy, root, _XA_WINDOWMAKER_STATE, 0, 2,
474 True, _XA_WINDOWMAKER_STATE,
475 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
476 (unsigned char **)&data)!=Success || !data)
477 return 0;
479 *state = wmalloc(sizeof(WWorkspaceState));
480 (*state)->workspace = data[0];
481 (*state)->flags = data[1];
483 XFree(data);
485 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
486 return 1;
487 else
488 return 0;
492 static void
493 getOffendingModifiers()
495 int i;
496 XModifierKeymap *modmap;
497 KeyCode nlock, slock;
498 static int mask_table[8] = {
499 ShiftMask,LockMask,ControlMask,Mod1Mask,
500 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
503 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
504 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
507 * Find out the masks for the NumLock and ScrollLock modifiers,
508 * so that we can bind the grabs for when they are enabled too.
510 modmap = XGetModifierMapping(dpy);
512 if (modmap!=NULL && modmap->max_keypermod>0) {
513 for (i=0; i<8*modmap->max_keypermod; i++) {
514 if (modmap->modifiermap[i]==nlock && nlock!=0)
515 _NumLockMask = mask_table[i/modmap->max_keypermod];
516 else if (modmap->modifiermap[i]==slock && slock!=0)
517 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
521 if (modmap)
522 XFreeModifiermap(modmap);
527 #ifdef NUMLOCK_HACK
528 void
529 wHackedGrabKey(int keycode, unsigned int modifiers,
530 Window grab_window, Bool owner_events, int pointer_mode,
531 int keyboard_mode)
533 if (modifiers == AnyModifier)
534 return;
536 /* grab all combinations of the modifier with CapsLock, NumLock and
537 * ScrollLock. How much memory/CPU does such a monstrosity consume
538 * in the server?
540 if (_NumLockMask)
541 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
542 grab_window, owner_events, pointer_mode, keyboard_mode);
543 if (_ScrollLockMask)
544 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
545 grab_window, owner_events, pointer_mode, keyboard_mode);
546 if (_NumLockMask && _ScrollLockMask)
547 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
548 grab_window, owner_events, pointer_mode, keyboard_mode);
549 if (_NumLockMask)
550 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
551 grab_window, owner_events, pointer_mode, keyboard_mode);
552 if (_ScrollLockMask)
553 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
554 grab_window, owner_events, pointer_mode, keyboard_mode);
555 if (_NumLockMask && _ScrollLockMask)
556 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
557 grab_window, owner_events, pointer_mode, keyboard_mode);
558 /* phew, I guess that's all, right? */
560 #endif
562 void
563 wHackedGrabButton(unsigned int button, unsigned int modifiers,
564 Window grab_window, Bool owner_events,
565 unsigned int event_mask, int pointer_mode,
566 int keyboard_mode, Window confine_to, Cursor cursor)
568 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
569 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
571 if (modifiers==AnyModifier)
572 return;
574 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
575 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
577 #ifdef NUMLOCK_HACK
578 /* same as above, but for mouse buttons */
579 if (_NumLockMask)
580 XGrabButton(dpy, button, modifiers|_NumLockMask,
581 grab_window, owner_events, event_mask, pointer_mode,
582 keyboard_mode, confine_to, cursor);
583 if (_ScrollLockMask)
584 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
585 grab_window, owner_events, event_mask, pointer_mode,
586 keyboard_mode, confine_to, cursor);
587 if (_NumLockMask && _ScrollLockMask)
588 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
589 grab_window, owner_events, event_mask, pointer_mode,
590 keyboard_mode, confine_to, cursor);
591 if (_NumLockMask)
592 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
593 grab_window, owner_events, event_mask, pointer_mode,
594 keyboard_mode, confine_to, cursor);
595 if (_ScrollLockMask)
596 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
597 grab_window, owner_events, event_mask, pointer_mode,
598 keyboard_mode, confine_to, cursor);
599 if (_NumLockMask && _ScrollLockMask)
600 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
601 grab_window, owner_events, event_mask, pointer_mode,
602 keyboard_mode, confine_to, cursor);
603 #endif /* NUMLOCK_HACK */
606 #ifdef notused
607 void
608 wHackedUngrabButton(unsigned int button, unsigned int modifiers,
609 Window grab_window)
611 XUngrabButton(dpy, button, modifiers|_NumLockMask,
612 grab_window);
613 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
614 grab_window);
615 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
616 grab_window);
617 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
618 grab_window);
619 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
620 grab_window);
621 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
622 grab_window);
624 #endif
628 WScreen*
629 wScreenWithNumber(int i)
631 assert(i < wScreenCount);
633 return wScreen[i];
637 WScreen*
638 wScreenForRootWindow(Window window)
640 int i;
642 if (wScreenCount==1)
643 return wScreen[0];
646 * Since the number of heads will probably be small (normally 2),
647 * it should be faster to use this than a hash table, because
648 * of the overhead.
650 for (i=0; i<wScreenCount; i++) {
651 if (wScreen[i]->root_win == window) {
652 return wScreen[i];
656 return wScreenForWindow(window);
660 WScreen*
661 wScreenSearchForRootWindow(Window window)
663 int i;
665 if (wScreenCount==1)
666 return wScreen[0];
669 * Since the number of heads will probably be small (normally 2),
670 * it should be faster to use this than a hash table, because
671 * of the overhead.
673 for (i=0; i<wScreenCount; i++) {
674 if (wScreen[i]->root_win == window) {
675 return wScreen[i];
679 return wScreenForWindow(window);
683 WScreen*
684 wScreenForWindow(Window window)
686 XWindowAttributes attr;
688 if (wScreenCount==1)
689 return wScreen[0];
691 if (XGetWindowAttributes(dpy, window, &attr)) {
692 return wScreenForRootWindow(attr.root);
694 return NULL;
698 static char *atomNames[] = {
699 "WM_STATE",
700 "WM_CHANGE_STATE",
701 "WM_PROTOCOLS",
702 "WM_TAKE_FOCUS",
703 "WM_DELETE_WINDOW",
704 "WM_SAVE_YOURSELF",
705 "WM_CLIENT_LEADER",
706 "WM_COLORMAP_WINDOWS",
707 "WM_COLORMAP_NOTIFY",
709 "_WINDOWMAKER_MENU",
710 "_WINDOWMAKER_STATE",
711 "_WINDOWMAKER_WM_PROTOCOLS",
712 "_WINDOWMAKER_WM_FUNCTION",
713 "_WINDOWMAKER_NOTICEBOARD",
714 "_WINDOWMAKER_COMMAND",
715 "_WINDOWMAKER_ICON_SIZE",
716 "_WINDOWMAKER_ICON_TILE",
718 GNUSTEP_WM_ATTR_NAME,
719 GNUSTEP_WM_MINIATURIZE_WINDOW,
720 GNUSTEP_TITLEBAR_STATE
725 *----------------------------------------------------------
726 * StartUp--
727 * starts the window manager and setup global data.
728 * Called from main() at startup.
730 * Side effects:
731 * global data declared in main.c is initialized
732 *----------------------------------------------------------
734 void
735 StartUp(Bool defaultScreenOnly)
737 WWorkspaceState *ws_state;
738 struct sigaction sig_action;
739 int j, max;
740 Atom atom[sizeof(atomNames)/sizeof(char*)];
743 * Ignore CapsLock in modifiers
745 ValidModMask = 0xff & ~LockMask;
747 getOffendingModifiers();
749 * Ignore NumLock and ScrollLock too
751 ValidModMask &= ~(_NumLockMask|_ScrollLockMask);
754 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
756 wWinContext = XUniqueContext();
757 wAppWinContext = XUniqueContext();
758 wStackContext = XUniqueContext();
760 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
762 #ifdef HAVE_XINTERNATOMS
763 XInternAtoms(dpy, atomNames, sizeof(atomNames)/sizeof(char*),
764 False, atom);
765 #else
768 int i;
769 for (i = 0; i < sizeof(atomNames)/sizeof(char*); i++) {
770 atom[i] = XInternAtom(dpy, atomNames[i], False);
773 #endif
775 _XA_WM_STATE = atom[0];
776 _XA_WM_CHANGE_STATE = atom[1];
777 _XA_WM_PROTOCOLS = atom[2];
778 _XA_WM_TAKE_FOCUS = atom[3];
779 _XA_WM_DELETE_WINDOW = atom[4];
780 _XA_WM_SAVE_YOURSELF = atom[5];
781 _XA_WM_CLIENT_LEADER = atom[6];
782 _XA_WM_COLORMAP_WINDOWS = atom[7];
783 _XA_WM_COLORMAP_NOTIFY = atom[8];
785 _XA_WINDOWMAKER_MENU = atom[9];
786 _XA_WINDOWMAKER_STATE = atom[10];
787 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
788 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
789 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
790 _XA_WINDOWMAKER_COMMAND = atom[14];
791 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
792 _XA_WINDOWMAKER_ICON_TILE = atom[16];
794 _XA_GNUSTEP_WM_ATTR = atom[17];
795 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
796 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
798 #ifdef OFFIX_DND
799 _XA_DND_SELECTION = XInternAtom(dpy, "DndSelection", False);
800 _XA_DND_PROTOCOL = XInternAtom(dpy, "DndProtocol", False);
801 #endif
802 #ifdef XDND
803 wXDNDInitializeAtoms();
804 #endif
807 /* cursors */
808 #ifdef DEFINABLE_CURSOR
809 wCursor[WCUR_NORMAL] = None;
810 #else
811 wCursor[WCUR_NORMAL] = XCreateFontCursor(dpy, XC_left_ptr);
812 #endif
813 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
814 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
815 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
816 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
817 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
818 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
819 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
820 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
821 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
822 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
823 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
824 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
825 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
826 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
828 /* signal handler stuff that gets called when a signal is caught */
829 WMAddPersistentTimerHandler(500, delayedAction, NULL);
831 /* emergency exit... */
832 sig_action.sa_handler = handleSig;
833 sigemptyset(&sig_action.sa_mask);
835 sig_action.sa_flags = SA_RESTART;
836 sigaction(SIGQUIT, &sig_action, NULL);
837 sigaction(SIGSEGV, &sig_action, NULL);
838 sigaction(SIGBUS, &sig_action, NULL);
839 sigaction(SIGFPE, &sig_action, NULL);
840 sigaction(SIGABRT, &sig_action, NULL);
842 sig_action.sa_handler = handleExitSig;
844 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
845 * immediately. -Dan */
846 sig_action.sa_flags = SA_RESTART;
847 sigaction(SIGTERM, &sig_action, NULL);
848 sigaction(SIGINT, &sig_action, NULL);
849 sigaction(SIGHUP, &sig_action, NULL);
850 sigaction(SIGUSR1, &sig_action, NULL);
851 sigaction(SIGUSR2, &sig_action, NULL);
853 /* ignore dead pipe */
854 /* Because POSIX mandates that only signal with handlers are reset
855 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
856 * to children. Hence the dummy handler.
857 * Philippe Troin <phil@fifi.org>
859 sig_action.sa_handler = &dummyHandler;
860 sig_action.sa_flags = SA_RESTART;
861 sigaction(SIGPIPE, &sig_action, NULL);
863 /* handle dead children */
864 sig_action.sa_handler = buryChild;
865 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
866 sigaction(SIGCHLD, &sig_action, NULL);
868 /* Now we unblock all signals, that may have been blocked by the parent
869 * who exec()-ed us. This can happen for example if Window Maker crashes
870 * and restarts itself or another window manager from the signal handler.
871 * In this case, the new proccess inherits the blocked signal mask and
872 * will no longer react to that signal, until unblocked.
873 * This is because the signal handler of the proccess who crashed (parent)
874 * didn't return, and the signal remained blocked. -Dan
876 sigfillset(&sig_action.sa_mask);
877 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
879 /* handle X shutdowns a such */
880 XSetIOErrorHandler(handleXIO);
882 /* set hook for out event dispatcher in WINGs event dispatcher */
883 WMHookEventHandler(DispatchEvent);
885 /* initialize defaults stuff */
886 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
887 if (!WDWindowMaker->dictionary) {
888 wwarning(_("could not read domain \"%s\" from defaults database"),
889 "WindowMaker");
892 /* read defaults that don't change until a restart and are
893 * screen independent */
894 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
896 /* check sanity of some values */
897 if (wPreferences.icon_size < 16) {
898 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
899 wPreferences.icon_size);
900 wPreferences.icon_size = 16;
903 /* init other domains */
904 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
905 if (!WDRootMenu->dictionary) {
906 wwarning(_("could not read domain \"%s\" from defaults database"),
907 "WMRootMenu");
910 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
911 if (!WDWindowAttributes->dictionary) {
912 wwarning(_("could not read domain \"%s\" from defaults database"),
913 "WMWindowAttributes");
916 XSetErrorHandler((XErrorHandler)catchXError);
918 #ifdef SHAPE
919 /* ignore j */
920 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
921 #endif
923 #ifdef KEEP_XKB_LOCK_STATUS
924 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
925 if(wPreferences.modelock && !wXkbSupported) {
926 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
927 wPreferences.modelock = 0;
929 #endif
931 if (defaultScreenOnly) {
932 max = 1;
933 } else {
934 max = ScreenCount(dpy);
936 wScreen = wmalloc(sizeof(WScreen*)*max);
938 wScreenCount = 0;
940 /* manage the screens */
941 for (j = 0; j < max; j++) {
942 if (defaultScreenOnly || max==1) {
943 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
944 if (!wScreen[wScreenCount]) {
945 wfatal(_("it seems that there is already a window manager running"));
946 Exit(1);
948 } else {
949 wScreen[wScreenCount] = wScreenInit(j);
950 if (!wScreen[wScreenCount]) {
951 wwarning(_("could not manage screen %i"), j);
952 continue;
955 wScreenCount++;
958 /* initialize/restore state for the screens */
959 for (j = 0; j < wScreenCount; j++) {
960 int crashed;
962 /* restore workspace state */
963 if (!getWorkspaceState(wScreen[j]->root_win, &ws_state)) {
964 ws_state = NULL;
967 wScreenRestoreState(wScreen[j]);
969 /* manage all windows that were already here before us */
970 if (!wPreferences.flags.nodock && wScreen[j]->dock)
971 wScreen[j]->last_dock = wScreen[j]->dock;
973 if (ws_state && (ws_state->flags & WFLAGS_CRASHED)!=0)
974 crashed = 1;
975 else
976 crashed = 0;
978 manageAllWindows(wScreen[j], crashed);
980 /* restore saved menus */
981 wMenuRestoreState(wScreen[j]);
983 /* If we're not restarting restore session */
984 if (ws_state == NULL && !wPreferences.flags.norestore)
985 wSessionRestoreState(wScreen[j]);
987 if (!wPreferences.flags.noautolaunch) {
988 /* auto-launch apps */
989 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
990 wScreen[j]->last_dock = wScreen[j]->dock;
991 wDockDoAutoLaunch(wScreen[j]->dock, 0);
993 /* auto-launch apps in clip */
994 if (!wPreferences.flags.noclip) {
995 int i;
996 for(i=0; i<wScreen[j]->workspace_count; i++) {
997 if (wScreen[j]->workspaces[i]->clip) {
998 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
999 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
1005 /* go to workspace where we were before restart */
1006 if (ws_state) {
1007 wWorkspaceForceChange(wScreen[j], ws_state->workspace);
1008 wfree(ws_state);
1009 } else {
1010 wSessionRestoreLastWorkspace(wScreen[j]);
1013 #ifdef KWM_HINTS
1014 wKWMSetInitializedHint(wScreen[j]);
1015 #endif
1018 if (wScreenCount == 0) {
1019 wfatal(_("could not manage any screen"));
1020 Exit(1);
1023 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates) {
1024 /* setup defaults file polling */
1025 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
1032 static Bool
1033 windowInList(Window window, Window *list, int count)
1035 for (; count>=0; count--) {
1036 if (window == list[count])
1037 return True;
1039 return False;
1043 *-----------------------------------------------------------------------
1044 * manageAllWindows--
1045 * Manages all windows in the screen.
1047 * Notes:
1048 * Called when the wm is being started.
1049 * No events can be processed while the windows are being
1050 * reparented/managed.
1051 *-----------------------------------------------------------------------
1053 static void
1054 manageAllWindows(WScreen *scr, int crashRecovery)
1056 Window root, parent;
1057 Window *children;
1058 unsigned int nchildren;
1059 unsigned int i, j;
1060 WWindow *wwin;
1062 XGrabServer(dpy);
1063 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
1065 scr->flags.startup = 1;
1067 /* first remove all icon windows */
1068 for (i = 0; i < nchildren; i++) {
1069 XWMHints *wmhints;
1071 if (children[i]==None)
1072 continue;
1074 wmhints = XGetWMHints(dpy, children[i]);
1075 if (wmhints && (wmhints->flags & IconWindowHint)) {
1076 for (j = 0; j < nchildren; j++) {
1077 if (children[j] == wmhints->icon_window) {
1078 XFree(wmhints);
1079 wmhints = NULL;
1080 children[j] = None;
1081 break;
1085 if (wmhints) {
1086 XFree(wmhints);
1091 for (i = 0; i < nchildren; i++) {
1092 if (children[i] == None)
1093 continue;
1095 #ifdef KWM_HINTS
1096 wKWMCheckModule(scr, children[i]);
1097 #endif
1098 wwin = wManageWindow(scr, children[i]);
1099 if (wwin) {
1100 /* apply states got from WSavedState */
1101 /* shaded + minimized is not restored correctly */
1102 if (wwin->flags.shaded) {
1103 wwin->flags.shaded = 0;
1104 wShadeWindow(wwin);
1106 if (wwin->flags.miniaturized
1107 && (wwin->transient_for == None
1108 || wwin->transient_for == scr->root_win
1109 || !windowInList(wwin->transient_for, children,
1110 nchildren))) {
1112 wwin->flags.skip_next_animation = 1;
1113 wwin->flags.miniaturized = 0;
1114 wIconifyWindow(wwin);
1115 } else {
1116 wClientSetState(wwin, NormalState, None);
1118 if (crashRecovery) {
1119 int border;
1121 border = (WFLAGP(wwin, no_border) ? 0 : FRAME_BORDER_WIDTH);
1123 wWindowMove(wwin, wwin->frame_x - border,
1124 wwin->frame_y - border -
1125 (wwin->frame->titlebar ?
1126 wwin->frame->titlebar->height : 0));
1130 XUngrabServer(dpy);
1132 /* hide apps */
1133 wwin = scr->focused_window;
1134 while (wwin) {
1135 if (wwin->flags.hidden) {
1136 WApplication *wapp = wApplicationOf(wwin->main_window);
1138 if (wapp) {
1139 wwin->flags.hidden = 0;
1140 wHideApplication(wapp);
1141 } else {
1142 wwin->flags.hidden = 0;
1145 wwin = wwin->prev;
1148 XFree(children);
1149 scr->flags.startup = 0;
1150 scr->flags.startup2 = 1;
1152 while (XPending(dpy)) {
1153 XEvent ev;
1154 WMNextEvent(dpy, &ev);
1155 WMHandleEvent(&ev);
1157 wWorkspaceForceChange(scr, 0);
1158 if (!wPreferences.flags.noclip)
1159 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1160 scr->flags.startup2 = 0;