Change to the linux kernel coding style
[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
56d85687 Rodney Padgett2008-08-09 11:39:24 +010022#include <sys/inotify.h>
722c82c8 Carlos R. Mafra2008-05-02 20:01:50 -030023
9d2e6ef9 scottc1998-09-29 22:36:29 +000024#include "wconfig.h"
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <string.h>
30#include <sys/stat.h>
31#include <sys/types.h>
32#include <fcntl.h>
33
34#include <X11/Xlib.h>
35#include <X11/Xutil.h>
36
9d2e6ef9 scottc1998-09-29 22:36:29 +000037/* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
38 * and if X_LOCALE is defined, X's locale emulating functions will be used.
39 * See Xlocale.h for more information.
40 */
41#include <X11/Xlocale.h>
42
9df0e167 kojima1999-04-03 03:51:17 +000043#define MAINFILE
44
9d2e6ef9 scottc1998-09-29 22:36:29 +000045#include "WindowMaker.h"
46#include "window.h"
47#include "funcs.h"
48#include "menu.h"
49#include "keybind.h"
50#include "xmodifier.h"
51#include "defaults.h"
9007a657 dan1998-11-23 11:32:19 +000052#include "session.h"
38807b65 dan1999-11-07 21:40:22 +000053#include "dialog.h"
9d2e6ef9 scottc1998-09-29 22:36:29 +000054
33cc542e dan2001-10-04 03:07:34 +000055#include <WINGs/WUtil.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000056
9d2e6ef9 scottc1998-09-29 22:36:29 +000057/****** Global Variables ******/
58
59/* general info */
60
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020061Display *dpy;
9d2e6ef9 scottc1998-09-29 22:36:29 +000062
63char *ProgName;
64
65unsigned int ValidModMask = 0xff;
66
56d85687
RP
Rodney Padgett2008-08-09 11:39:24 +010067int inotifyFD;
68int inotifyWD;
9d2e6ef9 scottc1998-09-29 22:36:29 +000069/* locale to use. NULL==POSIX or C */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020070char *Locale = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000071
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020072int wScreenCount = 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +000073
74WPreferences wPreferences;
75
33cc542e dan2001-10-04 03:07:34 +000076WMPropList *wDomainName;
77WMPropList *wAttributeDomainName;
9d2e6ef9 scottc1998-09-29 22:36:29 +000078
79WShortKey wKeyBindings[WKBD_LAST];
80
81/* defaults domains */
82WDDomain *WDWindowMaker = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000083WDDomain *WDWindowAttributes = NULL;
068c5a94 kojima2001-01-05 22:39:01 +000084WDDomain *WDRootMenu = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000085
86/* XContexts */
87XContext wWinContext;
88XContext wAppWinContext;
89XContext wStackContext;
9aca0d5f dan2004-10-12 01:34:32 +000090XContext wVEdgeContext;
9d2e6ef9 scottc1998-09-29 22:36:29 +000091
92/* Atoms */
93Atom _XA_WM_STATE;
94Atom _XA_WM_CHANGE_STATE;
95Atom _XA_WM_PROTOCOLS;
96Atom _XA_WM_TAKE_FOCUS;
97Atom _XA_WM_DELETE_WINDOW;
98Atom _XA_WM_SAVE_YOURSELF;
99Atom _XA_WM_CLIENT_LEADER;
100Atom _XA_WM_COLORMAP_WINDOWS;
59eb9475 dan1998-11-03 12:53:26 +0000101Atom _XA_WM_COLORMAP_NOTIFY;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000102
103Atom _XA_GNUSTEP_WM_ATTR;
9af1c6c4 dan1998-10-21 14:43:47 +0000104Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000105Atom _XA_GNUSTEP_WM_RESIZEBAR;
0af24483 richard2000-03-02 07:36:02 +0000106Atom _XA_GNUSTEP_TITLEBAR_STATE;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000107
9d2e6ef9 scottc1998-09-29 22:36:29 +0000108Atom _XA_WINDOWMAKER_MENU;
109Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
110Atom _XA_WINDOWMAKER_STATE;
111
112Atom _XA_WINDOWMAKER_WM_FUNCTION;
416e3a82 dan1999-01-25 19:06:50 +0000113Atom _XA_WINDOWMAKER_NOTICEBOARD;
9df0e167 kojima1999-04-03 03:51:17 +0000114Atom _XA_WINDOWMAKER_COMMAND;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000115
754230a6 kojima2000-02-16 18:26:15 +0000116Atom _XA_WINDOWMAKER_ICON_SIZE;
117Atom _XA_WINDOWMAKER_ICON_TILE;
118
9d2e6ef9 scottc1998-09-29 22:36:29 +0000119/* cursors */
120Cursor wCursor[WCUR_LAST];
121
122/* last event timestamp for XSetInputFocus */
9aca0d5f dan2004-10-12 01:34:32 +0000123Time LastTimestamp = CurrentTime;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000124/* timestamp on the last time we did XSetInputFocus() */
9aca0d5f dan2004-10-12 01:34:32 +0000125Time LastFocusChange = CurrentTime;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000126
127#ifdef SHAPE
9af1c6c4 dan1998-10-21 14:43:47 +0000128Bool wShapeSupported;
129int wShapeEventBase;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000130#endif
131
daaccf6f id1999-05-23 23:48:08 +0000132#ifdef KEEP_XKB_LOCK_STATUS
133Bool wXkbSupported;
134int wXkbEventBase;
135#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000136
137/* special flags */
51b1bf34 kojima2001-03-14 02:54:54 +0000138char WProgramSigState = 0;
9af1c6c4 dan1998-10-21 14:43:47 +0000139char WProgramState = WSTATE_NORMAL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000140char WDelayedActionSet = 0;
141
142/* temporary stuff */
143int wVisualID = -1;
144
5ef342d9 kojima2001-09-06 21:42:28 +0000145/* notifications */
146const char *WMNManaged = "WMNManaged";
147const char *WMNUnmanaged = "WMNUnmanaged";
148const char *WMNChangedWorkspace = "WMNChangedWorkspace";
149const char *WMNChangedState = "WMNChangedState";
150const char *WMNChangedFocus = "WMNChangedFocus";
151const char *WMNChangedStacking = "WMNChangedStacking";
152const char *WMNChangedName = "WMNChangedName";
153
154const char *WMNWorkspaceCreated = "WMNWorkspaceCreated";
155const char *WMNWorkspaceDestroyed = "WMNWorkspaceDestroyed";
156const char *WMNWorkspaceChanged = "WMNWorkspaceChanged";
157const char *WMNWorkspaceNameChanged = "WMNWorkspaceNameChanged";
158
159const char *WMNResetStacking = "WMNResetStacking";
160
9d2e6ef9 scottc1998-09-29 22:36:29 +0000161/******** End Global Variables *****/
162
163static char *DisplayName = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000164
8bdc78fc dan1999-04-17 20:25:07 +0000165static char **Arguments;
166
167static int ArgCount;
168
9d2e6ef9 scottc1998-09-29 22:36:29 +0000169extern void EventLoop();
170extern void StartUp();
879b00a5 kojima2004-10-13 05:09:08 +0000171extern int MonitorLoop(int argc, char **argv);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000172
94f4483d kojima1999-10-27 22:32:12 +0000173static Bool multiHead = True;
174
879b00a5 kojima2004-10-13 05:09:08 +0000175static int real_main(int argc, char **argv);
176
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200177void Exit(int status)
9007a657 dan1998-11-23 11:32:19 +0000178{
416e3a82 dan1999-01-25 19:06:50 +0000179#ifdef XSMP_ENABLED
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200180 wSessionDisconnectManager();
9007a657 dan1998-11-23 11:32:19 +0000181#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200182 if (dpy)
183 XCloseDisplay(dpy);
9007a657 dan1998-11-23 11:32:19 +0000184
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200185 exit(status);
9007a657 dan1998-11-23 11:32:19 +0000186}
9d2e6ef9 scottc1998-09-29 22:36:29 +0000187
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200188void Restart(char *manager, Bool abortOnFailure)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000189{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200190 char *prog = NULL;
191 char *argv[MAX_RESTART_ARGS];
192 int i;
193
194 if (manager && manager[0] != 0) {
195 prog = argv[0] = strtok(manager, " ");
196 for (i = 1; i < MAX_RESTART_ARGS; i++) {
197 argv[i] = strtok(NULL, " ");
198 if (argv[i] == NULL) {
199 break;
200 }
201 }
202 }
203 if (dpy) {
416e3a82 dan1999-01-25 19:06:50 +0000204#ifdef XSMP_ENABLED
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200205 wSessionDisconnectManager();
9007a657 dan1998-11-23 11:32:19 +0000206#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200207 XCloseDisplay(dpy);
208 dpy = NULL;
209 }
210 if (!prog) {
211 execvp(Arguments[0], Arguments);
212 wfatal(_("failed to restart Window Maker."));
213 } else {
214 execvp(prog, argv);
215 wsyserror(_("could not exec %s"), prog);
216 }
217 if (abortOnFailure)
218 exit(7);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000219}
220
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200221void SetupEnvironment(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000222{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200223 char *tmp, *ptr;
224 char buf[16];
225
226 if (multiHead) {
227 int len = strlen(DisplayName) + 64;
228 tmp = wmalloc(len);
229 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
230 ptr = strchr(strchr(tmp, ':'), '.');
231 if (ptr)
232 *ptr = 0;
233 snprintf(buf, sizeof(buf), ".%i", scr->screen);
234 strcat(tmp, buf);
235 putenv(tmp);
236 }
237 tmp = wmalloc(60);
238 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
239 scr->rcontext->attribs->colors_per_channel);
240 putenv(tmp);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000241}
242
eb87c409 kojima1999-10-20 03:25:06 +0000243typedef struct {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200244 WScreen *scr;
245 char *command;
eb87c409 kojima1999-10-20 03:25:06 +0000246} _tuple;
247
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200248static void shellCommandHandler(pid_t pid, unsigned char status, _tuple * data)
eb87c409 kojima1999-10-20 03:25:06 +0000249{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200250 if (status == 127) {
251 char *buffer;
eb87c409 kojima1999-10-20 03:25:06 +0000252
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200253 buffer = wstrconcat(_("Could not execute command: "), data->command);
eb87c409 kojima1999-10-20 03:25:06 +0000254
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200255 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
256 wfree(buffer);
257 } else if (status != 127) {
258 /*
259 printf("%s: %i\n", data->command, status);
260 */
261 }
eb87c409 kojima1999-10-20 03:25:06 +0000262
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200263 wfree(data->command);
264 wfree(data);
eb87c409 kojima1999-10-20 03:25:06 +0000265}
266
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200267void ExecuteShellCommand(WScreen * scr, char *command)
eb87c409 kojima1999-10-20 03:25:06 +0000268{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200269 static char *shell = NULL;
270 pid_t pid;
eb87c409 kojima1999-10-20 03:25:06 +0000271
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200272 /*
273 * This have a problem: if the shell is tcsh (not sure about others)
274 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
275 * will block and the command will not be executed.
276 if (!shell) {
277 shell = getenv("SHELL");
278 if (!shell)
279 shell = "/bin/sh";
280 }
281 */
282 shell = "/bin/sh";
eb87c409 kojima1999-10-20 03:25:06 +0000283
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200284 pid = fork();
6830b057 dan2004-10-12 21:28:27 +0000285
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200286 if (pid == 0) {
eb87c409 kojima1999-10-20 03:25:06 +0000287
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200288 SetupEnvironment(scr);
eb87c409 kojima1999-10-20 03:25:06 +0000289
d4de3d0a dan2002-09-09 04:25:51 +0000290#ifdef HAVE_SETSID
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200291 setsid();
eb87c409 kojima1999-10-20 03:25:06 +0000292#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200293 execl(shell, shell, "-c", command, NULL);
294 wsyserror("could not execute %s -c %s", shell, command);
295 Exit(-1);
296 } else if (pid < 0) {
297 wsyserror("cannot fork a new process");
298 } else {
299 _tuple *data = wmalloc(sizeof(_tuple));
300
301 data->scr = scr;
302 data->command = wstrdup(command);
303
304 wAddDeathHandler(pid, (WDeathHandler *) shellCommandHandler, data);
305 }
eb87c409 kojima1999-10-20 03:25:06 +0000306}
307
eb87c409 kojima1999-10-20 03:25:06 +0000308/*
9d2e6ef9 scottc1998-09-29 22:36:29 +0000309 *---------------------------------------------------------------------
310 * wAbort--
311 * Do a major cleanup and exit the program
6830b057 dan2004-10-12 21:28:27 +0000312 *
313 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000314 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200315void wAbort(Bool dumpCore)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000316{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200317 int i;
318 WScreen *scr;
319
320 for (i = 0; i < wScreenCount; i++) {
321 scr = wScreenWithNumber(i);
322 if (scr)
323 RestoreDesktop(scr);
324 }
325 printf(_("%s aborted.\n"), ProgName);
326 if (dumpCore)
327 abort();
328 else
329 exit(1);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000330}
331
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200332void print_help()
9d2e6ef9 scottc1998-09-29 22:36:29 +0000333{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200334 printf(_("Usage: %s [options]\n"), ProgName);
335 puts(_("The Window Maker window manager for the X window system"));
336 puts("");
337 puts(_(" -display host:dpy display to use"));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000338#ifdef USECPP
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200339 puts(_(" --no-cpp disable preprocessing of configuration files"));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000340#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200341 puts(_(" --no-dock do not open the application Dock"));
342 puts(_(" --no-clip do not open the workspace Clip"));
343 puts(_(" --no-autolaunch do not autolaunch applications"));
344 puts(_(" --dont-restore do not restore saved session"));
3c046182 kojima1999-09-16 02:58:56 +0000345
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200346 puts(_(" --locale locale locale to use"));
3c046182 kojima1999-09-16 02:58:56 +0000347
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200348 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
349 puts(_(" --visual-id visualid visual id of visual to use"));
350 puts(_(" --static do not update or save configurations"));
c98f229d dan1999-04-16 01:12:45 +0000351#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200352 puts(_(" --synchronous turn on synchronous display mode"));
c98f229d dan1999-04-16 01:12:45 +0000353#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200354 puts(_(" --version print version and exit"));
355 puts(_(" --help show this message"));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000356}
357
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200358void check_defaults()
9d2e6ef9 scottc1998-09-29 22:36:29 +0000359{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200360 char *path;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000361
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200362 path = wdefaultspathfordomain("WindowMaker");
f6682a9a kojima2004-10-20 03:31:14 +0000363
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200364 if (access(path, R_OK) != 0) {
80fefd29 kojima2001-09-03 22:31:30 +0000365#if 0
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200366 wfatal(_("could not find user GNUstep directory (%s).\n"
367 "Make sure you have installed Window Maker correctly and run wmaker.inst"), path);
368 exit(1);
80fefd29 kojima2001-09-03 22:31:30 +0000369#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200370 wwarning(_("could not find user GNUstep directory (%s)."), path);
371
372 if (system("wmaker.inst --batch") != 0) {
373 wwarning(_("There was an error while creating GNUstep directory, please "
374 "make sure you have installed Window Maker correctly and run wmaker.inst"));
375 } else {
376 wwarning(_("%s directory created with default configuration."), path);
377 }
80fefd29 kojima2001-09-03 22:31:30 +0000378#endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200379 }
6830b057 dan2004-10-12 21:28:27 +0000380
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200381 wfree(path);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000382}
383
722c82c8 Carlos R. Mafra2008-05-02 20:01:50 -0300384/*
56d85687
RP
Rodney Padgett2008-08-09 11:39:24 +0100385 * Add watch here, used to notify if configuration
386 * files have changed, using linux kernel inotify mechanism
722c82c8 Carlos R. Mafra2008-05-02 20:01:50 -0300387 */
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100388
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200389static void inotifyWatchConfig()
722c82c8 Carlos R. Mafra2008-05-02 20:01:50 -0300390{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200391 char *watchPath = NULL;
392 inotifyFD = inotify_init(); /* Initialise an inotify instance */
393 if (inotifyFD < 0) {
394 wwarning(_("could not initialise an inotify instance."
395 " Changes to the defaults database will require"
396 " a restart to take effect. Check your kernel!"));
397 } else {
398 watchPath = wstrconcat(wusergnusteppath(), "/Defaults");
399 /* Add the watch; really we are only looking for modify events
400 * but we might want more in the future so check all events for now.
401 * The individual events are checked for in event.c.
402 */
403 inotifyWD = inotify_add_watch(inotifyFD, watchPath, IN_ALL_EVENTS);
404 if (inotifyWD < 0) {
405 wwarning(_("could not add an inotify watch on path\n."
406 "%s\n"
407 "Changes to the defaults database will require"
408 " a restart to take effect."), watchPath);
409 close(inotifyFD);
410 }
411 }
412 wfree(watchPath);
722c82c8
CM
Carlos R. Mafra2008-05-02 20:01:50 -0300413}
414
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200415static void execInitScript()
9d2e6ef9 scottc1998-09-29 22:36:29 +0000416{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200417 char *file, *paths;
9aca0d5f dan2004-10-12 01:34:32 +0000418
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200419 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
420 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
39513b9f kojima1999-05-14 04:30:01 +0000421
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200422 file = wfindfile(paths, DEF_INIT_SCRIPT);
423 wfree(paths);
39513b9f kojima1999-05-14 04:30:01 +0000424
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200425 if (file) {
426 if (system(file) != 0) {
427 wsyserror(_("%s:could not execute initialization script"), file);
428 }
da918281 kojima1999-04-12 02:03:58 +0000429#if 0
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200430 if (fork() == 0) {
431 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
432 wsyserror(_("%s:could not execute initialization script"), file);
433 exit(1);
434 }
da918281 kojima1999-04-12 02:03:58 +0000435#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200436 wfree(file);
437 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000438}
439
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200440void ExecExitScript()
9d2e6ef9 scottc1998-09-29 22:36:29 +0000441{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200442 char *file, *paths;
9aca0d5f dan2004-10-12 01:34:32 +0000443
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200444 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
445 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
39513b9f kojima1999-05-14 04:30:01 +0000446
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200447 file = wfindfile(paths, DEF_EXIT_SCRIPT);
448 wfree(paths);
39513b9f kojima1999-05-14 04:30:01 +0000449
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200450 if (file) {
451 if (system(file) != 0) {
452 wsyserror(_("%s:could not execute exit script"), file);
453 }
da918281 kojima1999-04-12 02:03:58 +0000454#if 0
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200455 if (fork() == 0) {
456 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
457 wsyserror(_("%s:could not execute exit script"), file);
458 exit(1);
459 }
da918281 kojima1999-04-12 02:03:58 +0000460#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200461 wfree(file);
462 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000463}
464
3c046182 kojima1999-09-16 02:58:56 +0000465#if 0
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200466char *getFullPath(char *path)
3c046182 kojima1999-09-16 02:58:56 +0000467{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200468 char buffer[1024];
469 char *tmp;
470 char *basep = (char *)buffer;
471
472 if (*path != '/' && getcwd(buffer, 1023)) {
473
474 for (;;) {
475 if (strncmp(path, "../", 3) == 0) {
476 path += 3;
477 basep = strchr(basep, '/');
478 if (!basep || *path == 0)
479 break;
480 }
481 }
482 if (*path == '/' || strncmp(path, "./", 2) == 0) {
483 tmp =}
484
485 /*
486 * path
487 * ./path
488 * ../path
489 * ../../path
490 */
491
492 } else {
493 return wstrconcat(path);
494 }
495
496 return tmp;
3c046182 kojima1999-09-16 02:58:56 +0000497}
498#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000499
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200500int main(int argc, char **argv)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000501{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200502 int i_am_the_monitor, i, len;
503 char *str, *alt;
504
505 /* setup common stuff for the monitor and wmaker itself */
506 WMInitializeApplication("WindowMaker", &argc, argv);
507
508 memset(&wPreferences, 0, sizeof(WPreferences));
509
510 wPreferences.fallbackWMs = WMCreateArray(8);
511 alt = getenv("WINDOWMAKER_ALT_WM");
512 if (alt != NULL)
513 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
514
515 WMAddToArray(wPreferences.fallbackWMs, wstrdup("blackbox"));
516 WMAddToArray(wPreferences.fallbackWMs, wstrdup("metacity"));
517 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
518 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
519 WMAddToArray(wPreferences.fallbackWMs, NULL);
520 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
521 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
522
523 i_am_the_monitor = 1;
524
525 for (i = 1; i < argc; i++) {
526 if (strncmp(argv[i], "--for-real", strlen("--for-real")) == 0) {
527 i_am_the_monitor = 0;
528 break;
529 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
530 i++;
531 if (i >= argc) {
532 wwarning(_("too few arguments for %s"), argv[i - 1]);
533 exit(0);
534 }
535 DisplayName = argv[i];
536 }
537 }
538
539 DisplayName = XDisplayName(DisplayName);
540 len = strlen(DisplayName) + 64;
541 str = wmalloc(len);
542 snprintf(str, len, "DISPLAY=%s", DisplayName);
543 putenv(str);
544
545 if (i_am_the_monitor)
546 return MonitorLoop(argc, argv);
547 else
548 return real_main(argc, argv);
879b00a5 kojima2004-10-13 05:09:08 +0000549}
550
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200551static int real_main(int argc, char **argv)
879b00a5 kojima2004-10-13 05:09:08 +0000552{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200553 int i, restart = 0;
554 char *pos;
555 int d, s;
556 int flag;
c98f229d dan1999-04-16 01:12:45 +0000557#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200558 Bool doSync = False;
c98f229d dan1999-04-16 01:12:45 +0000559#endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200560 setlocale(LC_ALL, "");
5e4625da kojima1999-05-29 21:41:25 +0000561
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200562 wsetabort(wAbort);
5e4625da kojima1999-05-29 21:41:25 +0000563
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200564 /* for telling WPrefs what's the name of the wmaker binary being ran */
565 setenv("WMAKER_BIN_NAME", argv[0], 1);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000566
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200567 flag = 0;
568 ArgCount = argc;
569 Arguments = wmalloc(sizeof(char *) * (ArgCount + 1));
570 for (i = 0; i < argc; i++) {
571 Arguments[i] = argv[i];
572 }
573 /* add the extra option to signal that we're just restarting wmaker */
574 Arguments[argc - 1] = "--for-real=";
575 Arguments[argc] = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000576
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200577 ProgName = strrchr(argv[0], '/');
578 if (!ProgName)
579 ProgName = argv[0];
580 else
581 ProgName++;
9007a657 dan1998-11-23 11:32:19 +0000582
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200583 restart = 0;
f37b4cf5 dan2002-02-01 01:20:15 +0000584
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200585 if (argc > 1) {
586 for (i = 1; i < argc; i++) {
9d2e6ef9 scottc1998-09-29 22:36:29 +0000587#ifdef USECPP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200588 if (strcmp(argv[i], "-nocpp") == 0 || strcmp(argv[i], "--no-cpp") == 0) {
589 wPreferences.flags.nocpp = 1;
590 } else
9d2e6ef9 scottc1998-09-29 22:36:29 +0000591#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200592 if (strcmp(argv[i], "--for-real") == 0) {
593 wPreferences.flags.restarting = 0;
594 } else if (strcmp(argv[i], "--for-real=") == 0) {
595 wPreferences.flags.restarting = 1;
596 } else if (strcmp(argv[i], "--for-real-") == 0) {
597 wPreferences.flags.restarting = 2;
598 } else if (strcmp(argv[i], "-no-autolaunch") == 0
599 || strcmp(argv[i], "--no-autolaunch") == 0) {
600 wPreferences.flags.noautolaunch = 1;
601 } else if (strcmp(argv[i], "-dont-restore") == 0 || strcmp(argv[i], "--dont-restore") == 0) {
602 wPreferences.flags.norestore = 1;
603 } else if (strcmp(argv[i], "-nodock") == 0 || strcmp(argv[i], "--no-dock") == 0) {
604 wPreferences.flags.nodock = 1;
605 } else if (strcmp(argv[i], "-noclip") == 0 || strcmp(argv[i], "--no-clip") == 0) {
606 wPreferences.flags.noclip = 1;
607 } else if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
608 printf("Window Maker %s\n", VERSION);
609 exit(0);
610 } else if (strcmp(argv[i], "--global_defaults_path") == 0) {
611 printf("%s/WindowMaker\n", SYSCONFDIR);
612 exit(0);
c98f229d dan1999-04-16 01:12:45 +0000613#ifdef DEBUG
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200614 } else if (strcmp(argv[i], "--synchronous") == 0) {
615 doSync = 1;
c98f229d dan1999-04-16 01:12:45 +0000616#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200617 } else if (strcmp(argv[i], "-locale") == 0 || strcmp(argv[i], "--locale") == 0) {
618 i++;
619 if (i >= argc) {
620 wwarning(_("too few arguments for %s"), argv[i - 1]);
621 exit(0);
622 }
623 Locale = argv[i];
624 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
625 i++;
626 if (i >= argc) {
627 wwarning(_("too few arguments for %s"), argv[i - 1]);
628 exit(0);
629 }
630 DisplayName = argv[i];
631 } else if (strcmp(argv[i], "-visualid") == 0 || strcmp(argv[i], "--visual-id") == 0) {
632 i++;
633 if (i >= argc) {
634 wwarning(_("too few arguments for %s"), argv[i - 1]);
635 exit(0);
636 }
637 if (sscanf(argv[i], "%i", &wVisualID) != 1) {
638 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
639 exit(0);
640 }
641 } else if (strcmp(argv[i], "-static") == 0 || strcmp(argv[i], "--static") == 0) {
642
643 wPreferences.flags.noupdates = 1;
416e3a82 dan1999-01-25 19:06:50 +0000644#ifdef XSMP_ENABLED
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200645 } else if (strcmp(argv[i], "-clientid") == 0 || strcmp(argv[i], "-restore") == 0) {
646 i++;
647 if (i >= argc) {
648 wwarning(_("too few arguments for %s"), argv[i - 1]);
649 exit(0);
650 }
9007a657 dan1998-11-23 11:32:19 +0000651#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200652 } else if (strcmp(argv[i], "--help") == 0) {
653 print_help();
654 exit(0);
655 } else {
656 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
657 printf(_("Try '%s --help' for more information\n"), argv[0]);
658 exit(1);
659 }
660 }
661 }
662
663 if (!wPreferences.flags.noupdates) {
664 /* check existence of Defaults DB directory */
665 check_defaults();
666 }
667
668 if (Locale) {
669 setenv("LANG", Locale, 1);
670 } else {
671 Locale = getenv("LC_ALL");
672 if (!Locale) {
673 Locale = getenv("LANG");
674 }
675 }
676
677 setlocale(LC_ALL, "");
678
679 if (!Locale || strcmp(Locale, "C") == 0 || strcmp(Locale, "POSIX") == 0)
680 Locale = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000681#ifdef I18N
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200682 if (getenv("NLSPATH"))
683 bindtextdomain("WindowMaker", getenv("NLSPATH"));
684 else
685 bindtextdomain("WindowMaker", LOCALEDIR);
686 bind_textdomain_codeset("WindowMaker", "UTF-8");
687 textdomain("WindowMaker");
688
689 if (!XSupportsLocale()) {
690 wwarning(_("X server does not support locale"));
691 }
692
693 if (XSetLocaleModifiers("") == NULL) {
694 wwarning(_("cannot set locale modifiers"));
695 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000696#endif
9007a657 dan1998-11-23 11:32:19 +0000697
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200698 if (Locale) {
699 char *ptr;
700
701 Locale = wstrdup(Locale);
702 ptr = strchr(Locale, '.');
703 if (ptr)
704 *ptr = 0;
705 }
706
707 /* open display */
708 dpy = XOpenDisplay(DisplayName);
709 if (dpy == NULL) {
710 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
711 exit(1);
712 }
713
714 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
715 wsyserror("error setting close-on-exec flag for X connection");
716 exit(1);
717 }
718
719 if (wVisualID < 0)
720 /*
721 * If unspecified, use default visual instead of waiting
722 * for wrlib/context.c:bestContext() that may end up choosing
723 * the "fake" 24 bits added by the Composite extension.
724 * This is required to avoid all sort of corruptions when
725 * composite is enabled, and at a depth other than 24.
726 */
727 wVisualID = (int)DefaultVisual(dpy, DefaultScreen(dpy))->visualid;
728
729 /* check if the user specified a complete display name (with screen).
730 * If so, only manage the specified screen */
731 if (DisplayName)
732 pos = strchr(DisplayName, ':');
733 else
734 pos = NULL;
735
736 if (pos && sscanf(pos, ":%i.%i", &d, &s) == 2)
737 multiHead = False;
738
739 DisplayName = XDisplayName(DisplayName);
740 setenv("DISPLAY", DisplayName, 1);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000741
52988491 kojima1999-03-16 11:27:01 +0000742#ifdef DEBUG
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200743 if (doSync)
744 XSynchronize(dpy, True);
52988491 kojima1999-03-16 11:27:01 +0000745#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000746
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200747 wXModifierInitialize();
9d2e6ef9 scottc1998-09-29 22:36:29 +0000748
416e3a82 dan1999-01-25 19:06:50 +0000749#ifdef XSMP_ENABLED
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200750 wSessionConnectManager(argv, argc);
9007a657 dan1998-11-23 11:32:19 +0000751#endif
6830b057 dan2004-10-12 21:28:27 +0000752
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200753 StartUp(!multiHead);
6830b057 dan2004-10-12 21:28:27 +0000754
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200755 if (wScreenCount == 1)
756 multiHead = False;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000757
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200758 execInitScript();
759 inotifyWatchConfig();
760 EventLoop();
761 return -1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000762}