sys_evdev.c 10 KB

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