SDL_evdev_capabilities.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  4. Copyright (C) 2020 Collabora Ltd.
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include "SDL_internal.h"
  20. #include "SDL_evdev_capabilities.h"
  21. #ifdef HAVE_LINUX_INPUT_H
  22. /* missing defines in older Linux kernel headers */
  23. #ifndef BTN_TRIGGER_HAPPY
  24. #define BTN_TRIGGER_HAPPY 0x2c0
  25. #endif
  26. #ifndef BTN_DPAD_UP
  27. #define BTN_DPAD_UP 0x220
  28. #endif
  29. #ifndef KEY_ALS_TOGGLE
  30. #define KEY_ALS_TOGGLE 0x230
  31. #endif
  32. extern int
  33. SDL_EVDEV_GuessDeviceClass(const unsigned long bitmask_props[NBITS(INPUT_PROP_MAX)],
  34. const unsigned long bitmask_ev[NBITS(EV_MAX)],
  35. const unsigned long bitmask_abs[NBITS(ABS_MAX)],
  36. const unsigned long bitmask_key[NBITS(KEY_MAX)],
  37. const unsigned long bitmask_rel[NBITS(REL_MAX)])
  38. {
  39. struct range
  40. {
  41. unsigned start;
  42. unsigned end;
  43. };
  44. /* key code ranges above BTN_MISC (start is inclusive, stop is exclusive)*/
  45. static const struct range high_key_blocks[] = {
  46. { KEY_OK, BTN_DPAD_UP },
  47. { KEY_ALS_TOGGLE, BTN_TRIGGER_HAPPY }
  48. };
  49. int devclass = 0;
  50. unsigned long keyboard_mask;
  51. /* If the kernel specifically says it's an accelerometer, believe it */
  52. if (test_bit(INPUT_PROP_ACCELEROMETER, bitmask_props)) {
  53. return SDL_UDEV_DEVICE_ACCELEROMETER;
  54. }
  55. /* We treat pointing sticks as indistinguishable from mice */
  56. if (test_bit(INPUT_PROP_POINTING_STICK, bitmask_props)) {
  57. return SDL_UDEV_DEVICE_MOUSE;
  58. }
  59. /* We treat buttonpads as equivalent to touchpads */
  60. if (test_bit(INPUT_PROP_TOPBUTTONPAD, bitmask_props) ||
  61. test_bit(INPUT_PROP_BUTTONPAD, bitmask_props)) {
  62. return SDL_UDEV_DEVICE_TOUCHPAD;
  63. }
  64. /* X, Y, Z axes but no buttons probably means an accelerometer */
  65. if (test_bit(EV_ABS, bitmask_ev) &&
  66. test_bit(ABS_X, bitmask_abs) &&
  67. test_bit(ABS_Y, bitmask_abs) &&
  68. test_bit(ABS_Z, bitmask_abs) &&
  69. !test_bit(EV_KEY, bitmask_ev)) {
  70. return SDL_UDEV_DEVICE_ACCELEROMETER;
  71. }
  72. /* RX, RY, RZ axes but no buttons probably means a gyro or
  73. * accelerometer (we don't distinguish) */
  74. if (test_bit(EV_ABS, bitmask_ev) &&
  75. test_bit(ABS_RX, bitmask_abs) &&
  76. test_bit(ABS_RY, bitmask_abs) &&
  77. test_bit(ABS_RZ, bitmask_abs) &&
  78. !test_bit(EV_KEY, bitmask_ev)) {
  79. return SDL_UDEV_DEVICE_ACCELEROMETER;
  80. }
  81. if (test_bit(EV_ABS, bitmask_ev) &&
  82. test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs)) {
  83. if (test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key)) {
  84. ; /* ID_INPUT_TABLET */
  85. } else if (test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key)) {
  86. devclass |= SDL_UDEV_DEVICE_TOUCHPAD; /* ID_INPUT_TOUCHPAD */
  87. } else if (test_bit(BTN_MOUSE, bitmask_key)) {
  88. devclass |= SDL_UDEV_DEVICE_MOUSE; /* ID_INPUT_MOUSE */
  89. } else if (test_bit(BTN_TOUCH, bitmask_key)) {
  90. /* TODO: better determining between touchscreen and multitouch touchpad,
  91. see https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-input_id.c */
  92. devclass |= SDL_UDEV_DEVICE_TOUCHSCREEN; /* ID_INPUT_TOUCHSCREEN */
  93. }
  94. if (test_bit(BTN_TRIGGER, bitmask_key) ||
  95. test_bit(BTN_A, bitmask_key) ||
  96. test_bit(BTN_1, bitmask_key) ||
  97. test_bit(ABS_RX, bitmask_abs) ||
  98. test_bit(ABS_RY, bitmask_abs) ||
  99. test_bit(ABS_RZ, bitmask_abs) ||
  100. test_bit(ABS_THROTTLE, bitmask_abs) ||
  101. test_bit(ABS_RUDDER, bitmask_abs) ||
  102. test_bit(ABS_WHEEL, bitmask_abs) ||
  103. test_bit(ABS_GAS, bitmask_abs) ||
  104. test_bit(ABS_BRAKE, bitmask_abs)) {
  105. devclass |= SDL_UDEV_DEVICE_JOYSTICK; /* ID_INPUT_JOYSTICK */
  106. }
  107. }
  108. if (test_bit(EV_REL, bitmask_ev) &&
  109. test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel) &&
  110. test_bit(BTN_MOUSE, bitmask_key)) {
  111. devclass |= SDL_UDEV_DEVICE_MOUSE; /* ID_INPUT_MOUSE */
  112. }
  113. if (test_bit(EV_KEY, bitmask_ev)) {
  114. unsigned i;
  115. unsigned long found = 0;
  116. for (i = 0; i < BTN_MISC / BITS_PER_LONG; ++i) {
  117. found |= bitmask_key[i];
  118. }
  119. /* If there are no keys in the lower block, check the higher blocks */
  120. if (!found) {
  121. unsigned block;
  122. for (block = 0; block < (sizeof(high_key_blocks) / sizeof(struct range)); ++block) {
  123. for (i = high_key_blocks[block].start; i < high_key_blocks[block].end; ++i) {
  124. if (test_bit(i, bitmask_key)) {
  125. found = 1;
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. if (found > 0) {
  132. devclass |= SDL_UDEV_DEVICE_HAS_KEYS; /* ID_INPUT_KEY */
  133. }
  134. }
  135. /* the first 32 bits are ESC, numbers, and Q to D, so if we have all of
  136. * those, consider it to be a fully-featured keyboard;
  137. * do not test KEY_RESERVED, though */
  138. keyboard_mask = 0xFFFFFFFE;
  139. if ((bitmask_key[0] & keyboard_mask) == keyboard_mask) {
  140. devclass |= SDL_UDEV_DEVICE_KEYBOARD; /* ID_INPUT_KEYBOARD */
  141. }
  142. return devclass;
  143. }
  144. #endif /* HAVE_LINUX_INPUT_H */