diff options
Diffstat (limited to 'src/gamma-randr.c')
-rw-r--r-- | src/gamma-randr.c | 109 |
1 files changed, 57 insertions, 52 deletions
diff --git a/src/gamma-randr.c b/src/gamma-randr.c index 713139a..9bbb807 100644 --- a/src/gamma-randr.c +++ b/src/gamma-randr.c @@ -48,11 +48,16 @@ struct gamma_state { static int randr_init(struct gamma_state **state) { + 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; + /* Initialize state. */ - *state = malloc(sizeof(struct gamma_state)); + *state = malloc(sizeof(**state)); if (*state == NULL) return -1; - struct gamma_state *s = *state; + s = *state; s->screen_num = -1; s->crtc_num = NULL; @@ -60,17 +65,12 @@ randr_init(struct gamma_state **state) s->crtc_count = 0; s->crtcs = NULL; - xcb_generic_error_t *error; - /* Open X server connection */ s->conn = xcb_connect(NULL, &s->preferred_screen); /* Query RandR version */ - xcb_randr_query_version_cookie_t ver_cookie = - xcb_randr_query_version(s->conn, RANDR_VERSION_MAJOR, - RANDR_VERSION_MINOR); - xcb_randr_query_version_reply_t *ver_reply = - xcb_randr_query_version_reply(s->conn, ver_cookie, &error); + 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); /* TODO What does it mean when both error and ver_reply is NULL? Apparently, we have to check both to avoid seg faults. */ @@ -102,16 +102,22 @@ static int randr_start(struct gamma_state *state, enum program_mode mode) { xcb_generic_error_t *error; - - int screen_num = state->screen_num; + const xcb_setup_t *setup; + xcb_screen_iterator_t iter; + int i, screen_num; + xcb_randr_get_screen_resources_current_cookie_t res_cookie; + xcb_randr_get_screen_resources_current_reply_t *res_reply; + xcb_randr_crtc_t *crtcs; + + screen_num = state->screen_num; if (screen_num < 0) screen_num = state->preferred_screen; /* Get screen */ - const xcb_setup_t *setup = xcb_get_setup(state->conn); - xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup); + setup = xcb_get_setup(state->conn); + iter = xcb_setup_roots_iterator(setup); state->screen = NULL; - for (int i = 0; iter.rem > 0; i++) { + for (i = 0; iter.rem > 0; i++) { if (i == screen_num) { state->screen = iter.data; break; @@ -126,10 +132,10 @@ randr_start(struct gamma_state *state, enum program_mode mode) } /* Get list of CRTCs for the screen */ - xcb_randr_get_screen_resources_current_cookie_t res_cookie = + res_cookie = xcb_randr_get_screen_resources_current(state->conn, state->screen->root); - xcb_randr_get_screen_resources_current_reply_t *res_reply = + res_reply = xcb_randr_get_screen_resources_current_reply(state->conn, res_cookie, &error); @@ -149,11 +155,10 @@ randr_start(struct gamma_state *state, enum program_mode mode) return -1; } - xcb_randr_crtc_t *crtcs = - xcb_randr_get_screen_resources_current_crtcs(res_reply); + crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); /* Save CRTC identifier in state */ - for (int i = 0; i < state->crtc_count; i++) { + for (i = 0; i < state->crtc_count; i++) { state->crtcs[i].crtc = crtcs[i]; } @@ -162,16 +167,18 @@ randr_start(struct gamma_state *state, enum program_mode mode) /* Save size and gamma ramps of all CRTCs. Current gamma ramps are saved so we can restore them at program exit. */ - for (int i = 0; i < state->crtc_count; i++) { + for (i = 0; i < state->crtc_count; i++) { xcb_randr_crtc_t crtc = state->crtcs[i].crtc; + xcb_randr_get_crtc_gamma_size_cookie_t gamma_size_cookie; + xcb_randr_get_crtc_gamma_size_reply_t *gamma_size_reply; + xcb_randr_get_crtc_gamma_cookie_t gamma_get_cookie; + xcb_randr_get_crtc_gamma_reply_t *gamma_get_reply; + uint16_t *gamma_r, *gamma_g, *gamma_b; + unsigned int ramp_size; /* Request size of gamma ramps */ - xcb_randr_get_crtc_gamma_size_cookie_t gamma_size_cookie = - xcb_randr_get_crtc_gamma_size(state->conn, crtc); - xcb_randr_get_crtc_gamma_size_reply_t *gamma_size_reply = - xcb_randr_get_crtc_gamma_size_reply(state->conn, - gamma_size_cookie, - &error); + gamma_size_cookie = xcb_randr_get_crtc_gamma_size(state->conn, crtc); + gamma_size_reply = xcb_randr_get_crtc_gamma_size_reply(state->conn, gamma_size_cookie, &error); if (error) { fprintf(stderr, _("`%s' returned error %d\n"), @@ -180,7 +187,7 @@ randr_start(struct gamma_state *state, enum program_mode mode) return -1; } - unsigned int ramp_size = gamma_size_reply->size; + ramp_size = gamma_size_reply->size; state->crtcs[i].ramp_size = ramp_size; free(gamma_size_reply); @@ -192,12 +199,8 @@ randr_start(struct gamma_state *state, enum program_mode mode) } /* Request current gamma ramps */ - xcb_randr_get_crtc_gamma_cookie_t gamma_get_cookie = - xcb_randr_get_crtc_gamma(state->conn, crtc); - xcb_randr_get_crtc_gamma_reply_t *gamma_get_reply = - xcb_randr_get_crtc_gamma_reply(state->conn, - gamma_get_cookie, - &error); + gamma_get_cookie = xcb_randr_get_crtc_gamma(state->conn, crtc); + gamma_get_reply = xcb_randr_get_crtc_gamma_reply(state->conn, gamma_get_cookie, &error); if (error) { fprintf(stderr, _("`%s' returned error %d\n"), @@ -205,12 +208,9 @@ randr_start(struct gamma_state *state, enum program_mode mode) return -1; } - uint16_t *gamma_r = - xcb_randr_get_crtc_gamma_red(gamma_get_reply); - uint16_t *gamma_g = - xcb_randr_get_crtc_gamma_green(gamma_get_reply); - uint16_t *gamma_b = - xcb_randr_get_crtc_gamma_blue(gamma_get_reply); + gamma_r = xcb_randr_get_crtc_gamma_red(gamma_get_reply); + gamma_g = xcb_randr_get_crtc_gamma_green(gamma_get_reply); + gamma_b = xcb_randr_get_crtc_gamma_blue(gamma_get_reply); /* Allocate space for saved gamma ramps */ state->crtcs[i].saved_ramps = @@ -302,12 +302,13 @@ randr_set_option(struct gamma_state *state, const char *key, const char *value) state->screen_num = atoi(value); } else if (strcasecmp(key, "crtc") == 0) { char *tail; + int i, parsed; /* Check how many crtcs are configured */ const char *local_value = value; - while (1) { + for (;;) { errno = 0; - int parsed = strtol(local_value, &tail, 0); + parsed = strtol(local_value, &tail, 0); if (parsed == 0 && (errno != 0 || tail == local_value)) { fprintf(stderr, _("Unable to read screen" @@ -328,9 +329,9 @@ randr_set_option(struct gamma_state *state, const char *key, const char *value) /* Configure all given crtcs */ state->crtc_num = calloc(state->crtc_num_count, sizeof(int)); local_value = value; - for (int i = 0; i < state->crtc_num_count; i++) { + for (i = 0; i < state->crtc_num_count; i++) { errno = 0; - int parsed = strtol(local_value, &tail, 0); + parsed = strtol(local_value, &tail, 0); if (parsed == 0 && (errno != 0 || tail == local_value)) { return -1; @@ -363,7 +364,11 @@ randr_set_temperature_for_crtc( struct gamma_state *state, int crtc_num, const struct color_setting *setting, int preserve) { + xcb_randr_crtc_t crtc; + xcb_void_cookie_t gamma_set_cookie; xcb_generic_error_t *error; + unsigned int i, ramp_size; + uint16_t *gamma_ramps, *gamma_r, *gamma_g, *gamma_b, value; if (crtc_num >= state->crtc_count || crtc_num < 0) { fprintf(stderr, _("CRTC %d does not exist. "), @@ -378,19 +383,19 @@ randr_set_temperature_for_crtc( return -1; } - xcb_randr_crtc_t crtc = state->crtcs[crtc_num].crtc; - unsigned int ramp_size = state->crtcs[crtc_num].ramp_size; + crtc = state->crtcs[crtc_num].crtc; + ramp_size = state->crtcs[crtc_num].ramp_size; /* Create new gamma ramps */ - uint16_t *gamma_ramps = malloc(3*ramp_size*sizeof(uint16_t)); + gamma_ramps = malloc(3*ramp_size*sizeof(uint16_t)); if (gamma_ramps == NULL) { perror("malloc"); return -1; } - uint16_t *gamma_r = &gamma_ramps[0*ramp_size]; - uint16_t *gamma_g = &gamma_ramps[1*ramp_size]; - uint16_t *gamma_b = &gamma_ramps[2*ramp_size]; + gamma_r = &gamma_ramps[0*ramp_size]; + gamma_g = &gamma_ramps[1*ramp_size]; + gamma_b = &gamma_ramps[2*ramp_size]; if (preserve) { /* Initialize gamma ramps from saved state */ @@ -398,8 +403,8 @@ randr_set_temperature_for_crtc( 3*ramp_size*sizeof(uint16_t)); } else { /* Initialize gamma ramps to pure state */ - for (int i = 0; i < ramp_size; i++) { - uint16_t value = (double)i/ramp_size * (UINT16_MAX+1); + for (i = 0; i < ramp_size; i++) { + value = (double)i/ramp_size * (UINT16_MAX+1); gamma_r[i] = value; gamma_g[i] = value; gamma_b[i] = value; @@ -410,7 +415,7 @@ randr_set_temperature_for_crtc( ramp_size, ramp_size, setting); /* Set new gamma ramps */ - xcb_void_cookie_t gamma_set_cookie = + gamma_set_cookie = xcb_randr_set_crtc_gamma_checked(state->conn, crtc, ramp_size, gamma_r, gamma_g, gamma_b); |