SDL_udev.c 16 KB

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