diff --git a/swaybg/CMakeLists.txt b/swaybg/CMakeLists.txt index 8da31faa..5a46ffb8 100644 --- a/swaybg/CMakeLists.txt +++ b/swaybg/CMakeLists.txt @@ -23,7 +23,7 @@ add_executable(swaybg ${common} ) -TARGET_LINK_LIBRARIES(swaybg ${WAYLAND_CLIENT_LIBRARIES} ${CAIRO_LIBRARIES} ${PANGO_LIBRARIES}) +TARGET_LINK_LIBRARIES(swaybg ${WAYLAND_CLIENT_LIBRARIES} ${CAIRO_LIBRARIES} ${PANGO_LIBRARIES} -lm) install( TARGETS swaybg diff --git a/swaybg/main.c b/swaybg/main.c index 5ceb94bc..af0a1b27 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "client.h" #include "log.h" @@ -17,18 +18,28 @@ int main(int argc, char **argv) { uint8_t r = 0, g = 0, b = 0; + long last_ms = 0; int rs; do { - if (!client_prerender(state)) continue; + struct timespec spec; + clock_gettime(CLOCK_MONOTONIC, &spec); + long ms = round(spec.tv_nsec / 1.0e6); + cairo_set_source_rgb(state->cairo, r, g, b); cairo_rectangle(state->cairo, 0, 0, 100, 100); cairo_fill(state->cairo); rs = client_render(state); - if (rs == 1) { - sway_log(L_INFO, "rendering %d %d %d", r, g, b); - r++; g++; b++; + if (ms - last_ms > 100) { + r++; + if (r == 0) { + g++; + if (g == 0) { + b++; + } + } + ms = last_ms; } } while (rs); diff --git a/wayland/client.c b/wayland/client.c index 5c5c0751..fcbba885 100644 --- a/wayland/client.c +++ b/wayland/client.c @@ -93,7 +93,7 @@ static int create_pool_file(size_t size) { if (fd < 0) { return -1; - } + } if (ftruncate(fd, size) < 0) { close(fd); @@ -125,6 +125,9 @@ struct buffer *create_buffer(struct client_state *state, void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); buf->pool = wl_shm_create_pool(state->shm, fd, size); buf->buffer = wl_shm_pool_create_buffer(buf->pool, 0, width, height, stride, format); + wl_shm_pool_destroy(buf->pool); + close(fd); + fd = -1; state->cairo_surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride); state->cairo = cairo_create(state->cairo_surface); @@ -175,22 +178,21 @@ struct client_state *client_setup(void) { return state; } -int client_prerender(struct client_state *state) { - wl_display_dispatch_pending(state->display); - wl_display_flush(state->display); - return 1; -} - int client_render(struct client_state *state) { - if (state->frame_cb) { + if (state->frame_cb || state->busy) { return 2; } + sway_log(L_INFO, "Rendering"); + state->frame_cb = wl_surface_frame(state->surface); wl_callback_add_listener(state->frame_cb, &listener, state); - wl_surface_damage(state->surface, 0, 0, 100, 100); + + wl_surface_damage(state->surface, 0, 0, state->buffer->width, state->buffer->height); wl_surface_attach(state->surface, state->buffer->buffer, 0, 0); wl_surface_commit(state->surface); + state->busy = true; + return wl_display_dispatch(state->display) != -1; }