1 /* gamma-w32gdi.c -- Windows GDI gamma adjustment source
2    This file is part of Redshift.
3 
4    Redshift is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    Redshift is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with Redshift.  If not, see <http://www.gnu.org/licenses/>.
16 
17    Copyright (c) 2010-2017  Jon Lund Steffensen <jonlst@gmail.com>
18 */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #ifndef WINVER
24 # define WINVER  0x0500
25 #endif
26 #include <windows.h>
27 #include <wingdi.h>
28 
29 #ifdef ENABLE_NLS
30 # include <libintl.h>
31 # define _(s) gettext(s)
32 #else
33 # define _(s) s
34 #endif
35 
36 #include "gamma-w32gdi.h"
37 #include "colorramp.h"
38 
39 #define GAMMA_RAMP_SIZE  256
40 #define MAX_ATTEMPTS  10
41 
42 
43 typedef struct {
44 	WORD *saved_ramps;
45 } w32gdi_state_t;
46 
47 
48 static int
w32gdi_init(w32gdi_state_t ** state)49 w32gdi_init(w32gdi_state_t **state)
50 {
51 	*state = malloc(sizeof(w32gdi_state_t));
52 	if (state == NULL) return -1;
53 
54 	w32gdi_state_t *s = *state;
55 	s->saved_ramps = NULL;
56 
57 	return 0;
58 }
59 
60 static int
w32gdi_start(w32gdi_state_t * state)61 w32gdi_start(w32gdi_state_t *state)
62 {
63 	BOOL r;
64 
65 	/* Open device context */
66 	HDC hDC = GetDC(NULL);
67 	if (hDC == NULL) {
68 		fputs(_("Unable to open device context.\n"), stderr);
69 		return -1;
70 	}
71 
72 	/* Check support for gamma ramps */
73 	int cmcap = GetDeviceCaps(hDC, COLORMGMTCAPS);
74 	if (cmcap != CM_GAMMA_RAMP) {
75 		fputs(_("Display device does not support gamma ramps.\n"),
76 		      stderr);
77 		return -1;
78 	}
79 
80 	/* Allocate space for saved gamma ramps */
81 	state->saved_ramps = malloc(3*GAMMA_RAMP_SIZE*sizeof(WORD));
82 	if (state->saved_ramps == NULL) {
83 		perror("malloc");
84 		ReleaseDC(NULL, hDC);
85 		return -1;
86 	}
87 
88 	/* Save current gamma ramps so we can restore them at program exit */
89 	r = GetDeviceGammaRamp(hDC, state->saved_ramps);
90 	if (!r) {
91 		fputs(_("Unable to save current gamma ramp.\n"), stderr);
92 		ReleaseDC(NULL, hDC);
93 		return -1;
94 	}
95 
96 	/* Release device context */
97 	ReleaseDC(NULL, hDC);
98 
99 	return 0;
100 }
101 
102 static void
w32gdi_free(w32gdi_state_t * state)103 w32gdi_free(w32gdi_state_t *state)
104 {
105 	/* Free saved ramps */
106 	free(state->saved_ramps);
107 
108 	free(state);
109 }
110 
111 
112 static void
w32gdi_print_help(FILE * f)113 w32gdi_print_help(FILE *f)
114 {
115 	fputs(_("Adjust gamma ramps with the Windows GDI.\n"), f);
116 	fputs("\n", f);
117 }
118 
119 static int
w32gdi_set_option(w32gdi_state_t * state,const char * key,const char * value)120 w32gdi_set_option(w32gdi_state_t *state, const char *key, const char *value)
121 {
122 	if (strcasecmp(key, "preserve") == 0) {
123 		fprintf(stderr, _("Parameter `%s` is now always on; "
124 				  " Use the `%s` command-line option"
125 				  " to disable.\n"),
126 			key, "-P");
127 	} else {
128 		fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
129 		return -1;
130 	}
131 
132 	return 0;
133 }
134 
135 static void
w32gdi_restore(w32gdi_state_t * state)136 w32gdi_restore(w32gdi_state_t *state)
137 {
138 	/* Open device context */
139 	HDC hDC = GetDC(NULL);
140 	if (hDC == NULL) {
141 		fputs(_("Unable to open device context.\n"), stderr);
142 		return;
143 	}
144 
145 	/* Restore gamma ramps */
146 	BOOL r = FALSE;
147 	for (int i = 0; i < MAX_ATTEMPTS && !r; i++) {
148 		/* We retry a few times before giving up because some
149 		   buggy drivers fail on the first invocation of
150 		   SetDeviceGammaRamp just to succeed on the second. */
151 		r = SetDeviceGammaRamp(hDC, state->saved_ramps);
152 	}
153 	if (!r) fputs(_("Unable to restore gamma ramps.\n"), stderr);
154 
155 	/* Release device context */
156 	ReleaseDC(NULL, hDC);
157 }
158 
159 static int
w32gdi_set_temperature(w32gdi_state_t * state,const color_setting_t * setting,int preserve)160 w32gdi_set_temperature(
161 	w32gdi_state_t *state, const color_setting_t *setting, int preserve)
162 {
163 	BOOL r;
164 
165 	/* Open device context */
166 	HDC hDC = GetDC(NULL);
167 	if (hDC == NULL) {
168 		fputs(_("Unable to open device context.\n"), stderr);
169 		return -1;
170 	}
171 
172 	/* Create new gamma ramps */
173 	WORD *gamma_ramps = malloc(3*GAMMA_RAMP_SIZE*sizeof(WORD));
174 	if (gamma_ramps == NULL) {
175 		perror("malloc");
176 		ReleaseDC(NULL, hDC);
177 		return -1;
178 	}
179 
180 	WORD *gamma_r = &gamma_ramps[0*GAMMA_RAMP_SIZE];
181 	WORD *gamma_g = &gamma_ramps[1*GAMMA_RAMP_SIZE];
182 	WORD *gamma_b = &gamma_ramps[2*GAMMA_RAMP_SIZE];
183 
184 	if (preserve) {
185 		/* Initialize gamma ramps from saved state */
186 		memcpy(gamma_ramps, state->saved_ramps,
187 		       3*GAMMA_RAMP_SIZE*sizeof(WORD));
188 	} else {
189 		/* Initialize gamma ramps to pure state */
190 		for (int i = 0; i < GAMMA_RAMP_SIZE; i++) {
191 			WORD value = (double)i/GAMMA_RAMP_SIZE *
192 				(UINT16_MAX+1);
193 			gamma_r[i] = value;
194 			gamma_g[i] = value;
195 			gamma_b[i] = value;
196 		}
197 	}
198 
199 	colorramp_fill(gamma_r, gamma_g, gamma_b, GAMMA_RAMP_SIZE,
200 		       setting);
201 
202 	/* Set new gamma ramps */
203 	r = FALSE;
204 	for (int i = 0; i < MAX_ATTEMPTS && !r; i++) {
205 		/* We retry a few times before giving up because some
206 		   buggy drivers fail on the first invocation of
207 		   SetDeviceGammaRamp just to succeed on the second. */
208 		r = SetDeviceGammaRamp(hDC, gamma_ramps);
209 	}
210 	if (!r) {
211 		fputs(_("Unable to set gamma ramps.\n"), stderr);
212 		free(gamma_ramps);
213 		ReleaseDC(NULL, hDC);
214 		return -1;
215 	}
216 
217 	free(gamma_ramps);
218 
219 	/* Release device context */
220 	ReleaseDC(NULL, hDC);
221 
222 	return 0;
223 }
224 
225 
226 const gamma_method_t w32gdi_gamma_method = {
227 	"wingdi", 1,
228 	(gamma_method_init_func *)w32gdi_init,
229 	(gamma_method_start_func *)w32gdi_start,
230 	(gamma_method_free_func *)w32gdi_free,
231 	(gamma_method_print_help_func *)w32gdi_print_help,
232 	(gamma_method_set_option_func *)w32gdi_set_option,
233 	(gamma_method_restore_func *)w32gdi_restore,
234 	(gamma_method_set_temperature_func *)w32gdi_set_temperature
235 };
236