MeshLib C++ Docs
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 "MRPch/MRWinapi.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( std::function<void(const std::string&)> deviceSignal ) 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
65private:
66 std::function<void(const std::string&)> deviceSignal_;
67 hid_device* device_ = nullptr;
68 bool anyAction_ = false;
69 const std::vector<std::vector<SpaceMouseButtons>>* buttonsMapPtr_ = nullptr;
70 std::bitset<SMB_BUTTON_COUNT> buttonsState_;
71 std::thread listenerThread_;
72 std::atomic_bool terminateListenerThread_{ false };
73 std::mutex syncThreadMutex_; // which thread reads and handles SpaceMouse data
74 std::condition_variable cv_; // notify on thread change
75 DataPacketRaw dataPacket_; // packet from listener thread
76 int packetLength_ = 0;
77 std::atomic_bool active_{ false };
78 bool activeMouseScrollZoom_ = false;
79
80 // if you change this value, do not forget to update MeshLib/scripts/70-space-mouse-meshlib.rules
81 const std::unordered_map<VendorId, std::vector<ProductId>> vendor2device_ = {
82 { VendorId(0x046d), { // Logitech (3Dconnexion was a subsidiary)
83 0xc603, // SpaceMouse plus XT
84 0xc605, // cadman
85 0xc606, // SpaceMouse classic
86 0xc621, // spaceball 5000
87 0xc623, // space traveller
88 0xc625, // space pilot
89 0xc626, // Full-size SpaceNavigator
90 0xc627, // space explorer
91 0xc628, // SpaceNavigator for notebooks
92 0xc629, // space pilot pro
93 0xc62b, // space mouse pro
94 0xc640 // nulooq
95 }},
96 { VendorId(0x256f), { // 3Dconnexion
97 0xc62e, // SpaceMouse wireless (USB cable)
98 0xc62f, // SpaceMouse wireless receiver
99 0xc631, // SpaceMouse pro wireless (USB cable)
100 0xc632, // SpaceMouse pro wireless receiver
101 0xc633, // SpaceMouse enterprise
102 0xc635, // SpaceMouse compact
103 0xc638, // SpaceMouse Pro Wireless Bluetooth Edition (USB cable)
104 0xc63a, // SpaceMouse Wireless BT
105 0xc652, // Universal receiver
106 0xc658 // Wireless (3DConnexion Universal Wireless Receiver in WIN32)
107 }}
108 };
109
110 /* | <--- packet values --->
111 * #byte | 1 2
112 * ------+--------------------------
113 * 0 | - -
114 * 1 | custom_1 custom_2
115 */
116 std::vector<std::vector<SpaceMouseButtons>> buttonMapCompact = {
117 { }, // 0th byte (unused)
118 { SMB_CUSTOM_1, SMB_CUSTOM_2} // 1st byte
119 };
120
121 /* | <--- packet values --->
122 * #byte | 1 2 4 8 16 32 64 126
123 * ------+------------------------------------------------------------------
124 * 1 | menu fit T R F
125 * 2 | rot 1 2 3 4
126 * 3 | esc alt
127 * 4 | shift ctrl lock
128 *
129 */
130 std::vector<std::vector<SpaceMouseButtons>> buttonMapPro = {
131 { }, // 0th byte (unused)
132 //1 2 4 8 16 32 64 128
135 { SMB_NO, SMB_NO, SMB_NO, SMB_NO, SMB_NO, SMB_NO, SMB_ESC, SMB_ALT}, // 3rd byte
137 };
138
139 // @TODO !!! NOT TESTED !!!
140 std::vector<std::vector<SpaceMouseButtons>> buttonMapEnterprise = {
141 { }, // 0th byte (unused)
143 };
144 /* @TODO !!! NOT TESTED !!!
145 static constexpr int mapButtonsEnterprise[31] = {
146 SMB_MENU, SMB_FIT,
147 SMB_TOP, SMB_RIGHT, SMB_FRONT, SMB_ROLL_CW, SMB_LOCK_ROT,
148 SMB_ISO1, SMB_BTN_V1, SMB_BTN_V2, SMB_BTN_V3,
149 SMB_CUSTOM_1, SMB_CUSTOM_2, SMB_CUSTOM_3, SMB_CUSTOM_4, SMB_CUSTOM_5, SMB_CUSTOM_6,
150 SMB_CUSTOM_7, SMB_CUSTOM_8, SMB_CUSTOM_9, SMB_CUSTOM_10, SMB_CUSTOM_11, SMB_CUSTOM_12,
151 SMB_ESC, SMB_ENTER, SMB_ALT, SMB_SHIFT, SMB_CTRL, SMB_TAB, SMB_SPACE, SMB_DELETE
152 };
153 */
154};
155
156}
157#endif
Definition MRSpaceMouseHandlerHidapi.h:27
MRVIEWER_API bool isMouseScrollZoomActive()
Definition MRSpaceMouseHandlerHidapi.h:47
MRVIEWER_API void activateMouseScrollZoom(bool activeMouseScrollZoom)
void handle() override
handle device state and call Viewer signals
bool initialize(std::function< void(const std::string &)> deviceSignal) override
base class for handler of spacemouse devices
Definition MRSpaceMouseHandler.h:56
Definition MRCameraOrientationPlugin.h:8
@ SMB_SHIFT
Definition MRSpaceMouseHandler.h:19
@ SMB_ALT
Definition MRSpaceMouseHandler.h:21
@ SMB_FIT
Definition MRSpaceMouseHandler.h:38
@ SMB_RIGHT
Definition MRSpaceMouseHandler.h:40
@ SMB_MENU
Definition MRSpaceMouseHandler.h:14
@ SMB_ROLL_CW
Definition MRSpaceMouseHandler.h:42
@ SMB_NO
Definition MRSpaceMouseHandler.h:13
@ SMB_CUSTOM_1
Definition MRSpaceMouseHandler.h:25
@ SMB_CUSTOM_3
Definition MRSpaceMouseHandler.h:27
@ SMB_CUSTOM_2
Definition MRSpaceMouseHandler.h:26
@ SMB_FRONT
Definition MRSpaceMouseHandler.h:41
@ SMB_ESC
Definition MRSpaceMouseHandler.h:16
@ SMB_TOP
Definition MRSpaceMouseHandler.h:39
@ SMB_CUSTOM_4
Definition MRSpaceMouseHandler.h:28
@ SMB_LOCK_ROT
Definition MRSpaceMouseHandler.h:43
@ SMB_CTRL
Definition MRSpaceMouseHandler.h:20
class to subscribe on PostFocusSingal
Definition MRViewerEventsListener.h:388