- Finished moving to the new proplist handling code in WINGs.
[wmaker-crm.git] / src / misc.c
blob61f2f13c55384e637c1863b5d88f298bafe7e87e
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 #define BUFSIZE 512
625 char tbuffer[BUFSIZE], pbuffer[BUFSIZE];
628 title = _("Program Arguments");
629 prompt = _("Enter command arguments:");
630 ret = NULL;
632 #define _STARTING 0
633 #define _TITLE 1
634 #define _PROMPT 2
635 #define _DONE 3
637 state = _STARTING;
638 j = 0;
639 for (; line[*ptr]!=0 && state!=_DONE; (*ptr)++) {
640 switch (state) {
641 case _STARTING:
642 if (line[*ptr]=='(') {
643 state = _TITLE;
644 begin = *ptr+1;
645 } else {
646 state = _DONE;
648 break;
650 case _TITLE:
651 if (j <= 0 && line[*ptr]==',') {
653 j = 0;
654 if (*ptr > begin) {
655 strncpy(tbuffer, &line[begin], WMIN(*ptr-begin, BUFSIZE));
656 tbuffer[WMIN(*ptr-begin, BUFSIZE)] = 0;
657 title = (char*)tbuffer;
659 begin = *ptr+1;
660 state = _PROMPT;
662 } else if (j <= 0 && line[*ptr]==')') {
664 if (*ptr > begin) {
665 strncpy(tbuffer, &line[begin], WMIN(*ptr-begin, BUFSIZE));
666 tbuffer[WMIN(*ptr-begin, BUFSIZE)] = 0;
667 title = (char*)tbuffer;
669 state = _DONE;
671 } else if (line[*ptr]=='(') {
672 j++;
673 } else if (line[*ptr]==')') {
674 j--;
677 break;
679 case _PROMPT:
680 if (line[*ptr]==')' && j==0) {
682 if (*ptr-begin > 1) {
683 strncpy(pbuffer, &line[begin], WMIN(*ptr-begin, BUFSIZE));
684 pbuffer[WMIN(*ptr-begin, BUFSIZE)] = 0;
685 prompt = (char*)pbuffer;
687 state = _DONE;
688 } else if (line[*ptr]=='(')
689 j++;
690 else if (line[*ptr]==')')
691 j--;
692 break;
695 (*ptr)--;
696 #undef _STARTING
697 #undef _TITLE
698 #undef _PROMPT
699 #undef _DONE
701 if (!wInputDialog(scr, title, prompt, &ret))
702 return NULL;
703 else
704 return ret;
708 #ifdef OFFIX_DND
709 static char*
710 get_dnd_selection(WScreen *scr)
712 XTextProperty text_ret;
713 int result;
714 char **list;
715 char *flat_string;
716 int count;
718 result=XGetTextProperty(dpy, scr->root_win, &text_ret, _XA_DND_SELECTION);
720 if (result==0 || text_ret.value==NULL || text_ret.encoding==None
721 || text_ret.format==0 || text_ret.nitems == 0) {
722 wwarning(_("unable to get dropped data from DND drop"));
723 return NULL;
726 XTextPropertyToStringList(&text_ret, &list, &count);
728 if (!list || count<1) {
729 XFree(text_ret.value);
730 wwarning(_("error getting dropped data from DND drop"));
731 return NULL;
734 flat_string = wtokenjoin(list, count);
735 if (!flat_string) {
736 wwarning(_("out of memory while getting data from DND drop"));
739 XFreeStringList(list);
740 XFree(text_ret.value);
741 return flat_string;
743 #endif /* OFFIX_DND */
746 #define S_NORMAL 0
747 #define S_ESCAPE 1
748 #define S_OPTION 2
751 * state input new-state output
752 * NORMAL % OPTION <nil>
753 * NORMAL \ ESCAPE <nil>
754 * NORMAL etc. NORMAL <input>
755 * ESCAPE any NORMAL <input>
756 * OPTION s NORMAL <selection buffer>
757 * OPTION w NORMAL <selected window id>
758 * OPTION a NORMAL <input text>
759 * OPTION d NORMAL <OffiX DND selection object>
760 * OPTION W NORMAL <current workspace>
761 * OPTION etc. NORMAL %<input>
763 #define TMPBUFSIZE 64
764 char*
765 ExpandOptions(WScreen *scr, char *cmdline)
767 int ptr, optr, state, len, olen;
768 char *out, *nout;
769 char *selection=NULL;
770 char *user_input=NULL;
771 #if defined(OFFIX_DND) || defined(XDND)
772 char *dropped_thing=NULL;
773 #endif
774 char tmpbuf[TMPBUFSIZE];
775 int slen;
777 len = strlen(cmdline);
778 olen = len+1;
779 out = malloc(olen);
780 if (!out) {
781 wwarning(_("out of memory during expansion of \"%s\""));
782 return NULL;
784 *out = 0;
785 ptr = 0; /* input line pointer */
786 optr = 0; /* output line pointer */
787 state = S_NORMAL;
788 while (ptr < len) {
789 switch (state) {
790 case S_NORMAL:
791 switch (cmdline[ptr]) {
792 case '\\':
793 state = S_ESCAPE;
794 break;
795 case '%':
796 state = S_OPTION;
797 break;
798 default:
799 state = S_NORMAL;
800 out[optr++]=cmdline[ptr];
801 break;
803 break;
804 case S_ESCAPE:
805 switch (cmdline[ptr]) {
806 case 'n':
807 out[optr++]=10;
808 break;
810 case 'r':
811 out[optr++]=13;
812 break;
814 case 't':
815 out[optr++]=9;
816 break;
818 default:
819 out[optr++]=cmdline[ptr];
821 state = S_NORMAL;
822 break;
823 case S_OPTION:
824 state = S_NORMAL;
825 switch (cmdline[ptr]) {
826 case 'w':
827 if (scr->focused_window
828 && scr->focused_window->flags.focused) {
829 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x",
830 (unsigned int)scr->focused_window->client_win);
831 slen = strlen(tmpbuf);
832 olen += slen;
833 nout = realloc(out,olen);
834 if (!nout) {
835 wwarning(_("out of memory during expansion of \"%w\""));
836 goto error;
838 out = nout;
839 strcat(out,tmpbuf);
840 optr+=slen;
841 } else {
842 out[optr++]=' ';
844 break;
846 case 'W':
847 snprintf(tmpbuf, sizeof(tmpbuf), "0x%x",
848 (unsigned int)scr->current_workspace + 1);
849 slen = strlen(tmpbuf);
850 olen += slen;
851 nout = realloc(out,olen);
852 if (!nout) {
853 wwarning(_("out of memory during expansion of \"%W\""));
854 goto error;
856 out = nout;
857 strcat(out,tmpbuf);
858 optr+=slen;
859 break;
861 case 'a':
862 ptr++;
863 user_input = getuserinput(scr, cmdline, &ptr);
864 if (user_input) {
865 slen = strlen(user_input);
866 olen += slen;
867 nout = realloc(out,olen);
868 if (!nout) {
869 wwarning(_("out of memory during expansion of \"%a\""));
870 goto error;
872 out = nout;
873 strcat(out,user_input);
874 optr+=slen;
875 } else {
876 /* Not an error, but user has Canceled the dialog box.
877 * This will make the command to not be performed. */
878 goto error;
880 break;
882 #if defined(OFFIX_DND) || defined(XDND)
883 case 'd':
884 #ifdef XDND
885 if(scr->xdestring) {
886 dropped_thing = wstrdup(scr->xdestring);
888 #endif
889 if (!dropped_thing) {
890 dropped_thing = get_dnd_selection(scr);
892 if (!dropped_thing) {
893 scr->flags.dnd_data_convertion_status = 1;
894 goto error;
896 slen = strlen(dropped_thing);
897 olen += slen;
898 nout = realloc(out,olen);
899 if (!nout) {
900 wwarning(_("out of memory during expansion of \"%d\""));
901 goto error;
903 out = nout;
904 strcat(out,dropped_thing);
905 optr+=slen;
906 break;
907 #endif /* OFFIX_DND */
909 case 's':
910 if (!selection) {
911 selection = getselection(scr);
913 if (!selection) {
914 wwarning(_("selection not available"));
915 goto error;
917 slen = strlen(selection);
918 olen += slen;
919 nout = realloc(out,olen);
920 if (!nout) {
921 wwarning(_("out of memory during expansion of \"%s\""));
922 goto error;
924 out = nout;
925 strcat(out,selection);
926 optr+=slen;
927 break;
929 default:
930 out[optr++]='%';
931 out[optr++]=cmdline[ptr];
933 break;
935 out[optr]=0;
936 ptr++;
938 if (selection)
939 XFree(selection);
940 return out;
942 error:
943 wfree(out);
944 if (selection)
945 XFree(selection);
946 return NULL;
950 /* feof doesn't seem to work on pipes */
952 IsEof(FILE * stream)
954 static struct stat stinfo;
956 fstat(fileno(stream), &stinfo);
957 return ((S_ISFIFO(stinfo.st_dev) && stinfo.st_size == 0) ||
958 feof(stream));
962 void
963 ParseWindowName(WMPropList *value, char **winstance, char **wclass, char *where)
965 char *name;
967 *winstance = *wclass = NULL;
969 if (!WMIsPLString(value)) {
970 wwarning(_("bad window name value in %s state info"), where);
971 return;
974 name = WMGetFromPLString(value);
975 if (!name || strlen(name)==0) {
976 wwarning(_("bad window name value in %s state info"), where);
977 return;
980 UnescapeWM_CLASS(name, winstance, wclass);
984 #if 0
985 static char*
986 keysymToString(KeySym keysym, unsigned int state)
988 XKeyEvent kev;
989 char *buf = wmalloc(20);
990 int count;
992 kev.display = dpy;
993 kev.type = KeyPress;
994 kev.send_event = False;
995 kev.window = DefaultRootWindow(dpy);
996 kev.root = DefaultRootWindow(dpy);
997 kev.same_screen = True;
998 kev.subwindow = kev.root;
999 kev.serial = 0x12344321;
1000 kev.time = CurrentTime;
1001 kev.state = state;
1002 kev.keycode = XKeysymToKeycode(dpy, keysym);
1003 count = XLookupString(&kev, buf, 19, NULL, NULL);
1004 buf[count] = 0;
1006 return buf;
1008 #endif
1011 char*
1012 GetShortcutString(char *text)
1014 char *buffer = NULL;
1015 char *k;
1016 int modmask = 0;
1017 /* KeySym ksym;*/
1018 int control = 0;
1019 char *tmp;
1021 tmp = text = wstrdup(text);
1023 /* get modifiers */
1024 while ((k = strchr(text, '+'))!=NULL) {
1025 int mod;
1027 *k = 0;
1028 mod = wXModifierFromKey(text);
1029 if (mod<0) {
1030 return wstrdup("bug");
1033 modmask |= mod;
1035 if (strcasecmp(text, "Meta")==0) {
1036 buffer = wstrappend(buffer, "M+");
1037 } else if (strcasecmp(text, "Alt")==0) {
1038 buffer = wstrappend(buffer, "A+");
1039 } else if (strcasecmp(text, "Shift")==0) {
1040 buffer = wstrappend(buffer, "Sh+");
1041 } else if (strcasecmp(text, "Mod1")==0) {
1042 buffer = wstrappend(buffer, "M1+");
1043 } else if (strcasecmp(text, "Mod2")==0) {
1044 buffer = wstrappend(buffer, "M2+");
1045 } else if (strcasecmp(text, "Mod3")==0) {
1046 buffer = wstrappend(buffer, "M3+");
1047 } else if (strcasecmp(text, "Mod4")==0) {
1048 buffer = wstrappend(buffer, "M4+");
1049 } else if (strcasecmp(text, "Mod5")==0) {
1050 buffer = wstrappend(buffer, "M5+");
1051 } else if (strcasecmp(text, "Control")==0) {
1052 control = 1;
1053 } else {
1054 buffer = wstrappend(buffer, text);
1056 text = k+1;
1059 if (control) {
1060 buffer = wstrappend(buffer, "^");
1062 buffer = wstrappend(buffer, text);
1064 /* get key */
1065 /* ksym = XStringToKeysym(text);
1066 tmp = keysymToString(ksym, modmask);
1067 puts(tmp);
1068 buffer = wstrappend(buffer, tmp);
1070 wfree(tmp);
1072 return buffer;
1076 char*
1077 EscapeWM_CLASS(char *name, char *class)
1079 char *ret;
1080 char *ename = NULL, *eclass = NULL;
1081 int i, j, l;
1083 if (!name && !class)
1084 return NULL;
1086 if (name) {
1087 l = strlen(name);
1088 ename = wmalloc(l*2+1);
1089 j = 0;
1090 for (i=0; i<l; i++) {
1091 if (name[i]=='\\') {
1092 ename[j++] = '\\';
1093 } else if (name[i]=='.') {
1094 ename[j++] = '\\';
1096 ename[j++] = name[i];
1098 ename[j] = 0;
1100 if (class) {
1101 l = strlen(class);
1102 eclass = wmalloc(l*2+1);
1103 j = 0;
1104 for (i=0; i<l; i++) {
1105 if (class[i]=='\\') {
1106 eclass[j++] = '\\';
1107 } else if (class[i]=='.') {
1108 eclass[j++] = '\\';
1110 eclass[j++] = class[i];
1112 eclass[j] = 0;
1115 if (ename && eclass) {
1116 int len = strlen(ename)+strlen(eclass)+4;
1117 ret = wmalloc(len);
1118 snprintf(ret, len, "%s.%s", ename, eclass);
1119 wfree(ename);
1120 wfree(eclass);
1121 } else if (ename) {
1122 ret = wstrdup(ename);
1123 wfree(ename);
1124 } else {
1125 ret = wstrdup(eclass);
1126 wfree(eclass);
1129 return ret;
1133 void
1134 UnescapeWM_CLASS(char *str, char **name, char **class)
1136 int i, j, k, dot;
1137 Bool esc;
1139 j = strlen(str);
1140 *name = wmalloc(j);
1141 **name = 0;
1142 *class = wmalloc(j);
1143 **class = 0;
1145 /* separate string in 2 parts */
1146 esc = False;
1147 dot = 0;
1148 for (i = 0; i < j; i++) {
1149 if (!esc) {
1150 if (str[i]=='\\') {
1151 esc = True;
1152 } else if (str[i]=='.') {
1153 dot = i;
1154 break;
1156 } else {
1157 esc = False;
1161 /* unescape strings */
1162 esc = False;
1163 k = 0;
1164 for (i = 0; i < dot; i++) {
1165 if (!esc) {
1166 if (str[i]=='\\') {
1167 esc = True;
1168 } else {
1169 (*name)[k++] = str[i];
1171 } else {
1172 (*name)[k++] = str[i];
1173 esc = False;
1176 (*name)[k] = 0;
1178 esc = False;
1179 k = 0;
1180 for (i = dot+1; i<j; i++) {
1181 if (!esc) {
1182 if (str[i]=='\\') {
1183 esc = True;
1184 } else {
1185 (*class)[k++] = str[i];
1187 } else {
1188 esc = False;
1191 (*class)[k] = 0;
1193 if (!*name) {
1194 wfree(*name);
1195 *name = NULL;
1197 if (!*class) {
1198 wfree(*class);
1199 *class = NULL;
1205 void
1206 SendHelperMessage(WScreen *scr, char type, int workspace, char *msg)
1208 unsigned char *buffer;
1209 int len;
1210 int i;
1211 char buf[16];
1213 if (!scr->flags.backimage_helper_launched) {
1214 return;
1217 len = (msg ? strlen(msg) : 0) + (workspace >=0 ? 4 : 0) + 1 ;
1218 buffer = wmalloc(len+5);
1219 snprintf(buf, len, "%4i", len);
1220 memcpy(buffer, buf, 4);
1221 buffer[4] = type;
1222 i = 5;
1223 if (workspace >= 0) {
1224 snprintf(buf, sizeof(buf), "%4i", workspace);
1225 memcpy(&buffer[i], buf, 4);
1226 i += 4;
1227 buffer[i] = 0;
1229 if (msg)
1230 strcpy(&buffer[i], msg);
1232 if (write(scr->helper_fd, buffer, len+4) < 0) {
1233 wsyserror(_("could not send message to background image helper"));
1235 wfree(buffer);