v4l2-udev.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <libudev.h>
  15. #include <util/threading.h>
  16. #include <util/darray.h>
  17. #include <obs.h>
  18. #include "v4l2-udev.h"
  19. #define UDEV_DATA(voidptr) struct v4l2_udev_mon_t *m \
  20. = (struct v4l2_udev_mon_t *) voidptr;
  21. /** udev action enum */
  22. enum udev_action {
  23. UDEV_ACTION_ADDED,
  24. UDEV_ACTION_REMOVED,
  25. UDEV_ACTION_UNKNOWN
  26. };
  27. /** monitor object holding the callbacks */
  28. struct v4l2_udev_mon_t {
  29. /* data for the device added callback */
  30. void *dev_added_userdata;
  31. v4l2_device_added_cb dev_added_cb;
  32. /* data for the device removed callback */
  33. void *dev_removed_userdata;
  34. v4l2_device_removed_cb dev_removed_cb;
  35. };
  36. /* global data */
  37. static uint_fast32_t udev_refs = 0;
  38. static pthread_mutex_t udev_mutex = PTHREAD_MUTEX_INITIALIZER;
  39. static pthread_t udev_thread;
  40. static os_event_t *udev_event;
  41. static DARRAY(struct v4l2_udev_mon_t) udev_clients;
  42. /**
  43. * udev gives us the device action as string, so we convert it here ...
  44. *
  45. * @param action the udev action as string
  46. * @return the udev action as enum value
  47. */
  48. static enum udev_action udev_action_to_enum(const char *action)
  49. {
  50. if (!action)
  51. return UDEV_ACTION_UNKNOWN;
  52. if (!strncmp("add", action, 3))
  53. return UDEV_ACTION_ADDED;
  54. if (!strncmp("remove", action, 6))
  55. return UDEV_ACTION_REMOVED;
  56. return UDEV_ACTION_UNKNOWN;
  57. }
  58. /**
  59. * Call all registered callbacks with the event
  60. *
  61. * @param dev udev device that had an event occuring
  62. */
  63. static inline void udev_call_callbacks(struct udev_device *dev)
  64. {
  65. const char *node;
  66. enum udev_action action;
  67. pthread_mutex_lock(&udev_mutex);
  68. node = udev_device_get_devnode(dev);
  69. action = udev_action_to_enum(udev_device_get_action(dev));
  70. for (size_t idx = 0; idx < udev_clients.num; idx++) {
  71. struct v4l2_udev_mon_t *c = &udev_clients.array[idx];
  72. switch (action) {
  73. case UDEV_ACTION_ADDED:
  74. if (!c->dev_added_cb)
  75. continue;
  76. c->dev_added_cb(node, c->dev_added_userdata);
  77. break;
  78. case UDEV_ACTION_REMOVED:
  79. if (!c->dev_removed_cb)
  80. continue;
  81. c->dev_removed_cb(node, c->dev_removed_userdata);
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. pthread_mutex_unlock(&udev_mutex);
  88. }
  89. /**
  90. * Event listener thread
  91. */
  92. static void *udev_event_thread(void *vptr)
  93. {
  94. UNUSED_PARAMETER(vptr);
  95. int fd;
  96. fd_set fds;
  97. struct timeval tv;
  98. struct udev *udev;
  99. struct udev_monitor *mon;
  100. struct udev_device *dev;
  101. /* set up udev monitoring */
  102. udev = udev_new();
  103. mon = udev_monitor_new_from_netlink(udev, "udev");
  104. udev_monitor_filter_add_match_subsystem_devtype(
  105. mon, "video4linux", NULL);
  106. if (udev_monitor_enable_receiving(mon) < 0)
  107. return NULL;
  108. /* set up fds */
  109. fd = udev_monitor_get_fd(mon);
  110. while (os_event_try(udev_event) == EAGAIN) {
  111. FD_ZERO(&fds);
  112. FD_SET(fd, &fds);
  113. tv.tv_sec = 1;
  114. tv.tv_usec = 0;
  115. if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0)
  116. continue;
  117. dev = udev_monitor_receive_device(mon);
  118. if (!dev)
  119. continue;
  120. udev_call_callbacks(dev);
  121. udev_device_unref(dev);
  122. }
  123. udev_monitor_unref(mon);
  124. udev_unref(udev);
  125. return NULL;
  126. }
  127. void *v4l2_init_udev(void)
  128. {
  129. struct v4l2_udev_mon_t *ret = NULL;
  130. pthread_mutex_lock(&udev_mutex);
  131. /* set up udev */
  132. if (udev_refs == 0) {
  133. if (os_event_init(&udev_event, OS_EVENT_TYPE_MANUAL) != 0)
  134. goto fail;
  135. if (pthread_create(&udev_thread, NULL, udev_event_thread,
  136. NULL) != 0)
  137. goto fail;
  138. da_init(udev_clients);
  139. }
  140. udev_refs++;
  141. /* create monitor object */
  142. ret = da_push_back_new(udev_clients);
  143. fail:
  144. pthread_mutex_unlock(&udev_mutex);
  145. return ret;
  146. }
  147. void v4l2_unref_udev(void *monitor)
  148. {
  149. UDEV_DATA(monitor);
  150. pthread_mutex_lock(&udev_mutex);
  151. /* clean up monitor object */
  152. da_erase_item(udev_clients, m);
  153. /* unref udev monitor */
  154. udev_refs--;
  155. if (udev_refs == 0) {
  156. os_event_signal(udev_event);
  157. pthread_join(udev_thread, NULL);
  158. os_event_destroy(udev_event);
  159. da_free(udev_clients);
  160. }
  161. pthread_mutex_unlock(&udev_mutex);
  162. }
  163. void v4l2_set_device_added_callback(void *monitor, v4l2_device_added_cb cb,
  164. void *userdata)
  165. {
  166. UDEV_DATA(monitor);
  167. if (!m)
  168. return;
  169. m->dev_added_cb = cb;
  170. m->dev_added_userdata = userdata;
  171. }
  172. void v4l2_set_device_removed_callback(void *monitor, v4l2_device_removed_cb cb,
  173. void *userdata)
  174. {
  175. UDEV_DATA(monitor);
  176. if (!m)
  177. return;
  178. m->dev_removed_cb = cb;
  179. m->dev_removed_userdata = userdata;
  180. }