SDL_evdev.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_INPUT_LINUXEV
  20. // This is based on the linux joystick driver
  21. /* References: https://www.kernel.org/doc/Documentation/input/input.txt
  22. * https://www.kernel.org/doc/Documentation/input/event-codes.txt
  23. * /usr/include/linux/input.h
  24. * The evtest application is also useful to debug the protocol
  25. */
  26. #include "SDL_evdev.h"
  27. #include "SDL_evdev_kbd.h"
  28. #include <errno.h>
  29. #include <fcntl.h>
  30. #include <unistd.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/stat.h>
  33. #include <linux/input.h>
  34. #include "../../events/SDL_events_c.h"
  35. #include "../../events/SDL_scancode_tables_c.h"
  36. #include "../../core/linux/SDL_evdev_capabilities.h"
  37. #include "../../core/linux/SDL_udev.h"
  38. // These are not defined in older Linux kernel headers
  39. #ifndef SYN_DROPPED
  40. #define SYN_DROPPED 3
  41. #endif
  42. #ifndef ABS_MT_SLOT
  43. #define ABS_MT_SLOT 0x2f
  44. #define ABS_MT_POSITION_X 0x35
  45. #define ABS_MT_POSITION_Y 0x36
  46. #define ABS_MT_TRACKING_ID 0x39
  47. #define ABS_MT_PRESSURE 0x3a
  48. #endif
  49. #ifndef REL_WHEEL_HI_RES
  50. #define REL_WHEEL_HI_RES 0x0b
  51. #define REL_HWHEEL_HI_RES 0x0c
  52. #endif
  53. // The field to look up in struct input_event for integer seconds
  54. #ifndef input_event_sec
  55. #define input_event_sec time.tv_sec
  56. #endif
  57. // The field to look up in struct input_event for fractional seconds
  58. #ifndef input_event_usec
  59. #define input_event_usec time.tv_usec
  60. #endif
  61. typedef struct SDL_evdevlist_item
  62. {
  63. char *path;
  64. int fd;
  65. int udev_class;
  66. // TODO: use this for every device, not just touchscreen
  67. bool out_of_sync;
  68. /* TODO: expand on this to have data for every possible class (mouse,
  69. keyboard, touchpad, etc.). Also there's probably some things in here we
  70. can pull out to the SDL_evdevlist_item i.e. name */
  71. bool is_touchscreen;
  72. struct
  73. {
  74. char *name;
  75. int min_x, max_x, range_x;
  76. int min_y, max_y, range_y;
  77. int min_pressure, max_pressure, range_pressure;
  78. int max_slots;
  79. int current_slot;
  80. struct
  81. {
  82. enum
  83. {
  84. EVDEV_TOUCH_SLOTDELTA_NONE = 0,
  85. EVDEV_TOUCH_SLOTDELTA_DOWN,
  86. EVDEV_TOUCH_SLOTDELTA_UP,
  87. EVDEV_TOUCH_SLOTDELTA_MOVE
  88. } delta;
  89. int tracking_id;
  90. int x, y, pressure;
  91. } *slots;
  92. } *touchscreen_data;
  93. // Mouse state
  94. bool high_res_wheel;
  95. bool high_res_hwheel;
  96. bool relative_mouse;
  97. int mouse_x, mouse_y;
  98. int mouse_wheel, mouse_hwheel;
  99. int min_x, max_x, range_x;
  100. int min_y, max_y, range_y;
  101. struct SDL_evdevlist_item *next;
  102. } SDL_evdevlist_item;
  103. typedef struct SDL_EVDEV_PrivateData
  104. {
  105. int ref_count;
  106. int num_devices;
  107. SDL_evdevlist_item *first;
  108. SDL_evdevlist_item *last;
  109. SDL_EVDEV_keyboard_state *kbd;
  110. } SDL_EVDEV_PrivateData;
  111. static SDL_EVDEV_PrivateData *_this = NULL;
  112. static SDL_Scancode SDL_EVDEV_translate_keycode(int keycode);
  113. static void SDL_EVDEV_sync_device(SDL_evdevlist_item *item);
  114. static bool SDL_EVDEV_device_removed(const char *dev_path);
  115. static bool SDL_EVDEV_device_added(const char *dev_path, int udev_class);
  116. #ifdef SDL_USE_LIBUDEV
  117. static void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_event, int udev_class, const char *dev_path);
  118. #endif // SDL_USE_LIBUDEV
  119. static Uint8 EVDEV_MouseButtons[] = {
  120. SDL_BUTTON_LEFT, // BTN_LEFT 0x110
  121. SDL_BUTTON_RIGHT, // BTN_RIGHT 0x111
  122. SDL_BUTTON_MIDDLE, // BTN_MIDDLE 0x112
  123. SDL_BUTTON_X1, // BTN_SIDE 0x113
  124. SDL_BUTTON_X2, // BTN_EXTRA 0x114
  125. SDL_BUTTON_X2 + 1, // BTN_FORWARD 0x115
  126. SDL_BUTTON_X2 + 2, // BTN_BACK 0x116
  127. SDL_BUTTON_X2 + 3 // BTN_TASK 0x117
  128. };
  129. static bool SDL_EVDEV_SetRelativeMouseMode(bool enabled)
  130. {
  131. // Mice already send relative events through this interface
  132. return true;
  133. }
  134. static void SDL_EVDEV_UpdateKeyboardMute(void)
  135. {
  136. if (SDL_EVDEV_GetDeviceCount(SDL_UDEV_DEVICE_KEYBOARD) > 0) {
  137. SDL_EVDEV_kbd_set_muted(_this->kbd, true);
  138. } else {
  139. SDL_EVDEV_kbd_set_muted(_this->kbd, false);
  140. }
  141. }
  142. bool SDL_EVDEV_Init(void)
  143. {
  144. if (!_this) {
  145. _this = (SDL_EVDEV_PrivateData *)SDL_calloc(1, sizeof(*_this));
  146. if (!_this) {
  147. return false;
  148. }
  149. #ifdef SDL_USE_LIBUDEV
  150. if (!SDL_UDEV_Init()) {
  151. SDL_free(_this);
  152. _this = NULL;
  153. return false;
  154. }
  155. // Set up the udev callback
  156. if (!SDL_UDEV_AddCallback(SDL_EVDEV_udev_callback)) {
  157. SDL_UDEV_Quit();
  158. SDL_free(_this);
  159. _this = NULL;
  160. return false;
  161. }
  162. // Force a scan to build the initial device list
  163. SDL_UDEV_Scan();
  164. #else
  165. {
  166. /* Allow the user to specify a list of devices explicitly of
  167. the form:
  168. deviceclass:path[,deviceclass:path[,...]]
  169. where device class is an integer representing the
  170. SDL_UDEV_deviceclass and path is the full path to
  171. the event device. */
  172. const char *devices = SDL_GetHint(SDL_HINT_EVDEV_DEVICES);
  173. if (devices) {
  174. /* Assume this is the old use of the env var and it is not in
  175. ROM. */
  176. char *rest = (char *)devices;
  177. char *spec;
  178. while ((spec = SDL_strtok_r(rest, ",", &rest))) {
  179. char *endofcls = 0;
  180. long cls = SDL_strtol(spec, &endofcls, 0);
  181. if (endofcls) {
  182. SDL_EVDEV_device_added(endofcls + 1, cls);
  183. }
  184. }
  185. } else {
  186. // TODO: Scan the devices manually, like a caveman
  187. }
  188. }
  189. #endif // SDL_USE_LIBUDEV
  190. _this->kbd = SDL_EVDEV_kbd_init();
  191. SDL_EVDEV_UpdateKeyboardMute();
  192. }
  193. SDL_GetMouse()->SetRelativeMouseMode = SDL_EVDEV_SetRelativeMouseMode;
  194. _this->ref_count += 1;
  195. return true;
  196. }
  197. void SDL_EVDEV_Quit(void)
  198. {
  199. if (!_this) {
  200. return;
  201. }
  202. _this->ref_count -= 1;
  203. if (_this->ref_count < 1) {
  204. #ifdef SDL_USE_LIBUDEV
  205. SDL_UDEV_DelCallback(SDL_EVDEV_udev_callback);
  206. SDL_UDEV_Quit();
  207. #endif // SDL_USE_LIBUDEV
  208. // Remove existing devices
  209. while (_this->first) {
  210. SDL_EVDEV_device_removed(_this->first->path);
  211. }
  212. SDL_EVDEV_kbd_quit(_this->kbd);
  213. SDL_assert(_this->first == NULL);
  214. SDL_assert(_this->last == NULL);
  215. SDL_assert(_this->num_devices == 0);
  216. SDL_free(_this);
  217. _this = NULL;
  218. }
  219. }
  220. #ifdef SDL_USE_LIBUDEV
  221. static void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_event, int udev_class,
  222. const char *dev_path)
  223. {
  224. if (!dev_path) {
  225. return;
  226. }
  227. switch (udev_event) {
  228. case SDL_UDEV_DEVICEADDED:
  229. if (!(udev_class & (SDL_UDEV_DEVICE_MOUSE | SDL_UDEV_DEVICE_HAS_KEYS | SDL_UDEV_DEVICE_TOUCHSCREEN | SDL_UDEV_DEVICE_TOUCHPAD))) {
  230. return;
  231. }
  232. if (udev_class & SDL_UDEV_DEVICE_JOYSTICK) {
  233. return;
  234. }
  235. SDL_EVDEV_device_added(dev_path, udev_class);
  236. break;
  237. case SDL_UDEV_DEVICEREMOVED:
  238. SDL_EVDEV_device_removed(dev_path);
  239. break;
  240. default:
  241. break;
  242. }
  243. }
  244. #endif // SDL_USE_LIBUDEV
  245. void SDL_EVDEV_SetVTSwitchCallbacks(void (*release_callback)(void *), void *release_callback_data,
  246. void (*acquire_callback)(void *), void *acquire_callback_data)
  247. {
  248. SDL_EVDEV_kbd_set_vt_switch_callbacks(_this->kbd,
  249. release_callback, release_callback_data,
  250. acquire_callback, acquire_callback_data);
  251. }
  252. int SDL_EVDEV_GetDeviceCount(int device_class)
  253. {
  254. SDL_evdevlist_item *item;
  255. int count = 0;
  256. for (item = _this->first; item; item = item->next) {
  257. if ((item->udev_class & device_class) == device_class) {
  258. ++count;
  259. }
  260. }
  261. return count;
  262. }
  263. void SDL_EVDEV_Poll(void)
  264. {
  265. struct input_event events[32];
  266. int i, j, len;
  267. SDL_evdevlist_item *item;
  268. SDL_Scancode scancode;
  269. int mouse_button;
  270. SDL_Mouse *mouse;
  271. float norm_x, norm_y, norm_pressure;
  272. if (!_this) {
  273. return;
  274. }
  275. SDL_EVDEV_kbd_update(_this->kbd);
  276. mouse = SDL_GetMouse();
  277. for (item = _this->first; item; item = item->next) {
  278. while ((len = read(item->fd, events, sizeof(events))) > 0) {
  279. len /= sizeof(events[0]);
  280. for (i = 0; i < len; ++i) {
  281. struct input_event *event = &events[i];
  282. /* special handling for touchscreen, that should eventually be
  283. used for all devices */
  284. if (item->out_of_sync && item->is_touchscreen &&
  285. event->type == EV_SYN && event->code != SYN_REPORT) {
  286. break;
  287. }
  288. switch (event->type) {
  289. case EV_KEY:
  290. if (event->code >= BTN_MOUSE && event->code < BTN_MOUSE + SDL_arraysize(EVDEV_MouseButtons)) {
  291. Uint64 timestamp = SDL_EVDEV_GetEventTimestamp(event);
  292. mouse_button = event->code - BTN_MOUSE;
  293. SDL_SendMouseButton(timestamp, mouse->focus, (SDL_MouseID)item->fd, EVDEV_MouseButtons[mouse_button], (event->value != 0));
  294. break;
  295. }
  296. /* BTN_TOUCH event value 1 indicates there is contact with
  297. a touchscreen or trackpad (earliest finger's current
  298. position is sent in EV_ABS ABS_X/ABS_Y, switching to
  299. next finger after earliest is released) */
  300. if (item->is_touchscreen && event->code == BTN_TOUCH) {
  301. if (item->touchscreen_data->max_slots == 1) {
  302. if (event->value) {
  303. item->touchscreen_data->slots[0].delta = EVDEV_TOUCH_SLOTDELTA_DOWN;
  304. } else {
  305. item->touchscreen_data->slots[0].delta = EVDEV_TOUCH_SLOTDELTA_UP;
  306. }
  307. }
  308. break;
  309. }
  310. // Probably keyboard
  311. {
  312. Uint64 timestamp = SDL_EVDEV_GetEventTimestamp(event);
  313. scancode = SDL_EVDEV_translate_keycode(event->code);
  314. if (event->value == 0) {
  315. SDL_SendKeyboardKey(timestamp, (SDL_KeyboardID)item->fd, event->code, scancode, false);
  316. } else if (event->value == 1 || event->value == 2 /* key repeated */) {
  317. SDL_SendKeyboardKey(timestamp, (SDL_KeyboardID)item->fd, event->code, scancode, true);
  318. }
  319. SDL_EVDEV_kbd_keycode(_this->kbd, event->code, event->value);
  320. }
  321. break;
  322. case EV_ABS:
  323. switch (event->code) {
  324. case ABS_MT_SLOT:
  325. if (!item->is_touchscreen) { // FIXME: temp hack
  326. break;
  327. }
  328. item->touchscreen_data->current_slot = event->value;
  329. break;
  330. case ABS_MT_TRACKING_ID:
  331. if (!item->is_touchscreen) { // FIXME: temp hack
  332. break;
  333. }
  334. if (event->value >= 0) {
  335. item->touchscreen_data->slots[item->touchscreen_data->current_slot].tracking_id = event->value + 1;
  336. item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta = EVDEV_TOUCH_SLOTDELTA_DOWN;
  337. } else {
  338. item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta = EVDEV_TOUCH_SLOTDELTA_UP;
  339. }
  340. break;
  341. case ABS_MT_POSITION_X:
  342. if (!item->is_touchscreen) { // FIXME: temp hack
  343. break;
  344. }
  345. item->touchscreen_data->slots[item->touchscreen_data->current_slot].x = event->value;
  346. if (item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta == EVDEV_TOUCH_SLOTDELTA_NONE) {
  347. item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta = EVDEV_TOUCH_SLOTDELTA_MOVE;
  348. }
  349. break;
  350. case ABS_MT_POSITION_Y:
  351. if (!item->is_touchscreen) { // FIXME: temp hack
  352. break;
  353. }
  354. item->touchscreen_data->slots[item->touchscreen_data->current_slot].y = event->value;
  355. if (item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta == EVDEV_TOUCH_SLOTDELTA_NONE) {
  356. item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta = EVDEV_TOUCH_SLOTDELTA_MOVE;
  357. }
  358. break;
  359. case ABS_MT_PRESSURE:
  360. if (!item->is_touchscreen) { // FIXME: temp hack
  361. break;
  362. }
  363. item->touchscreen_data->slots[item->touchscreen_data->current_slot].pressure = event->value;
  364. if (item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta == EVDEV_TOUCH_SLOTDELTA_NONE) {
  365. item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta = EVDEV_TOUCH_SLOTDELTA_MOVE;
  366. }
  367. break;
  368. case ABS_X:
  369. if (item->is_touchscreen) {
  370. if (item->touchscreen_data->max_slots != 1) {
  371. break;
  372. }
  373. item->touchscreen_data->slots[0].x = event->value;
  374. } else if (!item->relative_mouse) {
  375. item->mouse_x = event->value;
  376. }
  377. break;
  378. case ABS_Y:
  379. if (item->is_touchscreen) {
  380. if (item->touchscreen_data->max_slots != 1) {
  381. break;
  382. }
  383. item->touchscreen_data->slots[0].y = event->value;
  384. } else if (!item->relative_mouse) {
  385. item->mouse_y = event->value;
  386. }
  387. break;
  388. default:
  389. break;
  390. }
  391. break;
  392. case EV_REL:
  393. switch (event->code) {
  394. case REL_X:
  395. if (item->relative_mouse) {
  396. item->mouse_x += event->value;
  397. }
  398. break;
  399. case REL_Y:
  400. if (item->relative_mouse) {
  401. item->mouse_y += event->value;
  402. }
  403. break;
  404. case REL_WHEEL:
  405. if (!item->high_res_wheel) {
  406. item->mouse_wheel += event->value;
  407. }
  408. break;
  409. case REL_WHEEL_HI_RES:
  410. SDL_assert(item->high_res_wheel);
  411. item->mouse_wheel += event->value;
  412. break;
  413. case REL_HWHEEL:
  414. if (!item->high_res_hwheel) {
  415. item->mouse_hwheel += event->value;
  416. }
  417. break;
  418. case REL_HWHEEL_HI_RES:
  419. SDL_assert(item->high_res_hwheel);
  420. item->mouse_hwheel += event->value;
  421. break;
  422. default:
  423. break;
  424. }
  425. break;
  426. case EV_SYN:
  427. switch (event->code) {
  428. case SYN_REPORT:
  429. // Send mouse axis changes together to ensure consistency and reduce event processing overhead
  430. if (item->relative_mouse) {
  431. if (item->mouse_x != 0 || item->mouse_y != 0) {
  432. Uint64 timestamp = SDL_EVDEV_GetEventTimestamp(event);
  433. SDL_SendMouseMotion(timestamp, mouse->focus, (SDL_MouseID)item->fd, item->relative_mouse, (float)item->mouse_x, (float)item->mouse_y);
  434. item->mouse_x = item->mouse_y = 0;
  435. }
  436. } else if (item->range_x > 0 && item->range_y > 0) {
  437. int screen_w = 0, screen_h = 0;
  438. const SDL_DisplayMode *mode = NULL;
  439. if (mouse->focus) {
  440. mode = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(mouse->focus));
  441. }
  442. if (!mode) {
  443. mode = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay());
  444. }
  445. if (mode) {
  446. screen_w = mode->w;
  447. screen_h = mode->h;
  448. }
  449. SDL_SendMouseMotion(SDL_EVDEV_GetEventTimestamp(event), mouse->focus, (SDL_MouseID)item->fd, item->relative_mouse,
  450. (float)(item->mouse_x - item->min_x) * screen_w / item->range_x,
  451. (float)(item->mouse_y - item->min_y) * screen_h / item->range_y);
  452. }
  453. if (item->mouse_wheel != 0 || item->mouse_hwheel != 0) {
  454. Uint64 timestamp = SDL_EVDEV_GetEventTimestamp(event);
  455. const float denom = (item->high_res_hwheel ? 120.0f : 1.0f);
  456. SDL_SendMouseWheel(timestamp,
  457. mouse->focus, (SDL_MouseID)item->fd,
  458. item->mouse_hwheel / denom,
  459. item->mouse_wheel / denom,
  460. SDL_MOUSEWHEEL_NORMAL);
  461. item->mouse_wheel = item->mouse_hwheel = 0;
  462. }
  463. if (!item->is_touchscreen) { // FIXME: temp hack
  464. break;
  465. }
  466. for (j = 0; j < item->touchscreen_data->max_slots; j++) {
  467. norm_x = (float)(item->touchscreen_data->slots[j].x - item->touchscreen_data->min_x) /
  468. (float)item->touchscreen_data->range_x;
  469. norm_y = (float)(item->touchscreen_data->slots[j].y - item->touchscreen_data->min_y) /
  470. (float)item->touchscreen_data->range_y;
  471. if (item->touchscreen_data->range_pressure > 0) {
  472. norm_pressure = (float)(item->touchscreen_data->slots[j].pressure - item->touchscreen_data->min_pressure) /
  473. (float)item->touchscreen_data->range_pressure;
  474. } else {
  475. // This touchscreen does not support pressure
  476. norm_pressure = 1.0f;
  477. }
  478. /* FIXME: the touch's window shouldn't be null, but
  479. * the coordinate space of touch positions needs to
  480. * be window-relative in that case. */
  481. switch (item->touchscreen_data->slots[j].delta) {
  482. case EVDEV_TOUCH_SLOTDELTA_DOWN:
  483. SDL_SendTouch(SDL_EVDEV_GetEventTimestamp(event), item->fd, item->touchscreen_data->slots[j].tracking_id, NULL, SDL_EVENT_FINGER_DOWN, norm_x, norm_y, norm_pressure);
  484. item->touchscreen_data->slots[j].delta = EVDEV_TOUCH_SLOTDELTA_NONE;
  485. break;
  486. case EVDEV_TOUCH_SLOTDELTA_UP:
  487. SDL_SendTouch(SDL_EVDEV_GetEventTimestamp(event), item->fd, item->touchscreen_data->slots[j].tracking_id, NULL, SDL_EVENT_FINGER_UP, norm_x, norm_y, norm_pressure);
  488. item->touchscreen_data->slots[j].tracking_id = 0;
  489. item->touchscreen_data->slots[j].delta = EVDEV_TOUCH_SLOTDELTA_NONE;
  490. break;
  491. case EVDEV_TOUCH_SLOTDELTA_MOVE:
  492. SDL_SendTouchMotion(SDL_EVDEV_GetEventTimestamp(event), item->fd, item->touchscreen_data->slots[j].tracking_id, NULL, norm_x, norm_y, norm_pressure);
  493. item->touchscreen_data->slots[j].delta = EVDEV_TOUCH_SLOTDELTA_NONE;
  494. break;
  495. default:
  496. break;
  497. }
  498. }
  499. if (item->out_of_sync) {
  500. item->out_of_sync = false;
  501. }
  502. break;
  503. case SYN_DROPPED:
  504. if (item->is_touchscreen) {
  505. item->out_of_sync = true;
  506. }
  507. SDL_EVDEV_sync_device(item);
  508. break;
  509. default:
  510. break;
  511. }
  512. break;
  513. }
  514. }
  515. }
  516. }
  517. }
  518. static SDL_Scancode SDL_EVDEV_translate_keycode(int keycode)
  519. {
  520. SDL_Scancode scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_LINUX, keycode);
  521. #ifdef DEBUG_SCANCODES
  522. if (scancode == SDL_SCANCODE_UNKNOWN) {
  523. /* BTN_TOUCH is handled elsewhere, but we might still end up here if
  524. you get an unexpected BTN_TOUCH from something SDL believes is not
  525. a touch device. In this case, we'd rather not get a misleading
  526. SDL_Log message about an unknown key. */
  527. if (keycode != BTN_TOUCH) {
  528. SDL_Log("The key you just pressed is not recognized by SDL. To help "
  529. "get this fixed, please report this to the SDL forums/mailing list "
  530. "<https://discourse.libsdl.org/> EVDEV KeyCode %d",
  531. keycode);
  532. }
  533. }
  534. #endif // DEBUG_SCANCODES
  535. return scancode;
  536. }
  537. static bool SDL_EVDEV_init_keyboard(SDL_evdevlist_item *item, int udev_class)
  538. {
  539. char name[128];
  540. name[0] = '\0';
  541. ioctl(item->fd, EVIOCGNAME(sizeof(name)), name);
  542. SDL_AddKeyboard((SDL_KeyboardID)item->fd, name);
  543. return true;
  544. }
  545. static void SDL_EVDEV_destroy_keyboard(SDL_evdevlist_item *item)
  546. {
  547. SDL_RemoveKeyboard((SDL_KeyboardID)item->fd);
  548. }
  549. static bool SDL_EVDEV_init_mouse(SDL_evdevlist_item *item, int udev_class)
  550. {
  551. char name[128];
  552. int ret;
  553. struct input_absinfo abs_info;
  554. name[0] = '\0';
  555. ioctl(item->fd, EVIOCGNAME(sizeof(name)), name);
  556. SDL_AddMouse((SDL_MouseID)item->fd, name);
  557. ret = ioctl(item->fd, EVIOCGABS(ABS_X), &abs_info);
  558. if (ret < 0) {
  559. // no absolute mode info, continue
  560. return true;
  561. }
  562. item->min_x = abs_info.minimum;
  563. item->max_x = abs_info.maximum;
  564. item->range_x = abs_info.maximum - abs_info.minimum;
  565. ret = ioctl(item->fd, EVIOCGABS(ABS_Y), &abs_info);
  566. if (ret < 0) {
  567. // no absolute mode info, continue
  568. return true;
  569. }
  570. item->min_y = abs_info.minimum;
  571. item->max_y = abs_info.maximum;
  572. item->range_y = abs_info.maximum - abs_info.minimum;
  573. return true;
  574. }
  575. static void SDL_EVDEV_destroy_mouse(SDL_evdevlist_item *item)
  576. {
  577. SDL_RemoveMouse((SDL_MouseID)item->fd);
  578. }
  579. static bool SDL_EVDEV_init_touchscreen(SDL_evdevlist_item *item, int udev_class)
  580. {
  581. int ret;
  582. unsigned long xreq, yreq;
  583. char name[64];
  584. struct input_absinfo abs_info;
  585. if (!item->is_touchscreen) {
  586. return true;
  587. }
  588. item->touchscreen_data = SDL_calloc(1, sizeof(*item->touchscreen_data));
  589. if (!item->touchscreen_data) {
  590. return false;
  591. }
  592. ret = ioctl(item->fd, EVIOCGNAME(sizeof(name)), name);
  593. if (ret < 0) {
  594. SDL_free(item->touchscreen_data);
  595. SDL_LogError(SDL_LOG_CATEGORY_INPUT, "Failed to get evdev touchscreen name");
  596. return false;
  597. }
  598. item->touchscreen_data->name = SDL_strdup(name);
  599. if (!item->touchscreen_data->name) {
  600. SDL_free(item->touchscreen_data);
  601. return false;
  602. }
  603. ret = ioctl(item->fd, EVIOCGABS(ABS_MT_SLOT), &abs_info);
  604. if (ret < 0) {
  605. SDL_free(item->touchscreen_data->name);
  606. SDL_free(item->touchscreen_data);
  607. SDL_LogError(SDL_LOG_CATEGORY_INPUT, "Failed to get evdev touchscreen limits");
  608. return false;
  609. }
  610. if (abs_info.maximum == 0) {
  611. item->touchscreen_data->max_slots = 1;
  612. xreq = EVIOCGABS(ABS_X);
  613. yreq = EVIOCGABS(ABS_Y);
  614. } else {
  615. item->touchscreen_data->max_slots = abs_info.maximum + 1;
  616. xreq = EVIOCGABS(ABS_MT_POSITION_X);
  617. yreq = EVIOCGABS(ABS_MT_POSITION_Y);
  618. }
  619. ret = ioctl(item->fd, xreq, &abs_info);
  620. if (ret < 0) {
  621. SDL_free(item->touchscreen_data->name);
  622. SDL_free(item->touchscreen_data);
  623. SDL_LogError(SDL_LOG_CATEGORY_INPUT, "Failed to get evdev touchscreen limits");
  624. return false;
  625. }
  626. item->touchscreen_data->min_x = abs_info.minimum;
  627. item->touchscreen_data->max_x = abs_info.maximum;
  628. item->touchscreen_data->range_x = abs_info.maximum - abs_info.minimum;
  629. ret = ioctl(item->fd, yreq, &abs_info);
  630. if (ret < 0) {
  631. SDL_free(item->touchscreen_data->name);
  632. SDL_free(item->touchscreen_data);
  633. SDL_LogError(SDL_LOG_CATEGORY_INPUT, "Failed to get evdev touchscreen limits");
  634. return false;
  635. }
  636. item->touchscreen_data->min_y = abs_info.minimum;
  637. item->touchscreen_data->max_y = abs_info.maximum;
  638. item->touchscreen_data->range_y = abs_info.maximum - abs_info.minimum;
  639. ret = ioctl(item->fd, EVIOCGABS(ABS_MT_PRESSURE), &abs_info);
  640. if (ret < 0) {
  641. SDL_free(item->touchscreen_data->name);
  642. SDL_free(item->touchscreen_data);
  643. SDL_LogError(SDL_LOG_CATEGORY_INPUT, "Failed to get evdev touchscreen limits");
  644. return false;
  645. }
  646. item->touchscreen_data->min_pressure = abs_info.minimum;
  647. item->touchscreen_data->max_pressure = abs_info.maximum;
  648. item->touchscreen_data->range_pressure = abs_info.maximum - abs_info.minimum;
  649. item->touchscreen_data->slots = SDL_calloc(
  650. item->touchscreen_data->max_slots,
  651. sizeof(*item->touchscreen_data->slots));
  652. if (!item->touchscreen_data->slots) {
  653. SDL_free(item->touchscreen_data->name);
  654. SDL_free(item->touchscreen_data);
  655. return false;
  656. }
  657. ret = SDL_AddTouch(item->fd, // I guess our fd is unique enough
  658. (udev_class & SDL_UDEV_DEVICE_TOUCHPAD) ? SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE : SDL_TOUCH_DEVICE_DIRECT,
  659. item->touchscreen_data->name);
  660. if (ret < 0) {
  661. SDL_free(item->touchscreen_data->slots);
  662. SDL_free(item->touchscreen_data->name);
  663. SDL_free(item->touchscreen_data);
  664. return false;
  665. }
  666. return true;
  667. }
  668. static void SDL_EVDEV_destroy_touchscreen(SDL_evdevlist_item *item)
  669. {
  670. if (!item->is_touchscreen) {
  671. return;
  672. }
  673. SDL_DelTouch(item->fd);
  674. SDL_free(item->touchscreen_data->slots);
  675. SDL_free(item->touchscreen_data->name);
  676. SDL_free(item->touchscreen_data);
  677. }
  678. static void SDL_EVDEV_sync_device(SDL_evdevlist_item *item)
  679. {
  680. #ifdef EVIOCGMTSLOTS
  681. int i, ret;
  682. struct input_absinfo abs_info;
  683. /*
  684. * struct input_mt_request_layout {
  685. * __u32 code;
  686. * __s32 values[num_slots];
  687. * };
  688. *
  689. * this is the structure we're trying to emulate
  690. */
  691. Uint32 *mt_req_code;
  692. Sint32 *mt_req_values;
  693. size_t mt_req_size;
  694. // TODO: sync devices other than touchscreen
  695. if (!item->is_touchscreen) {
  696. return;
  697. }
  698. mt_req_size = sizeof(*mt_req_code) +
  699. sizeof(*mt_req_values) * item->touchscreen_data->max_slots;
  700. mt_req_code = SDL_calloc(1, mt_req_size);
  701. if (!mt_req_code) {
  702. return;
  703. }
  704. mt_req_values = (Sint32 *)mt_req_code + 1;
  705. *mt_req_code = ABS_MT_TRACKING_ID;
  706. ret = ioctl(item->fd, EVIOCGMTSLOTS(mt_req_size), mt_req_code);
  707. if (ret < 0) {
  708. SDL_free(mt_req_code);
  709. return;
  710. }
  711. for (i = 0; i < item->touchscreen_data->max_slots; i++) {
  712. /*
  713. * This doesn't account for the very edge case of the user removing their
  714. * finger and replacing it on the screen during the time we're out of sync,
  715. * which'll mean that we're not going from down -> up or up -> down, we're
  716. * going from down -> down but with a different tracking id, meaning we'd
  717. * have to tell SDL of the two events, but since we wait till SYN_REPORT in
  718. * SDL_EVDEV_Poll to tell SDL, the current structure of this code doesn't
  719. * allow it. Lets just pray to God it doesn't happen.
  720. */
  721. if (item->touchscreen_data->slots[i].tracking_id == 0 &&
  722. mt_req_values[i] >= 0) {
  723. item->touchscreen_data->slots[i].tracking_id = mt_req_values[i] + 1;
  724. item->touchscreen_data->slots[i].delta = EVDEV_TOUCH_SLOTDELTA_DOWN;
  725. } else if (item->touchscreen_data->slots[i].tracking_id != 0 &&
  726. mt_req_values[i] < 0) {
  727. item->touchscreen_data->slots[i].tracking_id = 0;
  728. item->touchscreen_data->slots[i].delta = EVDEV_TOUCH_SLOTDELTA_UP;
  729. }
  730. }
  731. *mt_req_code = ABS_MT_POSITION_X;
  732. ret = ioctl(item->fd, EVIOCGMTSLOTS(mt_req_size), mt_req_code);
  733. if (ret < 0) {
  734. SDL_free(mt_req_code);
  735. return;
  736. }
  737. for (i = 0; i < item->touchscreen_data->max_slots; i++) {
  738. if (item->touchscreen_data->slots[i].tracking_id != 0 &&
  739. item->touchscreen_data->slots[i].x != mt_req_values[i]) {
  740. item->touchscreen_data->slots[i].x = mt_req_values[i];
  741. if (item->touchscreen_data->slots[i].delta ==
  742. EVDEV_TOUCH_SLOTDELTA_NONE) {
  743. item->touchscreen_data->slots[i].delta =
  744. EVDEV_TOUCH_SLOTDELTA_MOVE;
  745. }
  746. }
  747. }
  748. *mt_req_code = ABS_MT_POSITION_Y;
  749. ret = ioctl(item->fd, EVIOCGMTSLOTS(mt_req_size), mt_req_code);
  750. if (ret < 0) {
  751. SDL_free(mt_req_code);
  752. return;
  753. }
  754. for (i = 0; i < item->touchscreen_data->max_slots; i++) {
  755. if (item->touchscreen_data->slots[i].tracking_id != 0 &&
  756. item->touchscreen_data->slots[i].y != mt_req_values[i]) {
  757. item->touchscreen_data->slots[i].y = mt_req_values[i];
  758. if (item->touchscreen_data->slots[i].delta ==
  759. EVDEV_TOUCH_SLOTDELTA_NONE) {
  760. item->touchscreen_data->slots[i].delta =
  761. EVDEV_TOUCH_SLOTDELTA_MOVE;
  762. }
  763. }
  764. }
  765. *mt_req_code = ABS_MT_PRESSURE;
  766. ret = ioctl(item->fd, EVIOCGMTSLOTS(mt_req_size), mt_req_code);
  767. if (ret < 0) {
  768. SDL_free(mt_req_code);
  769. return;
  770. }
  771. for (i = 0; i < item->touchscreen_data->max_slots; i++) {
  772. if (item->touchscreen_data->slots[i].tracking_id != 0 &&
  773. item->touchscreen_data->slots[i].pressure != mt_req_values[i]) {
  774. item->touchscreen_data->slots[i].pressure = mt_req_values[i];
  775. if (item->touchscreen_data->slots[i].delta ==
  776. EVDEV_TOUCH_SLOTDELTA_NONE) {
  777. item->touchscreen_data->slots[i].delta =
  778. EVDEV_TOUCH_SLOTDELTA_MOVE;
  779. }
  780. }
  781. }
  782. ret = ioctl(item->fd, EVIOCGABS(ABS_MT_SLOT), &abs_info);
  783. if (ret < 0) {
  784. SDL_free(mt_req_code);
  785. return;
  786. }
  787. item->touchscreen_data->current_slot = abs_info.value;
  788. SDL_free(mt_req_code);
  789. #endif // EVIOCGMTSLOTS
  790. }
  791. static bool SDL_EVDEV_device_added(const char *dev_path, int udev_class)
  792. {
  793. SDL_evdevlist_item *item;
  794. unsigned long relbit[NBITS(REL_MAX)] = { 0 };
  795. // Check to make sure it's not already in list.
  796. for (item = _this->first; item; item = item->next) {
  797. if (SDL_strcmp(dev_path, item->path) == 0) {
  798. return false; // already have this one
  799. }
  800. }
  801. item = (SDL_evdevlist_item *)SDL_calloc(1, sizeof(SDL_evdevlist_item));
  802. if (!item) {
  803. return false;
  804. }
  805. item->fd = open(dev_path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
  806. if (item->fd < 0) {
  807. SDL_LogError(SDL_LOG_CATEGORY_INPUT, "Couldn't open %s: %s", dev_path, strerror(errno));
  808. SDL_free(item);
  809. return false;
  810. }
  811. item->path = SDL_strdup(dev_path);
  812. if (!item->path) {
  813. close(item->fd);
  814. SDL_free(item);
  815. return false;
  816. }
  817. item->udev_class = udev_class;
  818. if (ioctl(item->fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0) {
  819. item->relative_mouse = test_bit(REL_X, relbit) && test_bit(REL_Y, relbit);
  820. item->high_res_wheel = test_bit(REL_WHEEL_HI_RES, relbit);
  821. item->high_res_hwheel = test_bit(REL_HWHEEL_HI_RES, relbit);
  822. }
  823. // For now, we just treat a touchpad like a touchscreen
  824. if (udev_class & (SDL_UDEV_DEVICE_TOUCHSCREEN | SDL_UDEV_DEVICE_TOUCHPAD)) {
  825. item->is_touchscreen = true;
  826. if (!SDL_EVDEV_init_touchscreen(item, udev_class)) {
  827. close(item->fd);
  828. SDL_free(item->path);
  829. SDL_free(item);
  830. return false;
  831. }
  832. }
  833. if (udev_class & SDL_UDEV_DEVICE_MOUSE) {
  834. if (!SDL_EVDEV_init_mouse(item, udev_class)) {
  835. close(item->fd);
  836. SDL_free(item->path);
  837. SDL_free(item);
  838. return false;
  839. }
  840. }
  841. if (udev_class & SDL_UDEV_DEVICE_KEYBOARD) {
  842. if (!SDL_EVDEV_init_keyboard(item, udev_class)) {
  843. close(item->fd);
  844. SDL_free(item->path);
  845. SDL_free(item);
  846. return false;
  847. }
  848. }
  849. if (!_this->last) {
  850. _this->first = _this->last = item;
  851. } else {
  852. _this->last->next = item;
  853. _this->last = item;
  854. }
  855. SDL_EVDEV_sync_device(item);
  856. SDL_EVDEV_UpdateKeyboardMute();
  857. ++_this->num_devices;
  858. return true;
  859. }
  860. static bool SDL_EVDEV_device_removed(const char *dev_path)
  861. {
  862. SDL_evdevlist_item *item;
  863. SDL_evdevlist_item *prev = NULL;
  864. for (item = _this->first; item; item = item->next) {
  865. // found it, remove it.
  866. if (SDL_strcmp(dev_path, item->path) == 0) {
  867. if (prev) {
  868. prev->next = item->next;
  869. } else {
  870. SDL_assert(_this->first == item);
  871. _this->first = item->next;
  872. }
  873. if (item == _this->last) {
  874. _this->last = prev;
  875. }
  876. if (item->is_touchscreen) {
  877. SDL_EVDEV_destroy_touchscreen(item);
  878. }
  879. if (item->udev_class & SDL_UDEV_DEVICE_MOUSE) {
  880. SDL_EVDEV_destroy_mouse(item);
  881. }
  882. if (item->udev_class & SDL_UDEV_DEVICE_KEYBOARD) {
  883. SDL_EVDEV_destroy_keyboard(item);
  884. }
  885. close(item->fd);
  886. SDL_free(item->path);
  887. SDL_free(item);
  888. SDL_EVDEV_UpdateKeyboardMute();
  889. _this->num_devices--;
  890. return true;
  891. }
  892. prev = item;
  893. }
  894. return false;
  895. }
  896. Uint64 SDL_EVDEV_GetEventTimestamp(struct input_event *event)
  897. {
  898. static Uint64 timestamp_offset;
  899. Uint64 timestamp;
  900. Uint64 now = SDL_GetTicksNS();
  901. /* The kernel internally has nanosecond timestamps, but converts it
  902. to microseconds when delivering the events */
  903. timestamp = event->input_event_sec;
  904. timestamp *= SDL_NS_PER_SECOND;
  905. timestamp += SDL_US_TO_NS(event->input_event_usec);
  906. if (!timestamp_offset) {
  907. timestamp_offset = (now - timestamp);
  908. }
  909. timestamp += timestamp_offset;
  910. if (timestamp > now) {
  911. timestamp_offset -= (timestamp - now);
  912. timestamp = now;
  913. }
  914. return timestamp;
  915. }
  916. #endif // SDL_INPUT_LINUXEV