aboutsummaryrefslogtreecommitdiff
path: root/backend/drm/properties.c
blob: 2717e09ea2f8ac5bbcaa5111fcbe20a79a4300d5 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#define _POSIX_C_SOURCE 200809L
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <wlr/util/log.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include "backend/drm/properties.h"

/*
 * Creates a mapping between property names and an array index where to store
 * the ids.  The prop_info arrays must be sorted by name, as bsearch is used to
 * search them.
 */
struct prop_info {
	const char *name;
	size_t index;
};

static const struct prop_info connector_info[] = {
#define INDEX(name) (offsetof(union wlr_drm_connector_props, name) / sizeof(uint32_t))
	{ "CRTC_ID", INDEX(crtc_id) },
	{ "DPMS", INDEX(dpms) },
	{ "EDID", INDEX(edid) },
	{ "PATH", INDEX(path) },
	{ "link-status", INDEX(link_status) },
	{ "non-desktop", INDEX(non_desktop) },
	{ "subconnector", INDEX(subconnector) },
	{ "vrr_capable", INDEX(vrr_capable) },
#undef INDEX
};

static const struct prop_info crtc_info[] = {
#define INDEX(name) (offsetof(union wlr_drm_crtc_props, name) / sizeof(uint32_t))
	{ "ACTIVE", INDEX(active) },
	{ "GAMMA_LUT", INDEX(gamma_lut) },
	{ "GAMMA_LUT_SIZE", INDEX(gamma_lut_size) },
	{ "MODE_ID", INDEX(mode_id) },
	{ "VRR_ENABLED", INDEX(vrr_enabled) },
#undef INDEX
};

static const struct prop_info plane_info[] = {
#define INDEX(name) (offsetof(union wlr_drm_plane_props, name) / sizeof(uint32_t))
	{ "CRTC_H", INDEX(crtc_h) },
	{ "CRTC_ID", INDEX(crtc_id) },
	{ "CRTC_W", INDEX(crtc_w) },
	{ "CRTC_X", INDEX(crtc_x) },
	{ "CRTC_Y", INDEX(crtc_y) },
	{ "FB_DAMAGE_CLIPS", INDEX(fb_damage_clips) },
	{ "FB_ID", INDEX(fb_id) },
	{ "IN_FORMATS", INDEX(in_formats) },
	{ "SRC_H", INDEX(src_h) },
	{ "SRC_W", INDEX(src_w) },
	{ "SRC_X", INDEX(src_x) },
	{ "SRC_Y", INDEX(src_y) },
	{ "rotation", INDEX(rotation) },
	{ "type", INDEX(type) },
#undef INDEX
};

static int cmp_prop_info(const void *arg1, const void *arg2) {
	const char *key = arg1;
	const struct prop_info *elem = arg2;

	return strcmp(key, elem->name);
}

static bool scan_properties(int fd, uint32_t id, uint32_t type, uint32_t *result,
		const struct prop_info *info, size_t info_len) {
	drmModeObjectProperties *props = drmModeObjectGetProperties(fd, id, type);
	if (!props) {
		wlr_log_errno(WLR_ERROR, "Failed to get DRM object properties");
		return false;
	}

	for (uint32_t i = 0; i < props->count_props; ++i) {
		drmModePropertyRes *prop = drmModeGetProperty(fd, props->props[i]);
		if (!prop) {
			wlr_log_errno(WLR_ERROR, "Failed to get DRM object property");
			continue;
		}

		const struct prop_info *p =
			bsearch(prop->name, info, info_len, sizeof(info[0]), cmp_prop_info);
		if (p) {
			result[p->index] = prop->prop_id;
		}

		drmModeFreeProperty(prop);
	}

	drmModeFreeObjectProperties(props);
	return true;
}

bool get_drm_connector_props(int fd, uint32_t id,
		union wlr_drm_connector_props *out) {
	return scan_properties(fd, id, DRM_MODE_OBJECT_CONNECTOR, out->props,
		connector_info, sizeof(connector_info) / sizeof(connector_info[0]));
}

bool get_drm_crtc_props(int fd, uint32_t id, union wlr_drm_crtc_props *out) {
	return scan_properties(fd, id, DRM_MODE_OBJECT_CRTC, out->props,
		crtc_info, sizeof(crtc_info) / sizeof(crtc_info[0]));
}

bool get_drm_plane_props(int fd, uint32_t id, union wlr_drm_plane_props *out) {
	return scan_properties(fd, id, DRM_MODE_OBJECT_PLANE, out->props,
		plane_info, sizeof(plane_info) / sizeof(plane_info[0]));
}

bool get_drm_prop(int fd, uint32_t obj, uint32_t prop, uint64_t *ret) {
	drmModeObjectProperties *props =
		drmModeObjectGetProperties(fd, obj, DRM_MODE_OBJECT_ANY);
	if (!props) {
		return false;
	}

	bool found = false;

	for (uint32_t i = 0; i < props->count_props; ++i) {
		if (props->props[i] == prop) {
			*ret = props->prop_values[i];
			found = true;
			break;
		}
	}

	drmModeFreeObjectProperties(props);
	return found;
}

void *get_drm_prop_blob(int fd, uint32_t obj, uint32_t prop, size_t *ret_len) {
	uint64_t blob_id;
	if (!get_drm_prop(fd, obj, prop, &blob_id)) {
		return NULL;
	}

	drmModePropertyBlobRes *blob = drmModeGetPropertyBlob(fd, blob_id);
	if (!blob) {
		return NULL;
	}

	void *ptr = malloc(blob->length);
	if (!ptr) {
		drmModeFreePropertyBlob(blob);
		return NULL;
	}

	memcpy(ptr, blob->data, blob->length);
	*ret_len = blob->length;

	drmModeFreePropertyBlob(blob);
	return ptr;
}

char *get_drm_prop_enum(int fd, uint32_t obj, uint32_t prop_id) {
	uint64_t value;
	if (!get_drm_prop(fd, obj, prop_id, &value)) {
		return NULL;
	}

	drmModePropertyRes *prop = drmModeGetProperty(fd, prop_id);
	if (!prop) {
		return NULL;
	}

	char *str = NULL;
	for (int i = 0; i < prop->count_enums; i++) {
		if (prop->enums[i].value == value) {
			str = strdup(prop->enums[i].name);
			break;
		}
	}

	drmModeFreeProperty(prop);

	return str;
}