wlsunset/main.c

527 lines
14 KiB
C
Raw Normal View History

#include <assert.h>
2020-09-12 16:30:39 +00:00
#include <errno.h>
2020-10-07 19:55:46 +00:00
#include <poll.h>
#include <signal.h>
2020-09-13 01:18:14 +00:00
#include <stdbool.h>
2020-09-12 16:30:39 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
2020-10-07 19:55:46 +00:00
#include <time.h>
2020-09-12 16:30:39 +00:00
#include <unistd.h>
#include <wayland-client-protocol.h>
#include <wayland-client.h>
2020-10-04 17:49:45 +00:00
2020-09-12 16:30:39 +00:00
#include "wlr-gamma-control-unstable-v1-client-protocol.h"
2020-09-13 01:22:18 +00:00
#include "color_math.h"
2020-09-12 16:30:39 +00:00
#if defined(SPEEDRUN)
2020-10-04 17:49:45 +00:00
static time_t start = 0;
static void init_time(time_t *tloc) {
(void)tloc;
tzset();
struct timespec realtime;
clock_gettime(CLOCK_REALTIME, &realtime);
start = realtime.tv_sec;
}
static time_t get_time_sec(time_t *tloc) {
2020-10-04 17:49:45 +00:00
(void)tloc;
struct timespec realtime;
clock_gettime(CLOCK_REALTIME, &realtime);
time_t now = start + ((realtime.tv_sec - start) * 1000 + realtime.tv_nsec / 1000000);
struct tm tm;
localtime_r(&now, &tm);
fprintf(stderr, "time in termina: %02d:%02d:%02d\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
return now;
}
2020-10-04 17:49:45 +00:00
static int set_timer(timer_t timer, time_t deadline) {
time_t diff = deadline - start;
struct itimerspec timerspec = {{0}, {0}};
timerspec.it_value.tv_sec = start + diff / 1000;
timerspec.it_value.tv_nsec = (diff % 1000) * 1000000;
struct tm tm;
localtime_r(&deadline, &tm);
fprintf(stderr, "sleeping until %02d:%02d:%02d\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
return timer_settime(timer, TIMER_ABSTIME, &timerspec, NULL);
}
#else
2020-10-04 17:49:45 +00:00
static inline void init_time(time_t *tloc) {
(void)tloc;
tzset();
}
static inline time_t get_time_sec(time_t *tloc) {
return time(tloc);
}
2020-10-04 17:49:45 +00:00
static int set_timer(timer_t timer, time_t deadline) {
struct itimerspec timerspec = {{0}, {0}};
timerspec.it_value.tv_sec = deadline;
return timer_settime(timer, TIMER_ABSTIME, &timerspec, NULL);
}
#endif
2020-10-04 17:49:45 +00:00
static const int LONG_SLEEP = 600;
2020-09-13 01:18:14 +00:00
struct context {
int high_temp;
int low_temp;
int duration;
2020-09-13 19:12:44 +00:00
double longitude;
double latitude;
2020-09-13 01:18:14 +00:00
2020-10-04 13:27:46 +00:00
time_t dawn;
time_t sunrise;
time_t sunset;
time_t dusk;
2020-10-04 17:49:45 +00:00
2020-09-13 19:12:44 +00:00
bool new_output;
2020-09-13 01:18:14 +00:00
struct wl_list outputs;
2020-10-07 19:55:46 +00:00
timer_t timer;
2020-09-13 01:18:14 +00:00
};
2020-09-12 16:30:39 +00:00
struct output {
2020-10-07 19:55:46 +00:00
struct wl_list link;
2020-09-13 01:18:14 +00:00
struct context *context;
2020-09-12 16:30:39 +00:00
struct wl_output *wl_output;
struct zwlr_gamma_control_v1 *gamma_control;
2020-10-07 19:55:46 +00:00
2020-09-12 16:30:39 +00:00
int table_fd;
2020-10-07 19:55:46 +00:00
uint32_t id;
uint32_t ramp_size;
2020-09-12 16:30:39 +00:00
uint16_t *table;
};
static struct zwlr_gamma_control_manager_v1 *gamma_control_manager = NULL;
static int create_anonymous_file(off_t size) {
char template[] = "/tmp/wlsunset-shared-XXXXXX";
2020-09-12 16:30:39 +00:00
int fd = mkstemp(template);
if (fd < 0) {
return -1;
}
int ret;
do {
errno = 0;
ret = ftruncate(fd, size);
} while (errno == EINTR);
if (ret < 0) {
close(fd);
return -1;
}
unlink(template);
return fd;
}
static int create_gamma_table(uint32_t ramp_size, uint16_t **table) {
size_t table_size = ramp_size * 3 * sizeof(uint16_t);
int fd = create_anonymous_file(table_size);
if (fd < 0) {
fprintf(stderr, "failed to create anonymous file\n");
return -1;
}
void *data =
mmap(NULL, table_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
fprintf(stderr, "failed to mmap()\n");
close(fd);
return -1;
}
*table = data;
return fd;
}
static void gamma_control_handle_gamma_size(void *data,
struct zwlr_gamma_control_v1 *gamma_control, uint32_t ramp_size) {
2020-09-13 19:38:56 +00:00
(void)gamma_control;
2020-09-12 16:30:39 +00:00
struct output *output = data;
output->ramp_size = ramp_size;
2020-10-04 13:24:54 +00:00
if (output->table_fd != -1) {
close(output->table_fd);
}
2020-09-12 16:30:39 +00:00
output->table_fd = create_gamma_table(ramp_size, &output->table);
2020-09-13 01:18:14 +00:00
output->context->new_output = true;
2020-09-12 16:30:39 +00:00
if (output->table_fd < 0) {
2020-10-03 22:57:17 +00:00
fprintf(stderr, "could not create gamma table for output %d\n",
output->id);
2020-09-12 16:30:39 +00:00
exit(EXIT_FAILURE);
}
}
static void gamma_control_handle_failed(void *data,
struct zwlr_gamma_control_v1 *gamma_control) {
2020-09-13 19:38:56 +00:00
(void)gamma_control;
2020-10-03 22:57:17 +00:00
struct output *output = data;
2020-10-04 13:24:54 +00:00
fprintf(stderr, "gamma control of output %d failed\n",
2020-10-03 22:57:17 +00:00
output->id);
2020-10-04 13:24:54 +00:00
zwlr_gamma_control_v1_destroy(output->gamma_control);
output->gamma_control = NULL;
if (output->table_fd != -1) {
close(output->table_fd);
output->table_fd = -1;
}
2020-09-12 16:30:39 +00:00
}
static const struct zwlr_gamma_control_v1_listener gamma_control_listener = {
.gamma_size = gamma_control_handle_gamma_size,
.failed = gamma_control_handle_failed,
};
2020-10-03 22:57:17 +00:00
static void setup_output(struct output *output) {
if (output->gamma_control != NULL) {
return;
}
if (gamma_control_manager == NULL) {
fprintf(stderr, "skipping setup of output %d: gamma_control_manager missing\n",
output->id);
return;
2020-09-13 01:18:14 +00:00
}
output->gamma_control = zwlr_gamma_control_manager_v1_get_gamma_control(
gamma_control_manager, output->wl_output);
zwlr_gamma_control_v1_add_listener(output->gamma_control,
&gamma_control_listener, output);
}
2020-09-12 16:30:39 +00:00
static void registry_handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version) {
2020-09-13 19:38:56 +00:00
(void)version;
2020-09-13 01:18:14 +00:00
struct context *ctx = (struct context *)data;
2020-09-12 16:30:39 +00:00
if (strcmp(interface, wl_output_interface.name) == 0) {
2020-10-04 13:31:17 +00:00
fprintf(stderr, "registry: adding output %d\n", name);
2020-09-12 16:30:39 +00:00
struct output *output = calloc(1, sizeof(struct output));
2020-09-13 01:18:14 +00:00
output->id = name;
2020-09-12 16:30:39 +00:00
output->wl_output = wl_registry_bind(registry, name,
&wl_output_interface, 1);
2020-09-13 01:18:14 +00:00
output->table_fd = -1;
output->context = ctx;
wl_list_insert(&ctx->outputs, &output->link);
setup_output(output);
2020-09-12 16:30:39 +00:00
} else if (strcmp(interface,
zwlr_gamma_control_manager_v1_interface.name) == 0) {
gamma_control_manager = wl_registry_bind(registry, name,
&zwlr_gamma_control_manager_v1_interface, 1);
}
}
static void registry_handle_global_remove(void *data,
struct wl_registry *registry, uint32_t name) {
2020-09-13 19:38:56 +00:00
(void)registry;
2020-09-13 01:18:14 +00:00
struct context *ctx = (struct context *)data;
struct output *output, *tmp;
wl_list_for_each_safe(output, tmp, &ctx->outputs, link) {
if (output->id == name) {
2020-10-04 13:31:17 +00:00
fprintf(stderr, "registry: removing output %d\n", name);
2020-10-03 22:56:17 +00:00
wl_list_remove(&output->link);
2020-09-13 01:18:14 +00:00
if (output->gamma_control != NULL) {
zwlr_gamma_control_v1_destroy(output->gamma_control);
}
if (output->table_fd != -1) {
close(output->table_fd);
}
2020-10-03 22:56:17 +00:00
free(output);
2020-09-13 01:18:14 +00:00
break;
}
}
2020-09-12 16:30:39 +00:00
}
static const struct wl_registry_listener registry_listener = {
.global = registry_handle_global,
.global_remove = registry_handle_global_remove,
};
static void fill_gamma_table(uint16_t *table, uint32_t ramp_size, double rw, double gw, double bw, double gamma) {
uint16_t *r = table;
uint16_t *g = table + ramp_size;
uint16_t *b = table + 2 * ramp_size;
for (uint32_t i = 0; i < ramp_size; ++i) {
double val = (double)i / (ramp_size - 1);
r[i] = (uint16_t)(UINT16_MAX * pow(val * rw, 1.0 / gamma));
g[i] = (uint16_t)(UINT16_MAX * pow(val * gw, 1.0 / gamma));
b[i] = (uint16_t)(UINT16_MAX * pow(val * bw, 1.0 / gamma));
}
}
2020-10-07 19:55:46 +00:00
static void set_temperature(struct wl_list *outputs, int temp, int gamma) {
2020-09-13 01:18:14 +00:00
double rw, gw, bw;
2020-10-07 19:55:46 +00:00
calc_whitepoint(temp, &rw, &gw, &bw);
fprintf(stderr, "setting temperature to %d K\n", temp);
2020-09-13 01:18:14 +00:00
struct output *output;
2020-10-07 19:55:46 +00:00
wl_list_for_each(output, outputs, link) {
2020-09-13 01:18:14 +00:00
if (output->gamma_control == NULL || output->table_fd == -1) {
continue;
}
fill_gamma_table(output->table, output->ramp_size,
2020-10-07 19:55:46 +00:00
rw, gw, bw, gamma);
2020-09-13 01:18:14 +00:00
lseek(output->table_fd, 0, SEEK_SET);
zwlr_gamma_control_v1_set_gamma(output->gamma_control,
output->table_fd);
}
}
2020-09-13 19:12:44 +00:00
static void recalc_stops(struct context *ctx, time_t now) {
time_t day = now - (now % 86400);
2020-10-04 13:27:46 +00:00
if (ctx->dusk == 0) {
2020-09-27 14:08:26 +00:00
// First calculation
2020-10-04 13:27:46 +00:00
} else if (now > ctx->dusk) {
day += 86400;
2020-10-04 13:27:46 +00:00
} else if (day < ctx->dusk) {
2020-09-13 19:12:44 +00:00
return;
}
struct tm tm = { 0 };
gmtime_r(&now, &tm);
2020-10-04 13:27:46 +00:00
sun(&tm, ctx->longitude, ctx->latitude, &ctx->dawn, &ctx->sunrise, &ctx->sunset, &ctx->dusk);
if (ctx->duration != -1) {
ctx->dawn = ctx->sunrise - ctx->duration;
ctx->dusk = ctx->sunset + ctx->duration;
}
ctx->dawn += day;
ctx->sunrise += day;
ctx->sunset += day;
ctx->dusk += day;
struct tm dawn, sunrise, sunset, dusk;
localtime_r(&ctx->dawn, &dawn);
localtime_r(&ctx->sunrise, &sunrise);
localtime_r(&ctx->sunset, &sunset);
localtime_r(&ctx->dusk, &dusk);
fprintf(stderr, "calculated new sun trajectory: dawn %02d:%02d, sunrise %02d:%02d, sunset %02d:%02d, dusk %02d:%02d\n",
dawn.tm_hour, dusk.tm_min,
2020-09-13 19:12:44 +00:00
sunrise.tm_hour, sunrise.tm_min,
2020-10-04 13:27:46 +00:00
sunset.tm_hour, sunset.tm_min,
dusk.tm_hour, dusk.tm_min);
}
2020-10-04 13:27:46 +00:00
static int interpolate_temperature(time_t now, time_t start, time_t stop, int temp_start, int temp_stop) {
double time_pos = clamp((double)(now - start) / (double)(stop - start));
int temp_pos = (double)(temp_stop - temp_start) * time_pos;
return temp_start + temp_pos;
2020-09-13 19:12:44 +00:00
}
2020-10-07 19:55:46 +00:00
static int get_temperature(const struct context *ctx, time_t now) {
if (now < ctx->dawn) {
return ctx->low_temp;
} else if (now < ctx->sunrise) {
return interpolate_temperature(now, ctx->dawn, ctx->sunrise, ctx->low_temp, ctx->high_temp);
} else if (now < ctx->sunset) {
return ctx->high_temp;
} else if (now < ctx->dusk) {
return interpolate_temperature(now, ctx->sunset, ctx->dusk, ctx->high_temp, ctx->low_temp);
} else {
return ctx->low_temp;
2020-09-13 01:18:14 +00:00
}
}
2020-10-04 13:27:46 +00:00
static int increments(int temp_diff, int duration) {
assert(temp_diff > 0);
2020-10-04 17:49:45 +00:00
int time = duration * 25 / temp_diff;
return time > LONG_SLEEP ? LONG_SLEEP : time;
2020-09-13 01:18:14 +00:00
}
2020-10-07 19:55:46 +00:00
static void update_timer(struct context *ctx, timer_t timer, time_t now) {
time_t deadline;
2020-10-07 19:55:46 +00:00
if (now < ctx->dawn) {
2020-10-04 13:27:46 +00:00
deadline = ctx->dawn;
2020-10-07 19:55:46 +00:00
} else if (now < ctx->sunrise) {
2020-10-04 17:49:45 +00:00
deadline = now + increments(ctx->high_temp - ctx->low_temp, ctx->sunrise - ctx->dawn);
2020-10-07 19:55:46 +00:00
} else if (now < ctx->sunset) {
deadline = ctx->sunset;
} else if (now < ctx->dusk) {
2020-10-04 17:49:45 +00:00
deadline = now + increments(ctx->high_temp - ctx->low_temp, ctx->dusk - ctx->sunset);
2020-10-07 19:55:46 +00:00
} else {
deadline = ctx->dawn;
deadline = ((deadline / 86400 + 1) * 86400);
}
2020-10-04 17:49:45 +00:00
assert(deadline > now);
2020-10-07 19:55:46 +00:00
set_timer(timer, deadline);
2020-09-13 01:18:14 +00:00
}
static int display_poll(struct wl_display *display, short int events, int timeout) {
struct pollfd pfd[1];
pfd[0].fd = wl_display_get_fd(display);
pfd[0].events = events;
2020-10-04 17:49:45 +00:00
if (poll(pfd, 1, timeout) == -1) {
return errno == EINTR ? 0 : -1;
}
return 0;
2020-09-13 01:18:14 +00:00
}
2020-10-04 17:49:45 +00:00
static int display_dispatch(struct wl_display *display, int timeout) {
2020-09-13 01:18:14 +00:00
if (wl_display_prepare_read(display) == -1) {
return wl_display_dispatch_pending(display);
}
int ret;
while (true) {
ret = wl_display_flush(display);
if (ret != -1 || errno != EAGAIN) {
break;
}
if (display_poll(display, POLLOUT, -1) == -1) {
wl_display_cancel_read(display);
return -1;
}
}
if (ret < 0 && errno != EPIPE) {
wl_display_cancel_read(display);
return -1;
}
if (display_poll(display, POLLIN, timeout) == -1) {
wl_display_cancel_read(display);
return -1;
}
if (wl_display_read_events(display) == -1) {
return -1;
}
return wl_display_dispatch_pending(display);
2020-09-12 16:30:39 +00:00
}
static const char usage[] = "usage: %s [options]\n"
2020-09-13 19:19:02 +00:00
" -h show this help message\n"
" -T <temp> set high temperature (default: 6500)\n"
" -t <temp> set low temperature (default: 4000)\n"
2020-09-14 09:15:42 +00:00
" -l <lat> set latitude (e.g. 39.9)\n"
" -L <long> set longitude (e.g. 116.3)\n"
2020-09-13 19:19:02 +00:00
" -d <minutes> set ramping duration in minutes (default: 60)\n"
" -g <gamma> set gamma (default: 1.0)\n";
2020-09-12 16:30:39 +00:00
2020-10-04 17:49:45 +00:00
static int timer_fired = 0;
static void timer_signal(int signal) {
(void)signal;
timer_fired = true;
}
2020-09-12 16:30:39 +00:00
int main(int argc, char *argv[]) {
2020-10-04 17:49:45 +00:00
init_time(NULL);
2020-09-13 01:18:14 +00:00
// Initialize defaults
struct context ctx = {
.high_temp = 6500,
2020-09-13 19:12:44 +00:00
.low_temp = 4000,
2020-10-04 13:27:46 +00:00
.duration = -1,
2020-09-13 01:18:14 +00:00
};
2020-10-07 19:55:46 +00:00
double gamma = 1.0;
2020-09-13 01:18:14 +00:00
wl_list_init(&ctx.outputs);
2020-10-04 17:49:45 +00:00
struct sigaction timer_action = {
.sa_handler = timer_signal,
.sa_flags = 0,
};
if (sigaction(SIGALRM, &timer_action, NULL) == -1) {
fprintf(stderr, "could not configure alarm handler: %s\n", strerror(errno));
return 1;
}
if (timer_create(CLOCK_REALTIME, NULL, &ctx.timer) == -1) {
fprintf(stderr, "could not configure timer: %s\n", strerror(errno));
return 1;
}
2020-10-04 13:30:58 +00:00
#ifdef SPEEDRUN
fprintf(stderr, "warning: speedrun mode enabled\n");
#endif
2020-09-12 16:30:39 +00:00
int opt;
2020-09-13 19:12:44 +00:00
while ((opt = getopt(argc, argv, "hT:t:g:d:l:L:")) != -1) {
2020-09-12 16:30:39 +00:00
switch (opt) {
2020-09-13 01:18:14 +00:00
case 'T':
ctx.high_temp = strtol(optarg, NULL, 10);
break;
2020-09-12 16:30:39 +00:00
case 't':
2020-09-13 01:18:14 +00:00
ctx.low_temp = strtol(optarg, NULL, 10);
break;
2020-09-13 19:12:44 +00:00
case 'l':
ctx.latitude = strtod(optarg, NULL);
2020-09-13 01:18:14 +00:00
break;
2020-09-13 19:12:44 +00:00
case 'L':
ctx.longitude = strtod(optarg, NULL);
2020-09-13 01:18:14 +00:00
break;
case 'd':
2020-10-04 13:27:46 +00:00
fprintf(stderr, "using animation duration override\n");
2020-09-13 01:18:14 +00:00
ctx.duration = strtod(optarg, NULL) * 60;
2020-09-12 16:30:39 +00:00
break;
case 'g':
2020-10-07 19:55:46 +00:00
gamma = strtod(optarg, NULL);
2020-09-12 16:30:39 +00:00
break;
case 'h':
default:
fprintf(stderr, usage, argv[0]);
return opt == 'h' ? EXIT_SUCCESS : EXIT_FAILURE;
}
}
2020-09-20 15:25:38 +00:00
if (ctx.high_temp == ctx.low_temp) {
fprintf(stderr, "high (%d) and low (%d) temperature must not be identical\n",
ctx.high_temp, ctx.low_temp);
return -1;
}
2020-09-12 16:30:39 +00:00
struct wl_display *display = wl_display_connect(NULL);
if (display == NULL) {
fprintf(stderr, "failed to create display\n");
return -1;
}
struct wl_registry *registry = wl_display_get_registry(display);
2020-09-13 01:18:14 +00:00
wl_registry_add_listener(registry, &registry_listener, &ctx);
2020-09-12 16:30:39 +00:00
wl_display_roundtrip(display);
if (gamma_control_manager == NULL) {
fprintf(stderr,
"compositor doesn't support wlr-gamma-control-unstable-v1\n");
return EXIT_FAILURE;
}
struct output *output;
2020-09-13 01:18:14 +00:00
wl_list_for_each(output, &ctx.outputs, link) {
setup_output(output);
2020-09-12 16:30:39 +00:00
}
wl_display_roundtrip(display);
2020-10-07 19:55:46 +00:00
time_t now = get_time_sec(NULL);
2020-10-07 19:55:46 +00:00
recalc_stops(&ctx, now);
int temp = get_temperature(&ctx, now);
set_temperature(&ctx.outputs, temp, gamma);
update_timer(&ctx, ctx.timer, now);
int old_temp = temp;
2020-10-04 17:49:45 +00:00
while (display_dispatch(display, -1) != -1) {
if (timer_fired) {
timer_fired = false;
2020-10-07 19:55:46 +00:00
2020-10-04 17:49:45 +00:00
now = get_time_sec(NULL);
2020-10-07 19:55:46 +00:00
recalc_stops(&ctx, now);
update_timer(&ctx, ctx.timer, now);
if ((temp = get_temperature(&ctx, now)) != old_temp) {
old_temp = temp;
ctx.new_output = false;
set_temperature(&ctx.outputs, temp, gamma);
}
} else if (ctx.new_output) {
ctx.new_output = false;
set_temperature(&ctx.outputs, temp, gamma);
2020-10-04 17:49:45 +00:00
}
2020-09-12 16:30:39 +00:00
}
return EXIT_SUCCESS;
}