WPrefs appearance stuff update (shows preview)
[wmaker-crm.git] / src / main.c
blobb51095bcef30bf644a84b3660bf9880f140a0707
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 #include "WindowMaker.h"
43 #include "window.h"
44 #include "funcs.h"
45 #include "menu.h"
46 #include "keybind.h"
47 #include "xmodifier.h"
48 #include "defaults.h"
49 #include "session.h"
51 #include <proplist.h>
54 /****** Global Variables ******/
56 /* general info */
58 Display *dpy;
60 char *ProgName;
62 unsigned int ValidModMask = 0xff;
64 /* locale to use. NULL==POSIX or C */
65 char *Locale=NULL;
67 int wScreenCount=0;
69 WPreferences wPreferences;
72 proplist_t wDomainName;
73 proplist_t wAttributeDomainName;
75 WShortKey wKeyBindings[WKBD_LAST];
77 /* defaults domains */
78 WDDomain *WDWindowMaker = NULL;
79 WDDomain *WDRootMenu = NULL;
80 WDDomain *WDWindowAttributes = NULL;
83 /* XContexts */
84 XContext wWinContext;
85 XContext wAppWinContext;
86 XContext wStackContext;
88 /* Atoms */
89 Atom _XA_WM_STATE;
90 Atom _XA_WM_CHANGE_STATE;
91 Atom _XA_WM_PROTOCOLS;
92 Atom _XA_WM_TAKE_FOCUS;
93 Atom _XA_WM_DELETE_WINDOW;
94 Atom _XA_WM_SAVE_YOURSELF;
95 Atom _XA_WM_CLIENT_LEADER;
96 Atom _XA_WM_COLORMAP_WINDOWS;
97 Atom _XA_WM_COLORMAP_NOTIFY;
99 Atom _XA_GNUSTEP_WM_ATTR;
100 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
101 Atom _XA_GNUSTEP_WM_RESIZEBAR;
103 Atom _XA_WINDOWMAKER_MENU;
104 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
105 Atom _XA_WINDOWMAKER_STATE;
107 Atom _XA_WINDOWMAKER_WM_FUNCTION;
108 Atom _XA_WINDOWMAKER_NOTICEBOARD;
110 #ifdef OFFIX_DND
111 Atom _XA_DND_PROTOCOL;
112 Atom _XA_DND_SELECTION;
113 #endif
114 #ifdef XDE_DND
115 Atom _XA_XDE_REQUEST;
116 Atom _XA_XDE_ENTER;
117 Atom _XA_XDE_LEAVE;
118 Atom _XA_XDE_DATA_AVAILABLE;
119 Atom _XDE_FILETYPE;
120 Atom _XDE_URLTYPE;
121 #endif
124 /* cursors */
125 Cursor wCursor[WCUR_LAST];
127 /* last event timestamp for XSetInputFocus */
128 Time LastTimestamp;
129 /* timestamp on the last time we did XSetInputFocus() */
130 Time LastFocusChange;
132 #ifdef SHAPE
133 Bool wShapeSupported;
134 int wShapeEventBase;
135 #endif
138 /* special flags */
139 char WProgramState = WSTATE_NORMAL;
140 char WDelayedActionSet = 0;
142 /* temporary stuff */
143 int wVisualID = -1;
146 /******** End Global Variables *****/
148 static char *DisplayName = NULL;
149 static char **Arguments;
150 static int ArgCount;
152 extern void EventLoop();
153 extern void StartUp();
156 void
157 Exit(int status)
159 #ifdef XSMP_ENABLED
160 wSessionDisconnectManager();
161 #endif
162 if (dpy)
163 XCloseDisplay(dpy);
165 exit(status);
168 void
169 Restart(char *manager)
171 char *prog=NULL;
172 char *argv[MAX_RESTART_ARGS];
173 int i;
175 if (manager && manager[0]!=0) {
176 prog = argv[0] = strtok(manager, " ");
177 for (i=1; i<MAX_RESTART_ARGS; i++) {
178 argv[i]=strtok(NULL, " ");
179 if (argv[i]==NULL) {
180 break;
184 #ifdef XSMP_ENABLED
185 wSessionDisconnectManager();
186 #endif
187 XCloseDisplay(dpy);
188 if (!prog)
189 execvp(Arguments[0], Arguments);
190 else {
191 execvp(prog, argv);
192 /* fallback */
193 execv(Arguments[0], Arguments);
195 wsyserror(_("could not exec window manager"));
196 wfatal(_("Restart failed!!!"));
197 exit(-1);
202 void
203 SetupEnvironment(WScreen *scr)
205 char *tmp, *ptr;
206 char buf[16];
208 if (wScreenCount > 1) {
209 tmp = wmalloc(strlen(DisplayName)+64);
210 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
211 ptr = strchr(strchr(tmp, ':'), '.');
212 if (ptr)
213 *ptr = 0;
214 sprintf(buf, ".%i", scr->screen);
215 strcat(tmp, buf);
216 putenv(tmp);
218 tmp = wmalloc(60);
219 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
220 scr->rcontext->attribs->colors_per_channel);
221 putenv(tmp);
226 *---------------------------------------------------------------------
227 * wAbort--
228 * Do a major cleanup and exit the program
230 *----------------------------------------------------------------------
232 void
233 wAbort(Bool dumpCore)
235 int i;
236 WScreen *scr;
238 for (i=0; i<wScreenCount; i++) {
239 scr = wScreenWithNumber(i);
240 if (scr)
241 RestoreDesktop(scr);
243 printf(_("%s aborted.\n"), ProgName);
244 if (dumpCore)
245 abort();
246 else
247 exit(1);
251 void
252 print_help()
254 printf(_("Usage: %s [options]\n"), ProgName);
255 puts(_("The Window Maker window manager for the X window system"));
256 puts("");
257 puts(_(" -display host:dpy display to use"));
258 #ifdef USECPP
259 puts(_(" --no-cpp disable preprocessing of configuration files"));
260 #endif
261 puts(_(" --no-dock do not open the application Dock"));
262 puts(_(" --no-clip do not open the workspace Clip"));
264 puts(_(" --locale locale locale to use"));
266 puts(_(" --visual-id visualid visual id of visual to use"));
267 puts(_(" --static do not update or save configurations"));
268 puts(_(" --version print version and exit"));
269 puts(_(" --help show this message"));
274 void
275 check_defaults()
277 char *path;
279 path = wdefaultspathfordomain("");
280 if (access(path, R_OK)!=0) {
281 wfatal(_("could not find user GNUstep directory (%s).\n"
282 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
283 path);
284 exit(1);
287 free(path);
291 static void
292 execInitScript()
294 char *file;
296 file = wfindfile(DEF_CONFIG_PATHS, DEF_INIT_SCRIPT);
297 if (file) {
298 if (fork()==0) {
299 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
300 wsyserror(_("%s:could not execute initialization script"), file);
301 exit(1);
303 free(file);
308 void
309 ExecExitScript()
311 char *file;
313 file = wfindfile(DEF_CONFIG_PATHS, DEF_EXIT_SCRIPT);
314 if (file) {
315 if (fork()==0) {
316 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
317 wsyserror(_("%s:could not execute exit script"), file);
318 exit(1);
320 free(file);
327 main(int argc, char **argv)
329 int i, restart=0;
330 Bool multiHead = True;
331 char *str;
332 int d, s;
334 wsetabort(wAbort);
336 ArgCount = argc;
337 Arguments = argv;
339 WMInitializeApplication("WindowMaker", &argc, argv);
342 ProgName = strrchr(argv[0],'/');
343 if (!ProgName)
344 ProgName = argv[0];
345 else
346 ProgName++;
349 restart = 0;
351 memset(&wPreferences, 0, sizeof(WPreferences));
353 if (argc>1) {
354 for (i=1; i<argc; i++) {
355 #ifdef USECPP
356 if (strcmp(argv[i], "-nocpp")==0
357 || strcmp(argv[i], "--no-cpp")==0) {
358 wPreferences.flags.nocpp=1;
359 } else
360 #endif
361 if (strcmp(argv[i], "-nodock")==0
362 || strcmp(argv[i], "--no-dock")==0) {
363 wPreferences.flags.nodock=1;
364 } else if (strcmp(argv[i], "-noclip")==0
365 || strcmp(argv[i], "--no-clip")==0) {
366 wPreferences.flags.noclip=1;
367 } else if (strcmp(argv[i], "-version")==0
368 || strcmp(argv[i], "--version")==0) {
369 printf("Window Maker %s\n", VERSION);
370 exit(0);
371 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
372 puts(SYSCONFDIR);
373 exit(0);
374 } else if (strcmp(argv[i], "-locale")==0
375 || strcmp(argv[i], "--locale")==0) {
376 i++;
377 if (i>=argc) {
378 wwarning(_("too few arguments for %s"), argv[i-1]);
379 exit(0);
381 Locale = argv[i];
382 } else if (strcmp(argv[i], "-display")==0) {
383 i++;
384 if (i>=argc) {
385 wwarning(_("too few arguments for %s"), argv[i-1]);
386 exit(0);
388 DisplayName = argv[i];
389 } else if (strcmp(argv[i], "-visualid")==0
390 || strcmp(argv[i], "--visual-id")==0) {
391 i++;
392 if (i>=argc) {
393 wwarning(_("too few arguments for %s"), argv[i-1]);
394 exit(0);
396 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
397 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
398 exit(0);
400 } else if (strcmp(argv[i], "-static")==0
401 || strcmp(argv[i], "--static")==0) {
403 wPreferences.flags.noupdates = 1;
404 #ifdef XSMP_ENABLED
405 } else if (strcmp(argv[i], "-clientid")==0
406 || strcmp(argv[i], "-restore")==0) {
407 i++;
408 if (i>=argc) {
409 wwarning(_("too few arguments for %s"), argv[i-1]);
410 exit(0);
412 #endif
413 } else if (strcmp(argv[i], "--help")==0) {
414 print_help();
415 exit(0);
416 } else {
417 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
418 printf(_("Try '%s --help' for more information\n"), argv[0]);
419 exit(1);
424 if (!wPreferences.flags.noupdates) {
425 /* check existence of Defaults DB directory */
426 check_defaults();
429 #if 0
430 tmp = getenv("LANG");
431 if (tmp) {
432 if (setlocale(LC_ALL,"") == NULL) {
433 wwarning("cannot set locale %s", tmp);
434 wwarning("falling back to C locale");
435 setlocale(LC_ALL,"C");
436 Locale = NULL;
437 } else {
438 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
439 Locale = NULL;
440 else
441 Locale = tmp;
443 } else {
444 Locale = NULL;
446 #endif
447 if (!Locale) {
448 Locale = getenv("LANG");
450 setlocale(LC_ALL, Locale);
451 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
452 Locale = NULL;
453 #ifdef I18N
454 if (getenv("NLSPATH"))
455 bindtextdomain("WindowMaker", getenv("NLSPATH"));
456 else
457 bindtextdomain("WindowMaker", LOCALEDIR);
458 textdomain("WindowMaker");
460 if (!XSupportsLocale()) {
461 wwarning(_("X server does not support locale"));
463 if (XSetLocaleModifiers("") == NULL) {
464 wwarning(_("cannot set locale modifiers"));
466 #endif
468 if (Locale) {
469 char *ptr;
471 Locale = wstrdup(Locale);
472 ptr = strchr(Locale, '.');
473 if (ptr)
474 *ptr = 0;
478 /* open display */
479 dpy = XOpenDisplay(DisplayName);
480 if (dpy == NULL) {
481 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
482 exit(1);
485 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
486 wsyserror("error setting close-on-exec flag for X connection");
487 exit(1);
490 /* check if the user specified a complete display name (with screen).
491 * If so, only manage the specified screen */
492 if (DisplayName)
493 str = strchr(DisplayName, ':');
494 else
495 str = NULL;
497 if (str && sscanf(str, "%i.%i", &d, &s)==2)
498 multiHead = False;
500 DisplayName = XDisplayName(DisplayName);
501 str = wmalloc(strlen(DisplayName)+64);
502 sprintf(str, "DISPLAY=%s", DisplayName);
503 putenv(str);
505 #ifdef DEBUG
506 XSynchronize(dpy, True);
507 #endif
509 wXModifierInitialize();
511 #ifdef SOUNDS
512 wSoundInitialize();
513 #endif
515 #ifdef XSMP_ENABLED
516 wSessionConnectManager(argv, argc);
517 #endif
519 StartUp(!multiHead);
521 execInitScript();
523 EventLoop();
524 return -1;