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
);
297 width
-= WMWidthOfString(font
, "...", 3);
302 while (p2
> p1
&& p1
!= t
) {
303 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
306 t
= p1
+ (p2
- p1
) / 2;
307 } else if (w
< width
) {
309 t
= p1
+ (p2
- p1
) / 2;
313 strcat(text
, &string
[p
- p1
]);
318 char *FindImage(const char *paths
, const char *file
)
320 char *tmp
, *path
= NULL
;
322 tmp
= strrchr(file
, ':');
325 path
= wfindfile(paths
, file
);
329 path
= wfindfile(paths
, file
);
334 static void timeoutHandler(void *data
)
339 static char *getTextSelection(WScreen
* screen
, Atom selection
)
373 data
= XFetchBuffer(dpy
, &size
, buffer
);
380 unsigned long len
, bytes
;
384 static Atom clipboard
= 0;
387 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
389 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
391 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
393 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
395 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
398 WMDeleteTimerHandler(timer
);
400 wwarning("selection retrieval timed out");
404 /* nobody owns the selection or the current owner has
405 * nothing to do with what we need */
406 if (ev
.xselection
.property
== None
) {
410 if (XGetWindowProperty(dpy
, screen
->info_window
,
412 False
, XA_STRING
, &rtype
, &bits
, &len
,
413 &bytes
, (unsigned char **)&data
) != Success
) {
416 if (rtype
!= XA_STRING
|| bits
!= 8) {
417 wwarning("invalid data in text selection");
426 static char *getselection(WScreen
* scr
)
430 tmp
= getTextSelection(scr
, XA_PRIMARY
);
432 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
437 parseuserinputpart(const char *line
, int *ptr
, const char *endchars
)
439 int depth
= 0, begin
;
443 while(line
[*ptr
] != '\0') {
444 if(line
[*ptr
] == '(') {
446 } else if(depth
> 0 && line
[*ptr
] == ')') {
448 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
449 value
= wmalloc(*ptr
- begin
+ 1);
450 strncpy(value
, line
+ begin
, *ptr
- begin
);
451 value
[*ptr
- begin
] = '\0';
461 getuserinput(WScreen
*scr
, const char *line
, int *ptr
, Bool advanced
)
463 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
466 if(line
[*ptr
] == '(')
467 title
= parseuserinputpart(line
, ptr
, ",)");
468 if(title
!= NULL
&& line
[*ptr
] == ',')
469 prompt
= parseuserinputpart(line
, ptr
, ",)");
470 if(prompt
!= NULL
&& line
[*ptr
] == ',')
471 name
= parseuserinputpart(line
, ptr
, ")");
474 rv
= wAdvancedInputDialog(scr
,
475 title
? _(title
):_("Program Arguments"),
476 prompt
? _(prompt
):_("Enter command arguments:"),
479 rv
= wInputDialog(scr
,
480 title
? _(title
):_("Program Arguments"),
481 prompt
? _(prompt
):_("Enter command arguments:"),
484 if(title
) wfree(title
);
485 if(prompt
) wfree(prompt
);
486 if(name
) wfree(name
);
488 return rv
? ret
: NULL
;
496 * state input new-state output
497 * NORMAL % OPTION <nil>
498 * NORMAL \ ESCAPE <nil>
499 * NORMAL etc. NORMAL <input>
500 * ESCAPE any NORMAL <input>
501 * OPTION s NORMAL <selection buffer>
502 * OPTION w NORMAL <selected window id>
503 * OPTION a NORMAL <input text>
504 * OPTION d NORMAL <OffiX DND selection object>
505 * OPTION W NORMAL <current workspace>
506 * OPTION etc. NORMAL %<input>
508 #define TMPBUFSIZE 64
509 char *ExpandOptions(WScreen
*scr
, const char *cmdline
)
511 int ptr
, optr
, state
, len
, olen
;
513 char *selection
= NULL
;
514 char *user_input
= NULL
;
516 char *dropped_thing
= 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 \"%%w\""));
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 \"%%W\""));
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 \"%%a\""));
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 dropped_thing
= wstrdup(scr
->xdestring
);
630 if (!dropped_thing
) {
631 dropped_thing
= get_dnd_selection(scr
);
633 if (!dropped_thing
) {
634 scr
->flags
.dnd_data_convertion_status
= 1;
637 slen
= strlen(dropped_thing
);
639 nout
= realloc(out
, olen
);
641 wwarning(_("out of memory during expansion of \"%%d\""));
645 strcat(out
, dropped_thing
);
652 selection
= getselection(scr
);
655 wwarning(_("selection not available"));
658 slen
= strlen(selection
);
660 nout
= realloc(out
, olen
);
662 wwarning(_("out of memory during expansion of \"%%s\""));
666 strcat(out
, selection
);
672 out
[optr
++] = cmdline
[ptr
];
690 void ParseWindowName(WMPropList
*value
, char **winstance
, char **wclass
, const char *where
)
694 *winstance
= *wclass
= NULL
;
696 if (!WMIsPLString(value
)) {
697 wwarning(_("bad window name value in %s state info"), where
);
701 name
= WMGetFromPLString(value
);
702 if (!name
|| strlen(name
) == 0) {
703 wwarning(_("bad window name value in %s state info"), where
);
707 UnescapeWM_CLASS(name
, winstance
, wclass
);
711 static char *keysymToString(KeySym keysym
, unsigned int state
)
714 char *buf
= wmalloc(20);
719 kev
.send_event
= False
;
720 kev
.window
= DefaultRootWindow(dpy
);
721 kev
.root
= DefaultRootWindow(dpy
);
722 kev
.same_screen
= True
;
723 kev
.subwindow
= kev
.root
;
724 kev
.serial
= 0x12344321;
725 kev
.time
= CurrentTime
;
727 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
728 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
735 char *GetShortcutString(const char *shortcut
)
744 tmp
= text
= wstrdup(shortcut
);
747 while ((k
= strchr(text
, '+')) != NULL
) {
751 mod
= wXModifierFromKey(text
);
753 return wstrdup("bug");
758 if (strcasecmp(text
, "Meta") == 0) {
759 buffer
= wstrappend(buffer
, "M+");
760 } else if (strcasecmp(text
, "Alt") == 0) {
761 buffer
= wstrappend(buffer
, "A+");
762 } else if (strcasecmp(text
, "Shift") == 0) {
763 buffer
= wstrappend(buffer
, "Sh+");
764 } else if (strcasecmp(text
, "Mod1") == 0) {
765 buffer
= wstrappend(buffer
, "M1+");
766 } else if (strcasecmp(text
, "Mod2") == 0) {
767 buffer
= wstrappend(buffer
, "M2+");
768 } else if (strcasecmp(text
, "Mod3") == 0) {
769 buffer
= wstrappend(buffer
, "M3+");
770 } else if (strcasecmp(text
, "Mod4") == 0) {
771 buffer
= wstrappend(buffer
, "M4+");
772 } else if (strcasecmp(text
, "Mod5") == 0) {
773 buffer
= wstrappend(buffer
, "M5+");
774 } else if (strcasecmp(text
, "Control") == 0) {
777 buffer
= wstrappend(buffer
, text
);
783 buffer
= wstrappend(buffer
, "^");
785 buffer
= wstrappend(buffer
, text
);
788 /* ksym = XStringToKeysym(text);
789 tmp = keysymToString(ksym, modmask);
791 buffer = wstrappend(buffer, tmp);
798 char *GetShortcutKey(WShortKey key
)
801 char *k
= XKeysymToString(XkbKeycodeToKeysym(dpy
, key
.keycode
, 0, 0));
804 char **m
= wPreferences
.modifier_labels
;
805 if (key
.modifier
& ControlMask
) tmp
= wstrappend(tmp
, m
[1] ? m
[1] : "Ctrl+");
806 if (key
.modifier
& ShiftMask
) tmp
= wstrappend(tmp
, m
[0] ? m
[0] : "Shift+");
807 if (key
.modifier
& Mod1Mask
) tmp
= wstrappend(tmp
, m
[2] ? m
[2] : "Mod1+");
808 if (key
.modifier
& Mod2Mask
) tmp
= wstrappend(tmp
, m
[3] ? m
[3] : "Mod2+");
809 if (key
.modifier
& Mod3Mask
) tmp
= wstrappend(tmp
, m
[4] ? m
[4] : "Mod3+");
810 if (key
.modifier
& Mod4Mask
) tmp
= wstrappend(tmp
, m
[5] ? m
[5] : "Mod4+");
811 if (key
.modifier
& Mod5Mask
) tmp
= wstrappend(tmp
, m
[6] ? m
[6] : "Mod5+");
812 tmp
= wstrappend(tmp
, k
);
814 return GetShortcutString(tmp
);
817 char *EscapeWM_CLASS(const char *name
, const char *class)
820 char *ename
= NULL
, *eclass
= NULL
;
828 ename
= wmalloc(l
* 2 + 1);
830 for (i
= 0; i
< l
; i
++) {
831 if (name
[i
] == '\\') {
833 } else if (name
[i
] == '.') {
836 ename
[j
++] = name
[i
];
842 eclass
= wmalloc(l
* 2 + 1);
844 for (i
= 0; i
< l
; i
++) {
845 if (class[i
] == '\\') {
847 } else if (class[i
] == '.') {
850 eclass
[j
++] = class[i
];
855 if (ename
&& eclass
) {
856 int len
= strlen(ename
) + strlen(eclass
) + 4;
858 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
862 ret
= wstrdup(ename
);
865 ret
= wstrdup(eclass
);
872 static void UnescapeWM_CLASS(const char *str
, char **name
, char **class)
882 /* separate string in 2 parts */
884 for (i
= 0; i
< j
; i
++) {
885 if (str
[i
] == '\\') {
888 } else if (str
[i
] == '.') {
894 /* unescape strings */
895 for (i
= 0, k
= 0; i
< dot
; i
++) {
896 if (str
[i
] == '\\') {
899 (*name
)[k
++] = str
[i
];
904 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
905 if (str
[i
] == '\\') {
908 (*class)[k
++] = str
[i
];
923 void SendHelperMessage(WScreen
*scr
, char type
, int workspace
, const char *msg
)
930 if (!scr
->flags
.backimage_helper_launched
) {
934 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
935 buffer
= wmalloc(len
+ 5);
936 snprintf(buf
, sizeof(buf
), "%4i", len
);
937 memcpy(buffer
, buf
, 4);
940 if (workspace
>= 0) {
941 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
942 memcpy(&buffer
[i
], buf
, 4);
947 strcpy(&buffer
[i
], msg
);
949 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
950 werror(_("could not send message to background image helper"));
955 Bool
UpdateDomainFile(WDDomain
* domain
)
959 WMPropList
*shared_dict
, *dict
;
960 Bool result
, freeDict
= False
;
962 dict
= domain
->dictionary
;
963 if (WMIsPLDictionary(domain
->dictionary
)) {
964 /* retrieve global system dictionary */
965 snprintf(path
, sizeof(path
), "%s/WindowMaker/%s", SYSCONFDIR
, domain
->domain_name
);
966 if (stat(path
, &stbuf
) >= 0) {
967 shared_dict
= WMReadPropListFromFile(path
);
969 if (WMIsPLDictionary(shared_dict
)) {
971 dict
= WMDeepCopyPropList(domain
->dictionary
);
972 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
974 WMReleasePropList(shared_dict
);
979 result
= WMWritePropListToFile(dict
, domain
->path
);
982 WMReleasePropList(dict
);
988 char *StrConcatDot(const char *a
, const char *b
)
998 len
= strlen(a
) + strlen(b
) + 4;
1001 snprintf(str
, len
, "%s.%s", a
, b
);
1006 static char *getCommandForWindow(Window win
, int elements
)
1008 char **argv
, *command
= NULL
;
1011 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
1012 if (argc
> 0 && argv
!= NULL
) {
1015 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
1016 if (command
[0] == 0) {
1022 XFreeStringList(argv
);
1029 /* Free result when done */
1030 char *GetCommandForWindow(Window win
)
1032 return getCommandForWindow(win
, 0);