sys_watch_input.c 13 KB

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