sys_watch_input.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. static void templ_func_free (struct instance *o);
  73. static struct device * find_device_by_devname (struct instance *o, const char *devname)
  74. {
  75. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  76. while (list_node) {
  77. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  78. if (!strcmp(device->devname, devname)) {
  79. return device;
  80. }
  81. list_node = LinkedList1Node_Next(list_node);
  82. }
  83. return NULL;
  84. }
  85. static struct device * find_device_by_devpath (struct instance *o, const char *devpath)
  86. {
  87. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
  88. while (list_node) {
  89. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  90. if (!strcmp(device->devpath, devpath)) {
  91. return device;
  92. }
  93. list_node = LinkedList1Node_Next(list_node);
  94. }
  95. return NULL;
  96. }
  97. static void free_device (struct instance *o, struct device *device, int have_removed_map)
  98. {
  99. // remove from devices list
  100. LinkedList1_Remove(&o->devices_list, &device->devices_list_node);
  101. // free removed map
  102. if (have_removed_map) {
  103. BStringMap_Free(&device->removed_map);
  104. }
  105. // free devpath
  106. free(device->devpath);
  107. // free devname
  108. free(device->devname);
  109. // free structure
  110. free(device);
  111. }
  112. static int make_event_map (struct instance *o, int added, const char *devname, const char *device_type, BStringMap *out_map)
  113. {
  114. // init map
  115. BStringMap map;
  116. BStringMap_Init(&map);
  117. // set type
  118. if (!BStringMap_Set(&map, "event_type", (added ? "added" : "removed"))) {
  119. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  120. goto fail1;
  121. }
  122. // set devname
  123. if (!BStringMap_Set(&map, "devname", devname)) {
  124. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  125. goto fail1;
  126. }
  127. // set device type
  128. if (!BStringMap_Set(&map, "device_type", device_type)) {
  129. ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
  130. goto fail1;
  131. }
  132. *out_map = map;
  133. return 1;
  134. fail1:
  135. BStringMap_Free(&map);
  136. return 0;
  137. }
  138. static int devname_is_type (const char *devname, const char *devname_type)
  139. {
  140. // skip digits at the end
  141. size_t i;
  142. for (i = strlen(devname); i > 0; i--) {
  143. if (!isdigit(devname[i - 1])) {
  144. break;
  145. }
  146. }
  147. // check if devname_type precedes skipped digits
  148. for (size_t j = strlen(devname_type); j > 0; j--) {
  149. if (!(i > 0 && devname[i - 1] == devname_type[j - 1])) {
  150. return 0;
  151. }
  152. i--;
  153. }
  154. // check if slash precedes devname_type
  155. if (!(i > 0 && devname[i - 1] == '/')) {
  156. return 0;
  157. }
  158. return 1;
  159. }
  160. static void queue_event (struct instance *o, BStringMap map)
  161. {
  162. // pass event to template
  163. int was_empty;
  164. event_template_queue(&o->templ, map, &was_empty);
  165. // if event queue was empty, stop receiving udev events
  166. if (was_empty) {
  167. NCDUdevClient_Pause(&o->client);
  168. }
  169. }
  170. static void add_device (struct instance *o, const char *devname, const char *devpath, const char *device_type)
  171. {
  172. ASSERT(!find_device_by_devname(o, devname))
  173. ASSERT(!find_device_by_devpath(o, devpath))
  174. // allocate structure
  175. struct device *device = malloc(sizeof(*device));
  176. if (!device) {
  177. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  178. goto fail0;
  179. }
  180. // init devname
  181. if (!(device->devname = strdup(devname))) {
  182. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  183. goto fail1;
  184. }
  185. // init devpath
  186. if (!(device->devpath = strdup(devpath))) {
  187. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  188. goto fail2;
  189. }
  190. // init removed map
  191. if (!make_event_map(o, 0, devname, device_type, &device->removed_map)) {
  192. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  193. goto fail3;
  194. }
  195. // init added map
  196. BStringMap added_map;
  197. if (!make_event_map(o, 1, devname, device_type, &added_map)) {
  198. ModuleLog(o->i, BLOG_ERROR, "make_event_map failed");
  199. goto fail4;
  200. }
  201. // insert to devices list
  202. LinkedList1_Append(&o->devices_list, &device->devices_list_node);
  203. // queue event
  204. queue_event(o, added_map);
  205. return;
  206. fail4:
  207. BStringMap_Free(&device->removed_map);
  208. fail3:
  209. free(device->devpath);
  210. fail2:
  211. free(device->devname);
  212. fail1:
  213. free(device);
  214. fail0:
  215. ModuleLog(o->i, BLOG_ERROR, "failed to add device %s", devname);
  216. }
  217. static void remove_device (struct instance *o, struct device *device)
  218. {
  219. queue_event(o, device->removed_map);
  220. free_device(o, device, 0);
  221. }
  222. static void next_event (struct instance *o)
  223. {
  224. ASSERT(event_template_is_enabled(&o->templ))
  225. // order template to finish the current event
  226. int is_empty;
  227. event_template_dequeue(&o->templ, &is_empty);
  228. // if template has no events, continue udev events
  229. if (is_empty) {
  230. NCDUdevClient_Continue(&o->client);
  231. }
  232. }
  233. static void client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
  234. {
  235. // lookup existing device with this devpath
  236. struct device *ex_device = find_device_by_devpath(o, devpath);
  237. // lookup cache entry
  238. const BStringMap *cache_map = NCDUdevManager_Query(o->i->params->umanager, devpath);
  239. if (!cache_map) {
  240. if (ex_device) {
  241. remove_device(o, ex_device);
  242. }
  243. goto out;
  244. }
  245. const char *subsystem = BStringMap_Get(cache_map, "SUBSYSTEM");
  246. const char *devname = BStringMap_Get(cache_map, "DEVNAME");
  247. if (!(subsystem && !strcmp(subsystem, "input") && devname && devname_is_type(devname, o->devnode_type))) {
  248. if (ex_device) {
  249. remove_device(o, ex_device);
  250. }
  251. goto out;
  252. }
  253. if (ex_device && strcmp(ex_device->devname, devname)) {
  254. remove_device(o, ex_device);
  255. ex_device = NULL;
  256. }
  257. if (!ex_device) {
  258. struct device *ex_devname_device = find_device_by_devname(o, devname);
  259. if (ex_devname_device) {
  260. remove_device(o, ex_devname_device);
  261. }
  262. // determine type
  263. const char *device_type = "unknown";
  264. if (BStringMap_Get(cache_map, "ID_INPUT_TABLET")) {
  265. device_type = "tablet";
  266. }
  267. else if (BStringMap_Get(cache_map, "ID_INPUT_JOYSTICK")) {
  268. device_type = "joystick";
  269. }
  270. else if (BStringMap_Get(cache_map, "ID_INPUT_TOUCHSCREEN")) {
  271. device_type = "touchscreen";
  272. }
  273. else if (BStringMap_Get(cache_map, "ID_INPUT_MOUSE")) {
  274. device_type = "mouse";
  275. }
  276. else if (BStringMap_Get(cache_map, "ID_INPUT_TOUCHPAD")) {
  277. device_type = "touchpad";
  278. }
  279. else if (BStringMap_Get(cache_map, "ID_INPUT_KEY")) {
  280. device_type = "key";
  281. }
  282. else if (BStringMap_Get(cache_map, "ID_INPUT_KEYBOARD")) {
  283. device_type = "keyboard";
  284. }
  285. add_device(o, devname, devpath, device_type);
  286. }
  287. out:
  288. free(devpath);
  289. if (have_map) {
  290. BStringMap_Free(&map);
  291. }
  292. }
  293. static void func_new (NCDModuleInst *i)
  294. {
  295. // allocate instance
  296. struct instance *o = malloc(sizeof(*o));
  297. if (!o) {
  298. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  299. goto fail0;
  300. }
  301. NCDModuleInst_Backend_SetUser(i, o);
  302. // init arguments
  303. o->i = i;
  304. // check arguments
  305. NCDValue *devnode_type_arg;
  306. if (!NCDValue_ListRead(o->i->args, 1, &devnode_type_arg)) {
  307. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  308. goto fail1;
  309. }
  310. if (!NCDValue_IsStringNoNulls(devnode_type_arg)) {
  311. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  312. goto fail1;
  313. }
  314. o->devnode_type = NCDValue_StringValue(devnode_type_arg);
  315. // init client
  316. NCDUdevClient_Init(&o->client, o->i->params->umanager, o, (NCDUdevClient_handler)client_handler);
  317. // init devices list
  318. LinkedList1_Init(&o->devices_list);
  319. event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
  320. return;
  321. fail1:
  322. free(o);
  323. fail0:
  324. NCDModuleInst_Backend_SetError(i);
  325. NCDModuleInst_Backend_Dead(i);
  326. }
  327. static void templ_func_free (struct instance *o)
  328. {
  329. NCDModuleInst *i = o->i;
  330. // free devices
  331. LinkedList1Node *list_node;
  332. while (list_node = LinkedList1_GetFirst(&o->devices_list)) {
  333. struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
  334. free_device(o, device, 1);
  335. }
  336. // free client
  337. NCDUdevClient_Free(&o->client);
  338. // free instance
  339. free(o);
  340. NCDModuleInst_Backend_Dead(i);
  341. }
  342. static void func_die (void *vo)
  343. {
  344. struct instance *o = vo;
  345. event_template_die(&o->templ);
  346. }
  347. static int func_getvar (void *vo, const char *name, NCDValue *out)
  348. {
  349. struct instance *o = vo;
  350. return event_template_getvar(&o->templ, name, out);
  351. }
  352. static void nextevent_func_new (NCDModuleInst *i)
  353. {
  354. // check arguments
  355. if (!NCDValue_ListRead(i->args, 0)) {
  356. ModuleLog(i, BLOG_ERROR, "wrong arity");
  357. goto fail0;
  358. }
  359. // get method object
  360. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  361. // make sure we are currently reporting an event
  362. if (!event_template_is_enabled(&mo->templ)) {
  363. ModuleLog(i, BLOG_ERROR, "not reporting an event");
  364. goto fail0;
  365. }
  366. // signal up.
  367. // Do it before finishing the event so our process does not advance any further if
  368. // we would be killed the event provider going down.
  369. NCDModuleInst_Backend_Up(i);
  370. // wait for next event
  371. next_event(mo);
  372. return;
  373. fail0:
  374. NCDModuleInst_Backend_SetError(i);
  375. NCDModuleInst_Backend_Dead(i);
  376. }
  377. static const struct NCDModule modules[] = {
  378. {
  379. .type = "sys.watch_input",
  380. .func_new = func_new,
  381. .func_die = func_die,
  382. .func_getvar = func_getvar
  383. }, {
  384. .type = "sys.watch_input::nextevent",
  385. .func_new = nextevent_func_new
  386. }, {
  387. .type = NULL
  388. }
  389. };
  390. const struct NCDModuleGroup ncdmodule_sys_watch_input = {
  391. .modules = modules
  392. };