From ba46123aabb6f291d689ece74cc773954e575cbc Mon Sep 17 00:00:00 2001 From: yours3lf <0.tamas.marton@gmail.com> Date: Wed, 20 May 2020 22:39:29 +0100 Subject: [PATCH] added input handler, doesn't work --- test/inputHandler/inputHandler.c | 135 +++++++++++++++++++++++++++++++ test/inputHandler/inputHandler.h | 14 ++++ test/inputTest/CMakeLists.txt | 1 + test/inputTest/inputTest.cpp | 59 ++------------ test/mipmapping/CMakeLists.txt | 4 +- test/mipmapping/mipmapping.cpp | 47 +++-------- 6 files changed, 170 insertions(+), 90 deletions(-) create mode 100644 test/inputHandler/inputHandler.c create mode 100644 test/inputHandler/inputHandler.h diff --git a/test/inputHandler/inputHandler.c b/test/inputHandler/inputHandler.c new file mode 100644 index 0000000..02e873b --- /dev/null +++ b/test/inputHandler/inputHandler.c @@ -0,0 +1,135 @@ +#if defined (__cplusplus) +extern "C" { +#endif + +#include "inputHandler.h" + +#include + +#include +#include +#include +#include +#include + +#include + +#include "../../driver/CustomAssert.h" + +struct libinput *li; +struct udev* _udev; + +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 initInputHandler() +{ + _udev = udev_new(); + udev_ref(_udev); + li = libinput_udev_create_context(&interface, NULL, _udev); assert(li); + uint32_t res = libinput_udev_assign_seat(li, "seat0"); assert(res == 0); + + libinput_dispatch(li); + + //configure devices +// libinput_device_config_accel_set_speed() +// libinput_device_config_accel_set_profile() +// libinput_device_config_scroll_set_natural_scroll_enabled() + + struct libinput_event* event = 0; + while ((event = libinput_get_event(li)) != 0) + { + uint32_t type = libinput_event_get_type(event); + + switch(type) + { + case LIBINPUT_EVENT_DEVICE_ADDED: + { + struct libinput_device* dev = libinput_event_get_device(event); + printf("Device name: %s\n", libinput_device_get_name(dev)); + break; + } + }; + + + libinput_event_destroy(event); + libinput_dispatch(li); + } +} + +void handleInput() +{ + assert(li); + + struct libinput_event* event = 0; + while ((event = libinput_get_event(li)) != 0) + { + uint32_t type = libinput_event_get_type(event); + + switch(type) + { + case LIBINPUT_EVENT_DEVICE_ADDED: + { + struct libinput_device* dev = libinput_event_get_device(event); + printf("Device name: %s\n", libinput_device_get_name(dev)); + break; + } + case LIBINPUT_EVENT_KEYBOARD_KEY: + { + uint32_t keyCode = libinput_event_keyboard_get_key(libinput_event_get_keyboard_event(event)); + enum libinput_key_state keyState = libinput_event_keyboard_get_key_state(libinput_event_get_keyboard_event(event)); + printf("Keypress keycode %u state %u\n", keyCode, keyState); + break; + } + case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE: + { + double xCoord = libinput_event_pointer_get_absolute_x(libinput_event_get_pointer_event(event)); + double yCoord = libinput_event_pointer_get_absolute_y(libinput_event_get_pointer_event(event)); + printf("Pointer motion xcoord %lf ycoord %lf\n", xCoord, yCoord); + break; + } + case LIBINPUT_EVENT_POINTER_BUTTON: + { + uint32_t button = libinput_event_pointer_get_button(libinput_event_get_pointer_event(event)); + enum libinput_button_state buttonState = libinput_event_pointer_get_button_state(libinput_event_get_pointer_event(event)); + printf("Pointer button button %u, state %u\n", button, buttonState); + break; + } + case LIBINPUT_EVENT_POINTER_AXIS: + { + enum libinput_pointer_axis_source axisSource = libinput_event_pointer_get_axis_source(libinput_event_get_pointer_event(event)); + double axisValueVertical = libinput_event_pointer_get_axis_value(libinput_event_get_pointer_event(event), LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL); + double axisValueHorizontal = libinput_event_pointer_get_axis_value(libinput_event_get_pointer_event(event), LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL); + printf("Pointer axis source %u, value vertical %lf, value horizontal %lf\n", axisSource, axisValueVertical, axisValueHorizontal); + break; + } + }; + + + libinput_event_destroy(event); + libinput_dispatch(li); + } +} + +void uninitInputHandler() +{ + libinput_unref(li); + udev_unref(_udev); +} + +#if defined (__cplusplus) +} +#endif diff --git a/test/inputHandler/inputHandler.h b/test/inputHandler/inputHandler.h new file mode 100644 index 0000000..366ea0c --- /dev/null +++ b/test/inputHandler/inputHandler.h @@ -0,0 +1,14 @@ +#pragma once + +#if defined (__cplusplus) +extern "C" { +#endif + +void initInputHandler(); +void handleInput(); +void uninitInputHandler(); + +#if defined (__cplusplus) +} +#endif + diff --git a/test/inputTest/CMakeLists.txt b/test/inputTest/CMakeLists.txt index 3faf63e..5d8321c 100644 --- a/test/inputTest/CMakeLists.txt +++ b/test/inputTest/CMakeLists.txt @@ -1,6 +1,7 @@ file(GLOB testSrc "*.h" "*.cpp" + "../inputHandler/inputHandler.c" ) add_executable(inputTest ${testSrc}) diff --git a/test/inputTest/inputTest.cpp b/test/inputTest/inputTest.cpp index 74ce65f..f13786f 100644 --- a/test/inputTest/inputTest.cpp +++ b/test/inputTest/inputTest.cpp @@ -3,66 +3,17 @@ #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, -}; - +#include "../inputHandler/inputHandler.h" int main(void) { - struct libinput *li; - struct libinput_event *event; - struct udev* udev; + initInputHandler(); - udev = udev_new(); - li = libinput_udev_create_context(&interface, NULL, udev); - libinput_udev_assign_seat(li, "seat0"); - libinput_dispatch(li); - - while (true) + while(true) { - event = libinput_get_event(li); - - if(event) - { - uint32_t type = libinput_event_get_type(event); - struct libinput_device* dev = libinput_event_get_device(event); - - // handle the event here - std::cout << "Event type: " << type << std::endl; - - switch(type) - { - case LIBINPUT_EVENT_DEVICE_ADDED: - { - std::cout << "Device name: " << libinput_device_get_name(dev) << std::endl; - break; - } - }; - - - libinput_event_destroy(event); - libinput_dispatch(li); - } + handleInput(); } - libinput_unref(li); + uninitInputHandler(); return 0; } diff --git a/test/mipmapping/CMakeLists.txt b/test/mipmapping/CMakeLists.txt index 9608777..e02febd 100644 --- a/test/mipmapping/CMakeLists.txt +++ b/test/mipmapping/CMakeLists.txt @@ -1,6 +1,7 @@ file(GLOB testSrc "*.h" "*.cpp" + ../inputHandler/inputHandler.c ) add_executable(mipmapping ${testSrc} ) @@ -8,4 +9,5 @@ target_compile_options(mipmapping PRIVATE -Wall -std=c++11 -march=${RPI_ARCH} -fPIC ) -target_link_libraries(mipmapping vulkan $) +#target_link_libraries(mipmapping vulkan $) +target_link_libraries(mipmapping rpi-vk-driver mtdev evdev udev input) diff --git a/test/mipmapping/mipmapping.cpp b/test/mipmapping/mipmapping.cpp index 6704312..e469212 100644 --- a/test/mipmapping/mipmapping.cpp +++ b/test/mipmapping/mipmapping.cpp @@ -10,6 +10,8 @@ #include "driver/vkExt.h" #include "QPUassembler/qpu_assembler.h" +#include "../inputHandler/inputHandler.h" + //#define GLFW_INCLUDE_VULKAN //#define VK_USE_PLATFORM_WIN32_KHR //#include @@ -177,26 +179,7 @@ void cleanup() { vkDestroyInstance(instance, nullptr); } -void run() { - // Note: dynamically loading loader may be a better idea to fail gracefully when Vulkan is not supported - - // Create window for Vulkan - //glfwInit(); - - //glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - //glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - - //window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "The 630 line cornflower blue window", nullptr, nullptr); - - // Use Vulkan - setupVulkan(); - - mainLoop(); - - cleanup(); -} - -void setupVulkan() { +void setupVulkan() { createInstance(); findPhysicalDevice(); createWindowSurface(); @@ -217,11 +200,11 @@ void setupVulkan() { } void mainLoop() { - //while (!glfwWindowShouldClose(window)) { - for(int c = 0; c < 300; ++c){ + while(true) + { draw(); - //glfwPollEvents(); + handleInput(); } } @@ -804,7 +787,7 @@ void draw() { assert(0); } - std::cout << "acquired image" << std::endl; + //std::cout << "acquired image" << std::endl; // Wait for image to be available and draw VkSubmitInfo submitInfo = {}; @@ -824,7 +807,7 @@ void draw() { assert(0); } - std::cout << "submitted draw command buffer" << std::endl; + //std::cout << "submitted draw command buffer" << std::endl; // Present drawn image // Note: semaphore here is not strictly necessary, because commands are processed in submission order within a single queue @@ -844,7 +827,7 @@ void draw() { assert(0); } - std::cout << "submitted presentation command buffer" << std::endl; + //std::cout << "submitted presentation command buffer" << std::endl; } void CreateRenderPass() @@ -1805,15 +1788,7 @@ void CreateVertexBuffer() } int main() { - // Note: dynamically loading loader may be a better idea to fail gracefully when Vulkan is not supported - - // Create window for Vulkan - //glfwInit(); - - //glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - //glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - - //window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "The 630 line cornflower blue window", nullptr, nullptr); + initInputHandler(); // Use Vulkan setupVulkan(); @@ -1822,6 +1797,8 @@ int main() { cleanup(); + uninitInputHandler(); + return 0; }