aboutsummaryrefslogtreecommitdiffstats
path: root/src/gamma-randr.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2025-03-16 22:36:46 +0100
committerMattias Andrée <m@maandree.se>2025-03-16 22:36:46 +0100
commitf84a3ba77c61a351e1d7efb67bd40db23a435281 (patch)
tree83948f9074f2c761913cbb648f8b36bcdae43367 /src/gamma-randr.c
parentMajor refactoring and some fixes (diff)
downloadredshift-ng-f84a3ba77c61a351e1d7efb67bd40db23a435281.tar.gz
redshift-ng-f84a3ba77c61a351e1d7efb67bd40db23a435281.tar.bz2
redshift-ng-f84a3ba77c61a351e1d7efb67bd40db23a435281.tar.xz
Refactor
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'src/gamma-randr.c')
-rw-r--r--src/gamma-randr.c65
1 files changed, 26 insertions, 39 deletions
diff --git a/src/gamma-randr.c b/src/gamma-randr.c
index 6e5f154..561a243 100644
--- a/src/gamma-randr.c
+++ b/src/gamma-randr.c
@@ -48,58 +48,57 @@ struct gamma_state {
static int
-randr_init(struct gamma_state **state)
+randr_create(struct gamma_state **state_out)
{
xcb_randr_query_version_cookie_t ver_cookie;
xcb_randr_query_version_reply_t *ver_reply;
xcb_generic_error_t *error;
- struct gamma_state *s;
+ struct gamma_state *state;
int ec;
- /* Initialize state. */
- *state = emalloc(sizeof(**state));
-
- s = *state;
- s->screen_num = -1;
- s->crtc_num = NULL;
-
- s->crtc_num_count = 0;
- s->crtc_count = 0;
- s->crtcs = NULL;
+ /* Initialize state */
+ state = *state_out = emalloc(sizeof(**state_out));
+ state->screen_num = -1;
+ state->crtc_num = NULL;
+ state->crtc_num_count = 0;
+ state->crtc_count = 0;
+ state->crtcs = NULL;
/* Open X server connection */
- s->conn = xcb_connect(NULL, &s->preferred_screen);
+ state->conn = xcb_connect(NULL, &state->preferred_screen);
/* Query RandR version */
- ver_cookie = xcb_randr_query_version(s->conn, RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR);
- ver_reply = xcb_randr_query_version_reply(s->conn, ver_cookie, &error);
+ ver_cookie = xcb_randr_query_version(state->conn, RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR);
+ ver_reply = xcb_randr_query_version_reply(state->conn, ver_cookie, &error);
/* TODO What does it mean when both error and ver_reply is NULL?
Apparently, we have to check both to avoid seg faults. */
if (error || ver_reply == NULL) {
ec = (error != 0) ? error->error_code : -1;
weprintf(_("`%s' returned error %i."), "RANDR Query Version", ec);
- xcb_disconnect(s->conn);
- free(s);
- return -1;
+ goto fail;
}
if (ver_reply->major_version != RANDR_VERSION_MAJOR ||
ver_reply->minor_version < RANDR_VERSION_MINOR) {
weprintf(_("Unsupported RANDR version (%u.%u)."), ver_reply->major_version, ver_reply->minor_version);
free(ver_reply);
- xcb_disconnect(s->conn);
- free(s);
- return -1;
+ goto fail;
}
free(ver_reply);
return 0;
+
+fail:
+ xcb_disconnect(state->conn);
+ free(state);
+ *state_out = NULL;
+ return -1;
}
static int
-randr_start(struct gamma_state *state, enum program_mode mode)
+randr_start(struct gamma_state *state)
{
xcb_generic_error_t *error;
const xcb_setup_t *setup;
@@ -109,8 +108,6 @@ randr_start(struct gamma_state *state, enum program_mode mode)
xcb_randr_get_screen_resources_current_reply_t *res_reply;
xcb_randr_crtc_t *crtcs;
- (void) mode;
-
screen_num = state->screen_num;
if (screen_num < 0)
screen_num = state->preferred_screen;
@@ -326,8 +323,7 @@ randr_set_option(struct gamma_state *state, const char *key, const char *value)
}
static int
-randr_set_temperature_for_crtc(struct gamma_state *state, int crtc_num,
- const struct colour_setting *setting, int preserve)
+randr_apply_for_crtc(struct gamma_state *state, int crtc_num, const struct colour_setting *setting, int preserve)
{
xcb_randr_crtc_t crtc;
xcb_void_cookie_t gamma_set_cookie;
@@ -386,18 +382,18 @@ randr_set_temperature_for_crtc(struct gamma_state *state, int crtc_num,
}
static int
-randr_set_temperature(struct gamma_state *state, const struct colour_setting *setting, int preserve)
+randr_apply(struct gamma_state *state, const struct colour_setting *setting, int preserve)
{
int i;
/* If no CRTC numbers have been specified, set temperature on all CRTCs. */
if (!state->crtc_num_count) {
for (i = 0; i < state->crtc_count; i++)
- if (randr_set_temperature_for_crtc(state, i, setting, preserve) < 0)
+ if (randr_apply_for_crtc(state, i, setting, preserve) < 0)
return -1;
} else {
for (i = 0; i < state->crtc_num_count; ++i)
- if (randr_set_temperature_for_crtc(state, state->crtc_num[i], setting, preserve) < 0)
+ if (randr_apply_for_crtc(state, state->crtc_num[i], setting, preserve) < 0)
return -1;
}
@@ -405,13 +401,4 @@ randr_set_temperature(struct gamma_state *state, const struct colour_setting *se
}
-const struct gamma_method randr_gamma_method = {
- "randr", 1,
- &randr_init,
- &randr_start,
- &randr_free,
- &randr_print_help,
- &randr_set_option,
- &randr_restore,
- &randr_set_temperature
-};
+const struct gamma_method randr_gamma_method = GAMMA_METHOD_INIT("randr", 1, randr);