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;
82 struct WPreferences wPreferences
;
84 WShortKey wKeyBindings
[WKBD_LAST
];
86 /* defaults domains */
87 WDDomain
*WDWindowMaker
= NULL
;
88 WDDomain
*WDWindowAttributes
= NULL
;
89 WDDomain
*WDRootMenu
= NULL
;
96 #ifdef KEEP_XKB_LOCK_STATUS
102 char WDelayedActionSet
= 0;
105 const char WMNManaged
[] = "WMNManaged";
106 const char WMNUnmanaged
[] = "WMNUnmanaged";
107 const char WMNChangedWorkspace
[] = "WMNChangedWorkspace";
108 const char WMNChangedState
[] = "WMNChangedState";
109 const char WMNChangedFocus
[] = "WMNChangedFocus";
110 const char WMNChangedStacking
[] = "WMNChangedStacking";
111 const char WMNChangedName
[] = "WMNChangedName";
113 const char WMNWorkspaceCreated
[] = "WMNWorkspaceCreated";
114 const char WMNWorkspaceDestroyed
[] = "WMNWorkspaceDestroyed";
115 const char WMNWorkspaceChanged
[] = "WMNWorkspaceChanged";
116 const char WMNWorkspaceNameChanged
[] = "WMNWorkspaceNameChanged";
118 const char WMNResetStacking
[] = "WMNResetStacking";
120 /******** End Global Variables *****/
122 static char *DisplayName
= NULL
;
124 static char **Arguments
;
128 static Bool multiHead
= True
;
130 static int *wVisualID
= NULL
;
131 static int wVisualID_len
= 0;
133 static int real_main(int argc
, char **argv
);
135 int getWVisualID(int screen
)
137 if (wVisualID
== NULL
)
139 if (screen
< 0 || screen
>= wVisualID_len
)
142 return wVisualID
[screen
];
145 static void setWVisualID(int screen
, int val
)
152 if (wVisualID
== NULL
) {
153 /* no array at all, alloc space for screen + 1 entries
154 * and init with default value */
155 wVisualID_len
= screen
+ 1;
156 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
157 for (i
= 0; i
< wVisualID_len
; i
++) {
160 } else if (screen
>= wVisualID_len
) {
161 /* larger screen number than previously allocated
163 int oldlen
= wVisualID_len
;
165 wVisualID_len
= screen
+ 1;
166 wVisualID
= (int *)realloc(wVisualID
, wVisualID_len
* sizeof(int));
167 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
172 wVisualID
[screen
] = val
;
176 * this function splits a given string at the comma into tokens
177 * and set the wVisualID variable to each parsed number
179 static int initWVisualID(const char *user_str
)
181 char *mystr
= strdup(user_str
);
188 /* check for delimiter */
189 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
192 mystr
[cur_out_pos
] = '\0';
194 if (sscanf(mystr
, "%i", &v
) != 1) {
199 setWVisualID(cur_screen
, v
);
205 /* break in case last char has been consumed */
206 if (user_str
[cur_in_pos
] == '\0')
209 /* if the current char is no delimiter put it into mystr */
210 if (user_str
[cur_in_pos
] != ',') {
211 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
218 if (cur_screen
== 0||error_found
!= 0)
224 noreturn
void Exit(int status
)
232 void Restart(char *manager
, Bool abortOnFailure
)
235 char *argv
[MAX_RESTART_ARGS
];
238 if (manager
&& manager
[0] != 0) {
239 prog
= argv
[0] = strtok(manager
, " ");
240 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
241 argv
[i
] = strtok(NULL
, " ");
242 if (argv
[i
] == NULL
) {
252 execvp(Arguments
[0], Arguments
);
253 wfatal(_("failed to restart Window Maker."));
256 werror(_("could not exec %s"), prog
);
262 void SetupEnvironment(WScreen
* scr
)
268 int len
= strlen(DisplayName
) + 64;
270 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
271 ptr
= strchr(strchr(tmp
, ':'), '.');
274 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
279 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
280 scr
->rcontext
->attribs
->colors_per_channel
);
289 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
294 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
296 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
298 } else if (status
!= 127) {
300 printf("%s: %i\n", data->command, status);
304 wfree(data
->command
);
308 void ExecuteShellCommand(WScreen
*scr
, const char *command
)
310 static char *shell
= NULL
;
314 * This have a problem: if the shell is tcsh (not sure about others)
315 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
316 * will block and the command will not be executed.
318 shell = getenv("SHELL");
329 SetupEnvironment(scr
);
334 execl(shell
, shell
, "-c", command
, NULL
);
335 werror("could not execute %s -c %s", shell
, command
);
337 } else if (pid
< 0) {
338 werror("cannot fork a new process");
340 _tuple
*data
= wmalloc(sizeof(_tuple
));
343 data
->command
= wstrdup(command
);
345 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
350 *---------------------------------------------------------------------
352 * Launch a new instance of the active window
354 *----------------------------------------------------------------------
356 Bool
RelaunchWindow(WWindow
*wwin
)
358 if (! wwin
|| ! wwin
->client_win
) {
359 werror("no window to relaunch");
366 if (! XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
== 0 || argv
== NULL
) {
367 werror("cannot relaunch the application because no WM_COMMAND property is set");
374 SetupEnvironment(wwin
->screen_ptr
);
378 /* argv is not null-terminated */
379 char **a
= (char **) malloc(argc
+ 1);
381 werror("out of memory trying to relaunch the application");
386 for (i
= 0; i
< argc
; i
++)
392 } else if (pid
< 0) {
393 werror("cannot fork a new process");
395 XFreeStringList(argv
);
398 _tuple
*data
= wmalloc(sizeof(_tuple
));
400 data
->scr
= wwin
->screen_ptr
;
401 data
->command
= wtokenjoin(argv
, argc
);
403 /* not actually a shell command */
404 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
406 XFreeStringList(argv
);
413 *---------------------------------------------------------------------
415 * Do a major cleanup and exit the program
417 *----------------------------------------------------------------------
419 noreturn
void wAbort(Bool dumpCore
)
424 for (i
= 0; i
< w_global
.screen_count
; i
++) {
425 scr
= wScreenWithNumber(i
);
429 printf(_("%s aborted.\n"), ProgName
);
436 static void print_help(void)
438 printf(_("Usage: %s [options]\n"), ProgName
);
439 puts(_("The Window Maker window manager for the X window system"));
441 puts(_(" -display host:dpy display to use"));
442 puts(_(" --no-dock do not open the application Dock"));
443 puts(_(" --no-clip do not open the workspace Clip"));
444 puts(_(" --no-autolaunch do not autolaunch applications"));
445 puts(_(" --dont-restore do not restore saved session"));
447 puts(_(" --locale locale locale to use"));
449 puts(_(" --visual-id visualid visual id of visual to use"));
450 puts(_(" --static do not update or save configurations"));
452 puts(_(" --no-polling do not periodically check for configuration updates"));
454 puts(_(" --version print version and exit"));
455 puts(_(" --help show this message"));
458 static void check_defaults(void)
462 path
= wdefaultspathfordomain("WindowMaker");
464 if (access(path
, R_OK
) != 0) {
465 wwarning(_("could not find user GNUstep directory (%s)."), path
);
467 if (system("wmaker.inst --batch") != 0) {
468 wwarning(_("There was an error while creating GNUstep directory, please "
469 "make sure you have installed Window Maker correctly and run wmaker.inst"));
471 wwarning(_("%s directory created with default configuration."), path
);
480 * Add watch here, used to notify if configuration
481 * files have changed, using linux kernel inotify mechanism
484 static void inotifyWatchConfig(void)
486 char *watchPath
= NULL
;
487 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
489 wwarning(_("could not initialise an inotify instance."
490 " Changes to the defaults database will require"
491 " a restart to take effect. Check your kernel!"));
493 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
494 /* Add the watch; really we are only looking for modify events
495 * but we might want more in the future so check all events for now.
496 * The individual events are checked for in event.c.
498 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
500 wwarning(_("could not add an inotify watch on path %s."
501 "Changes to the defaults database will require"
502 " a restart to take effect."), watchPath
);
508 #endif /* HAVE_INOTIFY */
510 static void execInitScript(void)
514 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
515 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
517 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
521 if (system(file
) != 0)
522 werror(_("%s:could not execute initialization script"), file
);
528 void ExecExitScript(void)
532 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
533 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
535 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
539 if (system(file
) != 0)
540 werror(_("%s:could not execute exit script"), file
);
546 int main(int argc
, char **argv
)
548 int i_am_the_monitor
, i
, len
;
551 memset(&w_global
, 0, sizeof(w_global
));
552 w_global
.program
.state
= WSTATE_NORMAL
;
553 w_global
.program
.signal_state
= WSTATE_NORMAL
;
554 w_global
.timestamp
.last_event
= CurrentTime
;
555 w_global
.timestamp
.focus_change
= CurrentTime
;
556 w_global
.workspace
.ignore_change
= False
;
558 /* setup common stuff for the monitor and wmaker itself */
559 WMInitializeApplication("WindowMaker", &argc
, argv
);
561 memset(&wPreferences
, 0, sizeof(wPreferences
));
563 wPreferences
.fallbackWMs
= WMCreateArray(8);
564 alt
= getenv("WINDOWMAKER_ALT_WM");
566 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
568 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
569 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
570 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
571 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
572 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
573 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
574 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
576 i_am_the_monitor
= 1;
578 for (i
= 1; i
< argc
; i
++) {
579 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
580 i_am_the_monitor
= 0;
582 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
585 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
588 DisplayName
= argv
[i
];
592 DisplayName
= XDisplayName(DisplayName
);
593 len
= strlen(DisplayName
) + 64;
595 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
598 if (i_am_the_monitor
)
599 return MonitorLoop(argc
, argv
);
601 return real_main(argc
, argv
);
604 static int real_main(int argc
, char **argv
)
610 setlocale(LC_ALL
, "");
613 /* for telling WPrefs what's the name of the wmaker binary being ran */
614 setenv("WMAKER_BIN_NAME", argv
[0], 1);
617 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
618 for (i
= 0; i
< argc
; i
++) {
619 Arguments
[i
] = argv
[i
];
621 /* add the extra option to signal that we're just restarting wmaker */
622 Arguments
[argc
- 1] = "--for-real=";
623 Arguments
[argc
] = NULL
;
625 ProgName
= strrchr(argv
[0], '/');
632 for (i
= 1; i
< argc
; i
++) {
633 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
634 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv
[i
]);
635 } else if (strcmp(argv
[i
], "--for-real") == 0) {
636 wPreferences
.flags
.restarting
= 0;
637 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
638 wPreferences
.flags
.restarting
= 1;
639 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
640 wPreferences
.flags
.restarting
= 2;
641 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
642 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
643 wPreferences
.flags
.noautolaunch
= 1;
644 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
645 wPreferences
.flags
.norestore
= 1;
646 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
647 wPreferences
.flags
.nodock
= 1;
648 wPreferences
.flags
.nodrawer
= 1;
649 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
650 wPreferences
.flags
.noclip
= 1;
651 } else if (strcmp(argv
[i
], "-nodrawer") == 0 || strcmp(argv
[i
], "--no-drawer") == 0) {
652 wPreferences
.flags
.nodrawer
= 1;
653 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
654 printf("Window Maker %s\n", VERSION
);
656 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
657 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
659 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
662 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
665 w_global
.locale
= argv
[i
];
666 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
669 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
672 DisplayName
= argv
[i
];
673 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
676 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
679 if (initWVisualID(argv
[i
]) != 0) {
680 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
683 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
685 || strcmp(argv
[i
], "--no-polling") == 0
688 wPreferences
.flags
.noupdates
= 1;
689 } else if (strcmp(argv
[i
], "--help") == 0) {
693 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
694 printf(_("Try '%s --help' for more information\n"), argv
[0]);
700 if (!wPreferences
.flags
.noupdates
) {
701 /* check existence of Defaults DB directory */
705 if (w_global
.locale
) {
706 setenv("LANG", w_global
.locale
, 1);
708 w_global
.locale
= getenv("LC_ALL");
709 if (!w_global
.locale
) {
710 w_global
.locale
= getenv("LANG");
714 setlocale(LC_ALL
, "");
716 if (!w_global
.locale
|| strcmp(w_global
.locale
, "C") == 0 || strcmp(w_global
.locale
, "POSIX") == 0)
717 w_global
.locale
= NULL
;
719 if (getenv("NLSPATH")) {
720 bindtextdomain("WindowMaker", getenv("NLSPATH"));
721 #if defined(MENU_TEXTDOMAIN)
722 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
725 bindtextdomain("WindowMaker", LOCALEDIR
);
726 #if defined(MENU_TEXTDOMAIN)
727 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
730 bind_textdomain_codeset("WindowMaker", "UTF-8");
731 #if defined(MENU_TEXTDOMAIN)
732 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
734 textdomain("WindowMaker");
736 if (!XSupportsLocale()) {
737 wwarning(_("X server does not support locale"));
740 if (XSetLocaleModifiers("") == NULL
) {
741 wwarning(_("cannot set locale modifiers"));
745 if (w_global
.locale
) {
748 w_global
.locale
= wstrdup(w_global
.locale
);
749 ptr
= strchr(w_global
.locale
, '.');
755 dpy
= XOpenDisplay(DisplayName
);
757 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
761 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
762 werror("error setting close-on-exec flag for X connection");
767 if (getWVisualID(0) < 0) {
769 * If unspecified, use default visual instead of waiting
770 * for wrlib/context.c:bestContext() that may end up choosing
771 * the "fake" 24 bits added by the Composite extension.
772 * This is required to avoid all sort of corruptions when
773 * composite is enabled, and at a depth other than 24.
775 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
778 /* check if the user specified a complete display name (with screen).
779 * If so, only manage the specified screen */
781 pos
= strchr(DisplayName
, ':');
785 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
788 DisplayName
= XDisplayName(DisplayName
);
789 setenv("DISPLAY", DisplayName
, 1);
791 wXModifierInitialize();
794 if (w_global
.screen_count
== 1)
799 inotifyWatchConfig();