- s/sprintf/snprintf
[wmaker-crm.git] / src / startup.c
blob99a82092882ff9f144726354d5230224408c618e
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(WScreen *scr, int crashed);
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 return;
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 CARD32 data[2];
357 WWindow *wwin;
358 int i;
360 XGrabServer(dpy);
361 crashAction = wShowCrashingDialogPanel(sig);
364 * Save current workspace, so can go back to it if restarting.
365 * Also add hint that we crashed, so that windows will be placed
366 * correctly upon restart.
368 for (i=0; i<wScreenCount; i++) {
369 data[0] = wScreen[i]->current_workspace; /* workspace number */
370 data[1] = WFLAGS_CRASHED; /* signal that we crashed */
372 XChangeProperty(dpy, wScreen[i]->root_win,
373 _XA_WINDOWMAKER_STATE, _XA_WINDOWMAKER_STATE,
374 32, PropModeReplace, (unsigned char*) data, 2);
376 /* save state of windows */
377 wwin = wScreen[i]->focused_window;
378 while (wwin) {
379 wWindowSaveState(wwin);
380 wwin = wwin->prev;
385 XCloseDisplay(dpy);
386 dpy = NULL;
387 } else {
388 wsyserror(_("cannot open connection for crashing dialog panel. Aborting."));
389 crashAction = WMAbort;
392 if (crashAction == WMAbort) {
393 signal(sig, SIG_DFL);
394 kill(getpid(), sig);
395 return;
398 if (crashAction == WMRestart) {
399 /* we try to restart Window Maker */
400 wmessage(_("trying to restart Window Maker..."));
401 Restart(NULL, False);
402 /* fallback to alternate window manager then */
405 wmessage(_("trying to start alternate window manager..."));
407 Restart(FALLBACK_WINDOWMANAGER, False);
408 Restart("fvwm", False);
409 Restart("twm", False);
410 wfatal(_("failed to start alternate window manager. Aborting."));
411 #else
412 wfatal(_("a fatal error has occured, probably due to a bug. "
413 "Please fill the included BUGFORM and report it."));
414 #endif /* !NO_EMERGENCY_AUTORESTART */
416 signal(sig, SIG_DFL);
417 kill(getpid(), sig);
418 return;
422 wAbort(dumpcore);
426 static RETSIGTYPE
427 buryChild(int foo)
429 pid_t pid;
430 int status;
431 int save_errno = errno;
432 sigset_t sigs;
434 sigfillset(&sigs);
435 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
436 sigprocmask(SIG_BLOCK, &sigs, NULL);
438 /* R.I.P. */
439 /* If 2 or more kids exit in a small time window, before this handler gets
440 * the chance to get invoked, the SIGCHLD signals will be merged and only
441 * one SIGCHLD signal will be sent to us. We use a while loop to get all
442 * exited child status because we can't count on the number of SIGCHLD
443 * signals to know exactly how many kids have exited. -Dan
445 while ((pid=waitpid(-1, &status, WNOHANG))>0 || (pid<0 && errno==EINTR)) {
446 NotifyDeadProcess(pid, WEXITSTATUS(status));
449 WDelayedActionSet++;
451 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
453 errno = save_errno;
457 static int
458 getWorkspaceState(Window root, WWorkspaceState **state)
460 Atom type_ret;
461 int fmt_ret;
462 unsigned long nitems_ret;
463 unsigned long bytes_after_ret;
464 CARD32 *data;
466 if (XGetWindowProperty(dpy, root, _XA_WINDOWMAKER_STATE, 0, 2,
467 True, _XA_WINDOWMAKER_STATE,
468 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
469 (unsigned char **)&data)!=Success || !data)
470 return 0;
472 *state = wmalloc(sizeof(WWorkspaceState));
473 (*state)->workspace = data[0];
474 (*state)->flags = data[1];
476 XFree(data);
478 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
479 return 1;
480 else
481 return 0;
485 static void
486 getOffendingModifiers()
488 int i;
489 XModifierKeymap *modmap;
490 KeyCode nlock, slock;
491 static int mask_table[8] = {
492 ShiftMask,LockMask,ControlMask,Mod1Mask,
493 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
496 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
497 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
500 * Find out the masks for the NumLock and ScrollLock modifiers,
501 * so that we can bind the grabs for when they are enabled too.
503 modmap = XGetModifierMapping(dpy);
505 if (modmap!=NULL && modmap->max_keypermod>0) {
506 for (i=0; i<8*modmap->max_keypermod; i++) {
507 if (modmap->modifiermap[i]==nlock && nlock!=0)
508 _NumLockMask = mask_table[i/modmap->max_keypermod];
509 else if (modmap->modifiermap[i]==slock && slock!=0)
510 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
514 if (modmap)
515 XFreeModifiermap(modmap);
520 #ifdef NUMLOCK_HACK
521 void
522 wHackedGrabKey(int keycode, unsigned int modifiers,
523 Window grab_window, Bool owner_events, int pointer_mode,
524 int keyboard_mode)
526 if (modifiers == AnyModifier)
527 return;
529 /* grab all combinations of the modifier with CapsLock, NumLock and
530 * ScrollLock. How much memory/CPU does such a monstrosity consume
531 * in the server?
533 if (_NumLockMask)
534 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
535 grab_window, owner_events, pointer_mode, keyboard_mode);
536 if (_ScrollLockMask)
537 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
538 grab_window, owner_events, pointer_mode, keyboard_mode);
539 if (_NumLockMask && _ScrollLockMask)
540 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
541 grab_window, owner_events, pointer_mode, keyboard_mode);
542 if (_NumLockMask)
543 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
544 grab_window, owner_events, pointer_mode, keyboard_mode);
545 if (_ScrollLockMask)
546 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
547 grab_window, owner_events, pointer_mode, keyboard_mode);
548 if (_NumLockMask && _ScrollLockMask)
549 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
550 grab_window, owner_events, pointer_mode, keyboard_mode);
551 /* phew, I guess that's all, right? */
553 #endif
555 void
556 wHackedGrabButton(unsigned int button, unsigned int modifiers,
557 Window grab_window, Bool owner_events,
558 unsigned int event_mask, int pointer_mode,
559 int keyboard_mode, Window confine_to, Cursor cursor)
561 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
562 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
564 if (modifiers==AnyModifier)
565 return;
567 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
568 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
570 #ifdef NUMLOCK_HACK
571 /* same as above, but for mouse buttons */
572 if (_NumLockMask)
573 XGrabButton(dpy, button, modifiers|_NumLockMask,
574 grab_window, owner_events, event_mask, pointer_mode,
575 keyboard_mode, confine_to, cursor);
576 if (_ScrollLockMask)
577 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
578 grab_window, owner_events, event_mask, pointer_mode,
579 keyboard_mode, confine_to, cursor);
580 if (_NumLockMask && _ScrollLockMask)
581 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
582 grab_window, owner_events, event_mask, pointer_mode,
583 keyboard_mode, confine_to, cursor);
584 if (_NumLockMask)
585 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
586 grab_window, owner_events, event_mask, pointer_mode,
587 keyboard_mode, confine_to, cursor);
588 if (_ScrollLockMask)
589 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
590 grab_window, owner_events, event_mask, pointer_mode,
591 keyboard_mode, confine_to, cursor);
592 if (_NumLockMask && _ScrollLockMask)
593 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
594 grab_window, owner_events, event_mask, pointer_mode,
595 keyboard_mode, confine_to, cursor);
596 #endif /* NUMLOCK_HACK */
599 #ifdef notused
600 void
601 wHackedUngrabButton(unsigned int button, unsigned int modifiers,
602 Window grab_window)
604 XUngrabButton(dpy, button, modifiers|_NumLockMask,
605 grab_window);
606 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
607 grab_window);
608 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
609 grab_window);
610 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
611 grab_window);
612 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
613 grab_window);
614 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
615 grab_window);
617 #endif
621 WScreen*
622 wScreenWithNumber(int i)
624 assert(i < wScreenCount);
626 return wScreen[i];
630 WScreen*
631 wScreenForRootWindow(Window window)
633 int i;
635 if (wScreenCount==1)
636 return wScreen[0];
639 * Since the number of heads will probably be small (normally 2),
640 * it should be faster to use this than a hash table, because
641 * of the overhead.
643 for (i=0; i<wScreenCount; i++) {
644 if (wScreen[i]->root_win == window) {
645 return wScreen[i];
649 assert("bad_root_window" && 0);
651 return NULL;
655 WScreen*
656 wScreenSearchForRootWindow(Window window)
658 int i;
660 if (wScreenCount==1)
661 return wScreen[0];
664 * Since the number of heads will probably be small (normally 2),
665 * it should be faster to use this than a hash table, because
666 * of the overhead.
668 for (i=0; i<wScreenCount; i++) {
669 if (wScreen[i]->root_win == window) {
670 return wScreen[i];
674 return NULL;
678 WScreen*
679 wScreenForWindow(Window window)
681 XWindowAttributes attr;
683 if (wScreenCount==1)
684 return wScreen[0];
686 if (XGetWindowAttributes(dpy, window, &attr)) {
687 return wScreenForRootWindow(attr.root);
689 return NULL;
693 static char *atomNames[] = {
694 "WM_STATE",
695 "WM_CHANGE_STATE",
696 "WM_PROTOCOLS",
697 "WM_TAKE_FOCUS",
698 "WM_DELETE_WINDOW",
699 "WM_SAVE_YOURSELF",
700 "WM_CLIENT_LEADER",
701 "WM_COLORMAP_WINDOWS",
702 "WM_COLORMAP_NOTIFY",
704 "_WINDOWMAKER_MENU",
705 "_WINDOWMAKER_STATE",
706 "_WINDOWMAKER_WM_PROTOCOLS",
707 "_WINDOWMAKER_WM_FUNCTION",
708 "_WINDOWMAKER_NOTICEBOARD",
709 "_WINDOWMAKER_COMMAND",
710 "_WINDOWMAKER_ICON_SIZE",
711 "_WINDOWMAKER_ICON_TILE",
713 GNUSTEP_WM_ATTR_NAME,
714 GNUSTEP_WM_MINIATURIZE_WINDOW,
715 GNUSTEP_TITLEBAR_STATE
718 static void
719 handle_sigpipe(int signum)
721 if (0) signum=0; /* To avoid a gcc warning */
722 return;
726 *----------------------------------------------------------
727 * StartUp--
728 * starts the window manager and setup global data.
729 * Called from main() at startup.
731 * Side effects:
732 * global data declared in main.c is initialized
733 *----------------------------------------------------------
735 void
736 StartUp(Bool defaultScreenOnly)
738 WWorkspaceState *ws_state;
739 struct sigaction sig_action;
740 int j, max;
741 Atom atom[sizeof(atomNames)/sizeof(char*)];
744 * Ignore CapsLock in modifiers
746 ValidModMask = 0xff & ~LockMask;
748 getOffendingModifiers();
750 * Ignore NumLock and ScrollLock too
752 ValidModMask &= ~(_NumLockMask|_ScrollLockMask);
755 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
757 wWinContext = XUniqueContext();
758 wAppWinContext = XUniqueContext();
759 wStackContext = XUniqueContext();
761 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
763 #ifdef HAVE_XINTERNATOMS
764 XInternAtoms(dpy, atomNames, sizeof(atomNames)/sizeof(char*),
765 False, atom);
766 #else
769 int i;
770 for (i = 0; i < sizeof(atomNames)/sizeof(char*); i++) {
771 atom[i] = XInternAtom(dpy, atomNames[i], False);
774 #endif
776 _XA_WM_STATE = atom[0];
777 _XA_WM_CHANGE_STATE = atom[1];
778 _XA_WM_PROTOCOLS = atom[2];
779 _XA_WM_TAKE_FOCUS = atom[3];
780 _XA_WM_DELETE_WINDOW = atom[4];
781 _XA_WM_SAVE_YOURSELF = atom[5];
782 _XA_WM_CLIENT_LEADER = atom[6];
783 _XA_WM_COLORMAP_WINDOWS = atom[7];
784 _XA_WM_COLORMAP_NOTIFY = atom[8];
786 _XA_WINDOWMAKER_MENU = atom[9];
787 _XA_WINDOWMAKER_STATE = atom[10];
788 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
789 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
790 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
791 _XA_WINDOWMAKER_COMMAND = atom[14];
792 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
793 _XA_WINDOWMAKER_ICON_TILE = atom[16];
795 _XA_GNUSTEP_WM_ATTR = atom[17];
796 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
797 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
799 #ifdef OFFIX_DND
800 _XA_DND_SELECTION = XInternAtom(dpy, "DndSelection", False);
801 _XA_DND_PROTOCOL = XInternAtom(dpy, "DndProtocol", False);
802 #endif
803 #ifdef XDND
804 wXDNDInitializeAtoms();
805 #endif
808 /* cursors */
809 #ifdef DEFINABLE_CURSOR
810 wCursor[WCUR_NORMAL] = None;
811 #else
812 wCursor[WCUR_NORMAL] = XCreateFontCursor(dpy, XC_left_ptr);
813 #endif
814 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
815 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
816 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
817 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
818 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
819 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
820 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
821 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
822 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
823 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
824 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
825 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
826 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
827 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
829 /* signal handler stuff that gets called when a signal is caught */
830 WMAddPersistentTimerHandler(500, delayedAction, NULL);
832 /* emergency exit... */
833 sig_action.sa_handler = handleSig;
834 sigemptyset(&sig_action.sa_mask);
836 sig_action.sa_flags = SA_RESTART;
837 sigaction(SIGQUIT, &sig_action, NULL);
838 sigaction(SIGSEGV, &sig_action, NULL);
839 sigaction(SIGBUS, &sig_action, NULL);
840 sigaction(SIGFPE, &sig_action, NULL);
841 sigaction(SIGABRT, &sig_action, NULL);
843 sig_action.sa_handler = handleExitSig;
845 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
846 * immediately.
847 * -Dan */
848 sig_action.sa_flags = SA_RESTART;
849 /* sigaction(SIGTERM, &sig_action, NULL);*/
850 sigaction(SIGINT, &sig_action, NULL);
851 sigaction(SIGHUP, &sig_action, NULL);
852 sigaction(SIGUSR1, &sig_action, NULL);
853 sigaction(SIGUSR2, &sig_action, NULL);
855 /* ignore dead pipe */
856 sig_action.sa_handler = &handle_sigpipe;
857 /* Because POSIX mandates that only signal with handlers are reset
858 accross an exec*(), we do not want to propagate ignoring SIGPIPEs
859 to children. Hence the dummy handler.
860 Philippe Troin <phil@fifi.org>
862 sig_action.sa_flags = SA_RESTART;
863 sigaction(SIGPIPE, &sig_action, NULL);
865 /* handle dead children */
866 sig_action.sa_handler = buryChild;
867 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
868 sigaction(SIGCHLD, &sig_action, NULL);
870 /* Now we unblock all signals, that may have been blocked by the parent
871 * who exec()-ed us. This can happen for example if Window Maker crashes
872 * and restarts itself or another window manager from the signal handler.
873 * In this case, the new proccess inherits the blocked signal mask and
874 * will no longer react to that signal, until unblocked.
875 * This is because the signal handler of the proccess who crashed (parent)
876 * didn't return, and the signal remained blocked. -Dan
878 sigfillset(&sig_action.sa_mask);
879 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
881 /* handle X shutdowns a such */
882 XSetIOErrorHandler(handleXIO);
884 /* set hook for out event dispatcher in WINGs event dispatcher */
885 WMHookEventHandler(DispatchEvent);
887 /* initialize defaults stuff */
888 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
889 if (!WDWindowMaker->dictionary) {
890 wwarning(_("could not read domain \"%s\" from defaults database"),
891 "WindowMaker");
894 /* read defaults that don't change until a restart and are
895 * screen independent */
896 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
898 /* check sanity of some values */
899 if (wPreferences.icon_size < 16) {
900 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
901 wPreferences.icon_size);
902 wPreferences.icon_size = 16;
905 /* init other domains */
906 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
907 if (!WDRootMenu->dictionary) {
908 wwarning(_("could not read domain \"%s\" from defaults database"),
909 "WMRootMenu");
912 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
913 if (!WDWindowAttributes->dictionary) {
914 wwarning(_("could not read domain \"%s\" from defaults database"),
915 "WMWindowAttributes");
918 XSetErrorHandler((XErrorHandler)catchXError);
920 #ifdef SHAPE
921 /* ignore j */
922 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
923 #endif
925 #ifdef KEEP_XKB_LOCK_STATUS
926 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
927 if(wPreferences.modelock && !wXkbSupported) {
928 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
929 wPreferences.modelock = 0;
931 #endif
933 if (defaultScreenOnly) {
934 max = 1;
935 } else {
936 max = ScreenCount(dpy);
938 wScreen = wmalloc(sizeof(WScreen*)*max);
940 wScreenCount = 0;
942 /* manage the screens */
943 for (j = 0; j < max; j++) {
944 if (defaultScreenOnly || max==1) {
945 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
946 if (!wScreen[wScreenCount]) {
947 wfatal(_("it seems that there is already a window manager running"));
948 Exit(1);
950 } else {
951 wScreen[wScreenCount] = wScreenInit(j);
952 if (!wScreen[wScreenCount]) {
953 wwarning(_("could not manage screen %i"), j);
954 continue;
957 wScreenCount++;
960 /* initialize/restore state for the screens */
961 for (j = 0; j < wScreenCount; j++) {
962 int crashed;
964 /* restore workspace state */
965 if (!getWorkspaceState(wScreen[j]->root_win, &ws_state)) {
966 ws_state = NULL;
969 wScreenRestoreState(wScreen[j]);
971 /* manage all windows that were already here before us */
972 if (!wPreferences.flags.nodock && wScreen[j]->dock)
973 wScreen[j]->last_dock = wScreen[j]->dock;
975 if (ws_state && (ws_state->flags & WFLAGS_CRASHED)!=0)
976 crashed = 1;
977 else
978 crashed = 0;
980 manageAllWindows(wScreen[j], crashed);
982 /* restore saved menus */
983 wMenuRestoreState(wScreen[j]);
985 /* If we're not restarting restore session */
986 if (ws_state == NULL && !wPreferences.flags.norestore)
987 wSessionRestoreState(wScreen[j]);
989 if (!wPreferences.flags.noautolaunch) {
990 /* auto-launch apps */
991 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
992 wScreen[j]->last_dock = wScreen[j]->dock;
993 wDockDoAutoLaunch(wScreen[j]->dock, 0);
995 /* auto-launch apps in clip */
996 if (!wPreferences.flags.noclip) {
997 int i;
998 for(i=0; i<wScreen[j]->workspace_count; i++) {
999 if (wScreen[j]->workspaces[i]->clip) {
1000 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
1001 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
1007 /* go to workspace where we were before restart */
1008 if (ws_state) {
1009 wWorkspaceForceChange(wScreen[j], ws_state->workspace);
1010 wfree(ws_state);
1011 } else {
1012 wSessionRestoreLastWorkspace(wScreen[j]);
1015 #ifdef KWM_HINTS
1016 wKWMSetInitializedHint(wScreen[j]);
1017 #endif
1020 if (wScreenCount == 0) {
1021 wfatal(_("could not manage any screen"));
1022 Exit(1);
1025 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates) {
1026 /* setup defaults file polling */
1027 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
1034 static Bool
1035 windowInList(Window window, Window *list, int count)
1037 for (; count>=0; count--) {
1038 if (window == list[count])
1039 return True;
1041 return False;
1045 *-----------------------------------------------------------------------
1046 * manageAllWindows--
1047 * Manages all windows in the screen.
1049 * Notes:
1050 * Called when the wm is being started.
1051 * No events can be processed while the windows are being
1052 * reparented/managed.
1053 *-----------------------------------------------------------------------
1055 static void
1056 manageAllWindows(WScreen *scr, int crashRecovery)
1058 Window root, parent;
1059 Window *children;
1060 unsigned int nchildren;
1061 unsigned int i, j;
1062 WWindow *wwin;
1064 XGrabServer(dpy);
1065 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
1067 scr->flags.startup = 1;
1069 /* first remove all icon windows */
1070 for (i = 0; i < nchildren; i++) {
1071 XWMHints *wmhints;
1073 if (children[i]==None)
1074 continue;
1076 wmhints = XGetWMHints(dpy, children[i]);
1077 if (wmhints && (wmhints->flags & IconWindowHint)) {
1078 for (j = 0; j < nchildren; j++) {
1079 if (children[j] == wmhints->icon_window) {
1080 XFree(wmhints);
1081 wmhints = NULL;
1082 children[j] = None;
1083 break;
1087 if (wmhints) {
1088 XFree(wmhints);
1093 for (i = 0; i < nchildren; i++) {
1094 if (children[i] == None)
1095 continue;
1097 #ifdef KWM_HINTS
1098 wKWMCheckModule(scr, children[i]);
1099 #endif
1100 wwin = wManageWindow(scr, children[i]);
1101 if (wwin) {
1102 /* apply states got from WSavedState */
1103 /* shaded + minimized is not restored correctly */
1104 if (wwin->flags.shaded) {
1105 wwin->flags.shaded = 0;
1106 wShadeWindow(wwin);
1108 if (wwin->flags.miniaturized
1109 && (wwin->transient_for == None
1110 || wwin->transient_for == scr->root_win
1111 || !windowInList(wwin->transient_for, children,
1112 nchildren))) {
1114 wwin->flags.skip_next_animation = 1;
1115 wwin->flags.miniaturized = 0;
1116 wIconifyWindow(wwin);
1117 } else {
1118 wClientSetState(wwin, NormalState, None);
1120 if (crashRecovery) {
1121 int border;
1123 border = (WFLAGP(wwin, no_border) ? 0 : FRAME_BORDER_WIDTH);
1125 wWindowMove(wwin, wwin->frame_x - border,
1126 wwin->frame_y - border -
1127 wwin->frame->titlebar->height);
1131 XUngrabServer(dpy);
1133 /* hide apps */
1134 wwin = scr->focused_window;
1135 while (wwin) {
1136 if (wwin->flags.hidden) {
1137 WApplication *wapp = wApplicationOf(wwin->main_window);
1139 if (wapp) {
1140 wwin->flags.hidden = 0;
1141 wHideApplication(wapp);
1142 } else {
1143 wwin->flags.hidden = 0;
1146 wwin = wwin->prev;
1149 XFree(children);
1150 scr->flags.startup = 0;
1151 scr->flags.startup2 = 1;
1153 while (XPending(dpy)) {
1154 XEvent ev;
1155 WMNextEvent(dpy, &ev);
1156 WMHandleEvent(&ev);
1158 wWorkspaceForceChange(scr, 0);
1159 if (!wPreferences.flags.noclip)
1160 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1161 scr->flags.startup2 = 0;