1 /* gamma-vidmode.c -- X VidMode 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 <stdlib.h>
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <string.h>
24
25 #ifdef ENABLE_NLS
26 # include <libintl.h>
27 # define _(s) gettext(s)
28 #else
29 # define _(s) s
30 #endif
31
32 #include <X11/Xlib.h>
33 #include <X11/extensions/xf86vmode.h>
34
35 #include "gamma-vidmode.h"
36 #include "redshift.h"
37 #include "colorramp.h"
38
39
40 typedef struct {
41 Display *display;
42 int screen_num;
43 int ramp_size;
44 uint16_t *saved_ramps;
45 } vidmode_state_t;
46
47
48 static int
vidmode_init(vidmode_state_t ** state)49 vidmode_init(vidmode_state_t **state)
50 {
51 *state = malloc(sizeof(vidmode_state_t));
52 if (*state == NULL) return -1;
53
54 vidmode_state_t *s = *state;
55 s->screen_num = -1;
56 s->saved_ramps = NULL;
57
58 /* Open display */
59 s->display = XOpenDisplay(NULL);
60 if (s->display == NULL) {
61 fprintf(stderr, _("X request failed: %s\n"), "XOpenDisplay");
62 return -1;
63 }
64
65 return 0;
66 }
67
68 static int
vidmode_start(vidmode_state_t * state)69 vidmode_start(vidmode_state_t *state)
70 {
71 int r;
72 int screen_num = state->screen_num;
73
74 if (screen_num < 0) screen_num = DefaultScreen(state->display);
75 state->screen_num = screen_num;
76
77 /* Query extension version */
78 int major, minor;
79 r = XF86VidModeQueryVersion(state->display, &major, &minor);
80 if (!r) {
81 fprintf(stderr, _("X request failed: %s\n"),
82 "XF86VidModeQueryVersion");
83 return -1;
84 }
85
86 /* Request size of gamma ramps */
87 r = XF86VidModeGetGammaRampSize(state->display, state->screen_num,
88 &state->ramp_size);
89 if (!r) {
90 fprintf(stderr, _("X request failed: %s\n"),
91 "XF86VidModeGetGammaRampSize");
92 return -1;
93 }
94
95 if (state->ramp_size == 0) {
96 fprintf(stderr, _("Gamma ramp size too small: %i\n"),
97 state->ramp_size);
98 return -1;
99 }
100
101 /* Allocate space for saved gamma ramps */
102 state->saved_ramps = malloc(3*state->ramp_size*sizeof(uint16_t));
103 if (state->saved_ramps == NULL) {
104 perror("malloc");
105 return -1;
106 }
107
108 uint16_t *gamma_r = &state->saved_ramps[0*state->ramp_size];
109 uint16_t *gamma_g = &state->saved_ramps[1*state->ramp_size];
110 uint16_t *gamma_b = &state->saved_ramps[2*state->ramp_size];
111
112 /* Save current gamma ramps so we can restore them at program exit. */
113 r = XF86VidModeGetGammaRamp(state->display, state->screen_num,
114 state->ramp_size, gamma_r, gamma_g,
115 gamma_b);
116 if (!r) {
117 fprintf(stderr, _("X request failed: %s\n"),
118 "XF86VidModeGetGammaRamp");
119 return -1;
120 }
121
122 return 0;
123 }
124
125 static void
vidmode_free(vidmode_state_t * state)126 vidmode_free(vidmode_state_t *state)
127 {
128 /* Free saved ramps */
129 free(state->saved_ramps);
130
131 /* Close display connection */
132 XCloseDisplay(state->display);
133
134 free(state);
135 }
136
137 static void
vidmode_print_help(FILE * f)138 vidmode_print_help(FILE *f)
139 {
140 fputs(_("Adjust gamma ramps with the X VidMode extension.\n"), f);
141 fputs("\n", f);
142
143 /* TRANSLATORS: VidMode help output
144 left column must not be translated */
145 fputs(_(" screen=N\t\tX screen to apply adjustments to\n"),
146 f);
147 fputs("\n", f);
148 }
149
150 static int
vidmode_set_option(vidmode_state_t * state,const char * key,const char * value)151 vidmode_set_option(vidmode_state_t *state, const char *key, const char *value)
152 {
153 if (strcasecmp(key, "screen") == 0) {
154 state->screen_num = atoi(value);
155 } else if (strcasecmp(key, "preserve") == 0) {
156 fprintf(stderr, _("Parameter `%s` is now always on; "
157 " Use the `%s` command-line option"
158 " to disable.\n"),
159 key, "-P");
160 } else {
161 fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
162 return -1;
163 }
164
165 return 0;
166 }
167
168 static void
vidmode_restore(vidmode_state_t * state)169 vidmode_restore(vidmode_state_t *state)
170 {
171 uint16_t *gamma_r = &state->saved_ramps[0*state->ramp_size];
172 uint16_t *gamma_g = &state->saved_ramps[1*state->ramp_size];
173 uint16_t *gamma_b = &state->saved_ramps[2*state->ramp_size];
174
175 /* Restore gamma ramps */
176 int r = XF86VidModeSetGammaRamp(state->display, state->screen_num,
177 state->ramp_size, gamma_r, gamma_g,
178 gamma_b);
179 if (!r) {
180 fprintf(stderr, _("X request failed: %s\n"),
181 "XF86VidModeSetGammaRamp");
182 }
183 }
184
185 static int
vidmode_set_temperature(vidmode_state_t * state,const color_setting_t * setting,int preserve)186 vidmode_set_temperature(
187 vidmode_state_t *state, const color_setting_t *setting, int preserve)
188 {
189 int r;
190
191 /* Create new gamma ramps */
192 uint16_t *gamma_ramps = malloc(3*state->ramp_size*sizeof(uint16_t));
193 if (gamma_ramps == NULL) {
194 perror("malloc");
195 return -1;
196 }
197
198 uint16_t *gamma_r = &gamma_ramps[0*state->ramp_size];
199 uint16_t *gamma_g = &gamma_ramps[1*state->ramp_size];
200 uint16_t *gamma_b = &gamma_ramps[2*state->ramp_size];
201
202 if (preserve) {
203 /* Initialize gamma ramps from saved state */
204 memcpy(gamma_ramps, state->saved_ramps,
205 3*state->ramp_size*sizeof(uint16_t));
206 } else {
207 /* Initialize gamma ramps to pure state */
208 for (int i = 0; i < state->ramp_size; i++) {
209 uint16_t value = (double)i/state->ramp_size *
210 (UINT16_MAX+1);
211 gamma_r[i] = value;
212 gamma_g[i] = value;
213 gamma_b[i] = value;
214 }
215 }
216
217 colorramp_fill(gamma_r, gamma_g, gamma_b, state->ramp_size,
218 setting);
219
220 /* Set new gamma ramps */
221 r = XF86VidModeSetGammaRamp(state->display, state->screen_num,
222 state->ramp_size, gamma_r, gamma_g,
223 gamma_b);
224 if (!r) {
225 fprintf(stderr, _("X request failed: %s\n"),
226 "XF86VidModeSetGammaRamp");
227 free(gamma_ramps);
228 return -1;
229 }
230
231 free(gamma_ramps);
232
233 return 0;
234 }
235
236
237 const gamma_method_t vidmode_gamma_method = {
238 "vidmode", 1,
239 (gamma_method_init_func *)vidmode_init,
240 (gamma_method_start_func *)vidmode_start,
241 (gamma_method_free_func *)vidmode_free,
242 (gamma_method_print_help_func *)vidmode_print_help,
243 (gamma_method_set_option_func *)vidmode_set_option,
244 (gamma_method_restore_func *)vidmode_restore,
245 (gamma_method_set_temperature_func *)vidmode_set_temperature
246 };
247