From 49fb5bdeb16bf23bf08bb8233324d73a34deae91 Mon Sep 17 00:00:00 2001 From: Abi Hafshin Date: Sat, 12 Dec 2015 15:28:34 +0700 Subject: [PATCH] [WPE] Touch event support --- Source/WPE/Source/Input/LibinputServer.cpp | 50 +++++++++++++++++++ Source/WPE/Source/Input/LibinputServer.h | 5 ++ .../ViewBackend/BCMRPi/ViewBackendBCMRPi.cpp | 8 ++- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/Source/WPE/Source/Input/LibinputServer.cpp b/Source/WPE/Source/Input/LibinputServer.cpp index 09887a993e46d..33fc4294f7d07 100644 --- a/Source/WPE/Source/Input/LibinputServer.cpp +++ b/Source/WPE/Source/Input/LibinputServer.cpp @@ -103,6 +103,12 @@ void LibinputServer::setHandlePointerEvents(bool handle) fprintf(stderr, "[LibinputServer] %s pointer events.\n", handle ? "Enabling" : "Disabling"); } +void LibinputServer::setHandleTouchEvents(bool handle) +{ + m_handleTouchEvents = handle; + fprintf(stderr, "[LibinputServer] %s handle events.\n", handle ? "Enabling" : "Disabling"); +} + void LibinputServer::setPointerBounds(uint32_t width, uint32_t height) { m_pointerBounds = { width, height }; @@ -114,6 +120,19 @@ void LibinputServer::processEvents() while (auto* event = libinput_get_event(m_libinput)) { switch (libinput_event_get_type(event)) { + case LIBINPUT_EVENT_TOUCH_DOWN: + if (m_handleTouchEvents) + handleTouchEvent(event, Input::TouchEvent::Type::Down); + printf("touch down\n"); + break; + case LIBINPUT_EVENT_TOUCH_UP: + if (m_handleTouchEvents) + handleTouchEvent(event, Input::TouchEvent::Type::Up); + break; + case LIBINPUT_EVENT_TOUCH_MOTION: + if (m_handleTouchEvents) + handleTouchEvent(event, Input::TouchEvent::Type::Motion); + break; case LIBINPUT_EVENT_KEYBOARD_KEY: { auto* keyEvent = libinput_event_get_keyboard_event(event); @@ -204,6 +223,37 @@ void LibinputServer::dispatchKeyboardEvent(const Input::KeyboardEvent::Raw& even m_client->handleKeyboardEvent({ event.time, std::get<0>(result), std::get<1>(result), !!event.state, std::get<2>(result) }); } +void LibinputServer::handleTouchEvent(struct libinput_event *event, Input::TouchEvent::Type type) +{ + auto* touchEvent = libinput_event_get_touch_event(event); + uint32_t time = libinput_event_touch_get_time(touchEvent); + int id = libinput_event_touch_get_slot(touchEvent); + auto& targetPoint = m_touchEvents[id]; + int32_t x, y; + + if (type != Input::TouchEvent::Up) { + x = libinput_event_touch_get_x_transformed(touchEvent, m_pointerBounds.first); + y = libinput_event_touch_get_y_transformed(touchEvent, m_pointerBounds.second); + } else { + // libinput can't return pointer position on touch-up + x = targetPoint.x; + y = targetPoint.y; + } + targetPoint = Input::TouchEvent::Raw{ type, time, id, x, y }; + + m_client->handleTouchEvent({ + m_touchEvents, + type, + id, + time + }); + + if (type == Input::TouchEvent::Up) { + targetPoint = Input::TouchEvent::Raw{ Input::TouchEvent::Null, 0, -1, -1, -1 }; + } +} + + GSourceFuncs LibinputServer::EventSource::s_sourceFuncs = { nullptr, // prepare // check diff --git a/Source/WPE/Source/Input/LibinputServer.h b/Source/WPE/Source/Input/LibinputServer.h index b2276627defe8..2dff4288303b5 100644 --- a/Source/WPE/Source/Input/LibinputServer.h +++ b/Source/WPE/Source/Input/LibinputServer.h @@ -46,6 +46,7 @@ class LibinputServer : public Input::KeyboardEventRepeating::Client { void setClient(Input::Client* client); void setHandlePointerEvents(bool handle); + void setHandleTouchEvents(bool handle); void setPointerBounds(uint32_t, uint32_t); private: @@ -68,6 +69,10 @@ class LibinputServer : public Input::KeyboardEventRepeating::Client { std::pair m_pointerCoords; std::pair m_pointerBounds; + bool m_handleTouchEvents { false }; + std::array m_touchEvents; + void handleTouchEvent(struct libinput_event *event, Input::TouchEvent::Type type) + class EventSource { public: static GSourceFuncs s_sourceFuncs; diff --git a/Source/WPE/Source/ViewBackend/BCMRPi/ViewBackendBCMRPi.cpp b/Source/WPE/Source/ViewBackend/BCMRPi/ViewBackendBCMRPi.cpp index 067f846af9eb5..f5e3d6ed85261 100644 --- a/Source/WPE/Source/ViewBackend/BCMRPi/ViewBackendBCMRPi.cpp +++ b/Source/WPE/Source/ViewBackend/BCMRPi/ViewBackendBCMRPi.cpp @@ -122,12 +122,15 @@ void ViewBackendBCMRPi::destroyBuffer(uint32_t) void ViewBackendBCMRPi::setInputClient(Input::Client* client) { + if (std::getenv("WPE_BCMRPI_TOUCH")) { + LibinputServer::singleton().setHandleTouchEvents(true); + } if (std::getenv("WPE_BCMRPI_CURSOR")) { m_cursor.reset(new Cursor(client, m_displayHandle, m_width, m_height)); client = m_cursor.get(); LibinputServer::singleton().setHandlePointerEvents(true); - LibinputServer::singleton().setPointerBounds(m_width, m_height); } + LibinputServer::singleton().setPointerBounds(m_width, m_height); LibinputServer::singleton().setClient(client); } @@ -197,8 +200,9 @@ void ViewBackendBCMRPi::Cursor::handleAxisEvent(Input::AxisEvent&& event) m_targetClient->handleAxisEvent(std::move(event)); } -void ViewBackendBCMRPi::Cursor::handleTouchEvent(Input::TouchEvent&&) +void ViewBackendBCMRPi::Cursor::handleTouchEvent(Input::TouchEvent&& event) { + m_targetClient->handleTouchEvent(std::move(event)); } // The cursor pointer data uses the modified left_ptr cursor from the