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>
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 /****** Global Variables ******/
66 unsigned int ValidModMask
= 0xff;
70 /* locale to use. NULL==POSIX or C */
75 WPreferences wPreferences
;
78 WMPropList
*wDomainName
;
79 WMPropList
*wAttributeDomainName
;
81 WShortKey wKeyBindings
[WKBD_LAST
];
83 /* defaults domains */
84 WDDomain
*WDWindowMaker
= NULL
;
85 WDDomain
*WDWindowAttributes
= NULL
;
86 WDDomain
*WDRootMenu
= NULL
;
90 XContext wAppWinContext
;
91 XContext wStackContext
;
92 XContext wVEdgeContext
;
96 Atom _XA_WM_CHANGE_STATE
;
97 Atom _XA_WM_PROTOCOLS
;
98 Atom _XA_WM_TAKE_FOCUS
;
99 Atom _XA_WM_DELETE_WINDOW
;
100 Atom _XA_WM_SAVE_YOURSELF
;
101 Atom _XA_WM_CLIENT_LEADER
;
102 Atom _XA_WM_COLORMAP_WINDOWS
;
103 Atom _XA_WM_COLORMAP_NOTIFY
;
105 Atom _XA_GNUSTEP_WM_ATTR
;
106 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
107 Atom _XA_GNUSTEP_WM_RESIZEBAR
;
108 Atom _XA_GNUSTEP_TITLEBAR_STATE
;
110 Atom _XA_WINDOWMAKER_MENU
;
111 Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
112 Atom _XA_WINDOWMAKER_STATE
;
114 Atom _XA_WINDOWMAKER_WM_FUNCTION
;
115 Atom _XA_WINDOWMAKER_NOTICEBOARD
;
116 Atom _XA_WINDOWMAKER_COMMAND
;
118 Atom _XA_WINDOWMAKER_ICON_SIZE
;
119 Atom _XA_WINDOWMAKER_ICON_TILE
;
123 Cursor wCursor
[WCUR_LAST
];
125 /* last event timestamp for XSetInputFocus */
126 Time LastTimestamp
= CurrentTime
;
127 /* timestamp on the last time we did XSetInputFocus() */
128 Time LastFocusChange
= CurrentTime
;
131 Bool wShapeSupported
;
135 #ifdef KEEP_XKB_LOCK_STATUS
141 char WProgramSigState
= 0;
142 char WProgramState
= WSTATE_NORMAL
;
143 char WDelayedActionSet
= 0;
146 /* temporary stuff */
151 const char *WMNManaged
= "WMNManaged";
152 const char *WMNUnmanaged
= "WMNUnmanaged";
153 const char *WMNChangedWorkspace
= "WMNChangedWorkspace";
154 const char *WMNChangedState
= "WMNChangedState";
155 const char *WMNChangedFocus
= "WMNChangedFocus";
156 const char *WMNChangedStacking
= "WMNChangedStacking";
157 const char *WMNChangedName
= "WMNChangedName";
159 const char *WMNWorkspaceCreated
= "WMNWorkspaceCreated";
160 const char *WMNWorkspaceDestroyed
= "WMNWorkspaceDestroyed";
161 const char *WMNWorkspaceChanged
= "WMNWorkspaceChanged";
162 const char *WMNWorkspaceNameChanged
= "WMNWorkspaceNameChanged";
164 const char *WMNResetStacking
= "WMNResetStacking";
166 /******** End Global Variables *****/
168 static char *DisplayName
= NULL
;
170 static char **Arguments
;
174 extern void EventLoop();
175 extern void StartUp();
176 extern int MonitorLoop(int argc
, char **argv
);
178 static Bool multiHead
= True
;
180 static int real_main(int argc
, char **argv
);
186 wSessionDisconnectManager();
196 Restart(char *manager
, Bool abortOnFailure
)
199 char *argv
[MAX_RESTART_ARGS
];
202 if (manager
&& manager
[0]!=0) {
203 prog
= argv
[0] = strtok(manager
, " ");
204 for (i
=1; i
<MAX_RESTART_ARGS
; i
++) {
205 argv
[i
]=strtok(NULL
, " ");
213 wSessionDisconnectManager();
219 execvp(Arguments
[0], Arguments
);
220 wfatal(_("failed to restart Window Maker."));
223 wsyserror(_("could not exec %s"), prog
);
232 SetupEnvironment(WScreen
*scr
)
238 int len
= strlen(DisplayName
)+64;
240 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
241 ptr
= strchr(strchr(tmp
, ':'), '.');
244 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
249 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
250 scr
->rcontext
->attribs
->colors_per_channel
);
264 shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
*data
)
269 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
271 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
273 } else if (status
!= 127) {
275 printf("%s: %i\n", data->command, status);
279 wfree(data
->command
);
285 ExecuteShellCommand(WScreen
*scr
, char *command
)
287 static char *shell
= NULL
;
291 * This have a problem: if the shell is tcsh (not sure about others)
292 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
293 * will block and the command will not be executed.
295 shell = getenv("SHELL");
306 SetupEnvironment(scr
);
311 execl(shell
, shell
, "-c", command
, NULL
);
312 wsyserror("could not execute %s -c %s", shell
, command
);
314 } else if (pid
< 0) {
315 wsyserror("cannot fork a new process");
317 _tuple
*data
= wmalloc(sizeof(_tuple
));
320 data
->command
= wstrdup(command
);
322 wAddDeathHandler(pid
, (WDeathHandler
*)shellCommandHandler
, data
);
328 *---------------------------------------------------------------------
330 * Do a major cleanup and exit the program
332 *----------------------------------------------------------------------
335 wAbort(Bool dumpCore
)
340 for (i
=0; i
<wScreenCount
; i
++) {
341 scr
= wScreenWithNumber(i
);
345 printf(_("%s aborted.\n"), ProgName
);
356 printf(_("Usage: %s [options]\n"), ProgName
);
357 puts(_("The Window Maker window manager for the X window system"));
359 puts(_(" -display host:dpy display to use"));
361 puts(_(" --no-cpp disable preprocessing of configuration files"));
363 puts(_(" --no-dock do not open the application Dock"));
364 puts(_(" --no-clip do not open the workspace Clip"));
365 puts(_(" --no-autolaunch do not autolaunch applications"));
366 puts(_(" --dont-restore do not restore saved session"));
368 puts(_(" --locale locale locale to use"));
370 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
371 puts(_(" --visual-id visualid visual id of visual to use"));
372 puts(_(" --static do not update or save configurations"));
374 puts(_(" --synchronous turn on synchronous display mode"));
376 puts(_(" --version print version and exit"));
377 puts(_(" --help show this message"));
387 path
= wdefaultspathfordomain("WindowMaker");
389 if (access(path
, R_OK
)!=0) {
391 wfatal(_("could not find user GNUstep directory (%s).\n"
392 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
396 wwarning(_("could not find user GNUstep directory (%s)."), path
);
398 if (system("wmaker.inst --batch") != 0) {
399 wwarning(_("There was an error while creating GNUstep directory, please "
400 "make sure you have installed Window Maker correctly and run wmaker.inst"));
402 wwarning(_("%s directory created with default configuration."), path
);
412 * Add watch here, used to notify if configuration
413 * files have changed, using linux kernel inotify mechanism
419 char *watchPath
= NULL
;
420 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
422 wwarning(_("could not initialise an inotify instance."
423 " Changes to the defaults database will require"
424 " a restart to take effect. Check your kernel!"));
426 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
427 /* Add the watch; really we are only looking for modify events
428 * but we might want more in the future so check all events for now.
429 * The individual events are checked for in event.c.
431 inotifyWD
= inotify_add_watch (inotifyFD
, watchPath
, IN_ALL_EVENTS
);
433 wwarning(_("could not add an inotify watch on path\n."
435 "Changes to the defaults database will require"
436 " a restart to take effect."),watchPath
);
448 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
449 paths
= wstrappend(paths
, ":"DEF_CONFIG_PATHS
);
451 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
455 if (system(file
) != 0) {
456 wsyserror(_("%s:could not execute initialization script"), file
);
460 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
461 wsyserror(_("%s:could not execute initialization script"), file
);
475 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
476 paths
= wstrappend(paths
, ":"DEF_CONFIG_PATHS
);
478 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
482 if (system(file
) != 0) {
483 wsyserror(_("%s:could not execute exit script"), file
);
487 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
488 wsyserror(_("%s:could not execute exit script"), file
);
498 getFullPath(char *path
)
502 char *basep
= (char*)buffer
;
504 if (*path
!= '/' && getcwd(buffer
, 1023)) {
507 if (strncmp(path
, "../", 3)==0) {
509 basep
= strchr(basep
, '/');
510 if (!basep
|| *path
==0)
514 if (*path
== '/' || strncmp(path
, "./",2)==0) {
527 return wstrconcat(path
);
536 main(int argc
, char **argv
)
538 int i_am_the_monitor
, i
, len
;
541 /* setup common stuff for the monitor and wmaker itself */
542 WMInitializeApplication("WindowMaker", &argc
, argv
);
544 memset(&wPreferences
, 0, sizeof(WPreferences
));
546 wPreferences
.fallbackWMs
= WMCreateArray(8);
547 alt
= getenv("WINDOWMAKER_ALT_WM");
549 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
551 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
552 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
553 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
554 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
555 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
556 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
557 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
561 for (i
= 1; i
< argc
; i
++)
563 if (strncmp(argv
[i
], "--for-real", strlen("--for-real"))==0)
568 else if (strcmp(argv
[i
], "-display")==0 || strcmp(argv
[i
], "--display")==0)
572 wwarning(_("too few arguments for %s"), argv
[i
-1]);
575 DisplayName
= argv
[i
];
579 DisplayName
= XDisplayName(DisplayName
);
580 len
= strlen(DisplayName
)+64;
582 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
585 if (i_am_the_monitor
)
586 return MonitorLoop(argc
, argv
);
588 return real_main(argc
, argv
);
593 real_main(int argc
, char **argv
)
602 setlocale(LC_ALL
, "");
606 /* for telling WPrefs what's the name of the wmaker binary being ran */
607 setenv("WMAKER_BIN_NAME", argv
[0], 1);
611 Arguments
= wmalloc(sizeof(char*)*(ArgCount
+1));
612 for (i
= 0; i
< argc
; i
++)
614 Arguments
[i
]= argv
[i
];
616 /* add the extra option to signal that we're just restarting wmaker */
617 Arguments
[argc
-1]= "--for-real=";
618 Arguments
[argc
]= NULL
;
620 ProgName
= strrchr(argv
[0],'/');
630 for (i
=1; i
<argc
; i
++) {
632 if (strcmp(argv
[i
], "-nocpp")==0
633 || strcmp(argv
[i
], "--no-cpp")==0) {
634 wPreferences
.flags
.nocpp
=1;
637 if (strcmp(argv
[i
], "--for-real")==0) {
638 wPreferences
.flags
.restarting
= 0;
639 } else if (strcmp(argv
[i
], "--for-real=")==0) {
640 wPreferences
.flags
.restarting
= 1;
641 } else if (strcmp(argv
[i
], "--for-real-")==0) {
642 wPreferences
.flags
.restarting
= 2;
643 } else if (strcmp(argv
[i
], "-no-autolaunch")==0
644 || strcmp(argv
[i
], "--no-autolaunch")==0) {
645 wPreferences
.flags
.noautolaunch
= 1;
646 } else if (strcmp(argv
[i
], "-dont-restore")==0
647 || strcmp(argv
[i
], "--dont-restore")==0) {
648 wPreferences
.flags
.norestore
= 1;
649 } else if (strcmp(argv
[i
], "-nodock")==0
650 || strcmp(argv
[i
], "--no-dock")==0) {
651 wPreferences
.flags
.nodock
=1;
652 } else if (strcmp(argv
[i
], "-noclip")==0
653 || strcmp(argv
[i
], "--no-clip")==0) {
654 wPreferences
.flags
.noclip
=1;
655 } else if (strcmp(argv
[i
], "-version")==0
656 || strcmp(argv
[i
], "--version")==0) {
657 printf("Window Maker %s\n", VERSION
);
659 } else if (strcmp(argv
[i
], "--global_defaults_path")==0) {
660 printf("%s/WindowMaker\n", SYSCONFDIR
);
663 } else if (strcmp(argv
[i
], "--synchronous")==0) {
666 } else if (strcmp(argv
[i
], "-locale")==0
667 || strcmp(argv
[i
], "--locale")==0) {
670 wwarning(_("too few arguments for %s"), argv
[i
-1]);
674 } else if (strcmp(argv
[i
], "-display")==0
675 || strcmp(argv
[i
], "--display")==0) {
678 wwarning(_("too few arguments for %s"), argv
[i
-1]);
681 DisplayName
= argv
[i
];
682 } else if (strcmp(argv
[i
], "-visualid")==0
683 || strcmp(argv
[i
], "--visual-id")==0) {
686 wwarning(_("too few arguments for %s"), argv
[i
-1]);
689 if (sscanf(argv
[i
], "%i", &wVisualID
)!=1) {
690 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
693 } else if (strcmp(argv
[i
], "-static")==0
694 || strcmp(argv
[i
], "--static")==0) {
696 wPreferences
.flags
.noupdates
= 1;
698 } else if (strcmp(argv
[i
], "-clientid")==0
699 || strcmp(argv
[i
], "-restore")==0) {
702 wwarning(_("too few arguments for %s"), argv
[i
-1]);
706 } else if (strcmp(argv
[i
], "--help")==0) {
710 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
711 printf(_("Try '%s --help' for more information\n"), argv
[0]);
717 if (!wPreferences
.flags
.noupdates
) {
718 /* check existence of Defaults DB directory */
724 setenv("LANG", Locale
, 1);
726 Locale
= getenv("LC_ALL");
728 Locale
= getenv("LANG");
732 setlocale(LC_ALL
, "");
734 if (!Locale
|| strcmp(Locale
, "C")==0 || strcmp(Locale
, "POSIX")==0)
737 if (getenv("NLSPATH"))
738 bindtextdomain("WindowMaker", getenv("NLSPATH"));
740 bindtextdomain("WindowMaker", LOCALEDIR
);
741 bind_textdomain_codeset("WindowMaker", "UTF-8");
742 textdomain("WindowMaker");
744 if (!XSupportsLocale()) {
745 wwarning(_("X server does not support locale"));
748 if (XSetLocaleModifiers("") == NULL
) {
749 wwarning(_("cannot set locale modifiers"));
756 Locale
= wstrdup(Locale
);
757 ptr
= strchr(Locale
, '.');
763 dpy
= XOpenDisplay(DisplayName
);
765 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
769 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
770 wsyserror("error setting close-on-exec flag for X connection");
776 * If unspecified, use default visual instead of waiting
777 * for wrlib/context.c:bestContext() that may end up choosing
778 * the "fake" 24 bits added by the Composite extension.
779 * This is required to avoid all sort of corruptions when
780 * composite is enabled, and at a depth other than 24.
782 wVisualID
= (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);
799 XSynchronize(dpy
, True
);
802 wXModifierInitialize();
805 wSessionConnectManager(argv
, argc
);
814 inotifyWatchConfig();