aboutsummaryrefslogtreecommitdiffstats
path: root/src/gamma-vidmode.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gamma-vidmode.c')
-rw-r--r--src/gamma-vidmode.c93
1 files changed, 34 insertions, 59 deletions
diff --git a/src/gamma-vidmode.c b/src/gamma-vidmode.c
index b462480..1fda89a 100644
--- a/src/gamma-vidmode.c
+++ b/src/gamma-vidmode.c
@@ -36,17 +36,15 @@ vidmode_init(struct gamma_state **state)
{
struct gamma_state *s;
- *state = malloc(sizeof(struct gamma_state));
- if (*state == NULL) return -1;
+ s = *state = emalloc(sizeof(struct gamma_state));
- s = *state;
s->screen_num = -1;
s->saved_ramps = NULL;
/* Open display */
s->display = XOpenDisplay(NULL);
- if (s->display == NULL) {
- fprintf(stderr, _("X request failed: %s\n"), "XOpenDisplay");
+ if (!s->display) {
+ weprintf(_("X request failed: %s\n"), "XOpenDisplay");
return -1;
}
@@ -63,38 +61,31 @@ vidmode_start(struct gamma_state *state, enum program_mode mode)
(void) mode;
- if (screen_num < 0) screen_num = DefaultScreen(state->display);
+ if (screen_num < 0)
+ screen_num = DefaultScreen(state->display);
state->screen_num = screen_num;
/* Query extension version */
r = XF86VidModeQueryVersion(state->display, &major, &minor);
if (!r) {
- fprintf(stderr, _("X request failed: %s\n"),
- "XF86VidModeQueryVersion");
+ weprintf(_("X request failed: %s\n"), "XF86VidModeQueryVersion");
return -1;
}
/* Request size of gamma ramps */
- r = XF86VidModeGetGammaRampSize(state->display, state->screen_num,
- &state->ramp_size);
+ r = XF86VidModeGetGammaRampSize(state->display, state->screen_num, &state->ramp_size);
if (!r) {
- fprintf(stderr, _("X request failed: %s\n"),
- "XF86VidModeGetGammaRampSize");
+ weprintf(_("X request failed: %s\n"), "XF86VidModeGetGammaRampSize");
return -1;
}
- if (state->ramp_size == 0) {
- fprintf(stderr, _("Gamma ramp size too small: %i\n"),
- state->ramp_size);
+ if (!state->ramp_size) {
+ weprintf(_("Gamma ramp size too small: %i\n"), state->ramp_size);
return -1;
}
/* Allocate space for saved gamma ramps */
- state->saved_ramps = malloc(3*state->ramp_size*sizeof(uint16_t));
- if (state->saved_ramps == NULL) {
- perror("malloc");
- return -1;
- }
+ state->saved_ramps = emalloc(3 * state->ramp_size * sizeof(uint16_t));
gamma_r = &state->saved_ramps[0 * state->ramp_size];
gamma_g = &state->saved_ramps[1 * state->ramp_size];
@@ -102,11 +93,9 @@ vidmode_start(struct gamma_state *state, enum program_mode mode)
/* Save current gamma ramps so we can restore them at program exit. */
r = XF86VidModeGetGammaRamp(state->display, state->screen_num,
- state->ramp_size, gamma_r, gamma_g,
- gamma_b);
+ state->ramp_size, gamma_r, gamma_g, gamma_b);
if (!r) {
- fprintf(stderr, _("X request failed: %s\n"),
- "XF86VidModeGetGammaRamp");
+ weprintf(_("X request failed: %s\n"), "XF86VidModeGetGammaRamp");
return -1;
}
@@ -131,25 +120,21 @@ vidmode_print_help(FILE *f)
fputs(_("Adjust gamma ramps with the X VidMode extension.\n"), f);
fputs("\n", f);
- /* TRANSLATORS: VidMode help output
- left column must not be translated */
- fputs(_(" screen=N\t\tX screen to apply adjustments to\n"),
- f);
+ /* TRANSLATORS: VidMode help output left column must not be translated */
+ fputs(_(" screen=N\t\tX screen to apply adjustments to\n"), f);
fputs("\n", f);
}
static int
vidmode_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, "preserve") == 0) {
- fprintf(stderr, _("Parameter `%s` is now always on; "
- " Use the `%s` command-line option"
- " to disable.\n"),
- key, "-P");
+ } else if (!strcasecmp(key, "preserve")) {
+ 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;
}
@@ -159,33 +144,27 @@ vidmode_set_option(struct gamma_state *state, const char *key, const char *value
static void
vidmode_restore(struct gamma_state *state)
{
- uint16_t *gamma_r = &state->saved_ramps[0*state->ramp_size];
- uint16_t *gamma_g = &state->saved_ramps[1*state->ramp_size];
- uint16_t *gamma_b = &state->saved_ramps[2*state->ramp_size];
+ uint16_t *gamma_r = &state->saved_ramps[0 * state->ramp_size];
+ uint16_t *gamma_g = &state->saved_ramps[1 * state->ramp_size];
+ uint16_t *gamma_b = &state->saved_ramps[2 * state->ramp_size];
+ int r;
/* Restore gamma ramps */
- int r = XF86VidModeSetGammaRamp(state->display, state->screen_num,
- state->ramp_size, gamma_r, gamma_g,
- gamma_b);
- if (!r) {
- fprintf(stderr, _("X request failed: %s\n"),
- "XF86VidModeSetGammaRamp");
- }
+ r = XF86VidModeSetGammaRamp(state->display, state->screen_num,
+ state->ramp_size, gamma_r, gamma_g, gamma_b);
+ if (!r)
+ weprintf(_("X request failed: %s\n"), "XF86VidModeSetGammaRamp");
}
static int
vidmode_set_temperature(
struct gamma_state *state, const struct color_setting *setting, int preserve)
{
- int r, i;
uint16_t value, *gamma_ramps, *gamma_r, *gamma_g, *gamma_b;
+ int r, i;
/* Create new gamma ramps */
- gamma_ramps = malloc(3*state->ramp_size*sizeof(uint16_t));
- if (gamma_ramps == NULL) {
- perror("malloc");
- return -1;
- }
+ gamma_ramps = emalloc(3 * state->ramp_size * sizeof(uint16_t));
gamma_r = &gamma_ramps[0 * state->ramp_size];
gamma_g = &gamma_ramps[1 * state->ramp_size];
@@ -193,13 +172,11 @@ vidmode_set_temperature(
if (preserve) {
/* Initialize gamma ramps from saved state */
- memcpy(gamma_ramps, state->saved_ramps,
- 3*state->ramp_size*sizeof(uint16_t));
+ memcpy(gamma_ramps, state->saved_ramps, 3 * state->ramp_size * sizeof(uint16_t));
} else {
/* Initialize gamma ramps to pure state */
for (i = 0; i < state->ramp_size; i++) {
- value = (double)i/state->ramp_size *
- (UINT16_MAX+1);
+ value = (double)i / (state->ramp_size - 1) * UINT16_MAX;
gamma_r[i] = value;
gamma_g[i] = value;
gamma_b[i] = value;
@@ -211,11 +188,9 @@ vidmode_set_temperature(
/* Set new gamma ramps */
r = XF86VidModeSetGammaRamp(state->display, state->screen_num,
- state->ramp_size, gamma_r, gamma_g,
- gamma_b);
+ state->ramp_size, gamma_r, gamma_g, gamma_b);
if (!r) {
- fprintf(stderr, _("X request failed: %s\n"),
- "XF86VidModeSetGammaRamp");
+ weprintf(_("X request failed: %s\n"), "XF86VidModeSetGammaRamp");
free(gamma_ramps);
return -1;
}