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"
61 #include <WINGs/WUtil.h>
63 /****** Global Variables ******/
64 struct wmaker_global_variables w_global
;
72 struct WPreferences wPreferences
;
74 WShortKey wKeyBindings
[WKBD_LAST
];
77 const char WMNManaged
[] = "WMNManaged";
78 const char WMNUnmanaged
[] = "WMNUnmanaged";
79 const char WMNChangedWorkspace
[] = "WMNChangedWorkspace";
80 const char WMNChangedState
[] = "WMNChangedState";
81 const char WMNChangedFocus
[] = "WMNChangedFocus";
82 const char WMNChangedStacking
[] = "WMNChangedStacking";
83 const char WMNChangedName
[] = "WMNChangedName";
85 const char WMNWorkspaceCreated
[] = "WMNWorkspaceCreated";
86 const char WMNWorkspaceDestroyed
[] = "WMNWorkspaceDestroyed";
87 const char WMNWorkspaceChanged
[] = "WMNWorkspaceChanged";
88 const char WMNWorkspaceNameChanged
[] = "WMNWorkspaceNameChanged";
90 const char WMNResetStacking
[] = "WMNResetStacking";
92 /******** End Global Variables *****/
94 static char *DisplayName
= NULL
;
96 static char **Arguments
;
100 static Bool multiHead
= True
;
102 static int *wVisualID
= NULL
;
103 static int wVisualID_len
= 0;
105 static int real_main(int argc
, char **argv
);
107 int getWVisualID(int screen
)
109 if (wVisualID
== NULL
)
111 if (screen
< 0 || screen
>= wVisualID_len
)
114 return wVisualID
[screen
];
117 static void setWVisualID(int screen
, int val
)
124 if (wVisualID
== NULL
) {
125 /* no array at all, alloc space for screen + 1 entries
126 * and init with default value */
127 wVisualID_len
= screen
+ 1;
128 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
129 for (i
= 0; i
< wVisualID_len
; i
++) {
132 } else if (screen
>= wVisualID_len
) {
133 /* larger screen number than previously allocated
135 int oldlen
= wVisualID_len
;
137 wVisualID_len
= screen
+ 1;
138 wVisualID
= (int *)wrealloc(wVisualID
, wVisualID_len
* sizeof(int));
139 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
144 wVisualID
[screen
] = val
;
148 * this function splits a given string at the comma into tokens
149 * and set the wVisualID variable to each parsed number
151 static int initWVisualID(const char *user_str
)
153 char *mystr
= strdup(user_str
);
160 /* check for delimiter */
161 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
164 mystr
[cur_out_pos
] = '\0';
166 if (sscanf(mystr
, "%i", &v
) != 1) {
171 setWVisualID(cur_screen
, v
);
177 /* break in case last char has been consumed */
178 if (user_str
[cur_in_pos
] == '\0')
181 /* if the current char is no delimiter put it into mystr */
182 if (user_str
[cur_in_pos
] != ',') {
183 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
190 if (cur_screen
== 0||error_found
!= 0)
196 noreturn
void Exit(int status
)
201 RShutdown(); /* wrlib clean exit */
202 wutil_shutdown(); /* WUtil clean-up */
207 void Restart(char *manager
, Bool abortOnFailure
)
210 char *argv
[MAX_RESTART_ARGS
];
213 if (manager
&& manager
[0] != 0) {
214 prog
= argv
[0] = strtok(manager
, " ");
215 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
216 argv
[i
] = strtok(NULL
, " ");
217 if (argv
[i
] == NULL
) {
227 execvp(Arguments
[0], Arguments
);
228 wfatal(_("failed to restart Window Maker."));
231 werror(_("could not exec %s"), prog
);
237 void SetupEnvironment(WScreen
* scr
)
243 int len
= strlen(DisplayName
) + 64;
245 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
247 /* Search from the end to be compatible with ipv6 address */
248 ptr
= strrchr(tmp
, ':');
250 static Bool message_already_displayed
= False
;
252 if (!message_already_displayed
)
253 wwarning(_("the display name has an unexpected syntax: \"%s\""),
254 XDisplayName(DisplayName
));
255 message_already_displayed
= True
;
257 /* If found, remove the screen specification from the display variable */
258 ptr
= strchr(ptr
, '.');
262 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
267 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
268 scr
->rcontext
->attribs
->colors_per_channel
);
277 static void shellCommandHandler(pid_t pid
, unsigned int status
, void *client_data
)
279 _tuple
*data
= (_tuple
*) client_data
;
281 /* Parameter not used, but tell the compiler that it is ok */
287 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
289 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
291 } else if (status
!= 127) {
293 printf("%s: %i\n", data->command, status);
297 wfree(data
->command
);
301 void ExecuteShellCommand(WScreen
*scr
, const char *command
)
303 static char *shell
= NULL
;
307 * This have a problem: if the shell is tcsh (not sure about others)
308 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
309 * will block and the command will not be executed.
311 shell = getenv("SHELL");
322 SetupEnvironment(scr
);
327 execl(shell
, shell
, "-c", command
, NULL
);
328 werror("could not execute %s -c %s", shell
, command
);
330 } else if (pid
< 0) {
331 werror("cannot fork a new process");
333 _tuple
*data
= wmalloc(sizeof(_tuple
));
336 data
->command
= wstrdup(command
);
338 wAddDeathHandler(pid
, shellCommandHandler
, data
);
343 *---------------------------------------------------------------------
345 * Launch a new instance of the active window
347 *----------------------------------------------------------------------
349 Bool
RelaunchWindow(WWindow
*wwin
)
351 if (! wwin
|| ! wwin
->client_win
) {
352 werror("no window to relaunch");
358 char *command
= NULL
;
360 command
= GetCommandForWindow(wwin
->client_win
);
363 werror("cannot relaunch the application because no associated process found");
365 werror("cannot relaunch the application because no WM_COMMAND property is set");
369 wtokensplit(command
, &argv
, &argc
);
375 SetupEnvironment(wwin
->screen_ptr
);
379 /* argv is not null-terminated */
380 char **a
= (char **) malloc(argc
+ 1);
382 werror("out of memory trying to relaunch the application");
387 for (i
= 0; i
< argc
; i
++)
393 } else if (pid
< 0) {
394 werror("cannot fork a new process");
396 wtokenfree(argv
, argc
);
400 _tuple
*data
= wmalloc(sizeof(_tuple
));
402 data
->scr
= wwin
->screen_ptr
;
403 data
->command
= command
;
405 /* not actually a shell command */
406 wAddDeathHandler(pid
, shellCommandHandler
, data
);
408 wtokenfree(argv
, argc
);
415 *---------------------------------------------------------------------
417 * Do a major cleanup and exit the program
419 *----------------------------------------------------------------------
421 noreturn
void wAbort(Bool dumpCore
)
426 for (i
= 0; i
< w_global
.screen_count
; 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 #ifdef USE_ICCCM_WMREPLACE
445 puts(_(" --replace replace running window manager"));
447 puts(_(" --no-dock do not open the application Dock"));
448 puts(_(" --no-clip do not open the workspace Clip"));
449 puts(_(" --no-autolaunch do not autolaunch applications"));
450 puts(_(" --no-drawer disable drawers in the dock"));
451 puts(_(" --dont-restore do not restore saved session"));
453 puts(_(" --locale locale locale to use"));
455 puts(_(" --visual-id visualid visual id of visual to use"));
456 puts(_(" --static do not update or save configurations"));
458 puts(_(" --no-polling do not periodically check for configuration updates"));
460 puts(_(" --global_defaults_path print the path for default config and exit"));
461 puts(_(" --version print version and exit"));
462 puts(_(" --help show this message"));
465 static void check_defaults(void)
469 path
= wdefaultspathfordomain("WindowMaker");
471 if (access(path
, R_OK
) != 0) {
472 wwarning(_("could not find user GNUstep directory (%s)."), path
);
474 if (system("wmaker.inst --batch") != 0) {
475 wwarning(_("There was an error while creating GNUstep directory, please "
476 "make sure you have installed Window Maker correctly and run wmaker.inst"));
478 wwarning(_("%s directory created with default configuration."), path
);
487 * Add watch here, used to notify if configuration
488 * files have changed, using linux kernel inotify mechanism
491 static void inotifyWatchConfig(void)
493 char *watchPath
= NULL
;
495 w_global
.inotify
.fd_event_queue
= inotify_init(); /* Initialise an inotify instance */
496 if (w_global
.inotify
.fd_event_queue
< 0) {
497 wwarning(_("could not initialise an inotify instance."
498 " Changes to the defaults database will require"
499 " a restart to take effect. Check your kernel!"));
501 watchPath
= wdefaultspathfordomain("");
502 /* Add the watch; really we are only looking for modify events
503 * but we might want more in the future so check all events for now.
504 * The individual events are checked for in event.c.
506 w_global
.inotify
.wd_defaults
= inotify_add_watch(w_global
.inotify
.fd_event_queue
, watchPath
, IN_ALL_EVENTS
);
507 if (w_global
.inotify
.wd_defaults
< 0) {
508 wwarning(_("could not add an inotify watch on path %s."
509 " Changes to the defaults database will require"
510 " a restart to take effect."), watchPath
);
511 close(w_global
.inotify
.fd_event_queue
);
512 w_global
.inotify
.fd_event_queue
= -1;
517 #endif /* HAVE_INOTIFY */
519 static void execInitScript(void)
523 paths
= wstrconcat(wuserdatapath(), "/" PACKAGE_TARNAME
);
524 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
526 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
530 if (system(file
) != 0)
531 werror(_("%s:could not execute initialization script"), file
);
537 void ExecExitScript(void)
541 paths
= wstrconcat(wuserdatapath(), "/" PACKAGE_TARNAME
);
542 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
544 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
548 if (system(file
) != 0)
549 werror(_("%s:could not execute exit script"), file
);
555 int main(int argc
, char **argv
)
557 int i_am_the_monitor
, i
, len
;
560 memset(&w_global
, 0, sizeof(w_global
));
561 w_global
.program
.state
= WSTATE_NORMAL
;
562 w_global
.program
.signal_state
= WSTATE_NORMAL
;
563 w_global
.timestamp
.last_event
= CurrentTime
;
564 w_global
.timestamp
.focus_change
= CurrentTime
;
565 w_global
.ignore_workspace_change
= False
;
566 w_global
.shortcut
.modifiers_mask
= 0xff;
568 /* setup common stuff for the monitor and wmaker itself */
569 WMInitializeApplication("WindowMaker", &argc
, argv
);
571 memset(&wPreferences
, 0, sizeof(wPreferences
));
573 wPreferences
.fallbackWMs
= WMCreateArray(8);
574 alt
= getenv("WINDOWMAKER_ALT_WM");
576 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
578 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
579 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
580 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
581 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
582 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
583 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
584 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
586 i_am_the_monitor
= 1;
588 for (i
= 1; i
< argc
; i
++) {
589 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
590 i_am_the_monitor
= 0;
592 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
595 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
598 DisplayName
= argv
[i
];
602 DisplayName
= XDisplayName(DisplayName
);
603 len
= strlen(DisplayName
) + 64;
605 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
608 if (i_am_the_monitor
)
609 return MonitorLoop(argc
, argv
);
611 return real_main(argc
, argv
);
614 static int real_main(int argc
, char **argv
)
620 setlocale(LC_ALL
, "");
623 /* for telling WPrefs what's the name of the wmaker binary being ran */
624 setenv("WMAKER_BIN_NAME", argv
[0], 1);
627 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
628 for (i
= 0; i
< argc
; i
++) {
629 Arguments
[i
] = argv
[i
];
631 /* add the extra option to signal that we're just restarting wmaker */
632 Arguments
[argc
- 1] = "--for-real=";
633 Arguments
[argc
] = NULL
;
635 ProgName
= strrchr(argv
[0], '/');
642 for (i
= 1; i
< argc
; i
++) {
643 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
644 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv
[i
]);
645 } else if (strcmp(argv
[i
], "--for-real") == 0) {
646 wPreferences
.flags
.restarting
= 0;
647 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
648 wPreferences
.flags
.restarting
= 1;
649 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
650 wPreferences
.flags
.restarting
= 2;
651 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
652 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
653 wPreferences
.flags
.noautolaunch
= 1;
654 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
655 wPreferences
.flags
.norestore
= 1;
656 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
657 wPreferences
.flags
.nodock
= 1;
658 wPreferences
.flags
.nodrawer
= 1;
659 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
660 wPreferences
.flags
.noclip
= 1;
661 } else if (strcmp(argv
[i
], "-nodrawer") == 0 || strcmp(argv
[i
], "--no-drawer") == 0) {
662 wPreferences
.flags
.nodrawer
= 1;
663 #ifdef USE_ICCCM_WMREPLACE
664 } else if (strcmp(argv
[i
], "-replace") == 0 || strcmp(argv
[i
], "--replace") == 0) {
665 wPreferences
.flags
.replace
= 1;
667 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
668 printf("Window Maker %s\n", VERSION
);
670 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
671 printf("%s\n", PKGCONFDIR
);
673 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
676 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
679 w_global
.locale
= argv
[i
];
680 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
683 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
686 DisplayName
= argv
[i
];
687 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
690 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
693 if (initWVisualID(argv
[i
]) != 0) {
694 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
697 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0) {
698 wPreferences
.flags
.noupdates
= 1;
699 } else if (strcmp(argv
[i
], "--no-polling") == 0) {
701 wPreferences
.flags
.noupdates
= 1;
703 wmessage(_("your version of Window Maker was compiled with INotify support, so \"--no-polling\" has no effect"));
705 } else if (strcmp(argv
[i
], "--help") == 0) {
709 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
710 printf(_("Try '%s --help' for more information\n"), argv
[0]);
716 if (!wPreferences
.flags
.noupdates
) {
717 /* check existence of Defaults DB directory */
721 if (w_global
.locale
) {
722 setenv("LANG", w_global
.locale
, 1);
724 w_global
.locale
= getenv("LC_ALL");
725 if (!w_global
.locale
) {
726 w_global
.locale
= getenv("LANG");
730 setlocale(LC_ALL
, "");
732 if (!w_global
.locale
|| strcmp(w_global
.locale
, "C") == 0 || strcmp(w_global
.locale
, "POSIX") == 0)
733 w_global
.locale
= NULL
;
735 if (getenv("NLSPATH")) {
736 bindtextdomain("WindowMaker", getenv("NLSPATH"));
737 #if defined(MENU_TEXTDOMAIN)
738 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
741 bindtextdomain("WindowMaker", LOCALEDIR
);
742 #if defined(MENU_TEXTDOMAIN)
743 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
746 bind_textdomain_codeset("WindowMaker", "UTF-8");
747 #if defined(MENU_TEXTDOMAIN)
748 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
750 textdomain("WindowMaker");
752 if (!XSupportsLocale()) {
753 wwarning(_("X server does not support locale"));
756 if (XSetLocaleModifiers("") == NULL
) {
757 wwarning(_("cannot set locale modifiers"));
761 if (w_global
.locale
) {
764 w_global
.locale
= wstrdup(w_global
.locale
);
765 ptr
= strchr(w_global
.locale
, '.');
771 dpy
= XOpenDisplay(DisplayName
);
773 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
777 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
778 werror("error setting close-on-exec flag for X connection");
783 if (getWVisualID(0) < 0) {
785 * If unspecified, use default visual instead of waiting
786 * for wrlib/context.c:bestContext() that may end up choosing
787 * the "fake" 24 bits added by the Composite extension.
788 * This is required to avoid all sort of corruptions when
789 * composite is enabled, and at a depth other than 24.
791 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
794 /* check if the user specified a complete display name (with screen).
795 * If so, only manage the specified screen */
797 pos
= strchr(DisplayName
, ':');
801 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
804 DisplayName
= XDisplayName(DisplayName
);
805 setenv("DISPLAY", DisplayName
, 1);
807 wXModifierInitialize();
810 if (w_global
.screen_count
== 1)
815 inotifyWatchConfig();