SDL_evdev_capabilities.c (4069B)
1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org> 4 Copyright (C) 2020 Collabora Ltd. 5 6 This software is provided 'as-is', without any express or implied 7 warranty. In no event will the authors be held liable for any damages 8 arising from the use of this software. 9 10 Permission is granted to anyone to use this software for any purpose, 11 including commercial applications, and to alter it and redistribute it 12 freely, subject to the following restrictions: 13 14 1. The origin of this software must not be misrepresented; you must not 15 claim that you wrote the original software. If you use this software 16 in a product, an acknowledgment in the product documentation would be 17 appreciated but is not required. 18 2. Altered source versions must be plainly marked as such, and must not be 19 misrepresented as being the original software. 20 3. This notice may not be removed or altered from any source distribution. 21 */ 22 23 #include "SDL_evdev_capabilities.h" 24 25 #if HAVE_LIBUDEV_H || defined(SDL_JOYSTICK_LINUX) 26 27 extern int 28 SDL_EVDEV_GuessDeviceClass(unsigned long bitmask_ev[NBITS(EV_MAX)], 29 unsigned long bitmask_abs[NBITS(ABS_MAX)], 30 unsigned long bitmask_key[NBITS(KEY_MAX)], 31 unsigned long bitmask_rel[NBITS(REL_MAX)]) 32 { 33 int devclass = 0; 34 unsigned long keyboard_mask; 35 36 /* X, Y, Z axes but no buttons probably means an accelerometer */ 37 if (test_bit(EV_ABS, bitmask_ev) && 38 test_bit(ABS_X, bitmask_abs) && 39 test_bit(ABS_Y, bitmask_abs) && 40 test_bit(ABS_Z, bitmask_abs) && 41 !test_bit(EV_KEY, bitmask_ev)) { 42 return SDL_UDEV_DEVICE_ACCELEROMETER; 43 } 44 45 /* RX, RY, RZ axes but no buttons also probably means an accelerometer */ 46 if (test_bit(EV_ABS, bitmask_ev) && 47 test_bit(ABS_RX, bitmask_abs) && 48 test_bit(ABS_RY, bitmask_abs) && 49 test_bit(ABS_RZ, bitmask_abs) && 50 !test_bit(EV_KEY, bitmask_ev)) { 51 return SDL_UDEV_DEVICE_ACCELEROMETER; 52 } 53 54 if (test_bit(EV_ABS, bitmask_ev) && 55 test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs)) { 56 if (test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key)) { 57 ; /* ID_INPUT_TABLET */ 58 } else if (test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key)) { 59 ; /* ID_INPUT_TOUCHPAD */ 60 } else if (test_bit(BTN_MOUSE, bitmask_key)) { 61 devclass |= SDL_UDEV_DEVICE_MOUSE; /* ID_INPUT_MOUSE */ 62 } else if (test_bit(BTN_TOUCH, bitmask_key)) { 63 /* TODO: better determining between touchscreen and multitouch touchpad, 64 see https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-input_id.c */ 65 devclass |= SDL_UDEV_DEVICE_TOUCHSCREEN; /* ID_INPUT_TOUCHSCREEN */ 66 } 67 68 if (test_bit(BTN_TRIGGER, bitmask_key) || 69 test_bit(BTN_A, bitmask_key) || 70 test_bit(BTN_1, bitmask_key) || 71 test_bit(ABS_RX, bitmask_abs) || 72 test_bit(ABS_RY, bitmask_abs) || 73 test_bit(ABS_RZ, bitmask_abs) || 74 test_bit(ABS_THROTTLE, bitmask_abs) || 75 test_bit(ABS_RUDDER, bitmask_abs) || 76 test_bit(ABS_WHEEL, bitmask_abs) || 77 test_bit(ABS_GAS, bitmask_abs) || 78 test_bit(ABS_BRAKE, bitmask_abs)) { 79 devclass |= SDL_UDEV_DEVICE_JOYSTICK; /* ID_INPUT_JOYSTICK */ 80 } 81 } 82 83 if (test_bit(EV_REL, bitmask_ev) && 84 test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel) && 85 test_bit(BTN_MOUSE, bitmask_key)) { 86 devclass |= SDL_UDEV_DEVICE_MOUSE; /* ID_INPUT_MOUSE */ 87 } 88 89 /* the first 32 bits are ESC, numbers, and Q to D; if we have any of 90 * those, consider it a keyboard device; do not test KEY_RESERVED, though */ 91 keyboard_mask = 0xFFFFFFFE; 92 if ((bitmask_key[0] & keyboard_mask) != 0) 93 devclass |= SDL_UDEV_DEVICE_KEYBOARD; /* ID_INPUT_KEYBOARD */ 94 95 return devclass; 96 } 97 98 #endif