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"
51 #include "xmodifier.h"
56 #include <WINGs/WUtil.h>
58 #ifndef GLOBAL_DEFAULTS_SUBDIR
59 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
62 /****** Global Variables ******/
70 unsigned int ValidModMask
= 0xff;
76 /* locale to use. NULL==POSIX or C */
81 WPreferences wPreferences
;
83 WShortKey wKeyBindings
[WKBD_LAST
];
85 /* defaults domains */
86 WDDomain
*WDWindowMaker
= NULL
;
87 WDDomain
*WDWindowAttributes
= NULL
;
88 WDDomain
*WDRootMenu
= NULL
;
92 XContext wAppWinContext
;
93 XContext wStackContext
;
94 XContext wVEdgeContext
;
98 Atom _XA_WM_CHANGE_STATE
;
99 Atom _XA_WM_PROTOCOLS
;
100 Atom _XA_WM_TAKE_FOCUS
;
101 Atom _XA_WM_DELETE_WINDOW
;
102 Atom _XA_WM_SAVE_YOURSELF
;
103 Atom _XA_WM_CLIENT_LEADER
;
104 Atom _XA_WM_COLORMAP_WINDOWS
;
105 Atom _XA_WM_COLORMAP_NOTIFY
;
107 Atom _XA_GNUSTEP_WM_ATTR
;
108 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
109 Atom _XA_GNUSTEP_WM_RESIZEBAR
;
110 Atom _XA_GNUSTEP_TITLEBAR_STATE
;
112 Atom _XA_WINDOWMAKER_MENU
;
113 Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
114 Atom _XA_WINDOWMAKER_STATE
;
116 Atom _XA_WINDOWMAKER_WM_FUNCTION
;
117 Atom _XA_WINDOWMAKER_NOTICEBOARD
;
118 Atom _XA_WINDOWMAKER_COMMAND
;
120 Atom _XA_WINDOWMAKER_ICON_SIZE
;
121 Atom _XA_WINDOWMAKER_ICON_TILE
;
123 Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
126 Cursor wCursor
[WCUR_LAST
];
128 /* last event timestamp for XSetInputFocus */
129 Time LastTimestamp
= CurrentTime
;
130 /* timestamp on the last time we did XSetInputFocus() */
131 Time LastFocusChange
= CurrentTime
;
134 Bool wShapeSupported
;
138 #ifdef KEEP_XKB_LOCK_STATUS
144 char WProgramSigState
= 0;
145 char WProgramState
= WSTATE_NORMAL
;
146 char WDelayedActionSet
= 0;
149 const char *WMNManaged
= "WMNManaged";
150 const char *WMNUnmanaged
= "WMNUnmanaged";
151 const char *WMNChangedWorkspace
= "WMNChangedWorkspace";
152 const char *WMNChangedState
= "WMNChangedState";
153 const char *WMNChangedFocus
= "WMNChangedFocus";
154 const char *WMNChangedStacking
= "WMNChangedStacking";
155 const char *WMNChangedName
= "WMNChangedName";
157 const char *WMNWorkspaceCreated
= "WMNWorkspaceCreated";
158 const char *WMNWorkspaceDestroyed
= "WMNWorkspaceDestroyed";
159 const char *WMNWorkspaceChanged
= "WMNWorkspaceChanged";
160 const char *WMNWorkspaceNameChanged
= "WMNWorkspaceNameChanged";
162 const char *WMNResetStacking
= "WMNResetStacking";
164 /******** End Global Variables *****/
166 static char *DisplayName
= NULL
;
168 static char **Arguments
;
172 static Bool multiHead
= True
;
174 static int *wVisualID
= NULL
;
175 static int wVisualID_len
= 0;
177 static int real_main(int argc
, char **argv
);
179 int getWVisualID(int screen
)
181 if (wVisualID
== NULL
)
183 if (screen
< 0 || screen
>= wVisualID_len
)
186 return wVisualID
[screen
];
189 static void setWVisualID(int screen
, int val
)
196 if (wVisualID
== NULL
) {
197 /* no array at all, alloc space for screen + 1 entries
198 * and init with default value */
199 wVisualID_len
= screen
+ 1;
200 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
201 for (i
= 0; i
< wVisualID_len
; i
++) {
204 } else if (screen
>= wVisualID_len
) {
205 /* larger screen number than previously allocated
207 int oldlen
= wVisualID_len
;
209 wVisualID_len
= screen
+ 1;
210 wVisualID
= (int *)realloc(wVisualID
, wVisualID_len
* sizeof(int));
211 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
216 wVisualID
[screen
] = val
;
220 * this function splits a given string at the comma into tokens
221 * and set the wVisualID variable to each parsed number
223 static int initWVisualID(const char *user_str
)
225 char *mystr
= strdup(user_str
);
232 /* check for delimiter */
233 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
236 mystr
[cur_out_pos
] = '\0';
238 if (sscanf(mystr
, "%i", &v
) != 1) {
243 setWVisualID(cur_screen
, v
);
249 /* break in case last char has been consumed */
250 if (user_str
[cur_in_pos
] == '\0')
253 /* if the current char is no delimiter put it into mystr */
254 if (user_str
[cur_in_pos
] != ',') {
255 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
262 if (cur_screen
== 0||error_found
!= 0)
268 void Exit(int status
)
276 void Restart(char *manager
, Bool abortOnFailure
)
279 char *argv
[MAX_RESTART_ARGS
];
282 if (manager
&& manager
[0] != 0) {
283 prog
= argv
[0] = strtok(manager
, " ");
284 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
285 argv
[i
] = strtok(NULL
, " ");
286 if (argv
[i
] == NULL
) {
296 execvp(Arguments
[0], Arguments
);
297 wfatal(_("failed to restart Window Maker."));
300 werror(_("could not exec %s"), prog
);
306 void SetupEnvironment(WScreen
* scr
)
312 int len
= strlen(DisplayName
) + 64;
314 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
315 ptr
= strchr(strchr(tmp
, ':'), '.');
318 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
323 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
324 scr
->rcontext
->attribs
->colors_per_channel
);
333 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
338 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
340 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
342 } else if (status
!= 127) {
344 printf("%s: %i\n", data->command, status);
348 wfree(data
->command
);
352 void ExecuteShellCommand(WScreen
* scr
, char *command
)
354 static char *shell
= NULL
;
358 * This have a problem: if the shell is tcsh (not sure about others)
359 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
360 * will block and the command will not be executed.
362 shell = getenv("SHELL");
373 SetupEnvironment(scr
);
378 execl(shell
, shell
, "-c", command
, NULL
);
379 werror("could not execute %s -c %s", shell
, command
);
381 } else if (pid
< 0) {
382 werror("cannot fork a new process");
384 _tuple
*data
= wmalloc(sizeof(_tuple
));
387 data
->command
= wstrdup(command
);
389 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
394 *---------------------------------------------------------------------
396 * Launch a new instance of the active window
398 *----------------------------------------------------------------------
400 Bool
RelaunchWindow(WWindow
*wwin
)
402 if (! wwin
|| ! wwin
->client_win
) {
403 werror("no window to relaunch");
410 if (! XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
== 0 || argv
== NULL
) {
411 werror("cannot relaunch the application because no WM_COMMAND property is set");
418 SetupEnvironment(wwin
->screen_ptr
);
422 /* argv is not null-terminated */
423 char **a
= (char **) malloc(argc
+ 1);
425 werror("out of memory trying to relaunch the application");
430 for (i
= 0; i
< argc
; i
++)
436 } else if (pid
< 0) {
437 werror("cannot fork a new process");
439 XFreeStringList(argv
);
442 _tuple
*data
= wmalloc(sizeof(_tuple
));
444 data
->scr
= wwin
->screen_ptr
;
445 data
->command
= wtokenjoin(argv
, argc
);
447 /* not actually a shell command */
448 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
450 XFreeStringList(argv
);
457 *---------------------------------------------------------------------
459 * Do a major cleanup and exit the program
461 *----------------------------------------------------------------------
463 void wAbort(Bool dumpCore
)
468 for (i
= 0; i
< wScreenCount
; i
++) {
469 scr
= wScreenWithNumber(i
);
473 printf(_("%s aborted.\n"), ProgName
);
480 static void print_help(void)
482 printf(_("Usage: %s [options]\n"), ProgName
);
483 puts(_("The Window Maker window manager for the X window system"));
485 puts(_(" -display host:dpy display to use"));
486 puts(_(" --no-dock do not open the application Dock"));
487 puts(_(" --no-clip do not open the workspace Clip"));
488 puts(_(" --no-autolaunch do not autolaunch applications"));
489 puts(_(" --dont-restore do not restore saved session"));
491 puts(_(" --locale locale locale to use"));
493 puts(_(" --visual-id visualid visual id of visual to use"));
494 puts(_(" --static do not update or save configurations"));
496 puts(_(" --no-polling do not periodically check for configuration updates"));
498 puts(_(" --version print version and exit"));
499 puts(_(" --help show this message"));
502 static void check_defaults(void)
506 path
= wdefaultspathfordomain("WindowMaker");
508 if (access(path
, R_OK
) != 0) {
509 wwarning(_("could not find user GNUstep directory (%s)."), path
);
511 if (system("wmaker.inst --batch") != 0) {
512 wwarning(_("There was an error while creating GNUstep directory, please "
513 "make sure you have installed Window Maker correctly and run wmaker.inst"));
515 wwarning(_("%s directory created with default configuration."), path
);
524 * Add watch here, used to notify if configuration
525 * files have changed, using linux kernel inotify mechanism
528 static void inotifyWatchConfig(void)
530 char *watchPath
= NULL
;
531 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
533 wwarning(_("could not initialise an inotify instance."
534 " Changes to the defaults database will require"
535 " a restart to take effect. Check your kernel!"));
537 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
538 /* Add the watch; really we are only looking for modify events
539 * but we might want more in the future so check all events for now.
540 * The individual events are checked for in event.c.
542 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
544 wwarning(_("could not add an inotify watch on path %s."
545 "Changes to the defaults database will require"
546 " a restart to take effect."), watchPath
);
552 #endif /* HAVE_INOTIFY */
554 static void execInitScript(void)
558 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
559 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
561 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
565 if (system(file
) != 0)
566 werror(_("%s:could not execute initialization script"), file
);
572 void ExecExitScript(void)
576 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
577 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
579 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
583 if (system(file
) != 0)
584 werror(_("%s:could not execute exit script"), file
);
590 int main(int argc
, char **argv
)
592 int i_am_the_monitor
, i
, len
;
595 /* setup common stuff for the monitor and wmaker itself */
596 WMInitializeApplication("WindowMaker", &argc
, argv
);
598 memset(&wPreferences
, 0, sizeof(WPreferences
));
600 wPreferences
.fallbackWMs
= WMCreateArray(8);
601 alt
= getenv("WINDOWMAKER_ALT_WM");
603 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
605 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
606 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
607 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
608 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
609 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
610 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
611 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
613 i_am_the_monitor
= 1;
615 for (i
= 1; i
< argc
; i
++) {
616 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
617 i_am_the_monitor
= 0;
619 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
622 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
625 DisplayName
= argv
[i
];
629 DisplayName
= XDisplayName(DisplayName
);
630 len
= strlen(DisplayName
) + 64;
632 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
635 if (i_am_the_monitor
)
636 return MonitorLoop(argc
, argv
);
638 return real_main(argc
, argv
);
641 static int real_main(int argc
, char **argv
)
647 setlocale(LC_ALL
, "");
650 /* for telling WPrefs what's the name of the wmaker binary being ran */
651 setenv("WMAKER_BIN_NAME", argv
[0], 1);
654 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
655 for (i
= 0; i
< argc
; i
++) {
656 Arguments
[i
] = argv
[i
];
658 /* add the extra option to signal that we're just restarting wmaker */
659 Arguments
[argc
- 1] = "--for-real=";
660 Arguments
[argc
] = NULL
;
662 ProgName
= strrchr(argv
[0], '/');
669 for (i
= 1; i
< argc
; i
++) {
670 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
671 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv
[i
]);
672 } else if (strcmp(argv
[i
], "--for-real") == 0) {
673 wPreferences
.flags
.restarting
= 0;
674 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
675 wPreferences
.flags
.restarting
= 1;
676 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
677 wPreferences
.flags
.restarting
= 2;
678 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
679 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
680 wPreferences
.flags
.noautolaunch
= 1;
681 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
682 wPreferences
.flags
.norestore
= 1;
683 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
684 wPreferences
.flags
.nodock
= 1;
685 wPreferences
.flags
.nodrawer
= 1;
686 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
687 wPreferences
.flags
.noclip
= 1;
688 } else if (strcmp(argv
[i
], "-nodrawer") == 0 || strcmp(argv
[i
], "--no-drawer") == 0) {
689 wPreferences
.flags
.nodrawer
= 1;
690 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
691 printf("Window Maker %s\n", VERSION
);
693 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
694 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
696 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
699 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
703 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
706 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
709 DisplayName
= argv
[i
];
710 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
713 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
716 if (initWVisualID(argv
[i
]) != 0) {
717 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
720 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
722 || strcmp(argv
[i
], "--no-polling") == 0
725 wPreferences
.flags
.noupdates
= 1;
726 } else if (strcmp(argv
[i
], "--help") == 0) {
730 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
731 printf(_("Try '%s --help' for more information\n"), argv
[0]);
737 if (!wPreferences
.flags
.noupdates
) {
738 /* check existence of Defaults DB directory */
743 setenv("LANG", Locale
, 1);
745 Locale
= getenv("LC_ALL");
747 Locale
= getenv("LANG");
751 setlocale(LC_ALL
, "");
753 if (!Locale
|| strcmp(Locale
, "C") == 0 || strcmp(Locale
, "POSIX") == 0)
756 if (getenv("NLSPATH")) {
757 bindtextdomain("WindowMaker", getenv("NLSPATH"));
758 #if defined(MENU_TEXTDOMAIN)
759 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
762 bindtextdomain("WindowMaker", LOCALEDIR
);
763 #if defined(MENU_TEXTDOMAIN)
764 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
767 bind_textdomain_codeset("WindowMaker", "UTF-8");
768 #if defined(MENU_TEXTDOMAIN)
769 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
771 textdomain("WindowMaker");
773 if (!XSupportsLocale()) {
774 wwarning(_("X server does not support locale"));
777 if (XSetLocaleModifiers("") == NULL
) {
778 wwarning(_("cannot set locale modifiers"));
785 Locale
= wstrdup(Locale
);
786 ptr
= strchr(Locale
, '.');
792 dpy
= XOpenDisplay(DisplayName
);
794 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
798 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
799 werror("error setting close-on-exec flag for X connection");
804 if (getWVisualID(0) < 0) {
806 * If unspecified, use default visual instead of waiting
807 * for wrlib/context.c:bestContext() that may end up choosing
808 * the "fake" 24 bits added by the Composite extension.
809 * This is required to avoid all sort of corruptions when
810 * composite is enabled, and at a depth other than 24.
812 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
815 /* check if the user specified a complete display name (with screen).
816 * If so, only manage the specified screen */
818 pos
= strchr(DisplayName
, ':');
822 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
825 DisplayName
= XDisplayName(DisplayName
);
826 setenv("DISPLAY", DisplayName
, 1);
828 wXModifierInitialize();
831 if (wScreenCount
== 1)
836 inotifyWatchConfig();