KiCad PCB EDA Suite
Loading...
Searching...
No Matches
libspnav_driver.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see CHANGELOG.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "libspnav_driver.h"
25
26#include <cstring>
27#include <mutex>
28
29// Static member definitions
33
37
42
44{
45 std::lock_guard<std::mutex> lock( s_mutex );
46
48 return true;
49
50 // If this is the first client, establish the connection to spacenavd
51 if( s_connection_count == 0 )
52 {
53 if( spnav_open() == -1 )
54 return false;
55
56 s_spnav_connected = true;
57 }
58
60 m_client_connected = true;
61 return true;
62}
63
65{
66 std::lock_guard<std::mutex> lock( s_mutex );
67
69 return;
70
71 m_client_connected = false;
73
74 // Close the connection when the last client disconnects
76 {
77 spnav_close();
78 s_spnav_connected = false;
79 }
80}
81
83{
84 // Only poll if this client is connected and has a handler
86 return;
87
88 // Lock only for the duration of polling to prevent race conditions
89 // but allow other clients to poll independently
90 std::lock_guard<std::mutex> lock( s_mutex );
91
93 return;
94
95 spnav_event sev;
96 while( spnav_poll_event( &sev ) > 0 )
97 {
98 if( sev.type == SPNAV_EVENT_MOTION )
99 {
100 m_handler->OnPan( sev.motion.x, sev.motion.y, sev.motion.z );
101 m_handler->OnRotate( sev.motion.rx, sev.motion.ry, sev.motion.rz );
102 }
103 else if( sev.type == SPNAV_EVENT_BUTTON )
104 {
105 m_handler->OnButton( sev.button.bnum, sev.button.press != 0 );
106 }
107 }
108}
static int s_connection_count
bool Connect() override
Connect to the device.
void Disconnect() override
Disconnect from the device.
void Poll() override
Poll for pending events and dispatch them to the handler.
static std::mutex s_mutex
static bool s_spnav_connected
SPACEMOUSE_HANDLER * m_handler