river/river/SceneNodeData.zig

80 lines
2.5 KiB
Zig
Raw Normal View History

// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 2023 The River Developers
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const SceneNodeData = @This();
const build_options = @import("build_options");
const wlr = @import("wlroots");
const wl = @import("wayland").server.wl;
const util = @import("util.zig");
const LayerSurface = @import("LayerSurface.zig");
2023-01-31 06:47:19 -08:00
const LockSurface = @import("LockSurface.zig");
const View = @import("View.zig");
2023-02-20 09:01:24 -08:00
const XwaylandOverrideRedirect = @import("XwaylandOverrideRedirect.zig");
pub const Data = union(enum) {
view: *View,
2023-01-31 06:47:19 -08:00
lock_surface: *LockSurface,
layer_surface: *LayerSurface,
2023-02-20 09:01:24 -08:00
xwayland_override_redirect: if (build_options.xwayland) *XwaylandOverrideRedirect else noreturn,
};
node: *wlr.SceneNode,
data: Data,
destroy: wl.Listener(void) = wl.Listener(void).init(handleDestroy),
pub fn attach(node: *wlr.SceneNode, data: Data) error{OutOfMemory}!void {
const scene_node_data = try util.gpa.create(SceneNodeData);
scene_node_data.* = .{
.node = node,
.data = data,
};
node.data = @ptrToInt(scene_node_data);
node.events.destroy.add(&scene_node_data.destroy);
}
pub fn fromNode(node: *wlr.SceneNode) ?*SceneNodeData {
var it: ?*wlr.SceneNode = node;
while (it) |n| : (it = n.parent) {
if (@intToPtr(?*SceneNodeData, n.data)) |scene_node_data| {
return scene_node_data;
}
}
return null;
}
pub fn fromSurface(surface: *wlr.Surface) ?*SceneNodeData {
if (surface.getRootSurface()) |root_surface| {
if (@intToPtr(?*wlr.SceneNode, root_surface.data)) |node| {
return fromNode(node);
}
}
return null;
}
fn handleDestroy(listener: *wl.Listener(void)) void {
const scene_node_data = @fieldParentPtr(SceneNodeData, "destroy", listener);
scene_node_data.destroy.link.remove();
scene_node_data.node.data = 0;
util.gpa.destroy(scene_node_data);
}