wlroots-hyprland/rootston/xdg_shell_v6.c

494 lines
16 KiB
C
Raw Normal View History

2017-09-23 23:48:13 +02:00
#include <assert.h>
#include <stdbool.h>
2018-02-12 21:29:23 +01:00
#include <stdlib.h>
2017-09-23 17:13:18 +02:00
#include <wayland-server.h>
2017-09-23 23:48:13 +02:00
#include <wlr/types/wlr_box.h>
2017-09-23 17:13:18 +02:00
#include <wlr/types/wlr_surface.h>
2017-09-23 23:48:13 +02:00
#include <wlr/types/wlr_xdg_shell_v6.h>
2017-09-23 17:13:18 +02:00
#include <wlr/util/log.h>
#include "rootston/desktop.h"
2017-09-24 00:18:19 +02:00
#include "rootston/input.h"
2018-02-12 21:29:23 +01:00
#include "rootston/server.h"
2017-09-23 17:13:18 +02:00
static void popup_destroy(struct roots_view_child *child) {
assert(child->destroy == popup_destroy);
struct roots_xdg_popup_v6 *popup = (struct roots_xdg_popup_v6 *)child;
if (popup == NULL) {
return;
}
wl_list_remove(&popup->destroy.link);
wl_list_remove(&popup->new_popup.link);
view_child_finish(&popup->view_child);
free(popup);
}
static void popup_handle_destroy(struct wl_listener *listener, void *data) {
struct roots_xdg_popup_v6 *popup =
wl_container_of(listener, popup, destroy);
popup_destroy((struct roots_view_child *)popup);
}
static struct roots_xdg_popup_v6 *popup_create(struct roots_view *view,
struct wlr_xdg_popup_v6 *wlr_popup);
static void popup_handle_new_popup(struct wl_listener *listener, void *data) {
struct roots_xdg_popup_v6 *popup =
wl_container_of(listener, popup, new_popup);
struct wlr_xdg_popup_v6 *wlr_popup = data;
popup_create(popup->view_child.view, wlr_popup);
}
2018-03-27 20:49:31 +02:00
static void popup_get_coords(struct wlr_xdg_popup_v6 *popup,
double *sx, double *sy) {
struct wlr_xdg_surface_v6 *parent = popup->parent;
double popup_sx = popup->geometry.x;
double popup_sy = popup->geometry.y;
2018-03-27 05:48:32 +02:00
while (parent->role != WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) {
2018-03-27 20:49:31 +02:00
popup_sx += parent->popup->geometry.x;
popup_sy += parent->popup->geometry.y;
2018-03-27 05:48:32 +02:00
parent = parent->popup->parent;
}
2018-03-27 20:49:31 +02:00
*sx = popup_sx + parent->geometry.x;
*sy = popup_sy + parent->geometry.y;
}
static void popup_is_constrained(struct roots_xdg_popup_v6 *popup,
bool *x_constrained, bool *y_constrained) {
struct roots_view *view = popup->view_child.view;
struct wlr_output_layout *layout = view->desktop->layout;
struct wlr_xdg_popup_v6 *wlr_popup = popup->wlr_popup;
2018-03-27 05:48:32 +02:00
int popup_width = wlr_popup->geometry.width;
int popup_height = wlr_popup->geometry.height;
2018-03-27 20:49:31 +02:00
int anchor_lx, anchor_ly;
wlr_xdg_popup_v6_get_anchor_point(wlr_popup, &anchor_lx, &anchor_ly);
2018-03-27 05:48:32 +02:00
2018-03-27 20:49:31 +02:00
double popup_lx, popup_ly;
popup_get_coords(wlr_popup, &popup_lx, &popup_ly);
popup_lx += view->x;
popup_ly += view->y;
anchor_lx += popup_lx;
anchor_ly += popup_ly;
double dest_x = 0, dest_y = 0;
wlr_output_layout_closest_point(layout, NULL, anchor_lx, anchor_ly,
&dest_x, &dest_y);
2018-03-27 05:48:32 +02:00
struct wlr_output *output =
2018-03-27 20:49:31 +02:00
wlr_output_layout_output_at(layout, dest_x, dest_y);
// XXX: handle empty output layout
assert(output);
2018-03-27 05:48:32 +02:00
struct wlr_box *output_box = wlr_output_layout_get_box(layout, output);
2018-03-27 20:49:31 +02:00
*x_constrained = popup_lx <= output_box->x ||
popup_lx + popup_width >= output_box->x + output_box->width;
*y_constrained = popup_ly <= output_box->y ||
popup_ly + popup_height >= output_box->y + output_box->height;
}
static void popup_unconstrain_flip(struct roots_xdg_popup_v6 *popup) {
bool x_constrained, y_constrained;
popup_is_constrained(popup, &x_constrained, &y_constrained);
2018-03-27 05:48:32 +02:00
2018-03-27 20:49:31 +02:00
if (x_constrained) {
wlr_positioner_v6_invert_x(&popup->wlr_popup->positioner);
2018-03-27 05:48:32 +02:00
}
2018-03-27 20:49:31 +02:00
if (y_constrained) {
wlr_positioner_v6_invert_y(&popup->wlr_popup->positioner);
}
popup->wlr_popup->geometry =
wlr_xdg_positioner_v6_get_geometry(&popup->wlr_popup->positioner);
}
2018-03-27 21:13:35 +02:00
static void popup_unconstrain_slide(struct roots_xdg_popup_v6 *popup) {
bool x_constrained, y_constrained;
popup_is_constrained(popup, &x_constrained, &y_constrained);
if (x_constrained) {
// TODO slide_x
}
if (y_constrained) {
// TODO slide_y
}
}
static void popup_unconstrain_resize(struct roots_xdg_popup_v6 *popup) {
bool x_constrained, y_constrained;
popup_is_constrained(popup, &x_constrained, &y_constrained);
if (x_constrained) {
// TODO resize_x
}
if (y_constrained) {
// TODO resize_y
}
}
2018-03-27 20:49:31 +02:00
static void popup_unconstrain(struct roots_xdg_popup_v6 *popup) {
popup_unconstrain_flip(popup);
2018-03-27 21:13:35 +02:00
popup_unconstrain_slide(popup);
popup_unconstrain_resize(popup);
2018-03-27 20:49:31 +02:00
}
2018-03-27 05:48:32 +02:00
2018-03-27 20:49:31 +02:00
static struct roots_xdg_popup_v6 *popup_create(struct roots_view *view,
struct wlr_xdg_popup_v6 *wlr_popup) {
struct roots_xdg_popup_v6 *popup =
calloc(1, sizeof(struct roots_xdg_popup_v6));
if (popup == NULL) {
return NULL;
}
popup->wlr_popup = wlr_popup;
popup->view_child.destroy = popup_destroy;
view_child_init(&popup->view_child, view, wlr_popup->base->surface);
popup->destroy.notify = popup_handle_destroy;
wl_signal_add(&wlr_popup->base->events.destroy, &popup->destroy);
popup->new_popup.notify = popup_handle_new_popup;
wl_signal_add(&wlr_popup->base->events.new_popup, &popup->new_popup);
2018-03-27 20:49:31 +02:00
popup_unconstrain(popup);
return popup;
}
2017-10-26 04:37:02 +02:00
static void get_size(const struct roots_view *view, struct wlr_box *box) {
2017-09-23 23:48:13 +02:00
assert(view->type == ROOTS_XDG_SHELL_V6_VIEW);
2017-11-05 10:22:42 +01:00
struct wlr_xdg_surface_v6 *surface = view->xdg_surface_v6;
if (surface->geometry.width > 0 && surface->geometry.height > 0) {
box->width = surface->geometry.width;
box->height = surface->geometry.height;
} else if (view->wlr_surface != NULL) {
box->width = view->wlr_surface->current->width;
box->height = view->wlr_surface->current->height;
} else {
box->width = box->height = 0;
}
2017-09-30 14:02:09 +02:00
}
2017-09-23 23:48:13 +02:00
static void activate(struct roots_view *view, bool active) {
assert(view->type == ROOTS_XDG_SHELL_V6_VIEW);
2017-11-05 10:22:42 +01:00
struct wlr_xdg_surface_v6 *surface = view->xdg_surface_v6;
if (surface->role == WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) {
wlr_xdg_toplevel_v6_set_activated(surface, active);
2017-09-23 23:48:13 +02:00
}
}
2017-11-05 10:22:42 +01:00
static void apply_size_constraints(struct wlr_xdg_surface_v6 *surface,
uint32_t width, uint32_t height, uint32_t *dest_width,
uint32_t *dest_height) {
*dest_width = width;
*dest_height = height;
struct wlr_xdg_toplevel_v6_state *state = &surface->toplevel->current;
if (width < state->min_width) {
*dest_width = state->min_width;
} else if (state->max_width > 0 &&
width > state->max_width) {
*dest_width = state->max_width;
}
if (height < state->min_height) {
*dest_height = state->min_height;
} else if (state->max_height > 0 &&
height > state->max_height) {
*dest_height = state->max_height;
}
}
2017-09-30 11:57:39 +02:00
static void resize(struct roots_view *view, uint32_t width, uint32_t height) {
assert(view->type == ROOTS_XDG_SHELL_V6_VIEW);
2017-11-05 10:22:42 +01:00
struct wlr_xdg_surface_v6 *surface = view->xdg_surface_v6;
if (surface->role != WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) {
return;
}
2017-11-05 10:22:42 +01:00
uint32_t constrained_width, constrained_height;
apply_size_constraints(surface, width, height, &constrained_width,
&constrained_height);
2017-11-05 10:22:42 +01:00
wlr_xdg_toplevel_v6_set_size(surface, constrained_width,
constrained_height);
}
static void move_resize(struct roots_view *view, double x, double y,
uint32_t width, uint32_t height) {
assert(view->type == ROOTS_XDG_SHELL_V6_VIEW);
2017-11-05 10:22:42 +01:00
struct roots_xdg_surface_v6 *roots_surface = view->roots_xdg_surface_v6;
struct wlr_xdg_surface_v6 *surface = view->xdg_surface_v6;
if (surface->role != WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) {
return;
2017-09-30 11:57:39 +02:00
}
bool update_x = x != view->x;
bool update_y = y != view->y;
2017-11-05 10:22:42 +01:00
uint32_t constrained_width, constrained_height;
apply_size_constraints(surface, width, height, &constrained_width,
&constrained_height);
if (update_x) {
x = x + width - constrained_width;
}
if (update_y) {
y = y + height - constrained_height;
}
view->pending_move_resize.update_x = update_x;
view->pending_move_resize.update_y = update_y;
view->pending_move_resize.x = x;
view->pending_move_resize.y = y;
view->pending_move_resize.width = constrained_width;
view->pending_move_resize.height = constrained_height;
uint32_t serial = wlr_xdg_toplevel_v6_set_size(surface, constrained_width,
constrained_height);
if (serial > 0) {
roots_surface->pending_move_resize_configure_serial = serial;
2018-02-07 20:26:30 +01:00
} else if (roots_surface->pending_move_resize_configure_serial == 0) {
2018-01-18 12:25:39 +01:00
view_update_position(view, x, y);
}
2017-09-30 11:57:39 +02:00
}
static void maximize(struct roots_view *view, bool maximized) {
assert(view->type == ROOTS_XDG_SHELL_V6_VIEW);
struct wlr_xdg_surface_v6 *surface = view->xdg_surface_v6;
if (surface->role != WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) {
return;
}
wlr_xdg_toplevel_v6_set_maximized(surface, maximized);
}
2017-11-20 17:27:36 +01:00
static void set_fullscreen(struct roots_view *view, bool fullscreen) {
assert(view->type == ROOTS_XDG_SHELL_V6_VIEW);
struct wlr_xdg_surface_v6 *surface = view->xdg_surface_v6;
if (surface->role != WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) {
return;
}
wlr_xdg_toplevel_v6_set_fullscreen(surface, fullscreen);
}
static void close(struct roots_view *view) {
assert(view->type == ROOTS_XDG_SHELL_V6_VIEW);
2017-11-05 10:22:42 +01:00
struct wlr_xdg_surface_v6 *surface = view->xdg_surface_v6;
if (surface->role == WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) {
wlr_xdg_toplevel_v6_send_close(surface);
}
}
static void destroy(struct roots_view *view) {
assert(view->type == ROOTS_XDG_SHELL_V6_VIEW);
struct roots_xdg_surface_v6 *roots_xdg_surface = view->roots_xdg_surface_v6;
wl_list_remove(&roots_xdg_surface->surface_commit.link);
wl_list_remove(&roots_xdg_surface->destroy.link);
wl_list_remove(&roots_xdg_surface->new_popup.link);
wl_list_remove(&roots_xdg_surface->map.link);
wl_list_remove(&roots_xdg_surface->unmap.link);
wl_list_remove(&roots_xdg_surface->request_move.link);
wl_list_remove(&roots_xdg_surface->request_resize.link);
wl_list_remove(&roots_xdg_surface->request_maximize.link);
wl_list_remove(&roots_xdg_surface->request_fullscreen.link);
free(roots_xdg_surface);
}
2017-09-24 00:18:19 +02:00
static void handle_request_move(struct wl_listener *listener, void *data) {
struct roots_xdg_surface_v6 *roots_xdg_surface =
wl_container_of(listener, roots_xdg_surface, request_move);
struct roots_view *view = roots_xdg_surface->view;
struct roots_input *input = view->desktop->server->input;
struct wlr_xdg_toplevel_v6_move_event *e = data;
2017-11-07 21:56:11 +01:00
struct roots_seat *seat = input_seat_from_wlr_seat(input, e->seat->seat);
// TODO verify event serial
if (!seat || seat->cursor->mode != ROOTS_CURSOR_PASSTHROUGH) {
2017-09-24 00:18:19 +02:00
return;
}
2017-11-07 21:56:11 +01:00
roots_seat_begin_move(seat, view);
2017-09-24 00:18:19 +02:00
}
2017-09-30 10:39:06 +02:00
static void handle_request_resize(struct wl_listener *listener, void *data) {
struct roots_xdg_surface_v6 *roots_xdg_surface =
wl_container_of(listener, roots_xdg_surface, request_resize);
struct roots_view *view = roots_xdg_surface->view;
struct roots_input *input = view->desktop->server->input;
struct wlr_xdg_toplevel_v6_resize_event *e = data;
2017-11-07 21:56:11 +01:00
// TODO verify event serial
struct roots_seat *seat = input_seat_from_wlr_seat(input, e->seat->seat);
2017-11-08 14:35:27 +01:00
assert(seat);
2017-11-07 21:56:11 +01:00
if (!seat || seat->cursor->mode != ROOTS_CURSOR_PASSTHROUGH) {
2017-09-30 10:39:06 +02:00
return;
}
2017-11-07 21:56:11 +01:00
roots_seat_begin_resize(seat, view, e->edges);
2017-09-30 10:39:06 +02:00
}
2017-11-09 21:41:11 +01:00
static void handle_request_maximize(struct wl_listener *listener, void *data) {
struct roots_xdg_surface_v6 *roots_xdg_surface =
2017-11-09 21:41:11 +01:00
wl_container_of(listener, roots_xdg_surface, request_maximize);
struct roots_view *view = roots_xdg_surface->view;
struct wlr_xdg_surface_v6 *surface = view->xdg_surface_v6;
if (surface->role != WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) {
return;
}
view_maximize(view, surface->toplevel->next.maximized);
2017-11-09 21:41:11 +01:00
}
2017-11-20 17:27:36 +01:00
static void handle_request_fullscreen(struct wl_listener *listener,
void *data) {
struct roots_xdg_surface_v6 *roots_xdg_surface =
wl_container_of(listener, roots_xdg_surface, request_fullscreen);
struct roots_view *view = roots_xdg_surface->view;
struct wlr_xdg_surface_v6 *surface = view->xdg_surface_v6;
struct wlr_xdg_toplevel_v6_set_fullscreen_event *e = data;
if (surface->role != WLR_XDG_SURFACE_V6_ROLE_TOPLEVEL) {
return;
}
view_set_fullscreen(view, e->fullscreen, e->output);
}
static void handle_surface_commit(struct wl_listener *listener, void *data) {
2017-11-05 10:22:42 +01:00
struct roots_xdg_surface_v6 *roots_surface =
wl_container_of(listener, roots_surface, surface_commit);
2017-11-05 10:22:42 +01:00
struct roots_view *view = roots_surface->view;
struct wlr_xdg_surface_v6 *surface = view->xdg_surface_v6;
2018-03-10 11:18:50 +01:00
if (!surface->mapped) {
return;
}
view_apply_damage(view);
2018-01-28 10:11:31 +01:00
struct wlr_box size;
get_size(view, &size);
view_update_size(view, size.width, size.height);
uint32_t pending_serial =
roots_surface->pending_move_resize_configure_serial;
if (pending_serial > 0 && pending_serial >= surface->configure_serial) {
double x = view->x;
double y = view->y;
if (view->pending_move_resize.update_x) {
x = view->pending_move_resize.x + view->pending_move_resize.width -
size.width;
}
if (view->pending_move_resize.update_y) {
y = view->pending_move_resize.y + view->pending_move_resize.height -
size.height;
}
view_update_position(view, x, y);
if (pending_serial == surface->configure_serial) {
roots_surface->pending_move_resize_configure_serial = 0;
}
2017-11-05 10:22:42 +01:00
}
2017-10-08 12:17:25 +02:00
}
static void handle_new_popup(struct wl_listener *listener, void *data) {
struct roots_xdg_surface_v6 *roots_xdg_surface =
wl_container_of(listener, roots_xdg_surface, new_popup);
struct wlr_xdg_popup_v6 *wlr_popup = data;
popup_create(roots_xdg_surface->view, wlr_popup);
}
2018-03-10 11:18:50 +01:00
static void handle_map(struct wl_listener *listener, void *data) {
struct roots_xdg_surface_v6 *roots_xdg_surface =
wl_container_of(listener, roots_xdg_surface, map);
struct roots_view *view = roots_xdg_surface->view;
struct wlr_box box;
get_size(view, &box);
view->width = box.width;
view->height = box.height;
view_map(view, view->xdg_surface_v6->surface);
view_setup(view);
}
static void handle_unmap(struct wl_listener *listener, void *data) {
struct roots_xdg_surface_v6 *roots_xdg_surface =
wl_container_of(listener, roots_xdg_surface, unmap);
view_unmap(roots_xdg_surface->view);
2018-03-10 11:18:50 +01:00
}
2017-09-23 17:13:18 +02:00
static void handle_destroy(struct wl_listener *listener, void *data) {
struct roots_xdg_surface_v6 *roots_xdg_surface =
wl_container_of(listener, roots_xdg_surface, destroy);
2018-03-09 10:29:22 +01:00
view_destroy(roots_xdg_surface->view);
2017-09-23 17:13:18 +02:00
}
void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) {
struct wlr_xdg_surface_v6 *surface = data;
assert(surface->role != WLR_XDG_SURFACE_V6_ROLE_NONE);
if (surface->role == WLR_XDG_SURFACE_V6_ROLE_POPUP) {
wlr_log(L_DEBUG, "new xdg popup");
return;
}
2017-09-23 17:13:18 +02:00
struct roots_desktop *desktop =
wl_container_of(listener, desktop, xdg_shell_v6_surface);
wlr_log(L_DEBUG, "new xdg toplevel: title=%s, app_id=%s",
2017-09-23 17:13:18 +02:00
surface->title, surface->app_id);
wlr_xdg_surface_v6_ping(surface);
struct roots_xdg_surface_v6 *roots_surface =
calloc(1, sizeof(struct roots_xdg_surface_v6));
2017-10-08 12:17:25 +02:00
if (!roots_surface) {
return;
}
roots_surface->surface_commit.notify = handle_surface_commit;
wl_signal_add(&surface->surface->events.commit,
&roots_surface->surface_commit);
2017-09-23 17:13:18 +02:00
roots_surface->destroy.notify = handle_destroy;
wl_signal_add(&surface->events.destroy, &roots_surface->destroy);
2018-03-10 11:18:50 +01:00
roots_surface->map.notify = handle_map;
wl_signal_add(&surface->events.map, &roots_surface->map);
roots_surface->unmap.notify = handle_unmap;
wl_signal_add(&surface->events.unmap, &roots_surface->unmap);
2017-09-24 00:18:19 +02:00
roots_surface->request_move.notify = handle_request_move;
wl_signal_add(&surface->events.request_move, &roots_surface->request_move);
2017-09-30 10:39:06 +02:00
roots_surface->request_resize.notify = handle_request_resize;
wl_signal_add(&surface->events.request_resize,
&roots_surface->request_resize);
2017-11-09 21:41:11 +01:00
roots_surface->request_maximize.notify = handle_request_maximize;
wl_signal_add(&surface->events.request_maximize,
&roots_surface->request_maximize);
2017-11-20 17:27:36 +01:00
roots_surface->request_fullscreen.notify = handle_request_fullscreen;
wl_signal_add(&surface->events.request_fullscreen,
&roots_surface->request_fullscreen);
roots_surface->new_popup.notify = handle_new_popup;
wl_signal_add(&surface->events.new_popup, &roots_surface->new_popup);
2017-09-23 17:13:18 +02:00
2018-03-09 10:29:22 +01:00
struct roots_view *view = view_create(desktop);
if (!view) {
free(roots_surface);
return;
}
2017-09-23 17:13:18 +02:00
view->type = ROOTS_XDG_SHELL_V6_VIEW;
2018-01-28 10:11:31 +01:00
2017-09-23 17:13:18 +02:00
view->xdg_surface_v6 = surface;
view->roots_xdg_surface_v6 = roots_surface;
2017-09-23 23:48:13 +02:00
view->activate = activate;
2017-09-30 11:57:39 +02:00
view->resize = resize;
view->move_resize = move_resize;
view->maximize = maximize;
2017-11-20 17:27:36 +01:00
view->set_fullscreen = set_fullscreen;
view->close = close;
view->destroy = destroy;
2017-09-23 17:13:18 +02:00
roots_surface->view = view;
if (surface->toplevel->next.maximized) {
view_maximize(view, true);
}
if (surface->toplevel->next.fullscreen) {
view_set_fullscreen(view, true, NULL);
}
2017-09-23 17:13:18 +02:00
}