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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
33 #include <sys/signal.h>
37 #include <X11/Xresource.h>
38 #include <X11/Xutil.h>
39 #include <X11/cursorfont.h>
40 #include <X11/Xproto.h>
41 #include <X11/keysym.h>
43 #include <X11/extensions/shape.h>
45 #ifdef KEEP_XKB_LOCK_STATUS
46 #include <X11/XKBlib.h>
49 #include "WindowMaker.h"
58 #include "workspace.h"
63 #include "properties.h"
77 /* Just in case, for weirdo systems */
82 /****** Global Variables ******/
83 extern WPreferences wPreferences
;
84 extern WDDomain
*WDWindowMaker
;
85 extern WDDomain
*WDRootMenu
;
86 extern WDDomain
*WDWindowAttributes
;
87 extern WShortKey wKeyBindings
[WKBD_LAST
];
88 extern int wScreenCount
;
91 extern Bool wShapeSupported
;
92 extern int wShapeEventBase
;
95 #ifdef KEEP_XKB_LOCK_STATUS
96 extern Bool wXkbSupported
;
97 extern int wXkbEventBase
;
101 extern XContext wWinContext
;
102 extern XContext wAppWinContext
;
103 extern XContext wStackContext
;
104 extern XContext wVEdgeContext
;
107 extern Atom _XA_WM_STATE
;
108 extern Atom _XA_WM_CHANGE_STATE
;
109 extern Atom _XA_WM_PROTOCOLS
;
110 extern Atom _XA_WM_TAKE_FOCUS
;
111 extern Atom _XA_WM_DELETE_WINDOW
;
112 extern Atom _XA_WM_SAVE_YOURSELF
;
113 extern Atom _XA_WM_CLIENT_LEADER
;
114 extern Atom _XA_WM_COLORMAP_WINDOWS
;
115 extern Atom _XA_WM_COLORMAP_NOTIFY
;
116 extern Atom _XA_GNUSTEP_WM_ATTR
;
117 extern Atom _XA_WINDOWMAKER_MENU
;
118 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
119 extern Atom _XA_WINDOWMAKER_STATE
;
120 extern Atom _XA_WINDOWMAKER_WM_FUNCTION
;
121 extern Atom _XA_WINDOWMAKER_NOTICEBOARD
;
122 extern Atom _XA_WINDOWMAKER_COMMAND
;
123 extern Atom _XA_WINDOWMAKER_ICON_SIZE
;
124 extern Atom _XA_WINDOWMAKER_ICON_TILE
;
125 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
126 extern Atom _XA_GNUSTEP_TITLEBAR_STATE
;
127 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
130 extern Cursor wCursor
[WCUR_LAST
];
134 extern char WDelayedActionSet
;
137 extern void NotifyDeadProcess(pid_t pid
, unsigned char status
);
140 static WScreen
**wScreen
= NULL
;
141 static unsigned int _NumLockMask
= 0;
142 static unsigned int _ScrollLockMask
= 0;
143 static void manageAllWindows(WScreen
* scr
, int crashed
);
145 static int catchXError(Display
* dpy
, XErrorEvent
* error
)
147 char buffer
[MAXLINE
];
149 /* ignore some errors */
150 if (error
->resourceid
!= None
151 && ((error
->error_code
== BadDrawable
&& error
->request_code
== X_GetGeometry
)
152 || (error
->error_code
== BadMatch
&& (error
->request_code
== X_SetInputFocus
))
153 || (error
->error_code
== BadWindow
)
155 && (error->request_code == X_GetWindowAttributes
156 || error->request_code == X_SetInputFocus
157 || error->request_code == X_ChangeWindowAttributes
158 || error->request_code == X_GetProperty
159 || error->request_code == X_ChangeProperty
160 || error->request_code == X_QueryTree
161 || error->request_code == X_GrabButton
162 || error->request_code == X_UngrabButton
163 || error->request_code == X_SendEvent
164 || error->request_code == X_ConfigureWindow))
166 || (error
->request_code
== X_InstallColormap
))) {
169 FormatXError(dpy
, error
, buffer
, MAXLINE
);
170 wwarning(_("internal X error: %s\n"), buffer
);
175 *----------------------------------------------------------------------
177 * Handle X shutdowns and other stuff.
178 *----------------------------------------------------------------------
180 static int handleXIO(Display
* xio_dpy
)
189 *----------------------------------------------------------------------
191 * Action to be executed after the signal() handler is exited.
192 *----------------------------------------------------------------------
194 static void delayedAction(void *cdata
)
196 if (WDelayedActionSet
== 0)
201 * Make the event dispatcher do whatever it needs to do,
202 * including handling zombie processes, restart and exit
210 *----------------------------------------------------------------------
212 * User generated exit signal handler.
213 *----------------------------------------------------------------------
215 static RETSIGTYPE
handleExitSig(int sig
)
220 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
222 if (sig
== SIGUSR1
) {
223 wwarning("got signal %i - restarting\n", sig
);
224 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART
);
225 } else if (sig
== SIGUSR2
) {
226 wwarning("got signal %i - rereading defaults\n", sig
);
227 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD
);
228 } else if (sig
== SIGTERM
|| sig
== SIGINT
|| sig
== SIGHUP
) {
229 wwarning("got signal %i - exiting...\n", sig
);
230 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT
);
233 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
234 DispatchEvent(NULL
); /* Dispatch events imediately. */
237 /* Dummy signal handler */
238 static void dummyHandler(int sig
)
243 *----------------------------------------------------------------------
245 * general signal handler. Exits the program gently.
246 *----------------------------------------------------------------------
248 static RETSIGTYPE
handleSig(int sig
)
250 wfatal("got signal %i\n", sig
);
252 /* Setting the signal behaviour back to default and then reraising the
253 * signal is a cleaner way to make program exit and core dump than calling
254 * abort(), since it correctly returns from the signal handler and sets
255 * the flags accordingly. -Dan
257 if (sig
== SIGSEGV
|| sig
== SIGFPE
|| sig
== SIGBUS
|| sig
== SIGILL
|| sig
== SIGABRT
) {
258 signal(sig
, SIG_DFL
);
266 static RETSIGTYPE
buryChild(int foo
)
270 int save_errno
= errno
;
274 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
275 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
278 /* If 2 or more kids exit in a small time window, before this handler gets
279 * the chance to get invoked, the SIGCHLD signals will be merged and only
280 * one SIGCHLD signal will be sent to us. We use a while loop to get all
281 * exited child status because we can't count on the number of SIGCHLD
282 * signals to know exactly how many kids have exited. -Dan
284 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0 || (pid
< 0 && errno
== EINTR
)) {
285 NotifyDeadProcess(pid
, WEXITSTATUS(status
));
288 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
293 static void getOffendingModifiers(void)
296 XModifierKeymap
*modmap
;
297 KeyCode nlock
, slock
;
298 static int mask_table
[8] = {
299 ShiftMask
, LockMask
, ControlMask
, Mod1Mask
,
300 Mod2Mask
, Mod3Mask
, Mod4Mask
, Mod5Mask
303 nlock
= XKeysymToKeycode(dpy
, XK_Num_Lock
);
304 slock
= XKeysymToKeycode(dpy
, XK_Scroll_Lock
);
307 * Find out the masks for the NumLock and ScrollLock modifiers,
308 * so that we can bind the grabs for when they are enabled too.
310 modmap
= XGetModifierMapping(dpy
);
312 if (modmap
!= NULL
&& modmap
->max_keypermod
> 0) {
313 for (i
= 0; i
< 8 * modmap
->max_keypermod
; i
++) {
314 if (modmap
->modifiermap
[i
] == nlock
&& nlock
!= 0)
315 _NumLockMask
= mask_table
[i
/ modmap
->max_keypermod
];
316 else if (modmap
->modifiermap
[i
] == slock
&& slock
!= 0)
317 _ScrollLockMask
= mask_table
[i
/ modmap
->max_keypermod
];
322 XFreeModifiermap(modmap
);
327 wHackedGrabKey(int keycode
, unsigned int modifiers
,
328 Window grab_window
, Bool owner_events
, int pointer_mode
, int keyboard_mode
)
330 if (modifiers
== AnyModifier
)
333 /* grab all combinations of the modifier with CapsLock, NumLock and
334 * ScrollLock. How much memory/CPU does such a monstrosity consume
338 XGrabKey(dpy
, keycode
, modifiers
| _NumLockMask
,
339 grab_window
, owner_events
, pointer_mode
, keyboard_mode
);
341 XGrabKey(dpy
, keycode
, modifiers
| _ScrollLockMask
,
342 grab_window
, owner_events
, pointer_mode
, keyboard_mode
);
343 if (_NumLockMask
&& _ScrollLockMask
)
344 XGrabKey(dpy
, keycode
, modifiers
| _NumLockMask
| _ScrollLockMask
,
345 grab_window
, owner_events
, pointer_mode
, keyboard_mode
);
347 XGrabKey(dpy
, keycode
, modifiers
| _NumLockMask
| LockMask
,
348 grab_window
, owner_events
, pointer_mode
, keyboard_mode
);
350 XGrabKey(dpy
, keycode
, modifiers
| _ScrollLockMask
| LockMask
,
351 grab_window
, owner_events
, pointer_mode
, keyboard_mode
);
352 if (_NumLockMask
&& _ScrollLockMask
)
353 XGrabKey(dpy
, keycode
, modifiers
| _NumLockMask
| _ScrollLockMask
| LockMask
,
354 grab_window
, owner_events
, pointer_mode
, keyboard_mode
);
355 /* phew, I guess that's all, right? */
360 wHackedGrabButton(unsigned int button
, unsigned int modifiers
,
361 Window grab_window
, Bool owner_events
,
362 unsigned int event_mask
, int pointer_mode
, int keyboard_mode
, Window confine_to
, Cursor cursor
)
364 XGrabButton(dpy
, button
, modifiers
, grab_window
, owner_events
,
365 event_mask
, pointer_mode
, keyboard_mode
, confine_to
, cursor
);
367 if (modifiers
== AnyModifier
)
370 XGrabButton(dpy
, button
, modifiers
| LockMask
, grab_window
, owner_events
,
371 event_mask
, pointer_mode
, keyboard_mode
, confine_to
, cursor
);
374 /* same as above, but for mouse buttons */
376 XGrabButton(dpy
, button
, modifiers
| _NumLockMask
,
377 grab_window
, owner_events
, event_mask
, pointer_mode
,
378 keyboard_mode
, confine_to
, cursor
);
380 XGrabButton(dpy
, button
, modifiers
| _ScrollLockMask
,
381 grab_window
, owner_events
, event_mask
, pointer_mode
,
382 keyboard_mode
, confine_to
, cursor
);
383 if (_NumLockMask
&& _ScrollLockMask
)
384 XGrabButton(dpy
, button
, modifiers
| _ScrollLockMask
| _NumLockMask
,
385 grab_window
, owner_events
, event_mask
, pointer_mode
,
386 keyboard_mode
, confine_to
, cursor
);
388 XGrabButton(dpy
, button
, modifiers
| _NumLockMask
| LockMask
,
389 grab_window
, owner_events
, event_mask
, pointer_mode
,
390 keyboard_mode
, confine_to
, cursor
);
392 XGrabButton(dpy
, button
, modifiers
| _ScrollLockMask
| LockMask
,
393 grab_window
, owner_events
, event_mask
, pointer_mode
,
394 keyboard_mode
, confine_to
, cursor
);
395 if (_NumLockMask
&& _ScrollLockMask
)
396 XGrabButton(dpy
, button
, modifiers
| _ScrollLockMask
| _NumLockMask
| LockMask
,
397 grab_window
, owner_events
, event_mask
, pointer_mode
,
398 keyboard_mode
, confine_to
, cursor
);
399 #endif /* NUMLOCK_HACK */
402 WScreen
*wScreenWithNumber(int i
)
404 assert(i
< wScreenCount
);
409 WScreen
*wScreenForRootWindow(Window window
)
413 if (wScreenCount
== 1)
417 * Since the number of heads will probably be small (normally 2),
418 * it should be faster to use this than a hash table, because
421 for (i
= 0; i
< wScreenCount
; i
++) {
422 if (wScreen
[i
]->root_win
== window
) {
427 return wScreenForWindow(window
);
430 WScreen
*wScreenSearchForRootWindow(Window window
)
434 if (wScreenCount
== 1)
438 * Since the number of heads will probably be small (normally 2),
439 * it should be faster to use this than a hash table, because
442 for (i
= 0; i
< wScreenCount
; i
++) {
443 if (wScreen
[i
]->root_win
== window
) {
448 return wScreenForWindow(window
);
451 WScreen
*wScreenForWindow(Window window
)
453 XWindowAttributes attr
;
455 if (wScreenCount
== 1)
458 if (XGetWindowAttributes(dpy
, window
, &attr
)) {
459 return wScreenForRootWindow(attr
.root
);
464 static char *atomNames
[] = {
472 "WM_COLORMAP_WINDOWS",
473 "WM_COLORMAP_NOTIFY",
476 "_WINDOWMAKER_STATE",
477 "_WINDOWMAKER_WM_PROTOCOLS",
478 "_WINDOWMAKER_WM_FUNCTION",
479 "_WINDOWMAKER_NOTICEBOARD",
480 "_WINDOWMAKER_COMMAND",
481 "_WINDOWMAKER_ICON_SIZE",
482 "_WINDOWMAKER_ICON_TILE",
484 GNUSTEP_WM_ATTR_NAME
,
485 GNUSTEP_WM_MINIATURIZE_WINDOW
,
486 GNUSTEP_TITLEBAR_STATE
,
488 "WM_IGNORE_FOCUS_EVENTS"
492 *----------------------------------------------------------
494 * starts the window manager and setup global data.
495 * Called from main() at startup.
498 * global data declared in main.c is initialized
499 *----------------------------------------------------------
501 void StartUp(Bool defaultScreenOnly
)
503 struct sigaction sig_action
;
505 Atom atom
[sizeof(atomNames
) / sizeof(char *)];
508 * Ignore CapsLock in modifiers
510 ValidModMask
= 0xff & ~LockMask
;
512 getOffendingModifiers();
514 * Ignore NumLock and ScrollLock too
516 ValidModMask
&= ~(_NumLockMask
| _ScrollLockMask
);
518 memset(&wKeyBindings
, 0, sizeof(wKeyBindings
));
520 wWinContext
= XUniqueContext();
521 wAppWinContext
= XUniqueContext();
522 wStackContext
= XUniqueContext();
523 wVEdgeContext
= XUniqueContext();
525 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False); */
527 #ifdef HAVE_XINTERNATOMS
528 XInternAtoms(dpy
, atomNames
, sizeof(atomNames
) / sizeof(char *), False
, atom
);
533 for (i
= 0; i
< sizeof(atomNames
) / sizeof(char *); i
++) {
534 atom
[i
] = XInternAtom(dpy
, atomNames
[i
], False
);
539 _XA_WM_STATE
= atom
[0];
540 _XA_WM_CHANGE_STATE
= atom
[1];
541 _XA_WM_PROTOCOLS
= atom
[2];
542 _XA_WM_TAKE_FOCUS
= atom
[3];
543 _XA_WM_DELETE_WINDOW
= atom
[4];
544 _XA_WM_SAVE_YOURSELF
= atom
[5];
545 _XA_WM_CLIENT_LEADER
= atom
[6];
546 _XA_WM_COLORMAP_WINDOWS
= atom
[7];
547 _XA_WM_COLORMAP_NOTIFY
= atom
[8];
549 _XA_WINDOWMAKER_MENU
= atom
[9];
550 _XA_WINDOWMAKER_STATE
= atom
[10];
551 _XA_WINDOWMAKER_WM_PROTOCOLS
= atom
[11];
552 _XA_WINDOWMAKER_WM_FUNCTION
= atom
[12];
553 _XA_WINDOWMAKER_NOTICEBOARD
= atom
[13];
554 _XA_WINDOWMAKER_COMMAND
= atom
[14];
555 _XA_WINDOWMAKER_ICON_SIZE
= atom
[15];
556 _XA_WINDOWMAKER_ICON_TILE
= atom
[16];
558 _XA_GNUSTEP_WM_ATTR
= atom
[17];
559 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
= atom
[18];
560 _XA_GNUSTEP_TITLEBAR_STATE
= atom
[19];
562 _XA_WM_IGNORE_FOCUS_EVENTS
= atom
[20];
565 wXDNDInitializeAtoms();
569 wCursor
[WCUR_NORMAL
] = None
; /* inherit from root */
570 wCursor
[WCUR_ROOT
] = XCreateFontCursor(dpy
, XC_left_ptr
);
571 wCursor
[WCUR_ARROW
] = XCreateFontCursor(dpy
, XC_top_left_arrow
);
572 wCursor
[WCUR_MOVE
] = XCreateFontCursor(dpy
, XC_fleur
);
573 wCursor
[WCUR_RESIZE
] = XCreateFontCursor(dpy
, XC_sizing
);
574 wCursor
[WCUR_TOPLEFTRESIZE
] = XCreateFontCursor(dpy
, XC_top_left_corner
);
575 wCursor
[WCUR_TOPRIGHTRESIZE
] = XCreateFontCursor(dpy
, XC_top_right_corner
);
576 wCursor
[WCUR_BOTTOMLEFTRESIZE
] = XCreateFontCursor(dpy
, XC_bottom_left_corner
);
577 wCursor
[WCUR_BOTTOMRIGHTRESIZE
] = XCreateFontCursor(dpy
, XC_bottom_right_corner
);
578 wCursor
[WCUR_VERTICALRESIZE
] = XCreateFontCursor(dpy
, XC_sb_v_double_arrow
);
579 wCursor
[WCUR_HORIZONRESIZE
] = XCreateFontCursor(dpy
, XC_sb_h_double_arrow
);
580 wCursor
[WCUR_WAIT
] = XCreateFontCursor(dpy
, XC_watch
);
581 wCursor
[WCUR_QUESTION
] = XCreateFontCursor(dpy
, XC_question_arrow
);
582 wCursor
[WCUR_TEXT
] = XCreateFontCursor(dpy
, XC_xterm
); /* odd name??? */
583 wCursor
[WCUR_SELECT
] = XCreateFontCursor(dpy
, XC_cross
);
585 Pixmap cur
= XCreatePixmap(dpy
, DefaultRootWindow(dpy
), 16, 16, 1);
586 GC gc
= XCreateGC(dpy
, cur
, 0, NULL
);
588 memset(&black
, 0, sizeof(XColor
));
589 XSetForeground(dpy
, gc
, 0);
590 XFillRectangle(dpy
, cur
, gc
, 0, 0, 16, 16);
592 wCursor
[WCUR_EMPTY
] = XCreatePixmapCursor(dpy
, cur
, cur
, &black
, &black
, 0, 0);
593 XFreePixmap(dpy
, cur
);
597 /* signal handler stuff that gets called when a signal is caught */
598 WMAddPersistentTimerHandler(500, delayedAction
, NULL
);
601 /* emergency exit... */
602 sig_action
.sa_handler
= handleSig
;
603 sigemptyset(&sig_action
.sa_mask
);
605 sig_action
.sa_flags
= SA_RESTART
;
606 sigaction(SIGQUIT
, &sig_action
, NULL
);
607 /* instead of catching these, we let the default handler abort the
608 * program. The new monitor process will take appropriate action
609 * when it detects the crash.
610 sigaction(SIGSEGV, &sig_action, NULL);
611 sigaction(SIGBUS, &sig_action, NULL);
612 sigaction(SIGFPE, &sig_action, NULL);
613 sigaction(SIGABRT, &sig_action, NULL);
616 sig_action
.sa_handler
= handleExitSig
;
618 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
619 * immediately. -Dan */
620 sig_action
.sa_flags
= SA_RESTART
;
621 sigaction(SIGTERM
, &sig_action
, NULL
);
622 sigaction(SIGINT
, &sig_action
, NULL
);
623 sigaction(SIGHUP
, &sig_action
, NULL
);
624 sigaction(SIGUSR1
, &sig_action
, NULL
);
625 sigaction(SIGUSR2
, &sig_action
, NULL
);
627 /* ignore dead pipe */
628 /* Because POSIX mandates that only signal with handlers are reset
629 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
630 * to children. Hence the dummy handler.
631 * Philippe Troin <phil@fifi.org>
633 sig_action
.sa_handler
= &dummyHandler
;
634 sig_action
.sa_flags
= SA_RESTART
;
635 sigaction(SIGPIPE
, &sig_action
, NULL
);
637 /* handle dead children */
638 sig_action
.sa_handler
= buryChild
;
639 sig_action
.sa_flags
= SA_NOCLDSTOP
| SA_RESTART
;
640 sigaction(SIGCHLD
, &sig_action
, NULL
);
642 /* Now we unblock all signals, that may have been blocked by the parent
643 * who exec()-ed us. This can happen for example if Window Maker crashes
644 * and restarts itself or another window manager from the signal handler.
645 * In this case, the new proccess inherits the blocked signal mask and
646 * will no longer react to that signal, until unblocked.
647 * This is because the signal handler of the proccess who crashed (parent)
648 * didn't return, and the signal remained blocked. -Dan
650 sigfillset(&sig_action
.sa_mask
);
651 sigprocmask(SIG_UNBLOCK
, &sig_action
.sa_mask
, NULL
);
653 /* handle X shutdowns a such */
654 XSetIOErrorHandler(handleXIO
);
656 /* set hook for out event dispatcher in WINGs event dispatcher */
657 WMHookEventHandler(DispatchEvent
);
659 /* initialize defaults stuff */
660 WDWindowMaker
= wDefaultsInitDomain("WindowMaker", True
);
661 if (!WDWindowMaker
->dictionary
) {
662 wwarning(_("could not read domain \"%s\" from defaults database"), "WindowMaker");
665 /* read defaults that don't change until a restart and are
666 * screen independent */
667 wReadStaticDefaults(WDWindowMaker
? WDWindowMaker
->dictionary
: NULL
);
669 /* check sanity of some values */
670 if (wPreferences
.icon_size
< 16) {
671 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
672 wPreferences
.icon_size
);
673 wPreferences
.icon_size
= 16;
676 /* init other domains */
677 WDRootMenu
= wDefaultsInitDomain("WMRootMenu", False
);
678 if (!WDRootMenu
->dictionary
) {
679 wwarning(_("could not read domain \"%s\" from defaults database"), "WMRootMenu");
681 wDefaultsMergeGlobalMenus(WDRootMenu
);
683 WDWindowAttributes
= wDefaultsInitDomain("WMWindowAttributes", True
);
684 if (!WDWindowAttributes
->dictionary
) {
685 wwarning(_("could not read domain \"%s\" from defaults database"), "WMWindowAttributes");
688 XSetErrorHandler((XErrorHandler
) catchXError
);
692 wShapeSupported
= XShapeQueryExtension(dpy
, &wShapeEventBase
, &j
);
695 #ifdef KEEP_XKB_LOCK_STATUS
696 wXkbSupported
= XkbQueryExtension(dpy
, NULL
, &wXkbEventBase
, NULL
, NULL
, NULL
);
697 if (wPreferences
.modelock
&& !wXkbSupported
) {
698 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
699 wPreferences
.modelock
= 0;
703 if (defaultScreenOnly
) {
706 max
= ScreenCount(dpy
);
708 wScreen
= wmalloc(sizeof(WScreen
*) * max
);
712 /* manage the screens */
713 for (j
= 0; j
< max
; j
++) {
714 if (defaultScreenOnly
|| max
== 1) {
715 wScreen
[wScreenCount
] = wScreenInit(DefaultScreen(dpy
));
716 if (!wScreen
[wScreenCount
]) {
717 wfatal(_("it seems that there is already a window manager running"));
721 wScreen
[wScreenCount
] = wScreenInit(j
);
722 if (!wScreen
[wScreenCount
]) {
723 wwarning(_("could not manage screen %i"), j
);
730 InitializeSwitchMenu();
732 /* initialize/restore state for the screens */
733 for (j
= 0; j
< wScreenCount
; j
++) {
736 lastDesktop
= wNETWMGetCurrentDesktopFromHint(wScreen
[j
]);
738 wScreenRestoreState(wScreen
[j
]);
740 /* manage all windows that were already here before us */
741 if (!wPreferences
.flags
.nodock
&& wScreen
[j
]->dock
)
742 wScreen
[j
]->last_dock
= wScreen
[j
]->dock
;
744 manageAllWindows(wScreen
[j
], wPreferences
.flags
.restarting
== 2);
746 /* restore saved menus */
747 wMenuRestoreState(wScreen
[j
]);
749 /* If we're not restarting, restore session */
750 if (wPreferences
.flags
.restarting
== 0 && !wPreferences
.flags
.norestore
)
751 wSessionRestoreState(wScreen
[j
]);
753 if (!wPreferences
.flags
.noautolaunch
) {
754 /* auto-launch apps */
755 if (!wPreferences
.flags
.nodock
&& wScreen
[j
]->dock
) {
756 wScreen
[j
]->last_dock
= wScreen
[j
]->dock
;
757 wDockDoAutoLaunch(wScreen
[j
]->dock
, 0);
759 /* auto-launch apps in clip */
760 if (!wPreferences
.flags
.noclip
) {
762 for (i
= 0; i
< wScreen
[j
]->workspace_count
; i
++) {
763 if (wScreen
[j
]->workspaces
[i
]->clip
) {
764 wScreen
[j
]->last_dock
= wScreen
[j
]->workspaces
[i
]->clip
;
765 wDockDoAutoLaunch(wScreen
[j
]->workspaces
[i
]->clip
, i
);
771 /* go to workspace where we were before restart */
772 if (lastDesktop
>= 0) {
773 wWorkspaceForceChange(wScreen
[j
], lastDesktop
);
775 wSessionRestoreLastWorkspace(wScreen
[j
]);
779 if (wScreenCount
== 0) {
780 wfatal(_("could not manage any screen"));
785 if (!wPreferences
.flags
.nopolling
&& !wPreferences
.flags
.noupdates
) {
786 /* setup defaults file polling */
787 WMAddTimerHandler(3000, wDefaultsCheckDomains
, NULL
);
793 static Bool
windowInList(Window window
, Window
* list
, int count
)
795 for (; count
>= 0; count
--) {
796 if (window
== list
[count
])
803 *-----------------------------------------------------------------------
805 * Manages all windows in the screen.
808 * Called when the wm is being started.
809 * No events can be processed while the windows are being
810 * reparented/managed.
811 *-----------------------------------------------------------------------
813 static void manageAllWindows(WScreen
* scr
, int crashRecovery
)
817 unsigned int nchildren
;
822 XQueryTree(dpy
, scr
->root_win
, &root
, &parent
, &children
, &nchildren
);
824 scr
->flags
.startup
= 1;
826 /* first remove all icon windows */
827 for (i
= 0; i
< nchildren
; i
++) {
830 if (children
[i
] == None
)
833 wmhints
= XGetWMHints(dpy
, children
[i
]);
834 if (wmhints
&& (wmhints
->flags
& IconWindowHint
)) {
835 for (j
= 0; j
< nchildren
; j
++) {
836 if (children
[j
] == wmhints
->icon_window
) {
849 for (i
= 0; i
< nchildren
; i
++) {
850 if (children
[i
] == None
)
853 wwin
= wManageWindow(scr
, children
[i
]);
855 /* apply states got from WSavedState */
856 /* shaded + minimized is not restored correctly */
857 if (wwin
->flags
.shaded
) {
858 wwin
->flags
.shaded
= 0;
861 if (wwin
->flags
.miniaturized
862 && (wwin
->transient_for
== None
863 || wwin
->transient_for
== scr
->root_win
864 || !windowInList(wwin
->transient_for
, children
, nchildren
))) {
866 wwin
->flags
.skip_next_animation
= 1;
867 wwin
->flags
.miniaturized
= 0;
868 wIconifyWindow(wwin
);
870 wClientSetState(wwin
, NormalState
, None
);
875 border
= (!HAS_BORDER(wwin
) ? 0 : FRAME_BORDER_WIDTH
);
877 wWindowMove(wwin
, wwin
->frame_x
- border
,
878 wwin
->frame_y
- border
-
879 (wwin
->frame
->titlebar
? wwin
->frame
->titlebar
->height
: 0));
886 wwin
= scr
->focused_window
;
888 if (wwin
->flags
.hidden
) {
889 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
892 wwin
->flags
.hidden
= 0;
893 wHideApplication(wapp
);
895 wwin
->flags
.hidden
= 0;
902 scr
->flags
.startup
= 0;
903 scr
->flags
.startup2
= 1;
905 while (XPending(dpy
)) {
907 WMNextEvent(dpy
, &ev
);
910 wWorkspaceForceChange(scr
, 0);
911 if (!wPreferences
.flags
.noclip
)
912 wDockShowIcons(scr
->workspaces
[scr
->current_workspace
]->clip
);
913 scr
->flags
.startup2
= 0;