1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
/* redshift-ng - Automatically adjust display colour temperature according the Sun
*
* Copyright (c) 2009-2018 Jon Lund Steffensen <jonlst@gmail.com>
* Copyright (c) 2014-2016, 2025 Mattias Andrée <m@maandree.se>
*
* redshift-ng 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-ng 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-ng. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
/* poll.h is not available on Windows but there is no Windows location provider
* using polling. On Windows, we just define some stubs to make things compile.
*/
#ifndef WINDOWS
# include <poll.h>
#else
# define POLLIN 0
struct pollfd {
int fd;
short events;
short revents;
};
int poll(struct pollfd *fds, int nfds, int timeout) { abort(); }
#endif
/* Duration of sleep between screen updates (milliseconds). */
#define SLEEP_DURATION 5000
#define SLEEP_DURATION_SHORT 100
/* Length of fade in numbers of short sleep durations. */
#define FADE_LENGTH 40
#if defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
static int
exact_eq(double a, double b)
{
return a == b;
}
#if defined(__GNUC__)
# pragma GCC diagnostic pop
#endif
/**
* Suspend the process for a short time
*
* The process may be resumed earily, specifically
* if it receives a signal
*
* @param msecs The number of milliseconds to sleep
*/
static void
millisleep(unsigned int msecs)
{
#ifdef WINDOWS
Sleep(msecs); /* TODO [Windows] not interruptible */
#else
struct timespec ts;
ts.tv_sec = (time_t)(msecs / 1000U);
ts.tv_nsec = (long)(msecs % 1000U) * 1000000L;
nanosleep(&ts, NULL);
#endif
}
/**
* Get the number of seconds since midnight
*
* @return The number of seconds since midnight
*/
static time_t
get_time_since_midnight(void)
{
time_t t = time(NULL);
struct tm tm;
localtime_r(&t, &tm);
t = (time_t)tm.tm_sec;
t += (time_t)tm.tm_min * 60;
t += (time_t)tm.tm_hour * 3600;
return t;
}
/**
* Print the current period of the day
*
* @param period The current period of the day
* @param day_level The current dayness level
*/
static void
print_period(enum period period, double day_level)
{
static const char *period_names[] = {
/* TRANSLATORS: Name printed when period of day is unknown */
[PERIOD_NONE] = N_("None"),
[PERIOD_DAYTIME] = N_("Daytime"),
[PERIOD_NIGHT] = N_("Night"),
[PERIOD_TRANSITION] = N_("Transition")
};
if (period == PERIOD_TRANSITION)
printf(_("Period: %s (%.2f%% day)\n"), gettext(period_names[period]), day_level * 100);
else
printf(_("Period: %s\n"), gettext(period_names[period]));
}
/**
* Get the current period of day and the colour settings
* applicable to the current time of the day
*
* @param location Geographical user location
* @param colour_out Output parameter for the colour settings
* @param period_out Output parameter for the period of the day
* @param day_level_out Output parameter for the dayness level
*/
static void
get_colour_settings(const struct location *location, struct colour_setting *colour_out,
enum period *period_out, double *day_level_out)
{
time_t time_offset;
double t, elevation;
/* Get dayness level */
if (scheme.type == CLOCK_SCHEME) {
time_offset = get_time_since_midnight();
while (time_offset >= scheme.time.periods->next->start)
scheme.time.periods = scheme.time.periods->next;
time_offset -= scheme.time.periods->start;
if (time_offset < 0)
time_offset += ONE_DAY;
t = (double)time_offset;
*day_level_out = fma(t, scheme.time.periods->diff_over_duration, scheme.time.periods->day_level);
} else if (scheme.type == SOLAR_SCHEME) {
if (libred_solar_elevation(location->latitude, location->longitude, &elevation))
eprintf("libred_solar_elevation:");
if (verbose) {
/* TRANSLATORS: Append degree symbol if possible. */
printf(_("Solar elevation: %f\n"), elevation);
}
*day_level_out = (elevation - scheme.elevation.low) / scheme.elevation.range;
} else {
/* Static scheme, no dayness-level or peroid; day_settings == nigh_settings, use either */
*day_level_out = NAN;
*period_out = PERIOD_NONE;
*colour_out = day_settings;
return;
}
/* Clamp dayness level and get colour */
if (*day_level_out <= 0.0) {
*day_level_out = 0.0;
*period_out = PERIOD_NIGHT;
*colour_out = night_settings;
} else if (*day_level_out >= 1.0) {
*day_level_out = 1.0;
*period_out = PERIOD_DAYTIME;
*colour_out = day_settings;
} else {
*period_out = PERIOD_TRANSITION;
interpolate_colour_settings(&night_settings, &day_settings, *day_level_out, colour_out);
}
}
/**
* Easing function used for fade effect
*
* See https://github.com/mietek/ease-tween
*
* @param t Raw fade progress
* @return Fade progress to apply
*/
GCC_ONLY(__attribute__((__const__)))
static double
ease_fade(double t)
{
if (t <= 0) return 0;
if (t >= 1) return 1;
return 1.0042954579734844 * exp(-6.4041738958415664 * exp(-7.2908241330981340 * t));
}
/* Run continual mode loop
This is the main loop of the continual mode which keeps track of the
current time and continuously updates the screen to the appropriate
colour temperature. */
static void
run_continual_mode(const struct location_provider *provider, LOCATION_STATE *location_state,
const struct gamma_method *method, GAMMA_STATE *method_state, int use_fade,
int preserve_gamma)
{
int done = 0;
int prev_disabled = 1;
int disabled = 0;
int location_available = 1;
struct colour_setting fade_start_interp;
struct colour_setting prev_target_interp;
struct colour_setting interp;
struct location loc;
/* Short fade parameters */
int fade_length = 0;
int fade_time = 0;
/* Save previous parameters so we can avoid printing status updates if
* the values did not change. */
enum period prev_period = PERIOD_NONE;
install_signal_handlers();
/* Previous target colour setting and current actual colour setting.
* Actual colour setting takes into account the current colour fade. */
prev_target_interp = COLOUR_SETTING_NEUTRAL;
interp = COLOUR_SETTING_NEUTRAL;
loc = (struct location){NAN, NAN};
if (scheme.type == SOLAR_SCHEME) {
weprintf(_("Waiting for initial location to become available..."));
if (get_location(provider, location_state, -1, &loc) < 0)
eprintf(_("Unable to get location from provider."));
if (!location_is_valid(&loc))
eprintf(_("Invalid location returned from provider."));
print_location(&loc);
}
if (verbose) {
printf(_("Color temperature: %luK\n"), interp.temperature);
printf(_("Brightness: %.2f\n"), interp.brightness);
}
/* Continuously adjust colour temperature */
for (;;) {
enum period period;
double day_level;
struct colour_setting target_interp;
int delay, loc_fd;
/* Check to see if disable signal was caught */
if (disable && !done) {
disabled = !disabled;
disable = 0;
}
/* Check to see if exit signal was caught */
if (exiting) {
if (done)
break; /* On second signal stop the ongoing fade */
done = 1;
disabled = 1;
exiting = 0;
}
/* Print status change */
if (verbose && disabled != prev_disabled)
printf(_("Status: %s\n"), disabled ? _("Disabled") : _("Enabled"));
prev_disabled = disabled;
get_colour_settings(&loc, &target_interp, &period, &day_level);
if (disabled) {
period = PERIOD_NONE;
target_interp = COLOUR_SETTING_NEUTRAL;
}
if (done)
period = PERIOD_NONE;
/* Print period if it changed during this update,
* or if we are in the transition period. In transition we
* print the progress, so we always print it in
* that case. */
if (verbose && (period != prev_period || period == PERIOD_TRANSITION))
print_period(period, day_level);
/* Activate hooks if period changed */
if (period != prev_period)
run_period_change_hooks(prev_period, period);
/* Start fade if the parameter differences are too big to apply instantly */
if (use_fade && colour_setting_diff_is_major(&target_interp, fade_length ? &prev_target_interp : &interp)) {
fade_length = FADE_LENGTH;
fade_time = 0;
fade_start_interp = interp;
}
/* Handle ongoing fade */
if (fade_length != 0) {
double frac = ++fade_time / (double)fade_length;
double alpha = CLAMP(0.0, ease_fade(frac), 1.0);
interpolate_colour_settings(&fade_start_interp, &target_interp, alpha, &interp);
if (fade_time > fade_length) {
fade_time = 0;
fade_length = 0;
}
} else {
interp = target_interp;
}
/* Break loop when done and final fade is over */
if (done && fade_length == 0)
break;
if (verbose) {
if (prev_target_interp.temperature != target_interp.temperature)
printf(_("Color temperature: %luK\n"), target_interp.temperature);
if (!exact_eq(prev_target_interp.brightness, target_interp.brightness))
printf(_("Brightness: %.2f\n"), target_interp.brightness);
}
/* Adjust temperature */
if (method->apply(method_state, &interp, preserve_gamma) < 0)
eprintf(_("Temperature adjustment failed."));
/* Save period and target colour setting as previous */
prev_period = period;
prev_target_interp = target_interp;
/* Sleep length depends on whether a fade is ongoing */
delay = fade_length ? SLEEP_DURATION_SHORT : SLEEP_DURATION;
/* Update location */
loc_fd = scheme.type == SOLAR_SCHEME ? provider->get_fd(location_state) : -1;
if (loc_fd >= 0) {
struct pollfd pollfds[1];
struct location new_loc;
int r, new_available;
/* Provider is dynamic */
pollfds[0].fd = loc_fd;
pollfds[0].events = POLLIN;
r = poll(pollfds, 1, delay);
if (r < 0) {
#ifndef WINDOWS
if (errno == EINTR)
continue;
#endif
weprintf("poll:");
eprintf(_("Unable to get location from provider."));
} else if (!r) {
continue;
}
/* Get new location and availability information */
if (provider->fetch(location_state, &new_loc, &new_available) < 0)
eprintf(_("Unable to get location from provider."));
if (!new_available && new_available != location_available) {
weprintf(_("Location is temporarily unavailable; using previous"
" location until it becomes available..."));
}
if (new_available &&
(!exact_eq(new_loc.latitude, loc.latitude) ||
!exact_eq(new_loc.longitude, loc.longitude) ||
new_available != location_available)) {
loc = new_loc;
print_location(&loc);
}
location_available = new_available;
if (!location_is_valid(&loc))
eprintf(_("Invalid location returned from provider."));
} else {
millisleep(delay);
}
}
/* Restore saved gamma ramps */
method->restore(method_state);
}
int
main(int argc, char *argv[])
{
struct settings settings;
GAMMA_STATE *method_state = NULL;
LOCATION_STATE *location_state = NULL;
struct location loc = {NAN, NAN};
double day_level;
enum period period;
struct colour_setting colour;
argv0 = argv[0];
#ifdef ENABLE_NLS
setlocale(LC_CTYPE, "");
setlocale(LC_MESSAGES, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
#endif
load_settings(&settings, argc, argv);
if (scheme.type == SOLAR_SCHEME)
acquire_location_provider(&settings, &location_state);
if (mode != PROGRAM_MODE_PRINT)
acquire_adjustment_method(&settings, &method_state);
config_ini_free(&settings.config);
switch (mode) {
case PROGRAM_MODE_ONE_SHOT:
case PROGRAM_MODE_PRINT:
case PROGRAM_MODE_RESET:
if (scheme.type == SOLAR_SCHEME) {
weprintf(_("Waiting for current location to become available..."));
if (get_location(settings.provider, location_state, -1, &loc) < 0)
eprintf(_("Unable to get location from provider."));
if (!location_is_valid(&loc))
exit(1);
print_location(&loc);
}
get_colour_settings(&loc, &colour, &period, &day_level);
if (verbose || mode == PROGRAM_MODE_PRINT) {
if (scheme.type != STATIC_SCHEME)
print_period(period, day_level);
printf(_("Color temperature: %luK\n"), colour.temperature);
printf(_("Brightness: %.2f\n"), colour.brightness);
if (mode == PROGRAM_MODE_PRINT)
break;
}
if (settings.method->apply(method_state, &colour, settings.preserve_gamma.value) < 0)
eprintf(_("Temperature adjustment failed."));
#ifndef WINDOWS
/* In Quartz (OSX) the gamma adjustments will automatically revert when
* the process exits. Therefore, we have to loop until Ctrl+C is received. */
if (!strcmp(settings.method->name, "quartz")) {
weprintf(_("Press ctrl-c to stop..."));
while (!exiting)
pause();
}
#endif
break;
case PROGRAM_MODE_CONTINUAL:
run_continual_mode(settings.provider, location_state, settings.method, method_state,
settings.use_fade.value, settings.preserve_gamma.value);
break;
}
if (method_state)
settings.method->free(method_state);
if (location_state)
settings.provider->free(location_state);
return 0;
}
|