From d9d89f712280844fb4fd69a2dd7ed7c664e6d963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sun, 21 Jun 2020 21:30:46 +0200 Subject: [PATCH] Get Events from evdev --- setsu/CMakeLists.txt | 5 +- setsu/cmake/FindEvdev.cmake | 26 ++++++++ .../cmake/{FindUDEV.cmake => FindUdev.cmake} | 4 +- setsu/include/setsu.h | 1 + setsu/src/setsu.c | 65 +++++++++++++++++-- setsu/test/main.c | 17 +++++ 6 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 setsu/cmake/FindEvdev.cmake rename setsu/cmake/{FindUDEV.cmake => FindUdev.cmake} (92%) diff --git a/setsu/CMakeLists.txt b/setsu/CMakeLists.txt index e230c49..3c63a9f 100644 --- a/setsu/CMakeLists.txt +++ b/setsu/CMakeLists.txt @@ -13,8 +13,9 @@ add_library(setsu target_include_directories(setsu PUBLIC include) -find_package(UDEV REQUIRED) -target_link_libraries(setsu UDEV::libudev) +find_package(Udev REQUIRED) +find_package(Evdev REQUIRED) +target_link_libraries(setsu Udev::libudev Evdev::libevdev) if(SETSU_BUILD_TEST) add_executable(setsutest diff --git a/setsu/cmake/FindEvdev.cmake b/setsu/cmake/FindEvdev.cmake new file mode 100644 index 0000000..a6be138 --- /dev/null +++ b/setsu/cmake/FindEvdev.cmake @@ -0,0 +1,26 @@ +# Provides: Evdev::libevdev + +set(_prefix Evdev) +set(_target "${_prefix}::libevdev") + +find_package(PkgConfig REQUIRED) + +pkg_check_modules("${_prefix}" libevdev) + +function(resolve_location) + +endfunction() + +if(${_prefix}_FOUND) + add_library("${_target}" INTERFACE IMPORTED) + target_link_libraries("${_target}" INTERFACE ${${_prefix}_LIBRARIES}) + target_include_directories("${_target}" INTERFACE ${${_prefix}_INCLUDE_DIRS}) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args("${_prefix}" + REQUIRED_VARS "${_prefix}_LIBRARIES" + VERSION_VAR "${_prefix}_VERSION") + +unset(_prefix) +unset(_target) diff --git a/setsu/cmake/FindUDEV.cmake b/setsu/cmake/FindUdev.cmake similarity index 92% rename from setsu/cmake/FindUDEV.cmake rename to setsu/cmake/FindUdev.cmake index 3b6b1e0..369f806 100644 --- a/setsu/cmake/FindUDEV.cmake +++ b/setsu/cmake/FindUdev.cmake @@ -1,6 +1,6 @@ -# Provides: UDEV::libudev +# Provides: Udev::libudev -set(_prefix UDEV) +set(_prefix Udev) set(_target "${_prefix}::libudev") find_package(PkgConfig REQUIRED) diff --git a/setsu/include/setsu.h b/setsu/include/setsu.h index 8df7536..5d8e766 100644 --- a/setsu/include/setsu.h +++ b/setsu/include/setsu.h @@ -22,5 +22,6 @@ typedef struct setsu_ctx_t SetsuCtx; SetsuCtx *setsu_ctx_new(); void setsu_ctx_free(SetsuCtx *ctx); +void setsu_ctx_run(SetsuCtx *ctx); #endif diff --git a/setsu/src/setsu.c b/setsu/src/setsu.c index 76ae7d5..5bd2681 100644 --- a/setsu/src/setsu.c +++ b/setsu/src/setsu.c @@ -1,11 +1,31 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ #include #include +#include #include #include #include +#include +#include +#include #include @@ -15,6 +35,7 @@ typedef struct setsu_device_t struct setsu_device_t *next; char *path; int fd; + struct libevdev *evdev; } SetsuDevice; struct setsu_ctx_t @@ -140,19 +161,32 @@ static SetsuDevice *connect(SetsuCtx *ctx, const char *path) SetsuDevice *dev = malloc(sizeof(SetsuDevice)); if(!dev) return NULL; + memset(dev, 0, sizeof(*dev)); + dev->fd = -1; dev->path = strdup(path); if(!dev->path) - { - free(dev); - return NULL; - } + goto error; - printf("connect %s\n", dev->path); - //dev->fd = open(dev->path, O_RDONLY | O_NONBLOCK); + dev->fd = open(dev->path, O_RDONLY | O_NONBLOCK); + if(dev->fd == -1) + goto error; + + if(libevdev_new_from_fd(dev->fd, &dev->evdev) < 0) + goto error; + + printf("connected to %s\n", libevdev_get_name(dev->evdev)); dev->next = ctx->dev; ctx->dev = dev; return dev; +error: + if(dev->evdev) + libevdev_free(dev->evdev); + if(dev->fd != -1) + close(dev->fd); + free(dev->path); + free(dev); + return NULL; } static void disconnect(SetsuCtx *ctx, SetsuDevice *dev) @@ -175,3 +209,22 @@ static void disconnect(SetsuCtx *ctx, SetsuDevice *dev) free(dev); } +void setsu_ctx_run(SetsuCtx *ctx) +{ + SetsuDevice *dev = ctx->dev; + if(!dev) + return; + + int rc; + do + { + struct input_event ev; + rc = libevdev_next_event(dev->evdev, LIBEVDEV_READ_FLAG_NORMAL, &ev); + if (rc == 0) + printf("Event: %s %s %d\n", + libevdev_event_type_get_name(ev.type), + libevdev_event_code_get_name(ev.type, ev.code), + ev.value); + } while (rc == 1 || rc == 0 || rc == -EAGAIN); +} + diff --git a/setsu/test/main.c b/setsu/test/main.c index 823c283..bfbf0f7 100644 --- a/setsu/test/main.c +++ b/setsu/test/main.c @@ -1,3 +1,19 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ #include @@ -19,6 +35,7 @@ int main() printf("Failed to init setsu\n"); return 1; } + setsu_ctx_run(ctx); atexit(quit); return 0; }