From 0de13b3fdfd90c349c94d25630d1a2810aa11ede Mon Sep 17 00:00:00 2001 From: jiangwenyu1 Date: Wed, 27 Sep 2023 16:41:18 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E9=9C=B2:=20=E9=80=80=E5=87=BA=E5=BA=94=E7=94=A8=E6=97=B6?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E9=87=8A=E6=94=BEWaylandSeatObject,=20Waylan?= =?UTF-8?q?dPointer,=20WaylandKeyboard=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/core/wayland_keyboard.cpp | 10 +++- .../framework/core/wayland_pointer.cpp | 11 ++-- .../framework/core/wayland_seat.cpp | 54 ++++++++++++++++++- wayland_adapter/framework/core/wayland_seat.h | 3 ++ 4 files changed, 71 insertions(+), 7 deletions(-) diff --git a/wayland_adapter/framework/core/wayland_keyboard.cpp b/wayland_adapter/framework/core/wayland_keyboard.cpp index 2d6c99c..d016759 100644 --- a/wayland_adapter/framework/core/wayland_keyboard.cpp +++ b/wayland_adapter/framework/core/wayland_keyboard.cpp @@ -90,8 +90,14 @@ void WaylandKeyboard::OnKeyboardLeave(struct wl_resource *surface_resource) } WaylandKeyboard::WaylandKeyboard(struct wl_client *client, uint32_t version, uint32_t id) - : WaylandResourceObject(client, &wl_keyboard_interface, version, id, &IWaylandKeyboard::impl_) {} + : WaylandResourceObject(client, &wl_keyboard_interface, version, id, &IWaylandKeyboard::impl_) +{ + LOG_DEBUG("WaylandKeyboard create, this=%{public}p", this); +} -WaylandKeyboard::~WaylandKeyboard() noexcept {} +WaylandKeyboard::~WaylandKeyboard() noexcept +{ + LOG_DEBUG("WaylandKeyboard release, this=%{public}p", this); +} } // namespace Wayland } // namespace FT \ No newline at end of file diff --git a/wayland_adapter/framework/core/wayland_pointer.cpp b/wayland_adapter/framework/core/wayland_pointer.cpp index 1c09654..94e0bbd 100644 --- a/wayland_adapter/framework/core/wayland_pointer.cpp +++ b/wayland_adapter/framework/core/wayland_pointer.cpp @@ -49,10 +49,15 @@ OHOS::sptr WaylandPointer::Create(struct wl_client *client, uint } WaylandPointer::WaylandPointer(struct wl_client *client, uint32_t version, uint32_t id) - : WaylandResourceObject(client, &wl_pointer_interface, version, id, &IWaylandPointer::impl_) {} - -WaylandPointer::~WaylandPointer() noexcept {} + : WaylandResourceObject(client, &wl_pointer_interface, version, id, &IWaylandPointer::impl_) +{ + LOG_DEBUG("WaylandPointer create, this=%{public}p", this); +} +WaylandPointer::~WaylandPointer() noexcept +{ + LOG_DEBUG("WaylandPointer release, this=%{public}p", this); +} void WaylandPointer::OnPointerButton(uint32_t time, uint32_t button, bool isPressed) { diff --git a/wayland_adapter/framework/core/wayland_seat.cpp b/wayland_adapter/framework/core/wayland_seat.cpp index b9f2f20..1b45c47 100644 --- a/wayland_adapter/framework/core/wayland_seat.cpp +++ b/wayland_adapter/framework/core/wayland_seat.cpp @@ -77,6 +77,26 @@ OHOS::sptr WaylandSeat::GetWaylandSeatGlobal() return wl_seat_global; } +void WaylandSeat::FreeSeatResource(struct wl_client *client, struct wl_resource *resource) +{ + std::lock_guard lock(seatResourcesMutex_); + auto iter = seatResourcesMap_.find(client); + if (iter == seatResourcesMap_.end()) { + return; + } + + auto seatObjectList = &iter->second; + for (auto itr = seatObjectList->begin(); itr != seatObjectList->end(); itr++) { + if ((*itr)->WlResource() == resource) { + seatObjectList->erase(itr); + break; + } + } + if (seatResourcesMap_[client].empty()) { + seatResourcesMap_.erase(client); + } +} + WaylandSeat::WaylandSeat(struct wl_display *display) : WaylandGlobal(display, &wl_seat_interface, WL_SEAT_MAX_VERSION) {} @@ -97,6 +117,8 @@ void WaylandSeat::Bind(struct wl_client *client, uint32_t version, uint32_t id) LOG_ERROR("no memory"); return; } + + std::lock_guard lock(seatResourcesMutex_); WaylandObjectsPool::GetInstance().AddObject(ObjectId(object->WlClient(), object->Id()), object); seatResourcesMap_[client].emplace_back(object); @@ -111,6 +133,7 @@ void WaylandSeat::Bind(struct wl_client *client, uint32_t version, uint32_t id) void WaylandSeat::GetKeyboardResource(struct wl_client *client, std::list>& list) { + std::lock_guard lock(seatResourcesMutex_); auto iter = seatResourcesMap_.find(client); if (iter == seatResourcesMap_.end()) { return; @@ -134,6 +157,7 @@ void WaylandSeat::GetKeyboardResource(struct wl_client *client, std::list>& list) { + std::lock_guard lock(seatResourcesMutex_); auto iter = seatResourcesMap_.find(client); if (iter == seatResourcesMap_.end()) { return; @@ -197,10 +221,36 @@ void WaylandSeat::UpdateCapabilities(struct wl_resource *resource) } WaylandSeatObject::WaylandSeatObject(struct wl_client *client, uint32_t version, uint32_t id) - : WaylandResourceObject(client, &wl_seat_interface, version, id, &IWaylandSeat::impl_) {} + : WaylandResourceObject(client, &wl_seat_interface, version, id, &IWaylandSeat::impl_) +{ + LOG_DEBUG("WaylandSeatObject create, this=%{public}p, id=%{public}u", this, id); +} + +void WaylandSeatObject::OnResourceDestroy() +{ + // free pointer + auto pointerIter = pointerResourcesMap_.find(WlClient()); + if (pointerIter != pointerResourcesMap_.end()) { + pointerIter->second.clear(); + } + + // free keyboard + auto keyboardIter = keyboardResourcesMap_.find(WlClient()); + if (keyboardIter != keyboardResourcesMap_.end()) { + keyboardIter->second.clear(); + } -WaylandSeatObject::~WaylandSeatObject() noexcept {} + OHOS::sptr wlSeat = WaylandSeat::GetWaylandSeatGlobal(); + if (wlSeat == nullptr) { + return; + } + wlSeat->FreeSeatResource(WlClient(), WlResource()); +} +WaylandSeatObject::~WaylandSeatObject() noexcept +{ + LOG_DEBUG("WaylandSeatObject release, this=%{public}p", this); +} void WaylandSeatObject::GetChildPointer(std::list> &list) { diff --git a/wayland_adapter/framework/core/wayland_seat.h b/wayland_adapter/framework/core/wayland_seat.h index 90961eb..09d6400 100644 --- a/wayland_adapter/framework/core/wayland_seat.h +++ b/wayland_adapter/framework/core/wayland_seat.h @@ -42,6 +42,7 @@ public: void GetPointerResource(struct wl_client *client, std::list> &list); void GetKeyboardResource(struct wl_client *client, std::list> &list); ~WaylandSeat() noexcept override; + void FreeSeatResource(struct wl_client *client, struct wl_resource *resource); private: WaylandSeat(struct wl_display *display); @@ -49,6 +50,7 @@ private: static void UpdateCapabilities(struct wl_resource *resource); std::unordered_map>> seatResourcesMap_; std::unique_ptr thread_ = nullptr; + mutable std::mutex seatResourcesMutex_; }; class WaylandSeatObject final : public WaylandResourceObject { @@ -59,6 +61,7 @@ public: ~WaylandSeatObject() noexcept; void GetChildPointer(std::list> &list); void GetChildKeyboard(std::list> &list); + void OnResourceDestroy() override; private: void GetPointer(uint32_t id); -- Gitee