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>
37 #include <WINGs/WUtil.h>
40 #include "WindowMaker.h"
50 #include "xmodifier.h"
52 /**** global variables *****/
54 extern WPreferences wPreferences
;
56 extern Time LastTimestamp
;
59 static void putdef(char *line
, char *name
, char *value
)
62 wwarning(_("could not define value for %s for cpp"), name
);
69 static void putidef(char *line
, char *name
, int value
)
72 snprintf(tmp
, sizeof(tmp
), "%i", value
);
77 static char *username()
85 user
= getpwuid(getuid());
87 wsyserror(_("could not get password entry for UID %i"), getuid());
99 char *MakeCPPArgs(char *path
)
102 char buffer
[MAXLINE
], *buf
, *line
;
106 line
= wmalloc(MAXLINE
);
109 if ((buf
= getenv("HOSTNAME")) != NULL
) {
111 wwarning(_("your machine is misconfigured. HOSTNAME is set to %s"), buf
);
113 putdef(line
, " -DHOST=", buf
);
114 } else if ((buf
= getenv("HOST")) != NULL
) {
116 wwarning(_("your machine is misconfigured. HOST is set to %s"), buf
);
118 putdef(line
, " -DHOST=", buf
);
122 putdef(line
, " -DUSER=", buf
);
123 putidef(line
, " -DUID=", getuid());
124 buf
= XDisplayName(DisplayString(dpy
));
125 putdef(line
, " -DDISPLAY=", buf
);
126 putdef(line
, " -DWM_VERSION=", VERSION
);
128 visual
= DefaultVisual(dpy
, DefaultScreen(dpy
));
129 putidef(line
, " -DVISUAL=", visual
->class);
131 putidef(line
, " -DDEPTH=", DefaultDepth(dpy
, DefaultScreen(dpy
)));
133 putidef(line
, " -DSCR_WIDTH=", WidthOfScreen(DefaultScreenOfDisplay(dpy
)));
134 putidef(line
, " -DSCR_HEIGHT=", HeightOfScreen(DefaultScreenOfDisplay(dpy
)));
136 /* put the dir where the menu is being read from to the
140 buf
= strchr(tmp
+ 1, ' ');
144 buf
= strrchr(tmp
, '/');
146 *buf
= 0; /* trunc filename */
147 putdef(line
, " -I", tmp
);
152 /* this should be done just once, but it works this way */
153 strcpy(buffer
, DEF_CONFIG_PATHS
);
154 buf
= strtok(buffer
, ":");
157 char fullpath
[MAXLINE
];
160 strcpy(fullpath
, buf
);
163 /* home is statically allocated. Don't free it! */
164 char *home
= wgethomedir();
166 strcpy(fullpath
, home
);
167 strcat(fullpath
, &(buf
[1]));
170 putdef(line
, " -I", fullpath
);
172 } while ((buf
= strtok(NULL
, ":")) != NULL
);
185 * Is win2 below win1?
187 static Bool
isBelow(WWindow
* win1
, WWindow
* win2
)
192 tmp
= win1
->frame
->core
->stacking
->under
;
194 if (tmp
== win2
->frame
->core
)
196 tmp
= tmp
->stacking
->under
;
199 for (i
= win1
->frame
->core
->stacking
->window_level
- 1; i
>= 0; i
--) {
200 tmp
= win1
->screen_ptr
->stacking_list
[i
];
202 if (tmp
== win2
->frame
->core
)
204 tmp
= tmp
->stacking
->under
;
211 /* XFetchName Wrapper */
212 Bool
wFetchName(Display
*dpy
, Window win
, char **winname
)
214 XTextProperty text_prop
;
218 if (XGetWMName(dpy
, win
, &text_prop
)) {
219 if (text_prop
.value
&& text_prop
.nitems
> 0) {
220 if (text_prop
.encoding
== XA_STRING
) {
221 *winname
= wstrdup((char *)text_prop
.value
);
222 XFree(text_prop
.value
);
224 text_prop
.nitems
= strlen((char *)text_prop
.value
);
225 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >=
226 Success
&& num
> 0 && *list
) {
227 XFree(text_prop
.value
);
228 *winname
= wstrdup(*list
);
229 XFreeStringList(list
);
231 *winname
= wstrdup((char *)text_prop
.value
);
232 XFree(text_prop
.value
);
236 /* the title is set, but it was set to none */
237 *winname
= wstrdup("");
241 /* the hint is probably not set */
248 /* XGetIconName Wrapper */
249 Bool
wGetIconName(Display
*dpy
, Window win
, char **iconname
)
251 XTextProperty text_prop
;
255 if (XGetWMIconName(dpy
, win
, &text_prop
) != 0 && text_prop
.value
&& text_prop
.nitems
> 0) {
256 if (text_prop
.encoding
== XA_STRING
)
257 *iconname
= (char *)text_prop
.value
;
259 text_prop
.nitems
= strlen((char *)text_prop
.value
);
260 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >= Success
&& num
> 0 && *list
) {
261 XFree(text_prop
.value
);
262 *iconname
= wstrdup(*list
);
263 XFreeStringList(list
);
265 *iconname
= (char *)text_prop
.value
;
273 static void eatExpose()
277 /* compress all expose events into a single one */
279 if (XCheckMaskEvent(dpy
, ExposureMask
, &event
)) {
280 /* ignore other exposure events for this window */
281 while (XCheckWindowEvent(dpy
, event
.xexpose
.window
, ExposureMask
, &foo
)) ;
282 /* eat exposes for other windows */
285 event
.xexpose
.count
= 0;
286 XPutBackEvent(dpy
, &event
);
290 void SlideWindow(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
292 time_t time0
= time(NULL
);
293 float dx
, dy
, x
= from_x
, y
= from_y
, sx
, sy
, px
, py
;
294 int dx_is_bigger
= 0;
295 int slide_delay
, slide_steps
, slide_slowdown
;
297 /* animation parameters */
303 {ICON_SLIDE_DELAY_UF
, ICON_SLIDE_STEPS_UF
, ICON_SLIDE_SLOWDOWN_UF
},
304 {ICON_SLIDE_DELAY_F
, ICON_SLIDE_STEPS_F
, ICON_SLIDE_SLOWDOWN_F
},
305 {ICON_SLIDE_DELAY_M
, ICON_SLIDE_STEPS_M
, ICON_SLIDE_SLOWDOWN_M
},
306 {ICON_SLIDE_DELAY_S
, ICON_SLIDE_STEPS_S
, ICON_SLIDE_SLOWDOWN_S
},
307 {ICON_SLIDE_DELAY_US
, ICON_SLIDE_STEPS_US
, ICON_SLIDE_SLOWDOWN_US
}
310 slide_slowdown
= apars
[(int)wPreferences
.icon_slide_speed
].slowdown
;
311 slide_steps
= apars
[(int)wPreferences
.icon_slide_speed
].steps
;
312 slide_delay
= apars
[(int)wPreferences
.icon_slide_speed
].delay
;
314 dx
= (float)(to_x
- from_x
);
315 dy
= (float)(to_y
- from_y
);
316 sx
= (dx
== 0 ? 0 : fabs(dx
) / dx
);
317 sy
= (dy
== 0 ? 0 : fabs(dy
) / dy
);
319 if (fabs(dx
) > fabs(dy
)) {
324 px
= dx
/ slide_slowdown
;
325 if (px
< slide_steps
&& px
> 0)
327 else if (px
> -slide_steps
&& px
< 0)
329 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
331 py
= dy
/ slide_slowdown
;
332 if (py
< slide_steps
&& py
> 0)
334 else if (py
> -slide_steps
&& py
< 0)
336 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
339 while (x
!= to_x
|| y
!= to_y
) {
342 if ((px
< 0 && (int)x
< to_x
) || (px
> 0 && (int)x
> to_x
))
344 if ((py
< 0 && (int)y
< to_y
) || (py
> 0 && (int)y
> to_y
))
348 px
= px
* (1.0 - 1 / (float)slide_slowdown
);
349 if (px
< slide_steps
&& px
> 0)
351 else if (px
> -slide_steps
&& px
< 0)
353 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
355 py
= py
* (1.0 - 1 / (float)slide_slowdown
);
356 if (py
< slide_steps
&& py
> 0)
358 else if (py
> -slide_steps
&& py
< 0)
360 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
363 XMoveWindow(dpy
, win
, (int)x
, (int)y
);
365 if (slide_delay
> 0) {
366 wusleep(slide_delay
* 1000L);
370 if (time(NULL
) - time0
> MAX_ANIMATION_TIME
)
373 XMoveWindow(dpy
, win
, to_x
, to_y
);
376 /* compress expose events */
380 char *ShrinkString(WMFont
* font
, char *string
, int width
)
389 w
= WMWidthOfString(font
, string
, p
);
390 text
= wmalloc(strlen(string
) + 8);
391 strcpy(text
, string
);
395 pos
= strchr(text
, ' ');
397 pos
= strchr(text
, ':');
402 w1
= WMWidthOfString(font
, text
, p
);
419 width
-= WMWidthOfString(font
, "...", 3);
424 while (p2
> p1
&& p1
!= t
) {
425 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
428 t
= p1
+ (p2
- p1
) / 2;
429 } else if (w
< width
) {
431 t
= p1
+ (p2
- p1
) / 2;
435 strcat(text
, &string
[p
- p1
]);
440 char *FindImage(char *paths
, char *file
)
444 tmp
= strrchr(file
, ':');
447 path
= wfindfile(paths
, file
);
451 path
= wfindfile(paths
, file
);
457 static void timeoutHandler(void *data
)
462 static char *getTextSelection(WScreen
* screen
, Atom selection
)
496 data
= XFetchBuffer(dpy
, &size
, buffer
);
503 unsigned long len
, bytes
;
507 static Atom clipboard
= 0;
510 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
512 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
514 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
516 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
518 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
521 WMDeleteTimerHandler(timer
);
523 wwarning("selection retrieval timed out");
527 /* nobody owns the selection or the current owner has
528 * nothing to do with what we need */
529 if (ev
.xselection
.property
== None
) {
533 if (XGetWindowProperty(dpy
, screen
->info_window
,
535 False
, XA_STRING
, &rtype
, &bits
, &len
,
536 &bytes
, (unsigned char **)&data
) != Success
) {
539 if (rtype
!= XA_STRING
|| bits
!= 8) {
540 wwarning("invalid data in text selection");
549 static char *getselection(WScreen
* scr
)
553 tmp
= getTextSelection(scr
, XA_PRIMARY
);
555 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
560 parseuserinputpart(char *line
, int *ptr
, char *endchars
)
562 int depth
= 0, begin
;
566 while(line
[*ptr
] != '\0') {
567 if(line
[*ptr
] == '(') {
569 } else if(depth
> 0 && line
[*ptr
] == ')') {
571 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
572 value
= wmalloc(*ptr
- begin
+ 1);
573 strncpy(value
, line
+ begin
, *ptr
- begin
);
574 value
[*ptr
- begin
] = '\0';
584 getuserinput(WScreen
*scr
, char *line
, int *ptr
, Bool advanced
)
586 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
589 if(line
[*ptr
] == '(')
590 title
= parseuserinputpart(line
, ptr
, ",)");
591 if(title
!= NULL
&& line
[*ptr
] == ',')
592 prompt
= parseuserinputpart(line
, ptr
, ",)");
593 if(prompt
!= NULL
&& line
[*ptr
] == ',')
594 name
= parseuserinputpart(line
, ptr
, ")");
597 rv
= wAdvancedInputDialog(scr
,
598 title
? gettext(title
):_("Program Arguments"),
599 prompt
? gettext(prompt
):_("Enter command arguments:"),
602 rv
= wInputDialog(scr
,
603 title
? gettext(title
):_("Program Arguments"),
604 prompt
? gettext(prompt
):_("Enter command arguments:"),
607 if(title
) wfree(title
);
608 if(prompt
) wfree(prompt
);
609 if(name
) wfree(name
);
611 return rv
? ret
: NULL
;
619 * state input new-state output
620 * NORMAL % OPTION <nil>
621 * NORMAL \ ESCAPE <nil>
622 * NORMAL etc. NORMAL <input>
623 * ESCAPE any NORMAL <input>
624 * OPTION s NORMAL <selection buffer>
625 * OPTION w NORMAL <selected window id>
626 * OPTION a NORMAL <input text>
627 * OPTION d NORMAL <OffiX DND selection object>
628 * OPTION W NORMAL <current workspace>
629 * OPTION etc. NORMAL %<input>
631 #define TMPBUFSIZE 64
632 char *ExpandOptions(WScreen
* scr
, char *cmdline
)
634 int ptr
, optr
, state
, len
, olen
;
636 char *selection
= NULL
;
637 char *user_input
= NULL
;
639 char *dropped_thing
= NULL
;
641 char tmpbuf
[TMPBUFSIZE
];
644 len
= strlen(cmdline
);
648 wwarning(_("out of memory during expansion of \"%s\""));
652 ptr
= 0; /* input line pointer */
653 optr
= 0; /* output line pointer */
658 switch (cmdline
[ptr
]) {
667 out
[optr
++] = cmdline
[ptr
];
672 switch (cmdline
[ptr
]) {
686 out
[optr
++] = cmdline
[ptr
];
692 switch (cmdline
[ptr
]) {
694 if (scr
->focused_window
&& scr
->focused_window
->flags
.focused
) {
695 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
696 (unsigned int)scr
->focused_window
->client_win
);
697 slen
= strlen(tmpbuf
);
699 nout
= realloc(out
, olen
);
701 wwarning(_("out of memory during expansion of \"%w\""));
713 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x", (unsigned int)scr
->current_workspace
+ 1);
714 slen
= strlen(tmpbuf
);
716 nout
= realloc(out
, olen
);
718 wwarning(_("out of memory during expansion of \"%W\""));
729 user_input
= getuserinput(scr
, cmdline
, &ptr
, cmdline
[ptr
-1] == 'A');
731 slen
= strlen(user_input
);
733 nout
= realloc(out
, olen
);
735 wwarning(_("out of memory during expansion of \"%a\""));
739 strcat(out
, user_input
);
742 /* Not an error, but user has Canceled the dialog box.
743 * This will make the command to not be performed. */
750 if (scr
->xdestring
) {
751 dropped_thing
= wstrdup(scr
->xdestring
);
753 if (!dropped_thing
) {
754 dropped_thing
= get_dnd_selection(scr
);
756 if (!dropped_thing
) {
757 scr
->flags
.dnd_data_convertion_status
= 1;
760 slen
= strlen(dropped_thing
);
762 nout
= realloc(out
, olen
);
764 wwarning(_("out of memory during expansion of \"%d\""));
768 strcat(out
, dropped_thing
);
775 selection
= getselection(scr
);
778 wwarning(_("selection not available"));
781 slen
= strlen(selection
);
783 nout
= realloc(out
, olen
);
785 wwarning(_("out of memory during expansion of \"%s\""));
789 strcat(out
, selection
);
795 out
[optr
++] = cmdline
[ptr
];
813 void ParseWindowName(WMPropList
* value
, char **winstance
, char **wclass
, char *where
)
817 *winstance
= *wclass
= NULL
;
819 if (!WMIsPLString(value
)) {
820 wwarning(_("bad window name value in %s state info"), where
);
824 name
= WMGetFromPLString(value
);
825 if (!name
|| strlen(name
) == 0) {
826 wwarning(_("bad window name value in %s state info"), where
);
830 UnescapeWM_CLASS(name
, winstance
, wclass
);
834 static char *keysymToString(KeySym keysym
, unsigned int state
)
837 char *buf
= wmalloc(20);
842 kev
.send_event
= False
;
843 kev
.window
= DefaultRootWindow(dpy
);
844 kev
.root
= DefaultRootWindow(dpy
);
845 kev
.same_screen
= True
;
846 kev
.subwindow
= kev
.root
;
847 kev
.serial
= 0x12344321;
848 kev
.time
= CurrentTime
;
850 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
851 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
858 char *GetShortcutString(char *text
)
867 tmp
= text
= wstrdup(text
);
870 while ((k
= strchr(text
, '+')) != NULL
) {
874 mod
= wXModifierFromKey(text
);
876 return wstrdup("bug");
881 if (strcasecmp(text
, "Meta") == 0) {
882 buffer
= wstrappend(buffer
, "M+");
883 } else if (strcasecmp(text
, "Alt") == 0) {
884 buffer
= wstrappend(buffer
, "A+");
885 } else if (strcasecmp(text
, "Shift") == 0) {
886 buffer
= wstrappend(buffer
, "Sh+");
887 } else if (strcasecmp(text
, "Mod1") == 0) {
888 buffer
= wstrappend(buffer
, "M1+");
889 } else if (strcasecmp(text
, "Mod2") == 0) {
890 buffer
= wstrappend(buffer
, "M2+");
891 } else if (strcasecmp(text
, "Mod3") == 0) {
892 buffer
= wstrappend(buffer
, "M3+");
893 } else if (strcasecmp(text
, "Mod4") == 0) {
894 buffer
= wstrappend(buffer
, "M4+");
895 } else if (strcasecmp(text
, "Mod5") == 0) {
896 buffer
= wstrappend(buffer
, "M5+");
897 } else if (strcasecmp(text
, "Control") == 0) {
900 buffer
= wstrappend(buffer
, text
);
906 buffer
= wstrappend(buffer
, "^");
908 buffer
= wstrappend(buffer
, text
);
911 /* ksym = XStringToKeysym(text);
912 tmp = keysymToString(ksym, modmask);
914 buffer = wstrappend(buffer, tmp);
921 char *EscapeWM_CLASS(char *name
, char *class)
924 char *ename
= NULL
, *eclass
= NULL
;
932 ename
= wmalloc(l
* 2 + 1);
934 for (i
= 0; i
< l
; i
++) {
935 if (name
[i
] == '\\') {
937 } else if (name
[i
] == '.') {
940 ename
[j
++] = name
[i
];
946 eclass
= wmalloc(l
* 2 + 1);
948 for (i
= 0; i
< l
; i
++) {
949 if (class[i
] == '\\') {
951 } else if (class[i
] == '.') {
954 eclass
[j
++] = class[i
];
959 if (ename
&& eclass
) {
960 int len
= strlen(ename
) + strlen(eclass
) + 4;
962 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
966 ret
= wstrdup(ename
);
969 ret
= wstrdup(eclass
);
976 void UnescapeWM_CLASS(char *str
, char **name
, char **class)
986 /* separate string in 2 parts */
988 for (i
= 0; i
< j
; i
++) {
989 if (str
[i
] == '\\') {
992 } else if (str
[i
] == '.') {
998 /* unescape strings */
999 for (i
= 0, k
= 0; i
< dot
; i
++) {
1000 if (str
[i
] == '\\') {
1003 (*name
)[k
++] = str
[i
];
1008 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
1009 if (str
[i
] == '\\') {
1012 (*class)[k
++] = str
[i
];
1027 void SendHelperMessage(WScreen
* scr
, char type
, int workspace
, char *msg
)
1034 if (!scr
->flags
.backimage_helper_launched
) {
1038 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
1039 buffer
= wmalloc(len
+ 5);
1040 snprintf(buf
, sizeof(buf
), "%4i", len
);
1041 memcpy(buffer
, buf
, 4);
1044 if (workspace
>= 0) {
1045 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
1046 memcpy(&buffer
[i
], buf
, 4);
1051 strcpy(&buffer
[i
], msg
);
1053 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
1054 wsyserror(_("could not send message to background image helper"));
1059 Bool
UpdateDomainFile(WDDomain
* domain
)
1062 char path
[PATH_MAX
];
1063 WMPropList
*shared_dict
, *dict
;
1064 Bool result
, freeDict
= False
;
1066 dict
= domain
->dictionary
;
1067 if (WMIsPLDictionary(domain
->dictionary
)) {
1068 /* retrieve global system dictionary */
1069 snprintf(path
, sizeof(path
), "%s/WindowMaker/%s", SYSCONFDIR
, domain
->domain_name
);
1070 if (stat(path
, &stbuf
) >= 0) {
1071 shared_dict
= WMReadPropListFromFile(path
);
1073 if (WMIsPLDictionary(shared_dict
)) {
1075 dict
= WMDeepCopyPropList(domain
->dictionary
);
1076 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
1078 WMReleasePropList(shared_dict
);
1083 result
= WMWritePropListToFile(dict
, domain
->path
, True
);
1086 WMReleasePropList(dict
);
1092 char *StrConcatDot(char *a
, char *b
)
1102 len
= strlen(a
) + strlen(b
) + 4;
1105 snprintf(str
, len
, "%s.%s", a
, b
);
1110 #define MAX_CMD_SIZE 4096
1112 Bool
GetCommandForPid(int pid
, char ***argv
, int *argc
)
1114 static char buf
[MAX_CMD_SIZE
];
1119 sprintf(buf
, "/proc/%d/cmdline", pid
);
1120 fPtr
= fopen(buf
, "r");
1122 count
= read(fileno(fPtr
), buf
, MAX_CMD_SIZE
);
1125 for (i
= 0, *argc
= 0; i
< count
; i
++) {
1134 *argv
= (char **)wmalloc(sizeof(char *) * (*argc
));
1136 for (i
= 0, j
= 1; i
< count
; i
++) {
1139 if (i
< count
- 1) {
1140 (*argv
)[j
++] = &buf
[i
+ 1];
1155 static char *getCommandForWindow(Window win
, int elements
)
1157 char **argv
, *command
= NULL
;
1160 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
1161 if (argc
> 0 && argv
!= NULL
) {
1164 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
1165 if (command
[0] == 0) {
1171 XFreeStringList(argv
);
1178 /* Free result when done */
1179 char *GetCommandForWindow(Window win
)
1181 return getCommandForWindow(win
, 0);
1184 /* Free result when done */
1185 char *GetProgramNameForWindow(Window win
)
1187 return getCommandForWindow(win
, 1);