ResizebarBack option
[wmaker-crm.git] / src / main.c
blob06a6547a386593c9a9f38e0e396d003503b79a6b
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;
151 static char **Arguments;
152 static int ArgCount;
154 extern void EventLoop();
155 extern void StartUp();
158 void
159 Exit(int status)
161 #ifdef XSMP_ENABLED
162 wSessionDisconnectManager();
163 #endif
164 if (dpy)
165 XCloseDisplay(dpy);
167 exit(status);
170 void
171 Restart(char *manager)
173 char *prog=NULL;
174 char *argv[MAX_RESTART_ARGS];
175 int i;
177 if (manager && manager[0]!=0) {
178 prog = argv[0] = strtok(manager, " ");
179 for (i=1; i<MAX_RESTART_ARGS; i++) {
180 argv[i]=strtok(NULL, " ");
181 if (argv[i]==NULL) {
182 break;
186 #ifdef XSMP_ENABLED
187 wSessionDisconnectManager();
188 #endif
189 XCloseDisplay(dpy);
190 if (!prog)
191 execvp(Arguments[0], Arguments);
192 else {
193 execvp(prog, argv);
194 /* fallback */
195 execv(Arguments[0], Arguments);
197 wsyserror(_("could not exec window manager"));
198 wfatal(_("Restart failed!!!"));
199 exit(-1);
204 void
205 SetupEnvironment(WScreen *scr)
207 char *tmp, *ptr;
208 char buf[16];
210 if (wScreenCount > 1) {
211 tmp = wmalloc(strlen(DisplayName)+64);
212 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
213 ptr = strchr(strchr(tmp, ':'), '.');
214 if (ptr)
215 *ptr = 0;
216 sprintf(buf, ".%i", scr->screen);
217 strcat(tmp, buf);
218 putenv(tmp);
220 tmp = wmalloc(60);
221 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
222 scr->rcontext->attribs->colors_per_channel);
223 putenv(tmp);
228 *---------------------------------------------------------------------
229 * wAbort--
230 * Do a major cleanup and exit the program
232 *----------------------------------------------------------------------
234 void
235 wAbort(Bool dumpCore)
237 int i;
238 WScreen *scr;
240 for (i=0; i<wScreenCount; i++) {
241 scr = wScreenWithNumber(i);
242 if (scr)
243 RestoreDesktop(scr);
245 printf(_("%s aborted.\n"), ProgName);
246 if (dumpCore)
247 abort();
248 else
249 exit(1);
253 void
254 print_help()
256 printf(_("Usage: %s [options]\n"), ProgName);
257 puts(_("The Window Maker window manager for the X window system"));
258 puts("");
259 puts(_(" -display host:dpy display to use"));
260 #ifdef USECPP
261 puts(_(" --no-cpp disable preprocessing of configuration files"));
262 #endif
263 puts(_(" --no-dock do not open the application Dock"));
264 puts(_(" --no-clip do not open the workspace Clip"));
266 puts(_(" --locale locale locale to use"));
268 puts(_(" --visual-id visualid visual id of visual to use"));
269 puts(_(" --static do not update or save configurations"));
270 puts(_(" --version print version and exit"));
271 puts(_(" --help show this message"));
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 (system(file) != 0) {
301 wsyserror(_("%s:could not execute initialization script"), file);
303 #if 0
304 if (fork()==0) {
305 execl("/bin/sh", "/bin/sh", "-c",file, NULL);
306 wsyserror(_("%s:could not execute initialization script"), file);
307 exit(1);
309 #endif
310 free(file);
315 void
316 ExecExitScript()
318 char *file;
320 file = wfindfile(DEF_CONFIG_PATHS, DEF_EXIT_SCRIPT);
321 if (file) {
322 if (system(file) != 0) {
323 wsyserror(_("%s:could not execute exit script"), file);
325 #if 0
326 if (fork()==0) {
327 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
328 wsyserror(_("%s:could not execute exit script"), file);
329 exit(1);
331 #endif
332 free(file);
339 main(int argc, char **argv)
341 int i, restart=0;
342 Bool multiHead = True;
343 char *str;
344 int d, s;
346 wsetabort(wAbort);
348 ArgCount = argc;
349 Arguments = argv;
351 WMInitializeApplication("WindowMaker", &argc, argv);
354 ProgName = strrchr(argv[0],'/');
355 if (!ProgName)
356 ProgName = argv[0];
357 else
358 ProgName++;
361 restart = 0;
363 memset(&wPreferences, 0, sizeof(WPreferences));
365 if (argc>1) {
366 for (i=1; i<argc; i++) {
367 #ifdef USECPP
368 if (strcmp(argv[i], "-nocpp")==0
369 || strcmp(argv[i], "--no-cpp")==0) {
370 wPreferences.flags.nocpp=1;
371 } else
372 #endif
373 if (strcmp(argv[i], "-nodock")==0
374 || strcmp(argv[i], "--no-dock")==0) {
375 wPreferences.flags.nodock=1;
376 } else if (strcmp(argv[i], "-noclip")==0
377 || strcmp(argv[i], "--no-clip")==0) {
378 wPreferences.flags.noclip=1;
379 } else if (strcmp(argv[i], "-version")==0
380 || strcmp(argv[i], "--version")==0) {
381 printf("Window Maker %s\n", VERSION);
382 exit(0);
383 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
384 puts(SYSCONFDIR);
385 exit(0);
386 } else if (strcmp(argv[i], "-locale")==0
387 || strcmp(argv[i], "--locale")==0) {
388 i++;
389 if (i>=argc) {
390 wwarning(_("too few arguments for %s"), argv[i-1]);
391 exit(0);
393 Locale = argv[i];
394 } else if (strcmp(argv[i], "-display")==0) {
395 i++;
396 if (i>=argc) {
397 wwarning(_("too few arguments for %s"), argv[i-1]);
398 exit(0);
400 DisplayName = argv[i];
401 } else if (strcmp(argv[i], "-visualid")==0
402 || strcmp(argv[i], "--visual-id")==0) {
403 i++;
404 if (i>=argc) {
405 wwarning(_("too few arguments for %s"), argv[i-1]);
406 exit(0);
408 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
409 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
410 exit(0);
412 } else if (strcmp(argv[i], "-static")==0
413 || strcmp(argv[i], "--static")==0) {
415 wPreferences.flags.noupdates = 1;
416 #ifdef XSMP_ENABLED
417 } else if (strcmp(argv[i], "-clientid")==0
418 || strcmp(argv[i], "-restore")==0) {
419 i++;
420 if (i>=argc) {
421 wwarning(_("too few arguments for %s"), argv[i-1]);
422 exit(0);
424 #endif
425 } else if (strcmp(argv[i], "--help")==0) {
426 print_help();
427 exit(0);
428 } else {
429 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
430 printf(_("Try '%s --help' for more information\n"), argv[0]);
431 exit(1);
436 if (!wPreferences.flags.noupdates) {
437 /* check existence of Defaults DB directory */
438 check_defaults();
441 #if 0
442 tmp = getenv("LANG");
443 if (tmp) {
444 if (setlocale(LC_ALL,"") == NULL) {
445 wwarning("cannot set locale %s", tmp);
446 wwarning("falling back to C locale");
447 setlocale(LC_ALL,"C");
448 Locale = NULL;
449 } else {
450 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
451 Locale = NULL;
452 else
453 Locale = tmp;
455 } else {
456 Locale = NULL;
458 #endif
459 if (!Locale) {
460 Locale = getenv("LANG");
462 setlocale(LC_ALL, Locale);
463 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
464 Locale = NULL;
465 #ifdef I18N
466 if (getenv("NLSPATH"))
467 bindtextdomain("WindowMaker", getenv("NLSPATH"));
468 else
469 bindtextdomain("WindowMaker", LOCALEDIR);
470 textdomain("WindowMaker");
472 if (!XSupportsLocale()) {
473 wwarning(_("X server does not support locale"));
475 if (XSetLocaleModifiers("") == NULL) {
476 wwarning(_("cannot set locale modifiers"));
478 #endif
480 if (Locale) {
481 char *ptr;
483 Locale = wstrdup(Locale);
484 ptr = strchr(Locale, '.');
485 if (ptr)
486 *ptr = 0;
490 /* open display */
491 dpy = XOpenDisplay(DisplayName);
492 if (dpy == NULL) {
493 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
494 exit(1);
497 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
498 wsyserror("error setting close-on-exec flag for X connection");
499 exit(1);
502 /* check if the user specified a complete display name (with screen).
503 * If so, only manage the specified screen */
504 if (DisplayName)
505 str = strchr(DisplayName, ':');
506 else
507 str = NULL;
509 if (str && sscanf(str, "%i.%i", &d, &s)==2)
510 multiHead = False;
512 DisplayName = XDisplayName(DisplayName);
513 str = wmalloc(strlen(DisplayName)+64);
514 sprintf(str, "DISPLAY=%s", DisplayName);
515 putenv(str);
517 #ifdef DEBUG
518 XSynchronize(dpy, True);
519 #endif
521 wXModifierInitialize();
523 #ifdef XSMP_ENABLED
524 wSessionConnectManager(argv, argc);
525 #endif
527 StartUp(!multiHead);
529 execInitScript();
531 EventLoop();
532 return -1;