GetProgramNameForWindow removed
[wmaker-crm.git] / src / misc.c
blob389ad00cb5150f1b11c87b50d87ea6aa3e23f867
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.
20 #include "wconfig.h"
22 #include <X11/Xlib.h>
23 #include <X11/Xutil.h>
24 #include <X11/Xatom.h>
25 #include <sys/stat.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #include <stdarg.h>
32 #include <pwd.h>
33 #include <math.h>
34 #include <time.h>
36 #include <WINGs/WUtil.h>
37 #include <wraster.h>
39 #include "WindowMaker.h"
40 #include "GNUstep.h"
41 #include "screen.h"
42 #include "wcore.h"
43 #include "window.h"
44 #include "framewin.h"
45 #include "funcs.h"
46 #include "dialog.h"
47 #include "xutil.h"
48 #include "xmodifier.h"
50 /**** global variables *****/
51 extern WPreferences wPreferences;
53 #ifdef USECPP
54 static void putdef(char *line, char *name, char *value)
56 if (!value) {
57 wwarning(_("could not define value for %s for cpp"), name);
58 return;
60 strcat(line, name);
61 strcat(line, value);
64 static void putidef(char *line, char *name, int value)
66 char tmp[64];
67 snprintf(tmp, sizeof(tmp), "%i", value);
68 strcat(line, name);
69 strcat(line, tmp);
72 static char *username(void)
74 char *tmp;
76 tmp = getlogin();
77 if (!tmp) {
78 struct passwd *user;
80 user = getpwuid(getuid());
81 if (!user) {
82 werror(_("could not get password entry for UID %i"), getuid());
83 return NULL;
85 if (!user->pw_name) {
86 return NULL;
87 } else {
88 return user->pw_name;
91 return tmp;
94 char *MakeCPPArgs(char *path)
96 char buffer[MAXLINE], *buf, *line;
97 Visual *visual;
98 char *tmp;
100 line = wmalloc(MAXLINE);
101 *line = 0;
102 if ((buf = getenv("HOSTNAME")) != NULL) {
103 if (buf[0] == '(') {
104 wwarning(_("your machine is misconfigured. HOSTNAME is set to %s"), buf);
105 } else
106 putdef(line, " -DHOST=", buf);
107 } else if ((buf = getenv("HOST")) != NULL) {
108 if (buf[0] == '(') {
109 wwarning(_("your machine is misconfigured. HOST is set to %s"), buf);
110 } else
111 putdef(line, " -DHOST=", buf);
113 buf = username();
114 if (buf)
115 putdef(line, " -DUSER=", buf);
116 putidef(line, " -DUID=", getuid());
117 buf = XDisplayName(DisplayString(dpy));
118 putdef(line, " -DDISPLAY=", buf);
119 putdef(line, " -DWM_VERSION=", VERSION);
121 visual = DefaultVisual(dpy, DefaultScreen(dpy));
122 putidef(line, " -DVISUAL=", visual->class);
124 putidef(line, " -DDEPTH=", DefaultDepth(dpy, DefaultScreen(dpy)));
126 putidef(line, " -DSCR_WIDTH=", WidthOfScreen(DefaultScreenOfDisplay(dpy)));
127 putidef(line, " -DSCR_HEIGHT=", HeightOfScreen(DefaultScreenOfDisplay(dpy)));
129 /* put the dir where the menu is being read from to the
130 * search path */
131 if (path) {
132 tmp = wstrdup(path);
133 buf = strchr(tmp + 1, ' ');
134 if (buf) {
135 *buf = 0;
137 buf = strrchr(tmp, '/');
138 if (buf) {
139 *buf = 0; /* trunc filename */
140 putdef(line, " -I", tmp);
142 wfree(tmp);
145 /* this should be done just once, but it works this way */
146 strcpy(buffer, DEF_CONFIG_PATHS);
147 buf = strtok(buffer, ":");
149 do {
150 char fullpath[MAXLINE];
152 if (buf[0] != '~') {
153 strcpy(fullpath, buf);
154 } else {
155 /* home is statically allocated. Don't free it! */
156 char *home = wgethomedir();
158 strcpy(fullpath, home);
159 strcat(fullpath, &(buf[1]));
162 putdef(line, " -I", fullpath);
164 } while ((buf = strtok(NULL, ":")) != NULL);
166 #undef arg
167 return line;
169 #endif /* USECPP */
171 /* XFetchName Wrapper */
172 Bool wFetchName(Display *dpy, Window win, char **winname)
174 XTextProperty text_prop;
175 char **list;
176 int num;
178 if (XGetWMName(dpy, win, &text_prop)) {
179 if (text_prop.value && text_prop.nitems > 0) {
180 if (text_prop.encoding == XA_STRING) {
181 *winname = wstrdup((char *)text_prop.value);
182 XFree(text_prop.value);
183 } else {
184 text_prop.nitems = strlen((char *)text_prop.value);
185 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >=
186 Success && num > 0 && *list) {
187 XFree(text_prop.value);
188 *winname = wstrdup(*list);
189 XFreeStringList(list);
190 } else {
191 *winname = wstrdup((char *)text_prop.value);
192 XFree(text_prop.value);
195 } else {
196 /* the title is set, but it was set to none */
197 *winname = wstrdup("");
199 return True;
200 } else {
201 /* the hint is probably not set */
202 *winname = NULL;
204 return False;
208 /* XGetIconName Wrapper */
209 Bool wGetIconName(Display *dpy, Window win, char **iconname)
211 XTextProperty text_prop;
212 char **list;
213 int num;
215 if (XGetWMIconName(dpy, win, &text_prop) != 0 && text_prop.value && text_prop.nitems > 0) {
216 if (text_prop.encoding == XA_STRING)
217 *iconname = (char *)text_prop.value;
218 else {
219 text_prop.nitems = strlen((char *)text_prop.value);
220 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >= Success && num > 0 && *list) {
221 XFree(text_prop.value);
222 *iconname = wstrdup(*list);
223 XFreeStringList(list);
224 } else
225 *iconname = (char *)text_prop.value;
227 return True;
229 *iconname = NULL;
230 return False;
233 static void eatExpose(void)
235 XEvent event, foo;
237 /* compress all expose events into a single one */
239 if (XCheckMaskEvent(dpy, ExposureMask, &event)) {
240 /* ignore other exposure events for this window */
241 while (XCheckWindowEvent(dpy, event.xexpose.window, ExposureMask, &foo)) ;
242 /* eat exposes for other windows */
243 eatExpose();
245 event.xexpose.count = 0;
246 XPutBackEvent(dpy, &event);
250 void SlideWindow(Window win, int from_x, int from_y, int to_x, int to_y)
252 time_t time0 = time(NULL);
253 float dx, dy, x = from_x, y = from_y, sx, sy, px, py;
254 int dx_is_bigger = 0;
255 int slide_delay, slide_steps, slide_slowdown;
257 /* animation parameters */
258 static struct {
259 int delay;
260 int steps;
261 int slowdown;
262 } apars[5] = {
263 {ICON_SLIDE_DELAY_UF, ICON_SLIDE_STEPS_UF, ICON_SLIDE_SLOWDOWN_UF},
264 {ICON_SLIDE_DELAY_F, ICON_SLIDE_STEPS_F, ICON_SLIDE_SLOWDOWN_F},
265 {ICON_SLIDE_DELAY_M, ICON_SLIDE_STEPS_M, ICON_SLIDE_SLOWDOWN_M},
266 {ICON_SLIDE_DELAY_S, ICON_SLIDE_STEPS_S, ICON_SLIDE_SLOWDOWN_S},
267 {ICON_SLIDE_DELAY_US, ICON_SLIDE_STEPS_US, ICON_SLIDE_SLOWDOWN_US}
270 slide_slowdown = apars[(int)wPreferences.icon_slide_speed].slowdown;
271 slide_steps = apars[(int)wPreferences.icon_slide_speed].steps;
272 slide_delay = apars[(int)wPreferences.icon_slide_speed].delay;
274 dx = (float)(to_x - from_x);
275 dy = (float)(to_y - from_y);
276 sx = (dx == 0 ? 0 : fabs(dx) / dx);
277 sy = (dy == 0 ? 0 : fabs(dy) / dy);
279 if (fabs(dx) > fabs(dy)) {
280 dx_is_bigger = 1;
283 if (dx_is_bigger) {
284 px = dx / slide_slowdown;
285 if (px < slide_steps && px > 0)
286 px = slide_steps;
287 else if (px > -slide_steps && px < 0)
288 px = -slide_steps;
289 py = (sx == 0 ? 0 : px * dy / dx);
290 } else {
291 py = dy / slide_slowdown;
292 if (py < slide_steps && py > 0)
293 py = slide_steps;
294 else if (py > -slide_steps && py < 0)
295 py = -slide_steps;
296 px = (sy == 0 ? 0 : py * dx / dy);
299 while (x != to_x || y != to_y) {
300 x += px;
301 y += py;
302 if ((px < 0 && (int)x < to_x) || (px > 0 && (int)x > to_x))
303 x = (float)to_x;
304 if ((py < 0 && (int)y < to_y) || (py > 0 && (int)y > to_y))
305 y = (float)to_y;
307 if (dx_is_bigger) {
308 px = px * (1.0 - 1 / (float)slide_slowdown);
309 if (px < slide_steps && px > 0)
310 px = slide_steps;
311 else if (px > -slide_steps && px < 0)
312 px = -slide_steps;
313 py = (sx == 0 ? 0 : px * dy / dx);
314 } else {
315 py = py * (1.0 - 1 / (float)slide_slowdown);
316 if (py < slide_steps && py > 0)
317 py = slide_steps;
318 else if (py > -slide_steps && py < 0)
319 py = -slide_steps;
320 px = (sy == 0 ? 0 : py * dx / dy);
323 XMoveWindow(dpy, win, (int)x, (int)y);
324 XFlush(dpy);
325 if (slide_delay > 0) {
326 wusleep(slide_delay * 1000L);
327 } else {
328 wusleep(10);
330 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
331 break;
333 XMoveWindow(dpy, win, to_x, to_y);
335 XSync(dpy, 0);
336 /* compress expose events */
337 eatExpose();
340 char *ShrinkString(WMFont * font, char *string, int width)
342 int w, w1 = 0;
343 int p;
344 char *pos;
345 char *text;
346 int p1, p2, t;
348 p = strlen(string);
349 w = WMWidthOfString(font, string, p);
350 text = wmalloc(strlen(string) + 8);
351 strcpy(text, string);
352 if (w <= width)
353 return text;
355 pos = strchr(text, ' ');
356 if (!pos)
357 pos = strchr(text, ':');
359 if (pos) {
360 *pos = 0;
361 p = strlen(text);
362 w1 = WMWidthOfString(font, text, p);
363 if (w1 > width) {
364 w1 = 0;
365 p = 0;
366 *pos = ' ';
367 *text = 0;
368 } else {
369 *pos = 0;
370 width -= w1;
371 p++;
373 string += p;
374 p = strlen(string);
375 } else {
376 *text = 0;
378 strcat(text, "...");
379 width -= WMWidthOfString(font, "...", 3);
380 pos = string;
381 p1 = 0;
382 p2 = p;
383 t = (p2 - p1) / 2;
384 while (p2 > p1 && p1 != t) {
385 w = WMWidthOfString(font, &string[p - t], t);
386 if (w > width) {
387 p2 = t;
388 t = p1 + (p2 - p1) / 2;
389 } else if (w < width) {
390 p1 = t;
391 t = p1 + (p2 - p1) / 2;
392 } else
393 p2 = p1 = t;
395 strcat(text, &string[p - p1]);
397 return text;
400 char *FindImage(char *paths, char *file)
402 char *tmp, *path = NULL;
404 tmp = strrchr(file, ':');
405 if (tmp) {
406 *tmp = 0;
407 path = wfindfile(paths, file);
408 *tmp = ':';
410 if (!tmp || !path)
411 path = wfindfile(paths, file);
413 return path;
416 static void timeoutHandler(void *data)
418 *(int *)data = 1;
421 static char *getTextSelection(WScreen * screen, Atom selection)
423 int buffer = -1;
425 switch (selection) {
426 case XA_CUT_BUFFER0:
427 buffer = 0;
428 break;
429 case XA_CUT_BUFFER1:
430 buffer = 1;
431 break;
432 case XA_CUT_BUFFER2:
433 buffer = 2;
434 break;
435 case XA_CUT_BUFFER3:
436 buffer = 3;
437 break;
438 case XA_CUT_BUFFER4:
439 buffer = 4;
440 break;
441 case XA_CUT_BUFFER5:
442 buffer = 5;
443 break;
444 case XA_CUT_BUFFER6:
445 buffer = 6;
446 break;
447 case XA_CUT_BUFFER7:
448 buffer = 7;
449 break;
451 if (buffer >= 0) {
452 char *data;
453 int size;
455 data = XFetchBuffer(dpy, &size, buffer);
457 return data;
458 } else {
459 char *data;
460 int bits;
461 Atom rtype;
462 unsigned long len, bytes;
463 WMHandlerID timer;
464 int timeout = 0;
465 XEvent ev;
466 static Atom clipboard = 0;
468 if (!clipboard)
469 clipboard = XInternAtom(dpy, "CLIPBOARD", False);
471 XDeleteProperty(dpy, screen->info_window, clipboard);
473 XConvertSelection(dpy, selection, XA_STRING, clipboard, screen->info_window, CurrentTime);
475 timer = WMAddTimerHandler(1000, timeoutHandler, &timeout);
477 while (!XCheckTypedWindowEvent(dpy, screen->info_window, SelectionNotify, &ev) && !timeout) ;
479 if (!timeout) {
480 WMDeleteTimerHandler(timer);
481 } else {
482 wwarning("selection retrieval timed out");
483 return NULL;
486 /* nobody owns the selection or the current owner has
487 * nothing to do with what we need */
488 if (ev.xselection.property == None) {
489 return NULL;
492 if (XGetWindowProperty(dpy, screen->info_window,
493 clipboard, 0, 1024,
494 False, XA_STRING, &rtype, &bits, &len,
495 &bytes, (unsigned char **)&data) != Success) {
496 return NULL;
498 if (rtype != XA_STRING || bits != 8) {
499 wwarning("invalid data in text selection");
500 if (data)
501 XFree(data);
502 return NULL;
504 return data;
508 static char *getselection(WScreen * scr)
510 char *tmp;
512 tmp = getTextSelection(scr, XA_PRIMARY);
513 if (!tmp)
514 tmp = getTextSelection(scr, XA_CUT_BUFFER0);
515 return tmp;
518 static char*
519 parseuserinputpart(char *line, int *ptr, char *endchars)
521 int depth = 0, begin;
522 char *value = NULL;
523 begin = ++*ptr;
525 while(line[*ptr] != '\0') {
526 if(line[*ptr] == '(') {
527 ++depth;
528 } else if(depth > 0 && line[*ptr] == ')') {
529 --depth;
530 } else if(depth == 0 && strchr(endchars, line[*ptr]) != NULL) {
531 value = wmalloc(*ptr - begin + 1);
532 strncpy(value, line + begin, *ptr - begin);
533 value[*ptr - begin] = '\0';
534 break;
536 ++*ptr;
539 return value;
542 static char*
543 getuserinput(WScreen *scr, char *line, int *ptr, Bool advanced)
545 char *ret = NULL, *title = NULL, *prompt = NULL, *name = NULL;
546 int rv;
548 if(line[*ptr] == '(')
549 title = parseuserinputpart(line, ptr, ",)");
550 if(title != NULL && line[*ptr] == ',')
551 prompt = parseuserinputpart(line, ptr, ",)");
552 if(prompt != NULL && line[*ptr] == ',')
553 name = parseuserinputpart(line, ptr, ")");
555 if(advanced)
556 rv = wAdvancedInputDialog(scr,
557 title ? _(title):_("Program Arguments"),
558 prompt ? _(prompt):_("Enter command arguments:"),
559 name, &ret);
560 else
561 rv = wInputDialog(scr,
562 title ? _(title):_("Program Arguments"),
563 prompt ? _(prompt):_("Enter command arguments:"),
564 &ret);
566 if(title) wfree(title);
567 if(prompt) wfree(prompt);
568 if(name) wfree(name);
570 return rv ? ret : NULL;
573 #define S_NORMAL 0
574 #define S_ESCAPE 1
575 #define S_OPTION 2
578 * state input new-state output
579 * NORMAL % OPTION <nil>
580 * NORMAL \ ESCAPE <nil>
581 * NORMAL etc. NORMAL <input>
582 * ESCAPE any NORMAL <input>
583 * OPTION s NORMAL <selection buffer>
584 * OPTION w NORMAL <selected window id>
585 * OPTION a NORMAL <input text>
586 * OPTION d NORMAL <OffiX DND selection object>
587 * OPTION W NORMAL <current workspace>
588 * OPTION etc. NORMAL %<input>
590 #define TMPBUFSIZE 64
591 char *ExpandOptions(WScreen * scr, char *cmdline)
593 int ptr, optr, state, len, olen;
594 char *out, *nout;
595 char *selection = NULL;
596 char *user_input = NULL;
597 #ifdef XDND
598 char *dropped_thing = NULL;
599 #endif
600 char tmpbuf[TMPBUFSIZE];
601 int slen;
603 len = strlen(cmdline);
604 olen = len + 1;
605 out = malloc(olen);
606 if (!out) {
607 wwarning(_("out of memory during expansion of \"%s\""), cmdline);
608 return NULL;
610 *out = 0;
611 ptr = 0; /* input line pointer */
612 optr = 0; /* output line pointer */
613 state = S_NORMAL;
614 while (ptr < len) {
615 switch (state) {
616 case S_NORMAL:
617 switch (cmdline[ptr]) {
618 case '\\':
619 state = S_ESCAPE;
620 break;
621 case '%':
622 state = S_OPTION;
623 break;
624 default:
625 state = S_NORMAL;
626 out[optr++] = cmdline[ptr];
627 break;
629 break;
630 case S_ESCAPE:
631 switch (cmdline[ptr]) {
632 case 'n':
633 out[optr++] = 10;
634 break;
636 case 'r':
637 out[optr++] = 13;
638 break;
640 case 't':
641 out[optr++] = 9;
642 break;
644 default:
645 out[optr++] = cmdline[ptr];
647 state = S_NORMAL;
648 break;
649 case S_OPTION:
650 state = S_NORMAL;
651 switch (cmdline[ptr]) {
652 case 'w':
653 if (scr->focused_window && scr->focused_window->flags.focused) {
654 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x",
655 (unsigned int)scr->focused_window->client_win);
656 slen = strlen(tmpbuf);
657 olen += slen;
658 nout = realloc(out, olen);
659 if (!nout) {
660 wwarning(_("out of memory during expansion of \"%%w\""));
661 goto error;
663 out = nout;
664 strcat(out, tmpbuf);
665 optr += slen;
666 } else {
667 out[optr++] = ' ';
669 break;
671 case 'W':
672 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x", (unsigned int)scr->current_workspace + 1);
673 slen = strlen(tmpbuf);
674 olen += slen;
675 nout = realloc(out, olen);
676 if (!nout) {
677 wwarning(_("out of memory during expansion of \"%%W\""));
678 goto error;
680 out = nout;
681 strcat(out, tmpbuf);
682 optr += slen;
683 break;
685 case 'a':
686 case 'A':
687 ptr++;
688 user_input = getuserinput(scr, cmdline, &ptr, cmdline[ptr-1] == 'A');
689 if (user_input) {
690 slen = strlen(user_input);
691 olen += slen;
692 nout = realloc(out, olen);
693 if (!nout) {
694 wwarning(_("out of memory during expansion of \"%%a\""));
695 goto error;
697 out = nout;
698 strcat(out, user_input);
699 optr += slen;
700 } else {
701 /* Not an error, but user has Canceled the dialog box.
702 * This will make the command to not be performed. */
703 goto error;
705 break;
707 #ifdef XDND
708 case 'd':
709 if (scr->xdestring) {
710 dropped_thing = wstrdup(scr->xdestring);
712 if (!dropped_thing) {
713 dropped_thing = get_dnd_selection(scr);
715 if (!dropped_thing) {
716 scr->flags.dnd_data_convertion_status = 1;
717 goto error;
719 slen = strlen(dropped_thing);
720 olen += slen;
721 nout = realloc(out, olen);
722 if (!nout) {
723 wwarning(_("out of memory during expansion of \"%%d\""));
724 goto error;
726 out = nout;
727 strcat(out, dropped_thing);
728 optr += slen;
729 break;
730 #endif /* XDND */
732 case 's':
733 if (!selection) {
734 selection = getselection(scr);
736 if (!selection) {
737 wwarning(_("selection not available"));
738 goto error;
740 slen = strlen(selection);
741 olen += slen;
742 nout = realloc(out, olen);
743 if (!nout) {
744 wwarning(_("out of memory during expansion of \"%%s\""));
745 goto error;
747 out = nout;
748 strcat(out, selection);
749 optr += slen;
750 break;
752 default:
753 out[optr++] = '%';
754 out[optr++] = cmdline[ptr];
756 break;
758 out[optr] = 0;
759 ptr++;
761 if (selection)
762 XFree(selection);
763 return out;
765 error:
766 wfree(out);
767 if (selection)
768 XFree(selection);
769 return NULL;
772 void ParseWindowName(WMPropList * value, char **winstance, char **wclass, char *where)
774 char *name;
776 *winstance = *wclass = NULL;
778 if (!WMIsPLString(value)) {
779 wwarning(_("bad window name value in %s state info"), where);
780 return;
783 name = WMGetFromPLString(value);
784 if (!name || strlen(name) == 0) {
785 wwarning(_("bad window name value in %s state info"), where);
786 return;
789 UnescapeWM_CLASS(name, winstance, wclass);
792 #if 0
793 static char *keysymToString(KeySym keysym, unsigned int state)
795 XKeyEvent kev;
796 char *buf = wmalloc(20);
797 int count;
799 kev.display = dpy;
800 kev.type = KeyPress;
801 kev.send_event = False;
802 kev.window = DefaultRootWindow(dpy);
803 kev.root = DefaultRootWindow(dpy);
804 kev.same_screen = True;
805 kev.subwindow = kev.root;
806 kev.serial = 0x12344321;
807 kev.time = CurrentTime;
808 kev.state = state;
809 kev.keycode = XKeysymToKeycode(dpy, keysym);
810 count = XLookupString(&kev, buf, 19, NULL, NULL);
811 buf[count] = 0;
813 return buf;
815 #endif
817 char *GetShortcutString(char *text)
819 char *buffer = NULL;
820 char *k;
821 int modmask = 0;
822 /* KeySym ksym; */
823 int control = 0;
824 char *tmp;
826 tmp = text = wstrdup(text);
828 /* get modifiers */
829 while ((k = strchr(text, '+')) != NULL) {
830 int mod;
832 *k = 0;
833 mod = wXModifierFromKey(text);
834 if (mod < 0) {
835 return wstrdup("bug");
838 modmask |= mod;
840 if (strcasecmp(text, "Meta") == 0) {
841 buffer = wstrappend(buffer, "M+");
842 } else if (strcasecmp(text, "Alt") == 0) {
843 buffer = wstrappend(buffer, "A+");
844 } else if (strcasecmp(text, "Shift") == 0) {
845 buffer = wstrappend(buffer, "Sh+");
846 } else if (strcasecmp(text, "Mod1") == 0) {
847 buffer = wstrappend(buffer, "M1+");
848 } else if (strcasecmp(text, "Mod2") == 0) {
849 buffer = wstrappend(buffer, "M2+");
850 } else if (strcasecmp(text, "Mod3") == 0) {
851 buffer = wstrappend(buffer, "M3+");
852 } else if (strcasecmp(text, "Mod4") == 0) {
853 buffer = wstrappend(buffer, "M4+");
854 } else if (strcasecmp(text, "Mod5") == 0) {
855 buffer = wstrappend(buffer, "M5+");
856 } else if (strcasecmp(text, "Control") == 0) {
857 control = 1;
858 } else {
859 buffer = wstrappend(buffer, text);
861 text = k + 1;
864 if (control) {
865 buffer = wstrappend(buffer, "^");
867 buffer = wstrappend(buffer, text);
869 /* get key */
870 /* ksym = XStringToKeysym(text);
871 tmp = keysymToString(ksym, modmask);
872 puts(tmp);
873 buffer = wstrappend(buffer, tmp);
875 wfree(tmp);
877 return buffer;
880 char *EscapeWM_CLASS(char *name, char *class)
882 char *ret;
883 char *ename = NULL, *eclass = NULL;
884 int i, j, l;
886 if (!name && !class)
887 return NULL;
889 if (name) {
890 l = strlen(name);
891 ename = wmalloc(l * 2 + 1);
892 j = 0;
893 for (i = 0; i < l; i++) {
894 if (name[i] == '\\') {
895 ename[j++] = '\\';
896 } else if (name[i] == '.') {
897 ename[j++] = '\\';
899 ename[j++] = name[i];
901 ename[j] = 0;
903 if (class) {
904 l = strlen(class);
905 eclass = wmalloc(l * 2 + 1);
906 j = 0;
907 for (i = 0; i < l; i++) {
908 if (class[i] == '\\') {
909 eclass[j++] = '\\';
910 } else if (class[i] == '.') {
911 eclass[j++] = '\\';
913 eclass[j++] = class[i];
915 eclass[j] = 0;
918 if (ename && eclass) {
919 int len = strlen(ename) + strlen(eclass) + 4;
920 ret = wmalloc(len);
921 snprintf(ret, len, "%s.%s", ename, eclass);
922 wfree(ename);
923 wfree(eclass);
924 } else if (ename) {
925 ret = wstrdup(ename);
926 wfree(ename);
927 } else {
928 ret = wstrdup(eclass);
929 wfree(eclass);
932 return ret;
935 void UnescapeWM_CLASS(char *str, char **name, char **class)
937 int i, j, k, dot;
939 j = strlen(str);
940 *name = wmalloc(j);
941 **name = 0;
942 *class = wmalloc(j);
943 **class = 0;
945 /* separate string in 2 parts */
946 dot = -1;
947 for (i = 0; i < j; i++) {
948 if (str[i] == '\\') {
949 i++;
950 continue;
951 } else if (str[i] == '.') {
952 dot = i;
953 break;
957 /* unescape strings */
958 for (i = 0, k = 0; i < dot; i++) {
959 if (str[i] == '\\') {
960 continue;
961 } else {
962 (*name)[k++] = str[i];
965 (*name)[k] = 0;
967 for (i = dot + 1, k = 0; i < j; i++) {
968 if (str[i] == '\\') {
969 continue;
970 } else {
971 (*class)[k++] = str[i];
974 (*class)[k] = 0;
976 if (!*name) {
977 wfree(*name);
978 *name = NULL;
980 if (!*class) {
981 wfree(*class);
982 *class = NULL;
986 void SendHelperMessage(WScreen * scr, char type, int workspace, char *msg)
988 char *buffer;
989 int len;
990 int i;
991 char buf[16];
993 if (!scr->flags.backimage_helper_launched) {
994 return;
997 len = (msg ? strlen(msg) : 0) + (workspace >= 0 ? 4 : 0) + 1;
998 buffer = wmalloc(len + 5);
999 snprintf(buf, sizeof(buf), "%4i", len);
1000 memcpy(buffer, buf, 4);
1001 buffer[4] = type;
1002 i = 5;
1003 if (workspace >= 0) {
1004 snprintf(buf, sizeof(buf), "%4i", workspace);
1005 memcpy(&buffer[i], buf, 4);
1006 i += 4;
1007 buffer[i] = 0;
1009 if (msg)
1010 strcpy(&buffer[i], msg);
1012 if (write(scr->helper_fd, buffer, len + 4) < 0) {
1013 werror(_("could not send message to background image helper"));
1015 wfree(buffer);
1018 Bool UpdateDomainFile(WDDomain * domain)
1020 struct stat stbuf;
1021 char path[PATH_MAX];
1022 WMPropList *shared_dict, *dict;
1023 Bool result, freeDict = False;
1025 dict = domain->dictionary;
1026 if (WMIsPLDictionary(domain->dictionary)) {
1027 /* retrieve global system dictionary */
1028 snprintf(path, sizeof(path), "%s/WindowMaker/%s", SYSCONFDIR, domain->domain_name);
1029 if (stat(path, &stbuf) >= 0) {
1030 shared_dict = WMReadPropListFromFile(path);
1031 if (shared_dict) {
1032 if (WMIsPLDictionary(shared_dict)) {
1033 freeDict = True;
1034 dict = WMDeepCopyPropList(domain->dictionary);
1035 WMSubtractPLDictionaries(dict, shared_dict, True);
1037 WMReleasePropList(shared_dict);
1042 result = WMWritePropListToFile(dict, domain->path);
1044 if (freeDict) {
1045 WMReleasePropList(dict);
1048 return result;
1051 char *StrConcatDot(char *a, char *b)
1053 int len;
1054 char *str;
1056 if (!a)
1057 a = "";
1058 if (!b)
1059 b = "";
1061 len = strlen(a) + strlen(b) + 4;
1062 str = wmalloc(len);
1064 snprintf(str, len, "%s.%s", a, b);
1066 return str;
1069 static char *getCommandForWindow(Window win, int elements)
1071 char **argv, *command = NULL;
1072 int argc;
1074 if (XGetCommand(dpy, win, &argv, &argc)) {
1075 if (argc > 0 && argv != NULL) {
1076 if (elements == 0)
1077 elements = argc;
1078 command = wtokenjoin(argv, WMIN(argc, elements));
1079 if (command[0] == 0) {
1080 wfree(command);
1081 command = NULL;
1084 if (argv) {
1085 XFreeStringList(argv);
1089 return command;
1092 /* Free result when done */
1093 char *GetCommandForWindow(Window win)
1095 return getCommandForWindow(win, 0);