changed indentation to use spaces only
[wmaker-crm.git] / src / startup.c
blob514087c0178bd8337c4f8c5f9dfbcd44e65f8f5b
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 Dan Pascu
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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;
114 extern XContext wVEdgeContext;
116 /* atoms */
117 extern Atom _XA_WM_STATE;
118 extern Atom _XA_WM_CHANGE_STATE;
119 extern Atom _XA_WM_PROTOCOLS;
120 extern Atom _XA_WM_TAKE_FOCUS;
121 extern Atom _XA_WM_DELETE_WINDOW;
122 extern Atom _XA_WM_SAVE_YOURSELF;
123 extern Atom _XA_WM_CLIENT_LEADER;
124 extern Atom _XA_WM_COLORMAP_WINDOWS;
125 extern Atom _XA_WM_COLORMAP_NOTIFY;
127 extern Atom _XA_GNUSTEP_WM_ATTR;
129 extern Atom _XA_WINDOWMAKER_MENU;
130 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
131 extern Atom _XA_WINDOWMAKER_STATE;
132 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
133 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
134 extern Atom _XA_WINDOWMAKER_COMMAND;
135 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
136 extern Atom _XA_WINDOWMAKER_ICON_TILE;
138 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
139 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
141 #ifdef OFFIX_DND
142 extern Atom _XA_DND_PROTOCOL;
143 extern Atom _XA_DND_SELECTION;
144 #endif
147 /* cursors */
148 extern Cursor wCursor[WCUR_LAST];
150 /* special flags */
151 extern char WDelayedActionSet;
153 /***** Local *****/
155 static WScreen **wScreen = NULL;
158 static unsigned int _NumLockMask = 0;
159 static unsigned int _ScrollLockMask = 0;
163 static void manageAllWindows(WScreen *scr, int crashed);
165 extern void NotifyDeadProcess(pid_t pid, unsigned char status);
168 static int
169 catchXError(Display *dpy, XErrorEvent *error)
171 char buffer[MAXLINE];
173 /* ignore some errors */
174 if (error->resourceid != None
175 && ((error->error_code == BadDrawable
176 && error->request_code == X_GetGeometry)
177 || (error->error_code == BadMatch
178 && (error->request_code == X_SetInputFocus))
179 || (error->error_code == BadWindow)
181 && (error->request_code == X_GetWindowAttributes
182 || error->request_code == X_SetInputFocus
183 || error->request_code == X_ChangeWindowAttributes
184 || error->request_code == X_GetProperty
185 || error->request_code == X_ChangeProperty
186 || error->request_code == X_QueryTree
187 || error->request_code == X_GrabButton
188 || error->request_code == X_UngrabButton
189 || error->request_code == X_SendEvent
190 || error->request_code == X_ConfigureWindow))
192 || (error->request_code == X_InstallColormap))) {
193 #ifndef DEBUG
195 return 0;
196 #else
197 printf("got X error %x %x %x\n", error->request_code,
198 error->error_code, (unsigned)error->resourceid);
199 return 0;
200 #endif
202 FormatXError(dpy, error, buffer, MAXLINE);
203 wwarning(_("internal X error: %s\n"), buffer);
204 return -1;
209 *----------------------------------------------------------------------
210 * handleXIO-
211 * Handle X shutdowns and other stuff.
212 *----------------------------------------------------------------------
214 static int
215 handleXIO(Display *xio_dpy)
217 dpy = NULL;
218 Exit(0);
219 return 0;
224 *----------------------------------------------------------------------
225 * delayedAction-
226 * Action to be executed after the signal() handler is exited.
227 *----------------------------------------------------------------------
229 static void
230 delayedAction(void *cdata)
232 if (WDelayedActionSet == 0) {
233 return;
235 WDelayedActionSet--;
237 * Make the event dispatcher do whatever it needs to do,
238 * including handling zombie processes, restart and exit
239 * signals.
241 DispatchEvent(NULL);
246 *----------------------------------------------------------------------
247 * handleExitSig--
248 * User generated exit signal handler.
249 *----------------------------------------------------------------------
251 static RETSIGTYPE
252 handleExitSig(int sig)
254 sigset_t sigs;
256 sigfillset(&sigs);
257 sigprocmask(SIG_BLOCK, &sigs, NULL);
259 if (sig == SIGUSR1) {
260 #ifdef SYS_SIGLIST_DECLARED
261 wwarning(_("got signal %i (%s) - restarting\n"), sig, sys_siglist[sig]);
262 #else
263 wwarning(_("got signal %i - restarting\n"), sig);
264 #endif
266 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
267 /* setup idle handler, so that this will be handled when
268 * the select() is returned becaused of the signal, even if
269 * there are no X events in the queue */
270 WDelayedActionSet++;
271 } else if (sig == SIGUSR2) {
272 #ifdef SYS_SIGLIST_DECLARED
273 wwarning(_("got signal %i (%s) - rereading defaults\n"), sig, sys_siglist[sig]);
274 #else
275 wwarning(_("got signal %i - rereading defaults\n"), sig);
276 #endif
278 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
279 /* setup idle handler, so that this will be handled when
280 * the select() is returned becaused of the signal, even if
281 * there are no X events in the queue */
282 WDelayedActionSet++;
283 } else if (sig==SIGTERM || sig==SIGINT || sig==SIGHUP) {
284 #ifdef SYS_SIGLIST_DECLARED
285 wwarning(_("got signal %i (%s) - exiting...\n"), sig, sys_siglist[sig]);
286 #else
287 wwarning(_("got signal %i - exiting...\n"), sig);
288 #endif
290 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
292 WDelayedActionSet++;
295 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
298 /* Dummy signal handler */
299 static void
300 dummyHandler(int sig)
306 *----------------------------------------------------------------------
307 * handleSig--
308 * general signal handler. Exits the program gently.
309 *----------------------------------------------------------------------
311 static RETSIGTYPE
312 handleSig(int sig)
314 static int already_crashed = 0;
315 int dumpcore = 0;
316 #ifndef NO_EMERGENCY_AUTORESTART
317 int crashAction, i;
318 char *argv[2];
320 argv[1] = NULL;
321 #endif
324 * No functions that potentially do Xlib calls should be called from
325 * here. Xlib calls are not reentrant so the integrity of Xlib is
326 * not guaranteed if a Xlib call is made from a signal handler.
329 #ifdef SYS_SIGLIST_DECLARED
330 wfatal(_("got signal %i (%s)\n"), sig, sys_siglist[sig]);
331 #else
332 wfatal(_("got signal %i\n"), sig);
333 #endif
335 /* Setting the signal behaviour back to default and then reraising the
336 * signal is a cleaner way to make program exit and core dump than calling
337 * abort(), since it correctly returns from the signal handler and sets
338 * the flags accordingly. -Dan
340 if (sig==SIGSEGV || sig==SIGFPE || sig==SIGBUS || sig==SIGILL
341 || sig==SIGABRT) {
342 if (already_crashed) {
343 wfatal(_("crashed while trying to do some post-crash cleanup. Aborting immediatelly."));
344 signal(sig, SIG_DFL);
345 kill(getpid(), sig);
346 return;
348 already_crashed = 1;
350 dumpcore = 1;
353 * Yeah, we shouldn't do this, but it's already crashed anyway :P
356 #ifndef NO_EMERGENCY_AUTORESTART
357 /* Close the X connection and open a new one. This is to avoid messing
358 * Xlib because we call to Xlib functions in a signal handler.
360 if (dpy)
361 XCloseDisplay(dpy);
362 dpy = XOpenDisplay("");
363 if (dpy) {
364 CARD32 data[2];
365 WWindow *wwin;
367 XGrabServer(dpy);
368 crashAction = wShowCrashingDialogPanel(sig);
371 * Save current workspace, so can go back to it if restarting.
372 * Also add hint that we crashed, so that windows will be placed
373 * correctly upon restart.
375 for (i=0; i<wScreenCount; i++) {
376 data[0] = wScreen[i]->current_workspace; /* workspace number */
377 data[1] = WFLAGS_CRASHED; /* signal that we crashed */
379 XChangeProperty(dpy, wScreen[i]->root_win,
380 _XA_WINDOWMAKER_STATE, _XA_WINDOWMAKER_STATE,
381 32, PropModeReplace, (unsigned char*) data, 2);
383 /* save state of windows */
384 wwin = wScreen[i]->focused_window;
385 while (wwin) {
386 wWindowSaveState(wwin);
387 wwin = wwin->prev;
392 XCloseDisplay(dpy);
393 dpy = NULL;
394 } else {
395 wsyserror(_("cannot open connection for crashing dialog panel. Aborting."));
396 crashAction = WMAbort;
399 if (crashAction == WMAbort) {
400 signal(sig, SIG_DFL);
401 kill(getpid(), sig);
402 return;
405 if (crashAction == WMRestart) {
406 /* we try to restart Window Maker */
407 wmessage(_("trying to restart Window Maker..."));
408 Restart(NULL, False);
409 /* fallback to alternate window manager then */
412 wmessage(_("trying to start alternate window manager..."));
414 for (i=0; i<WMGetArrayItemCount(wPreferences.fallbackWMs); i++) {
415 Restart(WMGetFromArray(wPreferences.fallbackWMs, i), False);
418 wfatal(_("failed to start alternate window manager. Aborting."));
419 #else
420 wfatal(_("a fatal error has occured, probably due to a bug. "
421 "Please fill the included BUGFORM and report it."));
422 #endif /* !NO_EMERGENCY_AUTORESTART */
424 signal(sig, SIG_DFL);
425 kill(getpid(), sig);
426 return;
430 wAbort(dumpcore);
434 static RETSIGTYPE
435 buryChild(int foo)
437 pid_t pid;
438 int status;
439 int save_errno = errno;
440 sigset_t sigs;
442 sigfillset(&sigs);
443 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
444 sigprocmask(SIG_BLOCK, &sigs, NULL);
446 /* R.I.P. */
447 /* If 2 or more kids exit in a small time window, before this handler gets
448 * the chance to get invoked, the SIGCHLD signals will be merged and only
449 * one SIGCHLD signal will be sent to us. We use a while loop to get all
450 * exited child status because we can't count on the number of SIGCHLD
451 * signals to know exactly how many kids have exited. -Dan
453 while ((pid=waitpid(-1, &status, WNOHANG))>0 || (pid<0 && errno==EINTR)) {
454 NotifyDeadProcess(pid, WEXITSTATUS(status));
457 WDelayedActionSet++;
459 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
461 errno = save_errno;
465 static int
466 getWorkspaceState(Window root, WWorkspaceState **state)
468 Atom type_ret;
469 int fmt_ret;
470 unsigned long nitems_ret;
471 unsigned long bytes_after_ret;
472 CARD32 *data;
474 if (XGetWindowProperty(dpy, root, _XA_WINDOWMAKER_STATE, 0, 2,
475 True, _XA_WINDOWMAKER_STATE,
476 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
477 (unsigned char **)&data)!=Success || !data)
478 return 0;
480 *state = wmalloc(sizeof(WWorkspaceState));
481 (*state)->workspace = data[0];
482 (*state)->flags = data[1];
484 XFree(data);
486 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
487 return 1;
488 else
489 return 0;
493 static void
494 getOffendingModifiers()
496 int i;
497 XModifierKeymap *modmap;
498 KeyCode nlock, slock;
499 static int mask_table[8] = {
500 ShiftMask,LockMask,ControlMask,Mod1Mask,
501 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
504 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
505 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
508 * Find out the masks for the NumLock and ScrollLock modifiers,
509 * so that we can bind the grabs for when they are enabled too.
511 modmap = XGetModifierMapping(dpy);
513 if (modmap!=NULL && modmap->max_keypermod>0) {
514 for (i=0; i<8*modmap->max_keypermod; i++) {
515 if (modmap->modifiermap[i]==nlock && nlock!=0)
516 _NumLockMask = mask_table[i/modmap->max_keypermod];
517 else if (modmap->modifiermap[i]==slock && slock!=0)
518 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
522 if (modmap)
523 XFreeModifiermap(modmap);
528 #ifdef NUMLOCK_HACK
529 void
530 wHackedGrabKey(int keycode, unsigned int modifiers,
531 Window grab_window, Bool owner_events, int pointer_mode,
532 int keyboard_mode)
534 if (modifiers == AnyModifier)
535 return;
537 /* grab all combinations of the modifier with CapsLock, NumLock and
538 * ScrollLock. How much memory/CPU does such a monstrosity consume
539 * in the server?
541 if (_NumLockMask)
542 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
543 grab_window, owner_events, pointer_mode, keyboard_mode);
544 if (_ScrollLockMask)
545 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
546 grab_window, owner_events, pointer_mode, keyboard_mode);
547 if (_NumLockMask && _ScrollLockMask)
548 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
549 grab_window, owner_events, pointer_mode, keyboard_mode);
550 if (_NumLockMask)
551 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
552 grab_window, owner_events, pointer_mode, keyboard_mode);
553 if (_ScrollLockMask)
554 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
555 grab_window, owner_events, pointer_mode, keyboard_mode);
556 if (_NumLockMask && _ScrollLockMask)
557 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
558 grab_window, owner_events, pointer_mode, keyboard_mode);
559 /* phew, I guess that's all, right? */
561 #endif
563 void
564 wHackedGrabButton(unsigned int button, unsigned int modifiers,
565 Window grab_window, Bool owner_events,
566 unsigned int event_mask, int pointer_mode,
567 int keyboard_mode, Window confine_to, Cursor cursor)
569 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
570 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
572 if (modifiers==AnyModifier)
573 return;
575 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
576 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
578 #ifdef NUMLOCK_HACK
579 /* same as above, but for mouse buttons */
580 if (_NumLockMask)
581 XGrabButton(dpy, button, modifiers|_NumLockMask,
582 grab_window, owner_events, event_mask, pointer_mode,
583 keyboard_mode, confine_to, cursor);
584 if (_ScrollLockMask)
585 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
586 grab_window, owner_events, event_mask, pointer_mode,
587 keyboard_mode, confine_to, cursor);
588 if (_NumLockMask && _ScrollLockMask)
589 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
590 grab_window, owner_events, event_mask, pointer_mode,
591 keyboard_mode, confine_to, cursor);
592 if (_NumLockMask)
593 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
594 grab_window, owner_events, event_mask, pointer_mode,
595 keyboard_mode, confine_to, cursor);
596 if (_ScrollLockMask)
597 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
598 grab_window, owner_events, event_mask, pointer_mode,
599 keyboard_mode, confine_to, cursor);
600 if (_NumLockMask && _ScrollLockMask)
601 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
602 grab_window, owner_events, event_mask, pointer_mode,
603 keyboard_mode, confine_to, cursor);
604 #endif /* NUMLOCK_HACK */
607 #ifdef notused
608 void
609 wHackedUngrabButton(unsigned int button, unsigned int modifiers,
610 Window grab_window)
612 XUngrabButton(dpy, button, modifiers|_NumLockMask,
613 grab_window);
614 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
615 grab_window);
616 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
617 grab_window);
618 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
619 grab_window);
620 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
621 grab_window);
622 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
623 grab_window);
625 #endif
629 WScreen*
630 wScreenWithNumber(int i)
632 assert(i < wScreenCount);
634 return wScreen[i];
638 WScreen*
639 wScreenForRootWindow(Window window)
641 int i;
643 if (wScreenCount==1)
644 return wScreen[0];
647 * Since the number of heads will probably be small (normally 2),
648 * it should be faster to use this than a hash table, because
649 * of the overhead.
651 for (i=0; i<wScreenCount; i++) {
652 if (wScreen[i]->root_win == window) {
653 return wScreen[i];
657 return wScreenForWindow(window);
661 WScreen*
662 wScreenSearchForRootWindow(Window window)
664 int i;
666 if (wScreenCount==1)
667 return wScreen[0];
670 * Since the number of heads will probably be small (normally 2),
671 * it should be faster to use this than a hash table, because
672 * of the overhead.
674 for (i=0; i<wScreenCount; i++) {
675 if (wScreen[i]->root_win == window) {
676 return wScreen[i];
680 return wScreenForWindow(window);
684 WScreen*
685 wScreenForWindow(Window window)
687 XWindowAttributes attr;
689 if (wScreenCount==1)
690 return wScreen[0];
692 if (XGetWindowAttributes(dpy, window, &attr)) {
693 return wScreenForRootWindow(attr.root);
695 return NULL;
699 static char *atomNames[] = {
700 "WM_STATE",
701 "WM_CHANGE_STATE",
702 "WM_PROTOCOLS",
703 "WM_TAKE_FOCUS",
704 "WM_DELETE_WINDOW",
705 "WM_SAVE_YOURSELF",
706 "WM_CLIENT_LEADER",
707 "WM_COLORMAP_WINDOWS",
708 "WM_COLORMAP_NOTIFY",
710 "_WINDOWMAKER_MENU",
711 "_WINDOWMAKER_STATE",
712 "_WINDOWMAKER_WM_PROTOCOLS",
713 "_WINDOWMAKER_WM_FUNCTION",
714 "_WINDOWMAKER_NOTICEBOARD",
715 "_WINDOWMAKER_COMMAND",
716 "_WINDOWMAKER_ICON_SIZE",
717 "_WINDOWMAKER_ICON_TILE",
719 GNUSTEP_WM_ATTR_NAME,
720 GNUSTEP_WM_MINIATURIZE_WINDOW,
721 GNUSTEP_TITLEBAR_STATE
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();
760 wVEdgeContext = XUniqueContext();
762 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
764 #ifdef HAVE_XINTERNATOMS
765 XInternAtoms(dpy, atomNames, sizeof(atomNames)/sizeof(char*),
766 False, atom);
767 #else
770 int i;
771 for (i = 0; i < sizeof(atomNames)/sizeof(char*); i++) {
772 atom[i] = XInternAtom(dpy, atomNames[i], False);
775 #endif
777 _XA_WM_STATE = atom[0];
778 _XA_WM_CHANGE_STATE = atom[1];
779 _XA_WM_PROTOCOLS = atom[2];
780 _XA_WM_TAKE_FOCUS = atom[3];
781 _XA_WM_DELETE_WINDOW = atom[4];
782 _XA_WM_SAVE_YOURSELF = atom[5];
783 _XA_WM_CLIENT_LEADER = atom[6];
784 _XA_WM_COLORMAP_WINDOWS = atom[7];
785 _XA_WM_COLORMAP_NOTIFY = atom[8];
787 _XA_WINDOWMAKER_MENU = atom[9];
788 _XA_WINDOWMAKER_STATE = atom[10];
789 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
790 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
791 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
792 _XA_WINDOWMAKER_COMMAND = atom[14];
793 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
794 _XA_WINDOWMAKER_ICON_TILE = atom[16];
796 _XA_GNUSTEP_WM_ATTR = atom[17];
797 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
798 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
800 #ifdef OFFIX_DND
801 _XA_DND_SELECTION = XInternAtom(dpy, "DndSelection", False);
802 _XA_DND_PROTOCOL = XInternAtom(dpy, "DndProtocol", False);
803 #endif
804 #ifdef XDND
805 wXDNDInitializeAtoms();
806 #endif
809 /* cursors */
810 wCursor[WCUR_NORMAL] = None; /* inherit from root */
811 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
812 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
813 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
814 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
815 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
816 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
817 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
818 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
819 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
820 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
821 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
822 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
823 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
824 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
826 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
827 GC gc = XCreateGC(dpy, cur, 0, NULL);
828 XColor black;
829 memset(&black, 0, sizeof(XColor));
830 XSetForeground(dpy, gc, 0);
831 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
832 XFreeGC(dpy, gc);
833 wCursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
834 XFreePixmap(dpy, cur);
837 /* signal handler stuff that gets called when a signal is caught */
838 WMAddPersistentTimerHandler(500, delayedAction, NULL);
840 /* emergency exit... */
841 sig_action.sa_handler = handleSig;
842 sigemptyset(&sig_action.sa_mask);
844 sig_action.sa_flags = SA_RESTART;
845 sigaction(SIGQUIT, &sig_action, NULL);
846 sigaction(SIGSEGV, &sig_action, NULL);
847 sigaction(SIGBUS, &sig_action, NULL);
848 sigaction(SIGFPE, &sig_action, NULL);
849 sigaction(SIGABRT, &sig_action, NULL);
851 sig_action.sa_handler = handleExitSig;
853 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
854 * immediately. -Dan */
855 sig_action.sa_flags = SA_RESTART;
856 sigaction(SIGTERM, &sig_action, NULL);
857 sigaction(SIGINT, &sig_action, NULL);
858 sigaction(SIGHUP, &sig_action, NULL);
859 sigaction(SIGUSR1, &sig_action, NULL);
860 sigaction(SIGUSR2, &sig_action, NULL);
862 /* ignore dead pipe */
863 /* Because POSIX mandates that only signal with handlers are reset
864 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
865 * to children. Hence the dummy handler.
866 * Philippe Troin <phil@fifi.org>
868 sig_action.sa_handler = &dummyHandler;
869 sig_action.sa_flags = SA_RESTART;
870 sigaction(SIGPIPE, &sig_action, NULL);
872 /* handle dead children */
873 sig_action.sa_handler = buryChild;
874 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
875 sigaction(SIGCHLD, &sig_action, NULL);
877 /* Now we unblock all signals, that may have been blocked by the parent
878 * who exec()-ed us. This can happen for example if Window Maker crashes
879 * and restarts itself or another window manager from the signal handler.
880 * In this case, the new proccess inherits the blocked signal mask and
881 * will no longer react to that signal, until unblocked.
882 * This is because the signal handler of the proccess who crashed (parent)
883 * didn't return, and the signal remained blocked. -Dan
885 sigfillset(&sig_action.sa_mask);
886 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
888 /* handle X shutdowns a such */
889 XSetIOErrorHandler(handleXIO);
891 /* set hook for out event dispatcher in WINGs event dispatcher */
892 WMHookEventHandler(DispatchEvent);
894 /* initialize defaults stuff */
895 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
896 if (!WDWindowMaker->dictionary) {
897 wwarning(_("could not read domain \"%s\" from defaults database"),
898 "WindowMaker");
901 /* read defaults that don't change until a restart and are
902 * screen independent */
903 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
905 /* check sanity of some values */
906 if (wPreferences.icon_size < 16) {
907 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
908 wPreferences.icon_size);
909 wPreferences.icon_size = 16;
912 /* init other domains */
913 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
914 if (!WDRootMenu->dictionary) {
915 wwarning(_("could not read domain \"%s\" from defaults database"),
916 "WMRootMenu");
918 wDefaultsMergeGlobalMenus(WDRootMenu);
920 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
921 if (!WDWindowAttributes->dictionary) {
922 wwarning(_("could not read domain \"%s\" from defaults database"),
923 "WMWindowAttributes");
926 XSetErrorHandler((XErrorHandler)catchXError);
928 #ifdef SHAPE
929 /* ignore j */
930 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
931 #endif
933 #ifdef KEEP_XKB_LOCK_STATUS
934 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
935 if(wPreferences.modelock && !wXkbSupported) {
936 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
937 wPreferences.modelock = 0;
939 #endif
941 if (defaultScreenOnly) {
942 max = 1;
943 } else {
944 max = ScreenCount(dpy);
946 wScreen = wmalloc(sizeof(WScreen*)*max);
948 wScreenCount = 0;
950 /* manage the screens */
951 for (j = 0; j < max; j++) {
952 if (defaultScreenOnly || max==1) {
953 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
954 if (!wScreen[wScreenCount]) {
955 wfatal(_("it seems that there is already a window manager running"));
956 Exit(1);
958 } else {
959 wScreen[wScreenCount] = wScreenInit(j);
960 if (!wScreen[wScreenCount]) {
961 wwarning(_("could not manage screen %i"), j);
962 continue;
965 wScreenCount++;
968 #ifndef LITE
969 InitializeSwitchMenu();
970 #endif
972 /* initialize/restore state for the screens */
973 for (j = 0; j < wScreenCount; j++) {
974 int crashed;
976 /* restore workspace state */
977 if (!getWorkspaceState(wScreen[j]->root_win, &ws_state)) {
978 ws_state = NULL;
981 wScreenRestoreState(wScreen[j]);
983 /* manage all windows that were already here before us */
984 if (!wPreferences.flags.nodock && wScreen[j]->dock)
985 wScreen[j]->last_dock = wScreen[j]->dock;
987 if (ws_state && (ws_state->flags & WFLAGS_CRASHED)!=0)
988 crashed = 1;
989 else
990 crashed = 0;
992 manageAllWindows(wScreen[j], crashed);
994 /* restore saved menus */
995 wMenuRestoreState(wScreen[j]);
997 /* If we're not restarting restore session */
998 if (ws_state == NULL && !wPreferences.flags.norestore)
999 wSessionRestoreState(wScreen[j]);
1001 if (!wPreferences.flags.noautolaunch) {
1002 /* auto-launch apps */
1003 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
1004 wScreen[j]->last_dock = wScreen[j]->dock;
1005 wDockDoAutoLaunch(wScreen[j]->dock, 0);
1007 /* auto-launch apps in clip */
1008 if (!wPreferences.flags.noclip) {
1009 int i;
1010 for(i=0; i<wScreen[j]->workspace_count; i++) {
1011 if (wScreen[j]->workspaces[i]->clip) {
1012 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
1013 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
1019 /* go to workspace where we were before restart */
1020 if (ws_state) {
1021 wWorkspaceForceChange(wScreen[j], ws_state->workspace);
1022 wfree(ws_state);
1023 } else {
1024 wSessionRestoreLastWorkspace(wScreen[j]);
1027 #ifdef KWM_HINTS
1028 wKWMSetInitializedHint(wScreen[j]);
1029 #endif
1032 if (wScreenCount == 0) {
1033 wfatal(_("could not manage any screen"));
1034 Exit(1);
1037 if (!wPreferences.flags.nopolling && !wPreferences.flags.noupdates) {
1038 /* setup defaults file polling */
1039 WMAddTimerHandler(3000, wDefaultsCheckDomains, NULL);
1046 static Bool
1047 windowInList(Window window, Window *list, int count)
1049 for (; count>=0; count--) {
1050 if (window == list[count])
1051 return True;
1053 return False;
1057 *-----------------------------------------------------------------------
1058 * manageAllWindows--
1059 * Manages all windows in the screen.
1061 * Notes:
1062 * Called when the wm is being started.
1063 * No events can be processed while the windows are being
1064 * reparented/managed.
1065 *-----------------------------------------------------------------------
1067 static void
1068 manageAllWindows(WScreen *scr, int crashRecovery)
1070 Window root, parent;
1071 Window *children;
1072 unsigned int nchildren;
1073 unsigned int i, j;
1074 WWindow *wwin;
1076 XGrabServer(dpy);
1077 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
1079 scr->flags.startup = 1;
1081 /* first remove all icon windows */
1082 for (i = 0; i < nchildren; i++) {
1083 XWMHints *wmhints;
1085 if (children[i]==None)
1086 continue;
1088 wmhints = XGetWMHints(dpy, children[i]);
1089 if (wmhints && (wmhints->flags & IconWindowHint)) {
1090 for (j = 0; j < nchildren; j++) {
1091 if (children[j] == wmhints->icon_window) {
1092 XFree(wmhints);
1093 wmhints = NULL;
1094 children[j] = None;
1095 break;
1099 if (wmhints) {
1100 XFree(wmhints);
1105 for (i = 0; i < nchildren; i++) {
1106 if (children[i] == None)
1107 continue;
1109 #ifdef KWM_HINTS
1110 wKWMCheckModule(scr, children[i]);
1111 #endif
1112 wwin = wManageWindow(scr, children[i]);
1113 if (wwin) {
1114 /* apply states got from WSavedState */
1115 /* shaded + minimized is not restored correctly */
1116 if (wwin->flags.shaded) {
1117 wwin->flags.shaded = 0;
1118 wShadeWindow(wwin);
1120 if (wwin->flags.miniaturized
1121 && (wwin->transient_for == None
1122 || wwin->transient_for == scr->root_win
1123 || !windowInList(wwin->transient_for, children,
1124 nchildren))) {
1126 wwin->flags.skip_next_animation = 1;
1127 wwin->flags.miniaturized = 0;
1128 wIconifyWindow(wwin);
1129 } else {
1130 wClientSetState(wwin, NormalState, None);
1132 if (crashRecovery) {
1133 int border;
1135 border = (!HAS_BORDER(wwin) ? 0 : FRAME_BORDER_WIDTH);
1137 wWindowMove(wwin, wwin->frame_x - border,
1138 wwin->frame_y - border -
1139 (wwin->frame->titlebar ?
1140 wwin->frame->titlebar->height : 0));
1144 XUngrabServer(dpy);
1146 /* hide apps */
1147 wwin = scr->focused_window;
1148 while (wwin) {
1149 if (wwin->flags.hidden) {
1150 WApplication *wapp = wApplicationOf(wwin->main_window);
1152 if (wapp) {
1153 wwin->flags.hidden = 0;
1154 wHideApplication(wapp);
1155 } else {
1156 wwin->flags.hidden = 0;
1159 wwin = wwin->prev;
1162 XFree(children);
1163 scr->flags.startup = 0;
1164 scr->flags.startup2 = 1;
1166 while (XPending(dpy)) {
1167 XEvent ev;
1168 WMNextEvent(dpy, &ev);
1169 WMHandleEvent(&ev);
1171 wWorkspaceForceChange(scr, 0);
1172 if (!wPreferences.flags.noclip)
1173 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1174 scr->flags.startup2 = 0;