sys_watch_input.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /**
  2. * @file sys_watch_input.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Input device watcher.
  25. *
  26. * Synopsis: sys.watch_input(string devnode_type)
  27. * Description: reports input device events. Transitions up when an event is detected, and
  28. * goes down waiting for the next event when sys.watch_input::nextevent() is called.
  29. * On startup, "added" events are reported for existing input devices.
  30. * Arguments:
  31. * string devnode_type - device node type, for example "event", "mouse" or "js".
  32. * Variables:
  33. * string event_type - what happened with the input device: "added" or "removed"
  34. * string devname - device node path
  35. * string device_type - input device type, "tablet", "joystick", "touchscreen", "mouse",
  36. * "touchpad", "key", "keyboard" or "unknown"
  37. *
  38. * Synopsis: sys.watch_input::nextevent()
  39. * Description: makes the watch_input module transition down in order to report the next event.
  40. */
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <ctype.h>
  44. #include <misc/debug.h>
  45. #include <misc/offset.h>
  46. #include <structure/LinkedList1.h>
  47. #include <udevmonitor/NCDUdevManager.h>
  48. #include <ncd/NCDModule.h>
  49. #include <ncd/modules/event_template.h>
  50. #include <generated/blog_channel_ncd_sys_watch_input.h>
  51. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  52. struct device {
  53. char *devname;
  54. char *devpath;
  55. BStringMap removed_map;
  56. LinkedList1Node devices_list_node;
  57. };
  58. struct instance {
  59. NCDModuleInst *i;
  60. char *devnode_type;
  61. NCDUdevClient client;
  62. LinkedList1 devices_list;
  63. event_template templ;
  64. };
  65. struct nextevent_instance {
  66. NCDModuleInst *i;
  67. };
  68. static void templ_func_free (struct instance *o);
  69. struct device * find_device_by_devname (struct instance *o, const char *devname)
  70. {
  71. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  72. while (list_node) {
  73. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  74. if (!strcmp(device->devname, devname)) {
  75. return device;
  76. }
  77. list_node = LinkedList1Node_Next(list_node);
  78. }
  79. return NULL;
  80. }
  81. struct device * find_device_by_devpath (struct instance *o, const char *devpath)
  82. {
  83. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  84. while (list_node) {
  85. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  86. if (!strcmp(device->devpath, devpath)) {
  87. return device;
  88. }
  89. list_node = LinkedList1Node_Next(list_node);
  90. }
  91. return NULL;
  92. }
  93. static void free_device (struct instance *o, struct device *device, int have_removed_map)
  94. {
  95. // remove from devices list
  96. LinkedList1_Remove(&o->devices_list, &device->devices_list_node);
  97. // free removed map
  98. if (have_removed_map) {
  99. BStringMap_Free(&device->removed_map);
  100. }
  101. // free devpath
  102. free(device->devpath);
  103. // free devname
  104. free(device->devname);
  105. // free structure
  106. free(device);
  107. }
  108. static int make_event_map (struct instance *o, int added, const char *devname, const char *device_type, BStringMap *out_map)
  109. {
  110. // init map
  111. BStringMap map;
  112. BStringMap_Init(&map);
  113. // set type
  114. if (!BStringMap_Set(&map, "event_type", (added ? "added" : "removed"))) {
  115. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  116. goto fail1;
  117. }
  118. // set devname
  119. if (!BStringMap_Set(&map, "devname", devname)) {
  120. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  121. goto fail1;
  122. }
  123. // set device type
  124. if (!BStringMap_Set(&map, "device_type", device_type)) {
  125. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  126. goto fail1;
  127. }
  128. *out_map = map;
  129. return 1;
  130. fail1:
  131. BStringMap_Free(&map);
  132. return 0;
  133. }
  134. static int devname_is_type (const char *devname, const char *devname_type)
  135. {
  136. // skip digits at the end
  137. size_t i;
  138. for (i = strlen(devname); i > 0; i--) {
  139. if (!isdigit(devname[i - 1])) {
  140. break;
  141. }
  142. }
  143. // check if devname_type precedes skipped digits
  144. for (size_t j = strlen(devname_type); j > 0; j--) {
  145. if (!(i > 0 && devname[i - 1] == devname_type[j - 1])) {
  146. return 0;
  147. }
  148. i--;
  149. }
  150. // check if slash precedes devname_type
  151. if (!(i > 0 && devname[i - 1] == '/')) {
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. static void queue_event (struct instance *o, BStringMap map)
  157. {
  158. // pass event to template
  159. int was_empty;
  160. event_template_queue(&o->templ, map, &was_empty);
  161. // if event queue was empty, stop receiving udev events
  162. if (was_empty) {
  163. NCDUdevClient_Pause(&o->client);
  164. }
  165. }
  166. static void add_device (struct instance *o, const char *devname, const char *devpath, const char *device_type)
  167. {
  168. ASSERT(!find_device_by_devname(o, devname))
  169. ASSERT(!find_device_by_devpath(o, devpath))
  170. // allocate structure
  171. struct device *device = malloc(sizeof(*device));
  172. if (!device) {
  173. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  174. goto fail0;
  175. }
  176. // init devname
  177. if (!(device->devname = strdup(devname))) {
  178. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  179. goto fail1;
  180. }
  181. // init devpath
  182. if (!(device->devpath = strdup(devpath))) {
  183. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  184. goto fail2;
  185. }
  186. // init removed map
  187. if (!make_event_map(o, 0, devname, device_type, &device->removed_map)) {
  188. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  189. goto fail3;
  190. }
  191. // init added map
  192. BStringMap added_map;
  193. if (!make_event_map(o, 1, devname, device_type, &added_map)) {
  194. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  195. goto fail4;
  196. }
  197. // insert to devices list
  198. LinkedList1_Append(&o->devices_list, &device->devices_list_node);
  199. // queue event
  200. queue_event(o, added_map);
  201. return;
  202. fail4:
  203. BStringMap_Free(&device->removed_map);
  204. fail3:
  205. free(device->devpath);
  206. fail2:
  207. free(device->devname);
  208. fail1:
  209. free(device);
  210. fail0:
  211. ModuleLog(o->i, BLOG_ERROR, "failed to add device %s", devname);
  212. }
  213. static void remove_device (struct instance *o, struct device *device)
  214. {
  215. queue_event(o, device->removed_map);
  216. free_device(o, device, 0);
  217. }
  218. static void next_event (struct instance *o)
  219. {
  220. event_template_assert_enabled(&o->templ);
  221. // order template to finish the current event
  222. int is_empty;
  223. event_template_dequeue(&o->templ, &is_empty);
  224. // if template has no events, continue udev events
  225. if (is_empty) {
  226. NCDUdevClient_Continue(&o->client);
  227. }
  228. }
  229. static void client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
  230. {
  231. // lookup existing device with this devpath
  232. struct device *ex_device = find_device_by_devpath(o, devpath);
  233. // lookup cache entry
  234. const BStringMap *cache_map = NCDUdevManager_Query(o->i->umanager, devpath);
  235. if (!cache_map) {
  236. if (ex_device) {
  237. remove_device(o, ex_device);
  238. }
  239. goto out;
  240. }
  241. const char *subsystem = BStringMap_Get(cache_map, "SUBSYSTEM");
  242. const char *devname = BStringMap_Get(cache_map, "DEVNAME");
  243. if (!(subsystem && !strcmp(subsystem, "input") && devname && devname_is_type(devname, o->devnode_type))) {
  244. if (ex_device) {
  245. remove_device(o, ex_device);
  246. }
  247. goto out;
  248. }
  249. if (ex_device && strcmp(ex_device->devname, devname)) {
  250. remove_device(o, ex_device);
  251. ex_device = NULL;
  252. }
  253. if (!ex_device) {
  254. struct device *ex_devname_device = find_device_by_devname(o, devname);
  255. if (ex_devname_device) {
  256. remove_device(o, ex_devname_device);
  257. }
  258. // determine type
  259. const char *device_type = "unknown";
  260. if (BStringMap_Get(cache_map, "ID_INPUT_TABLET")) {
  261. device_type = "tablet";
  262. }
  263. else if (BStringMap_Get(cache_map, "ID_INPUT_JOYSTICK")) {
  264. device_type = "joystick";
  265. }
  266. else if (BStringMap_Get(cache_map, "ID_INPUT_TOUCHSCREEN")) {
  267. device_type = "touchscreen";
  268. }
  269. else if (BStringMap_Get(cache_map, "ID_INPUT_MOUSE")) {
  270. device_type = "mouse";
  271. }
  272. else if (BStringMap_Get(cache_map, "ID_INPUT_TOUCHPAD")) {
  273. device_type = "touchpad";
  274. }
  275. else if (BStringMap_Get(cache_map, "ID_INPUT_KEY")) {
  276. device_type = "key";
  277. }
  278. else if (BStringMap_Get(cache_map, "ID_INPUT_KEYBOARD")) {
  279. device_type = "keyboard";
  280. }
  281. add_device(o, devname, devpath, device_type);
  282. }
  283. out:
  284. free(devpath);
  285. if (have_map) {
  286. BStringMap_Free(&map);
  287. }
  288. }
  289. static void func_new (NCDModuleInst *i)
  290. {
  291. // allocate instance
  292. struct instance *o = malloc(sizeof(*o));
  293. if (!o) {
  294. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  295. goto fail0;
  296. }
  297. NCDModuleInst_Backend_SetUser(i, o);
  298. // init arguments
  299. o->i = i;
  300. // check arguments
  301. NCDValue *devnode_type_arg;
  302. if (!NCDValue_ListRead(o->i->args, 1, &devnode_type_arg)) {
  303. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  304. goto fail1;
  305. }
  306. if (NCDValue_Type(devnode_type_arg) != NCDVALUE_STRING) {
  307. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  308. goto fail1;
  309. }
  310. o->devnode_type = NCDValue_StringValue(devnode_type_arg);
  311. // init client
  312. NCDUdevClient_Init(&o->client, o->i->umanager, o, (NCDUdevClient_handler)client_handler);
  313. // init devices list
  314. LinkedList1_Init(&o->devices_list);
  315. event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
  316. return;
  317. fail1:
  318. free(o);
  319. fail0:
  320. NCDModuleInst_Backend_SetError(i);
  321. NCDModuleInst_Backend_Dead(i);
  322. }
  323. static void templ_func_free (struct instance *o)
  324. {
  325. NCDModuleInst *i = o->i;
  326. // free devices
  327. LinkedList1Node *list_node;
  328. while (list_node = LinkedList1_GetFirst(&o->devices_list)) {
  329. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  330. free_device(o, device, 1);
  331. }
  332. // free client
  333. NCDUdevClient_Free(&o->client);
  334. // free instance
  335. free(o);
  336. NCDModuleInst_Backend_Dead(i);
  337. }
  338. static void func_die (void *vo)
  339. {
  340. struct instance *o = vo;
  341. event_template_die(&o->templ);
  342. }
  343. static int func_getvar (void *vo, const char *name, NCDValue *out)
  344. {
  345. struct instance *o = vo;
  346. return event_template_getvar(&o->templ, name, out);
  347. }
  348. static void nextevent_func_new (NCDModuleInst *i)
  349. {
  350. // allocate instance
  351. struct nextevent_instance *o = malloc(sizeof(*o));
  352. if (!o) {
  353. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  354. goto fail0;
  355. }
  356. NCDModuleInst_Backend_SetUser(i, o);
  357. // init arguments
  358. o->i = i;
  359. // check arguments
  360. if (!NCDValue_ListRead(o->i->args, 0)) {
  361. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  362. goto fail1;
  363. }
  364. // get method object
  365. struct instance *mo = i->method_object->inst_user;
  366. event_template_assert_enabled(&mo->templ);
  367. // signal up.
  368. // Do it before finishing the event so our process does not advance any further if
  369. // we would be killed the event provider going down.
  370. NCDModuleInst_Backend_Up(o->i);
  371. // wait for next event
  372. next_event(mo);
  373. return;
  374. fail1:
  375. free(o);
  376. fail0:
  377. NCDModuleInst_Backend_SetError(i);
  378. NCDModuleInst_Backend_Dead(i);
  379. }
  380. static void nextevent_func_die (void *vo)
  381. {
  382. struct nextevent_instance *o = vo;
  383. NCDModuleInst *i = o->i;
  384. // free instance
  385. free(o);
  386. NCDModuleInst_Backend_Dead(i);
  387. }
  388. static const struct NCDModule modules[] = {
  389. {
  390. .type = "sys.watch_input",
  391. .func_new = func_new,
  392. .func_die = func_die,
  393. .func_getvar = func_getvar
  394. }, {
  395. .type = "sys.watch_input::nextevent",
  396. .func_new = nextevent_func_new,
  397. .func_die = nextevent_func_die
  398. }, {
  399. .type = NULL
  400. }
  401. };
  402. const struct NCDModuleGroup ncdmodule_sys_watch_input = {
  403. .modules = modules
  404. };