Updating to version 0.20.2
[wmaker-crm.git] / src / main.c
blob098be86ee33db594f2699bbafcb19a77b84d83a0
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"
50 #include <proplist.h>
53 /****** Global Variables ******/
55 /* general info */
57 Display *dpy;
59 char *ProgName;
61 unsigned int ValidModMask = 0xff;
63 /* locale to use. NULL==POSIX or C */
64 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;
98 Atom _XA_GNUSTEP_WM_ATTR;
99 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
100 Atom _XA_GNUSTEP_WM_RESIZEBAR;
102 #ifdef MWM_HINTS
103 /* MWM support */
104 Atom _XA_MOTIF_WM_HINTS;
105 #endif
107 Atom _XA_WINDOWMAKER_MENU;
108 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
109 Atom _XA_WINDOWMAKER_STATE;
111 Atom _XA_WINDOWMAKER_WM_FUNCTION;
113 #ifdef OFFIX_DND
114 Atom _XA_DND_PROTOCOL;
115 Atom _XA_DND_SELECTION;
116 #endif
117 #ifdef XDE_DND
118 Atom _XA_XDE_REQUEST;
119 Atom _XA_XDE_ENTER;
120 Atom _XA_XDE_LEAVE;
121 Atom _XA_XDE_DATA_AVAILABLE;
122 Atom _XDE_FILETYPE;
123 Atom _XDE_URLTYPE;
124 #endif
127 /* cursors */
128 Cursor wCursor[WCUR_LAST];
130 /* last event timestamp for XSetInputFocus */
131 Time LastTimestamp;
132 /* timestamp on the last time we did XSetInputFocus() */
133 Time LastFocusChange;
135 #ifdef SHAPE
136 Bool wShapeSupported;
137 int wShapeEventBase;
138 #endif
141 /* special flags */
142 char WProgramState = WSTATE_NORMAL;
143 char WDelayedActionSet = 0;
145 /* temporary stuff */
146 int wVisualID = -1;
149 /******** End Global Variables *****/
151 static char *DisplayName = NULL;
152 static char **Arguments;
153 static int ArgCount;
155 extern void EventLoop();
156 extern void StartUp();
160 void
161 Restart(char *manager)
163 char *prog=NULL;
164 char *argv[MAX_RESTART_ARGS];
165 int i;
167 if (manager && manager[0]!=0) {
168 prog = argv[0] = strtok(manager, " ");
169 for (i=1; i<MAX_RESTART_ARGS; i++) {
170 argv[i]=strtok(NULL, " ");
171 if (argv[i]==NULL) {
172 break;
177 XCloseDisplay(dpy);
178 if (!prog)
179 execvp(Arguments[0], Arguments);
180 else {
181 execvp(prog, argv);
182 /* fallback */
183 execv(Arguments[0], Arguments);
185 wsyserror(_("could not exec window manager"));
186 wfatal(_("Restart failed!!!"));
187 exit(-1);
192 void
193 SetupEnvironment(WScreen *scr)
195 char *tmp, *ptr;
196 char buf[16];
198 if (wScreenCount > 1) {
199 tmp = wmalloc(strlen(DisplayName)+64);
200 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
201 ptr = strchr(strchr(tmp, ':'), '.');
202 if (ptr)
203 *ptr = 0;
204 sprintf(buf, ".%i", scr->screen);
205 strcat(tmp, buf);
206 putenv(tmp);
208 tmp = wmalloc(60);
209 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
210 scr->rcontext->attribs->colors_per_channel);
211 putenv(tmp);
216 *---------------------------------------------------------------------
217 * wAbort--
218 * Do a major cleanup and exit the program
220 *----------------------------------------------------------------------
222 void
223 wAbort(Bool dumpCore)
225 int i;
226 WScreen *scr;
228 for (i=0; i<wScreenCount; i++) {
229 scr = wScreenWithNumber(i);
230 if (scr)
231 RestoreDesktop(scr);
233 printf(_("%s aborted.\n"), ProgName);
234 if (dumpCore)
235 abort();
236 else
237 exit(1);
241 void
242 print_help()
244 printf(_("usage: %s [-options]\n"), ProgName);
245 puts(_("options:"));
246 #ifdef USECPP
247 puts(_(" -nocpp disable preprocessing of configuration files"));
248 #endif
249 puts(_(" -nodock do not open the application Dock"));
250 puts(_(" -noclip do not open the workspace Clip"));
252 puts(_(" -locale locale locale to use"));
254 puts(_(" -visualid visualid visual id of visual to use"));
255 puts(_(" -display host:dpy display to use"));
256 puts(_(" -version print version and exit"));
261 void
262 check_defaults()
264 char *path;
266 path = wdefaultspathfordomain("");
267 if (access(path, R_OK)!=0) {
268 wfatal(_("could not find user GNUstep directory (%s).\n"
269 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
270 path);
271 exit(1);
274 free(path);
279 static void
280 execInitScript()
282 char *file;
284 file = wfindfile(DEF_CONFIG_PATHS, DEF_INIT_SCRIPT);
285 if (file) {
286 if (fork()==0) {
287 close(ConnectionNumber(dpy));
289 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
290 wsyserror(_("%s:could not execute initialization script"), file);
291 exit(1);
293 free(file);
298 void
299 ExecExitScript()
301 char *file;
303 file = wfindfile(DEF_CONFIG_PATHS, DEF_EXIT_SCRIPT);
304 if (file) {
305 if (fork()==0) {
306 close(ConnectionNumber(dpy));
308 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
309 wsyserror(_("%s:could not execute exit script"), file);
310 exit(1);
312 free(file);
319 main(int argc, char **argv)
321 int i, restart=0;
322 Bool multiHead = True;
323 char *str;
324 int d, s;
326 ArgCount = argc;
327 Arguments = argv;
329 WMInitializeApplication("WindowMaker", &argc, argv);
332 ProgName = strrchr(argv[0],'/');
333 if (!ProgName)
334 ProgName = argv[0];
335 else
336 ProgName++;
339 /* check existence of Defaults DB directory */
340 check_defaults();
342 restart = 0;
344 memset(&wPreferences, 0, sizeof(WPreferences));
346 if (argc>1) {
347 for (i=1; i<argc; i++) {
348 #ifdef USECPP
349 if (strcmp(argv[i], "-nocpp")==0) {
350 wPreferences.flags.nocpp=1;
351 } else
352 #endif
353 if (strcmp(argv[i], "-nodock")==0) {
354 wPreferences.flags.nodock=1;
355 } else if (strcmp(argv[i], "-noclip")==0) {
356 wPreferences.flags.noclip=1;
357 } else if (strcmp(argv[i], "-version")==0) {
358 printf("Window Maker %s\n", VERSION);
359 exit(0);
360 } else if (strcmp(argv[i], "-global_defaults_path")==0) {
361 printf("%s/Defaults/WindowMaker", PKGDATADIR);
362 exit(0);
363 } else if (strcmp(argv[i], "-locale")==0) {
364 i++;
365 if (i>=argc) {
366 wwarning(_("too few arguments for %s"), argv[i-1]);
367 exit(0);
369 Locale = argv[i];
370 } else if (strcmp(argv[i], "-display")==0) {
371 i++;
372 if (i>=argc) {
373 wwarning(_("too few arguments for %s"), argv[i-1]);
374 exit(0);
376 DisplayName = argv[i];
377 } else if (strcmp(argv[i], "-visualid")==0) {
378 i++;
379 if (i>=argc) {
380 wwarning(_("too few arguments for %s"), argv[i-1]);
381 exit(0);
383 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
384 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
385 exit(0);
387 } else {
388 print_help();
389 exit(0);
393 #if 0
394 tmp = getenv("LANG");
395 if (tmp) {
396 if (setlocale(LC_ALL,"") == NULL) {
397 wwarning("cannot set locale %s", tmp);
398 wwarning("falling back to C locale");
399 setlocale(LC_ALL,"C");
400 Locale = NULL;
401 } else {
402 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
403 Locale = NULL;
404 else
405 Locale = tmp;
407 } else {
408 Locale = NULL;
410 #endif
411 if (!Locale) {
412 Locale = getenv("LANG");
414 setlocale(LC_ALL, Locale);
415 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
416 Locale = NULL;
417 #ifdef I18N
418 if (getenv("NLSPATH"))
419 bindtextdomain("WindowMaker", getenv("NLSPATH"));
420 else
421 bindtextdomain("WindowMaker", NLSDIR);
422 textdomain("WindowMaker");
424 if (!XSupportsLocale()) {
425 wwarning(_("X server does not support locale"));
427 if (XSetLocaleModifiers("") == NULL) {
428 wwarning(_("cannot set locale modifiers"));
430 #endif
432 if (Locale) {
433 char *ptr;
435 Locale = wstrdup(Locale);
436 ptr = strchr(Locale, '.');
437 if (ptr)
438 *ptr = 0;
442 /* open display */
443 dpy = XOpenDisplay(DisplayName);
444 if (dpy == NULL) {
445 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
446 exit(1);
449 /* check if the user specified a complete display name (with screen).
450 * If so, only manage the specified screen */
451 if (DisplayName)
452 str = strchr(DisplayName, ':');
453 else
454 str = NULL;
455 if (!str)
456 str = "";
457 if (sscanf(str, "%i.%i", &d, &s)==2)
458 multiHead = False;
460 DisplayName = XDisplayName(DisplayName);
461 str = wmalloc(strlen(DisplayName)+64);
462 sprintf(str, "DISPLAY=%s", DisplayName);
463 putenv(str);
465 #ifdef DEBUG
466 XSynchronize(dpy, True);
467 #endif
469 wXModifierInitialize();
471 #ifdef SOUNDS
472 wSoundInitialize();
473 #endif
474 StartUp(!multiHead);
476 execInitScript();
478 EventLoop();
479 return -1;