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 RShutdown(); /* wrlib clean exit */
205 wutil_shutdown(); /* WUtil clean-up */
210 void Restart(char *manager
, Bool abortOnFailure
)
213 char *argv
[MAX_RESTART_ARGS
];
216 if (manager
&& manager
[0] != 0) {
217 prog
= argv
[0] = strtok(manager
, " ");
218 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
219 argv
[i
] = strtok(NULL
, " ");
220 if (argv
[i
] == NULL
) {
230 execvp(Arguments
[0], Arguments
);
231 wfatal(_("failed to restart Window Maker."));
234 werror(_("could not exec %s"), prog
);
240 void SetupEnvironment(WScreen
* scr
)
246 int len
= strlen(DisplayName
) + 64;
248 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
249 ptr
= strchr(strchr(tmp
, ':'), '.');
252 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
257 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
258 scr
->rcontext
->attribs
->colors_per_channel
);
267 static void shellCommandHandler(pid_t pid
, unsigned int status
, void *client_data
)
269 _tuple
*data
= (_tuple
*) client_data
;
271 /* Parameter not used, but tell the compiler that it is ok */
277 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
279 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
281 } else if (status
!= 127) {
283 printf("%s: %i\n", data->command, status);
287 wfree(data
->command
);
291 void ExecuteShellCommand(WScreen
*scr
, const char *command
)
293 static char *shell
= NULL
;
297 * This have a problem: if the shell is tcsh (not sure about others)
298 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
299 * will block and the command will not be executed.
301 shell = getenv("SHELL");
312 SetupEnvironment(scr
);
317 execl(shell
, shell
, "-c", command
, NULL
);
318 werror("could not execute %s -c %s", shell
, command
);
320 } else if (pid
< 0) {
321 werror("cannot fork a new process");
323 _tuple
*data
= wmalloc(sizeof(_tuple
));
326 data
->command
= wstrdup(command
);
328 wAddDeathHandler(pid
, shellCommandHandler
, data
);
333 *---------------------------------------------------------------------
335 * Launch a new instance of the active window
337 *----------------------------------------------------------------------
339 Bool
RelaunchWindow(WWindow
*wwin
)
341 if (! wwin
|| ! wwin
->client_win
) {
342 werror("no window to relaunch");
349 if (! XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
== 0 || argv
== NULL
) {
350 werror("cannot relaunch the application because no WM_COMMAND property is set");
357 SetupEnvironment(wwin
->screen_ptr
);
361 /* argv is not null-terminated */
362 char **a
= (char **) malloc(argc
+ 1);
364 werror("out of memory trying to relaunch the application");
369 for (i
= 0; i
< argc
; i
++)
375 } else if (pid
< 0) {
376 werror("cannot fork a new process");
378 XFreeStringList(argv
);
381 _tuple
*data
= wmalloc(sizeof(_tuple
));
383 data
->scr
= wwin
->screen_ptr
;
384 data
->command
= wtokenjoin(argv
, argc
);
386 /* not actually a shell command */
387 wAddDeathHandler(pid
, shellCommandHandler
, data
);
389 XFreeStringList(argv
);
396 *---------------------------------------------------------------------
398 * Do a major cleanup and exit the program
400 *----------------------------------------------------------------------
402 noreturn
void wAbort(Bool dumpCore
)
407 for (i
= 0; i
< w_global
.screen_count
; i
++) {
408 scr
= wScreenWithNumber(i
);
412 printf(_("%s aborted.\n"), ProgName
);
419 static void print_help(void)
421 printf(_("Usage: %s [options]\n"), ProgName
);
422 puts(_("The Window Maker window manager for the X window system"));
424 puts(_(" -display host:dpy display to use"));
425 puts(_(" --no-dock do not open the application Dock"));
426 puts(_(" --no-clip do not open the workspace Clip"));
427 puts(_(" --no-autolaunch do not autolaunch applications"));
428 puts(_(" --dont-restore do not restore saved session"));
430 puts(_(" --locale locale locale to use"));
432 puts(_(" --visual-id visualid visual id of visual to use"));
433 puts(_(" --static do not update or save configurations"));
435 puts(_(" --no-polling do not periodically check for configuration updates"));
437 puts(_(" --version print version and exit"));
438 puts(_(" --help show this message"));
441 static void check_defaults(void)
445 path
= wdefaultspathfordomain("WindowMaker");
447 if (access(path
, R_OK
) != 0) {
448 wwarning(_("could not find user GNUstep directory (%s)."), path
);
450 if (system("wmaker.inst --batch") != 0) {
451 wwarning(_("There was an error while creating GNUstep directory, please "
452 "make sure you have installed Window Maker correctly and run wmaker.inst"));
454 wwarning(_("%s directory created with default configuration."), path
);
463 * Add watch here, used to notify if configuration
464 * files have changed, using linux kernel inotify mechanism
467 static void inotifyWatchConfig(void)
469 char *watchPath
= NULL
;
471 w_global
.inotify
.fd_event_queue
= inotify_init(); /* Initialise an inotify instance */
472 if (w_global
.inotify
.fd_event_queue
< 0) {
473 wwarning(_("could not initialise an inotify instance."
474 " Changes to the defaults database will require"
475 " a restart to take effect. Check your kernel!"));
477 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
478 /* Add the watch; really we are only looking for modify events
479 * but we might want more in the future so check all events for now.
480 * The individual events are checked for in event.c.
482 w_global
.inotify
.wd_defaults
= inotify_add_watch(w_global
.inotify
.fd_event_queue
, watchPath
, IN_ALL_EVENTS
);
483 if (w_global
.inotify
.wd_defaults
< 0) {
484 wwarning(_("could not add an inotify watch on path %s."
485 "Changes to the defaults database will require"
486 " a restart to take effect."), watchPath
);
487 close(w_global
.inotify
.fd_event_queue
);
488 w_global
.inotify
.fd_event_queue
= -1;
493 #endif /* HAVE_INOTIFY */
495 static void execInitScript(void)
499 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
500 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
502 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
506 if (system(file
) != 0)
507 werror(_("%s:could not execute initialization script"), file
);
513 void ExecExitScript(void)
517 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
518 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
520 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
524 if (system(file
) != 0)
525 werror(_("%s:could not execute exit script"), file
);
531 int main(int argc
, char **argv
)
533 int i_am_the_monitor
, i
, len
;
536 memset(&w_global
, 0, sizeof(w_global
));
537 w_global
.program
.state
= WSTATE_NORMAL
;
538 w_global
.program
.signal_state
= WSTATE_NORMAL
;
539 w_global
.timestamp
.last_event
= CurrentTime
;
540 w_global
.timestamp
.focus_change
= CurrentTime
;
541 w_global
.workspace
.ignore_change
= False
;
542 w_global
.shortcut
.modifiers_mask
= 0xff;
544 /* setup common stuff for the monitor and wmaker itself */
545 WMInitializeApplication("WindowMaker", &argc
, argv
);
547 memset(&wPreferences
, 0, sizeof(wPreferences
));
549 wPreferences
.fallbackWMs
= WMCreateArray(8);
550 alt
= getenv("WINDOWMAKER_ALT_WM");
552 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
554 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
555 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
556 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
557 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
558 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
559 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
560 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
562 i_am_the_monitor
= 1;
564 for (i
= 1; i
< argc
; i
++) {
565 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
566 i_am_the_monitor
= 0;
568 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
571 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
574 DisplayName
= argv
[i
];
578 DisplayName
= XDisplayName(DisplayName
);
579 len
= strlen(DisplayName
) + 64;
581 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
584 if (i_am_the_monitor
)
585 return MonitorLoop(argc
, argv
);
587 return real_main(argc
, argv
);
590 static int real_main(int argc
, char **argv
)
596 setlocale(LC_ALL
, "");
599 /* for telling WPrefs what's the name of the wmaker binary being ran */
600 setenv("WMAKER_BIN_NAME", argv
[0], 1);
603 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
604 for (i
= 0; i
< argc
; i
++) {
605 Arguments
[i
] = argv
[i
];
607 /* add the extra option to signal that we're just restarting wmaker */
608 Arguments
[argc
- 1] = "--for-real=";
609 Arguments
[argc
] = NULL
;
611 ProgName
= strrchr(argv
[0], '/');
618 for (i
= 1; i
< argc
; i
++) {
619 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
620 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv
[i
]);
621 } else if (strcmp(argv
[i
], "--for-real") == 0) {
622 wPreferences
.flags
.restarting
= 0;
623 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
624 wPreferences
.flags
.restarting
= 1;
625 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
626 wPreferences
.flags
.restarting
= 2;
627 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
628 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
629 wPreferences
.flags
.noautolaunch
= 1;
630 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
631 wPreferences
.flags
.norestore
= 1;
632 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
633 wPreferences
.flags
.nodock
= 1;
634 wPreferences
.flags
.nodrawer
= 1;
635 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
636 wPreferences
.flags
.noclip
= 1;
637 } else if (strcmp(argv
[i
], "-nodrawer") == 0 || strcmp(argv
[i
], "--no-drawer") == 0) {
638 wPreferences
.flags
.nodrawer
= 1;
639 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
640 printf("Window Maker %s\n", VERSION
);
642 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
643 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
645 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
648 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
651 w_global
.locale
= argv
[i
];
652 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
655 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
658 DisplayName
= argv
[i
];
659 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
662 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
665 if (initWVisualID(argv
[i
]) != 0) {
666 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
669 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
671 || strcmp(argv
[i
], "--no-polling") == 0
674 wPreferences
.flags
.noupdates
= 1;
675 } else if (strcmp(argv
[i
], "--help") == 0) {
679 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
680 printf(_("Try '%s --help' for more information\n"), argv
[0]);
686 if (!wPreferences
.flags
.noupdates
) {
687 /* check existence of Defaults DB directory */
691 if (w_global
.locale
) {
692 setenv("LANG", w_global
.locale
, 1);
694 w_global
.locale
= getenv("LC_ALL");
695 if (!w_global
.locale
) {
696 w_global
.locale
= getenv("LANG");
700 setlocale(LC_ALL
, "");
702 if (!w_global
.locale
|| strcmp(w_global
.locale
, "C") == 0 || strcmp(w_global
.locale
, "POSIX") == 0)
703 w_global
.locale
= NULL
;
705 if (getenv("NLSPATH")) {
706 bindtextdomain("WindowMaker", getenv("NLSPATH"));
707 #if defined(MENU_TEXTDOMAIN)
708 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
711 bindtextdomain("WindowMaker", LOCALEDIR
);
712 #if defined(MENU_TEXTDOMAIN)
713 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
716 bind_textdomain_codeset("WindowMaker", "UTF-8");
717 #if defined(MENU_TEXTDOMAIN)
718 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
720 textdomain("WindowMaker");
722 if (!XSupportsLocale()) {
723 wwarning(_("X server does not support locale"));
726 if (XSetLocaleModifiers("") == NULL
) {
727 wwarning(_("cannot set locale modifiers"));
731 if (w_global
.locale
) {
734 w_global
.locale
= wstrdup(w_global
.locale
);
735 ptr
= strchr(w_global
.locale
, '.');
741 dpy
= XOpenDisplay(DisplayName
);
743 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
747 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
748 werror("error setting close-on-exec flag for X connection");
753 if (getWVisualID(0) < 0) {
755 * If unspecified, use default visual instead of waiting
756 * for wrlib/context.c:bestContext() that may end up choosing
757 * the "fake" 24 bits added by the Composite extension.
758 * This is required to avoid all sort of corruptions when
759 * composite is enabled, and at a depth other than 24.
761 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
764 /* check if the user specified a complete display name (with screen).
765 * If so, only manage the specified screen */
767 pos
= strchr(DisplayName
, ':');
771 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
774 DisplayName
= XDisplayName(DisplayName
);
775 setenv("DISPLAY", DisplayName
, 1);
777 wXModifierInitialize();
780 if (w_global
.screen_count
== 1)
785 inotifyWatchConfig();