uterm: share drm dpms code
authorDavid Herrmann <dh.herrmann@googlemail.com>
Fri, 11 Jan 2013 16:45:48 +0000 (17:45 +0100)
committerDavid Herrmann <dh.herrmann@googlemail.com>
Fri, 11 Jan 2013 16:45:48 +0000 (17:45 +0100)
drm and dumb backends use the same DPMS code so share it.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
src/uterm_drm_shared.c
src/uterm_drm_shared_internal.h
src/uterm_video_drm.c
src/uterm_video_dumb.c

index bef6ff8..2c9c126 100644 (file)
@@ -93,3 +93,101 @@ const struct mode_ops uterm_drm_mode_ops = {
        .get_width = uterm_drm_mode_get_width,
        .get_height = uterm_drm_mode_get_height,
 };
+
+int uterm_drm_set_dpms(int fd, uint32_t conn_id, int state)
+{
+       int i, ret, set;
+       drmModeConnector *conn;
+       drmModePropertyRes *prop;
+
+       switch (state) {
+       case UTERM_DPMS_ON:
+               set = DRM_MODE_DPMS_ON;
+               break;
+       case UTERM_DPMS_STANDBY:
+               set = DRM_MODE_DPMS_STANDBY;
+               break;
+       case UTERM_DPMS_SUSPEND:
+               set = DRM_MODE_DPMS_SUSPEND;
+               break;
+       case UTERM_DPMS_OFF:
+               set = DRM_MODE_DPMS_OFF;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       conn = drmModeGetConnector(fd, conn_id);
+       if (!conn) {
+               log_err("cannot get display connector");
+               return -EFAULT;
+       }
+
+       ret = state;
+       for (i = 0; i < conn->count_props; ++i) {
+               prop = drmModeGetProperty(fd, conn->props[i]);
+               if (!prop) {
+                       log_error("cannot get DRM property (%d): %m", errno);
+                       continue;
+               }
+
+               if (!strcmp(prop->name, "DPMS")) {
+                       ret = drmModeConnectorSetProperty(fd, conn_id,
+                                                         prop->prop_id, set);
+                       if (ret) {
+                               log_info("cannot set DPMS");
+                               ret = -EFAULT;
+                       }
+                       drmModeFreeProperty(prop);
+                       break;
+               }
+               drmModeFreeProperty(prop);
+       }
+
+       if (i == conn->count_props) {
+               log_warn("display does not support DPMS");
+               ret = UTERM_DPMS_UNKNOWN;
+       }
+
+       drmModeFreeConnector(conn);
+       return ret;
+}
+
+int uterm_drm_get_dpms(int fd, drmModeConnector *conn)
+{
+       int i, ret;
+       drmModePropertyRes *prop;
+
+       for (i = 0; i < conn->count_props; ++i) {
+               prop = drmModeGetProperty(fd, conn->props[i]);
+               if (!prop) {
+                       log_error("cannot get DRM property (%d): %m", errno);
+                       continue;
+               }
+
+               if (!strcmp(prop->name, "DPMS")) {
+                       switch (conn->prop_values[i]) {
+                       case DRM_MODE_DPMS_ON:
+                               ret = UTERM_DPMS_ON;
+                               break;
+                       case DRM_MODE_DPMS_STANDBY:
+                               ret = UTERM_DPMS_STANDBY;
+                               break;
+                       case DRM_MODE_DPMS_SUSPEND:
+                               ret = UTERM_DPMS_SUSPEND;
+                               break;
+                       case DRM_MODE_DPMS_OFF:
+                       default:
+                               ret = UTERM_DPMS_OFF;
+                       }
+
+                       drmModeFreeProperty(prop);
+                       return ret;
+               }
+               drmModeFreeProperty(prop);
+       }
+
+       if (i == conn->count_props)
+               log_warn("display does not support DPMS");
+       return UTERM_DPMS_UNKNOWN;
+}
index 5e918d9..53d00ae 100644 (file)
@@ -34,6 +34,8 @@
 #include "uterm_video.h"
 #include "uterm_video_internal.h"
 
+/* drm mode */
+
 struct uterm_drm_mode {
        drmModeModeInfo info;
 };
@@ -54,4 +56,9 @@ static inline drmModeModeInfo *uterm_drm_mode_get_info(struct uterm_mode *m)
 
 extern const struct mode_ops uterm_drm_mode_ops;
 
+/* drm dpms */
+
+int uterm_drm_set_dpms(int fd, uint32_t conn_id, int state);
+int uterm_drm_get_dpms(int fd, drmModeConnector *conn);
+
 #endif /* UTERM_DRM_SHARED_INTERNAL_H */
index 9630081..1cfec3b 100644 (file)
@@ -306,69 +306,21 @@ static void display_deactivate(struct uterm_display *disp)
 
 static int display_set_dpms(struct uterm_display *disp, int state)
 {
-       int i, ret, set;
-       drmModeConnector *conn;
-       drmModePropertyRes *prop;
+       int ret;
 
        if (!display_is_conn(disp) || !video_is_awake(disp->video))
                return -EINVAL;
 
-       switch (state) {
-       case UTERM_DPMS_ON:
-               set = DRM_MODE_DPMS_ON;
-               break;
-       case UTERM_DPMS_STANDBY:
-               set = DRM_MODE_DPMS_STANDBY;
-               break;
-       case UTERM_DPMS_SUSPEND:
-               set = DRM_MODE_DPMS_SUSPEND;
-               break;
-       case UTERM_DPMS_OFF:
-               set = DRM_MODE_DPMS_OFF;
-               break;
-       default:
-               return -EINVAL;
-       }
-
        log_info("setting DPMS of display %p to %s", disp,
-                       uterm_dpms_to_name(state));
-
-       conn = drmModeGetConnector(disp->video->drm.fd, disp->drm.conn_id);
-       if (!conn) {
-               log_err("cannot get display connector");
-               return -EFAULT;
-       }
+                uterm_dpms_to_name(state));
 
-       ret = 0;
-       for (i = 0; i < conn->count_props; ++i) {
-               prop = drmModeGetProperty(disp->video->drm.fd, conn->props[i]);
-               if (!prop) {
-                       log_error("cannot get DRM property (%d): %m", errno);
-                       continue;
-               }
-
-               if (!strcmp(prop->name, "DPMS")) {
-                       ret = drmModeConnectorSetProperty(disp->video->drm.fd,
-                               disp->drm.conn_id, prop->prop_id, set);
-                       if (ret) {
-                               log_info("cannot set DPMS");
-                               ret = -EFAULT;
-                       }
-                       drmModeFreeProperty(prop);
-                       break;
-               }
-               drmModeFreeProperty(prop);
-       }
-
-       if (i == conn->count_props) {
-               ret = 0;
-               log_warn("display does not support DPMS");
-               state = UTERM_DPMS_UNKNOWN;
-       }
+       ret = uterm_drm_set_dpms(disp->video->drm.fd, disp->drm.conn_id,
+                                state);
+       if (ret < 0)
+               return ret;
 
-       drmModeFreeConnector(conn);
-       disp->dpms = state;
-       return ret;
+       disp->dpms = ret;
+       return 0;
 }
 
 static int display_use(struct uterm_display *disp)
