- Replaced all free() with wfree() where appropriate
[wmaker-crm.git] / src / misc.c
blob8f53f67b733dc79ea1636c50bfe5ebb15adbba79
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"
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 sprintf(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);
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
1026 static char *
1027 appendrealloc(char *a, char *b)
1029 if (a == NULL)
1030 return wstrdup(b);
1031 else {
1032 char *c = wstrappend(a, b);
1033 wfree(a);
1034 return c;
1039 char*
1040 GetShortcutString(char *text)
1042 char *buffer = NULL;
1043 char *k;
1044 int modmask = 0;
1045 /* KeySym ksym;*/
1046 int control = 0;
1047 char *tmp;
1049 tmp = text = wstrdup(text);
1051 /* get modifiers */
1052 while ((k = strchr(text, '+'))!=NULL) {
1053 int mod;
1055 *k = 0;
1056 mod = wXModifierFromKey(text);
1057 if (mod<0) {
1058 return wstrdup("bug");
1061 modmask |= mod;
1063 if (strcasecmp(text, "Meta")==0) {
1064 buffer = appendrealloc(buffer, "M+");
1065 } else if (strcasecmp(text, "Alt")==0) {
1066 buffer = appendrealloc(buffer, "A+");
1067 } else if (strcasecmp(text, "Shift")==0) {
1068 buffer = appendrealloc(buffer, "Sh+");
1069 } else if (strcasecmp(text, "Mod1")==0) {
1070 buffer = appendrealloc(buffer, "M1+");
1071 } else if (strcasecmp(text, "Mod2")==0) {
1072 buffer = appendrealloc(buffer, "M2+");
1073 } else if (strcasecmp(text, "Mod3")==0) {
1074 buffer = appendrealloc(buffer, "M3+");
1075 } else if (strcasecmp(text, "Mod4")==0) {
1076 buffer = appendrealloc(buffer, "M4+");
1077 } else if (strcasecmp(text, "Mod5")==0) {
1078 buffer = appendrealloc(buffer, "M5+");
1079 } else if (strcasecmp(text, "Control")==0) {
1080 control = 1;
1081 } else {
1082 buffer = appendrealloc(buffer, text);
1084 text = k+1;
1087 if (control) {
1088 buffer = appendrealloc(buffer, "^");
1090 buffer = appendrealloc(buffer, text);
1092 /* get key */
1093 /* ksym = XStringToKeysym(text);
1094 tmp = keysymToString(ksym, modmask);
1095 puts(tmp);
1096 buffer = wstrappend(buffer, tmp);
1098 wfree(tmp);
1100 return buffer;
1104 char*
1105 EscapeWM_CLASS(char *name, char *class)
1107 char *ret;
1108 char *ename = NULL, *eclass = NULL;
1109 int i, j, l;
1111 if (!name && !class)
1112 return NULL;
1114 if (name) {
1115 l = strlen(name);
1116 ename = wmalloc(l*2);
1117 j = 0;
1118 for (i=0; i<l; i++) {
1119 if (name[i]=='\\') {
1120 ename[j++] = '\\';
1121 } else if (name[i]=='.') {
1122 ename[j++] = '\\';
1124 ename[j++] = name[i];
1126 ename[j] = 0;
1128 if (class) {
1129 l = strlen(class);
1130 eclass = wmalloc(l*2);
1131 j = 0;
1132 for (i=0; i<l; i++) {
1133 if (class[i]=='\\') {
1134 eclass[j++] = '\\';
1135 } else if (class[i]=='.') {
1136 eclass[j++] = '\\';
1138 eclass[j++] = class[i];
1140 eclass[j] = 0;
1143 if (ename && eclass) {
1144 ret = wmalloc(strlen(ename)+strlen(eclass)+4);
1145 sprintf(ret, "%s.%s", ename, eclass);
1146 wfree(ename);
1147 wfree(eclass);
1148 } else if (ename) {
1149 ret = wstrdup(ename);
1150 wfree(ename);
1151 } else {
1152 ret = wstrdup(eclass);
1153 wfree(eclass);
1156 return ret;
1160 void
1161 UnescapeWM_CLASS(char *str, char **name, char **class)
1163 int i, j, k, dot;
1164 Bool esc;
1166 j = strlen(str);
1167 *name = wmalloc(j);
1168 **name = 0;
1169 *class = wmalloc(j);
1170 **class = 0;
1172 /* separate string in 2 parts */
1173 esc = False;
1174 dot = 0;
1175 for (i = 0; i < j; i++) {
1176 if (!esc) {
1177 if (str[i]=='\\') {
1178 esc = True;
1179 } else if (str[i]=='.') {
1180 dot = i;
1181 break;
1183 } else {
1184 esc = False;
1188 /* unescape strings */
1189 esc = False;
1190 k = 0;
1191 for (i = 0; i < dot; i++) {
1192 if (!esc) {
1193 if (str[i]=='\\') {
1194 esc = True;
1195 } else {
1196 (*name)[k++] = str[i];
1198 } else {
1199 (*name)[k++] = str[i];
1200 esc = False;
1203 (*name)[k] = 0;
1205 esc = False;
1206 k = 0;
1207 for (i = dot+1; i<j; i++) {
1208 if (!esc) {
1209 if (str[i]=='\\') {
1210 esc = True;
1211 } else {
1212 (*class)[k++] = str[i];
1214 } else {
1215 esc = False;
1218 (*class)[k] = 0;
1220 if (!*name) {
1221 wfree(*name);
1222 *name = NULL;
1224 if (!*class) {
1225 wfree(*class);
1226 *class = NULL;
1232 void
1233 SendHelperMessage(WScreen *scr, char type, int workspace, char *msg)
1235 unsigned char *buffer;
1236 int len;
1237 int i;
1238 char buf[16];
1240 if (!scr->flags.backimage_helper_launched) {
1241 return;
1244 len = (msg ? strlen(msg) : 0) + (workspace >=0 ? 4 : 0) + 1 ;
1245 buffer = wmalloc(len+5);
1246 sprintf(buf, "%4i", len);
1247 memcpy(buffer, buf, 4);
1248 buffer[4] = type;
1249 i = 5;
1250 if (workspace >= 0) {
1251 sprintf(buf, "%4i", workspace);
1252 memcpy(&buffer[i], buf, 4);
1253 i += 4;
1254 buffer[i] = 0;
1256 if (msg)
1257 strcpy(&buffer[i], msg);
1259 if (write(scr->helper_fd, buffer, len+4) < 0) {
1260 wsyserror(_("could not send message to background image helper"));
1262 wfree(buffer);