summaryrefslogtreecommitdiff
path: root/src/screen/drm/resources.zig
blob: 9a8566c2c5f75e1862c2c95199bb1981336f6430 (plain)
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
const std = @import("std");
const os = std.os.linux;
const cerror = @import("../cerror.zig");

const drm = @import("root.zig");
const Card = drm.Card;

pub const Resources = struct {
	const Self = @This();
	const Range = struct { min: u32, max: u32 };

	card: *Card,

	fb_ids: []u32,
	crtc_ids: []u32,
	connector_ids: []u32,
	encoder_ids: []u32,
	width: Range,
	height: Range,

	pub fn raw_without_ids(card: *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: *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);
	}
};