sys_evdev.c 10 KB

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