remove drawstring plugins
[wmaker-crm.git] / src / misc.c
blobf8a65ff5ae8bce3cb5513c447571df9b53ea31b0
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
26 #include <sys/stat.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdarg.h>
32 #include <pwd.h>
33 #include <math.h>
34 #include <time.h>
36 #include <WUtil.h>
37 #include <wraster.h>
40 #include "WindowMaker.h"
41 #include "GNUstep.h"
42 #include "screen.h"
43 #include "wcore.h"
44 #include "window.h"
45 #include "framewin.h"
46 #include "funcs.h"
47 #include "defaults.h"
48 #include "dialog.h"
49 #include "xutil.h"
50 #include "xmodifier.h"
51 #include "plugin.h"
54 /**** global variables *****/
56 extern char *DisplayName;
58 extern WPreferences wPreferences;
60 extern Time LastTimestamp;
62 #ifdef OFFIX_DND
63 extern Atom _XA_DND_SELECTION;
64 #endif
67 #ifdef USECPP
68 static void
69 putdef(char *line, char *name, char *value)
71 if (!value) {
72 wwarning(_("could not define value for %s for cpp"), name);
73 return;
75 strcat(line, name);
76 strcat(line, value);
81 static void
82 putidef(char *line, char *name, int value)
84 char tmp[64];
85 sprintf(tmp, "%i", value);
86 strcat(line, name);
87 strcat(line, tmp);
91 static char*
92 username()
94 char *tmp;
96 tmp = getlogin();
97 if (!tmp) {
98 struct passwd *user;
100 user = getpwuid(getuid());
101 if (!user) {
102 wsyserror(_("could not get password entry for UID %i"), getuid());
103 return NULL;
105 if (!user->pw_name) {
106 return NULL;
107 } else {
108 return user->pw_name;
111 return tmp;
114 char *
115 MakeCPPArgs(char *path)
117 int i;
118 char buffer[MAXLINE], *buf, *line;
119 Visual *visual;
120 char *tmp;
122 line = wmalloc(MAXLINE);
123 *line = 0;
124 i=1;
125 if ((buf=getenv("HOSTNAME"))!=NULL) {
126 if (buf[0]=='(') {
127 wwarning(_("your machine is misconfigured. HOSTNAME is set to %s"),
128 buf);
129 } else
130 putdef(line, " -DHOST=", buf);
131 } else if ((buf=getenv("HOST"))!=NULL) {
132 if (buf[0]=='(') {
133 wwarning(_("your machine is misconfigured. HOST is set to %s"),
134 buf);
135 } else
136 putdef(line, " -DHOST=", buf);
138 buf = username();
139 if (buf)
140 putdef(line, " -DUSER=", buf);
141 putidef(line, " -DUID=", getuid());
142 buf = XDisplayName(DisplayString(dpy));
143 putdef(line, " -DDISPLAY=", buf);
144 putdef(line, " -DWM_VERSION=", VERSION);
146 visual = DefaultVisual(dpy, DefaultScreen(dpy));
147 putidef(line, " -DVISUAL=", visual->class);
149 putidef(line, " -DDEPTH=", DefaultDepth(dpy, DefaultScreen(dpy)));
151 putidef(line, " -DSCR_WIDTH=", WidthOfScreen(DefaultScreenOfDisplay(dpy)));
152 putidef(line, " -DSCR_HEIGHT=",
153 HeightOfScreen(DefaultScreenOfDisplay(dpy)));
155 /* put the dir where the menu is being read from to the
156 * search path */
157 if (path) {
158 tmp = wstrdup(path);
159 buf = strchr(tmp+1, ' ');
160 if (buf) {
161 *buf = 0;
163 buf = strrchr(tmp, '/');
164 if (buf) {
165 *buf = 0; /* trunc filename */
166 putdef(line, " -I", tmp);
168 wfree(tmp);
172 /* this should be done just once, but it works this way */
173 strcpy(buffer, DEF_CONFIG_PATHS);
174 buf = strtok(buffer, ":");
176 do {
177 char fullpath[MAXLINE];
179 if (buf[0]!='~') {
180 strcpy(fullpath, buf);
181 } else {
182 char * wgethomedir();
183 /* home is statically allocated. Don't free it! */
184 char *home = wgethomedir();
186 strcpy(fullpath, home);
187 strcat(fullpath, &(buf[1]));
190 putdef(line, " -I", fullpath);
192 } while ((buf = strtok(NULL, ":"))!=NULL);
194 #undef arg
195 #ifdef DEBUG
196 puts("CPP ARGS");
197 puts(line);
198 #endif
199 return line;
201 #endif /* USECPP */
206 #if 0
208 * Is win2 below win1?
210 static Bool
211 isBelow(WWindow *win1, WWindow *win2)
213 int i;
214 WCoreWindow *tmp;
216 tmp = win1->frame->core->stacking->under;
217 while (tmp) {
218 if (tmp == win2->frame->core)
219 return True;
220 tmp = tmp->stacking->under;
223 for (i=win1->frame->core->stacking->window_level-1; i>=0; i--) {
224 tmp = win1->screen_ptr->stacking_list[i];
225 while (tmp) {
226 if (tmp == win2->frame->core)
227 return True;
228 tmp = tmp->stacking->under;
231 return True;
233 #endif
238 * XFetchName Wrapper
241 Bool
242 wFetchName(dpy, win, winname)
243 Display *dpy;
244 Window win;
245 char **winname;
247 XTextProperty text_prop;
248 char **list;
249 int num;
251 if (XGetWMName(dpy, win, &text_prop)) {
252 if (text_prop.value && text_prop.nitems > 0) {
253 if (text_prop.encoding == XA_STRING) {
254 *winname = wstrdup((char *)text_prop.value);
255 XFree(text_prop.value);
256 } else {
257 text_prop.nitems = strlen((char *)text_prop.value);
258 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >=
259 Success && num > 0 && *list) {
260 XFree(text_prop.value);
261 *winname = wstrdup(*list);
262 XFreeStringList(list);
263 } else {
264 *winname = wstrdup((char *)text_prop.value);
265 XFree(text_prop.value);
268 } else {
269 /* the title is set, but it was set to none */
270 *winname = wstrdup("");
272 return True;
273 } else {
274 /* the hint is probably not set */
275 *winname = NULL;
277 return False;
282 * XGetIconName Wrapper
286 Bool
287 wGetIconName(dpy, win, iconname)
288 Display *dpy;
289 Window win;
290 char **iconname;
292 XTextProperty text_prop;
293 char **list;
294 int num;
296 if (XGetWMIconName(dpy, win, &text_prop) != 0 && text_prop.value
297 && text_prop.nitems > 0) {
298 if (text_prop.encoding == XA_STRING)
299 *iconname = (char *)text_prop.value;
300 else {
301 text_prop.nitems = strlen((char *)text_prop.value);
302 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >=
303 Success && num > 0 && *list) {
304 XFree(text_prop.value);
305 *iconname = wstrdup(*list);
306 XFreeStringList(list);
307 } else
308 *iconname = (char *)text_prop.value;
310 return True;
312 *iconname = NULL;
313 return False;
317 static void
318 eatExpose()
320 XEvent event, foo;
322 /* compress all expose events into a single one */
324 if (XCheckMaskEvent(dpy, ExposureMask, &event)) {
325 /* ignore other exposure events for this window */
326 while (XCheckWindowEvent(dpy, event.xexpose.window, ExposureMask,
327 &foo));
328 /* eat exposes for other windows */
329 eatExpose();
331 event.xexpose.count = 0;
332 XPutBackEvent(dpy, &event);
337 void
338 SlideWindow(Window win, int from_x, int from_y, int to_x, int to_y)
340 time_t time0 = time(NULL);
341 float dx, dy, x=from_x, y=from_y, sx, sy, px, py;
342 int dx_is_bigger=0;
344 /* animation parameters */
345 static struct {
346 int delay;
347 int steps;
348 int slowdown;
349 } apars[5] = {
350 {ICON_SLIDE_DELAY_UF, ICON_SLIDE_STEPS_UF, ICON_SLIDE_SLOWDOWN_UF},
351 {ICON_SLIDE_DELAY_F, ICON_SLIDE_STEPS_F, ICON_SLIDE_SLOWDOWN_F},
352 {ICON_SLIDE_DELAY_M, ICON_SLIDE_STEPS_M, ICON_SLIDE_SLOWDOWN_M},
353 {ICON_SLIDE_DELAY_S, ICON_SLIDE_STEPS_S, ICON_SLIDE_SLOWDOWN_S},
354 {ICON_SLIDE_DELAY_U, ICON_SLIDE_STEPS_U, ICON_SLIDE_SLOWDOWN_U}};
358 dx = (float)(to_x-from_x);
359 dy = (float)(to_y-from_y);
360 sx = (dx == 0 ? 0 : fabs(dx)/dx);
361 sy = (dy == 0 ? 0 : fabs(dy)/dy);
363 if (fabs(dx) > fabs(dy)) {
364 dx_is_bigger = 1;
367 if (dx_is_bigger) {
368 px = dx / apars[(int)wPreferences.icon_slide_speed].slowdown;
369 if (px < apars[(int)wPreferences.icon_slide_speed].steps && px > 0)
370 px = apars[(int)wPreferences.icon_slide_speed].steps;
371 else if (px > -apars[(int)wPreferences.icon_slide_speed].steps && px < 0)
372 px = -apars[(int)wPreferences.icon_slide_speed].steps;
373 py = (sx == 0 ? 0 : px*dy/dx);
374 } else {
375 py = dy / apars[(int)wPreferences.icon_slide_speed].slowdown;
376 if (py < apars[(int)wPreferences.icon_slide_speed].steps && py > 0)
377 py = apars[(int)wPreferences.icon_slide_speed].steps;
378 else if (py > -apars[(int)wPreferences.icon_slide_speed].steps && py < 0)
379 py = -apars[(int)wPreferences.icon_slide_speed].steps;
380 px = (sy == 0 ? 0 : py*dx/dy);
383 while (x != to_x || y != to_y) {
384 x += px;
385 y += py;
386 if ((px<0 && (int)x < to_x) || (px>0 && (int)x > to_x))
387 x = (float)to_x;
388 if ((py<0 && (int)y < to_y) || (py>0 && (int)y > to_y))
389 y = (float)to_y;
391 if (dx_is_bigger) {
392 px = px * (1.0 - 1/(float)apars[(int)wPreferences.icon_slide_speed].slowdown);
393 if (px < apars[(int)wPreferences.icon_slide_speed].steps && px > 0)
394 px = apars[(int)wPreferences.icon_slide_speed].steps;
395 else if (px > -apars[(int)wPreferences.icon_slide_speed].steps && px < 0)
396 px = -apars[(int)wPreferences.icon_slide_speed].steps;
397 py = (sx == 0 ? 0 : px*dy/dx);
398 } else {
399 py = py * (1.0 - 1/(float)apars[(int)wPreferences.icon_slide_speed].slowdown);
400 if (py < apars[(int)wPreferences.icon_slide_speed].steps && py > 0)
401 py = apars[(int)wPreferences.icon_slide_speed].steps;
402 else if (py > -apars[(int)wPreferences.icon_slide_speed].steps && py < 0)
403 py = -apars[(int)wPreferences.icon_slide_speed].steps;
404 px = (sy == 0 ? 0 : py*dx/dy);
407 XMoveWindow(dpy, win, (int)x, (int)y);
408 XFlush(dpy);
409 if (apars[(int)wPreferences.icon_slide_speed].delay > 0) {
410 wusleep(apars[(int)wPreferences.icon_slide_speed].delay*1000L);
412 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
413 break;
415 XMoveWindow(dpy, win, to_x, to_y);
417 XSync(dpy, 0);
418 /* compress expose events */
419 eatExpose();
423 char*
424 ShrinkString(WMFont *font, char *string, int width)
426 int w, w1=0;
427 int p;
428 char *pos;
429 char *text;
430 int p1, p2, t;
432 if (wPreferences.multi_byte_text)
433 return wstrdup(string);
435 p = strlen(string);
436 w = WMWidthOfString(font, string, p);
437 text = wmalloc(strlen(string)+8);
438 strcpy(text, string);
439 if (w<=width)
440 return text;
442 pos = strchr(text, ' ');
443 if (!pos)
444 pos = strchr(text, ':');
446 if (pos) {
447 *pos = 0;
448 p = strlen(text);
449 w1 = WMWidthOfString(font, text, p);
450 if (w1 > width) {
451 w1 = 0;
452 p = 0;
453 *pos = ' ';
454 *text = 0;
455 } else {
456 *pos = 0;
457 width -= w1;
458 p++;
460 string += p;
461 p=strlen(string);
462 } else {
463 *text=0;
465 strcat(text, "...");
466 width -= WMWidthOfString(font, "...", 3);
467 pos = string;
468 p1=0;
469 p2=p;
470 t = (p2-p1)/2;
471 while (p2>p1 && p1!=t) {
472 w = WMWidthOfString(font, &string[p-t], t);
473 if (w>width) {
474 p2 = t;
475 t = p1+(p2-p1)/2;
476 } else if (w<width) {
477 p1 = t;
478 t = p1+(p2-p1)/2;
479 } else
480 p2=p1=t;
482 strcat(text, &string[p-p1]);
484 return text;
488 char*
489 FindImage(char *paths, char *file)
491 char *tmp, *path;
493 tmp = strrchr(file, ':');
494 if (tmp) {
495 *tmp = 0;
496 path = wfindfile(paths, file);
497 *tmp = ':';
499 if (!tmp || !path) {
500 path = wfindfile(paths, file);
503 return path;
507 static void
508 timeoutHandler(void *data)
510 *(int*)data = 1;
514 static char*
515 getTextSelection(WScreen *screen, Atom selection)
517 int buffer = -1;
519 switch (selection) {
520 case XA_CUT_BUFFER0:
521 buffer = 0;
522 break;
523 case XA_CUT_BUFFER1:
524 buffer = 1;
525 break;
526 case XA_CUT_BUFFER2:
527 buffer = 2;
528 break;
529 case XA_CUT_BUFFER3:
530 buffer = 3;
531 break;
532 case XA_CUT_BUFFER4:
533 buffer = 4;
534 break;
535 case XA_CUT_BUFFER5:
536 buffer = 5;
537 break;
538 case XA_CUT_BUFFER6:
539 buffer = 6;
540 break;
541 case XA_CUT_BUFFER7:
542 buffer = 7;
543 break;
545 if (buffer >= 0) {
546 char *data;
547 int size;
549 data = XFetchBuffer(dpy, &size, buffer);
551 return data;
552 } else {
553 char *data;
554 int bits;
555 Atom rtype;
556 unsigned long len, bytes;
557 WMHandlerID timer;
558 int timeout = 0;
559 XEvent ev;
560 static Atom clipboard = 0;
562 if (!clipboard)
563 clipboard = XInternAtom(dpy, "CLIPBOARD", False);
565 XDeleteProperty(dpy, screen->info_window, clipboard);
567 XConvertSelection(dpy, selection, XA_STRING,
568 clipboard, screen->info_window,
569 CurrentTime);
571 timer = WMAddTimerHandler(1000, timeoutHandler, &timeout);
573 while (!XCheckTypedWindowEvent(dpy, screen->info_window,
574 SelectionNotify, &ev) && !timeout);
576 if (!timeout) {
577 WMDeleteTimerHandler(timer);
578 } else {
579 wwarning("selection retrieval timed out");
580 return NULL;
583 /* nobody owns the selection or the current owner has
584 * nothing to do with what we need */
585 if (ev.xselection.property == None) {
586 return NULL;
589 if (XGetWindowProperty(dpy, screen->info_window,
590 clipboard, 0, 1024,
591 False, XA_STRING, &rtype, &bits, &len,
592 &bytes, (unsigned char**)&data)!=Success) {
593 return NULL;
595 if (rtype!=XA_STRING || bits!=8) {
596 wwarning("invalid data in text selection");
597 if (data)
598 XFree(data);
599 return NULL;
601 return data;
605 static char*
606 getselection(WScreen *scr)
608 char *tmp;
610 tmp = getTextSelection(scr, XA_PRIMARY);
611 if (!tmp)
612 tmp = getTextSelection(scr, XA_CUT_BUFFER0);
613 return tmp;
617 static char*
618 getuserinput(WScreen *scr, char *line, int *ptr)
620 char *ret;
621 char *title;
622 char *prompt;
623 int j, state;
624 int begin = 0;
625 char tbuffer[256], pbuffer[256];
627 title = _("Program Arguments");
628 prompt = _("Enter command arguments:");
629 ret = NULL;
631 #define _STARTING 0
632 #define _TITLE 1
633 #define _PROMPT 2
634 #define _DONE 3
636 state = _STARTING;
637 j = 0;
638 for (; line[*ptr]!=0 && state!=_DONE; (*ptr)++) {
639 switch (state) {
640 case _STARTING:
641 if (line[*ptr]=='(') {
642 state = _TITLE;
643 begin = *ptr+1;
644 } else {
645 state = _DONE;
647 break;
649 case _TITLE:
650 if (j <= 0 && line[*ptr]==',') {
652 j = 0;
653 if (*ptr > begin) {
654 strncpy(tbuffer, &line[begin], WMIN(*ptr-begin, 255));
655 tbuffer[WMIN(*ptr-begin, 255)] = 0;
656 title = (char*)tbuffer;
658 begin = *ptr+1;
659 state = _PROMPT;
661 } else if (j <= 0 && line[*ptr]==')') {
663 if (*ptr > begin) {
664 strncpy(tbuffer, &line[begin], WMIN(*ptr-begin, 255));
665 tbuffer[WMIN(*ptr-begin, 255)] = 0;
666 title = (char*)tbuffer;
668 state = _DONE;
670 } else if (line[*ptr]=='(') {
671 j++;
672 } else if (line[*ptr]==')') {
673 j--;
676 break;
678 case _PROMPT:
679 if (line[*ptr]==')' && j==0) {
681 if (*ptr-begin > 1) {
682 strncpy(pbuffer, &line[begin], WMIN(*ptr-begin, 255));
683 pbuffer[WMIN(*ptr-begin, 255)] = 0;
684 prompt = (char*)pbuffer;
686 state = _DONE;
687 } else if (line[*ptr]=='(')
688 j++;
689 else if (line[*ptr]==')')
690 j--;
691 break;
694 (*ptr)--;
695 #undef _STARTING
696 #undef _TITLE
697 #undef _PROMPT
698 #undef _DONE
700 if (!wInputDialog(scr, title, prompt, &ret))
701 return NULL;
702 else
703 return ret;
707 #ifdef OFFIX_DND
708 static char*
709 get_dnd_selection(WScreen *scr)
711 XTextProperty text_ret;
712 int result;
713 char **list;
714 char *flat_string;
715 int count;
717 result=XGetTextProperty(dpy, scr->root_win, &text_ret, _XA_DND_SELECTION);
719 if (result==0 || text_ret.value==NULL || text_ret.encoding==None
720 || text_ret.format==0 || text_ret.nitems == 0) {
721 wwarning(_("unable to get dropped data from DND drop"));
722 return NULL;
725 XTextPropertyToStringList(&text_ret, &list, &count);
727 if (!list || count<1) {
728 XFree(text_ret.value);
729 wwarning(_("error getting dropped data from DND drop"));
730 return NULL;
733 flat_string = wtokenjoin(list, count);
734 if (!flat_string) {
735 wwarning(_("out of memory while getting data from DND drop"));
738 XFreeStringList(list);
739 XFree(text_ret.value);
740 return flat_string;
742 #endif /* OFFIX_DND */
745 #define S_NORMAL 0
746 #define S_ESCAPE 1
747 #define S_OPTION 2
750 * state input new-state output
751 * NORMAL % OPTION <nil>
752 * NORMAL \ ESCAPE <nil>
753 * NORMAL etc. NORMAL <input>
754 * ESCAPE any NORMAL <input>
755 * OPTION s NORMAL <selection buffer>
756 * OPTION w NORMAL <selected window id>
757 * OPTION a NORMAL <input text>
758 * OPTION d NORMAL <OffiX DND selection object>
759 * OPTION W NORMAL <current workspace>
760 * OPTION etc. NORMAL %<input>
762 #define TMPBUFSIZE 64
763 char*
764 ExpandOptions(WScreen *scr, char *cmdline)
766 int ptr, optr, state, len, olen;
767 char *out, *nout;
768 char *selection=NULL;
769 char *user_input=NULL;
770 #if defined(OFFIX_DND) || defined(XDND)
771 char *dropped_thing=NULL;
772 #endif
773 char tmpbuf[TMPBUFSIZE];
774 int slen;
776 len = strlen(cmdline);
777 olen = len+1;
778 out = malloc(olen);
779 if (!out) {
780 wwarning(_("out of memory during expansion of \"%s\""));
781 return NULL;
783 *out = 0;
784 ptr = 0; /* input line pointer */
785 optr = 0; /* output line pointer */
786 state = S_NORMAL;
787 while (ptr < len) {
788 switch (state) {
789 case S_NORMAL:
790 switch (cmdline[ptr]) {
791 case '\\':
792 state = S_ESCAPE;
793 break;
794 case '%':
795 state = S_OPTION;
796 break;
797 default:
798 state = S_NORMAL;
799 out[optr++]=cmdline[ptr];
800 break;
802 break;
803 case S_ESCAPE:
804 switch (cmdline[ptr]) {
805 case 'n':
806 out[optr++]=10;
807 break;
809 case 'r':
810 out[optr++]=13;
811 break;
813 case 't':
814 out[optr++]=9;
815 break;
817 default:
818 out[optr++]=cmdline[ptr];
820 state = S_NORMAL;
821 break;
822 case S_OPTION:
823 state = S_NORMAL;
824 switch (cmdline[ptr]) {
825 case 'w':
826 if (scr->focused_window
827 && scr->focused_window->flags.focused) {
828 sprintf(tmpbuf, "0x%x",
829 (unsigned int)scr->focused_window->client_win);
830 slen = strlen(tmpbuf);
831 olen += slen;
832 nout = realloc(out,olen);
833 if (!nout) {
834 wwarning(_("out of memory during expansion of \"%w\""));
835 goto error;
837 out = nout;
838 strcat(out,tmpbuf);
839 optr+=slen;
840 } else {
841 out[optr++]=' ';
843 break;
845 case 'W':
846 sprintf(tmpbuf, "0x%x",
847 (unsigned int)scr->current_workspace + 1);
848 slen = strlen(tmpbuf);
849 olen += slen;
850 nout = realloc(out,olen);
851 if (!nout) {
852 wwarning(_("out of memory during expansion of \"%W\""));
853 goto error;
855 out = nout;
856 strcat(out,tmpbuf);
857 optr+=slen;
858 break;
860 case 'a':
861 ptr++;
862 user_input = getuserinput(scr, cmdline, &ptr);
863 if (user_input) {
864 slen = strlen(user_input);
865 olen += slen;
866 nout = realloc(out,olen);
867 if (!nout) {
868 wwarning(_("out of memory during expansion of \"%a\""));
869 goto error;
871 out = nout;
872 strcat(out,user_input);
873 optr+=slen;
874 } else {
875 /* Not an error, but user has Canceled the dialog box.
876 * This will make the command to not be performed. */
877 goto error;
879 break;
881 #if defined(OFFIX_DND) || defined(XDND)
882 case 'd':
883 #ifdef XDND
884 if(scr->xdestring) {
885 dropped_thing = wstrdup(scr->xdestring);
887 #endif
888 if (!dropped_thing) {
889 dropped_thing = get_dnd_selection(scr);
891 if (!dropped_thing) {
892 scr->flags.dnd_data_convertion_status = 1;
893 goto error;
895 slen = strlen(dropped_thing);
896 olen += slen;
897 nout = realloc(out,olen);
898 if (!nout) {
899 wwarning(_("out of memory during expansion of \"%d\""));
900 goto error;
902 out = nout;
903 strcat(out,dropped_thing);
904 optr+=slen;
905 break;
906 #endif /* OFFIX_DND */
908 case 's':
909 if (!selection) {
910 selection = getselection(scr);
912 if (!selection) {
913 wwarning(_("selection not available"));
914 goto error;
916 slen = strlen(selection);
917 olen += slen;
918 nout = realloc(out,olen);
919 if (!nout) {
920 wwarning(_("out of memory during expansion of \"%s\""));
921 goto error;
923 out = nout;
924 strcat(out,selection);
925 optr+=slen;
926 break;
928 default:
929 out[optr++]='%';
930 out[optr++]=cmdline[ptr];
932 break;
934 out[optr]=0;
935 ptr++;
937 if (selection)
938 XFree(selection);
939 return out;
941 error:
942 wfree(out);
943 if (selection)
944 XFree(selection);
945 return NULL;
949 /* We don't care for upper/lower case in comparing the keys; so we
950 have to define our own comparison function here */
951 BOOL
952 StringCompareHook(proplist_t pl1, proplist_t pl2)
954 char *str1, *str2;
956 str1 = PLGetString(pl1);
957 str2 = PLGetString(pl2);
959 if (strcasecmp(str1, str2)==0)
960 return YES;
961 else
962 return NO;
966 /* feof doesn't seem to work on pipes */
968 IsEof(FILE * stream)
970 static struct stat stinfo;
972 fstat(fileno(stream), &stinfo);
973 return ((S_ISFIFO(stinfo.st_dev) && stinfo.st_size == 0) ||
974 feof(stream));
978 void
979 ParseWindowName(proplist_t value, char **winstance, char **wclass, char *where)
981 char *name;
983 *winstance = *wclass = NULL;
985 if (!PLIsString(value)) {
986 wwarning(_("bad window name value in %s state info"), where);
987 return;
990 name = PLGetString(value);
991 if (!name || strlen(name)==0) {
992 wwarning(_("bad window name value in %s state info"), where);
993 return;
996 UnescapeWM_CLASS(name, winstance, wclass);
1000 #if 0
1001 static char*
1002 keysymToString(KeySym keysym, unsigned int state)
1004 XKeyEvent kev;
1005 char *buf = wmalloc(20);
1006 int count;
1008 kev.display = dpy;
1009 kev.type = KeyPress;
1010 kev.send_event = False;
1011 kev.window = DefaultRootWindow(dpy);
1012 kev.root = DefaultRootWindow(dpy);
1013 kev.same_screen = True;
1014 kev.subwindow = kev.root;
1015 kev.serial = 0x12344321;
1016 kev.time = CurrentTime;
1017 kev.state = state;
1018 kev.keycode = XKeysymToKeycode(dpy, keysym);
1019 count = XLookupString(&kev, buf, 19, NULL, NULL);
1020 buf[count] = 0;
1022 return buf;
1024 #endif
1027 char*
1028 GetShortcutString(char *text)
1030 char *buffer = NULL;
1031 char *k;
1032 int modmask = 0;
1033 /* KeySym ksym;*/
1034 int control = 0;
1035 char *tmp;
1037 tmp = text = wstrdup(text);
1039 /* get modifiers */
1040 while ((k = strchr(text, '+'))!=NULL) {
1041 int mod;
1043 *k = 0;
1044 mod = wXModifierFromKey(text);
1045 if (mod<0) {
1046 return wstrdup("bug");
1049 modmask |= mod;
1051 if (strcasecmp(text, "Meta")==0) {
1052 buffer = wstrappend(buffer, "M+");
1053 } else if (strcasecmp(text, "Alt")==0) {
1054 buffer = wstrappend(buffer, "A+");
1055 } else if (strcasecmp(text, "Shift")==0) {
1056 buffer = wstrappend(buffer, "Sh+");
1057 } else if (strcasecmp(text, "Mod1")==0) {
1058 buffer = wstrappend(buffer, "M1+");
1059 } else if (strcasecmp(text, "Mod2")==0) {
1060 buffer = wstrappend(buffer, "M2+");
1061 } else if (strcasecmp(text, "Mod3")==0) {
1062 buffer = wstrappend(buffer, "M3+");
1063 } else if (strcasecmp(text, "Mod4")==0) {
1064 buffer = wstrappend(buffer, "M4+");
1065 } else if (strcasecmp(text, "Mod5")==0) {
1066 buffer = wstrappend(buffer, "M5+");
1067 } else if (strcasecmp(text, "Control")==0) {
1068 control = 1;
1069 } else {
1070 buffer = wstrappend(buffer, text);
1072 text = k+1;
1075 if (control) {
1076 buffer = wstrappend(buffer, "^");
1078 buffer = wstrappend(buffer, text);
1080 /* get key */
1081 /* ksym = XStringToKeysym(text);
1082 tmp = keysymToString(ksym, modmask);
1083 puts(tmp);
1084 buffer = wstrappend(buffer, tmp);
1086 wfree(tmp);
1088 return buffer;
1092 char*
1093 EscapeWM_CLASS(char *name, char *class)
1095 char *ret;
1096 char *ename = NULL, *eclass = NULL;
1097 int i, j, l;
1099 if (!name && !class)
1100 return NULL;
1102 if (name) {
1103 l = strlen(name);
1104 ename = wmalloc(l*2);
1105 j = 0;
1106 for (i=0; i<l; i++) {
1107 if (name[i]=='\\') {
1108 ename[j++] = '\\';
1109 } else if (name[i]=='.') {
1110 ename[j++] = '\\';
1112 ename[j++] = name[i];
1114 ename[j] = 0;
1116 if (class) {
1117 l = strlen(class);
1118 eclass = wmalloc(l*2);
1119 j = 0;
1120 for (i=0; i<l; i++) {
1121 if (class[i]=='\\') {
1122 eclass[j++] = '\\';
1123 } else if (class[i]=='.') {
1124 eclass[j++] = '\\';
1126 eclass[j++] = class[i];
1128 eclass[j] = 0;
1131 if (ename && eclass) {
1132 ret = wmalloc(strlen(ename)+strlen(eclass)+4);
1133 sprintf(ret, "%s.%s", ename, eclass);
1134 wfree(ename);
1135 wfree(eclass);
1136 } else if (ename) {
1137 ret = wstrdup(ename);
1138 wfree(ename);
1139 } else {
1140 ret = wstrdup(eclass);
1141 wfree(eclass);
1144 return ret;
1148 void
1149 UnescapeWM_CLASS(char *str, char **name, char **class)
1151 int i, j, k, dot;
1152 Bool esc;
1154 j = strlen(str);
1155 *name = wmalloc(j);
1156 **name = 0;
1157 *class = wmalloc(j);
1158 **class = 0;
1160 /* separate string in 2 parts */
1161 esc = False;
1162 dot = 0;
1163 for (i = 0; i < j; i++) {
1164 if (!esc) {
1165 if (str[i]=='\\') {
1166 esc = True;
1167 } else if (str[i]=='.') {
1168 dot = i;
1169 break;
1171 } else {
1172 esc = False;
1176 /* unescape strings */
1177 esc = False;
1178 k = 0;
1179 for (i = 0; i < dot; i++) {
1180 if (!esc) {
1181 if (str[i]=='\\') {
1182 esc = True;
1183 } else {
1184 (*name)[k++] = str[i];
1186 } else {
1187 (*name)[k++] = str[i];
1188 esc = False;
1191 (*name)[k] = 0;
1193 esc = False;
1194 k = 0;
1195 for (i = dot+1; i<j; i++) {
1196 if (!esc) {
1197 if (str[i]=='\\') {
1198 esc = True;
1199 } else {
1200 (*class)[k++] = str[i];
1202 } else {
1203 esc = False;
1206 (*class)[k] = 0;
1208 if (!*name) {
1209 wfree(*name);
1210 *name = NULL;
1212 if (!*class) {
1213 wfree(*class);
1214 *class = NULL;
1220 void
1221 SendHelperMessage(WScreen *scr, char type, int workspace, char *msg)
1223 unsigned char *buffer;
1224 int len;
1225 int i;
1226 char buf[16];
1228 if (!scr->flags.backimage_helper_launched) {
1229 return;
1232 len = (msg ? strlen(msg) : 0) + (workspace >=0 ? 4 : 0) + 1 ;
1233 buffer = wmalloc(len+5);
1234 sprintf(buf, "%4i", len);
1235 memcpy(buffer, buf, 4);
1236 buffer[4] = type;
1237 i = 5;
1238 if (workspace >= 0) {
1239 sprintf(buf, "%4i", workspace);
1240 memcpy(&buffer[i], buf, 4);
1241 i += 4;
1242 buffer[i] = 0;
1244 if (msg)
1245 strcpy(&buffer[i], msg);
1247 if (write(scr->helper_fd, buffer, len+4) < 0) {
1248 wsyserror(_("could not send message to background image helper"));
1250 wfree(buffer);