From 0e318df13d4d98ffc69f2390562fc03aeca36f3b Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 26 Mar 2018 18:44:36 -0400 Subject: [PATCH] Add margin animation to the example demo --- examples/layer-shell.c | 27 ++++++++++++++++++++++----- rootston/layer_shell.c | 28 +++++++++++++++++++++------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/examples/layer-shell.c b/examples/layer-shell.c index 3518a56f..4c501d3d 100644 --- a/examples/layer-shell.c +++ b/examples/layer-shell.c @@ -14,6 +14,7 @@ static struct wl_compositor *compositor = NULL; static struct zwlr_layer_shell_v1 *layer_shell = NULL; +struct zwlr_layer_surface_v1 *layer_surface; static struct wl_output *wl_output = NULL; struct wl_surface *wl_surface; @@ -26,8 +27,11 @@ static uint32_t output = 0; static uint32_t layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND; static uint32_t anchor = 0; static uint32_t width = 256, height = 256; +static int32_t margin_top = 0; static double alpha = 1.0; static bool run_display = true; +static bool animate = false; +static double frame = 0; static struct { struct timespec last_frame; @@ -67,6 +71,18 @@ static void draw(void) { demo.dec = inc; } + if (animate) { + frame += ms / 50.0; + int32_t old_top = margin_top; + margin_top = -(20 - ((int)frame % 20)); + if (old_top != margin_top) { + wlr_log(L_DEBUG, "setting margin to %d", margin_top); + zwlr_layer_surface_v1_set_margin(layer_surface, + margin_top, 0, 0, 0); + wl_surface_commit(wl_surface); + } + } + glViewport(0, 0, width, height); glClearColor(demo.color[0], demo.color[1], demo.color[2], alpha); glClear(GL_COLOR_BUFFER_BIT); @@ -136,11 +152,10 @@ int main(int argc, char **argv) { wlr_log_init(L_DEBUG, NULL); char *namespace = "wlroots"; int exclusive_zone = 0; - int32_t margin_top = 0, margin_right = 0, - margin_bottom = 0, margin_left = 0; + int32_t margin_right = 0, margin_bottom = 0, margin_left = 0; bool found; int c; - while ((c = getopt(argc, argv, "w:h:o:l:a:x:m:t:")) != -1) { + while ((c = getopt(argc, argv, "nw:h:o:l:a:x:m:t:")) != -1) { switch (c) { case 'o': output = atoi(optarg); @@ -217,6 +232,9 @@ int main(int argc, char **argv) { assert(!*endptr); break; } + case 'n': + animate = true; + break; default: break; } @@ -253,8 +271,7 @@ int main(int argc, char **argv) { wl_surface = wl_compositor_create_surface(compositor); assert(wl_surface); - struct zwlr_layer_surface_v1 *layer_surface = - zwlr_layer_shell_v1_get_layer_surface(layer_shell, + layer_surface = zwlr_layer_shell_v1_get_layer_surface(layer_shell, wl_surface, wl_output, layer, namespace); assert(layer_surface); zwlr_layer_surface_v1_set_size(layer_surface, width, height); diff --git a/rootston/layer_shell.c b/rootston/layer_shell.c index e2d5b9f0..35896228 100644 --- a/rootston/layer_shell.c +++ b/rootston/layer_shell.c @@ -12,7 +12,9 @@ #include "rootston/server.h" static void apply_exclusive(struct wlr_box *usable_area, - uint32_t anchor, int32_t exclusive) { + uint32_t anchor, int32_t exclusive, + int32_t margin_top, int32_t margin_right, + int32_t margin_bottom, int32_t margin_left) { if (exclusive <= 0) { return; } @@ -20,6 +22,7 @@ static void apply_exclusive(struct wlr_box *usable_area, uint32_t anchors; int *positive_axis; int *negative_axis; + int margin; } edges[] = { { .anchors = @@ -28,6 +31,7 @@ static void apply_exclusive(struct wlr_box *usable_area, ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP, .positive_axis = &usable_area->y, .negative_axis = &usable_area->height, + .margin = margin_top, }, { .anchors = @@ -36,6 +40,7 @@ static void apply_exclusive(struct wlr_box *usable_area, ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, .positive_axis = NULL, .negative_axis = &usable_area->height, + .margin = margin_bottom, }, { .anchors = @@ -44,6 +49,7 @@ static void apply_exclusive(struct wlr_box *usable_area, ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, .positive_axis = &usable_area->x, .negative_axis = &usable_area->width, + .margin = margin_left, }, { .anchors = @@ -52,15 +58,16 @@ static void apply_exclusive(struct wlr_box *usable_area, ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, .positive_axis = NULL, .negative_axis = &usable_area->width, + .margin = margin_right, }, }; for (size_t i = 0; i < sizeof(edges) / sizeof(edges[0]); ++i) { if ((anchor & edges[i].anchors) == edges[i].anchors) { if (edges[i].positive_axis) { - *edges[i].positive_axis += exclusive; + *edges[i].positive_axis += exclusive + edges[i].margin; } if (edges[i].negative_axis) { - *edges[i].negative_axis -= exclusive; + *edges[i].negative_axis -= exclusive + edges[i].margin; } } } @@ -121,15 +128,15 @@ static void arrange_layer(struct wlr_output *output, struct wl_list *list, box.y = bounds.y + ((bounds.height / 2) - (box.height / 2)); } // Margin - if ((state->anchor & both_horiz)) { + if ((state->anchor & both_horiz) == both_horiz) { box.x += state->margin.left; box.width -= state->margin.left + state->margin.right; } else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT)) { - box.x += state->margin.bottom; + box.x += state->margin.left; } else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) { box.x -= state->margin.right; } - if ((state->anchor & both_vert)) { + if ((state->anchor & both_vert) == both_vert) { box.y += state->margin.top; box.height -= state->margin.top + state->margin.bottom; } else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP)) { @@ -139,7 +146,11 @@ static void arrange_layer(struct wlr_output *output, struct wl_list *list, } // Apply roots_surface->geo = box; - apply_exclusive(usable_area, state->anchor, state->exclusive_zone); + apply_exclusive(usable_area, state->anchor, state->exclusive_zone, + state->margin.top, state->margin.right, + state->margin.bottom, state->margin.left); + wlr_log(L_DEBUG, "arranged layer at %dx%d@%d,%d", + box.width, box.height, box.x, box.y); wlr_layer_surface_configure(layer, box.width, box.height); } } @@ -212,6 +223,9 @@ static void handle_surface_commit(struct wl_listener *listener, void *data) { struct roots_output *output = wlr_output->data; output_damage_from_local_surface(output, layer_surface->surface, layer->geo.x, layer->geo.y, 0); + arrange_layers(wlr_output); + output_damage_from_local_surface(output, layer_surface->surface, + layer->geo.x, layer->geo.y, 0); } }