almost working, but there is an SIGABRT v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 08 Mar 2019 20:11:23 +0100
branchv_0
changeset 1 ac5f3768ebb1
parent 0 37aa134ae57b
child 2 3ba27504be0e
almost working, but there is an SIGABRT D_PRELOAD=./spacenav-lib-hack/build/Debug/src/libspnav-lib-hack.so freecad FreeCAD 0.16, Libs: 0.16R © Juergen Riegel, Werner Mayer, Yorik van Havre 2001-2015 ##### #### ### #### # # # # # # # ## #### #### # # # # # #### # # # # # # # ##### # # # # #### #### # # # # # # # # # # # # # # ## ## ## # # #### #### ### # # #### ## ## ## No module named WebGui spnav-lib-hack: instead of spnav_x11_open(0x557304430830, 0xb600088) calling spnav_open() = 0 [xcb] Unknown sequence number while processing queue [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called [xcb] Aborting, sorry about that. freecad: ../../src/xcb_io.c:259: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed. Neúspěšně ukončen (SIGABRT) (core dumped [obraz paměti uložen])
CMakeLists.txt
nbproject/configurations.xml
nbproject/project.xml
src/CMakeLists.txt
src/spacenav-demo-qt.cpp
src/spacenav-lib-hack.c
src/spacenav-lib-hack.h
--- a/CMakeLists.txt	Wed Mar 06 16:21:18 2019 +0100
+++ b/CMakeLists.txt	Fri Mar 08 20:11:23 2019 +0100
@@ -1,5 +1,5 @@
-# Spacenav Demo Qt
-# Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
+# 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
@@ -14,6 +14,6 @@
 # You should have received a copy of the GNU General Public License
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
 
-project (spacenav-demo-qt)
+project (spacenav-lib-hack)
 cmake_minimum_required(VERSION 3.7.2)
 add_subdirectory (src)
--- a/nbproject/configurations.xml	Wed Mar 06 16:21:18 2019 +0100
+++ b/nbproject/configurations.xml	Fri Mar 08 20:11:23 2019 +0100
@@ -51,21 +51,21 @@
       <df name="build">
         <df name="Release">
           <df name="src">
-            <df name="spacenav-demo-qt_autogen">
+            <df name="spacenav-lib-hack_autogen">
               <in>mocs_compilation.cpp</in>
             </df>
           </df>
         </df>
       </df>
       <df name="src">
-        <in>spacenav-demo-qt.cpp</in>
+        <in>spacenav-lib-hack.c</in>
       </df>
     </df>
-    <logicalFolder name="spacenav-demo-qt_autogen"
-                   displayName="spacenav-demo-qt_autogen"
+    <logicalFolder name="spacenav-lib-hack_autogen"
+                   displayName="spacenav-lib-hack_autogen"
                    projectFiles="true"
-                   root="build/Debug/src/spacenav-demo-qt_autogen">
-      <itemPath>build/Debug/src/spacenav-demo-qt_autogen/mocs_compilation.cpp</itemPath>
+                   root="build/Debug/src/spacenav-lib-hack_autogen">
+      <itemPath>build/Debug/src/spacenav-lib-hack_autogen/mocs_compilation.cpp</itemPath>
     </logicalFolder>
     <logicalFolder name="ExternalFiles"
                    displayName="Important Files"
@@ -90,6 +90,8 @@
       </toolsSet>
       <flagsDictionary>
         <element flagsID="0" commonFlags="-fsanitize=address -fPIC -std=gnu++11"/>
+        <element flagsID="1" commonFlags="-g -fPIC"/>
+        <element flagsID="2" commonFlags="-std=c++14"/>
       </flagsDictionary>
       <codeAssistance>
       </codeAssistance>
@@ -98,23 +100,19 @@
           <buildCommandWorkingDir>build/Debug</buildCommandWorkingDir>
           <buildCommand>${MAKE} -f Makefile</buildCommand>
           <cleanCommand>${MAKE} -f Makefile clean</cleanCommand>
-          <executablePath>build/Debug/src/spacenav-demo-qt</executablePath>
+          <executablePath>build/Debug/src/spacenav-lib-hack</executablePath>
+          <cTool>
+            <incDir>
+              <pElem>build/Debug/src</pElem>
+            </incDir>
+            <preprocessorList>
+              <Elem>spnav_lib_hack_EXPORTS</Elem>
+            </preprocessorList>
+          </cTool>
           <ccTool>
             <incDir>
               <pElem>build/Debug/src</pElem>
