util: clarify a bit of the code for parsing commands in wmgenmenu
[wmaker-crm.git] / src / misc.c
blob572fbe4294f8829c41b67eb46e8b4bdb36c16b0b
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 <X11/XKBlib.h>
38 #include <WINGs/WUtil.h>
39 #include <wraster.h>
41 #include "window.h"
42 #include "misc.h"
43 #include "WindowMaker.h"
44 #include "GNUstep.h"
45 #include "screen.h"
46 #include "wcore.h"
47 #include "window.h"
48 #include "framewin.h"
49 #include "dialog.h"
50 #include "xutil.h"
51 #include "xmodifier.h"
54 #define ICON_SIZE wPreferences.icon_size
56 /**** Local prototypes *****/
57 static void UnescapeWM_CLASS(const char *str, char **name, char **class);
59 /* XFetchName Wrapper */
60 Bool wFetchName(Display *dpy, Window win, char **winname)
62 XTextProperty text_prop;
63 char **list;
64 int num;
66 if (XGetWMName(dpy, win, &text_prop)) {
67 if (text_prop.value && text_prop.nitems > 0) {
68 if (text_prop.encoding == XA_STRING) {
69 *winname = wstrdup((char *)text_prop.value);
70 XFree(text_prop.value);
71 } else {
72 text_prop.nitems = strlen((char *)text_prop.value);
73 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >=
74 Success && num > 0 && *list) {
75 XFree(text_prop.value);
76 *winname = wstrdup(*list);
77 XFreeStringList(list);
78 } else {
79 *winname = wstrdup((char *)text_prop.value);
80 XFree(text_prop.value);
83 } else {
84 /* the title is set, but it was set to none */
85 *winname = wstrdup("");
87 return True;
88 } else {
89 /* the hint is probably not set */
90 *winname = NULL;
92 return False;
96 /* XGetIconName Wrapper */
97 Bool wGetIconName(Display *dpy, Window win, char **iconname)
99 XTextProperty text_prop;
100 char **list;
101 int num;
103 if (XGetWMIconName(dpy, win, &text_prop) != 0 && text_prop.value && text_prop.nitems > 0) {
104 if (text_prop.encoding == XA_STRING)
105 *iconname = (char *)text_prop.value;
106 else {
107 text_prop.nitems = strlen((char *)text_prop.value);
108 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >= Success && num > 0 && *list) {
109 XFree(text_prop.value);
110 *iconname = wstrdup(*list);
111 XFreeStringList(list);
112 } else
113 *iconname = (char *)text_prop.value;
115 return True;
117 *iconname = NULL;
118 return False;
121 static void eatExpose(void)
123 XEvent event, foo;
125 /* compress all expose events into a single one */
127 if (XCheckMaskEvent(dpy, ExposureMask, &event)) {
128 /* ignore other exposure events for this window */
129 while (XCheckWindowEvent(dpy, event.xexpose.window, ExposureMask, &foo)) ;
130 /* eat exposes for other windows */
131 eatExpose();
133 event.xexpose.count = 0;
134 XPutBackEvent(dpy, &event);
138 void move_window(Window win, int from_x, int from_y, int to_x, int to_y)
140 #ifdef ANIMATIONS
141 if (wPreferences.no_animations)
142 XMoveWindow(dpy, win, to_x, to_y);
143 else
144 SlideWindow(win, from_x, from_y, to_x, to_y);
145 #else
146 XMoveWindow(dpy, win, to_x, to_y);
147 #endif
150 void SlideWindow(Window win, int from_x, int from_y, int to_x, int to_y)
152 Window *wins[1] = { &win };
153 SlideWindows(wins, 1, from_x, from_y, to_x, to_y);
156 /* wins is an array of Window, sorted from left to right, the first is
157 * going to be moved from (from_x,from_y) to (to_x,to_y) and the
158 * following windows are going to be offset by (ICON_SIZE*i,0) */
159 void SlideWindows(Window *wins[], int n, int from_x, int from_y, int to_x, int to_y)
161 time_t time0 = time(NULL);
162 float dx, dy, x = from_x, y = from_y, px, py;
163 Bool is_dx_nul, is_dy_nul;
164 int dx_is_bigger = 0, dx_int, dy_int;
165 int slide_delay, slide_steps, slide_slowdown;
166 int i;
168 /* animation parameters */
169 static const struct {
170 int delay;
171 int steps;
172 int slowdown;
173 } apars[5] = {
174 {ICON_SLIDE_DELAY_UF, ICON_SLIDE_STEPS_UF, ICON_SLIDE_SLOWDOWN_UF},
175 {ICON_SLIDE_DELAY_F, ICON_SLIDE_STEPS_F, ICON_SLIDE_SLOWDOWN_F},
176 {ICON_SLIDE_DELAY_M, ICON_SLIDE_STEPS_M, ICON_SLIDE_SLOWDOWN_M},
177 {ICON_SLIDE_DELAY_S, ICON_SLIDE_STEPS_S, ICON_SLIDE_SLOWDOWN_S},
178 {ICON_SLIDE_DELAY_US, ICON_SLIDE_STEPS_US, ICON_SLIDE_SLOWDOWN_US}
181 slide_slowdown = apars[(int)wPreferences.icon_slide_speed].slowdown;
182 slide_steps = apars[(int)wPreferences.icon_slide_speed].steps;
183 slide_delay = apars[(int)wPreferences.icon_slide_speed].delay;
185 dx_int = to_x - from_x;
186 dy_int = to_y - from_y;
187 is_dx_nul = (dx_int == 0);
188 is_dy_nul = (dy_int == 0);
189 dx = (float) dx_int;
190 dy = (float) dy_int;
192 if (abs(dx_int) > abs(dy_int)) {
193 dx_is_bigger = 1;
196 if (dx_is_bigger) {
197 px = dx / slide_slowdown;
198 if (px < slide_steps && px > 0)
199 px = slide_steps;
200 else if (px > -slide_steps && px < 0)
201 px = -slide_steps;
202 py = (is_dx_nul ? 0.0 : px * dy / dx);
203 } else {
204 py = dy / slide_slowdown;
205 if (py < slide_steps && py > 0)
206 py = slide_steps;
207 else if (py > -slide_steps && py < 0)
208 py = -slide_steps;
209 px = (is_dy_nul ? 0.0 : py * dx / dy);
212 while (((int)x) != to_x ||
213 ((int)y) != to_y) {
214 x += px;
215 y += py;
216 if ((px < 0 && (int)x < to_x) || (px > 0 && (int)x > to_x))
217 x = (float)to_x;
218 if ((py < 0 && (int)y < to_y) || (py > 0 && (int)y > to_y))
219 y = (float)to_y;
221 if (dx_is_bigger) {
222 px = px * (1.0 - 1 / (float)slide_slowdown);
223 if (px < slide_steps && px > 0)
224 px = slide_steps;
225 else if (px > -slide_steps && px < 0)
226 px = -slide_steps;
227 py = (is_dx_nul ? 0.0 : px * dy / dx);
228 } else {
229 py = py * (1.0 - 1 / (float)slide_slowdown);
230 if (py < slide_steps && py > 0)
231 py = slide_steps;
232 else if (py > -slide_steps && py < 0)
233 py = -slide_steps;
234 px = (is_dy_nul ? 0.0 : py * dx / dy);
237 for (i = 0; i < n; i++) {
238 XMoveWindow(dpy, *wins[i], (int)x + i * ICON_SIZE, (int)y);
240 XFlush(dpy);
241 if (slide_delay > 0) {
242 wusleep(slide_delay * 1000L);
243 } else {
244 wusleep(10);
246 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
247 break;
249 for (i = 0; i < n; i++) {
250 XMoveWindow(dpy, *wins[i], to_x + i * ICON_SIZE, to_y);
253 XSync(dpy, 0);
254 /* compress expose events */
255 eatExpose();
258 char *ShrinkString(WMFont *font, const char *string, int width)
260 int w, w1 = 0;
261 int p;
262 char *pos;
263 char *text;
264 int p1, p2, t;
266 p = strlen(string);
267 w = WMWidthOfString(font, string, p);
268 text = wmalloc(strlen(string) + 8);
269 strcpy(text, string);
270 if (w <= width)
271 return text;
273 pos = strchr(text, ' ');
274 if (!pos)
275 pos = strchr(text, ':');
277 if (pos) {
278 *pos = 0;
279 p = strlen(text);
280 w1 = WMWidthOfString(font, text, p);
281 if (w1 > width) {
282 p = 0;
283 *pos = ' ';
284 *text = 0;
285 } else {
286 *pos = 0;
287 width -= w1;
288 p++;
290 string += p;
291 p = strlen(string);
292 } else {
293 *text = 0;
295 strcat(text, "...");
296 width -= WMWidthOfString(font, "...", 3);
298 p1 = 0;
299 p2 = p;
300 t = (p2 - p1) / 2;
301 while (p2 > p1 && p1 != t) {
302 w = WMWidthOfString(font, &string[p - t], t);
303 if (w > width) {
304 p2 = t;
305 t = p1 + (p2 - p1) / 2;
306 } else if (w < width) {
307 p1 = t;
308 t = p1 + (p2 - p1) / 2;
309 } else
310 p2 = p1 = t;
312 strcat(text, &string[p - p1]);
314 return text;
317 char *FindImage(const char *paths, const char *file)
319 char *tmp, *path = NULL;
321 tmp = strrchr(file, ':');
322 if (tmp) {
323 *tmp = 0;
324 path = wfindfile(paths, file);
325 *tmp = ':';
327 if (!tmp || !path)
328 path = wfindfile(paths, file);
330 return path;
333 static void timeoutHandler(void *data)
335 *(int *)data = 1;
338 static char *getTextSelection(WScreen * screen, Atom selection)
340 int buffer = -1;
342 switch (selection) {
343 case XA_CUT_BUFFER0:
344 buffer = 0;
345 break;
346 case XA_CUT_BUFFER1:
347 buffer = 1;
348 break;
349 case XA_CUT_BUFFER2:
350 buffer = 2;
351 break;
352 case XA_CUT_BUFFER3:
353 buffer = 3;
354 break;
355 case XA_CUT_BUFFER4:
356 buffer = 4;
357 break;
358 case XA_CUT_BUFFER5:
359 buffer = 5;
360 break;
361 case XA_CUT_BUFFER6:
362 buffer = 6;
363 break;
364 case XA_CUT_BUFFER7:
365 buffer = 7;
366 break;
368 if (buffer >= 0) {
369 char *data;
370 int size;
372 data = XFetchBuffer(dpy, &size, buffer);
374 return data;
375 } else {
376 char *data;
377 int bits;
378 Atom rtype;
379 unsigned long len, bytes;
380 WMHandlerID timer;
381 int timeout = 0;
382 XEvent ev;
383 static Atom clipboard = 0;
385 if (!clipboard)
386 clipboard = XInternAtom(dpy, "CLIPBOARD", False);
388 XDeleteProperty(dpy, screen->info_window, clipboard);
390 XConvertSelection(dpy, selection, XA_STRING, clipboard, screen->info_window, CurrentTime);
392 timer = WMAddTimerHandler(1000, timeoutHandler, &timeout);
394 while (!XCheckTypedWindowEvent(dpy, screen->info_window, SelectionNotify, &ev) && !timeout) ;
396 if (!timeout) {
397 WMDeleteTimerHandler(timer);
398 } else {
399 wwarning("selection retrieval timed out");
400 return NULL;
403 /* nobody owns the selection or the current owner has
404 * nothing to do with what we need */
405 if (ev.xselection.property == None) {
406 return NULL;
409 if (XGetWindowProperty(dpy, screen->info_window,
410 clipboard, 0, 1024,
411 False, XA_STRING, &rtype, &bits, &len,
412 &bytes, (unsigned char **)&data) != Success) {
413 return NULL;
415 if (rtype != XA_STRING || bits != 8) {
416 wwarning("invalid data in text selection");
417 if (data)
418 XFree(data);
419 return NULL;
421 return data;
425 static char *getselection(WScreen * scr)
427 char *tmp;
429 tmp = getTextSelection(scr, XA_PRIMARY);
430 if (!tmp)
431 tmp = getTextSelection(scr, XA_CUT_BUFFER0);
432 return tmp;
435 static char*
436 parseuserinputpart(const char *line, int *ptr, const char *endchars)
438 int depth = 0, begin;
439 char *value = NULL;
440 begin = ++*ptr;
442 while(line[*ptr] != '\0') {
443 if(line[*ptr] == '(') {
444 ++depth;
445 } else if(depth > 0 && line[*ptr] == ')') {
446 --depth;
447 } else if(depth == 0 && strchr(endchars, line[*ptr]) != NULL) {
448 value = wmalloc(*ptr - begin + 1);
449 strncpy(value, line + begin, *ptr - begin);
450 value[*ptr - begin] = '\0';
451 break;
453 ++*ptr;
456 return value;
459 static char*
460 getuserinput(WScreen *scr, const char *line, int *ptr, Bool advanced)
462 char *ret = NULL, *title = NULL, *prompt = NULL, *name = NULL;
463 int rv;
465 if(line[*ptr] == '(')
466 title = parseuserinputpart(line, ptr, ",)");
467 if(title != NULL && line[*ptr] == ',')
468 prompt = parseuserinputpart(line, ptr, ",)");
469 if(prompt != NULL && line[*ptr] == ',')
470 name = parseuserinputpart(line, ptr, ")");
472 if(advanced)
473 rv = wAdvancedInputDialog(scr,
474 title ? _(title):_("Program Arguments"),
475 prompt ? _(prompt):_("Enter command arguments:"),
476 name, &ret);
477 else
478 rv = wInputDialog(scr,
479 title ? _(title):_("Program Arguments"),
480 prompt ? _(prompt):_("Enter command arguments:"),
481 &ret);
483 if(title) wfree(title);
484 if(prompt) wfree(prompt);
485 if(name) wfree(name);
487 return rv ? ret : NULL;
490 #define S_NORMAL 0
491 #define S_ESCAPE 1
492 #define S_OPTION 2
495 * state input new-state output
496 * NORMAL % OPTION <nil>
497 * NORMAL \ ESCAPE <nil>
498 * NORMAL etc. NORMAL <input>
499 * ESCAPE any NORMAL <input>
500 * OPTION s NORMAL <selection buffer>
501 * OPTION w NORMAL <selected window id>
502 * OPTION a NORMAL <input text>
503 * OPTION d NORMAL <OffiX DND selection object>
504 * OPTION W NORMAL <current workspace>
505 * OPTION etc. NORMAL %<input>
507 #define TMPBUFSIZE 64
508 char *ExpandOptions(WScreen *scr, const char *cmdline)
510 int ptr, optr, state, len, olen;
511 char *out, *nout;
512 char *selection = NULL;
513 char *user_input = NULL;
514 #ifdef XDND
515 char *dropped_thing = NULL;
516 #endif
517 char tmpbuf[TMPBUFSIZE];
518 int slen;
520 len = strlen(cmdline);
521 olen = len + 1;
522 out = malloc(olen);
523 if (!out) {
524 wwarning(_("out of memory during expansion of \"%s\""), cmdline);
525 return NULL;
527 *out = 0;
528 ptr = 0; /* input line pointer */
529 optr = 0; /* output line pointer */
530 state = S_NORMAL;
531 while (ptr < len) {
532 switch (state) {
533 case S_NORMAL:
534 switch (cmdline[ptr]) {
535 case '\\':
536 state = S_ESCAPE;
537 break;
538 case '%':
539 state = S_OPTION;
540 break;
541 default:
542 state = S_NORMAL;
543 out[optr++] = cmdline[ptr];
544 break;
546 break;
547 case S_ESCAPE:
548 switch (cmdline[ptr]) {
549 case 'n':
550 out[optr++] = 10;
551 break;
553 case 'r':
554 out[optr++] = 13;
555 break;
557 case 't':
558 out[optr++] = 9;
559 break;
561 default:
562 out[optr++] = cmdline[ptr];
564 state = S_NORMAL;
565 break;
566 case S_OPTION:
567 state = S_NORMAL;
568 switch (cmdline[ptr]) {
569 case 'w':
570 if (scr->focused_window && scr->focused_window->flags.focused) {
571 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x",
572 (unsigned int)scr->focused_window->client_win);
573 slen = strlen(tmpbuf);
574 olen += slen;
575 nout = realloc(out, olen);
576 if (!nout) {
577 wwarning(_("out of memory during expansion of \"%%w\""));
578 goto error;
580 out = nout;
581 strcat(out, tmpbuf);
582 optr += slen;
583 } else {
584 out[optr++] = ' ';
586 break;
588 case 'W':
589 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x", (unsigned int)w_global.workspace.current + 1);
590 slen = strlen(tmpbuf);
591 olen += slen;
592 nout = realloc(out, olen);
593 if (!nout) {
594 wwarning(_("out of memory during expansion of \"%%W\""));
595 goto error;
597 out = nout;
598 strcat(out, tmpbuf);
599 optr += slen;
600 break;
602 case 'a':
603 case 'A':
604 ptr++;
605 user_input = getuserinput(scr, cmdline, &ptr, cmdline[ptr-1] == 'A');
606 if (user_input) {
607 slen = strlen(user_input);
608 olen += slen;
609 nout = realloc(out, olen);
610 if (!nout) {
611 wwarning(_("out of memory during expansion of \"%%a\""));
612 goto error;
614 out = nout;
615 strcat(out, user_input);
616 optr += slen;
617 } else {
618 /* Not an error, but user has Canceled the dialog box.
619 * This will make the command to not be performed. */
620 goto error;
622 break;
624 #ifdef XDND
625 case 'd':
626 if (scr->xdestring) {
627 dropped_thing = wstrdup(scr->xdestring);
629 if (!dropped_thing) {
630 scr->flags.dnd_data_convertion_status = 1;
631 goto error;
633 slen = strlen(dropped_thing);
634 olen += slen;
635 nout = realloc(out, olen);
636 if (!nout) {
637 wwarning(_("out of memory during expansion of \"%%d\""));
638 goto error;
640 out = nout;
641 strcat(out, dropped_thing);
642 optr += slen;
643 break;
644 #endif /* XDND */
646 case 's':
647 if (!selection) {
648 selection = getselection(scr);
650 if (!selection) {
651 wwarning(_("selection not available"));
652 goto error;
654 slen = strlen(selection);
655 olen += slen;
656 nout = realloc(out, olen);
657 if (!nout) {
658 wwarning(_("out of memory during expansion of \"%%s\""));
659 goto error;
661 out = nout;
662 strcat(out, selection);
663 optr += slen;
664 break;
666 default:
667 out[optr++] = '%';
668 out[optr++] = cmdline[ptr];
670 break;
672 out[optr] = 0;
673 ptr++;
675 if (selection)
676 XFree(selection);
677 return out;
679 error:
680 wfree(out);
681 if (selection)
682 XFree(selection);
683 return NULL;
686 void ParseWindowName(WMPropList *value, char **winstance, char **wclass, const char *where)
688 char *name;
690 *winstance = *wclass = NULL;
692 if (!WMIsPLString(value)) {
693 wwarning(_("bad window name value in %s state info"), where);
694 return;
697 name = WMGetFromPLString(value);
698 if (!name || strlen(name) == 0) {
699 wwarning(_("bad window name value in %s state info"), where);
700 return;
703 UnescapeWM_CLASS(name, winstance, wclass);
706 #if 0
707 static char *keysymToString(KeySym keysym, unsigned int state)
709 XKeyEvent kev;
710 char *buf = wmalloc(20);
711 int count;
713 kev.display = dpy;
714 kev.type = KeyPress;
715 kev.send_event = False;
716 kev.window = DefaultRootWindow(dpy);
717 kev.root = DefaultRootWindow(dpy);
718 kev.same_screen = True;
719 kev.subwindow = kev.root;
720 kev.serial = 0x12344321;
721 kev.time = CurrentTime;
722 kev.state = state;
723 kev.keycode = XKeysymToKeycode(dpy, keysym);
724 count = XLookupString(&kev, buf, 19, NULL, NULL);
725 buf[count] = 0;
727 return buf;
729 #endif
731 char *GetShortcutString(const char *shortcut)
733 char *buffer = NULL;
734 char *k;
735 /* KeySym ksym; */
736 int control = 0;
737 char *tmp, *text;
739 tmp = text = wstrdup(shortcut);
741 /* get modifiers */
742 while ((k = strchr(text, '+')) != NULL) {
743 int mod;
745 *k = 0;
746 mod = wXModifierFromKey(text);
747 if (mod < 0) {
748 return wstrdup("bug");
751 if (strcasecmp(text, "Meta") == 0) {
752 buffer = wstrappend(buffer, "M+");
753 } else if (strcasecmp(text, "Alt") == 0) {
754 buffer = wstrappend(buffer, "A+");
755 } else if (strcasecmp(text, "Shift") == 0) {
756 buffer = wstrappend(buffer, "Sh+");
757 } else if (strcasecmp(text, "Mod1") == 0) {
758 buffer = wstrappend(buffer, "M1+");
759 } else if (strcasecmp(text, "Mod2") == 0) {
760 buffer = wstrappend(buffer, "M2+");
761 } else if (strcasecmp(text, "Mod3") == 0) {
762 buffer = wstrappend(buffer, "M3+");
763 } else if (strcasecmp(text, "Mod4") == 0) {
764 buffer = wstrappend(buffer, "M4+");
765 } else if (strcasecmp(text, "Mod5") == 0) {
766 buffer = wstrappend(buffer, "M5+");
767 } else if (strcasecmp(text, "Control") == 0) {
768 control = 1;
769 } else {
770 buffer = wstrappend(buffer, text);
772 text = k + 1;
775 if (control) {
776 buffer = wstrappend(buffer, "^");
778 buffer = wstrappend(buffer, text);
779 wfree(tmp);
781 return buffer;
784 char *GetShortcutKey(WShortKey key)
786 char *tmp = NULL;
787 char *k = XKeysymToString(XkbKeycodeToKeysym(dpy, key.keycode, 0, 0));
788 if (!k) return NULL;
790 char **m = wPreferences.modifier_labels;
791 if (key.modifier & ControlMask) tmp = wstrappend(tmp, m[1] ? m[1] : "Ctrl+");
792 if (key.modifier & ShiftMask) tmp = wstrappend(tmp, m[0] ? m[0] : "Shift+");
793 if (key.modifier & Mod1Mask) tmp = wstrappend(tmp, m[2] ? m[2] : "Mod1+");
794 if (key.modifier & Mod2Mask) tmp = wstrappend(tmp, m[3] ? m[3] : "Mod2+");
795 if (key.modifier & Mod3Mask) tmp = wstrappend(tmp, m[4] ? m[4] : "Mod3+");
796 if (key.modifier & Mod4Mask) tmp = wstrappend(tmp, m[5] ? m[5] : "Mod4+");
797 if (key.modifier & Mod5Mask) tmp = wstrappend(tmp, m[6] ? m[6] : "Mod5+");
798 tmp = wstrappend(tmp, k);
800 return GetShortcutString(tmp);
803 char *EscapeWM_CLASS(const char *name, const char *class)
805 char *ret;
806 char *ename = NULL, *eclass = NULL;
807 int i, j, l;
809 if (!name && !class)
810 return NULL;
812 if (name) {
813 l = strlen(name);
814 ename = wmalloc(l * 2 + 1);
815 j = 0;
816 for (i = 0; i < l; i++) {
817 if (name[i] == '\\') {
818 ename[j++] = '\\';
819 } else if (name[i] == '.') {
820 ename[j++] = '\\';
822 ename[j++] = name[i];
824 ename[j] = 0;
826 if (class) {
827 l = strlen(class);
828 eclass = wmalloc(l * 2 + 1);
829 j = 0;
830 for (i = 0; i < l; i++) {
831 if (class[i] == '\\') {
832 eclass[j++] = '\\';
833 } else if (class[i] == '.') {
834 eclass[j++] = '\\';
836 eclass[j++] = class[i];
838 eclass[j] = 0;
841 if (ename && eclass) {
842 int len = strlen(ename) + strlen(eclass) + 4;
843 ret = wmalloc(len);
844 snprintf(ret, len, "%s.%s", ename, eclass);
845 wfree(ename);
846 wfree(eclass);
847 } else if (ename) {
848 ret = wstrdup(ename);
849 wfree(ename);
850 } else {
851 ret = wstrdup(eclass);
852 wfree(eclass);
855 return ret;
858 static void UnescapeWM_CLASS(const char *str, char **name, char **class)
860 int i, j, k, dot;
862 j = strlen(str);
863 *name = wmalloc(j);
864 **name = 0;
865 *class = wmalloc(j);
866 **class = 0;
868 /* separate string in 2 parts */
869 dot = -1;
870 for (i = 0; i < j; i++) {
871 if (str[i] == '\\') {
872 i++;
873 continue;
874 } else if (str[i] == '.') {
875 dot = i;
876 break;
880 /* unescape strings */
881 for (i = 0, k = 0; i < dot; i++) {
882 if (str[i] == '\\') {
883 continue;
884 } else {
885 (*name)[k++] = str[i];
888 (*name)[k] = 0;
890 for (i = dot + 1, k = 0; i < j; i++) {
891 if (str[i] == '\\') {
892 continue;
893 } else {
894 (*class)[k++] = str[i];
897 (*class)[k] = 0;
899 if (!*name) {
900 wfree(*name);
901 *name = NULL;
903 if (!*class) {
904 wfree(*class);
905 *class = NULL;
909 void SendHelperMessage(WScreen *scr, char type, int workspace, const char *msg)
911 char *buffer;
912 int len;
913 int i;
914 char buf[16];
916 if (!scr->flags.backimage_helper_launched) {
917 return;
920 len = (msg ? strlen(msg) : 0) + (workspace >= 0 ? 4 : 0) + 1;
921 buffer = wmalloc(len + 5);
922 snprintf(buf, sizeof(buf), "%4i", len);
923 memcpy(buffer, buf, 4);
924 buffer[4] = type;
925 i = 5;
926 if (workspace >= 0) {
927 snprintf(buf, sizeof(buf), "%4i", workspace);
928 memcpy(&buffer[i], buf, 4);
929 i += 4;
930 buffer[i] = 0;
932 if (msg)
933 strcpy(&buffer[i], msg);
935 if (write(scr->helper_fd, buffer, len + 4) < 0) {
936 werror(_("could not send message to background image helper"));
938 wfree(buffer);
941 Bool UpdateDomainFile(WDDomain * domain)
943 struct stat stbuf;
944 char path[PATH_MAX];
945 WMPropList *shared_dict, *dict;
946 Bool result, freeDict = False;
948 dict = domain->dictionary;
949 if (WMIsPLDictionary(domain->dictionary)) {
950 /* retrieve global system dictionary */
951 snprintf(path, sizeof(path), "%s/WindowMaker/%s", SYSCONFDIR, domain->domain_name);
952 if (stat(path, &stbuf) >= 0) {
953 shared_dict = WMReadPropListFromFile(path);
954 if (shared_dict) {
955 if (WMIsPLDictionary(shared_dict)) {
956 freeDict = True;
957 dict = WMDeepCopyPropList(domain->dictionary);
958 WMSubtractPLDictionaries(dict, shared_dict, True);
960 WMReleasePropList(shared_dict);
965 result = WMWritePropListToFile(dict, domain->path);
967 if (freeDict) {
968 WMReleasePropList(dict);
971 return result;
974 char *StrConcatDot(const char *a, const char *b)
976 int len;
977 char *str;
979 if (!a)
980 a = "";
981 if (!b)
982 b = "";
984 len = strlen(a) + strlen(b) + 4;
985 str = wmalloc(len);
987 snprintf(str, len, "%s.%s", a, b);
989 return str;
992 static char *getCommandForWindow(Window win, int elements)
994 char **argv, *command = NULL;
995 int argc;
997 if (XGetCommand(dpy, win, &argv, &argc)) {
998 if (argc > 0 && argv != NULL) {
999 if (elements == 0)
1000 elements = argc;
1001 command = wtokenjoin(argv, WMIN(argc, elements));
1002 if (command[0] == 0) {
1003 wfree(command);
1004 command = NULL;
1007 if (argv) {
1008 XFreeStringList(argv);
1012 return command;
1015 /* Free result when done */
1016 char *GetCommandForWindow(Window win)
1018 return getCommandForWindow(win, 0);