Make wmaker XRandR aware
[wmaker-crm.git] / src / main.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/*
9af1c6c4 dan1998-10-21 14:43:47 +00002 * Window Maker window manager
6830b057 dan2004-10-12 21:28:27 +00003 *
4153e2fd dan2003-01-16 23:30:45 +00004 * Copyright (c) 1997-2003 Alfredo K. Kojima
6830b057 dan2004-10-12 21:28:27 +00005 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
6830b057 dan2004-10-12 21:28:27 +000018 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000019 * USA.
20 */
21
22#include "wconfig.h"
23
c7868fa4
TT
Tamas TEVESZ2010-03-18 19:18:18 +010024#ifdef HAVE_INOTIFY
25#include <sys/inotify.h>
26#endif
27
9d2e6ef9 scottc1998-09-29 22:36:29 +000028#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <string.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <fcntl.h>
35
36#include <X11/Xlib.h>
37#include <X11/Xutil.h>
38
9d2e6ef9 scottc1998-09-29 22:36:29 +000039/* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
40 * and if X_LOCALE is defined, X's locale emulating functions will be used.
41 * See Xlocale.h for more information.
42 */
43#include <X11/Xlocale.h>
44
9df0e167 kojima1999-04-03 03:51:17 +000045#define MAINFILE
46
9d2e6ef9 scottc1998-09-29 22:36:29 +000047#include "WindowMaker.h"
48#include "window.h"
49#include "funcs.h"
50#include "menu.h"
51#include "keybind.h"
52#include "xmodifier.h"
9007a657 dan1998-11-23 11:32:19 +000053#include "session.h"
38807b65 dan1999-11-07 21:40:22 +000054#include "dialog.h"
9d2e6ef9 scottc1998-09-29 22:36:29 +000055
33cc542e dan2001-10-04 03:07:34 +000056#include <WINGs/WUtil.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000057
3f6ae395
MD
Martin Dietze2009-08-24 10:10:03 +020058#ifndef GLOBAL_DEFAULTS_SUBDIR
59#define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
60#endif
61
9d2e6ef9 scottc1998-09-29 22:36:29 +000062/****** Global Variables ******/
63
64/* general info */
65
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020066Display *dpy;
9d2e6ef9 scottc1998-09-29 22:36:29 +000067
68char *ProgName;
69
70unsigned int ValidModMask = 0xff;
71
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +010072#ifdef HAVE_INOTIFY
56d85687
RP
Rodney Padgett2008-08-09 11:39:24 +010073int inotifyFD;
74int inotifyWD;
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +010075#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +000076/* locale to use. NULL==POSIX or C */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020077char *Locale = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000078
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020079int wScreenCount = 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +000080
81WPreferences wPreferences;
82
33cc542e dan2001-10-04 03:07:34 +000083WMPropList *wDomainName;
84WMPropList *wAttributeDomainName;
9d2e6ef9 scottc1998-09-29 22:36:29 +000085
86WShortKey wKeyBindings[WKBD_LAST];
87
88/* defaults domains */
89WDDomain *WDWindowMaker = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000090WDDomain *WDWindowAttributes = NULL;
068c5a94 kojima2001-01-05 22:39:01 +000091WDDomain *WDRootMenu = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000092
93/* XContexts */
94XContext wWinContext;
95XContext wAppWinContext;
96XContext wStackContext;
9aca0d5f dan2004-10-12 01:34:32 +000097XContext wVEdgeContext;
9d2e6ef9 scottc1998-09-29 22:36:29 +000098
99/* Atoms */
100Atom _XA_WM_STATE;
101Atom _XA_WM_CHANGE_STATE;
102Atom _XA_WM_PROTOCOLS;
103Atom _XA_WM_TAKE_FOCUS;
104Atom _XA_WM_DELETE_WINDOW;
105Atom _XA_WM_SAVE_YOURSELF;
106Atom _XA_WM_CLIENT_LEADER;
107Atom _XA_WM_COLORMAP_WINDOWS;
59eb9475 dan1998-11-03 12:53:26 +0000108Atom _XA_WM_COLORMAP_NOTIFY;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000109
110Atom _XA_GNUSTEP_WM_ATTR;
9af1c6c4 dan1998-10-21 14:43:47 +0000111Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000112Atom _XA_GNUSTEP_WM_RESIZEBAR;
0af24483 richard2000-03-02 07:36:02 +0000113Atom _XA_GNUSTEP_TITLEBAR_STATE;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000114
9d2e6ef9 scottc1998-09-29 22:36:29 +0000115Atom _XA_WINDOWMAKER_MENU;
116Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
117Atom _XA_WINDOWMAKER_STATE;
118
119Atom _XA_WINDOWMAKER_WM_FUNCTION;
416e3a82 dan1999-01-25 19:06:50 +0000120Atom _XA_WINDOWMAKER_NOTICEBOARD;
9df0e167 kojima1999-04-03 03:51:17 +0000121Atom _XA_WINDOWMAKER_COMMAND;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000122
754230a6 kojima2000-02-16 18:26:15 +0000123Atom _XA_WINDOWMAKER_ICON_SIZE;
124Atom _XA_WINDOWMAKER_ICON_TILE;
125
3f7110b1
DD
Daniel D├ęchelotte2009-08-19 00:50:49 +0200126Atom _XA_WM_IGNORE_FOCUS_EVENTS;
127
9d2e6ef9 scottc1998-09-29 22:36:29 +0000128/* cursors */
129Cursor wCursor[WCUR_LAST];
130
131/* last event timestamp for XSetInputFocus */
9aca0d5f dan2004-10-12 01:34:32 +0000132Time LastTimestamp = CurrentTime;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000133/* timestamp on the last time we did XSetInputFocus() */
9aca0d5f dan2004-10-12 01:34:32 +0000134Time LastFocusChange = CurrentTime;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000135
136#ifdef SHAPE
9af1c6c4 dan1998-10-21 14:43:47 +0000137Bool wShapeSupported;
138int wShapeEventBase;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000139#endif
140
daaccf6f id1999-05-23 23:48:08 +0000141#ifdef KEEP_XKB_LOCK_STATUS
142Bool wXkbSupported;
143int wXkbEventBase;
144#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000145
c201e161
CM
Carlos R. Mafra2010-08-22 19:05:07 +0200146#ifdef HAVE_XRANDR
147Bool has_randr;
148int randr_event_base;
149#endif
150
9d2e6ef9 scottc1998-09-29 22:36:29 +0000151/* special flags */
51b1bf34 kojima2001-03-14 02:54:54 +0000152char WProgramSigState = 0;
9af1c6c4 dan1998-10-21 14:43:47 +0000153char WProgramState = WSTATE_NORMAL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000154char WDelayedActionSet = 0;
155
5ef342d9 kojima2001-09-06 21:42:28 +0000156/* notifications */
157const char *WMNManaged = "WMNManaged";
158const char *WMNUnmanaged = "WMNUnmanaged";
159const char *WMNChangedWorkspace = "WMNChangedWorkspace";
160const char *WMNChangedState = "WMNChangedState";
161const char *WMNChangedFocus = "WMNChangedFocus";
162const char *WMNChangedStacking = "WMNChangedStacking";
163const char *WMNChangedName = "WMNChangedName";
164
165const char *WMNWorkspaceCreated = "WMNWorkspaceCreated";
166const char *WMNWorkspaceDestroyed = "WMNWorkspaceDestroyed";
167const char *WMNWorkspaceChanged = "WMNWorkspaceChanged";
168const char *WMNWorkspaceNameChanged = "WMNWorkspaceNameChanged";
169
170const char *WMNResetStacking = "WMNResetStacking";
171
9d2e6ef9 scottc1998-09-29 22:36:29 +0000172/******** End Global Variables *****/
173
174static char *DisplayName = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000175
8bdc78fc dan1999-04-17 20:25:07 +0000176static char **Arguments;
177
178static int ArgCount;
179
9d2e6ef9 scottc1998-09-29 22:36:29 +0000180extern void EventLoop();
181extern void StartUp();
879b00a5 kojima2004-10-13 05:09:08 +0000182extern int MonitorLoop(int argc, char **argv);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000183
94f4483d kojima1999-10-27 22:32:12 +0000184static Bool multiHead = True;
185
629b1187
RH
Ralf Hoffmann2009-12-10 19:57:23 +0100186static int *wVisualID = NULL;
187static int wVisualID_len = 0;
188
879b00a5 kojima2004-10-13 05:09:08 +0000189static int real_main(int argc, char **argv);
190
629b1187
RH
Ralf Hoffmann2009-12-10 19:57:23 +0100191int getWVisualID(int screen)
192{
193 if (wVisualID == NULL)
194 return -1;
195 if (screen < 0 || screen >= wVisualID_len)
196 return -1;
197
198 return wVisualID[screen];
199}
200
201static void setWVisualID(int screen, int val)
202{
203 int i;
204
205 if (screen < 0)
206 return;
207
208 if (wVisualID == NULL) {
209 /* no array at all, alloc space for screen + 1 entries
210 * and init with default value */
211 wVisualID_len = screen + 1;
212 wVisualID = (int *)malloc(wVisualID_len * sizeof(int));
213 for (i = 0; i < wVisualID_len; i++) {
214 wVisualID[i] = -1;
215 }
216 } else if (screen >= wVisualID_len) {
217 /* larger screen number than previously allocated
218 so enlarge array */
219 int oldlen = wVisualID_len;
220
221 wVisualID_len = screen + 1;
222 wVisualID = (int *)realloc(wVisualID, wVisualID_len * sizeof(int));
223 for (i = oldlen; i < wVisualID_len; i++) {
224 wVisualID[i] = -1;
225 }
226 }
227
228 wVisualID[screen] = val;
229}
230
231/*
232 * this function splits a given string at the comma into tokens
233 * and set the wVisualID variable to each parsed number
234 */
235static int initWVisualID(const char *user_str)
236{
237 char *mystr = strdup(user_str);
238 int cur_in_pos = 0;
239 int cur_out_pos = 0;
240 int cur_screen = 0;
241 int error_found = 0;
242
243 for (;;) {
244 /* check for delimiter */
245 if (user_str[cur_in_pos] == '\0' || user_str[cur_in_pos] == ',') {
246 int v;
247
248 mystr[cur_out_pos] = '\0';
249
250 if (sscanf(mystr, "%i", &v) != 1) {
251 error_found = 1;
252 break;
253 }
254
255 setWVisualID(cur_screen, v);
256
257 cur_screen++;
258 cur_out_pos = 0;
259 }
260
261 /* break in case last char has been consumed */
262 if (user_str[cur_in_pos] == '\0')
263 break;
264
265 /* if the current char is no delimiter put it into mystr */
266 if (user_str[cur_in_pos] != ',') {
267 mystr[cur_out_pos++] = user_str[cur_in_pos];
268 }
269 cur_in_pos++;
270 }
271
272 free(mystr);
273
274 if (cur_screen == 0||error_found != 0)
275 return 1;
276
277 return 0;
278}
279
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200280void Exit(int status)
9007a657 dan1998-11-23 11:32:19 +0000281{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200282 if (dpy)
283 XCloseDisplay(dpy);
9007a657 dan1998-11-23 11:32:19 +0000284
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200285 exit(status);
9007a657 dan1998-11-23 11:32:19 +0000286}
9d2e6ef9 scottc1998-09-29 22:36:29 +0000287
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200288void Restart(char *manager, Bool abortOnFailure)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000289{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200290 char *prog = NULL;
291 char *argv[MAX_RESTART_ARGS];
292 int i;
293
294 if (manager && manager[0] != 0) {
295 prog = argv[0] = strtok(manager, " ");
296 for (i = 1; i < MAX_RESTART_ARGS; i++) {
297 argv[i] = strtok(NULL, " ");
298 if (argv[i] == NULL) {
299 break;
300 }
301 }
302 }
303 if (dpy) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200304 XCloseDisplay(dpy);
305 dpy = NULL;
306 }
307 if (!prog) {
308 execvp(Arguments[0], Arguments);
309 wfatal(_("failed to restart Window Maker."));
310 } else {
311 execvp(prog, argv);
312 wsyserror(_("could not exec %s"), prog);
313 }
314 if (abortOnFailure)
315 exit(7);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000316}
317
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200318void SetupEnvironment(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000319{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200320 char *tmp, *ptr;
321 char buf[16];
322
323 if (multiHead) {
324 int len = strlen(DisplayName) + 64;
325 tmp = wmalloc(len);
326 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
327 ptr = strchr(strchr(tmp, ':'), '.');
328 if (ptr)
329 *ptr = 0;
330 snprintf(buf, sizeof(buf), ".%i", scr->screen);
331 strcat(tmp, buf);
332 putenv(tmp);
333 }
334 tmp = wmalloc(60);
335 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
336 scr->rcontext->attribs->colors_per_channel);
337 putenv(tmp);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000338}
339
eb87c409 kojima1999-10-20 03:25:06 +0000340typedef struct {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200341 WScreen *scr;
342 char *command;
eb87c409 kojima1999-10-20 03:25:06 +0000343} _tuple;
344
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200345static void shellCommandHandler(pid_t pid, unsigned char status, _tuple * data)
eb87c409 kojima1999-10-20 03:25:06 +0000346{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200347 if (status == 127) {
348 char *buffer;
eb87c409 kojima1999-10-20 03:25:06 +0000349
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200350 buffer = wstrconcat(_("Could not execute command: "), data->command);
eb87c409 kojima1999-10-20 03:25:06 +0000351
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200352 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
353 wfree(buffer);
354 } else if (status != 127) {
355 /*
356 printf("%s: %i\n", data->command, status);
357 */
358 }
eb87c409 kojima1999-10-20 03:25:06 +0000359
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200360 wfree(data->command);
361 wfree(data);
eb87c409 kojima1999-10-20 03:25:06 +0000362}
363
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200364void ExecuteShellCommand(WScreen * scr, char *command)
eb87c409 kojima1999-10-20 03:25:06 +0000365{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200366 static char *shell = NULL;
367 pid_t pid;
eb87c409 kojima1999-10-20 03:25:06 +0000368
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200369 /*
370 * This have a problem: if the shell is tcsh (not sure about others)
371 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
372 * will block and the command will not be executed.
373 if (!shell) {
374 shell = getenv("SHELL");
375 if (!shell)
376 shell = "/bin/sh";
377 }
378 */
379 shell = "/bin/sh";
eb87c409 kojima1999-10-20 03:25:06 +0000380
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200381 pid = fork();
6830b057 dan2004-10-12 21:28:27 +0000382
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200383 if (pid == 0) {
eb87c409 kojima1999-10-20 03:25:06 +0000384
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200385 SetupEnvironment(scr);
eb87c409 kojima1999-10-20 03:25:06 +0000386
d4de3d0a dan2002-09-09 04:25:51 +0000387#ifdef HAVE_SETSID
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200388 setsid();
eb87c409 kojima1999-10-20 03:25:06 +0000389#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200390 execl(shell, shell, "-c", command, NULL);
391 wsyserror("could not execute %s -c %s", shell, command);
392 Exit(-1);
393 } else if (pid < 0) {
394 wsyserror("cannot fork a new process");
395 } else {
396 _tuple *data = wmalloc(sizeof(_tuple));
397
398 data->scr = scr;
399 data->command = wstrdup(command);
400
401 wAddDeathHandler(pid, (WDeathHandler *) shellCommandHandler, data);
402 }
eb87c409 kojima1999-10-20 03:25:06 +0000403}
404
eb87c409 kojima1999-10-20 03:25:06 +0000405/*
9d2e6ef9 scottc1998-09-29 22:36:29 +0000406 *---------------------------------------------------------------------
407 * wAbort--
408 * Do a major cleanup and exit the program
6830b057 dan2004-10-12 21:28:27 +0000409 *
410 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000411 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200412void wAbort(Bool dumpCore)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000413{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200414 int i;
415 WScreen *scr;
416
417 for (i = 0; i < wScreenCount; i++) {
418 scr = wScreenWithNumber(i);
419 if (scr)
420 RestoreDesktop(scr);
421 }
422 printf(_("%s aborted.\n"), ProgName);
423 if (dumpCore)
424 abort();
425 else
426 exit(1);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000427}
428
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200429void print_help()
9d2e6ef9 scottc1998-09-29 22:36:29 +0000430{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200431 printf(_("Usage: %s [options]\n"), ProgName);
432 puts(_("The Window Maker window manager for the X window system"));
433 puts("");
434 puts(_(" -display host:dpy display to use"));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000435#ifdef USECPP
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200436 puts(_(" --no-cpp disable preprocessing of configuration files"));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000437#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200438 puts(_(" --no-dock do not open the application Dock"));
439 puts(_(" --no-clip do not open the workspace Clip"));
440 puts(_(" --no-autolaunch do not autolaunch applications"));
441 puts(_(" --dont-restore do not restore saved session"));
3c046182 kojima1999-09-16 02:58:56 +0000442
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200443 puts(_(" --locale locale locale to use"));
3c046182 kojima1999-09-16 02:58:56 +0000444
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200445 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
446 puts(_(" --visual-id visualid visual id of visual to use"));
447 puts(_(" --static do not update or save configurations"));
c7868fa4
TT
Tamas TEVESZ2010-03-18 19:18:18 +0100448#ifndef HAVE_INOTIFY
449 puts(_(" --no-polling do not periodically check for configuration updates"));
450#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200451 puts(_(" --version print version and exit"));
452 puts(_(" --help show this message"));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000453}
454
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200455void check_defaults()
9d2e6ef9 scottc1998-09-29 22:36:29 +0000456{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200457 char *path;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000458
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200459 path = wdefaultspathfordomain("WindowMaker");
f6682a9a kojima2004-10-20 03:31:14 +0000460
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200461 if (access(path, R_OK) != 0) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200462 wwarning(_("could not find user GNUstep directory (%s)."), path);
463
464 if (system("wmaker.inst --batch") != 0) {
465 wwarning(_("There was an error while creating GNUstep directory, please "
466 "make sure you have installed Window Maker correctly and run wmaker.inst"));
467 } else {
468 wwarning(_("%s directory created with default configuration."), path);
469 }
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200470 }
6830b057 dan2004-10-12 21:28:27 +0000471
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200472 wfree(path);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000473}
474
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100475#ifdef HAVE_INOTIFY
722c82c8 Carlos R. Mafra2008-05-02 20:01:50 -0300476/*
56d85687
RP
Rodney Padgett2008-08-09 11:39:24 +0100477 * Add watch here, used to notify if configuration
478 * files have changed, using linux kernel inotify mechanism
722c82c8 Carlos R. Mafra2008-05-02 20:01:50 -0300479 */
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100480
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200481static void inotifyWatchConfig()
722c82c8 Carlos R. Mafra2008-05-02 20:01:50 -0300482{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200483 char *watchPath = NULL;
484 inotifyFD = inotify_init(); /* Initialise an inotify instance */
485 if (inotifyFD < 0) {
486 wwarning(_("could not initialise an inotify instance."
487 " Changes to the defaults database will require"
488 " a restart to take effect. Check your kernel!"));
489 } else {
490 watchPath = wstrconcat(wusergnusteppath(), "/Defaults");
491 /* Add the watch; really we are only looking for modify events
492 * but we might want more in the future so check all events for now.
493 * The individual events are checked for in event.c.
494 */
495 inotifyWD = inotify_add_watch(inotifyFD, watchPath, IN_ALL_EVENTS);
496 if (inotifyWD < 0) {
497 wwarning(_("could not add an inotify watch on path\n."
498 "%s\n"
499 "Changes to the defaults database will require"
500 " a restart to take effect."), watchPath);
501 close(inotifyFD);
502 }
503 }
504 wfree(watchPath);
722c82c8 Carlos R. Mafra2008-05-02 20:01:50 -0300505}
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100506#endif /* HAVE_INOTIFY */
722c82c8 Carlos R. Mafra2008-05-02 20:01:50 -0300507
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200508static void execInitScript()
9d2e6ef9 scottc1998-09-29 22:36:29 +0000509{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200510 char *file, *paths;
9aca0d5f dan2004-10-12 01:34:32 +0000511
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200512 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
513 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
39513b9f kojima1999-05-14 04:30:01 +0000514
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200515 file = wfindfile(paths, DEF_INIT_SCRIPT);
516 wfree(paths);
39513b9f kojima1999-05-14 04:30:01 +0000517
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200518 if (file) {
519 if (system(file) != 0) {
520 wsyserror(_("%s:could not execute initialization script"), file);
521 }
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200522 wfree(file);
523 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000524}
525
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200526void ExecExitScript()
9d2e6ef9 scottc1998-09-29 22:36:29 +0000527{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200528 char *file, *paths;
9aca0d5f dan2004-10-12 01:34:32 +0000529
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200530 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
531 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
39513b9f kojima1999-05-14 04:30:01 +0000532
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200533 file = wfindfile(paths, DEF_EXIT_SCRIPT);
534 wfree(paths);
39513b9f kojima1999-05-14 04:30:01 +0000535
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200536 if (file) {
537 if (system(file) != 0) {
538 wsyserror(_("%s:could not execute exit script"), file);
539 }
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200540 wfree(file);
541 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000542}
543
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200544int main(int argc, char **argv)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000545{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200546 int i_am_the_monitor, i, len;
547 char *str, *alt;
548
549 /* setup common stuff for the monitor and wmaker itself */
550 WMInitializeApplication("WindowMaker", &argc, argv);
551
552 memset(&wPreferences, 0, sizeof(WPreferences));
553
554 wPreferences.fallbackWMs = WMCreateArray(8);
555 alt = getenv("WINDOWMAKER_ALT_WM");
556 if (alt != NULL)
557 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
558
559 WMAddToArray(wPreferences.fallbackWMs, wstrdup("blackbox"));
560 WMAddToArray(wPreferences.fallbackWMs, wstrdup("metacity"));
561 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
562 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
563 WMAddToArray(wPreferences.fallbackWMs, NULL);
564 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
565 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
566
567 i_am_the_monitor = 1;
568
569 for (i = 1; i < argc; i++) {
570 if (strncmp(argv[i], "--for-real", strlen("--for-real")) == 0) {
571 i_am_the_monitor = 0;
572 break;
573 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
574 i++;
575 if (i >= argc) {
576 wwarning(_("too few arguments for %s"), argv[i - 1]);
577 exit(0);
578 }
579 DisplayName = argv[i];
580 }
581 }
582
583 DisplayName = XDisplayName(DisplayName);
584 len = strlen(DisplayName) + 64;
585 str = wmalloc(len);
586 snprintf(str, len, "DISPLAY=%s", DisplayName);
587 putenv(str);
588
589 if (i_am_the_monitor)
590 return MonitorLoop(argc, argv);
591 else
592 return real_main(argc, argv);
879b00a5 kojima2004-10-13 05:09:08 +0000593}
594
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200595static int real_main(int argc, char **argv)
879b00a5 kojima2004-10-13 05:09:08 +0000596{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200597 int i, restart = 0;
598 char *pos;
599 int d, s;
600 int flag;
5e4625da kojima1999-05-29 21:41:25 +0000601
5178465b Carlos R. Mafra2010-03-17 17:44:14 +0100602 setlocale(LC_ALL, "");
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200603 wsetabort(wAbort);
5e4625da kojima1999-05-29 21:41:25 +0000604
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200605 /* for telling WPrefs what's the name of the wmaker binary being ran */
606 setenv("WMAKER_BIN_NAME", argv[0], 1);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000607
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200608 flag = 0;
609 ArgCount = argc;
610 Arguments = wmalloc(sizeof(char *) * (ArgCount + 1));
611 for (i = 0; i < argc; i++) {
612 Arguments[i] = argv[i];
613 }
614 /* add the extra option to signal that we're just restarting wmaker */
615 Arguments[argc - 1] = "--for-real=";
616 Arguments[argc] = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000617
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200618 ProgName = strrchr(argv[0], '/');
619 if (!ProgName)
620 ProgName = argv[0];
621 else
622 ProgName++;
9007a657 dan1998-11-23 11:32:19 +0000623
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200624 restart = 0;
f37b4cf5 dan2002-02-01 01:20:15 +0000625
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200626 if (argc > 1) {
627 for (i = 1; i < argc; i++) {
9d2e6ef9 scottc1998-09-29 22:36:29 +0000628#ifdef USECPP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200629 if (strcmp(argv[i], "-nocpp") == 0 || strcmp(argv[i], "--no-cpp") == 0) {
630 wPreferences.flags.nocpp = 1;
631 } else
9d2e6ef9 scottc1998-09-29 22:36:29 +0000632#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200633 if (strcmp(argv[i], "--for-real") == 0) {
634 wPreferences.flags.restarting = 0;
635 } else if (strcmp(argv[i], "--for-real=") == 0) {
636 wPreferences.flags.restarting = 1;
637 } else if (strcmp(argv[i], "--for-real-") == 0) {
638 wPreferences.flags.restarting = 2;
639 } else if (strcmp(argv[i], "-no-autolaunch") == 0
640 || strcmp(argv[i], "--no-autolaunch") == 0) {
641 wPreferences.flags.noautolaunch = 1;
642 } else if (strcmp(argv[i], "-dont-restore") == 0 || strcmp(argv[i], "--dont-restore") == 0) {
643 wPreferences.flags.norestore = 1;
644 } else if (strcmp(argv[i], "-nodock") == 0 || strcmp(argv[i], "--no-dock") == 0) {
645 wPreferences.flags.nodock = 1;
646 } else if (strcmp(argv[i], "-noclip") == 0 || strcmp(argv[i], "--no-clip") == 0) {
647 wPreferences.flags.noclip = 1;
648 } else if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
649 printf("Window Maker %s\n", VERSION);
650 exit(0);
651 } else if (strcmp(argv[i], "--global_defaults_path") == 0) {
3f6ae395 Martin Dietze2009-08-24 10:10:03 +0200652 printf("%s/%s\n", SYSCONFDIR, GLOBAL_DEFAULTS_SUBDIR);
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200653 exit(0);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200654 } else if (strcmp(argv[i], "-locale") == 0 || strcmp(argv[i], "--locale") == 0) {
655 i++;
656 if (i >= argc) {
657 wwarning(_("too few arguments for %s"), argv[i - 1]);
658 exit(0);
659 }
660 Locale = argv[i];
661 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
662 i++;
663 if (i >= argc) {
664 wwarning(_("too few arguments for %s"), argv[i - 1]);
665 exit(0);
666 }
667 DisplayName = argv[i];
668 } else if (strcmp(argv[i], "-visualid") == 0 || strcmp(argv[i], "--visual-id") == 0) {
669 i++;
670 if (i >= argc) {
671 wwarning(_("too few arguments for %s"), argv[i - 1]);
672 exit(0);
673 }
629b1187 Ralf Hoffmann2009-12-10 19:57:23 +0100674 if (initWVisualID(argv[i]) != 0) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200675 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
676 exit(0);
677 }
c7868fa4
TT
Tamas TEVESZ2010-03-18 19:18:18 +0100678 } else if (strcmp(argv[i], "-static") == 0 || strcmp(argv[i], "--static") == 0
679#ifndef HAVE_INOTIFY
680 || strcmp(argv[i], "--no-polling") == 0
681#endif
682 ) {
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200683 wPreferences.flags.noupdates = 1;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200684 } else if (strcmp(argv[i], "--help") == 0) {
685 print_help();
686 exit(0);
687 } else {
688 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
689 printf(_("Try '%s --help' for more information\n"), argv[0]);
690 exit(1);
691 }
692 }
693 }
694
695 if (!wPreferences.flags.noupdates) {
696 /* check existence of Defaults DB directory */
697 check_defaults();
698 }
699
700 if (Locale) {
701 setenv("LANG", Locale, 1);
702 } else {
703 Locale = getenv("LC_ALL");
704 if (!Locale) {
705 Locale = getenv("LANG");
706 }
707 }
708
709 setlocale(LC_ALL, "");
710
711 if (!Locale || strcmp(Locale, "C") == 0 || strcmp(Locale, "POSIX") == 0)
712 Locale = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000713#ifdef I18N
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200714 if (getenv("NLSPATH"))
715 bindtextdomain("WindowMaker", getenv("NLSPATH"));
716 else
717 bindtextdomain("WindowMaker", LOCALEDIR);
718 bind_textdomain_codeset("WindowMaker", "UTF-8");
719 textdomain("WindowMaker");
720
721 if (!XSupportsLocale()) {
722 wwarning(_("X server does not support locale"));
723 }
724
725 if (XSetLocaleModifiers("") == NULL) {
726 wwarning(_("cannot set locale modifiers"));
727 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000728#endif
9007a657 dan1998-11-23 11:32:19 +0000729
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200730 if (Locale) {
731 char *ptr;
732
733 Locale = wstrdup(Locale);
734 ptr = strchr(Locale, '.');
735 if (ptr)
736 *ptr = 0;
737 }
738
739 /* open display */
740 dpy = XOpenDisplay(DisplayName);
741 if (dpy == NULL) {
742 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
743 exit(1);
744 }
745
746 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
747 wsyserror("error setting close-on-exec flag for X connection");
748 exit(1);
749 }
750
629b1187
RH
Ralf Hoffmann2009-12-10 19:57:23 +0100751
752 if (getWVisualID(0) < 0) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200753 /*
754 * If unspecified, use default visual instead of waiting
755 * for wrlib/context.c:bestContext() that may end up choosing
756 * the "fake" 24 bits added by the Composite extension.
757 * This is required to avoid all sort of corruptions when
758 * composite is enabled, and at a depth other than 24.
759 */
629b1187
RH
Ralf Hoffmann2009-12-10 19:57:23 +0100760 setWVisualID(0, (int)DefaultVisual(dpy, DefaultScreen(dpy))->visualid);
761 }
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200762
763 /* check if the user specified a complete display name (with screen).
764 * If so, only manage the specified screen */
765 if (DisplayName)
766 pos = strchr(DisplayName, ':');
767 else
768 pos = NULL;
769
770 if (pos && sscanf(pos, ":%i.%i", &d, &s) == 2)
771 multiHead = False;
772
773 DisplayName = XDisplayName(DisplayName);
774 setenv("DISPLAY", DisplayName, 1);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000775
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200776 wXModifierInitialize();
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200777 StartUp(!multiHead);
6830b057 dan2004-10-12 21:28:27 +0000778
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200779 if (wScreenCount == 1)
780 multiHead = False;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000781
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200782 execInitScript();
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100783#ifdef HAVE_INOTIFY
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200784 inotifyWatchConfig();
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100785#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200786 EventLoop();
787 return -1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000788}