aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/gamma-linux-drm.c109
-rw-r--r--src/lib/gamma-x-randr.c53
2 files changed, 85 insertions, 77 deletions
diff --git a/src/lib/gamma-linux-drm.c b/src/lib/gamma-linux-drm.c
index b97f94d..dba98b1 100644
--- a/src/lib/gamma-linux-drm.c
+++ b/src/lib/gamma-linux-drm.c
@@ -315,17 +315,21 @@ int libgamma_linux_drm_partition_initialise(libgamma_partition_state_t* restrict
static void release_connectors_and_encoders(libgamma_drm_card_data_t* restrict this)
{
size_t i, n;
+ /* Release individual encoders. */
if (this->encoders != NULL)
for (i = 0, n = (size_t)(this->res->count_connectors); i < n; i++)
if (this->encoders[i] != NULL)
drmModeFreeEncoder(this->encoders[i]);
+ /* Release encoder array. */
free(this->encoders);
this->encoders = NULL;
+ /* Release individual connectors. */
if (this->connectors != NULL)
for (i = 0, n = (size_t)(this->res->count_connectors); i < n; i++)
if (this->connectors[i] != NULL)
drmModeFreeConnector(this->connectors[i]);
+ /* Release connector array. */
free(this->connectors);
this->connectors = NULL;
}
@@ -340,10 +344,8 @@ void libgamma_linux_drm_partition_destroy(libgamma_partition_state_t* restrict t
{
libgamma_drm_card_data_t* restrict data = this->data;
release_connectors_and_encoders(data);
- if (data->res != NULL)
- drmModeFreeResources(data->res);
- if (data->fd >= 0)
- close(data->fd);
+ if (data->res != NULL) drmModeFreeResources(data->res);
+ if (data->fd >= 0) close(data->fd);
free(data);
}
@@ -375,15 +377,12 @@ int libgamma_linux_drm_partition_restore(libgamma_partition_state_t* restrict th
int libgamma_linux_drm_crtc_initialise(libgamma_crtc_state_t* restrict this,
libgamma_partition_state_t* restrict partition, size_t crtc)
{
- libgamma_drm_card_data_t* restrict card;
+ libgamma_drm_card_data_t* restrict card = partition->data;
uint32_t crtc_id;
if (crtc >= partition->crtcs_available)
return LIBGAMMA_NO_SUCH_CRTC;
-
- card = partition->data;
- crtc_id = card->res->crtcs[crtc];
- this->data = (void*)(size_t)crtc_id;
+ this->data = (void*)(size_t)(card->res->crtcs[crtc]);
return 0;
}
@@ -429,25 +428,29 @@ static drmModeConnector* find_connector(libgamma_crtc_state_t* restrict this, in
/* Open connectors and encoders if not already opened. */
if (card->connectors == NULL)
{
- /* We use calloc so all non-loaded elements are `NULL` after an error. */
- if ((card->connectors = calloc(n, sizeof(drmModeConnector*))) == NULL)
- goto fail;
- if ((card->encoders = calloc(n, sizeof(drmModeEncoder*))) == NULL)
- goto fail;
+ /* Allocate connector and encoder arrays.
+ We use `calloc` so all non-loaded elements are `NULL` after an error. */
+ if ((card->connectors = calloc(n, sizeof(drmModeConnector*))) == NULL) goto fail;
+ if ((card->encoders = calloc(n, sizeof(drmModeEncoder*))) == NULL) goto fail;
+ /* Fill connector and encoder arrays. */
for (i = 0; i < n; i++)
if (((card->connectors[i] = drmModeGetConnector(card->fd, card->res->connectors[i])) == NULL) ||
((card->encoders[i] = drmModeGetEncoder(card->fd, card->connectors[i]->encoder_id)) == NULL))
goto fail;
}
- /* Find connector. */
+ /* No error has occurred yet. */
*error = 0;
+ /* Find connector. */
for (i = 0; i < n; i++)
if (card->encoders[i]->crtc_id == crtc_id)
return card->connectors[i];
+ /* We did not find the connector. */
*error = LIBGAMMA_CONNECTOR_UNKNOWN;
return NULL;
fail:
+ /* Report the error that got us here, release
+ resouces and exit with `NULL` for failure. */
*error = errno;
release_connectors_and_encoders(card);
return NULL;
@@ -471,9 +474,7 @@ static int get_gamma_ramp_size(libgamma_crtc_information_t* restrict out, const
out->gamma_size_error = crtc_info == NULL ? errno : 0;
if (out->gamma_size_error == 0)
{
- out->red_gamma_size = (size_t)(crtc_info->gamma_size);
- out->green_gamma_size = (size_t)(crtc_info->gamma_size);
- out->blue_gamma_size = (size_t)(crtc_info->gamma_size);
+ out->red_gamma_size = out->green_gamma_size = out->blue_gamma_size = (size_t)(crtc_info->gamma_size);
out->gamma_size_error = crtc_info->gamma_size < 2 ? LIBGAMMA_SINGLETON_GAMMA_RAMP : 0;
drmModeFreeCrtc(crtc_info);
}
@@ -495,18 +496,18 @@ static void get_subpixel_order(libgamma_crtc_information_t* restrict out,
out->subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_##value; \
break
- switch (connector->subpixel)
- {
- __select (UNKNOWN);
- __select (HORIZONTAL_RGB);
- __select (HORIZONTAL_BGR);
- __select (VERTICAL_RGB);
- __select (VERTICAL_BGR);
- __select (NONE);
- default:
- out->subpixel_order_error = LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED;
- break;
- }
+ switch (connector->subpixel)
+ {
+ __select (UNKNOWN);
+ __select (HORIZONTAL_RGB);
+ __select (HORIZONTAL_BGR);
+ __select (VERTICAL_RGB);
+ __select (VERTICAL_BGR);
+ __select (NONE);
+ default:
+ out->subpixel_order_error = LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED;
+ break;
+ }
#undef __select
}
@@ -529,36 +530,36 @@ static void get_connector_type(libgamma_crtc_information_t* restrict out,
*connector_name_base = name; \
break
- switch (connector->connector_type)
- {
+ switch (connector->connector_type)
+ {
#ifndef DRM_MODE_CONNECTOR_VIRTUAL
# define DRM_MODE_CONNECTOR_VIRTUAL 15
#endif
#ifndef DRM_MODE_CONNECTOR_DSI
# define DRM_MODE_CONNECTOR_DSI 16
#endif
- __select (Unknown, "Unknown" );
- __select (VGA, "VGA" );
- __select (DVII, "DVI-I" );
- __select (DVID, "DVI-D" );
- __select (DVIA, "DVI-A" );
- __select (Composite, "Composite");
- __select (SVIDEO, "SVIDEO" );
- __select (LVDS, "LVDS" );
- __select (Component, "Component");
- __select (9PinDIN, "DIN" );
- __select (DisplayPort, "DP" );
- __select (HDMIA, "HDMI-A" );
- __select (HDMIB, "HDMI-B" );
- __select (TV, "TV" );
- __select (eDP, "eDP" );
- __select (VIRTUAL, "VIRTUAL" );
- __select (DSI, "DSI" );
- default:
- out->connector_type_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED;
- out->connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED;
- break;
- }
+ __select (Unknown, "Unknown" );
+ __select (VGA, "VGA" );
+ __select (DVII, "DVI-I" );
+ __select (DVID, "DVI-D" );
+ __select (DVIA, "DVI-A" );
+ __select (Composite, "Composite");
+ __select (SVIDEO, "SVIDEO" );
+ __select (LVDS, "LVDS" );
+ __select (Component, "Component");
+ __select (9PinDIN, "DIN" );
+ __select (DisplayPort, "DP" );
+ __select (HDMIA, "HDMI-A" );
+ __select (HDMIB, "HDMI-B" );
+ __select (TV, "TV" );
+ __select (eDP, "eDP" );
+ __select (VIRTUAL, "VIRTUAL" );
+ __select (DSI, "DSI" );
+ default:
+ out->connector_type_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED;
+ out->connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED;
+ break;
+ }
#undef __select
}
diff --git a/src/lib/gamma-x-randr.c b/src/lib/gamma-x-randr.c
index 099dc05..b9139fd 100644
--- a/src/lib/gamma-x-randr.c
+++ b/src/lib/gamma-x-randr.c
@@ -506,14 +506,18 @@ static int get_gamma_ramp_size(libgamma_crtc_information_t* restrict out, libgam
xcb_randr_get_crtc_gamma_size_reply_t* restrict reply;
xcb_generic_error_t* error;
+ /* Query gamma ramp size. */
out->gamma_size_error = 0;
cookie = xcb_randr_get_crtc_gamma_size(connection, *crtc_id);
reply = xcb_randr_get_crtc_gamma_size_reply(connection, cookie, &error);
if (error != NULL)
return out->gamma_size_error = translate_error(error->error_code, LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED, 1);
+ /* Sanity check gamma ramp size. */
if (reply->size < 2)
out->gamma_size_error = LIBGAMMA_SINGLETON_GAMMA_RAMP;
+ /* Store gamma ramp size. */
out->red_gamma_size = out->green_gamma_size = out->blue_gamma_size = reply->size;
+ /* Release resources and return successfulnes. */
free(reply);
return out->gamma_size_error;
}
@@ -528,14 +532,16 @@ static int get_gamma_ramp_size(libgamma_crtc_information_t* restrict out, libgam
*/
static int read_output_data(libgamma_crtc_information_t* restrict out, xcb_randr_get_output_info_reply_t* restrict output)
{
+#define __select(value) \
+ case XCB_RENDER_SUB_PIXEL_##value: out->subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_##value; break
+
switch (output->connection)
{
case XCB_RANDR_CONNECTION_CONNECTED:
+ /* We have a monitor connected, report that and store information that is provided to us. */
out->active = 1;
out->width_mm = output->mm_width;
out->height_mm = output->mm_height;
-#define __select(value) \
- case XCB_RENDER_SUB_PIXEL_##value: out->subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_##value; break
switch (output->subpixel_order)
{
__select (UNKNOWN);
@@ -548,24 +554,24 @@ static int read_output_data(libgamma_crtc_information_t* restrict out, xcb_randr
out->subpixel_order_error = LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED;
break;
}
-#undef __select
- return 0;
-
- case XCB_RANDR_CONNECTION_DISCONNECTED:
- out->active = 0;
- out->width_mm_error = LIBGAMMA_NOT_CONNECTED;
- out->height_mm_error = LIBGAMMA_NOT_CONNECTED;
- out->subpixel_order_error = LIBGAMMA_NOT_CONNECTED;
return 0;
- default:
- out->active = 0;
+ case XCB_RANDR_CONNECTION_UNKNOWN:
+ /* If we do know whether a monitor is connected report that and assume it is not. */
out->active_error = LIBGAMMA_STATE_UNKNOWN;
- out->width_mm_error = LIBGAMMA_NOT_CONNECTED;
- out->height_mm_error = LIBGAMMA_NOT_CONNECTED;
+ /* Fall through. */
+ default:
+ /* If no monitor is connected, report that on fails that require it. */
+ out->width_mm_error = LIBGAMMA_NOT_CONNECTED;
+ out->height_mm_error = LIBGAMMA_NOT_CONNECTED;
out->subpixel_order_error = LIBGAMMA_NOT_CONNECTED;
- return -1;
+ /* And store that we are not connected. */
+ out->active = 0;
+ /* This fuction only failed if we could not figure out whether a monitor is connected. */
+ return (output->connection == XCB_RANDR_CONNECTION_UNKNOWN) ? -1 : 0;
}
+
+#undef __select
}
@@ -577,6 +583,9 @@ static int read_output_data(libgamma_crtc_information_t* restrict out, xcb_randr
*/
static int get_connector_type(libgamma_crtc_information_t* restrict this)
{
+ /* Since we require the name of the output of get the type of the connected,
+ copy any reported error on the output's name to the connector's type,
+ and report failure if there was an error. */
if ((this->connector_type_error = this->connector_name_error))
return -1;
@@ -584,6 +593,7 @@ static int get_connector_type(libgamma_crtc_information_t* restrict this)
if (strstr(this->connector_name, name "-") == this->connector_name) \
return this->connector_type = LIBGAMMA_CONNECTOR_TYPE_##type, 0
+ /* Check begin on the name of the output to find out what type the connector is of. */
__select ("None", Unknown);
__select ("VGA", VGA);
__select ("DVI-I", DVII);
@@ -599,6 +609,7 @@ static int get_connector_type(libgamma_crtc_information_t* restrict this)
#undef __select
+ /* If there was no matching output name pattern report that and exit with an error. */
this->connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED;
return -1;
}
@@ -618,20 +629,16 @@ static int get_output_name(libgamma_crtc_information_t* restrict out, xcb_randr_
uint16_t length;
size_t i;
+ /* Get the name of the output and the length of that name. */
name = xcb_randr_get_output_info_name(output);
length = output->name_len; /* There is no NUL-termination. */
if (name == NULL)
- {
- out->connector_name_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED;
- return -1;
- }
+ return out->connector_name_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED;
+ /* Allocate a memory area for a NUL-terminated copy of the name. */
store = malloc(((size_t)length + 1) * sizeof(char));
if (store == NULL)
- {
- out->connector_name_error = errno;
- return -1;
- }
+ return out->connector_name_error = errno, -1;
/* char is guaranteed to be (u)int_least8_t, but it is only guaranteed to be (u)int8_t
* on POSIX, so to be truly portable we will not assume that char is (u)int8_t. */