SDL_evdev.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  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. SDL_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. SDL_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. SDL_bool high_res_wheel;
  94. SDL_bool high_res_hwheel;
  95. SDL_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 int SDL_EVDEV_device_removed(const char *dev_path);
  114. static int 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 int SDL_EVDEV_SetRelativeMouseMode(SDL_bool enabled)
  129. {
  130. /* Mice already send relative events through this interface */
  131. return 0;
  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, SDL_TRUE);
  137. } else {
  138. SDL_EVDEV_kbd_set_muted(_this->kbd, SDL_FALSE);
  139. }
  140. }
  141. int SDL_EVDEV_Init(void)
  142. {
  143. if (!_this) {
  144. _this = (SDL_EVDEV_PrivateData *)SDL_calloc(1, sizeof(*_this));
  145. if (!_this) {
  146. return -1;
  147. }
  148. #ifdef SDL_USE_LIBUDEV
  149. if (SDL_UDEV_Init() < 0) {
  150. SDL_free(_this);
  151. _this = NULL;
  152. return -1;
  153. }
  154. /* Set up the udev callback */
  155. if (SDL_UDEV_AddCallback(SDL_EVDEV_udev_callback) < 0) {
  156. SDL_UDEV_Quit();
  157. SDL_free(_this);
  158. _this = NULL;
  159. return -1;
  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 0;
  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. mouse_button = event->code - BTN_MOUSE;
  294. if (event->value == 0) {
  295. SDL_SendMouseButton(SDL_EVDEV_GetEventTimestamp(event), mouse->focus, (SDL_MouseID)item->fd, SDL_RELEASED, EVDEV_MouseButtons[mouse_button]);
  296. } else if (event->value == 1) {
  297. SDL_SendMouseButton(SDL_EVDEV_GetEventTimestamp(event), mouse->focus, (SDL_MouseID)item->fd, SDL_PRESSED, EVDEV_MouseButtons[mouse_button]);
  298. }
  299. break;
  300. }
  301. /* BTN_TOUCH event value 1 indicates there is contact with
  302. a touchscreen or trackpad (earlist finger's current
  303. position is sent in EV_ABS ABS_X/ABS_Y, switching to
  304. next finger after earlist is released) */
  305. if (item->is_touchscreen && event->code == BTN_TOUCH) {
  306. if (item->touchscreen_data->max_slots == 1) {
  307. if (event->value) {
  308. item->touchscreen_data->slots[0].delta = EVDEV_TOUCH_SLOTDELTA_DOWN;
  309. } else {
  310. item->touchscreen_data->slots[0].delta = EVDEV_TOUCH_SLOTDELTA_UP;
  311. }
  312. }
  313. break;
  314. }
  315. /* Probably keyboard */
  316. scancode = SDL_EVDEV_translate_keycode(event->code);
  317. if (event->value == 0) {
  318. SDL_SendKeyboardKey(SDL_EVDEV_GetEventTimestamp(event), (SDL_KeyboardID)item->fd, event->code, scancode, SDL_RELEASED);
  319. } else if (event->value == 1 || event->value == 2 /* key repeated */) {
  320. SDL_SendKeyboardKey(SDL_EVDEV_GetEventTimestamp(event), (SDL_KeyboardID)item->fd, event->code, scancode, SDL_PRESSED);
  321. }
  322. SDL_EVDEV_kbd_keycode(_this->kbd, event->code, event->value);
  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. SDL_SendMouseMotion(SDL_EVDEV_GetEventTimestamp(event), mouse->focus, (SDL_MouseID)item->fd, item->relative_mouse, (float)item->mouse_x, (float)item->mouse_y);
  435. item->mouse_x = item->mouse_y = 0;
  436. }
  437. } else if (item->range_x > 0 && item->range_y > 0) {
  438. int screen_w = 0, screen_h = 0;
  439. const SDL_DisplayMode *mode = NULL;
  440. if (mouse->focus) {
  441. mode = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(mouse->focus));
  442. }
  443. if (!mode) {
  444. mode = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay());
  445. }
  446. if (mode) {
  447. screen_w = mode->w;
  448. screen_h = mode->h;
  449. }
  450. SDL_SendMouseMotion(SDL_EVDEV_GetEventTimestamp(event), mouse->focus, (SDL_MouseID)item->fd, item->relative_mouse,
  451. (float)(item->mouse_x - item->min_x) * screen_w / item->range_x,
  452. (float)(item->mouse_y - item->min_y) * screen_h / item->range_y);
  453. }
  454. if (item->mouse_wheel != 0 || item->mouse_hwheel != 0) {
  455. SDL_SendMouseWheel(SDL_EVDEV_GetEventTimestamp(event),
  456. mouse->focus, (SDL_MouseID)item->fd,
  457. item->mouse_hwheel / (item->high_res_hwheel ? 120.0f : 1.0f),
  458. item->mouse_wheel / (item->high_res_wheel ? 120.0f : 1.0f),
  459. SDL_MOUSEWHEEL_NORMAL);
  460. item->mouse_wheel = item->mouse_hwheel = 0;
  461. }
  462. if (!item->is_touchscreen) { /* FIXME: temp hack */
  463. break;
  464. }
  465. for (j = 0; j < item->touchscreen_data->max_slots; j++) {
  466. norm_x = (float)(item->touchscreen_data->slots[j].x - item->touchscreen_data->min_x) /
  467. (float)item->touchscreen_data->range_x;
  468. norm_y = (float)(item->touchscreen_data->slots[j].y - item->touchscreen_data->min_y) /
  469. (float)item->touchscreen_data->range_y;
  470. if (item->touchscreen_data->range_pressure > 0) {
  471. norm_pressure = (float)(item->touchscreen_data->slots[j].pressure - item->touchscreen_data->min_pressure) /
  472. (float)item->touchscreen_data->range_pressure;
  473. } else {
  474. /* This touchscreen does not support pressure */
  475. norm_pressure = 1.0f;
  476. }
  477. /* FIXME: the touch's window shouldn't be null, but
  478. * the coordinate space of touch positions needs to
  479. * be window-relative in that case. */
  480. switch (item->touchscreen_data->slots[j].delta) {
  481. case EVDEV_TOUCH_SLOTDELTA_DOWN:
  482. SDL_SendTouch(SDL_EVDEV_GetEventTimestamp(event), item->fd, item->touchscreen_data->slots[j].tracking_id, NULL, SDL_TRUE, norm_x, norm_y, norm_pressure);
  483. item->touchscreen_data->slots[j].delta = EVDEV_TOUCH_SLOTDELTA_NONE;
  484. break;
  485. case EVDEV_TOUCH_SLOTDELTA_UP:
  486. SDL_SendTouch(SDL_EVDEV_GetEventTimestamp(event), item->fd, item->touchscreen_data->slots[j].tracking_id, NULL, SDL_FALSE, norm_x, norm_y, norm_pressure);
  487. item->touchscreen_data->slots[j].tracking_id = 0;
  488. item->touchscreen_data->slots[j].delta = EVDEV_TOUCH_SLOTDELTA_NONE;
  489. break;
  490. case EVDEV_TOUCH_SLOTDELTA_MOVE:
  491. SDL_SendTouchMotion(SDL_EVDEV_GetEventTimestamp(event), item->fd, item->touchscreen_data->slots[j].tracking_id, NULL, norm_x, norm_y, norm_pressure);
  492. item->touchscreen_data->slots[j].delta = EVDEV_TOUCH_SLOTDELTA_NONE;
  493. break;
  494. default:
  495. break;
  496. }
  497. }
  498. if (item->out_of_sync) {
  499. item->out_of_sync = SDL_FALSE;
  500. }
  501. break;
  502. case SYN_DROPPED:
  503. if (item->is_touchscreen) {
  504. item->out_of_sync = SDL_TRUE;
  505. }
  506. SDL_EVDEV_sync_device(item);
  507. break;
  508. default:
  509. break;
  510. }
  511. break;
  512. }
  513. }
  514. }
  515. }
  516. }
  517. static SDL_Scancode SDL_EVDEV_translate_keycode(int keycode)
  518. {
  519. SDL_Scancode scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_LINUX, keycode);
  520. #ifdef DEBUG_SCANCODES
  521. if (scancode == SDL_SCANCODE_UNKNOWN) {
  522. /* BTN_TOUCH is handled elsewhere, but we might still end up here if
  523. you get an unexpected BTN_TOUCH from something SDL believes is not
  524. a touch device. In this case, we'd rather not get a misleading
  525. SDL_Log message about an unknown key. */
  526. if (keycode != BTN_TOUCH) {
  527. SDL_Log("The key you just pressed is not recognized by SDL. To help "
  528. "get this fixed, please report this to the SDL forums/mailing list "
  529. "<https://discourse.libsdl.org/> EVDEV KeyCode %d",
  530. keycode);
  531. }
  532. }
  533. #endif /* DEBUG_SCANCODES */
  534. return scancode;
  535. }
  536. static int SDL_EVDEV_init_keyboard(SDL_evdevlist_item *item, int udev_class)
  537. {
  538. char name[128];
  539. name[0] = '\0';
  540. ioctl(item->fd, EVIOCGNAME(sizeof(name)), name);
  541. SDL_AddKeyboard((SDL_KeyboardID)item->fd, name, SDL_TRUE);
  542. return 0;
  543. }
  544. static void SDL_EVDEV_destroy_keyboard(SDL_evdevlist_item *item)
  545. {
  546. SDL_RemoveKeyboard((SDL_KeyboardID)item->fd, SDL_TRUE);
  547. }
  548. static int SDL_EVDEV_init_mouse(SDL_evdevlist_item *item, int udev_class)
  549. {
  550. char name[128];
  551. int ret;
  552. struct input_absinfo abs_info;
  553. name[0] = '\0';
  554. ioctl(item->fd, EVIOCGNAME(sizeof(name)), name);
  555. SDL_AddMouse((SDL_MouseID)item->fd, name, SDL_TRUE);
  556. ret = ioctl(item->fd, EVIOCGABS(ABS_X), &abs_info);
  557. if (ret < 0) {
  558. // no absolute mode info, continue
  559. return 0;
  560. }
  561. item->min_x = abs_info.minimum;
  562. item->max_x = abs_info.maximum;
  563. item->range_x = abs_info.maximum - abs_info.minimum;
  564. ret = ioctl(item->fd, EVIOCGABS(ABS_Y), &abs_info);
  565. if (ret < 0) {
  566. // no absolute mode info, continue
  567. return 0;
  568. }
  569. item->min_y = abs_info.minimum;
  570. item->max_y = abs_info.maximum;
  571. item->range_y = abs_info.maximum - abs_info.minimum;
  572. return 0;
  573. }
  574. static void SDL_EVDEV_destroy_mouse(SDL_evdevlist_item *item)
  575. {
  576. SDL_RemoveMouse((SDL_MouseID)item->fd, SDL_TRUE);
  577. }
  578. static int SDL_EVDEV_init_touchscreen(SDL_evdevlist_item *item, int udev_class)
  579. {
  580. int ret;
  581. unsigned long xreq, yreq;
  582. char name[64];
  583. struct input_absinfo abs_info;
  584. if (!item->is_touchscreen) {
  585. return 0;
  586. }
  587. item->touchscreen_data = SDL_calloc(1, sizeof(*item->touchscreen_data));
  588. if (!item->touchscreen_data) {
  589. return -1;
  590. }
  591. ret = ioctl(item->fd, EVIOCGNAME(sizeof(name)), name);
  592. if (ret < 0) {
  593. SDL_free(item->touchscreen_data);
  594. return SDL_SetError("Failed to get evdev touchscreen name");
  595. }
  596. item->touchscreen_data->name = SDL_strdup(name);
  597. if (!item->touchscreen_data->name) {
  598. SDL_free(item->touchscreen_data);
  599. return -1;
  600. }
  601. ret = ioctl(item->fd, EVIOCGABS(ABS_MT_SLOT), &abs_info);
  602. if (ret < 0) {
  603. SDL_free(item->touchscreen_data->name);
  604. SDL_free(item->touchscreen_data);
  605. return SDL_SetError("Failed to get evdev touchscreen limits");
  606. }
  607. if (abs_info.maximum == 0) {
  608. item->touchscreen_data->max_slots = 1;
  609. xreq = EVIOCGABS(ABS_X);
  610. yreq = EVIOCGABS(ABS_Y);
  611. } else {
  612. item->touchscreen_data->max_slots = abs_info.maximum + 1;
  613. xreq = EVIOCGABS(ABS_MT_POSITION_X);
  614. yreq = EVIOCGABS(ABS_MT_POSITION_Y);
  615. }
  616. ret = ioctl(item->fd, xreq, &abs_info);
  617. if (ret < 0) {
  618. SDL_free(item->touchscreen_data->name);
  619. SDL_free(item->touchscreen_data);
  620. return SDL_SetError("Failed to get evdev touchscreen limits");
  621. }
  622. item->touchscreen_data->min_x = abs_info.minimum;
  623. item->touchscreen_data->max_x = abs_info.maximum;
  624. item->touchscreen_data->range_x = abs_info.maximum - abs_info.minimum;
  625. ret = ioctl(item->fd, yreq, &abs_info);
  626. if (ret < 0) {
  627. SDL_free(item->touchscreen_data->name);
  628. SDL_free(item->touchscreen_data);
  629. return SDL_SetError("Failed to get evdev touchscreen limits");
  630. }
  631. item->touchscreen_data->min_y = abs_info.minimum;
  632. item->touchscreen_data->max_y = abs_info.maximum;
  633. item->touchscreen_data->range_y = abs_info.maximum - abs_info.minimum;
  634. ret = ioctl(item->fd, EVIOCGABS(ABS_MT_PRESSURE), &abs_info);
  635. if (ret < 0) {
  636. SDL_free(item->touchscreen_data->name);
  637. SDL_free(item->touchscreen_data);
  638. return SDL_SetError("Failed to get evdev touchscreen limits");
  639. }
  640. item->touchscreen_data->min_pressure = abs_info.minimum;
  641. item->touchscreen_data->max_pressure = abs_info.maximum;
  642. item->touchscreen_data->range_pressure = abs_info.maximum - abs_info.minimum;
  643. item->touchscreen_data->slots = SDL_calloc(
  644. item->touchscreen_data->max_slots,
  645. sizeof(*item->touchscreen_data->slots));
  646. if (!item->touchscreen_data->slots) {
  647. SDL_free(item->touchscreen_data->name);
  648. SDL_free(item->touchscreen_data);
  649. return -1;
  650. }
  651. ret = SDL_AddTouch(item->fd, /* I guess our fd is unique enough */
  652. (udev_class & SDL_UDEV_DEVICE_TOUCHPAD) ? SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE : SDL_TOUCH_DEVICE_DIRECT,
  653. item->touchscreen_data->name);
  654. if (ret < 0) {
  655. SDL_free(item->touchscreen_data->slots);
  656. SDL_free(item->touchscreen_data->name);
  657. SDL_free(item->touchscreen_data);
  658. return ret;
  659. }
  660. return 0;
  661. }
  662. static void SDL_EVDEV_destroy_touchscreen(SDL_evdevlist_item *item)
  663. {
  664. if (!item->is_touchscreen) {
  665. return;
  666. }
  667. SDL_DelTouch(item->fd);
  668. SDL_free(item->touchscreen_data->slots);
  669. SDL_free(item->touchscreen_data->name);
  670. SDL_free(item->touchscreen_data);
  671. }
  672. static void SDL_EVDEV_sync_device(SDL_evdevlist_item *item)
  673. {
  674. #ifdef EVIOCGMTSLOTS
  675. int i, ret;
  676. struct input_absinfo abs_info;
  677. /*
  678. * struct input_mt_request_layout {
  679. * __u32 code;
  680. * __s32 values[num_slots];
  681. * };
  682. *
  683. * this is the structure we're trying to emulate
  684. */
  685. Uint32 *mt_req_code;
  686. Sint32 *mt_req_values;
  687. size_t mt_req_size;
  688. /* TODO: sync devices other than touchscreen */
  689. if (!item->is_touchscreen) {
  690. return;
  691. }
  692. mt_req_size = sizeof(*mt_req_code) +
  693. sizeof(*mt_req_values) * item->touchscreen_data->max_slots;
  694. mt_req_code = SDL_calloc(1, mt_req_size);
  695. if (!mt_req_code) {
  696. return;
  697. }
  698. mt_req_values = (Sint32 *)mt_req_code + 1;
  699. *mt_req_code = ABS_MT_TRACKING_ID;
  700. ret = ioctl(item->fd, EVIOCGMTSLOTS(mt_req_size), mt_req_code);
  701. if (ret < 0) {
  702. SDL_free(mt_req_code);
  703. return;
  704. }
  705. for (i = 0; i < item->touchscreen_data->max_slots; i++) {
  706. /*
  707. * This doesn't account for the very edge case of the user removing their
  708. * finger and replacing it on the screen during the time we're out of sync,
  709. * which'll mean that we're not going from down -> up or up -> down, we're
  710. * going from down -> down but with a different tracking id, meaning we'd
  711. * have to tell SDL of the two events, but since we wait till SYN_REPORT in
  712. * SDL_EVDEV_Poll to tell SDL, the current structure of this code doesn't
  713. * allow it. Lets just pray to God it doesn't happen.
  714. */
  715. if (item->touchscreen_data->slots[i].tracking_id == 0 &&
  716. mt_req_values[i] >= 0) {
  717. item->touchscreen_data->slots[i].tracking_id = mt_req_values[i] + 1;
  718. item->touchscreen_data->slots[i].delta = EVDEV_TOUCH_SLOTDELTA_DOWN;
  719. } else if (item->touchscreen_data->slots[i].tracking_id != 0 &&
  720. mt_req_values[i] < 0) {
  721. item->touchscreen_data->slots[i].tracking_id = 0;
  722. item->touchscreen_data->slots[i].delta = EVDEV_TOUCH_SLOTDELTA_UP;
  723. }
  724. }
  725. *mt_req_code = ABS_MT_POSITION_X;
  726. ret = ioctl(item->fd, EVIOCGMTSLOTS(mt_req_size), mt_req_code);
  727. if (ret < 0) {
  728. SDL_free(mt_req_code);
  729. return;
  730. }
  731. for (i = 0; i < item->touchscreen_data->max_slots; i++) {
  732. if (item->touchscreen_data->slots[i].tracking_id != 0 &&
  733. item->touchscreen_data->slots[i].x != mt_req_values[i]) {
  734. item->touchscreen_data->slots[i].x = mt_req_values[i];
  735. if (item->touchscreen_data->slots[i].delta ==
  736. EVDEV_TOUCH_SLOTDELTA_NONE) {
  737. item->touchscreen_data->slots[i].delta =
  738. EVDEV_TOUCH_SLOTDELTA_MOVE;
  739. }
  740. }
  741. }
  742. *mt_req_code = ABS_MT_POSITION_Y;
  743. ret = ioctl(item->fd, EVIOCGMTSLOTS(mt_req_size), mt_req_code);
  744. if (ret < 0) {
  745. SDL_free(mt_req_code);
  746. return;
  747. }
  748. for (i = 0; i < item->touchscreen_data->max_slots; i++) {
  749. if (item->touchscreen_data->slots[i].tracking_id != 0 &&
  750. item->touchscreen_data->slots[i].y != mt_req_values[i]) {
  751. item->touchscreen_data->slots[i].y = mt_req_values[i];
  752. if (item->touchscreen_data->slots[i].delta ==
  753. EVDEV_TOUCH_SLOTDELTA_NONE) {
  754. item->touchscreen_data->slots[i].delta =
  755. EVDEV_TOUCH_SLOTDELTA_MOVE;
  756. }
  757. }
  758. }
  759. *mt_req_code = ABS_MT_PRESSURE;
  760. ret = ioctl(item->fd, EVIOCGMTSLOTS(mt_req_size), mt_req_code);
  761. if (ret < 0) {
  762. SDL_free(mt_req_code);
  763. return;
  764. }
  765. for (i = 0; i < item->touchscreen_data->max_slots; i++) {
  766. if (item->touchscreen_data->slots[i].tracking_id != 0 &&
  767. item->touchscreen_data->slots[i].pressure != mt_req_values[i]) {
  768. item->touchscreen_data->slots[i].pressure = mt_req_values[i];
  769. if (item->touchscreen_data->slots[i].delta ==
  770. EVDEV_TOUCH_SLOTDELTA_NONE) {
  771. item->touchscreen_data->slots[i].delta =
  772. EVDEV_TOUCH_SLOTDELTA_MOVE;
  773. }
  774. }
  775. }
  776. ret = ioctl(item->fd, EVIOCGABS(ABS_MT_SLOT), &abs_info);
  777. if (ret < 0) {
  778. SDL_free(mt_req_code);
  779. return;
  780. }
  781. item->touchscreen_data->current_slot = abs_info.value;
  782. SDL_free(mt_req_code);
  783. #endif /* EVIOCGMTSLOTS */
  784. }
  785. static int SDL_EVDEV_device_added(const char *dev_path, int udev_class)
  786. {
  787. SDL_evdevlist_item *item;
  788. unsigned long relbit[NBITS(REL_MAX)] = { 0 };
  789. /* Check to make sure it's not already in list. */
  790. for (item = _this->first; item; item = item->next) {
  791. if (SDL_strcmp(dev_path, item->path) == 0) {
  792. return -1; /* already have this one */
  793. }
  794. }
  795. item = (SDL_evdevlist_item *)SDL_calloc(1, sizeof(SDL_evdevlist_item));
  796. if (!item) {
  797. return -1;
  798. }
  799. item->fd = open(dev_path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
  800. if (item->fd < 0) {
  801. SDL_free(item);
  802. return SDL_SetError("Unable to open %s", dev_path);
  803. }
  804. item->path = SDL_strdup(dev_path);
  805. if (!item->path) {
  806. close(item->fd);
  807. SDL_free(item);
  808. return -1;
  809. }
  810. item->udev_class = udev_class;
  811. if (ioctl(item->fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0) {
  812. item->relative_mouse = test_bit(REL_X, relbit) && test_bit(REL_Y, relbit);
  813. item->high_res_wheel = test_bit(REL_WHEEL_HI_RES, relbit);
  814. item->high_res_hwheel = test_bit(REL_HWHEEL_HI_RES, relbit);
  815. }
  816. /* For now, we just treat a touchpad like a touchscreen */
  817. if (udev_class & (SDL_UDEV_DEVICE_TOUCHSCREEN | SDL_UDEV_DEVICE_TOUCHPAD)) {
  818. int ret;
  819. item->is_touchscreen = SDL_TRUE;
  820. ret = SDL_EVDEV_init_touchscreen(item, udev_class);
  821. if (ret < 0) {
  822. close(item->fd);
  823. SDL_free(item->path);
  824. SDL_free(item);
  825. return ret;
  826. }
  827. } else if (udev_class & SDL_UDEV_DEVICE_MOUSE) {
  828. int ret = SDL_EVDEV_init_mouse(item, udev_class);
  829. if (ret < 0) {
  830. close(item->fd);
  831. SDL_free(item->path);
  832. SDL_free(item);
  833. return ret;
  834. }
  835. } else if (udev_class & SDL_UDEV_DEVICE_KEYBOARD) {
  836. int ret = SDL_EVDEV_init_keyboard(item, udev_class);
  837. if (ret < 0) {
  838. close(item->fd);
  839. SDL_free(item->path);
  840. SDL_free(item);
  841. return ret;
  842. }
  843. }
  844. if (!_this->last) {
  845. _this->first = _this->last = item;
  846. } else {
  847. _this->last->next = item;
  848. _this->last = item;
  849. }
  850. SDL_EVDEV_sync_device(item);
  851. SDL_EVDEV_UpdateKeyboardMute();
  852. return _this->num_devices++;
  853. }
  854. static int SDL_EVDEV_device_removed(const char *dev_path)
  855. {
  856. SDL_evdevlist_item *item;
  857. SDL_evdevlist_item *prev = NULL;
  858. for (item = _this->first; item; item = item->next) {
  859. /* found it, remove it. */
  860. if (SDL_strcmp(dev_path, item->path) == 0) {
  861. if (prev) {
  862. prev->next = item->next;
  863. } else {
  864. SDL_assert(_this->first == item);
  865. _this->first = item->next;
  866. }
  867. if (item == _this->last) {
  868. _this->last = prev;
  869. }
  870. if (item->is_touchscreen) {
  871. SDL_EVDEV_destroy_touchscreen(item);
  872. } else if (item->udev_class & SDL_UDEV_DEVICE_MOUSE) {
  873. SDL_EVDEV_destroy_mouse(item);
  874. } else if (item->udev_class & SDL_UDEV_DEVICE_KEYBOARD) {
  875. SDL_EVDEV_destroy_keyboard(item);
  876. }
  877. close(item->fd);
  878. SDL_free(item->path);
  879. SDL_free(item);
  880. SDL_EVDEV_UpdateKeyboardMute();
  881. _this->num_devices--;
  882. return 0;
  883. }
  884. prev = item;
  885. }
  886. return -1;
  887. }
  888. Uint64 SDL_EVDEV_GetEventTimestamp(struct input_event *event)
  889. {
  890. static Uint64 timestamp_offset;
  891. Uint64 timestamp;
  892. Uint64 now = SDL_GetTicksNS();
  893. /* The kernel internally has nanosecond timestamps, but converts it
  894. to microseconds when delivering the events */
  895. timestamp = event->input_event_sec;
  896. timestamp *= SDL_NS_PER_SECOND;
  897. timestamp += SDL_US_TO_NS(event->input_event_usec);
  898. if (!timestamp_offset) {
  899. timestamp_offset = (now - timestamp);
  900. }
  901. timestamp += timestamp_offset;
  902. if (timestamp > now) {
  903. timestamp_offset -= (timestamp - now);
  904. timestamp = now;
  905. }
  906. return timestamp;
  907. }
  908. #endif /* SDL_INPUT_LINUXEV */