Syncing some files updated after running configure
[wmaker-crm.git] / src / main.c
bloba105a012d8e38e4826b5a09c2b6d368c2816355c
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
6 * 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.
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.
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
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
36 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
37 * and if X_LOCALE is defined, X's locale emulating functions will be used.
38 * See Xlocale.h for more information.
40 #include <X11/Xlocale.h>
42 #define MAINFILE
44 #include "WindowMaker.h"
45 #include "window.h"
46 #include "funcs.h"
47 #include "menu.h"
48 #include "keybind.h"
49 #include "xmodifier.h"
50 #include "defaults.h"
51 #include "session.h"
53 #include <proplist.h>
55 /****** Global Variables ******/
57 /* general info */
59 Display *dpy;
61 char *ProgName;
63 unsigned int ValidModMask = 0xff;
65 /* locale to use. NULL==POSIX or C */
66 char *Locale=NULL;
68 int wScreenCount=0;
70 WPreferences wPreferences;
73 proplist_t wDomainName;
74 proplist_t wAttributeDomainName;
76 WShortKey wKeyBindings[WKBD_LAST];
78 /* defaults domains */
79 WDDomain *WDWindowMaker = NULL;
80 WDDomain *WDRootMenu = NULL;
81 WDDomain *WDWindowAttributes = NULL;
84 /* XContexts */
85 XContext wWinContext;
86 XContext wAppWinContext;
87 XContext wStackContext;
89 /* Atoms */
90 Atom _XA_WM_STATE;
91 Atom _XA_WM_CHANGE_STATE;
92 Atom _XA_WM_PROTOCOLS;
93 Atom _XA_WM_TAKE_FOCUS;
94 Atom _XA_WM_DELETE_WINDOW;
95 Atom _XA_WM_SAVE_YOURSELF;
96 Atom _XA_WM_CLIENT_LEADER;
97 Atom _XA_WM_COLORMAP_WINDOWS;
98 Atom _XA_WM_COLORMAP_NOTIFY;
100 Atom _XA_GNUSTEP_WM_ATTR;
101 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
102 Atom _XA_GNUSTEP_WM_RESIZEBAR;
104 Atom _XA_WINDOWMAKER_MENU;
105 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
106 Atom _XA_WINDOWMAKER_STATE;
108 Atom _XA_WINDOWMAKER_WM_FUNCTION;
109 Atom _XA_WINDOWMAKER_NOTICEBOARD;
110 Atom _XA_WINDOWMAKER_COMMAND;
112 #ifdef OFFIX_DND
113 Atom _XA_DND_PROTOCOL;
114 Atom _XA_DND_SELECTION;
115 #endif
116 #ifdef XDE_DND
117 Atom _XA_XDE_REQUEST;
118 Atom _XA_XDE_ENTER;
119 Atom _XA_XDE_LEAVE;
120 Atom _XA_XDE_DATA_AVAILABLE;
121 Atom _XDE_FILETYPE;
122 Atom _XDE_URLTYPE;
123 #endif
126 /* cursors */
127 Cursor wCursor[WCUR_LAST];
129 /* last event timestamp for XSetInputFocus */
130 Time LastTimestamp;
131 /* timestamp on the last time we did XSetInputFocus() */
132 Time LastFocusChange;
134 #ifdef SHAPE
135 Bool wShapeSupported;
136 int wShapeEventBase;
137 #endif
140 /* special flags */
141 char WProgramState = WSTATE_NORMAL;
142 char WDelayedActionSet = 0;
144 /* temporary stuff */
145 int wVisualID = -1;
148 /******** End Global Variables *****/
150 static char *DisplayName = NULL;
152 static char **Arguments;
154 static int ArgCount;
156 extern void EventLoop();
157 extern void StartUp();
160 void
161 Exit(int status)
163 #ifdef XSMP_ENABLED
164 wSessionDisconnectManager();
165 #endif
166 if (dpy)
167 XCloseDisplay(dpy);
169 exit(status);
173 void
174 Restart(char *manager, Bool abortOnFailure)
176 char *prog=NULL;
177 char *argv[MAX_RESTART_ARGS];
178 int i;
180 if (manager && manager[0]!=0) {
181 prog = argv[0] = strtok(manager, " ");
182 for (i=1; i<MAX_RESTART_ARGS; i++) {
183 argv[i]=strtok(NULL, " ");
184 if (argv[i]==NULL) {
185 break;
189 if (dpy) {
190 #ifdef XSMP_ENABLED
191 wSessionDisconnectManager();
192 #endif
193 XCloseDisplay(dpy);
194 dpy = NULL;
196 if (!prog) {
197 execvp(Arguments[0], Arguments);
198 wfatal(_("failed to restart Window Maker."));
199 } else {
200 execvp(prog, argv);
201 wsyserror(_("could not exec %s"), prog);
203 if (abortOnFailure)
204 exit(-1);
209 void
210 SetupEnvironment(WScreen *scr)
212 char *tmp, *ptr;
213 char buf[16];
215 if (wScreenCount > 1) {
216 tmp = wmalloc(strlen(DisplayName)+64);
217 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
218 ptr = strchr(strchr(tmp, ':'), '.');
219 if (ptr)
220 *ptr = 0;
221 sprintf(buf, ".%i", scr->screen);
222 strcat(tmp, buf);
223 putenv(tmp);
225 tmp = wmalloc(60);
226 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
227 scr->rcontext->attribs->colors_per_channel);
228 putenv(tmp);
233 *---------------------------------------------------------------------
234 * wAbort--
235 * Do a major cleanup and exit the program
237 *----------------------------------------------------------------------
239 void
240 wAbort(Bool dumpCore)
242 int i;
243 WScreen *scr;
245 for (i=0; i<wScreenCount; i++) {
246 scr = wScreenWithNumber(i);
247 if (scr)
248 RestoreDesktop(scr);
250 printf(_("%s aborted.\n"), ProgName);
251 if (dumpCore)
252 abort();
253 else
254 exit(1);
258 void
259 print_help()
261 printf(_("Usage: %s [options]\n"), ProgName);
262 puts(_("The Window Maker window manager for the X window system"));
263 puts("");
264 puts(_(" -display host:dpy display to use"));
265 #ifdef USECPP
266 puts(_(" --no-cpp disable preprocessing of configuration files"));
267 #endif
268 puts(_(" --no-dock do not open the application Dock"));
269 puts(_(" --no-clip do not open the workspace Clip"));
271 puts(_(" --locale locale locale to use"));
273 puts(_(" --visual-id visualid visual id of visual to use"));
274 puts(_(" --static do not update or save configurations"));
275 #ifdef DEBUG
276 puts(_(" --synchronous turn on synchronous display mode"));
277 #endif
278 puts(_(" --version print version and exit"));
279 puts(_(" --help show this message"));
284 void
285 check_defaults()
287 char *path;
289 path = wdefaultspathfordomain("");
290 if (access(path, R_OK)!=0) {
291 wfatal(_("could not find user GNUstep directory (%s).\n"
292 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
293 path);
294 exit(1);
297 free(path);
301 static void
302 execInitScript()
304 char *file;
306 file = wfindfile(DEF_CONFIG_PATHS, DEF_INIT_SCRIPT);
307 if (file) {
308 if (system(file) != 0) {
309 wsyserror(_("%s:could not execute initialization script"), file);
311 #if 0
312 if (fork()==0) {
313 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
314 wsyserror(_("%s:could not execute initialization script"), file);
315 exit(1);
317 #endif
318 free(file);
323 void
324 ExecExitScript()
326 char *file;
328 file = wfindfile(DEF_CONFIG_PATHS, DEF_EXIT_SCRIPT);
329 if (file) {
330 if (system(file) != 0) {
331 wsyserror(_("%s:could not execute exit script"), file);
333 #if 0
334 if (fork()==0) {
335 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
336 wsyserror(_("%s:could not execute exit script"), file);
337 exit(1);
339 #endif
340 free(file);
347 main(int argc, char **argv)
349 int i, restart=0;
350 Bool multiHead = True;
351 char *str;
352 int d, s;
353 #ifdef DEBUG
354 Bool doSync = False;
355 #endif
357 wsetabort(wAbort);
359 ArgCount = argc;
360 Arguments = argv;
362 WMInitializeApplication("WindowMaker", &argc, argv);
365 ProgName = strrchr(argv[0],'/');
366 if (!ProgName)
367 ProgName = argv[0];
368 else
369 ProgName++;
372 restart = 0;
374 memset(&wPreferences, 0, sizeof(WPreferences));
376 if (argc>1) {
377 for (i=1; i<argc; i++) {
378 #ifdef USECPP
379 if (strcmp(argv[i], "-nocpp")==0
380 || strcmp(argv[i], "--no-cpp")==0) {
381 wPreferences.flags.nocpp=1;
382 } else
383 #endif
384 if (strcmp(argv[i], "-nodock")==0
385 || strcmp(argv[i], "--no-dock")==0) {
386 wPreferences.flags.nodock=1;
387 } else if (strcmp(argv[i], "-noclip")==0
388 || strcmp(argv[i], "--no-clip")==0) {
389 wPreferences.flags.noclip=1;
390 } else if (strcmp(argv[i], "-version")==0
391 || strcmp(argv[i], "--version")==0) {
392 printf("Window Maker %s\n", VERSION);
393 exit(0);
394 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
395 puts(SYSCONFDIR);
396 exit(0);
397 #ifdef DEBUG
398 } else if (strcmp(argv[i], "--synchronous")==0) {
399 doSync = 1;
400 #endif
401 } else if (strcmp(argv[i], "-locale")==0
402 || strcmp(argv[i], "--locale")==0) {
403 i++;
404 if (i>=argc) {
405 wwarning(_("too few arguments for %s"), argv[i-1]);
406 exit(0);
408 Locale = argv[i];
409 } else if (strcmp(argv[i], "-display")==0) {
410 i++;
411 if (i>=argc) {
412 wwarning(_("too few arguments for %s"), argv[i-1]);
413 exit(0);
415 DisplayName = argv[i];
416 } else if (strcmp(argv[i], "-visualid")==0
417 || strcmp(argv[i], "--visual-id")==0) {
418 i++;
419 if (i>=argc) {
420 wwarning(_("too few arguments for %s"), argv[i-1]);
421 exit(0);
423 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
424 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
425 exit(0);
427 } else if (strcmp(argv[i], "-static")==0
428 || strcmp(argv[i], "--static")==0) {
430 wPreferences.flags.noupdates = 1;
431 #ifdef XSMP_ENABLED
432 } else if (strcmp(argv[i], "-clientid")==0
433 || strcmp(argv[i], "-restore")==0) {
434 i++;
435 if (i>=argc) {
436 wwarning(_("too few arguments for %s"), argv[i-1]);
437 exit(0);
439 #endif
440 } else if (strcmp(argv[i], "--help")==0) {
441 print_help();
442 exit(0);
443 } else {
444 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
445 printf(_("Try '%s --help' for more information\n"), argv[0]);
446 exit(1);
451 if (!wPreferences.flags.noupdates) {
452 /* check existence of Defaults DB directory */
453 check_defaults();
456 #if 0
457 tmp = getenv("LANG");
458 if (tmp) {
459 if (setlocale(LC_ALL,"") == NULL) {
460 wwarning("cannot set locale %s", tmp);
461 wwarning("falling back to C locale");
462 setlocale(LC_ALL,"C");
463 Locale = NULL;
464 } else {
465 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
466 Locale = NULL;
467 else
468 Locale = tmp;
470 } else {
471 Locale = NULL;
473 #endif
474 if (!Locale) {
475 Locale = getenv("LANG");
477 setlocale(LC_ALL, Locale);
478 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
479 Locale = NULL;
480 #ifdef I18N
481 if (getenv("NLSPATH"))
482 bindtextdomain("WindowMaker", getenv("NLSPATH"));
483 else
484 bindtextdomain("WindowMaker", LOCALEDIR);
485 textdomain("WindowMaker");
487 if (!XSupportsLocale()) {
488 wwarning(_("X server does not support locale"));
490 if (XSetLocaleModifiers("") == NULL) {
491 wwarning(_("cannot set locale modifiers"));
493 #endif
495 if (Locale) {
496 char *ptr;
498 Locale = wstrdup(Locale);
499 ptr = strchr(Locale, '.');
500 if (ptr)
501 *ptr = 0;
505 /* open display */
506 dpy = XOpenDisplay(DisplayName);
507 if (dpy == NULL) {
508 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
509 exit(1);
512 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
513 wsyserror("error setting close-on-exec flag for X connection");
514 exit(1);
517 /* check if the user specified a complete display name (with screen).
518 * If so, only manage the specified screen */
519 if (DisplayName)
520 str = strchr(DisplayName, ':');
521 else
522 str = NULL;
524 if (str && sscanf(str, "%i.%i", &d, &s)==2)
525 multiHead = False;
527 DisplayName = XDisplayName(DisplayName);
528 str = wmalloc(strlen(DisplayName)+64);
529 sprintf(str, "DISPLAY=%s", DisplayName);
530 putenv(str);
532 #ifdef DEBUG
533 if (doSync)
534 XSynchronize(dpy, True);
535 #endif
537 wXModifierInitialize();
539 #ifdef XSMP_ENABLED
540 wSessionConnectManager(argv, argc);
541 #endif
543 StartUp(!multiHead);
545 execInitScript();
547 EventLoop();
548 return -1;