2 * Window Maker window manager
4 * Copyright (c) 1997-2003 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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <X11/Xutil.h>
24 #include <X11/Xatom.h>
36 #include <X11/XKBlib.h>
38 #include <WINGs/WUtil.h>
43 #include "WindowMaker.h"
51 #include "xmodifier.h"
54 #define ICON_SIZE wPreferences.icon_size
56 /**** Local prototypes *****/
57 static void UnescapeWM_CLASS(const char *str
, char **name
, char **class);
59 /* XFetchName Wrapper */
60 Bool
wFetchName(Display
*dpy
, Window win
, char **winname
)
62 XTextProperty text_prop
;
66 if (XGetWMName(dpy
, win
, &text_prop
)) {
67 if (text_prop
.value
&& text_prop
.nitems
> 0) {
68 if (text_prop
.encoding
== XA_STRING
) {
69 *winname
= wstrdup((char *)text_prop
.value
);
70 XFree(text_prop
.value
);
72 text_prop
.nitems
= strlen((char *)text_prop
.value
);
73 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >=
74 Success
&& num
> 0 && *list
) {
75 XFree(text_prop
.value
);
76 *winname
= wstrdup(*list
);
77 XFreeStringList(list
);
79 *winname
= wstrdup((char *)text_prop
.value
);
80 XFree(text_prop
.value
);
84 /* the title is set, but it was set to none */
85 *winname
= wstrdup("");
89 /* the hint is probably not set */
96 /* XGetIconName Wrapper */
97 Bool
wGetIconName(Display
*dpy
, Window win
, char **iconname
)
99 XTextProperty text_prop
;
103 if (XGetWMIconName(dpy
, win
, &text_prop
) != 0 && text_prop
.value
&& text_prop
.nitems
> 0) {
104 if (text_prop
.encoding
== XA_STRING
)
105 *iconname
= (char *)text_prop
.value
;
107 text_prop
.nitems
= strlen((char *)text_prop
.value
);
108 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >= Success
&& num
> 0 && *list
) {
109 XFree(text_prop
.value
);
110 *iconname
= wstrdup(*list
);
111 XFreeStringList(list
);
113 *iconname
= (char *)text_prop
.value
;
121 static void eatExpose(void)
125 /* compress all expose events into a single one */
127 if (XCheckMaskEvent(dpy
, ExposureMask
, &event
)) {
128 /* ignore other exposure events for this window */
129 while (XCheckWindowEvent(dpy
, event
.xexpose
.window
, ExposureMask
, &foo
)) ;
130 /* eat exposes for other windows */
133 event
.xexpose
.count
= 0;
134 XPutBackEvent(dpy
, &event
);
138 void move_window(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
141 if (wPreferences
.no_animations
)
142 XMoveWindow(dpy
, win
, to_x
, to_y
);
144 SlideWindow(win
, from_x
, from_y
, to_x
, to_y
);
146 XMoveWindow(dpy
, win
, to_x
, to_y
);
150 void SlideWindow(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
152 Window
*wins
[1] = { &win
};
153 SlideWindows(wins
, 1, from_x
, from_y
, to_x
, to_y
);
156 /* wins is an array of Window, sorted from left to right, the first is
157 * going to be moved from (from_x,from_y) to (to_x,to_y) and the
158 * following windows are going to be offset by (ICON_SIZE*i,0) */
159 void SlideWindows(Window
*wins
[], int n
, int from_x
, int from_y
, int to_x
, int to_y
)
161 time_t time0
= time(NULL
);
162 float dx
, dy
, x
= from_x
, y
= from_y
, px
, py
;
163 Bool is_dx_nul
, is_dy_nul
;
164 int dx_is_bigger
= 0, dx_int
, dy_int
;
165 int slide_delay
, slide_steps
, slide_slowdown
;
168 /* animation parameters */
169 static const struct {
174 {ICON_SLIDE_DELAY_UF
, ICON_SLIDE_STEPS_UF
, ICON_SLIDE_SLOWDOWN_UF
},
175 {ICON_SLIDE_DELAY_F
, ICON_SLIDE_STEPS_F
, ICON_SLIDE_SLOWDOWN_F
},
176 {ICON_SLIDE_DELAY_M
, ICON_SLIDE_STEPS_M
, ICON_SLIDE_SLOWDOWN_M
},
177 {ICON_SLIDE_DELAY_S
, ICON_SLIDE_STEPS_S
, ICON_SLIDE_SLOWDOWN_S
},
178 {ICON_SLIDE_DELAY_US
, ICON_SLIDE_STEPS_US
, ICON_SLIDE_SLOWDOWN_US
}
181 slide_slowdown
= apars
[(int)wPreferences
.icon_slide_speed
].slowdown
;
182 slide_steps
= apars
[(int)wPreferences
.icon_slide_speed
].steps
;
183 slide_delay
= apars
[(int)wPreferences
.icon_slide_speed
].delay
;
185 dx_int
= to_x
- from_x
;
186 dy_int
= to_y
- from_y
;
187 is_dx_nul
= (dx_int
== 0);
188 is_dy_nul
= (dy_int
== 0);
192 if (abs(dx_int
) > abs(dy_int
)) {
197 px
= dx
/ slide_slowdown
;
198 if (px
< slide_steps
&& px
> 0)
200 else if (px
> -slide_steps
&& px
< 0)
202 py
= (is_dx_nul
? 0.0 : px
* dy
/ dx
);
204 py
= dy
/ slide_slowdown
;
205 if (py
< slide_steps
&& py
> 0)
207 else if (py
> -slide_steps
&& py
< 0)
209 px
= (is_dy_nul
? 0.0 : py
* dx
/ dy
);
212 while (((int)x
) != to_x
||
216 if ((px
< 0 && (int)x
< to_x
) || (px
> 0 && (int)x
> to_x
))
218 if ((py
< 0 && (int)y
< to_y
) || (py
> 0 && (int)y
> to_y
))
222 px
= px
* (1.0 - 1 / (float)slide_slowdown
);
223 if (px
< slide_steps
&& px
> 0)
225 else if (px
> -slide_steps
&& px
< 0)
227 py
= (is_dx_nul
? 0.0 : px
* dy
/ dx
);
229 py
= py
* (1.0 - 1 / (float)slide_slowdown
);
230 if (py
< slide_steps
&& py
> 0)
232 else if (py
> -slide_steps
&& py
< 0)
234 px
= (is_dy_nul
? 0.0 : py
* dx
/ dy
);
237 for (i
= 0; i
< n
; i
++) {
238 XMoveWindow(dpy
, *wins
[i
], (int)x
+ i
* ICON_SIZE
, (int)y
);
241 if (slide_delay
> 0) {
242 wusleep(slide_delay
* 1000L);
246 if (time(NULL
) - time0
> MAX_ANIMATION_TIME
)
249 for (i
= 0; i
< n
; i
++) {
250 XMoveWindow(dpy
, *wins
[i
], to_x
+ i
* ICON_SIZE
, to_y
);
254 /* compress expose events */
258 char *ShrinkString(WMFont
*font
, const char *string
, int width
)
267 w
= WMWidthOfString(font
, string
, p
);
268 text
= wmalloc(strlen(string
) + 8);
269 strcpy(text
, string
);
273 pos
= strchr(text
, ' ');
275 pos
= strchr(text
, ':');
280 w1
= WMWidthOfString(font
, text
, p
);
296 width
-= WMWidthOfString(font
, "...", 3);
301 while (p2
> p1
&& p1
!= t
) {
302 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
305 t
= p1
+ (p2
- p1
) / 2;
306 } else if (w
< width
) {
308 t
= p1
+ (p2
- p1
) / 2;
312 strcat(text
, &string
[p
- p1
]);
317 char *FindImage(const char *paths
, const char *file
)
319 char *tmp
, *path
= NULL
;
321 tmp
= strrchr(file
, ':');
324 path
= wfindfile(paths
, file
);
328 path
= wfindfile(paths
, file
);
333 static void timeoutHandler(void *data
)
338 static char *getTextSelection(WScreen
* screen
, Atom selection
)
372 data
= XFetchBuffer(dpy
, &size
, buffer
);
379 unsigned long len
, bytes
;
383 static Atom clipboard
= 0;
386 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
388 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
390 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
392 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
394 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
397 WMDeleteTimerHandler(timer
);
399 wwarning("selection retrieval timed out");
403 /* nobody owns the selection or the current owner has
404 * nothing to do with what we need */
405 if (ev
.xselection
.property
== None
) {
409 if (XGetWindowProperty(dpy
, screen
->info_window
,
411 False
, XA_STRING
, &rtype
, &bits
, &len
,
412 &bytes
, (unsigned char **)&data
) != Success
) {
415 if (rtype
!= XA_STRING
|| bits
!= 8) {
416 wwarning("invalid data in text selection");
425 static char *getselection(WScreen
* scr
)
429 tmp
= getTextSelection(scr
, XA_PRIMARY
);
431 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
436 parseuserinputpart(const char *line
, int *ptr
, const char *endchars
)
438 int depth
= 0, begin
;
442 while(line
[*ptr
] != '\0') {
443 if(line
[*ptr
] == '(') {
445 } else if(depth
> 0 && line
[*ptr
] == ')') {
447 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
448 value
= wmalloc(*ptr
- begin
+ 1);
449 strncpy(value
, line
+ begin
, *ptr
- begin
);
450 value
[*ptr
- begin
] = '\0';
460 getuserinput(WScreen
*scr
, const char *line
, int *ptr
, Bool advanced
)
462 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
465 if(line
[*ptr
] == '(')
466 title
= parseuserinputpart(line
, ptr
, ",)");
467 if(title
!= NULL
&& line
[*ptr
] == ',')
468 prompt
= parseuserinputpart(line
, ptr
, ",)");
469 if(prompt
!= NULL
&& line
[*ptr
] == ',')
470 name
= parseuserinputpart(line
, ptr
, ")");
473 rv
= wAdvancedInputDialog(scr
,
474 title
? _(title
):_("Program Arguments"),
475 prompt
? _(prompt
):_("Enter command arguments:"),
478 rv
= wInputDialog(scr
,
479 title
? _(title
):_("Program Arguments"),
480 prompt
? _(prompt
):_("Enter command arguments:"),
483 if(title
) wfree(title
);
484 if(prompt
) wfree(prompt
);
485 if(name
) wfree(name
);
487 return rv
? ret
: NULL
;
495 * state input new-state output
496 * NORMAL % OPTION <nil>
497 * NORMAL \ ESCAPE <nil>
498 * NORMAL etc. NORMAL <input>
499 * ESCAPE any NORMAL <input>
500 * OPTION s NORMAL <selection buffer>
501 * OPTION w NORMAL <selected window id>
502 * OPTION a NORMAL <input text>
503 * OPTION d NORMAL <OffiX DND selection object>
504 * OPTION W NORMAL <current workspace>
505 * OPTION etc. NORMAL %<input>
507 #define TMPBUFSIZE 64
508 char *ExpandOptions(WScreen
*scr
, const char *cmdline
)
510 int ptr
, optr
, state
, len
, olen
;
512 char *selection
= NULL
;
513 char *user_input
= NULL
;
515 char *dropped_thing
= NULL
;
517 char tmpbuf
[TMPBUFSIZE
];
520 len
= strlen(cmdline
);
524 wwarning(_("out of memory during expansion of \"%s\""), cmdline
);
528 ptr
= 0; /* input line pointer */
529 optr
= 0; /* output line pointer */
534 switch (cmdline
[ptr
]) {
543 out
[optr
++] = cmdline
[ptr
];
548 switch (cmdline
[ptr
]) {
562 out
[optr
++] = cmdline
[ptr
];
568 switch (cmdline
[ptr
]) {
570 if (scr
->focused_window
&& scr
->focused_window
->flags
.focused
) {
571 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
572 (unsigned int)scr
->focused_window
->client_win
);
573 slen
= strlen(tmpbuf
);
575 nout
= realloc(out
, olen
);
577 wwarning(_("out of memory during expansion of \"%%w\""));
589 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x", (unsigned int)w_global
.workspace
.current
+ 1);
590 slen
= strlen(tmpbuf
);
592 nout
= realloc(out
, olen
);
594 wwarning(_("out of memory during expansion of \"%%W\""));
605 user_input
= getuserinput(scr
, cmdline
, &ptr
, cmdline
[ptr
-1] == 'A');
607 slen
= strlen(user_input
);
609 nout
= realloc(out
, olen
);
611 wwarning(_("out of memory during expansion of \"%%a\""));
615 strcat(out
, user_input
);
618 /* Not an error, but user has Canceled the dialog box.
619 * This will make the command to not be performed. */
626 if (scr
->xdestring
) {
627 dropped_thing
= wstrdup(scr
->xdestring
);
629 if (!dropped_thing
) {
630 dropped_thing
= get_dnd_selection(scr
);
632 if (!dropped_thing
) {
633 scr
->flags
.dnd_data_convertion_status
= 1;
636 slen
= strlen(dropped_thing
);
638 nout
= realloc(out
, olen
);
640 wwarning(_("out of memory during expansion of \"%%d\""));
644 strcat(out
, dropped_thing
);
651 selection
= getselection(scr
);
654 wwarning(_("selection not available"));
657 slen
= strlen(selection
);
659 nout
= realloc(out
, olen
);
661 wwarning(_("out of memory during expansion of \"%%s\""));
665 strcat(out
, selection
);
671 out
[optr
++] = cmdline
[ptr
];
689 void ParseWindowName(WMPropList
*value
, char **winstance
, char **wclass
, const char *where
)
693 *winstance
= *wclass
= NULL
;
695 if (!WMIsPLString(value
)) {
696 wwarning(_("bad window name value in %s state info"), where
);
700 name
= WMGetFromPLString(value
);
701 if (!name
|| strlen(name
) == 0) {
702 wwarning(_("bad window name value in %s state info"), where
);
706 UnescapeWM_CLASS(name
, winstance
, wclass
);
710 static char *keysymToString(KeySym keysym
, unsigned int state
)
713 char *buf
= wmalloc(20);
718 kev
.send_event
= False
;
719 kev
.window
= DefaultRootWindow(dpy
);
720 kev
.root
= DefaultRootWindow(dpy
);
721 kev
.same_screen
= True
;
722 kev
.subwindow
= kev
.root
;
723 kev
.serial
= 0x12344321;
724 kev
.time
= CurrentTime
;
726 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
727 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
734 char *GetShortcutString(const char *shortcut
)
742 tmp
= text
= wstrdup(shortcut
);
745 while ((k
= strchr(text
, '+')) != NULL
) {
749 mod
= wXModifierFromKey(text
);
751 return wstrdup("bug");
754 if (strcasecmp(text
, "Meta") == 0) {
755 buffer
= wstrappend(buffer
, "M+");
756 } else if (strcasecmp(text
, "Alt") == 0) {
757 buffer
= wstrappend(buffer
, "A+");
758 } else if (strcasecmp(text
, "Shift") == 0) {
759 buffer
= wstrappend(buffer
, "Sh+");
760 } else if (strcasecmp(text
, "Mod1") == 0) {
761 buffer
= wstrappend(buffer
, "M1+");
762 } else if (strcasecmp(text
, "Mod2") == 0) {
763 buffer
= wstrappend(buffer
, "M2+");
764 } else if (strcasecmp(text
, "Mod3") == 0) {
765 buffer
= wstrappend(buffer
, "M3+");
766 } else if (strcasecmp(text
, "Mod4") == 0) {
767 buffer
= wstrappend(buffer
, "M4+");
768 } else if (strcasecmp(text
, "Mod5") == 0) {
769 buffer
= wstrappend(buffer
, "M5+");
770 } else if (strcasecmp(text
, "Control") == 0) {
773 buffer
= wstrappend(buffer
, text
);
779 buffer
= wstrappend(buffer
, "^");
781 buffer
= wstrappend(buffer
, text
);
787 char *GetShortcutKey(WShortKey key
)
790 char *k
= XKeysymToString(XkbKeycodeToKeysym(dpy
, key
.keycode
, 0, 0));
793 char **m
= wPreferences
.modifier_labels
;
794 if (key
.modifier
& ControlMask
) tmp
= wstrappend(tmp
, m
[1] ? m
[1] : "Ctrl+");
795 if (key
.modifier
& ShiftMask
) tmp
= wstrappend(tmp
, m
[0] ? m
[0] : "Shift+");
796 if (key
.modifier
& Mod1Mask
) tmp
= wstrappend(tmp
, m
[2] ? m
[2] : "Mod1+");
797 if (key
.modifier
& Mod2Mask
) tmp
= wstrappend(tmp
, m
[3] ? m
[3] : "Mod2+");
798 if (key
.modifier
& Mod3Mask
) tmp
= wstrappend(tmp
, m
[4] ? m
[4] : "Mod3+");
799 if (key
.modifier
& Mod4Mask
) tmp
= wstrappend(tmp
, m
[5] ? m
[5] : "Mod4+");
800 if (key
.modifier
& Mod5Mask
) tmp
= wstrappend(tmp
, m
[6] ? m
[6] : "Mod5+");
801 tmp
= wstrappend(tmp
, k
);
803 return GetShortcutString(tmp
);
806 char *EscapeWM_CLASS(const char *name
, const char *class)
809 char *ename
= NULL
, *eclass
= NULL
;
817 ename
= wmalloc(l
* 2 + 1);
819 for (i
= 0; i
< l
; i
++) {
820 if (name
[i
] == '\\') {
822 } else if (name
[i
] == '.') {
825 ename
[j
++] = name
[i
];
831 eclass
= wmalloc(l
* 2 + 1);
833 for (i
= 0; i
< l
; i
++) {
834 if (class[i
] == '\\') {
836 } else if (class[i
] == '.') {
839 eclass
[j
++] = class[i
];
844 if (ename
&& eclass
) {
845 int len
= strlen(ename
) + strlen(eclass
) + 4;
847 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
851 ret
= wstrdup(ename
);
854 ret
= wstrdup(eclass
);
861 static void UnescapeWM_CLASS(const char *str
, char **name
, char **class)
871 /* separate string in 2 parts */
873 for (i
= 0; i
< j
; i
++) {
874 if (str
[i
] == '\\') {
877 } else if (str
[i
] == '.') {
883 /* unescape strings */
884 for (i
= 0, k
= 0; i
< dot
; i
++) {
885 if (str
[i
] == '\\') {
888 (*name
)[k
++] = str
[i
];
893 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
894 if (str
[i
] == '\\') {
897 (*class)[k
++] = str
[i
];
912 void SendHelperMessage(WScreen
*scr
, char type
, int workspace
, const char *msg
)
919 if (!scr
->flags
.backimage_helper_launched
) {
923 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
924 buffer
= wmalloc(len
+ 5);
925 snprintf(buf
, sizeof(buf
), "%4i", len
);
926 memcpy(buffer
, buf
, 4);
929 if (workspace
>= 0) {
930 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
931 memcpy(&buffer
[i
], buf
, 4);
936 strcpy(&buffer
[i
], msg
);
938 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
939 werror(_("could not send message to background image helper"));
944 Bool
UpdateDomainFile(WDDomain
* domain
)
948 WMPropList
*shared_dict
, *dict
;
949 Bool result
, freeDict
= False
;
951 dict
= domain
->dictionary
;
952 if (WMIsPLDictionary(domain
->dictionary
)) {
953 /* retrieve global system dictionary */
954 snprintf(path
, sizeof(path
), "%s/WindowMaker/%s", SYSCONFDIR
, domain
->domain_name
);
955 if (stat(path
, &stbuf
) >= 0) {
956 shared_dict
= WMReadPropListFromFile(path
);
958 if (WMIsPLDictionary(shared_dict
)) {
960 dict
= WMDeepCopyPropList(domain
->dictionary
);
961 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
963 WMReleasePropList(shared_dict
);
968 result
= WMWritePropListToFile(dict
, domain
->path
);
971 WMReleasePropList(dict
);
977 char *StrConcatDot(const char *a
, const char *b
)
987 len
= strlen(a
) + strlen(b
) + 4;
990 snprintf(str
, len
, "%s.%s", a
, b
);
995 static char *getCommandForWindow(Window win
, int elements
)
997 char **argv
, *command
= NULL
;
1000 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
1001 if (argc
> 0 && argv
!= NULL
) {
1004 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
1005 if (command
[0] == 0) {
1011 XFreeStringList(argv
);
1018 /* Free result when done */
1019 char *GetCommandForWindow(Window win
)
1021 return getCommandForWindow(win
, 0);