fixed bugs in configure.in, removed
[wmaker-crm.git] / src / startup.c
blob2fb1d689d0b3d26b30539b0b3518cd8f5cb36029
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 == SIGTERM || sig == SIGHUP) {
282 #ifdef SYS_SIGLIST_DECLARED
283 wwarning(_("got signal %i (%s) - exiting...\n"), sig, sys_siglist[sig]);
284 #else
285 wwarning(_("got signal %i - exiting...\n"), sig);
286 #endif
288 WCHANGE_STATE(WSTATE_NEED_EXIT);
290 if (!WDelayedActionSet) {
291 WDelayedActionSet = 1;
292 WMAddIdleHandler(delayedAction, NULL);
294 return;
297 #ifdef SYS_SIGLIST_DECLARED
298 wfatal(_("got signal %i (%s)\n"), sig, sys_siglist[sig]);
299 #else
300 wfatal(_("got signal %i\n"), sig);
301 #endif
303 /* Setting the signal behaviour back to default and then reraising the
304 * signal is a cleaner way to make program exit and core dump than calling
305 * abort(), since it correctly returns from the signal handler and sets
306 * the flags accordingly. -Dan
308 if (sig==SIGSEGV || sig==SIGFPE || sig==SIGBUS || sig==SIGILL
309 || sig==SIGABRT) {
310 if (already_crashed) {
311 wfatal(_("crashed while trying to do some post-crash cleanup. Aborting immediatelly."));
312 signal(sig, SIG_DFL);
313 kill(getpid(), sig);
314 return;
316 already_crashed = 1;
318 dumpcore = 1;
320 #ifndef NO_EMERGENCY_AUTORESTART
321 /* Close the X connection and open a new one. This is to avoid messing
322 * Xlib because we call to Xlib functions in a signal handler.
324 if (dpy)
325 XCloseDisplay(dpy);
326 dpy = XOpenDisplay("");
327 if (dpy) {
328 XGrabServer(dpy);
329 crashAction = wShowCrashingDialogPanel(sig);
330 XCloseDisplay(dpy);
331 dpy = NULL;
332 } else {
333 wsyserror(_("cannot open connection for crashing dialog panel. Aborting."));
334 crashAction = WMAbort;
337 if (crashAction == WMAbort) {
338 signal(sig, SIG_DFL);
339 kill(getpid(), sig);
340 return;
343 if (crashAction == WMRestart) {
344 /* we try to restart Window Maker */
345 wmessage(_("trying to restart Window Maker..."));
346 Restart(NULL, False);
347 /* fallback to alternate window manager then */
350 wmessage(_("trying to start alternate window manager..."));
352 Restart(FALLBACK_WINDOWMANAGER, False);
353 Restart("fvwm", False);
354 Restart("twm", False);
355 wfatal(_("failed to start alternate window manager. Aborting."));
356 #else
357 wfatal(_("a fatal error has occured, probably due to a bug. "
358 "Please fill the included BUGFORM and report it."));
359 #endif /* !NO_EMERGENCY_AUTORESTART */
361 signal(sig, SIG_DFL);
362 kill(getpid(), sig);
363 return;
367 wAbort(dumpcore);
371 static RETSIGTYPE
372 ignoreSig(int signal)
374 return;
378 static RETSIGTYPE
379 buryChild(int foo)
381 pid_t pid;
382 int status;
384 /* R.I.P. */
385 /* If 2 or more kids exit in a small time window, before this handler gets
386 * the chance to get invoked, the SIGCHLD signals will be merged and only
387 * one SIGCHLD signal will be sent to us. We use a while loop to get all
388 * exited child status because we can't count on the number of SIGCHLD
389 * signals to know exactly how many kids have exited. -Dan
391 while ((pid=waitpid(-1, &status, WNOHANG))>0 || (pid<0 && errno==EINTR)) {
392 NotifyDeadProcess(pid, WEXITSTATUS(status));
394 * Make sure that the kid will be buried even if there are
395 * no events in the X event queue
397 if (!WDelayedActionSet) {
398 WDelayedActionSet = 1;
399 WMAddIdleHandler(delayedAction, NULL);
405 static int
406 getWorkspaceState(Window root, WWorkspaceState **state)
408 Atom type_ret;
409 int fmt_ret;
410 unsigned long nitems_ret;
411 unsigned long bytes_after_ret;
412 CARD32 *data;
414 if (XGetWindowProperty(dpy, root, _XA_WINDOWMAKER_STATE, 0, 2,
415 True, _XA_WINDOWMAKER_STATE,
416 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
417 (unsigned char **)&data)!=Success || !data)
418 return 0;
420 *state = malloc(sizeof(WWorkspaceState));
421 if (*state) {
422 (*state)->flags = data[0];
423 (*state)->workspace = data[1];
425 XFree(data);
427 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
428 return 1;
429 else
430 return 0;
434 static void
435 getOffendingModifiers()
437 int i;
438 XModifierKeymap *modmap;
439 KeyCode nlock, slock;
440 static int mask_table[8] = {
441 ShiftMask,LockMask,ControlMask,Mod1Mask,
442 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
445 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
446 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
449 * Find out the masks for the NumLock and ScrollLock modifiers,
450 * so that we can bind the grabs for when they are enabled too.
452 modmap = XGetModifierMapping(dpy);
454 if (modmap!=NULL && modmap->max_keypermod>0) {
455 for (i=0; i<8*modmap->max_keypermod; i++) {
456 if (modmap->modifiermap[i]==nlock && nlock!=0)
457 _NumLockMask = mask_table[i/modmap->max_keypermod];
458 else if (modmap->modifiermap[i]==slock && slock!=0)
459 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
463 if (modmap)
464 XFreeModifiermap(modmap);
469 #ifdef NUMLOCK_HACK
470 void
471 wHackedGrabKey(int keycode, unsigned int modifiers,
472 Window grab_window, Bool owner_events, int pointer_mode,
473 int keyboard_mode)
475 if (modifiers == AnyModifier)
476 return;
478 /* grab all combinations of the modifier with CapsLock, NumLock and
479 * ScrollLock. How much memory/CPU does such a monstrosity consume
480 * in the server?
482 if (_NumLockMask)
483 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
484 grab_window, owner_events, pointer_mode, keyboard_mode);
485 if (_ScrollLockMask)
486 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
487 grab_window, owner_events, pointer_mode, keyboard_mode);
488 if (_NumLockMask && _ScrollLockMask)
489 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
490 grab_window, owner_events, pointer_mode, keyboard_mode);
491 if (_NumLockMask)
492 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
493 grab_window, owner_events, pointer_mode, keyboard_mode);
494 if (_ScrollLockMask)
495 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
496 grab_window, owner_events, pointer_mode, keyboard_mode);
497 if (_NumLockMask && _ScrollLockMask)
498 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
499 grab_window, owner_events, pointer_mode, keyboard_mode);
500 /* phew, I guess that's all, right? */
502 #endif
504 void
505 wHackedGrabButton(unsigned int button, unsigned int modifiers,
506 Window grab_window, Bool owner_events,
507 unsigned int event_mask, int pointer_mode,
508 int keyboard_mode, Window confine_to, Cursor cursor)
510 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
511 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
513 if (modifiers==AnyModifier)
514 return;
516 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
517 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
519 #ifdef NUMLOCK_HACK
520 /* same as above, but for mouse buttons */
521 if (_NumLockMask)
522 XGrabButton(dpy, button, modifiers|_NumLockMask,
523 grab_window, owner_events, event_mask, pointer_mode,
524 keyboard_mode, confine_to, cursor);
525 if (_ScrollLockMask)
526 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
527 grab_window, owner_events, event_mask, pointer_mode,
528 keyboard_mode, confine_to, cursor);
529 if (_NumLockMask && _ScrollLockMask)
530 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
531 grab_window, owner_events, event_mask, pointer_mode,
532 keyboard_mode, confine_to, cursor);
533 if (_NumLockMask)
534 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
535 grab_window, owner_events, event_mask, pointer_mode,
536 keyboard_mode, confine_to, cursor);
537 if (_ScrollLockMask)
538 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
539 grab_window, owner_events, event_mask, pointer_mode,
540 keyboard_mode, confine_to, cursor);
541 if (_NumLockMask && _ScrollLockMask)
542 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
543 grab_window, owner_events, event_mask, pointer_mode,
544 keyboard_mode, confine_to, cursor);
545 #endif /* NUMLOCK_HACK */
548 #ifdef notused
549 void
550 wHackedUngrabButton(unsigned int button, unsigned int modifiers,
551 Window grab_window)
553 XUngrabButton(dpy, button, modifiers|_NumLockMask,
554 grab_window);
555 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
556 grab_window);
557 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
558 grab_window);
559 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
560 grab_window);
561 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
562 grab_window);
563 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
564 grab_window);
566 #endif
570 WScreen*
571 wScreenWithNumber(int i)
573 assert(i < wScreenCount);
575 return wScreen[i];
579 WScreen*
580 wScreenForRootWindow(Window window)
582 int i;
584 if (wScreenCount==1)
585 return wScreen[0];
588 * Since the number of heads will probably be small (normally 2),
589 * it should be faster to use this than a hash table, because
590 * of the overhead.
592 for (i=0; i<wScreenCount; i++) {
593 if (wScreen[i]->root_win == window) {
594 return wScreen[i];
598 assert("bad_root_window" && 0);
600 return NULL;
604 WScreen*
605 wScreenSearchForRootWindow(Window window)
607 int i;
609 if (wScreenCount==1)
610 return wScreen[0];
613 * Since the number of heads will probably be small (normally 2),
614 * it should be faster to use this than a hash table, because
615 * of the overhead.
617 for (i=0; i<wScreenCount; i++) {
618 if (wScreen[i]->root_win == window) {
619 return wScreen[i];
623 return NULL;
627 WScreen*
628 wScreenForWindow(Window window)
630 XWindowAttributes attr;
632 if (wScreenCount==1)
633 return wScreen[0];
635 if (XGetWindowAttributes(dpy, window, &attr)) {
636 return wScreenForRootWindow(attr.root);
638 return NULL;
642 static char *atomNames[] = {
643 "WM_STATE",
644 "WM_CHANGE_STATE",
645 "WM_PROTOCOLS",
646 "WM_TAKE_FOCUS",
647 "WM_DELETE_WINDOW",
648 "WM_SAVE_YOURSELF",
649 "WM_CLIENT_LEADER",
650 "WM_COLORMAP_WINDOWS",
651 "WM_COLORMAP_NOTIFY",
652 GNUSTEP_WM_ATTR_NAME,
653 "_WINDOWMAKER_MENU",
654 "_WINDOWMAKER_STATE",
655 "_WINDOWMAKER_WM_PROTOCOLS",
656 GNUSTEP_WM_MINIATURIZE_WINDOW,
657 "_WINDOWMAKER_WM_FUNCTION",
658 "_WINDOWMAKER_NOTICEBOARD",
659 "_WINDOWMAKER_COMMAND",
660 "_WINDOWMAKER_ICON_SIZE",
661 "_WINDOWMAKER_ICON_TILE",
662 GNUSTEP_TITLEBAR_STATE
667 *----------------------------------------------------------
668 * StartUp--
669 * starts the window manager and setup global data.
670 * Called from main() at startup.
672 * Side effects:
673 * global data declared in main.c is initialized
674 *----------------------------------------------------------
676 void
677 StartUp(Bool defaultScreenOnly)
679 WWorkspaceState *ws_state;
680 struct sigaction sig_action;
681 int j, max;
682 Atom atom[sizeof(atomNames)/sizeof(char*)];
685 * Ignore CapsLock in modifiers
687 ValidModMask = 0xff & ~LockMask;
689 getOffendingModifiers();
691 * Ignore NumLock and ScrollLock too
693 ValidModMask &= ~(_NumLockMask|_ScrollLockMask);
696 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
698 wWinContext = XUniqueContext();
699 wAppWinContext = XUniqueContext();
700 wStackContext = XUniqueContext();
702 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
704 #ifdef HAVE_XINTERNATOMS
705 XInternAtoms(dpy, atomNames, sizeof(atomNames)/sizeof(char*),
706 False, atom);
707 #else
710 int i;
711 for (i = 0; i < sizeof(atomNames)/sizeof(char*); i++) {
712 atom[i] = XInternAtom(dpy, atomNames[i], False);
715 #endif
717 _XA_WM_STATE = atom[0];
718 _XA_WM_CHANGE_STATE = atom[1];
719 _XA_WM_PROTOCOLS = atom[2];
720 _XA_WM_TAKE_FOCUS = atom[3];
721 _XA_WM_DELETE_WINDOW = atom[4];
722 _XA_WM_SAVE_YOURSELF = atom[5];
723 _XA_WM_CLIENT_LEADER = atom[6];
724 _XA_WM_COLORMAP_WINDOWS = atom[7];
725 _XA_WM_COLORMAP_NOTIFY = atom[8];
727 _XA_GNUSTEP_WM_ATTR = atom[9];
729 _XA_WINDOWMAKER_MENU = atom[10];
730 _XA_WINDOWMAKER_STATE = atom[11];
732 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[12];
734 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[13];
736 _XA_WINDOWMAKER_WM_FUNCTION = atom[14];
738 _XA_WINDOWMAKER_NOTICEBOARD = atom[15];
740 _XA_WINDOWMAKER_COMMAND = atom[16];
742 _XA_WINDOWMAKER_ICON_SIZE = atom[17];
743 _XA_WINDOWMAKER_ICON_TILE = atom[18];
745 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
747 #ifdef OFFIX_DND
748 _XA_DND_SELECTION = XInternAtom(dpy, "DndSelection", False);
749 _XA_DND_PROTOCOL = XInternAtom(dpy, "DndProtocol", False);
750 #endif
751 #ifdef XDND
752 wXDNDInitializeAtoms();
753 #endif
756 /* cursors */
757 #ifdef DEFINABLE_CURSOR
758 wCursor[WCUR_NORMAL] = None;
759 #else
760 wCursor[WCUR_NORMAL] = XCreateFontCursor(dpy, XC_left_ptr);
761 #endif
762 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
763 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
764 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
765 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
766 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
767 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
768 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
769 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
770 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
771 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
772 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
773 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
774 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
775 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
777 /* emergency exit... */
778 sig_action.sa_handler = handleSig;
779 sigemptyset(&sig_action.sa_mask);
781 /* Here we don't care about SA_RESTART since these signals will close
782 * wmaker anyway.
783 * -Dan */
784 sig_action.sa_flags = 0;
785 sigaction(SIGINT, &sig_action, NULL);
786 sigaction(SIGTERM, &sig_action, NULL);
787 sigaction(SIGHUP, &sig_action, NULL);
788 sigaction(SIGQUIT, &sig_action, NULL);
789 sigaction(SIGSEGV, &sig_action, NULL);
790 sigaction(SIGBUS, &sig_action, NULL);
791 sigaction(SIGFPE, &sig_action, NULL);
792 sigaction(SIGABRT, &sig_action, NULL);
794 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
795 * immediately.
796 * -Dan */
797 sig_action.sa_flags = SA_RESTART;
798 sigaction(SIGUSR1, &sig_action, NULL);
800 /* ignore dead pipe */
801 sig_action.sa_handler = ignoreSig;
802 sig_action.sa_flags = SA_RESTART;
803 sigaction(SIGPIPE, &sig_action, NULL);
805 /* handle dead children */
806 sig_action.sa_handler = buryChild;
807 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
808 sigaction(SIGCHLD, &sig_action, NULL);
810 /* Now we unblock all signals, that may have been blocked by the parent
811 * who exec()-ed us. This can happen for example if Window Maker crashes
812 * and restarts itself or another window manager from the signal handler.
813 * In this case, the new proccess inherits the blocked signal mask and
814 * will no longer react to that signal, until unblocked.
815 * This is because the signal handler of the proccess who crashed (parent)
816 * didn't return, and the signal remained blocked. -Dan
818 sigfillset(&sig_action.sa_mask);
819 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
821 /* handle X shutdowns a such */
822 XSetIOErrorHandler(handleXIO);
824 /* set hook for out event dispatcher in WINGs event dispatcher */
825 WMHookEventHandler(DispatchEvent);
827 /* initialize defaults stuff */
828 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
829 if (!WDWindowMaker->dictionary) {
830 wwarning(_("could not read domain \"%s\" from defaults database"),
831 "WindowMaker");
834 /* read defaults that don't change until a restart and are
835 * screen independent */
836 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
838 /* check sanity of some values */
839 if (wPreferences.icon_size < 16) {
840 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
841 wPreferences.icon_size);
842 wPreferences.icon_size = 16;
845 /* init other domains */
846 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
847 if (!WDRootMenu->dictionary) {
848 wwarning(_("could not read domain \"%s\" from defaults database"),
849 "WMRootMenu");
852 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
853 if (!WDWindowAttributes->dictionary) {
854 wwarning(_("could not read domain \"%s\" from defaults database"),
855 "WMWindowAttributes");
858 XSetErrorHandler((XErrorHandler)catchXError);
860 #ifdef SHAPE
861 /* ignore j */
862 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
863 #endif
865 #ifdef KEEP_XKB_LOCK_STATUS
866 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
867 if(wPreferences.modelock && !wXkbSupported) {
868 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
869 wPreferences.modelock = 0;
871 #endif
873 if (defaultScreenOnly) {
874 max = 1;
875 } else {
876 max = ScreenCount(dpy);
878 wScreen = wmalloc(sizeof(WScreen*)*max);
880 wScreenCount = 0;
882 /* manage the screens */
883 for (j = 0; j < max; j++) {
884 if (defaultScreenOnly || max==1) {
885 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
886 if (!wScreen[wScreenCount]) {
887 wfatal(_("it seems that there is already a window manager running"));
888 Exit(1);
890 } else {
891 wScreen[wScreenCount] = wScreenInit(j);
892 if (!wScreen[wScreenCount]) {
893 wwarning(_("could not manage screen %i"), j);
894 continue;
897 wScreenCount++;
900 /* initialize/restore state for the screens */
901 for (j = 0; j < wScreenCount; j++) {
902 /* restore workspace state */
903 if (!getWorkspaceState(wScreen[j]->root_win, &ws_state)) {
904 ws_state = NULL;
907 wScreenRestoreState(wScreen[j]);
909 /* manage all windows that were already here before us */
910 if (!wPreferences.flags.nodock && wScreen[j]->dock)
911 wScreen[j]->last_dock = wScreen[j]->dock;
913 manageAllWindows(wScreen[j]);
915 /* restore saved menus */
916 wMenuRestoreState(wScreen[j]);
918 /* If we're not restarting restore session */
919 if (ws_state == NULL && !wPreferences.flags.norestore)
920 wSessionRestoreState(wScreen[j]);
922 if (!wPreferences.flags.noautolaunch) {
923 /* auto-launch apps */
924 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
925 wScreen[j]->last_dock = wScreen[j]->dock;
926 wDockDoAutoLaunch(wScreen[j]->dock, 0);
928 /* auto-launch apps in clip */
929 if (!wPreferences.flags.noclip) {
930 int i;
931 for(i=0; i<wScreen[j]->workspace_count; i++) {
932 if (wScreen[j]->workspaces[i]->clip) {
933 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
934 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
940 /* go to workspace where we were before restart */
941 if (ws_state) {
942 wWorkspaceForceChange(wScreen[j], ws_state->workspace);
943 wfree(ws_state);
944 } else {
945 wSessionRestoreLastWorkspace(wScreen[j]);
948 #ifdef KWM_HINTS
949 wKWMSetInitializedHint(wScreen[j]);
950 #endif
953 if (wScreenCount == 0) {
954 wfatal(_("could not manage any screen"));
955 Exit(1);
958 if (!wPreferences.flags.noupdates) {
959 /* setup defaults file polling */
960 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
967 static Bool
968 windowInList(Window window, Window *list, int count)
970 for (; count>=0; count--) {
971 if (window == list[count])
972 return True;
974 return False;
978 *-----------------------------------------------------------------------
979 * manageAllWindows--
980 * Manages all windows in the screen.
982 * Notes:
983 * Called when the wm is being started.
984 * No events can be processed while the windows are being
985 * reparented/managed.
986 *-----------------------------------------------------------------------
988 static void
989 manageAllWindows(WScreen *scr)
991 Window root, parent;
992 Window *children;
993 unsigned int nchildren;
994 unsigned int i, j;
995 WWindow *wwin;
997 XGrabServer(dpy);
998 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
1000 scr->flags.startup = 1;
1002 /* first remove all icon windows */
1003 for (i = 0; i < nchildren; i++) {
1004 XWMHints *wmhints;
1006 if (children[i]==None)
1007 continue;
1009 wmhints = XGetWMHints(dpy, children[i]);
1010 if (wmhints && (wmhints->flags & IconWindowHint)) {
1011 for (j = 0; j < nchildren; j++) {
1012 if (children[j] == wmhints->icon_window) {
1013 XFree(wmhints);
1014 wmhints = NULL;
1015 children[j] = None;
1016 break;
1020 if (wmhints) {
1021 XFree(wmhints);
1026 for (i = 0; i < nchildren; i++) {
1027 if (children[i] == None)
1028 continue;
1030 #ifdef KWM_HINTS
1031 wKWMCheckModule(scr, children[i]);
1032 #endif
1033 wwin = wManageWindow(scr, children[i]);
1034 if (wwin) {
1035 /* apply states got from WSavedState */
1036 /* shaded + minimized is not restored correctly */
1037 if (wwin->flags.shaded) {
1038 wwin->flags.shaded = 0;
1039 wShadeWindow(wwin);
1041 if (wwin->flags.miniaturized
1042 && (wwin->transient_for == None
1043 || wwin->transient_for == scr->root_win
1044 || !windowInList(wwin->transient_for, children,
1045 nchildren))) {
1047 wwin->flags.skip_next_animation = 1;
1048 wwin->flags.miniaturized = 0;
1049 wIconifyWindow(wwin);
1050 } else {
1051 wClientSetState(wwin, NormalState, None);
1055 XUngrabServer(dpy);
1057 /* hide apps */
1058 wwin = scr->focused_window;
1059 while (wwin) {
1060 if (wwin->flags.hidden) {
1061 WApplication *wapp = wApplicationOf(wwin->main_window);
1063 if (wapp) {
1064 wwin->flags.hidden = 0;
1065 wHideApplication(wapp);
1066 } else {
1067 wwin->flags.hidden = 0;
1070 wwin = wwin->prev;
1073 XFree(children);
1074 scr->flags.startup = 0;
1075 scr->flags.startup2 = 1;
1077 while (XPending(dpy)) {
1078 XEvent ev;
1079 WMNextEvent(dpy, &ev);
1080 WMHandleEvent(&ev);
1082 wWorkspaceForceChange(scr, 0);
1083 if (!wPreferences.flags.noclip)
1084 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1085 scr->flags.startup2 = 0;