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>
35 #include <WINGs/WUtil.h>
38 #include "WindowMaker.h"
47 #include "xmodifier.h"
49 /**** global variables *****/
50 extern WPreferences wPreferences
;
53 static void putdef(char *line
, char *name
, char *value
)
56 wwarning(_("could not define value for %s for cpp"), name
);
63 static void putidef(char *line
, char *name
, int value
)
66 snprintf(tmp
, sizeof(tmp
), "%i", value
);
71 static char *username(void)
79 user
= getpwuid(getuid());
81 werror(_("could not get password entry for UID %i"), getuid());
93 char *MakeCPPArgs(char *path
)
96 char buffer
[MAXLINE
], *buf
, *line
;
100 line
= wmalloc(MAXLINE
);
103 if ((buf
= getenv("HOSTNAME")) != NULL
) {
105 wwarning(_("your machine is misconfigured. HOSTNAME is set to %s"), buf
);
107 putdef(line
, " -DHOST=", buf
);
108 } else if ((buf
= getenv("HOST")) != NULL
) {
110 wwarning(_("your machine is misconfigured. HOST is set to %s"), buf
);
112 putdef(line
, " -DHOST=", buf
);
116 putdef(line
, " -DUSER=", buf
);
117 putidef(line
, " -DUID=", getuid());
118 buf
= XDisplayName(DisplayString(dpy
));
119 putdef(line
, " -DDISPLAY=", buf
);
120 putdef(line
, " -DWM_VERSION=", VERSION
);
122 visual
= DefaultVisual(dpy
, DefaultScreen(dpy
));
123 putidef(line
, " -DVISUAL=", visual
->class);
125 putidef(line
, " -DDEPTH=", DefaultDepth(dpy
, DefaultScreen(dpy
)));
127 putidef(line
, " -DSCR_WIDTH=", WidthOfScreen(DefaultScreenOfDisplay(dpy
)));
128 putidef(line
, " -DSCR_HEIGHT=", HeightOfScreen(DefaultScreenOfDisplay(dpy
)));
130 /* put the dir where the menu is being read from to the
134 buf
= strchr(tmp
+ 1, ' ');
138 buf
= strrchr(tmp
, '/');
140 *buf
= 0; /* trunc filename */
141 putdef(line
, " -I", tmp
);
146 /* this should be done just once, but it works this way */
147 strcpy(buffer
, DEF_CONFIG_PATHS
);
148 buf
= strtok(buffer
, ":");
151 char fullpath
[MAXLINE
];
154 strcpy(fullpath
, buf
);
156 /* home is statically allocated. Don't free it! */
157 char *home
= wgethomedir();
159 strcpy(fullpath
, home
);
160 strcat(fullpath
, &(buf
[1]));
163 putdef(line
, " -I", fullpath
);
165 } while ((buf
= strtok(NULL
, ":")) != NULL
);
172 /* XFetchName Wrapper */
173 Bool
wFetchName(Display
*dpy
, Window win
, char **winname
)
175 XTextProperty text_prop
;
179 if (XGetWMName(dpy
, win
, &text_prop
)) {
180 if (text_prop
.value
&& text_prop
.nitems
> 0) {
181 if (text_prop
.encoding
== XA_STRING
) {
182 *winname
= wstrdup((char *)text_prop
.value
);
183 XFree(text_prop
.value
);
185 text_prop
.nitems
= strlen((char *)text_prop
.value
);
186 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >=
187 Success
&& num
> 0 && *list
) {
188 XFree(text_prop
.value
);
189 *winname
= wstrdup(*list
);
190 XFreeStringList(list
);
192 *winname
= wstrdup((char *)text_prop
.value
);
193 XFree(text_prop
.value
);
197 /* the title is set, but it was set to none */
198 *winname
= wstrdup("");
202 /* the hint is probably not set */
209 /* XGetIconName Wrapper */
210 Bool
wGetIconName(Display
*dpy
, Window win
, char **iconname
)
212 XTextProperty text_prop
;
216 if (XGetWMIconName(dpy
, win
, &text_prop
) != 0 && text_prop
.value
&& text_prop
.nitems
> 0) {
217 if (text_prop
.encoding
== XA_STRING
)
218 *iconname
= (char *)text_prop
.value
;
220 text_prop
.nitems
= strlen((char *)text_prop
.value
);
221 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >= Success
&& num
> 0 && *list
) {
222 XFree(text_prop
.value
);
223 *iconname
= wstrdup(*list
);
224 XFreeStringList(list
);
226 *iconname
= (char *)text_prop
.value
;
234 static void eatExpose(void)
238 /* compress all expose events into a single one */
240 if (XCheckMaskEvent(dpy
, ExposureMask
, &event
)) {
241 /* ignore other exposure events for this window */
242 while (XCheckWindowEvent(dpy
, event
.xexpose
.window
, ExposureMask
, &foo
)) ;
243 /* eat exposes for other windows */
246 event
.xexpose
.count
= 0;
247 XPutBackEvent(dpy
, &event
);
251 void SlideWindow(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
253 time_t time0
= time(NULL
);
254 float dx
, dy
, x
= from_x
, y
= from_y
, sx
, sy
, px
, py
;
255 int dx_is_bigger
= 0;
256 int slide_delay
, slide_steps
, slide_slowdown
;
258 /* animation parameters */
264 {ICON_SLIDE_DELAY_UF
, ICON_SLIDE_STEPS_UF
, ICON_SLIDE_SLOWDOWN_UF
},
265 {ICON_SLIDE_DELAY_F
, ICON_SLIDE_STEPS_F
, ICON_SLIDE_SLOWDOWN_F
},
266 {ICON_SLIDE_DELAY_M
, ICON_SLIDE_STEPS_M
, ICON_SLIDE_SLOWDOWN_M
},
267 {ICON_SLIDE_DELAY_S
, ICON_SLIDE_STEPS_S
, ICON_SLIDE_SLOWDOWN_S
},
268 {ICON_SLIDE_DELAY_US
, ICON_SLIDE_STEPS_US
, ICON_SLIDE_SLOWDOWN_US
}
271 slide_slowdown
= apars
[(int)wPreferences
.icon_slide_speed
].slowdown
;
272 slide_steps
= apars
[(int)wPreferences
.icon_slide_speed
].steps
;
273 slide_delay
= apars
[(int)wPreferences
.icon_slide_speed
].delay
;
275 dx
= (float)(to_x
- from_x
);
276 dy
= (float)(to_y
- from_y
);
277 sx
= (dx
== 0 ? 0 : fabs(dx
) / dx
);
278 sy
= (dy
== 0 ? 0 : fabs(dy
) / dy
);
280 if (fabs(dx
) > fabs(dy
)) {
285 px
= dx
/ slide_slowdown
;
286 if (px
< slide_steps
&& px
> 0)
288 else if (px
> -slide_steps
&& px
< 0)
290 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
292 py
= dy
/ slide_slowdown
;
293 if (py
< slide_steps
&& py
> 0)
295 else if (py
> -slide_steps
&& py
< 0)
297 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
300 while (x
!= to_x
|| y
!= to_y
) {
303 if ((px
< 0 && (int)x
< to_x
) || (px
> 0 && (int)x
> to_x
))
305 if ((py
< 0 && (int)y
< to_y
) || (py
> 0 && (int)y
> to_y
))
309 px
= px
* (1.0 - 1 / (float)slide_slowdown
);
310 if (px
< slide_steps
&& px
> 0)
312 else if (px
> -slide_steps
&& px
< 0)
314 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
316 py
= py
* (1.0 - 1 / (float)slide_slowdown
);
317 if (py
< slide_steps
&& py
> 0)
319 else if (py
> -slide_steps
&& py
< 0)
321 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
324 XMoveWindow(dpy
, win
, (int)x
, (int)y
);
326 if (slide_delay
> 0) {
327 wusleep(slide_delay
* 1000L);
331 if (time(NULL
) - time0
> MAX_ANIMATION_TIME
)
334 XMoveWindow(dpy
, win
, to_x
, to_y
);
337 /* compress expose events */
341 char *ShrinkString(WMFont
* font
, char *string
, int width
)
350 w
= WMWidthOfString(font
, string
, p
);
351 text
= wmalloc(strlen(string
) + 8);
352 strcpy(text
, string
);
356 pos
= strchr(text
, ' ');
358 pos
= strchr(text
, ':');
363 w1
= WMWidthOfString(font
, text
, p
);
380 width
-= WMWidthOfString(font
, "...", 3);
385 while (p2
> p1
&& p1
!= t
) {
386 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
389 t
= p1
+ (p2
- p1
) / 2;
390 } else if (w
< width
) {
392 t
= p1
+ (p2
- p1
) / 2;
396 strcat(text
, &string
[p
- p1
]);
401 char *FindImage(char *paths
, char *file
)
403 char *tmp
, *path
= NULL
;
405 tmp
= strrchr(file
, ':');
408 path
= wfindfile(paths
, file
);
412 path
= wfindfile(paths
, file
);
418 static void timeoutHandler(void *data
)
423 static char *getTextSelection(WScreen
* screen
, Atom selection
)
457 data
= XFetchBuffer(dpy
, &size
, buffer
);
464 unsigned long len
, bytes
;
468 static Atom clipboard
= 0;
471 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
473 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
475 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
477 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
479 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
482 WMDeleteTimerHandler(timer
);
484 wwarning("selection retrieval timed out");
488 /* nobody owns the selection or the current owner has
489 * nothing to do with what we need */
490 if (ev
.xselection
.property
== None
) {
494 if (XGetWindowProperty(dpy
, screen
->info_window
,
496 False
, XA_STRING
, &rtype
, &bits
, &len
,
497 &bytes
, (unsigned char **)&data
) != Success
) {
500 if (rtype
!= XA_STRING
|| bits
!= 8) {
501 wwarning("invalid data in text selection");
510 static char *getselection(WScreen
* scr
)
514 tmp
= getTextSelection(scr
, XA_PRIMARY
);
516 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
521 parseuserinputpart(char *line
, int *ptr
, char *endchars
)
523 int depth
= 0, begin
;
527 while(line
[*ptr
] != '\0') {
528 if(line
[*ptr
] == '(') {
530 } else if(depth
> 0 && line
[*ptr
] == ')') {
532 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
533 value
= wmalloc(*ptr
- begin
+ 1);
534 strncpy(value
, line
+ begin
, *ptr
- begin
);
535 value
[*ptr
- begin
] = '\0';
545 getuserinput(WScreen
*scr
, char *line
, int *ptr
, Bool advanced
)
547 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
550 if(line
[*ptr
] == '(')
551 title
= parseuserinputpart(line
, ptr
, ",)");
552 if(title
!= NULL
&& line
[*ptr
] == ',')
553 prompt
= parseuserinputpart(line
, ptr
, ",)");
554 if(prompt
!= NULL
&& line
[*ptr
] == ',')
555 name
= parseuserinputpart(line
, ptr
, ")");
558 rv
= wAdvancedInputDialog(scr
,
559 title
? _(title
):_("Program Arguments"),
560 prompt
? _(prompt
):_("Enter command arguments:"),
563 rv
= wInputDialog(scr
,
564 title
? _(title
):_("Program Arguments"),
565 prompt
? _(prompt
):_("Enter command arguments:"),
568 if(title
) wfree(title
);
569 if(prompt
) wfree(prompt
);
570 if(name
) wfree(name
);
572 return rv
? ret
: NULL
;
580 * state input new-state output
581 * NORMAL % OPTION <nil>
582 * NORMAL \ ESCAPE <nil>
583 * NORMAL etc. NORMAL <input>
584 * ESCAPE any NORMAL <input>
585 * OPTION s NORMAL <selection buffer>
586 * OPTION w NORMAL <selected window id>
587 * OPTION a NORMAL <input text>
588 * OPTION d NORMAL <OffiX DND selection object>
589 * OPTION W NORMAL <current workspace>
590 * OPTION etc. NORMAL %<input>
592 #define TMPBUFSIZE 64
593 char *ExpandOptions(WScreen
* scr
, char *cmdline
)
595 int ptr
, optr
, state
, len
, olen
;
597 char *selection
= NULL
;
598 char *user_input
= NULL
;
600 char *dropped_thing
= NULL
;
602 char tmpbuf
[TMPBUFSIZE
];
605 len
= strlen(cmdline
);
609 wwarning(_("out of memory during expansion of \"%s\""), cmdline
);
613 ptr
= 0; /* input line pointer */
614 optr
= 0; /* output line pointer */
619 switch (cmdline
[ptr
]) {
628 out
[optr
++] = cmdline
[ptr
];
633 switch (cmdline
[ptr
]) {
647 out
[optr
++] = cmdline
[ptr
];
653 switch (cmdline
[ptr
]) {
655 if (scr
->focused_window
&& scr
->focused_window
->flags
.focused
) {
656 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
657 (unsigned int)scr
->focused_window
->client_win
);
658 slen
= strlen(tmpbuf
);
660 nout
= realloc(out
, olen
);
662 wwarning(_("out of memory during expansion of \"%%w\""));
674 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x", (unsigned int)scr
->current_workspace
+ 1);
675 slen
= strlen(tmpbuf
);
677 nout
= realloc(out
, olen
);
679 wwarning(_("out of memory during expansion of \"%%W\""));
690 user_input
= getuserinput(scr
, cmdline
, &ptr
, cmdline
[ptr
-1] == 'A');
692 slen
= strlen(user_input
);
694 nout
= realloc(out
, olen
);
696 wwarning(_("out of memory during expansion of \"%%a\""));
700 strcat(out
, user_input
);
703 /* Not an error, but user has Canceled the dialog box.
704 * This will make the command to not be performed. */
711 if (scr
->xdestring
) {
712 dropped_thing
= wstrdup(scr
->xdestring
);
714 if (!dropped_thing
) {
715 dropped_thing
= get_dnd_selection(scr
);
717 if (!dropped_thing
) {
718 scr
->flags
.dnd_data_convertion_status
= 1;
721 slen
= strlen(dropped_thing
);
723 nout
= realloc(out
, olen
);
725 wwarning(_("out of memory during expansion of \"%%d\""));
729 strcat(out
, dropped_thing
);
736 selection
= getselection(scr
);
739 wwarning(_("selection not available"));
742 slen
= strlen(selection
);
744 nout
= realloc(out
, olen
);
746 wwarning(_("out of memory during expansion of \"%%s\""));
750 strcat(out
, selection
);
756 out
[optr
++] = cmdline
[ptr
];
774 void ParseWindowName(WMPropList
* value
, char **winstance
, char **wclass
, char *where
)
778 *winstance
= *wclass
= NULL
;
780 if (!WMIsPLString(value
)) {
781 wwarning(_("bad window name value in %s state info"), where
);
785 name
= WMGetFromPLString(value
);
786 if (!name
|| strlen(name
) == 0) {
787 wwarning(_("bad window name value in %s state info"), where
);
791 UnescapeWM_CLASS(name
, winstance
, wclass
);
795 static char *keysymToString(KeySym keysym
, unsigned int state
)
798 char *buf
= wmalloc(20);
803 kev
.send_event
= False
;
804 kev
.window
= DefaultRootWindow(dpy
);
805 kev
.root
= DefaultRootWindow(dpy
);
806 kev
.same_screen
= True
;
807 kev
.subwindow
= kev
.root
;
808 kev
.serial
= 0x12344321;
809 kev
.time
= CurrentTime
;
811 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
812 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
819 char *GetShortcutString(char *text
)
828 tmp
= text
= wstrdup(text
);
831 while ((k
= strchr(text
, '+')) != NULL
) {
835 mod
= wXModifierFromKey(text
);
837 return wstrdup("bug");
842 if (strcasecmp(text
, "Meta") == 0) {
843 buffer
= wstrappend(buffer
, "M+");
844 } else if (strcasecmp(text
, "Alt") == 0) {
845 buffer
= wstrappend(buffer
, "A+");
846 } else if (strcasecmp(text
, "Shift") == 0) {
847 buffer
= wstrappend(buffer
, "Sh+");
848 } else if (strcasecmp(text
, "Mod1") == 0) {
849 buffer
= wstrappend(buffer
, "M1+");
850 } else if (strcasecmp(text
, "Mod2") == 0) {
851 buffer
= wstrappend(buffer
, "M2+");
852 } else if (strcasecmp(text
, "Mod3") == 0) {
853 buffer
= wstrappend(buffer
, "M3+");
854 } else if (strcasecmp(text
, "Mod4") == 0) {
855 buffer
= wstrappend(buffer
, "M4+");
856 } else if (strcasecmp(text
, "Mod5") == 0) {
857 buffer
= wstrappend(buffer
, "M5+");
858 } else if (strcasecmp(text
, "Control") == 0) {
861 buffer
= wstrappend(buffer
, text
);
867 buffer
= wstrappend(buffer
, "^");
869 buffer
= wstrappend(buffer
, text
);
872 /* ksym = XStringToKeysym(text);
873 tmp = keysymToString(ksym, modmask);
875 buffer = wstrappend(buffer, tmp);
882 char *EscapeWM_CLASS(char *name
, char *class)
885 char *ename
= NULL
, *eclass
= NULL
;
893 ename
= wmalloc(l
* 2 + 1);
895 for (i
= 0; i
< l
; i
++) {
896 if (name
[i
] == '\\') {
898 } else if (name
[i
] == '.') {
901 ename
[j
++] = name
[i
];
907 eclass
= wmalloc(l
* 2 + 1);
909 for (i
= 0; i
< l
; i
++) {
910 if (class[i
] == '\\') {
912 } else if (class[i
] == '.') {
915 eclass
[j
++] = class[i
];
920 if (ename
&& eclass
) {
921 int len
= strlen(ename
) + strlen(eclass
) + 4;
923 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
927 ret
= wstrdup(ename
);
930 ret
= wstrdup(eclass
);
937 void UnescapeWM_CLASS(char *str
, char **name
, char **class)
947 /* separate string in 2 parts */
949 for (i
= 0; i
< j
; i
++) {
950 if (str
[i
] == '\\') {
953 } else if (str
[i
] == '.') {
959 /* unescape strings */
960 for (i
= 0, k
= 0; i
< dot
; i
++) {
961 if (str
[i
] == '\\') {
964 (*name
)[k
++] = str
[i
];
969 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
970 if (str
[i
] == '\\') {
973 (*class)[k
++] = str
[i
];
988 void SendHelperMessage(WScreen
* scr
, char type
, int workspace
, char *msg
)
995 if (!scr
->flags
.backimage_helper_launched
) {
999 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
1000 buffer
= wmalloc(len
+ 5);
1001 snprintf(buf
, sizeof(buf
), "%4i", len
);
1002 memcpy(buffer
, buf
, 4);
1005 if (workspace
>= 0) {
1006 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
1007 memcpy(&buffer
[i
], buf
, 4);
1012 strcpy(&buffer
[i
], msg
);
1014 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
1015 werror(_("could not send message to background image helper"));
1020 Bool
UpdateDomainFile(WDDomain
* domain
)
1023 char path
[PATH_MAX
];
1024 WMPropList
*shared_dict
, *dict
;
1025 Bool result
, freeDict
= False
;
1027 dict
= domain
->dictionary
;
1028 if (WMIsPLDictionary(domain
->dictionary
)) {
1029 /* retrieve global system dictionary */
1030 snprintf(path
, sizeof(path
), "%s/WindowMaker/%s", SYSCONFDIR
, domain
->domain_name
);
1031 if (stat(path
, &stbuf
) >= 0) {
1032 shared_dict
= WMReadPropListFromFile(path
);
1034 if (WMIsPLDictionary(shared_dict
)) {
1036 dict
= WMDeepCopyPropList(domain
->dictionary
);
1037 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
1039 WMReleasePropList(shared_dict
);
1044 result
= WMWritePropListToFile(dict
, domain
->path
);
1047 WMReleasePropList(dict
);
1053 char *StrConcatDot(char *a
, char *b
)
1063 len
= strlen(a
) + strlen(b
) + 4;
1066 snprintf(str
, len
, "%s.%s", a
, b
);
1071 static char *getCommandForWindow(Window win
, int elements
)
1073 char **argv
, *command
= NULL
;
1076 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
1077 if (argc
> 0 && argv
!= NULL
) {
1080 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
1081 if (command
[0] == 0) {
1087 XFreeStringList(argv
);
1094 /* Free result when done */
1095 char *GetCommandForWindow(Window win
)
1097 return getCommandForWindow(win
, 0);
1100 /* Free result when done */
1101 char *GetProgramNameForWindow(Window win
)
1103 return getCommandForWindow(win
, 1);