diff --git a/wayland_adapter/framework/core/wayland_surface.cpp b/wayland_adapter/framework/core/wayland_surface.cpp index 1db9f391b8d85a88fcab856cea5c010c6e382a59..a69722abcf54176a5bcd3f12c75fff7c83f9f53d 100644 --- a/wayland_adapter/framework/core/wayland_surface.cpp +++ b/wayland_adapter/framework/core/wayland_surface.cpp @@ -17,6 +17,7 @@ #include "wayland_surface.h" #include "wayland_objects_pool.h" +#include "wayland_event_loop.h" #include "ui/rs_surface_extractor.h" #include "wayland_region.h" #include "wayland_seat.h" @@ -75,22 +76,25 @@ 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; - } + keyEvent->MarkProcessed(); + WaylandEventLoop::GetInstance().QueueToLoop([this, keyEvent]{ + OHOS::sptr wlSeat = WaylandSeat::GetWaylandSeatGlobal(); + if (wlSeat == nullptr) { + return; + } + std::list> keyboardList; + wlSeat->GetKeyboardResource(wlSurface_->WlClient(), keyboardList); + int32_t keyAction = MapKeyAction(keyEvent->GetKeyAction()); + if (keyAction == INVALID_KEYACTION) { + return; + } - std::list> keyboardList; - wlSeat->GetKeyboardResource(wlSurface_->WlClient(), keyboardList); - int32_t keyAction = MapKeyAction(keyEvent->GetKeyAction()); - if (keyAction == INVALID_KEYACTION) { - return false; - } + for (auto &keyboard : keyboardList) { + keyboard->OnKeyboardKey(keyEvent->GetKeyCode(), keyAction, keyEvent->GetActionTime() / US_TO_MS); + } + wl_display_flush_clients(wlSurface_->WlDisplay()); + }); - for (auto &keyboard : keyboardList) { - keyboard->OnKeyboardKey(keyEvent->GetKeyCode(), keyAction, keyEvent->GetActionTime() / US_TO_MS); - } - keyEvent->MarkProcessed(); return true; } @@ -102,55 +106,58 @@ bool InputEventConsumer::OnInputEvent(const std::shared_ptr& pointerEvent) const { - OHOS::sptr wlSeat = WaylandSeat::GetWaylandSeatGlobal(); - if (wlSeat == nullptr) { - return false; - } pointerEvent->MarkProcessed(); - std::list> pointerList; - std::list> keyboardList; - wlSeat->GetPointerResource(wlSurface_->WlClient(), pointerList); - wlSeat->GetKeyboardResource(wlSurface_->WlClient(), keyboardList); - - OHOS::MMI::PointerEvent::PointerItem pointerItem; - int32_t pointId = pointerEvent->GetPointerId(); - if (!pointerEvent->GetPointerItem(pointId, pointerItem)) { - LOG_WARN("GetPointerItem fail"); - return false; - } - - Rect rect = wlSurface_->GetWindowGeometry(); - if (rect.x >= 0 && rect.y >= 0 && rect.width > 0 && rect.height > 0) { - pointerItem.SetWindowX(pointerItem.GetWindowX() + rect.x); - pointerItem.SetWindowY(pointerItem.GetWindowY() + rect.y); - } - if (pointerEvent->GetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW) { - for (auto &pointer : pointerList) { - pointer->OnPointerEnter(pointerItem.GetWindowX(), pointerItem.GetWindowY(), wlSurface_->WlResource()); - } - for (auto &keyboard : keyboardList) { - keyboard->OnKeyboardEnter(wlSurface_->WlResource()); + WaylandEventLoop::GetInstance().QueueToLoop([this, pointerEvent]{ + OHOS::sptr wlSeat = WaylandSeat::GetWaylandSeatGlobal(); + if (wlSeat == nullptr) { + return; } - } else if (pointerEvent->GetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW) { - for (auto &pointer : pointerList) { - pointer->OnPointerLeave(wlSurface_->WlResource()); + std::list> pointerList; + std::list> keyboardList; + wlSeat->GetPointerResource(wlSurface_->WlClient(), pointerList); + wlSeat->GetKeyboardResource(wlSurface_->WlClient(), keyboardList); + + OHOS::MMI::PointerEvent::PointerItem pointerItem; + int32_t pointId = pointerEvent->GetPointerId(); + if (!pointerEvent->GetPointerItem(pointId, pointerItem)) { + LOG_WARN("GetPointerItem fail"); + return; } - for (auto &keyboard : keyboardList) { - keyboard->OnKeyboardLeave(wlSurface_->WlResource()); + + Rect rect = wlSurface_->GetWindowGeometry(); + if (rect.x >= 0 && rect.y >= 0 && rect.width > 0 && rect.height > 0) { + pointerItem.SetWindowX(pointerItem.GetWindowX() + rect.x); + pointerItem.SetWindowY(pointerItem.GetWindowY() + rect.y); } - } 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()); - if (buttonId != OHOS::MMI::PointerEvent::BUTTON_NONE) { + if (pointerEvent->GetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW) { for (auto &pointer : pointerList) { - pointer->OnPointerButton(pointerEvent->GetActionTime() / US_TO_MS, buttonId, pointerItem.IsPressed()); + pointer->OnPointerEnter(pointerItem.GetWindowX(), pointerItem.GetWindowY(), wlSurface_->WlResource()); + } + for (auto &keyboard : keyboardList) { + keyboard->OnKeyboardEnter(wlSurface_->WlResource()); + } + } else if (pointerEvent->GetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW) { + for (auto &pointer : pointerList) { + pointer->OnPointerLeave(wlSurface_->WlResource()); + } + for (auto &keyboard : keyboardList) { + 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()); + if (buttonId != OHOS::MMI::PointerEvent::BUTTON_NONE) { + for (auto &pointer : pointerList) { + pointer->OnPointerButton(pointerEvent->GetActionTime() / US_TO_MS, buttonId, pointerItem.IsPressed()); + } + } + } else if (pointerEvent->GetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE) { + for (auto &pointer : pointerList) { + pointer->OnPointerMotionAbsolute(pointerEvent->GetActionTime() / US_TO_MS, pointerItem.GetWindowX(), pointerItem.GetWindowY()); } } - } else if (pointerEvent->GetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE) { - for (auto &pointer : pointerList) { - pointer->OnPointerMotionAbsolute(pointerEvent->GetActionTime() / US_TO_MS, pointerItem.GetWindowX(), pointerItem.GetWindowY()); - } - } + wl_display_flush_clients(wlSurface_->WlDisplay()); + }); return true; } diff --git a/wayland_adapter/utils/BUILD.gn b/wayland_adapter/utils/BUILD.gn index 50197a3c92f1cc6027d6a78dbd1519a200a853fe..141cbb99673d9cd56c0661959aadc077634ee7c0 100644 --- a/wayland_adapter/utils/BUILD.gn +++ b/wayland_adapter/utils/BUILD.gn @@ -24,6 +24,7 @@ ft_source_set("wayland_adapter_utils_sources") { "src/wayland_objects_pool.cpp", "src/wayland_resource_object.cpp", "src/wayland_keycode_trans.cpp", + "src/wayland_event_loop.cpp", ] public_configs = [ ":wayland_utils_public_config" ] diff --git a/wayland_adapter/utils/include/wayland_event_loop.h b/wayland_adapter/utils/include/wayland_event_loop.h new file mode 100644 index 0000000000000000000000000000000000000000..032cacc16d3c1069d0f197b94ccb91d98a6dde3b --- /dev/null +++ b/wayland_adapter/utils/include/wayland_event_loop.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Technologies Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "wayland_singleton.h" +#include "event_loop.h" + +namespace FT { +namespace Wayland { + +class WaylandEventLoop : public Singleton { + DECLARE_SINGLETON(WaylandEventLoop) + +public: + void Start(); + void QueueToLoop(Functor func); + template > + std::future Schedule(Task task) + { + return loop_->Schedule(task); + } + EventLoop *GetEventLoopPtr(); + WaylandEventLoop(); + ~WaylandEventLoop() noexcept; + +private: + std::shared_ptr loop_ = nullptr; +}; +} // namespace Wayland +} // namespace FT diff --git a/wayland_adapter/utils/src/wayland_event_loop.cpp b/wayland_adapter/utils/src/wayland_event_loop.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdbfb6efcd4e32803c8118410bc0230f48f9a1c2 --- /dev/null +++ b/wayland_adapter/utils/src/wayland_event_loop.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Technologies Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "wayland_event_loop.h" + +namespace FT { +namespace Wayland { + +WaylandEventLoop::WaylandEventLoop() +{ + loop_ = std::make_shared(); +} + +WaylandEventLoop::~WaylandEventLoop() +{ + loop_ = nullptr; +} + +void WaylandEventLoop::QueueToLoop(Functor func) +{ + if (loop_) { + loop_->QueueToLoop(func); + } +} + +EventLoop *WaylandEventLoop::GetEventLoopPtr() +{ + if (loop_) { + return loop_.get(); + } + return nullptr; +} + +void WaylandEventLoop::Start() +{ + if (loop_) { + loop_->Start(); + } +} +} // namespace Wayland +} // namespace FT diff --git a/wayland_adapter/wayland_server.cpp b/wayland_adapter/wayland_server.cpp index b1193515efc2e0751f38041ab16eca79974fa13a..5ebbad8e83f0ade175ed5493ab6918684d266cb0 100644 --- a/wayland_adapter/wayland_server.cpp +++ b/wayland_adapter/wayland_server.cpp @@ -17,6 +17,7 @@ #include #include "wayland_adapter_hilog.h" +#include "wayland_event_loop.h" namespace FT { namespace Wayland { @@ -72,26 +73,25 @@ void WaylandServer::OnStart() } CreateGlobalObjects(); - - loop_ = std::make_shared(); - wlDisplayChannel_ = std::make_unique(wl_event_loop_get_fd(wlDisplayLoop_), loop_.get()); + wlDisplayChannel_ = std::make_unique(wl_event_loop_get_fd(wlDisplayLoop_), + WaylandEventLoop::GetInstance().GetEventLoopPtr()); wlDisplayChannel_->SetReadCallback([this](TimeStamp timeStamp) { wl_event_loop_dispatch(wlDisplayLoop_, -1); wl_display_flush_clients(display_); }); wlDisplayChannel_->EnableReading(true); - loop_->Start(); + WaylandEventLoop::GetInstance().Start(); } void WaylandServer::OnStop() { LOG_INFO("OnStop"); - if (display_ == nullptr || loop_ == nullptr) { + if (display_ == nullptr) { return; } - auto stopWlDisplay = loop_->Schedule([this]() { + auto stopWlDisplay = WaylandEventLoop::GetInstance().Schedule([this]() { wl_display_terminate(display_); wl_display_destroy_clients(display_); wl_display_destroy(display_); @@ -103,7 +103,6 @@ void WaylandServer::OnStop() stopWlDisplay.wait(); display_ = nullptr; - loop_ = nullptr; compositorGlobal_ = nullptr; xdgWmBaseGlobal_ = nullptr; seatGlobal_ = nullptr; diff --git a/wayland_adapter/wayland_server.h b/wayland_adapter/wayland_server.h index 0ea0e2ab6409b81051f085e87a1a9509c97faf74..a22d5e9a28d8dbcd1ed144902b7e0ef9e5150352 100644 --- a/wayland_adapter/wayland_server.h +++ b/wayland_adapter/wayland_server.h @@ -52,7 +52,6 @@ private: struct wl_event_loop *wlDisplayLoop_ = nullptr; std::string socketName_; std::unique_ptr wlDisplayChannel_; - std::shared_ptr loop_; OHOS::sptr compositorGlobal_; OHOS::sptr xdgWmBaseGlobal_; OHOS::sptr outputGlobal_;