-              <pElem>src</pElem>
-              <pElem>build/Debug/src/spacenav-demo-qt_autogen/include</pElem>
-              <pElem>/usr/include/x86_64-linux-gnu/qt5</pElem>
-              <pElem>/usr/include/x86_64-linux-gnu/qt5/QtWidgets</pElem>
-              <pElem>/usr/include/x86_64-linux-gnu/qt5/QtGui</pElem>
-              <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCore</pElem>
-              <pElem>/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++</pElem>
             </incDir>
-            <preprocessorList>
-              <Elem>QT_CORE_LIB</Elem>
-              <Elem>QT_GUI_LIB</Elem>
-              <Elem>QT_WIDGETS_LIB</Elem>
-            </preprocessorList>
           </ccTool>
         </makeTool>
         <preBuild>
@@ -127,24 +125,63 @@
             ex="false"
             tool="1"
             flavor2="11">
+        <ccTool flags="2">
+        </ccTool>
       </item>
-      <item path="build/Debug/src/spacenav-demo-qt_autogen/mocs_compilation.cpp"
+      <item path="build/Debug/src/spacenav-lib-hack_autogen/mocs_compilation.cpp"
             ex="false"
             tool="1"
             flavor2="8">
         <ccTool flags="0">
         </ccTool>
       </item>
+      <folder path="0">
+        <ccTool>
+          <preprocessorList>
+            <Elem>spnav_lib_hack_EXPORTS</Elem>
+          </preprocessorList>
+        </ccTool>
+      </folder>
       <folder path="Modules">
         <ccTool>
           <incDir>
+            <pElem>src</pElem>
+            <pElem>build/Debug/src/spacenav-lib-hack_autogen/include</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtWidgets</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtGui</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCore</pElem>
+            <pElem>/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++</pElem>
             <pElem>/usr/include</pElem>
           </incDir>
+          <preprocessorList>
+            <Elem>QT_CORE_LIB</Elem>
+            <Elem>QT_GUI_LIB</Elem>
+            <Elem>QT_WIDGETS_LIB</Elem>
+          </preprocessorList>
         </ccTool>
       </folder>
-      <item path="src/spacenav-demo-qt.cpp" ex="false" tool="1" flavor2="8">
-        <ccTool flags="0">
+      <folder path="spacenav-lib-hack_autogen">
+        <ccTool>
+          <incDir>
+            <pElem>src</pElem>
+            <pElem>build/Debug/src/spacenav-lib-hack_autogen/include</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtWidgets</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtGui</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCore</pElem>
+            <pElem>/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++</pElem>
+          </incDir>
+          <preprocessorList>
+            <Elem>QT_CORE_LIB</Elem>
+            <Elem>QT_GUI_LIB</Elem>
+            <Elem>QT_WIDGETS_LIB</Elem>
+          </preprocessorList>
         </ccTool>
+      </folder>
+      <item path="src/spacenav-lib-hack.c" ex="false" tool="0" flavor2="0">
+        <cTool flags="1">
+        </cTool>
       </item>
     </conf>
     <conf name="Release" type="0">
@@ -168,7 +205,7 @@
             <incDir>
               <pElem>build/Release/src</pElem>
               <pElem>src</pElem>
-              <pElem>build/Release/src/spacenav-demo-qt_autogen/include</pElem>
+              <pElem>build/Release/src/spacenav-lib-hack_autogen/include</pElem>
               <pElem>/usr/include/x86_64-linux-gnu/qt5</pElem>
               <pElem>/usr/include/x86_64-linux-gnu/qt5/QtWidgets</pElem>
               <pElem>/usr/include/x86_64-linux-gnu/qt5/QtGui</pElem>
@@ -194,7 +231,7 @@
             tool="1"
             flavor2="0">
       </item>
-      <item path="build/Release/src/spacenav-demo-qt_autogen/mocs_compilation.cpp"
+      <item path="build/Release/src/spacenav-lib-hack_autogen/mocs_compilation.cpp"
             ex="false"
             tool="1"
             flavor2="8">
