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>
|
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-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-10 17:11:47 +01:00
|
|
|
seat->seat = wlr_seat_create(input->server->wl_display, seat_name);
|
2017-12-08 13:22:26 +01:00
|
|
|
if (!sway_assert(seat->seat, "could not allocate seat")) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
seat->cursor = sway_cursor_create(seat);
|
|
|
|
if (!seat->cursor) {
|
|
|
|
wlr_seat_destroy(seat->seat);
|
|
|
|
free(seat);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-10 17:11:47 +01:00
|
|
|
seat->input = input;
|
2017-12-12 14:29:37 +01:00
|
|
|
seat->devices = create_list();
|
2017-12-10 17:11:47 +01:00
|
|
|
|
2017-12-08 13:22:26 +01:00
|
|
|
wlr_seat_set_capabilities(seat->seat,
|
|
|
|
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-08 13:22:26 +01:00
|
|
|
static void seat_add_pointer(struct sway_seat *seat,
|
2017-12-12 14:29:37 +01:00
|
|
|
struct sway_input_device *sway_device) {
|
2017-12-08 13:22:26 +01:00
|
|
|
// TODO pointer configuration
|
2017-12-12 14:29:37 +01:00
|
|
|
wlr_cursor_attach_input_device(seat->cursor->cursor,
|
|
|
|
sway_device->wlr_device);
|
2017-12-08 13:22:26 +01:00
|
|
|
}
|
|
|
|
|
2017-12-10 19:59:04 +01:00
|
|
|
static void seat_add_keyboard(struct sway_seat *seat,
|
2017-12-12 14:29:37 +01:00
|
|
|
struct sway_input_device *device) {
|
2017-12-10 21:37:17 +01:00
|
|
|
// TODO keyboard configuration
|
|
|
|
sway_keyboard_create(seat, device);
|
2017-12-12 14:29:37 +01:00
|
|
|
wlr_seat_set_keyboard(seat->seat, device->wlr_device);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sway_seat_has_device(struct sway_seat *seat,
|
|
|
|
struct sway_input_device *device) {
|
|
|
|
return false;
|
2017-12-10 19:59:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 15:58:32 +01:00
|
|
|
void sway_seat_add_device(struct sway_seat *seat,
|
2017-12-12 14:29:37 +01:00
|
|
|
struct sway_input_device *device) {
|
|
|
|
if (sway_seat_has_device(seat, device)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sway_log(L_DEBUG, "input add: %s", device->identifier);
|
|
|
|
switch (device->wlr_device->type) {
|
2017-12-08 13:22:26 +01:00
|
|
|
case WLR_INPUT_DEVICE_POINTER:
|
|
|
|
seat_add_pointer(seat, device);
|
|
|
|
break;
|
|
|
|
case WLR_INPUT_DEVICE_KEYBOARD:
|
2017-12-10 19:59:04 +01:00
|
|
|
seat_add_keyboard(seat, device);
|
|
|
|
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:
|
|
|
|
sway_log(L_DEBUG, "TODO: add other devices");
|
|
|
|
break;
|
|
|
|
}
|
2017-12-12 14:29:37 +01:00
|
|
|
|
|
|
|
list_add(seat->devices, device);
|
2017-12-08 13:22:26 +01:00
|
|
|
}
|
|
|
|
|
2017-12-10 21:37:17 +01:00
|
|
|
static void seat_remove_keyboard(struct sway_seat *seat,
|
2017-12-12 14:29:37 +01:00
|
|
|
struct sway_input_device *device) {
|
|
|
|
if (device && device->keyboard) {
|
|
|
|
sway_keyboard_destroy(device->keyboard);
|
2017-12-10 21:37:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 13:22:26 +01:00
|
|
|
static void seat_remove_pointer(struct sway_seat *seat,
|
2017-12-12 14:29:37 +01:00
|
|
|
struct sway_input_device *device) {
|
|
|
|
wlr_cursor_detach_input_device(seat->cursor->cursor, device->wlr_device);
|
2017-12-07 15:58:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void sway_seat_remove_device(struct sway_seat *seat,
|
2017-12-12 14:29:37 +01:00
|
|
|
struct sway_input_device *device) {
|
|
|
|
sway_log(L_DEBUG, "input remove: %s", device->identifier);
|
|
|
|
if (!sway_seat_has_device(seat, device)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (device->wlr_device->type) {
|
2017-12-08 13:22:26 +01:00
|
|
|
case WLR_INPUT_DEVICE_POINTER:
|
|
|
|
seat_remove_pointer(seat, device);
|
|
|
|
break;
|
|
|
|
case WLR_INPUT_DEVICE_KEYBOARD:
|
2017-12-10 21:37:17 +01:00
|
|
|
seat_remove_keyboard(seat, device);
|
|
|
|
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:
|
|
|
|
sway_log(L_DEBUG, "TODO: remove other devices");
|
|
|
|
break;
|
|
|
|
}
|
2017-12-12 14:29:37 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < seat->devices->length; ++i) {
|
|
|
|
if (seat->devices->items[i] == device) {
|
|
|
|
list_del(seat->devices, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
"Cannot create XCursor manager for theme %s", cursor_theme)) {
|
|
|
|
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-09 17:51:28 +01:00
|
|
|
"Cannot load xcursor theme for output '%s' with scale %d",
|
|
|
|
output->name, output->scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
//swayc_t *container = data;
|
|
|
|
|
|
|
|
// TODO set new focus based on the state of the tree
|
|
|
|
sway_seat_set_focus(seat, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) {
|
|
|
|
swayc_t *last_focus = seat->focus;
|
|
|
|
|
|
|
|
if (last_focus == container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_focus) {
|
|
|
|
wl_list_remove(&seat->focus_destroy.link);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (container) {
|
|
|
|
struct sway_view *view = container->sway_view;
|
|
|
|
view->iface.set_activated(view, true);
|
|
|
|
wl_signal_add(&container->events.destroy, &seat->focus_destroy);
|
|
|
|
seat->focus_destroy.notify = handle_focus_destroy;
|
2017-12-10 19:59:04 +01:00
|
|
|
wlr_seat_keyboard_notify_enter(seat->seat, view->surface);
|
2017-12-10 17:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
seat->focus = container;
|
|
|
|
|
|
|
|
if (last_focus &&
|
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;
|
|
|
|
view->iface.set_activated(view, false);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|