sys_evdev.c 10 KB

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