diff --git a/backend/drm/drm-properties.c b/backend/drm/drm-properties.c new file mode 100644 index 00000000..3e089c31 --- /dev/null +++ b/backend/drm/drm-properties.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include +#include +#include +#include "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) }, +#undef INDEX +}; + +static const struct prop_info crtc_info[] = { +#define INDEX(name) (offsetof(union wlr_drm_crtc_props, name) / sizeof(uint32_t)) + { "rotation", INDEX(rotation) }, + { "scaling mode", INDEX(scaling_mode) }, +#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_ID", INDEX(fb_id) }, + { "SRC_H", INDEX(src_h) }, + { "SRC_W", INDEX(src_w) }, + { "SRC_X", INDEX(src_x) }, + { "SRC_Y", INDEX(src_y) }, + { "type", INDEX(type) }, +#undef INDEX +}; + +static int cmp_prop_info(const void *arg1, const void *arg2) { + const struct prop_info *a = arg1; + const struct prop_info *b = arg2; + + return strcmp(a->name, b->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(L_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(L_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 wlr_drm_get_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 wlr_drm_get_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 wlr_drm_get_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])); +} diff --git a/backend/drm/drm-properties.h b/backend/drm/drm-properties.h new file mode 100644 index 00000000..99791958 --- /dev/null +++ b/backend/drm/drm-properties.h @@ -0,0 +1,59 @@ +#ifndef DRM_PROPERTIES_H +#define DRM_PROPERTIES_H + +#include +#include + +/* + * These types contain the property ids for several DRM objects. + * See https://01.org/linuxgraphics/gfx-docs/drm/drm-kms-properties.html + * for more details. + */ + +union wlr_drm_connector_props { + struct { + uint32_t edid; + uint32_t dpms; + + // atomic-modesetting only + + uint32_t crtc_id; + }; + uint32_t props[3]; +}; + +union wlr_drm_crtc_props { + struct { + // Neither of these are guranteed to exist + uint32_t rotation; + uint32_t scaling_mode; + }; + uint32_t props[2]; +}; + +union wlr_drm_plane_props { + struct { + uint32_t type; + uint32_t rotation; // Not guranteed to exist + + // atomic-modesetting only + + uint32_t src_x; + uint32_t src_y; + uint32_t src_w; + uint32_t src_h; + uint32_t crtc_x; + uint32_t crtc_y; + uint32_t crtc_w; + uint32_t crtc_h; + uint32_t fb_id; + uint32_t crtc_id; + }; + uint32_t props[12]; +}; + +bool wlr_drm_get_connector_props(int fd, uint32_t id, union wlr_drm_connector_props *out); +bool wlr_drm_get_crtc_props(int fd, uint32_t id, union wlr_drm_crtc_props *out); +bool wlr_drm_get_plane_props(int fd, uint32_t id, union wlr_drm_plane_props *out); + +#endif