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
)
145 if (wPreferences
.no_animations
)
146 XMoveWindow(dpy
, win
, to_x
, to_y
);
148 SlideWindow(win
, from_x
, from_y
, to_x
, to_y
);
150 XMoveWindow(dpy
, win
, to_x
, to_y
);
154 void SlideWindow(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
156 Window
*wins
[1] = { &win
};
157 SlideWindows(wins
, 1, from_x
, from_y
, to_x
, to_y
);
160 /* wins is an array of Window, sorted from left to right, the first is
161 * going to be moved from (from_x,from_y) to (to_x,to_y) and the
162 * following windows are going to be offset by (ICON_SIZE*i,0) */
163 void SlideWindows(Window
*wins
[], int n
, int from_x
, int from_y
, int to_x
, int to_y
)
165 time_t time0
= time(NULL
);
166 float dx
, dy
, x
= from_x
, y
= from_y
, px
, py
;
167 Bool is_dx_nul
, is_dy_nul
;
168 int dx_is_bigger
= 0, dx_int
, dy_int
;
169 int slide_delay
, slide_steps
, slide_slowdown
;
172 /* animation parameters */
173 static const struct {
178 {ICON_SLIDE_DELAY_UF
, ICON_SLIDE_STEPS_UF
, ICON_SLIDE_SLOWDOWN_UF
},
179 {ICON_SLIDE_DELAY_F
, ICON_SLIDE_STEPS_F
, ICON_SLIDE_SLOWDOWN_F
},
180 {ICON_SLIDE_DELAY_M
, ICON_SLIDE_STEPS_M
, ICON_SLIDE_SLOWDOWN_M
},
181 {ICON_SLIDE_DELAY_S
, ICON_SLIDE_STEPS_S
, ICON_SLIDE_SLOWDOWN_S
},
182 {ICON_SLIDE_DELAY_US
, ICON_SLIDE_STEPS_US
, ICON_SLIDE_SLOWDOWN_US
}
185 slide_slowdown
= apars
[(int)wPreferences
.icon_slide_speed
].slowdown
;
186 slide_steps
= apars
[(int)wPreferences
.icon_slide_speed
].steps
;
187 slide_delay
= apars
[(int)wPreferences
.icon_slide_speed
].delay
;
189 dx_int
= to_x
- from_x
;
190 dy_int
= to_y
- from_y
;
191 is_dx_nul
= (dx_int
== 0);
192 is_dy_nul
= (dy_int
== 0);
196 if (abs(dx_int
) > abs(dy_int
)) {
201 px
= dx
/ slide_slowdown
;
202 if (px
< slide_steps
&& px
> 0)
204 else if (px
> -slide_steps
&& px
< 0)
206 py
= (is_dx_nul
? 0.0 : px
* dy
/ dx
);
208 py
= dy
/ slide_slowdown
;
209 if (py
< slide_steps
&& py
> 0)
211 else if (py
> -slide_steps
&& py
< 0)
213 px
= (is_dy_nul
? 0.0 : py
* dx
/ dy
);
216 while (((int)x
) != to_x
||
220 if ((px
< 0 && (int)x
< to_x
) || (px
> 0 && (int)x
> to_x
))
222 if ((py
< 0 && (int)y
< to_y
) || (py
> 0 && (int)y
> to_y
))
226 px
= px
* (1.0 - 1 / (float)slide_slowdown
);
227 if (px
< slide_steps
&& px
> 0)
229 else if (px
> -slide_steps
&& px
< 0)
231 py
= (is_dx_nul
? 0.0 : px
* dy
/ dx
);
233 py
= py
* (1.0 - 1 / (float)slide_slowdown
);
234 if (py
< slide_steps
&& py
> 0)
236 else if (py
> -slide_steps
&& py
< 0)
238 px
= (is_dy_nul
? 0.0 : py
* dx
/ dy
);
241 for (i
= 0; i
< n
; i
++) {
242 XMoveWindow(dpy
, *wins
[i
], (int)x
+ i
* ICON_SIZE
, (int)y
);
245 if (slide_delay
> 0) {
246 wusleep(slide_delay
* 1000L);
250 if (time(NULL
) - time0
> MAX_ANIMATION_TIME
)
253 for (i
= 0; i
< n
; i
++) {
254 XMoveWindow(dpy
, *wins
[i
], to_x
+ i
* ICON_SIZE
, to_y
);
258 /* compress expose events */
262 char *ShrinkString(WMFont
*font
, const char *string
, int width
)
271 w
= WMWidthOfString(font
, string
, p
);
272 text
= wmalloc(strlen(string
) + 8);
273 strcpy(text
, string
);
277 pos
= strchr(text
, ' ');
279 pos
= strchr(text
, ':');
284 w1
= WMWidthOfString(font
, text
, p
);
300 width
-= WMWidthOfString(font
, "...", 3);
305 while (p2
> p1
&& p1
!= t
) {
306 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
309 t
= p1
+ (p2
- p1
) / 2;
310 } else if (w
< width
) {
312 t
= p1
+ (p2
- p1
) / 2;
316 strcat(text
, &string
[p
- p1
]);
321 char *FindImage(const char *paths
, const char *file
)
323 char *tmp
, *path
= NULL
;
325 tmp
= strrchr(file
, ':');
328 path
= wfindfile(paths
, file
);
332 path
= wfindfile(paths
, file
);
337 static void timeoutHandler(void *data
)
342 static char *getTextSelection(WScreen
* screen
, Atom selection
)
376 data
= XFetchBuffer(dpy
, &size
, buffer
);
383 unsigned long len
, bytes
;
387 static Atom clipboard
= 0;
390 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
392 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
394 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
396 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
398 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
401 WMDeleteTimerHandler(timer
);
403 wwarning("selection retrieval timed out");
407 /* nobody owns the selection or the current owner has
408 * nothing to do with what we need */
409 if (ev
.xselection
.property
== None
) {
413 if (XGetWindowProperty(dpy
, screen
->info_window
,
415 False
, XA_STRING
, &rtype
, &bits
, &len
,
416 &bytes
, (unsigned char **)&data
) != Success
) {
419 if (rtype
!= XA_STRING
|| bits
!= 8) {
420 wwarning("invalid data in text selection");
429 static char *getselection(WScreen
* scr
)
433 tmp
= getTextSelection(scr
, XA_PRIMARY
);
435 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
440 parseuserinputpart(const char *line
, int *ptr
, const char *endchars
)
442 int depth
= 0, begin
;
446 while(line
[*ptr
] != '\0') {
447 if(line
[*ptr
] == '(') {
449 } else if(depth
> 0 && line
[*ptr
] == ')') {
451 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
452 value
= wmalloc(*ptr
- begin
+ 1);
453 strncpy(value
, line
+ begin
, *ptr
- begin
);
454 value
[*ptr
- begin
] = '\0';
464 getuserinput(WScreen
*scr
, const char *line
, int *ptr
, Bool advanced
)
466 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
469 if(line
[*ptr
] == '(')
470 title
= parseuserinputpart(line
, ptr
, ",)");
471 if(title
!= NULL
&& line
[*ptr
] == ',')
472 prompt
= parseuserinputpart(line
, ptr
, ",)");
473 if(prompt
!= NULL
&& line
[*ptr
] == ',')
474 name
= parseuserinputpart(line
, ptr
, ")");
477 rv
= wAdvancedInputDialog(scr
,
478 title
? _(title
):_("Program Arguments"),
479 prompt
? _(prompt
):_("Enter command arguments:"),
482 rv
= wInputDialog(scr
,
483 title
? _(title
):_("Program Arguments"),
484 prompt
? _(prompt
):_("Enter command arguments:"),
487 if(title
) wfree(title
);
488 if(prompt
) wfree(prompt
);
489 if(name
) wfree(name
);
491 return rv
? ret
: NULL
;
499 * state input new-state output
500 * NORMAL % OPTION <nil>
501 * NORMAL \ ESCAPE <nil>
502 * NORMAL etc. NORMAL <input>
503 * ESCAPE any NORMAL <input>
504 * OPTION s NORMAL <selection buffer>
505 * OPTION w NORMAL <selected window id>
506 * OPTION a NORMAL <input text>
507 * OPTION d NORMAL <OffiX DND selection object>
508 * OPTION W NORMAL <current workspace>
509 * OPTION etc. NORMAL %<input>
511 #define TMPBUFSIZE 64
512 char *ExpandOptions(WScreen
*scr
, const char *cmdline
)
514 int ptr
, optr
, state
, len
, olen
;
516 char *selection
= NULL
;
517 char *user_input
= NULL
;
518 char tmpbuf
[TMPBUFSIZE
];
521 len
= strlen(cmdline
);
525 wwarning(_("out of memory during expansion of \"%s\""), cmdline
);
529 ptr
= 0; /* input line pointer */
530 optr
= 0; /* output line pointer */
535 switch (cmdline
[ptr
]) {
544 out
[optr
++] = cmdline
[ptr
];
549 switch (cmdline
[ptr
]) {
563 out
[optr
++] = cmdline
[ptr
];
569 switch (cmdline
[ptr
]) {
571 if (scr
->focused_window
&& scr
->focused_window
->flags
.focused
) {
572 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
573 (unsigned int)scr
->focused_window
->client_win
);
574 slen
= strlen(tmpbuf
);
576 nout
= realloc(out
, olen
);
578 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%w", cmdline
);
590 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x", (unsigned int)scr
->current_workspace
+ 1);
591 slen
= strlen(tmpbuf
);
593 nout
= realloc(out
, olen
);
595 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%W", cmdline
);
606 user_input
= getuserinput(scr
, cmdline
, &ptr
, cmdline
[ptr
-1] == 'A');
608 slen
= strlen(user_input
);
610 nout
= realloc(out
, olen
);
612 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%a", cmdline
);
616 strcat(out
, user_input
);
619 /* Not an error, but user has Canceled the dialog box.
620 * This will make the command to not be performed. */
627 if (!scr
->xdestring
) {
628 scr
->flags
.dnd_data_convertion_status
= 1;
631 slen
= strlen(scr
->xdestring
);
633 nout
= realloc(out
, olen
);
635 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%d", cmdline
);
639 strcat(out
, scr
->xdestring
);
646 selection
= getselection(scr
);
649 wwarning(_("selection not available"));
652 slen
= strlen(selection
);
654 nout
= realloc(out
, olen
);
656 wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%s", cmdline
);
660 strcat(out
, selection
);
666 out
[optr
++] = cmdline
[ptr
];
684 void ParseWindowName(WMPropList
*value
, char **winstance
, char **wclass
, const char *where
)
688 *winstance
= *wclass
= NULL
;
690 if (!WMIsPLString(value
)) {
691 wwarning(_("bad window name value in %s state info"), where
);
695 name
= WMGetFromPLString(value
);
696 if (!name
|| strlen(name
) == 0) {
697 wwarning(_("bad window name value in %s state info"), where
);
701 UnescapeWM_CLASS(name
, winstance
, wclass
);
705 static char *keysymToString(KeySym keysym
, unsigned int state
)
708 char *buf
= wmalloc(20);
713 kev
.send_event
= False
;
714 kev
.window
= DefaultRootWindow(dpy
);
715 kev
.root
= DefaultRootWindow(dpy
);
716 kev
.same_screen
= True
;
717 kev
.subwindow
= kev
.root
;
718 kev
.serial
= 0x12344321;
719 kev
.time
= CurrentTime
;
721 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
722 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
729 char *GetShortcutString(const char *shortcut
)
732 char *k
, *tmp
, *text
;
734 tmp
= text
= wstrdup(shortcut
);
737 while ((k
= strchr(text
, '+')) != NULL
) {
742 mod
= wXModifierFromKey(text
);
744 return wstrdup("bug");
746 lbl
= wXModifierToShortcutLabel(mod
);
748 buffer
= wstrappend(buffer
, lbl
);
750 buffer
= wstrappend(buffer
, text
);
754 buffer
= wstrappend(buffer
, text
);
760 char *GetShortcutKey(WShortKey key
)
762 const char *key_name
;
766 void append_string(const char *text
)
768 const char *string
= text
;
771 if (wr
>= buffer
+ sizeof(buffer
) - 1)
777 void append_modifier(int modifier_index
, const char *fallback_name
)
779 if (wPreferences
.modifier_labels
[modifier_index
]) {
780 append_string(wPreferences
.modifier_labels
[modifier_index
]);
782 append_string(fallback_name
);
786 key_name
= XKeysymToString(XkbKeycodeToKeysym(dpy
, key
.keycode
, 0, 0));
791 if (key
.modifier
& ControlMask
) append_modifier(1, "Control+");
792 if (key
.modifier
& ShiftMask
) append_modifier(0, "Shift+");
793 if (key
.modifier
& Mod1Mask
) append_modifier(2, "Mod1+");
794 if (key
.modifier
& Mod2Mask
) append_modifier(3, "Mod2+");
795 if (key
.modifier
& Mod3Mask
) append_modifier(4, "Mod3+");
796 if (key
.modifier
& Mod4Mask
) append_modifier(5, "Mod4+");
797 if (key
.modifier
& Mod5Mask
) append_modifier(6, "Mod5+");
798 append_string(key_name
);
801 return GetShortcutString(buffer
);
804 char *EscapeWM_CLASS(const char *name
, const char *class)
807 char *ename
= NULL
, *eclass
= NULL
;
815 ename
= wmalloc(l
* 2 + 1);
817 for (i
= 0; i
< l
; i
++) {
818 if (name
[i
] == '\\') {
820 } else if (name
[i
] == '.') {
823 ename
[j
++] = name
[i
];
829 eclass
= wmalloc(l
* 2 + 1);
831 for (i
= 0; i
< l
; i
++) {
832 if (class[i
] == '\\') {
834 } else if (class[i
] == '.') {
837 eclass
[j
++] = class[i
];
842 if (ename
&& eclass
) {
843 int len
= strlen(ename
) + strlen(eclass
) + 4;
845 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
849 ret
= wstrdup(ename
);
852 ret
= wstrdup(eclass
);
859 static void UnescapeWM_CLASS(const char *str
, char **name
, char **class)
866 /* separate string in 2 parts */
869 for (i
= 0; i
< j
; i
++, length_of_name
++) {
870 if (str
[i
] == '\\') {
873 } else if (str
[i
] == '.') {
879 /* unescape the name */
880 if (length_of_name
> 0) {
881 *name
= wmalloc(length_of_name
+ 1);
882 for (i
= 0, k
= 0; i
< dot
; i
++) {
884 (*name
)[k
++] = str
[i
];
891 /* unescape the class */
893 *class = wmalloc(j
- (dot
+ 1) + 1);
894 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
896 (*class)[k
++] = str
[i
];
904 static void track_bg_helper_death(pid_t pid
, unsigned int status
, void *client_data
)
906 WScreen
*scr
= (WScreen
*) client_data
;
908 /* Parameter not used, but tell the compiler that it is ok */
912 close(scr
->helper_fd
);
915 scr
->flags
.backimage_helper_launched
= 0;
918 Bool
start_bg_helper(WScreen
*scr
)
923 if (pipe(filedes
) < 0) {
924 werror(_("%s failed, can't set workspace specific background image (%s)"),
925 "pipe()", strerror(errno
));
931 werror(_("%s failed, can't set workspace specific background image (%s)"),
932 "fork()", strerror(errno
));
937 } else if (pid
== 0) {
940 /* We don't need this side of the pipe in the child process */
943 SetupEnvironment(scr
);
946 if (dup2(filedes
[0], STDIN_FILENO
) < 0) {
947 werror(_("%s failed, can't set workspace specific background image (%s)"),
948 "dup2()", strerror(errno
));
953 dither
= wPreferences
.no_dithering
? "-m" : "-d";
954 if (wPreferences
.smooth_workspace_back
)
955 execlp("wmsetbg", "wmsetbg", "-helper", "-S", dither
, NULL
);
957 execlp("wmsetbg", "wmsetbg", "-helper", dither
, NULL
);
959 werror(_("could not execute \"%s\": %s"), "wmsetbg", strerror(errno
));
963 /* We don't need this side of the pipe in the parent process */
966 if (fcntl(filedes
[1], F_SETFD
, FD_CLOEXEC
) < 0)
967 wwarning(_("could not set close-on-exec flag for bg_helper's communication file handle (%s)"),
970 scr
->helper_fd
= filedes
[1];
971 scr
->helper_pid
= pid
;
972 scr
->flags
.backimage_helper_launched
= 1;
974 wAddDeathHandler(pid
, track_bg_helper_death
, scr
);
980 void SendHelperMessage(WScreen
*scr
, char type
, int workspace
, const char *msg
)
987 if (!scr
->flags
.backimage_helper_launched
) {
991 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
992 buffer
= wmalloc(len
+ 5);
993 snprintf(buf
, sizeof(buf
), "%4i", len
);
994 memcpy(buffer
, buf
, 4);
997 if (workspace
>= 0) {
998 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
999 memcpy(&buffer
[i
], buf
, 4);
1004 strcpy(&buffer
[i
], msg
);
1006 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
1007 werror(_("could not send message to background image helper"));
1012 Bool
UpdateDomainFile(WDDomain
* domain
)
1015 char path
[PATH_MAX
];
1016 WMPropList
*shared_dict
, *dict
;
1017 Bool result
, freeDict
= False
;
1019 dict
= domain
->dictionary
;
1020 if (WMIsPLDictionary(domain
->dictionary
)) {
1021 /* retrieve global system dictionary */
1022 snprintf(path
, sizeof(path
), "%s/WindowMaker/%s", SYSCONFDIR
, domain
->domain_name
);
1023 if (stat(path
, &stbuf
) >= 0) {
1024 shared_dict
= WMReadPropListFromFile(path
);
1026 if (WMIsPLDictionary(shared_dict
)) {
1028 dict
= WMDeepCopyPropList(domain
->dictionary
);
1029 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
1031 WMReleasePropList(shared_dict
);
1036 result
= WMWritePropListToFile(dict
, domain
->path
);
1039 WMReleasePropList(dict
);
1045 char *StrConcatDot(const char *a
, const char *b
)
1055 len
= strlen(a
) + strlen(b
) + 4;
1058 snprintf(str
, len
, "%s.%s", a
, b
);
1063 static char *getCommandForWindow(Window win
, int elements
)
1065 char **argv
, *command
= NULL
;
1068 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
1069 if (argc
> 0 && argv
!= NULL
) {
1072 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
1073 if (command
[0] == 0) {
1079 XFreeStringList(argv
);
1086 /* Free result when done */
1087 char *GetCommandForWindow(Window win
)
1089 return getCommandForWindow(win
, 0);