2017-09-23 18:53:15 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2017-09-23 22:18:19 +00:00
|
|
|
// TODO: BSD et al
|
|
|
|
#include <linux/input-event-codes.h>
|
2017-09-23 18:53:15 +00:00
|
|
|
#include <wayland-server.h>
|
|
|
|
#include <wlr/types/wlr_cursor.h>
|
2017-09-23 21:48:13 +00:00
|
|
|
#include <wlr/util/log.h>
|
2017-09-23 18:53:15 +00:00
|
|
|
#include "rootston/config.h"
|
|
|
|
#include "rootston/input.h"
|
|
|
|
#include "rootston/desktop.h"
|
2017-09-23 14:36:32 +00:00
|
|
|
|
|
|
|
void cursor_update_position(struct roots_input *input, uint32_t time) {
|
2017-09-23 21:52:21 +00:00
|
|
|
struct roots_desktop *desktop = input->server->desktop;
|
|
|
|
struct roots_view *view;
|
|
|
|
switch (input->mode) {
|
|
|
|
case ROOTS_CURSOR_PASSTHROUGH:
|
|
|
|
view = view_at(desktop, input->cursor->x, input->cursor->y);
|
|
|
|
if (view) {
|
|
|
|
struct wlr_box box;
|
|
|
|
view_get_input_bounds(view, &box);
|
|
|
|
double sx = input->cursor->x - view->x;
|
|
|
|
double sy = input->cursor->y - view->y;
|
|
|
|
wlr_seat_pointer_enter(input->wl_seat, view->wlr_surface, sx, sy);
|
|
|
|
wlr_seat_pointer_send_motion(input->wl_seat, time, sx, sy);
|
|
|
|
} else {
|
|
|
|
wlr_seat_pointer_clear_focus(input->wl_seat);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ROOTS_CURSOR_MOVE:
|
2017-09-23 22:18:19 +00:00
|
|
|
if (input->active_view) {
|
|
|
|
input->active_view->x = input->cursor->x - input->offs_x;
|
|
|
|
input->active_view->y = input->cursor->y - input->offs_y;
|
|
|
|
}
|
2017-09-23 21:52:21 +00:00
|
|
|
break;
|
|
|
|
case ROOTS_CURSOR_RESIZE:
|
|
|
|
break;
|
|
|
|
case ROOTS_CURSOR_ROTATE:
|
|
|
|
break;
|
|
|
|
}
|
2017-09-23 21:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_view_focus(struct roots_input *input,
|
|
|
|
struct roots_desktop *desktop, struct roots_view *view) {
|
|
|
|
if (input->active_view == view) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct roots_view *_view;
|
|
|
|
wl_list_for_each(_view, &desktop->views, link) {
|
|
|
|
view_activate(_view, _view == view);
|
|
|
|
}
|
|
|
|
input->active_view = view;
|
|
|
|
input->mode = ROOTS_CURSOR_PASSTHROUGH;
|
2017-09-23 18:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_cursor_motion(struct wl_listener *listener, void *data) {
|
|
|
|
struct roots_input *input = wl_container_of(listener, input, cursor_motion);
|
|
|
|
struct wlr_event_pointer_motion *event = data;
|
|
|
|
wlr_cursor_move(input->cursor, event->device,
|
|
|
|
event->delta_x, event->delta_y);
|
|
|
|
cursor_update_position(input, (uint32_t)event->time_usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_cursor_motion_absolute(struct wl_listener *listener,
|
|
|
|
void *data) {
|
|
|
|
struct roots_input *input = wl_container_of(listener,
|
|
|
|
input, cursor_motion_absolute);
|
|
|
|
struct wlr_event_pointer_motion_absolute *event = data;
|
|
|
|
wlr_cursor_warp_absolute(input->cursor, event->device,
|
|
|
|
event->x_mm / event->width_mm, event->y_mm / event->height_mm);
|
|
|
|
cursor_update_position(input, (uint32_t)event->time_usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_cursor_axis(struct wl_listener *listener, void *data) {
|
|
|
|
struct roots_input *input =
|
|
|
|
wl_container_of(listener, input, cursor_axis);
|
|
|
|
struct wlr_event_pointer_axis *event = data;
|
|
|
|
wlr_seat_pointer_send_axis(input->wl_seat, event->time_sec,
|
|
|
|
event->orientation, event->delta);
|
|
|
|
}
|
|
|
|
|
2017-09-23 22:18:19 +00:00
|
|
|
static void do_cursor_button_press(struct roots_input *input,
|
|
|
|
struct wlr_cursor *cursor, struct wlr_input_device *device,
|
|
|
|
uint32_t time, uint32_t button, uint32_t state) {
|
2017-09-23 21:48:13 +00:00
|
|
|
struct roots_desktop *desktop = input->server->desktop;
|
2017-09-23 22:18:19 +00:00
|
|
|
struct roots_view *view = view_at(desktop,
|
|
|
|
input->cursor->x, input->cursor->y);
|
|
|
|
uint32_t serial = wlr_seat_pointer_send_button(
|
|
|
|
input->wl_seat, time, button, state);
|
2017-09-23 18:53:15 +00:00
|
|
|
int i;
|
2017-09-23 22:18:19 +00:00
|
|
|
switch (state) {
|
2017-09-23 18:53:15 +00:00
|
|
|
case WLR_BUTTON_RELEASED:
|
2017-09-23 21:52:21 +00:00
|
|
|
input->active_view = NULL;
|
2017-09-23 22:18:19 +00:00
|
|
|
input->mode = ROOTS_CURSOR_PASSTHROUGH;
|
2017-09-23 18:53:15 +00:00
|
|
|
break;
|
|
|
|
case WLR_BUTTON_PRESSED:
|
2017-09-23 21:48:13 +00:00
|
|
|
i = input->input_events_idx;
|
|
|
|
input->input_events[i].serial = serial;
|
2017-09-23 22:18:19 +00:00
|
|
|
input->input_events[i].cursor = cursor;
|
|
|
|
input->input_events[i].device = device;
|
2017-09-23 21:48:13 +00:00
|
|
|
input->input_events_idx = (i + 1)
|
|
|
|
% (sizeof(input->input_events) / sizeof(input->input_events[0]));
|
|
|
|
set_view_focus(input, desktop, view);
|
2017-09-23 18:53:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 22:18:19 +00:00
|
|
|
static void handle_cursor_button(struct wl_listener *listener, void *data) {
|
|
|
|
struct roots_input *input = wl_container_of(listener, input, cursor_button);
|
|
|
|
struct wlr_event_pointer_button *event = data;
|
|
|
|
do_cursor_button_press(input, input->cursor, event->device,
|
|
|
|
(uint32_t)event->time_usec, event->button, event->state);
|
|
|
|
}
|
|
|
|
|
2017-09-23 18:53:15 +00:00
|
|
|
static void handle_tool_axis(struct wl_listener *listener, void *data) {
|
|
|
|
struct roots_input *input = wl_container_of(listener, input, cursor_tool_axis);
|
|
|
|
struct wlr_event_tablet_tool_axis *event = data;
|
|
|
|
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_X) &&
|
|
|
|
(event->updated_axes & WLR_TABLET_TOOL_AXIS_Y)) {
|
|
|
|
wlr_cursor_warp_absolute(input->cursor, event->device,
|
|
|
|
event->x_mm / event->width_mm, event->y_mm / event->height_mm);
|
|
|
|
cursor_update_position(input, (uint32_t)event->time_usec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_tool_tip(struct wl_listener *listener, void *data) {
|
2017-09-23 22:18:19 +00:00
|
|
|
struct roots_input *input = wl_container_of(listener, input, cursor_tool_tip);
|
2017-09-23 18:53:15 +00:00
|
|
|
struct wlr_event_tablet_tool_tip *event = data;
|
2017-09-23 22:18:19 +00:00
|
|
|
do_cursor_button_press(input, input->cursor, event->device,
|
|
|
|
(uint32_t)event->time_usec, BTN_LEFT, event->state);
|
2017-09-23 18:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cursor_initialize(struct roots_input *input) {
|
|
|
|
struct wlr_cursor *cursor = input->cursor;
|
|
|
|
|
|
|
|
wl_list_init(&input->cursor_motion.link);
|
|
|
|
wl_signal_add(&cursor->events.motion, &input->cursor_motion);
|
|
|
|
input->cursor_motion.notify = handle_cursor_motion;
|
|
|
|
|
|
|
|
wl_list_init(&input->cursor_motion_absolute.link);
|
|
|
|
wl_signal_add(&cursor->events.motion_absolute,
|
|
|
|
&input->cursor_motion_absolute);
|
|
|
|
input->cursor_motion_absolute.notify = handle_cursor_motion_absolute;
|
|
|
|
|
|
|
|
wl_list_init(&input->cursor_button.link);
|
|
|
|
wl_signal_add(&cursor->events.button, &input->cursor_button);
|
|
|
|
input->cursor_button.notify = handle_cursor_button;
|
|
|
|
|
|
|
|
wl_list_init(&input->cursor_axis.link);
|
|
|
|
wl_signal_add(&cursor->events.axis, &input->cursor_axis);
|
|
|
|
input->cursor_axis.notify = handle_cursor_axis;
|
|
|
|
|
|
|
|
wl_list_init(&input->cursor_tool_axis.link);
|
|
|
|
wl_signal_add(&cursor->events.tablet_tool_axis, &input->cursor_tool_axis);
|
|
|
|
input->cursor_tool_axis.notify = handle_tool_axis;
|
|
|
|
|
|
|
|
wl_list_init(&input->cursor_tool_tip.link);
|
|
|
|
wl_signal_add(&cursor->events.tablet_tool_tip, &input->cursor_tool_tip);
|
|
|
|
input->cursor_tool_tip.notify = handle_tool_tip;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset_device_mappings(struct roots_config *config,
|
|
|
|
struct wlr_cursor *cursor, struct wlr_input_device *device) {
|
|
|
|
wlr_cursor_map_input_to_output(cursor, device, NULL);
|
|
|
|
struct device_config *dconfig;
|
|
|
|
if ((dconfig = config_get_device(config, device))) {
|
|
|
|
wlr_cursor_map_input_to_region(cursor, device, dconfig->mapped_box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_device_output_mappings(struct roots_config *config,
|
|
|
|
struct wlr_cursor *cursor, struct wlr_output *output,
|
|
|
|
struct wlr_input_device *device) {
|
|
|
|
struct device_config *dconfig;
|
|
|
|
dconfig = config_get_device(config, device);
|
|
|
|
if (dconfig && dconfig->mapped_output &&
|
|
|
|
strcmp(dconfig->mapped_output, output->name) == 0) {
|
|
|
|
wlr_cursor_map_input_to_output(cursor, device, output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cursor_load_config(struct roots_config *config,
|
|
|
|
struct wlr_cursor *cursor,
|
|
|
|
struct roots_input *input,
|
|
|
|
struct roots_desktop *desktop) {
|
|
|
|
struct roots_pointer *pointer;
|
|
|
|
struct roots_touch *touch;
|
|
|
|
struct roots_tablet_tool *tablet_tool;
|
|
|
|
struct roots_output *output;
|
|
|
|
|
|
|
|
// reset mappings
|
|
|
|
wlr_cursor_map_to_output(cursor, NULL);
|
|
|
|
wl_list_for_each(pointer, &input->pointers, link) {
|
|
|
|
reset_device_mappings(config, cursor, pointer->device);
|
|
|
|
}
|
|
|
|
wl_list_for_each(touch, &input->touch, link) {
|
|
|
|
reset_device_mappings(config, cursor, touch->device);
|
|
|
|
}
|
|
|
|
wl_list_for_each(tablet_tool, &input->tablet_tools, link) {
|
|
|
|
reset_device_mappings(config, cursor, tablet_tool->device);
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure device to output mappings
|
|
|
|
const char *mapped_output = config->cursor.mapped_output;
|
|
|
|
wl_list_for_each(output, &desktop->outputs, link) {
|
|
|
|
if (mapped_output && strcmp(mapped_output, output->wlr_output->name) == 0) {
|
|
|
|
wlr_cursor_map_to_output(cursor, output->wlr_output);
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_list_for_each(pointer, &input->pointers, link) {
|
|
|
|
set_device_output_mappings(config, cursor, output->wlr_output,
|
|
|
|
pointer->device);
|
|
|
|
}
|
|
|
|
wl_list_for_each(tablet_tool, &input->tablet_tools, link) {
|
|
|
|
set_device_output_mappings(config, cursor, output->wlr_output,
|
|
|
|
tablet_tool->device);
|
|
|
|
}
|
|
|
|
wl_list_for_each(touch, &input->touch, link) {
|
|
|
|
set_device_output_mappings(config, cursor, output->wlr_output,
|
|
|
|
touch->device);
|
|
|
|
}
|
|
|
|
}
|
2017-09-23 14:36:32 +00:00
|
|
|
}
|