sway-patched-tray-menu/sway/input/seat.c

369 lines
10 KiB
C
Raw Normal View History

2017-12-07 15:58:32 +01:00
#define _XOPEN_SOURCE 700
2017-12-08 13:22:26 +01:00
#include <wlr/types/wlr_cursor.h>
2017-12-09 17:51:28 +01:00
#include <wlr/types/wlr_xcursor_manager.h>
2018-01-31 05:09:21 +01:00
#include "sway/container.h"
2017-12-08 14:07:47 +01:00
#include "sway/input/seat.h"
#include "sway/input/cursor.h"
#include "sway/input/input-manager.h"
2017-12-10 19:59:04 +01:00
#include "sway/input/keyboard.h"
2017-12-09 17:51:28 +01:00
#include "sway/output.h"
2017-12-10 17:11:47 +01:00
#include "sway/view.h"
2017-12-07 15:58:32 +01:00
#include "log.h"
2017-12-14 17:11:56 +01: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);
}
void sway_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-02-04 19:39:10 +01: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);
wl_list_remove(&seat_con->link);
wl_list_remove(&seat_con->destroy.link);
free(seat_con);
}
static struct sway_seat_container *seat_container_from_container(
struct sway_seat *seat, swayc_t *con) {
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;
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);
swayc_t *con = data;
seat_container_from_container(seat, con);
}
2017-12-10 17:11:47 +01:00
struct sway_seat *sway_seat_create(struct sway_input_manager *input,
2017-12-07 15:58:32 +01:00
const char *seat_name) {
struct sway_seat *seat = calloc(1, sizeof(struct sway_seat));
if (!seat) {
return NULL;
}
2017-12-08 13:22:26 +01:00
2017-12-14 17:11:56 +01: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 12:12:08 +01:00
free(seat);
2017-12-08 13:22:26 +01:00
return NULL;
}
seat->cursor = sway_cursor_create(seat);
if (!seat->cursor) {
2017-12-14 17:11:56 +01:00
wlr_seat_destroy(seat->wlr_seat);
2017-12-08 13:22:26 +01:00
free(seat);
return NULL;
}
2018-02-04 19:39:10 +01:00
// init the focus stack
wl_list_init(&seat->focus_stack);
list_t *containers = container_list_children(&root_container);
if (containers == NULL) {
wlr_seat_destroy(seat->wlr_seat);
free(seat);
return NULL;
}
for (int i = containers->length - 1; i >= 0; --i) {
swayc_t *con = containers->items[i];
seat_container_from_container(seat, con);
}
free(containers);
wl_signal_add(&root_container.sway_root->events.new_container,
&seat->new_container);
seat->new_container.notify = handle_new_container;
2017-12-10 17:11:47 +01:00
seat->input = input;
2017-12-14 17:11:56 +01:00
wl_list_init(&seat->devices);
2017-12-10 17:11:47 +01:00
2017-12-14 17:11:56 +01:00
wlr_seat_set_capabilities(seat->wlr_seat,
2017-12-08 13:22:26 +01:00
WL_SEAT_CAPABILITY_KEYBOARD |
WL_SEAT_CAPABILITY_POINTER |
WL_SEAT_CAPABILITY_TOUCH);
2017-12-09 17:51:28 +01:00
sway_seat_configure_xcursor(seat);
2017-12-12 14:29:37 +01:00
wl_list_insert(&input->seats, &seat->link);
2017-12-10 19:59:04 +01:00
2017-12-07 15:58:32 +01:00
return seat;
}
2017-12-14 17:11:56 +01:00
static void seat_configure_pointer(struct sway_seat *seat,
struct sway_seat_device *sway_device) {
2017-12-12 14:29:37 +01:00
wlr_cursor_attach_input_device(seat->cursor->cursor,
2017-12-14 17:11:56 +01:00
sway_device->input_device->wlr_device);
2017-12-08 13:22:26 +01:00
}
2017-12-14 17:11:56 +01: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-01-17 17:47:27 +01:00
struct wlr_keyboard *wlr_keyboard = seat_device->input_device->wlr_device->keyboard;
2017-12-14 17:11:56 +01:00
sway_keyboard_configure(seat_device->keyboard);
wlr_seat_set_keyboard(seat->wlr_seat,
seat_device->input_device->wlr_device);
swayc_t *focus = sway_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 14:29:37 +01:00
}
2017-12-14 17:11:56 +01:00
static struct sway_seat_device *sway_seat_get_device(struct sway_seat *seat,
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 19:59:04 +01:00
}
2017-12-14 17:11:56 +01:00
void sway_seat_configure_device(struct sway_seat *seat,
struct sway_input_device *input_device) {
struct sway_seat_device *seat_device =
sway_seat_get_device(seat, input_device);
if (!seat_device) {
2017-12-12 14:29:37 +01:00
return;
}
2017-12-14 17:11:56 +01:00
if (seat->config) {
seat_device->attachment_config =
seat_config_get_attachment(seat->config, input_device->identifier);
}
switch (input_device->wlr_device->type) {
2017-12-08 13:22:26 +01:00
case WLR_INPUT_DEVICE_POINTER:
2017-12-14 17:11:56 +01:00
seat_configure_pointer(seat, seat_device);
2017-12-08 13:22:26 +01:00
break;
case WLR_INPUT_DEVICE_KEYBOARD:
2017-12-14 17:11:56 +01:00
seat_configure_keyboard(seat, seat_device);
2017-12-10 19:59:04 +01:00
break;
2017-12-08 13:22:26 +01:00
case WLR_INPUT_DEVICE_TOUCH:
case WLR_INPUT_DEVICE_TABLET_PAD:
case WLR_INPUT_DEVICE_TABLET_TOOL:
2018-01-05 22:32:51 +01:00
wlr_log(L_DEBUG, "TODO: configure other devices");
2017-12-08 13:22:26 +01:00
break;
}
}
2017-12-14 17:11:56 +01:00
void sway_seat_add_device(struct sway_seat *seat,
struct sway_input_device *input_device) {
if (sway_seat_get_device(seat, input_device)) {
2017-12-17 16:39:22 +01:00
sway_seat_configure_device(seat, input_device);
2017-12-14 17:11:56 +01:00
return;
2017-12-10 21:37:17 +01:00
}
2017-12-14 17:11:56 +01:00
struct sway_seat_device *seat_device =
calloc(1, sizeof(struct sway_seat_device));
if (!seat_device) {
2018-01-05 22:32:51 +01:00
wlr_log(L_DEBUG, "could not allocate seat device");
2017-12-14 17:11:56 +01:00
return;
}
2018-01-05 22:32:51 +01:00
wlr_log(L_DEBUG, "adding device %s to seat %s",
2017-12-16 19:16:58 +01:00
input_device->identifier, seat->wlr_seat->name);
2017-12-14 17:11:56 +01:00
seat_device->sway_seat = seat;
seat_device->input_device = input_device;
wl_list_insert(&seat->devices, &seat_device->link);
sway_seat_configure_device(seat, input_device);
2017-12-07 15:58:32 +01:00
}
void sway_seat_remove_device(struct sway_seat *seat,
2017-12-14 17:11:56 +01:00
struct sway_input_device *input_device) {
struct sway_seat_device *seat_device =
sway_seat_get_device(seat, input_device);
2017-12-12 14:29:37 +01:00
2017-12-14 17:11:56 +01:00
if (!seat_device) {
return;
2017-12-08 13:22:26 +01:00
}
2017-12-12 14:29:37 +01:00
2018-01-05 22:32:51 +01:00
wlr_log(L_DEBUG, "removing device %s from seat %s",
2017-12-16 19:16:58 +01:00
input_device->identifier, seat->wlr_seat->name);
2017-12-14 17:11:56 +01:00
seat_device_destroy(seat_device);
2017-12-07 15:58:32 +01:00
}
2017-12-09 17:51:28 +01:00
void sway_seat_configure_xcursor(struct sway_seat *seat) {
// TODO configure theme and size
const char *cursor_theme = "default";
2017-12-09 20:06:00 +01:00
if (!seat->cursor->xcursor_manager) {
seat->cursor->xcursor_manager =
wlr_xcursor_manager_create("default", 24);
if (sway_assert(seat->cursor->xcursor_manager,
2017-12-14 17:11:56 +01:00
"Cannot create XCursor manager for theme %s",
cursor_theme)) {
2017-12-09 20:06:00 +01:00
return;
}
2017-12-09 17:51:28 +01:00
}
for (int i = 0; i < root_container.children->length; ++i) {
swayc_t *output_container = root_container.children->items[i];
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 20:06:00 +01:00
sway_assert(!result,
2017-12-16 13:39:22 +01:00
"Cannot load xcursor theme for output '%s' with scale %f",
2017-12-18 03:00:17 +01:00
// TODO: Fractional scaling
output->name, (double)output->scale);
2017-12-09 17:51:28 +01: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 17:11:47 +01:00
static void handle_focus_destroy(struct wl_listener *listener, void *data) {
struct sway_seat *seat = wl_container_of(listener, seat, focus_destroy);
2018-01-31 05:09:21 +01:00
swayc_t *container = data;
// TODO dont set focus to the parent, set focus to the next focus inactive
// of the parent
2018-01-31 05:09:21 +01:00
sway_seat_set_focus(seat, container->parent);
2017-12-10 17:11:47 +01:00
}
void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) {
swayc_t *last_focus = sway_seat_get_focus(seat);
2017-12-10 17:11:47 +01:00
2018-02-04 19:39:10 +01:00
if (container && last_focus == container) {
2017-12-10 17:11:47 +01:00
return;
}
2018-02-04 19:39:10 +01:00
if (last_focus) {
2017-12-10 17:11:47 +01:00
wl_list_remove(&seat->focus_destroy.link);
2018-02-04 19:39:10 +01:00
seat->has_focus = false;
2017-12-10 17:11:47 +01:00
}
2018-02-04 19:39:10 +01:00
if (container) {
struct sway_seat_container *seat_con =
seat_container_from_container(seat, container);
2017-12-10 17:11:47 +01:00
wl_signal_add(&container->events.destroy, &seat->focus_destroy);
seat->focus_destroy.notify = handle_focus_destroy;
2018-02-04 19:39:10 +01:00
seat->has_focus = true;
wl_list_remove(&seat_con->link);
wl_list_insert(&seat->focus_stack, &seat_con->link);
if (container->type == C_VIEW) {
struct sway_view *view = container->sway_view;
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-01-17 17:47:27 +01:00
}
2017-12-10 17:11:47 +01:00
}
2018-01-31 05:09:21 +01:00
if (last_focus && last_focus->type == C_VIEW &&
2017-12-12 14:29:37 +01:00
!sway_input_manager_has_focus(seat->input, last_focus)) {
2017-12-10 17:11:47 +01:00
struct sway_view *view = last_focus->sway_view;
2018-01-21 15:09:53 +01:00
view_set_activated(view, false);
2017-12-10 17:11:47 +01:00
}
}
2017-12-14 17:11:56 +01:00
swayc_t *sway_seat_get_focus_inactive(struct sway_seat *seat, swayc_t *container) {
2018-02-04 19:39:10 +01:00
struct sway_seat_container *current = NULL;
swayc_t *parent = NULL;
wl_list_for_each(current, &seat->focus_stack, link) {
if (current->container->type < C_WORKSPACE) {
continue;
}
parent = current->container->parent;
if (current->container == container) {
return current->container;
}
while (parent) {
if (parent == container) {
return current->container;
}
parent = parent->parent;
}
}
return NULL;
}
swayc_t *sway_seat_get_focus(struct sway_seat *seat) {
if (!seat->has_focus) {
return NULL;
}
return sway_seat_get_focus_inactive(seat, &root_container);
}
2017-12-14 17:11:56 +01:00
void sway_seat_set_config(struct sway_seat *seat,
struct seat_config *seat_config) {
// clear configs
free_seat_config(seat->config);
2017-12-14 17:11:56 +01:00
seat->config = NULL;
struct sway_seat_device *seat_device = NULL;
wl_list_for_each(seat_device, &seat->devices, link) {
seat_device->attachment_config = NULL;
}
if (!seat_config) {
return;
}
// add configs
seat->config = copy_seat_config(seat_config);
2017-12-14 17:11:56 +01:00
wl_list_for_each(seat_device, &seat->devices, link) {
sway_seat_configure_device(seat, seat_device->input_device);
2017-12-14 17:11:56 +01:00
}
}