MeshLib
 
Loading...
Searching...
No Matches
MRSpaceMouseHandlerHidapi.h
Go to the documentation of this file.
1#pragma once
2#ifndef __EMSCRIPTEN__
5#include "MRMesh/MRVector.h"
6
7#include <hidapi/hidapi.h>
8
9#include <atomic>
10#include <bitset>
11#include <condition_variable>
12#include <mutex>
13#include <thread>
14#include <unordered_map>
15
16#ifdef __APPLE__
17#include <hidapi/hidapi_darwin.h>
18#endif
19#ifdef _WIN32
20#include <windows.h>
21#include <hidapi/hidapi_winapi.h>
22#endif
23
24namespace MR
25{
26class MRVIEWER_CLASS SpaceMouseHandlerHidapi : public SpaceMouseHandler, public PostFocusListener
27{
28 typedef std::array<unsigned char, 13> DataPacketRaw;
29 typedef short unsigned int VendorId;
30 typedef short unsigned int ProductId;
31 struct SpaceMouseAction {
32 bool isButtonStateChanged = false;
33 std::bitset<SMB_BUTTON_COUNT> buttons = 0;
34 Vector3f translate = { 0.0f, 0.0f, 0.0f };
35 Vector3f rotate = { 0.0f, 0.0f, 0.0f };
36 };
37public:
40
41 bool initialize() override;
42 void handle() override;
43
44 // set state of zoom by mouse scroll (to fix scroll signal from SpaceMouse driver)
45 MRVIEWER_API void activateMouseScrollZoom( bool activeMouseScrollZoom );
46 // get state of zoom by mouse scroll
47 MRVIEWER_API bool isMouseScrollZoomActive()
48 {
49 return activeMouseScrollZoom_;
50 }
51
52private:
53 void initListenerThread_();
54 void setButtonsMap_( VendorId vendorId, ProductId productId );
55 virtual void postFocus_( bool focused ) override;
56
57 void processAction_( const SpaceMouseAction& action );
58 float convertCoord_( int coord_byte_low, int coord_byte_high );
59
60 // update (rewrite its data) SpaceMouseAction if DataPacketRaw is not empty
61 void updateActionWithInput_( const DataPacketRaw& packet, int packet_length, SpaceMouseAction& action );
62
63 bool findAndAttachDevice_( bool verbose );
64 void printDevices_( struct hid_device_info* cur_dev );
65
66private:
67 hid_device* device_ = nullptr;
68 const std::vector<std::vector<SpaceMouseButtons>>* buttonsMapPtr_ = nullptr;
69 std::bitset<SMB_BUTTON_COUNT> buttonsState_;
70 std::thread listenerThread_;
71 std::atomic_bool terminateListenerThread_{ false };
72 std::mutex syncThreadMutex_; // which thread reads and handles SpaceMouse data
73 std::condition_variable cv_; // notify on thread change
74 DataPacketRaw dataPacket_; // packet from listener thread
75 int packetLength_ = 0;
76 std::atomic_bool active_{ false };
77 bool activeMouseScrollZoom_ = false;
78
79 // if you change this value, do not forget to update MeshLib/scripts/70-space-mouse-meshlib.rules
80 const std::unordered_map<VendorId, std::vector<ProductId>> vendor2device_ = {
81 { VendorId(0x046d), { // Logitech (3Dconnexion was a subsidiary)
82 0xc603, // SpaceMouse plus XT
83 0xc605, // cadman
84 0xc606, // SpaceMouse classic
85 0xc621, // spaceball 5000
86 0xc623, // space traveller
87 0xc625, // space pilot
88 0xc626, // Full-size SpaceNavigator
89 0xc627, // space explorer
90 0xc628, // SpaceNavigator for notebooks
91 0xc629, // space pilot pro
92 0xc62b, // space mouse pro
93 0xc640 // nulooq
94 }},
95 { VendorId(0x256f), { // 3Dconnexion
96 0xc62e, // SpaceMouse wireless (USB cable)
97 0xc62f, // SpaceMouse wireless receiver
98 0xc631, // SpaceMouse pro wireless (USB cable)
99 0xc632, // SpaceMouse pro wireless receiver
100 0xc633, // SpaceMouse enterprise
101 0xc635, // SpaceMouse compact
102 0xc638, // SpaceMouse Pro Wireless Bluetooth Edition (USB cable)
103 0xc652, // Universal receiver
104 0xc658 // Wireless (3DConnexion Universal Wireless Receiver in WIN32)
105 }}
106 };
107
108 /* | <--- packet values --->
109 * #byte | 1 2
110 * ------+--------------------------
111 * 0 | - -
112 * 1 | custom_1 custom_2
113 */
114 std::vector<std::vector<SpaceMouseButtons>> buttonMapCompact = {
115 { }, // 0th byte (unused)
116 { SMB_CUSTOM_1, SMB_CUSTOM_2} // 1st byte
117 };
118
119 /* | <--- packet values --->
120 * #byte | 1 2 4 8 16 32 64 126
121 * ------+------------------------------------------------------------------
122 * 1 | menu fit T R F
123 * 2 | rot 1 2 3 4
124 * 3 | esc alt
125 * 4 | shift ctrl lock
126 *
127 */
128 std::vector<std::vector<SpaceMouseButtons>> buttonMapPro = {
129 { }, // 0th byte (unused)
130 //1 2 4 8 16 32 64 128
133 { SMB_NO, SMB_NO, SMB_NO, SMB_NO, SMB_NO, SMB_NO, SMB_ESC, SMB_ALT}, // 3rd byte
135 };
136
137 // @TODO !!! NOT TESTED !!!
138 std::vector<std::vector<SpaceMouseButtons>> buttonMapEnterprise = {
139 { }, // 0th byte (unused)
141 };
142 /* @TODO !!! NOT TESTED !!!
143 static constexpr int mapButtonsEnterprise[31] = {
144 SMB_MENU, SMB_FIT,
145 SMB_TOP, SMB_RIGHT, SMB_FRONT, SMB_ROLL_CW, SMB_LOCK_ROT,
146 SMB_ISO1, SMB_BTN_V1, SMB_BTN_V2, SMB_BTN_V3,
147 SMB_CUSTOM_1, SMB_CUSTOM_2, SMB_CUSTOM_3, SMB_CUSTOM_4, SMB_CUSTOM_5, SMB_CUSTOM_6,
148 SMB_CUSTOM_7, SMB_CUSTOM_8, SMB_CUSTOM_9, SMB_CUSTOM_10, SMB_CUSTOM_11, SMB_CUSTOM_12,
149 SMB_ESC, SMB_ENTER, SMB_ALT, SMB_SHIFT, SMB_CTRL, SMB_TAB, SMB_SPACE, SMB_DELETE
150 };
151 */
152};
153
154}
155#endif
Definition MRSpaceMouseHandlerHidapi.h:27
MRVIEWER_API bool isMouseScrollZoomActive()
Definition MRSpaceMouseHandlerHidapi.h:47
MRVIEWER_API void activateMouseScrollZoom(bool activeMouseScrollZoom)
bool initialize() override
initialize device
void handle() override
handle device state and call Viewer signals
class to handle spacemouse
Definition MRSpaceMouseHandler.h:55
represents a 3-dimentional float-typed vector
Definition MRDotNet/MRVector3.h:8
Definition MRCameraOrientationPlugin.h:7
@ SMB_SHIFT
Definition MRSpaceMouseHandler.h:18
@ SMB_ALT
Definition MRSpaceMouseHandler.h:20
@ SMB_FIT
Definition MRSpaceMouseHandler.h:37
@ SMB_RIGHT
Definition MRSpaceMouseHandler.h:39
@ SMB_MENU
Definition MRSpaceMouseHandler.h:13
@ SMB_ROLL_CW
Definition MRSpaceMouseHandler.h:41
@ SMB_NO
Definition MRSpaceMouseHandler.h:12
@ SMB_CUSTOM_1
Definition MRSpaceMouseHandler.h:24
@ SMB_CUSTOM_3
Definition MRSpaceMouseHandler.h:26
@ SMB_CUSTOM_2
Definition MRSpaceMouseHandler.h:25
@ SMB_FRONT
Definition MRSpaceMouseHandler.h:40
@ SMB_ESC
Definition MRSpaceMouseHandler.h:15
@ SMB_TOP
Definition MRSpaceMouseHandler.h:38
@ SMB_CUSTOM_4
Definition MRSpaceMouseHandler.h:27
@ SMB_LOCK_ROT
Definition MRSpaceMouseHandler.h:42
@ SMB_CTRL
Definition MRSpaceMouseHandler.h:19
class to subscribe on PostFocusSingal
Definition MRViewerEventsListener.h:370