Skip to content

Commit

Permalink
LayerSubsurface -> Subsurface
Browse files Browse the repository at this point in the history
  • Loading branch information
serebit committed Jul 7, 2024
1 parent 03cef50 commit 1f5c747
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 53 deletions.
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ magpie_sources = [
'input/seat.cpp',
'surface/layer.cpp',
'surface/popup.cpp',
'surface/subsurface.cpp',
'surface/view.cpp',
'surface/xdg_view.cpp',
'surface/xwayland_view.cpp',
Expand Down
32 changes: 2 additions & 30 deletions src/surface/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "popup.hpp"
#include "server.hpp"
#include "surface.hpp"
#include "subsurface.hpp"
#include "types.hpp"

#include "wlr-wrap-start.hpp"
Expand All @@ -29,35 +30,6 @@ static magpie_scene_layer_t magpie_layer_from_wlr_layer(const zwlr_layer_shell_v
}
}

static void subsurface_map_notify(wl_listener* listener, [[maybe_unused]] void* data) {
wlr_log(WLR_DEBUG, "wlr_subsurface.events.map(listener=%p, data=%p)", (void*) listener, data);

LayerSubsurface& subsurface = magpie_container_of(listener, subsurface, map);

wlr_surface_send_enter(subsurface.wlr->surface, &subsurface.parent.output.wlr);
}

static void subsurface_destroy_notify(wl_listener* listener, [[maybe_unused]] void* data) {
wlr_log(WLR_DEBUG, "wlr_subsurface.events.destroy(listener=%p, data=%p)", (void*) listener, data);

LayerSubsurface& subsurface = magpie_container_of(listener, subsurface, destroy);

subsurface.parent.subsurfaces.erase(subsurface.shared_from_this());
}

LayerSubsurface::LayerSubsurface(Layer& parent, wlr_subsurface& subsurface) noexcept
: listeners(*this), parent(parent), wlr(&subsurface) {
listeners.map.notify = subsurface_map_notify;
wl_signal_add(&subsurface.surface->events.map, &listeners.map);
listeners.destroy.notify = subsurface_destroy_notify;
wl_signal_add(&subsurface.events.destroy, &listeners.destroy);
}

LayerSubsurface::~LayerSubsurface() noexcept {
wl_list_remove(&listeners.map.link);
wl_list_remove(&listeners.destroy.link);
}

