Blender V5.3
GHOST_NDOFManagerUnix.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6#include "GHOST_System.hh"
7
8/* Logging, use `ghost.ndof.unix.*` prefix. */
9#include "CLG_log.h"
10
11#include <cstdio>
12#include <cstdlib> /* For `getenv`. */
13#include <spnav.h>
14#include <unistd.h>
15
16static const char *spnav_sock_path_default = "/var/run/spnav.sock";
17
18static const char *spnav_sock_path_get()
19{
20 /* The `SPNAV_SOCKET` environment variable convention
21 * comes from LIBSPNAV 1.2, see: `src/spnav.c`. */
22 const char *env_path = getenv("SPNAV_SOCKET");
23 return env_path ? env_path : spnav_sock_path_default;
24}
25
26static CLG_LogRef LOG = {"ghost.ndof"};
27
29 : GHOST_NDOFManager(sys), available_(false)
30{
31 const char *spnav_sock_path = spnav_sock_path_get();
32 if (access(spnav_sock_path, F_OK) != 0) {
33 CLOG_DEBUG(&LOG, "'spacenavd' not found at \"%s\"", spnav_sock_path);
34 }
35 else if (spnav_open() != -1) {
36 CLOG_DEBUG(&LOG, "'spacenavd' found at\"%s\"", spnav_sock_path);
37 available_ = true;
38
39 /* Only used for logging as of LIBSPNAV 1.2. */
40 spnav_client_name("blender");
41
42 /* determine exactly which device (if any) is plugged in */
43
44#define MAX_LINE_LENGTH 100
45
46 /* look for USB devices with Logitech or 3Dconnexion's vendor ID */
47 FILE *command_output = popen("lsusb | grep '046d:\\|256f:'", "r");
48 if (command_output) {
49 char line[MAX_LINE_LENGTH] = {0};
50 while (fgets(line, MAX_LINE_LENGTH, command_output)) {
51 ushort vendor_id = 0, product_id = 0;
52 if (sscanf(line, "Bus %*d Device %*d: ID %hx:%hx", &vendor_id, &product_id) == 2) {
53 if (setDevice(vendor_id, product_id)) {
54 break; /* stop looking once the first 3D mouse is found */
55 }
56 }
57 }
58 pclose(command_output);
59 }
60 }
61}
62
64{
65 if (available_) {
66 spnav_close();
67 }
68}
69
71{
72 return available_;
73}
74
83#define USE_FINISH_GLITCH_WORKAROUND
84/* TODO: make this available on all platforms */
85
86#ifdef USE_FINISH_GLITCH_WORKAROUND
87static bool motion_test_prev = false;
89/* NOTE(ideasman42): It's important not to generate zero-motion event immediately
90 * because the SPNAV API may not have had a chance to generate events since the last
91 * call to this function.
92 * In my tests values between 50-100ms work well, opt for the larger value
93 * to avoid false positives:
94 * - Releasing the device can cause subtle wobble generating start/finish events.
95 * - Subtle motion can *sometimes* cause start/finish events too.
96 *
97 * Furthermore, waiting a 10/th of a second for the "finish" event is fast enough
98 * that any motion occurring in a shorter time-span can be considered "continuous". */
99# define MOTION_TEST_IDLE_MS 100
100#endif /* USE_FINISH_GLITCH_WORKAROUND */
101
103{
104 bool anyProcessed = false;
105
106 if (available_) {
107 spnav_event e;
108
109#ifdef USE_FINISH_GLITCH_WORKAROUND
110 bool motion_test = false;
111#endif
112
113 while (spnav_poll_event(&e)) {
114 switch (e.type) {
115 case SPNAV_EVENT_MOTION: {
116 /* convert to blender view coords */
118 const int t[3] = {int(e.motion.x), int(e.motion.y), int(-e.motion.z)};
119 const int r[3] = {int(-e.motion.rx), int(-e.motion.ry), int(e.motion.rz)};
120
121 updateTranslation(t, now);
122 updateRotation(r, now);
123#ifdef USE_FINISH_GLITCH_WORKAROUND
124 motion_test = true;
126#endif
127 break;
128 }
129 case SPNAV_EVENT_BUTTON:
131 updateButtonRAW(e.button.bnum, e.button.press, now);
132 break;
133 }
134 anyProcessed = true;
135 }
136
137#ifdef USE_FINISH_GLITCH_WORKAROUND
138 if (motion_test_prev == true && motion_test == false) {
139 const uint64_t now = system_.getMilliSeconds();
140 GHOST_ASSERT(motion_test_prev_time <= now, "Invalid time offset");
142 /* Re-run this check next time `processEvents` is called. */
143 motion_test = true;
144 }
145 else {
146 const int v[3] = {0, 0, 0};
147
148 updateTranslation(v, now);
149 updateRotation(v, now);
150
151 anyProcessed = true;
152 }
153 }
154 motion_test_prev = motion_test;
155#endif /* USE_FINISH_GLITCH_WORKAROUND */
156 }
157
158 return anyProcessed;
159}
160
161#undef USE_FINISH_GLITCH_WORKAROUND
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define GHOST_ASSERT(x, info)
static uint64_t motion_test_prev_time
static bool motion_test_prev
#define MOTION_TEST_IDLE_MS
static const char * spnav_sock_path_get()
static const char * spnav_sock_path_default
#define MAX_LINE_LENGTH
unsigned long long int uint64_t
virtual uint64_t getMilliSeconds() const =0
GHOST_NDOFManagerUnix(GHOST_System &)
void updateTranslation(const int t[3], uint64_t time)
GHOST_NDOFManager(GHOST_System &)
void updateButtonRAW(int button_number, bool press, uint64_t time)
void updateRotation(const int r[3], uint64_t time)
bool setDevice(unsigned short vendor_id, unsigned short product_id)
#define LOG(level)
Definition log.h:97