sway/sway/input/seat.c

530 lines
15 KiB
C
Raw Normal View History

2017-12-07 14:58:32 +00:00
#define _XOPEN_SOURCE 700
2018-03-31 04:44:17 +00:00
#include <assert.h>
2017-12-08 12:22:26 +00:00
#include <wlr/types/wlr_cursor.h>
2018-03-31 19:13:27 +00:00
#include <wlr/types/wlr_output_layout.h>
2017-12-09 16:51:28 +00:00
#include <wlr/types/wlr_xcursor_manager.h>
#include "sway/tree/container.h"
2017-12-08 13:07:47 +00:00
#include "sway/input/seat.h"
#include "sway/input/cursor.h"
#include "sway/input/input-manager.h"
2017-12-10 18:59:04 +00:00
#include "sway/input/keyboard.h"
#include "sway/ipc-server.h"
2017-12-09 16:51:28 +00:00
#include "sway/output.h"
#include "sway/tree/container.h"
#include "sway/tree/view.h"
2017-12-07 14:58:32 +00:00
#include "log.h"
2017-12-14 16:11:56 +00:00
static void seat_device_destroy(struct sway_seat_device *seat_device) {
if (!seat_device) {
return;
}
sway_keyboard_destroy(seat_device->keyboard);
wlr_cursor_detach_input_device(seat_device->sway_seat->cursor->cursor,
seat_device->input_device->wlr_device);
wl_list_remove(&seat_device->link);
free(seat_device);
}
2018-04-02 12:45:37 +00:00
void seat_destroy(struct sway_seat *seat) {
struct sway_seat_device *seat_device, *next;
wl_list_for_each_safe(seat_device, next, &seat->devices, link) {
seat_device_destroy(seat_device);
}
sway_cursor_destroy(seat->cursor);
wl_list_remove(&seat->link);
wlr_seat_destroy(seat->wlr_seat);
}
2018-03-31 19:22:10 +00:00
static struct sway_seat_container *seat_container_from_container(
struct sway_seat *seat, struct sway_container *con);
static void seat_container_destroy(struct sway_seat_container *seat_con) {
struct sway_container *con = seat_con->container;
struct sway_container *child = NULL;
if (con->children != NULL) {
for (int i = 0; i < con->children->length; ++i) {
child = con->children->items[i];
struct sway_seat_container *seat_child =
seat_container_from_container(seat_con->seat, child);
seat_container_destroy(seat_child);
}
}
wl_list_remove(&seat_con->destroy.link);
wl_list_remove(&seat_con->link);
free(seat_con);
}
2018-03-31 21:07:37 +00:00
static void seat_send_focus(struct sway_seat *seat,
struct sway_container *con) {
if (con->type != C_VIEW) {
return;
}
struct sway_view *view = con->sway_view;
if (view->type == SWAY_XWAYLAND_VIEW) {
struct wlr_xwayland *xwayland =
seat->input->server->xwayland;
wlr_xwayland_set_seat(xwayland, seat->wlr_seat);
}
view_set_activated(view, true);
struct wlr_keyboard *keyboard =
wlr_seat_get_keyboard(seat->wlr_seat);
if (keyboard) {
wlr_seat_keyboard_notify_enter(seat->wlr_seat,
view->surface, keyboard->keycodes,
keyboard->num_keycodes, &keyboard->modifiers);
} else {
wlr_seat_keyboard_notify_enter(
seat->wlr_seat, view->surface, NULL, 0, NULL);
}
}
2018-02-04 18:39:10 +00:00
static void handle_seat_container_destroy(struct wl_listener *listener,
void *data) {
struct sway_seat_container *seat_con =
wl_container_of(listener, seat_con, destroy);
2018-02-10 23:10:29 +00:00
struct sway_seat *seat = seat_con->seat;
struct sway_container *con = seat_con->container;
2018-03-31 19:22:10 +00:00
struct sway_container *parent = con->parent;
struct sway_container *focus = seat_get_focus(seat);
2018-02-10 23:10:29 +00:00
2018-03-31 19:22:10 +00:00
bool set_focus =
2018-03-31 22:52:02 +00:00
focus != NULL &&
2018-03-31 19:22:10 +00:00
(focus == con || container_has_child(con, focus)) &&
con->type != C_WORKSPACE;
2018-02-10 23:10:29 +00:00
2018-03-31 19:22:10 +00:00
seat_container_destroy(seat_con);
2018-02-10 23:10:29 +00:00
2018-03-31 21:07:37 +00:00
if (set_focus) {
2018-03-31 19:22:10 +00:00
struct sway_container *next_focus = NULL;
while (next_focus == NULL) {
next_focus = seat_get_focus_by_type(seat, parent, C_VIEW);
2018-02-10 23:10:29 +00:00
2018-03-31 19:22:10 +00:00
if (next_focus == NULL && parent->type == C_WORKSPACE) {
next_focus = parent;
break;
}
2018-03-31 22:52:02 +00:00
parent = parent->parent;
2018-03-31 19:22:10 +00:00
}
2018-02-10 23:10:29 +00:00
2018-03-31 21:07:37 +00:00
// the structure change might have caused it to move up to the top of
// the focus stack without sending focus notifications to the view
if (seat_get_focus(seat) == next_focus) {
2018-03-31 21:07:37 +00:00
seat_send_focus(seat, next_focus);
} else {
seat_set_focus(seat, next_focus);
2018-03-31 21:07:37 +00:00
}
2018-03-31 19:22:10 +00:00
}
2018-02-04 18:39:10 +00:00
}
static struct sway_seat_container *seat_container_from_container(
struct sway_seat *seat, struct sway_container *con) {
2018-02-10 23:10:29 +00:00
if (con->type < C_WORKSPACE) {
// these don't get seat containers ever
return NULL;
}
2018-02-04 18:39:10 +00:00
struct sway_seat_container *seat_con = NULL;
wl_list_for_each(seat_con, &seat->focus_stack, link) {
if (seat_con->container == con) {
return seat_con;
}
}
seat_con = calloc(1, sizeof(struct sway_seat_container));
if (seat_con == NULL) {
wlr_log(L_ERROR, "could not allocate seat container");
return NULL;
}
seat_con->container = con;
2018-02-10 23:10:29 +00:00
seat_con->seat = seat;
2018-02-04 18:39:10 +00:00
wl_list_insert(seat->focus_stack.prev, &seat_con->link);
wl_signal_add(&con->events.destroy, &seat_con->destroy);
seat_con->destroy.notify = handle_seat_container_destroy;
return seat_con;
}
static void handle_new_container(struct wl_listener *listener, void *data) {
struct sway_seat *seat = wl_container_of(listener, seat, new_container);
struct sway_container *con = data;
2018-02-04 18:39:10 +00:00
seat_container_from_container(seat, con);
}
static void collect_focus_iter(struct sway_container *con, void *data) {
struct sway_seat *seat = data;
if (con->type > C_WORKSPACE) {
return;
}
struct sway_seat_container *seat_con =
seat_container_from_container(seat, con);
if (!seat_con) {
return;
}
wl_list_remove(&seat_con->link);
wl_list_insert(&seat->focus_stack, &seat_con->link);
}
2018-04-02 12:45:37 +00:00
struct sway_seat *seat_create(struct sway_input_manager *input,
2017-12-07 14:58:32 +00:00
const char *seat_name) {
struct sway_seat *seat = calloc(1, sizeof(struct sway_seat));
if (!seat) {
return NULL;
}
2017-12-08 12:22:26 +00:00
2017-12-14 16:11:56 +00:00
seat->wlr_seat = wlr_seat_create(input->server->wl_display, seat_name);
if (!sway_assert(seat->wlr_seat, "could not allocate seat")) {
2017-12-20 11:12:08 +00:00
free(seat);
2017-12-08 12:22:26 +00:00
return NULL;
}
seat->cursor = sway_cursor_create(seat);
if (!seat->cursor) {
2017-12-14 16:11:56 +00:00
wlr_seat_destroy(seat->wlr_seat);
2017-12-08 12:22:26 +00:00
free(seat);
return NULL;
}
2018-02-04 18:39:10 +00:00
// init the focus stack
wl_list_init(&seat->focus_stack);
2018-04-02 15:44:42 +00:00
container_for_each_descendant_dfs(&root_container,
collect_focus_iter, seat);
2018-02-04 18:39:10 +00:00
wl_signal_add(&root_container.sway_root->events.new_container,
&seat->new_container);
seat->new_container.notify = handle_new_container;
2017-12-10 16:11:47 +00:00
seat->input = input;
2017-12-14 16:11:56 +00:00
wl_list_init(&seat->devices);
2017-12-10 16:11:47 +00:00
2017-12-14 16:11:56 +00:00
wlr_seat_set_capabilities(seat->wlr_seat,
2017-12-08 12:22:26 +00:00
WL_SEAT_CAPABILITY_KEYBOARD |
WL_SEAT_CAPABILITY_POINTER |
WL_SEAT_CAPABILITY_TOUCH);
2018-04-02 12:45:37 +00:00
seat_configure_xcursor(seat);
2017-12-09 16:51:28 +00:00
2017-12-12 13:29:37 +00:00
wl_list_insert(&input->seats, &seat->link);
2017-12-10 18:59:04 +00:00
2017-12-07 14:58:32 +00:00
return seat;
}
2017-12-14 16:11:56 +00:00
static void seat_configure_pointer(struct sway_seat *seat,
struct sway_seat_device *sway_device) {
2017-12-12 13:29:37 +00:00
wlr_cursor_attach_input_device(seat->cursor->cursor,
2017-12-14 16:11:56 +00:00
sway_device->input_device->wlr_device);
2017-12-08 12:22:26 +00:00
}
2017-12-14 16:11:56 +00:00
static void seat_configure_keyboard(struct sway_seat *seat,
struct sway_seat_device *seat_device) {
if (!seat_device->keyboard) {
sway_keyboard_create(seat, seat_device);
}
2018-04-02 15:44:42 +00:00
struct wlr_keyboard *wlr_keyboard =
seat_device->input_device->wlr_device->keyboard;
2017-12-14 16:11:56 +00:00
sway_keyboard_configure(seat_device->keyboard);
wlr_seat_set_keyboard(seat->wlr_seat,
seat_device->input_device->wlr_device);
2018-04-02 12:45:37 +00:00
struct sway_container *focus = seat_get_focus(seat);
if (focus && focus->type == C_VIEW) {
// force notify reenter to pick up the new configuration
wlr_seat_keyboard_clear_focus(seat->wlr_seat);
wlr_seat_keyboard_notify_enter(seat->wlr_seat,
focus->sway_view->surface, wlr_keyboard->keycodes,
wlr_keyboard->num_keycodes, &wlr_keyboard->modifiers);
}
2017-12-12 13:29:37 +00:00
}
2018-04-02 12:45:37 +00:00
static struct sway_seat_device *seat_get_device(struct sway_seat *seat,
2017-12-14 16:11:56 +00:00
struct sway_input_device *input_device) {
struct sway_seat_device *seat_device = NULL;
wl_list_for_each(seat_device, &seat->devices, link) {
if (seat_device->input_device == input_device) {
return seat_device;
}
}
return NULL;
2017-12-10 18:59:04 +00:00
}
2018-04-02 12:45:37 +00:00
void seat_configure_device(struct sway_seat *seat,
2017-12-14 16:11:56 +00:00
struct sway_input_device *input_device) {
struct sway_seat_device *seat_device =
2018-04-02 12:45:37 +00:00
seat_get_device(seat, input_device);
2017-12-14 16:11:56 +00:00
if (!seat_device) {
2017-12-12 13:29:37 +00:00
return;
}
2017-12-14 16:11:56 +00:00
switch (input_device->wlr_device->type) {
2017-12-08 12:22:26 +00:00
case WLR_INPUT_DEVICE_POINTER:
2017-12-14 16:11:56 +00:00
seat_configure_pointer(seat, seat_device);
2017-12-08 12:22:26 +00:00
break;
case WLR_INPUT_DEVICE_KEYBOARD:
2017-12-14 16:11:56 +00:00
seat_configure_keyboard(seat, seat_device);
2017-12-10 18:59:04 +00:00
break;
2017-12-08 12:22:26 +00:00
case WLR_INPUT_DEVICE_TOUCH:
case WLR_INPUT_DEVICE_TABLET_PAD:
case WLR_INPUT_DEVICE_TABLET_TOOL:
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "TODO: configure other devices");
2017-12-08 12:22:26 +00:00
break;
}
}
2018-04-02 12:45:37 +00:00
void seat_add_device(struct sway_seat *seat,
2017-12-14 16:11:56 +00:00
struct sway_input_device *input_device) {
2018-04-02 12:45:37 +00:00
if (seat_get_device(seat, input_device)) {
seat_configure_device(seat, input_device);
2017-12-14 16:11:56 +00:00
return;
2017-12-10 20:37:17 +00:00
}
2017-12-14 16:11:56 +00:00
struct sway_seat_device *seat_device =
calloc(1, sizeof(struct sway_seat_device));
if (!seat_device) {
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "could not allocate seat device");
2017-12-14 16:11:56 +00:00
return;
}
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "adding device %s to seat %s",
2017-12-16 18:16:58 +00:00
input_device->identifier, seat->wlr_seat->name);
2017-12-14 16:11:56 +00:00
seat_device->sway_seat = seat;
seat_device->input_device = input_device;
wl_list_insert(&seat->devices, &seat_device->link);
2018-04-02 12:45:37 +00:00
seat_configure_device(seat, input_device);
2017-12-07 14:58:32 +00:00
}
2018-04-02 12:45:37 +00:00
void seat_remove_device(struct sway_seat *seat,
2017-12-14 16:11:56 +00:00
struct sway_input_device *input_device) {
struct sway_seat_device *seat_device =
2018-04-02 12:45:37 +00:00
seat_get_device(seat, input_device);
2017-12-12 13:29:37 +00:00
2017-12-14 16:11:56 +00:00
if (!seat_device) {
return;
2017-12-08 12:22:26 +00:00
}
2017-12-12 13:29:37 +00:00
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "removing device %s from seat %s",
2017-12-16 18:16:58 +00:00
input_device->identifier, seat->wlr_seat->name);
2017-12-14 16:11:56 +00:00
seat_device_destroy(seat_device);
2017-12-07 14:58:32 +00:00
}
2017-12-09 16:51:28 +00:00
2018-04-02 12:45:37 +00:00
void seat_configure_xcursor(struct sway_seat *seat) {
2017-12-09 16:51:28 +00:00
// TODO configure theme and size
2018-03-31 04:13:26 +00:00
const char *cursor_theme = NULL;
2017-12-09 16:51:28 +00:00
2017-12-09 19:06:00 +00:00
if (!seat->cursor->xcursor_manager) {
seat->cursor->xcursor_manager =
2018-03-31 04:13:26 +00:00
wlr_xcursor_manager_create(cursor_theme, 24);
2017-12-09 19:06:00 +00:00
if (sway_assert(seat->cursor->xcursor_manager,
2017-12-14 16:11:56 +00:00
"Cannot create XCursor manager for theme %s",
cursor_theme)) {
2017-12-09 19:06:00 +00:00
return;
}
2017-12-09 16:51:28 +00:00
}
for (int i = 0; i < root_container.children->length; ++i) {
2018-04-02 15:44:42 +00:00
struct sway_container *output_container =
root_container.children->items[i];
2017-12-09 16:51:28 +00:00
struct wlr_output *output =
output_container->sway_output->wlr_output;
bool result =
wlr_xcursor_manager_load(seat->cursor->xcursor_manager,
output->scale);
2017-12-09 19:06:00 +00:00
sway_assert(!result,
2017-12-16 12:39:22 +00:00
"Cannot load xcursor theme for output '%s' with scale %f",
2017-12-18 02:00:17 +00:00
// TODO: Fractional scaling
output->name, (double)output->scale);
2017-12-09 16:51:28 +00:00
}
wlr_xcursor_manager_set_cursor_image(seat->cursor->xcursor_manager,
"left_ptr", seat->cursor->cursor);
wlr_cursor_warp(seat->cursor->cursor, NULL, seat->cursor->cursor->x,
seat->cursor->cursor->y);
}
2017-12-10 16:11:47 +00:00
2018-04-02 12:45:37 +00:00
void seat_set_focus_warp(struct sway_seat *seat,
struct sway_container *container, bool warp) {
if (seat->focused_layer) {
return;
}
2017-12-10 16:11:47 +00:00
struct sway_container *last_focus = seat_get_focus(seat);
2018-02-04 18:39:10 +00:00
if (container && last_focus == container) {
2017-12-10 16:11:47 +00:00
return;
}
2018-02-04 18:39:10 +00:00
if (container) {
struct sway_seat_container *seat_con =
seat_container_from_container(seat, container);
2018-02-10 23:10:29 +00:00
if (!seat_con) {
return;
}
2018-02-04 18:39:10 +00:00
wl_list_remove(&seat_con->link);
wl_list_insert(&seat->focus_stack, &seat_con->link);
if (container->type == C_VIEW) {
2018-03-31 19:22:10 +00:00
seat_send_focus(seat, container);
2018-01-17 16:47:27 +00:00
}
2017-12-10 16:11:47 +00:00
}
if (last_focus) {
struct sway_container *last_ws = last_focus;
if (last_ws && last_ws->type != C_WORKSPACE) {
2018-03-31 14:49:52 +00:00
last_ws = container_parent(last_ws, C_WORKSPACE);
}
if (last_ws) {
ipc_event_workspace(last_ws, container, "focus");
if (last_ws->children->length == 0) {
container_destroy(last_ws);
}
}
2018-03-31 14:49:52 +00:00
struct sway_container *last_output = last_focus;
if (last_output && last_output->type != C_OUTPUT) {
last_output = container_parent(last_output, C_OUTPUT);
}
struct sway_container *new_output = container;
if (new_output && new_output->type != C_OUTPUT) {
new_output = container_parent(new_output, C_OUTPUT);
}
if (new_output && last_output && new_output != last_output
&& config->mouse_warping && warp) {
2018-03-31 14:49:52 +00:00
struct wlr_output *output = new_output->sway_output->wlr_output;
double x = container->x + output->lx + container->width / 2.0;
double y = container->y + output->ly + container->height / 2.0;
2018-03-31 19:13:27 +00:00
if (!wlr_output_layout_contains_point(
root_container.sway_root->output_layout,
output, seat->cursor->cursor->x, seat->cursor->cursor->y)) {
wlr_cursor_warp(seat->cursor->cursor, NULL, x, y);
}
2018-03-31 14:49:52 +00:00
}
}
2018-01-31 04:09:21 +00:00
if (last_focus && last_focus->type == C_VIEW &&
2018-04-02 12:49:38 +00:00
!input_manager_has_focus(seat->input, last_focus)) {
2017-12-10 16:11:47 +00:00
struct sway_view *view = last_focus->sway_view;
2018-01-21 14:09:53 +00:00
view_set_activated(view, false);
2017-12-10 16:11:47 +00:00
}
2018-02-10 23:10:29 +00:00
seat->has_focus = (container != NULL);
2017-12-10 16:11:47 +00:00
}
2017-12-14 16:11:56 +00:00
2018-04-02 12:45:37 +00:00
void seat_set_focus(struct sway_seat *seat,
struct sway_container *container) {
2018-04-02 12:45:37 +00:00
seat_set_focus_warp(seat, container, true);
}
void seat_set_focus_layer(struct sway_seat *seat,
struct wlr_layer_surface *layer) {
if (!layer && seat->focused_layer) {
seat->focused_layer = NULL;
struct sway_container *c = seat_get_focus(seat);
if (c) {
wlr_log(L_DEBUG, "Returning focus to %p %s '%s'", c,
container_type_to_str(c->type), c->name);
// Hack to get seat to re-focus the return value of get_focus
seat_set_focus(seat, c->parent);
seat_set_focus(seat, c);
}
return;
} else if (!layer || seat->focused_layer == layer) {
return;
}
if (seat->has_focus) {
struct sway_container *focus = seat_get_focus(seat);
if (focus->type == C_VIEW) {
wlr_seat_keyboard_clear_focus(seat->wlr_seat);
view_set_activated(focus->sway_view, false);
}
}
if (layer->layer >= ZWLR_LAYER_SHELL_V1_LAYER_TOP) {
seat->focused_layer = layer;
}
struct wlr_keyboard *keyboard =
wlr_seat_get_keyboard(seat->wlr_seat);
if (keyboard) {
wlr_seat_keyboard_notify_enter(seat->wlr_seat,
layer->surface, keyboard->keycodes,
keyboard->num_keycodes, &keyboard->modifiers);
} else {
wlr_seat_keyboard_notify_enter(seat->wlr_seat,
layer->surface, NULL, 0, NULL);
}
}
2018-04-02 15:44:42 +00:00
struct sway_container *seat_get_focus_inactive(struct sway_seat *seat,
2018-03-31 19:22:10 +00:00
struct sway_container *container) {
return seat_get_focus_by_type(seat, container, C_TYPES);
2018-03-31 04:44:17 +00:00
}
struct sway_container *sway_seat_get_focus(struct sway_seat *seat) {
if (!seat->has_focus) {
return NULL;
}
return seat_get_focus_inactive(seat, &root_container);
2018-03-31 04:44:17 +00:00
}
struct sway_container *seat_get_focus_by_type(struct sway_seat *seat,
2018-03-31 04:44:17 +00:00
struct sway_container *container, enum sway_container_type type) {
2018-02-04 18:39:10 +00:00
struct sway_seat_container *current = NULL;
struct sway_container *parent = NULL;
2018-02-04 18:39:10 +00:00
wl_list_for_each(current, &seat->focus_stack, link) {
parent = current->container->parent;
2018-03-31 22:52:02 +00:00
if (current->container == container &&
(type == C_TYPES || container->type == type)) {
2018-02-04 18:39:10 +00:00
return current->container;
}
while (parent) {
2018-03-31 19:22:10 +00:00
if (parent == container && (type == C_TYPES ||
current->container->type == type)) {
2018-02-04 18:39:10 +00:00
return current->container;
}
parent = parent->parent;
}
}
return NULL;
}
2018-04-02 12:45:37 +00:00
struct sway_container *seat_get_focus(struct sway_seat *seat) {
if (!seat->has_focus) {
return NULL;
}
2018-04-02 12:45:37 +00:00
return seat_get_focus_inactive(seat, &root_container);
}
2017-12-14 16:11:56 +00:00
2018-04-02 14:37:31 +00:00
void seat_apply_config(struct sway_seat *seat,
2017-12-14 16:11:56 +00:00
struct seat_config *seat_config) {
struct sway_seat_device *seat_device = NULL;
if (!seat_config) {
return;
}
wl_list_for_each(seat_device, &seat->devices, link) {
2018-04-02 12:45:37 +00:00
seat_configure_device(seat, seat_device->input_device);
2017-12-14 16:11:56 +00:00
}
}
2018-04-02 14:37:31 +00:00
struct seat_config *seat_get_config(struct sway_seat *seat) {
struct seat_config *seat_config = NULL;
for (int i = 0; i < config->seat_configs->length; ++i ) {
seat_config = config->seat_configs->items[i];
if (strcmp(seat->wlr_seat->name, seat_config->name) == 0) {
return seat_config;
}
2017-12-14 16:11:56 +00:00
}
2018-04-02 14:37:31 +00:00
return NULL;
2017-12-14 16:11:56 +00:00
}