diff --git a/src/xdg_popup.zig b/src/xdg_popup.zig new file mode 100644 index 0000000..56e3580 --- /dev/null +++ b/src/xdg_popup.zig @@ -0,0 +1,55 @@ +const Self = @This(); + +const c = @import("c.zig"); +const std = @import("std"); + +const XdgToplevel = @import("xdg_toplevel.zig"); + +/// The toplevel this popup is a child of +xdg_toplevel: *XdgToplevel, + +/// The corresponding wlroots object +wlr_xdg_popup: *c.wlr_xdg_popup, + +listen_destroy: c.wl_listener, +listen_new_popup: c.wl_listener, + +pub fn init(self: *Self, xdg_toplevel: *XdgToplevel, wlr_xdg_popup: *c.wlr_xdg_popup) void { + self.xdg_toplevel = xdg_toplevel; + self.wlr_xdg_popup = wlr_xdg_popup; + + // The output box relative to the toplevel parent of the popup + const output = xdg_toplevel.view.output; + var box = c.wlr_output_layout_get_box(output.root.wlr_output_layout, output.wlr_output).*; + box.x -= xdg_toplevel.view.current_box.x; + box.y -= xdg_toplevel.view.current_box.y; + c.wlr_xdg_popup_unconstrain_from_box(wlr_xdg_popup, &box); + + // Setup listeners + self.listen_destroy.notify = handleDestroy; + c.wl_signal_add(&wlr_xdg_popup.base.*.events.destroy, &self.listen_destroy); + + self.listen_new_popup.notify = handleNewPopup; + c.wl_signal_add(&wlr_xdg_popup.base.*.events.new_popup, &self.listen_new_popup); +} + +fn handleDestroy(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { + const self = @fieldParentPtr(Self, "listen_destroy", listener.?); + const server = self.xdg_toplevel.view.output.root.server; + + c.wl_list_remove(&self.listen_destroy.link); + c.wl_list_remove(&self.listen_new_popup.link); + + server.allocator.destroy(self); +} + +/// Called when a new xdg popup is requested by the client +fn handleNewPopup(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { + const self = @fieldParentPtr(Self, "listen_new_popup", listener.?); + const wlr_xdg_popup = @ptrCast(*c.wlr_xdg_popup, @alignCast(@alignOf(*c.wlr_xdg_popup), data)); + const server = self.xdg_toplevel.view.output.root.server; + + // This will free itself on destroy + var xdg_popup = server.allocator.create(Self) catch unreachable; + xdg_popup.init(self.xdg_toplevel, wlr_xdg_popup); +} diff --git a/src/xdg_toplevel.zig b/src/xdg_toplevel.zig index 0e64396..0bdfa7d 100644 --- a/src/xdg_toplevel.zig +++ b/src/xdg_toplevel.zig @@ -7,6 +7,7 @@ const Box = @import("box.zig"); const Log = @import("log.zig").Log; const View = @import("view.zig").View; const ViewStack = @import("view_stack.zig").ViewStack; +const XdgPopup = @import("xdg_popup.zig"); /// The view this xdg toplevel implements view: *View, @@ -21,6 +22,7 @@ listen_unmap: c.wl_listener, // Listeners that are only active while the view is mapped listen_commit: c.wl_listener, +listen_new_popup: c.wl_listener, pub fn init(self: *Self, view: *View, wlr_xdg_surface: *c.wlr_xdg_surface) void { self.view = view; @@ -93,6 +95,9 @@ fn handleMap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { self.listen_commit.notify = handleCommit; c.wl_signal_add(&self.wlr_xdg_surface.surface.*.events.commit, &self.listen_commit); + self.listen_new_popup.notify = handleNewPopup; + c.wl_signal_add(&self.wlr_xdg_surface.events.new_popup, &self.listen_new_popup); + view.wlr_surface = self.wlr_xdg_surface.surface; view.floating = false; @@ -147,6 +152,7 @@ fn handleUnmap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { // Remove listeners that are only active while mapped c.wl_list_remove(&self.listen_commit.link); + c.wl_list_remove(&self.listen_new_popup.link); } /// Called when the surface is comitted @@ -162,3 +168,14 @@ fn handleCommit(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { } } } + +/// Called when a new xdg popup is requested by the client +fn handleNewPopup(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { + const self = @fieldParentPtr(Self, "listen_new_popup", listener.?); + const wlr_xdg_popup = @ptrCast(*c.wlr_xdg_popup, @alignCast(@alignOf(*c.wlr_xdg_popup), data)); + const server = self.view.output.root.server; + + // This will free itself on destroy + var xdg_popup = server.allocator.create(XdgPopup) catch unreachable; + xdg_popup.init(self, wlr_xdg_popup); +}