@@ -984,45 +936,6 @@ static void show_displays(struct uterm_video *video)
        }
 }
 
-static int get_dpms(struct uterm_display *disp, drmModeConnector *conn)
-{
-       int i, ret;
-       drmModePropertyRes *prop;
-
-       for (i = 0; i < conn->count_props; ++i) {
-               prop = drmModeGetProperty(disp->video->drm.fd, conn->props[i]);
-               if (!prop) {
-                       log_error("cannot get DRM property (%d): %m", errno);
-                       continue;
-               }
-
-               if (!strcmp(prop->name, "DPMS")) {
-                       switch (conn->prop_values[i]) {
-                       case DRM_MODE_DPMS_ON:
-                               ret = UTERM_DPMS_ON;
-                               break;
-                       case DRM_MODE_DPMS_STANDBY:
-                               ret = UTERM_DPMS_STANDBY;
-                               break;
-                       case DRM_MODE_DPMS_SUSPEND:
-                               ret = UTERM_DPMS_SUSPEND;
-                               break;
-                       case DRM_MODE_DPMS_OFF:
-                       default:
-                               ret = UTERM_DPMS_OFF;
-                       }
-
-                       drmModeFreeProperty(prop);
-                       return ret;
-               }
-               drmModeFreeProperty(prop);
-       }
-
-       if (i == conn->count_props)
-               log_warn("display does not support DPMS");
-       return UTERM_DPMS_UNKNOWN;
-}
-
 static void bind_display(struct uterm_video *video, drmModeRes *res,
                                                        drmModeConnector *conn)
 {
@@ -1057,7 +970,7 @@ static void bind_display(struct uterm_video *video, drmModeRes *res,
        disp->flags |= DISPLAY_AVAILABLE;
        disp->next = video->displays;
        video->displays = disp;
-       disp->dpms = get_dpms(disp, conn);
+       disp->dpms = uterm_drm_get_dpms(disp->video->drm.fd, conn);
        log_info("display %p DPMS is %s", disp,
                        uterm_dpms_to_name(disp->dpms));
        VIDEO_CB(video, disp, UTERM_NEW);
index 3fba9ab..4333e85 100644 (file)
@@ -265,69 +265,21 @@ static void display_deactivate(struct uterm_display *disp)
 
 static int display_set_dpms(struct uterm_display *disp, int state)
 {
-       int i, ret, set;
-       drmModeConnector *conn;
-       drmModePropertyRes *prop;
+       int ret;
 
        if (!display_is_conn(disp) || !video_is_awake(disp->video))
                return -EINVAL;
 
-       switch (state) {
-       case UTERM_DPMS_ON:
-               set = DRM_MODE_DPMS_ON;
-               break;
-       case UTERM_DPMS_STANDBY:
-               set = DRM_MODE_DPMS_STANDBY;
-               break;
-       case UTERM_DPMS_SUSPEND:
-               set = DRM_MODE_DPMS_SUSPEND;
-               break;
-       case UTERM_DPMS_OFF:
-               set = DRM_MODE_DPMS_OFF;
-               break;
-       default:
-               return -EINVAL;
-       }
-
        log_info("setting DPMS of display %p to %s", disp,
-                       uterm_dpms_to_name(state));
-
-       conn = drmModeGetConnector(disp->video->dumb.fd, disp->dumb.conn_id);
-       if (!conn) {
-               log_err("cannot get display connector");
-               return -EFAULT;
-       }
-
-       ret = 0;
-       for (i = 0; i < conn->count_props; ++i) {
-               prop = drmModeGetProperty(disp->video->dumb.fd, conn->props[i]);
-               if (!prop) {
-                       log_error("cannot get DRM property (%d): %m", errno);
-                       continue;
-               }
-
-               if (!strcmp(prop->name, "DPMS")) {
-                       ret = drmModeConnectorSetProperty(disp->video->dumb.fd,
-                               disp->dumb.conn_id, prop->prop_id, set);
-                       if (ret) {
-                               log_info("cannot set DPMS");
-                               ret = -EFAULT;
-                       }
-                       drmModeFreeProperty(prop);
-                       break;
-               }
-               drmModeFreeProperty(prop);
-       }
+                uterm_dpms_to_name(state));
 
-       if (i == conn->count_props) {
-               ret = 0;
-               log_warn("display does not support DPMS");
-               state = UTERM_DPMS_UNKNOWN;
-       }
+       ret = uterm_drm_set_dpms(disp->video->dumb.fd, disp->dumb.conn_id,
+                                state);
+       if (ret < 0)
+               return ret;
 
-       drmModeFreeConnector(conn);
-       disp->dpms = state;
-       return ret;
+       disp->dpms = ret;
+       return 0;
 }
 
 static int display_swap(struct uterm_display *disp)
@@ -568,45 +520,6 @@ static void show_displays(struct uterm_video *video)
        }
 }
 
