properly handle gtk windows

This commit is contained in:
Adrien Schildknecht 2013-04-25 20:27:50 +02:00
parent c338ec08c2
commit 7d4aecffc2
2 changed files with 37 additions and 11 deletions

1
TODO
View File

@ -8,5 +8,4 @@ multi-monitor ?
Bugs Bugs
---- ----
xcwd is unable to get the properties of a gtk window.

47
xcwd.c
View File

@ -12,7 +12,9 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#define XA_STRING (XInternAtom(dpy, "STRING", 0)) #define XA_STRING (XInternAtom(dpy, "STRING", 0))
#define XA_CARDINAL (XInternAtom(dpy, "CARDINAL", 0))
#define XA_WM_STATE (XInternAtom(dpy, "WM_STATE", 0))
Display *dpy; Display *dpy;
@ -39,31 +41,52 @@ int ppidCmp(const void *p1, const void *p2)
static Window focusedWindow() static Window focusedWindow()
{ {
Window focuswin; Window focuswin, root;
int focusrevert; int focusrevert;
int format, status;
unsigned long nitems, after;
unsigned char *data;
Atom type;
Window* children;
unsigned int nchildren;
dpy = XOpenDisplay (NULL); dpy = XOpenDisplay (NULL);
if (!dpy) if (!dpy)
exit (1); exit (1);
XGetInputFocus (dpy, &focuswin, &focusrevert); XGetInputFocus (dpy, &focuswin, &focusrevert);
root = XDefaultRootWindow(dpy);
do {
status = XGetWindowProperty(dpy, focuswin, XA_WM_STATE, 0, 65536, 0,
XA_WM_STATE, &type, &format, &nitems, &after, &data);
if(status == Success && data) {
XFree(data);
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "Window ID = %lu\n", focuswin); fprintf(stderr, "Window ID = %lu\n", focuswin);
#endif #endif
return focuswin; return focuswin;
}
XQueryTree(dpy, focuswin, &root, &focuswin, &children, &nchildren);
#ifdef DEBUG
fprintf(stderr, "Current window do not have WM_STATE, getting parent\n");
#endif
} while(focuswin != root);
return 0;
} }
static long windowPid(Window focuswin) static long windowPid(Window focuswin)
{ {
Atom nameAtom = XInternAtom(dpy, "_NET_WM_PID", 1); Atom nameAtom = XInternAtom(dpy, "_NET_WM_PID", 1);
Atom cardinalAtom = XInternAtom(dpy, "CARDINAL", 0);
Atom type; Atom type;
int format; int format, status;
long pid = -1; long pid = -1;
unsigned long nitems, after; unsigned long nitems, after;
unsigned char *data = 0; unsigned char *data;
if (XGetWindowProperty(dpy, focuswin, nameAtom, 0, 1024, 0, cardinalAtom, status = XGetWindowProperty(dpy, focuswin, nameAtom, 0, 65536, 0,
&type, &format, &nitems, &after, &data) == Success) { XA_CARDINAL, &type, &format, &nitems, &after, &data);
if(status == Success) {
if (data) { if (data) {
pid = *((long*)data); pid = *((long*)data);
XFree(data); XFree(data);
@ -187,8 +210,12 @@ static void readPath(long pid)
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
{ {
processes_t p; processes_t p;
Window w = focusedWindow();
long pid; long pid;
Window w = focusedWindow();
if(w == 0) {
fprintf(stdout, "%s\n", getenv("HOME"));
return EXIT_FAILURE;
}
pid = windowPid(w); pid = windowPid(w);
p = getProcesses(); p = getProcesses();