summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/find_desktop.c87
1 files changed, 64 insertions, 23 deletions
diff --git a/src/find_desktop.c b/src/find_desktop.c
index ca2004b..fa886e3 100644
--- a/src/find_desktop.c
+++ b/src/find_desktop.c
@@ -13,6 +13,8 @@
# include <unistd.h>
#endif
+#include <sys/select.h>
+#include <sys/time.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
@@ -69,7 +71,7 @@ int main(int argc, char** argv)
return EXIT_SUCCESS;
}
-static int _get_from_e17_using_window(Display* dsp, Window wnd);
+static int _get_from_e17_using_window(Display* dsp, Window wnd, int timeout_ms);
static char* create_desktop(const char* prefix, int desktop, int use_prefix);
char* find_desktop(const char* display, int use_prefix)
@@ -115,7 +117,7 @@ char* find_desktop(const char* display, int use_prefix)
errno = 0;
windowid = strtoul(windowid_env, &end, 10);
if (windowid != 0 && errno == 0 && end != NULL && *end == '\0') {
- int ret = _get_from_e17_using_window(dsp, (Window)windowid);
+ int ret = _get_from_e17_using_window(dsp, (Window)windowid, 0);
if (ret != -1) {
XCloseDisplay(dsp);
return create_desktop("enlightenment", ret, use_prefix);
@@ -132,8 +134,9 @@ char* find_desktop(const char* display, int use_prefix)
wnd = XCreateWindow(dsp, root, 0, 0, 1, 1,
0, CopyFromParent, CopyFromParent,
CopyFromParent, 0, &attrs);
+ XSelectInput(dsp, wnd, PropertyChangeMask);
XMapWindow(dsp, wnd);
- ret = _get_from_e17_using_window(dsp, wnd);
+ ret = _get_from_e17_using_window(dsp, wnd, 2000);
XUnmapWindow(dsp, wnd);
XDestroyWindow(dsp, wnd);
if (ret != -1) {
@@ -163,37 +166,75 @@ char* create_desktop(const char* prefix, int desktop, int use_prefix)
static Atom __e_window_desk = 0;
-int _get_from_e17_using_window(Display* dsp, Window wnd)
+static int _read_desk(Display* dsp, Window wnd, int* out)
{
Atom actual = 0;
int format = 32;
unsigned long count = 0, remaining = 0;
unsigned char* data = NULL;
- int try;
+ int found = 0;
+
+ if (!XGetWindowProperty(dsp, wnd, __e_window_desk,
+ 0, 2 * sizeof(long), False, XA_CARDINAL,
+ &actual, &format, &count, &remaining, &data) &&
+ data != NULL) {
+ if (count >= 2 && format == 32) {
+ int x = ((long*)data)[0];
+ int y = ((long*)data)[1];
+ *out = (x * 256) + y;
+ found = 1;
+ }
+ }
+ if (data != NULL)
+ XFree(data);
+ return found;
+}
+
+/* Waits up to timeout_ms for the property to show up, woken by
+ * PropertyNotify instead of polling/sleeping. The caller must have
+ * selected PropertyChangeMask on wnd if timeout_ms > 0. */
+int _get_from_e17_using_window(Display* dsp, Window wnd, int timeout_ms)
+{
+ int ret;
+ struct timeval end, now;
if (__e_window_desk == 0) {
__e_window_desk = XInternAtom(dsp, "__E_WINDOW_DESK", 0);
if (__e_window_desk == 0)
return -1;
}
- for (try = 0; try < 2; ++try)
- {
- if (!XGetWindowProperty(dsp, wnd, __e_window_desk,
- 0, 2 * sizeof(long), False, XA_CARDINAL,
- &actual, &format, &count, &remaining, &data))
- {
- if (data != NULL)
- {
- break;
- }
- }
- sleep(1);
+ if (_read_desk(dsp, wnd, &ret))
+ return ret;
+
+ gettimeofday(&end, NULL);
+ end.tv_sec += timeout_ms / 1000;
+ end.tv_usec += (timeout_ms % 1000) * 1000;
+ if (end.tv_usec >= 1000000) {
+ end.tv_sec++;
+ end.tv_usec -= 1000000;
}
- if (data != NULL && count > 0 && format == 32) {
- int x = ((long*)data)[0];
- int y = ((long*)data)[1];
- XFree(data);
- return (x * 256) + y;
+ for (;;) {
+ XEvent ev;
+ struct timeval left;
+ fd_set fds;
+ int fd = ConnectionNumber(dsp);
+
+ while (XPending(dsp)) {
+ XNextEvent(dsp, &ev);
+ if (ev.type == PropertyNotify &&
+ ev.xproperty.window == wnd &&
+ ev.xproperty.atom == __e_window_desk &&
+ ev.xproperty.state == PropertyNewValue &&
+ _read_desk(dsp, wnd, &ret))
+ return ret;
+ }
+ gettimeofday(&now, NULL);
+ if (timercmp(&now, &end, >=))
+ return -1;
+ timersub(&end, &now, &left);
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ if (select(fd + 1, &fds, NULL, NULL, &left) == 0)
+ return _read_desk(dsp, wnd, &ret) ? ret : -1;
}
- return -1;
}