fixed some memleaks (1 serious, 5 minor)
[wmaker-crm.git] / src / main.c
blob1eb46b50bc6cf2b983d5e568049249ca519d7014
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997-2002 Alfredo K. Kojima
5 *
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,
19 * USA.
22 #include "wconfig.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
36 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
37 * and if X_LOCALE is defined, X's locale emulating functions will be used.
38 * See Xlocale.h for more information.
40 #include <X11/Xlocale.h>
42 #define MAINFILE
44 #include "WindowMaker.h"
45 #include "window.h"
46 #include "funcs.h"
47 #include "menu.h"
48 #include "keybind.h"
49 #include "xmodifier.h"
50 #include "defaults.h"
51 #include "session.h"
52 #include "dialog.h"
54 #include <WINGs/WUtil.h>
56 /****** Global Variables ******/
58 /* general info */
60 Display *dpy;
62 char *ProgName;
64 unsigned int ValidModMask = 0xff;
66 /* locale to use. NULL==POSIX or C */
67 char *Locale=NULL;
69 int wScreenCount=0;
71 WPreferences wPreferences;
74 WMPropList *wDomainName;
75 WMPropList *wAttributeDomainName;
77 WShortKey wKeyBindings[WKBD_LAST];
79 /* defaults domains */
80 WDDomain *WDWindowMaker = NULL;
81 WDDomain *WDWindowAttributes = NULL;
82 WDDomain *WDRootMenu = NULL;
84 /* XContexts */
85 XContext wWinContext;
86 XContext wAppWinContext;
87 XContext wStackContext;
89 /* Atoms */
90 Atom _XA_WM_STATE;
91 Atom _XA_WM_CHANGE_STATE;
92 Atom _XA_WM_PROTOCOLS;
93 Atom _XA_WM_TAKE_FOCUS;
94 Atom _XA_WM_DELETE_WINDOW;
95 Atom _XA_WM_SAVE_YOURSELF;
96 Atom _XA_WM_CLIENT_LEADER;
97 Atom _XA_WM_COLORMAP_WINDOWS;
98 Atom _XA_WM_COLORMAP_NOTIFY;
100 Atom _XA_GNUSTEP_WM_ATTR;
101 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
102 Atom _XA_GNUSTEP_WM_RESIZEBAR;
103 Atom _XA_GNUSTEP_TITLEBAR_STATE;
105 Atom _XA_WINDOWMAKER_MENU;
106 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
107 Atom _XA_WINDOWMAKER_STATE;
109 Atom _XA_WINDOWMAKER_WM_FUNCTION;
110 Atom _XA_WINDOWMAKER_NOTICEBOARD;
111 Atom _XA_WINDOWMAKER_COMMAND;
113 Atom _XA_WINDOWMAKER_ICON_SIZE;
114 Atom _XA_WINDOWMAKER_ICON_TILE;
117 #ifdef OFFIX_DND
118 Atom _XA_DND_PROTOCOL;
119 Atom _XA_DND_SELECTION;
120 #endif
122 /* cursors */
123 Cursor wCursor[WCUR_LAST];
125 /* last event timestamp for XSetInputFocus */
126 Time LastTimestamp;
127 /* timestamp on the last time we did XSetInputFocus() */
128 Time LastFocusChange;
130 #ifdef SHAPE
131 Bool wShapeSupported;
132 int wShapeEventBase;
133 #endif
135 #ifdef KEEP_XKB_LOCK_STATUS
136 Bool wXkbSupported;
137 int wXkbEventBase;
138 #endif
140 /* special flags */
141 char WProgramSigState = 0;
142 char WProgramState = WSTATE_NORMAL;
143 char WDelayedActionSet = 0;
146 /* temporary stuff */
147 int wVisualID = -1;
151 /* notifications */
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;
173 static int ArgCount;
175 extern void EventLoop();
176 extern void StartUp();
178 static Bool multiHead = True;
180 /* stdi/o for log shell */
181 static int LogStdIn = -1, LogStdOut = -1, LogStdErr = -1;
184 void
185 Exit(int status)
187 #ifdef XSMP_ENABLED
188 wSessionDisconnectManager();
189 #endif
190 if (dpy)
191 XCloseDisplay(dpy);
193 exit(status);
197 void
198 Restart(char *manager, Bool abortOnFailure)
200 char *prog=NULL;
201 char *argv[MAX_RESTART_ARGS];
202 int i;
204 if (manager && manager[0]!=0) {
205 prog = argv[0] = strtok(manager, " ");
206 for (i=1; i<MAX_RESTART_ARGS; i++) {
207 argv[i]=strtok(NULL, " ");
208 if (argv[i]==NULL) {
209 break;
213 if (dpy) {
214 #ifdef XSMP_ENABLED
215 wSessionDisconnectManager();
216 #endif
217 XCloseDisplay(dpy);
218 dpy = NULL;
220 if (!prog) {
221 execvp(Arguments[0], Arguments);
222 wfatal(_("failed to restart Window Maker."));
223 } else {
224 execvp(prog, argv);
225 wsyserror(_("could not exec %s"), prog);
227 if (abortOnFailure)
228 exit(-1);
233 void
234 SetupEnvironment(WScreen *scr)
236 char *tmp, *ptr;
237 char buf[16];
239 if (multiHead) {
240 int len = strlen(DisplayName)+64;
241 tmp = wmalloc(len);
242 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
243 ptr = strchr(strchr(tmp, ':'), '.');
244 if (ptr)
245 *ptr = 0;
246 snprintf(buf, sizeof(buf), ".%i", scr->screen);
247 strcat(tmp, buf);
248 putenv(tmp);
249 wfree(tmp);
251 tmp = wmalloc(60);
252 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
253 scr->rcontext->attribs->colors_per_channel);
254 putenv(tmp);
255 wfree(tmp);
261 typedef struct {
262 WScreen *scr;
263 char *command;
264 } _tuple;
267 static void
268 shellCommandHandler(pid_t pid, unsigned char status, _tuple *data)
270 if (status == 127) {
271 char *buffer;
273 buffer = wstrconcat(_("Could not execute command: "), data->command);
275 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
276 wfree(buffer);
277 } else if (status != 127) {
279 printf("%s: %i\n", data->command, status);
283 wfree(data->command);
284 wfree(data);
288 void
289 ExecuteShellCommand(WScreen *scr, char *command)
291 static char *shell = NULL;
292 pid_t pid;
295 * This have a problem: if the shell is tcsh (not sure about others)
296 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
297 * will block and the command will not be executed.
298 if (!shell) {
299 shell = getenv("SHELL");
300 if (!shell)
301 shell = "/bin/sh";
304 shell = "/bin/sh";
306 pid = fork();
308 if (pid==0) {
310 SetupEnvironment(scr);
312 #ifdef HAVE_SETPGID
313 setpgid(0, 0);
314 #endif
315 execl(shell, shell, "-c", command, NULL);
316 wsyserror("could not execute %s -c %s", shell, command);
317 Exit(-1);
318 } else if (pid < 0) {
319 wsyserror("cannot fork a new process");
320 } else {
321 _tuple *data = wmalloc(sizeof(_tuple));
323 data->scr = scr;
324 data->command = wstrdup(command);
326 wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data);
332 *---------------------------------------------------------------------------
333 * StartLogShell
334 * Start a shell that will receive all stdin and stdout from processes
335 * forked by wmaker.
336 *---------------------------------------------------------------------------
338 void
339 StartLogShell(WScreen *scr)
341 int in_fd[2];
342 int out_fd[2];
343 int err_fd[2];
344 pid_t pid;
346 SetupEnvironment(scr);
348 if (pipe(in_fd) < 0) {
349 wsyserror("could not create pipe for log shell\n");
350 return;
352 if (pipe(out_fd) < 0) {
353 wsyserror("could not create pipe for log shell\n");
354 close(in_fd[0]);
355 close(in_fd[1]);
356 return;
358 if (pipe(err_fd) < 0) {
359 wsyserror("could not create pipe for log shell\n");
360 close(out_fd[0]);
361 close(out_fd[1]);
362 close(in_fd[0]);
363 close(in_fd[1]);
364 return;
367 pid = fork();
368 if (pid < 0) {
369 wsyserror("could not fork a new process for log shell\n");
370 return;
371 } else if (pid == 0) {
372 close(in_fd[0]);
373 close(out_fd[1]);
374 close(err_fd[1]);
376 close(0);
377 close(1);
378 close(2);
380 if (dup2(in_fd[1], 0) < 0) {
381 wsyserror("could not redirect stdin for log shell\n");
382 exit(1);
384 if (dup2(out_fd[1], 1) < 0) {
385 wsyserror("could not redirect stdout for log shell\n");
386 exit(1);
388 if (dup2(err_fd[1], 2) < 0) {
389 wsyserror("could not redirect stderr for log shell\n");
390 exit(1);
393 close(in_fd[1]);
394 close(out_fd[1]);
395 close(err_fd[1]);
397 execl("/bin/sh", "/bin/sh", "-c", wPreferences.logger_shell, NULL);
398 wsyserror("could not execute %s\n", wPreferences.logger_shell);
399 exit(1);
400 } else {
401 close(in_fd[1]);
402 close(out_fd[0]);
403 close(err_fd[0]);
405 LogStdIn = in_fd[1];
406 LogStdOut = out_fd[0];
407 LogStdErr = err_fd[0];
413 *---------------------------------------------------------------------
414 * wAbort--
415 * Do a major cleanup and exit the program
417 *----------------------------------------------------------------------
419 void
420 wAbort(Bool dumpCore)
422 int i;
423 WScreen *scr;
425 for (i=0; i<wScreenCount; i++) {
426 scr = wScreenWithNumber(i);
427 if (scr)
428 RestoreDesktop(scr);
430 printf(_("%s aborted.\n"), ProgName);
431 if (dumpCore)
432 abort();
433 else
434 exit(1);
438 void
439 print_help()
441 printf(_("Usage: %s [options]\n"), ProgName);
442 puts(_("The Window Maker window manager for the X window system"));
443 puts("");
444 puts(_(" -display host:dpy display to use"));
445 #ifdef USECPP
446 puts(_(" --no-cpp disable preprocessing of configuration files"));
447 #endif
448 puts(_(" --no-dock do not open the application Dock"));
449 puts(_(" --no-clip do not open the workspace Clip"));
450 puts(_(" --no-autolaunch do not autolaunch applications"));
451 puts(_(" --dont-restore do not restore saved session"));
453 puts(_(" --locale locale locale to use"));
455 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
456 puts(_(" --visual-id visualid visual id of visual to use"));
457 puts(_(" --static do not update or save configurations"));
458 puts(_(" --no-polling do not periodically check for configuration updates"));
459 #ifdef DEBUG
460 puts(_(" --synchronous turn on synchronous display mode"));
461 #endif
462 puts(_(" --version print version and exit"));
463 puts(_(" --help show this message"));
468 void
469 check_defaults()
471 char *path;
473 path = wdefaultspathfordomain("");
474 if (access(path, R_OK)!=0) {
475 #if 0
476 wfatal(_("could not find user GNUstep directory (%s).\n"
477 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
478 path);
479 exit(1);
480 #else
481 wwarning(_("could not find user GNUstep directory (%s)."), path);
483 if (system("wmaker.inst --batch") != 0) {
484 wwarning(_("There was an error while creating GNUstep directory, please "
485 "make sure you have installed Window Maker correctly and run wmaker.inst"));
486 } else {
487 wwarning(_("%s directory created with default configuration."), path);
489 #endif
492 wfree(path);
496 static void
497 execInitScript()
499 char *file;
500 char *paths = wstrconcat(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
502 file = wfindfile(paths, DEF_INIT_SCRIPT);
503 wfree(paths);
505 if (file) {
506 if (system(file) != 0) {
507 wsyserror(_("%s:could not execute initialization script"), file);
509 #if 0
510 if (fork()==0) {
511 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
512 wsyserror(_("%s:could not execute initialization script"), file);
513 exit(1);
515 #endif
516 wfree(file);
521 void
522 ExecExitScript()
524 char *file;
525 char *paths = wstrconcat(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
527 file = wfindfile(paths, DEF_EXIT_SCRIPT);
528 wfree(paths);
530 if (file) {
531 if (system(file) != 0) {
532 wsyserror(_("%s:could not execute exit script"), file);
534 #if 0
535 if (fork()==0) {
536 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
537 wsyserror(_("%s:could not execute exit script"), file);
538 exit(1);
540 #endif
541 wfree(file);
545 #if 0
546 char*
547 getFullPath(char *path)
549 char buffer[1024];
550 char *tmp;
551 char *basep = (char*)buffer;
553 if (*path != '/' && getcwd(buffer, 1023)) {
555 for (;;) {
556 if (strncmp(path, "../", 3)==0) {
557 path += 3;
558 basep = strchr(basep, '/');
559 if (!basep || *path==0)
560 break;
563 if (*path == '/' || strncmp(path, "./",2)==0) {
564 tmp =
568 * path
569 * ./path
570 * ../path
571 * ../../path
575 } else {
576 return wstrconcat(path);
579 return tmp;
581 #endif
584 main(int argc, char **argv)
586 int i, restart=0;
587 char *str, *alt;
588 int d, s;
589 #ifdef DEBUG
590 Bool doSync = False;
591 #endif
592 setlocale(LC_ALL, "");
594 wsetabort(wAbort);
596 /* for telling WPrefs what's the name of the wmaker binary being ran */
598 str = wstrconcat("WMAKER_BIN_NAME=", argv[0]);
599 putenv(str);
600 wfree(str);
602 ArgCount = argc;
603 Arguments = argv;
605 WMInitializeApplication("WindowMaker", &argc, argv);
608 ProgName = strrchr(argv[0],'/');
609 if (!ProgName)
610 ProgName = argv[0];
611 else
612 ProgName++;
615 restart = 0;
617 memset(&wPreferences, 0, sizeof(WPreferences));
619 wPreferences.fallbackWMs = WMCreateArray(8);
620 alt = getenv("WINDOWMAKER_ALT_WM");
621 if (alt != NULL)
622 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
624 WMAddToArray(wPreferences.fallbackWMs, wstrdup(FALLBACK_WINDOWMANAGER));
625 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
626 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
627 WMAddToArray(wPreferences.fallbackWMs, NULL);
628 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
629 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
631 if (argc>1) {
632 for (i=1; i<argc; i++) {
633 #ifdef USECPP
634 if (strcmp(argv[i], "-nocpp")==0
635 || strcmp(argv[i], "--no-cpp")==0) {
636 wPreferences.flags.nocpp=1;
637 } else
638 #endif
639 if (strcmp(argv[i], "-no-autolaunch")==0
640 || strcmp(argv[i], "--no-autolaunch")==0) {
641 wPreferences.flags.noautolaunch = 1;
642 } else if (strcmp(argv[i], "-dont-restore")==0
643 || strcmp(argv[i], "--dont-restore")==0) {
644 wPreferences.flags.norestore = 1;
645 } else if (strcmp(argv[i], "-nodock")==0
646 || strcmp(argv[i], "--no-dock")==0) {
647 wPreferences.flags.nodock=1;
648 } else if (strcmp(argv[i], "-noclip")==0
649 || strcmp(argv[i], "--no-clip")==0) {
650 wPreferences.flags.noclip=1;
651 } else if (strcmp(argv[i], "-version")==0
652 || strcmp(argv[i], "--version")==0) {
653 printf("Window Maker %s\n", VERSION);
654 exit(0);
655 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
656 printf("%s/WindowMaker\n", SYSCONFDIR);
657 exit(0);
658 #ifdef DEBUG
659 } else if (strcmp(argv[i], "--synchronous")==0) {
660 doSync = 1;
661 #endif
662 } else if (strcmp(argv[i], "-locale")==0
663 || strcmp(argv[i], "--locale")==0) {
664 i++;
665 if (i>=argc) {
666 wwarning(_("too few arguments for %s"), argv[i-1]);
667 exit(0);
669 Locale = argv[i];
670 } else if (strcmp(argv[i], "-display")==0
671 || strcmp(argv[i], "--display")==0) {
672 i++;
673 if (i>=argc) {
674 wwarning(_("too few arguments for %s"), argv[i-1]);
675 exit(0);
677 DisplayName = argv[i];
678 } else if (strcmp(argv[i], "-visualid")==0
679 || strcmp(argv[i], "--visual-id")==0) {
680 i++;
681 if (i>=argc) {
682 wwarning(_("too few arguments for %s"), argv[i-1]);
683 exit(0);
685 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
686 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
687 exit(0);
689 } else if (strcmp(argv[i], "-static")==0
690 || strcmp(argv[i], "--static")==0) {
692 wPreferences.flags.noupdates = 1;
693 } else if (strcmp(argv[i], "-nopolling")==0
694 || strcmp(argv[i], "--no-polling")==0) {
696 wPreferences.flags.nopolling = 1;
697 #ifdef XSMP_ENABLED
698 } else if (strcmp(argv[i], "-clientid")==0
699 || strcmp(argv[i], "-restore")==0) {
700 i++;
701 if (i>=argc) {
702 wwarning(_("too few arguments for %s"), argv[i-1]);
703 exit(0);
705 #endif
706 } else if (strcmp(argv[i], "--help")==0) {
707 print_help();
708 exit(0);
709 } else {
710 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
711 printf(_("Try '%s --help' for more information\n"), argv[0]);
712 exit(1);
717 if (!wPreferences.flags.noupdates) {
718 /* check existence of Defaults DB directory */
719 check_defaults();
723 if (!Locale) {
724 Locale = getenv("LC_ALL");
726 if (!Locale) {
727 Locale = getenv("LANG");
730 setlocale(LC_ALL, "");
732 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
733 Locale = NULL;
734 #ifdef I18N
735 if (getenv("NLSPATH"))
736 bindtextdomain("WindowMaker", getenv("NLSPATH"));
737 else
738 bindtextdomain("WindowMaker", LOCALEDIR);
739 textdomain("WindowMaker");
741 if (!XSupportsLocale()) {
742 wwarning(_("X server does not support locale"));
745 if (XSetLocaleModifiers("") == NULL) {
746 wwarning(_("cannot set locale modifiers"));
748 #endif
750 if (Locale) {
751 char *ptr;
753 Locale = wstrdup(Locale);
754 ptr = strchr(Locale, '.');
755 if (ptr)
756 *ptr = 0;
759 /* open display */
760 dpy = XOpenDisplay(DisplayName);
761 if (dpy == NULL) {
762 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
763 exit(1);
766 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
767 wsyserror("error setting close-on-exec flag for X connection");
768 exit(1);
771 /* check if the user specified a complete display name (with screen).
772 * If so, only manage the specified screen */
773 if (DisplayName)
774 str = strchr(DisplayName, ':');
775 else
776 str = NULL;
778 if (str && sscanf(str, ":%i.%i", &d, &s)==2)
779 multiHead = False;
781 DisplayName = XDisplayName(DisplayName);
783 int len = strlen(DisplayName)+64;
784 str = wmalloc(len);
785 snprintf(str, len, "DISPLAY=%s", DisplayName);
787 putenv(str);
788 wfree(str);
790 #ifdef DEBUG
791 if (doSync)
792 XSynchronize(dpy, True);
793 #endif
795 wXModifierInitialize();
797 #ifdef XSMP_ENABLED
798 wSessionConnectManager(argv, argc);
799 #endif
801 StartUp(!multiHead);
803 if (wScreenCount==1)
804 multiHead = False;
806 execInitScript();
808 EventLoop();
809 return -1;