wlroots/rootston/desktop.c

1111 lines
32 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200112L
2017-09-28 23:48:55 +00:00
#include <assert.h>
2017-10-04 19:05:00 +00:00
#include <math.h>
2018-02-12 20:29:23 +00:00
#include <stdlib.h>
#include <time.h>
#include <wlr/config.h>
2017-09-23 21:48:13 +00:00
#include <wlr/types/wlr_box.h>
#include <wlr/types/wlr_compositor.h>
2017-09-23 14:13:05 +00:00
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_data_control_v1.h>
2018-05-23 08:07:40 +00:00
#include <wlr/types/wlr_export_dmabuf_v1.h>
#include <wlr/types/wlr_gamma_control_v1.h>
#include <wlr/types/wlr_gamma_control.h>
#include <wlr/types/wlr_gtk_primary_selection.h>
2018-03-17 19:07:48 +00:00
#include <wlr/types/wlr_idle_inhibit_v1.h>
2018-05-23 08:07:40 +00:00
#include <wlr/types/wlr_idle.h>
2018-04-03 15:29:32 +00:00
#include <wlr/types/wlr_input_inhibitor.h>
2018-09-14 17:32:33 +00:00
#include <wlr/types/wlr_layer_shell_v1.h>
2018-02-12 20:29:23 +00:00
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_pointer_constraints_v1.h>
#include <wlr/types/wlr_primary_selection_v1.h>
#include <wlr/types/wlr_server_decoration.h>
#include <wlr/types/wlr_tablet_v2.h>
#include <wlr/types/wlr_wl_shell.h>
2017-11-12 10:10:56 +00:00
#include <wlr/types/wlr_xcursor_manager.h>
2018-09-14 17:29:08 +00:00
#include <wlr/types/wlr_xdg_output_v1.h>
#include <wlr/types/wlr_xdg_output_v1.h>
#include <wlr/types/wlr_xdg_shell_v6.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/util/log.h>
2018-03-30 20:54:37 +00:00
#include "rootston/layers.h"
2017-11-07 20:56:11 +00:00
#include "rootston/seat.h"
2018-02-12 20:29:23 +00:00
#include "rootston/server.h"
2018-01-21 23:24:53 +00:00
#include "rootston/view.h"
#include "rootston/virtual_keyboard.h"
2018-02-12 20:29:23 +00:00
#include "rootston/xcursor.h"
2018-03-30 20:54:37 +00:00
#include "wlr-layer-shell-unstable-v1-protocol.h"
2018-03-09 09:29:22 +00:00
struct roots_view *view_create(struct roots_desktop *desktop) {
struct roots_view *view = calloc(1, sizeof(struct roots_view));
2018-02-23 10:30:55 +00:00
if (!view) {
return NULL;
}
2018-03-09 09:29:22 +00:00
view->desktop = desktop;
2018-02-23 10:30:55 +00:00
view->alpha = 1.0f;
2018-03-13 11:31:45 +00:00
wl_signal_init(&view->events.unmap);
2018-03-09 09:29:22 +00:00
wl_signal_init(&view->events.destroy);
wl_list_init(&view->children);
return view;
}
void view_get_box(const struct roots_view *view, struct wlr_box *box) {
2018-12-05 09:01:25 +00:00
box->x = view->box.x;
box->y = view->box.y;
box->width = view->box.width;
box->height = view->box.height;
2017-09-30 12:02:09 +00:00
}
2018-01-16 12:50:40 +00:00
void view_get_deco_box(const struct roots_view *view, struct wlr_box *box) {
view_get_box(view, box);
if (!view->decorated) {
return;
}
box->x -= view->border_width;
box->y -= (view->border_width + view->titlebar_height);
box->width += view->border_width * 2;
box->height += (view->border_width * 2 + view->titlebar_height);
}
2018-03-10 10:18:50 +00:00
enum roots_deco_part view_get_deco_part(struct roots_view *view, double sx,
double sy) {
2018-01-21 23:24:53 +00:00
if (!view->decorated) {
return ROOTS_DECO_PART_NONE;
2018-01-21 23:24:53 +00:00
}
int sw = view->wlr_surface->current.width;
int sh = view->wlr_surface->current.height;
2018-01-21 23:24:53 +00:00
int bw = view->border_width;
int titlebar_h = view->titlebar_height;
if (sx > 0 && sx < sw && sy < 0 && sy > -view->titlebar_height) {
return ROOTS_DECO_PART_TITLEBAR;
2018-01-21 23:24:53 +00:00
}
enum roots_deco_part parts = 0;
2018-01-21 23:24:53 +00:00
if (sy >= -(titlebar_h + bw) &&
sy <= sh + bw) {
if (sx < 0 && sx > -bw) {
parts |= ROOTS_DECO_PART_LEFT_BORDER;
2018-01-21 23:24:53 +00:00
} else if (sx > sw && sx < sw + bw) {
parts |= ROOTS_DECO_PART_RIGHT_BORDER;
2018-01-21 23:24:53 +00:00
}
}
if (sx >= -bw && sx <= sw + bw) {
if (sy > sh && sy <= sh + bw) {
parts |= ROOTS_DECO_PART_BOTTOM_BORDER;
2018-01-21 23:24:53 +00:00
} else if (sy >= -(titlebar_h + bw) && sy < 0) {
parts |= ROOTS_DECO_PART_TOP_BORDER;
2018-01-21 23:24:53 +00:00
}
}
// TODO corners
return parts;
}
2017-10-26 02:37:02 +00:00
static void view_update_output(const struct roots_view *view,
const struct wlr_box *before) {
struct roots_desktop *desktop = view->desktop;
2018-03-10 10:18:50 +00:00
if (view->wlr_surface == NULL) {
return;
}
struct wlr_box box;
view_get_box(view, &box);
2018-03-10 10:18:50 +00:00
struct roots_output *output;
2017-10-26 02:37:02 +00:00
wl_list_for_each(output, &desktop->outputs, link) {
2017-11-21 19:58:15 +00:00
bool intersected = before != NULL && wlr_output_layout_intersects(
2017-12-31 11:49:06 +00:00
desktop->layout, output->wlr_output, before);
bool intersects = wlr_output_layout_intersects(desktop->layout,
output->wlr_output, &box);
2017-10-26 02:37:02 +00:00
if (intersected && !intersects) {
wlr_surface_send_leave(view->wlr_surface, output->wlr_output);
if (view->toplevel_handle) {
wlr_foreign_toplevel_handle_v1_output_leave(
view->toplevel_handle, output->wlr_output);
}
}
2017-10-26 02:37:02 +00:00
if (!intersected && intersects) {
wlr_surface_send_enter(view->wlr_surface, output->wlr_output);
if (view->toplevel_handle) {
wlr_foreign_toplevel_handle_v1_output_enter(
view->toplevel_handle, output->wlr_output);
}
}
}
}
void view_move(struct roots_view *view, double x, double y) {
2018-12-05 09:01:25 +00:00
if (view->box.x == x && view->box.y == y) {
return;
}
2017-10-26 02:37:02 +00:00
struct wlr_box before;
view_get_box(view, &before);
if (view->move) {
view->move(view, x, y);
} else {
2018-01-18 11:25:39 +00:00
view_update_position(view, x, y);
2017-10-19 16:33:02 +00:00
}
view_update_output(view, &before);
2017-10-19 16:33:02 +00:00
}
2017-09-23 21:48:13 +00:00
void view_activate(struct roots_view *view, bool activate) {
if (view->activate) {
view->activate(view, activate);
}
if (view->toplevel_handle) {
wlr_foreign_toplevel_handle_v1_set_activated(view->toplevel_handle,
activate);
}
2017-10-19 16:33:02 +00:00
}
2017-09-30 09:57:39 +00:00
void view_resize(struct roots_view *view, uint32_t width, uint32_t height) {
2017-10-26 02:37:02 +00:00
struct wlr_box before;
view_get_box(view, &before);
2017-09-30 09:57:39 +00:00
if (view->resize) {
view->resize(view, width, height);
}
2017-10-26 02:37:02 +00:00
view_update_output(view, &before);
2017-09-30 09:57:39 +00:00
}
void view_move_resize(struct roots_view *view, double x, double y,
uint32_t width, uint32_t height) {
2018-12-05 09:01:25 +00:00
bool update_x = x != view->box.x;
bool update_y = y != view->box.y;
if (!update_x && !update_y) {
view_resize(view, width, height);
return;
}
if (view->move_resize) {
view->move_resize(view, x, y, width, height);
return;
}
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 = width;
view->pending_move_resize.height = height;
view_resize(view, width, height);
}
2017-11-20 16:27:36 +00:00
static struct wlr_output *view_get_output(struct roots_view *view) {
struct wlr_box view_box;
view_get_box(view, &view_box);
double output_x, output_y;
wlr_output_layout_closest_point(view->desktop->layout, NULL,
2018-12-05 09:01:25 +00:00
view->box.x + (double)view_box.width/2,
view->box.y + (double)view_box.height/2,
2017-11-20 16:27:36 +00:00
&output_x, &output_y);
return wlr_output_layout_output_at(view->desktop->layout, output_x,
output_y);
}
void view_arrange_maximized(struct roots_view *view) {
struct wlr_box view_box;
view_get_box(view, &view_box);
struct wlr_output *output = view_get_output(view);
struct roots_output *roots_output = output->data;
struct wlr_box *output_box =
wlr_output_layout_get_box(view->desktop->layout, output);
struct wlr_box usable_area;
memcpy(&usable_area, &roots_output->usable_area,
sizeof(struct wlr_box));
usable_area.x += output_box->x;
usable_area.y += output_box->y;
view_move_resize(view, usable_area.x, usable_area.y,
usable_area.width, usable_area.height);
view_rotate(view, 0);
}
void view_maximize(struct roots_view *view, bool maximized) {
if (view->maximized == maximized) {
return;
}
if (view->maximize) {
view->maximize(view, maximized);
}
if (view->toplevel_handle) {
wlr_foreign_toplevel_handle_v1_set_maximized(view->toplevel_handle,
maximized);
}
if (!view->maximized && maximized) {
2018-03-28 01:25:40 +00:00
view->maximized = true;
2018-12-05 09:01:25 +00:00
view->saved.x = view->box.x;
view->saved.y = view->box.y;
2018-03-28 01:25:40 +00:00
view->saved.rotation = view->rotation;
2018-12-05 09:01:25 +00:00
view->saved.width = view->box.width;
view->saved.height = view->box.height;
2018-03-28 01:25:40 +00:00
view_arrange_maximized(view);
}
if (view->maximized && !maximized) {
view->maximized = false;
view_move_resize(view, view->saved.x, view->saved.y, view->saved.width,
view->saved.height);
view_rotate(view, view->saved.rotation);
}
}
2017-11-20 16:27:36 +00:00
void view_set_fullscreen(struct roots_view *view, bool fullscreen,
struct wlr_output *output) {
bool was_fullscreen = view->fullscreen_output != NULL;
if (was_fullscreen == fullscreen) {
// TODO: support changing the output?
return;
}
// TODO: check if client is focused?
if (view->set_fullscreen) {
view->set_fullscreen(view, fullscreen);
}
if (!was_fullscreen && fullscreen) {
if (output == NULL) {
output = view_get_output(view);
}
struct roots_output *roots_output =
desktop_output_from_wlr_output(view->desktop, output);
if (roots_output == NULL) {
return;
}
2017-11-20 16:27:36 +00:00
struct wlr_box view_box;
view_get_box(view, &view_box);
2018-12-05 09:01:25 +00:00
view->saved.x = view->box.x;
view->saved.y = view->box.y;
2017-11-20 16:27:36 +00:00
view->saved.rotation = view->rotation;
view->saved.width = view_box.width;
view->saved.height = view_box.height;
struct wlr_box *output_box =
wlr_output_layout_get_box(view->desktop->layout, output);
view_move_resize(view, output_box->x, output_box->y, output_box->width,
output_box->height);
view_rotate(view, 0);
2017-11-20 16:27:36 +00:00
roots_output->fullscreen_view = view;
view->fullscreen_output = roots_output;
output_damage_whole(roots_output);
2017-11-20 16:27:36 +00:00
}
if (was_fullscreen && !fullscreen) {
view_move_resize(view, view->saved.x, view->saved.y, view->saved.width,
view->saved.height);
view_rotate(view, view->saved.rotation);
2017-11-20 16:27:36 +00:00
output_damage_whole(view->fullscreen_output);
view->fullscreen_output->fullscreen_view = NULL;
2017-11-20 16:27:36 +00:00
view->fullscreen_output = NULL;
}
}
void view_rotate(struct roots_view *view, float rotation) {
if (view->rotation == rotation) {
return;
}
view_damage_whole(view);
view->rotation = rotation;
view_damage_whole(view);
}
void view_cycle_alpha(struct roots_view *view) {
view->alpha -= 0.05;
/* Don't go completely transparent */
if (view->alpha < 0.1) {
view->alpha = 1.0;
}
view_damage_whole(view);
}
void view_close(struct roots_view *view) {
if (view->close) {
view->close(view);
}
}
2017-10-08 10:17:25 +00:00
bool view_center(struct roots_view *view) {
2017-10-26 02:37:02 +00:00
struct wlr_box box;
view_get_box(view, &box);
2017-10-08 10:17:25 +00:00
struct roots_desktop *desktop = view->desktop;
2017-11-12 14:55:28 +00:00
struct roots_input *input = desktop->server->input;
struct roots_seat *seat = input_last_active_seat(input);
2017-11-12 14:55:28 +00:00
if (!seat) {
return false;
}
2017-10-16 18:35:16 +00:00
struct wlr_output *output =
2017-11-12 14:55:28 +00:00
wlr_output_layout_output_at(desktop->layout,
seat->cursor->cursor->x,
seat->cursor->cursor->y);
2017-10-08 10:17:25 +00:00
if (!output) {
2017-10-16 18:35:16 +00:00
// empty layout
2017-10-08 10:17:25 +00:00
return false;
}
2017-10-16 18:35:16 +00:00
const struct wlr_output_layout_output *l_output =
wlr_output_layout_get(desktop->layout, output);
int width, height;
wlr_output_effective_resolution(output, &width, &height);
2017-11-01 12:57:30 +00:00
double view_x = (double)(width - box.width) / 2 + l_output->x;
double view_y = (double)(height - box.height) / 2 + l_output->y;
view_move(view, view_x, view_y);
2017-10-16 18:35:16 +00:00
2017-10-08 10:17:25 +00:00
return true;
}
2018-01-21 13:50:37 +00:00
void view_child_finish(struct roots_view_child *child) {
if (child == NULL) {
return;
}
view_damage_whole(child->view);
wl_list_remove(&child->link);
wl_list_remove(&child->commit.link);
wl_list_remove(&child->new_subsurface.link);
}
static void view_child_handle_commit(struct wl_listener *listener,
void *data) {
struct roots_view_child *child = wl_container_of(listener, child, commit);
view_apply_damage(child->view);
}
static void view_child_handle_new_subsurface(struct wl_listener *listener,
void *data) {
struct roots_view_child *child =
wl_container_of(listener, child, new_subsurface);
struct wlr_subsurface *wlr_subsurface = data;
subsurface_create(child->view, wlr_subsurface);
}
void view_child_init(struct roots_view_child *child, struct roots_view *view,
struct wlr_surface *wlr_surface) {
assert(child->destroy);
2018-01-21 13:50:37 +00:00
child->view = view;
child->wlr_surface = wlr_surface;
child->commit.notify = view_child_handle_commit;
wl_signal_add(&wlr_surface->events.commit, &child->commit);
child->new_subsurface.notify = view_child_handle_new_subsurface;
wl_signal_add(&wlr_surface->events.new_subsurface, &child->new_subsurface);
wl_list_insert(&view->children, &child->link);
}
static void subsurface_destroy(struct roots_view_child *child) {
assert(child->destroy == subsurface_destroy);
struct roots_subsurface *subsurface = (struct roots_subsurface *)child;
if (subsurface == NULL) {
return;
}
wl_list_remove(&subsurface->destroy.link);
view_child_finish(&subsurface->view_child);
free(subsurface);
}
2018-01-21 13:50:37 +00:00
static void subsurface_handle_destroy(struct wl_listener *listener,
void *data) {
struct roots_subsurface *subsurface =
wl_container_of(listener, subsurface, destroy);
subsurface_destroy((struct roots_view_child *)subsurface);
2018-01-21 13:50:37 +00:00
}
struct roots_subsurface *subsurface_create(struct roots_view *view,
struct wlr_subsurface *wlr_subsurface) {
struct roots_subsurface *subsurface =
calloc(1, sizeof(struct roots_subsurface));
if (subsurface == NULL) {
return NULL;
}
subsurface->wlr_subsurface = wlr_subsurface;
subsurface->view_child.destroy = subsurface_destroy;
2018-01-21 13:50:37 +00:00
view_child_init(&subsurface->view_child, view, wlr_subsurface->surface);
subsurface->destroy.notify = subsurface_handle_destroy;
wl_signal_add(&wlr_subsurface->events.destroy, &subsurface->destroy);
input_update_cursor_focus(view->desktop->server->input);
2018-01-21 13:50:37 +00:00
return subsurface;
}
2018-03-09 09:29:22 +00:00
void view_destroy(struct roots_view *view) {
if (view == NULL) {
return;
}
2017-11-17 11:45:07 +00:00
wl_signal_emit(&view->events.destroy, view);
2018-01-21 13:50:37 +00:00
2018-03-09 09:29:22 +00:00
if (view->wlr_surface != NULL) {
view_unmap(view);
}
// Can happen if fullscreened while unmapped, and hasn't been mapped
if (view->fullscreen_output != NULL) {
view->fullscreen_output->fullscreen_view = NULL;
}
if (view->destroy) {
view->destroy(view);
}
2018-03-09 09:29:22 +00:00
free(view);
2018-01-21 13:50:37 +00:00
}
2018-01-21 13:50:37 +00:00
static void view_handle_new_subsurface(struct wl_listener *listener,
void *data) {
struct roots_view *view = wl_container_of(listener, view, new_subsurface);
struct wlr_subsurface *wlr_subsurface = data;
subsurface_create(view, wlr_subsurface);
2017-11-17 11:45:07 +00:00
}
2018-03-09 09:29:22 +00:00
void view_map(struct roots_view *view, struct wlr_surface *surface) {
assert(view->wlr_surface == NULL);
2018-01-21 13:50:37 +00:00
2018-03-09 09:29:22 +00:00
view->wlr_surface = surface;
2018-01-21 13:50:37 +00:00
struct wlr_subsurface *subsurface;
2018-04-21 17:40:25 +00:00
wl_list_for_each(subsurface, &view->wlr_surface->subsurfaces,
2018-01-21 13:50:37 +00:00
parent_link) {
subsurface_create(view, subsurface);
}
view->new_subsurface.notify = view_handle_new_subsurface;
wl_signal_add(&view->wlr_surface->events.new_subsurface,
&view->new_subsurface);
2018-03-09 09:29:22 +00:00
wl_list_insert(&view->desktop->views, &view->link);
view_damage_whole(view);
input_update_cursor_focus(view->desktop->server->input);
2017-11-17 11:45:07 +00:00
}
2018-03-09 09:29:22 +00:00
void view_unmap(struct roots_view *view) {
assert(view->wlr_surface != NULL);
wl_signal_emit(&view->events.unmap, view);
view_damage_whole(view);
2018-03-09 09:29:22 +00:00
wl_list_remove(&view->link);
wl_list_remove(&view->new_subsurface.link);
struct roots_view_child *child, *tmp;
wl_list_for_each_safe(child, tmp, &view->children, link) {
child->destroy(child);
}
if (view->fullscreen_output != NULL) {
output_damage_whole(view->fullscreen_output);
view->fullscreen_output->fullscreen_view = NULL;
view->fullscreen_output = NULL;
}
view->wlr_surface = NULL;
2018-12-05 09:01:25 +00:00
view->box.width = view->box.height = 0;
if (view->toplevel_handle) {
wlr_foreign_toplevel_handle_v1_destroy(view->toplevel_handle);
view->toplevel_handle = NULL;
}
2017-11-17 11:45:07 +00:00
}
void view_initial_focus(struct roots_view *view) {
2017-10-25 18:34:40 +00:00
struct roots_input *input = view->desktop->server->input;
2017-11-07 20:56:11 +00:00
// TODO what seat gets focus? the one with the last input event?
struct roots_seat *seat;
wl_list_for_each(seat, &input->seats, link) {
2017-11-19 18:21:18 +00:00
roots_seat_set_focus(seat, view);
2017-11-07 20:56:11 +00:00
}
}
void view_setup(struct roots_view *view) {
view_initial_focus(view);
if (view->fullscreen_output == NULL && !view->maximized) {
2018-03-10 10:18:50 +00:00
view_center(view);
}
view_create_foreign_toplevel_handle(view);
view_update_output(view, NULL);
2017-10-08 16:19:05 +00:00
}
void view_apply_damage(struct roots_view *view) {
2018-01-18 11:25:39 +00:00
struct roots_output *output;
wl_list_for_each(output, &view->desktop->outputs, link) {
output_damage_from_view(output, view);
}
}
void view_damage_whole(struct roots_view *view) {
struct roots_output *output;
wl_list_for_each(output, &view->desktop->outputs, link) {
output_damage_whole_view(output, view);
2018-01-18 11:25:39 +00:00
}
}
2018-12-05 09:01:25 +00:00
void view_update_position(struct roots_view *view, int x, int y) {
if (view->box.x == x && view->box.y == y) {
return;
}
view_damage_whole(view);
2018-12-05 09:01:25 +00:00
view->box.x = x;
view->box.y = y;
view_damage_whole(view);
2018-01-18 11:25:39 +00:00
}
2018-12-05 08:58:51 +00:00
void view_update_size(struct roots_view *view, int width, int height) {
2018-12-05 09:01:25 +00:00
if (view->box.width == width && view->box.height == height) {
2018-01-28 09:11:31 +00:00
return;
}
view_damage_whole(view);
2018-12-05 09:01:25 +00:00
view->box.width = width;
view->box.height = height;
2018-01-28 09:11:31 +00:00
view_damage_whole(view);
}
2018-06-10 10:15:26 +00:00
void view_update_decorated(struct roots_view *view, bool decorated) {
if (view->decorated == decorated) {
return;
}
view_damage_whole(view);
view->decorated = decorated;
if (decorated) {
view->border_width = 4;
view->titlebar_height = 12;
} else {
view->border_width = 0;
view->titlebar_height = 0;
}
view_damage_whole(view);
}
void view_set_title(struct roots_view *view, const char *title) {
if (view->toplevel_handle) {
wlr_foreign_toplevel_handle_v1_set_title(view->toplevel_handle, title);
}
}
void view_set_app_id(struct roots_view *view, const char *app_id) {
if (view->toplevel_handle) {
wlr_foreign_toplevel_handle_v1_set_app_id(view->toplevel_handle, app_id);
}
}
static void handle_toplevel_handle_request_maximize(struct wl_listener *listener,
void *data) {
struct roots_view *view = wl_container_of(listener, view,
toplevel_handle_request_maximize);
struct wlr_foreign_toplevel_handle_v1_maximized_event *event = data;
view_maximize(view, event->maximized);
}
static void handle_toplevel_handle_request_activate(struct wl_listener *listener,
void *data) {
struct roots_view *view =
wl_container_of(listener, view, toplevel_handle_request_activate);
struct wlr_foreign_toplevel_handle_v1_activated_event *event = data;
struct roots_seat *seat;
wl_list_for_each(seat, &view->desktop->server->input->seats, link) {
if (event->seat == seat->seat) {
roots_seat_set_focus(seat, view);
}
}
}
static void handle_toplevel_handle_request_close(struct wl_listener *listener,
void *data) {
struct roots_view *view =
wl_container_of(listener, view, toplevel_handle_request_close);
view_close(view);
}
void view_create_foreign_toplevel_handle(struct roots_view *view) {
view->toplevel_handle =
wlr_foreign_toplevel_handle_v1_create(
view->desktop->foreign_toplevel_manager_v1);
view->toplevel_handle_request_maximize.notify =
handle_toplevel_handle_request_maximize;
wl_signal_add(&view->toplevel_handle->events.request_maximize,
&view->toplevel_handle_request_maximize);
view->toplevel_handle_request_activate.notify =
handle_toplevel_handle_request_activate;
wl_signal_add(&view->toplevel_handle->events.request_activate,
&view->toplevel_handle_request_activate);
view->toplevel_handle_request_close.notify =
handle_toplevel_handle_request_close;
wl_signal_add(&view->toplevel_handle->events.request_close,
&view->toplevel_handle_request_close);
}
static bool view_at(struct roots_view *view, double lx, double ly,
2017-09-30 16:33:39 +00:00
struct wlr_surface **surface, double *sx, double *sy) {
if (view->type == ROOTS_WL_SHELL_VIEW &&
view->wl_shell_surface->state == WLR_WL_SHELL_SURFACE_STATE_POPUP) {
return false;
}
if (view->wlr_surface == NULL) {
return false;
}
2017-10-10 14:00:09 +00:00
2018-12-05 09:01:25 +00:00
double view_sx = lx - view->box.x;
double view_sy = ly - view->box.y;
struct wlr_surface_state *state = &view->wlr_surface->current;
struct wlr_box box = {
.x = 0, .y = 0,
.width = state->width, .height = state->height,
};
if (view->rotation != 0.0) {
// Coordinates relative to the center of the view
double ox = view_sx - (double)box.width/2,
oy = view_sy - (double)box.height/2;
// Rotated coordinates
double rx = cos(view->rotation)*ox + sin(view->rotation)*oy,
ry = cos(view->rotation)*oy - sin(view->rotation)*ox;
view_sx = rx + (double)box.width/2;
view_sy = ry + (double)box.height/2;
}
2017-10-04 19:05:00 +00:00
double _sx, _sy;
2018-04-05 17:13:21 +00:00
struct wlr_surface *_surface = NULL;
switch (view->type) {
case ROOTS_XDG_SHELL_V6_VIEW:
_surface = wlr_xdg_surface_v6_surface_at(view->xdg_surface_v6,
view_sx, view_sy, &_sx, &_sy);
break;
case ROOTS_XDG_SHELL_VIEW:
_surface = wlr_xdg_surface_surface_at(view->xdg_surface,
view_sx, view_sy, &_sx, &_sy);
break;
case ROOTS_WL_SHELL_VIEW:
_surface = wlr_wl_shell_surface_surface_at(view->wl_shell_surface,
view_sx, view_sy, &_sx, &_sy);
break;
#if WLR_HAS_XWAYLAND
case ROOTS_XWAYLAND_VIEW:
_surface = wlr_surface_surface_at(view->wlr_surface,
view_sx, view_sy, &_sx, &_sy);
break;
#endif
}
2018-04-04 21:16:35 +00:00
if (_surface != NULL) {
*sx = _sx;
*sy = _sy;
*surface = _surface;
return true;
}
2018-01-21 23:24:53 +00:00
if (view_get_deco_part(view, view_sx, view_sy)) {
*sx = view_sx;
*sy = view_sy;
2018-01-23 12:11:54 +00:00
*surface = NULL;
return true;
2018-01-21 23:24:53 +00:00
}
return false;
}
2018-03-30 20:54:37 +00:00
static struct roots_view *desktop_view_at(struct roots_desktop *desktop,
double lx, double ly, struct wlr_surface **surface,
double *sx, double *sy) {
struct wlr_output *wlr_output =
wlr_output_layout_output_at(desktop->layout, lx, ly);
if (wlr_output != NULL) {
struct roots_output *output =
desktop_output_from_wlr_output(desktop, wlr_output);
if (output != NULL && output->fullscreen_view != NULL) {
if (view_at(output->fullscreen_view, lx, ly, surface, sx, sy)) {
return output->fullscreen_view;
} else {
return NULL;
}
}
}
struct roots_view *view;
wl_list_for_each(view, &desktop->views, link) {
if (view_at(view, lx, ly, surface, sx, sy)) {
2017-09-23 21:48:13 +00:00
return view;
}
}
return NULL;
}
2018-03-30 20:54:37 +00:00
static struct wlr_surface *layer_surface_at(struct roots_output *output,
struct wl_list *layer, double ox, double oy, double *sx, double *sy) {
struct roots_layer_surface *roots_surface;
wl_list_for_each_reverse(roots_surface, layer, link) {
double _sx = ox - roots_surface->geo.x;
double _sy = oy - roots_surface->geo.y;
2018-09-14 17:32:33 +00:00
struct wlr_surface *sub = wlr_layer_surface_v1_surface_at(
roots_surface->layer_surface, _sx, _sy, sx, sy);
if (sub) {
return sub;
2018-03-30 20:54:37 +00:00
}
}
2018-03-30 20:54:37 +00:00
return NULL;
}
2018-03-30 19:19:33 +00:00
struct wlr_surface *desktop_surface_at(struct roots_desktop *desktop,
double lx, double ly, double *sx, double *sy,
struct roots_view **view) {
2018-03-30 20:54:37 +00:00
struct wlr_surface *surface = NULL;
struct wlr_output *wlr_output =
wlr_output_layout_output_at(desktop->layout, lx, ly);
struct roots_output *roots_output = NULL;
double ox = lx, oy = ly;
2018-03-31 13:55:41 +00:00
if (view) {
*view = NULL;
}
2018-03-30 22:28:21 +00:00
2018-03-30 20:54:37 +00:00
if (wlr_output) {
roots_output = wlr_output->data;
wlr_output_layout_output_coords(desktop->layout, wlr_output, &ox, &oy);
if ((surface = layer_surface_at(roots_output,
&roots_output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY],
ox, oy, sx, sy))) {
return surface;
}
if ((surface = layer_surface_at(roots_output,
&roots_output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP],
ox, oy, sx, sy))) {
return surface;
}
}
2018-03-30 19:19:33 +00:00
struct roots_view *_view;
if ((_view = desktop_view_at(desktop, lx, ly, &surface, sx, sy))) {
if (view) {
*view = _view;
}
return surface;
}
2018-03-30 20:54:37 +00:00
if (wlr_output) {
if ((surface = layer_surface_at(roots_output,
&roots_output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM],
ox, oy, sx, sy))) {
return surface;
}
if ((surface = layer_surface_at(roots_output,
&roots_output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND],
ox, oy, sx, sy))) {
return surface;
}
}
2018-03-30 19:19:33 +00:00
return NULL;
}
2017-12-31 11:49:06 +00:00
static void handle_layout_change(struct wl_listener *listener, void *data) {
struct roots_desktop *desktop =
wl_container_of(listener, desktop, layout_change);
struct wlr_output *center_output =
wlr_output_layout_get_center_output(desktop->layout);
if (center_output == NULL) {
return;
}
struct wlr_box *center_output_box =
wlr_output_layout_get_box(desktop->layout, center_output);
double center_x = center_output_box->x + center_output_box->width/2;
double center_y = center_output_box->y + center_output_box->height/2;
struct roots_view *view;
wl_list_for_each(view, &desktop->views, link) {
struct wlr_box box;
view_get_box(view, &box);
if (wlr_output_layout_intersects(desktop->layout, NULL, &box)) {
continue;
}
view_move(view, center_x - box.width/2, center_y - box.height/2);
}
}
2018-04-03 16:50:05 +00:00
static void input_inhibit_activate(struct wl_listener *listener, void *data) {
struct roots_desktop *desktop = wl_container_of(
listener, desktop, input_inhibit_activate);
struct roots_seat *seat;
wl_list_for_each(seat, &desktop->server->input->seats, link) {
roots_seat_set_exclusive_client(seat,
desktop->input_inhibit->active_client);
}
}
static void input_inhibit_deactivate(struct wl_listener *listener, void *data) {
struct roots_desktop *desktop = wl_container_of(
listener, desktop, input_inhibit_deactivate);
struct roots_seat *seat;
wl_list_for_each(seat, &desktop->server->input->seats, link) {
roots_seat_set_exclusive_client(seat, NULL);
}
}
static void handle_constraint_destroy(struct wl_listener *listener,
void *data) {
struct roots_pointer_constraint *constraint =
wl_container_of(listener, constraint, destroy);
struct wlr_pointer_constraint_v1 *wlr_constraint = data;
struct roots_seat *seat = wlr_constraint->seat->data;
wl_list_remove(&constraint->destroy.link);
if (seat->cursor->active_constraint == wlr_constraint) {
wl_list_remove(&seat->cursor->constraint_commit.link);
wl_list_init(&seat->cursor->constraint_commit.link);
seat->cursor->active_constraint = NULL;
if (wlr_constraint->current.committed &
WLR_POINTER_CONSTRAINT_V1_STATE_CURSOR_HINT &&
seat->cursor->pointer_view) {
double sx = wlr_constraint->current.cursor_hint.x;
double sy = wlr_constraint->current.cursor_hint.y;
struct roots_view *view = seat->cursor->pointer_view->view;
2018-12-05 09:01:25 +00:00
rotate_child_position(&sx, &sy, 0, 0, view->box.width, view->box.height,
view->rotation);
2018-12-05 09:01:25 +00:00
double lx = view->box.x + sx;
double ly = view->box.y + sy;
wlr_cursor_warp(seat->cursor->cursor, NULL, lx, ly);
}
}
free(constraint);
}
static void handle_pointer_constraint(struct wl_listener *listener,
void *data) {
struct wlr_pointer_constraint_v1 *wlr_constraint = data;
struct roots_seat *seat = wlr_constraint->seat->data;
struct roots_pointer_constraint *constraint =
calloc(1, sizeof(struct roots_pointer_constraint));
constraint->constraint = wlr_constraint;
constraint->destroy.notify = handle_constraint_destroy;
wl_signal_add(&wlr_constraint->events.destroy, &constraint->destroy);
double sx, sy;
struct wlr_surface *surface = desktop_surface_at(
seat->input->server->desktop,
seat->cursor->cursor->x, seat->cursor->cursor->y, &sx, &sy, NULL);
if (surface == wlr_constraint->surface) {
assert(!seat->cursor->active_constraint);
roots_cursor_constrain(seat->cursor, wlr_constraint, sx, sy);
}
}
struct roots_desktop *desktop_create(struct roots_server *server,
struct roots_config *config) {
2018-07-09 21:49:54 +00:00
wlr_log(WLR_DEBUG, "Initializing roots desktop");
struct roots_desktop *desktop = calloc(1, sizeof(struct roots_desktop));
if (desktop == NULL) {
return NULL;
}
2017-11-17 11:45:07 +00:00
wl_list_init(&desktop->views);
wl_list_init(&desktop->outputs);
desktop->new_output.notify = handle_new_output;
wl_signal_add(&server->backend->events.new_output, &desktop->new_output);
desktop->server = server;
desktop->config = config;
desktop->layout = wlr_output_layout_create();
2018-09-14 17:29:08 +00:00
wlr_xdg_output_manager_v1_create(server->wl_display, desktop->layout);
desktop->layout_change.notify = handle_layout_change;
wl_signal_add(&desktop->layout->events.change, &desktop->layout_change);
desktop->compositor = wlr_compositor_create(server->wl_display,
server->renderer);
desktop->xdg_shell_v6 = wlr_xdg_shell_v6_create(server->wl_display);
wl_signal_add(&desktop->xdg_shell_v6->events.new_surface,
&desktop->xdg_shell_v6_surface);
desktop->xdg_shell_v6_surface.notify = handle_xdg_shell_v6_surface;
desktop->xdg_shell = wlr_xdg_shell_create(server->wl_display);
wl_signal_add(&desktop->xdg_shell->events.new_surface,
&desktop->xdg_shell_surface);
desktop->xdg_shell_surface.notify = handle_xdg_shell_surface;
desktop->wl_shell = wlr_wl_shell_create(server->wl_display);
wl_signal_add(&desktop->wl_shell->events.new_surface,
&desktop->wl_shell_surface);
desktop->wl_shell_surface.notify = handle_wl_shell_surface;
2018-09-14 17:32:33 +00:00
desktop->layer_shell = wlr_layer_shell_v1_create(server->wl_display);
wl_signal_add(&desktop->layer_shell->events.new_surface,
&desktop->layer_shell_surface);
desktop->layer_shell_surface.notify = handle_layer_shell_surface;
2018-03-17 19:07:48 +00:00
2018-04-24 06:10:45 +00:00
desktop->tablet_v2 = wlr_tablet_v2_create(server->wl_display);
2017-11-18 16:34:24 +00:00
const char *cursor_theme = NULL;
#if WLR_HAS_XWAYLAND
2017-12-11 09:36:22 +00:00
const char *cursor_default = ROOTS_XCURSOR_DEFAULT;
#endif
2017-11-18 16:34:24 +00:00
struct roots_cursor_config *cc =
roots_config_get_cursor(config, ROOTS_CONFIG_DEFAULT_SEAT_NAME);
if (cc != NULL) {
cursor_theme = cc->theme;
#if WLR_HAS_XWAYLAND
2017-12-11 09:36:22 +00:00
if (cc->default_image != NULL) {
cursor_default = cc->default_image;
}
#endif
2017-11-18 16:34:24 +00:00
}
char cursor_size_fmt[16];
snprintf(cursor_size_fmt, sizeof(cursor_size_fmt),
"%d", ROOTS_XCURSOR_SIZE);
setenv("XCURSOR_SIZE", cursor_size_fmt, 1);
if (cursor_theme != NULL) {
setenv("XCURSOR_THEME", cursor_theme, 1);
}
#if WLR_HAS_XWAYLAND
2017-11-18 16:34:24 +00:00
desktop->xcursor_manager = wlr_xcursor_manager_create(cursor_theme,
2017-11-12 10:10:56 +00:00
ROOTS_XCURSOR_SIZE);
if (desktop->xcursor_manager == NULL) {
2018-07-09 21:49:54 +00:00
wlr_log(WLR_ERROR, "Cannot create XCursor manager for theme %s",
2017-11-18 16:34:24 +00:00
cursor_theme);
free(desktop);
return NULL;
}
2017-10-06 06:41:50 +00:00
if (config->xwayland) {
desktop->xwayland = wlr_xwayland_create(server->wl_display,
desktop->compositor, config->xwayland_lazy);
2017-10-06 06:41:50 +00:00
wl_signal_add(&desktop->xwayland->events.new_surface,
&desktop->xwayland_surface);
desktop->xwayland_surface.notify = handle_xwayland_surface;
2017-11-12 10:10:56 +00:00
if (wlr_xcursor_manager_load(desktop->xcursor_manager, 1)) {
2018-07-09 21:49:54 +00:00
wlr_log(WLR_ERROR, "Cannot load XWayland XCursor theme");
2017-11-12 10:10:56 +00:00
}
struct wlr_xcursor *xcursor = wlr_xcursor_manager_get_xcursor(
2017-12-11 09:36:22 +00:00
desktop->xcursor_manager, cursor_default, 1);
2017-11-12 10:10:56 +00:00
if (xcursor != NULL) {
struct wlr_xcursor_image *image = xcursor->images[0];
wlr_xwayland_set_cursor(desktop->xwayland, image->buffer,
image->width * 4, image->width, image->height, image->hotspot_x,
2017-11-12 10:10:56 +00:00
image->hotspot_y);
}
2017-10-06 06:41:50 +00:00
}
2017-10-06 21:50:25 +00:00
#endif
2017-09-28 12:54:57 +00:00
desktop->gamma_control_manager = wlr_gamma_control_manager_create(
server->wl_display);
desktop->gamma_control_manager_v1 = wlr_gamma_control_manager_v1_create(
server->wl_display);
desktop->screenshooter = wlr_screenshooter_create(server->wl_display);
2018-05-23 08:07:40 +00:00
desktop->export_dmabuf_manager_v1 =
wlr_export_dmabuf_manager_v1_create(server->wl_display);
desktop->server_decoration_manager =
wlr_server_decoration_manager_create(server->wl_display);
wlr_server_decoration_manager_set_default_mode(
desktop->server_decoration_manager,
2017-12-03 18:16:41 +00:00
WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT);
desktop->idle = wlr_idle_create(server->wl_display);
desktop->idle_inhibit = wlr_idle_inhibit_v1_create(server->wl_display);
gtk-primary-selection: refactor everything, untie from seat This commits completely refactors wlr_gtk_primary_selection. The goal is to remove gtk-primary-selection state from the seat and better handle inert resources where it makes sense. wlr_seat_client.primary_selection_devices has been removed and replaced by wlr_gtk_primary_selection_device. This allows us to make offers inert when the current selection is replaced. wlr_seat_set_primary_selection has been removed because it relied on wlr_seat instead of wlr_gtk_primary_selection_device_manager. A new function, wlr_gtk_primary_selection_device_manager_set_selection (candidate for the longest function name in wlroots) has been added. It doesn't take a serial anymore as serial checking only makes sense for set_selection requests coming from Wayland clients (serial checking is now done in the Wayland interface implementation). Since wlr_gtk_primary_selection_device_manager is now required to set the selection, a new function wlr_xwayland_set_gtk_primary_selection_device_manager (candidate number two for longest function name) has been added. Devices are now made inert when the seat goes away. Future work includes removing the last primary selection bits from the seat, mainly wlr_seat.primary_selection_source and wlr_seat.events.primary_selection, replacing those with new fields in wlr_gtk_primary_selection_device. Or maybe we could keep those in the seat and replace them with a re-usable interface (for future zwp_primary_selection_v1 support). We need to think how we'll sync these three protocols (GTK, X11 and wayland-protocols). See https://github.com/swaywm/wlroots/issues/1388
2018-11-27 17:41:46 +00:00
desktop->primary_selection_device_manager =
wlr_gtk_primary_selection_device_manager_create(server->wl_display);
2018-04-03 15:29:32 +00:00
desktop->input_inhibit =
wlr_input_inhibit_manager_create(server->wl_display);
2018-04-03 16:50:05 +00:00
desktop->input_inhibit_activate.notify = input_inhibit_activate;
wl_signal_add(&desktop->input_inhibit->events.activate,
&desktop->input_inhibit_activate);
2018-04-03 16:50:05 +00:00
desktop->input_inhibit_deactivate.notify = input_inhibit_deactivate;
wl_signal_add(&desktop->input_inhibit->events.deactivate,
&desktop->input_inhibit_deactivate);
desktop->input_method =
wlr_input_method_manager_v2_create(server->wl_display);
desktop->text_input = wlr_text_input_manager_v3_create(server->wl_display);
desktop->virtual_keyboard = wlr_virtual_keyboard_manager_v1_create(
server->wl_display);
wl_signal_add(&desktop->virtual_keyboard->events.new_virtual_keyboard,
&desktop->virtual_keyboard_new);
desktop->virtual_keyboard_new.notify = handle_virtual_keyboard;
desktop->screencopy = wlr_screencopy_manager_v1_create(server->wl_display);
2018-06-10 10:15:26 +00:00
desktop->xdg_decoration_manager =
wlr_xdg_decoration_manager_v1_create(server->wl_display);
wl_signal_add(&desktop->xdg_decoration_manager->events.new_toplevel_decoration,
&desktop->xdg_toplevel_decoration);
desktop->xdg_toplevel_decoration.notify = handle_xdg_toplevel_decoration;
desktop->pointer_constraints =
wlr_pointer_constraints_v1_create(server->wl_display);
desktop->pointer_constraint.notify = handle_pointer_constraint;
wl_signal_add(&desktop->pointer_constraints->events.new_constraint,
&desktop->pointer_constraint);
2018-10-02 09:46:28 +00:00
desktop->presentation =
wlr_presentation_create(server->wl_display, server->backend);
desktop->foreign_toplevel_manager_v1 =
wlr_foreign_toplevel_manager_v1_create(server->wl_display);
desktop->relative_pointer_manager =
wlr_relative_pointer_manager_v1_create(server->wl_display);
desktop->pointer_gestures =
wlr_pointer_gestures_v1_create(server->wl_display);
2018-10-02 09:46:28 +00:00
wlr_primary_selection_v1_device_manager_create(server->wl_display);
wlr_data_control_manager_v1_create(server->wl_display);
return desktop;
}
void desktop_destroy(struct roots_desktop *desktop) {
// TODO
}
struct roots_output *desktop_output_from_wlr_output(
struct roots_desktop *desktop, struct wlr_output *wlr_output) {
struct roots_output *output;
wl_list_for_each(output, &desktop->outputs, link) {
if (output->wlr_output == wlr_output) {
return output;
}
}
return NULL;
}