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>
41 #include "WindowMaker.h"
50 #include "xmodifier.h"
52 /**** global variables *****/
53 extern WPreferences wPreferences
;
54 #define ICON_SIZE wPreferences.icon_size
56 /* XFetchName Wrapper */
57 Bool
wFetchName(Display
*dpy
, Window win
, char **winname
)
59 XTextProperty text_prop
;
63 if (XGetWMName(dpy
, win
, &text_prop
)) {
64 if (text_prop
.value
&& text_prop
.nitems
> 0) {
65 if (text_prop
.encoding
== XA_STRING
) {
66 *winname
= wstrdup((char *)text_prop
.value
);
67 XFree(text_prop
.value
);
69 text_prop
.nitems
= strlen((char *)text_prop
.value
);
70 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >=
71 Success
&& num
> 0 && *list
) {
72 XFree(text_prop
.value
);
73 *winname
= wstrdup(*list
);
74 XFreeStringList(list
);
76 *winname
= wstrdup((char *)text_prop
.value
);
77 XFree(text_prop
.value
);
81 /* the title is set, but it was set to none */
82 *winname
= wstrdup("");
86 /* the hint is probably not set */
93 /* XGetIconName Wrapper */
94 Bool
wGetIconName(Display
*dpy
, Window win
, char **iconname
)
96 XTextProperty text_prop
;
100 if (XGetWMIconName(dpy
, win
, &text_prop
) != 0 && text_prop
.value
&& text_prop
.nitems
> 0) {
101 if (text_prop
.encoding
== XA_STRING
)
102 *iconname
= (char *)text_prop
.value
;
104 text_prop
.nitems
= strlen((char *)text_prop
.value
);
105 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >= Success
&& num
> 0 && *list
) {
106 XFree(text_prop
.value
);
107 *iconname
= wstrdup(*list
);
108 XFreeStringList(list
);
110 *iconname
= (char *)text_prop
.value
;
118 static void eatExpose(void)
122 /* compress all expose events into a single one */
124 if (XCheckMaskEvent(dpy
, ExposureMask
, &event
)) {
125 /* ignore other exposure events for this window */
126 while (XCheckWindowEvent(dpy
, event
.xexpose
.window
, ExposureMask
, &foo
)) ;
127 /* eat exposes for other windows */
130 event
.xexpose
.count
= 0;
131 XPutBackEvent(dpy
, &event
);
135 void move_window(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
138 if (wPreferences
.no_animations
)
139 XMoveWindow(dpy
, win
, to_x
, to_y
);
141 SlideWindow(win
, from_x
, from_y
, to_x
, to_y
);
143 XMoveWindow(dpy
, win
, to_x
, to_y
);
147 void SlideWindow(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
149 Window
*wins
[1] = { &win
};
150 SlideWindows(wins
, 1, from_x
, from_y
, to_x
, to_y
);
153 /* wins is an array of Window, sorted from left to right, the first is
154 * going to be moved from (from_x,from_y) to (to_x,to_y) and the
155 * following windows are going to be offset by (ICON_SIZE*i,0) */
156 void SlideWindows(Window
*wins
[], int n
, int from_x
, int from_y
, int to_x
, int to_y
)
158 time_t time0
= time(NULL
);
159 float dx
, dy
, x
= from_x
, y
= from_y
, sx
, sy
, px
, py
;
160 int dx_is_bigger
= 0;
161 int slide_delay
, slide_steps
, slide_slowdown
;
164 /* animation parameters */
170 {ICON_SLIDE_DELAY_UF
, ICON_SLIDE_STEPS_UF
, ICON_SLIDE_SLOWDOWN_UF
},
171 {ICON_SLIDE_DELAY_F
, ICON_SLIDE_STEPS_F
, ICON_SLIDE_SLOWDOWN_F
},
172 {ICON_SLIDE_DELAY_M
, ICON_SLIDE_STEPS_M
, ICON_SLIDE_SLOWDOWN_M
},
173 {ICON_SLIDE_DELAY_S
, ICON_SLIDE_STEPS_S
, ICON_SLIDE_SLOWDOWN_S
},
174 {ICON_SLIDE_DELAY_US
, ICON_SLIDE_STEPS_US
, ICON_SLIDE_SLOWDOWN_US
}
177 slide_slowdown
= apars
[(int)wPreferences
.icon_slide_speed
].slowdown
;
178 slide_steps
= apars
[(int)wPreferences
.icon_slide_speed
].steps
;
179 slide_delay
= apars
[(int)wPreferences
.icon_slide_speed
].delay
;
181 dx
= (float)(to_x
- from_x
);
182 dy
= (float)(to_y
- from_y
);
183 sx
= (dx
== 0 ? 0 : fabs(dx
) / dx
);
184 sy
= (dy
== 0 ? 0 : fabs(dy
) / dy
);
186 if (fabs(dx
) > fabs(dy
)) {
191 px
= dx
/ slide_slowdown
;
192 if (px
< slide_steps
&& px
> 0)
194 else if (px
> -slide_steps
&& px
< 0)
196 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
198 py
= dy
/ slide_slowdown
;
199 if (py
< slide_steps
&& py
> 0)
201 else if (py
> -slide_steps
&& py
< 0)
203 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
206 while (x
!= to_x
|| y
!= to_y
) {
209 if ((px
< 0 && (int)x
< to_x
) || (px
> 0 && (int)x
> to_x
))
211 if ((py
< 0 && (int)y
< to_y
) || (py
> 0 && (int)y
> to_y
))
215 px
= px
* (1.0 - 1 / (float)slide_slowdown
);
216 if (px
< slide_steps
&& px
> 0)
218 else if (px
> -slide_steps
&& px
< 0)
220 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
222 py
= py
* (1.0 - 1 / (float)slide_slowdown
);
223 if (py
< slide_steps
&& py
> 0)
225 else if (py
> -slide_steps
&& py
< 0)
227 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
230 for (i
= 0; i
< n
; i
++) {
231 XMoveWindow(dpy
, *wins
[i
], (int)x
+ i
* ICON_SIZE
, (int)y
);
234 if (slide_delay
> 0) {
235 wusleep(slide_delay
* 1000L);
239 if (time(NULL
) - time0
> MAX_ANIMATION_TIME
)
242 for (i
= 0; i
< n
; i
++) {
243 XMoveWindow(dpy
, *wins
[i
], to_x
+ i
* ICON_SIZE
, to_y
);
247 /* compress expose events */
251 char *ShrinkString(WMFont
* font
, char *string
, int width
)
260 w
= WMWidthOfString(font
, string
, p
);
261 text
= wmalloc(strlen(string
) + 8);
262 strcpy(text
, string
);
266 pos
= strchr(text
, ' ');
268 pos
= strchr(text
, ':');
273 w1
= WMWidthOfString(font
, text
, p
);
290 width
-= WMWidthOfString(font
, "...", 3);
295 while (p2
> p1
&& p1
!= t
) {
296 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
299 t
= p1
+ (p2
- p1
) / 2;
300 } else if (w
< width
) {
302 t
= p1
+ (p2
- p1
) / 2;
306 strcat(text
, &string
[p
- p1
]);
311 char *FindImage(char *paths
, char *file
)
313 char *tmp
, *path
= NULL
;
315 tmp
= strrchr(file
, ':');
318 path
= wfindfile(paths
, file
);
322 path
= wfindfile(paths
, file
);
327 static void timeoutHandler(void *data
)
332 static char *getTextSelection(WScreen
* screen
, Atom selection
)
366 data
= XFetchBuffer(dpy
, &size
, buffer
);
373 unsigned long len
, bytes
;
377 static Atom clipboard
= 0;
380 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
382 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
384 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
386 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
388 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
391 WMDeleteTimerHandler(timer
);
393 wwarning("selection retrieval timed out");
397 /* nobody owns the selection or the current owner has
398 * nothing to do with what we need */
399 if (ev
.xselection
.property
== None
) {
403 if (XGetWindowProperty(dpy
, screen
->info_window
,
405 False
, XA_STRING
, &rtype
, &bits
, &len
,
406 &bytes
, (unsigned char **)&data
) != Success
) {
409 if (rtype
!= XA_STRING
|| bits
!= 8) {
410 wwarning("invalid data in text selection");
419 static char *getselection(WScreen
* scr
)
423 tmp
= getTextSelection(scr
, XA_PRIMARY
);
425 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
430 parseuserinputpart(char *line
, int *ptr
, char *endchars
)
432 int depth
= 0, begin
;
436 while(line
[*ptr
] != '\0') {
437 if(line
[*ptr
] == '(') {
439 } else if(depth
> 0 && line
[*ptr
] == ')') {
441 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
442 value
= wmalloc(*ptr
- begin
+ 1);
443 strncpy(value
, line
+ begin
, *ptr
- begin
);
444 value
[*ptr
- begin
] = '\0';
454 getuserinput(WScreen
*scr
, char *line
, int *ptr
, Bool advanced
)
456 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
459 if(line
[*ptr
] == '(')
460 title
= parseuserinputpart(line
, ptr
, ",)");
461 if(title
!= NULL
&& line
[*ptr
] == ',')
462 prompt
= parseuserinputpart(line
, ptr
, ",)");
463 if(prompt
!= NULL
&& line
[*ptr
] == ',')
464 name
= parseuserinputpart(line
, ptr
, ")");
467 rv
= wAdvancedInputDialog(scr
,
468 title
? _(title
):_("Program Arguments"),
469 prompt
? _(prompt
):_("Enter command arguments:"),
472 rv
= wInputDialog(scr
,
473 title
? _(title
):_("Program Arguments"),
474 prompt
? _(prompt
):_("Enter command arguments:"),
477 if(title
) wfree(title
);
478 if(prompt
) wfree(prompt
);
479 if(name
) wfree(name
);
481 return rv
? ret
: NULL
;
489 * state input new-state output
490 * NORMAL % OPTION <nil>
491 * NORMAL \ ESCAPE <nil>
492 * NORMAL etc. NORMAL <input>
493 * ESCAPE any NORMAL <input>
494 * OPTION s NORMAL <selection buffer>
495 * OPTION w NORMAL <selected window id>
496 * OPTION a NORMAL <input text>
497 * OPTION d NORMAL <OffiX DND selection object>
498 * OPTION W NORMAL <current workspace>
499 * OPTION etc. NORMAL %<input>
501 #define TMPBUFSIZE 64
502 char *ExpandOptions(WScreen
* scr
, char *cmdline
)
504 int ptr
, optr
, state
, len
, olen
;
506 char *selection
= NULL
;
507 char *user_input
= NULL
;
509 char *dropped_thing
= NULL
;
511 char tmpbuf
[TMPBUFSIZE
];
514 len
= strlen(cmdline
);
518 wwarning(_("out of memory during expansion of \"%s\""), cmdline
);
522 ptr
= 0; /* input line pointer */
523 optr
= 0; /* output line pointer */
528 switch (cmdline
[ptr
]) {
537 out
[optr
++] = cmdline
[ptr
];
542 switch (cmdline
[ptr
]) {
556 out
[optr
++] = cmdline
[ptr
];
562 switch (cmdline
[ptr
]) {
564 if (scr
->focused_window
&& scr
->focused_window
->flags
.focused
) {
565 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
566 (unsigned int)scr
->focused_window
->client_win
);
567 slen
= strlen(tmpbuf
);
569 nout
= realloc(out
, olen
);
571 wwarning(_("out of memory during expansion of \"%%w\""));
583 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x", (unsigned int)scr
->current_workspace
+ 1);
584 slen
= strlen(tmpbuf
);
586 nout
= realloc(out
, olen
);
588 wwarning(_("out of memory during expansion of \"%%W\""));
599 user_input
= getuserinput(scr
, cmdline
, &ptr
, cmdline
[ptr
-1] == 'A');
601 slen
= strlen(user_input
);
603 nout
= realloc(out
, olen
);
605 wwarning(_("out of memory during expansion of \"%%a\""));
609 strcat(out
, user_input
);
612 /* Not an error, but user has Canceled the dialog box.
613 * This will make the command to not be performed. */
620 if (scr
->xdestring
) {
621 dropped_thing
= wstrdup(scr
->xdestring
);
623 if (!dropped_thing
) {
624 dropped_thing
= get_dnd_selection(scr
);
626 if (!dropped_thing
) {
627 scr
->flags
.dnd_data_convertion_status
= 1;
630 slen
= strlen(dropped_thing
);
632 nout
= realloc(out
, olen
);
634 wwarning(_("out of memory during expansion of \"%%d\""));
638 strcat(out
, dropped_thing
);
645 selection
= getselection(scr
);
648 wwarning(_("selection not available"));
651 slen
= strlen(selection
);
653 nout
= realloc(out
, olen
);
655 wwarning(_("out of memory during expansion of \"%%s\""));
659 strcat(out
, selection
);
665 out
[optr
++] = cmdline
[ptr
];
683 void ParseWindowName(WMPropList
* value
, char **winstance
, char **wclass
, char *where
)
687 *winstance
= *wclass
= NULL
;
689 if (!WMIsPLString(value
)) {
690 wwarning(_("bad window name value in %s state info"), where
);
694 name
= WMGetFromPLString(value
);
695 if (!name
|| strlen(name
) == 0) {
696 wwarning(_("bad window name value in %s state info"), where
);
700 UnescapeWM_CLASS(name
, winstance
, wclass
);
704 static char *keysymToString(KeySym keysym
, unsigned int state
)
707 char *buf
= wmalloc(20);
712 kev
.send_event
= False
;
713 kev
.window
= DefaultRootWindow(dpy
);
714 kev
.root
= DefaultRootWindow(dpy
);
715 kev
.same_screen
= True
;
716 kev
.subwindow
= kev
.root
;
717 kev
.serial
= 0x12344321;
718 kev
.time
= CurrentTime
;
720 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
721 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
728 char *GetShortcutString(char *text
)
737 tmp
= text
= wstrdup(text
);
740 while ((k
= strchr(text
, '+')) != NULL
) {
744 mod
= wXModifierFromKey(text
);
746 return wstrdup("bug");
751 if (strcasecmp(text
, "Meta") == 0) {
752 buffer
= wstrappend(buffer
, "M+");
753 } else if (strcasecmp(text
, "Alt") == 0) {
754 buffer
= wstrappend(buffer
, "A+");
755 } else if (strcasecmp(text
, "Shift") == 0) {
756 buffer
= wstrappend(buffer
, "Sh+");
757 } else if (strcasecmp(text
, "Mod1") == 0) {
758 buffer
= wstrappend(buffer
, "M1+");
759 } else if (strcasecmp(text
, "Mod2") == 0) {
760 buffer
= wstrappend(buffer
, "M2+");
761 } else if (strcasecmp(text
, "Mod3") == 0) {
762 buffer
= wstrappend(buffer
, "M3+");
763 } else if (strcasecmp(text
, "Mod4") == 0) {
764 buffer
= wstrappend(buffer
, "M4+");
765 } else if (strcasecmp(text
, "Mod5") == 0) {
766 buffer
= wstrappend(buffer
, "M5+");
767 } else if (strcasecmp(text
, "Control") == 0) {
770 buffer
= wstrappend(buffer
, text
);
776 buffer
= wstrappend(buffer
, "^");
778 buffer
= wstrappend(buffer
, text
);
781 /* ksym = XStringToKeysym(text);
782 tmp = keysymToString(ksym, modmask);
784 buffer = wstrappend(buffer, tmp);
791 char *GetShortcutKey(WShortKey key
)
794 char *k
= XKeysymToString(XkbKeycodeToKeysym(dpy
, key
.keycode
, 0, 0));
797 char **m
= wPreferences
.modifier_labels
;
798 if (key
.modifier
& ControlMask
) tmp
= wstrappend(tmp
, m
[1] ? m
[1] : "Ctrl+");
799 if (key
.modifier
& ShiftMask
) tmp
= wstrappend(tmp
, m
[0] ? m
[0] : "Shift+");
800 if (key
.modifier
& Mod1Mask
) tmp
= wstrappend(tmp
, m
[2] ? m
[2] : "Mod1+");
801 if (key
.modifier
& Mod2Mask
) tmp
= wstrappend(tmp
, m
[3] ? m
[3] : "Mod2+");
802 if (key
.modifier
& Mod3Mask
) tmp
= wstrappend(tmp
, m
[4] ? m
[4] : "Mod3+");
803 if (key
.modifier
& Mod4Mask
) tmp
= wstrappend(tmp
, m
[5] ? m
[5] : "Mod4+");
804 if (key
.modifier
& Mod5Mask
) tmp
= wstrappend(tmp
, m
[6] ? m
[6] : "Mod5+");
805 tmp
= wstrappend(tmp
, k
);
807 return GetShortcutString(tmp
);
810 char *EscapeWM_CLASS(char *name
, char *class)
813 char *ename
= NULL
, *eclass
= NULL
;
821 ename
= wmalloc(l
* 2 + 1);
823 for (i
= 0; i
< l
; i
++) {
824 if (name
[i
] == '\\') {
826 } else if (name
[i
] == '.') {
829 ename
[j
++] = name
[i
];
835 eclass
= wmalloc(l
* 2 + 1);
837 for (i
= 0; i
< l
; i
++) {
838 if (class[i
] == '\\') {
840 } else if (class[i
] == '.') {
843 eclass
[j
++] = class[i
];
848 if (ename
&& eclass
) {
849 int len
= strlen(ename
) + strlen(eclass
) + 4;
851 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
855 ret
= wstrdup(ename
);
858 ret
= wstrdup(eclass
);
865 void UnescapeWM_CLASS(char *str
, char **name
, char **class)
875 /* separate string in 2 parts */
877 for (i
= 0; i
< j
; i
++) {
878 if (str
[i
] == '\\') {
881 } else if (str
[i
] == '.') {
887 /* unescape strings */
888 for (i
= 0, k
= 0; i
< dot
; i
++) {
889 if (str
[i
] == '\\') {
892 (*name
)[k
++] = str
[i
];
897 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
898 if (str
[i
] == '\\') {
901 (*class)[k
++] = str
[i
];
916 void SendHelperMessage(WScreen
* scr
, char type
, int workspace
, char *msg
)
923 if (!scr
->flags
.backimage_helper_launched
) {
927 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
928 buffer
= wmalloc(len
+ 5);
929 snprintf(buf
, sizeof(buf
), "%4i", len
);
930 memcpy(buffer
, buf
, 4);
933 if (workspace
>= 0) {
934 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
935 memcpy(&buffer
[i
], buf
, 4);
940 strcpy(&buffer
[i
], msg
);
942 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
943 werror(_("could not send message to background image helper"));
948 Bool
UpdateDomainFile(WDDomain
* domain
)
952 WMPropList
*shared_dict
, *dict
;
953 Bool result
, freeDict
= False
;
955 dict
= domain
->dictionary
;
956 if (WMIsPLDictionary(domain
->dictionary
)) {
957 /* retrieve global system dictionary */
958 snprintf(path
, sizeof(path
), "%s/WindowMaker/%s", SYSCONFDIR
, domain
->domain_name
);
959 if (stat(path
, &stbuf
) >= 0) {
960 shared_dict
= WMReadPropListFromFile(path
);
962 if (WMIsPLDictionary(shared_dict
)) {
964 dict
= WMDeepCopyPropList(domain
->dictionary
);
965 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
967 WMReleasePropList(shared_dict
);
972 result
= WMWritePropListToFile(dict
, domain
->path
);
975 WMReleasePropList(dict
);
981 char *StrConcatDot(char *a
, char *b
)
991 len
= strlen(a
) + strlen(b
) + 4;
994 snprintf(str
, len
, "%s.%s", a
, b
);
999 static char *getCommandForWindow(Window win
, int elements
)
1001 char **argv
, *command
= NULL
;
1004 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
1005 if (argc
> 0 && argv
!= NULL
) {
1008 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
1009 if (command
[0] == 0) {
1015 XFreeStringList(argv
);
1022 /* Free result when done */
1023 char *GetCommandForWindow(Window win
)
1025 return getCommandForWindow(win
, 0);