sys_evdev.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #define MAKE_LOOKUP_FUNC(_name_) \
  66. static const char * evdev_##_name_##_to_str (uint16_t type) \
  67. { \
  68. if (type >= (sizeof(_name_##_names) / sizeof(_name_##_names[0])) || !_name_##_names[type]) { \
  69. return "unknown"; \
  70. } \
  71. return _name_##_names[type]; \
  72. }
  73. MAKE_LOOKUP_FUNC(type)
  74. MAKE_LOOKUP_FUNC(key)
  75. MAKE_LOOKUP_FUNC(rel)
  76. MAKE_LOOKUP_FUNC(abs)
  77. MAKE_LOOKUP_FUNC(sw)
  78. MAKE_LOOKUP_FUNC(msc)
  79. MAKE_LOOKUP_FUNC(led)
  80. MAKE_LOOKUP_FUNC(rep)
  81. MAKE_LOOKUP_FUNC(snd)
  82. MAKE_LOOKUP_FUNC(ffstatus)
  83. static void device_handler (struct instance *o, int events)
  84. {
  85. ASSERT(!o->processing)
  86. int res = read(o->evdev_fd, &o->event, sizeof(o->event));
  87. if (res < 0) {
  88. ModuleLog(o->i, BLOG_ERROR, "read failed");
  89. return;
  90. }
  91. if (res != sizeof(o->event)) {
  92. ModuleLog(o->i, BLOG_ERROR, "read wrong");
  93. return;
  94. }
  95. // stop reading
  96. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, 0);
  97. // set processing
  98. o->processing = 1;
  99. // signal up
  100. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  101. }
  102. static void device_nextevent (struct instance *o)
  103. {
  104. ASSERT(o->processing)
  105. // start reading
  106. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, BREACTOR_READ);
  107. // set not processing
  108. o->processing = 0;
  109. // signal down
  110. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  111. }
  112. static void func_new (NCDModuleInst *i)
  113. {
  114. // allocate instance
  115. struct instance *o = malloc(sizeof(*o));
  116. if (!o) {
  117. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  118. goto fail0;
  119. }
  120. NCDModuleInst_Backend_SetUser(i, o);
  121. // init arguments
  122. o->i = i;
  123. // check arguments
  124. NCDValue *device_arg;
  125. if (!NCDValue_ListRead(o->i->args, 1, &device_arg)) {
  126. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  127. goto fail1;
  128. }
  129. if (NCDValue_Type(device_arg) != NCDVALUE_STRING) {
  130. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  131. goto fail1;
  132. }
  133. // open device
  134. if ((o->evdev_fd = open(NCDValue_StringValue(device_arg), O_RDONLY)) < 0) {
  135. ModuleLog(o->i, BLOG_ERROR, "open failed");
  136. goto fail1;
  137. }
  138. // set non-blocking
  139. if (!badvpn_set_nonblocking(o->evdev_fd)) {
  140. ModuleLog(o->i, BLOG_ERROR, "badvpn_set_nonblocking failed");
  141. goto fail2;
  142. }
  143. // init BFileDescriptor
  144. BFileDescriptor_Init(&o->bfd, o->evdev_fd, (BFileDescriptor_handler)device_handler, o);
  145. if (!BReactor_AddFileDescriptor(o->i->reactor, &o->bfd)) {
  146. ModuleLog(o->i, BLOG_ERROR, "BReactor_AddFileDescriptor failed");
  147. goto fail2;
  148. }
  149. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, BREACTOR_READ);
  150. // set not processing
  151. o->processing = 0;
  152. return;
  153. fail2:
  154. ASSERT_FORCE(close(o->evdev_fd) == 0)
  155. fail1:
  156. free(o);
  157. fail0:
  158. NCDModuleInst_Backend_SetError(i);
  159. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  160. }
  161. static void func_die (void *vo)
  162. {
  163. struct instance *o = vo;
  164. NCDModuleInst *i = o->i;
  165. // free BFileDescriptor
  166. BReactor_RemoveFileDescriptor(o->i->reactor, &o->bfd);
  167. // close device
  168. ASSERT_FORCE(close(o->evdev_fd) == 0)
  169. // free instance
  170. free(o);
  171. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  172. }
  173. static int func_getvar (void *vo, const char *name, NCDValue *out)
  174. {
  175. struct instance *o = vo;
  176. ASSERT(o->processing)
  177. if (!strcmp(name, "type")) {
  178. if (!NCDValue_InitString(out, evdev_type_to_str(o->event.type))) {
  179. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  180. return 0;
  181. }
  182. return 1;
  183. }
  184. if (!strcmp(name, "value")) {
  185. char str[50];
  186. snprintf(str, sizeof(str), "%"PRIi32, o->event.value);
  187. if (!NCDValue_InitString(out, str)) {
  188. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. if (!strcmp(name, "code_numeric")) {
  194. char str[50];
  195. snprintf(str, sizeof(str), "%"PRIu16, o->event.code);
  196. if (!NCDValue_InitString(out, str)) {
  197. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  198. return 0;
  199. }
  200. return 1;
  201. }
  202. if (!strcmp(name, "code")) {
  203. const char *str = "unknown";
  204. #define MAKE_CASE(_evname_, _name_) \
  205. case _evname_: \
  206. str = evdev_##_name_##_to_str(o->event.code); \
  207. break;
  208. switch (o->event.type) {
  209. #ifdef EV_KEY
  210. MAKE_CASE(EV_KEY, key)
  211. #endif
  212. #ifdef EV_REL
  213. MAKE_CASE(EV_REL, rel)
  214. #endif
  215. #ifdef EV_ABS
  216. MAKE_CASE(EV_ABS, abs)
  217. #endif
  218. #ifdef EV_SW
  219. MAKE_CASE(EV_SW, sw)
  220. #endif
  221. #ifdef EV_MSC
  222. MAKE_CASE(EV_MSC, msc)
  223. #endif
  224. #ifdef EV_LED
  225. MAKE_CASE(EV_LED, led)
  226. #endif
  227. #ifdef EV_REP
  228. MAKE_CASE(EV_REP, rep)
  229. #endif
  230. #ifdef EV_SND
  231. MAKE_CASE(EV_SND, snd)
  232. #endif
  233. #ifdef EV_FF_STATUS
  234. MAKE_CASE(EV_FF_STATUS, ffstatus)
  235. #endif
  236. }
  237. if (!NCDValue_InitString(out, str)) {
  238. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. return 0;
  244. }
  245. static void nextevent_func_new (NCDModuleInst *i)
  246. {
  247. // allocate instance
  248. struct nextevent_instance *o = malloc(sizeof(*o));
  249. if (!o) {
  250. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  251. goto fail0;
  252. }
  253. NCDModuleInst_Backend_SetUser(i, o);
  254. // init arguments
  255. o->i = i;
  256. // check arguments
  257. if (!NCDValue_ListRead(o->i->args, 0)) {
  258. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  259. goto fail1;
  260. }
  261. // get method object
  262. struct instance *mo = i->method_object->inst_user;
  263. ASSERT(mo->processing)
  264. // signal up.
  265. // Do it before finishing the event so our process does not advance any further if
  266. // we would be killed the event provider going down.
  267. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  268. // wait for next event
  269. device_nextevent(mo);
  270. return;
  271. fail1:
  272. free(o);
  273. fail0:
  274. NCDModuleInst_Backend_SetError(i);
  275. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  276. }
  277. static void nextevent_func_die (void *vo)
  278. {
  279. struct nextevent_instance *o = vo;
  280. NCDModuleInst *i = o->i;
  281. // free instance
  282. free(o);
  283. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  284. }
  285. static const struct NCDModule modules[] = {
  286. {
  287. .type = "sys.evdev",
  288. .func_new = func_new,
  289. .func_die = func_die,
  290. .func_getvar = func_getvar
  291. }, {
  292. .type = "sys.evdev::nextevent",
  293. .func_new = nextevent_func_new,
  294. .func_die = nextevent_func_die
  295. }, {
  296. .type = NULL
  297. }
  298. };
  299. const struct NCDModuleGroup ncdmodule_sys_evdev = {
  300. .modules = modules
  301. };