-static int get_dpms(struct uterm_display *disp, drmModeConnector *conn)
-{
-       int i, ret;
-       drmModePropertyRes *prop;
-
-       for (i = 0; i < conn->count_props; ++i) {
-               prop = drmModeGetProperty(disp->video->dumb.fd, conn->props[i]);
-               if (!prop) {
-                       log_error("cannot get DRM property (%d): %m", errno);
-                       continue;
-               }
-
-               if (!strcmp(prop->name, "DPMS")) {
-                       switch (conn->prop_values[i]) {
-                       case DRM_MODE_DPMS_ON:
-                               ret = UTERM_DPMS_ON;
-                               break;
-                       case DRM_MODE_DPMS_STANDBY:
-                               ret = UTERM_DPMS_STANDBY;
-                               break;
-                       case DRM_MODE_DPMS_SUSPEND:
-                               ret = UTERM_DPMS_SUSPEND;
-                               break;
-                       case DRM_MODE_DPMS_OFF:
-                       default:
-                               ret = UTERM_DPMS_OFF;
-                       }
-
-                       drmModeFreeProperty(prop);
-                       return ret;
-               }
-               drmModeFreeProperty(prop);
-       }
-
-       if (i == conn->count_props)
-               log_warn("display does not support DPMS");
-       return UTERM_DPMS_UNKNOWN;
-}
-
 static void bind_display(struct uterm_video *video, drmModeRes *res,
                                                        drmModeConnector *conn)
 {
@@ -641,7 +554,7 @@ static void bind_display(struct uterm_video *video, drmModeRes *res,
        disp->flags |= DISPLAY_AVAILABLE;
        disp->next = video->displays;
        video->displays = disp;
-       disp->dpms = get_dpms(disp, conn);
+       disp->dpms = uterm_drm_get_dpms(disp->video->dumb.fd, conn);
        log_info("display %p DPMS is %s", disp,
                        uterm_dpms_to_name(disp->dpms));
        VIDEO_CB(video, disp, UTERM_NEW);