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>
38 #include <X11/XKBlib.h>
40 #include <WINGs/WUtil.h>
45 #include "WindowMaker.h"
53 #include "xmodifier.h"
58 #define ICON_SIZE wPreferences.icon_size
60 /**** Local prototypes *****/
61 static void UnescapeWM_CLASS(const char *str
, char **name
, char **class);
63 /* XFetchName Wrapper */
64 Bool
wFetchName(Display
*dpy
, Window win
, char **winname
)
66 XTextProperty text_prop
;
70 if (XGetWMName(dpy
, win
, &text_prop
)) {
71 if (text_prop
.value
&& text_prop
.nitems
> 0) {
72 if (text_prop
.encoding
== XA_STRING
) {
73 *winname
= wstrdup((char *)text_prop
.value
);
74 XFree(text_prop
.value
);
76 text_prop
.nitems
= strlen((char *)text_prop
.value
);
77 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >=
78 Success
&& num
> 0 && *list
) {
79 XFree(text_prop
.value
);
80 *winname
= wstrdup(*list
);
81 XFreeStringList(list
);
83 *winname
= wstrdup((char *)text_prop
.value
);
84 XFree(text_prop
.value
);
88 /* the title is set, but it was set to none */
89 *winname
= wstrdup("");
93 /* the hint is probably not set */
100 /* XGetIconName Wrapper */
101 Bool
wGetIconName(Display
*dpy
, Window win
, char **iconname
)
103 XTextProperty text_prop
;
107 if (XGetWMIconName(dpy
, win
, &text_prop
) != 0 && text_prop
.value
&& text_prop
.nitems
> 0) {
108 if (text_prop
.encoding
== XA_STRING
)
109 *iconname
= (char *)text_prop
.value
;
111 text_prop
.nitems
= strlen((char *)text_prop
.value
);
112 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >= Success
&& num
> 0 && *list
) {
113 XFree(text_prop
.value
);
114 *iconname
= wstrdup(*list
);
115 XFreeStringList(list
);
117 *iconname
= (char *)text_prop
.value
;
125 static void eatExpose(void)
129 /* compress all expose events into a single one */
131 if (XCheckMaskEvent(dpy
, ExposureMask
, &event
)) {
132 /* ignore other exposure events for this window */
133 while (XCheckWindowEvent(dpy
, event
.xexpose
.window
, ExposureMask
, &foo
)) ;
134 /* eat exposes for other windows */
137 event
.xexpose
.count
= 0;
138 XPutBackEvent(dpy
, &event
);
142 void move_window(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
144 #ifdef USE_ANIMATIONS
145 if (wPreferences
.no_animations
)
146 XMoveWindow(dpy
, win
, to_x
, to_y
);
148 slide_window(win
, from_x
, from_y
, to_x
, to_y
);
150 XMoveWindow(dpy
, win
, to_x
, to_y
);
152 /* Tell the compiler it is normal that those parameters are not used in this case */
158 /* wins is an array of Window, sorted from left to right, the first is
159 * going to be moved from (from_x,from_y) to (to_x,to_y) and the
160 * following windows are going to be offset by (ICON_SIZE*i,0) */
161 void slide_windows(Window wins
[], int n
, int from_x
, int from_y
, int to_x
, int to_y
)
163 time_t time0
= time(NULL
);
164 float dx
, dy
, x
= from_x
, y
= from_y
, px
, py
;
165 Bool is_dx_nul
, is_dy_nul
;
166 int dx_is_bigger
= 0, dx_int
, dy_int
;
167 int slide_delay
, slide_steps
, slide_slowdown
;
170 /* animation parameters */
171 static const struct {
176 {ICON_SLIDE_DELAY_UF
, ICON_SLIDE_STEPS_UF
, ICON_SLIDE_SLOWDOWN_UF
},
177 {ICON_SLIDE_DELAY_F
, ICON_SLIDE_STEPS_F
, ICON_SLIDE_SLOWDOWN_F
},
178 {ICON_SLIDE_DELAY_M
, ICON_SLIDE_STEPS_M
, ICON_SLIDE_SLOWDOWN_M
},
179 {ICON_SLIDE_DELAY_S
, ICON_SLIDE_STEPS_S
, ICON_SLIDE_SLOWDOWN_S
},
180 {ICON_SLIDE_DELAY_US
, ICON_SLIDE_STEPS_US
, ICON_SLIDE_SLOWDOWN_US
}
183 slide_slowdown
= apars
[(int)wPreferences
.icon_slide_speed
].slowdown
;
184 slide_steps
= apars
[(int)wPreferences
.icon_slide_speed
].steps
;
185 slide_delay
= apars
[(int)wPreferences
.icon_slide_speed
].delay
;
187 dx_int
= to_x
- from_x
;
188 dy_int
= to_y
- from_y
;
189 is_dx_nul
= (dx_int
== 0);
190 is_dy_nul
= (dy_int
== 0);
194 if (abs(dx_int
) > abs(dy_int
)) {
199 px
= dx
/ slide_slowdown
;
200 if (px
< slide_steps
&& px
> 0)
202 else if (px
> -slide_steps
&& px
< 0)
204 py
= (is_dx_nul
? 0.0F
: px
* dy
/ dx
);
206 py
= dy
/ slide_slowdown
;
207 if (py
< slide_steps
&& py
> 0)
209 else if (py
> -slide_steps
&& py
< 0)
211 px
= (is_dy_nul
? 0.0F
: py
* dx
/ dy
);
214 while (((int)x
) != to_x
||
218 if ((px
< 0 && (int)x
< to_x
) || (px
> 0 && (int)x
> to_x
))
220 if ((py
< 0 && (int)y
< to_y
) || (py
> 0 && (int)y
> to_y
))
224 px
= px
* (1.0F
- 1 / (float)slide_slowdown
);
225 if (px
< slide_steps
&& px
> 0)
227 else if (px
> -slide_steps
&& px
< 0)
229 py
= (is_dx_nul
? 0.0F
: px
* dy
/ dx
);
231 py
= py
* (1.0F
- 1 / (float)slide_slowdown
);
232 if (py
< slide_steps
&& py
> 0)
234 else if (py
> -slide_steps
&& py
< 0)
236 px
= (is_dy_nul
? 0.0F
: py
* dx
/ dy
);
239 for (i
= 0; i
< n
; i
++) {
240 XMoveWindow(dpy
, wins
[i
], (int)x
+ i
* ICON_SIZE
, (int)y
);
243 if (slide_delay
> 0) {
244 wusleep(slide_delay
* 1000L);
248 if (time(NULL
) - time0
> MAX_ANIMATION_TIME
)
251 for (i
= 0; i
< n
; i
++) {
252 XMoveWindow(dpy
, wins
[i
], to_x
+ i
* ICON_SIZE
, to_y
);
256 /* compress expose events */
260 char *ShrinkString(WMFont
*font
, const char *string
, int width
)
269 w
= WMWidthOfString(font
, string
, p
);
270 text
= wmalloc(strlen(string
) + 8);
271 strcpy(text
, string
);
275 pos
= strchr(text
, ' ');
277 pos
= strchr(text
, ':');
282 w1
= WMWidthOfString(font
, text
, p
);
298 width
-= WMWidthOfString(font
, "...", 3);
303 while (p2
> p1
&& p1
!= t
) {
304 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
307 t
= p1
+ (p2
- p1
) / 2;
308 } else if (w
< width
) {
310 t
= p1
+ (p2
- p1
) / 2;
314 strcat(text
, &string
[p
- p1
]);
319 char *FindImage(const char *paths
, const char *file
)
321 char *tmp
, *path
= NULL
;
323 tmp
= strrchr(file
, ':');
326 path
= wfindfile(paths
, file
);
330 path
= wfindfile(paths
, file
);
335 static void timeoutHandler(void *data
)
340 static char *getTextSelection(WScreen
* screen
, Atom selection
)
374 data
= XFetchBuffer(dpy
, &size
, buffer
);
381 unsigned long len
, bytes
;
385 static Atom clipboard
= 0;
388 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
390 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
392 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
394 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
396 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
399 WMDeleteTimerHandler(timer
);
401 wwarning("selection retrieval timed out");
405 /* nobody owns the selection or the current owner has
406 * nothing to do with what we need */
407 if (ev
.xselection
.property
== None
) {
411 if (XGetWindowProperty(dpy
, screen
->info_window
,
413 False
, XA_STRING
, &rtype
, &bits
, &len
,
414 &bytes
, (unsigned char **)&data
) != Success
) {
417 if (rtype
!= XA_STRING
|| bits
!= 8) {
418 wwarning("invalid data in text selection");
427 static char *getselection(WScreen
* scr
)
431 tmp
= getTextSelection(scr
, XA_PRIMARY
);
433 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
438 parseuserinputpart(const char *line
, int *ptr
, const char *endchars
)
440 int depth
= 0, begin
;
444 while(line
[*ptr
] != '\0') {
445 if(line
[*ptr
] == '(') {
447 } else if(depth
> 0 && line
[*ptr
] == ')') {
449 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
450 value
= wmalloc(*ptr
- begin
+ 1);
451 strncpy(value
, line
+ begin
, *ptr
- begin
);
452 value
[*ptr
- begin
] = '\0';
462 getuserinput(WScreen
*scr
, const char *line
, int *ptr
, Bool advanced
)
464 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
467 if(line
[*ptr
] == '(')
468 title
= parseuserinputpart(line
, ptr
, ",)");
469 if(title
!= NULL
&& line
[*ptr
] == ',')
470 prompt
= parseuserinputpart(line
, ptr
, ",)");
471 if(prompt
!= NULL
&& line
[*ptr
] == ',')
472 name
= parseuserinputpart(line
, ptr
, ")");
475 rv
= wAdvancedInputDialog(scr
,
476 title
? _(title
):_("Program Arguments"),
477 prompt
? _(prompt
):_("Enter command arguments:"),
480 rv
= wInputDialog(scr
,
481 title
? _(title
):_("Program Arguments"),
482 prompt
? _(prompt
):_("Enter command arguments:"),
485 if(title
) wfree(title
);
486 if(prompt
) wfree(prompt
);
487 if(name
) wfree(name
);
489 return rv
? ret
: NULL
;
497 * state input new-state output
498 * NORMAL % OPTION <nil>
499 * NORMAL \ ESCAPE <nil>
500 * NORMAL etc. NORMAL <input>
501 * ESCAPE any NORMAL <input>
502 * OPTION s NORMAL <selection buffer>
503 * OPTION w NORMAL <selected window id>
504 * OPTION a NORMAL <input text>
505 * OPTION d NORMAL <OffiX DND selection object>
506 * OPTION W NORMAL <current workspace>
507 * OPTION etc. NORMAL %<input>
509 #define TMPBUFSIZE 64
510 char *ExpandOptions(WScreen
*scr
, const char *cmdline
)
512 int ptr
, optr
, state
, len
, olen
;
514 char *selection
= NULL
;
515 char *user_input
= NULL
;
516 char tmpbuf
[TMPBUFSIZE
];
519 len
= strlen(cmdline
);
523 wwarning(_("out of memory during expansion of \"%s\""), cmdline
);
527 ptr
= 0; /* input line pointer */
528 optr
= 0; /* output line pointer */
533 switch (cmdline
[ptr
]) {
542 out
[optr
++] = cmdline
[ptr
];
547 switch (cmdline
[ptr
]) {
561 out
[optr
++] = cmdline
[ptr
];
567 switch (cmdline
[ptr
]) {
569 if (scr
->focused_window
&& scr
->focused_window
->flags
.focused
) {
570 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
571 (unsigned int)scr
->focused_window
->client_win
);
572 slen
= strlen(tmpbuf
);
574 nout
= realloc(out
, olen
);
576 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%w", cmdline
);
588 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x", (unsigned int)scr
->current_workspace
+ 1);
589 slen
= strlen(tmpbuf
);
591 nout
= realloc(out
, olen
);
593 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%W", cmdline
);
604 user_input
= getuserinput(scr
, cmdline
, &ptr
, cmdline
[ptr
-1] == 'A');
606 slen
= strlen(user_input
);
608 nout
= realloc(out
, olen
);
610 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%a", cmdline
);
614 strcat(out
, user_input
);
617 /* Not an error, but user has Canceled the dialog box.
618 * This will make the command to not be performed. */
625 if (!scr
->xdestring
) {
626 scr
->flags
.dnd_data_convertion_status
= 1;
629 slen
= strlen(scr
->xdestring
);
631 nout
= realloc(out
, olen
);
633 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%d", cmdline
);
637 strcat(out
, scr
->xdestring
);
640 #endif /* USE_DOCK_XDND */
644 selection
= getselection(scr
);
647 wwarning(_("selection not available"));
650 slen
= strlen(selection
);
652 nout
= realloc(out
, olen
);
654 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%s", cmdline
);
658 strcat(out
, selection
);
664 out
[optr
++] = cmdline
[ptr
];
682 void ParseWindowName(WMPropList
*value
, char **winstance
, char **wclass
, const char *where
)
686 *winstance
= *wclass
= NULL
;
688 if (!WMIsPLString(value
)) {
689 wwarning(_("bad window name value in %s state info"), where
);
693 name
= WMGetFromPLString(value
);
694 if (!name
|| strlen(name
) == 0) {
695 wwarning(_("bad window name value in %s state info"), where
);
699 UnescapeWM_CLASS(name
, winstance
, wclass
);
703 static char *keysymToString(KeySym keysym
, unsigned int state
)
706 char *buf
= wmalloc(20);
711 kev
.send_event
= False
;
712 kev
.window
= DefaultRootWindow(dpy
);
713 kev
.root
= DefaultRootWindow(dpy
);
714 kev
.same_screen
= True
;
715 kev
.subwindow
= kev
.root
;
716 kev
.serial
= 0x12344321;
717 kev
.time
= CurrentTime
;
719 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
720 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
727 char *GetShortcutString(const char *shortcut
)
730 char *k
, *tmp
, *text
;
732 tmp
= text
= wstrdup(shortcut
);
735 while ((k
= strchr(text
, '+')) != NULL
) {
740 mod
= wXModifierFromKey(text
);
742 return wstrdup("bug");
744 lbl
= wXModifierToShortcutLabel(mod
);
746 buffer
= wstrappend(buffer
, lbl
);
748 buffer
= wstrappend(buffer
, text
);
752 buffer
= wstrappend(buffer
, text
);
758 char *GetShortcutKey(WShortKey key
)
760 const char *key_name
;
764 void append_string(const char *text
)
766 const char *string
= text
;
769 if (wr
>= buffer
+ sizeof(buffer
) - 1)
775 void append_modifier(int modifier_index
, const char *fallback_name
)
777 if (wPreferences
.modifier_labels
[modifier_index
]) {
778 append_string(wPreferences
.modifier_labels
[modifier_index
]);
780 append_string(fallback_name
);
784 key_name
= XKeysymToString(XkbKeycodeToKeysym(dpy
, key
.keycode
, 0, 0));
789 if (key
.modifier
& ControlMask
) append_modifier(1, "Control+");
790 if (key
.modifier
& ShiftMask
) append_modifier(0, "Shift+");
791 if (key
.modifier
& Mod1Mask
) append_modifier(2, "Mod1+");
792 if (key
.modifier
& Mod2Mask
) append_modifier(3, "Mod2+");
793 if (key
.modifier
& Mod3Mask
) append_modifier(4, "Mod3+");
794 if (key
.modifier
& Mod4Mask
) append_modifier(5, "Mod4+");
795 if (key
.modifier
& Mod5Mask
) append_modifier(6, "Mod5+");
796 append_string(key_name
);
799 return GetShortcutString(buffer
);
802 char *EscapeWM_CLASS(const char *name
, const char *class)
805 char *ename
= NULL
, *eclass
= NULL
;
813 ename
= wmalloc(l
* 2 + 1);
815 for (i
= 0; i
< l
; i
++) {
816 if (name
[i
] == '\\') {
818 } else if (name
[i
] == '.') {
821 ename
[j
++] = name
[i
];
827 eclass
= wmalloc(l
* 2 + 1);
829 for (i
= 0; i
< l
; i
++) {
830 if (class[i
] == '\\') {
832 } else if (class[i
] == '.') {
835 eclass
[j
++] = class[i
];
840 if (ename
&& eclass
) {
841 int len
= strlen(ename
) + strlen(eclass
) + 4;
843 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
847 ret
= wstrdup(ename
);
850 ret
= wstrdup(eclass
);
857 static void UnescapeWM_CLASS(const char *str
, char **name
, char **class)
864 /* separate string in 2 parts */
867 for (i
= 0; i
< j
; i
++, length_of_name
++) {
868 if (str
[i
] == '\\') {
871 } else if (str
[i
] == '.') {
877 /* unescape the name */
878 if (length_of_name
> 0) {
879 *name
= wmalloc(length_of_name
+ 1);
880 for (i
= 0, k
= 0; i
< dot
; i
++) {
882 (*name
)[k
++] = str
[i
];
889 /* unescape the class */
891 *class = wmalloc(j
- (dot
+ 1) + 1);
892 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
894 (*class)[k
++] = str
[i
];
902 static void track_bg_helper_death(pid_t pid
, unsigned int status
, void *client_data
)
904 WScreen
*scr
= (WScreen
*) client_data
;
906 /* Parameter not used, but tell the compiler that it is ok */
910 close(scr
->helper_fd
);
913 scr
->flags
.backimage_helper_launched
= 0;
916 Bool
start_bg_helper(WScreen
*scr
)
921 if (pipe(filedes
) < 0) {
922 werror(_("%s failed, can't set workspace specific background image (%s)"),
923 "pipe()", strerror(errno
));
929 werror(_("%s failed, can't set workspace specific background image (%s)"),
930 "fork()", strerror(errno
));
935 } else if (pid
== 0) {
938 /* We don't need this side of the pipe in the child process */
941 SetupEnvironment(scr
);
944 if (dup2(filedes
[0], STDIN_FILENO
) < 0) {
945 werror(_("%s failed, can't set workspace specific background image (%s)"),
946 "dup2()", strerror(errno
));
951 dither
= wPreferences
.no_dithering
? "-m" : "-d";
952 if (wPreferences
.smooth_workspace_back
)
953 execlp("wmsetbg", "wmsetbg", "-helper", "-S", dither
, NULL
);
955 execlp("wmsetbg", "wmsetbg", "-helper", dither
, NULL
);
957 werror(_("could not execute \"%s\": %s"), "wmsetbg", strerror(errno
));
961 /* We don't need this side of the pipe in the parent process */
964 if (fcntl(filedes
[1], F_SETFD
, FD_CLOEXEC
) < 0)
965 wwarning(_("could not set close-on-exec flag for bg_helper's communication file handle (%s)"),
968 scr
->helper_fd
= filedes
[1];
969 scr
->helper_pid
= pid
;
970 scr
->flags
.backimage_helper_launched
= 1;
972 wAddDeathHandler(pid
, track_bg_helper_death
, scr
);
978 void SendHelperMessage(WScreen
*scr
, char type
, int workspace
, const char *msg
)
985 if (!scr
->flags
.backimage_helper_launched
) {
989 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
990 buffer
= wmalloc(len
+ 5);
991 snprintf(buf
, sizeof(buf
), "%4i", len
);
992 memcpy(buffer
, buf
, 4);
995 if (workspace
>= 0) {
996 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
997 memcpy(&buffer
[i
], buf
, 4);
1002 strcpy(&buffer
[i
], msg
);
1004 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
1005 werror(_("could not send message to background image helper"));
1010 Bool
UpdateDomainFile(WDDomain
* domain
)
1013 char path
[PATH_MAX
];
1014 WMPropList
*shared_dict
, *dict
;
1015 Bool result
, freeDict
= False
;
1017 dict
= domain
->dictionary
;
1018 if (WMIsPLDictionary(domain
->dictionary
)) {
1019 /* retrieve global system dictionary */
1020 snprintf(path
, sizeof(path
), "%s/%s", DEFSDATADIR
, domain
->domain_name
);
1021 if (stat(path
, &stbuf
) >= 0) {
1022 shared_dict
= WMReadPropListFromFile(path
);
1024 if (WMIsPLDictionary(shared_dict
)) {
1026 dict
= WMDeepCopyPropList(domain
->dictionary
);
1027 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
1029 WMReleasePropList(shared_dict
);
1034 result
= WMWritePropListToFile(dict
, domain
->path
);
1037 WMReleasePropList(dict
);
1043 char *StrConcatDot(const char *a
, const char *b
)
1053 len
= strlen(a
) + strlen(b
) + 4;
1056 snprintf(str
, len
, "%s.%s", a
, b
);
1061 static char *getCommandForWindow(Window win
, int elements
)
1063 char **argv
, *command
= NULL
;
1066 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
1067 if (argc
> 0 && argv
!= NULL
) {
1070 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
1071 if (command
[0] == 0) {
1077 XFreeStringList(argv
);
1084 /* Free result when done */
1085 char *GetCommandForWindow(Window win
)
1087 return getCommandForWindow(win
, 0);