fixed bug with icons of different sizes
[wmaker-crm.git] / src / main.c
blob2042cda6c10fea6a71a06c5f60bce45d4741d4ec
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 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 <proplist.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 proplist_t wDomainName;
75 proplist_t wAttributeDomainName;
77 WShortKey wKeyBindings[WKBD_LAST];
79 /* defaults domains */
80 WDDomain *WDWindowMaker = NULL;
81 WDDomain *WDRootMenu = NULL;
82 WDDomain *WDWindowAttributes = NULL;
85 /* XContexts */
86 XContext wWinContext;
87 XContext wAppWinContext;
88 XContext wStackContext;
90 /* Atoms */
91 Atom _XA_WM_STATE;
92 Atom _XA_WM_CHANGE_STATE;
93 Atom _XA_WM_PROTOCOLS;
94 Atom _XA_WM_TAKE_FOCUS;
95 Atom _XA_WM_DELETE_WINDOW;
96 Atom _XA_WM_SAVE_YOURSELF;
97 Atom _XA_WM_CLIENT_LEADER;
98 Atom _XA_WM_COLORMAP_WINDOWS;
99 Atom _XA_WM_COLORMAP_NOTIFY;
101 Atom _XA_GNUSTEP_WM_ATTR;
102 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
103 Atom _XA_GNUSTEP_WM_RESIZEBAR;
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 #ifdef OFFIX_DND
114 Atom _XA_DND_PROTOCOL;
115 Atom _XA_DND_SELECTION;
116 #endif
118 /* cursors */
119 Cursor wCursor[WCUR_LAST];
121 /* last event timestamp for XSetInputFocus */
122 Time LastTimestamp;
123 /* timestamp on the last time we did XSetInputFocus() */
124 Time LastFocusChange;
126 #ifdef SHAPE
127 Bool wShapeSupported;
128 int wShapeEventBase;
129 #endif
131 #ifdef KEEP_XKB_LOCK_STATUS
132 Bool wXkbSupported;
133 int wXkbEventBase;
134 #endif
136 /* special flags */
137 char WProgramState = WSTATE_NORMAL;
138 char WDelayedActionSet = 0;
140 /* temporary stuff */
141 int wVisualID = -1;
144 /******** End Global Variables *****/
146 static char *DisplayName = NULL;
148 static char **Arguments;
150 static int ArgCount;
152 extern void EventLoop();
153 extern void StartUp();
155 static Bool multiHead = True;
157 /* stdi/o for log shell */
158 static int LogStdIn = -1, LogStdOut = -1, LogStdErr = -1;
161 void
162 Exit(int status)
164 #ifdef XSMP_ENABLED
165 wSessionDisconnectManager();
166 #endif
167 if (dpy)
168 XCloseDisplay(dpy);
170 exit(status);
174 void
175 Restart(char *manager, Bool abortOnFailure)
177 char *prog=NULL;
178 char *argv[MAX_RESTART_ARGS];
179 int i;
181 if (manager && manager[0]!=0) {
182 prog = argv[0] = strtok(manager, " ");
183 for (i=1; i<MAX_RESTART_ARGS; i++) {
184 argv[i]=strtok(NULL, " ");
185 if (argv[i]==NULL) {
186 break;
190 if (dpy) {
191 #ifdef XSMP_ENABLED
192 wSessionDisconnectManager();
193 #endif
194 XCloseDisplay(dpy);
195 dpy = NULL;
197 if (!prog) {
198 execvp(Arguments[0], Arguments);
199 wfatal(_("failed to restart Window Maker."));
200 } else {
201 execvp(prog, argv);
202 wsyserror(_("could not exec %s"), prog);
204 if (abortOnFailure)
205 exit(-1);
210 void
211 SetupEnvironment(WScreen *scr)
213 char *tmp, *ptr;
214 char buf[16];
216 if (multiHead) {
217 tmp = wmalloc(strlen(DisplayName)+64);
218 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
219 ptr = strchr(strchr(tmp, ':'), '.');
220 if (ptr)
221 *ptr = 0;
222 sprintf(buf, ".%i", scr->screen);
223 strcat(tmp, buf);
224 putenv(tmp);
226 tmp = wmalloc(60);
227 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
228 scr->rcontext->attribs->colors_per_channel);
229 putenv(tmp);
235 typedef struct {
236 WScreen *scr;
237 char *command;
238 } _tuple;
241 static void
242 shellCommandHandler(pid_t pid, unsigned char status, _tuple *data)
244 if (status == 127) {
245 char *buffer;
247 buffer = wstrappend(_("Could not execute command: "), data->command);
249 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
250 free(buffer);
251 } else if (status != 127) {
253 printf("%s: %i\n", data->command, status);
257 free(data->command);
258 free(data);
262 void
263 ExecuteShellCommand(WScreen *scr, char *command)
265 static char *shell = NULL;
266 pid_t pid;
269 * This have a problem: if the shell is tcsh (not sure about others)
270 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
271 * will block and the command will not be executed.
272 if (!shell) {
273 shell = getenv("SHELL");
274 if (!shell)
275 shell = "/bin/sh";
278 shell = "/bin/sh";
280 pid = fork();
282 if (pid==0) {
284 SetupEnvironment(scr);
286 #ifdef HAVE_SETPGID
287 setpgid(0, 0);
288 #endif
289 execl(shell, shell, "-c", command, NULL);
290 wsyserror("could not execute %s -c %s", shell, command);
291 Exit(-1);
292 } else if (pid < 0) {
293 wsyserror("cannot fork a new process");
294 } else {
295 _tuple *data = wmalloc(sizeof(_tuple));
297 data->scr = scr;
298 data->command = wstrdup(command);
300 wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data);
306 *---------------------------------------------------------------------------
307 * StartLogShell
308 * Start a shell that will receive all stdin and stdout from processes
309 * forked by wmaker.
310 *---------------------------------------------------------------------------
312 void
313 StartLogShell(WScreen *scr)
315 int in_fd[2];
316 int out_fd[2];
317 int err_fd[2];
318 pid_t pid;
320 SetupEnvironment(scr);
322 if (pipe(in_fd) < 0) {
323 wsyserror("could not create pipe for log shell\n");
324 return;
326 if (pipe(out_fd) < 0) {
327 wsyserror("could not create pipe for log shell\n");
328 close(in_fd[0]);
329 close(in_fd[1]);
330 return;
332 if (pipe(err_fd) < 0) {
333 wsyserror("could not create pipe for log shell\n");
334 close(out_fd[0]);
335 close(out_fd[1]);
336 close(in_fd[0]);
337 close(in_fd[1]);
338 return;
341 pid = fork();
342 if (pid < 0) {
343 wsyserror("could not fork a new process for log shell\n");
344 return;
345 } else if (pid == 0) {
346 close(in_fd[0]);
347 close(out_fd[1]);
348 close(err_fd[1]);
350 close(0);
351 close(1);
352 close(2);
354 if (dup2(in_fd[1], 0) < 0) {
355 wsyserror("could not redirect stdin for log shell\n");
356 exit(1);
358 if (dup2(out_fd[1], 1) < 0) {
359 wsyserror("could not redirect stdout for log shell\n");
360 exit(1);
362 if (dup2(err_fd[1], 2) < 0) {
363 wsyserror("could not redirect stderr for log shell\n");
364 exit(1);
367 close(in_fd[1]);
368 close(out_fd[1]);
369 close(err_fd[1]);
371 execl("/bin/sh", "/bin/sh", "-c", wPreferences.logger_shell, NULL);
372 wsyserror("could not execute %s\n", wPreferences.logger_shell);
373 exit(1);
374 } else {
375 close(in_fd[1]);
376 close(out_fd[0]);
377 close(err_fd[0]);
379 LogStdIn = in_fd[1];
380 LogStdOut = out_fd[0];
381 LogStdErr = err_fd[0];
387 *---------------------------------------------------------------------
388 * wAbort--
389 * Do a major cleanup and exit the program
391 *----------------------------------------------------------------------
393 void
394 wAbort(Bool dumpCore)
396 int i;
397 WScreen *scr;
399 for (i=0; i<wScreenCount; i++) {
400 scr = wScreenWithNumber(i);
401 if (scr)
402 RestoreDesktop(scr);
404 printf(_("%s aborted.\n"), ProgName);
405 if (dumpCore)
406 abort();
407 else
408 exit(1);
412 void
413 print_help()
415 printf(_("Usage: %s [options]\n"), ProgName);
416 puts(_("The Window Maker window manager for the X window system"));
417 puts("");
418 puts(_(" -display host:dpy display to use"));
419 #ifdef USECPP
420 puts(_(" --no-cpp disable preprocessing of configuration files"));
421 #endif
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"));
426 puts(_(" --locale locale locale to use"));
428 puts(_(" --visual-id visualid visual id of visual to use"));
429 puts(_(" --static do not update or save configurations"));
430 #ifdef DEBUG
431 puts(_(" --synchronous turn on synchronous display mode"));
432 #endif
433 puts(_(" --version print version and exit"));
434 puts(_(" --help show this message"));
439 void
440 check_defaults()
442 char *path;
444 path = wdefaultspathfordomain("");
445 if (access(path, R_OK)!=0) {
446 wfatal(_("could not find user GNUstep directory (%s).\n"
447 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
448 path);
449 exit(1);
452 free(path);
456 static void
457 execInitScript()
459 char *file;
460 char *paths = wstrappend(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
462 file = wfindfile(paths, DEF_INIT_SCRIPT);
463 free(paths);
465 if (file) {
466 if (system(file) != 0) {
467 wsyserror(_("%s:could not execute initialization script"), file);
469 #if 0
470 if (fork()==0) {
471 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
472 wsyserror(_("%s:could not execute initialization script"), file);
473 exit(1);
475 #endif
476 free(file);
481 void
482 ExecExitScript()
484 char *file;
485 char *paths = wstrappend(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
487 file = wfindfile(paths, DEF_EXIT_SCRIPT);
488 free(paths);
490 if (file) {
491 if (system(file) != 0) {
492 wsyserror(_("%s:could not execute exit script"), file);
494 #if 0
495 if (fork()==0) {
496 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
497 wsyserror(_("%s:could not execute exit script"), file);
498 exit(1);
500 #endif
501 free(file);
505 #if 0
506 char*
507 getFullPath(char *path)
509 char buffer[1024];
510 char *tmp;
511 char *basep = (char*)buffer;
513 if (*path != '/' && getcwd(buffer, 1023)) {
515 for (;;) {
516 if (strncmp(path, "../", 3)==0) {
517 path += 3;
518 basep = strchr(basep, '/');
519 if (!basep || *path==0)
520 break;
523 if (*path == '/' || strncmp(path, "./",2)==0) {
524 tmp =
528 * path
529 * ./path
530 * ../path
531 * ../../path
535 } else {
536 return wstrappend(path);
539 return tmp;
541 #endif
544 main(int argc, char **argv)
546 int i, restart=0;
547 char *str;
548 int d, s;
549 #ifdef DEBUG
550 Bool doSync = False;
551 #endif
552 setlocale(LC_ALL, "");
554 wsetabort(wAbort);
556 /* for telling WPrefs what's the name of the wmaker binary being ran */
558 str = wstrappend("WMAKER_BIN_NAME=", argv[0]);
559 putenv(str);
561 ArgCount = argc;
562 Arguments = argv;
564 WMInitializeApplication("WindowMaker", &argc, argv);
567 ProgName = strrchr(argv[0],'/');
568 if (!ProgName)
569 ProgName = argv[0];
570 else
571 ProgName++;
574 restart = 0;
576 memset(&wPreferences, 0, sizeof(WPreferences));
578 if (argc>1) {
579 for (i=1; i<argc; i++) {
580 #ifdef USECPP
581 if (strcmp(argv[i], "-nocpp")==0
582 || strcmp(argv[i], "--no-cpp")==0) {
583 wPreferences.flags.nocpp=1;
584 } else
585 #endif
586 if (strcmp(argv[i], "-no-autolaunch")==0
587 || strcmp(argv[i], "--no-autolaunch")==0) {
588 wPreferences.flags.noautolaunch = 1;
589 } else if (strcmp(argv[i], "-nodock")==0
590 || strcmp(argv[i], "--no-dock")==0) {
591 wPreferences.flags.nodock=1;
592 } else if (strcmp(argv[i], "-noclip")==0
593 || strcmp(argv[i], "--no-clip")==0) {
594 wPreferences.flags.noclip=1;
595 } else if (strcmp(argv[i], "-version")==0
596 || strcmp(argv[i], "--version")==0) {
597 printf("Window Maker %s\n", VERSION);
598 exit(0);
599 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
600 printf("%s/WindowMaker\n", SYSCONFDIR);
601 exit(0);
602 #ifdef DEBUG
603 } else if (strcmp(argv[i], "--synchronous")==0) {
604 doSync = 1;
605 #endif
606 } else if (strcmp(argv[i], "-locale")==0
607 || strcmp(argv[i], "--locale")==0) {
608 i++;
609 if (i>=argc) {
610 wwarning(_("too few arguments for %s"), argv[i-1]);
611 exit(0);
613 Locale = argv[i];
614 } else if (strcmp(argv[i], "-display")==0) {
615 i++;
616 if (i>=argc) {
617 wwarning(_("too few arguments for %s"), argv[i-1]);
618 exit(0);
620 DisplayName = argv[i];
621 } else if (strcmp(argv[i], "-visualid")==0
622 || strcmp(argv[i], "--visual-id")==0) {
623 i++;
624 if (i>=argc) {
625 wwarning(_("too few arguments for %s"), argv[i-1]);
626 exit(0);
628 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
629 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
630 exit(0);
632 } else if (strcmp(argv[i], "-static")==0
633 || strcmp(argv[i], "--static")==0) {
635 wPreferences.flags.noupdates = 1;
636 #ifdef XSMP_ENABLED
637 } else if (strcmp(argv[i], "-clientid")==0
638 || strcmp(argv[i], "-restore")==0) {
639 i++;
640 if (i>=argc) {
641 wwarning(_("too few arguments for %s"), argv[i-1]);
642 exit(0);
644 #endif
645 } else if (strcmp(argv[i], "--help")==0) {
646 print_help();
647 exit(0);
648 } else {
649 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
650 printf(_("Try '%s --help' for more information\n"), argv[0]);
651 exit(1);
656 if (!wPreferences.flags.noupdates) {
657 /* check existence of Defaults DB directory */
658 check_defaults();
662 if (!Locale) {
663 Locale = getenv("LC_ALL");
665 if (!Locale) {
666 Locale = getenv("LANG");
669 setlocale(LC_ALL, "");
671 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
672 Locale = NULL;
673 #ifdef I18N
674 if (getenv("NLSPATH"))
675 bindtextdomain("WindowMaker", getenv("NLSPATH"));
676 else
677 bindtextdomain("WindowMaker", LOCALEDIR);
678 textdomain("WindowMaker");
680 if (!XSupportsLocale()) {
681 wwarning(_("X server does not support locale"));
684 if (XSetLocaleModifiers("") == NULL) {
685 wwarning(_("cannot set locale modifiers"));
687 #endif
689 if (Locale) {
690 char *ptr;
692 Locale = wstrdup(Locale);
693 ptr = strchr(Locale, '.');
694 if (ptr)
695 *ptr = 0;
698 /* open display */
699 dpy = XOpenDisplay(DisplayName);
700 if (dpy == NULL) {
701 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
702 exit(1);
705 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
706 wsyserror("error setting close-on-exec flag for X connection");
707 exit(1);
710 /* check if the user specified a complete display name (with screen).
711 * If so, only manage the specified screen */
712 if (DisplayName)
713 str = strchr(DisplayName, ':');
714 else
715 str = NULL;
717 if (str && sscanf(str, "%i.%i", &d, &s)==2)
718 multiHead = False;
720 DisplayName = XDisplayName(DisplayName);
721 str = wmalloc(strlen(DisplayName)+64);
722 sprintf(str, "DISPLAY=%s", DisplayName);
723 putenv(str);
725 #ifdef DEBUG
726 if (doSync)
727 XSynchronize(dpy, True);
728 #endif
730 wXModifierInitialize();
732 #ifdef XSMP_ENABLED
733 wSessionConnectManager(argv, argc);
734 #endif
736 StartUp(!multiHead);
738 if (wScreenCount==1)
739 multiHead = False;
741 execInitScript();
743 EventLoop();
744 return -1;