From c9fb7d0f190e9f140e5730da3f0cd67fe1413c6f Mon Sep 17 00:00:00 2001 From: jiangwenyu1 Date: Wed, 13 Sep 2023 20:45:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=20wl=5Fkeyboard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/core/wayland_keyboard.cpp | 43 +++++++++++++++ .../framework/core/wayland_keyboard.h | 5 ++ .../framework/core/wayland_seat.cpp | 21 +++++++ wayland_adapter/framework/core/wayland_seat.h | 2 + .../framework/core/wayland_surface.cpp | 42 +++++++++++++- wayland_adapter/test/wayland_demo.cpp | 55 ++++++++++++++++++- 6 files changed, 163 insertions(+), 5 deletions(-) diff --git a/wayland_adapter/framework/core/wayland_keyboard.cpp b/wayland_adapter/framework/core/wayland_keyboard.cpp index ac5d37b..896bedd 100644 --- a/wayland_adapter/framework/core/wayland_keyboard.cpp +++ b/wayland_adapter/framework/core/wayland_keyboard.cpp @@ -40,6 +40,49 @@ OHOS::sptr WaylandKeyboard::Create(struct wl_client *client, ui return keyboard; } +void WaylandKeyboard::OnKeyboardKey(uint32_t key, int32_t state, uint32_t time) +{ + wl_resource *keyboard = WlResource(); + if (keyboard == nullptr) { + return; + } + wl_display *display = WlDisplay(); + if (display == nullptr) { + return; + } + uint32_t serial = wl_display_next_serial(display); + wl_keyboard_send_key(keyboard, serial, time, key, state); +} + +void WaylandKeyboard::OnKeyboardEnter(struct wl_resource *surface_resource) +{ + wl_display *display = WlDisplay(); + if (display == nullptr) { + return; + } + uint32_t serial = wl_display_next_serial(display); + wl_resource *keyboard = WlResource(); + if (keyboard == nullptr) { + return; + } + struct wl_array keys = {}; + wl_keyboard_send_enter(keyboard, serial, surface_resource, &keys); +} + +void WaylandKeyboard::OnKeyboardLeave(struct wl_resource *surface_resource) +{ + wl_display *display = WlDisplay(); + if (display == nullptr) { + return; + } + uint32_t serial = wl_display_next_serial(display); + wl_resource *keyboard = WlResource(); + if (keyboard == nullptr) { + return; + } + wl_keyboard_send_leave(keyboard, serial, surface_resource); +} + WaylandKeyboard::WaylandKeyboard(struct wl_client *client, uint32_t version, uint32_t id) : WaylandResourceObject(client, &wl_keyboard_interface, version, id, &IWaylandKeyboard::impl_) {} diff --git a/wayland_adapter/framework/core/wayland_keyboard.h b/wayland_adapter/framework/core/wayland_keyboard.h index 91888f5..156ddda 100644 --- a/wayland_adapter/framework/core/wayland_keyboard.h +++ b/wayland_adapter/framework/core/wayland_keyboard.h @@ -30,6 +30,11 @@ public: static OHOS::sptr Create(struct wl_client *client, uint32_t version, uint32_t id); ~WaylandKeyboard() noexcept override; + void OnKeyboardKey(uint32_t key, int32_t state, uint32_t time); + void OnKeyboardEnter(struct wl_resource *surface_resource); + void OnKeyboardLeave(struct wl_resource *surface_resource); + + private: WaylandKeyboard(struct wl_client *client, uint32_t version, uint32_t id); }; diff --git a/wayland_adapter/framework/core/wayland_seat.cpp b/wayland_adapter/framework/core/wayland_seat.cpp index f034b4c..93e148b 100644 --- a/wayland_adapter/framework/core/wayland_seat.cpp +++ b/wayland_adapter/framework/core/wayland_seat.cpp @@ -109,6 +109,22 @@ void WaylandSeat::Bind(struct wl_client *client, uint32_t version, uint32_t id) thread_ = std::make_unique(&WaylandSeat::UpdateCapabilities, this); } +OHOS::sptr WaylandSeat::GetKeyboardResource(struct wl_client *client) +{ + if (seatResourcesMap_.count(client) == 0) { + return nullptr; + } + + auto seatResource = seatResourcesMap_.at(client); + // Erase seat resource from map if it is destroyed. + if (seatResource == nullptr) { + seatResourcesMap_.erase(client); + return nullptr; + } + + return seatResource->GetChildKeyboard(); +} + OHOS::sptr WaylandSeat::GetPointerResource(struct wl_client *client) { if (seatResourcesMap_.count(client) == 0) { @@ -185,6 +201,11 @@ OHOS::sptr WaylandSeatObject::GetChildPointer() return pointer_; } +OHOS::sptr WaylandSeatObject::GetChildKeyboard() +{ + return keyboard_; +} + void WaylandSeatObject::GetPointer(uint32_t id) { auto pointer = WaylandPointer::Create(WlClient(), wl_resource_get_version(WlResource()), id); diff --git a/wayland_adapter/framework/core/wayland_seat.h b/wayland_adapter/framework/core/wayland_seat.h index f0e0122..2143e07 100644 --- a/wayland_adapter/framework/core/wayland_seat.h +++ b/wayland_adapter/framework/core/wayland_seat.h @@ -39,6 +39,7 @@ public: static OHOS::sptr Create(struct wl_display *display); static OHOS::sptr GetWaylandSeatGlobal(); OHOS::sptr GetPointerResource(struct wl_client *client); + OHOS::sptr GetKeyboardResource(struct wl_client *client); ~WaylandSeat() noexcept override; private: @@ -56,6 +57,7 @@ public: WaylandSeatObject(struct wl_client *client, uint32_t version, uint32_t id); ~WaylandSeatObject() noexcept; OHOS::sptr GetChildPointer(); + OHOS::sptr GetChildKeyboard(); private: void GetPointer(uint32_t id); diff --git a/wayland_adapter/framework/core/wayland_surface.cpp b/wayland_adapter/framework/core/wayland_surface.cpp index a237965..5b8de8e 100644 --- a/wayland_adapter/framework/core/wayland_surface.cpp +++ b/wayland_adapter/framework/core/wayland_surface.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include +#include #include "wayland_surface.h" #include "wayland_objects_pool.h" @@ -42,13 +42,27 @@ public: private: OHOS::sptr wlSurface_ = nullptr; int32_t MapPointerActionButton(int32_t PointerActionButtonType) const; + int32_t MapKeyAction(int32_t keyAction) const; const std::map ptrActionMap_ = { {OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT, BTN_LEFT}, {OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT, BTN_RIGHT}, }; - + const std::map keyActionMap_ = { + {OHOS::MMI::KeyEvent::KEY_ACTION_UP, WL_KEYBOARD_KEY_STATE_RELEASED}, + {OHOS::MMI::KeyEvent::KEY_ACTION_DOWN, WL_KEYBOARD_KEY_STATE_PRESSED}, + }; + const int32_t INVALID_KEYACTION = -1; }; +int32_t InputEventConsumer::MapKeyAction(int32_t keyAction) const +{ + auto it = keyActionMap_.find(keyAction); + if (it == keyActionMap_.end()) { + return INVALID_KEYACTION; + } + return it->second; +} + int32_t InputEventConsumer::MapPointerActionButton(int32_t PointerActionButtonType) const { auto it = ptrActionMap_.find(PointerActionButtonType); @@ -60,6 +74,22 @@ int32_t InputEventConsumer::MapPointerActionButton(int32_t PointerActionButtonTy bool InputEventConsumer::OnInputEvent(const std::shared_ptr& keyEvent) const { + OHOS::sptr wlSeat = WaylandSeat::GetWaylandSeatGlobal(); + if (wlSeat == nullptr) { + return false; + } + auto keyboard = wlSeat->GetKeyboardResource(wlSurface_->WlClient()); + if (keyboard == nullptr) { + LOG_WARN("GetKeyboardResource fail"); + return false; + } + + int32_t keyAction = MapKeyAction(keyEvent->GetKeyAction()); + if (keyAction == INVALID_KEYACTION) { + return false; + } + + keyboard->OnKeyboardKey(keyEvent->GetKeyCode(), keyAction, keyEvent->GetActionTime()); keyEvent->MarkProcessed(); return true; } @@ -83,6 +113,12 @@ bool InputEventConsumer::OnInputEvent(const std::shared_ptrGetKeyboardResource(wlSurface_->WlClient()); + if (keyboard == nullptr) { + LOG_WARN("GetKeyboardResource fail"); + return false; + } + OHOS::MMI::PointerEvent::PointerItem pointerItem; int32_t pointId = pointerEvent->GetPointerId(); if (!pointerEvent->GetPointerItem(pointId, pointerItem)) { @@ -92,8 +128,10 @@ bool InputEventConsumer::OnInputEvent(const std::shared_ptrGetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW) { pointer->OnPointerEnter(pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), wlSurface_->WlResource()); + keyboard->OnKeyboardEnter(wlSurface_->WlResource()); } else if (pointerEvent->GetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW) { pointer->OnPointerLeave(wlSurface_->WlResource()); + keyboard->OnKeyboardLeave(wlSurface_->WlResource()); } else if (pointerEvent->GetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN || pointerEvent->GetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_UP) { int32_t buttonId = MapPointerActionButton(pointerEvent->GetButtonId()); diff --git a/wayland_adapter/test/wayland_demo.cpp b/wayland_adapter/test/wayland_demo.cpp index 52ce57f..e3ed15e 100644 --- a/wayland_adapter/test/wayland_demo.cpp +++ b/wayland_adapter/test/wayland_demo.cpp @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include @@ -58,6 +58,7 @@ struct display { struct wl_shm *shm; struct wl_seat *seat; struct wl_pointer *pointer; + struct wl_keyboard *keyboard; struct window *window; bool has_rgba8888 = false; }; @@ -209,7 +210,7 @@ static void HandleXdgToplevelConfigure(void *data, struct xdg_toplevel *xdg_topl struct window *window = static_cast(data); if (window->width != width || window->height != height) { - fprintf(stderr, "HandleXdgToplevelConfigure, width:%d, height:%d\n", width, height); + fprintf(stderr, "HandleXdgToplevelConfigure, width:%d, height:%d\n", width, height); window->width = width; window->height = height; } @@ -624,6 +625,52 @@ struct wl_pointer_listener pointer_listener = { .axis_discrete = pointer_axis_discrete, }; + +void keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, uint32_t format, + int32_t fd, uint32_t size) +{ + +} + +void keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, + struct wl_surface *surface, struct wl_array *keys) +{ + fprintf(stderr, "keyboard_enter in\n"); +} + +void keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, + struct wl_surface *surface) +{ + fprintf(stderr, "keyboard_leave in\n"); +} + +void keyboard_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, + uint32_t time, uint32_t key, uint32_t state) +{ + fprintf(stderr, "keyboard_key in, key=%u, state=%u\n", key, state); +} + +void keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, + uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) +{ + +} + +void keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard, + int32_t rate, int32_t delay) +{ + +} + +struct wl_keyboard_listener keyboard_listener = { + .keymap = keyboard_keymap, + .enter = keyboard_enter, + .leave = keyboard_leave, + .key = keyboard_key, + .modifiers = keyboard_modifiers, + .repeat_info = keyboard_repeat_info, +}; + void wl_seat_capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabilities) { struct display *d = static_cast(data); @@ -633,7 +680,9 @@ void wl_seat_capabilities(void *data, struct wl_seat *wl_seat, uint32_t capabili wl_pointer_add_listener(d->pointer, &pointer_listener, d); } if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) { - fprintf(stderr, "fangtian has a keyboard\n"); + fprintf(stderr, "fangtian has a keyboard, get it\n"); + d->keyboard = wl_seat_get_keyboard(d->seat); + wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d); } } -- Gitee