fixed timerhandler cpu eater
[wmaker-crm.git] / src / startup.c
blobbb779af2eda7aac82e041c44f6c3da2578dfd8e2
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 if (WDelayedActionSet == 0) {
232 return;
234 WDelayedActionSet--;
236 * Make the event dispatcher do whatever it needs to do,
237 * including handling zombie processes, restart and exit
238 * signals.
240 DispatchEvent(NULL);
245 *----------------------------------------------------------------------
246 * handleExitSig--
247 * User generated exit signal handler.
248 *----------------------------------------------------------------------
250 static RETSIGTYPE
251 handleExitSig(int sig)
253 sigset_t sigs;
255 sigfillset(&sigs);
256 sigprocmask(SIG_BLOCK, &sigs, NULL);
258 if (sig == SIGUSR1) {
259 #ifdef SYS_SIGLIST_DECLARED
260 wwarning(_("got signal %i (%s) - restarting\n"), sig, sys_siglist[sig]);
261 #else
262 wwarning(_("got signal %i - restarting\n"), sig);
263 #endif
265 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
266 /* setup idle handler, so that this will be handled when
267 * the select() is returned becaused of the signal, even if
268 * there are no X events in the queue */
269 WDelayedActionSet++;
270 } else if (sig == SIGUSR2) {
271 #ifdef SYS_SIGLIST_DECLARED
272 wwarning(_("got signal %i (%s) - rereading defaults\n"), sig, sys_siglist[sig]);
273 #else
274 wwarning(_("got signal %i - rereading defaults\n"), sig);
275 #endif
277 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
278 /* setup idle handler, so that this will be handled when
279 * the select() is returned becaused of the signal, even if
280 * there are no X events in the queue */
281 WDelayedActionSet++;
282 } else if (sig == SIGINT || sig == SIGHUP) {
283 #ifdef SYS_SIGLIST_DECLARED
284 wwarning(_("got signal %i (%s) - exiting...\n"), sig, sys_siglist[sig]);
285 #else
286 wwarning(_("got signal %i - exiting...\n"), sig);
287 #endif
289 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
291 WDelayedActionSet++;
294 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
298 *----------------------------------------------------------------------
299 * handleSig--
300 * general signal handler. Exits the program gently.
301 *----------------------------------------------------------------------
303 static RETSIGTYPE
304 handleSig(int sig)
306 static int already_crashed = 0;
307 int dumpcore = 0;
308 #ifndef NO_EMERGENCY_AUTORESTART
309 int crashAction;
310 char *argv[2];
312 argv[1] = NULL;
313 #endif
316 * No functions that potentially do Xlib calls should be called from
317 * here. Xlib calls are not reentrant so the integrity of Xlib is
318 * not guaranteed if a Xlib call is made from a signal handler.
321 #ifdef SYS_SIGLIST_DECLARED
322 wfatal(_("got signal %i (%s)\n"), sig, sys_siglist[sig]);
323 #else
324 wfatal(_("got signal %i\n"), sig);
325 #endif
327 /* Setting the signal behaviour back to default and then reraising the
328 * signal is a cleaner way to make program exit and core dump than calling
329 * abort(), since it correctly returns from the signal handler and sets
330 * the flags accordingly. -Dan
332 if (sig==SIGSEGV || sig==SIGFPE || sig==SIGBUS || sig==SIGILL
333 || sig==SIGABRT) {
334 if (already_crashed) {
335 wfatal(_("crashed while trying to do some post-crash cleanup. Aborting immediatelly."));
336 signal(sig, SIG_DFL);
337 kill(getpid(), sig);
338 exit(1);
340 already_crashed = 1;
342 dumpcore = 1;
345 * Yeah, we shouldn't do this, but it's already crashed anyway :P
348 #ifndef NO_EMERGENCY_AUTORESTART
349 /* Close the X connection and open a new one. This is to avoid messing
350 * Xlib because we call to Xlib functions in a signal handler.
352 if (dpy)
353 XCloseDisplay(dpy);
354 dpy = XOpenDisplay("");
355 if (dpy) {
356 XGrabServer(dpy);
357 crashAction = wShowCrashingDialogPanel(sig);
358 XCloseDisplay(dpy);
359 dpy = NULL;
360 } else {
361 wsyserror(_("cannot open connection for crashing dialog panel. Aborting."));
362 crashAction = WMAbort;
365 if (crashAction == WMAbort) {
366 signal(sig, SIG_DFL);
367 kill(getpid(), sig);
368 return;
371 if (crashAction == WMRestart) {
372 /* we try to restart Window Maker */
373 wmessage(_("trying to restart Window Maker..."));
374 Restart(NULL, False);
375 /* fallback to alternate window manager then */
378 wmessage(_("trying to start alternate window manager..."));
380 Restart(FALLBACK_WINDOWMANAGER, False);
381 Restart("fvwm", False);
382 Restart("twm", False);
383 wfatal(_("failed to start alternate window manager. Aborting."));
384 #else
385 wfatal(_("a fatal error has occured, probably due to a bug. "
386 "Please fill the included BUGFORM and report it."));
387 #endif /* !NO_EMERGENCY_AUTORESTART */
389 signal(sig, SIG_DFL);
390 kill(getpid(), sig);
391 return;
395 wAbort(dumpcore);
399 static RETSIGTYPE
400 buryChild(int foo)
402 pid_t pid;
403 int status;
404 int save_errno = errno;
405 sigset_t sigs;
407 sigfillset(&sigs);
408 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
409 sigprocmask(SIG_BLOCK, &sigs, NULL);
411 /* R.I.P. */
412 /* If 2 or more kids exit in a small time window, before this handler gets
413 * the chance to get invoked, the SIGCHLD signals will be merged and only
414 * one SIGCHLD signal will be sent to us. We use a while loop to get all
415 * exited child status because we can't count on the number of SIGCHLD
416 * signals to know exactly how many kids have exited. -Dan
418 while ((pid=waitpid(-1, &status, WNOHANG))>0 || (pid<0 && errno==EINTR)) {
419 NotifyDeadProcess(pid, WEXITSTATUS(status));
422 WDelayedActionSet++;
424 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
426 errno = save_errno;
430 static int
431 getWorkspaceState(Window root, WWorkspaceState **state)
433 Atom type_ret;
434 int fmt_ret;
435 unsigned long nitems_ret;
436 unsigned long bytes_after_ret;
437 CARD32 *data;
439 if (XGetWindowProperty(dpy, root, _XA_WINDOWMAKER_STATE, 0, 2,
440 True, _XA_WINDOWMAKER_STATE,
441 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
442 (unsigned char **)&data)!=Success || !data)
443 return 0;
445 *state = malloc(sizeof(WWorkspaceState));
446 if (*state) {
447 (*state)->flags = data[0];
448 (*state)->workspace = data[1];
450 XFree(data);
452 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
453 return 1;
454 else
455 return 0;
459 static void
460 getOffendingModifiers()
462 int i;
463 XModifierKeymap *modmap;
464 KeyCode nlock, slock;
465 static int mask_table[8] = {
466 ShiftMask,LockMask,ControlMask,Mod1Mask,
467 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
470 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
471 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
474 * Find out the masks for the NumLock and ScrollLock modifiers,
475 * so that we can bind the grabs for when they are enabled too.
477 modmap = XGetModifierMapping(dpy);
479 if (modmap!=NULL && modmap->max_keypermod>0) {
480 for (i=0; i<8*modmap->max_keypermod; i++) {
481 if (modmap->modifiermap[i]==nlock && nlock!=0)
482 _NumLockMask = mask_table[i/modmap->max_keypermod];
483 else if (modmap->modifiermap[i]==slock && slock!=0)
484 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
488 if (modmap)
489 XFreeModifiermap(modmap);
494 #ifdef NUMLOCK_HACK
495 void
496 wHackedGrabKey(int keycode, unsigned int modifiers,
497 Window grab_window, Bool owner_events, int pointer_mode,
498 int keyboard_mode)
500 if (modifiers == AnyModifier)
501 return;
503 /* grab all combinations of the modifier with CapsLock, NumLock and
504 * ScrollLock. How much memory/CPU does such a monstrosity consume
505 * in the server?
507 if (_NumLockMask)
508 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
509 grab_window, owner_events, pointer_mode, keyboard_mode);
510 if (_ScrollLockMask)
511 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
512 grab_window, owner_events, pointer_mode, keyboard_mode);
513 if (_NumLockMask && _ScrollLockMask)
514 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
515 grab_window, owner_events, pointer_mode, keyboard_mode);
516 if (_NumLockMask)
517 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
518 grab_window, owner_events, pointer_mode, keyboard_mode);
519 if (_ScrollLockMask)
520 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
521 grab_window, owner_events, pointer_mode, keyboard_mode);
522 if (_NumLockMask && _ScrollLockMask)
523 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
524 grab_window, owner_events, pointer_mode, keyboard_mode);
525 /* phew, I guess that's all, right? */
527 #endif
529 void
530 wHackedGrabButton(unsigned int button, unsigned int modifiers,
531 Window grab_window, Bool owner_events,
532 unsigned int event_mask, int pointer_mode,
533 int keyboard_mode, Window confine_to, Cursor cursor)
535 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
536 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
538 if (modifiers==AnyModifier)
539 return;
541 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
542 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
544 #ifdef NUMLOCK_HACK
545 /* same as above, but for mouse buttons */
546 if (_NumLockMask)
547 XGrabButton(dpy, button, modifiers|_NumLockMask,
548 grab_window, owner_events, event_mask, pointer_mode,
549 keyboard_mode, confine_to, cursor);
550 if (_ScrollLockMask)
551 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
552 grab_window, owner_events, event_mask, pointer_mode,
553 keyboard_mode, confine_to, cursor);
554 if (_NumLockMask && _ScrollLockMask)
555 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
556 grab_window, owner_events, event_mask, pointer_mode,
557 keyboard_mode, confine_to, cursor);
558 if (_NumLockMask)
559 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
560 grab_window, owner_events, event_mask, pointer_mode,
561 keyboard_mode, confine_to, cursor);
562 if (_ScrollLockMask)
563 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
564 grab_window, owner_events, event_mask, pointer_mode,
565 keyboard_mode, confine_to, cursor);
566 if (_NumLockMask && _ScrollLockMask)
567 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
568 grab_window, owner_events, event_mask, pointer_mode,
569 keyboard_mode, confine_to, cursor);
570 #endif /* NUMLOCK_HACK */
573 #ifdef notused
574 void
575 wHackedUngrabButton(unsigned int button, unsigned int modifiers,
576 Window grab_window)
578 XUngrabButton(dpy, button, modifiers|_NumLockMask,
579 grab_window);
580 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
581 grab_window);
582 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
583 grab_window);
584 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
585 grab_window);
586 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
587 grab_window);
588 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
589 grab_window);
591 #endif
595 WScreen*
596 wScreenWithNumber(int i)
598 assert(i < wScreenCount);
600 return wScreen[i];
604 WScreen*
605 wScreenForRootWindow(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 assert("bad_root_window" && 0);
625 return NULL;
629 WScreen*
630 wScreenSearchForRootWindow(Window window)
632 int i;
634 if (wScreenCount==1)
635 return wScreen[0];
638 * Since the number of heads will probably be small (normally 2),
639 * it should be faster to use this than a hash table, because
640 * of the overhead.
642 for (i=0; i<wScreenCount; i++) {
643 if (wScreen[i]->root_win == window) {
644 return wScreen[i];
648 return NULL;
652 WScreen*
653 wScreenForWindow(Window window)
655 XWindowAttributes attr;
657 if (wScreenCount==1)
658 return wScreen[0];
660 if (XGetWindowAttributes(dpy, window, &attr)) {
661 return wScreenForRootWindow(attr.root);
663 return NULL;
667 static char *atomNames[] = {
668 "WM_STATE",
669 "WM_CHANGE_STATE",
670 "WM_PROTOCOLS",
671 "WM_TAKE_FOCUS",
672 "WM_DELETE_WINDOW",
673 "WM_SAVE_YOURSELF",
674 "WM_CLIENT_LEADER",
675 "WM_COLORMAP_WINDOWS",
676 "WM_COLORMAP_NOTIFY",
677 GNUSTEP_WM_ATTR_NAME,
678 "_WINDOWMAKER_MENU",
679 "_WINDOWMAKER_STATE",
680 "_WINDOWMAKER_WM_PROTOCOLS",
681 GNUSTEP_WM_MINIATURIZE_WINDOW,
682 "_WINDOWMAKER_WM_FUNCTION",
683 "_WINDOWMAKER_NOTICEBOARD",
684 "_WINDOWMAKER_COMMAND",
685 "_WINDOWMAKER_ICON_SIZE",
686 "_WINDOWMAKER_ICON_TILE",
687 GNUSTEP_TITLEBAR_STATE
692 *----------------------------------------------------------
693 * StartUp--
694 * starts the window manager and setup global data.
695 * Called from main() at startup.
697 * Side effects:
698 * global data declared in main.c is initialized
699 *----------------------------------------------------------
701 void
702 StartUp(Bool defaultScreenOnly)
704 WWorkspaceState *ws_state;
705 struct sigaction sig_action;
706 int j, max;
707 Atom atom[sizeof(atomNames)/sizeof(char*)];
710 * Ignore CapsLock in modifiers
712 ValidModMask = 0xff & ~LockMask;
714 getOffendingModifiers();
716 * Ignore NumLock and ScrollLock too
718 ValidModMask &= ~(_NumLockMask|_ScrollLockMask);
721 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
723 wWinContext = XUniqueContext();
724 wAppWinContext = XUniqueContext();
725 wStackContext = XUniqueContext();
727 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
729 #ifdef HAVE_XINTERNATOMS
730 XInternAtoms(dpy, atomNames, sizeof(atomNames)/sizeof(char*),
731 False, atom);
732 #else
735 int i;
736 for (i = 0; i < sizeof(atomNames)/sizeof(char*); i++) {
737 atom[i] = XInternAtom(dpy, atomNames[i], False);
740 #endif
742 _XA_WM_STATE = atom[0];
743 _XA_WM_CHANGE_STATE = atom[1];
744 _XA_WM_PROTOCOLS = atom[2];
745 _XA_WM_TAKE_FOCUS = atom[3];
746 _XA_WM_DELETE_WINDOW = atom[4];
747 _XA_WM_SAVE_YOURSELF = atom[5];
748 _XA_WM_CLIENT_LEADER = atom[6];
749 _XA_WM_COLORMAP_WINDOWS = atom[7];
750 _XA_WM_COLORMAP_NOTIFY = atom[8];
752 _XA_GNUSTEP_WM_ATTR = atom[9];
754 _XA_WINDOWMAKER_MENU = atom[10];
755 _XA_WINDOWMAKER_STATE = atom[11];
757 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[12];
759 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[13];
761 _XA_WINDOWMAKER_WM_FUNCTION = atom[14];
763 _XA_WINDOWMAKER_NOTICEBOARD = atom[15];
765 _XA_WINDOWMAKER_COMMAND = atom[16];
767 _XA_WINDOWMAKER_ICON_SIZE = atom[17];
768 _XA_WINDOWMAKER_ICON_TILE = atom[18];
770 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
772 #ifdef OFFIX_DND
773 _XA_DND_SELECTION = XInternAtom(dpy, "DndSelection", False);
774 _XA_DND_PROTOCOL = XInternAtom(dpy, "DndProtocol", False);
775 #endif
776 #ifdef XDND
777 wXDNDInitializeAtoms();
778 #endif
781 /* cursors */
782 #ifdef DEFINABLE_CURSOR
783 wCursor[WCUR_NORMAL] = None;
784 #else
785 wCursor[WCUR_NORMAL] = XCreateFontCursor(dpy, XC_left_ptr);
786 #endif
787 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
788 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
789 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
790 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
791 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
792 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
793 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
794 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
795 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
796 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
797 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
798 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
799 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
800 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
802 /* signal handler stuff that gets called when a signal is caught */
803 WMAddEternalTimerHandler(500, delayedAction, NULL);
805 /* emergency exit... */
806 sig_action.sa_handler = handleSig;
807 sigemptyset(&sig_action.sa_mask);
809 sig_action.sa_flags = SA_RESTART;
810 sigaction(SIGQUIT, &sig_action, NULL);
811 sigaction(SIGSEGV, &sig_action, NULL);
812 sigaction(SIGBUS, &sig_action, NULL);
813 sigaction(SIGFPE, &sig_action, NULL);
814 sigaction(SIGABRT, &sig_action, NULL);
816 sig_action.sa_handler = handleExitSig;
818 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
819 * immediately.
820 * -Dan */
821 sig_action.sa_flags = SA_RESTART;
822 /* sigaction(SIGTERM, &sig_action, NULL);*/
823 sigaction(SIGINT, &sig_action, NULL);
824 sigaction(SIGHUP, &sig_action, NULL);
825 sigaction(SIGUSR1, &sig_action, NULL);
826 sigaction(SIGUSR2, &sig_action, NULL);
828 /* ignore dead pipe */
829 sig_action.sa_handler = SIG_IGN;
830 sig_action.sa_flags = SA_RESTART;
831 sigaction(SIGPIPE, &sig_action, NULL);
833 /* handle dead children */
834 sig_action.sa_handler = buryChild;
835 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
836 sigaction(SIGCHLD, &sig_action, NULL);
838 /* Now we unblock all signals, that may have been blocked by the parent
839 * who exec()-ed us. This can happen for example if Window Maker crashes
840 * and restarts itself or another window manager from the signal handler.
841 * In this case, the new proccess inherits the blocked signal mask and
842 * will no longer react to that signal, until unblocked.
843 * This is because the signal handler of the proccess who crashed (parent)
844 * didn't return, and the signal remained blocked. -Dan
846 sigfillset(&sig_action.sa_mask);
847 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
849 /* handle X shutdowns a such */
850 XSetIOErrorHandler(handleXIO);
852 /* set hook for out event dispatcher in WINGs event dispatcher */
853 WMHookEventHandler(DispatchEvent);
855 /* initialize defaults stuff */
856 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
857 if (!WDWindowMaker->dictionary) {
858 wwarning(_("could not read domain \"%s\" from defaults database"),
859 "WindowMaker");
862 /* read defaults that don't change until a restart and are
863 * screen independent */
864 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
866 /* check sanity of some values */
867 if (wPreferences.icon_size < 16) {
868 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
869 wPreferences.icon_size);
870 wPreferences.icon_size = 16;
873 /* init other domains */
874 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
875 if (!WDRootMenu->dictionary) {
876 wwarning(_("could not read domain \"%s\" from defaults database"),
877 "WMRootMenu");
880 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
881 if (!WDWindowAttributes->dictionary) {
882 wwarning(_("could not read domain \"%s\" from defaults database"),
883 "WMWindowAttributes");
886 XSetErrorHandler((XErrorHandler)catchXError);
888 #ifdef SHAPE
889 /* ignore j */
890 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
891 #endif
893 #ifdef KEEP_XKB_LOCK_STATUS
894 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
895 if(wPreferences.modelock && !wXkbSupported) {
896 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
897 wPreferences.modelock = 0;
899 #endif
901 if (defaultScreenOnly) {
902 max = 1;
903 } else {
904 max = ScreenCount(dpy);
906 wScreen = wmalloc(sizeof(WScreen*)*max);
908 wScreenCount = 0;
910 /* manage the screens */
911 for (j = 0; j < max; j++) {
912 if (defaultScreenOnly || max==1) {
913 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
914 if (!wScreen[wScreenCount]) {
915 wfatal(_("it seems that there is already a window manager running"));
916 Exit(1);
918 } else {
919 wScreen[wScreenCount] = wScreenInit(j);
920 if (!wScreen[wScreenCount]) {
921 wwarning(_("could not manage screen %i"), j);
922 continue;
925 wScreenCount++;
928 /* initialize/restore state for the screens */
929 for (j = 0; j < wScreenCount; j++) {
930 /* restore workspace state */
931 if (!getWorkspaceState(wScreen[j]->root_win, &ws_state)) {
932 ws_state = NULL;
935 wScreenRestoreState(wScreen[j]);
937 /* manage all windows that were already here before us */
938 if (!wPreferences.flags.nodock && wScreen[j]->dock)
939 wScreen[j]->last_dock = wScreen[j]->dock;
941 manageAllWindows(wScreen[j]);
943 /* restore saved menus */
944 wMenuRestoreState(wScreen[j]);
946 /* If we're not restarting restore session */
947 if (ws_state == NULL && !wPreferences.flags.norestore)
948 wSessionRestoreState(wScreen[j]);
950 if (!wPreferences.flags.noautolaunch) {
951 /* auto-launch apps */
952 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
953 wScreen[j]->last_dock = wScreen[j]->dock;
954 wDockDoAutoLaunch(wScreen[j]->dock, 0);
956 /* auto-launch apps in clip */
957 if (!wPreferences.flags.noclip) {
958 int i;
959 for(i=0; i<wScreen[j]->workspace_count; i++) {
960 if (wScreen[j]->workspaces[i]->clip) {
961 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
962 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
968 /* go to workspace where we were before restart */
969 if (ws_state) {
970 wWorkspaceForceChange(wScreen[j], ws_state->workspace);
971 wfree(ws_state);
972 } else {
973 wSessionRestoreLastWorkspace(wScreen[j]);
976 #ifdef KWM_HINTS
977 wKWMSetInitializedHint(wScreen[j]);
978 #endif
981 if (wScreenCount == 0) {
982 wfatal(_("could not manage any screen"));
983 Exit(1);
986 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates) {
987 /* setup defaults file polling */
988 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
995 static Bool
996 windowInList(Window window, Window *list, int count)
998 for (; count>=0; count--) {
999 if (window == list[count])
1000 return True;
1002 return False;
1006 *-----------------------------------------------------------------------
1007 * manageAllWindows--
1008 * Manages all windows in the screen.
1010 * Notes:
1011 * Called when the wm is being started.
1012 * No events can be processed while the windows are being
1013 * reparented/managed.
1014 *-----------------------------------------------------------------------
1016 static void
1017 manageAllWindows(WScreen *scr)
1019 Window root, parent;
1020 Window *children;
1021 unsigned int nchildren;
1022 unsigned int i, j;
1023 WWindow *wwin;
1025 XGrabServer(dpy);
1026 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
1028 scr->flags.startup = 1;
1030 /* first remove all icon windows */
1031 for (i = 0; i < nchildren; i++) {
1032 XWMHints *wmhints;
1034 if (children[i]==None)
1035 continue;
1037 wmhints = XGetWMHints(dpy, children[i]);
1038 if (wmhints && (wmhints->flags & IconWindowHint)) {
1039 for (j = 0; j < nchildren; j++) {
1040 if (children[j] == wmhints->icon_window) {
1041 XFree(wmhints);
1042 wmhints = NULL;
1043 children[j] = None;
1044 break;
1048 if (wmhints) {
1049 XFree(wmhints);
1054 for (i = 0; i < nchildren; i++) {
1055 if (children[i] == None)
1056 continue;
1058 #ifdef KWM_HINTS
1059 wKWMCheckModule(scr, children[i]);
1060 #endif
1061 wwin = wManageWindow(scr, children[i]);
1062 if (wwin) {
1063 /* apply states got from WSavedState */
1064 /* shaded + minimized is not restored correctly */
1065 if (wwin->flags.shaded) {
1066 wwin->flags.shaded = 0;
1067 wShadeWindow(wwin);
1069 if (wwin->flags.miniaturized
1070 && (wwin->transient_for == None
1071 || wwin->transient_for == scr->root_win
1072 || !windowInList(wwin->transient_for, children,
1073 nchildren))) {
1075 wwin->flags.skip_next_animation = 1;
1076 wwin->flags.miniaturized = 0;
1077 wIconifyWindow(wwin);
1078 } else {
1079 wClientSetState(wwin, NormalState, None);
1083 XUngrabServer(dpy);
1085 /* hide apps */
1086 wwin = scr->focused_window;
1087 while (wwin) {
1088 if (wwin->flags.hidden) {
1089 WApplication *wapp = wApplicationOf(wwin->main_window);
1091 if (wapp) {
1092 wwin->flags.hidden = 0;
1093 wHideApplication(wapp);
1094 } else {
1095 wwin->flags.hidden = 0;
1098 wwin = wwin->prev;
1101 XFree(children);
1102 scr->flags.startup = 0;
1103 scr->flags.startup2 = 1;
1105 while (XPending(dpy)) {
1106 XEvent ev;
1107 WMNextEvent(dpy, &ev);
1108 WMHandleEvent(&ev);
1110 wWorkspaceForceChange(scr, 0);
1111 if (!wPreferences.flags.noclip)
1112 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1113 scr->flags.startup2 = 0;