# HG changeset patch # User František Kučera # Date 1552072283 -3600 # Node ID ac5f3768ebb13d292241928082a3de6e1b3f1eae # Parent 37aa134ae57b7a20f90242829598ba27d8844b80 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]) diff -r 37aa134ae57b -r ac5f3768ebb1 CMakeLists.txt --- 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 . -project (spacenav-demo-qt) +project (spacenav-lib-hack) cmake_minimum_required(VERSION 3.7.2) add_subdirectory (src) diff -r 37aa134ae57b -r ac5f3768ebb1 nbproject/configurations.xml --- 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 @@ - + mocs_compilation.cpp - spacenav-demo-qt.cpp + spacenav-lib-hack.c - - build/Debug/src/spacenav-demo-qt_autogen/mocs_compilation.cpp + root="build/Debug/src/spacenav-lib-hack_autogen"> + build/Debug/src/spacenav-lib-hack_autogen/mocs_compilation.cpp + + @@ -98,23 +100,19 @@ build/Debug ${MAKE} -f Makefile ${MAKE} -f Makefile clean - build/Debug/src/spacenav-demo-qt + build/Debug/src/spacenav-lib-hack + + + build/Debug/src + + + spnav_lib_hack_EXPORTS + + build/Debug/src - src - build/Debug/src/spacenav-demo-qt_autogen/include - /usr/include/x86_64-linux-gnu/qt5 - /usr/include/x86_64-linux-gnu/qt5/QtWidgets - /usr/include/x86_64-linux-gnu/qt5/QtGui - /usr/include/x86_64-linux-gnu/qt5/QtCore - /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ - - QT_CORE_LIB - QT_GUI_LIB - QT_WIDGETS_LIB - @@ -127,24 +125,63 @@ ex="false" tool="1" flavor2="11"> + + - + + + + spnav_lib_hack_EXPORTS + + + + src + build/Debug/src/spacenav-lib-hack_autogen/include + /usr/include/x86_64-linux-gnu/qt5 + /usr/include/x86_64-linux-gnu/qt5/QtWidgets + /usr/include/x86_64-linux-gnu/qt5/QtGui + /usr/include/x86_64-linux-gnu/qt5/QtCore + /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ /usr/include + + QT_CORE_LIB + QT_GUI_LIB + QT_WIDGETS_LIB + - - + + + + src + build/Debug/src/spacenav-lib-hack_autogen/include + /usr/include/x86_64-linux-gnu/qt5 + /usr/include/x86_64-linux-gnu/qt5/QtWidgets + /usr/include/x86_64-linux-gnu/qt5/QtGui + /usr/include/x86_64-linux-gnu/qt5/QtCore + /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ + + + QT_CORE_LIB + QT_GUI_LIB + QT_WIDGETS_LIB + + + + + @@ -168,7 +205,7 @@ build/Release/src src - build/Release/src/spacenav-demo-qt_autogen/include + build/Release/src/spacenav-lib-hack_autogen/include /usr/include/x86_64-linux-gnu/qt5 /usr/include/x86_64-linux-gnu/qt5/QtWidgets /usr/include/x86_64-linux-gnu/qt5/QtGui @@ -194,7 +231,7 @@ tool="1" flavor2="0"> - @@ -215,10 +252,6 @@ - - - - diff -r 37aa134ae57b -r ac5f3768ebb1 nbproject/project.xml --- 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 @@ org.netbeans.modules.cnd.makeproject - spacenav-demo-qt - + spacenav-lib-hack + c cpp h UTF-8 diff -r 37aa134ae57b -r ac5f3768ebb1 src/CMakeLists.txt --- 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 . -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() diff -r 37aa134ae57b -r ac5f3768ebb1 src/spacenav-demo-qt.cpp --- 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 . - */ -#include - -#include -#include - -#include -#include -#include -#include - -int main(int argc, char**argv) { - setlocale(LC_ALL, ""); - QApplication app(argc, argv); - - return 0; -} diff -r 37aa134ae57b -r ac5f3768ebb1 src/spacenav-lib-hack.c --- /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 . + */ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include + +#include +#include + +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 + * 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; +} + */ diff -r 37aa134ae57b -r ac5f3768ebb1 src/spacenav-lib-hack.h --- /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 . + */ +#pragma once +