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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "libspnav_driver.h"
21
22#include <cstring>
23#include <mutex>
24
25// Static member definitions
29
33
38
40{
41 std::lock_guard<std::mutex> lock( s_mutex );
42
44 return true;
45
46 // If this is the first client, establish the connection to spacenavd
47 if( s_connection_count == 0 )
48 {
49 if( spnav_open() == -1 )
50 return false;
51
52 s_spnav_connected = true;
53 }
54
56 m_client_connected = true;
57 return true;
58}
59
61{
62 std::lock_guard<std::mutex> lock( s_mutex );
63
65 return;
66
67 m_client_connected = false;
69
70 // Close the connection when the last client disconnects
72 {
73 spnav_close();
74 s_spnav_connected = false;
75 }
76}
77
79{
80 // Only poll if this client is connected and has a handler
82 return;
83
84 // Lock only for the duration of polling to prevent race conditions
85 // but allow other clients to poll independently
86 std::lock_guard<std::mutex> lock( s_mutex );
87
89 return;
90
91 spnav_event sev;
92 while( spnav_poll_event( &sev ) > 0 )
93 {
94 if( sev.type == SPNAV_EVENT_MOTION )
95 {
96 m_handler->OnPan( sev.motion.x, sev.motion.y, sev.motion.z );
97 m_handler->OnRotate( sev.motion.rx, sev.motion.ry, sev.motion.rz );
98 }
99 else if( sev.type == SPNAV_EVENT_BUTTON )
100 {
101 m_handler->OnButton( sev.button.bnum, sev.button.press != 0 );
102 }
103 }
104}
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