/* Called when the surface is mapped, or ready to display on-screen. */
static void wlr_layer_surface_v1_map_notify(wl_listener* listener, [[maybe_unused]] void* data) {
wlr_log(WLR_DEBUG, "wlr_layer_surface_v1.events.map(listener=%p, data=%p)", (void*) listener, data);
Expand Down Expand Up @@ -141,7 +113,7 @@ static void wlr_layer_surface_v1_new_subsurface_notify(wl_listener* listener, vo
Layer& layer = magpie_container_of(listener, layer, new_subsurface);
auto& subsurface = *static_cast<wlr_subsurface*>(data);

layer.subsurfaces.emplace(std::make_shared<LayerSubsurface>(layer, subsurface));
layer.subsurfaces.emplace(std::make_shared<Subsurface>(layer, subsurface));
}

Layer::Layer(Output& output, wlr_layer_surface_v1& surface) noexcept
Expand Down
22 changes: 0 additions & 22 deletions src/surface/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ class Layer final : public Surface {
wlr_scene_layer_surface_v1* scene_surface;
magpie_scene_layer_t scene_layer = MAGPIE_SCENE_LAYER_NORMAL;

std::set<std::shared_ptr<LayerSubsurface>> subsurfaces;

Layer(Output& output, wlr_layer_surface_v1& surface) noexcept;
~Layer() noexcept override;

Expand All @@ -47,24 +45,4 @@ class Layer final : public Surface {
[[nodiscard]] bool is_view() const override;
};

class LayerSubsurface final : public std::enable_shared_from_this<LayerSubsurface> {
public:
struct Listeners {
std::reference_wrapper<LayerSubsurface> parent;
wl_listener map = {};
wl_listener destroy = {};
explicit Listeners(LayerSubsurface& parent) noexcept : parent(parent) {}
};

private:
Listeners listeners;

public:
Layer& parent;
wlr_subsurface* wlr;

LayerSubsurface(Layer& parent, wlr_subsurface& subsurface) noexcept;
~LayerSubsurface() noexcept;
};

#endif
16 changes: 16 additions & 0 deletions src/surface/popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "output.hpp"
#include "server.hpp"
#include "subsurface.hpp"
#include "surface.hpp"
#include "types.hpp"

Expand Down Expand Up @@ -53,6 +54,18 @@ static void popup_new_popup_notify(wl_listener* listener, void* data) {
popup.popups.emplace(std::make_shared<Popup>(popup, *static_cast<wlr_xdg_popup*>(data)));
}

static void popup_new_subsurface_notify(wl_listener* listener, void* data) {
wlr_log(WLR_DEBUG, "wlr_xdg_popup.events.new_subsurface(listener=%p, data=%p)", (void*) listener, data);

if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_xdg_popup.events.new_subsurface");
return;
}

Popup& popup = magpie_container_of(listener, popup, new_subsurface);
popup.subsurfaces.emplace(std::make_shared<Subsurface>(popup, *static_cast<wlr_subsurface*>(data)));
}

Popup::Popup(Surface& parent, wlr_xdg_popup& wlr) noexcept
: listeners(*this), server(parent.get_server()), parent(parent), wlr(&wlr) {
auto* scene_tree = wlr_scene_xdg_surface_create(wlr_scene_tree_from_node(parent.scene_node), wlr.base);
Expand All @@ -67,12 +80,15 @@ Popup::Popup(Surface& parent, wlr_xdg_popup& wlr) noexcept
wl_signal_add(&wlr.base->events.destroy, &listeners.destroy);
listeners.new_popup.notify = popup_new_popup_notify;
wl_signal_add(&wlr.base->events.new_popup, &listeners.new_popup);
listeners.new_subsurface.notify = popup_new_subsurface_notify;
wl_signal_add(&wlr.base->surface->events.new_subsurface, &listeners.new_subsurface);
}

Popup::~Popup() noexcept {
wl_list_remove(&listeners.map.link);
wl_list_remove(&listeners.destroy.link);
wl_list_remove(&listeners.new_popup.link);
wl_list_remove(&listeners.new_subsurface.link);
if (wlr != nullptr) {
wlr_xdg_popup_destroy(wlr);
}
Expand Down
1 change: 1 addition & 0 deletions src/surface/popup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Popup final : public Surface {
wl_listener map = {};
wl_listener destroy = {};
wl_listener new_popup = {};
wl_listener new_subsurface = {};
explicit Listeners(Popup& parent) noexcept : parent(parent) {}
};

Expand Down
43 changes: 43 additions & 0 deletions src/surface/subsurface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "subsurface.hpp"

#include "types.hpp"

#include <utility>

#include "wlr-wrap-start.hpp"
#include <wlr/types/wlr_layer_shell_v1.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_subcompositor.h>
#include <wlr/util/log.h>
#include "wlr-wrap-end.hpp"

static void subsurface_destroy_notify(wl_listener* listener, [[maybe_unused]] void* data) {
wlr_log(WLR_DEBUG, "wlr_subsurface.events.destroy(listener=%p, data=%p)", (void*) listener, data);

Subsurface& subsurface = magpie_container_of(listener, subsurface, destroy);

subsurface.parent.subsurfaces.erase(std::dynamic_pointer_cast<Subsurface>(subsurface.shared_from_this()));
}

Subsurface::Subsurface(Surface& parent, wlr_subsurface& subsurface) noexcept
: listeners(*this), parent(parent), wlr(subsurface) {
listeners.destroy.notify = subsurface_destroy_notify;
wl_signal_add(&subsurface.events.destroy, &listeners.destroy);
}

Subsurface::~Subsurface() noexcept {
wl_list_remove(&listeners.map.link);
wl_list_remove(&listeners.destroy.link);
}

wlr_surface* Subsurface::get_wlr_surface() const {
return wlr.surface;
}

Server& Subsurface::get_server() const {
return parent.get_server();
}

bool Subsurface::is_view() const {
return false;
}
40 changes: 40 additions & 0 deletions src/surface/subsurface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef MAGPIE_SUBSURFACE_HPP
#define MAGPIE_SUBSURFACE_HPP

#include "server.hpp"
#include "surface.hpp"
#include "types.hpp"

#include <functional>
#include <memory>
#include <set>

#include "wlr-wrap-start.hpp"
#include <wlr/types/wlr_subcompositor.h>
#include "wlr-wrap-end.hpp"

class Subsurface final : public Surface {
public:
struct Listeners {
std::reference_wrapper<Subsurface> parent;
wl_listener map = {};
wl_listener destroy = {};
explicit Listeners(Subsurface& parent) noexcept : parent(parent) {}
};

private:
Listeners listeners;

public:
Surface& parent;
wlr_subsurface& wlr;

Subsurface(Surface& parent, wlr_subsurface& subsurface) noexcept;
~Subsurface() noexcept override;

[[nodiscard]] wlr_surface* get_wlr_surface() const override;
[[nodiscard]] Server& get_server() const override;
[[nodiscard]] bool is_view() const override;
};

#endif
1 change: 1 addition & 0 deletions src/surface/surface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
struct Surface : public virtual std::enable_shared_from_this<Surface> {
wlr_scene_node* scene_node = nullptr;
std::set<std::shared_ptr<Popup>> popups;
std::set<std::shared_ptr<Subsurface>> subsurfaces;

virtual ~Surface() noexcept = default;

Expand Down
1 change: 1 addition & 0 deletions src/surface/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class XdgView final : public View {
wl_listener set_app_id = {};
wl_listener set_parent = {};
wl_listener new_popup = {};
wl_listener new_subsurface = {};
explicit Listeners(XdgView& parent) noexcept : parent(parent) {}
};

Expand Down
16 changes: 16 additions & 0 deletions src/surface/xdg_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "output.hpp"
#include "popup.hpp"
#include "server.hpp"
#include "subsurface.hpp"
#include "surface.hpp"
#include "input/seat.hpp"

Expand Down Expand Up @@ -147,6 +148,18 @@ static void xdg_surface_new_popup_notify(wl_listener* listener, void* data) {
view.popups.emplace(std::make_shared<Popup>(view, *static_cast<wlr_xdg_popup*>(data)));
}

static void xdg_surface_new_subsurface_notify(wl_listener* listener, void* data) {
wlr_log(WLR_DEBUG, "wlr_xdg_toplevel.events.new_subsurface(listener=%p, data=%p)", (void*) listener, data);

if (data == nullptr) {
wlr_log(WLR_ERROR, "No data passed to wlr_xdg_surface.events.new_subsurface");
return;
}

XdgView& view = magpie_container_of(listener, view, new_subsurface);
view.subsurfaces.emplace(std::make_shared<Subsurface>(view, *static_cast<wlr_subsurface*>(data)));
}

XdgView::XdgView(Server& server, wlr_xdg_toplevel& xdg_toplevel) noexcept
: listeners(*this), server(server), wlr(xdg_toplevel) {
auto* scene_tree = wlr_scene_xdg_surface_create(&server.scene->tree, xdg_toplevel.base);
Expand Down Expand Up @@ -194,6 +207,8 @@ XdgView::XdgView(Server& server, wlr_xdg_toplevel& xdg_toplevel) noexcept
wl_signal_add(&wlr.events.set_parent, &listeners.set_parent);
listeners.new_popup.notify = xdg_surface_new_popup_notify;
wl_signal_add(&wlr.base->events.new_popup, &listeners.new_popup);
listeners.new_subsurface.notify = xdg_surface_new_subsurface_notify;
wl_signal_add(&wlr.base->surface->events.new_subsurface, &listeners.new_subsurface);
}

XdgView::~XdgView() noexcept {
Expand All @@ -208,6 +223,7 @@ XdgView::~XdgView() noexcept {
wl_list_remove(&listeners.set_app_id.link);
wl_list_remove(&listeners.set_parent.link);
wl_list_remove(&listeners.new_popup.link);
wl_list_remove(&listeners.new_subsurface.link);
}

wlr_surface* XdgView::get_wlr_surface() const {
Expand Down
2 changes: 1 addition & 1 deletion src/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct View;
class XdgView;
class XWaylandView;
class Layer;
class LayerSubsurface;
class Subsurface;
class Popup;

class ForeignToplevelHandle;
Expand Down

0 comments on commit 1f5c747

Please sign in to comment.