Skip to content

Commit

Permalink
fix: fix compile error on GTK backend's Monitor.getInternalName and p…
Browse files Browse the repository at this point in the history
…roperly deinit
  • Loading branch information
zenith391 committed Jul 17, 2024
1 parent 6f9228f commit 064b9b9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
14 changes: 12 additions & 2 deletions src/backends/gtk/Monitor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ pub fn getList() []Monitor {
}
}

pub fn deinitAllPeers() void {
if (monitor_list) |list| {
for (list) |*monitor| monitor.deinit();
lib.internal.lasting_allocator.free(list);
monitor_list = null;
}
}

pub fn getName(self: *Monitor) []const u8 {
// TODO: detect if GTK version is >= 4.10 and use c.gdk_monitor_get_description if so is the case.
return std.mem.span(c.gdk_monitor_get_connector(self.peer));
Expand All @@ -37,9 +45,10 @@ pub fn getInternalName(self: *Monitor) []const u8 {
return internal_name;
} else {
self.internal_name = std.mem.concat(lib.internal.lasting_allocator, u8, &.{
std.mem.span(c.gdk_monitor_get_manufacturer(self.peer) orelse ""),
std.mem.span(c.gdk_monitor_get_model(self.peer) orelse ""),
std.mem.span(c.gdk_monitor_get_manufacturer(self.peer) orelse @as([:0]const u8, "").ptr),
std.mem.span(c.gdk_monitor_get_model(self.peer) orelse @as([:0]const u8, "").ptr),
}) catch @panic("OOM");
return self.internal_name.?;
}
}

Expand Down Expand Up @@ -95,5 +104,6 @@ pub fn getVideoMode(self: *Monitor, index: usize) lib.VideoMode {
pub fn deinit(self: *Monitor) void {
if (self.internal_name) |internal_name| {
lib.internal.lasting_allocator.free(internal_name);
self.internal_name = null;
}
}
8 changes: 8 additions & 0 deletions src/backends/win32/Monitor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ pub fn getList() []Monitor {
}
}

pub fn deinitAllPeers() void {
if (monitor_list) |list| {
for (list) |*monitor| monitor.deinit();
lib.internal.lasting_allocator.free(list);
monitor_list = null;
}
}

pub fn getName(self: *Monitor) []const u8 {
return self.device_name;
}
Expand Down
11 changes: 9 additions & 2 deletions src/data.zig
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ pub fn ListAtom(comptime T: type) type {
return struct {
backing_list: ListType,
length: Atom(usize),
// TODO: since RwLock doesn't report deadlocks in Debug mode like Mutex does, do it manually here in ListAtom
lock: std.Thread.RwLock = .{},
allocator: std.mem.Allocator,

Expand Down Expand Up @@ -759,6 +760,12 @@ pub fn ListAtom(comptime T: type) type {
};
}

pub fn map(self: *Self, comptime U: type, func: *const fn (T) U) *ListAtom(U) {
_ = self;
_ = func;
return undefined;
}

pub fn deinit(self: *Self) void {
self.lock.lock();
defer self.lock.unlock();
Expand All @@ -768,8 +775,8 @@ pub fn ListAtom(comptime T: type) type {
};
}

test "list atom" {
var list = ListAtom(u32).init();
test ListAtom {
var list = ListAtom(u32).init(std.testing.allocator);
defer list.deinit();

try list.append(1);
Expand Down
33 changes: 23 additions & 10 deletions src/monitor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,32 @@ pub const Monitors = struct {

/// This function is called by `capy.deinit()`, so it doesn't need to call it manually.
pub fn deinit() void {
var iterator = Monitors.list.iterate();
defer iterator.deinit();
while (iterator.next()) |monitor| {
monitor.deinit();
{
var iterator = Monitors.list.iterate();
defer iterator.deinit();
while (iterator.next()) |monitor| {
monitor.deinit();
}
}

Monitors.list.deinit();
Monitors.list = ListAtom(Monitor).init(internal.lasting_allocator);
backend.Monitor.deinitAllPeers();
}
};

test "Monitors" {
var iterator = Monitors.list.iterate();
defer iterator.deinit();
// NOTE: You don't need to initialize the API in user code. This is only for the testing environment.
Monitors.init();
defer Monitors.deinit();

std.log.info("Monitor(s):", .{});
while (iterator.next()) |monitor| {
std.log.info(" - Name: {s}", .{monitor.getName()});
{
var iterator = Monitors.list.iterate();
defer iterator.deinit();

std.log.info("Monitor(s):", .{});
while (iterator.next()) |monitor| {
std.log.info(" - Name: {s}", .{monitor.getName()});
}
}
}

Expand Down Expand Up @@ -113,6 +122,10 @@ pub const Monitor = struct {
}

test getSize {
// NOTE: You don't need to initialize the API in user code. This is only for the testing environment.
Monitors.init();
defer Monitors.deinit();

const monitor = Monitors.list.get(0);
const width, const height = monitor.getSize();
std.log.info("Monitor pixels: {d} px x {d} px", .{ width, height });
Expand Down

0 comments on commit 064b9b9

Please sign in to comment.