SDL_evdev.c 37 KB

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