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>
42 #include "WindowMaker.h"
50 #include "xmodifier.h"
52 /**** global variables *****/
53 extern WPreferences wPreferences
;
54 #define ICON_SIZE wPreferences.icon_size
56 /**** Local prototypes *****/
57 static void UnescapeWM_CLASS(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
, sx
, sy
, px
, py
;
163 int dx_is_bigger
= 0;
164 int slide_delay
, slide_steps
, slide_slowdown
;
167 /* animation parameters */
173 {ICON_SLIDE_DELAY_UF
, ICON_SLIDE_STEPS_UF
, ICON_SLIDE_SLOWDOWN_UF
},
174 {ICON_SLIDE_DELAY_F
, ICON_SLIDE_STEPS_F
, ICON_SLIDE_SLOWDOWN_F
},
175 {ICON_SLIDE_DELAY_M
, ICON_SLIDE_STEPS_M
, ICON_SLIDE_SLOWDOWN_M
},
176 {ICON_SLIDE_DELAY_S
, ICON_SLIDE_STEPS_S
, ICON_SLIDE_SLOWDOWN_S
},
177 {ICON_SLIDE_DELAY_US
, ICON_SLIDE_STEPS_US
, ICON_SLIDE_SLOWDOWN_US
}
180 slide_slowdown
= apars
[(int)wPreferences
.icon_slide_speed
].slowdown
;
181 slide_steps
= apars
[(int)wPreferences
.icon_slide_speed
].steps
;
182 slide_delay
= apars
[(int)wPreferences
.icon_slide_speed
].delay
;
184 dx
= (float)(to_x
- from_x
);
185 dy
= (float)(to_y
- from_y
);
186 sx
= (dx
== 0 ? 0 : fabs(dx
) / dx
);
187 sy
= (dy
== 0 ? 0 : fabs(dy
) / dy
);
189 if (fabs(dx
) > fabs(dy
)) {
194 px
= dx
/ slide_slowdown
;
195 if (px
< slide_steps
&& px
> 0)
197 else if (px
> -slide_steps
&& px
< 0)
199 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
201 py
= dy
/ slide_slowdown
;
202 if (py
< slide_steps
&& py
> 0)
204 else if (py
> -slide_steps
&& py
< 0)
206 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
209 while (x
!= to_x
|| y
!= to_y
) {
212 if ((px
< 0 && (int)x
< to_x
) || (px
> 0 && (int)x
> to_x
))
214 if ((py
< 0 && (int)y
< to_y
) || (py
> 0 && (int)y
> to_y
))
218 px
= px
* (1.0 - 1 / (float)slide_slowdown
);
219 if (px
< slide_steps
&& px
> 0)
221 else if (px
> -slide_steps
&& px
< 0)
223 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
225 py
= py
* (1.0 - 1 / (float)slide_slowdown
);
226 if (py
< slide_steps
&& py
> 0)
228 else if (py
> -slide_steps
&& py
< 0)
230 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
233 for (i
= 0; i
< n
; i
++) {
234 XMoveWindow(dpy
, *wins
[i
], (int)x
+ i
* ICON_SIZE
, (int)y
);
237 if (slide_delay
> 0) {
238 wusleep(slide_delay
* 1000L);
242 if (time(NULL
) - time0
> MAX_ANIMATION_TIME
)
245 for (i
= 0; i
< n
; i
++) {
246 XMoveWindow(dpy
, *wins
[i
], to_x
+ i
* ICON_SIZE
, to_y
);
250 /* compress expose events */
254 char *ShrinkString(WMFont
* font
, char *string
, int width
)
263 w
= WMWidthOfString(font
, string
, p
);
264 text
= wmalloc(strlen(string
) + 8);
265 strcpy(text
, string
);
269 pos
= strchr(text
, ' ');
271 pos
= strchr(text
, ':');
276 w1
= WMWidthOfString(font
, text
, p
);
293 width
-= WMWidthOfString(font
, "...", 3);
298 while (p2
> p1
&& p1
!= t
) {
299 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
302 t
= p1
+ (p2
- p1
) / 2;
303 } else if (w
< width
) {
305 t
= p1
+ (p2
- p1
) / 2;
309 strcat(text
, &string
[p
- p1
]);
314 char *FindImage(char *paths
, char *file
)
316 char *tmp
, *path
= NULL
;
318 tmp
= strrchr(file
, ':');
321 path
= wfindfile(paths
, file
);
325 path
= wfindfile(paths
, file
);
330 static void timeoutHandler(void *data
)
335 static char *getTextSelection(WScreen
* screen
, Atom selection
)
369 data
= XFetchBuffer(dpy
, &size
, buffer
);
376 unsigned long len
, bytes
;
380 static Atom clipboard
= 0;
383 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
385 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
387 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
389 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
391 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
394 WMDeleteTimerHandler(timer
);
396 wwarning("selection retrieval timed out");
400 /* nobody owns the selection or the current owner has
401 * nothing to do with what we need */
402 if (ev
.xselection
.property
== None
) {
406 if (XGetWindowProperty(dpy
, screen
->info_window
,
408 False
, XA_STRING
, &rtype
, &bits
, &len
,
409 &bytes
, (unsigned char **)&data
) != Success
) {
412 if (rtype
!= XA_STRING
|| bits
!= 8) {
413 wwarning("invalid data in text selection");
422 static char *getselection(WScreen
* scr
)
426 tmp
= getTextSelection(scr
, XA_PRIMARY
);
428 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
433 parseuserinputpart(char *line
, int *ptr
, char *endchars
)
435 int depth
= 0, begin
;
439 while(line
[*ptr
] != '\0') {
440 if(line
[*ptr
] == '(') {
442 } else if(depth
> 0 && line
[*ptr
] == ')') {
444 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
445 value
= wmalloc(*ptr
- begin
+ 1);
446 strncpy(value
, line
+ begin
, *ptr
- begin
);
447 value
[*ptr
- begin
] = '\0';
457 getuserinput(WScreen
*scr
, char *line
, int *ptr
, Bool advanced
)
459 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
462 if(line
[*ptr
] == '(')
463 title
= parseuserinputpart(line
, ptr
, ",)");
464 if(title
!= NULL
&& line
[*ptr
] == ',')
465 prompt
= parseuserinputpart(line
, ptr
, ",)");
466 if(prompt
!= NULL
&& line
[*ptr
] == ',')
467 name
= parseuserinputpart(line
, ptr
, ")");
470 rv
= wAdvancedInputDialog(scr
,
471 title
? _(title
):_("Program Arguments"),
472 prompt
? _(prompt
):_("Enter command arguments:"),
475 rv
= wInputDialog(scr
,
476 title
? _(title
):_("Program Arguments"),
477 prompt
? _(prompt
):_("Enter command arguments:"),
480 if(title
) wfree(title
);
481 if(prompt
) wfree(prompt
);
482 if(name
) wfree(name
);
484 return rv
? ret
: NULL
;
492 * state input new-state output
493 * NORMAL % OPTION <nil>
494 * NORMAL \ ESCAPE <nil>
495 * NORMAL etc. NORMAL <input>
496 * ESCAPE any NORMAL <input>
497 * OPTION s NORMAL <selection buffer>
498 * OPTION w NORMAL <selected window id>
499 * OPTION a NORMAL <input text>
500 * OPTION d NORMAL <OffiX DND selection object>
501 * OPTION W NORMAL <current workspace>
502 * OPTION etc. NORMAL %<input>
504 #define TMPBUFSIZE 64
505 char *ExpandOptions(WScreen
* scr
, char *cmdline
)
507 int ptr
, optr
, state
, len
, olen
;
509 char *selection
= NULL
;
510 char *user_input
= NULL
;
512 char *dropped_thing
= NULL
;
514 char tmpbuf
[TMPBUFSIZE
];
517 len
= strlen(cmdline
);
521 wwarning(_("out of memory during expansion of \"%s\""), cmdline
);
525 ptr
= 0; /* input line pointer */
526 optr
= 0; /* output line pointer */
531 switch (cmdline
[ptr
]) {
540 out
[optr
++] = cmdline
[ptr
];
545 switch (cmdline
[ptr
]) {
559 out
[optr
++] = cmdline
[ptr
];
565 switch (cmdline
[ptr
]) {
567 if (scr
->focused_window
&& scr
->focused_window
->flags
.focused
) {
568 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
569 (unsigned int)scr
->focused_window
->client_win
);
570 slen
= strlen(tmpbuf
);
572 nout
= realloc(out
, olen
);
574 wwarning(_("out of memory during expansion of \"%%w\""));
586 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x", (unsigned int)scr
->current_workspace
+ 1);
587 slen
= strlen(tmpbuf
);
589 nout
= realloc(out
, olen
);
591 wwarning(_("out of memory during expansion of \"%%W\""));
602 user_input
= getuserinput(scr
, cmdline
, &ptr
, cmdline
[ptr
-1] == 'A');
604 slen
= strlen(user_input
);
606 nout
= realloc(out
, olen
);
608 wwarning(_("out of memory during expansion of \"%%a\""));
612 strcat(out
, user_input
);
615 /* Not an error, but user has Canceled the dialog box.
616 * This will make the command to not be performed. */
623 if (scr
->xdestring
) {
624 dropped_thing
= wstrdup(scr
->xdestring
);
626 if (!dropped_thing
) {
627 dropped_thing
= get_dnd_selection(scr
);
629 if (!dropped_thing
) {
630 scr
->flags
.dnd_data_convertion_status
= 1;
633 slen
= strlen(dropped_thing
);
635 nout
= realloc(out
, olen
);
637 wwarning(_("out of memory during expansion of \"%%d\""));
641 strcat(out
, dropped_thing
);
648 selection
= getselection(scr
);
651 wwarning(_("selection not available"));
654 slen
= strlen(selection
);
656 nout
= realloc(out
, olen
);
658 wwarning(_("out of memory during expansion of \"%%s\""));
662 strcat(out
, selection
);
668 out
[optr
++] = cmdline
[ptr
];
686 void ParseWindowName(WMPropList
* value
, char **winstance
, char **wclass
, char *where
)
690 *winstance
= *wclass
= NULL
;
692 if (!WMIsPLString(value
)) {
693 wwarning(_("bad window name value in %s state info"), where
);
697 name
= WMGetFromPLString(value
);
698 if (!name
|| strlen(name
) == 0) {
699 wwarning(_("bad window name value in %s state info"), where
);
703 UnescapeWM_CLASS(name
, winstance
, wclass
);
707 static char *keysymToString(KeySym keysym
, unsigned int state
)
710 char *buf
= wmalloc(20);
715 kev
.send_event
= False
;
716 kev
.window
= DefaultRootWindow(dpy
);
717 kev
.root
= DefaultRootWindow(dpy
);
718 kev
.same_screen
= True
;
719 kev
.subwindow
= kev
.root
;
720 kev
.serial
= 0x12344321;
721 kev
.time
= CurrentTime
;
723 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
724 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
731 char *GetShortcutString(char *text
)
740 tmp
= text
= wstrdup(text
);
743 while ((k
= strchr(text
, '+')) != NULL
) {
747 mod
= wXModifierFromKey(text
);
749 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
);
784 /* ksym = XStringToKeysym(text);
785 tmp = keysymToString(ksym, modmask);
787 buffer = wstrappend(buffer, tmp);
794 char *GetShortcutKey(WShortKey key
)
797 char *k
= XKeysymToString(XkbKeycodeToKeysym(dpy
, key
.keycode
, 0, 0));
800 char **m
= wPreferences
.modifier_labels
;
801 if (key
.modifier
& ControlMask
) tmp
= wstrappend(tmp
, m
[1] ? m
[1] : "Ctrl+");
802 if (key
.modifier
& ShiftMask
) tmp
= wstrappend(tmp
, m
[0] ? m
[0] : "Shift+");
803 if (key
.modifier
& Mod1Mask
) tmp
= wstrappend(tmp
, m
[2] ? m
[2] : "Mod1+");
804 if (key
.modifier
& Mod2Mask
) tmp
= wstrappend(tmp
, m
[3] ? m
[3] : "Mod2+");
805 if (key
.modifier
& Mod3Mask
) tmp
= wstrappend(tmp
, m
[4] ? m
[4] : "Mod3+");
806 if (key
.modifier
& Mod4Mask
) tmp
= wstrappend(tmp
, m
[5] ? m
[5] : "Mod4+");
807 if (key
.modifier
& Mod5Mask
) tmp
= wstrappend(tmp
, m
[6] ? m
[6] : "Mod5+");
808 tmp
= wstrappend(tmp
, k
);
810 return GetShortcutString(tmp
);
813 char *EscapeWM_CLASS(char *name
, char *class)
816 char *ename
= NULL
, *eclass
= NULL
;
824 ename
= wmalloc(l
* 2 + 1);
826 for (i
= 0; i
< l
; i
++) {
827 if (name
[i
] == '\\') {
829 } else if (name
[i
] == '.') {
832 ename
[j
++] = name
[i
];
838 eclass
= wmalloc(l
* 2 + 1);
840 for (i
= 0; i
< l
; i
++) {
841 if (class[i
] == '\\') {
843 } else if (class[i
] == '.') {
846 eclass
[j
++] = class[i
];
851 if (ename
&& eclass
) {
852 int len
= strlen(ename
) + strlen(eclass
) + 4;
854 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
858 ret
= wstrdup(ename
);
861 ret
= wstrdup(eclass
);
868 static void UnescapeWM_CLASS(char *str
, char **name
, char **class)
878 /* separate string in 2 parts */
880 for (i
= 0; i
< j
; i
++) {
881 if (str
[i
] == '\\') {
884 } else if (str
[i
] == '.') {
890 /* unescape strings */
891 for (i
= 0, k
= 0; i
< dot
; i
++) {
892 if (str
[i
] == '\\') {
895 (*name
)[k
++] = str
[i
];
900 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
901 if (str
[i
] == '\\') {
904 (*class)[k
++] = str
[i
];
919 void SendHelperMessage(WScreen
* scr
, char type
, int workspace
, char *msg
)
926 if (!scr
->flags
.backimage_helper_launched
) {
930 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
931 buffer
= wmalloc(len
+ 5);
932 snprintf(buf
, sizeof(buf
), "%4i", len
);
933 memcpy(buffer
, buf
, 4);
936 if (workspace
>= 0) {
937 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
938 memcpy(&buffer
[i
], buf
, 4);
943 strcpy(&buffer
[i
], msg
);
945 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
946 werror(_("could not send message to background image helper"));
951 Bool
UpdateDomainFile(WDDomain
* domain
)
955 WMPropList
*shared_dict
, *dict
;
956 Bool result
, freeDict
= False
;
958 dict
= domain
->dictionary
;
959 if (WMIsPLDictionary(domain
->dictionary
)) {
960 /* retrieve global system dictionary */
961 snprintf(path
, sizeof(path
), "%s/WindowMaker/%s", SYSCONFDIR
, domain
->domain_name
);
962 if (stat(path
, &stbuf
) >= 0) {
963 shared_dict
= WMReadPropListFromFile(path
);
965 if (WMIsPLDictionary(shared_dict
)) {
967 dict
= WMDeepCopyPropList(domain
->dictionary
);
968 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
970 WMReleasePropList(shared_dict
);
975 result
= WMWritePropListToFile(dict
, domain
->path
);
978 WMReleasePropList(dict
);
984 char *StrConcatDot(char *a
, char *b
)
994 len
= strlen(a
) + strlen(b
) + 4;
997 snprintf(str
, len
, "%s.%s", a
, b
);
1002 static char *getCommandForWindow(Window win
, int elements
)
1004 char **argv
, *command
= NULL
;
1007 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
1008 if (argc
> 0 && argv
!= NULL
) {
1011 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
1012 if (command
[0] == 0) {
1018 XFreeStringList(argv
);
1025 /* Free result when done */
1026 char *GetCommandForWindow(Window win
)
1028 return getCommandForWindow(win
, 0);