Migrated to the scene graph API

This commit is contained in:
Keith Bowes 2022-02-27 13:57:34 -05:00
parent 03fbe71903
commit d7b799ab80
12 changed files with 196 additions and 271 deletions

View file

@ -6,6 +6,7 @@
#include <wlr/render/wlr_renderer.h>
#include <wlr/render/wlr_texture.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_xdg_output_v1.h>
#include "waybox/server.h"
@ -14,7 +15,9 @@ struct wb_output {
struct wlr_output *wlr_output;
struct wb_server *server;
struct wl_list layers[4];
struct wl_list layers[4]; /* wb_layer_surface::link */
struct wlr_scene_rect *scene_rect;
struct wl_listener destroy;
struct wl_listener frame;
@ -29,9 +32,9 @@ struct wb_view {
#if !WLR_CHECK_VERSION(0, 16, 0)
struct wlr_xdg_surface *xdg_surface;
#endif
struct wlr_scene_node *scene_node;
struct wlr_xdg_toplevel_decoration_v1 *decoration;
int decoration_height;
struct wl_listener map;
struct wl_listener unmap;
@ -41,8 +44,6 @@ struct wb_view {
struct wl_listener request_minimize;
struct wl_listener request_move;
struct wl_listener request_resize;
struct wl_listener surface_commit;
bool mapped;
struct wlr_box current_position;
struct wlr_box previous_position;

View file

@ -16,7 +16,6 @@
#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_idle.h>
#include <wlr/types/wlr_screencopy_v1.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/types/wlr_gamma_control_v1.h>
#include <wlr/types/wlr_output_layout.h>
#if WLR_CHECK_VERSION(0, 16, 0)
@ -49,6 +48,7 @@ struct wb_server {
struct wlr_output_layout *output_layout;
struct wlr_xdg_output_manager_v1 *output_manager;
struct wlr_renderer *renderer;
struct wlr_scene *scene;
#if WLR_CHECK_VERSION(0, 16, 0)
struct wlr_subcompositor *subcompositor;
#endif

View file

@ -5,7 +5,7 @@
void init_xdg_shell(struct wb_server *server);
void focus_view(struct wb_view *view, struct wlr_surface *surface);
struct wb_view *desktop_view_at(
struct wb_view *get_view_at(
struct wb_server *server, double lx, double ly,
struct wlr_surface **surface, double *sx, double *sy);
#endif

View file

@ -4,16 +4,16 @@
#include "waybox/server.h"
enum action_type {
ACTION_CLOSE = 1,
ACTION_EXECUTE = 2,
ACTION_EXIT = 4,
ACTION_ICONIFY = 8,
ACTION_NEXT_WINDOW = 16,
ACTION_PREVIOUS_WINDOW = 32,
ACTION_RECONFIGURE = 64,
ACTION_SHADE = 128,
ACTION_TOGGLE_MAXIMIZE = 256,
ACTION_UNSHADE = 512,
ACTION_EXECUTE = 1<<0,
ACTION_EXIT = 1<<1,
ACTION_NEXT_WINDOW = 1<<2,
ACTION_PREVIOUS_WINDOW = 1<<3,
ACTION_CLOSE = 1<<4,
ACTION_RECONFIGURE = 1<<5,
ACTION_ICONIFY = 1<<6,
ACTION_TOGGLE_MAXIMIZE = 1<<7,
ACTION_SHADE = 1<<8,
ACTION_UNSHADE = 1<<9,
};
struct wb_config {

View file

@ -4,8 +4,11 @@
static void process_cursor_move(struct wb_server *server) {
/* Move the grabbed view to the new position. */
server->grabbed_view->current_position.x = server->cursor->cursor->x - server->grab_x;
server->grabbed_view->current_position.y = server->cursor->cursor->y - server->grab_y;
struct wb_view *view = server->grabbed_view;
view->current_position.x = server->cursor->cursor->x - server->grab_x;
view->current_position.y = server->cursor->cursor->y - server->grab_y;
wlr_scene_node_set_position(view->scene_node,
view->current_position.x, view->current_position.y);
}
static void process_cursor_resize(struct wb_server *server) {
@ -68,7 +71,7 @@ static void process_cursor_motion(struct wb_server *server, uint32_t time) {
double sx, sy;
struct wlr_seat *seat = server->seat->seat;
struct wlr_surface *surface = NULL;
struct wb_view *view = desktop_view_at(server,
struct wb_view *view = get_view_at(server,
server->cursor->cursor->x, server->cursor->cursor->y, &surface, &sx, &sy);
if (!view) {
/* If there's no view under the cursor, set the cursor image to a
@ -120,7 +123,7 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) {
event->time_msec, event->button, event->state);
double sx, sy;
struct wlr_surface *surface = NULL;
struct wb_view *view = desktop_view_at(cursor->server,
struct wb_view *view = get_view_at(cursor->server,
cursor->server->cursor->cursor->x, cursor->server->cursor->cursor->y, &surface, &sx, &sy);
if (event->state == WLR_BUTTON_RELEASED) {
/* If you released any buttons, we exit interactive move/resize mode. */

View file

@ -1,21 +1,18 @@
#include "decoration.h"
static void destroy_xdg_toplevel_decoration(struct wl_listener *listener, void *data)
{
static void destroy_xdg_toplevel_decoration(struct wl_listener *listener, void *data) {
struct wb_decoration *decoration = wl_container_of(listener, decoration, toplevel_decoration_destroy);
wl_list_remove(&decoration->toplevel_decoration_destroy.link);
free(decoration);
}
static void free_xdg_decoration_mode(struct wl_listener *listener, void *data)
{
static void free_xdg_decoration_mode(struct wl_listener *listener, void *data) {
struct wb_decoration *decoration = wl_container_of(listener, decoration, mode_destroy);
wl_list_remove(&decoration->mode_destroy.link);
wl_list_remove(&decoration->request_mode.link);
}
static void handle_xdg_decoration_mode(struct wl_listener *listener, void *data)
{
static void handle_xdg_decoration_mode(struct wl_listener *listener, void *data) {
struct wlr_xdg_toplevel_decoration_v1 *toplevel_decoration = data;
struct wb_decoration *decoration = wl_container_of(listener, decoration, request_mode);
struct wb_view *view = wl_container_of(decoration->server->views.next, view, link);
@ -23,8 +20,7 @@ static void handle_xdg_decoration_mode(struct wl_listener *listener, void *data)
view->decoration = toplevel_decoration;
}
static void handle_new_xdg_toplevel_decoration(struct wl_listener *listener, void *data)
{
static void handle_new_xdg_toplevel_decoration(struct wl_listener *listener, void *data) {
struct wb_decoration *decoration = (struct wb_decoration *) calloc(1, sizeof(struct wb_decoration));
struct wb_server *server = wl_container_of(listener, server, new_xdg_decoration);
decoration->server = server;
@ -39,8 +35,7 @@ static void handle_new_xdg_toplevel_decoration(struct wl_listener *listener, voi
handle_xdg_decoration_mode(&decoration->request_mode, toplevel_decoration);
}
void init_xdg_decoration(struct wb_server *server)
{
void init_xdg_decoration(struct wb_server *server) {
struct wlr_xdg_decoration_manager_v1 *decoration = wlr_xdg_decoration_manager_v1_create(server->wl_display);
server->new_xdg_decoration.notify = handle_new_xdg_toplevel_decoration;
wl_signal_add(&decoration->events.new_toplevel_decoration, &server->new_xdg_decoration);

View file

@ -1,8 +1,9 @@
/*
* More or less taken verbatim from wio <https://git.sr.ht/~sircmpwn/wio>, so in
* accordance with its MIT license:
* More or less taken verbatim from wio <https://git.sr.ht/~sircmpwn/wio>.
* Additional material taken from sway <https://github.com/swaywm/sway>.
*
* Copyright 2019 Drew DeVault
* Copyright 2022 Sway Developers
*/
#include <wlr/types/wlr_layer_shell_v1.h>
#include "waybox/xdg_shell.h"
@ -285,9 +286,9 @@ void server_new_layer_surface(struct wl_listener *listener, void *data) {
layer_surface->current = old_state;
}
void init_layer_shell(struct wb_server *server)
{
void init_layer_shell(struct wb_server *server) {
server->layer_shell = wlr_layer_shell_v1_create(server->wl_display);
return;
server->new_layer_surface.notify = server_new_layer_surface;
wl_signal_add(&server->layer_shell->events.new_surface,
&server->new_layer_surface);

View file

@ -4,8 +4,7 @@
#include "waybox/server.h"
bool show_help(char *name)
{
bool show_help(char *name) {
printf(_("Syntax: %s [options]\n"), name);
printf(_("\nOptions:\n"));
printf(_(" --help Display this help and exit\n"));
@ -22,8 +21,7 @@ bool show_help(char *name)
}
struct wb_server server = {0};
void signal_handler(int sig)
{
void signal_handler(int sig) {
switch (sig) {
case SIGINT:
case SIGTERM:

View file

@ -1,73 +1,6 @@
#include "waybox/output.h"
struct render_data {
struct wlr_output *output;
struct wlr_renderer *renderer;
struct wb_view *view;
struct timespec *when;
};
static void render_surface(struct wlr_surface *surface,
int sx, int sy, void *data) {
/* This function is called for every surface that needs to be rendered. */
struct render_data *rdata = data;
struct wb_view *view = rdata->view;
struct wlr_output *output = rdata->output;
/* We first obtain a wlr_texture, which is a GPU resource. wlroots
* automatically handles negotiating these with the client. The underlying
* resource could be an opaque handle passed from the client, or the client
* could have sent a pixel buffer which we copied to the GPU, or a few other
* means. You don't have to worry about this, wlroots takes care of it. */
struct wlr_texture *texture = wlr_surface_get_texture(surface);
if (texture == NULL) {
wlr_log(WLR_ERROR, "%s", _("Couldn't get a surface texture"));
return;
}
/* The view has a position in layout coordinates. If you have two displays,
* one next to the other, both 1080p, a view on the rightmost display might
* have layout coordinates of 2000,100. We need to translate that to
* output-local coordinates, or (2000 - 1920). */
double ox = 0, oy = 0;
wlr_output_layout_output_coords(
view->server->output_layout, output, &ox, &oy);
ox += view->current_position.x + sx, oy += view->current_position.y + sy;
/* We also have to apply the scale factor for HiDPI outputs. This is only
* part of the puzzle, Waybox does not fully support HiDPI. */
struct wlr_box box = {
.x = ox * output->scale,
.y = oy * output->scale,
.width = surface->current.width * output->scale,
.height = surface->current.height * output->scale,
};
/*
* Those familiar with OpenGL are also familiar with the role of matrices
* in graphics programming. We need to prepare a matrix to render the view
* with. wlr_matrix_project_box is a helper which takes a box with a desired
* x, y coordinates, width and height, and an output geometry, then
* prepares an orthographic projection and multiplies the necessary
* transforms to produce a model-view-projection matrix.
*
* Naturally you can do this any way you like.
*/
float matrix[9];
enum wl_output_transform transform =
wlr_output_transform_invert(surface->current.transform);
wlr_matrix_project_box(matrix, &box, transform, 0,
output->transform_matrix);
/* This takes our matrix, the texture, and an alpha, and performs the actual
* rendering on the GPU. */
wlr_render_texture_with_matrix(rdata->renderer, texture, matrix, 1);
/* This lets the client know that we've displayed that frame and it can
* prepare another one now if it likes. */
wlr_surface_send_frame_done(surface, rdata->when);
}
#if !WLR_CHECK_VERSION(0, 16, 0)
static void render_layer_surface(struct wlr_surface *surface,
int sx, int sy, void *data) {
struct wb_layer_surface *layer_surface = data;
@ -81,12 +14,8 @@ static void render_layer_surface(struct wlr_surface *surface,
layer_surface->server->output_layout, output, &ox, &oy);
ox += layer_surface->geo.x + sx, oy += layer_surface->geo.y + sy;
float matrix[9];
enum wl_output_transform transform =
wlr_output_transform_invert(surface->current.transform);
struct wlr_box box;
memcpy(&box, &layer_surface->geo, sizeof(struct wlr_box));
wlr_matrix_project_box(matrix, &box, transform, 0,
output->transform_matrix);
wlr_render_texture_with_matrix(layer_surface->server->renderer,
texture, matrix, 1);
struct timespec now;
@ -104,60 +33,42 @@ static void render_layer(
render_layer_surface, layer_surface);
}
}
#endif
void output_frame_notify(struct wl_listener *listener, void *data) {
struct wb_output *output = wl_container_of(listener, output, frame);
struct wlr_renderer *renderer = output->server->renderer;
struct wlr_scene *scene = output->server->scene;
struct wlr_scene_output *scene_output = wlr_scene_get_scene_output(
scene, output->wlr_output);
wlr_scene_rect_set_size(output->scene_rect, output->wlr_output->width, output->wlr_output->height);
#if !WLR_CHECK_VERSION(0, 16, 0)
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND]);
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]);
#endif
/* Render the scene if needed and commit the output */
wlr_scene_output_commit(scene_output);
#if !WLR_CHECK_VERSION(0, 16, 0)
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]);
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);
#endif
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (!wlr_output_attach_render(output->wlr_output, NULL)) {
wlr_log_errno(WLR_ERROR, "%s", _("Couldn't attach renderer to output"));
return;
}
int width, height;
wlr_output_effective_resolution(output->wlr_output, &width, &height);
wlr_renderer_begin(renderer, width, height);
float color[4] = {0.4f, 0.4f, 0.4f, 1.0f};
wlr_renderer_clear(renderer, color);
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND]);
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]);
struct wb_view *view;
wl_list_for_each_reverse(view, &output->server->views, link) {
if (!view->mapped)
/* An unmapped view should not be rendered */
continue;
struct render_data rdata = {
.output = output->wlr_output,
.renderer = renderer,
.view = view,
.when = &now,
};
if (view->xdg_toplevel->base->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL)
wlr_xdg_surface_for_each_surface(view->xdg_toplevel->base,
render_surface, &rdata);
}
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]);
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);
wlr_output_render_software_cursors(output->wlr_output, NULL);
wlr_renderer_end(renderer);
wlr_output_commit(output->wlr_output);
wlr_scene_output_send_frame_done(scene_output, &now);
}
void output_destroy_notify(struct wl_listener *listener, void *data) {
struct wb_output *output = wl_container_of(listener, output, destroy);
wlr_output_layout_remove(output->server->output_layout, output->wlr_output);
struct wb_output *output = wl_container_of(listener, output, destroy);
wl_list_remove(&output->link);
wl_list_remove(&output->destroy.link);
wl_list_remove(&output->frame.link);
free(output);
}
@ -189,6 +100,9 @@ void new_output_notify(struct wl_listener *listener, void *data) {
wlr_output->data = output;
wl_list_insert(&server->outputs, &output->link);
float color[4] = {0.3, 0.3, 0.3, 1.0};
output->scene_rect = wlr_scene_rect_create(&server->scene->node, 0, 0, color);
wl_list_init(&output->layers[0]);
wl_list_init(&output->layers[1]);
wl_list_init(&output->layers[2]);

View file

@ -15,10 +15,12 @@ static void cycle_views(struct wb_server *server) {
if (wl_list_length(&server->views) < 1) {
return;
}
struct wb_view *current_view = wl_container_of(
server->views.prev, current_view, link);
deiconify_view(current_view);
focus_view(current_view, current_view->xdg_toplevel->base->surface);
/* Move the current view to the beginning of the list */
wl_list_remove(&current_view->link);
wl_list_insert(&server->views, &current_view->link);
@ -29,12 +31,14 @@ static void cycle_views_reverse(struct wb_server *server) {
if (wl_list_length(&server->views) < 1) {
return;
}
struct wb_view *current_view = wl_container_of(
server->views.next, current_view, link);
struct wb_view *next_view = wl_container_of(
current_view->link.next, next_view, link);
deiconify_view(next_view);
focus_view(next_view, next_view->xdg_toplevel->base->surface);
/* Move the current view to after the previous view in the list */
wl_list_remove(&current_view->link);
wl_list_insert(server->views.prev, &current_view->link);
@ -74,7 +78,7 @@ static bool handle_keybinding(struct wb_server *server, xkb_keysym_t sym, uint32
if (key_binding->action & ACTION_CLOSE) {
struct wb_view *current_view = wl_container_of(
server->views.next, current_view, link);
if (current_view->mapped)
if (current_view->scene_node->state.enabled)
#if WLR_CHECK_VERSION(0, 16, 0)
wlr_xdg_toplevel_send_close(current_view->xdg_toplevel);
#else
@ -88,34 +92,37 @@ static bool handle_keybinding(struct wb_server *server, xkb_keysym_t sym, uint32
}
if (key_binding->action & ACTION_TOGGLE_MAXIMIZE) {
struct wb_view *view = wl_container_of(server->views.next, view, link);
if (view->mapped)
if (view->scene_node->state.enabled)
wl_signal_emit(&view->xdg_toplevel->events.request_maximize, NULL);
}
if (key_binding->action & ACTION_ICONIFY) {
struct wb_view *view = wl_container_of(server->views.next, view, link);
if (view->mapped) {
if (view->scene_node->state.enabled) {
view->xdg_toplevel->requested.minimized = true;
wl_signal_emit(&view->xdg_toplevel->events.request_minimize, NULL);
struct wb_view *previous_view = wl_container_of(server->views.prev, previous_view, link);
focus_view(previous_view, previous_view->xdg_toplevel->base->surface);
}
}
if (key_binding->action & ACTION_SHADE) {
struct wb_view *view = wl_container_of(server->views.next, view, link);
if (view->mapped) {
if (view->scene_node->state.enabled) {
struct wlr_box geo_box;
wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo_box);
/* TODO: Get the minimum height from the theme rather than hard-coded. */
int decoration_height = MAX(geo_box.y - view->current_position.y, 8);
view->previous_position = view->current_position;
#if WLR_CHECK_VERSION(0, 16, 0)
wlr_xdg_toplevel_set_size(view->xdg_toplevel,
view->current_position.width, view->decoration_height);
view->current_position.width, decoration_height);
#else
wlr_xdg_toplevel_set_size(view->xdg_surface,
view->current_position.width, view->decoration_height);
view->current_position.width, decoration_height);
#endif
}
}
if (key_binding->action & ACTION_UNSHADE) {
struct wb_view *view = wl_container_of(server->views.next, view, link);
if (view->mapped) {
if (view->scene_node->state.enabled) {
#if WLR_CHECK_VERSION(0, 16, 0)
wlr_xdg_toplevel_set_size(view->xdg_toplevel,
view->previous_position.width, view->previous_position.height);

View file

@ -54,6 +54,15 @@ bool wb_start_server(struct wb_server* server) {
server->new_output.notify = new_output_notify;
wl_signal_add(&server->backend->events.new_output, &server->new_output);
/* Create a scene graph. This is a wlroots abstraction that handles all
* rendering and damage tracking. All the compositor author needs to do
* is add things that should be rendered to the scene graph at the proper
* positions and then call wlr_scene_output_commit() to render a frame if
* necessary.
*/
server->scene = wlr_scene_create();
wlr_scene_attach_output_layout(server->scene, server->output_layout);
const char *socket = wl_display_add_socket_auto(server->wl_display);
if (!socket) {
wlr_backend_destroy(server->backend);
@ -78,6 +87,13 @@ bool wb_start_server(struct wb_server* server) {
wl_list_init(&server->views);
init_xdg_decoration(server);
init_layer_shell(server);
/* Set up the xdg-shell. The xdg-shell is a Wayland protocol which is used
* for application windows. For more detail on shells, refer to Drew
* DeVault's article:
*
* https://drewdevault.com/2018/07/29/Wayland-shells.html
*/
init_xdg_shell(server);
return true;

View file

@ -1,14 +1,36 @@
#include "waybox/xdg_shell.h"
struct wb_view *get_view_at(
struct wb_server *server, double lx, double ly,
struct wlr_surface **surface, double *sx, double *sy) {
/* This returns the topmost node in the scene at the given layout coords.
* we only care about surface nodes as we are specifically looking for a
* surface in the surface tree of a wb_view. */
struct wlr_scene_node *node = wlr_scene_node_at(
&server->scene->node, lx, ly, sx, sy);
if (node == NULL || node->type != WLR_SCENE_NODE_SURFACE) {
return NULL;
}
*surface = wlr_scene_surface_from_node(node)->surface;
/* Find the node corresponding to the tinywl_view at the root of this
* surface tree, it is the only one for which we set the data field. */
while (node != NULL && node->data == NULL) {
node = node->parent;
}
return node->data;
}
void focus_view(struct wb_view *view, struct wlr_surface *surface) {
/* Note: this function only deals with keyboard focus. */
if (view == NULL || surface == NULL || !wlr_surface_is_xdg_surface(surface)) {
return;
}
struct wlr_xdg_surface *xdg_surface = wlr_xdg_surface_from_wlr_surface(surface);
if (xdg_surface)
wlr_log(WLR_INFO, "%s: %s", _("Keyboard focus is now on surface"),
xdg_surface->toplevel->app_id);
struct wb_server *server = view->server;
struct wlr_seat *seat = server->seat->seat;
struct wlr_surface *prev_surface = seat->keyboard_state.focused_surface;
@ -32,6 +54,7 @@ void focus_view(struct wb_view *view, struct wlr_surface *surface) {
}
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat);
/* Move the view to the front */
wlr_scene_node_raise_to_top(view->scene_node);
wl_list_remove(&view->link);
wl_list_insert(&server->views, &view->link);
/* Activate the new surface */
@ -62,91 +85,81 @@ static struct wlr_box get_usable_area(struct wb_view *view) {
return usable_area;
}
static void xdg_surface_commit(struct wl_listener *listener, void *data) {
/* Called after the surface is committed */
struct wb_view *view = wl_container_of(listener, view, surface_commit);
struct wlr_xdg_surface *xdg_surface = view->xdg_toplevel->base;
if (xdg_surface->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL ||
xdg_surface->toplevel->requested.minimized)
return;
struct wb_config *config = view->server->config;
int left_margin, top_margin;
if (config) {
left_margin = config->margins.left;
top_margin = config->margins.top;
} else {
left_margin = 0;
top_margin = 0;
}
struct wlr_box geo_box = {0};
wlr_xdg_surface_get_geometry(xdg_surface, &geo_box);
if (geo_box.x < 0 && view->current_position.x - left_margin < 1)
view->current_position.x += -geo_box.x;
if (geo_box.y < 0 && view->current_position.y - top_margin < 1) {
view->decoration_height = -geo_box.y;
view->current_position.y += view->decoration_height;
}
}
static void xdg_surface_map(struct wl_listener *listener, void *data) {
static void xdg_toplevel_map(struct wl_listener *listener, void *data) {
/* Called when the surface is mapped, or ready to display on-screen. */
struct wb_view *view = wl_container_of(listener, view, map);
view->mapped = true;
if (view->xdg_toplevel->base->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL)
return;
wl_list_insert(&view->server->views, &view->link);
struct wb_config *config = view->server->config;
struct wlr_box geo_box = {0};
struct wlr_box usable_area = get_usable_area(view);
wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo_box);
view->current_position = geo_box;
if (config) {
struct wlr_box usable_area = get_usable_area(view);
view->current_position.height = MIN(view->current_position.height,
view->current_position.height = MIN(geo_box.height,
usable_area.height - config->margins.top - config->margins.bottom);
view->current_position.width = MIN(view->current_position.width,
view->current_position.width = MIN(geo_box.width,
usable_area.width - config->margins.left - config->margins.right);
view->current_position.x = config->margins.left;
view->current_position.y = config->margins.top;
} else {
view->current_position.height = MIN(geo_box.height, usable_area.height);
view->current_position.width = MIN(geo_box.width, usable_area.width);
view->current_position.x = 0;
view->current_position.y = 0;
}
#if WLR_CHECK_VERSION(0, 16, 0)
wlr_xdg_toplevel_set_size(view->xdg_toplevel, view->current_position.width, view->current_position.height);
#else
wlr_xdg_toplevel_set_size(view->xdg_surface, view->current_position.width, view->current_position.height);
#endif
focus_view(view, view->xdg_toplevel->base->surface);
wlr_scene_node_set_position(view->scene_node,
view->current_position.x, view->current_position.y);
}
static void xdg_surface_unmap(struct wl_listener *listener, void *data) {
static void xdg_toplevel_unmap(struct wl_listener *listener, void *data) {
/* Called when the surface is unmapped, and should no longer be shown. */
struct wb_view *view = wl_container_of(listener, view, unmap);
view->mapped = false;
struct wb_view *current_view = wl_container_of(view->server->views.next, current_view, link);
struct wb_view *next_view = wl_container_of(current_view->link.next, next_view, link);
if (current_view->xdg_toplevel->base->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL)
if (view->xdg_toplevel->base->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL)
return;
struct wb_view *next_view = wl_container_of(view->link.next, next_view, link);
/* If the current view is mapped, focus it. */
if (current_view->mapped) {
if (view->scene_node->state.enabled) {
wlr_log(WLR_INFO, "%s: %s", _("Focusing current view"),
current_view->xdg_toplevel->app_id);
focus_view(current_view, current_view->xdg_toplevel->base->surface);
view->xdg_toplevel->app_id);
focus_view(view, view->xdg_toplevel->base->surface);
}
/* Otherwise, focus the next view, if any. */
else if (next_view->xdg_toplevel->base->surface &&
wlr_surface_is_xdg_surface(next_view->xdg_toplevel->base->surface)) {
else if (next_view && next_view->scene_node && next_view->scene_node->state.enabled) {
wlr_log(WLR_INFO, "%s: %s", _("Focusing next view"),
next_view->xdg_toplevel->app_id);
focus_view(next_view, next_view->xdg_toplevel->base->surface);
}
wl_list_remove(&view->link);
}
static void xdg_surface_destroy(struct wl_listener *listener, void *data) {
static void xdg_toplevel_destroy(struct wl_listener *listener, void *data) {
/* Called when the surface is destroyed and should never be shown again. */
struct wb_view *view = wl_container_of(listener, view, destroy);
if (view->xdg_toplevel->base->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL)
wl_list_remove(&view->link);
wl_list_remove(&view->map.link);
wl_list_remove(&view->unmap.link);
wl_list_remove(&view->destroy.link);
if (view->xdg_toplevel->base->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
wl_list_remove(&view->request_minimize.link);
wl_list_remove(&view->request_maximize.link);
wl_list_remove(&view->request_move.link);
wl_list_remove(&view->request_resize.link);
}
free(view);
}
@ -160,12 +173,12 @@ static void xdg_toplevel_request_maximize(struct wl_listener *listener, void *da
view->previous_position = view->current_position;
if (config) {
view->current_position.x = config->margins.left;
view->current_position.y = config->margins.top + view->decoration_height;
view->current_position.y = config->margins.top;
usable_area.height -= config->margins.top + config->margins.bottom;
usable_area.width -= config->margins.left + config->margins.right;
} else {
view->current_position.x = 0;
view->current_position.y = view->decoration_height;
view->current_position.y = 0;
}
} else {
usable_area = view->previous_position;
@ -179,18 +192,25 @@ static void xdg_toplevel_request_maximize(struct wl_listener *listener, void *da
wlr_xdg_toplevel_set_size(view->xdg_surface, usable_area.width, usable_area.height);
wlr_xdg_toplevel_set_maximized(view->xdg_surface, !is_maximized);
#endif
wlr_scene_node_set_position(view->scene_node,
view->current_position.x, view->current_position.y);
}
static void xdg_toplevel_request_minimize(struct wl_listener *listener, void *data) {
struct wb_view *view = wl_container_of(listener, view, request_minimize);
bool minimize_requested = view->xdg_toplevel->requested.minimized;
if (minimize_requested) {
view->previous_position.height = view->current_position.height;
view->current_position.y = 0 -
view->decoration_height * 2 - view->current_position.height;
view->previous_position = view->current_position;
view->current_position.y = -view->current_position.height;
} else {
view->current_position = view->previous_position;
}
struct wb_view *parent_view = wl_container_of(view->link.prev, parent_view, link);
struct wb_view *previous_view = wl_container_of(parent_view->link.prev, previous_view, link);
focus_view(previous_view, previous_view->xdg_toplevel->base->surface);
wlr_scene_node_set_position(view->scene_node,
view->current_position.x, view->current_position.y);
}
static void begin_interactive(struct wb_view *view,
@ -215,8 +235,10 @@ static void begin_interactive(struct wb_view *view,
struct wlr_box geo_box;
wlr_xdg_surface_get_geometry(view->xdg_toplevel->base, &geo_box);
double border_x = (view->current_position.x + geo_box.x) + ((edges & WLR_EDGE_RIGHT) ? geo_box.width : 0);
double border_y = (view->current_position.y + geo_box.y) + ((edges & WLR_EDGE_BOTTOM) ? geo_box.height : 0);
double border_x = (view->current_position.x + geo_box.x) +
((edges & WLR_EDGE_RIGHT) ? geo_box.width : 0);
double border_y = (view->current_position.y + geo_box.y) +
((edges & WLR_EDGE_BOTTOM) ? geo_box.height : 0);
server->grab_x = server->cursor->cursor->x - border_x;
server->grab_y = server->cursor->cursor->y - border_y;
@ -276,9 +298,23 @@ static void handle_new_xdg_surface(struct wl_listener *listener, void *data) {
struct wb_server *server =
wl_container_of(listener, server, new_xdg_surface);
struct wlr_xdg_surface *xdg_surface = data;
if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_NONE) {
return;
/* We must add xdg popups to the scene graph so they get rendered. The
* wlroots scene graph provides a helper for this, but to use it we must
* provide the proper parent scene node of the xdg popup. To enable this,
* we always set the user data field of xdg_surfaces to the corresponding
* scene node. */
if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) {
struct wlr_xdg_surface *parent = wlr_xdg_surface_from_wlr_surface(
xdg_surface->popup->parent);
struct wlr_scene_node *parent_node = parent->data;
xdg_surface->data = wlr_scene_xdg_surface_create(
parent_node, xdg_surface);
/* The scene graph doesn't currently unconstrain popups, so keep going */
/* return; */
}
if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_NONE)
return;
/* Allocate a wb_view for this surface */
struct wb_view *view =
@ -290,19 +326,21 @@ static void handle_new_xdg_surface(struct wl_listener *listener, void *data) {
#endif
/* Listen to the various events it can emit */
view->surface_commit.notify = xdg_surface_commit;
wl_signal_add(&xdg_surface->surface->events.commit, &view->surface_commit);
view->map.notify = xdg_surface_map;
view->map.notify = xdg_toplevel_map;
wl_signal_add(&xdg_surface->events.map, &view->map);
view->unmap.notify = xdg_surface_unmap;
view->unmap.notify = xdg_toplevel_unmap;
wl_signal_add(&xdg_surface->events.unmap, &view->unmap);
view->destroy.notify = xdg_surface_destroy;
view->destroy.notify = xdg_toplevel_destroy;
wl_signal_add(&xdg_surface->events.destroy, &view->destroy);
view->new_popup.notify = handle_new_popup;
wl_signal_add(&xdg_surface->events.new_popup, &view->new_popup);
if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
view->scene_node = wlr_scene_xdg_surface_create(
&view->server->scene->node, view->xdg_toplevel->base);
view->scene_node->data = view;
xdg_surface->data = view->scene_node;
struct wlr_xdg_toplevel *toplevel = view->xdg_toplevel;
view->request_maximize.notify = xdg_toplevel_request_maximize;
wl_signal_add(&toplevel->events.request_maximize, &view->request_maximize);
@ -312,57 +350,9 @@ static void handle_new_xdg_surface(struct wl_listener *listener, void *data) {
wl_signal_add(&toplevel->events.request_move, &view->request_move);
view->request_resize.notify = xdg_toplevel_request_resize;
wl_signal_add(&toplevel->events.request_resize, &view->request_resize);
/* Add it to the list of views. */
wl_list_insert(&server->views, &view->link);
}
}
bool view_at(struct wb_view *view,
double lx, double ly, struct wlr_surface **surface,
double *sx, double *sy) {
/*
* XDG toplevels may have nested surfaces, such as popup windows for context
* menus or tooltips. This function tests if any of those are underneath the
* coordinates lx and ly (in output Layout Coordinates). If so, it sets the
* surface pointer to that wlr_surface and the sx and sy coordinates to the
* coordinates relative to that surface's top-left corner.
*/
if (view->xdg_toplevel->base->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL)
return false;
double view_sx = lx - view->current_position.x;
double view_sy = ly - view->current_position.y;
double _sx, _sy;
struct wlr_surface *_surface = NULL;
_surface = wlr_xdg_surface_surface_at(
view->xdg_toplevel->base, view_sx, view_sy, &_sx, &_sy);
if (_surface != NULL) {
*sx = _sx;
*sy = _sy;
*surface = _surface;
return true;
}
return false;
}
struct wb_view *desktop_view_at(
struct wb_server *server, double lx, double ly,
struct wlr_surface **surface, double *sx, double *sy) {
/* This iterates over all of our surfaces and attempts to find one under the
* cursor. This relies on server->views being ordered from top-to-bottom. */
struct wb_view *view;
wl_list_for_each(view, &server->views, link) {
if (view_at(view, lx, ly, surface, sx, sy)) {
return view;
}
}
return NULL;
}
void init_xdg_shell(struct wb_server *server) {
server->xdg_shell = wlr_xdg_shell_create(server->wl_display);
server->new_xdg_surface.notify = handle_new_xdg_surface;