xdnd drop support on dock.
[wmaker-crm.git] / src / main.c
blob5d260337952727e338bf40857385356fab2ad599
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
117 /* cursors */
118 Cursor wCursor[WCUR_LAST];
120 /* last event timestamp for XSetInputFocus */
121 Time LastTimestamp;
122 /* timestamp on the last time we did XSetInputFocus() */
123 Time LastFocusChange;
125 #ifdef SHAPE
126 Bool wShapeSupported;
127 int wShapeEventBase;
128 #endif
131 /* special flags */
132 char WProgramState = WSTATE_NORMAL;
133 char WDelayedActionSet = 0;
135 /* temporary stuff */
136 int wVisualID = -1;
139 /******** End Global Variables *****/
141 static char *DisplayName = NULL;
143 static char **Arguments;
145 static int ArgCount;
147 extern void EventLoop();
148 extern void StartUp();
151 void
152 Exit(int status)
154 #ifdef XSMP_ENABLED
155 wSessionDisconnectManager();
156 #endif
157 if (dpy)
158 XCloseDisplay(dpy);
160 exit(status);
164 void
165 Restart(char *manager, Bool abortOnFailure)
167 char *prog=NULL;
168 char *argv[MAX_RESTART_ARGS];
169 int i;
171 if (manager && manager[0]!=0) {
172 prog = argv[0] = strtok(manager, " ");
173 for (i=1; i<MAX_RESTART_ARGS; i++) {
174 argv[i]=strtok(NULL, " ");
175 if (argv[i]==NULL) {
176 break;
180 if (dpy) {
181 #ifdef XSMP_ENABLED
182 wSessionDisconnectManager();
183 #endif
184 XCloseDisplay(dpy);
185 dpy = NULL;
187 if (!prog) {
188 execvp(Arguments[0], Arguments);
189 wfatal(_("failed to restart Window Maker."));
190 } else {
191 execvp(prog, argv);
192 wsyserror(_("could not exec %s"), prog);
194 if (abortOnFailure)
195 exit(-1);
200 void
201 SetupEnvironment(WScreen *scr)
203 char *tmp, *ptr;
204 char buf[16];
206 if (wScreenCount > 1) {
207 tmp = wmalloc(strlen(DisplayName)+64);
208 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
209 ptr = strchr(strchr(tmp, ':'), '.');
210 if (ptr)
211 *ptr = 0;
212 sprintf(buf, ".%i", scr->screen);
213 strcat(tmp, buf);
214 putenv(tmp);
216 tmp = wmalloc(60);
217 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
218 scr->rcontext->attribs->colors_per_channel);
219 putenv(tmp);
224 *---------------------------------------------------------------------
225 * wAbort--
226 * Do a major cleanup and exit the program
228 *----------------------------------------------------------------------
230 void
231 wAbort(Bool dumpCore)
233 int i;
234 WScreen *scr;
236 for (i=0; i<wScreenCount; i++) {
237 scr = wScreenWithNumber(i);
238 if (scr)
239 RestoreDesktop(scr);
241 printf(_("%s aborted.\n"), ProgName);
242 if (dumpCore)
243 abort();
244 else
245 exit(1);
249 void
250 print_help()
252 printf(_("Usage: %s [options]\n"), ProgName);
253 puts(_("The Window Maker window manager for the X window system"));
254 puts("");
255 puts(_(" -display host:dpy display to use"));
256 #ifdef USECPP
257 puts(_(" --no-cpp disable preprocessing of configuration files"));
258 #endif
259 puts(_(" --no-dock do not open the application Dock"));
260 puts(_(" --no-clip do not open the workspace Clip"));
262 puts(_(" --locale locale locale to use"));
264 puts(_(" --visual-id visualid visual id of visual to use"));
265 puts(_(" --static do not update or save configurations"));
266 #ifdef DEBUG
267 puts(_(" --synchronous turn on synchronous display mode"));
268 #endif
269 puts(_(" --version print version and exit"));
270 puts(_(" --help show this message"));
275 void
276 check_defaults()
278 char *path;
280 path = wdefaultspathfordomain("");
281 if (access(path, R_OK)!=0) {
282 wfatal(_("could not find user GNUstep directory (%s).\n"
283 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
284 path);
285 exit(1);
288 free(path);
292 static void
293 execInitScript()
295 char *file;
296 char *paths = wstrappend(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
298 file = wfindfile(paths, DEF_INIT_SCRIPT);
299 free(paths);
301 if (file) {
302 if (system(file) != 0) {
303 wsyserror(_("%s:could not execute initialization script"), file);
305 #if 0
306 if (fork()==0) {
307 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
308 wsyserror(_("%s:could not execute initialization script"), file);
309 exit(1);
311 #endif
312 free(file);
317 void
318 ExecExitScript()
320 char *file;
321 char *paths = wstrappend(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
323 file = wfindfile(paths, DEF_EXIT_SCRIPT);
324 free(paths);
326 if (file) {
327 if (system(file) != 0) {
328 wsyserror(_("%s:could not execute exit script"), file);
330 #if 0
331 if (fork()==0) {
332 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
333 wsyserror(_("%s:could not execute exit script"), file);
334 exit(1);
336 #endif
337 free(file);
344 main(int argc, char **argv)
346 int i, restart=0;
347 Bool multiHead = True;
348 char *str;
349 int d, s;
350 #ifdef DEBUG
351 Bool doSync = False;
352 #endif
354 wsetabort(wAbort);
356 ArgCount = argc;
357 Arguments = argv;
359 WMInitializeApplication("WindowMaker", &argc, argv);
362 ProgName = strrchr(argv[0],'/');
363 if (!ProgName)
364 ProgName = argv[0];
365 else
366 ProgName++;
369 restart = 0;
371 memset(&wPreferences, 0, sizeof(WPreferences));
373 if (argc>1) {
374 for (i=1; i<argc; i++) {
375 #ifdef USECPP
376 if (strcmp(argv[i], "-nocpp")==0
377 || strcmp(argv[i], "--no-cpp")==0) {
378 wPreferences.flags.nocpp=1;
379 } else
380 #endif
381 if (strcmp(argv[i], "-nodock")==0
382 || strcmp(argv[i], "--no-dock")==0) {
383 wPreferences.flags.nodock=1;
384 } else if (strcmp(argv[i], "-noclip")==0
385 || strcmp(argv[i], "--no-clip")==0) {
386 wPreferences.flags.noclip=1;
387 } else if (strcmp(argv[i], "-version")==0
388 || strcmp(argv[i], "--version")==0) {
389 printf("Window Maker %s\n", VERSION);
390 exit(0);
391 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
392 puts(SYSCONFDIR);
393 exit(0);
394 #ifdef DEBUG
395 } else if (strcmp(argv[i], "--synchronous")==0) {
396 doSync = 1;
397 #endif
398 } else if (strcmp(argv[i], "-locale")==0
399 || strcmp(argv[i], "--locale")==0) {
400 i++;
401 if (i>=argc) {
402 wwarning(_("too few arguments for %s"), argv[i-1]);
403 exit(0);
405 Locale = argv[i];
406 } else if (strcmp(argv[i], "-display")==0) {
407 i++;
408 if (i>=argc) {
409 wwarning(_("too few arguments for %s"), argv[i-1]);
410 exit(0);
412 DisplayName = argv[i];
413 } else if (strcmp(argv[i], "-visualid")==0
414 || strcmp(argv[i], "--visual-id")==0) {
415 i++;
416 if (i>=argc) {
417 wwarning(_("too few arguments for %s"), argv[i-1]);
418 exit(0);
420 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
421 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
422 exit(0);
424 } else if (strcmp(argv[i], "-static")==0
425 || strcmp(argv[i], "--static")==0) {
427 wPreferences.flags.noupdates = 1;
428 #ifdef XSMP_ENABLED
429 } else if (strcmp(argv[i], "-clientid")==0
430 || strcmp(argv[i], "-restore")==0) {
431 i++;
432 if (i>=argc) {
433 wwarning(_("too few arguments for %s"), argv[i-1]);
434 exit(0);
436 #endif
437 } else if (strcmp(argv[i], "--help")==0) {
438 print_help();
439 exit(0);
440 } else {
441 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
442 printf(_("Try '%s --help' for more information\n"), argv[0]);
443 exit(1);
448 if (!wPreferences.flags.noupdates) {
449 /* check existence of Defaults DB directory */
450 check_defaults();
453 #if 0
454 tmp = getenv("LANG");
455 if (tmp) {
456 if (setlocale(LC_ALL,"") == NULL) {
457 wwarning("cannot set locale %s", tmp);
458 wwarning("falling back to C locale");
459 setlocale(LC_ALL,"C");
460 Locale = NULL;
461 } else {
462 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
463 Locale = NULL;
464 else
465 Locale = tmp;
467 } else {
468 Locale = NULL;
470 #endif
471 if (!Locale) {
472 Locale = getenv("LANG");
474 setlocale(LC_ALL, Locale);
475 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
476 Locale = NULL;
477 #ifdef I18N
478 if (getenv("NLSPATH"))
479 bindtextdomain("WindowMaker", getenv("NLSPATH"));
480 else
481 bindtextdomain("WindowMaker", LOCALEDIR);
482 textdomain("WindowMaker");
484 if (!XSupportsLocale()) {
485 wwarning(_("X server does not support locale"));
487 if (XSetLocaleModifiers("") == NULL) {
488 wwarning(_("cannot set locale modifiers"));
490 #endif
492 if (Locale) {
493 char *ptr;
495 Locale = wstrdup(Locale);
496 ptr = strchr(Locale, '.');
497 if (ptr)
498 *ptr = 0;
502 /* open display */
503 dpy = XOpenDisplay(DisplayName);
504 if (dpy == NULL) {
505 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
506 exit(1);
509 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
510 wsyserror("error setting close-on-exec flag for X connection");
511 exit(1);
514 /* check if the user specified a complete display name (with screen).
515 * If so, only manage the specified screen */
516 if (DisplayName)
517 str = strchr(DisplayName, ':');
518 else
519 str = NULL;
521 if (str && sscanf(str, "%i.%i", &d, &s)==2)
522 multiHead = False;
524 DisplayName = XDisplayName(DisplayName);
525 str = wmalloc(strlen(DisplayName)+64);
526 sprintf(str, "DISPLAY=%s", DisplayName);
527 putenv(str);
529 #ifdef DEBUG
530 if (doSync)
531 XSynchronize(dpy, True);
532 #endif
534 wXModifierInitialize();
536 #ifdef XSMP_ENABLED
537 wSessionConnectManager(argv, argc);
538 #endif
540 StartUp(!multiHead);
542 execInitScript();
544 EventLoop();
545 return -1;