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