sys_watch_input.c 13 KB

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