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
;
54 static void putdef(char *line
, char *name
, char *value
)
57 wwarning(_("could not define value for %s for cpp"), name
);
64 static void putidef(char *line
, char *name
, int value
)
67 snprintf(tmp
, sizeof(tmp
), "%i", value
);
72 static char *username(void)
80 user
= getpwuid(getuid());
82 werror(_("could not get password entry for UID %i"), getuid());
94 char *MakeCPPArgs(char *path
)
96 char buffer
[MAXLINE
], *buf
, *line
;
100 line
= wmalloc(MAXLINE
);
102 if ((buf
= getenv("HOSTNAME")) != NULL
) {
104 wwarning(_("your machine is misconfigured. HOSTNAME is set to %s"), buf
);
106 putdef(line
, " -DHOST=", buf
);
107 } else if ((buf
= getenv("HOST")) != NULL
) {
109 wwarning(_("your machine is misconfigured. HOST is set to %s"), buf
);
111 putdef(line
, " -DHOST=", buf
);
115 putdef(line
, " -DUSER=", buf
);
116 putidef(line
, " -DUID=", getuid());
117 buf
= XDisplayName(DisplayString(dpy
));
118 putdef(line
, " -DDISPLAY=", buf
);
119 putdef(line
, " -DWM_VERSION=", VERSION
);
121 visual
= DefaultVisual(dpy
, DefaultScreen(dpy
));
122 putidef(line
, " -DVISUAL=", visual
->class);
124 putidef(line
, " -DDEPTH=", DefaultDepth(dpy
, DefaultScreen(dpy
)));
126 putidef(line
, " -DSCR_WIDTH=", WidthOfScreen(DefaultScreenOfDisplay(dpy
)));
127 putidef(line
, " -DSCR_HEIGHT=", HeightOfScreen(DefaultScreenOfDisplay(dpy
)));
129 /* put the dir where the menu is being read from to the
133 buf
= strchr(tmp
+ 1, ' ');
137 buf
= strrchr(tmp
, '/');
139 *buf
= 0; /* trunc filename */
140 putdef(line
, " -I", tmp
);
145 /* this should be done just once, but it works this way */
146 strcpy(buffer
, DEF_CONFIG_PATHS
);
147 buf
= strtok(buffer
, ":");
150 char fullpath
[MAXLINE
];
153 strcpy(fullpath
, buf
);
155 /* home is statically allocated. Don't free it! */
156 char *home
= wgethomedir();
158 strcpy(fullpath
, home
);
159 strcat(fullpath
, &(buf
[1]));
162 putdef(line
, " -I", fullpath
);
164 } while ((buf
= strtok(NULL
, ":")) != NULL
);
171 /* XFetchName Wrapper */
172 Bool
wFetchName(Display
*dpy
, Window win
, char **winname
)
174 XTextProperty text_prop
;
178 if (XGetWMName(dpy
, win
, &text_prop
)) {
179 if (text_prop
.value
&& text_prop
.nitems
> 0) {
180 if (text_prop
.encoding
== XA_STRING
) {
181 *winname
= wstrdup((char *)text_prop
.value
);
182 XFree(text_prop
.value
);
184 text_prop
.nitems
= strlen((char *)text_prop
.value
);
185 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >=
186 Success
&& num
> 0 && *list
) {
187 XFree(text_prop
.value
);
188 *winname
= wstrdup(*list
);
189 XFreeStringList(list
);
191 *winname
= wstrdup((char *)text_prop
.value
);
192 XFree(text_prop
.value
);
196 /* the title is set, but it was set to none */
197 *winname
= wstrdup("");
201 /* the hint is probably not set */
208 /* XGetIconName Wrapper */
209 Bool
wGetIconName(Display
*dpy
, Window win
, char **iconname
)
211 XTextProperty text_prop
;
215 if (XGetWMIconName(dpy
, win
, &text_prop
) != 0 && text_prop
.value
&& text_prop
.nitems
> 0) {
216 if (text_prop
.encoding
== XA_STRING
)
217 *iconname
= (char *)text_prop
.value
;
219 text_prop
.nitems
= strlen((char *)text_prop
.value
);
220 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >= Success
&& num
> 0 && *list
) {
221 XFree(text_prop
.value
);
222 *iconname
= wstrdup(*list
);
223 XFreeStringList(list
);
225 *iconname
= (char *)text_prop
.value
;
233 static void eatExpose(void)
237 /* compress all expose events into a single one */
239 if (XCheckMaskEvent(dpy
, ExposureMask
, &event
)) {
240 /* ignore other exposure events for this window */
241 while (XCheckWindowEvent(dpy
, event
.xexpose
.window
, ExposureMask
, &foo
)) ;
242 /* eat exposes for other windows */
245 event
.xexpose
.count
= 0;
246 XPutBackEvent(dpy
, &event
);
250 void SlideWindow(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
252 time_t time0
= time(NULL
);
253 float dx
, dy
, x
= from_x
, y
= from_y
, sx
, sy
, px
, py
;
254 int dx_is_bigger
= 0;
255 int slide_delay
, slide_steps
, slide_slowdown
;
257 /* animation parameters */
263 {ICON_SLIDE_DELAY_UF
, ICON_SLIDE_STEPS_UF
, ICON_SLIDE_SLOWDOWN_UF
},
264 {ICON_SLIDE_DELAY_F
, ICON_SLIDE_STEPS_F
, ICON_SLIDE_SLOWDOWN_F
},
265 {ICON_SLIDE_DELAY_M
, ICON_SLIDE_STEPS_M
, ICON_SLIDE_SLOWDOWN_M
},
266 {ICON_SLIDE_DELAY_S
, ICON_SLIDE_STEPS_S
, ICON_SLIDE_SLOWDOWN_S
},
267 {ICON_SLIDE_DELAY_US
, ICON_SLIDE_STEPS_US
, ICON_SLIDE_SLOWDOWN_US
}
270 slide_slowdown
= apars
[(int)wPreferences
.icon_slide_speed
].slowdown
;
271 slide_steps
= apars
[(int)wPreferences
.icon_slide_speed
].steps
;
272 slide_delay
= apars
[(int)wPreferences
.icon_slide_speed
].delay
;
274 dx
= (float)(to_x
- from_x
);
275 dy
= (float)(to_y
- from_y
);
276 sx
= (dx
== 0 ? 0 : fabs(dx
) / dx
);
277 sy
= (dy
== 0 ? 0 : fabs(dy
) / dy
);
279 if (fabs(dx
) > fabs(dy
)) {
284 px
= dx
/ slide_slowdown
;
285 if (px
< slide_steps
&& px
> 0)
287 else if (px
> -slide_steps
&& px
< 0)
289 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
291 py
= dy
/ slide_slowdown
;
292 if (py
< slide_steps
&& py
> 0)
294 else if (py
> -slide_steps
&& py
< 0)
296 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
299 while (x
!= to_x
|| y
!= to_y
) {
302 if ((px
< 0 && (int)x
< to_x
) || (px
> 0 && (int)x
> to_x
))
304 if ((py
< 0 && (int)y
< to_y
) || (py
> 0 && (int)y
> to_y
))
308 px
= px
* (1.0 - 1 / (float)slide_slowdown
);
309 if (px
< slide_steps
&& px
> 0)
311 else if (px
> -slide_steps
&& px
< 0)
313 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
315 py
= py
* (1.0 - 1 / (float)slide_slowdown
);
316 if (py
< slide_steps
&& py
> 0)
318 else if (py
> -slide_steps
&& py
< 0)
320 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
323 XMoveWindow(dpy
, win
, (int)x
, (int)y
);
325 if (slide_delay
> 0) {
326 wusleep(slide_delay
* 1000L);
330 if (time(NULL
) - time0
> MAX_ANIMATION_TIME
)
333 XMoveWindow(dpy
, win
, to_x
, to_y
);
336 /* compress expose events */
340 char *ShrinkString(WMFont
* font
, char *string
, int width
)
349 w
= WMWidthOfString(font
, string
, p
);
350 text
= wmalloc(strlen(string
) + 8);
351 strcpy(text
, string
);
355 pos
= strchr(text
, ' ');
357 pos
= strchr(text
, ':');
362 w1
= WMWidthOfString(font
, text
, p
);
379 width
-= WMWidthOfString(font
, "...", 3);
384 while (p2
> p1
&& p1
!= t
) {
385 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
388 t
= p1
+ (p2
- p1
) / 2;
389 } else if (w
< width
) {
391 t
= p1
+ (p2
- p1
) / 2;
395 strcat(text
, &string
[p
- p1
]);
400 char *FindImage(char *paths
, char *file
)
402 char *tmp
, *path
= NULL
;
404 tmp
= strrchr(file
, ':');
407 path
= wfindfile(paths
, file
);
411 path
= wfindfile(paths
, file
);
417 static void timeoutHandler(void *data
)
422 static char *getTextSelection(WScreen
* screen
, Atom selection
)
456 data
= XFetchBuffer(dpy
, &size
, buffer
);
463 unsigned long len
, bytes
;
467 static Atom clipboard
= 0;
470 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
472 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
474 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
476 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
478 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
481 WMDeleteTimerHandler(timer
);
483 wwarning("selection retrieval timed out");
487 /* nobody owns the selection or the current owner has
488 * nothing to do with what we need */
489 if (ev
.xselection
.property
== None
) {
493 if (XGetWindowProperty(dpy
, screen
->info_window
,
495 False
, XA_STRING
, &rtype
, &bits
, &len
,
496 &bytes
, (unsigned char **)&data
) != Success
) {
499 if (rtype
!= XA_STRING
|| bits
!= 8) {
500 wwarning("invalid data in text selection");
509 static char *getselection(WScreen
* scr
)
513 tmp
= getTextSelection(scr
, XA_PRIMARY
);
515 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
520 parseuserinputpart(char *line
, int *ptr
, char *endchars
)
522 int depth
= 0, begin
;
526 while(line
[*ptr
] != '\0') {
527 if(line
[*ptr
] == '(') {
529 } else if(depth
> 0 && line
[*ptr
] == ')') {
531 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
532 value
= wmalloc(*ptr
- begin
+ 1);
533 strncpy(value
, line
+ begin
, *ptr
- begin
);
534 value
[*ptr
- begin
] = '\0';
544 getuserinput(WScreen
*scr
, char *line
, int *ptr
, Bool advanced
)
546 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
549 if(line
[*ptr
] == '(')
550 title
= parseuserinputpart(line
, ptr
, ",)");
551 if(title
!= NULL
&& line
[*ptr
] == ',')
552 prompt
= parseuserinputpart(line
, ptr
, ",)");
553 if(prompt
!= NULL
&& line
[*ptr
] == ',')
554 name
= parseuserinputpart(line
, ptr
, ")");
557 rv
= wAdvancedInputDialog(scr
,
558 title
? _(title
):_("Program Arguments"),
559 prompt
? _(prompt
):_("Enter command arguments:"),
562 rv
= wInputDialog(scr
,
563 title
? _(title
):_("Program Arguments"),
564 prompt
? _(prompt
):_("Enter command arguments:"),
567 if(title
) wfree(title
);
568 if(prompt
) wfree(prompt
);
569 if(name
) wfree(name
);
571 return rv
? ret
: NULL
;
579 * state input new-state output
580 * NORMAL % OPTION <nil>
581 * NORMAL \ ESCAPE <nil>
582 * NORMAL etc. NORMAL <input>
583 * ESCAPE any NORMAL <input>
584 * OPTION s NORMAL <selection buffer>
585 * OPTION w NORMAL <selected window id>
586 * OPTION a NORMAL <input text>
587 * OPTION d NORMAL <OffiX DND selection object>
588 * OPTION W NORMAL <current workspace>
589 * OPTION etc. NORMAL %<input>
591 #define TMPBUFSIZE 64
592 char *ExpandOptions(WScreen
* scr
, char *cmdline
)
594 int ptr
, optr
, state
, len
, olen
;
596 char *selection
= NULL
;
597 char *user_input
= NULL
;
599 char *dropped_thing
= NULL
;
601 char tmpbuf
[TMPBUFSIZE
];
604 len
= strlen(cmdline
);
608 wwarning(_("out of memory during expansion of \"%s\""), cmdline
);
612 ptr
= 0; /* input line pointer */
613 optr
= 0; /* output line pointer */
618 switch (cmdline
[ptr
]) {
627 out
[optr
++] = cmdline
[ptr
];
632 switch (cmdline
[ptr
]) {
646 out
[optr
++] = cmdline
[ptr
];
652 switch (cmdline
[ptr
]) {
654 if (scr
->focused_window
&& scr
->focused_window
->flags
.focused
) {
655 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
656 (unsigned int)scr
->focused_window
->client_win
);
657 slen
= strlen(tmpbuf
);
659 nout
= realloc(out
, olen
);
661 wwarning(_("out of memory during expansion of \"%%w\""));
673 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x", (unsigned int)scr
->current_workspace
+ 1);
674 slen
= strlen(tmpbuf
);
676 nout
= realloc(out
, olen
);
678 wwarning(_("out of memory during expansion of \"%%W\""));
689 user_input
= getuserinput(scr
, cmdline
, &ptr
, cmdline
[ptr
-1] == 'A');
691 slen
= strlen(user_input
);
693 nout
= realloc(out
, olen
);
695 wwarning(_("out of memory during expansion of \"%%a\""));
699 strcat(out
, user_input
);
702 /* Not an error, but user has Canceled the dialog box.
703 * This will make the command to not be performed. */
710 if (scr
->xdestring
) {
711 dropped_thing
= wstrdup(scr
->xdestring
);
713 if (!dropped_thing
) {
714 dropped_thing
= get_dnd_selection(scr
);
716 if (!dropped_thing
) {
717 scr
->flags
.dnd_data_convertion_status
= 1;
720 slen
= strlen(dropped_thing
);
722 nout
= realloc(out
, olen
);
724 wwarning(_("out of memory during expansion of \"%%d\""));
728 strcat(out
, dropped_thing
);
735 selection
= getselection(scr
);
738 wwarning(_("selection not available"));
741 slen
= strlen(selection
);
743 nout
= realloc(out
, olen
);
745 wwarning(_("out of memory during expansion of \"%%s\""));
749 strcat(out
, selection
);
755 out
[optr
++] = cmdline
[ptr
];
773 void ParseWindowName(WMPropList
* value
, char **winstance
, char **wclass
, char *where
)
777 *winstance
= *wclass
= NULL
;
779 if (!WMIsPLString(value
)) {
780 wwarning(_("bad window name value in %s state info"), where
);
784 name
= WMGetFromPLString(value
);
785 if (!name
|| strlen(name
) == 0) {
786 wwarning(_("bad window name value in %s state info"), where
);
790 UnescapeWM_CLASS(name
, winstance
, wclass
);
794 static char *keysymToString(KeySym keysym
, unsigned int state
)
797 char *buf
= wmalloc(20);
802 kev
.send_event
= False
;
803 kev
.window
= DefaultRootWindow(dpy
);
804 kev
.root
= DefaultRootWindow(dpy
);
805 kev
.same_screen
= True
;
806 kev
.subwindow
= kev
.root
;
807 kev
.serial
= 0x12344321;
808 kev
.time
= CurrentTime
;
810 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
811 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
818 char *GetShortcutString(char *text
)
827 tmp
= text
= wstrdup(text
);
830 while ((k
= strchr(text
, '+')) != NULL
) {
834 mod
= wXModifierFromKey(text
);
836 return wstrdup("bug");
841 if (strcasecmp(text
, "Meta") == 0) {
842 buffer
= wstrappend(buffer
, "M+");
843 } else if (strcasecmp(text
, "Alt") == 0) {
844 buffer
= wstrappend(buffer
, "A+");
845 } else if (strcasecmp(text
, "Shift") == 0) {
846 buffer
= wstrappend(buffer
, "Sh+");
847 } else if (strcasecmp(text
, "Mod1") == 0) {
848 buffer
= wstrappend(buffer
, "M1+");
849 } else if (strcasecmp(text
, "Mod2") == 0) {
850 buffer
= wstrappend(buffer
, "M2+");
851 } else if (strcasecmp(text
, "Mod3") == 0) {
852 buffer
= wstrappend(buffer
, "M3+");
853 } else if (strcasecmp(text
, "Mod4") == 0) {
854 buffer
= wstrappend(buffer
, "M4+");
855 } else if (strcasecmp(text
, "Mod5") == 0) {
856 buffer
= wstrappend(buffer
, "M5+");
857 } else if (strcasecmp(text
, "Control") == 0) {
860 buffer
= wstrappend(buffer
, text
);
866 buffer
= wstrappend(buffer
, "^");
868 buffer
= wstrappend(buffer
, text
);
871 /* ksym = XStringToKeysym(text);
872 tmp = keysymToString(ksym, modmask);
874 buffer = wstrappend(buffer, tmp);
881 char *EscapeWM_CLASS(char *name
, char *class)
884 char *ename
= NULL
, *eclass
= NULL
;
892 ename
= wmalloc(l
* 2 + 1);
894 for (i
= 0; i
< l
; i
++) {
895 if (name
[i
] == '\\') {
897 } else if (name
[i
] == '.') {
900 ename
[j
++] = name
[i
];
906 eclass
= wmalloc(l
* 2 + 1);
908 for (i
= 0; i
< l
; i
++) {
909 if (class[i
] == '\\') {
911 } else if (class[i
] == '.') {
914 eclass
[j
++] = class[i
];
919 if (ename
&& eclass
) {
920 int len
= strlen(ename
) + strlen(eclass
) + 4;
922 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
926 ret
= wstrdup(ename
);
929 ret
= wstrdup(eclass
);
936 void UnescapeWM_CLASS(char *str
, char **name
, char **class)
946 /* separate string in 2 parts */
948 for (i
= 0; i
< j
; i
++) {
949 if (str
[i
] == '\\') {
952 } else if (str
[i
] == '.') {
958 /* unescape strings */
959 for (i
= 0, k
= 0; i
< dot
; i
++) {
960 if (str
[i
] == '\\') {
963 (*name
)[k
++] = str
[i
];
968 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
969 if (str
[i
] == '\\') {
972 (*class)[k
++] = str
[i
];
987 void SendHelperMessage(WScreen
* scr
, char type
, int workspace
, char *msg
)
994 if (!scr
->flags
.backimage_helper_launched
) {
998 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
999 buffer
= wmalloc(len
+ 5);
1000 snprintf(buf
, sizeof(buf
), "%4i", len
);
1001 memcpy(buffer
, buf
, 4);
1004 if (workspace
>= 0) {
1005 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
1006 memcpy(&buffer
[i
], buf
, 4);
1011 strcpy(&buffer
[i
], msg
);
1013 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
1014 werror(_("could not send message to background image helper"));
1019 Bool
UpdateDomainFile(WDDomain
* domain
)
1022 char path
[PATH_MAX
];
1023 WMPropList
*shared_dict
, *dict
;
1024 Bool result
, freeDict
= False
;
1026 dict
= domain
->dictionary
;
1027 if (WMIsPLDictionary(domain
->dictionary
)) {
1028 /* retrieve global system dictionary */
1029 snprintf(path
, sizeof(path
), "%s/WindowMaker/%s", SYSCONFDIR
, domain
->domain_name
);
1030 if (stat(path
, &stbuf
) >= 0) {
1031 shared_dict
= WMReadPropListFromFile(path
);
1033 if (WMIsPLDictionary(shared_dict
)) {
1035 dict
= WMDeepCopyPropList(domain
->dictionary
);
1036 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
1038 WMReleasePropList(shared_dict
);
1043 result
= WMWritePropListToFile(dict
, domain
->path
);
1046 WMReleasePropList(dict
);
1052 char *StrConcatDot(char *a
, char *b
)
1062 len
= strlen(a
) + strlen(b
) + 4;
1065 snprintf(str
, len
, "%s.%s", a
, b
);
1070 static char *getCommandForWindow(Window win
, int elements
)
1072 char **argv
, *command
= NULL
;
1075 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
1076 if (argc
> 0 && argv
!= NULL
) {
1079 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
1080 if (command
[0] == 0) {
1086 XFreeStringList(argv
);
1093 /* Free result when done */
1094 char *GetCommandForWindow(Window win
)
1096 return getCommandForWindow(win
, 0);
1099 /* Free result when done */
1100 char *GetProgramNameForWindow(Window win
)
1102 return getCommandForWindow(win
, 1);