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>
36 #include <WINGs/WUtil.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 snprintf(tmp
, sizeof(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);
470 while (p2
>p1
&& p1
!=t
) {
471 w
= WMWidthOfString(font
, &string
[p
-t
], t
);
475 } else if (w
<width
) {
481 strcat(text
, &string
[p
-p1
]);
488 FindImage(char *paths
, char *file
)
492 tmp
= strrchr(file
, ':');
495 path
= wfindfile(paths
, file
);
499 path
= wfindfile(paths
, file
);
507 timeoutHandler(void *data
)
514 getTextSelection(WScreen
*screen
, Atom selection
)
548 data
= XFetchBuffer(dpy
, &size
, buffer
);
555 unsigned long len
, bytes
;
559 static Atom clipboard
= 0;
562 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
564 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
566 XConvertSelection(dpy
, selection
, XA_STRING
,
567 clipboard
, screen
->info_window
,
570 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
572 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
,
573 SelectionNotify
, &ev
) && !timeout
);
576 WMDeleteTimerHandler(timer
);
578 wwarning("selection retrieval timed out");
582 /* nobody owns the selection or the current owner has
583 * nothing to do with what we need */
584 if (ev
.xselection
.property
== None
) {
588 if (XGetWindowProperty(dpy
, screen
->info_window
,
590 False
, XA_STRING
, &rtype
, &bits
, &len
,
591 &bytes
, (unsigned char**)&data
)!=Success
) {
594 if (rtype
!=XA_STRING
|| bits
!=8) {
595 wwarning("invalid data in text selection");
605 getselection(WScreen
*scr
)
609 tmp
= getTextSelection(scr
, XA_PRIMARY
);
611 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
617 getuserinput(WScreen
*scr
, char *line
, int *ptr
)
625 char tbuffer
[BUFSIZE
], pbuffer
[BUFSIZE
];
628 title
= _("Program Arguments");
629 prompt
= _("Enter command arguments:");
639 for (; line
[*ptr
]!=0 && state
!=_DONE
; (*ptr
)++) {
642 if (line
[*ptr
]=='(') {
651 if (j
<= 0 && line
[*ptr
]==',') {
655 strncpy(tbuffer
, &line
[begin
], WMIN(*ptr
-begin
, BUFSIZE
));
656 tbuffer
[WMIN(*ptr
-begin
, BUFSIZE
)] = 0;
657 title
= (char*)tbuffer
;
662 } else if (j
<= 0 && line
[*ptr
]==')') {
665 strncpy(tbuffer
, &line
[begin
], WMIN(*ptr
-begin
, BUFSIZE
));
666 tbuffer
[WMIN(*ptr
-begin
, BUFSIZE
)] = 0;
667 title
= (char*)tbuffer
;
671 } else if (line
[*ptr
]=='(') {
673 } else if (line
[*ptr
]==')') {
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
;
688 } else if (line
[*ptr
]=='(')
690 else if (line
[*ptr
]==')')
701 if (!wInputDialog(scr
, title
, prompt
, &ret
))
710 get_dnd_selection(WScreen
*scr
)
712 XTextProperty text_ret
;
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"));
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"));
734 flat_string
= wtokenjoin(list
, count
);
736 wwarning(_("out of memory while getting data from DND drop"));
739 XFreeStringList(list
);
740 XFree(text_ret
.value
);
743 #endif /* OFFIX_DND */
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
765 ExpandOptions(WScreen
*scr
, char *cmdline
)
767 int ptr
, optr
, state
, len
, olen
;
769 char *selection
=NULL
;
770 char *user_input
=NULL
;
771 #if defined(OFFIX_DND) || defined(XDND)
772 char *dropped_thing
=NULL
;
774 char tmpbuf
[TMPBUFSIZE
];
777 len
= strlen(cmdline
);
781 wwarning(_("out of memory during expansion of \"%s\""));
785 ptr
= 0; /* input line pointer */
786 optr
= 0; /* output line pointer */
791 switch (cmdline
[ptr
]) {
800 out
[optr
++]=cmdline
[ptr
];
805 switch (cmdline
[ptr
]) {
819 out
[optr
++]=cmdline
[ptr
];
825 switch (cmdline
[ptr
]) {
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
);
833 nout
= realloc(out
,olen
);
835 wwarning(_("out of memory during expansion of \"%w\""));
847 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
848 (unsigned int)scr
->current_workspace
+ 1);
849 slen
= strlen(tmpbuf
);
851 nout
= realloc(out
,olen
);
853 wwarning(_("out of memory during expansion of \"%W\""));
863 user_input
= getuserinput(scr
, cmdline
, &ptr
);
865 slen
= strlen(user_input
);
867 nout
= realloc(out
,olen
);
869 wwarning(_("out of memory during expansion of \"%a\""));
873 strcat(out
,user_input
);
876 /* Not an error, but user has Canceled the dialog box.
877 * This will make the command to not be performed. */
882 #if defined(OFFIX_DND) || defined(XDND)
886 dropped_thing
= wstrdup(scr
->xdestring
);
889 if (!dropped_thing
) {
890 dropped_thing
= get_dnd_selection(scr
);
892 if (!dropped_thing
) {
893 scr
->flags
.dnd_data_convertion_status
= 1;
896 slen
= strlen(dropped_thing
);
898 nout
= realloc(out
,olen
);
900 wwarning(_("out of memory during expansion of \"%d\""));
904 strcat(out
,dropped_thing
);
907 #endif /* OFFIX_DND */
911 selection
= getselection(scr
);
914 wwarning(_("selection not available"));
917 slen
= strlen(selection
);
919 nout
= realloc(out
,olen
);
921 wwarning(_("out of memory during expansion of \"%s\""));
925 strcat(out
,selection
);
931 out
[optr
++]=cmdline
[ptr
];
950 /* feof doesn't seem to work on pipes */
954 static struct stat stinfo
;
956 fstat(fileno(stream
), &stinfo
);
957 return ((S_ISFIFO(stinfo
.st_dev
) && stinfo
.st_size
== 0) ||
963 ParseWindowName(WMPropList
*value
, char **winstance
, char **wclass
, char *where
)
967 *winstance
= *wclass
= NULL
;
969 if (!WMIsPLString(value
)) {
970 wwarning(_("bad window name value in %s state info"), where
);
974 name
= WMGetFromPLString(value
);
975 if (!name
|| strlen(name
)==0) {
976 wwarning(_("bad window name value in %s state info"), where
);
980 UnescapeWM_CLASS(name
, winstance
, wclass
);
986 keysymToString(KeySym keysym
, unsigned int state
)
989 char *buf
= wmalloc(20);
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
;
1002 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
1003 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
1012 GetShortcutString(char *text
)
1014 char *buffer
= NULL
;
1021 tmp
= text
= wstrdup(text
);
1024 while ((k
= strchr(text
, '+'))!=NULL
) {
1028 mod
= wXModifierFromKey(text
);
1030 return wstrdup("bug");
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) {
1054 buffer
= wstrappend(buffer
, text
);
1060 buffer
= wstrappend(buffer
, "^");
1062 buffer
= wstrappend(buffer
, text
);
1065 /* ksym = XStringToKeysym(text);
1066 tmp = keysymToString(ksym, modmask);
1068 buffer = wstrappend(buffer, tmp);
1077 EscapeWM_CLASS(char *name
, char *class)
1080 char *ename
= NULL
, *eclass
= NULL
;
1083 if (!name
&& !class)
1088 ename
= wmalloc(l
*2+1);
1090 for (i
=0; i
<l
; i
++) {
1091 if (name
[i
]=='\\') {
1093 } else if (name
[i
]=='.') {
1096 ename
[j
++] = name
[i
];
1102 eclass
= wmalloc(l
*2+1);
1104 for (i
=0; i
<l
; i
++) {
1105 if (class[i
]=='\\') {
1107 } else if (class[i
]=='.') {
1110 eclass
[j
++] = class[i
];
1115 if (ename
&& eclass
) {
1116 int len
= strlen(ename
)+strlen(eclass
)+4;
1118 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
1122 ret
= wstrdup(ename
);
1125 ret
= wstrdup(eclass
);
1134 UnescapeWM_CLASS(char *str
, char **name
, char **class)
1142 *class = wmalloc(j
);
1145 /* separate string in 2 parts */
1148 for (i
= 0; i
< j
; i
++) {
1152 } else if (str
[i
]=='.') {
1161 /* unescape strings */
1164 for (i
= 0; i
< dot
; i
++) {
1169 (*name
)[k
++] = str
[i
];
1172 (*name
)[k
++] = str
[i
];
1180 for (i
= dot
+1; i
<j
; i
++) {
1185 (*class)[k
++] = str
[i
];
1206 SendHelperMessage(WScreen
*scr
, char type
, int workspace
, char *msg
)
1208 unsigned char *buffer
;
1213 if (!scr
->flags
.backimage_helper_launched
) {
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);
1223 if (workspace
>= 0) {
1224 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
1225 memcpy(&buffer
[i
], buf
, 4);
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"));