Debian Codegraph @

src/Gui/3Dconnexion/GuiNativeEventLinuxX11.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2018 Torsten Sadowski <tsadowski[at]gmx.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <FCConfig.h>
24 #include "SpaceballEvent.h"
25
26 #include <QMainWindow>
27
28 #include "GuiNativeEventLinuxX11.h"
29
30 #include "GuiApplicationNativeEventAware.h"
31 #include <Base/Console.h>
32
33 #include <QX11Info>
34 #include <spnav.h>
35
36 #include "GuiRawInputEventFilter.h"
37 #undef Bool
38 #undef CursorShape
39 #undef Expose
40 #undef KeyPress
41 #undef KeyRelease
42 #undef FocusIn
43 #undef FocusOut
44 #undef FontChange
45 #undef None
46 #undef Status
47 #undef Unsorted
48 #undef False
49 #undef True
50 #undef Complex
51
52
53 Gui::GuiNativeEvent::GuiNativeEvent(Gui::GUIApplicationNativeEventAware* app)
54 : GuiAbstractNativeEvent(app)
55 {}
56
57 Gui::GuiNativeEvent::~GuiNativeEvent()
58 {
59 if (spnav_close()) {
60 Base::Console().log("Couldn't disconnect from spacenav daemon\n");
61 }
62 else {
63 Base::Console().log("Disconnected from spacenav daemon\n");
64 }
65 }
66
67 void Gui::GuiNativeEvent::initSpaceball(QMainWindow* window)
68 {
69 if (!QX11Info::isPlatformX11()) {
70 Base::Console().log("Application is not running on X11\n");
71 return;
72 }
73 if (spnav_x11_open(QX11Info::display(), window->winId()) == -1) {
74 Base::Console()
75 .log("Couldn't connect to spacenav daemon on X11. Please ignore if you don't have a spacemouse.\n");
76 }
77 else {
78 Base::Console().log("Connected to spacenav daemon on X11\n");
79 mainApp->setSpaceballPresent(true);
80 mainApp->installNativeEventFilter(new Gui::RawInputEventFilter(&xcbEventFilter));
81 }
82 }
83
84 bool Gui::GuiNativeEvent::xcbEventFilter(void* xcb_void, long* result)
85 {
86 Q_UNUSED(result);
87 auto inst(dynamic_cast<Gui::GUIApplicationNativeEventAware*>(QApplication::instance()));
88 if (!inst) {
89 return false;
90 }
91
92 spnav_event navEvent;
93
94 const xcb_client_message_event_t* xcb_ev = static_cast<const xcb_client_message_event_t*>(xcb_void);
95 // Qt4 used XEvents in native event filters, but Qt5 changed to XCB. The
96 // SpaceNavigator API only works with XEvent, so we need to construct a
97 // temporary XEvent with just enough information for spnav_x11_event()
98 if ((xcb_ev->response_type & 0x7F) == XCB_CLIENT_MESSAGE) {
99 XClientMessageEvent xev;
100
101 xev.type = ClientMessage;
102 xev.message_type = xcb_ev->type;
103 memcpy(xev.data.b, xcb_ev->data.data8, sizeof(xev.data.b));
104 xev.serial = 0; // These are just to squash warnings...
105 xev.send_event = 0;
106 xev.display = 0;
107 xev.window = 0;
108 xev.format = 0;
109
110 if (!spnav_x11_event(reinterpret_cast<XEvent*>(&xev), &navEvent)) {
111 return false;
112 }
113 }
114 else {
115 return false;
116 }
117 // navEvent is now initialised
118
119 switch (navEvent.type) {
120 case SPNAV_EVENT_MOTION: {
121 motionDataArray[0] = -navEvent.motion.x;
122 motionDataArray[1] = -navEvent.motion.z;
123 motionDataArray[2] = -navEvent.motion.y;
124 motionDataArray[3] = -navEvent.motion.rx;
125 motionDataArray[4] = -navEvent.motion.rz;
126 motionDataArray[5] = -navEvent.motion.ry;
127
128 inst->postMotionEvent(motionDataArray);
129 return true;
130 }
131
132 case SPNAV_EVENT_BUTTON: {
133 auto buttonEvent(new Spaceball::ButtonEvent());
134 buttonEvent->setButtonNumber(navEvent.button.bnum);
135 if (navEvent.button.press) {
136 buttonEvent->setButtonStatus(Spaceball::BUTTON_PRESSED);
137 }
138 else {
139 buttonEvent->setButtonStatus(Spaceball::BUTTON_RELEASED);
140 }
141 inst->postButtonEvent(navEvent.button.bnum, navEvent.button.press);
142 return true;
143 }
144 default:
145 Base::Console().log("Unknown spaceball event\n");
146 return true;
147 } // end switch (navEvent.type) {
148 }
149
150 #include "3Dconnexion/moc_GuiNativeEventLinuxX11.cpp"