- s/sprintf/snprintf
[wmaker-crm.git] / src / misc.c
blob8c8130419ed4cc6f78195674a9fa77800c51d83d
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 <WINGs/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"
53 /**** global variables *****/
55 extern char *DisplayName;
57 extern WPreferences wPreferences;
59 extern Time LastTimestamp;
61 #ifdef OFFIX_DND
62 extern Atom _XA_DND_SELECTION;
63 #endif
66 #ifdef USECPP
67 static void
68 putdef(char *line, char *name, char *value)
70 if (!value) {
71 wwarning(_("could not define value for %s for cpp"), name);
72 return;
74 strcat(line, name);
75 strcat(line, value);
80 static void
81 putidef(char *line, char *name, int value)
83 char tmp[64];
84 snprintf(tmp, sizeof(tmp), "%i", value);
85 strcat(line, name);
86 strcat(line, tmp);
90 static char*
91 username()
93 char *tmp;
95 tmp = getlogin();
96 if (!tmp) {
97 struct passwd *user;
99 user = getpwuid(getuid());
100 if (!user) {
101 wsyserror(_("could not get password entry for UID %i"), getuid());
102 return NULL;
104 if (!user->pw_name) {
105 return NULL;
106 } else {
107 return user->pw_name;
110 return tmp;
113 char *
114 MakeCPPArgs(char *path)
116 int i;
117 char buffer[MAXLINE], *buf, *line;
118 Visual *visual;
119 char *tmp;
121 line = wmalloc(MAXLINE);
122 *line = 0;
123 i=1;
124 if ((buf=getenv("HOSTNAME"))!=NULL) {
125 if (buf[0]=='(') {
126 wwarning(_("your machine is misconfigured. HOSTNAME is set to %s"),
127 buf);
128 } else
129 putdef(line, " -DHOST=", buf);
130 } else if ((buf=getenv("HOST"))!=NULL) {
131 if (buf[0]=='(') {
132 wwarning(_("your machine is misconfigured. HOST is set to %s"),
133 buf);
134 } else
135 putdef(line, " -DHOST=", buf);
137 buf = username();
138 if (buf)
139 putdef(line, " -DUSER=", buf);
140 putidef(line, " -DUID=", getuid());
141 buf = XDisplayName(DisplayString(dpy));
142 putdef(line, " -DDISPLAY=", buf);
143 putdef(line, " -DWM_VERSION=", VERSION);
145 visual = DefaultVisual(dpy, DefaultScreen(dpy));
146 putidef(line, " -DVISUAL=", visual->class);
148 putidef(line, " -DDEPTH=", DefaultDepth(dpy, DefaultScreen(dpy)));
150 putidef(line, " -DSCR_WIDTH=", WidthOfScreen(DefaultScreenOfDisplay(dpy)));
151 putidef(line, " -DSCR_HEIGHT=",
152 HeightOfScreen(DefaultScreenOfDisplay(dpy)));
154 /* put the dir where the menu is being read from to the
155 * search path */
156 if (path) {
157 tmp = wstrdup(path);
158 buf = strchr(tmp+1, ' ');
159 if (buf) {
160 *buf = 0;
162 buf = strrchr(tmp, '/');
163 if (buf) {
164 *buf = 0; /* trunc filename */
165 putdef(line, " -I", tmp);
167 wfree(tmp);
171 /* this should be done just once, but it works this way */
172 strcpy(buffer, DEF_CONFIG_PATHS);
173 buf = strtok(buffer, ":");
175 do {
176 char fullpath[MAXLINE];
178 if (buf[0]!='~') {
179 strcpy(fullpath, buf);
180 } else {
181 char * wgethomedir();
182 /* home is statically allocated. Don't free it! */
183 char *home = wgethomedir();
185 strcpy(fullpath, home);
186 strcat(fullpath, &(buf[1]));
189 putdef(line, " -I", fullpath);
191 } while ((buf = strtok(NULL, ":"))!=NULL);
193 #undef arg
194 #ifdef DEBUG
195 puts("CPP ARGS");
196 puts(line);
197 #endif
198 return line;
200 #endif /* USECPP */
205 #if 0
207 * Is win2 below win1?
209 static Bool
210 isBelow(WWindow *win1, WWindow *win2)
212 int i;
213 WCoreWindow *tmp;
215 tmp = win1->frame->core->stacking->under;
216 while (tmp) {
217 if (tmp == win2->frame->core)
218 return True;
219 tmp = tmp->stacking->under;
222 for (i=win1->frame->core->stacking->window_level-1; i>=0; i--) {
223 tmp = win1->screen_ptr->stacking_list[i];
224 while (tmp) {
225 if (tmp == win2->frame->core)
226 return True;
227 tmp = tmp->stacking->under;
230 return True;
232 #endif
237 * XFetchName Wrapper
240 Bool
241 wFetchName(dpy, win, winname)
242 Display *dpy;
243 Window win;
244 char **winname;
246 XTextProperty text_prop;
247 char **list;
248 int num;
250 if (XGetWMName(dpy, win, &text_prop)) {
251 if (text_prop.value && text_prop.nitems > 0) {
252 if (text_prop.encoding == XA_STRING) {
253 *winname = wstrdup((char *)text_prop.value);
254 XFree(text_prop.value);
255 } else {
256 text_prop.nitems = strlen((char *)text_prop.value);
257 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >=
258 Success && num > 0 && *list) {
259 XFree(text_prop.value);
260 *winname = wstrdup(*list);
261 XFreeStringList(list);
262 } else {
263 *winname = wstrdup((char *)text_prop.value);
264 XFree(text_prop.value);
267 } else {
268 /* the title is set, but it was set to none */
269 *winname = wstrdup("");
271 return True;
272 } else {
273 /* the hint is probably not set */
274 *winname = NULL;
276 return False;
281 * XGetIconName Wrapper
285 Bool
286 wGetIconName(dpy, win, iconname)
287 Display *dpy;
288 Window win;
289 char **iconname;
291 XTextProperty text_prop;
292 char **list;
293 int num;
295 if (XGetWMIconName(dpy, win, &text_prop) != 0 && text_prop.value
296 && text_prop.nitems > 0) {
297 if (text_prop.encoding == XA_STRING)
298 *iconname = (char *)text_prop.value;
299 else {
300 text_prop.nitems = strlen((char *)text_prop.value);
301 if (XmbTextPropertyToTextList(dpy, &text_prop, &list, &num) >=
302 Success && num > 0 && *list) {
303 XFree(text_prop.value);
304 *iconname = wstrdup(*list);
305 XFreeStringList(list);
306 } else
307 *iconname = (char *)text_prop.value;
309 return True;
311 *iconname = NULL;
312 return False;
316 static void
317 eatExpose()
319 XEvent event, foo;
321 /* compress all expose events into a single one */
323 if (XCheckMaskEvent(dpy, ExposureMask, &event)) {
324 /* ignore other exposure events for this window */
325 while (XCheckWindowEvent(dpy, event.xexpose.window, ExposureMask,
326 &foo));
327 /* eat exposes for other windows */
328 eatExpose();
330 event.xexpose.count = 0;
331 XPutBackEvent(dpy, &event);
336 void
337 SlideWindow(Window win, int from_x, int from_y, int to_x, int to_y)
339 time_t time0 = time(NULL);
340 float dx, dy, x=from_x, y=from_y, sx, sy, px, py;
341 int dx_is_bigger=0;
343 /* animation parameters */
344 static struct {
345 int delay;
346 int steps;
347 int slowdown;
348 } apars[5] = {
349 {ICON_SLIDE_DELAY_UF, ICON_SLIDE_STEPS_UF, ICON_SLIDE_SLOWDOWN_UF},
350 {ICON_SLIDE_DELAY_F, ICON_SLIDE_STEPS_F, ICON_SLIDE_SLOWDOWN_F},
351 {ICON_SLIDE_DELAY_M, ICON_SLIDE_STEPS_M, ICON_SLIDE_SLOWDOWN_M},
352 {ICON_SLIDE_DELAY_S, ICON_SLIDE_STEPS_S, ICON_SLIDE_SLOWDOWN_S},
353 {ICON_SLIDE_DELAY_U, ICON_SLIDE_STEPS_U, ICON_SLIDE_SLOWDOWN_U}};
357 dx = (float)(to_x-from_x);
358 dy = (float)(to_y-from_y);
359 sx = (dx == 0 ? 0 : fabs(dx)/dx);
360 sy = (dy == 0 ? 0 : fabs(dy)/dy);
362 if (fabs(dx) > fabs(dy)) {
363 dx_is_bigger = 1;
366 if (dx_is_bigger) {
367 px = dx / apars[(int)wPreferences.icon_slide_speed].slowdown;
368 if (px < apars[(int)wPreferences.icon_slide_speed].steps && px > 0)
369 px = apars[(int)wPreferences.icon_slide_speed].steps;
370 else if (px > -apars[(int)wPreferences.icon_slide_speed].steps && px < 0)
371 px = -apars[(int)wPreferences.icon_slide_speed].steps;
372 py = (sx == 0 ? 0 : px*dy/dx);
373 } else {
374 py = dy / apars[(int)wPreferences.icon_slide_speed].slowdown;
375 if (py < apars[(int)wPreferences.icon_slide_speed].steps && py > 0)
376 py = apars[(int)wPreferences.icon_slide_speed].steps;
377 else if (py > -apars[(int)wPreferences.icon_slide_speed].steps && py < 0)
378 py = -apars[(int)wPreferences.icon_slide_speed].steps;
379 px = (sy == 0 ? 0 : py*dx/dy);
382 while (x != to_x || y != to_y) {
383 x += px;
384 y += py;
385 if ((px<0 && (int)x < to_x) || (px>0 && (int)x > to_x))
386 x = (float)to_x;
387 if ((py<0 && (int)y < to_y) || (py>0 && (int)y > to_y))
388 y = (float)to_y;
390 if (dx_is_bigger) {
391 px = px * (1.0 - 1/(float)apars[(int)wPreferences.icon_slide_speed].slowdown);
392 if (px < apars[(int)wPreferences.icon_slide_speed].steps && px > 0)
393 px = apars[(int)wPreferences.icon_slide_speed].steps;
394 else if (px > -apars[(int)wPreferences.icon_slide_speed].steps && px < 0)
395 px = -apars[(int)wPreferences.icon_slide_speed].steps;
396 py = (sx == 0 ? 0 : px*dy/dx);
397 } else {
398 py = py * (1.0 - 1/(float)apars[(int)wPreferences.icon_slide_speed].slowdown);
399 if (py < apars[(int)wPreferences.icon_slide_speed].steps && py > 0)
400 py = apars[(int)wPreferences.icon_slide_speed].steps;
401 else if (py > -apars[(int)wPreferences.icon_slide_speed].steps && py < 0)
402 py = -apars[(int)wPreferences.icon_slide_speed].steps;
403 px = (sy == 0 ? 0 : py*dx/dy);
406 XMoveWindow(dpy, win, (int)x, (int)y);
407 XFlush(dpy);
408 if (apars[(int)wPreferences.icon_slide_speed].delay > 0) {
409 wusleep(apars[(int)wPreferences.icon_slide_speed].delay*1000L);
411 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
412 break;
414 XMoveWindow(dpy, win, to_x, to_y);
416 XSync(dpy, 0);
417 /* compress expose events */
418 eatExpose();
422 char*
423 ShrinkString(WMFont *font, char *string, int width)
425 int w, w1=0;
426 int p;
427 char *pos;
428 char *text;
429 int p1, p2, t;
431 if (wPreferences.multi_byte_text)
432 return wstrdup(string);
434 p = strlen(string);
435 w = WMWidthOfString(font, string, p);
436 text = wmalloc(strlen(string)+8);
437 strcpy(text, string);
438 if (w<=width)
439 return text;
441 pos = strchr(text, ' ');
442 if (!pos)
443 pos = strchr(text, ':');
445 if (pos) {
446 *pos = 0;
447 p = strlen(text);
448 w1 = WMWidthOfString(font, text, p);
449 if (w1 > width) {
450 w1 = 0;
451 p = 0;
452 *pos = ' ';
453 *text = 0;
454 } else {
455 *pos = 0;
456 width -= w1;
457 p++;
459 string += p;
460 p=strlen(string);
461 } else {
462 *text=0;
464 strcat(text, "...");
465 width -= WMWidthOfString(font, "...", 3);
466 pos = string;
467 p1=0;
468 p2=p;
469 t = (p2-p1)/2;
470 while (p2>p1 && p1!=t) {
471 w = WMWidthOfString(font, &string[p-t], t);
472 if (w>width) {
473 p2 = t;
474 t = p1+(p2-p1)/2;
475 } else if (w<width) {
476 p1 = t;
477 t = p1+(p2-p1)/2;
478 } else
479 p2=p1=t;
481 strcat(text, &string[p-p1]);
483 return text;
487 char*
488 FindImage(char *paths, char *file)
490 char *tmp, *path;
492 tmp = strrchr(file, ':');
493 if (tmp) {
494 *tmp = 0;
495 path = wfindfile(paths, file);
496 *tmp = ':';
498 if (!tmp || !path) {
499 path = wfindfile(paths, file);
502 return path;
506 static void
507 timeoutHandler(void *data)
509 *(int*)data = 1;
513 static char*
514 getTextSelection(WScreen *screen, Atom selection)
516 int buffer = -1;
518 switch (selection) {
519 case XA_CUT_BUFFER0:
520 buffer = 0;
521 break;
522 case XA_CUT_BUFFER1:
523 buffer = 1;
524 break;
525 case XA_CUT_BUFFER2:
526 buffer = 2;
527 break;
528 case XA_CUT_BUFFER3:
529 buffer = 3;
530 break;
531 case XA_CUT_BUFFER4:
532 buffer = 4;
533 break;
534 case XA_CUT_BUFFER5:
535 buffer = 5;
536 break;
537 case XA_CUT_BUFFER6:
538 buffer = 6;
539 break;
540 case XA_CUT_BUFFER7:
541 buffer = 7;
542 break;
544 if (buffer >= 0) {
545 char *data;
546 int size;
548 data = XFetchBuffer(dpy, &size, buffer);
550 return data;
551 } else {
552 char *data;
553 int bits;
554 Atom rtype;
555 unsigned long len, bytes;
556 WMHandlerID timer;
557 int timeout = 0;
558 XEvent ev;
559 static Atom clipboard = 0;
561 if (!clipboard)
562 clipboard = XInternAtom(dpy, "CLIPBOARD", False);
564 XDeleteProperty(dpy, screen->info_window, clipboard);
566 XConvertSelection(dpy, selection, XA_STRING,
567 clipboard, screen->info_window,
568 CurrentTime);
570 timer = WMAddTimerHandler(1000, timeoutHandler, &timeout);
572 while (!XCheckTypedWindowEvent(dpy, screen->info_window,
573 SelectionNotify, &ev) && !timeout);
575 if (!timeout) {
576 WMDeleteTimerHandler(timer);
577 } else {
578 wwarning("selection retrieval timed out");
579 return NULL;
582 /* nobody owns the selection or the current owner has
583 * nothing to do with what we need */
584 if (ev.xselection.property == None) {
585 return NULL;
588 if (XGetWindowProperty(dpy, screen->info_window,
589 clipboard, 0, 1024,
590 False, XA_STRING, &rtype, &bits, &len,
591 &bytes, (unsigned char**)&data)!=Success) {
592 return NULL;
594 if (rtype!=XA_STRING || bits!=8) {
595 wwarning("invalid data in text selection");
596 if (data)
597 XFree(data);
598 return NULL;
600 return data;
604 static char*
605 getselection(WScreen *scr)
607 char *tmp;
609 tmp = getTextSelection(scr, XA_PRIMARY);
610 if (!tmp)
611 tmp = getTextSelection(scr, XA_CUT_BUFFER0);
612 return tmp;
616 static char*
617 getuserinput(WScreen *scr, char *line, int *ptr)
619 char *ret;
620 char *title;
621 char *prompt;
622 int j, state;
623 int begin = 0;
624 char tbuffer[256], pbuffer[256];
626 title = _("Program Arguments");
627 prompt = _("Enter command arguments:");
628 ret = NULL;
630 #define _STARTING 0
631 #define _TITLE 1
632 #define _PROMPT 2
633 #define _DONE 3
635 state = _STARTING;
636 j = 0;
637 for (; line[*ptr]!=0 && state!=_DONE; (*ptr)++) {
638 switch (state) {
639 case _STARTING:
640 if (line[*ptr]=='(') {
641 state = _TITLE;
642 begin = *ptr+1;
643 } else {
644 state = _DONE;
646 break;
648 case _TITLE:
649 if (j <= 0 && line[*ptr]==',') {
651 j = 0;
652 if (*ptr > begin) {
653 strncpy(tbuffer, &line[begin], WMIN(*ptr-begin, 255));
654 tbuffer[WMIN(*ptr-begin, 255)] = 0;
655 title = (char*)tbuffer;
657 begin = *ptr+1;
658 state = _PROMPT;
660 } else if (j <= 0 && line[*ptr]==')') {
662 if (*ptr > begin) {
663 strncpy(tbuffer, &line[begin], WMIN(*ptr-begin, 255));
664 tbuffer[WMIN(*ptr-begin, 255)] = 0;
665 title = (char*)tbuffer;
667 state = _DONE;
669 } else if (line[*ptr]=='(') {
670 j++;
671 } else if (line[*ptr]==')') {
672 j--;
675 break;
677 case _PROMPT:
678 if (line[*ptr]==')' && j==0) {
680 if (*ptr-begin > 1) {
681 strncpy(pbuffer, &line[begin], WMIN(*ptr-begin, 255));
682 pbuffer[WMIN(*ptr-begin, 255)] = 0;
683 prompt = (char*)pbuffer;
685 state = _DONE;
686 } else if (line[*ptr]=='(')
687 j++;
688 else if (line[*ptr]==')')
689 j--;
690 break;
693 (*ptr)--;
694 #undef _STARTING
695 #undef _TITLE
696 #undef _PROMPT
697 #undef _DONE
699 if (!wInputDialog(scr, title, prompt, &ret))
700 return NULL;
701 else
702 return ret;
706 #ifdef OFFIX_DND
707 static char*
708 get_dnd_selection(WScreen *scr)
710 XTextProperty text_ret;
711 int result;
712 char **list;
713 char *flat_string;
714 int count;
716 result=XGetTextProperty(dpy, scr->root_win, &text_ret, _XA_DND_SELECTION);
718 if (result==0 || text_ret.value==NULL || text_ret.encoding==None
719 || text_ret.format==0 || text_ret.nitems == 0) {
720 wwarning(_("unable to get dropped data from DND drop"));
721 return NULL;
724 XTextPropertyToStringList(&text_ret, &list, &count);
726 if (!list || count<1) {
727 XFree(text_ret.value);
728 wwarning(_("error getting dropped data from DND drop"));
729 return NULL;
732 flat_string = wtokenjoin(list, count);
733 if (!flat_string) {
734 wwarning(_("out of memory while getting data from DND drop"));
737 XFreeStringList(list);
738 XFree(text_ret.value);
739 return flat_string;
741 #endif /* OFFIX_DND */
744 #define S_NORMAL 0
745 #define S_ESCAPE 1
746 #define S_OPTION 2
749 * state input new-state output
750 * NORMAL % OPTION <nil>
751 * NORMAL \ ESCAPE <nil>
752 * NORMAL etc. NORMAL <input>
753 * ESCAPE any NORMAL <input>
754 * OPTION s NORMAL <selection buffer>
755 * OPTION w NORMAL <selected window id>
756 * OPTION a NORMAL <input text>
757 * OPTION d NORMAL <OffiX DND selection object>
758 * OPTION W NORMAL <current workspace>
759 * OPTION etc. NORMAL %<input>
761 #define TMPBUFSIZE 64
762 char*
763 ExpandOptions(WScreen *scr, char *cmdline)
765 int ptr, optr, state, len, olen;
766 char *out, *nout;
767 char *selection=NULL;
768 char *user_input=NULL;
769 #if defined(OFFIX_DND) || defined(XDND)
770 char *dropped_thing=NULL;
771 #endif
772 char tmpbuf[TMPBUFSIZE];
773 int slen;
775 len = strlen(cmdline);
776 olen = len+1;
777 out = malloc(olen);
778 if (!out) {
779 wwarning(_("out of memory during expansion of \"%s\""));
780 return NULL;
782 *out = 0;
783 ptr = 0; /* input line pointer */
784 optr = 0; /* output line pointer */
785 state = S_NORMAL;
786 while (ptr < len) {
787 switch (state) {
788 case S_NORMAL:
789 switch (cmdline[ptr]) {
790 case '\\':
791 state = S_ESCAPE;
792 break;
793 case '%':
794 state = S_OPTION;
795 break;
796 default:
797 state = S_NORMAL;
798 out[optr++]=cmdline[ptr];
799 break;
801 break;
802 case S_ESCAPE:
803 switch (cmdline[ptr]) {
804 case 'n':
805 out[optr++]=10;
806 break;
808 case 'r':
809 out[optr++]=13;
810 break;
812 case 't':
813 out[optr++]=9;
814 break;
816 default:
817 out[optr++]=cmdline[ptr];
819 state = S_NORMAL;
820 break;
821 case S_OPTION:
822 state = S_NORMAL;
823 switch (cmdline[ptr]) {
824 case 'w':
825 if (scr->focused_window
826 && scr->focused_window->flags.focused) {
827 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x",
828 (unsigned int)scr->focused_window->client_win);
829 slen = strlen(tmpbuf);
830 olen += slen;
831 nout = realloc(out,olen);
832 if (!nout) {
833 wwarning(_("out of memory during expansion of \"%w\""));
834 goto error;
836 out = nout;
837 strcat(out,tmpbuf);
838 optr+=slen;
839 } else {
840 out[optr++]=' ';
842 break;
844 case 'W':
845 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x",
846 (unsigned int)scr->current_workspace + 1);
847 slen = strlen(tmpbuf);
848 olen += slen;
849 nout = realloc(out,olen);
850 if (!nout) {
851 wwarning(_("out of memory during expansion of \"%W\""));
852 goto error;
854 out = nout;
855 strcat(out,tmpbuf);
856 optr+=slen;
857 break;
859 case 'a':
860 ptr++;
861 user_input = getuserinput(scr, cmdline, &ptr);
862 if (user_input) {
863 slen = strlen(user_input);
864 olen += slen;
865 nout = realloc(out,olen);
866 if (!nout) {
867 wwarning(_("out of memory during expansion of \"%a\""));
868 goto error;
870 out = nout;
871 strcat(out,user_input);
872 optr+=slen;
873 } else {
874 /* Not an error, but user has Canceled the dialog box.
875 * This will make the command to not be performed. */
876 goto error;
878 break;
880 #if defined(OFFIX_DND) || defined(XDND)
881 case 'd':
882 #ifdef XDND
883 if(scr->xdestring) {
884 dropped_thing = wstrdup(scr->xdestring);
886 #endif
887 if (!dropped_thing) {
888 dropped_thing = get_dnd_selection(scr);
890 if (!dropped_thing) {
891 scr->flags.dnd_data_convertion_status = 1;
892 goto error;
894 slen = strlen(dropped_thing);
895 olen += slen;
896 nout = realloc(out,olen);
897 if (!nout) {
898 wwarning(_("out of memory during expansion of \"%d\""));
899 goto error;
901 out = nout;
902 strcat(out,dropped_thing);
903 optr+=slen;
904 break;
905 #endif /* OFFIX_DND */
907 case 's':
908 if (!selection) {
909 selection = getselection(scr);
911 if (!selection) {
912 wwarning(_("selection not available"));
913 goto error;
915 slen = strlen(selection);
916 olen += slen;
917 nout = realloc(out,olen);
918 if (!nout) {
919 wwarning(_("out of memory during expansion of \"%s\""));
920 goto error;
922 out = nout;
923 strcat(out,selection);
924 optr+=slen;
925 break;
927 default:
928 out[optr++]='%';
929 out[optr++]=cmdline[ptr];
931 break;
933 out[optr]=0;
934 ptr++;
936 if (selection)
937 XFree(selection);
938 return out;
940 error:
941 wfree(out);
942 if (selection)
943 XFree(selection);
944 return NULL;
948 /* We don't care for upper/lower case in comparing the keys; so we
949 have to define our own comparison function here */
950 BOOL
951 StringCompareHook(proplist_t pl1, proplist_t pl2)
953 char *str1, *str2;
955 str1 = PLGetString(pl1);
956 str2 = PLGetString(pl2);
958 if (strcasecmp(str1, str2)==0)
959 return YES;
960 else
961 return NO;
965 /* feof doesn't seem to work on pipes */
967 IsEof(FILE * stream)
969 static struct stat stinfo;
971 fstat(fileno(stream), &stinfo);
972 return ((S_ISFIFO(stinfo.st_dev) && stinfo.st_size == 0) ||
973 feof(stream));
977 void
978 ParseWindowName(proplist_t value, char **winstance, char **wclass, char *where)
980 char *name;
982 *winstance = *wclass = NULL;
984 if (!PLIsString(value)) {
985 wwarning(_("bad window name value in %s state info"), where);
986 return;
989 name = PLGetString(value);
990 if (!name || strlen(name)==0) {
991 wwarning(_("bad window name value in %s state info"), where);
992 return;
995 UnescapeWM_CLASS(name, winstance, wclass);
999 #if 0
1000 static char*
1001 keysymToString(KeySym keysym, unsigned int state)
1003 XKeyEvent kev;
1004 char *buf = wmalloc(20);
1005 int count;
1007 kev.display = dpy;
1008 kev.type = KeyPress;
1009 kev.send_event = False;
1010 kev.window = DefaultRootWindow(dpy);
1011 kev.root = DefaultRootWindow(dpy);
1012 kev.same_screen = True;
1013 kev.subwindow = kev.root;
1014 kev.serial = 0x12344321;
1015 kev.time = CurrentTime;
1016 kev.state = state;
1017 kev.keycode = XKeysymToKeycode(dpy, keysym);
1018 count = XLookupString(&kev, buf, 19, NULL, NULL);
1019 buf[count] = 0;
1021 return buf;
1023 #endif
1026 char*
1027 GetShortcutString(char *text)
1029 char *buffer = NULL;
1030 char *k;
1031 int modmask = 0;
1032 /* KeySym ksym;*/
1033 int control = 0;
1034 char *tmp;
1036 tmp = text = wstrdup(text);
1038 /* get modifiers */
1039 while ((k = strchr(text, '+'))!=NULL) {
1040 int mod;
1042 *k = 0;
1043 mod = wXModifierFromKey(text);
1044 if (mod<0) {
1045 return wstrdup("bug");
1048 modmask |= mod;
1050 if (strcasecmp(text, "Meta")==0) {
1051 buffer = wstrappend(buffer, "M+");
1052 } else if (strcasecmp(text, "Alt")==0) {
1053 buffer = wstrappend(buffer, "A+");
1054 } else if (strcasecmp(text, "Shift")==0) {
1055 buffer = wstrappend(buffer, "Sh+");
1056 } else if (strcasecmp(text, "Mod1")==0) {
1057 buffer = wstrappend(buffer, "M1+");
1058 } else if (strcasecmp(text, "Mod2")==0) {
1059 buffer = wstrappend(buffer, "M2+");
1060 } else if (strcasecmp(text, "Mod3")==0) {
1061 buffer = wstrappend(buffer, "M3+");
1062 } else if (strcasecmp(text, "Mod4")==0) {
1063 buffer = wstrappend(buffer, "M4+");
1064 } else if (strcasecmp(text, "Mod5")==0) {
1065 buffer = wstrappend(buffer, "M5+");
1066 } else if (strcasecmp(text, "Control")==0) {
1067 control = 1;
1068 } else {
1069 buffer = wstrappend(buffer, text);
1071 text = k+1;
1074 if (control) {
1075 buffer = wstrappend(buffer, "^");
1077 buffer = wstrappend(buffer, text);
1079 /* get key */
1080 /* ksym = XStringToKeysym(text);
1081 tmp = keysymToString(ksym, modmask);
1082 puts(tmp);
1083 buffer = wstrappend(buffer, tmp);
1085 wfree(tmp);
1087 return buffer;
1091 char*
1092 EscapeWM_CLASS(char *name, char *class)
1094 char *ret;
1095 char *ename = NULL, *eclass = NULL;
1096 int i, j, l;
1098 if (!name && !class)
1099 return NULL;
1101 if (name) {
1102 l = strlen(name);
1103 ename = wmalloc(l*2+1);
1104 j = 0;
1105 for (i=0; i<l; i++) {
1106 if (name[i]=='\\') {
1107 ename[j++] = '\\';
1108 } else if (name[i]=='.') {
1109 ename[j++] = '\\';
1111 ename[j++] = name[i];
1113 ename[j] = 0;
1115 if (class) {
1116 l = strlen(class);
1117 eclass = wmalloc(l*2+1);
1118 j = 0;
1119 for (i=0; i<l; i++) {
1120 if (class[i]=='\\') {
1121 eclass[j++] = '\\';
1122 } else if (class[i]=='.') {
1123 eclass[j++] = '\\';
1125 eclass[j++] = class[i];
1127 eclass[j] = 0;
1130 if (ename && eclass) {
1131 int len = strlen(ename)+strlen(eclass)+4;
1132 ret = wmalloc(len);
1133 snprintf(ret, len, "%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 snprintf(buf, len, "%4i", len);
1235 memcpy(buffer, buf, 4);
1236 buffer[4] = type;
1237 i = 5;
1238 if (workspace >= 0) {
1239 snprintf(buf, sizeof(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);