sys_evdev.c 10 KB

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