const std = @import("std"); const os = std.os.linux; const cerror = @import("../cerror.zig"); const drm = @import("root.zig"); pub const Resources = struct { const Self = @This(); const Range = struct { min: u32, max: u32 }; card: drm.Card, fb_ids: []u32, crtc_ids: []u32, connector_ids: []u32, encoder_ids: []u32, width: Range, height: Range, pub fn raw_without_ids(card: drm.Card) !drm.request.Resources { var result = std.mem.zeroes(drm.request.Resources); try card.request(.get_resources, &result); return result; } // NOTE: This function does not take in account // that there might be some hot-plugging going // on. This might have to change in the future. pub fn init(card: drm.Card) !Self { var raw = try Self.raw_without_ids(card); const resources = .{ .fb_ids = try card.allocator.alloc(u32, raw.count_fbs), .crtc_ids = try card.allocator.alloc(u32, raw.count_crtcs), .connector_ids = try card.allocator.alloc(u32, raw.count_connectors), .encoder_ids = try card.allocator.alloc(u32, raw.count_encoders), .width = .{ .min = raw.min_width, .max = raw.max_width }, .height = .{ .min = raw.min_height, .max = raw.max_height }, .card = card, }; @memset(resources.fb_ids, 0); @memset(resources.crtc_ids, 0); @memset(resources.connector_ids, 0); @memset(resources.encoder_ids, 0); raw.fb_ids = @ptrCast(resources.fb_ids); raw.crtc_ids = @ptrCast(resources.crtc_ids); raw.connector_ids = @ptrCast(resources.connector_ids); raw.encoder_ids = @ptrCast(resources.encoder_ids); try card.request(.get_resources, &raw); return resources; } pub fn deinit(self: *Self) void { self.card.allocator.free(self.fb_ids); self.card.allocator.free(self.crtc_ids); self.card.allocator.free(self.connector_ids); self.card.allocator.free(self.encoder_ids); } pub fn connector(self: *Self, index: usize) !drm.Connector { return drm.Connector.init(self.card, self.connector_ids[index]); } pub fn crtc(self: *Self, index: usize) !drm.Crtc { return drm.Crtc.init(self.card, self.crtc_ids[index]); } pub fn encoder(self: *Self, index: usize) !drm.Encoder { return drm.Encoder.init(self.card, self.encoder_ids[index]); } };