-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProvider.zig
More file actions
81 lines (65 loc) · 2.03 KB
/
Provider.zig
File metadata and controls
81 lines (65 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const std = @import("std");
const Allocator = std.mem.Allocator;
const libdrm = @import("libdrm");
const Provider = @import("../../Provider.zig");
const Device = @import("../../Device.zig");
const LinuxDrmDevice = @import("Device.zig");
const Self = @This();
allocator: Allocator,
base: Provider,
pub fn create(alloc: Allocator) !*Provider {
const self = try alloc.create(Self);
errdefer alloc.destroy(self);
self.* = .{
.allocator = alloc,
.base = .{
.ptr = self,
.vtable = &.{
.getDevices = impl_getDevices,
.destroy = impl_destroy,
},
},
};
return &self.base;
}
pub fn getDevices(self: *Self) anyerror![]const *Device {
var list = std.ArrayList(*Device).init(self.allocator);
defer list.deinit();
var iter = libdrm.Node.Iterator.init(self.allocator, .primary);
while (iter.next()) |node| {
const device = try LinuxDrmDevice.create(self.allocator, node);
errdefer device.destroy();
try list.append(device);
}
return try list.toOwnedSlice();
}
pub fn destroy(self: *Self) void {
self.allocator.destroy(self);
}
fn impl_getDevices(ptr: *anyopaque) anyerror![]const *Device {
const self: *Self = @alignCast(@ptrCast(ptr));
return self.getDevices();
}
fn impl_destroy(ptr: *anyopaque) void {
const self: *Self = @alignCast(@ptrCast(ptr));
return self.destroy();
}
test {
const provider = try create(std.testing.allocator);
defer provider.destroy();
const devices = try provider.getDevices();
defer {
for (devices) |dev| dev.destroy();
std.testing.allocator.free(devices);
}
if (devices.len == 0) return error.SkipZigTest;
std.debug.print("{any}\n", .{devices});
for (devices) |dev| {
const connectors = dev.getConnectors() catch continue;
defer {
for (connectors) |conn| conn.destroy();
std.testing.allocator.free(connectors);
}
std.debug.print("{any}\n", .{connectors});
}
}