@@ -215,10 +252,6 @@
           </incDir>
         </ccTool>
       </folder>
-      <item path="src/spacenav-demo-qt.cpp" ex="false" tool="1" flavor2="8">
-        <ccTool flags="0">
-        </ccTool>
-      </item>
     </conf>
   </confs>
 </configurationDescriptor>
--- a/nbproject/project.xml	Wed Mar 06 16:21:18 2019 +0100
+++ b/nbproject/project.xml	Fri Mar 08 20:11:23 2019 +0100
@@ -42,8 +42,8 @@
     <type>org.netbeans.modules.cnd.makeproject</type>
     <configuration>
         <data xmlns="http://www.netbeans.org/ns/make-project/1">
-            <name>spacenav-demo-qt</name>
-            <c-extensions/>
+            <name>spacenav-lib-hack</name>
+            <c-extensions>c</c-extensions>
             <cpp-extensions>cpp</cpp-extensions>
             <header-extensions>h</header-extensions>
             <sourceEncoding>UTF-8</sourceEncoding>
--- a/src/CMakeLists.txt	Wed Mar 06 16:21:18 2019 +0100
+++ b/src/CMakeLists.txt	Fri Mar 08 20:11:23 2019 +0100
@@ -1,5 +1,5 @@
-# Spacenav Demo Qt
-# Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
+# 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
@@ -14,14 +14,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
 
-set(EXECUTABLE_FILE "spacenav-demo-qt")
-
-# Qt libraries:
-set(CMAKE_INCLUDE_CURRENT_DIR ON) # Find includes in corresponding build directories
-set(CMAKE_AUTOMOC ON) # Instruct CMake to run moc automatically when needed
-set(CMAKE_AUTOUIC ON) # Create code from a list of Qt designer ui files
-find_package(Qt5Widgets CONFIG REQUIRED) # Find the QtWidgets library
-# find_package(Qt5Charts CONFIG REQUIRED)
+set(LIBRARY_FILE spnav-lib-hack)
 
 # Add ASan AddressSanitizer
 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
@@ -29,28 +22,12 @@
 # From ASan wiki: "To get nicer stack traces in error messages add -fno-omit-frame-pointer."
 
 # Executable output:
-add_executable(
-	${EXECUTABLE_FILE}
-	# QObjects must be listed here (including them from other files is not enough)
-	spacenav-demo-qt.cpp
+add_library(
+	${LIBRARY_FILE} SHARED
+	spacenav-lib-hack.h
+	spacenav-lib-hack.c
 )
 
 # Link libraries:
-target_link_libraries(${EXECUTABLE_FILE} -lX11 -lm -lspnav)
-target_link_libraries(${EXECUTABLE_FILE} Qt5::Widgets)
-# target_link_libraries(${EXECUTABLE_FILE} Qt5::Charts)
-set_property(TARGET ${EXECUTABLE_FILE} PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE)
-
-install(TARGETS ${EXECUTABLE_FILE} DESTINATION bin)
+target_link_libraries(${LIBRARY_FILE} -ldl -lX11 -lm -lspnav)
 
-# Print all variables:
-#macro(print_all_variables)
-#	message(STATUS "print_all_variables------------------------------------------{")
-#	get_cmake_property(_variableNames VARIABLES)
-#	foreach (_variableName ${_variableNames})
-#		message(STATUS "${_variableName}=${${_variableName}}")
-#	endforeach()
-#	message(STATUS "print_all_variables------------------------------------------}")
-#endmacro()
-#
-#print_all_variables()
--- a/src/spacenav-demo-qt.cpp	Wed Mar 06 16:21:18 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,33 +0,0 @@
-/**
- * Spacenav Demo Qt
- * Copyright © 2018 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/>.
- */
-#include <stdio.h>
-
-#include <QApplication>
-#include <QThread>
-
-#include <stdlib.h>
-#include <signal.h>
-#include <X11/Xlib.h>
-#include <spnav.h>
-
-int main(int argc, char**argv) {
-	setlocale(LC_ALL, "");
-	QApplication app(argc, argv);
-
-	return 0;
-}
--- /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;
+}
+ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/spacenav-lib-hack.h	Fri Mar 08 20:11:23 2019 +0100
@@ -0,0 +1,19 @@
+/**
+ * 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/>.
+ */
+#pragma once
+