aboutsummaryrefslogtreecommitdiffstats
path: root/src/gamma-quartz.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gamma-quartz.c')
-rw-r--r--src/gamma-quartz.c125
1 files changed, 47 insertions, 78 deletions
diff --git a/src/gamma-quartz.c b/src/gamma-quartz.c
index c8e39d6..30a50d6 100644
--- a/src/gamma-quartz.c
+++ b/src/gamma-quartz.c
@@ -37,53 +37,40 @@ struct gamma_state {
static int
quartz_init(struct gamma_state **state)
{
- *state = malloc(sizeof(struct gamma_state));
- if (*state == NULL) return -1;
-
- struct gamma_state *s = *state;
- s->displays = NULL;
-
+ *state = emalloc(sizeof(**state));
+ (*state)->displays = NULL;
return 0;
}
static int
quartz_start(struct gamma_state *state, program_mode_t mode)
{
+ float *gamma_r, *gamma_g, *gamma_b;
+ uint32_t i, display_count, ramp_size, sample_count;
+ CGDirectDisplayID *displays, display;
CGError error;
- uint32_t display_count;
/* Get display count */
error = CGGetOnlineDisplayList(0, NULL, &display_count);
- if (error != kCGErrorSuccess) return -1;
+ if (error != kCGErrorSuccess)
+ return -1;
state->display_count = display_count;
- CGDirectDisplayID* displays =
- malloc(sizeof(CGDirectDisplayID)*display_count);
- if (displays == NULL) {
- perror("malloc");
- return -1;
- }
+ displays = emalloc(sizeof(CGDirectDisplayID) * display_count);
/* Get list of displays */
- error = CGGetOnlineDisplayList(display_count, displays,
- &display_count);
+ error = CGGetOnlineDisplayList(display_count, displays, &display_count);
if (error != kCGErrorSuccess) {
free(displays);
return -1;
}
/* Allocate list of display state */
- state->displays = malloc(display_count *
- sizeof(struct quartz_display_state));
- if (state->displays == NULL) {
- perror("malloc");
- free(displays);
- return -1;
- }
+ state->displays = emalloc(display_count * sizeof(struct quartz_display_state));
/* Copy display indentifiers to display state */
- for (int i = 0; i < display_count; i++) {
+ for (i = 0; i < display_count; i++) {
state->displays[i].display = displays[i];
state->displays[i].saved_ramps = NULL;
}
@@ -91,39 +78,28 @@ quartz_start(struct gamma_state *state, program_mode_t mode)
free(displays);
/* Save gamma ramps for all displays in display state */
- for (int i = 0; i < display_count; i++) {
- CGDirectDisplayID display = state->displays[i].display;
+ for (i = 0; i < display_count; i++) {
+ display = state->displays[i].display;
- uint32_t ramp_size = CGDisplayGammaTableCapacity(display);
- if (ramp_size == 0) {
- fprintf(stderr, _("Gamma ramp size too small: %i\n"),
- ramp_size);
+ ramp_size = CGDisplayGammaTableCapacity(display);
+ if (!ramp_size) {
+ weprintf(_("Gamma ramp size too small: %i\n"), ramp_size);
return -1;
}
state->displays[i].ramp_size = ramp_size;
/* Allocate space for saved ramps */
- state->displays[i].saved_ramps =
- malloc(3 * ramp_size * sizeof(float));
- if (state->displays[i].saved_ramps == NULL) {
- perror("malloc");
- return -1;
- }
+ state->displays[i].saved_ramps = emalloc(3 * ramp_size * sizeof(float));
- float *gamma_r = &state->displays[i].saved_ramps[0*ramp_size];
- float *gamma_g = &state->displays[i].saved_ramps[1*ramp_size];
- float *gamma_b = &state->displays[i].saved_ramps[2*ramp_size];
+ gamma_r = &state->displays[i].saved_ramps[0 * ramp_size];
+ gamma_g = &state->displays[i].saved_ramps[1 * ramp_size];
+ gamma_b = &state->displays[i].saved_ramps[2 * ramp_size];
/* Copy the ramps to allocated space */
- uint32_t sample_count;
- error = CGGetDisplayTransferByTable(display, ramp_size,
- gamma_r, gamma_g, gamma_b,
- &sample_count);
- if (error != kCGErrorSuccess ||
- sample_count != ramp_size) {
- fputs(_("Unable to save current gamma ramp.\n"),
- stderr);
+ error = CGGetDisplayTransferByTable(display, ramp_size, gamma_r, gamma_g, gamma_b, &sample_count);
+ if (error != kCGErrorSuccess || sample_count != ramp_size) {
+ weprintf(_("Unable to save current gamma ramp.\n"));
return -1;
}
}
@@ -140,11 +116,10 @@ quartz_restore(struct gamma_state *state)
static void
quartz_free(struct gamma_state *state)
{
- if (state->displays != NULL) {
- for (int i = 0; i < state->display_count; i++) {
+ uint32_t i;
+ if (state->displays)
+ for (i = 0; i < state->display_count; i++)
free(state->displays[i].saved_ramps);
- }
- }
free(state->displays);
free(state);
}
@@ -159,13 +134,11 @@ quartz_print_help(FILE *f)
static int
quartz_set_option(struct gamma_state *state, const char *key, const char *value)
{
- if (strcasecmp(key, "preserve") == 0) {
- fprintf(stderr, _("Parameter `%s` is now always on; "
- " Use the `%s` command-line option"
- " to disable.\n"),
- key, "-P");
+ 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;
}
@@ -173,44 +146,38 @@ quartz_set_option(struct gamma_state *state, const char *key, const char *value)
}
static void
-quartz_set_temperature_for_display(
- struct gamma_state *state, int display_index,
- const color_setting_t *setting, int preserve)
+quartz_set_temperature_for_display(struct gamma_state *state, int display_index,
+ const color_setting_t *setting, int preserve)
{
+ float *gamma_ramps, *gamma_r, *gamma_g, *gamma_b, value;
CGDirectDisplayID display = state->displays[display_index].display;
uint32_t ramp_size = state->displays[display_index].ramp_size;
+ CGError error;
+ uint32_t i;
/* Create new gamma ramps */
- float *gamma_ramps = malloc(3*ramp_size*sizeof(float));
- if (gamma_ramps == NULL) {
- perror("malloc");
- return;
- }
+ gamma_ramps = emalloc(3 * ramp_size * sizeof(float));
- float *gamma_r = &gamma_ramps[0*ramp_size];
- float *gamma_g = &gamma_ramps[1*ramp_size];
- float *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->displays[display_index].saved_ramps,
- 3*ramp_size*sizeof(float));
+ memcpy(gamma_ramps, state->displays[display_index].saved_ramps, 3 * ramp_size * sizeof(float));
} else {
/* Initialize gamma ramps to pure state */
- for (int i = 0; i < ramp_size; i++) {
- float value = (double)i/ramp_size;
+ for (i = 0; i < ramp_size; i++) {
+ value = (float)i / ramp_size;
gamma_r[i] = value;
gamma_g[i] = value;
gamma_b[i] = value;
}
}
- colorramp_fill_float(gamma_r, gamma_g, gamma_b, ramp_size,
- ramp_size, ramp_size, setting);
+ colorramp_fill_float(gamma_r, gamma_g, gamma_b, ramp_size, ramp_size, ramp_size, setting);
- CGError error =
- CGSetDisplayTransferByTable(display, ramp_size,
- gamma_r, gamma_g, gamma_b);
+ error = CGSetDisplayTransferByTable(display, ramp_size, gamma_r, gamma_g, gamma_b);
if (error != kCGErrorSuccess) {
free(gamma_ramps);
return;
@@ -223,7 +190,9 @@ static int
quartz_set_temperature(
struct gamma_state *state, const color_setting_t *setting, int preserve)
{
- for (int i = 0; i < state->display_count; i++) {
+ uint32_t i;
+
+ for (i = 0; i < state->display_count; i++) {
quartz_set_temperature_for_display(
state, i, setting, preserve);
}