diff options
author | Mattias Andrée <m@maandree.se> | 2025-03-23 14:22:25 +0100 |
---|---|---|
committer | Mattias Andrée <m@maandree.se> | 2025-03-23 14:22:25 +0100 |
commit | f080f5df7f4c03f236fcc879ff0ce1aa903662be (patch) | |
tree | 671f7cc61996283ad6bf8ade8e2c1089536b1633 | |
parent | Rewrite gamma-w32gdi to use libgamma (diff) | |
download | redshift-ng-f080f5df7f4c03f236fcc879ff0ce1aa903662be.tar.gz redshift-ng-f080f5df7f4c03f236fcc879ff0ce1aa903662be.tar.bz2 redshift-ng-f080f5df7f4c03f236fcc879ff0ce1aa903662be.tar.xz |
Rewrite gamma-quartz.c to use libgamma
Signed-off-by: Mattias Andrée <m@maandree.se>
-rw-r--r-- | src/gamma-quartz.c | 164 |
1 files changed, 7 insertions, 157 deletions
diff --git a/src/gamma-quartz.c b/src/gamma-quartz.c index c05e72d..4cb050a 100644 --- a/src/gamma-quartz.c +++ b/src/gamma-quartz.c @@ -19,113 +19,11 @@ */ #include "common.h" -#include <ApplicationServices/ApplicationServices.h> - - -struct quartz_display_state { - CGDirectDisplayID display; - uint32_t ramp_size; - float *saved_ramps; -}; - - -struct gamma_state { - struct quartz_display_state *displays; - uint32_t display_count; -}; - static int quartz_create(struct gamma_state **state_out) { - *state_out = emalloc(sizeof(**state_out)); - (*state_out)->displays = NULL; - return 0; -} - - -static int -quartz_start(struct gamma_state *state) -{ - float *gamma_r, *gamma_g, *gamma_b; - uint32_t i, display_count, ramp_size, sample_count; - CGDirectDisplayID *displays, display; - CGError error; - - /* Get display count */ - error = CGGetOnlineDisplayList(0, NULL, &display_count); - if (error != kCGErrorSuccess) - return -1; - - state->display_count = display_count; - - displays = emalloc(sizeof(CGDirectDisplayID) * display_count); - - /* Get list of displays */ - error = CGGetOnlineDisplayList(display_count, displays, &display_count); - if (error != kCGErrorSuccess) { - free(displays); - return -1; - } - - /* Allocate list of display state */ - state->displays = emalloc(display_count * sizeof(struct quartz_display_state)); - - /* Copy display indentifiers to display state */ - for (i = 0; i < display_count; i++) { - state->displays[i].display = displays[i]; - state->displays[i].saved_ramps = NULL; - } - - free(displays); - - /* Save gamma ramps for all displays in display state */ - for (i = 0; i < display_count; i++) { - display = state->displays[i].display; - - ramp_size = CGDisplayGammaTableCapacity(display); - if (!ramp_size) { - weprintf(_("Gamma ramp size too small: %zu"), (size_t)ramp_size); - return -1; - } - - state->displays[i].ramp_size = ramp_size; - - /* Allocate space for saved ramps */ - state->displays[i].saved_ramps = emalloc(3 * ramp_size * sizeof(float)); - - 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 */ - 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.")); - return -1; - } - } - - return 0; -} - - -static void -quartz_restore(struct gamma_state *state) -{ - CGDisplayRestoreColorSyncSettings(); -} - - -static void -quartz_free(struct gamma_state *state) -{ - 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); + return direct_create(state_out, LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS, "quartz"); } @@ -141,65 +39,17 @@ static int quartz_set_option(struct gamma_state *state, const char *key, const char *value) { if (!strcasecmp(key, "preserve")) { - weprintf(_("Parameter `%s' is now always on; use the `%s' command-line option to disable."), key, "-P"); + weprintf(_("Deprecated method parameter ignored: `%s'."), key); + return 0; } else { weprintf(_("Unknown method parameter: `%s'."), key); return -1; } - - return 0; -} - - -static void -quartz_apply_for_display(struct gamma_state *state, int display_index, const colour_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 */ - gamma_ramps = emalloc(3 * ramp_size * sizeof(float)); - - 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)); - } else { - /* Initialize gamma ramps to pure state */ - for (i = 0; i < ramp_size; i++) { - value = (float)i / ramp_size; - gamma_r[i] = value; - gamma_g[i] = value; - gamma_b[i] = value; - } - } - - fill_ramps_float(gamma_r, gamma_g, gamma_b, ramp_size, ramp_size, ramp_size, setting); - - error = CGSetDisplayTransferByTable(display, ramp_size, gamma_r, gamma_g, gamma_b); - if (error != kCGErrorSuccess) { - free(gamma_ramps); - return; - } - - free(gamma_ramps); -} - - -static int -quartz_apply(struct gamma_state *state, const colour_setting_t *setting, int preserve) -{ - uint32_t i; - for (i = 0; i < state->display_count; i++) - quartz_apply_for_display(state, i, setting, preserve); - return 0; } +#define quartz_start direct_start +#define quartz_apply direct_apply +#define quartz_restore direct_restore +#define quartz_free direct_free const struct gamma_method quartz_gamma_method = GAMMA_METHOD_INIT("quartz", 1, 1, quartz); |