aboutsummaryrefslogtreecommitdiff
path: root/backend/drm
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2022-10-13 15:37:22 +0200
committerSimon Ser <contact@emersion.fr>2022-10-17 11:36:58 +0200
commitca432ea539614a1e321824c90d81f663f88298ef (patch)
tree27da682894ff7d1edc69ffa3404686b7c83eab25 /backend/drm
parenteeb7a8113869790e5f6fc643bafa577b08b5f4c3 (diff)
backend/drm: extract current mode logic into separate function
Extract the logic to fetch the current mode to a separate function to make it more readable. Stop dying in an assert when get_drm_prop_blob() fails. Always make it so the drmModeModeInfo pointer is allocated so that we can free() it unconditionally.
Diffstat (limited to 'backend/drm')
-rw-r--r--backend/drm/drm.c52
1 files changed, 32 insertions, 20 deletions
diff --git a/backend/drm/drm.c b/backend/drm/drm.c
index 2ebdbdc6..24b31c8b 100644
--- a/backend/drm/drm.c
+++ b/backend/drm/drm.c
@@ -1157,6 +1157,35 @@ static struct wlr_drm_crtc *connector_get_current_crtc(
return NULL;
}
+static drmModeModeInfo *connector_get_current_mode(
+ struct wlr_drm_connector *wlr_conn, const drmModeConnector *drm_conn) {
+ struct wlr_drm_backend *drm = wlr_conn->backend;
+
+ if (wlr_conn->crtc == NULL) {
+ return NULL;
+ }
+
+ if (wlr_conn->crtc->props.mode_id != 0) {
+ size_t size = 0;
+ drmModeModeInfo *mode = get_drm_prop_blob(drm->fd, wlr_conn->crtc->id,
+ wlr_conn->crtc->props.mode_id, &size);
+ assert(mode == NULL || size == sizeof(*mode));
+ return mode;
+ } else {
+ // Fallback to the legacy API
+ if (!wlr_conn->crtc->legacy_crtc->mode_valid) {
+ return NULL;
+ }
+ drmModeModeInfo *mode = malloc(sizeof(*mode));
+ if (mode == NULL) {
+ wlr_log_errno(WLR_ERROR, "Allocation failed");
+ return NULL;
+ }
+ *mode = wlr_conn->crtc->legacy_crtc->mode;
+ return mode;
+ }
+}
+
static void connect_drm_connector(struct wlr_drm_connector *wlr_conn,
const drmModeConnector *drm_conn) {
struct wlr_drm_backend *drm = wlr_conn->backend;
@@ -1225,21 +1254,8 @@ static void connect_drm_connector(struct wlr_drm_connector *wlr_conn,
// Before iterating on the conn's modes, get the current KMS mode
// in use from the connector's CRTC.
- drmModeModeInfo *current_modeinfo = NULL;
- if (wlr_conn->crtc != NULL) {
- if (wlr_conn->crtc->props.mode_id == 0) {
- // Use the legacy drm interface.
- if (wlr_conn->crtc->legacy_crtc->mode_valid) {
- current_modeinfo = &wlr_conn->crtc->legacy_crtc->mode;
- }
- } else {
- // Use the modern atomic drm interface.
- size_t modeinfo_size = 0;
- current_modeinfo = get_drm_prop_blob(drm->fd, wlr_conn->crtc->id,
- wlr_conn->crtc->props.mode_id, &modeinfo_size);
- assert(modeinfo_size == sizeof(drmModeModeInfo));
- }
- }
+ drmModeModeInfo *current_modeinfo =
+ connector_get_current_mode(wlr_conn, drm_conn);
wlr_log(WLR_INFO, "Detected modes:");
@@ -1277,11 +1293,7 @@ static void connect_drm_connector(struct wlr_drm_connector *wlr_conn,
wl_list_insert(wlr_conn->output.modes.prev, &mode->wlr_mode.link);
}
- if (wlr_conn->crtc != NULL && wlr_conn->crtc->props.mode_id != 0) {
- // free() the modeinfo pointer, but only
- // if not using the legacy API.
- free(current_modeinfo);
- }
+ free(current_modeinfo);
wlr_conn->possible_crtcs =
drmModeConnectorGetPossibleCrtcs(drm->fd, drm_conn);