--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/spacenav-lib-hack.c Fri Mar 08 20:11:23 2019 +0100
@@ -0,0 +1,159 @@
+/**
+ * Spacenav lib hack
+ * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdint.h>
+#include <dlfcn.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#include <X11/Xlib.h>
+#include <spnav.h>
+
+static Display* spacenav_hack_dpy;
+static Window spacenav_hack_win;
+
+static pthread_t spacenav_hack_thread;
+
+// from proto_x11.c (spacenavd)
+static Atom xa_event_motion, xa_event_bpress, xa_event_brelease, xa_event_cmd;
+static float x11_sens = 1.0;
+
+/**
+ * This function is based on proto_x11.c (spacenavd) written by:
+ * Copyright (C) 2007-2019 John Tsiombikas <nuclear@member.fsf.org>
+ * and published under GNU GPLv3+
+ *
+ * @param ev
+ * @param dpy
+ * @param win
+ */
+static void send_xevent(spnav_event *ev, Display* dpy, Window win) {
+ int i;
+ XEvent xevent;
+
+ if (!dpy) return;
+
+ // if(setjmp(jbuf)) return;
+
+ xevent.type = ClientMessage;
+ xevent.xclient.send_event = False;
+ xevent.xclient.display = dpy;
+ xevent.xclient.window = win;
+
+ switch (ev->type) {
+ case SPNAV_EVENT_MOTION:
+ xevent.xclient.message_type = xa_event_motion;
+ xevent.xclient.format = 16;
+
+ for (i = 0; i < 6; i++) {
+ float val = (float) ev->motion.data[i] * x11_sens;
+ xevent.xclient.data.s[i + 2] = (short) val;
+ }
+ xevent.xclient.data.s[0] = xevent.xclient.data.s[1] = 0;
+ xevent.xclient.data.s[8] = ev->motion.period;
+ break;
+
+ case SPNAV_EVENT_BUTTON:
+ xevent.xclient.message_type = ev->button.press ? xa_event_bpress : xa_event_brelease;
+ xevent.xclient.format = 16;
+ xevent.xclient.data.s[2] = ev->button.bnum;
+ break;
+
+ default:
+ break;
+ }
+
+ XSendEvent(dpy, win, False, 0, &xevent);
+ XFlush(dpy);
+}
+
+static void* spacenav_hack_translate_events(void* arg) {
+ pthread_setname_np(pthread_self(), "spacenav-hack"); // can be found with: ps H -o 'pid tid cmd comm'
+ spnav_event event;
+ while (1) {
+ if (spnav_wait_event(&event)) {
+ //fprintf(stderr, "spnav-lib-hack: got event\n");
+ XLockDisplay(spacenav_hack_dpy);
+ send_xevent(&event, spacenav_hack_dpy, spacenav_hack_win);
+ XUnlockDisplay(spacenav_hack_dpy);
+ }
+ }
+}
+
+int spnav_x11_open(Display* dpy, Window win) {
+ spacenav_hack_dpy = dpy;
+ spacenav_hack_win = win;
+
+ int result = spnav_open();
+ fprintf(stderr, "spnav-lib-hack: instead of spnav_x11_open(%p, 0x%lx) calling spnav_open() = %d\n", dpy, win, result);
+
+ if (result == 0) {
+ xa_event_motion = XInternAtom(dpy, "MotionEvent", False);
+ xa_event_bpress = XInternAtom(dpy, "ButtonPressEvent", False);
+ xa_event_brelease = XInternAtom(dpy, "ButtonReleaseEvent", False);
+ xa_event_cmd = XInternAtom(dpy, "CommandEvent", False);
+
+ XInitThreads();
+ pthread_create(&spacenav_hack_thread, NULL, spacenav_hack_translate_events, NULL);
+ }
+
+ return result;
+}
+
+
+
+/*
+int spnav_x11_event(const XEvent* xev, spnav_event* event) {
+ xev.
+ static int (*real_spnav_x11_event)(const XEvent*, spnav_event*) = NULL;
+ if (!real_spnav_x11_event) real_spnav_x11_event = dlsym(RTLD_NEXT, "spnav_x11_event");
+ int result = real_spnav_x11_event(xev, event);
+ if (result == SPNAV_EVENT_MOTION || result == SPNAV_EVENT_BUTTON) fprintf(stderr, "spnav-lib-hack: spnav_x11_event() = %d\n", result);
+ return result;
+}
+ */
+
+
+/*
+int spnav_x11_open(Display* dpy, Window win) {
+ static int (*real_spnav_x11_open)(Display*, Window) = NULL;
+ if (!real_spnav_x11_open) real_spnav_x11_open = dlsym(RTLD_NEXT, "spnav_x11_open");
+ int result = real_spnav_x11_open(dpy, win);
+ fprintf(stderr, "spnav-lib-hack: spnav_x11_open() = %d\n", result);
+ return result;
+}
+ */
+
+
+/*
+int spnav_open() {
+ return -1;
+}
+ */
+
+/*
+int spnav_open() {
+ static int (*real_spnav_open)() = NULL;
+ if (!real_spnav_open) real_spnav_open = dlsym(RTLD_NEXT, "spnav_open");
+ int result = real_spnav_open();
+ // fprintf(stderr, "spnav-lib-hack: spnav_open() = %d\n", result);
+ return result;
+}
+ */