wmaker: Removed unused argument in function 'wDockFinishLaunch'
[wmaker-crm.git] / src / main.c
blob59a21f7a296ea0b7e8970c2c934678beb30ba797
1 /*
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.
21 #include "wconfig.h"
23 #ifdef HAVE_INOTIFY
24 #include <sys/inotify.h>
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <fcntl.h>
35 #include <X11/Xlib.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>
44 #define MAINFILE
46 #include "WindowMaker.h"
47 #include "window.h"
48 #include "defaults.h"
49 #include "event.h"
50 #include "startup.h"
51 #include "menu.h"
52 #include "keybind.h"
53 #include "xmodifier.h"
54 #include "session.h"
55 #include "shutdown.h"
56 #include "dialog.h"
57 #include "main.h"
58 #include "monitor.h"
60 #include <WINGs/WUtil.h>
62 #ifndef GLOBAL_DEFAULTS_SUBDIR
63 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
64 #endif
66 /****** Global Variables ******/
67 struct wmaker_global_variables w_global;
69 /* general info */
71 Display *dpy;
73 char *ProgName;
75 struct WPreferences wPreferences;
77 WShortKey wKeyBindings[WKBD_LAST];
79 /* notifications */
80 const char WMNManaged[] = "WMNManaged";
81 const char WMNUnmanaged[] = "WMNUnmanaged";
82 const char WMNChangedWorkspace[] = "WMNChangedWorkspace";
83 const char WMNChangedState[] = "WMNChangedState";
84 const char WMNChangedFocus[] = "WMNChangedFocus";
85 const char WMNChangedStacking[] = "WMNChangedStacking";
86 const char WMNChangedName[] = "WMNChangedName";
88 const char WMNWorkspaceCreated[] = "WMNWorkspaceCreated";
89 const char WMNWorkspaceDestroyed[] = "WMNWorkspaceDestroyed";
90 const char WMNWorkspaceChanged[] = "WMNWorkspaceChanged";
91 const char WMNWorkspaceNameChanged[] = "WMNWorkspaceNameChanged";
93 const char WMNResetStacking[] = "WMNResetStacking";
95 /******** End Global Variables *****/
97 static char *DisplayName = NULL;
99 static char **Arguments;
101 static int ArgCount;
103 static Bool multiHead = True;
105 static int *wVisualID = NULL;
106 static int wVisualID_len = 0;
108 static int real_main(int argc, char **argv);
110 int getWVisualID(int screen)
112 if (wVisualID == NULL)
113 return -1;
114 if (screen < 0 || screen >= wVisualID_len)
115 return -1;
117 return wVisualID[screen];
120 static void setWVisualID(int screen, int val)
122 int i;
124 if (screen < 0)
125 return;
127 if (wVisualID == NULL) {
128 /* no array at all, alloc space for screen + 1 entries
129 * and init with default value */
130 wVisualID_len = screen + 1;
131 wVisualID = (int *)malloc(wVisualID_len * sizeof(int));
132 for (i = 0; i < wVisualID_len; i++) {
133 wVisualID[i] = -1;
135 } else if (screen >= wVisualID_len) {
136 /* larger screen number than previously allocated
137 so enlarge array */
138 int oldlen = wVisualID_len;
140 wVisualID_len = screen + 1;
141 wVisualID = (int *)realloc(wVisualID, wVisualID_len * sizeof(int));
142 for (i = oldlen; i < wVisualID_len; i++) {
143 wVisualID[i] = -1;
147 wVisualID[screen] = val;
151 * this function splits a given string at the comma into tokens
152 * and set the wVisualID variable to each parsed number
154 static int initWVisualID(const char *user_str)
156 char *mystr = strdup(user_str);
157 int cur_in_pos = 0;
158 int cur_out_pos = 0;
159 int cur_screen = 0;
160 int error_found = 0;
162 for (;;) {
163 /* check for delimiter */
164 if (user_str[cur_in_pos] == '\0' || user_str[cur_in_pos] == ',') {
165 int v;
167 mystr[cur_out_pos] = '\0';
169 if (sscanf(mystr, "%i", &v) != 1) {
170 error_found = 1;
171 break;
174 setWVisualID(cur_screen, v);
176 cur_screen++;
177 cur_out_pos = 0;
180 /* break in case last char has been consumed */
181 if (user_str[cur_in_pos] == '\0')
182 break;
184 /* if the current char is no delimiter put it into mystr */
185 if (user_str[cur_in_pos] != ',') {
186 mystr[cur_out_pos++] = user_str[cur_in_pos];
188 cur_in_pos++;
191 free(mystr);
193 if (cur_screen == 0||error_found != 0)
194 return 1;
196 return 0;
199 noreturn void Exit(int status)
201 if (dpy)
202 XCloseDisplay(dpy);
204 exit(status);
207 void Restart(char *manager, Bool abortOnFailure)
209 char *prog = NULL;
210 char *argv[MAX_RESTART_ARGS];
211 int i;
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) {
218 break;
222 if (dpy) {
223 XCloseDisplay(dpy);
224 dpy = NULL;
226 if (!prog) {
227 execvp(Arguments[0], Arguments);
228 wfatal(_("failed to restart Window Maker."));
229 } else {
230 execvp(prog, argv);
231 werror(_("could not exec %s"), prog);
233 if (abortOnFailure)
234 exit(7);
237 void SetupEnvironment(WScreen * scr)
239 char *tmp, *ptr;
240 char buf[16];
242 if (multiHead) {
243 int len = strlen(DisplayName) + 64;
244 tmp = wmalloc(len);
245 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
246 ptr = strchr(strchr(tmp, ':'), '.');
247 if (ptr)
248 *ptr = 0;
249 snprintf(buf, sizeof(buf), ".%i", scr->screen);
250 strcat(tmp, buf);
251 putenv(tmp);
253 tmp = wmalloc(60);
254 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
255 scr->rcontext->attribs->colors_per_channel);
256 putenv(tmp);
259 typedef struct {
260 WScreen *scr;
261 char *command;
262 } _tuple;
264 static void shellCommandHandler(pid_t pid, unsigned int status, void *client_data)
266 _tuple *data = (_tuple *) client_data;
268 /* Parameter not used, but tell the compiler that it is ok */
269 (void) pid;
271 if (status == 127) {
272 char *buffer;
274 buffer = wstrconcat(_("Could not execute command: "), data->command);
276 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
277 wfree(buffer);
278 } else if (status != 127) {
280 printf("%s: %i\n", data->command, status);
284 wfree(data->command);
285 wfree(data);
288 void ExecuteShellCommand(WScreen *scr, const char *command)
290 static char *shell = NULL;
291 pid_t pid;
294 * This have a problem: if the shell is tcsh (not sure about others)
295 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
296 * will block and the command will not be executed.
297 if (!shell) {
298 shell = getenv("SHELL");
299 if (!shell)
300 shell = "/bin/sh";
303 shell = "/bin/sh";
305 pid = fork();
307 if (pid == 0) {
309 SetupEnvironment(scr);
311 #ifdef HAVE_SETSID
312 setsid();
313 #endif
314 execl(shell, shell, "-c", command, NULL);
315 werror("could not execute %s -c %s", shell, command);
316 Exit(-1);
317 } else if (pid < 0) {
318 werror("cannot fork a new process");
319 } else {
320 _tuple *data = wmalloc(sizeof(_tuple));
322 data->scr = scr;
323 data->command = wstrdup(command);
325 wAddDeathHandler(pid, shellCommandHandler, data);
330 *---------------------------------------------------------------------
331 * RelaunchWindow--
332 * Launch a new instance of the active window
334 *----------------------------------------------------------------------
336 Bool RelaunchWindow(WWindow *wwin)
338 if (! wwin || ! wwin->client_win) {
339 werror("no window to relaunch");
340 return False;
343 char **argv;
344 int argc;
346 if (! XGetCommand(dpy, wwin->client_win, &argv, &argc) || argc == 0 || argv == NULL) {
347 werror("cannot relaunch the application because no WM_COMMAND property is set");
348 return False;
351 pid_t pid = fork();
353 if (pid == 0) {
354 SetupEnvironment(wwin->screen_ptr);
355 #ifdef HAVE_SETSID
356 setsid();
357 #endif
358 /* argv is not null-terminated */
359 char **a = (char **) malloc(argc + 1);
360 if (! a) {
361 werror("out of memory trying to relaunch the application");
362 Exit(-1);
365 int i;
366 for (i = 0; i < argc; i++)
367 a[i] = argv[i];
368 a[i] = NULL;
370 execvp(a[0], a);
371 Exit(-1);
372 } else if (pid < 0) {
373 werror("cannot fork a new process");
375 XFreeStringList(argv);
376 return False;
377 } else {
378 _tuple *data = wmalloc(sizeof(_tuple));
380 data->scr = wwin->screen_ptr;
381 data->command = wtokenjoin(argv, argc);
383 /* not actually a shell command */
384 wAddDeathHandler(pid, shellCommandHandler, data);
386 XFreeStringList(argv);
389 return True;
393 *---------------------------------------------------------------------
394 * wAbort--
395 * Do a major cleanup and exit the program
397 *----------------------------------------------------------------------
399 noreturn void wAbort(Bool dumpCore)
401 int i;
402 WScreen *scr;
404 for (i = 0; i < w_global.screen_count; i++) {
405 scr = wScreenWithNumber(i);
406 if (scr)
407 RestoreDesktop(scr);
409 printf(_("%s aborted.\n"), ProgName);
410 if (dumpCore)
411 abort();
412 else
413 exit(1);
416 static void print_help(void)
418 printf(_("Usage: %s [options]\n"), ProgName);
419 puts(_("The Window Maker window manager for the X window system"));
420 puts("");
421 puts(_(" -display host:dpy display to use"));
422 puts(_(" --no-dock do not open the application Dock"));
423 puts(_(" --no-clip do not open the workspace Clip"));
424 puts(_(" --no-autolaunch do not autolaunch applications"));
425 puts(_(" --dont-restore do not restore saved session"));
427 puts(_(" --locale locale locale to use"));
429 puts(_(" --visual-id visualid visual id of visual to use"));
430 puts(_(" --static do not update or save configurations"));
431 #ifndef HAVE_INOTIFY
432 puts(_(" --no-polling do not periodically check for configuration updates"));
433 #endif
434 puts(_(" --version print version and exit"));
435 puts(_(" --help show this message"));
438 static void check_defaults(void)
440 char *path;
442 path = wdefaultspathfordomain("WindowMaker");
444 if (access(path, R_OK) != 0) {
445 wwarning(_("could not find user GNUstep directory (%s)."), path);
447 if (system("wmaker.inst --batch") != 0) {
448 wwarning(_("There was an error while creating GNUstep directory, please "
449 "make sure you have installed Window Maker correctly and run wmaker.inst"));
450 } else {
451 wwarning(_("%s directory created with default configuration."), path);
455 wfree(path);
458 #ifdef HAVE_INOTIFY
460 * Add watch here, used to notify if configuration
461 * files have changed, using linux kernel inotify mechanism
464 static void inotifyWatchConfig(void)
466 char *watchPath = NULL;
468 w_global.inotify.fd_event_queue = inotify_init(); /* Initialise an inotify instance */
469 if (w_global.inotify.fd_event_queue < 0) {
470 wwarning(_("could not initialise an inotify instance."
471 " Changes to the defaults database will require"
472 " a restart to take effect. Check your kernel!"));
473 } else {
474 watchPath = wstrconcat(wusergnusteppath(), "/Defaults");
475 /* Add the watch; really we are only looking for modify events
476 * but we might want more in the future so check all events for now.
477 * The individual events are checked for in event.c.
479 w_global.inotify.wd_defaults = inotify_add_watch(w_global.inotify.fd_event_queue, watchPath, IN_ALL_EVENTS);
480 if (w_global.inotify.wd_defaults < 0) {
481 wwarning(_("could not add an inotify watch on path %s."
482 "Changes to the defaults database will require"
483 " a restart to take effect."), watchPath);
484 close(w_global.inotify.fd_event_queue);
485 w_global.inotify.fd_event_queue = -1;
488 wfree(watchPath);
490 #endif /* HAVE_INOTIFY */
492 static void execInitScript(void)
494 char *file, *paths;
496 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
497 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
499 file = wfindfile(paths, DEF_INIT_SCRIPT);
500 wfree(paths);
502 if (file) {
503 if (system(file) != 0)
504 werror(_("%s:could not execute initialization script"), file);
506 wfree(file);
510 void ExecExitScript(void)
512 char *file, *paths;
514 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
515 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
517 file = wfindfile(paths, DEF_EXIT_SCRIPT);
518 wfree(paths);
520 if (file) {
521 if (system(file) != 0)
522 werror(_("%s:could not execute exit script"), file);
524 wfree(file);
528 int main(int argc, char **argv)
530 int i_am_the_monitor, i, len;
531 char *str, *alt;
533 memset(&w_global, 0, sizeof(w_global));
534 w_global.program.state = WSTATE_NORMAL;
535 w_global.program.signal_state = WSTATE_NORMAL;
536 w_global.timestamp.last_event = CurrentTime;
537 w_global.timestamp.focus_change = CurrentTime;
538 w_global.workspace.ignore_change = False;
539 w_global.shortcut.modifiers_mask = 0xff;
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");
548 if (alt != NULL)
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"));
559 i_am_the_monitor = 1;
561 for (i = 1; i < argc; i++) {
562 if (strncmp(argv[i], "--for-real", strlen("--for-real")) == 0) {
563 i_am_the_monitor = 0;
564 break;
565 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
566 i++;
567 if (i >= argc) {
568 wwarning(_("too few arguments for %s"), argv[i - 1]);
569 exit(0);
571 DisplayName = argv[i];
575 DisplayName = XDisplayName(DisplayName);
576 len = strlen(DisplayName) + 64;
577 str = wmalloc(len);
578 snprintf(str, len, "DISPLAY=%s", DisplayName);
579 putenv(str);
581 if (i_am_the_monitor)
582 return MonitorLoop(argc, argv);
583 else
584 return real_main(argc, argv);
587 static int real_main(int argc, char **argv)
589 int i;
590 char *pos;
591 int d, s;
593 setlocale(LC_ALL, "");
594 wsetabort(wAbort);
596 /* for telling WPrefs what's the name of the wmaker binary being ran */
597 setenv("WMAKER_BIN_NAME", argv[0], 1);
599 ArgCount = argc;
600 Arguments = wmalloc(sizeof(char *) * (ArgCount + 1));
601 for (i = 0; i < argc; i++) {
602 Arguments[i] = argv[i];
604 /* add the extra option to signal that we're just restarting wmaker */
605 Arguments[argc - 1] = "--for-real=";
606 Arguments[argc] = NULL;
608 ProgName = strrchr(argv[0], '/');
609 if (!ProgName)
610 ProgName = argv[0];
611 else
612 ProgName++;
614 if (argc > 1) {
615 for (i = 1; i < argc; i++) {
616 if (strcmp(argv[i], "-nocpp") == 0 || strcmp(argv[i], "--no-cpp") == 0) {
617 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv[i]);
618 } else if (strcmp(argv[i], "--for-real") == 0) {
619 wPreferences.flags.restarting = 0;
620 } else if (strcmp(argv[i], "--for-real=") == 0) {
621 wPreferences.flags.restarting = 1;
622 } else if (strcmp(argv[i], "--for-real-") == 0) {
623 wPreferences.flags.restarting = 2;
624 } else if (strcmp(argv[i], "-no-autolaunch") == 0
625 || strcmp(argv[i], "--no-autolaunch") == 0) {
626 wPreferences.flags.noautolaunch = 1;
627 } else if (strcmp(argv[i], "-dont-restore") == 0 || strcmp(argv[i], "--dont-restore") == 0) {
628 wPreferences.flags.norestore = 1;
629 } else if (strcmp(argv[i], "-nodock") == 0 || strcmp(argv[i], "--no-dock") == 0) {
630 wPreferences.flags.nodock = 1;
631 wPreferences.flags.nodrawer = 1;
632 } else if (strcmp(argv[i], "-noclip") == 0 || strcmp(argv[i], "--no-clip") == 0) {
633 wPreferences.flags.noclip = 1;
634 } else if (strcmp(argv[i], "-nodrawer") == 0 || strcmp(argv[i], "--no-drawer") == 0) {
635 wPreferences.flags.nodrawer = 1;
636 } else if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
637 printf("Window Maker %s\n", VERSION);
638 exit(0);
639 } else if (strcmp(argv[i], "--global_defaults_path") == 0) {
640 printf("%s/%s\n", SYSCONFDIR, GLOBAL_DEFAULTS_SUBDIR);
641 exit(0);
642 } else if (strcmp(argv[i], "-locale") == 0 || strcmp(argv[i], "--locale") == 0) {
643 i++;
644 if (i >= argc) {
645 wwarning(_("too few arguments for %s"), argv[i - 1]);
646 exit(0);
648 w_global.locale = argv[i];
649 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
650 i++;
651 if (i >= argc) {
652 wwarning(_("too few arguments for %s"), argv[i - 1]);
653 exit(0);
655 DisplayName = argv[i];
656 } else if (strcmp(argv[i], "-visualid") == 0 || strcmp(argv[i], "--visual-id") == 0) {
657 i++;
658 if (i >= argc) {
659 wwarning(_("too few arguments for %s"), argv[i - 1]);
660 exit(0);
662 if (initWVisualID(argv[i]) != 0) {
663 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
664 exit(0);
666 } else if (strcmp(argv[i], "-static") == 0 || strcmp(argv[i], "--static") == 0
667 #ifndef HAVE_INOTIFY
668 || strcmp(argv[i], "--no-polling") == 0
669 #endif
671 wPreferences.flags.noupdates = 1;
672 } else if (strcmp(argv[i], "--help") == 0) {
673 print_help();
674 exit(0);
675 } else {
676 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
677 printf(_("Try '%s --help' for more information\n"), argv[0]);
678 exit(1);
683 if (!wPreferences.flags.noupdates) {
684 /* check existence of Defaults DB directory */
685 check_defaults();
688 if (w_global.locale) {
689 setenv("LANG", w_global.locale, 1);
690 } else {
691 w_global.locale = getenv("LC_ALL");
692 if (!w_global.locale) {
693 w_global.locale = getenv("LANG");
697 setlocale(LC_ALL, "");
699 if (!w_global.locale || strcmp(w_global.locale, "C") == 0 || strcmp(w_global.locale, "POSIX") == 0)
700 w_global.locale = NULL;
701 #ifdef I18N
702 if (getenv("NLSPATH")) {
703 bindtextdomain("WindowMaker", getenv("NLSPATH"));
704 #if defined(MENU_TEXTDOMAIN)
705 bindtextdomain(MENU_TEXTDOMAIN, getenv("NLSPATH"));
706 #endif
707 } else {
708 bindtextdomain("WindowMaker", LOCALEDIR);
709 #if defined(MENU_TEXTDOMAIN)
710 bindtextdomain(MENU_TEXTDOMAIN, LOCALEDIR);
711 #endif
713 bind_textdomain_codeset("WindowMaker", "UTF-8");
714 #if defined(MENU_TEXTDOMAIN)
715 bind_textdomain_codeset(MENU_TEXTDOMAIN, "UTF-8");
716 #endif
717 textdomain("WindowMaker");
719 if (!XSupportsLocale()) {
720 wwarning(_("X server does not support locale"));
723 if (XSetLocaleModifiers("") == NULL) {
724 wwarning(_("cannot set locale modifiers"));
726 #endif
728 if (w_global.locale) {
729 char *ptr;
731 w_global.locale = wstrdup(w_global.locale);
732 ptr = strchr(w_global.locale, '.');
733 if (ptr)
734 *ptr = 0;
737 /* open display */
738 dpy = XOpenDisplay(DisplayName);
739 if (dpy == NULL) {
740 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
741 exit(1);
744 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
745 werror("error setting close-on-exec flag for X connection");
746 exit(1);
750 if (getWVisualID(0) < 0) {
752 * If unspecified, use default visual instead of waiting
753 * for wrlib/context.c:bestContext() that may end up choosing
754 * the "fake" 24 bits added by the Composite extension.
755 * This is required to avoid all sort of corruptions when
756 * composite is enabled, and at a depth other than 24.
758 setWVisualID(0, (int)DefaultVisual(dpy, DefaultScreen(dpy))->visualid);
761 /* check if the user specified a complete display name (with screen).
762 * If so, only manage the specified screen */
763 if (DisplayName)
764 pos = strchr(DisplayName, ':');
765 else
766 pos = NULL;
768 if (pos && sscanf(pos, ":%i.%i", &d, &s) == 2)
769 multiHead = False;
771 DisplayName = XDisplayName(DisplayName);
772 setenv("DISPLAY", DisplayName, 1);
774 wXModifierInitialize();
775 StartUp(!multiHead);
777 if (w_global.screen_count == 1)
778 multiHead = False;
780 execInitScript();
781 #ifdef HAVE_INOTIFY
782 inotifyWatchConfig();
783 #endif
784 EventLoop();
785 return -1;