You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
4.1 KiB
157 lines
4.1 KiB
8 years ago
|
#define _POSIX_C_SOURCE 199309L
|
||
|
#include <stdio.h>
|
||
8 years ago
|
#include <stdlib.h>
|
||
8 years ago
|
#include <time.h>
|
||
8 years ago
|
#include <wayland-server.h>
|
||
8 years ago
|
#include <GLES3/gl3.h>
|
||
8 years ago
|
#include <wlr/backend.h>
|
||
8 years ago
|
#include <wlr/session.h>
|
||
8 years ago
|
#include <wlr/types.h>
|
||
8 years ago
|
|
||
8 years ago
|
struct state {
|
||
|
float color[3];
|
||
|
int dec;
|
||
|
struct timespec last_frame;
|
||
|
struct wl_listener output_add;
|
||
|
struct wl_listener output_remove;
|
||
8 years ago
|
struct wl_list outputs;
|
||
8 years ago
|
};
|
||
|
|
||
|
struct output_state {
|
||
8 years ago
|
struct wl_list link;
|
||
8 years ago
|
struct wlr_output *output;
|
||
|
struct state *state;
|
||
|
struct wl_listener frame;
|
||
|
};
|
||
|
|
||
|
void output_frame(struct wl_listener *listener, void *data) {
|
||
8 years ago
|
struct output_state *ostate = wl_container_of(listener, ostate, frame);
|
||
8 years ago
|
struct state *s = ostate->state;
|
||
|
|
||
|
struct timespec now;
|
||
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||
|
|
||
|
long ms = (now.tv_sec - s->last_frame.tv_sec) * 1000 +
|
||
|
(now.tv_nsec - s->last_frame.tv_nsec) / 1000000;
|
||
|
int inc = (s->dec + 1) % 3;
|
||
|
|
||
|
s->color[inc] += ms / 2000.0f;
|
||
|
s->color[s->dec] -= ms / 2000.0f;
|
||
|
|
||
|
if (s->color[s->dec] < 0.0f) {
|
||
|
s->color[inc] = 1.0f;
|
||
|
s->color[s->dec] = 0.0f;
|
||
|
s->dec = inc;
|
||
|
}
|
||
|
|
||
|
s->last_frame = now;
|
||
|
|
||
|
glClearColor(s->color[0], s->color[1], s->color[2], 1.0);
|
||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||
|
}
|
||
|
|
||
|
void output_add(struct wl_listener *listener, void *data) {
|
||
|
struct wlr_output *output = data;
|
||
|
struct state *state = wl_container_of(listener, state, output_add);
|
||
|
fprintf(stderr, "Output '%s' added\n", output->name);
|
||
|
wlr_output_set_mode(output, output->modes->items[0]);
|
||
|
struct output_state *ostate = calloc(1, sizeof(struct output_state));
|
||
|
ostate->output = output;
|
||
|
ostate->state = state;
|
||
|
ostate->frame.notify = output_frame;
|
||
|
wl_list_init(&ostate->frame.link);
|
||
|
wl_signal_add(&output->events.frame, &ostate->frame);
|
||
8 years ago
|
wl_list_insert(&state->outputs, &ostate->link);
|
||
8 years ago
|
}
|
||
|
|
||
|
void output_remove(struct wl_listener *listener, void *data) {
|
||
|
struct wlr_output *output = data;
|
||
8 years ago
|
struct state *state = wl_container_of(listener, state, output_remove);
|
||
8 years ago
|
struct output_state *ostate = NULL, *_ostate;
|
||
|
wl_list_for_each(_ostate, &state->outputs, link) {
|
||
8 years ago
|
if (_ostate->output == output) {
|
||
|
ostate = _ostate;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if (!ostate) {
|
||
|
return; // We are unfamiliar with this output
|
||
|
}
|
||
8 years ago
|
wl_list_remove(&ostate->link);
|
||
8 years ago
|
wl_list_remove(&ostate->frame.link);
|
||
8 years ago
|
}
|
||
|
|
||
|
int timer_done(void *data) {
|
||
|
*(bool *)data = true;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
8 years ago
|
int enable_outputs(void *data) {
|
||
|
struct state *state = data;
|
||
8 years ago
|
struct output_state *ostate;
|
||
|
wl_list_for_each(ostate, &state->outputs, link) {
|
||
8 years ago
|
struct wlr_output *output = ostate->output;
|
||
|
wlr_output_enable(output, true);
|
||
|
}
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
8 years ago
|
int disable_outputs(void *data) {
|
||
|
struct state *state = data;
|
||
8 years ago
|
struct output_state *ostate;
|
||
|
wl_list_for_each(ostate, &state->outputs, link) {
|
||
8 years ago
|
struct wlr_output *output = ostate->output;
|
||
|
wlr_output_enable(output, false);
|
||
|
}
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
8 years ago
|
int main() {
|
||
|
struct state state = {
|
||
|
.color = { 1.0, 0.0, 0.0 },
|
||
|
.dec = 0,
|
||
|
.output_add = { .notify = output_add },
|
||
8 years ago
|
.output_remove = { .notify = output_remove }
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
wl_list_init(&state.outputs);
|
||
8 years ago
|
wl_list_init(&state.output_add.link);
|
||
|
wl_list_init(&state.output_remove.link);
|
||
|
clock_gettime(CLOCK_MONOTONIC, &state.last_frame);
|
||
|
|
||
|
struct wl_display *display = wl_display_create();
|
||
|
struct wl_event_loop *event_loop = wl_display_get_event_loop(display);
|
||
|
|
||
8 years ago
|
struct wlr_session *session = wlr_session_start(display);
|
||
8 years ago
|
if (!session) {
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
8 years ago
|
struct wlr_backend *wlr = wlr_backend_autocreate(display, session);
|
||
8 years ago
|
wl_signal_add(&wlr->events.output_add, &state.output_add);
|
||
|
wl_signal_add(&wlr->events.output_remove, &state.output_remove);
|
||
|
if (!wlr || !wlr_backend_init(wlr)) {
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
bool done = false;
|
||
|
struct wl_event_source *timer = wl_event_loop_add_timer(event_loop,
|
||
|
timer_done, &done);
|
||
8 years ago
|
struct wl_event_source *timer_disable_outputs =
|
||
|
wl_event_loop_add_timer(event_loop, disable_outputs, &state);
|
||
|
struct wl_event_source *timer_enable_outputs =
|
||
|
wl_event_loop_add_timer(event_loop, enable_outputs, &state);
|
||
8 years ago
|
|
||
|
wl_event_source_timer_update(timer, 20000);
|
||
8 years ago
|
wl_event_source_timer_update(timer_disable_outputs, 5000);
|
||
|
wl_event_source_timer_update(timer_enable_outputs, 10000);
|
||
8 years ago
|
|
||
|
while (!done) {
|
||
|
wl_event_loop_dispatch(event_loop, 0);
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
wl_event_source_remove(timer);
|
||
|
wlr_backend_destroy(wlr);
|
||
|
wl_display_destroy(display);
|
||
8 years ago
|
}
|