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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 #include <sys/inotify.h>
31 #include <sys/types.h>
35 #include <X11/Xutil.h>
37 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
38 * and if X_LOCALE is defined, X's locale emulating functions will be used.
39 * See Xlocale.h for more information.
41 #include <X11/Xlocale.h>
45 #include "WindowMaker.h"
50 #include "xmodifier.h"
55 #include <WINGs/WUtil.h>
57 #ifndef GLOBAL_DEFAULTS_SUBDIR
58 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
61 /****** Global Variables ******/
69 unsigned int ValidModMask
= 0xff;
73 /* locale to use. NULL==POSIX or C */
78 WPreferences wPreferences
;
80 WMPropList
*wDomainName
;
81 WMPropList
*wAttributeDomainName
;
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;
148 /* temporary stuff */
152 const char *WMNManaged
= "WMNManaged";
153 const char *WMNUnmanaged
= "WMNUnmanaged";
154 const char *WMNChangedWorkspace
= "WMNChangedWorkspace";
155 const char *WMNChangedState
= "WMNChangedState";
156 const char *WMNChangedFocus
= "WMNChangedFocus";
157 const char *WMNChangedStacking
= "WMNChangedStacking";
158 const char *WMNChangedName
= "WMNChangedName";
160 const char *WMNWorkspaceCreated
= "WMNWorkspaceCreated";
161 const char *WMNWorkspaceDestroyed
= "WMNWorkspaceDestroyed";
162 const char *WMNWorkspaceChanged
= "WMNWorkspaceChanged";
163 const char *WMNWorkspaceNameChanged
= "WMNWorkspaceNameChanged";
165 const char *WMNResetStacking
= "WMNResetStacking";
167 /******** End Global Variables *****/
169 static char *DisplayName
= NULL
;
171 static char **Arguments
;
175 extern void EventLoop();
176 extern void StartUp();
177 extern int MonitorLoop(int argc
, char **argv
);
179 static Bool multiHead
= True
;
181 static int real_main(int argc
, char **argv
);
183 void Exit(int status
)
186 wSessionDisconnectManager();
194 void Restart(char *manager
, Bool abortOnFailure
)
197 char *argv
[MAX_RESTART_ARGS
];
200 if (manager
&& manager
[0] != 0) {
201 prog
= argv
[0] = strtok(manager
, " ");
202 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
203 argv
[i
] = strtok(NULL
, " ");
204 if (argv
[i
] == NULL
) {
211 wSessionDisconnectManager();
217 execvp(Arguments
[0], Arguments
);
218 wfatal(_("failed to restart Window Maker."));
221 wsyserror(_("could not exec %s"), prog
);
227 void SetupEnvironment(WScreen
* scr
)
233 int len
= strlen(DisplayName
) + 64;
235 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
236 ptr
= strchr(strchr(tmp
, ':'), '.');
239 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
244 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
245 scr
->rcontext
->attribs
->colors_per_channel
);
254 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
259 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
261 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
263 } else if (status
!= 127) {
265 printf("%s: %i\n", data->command, status);
269 wfree(data
->command
);
273 void ExecuteShellCommand(WScreen
* scr
, char *command
)
275 static char *shell
= NULL
;
279 * This have a problem: if the shell is tcsh (not sure about others)
280 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
281 * will block and the command will not be executed.
283 shell = getenv("SHELL");
294 SetupEnvironment(scr
);
299 execl(shell
, shell
, "-c", command
, NULL
);
300 wsyserror("could not execute %s -c %s", shell
, command
);
302 } else if (pid
< 0) {
303 wsyserror("cannot fork a new process");
305 _tuple
*data
= wmalloc(sizeof(_tuple
));
308 data
->command
= wstrdup(command
);
310 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
315 *---------------------------------------------------------------------
317 * Do a major cleanup and exit the program
319 *----------------------------------------------------------------------
321 void wAbort(Bool dumpCore
)
326 for (i
= 0; i
< wScreenCount
; i
++) {
327 scr
= wScreenWithNumber(i
);
331 printf(_("%s aborted.\n"), ProgName
);
340 printf(_("Usage: %s [options]\n"), ProgName
);
341 puts(_("The Window Maker window manager for the X window system"));
343 puts(_(" -display host:dpy display to use"));
345 puts(_(" --no-cpp disable preprocessing of configuration files"));
347 puts(_(" --no-dock do not open the application Dock"));
348 puts(_(" --no-clip do not open the workspace Clip"));
349 puts(_(" --no-autolaunch do not autolaunch applications"));
350 puts(_(" --dont-restore do not restore saved session"));
352 puts(_(" --locale locale locale to use"));
354 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
355 puts(_(" --visual-id visualid visual id of visual to use"));
356 puts(_(" --static do not update or save configurations"));
358 puts(_(" --synchronous turn on synchronous display mode"));
360 puts(_(" --version print version and exit"));
361 puts(_(" --help show this message"));
364 void check_defaults()
368 path
= wdefaultspathfordomain("WindowMaker");
370 if (access(path
, R_OK
) != 0) {
372 wfatal(_("could not find user GNUstep directory (%s).\n"
373 "Make sure you have installed Window Maker correctly and run wmaker.inst"), path
);
376 wwarning(_("could not find user GNUstep directory (%s)."), path
);
378 if (system("wmaker.inst --batch") != 0) {
379 wwarning(_("There was an error while creating GNUstep directory, please "
380 "make sure you have installed Window Maker correctly and run wmaker.inst"));
382 wwarning(_("%s directory created with default configuration."), path
);
391 * Add watch here, used to notify if configuration
392 * files have changed, using linux kernel inotify mechanism
395 static void inotifyWatchConfig()
397 char *watchPath
= NULL
;
398 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
400 wwarning(_("could not initialise an inotify instance."
401 " Changes to the defaults database will require"
402 " a restart to take effect. Check your kernel!"));
404 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
405 /* Add the watch; really we are only looking for modify events
406 * but we might want more in the future so check all events for now.
407 * The individual events are checked for in event.c.
409 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
411 wwarning(_("could not add an inotify watch on path\n."
413 "Changes to the defaults database will require"
414 " a restart to take effect."), watchPath
);
421 static void execInitScript()
425 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
426 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
428 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
432 if (system(file
) != 0) {
433 wsyserror(_("%s:could not execute initialization script"), file
);
437 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
438 wsyserror(_("%s:could not execute initialization script"), file
);
446 void ExecExitScript()
450 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
451 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
453 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
457 if (system(file
) != 0) {
458 wsyserror(_("%s:could not execute exit script"), file
);
462 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
463 wsyserror(_("%s:could not execute exit script"), file
);
472 char *getFullPath(char *path
)
476 char *basep
= (char *)buffer
;
478 if (*path
!= '/' && getcwd(buffer
, 1023)) {
481 if (strncmp(path
, "../", 3) == 0) {
483 basep
= strchr(basep
, '/');
484 if (!basep
|| *path
== 0)
488 if (*path
== '/' || strncmp(path
, "./", 2) == 0) {
499 return wstrconcat(path
);
506 int main(int argc
, char **argv
)
508 int i_am_the_monitor
, i
, len
;
511 /* setup common stuff for the monitor and wmaker itself */
512 WMInitializeApplication("WindowMaker", &argc
, argv
);
514 memset(&wPreferences
, 0, sizeof(WPreferences
));
516 wPreferences
.fallbackWMs
= WMCreateArray(8);
517 alt
= getenv("WINDOWMAKER_ALT_WM");
519 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
521 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
522 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
523 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
524 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
525 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
526 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
527 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
529 i_am_the_monitor
= 1;
531 for (i
= 1; i
< argc
; i
++) {
532 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
533 i_am_the_monitor
= 0;
535 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
538 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
541 DisplayName
= argv
[i
];
545 DisplayName
= XDisplayName(DisplayName
);
546 len
= strlen(DisplayName
) + 64;
548 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
551 if (i_am_the_monitor
)
552 return MonitorLoop(argc
, argv
);
554 return real_main(argc
, argv
);
557 static int real_main(int argc
, char **argv
)
566 setlocale(LC_ALL
, "");
570 /* for telling WPrefs what's the name of the wmaker binary being ran */
571 setenv("WMAKER_BIN_NAME", argv
[0], 1);
575 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
576 for (i
= 0; i
< argc
; i
++) {
577 Arguments
[i
] = argv
[i
];
579 /* add the extra option to signal that we're just restarting wmaker */
580 Arguments
[argc
- 1] = "--for-real=";
581 Arguments
[argc
] = NULL
;
583 ProgName
= strrchr(argv
[0], '/');
592 for (i
= 1; i
< argc
; i
++) {
594 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
595 wPreferences
.flags
.nocpp
= 1;
598 if (strcmp(argv
[i
], "--for-real") == 0) {
599 wPreferences
.flags
.restarting
= 0;
600 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
601 wPreferences
.flags
.restarting
= 1;
602 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
603 wPreferences
.flags
.restarting
= 2;
604 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
605 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
606 wPreferences
.flags
.noautolaunch
= 1;
607 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
608 wPreferences
.flags
.norestore
= 1;
609 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
610 wPreferences
.flags
.nodock
= 1;
611 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
612 wPreferences
.flags
.noclip
= 1;
613 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
614 printf("Window Maker %s\n", VERSION
);
616 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
617 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
620 } else if (strcmp(argv
[i
], "--synchronous") == 0) {
623 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
626 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
630 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
633 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
636 DisplayName
= argv
[i
];
637 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
640 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
643 if (sscanf(argv
[i
], "%i", &wVisualID
) != 1) {
644 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
647 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0) {
649 wPreferences
.flags
.noupdates
= 1;
651 } else if (strcmp(argv
[i
], "-clientid") == 0 || strcmp(argv
[i
], "-restore") == 0) {
654 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
658 } else if (strcmp(argv
[i
], "--help") == 0) {
662 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
663 printf(_("Try '%s --help' for more information\n"), argv
[0]);
669 if (!wPreferences
.flags
.noupdates
) {
670 /* check existence of Defaults DB directory */
675 setenv("LANG", Locale
, 1);
677 Locale
= getenv("LC_ALL");
679 Locale
= getenv("LANG");
683 setlocale(LC_ALL
, "");
685 if (!Locale
|| strcmp(Locale
, "C") == 0 || strcmp(Locale
, "POSIX") == 0)
688 if (getenv("NLSPATH"))
689 bindtextdomain("WindowMaker", getenv("NLSPATH"));
691 bindtextdomain("WindowMaker", LOCALEDIR
);
692 bind_textdomain_codeset("WindowMaker", "UTF-8");
693 textdomain("WindowMaker");
695 if (!XSupportsLocale()) {
696 wwarning(_("X server does not support locale"));
699 if (XSetLocaleModifiers("") == NULL
) {
700 wwarning(_("cannot set locale modifiers"));
707 Locale
= wstrdup(Locale
);
708 ptr
= strchr(Locale
, '.');
714 dpy
= XOpenDisplay(DisplayName
);
716 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
720 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
721 wsyserror("error setting close-on-exec flag for X connection");
727 * If unspecified, use default visual instead of waiting
728 * for wrlib/context.c:bestContext() that may end up choosing
729 * the "fake" 24 bits added by the Composite extension.
730 * This is required to avoid all sort of corruptions when
731 * composite is enabled, and at a depth other than 24.
733 wVisualID
= (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
;
735 /* check if the user specified a complete display name (with screen).
736 * If so, only manage the specified screen */
738 pos
= strchr(DisplayName
, ':');
742 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
745 DisplayName
= XDisplayName(DisplayName
);
746 setenv("DISPLAY", DisplayName
, 1);
750 XSynchronize(dpy
, True
);
753 wXModifierInitialize();
756 wSessionConnectManager(argv
, argc
);
761 if (wScreenCount
== 1)
765 inotifyWatchConfig();