This update includes the 0.20.3pre3 code
[wmaker-crm.git] / src / main.c
blob58bf1fb5b8bc1723aa5bdf10bd73d4828f3b297a
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;
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 #ifdef MWM_HINTS
105 /* MWM support */
106 Atom _XA_MOTIF_WM_HINTS;
107 #endif
109 Atom _XA_WINDOWMAKER_MENU;
110 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
111 Atom _XA_WINDOWMAKER_STATE;
113 Atom _XA_WINDOWMAKER_WM_FUNCTION;
115 #ifdef OFFIX_DND
116 Atom _XA_DND_PROTOCOL;
117 Atom _XA_DND_SELECTION;
118 #endif
119 #ifdef XDE_DND
120 Atom _XA_XDE_REQUEST;
121 Atom _XA_XDE_ENTER;
122 Atom _XA_XDE_LEAVE;
123 Atom _XA_XDE_DATA_AVAILABLE;
124 Atom _XDE_FILETYPE;
125 Atom _XDE_URLTYPE;
126 #endif
129 /* cursors */
130 Cursor wCursor[WCUR_LAST];
132 /* last event timestamp for XSetInputFocus */
133 Time LastTimestamp;
134 /* timestamp on the last time we did XSetInputFocus() */
135 Time LastFocusChange;
137 #ifdef SHAPE
138 Bool wShapeSupported;
139 int wShapeEventBase;
140 #endif
143 /* special flags */
144 char WProgramState = WSTATE_NORMAL;
145 char WDelayedActionSet = 0;
147 /* temporary stuff */
148 int wVisualID = -1;
151 /******** End Global Variables *****/
153 static char *DisplayName = NULL;
154 static char **Arguments;
155 static int ArgCount;
157 extern void EventLoop();
158 extern void StartUp();
161 void
162 Exit(int status)
164 #ifdef R6SM
165 wSessionDisconnectManager();
166 #endif
167 XCloseDisplay(dpy);
169 exit(status);
172 void
173 Restart(char *manager)
175 char *prog=NULL;
176 char *argv[MAX_RESTART_ARGS];
177 int i;
179 if (manager && manager[0]!=0) {
180 prog = argv[0] = strtok(manager, " ");
181 for (i=1; i<MAX_RESTART_ARGS; i++) {
182 argv[i]=strtok(NULL, " ");
183 if (argv[i]==NULL) {
184 break;
188 #ifdef R6SM
189 wSessionDisconnectManager();
190 #endif
191 XCloseDisplay(dpy);
192 if (!prog)
193 execvp(Arguments[0], Arguments);
194 else {
195 execvp(prog, argv);
196 /* fallback */
197 execv(Arguments[0], Arguments);
199 wsyserror(_("could not exec window manager"));
200 wfatal(_("Restart failed!!!"));
201 exit(-1);
206 void
207 SetupEnvironment(WScreen *scr)
209 char *tmp, *ptr;
210 char buf[16];
212 if (wScreenCount > 1) {
213 tmp = wmalloc(strlen(DisplayName)+64);
214 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
215 ptr = strchr(strchr(tmp, ':'), '.');
216 if (ptr)
217 *ptr = 0;
218 sprintf(buf, ".%i", scr->screen);
219 strcat(tmp, buf);
220 putenv(tmp);
222 tmp = wmalloc(60);
223 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
224 scr->rcontext->attribs->colors_per_channel);
225 putenv(tmp);
230 *---------------------------------------------------------------------
231 * wAbort--
232 * Do a major cleanup and exit the program
234 *----------------------------------------------------------------------
236 void
237 wAbort(Bool dumpCore)
239 int i;
240 WScreen *scr;
242 for (i=0; i<wScreenCount; i++) {
243 scr = wScreenWithNumber(i);
244 if (scr)
245 RestoreDesktop(scr);
247 printf(_("%s aborted.\n"), ProgName);
248 if (dumpCore)
249 abort();
250 else
251 exit(1);
255 void
256 print_help()
258 printf(_("usage: %s [-options]\n"), ProgName);
259 puts(_("options:"));
260 #ifdef USECPP
261 puts(_(" -nocpp disable preprocessing of configuration files"));
262 #endif
263 puts(_(" -nodock do not open the application Dock"));
264 puts(_(" -noclip do not open the workspace Clip"));
266 puts(_(" -locale locale locale to use"));
268 puts(_(" -visualid visualid visual id of visual to use"));
269 puts(_(" -display host:dpy display to use"));
270 puts(_(" -static do not update or save configurations"));
271 puts(_(" -version print version and exit"));
276 void
277 check_defaults()
279 char *path;
281 path = wdefaultspathfordomain("");
282 if (access(path, R_OK)!=0) {
283 wfatal(_("could not find user GNUstep directory (%s).\n"
284 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
285 path);
286 exit(1);
289 free(path);
293 static void
294 execInitScript()
296 char *file;
298 file = wfindfile(DEF_CONFIG_PATHS, DEF_INIT_SCRIPT);
299 if (file) {
300 if (fork()==0) {
301 CloseDescriptors();
303 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
304 wsyserror(_("%s:could not execute initialization script"), file);
305 exit(1);
307 free(file);
312 void
313 ExecExitScript()
315 char *file;
317 file = wfindfile(DEF_CONFIG_PATHS, DEF_EXIT_SCRIPT);
318 if (file) {
319 if (fork()==0) {
320 CloseDescriptors();
322 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
323 wsyserror(_("%s:could not execute exit script"), file);
324 exit(1);
326 free(file);
333 main(int argc, char **argv)
335 int i, restart=0;
336 Bool multiHead = True;
337 char *str;
338 int d, s;
340 ArgCount = argc;
341 Arguments = argv;
343 WMInitializeApplication("WindowMaker", &argc, argv);
346 ProgName = strrchr(argv[0],'/');
347 if (!ProgName)
348 ProgName = argv[0];
349 else
350 ProgName++;
353 restart = 0;
355 memset(&wPreferences, 0, sizeof(WPreferences));
357 if (argc>1) {
358 for (i=1; i<argc; i++) {
359 #ifdef USECPP
360 if (strcmp(argv[i], "-nocpp")==0) {
361 wPreferences.flags.nocpp=1;
362 } else
363 #endif
364 if (strcmp(argv[i], "-nodock")==0) {
365 wPreferences.flags.nodock=1;
366 } else if (strcmp(argv[i], "-noclip")==0) {
367 wPreferences.flags.noclip=1;
368 } else if (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 printf("%s/Defaults/WindowMaker", PKGDATADIR);
373 exit(0);
374 } else if (strcmp(argv[i], "-locale")==0) {
375 i++;
376 if (i>=argc) {
377 wwarning(_("too few arguments for %s"), argv[i-1]);
378 exit(0);
380 Locale = argv[i];
381 } else if (strcmp(argv[i], "-display")==0) {
382 i++;
383 if (i>=argc) {
384 wwarning(_("too few arguments for %s"), argv[i-1]);
385 exit(0);
387 DisplayName = argv[i];
388 } else if (strcmp(argv[i], "-visualid")==0) {
389 i++;
390 if (i>=argc) {
391 wwarning(_("too few arguments for %s"), argv[i-1]);
392 exit(0);
394 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
395 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
396 exit(0);
398 } else if (strcmp(argv[i], "-static")==0) {
399 wPreferences.flags.noupdates = 1;
400 #ifdef R6SM
401 } else if (strcmp(argv[i], "-clientid")==0
402 || strcmp(argv[i], "-restore")==0) {
403 i++;
404 if (i>=argc) {
405 wwarning(_("too few arguments for %s"), argv[i-1]);
406 exit(0);
408 #endif
409 } else {
410 print_help();
411 exit(0);
416 if (!wPreferences.flags.noupdates) {
417 /* check existence of Defaults DB directory */
418 check_defaults();
421 #if 0
422 tmp = getenv("LANG");
423 if (tmp) {
424 if (setlocale(LC_ALL,"") == NULL) {
425 wwarning("cannot set locale %s", tmp);
426 wwarning("falling back to C locale");
427 setlocale(LC_ALL,"C");
428 Locale = NULL;
429 } else {
430 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
431 Locale = NULL;
432 else
433 Locale = tmp;
435 } else {
436 Locale = NULL;
438 #endif
439 if (!Locale) {
440 Locale = getenv("LANG");
442 setlocale(LC_ALL, Locale);
443 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
444 Locale = NULL;
445 #ifdef I18N
446 if (getenv("NLSPATH"))
447 bindtextdomain("WindowMaker", getenv("NLSPATH"));
448 else
449 bindtextdomain("WindowMaker", NLSDIR);
450 textdomain("WindowMaker");
452 if (!XSupportsLocale()) {
453 wwarning(_("X server does not support locale"));
455 if (XSetLocaleModifiers("") == NULL) {
456 wwarning(_("cannot set locale modifiers"));
458 #endif
460 if (Locale) {
461 char *ptr;
463 Locale = wstrdup(Locale);
464 ptr = strchr(Locale, '.');
465 if (ptr)
466 *ptr = 0;
470 /* open display */
471 dpy = XOpenDisplay(DisplayName);
472 if (dpy == NULL) {
473 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
474 exit(1);
477 /* check if the user specified a complete display name (with screen).
478 * If so, only manage the specified screen */
479 if (DisplayName)
480 str = strchr(DisplayName, ':');
481 else
482 str = NULL;
483 if (!str)
484 str = "";
485 if (sscanf(str, "%i.%i", &d, &s)==2)
486 multiHead = False;
488 DisplayName = XDisplayName(DisplayName);
489 str = wmalloc(strlen(DisplayName)+64);
490 sprintf(str, "DISPLAY=%s", DisplayName);
491 putenv(str);
493 #ifdef DEBUG
494 XSynchronize(dpy, True);
495 #endif
497 wXModifierInitialize();
499 #ifdef SOUNDS
500 wSoundInitialize();
501 #endif
503 #ifdef R6SM
504 wSessionConnectManager(argv, argc);
505 #endif
507 StartUp(!multiHead);
509 execInitScript();
511 EventLoop();
512 return -1;