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,
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
37 #include <WINGs/WUtil.h>
40 #include "WindowMaker.h"
50 #include "xmodifier.h"
52 /**** global variables *****/
54 extern WPreferences wPreferences;
56 extern Time LastTimestamp;
59 static void putdef(char *line, char *name, char *value)
62 wwarning(_("could not define value for %s for cpp"), name);
69 static void putidef(char *line, char *name, int value)
72 snprintf(tmp, sizeof(tmp), "%i", value);
77 static char *username()
85 user = getpwuid(getuid());
87 wsyserror(_("could not get password entry for UID %i"), getuid());
99 char *MakeCPPArgs(char *path)
102 char buffer[MAXLINE], *buf, *line;
106 line = wmalloc(MAXLINE);
109 if ((buf = getenv("HOSTNAME")) != NULL) {
111 wwarning(_("your machine is misconfigured. HOSTNAME is set to %s"), buf);
113 putdef(line, " -DHOST=", buf);
114 } else if ((buf = getenv("HOST")) != NULL) {
116 wwarning(_("your machine is misconfigured. HOST is set to %s"), buf);
118 putdef(line, " -DHOST=", buf);
122 putdef(line, " -DUSER=", buf);
123 putidef(line, " -DUID=", getuid());
124 buf = XDisplayName(DisplayString(dpy));
125 putdef(line, " -DDISPLAY=", buf);
126 putdef(line, " -DWM_VERSION=", VERSION);
128 visual = DefaultVisual(dpy, DefaultScreen(dpy));
129 putidef(line, " -DVISUAL=", visual->class);
131 putidef(line, " -DDEPTH=", DefaultDepth(dpy, DefaultScreen(dpy)));
133 putidef(line, " -DSCR_WIDTH=", WidthOfScreen(DefaultScreenOfDisplay(dpy)));
134 putidef(line, " -DSCR_HEIGHT=", HeightOfScreen(DefaultScreenOfDisplay(dpy)));
136 /* put the dir where the menu is being read from to the
140 buf = strchr(tmp + 1, ' ');
144 buf = strrchr(tmp, '/');
146 *buf = 0; /* trunc filename */
147 putdef(line, " -I", tmp);
152 /* this should be done just once, but it works this way */
153 strcpy(buffer, DEF_CONFIG_PATHS);
154 buf = strtok(buffer, ":");
157 char fullpath[MAXLINE];
160 strcpy(fullpath, buf);
163 /* home is statically allocated. Don't free it! */
164 char *home = wgethomedir();
166 strcpy(fullpath, home);
167 strcat(fullpath, &(buf[1]));
170 putdef(line, " -I", fullpath);
172 } while ((buf = strtok(NULL, ":")) != NULL);
185 * Is win2 below win1?
187 static Bool isBelow(WWindow * win1, WWindow * win2)
192 tmp = win1->frame->core->stacking->under;
194 if (tmp == win2->frame->core)
196 tmp = tmp->stacking->under;
199 for (i = win1->frame->core->stacking->window_level - 1; i >= 0; i--) {
200 tmp = win1->screen_ptr->stacking_list[i];
202 if (tmp == win2->frame->core)
204 tmp = tmp->stacking->under;
215 Bool wFetchName(dpy, win, winname)
220 XTextProperty text_prop;
224 if (XGetWMName(dpy, win, &text_prop)) {
225 if (text_prop.value && text_prop.nitems > 0) {
226 if (text_prop.encoding == XA_STRING) {
227 *winname = wstrdup((char *)text_prop.value);
228 XFree(text_prop.value);
230 text_prop.nitems = strlen((char *)text_prop.value);
231 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >=
232 Success && num > 0 && *list) {
233 XFree(text_prop.value);
234 *winname = wstrdup(*list);
235 XFreeStringList(list);
237 *winname = wstrdup((char *)text_prop.value);
238 XFree(text_prop.value);
242 /* the title is set, but it was set to none */
243 *winname = wstrdup("");
247 /* the hint is probably not set */
255 * XGetIconName Wrapper
259 Bool wGetIconName(dpy, win, iconname)
264 XTextProperty text_prop;
268 if (XGetWMIconName(dpy, win, &text_prop) != 0 && text_prop.value && text_prop.nitems > 0) {
269 if (text_prop.encoding == XA_STRING)
270 *iconname = (char *)text_prop.value;
272 text_prop.nitems = strlen((char *)text_prop.value);
273 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >= Success && num > 0 && *list) {
274 XFree(text_prop.value);
275 *iconname = wstrdup(*list);
276 XFreeStringList(list);
278 *iconname = (char *)text_prop.value;
286 static void eatExpose()
290 /* compress all expose events into a single one */
292 if (XCheckMaskEvent(dpy, ExposureMask, &event)) {
293 /* ignore other exposure events for this window */
294 while (XCheckWindowEvent(dpy, event.xexpose.window, ExposureMask, &foo)) ;
295 /* eat exposes for other windows */
298 event.xexpose.count = 0;
299 XPutBackEvent(dpy, &event);
303 void SlideWindow(Window win, int from_x, int from_y, int to_x, int to_y)
305 time_t time0 = time(NULL);
306 float dx, dy, x = from_x, y = from_y, sx, sy, px, py;
307 int dx_is_bigger = 0;
309 /* animation parameters */
316 ICON_SLIDE_DELAY_UF, ICON_SLIDE_STEPS_UF, ICON_SLIDE_SLOWDOWN_UF}, {
317 ICON_SLIDE_DELAY_F, ICON_SLIDE_STEPS_F, ICON_SLIDE_SLOWDOWN_F}, {
318 ICON_SLIDE_DELAY_M, ICON_SLIDE_STEPS_M, ICON_SLIDE_SLOWDOWN_M}, {
319 ICON_SLIDE_DELAY_S, ICON_SLIDE_STEPS_S, ICON_SLIDE_SLOWDOWN_S}, {
320 ICON_SLIDE_DELAY_US, ICON_SLIDE_STEPS_US, ICON_SLIDE_SLOWDOWN_US}};
322 dx = (float)(to_x - from_x);
323 dy = (float)(to_y - from_y);
324 sx = (dx == 0 ? 0 : fabs(dx) / dx);
325 sy = (dy == 0 ? 0 : fabs(dy) / dy);
327 if (fabs(dx) > fabs(dy)) {
332 px = dx / apars[(int)wPreferences.icon_slide_speed].slowdown;
333 if (px < apars[(int)wPreferences.icon_slide_speed].steps && px > 0)
334 px = apars[(int)wPreferences.icon_slide_speed].steps;
335 else if (px > -apars[(int)wPreferences.icon_slide_speed].steps && px < 0)
336 px = -apars[(int)wPreferences.icon_slide_speed].steps;
337 py = (sx == 0 ? 0 : px * dy / dx);
339 py = dy / apars[(int)wPreferences.icon_slide_speed].slowdown;
340 if (py < apars[(int)wPreferences.icon_slide_speed].steps && py > 0)
341 py = apars[(int)wPreferences.icon_slide_speed].steps;
342 else if (py > -apars[(int)wPreferences.icon_slide_speed].steps && py < 0)
343 py = -apars[(int)wPreferences.icon_slide_speed].steps;
344 px = (sy == 0 ? 0 : py * dx / dy);
347 while (x != to_x || y != to_y) {
350 if ((px < 0 && (int)x < to_x) || (px > 0 && (int)x > to_x))
352 if ((py < 0 && (int)y < to_y) || (py > 0 && (int)y > to_y))
356 px = px * (1.0 - 1 / (float)apars[(int)wPreferences.icon_slide_speed].slowdown);
357 if (px < apars[(int)wPreferences.icon_slide_speed].steps && px > 0)
358 px = apars[(int)wPreferences.icon_slide_speed].steps;
359 else if (px > -apars[(int)wPreferences.icon_slide_speed].steps && px < 0)
360 px = -apars[(int)wPreferences.icon_slide_speed].steps;
361 py = (sx == 0 ? 0 : px * dy / dx);
363 py = py * (1.0 - 1 / (float)apars[(int)wPreferences.icon_slide_speed].slowdown);
364 if (py < apars[(int)wPreferences.icon_slide_speed].steps && py > 0)
365 py = apars[(int)wPreferences.icon_slide_speed].steps;
366 else if (py > -apars[(int)wPreferences.icon_slide_speed].steps && py < 0)
367 py = -apars[(int)wPreferences.icon_slide_speed].steps;
368 px = (sy == 0 ? 0 : py * dx / dy);
371 XMoveWindow(dpy, win, (int)x, (int)y);
373 if (apars[(int)wPreferences.icon_slide_speed].delay > 0) {
374 wusleep(apars[(int)wPreferences.icon_slide_speed].delay * 1000L);
378 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
381 XMoveWindow(dpy, win, to_x, to_y);
384 /* compress expose events */
388 char *ShrinkString(WMFont * font, char *string, int width)
397 w = WMWidthOfString(font, string, p);
398 text = wmalloc(strlen(string) + 8);
399 strcpy(text, string);
403 pos = strchr(text, ' ');
405 pos = strchr(text, ':');
410 w1 = WMWidthOfString(font, text, p);
427 width -= WMWidthOfString(font, "...", 3);
432 while (p2 > p1 && p1 != t) {
433 w = WMWidthOfString(font, &string[p - t], t);
436 t = p1 + (p2 - p1) / 2;
437 } else if (w < width) {
439 t = p1 + (p2 - p1) / 2;
443 strcat(text, &string[p - p1]);
448 char *FindImage(char *paths, char *file)
452 tmp = strrchr(file, ':');
455 path = wfindfile(paths, file);
459 path = wfindfile(paths, file);
465 static void timeoutHandler(void *data)
470 static char *getTextSelection(WScreen * screen, Atom selection)
504 data = XFetchBuffer(dpy, &size, buffer);
511 unsigned long len, bytes;
515 static Atom clipboard = 0;
518 clipboard = XInternAtom(dpy, "CLIPBOARD", False);
520 XDeleteProperty(dpy, screen->info_window, clipboard);
522 XConvertSelection(dpy, selection, XA_STRING, clipboard, screen->info_window, CurrentTime);
524 timer = WMAddTimerHandler(1000, timeoutHandler, &timeout);
526 while (!XCheckTypedWindowEvent(dpy, screen->info_window, SelectionNotify, &ev) && !timeout) ;
529 WMDeleteTimerHandler(timer);
531 wwarning("selection retrieval timed out");
535 /* nobody owns the selection or the current owner has
536 * nothing to do with what we need */
537 if (ev.xselection.property == None) {
541 if (XGetWindowProperty(dpy, screen->info_window,
543 False, XA_STRING, &rtype, &bits, &len,
544 &bytes, (unsigned char **)&data) != Success) {
547 if (rtype != XA_STRING || bits != 8) {
548 wwarning("invalid data in text selection");
557 static char *getselection(WScreen * scr)
561 tmp = getTextSelection(scr, XA_PRIMARY);
563 tmp = getTextSelection(scr, XA_CUT_BUFFER0);
568 parseuserinputpart(char *line, int *ptr, char *endchars)
570 int depth = 0, begin;
574 while(line[*ptr] != '\0') {
575 if(line[*ptr] == '(') {
577 } else if(depth > 0 && line[*ptr] == ')') {
579 } else if(depth == 0 && strchr(endchars, line[*ptr]) != NULL) {
580 value = wmalloc(*ptr - begin + 1);
581 strncpy(value, line + begin, *ptr - begin);
582 value[*ptr - begin] = '\0';
592 getuserinput(WScreen *scr, char *line, int *ptr, Bool advanced)
594 char *ret = NULL, *title = NULL, *prompt = NULL, *name = NULL;
597 if(line[*ptr] == '(')
598 title = parseuserinputpart(line, ptr, ",)");
599 if(title != NULL && line[*ptr] == ',')
600 prompt = parseuserinputpart(line, ptr, ",)");
601 if(prompt != NULL && line[*ptr] == ',')
602 name = parseuserinputpart(line, ptr, ")");
605 rv = wAdvancedInputDialog(scr,
606 title ? gettext(title):_("Program Arguments"),
607 prompt ? gettext(prompt):_("Enter command arguments:"),
610 rv = wInputDialog(scr,
611 title ? gettext(title):_("Program Arguments"),
612 prompt ? gettext(prompt):_("Enter command arguments:"),
615 if(title) wfree(title);
616 if(prompt) wfree(prompt);
617 if(name) wfree(name);
619 return rv ? ret : NULL;
627 * state input new-state output
628 * NORMAL % OPTION <nil>
629 * NORMAL \ ESCAPE <nil>
630 * NORMAL etc. NORMAL <input>
631 * ESCAPE any NORMAL <input>
632 * OPTION s NORMAL <selection buffer>
633 * OPTION w NORMAL <selected window id>
634 * OPTION a NORMAL <input text>
635 * OPTION d NORMAL <OffiX DND selection object>
636 * OPTION W NORMAL <current workspace>
637 * OPTION etc. NORMAL %<input>
639 #define TMPBUFSIZE 64
640 char *ExpandOptions(WScreen * scr, char *cmdline)
642 int ptr, optr, state, len, olen;
644 char *selection = NULL;
645 char *user_input = NULL;
647 char *dropped_thing = NULL;
649 char tmpbuf[TMPBUFSIZE];
652 len = strlen(cmdline);
656 wwarning(_("out of memory during expansion of \"%s\""));
660 ptr = 0; /* input line pointer */
661 optr = 0; /* output line pointer */
666 switch (cmdline[ptr]) {
675 out[optr++] = cmdline[ptr];
680 switch (cmdline[ptr]) {
694 out[optr++] = cmdline[ptr];
700 switch (cmdline[ptr]) {
702 if (scr->focused_window && scr->focused_window->flags.focused) {
703 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x",
704 (unsigned int)scr->focused_window->client_win);
705 slen = strlen(tmpbuf);
707 nout = realloc(out, olen);
709 wwarning(_("out of memory during expansion of \"%w\""));
721 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x", (unsigned int)scr->current_workspace + 1);
722 slen = strlen(tmpbuf);
724 nout = realloc(out, olen);
726 wwarning(_("out of memory during expansion of \"%W\""));
737 user_input = getuserinput(scr, cmdline, &ptr, cmdline[ptr-1] == 'A');
739 slen = strlen(user_input);
741 nout = realloc(out, olen);
743 wwarning(_("out of memory during expansion of \"%a\""));
747 strcat(out, user_input);
750 /* Not an error, but user has Canceled the dialog box.
751 * This will make the command to not be performed. */
758 if (scr->xdestring) {
759 dropped_thing = wstrdup(scr->xdestring);
761 if (!dropped_thing) {
762 dropped_thing = get_dnd_selection(scr);
764 if (!dropped_thing) {
765 scr->flags.dnd_data_convertion_status = 1;
768 slen = strlen(dropped_thing);
770 nout = realloc(out, olen);
772 wwarning(_("out of memory during expansion of \"%d\""));
776 strcat(out, dropped_thing);
783 selection = getselection(scr);
786 wwarning(_("selection not available"));
789 slen = strlen(selection);
791 nout = realloc(out, olen);
793 wwarning(_("out of memory during expansion of \"%s\""));
797 strcat(out, selection);
803 out[optr++] = cmdline[ptr];
821 void ParseWindowName(WMPropList * value, char **winstance, char **wclass, char *where)
825 *winstance = *wclass = NULL;
827 if (!WMIsPLString(value)) {
828 wwarning(_("bad window name value in %s state info"), where);
832 name = WMGetFromPLString(value);
833 if (!name || strlen(name) == 0) {
834 wwarning(_("bad window name value in %s state info"), where);
838 UnescapeWM_CLASS(name, winstance, wclass);
842 static char *keysymToString(KeySym keysym, unsigned int state)
845 char *buf = wmalloc(20);
850 kev.send_event = False;
851 kev.window = DefaultRootWindow(dpy);
852 kev.root = DefaultRootWindow(dpy);
853 kev.same_screen = True;
854 kev.subwindow = kev.root;
855 kev.serial = 0x12344321;
856 kev.time = CurrentTime;
858 kev.keycode = XKeysymToKeycode(dpy, keysym);
859 count = XLookupString(&kev, buf, 19, NULL, NULL);
866 char *GetShortcutString(char *text)
875 tmp = text = wstrdup(text);
878 while ((k = strchr(text, '+')) != NULL) {
882 mod = wXModifierFromKey(text);
884 return wstrdup("bug");
889 if (strcasecmp(text, "Meta") == 0) {
890 buffer = wstrappend(buffer, "M+");
891 } else if (strcasecmp(text, "Alt") == 0) {
892 buffer = wstrappend(buffer, "A+");
893 } else if (strcasecmp(text, "Shift") == 0) {
894 buffer = wstrappend(buffer, "Sh+");
895 } else if (strcasecmp(text, "Mod1") == 0) {
896 buffer = wstrappend(buffer, "M1+");
897 } else if (strcasecmp(text, "Mod2") == 0) {
898 buffer = wstrappend(buffer, "M2+");
899 } else if (strcasecmp(text, "Mod3") == 0) {
900 buffer = wstrappend(buffer, "M3+");
901 } else if (strcasecmp(text, "Mod4") == 0) {
902 buffer = wstrappend(buffer, "M4+");
903 } else if (strcasecmp(text, "Mod5") == 0) {
904 buffer = wstrappend(buffer, "M5+");
905 } else if (strcasecmp(text, "Control") == 0) {
908 buffer = wstrappend(buffer, text);
914 buffer = wstrappend(buffer, "^");
916 buffer = wstrappend(buffer, text);
919 /* ksym = XStringToKeysym(text);
920 tmp = keysymToString(ksym, modmask);
922 buffer = wstrappend(buffer, tmp);
929 char *EscapeWM_CLASS(char *name, char *class)
932 char *ename = NULL, *eclass = NULL;
940 ename = wmalloc(l * 2 + 1);
942 for (i = 0; i < l; i++) {
943 if (name[i] == '\\') {
945 } else if (name[i] == '.') {
948 ename[j++] = name[i];
954 eclass = wmalloc(l * 2 + 1);
956 for (i = 0; i < l; i++) {
957 if (class[i] == '\\') {
959 } else if (class[i] == '.') {
962 eclass[j++] = class[i];
967 if (ename && eclass) {
968 int len = strlen(ename) + strlen(eclass) + 4;
970 snprintf(ret, len, "%s.%s", ename, eclass);
974 ret = wstrdup(ename);
977 ret = wstrdup(eclass);
984 void UnescapeWM_CLASS(char *str, char **name, char **class)
994 /* separate string in 2 parts */
996 for (i = 0; i < j; i++) {
997 if (str[i] == '\\') {
1000 } else if (str[i] == '.') {
1006 /* unescape strings */
1007 for (i = 0, k = 0; i < dot; i++) {
1008 if (str[i] == '\\') {
1011 (*name)[k++] = str[i];
1016 for (i = dot + 1, k = 0; i < j; i++) {
1017 if (str[i] == '\\') {
1020 (*class)[k++] = str[i];
1035 void SendHelperMessage(WScreen * scr, char type, int workspace, char *msg)
1042 if (!scr->flags.backimage_helper_launched) {
1046 len = (msg ? strlen(msg) : 0) + (workspace >= 0 ? 4 : 0) + 1;
1047 buffer = wmalloc(len + 5);
1048 snprintf(buf, sizeof(buf), "%4i", len);
1049 memcpy(buffer, buf, 4);
1052 if (workspace >= 0) {
1053 snprintf(buf, sizeof(buf), "%4i", workspace);
1054 memcpy(&buffer[i], buf, 4);
1059 strcpy(&buffer[i], msg);
1061 if (write(scr->helper_fd, buffer, len + 4) < 0) {
1062 wsyserror(_("could not send message to background image helper"));
1067 Bool UpdateDomainFile(WDDomain * domain)
1070 char path[PATH_MAX];
1071 WMPropList *shared_dict, *dict;
1072 Bool result, freeDict = False;
1074 dict = domain->dictionary;
1075 if (WMIsPLDictionary(domain->dictionary)) {
1076 /* retrieve global system dictionary */
1077 snprintf(path, sizeof(path), "%s/WindowMaker/%s", SYSCONFDIR, domain->domain_name);
1078 if (stat(path, &stbuf) >= 0) {
1079 shared_dict = WMReadPropListFromFile(path);
1081 if (WMIsPLDictionary(shared_dict)) {
1083 dict = WMDeepCopyPropList(domain->dictionary);
1084 WMSubtractPLDictionaries(dict, shared_dict, True);
1086 WMReleasePropList(shared_dict);
1091 result = WMWritePropListToFile(dict, domain->path, True);
1094 WMReleasePropList(dict);
1100 char *StrConcatDot(char *a, char *b)
1110 len = strlen(a) + strlen(b) + 4;
1113 snprintf(str, len, "%s.%s", a, b);
1118 #define MAX_CMD_SIZE 4096
1120 Bool GetCommandForPid(int pid, char ***argv, int *argc)
1122 static char buf[MAX_CMD_SIZE];
1127 sprintf(buf, "/proc/%d/cmdline", pid);
1128 fPtr = fopen(buf, "r");
1130 count = read(fileno(fPtr), buf, MAX_CMD_SIZE);
1133 for (i = 0, *argc = 0; i < count; i++) {
1142 *argv = (char **)wmalloc(sizeof(char *) * (*argc));
1144 for (i = 0, j = 1; i < count; i++) {
1147 if (i < count - 1) {
1148 (*argv)[j++] = &buf[i + 1];
1163 static char *getCommandForWindow(Window win, int elements)
1165 char **argv, *command = NULL;
1168 if (XGetCommand(dpy, win, &argv, &argc)) {
1169 if (argc > 0 && argv != NULL) {
1172 command = wtokenjoin(argv, WMIN(argc, elements));
1173 if (command[0] == 0) {
1179 XFreeStringList(argv);
1186 /* Free result when done */
1187 char *GetCommandForWindow(Window win)
1189 return getCommandForWindow(win, 0);
1192 /* Free result when done */
1193 char *GetProgramNameForWindow(Window win)
1195 return getCommandForWindow(win, 1);