missing header
[wmaker-crm.git] / src / startup.c
blob87b64d053e255bd6d97d97b5383c8bfe14a7d1bb
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 * Copyright (c) 1999 Dan Pascu
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <sys/wait.h>
31 #ifdef __FreeBSD__
32 #include <sys/signal.h>
33 #endif
35 #include <X11/Xlib.h>
36 #include <X11/Xutil.h>
37 #include <X11/Intrinsic.h>
38 #include <X11/cursorfont.h>
39 #include <X11/Xproto.h>
40 #include <X11/keysym.h>
41 #ifdef SHAPE
42 #include <X11/extensions/shape.h>
43 #endif
45 #include "WindowMaker.h"
46 #include "GNUstep.h"
47 #include "texture.h"
48 #include "screen.h"
49 #include "window.h"
50 #include "actions.h"
51 #include "client.h"
52 #include "funcs.h"
53 #include "dock.h"
54 #include "workspace.h"
55 #include "keybind.h"
56 #include "framewin.h"
57 #include "session.h"
58 #include "defaults.h"
59 #include "properties.h"
60 #include "dialog.h"
61 #ifdef XDND
62 #include "xdnd.h"
63 #endif
65 #include "xutil.h"
67 #ifdef KWM_HINTS
68 #include "kwm.h"
69 #endif
71 #if 0
72 #ifdef SYS_SIGLIST_DECLARED
73 extern const char * const sys_siglist[];
74 #endif
75 #endif
77 /* for SunOS */
78 #ifndef SA_RESTART
79 # define SA_RESTART 0
80 #endif
82 /* Just in case, for weirdo systems */
83 #ifndef SA_NODEFER
84 # define SA_NODEFER 0
85 #endif
87 /****** Global Variables ******/
89 extern WPreferences wPreferences;
91 extern WDDomain *WDWindowMaker;
92 extern WDDomain *WDRootMenu;
93 extern WDDomain *WDWindowAttributes;
95 extern WShortKey wKeyBindings[WKBD_LAST];
97 extern int wScreenCount;
100 #ifdef SHAPE
101 extern Bool wShapeSupported;
102 extern int wShapeEventBase;
103 #endif
105 #ifdef KEEP_XKB_LOCK_STATUS
106 extern Bool wXkbSupported;
107 extern int wXkbEventBase;
108 #endif
110 /* contexts */
111 extern XContext wWinContext;
112 extern XContext wAppWinContext;
113 extern XContext wStackContext;
115 /* atoms */
116 extern Atom _XA_WM_STATE;
117 extern Atom _XA_WM_CHANGE_STATE;
118 extern Atom _XA_WM_PROTOCOLS;
119 extern Atom _XA_WM_TAKE_FOCUS;
120 extern Atom _XA_WM_DELETE_WINDOW;
121 extern Atom _XA_WM_SAVE_YOURSELF;
122 extern Atom _XA_WM_CLIENT_LEADER;
123 extern Atom _XA_WM_COLORMAP_WINDOWS;
124 extern Atom _XA_WM_COLORMAP_NOTIFY;
126 extern Atom _XA_GNUSTEP_WM_ATTR;
128 extern Atom _XA_WINDOWMAKER_MENU;
129 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
130 extern Atom _XA_WINDOWMAKER_STATE;
131 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
132 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
133 extern Atom _XA_WINDOWMAKER_COMMAND;
134 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
135 extern Atom _XA_WINDOWMAKER_ICON_TILE;
137 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
138 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
140 #ifdef OFFIX_DND
141 extern Atom _XA_DND_PROTOCOL;
142 extern Atom _XA_DND_SELECTION;
143 #endif
146 /* cursors */
147 extern Cursor wCursor[WCUR_LAST];
149 /* special flags */
150 extern char WDelayedActionSet;
152 /***** Local *****/
154 static WScreen **wScreen = NULL;
157 static unsigned int _NumLockMask = 0;
158 static unsigned int _ScrollLockMask = 0;
162 static void manageAllWindows();
164 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
167 static int
168 catchXError(Display *dpy, XErrorEvent *error)
170 char buffer[MAXLINE];
172 /* ignore some errors */
173 if (error->resourceid != None
174 && ((error->error_code == BadDrawable
175 && error->request_code == X_GetGeometry)
176 || (error->error_code == BadMatch
177 && (error->request_code == X_SetInputFocus))
178 || (error->error_code == BadWindow)
180 && (error->request_code == X_GetWindowAttributes
181 || error->request_code == X_SetInputFocus
182 || error->request_code == X_ChangeWindowAttributes
183 || error->request_code == X_GetProperty
184 || error->request_code == X_ChangeProperty
185 || error->request_code == X_QueryTree
186 || error->request_code == X_GrabButton
187 || error->request_code == X_UngrabButton
188 || error->request_code == X_SendEvent
189 || error->request_code == X_ConfigureWindow))
191 || (error->request_code == X_InstallColormap))) {
192 #ifndef DEBUG
194 return 0;
195 #else
196 printf("got X error %x %x %x\n", error->request_code,
197 error->error_code, (unsigned)error->resourceid);
198 return 0;
199 #endif
201 FormatXError(dpy, error, buffer, MAXLINE);
202 wwarning(_("internal X error: %s\n"), buffer);
203 return -1;
208 *----------------------------------------------------------------------
209 * handleXIO-
210 * Handle X shutdowns and other stuff.
211 *----------------------------------------------------------------------
213 static int
214 handleXIO(Display *xio_dpy)
216 dpy = NULL;
217 Exit(0);
218 return 0;
223 *----------------------------------------------------------------------
224 * delayedAction-
225 * Action to be executed after the signal() handler is exited.
226 *----------------------------------------------------------------------
228 static void
229 delayedAction(void *cdata)
231 WDelayedActionSet = 0;
233 * Make the event dispatcher do whatever it needs to do,
234 * including handling zombie processes, restart and exit
235 * signals.
237 DispatchEvent(NULL);
243 *----------------------------------------------------------------------
244 * handleSig--
245 * general signal handler. Exits the program gently.
246 *----------------------------------------------------------------------
248 static RETSIGTYPE
249 handleSig(int sig)
251 static int already_crashed = 0;
252 int dumpcore = 0;
253 #ifndef NO_EMERGENCY_AUTORESTART
254 int crashAction;
255 char *argv[2];
257 argv[1] = NULL;
258 #endif
261 * No functions that potentially do Xlib calls should be called from
262 * here. Xlib calls are not reentrant so the integrity of Xlib is
263 * not guaranteed if a Xlib call is made from a signal handler.
265 if (sig == SIGUSR1) {
266 #ifdef SYS_SIGLIST_DECLARED
267 wwarning(_("got signal %i (%s) - restarting\n"), sig, sys_siglist[sig]);
268 #else
269 wwarning(_("got signal %i - restarting\n"), sig);
270 #endif
272 WCHANGE_STATE(WSTATE_NEED_RESTART);
273 /* setup idle handler, so that this will be handled when
274 * the select() is returned becaused of the signal, even if
275 * there are no X events in the queue */
276 if (!WDelayedActionSet) {
277 WDelayedActionSet = 1;
278 WMAddIdleHandler(delayedAction, NULL);
280 return;
281 } else if (sig == SIGUSR2) {
282 #ifdef SYS_SIGLIST_DECLARED
283 wwarning(_("got signal %i (%s) - rereading defaults\n"), sig, sys_siglist[sig]);
284 #else
285 wwarning(_("got signal %i - rereading defaults\n"), sig);
286 #endif
288 WCHANGE_STATE(WSTATE_NEED_REREAD);
289 /* setup idle handler, so that this will be handled when
290 * the select() is returned becaused of the signal, even if
291 * there are no X events in the queue */
292 if (!WDelayedActionSet) {
293 WDelayedActionSet = 1;
294 WMAddIdleHandler(delayedAction, NULL);
296 return;
297 } else if (sig == SIGTERM || sig == SIGHUP) {
298 #ifdef SYS_SIGLIST_DECLARED
299 wwarning(_("got signal %i (%s) - exiting...\n"), sig, sys_siglist[sig]);
300 #else
301 wwarning(_("got signal %i - exiting...\n"), sig);
302 #endif
304 WCHANGE_STATE(WSTATE_NEED_EXIT);
306 if (!WDelayedActionSet) {
307 WDelayedActionSet = 1;
308 WMAddIdleHandler(delayedAction, NULL);
310 return;
313 #ifdef SYS_SIGLIST_DECLARED
314 wfatal(_("got signal %i (%s)\n"), sig, sys_siglist[sig]);
315 #else
316 wfatal(_("got signal %i\n"), sig);
317 #endif
319 /* Setting the signal behaviour back to default and then reraising the
320 * signal is a cleaner way to make program exit and core dump than calling
321 * abort(), since it correctly returns from the signal handler and sets
322 * the flags accordingly. -Dan
324 if (sig==SIGSEGV || sig==SIGFPE || sig==SIGBUS || sig==SIGILL
325 || sig==SIGABRT) {
326 if (already_crashed) {
327 wfatal(_("crashed while trying to do some post-crash cleanup. Aborting immediatelly."));
328 signal(sig, SIG_DFL);
329 kill(getpid(), sig);
330 return;
332 already_crashed = 1;
334 dumpcore = 1;
336 #ifndef NO_EMERGENCY_AUTORESTART
337 /* Close the X connection and open a new one. This is to avoid messing
338 * Xlib because we call to Xlib functions in a signal handler.
340 if (dpy)
341 XCloseDisplay(dpy);
342 dpy = XOpenDisplay("");
343 if (dpy) {
344 XGrabServer(dpy);
345 crashAction = wShowCrashingDialogPanel(sig);
346 XCloseDisplay(dpy);
347 dpy = NULL;
348 } else {
349 wsyserror(_("cannot open connection for crashing dialog panel. Aborting."));
350 crashAction = WMAbort;
353 if (crashAction == WMAbort) {
354 signal(sig, SIG_DFL);
355 kill(getpid(), sig);
356 return;
359 if (crashAction == WMRestart) {
360 /* we try to restart Window Maker */
361 wmessage(_("trying to restart Window Maker..."));
362 Restart(NULL, False);
363 /* fallback to alternate window manager then */
366 wmessage(_("trying to start alternate window manager..."));
368 Restart(FALLBACK_WINDOWMANAGER, False);
369 Restart("fvwm", False);
370 Restart("twm", False);
371 wfatal(_("failed to start alternate window manager. Aborting."));
372 #else
373 wfatal(_("a fatal error has occured, probably due to a bug. "
374 "Please fill the included BUGFORM and report it."));
375 #endif /* !NO_EMERGENCY_AUTORESTART */
377 signal(sig, SIG_DFL);
378 kill(getpid(), sig);
379 return;
383 wAbort(dumpcore);
387 static RETSIGTYPE
388 ignoreSig(int signal)
390 return;
394 static RETSIGTYPE
395 buryChild(int foo)
397 pid_t pid;
398 int status;
400 /* R.I.P. */
401 /* If 2 or more kids exit in a small time window, before this handler gets
402 * the chance to get invoked, the SIGCHLD signals will be merged and only
403 * one SIGCHLD signal will be sent to us. We use a while loop to get all
404 * exited child status because we can't count on the number of SIGCHLD
405 * signals to know exactly how many kids have exited. -Dan
407 while ((pid=waitpid(-1, &status, WNOHANG))>0 || (pid<0 && errno==EINTR)) {
408 NotifyDeadProcess(pid, WEXITSTATUS(status));
410 * Make sure that the kid will be buried even if there are
411 * no events in the X event queue
413 if (!WDelayedActionSet) {
414 WDelayedActionSet = 1;
415 WMAddIdleHandler(delayedAction, NULL);
421 static int
422 getWorkspaceState(Window root, WWorkspaceState **state)
424 Atom type_ret;
425 int fmt_ret;
426 unsigned long nitems_ret;
427 unsigned long bytes_after_ret;
428 CARD32 *data;
430 if (XGetWindowProperty(dpy, root, _XA_WINDOWMAKER_STATE, 0, 2,
431 True, _XA_WINDOWMAKER_STATE,
432 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
433 (unsigned char **)&data)!=Success || !data)
434 return 0;
436 *state = malloc(sizeof(WWorkspaceState));
437 if (*state) {
438 (*state)->flags = data[0];
439 (*state)->workspace = data[1];
441 XFree(data);
443 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
444 return 1;
445 else
446 return 0;
450 static void
451 getOffendingModifiers()
453 int i;
454 XModifierKeymap *modmap;
455 KeyCode nlock, slock;
456 static int mask_table[8] = {
457 ShiftMask,LockMask,ControlMask,Mod1Mask,
458 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
461 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
462 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
465 * Find out the masks for the NumLock and ScrollLock modifiers,
466 * so that we can bind the grabs for when they are enabled too.
468 modmap = XGetModifierMapping(dpy);
470 if (modmap!=NULL && modmap->max_keypermod>0) {
471 for (i=0; i<8*modmap->max_keypermod; i++) {
472 if (modmap->modifiermap[i]==nlock && nlock!=0)
473 _NumLockMask = mask_table[i/modmap->max_keypermod];
474 else if (modmap->modifiermap[i]==slock && slock!=0)
475 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
479 if (modmap)
480 XFreeModifiermap(modmap);
485 #ifdef NUMLOCK_HACK
486 void
487 wHackedGrabKey(int keycode, unsigned int modifiers,
488 Window grab_window, Bool owner_events, int pointer_mode,
489 int keyboard_mode)
491 if (modifiers == AnyModifier)
492 return;
494 /* grab all combinations of the modifier with CapsLock, NumLock and
495 * ScrollLock. How much memory/CPU does such a monstrosity consume
496 * in the server?
498 if (_NumLockMask)
499 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
500 grab_window, owner_events, pointer_mode, keyboard_mode);
501 if (_ScrollLockMask)
502 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
503 grab_window, owner_events, pointer_mode, keyboard_mode);
504 if (_NumLockMask && _ScrollLockMask)
505 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
506 grab_window, owner_events, pointer_mode, keyboard_mode);
507 if (_NumLockMask)
508 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
509 grab_window, owner_events, pointer_mode, keyboard_mode);
510 if (_ScrollLockMask)
511 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
512 grab_window, owner_events, pointer_mode, keyboard_mode);
513 if (_NumLockMask && _ScrollLockMask)
514 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
515 grab_window, owner_events, pointer_mode, keyboard_mode);
516 /* phew, I guess that's all, right? */
518 #endif
520 void
521 wHackedGrabButton(unsigned int button, unsigned int modifiers,
522 Window grab_window, Bool owner_events,
523 unsigned int event_mask, int pointer_mode,
524 int keyboard_mode, Window confine_to, Cursor cursor)
526 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
527 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
529 if (modifiers==AnyModifier)
530 return;
532 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
533 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
535 #ifdef NUMLOCK_HACK
536 /* same as above, but for mouse buttons */
537 if (_NumLockMask)
538 XGrabButton(dpy, button, modifiers|_NumLockMask,
539 grab_window, owner_events, event_mask, pointer_mode,
540 keyboard_mode, confine_to, cursor);
541 if (_ScrollLockMask)
542 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
543 grab_window, owner_events, event_mask, pointer_mode,
544 keyboard_mode, confine_to, cursor);
545 if (_NumLockMask && _ScrollLockMask)
546 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
547 grab_window, owner_events, event_mask, pointer_mode,
548 keyboard_mode, confine_to, cursor);
549 if (_NumLockMask)
550 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
551 grab_window, owner_events, event_mask, pointer_mode,
552 keyboard_mode, confine_to, cursor);
553 if (_ScrollLockMask)
554 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
555 grab_window, owner_events, event_mask, pointer_mode,
556 keyboard_mode, confine_to, cursor);
557 if (_NumLockMask && _ScrollLockMask)
558 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
559 grab_window, owner_events, event_mask, pointer_mode,
560 keyboard_mode, confine_to, cursor);
561 #endif /* NUMLOCK_HACK */
564 #ifdef notused
565 void
566 wHackedUngrabButton(unsigned int button, unsigned int modifiers,
567 Window grab_window)
569 XUngrabButton(dpy, button, modifiers|_NumLockMask,
570 grab_window);
571 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
572 grab_window);
573 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
574 grab_window);
575 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
576 grab_window);
577 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
578 grab_window);
579 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
580 grab_window);
582 #endif
586 WScreen*
587 wScreenWithNumber(int i)
589 assert(i < wScreenCount);
591 return wScreen[i];
595 WScreen*
596 wScreenForRootWindow(Window window)
598 int i;
600 if (wScreenCount==1)
601 return wScreen[0];
604 * Since the number of heads will probably be small (normally 2),
605 * it should be faster to use this than a hash table, because
606 * of the overhead.
608 for (i=0; i<wScreenCount; i++) {
609 if (wScreen[i]->root_win == window) {
610 return wScreen[i];
614 assert("bad_root_window" && 0);
616 return NULL;
620 WScreen*
621 wScreenSearchForRootWindow(Window window)
623 int i;
625 if (wScreenCount==1)
626 return wScreen[0];
629 * Since the number of heads will probably be small (normally 2),
630 * it should be faster to use this than a hash table, because
631 * of the overhead.
633 for (i=0; i<wScreenCount; i++) {
634 if (wScreen[i]->root_win == window) {
635 return wScreen[i];
639 return NULL;
643 WScreen*
644 wScreenForWindow(Window window)
646 XWindowAttributes attr;
648 if (wScreenCount==1)
649 return wScreen[0];
651 if (XGetWindowAttributes(dpy, window, &attr)) {
652 return wScreenForRootWindow(attr.root);
654 return NULL;
658 static char *atomNames[] = {
659 "WM_STATE",
660 "WM_CHANGE_STATE",
661 "WM_PROTOCOLS",
662 "WM_TAKE_FOCUS",
663 "WM_DELETE_WINDOW",
664 "WM_SAVE_YOURSELF",
665 "WM_CLIENT_LEADER",
666 "WM_COLORMAP_WINDOWS",
667 "WM_COLORMAP_NOTIFY",
668 GNUSTEP_WM_ATTR_NAME,
669 "_WINDOWMAKER_MENU",
670 "_WINDOWMAKER_STATE",
671 "_WINDOWMAKER_WM_PROTOCOLS",
672 GNUSTEP_WM_MINIATURIZE_WINDOW,
673 "_WINDOWMAKER_WM_FUNCTION",
674 "_WINDOWMAKER_NOTICEBOARD",
675 "_WINDOWMAKER_COMMAND",
676 "_WINDOWMAKER_ICON_SIZE",
677 "_WINDOWMAKER_ICON_TILE",
678 GNUSTEP_TITLEBAR_STATE
683 *----------------------------------------------------------
684 * StartUp--
685 * starts the window manager and setup global data.
686 * Called from main() at startup.
688 * Side effects:
689 * global data declared in main.c is initialized
690 *----------------------------------------------------------
692 void
693 StartUp(Bool defaultScreenOnly)
695 WWorkspaceState *ws_state;
696 struct sigaction sig_action;
697 int j, max;
698 Atom atom[sizeof(atomNames)/sizeof(char*)];
701 * Ignore CapsLock in modifiers
703 ValidModMask = 0xff & ~LockMask;
705 getOffendingModifiers();
707 * Ignore NumLock and ScrollLock too
709 ValidModMask &= ~(_NumLockMask|_ScrollLockMask);
712 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
714 wWinContext = XUniqueContext();
715 wAppWinContext = XUniqueContext();
716 wStackContext = XUniqueContext();
718 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
720 #ifdef HAVE_XINTERNATOMS
721 XInternAtoms(dpy, atomNames, sizeof(atomNames)/sizeof(char*),
722 False, atom);
723 #else
726 int i;
727 for (i = 0; i < sizeof(atomNames)/sizeof(char*); i++) {
728 atom[i] = XInternAtom(dpy, atomNames[i], False);
731 #endif
733 _XA_WM_STATE = atom[0];
734 _XA_WM_CHANGE_STATE = atom[1];
735 _XA_WM_PROTOCOLS = atom[2];
736 _XA_WM_TAKE_FOCUS = atom[3];
737 _XA_WM_DELETE_WINDOW = atom[4];
738 _XA_WM_SAVE_YOURSELF = atom[5];
739 _XA_WM_CLIENT_LEADER = atom[6];
740 _XA_WM_COLORMAP_WINDOWS = atom[7];
741 _XA_WM_COLORMAP_NOTIFY = atom[8];
743 _XA_GNUSTEP_WM_ATTR = atom[9];
745 _XA_WINDOWMAKER_MENU = atom[10];
746 _XA_WINDOWMAKER_STATE = atom[11];
748 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[12];
750 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[13];
752 _XA_WINDOWMAKER_WM_FUNCTION = atom[14];
754 _XA_WINDOWMAKER_NOTICEBOARD = atom[15];
756 _XA_WINDOWMAKER_COMMAND = atom[16];
758 _XA_WINDOWMAKER_ICON_SIZE = atom[17];
759 _XA_WINDOWMAKER_ICON_TILE = atom[18];
761 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
763 #ifdef OFFIX_DND
764 _XA_DND_SELECTION = XInternAtom(dpy, "DndSelection", False);
765 _XA_DND_PROTOCOL = XInternAtom(dpy, "DndProtocol", False);
766 #endif
767 #ifdef XDND
768 wXDNDInitializeAtoms();
769 #endif
772 /* cursors */
773 #ifdef DEFINABLE_CURSOR
774 wCursor[WCUR_NORMAL] = None;
775 #else
776 wCursor[WCUR_NORMAL] = XCreateFontCursor(dpy, XC_left_ptr);
777 #endif
778 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
779 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
780 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
781 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
782 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
783 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
784 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
785 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
786 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
787 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
788 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
789 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
790 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
791 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
793 /* emergency exit... */
794 sig_action.sa_handler = handleSig;
795 sigemptyset(&sig_action.sa_mask);
797 /* Here we don't care about SA_RESTART since these signals will close
798 * wmaker anyway.
799 * -Dan */
800 sig_action.sa_flags = 0;
801 sigaction(SIGINT, &sig_action, NULL);
802 sigaction(SIGTERM, &sig_action, NULL);
803 sigaction(SIGHUP, &sig_action, NULL);
804 sigaction(SIGQUIT, &sig_action, NULL);
805 sigaction(SIGSEGV, &sig_action, NULL);
806 sigaction(SIGBUS, &sig_action, NULL);
807 sigaction(SIGFPE, &sig_action, NULL);
808 sigaction(SIGABRT, &sig_action, NULL);
810 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
811 * immediately.
812 * -Dan */
813 sig_action.sa_flags = SA_RESTART;
814 sigaction(SIGUSR1, &sig_action, NULL);
815 sigaction(SIGUSR2, &sig_action, NULL);
817 /* ignore dead pipe */
818 sig_action.sa_handler = ignoreSig;
819 sig_action.sa_flags = SA_RESTART;
820 sigaction(SIGPIPE, &sig_action, NULL);
822 /* handle dead children */
823 sig_action.sa_handler = buryChild;
824 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
825 sigaction(SIGCHLD, &sig_action, NULL);
827 /* Now we unblock all signals, that may have been blocked by the parent
828 * who exec()-ed us. This can happen for example if Window Maker crashes
829 * and restarts itself or another window manager from the signal handler.
830 * In this case, the new proccess inherits the blocked signal mask and
831 * will no longer react to that signal, until unblocked.
832 * This is because the signal handler of the proccess who crashed (parent)
833 * didn't return, and the signal remained blocked. -Dan
835 sigfillset(&sig_action.sa_mask);
836 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
838 /* handle X shutdowns a such */
839 XSetIOErrorHandler(handleXIO);
841 /* set hook for out event dispatcher in WINGs event dispatcher */
842 WMHookEventHandler(DispatchEvent);
844 /* initialize defaults stuff */
845 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
846 if (!WDWindowMaker->dictionary) {
847 wwarning(_("could not read domain \"%s\" from defaults database"),
848 "WindowMaker");
851 /* read defaults that don't change until a restart and are
852 * screen independent */
853 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
855 /* check sanity of some values */
856 if (wPreferences.icon_size < 16) {
857 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
858 wPreferences.icon_size);
859 wPreferences.icon_size = 16;
862 /* init other domains */
863 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
864 if (!WDRootMenu->dictionary) {
865 wwarning(_("could not read domain \"%s\" from defaults database"),
866 "WMRootMenu");
869 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
870 if (!WDWindowAttributes->dictionary) {
871 wwarning(_("could not read domain \"%s\" from defaults database"),
872 "WMWindowAttributes");
875 XSetErrorHandler((XErrorHandler)catchXError);
877 #ifdef SHAPE
878 /* ignore j */
879 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
880 #endif
882 #ifdef KEEP_XKB_LOCK_STATUS
883 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
884 if(wPreferences.modelock && !wXkbSupported) {
885 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
886 wPreferences.modelock = 0;
888 #endif
890 if (defaultScreenOnly) {
891 max = 1;
892 } else {
893 max = ScreenCount(dpy);
895 wScreen = wmalloc(sizeof(WScreen*)*max);
897 wScreenCount = 0;
899 /* manage the screens */
900 for (j = 0; j < max; j++) {
901 if (defaultScreenOnly || max==1) {
902 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
903 if (!wScreen[wScreenCount]) {
904 wfatal(_("it seems that there is already a window manager running"));
905 Exit(1);
907 } else {
908 wScreen[wScreenCount] = wScreenInit(j);
909 if (!wScreen[wScreenCount]) {
910 wwarning(_("could not manage screen %i"), j);
911 continue;
914 wScreenCount++;
917 /* initialize/restore state for the screens */
918 for (j = 0; j < wScreenCount; j++) {
919 /* restore workspace state */
920 if (!getWorkspaceState(wScreen[j]->root_win, &ws_state)) {
921 ws_state = NULL;
924 wScreenRestoreState(wScreen[j]);
926 /* manage all windows that were already here before us */
927 if (!wPreferences.flags.nodock && wScreen[j]->dock)
928 wScreen[j]->last_dock = wScreen[j]->dock;
930 manageAllWindows(wScreen[j]);
932 /* restore saved menus */
933 wMenuRestoreState(wScreen[j]);
935 /* If we're not restarting restore session */
936 if (ws_state == NULL && !wPreferences.flags.norestore)
937 wSessionRestoreState(wScreen[j]);
939 if (!wPreferences.flags.noautolaunch) {
940 /* auto-launch apps */
941 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
942 wScreen[j]->last_dock = wScreen[j]->dock;
943 wDockDoAutoLaunch(wScreen[j]->dock, 0);
945 /* auto-launch apps in clip */
946 if (!wPreferences.flags.noclip) {
947 int i;
948 for(i=0; i<wScreen[j]->workspace_count; i++) {
949 if (wScreen[j]->workspaces[i]->clip) {
950 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
951 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
957 /* go to workspace where we were before restart */
958 if (ws_state) {
959 wWorkspaceForceChange(wScreen[j], ws_state->workspace);
960 wfree(ws_state);
961 } else {
962 wSessionRestoreLastWorkspace(wScreen[j]);
965 #ifdef KWM_HINTS
966 wKWMSetInitializedHint(wScreen[j]);
967 #endif
970 if (wScreenCount == 0) {
971 wfatal(_("could not manage any screen"));
972 Exit(1);
975 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates) {
976 /* setup defaults file polling */
977 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
984 static Bool
985 windowInList(Window window, Window *list, int count)
987 for (; count>=0; count--) {
988 if (window == list[count])
989 return True;
991 return False;
995 *-----------------------------------------------------------------------
996 * manageAllWindows--
997 * Manages all windows in the screen.
999 * Notes:
1000 * Called when the wm is being started.
1001 * No events can be processed while the windows are being
1002 * reparented/managed.
1003 *-----------------------------------------------------------------------
1005 static void
1006 manageAllWindows(WScreen *scr)
1008 Window root, parent;
1009 Window *children;
1010 unsigned int nchildren;
1011 unsigned int i, j;
1012 WWindow *wwin;
1014 XGrabServer(dpy);
1015 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
1017 scr->flags.startup = 1;
1019 /* first remove all icon windows */
1020 for (i = 0; i < nchildren; i++) {
1021 XWMHints *wmhints;
1023 if (children[i]==None)
1024 continue;
1026 wmhints = XGetWMHints(dpy, children[i]);
1027 if (wmhints && (wmhints->flags & IconWindowHint)) {
1028 for (j = 0; j < nchildren; j++) {
1029 if (children[j] == wmhints->icon_window) {
1030 XFree(wmhints);
1031 wmhints = NULL;
1032 children[j] = None;
1033 break;
1037 if (wmhints) {
1038 XFree(wmhints);
1043 for (i = 0; i < nchildren; i++) {
1044 if (children[i] == None)
1045 continue;
1047 #ifdef KWM_HINTS
1048 wKWMCheckModule(scr, children[i]);
1049 #endif
1050 wwin = wManageWindow(scr, children[i]);
1051 if (wwin) {
1052 /* apply states got from WSavedState */
1053 /* shaded + minimized is not restored correctly */
1054 if (wwin->flags.shaded) {
1055 wwin->flags.shaded = 0;
1056 wShadeWindow(wwin);
1058 if (wwin->flags.miniaturized
1059 && (wwin->transient_for == None
1060 || wwin->transient_for == scr->root_win
1061 || !windowInList(wwin->transient_for, children,
1062 nchildren))) {
1064 wwin->flags.skip_next_animation = 1;
1065 wwin->flags.miniaturized = 0;
1066 wIconifyWindow(wwin);
1067 } else {
1068 wClientSetState(wwin, NormalState, None);
1072 XUngrabServer(dpy);
1074 /* hide apps */
1075 wwin = scr->focused_window;
1076 while (wwin) {
1077 if (wwin->flags.hidden) {
1078 WApplication *wapp = wApplicationOf(wwin->main_window);
1080 if (wapp) {
1081 wwin->flags.hidden = 0;
1082 wHideApplication(wapp);
1083 } else {
1084 wwin->flags.hidden = 0;
1087 wwin = wwin->prev;
1090 XFree(children);
1091 scr->flags.startup = 0;
1092 scr->flags.startup2 = 1;
1094 while (XPending(dpy)) {
1095 XEvent ev;
1096 WMNextEvent(dpy, &ev);
1097 WMHandleEvent(&ev);
1099 wWorkspaceForceChange(scr, 0);
1100 if (!wPreferences.flags.noclip)
1101 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1102 scr->flags.startup2 = 0;