2 * Window Maker window manager
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
40 #include "WindowMaker.h"
50 #include "xmodifier.h"
53 /**** global variables *****/
55 extern char *DisplayName
;
57 extern WPreferences wPreferences
;
59 extern Time LastTimestamp
;
62 extern Atom _XA_DND_SELECTION
;
68 putdef(char *line
, char *name
, char *value
)
71 wwarning(_("could not define value for %s for cpp"), name
);
81 putidef(char *line
, char *name
, int value
)
84 sprintf(tmp
, "%i", value
);
99 user
= getpwuid(getuid());
101 wsyserror(_("could not get password entry for UID %i"), getuid());
104 if (!user
->pw_name
) {
107 return user
->pw_name
;
114 MakeCPPArgs(char *path
)
117 char buffer
[MAXLINE
], *buf
, *line
;
121 line
= wmalloc(MAXLINE
);
124 if ((buf
=getenv("HOSTNAME"))!=NULL
) {
126 wwarning(_("your machine is misconfigured. HOSTNAME is set to %s"),
129 putdef(line
, " -DHOST=", buf
);
130 } else if ((buf
=getenv("HOST"))!=NULL
) {
132 wwarning(_("your machine is misconfigured. HOST is set to %s"),
135 putdef(line
, " -DHOST=", 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
158 buf
= strchr(tmp
+1, ' ');
162 buf
= strrchr(tmp
, '/');
164 *buf
= 0; /* trunc filename */
165 putdef(line
, " -I", tmp
);
171 /* this should be done just once, but it works this way */
172 strcpy(buffer
, DEF_CONFIG_PATHS
);
173 buf
= strtok(buffer
, ":");
176 char fullpath
[MAXLINE
];
179 strcpy(fullpath
, buf
);
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
);
207 * Is win2 below win1?
210 isBelow(WWindow
*win1
, WWindow
*win2
)
215 tmp
= win1
->frame
->core
->stacking
->under
;
217 if (tmp
== win2
->frame
->core
)
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
];
225 if (tmp
== win2
->frame
->core
)
227 tmp
= tmp
->stacking
->under
;
241 wFetchName(dpy
, win
, winname
)
246 XTextProperty text_prop
;
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
);
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
);
263 *winname
= wstrdup((char *)text_prop
.value
);
264 XFree(text_prop
.value
);
268 /* the title is set, but it was set to none */
269 *winname
= wstrdup("");
273 /* the hint is probably not set */
281 * XGetIconName Wrapper
286 wGetIconName(dpy
, win
, iconname
)
291 XTextProperty text_prop
;
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
;
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
);
307 *iconname
= (char *)text_prop
.value
;
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
,
327 /* eat exposes for other windows */
330 event
.xexpose
.count
= 0;
331 XPutBackEvent(dpy
, &event
);
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
;
343 /* animation parameters */
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
)) {
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
);
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
) {
385 if ((px
<0 && (int)x
< to_x
) || (px
>0 && (int)x
> to_x
))
387 if ((py
<0 && (int)y
< to_y
) || (py
>0 && (int)y
> to_y
))
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
);
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
);
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
)
414 XMoveWindow(dpy
, win
, to_x
, to_y
);
417 /* compress expose events */
423 ShrinkString(WMFont
*font
, char *string
, int width
)
431 if (wPreferences
.multi_byte_text
)
432 return wstrdup(string
);
435 w
= WMWidthOfString(font
, string
, p
);
436 text
= wmalloc(strlen(string
)+8);
437 strcpy(text
, string
);
441 pos
= strchr(text
, ' ');
443 pos
= strchr(text
, ':');
448 w1
= WMWidthOfString(font
, text
, p
);
465 width
-= WMWidthOfString(font
, "...", 3);
471 while (p2
>p1
&& p1
!=t
) {
472 w
= WMWidthOfString(font
, &string
[p
-t
], t
);
476 } else if (w
<width
) {
482 strcat(text
, &string
[p
-p1
]);
489 FindImage(char *paths
, char *file
)
493 tmp
= strrchr(file
, ':');
496 path
= wfindfile(paths
, file
);
500 path
= wfindfile(paths
, file
);
508 timeoutHandler(void *data
)
515 getTextSelection(WScreen
*screen
, Atom selection
)
549 data
= XFetchBuffer(dpy
, &size
, buffer
);
556 unsigned long len
, bytes
;
560 static Atom clipboard
= 0;
563 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
565 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
567 XConvertSelection(dpy
, selection
, XA_STRING
,
568 clipboard
, screen
->info_window
,
571 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
573 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
,
574 SelectionNotify
, &ev
) && !timeout
);
577 WMDeleteTimerHandler(timer
);
579 wwarning("selection retrieval timed out");
583 /* nobody owns the selection or the current owner has
584 * nothing to do with what we need */
585 if (ev
.xselection
.property
== None
) {
589 if (XGetWindowProperty(dpy
, screen
->info_window
,
591 False
, XA_STRING
, &rtype
, &bits
, &len
,
592 &bytes
, (unsigned char**)&data
)!=Success
) {
595 if (rtype
!=XA_STRING
|| bits
!=8) {
596 wwarning("invalid data in text selection");
606 getselection(WScreen
*scr
)
610 tmp
= getTextSelection(scr
, XA_PRIMARY
);
612 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
618 getuserinput(WScreen
*scr
, char *line
, int *ptr
)
625 char tbuffer
[256], pbuffer
[256];
627 title
= _("Program Arguments");
628 prompt
= _("Enter command arguments:");
638 for (; line
[*ptr
]!=0 && state
!=_DONE
; (*ptr
)++) {
641 if (line
[*ptr
]=='(') {
650 if (j
<= 0 && line
[*ptr
]==',') {
654 strncpy(tbuffer
, &line
[begin
], WMIN(*ptr
-begin
, 255));
655 tbuffer
[WMIN(*ptr
-begin
, 255)] = 0;
656 title
= (char*)tbuffer
;
661 } else if (j
<= 0 && line
[*ptr
]==')') {
664 strncpy(tbuffer
, &line
[begin
], WMIN(*ptr
-begin
, 255));
665 tbuffer
[WMIN(*ptr
-begin
, 255)] = 0;
666 title
= (char*)tbuffer
;
670 } else if (line
[*ptr
]=='(') {
672 } else if (line
[*ptr
]==')') {
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
;
687 } else if (line
[*ptr
]=='(')
689 else if (line
[*ptr
]==')')
700 if (!wInputDialog(scr
, title
, prompt
, &ret
))
709 get_dnd_selection(WScreen
*scr
)
711 XTextProperty text_ret
;
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"));
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"));
733 flat_string
= wtokenjoin(list
, count
);
735 wwarning(_("out of memory while getting data from DND drop"));
738 XFreeStringList(list
);
739 XFree(text_ret
.value
);
742 #endif /* OFFIX_DND */
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
764 ExpandOptions(WScreen
*scr
, char *cmdline
)
766 int ptr
, optr
, state
, len
, olen
;
768 char *selection
=NULL
;
769 char *user_input
=NULL
;
770 #if defined(OFFIX_DND) || defined(XDND)
771 char *dropped_thing
=NULL
;
773 char tmpbuf
[TMPBUFSIZE
];
776 len
= strlen(cmdline
);
780 wwarning(_("out of memory during expansion of \"%s\""));
784 ptr
= 0; /* input line pointer */
785 optr
= 0; /* output line pointer */
790 switch (cmdline
[ptr
]) {
799 out
[optr
++]=cmdline
[ptr
];
804 switch (cmdline
[ptr
]) {
818 out
[optr
++]=cmdline
[ptr
];
824 switch (cmdline
[ptr
]) {
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
);
832 nout
= realloc(out
,olen
);
834 wwarning(_("out of memory during expansion of \"%w\""));
846 sprintf(tmpbuf
, "0x%x",
847 (unsigned int)scr
->current_workspace
+ 1);
848 slen
= strlen(tmpbuf
);
850 nout
= realloc(out
,olen
);
852 wwarning(_("out of memory during expansion of \"%W\""));
862 user_input
= getuserinput(scr
, cmdline
, &ptr
);
864 slen
= strlen(user_input
);
866 nout
= realloc(out
,olen
);
868 wwarning(_("out of memory during expansion of \"%a\""));
872 strcat(out
,user_input
);
875 /* Not an error, but user has Canceled the dialog box.
876 * This will make the command to not be performed. */
881 #if defined(OFFIX_DND) || defined(XDND)
885 dropped_thing
= wstrdup(scr
->xdestring
);
888 if (!dropped_thing
) {
889 dropped_thing
= get_dnd_selection(scr
);
891 if (!dropped_thing
) {
892 scr
->flags
.dnd_data_convertion_status
= 1;
895 slen
= strlen(dropped_thing
);
897 nout
= realloc(out
,olen
);
899 wwarning(_("out of memory during expansion of \"%d\""));
903 strcat(out
,dropped_thing
);
906 #endif /* OFFIX_DND */
910 selection
= getselection(scr
);
913 wwarning(_("selection not available"));
916 slen
= strlen(selection
);
918 nout
= realloc(out
,olen
);
920 wwarning(_("out of memory during expansion of \"%s\""));
924 strcat(out
,selection
);
930 out
[optr
++]=cmdline
[ptr
];
949 /* We don't care for upper/lower case in comparing the keys; so we
950 have to define our own comparison function here */
952 StringCompareHook(proplist_t pl1
, proplist_t pl2
)
956 str1
= PLGetString(pl1
);
957 str2
= PLGetString(pl2
);
959 if (strcasecmp(str1
, str2
)==0)
966 /* feof doesn't seem to work on pipes */
970 static struct stat stinfo
;
972 fstat(fileno(stream
), &stinfo
);
973 return ((S_ISFIFO(stinfo
.st_dev
) && stinfo
.st_size
== 0) ||
979 ParseWindowName(proplist_t value
, char **winstance
, char **wclass
, char *where
)
983 *winstance
= *wclass
= NULL
;
985 if (!PLIsString(value
)) {
986 wwarning(_("bad window name value in %s state info"), where
);
990 name
= PLGetString(value
);
991 if (!name
|| strlen(name
)==0) {
992 wwarning(_("bad window name value in %s state info"), where
);
996 UnescapeWM_CLASS(name
, winstance
, wclass
);
1002 keysymToString(KeySym keysym
, unsigned int state
)
1005 char *buf
= wmalloc(20);
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
;
1018 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
1019 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
1027 appendrealloc(char *a
, char *b
)
1032 char *c
= wstrappend(a
, b
);
1040 GetShortcutString(char *text
)
1042 char *buffer
= NULL
;
1049 tmp
= text
= wstrdup(text
);
1052 while ((k
= strchr(text
, '+'))!=NULL
) {
1056 mod
= wXModifierFromKey(text
);
1058 return wstrdup("bug");
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) {
1082 buffer
= appendrealloc(buffer
, text
);
1088 buffer
= appendrealloc(buffer
, "^");
1090 buffer
= appendrealloc(buffer
, text
);
1093 /* ksym = XStringToKeysym(text);
1094 tmp = keysymToString(ksym, modmask);
1096 buffer = wstrappend(buffer, tmp);
1105 EscapeWM_CLASS(char *name
, char *class)
1108 char *ename
= NULL
, *eclass
= NULL
;
1111 if (!name
&& !class)
1116 ename
= wmalloc(l
*2);
1118 for (i
=0; i
<l
; i
++) {
1119 if (name
[i
]=='\\') {
1121 } else if (name
[i
]=='.') {
1124 ename
[j
++] = name
[i
];
1130 eclass
= wmalloc(l
*2);
1132 for (i
=0; i
<l
; i
++) {
1133 if (class[i
]=='\\') {
1135 } else if (class[i
]=='.') {
1138 eclass
[j
++] = class[i
];
1143 if (ename
&& eclass
) {
1144 ret
= wmalloc(strlen(ename
)+strlen(eclass
)+4);
1145 sprintf(ret
, "%s.%s", ename
, eclass
);
1149 ret
= wstrdup(ename
);
1152 ret
= wstrdup(eclass
);
1161 UnescapeWM_CLASS(char *str
, char **name
, char **class)
1169 *class = wmalloc(j
);
1172 /* separate string in 2 parts */
1175 for (i
= 0; i
< j
; i
++) {
1179 } else if (str
[i
]=='.') {
1188 /* unescape strings */
1191 for (i
= 0; i
< dot
; i
++) {
1196 (*name
)[k
++] = str
[i
];
1199 (*name
)[k
++] = str
[i
];
1207 for (i
= dot
+1; i
<j
; i
++) {
1212 (*class)[k
++] = str
[i
];
1233 SendHelperMessage(WScreen
*scr
, char type
, int workspace
, char *msg
)
1235 unsigned char *buffer
;
1240 if (!scr
->flags
.backimage_helper_launched
) {
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);
1250 if (workspace
>= 0) {
1251 sprintf(buf
, "%4i", workspace
);
1252 memcpy(&buffer
[i
], buf
, 4);
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"));