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 struct WPreferences wPreferences
;
77 WShortKey wKeyBindings
[WKBD_LAST
];
80 const char WMNManaged
[] = "WMNManaged";
81 const char WMNUnmanaged
[] = "WMNUnmanaged";
82 const char WMNChangedWorkspace
[] = "WMNChangedWorkspace";
83 const char WMNChangedState
[] = "WMNChangedState";
84 const char WMNChangedFocus
[] = "WMNChangedFocus";
85 const char WMNChangedStacking
[] = "WMNChangedStacking";
86 const char WMNChangedName
[] = "WMNChangedName";
88 const char WMNWorkspaceCreated
[] = "WMNWorkspaceCreated";
89 const char WMNWorkspaceDestroyed
[] = "WMNWorkspaceDestroyed";
90 const char WMNWorkspaceChanged
[] = "WMNWorkspaceChanged";
91 const char WMNWorkspaceNameChanged
[] = "WMNWorkspaceNameChanged";
93 const char WMNResetStacking
[] = "WMNResetStacking";
95 /******** End Global Variables *****/
97 static char *DisplayName
= NULL
;
99 static char **Arguments
;
103 static Bool multiHead
= True
;
105 static int *wVisualID
= NULL
;
106 static int wVisualID_len
= 0;
108 static int real_main(int argc
, char **argv
);
110 int getWVisualID(int screen
)
112 if (wVisualID
== NULL
)
114 if (screen
< 0 || screen
>= wVisualID_len
)
117 return wVisualID
[screen
];
120 static void setWVisualID(int screen
, int val
)
127 if (wVisualID
== NULL
) {
128 /* no array at all, alloc space for screen + 1 entries
129 * and init with default value */
130 wVisualID_len
= screen
+ 1;
131 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
132 for (i
= 0; i
< wVisualID_len
; i
++) {
135 } else if (screen
>= wVisualID_len
) {
136 /* larger screen number than previously allocated
138 int oldlen
= wVisualID_len
;
140 wVisualID_len
= screen
+ 1;
141 wVisualID
= (int *)realloc(wVisualID
, wVisualID_len
* sizeof(int));
142 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
147 wVisualID
[screen
] = val
;
151 * this function splits a given string at the comma into tokens
152 * and set the wVisualID variable to each parsed number
154 static int initWVisualID(const char *user_str
)
156 char *mystr
= strdup(user_str
);
163 /* check for delimiter */
164 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
167 mystr
[cur_out_pos
] = '\0';
169 if (sscanf(mystr
, "%i", &v
) != 1) {
174 setWVisualID(cur_screen
, v
);
180 /* break in case last char has been consumed */
181 if (user_str
[cur_in_pos
] == '\0')
184 /* if the current char is no delimiter put it into mystr */
185 if (user_str
[cur_in_pos
] != ',') {
186 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
193 if (cur_screen
== 0||error_found
!= 0)
199 noreturn
void Exit(int status
)
204 wutil_shutdown(); /* WUtil clean-up */
209 void Restart(char *manager
, Bool abortOnFailure
)
212 char *argv
[MAX_RESTART_ARGS
];
215 if (manager
&& manager
[0] != 0) {
216 prog
= argv
[0] = strtok(manager
, " ");
217 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
218 argv
[i
] = strtok(NULL
, " ");
219 if (argv
[i
] == NULL
) {
229 execvp(Arguments
[0], Arguments
);
230 wfatal(_("failed to restart Window Maker."));
233 werror(_("could not exec %s"), prog
);
239 void SetupEnvironment(WScreen
* scr
)
245 int len
= strlen(DisplayName
) + 64;
247 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
248 ptr
= strchr(strchr(tmp
, ':'), '.');
251 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
256 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
257 scr
->rcontext
->attribs
->colors_per_channel
);
266 static void shellCommandHandler(pid_t pid
, unsigned int status
, void *client_data
)
268 _tuple
*data
= (_tuple
*) client_data
;
270 /* Parameter not used, but tell the compiler that it is ok */
276 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
278 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
280 } else if (status
!= 127) {
282 printf("%s: %i\n", data->command, status);
286 wfree(data
->command
);
290 void ExecuteShellCommand(WScreen
*scr
, const char *command
)
292 static char *shell
= NULL
;
296 * This have a problem: if the shell is tcsh (not sure about others)
297 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
298 * will block and the command will not be executed.
300 shell = getenv("SHELL");
311 SetupEnvironment(scr
);
316 execl(shell
, shell
, "-c", command
, NULL
);
317 werror("could not execute %s -c %s", shell
, command
);
319 } else if (pid
< 0) {
320 werror("cannot fork a new process");
322 _tuple
*data
= wmalloc(sizeof(_tuple
));
325 data
->command
= wstrdup(command
);
327 wAddDeathHandler(pid
, shellCommandHandler
, data
);
332 *---------------------------------------------------------------------
334 * Launch a new instance of the active window
336 *----------------------------------------------------------------------
338 Bool
RelaunchWindow(WWindow
*wwin
)
340 if (! wwin
|| ! wwin
->client_win
) {
341 werror("no window to relaunch");
348 if (! XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
== 0 || argv
== NULL
) {
349 werror("cannot relaunch the application because no WM_COMMAND property is set");
356 SetupEnvironment(wwin
->screen_ptr
);
360 /* argv is not null-terminated */
361 char **a
= (char **) malloc(argc
+ 1);
363 werror("out of memory trying to relaunch the application");
368 for (i
= 0; i
< argc
; i
++)
374 } else if (pid
< 0) {
375 werror("cannot fork a new process");
377 XFreeStringList(argv
);
380 _tuple
*data
= wmalloc(sizeof(_tuple
));
382 data
->scr
= wwin
->screen_ptr
;
383 data
->command
= wtokenjoin(argv
, argc
);
385 /* not actually a shell command */
386 wAddDeathHandler(pid
, shellCommandHandler
, data
);
388 XFreeStringList(argv
);
395 *---------------------------------------------------------------------
397 * Do a major cleanup and exit the program
399 *----------------------------------------------------------------------
401 noreturn
void wAbort(Bool dumpCore
)
406 for (i
= 0; i
< w_global
.screen_count
; i
++) {
407 scr
= wScreenWithNumber(i
);
411 printf(_("%s aborted.\n"), ProgName
);
418 static void print_help(void)
420 printf(_("Usage: %s [options]\n"), ProgName
);
421 puts(_("The Window Maker window manager for the X window system"));
423 puts(_(" -display host:dpy display to use"));
424 puts(_(" --no-dock do not open the application Dock"));
425 puts(_(" --no-clip do not open the workspace Clip"));
426 puts(_(" --no-autolaunch do not autolaunch applications"));
427 puts(_(" --dont-restore do not restore saved session"));
429 puts(_(" --locale locale locale to use"));
431 puts(_(" --visual-id visualid visual id of visual to use"));
432 puts(_(" --static do not update or save configurations"));
434 puts(_(" --no-polling do not periodically check for configuration updates"));
436 puts(_(" --version print version and exit"));
437 puts(_(" --help show this message"));
440 static void check_defaults(void)
444 path
= wdefaultspathfordomain("WindowMaker");
446 if (access(path
, R_OK
) != 0) {
447 wwarning(_("could not find user GNUstep directory (%s)."), path
);
449 if (system("wmaker.inst --batch") != 0) {
450 wwarning(_("There was an error while creating GNUstep directory, please "
451 "make sure you have installed Window Maker correctly and run wmaker.inst"));
453 wwarning(_("%s directory created with default configuration."), path
);
462 * Add watch here, used to notify if configuration
463 * files have changed, using linux kernel inotify mechanism
466 static void inotifyWatchConfig(void)
468 char *watchPath
= NULL
;
470 w_global
.inotify
.fd_event_queue
= inotify_init(); /* Initialise an inotify instance */
471 if (w_global
.inotify
.fd_event_queue
< 0) {
472 wwarning(_("could not initialise an inotify instance."
473 " Changes to the defaults database will require"
474 " a restart to take effect. Check your kernel!"));
476 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
477 /* Add the watch; really we are only looking for modify events
478 * but we might want more in the future so check all events for now.
479 * The individual events are checked for in event.c.
481 w_global
.inotify
.wd_defaults
= inotify_add_watch(w_global
.inotify
.fd_event_queue
, watchPath
, IN_ALL_EVENTS
);
482 if (w_global
.inotify
.wd_defaults
< 0) {
483 wwarning(_("could not add an inotify watch on path %s."
484 "Changes to the defaults database will require"
485 " a restart to take effect."), watchPath
);
486 close(w_global
.inotify
.fd_event_queue
);
487 w_global
.inotify
.fd_event_queue
= -1;
492 #endif /* HAVE_INOTIFY */
494 static void execInitScript(void)
498 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
499 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
501 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
505 if (system(file
) != 0)
506 werror(_("%s:could not execute initialization script"), file
);
512 void ExecExitScript(void)
516 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
517 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
519 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
523 if (system(file
) != 0)
524 werror(_("%s:could not execute exit script"), file
);
530 int main(int argc
, char **argv
)
532 int i_am_the_monitor
, i
, len
;
535 memset(&w_global
, 0, sizeof(w_global
));
536 w_global
.program
.state
= WSTATE_NORMAL
;
537 w_global
.program
.signal_state
= WSTATE_NORMAL
;
538 w_global
.timestamp
.last_event
= CurrentTime
;
539 w_global
.timestamp
.focus_change
= CurrentTime
;
540 w_global
.workspace
.ignore_change
= False
;
541 w_global
.shortcut
.modifiers_mask
= 0xff;
543 /* setup common stuff for the monitor and wmaker itself */
544 WMInitializeApplication("WindowMaker", &argc
, argv
);
546 memset(&wPreferences
, 0, sizeof(wPreferences
));
548 wPreferences
.fallbackWMs
= WMCreateArray(8);
549 alt
= getenv("WINDOWMAKER_ALT_WM");
551 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
553 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
554 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
555 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
556 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
557 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
558 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
559 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
561 i_am_the_monitor
= 1;
563 for (i
= 1; i
< argc
; i
++) {
564 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
565 i_am_the_monitor
= 0;
567 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
570 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
573 DisplayName
= argv
[i
];
577 DisplayName
= XDisplayName(DisplayName
);
578 len
= strlen(DisplayName
) + 64;
580 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
583 if (i_am_the_monitor
)
584 return MonitorLoop(argc
, argv
);
586 return real_main(argc
, argv
);
589 static int real_main(int argc
, char **argv
)
595 setlocale(LC_ALL
, "");
598 /* for telling WPrefs what's the name of the wmaker binary being ran */
599 setenv("WMAKER_BIN_NAME", argv
[0], 1);
602 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
603 for (i
= 0; i
< argc
; i
++) {
604 Arguments
[i
] = argv
[i
];
606 /* add the extra option to signal that we're just restarting wmaker */
607 Arguments
[argc
- 1] = "--for-real=";
608 Arguments
[argc
] = NULL
;
610 ProgName
= strrchr(argv
[0], '/');
617 for (i
= 1; i
< argc
; i
++) {
618 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
619 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv
[i
]);
620 } else if (strcmp(argv
[i
], "--for-real") == 0) {
621 wPreferences
.flags
.restarting
= 0;
622 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
623 wPreferences
.flags
.restarting
= 1;
624 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
625 wPreferences
.flags
.restarting
= 2;
626 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
627 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
628 wPreferences
.flags
.noautolaunch
= 1;
629 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
630 wPreferences
.flags
.norestore
= 1;
631 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
632 wPreferences
.flags
.nodock
= 1;
633 wPreferences
.flags
.nodrawer
= 1;
634 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
635 wPreferences
.flags
.noclip
= 1;
636 } else if (strcmp(argv
[i
], "-nodrawer") == 0 || strcmp(argv
[i
], "--no-drawer") == 0) {
637 wPreferences
.flags
.nodrawer
= 1;
638 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
639 printf("Window Maker %s\n", VERSION
);
641 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
642 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
644 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
647 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
650 w_global
.locale
= argv
[i
];
651 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
654 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
657 DisplayName
= argv
[i
];
658 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
661 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
664 if (initWVisualID(argv
[i
]) != 0) {
665 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
668 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
670 || strcmp(argv
[i
], "--no-polling") == 0
673 wPreferences
.flags
.noupdates
= 1;
674 } else if (strcmp(argv
[i
], "--help") == 0) {
678 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
679 printf(_("Try '%s --help' for more information\n"), argv
[0]);
685 if (!wPreferences
.flags
.noupdates
) {
686 /* check existence of Defaults DB directory */
690 if (w_global
.locale
) {
691 setenv("LANG", w_global
.locale
, 1);
693 w_global
.locale
= getenv("LC_ALL");
694 if (!w_global
.locale
) {
695 w_global
.locale
= getenv("LANG");
699 setlocale(LC_ALL
, "");
701 if (!w_global
.locale
|| strcmp(w_global
.locale
, "C") == 0 || strcmp(w_global
.locale
, "POSIX") == 0)
702 w_global
.locale
= NULL
;
704 if (getenv("NLSPATH")) {
705 bindtextdomain("WindowMaker", getenv("NLSPATH"));
706 #if defined(MENU_TEXTDOMAIN)
707 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
710 bindtextdomain("WindowMaker", LOCALEDIR
);
711 #if defined(MENU_TEXTDOMAIN)
712 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
715 bind_textdomain_codeset("WindowMaker", "UTF-8");
716 #if defined(MENU_TEXTDOMAIN)
717 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
719 textdomain("WindowMaker");
721 if (!XSupportsLocale()) {
722 wwarning(_("X server does not support locale"));
725 if (XSetLocaleModifiers("") == NULL
) {
726 wwarning(_("cannot set locale modifiers"));
730 if (w_global
.locale
) {
733 w_global
.locale
= wstrdup(w_global
.locale
);
734 ptr
= strchr(w_global
.locale
, '.');
740 dpy
= XOpenDisplay(DisplayName
);
742 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
746 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
747 werror("error setting close-on-exec flag for X connection");
752 if (getWVisualID(0) < 0) {
754 * If unspecified, use default visual instead of waiting
755 * for wrlib/context.c:bestContext() that may end up choosing
756 * the "fake" 24 bits added by the Composite extension.
757 * This is required to avoid all sort of corruptions when
758 * composite is enabled, and at a depth other than 24.
760 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
763 /* check if the user specified a complete display name (with screen).
764 * If so, only manage the specified screen */
766 pos
= strchr(DisplayName
, ':');
770 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
773 DisplayName
= XDisplayName(DisplayName
);
774 setenv("DISPLAY", DisplayName
, 1);
776 wXModifierInitialize();
779 if (w_global
.screen_count
== 1)
784 inotifyWatchConfig();