sys_watch_input.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /**
  2. * @file sys_watch_input.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Input device watcher.
  32. *
  33. * Synopsis: sys.watch_input(string devnode_type)
  34. * Description: reports input device events. Transitions up when an event is detected, and
  35. * goes down waiting for the next event when sys.watch_input::nextevent() is called.
  36. * On startup, "added" events are reported for existing input devices.
  37. * Arguments:
  38. * string devnode_type - device node type, for example "event", "mouse" or "js".
  39. * Variables:
  40. * string event_type - what happened with the input device: "added" or "removed"
  41. * string devname - device node path
  42. * string device_type - input device type, "tablet", "joystick", "touchscreen", "mouse",
  43. * "touchpad", "key", "keyboard" or "unknown"
  44. *
  45. * Synopsis: sys.watch_input::nextevent()
  46. * Description: makes the watch_input module transition down in order to report the next event.
  47. */
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <ctype.h>
  51. #include <misc/debug.h>
  52. #include <misc/offset.h>
  53. #include <structure/LinkedList1.h>
  54. #include <udevmonitor/NCDUdevManager.h>
  55. #include <ncd/NCDModule.h>
  56. #include <ncd/modules/event_template.h>
  57. #include <generated/blog_channel_ncd_sys_watch_input.h>
  58. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  59. struct device {
  60. char *devname;
  61. char *devpath;
  62. BStringMap removed_map;
  63. LinkedList1Node devices_list_node;
  64. };
  65. struct instance {
  66. NCDModuleInst *i;
  67. char *devnode_type;
  68. NCDUdevClient client;
  69. LinkedList1 devices_list;
  70. event_template templ;
  71. };
  72. struct nextevent_instance {
  73. NCDModuleInst *i;
  74. };
  75. static void templ_func_free (struct instance *o);
  76. static struct device * find_device_by_devname (struct instance *o, const char *devname)
  77. {
  78. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  79. while (list_node) {
  80. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  81. if (!strcmp(device->devname, devname)) {
  82. return device;
  83. }
  84. list_node = LinkedList1Node_Next(list_node);
  85. }
  86. return NULL;
  87. }
  88. static struct device * find_device_by_devpath (struct instance *o, const char *devpath)
  89. {
  90. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  91. while (list_node) {
  92. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  93. if (!strcmp(device->devpath, devpath)) {
  94. return device;
  95. }
  96. list_node = LinkedList1Node_Next(list_node);
  97. }
  98. return NULL;
  99. }
  100. static void free_device (struct instance *o, struct device *device, int have_removed_map)
  101. {
  102. // remove from devices list
  103. LinkedList1_Remove(&o->devices_list, &device->devices_list_node);
  104. // free removed map
  105. if (have_removed_map) {
  106. BStringMap_Free(&device->removed_map);
  107. }
  108. // free devpath
  109. free(device->devpath);
  110. // free devname
  111. free(device->devname);
  112. // free structure
  113. free(device);
  114. }
  115. static int make_event_map (struct instance *o, int added, const char *devname, const char *device_type, BStringMap *out_map)
  116. {
  117. // init map
  118. BStringMap map;
  119. BStringMap_Init(&map);
  120. // set type
  121. if (!BStringMap_Set(&map, "event_type", (added ? "added" : "removed"))) {
  122. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  123. goto fail1;
  124. }
  125. // set devname
  126. if (!BStringMap_Set(&map, "devname", devname)) {
  127. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  128. goto fail1;
  129. }
  130. // set device type
  131. if (!BStringMap_Set(&map, "device_type", device_type)) {
  132. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  133. goto fail1;
  134. }
  135. *out_map = map;
  136. return 1;
  137. fail1:
  138. BStringMap_Free(&map);
  139. return 0;
  140. }
  141. static int devname_is_type (const char *devname, const char *devname_type)
  142. {
  143. // skip digits at the end
  144. size_t i;
  145. for (i = strlen(devname); i > 0; i--) {
  146. if (!isdigit(devname[i - 1])) {
  147. break;
  148. }
  149. }
  150. // check if devname_type precedes skipped digits
  151. for (size_t j = strlen(devname_type); j > 0; j--) {
  152. if (!(i > 0 && devname[i - 1] == devname_type[j - 1])) {
  153. return 0;
  154. }
  155. i--;
  156. }
  157. // check if slash precedes devname_type
  158. if (!(i > 0 && devname[i - 1] == '/')) {
  159. return 0;
  160. }
  161. return 1;
  162. }
  163. static void queue_event (struct instance *o, BStringMap map)
  164. {
  165. // pass event to template
  166. int was_empty;
  167. event_template_queue(&o->templ, map, &was_empty);
  168. // if event queue was empty, stop receiving udev events
  169. if (was_empty) {
  170. NCDUdevClient_Pause(&o->client);
  171. }
  172. }
  173. static void add_device (struct instance *o, const char *devname, const char *devpath, const char *device_type)
  174. {
  175. ASSERT(!find_device_by_devname(o, devname))
  176. ASSERT(!find_device_by_devpath(o, devpath))
  177. // allocate structure
  178. struct device *device = malloc(sizeof(*device));
  179. if (!device) {
  180. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  181. goto fail0;
  182. }
  183. // init devname
  184. if (!(device->devname = strdup(devname))) {
  185. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  186. goto fail1;
  187. }
  188. // init devpath
  189. if (!(device->devpath = strdup(devpath))) {
  190. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  191. goto fail2;
  192. }
  193. // init removed map
  194. if (!make_event_map(o, 0, devname, device_type, &device->removed_map)) {
  195. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  196. goto fail3;
  197. }
  198. // init added map
  199. BStringMap added_map;
  200. if (!make_event_map(o, 1, devname, device_type, &added_map)) {
  201. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  202. goto fail4;
  203. }
  204. // insert to devices list
  205. LinkedList1_Append(&o->devices_list, &device->devices_list_node);
  206. // queue event
  207. queue_event(o, added_map);
  208. return;
  209. fail4:
  210. BStringMap_Free(&device->removed_map);
  211. fail3:
  212. free(device->devpath);
  213. fail2:
  214. free(device->devname);
  215. fail1:
  216. free(device);
  217. fail0:
  218. ModuleLog(o->i, BLOG_ERROR, "failed to add device %s", devname);
  219. }
  220. static void remove_device (struct instance *o, struct device *device)
  221. {
  222. queue_event(o, device->removed_map);
  223. free_device(o, device, 0);
  224. }
  225. static void next_event (struct instance *o)
  226. {
  227. ASSERT(event_template_is_enabled(&o->templ))
  228. // order template to finish the current event
  229. int is_empty;
  230. event_template_dequeue(&o->templ, &is_empty);
  231. // if template has no events, continue udev events
  232. if (is_empty) {
  233. NCDUdevClient_Continue(&o->client);
  234. }
  235. }
  236. static void client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
  237. {
  238. // lookup existing device with this devpath
  239. struct device *ex_device = find_device_by_devpath(o, devpath);
  240. // lookup cache entry
  241. const BStringMap *cache_map = NCDUdevManager_Query(o->i->params->umanager, devpath);
  242. if (!cache_map) {
  243. if (ex_device) {
  244. remove_device(o, ex_device);
  245. }
  246. goto out;
  247. }
  248. const char *subsystem = BStringMap_Get(cache_map, "SUBSYSTEM");
  249. const char *devname = BStringMap_Get(cache_map, "DEVNAME");
  250. if (!(subsystem && !strcmp(subsystem, "input") && devname && devname_is_type(devname, o->devnode_type))) {
  251. if (ex_device) {
  252. remove_device(o, ex_device);
  253. }
  254. goto out;
  255. }
  256. if (ex_device && strcmp(ex_device->devname, devname)) {
  257. remove_device(o, ex_device);
  258. ex_device = NULL;
  259. }
  260. if (!ex_device) {
  261. struct device *ex_devname_device = find_device_by_devname(o, devname);
  262. if (ex_devname_device) {
  263. remove_device(o, ex_devname_device);
  264. }
  265. // determine type
  266. const char *device_type = "unknown";
  267. if (BStringMap_Get(cache_map, "ID_INPUT_TABLET")) {
  268. device_type = "tablet";
  269. }
  270. else if (BStringMap_Get(cache_map, "ID_INPUT_JOYSTICK")) {
  271. device_type = "joystick";
  272. }
  273. else if (BStringMap_Get(cache_map, "ID_INPUT_TOUCHSCREEN")) {
  274. device_type = "touchscreen";
  275. }
  276. else if (BStringMap_Get(cache_map, "ID_INPUT_MOUSE")) {
  277. device_type = "mouse";
  278. }
  279. else if (BStringMap_Get(cache_map, "ID_INPUT_TOUCHPAD")) {
  280. device_type = "touchpad";
  281. }
  282. else if (BStringMap_Get(cache_map, "ID_INPUT_KEY")) {
  283. device_type = "key";
  284. }
  285. else if (BStringMap_Get(cache_map, "ID_INPUT_KEYBOARD")) {
  286. device_type = "keyboard";
  287. }
  288. add_device(o, devname, devpath, device_type);
  289. }
  290. out:
  291. free(devpath);
  292. if (have_map) {
  293. BStringMap_Free(&map);
  294. }
  295. }
  296. static void func_new (NCDModuleInst *i)
  297. {
  298. // allocate instance
  299. struct instance *o = malloc(sizeof(*o));
  300. if (!o) {
  301. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  302. goto fail0;
  303. }
  304. NCDModuleInst_Backend_SetUser(i, o);
  305. // init arguments
  306. o->i = i;
  307. // check arguments
  308. NCDValue *devnode_type_arg;
  309. if (!NCDValue_ListRead(o->i->args, 1, &devnode_type_arg)) {
  310. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  311. goto fail1;
  312. }
  313. if (!NCDValue_IsStringNoNulls(devnode_type_arg)) {
  314. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  315. goto fail1;
  316. }
  317. o->devnode_type = NCDValue_StringValue(devnode_type_arg);
  318. // init client
  319. NCDUdevClient_Init(&o->client, o->i->params->umanager, o, (NCDUdevClient_handler)client_handler);
  320. // init devices list
  321. LinkedList1_Init(&o->devices_list);
  322. event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
  323. return;
  324. fail1:
  325. free(o);
  326. fail0:
  327. NCDModuleInst_Backend_SetError(i);
  328. NCDModuleInst_Backend_Dead(i);
  329. }
  330. static void templ_func_free (struct instance *o)
  331. {
  332. NCDModuleInst *i = o->i;
  333. // free devices
  334. LinkedList1Node *list_node;
  335. while (list_node = LinkedList1_GetFirst(&o->devices_list)) {
  336. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  337. free_device(o, device, 1);
  338. }
  339. // free client
  340. NCDUdevClient_Free(&o->client);
  341. // free instance
  342. free(o);
  343. NCDModuleInst_Backend_Dead(i);
  344. }
  345. static void func_die (void *vo)
  346. {
  347. struct instance *o = vo;
  348. event_template_die(&o->templ);
  349. }
  350. static int func_getvar (void *vo, const char *name, NCDValue *out)
  351. {
  352. struct instance *o = vo;
  353. return event_template_getvar(&o->templ, name, out);
  354. }
  355. static void nextevent_func_new (NCDModuleInst *i)
  356. {
  357. // allocate instance
  358. struct nextevent_instance *o = malloc(sizeof(*o));
  359. if (!o) {
  360. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  361. goto fail0;
  362. }
  363. NCDModuleInst_Backend_SetUser(i, o);
  364. // init arguments
  365. o->i = i;
  366. // check arguments
  367. if (!NCDValue_ListRead(o->i->args, 0)) {
  368. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  369. goto fail1;
  370. }
  371. // get method object
  372. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  373. // make sure we are currently reporting an event
  374. if (!event_template_is_enabled(&mo->templ)) {
  375. ModuleLog(o->i, BLOG_ERROR, "not reporting an event");
  376. goto fail1;
  377. }
  378. // signal up.
  379. // Do it before finishing the event so our process does not advance any further if
  380. // we would be killed the event provider going down.
  381. NCDModuleInst_Backend_Up(o->i);
  382. // wait for next event
  383. next_event(mo);
  384. return;
  385. fail1:
  386. free(o);
  387. fail0:
  388. NCDModuleInst_Backend_SetError(i);
  389. NCDModuleInst_Backend_Dead(i);
  390. }
  391. static void nextevent_func_die (void *vo)
  392. {
  393. struct nextevent_instance *o = vo;
  394. NCDModuleInst *i = o->i;
  395. // free instance
  396. free(o);
  397. NCDModuleInst_Backend_Dead(i);
  398. }
  399. static const struct NCDModule modules[] = {
  400. {
  401. .type = "sys.watch_input",
  402. .func_new = func_new,
  403. .func_die = func_die,
  404. .func_getvar = func_getvar
  405. }, {
  406. .type = "sys.watch_input::nextevent",
  407. .func_new = nextevent_func_new,
  408. .func_die = nextevent_func_die
  409. }, {
  410. .type = NULL
  411. }
  412. };
  413. const struct NCDModuleGroup ncdmodule_sys_watch_input = {
  414. .modules = modules
  415. };