wlsunset/main.c

751 lines
18 KiB
C
Raw Normal View History

#include <assert.h>
2020-09-12 16:30:39 +00:00
#include <errno.h>
2020-10-18 20:41:22 +00:00
#include <fcntl.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-17 11:37:22 +00:00
static time_t start = 0, offset = 0, multiplier = 1000;
static void init_time(void) {
2020-10-04 17:49:45 +00:00
tzset();
struct timespec realtime;
clock_gettime(CLOCK_REALTIME, &realtime);
2020-10-17 11:37:22 +00:00
offset = realtime.tv_sec;
char *startstr = getenv("SPEEDRUN_START");
if (startstr != NULL) {
start = atol(startstr);
} else {
start = offset;
}
char *multistr = getenv("SPEEDRUN_MULTIPLIER");
if (multistr != NULL) {
multiplier = atol(multistr);
}
}
2020-10-17 11:37:22 +00:00
static time_t get_time_sec(void) {
2020-10-04 17:49:45 +00:00
struct timespec realtime;
clock_gettime(CLOCK_REALTIME, &realtime);
2020-10-20 22:41:50 +00:00
time_t now = start + ((realtime.tv_sec - offset) * multiplier +
realtime.tv_nsec / (1000000000 / multiplier));
struct tm tm;
localtime_r(&now, &tm);
2020-10-20 22:41:50 +00:00
fprintf(stderr, "time in termina: %02d:%02d:%02d, %d/%d/%d\n",
tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_mday,
tm.tm_mon+1, tm.tm_year + 1900);
return now;
}
2020-10-17 11:37:22 +00:00
static void adjust_timerspec(struct itimerspec *timerspec) {
int diff = timerspec->it_value.tv_sec - offset;
timerspec->it_value.tv_sec = offset + diff / multiplier;
timerspec->it_value.tv_nsec = (diff % multiplier) * (1000000000 / multiplier);
2020-10-04 17:49:45 +00:00
}
#else
2020-10-17 11:37:22 +00:00
static inline void init_time(void) {
2020-10-04 17:49:45 +00:00
tzset();
}
2020-10-17 11:37:22 +00:00
static inline time_t get_time_sec(void) {
struct timespec realtime;
clock_gettime(CLOCK_REALTIME, &realtime);
return realtime.tv_sec;
}
2020-10-17 11:37:22 +00:00
static inline void adjust_timerspec(struct itimerspec *timerspec) {
(void)timerspec;
2020-10-04 17:49:45 +00:00
}
#endif
static time_t round_day_offset(time_t now, time_t offset) {
return now - ((now - offset) % 86400);
2020-10-18 22:39:38 +00:00
}
static time_t tomorrow(time_t now, time_t offset) {
return round_day_offset(now, offset) + 86400;
}
static time_t longitude_time_offset(double longitude) {
return longitude * 43200 / M_PI;
2020-10-18 22:39:38 +00:00
}
2020-10-14 15:57:34 +00:00
struct config {
2020-09-13 01:18:14 +00:00
int high_temp;
int low_temp;
2020-10-17 23:19:47 +00:00
double gamma;
2020-09-13 19:12:44 +00:00
double longitude;
double latitude;
2020-10-17 23:19:47 +00:00
int duration;
2020-10-14 15:57:34 +00:00
};
2020-09-13 01:18:14 +00:00
enum state {
STATE_INITIAL,
STATE_NORMAL,
STATE_TRANSITION,
STATE_STATIC,
};
2020-10-14 15:57:34 +00:00
struct context {
struct config config;
struct sun sun;
2020-10-04 17:49:45 +00:00
double longitude_time_offset;
enum state state;
enum sun_condition condition;
2020-10-14 13:50:25 +00:00
time_t dawn_step_time;
time_t dusk_step_time;
time_t calc_day;
2020-10-14 13:50:25 +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;
};
2020-10-18 22:37:02 +00:00
static void print_trajectory(struct context *ctx) {
fprintf(stderr, "calculated sun trajectory: ");
struct tm dawn, sunrise, sunset, dusk;
switch (ctx->condition) {
case NORMAL:
localtime_r(&ctx->sun.dawn, &dawn);
localtime_r(&ctx->sun.sunrise, &sunrise);
localtime_r(&ctx->sun.sunset, &sunset);
localtime_r(&ctx->sun.dusk, &dusk);
fprintf(stderr,
"dawn %02d:%02d, sunrise %02d:%02d, sunset %02d:%02d, dusk %02d:%02d\n",
dawn.tm_hour, dawn.tm_min,
sunrise.tm_hour, sunrise.tm_min,
sunset.tm_hour, sunset.tm_min,
dusk.tm_hour, dusk.tm_min);
break;
case MIDNIGHT_SUN:
fprintf(stderr, "midnight sun\n");
return;
case POLAR_NIGHT:
fprintf(stderr, "polar night\n");
return;
default:
abort();
}
}
2020-10-14 13:50:25 +00:00
static int anim_kelvin_step = 25;
2020-09-13 19:12:44 +00:00
static void recalc_stops(struct context *ctx, time_t now) {
time_t day = round_day_offset(now, -ctx->longitude_time_offset);
2020-10-20 22:41:50 +00:00
if (day == ctx->calc_day) {
2020-10-14 15:57:34 +00:00
return;
}
2020-10-20 22:41:50 +00:00
time_t last_day = ctx->calc_day;
ctx->calc_day = day;
2020-10-14 15:57:34 +00:00
struct sun sun;
2020-09-13 19:12:44 +00:00
struct tm tm = { 0 };
2020-10-14 15:57:34 +00:00
gmtime_r(&day, &tm);
enum sun_condition cond = calc_sun(&tm, ctx->config.latitude, &sun);
switch (cond) {
case NORMAL:
2020-10-18 22:37:02 +00:00
ctx->state = STATE_NORMAL;
ctx->sun.dawn = sun.dawn + day;
ctx->sun.sunrise = sun.sunrise + day;
ctx->sun.sunset = sun.sunset + day;
ctx->sun.dusk = sun.dusk + day;
if (ctx->condition == MIDNIGHT_SUN) {
// Yesterday had no sunset, so remove our sunrise.
2020-10-18 22:37:02 +00:00
ctx->sun.dawn = day;
ctx->sun.sunrise = day;
}
2020-10-18 22:37:02 +00:00
break;
case MIDNIGHT_SUN:
if (ctx->condition == POLAR_NIGHT) {
fprintf(stderr, "warning: direct polar night to midnight sun transition\n");
}
if (ctx->state != STATE_NORMAL) {
ctx->state = STATE_STATIC;
break;
}
// Borrow yesterday's sunrise to animate into the midnight sun
2020-10-18 22:37:02 +00:00
sun.dawn = ctx->sun.dawn - last_day + day;
sun.sunrise = ctx->sun.sunrise - last_day + day;
ctx->state = STATE_TRANSITION;
break;
case POLAR_NIGHT:
if (ctx->condition == MIDNIGHT_SUN) {
fprintf(stderr, "warning: direct midnight sun to polar night transition\n");
}
ctx->state = STATE_STATIC;
break;
default:
abort();
2020-10-04 13:27:46 +00:00
}
ctx->condition = cond;
2020-10-04 13:27:46 +00:00
2020-10-14 15:57:34 +00:00
int temp_diff = ctx->config.high_temp - ctx->config.low_temp;
2020-10-20 22:41:50 +00:00
ctx->dawn_step_time = (ctx->sun.sunrise - ctx->sun.dawn) *
anim_kelvin_step / temp_diff;
ctx->dusk_step_time = (ctx->sun.dusk - ctx->sun.sunset) *
anim_kelvin_step / temp_diff;
2020-10-14 13:50:25 +00:00
2020-10-18 22:37:02 +00:00
print_trajectory(ctx);
2020-10-04 13:27:46 +00:00
}
2020-10-20 22:41:50 +00:00
static int interpolate_temperature(time_t now, time_t start, time_t stop,
int temp_start, int temp_stop) {
if (start == stop) {
return stop;
}
2020-10-17 22:45:36 +00:00
double time_pos = (double)(now - start) / (double)(stop - start);
if (time_pos > 1.0) {
time_pos = 1.0;
} else if (time_pos < 0.0) {
time_pos = 0.0;
}
2020-10-04 13:27:46 +00:00
int temp_pos = (double)(temp_stop - temp_start) * time_pos;
return temp_start + temp_pos;
2020-09-13 19:12:44 +00:00
}
static int get_temperature_normal(const struct context *ctx, time_t now) {
2020-10-14 15:57:34 +00:00
if (now < ctx->sun.dawn) {
return ctx->config.low_temp;
} else if (now < ctx->sun.sunrise) {
2020-10-20 22:41:50 +00:00
return interpolate_temperature(now, ctx->sun.dawn,
ctx->sun.sunrise, ctx->config.low_temp,
ctx->config.high_temp);
2020-10-14 15:57:34 +00:00
} else if (now < ctx->sun.sunset) {
return ctx->config.high_temp;
} else if (now < ctx->sun.dusk) {
2020-10-20 22:41:50 +00:00
return interpolate_temperature(now, ctx->sun.sunset,
ctx->sun.dusk, ctx->config.high_temp,
ctx->config.low_temp);
2020-10-07 19:55:46 +00:00
} else {
2020-10-14 15:57:34 +00:00
return ctx->config.low_temp;
2020-09-13 01:18:14 +00:00
}
}
static int get_temperature_transition(const struct context *ctx, time_t now) {
switch (ctx->condition) {
case MIDNIGHT_SUN:
if (now < ctx->sun.sunrise) {
return get_temperature_normal(ctx, now);
}
return ctx->config.high_temp;
default:
abort();
}
}
2020-10-14 13:50:25 +00:00
static int get_temperature(const struct context *ctx, time_t now) {
switch (ctx->state) {
case STATE_NORMAL:
return get_temperature_normal(ctx, now);
case STATE_TRANSITION:
return get_temperature_transition(ctx, now);
case STATE_STATIC:
2020-10-20 22:41:50 +00:00
return ctx->condition == MIDNIGHT_SUN ? ctx->config.high_temp :
ctx->config.low_temp;
default:
abort();
}
}
static time_t get_deadline_normal(const struct context *ctx, time_t now) {
2020-10-14 15:57:34 +00:00
if (now < ctx->sun.dawn) {
return ctx->sun.dawn;
2020-10-14 15:57:34 +00:00
} else if (now < ctx->sun.sunrise) {
return now + ctx->dawn_step_time;
2020-10-14 15:57:34 +00:00
} else if (now < ctx->sun.sunset) {
return ctx->sun.sunset;
2020-10-14 15:57:34 +00:00
} else if (now < ctx->sun.dusk) {
return now + ctx->dusk_step_time;
} else {
return tomorrow(now, -ctx->longitude_time_offset);
}
}
static time_t get_deadline_transition(const struct context *ctx, time_t now) {
switch (ctx->condition) {
case MIDNIGHT_SUN:
if (now < ctx->sun.sunrise) {
return get_deadline_normal(ctx, now);
}
// fallthrough
case POLAR_NIGHT:
return tomorrow(now, -ctx->longitude_time_offset);
default:
abort();
}
}
static void update_timer(const struct context *ctx, timer_t timer, time_t now) {
time_t deadline;
switch (ctx->state) {
case STATE_NORMAL:
deadline = get_deadline_normal(ctx, now);
break;
case STATE_TRANSITION:
deadline = get_deadline_transition(ctx, now);
break;
case STATE_STATIC:
deadline = tomorrow(now, -ctx->longitude_time_offset);
break;
default:
abort();
}
2020-10-04 17:49:45 +00:00
assert(deadline > now);
2020-10-17 11:37:22 +00:00
struct itimerspec timerspec = {
.it_interval = {0},
.it_value = {
.tv_sec = deadline,
.tv_nsec = 0,
}
};
adjust_timerspec(&timerspec);
timer_settime(timer, TIMER_ABSTIME, &timerspec, NULL);
2020-09-13 01:18:14 +00:00
}
2020-10-18 22:39:38 +00:00
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";
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) {
(void)gamma_control;
struct output *output = data;
output->ramp_size = ramp_size;
if (output->table_fd != -1) {
close(output->table_fd);
}
output->table_fd = create_gamma_table(ramp_size, &output->table);
output->context->new_output = true;
if (output->table_fd < 0) {
fprintf(stderr, "could not create gamma table for output %d\n",
output->id);
exit(EXIT_FAILURE);
}
}
static void gamma_control_handle_failed(void *data,
struct zwlr_gamma_control_v1 *gamma_control) {
(void)gamma_control;
struct output *output = data;
fprintf(stderr, "gamma control of output %d failed\n",
output->id);
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;
}
}
static const struct zwlr_gamma_control_v1_listener gamma_control_listener = {
.gamma_size = gamma_control_handle_gamma_size,
.failed = gamma_control_handle_failed,
};
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;
}
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);
}
static void registry_handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version) {
(void)version;
struct context *ctx = (struct context *)data;
if (strcmp(interface, wl_output_interface.name) == 0) {
fprintf(stderr, "registry: adding output %d\n", name);
struct output *output = calloc(1, sizeof(struct output));
output->id = name;
output->wl_output = wl_registry_bind(registry, name,
&wl_output_interface, 1);
output->table_fd = -1;
output->context = ctx;
wl_list_insert(&ctx->outputs, &output->link);
setup_output(output);
} 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) {
(void)registry;
struct context *ctx = (struct context *)data;
struct output *output, *tmp;
wl_list_for_each_safe(output, tmp, &ctx->outputs, link) {
if (output->id == name) {
fprintf(stderr, "registry: removing output %d\n", name);
wl_list_remove(&output->link);
if (output->gamma_control != NULL) {
zwlr_gamma_control_v1_destroy(output->gamma_control);
}
if (output->table_fd != -1) {
close(output->table_fd);
}
free(output);
break;
}
}
}
static const struct wl_registry_listener registry_listener = {
.global = registry_handle_global,
.global_remove = registry_handle_global_remove,
};
2020-10-20 22:41:50 +00:00
static void fill_gamma_table(uint16_t *table, uint32_t ramp_size, double rw,
double gw, double bw, double gamma) {
2020-10-18 22:39:38 +00:00
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));
}
}
static void set_temperature(struct wl_list *outputs, int temp, int gamma) {
double rw, gw, bw;
calc_whitepoint(temp, &rw, &gw, &bw);
fprintf(stderr, "setting temperature to %d K\n", temp);
struct output *output;
wl_list_for_each(output, outputs, link) {
if (output->gamma_control == NULL || output->table_fd == -1) {
continue;
}
fill_gamma_table(output->table, output->ramp_size,
rw, gw, bw, gamma);
lseek(output->table_fd, 0, SEEK_SET);
zwlr_gamma_control_v1_set_gamma(output->gamma_control,
output->table_fd);
}
}
2020-10-18 20:41:22 +00:00
static int timer_fired = 0;
static int timer_signal_fds[2];
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);
}
2020-10-18 20:41:22 +00:00
struct pollfd pfd[2];
pfd[0].fd = wl_display_get_fd(display);
pfd[1].fd = timer_signal_fds[0];
pfd[0].events = POLLOUT;
while (wl_display_flush(display) == -1) {
if (errno != EAGAIN && errno != EPIPE) {
wl_display_cancel_read(display);
return -1;
}
// We only poll the wayland fd here
while (poll(pfd, 1, timeout) == -1) {
if (errno != EINTR) {
wl_display_cancel_read(display);
return -1;
}
2020-09-13 01:18:14 +00:00
}
2020-10-18 20:41:22 +00:00
}
2020-09-13 01:18:14 +00:00
2020-10-18 20:41:22 +00:00
pfd[0].events = POLLIN;
pfd[1].events = POLLIN;
while (poll(pfd, 2, timeout) == -1) {
if (errno != EINTR) {
2020-09-13 01:18:14 +00:00
wl_display_cancel_read(display);
return -1;
}
}
2020-10-18 20:41:22 +00:00
if (pfd[1].revents & POLLIN) {
// Empty signal fd
char garbage[8];
2020-10-20 22:41:50 +00:00
if (read(timer_signal_fds[0], &garbage, sizeof garbage) == -1
&& errno != EAGAIN) {
2020-10-18 20:41:22 +00:00
return -1;
}
2020-09-13 01:18:14 +00:00
}
2020-10-18 20:41:22 +00:00
if ((pfd[0].revents & POLLIN) == 0) {
2020-09-13 01:18:14 +00:00
wl_display_cancel_read(display);
2020-10-18 20:41:22 +00:00
return 0;
2020-09-13 01:18:14 +00:00
}
if (wl_display_read_events(display) == -1) {
return -1;
}
return wl_display_dispatch_pending(display);
2020-09-12 16:30:39 +00:00
}
2020-10-04 17:49:45 +00:00
static void timer_signal(int signal) {
(void)signal;
timer_fired = true;
2020-10-18 20:41:22 +00:00
if (write(timer_signal_fds[1], "\0", 1) == -1 && errno != EAGAIN) {
// This is unfortunate.
}
}
static int set_nonblock(int fd) {
int flags;
if ((flags = fcntl(fd, F_GETFL)) == -1 ||
fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
return -1;
}
return 0;
2020-10-04 17:49:45 +00:00
}
2020-10-17 23:19:47 +00:00
static int setup_timer(struct context *ctx) {
2020-10-04 17:49:45 +00:00
struct sigaction timer_action = {
.sa_handler = timer_signal,
.sa_flags = 0,
};
2020-10-18 20:41:22 +00:00
if (pipe(timer_signal_fds) == -1) {
2020-10-20 22:41:50 +00:00
fprintf(stderr, "could not create signal pipe: %s\n",
strerror(errno));
2020-10-18 20:41:22 +00:00
return -1;
}
if (set_nonblock(timer_signal_fds[0]) == -1 ||
set_nonblock(timer_signal_fds[1]) == -1) {
2020-10-20 22:41:50 +00:00
fprintf(stderr, "could not set nonblock on signal pipe: %s\n",
strerror(errno));
2020-10-18 20:41:22 +00:00
return -1;
}
2020-10-04 17:49:45 +00:00
if (sigaction(SIGALRM, &timer_action, NULL) == -1) {
2020-10-20 22:41:50 +00:00
fprintf(stderr, "could not configure alarm handler: %s\n",
strerror(errno));
2020-10-17 23:19:47 +00:00
return -1;
2020-10-04 17:49:45 +00:00
}
2020-10-17 23:19:47 +00:00
if (timer_create(CLOCK_REALTIME, NULL, &ctx->timer) == -1) {
2020-10-20 22:41:50 +00:00
fprintf(stderr, "could not configure timer: %s\n",
strerror(errno));
2020-10-17 23:19:47 +00:00
return -1;
2020-10-04 17:49:45 +00:00
}
2020-10-17 23:19:47 +00:00
return 0;
}
2020-10-04 17:49:45 +00:00
2020-10-17 23:19:47 +00:00
static int wlrun(struct config cfg) {
init_time();
2020-10-04 13:30:58 +00:00
2020-10-17 23:19:47 +00:00
// Initialize defaults
struct context ctx = {
.sun = { 0 },
.condition = SUN_CONDITION_LAST,
.longitude_time_offset = longitude_time_offset(cfg.longitude),
2020-10-17 23:19:47 +00:00
.state = STATE_INITIAL,
.config = cfg,
};
wl_list_init(&ctx.outputs);
2020-09-12 16:30:39 +00:00
2020-10-17 23:19:47 +00:00
if (setup_timer(&ctx) == -1) {
return EXIT_FAILURE;
2020-09-20 15:25:38 +00:00
}
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");
2020-10-17 23:19:47 +00:00
return EXIT_FAILURE;
2020-09-12 16:30:39 +00:00
}
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) {
2020-10-20 22:41:50 +00:00
fprintf(stderr, "compositor doesn't support wlr-gamma-control-unstable-v1\n");
2020-09-12 16:30:39 +00:00
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-17 11:37:22 +00:00
time_t now = get_time_sec();
2020-10-07 19:55:46 +00:00
recalc_stops(&ctx, now);
update_timer(&ctx, ctx.timer, now);
2020-10-07 19:55:46 +00:00
int temp = get_temperature(&ctx, now);
2020-10-17 23:19:47 +00:00
set_temperature(&ctx.outputs, temp, ctx.config.gamma);
2020-10-07 19:55:46 +00:00
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-17 11:37:22 +00:00
now = get_time_sec();
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;
2020-10-20 22:41:50 +00:00
set_temperature(&ctx.outputs, temp,
ctx.config.gamma);
2020-10-07 19:55:46 +00:00
}
} else if (ctx.new_output) {
ctx.new_output = false;
2020-10-17 23:19:47 +00:00
set_temperature(&ctx.outputs, temp, ctx.config.gamma);
2020-10-04 17:49:45 +00:00
}
2020-09-12 16:30:39 +00:00
}
return EXIT_SUCCESS;
}
2020-10-17 23:19:47 +00:00
static const char usage[] = "usage: %s [options]\n"
" -h show this help message\n"
" -t <temp> set low temperature (default: 4000)\n"
2020-10-20 23:41:49 +00:00
" -T <temp> set high temperature (default: 6500)\n"
2020-10-17 23:19:47 +00:00
" -l <lat> set latitude (e.g. 39.9)\n"
" -L <long> set longitude (e.g. 116.3)\n"
" -g <gamma> set gamma (default: 1.0)\n";
int main(int argc, char *argv[]) {
#ifdef SPEEDRUN
fprintf(stderr, "warning: speedrun mode enabled\n");
#endif
struct config config = {
.latitude = 0,
.longitude = 0,
.high_temp = 6500,
.low_temp = 4000,
.gamma = 1.0,
};
int opt;
while ((opt = getopt(argc, argv, "ht:T:l:L:d:g:")) != -1) {
switch (opt) {
case 't':
config.low_temp = strtol(optarg, NULL, 10);
break;
2020-10-20 23:41:49 +00:00
case 'T':
config.high_temp = strtol(optarg, NULL, 10);
break;
2020-10-17 23:19:47 +00:00
case 'l':
config.latitude = strtod(optarg, NULL);
break;
case 'L':
config.longitude = strtod(optarg, NULL);
break;
case 'g':
config.gamma = strtod(optarg, NULL);
break;
case 'h':
default:
fprintf(stderr, usage, argv[0]);
return opt == 'h' ? EXIT_SUCCESS : EXIT_FAILURE;
}
}
if (config.high_temp <= config.low_temp) {
fprintf(stderr, "high temp (%d) must be higher than low (%d) temp\n",
config.high_temp, config.low_temp);
return -1;
}
if (config.latitude > 90.0 || config.latitude < -90.0) {
2020-10-20 22:41:50 +00:00
fprintf(stderr, "latitude (%lf) must be in interval [-90,90]\n",
config.latitude);
2020-10-17 23:19:47 +00:00
return EXIT_FAILURE;
}
config.latitude = RADIANS(config.latitude);
if (config.longitude > 180.0 || config.longitude < -180.0) {
2020-10-20 22:41:50 +00:00
fprintf(stderr, "longitude (%lf) must be in interval [-180,180]\n",
config.longitude);
2020-10-17 23:19:47 +00:00
return EXIT_FAILURE;
}
config.longitude = RADIANS(config.longitude);
return wlrun(config);
}