wmaker: Remove dead links from BUGFORM.
[wmaker-crm.git] / src / misc.c
blob93f1be1ff3b46e5092bdc0ba1126c0d9b723ac0c
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, *tmp, *text;
736 tmp = text = wstrdup(shortcut);
738 /* get modifiers */
739 while ((k = strchr(text, '+')) != NULL) {
740 int mod;
741 const char *lbl;
743 *k = 0;
744 mod = wXModifierFromKey(text);
745 if (mod < 0) {
746 return wstrdup("bug");
748 lbl = wXModifierToShortcutLabel(mod);
749 if (lbl)
750 buffer = wstrappend(buffer, lbl);
751 else
752 buffer = wstrappend(buffer, text);
753 text = k + 1;
756 buffer = wstrappend(buffer, text);
757 wfree(tmp);
759 return buffer;
762 char *GetShortcutKey(WShortKey key)
764 char *tmp = NULL;
765 char *k = XKeysymToString(XkbKeycodeToKeysym(dpy, key.keycode, 0, 0));
766 if (!k) return NULL;
768 char **m = wPreferences.modifier_labels;
769 if (key.modifier & ControlMask) tmp = wstrappend(tmp, m[1] ? m[1] : "Control+");
770 if (key.modifier & ShiftMask) tmp = wstrappend(tmp, m[0] ? m[0] : "Shift+");
771 if (key.modifier & Mod1Mask) tmp = wstrappend(tmp, m[2] ? m[2] : "Mod1+");
772 if (key.modifier & Mod2Mask) tmp = wstrappend(tmp, m[3] ? m[3] : "Mod2+");
773 if (key.modifier & Mod3Mask) tmp = wstrappend(tmp, m[4] ? m[4] : "Mod3+");
774 if (key.modifier & Mod4Mask) tmp = wstrappend(tmp, m[5] ? m[5] : "Mod4+");
775 if (key.modifier & Mod5Mask) tmp = wstrappend(tmp, m[6] ? m[6] : "Mod5+");
776 tmp = wstrappend(tmp, k);
778 return GetShortcutString(tmp);
781 char *EscapeWM_CLASS(const char *name, const char *class)
783 char *ret;
784 char *ename = NULL, *eclass = NULL;
785 int i, j, l;
787 if (!name && !class)
788 return NULL;
790 if (name) {
791 l = strlen(name);
792 ename = wmalloc(l * 2 + 1);
793 j = 0;
794 for (i = 0; i < l; i++) {
795 if (name[i] == '\\') {
796 ename[j++] = '\\';
797 } else if (name[i] == '.') {
798 ename[j++] = '\\';
800 ename[j++] = name[i];
802 ename[j] = 0;
804 if (class) {
805 l = strlen(class);
806 eclass = wmalloc(l * 2 + 1);
807 j = 0;
808 for (i = 0; i < l; i++) {
809 if (class[i] == '\\') {
810 eclass[j++] = '\\';
811 } else if (class[i] == '.') {
812 eclass[j++] = '\\';
814 eclass[j++] = class[i];
816 eclass[j] = 0;
819 if (ename && eclass) {
820 int len = strlen(ename) + strlen(eclass) + 4;
821 ret = wmalloc(len);
822 snprintf(ret, len, "%s.%s", ename, eclass);
823 wfree(ename);
824 wfree(eclass);
825 } else if (ename) {
826 ret = wstrdup(ename);
827 wfree(ename);
828 } else {
829 ret = wstrdup(eclass);
830 wfree(eclass);
833 return ret;
836 static void UnescapeWM_CLASS(const char *str, char **name, char **class)
838 int i, j, k, dot;
840 j = strlen(str);
841 *name = wmalloc(j);
842 **name = 0;
843 *class = wmalloc(j);
844 **class = 0;
846 /* separate string in 2 parts */
847 dot = -1;
848 for (i = 0; i < j; i++) {
849 if (str[i] == '\\') {
850 i++;
851 continue;
852 } else if (str[i] == '.') {
853 dot = i;
854 break;
858 /* unescape strings */
859 for (i = 0, k = 0; i < dot; i++) {
860 if (str[i] == '\\') {
861 continue;
862 } else {
863 (*name)[k++] = str[i];
866 (*name)[k] = 0;
868 for (i = dot + 1, k = 0; i < j; i++) {
869 if (str[i] == '\\') {
870 continue;
871 } else {
872 (*class)[k++] = str[i];
875 (*class)[k] = 0;
877 if (!*name) {
878 wfree(*name);
879 *name = NULL;
881 if (!*class) {
882 wfree(*class);
883 *class = NULL;
887 void SendHelperMessage(WScreen *scr, char type, int workspace, const char *msg)
889 char *buffer;
890 int len;
891 int i;
892 char buf[16];
894 if (!scr->flags.backimage_helper_launched) {
895 return;
898 len = (msg ? strlen(msg) : 0) + (workspace >= 0 ? 4 : 0) + 1;
899 buffer = wmalloc(len + 5);
900 snprintf(buf, sizeof(buf), "%4i", len);
901 memcpy(buffer, buf, 4);
902 buffer[4] = type;
903 i = 5;
904 if (workspace >= 0) {
905 snprintf(buf, sizeof(buf), "%4i", workspace);
906 memcpy(&buffer[i], buf, 4);
907 i += 4;
908 buffer[i] = 0;
910 if (msg)
911 strcpy(&buffer[i], msg);
913 if (write(scr->helper_fd, buffer, len + 4) < 0) {
914 werror(_("could not send message to background image helper"));
916 wfree(buffer);
919 Bool UpdateDomainFile(WDDomain * domain)
921 struct stat stbuf;
922 char path[PATH_MAX];
923 WMPropList *shared_dict, *dict;
924 Bool result, freeDict = False;
926 dict = domain->dictionary;
927 if (WMIsPLDictionary(domain->dictionary)) {
928 /* retrieve global system dictionary */
929 snprintf(path, sizeof(path), "%s/WindowMaker/%s", SYSCONFDIR, domain->domain_name);
930 if (stat(path, &stbuf) >= 0) {
931 shared_dict = WMReadPropListFromFile(path);
932 if (shared_dict) {
933 if (WMIsPLDictionary(shared_dict)) {
934 freeDict = True;
935 dict = WMDeepCopyPropList(domain->dictionary);
936 WMSubtractPLDictionaries(dict, shared_dict, True);
938 WMReleasePropList(shared_dict);
943 result = WMWritePropListToFile(dict, domain->path);
945 if (freeDict) {
946 WMReleasePropList(dict);
949 return result;
952 char *StrConcatDot(const char *a, const char *b)
954 int len;
955 char *str;
957 if (!a)
958 a = "";
959 if (!b)
960 b = "";
962 len = strlen(a) + strlen(b) + 4;
963 str = wmalloc(len);
965 snprintf(str, len, "%s.%s", a, b);
967 return str;
970 static char *getCommandForWindow(Window win, int elements)
972 char **argv, *command = NULL;
973 int argc;
975 if (XGetCommand(dpy, win, &argv, &argc)) {
976 if (argc > 0 && argv != NULL) {
977 if (elements == 0)
978 elements = argc;
979 command = wtokenjoin(argv, WMIN(argc, elements));
980 if (command[0] == 0) {
981 wfree(command);
982 command = NULL;
985 if (argv) {
986 XFreeStringList(argv);
990 return command;
993 /* Free result when done */
994 char *GetCommandForWindow(Window win)
996 return getCommandForWindow(win, 0);