Array of workspaces moved to the workspace object in the global namespace
[wmaker-crm.git] / src / startup.c
blob8a229aa1b1a8199e6f8e6b129c5a26b923e98bd6
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.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/Xresource.h>
37 #include <X11/Xutil.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
44 #ifdef KEEP_XKB_LOCK_STATUS
45 #include <X11/XKBlib.h>
46 #endif
47 #ifdef HAVE_XRANDR
48 #include <X11/extensions/Xrandr.h>
49 #endif
51 #include "WindowMaker.h"
52 #include "GNUstep.h"
53 #include "texture.h"
54 #include "screen.h"
55 #include "window.h"
56 #include "actions.h"
57 #include "client.h"
58 #include "main.h"
59 #include "startup.h"
60 #include "dock.h"
61 #include "workspace.h"
62 #include "keybind.h"
63 #include "framewin.h"
64 #include "session.h"
65 #include "defaults.h"
66 #include "properties.h"
67 #include "dialog.h"
68 #include "wmspec.h"
69 #include "event.h"
70 #include "switchmenu.h"
71 #ifdef XDND
72 #include "xdnd.h"
73 #endif
75 #include "xutil.h"
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 ******/
88 extern WDDomain *WDWindowMaker;
89 extern WDDomain *WDRootMenu;
90 extern WDDomain *WDWindowAttributes;
91 extern WShortKey wKeyBindings[WKBD_LAST];
92 extern int wScreenCount;
94 #ifdef SHAPE
95 extern Bool wShapeSupported;
96 extern int wShapeEventBase;
97 #endif
99 #ifdef KEEP_XKB_LOCK_STATUS
100 extern Bool wXkbSupported;
101 extern int wXkbEventBase;
102 #endif
104 #ifndef HAVE_INOTIFY
105 /* special flags */
106 extern char WDelayedActionSet;
107 #endif
109 /***** Local *****/
110 static WScreen **wScreen = NULL;
111 static unsigned int _NumLockMask = 0;
112 static unsigned int _ScrollLockMask = 0;
113 static void manageAllWindows(WScreen * scr, int crashed);
115 static int catchXError(Display * dpy, XErrorEvent * error)
117 char buffer[MAXLINE];
119 /* ignore some errors */
120 if (error->resourceid != None
121 && ((error->error_code == BadDrawable && error->request_code == X_GetGeometry)
122 || (error->error_code == BadMatch && (error->request_code == X_SetInputFocus))
123 || (error->error_code == BadWindow)
125 && (error->request_code == X_GetWindowAttributes
126 || error->request_code == X_SetInputFocus
127 || error->request_code == X_ChangeWindowAttributes
128 || error->request_code == X_GetProperty
129 || error->request_code == X_ChangeProperty
130 || error->request_code == X_QueryTree
131 || error->request_code == X_GrabButton
132 || error->request_code == X_UngrabButton
133 || error->request_code == X_SendEvent
134 || error->request_code == X_ConfigureWindow))
136 || (error->request_code == X_InstallColormap))) {
137 return 0;
139 FormatXError(dpy, error, buffer, MAXLINE);
140 wwarning(_("internal X error: %s"), buffer);
141 return -1;
145 *----------------------------------------------------------------------
146 * handleXIO-
147 * Handle X shutdowns and other stuff.
148 *----------------------------------------------------------------------
150 static int handleXIO(Display * xio_dpy)
152 dpy = NULL;
153 Exit(0);
154 return 0;
157 #ifndef HAVE_INOTIFY
159 *----------------------------------------------------------------------
160 * delayedAction-
161 * Action to be executed after the signal() handler is exited.
162 *----------------------------------------------------------------------
164 static void delayedAction(void *cdata)
166 if (WDelayedActionSet == 0)
167 return;
169 WDelayedActionSet--;
171 * Make the event dispatcher do whatever it needs to do,
172 * including handling zombie processes, restart and exit
173 * signals.
175 DispatchEvent(NULL);
177 #endif
180 *----------------------------------------------------------------------
181 * handleExitSig--
182 * User generated exit signal handler.
183 *----------------------------------------------------------------------
185 static RETSIGTYPE handleExitSig(int sig)
187 sigset_t sigs;
189 sigfillset(&sigs);
190 sigprocmask(SIG_BLOCK, &sigs, NULL);
192 if (sig == SIGUSR1) {
193 wwarning("got signal %i - restarting", sig);
194 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
195 } else if (sig == SIGUSR2) {
196 wwarning("got signal %i - rereading defaults", sig);
197 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
198 } else if (sig == SIGTERM || sig == SIGINT || sig == SIGHUP) {
199 wwarning("got signal %i - exiting...", sig);
200 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
203 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
204 DispatchEvent(NULL); /* Dispatch events imediately. */
207 /* Dummy signal handler */
208 static void dummyHandler(int sig)
213 *----------------------------------------------------------------------
214 * handleSig--
215 * general signal handler. Exits the program gently.
216 *----------------------------------------------------------------------
218 static RETSIGTYPE handleSig(int sig)
220 wfatal("got signal %i", sig);
222 /* Setting the signal behaviour back to default and then reraising the
223 * signal is a cleaner way to make program exit and core dump than calling
224 * abort(), since it correctly returns from the signal handler and sets
225 * the flags accordingly. -Dan
227 if (sig == SIGSEGV || sig == SIGFPE || sig == SIGBUS || sig == SIGILL || sig == SIGABRT) {
228 signal(sig, SIG_DFL);
229 kill(getpid(), sig);
230 return;
233 wAbort(0);
236 static RETSIGTYPE buryChild(int foo)
238 pid_t pid;
239 int status;
240 int save_errno = errno;
241 sigset_t sigs;
243 sigfillset(&sigs);
244 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
245 sigprocmask(SIG_BLOCK, &sigs, NULL);
247 /* R.I.P. */
248 /* If 2 or more kids exit in a small time window, before this handler gets
249 * the chance to get invoked, the SIGCHLD signals will be merged and only
250 * one SIGCHLD signal will be sent to us. We use a while loop to get all
251 * exited child status because we can't count on the number of SIGCHLD
252 * signals to know exactly how many kids have exited. -Dan
254 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || (pid < 0 && errno == EINTR)) {
255 NotifyDeadProcess(pid, WEXITSTATUS(status));
258 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
260 errno = save_errno;
263 static void getOffendingModifiers(void)
265 int i;
266 XModifierKeymap *modmap;
267 KeyCode nlock, slock;
268 static int mask_table[8] = {
269 ShiftMask, LockMask, ControlMask, Mod1Mask,
270 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
273 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
274 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
277 * Find out the masks for the NumLock and ScrollLock modifiers,
278 * so that we can bind the grabs for when they are enabled too.
280 modmap = XGetModifierMapping(dpy);
282 if (modmap != NULL && modmap->max_keypermod > 0) {
283 for (i = 0; i < 8 * modmap->max_keypermod; i++) {
284 if (modmap->modifiermap[i] == nlock && nlock != 0)
285 _NumLockMask = mask_table[i / modmap->max_keypermod];
286 else if (modmap->modifiermap[i] == slock && slock != 0)
287 _ScrollLockMask = mask_table[i / modmap->max_keypermod];
291 if (modmap)
292 XFreeModifiermap(modmap);
295 #ifdef NUMLOCK_HACK
296 void
297 wHackedGrabKey(int keycode, unsigned int modifiers,
298 Window grab_window, Bool owner_events, int pointer_mode, int keyboard_mode)
300 if (modifiers == AnyModifier)
301 return;
303 /* grab all combinations of the modifier with CapsLock, NumLock and
304 * ScrollLock. How much memory/CPU does such a monstrosity consume
305 * in the server?
307 if (_NumLockMask)
308 XGrabKey(dpy, keycode, modifiers | _NumLockMask,
309 grab_window, owner_events, pointer_mode, keyboard_mode);
310 if (_ScrollLockMask)
311 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask,
312 grab_window, owner_events, pointer_mode, keyboard_mode);
313 if (_NumLockMask && _ScrollLockMask)
314 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask,
315 grab_window, owner_events, pointer_mode, keyboard_mode);
316 if (_NumLockMask)
317 XGrabKey(dpy, keycode, modifiers | _NumLockMask | LockMask,
318 grab_window, owner_events, pointer_mode, keyboard_mode);
319 if (_ScrollLockMask)
320 XGrabKey(dpy, keycode, modifiers | _ScrollLockMask | LockMask,
321 grab_window, owner_events, pointer_mode, keyboard_mode);
322 if (_NumLockMask && _ScrollLockMask)
323 XGrabKey(dpy, keycode, modifiers | _NumLockMask | _ScrollLockMask | LockMask,
324 grab_window, owner_events, pointer_mode, keyboard_mode);
325 /* phew, I guess that's all, right? */
327 #endif
329 void
330 wHackedGrabButton(unsigned int button, unsigned int modifiers,
331 Window grab_window, Bool owner_events,
332 unsigned int event_mask, int pointer_mode, int keyboard_mode, Window confine_to, Cursor cursor)
334 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
335 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
337 if (modifiers == AnyModifier)
338 return;
340 XGrabButton(dpy, button, modifiers | LockMask, grab_window, owner_events,
341 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
343 #ifdef NUMLOCK_HACK
344 /* same as above, but for mouse buttons */
345 if (_NumLockMask)
346 XGrabButton(dpy, button, modifiers | _NumLockMask,
347 grab_window, owner_events, event_mask, pointer_mode,
348 keyboard_mode, confine_to, cursor);
349 if (_ScrollLockMask)
350 XGrabButton(dpy, button, modifiers | _ScrollLockMask,
351 grab_window, owner_events, event_mask, pointer_mode,
352 keyboard_mode, confine_to, cursor);
353 if (_NumLockMask && _ScrollLockMask)
354 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask,
355 grab_window, owner_events, event_mask, pointer_mode,
356 keyboard_mode, confine_to, cursor);
357 if (_NumLockMask)
358 XGrabButton(dpy, button, modifiers | _NumLockMask | LockMask,
359 grab_window, owner_events, event_mask, pointer_mode,
360 keyboard_mode, confine_to, cursor);
361 if (_ScrollLockMask)
362 XGrabButton(dpy, button, modifiers | _ScrollLockMask | LockMask,
363 grab_window, owner_events, event_mask, pointer_mode,
364 keyboard_mode, confine_to, cursor);
365 if (_NumLockMask && _ScrollLockMask)
366 XGrabButton(dpy, button, modifiers | _ScrollLockMask | _NumLockMask | LockMask,
367 grab_window, owner_events, event_mask, pointer_mode,
368 keyboard_mode, confine_to, cursor);
369 #endif /* NUMLOCK_HACK */
372 WScreen *wScreenWithNumber(int i)
374 assert(i < wScreenCount);
376 return wScreen[i];
379 WScreen *wScreenForRootWindow(Window window)
381 int i;
383 if (wScreenCount == 1)
384 return wScreen[0];
386 /* Since the number of heads will probably be small (normally 2),
387 * it should be faster to use this than a hash table, because
388 * of the overhead. */
389 for (i = 0; i < wScreenCount; i++)
390 if (wScreen[i]->root_win == window)
391 return wScreen[i];
393 return wScreenForWindow(window);
396 WScreen *wScreenForWindow(Window window)
398 XWindowAttributes attr;
400 if (wScreenCount == 1)
401 return wScreen[0];
403 if (XGetWindowAttributes(dpy, window, &attr))
404 return wScreenForRootWindow(attr.root);
406 return NULL;
409 static char *atomNames[] = {
410 "WM_STATE",
411 "WM_CHANGE_STATE",
412 "WM_PROTOCOLS",
413 "WM_TAKE_FOCUS",
414 "WM_DELETE_WINDOW",
415 "WM_SAVE_YOURSELF",
416 "WM_CLIENT_LEADER",
417 "WM_COLORMAP_WINDOWS",
418 "WM_COLORMAP_NOTIFY",
420 "_WINDOWMAKER_MENU",
421 "_WINDOWMAKER_STATE",
422 "_WINDOWMAKER_WM_PROTOCOLS",
423 "_WINDOWMAKER_WM_FUNCTION",
424 "_WINDOWMAKER_NOTICEBOARD",
425 "_WINDOWMAKER_COMMAND",
426 "_WINDOWMAKER_ICON_SIZE",
427 "_WINDOWMAKER_ICON_TILE",
429 GNUSTEP_WM_ATTR_NAME,
430 GNUSTEP_WM_MINIATURIZE_WINDOW,
431 GNUSTEP_TITLEBAR_STATE,
433 "WM_IGNORE_FOCUS_EVENTS"
437 *----------------------------------------------------------
438 * StartUp--
439 * starts the window manager and setup global data.
440 * Called from main() at startup.
442 * Side effects:
443 * global data declared in main.c is initialized
444 *----------------------------------------------------------
446 void StartUp(Bool defaultScreenOnly)
448 struct sigaction sig_action;
449 int i, j, max;
450 char **formats;
451 #ifdef HAVE_XRANDR
452 int dummy;
453 #endif
454 Atom atom[sizeof(atomNames) / sizeof(atomNames[0])];
457 * Ignore CapsLock in modifiers
459 ValidModMask = 0xff & ~LockMask;
461 getOffendingModifiers();
463 * Ignore NumLock and ScrollLock too
465 ValidModMask &= ~(_NumLockMask | _ScrollLockMask);
467 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
469 w_global.context.client_win = XUniqueContext();
470 w_global.context.app_win = XUniqueContext();
471 w_global.context.stack = XUniqueContext();
473 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False); */
475 #ifdef HAVE_XINTERNATOMS
476 XInternAtoms(dpy, atomNames, sizeof(atomNames) / sizeof(atomNames[0]), False, atom);
477 #else
480 int i;
481 for (i = 0; i < sizeof(atomNames) / sizeof(atomNames[0]); i++)
482 atom[i] = XInternAtom(dpy, atomNames[i], False);
484 #endif
486 w_global.atom.wm.state = atom[0];
487 w_global.atom.wm.change_state = atom[1];
488 w_global.atom.wm.protocols = atom[2];
489 w_global.atom.wm.take_focus = atom[3];
490 w_global.atom.wm.delete_window = atom[4];
491 w_global.atom.wm.save_yourself = atom[5];
492 w_global.atom.wm.client_leader = atom[6];
493 w_global.atom.wm.colormap_windows = atom[7];
494 w_global.atom.wm.colormap_notify = atom[8];
496 w_global.atom.wmaker.menu = atom[9];
497 w_global.atom.wmaker.state = atom[10];
498 w_global.atom.wmaker.wm_protocols = atom[11];
499 w_global.atom.wmaker.wm_function = atom[12];
500 w_global.atom.wmaker.noticeboard = atom[13];
501 w_global.atom.wmaker.command = atom[14];
502 w_global.atom.wmaker.icon_size = atom[15];
503 w_global.atom.wmaker.icon_tile = atom[16];
505 w_global.atom.gnustep.wm_attr = atom[17];
506 w_global.atom.gnustep.wm_miniaturize_window = atom[18];
507 w_global.atom.gnustep.titlebar_state = atom[19];
509 w_global.atom.wm.ignore_focus_events = atom[20];
511 #ifdef XDND
512 wXDNDInitializeAtoms();
513 #endif
515 /* cursors */
516 wPreferences.cursor[WCUR_NORMAL] = None; /* inherit from root */
517 wPreferences.cursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
518 wPreferences.cursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
519 wPreferences.cursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
520 wPreferences.cursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
521 wPreferences.cursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
522 wPreferences.cursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
523 wPreferences.cursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
524 wPreferences.cursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
525 wPreferences.cursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
526 wPreferences.cursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
527 wPreferences.cursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
528 wPreferences.cursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
529 wPreferences.cursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name??? */
530 wPreferences.cursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
532 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
533 GC gc = XCreateGC(dpy, cur, 0, NULL);
534 XColor black;
535 memset(&black, 0, sizeof(XColor));
536 XSetForeground(dpy, gc, 0);
537 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
538 XFreeGC(dpy, gc);
539 wPreferences.cursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
540 XFreePixmap(dpy, cur);
543 #ifndef HAVE_INOTIFY
544 /* signal handler stuff that gets called when a signal is caught */
545 WMAddPersistentTimerHandler(500, delayedAction, NULL);
546 #endif
548 /* emergency exit... */
549 sig_action.sa_handler = handleSig;
550 sigemptyset(&sig_action.sa_mask);
552 sig_action.sa_flags = SA_RESTART;
553 sigaction(SIGQUIT, &sig_action, NULL);
554 /* instead of catching these, we let the default handler abort the
555 * program. The new monitor process will take appropriate action
556 * when it detects the crash.
557 sigaction(SIGSEGV, &sig_action, NULL);
558 sigaction(SIGBUS, &sig_action, NULL);
559 sigaction(SIGFPE, &sig_action, NULL);
560 sigaction(SIGABRT, &sig_action, NULL);
563 sig_action.sa_handler = handleExitSig;
565 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
566 * immediately. -Dan */
567 sig_action.sa_flags = SA_RESTART;
568 sigaction(SIGTERM, &sig_action, NULL);
569 sigaction(SIGINT, &sig_action, NULL);
570 sigaction(SIGHUP, &sig_action, NULL);
571 sigaction(SIGUSR1, &sig_action, NULL);
572 sigaction(SIGUSR2, &sig_action, NULL);
574 /* ignore dead pipe */
575 /* Because POSIX mandates that only signal with handlers are reset
576 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
577 * to children. Hence the dummy handler.
578 * Philippe Troin <phil@fifi.org>
580 sig_action.sa_handler = &dummyHandler;
581 sig_action.sa_flags = SA_RESTART;
582 sigaction(SIGPIPE, &sig_action, NULL);
584 /* handle dead children */
585 sig_action.sa_handler = buryChild;
586 sig_action.sa_flags = SA_NOCLDSTOP | SA_RESTART;
587 sigaction(SIGCHLD, &sig_action, NULL);
589 /* Now we unblock all signals, that may have been blocked by the parent
590 * who exec()-ed us. This can happen for example if Window Maker crashes
591 * and restarts itself or another window manager from the signal handler.
592 * In this case, the new proccess inherits the blocked signal mask and
593 * will no longer react to that signal, until unblocked.
594 * This is because the signal handler of the proccess who crashed (parent)
595 * didn't return, and the signal remained blocked. -Dan
597 sigfillset(&sig_action.sa_mask);
598 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
600 /* handle X shutdowns a such */
601 XSetIOErrorHandler(handleXIO);
603 /* set hook for out event dispatcher in WINGs event dispatcher */
604 WMHookEventHandler(DispatchEvent);
606 /* initialize defaults stuff */
607 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
608 if (!WDWindowMaker->dictionary)
609 wwarning(_("could not read domain \"%s\" from defaults database"), "WindowMaker");
611 /* read defaults that don't change until a restart and are
612 * screen independent */
613 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
615 /* check sanity of some values */
616 if (wPreferences.icon_size < 16) {
617 wwarning(_("icon size is configured to %i, but it's too small. Using 16 instead"),
618 wPreferences.icon_size);
619 wPreferences.icon_size = 16;
622 /* init other domains */
623 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
624 if (!WDRootMenu->dictionary)
625 wwarning(_("could not read domain \"%s\" from defaults database"), "WMRootMenu");
627 wDefaultsMergeGlobalMenus(WDRootMenu);
629 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
630 if (!WDWindowAttributes->dictionary)
631 wwarning(_("could not read domain \"%s\" from defaults database"), "WMWindowAttributes");
633 XSetErrorHandler((XErrorHandler) catchXError);
635 #ifdef SHAPE
636 /* ignore j */
637 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
638 #endif
640 #ifdef HAVE_XRANDR
641 has_randr = XRRQueryExtension(dpy, &randr_event_base, &dummy);
642 #endif
644 #ifdef KEEP_XKB_LOCK_STATUS
645 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
646 if (wPreferences.modelock && !wXkbSupported) {
647 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
648 wPreferences.modelock = 0;
650 #endif
652 if (defaultScreenOnly)
653 max = 1;
654 else
655 max = ScreenCount(dpy);
657 wScreen = wmalloc(sizeof(WScreen *) * max);
659 wScreenCount = 0;
661 /* Check if TIFF images are supported */
662 formats = RSupportedFileFormats();
663 if (formats) {
664 for (i = 0; formats[i] != NULL; i++) {
665 if (strcmp(formats[i], "TIFF") == 0) {
666 wPreferences.supports_tiff = 1;
667 break;
672 /* manage the screens */
673 for (j = 0; j < max; j++) {
674 if (defaultScreenOnly || max == 1) {
675 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
676 if (!wScreen[wScreenCount]) {
677 wfatal(_("it seems that there is already a window manager running"));
678 Exit(1);
680 } else {
681 wScreen[wScreenCount] = wScreenInit(j);
682 if (!wScreen[wScreenCount]) {
683 wwarning(_("could not manage screen %i"), j);
684 continue;
687 wScreenCount++;
690 InitializeSwitchMenu();
692 /* initialize/restore state for the screens */
693 for (j = 0; j < wScreenCount; j++) {
694 int lastDesktop;
696 lastDesktop = wNETWMGetCurrentDesktopFromHint(wScreen[j]);
698 wScreenRestoreState(wScreen[j]);
700 /* manage all windows that were already here before us */
701 if (!wPreferences.flags.nodock && wScreen[j]->dock)
702 wScreen[j]->last_dock = wScreen[j]->dock;
704 manageAllWindows(wScreen[j], wPreferences.flags.restarting == 2);
706 /* restore saved menus */
707 wMenuRestoreState(wScreen[j]);
709 /* If we're not restarting, restore session */
710 if (wPreferences.flags.restarting == 0 && !wPreferences.flags.norestore)
711 wSessionRestoreState(wScreen[j]);
713 if (!wPreferences.flags.noautolaunch) {
714 /* auto-launch apps */
715 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
716 wScreen[j]->last_dock = wScreen[j]->dock;
717 wDockDoAutoLaunch(wScreen[j]->dock, 0);
719 /* auto-launch apps in clip */
720 if (!wPreferences.flags.noclip) {
721 int i;
722 for (i = 0; i < w_global.workspace.count; i++) {
723 if (w_global.workspace.array[i]->clip) {
724 wScreen[j]->last_dock = w_global.workspace.array[i]->clip;
725 wDockDoAutoLaunch(w_global.workspace.array[i]->clip, i);
729 /* auto-launch apps in drawers */
730 if (!wPreferences.flags.nodrawer) {
731 WDrawerChain *dc;
732 for (dc = wScreen[j]->drawers; dc; dc = dc->next) {
733 wScreen[j]->last_dock = dc->adrawer;
734 wDockDoAutoLaunch(dc->adrawer, 0);
739 /* go to workspace where we were before restart */
740 if (lastDesktop >= 0)
741 wWorkspaceForceChange(wScreen[j], lastDesktop);
742 else
743 wSessionRestoreLastWorkspace(wScreen[j]);
746 if (wScreenCount == 0) {
747 wfatal(_("could not manage any screen"));
748 Exit(1);
751 #ifndef HAVE_INOTIFY
752 /* setup defaults file polling */
753 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates)
754 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
755 #endif
759 static Bool windowInList(Window window, Window * list, int count)
761 for (; count >= 0; count--) {
762 if (window == list[count])
763 return True;
765 return False;
769 *-----------------------------------------------------------------------
770 * manageAllWindows--
771 * Manages all windows in the screen.
773 * Notes:
774 * Called when the wm is being started.
775 * No events can be processed while the windows are being
776 * reparented/managed.
777 *-----------------------------------------------------------------------
779 static void manageAllWindows(WScreen * scr, int crashRecovery)
781 Window root, parent;
782 Window *children;
783 unsigned int nchildren;
784 unsigned int i, j;
785 WWindow *wwin;
787 XGrabServer(dpy);
788 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
790 scr->flags.startup = 1;
792 /* first remove all icon windows */
793 for (i = 0; i < nchildren; i++) {
794 XWMHints *wmhints;
796 if (children[i] == None)
797 continue;
799 wmhints = XGetWMHints(dpy, children[i]);
800 if (wmhints && (wmhints->flags & IconWindowHint)) {
801 for (j = 0; j < nchildren; j++) {
802 if (children[j] == wmhints->icon_window) {
803 XFree(wmhints);
804 wmhints = NULL;
805 children[j] = None;
806 break;
810 if (wmhints) {
811 XFree(wmhints);
815 for (i = 0; i < nchildren; i++) {
816 if (children[i] == None)
817 continue;
819 wwin = wManageWindow(scr, children[i]);
820 if (wwin) {
821 /* apply states got from WSavedState */
822 /* shaded + minimized is not restored correctly */
823 if (wwin->flags.shaded) {
824 wwin->flags.shaded = 0;
825 wShadeWindow(wwin);
827 if (wwin->flags.miniaturized
828 && (wwin->transient_for == None
829 || wwin->transient_for == scr->root_win
830 || !windowInList(wwin->transient_for, children, nchildren))) {
832 wwin->flags.skip_next_animation = 1;
833 wwin->flags.miniaturized = 0;
834 wIconifyWindow(wwin);
835 } else {
836 wClientSetState(wwin, NormalState, None);
838 if (crashRecovery) {
839 int border;
841 border = (!HAS_BORDER(wwin) ? 0 : scr->frame_border_width);
843 wWindowMove(wwin, wwin->frame_x - border,
844 wwin->frame_y - border -
845 (wwin->frame->titlebar ? wwin->frame->titlebar->height : 0));
849 XUngrabServer(dpy);
851 /* hide apps */
852 wwin = scr->focused_window;
853 while (wwin) {
854 if (wwin->flags.hidden) {
855 WApplication *wapp = wApplicationOf(wwin->main_window);
857 if (wapp) {
858 wwin->flags.hidden = 0;
859 wHideApplication(wapp);
860 } else {
861 wwin->flags.hidden = 0;
864 wwin = wwin->prev;
867 XFree(children);
868 scr->flags.startup = 0;
869 scr->flags.startup2 = 1;
871 while (XPending(dpy)) {
872 XEvent ev;
873 WMNextEvent(dpy, &ev);
874 WMHandleEvent(&ev);
876 w_global.workspace.last_used = 0;
877 wWorkspaceForceChange(scr, 0);
878 if (!wPreferences.flags.noclip)
879 wDockShowIcons(w_global.workspace.array[w_global.workspace.current]->clip);
880 scr->flags.startup2 = 0;