summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am11
-rw-r--r--src/common.h22
-rw-r--r--src/find_desktop.c179
3 files changed, 212 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..f7b1b36
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,11 @@
+MAINTAINERCLEANFILES = Makefile.in
+
+INCLUDES = -I. -I$(top_srcdir) @DEFINES@
+
+bin_PROGRAMS = find_desktop
+
+find_desktop_SOURCES = common.h find_desktop.c
+find_desktop_CFLAGS = @X_CFLAGS@
+find_desktop_LDFLAGS = @X_PRE_LIBS@ @X_LIBS@ -lX11 @X_EXTRA_LIBS@
+
+
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..60cfcea
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,22 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#if HAVE_STDLIB_H
+# include <stdlib.h>
+#else
+# errnor "no stdlib, wtf?"
+#endif
+
+#ifdef DEBUG
+# if HAVE_ASSERT_H
+# include <assert.h>
+# endif
+#else
+# define assert(__x) /* __x */
+#endif
+
+#endif /* COMMON_H */
diff --git a/src/find_desktop.c b/src/find_desktop.c
new file mode 100644
index 0000000..4c33f2d
--- /dev/null
+++ b/src/find_desktop.c
@@ -0,0 +1,179 @@
+#include "common.h"
+
+#if HAVE_STDIO_H
+# include <stdio.h>
+#endif
+#if HAVE_STRING_H
+# include <string.h>
+#endif
+#if HAVE_ERRNO_H
+# include <errno.h>
+#endif
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+
+static int find_desktop(const char* display);
+
+int main(int argc, char** argv)
+{
+ int d = -1, a;
+ const char* display = NULL;
+ int usage = 0, error = 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, "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 <DISPLAY> X11 display to connect to instead of default\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;
+ }
+ d = find_desktop(display);
+ if (d < 0)
+ {
+ return EXIT_FAILURE;
+ }
+ fprintf(stdout, "%d\n", d);
+ return EXIT_SUCCESS;
+}
+
+static int _get_from_e17_using_window(Display* dsp, Window wnd);
+
+int find_desktop(const char* display)
+{
+ 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 EXIT_FAILURE;
+ }
+
+ 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 ret;
+ }
+ }
+
+ /* 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);
+ if (ret != -1) {
+ XCloseDisplay(dsp);
+ return ret;
+ }
+ }
+ }
+
+ /* 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);
+ XMapWindow(dsp, wnd);
+ ret = _get_from_e17_using_window(dsp, wnd);
+ XUnmapWindow(dsp, wnd);
+ XDestroyWindow(dsp, wnd);
+ if (ret != -1) {
+ XCloseDisplay(dsp);
+ return ret;
+ }
+ }
+
+ /* Don't know what else to do */
+ XCloseDisplay(dsp);
+ return -1;
+}
+
+static Atom __e_window_desk = 0;
+
+int _get_from_e17_using_window(Display* dsp, Window wnd)
+{
+ Atom actual = 0;
+ int format = 32;
+ unsigned long count = 0, remaining = 0;
+ unsigned char* data = NULL;
+ int try;
+
+ 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 (data != NULL && count > 0 && format == 32) {
+ int x = ((long*)data)[0];
+ int y = ((long*)data)[1];
+ XFree(data);
+ return (x * 256) + y;
+ }
+ return -1;
+}