From 65dd76a221e23a7200b33645326e90c0c02be7ed Mon Sep 17 00:00:00 2001 From: Jon Lund Steffensen Date: Mon, 24 May 2010 23:37:36 +0200 Subject: Use the prefix 'gamma' for gamma adjustment source files. --- src/Makefile.am | 12 +- src/gamma-randr.c | 367 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/gamma-randr.h | 52 ++++++++ src/gamma-vidmode.c | 195 ++++++++++++++++++++++++++++ src/gamma-vidmode.h | 40 ++++++ src/gamma-w32gdi.c | 127 ++++++++++++++++++ src/gamma-w32gdi.h | 38 ++++++ src/randr.c | 367 ---------------------------------------------------- src/randr.h | 52 -------- src/redshift.c | 14 +- src/redshift.h | 2 +- src/vidmode.c | 195 ---------------------------- src/vidmode.h | 40 ------ src/w32gdi.c | 127 ------------------ src/w32gdi.h | 38 ------ 15 files changed, 833 insertions(+), 833 deletions(-) create mode 100644 src/gamma-randr.c create mode 100644 src/gamma-randr.h create mode 100644 src/gamma-vidmode.c create mode 100644 src/gamma-vidmode.h create mode 100644 src/gamma-w32gdi.c create mode 100644 src/gamma-w32gdi.h delete mode 100644 src/randr.c delete mode 100644 src/randr.h delete mode 100644 src/vidmode.c delete mode 100644 src/vidmode.h delete mode 100644 src/w32gdi.c delete mode 100644 src/w32gdi.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index c46c9ba..75a5261 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,16 +15,16 @@ redshift_SOURCES = \ systemtime.c systemtime.h EXTRA_redshift_SOURCES = \ - randr.c randr.h \ - vidmode.c vidmode.h \ - w32gdi.c w32gdi.h + gamma-randr.c gamma-randr.h \ + gamma-vidmode.c gamma-vidmode.h \ + gamma-w32gdi.c gamma-w32gdi.h AM_CFLAGS = redshift_LDADD = @LIBINTL@ EXTRA_DIST = if ENABLE_RANDR -redshift_SOURCES += randr.c randr.h +redshift_SOURCES += gamma-randr.c gamma-randr.h AM_CFLAGS += $(XCB_CFLAGS) $(XCB_RANDR_CFLAGS) redshift_LDADD += \ $(XCB_LIBS) $(XCB_CFLAGS) \ @@ -32,7 +32,7 @@ redshift_LDADD += \ endif if ENABLE_VIDMODE -redshift_SOURCES += vidmode.c vidmode.h +redshift_SOURCES += gamma-vidmode.c gamma-vidmode.h AM_CFLAGS += $(X11_CFLAGS) $(XF86VM_CFLAGS) redshift_LDADD += \ $(X11_LIBS) $(X11_CFLAGS) \ @@ -40,6 +40,6 @@ redshift_LDADD += \ endif if ENABLE_WINGDI -redshift_SOURCES += w32gdi.c w32gdi.h +redshift_SOURCES += gamma-w32gdi.c gamma-w32gdi.h redshift_LDADD += -lgdi32 endif diff --git a/src/gamma-randr.c b/src/gamma-randr.c new file mode 100644 index 0000000..ec70746 --- /dev/null +++ b/src/gamma-randr.c @@ -0,0 +1,367 @@ +/* gamma-randr.c -- X RANDR gamma adjustment source + This file is part of Redshift. + + Redshift is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Redshift is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Redshift. If not, see . + + Copyright (c) 2010 Jon Lund Steffensen +*/ + +#include +#include +#include +#include + +#ifdef ENABLE_NLS +# include +# define _(s) gettext(s) +#else +# define _(s) s +#endif + +#include +#include + +#include "gamma-randr.h" +#include "colorramp.h" + + +#define RANDR_VERSION_MAJOR 1 +#define RANDR_VERSION_MINOR 3 + + +int +randr_init(randr_state_t *state, char *args) +{ + int screen_num = -1; + int crtc_num = -1; + + /* Parse arguments. */ + while (args != NULL) { + char *next_arg = strchr(args, ':'); + if (next_arg != NULL) *(next_arg++) = '\0'; + + char *value = strchr(args, '='); + if (value != NULL) *(value++) = '\0'; + + if (strcasecmp(args, "screen") == 0) { + if (value == NULL) { + fprintf(stderr, _("Missing value for" + " parameter: `%s'.\n"), + args); + return -1; + } + screen_num = atoi(value); + } else if (strcasecmp(args, "crtc") == 0) { + if (value == NULL) { + fprintf(stderr, _("Missing value for" + " parameter: `%s'.\n"), + args); + return -1; + } + crtc_num = atoi(value); + } else { + fprintf(stderr, _("Unknown method parameter: `%s'.\n"), + args); + return -1; + } + + args = next_arg; + } + + xcb_generic_error_t *error; + + /* Open X server connection */ + int preferred_screen; + state->conn = xcb_connect(NULL, &preferred_screen); + + if (screen_num < 0) screen_num = preferred_screen; + + /* Query RandR version */ + xcb_randr_query_version_cookie_t ver_cookie = + xcb_randr_query_version(state->conn, RANDR_VERSION_MAJOR, + RANDR_VERSION_MINOR); + xcb_randr_query_version_reply_t *ver_reply = + xcb_randr_query_version_reply(state->conn, ver_cookie, &error); + + if (error) { + fprintf(stderr, _("`%s' returned error %d\n"), + "RANDR Query Version", error->error_code); + xcb_disconnect(state->conn); + return -1; + } + + 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); + free(ver_reply); + xcb_disconnect(state->conn); + return -1; + } + + free(ver_reply); + + /* Get screen */ + const xcb_setup_t *setup = xcb_get_setup(state->conn); + xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup); + state->screen = NULL; + + for (int i = 0; iter.rem > 0; i++) { + if (i == screen_num) { + state->screen = iter.data; + break; + } + xcb_screen_next(&iter); + } + + if (state->screen == NULL) { + fprintf(stderr, _("Screen %i could not be found.\n"), + screen_num); + xcb_disconnect(state->conn); + return -1; + } + + /* Get list of CRTCs for the screen */ + xcb_randr_get_screen_resources_current_cookie_t res_cookie = + xcb_randr_get_screen_resources_current(state->conn, + state->screen->root); + xcb_randr_get_screen_resources_current_reply_t *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); + xcb_disconnect(state->conn); + return -1; + } + + state->crtc_num = crtc_num; + state->crtc_count = res_reply->num_crtcs; + state->crtcs = malloc(state->crtc_count * sizeof(randr_crtc_state_t)); + if (state->crtcs == NULL) { + perror("malloc"); + xcb_disconnect(state->conn); + return -1; + } + + xcb_randr_crtc_t *crtcs = + xcb_randr_get_screen_resources_current_crtcs(res_reply); + + /* Save CRTC identifier in state */ + for (int i = 0; i < state->crtc_count; i++) { + state->crtcs[i].crtc = crtcs[i]; + } + + free(res_reply); + + /* Save size and gamma ramps of all CRTCs. + Current gamma ramps are saved so we can restore them + at program exit. */ + for (int i = 0; i < state->crtc_count; i++) { + xcb_randr_crtc_t crtc = state->crtcs[i].crtc; + + /* Request size of gamma ramps */ + xcb_randr_get_crtc_gamma_size_cookie_t gamma_size_cookie = + xcb_randr_get_crtc_gamma_size(state->conn, crtc); + xcb_randr_get_crtc_gamma_size_reply_t *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); + xcb_disconnect(state->conn); + return -1; + } + + unsigned int ramp_size = gamma_size_reply->size; + state->crtcs[i].ramp_size = ramp_size; + + free(gamma_size_reply); + + if (ramp_size == 0) { + fprintf(stderr, _("Gamma ramp size too small: %i\n"), + ramp_size); + xcb_disconnect(state->conn); + return -1; + } + + /* Request current gamma ramps */ + xcb_randr_get_crtc_gamma_cookie_t gamma_get_cookie = + xcb_randr_get_crtc_gamma(state->conn, crtc); + xcb_randr_get_crtc_gamma_reply_t *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); + xcb_disconnect(state->conn); + return -1; + } + + uint16_t *gamma_r = + xcb_randr_get_crtc_gamma_red(gamma_get_reply); + uint16_t *gamma_g = + xcb_randr_get_crtc_gamma_green(gamma_get_reply); + uint16_t *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); + xcb_disconnect(state->conn); + return -1; + } + + /* 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)); + + free(gamma_get_reply); + } + + return 0; +} + +void +randr_restore(randr_state_t *state) +{ + xcb_generic_error_t *error; + + /* Restore CRTC gamma ramps */ + for (int 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; + uint16_t *gamma_r = &state->crtcs[i].saved_ramps[0*ramp_size]; + uint16_t *gamma_g = &state->crtcs[i].saved_ramps[1*ramp_size]; + uint16_t *gamma_b = &state->crtcs[i].saved_ramps[2*ramp_size]; + + /* Set gamma ramps */ + xcb_void_cookie_t 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); + fprintf(stderr, _("Unable to restore CRTC %i\n"), i); + } + } +} + +void +randr_free(randr_state_t *state) +{ + /* Free CRTC state */ + for (int i = 0; i < state->crtc_count; i++) { + free(state->crtcs[i].saved_ramps); + } + free(state->crtcs); + + /* Close connection */ + xcb_disconnect(state->conn); +} + +static int +randr_set_temperature_for_crtc(randr_state_t *state, int crtc_num, int temp, + float gamma[3]) +{ + xcb_generic_error_t *error; + + if (crtc_num >= state->crtc_count || crtc_num < 0) { + fprintf(stderr, _("CRTC %d does not exist. "), + state->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; + } + + xcb_randr_crtc_t crtc = state->crtcs[crtc_num].crtc; + unsigned int ramp_size = state->crtcs[crtc_num].ramp_size; + + /* Create new gamma ramps */ + uint16_t *gamma_ramps = malloc(3*ramp_size*sizeof(uint16_t)); + if (gamma_ramps == NULL) { + perror("malloc"); + return -1; + } + + uint16_t *gamma_r = &gamma_ramps[0*ramp_size]; + uint16_t *gamma_g = &gamma_ramps[1*ramp_size]; + uint16_t *gamma_b = &gamma_ramps[2*ramp_size]; + + colorramp_fill(gamma_r, gamma_g, gamma_b, ramp_size, + temp, gamma); + + /* Set new gamma ramps */ + xcb_void_cookie_t 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); + free(gamma_ramps); + return -1; + } + + free(gamma_ramps); + + return 0; +} + +int +randr_set_temperature(randr_state_t *state, int temp, float gamma[3]) +{ + int r; + + /* If no CRTC number has been specified, + set temperature on all CRTCs. */ + if (state->crtc_num < 0) { + for (int i = 0; i < state->crtc_count; i++) { + r = randr_set_temperature_for_crtc(state, i, + temp, gamma); + if (r < 0) return -1; + } + } else { + return randr_set_temperature_for_crtc(state, state->crtc_num, + temp, gamma); + } + + return 0; +} diff --git a/src/gamma-randr.h b/src/gamma-randr.h new file mode 100644 index 0000000..307bdd2 --- /dev/null +++ b/src/gamma-randr.h @@ -0,0 +1,52 @@ +/* gamma-randr.h -- X RANDR gamma adjustment header + This file is part of Redshift. + + Redshift is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Redshift is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Redshift. If not, see . + + Copyright (c) 2010 Jon Lund Steffensen +*/ + +#ifndef _REDSHIFT_GAMMA_RANDR_H +#define _REDSHIFT_GAMMA_RANDR_H + +#include + +#include +#include + +#include "redshift.h" + + +typedef struct { + xcb_randr_crtc_t crtc; + unsigned int ramp_size; + uint16_t *saved_ramps; +} randr_crtc_state_t; + +typedef struct { + xcb_connection_t *conn; + xcb_screen_t *screen; + int crtc_num; + unsigned int crtc_count; + randr_crtc_state_t *crtcs; +} randr_state_t; + + +int randr_init(randr_state_t *state, char *args); +void randr_free(randr_state_t *state); +void randr_restore(randr_state_t *state); +int randr_set_temperature(randr_state_t *state, int temp, float gamma[3]); + + +#endif /* ! _REDSHIFT_GAMMA_RANDR_H */ diff --git a/src/gamma-vidmode.c b/src/gamma-vidmode.c new file mode 100644 index 0000000..f9be408 --- /dev/null +++ b/src/gamma-vidmode.c @@ -0,0 +1,195 @@ +/* gamma-vidmode.c -- X VidMode gamma adjustment source + This file is part of Redshift. + + Redshift is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Redshift is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Redshift. If not, see . + + Copyright (c) 2010 Jon Lund Steffensen +*/ + +#include +#include +#include +#include + +#ifdef ENABLE_NLS +# include +# define _(s) gettext(s) +#else +# define _(s) s +#endif + +#include +#include + +#include "gamma-vidmode.h" +#include "colorramp.h" + + +int +vidmode_init(vidmode_state_t *state, char *args) +{ + int screen_num = -1; + + /* Parse arguments. */ + while (args != NULL) { + char *next_arg = strchr(args, ':'); + if (next_arg != NULL) *(next_arg++) = '\0'; + + char *value = strchr(args, '='); + if (value != NULL) *(value++) = '\0'; + + if (strcasecmp(args, "screen") == 0) { + if (value == NULL) { + fprintf(stderr, _("Missing value for" + " parameter: `%s'.\n"), + args); + return -1; + } + screen_num = atoi(value); + } else { + fprintf(stderr, _("Unknown method parameter: `%s'.\n"), + args); + return -1; + } + + args = next_arg; + } + + int r; + + /* Open display */ + state->display = XOpenDisplay(NULL); + if (state->display == NULL) { + fprintf(stderr, _("X request failed: %s\n"), + "XOpenDisplay"); + return -1; + } + + if (screen_num < 0) screen_num = DefaultScreen(state->display); + state->screen_num = screen_num; + + /* Query extension version */ + int major, minor; + r = XF86VidModeQueryVersion(state->display, &major, &minor); + if (!r) { + fprintf(stderr, _("X request failed: %s\n"), + "XF86VidModeQueryVersion"); + XCloseDisplay(state->display); + return -1; + } + + /* Request size of gamma ramps */ + r = XF86VidModeGetGammaRampSize(state->display, state->screen_num, + &state->ramp_size); + if (!r) { + fprintf(stderr, _("X request failed: %s\n"), + "XF86VidModeGetGammaRampSize"); + XCloseDisplay(state->display); + return -1; + } + + if (state->ramp_size == 0) { + fprintf(stderr, _("Gamma ramp size too small: %i\n"), + state->ramp_size); + XCloseDisplay(state->display); + 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"); + XCloseDisplay(state->display); + return -1; + } + + 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]; + + /* 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); + if (!r) { + fprintf(stderr, _("X request failed: %s\n"), + "XF86VidModeGetGammaRamp"); + XCloseDisplay(state->display); + return -1; + } + + return 0; +} + +void +vidmode_free(vidmode_state_t *state) +{ + /* Free saved ramps */ + free(state->saved_ramps); + + /* Close display connection */ + XCloseDisplay(state->display); +} + +void +vidmode_restore(vidmode_state_t *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]; + + /* 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"); + } +} + +int +vidmode_set_temperature(vidmode_state_t *state, int temp, float gamma[3]) +{ + int r; + + /* Create new gamma ramps */ + uint16_t *gamma_ramps = malloc(3*state->ramp_size*sizeof(uint16_t)); + if (gamma_ramps == NULL) { + perror("malloc"); + return -1; + } + + uint16_t *gamma_r = &gamma_ramps[0*state->ramp_size]; + uint16_t *gamma_g = &gamma_ramps[1*state->ramp_size]; + uint16_t *gamma_b = &gamma_ramps[2*state->ramp_size]; + + colorramp_fill(gamma_r, gamma_g, gamma_b, state->ramp_size, + temp, gamma); + + /* Set new gamma ramps */ + 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"); + free(gamma_ramps); + return -1; + } + + free(gamma_ramps); + + return 0; +} diff --git a/src/gamma-vidmode.h b/src/gamma-vidmode.h new file mode 100644 index 0000000..7c351c9 --- /dev/null +++ b/src/gamma-vidmode.h @@ -0,0 +1,40 @@ +/* gamma-vidmode.h -- X VidMode gamma adjustment header + This file is part of Redshift. + + Redshift is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Redshift is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Redshift. If not, see . + + Copyright (c) 2010 Jon Lund Steffensen +*/ + +#ifndef _REDSHIFT_GAMMA_VIDMODE_H +#define _REDSHIFT_GAMMA_VIDMODE_H + +#include + +#include + +typedef struct { + Display *display; + int screen_num; + int ramp_size; + uint16_t *saved_ramps; +} vidmode_state_t; + +int vidmode_init(vidmode_state_t *state, char *args); +void vidmode_free(vidmode_state_t *state); +void vidmode_restore(vidmode_state_t *state); +int vidmode_set_temperature(vidmode_state_t *state, int temp, float gamma[3]); + + +#endif /* ! _REDSHIFT_GAMMA_VIDMODE_H */ diff --git a/src/gamma-w32gdi.c b/src/gamma-w32gdi.c new file mode 100644 index 0000000..d1b6533 --- /dev/null +++ b/src/gamma-w32gdi.c @@ -0,0 +1,127 @@ +/* gamma-w32gdi.c -- Windows GDI gamma adjustment source + This file is part of Redshift. + + Redshift is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Redshift is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Redshift. If not, see . + + Copyright (c) 2010 Jon Lund Steffensen +*/ + +#include +#include + +#define WINVER 0x0500 +#include +#include + +#ifdef ENABLE_NLS +# include +# define _(s) gettext(s) +#else +# define _(s) s +#endif + +#include "w32gdi.h" +#include "colorramp.h" + +#define GAMMA_RAMP_SIZE 256 + + +int +w32gdi_init(w32gdi_state_t *state, char *args) +{ + BOOL r; + + /* Open device context */ + state->hDC = GetDC(NULL); + if (state->hDC == NULL) { + fputs(_("Unable to open device context.\n"), stderr); + return -1; + } + + /* Check support for gamma ramps */ + int cmcap = GetDeviceCaps(state->hDC, COLORMGMTCAPS); + if (cmcap != CM_GAMMA_RAMP) { + fputs(_("Display device does not support gamma ramps.\n"), + stderr); + 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, state->hDC); + return -1; + } + + /* Save current gamma ramps so we can restore them at program exit */ + r = GetDeviceGammaRamp(state->hDC, state->saved_ramps); + if (!r) { + fputs(_("Unable to save current gamma ramp.\n"), stderr); + ReleaseDC(NULL, state->hDC); + return -1; + } + + return 0; +} + +void +w32gdi_free(w32gdi_state_t *state) +{ + /* Free saved ramps */ + free(state->saved_ramps); + + /* Release device context */ + ReleaseDC(NULL, state->hDC); +} + +void +w32gdi_restore(w32gdi_state_t *state) +{ + /* Restore gamma ramps */ + BOOL r = SetDeviceGammaRamp(state->hDC, state->saved_ramps); + if (!r) fputs(_("Unable to restore gamma ramps.\n"), stderr); +} + +int +w32gdi_set_temperature(w32gdi_state_t *state, int temp, float gamma[3]) +{ + BOOL r; + + /* Create new gamma ramps */ + WORD *gamma_ramps = malloc(3*GAMMA_RAMP_SIZE*sizeof(WORD)); + if (gamma_ramps == NULL) { + perror("malloc"); + return -1; + } + + 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]; + + colorramp_fill(gamma_r, gamma_g, gamma_b, GAMMA_RAMP_SIZE, + temp, gamma); + + /* Set new gamma ramps */ + r = SetDeviceGammaRamp(state->hDC, gamma_ramps); + if (!r) { + fputs(_("Unable to set gamma ramps.\n"), stderr); + free(gamma_ramps); + return -1; + } + + free(gamma_ramps); + + return 0; +} diff --git a/src/gamma-w32gdi.h b/src/gamma-w32gdi.h new file mode 100644 index 0000000..48787da --- /dev/null +++ b/src/gamma-w32gdi.h @@ -0,0 +1,38 @@ +/* gamma-w32gdi.h -- Windows GDI gamma adjustment header + This file is part of Redshift. + + Redshift is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Redshift is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Redshift. If not, see . + + Copyright (c) 2010 Jon Lund Steffensen +*/ + +#ifndef _REDSHIFT_GAMMA_W32GDI_H +#define _REDSHIFT_GAMMA_W32GDI_H + +#include +#include + + +typedef struct { + HDC hDC; + WORD *saved_ramps; +} w32gdi_state_t; + +int w32gdi_init(w32gdi_state_t *state, char *args); +void w32gdi_free(w32gdi_state_t *state); +void w32gdi_restore(w32gdi_state_t *state); +int w32gdi_set_temperature(w32gdi_state_t *state, int temp, float gamma[3]); + + +#endif /* ! _REDSHIFT_GAMMA_W32GDI_H */ diff --git a/src/randr.c b/src/randr.c deleted file mode 100644 index 4ee1756..0000000 --- a/src/randr.c +++ /dev/null @@ -1,367 +0,0 @@ -/* randr.c -- X RandR gamma adjustment source - This file is part of Redshift. - - Redshift is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Redshift is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Redshift. If not, see . - - Copyright (c) 2010 Jon Lund Steffensen -*/ - -#include -#include -#include -#include - -#ifdef ENABLE_NLS -# include -# define _(s) gettext(s) -#else -# define _(s) s -#endif - -#include -#include - -#include "randr.h" -#include "colorramp.h" - - -#define RANDR_VERSION_MAJOR 1 -#define RANDR_VERSION_MINOR 3 - - -int -randr_init(randr_state_t *state, char *args) -{ - int screen_num = -1; - int crtc_num = -1; - - /* Parse arguments. */ - while (args != NULL) { - char *next_arg = strchr(args, ':'); - if (next_arg != NULL) *(next_arg++) = '\0'; - - char *value = strchr(args, '='); - if (value != NULL) *(value++) = '\0'; - - if (strcasecmp(args, "screen") == 0) { - if (value == NULL) { - fprintf(stderr, _("Missing value for" - " parameter: `%s'.\n"), - args); - return -1; - } - screen_num = atoi(value); - } else if (strcasecmp(args, "crtc") == 0) { - if (value == NULL) { - fprintf(stderr, _("Missing value for" - " parameter: `%s'.\n"), - args); - return -1; - } - crtc_num = atoi(value); - } else { - fprintf(stderr, _("Unknown method parameter: `%s'.\n"), - args); - return -1; - } - - args = next_arg; - } - - xcb_generic_error_t *error; - - /* Open X server connection */ - int preferred_screen; - state->conn = xcb_connect(NULL, &preferred_screen); - - if (screen_num < 0) screen_num = preferred_screen; - - /* Query RandR version */ - xcb_randr_query_version_cookie_t ver_cookie = - xcb_randr_query_version(state->conn, RANDR_VERSION_MAJOR, - RANDR_VERSION_MINOR); - xcb_randr_query_version_reply_t *ver_reply = - xcb_randr_query_version_reply(state->conn, ver_cookie, &error); - - if (error) { - fprintf(stderr, _("`%s' returned error %d\n"), - "RANDR Query Version", error->error_code); - xcb_disconnect(state->conn); - return -1; - } - - 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); - free(ver_reply); - xcb_disconnect(state->conn); - return -1; - } - - free(ver_reply); - - /* Get screen */ - const xcb_setup_t *setup = xcb_get_setup(state->conn); - xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup); - state->screen = NULL; - - for (int i = 0; iter.rem > 0; i++) { - if (i == screen_num) { - state->screen = iter.data; - break; - } - xcb_screen_next(&iter); - } - - if (state->screen == NULL) { - fprintf(stderr, _("Screen %i could not be found.\n"), - screen_num); - xcb_disconnect(state->conn); - return -1; - } - - /* Get list of CRTCs for the screen */ - xcb_randr_get_screen_resources_current_cookie_t res_cookie = - xcb_randr_get_screen_resources_current(state->conn, - state->screen->root); - xcb_randr_get_screen_resources_current_reply_t *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); - xcb_disconnect(state->conn); - return -1; - } - - state->crtc_num = crtc_num; - state->crtc_count = res_reply->num_crtcs; - state->crtcs = malloc(state->crtc_count * sizeof(randr_crtc_state_t)); - if (state->crtcs == NULL) { - perror("malloc"); - xcb_disconnect(state->conn); - return -1; - } - - xcb_randr_crtc_t *crtcs = - xcb_randr_get_screen_resources_current_crtcs(res_reply); - - /* Save CRTC identifier in state */ - for (int i = 0; i < state->crtc_count; i++) { - state->crtcs[i].crtc = crtcs[i]; - } - - free(res_reply); - - /* Save size and gamma ramps of all CRTCs. - Current gamma ramps are saved so we can restore them - at program exit. */ - for (int i = 0; i < state->crtc_count; i++) { - xcb_randr_crtc_t crtc = state->crtcs[i].crtc; - - /* Request size of gamma ramps */ - xcb_randr_get_crtc_gamma_size_cookie_t gamma_size_cookie = - xcb_randr_get_crtc_gamma_size(state->conn, crtc); - xcb_randr_get_crtc_gamma_size_reply_t *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); - xcb_disconnect(state->conn); - return -1; - } - - unsigned int ramp_size = gamma_size_reply->size; - state->crtcs[i].ramp_size = ramp_size; - - free(gamma_size_reply); - - if (ramp_size == 0) { - fprintf(stderr, _("Gamma ramp size too small: %i\n"), - ramp_size); - xcb_disconnect(state->conn); - return -1; - } - - /* Request current gamma ramps */ - xcb_randr_get_crtc_gamma_cookie_t gamma_get_cookie = - xcb_randr_get_crtc_gamma(state->conn, crtc); - xcb_randr_get_crtc_gamma_reply_t *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); - xcb_disconnect(state->conn); - return -1; - } - - uint16_t *gamma_r = - xcb_randr_get_crtc_gamma_red(gamma_get_reply); - uint16_t *gamma_g = - xcb_randr_get_crtc_gamma_green(gamma_get_reply); - uint16_t *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); - xcb_disconnect(state->conn); - return -1; - } - - /* 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)); - - free(gamma_get_reply); - } - - return 0; -} - -void -randr_restore(randr_state_t *state) -{ - xcb_generic_error_t *error; - - /* Restore CRTC gamma ramps */ - for (int 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; - uint16_t *gamma_r = &state->crtcs[i].saved_ramps[0*ramp_size]; - uint16_t *gamma_g = &state->crtcs[i].saved_ramps[1*ramp_size]; - uint16_t *gamma_b = &state->crtcs[i].saved_ramps[2*ramp_size]; - - /* Set gamma ramps */ - xcb_void_cookie_t 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); - fprintf(stderr, _("Unable to restore CRTC %i\n"), i); - } - } -} - -void -randr_free(randr_state_t *state) -{ - /* Free CRTC state */ - for (int i = 0; i < state->crtc_count; i++) { - free(state->crtcs[i].saved_ramps); - } - free(state->crtcs); - - /* Close connection */ - xcb_disconnect(state->conn); -} - -static int -randr_set_temperature_for_crtc(randr_state_t *state, int crtc_num, int temp, - float gamma[3]) -{ - xcb_generic_error_t *error; - - if (crtc_num >= state->crtc_count || crtc_num < 0) { - fprintf(stderr, _("CRTC %d does not exist. "), - state->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; - } - - xcb_randr_crtc_t crtc = state->crtcs[crtc_num].crtc; - unsigned int ramp_size = state->crtcs[crtc_num].ramp_size; - - /* Create new gamma ramps */ - uint16_t *gamma_ramps = malloc(3*ramp_size*sizeof(uint16_t)); - if (gamma_ramps == NULL) { - perror("malloc"); - return -1; - } - - uint16_t *gamma_r = &gamma_ramps[0*ramp_size]; - uint16_t *gamma_g = &gamma_ramps[1*ramp_size]; - uint16_t *gamma_b = &gamma_ramps[2*ramp_size]; - - colorramp_fill(gamma_r, gamma_g, gamma_b, ramp_size, - temp, gamma); - - /* Set new gamma ramps */ - xcb_void_cookie_t 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); - free(gamma_ramps); - return -1; - } - - free(gamma_ramps); - - return 0; -} - -int -randr_set_temperature(randr_state_t *state, int temp, float gamma[3]) -{ - int r; - - /* If no CRTC number has been specified, - set temperature on all CRTCs. */ - if (state->crtc_num < 0) { - for (int i = 0; i < state->crtc_count; i++) { - r = randr_set_temperature_for_crtc(state, i, - temp, gamma); - if (r < 0) return -1; - } - } else { - return randr_set_temperature_for_crtc(state, state->crtc_num, - temp, gamma); - } - - return 0; -} diff --git a/src/randr.h b/src/randr.h deleted file mode 100644 index 3ad83bf..0000000 --- a/src/randr.h +++ /dev/null @@ -1,52 +0,0 @@ -/* randr.h -- X RandR gamma adjustment header - This file is part of Redshift. - - Redshift is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Redshift is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Redshift. If not, see . - - Copyright (c) 2010 Jon Lund Steffensen -*/ - -#ifndef _REDSHIFT_RANDR_H -#define _REDSHIFT_RANDR_H - -#include - -#include -#include - -#include "redshift.h" - - -typedef struct { - xcb_randr_crtc_t crtc; - unsigned int ramp_size; - uint16_t *saved_ramps; -} randr_crtc_state_t; - -typedef struct { - xcb_connection_t *conn; - xcb_screen_t *screen; - int crtc_num; - unsigned int crtc_count; - randr_crtc_state_t *crtcs; -} randr_state_t; - - -int randr_init(randr_state_t *state, char *args); -void randr_free(randr_state_t *state); -void randr_restore(randr_state_t *state); -int randr_set_temperature(randr_state_t *state, int temp, float gamma[3]); - - -#endif /* ! _REDSHIFT_RANDR_H */ diff --git a/src/redshift.c b/src/redshift.c index 869714f..1364527 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -55,15 +55,15 @@ #endif #ifdef ENABLE_RANDR -# include "randr.h" +# include "gamma-randr.h" #endif #ifdef ENABLE_VIDMODE -# include "vidmode.h" +# include "gamma-vidmode.h" #endif #ifdef ENABLE_WINGDI -# include "w32gdi.h" +# include "gamma-w32gdi.h" #endif @@ -82,7 +82,7 @@ typedef union { /* Gamma adjustment method structs */ -static const gamma_method_spec_t gamma_methods[] = { +static const gamma_method_t gamma_methods[] = { #ifdef ENABLE_RANDR { "RANDR", @@ -273,7 +273,7 @@ main(int argc, char *argv[]) int temp_night = DEFAULT_NIGHT_TEMP; float gamma[3] = { DEFAULT_GAMMA, DEFAULT_GAMMA, DEFAULT_GAMMA }; - const gamma_method_spec_t *method = NULL; + const gamma_method_t *method = NULL; char *method_args = NULL; int transition = 1; @@ -343,7 +343,7 @@ main(int argc, char *argv[]) /* Lookup argument in gamma methods table */ for (int i = 0; gamma_methods[i].name != NULL; i++) { - const gamma_method_spec_t *m = + const gamma_method_t *m = &gamma_methods[i]; if (strcasecmp(optarg, m->name) == 0) { method = m; @@ -463,7 +463,7 @@ main(int argc, char *argv[]) } else { /* Try all methods, use the first that works. */ for (int i = 0; gamma_methods[i].name != NULL; i++) { - const gamma_method_spec_t *m = &gamma_methods[i]; + const gamma_method_t *m = &gamma_methods[i]; r = m->init(&state, method_args); if (r < 0) { fprintf(stderr, _("Initialization of %s" diff --git a/src/redshift.h b/src/redshift.h index 3cfe738..38e7f6b 100644 --- a/src/redshift.h +++ b/src/redshift.h @@ -33,7 +33,7 @@ typedef struct { gamma_method_free_func *free; gamma_method_restore_func *restore; gamma_method_set_temperature_func *set_temperature; -} gamma_method_spec_t; +} gamma_method_t; #endif /* ! _REDSHIFT_REDSHIFT_H */ diff --git a/src/vidmode.c b/src/vidmode.c deleted file mode 100644 index df66f8c..0000000 --- a/src/vidmode.c +++ /dev/null @@ -1,195 +0,0 @@ -/* vidmode.c -- X VidMode gamma adjustment source - This file is part of Redshift. - - Redshift is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Redshift is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Redshift. If not, see . - - Copyright (c) 2010 Jon Lund Steffensen -*/ - -#include -#include -#include -#include - -#ifdef ENABLE_NLS -# include -# define _(s) gettext(s) -#else -# define _(s) s -#endif - -#include -#include - -#include "vidmode.h" -#include "colorramp.h" - - -int -vidmode_init(vidmode_state_t *state, char *args) -{ - int screen_num = -1; - - /* Parse arguments. */ - while (args != NULL) { - char *next_arg = strchr(args, ':'); - if (next_arg != NULL) *(next_arg++) = '\0'; - - char *value = strchr(args, '='); - if (value != NULL) *(value++) = '\0'; - - if (strcasecmp(args, "screen") == 0) { - if (value == NULL) { - fprintf(stderr, _("Missing value for" - " parameter: `%s'.\n"), - args); - return -1; - } - screen_num = atoi(value); - } else { - fprintf(stderr, _("Unknown method parameter: `%s'.\n"), - args); - return -1; - } - - args = next_arg; - } - - int r; - - /* Open display */ - state->display = XOpenDisplay(NULL); - if (state->display == NULL) { - fprintf(stderr, _("X request failed: %s\n"), - "XOpenDisplay"); - return -1; - } - - if (screen_num < 0) screen_num = DefaultScreen(state->display); - state->screen_num = screen_num; - - /* Query extension version */ - int major, minor; - r = XF86VidModeQueryVersion(state->display, &major, &minor); - if (!r) { - fprintf(stderr, _("X request failed: %s\n"), - "XF86VidModeQueryVersion"); - XCloseDisplay(state->display); - return -1; - } - - /* Request size of gamma ramps */ - r = XF86VidModeGetGammaRampSize(state->display, state->screen_num, - &state->ramp_size); - if (!r) { - fprintf(stderr, _("X request failed: %s\n"), - "XF86VidModeGetGammaRampSize"); - XCloseDisplay(state->display); - return -1; - } - - if (state->ramp_size == 0) { - fprintf(stderr, _("Gamma ramp size too small: %i\n"), - state->ramp_size); - XCloseDisplay(state->display); - 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"); - XCloseDisplay(state->display); - return -1; - } - - 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]; - - /* 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); - if (!r) { - fprintf(stderr, _("X request failed: %s\n"), - "XF86VidModeGetGammaRamp"); - XCloseDisplay(state->display); - return -1; - } - - return 0; -} - -void -vidmode_free(vidmode_state_t *state) -{ - /* Free saved ramps */ - free(state->saved_ramps); - - /* Close display connection */ - XCloseDisplay(state->display); -} - -void -vidmode_restore(vidmode_state_t *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]; - - /* 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"); - } -} - -int -vidmode_set_temperature(vidmode_state_t *state, int temp, float gamma[3]) -{ - int r; - - /* Create new gamma ramps */ - uint16_t *gamma_ramps = malloc(3*state->ramp_size*sizeof(uint16_t)); - if (gamma_ramps == NULL) { - perror("malloc"); - return -1; - } - - uint16_t *gamma_r = &gamma_ramps[0*state->ramp_size]; - uint16_t *gamma_g = &gamma_ramps[1*state->ramp_size]; - uint16_t *gamma_b = &gamma_ramps[2*state->ramp_size]; - - colorramp_fill(gamma_r, gamma_g, gamma_b, state->ramp_size, - temp, gamma); - - /* Set new gamma ramps */ - 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"); - free(gamma_ramps); - return -1; - } - - free(gamma_ramps); - - return 0; -} diff --git a/src/vidmode.h b/src/vidmode.h deleted file mode 100644 index 2b6796d..0000000 --- a/src/vidmode.h +++ /dev/null @@ -1,40 +0,0 @@ -/* vidmode.h -- X VidMode gamma adjustment header - This file is part of Redshift. - - Redshift is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Redshift is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Redshift. If not, see . - - Copyright (c) 2010 Jon Lund Steffensen -*/ - -#ifndef _REDSHIFT_VIDMODE_H -#define _REDSHIFT_VIDMODE_H - -#include - -#include - -typedef struct { - Display *display; - int screen_num; - int ramp_size; - uint16_t *saved_ramps; -} vidmode_state_t; - -int vidmode_init(vidmode_state_t *state, char *args); -void vidmode_free(vidmode_state_t *state); -void vidmode_restore(vidmode_state_t *state); -int vidmode_set_temperature(vidmode_state_t *state, int temp, float gamma[3]); - - -#endif /* ! _REDSHIFT_VIDMODE_H */ diff --git a/src/w32gdi.c b/src/w32gdi.c deleted file mode 100644 index f15746e..0000000 --- a/src/w32gdi.c +++ /dev/null @@ -1,127 +0,0 @@ -/* w32gdi.c -- Windows GDI gamma adjustment source - This file is part of Redshift. - - Redshift is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Redshift is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Redshift. If not, see . - - Copyright (c) 2010 Jon Lund Steffensen -*/ - -#include -#include - -#define WINVER 0x0500 -#include -#include - -#ifdef ENABLE_NLS -# include -# define _(s) gettext(s) -#else -# define _(s) s -#endif - -#include "w32gdi.h" -#include "colorramp.h" - -#define GAMMA_RAMP_SIZE 256 - - -int -w32gdi_init(w32gdi_state_t *state, char *args) -{ - BOOL r; - - /* Open device context */ - state->hDC = GetDC(NULL); - if (state->hDC == NULL) { - fputs(_("Unable to open device context.\n"), stderr); - return -1; - } - - /* Check support for gamma ramps */ - int cmcap = GetDeviceCaps(state->hDC, COLORMGMTCAPS); - if (cmcap != CM_GAMMA_RAMP) { - fputs(_("Display device does not support gamma ramps.\n"), - stderr); - 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, state->hDC); - return -1; - } - - /* Save current gamma ramps so we can restore them at program exit */ - r = GetDeviceGammaRamp(state->hDC, state->saved_ramps); - if (!r) { - fputs(_("Unable to save current gamma ramp.\n"), stderr); - ReleaseDC(NULL, state->hDC); - return -1; - } - - return 0; -} - -void -w32gdi_free(w32gdi_state_t *state) -{ - /* Free saved ramps */ - free(state->saved_ramps); - - /* Release device context */ - ReleaseDC(NULL, state->hDC); -} - -void -w32gdi_restore(w32gdi_state_t *state) -{ - /* Restore gamma ramps */ - BOOL r = SetDeviceGammaRamp(state->hDC, state->saved_ramps); - if (!r) fputs(_("Unable to restore gamma ramps.\n"), stderr); -} - -int -w32gdi_set_temperature(w32gdi_state_t *state, int temp, float gamma[3]) -{ - BOOL r; - - /* Create new gamma ramps */ - WORD *gamma_ramps = malloc(3*GAMMA_RAMP_SIZE*sizeof(WORD)); - if (gamma_ramps == NULL) { - perror("malloc"); - return -1; - } - - 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]; - - colorramp_fill(gamma_r, gamma_g, gamma_b, GAMMA_RAMP_SIZE, - temp, gamma); - - /* Set new gamma ramps */ - r = SetDeviceGammaRamp(state->hDC, gamma_ramps); - if (!r) { - fputs(_("Unable to set gamma ramps.\n"), stderr); - free(gamma_ramps); - return -1; - } - - free(gamma_ramps); - - return 0; -} diff --git a/src/w32gdi.h b/src/w32gdi.h deleted file mode 100644 index 99ba3ee..0000000 --- a/src/w32gdi.h +++ /dev/null @@ -1,38 +0,0 @@ -/* w32gdi.h -- Windows GDI gamma adjustment header - This file is part of Redshift. - - Redshift is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Redshift is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Redshift. If not, see . - - Copyright (c) 2010 Jon Lund Steffensen -*/ - -#ifndef _REDSHIFT_W32GDI_H -#define _REDSHIFT_W32GDI_H - -#include -#include - - -typedef struct { - HDC hDC; - WORD *saved_ramps; -} w32gdi_state_t; - -int w32gdi_init(w32gdi_state_t *state, char *args); -void w32gdi_free(w32gdi_state_t *state); -void w32gdi_restore(w32gdi_state_t *state); -int w32gdi_set_temperature(w32gdi_state_t *state, int temp, float gamma[3]); - - -#endif /* ! _REDSHIFT_W32GDI_H */ -- cgit v1.2.3-70-g09d2