Use inotify to check for changes to the defaults database. Workaround for
[wmaker-crm.git] / src / startup.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/*
9af1c6c4 dan1998-10-21 14:43:47 +00002 * Window Maker window manager
59eb9475 dan1998-11-03 12:53:26 +00003 *
4153e2fd dan2003-01-16 23:30:45 +00004 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 Dan Pascu
59eb9475 dan1998-11-03 12:53:26 +00006 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00007 * 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.
11 *
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.
16 *
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
6830b057 dan2004-10-12 21:28:27 +000019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000020 * USA.
21 */
22
23#include "wconfig.h"
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
c0317dda dan2005-08-22 23:58:19 +000028#include <string.h>
bb6182f4 dan2000-11-29 05:15:59 +000029#include <errno.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000030#include <signal.h>
31#include <sys/wait.h>
32#ifdef __FreeBSD__
33#include <sys/signal.h>
34#endif
35
36#include <X11/Xlib.h>
37#include <X11/Xutil.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000038#include <X11/cursorfont.h>
39#include <X11/Xproto.h>
40#include <X11/keysym.h>
9af1c6c4 dan1998-10-21 14:43:47 +000041#ifdef SHAPE
42#include <X11/extensions/shape.h>
43#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +000044
45#include "WindowMaker.h"
46#include "GNUstep.h"
9d2e6ef9 scottc1998-09-29 22:36:29 +000047#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"
088c0ac2 dan1999-03-09 14:58:01 +000059#include "properties.h"
52917e53 dan1999-04-16 20:05:26 +000060#include "dialog.h"
0d9b73cd dan2004-10-25 02:23:41 +000061#include "wmspec.h"
d4025072 id1999-05-17 20:21:43 +000062#ifdef XDND
63#include "xdnd.h"
64#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +000065
66#include "xutil.h"
67
9d2e6ef9 scottc1998-09-29 22:36:29 +000068#if 0
69#ifdef SYS_SIGLIST_DECLARED
70extern const char * const sys_siglist[];
71#endif
72#endif
73
9007a657 dan1998-11-23 11:32:19 +000074/* for SunOS */
75#ifndef SA_RESTART
76# define SA_RESTART 0
77#endif
78
4eb26a0b dan2000-11-25 04:13:39 +000079/* Just in case, for weirdo systems */
80#ifndef SA_NODEFER
81# define SA_NODEFER 0
82#endif
9007a657 dan1998-11-23 11:32:19 +000083
9d2e6ef9 scottc1998-09-29 22:36:29 +000084/****** Global Variables ******/
85
86extern WPreferences wPreferences;
87
88extern WDDomain *WDWindowMaker;
89extern WDDomain *WDRootMenu;
90extern WDDomain *WDWindowAttributes;
91
92extern WShortKey wKeyBindings[WKBD_LAST];
93
94extern int wScreenCount;
95
96
9af1c6c4 dan1998-10-21 14:43:47 +000097#ifdef SHAPE
98extern Bool wShapeSupported;
99extern int wShapeEventBase;
100#endif
101
daaccf6f id1999-05-23 23:48:08 +0000102#ifdef KEEP_XKB_LOCK_STATUS
103extern Bool wXkbSupported;
104extern int wXkbEventBase;
105#endif
106
9d2e6ef9 scottc1998-09-29 22:36:29 +0000107/* contexts */
108extern XContext wWinContext;
109extern XContext wAppWinContext;
110extern XContext wStackContext;
9aca0d5f dan2004-10-12 01:34:32 +0000111extern XContext wVEdgeContext;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000112
113/* atoms */
114extern Atom _XA_WM_STATE;
115extern Atom _XA_WM_CHANGE_STATE;
116extern Atom _XA_WM_PROTOCOLS;
117extern Atom _XA_WM_TAKE_FOCUS;
118extern Atom _XA_WM_DELETE_WINDOW;
119extern Atom _XA_WM_SAVE_YOURSELF;
120extern Atom _XA_WM_CLIENT_LEADER;
121extern Atom _XA_WM_COLORMAP_WINDOWS;
59eb9475 dan1998-11-03 12:53:26 +0000122extern Atom _XA_WM_COLORMAP_NOTIFY;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000123
124extern Atom _XA_GNUSTEP_WM_ATTR;
125
126extern Atom _XA_WINDOWMAKER_MENU;
127extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
128extern Atom _XA_WINDOWMAKER_STATE;
129extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
416e3a82 dan1999-01-25 19:06:50 +0000130extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
9df0e167 kojima1999-04-03 03:51:17 +0000131extern Atom _XA_WINDOWMAKER_COMMAND;
754230a6 kojima2000-02-16 18:26:15 +0000132extern Atom _XA_WINDOWMAKER_ICON_SIZE;
133extern Atom _XA_WINDOWMAKER_ICON_TILE;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000134
9af1c6c4 dan1998-10-21 14:43:47 +0000135extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
0af24483 richard2000-03-02 07:36:02 +0000136extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000137
9d2e6ef9 scottc1998-09-29 22:36:29 +0000138
139/* cursors */
140extern Cursor wCursor[WCUR_LAST];
141
142/* special flags */
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100143/*extern char WDelayedActionSet;*/
9d2e6ef9 scottc1998-09-29 22:36:29 +0000144
145/***** Local *****/
146
147static WScreen **wScreen = NULL;
148
149
150static unsigned int _NumLockMask = 0;
151static unsigned int _ScrollLockMask = 0;
152
153
154
c3d2a890 dan2001-04-18 00:25:58 +0000155static void manageAllWindows(WScreen *scr, int crashed);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000156
157extern void NotifyDeadProcess(pid_t pid, unsigned char status);
158
9d2e6ef9 scottc1998-09-29 22:36:29 +0000159
6830b057 dan2004-10-12 21:28:27 +0000160static int
9d2e6ef9 scottc1998-09-29 22:36:29 +0000161catchXError(Display *dpy, XErrorEvent *error)
162{
163 char buffer[MAXLINE];
6830b057 dan2004-10-12 21:28:27 +0000164
9d2e6ef9 scottc1998-09-29 22:36:29 +0000165 /* ignore some errors */
6830b057 dan2004-10-12 21:28:27 +0000166 if (error->resourceid != None
167 && ((error->error_code == BadDrawable
168 && error->request_code == X_GetGeometry)
169 || (error->error_code == BadMatch
170 && (error->request_code == X_SetInputFocus))
171 || (error->error_code == BadWindow)
172 /*
173 && (error->request_code == X_GetWindowAttributes
174 || error->request_code == X_SetInputFocus
175 || error->request_code == X_ChangeWindowAttributes
176 || error->request_code == X_GetProperty
177 || error->request_code == X_ChangeProperty
178 || error->request_code == X_QueryTree
179 || error->request_code == X_GrabButton
180 || error->request_code == X_UngrabButton
181 || error->request_code == X_SendEvent
182 || error->request_code == X_ConfigureWindow))
183 */
184 || (error->request_code == X_InstallColormap))) {
9d2e6ef9 scottc1998-09-29 22:36:29 +0000185#ifndef DEBUG
9af1c6c4 dan1998-10-21 14:43:47 +0000186
6830b057 dan2004-10-12 21:28:27 +0000187 return 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000188#else
6830b057 dan2004-10-12 21:28:27 +0000189 printf("got X error %x %x %x\n", error->request_code,
190 error->error_code, (unsigned)error->resourceid);
191 return 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000192#endif
193 }
9af1c6c4 dan1998-10-21 14:43:47 +0000194 FormatXError(dpy, error, buffer, MAXLINE);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000195 wwarning(_("internal X error: %s\n"), buffer);
196 return -1;
197}
198
199
200/*
6830b057 dan2004-10-12 21:28:27 +0000201 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000202 * handleXIO-
6830b057 dan2004-10-12 21:28:27 +0000203 * Handle X shutdowns and other stuff.
204 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000205 */
206static int
cef3b29d dan1999-04-20 20:56:30 +0000207handleXIO(Display *xio_dpy)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000208{
e7495baf dan1999-02-17 11:06:40 +0000209 dpy = NULL;
9007a657 dan1998-11-23 11:32:19 +0000210 Exit(0);
0261c326 dan1999-01-06 15:22:33 +0000211 return 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000212}
213
214
9d2e6ef9 scottc1998-09-29 22:36:29 +0000215/*
216 *----------------------------------------------------------------------
217 * delayedAction-
218 * Action to be executed after the signal() handler is exited.
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100219 * This was called every 500ms to 'clean up' signals. Not used now.
9d2e6ef9 scottc1998-09-29 22:36:29 +0000220 *----------------------------------------------------------------------
221 */
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100222#ifdef notused
9d2e6ef9 scottc1998-09-29 22:36:29 +0000223static void
224delayedAction(void *cdata)
225{
7f8d5155 kojima2001-03-14 04:19:06 +0000226 if (WDelayedActionSet == 0) {
6830b057 dan2004-10-12 21:28:27 +0000227 return;
7f8d5155 kojima2001-03-14 04:19:06 +0000228 }
229 WDelayedActionSet--;
6830b057 dan2004-10-12 21:28:27 +0000230 /*
9d2e6ef9 scottc1998-09-29 22:36:29 +0000231 * Make the event dispatcher do whatever it needs to do,
6830b057 dan2004-10-12 21:28:27 +0000232 * including handling zombie processes, restart and exit
9d2e6ef9 scottc1998-09-29 22:36:29 +0000233 * signals.
234 */
235 DispatchEvent(NULL);
236}
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100237#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000238
9d2e6ef9 scottc1998-09-29 22:36:29 +0000239/*
240 *----------------------------------------------------------------------
51b1bf34 kojima2001-03-14 02:54:54 +0000241 * handleExitSig--
242 * User generated exit signal handler.
6830b057 dan2004-10-12 21:28:27 +0000243 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000244 */
245static RETSIGTYPE
51b1bf34 kojima2001-03-14 02:54:54 +0000246handleExitSig(int sig)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000247{
51b1bf34 kojima2001-03-14 02:54:54 +0000248 sigset_t sigs;
249
250 sigfillset(&sigs);
251 sigprocmask(SIG_BLOCK, &sigs, NULL);
6830b057 dan2004-10-12 21:28:27 +0000252
416e3a82 dan1999-01-25 19:06:50 +0000253 if (sig == SIGUSR1) {
9d2e6ef9 scottc1998-09-29 22:36:29 +0000254#ifdef SYS_SIGLIST_DECLARED
3ed1d3e2 kojima2004-10-13 23:39:29 +0000255 wwarning("got signal %i (%s) - restarting\n", sig, sys_siglist[sig]);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000256#else
3ed1d3e2 kojima2004-10-13 23:39:29 +0000257 wwarning("got signal %i - restarting\n", sig);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000258#endif
259
6830b057 dan2004-10-12 21:28:27 +0000260 SIG_WCHANGE_STATE(WSTATE_NEED_RESTART);
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100261
4637c09d dan2001-01-30 03:04:02 +0000262 } else if (sig == SIGUSR2) {
263#ifdef SYS_SIGLIST_DECLARED
3ed1d3e2 kojima2004-10-13 23:39:29 +0000264 wwarning("got signal %i (%s) - rereading defaults\n", sig, sys_siglist[sig]);
4637c09d dan2001-01-30 03:04:02 +0000265#else
3ed1d3e2 kojima2004-10-13 23:39:29 +0000266 wwarning("got signal %i - rereading defaults\n", sig);
4637c09d dan2001-01-30 03:04:02 +0000267#endif
268
51b1bf34 kojima2001-03-14 02:54:54 +0000269 SIG_WCHANGE_STATE(WSTATE_NEED_REREAD);
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100270
7688a9f6 dan2001-11-16 07:45:33 +0000271 } else if (sig==SIGTERM || sig==SIGINT || sig==SIGHUP) {
416e3a82 dan1999-01-25 19:06:50 +0000272#ifdef SYS_SIGLIST_DECLARED
3ed1d3e2 kojima2004-10-13 23:39:29 +0000273 wwarning("got signal %i (%s) - exiting...\n", sig, sys_siglist[sig]);
416e3a82 dan1999-01-25 19:06:50 +0000274#else
3ed1d3e2 kojima2004-10-13 23:39:29 +0000275 wwarning("got signal %i - exiting...\n", sig);
416e3a82 dan1999-01-25 19:06:50 +0000276#endif
9af1c6c4 dan1998-10-21 14:43:47 +0000277
6830b057 dan2004-10-12 21:28:27 +0000278 SIG_WCHANGE_STATE(WSTATE_NEED_EXIT);
9af1c6c4 dan1998-10-21 14:43:47 +0000279
9d2e6ef9 scottc1998-09-29 22:36:29 +0000280 }
6830b057 dan2004-10-12 21:28:27 +0000281
51b1bf34 kojima2001-03-14 02:54:54 +0000282 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100283 DispatchEvent(NULL); /* Dispatch events imediately.*/
51b1bf34 kojima2001-03-14 02:54:54 +0000284}
285
71739224 dan2001-09-01 13:30:55 +0000286/* Dummy signal handler */
287static void
288dummyHandler(int sig)
289{
290}
291
292
51b1bf34 kojima2001-03-14 02:54:54 +0000293/*
294 *----------------------------------------------------------------------
295 * handleSig--
296 * general signal handler. Exits the program gently.
6830b057 dan2004-10-12 21:28:27 +0000297 *----------------------------------------------------------------------
51b1bf34 kojima2001-03-14 02:54:54 +0000298 */
299static RETSIGTYPE
300handleSig(int sig)
301{
9d2e6ef9 scottc1998-09-29 22:36:29 +0000302#ifdef SYS_SIGLIST_DECLARED
879b00a5 kojima2004-10-13 05:09:08 +0000303 wfatal("got signal %i (%s)\n", sig, sys_siglist[sig]);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000304#else
879b00a5 kojima2004-10-13 05:09:08 +0000305 wfatal("got signal %i\n", sig);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000306#endif
9af1c6c4 dan1998-10-21 14:43:47 +0000307
c98f229d dan1999-04-16 01:12:45 +0000308 /* Setting the signal behaviour back to default and then reraising the
309 * signal is a cleaner way to make program exit and core dump than calling
310 * abort(), since it correctly returns from the signal handler and sets
311 * the flags accordingly. -Dan
312 */
0261c326 dan1999-01-06 15:22:33 +0000313 if (sig==SIGSEGV || sig==SIGFPE || sig==SIGBUS || sig==SIGILL
6830b057 dan2004-10-12 21:28:27 +0000314 || sig==SIGABRT) {
c98f229d dan1999-04-16 01:12:45 +0000315 signal(sig, SIG_DFL);
7f36af4f kojima1999-05-31 20:34:00 +0000316 kill(getpid(), sig);
c98f229d dan1999-04-16 01:12:45 +0000317 return;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000318 }
9af1c6c4 dan1998-10-21 14:43:47 +0000319
879b00a5 kojima2004-10-13 05:09:08 +0000320 wAbort(0);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000321}
322
323
324static RETSIGTYPE
9d2e6ef9 scottc1998-09-29 22:36:29 +0000325buryChild(int foo)
326{
327 pid_t pid;
328 int status;
51b1bf34 kojima2001-03-14 02:54:54 +0000329 int save_errno = errno;
330 sigset_t sigs;
331
332 sigfillset(&sigs);
333 /* Block signals so that NotifyDeadProcess() doesn't get fux0red */
334 sigprocmask(SIG_BLOCK, &sigs, NULL);
6830b057 dan2004-10-12 21:28:27 +0000335
9d2e6ef9 scottc1998-09-29 22:36:29 +0000336 /* R.I.P. */
bb6182f4 dan2000-11-29 05:15:59 +0000337 /* If 2 or more kids exit in a small time window, before this handler gets
338 * the chance to get invoked, the SIGCHLD signals will be merged and only
339 * one SIGCHLD signal will be sent to us. We use a while loop to get all
340 * exited child status because we can't count on the number of SIGCHLD
341 * signals to know exactly how many kids have exited. -Dan
4eb26a0b dan2000-11-25 04:13:39 +0000342 */
bb6182f4 dan2000-11-29 05:15:59 +0000343 while ((pid=waitpid(-1, &status, WNOHANG))>0 || (pid<0 && errno==EINTR)) {
6830b057 dan2004-10-12 21:28:27 +0000344 NotifyDeadProcess(pid, WEXITSTATUS(status));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000345 }
51b1bf34 kojima2001-03-14 02:54:54 +0000346
51b1bf34 kojima2001-03-14 02:54:54 +0000347 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
348
6830b057 dan2004-10-12 21:28:27 +0000349 errno = save_errno;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000350}
351
352
9d2e6ef9 scottc1998-09-29 22:36:29 +0000353static void
354getOffendingModifiers()
355{
356 int i;
357 XModifierKeymap *modmap;
358 KeyCode nlock, slock;
0261c326 dan1999-01-06 15:22:33 +0000359 static int mask_table[8] = {
6830b057 dan2004-10-12 21:28:27 +0000360 ShiftMask,LockMask,ControlMask,Mod1Mask,
361 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
9d2e6ef9 scottc1998-09-29 22:36:29 +0000362 };
363
364 nlock = XKeysymToKeycode(dpy, XK_Num_Lock);
365 slock = XKeysymToKeycode(dpy, XK_Scroll_Lock);
366
367 /*
368 * Find out the masks for the NumLock and ScrollLock modifiers,
369 * so that we can bind the grabs for when they are enabled too.
370 */
371 modmap = XGetModifierMapping(dpy);
6830b057 dan2004-10-12 21:28:27 +0000372
9d2e6ef9 scottc1998-09-29 22:36:29 +0000373 if (modmap!=NULL && modmap->max_keypermod>0) {
6830b057 dan2004-10-12 21:28:27 +0000374 for (i=0; i<8*modmap->max_keypermod; i++) {
375 if (modmap->modifiermap[i]==nlock && nlock!=0)
376 _NumLockMask = mask_table[i/modmap->max_keypermod];
377 else if (modmap->modifiermap[i]==slock && slock!=0)
378 _ScrollLockMask = mask_table[i/modmap->max_keypermod];
379 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000380 }
381
382 if (modmap)
6830b057 dan2004-10-12 21:28:27 +0000383 XFreeModifiermap(modmap);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000384}
385
386
387
388#ifdef NUMLOCK_HACK
6830b057 dan2004-10-12 21:28:27 +0000389void
9d2e6ef9 scottc1998-09-29 22:36:29 +0000390wHackedGrabKey(int keycode, unsigned int modifiers,
6830b057 dan2004-10-12 21:28:27 +0000391 Window grab_window, Bool owner_events, int pointer_mode,
392 int keyboard_mode)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000393{
394 if (modifiers == AnyModifier)
6830b057 dan2004-10-12 21:28:27 +0000395 return;
396
9d2e6ef9 scottc1998-09-29 22:36:29 +0000397 /* grab all combinations of the modifier with CapsLock, NumLock and
398 * ScrollLock. How much memory/CPU does such a monstrosity consume
399 * in the server?
400 */
401 if (_NumLockMask)
6830b057 dan2004-10-12 21:28:27 +0000402 XGrabKey(dpy, keycode, modifiers|_NumLockMask,
403 grab_window, owner_events, pointer_mode, keyboard_mode);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000404 if (_ScrollLockMask)
6830b057 dan2004-10-12 21:28:27 +0000405 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask,
406 grab_window, owner_events, pointer_mode, keyboard_mode);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000407 if (_NumLockMask && _ScrollLockMask)
6830b057 dan2004-10-12 21:28:27 +0000408 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask,
409 grab_window, owner_events, pointer_mode, keyboard_mode);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000410 if (_NumLockMask)
6830b057 dan2004-10-12 21:28:27 +0000411 XGrabKey(dpy, keycode, modifiers|_NumLockMask|LockMask,
412 grab_window, owner_events, pointer_mode, keyboard_mode);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000413 if (_ScrollLockMask)
6830b057 dan2004-10-12 21:28:27 +0000414 XGrabKey(dpy, keycode, modifiers|_ScrollLockMask|LockMask,
415 grab_window, owner_events, pointer_mode, keyboard_mode);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000416 if (_NumLockMask && _ScrollLockMask)
6830b057 dan2004-10-12 21:28:27 +0000417 XGrabKey(dpy, keycode, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
418 grab_window, owner_events, pointer_mode, keyboard_mode);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000419 /* phew, I guess that's all, right? */
420}
9af1c6c4 dan1998-10-21 14:43:47 +0000421#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000422
6830b057 dan2004-10-12 21:28:27 +0000423void
424wHackedGrabButton(unsigned int button, unsigned int modifiers,
425 Window grab_window, Bool owner_events,
426 unsigned int event_mask, int pointer_mode,
427 int keyboard_mode, Window confine_to, Cursor cursor)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000428{
9af1c6c4 dan1998-10-21 14:43:47 +0000429 XGrabButton(dpy, button, modifiers, grab_window, owner_events,
6830b057 dan2004-10-12 21:28:27 +0000430 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
9af1c6c4 dan1998-10-21 14:43:47 +0000431
9d2e6ef9 scottc1998-09-29 22:36:29 +0000432 if (modifiers==AnyModifier)
6830b057 dan2004-10-12 21:28:27 +0000433 return;
9af1c6c4 dan1998-10-21 14:43:47 +0000434
435 XGrabButton(dpy, button, modifiers|LockMask, grab_window, owner_events,
6830b057 dan2004-10-12 21:28:27 +0000436 event_mask, pointer_mode, keyboard_mode, confine_to, cursor);
437
438#ifdef NUMLOCK_HACK
9d2e6ef9 scottc1998-09-29 22:36:29 +0000439 /* same as above, but for mouse buttons */
440 if (_NumLockMask)
6830b057 dan2004-10-12 21:28:27 +0000441 XGrabButton(dpy, button, modifiers|_NumLockMask,
442 grab_window, owner_events, event_mask, pointer_mode,
443 keyboard_mode, confine_to, cursor);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000444 if (_ScrollLockMask)
6830b057 dan2004-10-12 21:28:27 +0000445 XGrabButton(dpy, button, modifiers|_ScrollLockMask,
446 grab_window, owner_events, event_mask, pointer_mode,
447 keyboard_mode, confine_to, cursor);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000448 if (_NumLockMask && _ScrollLockMask)
6830b057 dan2004-10-12 21:28:27 +0000449 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask,
450 grab_window, owner_events, event_mask, pointer_mode,
451 keyboard_mode, confine_to, cursor);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000452 if (_NumLockMask)
6830b057 dan2004-10-12 21:28:27 +0000453 XGrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
454 grab_window, owner_events, event_mask, pointer_mode,
455 keyboard_mode, confine_to, cursor);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000456 if (_ScrollLockMask)
6830b057 dan2004-10-12 21:28:27 +0000457 XGrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
458 grab_window, owner_events, event_mask, pointer_mode,
459 keyboard_mode, confine_to, cursor);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000460 if (_NumLockMask && _ScrollLockMask)
6830b057 dan2004-10-12 21:28:27 +0000461 XGrabButton(dpy, button, modifiers|_ScrollLockMask|_NumLockMask|LockMask,
462 grab_window, owner_events, event_mask, pointer_mode,
463 keyboard_mode, confine_to, cursor);
9af1c6c4 dan1998-10-21 14:43:47 +0000464#endif /* NUMLOCK_HACK */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000465}
466
9af1c6c4 dan1998-10-21 14:43:47 +0000467#ifdef notused
9d2e6ef9 scottc1998-09-29 22:36:29 +0000468void
6830b057 dan2004-10-12 21:28:27 +0000469wHackedUngrabButton(unsigned int button, unsigned int modifiers,
470 Window grab_window)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000471{
472 XUngrabButton(dpy, button, modifiers|_NumLockMask,
6830b057 dan2004-10-12 21:28:27 +0000473 grab_window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000474 XUngrabButton(dpy, button, modifiers|_ScrollLockMask,
6830b057 dan2004-10-12 21:28:27 +0000475 grab_window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000476 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask,
6830b057 dan2004-10-12 21:28:27 +0000477 grab_window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000478 XUngrabButton(dpy, button, modifiers|_NumLockMask|LockMask,
6830b057 dan2004-10-12 21:28:27 +0000479 grab_window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000480 XUngrabButton(dpy, button, modifiers|_ScrollLockMask|LockMask,
6830b057 dan2004-10-12 21:28:27 +0000481 grab_window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000482 XUngrabButton(dpy, button, modifiers|_NumLockMask|_ScrollLockMask|LockMask,
6830b057 dan2004-10-12 21:28:27 +0000483 grab_window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000484}
9af1c6c4 dan1998-10-21 14:43:47 +0000485#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000486
487
488
489WScreen*
490wScreenWithNumber(int i)
491{
492 assert(i < wScreenCount);
e7495baf dan1999-02-17 11:06:40 +0000493
9d2e6ef9 scottc1998-09-29 22:36:29 +0000494 return wScreen[i];
495}
496
e7495baf dan1999-02-17 11:06:40 +0000497
9d2e6ef9 scottc1998-09-29 22:36:29 +0000498WScreen*
499wScreenForRootWindow(Window window)
500{
501 int i;
502
503 if (wScreenCount==1)
6830b057 dan2004-10-12 21:28:27 +0000504 return wScreen[0];
9d2e6ef9 scottc1998-09-29 22:36:29 +0000505
506 /*
507 * Since the number of heads will probably be small (normally 2),
508 * it should be faster to use this than a hash table, because
509 * of the overhead.
510 */
511 for (i=0; i<wScreenCount; i++) {
6830b057 dan2004-10-12 21:28:27 +0000512 if (wScreen[i]->root_win == window) {
513 return wScreen[i];
514 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000515 }
516
80fefd29 kojima2001-09-03 22:31:30 +0000517 return wScreenForWindow(window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000518}
519
e7495baf dan1999-02-17 11:06:40 +0000520
521WScreen*
522wScreenSearchForRootWindow(Window window)
523{
524 int i;
525
526 if (wScreenCount==1)
6830b057 dan2004-10-12 21:28:27 +0000527 return wScreen[0];
e7495baf dan1999-02-17 11:06:40 +0000528
529 /*
530 * Since the number of heads will probably be small (normally 2),
531 * it should be faster to use this than a hash table, because
532 * of the overhead.
533 */
534 for (i=0; i<wScreenCount; i++) {
6830b057 dan2004-10-12 21:28:27 +0000535 if (wScreen[i]->root_win == window) {
536 return wScreen[i];
537 }
e7495baf dan1999-02-17 11:06:40 +0000538 }
6830b057 dan2004-10-12 21:28:27 +0000539
80fefd29 kojima2001-09-03 22:31:30 +0000540 return wScreenForWindow(window);
e7495baf dan1999-02-17 11:06:40 +0000541}
542
543
9d2e6ef9 scottc1998-09-29 22:36:29 +0000544WScreen*
545wScreenForWindow(Window window)
546{
547 XWindowAttributes attr;
9af1c6c4 dan1998-10-21 14:43:47 +0000548
549 if (wScreenCount==1)
6830b057 dan2004-10-12 21:28:27 +0000550 return wScreen[0];
9af1c6c4 dan1998-10-21 14:43:47 +0000551
9d2e6ef9 scottc1998-09-29 22:36:29 +0000552 if (XGetWindowAttributes(dpy, window, &attr)) {
6830b057 dan2004-10-12 21:28:27 +0000553 return wScreenForRootWindow(attr.root);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000554 }
555 return NULL;
556}
557
558
34cd5125 kojima2000-01-20 11:51:42 +0000559static char *atomNames[] = {
6830b057 dan2004-10-12 21:28:27 +0000560 "WM_STATE",
561 "WM_CHANGE_STATE",
562 "WM_PROTOCOLS",
563 "WM_TAKE_FOCUS",
564 "WM_DELETE_WINDOW",
565 "WM_SAVE_YOURSELF",
566 "WM_CLIENT_LEADER",
567 "WM_COLORMAP_WINDOWS",
568 "WM_COLORMAP_NOTIFY",
c3d2a890 dan2001-04-18 00:25:58 +0000569
570 "_WINDOWMAKER_MENU",
6830b057 dan2004-10-12 21:28:27 +0000571 "_WINDOWMAKER_STATE",
572 "_WINDOWMAKER_WM_PROTOCOLS",
34cd5125 kojima2000-01-20 11:51:42 +0000573 "_WINDOWMAKER_WM_FUNCTION",
574 "_WINDOWMAKER_NOTICEBOARD",
754230a6 kojima2000-02-16 18:26:15 +0000575 "_WINDOWMAKER_COMMAND",
576 "_WINDOWMAKER_ICON_SIZE",
e8ecd3d5 kojima2000-03-02 21:15:13 +0000577 "_WINDOWMAKER_ICON_TILE",
c3d2a890 dan2001-04-18 00:25:58 +0000578
579 GNUSTEP_WM_ATTR_NAME,
580 GNUSTEP_WM_MINIATURIZE_WINDOW,
e8ecd3d5 kojima2000-03-02 21:15:13 +0000581 GNUSTEP_TITLEBAR_STATE
34cd5125 kojima2000-01-20 11:51:42 +0000582};
583
9d2e6ef9 scottc1998-09-29 22:36:29 +0000584
585/*
586 *----------------------------------------------------------
587 * StartUp--
588 * starts the window manager and setup global data.
589 * Called from main() at startup.
6830b057 dan2004-10-12 21:28:27 +0000590 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000591 * Side effects:
592 * global data declared in main.c is initialized
593 *----------------------------------------------------------
594 */
595void
596StartUp(Bool defaultScreenOnly)
597{
9d2e6ef9 scottc1998-09-29 22:36:29 +0000598 struct sigaction sig_action;
599 int j, max;
34cd5125 kojima2000-01-20 11:51:42 +0000600 Atom atom[sizeof(atomNames)/sizeof(char*)];
9d2e6ef9 scottc1998-09-29 22:36:29 +0000601
602 /*
603 * Ignore CapsLock in modifiers
604 */
605 ValidModMask = 0xff & ~LockMask;
606
607 getOffendingModifiers();
608 /*
609 * Ignore NumLock and ScrollLock too
610 */
611 ValidModMask &= ~(_NumLockMask|_ScrollLockMask);
612
613
614 memset(&wKeyBindings, 0, sizeof(wKeyBindings));
615
616 wWinContext = XUniqueContext();
617 wAppWinContext = XUniqueContext();
618 wStackContext = XUniqueContext();
9aca0d5f dan2004-10-12 01:34:32 +0000619 wVEdgeContext = XUniqueContext();
59eb9475 dan1998-11-03 12:53:26 +0000620
6830b057 dan2004-10-12 21:28:27 +0000621 /* _XA_VERSION = XInternAtom(dpy, "VERSION", False);*/
59eb9475 dan1998-11-03 12:53:26 +0000622
32160ff6 kojima2000-04-01 02:16:14 +0000623#ifdef HAVE_XINTERNATOMS
34cd5125 kojima2000-01-20 11:51:42 +0000624 XInternAtoms(dpy, atomNames, sizeof(atomNames)/sizeof(char*),
6830b057 dan2004-10-12 21:28:27 +0000625 False, atom);
32160ff6 kojima2000-04-01 02:16:14 +0000626#else
627
628 {
6830b057 dan2004-10-12 21:28:27 +0000629 int i;
630 for (i = 0; i < sizeof(atomNames)/sizeof(char*); i++) {
631 atom[i] = XInternAtom(dpy, atomNames[i], False);
632 }
32160ff6 kojima2000-04-01 02:16:14 +0000633 }
634#endif
6830b057 dan2004-10-12 21:28:27 +0000635
34cd5125 kojima2000-01-20 11:51:42 +0000636 _XA_WM_STATE = atom[0];
637 _XA_WM_CHANGE_STATE = atom[1];
638 _XA_WM_PROTOCOLS = atom[2];
639 _XA_WM_TAKE_FOCUS = atom[3];
640 _XA_WM_DELETE_WINDOW = atom[4];
641 _XA_WM_SAVE_YOURSELF = atom[5];
642 _XA_WM_CLIENT_LEADER = atom[6];
643 _XA_WM_COLORMAP_WINDOWS = atom[7];
644 _XA_WM_COLORMAP_NOTIFY = atom[8];
59eb9475 dan1998-11-03 12:53:26 +0000645
c3d2a890 dan2001-04-18 00:25:58 +0000646 _XA_WINDOWMAKER_MENU = atom[9];
647 _XA_WINDOWMAKER_STATE = atom[10];
648 _XA_WINDOWMAKER_WM_PROTOCOLS = atom[11];
649 _XA_WINDOWMAKER_WM_FUNCTION = atom[12];
650 _XA_WINDOWMAKER_NOTICEBOARD = atom[13];
651 _XA_WINDOWMAKER_COMMAND = atom[14];
652 _XA_WINDOWMAKER_ICON_SIZE = atom[15];
653 _XA_WINDOWMAKER_ICON_TILE = atom[16];
654
655 _XA_GNUSTEP_WM_ATTR = atom[17];
656 _XA_GNUSTEP_WM_MINIATURIZE_WINDOW = atom[18];
0f9aa277 richard2000-03-06 22:45:04 +0000657 _XA_GNUSTEP_TITLEBAR_STATE = atom[19];
0af24483 richard2000-03-02 07:36:02 +0000658
d4025072 id1999-05-17 20:21:43 +0000659#ifdef XDND
660 wXDNDInitializeAtoms();
9af1c6c4 dan1998-10-21 14:43:47 +0000661#endif
662
9d2e6ef9 scottc1998-09-29 22:36:29 +0000663
664 /* cursors */
520a6cd0 dan2001-12-21 11:00:45 +0000665 wCursor[WCUR_NORMAL] = None; /* inherit from root */
41334b84 kojima2000-03-03 20:55:35 +0000666 wCursor[WCUR_ROOT] = XCreateFontCursor(dpy, XC_left_ptr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000667 wCursor[WCUR_ARROW] = XCreateFontCursor(dpy, XC_top_left_arrow);
668 wCursor[WCUR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
669 wCursor[WCUR_RESIZE] = XCreateFontCursor(dpy, XC_sizing);
37217ce1 id2000-03-04 01:22:30 +0000670 wCursor[WCUR_TOPLEFTRESIZE] = XCreateFontCursor(dpy, XC_top_left_corner);
671 wCursor[WCUR_TOPRIGHTRESIZE] = XCreateFontCursor(dpy, XC_top_right_corner);
672 wCursor[WCUR_BOTTOMLEFTRESIZE] = XCreateFontCursor(dpy, XC_bottom_left_corner);
673 wCursor[WCUR_BOTTOMRIGHTRESIZE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
674 wCursor[WCUR_VERTICALRESIZE] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
675 wCursor[WCUR_HORIZONRESIZE] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000676 wCursor[WCUR_WAIT] = XCreateFontCursor(dpy, XC_watch);
677 wCursor[WCUR_QUESTION] = XCreateFontCursor(dpy, XC_question_arrow);
678 wCursor[WCUR_TEXT] = XCreateFontCursor(dpy, XC_xterm); /* odd name???*/
a1e552d9 kojima1999-11-05 23:23:48 +0000679 wCursor[WCUR_SELECT] = XCreateFontCursor(dpy, XC_cross);
9aca0d5f dan2004-10-12 01:34:32 +0000680 {
6830b057 dan2004-10-12 21:28:27 +0000681 Pixmap cur = XCreatePixmap(dpy, DefaultRootWindow(dpy), 16, 16, 1);
682 GC gc = XCreateGC(dpy, cur, 0, NULL);
683 XColor black;
684 memset(&black, 0, sizeof(XColor));
685 XSetForeground(dpy, gc, 0);
686 XFillRectangle(dpy, cur, gc, 0, 0, 16, 16);
687 XFreeGC(dpy, gc);
688 wCursor[WCUR_EMPTY] = XCreatePixmapCursor(dpy, cur, cur, &black, &black, 0, 0);
689 XFreePixmap(dpy, cur);
9aca0d5f dan2004-10-12 01:34:32 +0000690 }
6830b057 dan2004-10-12 21:28:27 +0000691
51b1bf34 kojima2001-03-14 02:54:54 +0000692
9d2e6ef9 scottc1998-09-29 22:36:29 +0000693 /* emergency exit... */
694 sig_action.sa_handler = handleSig;
695 sigemptyset(&sig_action.sa_mask);
696
51b1bf34 kojima2001-03-14 02:54:54 +0000697 sig_action.sa_flags = SA_RESTART;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000698 sigaction(SIGQUIT, &sig_action, NULL);
879b00a5 kojima2004-10-13 05:09:08 +0000699 /* instead of catching these, we let the default handler abort the
700 * program. The new monitor process will take appropriate action
701 * when it detects the crash.
9d2e6ef9 scottc1998-09-29 22:36:29 +0000702 sigaction(SIGSEGV, &sig_action, NULL);
703 sigaction(SIGBUS, &sig_action, NULL);
704 sigaction(SIGFPE, &sig_action, NULL);
0261c326 dan1999-01-06 15:22:33 +0000705 sigaction(SIGABRT, &sig_action, NULL);
879b00a5 kojima2004-10-13 05:09:08 +0000706 */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000707
51b1bf34 kojima2001-03-14 02:54:54 +0000708 sig_action.sa_handler = handleExitSig;
6830b057 dan2004-10-12 21:28:27 +0000709
416e3a82 dan1999-01-25 19:06:50 +0000710 /* Here we set SA_RESTART for safety, because SIGUSR1 may not be handled
7688a9f6 dan2001-11-16 07:45:33 +0000711 * immediately. -Dan */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000712 sig_action.sa_flags = SA_RESTART;
7688a9f6 dan2001-11-16 07:45:33 +0000713 sigaction(SIGTERM, &sig_action, NULL);
714 sigaction(SIGINT, &sig_action, NULL);
715 sigaction(SIGHUP, &sig_action, NULL);
416e3a82 dan1999-01-25 19:06:50 +0000716 sigaction(SIGUSR1, &sig_action, NULL);
4637c09d dan2001-01-30 03:04:02 +0000717 sigaction(SIGUSR2, &sig_action, NULL);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000718
719 /* ignore dead pipe */
882b9a8e kojima2001-07-23 20:31:32 +0000720 /* Because POSIX mandates that only signal with handlers are reset
71739224 dan2001-09-01 13:30:55 +0000721 * accross an exec*(), we do not want to propagate ignoring SIGPIPEs
722 * to children. Hence the dummy handler.
723 * Philippe Troin <phil@fifi.org>
724 */
725 sig_action.sa_handler = &dummyHandler;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000726 sig_action.sa_flags = SA_RESTART;
727 sigaction(SIGPIPE, &sig_action, NULL);
728
729 /* handle dead children */
730 sig_action.sa_handler = buryChild;
bb6182f4 dan2000-11-29 05:15:59 +0000731 sig_action.sa_flags = SA_NOCLDSTOP|SA_RESTART;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000732 sigaction(SIGCHLD, &sig_action, NULL);
733
c98f229d dan1999-04-16 01:12:45 +0000734 /* Now we unblock all signals, that may have been blocked by the parent
735 * who exec()-ed us. This can happen for example if Window Maker crashes
736 * and restarts itself or another window manager from the signal handler.
737 * In this case, the new proccess inherits the blocked signal mask and
738 * will no longer react to that signal, until unblocked.
739 * This is because the signal handler of the proccess who crashed (parent)
740 * didn't return, and the signal remained blocked. -Dan
741 */
742 sigfillset(&sig_action.sa_mask);
743 sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, NULL);
744
9d2e6ef9 scottc1998-09-29 22:36:29 +0000745 /* handle X shutdowns a such */
746 XSetIOErrorHandler(handleXIO);
747
748 /* set hook for out event dispatcher in WINGs event dispatcher */
749 WMHookEventHandler(DispatchEvent);
750
751 /* initialize defaults stuff */
752 WDWindowMaker = wDefaultsInitDomain("WindowMaker", True);
753 if (!WDWindowMaker->dictionary) {
6830b057 dan2004-10-12 21:28:27 +0000754 wwarning(_("could not read domain \"%s\" from defaults database"),
755 "WindowMaker");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000756 }
757
758 /* read defaults that don't change until a restart and are
759 * screen independent */
760 wReadStaticDefaults(WDWindowMaker ? WDWindowMaker->dictionary : NULL);
761
762 /* check sanity of some values */
763 if (wPreferences.icon_size < 16) {
6830b057 dan2004-10-12 21:28:27 +0000764 wwarning(_("icon size is configured to %i, but it's too small. Using 16, instead\n"),
765 wPreferences.icon_size);
766 wPreferences.icon_size = 16;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000767 }
768
769 /* init other domains */
770 WDRootMenu = wDefaultsInitDomain("WMRootMenu", False);
771 if (!WDRootMenu->dictionary) {
6830b057 dan2004-10-12 21:28:27 +0000772 wwarning(_("could not read domain \"%s\" from defaults database"),
773 "WMRootMenu");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000774 }
2cf59318 kojima2003-06-06 02:51:47 +0000775 wDefaultsMergeGlobalMenus(WDRootMenu);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000776
777 WDWindowAttributes = wDefaultsInitDomain("WMWindowAttributes", True);
778 if (!WDWindowAttributes->dictionary) {
6830b057 dan2004-10-12 21:28:27 +0000779 wwarning(_("could not read domain \"%s\" from defaults database"),
780 "WMWindowAttributes");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000781 }
9af1c6c4 dan1998-10-21 14:43:47 +0000782
783 XSetErrorHandler((XErrorHandler)catchXError);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000784
9af1c6c4 dan1998-10-21 14:43:47 +0000785#ifdef SHAPE
786 /* ignore j */
787 wShapeSupported = XShapeQueryExtension(dpy, &wShapeEventBase, &j);
788#endif
6830b057 dan2004-10-12 21:28:27 +0000789
daaccf6f id1999-05-23 23:48:08 +0000790#ifdef KEEP_XKB_LOCK_STATUS
791 wXkbSupported = XkbQueryExtension(dpy, NULL, &wXkbEventBase, NULL, NULL, NULL);
792 if(wPreferences.modelock && !wXkbSupported) {
793 wwarning(_("XKB is not supported. KbdModeLock is automatically disabled."));
794 wPreferences.modelock = 0;
795 }
796#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000797
798 if (defaultScreenOnly) {
6830b057 dan2004-10-12 21:28:27 +0000799 max = 1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000800 } else {
6830b057 dan2004-10-12 21:28:27 +0000801 max = ScreenCount(dpy);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000802 }
803 wScreen = wmalloc(sizeof(WScreen*)*max);
9af1c6c4 dan1998-10-21 14:43:47 +0000804
805 wScreenCount = 0;
806
807 /* manage the screens */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000808 for (j = 0; j < max; j++) {
6830b057 dan2004-10-12 21:28:27 +0000809 if (defaultScreenOnly || max==1) {
810 wScreen[wScreenCount] = wScreenInit(DefaultScreen(dpy));
811 if (!wScreen[wScreenCount]) {
812 wfatal(_("it seems that there is already a window manager running"));
813 Exit(1);
814 }
815 } else {
816 wScreen[wScreenCount] = wScreenInit(j);
817 if (!wScreen[wScreenCount]) {
818 wwarning(_("could not manage screen %i"), j);
819 continue;
820 }
821 }
822 wScreenCount++;
9af1c6c4 dan1998-10-21 14:43:47 +0000823 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000824
851835c7 dan2002-09-11 15:02:43 +0000825#ifndef LITE
826 InitializeSwitchMenu();
827#endif
828
9af1c6c4 dan1998-10-21 14:43:47 +0000829 /* initialize/restore state for the screens */
830 for (j = 0; j < wScreenCount; j++) {
879b00a5 kojima2004-10-13 05:09:08 +0000831 int lastDesktop;
c3d2a890 dan2001-04-18 00:25:58 +0000832
879b00a5 kojima2004-10-13 05:09:08 +0000833 lastDesktop= wNETWMGetCurrentDesktopFromHint(wScreen[j]);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000834
6830b057 dan2004-10-12 21:28:27 +0000835 wScreenRestoreState(wScreen[j]);
9af1c6c4 dan1998-10-21 14:43:47 +0000836
6830b057 dan2004-10-12 21:28:27 +0000837 /* manage all windows that were already here before us */
838 if (!wPreferences.flags.nodock && wScreen[j]->dock)
839 wScreen[j]->last_dock = wScreen[j]->dock;
c3d2a890 dan2001-04-18 00:25:58 +0000840
879b00a5 kojima2004-10-13 05:09:08 +0000841 manageAllWindows(wScreen[j], wPreferences.flags.restarting==2);
c3d2a890 dan2001-04-18 00:25:58 +0000842
6830b057 dan2004-10-12 21:28:27 +0000843 /* restore saved menus */
844 wMenuRestoreState(wScreen[j]);
845
879b00a5 kojima2004-10-13 05:09:08 +0000846 /* If we're not restarting, restore session */
847 if (wPreferences.flags.restarting==0 && !wPreferences.flags.norestore)
6830b057 dan2004-10-12 21:28:27 +0000848 wSessionRestoreState(wScreen[j]);
849
850 if (!wPreferences.flags.noautolaunch) {
851 /* auto-launch apps */
852 if (!wPreferences.flags.nodock && wScreen[j]->dock) {
853 wScreen[j]->last_dock = wScreen[j]->dock;
854 wDockDoAutoLaunch(wScreen[j]->dock, 0);
855 }
856 /* auto-launch apps in clip */
857 if (!wPreferences.flags.noclip) {
858 int i;
859 for(i=0; i<wScreen[j]->workspace_count; i++) {
860 if (wScreen[j]->workspaces[i]->clip) {
861 wScreen[j]->last_dock = wScreen[j]->workspaces[i]->clip;
862 wDockDoAutoLaunch(wScreen[j]->workspaces[i]->clip, i);
863 }
864 }
865 }
866 }
867
868 /* go to workspace where we were before restart */
879b00a5 kojima2004-10-13 05:09:08 +0000869 if (lastDesktop >= 0) {
870 wWorkspaceForceChange(wScreen[j], lastDesktop);
6830b057 dan2004-10-12 21:28:27 +0000871 } else {
872 wSessionRestoreLastWorkspace(wScreen[j]);
873 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000874 }
6830b057 dan2004-10-12 21:28:27 +0000875
9d2e6ef9 scottc1998-09-29 22:36:29 +0000876 if (wScreenCount == 0) {
6830b057 dan2004-10-12 21:28:27 +0000877 wfatal(_("could not manage any screen"));
878 Exit(1);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000879 }
880
9d2e6ef9 scottc1998-09-29 22:36:29 +0000881}
882
883
884
9d2e6ef9 scottc1998-09-29 22:36:29 +0000885
9af1c6c4 dan1998-10-21 14:43:47 +0000886static Bool
887windowInList(Window window, Window *list, int count)
888{
889 for (; count>=0; count--) {
6830b057 dan2004-10-12 21:28:27 +0000890 if (window == list[count])
891 return True;
9af1c6c4 dan1998-10-21 14:43:47 +0000892 }
893 return False;
894}
895
9d2e6ef9 scottc1998-09-29 22:36:29 +0000896/*
897 *-----------------------------------------------------------------------
898 * manageAllWindows--
899 * Manages all windows in the screen.
6830b057 dan2004-10-12 21:28:27 +0000900 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000901 * Notes:
902 * Called when the wm is being started.
903 * No events can be processed while the windows are being
6830b057 dan2004-10-12 21:28:27 +0000904 * reparented/managed.
905 *-----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000906 */
907static void
c3d2a890 dan2001-04-18 00:25:58 +0000908manageAllWindows(WScreen *scr, int crashRecovery)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000909{
910 Window root, parent;
911 Window *children;
912 unsigned int nchildren;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000913 unsigned int i, j;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000914 WWindow *wwin;
088c0ac2 dan1999-03-09 14:58:01 +0000915
9d2e6ef9 scottc1998-09-29 22:36:29 +0000916 XGrabServer(dpy);
917 XQueryTree(dpy, scr->root_win, &root, &parent, &children, &nchildren);
918
919 scr->flags.startup = 1;
920
921 /* first remove all icon windows */
e7495baf dan1999-02-17 11:06:40 +0000922 for (i = 0; i < nchildren; i++) {
6830b057 dan2004-10-12 21:28:27 +0000923 XWMHints *wmhints;
924
925 if (children[i]==None)
926 continue;
927
928 wmhints = XGetWMHints(dpy, children[i]);
929 if (wmhints && (wmhints->flags & IconWindowHint)) {
930 for (j = 0; j < nchildren; j++) {
931 if (children[j] == wmhints->icon_window) {
932 XFree(wmhints);
933 wmhints = NULL;
934 children[j] = None;
935 break;
936 }
937 }
938 }
939 if (wmhints) {
940 XFree(wmhints);
941 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000942 }
088c0ac2 dan1999-03-09 14:58:01 +0000943
944
e7495baf dan1999-02-17 11:06:40 +0000945 for (i = 0; i < nchildren; i++) {
6830b057 dan2004-10-12 21:28:27 +0000946 if (children[i] == None)
947 continue;
e7495baf dan1999-02-17 11:06:40 +0000948
6830b057 dan2004-10-12 21:28:27 +0000949 wwin = wManageWindow(scr, children[i]);
950 if (wwin) {
951 /* apply states got from WSavedState */
952 /* shaded + minimized is not restored correctly */
953 if (wwin->flags.shaded) {
954 wwin->flags.shaded = 0;
955 wShadeWindow(wwin);
956 }
957 if (wwin->flags.miniaturized
958 && (wwin->transient_for == None
959 || wwin->transient_for == scr->root_win
960 || !windowInList(wwin->transient_for, children,
961 nchildren))) {
962
963 wwin->flags.skip_next_animation = 1;
964 wwin->flags.miniaturized = 0;
965 wIconifyWindow(wwin);
966 } else {
967 wClientSetState(wwin, NormalState, None);
c3d2a890 dan2001-04-18 00:25:58 +0000968 }
969 if (crashRecovery) {
970 int border;
971
9aca0d5f dan2004-10-12 01:34:32 +0000972 border = (!HAS_BORDER(wwin) ? 0 : FRAME_BORDER_WIDTH);
c3d2a890 dan2001-04-18 00:25:58 +0000973
974 wWindowMove(wwin, wwin->frame_x - border,
975 wwin->frame_y - border -
6830b057 dan2004-10-12 21:28:27 +0000976 (wwin->frame->titlebar ?
80fefd29 kojima2001-09-03 22:31:30 +0000977 wwin->frame->titlebar->height : 0));
c3d2a890 dan2001-04-18 00:25:58 +0000978 }
6830b057 dan2004-10-12 21:28:27 +0000979 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000980 }
981 XUngrabServer(dpy);
e7495baf dan1999-02-17 11:06:40 +0000982
983 /* hide apps */
984 wwin = scr->focused_window;
985 while (wwin) {
6830b057 dan2004-10-12 21:28:27 +0000986 if (wwin->flags.hidden) {
987 WApplication *wapp = wApplicationOf(wwin->main_window);
988
989 if (wapp) {
990 wwin->flags.hidden = 0;
991 wHideApplication(wapp);
992 } else {
993 wwin->flags.hidden = 0;
994 }
995 }
996 wwin = wwin->prev;
e7495baf dan1999-02-17 11:06:40 +0000997 }
998
9d2e6ef9 scottc1998-09-29 22:36:29 +0000999 XFree(children);
1000 scr->flags.startup = 0;
1001 scr->flags.startup2 = 1;
0261c326 dan1999-01-06 15:22:33 +00001002
9d2e6ef9 scottc1998-09-29 22:36:29 +00001003 while (XPending(dpy)) {
6830b057 dan2004-10-12 21:28:27 +00001004 XEvent ev;
1005 WMNextEvent(dpy, &ev);
1006 WMHandleEvent(&ev);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001007 }
1008 wWorkspaceForceChange(scr, 0);
1009 if (!wPreferences.flags.noclip)
1010 wDockShowIcons(scr->workspaces[scr->current_workspace]->clip);
1011 scr->flags.startup2 = 0;
1012}
1013
1014