Panda3D
evdevInputDevice.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file evdevInputDevice.cxx
10  * @author rdb
11  * @date 2015-08-24
12  */
13 
14 #include "evdevInputDevice.h"
15 
16 #ifdef PHAVE_LINUX_INPUT_H
17 
18 #include "gamepadButton.h"
19 #include "keyboardButton.h"
20 #include "mouseButton.h"
22 
23 #include <fcntl.h>
24 #include <linux/input.h>
25 
26 #ifndef BTN_DPAD_UP
27 #define BTN_DPAD_UP 0x220
28 #define BTN_DPAD_DOWN 0x221
29 #define BTN_DPAD_LEFT 0x222
30 #define BTN_DPAD_RIGHT 0x223
31 #endif
32 
33 
34 // Android introduces these in API level 21.
35 #ifndef BTN_TRIGGER_HAPPY
36 #define BTN_TRIGGER_HAPPY 0x2c0
37 #define BTN_TRIGGER_HAPPY1 0x2c0
38 #define BTN_TRIGGER_HAPPY2 0x2c1
39 #define BTN_TRIGGER_HAPPY3 0x2c2
40 #define BTN_TRIGGER_HAPPY4 0x2c3
41 #endif
42 
43 #define test_bit(bit, array) ((array)[(bit)>>3] & (1<<((bit)&7)))
44 
45 enum QuirkBits {
46  // Right stick uses Z and Rz inputs.
47  QB_rstick_from_z = 1,
48 
49  // Throttle goes from -1 to 1 rather than from 0 to 1.
50  QB_centered_throttle = 2,
51 
52  // Throttle is reversed.
53  QB_reversed_throttle = 4,
54 
55  // Only consider the device "connected" if all axes are non-zero.
56  QB_connect_if_nonzero = 8,
57 
58  // ABS_THROTTLE maps to rudder
59  QB_rudder_from_throttle = 16,
60 
61  // Special handling for Steam Controller, which has many peculiarities.
62  // We only connect it if it is reporting any events, because when Steam is
63  // running, the Steam controller is muted in favour of a dummy Xbox device.
64  QB_steam_controller = 32,
65 
66  // Axes on the right stick are swapped, using x for y and vice versa.
67  QB_right_axes_swapped = 64,
68 
69  // Has no trigger axes.
70  QB_no_analog_triggers = 128,
71 
72  // Alternate button mapping.
73  QB_alt_button_mapping = 256,
74 };
75 
76 static const struct DeviceMapping {
77  unsigned short vendor;
78  unsigned short product;
79  InputDevice::DeviceClass device_class;
80  int quirks;
81 } mapping_presets[] = {
82  // NVIDIA Shield Controller
83  {0x0955, 0x7214, InputDevice::DeviceClass::gamepad, QB_rstick_from_z},
84  // T.Flight Hotas X
85  {0x044f, 0xb108, InputDevice::DeviceClass::flight_stick, QB_centered_throttle | QB_reversed_throttle | QB_rudder_from_throttle},
86  // Xbox 360 Wireless Controller
87  {0x045e, 0x0719, InputDevice::DeviceClass::gamepad, QB_connect_if_nonzero},
88  // Steam Controller (wired)
89  {0x28de, 0x1102, InputDevice::DeviceClass::unknown, QB_steam_controller},
90  // Steam Controller (wireless)
91  {0x28de, 0x1142, InputDevice::DeviceClass::unknown, QB_steam_controller},
92  // Jess Tech Colour Rumble Pad
93  {0x0f30, 0x0111, InputDevice::DeviceClass::gamepad, QB_rstick_from_z | QB_right_axes_swapped},
94  // Trust GXT 24
95  {0x0079, 0x0006, InputDevice::DeviceClass::gamepad, QB_no_analog_triggers | QB_alt_button_mapping},
96  // 8bitdo N30 Pro Controller
97  {0x2dc8, 0x9001, InputDevice::DeviceClass::gamepad, QB_rstick_from_z},
98  // Generic gamepad
99  {0x0810, 0x0001, InputDevice::DeviceClass::gamepad, QB_no_analog_triggers | QB_alt_button_mapping | QB_rstick_from_z | QB_right_axes_swapped},
100  // Generic gamepad without sticks
101  {0x0810, 0xe501, InputDevice::DeviceClass::gamepad, QB_no_analog_triggers | QB_alt_button_mapping},
102  // 3Dconnexion Space Traveller 3D Mouse
103  {0x046d, 0xc623, InputDevice::DeviceClass::spatial_mouse, 0},
104  // 3Dconnexion Space Pilot 3D Mouse
105  {0x046d, 0xc625, InputDevice::DeviceClass::spatial_mouse, 0},
106  // 3Dconnexion Space Navigator 3D Mouse
107  {0x046d, 0xc626, InputDevice::DeviceClass::spatial_mouse, 0},
108  // 3Dconnexion Space Explorer 3D Mouse
109  {0x046d, 0xc627, InputDevice::DeviceClass::spatial_mouse, 0},
110  // 3Dconnexion Space Navigator for Notebooks
111  {0x046d, 0xc628, InputDevice::DeviceClass::spatial_mouse, 0},
112  // 3Dconnexion SpacePilot Pro 3D Mouse
113  {0x046d, 0xc629, InputDevice::DeviceClass::spatial_mouse, 0},
114  // 3Dconnexion Space Mouse Pro
115  {0x046d, 0xc62b, InputDevice::DeviceClass::spatial_mouse, 0},
116  {0},
117 };
118 
119 TypeHandle EvdevInputDevice::_type_handle;
120 
121 /**
122  * Creates a new device representing the evdev device with the given index.
123  */
124 EvdevInputDevice::
125 EvdevInputDevice(LinuxInputDeviceManager *manager, size_t index) :
126  _manager(manager),
127  _index(index),
128  _fd(-1),
129  _can_write(false),
130  _ff_id(-1),
131  _ff_playing(false),
132  _ff_strong(-1),
133  _ff_weak(-1),
134  _dpad_x_axis(-1),
135  _dpad_y_axis(-1),
136  _dpad_left_button(-1),
137  _dpad_up_button(-1),
138  _ltrigger_code(-1),
139  _rtrigger_code(-1),
140  _quirks(0) {
141 
142  char path[64];
143  sprintf(path, "/dev/input/event%zd", index);
144 
145  _fd = open(path, O_RDWR | O_NONBLOCK);
146  if (_fd >= 0) {
147  _can_write = true;
148  } else {
149  // On failure, open device as read-only.
150  _fd = open(path, O_RDONLY | O_NONBLOCK);
151  }
152 
153  if (_fd >= 0) {
154  init_device();
155  } else {
156  _is_connected = false;
157  device_cat.error()
158  << "Opening raw input device: " << strerror(errno) << " " << path << "\n";
159  }
160 }
161 
162 /**
163  *
164  */
165 EvdevInputDevice::
166 ~EvdevInputDevice() {
167  if (_fd != -1) {
168  if (_ff_id != -1) {
169  // Remove force-feedback effect.
170  do_set_vibration(0, 0);
171  ioctl(_fd, EVIOCRMFF, _ff_id);
172  _ff_id = -1;
173  }
174 
175  close(_fd);
176  _fd = -1;
177  }
178 }
179 
180 /**
181  * Sets the vibration strength. The first argument controls a low-frequency
182  * motor, if present, and the latter controls a high-frequency motor.
183  * The values are within the 0-1 range.
184  */
185 void EvdevInputDevice::
186 do_set_vibration(double strong, double weak) {
187  if (_fd == -1 || !_can_write) {
188  return;
189  }
190 
191  int strong_level = strong * 0xffff;
192  int weak_level = weak * 0xffff;
193 
194  if (strong_level == _ff_strong && weak_level == _ff_weak) {
195  // No change.
196  return;
197  }
198 
199  // Upload the new effect parameters. Do this even if we are about
200  // to stop the effect, because some drivers don't respond to simply
201  // stopping the effect.
202  struct ff_effect effect;
203  effect.type = FF_RUMBLE;
204  effect.id = _ff_id;
205  effect.direction = 0;
206  effect.trigger.button = 0;
207  effect.trigger.interval = 0;
208  effect.replay.length = 0;
209  effect.replay.delay = 0;
210  effect.u.rumble.strong_magnitude = strong_level;
211  effect.u.rumble.weak_magnitude = weak_level;
212 
213  if (ioctl(_fd, EVIOCSFF, &effect) < 0) {
214  return;
215  } else {
216  _ff_id = effect.id;
217  _ff_strong = strong_level;
218  _ff_weak = weak_level;
219  }
220 
221  if (!_ff_playing) {
222  // Start the effect. We could pass 0 as value to stop the effect
223  // when a level of 0 is requested, but my driver seems to ignore it.
224  _ff_playing = true;
225 
226  struct input_event play;
227  play.type = EV_FF;
228  play.code = _ff_id;
229  play.value = 1;
230 
231  if (write(_fd, &play, sizeof(play)) < 0) {
232  device_cat.warning()
233  << "Failed to write force-feedback event: " << strerror(errno) << "\n";
234  }
235  }
236 }
237 
238 /**
239  * Special case for Steam controllers; called if a Steam virtual device has
240  * just been disconnected, and this is currently an inactive Steam Controller
241  * previously blocked by Steam, waiting to be reactivated.
242  * Returns true if the device has just been reconnected.
243  */
244 bool EvdevInputDevice::
245 reactivate_steam_controller() {
246  LightMutexHolder holder(_lock);
247  if (!_is_connected && (_quirks & QB_steam_controller) != 0) {
248  // Just check to make sure the device is still readable.
249  process_events();
250  if (_fd != -1) {
251  _is_connected = true;
252  return true;
253  }
254  }
255  return false;
256 }
257 
258 /**
259  * Polls the input device for new activity, to ensure it contains the latest
260  * events. This will only have any effect for some types of input devices;
261  * others may be updated automatically, and this method will be a no-op.
262  */
263 void EvdevInputDevice::
264 do_poll() {
265  if (_fd != -1 && process_events()) {
266  while (process_events()) {}
267 
268  // If we got events, we are obviously connected. Mark us so.
269  if (!_is_connected && _fd != -1) {
270  _is_connected = true;
271  if (_manager != nullptr) {
272  _manager->add_device(this);
273  }
274  }
275  }
276 }
277 
278 /**
279  * Reads basic properties from the device.
280  */
281 bool EvdevInputDevice::
282 init_device() {
283  using std::string;
284 
285  nassertr(_fd >= 0, false);
286 
287  LightMutexHolder holder(_lock);
288 
289  uint8_t evtypes[(EV_MAX + 8) >> 3] = {0};
290  char name[128];
291  if (ioctl(_fd, EVIOCGNAME(sizeof(name)), name) < 0 ||
292  ioctl(_fd, EVIOCGBIT(0, sizeof(evtypes)), evtypes) < 0) {
293  close(_fd);
294  _fd = -1;
295  _is_connected = false;
296  device_cat.error() << "Opening raw input device: ioctl failed\n";
297  return false;
298  }
299 
300  _name.assign(name);
301  //cerr << "##### Now initializing device " << name << "\n";
302 
303  struct input_id id;
304  if (ioctl(_fd, EVIOCGID, &id) >= 0) {
305  _vendor_id = id.vendor;
306  _product_id = id.product;
307  }
308 
309  bool all_values_zero = true;
310  bool emulate_dpad = true;
311  bool have_analog_triggers = false;
312 
313  bool has_keys = false;
314  bool has_axes = false;
315 
316  uint8_t keys[(KEY_MAX + 8) >> 3] = {0};
317  if (test_bit(EV_KEY, evtypes)) {
318  // Check which buttons are on the device.
319  ioctl(_fd, EVIOCGBIT(EV_KEY, sizeof(keys)), keys);
320  has_keys = true;
321 
322  if (test_bit(KEY_A, keys) && test_bit(KEY_Z, keys)) {
323  enable_feature(Feature::keyboard);
324  }
325  }
326 
327  int num_bits = 0;
328  uint8_t axes[(ABS_MAX + 8) >> 3] = {0};
329  if (test_bit(EV_ABS, evtypes)) {
330  // Check which axes are on the device.
331  num_bits = ioctl(_fd, EVIOCGBIT(EV_ABS, sizeof(axes)), axes) << 3;
332  has_axes = true;
333  }
334 
335  // Do we have a preset device mapping?
336  int quirks = 0;
337  const DeviceMapping *mapping = mapping_presets;
338  while (mapping->vendor != 0) {
339  if (_vendor_id == mapping->vendor && _product_id == mapping->product) {
340  _device_class = mapping->device_class;
341  quirks = mapping->quirks;
342  break;
343  }
344  ++mapping;
345  }
346 
347  // The Steam Controller reports as multiple devices, one of which a gamepad.
348  if (quirks & QB_steam_controller) {
349  if (test_bit(BTN_GAMEPAD, keys)) {
350  _device_class = DeviceClass::gamepad;
351 
352  // If we have a virtual gamepad on the system, then careful: if Steam is
353  // running, it may disable its own gamepad in favour of the virtual
354  // device it registers. If the virtual device is present, we will only
355  // register this gamepad as connected when it registers input.
356  if (_manager->has_virtual_device(0x28de, 0x11ff)) {
357  device_cat.debug()
358  << "Detected Steam virtual gamepad, disabling Steam Controller\n";
359  quirks |= QB_connect_if_nonzero;
360  }
361  }
362  }
363  _quirks = quirks;
364 
365  // Try to detect which type of device we have here
366  if (_device_class == DeviceClass::unknown) {
367  int device_scores[(size_t)DeviceClass::spatial_mouse] = {0};
368 
369  // Test for specific keys
370  if (test_bit(BTN_GAMEPAD, keys) && test_bit(ABS_X, axes) && test_bit(ABS_RX, axes)) {
371  device_scores[(size_t)DeviceClass::gamepad] += 5;
372  device_scores[(size_t)DeviceClass::steering_wheel] += 5;
373  device_scores[(size_t)DeviceClass::flight_stick] += 5;
374  }
375 
376  if (test_bit(ABS_WHEEL, axes) && test_bit(ABS_GAS, axes) && test_bit(ABS_BRAKE, axes)) {
377  device_scores[(size_t)DeviceClass::steering_wheel] += 10;
378  }
379  if (test_bit(BTN_GEAR_DOWN, keys) && test_bit(BTN_GEAR_UP, keys)) {
380  device_scores[(size_t)DeviceClass::steering_wheel] += 10;
381  }
382  if (test_bit(BTN_JOYSTICK, keys) && test_bit(ABS_X, axes)) {
383  device_scores[(size_t)DeviceClass::flight_stick] += 10;
384  }
385  if (test_bit(BTN_MOUSE, keys) && test_bit(EV_REL, evtypes)) {
386  device_scores[(size_t)DeviceClass::mouse] += 20;
387  }
388  uint8_t unknown_keys[] = {KEY_POWER};
389  for (int i = 0; i < 1; i++) {
390  if (test_bit(unknown_keys[i], keys)) {
391  if (unknown_keys[i] == KEY_POWER) {
392  }
393  device_scores[(size_t)DeviceClass::unknown] += 20;
394  }
395  }
396  if (_features & (unsigned int)Feature::keyboard) {
397  device_scores[(size_t)DeviceClass::keyboard] += 20;
398  }
399 
400  // Test for specific name tags
401  string lowercase_name = _name;
402  for(size_t x = 0; x < _name.length(); ++x) {
403  lowercase_name[x] = tolower(lowercase_name[x]);
404  }
405  if (lowercase_name.find("gamepad") != string::npos) {
406  device_scores[(size_t)DeviceClass::gamepad] += 10;
407  }
408  if (lowercase_name.find("wheel") != string::npos) {
409  device_scores[(size_t)DeviceClass::steering_wheel] += 10;
410  }
411  if (lowercase_name.find("mouse") != string::npos || lowercase_name.find("touchpad") != string::npos) {
412  device_scores[(size_t)DeviceClass::mouse] += 10;
413  }
414  if (lowercase_name.find("keyboard") != string::npos) {
415  device_scores[(size_t)DeviceClass::keyboard] += 10;
416  }
417  // List of lowercase names that occur in unknown devices
418  string unknown_names[] = {"video bus", "power button", "sleep button"};
419  for(int i = 0; i < 3; i++) {
420  if (lowercase_name.find(unknown_names[i]) != string::npos) {
421  device_scores[(size_t)DeviceClass::unknown] += 20;
422  }
423  }
424 
425  // Check which device type got the most points
426  int highest_score = 0;
427  for (size_t i = 0; i < (size_t)DeviceClass::spatial_mouse; i++) {
428  if (device_scores[i] > highest_score) {
429  highest_score = device_scores[i];
430  _device_class = (DeviceClass)i;
431  }
432  }
433  //std::cerr << "Found highscore class " << _device_class << " with this score: " << highest_score << "\n";
434  }
435 
436  if (has_keys) {
437  // Also check whether the buttons are currently pressed.
438  uint8_t states[(KEY_MAX + 8) >> 3] = {0};
439  ioctl(_fd, EVIOCGKEY(sizeof(states)), states);
440 
441  for (int i = 0; i <= KEY_MAX; ++i) {
442  if (test_bit(i, keys)) {
443  ButtonState button;
444  button.handle = map_button(i, _device_class, quirks);
445 
446  int button_index = (int)_buttons.size();
447  if (button.handle == ButtonHandle::none()) {
448  if (device_cat.is_debug()) {
449  device_cat.debug() << "Unmapped /dev/input/event" << _index
450  << " button " << button_index << ": 0x" << std::hex << i << std::dec << "\n";
451  }
452  }
453 
454  if (test_bit(i, states)) {
455  button._state = S_down;
456  all_values_zero = false;
457  } else {
458  button._state = S_up;
459  }
460  if (button.handle == GamepadButton::dpad_left()) {
461  emulate_dpad = false;
462  } else if (button.handle == GamepadButton::ltrigger()) {
463  _ltrigger_code = i;
464  } else if (button.handle == GamepadButton::rtrigger()) {
465  _rtrigger_code = i;
466  }
467 
468  _buttons.push_back(button);
469  if ((size_t)i >= _button_indices.size()) {
470  _button_indices.resize(i + 1, -1);
471  }
472  _button_indices[i] = button_index;
473  }
474  }
475  }
476 
477  if (has_axes) {
478  _axis_indices.resize(num_bits, -1);
479 
480  for (int i = 0; i < num_bits; ++i) {
481  if (test_bit(i, axes)) {
482  Axis axis = Axis::none;
483  switch (i) {
484  case ABS_X:
485  if (_device_class == DeviceClass::gamepad) {
486  axis = InputDevice::Axis::left_x;
487  } else if (_device_class == DeviceClass::flight_stick) {
488  axis = InputDevice::Axis::roll;
489  } else {
490  axis = InputDevice::Axis::x;
491  }
492  break;
493  case ABS_Y:
494  if (_device_class == DeviceClass::gamepad) {
495  axis = InputDevice::Axis::left_y;
496  } else if (_device_class == DeviceClass::flight_stick) {
497  axis = InputDevice::Axis::pitch;
498  } else {
499  axis = InputDevice::Axis::y;
500  }
501  break;
502  case ABS_Z:
503  if (quirks & QB_rstick_from_z) {
504  if (quirks & QB_right_axes_swapped) {
505  axis = InputDevice::Axis::right_y;
506  } else {
507  axis = InputDevice::Axis::right_x;
508  }
509  } else if (_device_class == DeviceClass::gamepad) {
510  if ((quirks & QB_no_analog_triggers) == 0) {
511  axis = InputDevice::Axis::left_trigger;
512  have_analog_triggers = true;
513  }
514  } else if (_device_class == DeviceClass::spatial_mouse) {
515  axis = InputDevice::Axis::z;
516  } else {
517  axis = InputDevice::Axis::throttle;
518  }
519  break;
520  case ABS_RX:
521  if (_device_class == DeviceClass::spatial_mouse) {
522  axis = InputDevice::Axis::pitch;
523  } else if ((quirks & QB_rstick_from_z) == 0) {
524  axis = InputDevice::Axis::right_x;
525  }
526  break;
527  case ABS_RY:
528  if (_device_class == DeviceClass::spatial_mouse) {
529  axis = InputDevice::Axis::roll;
530  } else if ((quirks & QB_rstick_from_z) == 0) {
531  axis = InputDevice::Axis::right_y;
532  }
533  break;
534  case ABS_RZ:
535  if (quirks & QB_rstick_from_z) {
536  if (quirks & QB_right_axes_swapped) {
537  axis = InputDevice::Axis::right_x;
538  } else {
539  axis = InputDevice::Axis::right_y;
540  }
541  } else if (_device_class == DeviceClass::gamepad) {
542  if ((quirks & QB_no_analog_triggers) == 0) {
543  axis = InputDevice::Axis::right_trigger;
544  have_analog_triggers = true;
545  } else {
546  // Special weird case for Trust GXT 24
547  axis = InputDevice::Axis::right_y;
548  }
549  } else {
550  axis = InputDevice::Axis::yaw;
551  }
552  break;
553  case ABS_THROTTLE:
554  if (quirks & QB_rudder_from_throttle) {
555  axis = InputDevice::Axis::rudder;
556  } else {
557  axis = InputDevice::Axis::throttle;
558  }
559  break;
560  case ABS_RUDDER:
561  axis = InputDevice::Axis::rudder;
562  break;
563  case ABS_WHEEL:
564  axis = InputDevice::Axis::wheel;
565  break;
566  case ABS_GAS:
567  if (_device_class == DeviceClass::gamepad) {
568  if ((quirks & QB_no_analog_triggers) == 0) {
569  axis = InputDevice::Axis::right_trigger;
570  have_analog_triggers = true;
571  }
572  } else {
573  axis = InputDevice::Axis::accelerator;
574  }
575  break;
576  case ABS_BRAKE:
577  if (_device_class == DeviceClass::gamepad) {
578  axis = InputDevice::Axis::left_trigger;
579  have_analog_triggers = true;
580  } else {
581  axis = InputDevice::Axis::brake;
582  }
583  break;
584  case ABS_HAT0X:
585  if (emulate_dpad) {
586  _dpad_x_axis = i;
587  _dpad_left_button = (int)_buttons.size();
588  if (_device_class == DeviceClass::gamepad) {
589  _buttons.push_back(ButtonState(GamepadButton::dpad_left()));
590  _buttons.push_back(ButtonState(GamepadButton::dpad_right()));
591  } else {
592  _buttons.push_back(ButtonState(GamepadButton::hat_left()));
593  _buttons.push_back(ButtonState(GamepadButton::hat_right()));
594  }
595  }
596  break;
597  case ABS_HAT0Y:
598  if (emulate_dpad) {
599  _dpad_y_axis = i;
600  _dpad_up_button = (int)_buttons.size();
601  if (_device_class == DeviceClass::gamepad) {
602  _buttons.push_back(ButtonState(GamepadButton::dpad_up()));
603  _buttons.push_back(ButtonState(GamepadButton::dpad_down()));
604  } else {
605  _buttons.push_back(ButtonState(GamepadButton::hat_up()));
606  _buttons.push_back(ButtonState(GamepadButton::hat_down()));
607  }
608  }
609  break;
610  case ABS_HAT2X:
611  if (quirks & QB_steam_controller) {
612  axis = InputDevice::Axis::right_trigger;
613  have_analog_triggers = true;
614  }
615  break;
616  case ABS_HAT2Y:
617  if (quirks & QB_steam_controller) {
618  axis = InputDevice::Axis::left_trigger;
619  have_analog_triggers = true;
620  }
621  break;
622  }
623 
624  // Check the initial value and ranges.
625  struct input_absinfo absinfo;
626  if (ioctl(_fd, EVIOCGABS(i), &absinfo) >= 0) {
627  int index;
628  // We'd like to reverse the Y axis to match the XInput behavior.
629  // Also reverse the yaw axis to match right-hand coordinate system.
630  // Also T.Flight Hotas X throttle is reversed and can go backwards.
631  if (axis == Axis::yaw || axis == Axis::rudder || axis == Axis::left_y || axis == Axis::right_y ||
632  (axis == Axis::throttle && (quirks & QB_reversed_throttle) != 0) ||
633  (_device_class == DeviceClass::spatial_mouse && (axis == Axis::y || axis == Axis::z || axis == Axis::roll))) {
634  std::swap(absinfo.maximum, absinfo.minimum);
635  }
636  if (axis == Axis::throttle && (quirks & QB_centered_throttle) != 0) {
637  index = add_axis(axis, absinfo.minimum, absinfo.maximum, true);
638  } else {
639  index = add_axis(axis, absinfo.minimum, absinfo.maximum);
640  }
641  axis_changed(index, absinfo.value);
642  _axis_indices[i] = index;
643 
644  if (absinfo.value != 0) {
645  all_values_zero = false;
646  }
647  }
648  }
649  }
650  }
651 
652  if (test_bit(EV_REL, evtypes)) {
653  enable_feature(Feature::pointer);
654  add_pointer(PointerType::unknown, 0);
655  }
656 
657  if (test_bit(EV_FF, evtypes)) {
658  uint8_t effects[(FF_MAX + 8) >> 3] = {0};
659  ioctl(_fd, EVIOCGBIT(EV_FF, sizeof(effects)), effects);
660 
661  if (test_bit(FF_RUMBLE, effects)) {
662  if (_can_write) {
663  enable_feature(Feature::vibration);
664  } else {
665  // Let the user know what he's missing out on.
666  device_cat.warning()
667  << "/dev/input/event" << _index << " is not writable, vibration "
668  << "effects will be unavailable.\n";
669  }
670  }
671  }
672 
673  if (_ltrigger_code >= 0 && _rtrigger_code >= 0 && !have_analog_triggers) {
674  // Emulate analog triggers.
675  _ltrigger_axis = (int)_axes.size();
676  add_axis(Axis::left_trigger, 0, 1, false);
677  add_axis(Axis::right_trigger, 0, 1, false);
678  } else {
679  _ltrigger_code = -1;
680  _rtrigger_code = -1;
681  }
682 
683  char path[64];
684  char buffer[256];
685  const char *parent = "";
686  sprintf(path, "/sys/class/input/event%zd/device/device/../product", _index);
687  FILE *f = fopen(path, "r");
688  if (!f) {
689  parent = "../";
690  sprintf(path, "/sys/class/input/event%zd/device/device/%s../product", _index, parent);
691  f = fopen(path, "r");
692  }
693  if (f) {
694  if (fgets(buffer, sizeof(buffer), f) != nullptr) {
695  buffer[strcspn(buffer, "\r\n")] = 0;
696  if (buffer[0] != 0) {
697  _name.assign(buffer);
698  }
699  }
700  fclose(f);
701  }
702  sprintf(path, "/sys/class/input/event%zd/device/device/%s../manufacturer", _index, parent);
703  f = fopen(path, "r");
704  if (f) {
705  if (fgets(buffer, sizeof(buffer), f) != nullptr) {
706  buffer[strcspn(buffer, "\r\n")] = 0;
707  _manufacturer.assign(buffer);
708  }
709  fclose(f);
710  }
711  sprintf(path, "/sys/class/input/event%zd/device/device/%s../serial", _index, parent);
712  f = fopen(path, "r");
713  if (f) {
714  if (fgets(buffer, sizeof(buffer), f) != nullptr) {
715  buffer[strcspn(buffer, "\r\n")] = 0;
716  _serial_number.assign(buffer);
717  }
718  fclose(f);
719  }
720 
721  // Special-case fix for Xbox 360 Wireless Receiver: the Linux kernel
722  // driver always reports 4 connected gamepads, regardless of the number
723  // of gamepads actually present. This hack partially remedies this.
724  if (all_values_zero && (quirks & QB_connect_if_nonzero) != 0) {
725  _is_connected = false;
726  } else {
727  _is_connected = true;
728  }
729  return true;
730 }
731 
732 /**
733  * Reads a number of events from the device. Returns true if events were read,
734  * meaning this function should keep being called until it returns false.
735  */
736 bool EvdevInputDevice::
737 process_events() {
738  // Read 8 events at a time.
739  struct input_event events[8];
740 
741  int n_read = read(_fd, events, sizeof(events));
742  if (n_read < 0) {
743  if (errno == EAGAIN || errno == EWOULDBLOCK) {
744  // No data available for now.
745 
746  } else if (errno == ENODEV || errno == EINVAL) {
747  // The device ceased to exist, so we better close it. No need
748  // to worry about removing it from the InputDeviceManager, as it
749  // will get an inotify event sooner or later about this.
750  close(_fd);
751  _fd = -1;
752  //_is_connected = false;
753  errno = 0;
754 
755  } else {
756  device_cat.error() << "read: " << strerror(errno) << "\n";
757  }
758  return false;
759  }
760 
761  if (n_read == 0) {
762  return false;
763  }
764 
765  n_read /= sizeof(struct input_event);
766 
767  int rel_x = 0;
768  int rel_y = 0;
770  int index;
771 
772  // It seems that some devices send a single EV_SYN event when being
773  // unplugged. Boo. Ignore it.
774  if (n_read == 1 && events[0].code == EV_SYN) {
775  return false;
776  }
777 
778  for (int i = 0; i < n_read; ++i) {
779  int code = events[i].code;
780 
781  switch (events[i].type) {
782  case EV_SYN:
783  break;
784 
785  case EV_REL:
786  if (code == REL_X) rel_x += events[i].value;
787  if (code == REL_Y) rel_y += events[i].value;
788  break;
789 
790  case EV_ABS:
791  if (code == _dpad_x_axis) {
792  button_changed(_dpad_left_button, events[i].value < 0);
793  button_changed(_dpad_left_button+1, events[i].value > 0);
794  } else if (code == _dpad_y_axis) {
795  button_changed(_dpad_up_button, events[i].value < 0);
796  button_changed(_dpad_up_button+1, events[i].value > 0);
797  }
798  nassertd(code >= 0 && (size_t)code < _axis_indices.size()) break;
799  index = _axis_indices[code];
800  if (index >= 0) {
801  axis_changed(index, events[i].value);
802  }
803  break;
804 
805  case EV_KEY:
806  nassertd(code >= 0 && (size_t)code < _button_indices.size()) break;
807  index = _button_indices[code];
808  if (index >= 0) {
809  button_changed(index, events[i].value != 0);
810  }
811  if (code == _ltrigger_code) {
812  axis_changed(_ltrigger_axis, events[i].value);
813  } else if (code == _rtrigger_code) {
814  axis_changed(_ltrigger_axis + 1, events[i].value);
815  }
816  break;
817 
818  default:
819  //cerr << "event " << events[i].type << " - " << events[i].code << " - " << events[i].value << "\n";
820  break;
821  }
822  }
823 
824  if (rel_x != 0 || rel_y != 0) {
825  pointer_moved(0, rel_x, rel_y, time);
826  }
827 
828  return true;
829 }
830 
831 /**
832  * Static function to map an evdev code to a ButtonHandle.
833  */
834 ButtonHandle EvdevInputDevice::
835 map_button(int code, DeviceClass device_class, int quirks) {
836  if (code >= 0 && code < 0x80) {
837  // See linux/input.h for the source of this mapping.
838  static const ButtonHandle keyboard_map[] = {
839  ButtonHandle::none(),
840  KeyboardButton::escape(),
853  KeyboardButton::backspace(),
854  KeyboardButton::tab(),
867  KeyboardButton::enter(),
868  KeyboardButton::lcontrol(),
881  KeyboardButton::lshift(),
893  KeyboardButton::rshift(),
895  KeyboardButton::lalt(),
896  KeyboardButton::space(),
897  KeyboardButton::caps_lock(),
898  KeyboardButton::f1(),
899  KeyboardButton::f2(),
900  KeyboardButton::f3(),
901  KeyboardButton::f4(),
902  KeyboardButton::f5(),
903  KeyboardButton::f6(),
904  KeyboardButton::f7(),
905  KeyboardButton::f8(),
906  KeyboardButton::f9(),
907  KeyboardButton::f10(),
908  KeyboardButton::num_lock(),
909  KeyboardButton::scroll_lock(),
923  ButtonHandle::none(),
924  ButtonHandle::none(),
925  ButtonHandle::none(),
926  KeyboardButton::f11(),
927  KeyboardButton::f12(),
928  ButtonHandle::none(),
929  ButtonHandle::none(),
930  ButtonHandle::none(),
931  ButtonHandle::none(),
932  ButtonHandle::none(),
933  ButtonHandle::none(),
934  ButtonHandle::none(),
935  KeyboardButton::enter(),
936  KeyboardButton::rcontrol(),
938  KeyboardButton::print_screen(),
939  KeyboardButton::ralt(),
940  ButtonHandle::none(),
941  KeyboardButton::home(),
942  KeyboardButton::up(),
943  KeyboardButton::page_up(),
944  KeyboardButton::left(),
945  KeyboardButton::right(),
946  KeyboardButton::end(),
947  KeyboardButton::down(),
948  KeyboardButton::page_down(),
949  KeyboardButton::insert(),
950  KeyboardButton::del(),
951  ButtonHandle::none(),
952  ButtonHandle::none(),
953  ButtonHandle::none(),
954  ButtonHandle::none(),
955  ButtonHandle::none(),
956  ButtonHandle::none(),
957  ButtonHandle::none(),
958  KeyboardButton::pause(),
959  ButtonHandle::none(),
960  ButtonHandle::none(),
961  ButtonHandle::none(),
962  ButtonHandle::none(),
963  ButtonHandle::none(),
964  KeyboardButton::lmeta(),
965  KeyboardButton::rmeta(),
966  KeyboardButton::menu(),
967  };
968  return keyboard_map[code];
969 
970  } else if (code == KEY_BACK) {
971  // Used by NVIDIA Shield Controller
972  return GamepadButton::back();
973 
974  } else if (code == KEY_SEARCH) {
975  // Used by NVIDIA Shield Controller
976  return GamepadButton::guide();
977 
978  } else if (code < 0x100) {
979  return ButtonHandle::none();
980 
981  } else if ((code & 0xfff0) == BTN_MOUSE) {
982  // The number for these is reversed in Panda.
983  if (code == BTN_RIGHT) {
984  return MouseButton::three();
985  } else if (code == BTN_MIDDLE) {
986  return MouseButton::two();
987  } else {
988  return MouseButton::button(code - BTN_MOUSE);
989  }
990 
991  } else if ((code & 0xfff0) == BTN_JOYSTICK) {
992  if (quirks & QB_steam_controller) {
993  // BTN_THUMB and BTN_THUMB2 detect touching the touchpads.
994  return ButtonHandle::none();
995 
996  } else if (device_class == DeviceClass::gamepad &&
997  (quirks & QB_alt_button_mapping) != 0) {
998  static const ButtonHandle mapping[] = {
999  GamepadButton::face_y(),
1000  GamepadButton::face_b(),
1001  GamepadButton::face_a(),
1002  GamepadButton::face_x(),
1003  GamepadButton::lshoulder(),
1004  GamepadButton::rshoulder(),
1005  GamepadButton::ltrigger(),
1006  GamepadButton::rtrigger(),
1007  GamepadButton::back(),
1008  GamepadButton::start(),
1009  GamepadButton::lstick(),
1010  GamepadButton::rstick(),
1011  };
1012  if ((code & 0xf) < 12) {
1013  return mapping[code & 0xf];
1014  }
1015 
1016  } else if (device_class == DeviceClass::gamepad) {
1017  // Based on "Jess Tech Colour Rumble Pad"
1018  static const ButtonHandle mapping[] = {
1019  GamepadButton::face_x(),
1020  GamepadButton::face_y(),
1021  GamepadButton::face_a(),
1022  GamepadButton::face_b(),
1023  GamepadButton::lshoulder(),
1024  GamepadButton::ltrigger(),
1025  GamepadButton::rshoulder(),
1026  GamepadButton::rtrigger(),
1027  GamepadButton::back(),
1028  GamepadButton::start(),
1029  GamepadButton::lstick(),
1030  GamepadButton::rstick(),
1031  };
1032  if ((code & 0xf) < 12) {
1033  return mapping[code & 0xf];
1034  }
1035  } else {
1036  return GamepadButton::joystick(code & 0xf);
1037  }
1038  }
1039 
1040  switch (code) {
1041  case BTN_A:
1042  return GamepadButton::face_a();
1043 
1044  case BTN_B:
1045  return GamepadButton::face_b();
1046 
1047  case BTN_C:
1048  return GamepadButton::face_c();
1049 
1050  case BTN_X:
1051  return GamepadButton::face_x();
1052 
1053  case BTN_Y:
1054  return GamepadButton::face_y();
1055 
1056  case BTN_Z:
1057  return GamepadButton::face_z();
1058 
1059  case BTN_TL:
1060  return GamepadButton::lshoulder();
1061 
1062  case BTN_TR:
1063  return GamepadButton::rshoulder();
1064 
1065  case BTN_TL2:
1066  return GamepadButton::ltrigger();
1067 
1068  case BTN_TR2:
1069  return GamepadButton::rtrigger();
1070 
1071  case BTN_1:
1072  return GamepadButton::face_1();
1073 
1074  case BTN_2:
1075  return GamepadButton::face_2();
1076 
1077  case BTN_SELECT:
1078  case KEY_PREVIOUS:
1079  return GamepadButton::back();
1080 
1081  case BTN_START:
1082  case KEY_NEXT:
1083  return GamepadButton::start();
1084 
1085  case BTN_MODE:
1086  return GamepadButton::guide();
1087 
1088  case BTN_THUMBL:
1089  return GamepadButton::lstick();
1090 
1091  case BTN_THUMBR:
1092  return GamepadButton::rstick();
1093 
1094  case BTN_DPAD_LEFT:
1095  case BTN_TRIGGER_HAPPY1:
1096  return GamepadButton::dpad_left();
1097 
1098  case BTN_DPAD_RIGHT:
1099  case BTN_TRIGGER_HAPPY2:
1100  return GamepadButton::dpad_right();
1101 
1102  case BTN_DPAD_UP:
1103  case BTN_TRIGGER_HAPPY3:
1104  return GamepadButton::dpad_up();
1105 
1106  case BTN_DPAD_DOWN:
1107  case BTN_TRIGGER_HAPPY4:
1108  return GamepadButton::dpad_down();
1109 
1110  // The next two are for the Steam Controller's grip buttons.
1111  case BTN_GEAR_DOWN:
1112  return GamepadButton::lgrip();
1113 
1114  case BTN_GEAR_UP:
1115  return GamepadButton::rgrip();
1116 
1117  default:
1118  return ButtonHandle::none();
1119  }
1120 }
1121 
1122 #endif
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
Definition: clockObject.I:215
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static ButtonHandle three()
Returns the ButtonHandle associated with the third mouse button.
Definition: mouseButton.cxx:59
static ButtonHandle two()
Returns the ButtonHandle associated with the second mouse button.
Definition: mouseButton.cxx:51
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:26
get_frame_time
Returns the time in seconds as of the last time tick() was called (typically, this will be as of the ...
Definition: clockObject.h:91
static ButtonHandle button(int button_number)
Returns the ButtonHandle associated with the particular numbered mouse button (zero-based),...
Definition: mouseButton.cxx:32
Similar to MutexHolder, but for a light mutex.
static ButtonHandle joystick(int button_number)
Returns the ButtonHandle associated with the particular numbered joystick button (zero-based),...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
static ButtonHandle ascii_key(char ascii_equivalent)
Returns the ButtonHandle associated with the particular ASCII character, if there is one,...