sys_evdev.c 10.0 KB

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