new/changelog fix
[wmaker-crm.git] / src / main.c
blob77724a7bc38521d4cdf40a7697717cbd1d10a8cf
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>
56 /****** Global Variables ******/
58 /* general info */
60 Display *dpy;
62 char *ProgName;
64 unsigned int ValidModMask = 0xff;
66 /* locale to use. NULL==POSIX or C */
67 char *Locale=NULL;
69 int wScreenCount=0;
71 WPreferences wPreferences;
74 proplist_t wDomainName;
75 proplist_t wAttributeDomainName;
77 WShortKey wKeyBindings[WKBD_LAST];
79 /* defaults domains */
80 WDDomain *WDWindowMaker = NULL;
81 WDDomain *WDRootMenu = NULL;
82 WDDomain *WDWindowAttributes = NULL;
85 /* XContexts */
86 XContext wWinContext;
87 XContext wAppWinContext;
88 XContext wStackContext;
90 /* Atoms */
91 Atom _XA_WM_STATE;
92 Atom _XA_WM_CHANGE_STATE;
93 Atom _XA_WM_PROTOCOLS;
94 Atom _XA_WM_TAKE_FOCUS;
95 Atom _XA_WM_DELETE_WINDOW;
96 Atom _XA_WM_SAVE_YOURSELF;
97 Atom _XA_WM_CLIENT_LEADER;
98 Atom _XA_WM_COLORMAP_WINDOWS;
99 Atom _XA_WM_COLORMAP_NOTIFY;
101 Atom _XA_GNUSTEP_WM_ATTR;
102 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
103 Atom _XA_GNUSTEP_WM_RESIZEBAR;
105 Atom _XA_WINDOWMAKER_MENU;
106 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
107 Atom _XA_WINDOWMAKER_STATE;
109 Atom _XA_WINDOWMAKER_WM_FUNCTION;
110 Atom _XA_WINDOWMAKER_NOTICEBOARD;
111 Atom _XA_WINDOWMAKER_COMMAND;
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();
159 void
160 Exit(int status)
162 #ifdef XSMP_ENABLED
163 wSessionDisconnectManager();
164 #endif
165 if (dpy)
166 XCloseDisplay(dpy);
168 exit(status);
171 void
172 Restart(char *manager)
174 char *prog=NULL;
175 char *argv[MAX_RESTART_ARGS];
176 int i;
178 if (manager && manager[0]!=0) {
179 prog = argv[0] = strtok(manager, " ");
180 for (i=1; i<MAX_RESTART_ARGS; i++) {
181 argv[i]=strtok(NULL, " ");
182 if (argv[i]==NULL) {
183 break;
187 #ifdef XSMP_ENABLED
188 wSessionDisconnectManager();
189 #endif
190 XCloseDisplay(dpy);
191 if (!prog)
192 execvp(Arguments[0], Arguments);
193 else {
194 execvp(prog, argv);
195 /* fallback */
196 execv(Arguments[0], Arguments);
198 wsyserror(_("could not exec window manager"));
199 wfatal(_("Restart failed!!!"));
200 exit(-1);
205 void
206 SetupEnvironment(WScreen *scr)
208 char *tmp, *ptr;
209 char buf[16];
211 if (wScreenCount > 1) {
212 tmp = wmalloc(strlen(DisplayName)+64);
213 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
214 ptr = strchr(strchr(tmp, ':'), '.');
215 if (ptr)
216 *ptr = 0;
217 sprintf(buf, ".%i", scr->screen);
218 strcat(tmp, buf);
219 putenv(tmp);
221 tmp = wmalloc(60);
222 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
223 scr->rcontext->attribs->colors_per_channel);
224 putenv(tmp);
229 *---------------------------------------------------------------------
230 * wAbort--
231 * Do a major cleanup and exit the program
233 *----------------------------------------------------------------------
235 void
236 wAbort(Bool dumpCore)
238 int i;
239 WScreen *scr;
241 for (i=0; i<wScreenCount; i++) {
242 scr = wScreenWithNumber(i);
243 if (scr)
244 RestoreDesktop(scr);
246 printf(_("%s aborted.\n"), ProgName);
247 if (dumpCore)
248 abort();
249 else
250 exit(1);
254 void
255 print_help()
257 printf(_("Usage: %s [options]\n"), ProgName);
258 puts(_("The Window Maker window manager for the X window system"));
259 puts("");
260 puts(_(" -display host:dpy display to use"));
261 #ifdef USECPP
262 puts(_(" --no-cpp disable preprocessing of configuration files"));
263 #endif
264 puts(_(" --no-dock do not open the application Dock"));
265 puts(_(" --no-clip do not open the workspace Clip"));
267 puts(_(" --locale locale locale to use"));
269 puts(_(" --visual-id visualid visual id of visual to use"));
270 puts(_(" --static do not update or save configurations"));
271 puts(_(" --version print version and exit"));
272 puts(_(" --help show this message"));
277 void
278 check_defaults()
280 char *path;
282 path = wdefaultspathfordomain("");
283 if (access(path, R_OK)!=0) {
284 wfatal(_("could not find user GNUstep directory (%s).\n"
285 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
286 path);
287 exit(1);
290 free(path);
294 static void
295 execInitScript()
297 char *file;
299 file = wfindfile(DEF_CONFIG_PATHS, DEF_INIT_SCRIPT);
300 if (file) {
301 if (fork()==0) {
302 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
303 wsyserror(_("%s:could not execute initialization script"), file);
304 exit(1);
306 free(file);
311 void
312 ExecExitScript()
314 char *file;
316 file = wfindfile(DEF_CONFIG_PATHS, DEF_EXIT_SCRIPT);
317 if (file) {
318 if (fork()==0) {
319 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
320 wsyserror(_("%s:could not execute exit script"), file);
321 exit(1);
323 free(file);
330 main(int argc, char **argv)
332 int i, restart=0;
333 Bool multiHead = True;
334 char *str;
335 int d, s;
337 wsetabort(wAbort);
339 ArgCount = argc;
340 Arguments = argv;
342 WMInitializeApplication("WindowMaker", &argc, argv);
345 ProgName = strrchr(argv[0],'/');
346 if (!ProgName)
347 ProgName = argv[0];
348 else
349 ProgName++;
352 restart = 0;
354 memset(&wPreferences, 0, sizeof(WPreferences));
356 if (argc>1) {
357 for (i=1; i<argc; i++) {
358 #ifdef USECPP
359 if (strcmp(argv[i], "-nocpp")==0
360 || strcmp(argv[i], "--no-cpp")==0) {
361 wPreferences.flags.nocpp=1;
362 } else
363 #endif
364 if (strcmp(argv[i], "-nodock")==0
365 || strcmp(argv[i], "--no-dock")==0) {
366 wPreferences.flags.nodock=1;
367 } else if (strcmp(argv[i], "-noclip")==0
368 || strcmp(argv[i], "--no-clip")==0) {
369 wPreferences.flags.noclip=1;
370 } else if (strcmp(argv[i], "-version")==0
371 || strcmp(argv[i], "--version")==0) {
372 printf("Window Maker %s\n", VERSION);
373 exit(0);
374 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
375 puts(SYSCONFDIR);
376 exit(0);
377 } else if (strcmp(argv[i], "-locale")==0
378 || strcmp(argv[i], "--locale")==0) {
379 i++;
380 if (i>=argc) {
381 wwarning(_("too few arguments for %s"), argv[i-1]);
382 exit(0);
384 Locale = argv[i];
385 } else if (strcmp(argv[i], "-display")==0) {
386 i++;
387 if (i>=argc) {
388 wwarning(_("too few arguments for %s"), argv[i-1]);
389 exit(0);
391 DisplayName = argv[i];
392 } else if (strcmp(argv[i], "-visualid")==0
393 || strcmp(argv[i], "--visual-id")==0) {
394 i++;
395 if (i>=argc) {
396 wwarning(_("too few arguments for %s"), argv[i-1]);
397 exit(0);
399 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
400 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
401 exit(0);
403 } else if (strcmp(argv[i], "-static")==0
404 || strcmp(argv[i], "--static")==0) {
406 wPreferences.flags.noupdates = 1;
407 #ifdef XSMP_ENABLED
408 } else if (strcmp(argv[i], "-clientid")==0
409 || strcmp(argv[i], "-restore")==0) {
410 i++;
411 if (i>=argc) {
412 wwarning(_("too few arguments for %s"), argv[i-1]);
413 exit(0);
415 #endif
416 } else if (strcmp(argv[i], "--help")==0) {
417 print_help();
418 exit(0);
419 } else {
420 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
421 printf(_("Try '%s --help' for more information\n"), argv[0]);
422 exit(1);
427 if (!wPreferences.flags.noupdates) {
428 /* check existence of Defaults DB directory */
429 check_defaults();
432 #if 0
433 tmp = getenv("LANG");
434 if (tmp) {
435 if (setlocale(LC_ALL,"") == NULL) {
436 wwarning("cannot set locale %s", tmp);
437 wwarning("falling back to C locale");
438 setlocale(LC_ALL,"C");
439 Locale = NULL;
440 } else {
441 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
442 Locale = NULL;
443 else
444 Locale = tmp;
446 } else {
447 Locale = NULL;
449 #endif
450 if (!Locale) {
451 Locale = getenv("LANG");
453 setlocale(LC_ALL, Locale);
454 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
455 Locale = NULL;
456 #ifdef I18N
457 if (getenv("NLSPATH"))
458 bindtextdomain("WindowMaker", getenv("NLSPATH"));
459 else
460 bindtextdomain("WindowMaker", LOCALEDIR);
461 textdomain("WindowMaker");
463 if (!XSupportsLocale()) {
464 wwarning(_("X server does not support locale"));
466 if (XSetLocaleModifiers("") == NULL) {
467 wwarning(_("cannot set locale modifiers"));
469 #endif
471 if (Locale) {
472 char *ptr;
474 Locale = wstrdup(Locale);
475 ptr = strchr(Locale, '.');
476 if (ptr)
477 *ptr = 0;
481 /* open display */
482 dpy = XOpenDisplay(DisplayName);
483 if (dpy == NULL) {
484 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
485 exit(1);
488 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
489 wsyserror("error setting close-on-exec flag for X connection");
490 exit(1);
493 /* check if the user specified a complete display name (with screen).
494 * If so, only manage the specified screen */
495 if (DisplayName)
496 str = strchr(DisplayName, ':');
497 else
498 str = NULL;
500 if (str && sscanf(str, "%i.%i", &d, &s)==2)
501 multiHead = False;
503 DisplayName = XDisplayName(DisplayName);
504 str = wmalloc(strlen(DisplayName)+64);
505 sprintf(str, "DISPLAY=%s", DisplayName);
506 putenv(str);
508 #ifdef DEBUG
509 XSynchronize(dpy, True);
510 #endif
512 wXModifierInitialize();
514 #ifdef SOUNDS
515 wSoundInitialize();
516 #endif
518 #ifdef XSMP_ENABLED
519 wSessionConnectManager(argv, argc);
520 #endif
522 StartUp(!multiHead);
524 execInitScript();
526 EventLoop();
527 return -1;