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-w32gdi.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-w32gdi.c')
-rw-r--r-- | src/gamma-w32gdi.c | 115 |
1 files changed, 50 insertions, 65 deletions
diff --git a/src/gamma-w32gdi.c b/src/gamma-w32gdi.c index 87568f9..a252e0a 100644 --- a/src/gamma-w32gdi.c +++ b/src/gamma-w32gdi.c @@ -17,12 +17,11 @@ Copyright (c) 2010-2017 Jon Lund Steffensen <jonlst@gmail.com> Copyright (c) 2025 Mattias Andrée <m@maandree.se> */ -#include "common.h" - #ifndef WINVER # define WINVER 0x0500 #endif -#include <windows.h> +#include "common.h" + #include <wingdi.h> #define GAMMA_RAMP_SIZE 256 @@ -37,47 +36,37 @@ struct gamma_state { static int w32gdi_init(struct gamma_state **state) { - *state = malloc(sizeof(struct gamma_state)); - if (state == NULL) return -1; - - struct gamma_state *s = *state; - s->saved_ramps = NULL; - + *state = emalloc(sizeof(**state)); + (*state)->saved_ramps = NULL; return 0; } static int w32gdi_start(struct gamma_state *state, program_mode_t mode) { - BOOL r; + HDC hDC; + int cmcap; /* Open device context */ - HDC hDC = GetDC(NULL); - if (hDC == NULL) { - fputs(_("Unable to open device context.\n"), stderr); + hDC = GetDC(NULL); + if (!hDC) { + weprintf(_("Unable to open device context.\n")); return -1; } /* Check support for gamma ramps */ - int cmcap = GetDeviceCaps(hDC, COLORMGMTCAPS); + cmcap = GetDeviceCaps(hDC, COLORMGMTCAPS); if (cmcap != CM_GAMMA_RAMP) { - fputs(_("Display device does not support gamma ramps.\n"), - stderr); + weprintf(_("Display device does not support gamma ramps.\n")); return -1; } /* Allocate space for saved gamma ramps */ - state->saved_ramps = malloc(3*GAMMA_RAMP_SIZE*sizeof(WORD)); - if (state->saved_ramps == NULL) { - perror("malloc"); - ReleaseDC(NULL, hDC); - return -1; - } + state->saved_ramps = emalloc(3 * GAMMA_RAMP_SIZE * sizeof(WORD)); /* Save current gamma ramps so we can restore them at program exit */ - r = GetDeviceGammaRamp(hDC, state->saved_ramps); - if (!r) { - fputs(_("Unable to save current gamma ramp.\n"), stderr); + if (!GetDeviceGammaRamp(hDC, state->saved_ramps)) { + weprintf(_("Unable to save current gamma ramp.\n")); ReleaseDC(NULL, hDC); return -1; } @@ -108,13 +97,11 @@ w32gdi_print_help(FILE *f) static int w32gdi_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; } @@ -124,23 +111,27 @@ w32gdi_set_option(struct gamma_state *state, const char *key, const char *value) static void w32gdi_restore(struct gamma_state *state) { + HDC hDC; + int i; + /* Open device context */ - HDC hDC = GetDC(NULL); - if (hDC == NULL) { + hDC = GetDC(NULL); + if (!hDC) { fputs(_("Unable to open device context.\n"), stderr); return; } /* Restore gamma ramps */ - BOOL r = FALSE; - for (int i = 0; i < MAX_ATTEMPTS && !r; i++) { + for (i = 0; i < MAX_ATTEMPTS; i++) { /* We retry a few times before giving up because some buggy drivers fail on the first invocation of SetDeviceGammaRamp just to succeed on the second. */ - r = SetDeviceGammaRamp(hDC, state->saved_ramps); + if (SetDeviceGammaRamp(hDC, state->saved_ramps)) + goto done; } - if (!r) fputs(_("Unable to restore gamma ramps.\n"), stderr); + weprintf(_("Unable to restore gamma ramps.\n")); +done: /* Release device context */ ReleaseDC(NULL, hDC); } @@ -149,36 +140,31 @@ static int w32gdi_set_temperature( struct gamma_state *state, const color_setting_t *setting, int preserve) { - BOOL r; + WORD *gamma_ramps, *gamma_r, *gamma_b, *gamma_g, value; + HDC hDC; + int i; /* Open device context */ - HDC hDC = GetDC(NULL); - if (hDC == NULL) { - fputs(_("Unable to open device context.\n"), stderr); + hDC = GetDC(NULL); + if (!hDC) { + weprintf(_("Unable to open device context.\n")); return -1; } /* Create new gamma ramps */ - WORD *gamma_ramps = malloc(3*GAMMA_RAMP_SIZE*sizeof(WORD)); - if (gamma_ramps == NULL) { - perror("malloc"); - ReleaseDC(NULL, hDC); - return -1; - } + gamma_ramps = emalloc(3 * GAMMA_RAMP_SIZE * sizeof(WORD)); - WORD *gamma_r = &gamma_ramps[0*GAMMA_RAMP_SIZE]; - WORD *gamma_g = &gamma_ramps[1*GAMMA_RAMP_SIZE]; - WORD *gamma_b = &gamma_ramps[2*GAMMA_RAMP_SIZE]; + gamma_r = &gamma_ramps[0 * GAMMA_RAMP_SIZE]; + gamma_g = &gamma_ramps[1 * GAMMA_RAMP_SIZE]; + gamma_b = &gamma_ramps[2 * GAMMA_RAMP_SIZE]; if (preserve) { /* Initialize gamma ramps from saved state */ - memcpy(gamma_ramps, state->saved_ramps, - 3*GAMMA_RAMP_SIZE*sizeof(WORD)); + memcpy(gamma_ramps, state->saved_ramps, 3 * GAMMA_RAMP_SIZE * sizeof(WORD)); } else { /* Initialize gamma ramps to pure state */ - for (int i = 0; i < GAMMA_RAMP_SIZE; i++) { - WORD value = (double)i/GAMMA_RAMP_SIZE * - (UINT16_MAX+1); + for (i = 0; i < GAMMA_RAMP_SIZE; i++) { + value = (double)i / (GAMMA_RAMP_SIZE - 1) * UINT16_MAX; gamma_r[i] = value; gamma_g[i] = value; gamma_b[i] = value; @@ -186,23 +172,22 @@ w32gdi_set_temperature( } colorramp_fill_u16(gamma_r, gamma_g, gamma_b, GAMMA_RAMP_SIZE, - GAMMA_RAMP_SIZE, GAMMA_RAMP_SIZE, setting); + GAMMA_RAMP_SIZE, GAMMA_RAMP_SIZE, setting); /* Set new gamma ramps */ - r = FALSE; - for (int i = 0; i < MAX_ATTEMPTS && !r; i++) { + for (i = 0; i < MAX_ATTEMPTS; i++) { /* We retry a few times before giving up because some buggy drivers fail on the first invocation of SetDeviceGammaRamp just to succeed on the second. */ - r = SetDeviceGammaRamp(hDC, gamma_ramps); - } - if (!r) { - fputs(_("Unable to set gamma ramps.\n"), stderr); - free(gamma_ramps); - ReleaseDC(NULL, hDC); - return -1; + if (SetDeviceGammaRamp(hDC, gamma_ramps)) + goto done; } + weprintf(_("Unable to set gamma ramps.\n")); + free(gamma_ramps); + ReleaseDC(NULL, hDC); + return -1; +done: free(gamma_ramps); /* Release device context */ |