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"
49 #include "xmodifier.h"
51 /**** global variables *****/
53 extern WPreferences wPreferences
;
55 extern Time LastTimestamp
;
58 static void putdef(char *line
, char *name
, char *value
)
61 wwarning(_("could not define value for %s for cpp"), name
);
68 static void putidef(char *line
, char *name
, int value
)
71 snprintf(tmp
, sizeof(tmp
), "%i", value
);
76 static char *username()
84 user
= getpwuid(getuid());
86 wsyserror(_("could not get password entry for UID %i"), getuid());
98 char *MakeCPPArgs(char *path
)
101 char buffer
[MAXLINE
], *buf
, *line
;
105 line
= wmalloc(MAXLINE
);
108 if ((buf
= getenv("HOSTNAME")) != NULL
) {
110 wwarning(_("your machine is misconfigured. HOSTNAME is set to %s"), buf
);
112 putdef(line
, " -DHOST=", buf
);
113 } else if ((buf
= getenv("HOST")) != NULL
) {
115 wwarning(_("your machine is misconfigured. HOST is set to %s"), buf
);
117 putdef(line
, " -DHOST=", buf
);
121 putdef(line
, " -DUSER=", buf
);
122 putidef(line
, " -DUID=", getuid());
123 buf
= XDisplayName(DisplayString(dpy
));
124 putdef(line
, " -DDISPLAY=", buf
);
125 putdef(line
, " -DWM_VERSION=", VERSION
);
127 visual
= DefaultVisual(dpy
, DefaultScreen(dpy
));
128 putidef(line
, " -DVISUAL=", visual
->class);
130 putidef(line
, " -DDEPTH=", DefaultDepth(dpy
, DefaultScreen(dpy
)));
132 putidef(line
, " -DSCR_WIDTH=", WidthOfScreen(DefaultScreenOfDisplay(dpy
)));
133 putidef(line
, " -DSCR_HEIGHT=", HeightOfScreen(DefaultScreenOfDisplay(dpy
)));
135 /* put the dir where the menu is being read from to the
139 buf
= strchr(tmp
+ 1, ' ');
143 buf
= strrchr(tmp
, '/');
145 *buf
= 0; /* trunc filename */
146 putdef(line
, " -I", tmp
);
151 /* this should be done just once, but it works this way */
152 strcpy(buffer
, DEF_CONFIG_PATHS
);
153 buf
= strtok(buffer
, ":");
156 char fullpath
[MAXLINE
];
159 strcpy(fullpath
, buf
);
161 /* home is statically allocated. Don't free it! */
162 char *home
= wgethomedir();
164 strcpy(fullpath
, home
);
165 strcat(fullpath
, &(buf
[1]));
168 putdef(line
, " -I", fullpath
);
170 } while ((buf
= strtok(NULL
, ":")) != NULL
);
177 /* XFetchName Wrapper */
178 Bool
wFetchName(Display
*dpy
, Window win
, char **winname
)
180 XTextProperty text_prop
;
184 if (XGetWMName(dpy
, win
, &text_prop
)) {
185 if (text_prop
.value
&& text_prop
.nitems
> 0) {
186 if (text_prop
.encoding
== XA_STRING
) {
187 *winname
= wstrdup((char *)text_prop
.value
);
188 XFree(text_prop
.value
);
190 text_prop
.nitems
= strlen((char *)text_prop
.value
);
191 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >=
192 Success
&& num
> 0 && *list
) {
193 XFree(text_prop
.value
);
194 *winname
= wstrdup(*list
);
195 XFreeStringList(list
);
197 *winname
= wstrdup((char *)text_prop
.value
);
198 XFree(text_prop
.value
);
202 /* the title is set, but it was set to none */
203 *winname
= wstrdup("");
207 /* the hint is probably not set */
214 /* XGetIconName Wrapper */
215 Bool
wGetIconName(Display
*dpy
, Window win
, char **iconname
)
217 XTextProperty text_prop
;
221 if (XGetWMIconName(dpy
, win
, &text_prop
) != 0 && text_prop
.value
&& text_prop
.nitems
> 0) {
222 if (text_prop
.encoding
== XA_STRING
)
223 *iconname
= (char *)text_prop
.value
;
225 text_prop
.nitems
= strlen((char *)text_prop
.value
);
226 if (XmbTextPropertyToTextList(dpy
, &text_prop
, &list
, &num
) >= Success
&& num
> 0 && *list
) {
227 XFree(text_prop
.value
);
228 *iconname
= wstrdup(*list
);
229 XFreeStringList(list
);
231 *iconname
= (char *)text_prop
.value
;
239 static void eatExpose()
243 /* compress all expose events into a single one */
245 if (XCheckMaskEvent(dpy
, ExposureMask
, &event
)) {
246 /* ignore other exposure events for this window */
247 while (XCheckWindowEvent(dpy
, event
.xexpose
.window
, ExposureMask
, &foo
)) ;
248 /* eat exposes for other windows */
251 event
.xexpose
.count
= 0;
252 XPutBackEvent(dpy
, &event
);
256 void SlideWindow(Window win
, int from_x
, int from_y
, int to_x
, int to_y
)
258 time_t time0
= time(NULL
);
259 float dx
, dy
, x
= from_x
, y
= from_y
, sx
, sy
, px
, py
;
260 int dx_is_bigger
= 0;
261 int slide_delay
, slide_steps
, slide_slowdown
;
263 /* animation parameters */
269 {ICON_SLIDE_DELAY_UF
, ICON_SLIDE_STEPS_UF
, ICON_SLIDE_SLOWDOWN_UF
},
270 {ICON_SLIDE_DELAY_F
, ICON_SLIDE_STEPS_F
, ICON_SLIDE_SLOWDOWN_F
},
271 {ICON_SLIDE_DELAY_M
, ICON_SLIDE_STEPS_M
, ICON_SLIDE_SLOWDOWN_M
},
272 {ICON_SLIDE_DELAY_S
, ICON_SLIDE_STEPS_S
, ICON_SLIDE_SLOWDOWN_S
},
273 {ICON_SLIDE_DELAY_US
, ICON_SLIDE_STEPS_US
, ICON_SLIDE_SLOWDOWN_US
}
276 slide_slowdown
= apars
[(int)wPreferences
.icon_slide_speed
].slowdown
;
277 slide_steps
= apars
[(int)wPreferences
.icon_slide_speed
].steps
;
278 slide_delay
= apars
[(int)wPreferences
.icon_slide_speed
].delay
;
280 dx
= (float)(to_x
- from_x
);
281 dy
= (float)(to_y
- from_y
);
282 sx
= (dx
== 0 ? 0 : fabs(dx
) / dx
);
283 sy
= (dy
== 0 ? 0 : fabs(dy
) / dy
);
285 if (fabs(dx
) > fabs(dy
)) {
290 px
= dx
/ slide_slowdown
;
291 if (px
< slide_steps
&& px
> 0)
293 else if (px
> -slide_steps
&& px
< 0)
295 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
297 py
= dy
/ slide_slowdown
;
298 if (py
< slide_steps
&& py
> 0)
300 else if (py
> -slide_steps
&& py
< 0)
302 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
305 while (x
!= to_x
|| y
!= to_y
) {
308 if ((px
< 0 && (int)x
< to_x
) || (px
> 0 && (int)x
> to_x
))
310 if ((py
< 0 && (int)y
< to_y
) || (py
> 0 && (int)y
> to_y
))
314 px
= px
* (1.0 - 1 / (float)slide_slowdown
);
315 if (px
< slide_steps
&& px
> 0)
317 else if (px
> -slide_steps
&& px
< 0)
319 py
= (sx
== 0 ? 0 : px
* dy
/ dx
);
321 py
= py
* (1.0 - 1 / (float)slide_slowdown
);
322 if (py
< slide_steps
&& py
> 0)
324 else if (py
> -slide_steps
&& py
< 0)
326 px
= (sy
== 0 ? 0 : py
* dx
/ dy
);
329 XMoveWindow(dpy
, win
, (int)x
, (int)y
);
331 if (slide_delay
> 0) {
332 wusleep(slide_delay
* 1000L);
336 if (time(NULL
) - time0
> MAX_ANIMATION_TIME
)
339 XMoveWindow(dpy
, win
, to_x
, to_y
);
342 /* compress expose events */
346 char *ShrinkString(WMFont
* font
, char *string
, int width
)
355 w
= WMWidthOfString(font
, string
, p
);
356 text
= wmalloc(strlen(string
) + 8);
357 strcpy(text
, string
);
361 pos
= strchr(text
, ' ');
363 pos
= strchr(text
, ':');
368 w1
= WMWidthOfString(font
, text
, p
);
385 width
-= WMWidthOfString(font
, "...", 3);
390 while (p2
> p1
&& p1
!= t
) {
391 w
= WMWidthOfString(font
, &string
[p
- t
], t
);
394 t
= p1
+ (p2
- p1
) / 2;
395 } else if (w
< width
) {
397 t
= p1
+ (p2
- p1
) / 2;
401 strcat(text
, &string
[p
- p1
]);
406 char *FindImage(char *paths
, char *file
)
410 tmp
= strrchr(file
, ':');
413 path
= wfindfile(paths
, file
);
417 path
= wfindfile(paths
, file
);
423 static void timeoutHandler(void *data
)
428 static char *getTextSelection(WScreen
* screen
, Atom selection
)
462 data
= XFetchBuffer(dpy
, &size
, buffer
);
469 unsigned long len
, bytes
;
473 static Atom clipboard
= 0;
476 clipboard
= XInternAtom(dpy
, "CLIPBOARD", False
);
478 XDeleteProperty(dpy
, screen
->info_window
, clipboard
);
480 XConvertSelection(dpy
, selection
, XA_STRING
, clipboard
, screen
->info_window
, CurrentTime
);
482 timer
= WMAddTimerHandler(1000, timeoutHandler
, &timeout
);
484 while (!XCheckTypedWindowEvent(dpy
, screen
->info_window
, SelectionNotify
, &ev
) && !timeout
) ;
487 WMDeleteTimerHandler(timer
);
489 wwarning("selection retrieval timed out");
493 /* nobody owns the selection or the current owner has
494 * nothing to do with what we need */
495 if (ev
.xselection
.property
== None
) {
499 if (XGetWindowProperty(dpy
, screen
->info_window
,
501 False
, XA_STRING
, &rtype
, &bits
, &len
,
502 &bytes
, (unsigned char **)&data
) != Success
) {
505 if (rtype
!= XA_STRING
|| bits
!= 8) {
506 wwarning("invalid data in text selection");
515 static char *getselection(WScreen
* scr
)
519 tmp
= getTextSelection(scr
, XA_PRIMARY
);
521 tmp
= getTextSelection(scr
, XA_CUT_BUFFER0
);
526 parseuserinputpart(char *line
, int *ptr
, char *endchars
)
528 int depth
= 0, begin
;
532 while(line
[*ptr
] != '\0') {
533 if(line
[*ptr
] == '(') {
535 } else if(depth
> 0 && line
[*ptr
] == ')') {
537 } else if(depth
== 0 && strchr(endchars
, line
[*ptr
]) != NULL
) {
538 value
= wmalloc(*ptr
- begin
+ 1);
539 strncpy(value
, line
+ begin
, *ptr
- begin
);
540 value
[*ptr
- begin
] = '\0';
550 getuserinput(WScreen
*scr
, char *line
, int *ptr
, Bool advanced
)
552 char *ret
= NULL
, *title
= NULL
, *prompt
= NULL
, *name
= NULL
;
555 if(line
[*ptr
] == '(')
556 title
= parseuserinputpart(line
, ptr
, ",)");
557 if(title
!= NULL
&& line
[*ptr
] == ',')
558 prompt
= parseuserinputpart(line
, ptr
, ",)");
559 if(prompt
!= NULL
&& line
[*ptr
] == ',')
560 name
= parseuserinputpart(line
, ptr
, ")");
563 rv
= wAdvancedInputDialog(scr
,
564 title
? gettext(title
):_("Program Arguments"),
565 prompt
? gettext(prompt
):_("Enter command arguments:"),
568 rv
= wInputDialog(scr
,
569 title
? gettext(title
):_("Program Arguments"),
570 prompt
? gettext(prompt
):_("Enter command arguments:"),
573 if(title
) wfree(title
);
574 if(prompt
) wfree(prompt
);
575 if(name
) wfree(name
);
577 return rv
? ret
: NULL
;
585 * state input new-state output
586 * NORMAL % OPTION <nil>
587 * NORMAL \ ESCAPE <nil>
588 * NORMAL etc. NORMAL <input>
589 * ESCAPE any NORMAL <input>
590 * OPTION s NORMAL <selection buffer>
591 * OPTION w NORMAL <selected window id>
592 * OPTION a NORMAL <input text>
593 * OPTION d NORMAL <OffiX DND selection object>
594 * OPTION W NORMAL <current workspace>
595 * OPTION etc. NORMAL %<input>
597 #define TMPBUFSIZE 64
598 char *ExpandOptions(WScreen
* scr
, char *cmdline
)
600 int ptr
, optr
, state
, len
, olen
;
602 char *selection
= NULL
;
603 char *user_input
= NULL
;
605 char *dropped_thing
= NULL
;
607 char tmpbuf
[TMPBUFSIZE
];
610 len
= strlen(cmdline
);
614 wwarning(_("out of memory during expansion of \"%s\""), cmdline
);
618 ptr
= 0; /* input line pointer */
619 optr
= 0; /* output line pointer */
624 switch (cmdline
[ptr
]) {
633 out
[optr
++] = cmdline
[ptr
];
638 switch (cmdline
[ptr
]) {
652 out
[optr
++] = cmdline
[ptr
];
658 switch (cmdline
[ptr
]) {
660 if (scr
->focused_window
&& scr
->focused_window
->flags
.focused
) {
661 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x",
662 (unsigned int)scr
->focused_window
->client_win
);
663 slen
= strlen(tmpbuf
);
665 nout
= realloc(out
, olen
);
667 wwarning(_("out of memory during expansion of \"%%w\""));
679 snprintf(tmpbuf
, sizeof(tmpbuf
), "0x%x", (unsigned int)scr
->current_workspace
+ 1);
680 slen
= strlen(tmpbuf
);
682 nout
= realloc(out
, olen
);
684 wwarning(_("out of memory during expansion of \"%%W\""));
695 user_input
= getuserinput(scr
, cmdline
, &ptr
, cmdline
[ptr
-1] == 'A');
697 slen
= strlen(user_input
);
699 nout
= realloc(out
, olen
);
701 wwarning(_("out of memory during expansion of \"%%a\""));
705 strcat(out
, user_input
);
708 /* Not an error, but user has Canceled the dialog box.
709 * This will make the command to not be performed. */
716 if (scr
->xdestring
) {
717 dropped_thing
= wstrdup(scr
->xdestring
);
719 if (!dropped_thing
) {
720 dropped_thing
= get_dnd_selection(scr
);
722 if (!dropped_thing
) {
723 scr
->flags
.dnd_data_convertion_status
= 1;
726 slen
= strlen(dropped_thing
);
728 nout
= realloc(out
, olen
);
730 wwarning(_("out of memory during expansion of \"%%d\""));
734 strcat(out
, dropped_thing
);
741 selection
= getselection(scr
);
744 wwarning(_("selection not available"));
747 slen
= strlen(selection
);
749 nout
= realloc(out
, olen
);
751 wwarning(_("out of memory during expansion of \"%%s\""));
755 strcat(out
, selection
);
761 out
[optr
++] = cmdline
[ptr
];
779 void ParseWindowName(WMPropList
* value
, char **winstance
, char **wclass
, char *where
)
783 *winstance
= *wclass
= NULL
;
785 if (!WMIsPLString(value
)) {
786 wwarning(_("bad window name value in %s state info"), where
);
790 name
= WMGetFromPLString(value
);
791 if (!name
|| strlen(name
) == 0) {
792 wwarning(_("bad window name value in %s state info"), where
);
796 UnescapeWM_CLASS(name
, winstance
, wclass
);
800 static char *keysymToString(KeySym keysym
, unsigned int state
)
803 char *buf
= wmalloc(20);
808 kev
.send_event
= False
;
809 kev
.window
= DefaultRootWindow(dpy
);
810 kev
.root
= DefaultRootWindow(dpy
);
811 kev
.same_screen
= True
;
812 kev
.subwindow
= kev
.root
;
813 kev
.serial
= 0x12344321;
814 kev
.time
= CurrentTime
;
816 kev
.keycode
= XKeysymToKeycode(dpy
, keysym
);
817 count
= XLookupString(&kev
, buf
, 19, NULL
, NULL
);
824 char *GetShortcutString(char *text
)
833 tmp
= text
= wstrdup(text
);
836 while ((k
= strchr(text
, '+')) != NULL
) {
840 mod
= wXModifierFromKey(text
);
842 return wstrdup("bug");
847 if (strcasecmp(text
, "Meta") == 0) {
848 buffer
= wstrappend(buffer
, "M+");
849 } else if (strcasecmp(text
, "Alt") == 0) {
850 buffer
= wstrappend(buffer
, "A+");
851 } else if (strcasecmp(text
, "Shift") == 0) {
852 buffer
= wstrappend(buffer
, "Sh+");
853 } else if (strcasecmp(text
, "Mod1") == 0) {
854 buffer
= wstrappend(buffer
, "M1+");
855 } else if (strcasecmp(text
, "Mod2") == 0) {
856 buffer
= wstrappend(buffer
, "M2+");
857 } else if (strcasecmp(text
, "Mod3") == 0) {
858 buffer
= wstrappend(buffer
, "M3+");
859 } else if (strcasecmp(text
, "Mod4") == 0) {
860 buffer
= wstrappend(buffer
, "M4+");
861 } else if (strcasecmp(text
, "Mod5") == 0) {
862 buffer
= wstrappend(buffer
, "M5+");
863 } else if (strcasecmp(text
, "Control") == 0) {
866 buffer
= wstrappend(buffer
, text
);
872 buffer
= wstrappend(buffer
, "^");
874 buffer
= wstrappend(buffer
, text
);
877 /* ksym = XStringToKeysym(text);
878 tmp = keysymToString(ksym, modmask);
880 buffer = wstrappend(buffer, tmp);
887 char *EscapeWM_CLASS(char *name
, char *class)
890 char *ename
= NULL
, *eclass
= NULL
;
898 ename
= wmalloc(l
* 2 + 1);
900 for (i
= 0; i
< l
; i
++) {
901 if (name
[i
] == '\\') {
903 } else if (name
[i
] == '.') {
906 ename
[j
++] = name
[i
];
912 eclass
= wmalloc(l
* 2 + 1);
914 for (i
= 0; i
< l
; i
++) {
915 if (class[i
] == '\\') {
917 } else if (class[i
] == '.') {
920 eclass
[j
++] = class[i
];
925 if (ename
&& eclass
) {
926 int len
= strlen(ename
) + strlen(eclass
) + 4;
928 snprintf(ret
, len
, "%s.%s", ename
, eclass
);
932 ret
= wstrdup(ename
);
935 ret
= wstrdup(eclass
);
942 void UnescapeWM_CLASS(char *str
, char **name
, char **class)
952 /* separate string in 2 parts */
954 for (i
= 0; i
< j
; i
++) {
955 if (str
[i
] == '\\') {
958 } else if (str
[i
] == '.') {
964 /* unescape strings */
965 for (i
= 0, k
= 0; i
< dot
; i
++) {
966 if (str
[i
] == '\\') {
969 (*name
)[k
++] = str
[i
];
974 for (i
= dot
+ 1, k
= 0; i
< j
; i
++) {
975 if (str
[i
] == '\\') {
978 (*class)[k
++] = str
[i
];
993 void SendHelperMessage(WScreen
* scr
, char type
, int workspace
, char *msg
)
1000 if (!scr
->flags
.backimage_helper_launched
) {
1004 len
= (msg
? strlen(msg
) : 0) + (workspace
>= 0 ? 4 : 0) + 1;
1005 buffer
= wmalloc(len
+ 5);
1006 snprintf(buf
, sizeof(buf
), "%4i", len
);
1007 memcpy(buffer
, buf
, 4);
1010 if (workspace
>= 0) {
1011 snprintf(buf
, sizeof(buf
), "%4i", workspace
);
1012 memcpy(&buffer
[i
], buf
, 4);
1017 strcpy(&buffer
[i
], msg
);
1019 if (write(scr
->helper_fd
, buffer
, len
+ 4) < 0) {
1020 wsyserror(_("could not send message to background image helper"));
1025 Bool
UpdateDomainFile(WDDomain
* domain
)
1028 char path
[PATH_MAX
];
1029 WMPropList
*shared_dict
, *dict
;
1030 Bool result
, freeDict
= False
;
1032 dict
= domain
->dictionary
;
1033 if (WMIsPLDictionary(domain
->dictionary
)) {
1034 /* retrieve global system dictionary */
1035 snprintf(path
, sizeof(path
), "%s/WindowMaker/%s", SYSCONFDIR
, domain
->domain_name
);
1036 if (stat(path
, &stbuf
) >= 0) {
1037 shared_dict
= WMReadPropListFromFile(path
);
1039 if (WMIsPLDictionary(shared_dict
)) {
1041 dict
= WMDeepCopyPropList(domain
->dictionary
);
1042 WMSubtractPLDictionaries(dict
, shared_dict
, True
);
1044 WMReleasePropList(shared_dict
);
1049 result
= WMWritePropListToFile(dict
, domain
->path
);
1052 WMReleasePropList(dict
);
1058 char *StrConcatDot(char *a
, char *b
)
1068 len
= strlen(a
) + strlen(b
) + 4;
1071 snprintf(str
, len
, "%s.%s", a
, b
);
1076 #define MAX_CMD_SIZE 4096
1078 Bool
GetCommandForPid(int pid
, char ***argv
, int *argc
)
1080 static char buf
[MAX_CMD_SIZE
];
1085 sprintf(buf
, "/proc/%d/cmdline", pid
);
1086 fPtr
= fopen(buf
, "r");
1088 count
= read(fileno(fPtr
), buf
, MAX_CMD_SIZE
);
1091 for (i
= 0, *argc
= 0; i
< count
; i
++) {
1100 *argv
= (char **)wmalloc(sizeof(char *) * (*argc
));
1102 for (i
= 0, j
= 1; i
< count
; i
++) {
1105 if (i
< count
- 1) {
1106 (*argv
)[j
++] = &buf
[i
+ 1];
1121 static char *getCommandForWindow(Window win
, int elements
)
1123 char **argv
, *command
= NULL
;
1126 if (XGetCommand(dpy
, win
, &argv
, &argc
)) {
1127 if (argc
> 0 && argv
!= NULL
) {
1130 command
= wtokenjoin(argv
, WMIN(argc
, elements
));
1131 if (command
[0] == 0) {
1137 XFreeStringList(argv
);
1144 /* Free result when done */
1145 char *GetCommandForWindow(Window win
)
1147 return getCommandForWindow(win
, 0);
1150 /* Free result when done */
1151 char *GetProgramNameForWindow(Window win
)
1153 return getCommandForWindow(win
, 1);