| 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 |
|
| 25 |
#include "GuiNativeEventLinux.h" |
| 26 |
|
| 27 |
#include "GuiApplicationNativeEventAware.h" |
| 28 |
#include <Base/Console.h> |
| 29 |
#include <QMainWindow> |
| 30 |
|
| 31 |
#include <QSocketNotifier> |
| 32 |
|
| 33 |
#include <cerrno> |
| 34 |
#include <sys/socket.h> |
| 35 |
|
| 36 |
#include <spnav.h> |
| 37 |
|
| 38 |
Gui::GuiNativeEvent::GuiNativeEvent(Gui::GUIApplicationNativeEventAware* app) |
| 39 |
: GuiAbstractNativeEvent(app) |
| 40 |
{} |
| 41 |
|
| 42 |
Gui::GuiNativeEvent::~GuiNativeEvent() |
| 43 |
{ |
| 44 |
if (spnavNotifier) { |
| 45 |
if (spnav_close()) { |
| 46 |
Base::Console().log("Couldn't disconnect from spacenav daemon\n"); |
| 47 |
} |
| 48 |
else { |
| 49 |
Base::Console().log("Disconnected from spacenav daemon\n"); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
void Gui::GuiNativeEvent::initSpaceball(QMainWindow* window) |
| 55 |
{ |
| 56 |
Q_UNUSED(window) |
| 57 |
if (spnav_open() == -1) { |
| 58 |
Base::Console().log( |
| 59 |
"Couldn't connect to spacenav daemon. Please ignore if you don't have a spacemouse.\n" |
| 60 |
); |
| 61 |
} |
| 62 |
else { |
| 63 |
Base::Console().log("Connected to spacenav daemon\n"); |
| 64 |
spnavNotifier = new QSocketNotifier(spnav_fd(), QSocketNotifier::Read, this); |
| 65 |
connect(spnavNotifier, SIGNAL(activated(int)), this, SLOT(pollSpacenav())); |
| 66 |
mainApp->setSpaceballPresent(true); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void Gui::GuiNativeEvent::pollSpacenav() |
| 71 |
{ |
| 72 |
spnav_event ev; |
| 73 |
bool gotEvent = false; |
| 74 |
|
| 75 |
while (spnav_poll_event(&ev)) { |
| 76 |
gotEvent = true; |
| 77 |
switch (ev.type) { |
| 78 |
case SPNAV_EVENT_MOTION: { |
| 79 |
motionDataArray[0] = -ev.motion.x; |
| 80 |
motionDataArray[1] = -ev.motion.z; |
| 81 |
motionDataArray[2] = -ev.motion.y; |
| 82 |
motionDataArray[3] = -ev.motion.rx; |
| 83 |
motionDataArray[4] = -ev.motion.rz; |
| 84 |
motionDataArray[5] = -ev.motion.ry; |
| 85 |
mainApp->postMotionEvent(motionDataArray); |
| 86 |
break; |
| 87 |
} |
| 88 |
case SPNAV_EVENT_BUTTON: { |
| 89 |
mainApp->postButtonEvent(ev.button.bnum, ev.button.press); |
| 90 |
break; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (!gotEvent) { |
| 96 |
// QSocketNotifier fired but no events were available. |
| 97 |
// Verify the connection is still alive using a non-consuming peek. |
| 98 |
int fd = spnav_fd(); |
| 99 |
if (fd >= 0) { |
| 100 |
char buf; |
| 101 |
ssize_t ret = recv(fd, &buf, 1, MSG_PEEK | MSG_DONTWAIT); |
| 102 |
if (ret == 0 || (ret < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) { |
| 103 |
// EOF or socket error — spacenavd disconnected |
| 104 |
Base::Console().warning( |
| 105 |
"Lost connection to spacenav daemon. Restart FreeCAD to reconnect.\n" |
| 106 |
); |
| 107 |
spnavNotifier->setEnabled(false); |
| 108 |
spnav_close(); |
| 109 |
spnavNotifier = nullptr; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
#include "3Dconnexion/moc_GuiNativeEventLinux.cpp" |