SDL_udev.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 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. /*
  19. * To list the properties of a device, try something like:
  20. * udevadm info -a -n snd/hwC0D0 (for a sound card)
  21. * udevadm info --query=all -n input/event3 (for a keyboard, mouse, etc)
  22. * udevadm info --query=property -n input/event2
  23. */
  24. #include "SDL_udev.h"
  25. #ifdef SDL_USE_LIBUDEV
  26. #include <linux/input.h>
  27. #include "SDL_assert.h"
  28. #include "SDL_loadso.h"
  29. #include "SDL_timer.h"
  30. #include "../unix/SDL_poll.h"
  31. static const char *SDL_UDEV_LIBS[] = {
  32. #ifdef SDL_UDEV_DYNAMIC
  33. SDL_UDEV_DYNAMIC
  34. #else
  35. "libudev.so.1",
  36. "libudev.so.0"
  37. #endif
  38. };
  39. #define _THIS SDL_UDEV_PrivateData *_this
  40. static _THIS = NULL;
  41. static SDL_bool SDL_UDEV_load_sym(const char *fn, void **addr);
  42. static int SDL_UDEV_load_syms(void);
  43. static SDL_bool SDL_UDEV_hotplug_update_available(void);
  44. static void device_event(SDL_UDEV_deviceevent type, struct udev_device *dev);
  45. static SDL_bool
  46. SDL_UDEV_load_sym(const char *fn, void **addr)
  47. {
  48. *addr = SDL_LoadFunction(_this->udev_handle, fn);
  49. if (*addr == NULL) {
  50. /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
  51. return SDL_FALSE;
  52. }
  53. return SDL_TRUE;
  54. }
  55. static int
  56. SDL_UDEV_load_syms(void)
  57. {
  58. /* cast funcs to char* first, to please GCC's strict aliasing rules. */
  59. #define SDL_UDEV_SYM(x) \
  60. if (!SDL_UDEV_load_sym(#x, (void **) (char *) & _this->x)) return -1
  61. SDL_UDEV_SYM(udev_device_get_action);
  62. SDL_UDEV_SYM(udev_device_get_devnode);
  63. SDL_UDEV_SYM(udev_device_get_subsystem);
  64. SDL_UDEV_SYM(udev_device_get_parent_with_subsystem_devtype);
  65. SDL_UDEV_SYM(udev_device_get_property_value);
  66. SDL_UDEV_SYM(udev_device_get_sysattr_value);
  67. SDL_UDEV_SYM(udev_device_new_from_syspath);
  68. SDL_UDEV_SYM(udev_device_unref);
  69. SDL_UDEV_SYM(udev_enumerate_add_match_property);
  70. SDL_UDEV_SYM(udev_enumerate_add_match_subsystem);
  71. SDL_UDEV_SYM(udev_enumerate_get_list_entry);
  72. SDL_UDEV_SYM(udev_enumerate_new);
  73. SDL_UDEV_SYM(udev_enumerate_scan_devices);
  74. SDL_UDEV_SYM(udev_enumerate_unref);
  75. SDL_UDEV_SYM(udev_list_entry_get_name);
  76. SDL_UDEV_SYM(udev_list_entry_get_next);
  77. SDL_UDEV_SYM(udev_monitor_enable_receiving);
  78. SDL_UDEV_SYM(udev_monitor_filter_add_match_subsystem_devtype);
  79. SDL_UDEV_SYM(udev_monitor_get_fd);
  80. SDL_UDEV_SYM(udev_monitor_new_from_netlink);
  81. SDL_UDEV_SYM(udev_monitor_receive_device);
  82. SDL_UDEV_SYM(udev_monitor_unref);
  83. SDL_UDEV_SYM(udev_new);
  84. SDL_UDEV_SYM(udev_unref);
  85. SDL_UDEV_SYM(udev_device_new_from_devnum);
  86. SDL_UDEV_SYM(udev_device_get_devnum);
  87. #undef SDL_UDEV_SYM
  88. return 0;
  89. }
  90. static SDL_bool
  91. SDL_UDEV_hotplug_update_available(void)
  92. {
  93. if (_this->udev_mon != NULL) {
  94. const int fd = _this->udev_monitor_get_fd(_this->udev_mon);
  95. if (SDL_IOReady(fd, SDL_FALSE, 0)) {
  96. return SDL_TRUE;
  97. }
  98. }
  99. return SDL_FALSE;
  100. }
  101. int
  102. SDL_UDEV_Init(void)
  103. {
  104. int retval = 0;
  105. if (_this == NULL) {
  106. _this = (SDL_UDEV_PrivateData *) SDL_calloc(1, sizeof(*_this));
  107. if(_this == NULL) {
  108. return SDL_OutOfMemory();
  109. }
  110. retval = SDL_UDEV_LoadLibrary();
  111. if (retval < 0) {
  112. SDL_UDEV_Quit();
  113. return retval;
  114. }
  115. /* Set up udev monitoring
  116. * Listen for input devices (mouse, keyboard, joystick, etc) and sound devices
  117. */
  118. _this->udev = _this->udev_new();
  119. if (_this->udev == NULL) {
  120. SDL_UDEV_Quit();
  121. return SDL_SetError("udev_new() failed");
  122. }
  123. _this->udev_mon = _this->udev_monitor_new_from_netlink(_this->udev, "udev");
  124. if (_this->udev_mon == NULL) {
  125. SDL_UDEV_Quit();
  126. return SDL_SetError("udev_monitor_new_from_netlink() failed");
  127. }
  128. _this->udev_monitor_filter_add_match_subsystem_devtype(_this->udev_mon, "input", NULL);
  129. _this->udev_monitor_filter_add_match_subsystem_devtype(_this->udev_mon, "sound", NULL);
  130. _this->udev_monitor_enable_receiving(_this->udev_mon);
  131. /* Do an initial scan of existing devices */
  132. SDL_UDEV_Scan();
  133. }
  134. _this->ref_count += 1;
  135. return retval;
  136. }
  137. void
  138. SDL_UDEV_Quit(void)
  139. {
  140. SDL_UDEV_CallbackList *item;
  141. printf("We do indeed get here\n");
  142. if (_this == NULL) {
  143. return;
  144. }
  145. _this->ref_count -= 1;
  146. if (_this->ref_count < 1) {
  147. if (_this->udev_mon != NULL) {
  148. _this->udev_monitor_unref(_this->udev_mon);
  149. _this->udev_mon = NULL;
  150. }
  151. if (_this->udev != NULL) {
  152. _this->udev_unref(_this->udev);
  153. _this->udev = NULL;
  154. }
  155. /* Remove existing devices */
  156. while (_this->first != NULL) {
  157. item = _this->first;
  158. _this->first = _this->first->next;
  159. SDL_free(item);
  160. }
  161. SDL_UDEV_UnloadLibrary();
  162. SDL_free(_this);
  163. _this = NULL;
  164. }
  165. }
  166. void
  167. SDL_UDEV_Scan(void)
  168. {
  169. struct udev_enumerate *enumerate = NULL;
  170. struct udev_list_entry *devs = NULL;
  171. struct udev_list_entry *item = NULL;
  172. if (_this == NULL) {
  173. return;
  174. }
  175. enumerate = _this->udev_enumerate_new(_this->udev);
  176. if (enumerate == NULL) {
  177. SDL_UDEV_Quit();
  178. SDL_SetError("udev_enumerate_new() failed");
  179. return;
  180. }
  181. _this->udev_enumerate_add_match_subsystem(enumerate, "input");
  182. _this->udev_enumerate_add_match_subsystem(enumerate, "sound");
  183. _this->udev_enumerate_scan_devices(enumerate);
  184. devs = _this->udev_enumerate_get_list_entry(enumerate);
  185. for (item = devs; item; item = _this->udev_list_entry_get_next(item)) {
  186. const char *path = _this->udev_list_entry_get_name(item);
  187. struct udev_device *dev = _this->udev_device_new_from_syspath(_this->udev, path);
  188. if (dev != NULL) {
  189. device_event(SDL_UDEV_DEVICEADDED, dev);
  190. _this->udev_device_unref(dev);
  191. }
  192. }
  193. _this->udev_enumerate_unref(enumerate);
  194. }
  195. void
  196. SDL_UDEV_UnloadLibrary(void)
  197. {
  198. if (_this == NULL) {
  199. return;
  200. }
  201. if (_this->udev_handle != NULL) {
  202. SDL_UnloadObject(_this->udev_handle);
  203. _this->udev_handle = NULL;
  204. }
  205. }
  206. int
  207. SDL_UDEV_LoadLibrary(void)
  208. {
  209. int retval = 0, i;
  210. if (_this == NULL) {
  211. return SDL_SetError("UDEV not initialized");
  212. }
  213. /* See if there is a udev library already loaded */
  214. if (SDL_UDEV_load_syms() == 0) {
  215. return 0;
  216. }
  217. if (_this->udev_handle == NULL) {
  218. for( i = 0 ; i < SDL_arraysize(SDL_UDEV_LIBS); i++) {
  219. _this->udev_handle = SDL_LoadObject(SDL_UDEV_LIBS[i]);
  220. if (_this->udev_handle != NULL) {
  221. retval = SDL_UDEV_load_syms();
  222. if (retval < 0) {
  223. SDL_UDEV_UnloadLibrary();
  224. }
  225. else {
  226. break;
  227. }
  228. }
  229. }
  230. if (_this->udev_handle == NULL) {
  231. retval = -1;
  232. /* Don't call SDL_SetError(): SDL_LoadObject already did. */
  233. }
  234. }
  235. return retval;
  236. }
  237. #define BITS_PER_LONG (sizeof(unsigned long) * 8)
  238. #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
  239. #define OFF(x) ((x)%BITS_PER_LONG)
  240. #define LONG(x) ((x)/BITS_PER_LONG)
  241. #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
  242. static void get_caps(struct udev_device *dev, struct udev_device *pdev, const char *attr, unsigned long *bitmask, size_t bitmask_len)
  243. {
  244. const char *value;
  245. char text[4096];
  246. char *word;
  247. int i;
  248. unsigned long v;
  249. SDL_memset(bitmask, 0, bitmask_len*sizeof(*bitmask));
  250. value = _this->udev_device_get_sysattr_value(pdev, attr);
  251. if (!value) {
  252. return;
  253. }
  254. SDL_strlcpy(text, value, sizeof(text));
  255. i = 0;
  256. while ((word = SDL_strrchr(text, ' ')) != NULL) {
  257. v = SDL_strtoul(word+1, NULL, 16);
  258. if (i < bitmask_len) {
  259. bitmask[i] = v;
  260. }
  261. ++i;
  262. *word = '\0';
  263. }
  264. v = SDL_strtoul(text, NULL, 16);
  265. if (i < bitmask_len) {
  266. bitmask[i] = v;
  267. }
  268. }
  269. static int
  270. guess_device_class(struct udev_device *dev)
  271. {
  272. int devclass = 0;
  273. struct udev_device *pdev;
  274. unsigned long bitmask_ev[NBITS(EV_MAX)];
  275. unsigned long bitmask_abs[NBITS(ABS_MAX)];
  276. unsigned long bitmask_key[NBITS(KEY_MAX)];
  277. unsigned long bitmask_rel[NBITS(REL_MAX)];
  278. unsigned long keyboard_mask;
  279. /* walk up the parental chain until we find the real input device; the
  280. * argument is very likely a subdevice of this, like eventN */
  281. pdev = dev;
  282. while (pdev && !_this->udev_device_get_sysattr_value(pdev, "capabilities/ev")) {
  283. pdev = _this->udev_device_get_parent_with_subsystem_devtype(pdev, "input", NULL);
  284. }
  285. if (!pdev) {
  286. return 0;
  287. }
  288. get_caps(dev, pdev, "capabilities/ev", bitmask_ev, SDL_arraysize(bitmask_ev));
  289. get_caps(dev, pdev, "capabilities/abs", bitmask_abs, SDL_arraysize(bitmask_abs));
  290. get_caps(dev, pdev, "capabilities/rel", bitmask_rel, SDL_arraysize(bitmask_rel));
  291. get_caps(dev, pdev, "capabilities/key", bitmask_key, SDL_arraysize(bitmask_key));
  292. if (test_bit(EV_ABS, bitmask_ev) &&
  293. test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs)) {
  294. if (test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key)) {
  295. ; /* ID_INPUT_TABLET */
  296. } else if (test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key)) {
  297. ; /* ID_INPUT_TOUCHPAD */
  298. } else if (test_bit(BTN_MOUSE, bitmask_key)) {
  299. devclass |= SDL_UDEV_DEVICE_MOUSE; /* ID_INPUT_MOUSE */
  300. } else if (test_bit(BTN_TOUCH, bitmask_key)) {
  301. /* TODO: better determining between touchscreen and multitouch touchpad,
  302. see https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-input_id.c */
  303. devclass |= SDL_UDEV_DEVICE_TOUCHSCREEN; /* ID_INPUT_TOUCHSCREEN */
  304. }
  305. if (test_bit(BTN_TRIGGER, bitmask_key) ||
  306. test_bit(BTN_A, bitmask_key) ||
  307. test_bit(BTN_1, bitmask_key) ||
  308. test_bit(ABS_RX, bitmask_abs) ||
  309. test_bit(ABS_RY, bitmask_abs) ||
  310. test_bit(ABS_RZ, bitmask_abs) ||
  311. test_bit(ABS_THROTTLE, bitmask_abs) ||
  312. test_bit(ABS_RUDDER, bitmask_abs) ||
  313. test_bit(ABS_WHEEL, bitmask_abs) ||
  314. test_bit(ABS_GAS, bitmask_abs) ||
  315. test_bit(ABS_BRAKE, bitmask_abs)) {
  316. devclass |= SDL_UDEV_DEVICE_JOYSTICK; /* ID_INPUT_JOYSTICK */
  317. }
  318. }
  319. if (test_bit(EV_REL, bitmask_ev) &&
  320. test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel) &&
  321. test_bit(BTN_MOUSE, bitmask_key)) {
  322. devclass |= SDL_UDEV_DEVICE_MOUSE; /* ID_INPUT_MOUSE */
  323. }
  324. /* the first 32 bits are ESC, numbers, and Q to D; if we have any of
  325. * those, consider it a keyboard device; do not test KEY_RESERVED, though */
  326. keyboard_mask = 0xFFFFFFFE;
  327. if ((bitmask_key[0] & keyboard_mask) != 0)
  328. devclass |= SDL_UDEV_DEVICE_KEYBOARD; /* ID_INPUT_KEYBOARD */
  329. return devclass;
  330. }
  331. static void
  332. device_event(SDL_UDEV_deviceevent type, struct udev_device *dev)
  333. {
  334. const char *subsystem;
  335. const char *val = NULL;
  336. int devclass = 0;
  337. const char *path;
  338. SDL_UDEV_CallbackList *item;
  339. path = _this->udev_device_get_devnode(dev);
  340. if (path == NULL) {
  341. return;
  342. }
  343. subsystem = _this->udev_device_get_subsystem(dev);
  344. if (SDL_strcmp(subsystem, "sound") == 0) {
  345. devclass = SDL_UDEV_DEVICE_SOUND;
  346. } else if (SDL_strcmp(subsystem, "input") == 0) {
  347. /* udev rules reference: http://cgit.freedesktop.org/systemd/systemd/tree/src/udev/udev-builtin-input_id.c */
  348. val = _this->udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK");
  349. if (val != NULL && SDL_strcmp(val, "1") == 0 ) {
  350. devclass |= SDL_UDEV_DEVICE_JOYSTICK;
  351. }
  352. val = _this->udev_device_get_property_value(dev, "ID_INPUT_MOUSE");
  353. if (val != NULL && SDL_strcmp(val, "1") == 0 ) {
  354. devclass |= SDL_UDEV_DEVICE_MOUSE;
  355. }
  356. val = _this->udev_device_get_property_value(dev, "ID_INPUT_TOUCHSCREEN");
  357. if (val != NULL && SDL_strcmp(val, "1") == 0 ) {
  358. devclass |= SDL_UDEV_DEVICE_TOUCHSCREEN;
  359. }
  360. /* The undocumented rule is:
  361. - All devices with keys get ID_INPUT_KEY
  362. - From this subset, if they have ESC, numbers, and Q to D, it also gets ID_INPUT_KEYBOARD
  363. Ref: http://cgit.freedesktop.org/systemd/systemd/tree/src/udev/udev-builtin-input_id.c#n183
  364. */
  365. val = _this->udev_device_get_property_value(dev, "ID_INPUT_KEY");
  366. if (val != NULL && SDL_strcmp(val, "1") == 0 ) {
  367. devclass |= SDL_UDEV_DEVICE_KEYBOARD;
  368. }
  369. if (devclass == 0) {
  370. /* Fall back to old style input classes */
  371. val = _this->udev_device_get_property_value(dev, "ID_CLASS");
  372. if (val != NULL) {
  373. if (SDL_strcmp(val, "joystick") == 0) {
  374. devclass = SDL_UDEV_DEVICE_JOYSTICK;
  375. } else if (SDL_strcmp(val, "mouse") == 0) {
  376. devclass = SDL_UDEV_DEVICE_MOUSE;
  377. } else if (SDL_strcmp(val, "kbd") == 0) {
  378. devclass = SDL_UDEV_DEVICE_KEYBOARD;
  379. } else {
  380. return;
  381. }
  382. } else {
  383. /* We could be linked with libudev on a system that doesn't have udev running */
  384. devclass = guess_device_class(dev);
  385. }
  386. }
  387. } else {
  388. return;
  389. }
  390. /* Process callbacks */
  391. for (item = _this->first; item != NULL; item = item->next) {
  392. item->callback(type, devclass, path);
  393. }
  394. }
  395. void
  396. SDL_UDEV_Poll(void)
  397. {
  398. struct udev_device *dev = NULL;
  399. const char *action = NULL;
  400. if (_this == NULL) {
  401. return;
  402. }
  403. while (SDL_UDEV_hotplug_update_available()) {
  404. dev = _this->udev_monitor_receive_device(_this->udev_mon);
  405. if (dev == NULL) {
  406. break;
  407. }
  408. action = _this->udev_device_get_action(dev);
  409. if (SDL_strcmp(action, "add") == 0) {
  410. /* Wait for the device to finish initialization */
  411. SDL_Delay(100);
  412. device_event(SDL_UDEV_DEVICEADDED, dev);
  413. } else if (SDL_strcmp(action, "remove") == 0) {
  414. device_event(SDL_UDEV_DEVICEREMOVED, dev);
  415. }
  416. _this->udev_device_unref(dev);
  417. }
  418. }
  419. int
  420. SDL_UDEV_AddCallback(SDL_UDEV_Callback cb)
  421. {
  422. SDL_UDEV_CallbackList *item;
  423. item = (SDL_UDEV_CallbackList *) SDL_calloc(1, sizeof (SDL_UDEV_CallbackList));
  424. if (item == NULL) {
  425. return SDL_OutOfMemory();
  426. }
  427. item->callback = cb;
  428. if (_this->last == NULL) {
  429. _this->first = _this->last = item;
  430. } else {
  431. _this->last->next = item;
  432. _this->last = item;
  433. }
  434. return 1;
  435. }
  436. void
  437. SDL_UDEV_DelCallback(SDL_UDEV_Callback cb)
  438. {
  439. SDL_UDEV_CallbackList *item;
  440. SDL_UDEV_CallbackList *prev = NULL;
  441. for (item = _this->first; item != NULL; item = item->next) {
  442. /* found it, remove it. */
  443. if (item->callback == cb) {
  444. if (prev != NULL) {
  445. prev->next = item->next;
  446. } else {
  447. SDL_assert(_this->first == item);
  448. _this->first = item->next;
  449. }
  450. if (item == _this->last) {
  451. _this->last = prev;
  452. }
  453. SDL_free(item);
  454. return;
  455. }
  456. prev = item;
  457. }
  458. }
  459. #endif /* SDL_USE_LIBUDEV */
  460. /* vi: set ts=4 sw=4 expandtab: */