show workspace name when changing workspace
[wmaker-crm.git] / src / main.c
blobc59cd8602a916bf91ae30d41beaedc3669f74f9f
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 **Arguments;
62 int ArgCount;
64 char *ProgName;
66 unsigned int ValidModMask = 0xff;
68 /* locale to use. NULL==POSIX or C */
69 char *Locale=NULL;
71 int wScreenCount=0;
73 WPreferences wPreferences;
76 proplist_t wDomainName;
77 proplist_t wAttributeDomainName;
79 WShortKey wKeyBindings[WKBD_LAST];
81 /* defaults domains */
82 WDDomain *WDWindowMaker = NULL;
83 WDDomain *WDRootMenu = NULL;
84 WDDomain *WDWindowAttributes = NULL;
87 /* XContexts */
88 XContext wWinContext;
89 XContext wAppWinContext;
90 XContext wStackContext;
92 /* Atoms */
93 Atom _XA_WM_STATE;
94 Atom _XA_WM_CHANGE_STATE;
95 Atom _XA_WM_PROTOCOLS;
96 Atom _XA_WM_TAKE_FOCUS;
97 Atom _XA_WM_DELETE_WINDOW;
98 Atom _XA_WM_SAVE_YOURSELF;
99 Atom _XA_WM_CLIENT_LEADER;
100 Atom _XA_WM_COLORMAP_WINDOWS;
101 Atom _XA_WM_COLORMAP_NOTIFY;
103 Atom _XA_GNUSTEP_WM_ATTR;
104 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
105 Atom _XA_GNUSTEP_WM_RESIZEBAR;
107 Atom _XA_WINDOWMAKER_MENU;
108 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
109 Atom _XA_WINDOWMAKER_STATE;
111 Atom _XA_WINDOWMAKER_WM_FUNCTION;
112 Atom _XA_WINDOWMAKER_NOTICEBOARD;
113 Atom _XA_WINDOWMAKER_COMMAND;
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;
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 if (dpy) {
188 #ifdef XSMP_ENABLED
189 wSessionDisconnectManager();
190 #endif
191 XCloseDisplay(dpy);
192 dpy = NULL;
194 if (!prog)
195 execvp(Arguments[0], Arguments);
196 else {
197 execvp(prog, argv);
198 /* fallback */
199 execv(Arguments[0], Arguments);
201 wsyserror(_("could not exec window manager"));
202 wfatal(_("Restart failed!!!"));
203 exit(-1);
208 void
209 SetupEnvironment(WScreen *scr)
211 char *tmp, *ptr;
212 char buf[16];
214 if (wScreenCount > 1) {
215 tmp = wmalloc(strlen(DisplayName)+64);
216 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
217 ptr = strchr(strchr(tmp, ':'), '.');
218 if (ptr)
219 *ptr = 0;
220 sprintf(buf, ".%i", scr->screen);
221 strcat(tmp, buf);
222 putenv(tmp);
224 tmp = wmalloc(60);
225 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
226 scr->rcontext->attribs->colors_per_channel);
227 putenv(tmp);
232 *---------------------------------------------------------------------
233 * wAbort--
234 * Do a major cleanup and exit the program
236 *----------------------------------------------------------------------
238 void
239 wAbort(Bool dumpCore)
241 int i;
242 WScreen *scr;
244 for (i=0; i<wScreenCount; i++) {
245 scr = wScreenWithNumber(i);
246 if (scr)
247 RestoreDesktop(scr);
249 printf(_("%s aborted.\n"), ProgName);
250 if (dumpCore)
251 abort();
252 else
253 exit(1);
257 void
258 print_help()
260 printf(_("Usage: %s [options]\n"), ProgName);
261 puts(_("The Window Maker window manager for the X window system"));
262 puts("");
263 puts(_(" -display host:dpy display to use"));
264 #ifdef USECPP
265 puts(_(" --no-cpp disable preprocessing of configuration files"));
266 #endif
267 puts(_(" --no-dock do not open the application Dock"));
268 puts(_(" --no-clip do not open the workspace Clip"));
270 puts(_(" --locale locale locale to use"));
272 puts(_(" --visual-id visualid visual id of visual to use"));
273 puts(_(" --static do not update or save configurations"));
274 #ifdef DEBUG
275 puts(_(" --synchronous turn on synchronous display mode"));
276 #endif
277 puts(_(" --version print version and exit"));
278 puts(_(" --help show this message"));
283 void
284 check_defaults()
286 char *path;
288 path = wdefaultspathfordomain("");
289 if (access(path, R_OK)!=0) {
290 wfatal(_("could not find user GNUstep directory (%s).\n"
291 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
292 path);
293 exit(1);
296 free(path);
300 static void
301 execInitScript()
303 char *file;
305 file = wfindfile(DEF_CONFIG_PATHS, DEF_INIT_SCRIPT);
306 if (file) {
307 if (system(file) != 0) {
308 wsyserror(_("%s:could not execute initialization script"), file);
310 #if 0
311 if (fork()==0) {
312 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
313 wsyserror(_("%s:could not execute initialization script"), file);
314 exit(1);
316 #endif
317 free(file);
322 void
323 ExecExitScript()
325 char *file;
327 file = wfindfile(DEF_CONFIG_PATHS, DEF_EXIT_SCRIPT);
328 if (file) {
329 if (system(file) != 0) {
330 wsyserror(_("%s:could not execute exit script"), file);
332 #if 0
333 if (fork()==0) {
334 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
335 wsyserror(_("%s:could not execute exit script"), file);
336 exit(1);
338 #endif
339 free(file);
346 main(int argc, char **argv)
348 int i, restart=0;
349 Bool multiHead = True;
350 char *str;
351 int d, s;
352 #ifdef DEBUG
353 Bool doSync = False;
354 #endif
356 wsetabort(wAbort);
358 ArgCount = argc;
359 Arguments = argv;
361 WMInitializeApplication("WindowMaker", &argc, argv);
364 ProgName = strrchr(argv[0],'/');
365 if (!ProgName)
366 ProgName = argv[0];
367 else
368 ProgName++;
371 restart = 0;
373 memset(&wPreferences, 0, sizeof(WPreferences));
375 if (argc>1) {
376 for (i=1; i<argc; i++) {
377 #ifdef USECPP
378 if (strcmp(argv[i], "-nocpp")==0
379 || strcmp(argv[i], "--no-cpp")==0) {
380 wPreferences.flags.nocpp=1;
381 } else
382 #endif
383 if (strcmp(argv[i], "-nodock")==0
384 || strcmp(argv[i], "--no-dock")==0) {
385 wPreferences.flags.nodock=1;
386 } else if (strcmp(argv[i], "-noclip")==0
387 || strcmp(argv[i], "--no-clip")==0) {
388 wPreferences.flags.noclip=1;
389 } else if (strcmp(argv[i], "-version")==0
390 || strcmp(argv[i], "--version")==0) {
391 printf("Window Maker %s\n", VERSION);
392 exit(0);
393 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
394 puts(SYSCONFDIR);
395 exit(0);
396 #ifdef DEBUG
397 } else if (strcmp(argv[i], "--synchronous")==0) {
398 doSync = 1;
399 #endif
400 } else if (strcmp(argv[i], "-locale")==0
401 || strcmp(argv[i], "--locale")==0) {
402 i++;
403 if (i>=argc) {
404 wwarning(_("too few arguments for %s"), argv[i-1]);
405 exit(0);
407 Locale = argv[i];
408 } else if (strcmp(argv[i], "-display")==0) {
409 i++;
410 if (i>=argc) {
411 wwarning(_("too few arguments for %s"), argv[i-1]);
412 exit(0);
414 DisplayName = argv[i];
415 } else if (strcmp(argv[i], "-visualid")==0
416 || strcmp(argv[i], "--visual-id")==0) {
417 i++;
418 if (i>=argc) {
419 wwarning(_("too few arguments for %s"), argv[i-1]);
420 exit(0);
422 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
423 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
424 exit(0);
426 } else if (strcmp(argv[i], "-static")==0
427 || strcmp(argv[i], "--static")==0) {
429 wPreferences.flags.noupdates = 1;
430 #ifdef XSMP_ENABLED
431 } else if (strcmp(argv[i], "-clientid")==0
432 || strcmp(argv[i], "-restore")==0) {
433 i++;
434 if (i>=argc) {
435 wwarning(_("too few arguments for %s"), argv[i-1]);
436 exit(0);
438 #endif
439 } else if (strcmp(argv[i], "--help")==0) {
440 print_help();
441 exit(0);
442 } else {
443 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
444 printf(_("Try '%s --help' for more information\n"), argv[0]);
445 exit(1);
450 if (!wPreferences.flags.noupdates) {
451 /* check existence of Defaults DB directory */
452 check_defaults();
455 #if 0
456 tmp = getenv("LANG");
457 if (tmp) {
458 if (setlocale(LC_ALL,"") == NULL) {
459 wwarning("cannot set locale %s", tmp);
460 wwarning("falling back to C locale");
461 setlocale(LC_ALL,"C");
462 Locale = NULL;
463 } else {
464 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
465 Locale = NULL;
466 else
467 Locale = tmp;
469 } else {
470 Locale = NULL;
472 #endif
473 if (!Locale) {
474 Locale = getenv("LANG");
476 setlocale(LC_ALL, Locale);
477 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
478 Locale = NULL;
479 #ifdef I18N
480 if (getenv("NLSPATH"))
481 bindtextdomain("WindowMaker", getenv("NLSPATH"));
482 else
483 bindtextdomain("WindowMaker", LOCALEDIR);
484 textdomain("WindowMaker");
486 if (!XSupportsLocale()) {
487 wwarning(_("X server does not support locale"));
489 if (XSetLocaleModifiers("") == NULL) {
490 wwarning(_("cannot set locale modifiers"));
492 #endif
494 if (Locale) {
495 char *ptr;
497 Locale = wstrdup(Locale);
498 ptr = strchr(Locale, '.');
499 if (ptr)
500 *ptr = 0;
504 /* open display */
505 dpy = XOpenDisplay(DisplayName);
506 if (dpy == NULL) {
507 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
508 exit(1);
511 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
512 wsyserror("error setting close-on-exec flag for X connection");
513 exit(1);
516 /* check if the user specified a complete display name (with screen).
517 * If so, only manage the specified screen */
518 if (DisplayName)
519 str = strchr(DisplayName, ':');
520 else
521 str = NULL;
523 if (str && sscanf(str, "%i.%i", &d, &s)==2)
524 multiHead = False;
526 DisplayName = XDisplayName(DisplayName);
527 str = wmalloc(strlen(DisplayName)+64);
528 sprintf(str, "DISPLAY=%s", DisplayName);
529 putenv(str);
531 #ifdef DEBUG
532 if (doSync)
533 XSynchronize(dpy, True);
534 #endif
536 wXModifierInitialize();
538 #ifdef XSMP_ENABLED
539 wSessionConnectManager(argv, argc);
540 #endif
542 StartUp(!multiHead);
544 execInitScript();
546 EventLoop();
547 return -1;