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 <WINGs/WUtil.h>
39 #include "WindowMaker.h"
48 #include "xmodifier.h"
50 /**** global variables *****/
51 extern WPreferences wPreferences
;
53 /* XFetchName Wrapper */
54 Bool
wFetchName(Display
*dpy
, Window win
, char **winname
)
56 XTextProperty text_prop
;
60 if (XGetWMName(dpy
, win
, &text_prop
)) {
61 if (text_prop
.value
&& text_prop
.nitems
> 0) {
62 if (text_prop
.encoding
== XA_STRING
) {
63 *winname
= wstrdup((char *)text_prop
.value
);
64 XFree(text_prop
.value
);
66 text_prop
.nitems
= strlen((char *)text_prop
.value
);
67 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >=
68 Success
&& num
> 0 && *list
) {
69 XFree(text_prop
.value
);
70 *winname
= wstrdup(*list
);
71 XFreeStringList(list
);
73 *winname
= wstrdup((char *)text_prop
.value
);
74 XFree(text_prop
.value
);
78 /* the title is set, but it was set to none */
79 *winname
= wstrdup("");
83 /* the hint is probably not set */
90 /* XGetIconName Wrapper */
91 Bool
wGetIconName(Display
*dpy
, Window win
, char **iconname
)
93 XTextProperty text_prop
;
97 if (XGetWMIconName(dpy
, win
, &text_prop
) != 0 && text_prop
.value
&& text_prop
.nitems
> 0) {
98 if (text_prop
.encoding
== XA_STRING
)
99 *iconname
= (char *)text_prop
.value
;
101 text_prop
.nitems
= strlen((char *)text_prop
.value
);
102 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >= Success
&& num
> 0 && *list
) {
103 XFree(text_prop
.value
);
104 *iconname
= wstrdup(*list
);
105 XFreeStringList(list
);
107 *iconname
= (char *)text_prop
.value
;
115 static void eatExpose(void)
119 /* compress all expose events into a single one */
121 if (XCheckMaskEvent(dpy
, ExposureMask
, &event
)) {
122 /* ignore other exposure events for this window */
123 while (XCheckWindowEvent(dpy
, event
.xexpose
.window
, ExposureMask
, &foo
)) ;
124 /* eat exposes for other windows */
127 event
.xexpose
.count
= 0;
128 XPutBackEvent(dpy
, &event
);
132 void move_window(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
135 if (wPreferences
.no_animations
)
136 XMoveWindow(dpy
, win
, to_x
, to_y
);
138 SlideWindow(win
, from_x
, from_y
, to_x
, to_y
);
140 XMoveWindow(dpy
, win
, to_x
, to_y
);
144 void SlideWindow(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
146 time_t time0
= time(NULL
);
147 float dx
, dy
, x
= from_x
, y
= from_y
, sx
, sy
, px
, py
;
148 int dx_is_bigger
= 0;
149 int slide_delay
, slide_steps
, slide_slowdown
;
151 /* animation parameters */
157 {ICON_SLIDE_DELAY_UF
, ICON_SLIDE_STEPS_UF
, ICON_SLIDE_SLOWDOWN_UF
},
158 {ICON_SLIDE_DELAY_F
, ICON_SLIDE_STEPS_F
, ICON_SLIDE_SLOWDOWN_F
},
159 {ICON_SLIDE_DELAY_M
, ICON_SLIDE_STEPS_M
, ICON_SLIDE_SLOWDOWN_M
},
160 {ICON_SLIDE_DELAY_S
, ICON_SLIDE_STEPS_S
, ICON_SLIDE_SLOWDOWN_S
},
161 {ICON_SLIDE_DELAY_US
, ICON_SLIDE_STEPS_US
, ICON_SLIDE_SLOWDOWN_US
}
164 slide_slowdown
= apars
[(int)wPreferences
.icon_slide_speed
].slowdown
;
165 slide_steps
= apars
[(int)wPreferences
.icon_slide_speed
].steps
;
166 slide_delay
= apars
[(int)wPreferences
.icon_slide_speed
].delay
;
168 dx
= (float)(to_x
- from_x
);
169 dy
= (float)(to_y
- from_y
);
170 sx
= (dx
== 0 ? 0 : fabs(dx
) / dx
);
171 sy
= (dy
== 0 ? 0 : fabs(dy
) / dy
);
173 if (fabs(dx
) > fabs(dy
)) {
178 px
= dx
/ slide_slowdown
;
179 if (px
< slide_steps
&& px
> 0)
181 else if (px
> -slide_steps
&& px
< 0)
183 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
185 py
= dy
/ slide_slowdown
;
186 if (py
< slide_steps
&& py
> 0)
188 else if (py
> -slide_steps
&& py
< 0)
190 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
193 while (x
!= to_x
|| y
!= to_y
) {
196 if ((px
< 0 && (int)x
< to_x
) || (px
> 0 && (int)x
> to_x
))
198 if ((py
< 0 && (int)y
< to_y
) || (py
> 0 && (int)y
> to_y
))
202 px
= px
* (1.0 - 1 / (float)slide_slowdown
);
203 if (px
< slide_steps
&& px
> 0)
205 else if (px
> -slide_steps
&& px
< 0)
207 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
209 py
= py
* (1.0 - 1 / (float)slide_slowdown
);
210 if (py
< slide_steps
&& py
> 0)
212 else if (py
> -slide_steps
&& py
< 0)
214 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
217 XMoveWindow(dpy
, win
, (int)x
, (int)y
);
219 if (slide_delay
> 0) {
220 wusleep(slide_delay
* 1000L);
224 if (time(NULL
) - time0
> MAX_ANIMATION_TIME
)
227 XMoveWindow(dpy
, win
, to_x
, to_y
);
230 /* compress expose events */
234 char *ShrinkString(WMFont
* font
, char *string
, int width
)
243 w
= WMWidthOfString(font
, string
, p
);
244 text
= wmalloc(strlen(string
) + 8);
245 strcpy(text
, string
);
249 pos
= strchr(text
, ' ');
251 pos
= strchr(text
, ':');
256 w1
= WMWidthOfString(font
, text
, p
);
273 width
-= WMWidthOfString(font
, "...", 3);
278 while (p2
> p1
&& p1
!= t
) {
279 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
282 t
= p1
+ (p2
- p1
) / 2;
283 } else if (w
< width
) {
285 t
= p1
+ (p2
- p1
) / 2;
289 strcat(text
, &string
[p
- p1
]);
294 char *FindImage(char *paths
, char *file
)
296 char *tmp
, *path
= NULL
;
298 tmp
= strrchr(file
, ':');
301 path
= wfindfile(paths
, file
);
305 path
= wfindfile(paths
, file
);
310 static void timeoutHandler(void *data
)
315 static char *getTextSelection(WScreen
* screen
, Atom selection
)
349 data
= XFetchBuffer(dpy
, &size
, buffer
);
356 unsigned long len
, bytes
;
360 static Atom clipboard
= 0;
363 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
365 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
367 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
369 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
371 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
374 WMDeleteTimerHandler(timer
);
376 wwarning("selection retrieval timed out");
380 /* nobody owns the selection or the current owner has
381 * nothing to do with what we need */
382 if (ev
.xselection
.property
== None
) {
386 if (XGetWindowProperty(dpy
, screen
->info_window
,
388 False
, XA_STRING
, &rtype
, &bits
, &len
,
389 &bytes
, (unsigned char **)&data
) != Success
) {
392 if (rtype
!= XA_STRING
|| bits
!= 8) {
393 wwarning("invalid data in text selection");
402 static char *getselection(WScreen
* scr
)
406 tmp
= getTextSelection(scr
, XA_PRIMARY
);
408 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
413 parseuserinputpart(char *line
, int *ptr
, char *endchars
)
415 int depth
= 0, begin
;
419 while(line
[*ptr
] != '\0') {
420 if(line
[*ptr
] == '(') {
422 } else if(depth
> 0 && line
[*ptr
] == ')') {
424 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
425 value
= wmalloc(*ptr
- begin
+ 1);
426 strncpy(value
, line
+ begin
, *ptr
- begin
);
427 value
[*ptr
- begin
] = '\0';
437 getuserinput(WScreen
*scr
, char *line
, int *ptr
, Bool advanced
)
439 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
442 if(line
[*ptr
] == '(')
443 title
= parseuserinputpart(line
, ptr
, ",)");
444 if(title
!= NULL
&& line
[*ptr
] == ',')
445 prompt
= parseuserinputpart(line
, ptr
, ",)");
446 if(prompt
!= NULL
&& line
[*ptr
] == ',')
447 name
= parseuserinputpart(line
, ptr
, ")");
450 rv
= wAdvancedInputDialog(scr
,
451 title
? _(title
):_("Program Arguments"),
452 prompt
? _(prompt
):_("Enter command arguments:"),
455 rv
= wInputDialog(scr
,
456 title
? _(title
):_("Program Arguments"),
457 prompt
? _(prompt
):_("Enter command arguments:"),
460 if(title
) wfree(title
);
461 if(prompt
) wfree(prompt
);
462 if(name
) wfree(name
);
464 return rv
? ret
: NULL
;
472 * state input new-state output
473 * NORMAL % OPTION <nil>
474 * NORMAL \ ESCAPE <nil>
475 * NORMAL etc. NORMAL <input>
476 * ESCAPE any NORMAL <input>
477 * OPTION s NORMAL <selection buffer>
478 * OPTION w NORMAL <selected window id>
479 * OPTION a NORMAL <input text>
480 * OPTION d NORMAL <OffiX DND selection object>
481 * OPTION W NORMAL <current workspace>
482 * OPTION etc. NORMAL %<input>
484 #define TMPBUFSIZE 64
485 char *ExpandOptions(WScreen
* scr
, char *cmdline
)
487 int ptr
, optr
, state
, len
, olen
;
489 char *selection
= NULL
;
490 char *user_input
= NULL
;
492 char *dropped_thing
= NULL
;
494 char tmpbuf
[TMPBUFSIZE
];
497 len
= strlen(cmdline
);
501 wwarning(_("out of memory during expansion of \"%s\""), cmdline
);
505 ptr
= 0; /* input line pointer */
506 optr
= 0; /* output line pointer */
511 switch (cmdline
[ptr
]) {
520 out
[optr
++] = cmdline
[ptr
];
525 switch (cmdline
[ptr
]) {
539 out
[optr
++] = cmdline
[ptr
];
545 switch (cmdline
[ptr
]) {
547 if (scr
->focused_window
&& scr
->focused_window
->flags
.focused
) {
548 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
549 (unsigned int)scr
->focused_window
->client_win
);
550 slen
= strlen(tmpbuf
);
552 nout
= realloc(out
, olen
);
554 wwarning(_("out of memory during expansion of \"%%w\""));
566 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x", (unsigned int)scr
->current_workspace
+ 1);
567 slen
= strlen(tmpbuf
);
569 nout
= realloc(out
, olen
);
571 wwarning(_("out of memory during expansion of \"%%W\""));
582 user_input
= getuserinput(scr
, cmdline
, &ptr
, cmdline
[ptr
-1] == 'A');
584 slen
= strlen(user_input
);
586 nout
= realloc(out
, olen
);
588 wwarning(_("out of memory during expansion of \"%%a\""));
592 strcat(out
, user_input
);
595 /* Not an error, but user has Canceled the dialog box.
596 * This will make the command to not be performed. */
603 if (scr
->xdestring
) {
604 dropped_thing
= wstrdup(scr
->xdestring
);
606 if (!dropped_thing
) {
607 dropped_thing
= get_dnd_selection(scr
);
609 if (!dropped_thing
) {
610 scr
->flags
.dnd_data_convertion_status
= 1;
613 slen
= strlen(dropped_thing
);
615 nout
= realloc(out
, olen
);
617 wwarning(_("out of memory during expansion of \"%%d\""));
621 strcat(out
, dropped_thing
);
628 selection
= getselection(scr
);
631 wwarning(_("selection not available"));
634 slen
= strlen(selection
);
636 nout
= realloc(out
, olen
);
638 wwarning(_("out of memory during expansion of \"%%s\""));
642 strcat(out
, selection
);
648 out
[optr
++] = cmdline
[ptr
];
666 void ParseWindowName(WMPropList
* value
, char **winstance
, char **wclass
, char *where
)
670 *winstance
= *wclass
= NULL
;
672 if (!WMIsPLString(value
)) {
673 wwarning(_("bad window name value in %s state info"), where
);
677 name
= WMGetFromPLString(value
);
678 if (!name
|| strlen(name
) == 0) {
679 wwarning(_("bad window name value in %s state info"), where
);
683 UnescapeWM_CLASS(name
, winstance
, wclass
);
687 static char *keysymToString(KeySym keysym
, unsigned int state
)
690 char *buf
= wmalloc(20);
695 kev
.send_event
= False
;
696 kev
.window
= DefaultRootWindow(dpy
);
697 kev
.root
= DefaultRootWindow(dpy
);
698 kev
.same_screen
= True
;
699 kev
.subwindow
= kev
.root
;
700 kev
.serial
= 0x12344321;
701 kev
.time
= CurrentTime
;
703 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
704 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
711 char *GetShortcutString(char *text
)
720 tmp
= text
= wstrdup(text
);
723 while ((k
= strchr(text
, '+')) != NULL
) {
727 mod
= wXModifierFromKey(text
);
729 return wstrdup("bug");
734 if (strcasecmp(text
, "Meta") == 0) {
735 buffer
= wstrappend(buffer
, "M+");
736 } else if (strcasecmp(text
, "Alt") == 0) {
737 buffer
= wstrappend(buffer
, "A+");
738 } else if (strcasecmp(text
, "Shift") == 0) {
739 buffer
= wstrappend(buffer
, "Sh+");
740 } else if (strcasecmp(text
, "Mod1") == 0) {
741 buffer
= wstrappend(buffer
, "M1+");
742 } else if (strcasecmp(text
, "Mod2") == 0) {
743 buffer
= wstrappend(buffer
, "M2+");
744 } else if (strcasecmp(text
, "Mod3") == 0) {
745 buffer
= wstrappend(buffer
, "M3+");
746 } else if (strcasecmp(text
, "Mod4") == 0) {
747 buffer
= wstrappend(buffer
, "M4+");
748 } else if (strcasecmp(text
, "Mod5") == 0) {
749 buffer
= wstrappend(buffer
, "M5+");
750 } else if (strcasecmp(text
, "Control") == 0) {
753 buffer
= wstrappend(buffer
, text
);
759 buffer
= wstrappend(buffer
, "^");
761 buffer
= wstrappend(buffer
, text
);
764 /* ksym = XStringToKeysym(text);
765 tmp = keysymToString(ksym, modmask);
767 buffer = wstrappend(buffer, tmp);
774 char *EscapeWM_CLASS(char *name
, char *class)
777 char *ename
= NULL
, *eclass
= NULL
;
785 ename
= wmalloc(l
* 2 + 1);
787 for (i
= 0; i
< l
; i
++) {
788 if (name
[i
] == '\\') {
790 } else if (name
[i
] == '.') {
793 ename
[j
++] = name
[i
];
799 eclass
= wmalloc(l
* 2 + 1);
801 for (i
= 0; i
< l
; i
++) {
802 if (class[i
] == '\\') {
804 } else if (class[i
] == '.') {
807 eclass
[j
++] = class[i
];
812 if (ename
&& eclass
) {
813 int len
= strlen(ename
) + strlen(eclass
) + 4;
815 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
819 ret
= wstrdup(ename
);
822 ret
= wstrdup(eclass
);
829 void UnescapeWM_CLASS(char *str
, char **name
, char **class)
839 /* separate string in 2 parts */
841 for (i
= 0; i
< j
; i
++) {
842 if (str
[i
] == '\\') {
845 } else if (str
[i
] == '.') {
851 /* unescape strings */
852 for (i
= 0, k
= 0; i
< dot
; i
++) {
853 if (str
[i
] == '\\') {
856 (*name
)[k
++] = str
[i
];
861 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
862 if (str
[i
] == '\\') {
865 (*class)[k
++] = str
[i
];
880 void SendHelperMessage(WScreen
* scr
, char type
, int workspace
, char *msg
)
887 if (!scr
->flags
.backimage_helper_launched
) {
891 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
892 buffer
= wmalloc(len
+ 5);
893 snprintf(buf
, sizeof(buf
), "%4i", len
);
894 memcpy(buffer
, buf
, 4);
897 if (workspace
>= 0) {
898 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
899 memcpy(&buffer
[i
], buf
, 4);
904 strcpy(&buffer
[i
], msg
);
906 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
907 werror(_("could not send message to background image helper"));
912 Bool
UpdateDomainFile(WDDomain
* domain
)
916 WMPropList
*shared_dict
, *dict
;
917 Bool result
, freeDict
= False
;
919 dict
= domain
->dictionary
;
920 if (WMIsPLDictionary(domain
->dictionary
)) {
921 /* retrieve global system dictionary */
922 snprintf(path
, sizeof(path
), "%s/WindowMaker/%s", SYSCONFDIR
, domain
->domain_name
);
923 if (stat(path
, &stbuf
) >= 0) {
924 shared_dict
= WMReadPropListFromFile(path
);
926 if (WMIsPLDictionary(shared_dict
)) {
928 dict
= WMDeepCopyPropList(domain
->dictionary
);
929 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
931 WMReleasePropList(shared_dict
);
936 result
= WMWritePropListToFile(dict
, domain
->path
);
939 WMReleasePropList(dict
);
945 char *StrConcatDot(char *a
, char *b
)
955 len
= strlen(a
) + strlen(b
) + 4;
958 snprintf(str
, len
, "%s.%s", a
, b
);
963 static char *getCommandForWindow(Window win
, int elements
)
965 char **argv
, *command
= NULL
;
968 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
969 if (argc
> 0 && argv
!= NULL
) {
972 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
973 if (command
[0] == 0) {
979 XFreeStringList(argv
);
986 /* Free result when done */
987 char *GetCommandForWindow(Window win
)
989 return getCommandForWindow(win
, 0);