| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- /**
- * @file sys_evdev.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @section DESCRIPTION
- *
- * Linux event device module.
- *
- * Synopsis: sys.evdev(string device)
- * Description: reports input events from a Linux event device. Transitions up when an event is
- * detected, and goes down waiting for the next event when sys.evdev::nextevent() is called.
- * Variables:
- * string type - symbolic event type (e.g. EV_KEY, EV_REL, EV_ABS), corresponding to
- * (struct input_event).type, or "unknown"
- * string value - event value (signed integer), equal to (struct input_event).value
- * string code_numeric - numeric event code (unsigned integer), equal to
- * (struct input_event).code
- * string code - symbolic event code (e.g. KEY_ESC. KEY_1, KEY_2, BTN_LEFT), corrresponding
- * to (struct input_event).code, or "unknown"
- *
- * Synopsis: sys.evdev::nextevent()
- * Description: makes the evdev module transition down in order to report the next event.
- */
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <inttypes.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <linux/input.h>
- #include <misc/nonblocking.h>
- #include <misc/debug.h>
- #include <ncd/NCDModule.h>
- #include <generated/blog_channel_ncd_sys_evdev.h>
- #include "linux_input_names.h"
- #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
- struct instance {
- NCDModuleInst *i;
- int evdev_fd;
- BFileDescriptor bfd;
- int processing;
- struct input_event event;
- };
- static void instance_free (struct instance *o, int is_error);
- #define MAKE_LOOKUP_FUNC(_name_) \
- static const char * evdev_##_name_##_to_str (uint16_t type) \
- { \
- if (type >= (sizeof(_name_##_names) / sizeof(_name_##_names[0])) || !_name_##_names[type]) { \
- return "unknown"; \
- } \
- return _name_##_names[type]; \
- }
- MAKE_LOOKUP_FUNC(type)
- MAKE_LOOKUP_FUNC(key)
- MAKE_LOOKUP_FUNC(rel)
- MAKE_LOOKUP_FUNC(abs)
- MAKE_LOOKUP_FUNC(sw)
- MAKE_LOOKUP_FUNC(msc)
- MAKE_LOOKUP_FUNC(led)
- MAKE_LOOKUP_FUNC(rep)
- MAKE_LOOKUP_FUNC(snd)
- MAKE_LOOKUP_FUNC(ffstatus)
- static void device_handler (struct instance *o, int events)
- {
- if (o->processing) {
- ModuleLog(o->i, BLOG_ERROR, "device error");
- instance_free(o, 1);
- return;
- }
-
- int res = read(o->evdev_fd, &o->event, sizeof(o->event));
- if (res < 0) {
- ModuleLog(o->i, BLOG_ERROR, "read failed");
- instance_free(o, 1);
- return;
- }
- if (res != sizeof(o->event)) {
- ModuleLog(o->i, BLOG_ERROR, "read wrong");
- instance_free(o, 1);
- return;
- }
-
- // stop reading
- BReactor_SetFileDescriptorEvents(o->i->iparams->reactor, &o->bfd, 0);
-
- // set processing
- o->processing = 1;
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
- }
- static void device_nextevent (struct instance *o)
- {
- ASSERT(o->processing)
-
- // start reading
- BReactor_SetFileDescriptorEvents(o->i->iparams->reactor, &o->bfd, BREACTOR_READ);
-
- // set not processing
- o->processing = 0;
-
- // signal down
- NCDModuleInst_Backend_Down(o->i);
- }
- static void func_new (void *vo, NCDModuleInst *i)
- {
- struct instance *o = vo;
- o->i = i;
-
- // check arguments
- NCDValRef device_arg;
- if (!NCDVal_ListRead(o->i->args, 1, &device_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
- if (!NCDVal_IsStringNoNulls(device_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail0;
- }
-
- // open device
- if ((o->evdev_fd = open(NCDVal_StringValue(device_arg), O_RDONLY)) < 0) {
- ModuleLog(o->i, BLOG_ERROR, "open failed");
- goto fail0;
- }
-
- // set non-blocking
- if (!badvpn_set_nonblocking(o->evdev_fd)) {
- ModuleLog(o->i, BLOG_ERROR, "badvpn_set_nonblocking failed");
- goto fail1;
- }
-
- // init BFileDescriptor
- BFileDescriptor_Init(&o->bfd, o->evdev_fd, (BFileDescriptor_handler)device_handler, o);
- if (!BReactor_AddFileDescriptor(o->i->iparams->reactor, &o->bfd)) {
- ModuleLog(o->i, BLOG_ERROR, "BReactor_AddFileDescriptor failed");
- goto fail1;
- }
- BReactor_SetFileDescriptorEvents(o->i->iparams->reactor, &o->bfd, BREACTOR_READ);
-
- // set not processing
- o->processing = 0;
- return;
-
- fail1:
- if (close(o->evdev_fd) < 0) {
- ModuleLog(o->i, BLOG_ERROR, "close failed");
- }
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- void instance_free (struct instance *o, int is_error)
- {
- // free BFileDescriptor
- BReactor_RemoveFileDescriptor(o->i->iparams->reactor, &o->bfd);
-
- // close device.
- // Ignore close error which happens if the device is removed.
- if (close(o->evdev_fd) < 0) {
- ModuleLog(o->i, BLOG_ERROR, "close failed");
- }
-
- if (is_error) {
- NCDModuleInst_Backend_SetError(o->i);
- }
- NCDModuleInst_Backend_Dead(o->i);
- }
- static void func_die (void *vo)
- {
- struct instance *o = vo;
- instance_free(o, 0);
- }
- static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
- {
- struct instance *o = vo;
- ASSERT(o->processing)
-
- if (!strcmp(name, "type")) {
- *out = NCDVal_NewString(mem, evdev_type_to_str(o->event.type));
- if (NCDVal_IsInvalid(*out)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
- }
- return 1;
- }
-
- if (!strcmp(name, "value")) {
- char str[50];
- snprintf(str, sizeof(str), "%"PRIi32, o->event.value);
- *out = NCDVal_NewString(mem, str);
- if (NCDVal_IsInvalid(*out)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
- }
- return 1;
- }
-
- if (!strcmp(name, "code_numeric")) {
- char str[50];
- snprintf(str, sizeof(str), "%"PRIu16, o->event.code);
- *out = NCDVal_NewString(mem, str);
- if (NCDVal_IsInvalid(*out)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
- }
- return 1;
- }
-
- if (!strcmp(name, "code")) {
- const char *str = "unknown";
-
- #define MAKE_CASE(_evname_, _name_) \
- case _evname_: \
- str = evdev_##_name_##_to_str(o->event.code); \
- break;
-
- switch (o->event.type) {
- #ifdef EV_KEY
- MAKE_CASE(EV_KEY, key)
- #endif
- #ifdef EV_REL
- MAKE_CASE(EV_REL, rel)
- #endif
- #ifdef EV_ABS
- MAKE_CASE(EV_ABS, abs)
- #endif
- #ifdef EV_SW
- MAKE_CASE(EV_SW, sw)
- #endif
- #ifdef EV_MSC
- MAKE_CASE(EV_MSC, msc)
- #endif
- #ifdef EV_LED
- MAKE_CASE(EV_LED, led)
- #endif
- #ifdef EV_REP
- MAKE_CASE(EV_REP, rep)
- #endif
- #ifdef EV_SND
- MAKE_CASE(EV_SND, snd)
- #endif
- #ifdef EV_FF_STATUS
- MAKE_CASE(EV_FF_STATUS, ffstatus)
- #endif
- }
-
- *out = NCDVal_NewString(mem, str);
- if (NCDVal_IsInvalid(*out)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
- }
- return 1;
- }
-
- return 0;
- }
- static void nextevent_func_new (NCDModuleInst *i)
- {
- // check arguments
- if (!NCDVal_ListRead(i->args, 0)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
-
- // get method object
- struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
-
- // make sure we are currently reporting an event
- if (!mo->processing) {
- ModuleLog(i, BLOG_ERROR, "not reporting an event");
- goto fail0;
- }
-
- // signal up.
- // Do it before finishing the event so our process does not advance any further if
- // we would be killed the event provider going down.
- NCDModuleInst_Backend_Up(i);
-
- // wait for next event
- device_nextevent(mo);
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static const struct NCDModule modules[] = {
- {
- .type = "sys.evdev",
- .func_new2 = func_new,
- .func_die = func_die,
- .func_getvar = func_getvar,
- .alloc_size = sizeof(struct instance)
- }, {
- .type = "sys.evdev::nextevent",
- .func_new = nextevent_func_new
- }, {
- .type = NULL
- }
- };
- const struct NCDModuleGroup ncdmodule_sys_evdev = {
- .modules = modules
- };
|