#include "common.h" #if HAVE_STDIO_H # include #endif #if HAVE_STRING_H # include #endif #if HAVE_ERRNO_H # include #endif #if HAVE_UNISTD_H # include #endif #include #include #include #include static char* find_desktop(const char* display, int use_prefix); int main(int argc, char** argv) { int a; const char* display = NULL; char* desktop; int usage = 0, error = 0, prefix = 0; for (a = 1; a < argc; ++a) { if (argv[a][0] == '-') { if (strcmp(argv[a] + 1, "display") == 0) { ++a; if (a == argc) { error = 1; } else { display = argv[a]; } } else if (strcmp(argv[a] + 1, "prefix") == 0) { prefix = 1; } else if (strcmp(argv[a] + 1, "help") == 0 || strcmp(argv[a] + 1, "-help") == 0) { usage = 1; } else { error = 1; } } else { error = 1; } } if (usage) { fputs("Usage `find_desktop [OPTIONS...]`\n", stdout); fputs("\n", stdout); fputs("Options:\n", stdout); fputs(" -display X11 display to connect to instead of default\n", stdout); fputs(" -prefix prefix display number with source\n", stdout); fputs(" -help display this text and exit\n", stdout); fputs("\n", stdout); return error ? EXIT_FAILURE : EXIT_SUCCESS; } if (error) { fputs("Try `find_desktop -help` for usage.\n", stdout); return EXIT_FAILURE; } desktop = find_desktop(display, prefix); if (!desktop) { return EXIT_FAILURE; } fprintf(stdout, "%s\n", desktop); free(desktop); return EXIT_SUCCESS; } 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) { Display* dsp = XOpenDisplay(display); Window root; int screen; Atom _net_current_desktop, actual; int format = 32; unsigned long count, remaining; unsigned char* data = NULL; const char* windowid_env; const char* desktop_env; if (dsp == NULL) { fputs("Unable to open X11 display.\n", stderr); return NULL; } screen = DefaultScreen(dsp); root = RootWindow(dsp, screen); desktop_env = getenv("DESKTOP"); if (desktop_env == NULL || strncmp(desktop_env, "Enlightenment-0.17", 18) != 0) { /* Skip _NET_CURRENT_DESKTOP if we're in e17 */ /* Try to read the _NET_CURRENT_DESKTOP property */ _net_current_desktop = XInternAtom(dsp, "_NET_CURRENT_DESKTOP", 0); if (!XGetWindowProperty(dsp, root, _net_current_desktop, 0, sizeof(long), False, XA_CARDINAL, &actual, &format, &count, &remaining, &data) && data != NULL && count > 0 && format == 32) { int ret = ((long*)data)[0]; XFree(data); XCloseDisplay(dsp); return create_desktop("ewmh", ret, use_prefix); } } /* Failing that, let's see if our owner knows */ windowid_env = getenv("WINDOWID"); if (windowid_env != NULL) { unsigned long windowid; char* end = NULL; 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, 0); if (ret != -1) { XCloseDisplay(dsp); return create_desktop("enlightenment", ret, use_prefix); } } } /* Try creating a bogus window then */ { XSetWindowAttributes attrs; Window wnd; int ret; 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, 2000); XUnmapWindow(dsp, wnd); XDestroyWindow(dsp, wnd); if (ret != -1) { XCloseDisplay(dsp); return create_desktop("enlightenment", ret, use_prefix); } } /* Don't know what else to do */ XCloseDisplay(dsp); return NULL; } char* create_desktop(const char* prefix, int desktop, int use_prefix) { if (use_prefix) { size_t len = strlen(prefix) + 11; char* ret = malloc(len); snprintf(ret, len, "%s_%d", prefix, desktop); return ret; } else { char* ret = malloc(10); snprintf(ret, 10, "%d", desktop); return ret; } } static Atom __e_window_desk = 0; 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 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; } 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; } 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; } }