Added some fixes from people, and updated Changelog about this.
[wmaker-crm.git] / src / startup.c
blob695c0d328e5b3f39db2ffb31d05f0ade8fbb3b5b
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"
61 #include "xutil.h"
63 #ifdef KWM_HINTS
64 #include "kwm.h"
65 #endif
67 #if 0
68 #ifdef SYS_SIGLIST_DECLARED
69 extern const char * const sys_siglist[];
70 #endif
71 #endif
73 /* for SunOS */
74 #ifndef SA_RESTART
75 # define SA_RESTART 0
76 #endif
79 /****** Global Variables ******/
81 extern WPreferences wPreferences;
83 extern WDDomain *WDWindowMaker;
84 extern WDDomain *WDRootMenu;
85 extern WDDomain *WDWindowAttributes;
87 extern WShortKey wKeyBindings[WKBD_LAST];
89 extern int wScreenCount;
92 #ifdef SHAPE
93 extern Bool wShapeSupported;
94 extern int wShapeEventBase;
95 #endif
97 /* contexts */
98 extern XContext wWinContext;
99 extern XContext wAppWinContext;
100 extern XContext wStackContext;
102 /* atoms */
103 extern Atom _XA_WM_STATE;
104 extern Atom _XA_WM_CHANGE_STATE;
105 extern Atom _XA_WM_PROTOCOLS;
106 extern Atom _XA_WM_TAKE_FOCUS;
107 extern Atom _XA_WM_DELETE_WINDOW;
108 extern Atom _XA_WM_SAVE_YOURSELF;
109 extern Atom _XA_WM_CLIENT_LEADER;
110 extern Atom _XA_WM_COLORMAP_WINDOWS;
111 extern Atom _XA_WM_COLORMAP_NOTIFY;
113 extern Atom _XA_GNUSTEP_WM_ATTR;
115 extern Atom _XA_WINDOWMAKER_MENU;
116 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
117 extern Atom _XA_WINDOWMAKER_STATE;
118 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
119 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
120 extern Atom _XA_WINDOWMAKER_COMMAND;
122 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
124 #ifdef OFFIX_DND
125 extern Atom _XA_DND_PROTOCOL;
126 extern Atom _XA_DND_SELECTION;
127 #endif
128 #ifdef XDE_DND
129 extern Atom _XA_XDE_REQUEST;
130 extern Atom _XA_XDE_ENTER;
131 extern Atom _XA_XDE_LEAVE;
132 extern Atom _XA_XDE_DATA_AVAILABLE;
133 extern Atom _XDE_FILETYPE;
134 extern Atom _XDE_URLTYPE;
135 #endif
138 /* cursors */
139 extern Cursor wCursor[WCUR_LAST];
141 /* special flags */
142 extern char WDelayedActionSet;
144 /***** Local *****/
146 static WScreen **wScreen = NULL;
149 static unsigned int _NumLockMask = 0;
150 static unsigned int _ScrollLockMask = 0;
154 static void manageAllWindows();
156 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
159 static int
160 catchXError(Display *dpy, XErrorEvent *error)
162 char buffer[MAXLINE];
164 /* ignore some errors */
165 if (error->resourceid != None
166 && ((error->error_code == BadDrawable
167 && error->request_code == X_GetGeometry)
168 || (error->error_code == BadMatch
169 && (error->request_code == X_SetInputFocus))
170 || (error->error_code == BadWindow)
172 && (error->request_code == X_GetWindowAttributes
173 || error->request_code == X_SetInputFocus
174 || error->request_code == X_ChangeWindowAttributes
175 || error->request_code == X_GetProperty
176 || error->request_code == X_ChangeProperty
177 || error->request_code == X_QueryTree
178 || error->request_code == X_GrabButton
179 || error->request_code == X_UngrabButton
180 || error->request_code == X_SendEvent
181 || error->request_code == X_ConfigureWindow))
183 || (error->request_code == X_InstallColormap))) {
184 #ifndef DEBUG
186 return 0;
187 #else
188 printf("got X error %x %x %x\n", error->request_code,
189 error->error_code, (unsigned)error->resourceid);
190 return 0;
191 #endif
193 FormatXError(dpy, error, buffer, MAXLINE);
194 wwarning(_("internal X error: %s\n"), buffer);
195 return -1;
200 *----------------------------------------------------------------------
201 * handleXIO-
202 * Handle X shutdowns and other stuff.
203 *----------------------------------------------------------------------
205 static int
206 handleXIO(Display *xio_dpy)
208 dpy = NULL;
209 Exit(0);
210 return 0;
215 *----------------------------------------------------------------------
216 * delayedAction-
217 * Action to be executed after the signal() handler is exited.
218 *----------------------------------------------------------------------
220 static void
221 delayedAction(void *cdata)
223 WDelayedActionSet = 0;
225 * Make the event dispatcher do whatever it needs to do,
226 * including handling zombie processes, restart and exit
227 * signals.
229 DispatchEvent(NULL);
235 *----------------------------------------------------------------------
236 * handleSig--
237 * general signal handler. Exits the program gently.
238 *----------------------------------------------------------------------
240 static RETSIGTYPE
241 handleSig(int sig)
243 static int already_crashed = 0;
244 int dumpcore = 0;
245 #ifndef NO_EMERGENCY_AUTORESTART
246 int crashAction;
247 char *argv[2];
249 argv[1] = NULL;
250 #endif
253 * No functions that potentially do Xlib calls should be called from
254 * here. Xlib calls are not reentrant so the integrity of Xlib is
255 * not guaranteed if a Xlib call is made from a signal handler.
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 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 if (!WDelayedActionSet) {
269 WDelayedActionSet = 1;
270 WMAddIdleHandler(delayedAction, NULL);
272 return;
273 } else if (sig == SIGTERM || sig == SIGHUP) {
274 #ifdef SYS_SIGLIST_DECLARED
275 wwarning(_("got signal %i (%s) - exiting...\n"), sig, sys_siglist[sig]);
276 #else
277 wwarning(_("got signal %i - exiting...\n"), sig);
278 #endif
280 WCHANGE_STATE(WSTATE_NEED_EXIT);
282 if (!WDelayedActionSet) {
283 WDelayedActionSet = 1;
284 WMAddIdleHandler(delayedAction, NULL);
286 return;
289 #ifdef SYS_SIGLIST_DECLARED
290 wfatal(_("got signal %i (%s)\n"), sig, sys_siglist[sig]);
291 #else
292 wfatal(_("got signal %i\n"), sig);
293 #endif
295 /* Setting the signal behaviour back to default and then reraising the
296 * signal is a cleaner way to make program exit and core dump than calling
297 * abort(), since it correctly returns from the signal handler and sets
298 * the flags accordingly. -Dan
300 if (sig==SIGSEGV || sig==SIGFPE || sig==SIGBUS || sig==SIGILL
301 || sig==SIGABRT) {
302 if (already_crashed) {
303 wfatal(_("crashed while trying to do some post-crash cleanup. Aborting immediatelly."));
304 signal(sig, SIG_DFL);
305 raise(sig);
306 return;
308 already_crashed = 1;
310 dumpcore = 1;
312 #ifndef NO_EMERGENCY_AUTORESTART
313 /* Close the X connection and open a new one. This is to avoid messing
314 * Xlib because we call to Xlib functions in a signal handler.
316 if (dpy)
317 XCloseDisplay(dpy);
318 dpy = XOpenDisplay("");
319 if (dpy) {
320 XGrabServer(dpy);
321 crashAction = wShowCrashingDialogPanel(sig);
322 XCloseDisplay(dpy);
323 dpy = NULL;
324 } else {
325 wsyserror(_("cannot open connection for crashing dialog panel. Aborting."));
326 crashAction = WMAbort;
329 if (crashAction == WMAbort) {
330 signal(sig, SIG_DFL);
331 raise(sig);
332 return;
335 if (crashAction == WMRestart) {
336 /* we try to restart Window Maker */
337 wwarning(_("trying to restart Window Maker..."));
338 Restart(NULL, False);
339 /* fallback to alternate window manager then */
342 wwarning(_("trying to start alternate window manager..."));
344 Restart(FALLBACK_WINDOWMANAGER, False);
345 Restart("fvwm", False);
346 Restart("twm", False);
347 wfatal(_("failed to start alternate window manager. Aborting."));
348 #else
349 wfatal(_("a fatal error has occured, probably due to a bug. "
350 "Please fill the included BUGFORM and report it."));
351 #endif /* !NO_EMERGENCY_AUTORESTART */
353 signal(sig, SIG_DFL);
354 raise(sig);
355 return;
359 wAbort(dumpcore);
363 static RETSIGTYPE
364 ignoreSig(int signal)
366 return;
370 static RETSIGTYPE
371 buryChild(int foo)
373 pid_t pid;
374 int status;
376 /* R.I.P. */
377 pid = waitpid(-1, &status, WNOHANG);
378 if (pid>0) {
379 NotifyDeadProcess(pid, WEXITSTATUS(status));
381 * Make sure that the kid will be buried even if there are
382 * no events in the X event queue
384 if (!WDelayedActionSet) {
385 WDelayedActionSet = 1;
386 WMAddIdleHandler(delayedAction, NULL);
392 static int
393 getWorkspaceState(Window root, WWorkspaceState **state)
395 Atom type_ret;
396 int fmt_ret;
397 unsigned long nitems_ret;
398 unsigned long bytes_after_ret;
399 CARD32 *data;
401 if (XGetWindowProperty(dpy, root, _XA_WINDOWMAKER_STATE, 0, 2,
402 True, _XA_WINDOWMAKER_STATE,
403 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
404 (unsigned char **)&data)!=Success || !data)
405 return 0;
407 *state = malloc(sizeof(WWorkspaceState));
408 if (*state) {
409 (*state)->flags = data[0];
410 (*state)->workspace = data[1];
412 XFree(data);
414 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
415 return 1;
416 else
417 return 0;
421 static void
422 getOffendingModifiers()
424 int i;
425 XModifierKeymap *modmap;
426 KeyCode nlock, slock;
427 static int mask_table[8] = {
428 ShiftMask,LockMask,ControlMask,Mod1Mask,
429 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
432 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
433 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
436 * Find out the masks for the NumLock and ScrollLock modifiers,
437 * so that we can bind the grabs for when they are enabled too.
439 modmap = XGetModifierMapping(dpy);
441 if (modmap!=NULL && modmap->max_keypermod>0) {
442 for (i=0; i<8*modmap->max_keypermod; i++) {
443 if (modmap->modifiermap[i]==nlock && nlock!=0)
444 _NumLockMask = mask_table[i/modmap->max_keypermod];
445 else if (modmap->modifiermap[i]==slock && slock!=0)
446 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
450 if (modmap)
451 XFreeModifiermap(modmap);
456 #ifdef NUMLOCK_HACK
457 void
458 wHackedGrabKey(int keycode, unsigned int modifiers,
459 Window grab_window, Bool owner_events, int pointer_mode,
460 int keyboard_mode)
462 if (modifiers == AnyModifier)
463 return;
465 /* grab all combinations of the modifier with CapsLock, NumLock and
466 * ScrollLock. How much memory/CPU does such a monstrosity consume
467 * in the server?
469 if (_NumLockMask)
470 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
471 grab_window, owner_events, pointer_mode, keyboard_mode);
472 if (_ScrollLockMask)
473 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
474 grab_window, owner_events, pointer_mode, keyboard_mode);
475 if (_NumLockMask && _ScrollLockMask)
476 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
477 grab_window, owner_events, pointer_mode, keyboard_mode);
478 if (_NumLockMask)
479 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
480 grab_window, owner_events, pointer_mode, keyboard_mode);
481 if (_ScrollLockMask)
482 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
483 grab_window, owner_events, pointer_mode, keyboard_mode);
484 if (_NumLockMask && _ScrollLockMask)
485 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
486 grab_window, owner_events, pointer_mode, keyboard_mode);
487 /* phew, I guess that's all, right? */
489 #endif
491 void
492 wHackedGrabButton(unsigned int button, unsigned int modifiers,
493 Window grab_window, Bool owner_events,
494 unsigned int event_mask, int pointer_mode,
495 int keyboard_mode, Window confine_to, Cursor cursor)
497 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
498 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
500 if (modifiers==AnyModifier)
501 return;
503 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
504 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
506 #ifdef NUMLOCK_HACK
507 /* same as above, but for mouse buttons */
508 if (_NumLockMask)
509 XGrabButton(dpy, button, modifiers|_NumLockMask,
510 grab_window, owner_events, event_mask, pointer_mode,
511 keyboard_mode, confine_to, cursor);
512 if (_ScrollLockMask)
513 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
514 grab_window, owner_events, event_mask, pointer_mode,
515 keyboard_mode, confine_to, cursor);
516 if (_NumLockMask && _ScrollLockMask)
517 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
518 grab_window, owner_events, event_mask, pointer_mode,
519 keyboard_mode, confine_to, cursor);
520 if (_NumLockMask)
521 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
522 grab_window, owner_events, event_mask, pointer_mode,
523 keyboard_mode, confine_to, cursor);
524 if (_ScrollLockMask)
525 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
526 grab_window, owner_events, event_mask, pointer_mode,
527 keyboard_mode, confine_to, cursor);
528 if (_NumLockMask && _ScrollLockMask)
529 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
530 grab_window, owner_events, event_mask, pointer_mode,
531 keyboard_mode, confine_to, cursor);
532 #endif /* NUMLOCK_HACK */
535 #ifdef notused
536 void
537 wHackedUngrabButton(unsigned int button, unsigned int modifiers,
538 Window grab_window)
540 XUngrabButton(dpy, button, modifiers|_NumLockMask,
541 grab_window);
542 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
543 grab_window);
544 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
545 grab_window);
546 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
547 grab_window);
548 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
549 grab_window);
550 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
551 grab_window);
553 #endif
557 WScreen*
558 wScreenWithNumber(int i)
560 assert(i < wScreenCount);
562 return wScreen[i];
566 WScreen*
567 wScreenForRootWindow(Window window)
569 int i;
571 if (wScreenCount==1)
572 return wScreen[0];
575 * Since the number of heads will probably be small (normally 2),
576 * it should be faster to use this than a hash table, because
577 * of the overhead.
579 for (i=0; i<wScreenCount; i++) {
580 if (wScreen[i]->root_win == window) {
581 return wScreen[i];
585 assert("bad_root_window" && 0);
587 return NULL;
591 WScreen*
592 wScreenSearchForRootWindow(Window window)
594 int i;
596 if (wScreenCount==1)
597 return wScreen[0];
600 * Since the number of heads will probably be small (normally 2),
601 * it should be faster to use this than a hash table, because
602 * of the overhead.
604 for (i=0; i<wScreenCount; i++) {
605 if (wScreen[i]->root_win == window) {
606 return wScreen[i];
610 return NULL;
614 WScreen*
615 wScreenForWindow(Window window)
617 XWindowAttributes attr;
619 if (wScreenCount==1)
620 return wScreen[0];
622 if (XGetWindowAttributes(dpy, window, &attr)) {
623 return wScreenForRootWindow(attr.root);
625 return NULL;
631 *----------------------------------------------------------
632 * StartUp--
633 * starts the window manager and setup global data.
634 * Called from main() at startup.
636 * Side effects:
637 * global data declared in main.c is initialized
638 *----------------------------------------------------------
640 void
641 StartUp(Bool defaultScreenOnly)
643 WWorkspaceState *ws_state;
644 struct sigaction sig_action;
645 int j, max;
648 * Ignore CapsLock in modifiers
650 ValidModMask = 0xff & ~LockMask;
652 getOffendingModifiers();
654 * Ignore NumLock and ScrollLock too
656 ValidModMask &= ~(_NumLockMask|_ScrollLockMask);
659 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
661 wWinContext = XUniqueContext();
662 wAppWinContext = XUniqueContext();
663 wStackContext = XUniqueContext();
665 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
667 _XA_WM_STATE = XInternAtom(dpy, "WM_STATE", False);
668 _XA_WM_CHANGE_STATE = XInternAtom(dpy, "WM_CHANGE_STATE", False);
669 _XA_WM_PROTOCOLS = XInternAtom(dpy, "WM_PROTOCOLS", False);
670 _XA_WM_TAKE_FOCUS = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
671 _XA_WM_DELETE_WINDOW = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
672 _XA_WM_SAVE_YOURSELF = XInternAtom(dpy, "WM_SAVE_YOURSELF", False);
673 _XA_WM_CLIENT_LEADER = XInternAtom(dpy, "WM_CLIENT_LEADER", False);
674 _XA_WM_COLORMAP_WINDOWS = XInternAtom(dpy, "WM_COLORMAP_WINDOWS", False);
675 _XA_WM_COLORMAP_NOTIFY = XInternAtom(dpy, "WM_COLORMAP_NOTIFY", False);
677 _XA_GNUSTEP_WM_ATTR = XInternAtom(dpy, GNUSTEP_WM_ATTR_NAME, False);
679 _XA_WINDOWMAKER_MENU = XInternAtom(dpy, "_WINDOWMAKER_MENU", False);
680 _XA_WINDOWMAKER_STATE = XInternAtom(dpy, "_WINDOWMAKER_STATE", False);
682 _XA_WINDOWMAKER_WM_PROTOCOLS =
683 XInternAtom(dpy, "_WINDOWMAKER_WM_PROTOCOLS", False);
685 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW =
686 XInternAtom(dpy, GNUSTEP_WM_MINIATURIZE_WINDOW, False);
688 _XA_WINDOWMAKER_WM_FUNCTION = XInternAtom(dpy, "_WINDOWMAKER_WM_FUNCTION",
689 False);
691 _XA_WINDOWMAKER_NOTICEBOARD = XInternAtom(dpy, "_WINDOWMAKER_NOTICEBOARD",
692 False);
694 _XA_WINDOWMAKER_COMMAND = XInternAtom(dpy, "_WINDOWMAKER_COMMAND", False);
696 #ifdef OFFIX_DND
697 _XA_DND_SELECTION = XInternAtom(dpy, "DndSelection", False);
698 _XA_DND_PROTOCOL = XInternAtom(dpy, "DndProtocol", False);
699 #endif
700 #ifdef XDE_DND
701 _XA_XDE_ENTER = XInternAtom(dpy, "_XDE_ENTER", False);
702 _XA_XDE_REQUEST = XInternAtom(dpy, "_XDE_REQUEST", False);
703 _XA_XDE_LEAVE = XInternAtom(dpy, "_XDE_LEAVE", False);
704 _XA_XDE_DATA_AVAILABLE = XInternAtom(dpy, "_XDE_DATA_AVAILABLE", False);
705 _XDE_FILETYPE = XInternAtom(dpy, "file:ALL", False);
706 _XDE_URLTYPE = XInternAtom(dpy, "url:ALL", False);
707 #endif
710 /* cursors */
711 wCursor[WCUR_NORMAL] = XCreateFontCursor(dpy, XC_left_ptr);
712 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
713 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
714 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
715 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
716 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
717 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
719 /* emergency exit... */
720 sig_action.sa_handler = handleSig;
721 sigemptyset(&sig_action.sa_mask);
723 /* Here we don't care about SA_RESTART since these signals will close
724 * wmaker anyway.
725 * -Dan */
726 sig_action.sa_flags = 0;
727 sigaction(SIGINT, &sig_action, NULL);
728 sigaction(SIGTERM, &sig_action, NULL);
729 sigaction(SIGHUP, &sig_action, NULL);
730 sigaction(SIGQUIT, &sig_action, NULL);
731 sigaction(SIGSEGV, &sig_action, NULL);
732 sigaction(SIGBUS, &sig_action, NULL);
733 sigaction(SIGFPE, &sig_action, NULL);
734 sigaction(SIGABRT, &sig_action, NULL);
736 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
737 * immediately.
738 * -Dan */
739 sig_action.sa_flags = SA_RESTART;
740 sigaction(SIGUSR1, &sig_action, NULL);
742 /* ignore dead pipe */
743 sig_action.sa_handler = ignoreSig;
744 sig_action.sa_flags = SA_RESTART;
745 sigaction(SIGPIPE, &sig_action, NULL);
747 /* handle dead children */
748 sig_action.sa_handler = buryChild;
749 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
750 sigaction(SIGCHLD, &sig_action, NULL);
752 /* Now we unblock all signals, that may have been blocked by the parent
753 * who exec()-ed us. This can happen for example if Window Maker crashes
754 * and restarts itself or another window manager from the signal handler.
755 * In this case, the new proccess inherits the blocked signal mask and
756 * will no longer react to that signal, until unblocked.
757 * This is because the signal handler of the proccess who crashed (parent)
758 * didn't return, and the signal remained blocked. -Dan
760 sigfillset(&sig_action.sa_mask);
761 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
763 /* handle X shutdowns a such */
764 XSetIOErrorHandler(handleXIO);
766 /* set hook for out event dispatcher in WINGs event dispatcher */
767 WMHookEventHandler(DispatchEvent);
769 /* initialize defaults stuff */
770 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
771 if (!WDWindowMaker->dictionary) {
772 wwarning(_("could not read domain \"%s\" from defaults database"),
773 "WindowMaker");
776 /* read defaults that don't change until a restart and are
777 * screen independent */
778 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
780 /* check sanity of some values */
781 if (wPreferences.icon_size < 16) {
782 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
783 wPreferences.icon_size);
784 wPreferences.icon_size = 16;
787 /* init other domains */
788 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
789 if (!WDRootMenu->dictionary) {
790 wwarning(_("could not read domain \"%s\" from defaults database"),
791 "WMRootMenu");
794 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
795 if (!WDWindowAttributes->dictionary) {
796 wwarning(_("could not read domain \"%s\" from defaults database"),
797 "WMWindowAttributes");
800 XSetErrorHandler((XErrorHandler)catchXError);
802 #ifdef SHAPE
803 /* ignore j */
804 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
805 #endif
807 if (defaultScreenOnly) {
808 max = 1;
809 } else {
810 max = ScreenCount(dpy);
812 wScreen = wmalloc(sizeof(WScreen*)*max);
814 wScreenCount = 0;
816 /* manage the screens */
817 for (j = 0; j < max; j++) {
818 if (defaultScreenOnly || max==1) {
819 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
820 if (!wScreen[wScreenCount]) {
821 wfatal(_("it seems that there is already a window manager running"));
822 Exit(1);
824 } else {
825 wScreen[wScreenCount] = wScreenInit(j);
826 if (!wScreen[wScreenCount]) {
827 wwarning(_("could not manage screen %i"), j);
828 continue;
831 wScreenCount++;
834 /* initialize/restore state for the screens */
835 for (j = 0; j < wScreenCount; j++) {
836 /* restore workspace state */
837 if (!getWorkspaceState(wScreen[j]->root_win, &ws_state)) {
838 ws_state = NULL;
841 wScreenRestoreState(wScreen[j]);
843 /* manage all windows that were already here before us */
844 if (!wPreferences.flags.nodock && wScreen[j]->dock)
845 wScreen[j]->last_dock = wScreen[j]->dock;
847 manageAllWindows(wScreen[j]);
849 /* restore saved menus */
850 wMenuRestoreState(wScreen[j]);
852 /* If we're not restarting restore session */
853 if (ws_state == NULL)
854 wSessionRestoreState(wScreen[j]);
857 /* auto-launch apps */
858 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
859 wScreen[j]->last_dock = wScreen[j]->dock;
860 wDockDoAutoLaunch(wScreen[j]->dock, 0);
862 /* auto-launch apps in clip */
863 if (!wPreferences.flags.noclip) {
864 int i;
865 for(i=0; i<wScreen[j]->workspace_count; i++) {
866 if (wScreen[j]->workspaces[i]->clip) {
867 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
868 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
873 /* go to workspace where we were before restart */
874 if (ws_state) {
875 wWorkspaceForceChange(wScreen[j], ws_state->workspace);
876 free(ws_state);
877 } else {
878 wSessionRestoreLastWorkspace(wScreen[j]);
881 #ifdef KWM_HINTS
882 wKWMSetInitializedHint(wScreen[j]);
883 #endif
886 if (wScreenCount == 0) {
887 wfatal(_("could not manage any screen"));
888 Exit(1);
891 if (!wPreferences.flags.noupdates) {
892 /* setup defaults file polling */
893 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
900 static Bool
901 windowInList(Window window, Window *list, int count)
903 for (; count>=0; count--) {
904 if (window == list[count])
905 return True;
907 return False;
911 *-----------------------------------------------------------------------
912 * manageAllWindows--
913 * Manages all windows in the screen.
915 * Notes:
916 * Called when the wm is being started.
917 * No events can be processed while the windows are being
918 * reparented/managed.
919 *-----------------------------------------------------------------------
921 static void
922 manageAllWindows(WScreen *scr)
924 Window root, parent;
925 Window *children;
926 unsigned int nchildren;
927 unsigned int i, j;
928 WWindow *wwin;
930 XGrabServer(dpy);
931 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
933 scr->flags.startup = 1;
935 /* first remove all icon windows */
936 for (i = 0; i < nchildren; i++) {
937 XWMHints *wmhints;
939 if (children[i]==None)
940 continue;
942 wmhints = XGetWMHints(dpy, children[i]);
943 if (wmhints && (wmhints->flags & IconWindowHint)) {
944 for (j = 0; j < nchildren; j++) {
945 if (children[j] == wmhints->icon_window) {
946 XFree(wmhints);
947 wmhints = NULL;
948 children[j] = None;
949 break;
953 if (wmhints) {
954 XFree(wmhints);
959 for (i = 0; i < nchildren; i++) {
960 if (children[i] == None)
961 continue;
963 #ifdef KWM_HINTS
964 wKWMCheckModule(scr, children[i]);
965 #endif
966 wwin = wManageWindow(scr, children[i]);
967 if (wwin) {
968 /* apply states got from WSavedState */
969 /* shaded + minimized is not restored correctly */
970 if (wwin->flags.shaded) {
971 wwin->flags.shaded = 0;
972 wShadeWindow(wwin);
974 if (wwin->flags.miniaturized
975 && (wwin->transient_for == None
976 || wwin->transient_for == scr->root_win
977 || !windowInList(wwin->transient_for, children,
978 nchildren))) {
980 wwin->flags.skip_next_animation = 1;
981 wwin->flags.miniaturized = 0;
982 wIconifyWindow(wwin);
983 } else {
984 wClientSetState(wwin, NormalState, None);
988 XUngrabServer(dpy);
990 /* hide apps */
991 wwin = scr->focused_window;
992 while (wwin) {
993 if (wwin->flags.hidden) {
994 WApplication *wapp = wApplicationOf(wwin->main_window);
996 if (wapp) {
997 wwin->flags.hidden = 0;
998 wHideApplication(wapp);
999 } else {
1000 wwin->flags.hidden = 0;
1003 wwin = wwin->prev;
1006 XFree(children);
1007 scr->flags.startup = 0;
1008 scr->flags.startup2 = 1;
1010 while (XPending(dpy)) {
1011 XEvent ev;
1012 WMNextEvent(dpy, &ev);
1013 WMHandleEvent(&ev);
1015 wWorkspaceForceChange(scr, 0);
1016 if (!wPreferences.flags.noclip)
1017 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1018 scr->flags.startup2 = 0;