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.
24 #include <sys/inotify.h>
32 #include <sys/types.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>
46 #include "WindowMaker.h"
53 #include "xmodifier.h"
60 #include <WINGs/WUtil.h>
62 #ifndef GLOBAL_DEFAULTS_SUBDIR
63 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
66 /****** Global Variables ******/
67 struct wmaker_global_variables w_global
;
75 unsigned int ValidModMask
= 0xff;
84 struct WPreferences wPreferences
;
86 WShortKey wKeyBindings
[WKBD_LAST
];
88 /* defaults domains */
89 WDDomain
*WDWindowMaker
= NULL
;
90 WDDomain
*WDWindowAttributes
= NULL
;
91 WDDomain
*WDRootMenu
= NULL
;
98 #ifdef KEEP_XKB_LOCK_STATUS
104 char WDelayedActionSet
= 0;
107 const char WMNManaged
[] = "WMNManaged";
108 const char WMNUnmanaged
[] = "WMNUnmanaged";
109 const char WMNChangedWorkspace
[] = "WMNChangedWorkspace";
110 const char WMNChangedState
[] = "WMNChangedState";
111 const char WMNChangedFocus
[] = "WMNChangedFocus";
112 const char WMNChangedStacking
[] = "WMNChangedStacking";
113 const char WMNChangedName
[] = "WMNChangedName";
115 const char WMNWorkspaceCreated
[] = "WMNWorkspaceCreated";
116 const char WMNWorkspaceDestroyed
[] = "WMNWorkspaceDestroyed";
117 const char WMNWorkspaceChanged
[] = "WMNWorkspaceChanged";
118 const char WMNWorkspaceNameChanged
[] = "WMNWorkspaceNameChanged";
120 const char WMNResetStacking
[] = "WMNResetStacking";
122 /******** End Global Variables *****/
124 static char *DisplayName
= NULL
;
126 static char **Arguments
;
130 static Bool multiHead
= True
;
132 static int *wVisualID
= NULL
;
133 static int wVisualID_len
= 0;
135 static int real_main(int argc
, char **argv
);
137 int getWVisualID(int screen
)
139 if (wVisualID
== NULL
)
141 if (screen
< 0 || screen
>= wVisualID_len
)
144 return wVisualID
[screen
];
147 static void setWVisualID(int screen
, int val
)
154 if (wVisualID
== NULL
) {
155 /* no array at all, alloc space for screen + 1 entries
156 * and init with default value */
157 wVisualID_len
= screen
+ 1;
158 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
159 for (i
= 0; i
< wVisualID_len
; i
++) {
162 } else if (screen
>= wVisualID_len
) {
163 /* larger screen number than previously allocated
165 int oldlen
= wVisualID_len
;
167 wVisualID_len
= screen
+ 1;
168 wVisualID
= (int *)realloc(wVisualID
, wVisualID_len
* sizeof(int));
169 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
174 wVisualID
[screen
] = val
;
178 * this function splits a given string at the comma into tokens
179 * and set the wVisualID variable to each parsed number
181 static int initWVisualID(const char *user_str
)
183 char *mystr
= strdup(user_str
);
190 /* check for delimiter */
191 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
194 mystr
[cur_out_pos
] = '\0';
196 if (sscanf(mystr
, "%i", &v
) != 1) {
201 setWVisualID(cur_screen
, v
);
207 /* break in case last char has been consumed */
208 if (user_str
[cur_in_pos
] == '\0')
211 /* if the current char is no delimiter put it into mystr */
212 if (user_str
[cur_in_pos
] != ',') {
213 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
220 if (cur_screen
== 0||error_found
!= 0)
226 noreturn
void Exit(int status
)
234 void Restart(char *manager
, Bool abortOnFailure
)
237 char *argv
[MAX_RESTART_ARGS
];
240 if (manager
&& manager
[0] != 0) {
241 prog
= argv
[0] = strtok(manager
, " ");
242 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
243 argv
[i
] = strtok(NULL
, " ");
244 if (argv
[i
] == NULL
) {
254 execvp(Arguments
[0], Arguments
);
255 wfatal(_("failed to restart Window Maker."));
258 werror(_("could not exec %s"), prog
);
264 void SetupEnvironment(WScreen
* scr
)
270 int len
= strlen(DisplayName
) + 64;
272 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
273 ptr
= strchr(strchr(tmp
, ':'), '.');
276 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
281 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
282 scr
->rcontext
->attribs
->colors_per_channel
);
291 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
296 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
298 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
300 } else if (status
!= 127) {
302 printf("%s: %i\n", data->command, status);
306 wfree(data
->command
);
310 void ExecuteShellCommand(WScreen
*scr
, const char *command
)
312 static char *shell
= NULL
;
316 * This have a problem: if the shell is tcsh (not sure about others)
317 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
318 * will block and the command will not be executed.
320 shell = getenv("SHELL");
331 SetupEnvironment(scr
);
336 execl(shell
, shell
, "-c", command
, NULL
);
337 werror("could not execute %s -c %s", shell
, command
);
339 } else if (pid
< 0) {
340 werror("cannot fork a new process");
342 _tuple
*data
= wmalloc(sizeof(_tuple
));
345 data
->command
= wstrdup(command
);
347 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
352 *---------------------------------------------------------------------
354 * Launch a new instance of the active window
356 *----------------------------------------------------------------------
358 Bool
RelaunchWindow(WWindow
*wwin
)
360 if (! wwin
|| ! wwin
->client_win
) {
361 werror("no window to relaunch");
368 if (! XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
== 0 || argv
== NULL
) {
369 werror("cannot relaunch the application because no WM_COMMAND property is set");
376 SetupEnvironment(wwin
->screen_ptr
);
380 /* argv is not null-terminated */
381 char **a
= (char **) malloc(argc
+ 1);
383 werror("out of memory trying to relaunch the application");
388 for (i
= 0; i
< argc
; i
++)
394 } else if (pid
< 0) {
395 werror("cannot fork a new process");
397 XFreeStringList(argv
);
400 _tuple
*data
= wmalloc(sizeof(_tuple
));
402 data
->scr
= wwin
->screen_ptr
;
403 data
->command
= wtokenjoin(argv
, argc
);
405 /* not actually a shell command */
406 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
408 XFreeStringList(argv
);
415 *---------------------------------------------------------------------
417 * Do a major cleanup and exit the program
419 *----------------------------------------------------------------------
421 noreturn
void wAbort(Bool dumpCore
)
426 for (i
= 0; i
< wScreenCount
; i
++) {
427 scr
= wScreenWithNumber(i
);
431 printf(_("%s aborted.\n"), ProgName
);
438 static void print_help(void)
440 printf(_("Usage: %s [options]\n"), ProgName
);
441 puts(_("The Window Maker window manager for the X window system"));
443 puts(_(" -display host:dpy display to use"));
444 puts(_(" --no-dock do not open the application Dock"));
445 puts(_(" --no-clip do not open the workspace Clip"));
446 puts(_(" --no-autolaunch do not autolaunch applications"));
447 puts(_(" --dont-restore do not restore saved session"));
449 puts(_(" --locale locale locale to use"));
451 puts(_(" --visual-id visualid visual id of visual to use"));
452 puts(_(" --static do not update or save configurations"));
454 puts(_(" --no-polling do not periodically check for configuration updates"));
456 puts(_(" --version print version and exit"));
457 puts(_(" --help show this message"));
460 static void check_defaults(void)
464 path
= wdefaultspathfordomain("WindowMaker");
466 if (access(path
, R_OK
) != 0) {
467 wwarning(_("could not find user GNUstep directory (%s)."), path
);
469 if (system("wmaker.inst --batch") != 0) {
470 wwarning(_("There was an error while creating GNUstep directory, please "
471 "make sure you have installed Window Maker correctly and run wmaker.inst"));
473 wwarning(_("%s directory created with default configuration."), path
);
482 * Add watch here, used to notify if configuration
483 * files have changed, using linux kernel inotify mechanism
486 static void inotifyWatchConfig(void)
488 char *watchPath
= NULL
;
489 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
491 wwarning(_("could not initialise an inotify instance."
492 " Changes to the defaults database will require"
493 " a restart to take effect. Check your kernel!"));
495 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
496 /* Add the watch; really we are only looking for modify events
497 * but we might want more in the future so check all events for now.
498 * The individual events are checked for in event.c.
500 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
502 wwarning(_("could not add an inotify watch on path %s."
503 "Changes to the defaults database will require"
504 " a restart to take effect."), watchPath
);
510 #endif /* HAVE_INOTIFY */
512 static void execInitScript(void)
516 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
517 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
519 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
523 if (system(file
) != 0)
524 werror(_("%s:could not execute initialization script"), file
);
530 void ExecExitScript(void)
534 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
535 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
537 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
541 if (system(file
) != 0)
542 werror(_("%s:could not execute exit script"), file
);
548 int main(int argc
, char **argv
)
550 int i_am_the_monitor
, i
, len
;
553 memset(&w_global
, 0, sizeof(w_global
));
554 w_global
.program
.state
= WSTATE_NORMAL
;
555 w_global
.program
.signal_state
= WSTATE_NORMAL
;
556 w_global
.timestamp
.last_event
= CurrentTime
;
557 w_global
.timestamp
.focus_change
= CurrentTime
;
559 /* setup common stuff for the monitor and wmaker itself */
560 WMInitializeApplication("WindowMaker", &argc
, argv
);
562 memset(&wPreferences
, 0, sizeof(wPreferences
));
564 wPreferences
.fallbackWMs
= WMCreateArray(8);
565 alt
= getenv("WINDOWMAKER_ALT_WM");
567 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
569 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
570 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
571 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
572 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
573 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
574 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
575 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
577 i_am_the_monitor
= 1;
579 for (i
= 1; i
< argc
; i
++) {
580 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
581 i_am_the_monitor
= 0;
583 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
586 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
589 DisplayName
= argv
[i
];
593 DisplayName
= XDisplayName(DisplayName
);
594 len
= strlen(DisplayName
) + 64;
596 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
599 if (i_am_the_monitor
)
600 return MonitorLoop(argc
, argv
);
602 return real_main(argc
, argv
);
605 static int real_main(int argc
, char **argv
)
611 setlocale(LC_ALL
, "");
614 /* for telling WPrefs what's the name of the wmaker binary being ran */
615 setenv("WMAKER_BIN_NAME", argv
[0], 1);
618 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
619 for (i
= 0; i
< argc
; i
++) {
620 Arguments
[i
] = argv
[i
];
622 /* add the extra option to signal that we're just restarting wmaker */
623 Arguments
[argc
- 1] = "--for-real=";
624 Arguments
[argc
] = NULL
;
626 ProgName
= strrchr(argv
[0], '/');
633 for (i
= 1; i
< argc
; i
++) {
634 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
635 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv
[i
]);
636 } else if (strcmp(argv
[i
], "--for-real") == 0) {
637 wPreferences
.flags
.restarting
= 0;
638 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
639 wPreferences
.flags
.restarting
= 1;
640 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
641 wPreferences
.flags
.restarting
= 2;
642 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
643 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
644 wPreferences
.flags
.noautolaunch
= 1;
645 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
646 wPreferences
.flags
.norestore
= 1;
647 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
648 wPreferences
.flags
.nodock
= 1;
649 wPreferences
.flags
.nodrawer
= 1;
650 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
651 wPreferences
.flags
.noclip
= 1;
652 } else if (strcmp(argv
[i
], "-nodrawer") == 0 || strcmp(argv
[i
], "--no-drawer") == 0) {
653 wPreferences
.flags
.nodrawer
= 1;
654 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
655 printf("Window Maker %s\n", VERSION
);
657 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
658 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
660 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
663 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
666 w_global
.locale
= argv
[i
];
667 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
670 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
673 DisplayName
= argv
[i
];
674 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
677 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
680 if (initWVisualID(argv
[i
]) != 0) {
681 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
684 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
686 || strcmp(argv
[i
], "--no-polling") == 0
689 wPreferences
.flags
.noupdates
= 1;
690 } else if (strcmp(argv
[i
], "--help") == 0) {
694 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
695 printf(_("Try '%s --help' for more information\n"), argv
[0]);
701 if (!wPreferences
.flags
.noupdates
) {
702 /* check existence of Defaults DB directory */
706 if (w_global
.locale
) {
707 setenv("LANG", w_global
.locale
, 1);
709 w_global
.locale
= getenv("LC_ALL");
710 if (!w_global
.locale
) {
711 w_global
.locale
= getenv("LANG");
715 setlocale(LC_ALL
, "");
717 if (!w_global
.locale
|| strcmp(w_global
.locale
, "C") == 0 || strcmp(w_global
.locale
, "POSIX") == 0)
718 w_global
.locale
= NULL
;
720 if (getenv("NLSPATH")) {
721 bindtextdomain("WindowMaker", getenv("NLSPATH"));
722 #if defined(MENU_TEXTDOMAIN)
723 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
726 bindtextdomain("WindowMaker", LOCALEDIR
);
727 #if defined(MENU_TEXTDOMAIN)
728 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
731 bind_textdomain_codeset("WindowMaker", "UTF-8");
732 #if defined(MENU_TEXTDOMAIN)
733 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
735 textdomain("WindowMaker");
737 if (!XSupportsLocale()) {
738 wwarning(_("X server does not support locale"));
741 if (XSetLocaleModifiers("") == NULL
) {
742 wwarning(_("cannot set locale modifiers"));
746 if (w_global
.locale
) {
749 w_global
.locale
= wstrdup(w_global
.locale
);
750 ptr
= strchr(w_global
.locale
, '.');
756 dpy
= XOpenDisplay(DisplayName
);
758 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
762 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
763 werror("error setting close-on-exec flag for X connection");
768 if (getWVisualID(0) < 0) {
770 * If unspecified, use default visual instead of waiting
771 * for wrlib/context.c:bestContext() that may end up choosing
772 * the "fake" 24 bits added by the Composite extension.
773 * This is required to avoid all sort of corruptions when
774 * composite is enabled, and at a depth other than 24.
776 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
779 /* check if the user specified a complete display name (with screen).
780 * If so, only manage the specified screen */
782 pos
= strchr(DisplayName
, ':');
786 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
789 DisplayName
= XDisplayName(DisplayName
);
790 setenv("DISPLAY", DisplayName
, 1);
792 wXModifierInitialize();
795 if (wScreenCount
== 1)
800 inotifyWatchConfig();