Skip to content

Commit

Permalink
[refactor] fix memory leaks, rendering, and xkb context
Browse files Browse the repository at this point in the history
Signed-off-by: Shinyzenith <aakashsensharma@gmail.com>
  • Loading branch information
Shinyzenith committed Jul 14, 2023
1 parent f83c58f commit f884d74
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 21 deletions.
7 changes: 3 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
path = deps/zig-wayland
url = https://github.com/ifreund/zig-wayland
ignore = dirty
[submodule "deps/zig-pixman"]
path = deps/zig-pixman
url = https://github.com/ifreund/zig-pixman
ignore = dirty
[submodule "deps/zig-xkbcommon"]
path = deps/zig-xkbcommon
url = https://github.com/ifreund/zig-xkbcommon
Expand All @@ -22,3 +18,6 @@
path = deps/zig-wlroots
url = https://github.com/swaywm/zig-wlroots.git
ignore = dirty
[submodule "deps/zig-pixman"]
path = deps/zig-pixman
url = https://github.com/ifreund/zig-pixman
2 changes: 1 addition & 1 deletion deps/zig-wayland
10 changes: 4 additions & 6 deletions next/desktop/Output.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ const Server = @import("../Server.zig");
server: *Server,

wlr_output: *wlr.Output,
damage: *wlr.OutputDamage,

frame: wl.Listener(*wlr.OutputDamage) = wl.Listener(*wlr.OutputDamage).init(handleFrame),
frame: wl.Listener(*wlr.Output) = wl.Listener(*wlr.Output).init(handleFrame),
destroy: wl.Listener(*wlr.Output) = wl.Listener(*wlr.Output).init(handleDestroy),

