ApolloInput Nova 0.2.0
Standalone virtual gamepad library
Loading...
Searching...
No Matches
uinput_backend.cpp
Go to the documentation of this file.
1
6
8
9#ifdef __linux__
10
11 #include <linux/input.h>
12 #include <linux/uinput.h>
13
14 #include <fcntl.h>
15 #include <sys/ioctl.h>
16 #include <unistd.h>
17
18 #include <array>
19 #include <cerrno>
20 #include <cstdint>
21 #include <cstdio>
22 #include <cstring>
23 #include <mutex>
24 #include <string>
25 #include <utility>
26
27namespace apollo::input::detail {
28 namespace {
38 bool emit_event(int fd, std::uint16_t type, std::uint16_t code, std::int32_t value) {
39 input_event event {};
40 event.type = type;
41 event.code = code;
42 event.value = value;
43 return ::write(fd, &event, sizeof(event)) == static_cast<ssize_t>(sizeof(event));
44 }
45
49 struct button_mapping_t {
50 Button button;
51 std::uint16_t code;
52 };
53
61 constexpr std::array BUTTON_MAPPINGS {
62 button_mapping_t {Button::a, BTN_A},
63 button_mapping_t {Button::b, BTN_B},
64 button_mapping_t {Button::x, BTN_X},
65 button_mapping_t {Button::y, BTN_Y},
66 button_mapping_t {Button::left_shoulder, BTN_TL},
67 button_mapping_t {Button::right_shoulder, BTN_TR},
68 button_mapping_t {Button::start, BTN_START},
69 button_mapping_t {Button::back, BTN_SELECT},
70 button_mapping_t {Button::guide, BTN_MODE},
71 button_mapping_t {Button::left_thumb, BTN_THUMBL},
72 button_mapping_t {Button::right_thumb, BTN_THUMBR},
73 button_mapping_t {Button::dpad_up, BTN_DPAD_UP},
74 button_mapping_t {Button::dpad_down, BTN_DPAD_DOWN},
75 button_mapping_t {Button::dpad_left, BTN_DPAD_LEFT},
76 button_mapping_t {Button::dpad_right, BTN_DPAD_RIGHT},
77 button_mapping_t {Button::paddle1, BTN_TRIGGER_HAPPY1},
78 button_mapping_t {Button::paddle2, BTN_TRIGGER_HAPPY2},
79 button_mapping_t {Button::paddle3, BTN_TRIGGER_HAPPY3},
80 button_mapping_t {Button::paddle4, BTN_TRIGGER_HAPPY4},
81 button_mapping_t {Button::share, BTN_TRIGGER_HAPPY5},
82 button_mapping_t {Button::touchpad_click, BTN_TRIGGER_HAPPY6},
83 button_mapping_t {Button::misc2, BTN_TRIGGER_HAPPY7},
84 };
85
96 std::uint32_t effective_buttons(const GamepadState &state) {
97 auto buttons = state.buttons;
98 buttons &= ~button_bit(Button::options);
99 buttons &= ~button_bit(Button::touchpad);
100 if (state.pressed(Button::options)) {
101 buttons |= button_bit(Button::start);
102 }
103 if (state.pressed(Button::touchpad)) {
105 }
106 return buttons;
107 }
108
112 class UinputDevice final: public Device {
113 public:
114 UinputDevice(Profile profile, std::string name):
115 info_ {profile, capabilities_for(profile), name.empty() ? "Apollo Virtual Gamepad" : std::move(name)} {
116 const auto identity = profile_identity(profile);
117
118 fd_ = ::open("/dev/uinput", O_WRONLY | O_NONBLOCK);
119 if (fd_ < 0) {
120 return;
121 }
122
123 ioctl(fd_, UI_SET_EVBIT, EV_KEY);
124 ioctl(fd_, UI_SET_EVBIT, EV_ABS);
125 for (const auto &mapping : BUTTON_MAPPINGS) {
126 ioctl(fd_, UI_SET_KEYBIT, mapping.code);
127 }
128
129 for (const auto code : {ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_Z, ABS_RZ, ABS_HAT0X, ABS_HAT0Y}) {
130 ioctl(fd_, UI_SET_ABSBIT, code);
131 }
132
133 uinput_user_dev device {};
134 std::snprintf(device.name, sizeof(device.name), "%s", info_.name.c_str());
135 device.id.bustype = BUS_USB;
136 device.id.vendor = identity.vendor_id;
137 device.id.product = identity.product_id;
138 device.id.version = identity.version;
139
140 set_abs(device, ABS_X, -32768, 32767);
141 set_abs(device, ABS_Y, -32768, 32767);
142 set_abs(device, ABS_RX, -32768, 32767);
143 set_abs(device, ABS_RY, -32768, 32767);
144 set_abs(device, ABS_Z, 0, 255);
145 set_abs(device, ABS_RZ, 0, 255);
146 set_abs(device, ABS_HAT0X, -1, 1);
147 set_abs(device, ABS_HAT0Y, -1, 1);
148
149 if (::write(fd_, &device, sizeof(device)) != static_cast<ssize_t>(sizeof(device))) {
150 ::close(fd_);
151 fd_ = -1;
152 return;
153 }
154 if (ioctl(fd_, UI_DEV_CREATE) < 0) {
155 ::close(fd_);
156 fd_ = -1;
157 return;
158 }
159
160 connected_ = true;
161 sync_();
162 }
163
164 ~UinputDevice() override {
165 disconnect();
166 }
167
168 const DeviceInfo &info() const override {
169 return info_;
170 }
171
172 bool connected() const override {
173 return connected_;
174 }
175
176 bool update(const GamepadState &state) override {
177 std::scoped_lock lock(mutex_);
178 if (!connected_) {
179 return false;
180 }
181
182 auto sanitized = state;
183 sanitize_gamepad_state(sanitized, deadzone_);
184
185 const auto previous = effective_buttons(state_);
186 state_ = sanitized;
187 const auto current = effective_buttons(state_);
188
189 emit_buttons_(previous, current);
190 emit_abs_(ABS_X, previous_state_.left_x, sanitized.left_x);
191 emit_abs_(ABS_Y, previous_state_.left_y, sanitized.left_y);
192 emit_abs_(ABS_RX, previous_state_.right_x, sanitized.right_x);
193 emit_abs_(ABS_RY, previous_state_.right_y, sanitized.right_y);
194 emit_abs_(ABS_Z, previous_state_.left_trigger, sanitized.left_trigger);
195 emit_abs_(ABS_RZ, previous_state_.right_trigger, sanitized.right_trigger);
196
197 const int hat_x = (sanitized.pressed(Button::dpad_right) ? 1 : 0) - (sanitized.pressed(Button::dpad_left) ? 1 : 0);
198 const int hat_y = (sanitized.pressed(Button::dpad_down) ? 1 : 0) - (sanitized.pressed(Button::dpad_up) ? 1 : 0);
199 emit_abs_(ABS_HAT0X, last_hat_x_, hat_x);
200 emit_abs_(ABS_HAT0Y, last_hat_y_, hat_y);
201 last_hat_x_ = hat_x;
202 last_hat_y_ = hat_y;
203
204 previous_state_ = sanitized;
205 return sync_();
206 }
207
208 void set_deadzone(const AnalogDeadzone &deadzone) override {
209 std::scoped_lock lock(mutex_);
210 deadzone_ = deadzone;
211 }
212
213 void disconnect() override {
214 std::scoped_lock lock(mutex_);
215 if (!connected_) {
216 return;
217 }
218 connected_ = false;
219 ioctl(fd_, UI_DEV_DESTROY);
220 ::close(fd_);
221 fd_ = -1;
222 }
223
224 private:
235 static Capabilities capabilities_for(Profile profile) {
236 auto capabilities = profile_capabilities(profile);
237 capabilities.rumble = false;
238 capabilities.trigger_rumble = false;
239 capabilities.leds = false;
240 capabilities.imu = false;
241 capabilities.touchpad = false;
242 capabilities.adaptive_triggers = false;
243 capabilities.player_leds = false;
244 capabilities.battery = false;
245 return capabilities;
246 }
247
251 static void set_abs(uinput_user_dev &device, std::uint16_t code, int minimum, int maximum) {
252 device.absmin[code] = minimum;
253 device.absmax[code] = maximum;
254 }
255
259 void emit_buttons_(std::uint32_t previous, std::uint32_t current) {
260 for (const auto &mapping : BUTTON_MAPPINGS) {
261 const bool was_pressed = (previous & button_bit(mapping.button)) != 0;
262 const bool is_pressed = (current & button_bit(mapping.button)) != 0;
263 if (was_pressed != is_pressed) {
264 emit_event(fd_, EV_KEY, mapping.code, is_pressed ? 1 : 0);
265 }
266 }
267 }
268
272 void emit_abs_(std::uint16_t code, std::int32_t previous, std::int32_t current) {
273 if (previous != current) {
274 emit_event(fd_, EV_ABS, code, current);
275 }
276 }
277
281 bool sync_() {
282 return emit_event(fd_, EV_SYN, SYN_REPORT, 0);
283 }
284
285 int fd_ = -1;
286 DeviceInfo info_;
287 AnalogDeadzone deadzone_;
288 GamepadState state_;
289 GamepadState previous_state_;
290 int last_hat_x_ = 0;
291 int last_hat_y_ = 0;
292 std::mutex mutex_;
293 bool connected_ = false;
294 };
295
299 class UinputBackend final: public Backend {
300 public:
301 const char *name() const override {
302 return "uinput";
303 }
304
305 bool available(std::string *error) const override {
306 if (::access("/dev/uinput", W_OK) == 0) {
307 if (error) {
308 error->clear();
309 }
310 return true;
311 }
312 if (error) {
313 *error = std::string {"/dev/uinput is not writable: "} + std::strerror(errno);
314 }
315 return false;
316 }
317
318 std::unique_ptr<Device> create(Profile profile, const std::string &name, std::string *error) override {
319 auto device = std::make_unique<UinputDevice>(profile, name);
320 if (!device->connected()) {
321 if (error) {
322 *error = "Unable to create a uinput device (check /dev/uinput permissions)";
323 }
324 return nullptr;
325 }
326 if (error) {
327 error->clear();
328 }
329 return device;
330 }
331
332 std::vector<Profile> supported_profiles() const override {
333 return {
334 Profile::xbox_360,
335 Profile::xbox_one,
336 Profile::xbox_series,
337 Profile::xbox_elite,
338 Profile::dualshock4,
339 Profile::dualsense,
340 Profile::dualsense_edge,
341 Profile::switch_pro,
342 Profile::steam_controller,
343 Profile::steam_deck,
344 Profile::generic,
345 };
346 }
347 };
348 } // namespace
349
350 std::unique_ptr<Backend> create_uinput_backend() {
351 return std::make_unique<UinputBackend>();
352 }
353} // namespace apollo::input::detail
354
355#endif
Backend and device interfaces for the Apollo input library.
Internal backend factory declarations.
Capabilities profile_capabilities(Profile profile)
Features a profile can expose when the backend supports them.
Definition gamepad.cpp:201
void sanitize_gamepad_state(GamepadState &state, const AnalogDeadzone &deadzone={})
Remove stick drift, trigger resting noise, and center jitter.
Definition gamepad.cpp:484
constexpr std::uint32_t button_bit(Button button)
Bit mask for one digital button.
Definition gamepad.hpp:98
Button
Digital gamepad buttons shared by all backends.
Definition gamepad.hpp:65
@ misc2
Second miscellaneous button, when a controller exposes one.
Definition gamepad.hpp:89
ProfileIdentity profile_identity(Profile profile)
USB identity associated with a profile.
Definition gamepad.cpp:277