Implement time ramp
parent
45dec286c5
commit
b2ebdea44a
280
main.c
280
main.c
|
@ -1,5 +1,7 @@
|
||||||
|
#define _XOPEN_SOURCE 700
|
||||||
#define _POSIX_C_SOURCE 200809L
|
#define _POSIX_C_SOURCE 200809L
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -8,10 +10,37 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <wayland-client-protocol.h>
|
#include <wayland-client-protocol.h>
|
||||||
#include <wayland-client.h>
|
#include <wayland-client.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <poll.h>
|
||||||
#include "wlr-gamma-control-unstable-v1-client-protocol.h"
|
#include "wlr-gamma-control-unstable-v1-client-protocol.h"
|
||||||
|
|
||||||
|
enum state {
|
||||||
|
HIGH_TEMP,
|
||||||
|
LOW_TEMP,
|
||||||
|
ANIMATING_TO_HIGH,
|
||||||
|
ANIMATING_TO_LOW,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct context {
|
||||||
|
double gamma;
|
||||||
|
time_t start_time;
|
||||||
|
time_t stop_time;
|
||||||
|
int high_temp;
|
||||||
|
int low_temp;
|
||||||
|
int duration;
|
||||||
|
|
||||||
|
int cur_temp;
|
||||||
|
bool new_output;
|
||||||
|
enum state state;
|
||||||
|
time_t animation_start;
|
||||||
|
|
||||||
|
struct wl_list outputs;
|
||||||
|
};
|
||||||
|
|
||||||
struct output {
|
struct output {
|
||||||
|
struct context *context;
|
||||||
struct wl_output *wl_output;
|
struct wl_output *wl_output;
|
||||||
|
int id;
|
||||||
struct zwlr_gamma_control_v1 *gamma_control;
|
struct zwlr_gamma_control_v1 *gamma_control;
|
||||||
uint32_t ramp_size;
|
uint32_t ramp_size;
|
||||||
int table_fd;
|
int table_fd;
|
||||||
|
@ -19,7 +48,6 @@ struct output {
|
||||||
struct wl_list link;
|
struct wl_list link;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct wl_list outputs;
|
|
||||||
static struct zwlr_gamma_control_manager_v1 *gamma_control_manager = NULL;
|
static struct zwlr_gamma_control_manager_v1 *gamma_control_manager = NULL;
|
||||||
|
|
||||||
static int create_anonymous_file(off_t size) {
|
static int create_anonymous_file(off_t size) {
|
||||||
|
@ -68,6 +96,7 @@ static void gamma_control_handle_gamma_size(void *data,
|
||||||
struct output *output = data;
|
struct output *output = data;
|
||||||
output->ramp_size = ramp_size;
|
output->ramp_size = ramp_size;
|
||||||
output->table_fd = create_gamma_table(ramp_size, &output->table);
|
output->table_fd = create_gamma_table(ramp_size, &output->table);
|
||||||
|
output->context->new_output = true;
|
||||||
if (output->table_fd < 0) {
|
if (output->table_fd < 0) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
@ -76,7 +105,6 @@ static void gamma_control_handle_gamma_size(void *data,
|
||||||
static void gamma_control_handle_failed(void *data,
|
static void gamma_control_handle_failed(void *data,
|
||||||
struct zwlr_gamma_control_v1 *gamma_control) {
|
struct zwlr_gamma_control_v1 *gamma_control) {
|
||||||
fprintf(stderr, "failed to set gamma table\n");
|
fprintf(stderr, "failed to set gamma table\n");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct zwlr_gamma_control_v1_listener gamma_control_listener = {
|
static const struct zwlr_gamma_control_v1_listener gamma_control_listener = {
|
||||||
|
@ -84,13 +112,29 @@ static const struct zwlr_gamma_control_v1_listener gamma_control_listener = {
|
||||||
.failed = gamma_control_handle_failed,
|
.failed = gamma_control_handle_failed,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static bool setup_output(struct output *output) {
|
||||||
|
if (gamma_control_manager == NULL || output->gamma_control != NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static void registry_handle_global(void *data, struct wl_registry *registry,
|
static void registry_handle_global(void *data, struct wl_registry *registry,
|
||||||
uint32_t name, const char *interface, uint32_t version) {
|
uint32_t name, const char *interface, uint32_t version) {
|
||||||
|
struct context *ctx = (struct context *)data;
|
||||||
if (strcmp(interface, wl_output_interface.name) == 0) {
|
if (strcmp(interface, wl_output_interface.name) == 0) {
|
||||||
struct output *output = calloc(1, sizeof(struct output));
|
struct output *output = calloc(1, sizeof(struct output));
|
||||||
|
output->id = name;
|
||||||
output->wl_output = wl_registry_bind(registry, name,
|
output->wl_output = wl_registry_bind(registry, name,
|
||||||
&wl_output_interface, 1);
|
&wl_output_interface, 1);
|
||||||
wl_list_insert(&outputs, &output->link);
|
output->table_fd = -1;
|
||||||
|
output->context = ctx;
|
||||||
|
wl_list_insert(&ctx->outputs, &output->link);
|
||||||
|
setup_output(output);
|
||||||
} else if (strcmp(interface,
|
} else if (strcmp(interface,
|
||||||
zwlr_gamma_control_manager_v1_interface.name) == 0) {
|
zwlr_gamma_control_manager_v1_interface.name) == 0) {
|
||||||
gamma_control_manager = wl_registry_bind(registry, name,
|
gamma_control_manager = wl_registry_bind(registry, name,
|
||||||
|
@ -100,7 +144,20 @@ static void registry_handle_global(void *data, struct wl_registry *registry,
|
||||||
|
|
||||||
static void registry_handle_global_remove(void *data,
|
static void registry_handle_global_remove(void *data,
|
||||||
struct wl_registry *registry, uint32_t name) {
|
struct wl_registry *registry, uint32_t name) {
|
||||||
// Who cares?
|
struct context *ctx = (struct context *)data;
|
||||||
|
struct output *output, *tmp;
|
||||||
|
wl_list_for_each_safe(output, tmp, &ctx->outputs, link) {
|
||||||
|
if (output->id == name) {
|
||||||
|
if (output->gamma_control != NULL) {
|
||||||
|
zwlr_gamma_control_v1_destroy(output->gamma_control);
|
||||||
|
}
|
||||||
|
if (output->table_fd != -1) {
|
||||||
|
close(output->table_fd);
|
||||||
|
}
|
||||||
|
wl_list_remove(&output->link);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct wl_registry_listener registry_listener = {
|
static const struct wl_registry_listener registry_listener = {
|
||||||
|
@ -178,6 +235,13 @@ static void xyz_to_srgb(double x, double y, double z, double *r, double *g, doub
|
||||||
*b = srgb_gamma(clamp(0.0556434 * x - 0.2040259 * y + 1.0572252 * z), 2.2);
|
*b = srgb_gamma(clamp(0.0556434 * x - 0.2040259 * y + 1.0572252 * z), 2.2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void srgb_normalize(double *r, double *g, double *b) {
|
||||||
|
double maxw = fmaxl(*r, fmaxl(*g, *b));
|
||||||
|
*r /= maxw;
|
||||||
|
*g /= maxw;
|
||||||
|
*b /= maxw;
|
||||||
|
}
|
||||||
|
|
||||||
static void calc_whitepoint(int temp, double *rw, double *gw, double *bw) {
|
static void calc_whitepoint(int temp, double *rw, double *gw, double *bw) {
|
||||||
if (temp == 6500) {
|
if (temp == 6500) {
|
||||||
*rw = *gw = *bw = 1.0;
|
*rw = *gw = *bw = 1.0;
|
||||||
|
@ -193,31 +257,193 @@ static void calc_whitepoint(int temp, double *rw, double *gw, double *bw) {
|
||||||
double z = 1.0 - x - y;
|
double z = 1.0 - x - y;
|
||||||
|
|
||||||
xyz_to_srgb(x, y, z, rw, gw, bw);
|
xyz_to_srgb(x, y, z, rw, gw, bw);
|
||||||
|
srgb_normalize(rw, gw, bw);
|
||||||
|
}
|
||||||
|
|
||||||
double maxw = fmaxl(*rw, fmaxl(*gw, *bw));
|
static void set_temperature(struct context *ctx) {
|
||||||
*rw /= maxw;
|
double rw, gw, bw;
|
||||||
*gw /= maxw;
|
calc_whitepoint(ctx->cur_temp, &rw, &gw, &bw);
|
||||||
*bw /= maxw;
|
|
||||||
|
struct output *output;
|
||||||
|
wl_list_for_each(output, &ctx->outputs, link) {
|
||||||
|
if (output->gamma_control == NULL || output->table_fd == -1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
fill_gamma_table(output->table, output->ramp_size,
|
||||||
|
rw, gw, bw, ctx->gamma);
|
||||||
|
lseek(output->table_fd, 0, SEEK_SET);
|
||||||
|
zwlr_gamma_control_v1_set_gamma(output->gamma_control,
|
||||||
|
output->table_fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void update_temperature(struct context *ctx) {
|
||||||
|
time_t now = time(NULL);
|
||||||
|
time_t t = now % 86400;
|
||||||
|
int temp, temp_pos;
|
||||||
|
double time_pos;
|
||||||
|
|
||||||
|
switch (ctx->state) {
|
||||||
|
case HIGH_TEMP:
|
||||||
|
if (t > ctx->stop_time || t < ctx->start_time) {
|
||||||
|
ctx->state = ANIMATING_TO_LOW;
|
||||||
|
ctx->animation_start = now;
|
||||||
|
}
|
||||||
|
temp = ctx->high_temp;
|
||||||
|
break;
|
||||||
|
case LOW_TEMP:
|
||||||
|
if (t > ctx->start_time || t < ctx->stop_time) {
|
||||||
|
ctx->state = ANIMATING_TO_HIGH;
|
||||||
|
ctx->animation_start = now;
|
||||||
|
}
|
||||||
|
temp = ctx->low_temp;
|
||||||
|
break;
|
||||||
|
case ANIMATING_TO_HIGH:
|
||||||
|
if (now > ctx->animation_start + ctx->duration) {
|
||||||
|
ctx->state = HIGH_TEMP;
|
||||||
|
}
|
||||||
|
time_pos = clamp(((double)now - (double)ctx->animation_start) / (double)ctx->duration);
|
||||||
|
temp_pos = (double)(ctx->high_temp - ctx->low_temp) * time_pos;
|
||||||
|
temp = ctx->low_temp + temp_pos;
|
||||||
|
break;
|
||||||
|
case ANIMATING_TO_LOW:
|
||||||
|
if (now > ctx->animation_start + ctx->duration) {
|
||||||
|
ctx->state = LOW_TEMP;
|
||||||
|
}
|
||||||
|
time_pos = clamp(((double)now - (double)ctx->animation_start) / (double)ctx->duration);
|
||||||
|
temp_pos = (double)(ctx->high_temp - ctx->low_temp) * time_pos;
|
||||||
|
temp = ctx->high_temp - temp_pos;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (temp != ctx->cur_temp || ctx->new_output) {
|
||||||
|
fprintf(stderr, "state: %d, temp: %d, cur: %d\n", ctx->state, temp, ctx->cur_temp);
|
||||||
|
ctx->cur_temp = temp;
|
||||||
|
set_temperature(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int increments(struct context *ctx) {
|
||||||
|
int diff = ctx->high_temp - ctx->low_temp;
|
||||||
|
diff /= 50;
|
||||||
|
int time = (ctx->duration * 1000) / diff;
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int time_to_next_event(struct context *ctx) {
|
||||||
|
switch (ctx->state) {
|
||||||
|
case ANIMATING_TO_HIGH:
|
||||||
|
case ANIMATING_TO_LOW:
|
||||||
|
return increments(ctx);
|
||||||
|
default:
|
||||||
|
return 300000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
int ret;
|
||||||
|
do {
|
||||||
|
ret = poll(pfd, 1, timeout);
|
||||||
|
} while (ret == -1 && errno == EINTR);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int display_dispatch_with_timeout(struct wl_display *display, int timeout);
|
||||||
|
int display_dispatch_with_timeout(struct wl_display *display, int timeout) {
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char usage[] = "usage: %s [options]\n"
|
static const char usage[] = "usage: %s [options]\n"
|
||||||
" -h show this help message\n"
|
" -h show this help message\n"
|
||||||
" -t <value> set temperature (default: 6500)\n"
|
" -T <value> set high temperature (default: 6500)\n"
|
||||||
|
" -t <value> set low temperature (default: 3500)\n"
|
||||||
|
" -S <value> set ramp up time (default: 6:00)\n"
|
||||||
|
" -s <value> set ramp down time (default: 18:00)\n"
|
||||||
|
" -d <value> set ramping duration in minutes (default: 30)\n"
|
||||||
" -g <value> set gamma (default: 1)\n";
|
" -g <value> set gamma (default: 1)\n";
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
wl_list_init(&outputs);
|
|
||||||
|
|
||||||
double gamma = 1;
|
tzset();
|
||||||
long temperature = 6500;
|
|
||||||
|
// Initialize defaults
|
||||||
|
struct context ctx = {
|
||||||
|
.gamma = 1.0,
|
||||||
|
.start_time = 6 * 60 * 60,
|
||||||
|
.stop_time = 18 * 60 * 60,
|
||||||
|
.high_temp = 6500,
|
||||||
|
.low_temp = 3500,
|
||||||
|
.duration = 30 * 60,
|
||||||
|
.state = HIGH_TEMP,
|
||||||
|
};
|
||||||
|
wl_list_init(&ctx.outputs);
|
||||||
|
|
||||||
int opt;
|
int opt;
|
||||||
while ((opt = getopt(argc, argv, "ht:g:")) != -1) {
|
time_t now = time(NULL);
|
||||||
|
struct tm tm = { 0 };
|
||||||
|
struct tm current = { 0 };
|
||||||
|
localtime_r(&now, ¤t);
|
||||||
|
while ((opt = getopt(argc, argv, "hT:t:S:s:g:d:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
|
case 'T':
|
||||||
|
ctx.high_temp = strtol(optarg, NULL, 10);
|
||||||
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
temperature = strtol(optarg, NULL, 10);
|
ctx.low_temp = strtol(optarg, NULL, 10);
|
||||||
|
break;
|
||||||
|
case 'S':
|
||||||
|
memcpy(&tm, ¤t, sizeof tm);
|
||||||
|
if (strptime(optarg, "%H:%M", &tm) != NULL) {
|
||||||
|
ctx.start_time = mktime(&tm) % 86400;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
memcpy(&tm, ¤t, sizeof tm);
|
||||||
|
if (strptime(optarg, "%H:%M", &tm) != NULL) {
|
||||||
|
ctx.stop_time = mktime(&tm) % 86400;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
ctx.duration = strtod(optarg, NULL) * 60;
|
||||||
break;
|
break;
|
||||||
case 'g':
|
case 'g':
|
||||||
gamma = strtod(optarg, NULL);
|
ctx.gamma = strtod(optarg, NULL);
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
default:
|
default:
|
||||||
|
@ -233,7 +459,7 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct wl_registry *registry = wl_display_get_registry(display);
|
struct wl_registry *registry = wl_display_get_registry(display);
|
||||||
wl_registry_add_listener(registry, ®istry_listener, NULL);
|
wl_registry_add_listener(registry, ®istry_listener, &ctx);
|
||||||
wl_display_roundtrip(display);
|
wl_display_roundtrip(display);
|
||||||
|
|
||||||
if (gamma_control_manager == NULL) {
|
if (gamma_control_manager == NULL) {
|
||||||
|
@ -243,27 +469,15 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct output *output;
|
struct output *output;
|
||||||
wl_list_for_each(output, &outputs, link) {
|
wl_list_for_each(output, &ctx.outputs, link) {
|
||||||
output->gamma_control = zwlr_gamma_control_manager_v1_get_gamma_control(
|
setup_output(output);
|
||||||
gamma_control_manager, output->wl_output);
|
|
||||||
zwlr_gamma_control_v1_add_listener(output->gamma_control,
|
|
||||||
&gamma_control_listener, output);
|
|
||||||
}
|
}
|
||||||
wl_display_roundtrip(display);
|
wl_display_roundtrip(display);
|
||||||
|
|
||||||
/* Approximate white point */
|
|
||||||
double rw, gw, bw;
|
|
||||||
calc_whitepoint(temperature, &rw, &gw, &bw);
|
|
||||||
|
|
||||||
wl_list_for_each(output, &outputs, link) {
|
update_temperature(&ctx);
|
||||||
fill_gamma_table(output->table, output->ramp_size,
|
while (display_dispatch_with_timeout(display, time_to_next_event(&ctx)) != -1) {
|
||||||
rw, gw, bw, gamma);
|
update_temperature(&ctx);
|
||||||
zwlr_gamma_control_v1_set_gamma(output->gamma_control,
|
|
||||||
output->table_fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (wl_display_dispatch(display) != -1) {
|
|
||||||
// This space is intentionnally left blank
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
Loading…
Reference in New Issue