// This callback prepares the output object to accept listeners.
Expand Down Expand Up @@ -58,13 +57,12 @@ pub fn init(self: *Self, wlr_output: *wlr.Output) void {
self.* = .{
.server = server,
.wlr_output = wlr_output,
.damage = wlr.OutputDamage.create(wlr_output) catch return,
};

self.wlr_output.data = @ptrToInt(&self);

// Add a callback for the frame event from the output struct.
self.damage.events.frame.add(&self.frame);
self.wlr_output.events.frame.add(&self.frame);

// Add the new output to the output_layout for automatic layout management by wlroots.
self.server.output_layout.wlr_output_layout.addAuto(self.wlr_output);
Expand All @@ -87,9 +85,9 @@ pub fn init(self: *Self, wlr_output: *wlr.Output) void {
}

// This callback is called everytime an output is ready to display a frame.
fn handleFrame(listener: *wl.Listener(*wlr.OutputDamage), _: *wlr.OutputDamage) void { // Get the parent struct, Output.
fn handleFrame(listener: *wl.Listener(*wlr.Output), _: *wlr.Output) void { // Get the parent struct, Output.
const self = @fieldParentPtr(Self, "frame", listener);
log.debug("Signal: wlr_output_damage_frame", .{});
log.debug("Signal: wlr_output_frame", .{});

// Get the scene output with respect to the wlr.Output object that's being passed.
const scene_output = self.server.wlr_scene.getSceneOutput(self.wlr_output).?;
Expand Down
2 changes: 1 addition & 1 deletion next/desktop/Window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn handleDestroy(self: *Self) void {
for (xdg_toplevel.borders) |border| {
border.node.destroy();
}
xdg_toplevel.scene.node.destroy();
xdg_toplevel.scene_tree.node.destroy();
},
}
if (std.mem.indexOfScalar(*Self, self.server.mapped_windows.items, self)) |i| {
Expand Down
12 changes: 6 additions & 6 deletions next/desktop/XdgToplevel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ popups: std.ArrayListUnmanaged(*XdgPopup) = .{},

geometry: wlr.Box = undefined,
draw_borders: bool = true,
scene: *wlr.SceneTree = undefined,
scene_tree: *wlr.SceneTree = undefined,
scene_surface: *wlr.SceneTree = undefined,

map: wl.Listener(void) = wl.Listener(void).init(handleMap),
Expand Down Expand Up @@ -78,19 +78,19 @@ pub fn handleMap(listener: *wl.Listener(void)) void {
// TODO: Check if view wants to be fullscreen then make it fullscreen.

// Setting some struct fields we will need later
self.scene = server.layer_tile.createSceneTree() catch return;
self.scene.node.setEnabled(false);
self.scene_tree = server.layer_tile.createSceneTree() catch return;
self.scene_tree.node.setEnabled(true);

// TODO: This should handle our resize logic primarily
//xdg_surface.surface.events.commit.add();

self.scene_surface = self.scene.createSceneXdgSurface(self.xdg_surface) catch return;
self.scene_surface = self.scene_tree.createSceneXdgSurface(self.xdg_surface) catch return;
self.xdg_surface.getGeometry(&self.geometry);

// Looping over 4 times to create the top, bottom, left, and right borders.
comptime var j: usize = 0;
inline while (j <= 3) : (j += 1) {
self.borders[j] = self.scene.createSceneRect(0, 0, &server.config.border_color) catch return;
self.borders[j] = self.scene_tree.createSceneRect(0, 0, &server.config.border_color) catch return;
}

// If the client can have csd then why draw servide side borders?
Expand Down Expand Up @@ -188,7 +188,7 @@ pub fn resize(self: *Self, x: c_int, y: c_int, width: c_int, height: c_int) void
self.borders[3].setSize(border_width, self.geometry.height - 2 * border_width);
self.borders[3].node.setPosition(self.geometry.width - border_width, border_width);
}
self.scene.node.setPosition(self.geometry.x, self.geometry.y);
self.scene_tree.node.setPosition(self.geometry.x, self.geometry.y);
self.scene_surface.node.setPosition(border_width, border_width);

self.updateSize(width, height);
Expand Down
6 changes: 5 additions & 1 deletion next/input/InputManager.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ fn newInput(listener: *wl.Listener(*wlr.InputDevice), input_device: *wlr.InputDe
};
errdefer allocator.destroy(keyboard);

keyboard.init(input_device);
keyboard.init(input_device) catch |err| {
log.err("Failed to initialize keyboard device: {s}", .{@errorName(err)});
allocator.destroy(keyboard);
return;
};
},
.pointer => {
const pointer = allocator.create(Cursor) catch {
Expand Down
15 changes: 14 additions & 1 deletion next/input/Keyboard.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ modifiers: wl.Listener(*wlr.Keyboard) = wl.Listener(*wlr.Keyboard).init(handleMo

destroy: wl.Listener(*wlr.InputDevice) = wl.Listener(*wlr.InputDevice).init(handleDestroy),

pub fn init(self: *Self, device: *wlr.InputDevice) void {
pub fn init(self: *Self, device: *wlr.InputDevice) !void {
log.debug("Initializing keyboard device", .{});
self.* = .{
.server = server,
Expand All @@ -40,8 +40,17 @@ pub fn init(self: *Self, device: *wlr.InputDevice) void {
return;
};

const context = xkb.Context.new(.no_flags) orelse return error.XkbContextNewFailed;
defer context.unref();

const keymap = xkb.Keymap.newFromNames(context, null, .no_flags) orelse return error.XkbKeymapCreationFailed;
defer keymap.unref();

_ = self.wlr_keyboard.setKeymap(keymap);
self.wlr_keyboard.setRepeatInfo(self.server.config.repeat_rate, self.server.config.repeat_delay);

self.server.seat.wlr_seat.setKeyboard(self.wlr_keyboard);

self.wlr_keyboard.events.key.add(&self.key);
self.wlr_input_device.events.destroy.add(&self.destroy);
}
Expand Down Expand Up @@ -106,6 +115,10 @@ fn handleCompositorBindings(keysym: xkb.Keysym) bool {
}
return true;
},
xkb.Keysym.j => {
server.wl_server.terminate();
return true;
},
else => return false,
}
}
1 change: 1 addition & 0 deletions next/utils/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub usingnamespace @cImport({
@cDefine("_POSIX_C_SOURCE", "200809L");
@cInclude("stdlib.h");
@cInclude("unistd.h");
@cInclude("libinput.h");
});

0 comments on commit f884d74

Please sign in to comment.