SDL_evdev.c 37 KB

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