Code update for Window Maker version 0.50.0
[wmaker-crm.git] / src / main.c
blob51a131cf3d3bed73f55f7f84c42c87d26c651882
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 Atom _XA_WINDOWMAKER_MENU;
105 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
106 Atom _XA_WINDOWMAKER_STATE;
108 Atom _XA_WINDOWMAKER_WM_FUNCTION;
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 R6SM
160 wSessionDisconnectManager();
161 #endif
162 XCloseDisplay(dpy);
164 exit(status);
167 void
168 Restart(char *manager)
170 char *prog=NULL;
171 char *argv[MAX_RESTART_ARGS];
172 int i;
174 if (manager && manager[0]!=0) {
175 prog = argv[0] = strtok(manager, " ");
176 for (i=1; i<MAX_RESTART_ARGS; i++) {
177 argv[i]=strtok(NULL, " ");
178 if (argv[i]==NULL) {
179 break;
183 #ifdef R6SM
184 wSessionDisconnectManager();
185 #endif
186 XCloseDisplay(dpy);
187 if (!prog)
188 execvp(Arguments[0], Arguments);
189 else {
190 execvp(prog, argv);
191 /* fallback */
192 execv(Arguments[0], Arguments);
194 wsyserror(_("could not exec window manager"));
195 wfatal(_("Restart failed!!!"));
196 exit(-1);
201 void
202 SetupEnvironment(WScreen *scr)
204 char *tmp, *ptr;
205 char buf[16];
207 if (wScreenCount > 1) {
208 tmp = wmalloc(strlen(DisplayName)+64);
209 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
210 ptr = strchr(strchr(tmp, ':'), '.');
211 if (ptr)
212 *ptr = 0;
213 sprintf(buf, ".%i", scr->screen);
214 strcat(tmp, buf);
215 putenv(tmp);
217 tmp = wmalloc(60);
218 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
219 scr->rcontext->attribs->colors_per_channel);
220 putenv(tmp);
225 *---------------------------------------------------------------------
226 * wAbort--
227 * Do a major cleanup and exit the program
229 *----------------------------------------------------------------------
231 void
232 wAbort(Bool dumpCore)
234 int i;
235 WScreen *scr;
237 for (i=0; i<wScreenCount; i++) {
238 scr = wScreenWithNumber(i);
239 if (scr)
240 RestoreDesktop(scr);
242 printf(_("%s aborted.\n"), ProgName);
243 if (dumpCore)
244 abort();
245 else
246 exit(1);
250 void
251 print_help()
253 printf(_("usage: %s [-options]\n"), ProgName);
254 puts(_("options:"));
255 #ifdef USECPP
256 puts(_(" -nocpp disable preprocessing of configuration files"));
257 #endif
258 puts(_(" -nodock do not open the application Dock"));
259 puts(_(" -noclip do not open the workspace Clip"));
261 puts(_(" -locale locale locale to use"));
263 puts(_(" -visualid visualid visual id of visual to use"));
264 puts(_(" -display host:dpy display to use"));
265 puts(_(" -static do not update or save configurations"));
266 puts(_(" -version print version and exit"));
271 void
272 check_defaults()
274 char *path;
276 path = wdefaultspathfordomain("");
277 if (access(path, R_OK)!=0) {
278 wfatal(_("could not find user GNUstep directory (%s).\n"
279 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
280 path);
281 exit(1);
284 free(path);
288 static void
289 execInitScript()
291 char *file;
293 file = wfindfile(DEF_CONFIG_PATHS, DEF_INIT_SCRIPT);
294 if (file) {
295 if (fork()==0) {
296 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
297 wsyserror(_("%s:could not execute initialization script"), file);
298 exit(1);
300 free(file);
305 void
306 ExecExitScript()
308 char *file;
310 file = wfindfile(DEF_CONFIG_PATHS, DEF_EXIT_SCRIPT);
311 if (file) {
312 if (fork()==0) {
313 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
314 wsyserror(_("%s:could not execute exit script"), file);
315 exit(1);
317 free(file);
324 main(int argc, char **argv)
326 int i, restart=0;
327 Bool multiHead = True;
328 char *str;
329 int d, s;
331 ArgCount = argc;
332 Arguments = argv;
334 WMInitializeApplication("WindowMaker", &argc, argv);
337 ProgName = strrchr(argv[0],'/');
338 if (!ProgName)
339 ProgName = argv[0];
340 else
341 ProgName++;
344 restart = 0;
346 memset(&wPreferences, 0, sizeof(WPreferences));
348 if (argc>1) {
349 for (i=1; i<argc; i++) {
350 #ifdef USECPP
351 if (strcmp(argv[i], "-nocpp")==0) {
352 wPreferences.flags.nocpp=1;
353 } else
354 #endif
355 if (strcmp(argv[i], "-nodock")==0) {
356 wPreferences.flags.nodock=1;
357 } else if (strcmp(argv[i], "-noclip")==0) {
358 wPreferences.flags.noclip=1;
359 } else if (strcmp(argv[i], "-version")==0) {
360 printf("Window Maker %s\n", VERSION);
361 exit(0);
362 } else if (strcmp(argv[i], "-global_defaults_path")==0) {
363 printf("%s/Defaults/WindowMaker", PKGDATADIR);
364 exit(0);
365 } else if (strcmp(argv[i], "-locale")==0) {
366 i++;
367 if (i>=argc) {
368 wwarning(_("too few arguments for %s"), argv[i-1]);
369 exit(0);
371 Locale = argv[i];
372 } else if (strcmp(argv[i], "-display")==0) {
373 i++;
374 if (i>=argc) {
375 wwarning(_("too few arguments for %s"), argv[i-1]);
376 exit(0);
378 DisplayName = argv[i];
379 } else if (strcmp(argv[i], "-visualid")==0) {
380 i++;
381 if (i>=argc) {
382 wwarning(_("too few arguments for %s"), argv[i-1]);
383 exit(0);
385 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
386 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
387 exit(0);
389 } else if (strcmp(argv[i], "-static")==0) {
390 wPreferences.flags.noupdates = 1;
391 #ifdef R6SM
392 } else if (strcmp(argv[i], "-clientid")==0
393 || strcmp(argv[i], "-restore")==0) {
394 i++;
395 if (i>=argc) {
396 wwarning(_("too few arguments for %s"), argv[i-1]);
397 exit(0);
399 #endif
400 } else {
401 print_help();
402 exit(0);
407 if (!wPreferences.flags.noupdates) {
408 /* check existence of Defaults DB directory */
409 check_defaults();
412 #if 0
413 tmp = getenv("LANG");
414 if (tmp) {
415 if (setlocale(LC_ALL,"") == NULL) {
416 wwarning("cannot set locale %s", tmp);
417 wwarning("falling back to C locale");
418 setlocale(LC_ALL,"C");
419 Locale = NULL;
420 } else {
421 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
422 Locale = NULL;
423 else
424 Locale = tmp;
426 } else {
427 Locale = NULL;
429 #endif
430 if (!Locale) {
431 Locale = getenv("LANG");
433 setlocale(LC_ALL, Locale);
434 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
435 Locale = NULL;
436 #ifdef I18N
437 if (getenv("NLSPATH"))
438 bindtextdomain("WindowMaker", getenv("NLSPATH"));
439 else
440 bindtextdomain("WindowMaker", NLSDIR);
441 textdomain("WindowMaker");
443 if (!XSupportsLocale()) {
444 wwarning(_("X server does not support locale"));
446 if (XSetLocaleModifiers("") == NULL) {
447 wwarning(_("cannot set locale modifiers"));
449 #endif
451 if (Locale) {
452 char *ptr;
454 Locale = wstrdup(Locale);
455 ptr = strchr(Locale, '.');
456 if (ptr)
457 *ptr = 0;
461 /* open display */
462 dpy = XOpenDisplay(DisplayName);
463 if (dpy == NULL) {
464 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
465 exit(1);
468 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
469 wsyserror("error setting close-on-exec flag for X connection");
470 exit(1);
473 /* check if the user specified a complete display name (with screen).
474 * If so, only manage the specified screen */
475 if (DisplayName)
476 str = strchr(DisplayName, ':');
477 else
478 str = NULL;
479 if (!str)
480 str = "";
481 if (sscanf(str, "%i.%i", &d, &s)==2)
482 multiHead = False;
484 DisplayName = XDisplayName(DisplayName);
485 str = wmalloc(strlen(DisplayName)+64);
486 sprintf(str, "DISPLAY=%s", DisplayName);
487 putenv(str);
489 #ifdef DEBUG
490 XSynchronize(dpy, True);
491 #endif
493 wXModifierInitialize();
495 #ifdef SOUNDS
496 wSoundInitialize();
497 #endif
499 #ifdef R6SM
500 wSessionConnectManager(argv, argc);
501 #endif
503 StartUp(!multiHead);
505 execInitScript();
507 EventLoop();
508 return -1;