Handle NULL pointer as good as possible (Coverity #50099)
[wmaker-crm.git] / src / misc.c
blob137d78ea5f9fa33892ebd63b34b5d099944f3bb8
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 <fcntl.h>
32 #include <stdarg.h>
33 #include <pwd.h>
34 #include <math.h>
35 #include <time.h>
36 #include <errno.h>
38 #include <X11/XKBlib.h>
40 #include <WINGs/WUtil.h>
41 #include <wraster.h>
43 #include "window.h"
44 #include "misc.h"
45 #include "WindowMaker.h"
46 #include "GNUstep.h"
47 #include "screen.h"
48 #include "wcore.h"
49 #include "window.h"
50 #include "framewin.h"
51 #include "dialog.h"
52 #include "xutil.h"
53 #include "xmodifier.h"
54 #include "main.h"
55 #include "event.h"
58 #define ICON_SIZE wPreferences.icon_size
60 /**** Local prototypes *****/
61 static void UnescapeWM_CLASS(const char *str, char **name, char **class);
63 /* XFetchName Wrapper */
64 Bool wFetchName(Display *dpy, Window win, char **winname)
66 XTextProperty text_prop;
67 char **list;
68 int num;
70 if (XGetWMName(dpy, win, &text_prop)) {
71 if (text_prop.value && text_prop.nitems > 0) {
72 if (text_prop.encoding == XA_STRING) {
73 *winname = wstrdup((char *)text_prop.value);
74 XFree(text_prop.value);
75 } else {
76 text_prop.nitems = strlen((char *)text_prop.value);
77 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >=
78 Success && num > 0 && *list) {
79 XFree(text_prop.value);
80 *winname = wstrdup(*list);
81 XFreeStringList(list);
82 } else {
83 *winname = wstrdup((char *)text_prop.value);
84 XFree(text_prop.value);
87 } else {
88 /* the title is set, but it was set to none */
89 *winname = wstrdup("");
91 return True;
92 } else {
93 /* the hint is probably not set */
94 *winname = NULL;
96 return False;
100 /* XGetIconName Wrapper */
101 Bool wGetIconName(Display *dpy, Window win, char **iconname)
103 XTextProperty text_prop;
104 char **list;
105 int num;
107 if (XGetWMIconName(dpy, win, &text_prop) != 0 && text_prop.value && text_prop.nitems > 0) {
108 if (text_prop.encoding == XA_STRING)
109 *iconname = (char *)text_prop.value;
110 else {
111 text_prop.nitems = strlen((char *)text_prop.value);
112 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >= Success && num > 0 && *list) {
113 XFree(text_prop.value);
114 *iconname = wstrdup(*list);
115 XFreeStringList(list);
116 } else
117 *iconname = (char *)text_prop.value;
119 return True;
121 *iconname = NULL;
122 return False;
125 static void eatExpose(void)
127 XEvent event, foo;
129 /* compress all expose events into a single one */
131 if (XCheckMaskEvent(dpy, ExposureMask, &event)) {
132 /* ignore other exposure events for this window */
133 while (XCheckWindowEvent(dpy, event.xexpose.window, ExposureMask, &foo)) ;
134 /* eat exposes for other windows */
135 eatExpose();
137 event.xexpose.count = 0;
138 XPutBackEvent(dpy, &event);
142 void move_window(Window win, int from_x, int from_y, int to_x, int to_y)
144 #ifdef ANIMATIONS
145 if (wPreferences.no_animations)
146 XMoveWindow(dpy, win, to_x, to_y);
147 else
148 SlideWindow(win, from_x, from_y, to_x, to_y);
149 #else
150 XMoveWindow(dpy, win, to_x, to_y);
151 #endif
154 void SlideWindow(Window win, int from_x, int from_y, int to_x, int to_y)
156 Window *wins[1] = { &win };
157 SlideWindows(wins, 1, from_x, from_y, to_x, to_y);
160 /* wins is an array of Window, sorted from left to right, the first is
161 * going to be moved from (from_x,from_y) to (to_x,to_y) and the
162 * following windows are going to be offset by (ICON_SIZE*i,0) */
163 void SlideWindows(Window *wins[], int n, int from_x, int from_y, int to_x, int to_y)
165 time_t time0 = time(NULL);
166 float dx, dy, x = from_x, y = from_y, px, py;
167 Bool is_dx_nul, is_dy_nul;
168 int dx_is_bigger = 0, dx_int, dy_int;
169 int slide_delay, slide_steps, slide_slowdown;
170 int i;
172 /* animation parameters */
173 static const struct {
174 int delay;
175 int steps;
176 int slowdown;
177 } apars[5] = {
178 {ICON_SLIDE_DELAY_UF, ICON_SLIDE_STEPS_UF, ICON_SLIDE_SLOWDOWN_UF},
179 {ICON_SLIDE_DELAY_F, ICON_SLIDE_STEPS_F, ICON_SLIDE_SLOWDOWN_F},
180 {ICON_SLIDE_DELAY_M, ICON_SLIDE_STEPS_M, ICON_SLIDE_SLOWDOWN_M},
181 {ICON_SLIDE_DELAY_S, ICON_SLIDE_STEPS_S, ICON_SLIDE_SLOWDOWN_S},
182 {ICON_SLIDE_DELAY_US, ICON_SLIDE_STEPS_US, ICON_SLIDE_SLOWDOWN_US}
185 slide_slowdown = apars[(int)wPreferences.icon_slide_speed].slowdown;
186 slide_steps = apars[(int)wPreferences.icon_slide_speed].steps;
187 slide_delay = apars[(int)wPreferences.icon_slide_speed].delay;
189 dx_int = to_x - from_x;
190 dy_int = to_y - from_y;
191 is_dx_nul = (dx_int == 0);
192 is_dy_nul = (dy_int == 0);
193 dx = (float) dx_int;
194 dy = (float) dy_int;
196 if (abs(dx_int) > abs(dy_int)) {
197 dx_is_bigger = 1;
200 if (dx_is_bigger) {
201 px = dx / slide_slowdown;
202 if (px < slide_steps && px > 0)
203 px = slide_steps;
204 else if (px > -slide_steps && px < 0)
205 px = -slide_steps;
206 py = (is_dx_nul ? 0.0 : px * dy / dx);
207 } else {
208 py = dy / slide_slowdown;
209 if (py < slide_steps && py > 0)
210 py = slide_steps;
211 else if (py > -slide_steps && py < 0)
212 py = -slide_steps;
213 px = (is_dy_nul ? 0.0 : py * dx / dy);
216 while (((int)x) != to_x ||
217 ((int)y) != to_y) {
218 x += px;
219 y += py;
220 if ((px < 0 && (int)x < to_x) || (px > 0 && (int)x > to_x))
221 x = (float)to_x;
222 if ((py < 0 && (int)y < to_y) || (py > 0 && (int)y > to_y))
223 y = (float)to_y;
225 if (dx_is_bigger) {
226 px = px * (1.0 - 1 / (float)slide_slowdown);
227 if (px < slide_steps && px > 0)
228 px = slide_steps;
229 else if (px > -slide_steps && px < 0)
230 px = -slide_steps;
231 py = (is_dx_nul ? 0.0 : px * dy / dx);
232 } else {
233 py = py * (1.0 - 1 / (float)slide_slowdown);
234 if (py < slide_steps && py > 0)
235 py = slide_steps;
236 else if (py > -slide_steps && py < 0)
237 py = -slide_steps;
238 px = (is_dy_nul ? 0.0 : py * dx / dy);
241 for (i = 0; i < n; i++) {
242 XMoveWindow(dpy, *wins[i], (int)x + i * ICON_SIZE, (int)y);
244 XFlush(dpy);
245 if (slide_delay > 0) {
246 wusleep(slide_delay * 1000L);
247 } else {
248 wusleep(1000L);
250 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
251 break;
253 for (i = 0; i < n; i++) {
254 XMoveWindow(dpy, *wins[i], to_x + i * ICON_SIZE, to_y);
257 XSync(dpy, 0);
258 /* compress expose events */
259 eatExpose();
262 char *ShrinkString(WMFont *font, const char *string, int width)
264 int w, w1 = 0;
265 int p;
266 char *pos;
267 char *text;
268 int p1, p2, t;
270 p = strlen(string);
271 w = WMWidthOfString(font, string, p);
272 text = wmalloc(strlen(string) + 8);
273 strcpy(text, string);
274 if (w <= width)
275 return text;
277 pos = strchr(text, ' ');
278 if (!pos)
279 pos = strchr(text, ':');
281 if (pos) {
282 *pos = 0;
283 p = strlen(text);
284 w1 = WMWidthOfString(font, text, p);
285 if (w1 > width) {
286 p = 0;
287 *pos = ' ';
288 *text = 0;
289 } else {
290 *pos = 0;
291 width -= w1;
292 p++;
294 string += p;
295 p = strlen(string);
296 } else {
297 *text = 0;
299 strcat(text, "...");
300 width -= WMWidthOfString(font, "...", 3);
302 p1 = 0;
303 p2 = p;
304 t = (p2 - p1) / 2;
305 while (p2 > p1 && p1 != t) {
306 w = WMWidthOfString(font, &string[p - t], t);
307 if (w > width) {
308 p2 = t;
309 t = p1 + (p2 - p1) / 2;
310 } else if (w < width) {
311 p1 = t;
312 t = p1 + (p2 - p1) / 2;
313 } else
314 p2 = p1 = t;
316 strcat(text, &string[p - p1]);
318 return text;
321 char *FindImage(const char *paths, const char *file)
323 char *tmp, *path = NULL;
325 tmp = strrchr(file, ':');
326 if (tmp) {
327 *tmp = 0;
328 path = wfindfile(paths, file);
329 *tmp = ':';
331 if (!tmp || !path)
332 path = wfindfile(paths, file);
334 return path;
337 static void timeoutHandler(void *data)
339 *(int *)data = 1;
342 static char *getTextSelection(WScreen * screen, Atom selection)
344 int buffer = -1;
346 switch (selection) {
347 case XA_CUT_BUFFER0:
348 buffer = 0;
349 break;
350 case XA_CUT_BUFFER1:
351 buffer = 1;
352 break;
353 case XA_CUT_BUFFER2:
354 buffer = 2;
355 break;
356 case XA_CUT_BUFFER3:
357 buffer = 3;
358 break;
359 case XA_CUT_BUFFER4:
360 buffer = 4;
361 break;
362 case XA_CUT_BUFFER5:
363 buffer = 5;
364 break;
365 case XA_CUT_BUFFER6:
366 buffer = 6;
367 break;
368 case XA_CUT_BUFFER7:
369 buffer = 7;
370 break;
372 if (buffer >= 0) {
373 char *data;
374 int size;
376 data = XFetchBuffer(dpy, &size, buffer);
378 return data;
379 } else {
380 char *data;
381 int bits;
382 Atom rtype;
383 unsigned long len, bytes;
384 WMHandlerID timer;
385 int timeout = 0;
386 XEvent ev;
387 static Atom clipboard = 0;
389 if (!clipboard)
390 clipboard = XInternAtom(dpy, "CLIPBOARD", False);
392 XDeleteProperty(dpy, screen->info_window, clipboard);
394 XConvertSelection(dpy, selection, XA_STRING, clipboard, screen->info_window, CurrentTime);
396 timer = WMAddTimerHandler(1000, timeoutHandler, &timeout);
398 while (!XCheckTypedWindowEvent(dpy, screen->info_window, SelectionNotify, &ev) && !timeout) ;
400 if (!timeout) {
401 WMDeleteTimerHandler(timer);
402 } else {
403 wwarning("selection retrieval timed out");
404 return NULL;
407 /* nobody owns the selection or the current owner has
408 * nothing to do with what we need */
409 if (ev.xselection.property == None) {
410 return NULL;
413 if (XGetWindowProperty(dpy, screen->info_window,
414 clipboard, 0, 1024,
415 False, XA_STRING, &rtype, &bits, &len,
416 &bytes, (unsigned char **)&data) != Success) {
417 return NULL;
419 if (rtype != XA_STRING || bits != 8) {
420 wwarning("invalid data in text selection");
421 if (data)
422 XFree(data);
423 return NULL;
425 return data;
429 static char *getselection(WScreen * scr)
431 char *tmp;
433 tmp = getTextSelection(scr, XA_PRIMARY);
434 if (!tmp)
435 tmp = getTextSelection(scr, XA_CUT_BUFFER0);
436 return tmp;
439 static char*
440 parseuserinputpart(const char *line, int *ptr, const char *endchars)
442 int depth = 0, begin;
443 char *value = NULL;
444 begin = ++*ptr;
446 while(line[*ptr] != '\0') {
447 if(line[*ptr] == '(') {
448 ++depth;
449 } else if(depth > 0 && line[*ptr] == ')') {
450 --depth;
451 } else if(depth == 0 && strchr(endchars, line[*ptr]) != NULL) {
452 value = wmalloc(*ptr - begin + 1);
453 strncpy(value, line + begin, *ptr - begin);
454 value[*ptr - begin] = '\0';
455 break;
457 ++*ptr;
460 return value;
463 static char*
464 getuserinput(WScreen *scr, const char *line, int *ptr, Bool advanced)
466 char *ret = NULL, *title = NULL, *prompt = NULL, *name = NULL;
467 int rv;
469 if(line[*ptr] == '(')
470 title = parseuserinputpart(line, ptr, ",)");
471 if(title != NULL && line[*ptr] == ',')
472 prompt = parseuserinputpart(line, ptr, ",)");
473 if(prompt != NULL && line[*ptr] == ',')
474 name = parseuserinputpart(line, ptr, ")");
476 if(advanced)
477 rv = wAdvancedInputDialog(scr,
478 title ? _(title):_("Program Arguments"),
479 prompt ? _(prompt):_("Enter command arguments:"),
480 name, &ret);
481 else
482 rv = wInputDialog(scr,
483 title ? _(title):_("Program Arguments"),
484 prompt ? _(prompt):_("Enter command arguments:"),
485 &ret);
487 if(title) wfree(title);
488 if(prompt) wfree(prompt);
489 if(name) wfree(name);
491 return rv ? ret : NULL;
494 #define S_NORMAL 0
495 #define S_ESCAPE 1
496 #define S_OPTION 2
499 * state input new-state output
500 * NORMAL % OPTION <nil>
501 * NORMAL \ ESCAPE <nil>
502 * NORMAL etc. NORMAL <input>
503 * ESCAPE any NORMAL <input>
504 * OPTION s NORMAL <selection buffer>
505 * OPTION w NORMAL <selected window id>
506 * OPTION a NORMAL <input text>
507 * OPTION d NORMAL <OffiX DND selection object>
508 * OPTION W NORMAL <current workspace>
509 * OPTION etc. NORMAL %<input>
511 #define TMPBUFSIZE 64
512 char *ExpandOptions(WScreen *scr, const char *cmdline)
514 int ptr, optr, state, len, olen;
515 char *out, *nout;
516 char *selection = NULL;
517 char *user_input = NULL;
518 char tmpbuf[TMPBUFSIZE];
519 int slen;
521 len = strlen(cmdline);
522 olen = len + 1;
523 out = malloc(olen);
524 if (!out) {
525 wwarning(_("out of memory during expansion of \"%s\""), cmdline);
526 return NULL;
528 *out = 0;
529 ptr = 0; /* input line pointer */
530 optr = 0; /* output line pointer */
531 state = S_NORMAL;
532 while (ptr < len) {
533 switch (state) {
534 case S_NORMAL:
535 switch (cmdline[ptr]) {
536 case '\\':
537 state = S_ESCAPE;
538 break;
539 case '%':
540 state = S_OPTION;
541 break;
542 default:
543 state = S_NORMAL;
544 out[optr++] = cmdline[ptr];
545 break;
547 break;
548 case S_ESCAPE:
549 switch (cmdline[ptr]) {
550 case 'n':
551 out[optr++] = 10;
552 break;
554 case 'r':
555 out[optr++] = 13;
556 break;
558 case 't':
559 out[optr++] = 9;
560 break;
562 default:
563 out[optr++] = cmdline[ptr];
565 state = S_NORMAL;
566 break;
567 case S_OPTION:
568 state = S_NORMAL;
569 switch (cmdline[ptr]) {
570 case 'w':
571 if (scr->focused_window && scr->focused_window->flags.focused) {
572 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x",
573 (unsigned int)scr->focused_window->client_win);
574 slen = strlen(tmpbuf);
575 olen += slen;
576 nout = realloc(out, olen);
577 if (!nout) {
578 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%w", cmdline);
579 goto error;
581 out = nout;
582 strcat(out, tmpbuf);
583 optr += slen;
584 } else {
585 out[optr++] = ' ';
587 break;
589 case 'W':
590 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x", (unsigned int)scr->current_workspace + 1);
591 slen = strlen(tmpbuf);
592 olen += slen;
593 nout = realloc(out, olen);
594 if (!nout) {
595 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%W", cmdline);
596 goto error;
598 out = nout;
599 strcat(out, tmpbuf);
600 optr += slen;
601 break;
603 case 'a':
604 case 'A':
605 ptr++;
606 user_input = getuserinput(scr, cmdline, &ptr, cmdline[ptr-1] == 'A');
607 if (user_input) {
608 slen = strlen(user_input);
609 olen += slen;
610 nout = realloc(out, olen);
611 if (!nout) {
612 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%a", cmdline);
613 goto error;
615 out = nout;
616 strcat(out, user_input);
617 optr += slen;
618 } else {
619 /* Not an error, but user has Canceled the dialog box.
620 * This will make the command to not be performed. */
621 goto error;
623 break;
625 #ifdef USE_DOCK_XDND
626 case 'd':
627 if (!scr->xdestring) {
628 scr->flags.dnd_data_convertion_status = 1;
629 goto error;
631 slen = strlen(scr->xdestring);
632 olen += slen;
633 nout = realloc(out, olen);
634 if (!nout) {
635 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%d", cmdline);
636 goto error;
638 out = nout;
639 strcat(out, scr->xdestring);
640 optr += slen;
641 break;
642 #endif /* USE_DOCK_XDND */
644 case 's':
645 if (!selection) {
646 selection = getselection(scr);
648 if (!selection) {
649 wwarning(_("selection not available"));
650 goto error;
652 slen = strlen(selection);
653 olen += slen;
654 nout = realloc(out, olen);
655 if (!nout) {
656 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%s", cmdline);
657 goto error;
659 out = nout;
660 strcat(out, selection);
661 optr += slen;
662 break;
664 default:
665 out[optr++] = '%';
666 out[optr++] = cmdline[ptr];
668 break;
670 out[optr] = 0;
671 ptr++;
673 if (selection)
674 XFree(selection);
675 return out;
677 error:
678 wfree(out);
679 if (selection)
680 XFree(selection);
681 return NULL;
684 void ParseWindowName(WMPropList *value, char **winstance, char **wclass, const char *where)
686 char *name;
688 *winstance = *wclass = NULL;
690 if (!WMIsPLString(value)) {
691 wwarning(_("bad window name value in %s state info"), where);
692 return;
695 name = WMGetFromPLString(value);
696 if (!name || strlen(name) == 0) {
697 wwarning(_("bad window name value in %s state info"), where);
698 return;
701 UnescapeWM_CLASS(name, winstance, wclass);
704 #if 0
705 static char *keysymToString(KeySym keysym, unsigned int state)
707 XKeyEvent kev;
708 char *buf = wmalloc(20);
709 int count;
711 kev.display = dpy;
712 kev.type = KeyPress;
713 kev.send_event = False;
714 kev.window = DefaultRootWindow(dpy);
715 kev.root = DefaultRootWindow(dpy);
716 kev.same_screen = True;
717 kev.subwindow = kev.root;
718 kev.serial = 0x12344321;
719 kev.time = CurrentTime;
720 kev.state = state;
721 kev.keycode = XKeysymToKeycode(dpy, keysym);
722 count = XLookupString(&kev, buf, 19, NULL, NULL);
723 buf[count] = 0;
725 return buf;
727 #endif
729 char *GetShortcutString(const char *shortcut)
731 char *buffer = NULL;
732 char *k, *tmp, *text;
734 tmp = text = wstrdup(shortcut);
736 /* get modifiers */
737 while ((k = strchr(text, '+')) != NULL) {
738 int mod;
739 const char *lbl;
741 *k = 0;
742 mod = wXModifierFromKey(text);
743 if (mod < 0) {
744 return wstrdup("bug");
746 lbl = wXModifierToShortcutLabel(mod);
747 if (lbl)
748 buffer = wstrappend(buffer, lbl);
749 else
750 buffer = wstrappend(buffer, text);
751 text = k + 1;
754 buffer = wstrappend(buffer, text);
755 wfree(tmp);
757 return buffer;
760 char *GetShortcutKey(WShortKey key)
762 const char *key_name;
763 char buffer[256];
764 char *wr;
766 void append_string(const char *text)
768 const char *string = text;
770 while (*string) {
771 if (wr >= buffer + sizeof(buffer) - 1)
772 break;
773 *wr++ = *string++;
777 void append_modifier(int modifier_index, const char *fallback_name)
779 if (wPreferences.modifier_labels[modifier_index]) {
780 append_string(wPreferences.modifier_labels[modifier_index]);
781 } else {
782 append_string(fallback_name);
786 key_name = XKeysymToString(XkbKeycodeToKeysym(dpy, key.keycode, 0, 0));
787 if (!key_name)
788 return NULL;
790 wr = buffer;
791 if (key.modifier & ControlMask) append_modifier(1, "Control+");
792 if (key.modifier & ShiftMask) append_modifier(0, "Shift+");
793 if (key.modifier & Mod1Mask) append_modifier(2, "Mod1+");
794 if (key.modifier & Mod2Mask) append_modifier(3, "Mod2+");
795 if (key.modifier & Mod3Mask) append_modifier(4, "Mod3+");
796 if (key.modifier & Mod4Mask) append_modifier(5, "Mod4+");
797 if (key.modifier & Mod5Mask) append_modifier(6, "Mod5+");
798 append_string(key_name);
799 *wr = '\0';
801 return GetShortcutString(buffer);
804 char *EscapeWM_CLASS(const char *name, const char *class)
806 char *ret;
807 char *ename = NULL, *eclass = NULL;
808 int i, j, l;
810 if (!name && !class)
811 return NULL;
813 if (name) {
814 l = strlen(name);
815 ename = wmalloc(l * 2 + 1);
816 j = 0;
817 for (i = 0; i < l; i++) {
818 if (name[i] == '\\') {
819 ename[j++] = '\\';
820 } else if (name[i] == '.') {
821 ename[j++] = '\\';
823 ename[j++] = name[i];
825 ename[j] = 0;
827 if (class) {
828 l = strlen(class);
829 eclass = wmalloc(l * 2 + 1);
830 j = 0;
831 for (i = 0; i < l; i++) {
832 if (class[i] == '\\') {
833 eclass[j++] = '\\';
834 } else if (class[i] == '.') {
835 eclass[j++] = '\\';
837 eclass[j++] = class[i];
839 eclass[j] = 0;
842 if (ename && eclass) {
843 int len = strlen(ename) + strlen(eclass) + 4;
844 ret = wmalloc(len);
845 snprintf(ret, len, "%s.%s", ename, eclass);
846 wfree(ename);
847 wfree(eclass);
848 } else if (ename) {
849 ret = wstrdup(ename);
850 wfree(ename);
851 } else {
852 ret = wstrdup(eclass);
853 wfree(eclass);
856 return ret;
859 static void UnescapeWM_CLASS(const char *str, char **name, char **class)
861 int i, j, k, dot;
862 int length_of_name;
864 j = strlen(str);
866 /* separate string in 2 parts */
867 length_of_name = 0;
868 dot = -1;
869 for (i = 0; i < j; i++, length_of_name++) {
870 if (str[i] == '\\') {
871 i++;
872 continue;
873 } else if (str[i] == '.') {
874 dot = i;
875 break;
879 /* unescape the name */
880 if (length_of_name > 0) {
881 *name = wmalloc(length_of_name + 1);
882 for (i = 0, k = 0; i < dot; i++) {
883 if (str[i] != '\\')
884 (*name)[k++] = str[i];
886 (*name)[k] = '\0';
887 } else {
888 *name = NULL;
891 /* unescape the class */
892 if (dot < j-1) {
893 *class = wmalloc(j - (dot + 1) + 1);
894 for (i = dot + 1, k = 0; i < j; i++) {
895 if (str[i] != '\\')
896 (*class)[k++] = str[i];
898 (*class)[k] = 0;
899 } else {
900 *class = NULL;
904 static void track_bg_helper_death(pid_t pid, unsigned int status, void *client_data)
906 WScreen *scr = (WScreen *) client_data;
908 /* Parameter not used, but tell the compiler that it is ok */
909 (void) pid;
910 (void) status;
912 close(scr->helper_fd);
913 scr->helper_fd = 0;
914 scr->helper_pid = 0;
915 scr->flags.backimage_helper_launched = 0;
918 Bool start_bg_helper(WScreen *scr)
920 pid_t pid;
921 int filedes[2];
923 if (pipe(filedes) < 0) {
924 werror(_("%s failed, can't set workspace specific background image (%s)"),
925 "pipe()", strerror(errno));
926 return False;
929 pid = fork();
930 if (pid < 0) {
931 werror(_("%s failed, can't set workspace specific background image (%s)"),
932 "fork()", strerror(errno));
933 close(filedes[0]);
934 close(filedes[1]);
935 return False;
937 } else if (pid == 0) {
938 const char *dither;
940 /* We don't need this side of the pipe in the child process */
941 close(filedes[1]);
943 SetupEnvironment(scr);
945 close(STDIN_FILENO);
946 if (dup2(filedes[0], STDIN_FILENO) < 0) {
947 werror(_("%s failed, can't set workspace specific background image (%s)"),
948 "dup2()", strerror(errno));
949 exit(1);
951 close(filedes[0]);
953 dither = wPreferences.no_dithering ? "-m" : "-d";
954 if (wPreferences.smooth_workspace_back)
955 execlp("wmsetbg", "wmsetbg", "-helper", "-S", dither, NULL);
956 else
957 execlp("wmsetbg", "wmsetbg", "-helper", dither, NULL);
959 werror(_("could not execute \"%s\": %s"), "wmsetbg", strerror(errno));
960 exit(1);
962 } else {
963 /* We don't need this side of the pipe in the parent process */
964 close(filedes[0]);
966 if (fcntl(filedes[1], F_SETFD, FD_CLOEXEC) < 0)
967 wwarning(_("could not set close-on-exec flag for bg_helper's communication file handle (%s)"),
968 strerror(errno));
970 scr->helper_fd = filedes[1];
971 scr->helper_pid = pid;
972 scr->flags.backimage_helper_launched = 1;
974 wAddDeathHandler(pid, track_bg_helper_death, scr);
976 return True;
980 void SendHelperMessage(WScreen *scr, char type, int workspace, const char *msg)
982 char *buffer;
983 int len;
984 int i;
985 char buf[16];
987 if (!scr->flags.backimage_helper_launched) {
988 return;
991 len = (msg ? strlen(msg) : 0) + (workspace >= 0 ? 4 : 0) + 1;
992 buffer = wmalloc(len + 5);
993 snprintf(buf, sizeof(buf), "%4i", len);
994 memcpy(buffer, buf, 4);
995 buffer[4] = type;
996 i = 5;
997 if (workspace >= 0) {
998 snprintf(buf, sizeof(buf), "%4i", workspace);
999 memcpy(&buffer[i], buf, 4);
1000 i += 4;
1001 buffer[i] = 0;
1003 if (msg)
1004 strcpy(&buffer[i], msg);
1006 if (write(scr->helper_fd, buffer, len + 4) < 0) {
1007 werror(_("could not send message to background image helper"));
1009 wfree(buffer);
1012 Bool UpdateDomainFile(WDDomain * domain)
1014 struct stat stbuf;
1015 char path[PATH_MAX];
1016 WMPropList *shared_dict, *dict;
1017 Bool result, freeDict = False;
1019 dict = domain->dictionary;
1020 if (WMIsPLDictionary(domain->dictionary)) {
1021 /* retrieve global system dictionary */
1022 snprintf(path, sizeof(path), "%s/WindowMaker/%s", SYSCONFDIR, domain->domain_name);
1023 if (stat(path, &stbuf) >= 0) {
1024 shared_dict = WMReadPropListFromFile(path);
1025 if (shared_dict) {
1026 if (WMIsPLDictionary(shared_dict)) {
1027 freeDict = True;
1028 dict = WMDeepCopyPropList(domain->dictionary);
1029 WMSubtractPLDictionaries(dict, shared_dict, True);
1031 WMReleasePropList(shared_dict);
1036 result = WMWritePropListToFile(dict, domain->path);
1038 if (freeDict) {
1039 WMReleasePropList(dict);
1042 return result;
1045 char *StrConcatDot(const char *a, const char *b)
1047 int len;
1048 char *str;
1050 if (!a)
1051 a = "";
1052 if (!b)
1053 b = "";
1055 len = strlen(a) + strlen(b) + 4;
1056 str = wmalloc(len);
1058 snprintf(str, len, "%s.%s", a, b);
1060 return str;
1063 static char *getCommandForWindow(Window win, int elements)
1065 char **argv, *command = NULL;
1066 int argc;
1068 if (XGetCommand(dpy, win, &argv, &argc)) {
1069 if (argc > 0 && argv != NULL) {
1070 if (elements == 0)
1071 elements = argc;
1072 command = wtokenjoin(argv, WMIN(argc, elements));
1073 if (command[0] == 0) {
1074 wfree(command);
1075 command = NULL;
1078 if (argv) {
1079 XFreeStringList(argv);
1083 return command;
1086 /* Free result when done */
1087 char *GetCommandForWindow(Window win)
1089 return getCommandForWindow(win, 0);