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 /****** Global Variables ******/
63 struct wmaker_global_variables w_global
;
71 struct WPreferences wPreferences
;
73 WShortKey wKeyBindings
[WKBD_LAST
];
76 const char WMNManaged
[] = "WMNManaged";
77 const char WMNUnmanaged
[] = "WMNUnmanaged";
78 const char WMNChangedWorkspace
[] = "WMNChangedWorkspace";
79 const char WMNChangedState
[] = "WMNChangedState";
80 const char WMNChangedFocus
[] = "WMNChangedFocus";
81 const char WMNChangedStacking
[] = "WMNChangedStacking";
82 const char WMNChangedName
[] = "WMNChangedName";
84 const char WMNWorkspaceCreated
[] = "WMNWorkspaceCreated";
85 const char WMNWorkspaceDestroyed
[] = "WMNWorkspaceDestroyed";
86 const char WMNWorkspaceChanged
[] = "WMNWorkspaceChanged";
87 const char WMNWorkspaceNameChanged
[] = "WMNWorkspaceNameChanged";
89 const char WMNResetStacking
[] = "WMNResetStacking";
91 /******** End Global Variables *****/
93 static char *DisplayName
= NULL
;
95 static char **Arguments
;
99 static Bool multiHead
= True
;
101 static int *wVisualID
= NULL
;
102 static int wVisualID_len
= 0;
104 static int real_main(int argc
, char **argv
);
106 int getWVisualID(int screen
)
108 if (wVisualID
== NULL
)
110 if (screen
< 0 || screen
>= wVisualID_len
)
113 return wVisualID
[screen
];
116 static void setWVisualID(int screen
, int val
)
123 if (wVisualID
== NULL
) {
124 /* no array at all, alloc space for screen + 1 entries
125 * and init with default value */
126 wVisualID_len
= screen
+ 1;
127 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
128 for (i
= 0; i
< wVisualID_len
; i
++) {
131 } else if (screen
>= wVisualID_len
) {
132 /* larger screen number than previously allocated
134 int oldlen
= wVisualID_len
;
136 wVisualID_len
= screen
+ 1;
137 wVisualID
= (int *)wrealloc(wVisualID
, wVisualID_len
* sizeof(int));
138 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
143 wVisualID
[screen
] = val
;
147 * this function splits a given string at the comma into tokens
148 * and set the wVisualID variable to each parsed number
150 static int initWVisualID(const char *user_str
)
152 char *mystr
= strdup(user_str
);
159 /* check for delimiter */
160 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
163 mystr
[cur_out_pos
] = '\0';
165 if (sscanf(mystr
, "%i", &v
) != 1) {
170 setWVisualID(cur_screen
, v
);
176 /* break in case last char has been consumed */
177 if (user_str
[cur_in_pos
] == '\0')
180 /* if the current char is no delimiter put it into mystr */
181 if (user_str
[cur_in_pos
] != ',') {
182 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
189 if (cur_screen
== 0||error_found
!= 0)
195 noreturn
void Exit(int status
)
200 RShutdown(); /* wrlib clean exit */
201 wutil_shutdown(); /* WUtil clean-up */
206 void Restart(char *manager
, Bool abortOnFailure
)
209 char *argv
[MAX_RESTART_ARGS
];
212 if (manager
&& manager
[0] != 0) {
213 prog
= argv
[0] = strtok(manager
, " ");
214 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
215 argv
[i
] = strtok(NULL
, " ");
216 if (argv
[i
] == NULL
) {
226 execvp(Arguments
[0], Arguments
);
227 wfatal(_("failed to restart Window Maker."));
230 werror(_("could not exec %s"), prog
);
236 void SetupEnvironment(WScreen
* scr
)
242 int len
= strlen(DisplayName
) + 64;
244 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
246 /* Search from the end to be compatible with ipv6 address */
247 ptr
= strrchr(tmp
, ':');
249 static Bool message_already_displayed
= False
;
251 if (!message_already_displayed
)
252 wwarning(_("the display name has an unexpected syntax: \"%s\""),
253 XDisplayName(DisplayName
));
254 message_already_displayed
= True
;
256 /* If found, remove the screen specification from the display variable */
257 ptr
= strchr(ptr
, '.');
261 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
266 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
267 scr
->rcontext
->attribs
->colors_per_channel
);
276 static void shellCommandHandler(pid_t pid
, unsigned int status
, void *client_data
)
278 _tuple
*data
= (_tuple
*) client_data
;
280 /* Parameter not used, but tell the compiler that it is ok */
286 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
288 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
290 } else if (status
!= 127) {
292 printf("%s: %i\n", data->command, status);
296 wfree(data
->command
);
300 void ExecuteShellCommand(WScreen
*scr
, const char *command
)
302 static char *shell
= NULL
;
306 * This have a problem: if the shell is tcsh (not sure about others)
307 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
308 * will block and the command will not be executed.
310 shell = getenv("SHELL");
321 SetupEnvironment(scr
);
326 execl(shell
, shell
, "-c", command
, NULL
);
327 werror("could not execute %s -c %s", shell
, command
);
329 } else if (pid
< 0) {
330 werror("cannot fork a new process");
332 _tuple
*data
= wmalloc(sizeof(_tuple
));
335 data
->command
= wstrdup(command
);
337 wAddDeathHandler(pid
, shellCommandHandler
, data
);
342 *---------------------------------------------------------------------
344 * Launch a new instance of the active window
346 *----------------------------------------------------------------------
348 Bool
RelaunchWindow(WWindow
*wwin
)
350 if (! wwin
|| ! wwin
->client_win
) {
351 werror("no window to relaunch");
358 if (! XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
== 0 || argv
== NULL
) {
359 werror("cannot relaunch the application because no WM_COMMAND property is set");
366 SetupEnvironment(wwin
->screen_ptr
);
370 /* argv is not null-terminated */
371 char **a
= (char **) malloc(argc
+ 1);
373 werror("out of memory trying to relaunch the application");
378 for (i
= 0; i
< argc
; i
++)
384 } else if (pid
< 0) {
385 werror("cannot fork a new process");
387 XFreeStringList(argv
);
390 _tuple
*data
= wmalloc(sizeof(_tuple
));
392 data
->scr
= wwin
->screen_ptr
;
393 data
->command
= wtokenjoin(argv
, argc
);
395 /* not actually a shell command */
396 wAddDeathHandler(pid
, shellCommandHandler
, data
);
398 XFreeStringList(argv
);
405 *---------------------------------------------------------------------
407 * Do a major cleanup and exit the program
409 *----------------------------------------------------------------------
411 noreturn
void wAbort(Bool dumpCore
)
416 for (i
= 0; i
< w_global
.screen_count
; i
++) {
417 scr
= wScreenWithNumber(i
);
421 printf(_("%s aborted.\n"), ProgName
);
428 static void print_help(void)
430 printf(_("Usage: %s [options]\n"), ProgName
);
431 puts(_("The Window Maker window manager for the X window system"));
433 puts(_(" -display host:dpy display to use"));
434 #ifdef USE_ICCCM_WMREPLACE
435 puts(_(" --replace replace running window manager"));
437 puts(_(" --no-dock do not open the application Dock"));
438 puts(_(" --no-clip do not open the workspace Clip"));
439 puts(_(" --no-autolaunch do not autolaunch applications"));
440 puts(_(" --no-drawer disable drawers in the dock"));
441 puts(_(" --dont-restore do not restore saved session"));
443 puts(_(" --locale locale locale to use"));
445 puts(_(" --visual-id visualid visual id of visual to use"));
446 puts(_(" --static do not update or save configurations"));
448 puts(_(" --no-polling do not periodically check for configuration updates"));
450 puts(_(" --global_defaults_path print the path for default config and exit"));
451 puts(_(" --version print version and exit"));
452 puts(_(" --help show this message"));
455 static void check_defaults(void)
459 path
= wdefaultspathfordomain("WindowMaker");
461 if (access(path
, R_OK
) != 0) {
462 wwarning(_("could not find user GNUstep directory (%s)."), path
);
464 if (system("wmaker.inst --batch") != 0) {
465 wwarning(_("There was an error while creating GNUstep directory, please "
466 "make sure you have installed Window Maker correctly and run wmaker.inst"));
468 wwarning(_("%s directory created with default configuration."), path
);
477 * Add watch here, used to notify if configuration
478 * files have changed, using linux kernel inotify mechanism
481 static void inotifyWatchConfig(void)
483 char *watchPath
= NULL
;
485 w_global
.inotify
.fd_event_queue
= inotify_init(); /* Initialise an inotify instance */
486 if (w_global
.inotify
.fd_event_queue
< 0) {
487 wwarning(_("could not initialise an inotify instance."
488 " Changes to the defaults database will require"
489 " a restart to take effect. Check your kernel!"));
491 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
492 /* Add the watch; really we are only looking for modify events
493 * but we might want more in the future so check all events for now.
494 * The individual events are checked for in event.c.
496 w_global
.inotify
.wd_defaults
= inotify_add_watch(w_global
.inotify
.fd_event_queue
, watchPath
, IN_ALL_EVENTS
);
497 if (w_global
.inotify
.wd_defaults
< 0) {
498 wwarning(_("could not add an inotify watch on path %s."
499 " Changes to the defaults database will require"
500 " a restart to take effect."), watchPath
);
501 close(w_global
.inotify
.fd_event_queue
);
502 w_global
.inotify
.fd_event_queue
= -1;
507 #endif /* HAVE_INOTIFY */
509 static void execInitScript(void)
513 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
514 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
516 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
520 if (system(file
) != 0)
521 werror(_("%s:could not execute initialization script"), file
);
527 void ExecExitScript(void)
531 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
532 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
534 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
538 if (system(file
) != 0)
539 werror(_("%s:could not execute exit script"), file
);
545 int main(int argc
, char **argv
)
547 int i_am_the_monitor
, i
, len
;
550 memset(&w_global
, 0, sizeof(w_global
));
551 w_global
.program
.state
= WSTATE_NORMAL
;
552 w_global
.program
.signal_state
= WSTATE_NORMAL
;
553 w_global
.timestamp
.last_event
= CurrentTime
;
554 w_global
.timestamp
.focus_change
= CurrentTime
;
555 w_global
.ignore_workspace_change
= False
;
556 w_global
.shortcut
.modifiers_mask
= 0xff;
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 #ifdef USE_ICCCM_WMREPLACE
654 } else if (strcmp(argv
[i
], "-replace") == 0 || strcmp(argv
[i
], "--replace") == 0) {
655 wPreferences
.flags
.replace
= 1;
657 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
658 printf("Window Maker %s\n", VERSION
);
660 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
661 printf("%s\n", DEFSDATADIR
);
663 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
666 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
669 w_global
.locale
= argv
[i
];
670 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
673 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
676 DisplayName
= argv
[i
];
677 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
680 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
683 if (initWVisualID(argv
[i
]) != 0) {
684 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
687 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0) {
688 wPreferences
.flags
.noupdates
= 1;
689 } else if (strcmp(argv
[i
], "--no-polling") == 0) {
691 wPreferences
.flags
.noupdates
= 1;
693 wmessage(_("your version of Window Maker was compiled with INotify support, so \"--no-polling\" has no effect"));
695 } else if (strcmp(argv
[i
], "--help") == 0) {
699 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
700 printf(_("Try '%s --help' for more information\n"), argv
[0]);
706 if (!wPreferences
.flags
.noupdates
) {
707 /* check existence of Defaults DB directory */
711 if (w_global
.locale
) {
712 setenv("LANG", w_global
.locale
, 1);
714 w_global
.locale
= getenv("LC_ALL");
715 if (!w_global
.locale
) {
716 w_global
.locale
= getenv("LANG");
720 setlocale(LC_ALL
, "");
722 if (!w_global
.locale
|| strcmp(w_global
.locale
, "C") == 0 || strcmp(w_global
.locale
, "POSIX") == 0)
723 w_global
.locale
= NULL
;
725 if (getenv("NLSPATH")) {
726 bindtextdomain("WindowMaker", getenv("NLSPATH"));
727 #if defined(MENU_TEXTDOMAIN)
728 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
731 bindtextdomain("WindowMaker", LOCALEDIR
);
732 #if defined(MENU_TEXTDOMAIN)
733 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
736 bind_textdomain_codeset("WindowMaker", "UTF-8");
737 #if defined(MENU_TEXTDOMAIN)
738 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
740 textdomain("WindowMaker");
742 if (!XSupportsLocale()) {
743 wwarning(_("X server does not support locale"));
746 if (XSetLocaleModifiers("") == NULL
) {
747 wwarning(_("cannot set locale modifiers"));
751 if (w_global
.locale
) {
754 w_global
.locale
= wstrdup(w_global
.locale
);
755 ptr
= strchr(w_global
.locale
, '.');
761 dpy
= XOpenDisplay(DisplayName
);
763 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
767 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
768 werror("error setting close-on-exec flag for X connection");
773 if (getWVisualID(0) < 0) {
775 * If unspecified, use default visual instead of waiting
776 * for wrlib/context.c:bestContext() that may end up choosing
777 * the "fake" 24 bits added by the Composite extension.
778 * This is required to avoid all sort of corruptions when
779 * composite is enabled, and at a depth other than 24.
781 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
784 /* check if the user specified a complete display name (with screen).
785 * If so, only manage the specified screen */
787 pos
= strchr(DisplayName
, ':');
791 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
794 DisplayName
= XDisplayName(DisplayName
);
795 setenv("DISPLAY", DisplayName
, 1);
797 wXModifierInitialize();
800 if (w_global
.screen_count
== 1)
805 inotifyWatchConfig();