sys_evdev.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * @file sys_evdev.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. * Linux event device module.
  25. *
  26. * Synopsis: sys.evdev(string device)
  27. * Description: reports input events from a Linux event device. Transitions up when an event is
  28. * detected, and goes down waiting for the next event when sys.evdev::nextevent() is called.
  29. * Variables:
  30. * string type - symbolic event type (e.g. EV_KEY, EV_REL, EV_ABS), corresponding to
  31. * (struct input_event).type, or "unknown"
  32. * string value - event value (signed integer), equal to (struct input_event).value
  33. * string code_numeric - numeric event code (unsigned integer), equal to
  34. * (struct input_event).code
  35. * string code - symbolic event code (e.g. KEY_ESC. KEY_1, KEY_2, BTN_LEFT), corrresponding
  36. * to (struct input_event).code, or "unknown"
  37. *
  38. * Synopsis: sys.evdev::nextevent()
  39. * Description: makes the evdev module transition down in order to report the next event.
  40. */
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <stdint.h>
  44. #include <inttypes.h>
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include <fcntl.h>
  48. #include <unistd.h>
  49. #include <linux/input.h>
  50. #include <misc/nonblocking.h>
  51. #include <ncd/NCDModule.h>
  52. #include <generated/blog_channel_ncd_sys_evdev.h>
  53. #include "linux_input_names.h"
  54. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  55. struct instance {
  56. NCDModuleInst *i;
  57. int evdev_fd;
  58. BFileDescriptor bfd;
  59. int processing;
  60. struct input_event event;
  61. };
  62. struct nextevent_instance {
  63. NCDModuleInst *i;
  64. };
  65. static void instance_free (struct instance *o, int is_error);
  66. #define MAKE_LOOKUP_FUNC(_name_) \
  67. static const char * evdev_##_name_##_to_str (uint16_t type) \
  68. { \
  69. if (type >= (sizeof(_name_##_names) / sizeof(_name_##_names[0])) || !_name_##_names[type]) { \
  70. return "unknown"; \
  71. } \
  72. return _name_##_names[type]; \
  73. }
  74. MAKE_LOOKUP_FUNC(type)
  75. MAKE_LOOKUP_FUNC(key)
  76. MAKE_LOOKUP_FUNC(rel)
  77. MAKE_LOOKUP_FUNC(abs)
  78. MAKE_LOOKUP_FUNC(sw)
  79. MAKE_LOOKUP_FUNC(msc)
  80. MAKE_LOOKUP_FUNC(led)
  81. MAKE_LOOKUP_FUNC(rep)
  82. MAKE_LOOKUP_FUNC(snd)
  83. MAKE_LOOKUP_FUNC(ffstatus)
  84. static void device_handler (struct instance *o, int events)
  85. {
  86. ASSERT(!o->processing)
  87. int res = read(o->evdev_fd, &o->event, sizeof(o->event));
  88. if (res < 0) {
  89. ModuleLog(o->i, BLOG_ERROR, "read failed");
  90. instance_free(o, 1);
  91. return;
  92. }
  93. if (res != sizeof(o->event)) {
  94. ModuleLog(o->i, BLOG_ERROR, "read wrong");
  95. instance_free(o, 1);
  96. return;
  97. }
  98. // stop reading
  99. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, 0);
  100. // set processing
  101. o->processing = 1;
  102. // signal up
  103. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  104. }
  105. static void device_nextevent (struct instance *o)
  106. {
  107. ASSERT(o->processing)
  108. // start reading
  109. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, BREACTOR_READ);
  110. // set not processing
  111. o->processing = 0;
  112. // signal down
  113. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  114. }
  115. static void func_new (NCDModuleInst *i)
  116. {
  117. // allocate instance
  118. struct instance *o = malloc(sizeof(*o));
  119. if (!o) {
  120. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  121. goto fail0;
  122. }
  123. NCDModuleInst_Backend_SetUser(i, o);
  124. // init arguments
  125. o->i = i;
  126. // check arguments
  127. NCDValue *device_arg;
  128. if (!NCDValue_ListRead(o->i->args, 1, &device_arg)) {
  129. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  130. goto fail1;
  131. }
  132. if (NCDValue_Type(device_arg) != NCDVALUE_STRING) {
  133. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  134. goto fail1;
  135. }
  136. // open device
  137. if ((o->evdev_fd = open(NCDValue_StringValue(device_arg), O_RDONLY)) < 0) {
  138. ModuleLog(o->i, BLOG_ERROR, "open failed");
  139. goto fail1;
  140. }
  141. // set non-blocking
  142. if (!badvpn_set_nonblocking(o->evdev_fd)) {
  143. ModuleLog(o->i, BLOG_ERROR, "badvpn_set_nonblocking failed");
  144. goto fail2;
  145. }
  146. // init BFileDescriptor
  147. BFileDescriptor_Init(&o->bfd, o->evdev_fd, (BFileDescriptor_handler)device_handler, o);
  148. if (!BReactor_AddFileDescriptor(o->i->reactor, &o->bfd)) {
  149. ModuleLog(o->i, BLOG_ERROR, "BReactor_AddFileDescriptor failed");
  150. goto fail2;
  151. }
  152. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, BREACTOR_READ);
  153. // set not processing
  154. o->processing = 0;
  155. return;
  156. fail2:
  157. ASSERT_FORCE(close(o->evdev_fd) == 0)
  158. fail1:
  159. free(o);
  160. fail0:
  161. NCDModuleInst_Backend_SetError(i);
  162. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  163. }
  164. void instance_free (struct instance *o, int is_error)
  165. {
  166. NCDModuleInst *i = o->i;
  167. // free BFileDescriptor
  168. BReactor_RemoveFileDescriptor(o->i->reactor, &o->bfd);
  169. // close device.
  170. // Ignore close error which happens if the device is removed.
  171. if (close(o->evdev_fd) < 0) {
  172. ModuleLog(o->i, BLOG_ERROR, "close failed");
  173. }
  174. // free instance
  175. free(o);
  176. if (is_error) {
  177. NCDModuleInst_Backend_SetError(i);
  178. }
  179. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  180. }
  181. static void func_die (void *vo)
  182. {
  183. struct instance *o = vo;
  184. instance_free(o, 0);
  185. }
  186. static int func_getvar (void *vo, const char *name, NCDValue *out)
  187. {
  188. struct instance *o = vo;
  189. ASSERT(o->processing)
  190. if (!strcmp(name, "type")) {
  191. if (!NCDValue_InitString(out, evdev_type_to_str(o->event.type))) {
  192. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  193. return 0;
  194. }
  195. return 1;
  196. }
  197. if (!strcmp(name, "value")) {
  198. char str[50];
  199. snprintf(str, sizeof(str), "%"PRIi32, o->event.value);
  200. if (!NCDValue_InitString(out, str)) {
  201. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  202. return 0;
  203. }
  204. return 1;
  205. }
  206. if (!strcmp(name, "code_numeric")) {
  207. char str[50];
  208. snprintf(str, sizeof(str), "%"PRIu16, o->event.code);
  209. if (!NCDValue_InitString(out, str)) {
  210. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  211. return 0;
  212. }
  213. return 1;
  214. }
  215. if (!strcmp(name, "code")) {
  216. const char *str = "unknown";
  217. #define MAKE_CASE(_evname_, _name_) \
  218. case _evname_: \
  219. str = evdev_##_name_##_to_str(o->event.code); \
  220. break;
  221. switch (o->event.type) {
  222. #ifdef EV_KEY
  223. MAKE_CASE(EV_KEY, key)
  224. #endif
  225. #ifdef EV_REL
  226. MAKE_CASE(EV_REL, rel)
  227. #endif
  228. #ifdef EV_ABS
  229. MAKE_CASE(EV_ABS, abs)
  230. #endif
  231. #ifdef EV_SW
  232. MAKE_CASE(EV_SW, sw)
  233. #endif
  234. #ifdef EV_MSC
  235. MAKE_CASE(EV_MSC, msc)
  236. #endif
  237. #ifdef EV_LED
  238. MAKE_CASE(EV_LED, led)
  239. #endif
  240. #ifdef EV_REP
  241. MAKE_CASE(EV_REP, rep)
  242. #endif
  243. #ifdef EV_SND
  244. MAKE_CASE(EV_SND, snd)
  245. #endif
  246. #ifdef EV_FF_STATUS
  247. MAKE_CASE(EV_FF_STATUS, ffstatus)
  248. #endif
  249. }
  250. if (!NCDValue_InitString(out, str)) {
  251. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  252. return 0;
  253. }
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. static void nextevent_func_new (NCDModuleInst *i)
  259. {
  260. // allocate instance
  261. struct nextevent_instance *o = malloc(sizeof(*o));
  262. if (!o) {
  263. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  264. goto fail0;
  265. }
  266. NCDModuleInst_Backend_SetUser(i, o);
  267. // init arguments
  268. o->i = i;
  269. // check arguments
  270. if (!NCDValue_ListRead(o->i->args, 0)) {
  271. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  272. goto fail1;
  273. }
  274. // get method object
  275. struct instance *mo = i->method_object->inst_user;
  276. ASSERT(mo->processing)
  277. // signal up.
  278. // Do it before finishing the event so our process does not advance any further if
  279. // we would be killed the event provider going down.
  280. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  281. // wait for next event
  282. device_nextevent(mo);
  283. return;
  284. fail1:
  285. free(o);
  286. fail0:
  287. NCDModuleInst_Backend_SetError(i);
  288. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  289. }
  290. static void nextevent_func_die (void *vo)
  291. {
  292. struct nextevent_instance *o = vo;
  293. NCDModuleInst *i = o->i;
  294. // free instance
  295. free(o);
  296. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  297. }
  298. static const struct NCDModule modules[] = {
  299. {
  300. .type = "sys.evdev",
  301. .func_new = func_new,
  302. .func_die = func_die,
  303. .func_getvar = func_getvar
  304. }, {
  305. .type = "sys.evdev::nextevent",
  306. .func_new = nextevent_func_new,
  307. .func_die = nextevent_func_die
  308. }, {
  309. .type = NULL
  310. }
  311. };
  312. const struct NCDModuleGroup ncdmodule_sys_evdev = {
  313. .modules = modules
  314. };