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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
36 #include <WINGs/WUtil.h>
39 #include "WindowMaker.h"
48 #include "xmodifier.h"
50 /**** global variables *****/
52 extern WPreferences wPreferences
;
54 extern Time LastTimestamp
;
57 static void putdef(char *line
, char *name
, char *value
)
60 wwarning(_("could not define value for %s for cpp"), name
);
67 static void putidef(char *line
, char *name
, int value
)
70 snprintf(tmp
, sizeof(tmp
), "%i", value
);
75 static char *username()
83 user
= getpwuid(getuid());
85 wsyserror(_("could not get password entry for UID %i"), getuid());
97 char *MakeCPPArgs(char *path
)
100 char buffer
[MAXLINE
], *buf
, *line
;
104 line
= wmalloc(MAXLINE
);
107 if ((buf
= getenv("HOSTNAME")) != NULL
) {
109 wwarning(_("your machine is misconfigured. HOSTNAME is set to %s"), buf
);
111 putdef(line
, " -DHOST=", buf
);
112 } else if ((buf
= getenv("HOST")) != NULL
) {
114 wwarning(_("your machine is misconfigured. HOST is set to %s"), buf
);
116 putdef(line
, " -DHOST=", buf
);
120 putdef(line
, " -DUSER=", buf
);
121 putidef(line
, " -DUID=", getuid());
122 buf
= XDisplayName(DisplayString(dpy
));
123 putdef(line
, " -DDISPLAY=", buf
);
124 putdef(line
, " -DWM_VERSION=", VERSION
);
126 visual
= DefaultVisual(dpy
, DefaultScreen(dpy
));
127 putidef(line
, " -DVISUAL=", visual
->class);
129 putidef(line
, " -DDEPTH=", DefaultDepth(dpy
, DefaultScreen(dpy
)));
131 putidef(line
, " -DSCR_WIDTH=", WidthOfScreen(DefaultScreenOfDisplay(dpy
)));
132 putidef(line
, " -DSCR_HEIGHT=", HeightOfScreen(DefaultScreenOfDisplay(dpy
)));
134 /* put the dir where the menu is being read from to the
138 buf
= strchr(tmp
+ 1, ' ');
142 buf
= strrchr(tmp
, '/');
144 *buf
= 0; /* trunc filename */
145 putdef(line
, " -I", tmp
);
150 /* this should be done just once, but it works this way */
151 strcpy(buffer
, DEF_CONFIG_PATHS
);
152 buf
= strtok(buffer
, ":");
155 char fullpath
[MAXLINE
];
158 strcpy(fullpath
, buf
);
160 /* home is statically allocated. Don't free it! */
161 char *home
= wgethomedir();
163 strcpy(fullpath
, home
);
164 strcat(fullpath
, &(buf
[1]));
167 putdef(line
, " -I", fullpath
);
169 } while ((buf
= strtok(NULL
, ":")) != NULL
);
176 /* XFetchName Wrapper */
177 Bool
wFetchName(Display
*dpy
, Window win
, char **winname
)
179 XTextProperty text_prop
;
183 if (XGetWMName(dpy
, win
, &text_prop
)) {
184 if (text_prop
.value
&& text_prop
.nitems
> 0) {
185 if (text_prop
.encoding
== XA_STRING
) {
186 *winname
= wstrdup((char *)text_prop
.value
);
187 XFree(text_prop
.value
);
189 text_prop
.nitems
= strlen((char *)text_prop
.value
);
190 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >=
191 Success
&& num
> 0 && *list
) {
192 XFree(text_prop
.value
);
193 *winname
= wstrdup(*list
);
194 XFreeStringList(list
);
196 *winname
= wstrdup((char *)text_prop
.value
);
197 XFree(text_prop
.value
);
201 /* the title is set, but it was set to none */
202 *winname
= wstrdup("");
206 /* the hint is probably not set */
213 /* XGetIconName Wrapper */
214 Bool
wGetIconName(Display
*dpy
, Window win
, char **iconname
)
216 XTextProperty text_prop
;
220 if (XGetWMIconName(dpy
, win
, &text_prop
) != 0 && text_prop
.value
&& text_prop
.nitems
> 0) {
221 if (text_prop
.encoding
== XA_STRING
)
222 *iconname
= (char *)text_prop
.value
;
224 text_prop
.nitems
= strlen((char *)text_prop
.value
);
225 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >= Success
&& num
> 0 && *list
) {
226 XFree(text_prop
.value
);
227 *iconname
= wstrdup(*list
);
228 XFreeStringList(list
);
230 *iconname
= (char *)text_prop
.value
;
238 static void eatExpose()
242 /* compress all expose events into a single one */
244 if (XCheckMaskEvent(dpy
, ExposureMask
, &event
)) {
245 /* ignore other exposure events for this window */
246 while (XCheckWindowEvent(dpy
, event
.xexpose
.window
, ExposureMask
, &foo
)) ;
247 /* eat exposes for other windows */
250 event
.xexpose
.count
= 0;
251 XPutBackEvent(dpy
, &event
);
255 void SlideWindow(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
257 time_t time0
= time(NULL
);
258 float dx
, dy
, x
= from_x
, y
= from_y
, sx
, sy
, px
, py
;
259 int dx_is_bigger
= 0;
260 int slide_delay
, slide_steps
, slide_slowdown
;
262 /* animation parameters */
268 {ICON_SLIDE_DELAY_UF
, ICON_SLIDE_STEPS_UF
, ICON_SLIDE_SLOWDOWN_UF
},
269 {ICON_SLIDE_DELAY_F
, ICON_SLIDE_STEPS_F
, ICON_SLIDE_SLOWDOWN_F
},
270 {ICON_SLIDE_DELAY_M
, ICON_SLIDE_STEPS_M
, ICON_SLIDE_SLOWDOWN_M
},
271 {ICON_SLIDE_DELAY_S
, ICON_SLIDE_STEPS_S
, ICON_SLIDE_SLOWDOWN_S
},
272 {ICON_SLIDE_DELAY_US
, ICON_SLIDE_STEPS_US
, ICON_SLIDE_SLOWDOWN_US
}
275 slide_slowdown
= apars
[(int)wPreferences
.icon_slide_speed
].slowdown
;
276 slide_steps
= apars
[(int)wPreferences
.icon_slide_speed
].steps
;
277 slide_delay
= apars
[(int)wPreferences
.icon_slide_speed
].delay
;
279 dx
= (float)(to_x
- from_x
);
280 dy
= (float)(to_y
- from_y
);
281 sx
= (dx
== 0 ? 0 : fabs(dx
) / dx
);
282 sy
= (dy
== 0 ? 0 : fabs(dy
) / dy
);
284 if (fabs(dx
) > fabs(dy
)) {
289 px
= dx
/ slide_slowdown
;
290 if (px
< slide_steps
&& px
> 0)
292 else if (px
> -slide_steps
&& px
< 0)
294 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
296 py
= dy
/ slide_slowdown
;
297 if (py
< slide_steps
&& py
> 0)
299 else if (py
> -slide_steps
&& py
< 0)
301 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
304 while (x
!= to_x
|| y
!= to_y
) {
307 if ((px
< 0 && (int)x
< to_x
) || (px
> 0 && (int)x
> to_x
))
309 if ((py
< 0 && (int)y
< to_y
) || (py
> 0 && (int)y
> to_y
))
313 px
= px
* (1.0 - 1 / (float)slide_slowdown
);
314 if (px
< slide_steps
&& px
> 0)
316 else if (px
> -slide_steps
&& px
< 0)
318 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
320 py
= py
* (1.0 - 1 / (float)slide_slowdown
);
321 if (py
< slide_steps
&& py
> 0)
323 else if (py
> -slide_steps
&& py
< 0)
325 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
328 XMoveWindow(dpy
, win
, (int)x
, (int)y
);
330 if (slide_delay
> 0) {
331 wusleep(slide_delay
* 1000L);
335 if (time(NULL
) - time0
> MAX_ANIMATION_TIME
)
338 XMoveWindow(dpy
, win
, to_x
, to_y
);
341 /* compress expose events */
345 char *ShrinkString(WMFont
* font
, char *string
, int width
)
354 w
= WMWidthOfString(font
, string
, p
);
355 text
= wmalloc(strlen(string
) + 8);
356 strcpy(text
, string
);
360 pos
= strchr(text
, ' ');
362 pos
= strchr(text
, ':');
367 w1
= WMWidthOfString(font
, text
, p
);
384 width
-= WMWidthOfString(font
, "...", 3);
389 while (p2
> p1
&& p1
!= t
) {
390 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
393 t
= p1
+ (p2
- p1
) / 2;
394 } else if (w
< width
) {
396 t
= p1
+ (p2
- p1
) / 2;
400 strcat(text
, &string
[p
- p1
]);
405 char *FindImage(char *paths
, char *file
)
409 tmp
= strrchr(file
, ':');
412 path
= wfindfile(paths
, file
);
416 path
= wfindfile(paths
, file
);
422 static void timeoutHandler(void *data
)
427 static char *getTextSelection(WScreen
* screen
, Atom selection
)
461 data
= XFetchBuffer(dpy
, &size
, buffer
);
468 unsigned long len
, bytes
;
472 static Atom clipboard
= 0;
475 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
477 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
479 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
481 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
483 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
486 WMDeleteTimerHandler(timer
);
488 wwarning("selection retrieval timed out");
492 /* nobody owns the selection or the current owner has
493 * nothing to do with what we need */
494 if (ev
.xselection
.property
== None
) {
498 if (XGetWindowProperty(dpy
, screen
->info_window
,
500 False
, XA_STRING
, &rtype
, &bits
, &len
,
501 &bytes
, (unsigned char **)&data
) != Success
) {
504 if (rtype
!= XA_STRING
|| bits
!= 8) {
505 wwarning("invalid data in text selection");
514 static char *getselection(WScreen
* scr
)
518 tmp
= getTextSelection(scr
, XA_PRIMARY
);
520 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
525 parseuserinputpart(char *line
, int *ptr
, char *endchars
)
527 int depth
= 0, begin
;
531 while(line
[*ptr
] != '\0') {
532 if(line
[*ptr
] == '(') {
534 } else if(depth
> 0 && line
[*ptr
] == ')') {
536 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
537 value
= wmalloc(*ptr
- begin
+ 1);
538 strncpy(value
, line
+ begin
, *ptr
- begin
);
539 value
[*ptr
- begin
] = '\0';
549 getuserinput(WScreen
*scr
, char *line
, int *ptr
, Bool advanced
)
551 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
554 if(line
[*ptr
] == '(')
555 title
= parseuserinputpart(line
, ptr
, ",)");
556 if(title
!= NULL
&& line
[*ptr
] == ',')
557 prompt
= parseuserinputpart(line
, ptr
, ",)");
558 if(prompt
!= NULL
&& line
[*ptr
] == ',')
559 name
= parseuserinputpart(line
, ptr
, ")");
562 rv
= wAdvancedInputDialog(scr
,
563 title
? _(title
):_("Program Arguments"),
564 prompt
? _(prompt
):_("Enter command arguments:"),
567 rv
= wInputDialog(scr
,
568 title
? _(title
):_("Program Arguments"),
569 prompt
? _(prompt
):_("Enter command arguments:"),
572 if(title
) wfree(title
);
573 if(prompt
) wfree(prompt
);
574 if(name
) wfree(name
);
576 return rv
? ret
: NULL
;
584 * state input new-state output
585 * NORMAL % OPTION <nil>
586 * NORMAL \ ESCAPE <nil>
587 * NORMAL etc. NORMAL <input>
588 * ESCAPE any NORMAL <input>
589 * OPTION s NORMAL <selection buffer>
590 * OPTION w NORMAL <selected window id>
591 * OPTION a NORMAL <input text>
592 * OPTION d NORMAL <OffiX DND selection object>
593 * OPTION W NORMAL <current workspace>
594 * OPTION etc. NORMAL %<input>
596 #define TMPBUFSIZE 64
597 char *ExpandOptions(WScreen
* scr
, char *cmdline
)
599 int ptr
, optr
, state
, len
, olen
;
601 char *selection
= NULL
;
602 char *user_input
= NULL
;
604 char *dropped_thing
= NULL
;
606 char tmpbuf
[TMPBUFSIZE
];
609 len
= strlen(cmdline
);
613 wwarning(_("out of memory during expansion of \"%s\""), cmdline
);
617 ptr
= 0; /* input line pointer */
618 optr
= 0; /* output line pointer */
623 switch (cmdline
[ptr
]) {
632 out
[optr
++] = cmdline
[ptr
];
637 switch (cmdline
[ptr
]) {
651 out
[optr
++] = cmdline
[ptr
];
657 switch (cmdline
[ptr
]) {
659 if (scr
->focused_window
&& scr
->focused_window
->flags
.focused
) {
660 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
661 (unsigned int)scr
->focused_window
->client_win
);
662 slen
= strlen(tmpbuf
);
664 nout
= realloc(out
, olen
);
666 wwarning(_("out of memory during expansion of \"%%w\""));
678 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x", (unsigned int)scr
->current_workspace
+ 1);
679 slen
= strlen(tmpbuf
);
681 nout
= realloc(out
, olen
);
683 wwarning(_("out of memory during expansion of \"%%W\""));
694 user_input
= getuserinput(scr
, cmdline
, &ptr
, cmdline
[ptr
-1] == 'A');
696 slen
= strlen(user_input
);
698 nout
= realloc(out
, olen
);
700 wwarning(_("out of memory during expansion of \"%%a\""));
704 strcat(out
, user_input
);
707 /* Not an error, but user has Canceled the dialog box.
708 * This will make the command to not be performed. */
715 if (scr
->xdestring
) {
716 dropped_thing
= wstrdup(scr
->xdestring
);
718 if (!dropped_thing
) {
719 dropped_thing
= get_dnd_selection(scr
);
721 if (!dropped_thing
) {
722 scr
->flags
.dnd_data_convertion_status
= 1;
725 slen
= strlen(dropped_thing
);
727 nout
= realloc(out
, olen
);
729 wwarning(_("out of memory during expansion of \"%%d\""));
733 strcat(out
, dropped_thing
);
740 selection
= getselection(scr
);
743 wwarning(_("selection not available"));
746 slen
= strlen(selection
);
748 nout
= realloc(out
, olen
);
750 wwarning(_("out of memory during expansion of \"%%s\""));
754 strcat(out
, selection
);
760 out
[optr
++] = cmdline
[ptr
];
778 void ParseWindowName(WMPropList
* value
, char **winstance
, char **wclass
, char *where
)
782 *winstance
= *wclass
= NULL
;
784 if (!WMIsPLString(value
)) {
785 wwarning(_("bad window name value in %s state info"), where
);
789 name
= WMGetFromPLString(value
);
790 if (!name
|| strlen(name
) == 0) {
791 wwarning(_("bad window name value in %s state info"), where
);
795 UnescapeWM_CLASS(name
, winstance
, wclass
);
799 static char *keysymToString(KeySym keysym
, unsigned int state
)
802 char *buf
= wmalloc(20);
807 kev
.send_event
= False
;
808 kev
.window
= DefaultRootWindow(dpy
);
809 kev
.root
= DefaultRootWindow(dpy
);
810 kev
.same_screen
= True
;
811 kev
.subwindow
= kev
.root
;
812 kev
.serial
= 0x12344321;
813 kev
.time
= CurrentTime
;
815 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
816 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
823 char *GetShortcutString(char *text
)
832 tmp
= text
= wstrdup(text
);
835 while ((k
= strchr(text
, '+')) != NULL
) {
839 mod
= wXModifierFromKey(text
);
841 return wstrdup("bug");
846 if (strcasecmp(text
, "Meta") == 0) {
847 buffer
= wstrappend(buffer
, "M+");
848 } else if (strcasecmp(text
, "Alt") == 0) {
849 buffer
= wstrappend(buffer
, "A+");
850 } else if (strcasecmp(text
, "Shift") == 0) {
851 buffer
= wstrappend(buffer
, "Sh+");
852 } else if (strcasecmp(text
, "Mod1") == 0) {
853 buffer
= wstrappend(buffer
, "M1+");
854 } else if (strcasecmp(text
, "Mod2") == 0) {
855 buffer
= wstrappend(buffer
, "M2+");
856 } else if (strcasecmp(text
, "Mod3") == 0) {
857 buffer
= wstrappend(buffer
, "M3+");
858 } else if (strcasecmp(text
, "Mod4") == 0) {
859 buffer
= wstrappend(buffer
, "M4+");
860 } else if (strcasecmp(text
, "Mod5") == 0) {
861 buffer
= wstrappend(buffer
, "M5+");
862 } else if (strcasecmp(text
, "Control") == 0) {
865 buffer
= wstrappend(buffer
, text
);
871 buffer
= wstrappend(buffer
, "^");
873 buffer
= wstrappend(buffer
, text
);
876 /* ksym = XStringToKeysym(text);
877 tmp = keysymToString(ksym, modmask);
879 buffer = wstrappend(buffer, tmp);
886 char *EscapeWM_CLASS(char *name
, char *class)
889 char *ename
= NULL
, *eclass
= NULL
;
897 ename
= wmalloc(l
* 2 + 1);
899 for (i
= 0; i
< l
; i
++) {
900 if (name
[i
] == '\\') {
902 } else if (name
[i
] == '.') {
905 ename
[j
++] = name
[i
];
911 eclass
= wmalloc(l
* 2 + 1);
913 for (i
= 0; i
< l
; i
++) {
914 if (class[i
] == '\\') {
916 } else if (class[i
] == '.') {
919 eclass
[j
++] = class[i
];
924 if (ename
&& eclass
) {
925 int len
= strlen(ename
) + strlen(eclass
) + 4;
927 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
931 ret
= wstrdup(ename
);
934 ret
= wstrdup(eclass
);
941 void UnescapeWM_CLASS(char *str
, char **name
, char **class)
951 /* separate string in 2 parts */
953 for (i
= 0; i
< j
; i
++) {
954 if (str
[i
] == '\\') {
957 } else if (str
[i
] == '.') {
963 /* unescape strings */
964 for (i
= 0, k
= 0; i
< dot
; i
++) {
965 if (str
[i
] == '\\') {
968 (*name
)[k
++] = str
[i
];
973 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
974 if (str
[i
] == '\\') {
977 (*class)[k
++] = str
[i
];
992 void SendHelperMessage(WScreen
* scr
, char type
, int workspace
, char *msg
)
999 if (!scr
->flags
.backimage_helper_launched
) {
1003 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
1004 buffer
= wmalloc(len
+ 5);
1005 snprintf(buf
, sizeof(buf
), "%4i", len
);
1006 memcpy(buffer
, buf
, 4);
1009 if (workspace
>= 0) {
1010 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
1011 memcpy(&buffer
[i
], buf
, 4);
1016 strcpy(&buffer
[i
], msg
);
1018 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
1019 wsyserror(_("could not send message to background image helper"));
1024 Bool
UpdateDomainFile(WDDomain
* domain
)
1027 char path
[PATH_MAX
];
1028 WMPropList
*shared_dict
, *dict
;
1029 Bool result
, freeDict
= False
;
1031 dict
= domain
->dictionary
;
1032 if (WMIsPLDictionary(domain
->dictionary
)) {
1033 /* retrieve global system dictionary */
1034 snprintf(path
, sizeof(path
), "%s/WindowMaker/%s", SYSCONFDIR
, domain
->domain_name
);
1035 if (stat(path
, &stbuf
) >= 0) {
1036 shared_dict
= WMReadPropListFromFile(path
);
1038 if (WMIsPLDictionary(shared_dict
)) {
1040 dict
= WMDeepCopyPropList(domain
->dictionary
);
1041 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
1043 WMReleasePropList(shared_dict
);
1048 result
= WMWritePropListToFile(dict
, domain
->path
);
1051 WMReleasePropList(dict
);
1057 char *StrConcatDot(char *a
, char *b
)
1067 len
= strlen(a
) + strlen(b
) + 4;
1070 snprintf(str
, len
, "%s.%s", a
, b
);
1075 static char *getCommandForWindow(Window win
, int elements
)
1077 char **argv
, *command
= NULL
;
1080 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
1081 if (argc
> 0 && argv
!= NULL
) {
1084 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
1085 if (command
[0] == 0) {
1091 XFreeStringList(argv
);
1098 /* Free result when done */
1099 char *GetCommandForWindow(Window win
)
1101 return getCommandForWindow(win
, 0);
1104 /* Free result when done */
1105 char *GetProgramNameForWindow(Window win
)
1107 return getCommandForWindow(win
, 1);