From 1d8f5a637ee5799fff7c5eb382afc0035dfde6d8 Mon Sep 17 00:00:00 2001 From: Reinout Meliesie Date: Sat, 27 Feb 2021 14:39:41 +0100 Subject: [PATCH] Import old version using libinput --- CMakeLists.txt | 22 +++++++++++ cmake/cmake-modules | 1 + main.c | 94 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 CMakeLists.txt create mode 160000 cmake/cmake-modules create mode 100644 main.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d78d717 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.17) +project(evdev_actions) + +set(CMAKE_C_STANDARD 11) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake-modules") + +add_executable(evdev_actions main.c) + +find_package(Libinput REQUIRED) +if(NOT ${Libinput_FOUND}) + message(FATAL_ERROR "libinput not found") +endif() +target_include_directories(evdev_actions PUBLIC ${Libinput_INCLUDE_DIRS}) +target_link_libraries(evdev_actions ${Libinput_LIBRARIES}) + +find_package(udev REQUIRED) +if(NOT ${UDEV_FOUND}) + message(FATAL_ERROR "libudev not found") +endif() +target_include_directories(evdev_actions PUBLIC ${UDEV_INCLUDE_DIRS}) +target_link_libraries(evdev_actions ${UDEV_LIBRARIES}) diff --git a/cmake/cmake-modules b/cmake/cmake-modules new file mode 160000 index 0000000..6a74889 --- /dev/null +++ b/cmake/cmake-modules @@ -0,0 +1 @@ +Subproject commit 6a748896188fdf3f885820ccd686393ea5215dbf diff --git a/main.c b/main.c new file mode 100644 index 0000000..c01c549 --- /dev/null +++ b/main.c @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include +#include + + +static int open_restricted(const char* path, int flags, void* user_data) +{ + int fd = open(path, flags); + return fd < 0 ? -errno : fd; +} + +static void close_restricted(int fd, void* user_data) +{ + close(fd); +} + +const static struct libinput_interface interface = { + .open_restricted = open_restricted, + .close_restricted = close_restricted, +}; + +void handle_device_added(struct libinput_event_device_notify* event) +{ + assert(event != NULL); +} + +void handle_button_press(struct libinput_event_switch* event) +{ + assert(event != NULL); + + +} + +int main(int argc, char* argv[]) +{ + struct udev* udev_context = udev_new(); + struct libinput* libinput_context = libinput_udev_create_context(&interface, NULL, udev_context); + + libinput_udev_assign_seat(libinput_context, "seat0"); + + int libinput_fd = libinput_get_fd(libinput_context); + + struct libinput_event* event; + + // Setup loop + libinput_dispatch(libinput_context); + while ((event = libinput_get_event(libinput_context)) != NULL) { + printf("Setup event occurred\n"); + + if (libinput_event_get_type(event) == LIBINPUT_EVENT_DEVICE_ADDED) { + handle_device_added(libinput_event_get_device_notify_event(event)); + printf("\tDevice added event\n"); + } + else { + printf("\tOther type of event\n"); + } + + libinput_event_destroy(event); + } + printf("No more setup events\n"); + + // Main loop + while (1) { + // TODO: use select/poll/epoll to block until fd has new data + // https://daniel.haxx.se/docs/poll-vs-select.html + // https://en.wikipedia.org/wiki/Epoll + // https://github.com/wayland-project/libinput/blob/master/tools/libinput-debug-events.c + // https://wayland.freedesktop.org/libinput/doc/latest/api/ + + libinput_dispatch(libinput_context); + while ((event = libinput_get_event(libinput_context)) != NULL) { + printf("Event occurred\n"); + + if (libinput_event_get_type(event) == LIBINPUT_EVENT_SWITCH_TOGGLE) { + handle_button_press(libinput_event_get_switch_event(event)); + printf("\tSwitch toggle event\n"); + } + else { + printf("\tOther type of event\n"); + } + + libinput_event_destroy(event); + libinput_dispatch(libinput_context); + } + } + + printf("No more events\n"); + + return EXIT_SUCCESS; +}