- Fixed the PPosition/PSize code (I hope) --Dan
[wmaker-crm.git] / src / startup.c
bloba8f826cb18662110f797ab1e45376f261ea04c8d
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
82 /****** Global Variables ******/
84 extern WPreferences wPreferences;
86 extern WDDomain *WDWindowMaker;
87 extern WDDomain *WDRootMenu;
88 extern WDDomain *WDWindowAttributes;
90 extern WShortKey wKeyBindings[WKBD_LAST];
92 extern int wScreenCount;
95 #ifdef SHAPE
96 extern Bool wShapeSupported;
97 extern int wShapeEventBase;
98 #endif
100 #ifdef KEEP_XKB_LOCK_STATUS
101 extern Bool wXkbSupported;
102 extern int wXkbEventBase;
103 #endif
105 /* contexts */
106 extern XContext wWinContext;
107 extern XContext wAppWinContext;
108 extern XContext wStackContext;
110 /* atoms */
111 extern Atom _XA_WM_STATE;
112 extern Atom _XA_WM_CHANGE_STATE;
113 extern Atom _XA_WM_PROTOCOLS;
114 extern Atom _XA_WM_TAKE_FOCUS;
115 extern Atom _XA_WM_DELETE_WINDOW;
116 extern Atom _XA_WM_SAVE_YOURSELF;
117 extern Atom _XA_WM_CLIENT_LEADER;
118 extern Atom _XA_WM_COLORMAP_WINDOWS;
119 extern Atom _XA_WM_COLORMAP_NOTIFY;
121 extern Atom _XA_GNUSTEP_WM_ATTR;
123 extern Atom _XA_WINDOWMAKER_MENU;
124 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
125 extern Atom _XA_WINDOWMAKER_STATE;
126 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
127 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
128 extern Atom _XA_WINDOWMAKER_COMMAND;
130 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
132 #ifdef OFFIX_DND
133 extern Atom _XA_DND_PROTOCOL;
134 extern Atom _XA_DND_SELECTION;
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 kill(getpid(), 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 kill(getpid(), 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 kill(getpid(), 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 XDND
701 wXDNDInitializeAtoms();
702 #endif
705 /* cursors */
706 wCursor[WCUR_NORMAL] = XCreateFontCursor(dpy, XC_left_ptr);
707 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
708 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
709 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
710 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
711 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
712 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
714 /* emergency exit... */
715 sig_action.sa_handler = handleSig;
716 sigemptyset(&sig_action.sa_mask);
718 /* Here we don't care about SA_RESTART since these signals will close
719 * wmaker anyway.
720 * -Dan */
721 sig_action.sa_flags = 0;
722 sigaction(SIGINT, &sig_action, NULL);
723 sigaction(SIGTERM, &sig_action, NULL);
724 sigaction(SIGHUP, &sig_action, NULL);
725 sigaction(SIGQUIT, &sig_action, NULL);
726 sigaction(SIGSEGV, &sig_action, NULL);
727 sigaction(SIGBUS, &sig_action, NULL);
728 sigaction(SIGFPE, &sig_action, NULL);
729 sigaction(SIGABRT, &sig_action, NULL);
731 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
732 * immediately.
733 * -Dan */
734 sig_action.sa_flags = SA_RESTART;
735 sigaction(SIGUSR1, &sig_action, NULL);
737 /* ignore dead pipe */
738 sig_action.sa_handler = ignoreSig;
739 sig_action.sa_flags = SA_RESTART;
740 sigaction(SIGPIPE, &sig_action, NULL);
742 /* handle dead children */
743 sig_action.sa_handler = buryChild;
744 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
745 sigaction(SIGCHLD, &sig_action, NULL);
747 /* Now we unblock all signals, that may have been blocked by the parent
748 * who exec()-ed us. This can happen for example if Window Maker crashes
749 * and restarts itself or another window manager from the signal handler.
750 * In this case, the new proccess inherits the blocked signal mask and
751 * will no longer react to that signal, until unblocked.
752 * This is because the signal handler of the proccess who crashed (parent)
753 * didn't return, and the signal remained blocked. -Dan
755 sigfillset(&sig_action.sa_mask);
756 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
758 /* handle X shutdowns a such */
759 XSetIOErrorHandler(handleXIO);
761 /* set hook for out event dispatcher in WINGs event dispatcher */
762 WMHookEventHandler(DispatchEvent);
764 /* initialize defaults stuff */
765 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
766 if (!WDWindowMaker->dictionary) {
767 wwarning(_("could not read domain \"%s\" from defaults database"),
768 "WindowMaker");
771 /* read defaults that don't change until a restart and are
772 * screen independent */
773 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
775 /* check sanity of some values */
776 if (wPreferences.icon_size < 16) {
777 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
778 wPreferences.icon_size);
779 wPreferences.icon_size = 16;
782 /* init other domains */
783 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
784 if (!WDRootMenu->dictionary) {
785 wwarning(_("could not read domain \"%s\" from defaults database"),
786 "WMRootMenu");
789 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
790 if (!WDWindowAttributes->dictionary) {
791 wwarning(_("could not read domain \"%s\" from defaults database"),
792 "WMWindowAttributes");
795 XSetErrorHandler((XErrorHandler)catchXError);
797 #ifdef SHAPE
798 /* ignore j */
799 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
800 #endif
802 #ifdef KEEP_XKB_LOCK_STATUS
803 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
804 if(wPreferences.modelock && !wXkbSupported) {
805 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
806 wPreferences.modelock = 0;
808 #endif
810 if (defaultScreenOnly) {
811 max = 1;
812 } else {
813 max = ScreenCount(dpy);
815 wScreen = wmalloc(sizeof(WScreen*)*max);
817 wScreenCount = 0;
819 /* manage the screens */
820 for (j = 0; j < max; j++) {
821 if (defaultScreenOnly || max==1) {
822 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
823 if (!wScreen[wScreenCount]) {
824 wfatal(_("it seems that there is already a window manager running"));
825 Exit(1);
827 } else {
828 wScreen[wScreenCount] = wScreenInit(j);
829 if (!wScreen[wScreenCount]) {
830 wwarning(_("could not manage screen %i"), j);
831 continue;
834 wScreenCount++;
837 /* initialize/restore state for the screens */
838 for (j = 0; j < wScreenCount; j++) {
839 /* restore workspace state */
840 if (!getWorkspaceState(wScreen[j]->root_win, &ws_state)) {
841 ws_state = NULL;
844 wScreenRestoreState(wScreen[j]);
846 /* manage all windows that were already here before us */
847 if (!wPreferences.flags.nodock && wScreen[j]->dock)
848 wScreen[j]->last_dock = wScreen[j]->dock;
850 manageAllWindows(wScreen[j]);
852 /* restore saved menus */
853 wMenuRestoreState(wScreen[j]);
855 /* If we're not restarting restore session */
856 if (ws_state == NULL)
857 wSessionRestoreState(wScreen[j]);
860 /* auto-launch apps */
861 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
862 wScreen[j]->last_dock = wScreen[j]->dock;
863 wDockDoAutoLaunch(wScreen[j]->dock, 0);
865 /* auto-launch apps in clip */
866 if (!wPreferences.flags.noclip) {
867 int i;
868 for(i=0; i<wScreen[j]->workspace_count; i++) {
869 if (wScreen[j]->workspaces[i]->clip) {
870 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
871 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
876 /* go to workspace where we were before restart */
877 if (ws_state) {
878 wWorkspaceForceChange(wScreen[j], ws_state->workspace);
879 free(ws_state);
880 } else {
881 wSessionRestoreLastWorkspace(wScreen[j]);
884 #ifdef KWM_HINTS
885 wKWMSetInitializedHint(wScreen[j]);
886 #endif
889 if (wScreenCount == 0) {
890 wfatal(_("could not manage any screen"));
891 Exit(1);
894 if (!wPreferences.flags.noupdates) {
895 /* setup defaults file polling */
896 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
903 static Bool
904 windowInList(Window window, Window *list, int count)
906 for (; count>=0; count--) {
907 if (window == list[count])
908 return True;
910 return False;
914 *-----------------------------------------------------------------------
915 * manageAllWindows--
916 * Manages all windows in the screen.
918 * Notes:
919 * Called when the wm is being started.
920 * No events can be processed while the windows are being
921 * reparented/managed.
922 *-----------------------------------------------------------------------
924 static void
925 manageAllWindows(WScreen *scr)
927 Window root, parent;
928 Window *children;
929 unsigned int nchildren;
930 unsigned int i, j;
931 WWindow *wwin;
933 XGrabServer(dpy);
934 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
936 scr->flags.startup = 1;
938 /* first remove all icon windows */
939 for (i = 0; i < nchildren; i++) {
940 XWMHints *wmhints;
942 if (children[i]==None)
943 continue;
945 wmhints = XGetWMHints(dpy, children[i]);
946 if (wmhints && (wmhints->flags & IconWindowHint)) {
947 for (j = 0; j < nchildren; j++) {
948 if (children[j] == wmhints->icon_window) {
949 XFree(wmhints);
950 wmhints = NULL;
951 children[j] = None;
952 break;
956 if (wmhints) {
957 XFree(wmhints);
962 for (i = 0; i < nchildren; i++) {
963 if (children[i] == None)
964 continue;
966 #ifdef KWM_HINTS
967 wKWMCheckModule(scr, children[i]);
968 #endif
969 wwin = wManageWindow(scr, children[i]);
970 if (wwin) {
971 /* apply states got from WSavedState */
972 /* shaded + minimized is not restored correctly */
973 if (wwin->flags.shaded) {
974 wwin->flags.shaded = 0;
975 wShadeWindow(wwin);
977 if (wwin->flags.miniaturized
978 && (wwin->transient_for == None
979 || wwin->transient_for == scr->root_win
980 || !windowInList(wwin->transient_for, children,
981 nchildren))) {
983 wwin->flags.skip_next_animation = 1;
984 wwin->flags.miniaturized = 0;
985 wIconifyWindow(wwin);
986 } else {
987 wClientSetState(wwin, NormalState, None);
991 XUngrabServer(dpy);
993 /* hide apps */
994 wwin = scr->focused_window;
995 while (wwin) {
996 if (wwin->flags.hidden) {
997 WApplication *wapp = wApplicationOf(wwin->main_window);
999 if (wapp) {
1000 wwin->flags.hidden = 0;
1001 wHideApplication(wapp);
1002 } else {
1003 wwin->flags.hidden = 0;
1006 wwin = wwin->prev;
1009 XFree(children);
1010 scr->flags.startup = 0;
1011 scr->flags.startup2 = 1;
1013 while (XPending(dpy)) {
1014 XEvent ev;
1015 WMNextEvent(dpy, &ev);
1016 WMHandleEvent(&ev);
1018 wWorkspaceForceChange(scr, 0);
1019 if (!wPreferences.flags.noclip)
1020 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1021 scr->flags.startup2 = 0;