Prevent crash when switchpanel is not initialised.
[wmaker-crm.git] / src / main.c
blobb1958e81c121ec4b46d66325fbff4603a298abc8
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #ifdef HAVE_INOTIFY
24 #include <sys/inotify.h>
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <fcntl.h>
35 #include <X11/Xlib.h>
36 #include <X11/Xutil.h>
38 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
39 * and if X_LOCALE is defined, X's locale emulating functions will be used.
40 * See Xlocale.h for more information.
42 #include <X11/Xlocale.h>
44 #define MAINFILE
46 #include "WindowMaker.h"
47 #include "window.h"
48 #include "funcs.h"
49 #include "menu.h"
50 #include "keybind.h"
51 #include "xmodifier.h"
52 #include "session.h"
53 #include "shutdown.h"
54 #include "dialog.h"
55 #include "main.h"
57 #include <WINGs/WUtil.h>
59 #ifndef GLOBAL_DEFAULTS_SUBDIR
60 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
61 #endif
63 /****** Global Variables ******/
65 /* general info */
67 Display *dpy;
69 char *ProgName;
71 unsigned int ValidModMask = 0xff;
73 #ifdef HAVE_INOTIFY
74 int inotifyFD;
75 int inotifyWD;
76 #endif
77 /* locale to use. NULL==POSIX or C */
78 char *Locale = NULL;
80 int wScreenCount = 0;
82 WPreferences wPreferences;
84 WShortKey wKeyBindings[WKBD_LAST];
86 /* defaults domains */
87 WDDomain *WDWindowMaker = NULL;
88 WDDomain *WDWindowAttributes = NULL;
89 WDDomain *WDRootMenu = NULL;
91 /* XContexts */
92 XContext wWinContext;
93 XContext wAppWinContext;
94 XContext wStackContext;
95 XContext wVEdgeContext;
97 /* Atoms */
98 Atom _XA_WM_STATE;
99 Atom _XA_WM_CHANGE_STATE;
100 Atom _XA_WM_PROTOCOLS;
101 Atom _XA_WM_TAKE_FOCUS;
102 Atom _XA_WM_DELETE_WINDOW;
103 Atom _XA_WM_SAVE_YOURSELF;
104 Atom _XA_WM_CLIENT_LEADER;
105 Atom _XA_WM_COLORMAP_WINDOWS;
106 Atom _XA_WM_COLORMAP_NOTIFY;
108 Atom _XA_GNUSTEP_WM_ATTR;
109 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
110 Atom _XA_GNUSTEP_WM_RESIZEBAR;
111 Atom _XA_GNUSTEP_TITLEBAR_STATE;
113 Atom _XA_WINDOWMAKER_MENU;
114 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
115 Atom _XA_WINDOWMAKER_STATE;
117 Atom _XA_WINDOWMAKER_WM_FUNCTION;
118 Atom _XA_WINDOWMAKER_NOTICEBOARD;
119 Atom _XA_WINDOWMAKER_COMMAND;
121 Atom _XA_WINDOWMAKER_ICON_SIZE;
122 Atom _XA_WINDOWMAKER_ICON_TILE;
124 Atom _XA_WM_IGNORE_FOCUS_EVENTS;
126 /* cursors */
127 Cursor wCursor[WCUR_LAST];
129 /* last event timestamp for XSetInputFocus */
130 Time LastTimestamp = CurrentTime;
131 /* timestamp on the last time we did XSetInputFocus() */
132 Time LastFocusChange = CurrentTime;
134 #ifdef SHAPE
135 Bool wShapeSupported;
136 int wShapeEventBase;
137 #endif
139 #ifdef KEEP_XKB_LOCK_STATUS
140 Bool wXkbSupported;
141 int wXkbEventBase;
142 #endif
144 /* special flags */
145 char WProgramSigState = 0;
146 char WProgramState = WSTATE_NORMAL;
147 char WDelayedActionSet = 0;
149 /* notifications */
150 const char *WMNManaged = "WMNManaged";
151 const char *WMNUnmanaged = "WMNUnmanaged";
152 const char *WMNChangedWorkspace = "WMNChangedWorkspace";
153 const char *WMNChangedState = "WMNChangedState";
154 const char *WMNChangedFocus = "WMNChangedFocus";
155 const char *WMNChangedStacking = "WMNChangedStacking";
156 const char *WMNChangedName = "WMNChangedName";
158 const char *WMNWorkspaceCreated = "WMNWorkspaceCreated";
159 const char *WMNWorkspaceDestroyed = "WMNWorkspaceDestroyed";
160 const char *WMNWorkspaceChanged = "WMNWorkspaceChanged";
161 const char *WMNWorkspaceNameChanged = "WMNWorkspaceNameChanged";
163 const char *WMNResetStacking = "WMNResetStacking";
165 /******** End Global Variables *****/
167 static char *DisplayName = NULL;
169 static char **Arguments;
171 static int ArgCount;
173 static Bool multiHead = True;
175 static int *wVisualID = NULL;
176 static int wVisualID_len = 0;
178 static int real_main(int argc, char **argv);
180 int getWVisualID(int screen)
182 if (wVisualID == NULL)
183 return -1;
184 if (screen < 0 || screen >= wVisualID_len)
185 return -1;
187 return wVisualID[screen];
190 static void setWVisualID(int screen, int val)
192 int i;
194 if (screen < 0)
195 return;
197 if (wVisualID == NULL) {
198 /* no array at all, alloc space for screen + 1 entries
199 * and init with default value */
200 wVisualID_len = screen + 1;
201 wVisualID = (int *)malloc(wVisualID_len * sizeof(int));
202 for (i = 0; i < wVisualID_len; i++) {
203 wVisualID[i] = -1;
205 } else if (screen >= wVisualID_len) {
206 /* larger screen number than previously allocated
207 so enlarge array */
208 int oldlen = wVisualID_len;
210 wVisualID_len = screen + 1;
211 wVisualID = (int *)realloc(wVisualID, wVisualID_len * sizeof(int));
212 for (i = oldlen; i < wVisualID_len; i++) {
213 wVisualID[i] = -1;
217 wVisualID[screen] = val;
221 * this function splits a given string at the comma into tokens
222 * and set the wVisualID variable to each parsed number
224 static int initWVisualID(const char *user_str)
226 char *mystr = strdup(user_str);
227 int cur_in_pos = 0;
228 int cur_out_pos = 0;
229 int cur_screen = 0;
230 int error_found = 0;
232 for (;;) {
233 /* check for delimiter */
234 if (user_str[cur_in_pos] == '\0' || user_str[cur_in_pos] == ',') {
235 int v;
237 mystr[cur_out_pos] = '\0';
239 if (sscanf(mystr, "%i", &v) != 1) {
240 error_found = 1;
241 break;
244 setWVisualID(cur_screen, v);
246 cur_screen++;
247 cur_out_pos = 0;
250 /* break in case last char has been consumed */
251 if (user_str[cur_in_pos] == '\0')
252 break;
254 /* if the current char is no delimiter put it into mystr */
255 if (user_str[cur_in_pos] != ',') {
256 mystr[cur_out_pos++] = user_str[cur_in_pos];
258 cur_in_pos++;
261 free(mystr);
263 if (cur_screen == 0||error_found != 0)
264 return 1;
266 return 0;
269 void Exit(int status)
271 if (dpy)
272 XCloseDisplay(dpy);
274 exit(status);
277 void Restart(char *manager, Bool abortOnFailure)
279 char *prog = NULL;
280 char *argv[MAX_RESTART_ARGS];
281 int i;
283 if (manager && manager[0] != 0) {
284 prog = argv[0] = strtok(manager, " ");
285 for (i = 1; i < MAX_RESTART_ARGS; i++) {
286 argv[i] = strtok(NULL, " ");
287 if (argv[i] == NULL) {
288 break;
292 if (dpy) {
293 XCloseDisplay(dpy);
294 dpy = NULL;
296 if (!prog) {
297 execvp(Arguments[0], Arguments);
298 wfatal(_("failed to restart Window Maker."));
299 } else {
300 execvp(prog, argv);
301 werror(_("could not exec %s"), prog);
303 if (abortOnFailure)
304 exit(7);
307 void SetupEnvironment(WScreen * scr)
309 char *tmp, *ptr;
310 char buf[16];
312 if (multiHead) {
313 int len = strlen(DisplayName) + 64;
314 tmp = wmalloc(len);
315 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
316 ptr = strchr(strchr(tmp, ':'), '.');
317 if (ptr)
318 *ptr = 0;
319 snprintf(buf, sizeof(buf), ".%i", scr->screen);
320 strcat(tmp, buf);
321 putenv(tmp);
323 tmp = wmalloc(60);
324 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
325 scr->rcontext->attribs->colors_per_channel);
326 putenv(tmp);
329 typedef struct {
330 WScreen *scr;
331 char *command;
332 } _tuple;
334 static void shellCommandHandler(pid_t pid, unsigned char status, _tuple * data)
336 if (status == 127) {
337 char *buffer;
339 buffer = wstrconcat(_("Could not execute command: "), data->command);
341 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
342 wfree(buffer);
343 } else if (status != 127) {
345 printf("%s: %i\n", data->command, status);
349 wfree(data->command);
350 wfree(data);
353 void ExecuteShellCommand(WScreen * scr, char *command)
355 static char *shell = NULL;
356 pid_t pid;
359 * This have a problem: if the shell is tcsh (not sure about others)
360 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
361 * will block and the command will not be executed.
362 if (!shell) {
363 shell = getenv("SHELL");
364 if (!shell)
365 shell = "/bin/sh";
368 shell = "/bin/sh";
370 pid = fork();
372 if (pid == 0) {
374 SetupEnvironment(scr);
376 #ifdef HAVE_SETSID
377 setsid();
378 #endif
379 execl(shell, shell, "-c", command, NULL);
380 werror("could not execute %s -c %s", shell, command);
381 Exit(-1);
382 } else if (pid < 0) {
383 werror("cannot fork a new process");
384 } else {
385 _tuple *data = wmalloc(sizeof(_tuple));
387 data->scr = scr;
388 data->command = wstrdup(command);
390 wAddDeathHandler(pid, (WDeathHandler *) shellCommandHandler, data);
395 *---------------------------------------------------------------------
396 * RelaunchWindow--
397 * Launch a new instance of the active window
399 *----------------------------------------------------------------------
401 Bool RelaunchWindow(WWindow *wwin)
403 if (! wwin || ! wwin->client_win) {
404 werror("no window to relaunch");
405 return False;
408 char **argv;
409 int argc;
411 if (! XGetCommand(dpy, wwin->client_win, &argv, &argc) || argc == 0 || argv == NULL) {
412 werror("cannot relaunch the application because no WM_COMMAND property is set");
413 return False;
416 pid_t pid = fork();
418 if (pid == 0) {
419 SetupEnvironment(wwin->screen_ptr);
420 #ifdef HAVE_SETSID
421 setsid();
422 #endif
423 /* argv is not null-terminated */
424 char **a = (char **) malloc(argc + 1);
425 if (! a) {
426 werror("out of memory trying to relaunch the application");
427 Exit(-1);
430 int i;
431 for (i = 0; i < argc; i++)
432 a[i] = argv[i];
433 a[i] = NULL;
435 execvp(a[0], a);
436 Exit(-1);
437 } else if (pid < 0) {
438 werror("cannot fork a new process");
440 XFreeStringList(argv);
441 return False;
442 } else {
443 _tuple *data = wmalloc(sizeof(_tuple));
445 data->scr = wwin->screen_ptr;
446 data->command = wtokenjoin(argv, argc);
448 /* not actually a shell command */
449 wAddDeathHandler(pid, (WDeathHandler *) shellCommandHandler, data);
451 XFreeStringList(argv);
454 return True;
458 *---------------------------------------------------------------------
459 * wAbort--
460 * Do a major cleanup and exit the program
462 *----------------------------------------------------------------------
464 void wAbort(Bool dumpCore)
466 int i;
467 WScreen *scr;
469 for (i = 0; i < wScreenCount; i++) {
470 scr = wScreenWithNumber(i);
471 if (scr)
472 RestoreDesktop(scr);
474 printf(_("%s aborted.\n"), ProgName);
475 if (dumpCore)
476 abort();
477 else
478 exit(1);
481 static void print_help(void)
483 printf(_("Usage: %s [options]\n"), ProgName);
484 puts(_("The Window Maker window manager for the X window system"));
485 puts("");
486 puts(_(" -display host:dpy display to use"));
487 puts(_(" --no-dock do not open the application Dock"));
488 puts(_(" --no-clip do not open the workspace Clip"));
489 puts(_(" --no-autolaunch do not autolaunch applications"));
490 puts(_(" --dont-restore do not restore saved session"));
492 puts(_(" --locale locale locale to use"));
494 puts(_(" --visual-id visualid visual id of visual to use"));
495 puts(_(" --static do not update or save configurations"));
496 #ifndef HAVE_INOTIFY
497 puts(_(" --no-polling do not periodically check for configuration updates"));
498 #endif
499 puts(_(" --version print version and exit"));
500 puts(_(" --help show this message"));
503 static void check_defaults(void)
505 char *path;
507 path = wdefaultspathfordomain("WindowMaker");
509 if (access(path, R_OK) != 0) {
510 wwarning(_("could not find user GNUstep directory (%s)."), path);
512 if (system("wmaker.inst --batch") != 0) {
513 wwarning(_("There was an error while creating GNUstep directory, please "
514 "make sure you have installed Window Maker correctly and run wmaker.inst"));
515 } else {
516 wwarning(_("%s directory created with default configuration."), path);
520 wfree(path);
523 #ifdef HAVE_INOTIFY
525 * Add watch here, used to notify if configuration
526 * files have changed, using linux kernel inotify mechanism
529 static void inotifyWatchConfig(void)
531 char *watchPath = NULL;
532 inotifyFD = inotify_init(); /* Initialise an inotify instance */
533 if (inotifyFD < 0) {
534 wwarning(_("could not initialise an inotify instance."
535 " Changes to the defaults database will require"
536 " a restart to take effect. Check your kernel!"));
537 } else {
538 watchPath = wstrconcat(wusergnusteppath(), "/Defaults");
539 /* Add the watch; really we are only looking for modify events
540 * but we might want more in the future so check all events for now.
541 * The individual events are checked for in event.c.
543 inotifyWD = inotify_add_watch(inotifyFD, watchPath, IN_ALL_EVENTS);
544 if (inotifyWD < 0) {
545 wwarning(_("could not add an inotify watch on path %s."
546 "Changes to the defaults database will require"
547 " a restart to take effect."), watchPath);
548 close(inotifyFD);
551 wfree(watchPath);
553 #endif /* HAVE_INOTIFY */
555 static void execInitScript(void)
557 char *file, *paths;
559 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
560 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
562 file = wfindfile(paths, DEF_INIT_SCRIPT);
563 wfree(paths);
565 if (file) {
566 if (system(file) != 0)
567 werror(_("%s:could not execute initialization script"), file);
569 wfree(file);
573 void ExecExitScript(void)
575 char *file, *paths;
577 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
578 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
580 file = wfindfile(paths, DEF_EXIT_SCRIPT);
581 wfree(paths);
583 if (file) {
584 if (system(file) != 0)
585 werror(_("%s:could not execute exit script"), file);
587 wfree(file);
591 int main(int argc, char **argv)
593 int i_am_the_monitor, i, len;
594 char *str, *alt;
596 /* setup common stuff for the monitor and wmaker itself */
597 WMInitializeApplication("WindowMaker", &argc, argv);
599 memset(&wPreferences, 0, sizeof(WPreferences));
601 wPreferences.fallbackWMs = WMCreateArray(8);
602 alt = getenv("WINDOWMAKER_ALT_WM");
603 if (alt != NULL)
604 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
606 WMAddToArray(wPreferences.fallbackWMs, wstrdup("blackbox"));
607 WMAddToArray(wPreferences.fallbackWMs, wstrdup("metacity"));
608 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
609 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
610 WMAddToArray(wPreferences.fallbackWMs, NULL);
611 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
612 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
614 i_am_the_monitor = 1;
616 for (i = 1; i < argc; i++) {
617 if (strncmp(argv[i], "--for-real", strlen("--for-real")) == 0) {
618 i_am_the_monitor = 0;
619 break;
620 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
621 i++;
622 if (i >= argc) {
623 wwarning(_("too few arguments for %s"), argv[i - 1]);
624 exit(0);
626 DisplayName = argv[i];
630 DisplayName = XDisplayName(DisplayName);
631 len = strlen(DisplayName) + 64;
632 str = wmalloc(len);
633 snprintf(str, len, "DISPLAY=%s", DisplayName);
634 putenv(str);
636 if (i_am_the_monitor)
637 return MonitorLoop(argc, argv);
638 else
639 return real_main(argc, argv);
642 static int real_main(int argc, char **argv)
644 int i;
645 char *pos;
646 int d, s;
648 setlocale(LC_ALL, "");
649 wsetabort(wAbort);
651 /* for telling WPrefs what's the name of the wmaker binary being ran */
652 setenv("WMAKER_BIN_NAME", argv[0], 1);
654 ArgCount = argc;
655 Arguments = wmalloc(sizeof(char *) * (ArgCount + 1));
656 for (i = 0; i < argc; i++) {
657 Arguments[i] = argv[i];
659 /* add the extra option to signal that we're just restarting wmaker */
660 Arguments[argc - 1] = "--for-real=";
661 Arguments[argc] = NULL;
663 ProgName = strrchr(argv[0], '/');
664 if (!ProgName)
665 ProgName = argv[0];
666 else
667 ProgName++;
669 if (argc > 1) {
670 for (i = 1; i < argc; i++) {
671 if (strcmp(argv[i], "-nocpp") == 0 || strcmp(argv[i], "--no-cpp") == 0) {
672 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv[i]);
673 } else if (strcmp(argv[i], "--for-real") == 0) {
674 wPreferences.flags.restarting = 0;
675 } else if (strcmp(argv[i], "--for-real=") == 0) {
676 wPreferences.flags.restarting = 1;
677 } else if (strcmp(argv[i], "--for-real-") == 0) {
678 wPreferences.flags.restarting = 2;
679 } else if (strcmp(argv[i], "-no-autolaunch") == 0
680 || strcmp(argv[i], "--no-autolaunch") == 0) {
681 wPreferences.flags.noautolaunch = 1;
682 } else if (strcmp(argv[i], "-dont-restore") == 0 || strcmp(argv[i], "--dont-restore") == 0) {
683 wPreferences.flags.norestore = 1;
684 } else if (strcmp(argv[i], "-nodock") == 0 || strcmp(argv[i], "--no-dock") == 0) {
685 wPreferences.flags.nodock = 1;
686 wPreferences.flags.nodrawer = 1;
687 } else if (strcmp(argv[i], "-noclip") == 0 || strcmp(argv[i], "--no-clip") == 0) {
688 wPreferences.flags.noclip = 1;
689 } else if (strcmp(argv[i], "-nodrawer") == 0 || strcmp(argv[i], "--no-drawer") == 0) {
690 wPreferences.flags.nodrawer = 1;
691 } else if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
692 printf("Window Maker %s\n", VERSION);
693 exit(0);
694 } else if (strcmp(argv[i], "--global_defaults_path") == 0) {
695 printf("%s/%s\n", SYSCONFDIR, GLOBAL_DEFAULTS_SUBDIR);
696 exit(0);
697 } else if (strcmp(argv[i], "-locale") == 0 || strcmp(argv[i], "--locale") == 0) {
698 i++;
699 if (i >= argc) {
700 wwarning(_("too few arguments for %s"), argv[i - 1]);
701 exit(0);
703 Locale = argv[i];
704 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
705 i++;
706 if (i >= argc) {
707 wwarning(_("too few arguments for %s"), argv[i - 1]);
708 exit(0);
710 DisplayName = argv[i];
711 } else if (strcmp(argv[i], "-visualid") == 0 || strcmp(argv[i], "--visual-id") == 0) {
712 i++;
713 if (i >= argc) {
714 wwarning(_("too few arguments for %s"), argv[i - 1]);
715 exit(0);
717 if (initWVisualID(argv[i]) != 0) {
718 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
719 exit(0);
721 } else if (strcmp(argv[i], "-static") == 0 || strcmp(argv[i], "--static") == 0
722 #ifndef HAVE_INOTIFY
723 || strcmp(argv[i], "--no-polling") == 0
724 #endif
726 wPreferences.flags.noupdates = 1;
727 } else if (strcmp(argv[i], "--help") == 0) {
728 print_help();
729 exit(0);
730 } else {
731 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
732 printf(_("Try '%s --help' for more information\n"), argv[0]);
733 exit(1);
738 if (!wPreferences.flags.noupdates) {
739 /* check existence of Defaults DB directory */
740 check_defaults();
743 if (Locale) {
744 setenv("LANG", Locale, 1);
745 } else {
746 Locale = getenv("LC_ALL");
747 if (!Locale) {
748 Locale = getenv("LANG");
752 setlocale(LC_ALL, "");
754 if (!Locale || strcmp(Locale, "C") == 0 || strcmp(Locale, "POSIX") == 0)
755 Locale = NULL;
756 #ifdef I18N
757 if (getenv("NLSPATH")) {
758 bindtextdomain("WindowMaker", getenv("NLSPATH"));
759 #if defined(MENU_TEXTDOMAIN)
760 bindtextdomain(MENU_TEXTDOMAIN, getenv("NLSPATH"));
761 #endif
762 } else {
763 bindtextdomain("WindowMaker", LOCALEDIR);
764 #if defined(MENU_TEXTDOMAIN)
765 bindtextdomain(MENU_TEXTDOMAIN, LOCALEDIR);
766 #endif
768 bind_textdomain_codeset("WindowMaker", "UTF-8");
769 #if defined(MENU_TEXTDOMAIN)
770 bind_textdomain_codeset(MENU_TEXTDOMAIN, "UTF-8");
771 #endif
772 textdomain("WindowMaker");
774 if (!XSupportsLocale()) {
775 wwarning(_("X server does not support locale"));
778 if (XSetLocaleModifiers("") == NULL) {
779 wwarning(_("cannot set locale modifiers"));
781 #endif
783 if (Locale) {
784 char *ptr;
786 Locale = wstrdup(Locale);
787 ptr = strchr(Locale, '.');
788 if (ptr)
789 *ptr = 0;
792 /* open display */
793 dpy = XOpenDisplay(DisplayName);
794 if (dpy == NULL) {
795 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
796 exit(1);
799 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
800 werror("error setting close-on-exec flag for X connection");
801 exit(1);
805 if (getWVisualID(0) < 0) {
807 * If unspecified, use default visual instead of waiting
808 * for wrlib/context.c:bestContext() that may end up choosing
809 * the "fake" 24 bits added by the Composite extension.
810 * This is required to avoid all sort of corruptions when
811 * composite is enabled, and at a depth other than 24.
813 setWVisualID(0, (int)DefaultVisual(dpy, DefaultScreen(dpy))->visualid);
816 /* check if the user specified a complete display name (with screen).
817 * If so, only manage the specified screen */
818 if (DisplayName)
819 pos = strchr(DisplayName, ':');
820 else
821 pos = NULL;
823 if (pos && sscanf(pos, ":%i.%i", &d, &s) == 2)
824 multiHead = False;
826 DisplayName = XDisplayName(DisplayName);
827 setenv("DISPLAY", DisplayName, 1);
829 wXModifierInitialize();
830 StartUp(!multiHead);
832 if (wScreenCount == 1)
833 multiHead = False;
835 execInitScript();
836 #ifdef HAVE_INOTIFY
837 inotifyWatchConfig();
838 #endif
839 EventLoop();
840 return -1;