Get Events from evdev

This commit is contained in:
Florian Märkl 2020-06-21 21:30:46 +02:00
commit d9d89f7122
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
6 changed files with 108 additions and 10 deletions

View file

@ -13,8 +13,9 @@ add_library(setsu
target_include_directories(setsu PUBLIC include) target_include_directories(setsu PUBLIC include)
find_package(UDEV REQUIRED) find_package(Udev REQUIRED)
target_link_libraries(setsu UDEV::libudev) find_package(Evdev REQUIRED)
target_link_libraries(setsu Udev::libudev Evdev::libevdev)
if(SETSU_BUILD_TEST) if(SETSU_BUILD_TEST)
add_executable(setsutest add_executable(setsutest

View file

@ -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)

View file

@ -1,6 +1,6 @@
# Provides: UDEV::libudev # Provides: Udev::libudev
set(_prefix UDEV) set(_prefix Udev)
set(_target "${_prefix}::libudev") set(_target "${_prefix}::libudev")
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)

View file

@ -22,5 +22,6 @@ typedef struct setsu_ctx_t SetsuCtx;
SetsuCtx *setsu_ctx_new(); SetsuCtx *setsu_ctx_new();
void setsu_ctx_free(SetsuCtx *ctx); void setsu_ctx_free(SetsuCtx *ctx);
void setsu_ctx_run(SetsuCtx *ctx);
#endif #endif

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <setsu.h> #include <setsu.h>
#include <libudev.h> #include <libudev.h>
#include <libevdev/libevdev.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
@ -15,6 +35,7 @@ typedef struct setsu_device_t
struct setsu_device_t *next; struct setsu_device_t *next;
char *path; char *path;
int fd; int fd;
struct libevdev *evdev;
} SetsuDevice; } SetsuDevice;
struct setsu_ctx_t struct setsu_ctx_t
@ -140,19 +161,32 @@ static SetsuDevice *connect(SetsuCtx *ctx, const char *path)
SetsuDevice *dev = malloc(sizeof(SetsuDevice)); SetsuDevice *dev = malloc(sizeof(SetsuDevice));
if(!dev) if(!dev)
return NULL; return NULL;
memset(dev, 0, sizeof(*dev));
dev->fd = -1;
dev->path = strdup(path); dev->path = strdup(path);
if(!dev->path) if(!dev->path)
{ goto error;
free(dev);
return NULL;
}
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; dev->next = ctx->dev;
ctx->dev = dev; ctx->dev = dev;
return 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) static void disconnect(SetsuCtx *ctx, SetsuDevice *dev)
@ -175,3 +209,22 @@ static void disconnect(SetsuCtx *ctx, SetsuDevice *dev)
free(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);
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <setsu.h> #include <setsu.h>
@ -19,6 +35,7 @@ int main()
printf("Failed to init setsu\n"); printf("Failed to init setsu\n");
return 1; return 1;
} }
setsu_ctx_run(ctx);
atexit(quit); atexit(quit);
return 0; return 0;
} }