From 02c8cf17bd3d426e5b7ff8eec1d0e32cf8607b0a Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 12 Mar 2014 06:42:59 +0100 Subject: translate stuff to functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/blueshift_drm_c.c | 196 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 151 insertions(+), 45 deletions(-) diff --git a/src/blueshift_drm_c.c b/src/blueshift_drm_c.c index be6a5e1..21cc741 100644 --- a/src/blueshift_drm_c.c +++ b/src/blueshift_drm_c.c @@ -43,6 +43,11 @@ static int drm_fd; */ static drmModeRes* drm_res = NULL; +/** + * Connector information + */ +static drmModeConnector* connector = NULL; + /** @@ -155,6 +160,119 @@ int blueshift_drm_gamma_size(int crtc_index) } +/** + * Acquire information about a connector + * + * @param connector_index The index of the connector + */ +void blueshift_drm_open_connector(int connector_index) +{ + connector = drmModeGetConnector(drm_fd, *(drm_res->connectors + connector_index)); +} + + +/** + * Release information about the connector + */ +void blueshift_drm_close_connector() +{ + drmModeFreeConnector(connector); +} + + +/* Accurate dimension on area not covered by the edges */ + +/** + * Get the physical width the monitor connected to the connector + * + * @return The physical width of the monitor in millimetres, 0 if unknown or not connected + */ +int blueshift_drm_get_width() +{ + return connector->mmWidth; +} + + +/** + * Get the physical height the monitor connected to the connector + * + * @return The physical height of the monitor in millimetres, 0 if unknown or not connected + */ +int blueshift_drm_get_height() +{ + return connector->mmHeight; +} + + +/** + * Get whether a monitor is connected to a connector + * + * @return 1 if there is a connection, 0 otherwise, -1 if unknown + */ +int blueshift_drm_is_connected() +{ + switch (connector->connection) + { + case DRM_MODE_CONNECTED: + return 1; + case DRM_MODE_DISCONNECTED: + return 0; + case DRM_MODE_UNKNOWNCONNECTION: + default: + return -1; + } +} + + +/** + * Get the index of the CRTC of the monitor connected to the connector + * + * @return The index of the CRTC + */ +int blueshift_drm_get_crtc() +{ + drmModeEncoder* encoder = drmModeGetEncoder(drm_fd, connector->encoder_id); + uint32_t crtc_id = encoder->crtc_id; + int crtc; + + drmModeFreeEncoder(encoder); + + for (crtc = 0; crtc < drm_res->count_crtcs; crtc++) + if (*(drm_res->crtcs + crtc) == crtc_id) + return crtc; + + return -1; +} + + +/** + * Get the index of the type of the connector + * + * @return The connector type by index, 0 for unknown + */ +int blueshift_drm_get_connector_type_index() +{ + return connector->connector_type; +} + + +/** + * Get the name of the type of the connector + * + * @return The connector type by name, "Unknown" if not identifiable, + * "Unrecognised" if Blueshift does not recognise it. + */ +const char* blueshift_drm_get_connector_type_name() +{ + static const char* TYPE_NAMES[] = { + "Unknown", "VGA", "DVII", "DVID", "DVIA", "Composite", "SVIDEO", "LVDS", "Component", + "9PinDIN", "DisplayPort", "HDMIA", "HDMIB", "TV", "eDP", "VIRTUAL", "DSI"}; + + int type = connector->connector_type; + return (size_t)type < sizeof(TYPE_NAMES) / sizeof(char*) ? TYPE_NAMES[type] : "Unrecognised"; +} + + int main(int argc, char** argv) { @@ -166,31 +284,17 @@ int main(int argc, char** argv) blueshift_drm_open(0); blueshift_drm_update(); + blueshift_drm_open_connector(3); printf("CRTC count: %i\n", blueshift_drm_crtc_count()); printf("Connector count: %i\n", blueshift_drm_connector_count()); printf("Gamma size: %i\n", blueshift_drm_gamma_size(0)); - - drmModeConnector* connector = drmModeGetConnector(drm_fd, *(drm_res->connectors + 3)); - printf("Physical size: %i mm by %i mm\n", connector->mmWidth, connector->mmHeight); - /* Accurate dimension on area not covered by the edges */ - printf("Connected: %i\n", connector->connection == DRM_MODE_CONNECTED); - /* DRM_MODE_DISCONNECTED DRM_MODE_UNKNOWNCONNECTION */ - if (connector->connection == DRM_MODE_CONNECTED) - { - drmModeEncoder* encoder = drmModeGetEncoder(drm_fd, connector->encoder_id); - uint32_t crtc_id = encoder->crtc_id; - int crtc; - - drmModeFreeEncoder(encoder); - for (crtc = 0; crtc < drm_res->count_crtcs; crtc++) - if (*(drm_res->crtcs + crtc) == crtc_id) - { - printf("CRTC: %i\n", crtc); - break; - } - - if (crtc < drm_res->count_crtcs) + printf("Physical size: %i mm by %i mm\n", blueshift_drm_get_width(), blueshift_drm_get_height()); + printf("Connected: %i\n", blueshift_drm_is_connected()); + if (blueshift_drm_is_connected() == 1) + {; + int crtc = blueshift_drm_get_crtc(); + if (crtc >= 0) { int gamma_size = blueshift_drm_gamma_size(crtc); uint16_t* red = alloca(gamma_size * sizeof(uint16_t)); @@ -198,11 +302,13 @@ int main(int argc, char** argv) uint16_t* blue = alloca(gamma_size * sizeof(uint16_t)); int j; + printf("CRTC: %i\n", crtc); + /* We need to initialise it to avoid valgrind warnings */ for (j = 0; j < gamma_size; j++) *(red + j) = *(green + j) = *(blue + j) = 0; - if (!drmModeCrtcGetGamma(drm_fd, crtc_id, gamma_size, red, green, blue)) + if (!drmModeCrtcGetGamma(drm_fd, *(drm_res->crtcs + crtc), gamma_size, red, green, blue)) { printf("Red:"); for (j = 0; j < gamma_size; j++) @@ -222,38 +328,38 @@ int main(int argc, char** argv) for (j = 0; j < gamma_size; j++) *(blue + j) /= 2; - printf("(%i)\n", drmModeCrtcSetGamma(drm_fd, crtc_id, gamma_size, red, green, blue)); + drmModeCrtcSetGamma(drm_fd, *(drm_res->crtcs + crtc), gamma_size, red, green, blue); /* TODO what more is required to set gamma ramps? */ } } - } - static const char* types[] = {"Unknown", "VGA", "DVII", "DVID", "DVIA", "Composite", "SVIDEO", "LVDS", - "Component", "9PinDIN", "DisplayPort", "HDMIA", "HDMIB", "TV", "eDP", - "VIRTUAL", "DSI"}; - printf("Type: %s (%i)\n", types[connector->connector_type], connector->connector_type); - int i; - for (i = 0; i < connector->count_props; i++) - { - drmModePropertyRes* prop; - prop = drmModeGetProperty(drm_fd, connector->props[i]); - if (!strcmp("EDID", prop->name)) + + int i; + for (i = 0; i < connector->count_props; i++) { - drmModePropertyBlobRes* blob = drmModeGetPropertyBlob(drm_fd, connector->prop_values[i]); - char* value = alloca((blob->length * 2 + 1) * sizeof(char)); - uint32_t j; - for (j = 0; j < blob->length; j++) + drmModePropertyRes* prop; + prop = drmModeGetProperty(drm_fd, connector->props[i]); + if (!strcmp("EDID", prop->name)) { - *(value + j * 2 + 0) = "0123456789abcdef"[(*((char*)(blob->data) + j) >> 4) & 15]; - *(value + j * 2 + 1) = "0123456789abcdef"[(*((char*)(blob->data) + j) >> 0) & 15]; + drmModePropertyBlobRes* blob = drmModeGetPropertyBlob(drm_fd, connector->prop_values[i]); + char* value = alloca((blob->length * 2 + 1) * sizeof(char)); + uint32_t j; + for (j = 0; j < blob->length; j++) + { + *(value + j * 2 + 0) = "0123456789abcdef"[(*((char*)(blob->data) + j) >> 4) & 15]; + *(value + j * 2 + 1) = "0123456789abcdef"[(*((char*)(blob->data) + j) >> 0) & 15]; + } + *(value + blob->length * 2) = 0; + printf("%s: %s\n", prop->name, value); + drmModeFreePropertyBlob(blob); } - *(value + blob->length * 2) = 0; - printf("%s: %s\n", prop->name, value); - drmModeFreePropertyBlob(blob); + drmModeFreeProperty(prop); } - drmModeFreeProperty(prop); } - drmModeFreeConnector(connector); + printf("Connector type: %s (%i)\n", + blueshift_drm_get_connector_type_name(), + blueshift_drm_get_connector_type_index()); + blueshift_drm_close_connector(); blueshift_drm_close(); return 0; -- cgit v1.2.3-70-g09d2