diff options
author | Mattias Andrée <m@maandree.se> | 2025-03-06 09:55:27 +0100 |
---|---|---|
committer | Mattias Andrée <m@maandree.se> | 2025-03-06 09:55:27 +0100 |
commit | 4ff6090dfb9c947e894a7c1d0474b8d8d8f9031a (patch) | |
tree | 6c137a20f95f0be71d11940c6acb77e11aee6377 /src/gamma-randr.c | |
parent | Fix warning (diff) | |
download | redshift-ng-4ff6090dfb9c947e894a7c1d0474b8d8d8f9031a.tar.gz redshift-ng-4ff6090dfb9c947e894a7c1d0474b8d8d8f9031a.tar.bz2 redshift-ng-4ff6090dfb9c947e894a7c1d0474b8d8d8f9031a.tar.xz |
Style
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'src/gamma-randr.c')
-rw-r--r-- | src/gamma-randr.c | 187 |
1 files changed, 69 insertions, 118 deletions
diff --git a/src/gamma-randr.c b/src/gamma-randr.c index 6c00625..c8ea754 100644 --- a/src/gamma-randr.c +++ b/src/gamma-randr.c @@ -42,7 +42,7 @@ struct gamma_state { int preferred_screen; int screen_num; int crtc_num_count; - int* crtc_num; + int *crtc_num; unsigned int crtc_count; struct randr_crtc_state *crtcs; }; @@ -79,8 +79,7 @@ randr_init(struct gamma_state **state) Apparently, we have to check both to avoid seg faults. */ if (error || ver_reply == NULL) { int ec = (error != 0) ? error->error_code : -1; - fprintf(stderr, _("`%s' returned error %d\n"), - "RANDR Query Version", ec); + weprintf(_("`%s' returned error %d\n"), "RANDR Query Version", ec); xcb_disconnect(s->conn); free(s); return -1; @@ -88,8 +87,8 @@ randr_init(struct gamma_state **state) if (ver_reply->major_version != RANDR_VERSION_MAJOR || ver_reply->minor_version < RANDR_VERSION_MINOR) { - fprintf(stderr, _("Unsupported RANDR version (%u.%u)\n"), - ver_reply->major_version, ver_reply->minor_version); + weprintf(_("Unsupported RANDR version (%u.%u)\n"), + ver_reply->major_version, ver_reply->minor_version); free(ver_reply); xcb_disconnect(s->conn); free(s); @@ -115,7 +114,8 @@ randr_start(struct gamma_state *state, enum program_mode mode) (void) mode; screen_num = state->screen_num; - if (screen_num < 0) screen_num = state->preferred_screen; + if (screen_num < 0) + screen_num = state->preferred_screen; /* Get screen */ setup = xcb_get_setup(state->conn); @@ -130,42 +130,28 @@ randr_start(struct gamma_state *state, enum program_mode mode) xcb_screen_next(&iter); } - if (state->screen == NULL) { - fprintf(stderr, _("Screen %i could not be found.\n"), - screen_num); + if (!state->screen) { + weprintf(_("Screen %i could not be found.\n"), screen_num); return -1; } /* Get list of CRTCs for the screen */ - res_cookie = - xcb_randr_get_screen_resources_current(state->conn, - state->screen->root); - res_reply = - xcb_randr_get_screen_resources_current_reply(state->conn, - res_cookie, - &error); + res_cookie = xcb_randr_get_screen_resources_current(state->conn, state->screen->root); + res_reply = xcb_randr_get_screen_resources_current_reply(state->conn, res_cookie, &error); if (error) { - fprintf(stderr, _("`%s' returned error %d\n"), - "RANDR Get Screen Resources Current", - error->error_code); + weprintf(_("`%s' returned error %d\n"), "RANDR Get Screen Resources Current", error->error_code); return -1; } state->crtc_count = res_reply->num_crtcs; - state->crtcs = calloc(state->crtc_count, sizeof(struct randr_crtc_state)); - if (state->crtcs == NULL) { - perror("malloc"); - state->crtc_count = 0; - return -1; - } + state->crtcs = ecalloc(state->crtc_count, sizeof(struct randr_crtc_state)); crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); /* Save CRTC identifier in state */ - for (i = 0; i < state->crtc_count; i++) { + for (i = 0; i < state->crtc_count; i++) state->crtcs[i].crtc = crtcs[i]; - } free(res_reply); @@ -186,9 +172,7 @@ randr_start(struct gamma_state *state, enum program_mode mode) 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"), - "RANDR Get CRTC Gamma Size", - error->error_code); + weprintf(_("`%s' returned error %d\n"), "RANDR Get CRTC Gamma Size", error->error_code); return -1; } @@ -198,8 +182,7 @@ randr_start(struct gamma_state *state, enum program_mode mode) free(gamma_size_reply); if (ramp_size == 0) { - fprintf(stderr, _("Gamma ramp size too small: %i\n"), - ramp_size); + weprintf(_("Gamma ramp size too small: %i\n"), ramp_size); return -1; } @@ -208,8 +191,7 @@ randr_start(struct gamma_state *state, enum program_mode mode) gamma_get_reply = xcb_randr_get_crtc_gamma_reply(state->conn, gamma_get_cookie, &error); if (error) { - fprintf(stderr, _("`%s' returned error %d\n"), - "RANDR Get CRTC Gamma", error->error_code); + weprintf(_("`%s' returned error %d\n"), "RANDR Get CRTC Gamma", error->error_code); return -1; } @@ -218,21 +200,12 @@ randr_start(struct gamma_state *state, enum program_mode mode) gamma_b = xcb_randr_get_crtc_gamma_blue(gamma_get_reply); /* Allocate space for saved gamma ramps */ - state->crtcs[i].saved_ramps = - malloc(3*ramp_size*sizeof(uint16_t)); - if (state->crtcs[i].saved_ramps == NULL) { - perror("malloc"); - free(gamma_get_reply); - return -1; - } + state->crtcs[i].saved_ramps = emalloc(3 * ramp_size * sizeof(uint16_t)); /* Copy gamma ramps into CRTC state */ - memcpy(&state->crtcs[i].saved_ramps[0*ramp_size], gamma_r, - ramp_size*sizeof(uint16_t)); - memcpy(&state->crtcs[i].saved_ramps[1*ramp_size], gamma_g, - ramp_size*sizeof(uint16_t)); - memcpy(&state->crtcs[i].saved_ramps[2*ramp_size], gamma_b, - ramp_size*sizeof(uint16_t)); + memcpy(&state->crtcs[i].saved_ramps[0 * ramp_size], gamma_r, ramp_size * sizeof(uint16_t)); + memcpy(&state->crtcs[i].saved_ramps[1 * ramp_size], gamma_g, ramp_size * sizeof(uint16_t)); + memcpy(&state->crtcs[i].saved_ramps[2 * ramp_size], gamma_b, ramp_size * sizeof(uint16_t)); free(gamma_get_reply); } @@ -244,9 +217,10 @@ static void randr_restore(struct gamma_state *state) { xcb_generic_error_t *error; + int i; /* Restore CRTC gamma ramps */ - 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; unsigned int ramp_size = state->crtcs[i].ramp_size; @@ -262,9 +236,8 @@ randr_restore(struct gamma_state *state) error = xcb_request_check(state->conn, gamma_set_cookie); if (error) { - fprintf(stderr, _("`%s' returned error %d\n"), - "RANDR Set CRTC Gamma", error->error_code); - fprintf(stderr, _("Unable to restore CRTC %i\n"), i); + weprintf(_("`%s' returned error %d\n"), "RANDR Set CRTC Gamma", error->error_code); + weprintf(_("Unable to restore CRTC %i\n"), i); } } } @@ -272,10 +245,11 @@ randr_restore(struct gamma_state *state) static void randr_free(struct gamma_state *state) { + int i; + /* Free CRTC state */ - for (int i = 0; i < state->crtc_count; i++) { + for (i = 0; i < state->crtc_count; i++) free(state->crtcs[i].saved_ramps); - } free(state->crtcs); free(state->crtc_num); @@ -303,9 +277,9 @@ randr_print_help(FILE *f) static int randr_set_option(struct gamma_state *state, const char *key, const char *value) { - if (strcasecmp(key, "screen") == 0) { + if (!strcasecmp(key, "screen")) { state->screen_num = atoi(value); - } else if (strcasecmp(key, "crtc") == 0) { + } else if (!strcasecmp(key, "crtc")) { char *tail; int i, parsed; @@ -314,21 +288,17 @@ randr_set_option(struct gamma_state *state, const char *key, const char *value) for (;;) { errno = 0; parsed = strtol(local_value, &tail, 0); - if (parsed == 0 && (errno != 0 || - tail == local_value)) { - fprintf(stderr, _("Unable to read screen" - " number: `%s'.\n"), value); + if (parsed == 0 && (errno != 0 || tail == local_value)) { + weprintf(_("Unable to read screen number: `%s'.\n"), value); return -1; - } else { - state->crtc_num_count += 1; } + state->crtc_num_count += 1; local_value = tail; - if (*local_value == ',') { + if (*local_value == ',') local_value += 1; - } else if (*local_value == '\0') { + else if (!*local_value) break; - } } /* Configure all given crtcs */ @@ -337,27 +307,21 @@ randr_set_option(struct gamma_state *state, const char *key, const char *value) for (i = 0; i < state->crtc_num_count; i++) { errno = 0; parsed = strtol(local_value, &tail, 0); - if (parsed == 0 && (errno != 0 || - tail == local_value)) { + if (parsed == 0 && (errno != 0 || tail == local_value)) return -1; - } else { - state->crtc_num[i] = parsed; - } + state->crtc_num[i] = parsed; local_value = tail; - if (*local_value == ',') { + if (*local_value == ',') local_value += 1; - } else if (*local_value == '\0') { + else if (!*local_value) break; - } } } else if (strcasecmp(key, "preserve") == 0) { - fprintf(stderr, _("Parameter `%s` is now always on; " - " Use the `%s` command-line option" - " to disable.\n"), - key, "-P"); + weprintf(_("Parameter `%s` is now always on; Use the `%s`" + " command-line option to disable.\n"), key, "-P"); } else { - fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key); + weprintf(_("Unknown method parameter: `%s'.\n"), key); return -1; } @@ -365,9 +329,8 @@ 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 color_setting *setting, - int preserve) +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; @@ -376,14 +339,11 @@ randr_set_temperature_for_crtc( 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. "), - crtc_num); - if (state->crtc_count > 1) { - fprintf(stderr, _("Valid CRTCs are [0-%d].\n"), - state->crtc_count-1); - } else { + fprintf(stderr, _("CRTC %d does not exist. "), crtc_num); + if (state->crtc_count > 1) + fprintf(stderr, _("Valid CRTCs are [0-%d].\n"), state->crtc_count-1); + else fprintf(stderr, _("Only CRTC 0 exists.\n")); - } return -1; } @@ -392,24 +352,20 @@ randr_set_temperature_for_crtc( ramp_size = state->crtcs[crtc_num].ramp_size; /* Create new gamma ramps */ - gamma_ramps = malloc(3*ramp_size*sizeof(uint16_t)); - if (gamma_ramps == NULL) { - perror("malloc"); - return -1; - } + gamma_ramps = emalloc(3 * ramp_size * sizeof(uint16_t)); - gamma_r = &gamma_ramps[0*ramp_size]; - gamma_g = &gamma_ramps[1*ramp_size]; - 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 */ memcpy(gamma_ramps, state->crtcs[crtc_num].saved_ramps, - 3*ramp_size*sizeof(uint16_t)); + 3 * ramp_size * sizeof(uint16_t)); } else { /* Initialize gamma ramps to pure state */ for (i = 0; i < ramp_size; i++) { - value = (double)i/ramp_size * (UINT16_MAX+1); + value = (double)i / (ramp_size - 1) * UINT16_MAX; gamma_r[i] = value; gamma_g[i] = value; gamma_b[i] = value; @@ -420,15 +376,12 @@ randr_set_temperature_for_crtc( ramp_size, ramp_size, setting); /* Set new gamma ramps */ - gamma_set_cookie = - xcb_randr_set_crtc_gamma_checked(state->conn, crtc, - ramp_size, gamma_r, - gamma_g, gamma_b); + gamma_set_cookie = xcb_randr_set_crtc_gamma_checked(state->conn, crtc, ramp_size, + gamma_r, gamma_g, gamma_b); error = xcb_request_check(state->conn, gamma_set_cookie); if (error) { - fprintf(stderr, _("`%s' returned error %d\n"), - "RANDR Set CRTC Gamma", error->error_code); + weprintf(_("`%s' returned error %d\n"), "RANDR Set CRTC Gamma", error->error_code); free(gamma_ramps); return -1; } @@ -439,24 +392,22 @@ randr_set_temperature_for_crtc( } static int -randr_set_temperature( - struct gamma_state *state, const struct color_setting *setting, int preserve) +randr_set_temperature(struct gamma_state *state, const struct color_setting *setting, int preserve) { - int r; - - /* If no CRTC numbers have been specified, - set temperature on all CRTCs. */ - if (state->crtc_num_count == 0) { - for (int i = 0; i < state->crtc_count; i++) { - r = randr_set_temperature_for_crtc( - state, i, setting, preserve); - if (r < 0) return -1; + int i, r; + + /* 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++) { + r = randr_set_temperature_for_crtc(state, i, setting, preserve); + if (r < 0) + return -1; } } else { - for (int i = 0; i < state->crtc_num_count; ++i) { - r = randr_set_temperature_for_crtc( - state, state->crtc_num[i], setting, preserve); - if (r < 0) return -1; + for (i = 0; i < state->crtc_num_count; ++i) { + r = randr_set_temperature_for_crtc(state, state->crtc_num[i], setting, preserve); + if (r < 0) + return -1; } } |