feat: add more protocols + touch support:

* add touch support (experimental)
* add support for wlr_cursor_shape_v1
* add support for wlr_relative_pointer_manager_v1
This commit is contained in:
DL
2025-07-29 15:33:19 +02:00
parent 0b738f4476
commit 0f31545ccd
11 changed files with 639 additions and 60 deletions

136
cursor.c
View File

@@ -1,11 +1,13 @@
#define _POSIX_C_SOURCE 200112L #define _POSIX_C_SOURCE 200112L
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include <wlr/types/wlr_cursor_shape_v1.h>
#include <assert.h> #include <assert.h>
#include "cursor.h" #include "cursor.h"
#include "view.h" #include "view.h"
#include "seat.h" #include "seat.h"
#include "node.h" #include "node.h"
#include "idle.h" #include "idle.h"
#include "touch.h"
void diyac_cursor_focus(struct diyac_server *server) void diyac_cursor_focus(struct diyac_server *server)
{ {
@@ -56,10 +58,10 @@ void diyac_reset_cursor_mode(struct diyac_server *server)
static void process_cursor_move(struct diyac_server *server, uint32_t time) static void process_cursor_move(struct diyac_server *server, uint32_t time)
{ {
(void) time; (void)time;
struct diyac_view *toplevel = server->grabbed_view; struct diyac_view *toplevel = server->grabbed_view;
/* Move the grabbed toplevel to the new position. */ /* Move the grabbed toplevel to the new position. */
if(!toplevel->output) if (!toplevel->output)
{ {
return; return;
} }
@@ -86,7 +88,7 @@ static void process_cursor_move(struct diyac_server *server, uint32_t time)
static void process_cursor_resize(struct diyac_server *server, uint32_t time) static void process_cursor_resize(struct diyac_server *server, uint32_t time)
{ {
(void) time; (void)time;
/* /*
* Resizing the grabbed toplevel can be a little bit complicated, because we * Resizing the grabbed toplevel can be a little bit complicated, because we
* could be resizing from any corner or edge. This not only resizes the * could be resizing from any corner or edge. This not only resizes the
@@ -98,7 +100,7 @@ static void process_cursor_resize(struct diyac_server *server, uint32_t time)
* size, then commit any movement that was prepared. * size, then commit any movement that was prepared.
*/ */
struct diyac_view *toplevel = server->grabbed_view; struct diyac_view *toplevel = server->grabbed_view;
if(toplevel->state.fullscreen) if (toplevel->state.fullscreen)
{ {
return; return;
} }
@@ -218,6 +220,11 @@ static void server_cursor_motion(struct wl_listener *listener, void *data)
wl_container_of(listener, seat, cursor_motion); wl_container_of(listener, seat, cursor_motion);
struct wlr_pointer_motion_event *event = data; struct wlr_pointer_motion_event *event = data;
diya_idle_manager_notify_activity(seat->wlr_seat); diya_idle_manager_notify_activity(seat->wlr_seat);
wlr_relative_pointer_manager_v1_send_relative_motion(
seat->server->relative_pointer_manager,
seat->wlr_seat, (uint64_t)event->time_msec * 1000,
event->delta_x, event->delta_y, event->unaccel_dx,
event->unaccel_dy);
/* The cursor doesn't move unless we tell it to. The cursor automatically /* The cursor doesn't move unless we tell it to. The cursor automatically
* handles constraining the motion to the output layout, as well as any * handles constraining the motion to the output layout, as well as any
* special configuration applied for the specific input device which * special configuration applied for the specific input device which
@@ -241,6 +248,16 @@ static void server_cursor_motion_absolute(
wl_container_of(listener, seat, cursor_motion_absolute); wl_container_of(listener, seat, cursor_motion_absolute);
diya_idle_manager_notify_activity(seat->wlr_seat); diya_idle_manager_notify_activity(seat->wlr_seat);
struct wlr_pointer_motion_absolute_event *event = data; struct wlr_pointer_motion_absolute_event *event = data;
double lx, ly;
wlr_cursor_absolute_to_layout_coords(seat->cursor,
&event->pointer->base, event->x, event->y, &lx, &ly);
double dx = lx - seat->cursor->x;
double dy = ly - seat->cursor->y;
wlr_relative_pointer_manager_v1_send_relative_motion(
seat->server->relative_pointer_manager,
seat->wlr_seat, (uint64_t)event->time_msec * 1000,
dx, dy, dx, dy);
wlr_cursor_warp_absolute(seat->cursor, &event->pointer->base, event->x, wlr_cursor_warp_absolute(seat->cursor, &event->pointer->base, event->x,
event->y); event->y);
process_cursor_motion(seat->server, event->time_msec); process_cursor_motion(seat->server, event->time_msec);
@@ -285,7 +302,7 @@ static void server_cursor_axis(struct wl_listener *listener, void *data)
static void server_cursor_frame(struct wl_listener *listener, void *data) static void server_cursor_frame(struct wl_listener *listener, void *data)
{ {
(void) data; (void)data;
/* This event is forwarded by the cursor when a pointer emits an frame /* This event is forwarded by the cursor when a pointer emits an frame
* event. Frame events are sent after regular pointer events to group * event. Frame events are sent after regular pointer events to group
* multiple events together. For instance, two axis events may happen at the * multiple events together. For instance, two axis events may happen at the
@@ -296,6 +313,90 @@ static void server_cursor_frame(struct wl_listener *listener, void *data)
wlr_seat_pointer_notify_frame(seat->wlr_seat); wlr_seat_pointer_notify_frame(seat->wlr_seat);
} }
static void request_set_shape_notify(struct wl_listener *listener, void *data)
{
struct wlr_cursor_shape_manager_v1_request_set_shape_event *event = data;
const char *shape_name = wlr_cursor_shape_v1_name(event->shape);
struct diyac_seat *seat = wl_container_of(listener, seat, request_set_shape);
struct wlr_seat_client *focused_client = seat->wlr_seat->pointer_state.focused_client;
/* Prevent setting a cursor image when moving or resizing */
if (seat->cursor_mode != DIYAC_CURSOR_PASSTHROUGH)
{
return;
}
/*
* Omit set shape when the current cursor is
* invisible, e.g. on touch input.
if (!seat->cursor_visible)
{
return;
}
*/
/*
* This can be sent by any client, so we check to make sure this one
* actually has pointer focus first.
*/
if (event->seat_client != focused_client)
{
wlr_log(WLR_INFO, "seat client %p != focused client %p",
event->seat_client, focused_client);
return;
}
wlr_log(WLR_DEBUG, "set xcursor to shape %s", shape_name);
wlr_cursor_set_xcursor(seat->cursor, seat->cursor_mgr, shape_name);
}
void diyac_cursor_emulate_move(struct diyac_seat *seat, struct wlr_input_device *device, double dx, double dy, uint32_t time_msec)
{
if (!dx && !dy)
{
wlr_log(WLR_DEBUG, "dropping useless cursor_emulate: %.10f,%.10f", dx, dy);
return;
}
wlr_relative_pointer_manager_v1_send_relative_motion(
seat->server->relative_pointer_manager,
seat->wlr_seat, (uint64_t)time_msec * 1000,
dx, dy, dx, dy);
wlr_cursor_move(seat->cursor, device, dx, dy);
process_cursor_motion(seat->server, time_msec);
}
void diyac_cursor_emulate_move_absolute(struct diyac_seat *seat, struct wlr_input_device *device, double x, double y, uint32_t time_msec)
{
double lx, ly;
wlr_cursor_absolute_to_layout_coords(seat->cursor, device, x, y, &lx, &ly);
double dx = lx - seat->cursor->x;
double dy = ly - seat->cursor->y;
diyac_cursor_emulate_move(seat, device, dx, dy, time_msec);
}
void diyac_cursor_emulate_button(struct diyac_seat *seat, uint32_t button, enum wl_pointer_button_state state, uint32_t time_msec)
{
switch (state)
{
case WL_POINTER_BUTTON_STATE_PRESSED:
wlr_seat_pointer_notify_button(seat->wlr_seat, time_msec, button, state);
diyac_cursor_focus(seat->server);
break;
case WL_POINTER_BUTTON_STATE_RELEASED:
wlr_seat_pointer_notify_button(seat->wlr_seat, time_msec, button, state);
diyac_reset_cursor_mode(seat->server);
break;
default:
break;
}
wlr_seat_pointer_notify_frame(seat->wlr_seat);
}
void diyac_init_cursor_manager(struct diyac_server *server) void diyac_init_cursor_manager(struct diyac_server *server)
{ {
/* /*
@@ -333,4 +434,29 @@ void diyac_init_cursor_manager(struct diyac_server *server)
wl_signal_add(&server->seat.cursor->events.axis, &server->seat.cursor_axis); wl_signal_add(&server->seat.cursor->events.axis, &server->seat.cursor_axis);
server->seat.cursor_frame.notify = server_cursor_frame; server->seat.cursor_frame.notify = server_cursor_frame;
wl_signal_add(&server->seat.cursor->events.frame, &server->seat.cursor_frame); wl_signal_add(&server->seat.cursor->events.frame, &server->seat.cursor_frame);
struct wlr_cursor_shape_manager_v1 *cursor_shape_manager =
wlr_cursor_shape_manager_v1_create(server->wl_display, 1);
if (cursor_shape_manager)
{
server->seat.request_set_shape.notify = request_set_shape_notify;
wl_signal_add(&cursor_shape_manager->events.request_set_shape,
&server->seat.request_set_shape);
}
diyac_touch_init(&server->seat);
}
void diyac_finish_cusor_manager(struct diyac_server *server)
{
wl_list_remove(&server->seat.cursor_motion.link);
wl_list_remove(&server->seat.cursor_motion_absolute.link);
wl_list_remove(&server->seat.cursor_button.link);
wl_list_remove(&server->seat.cursor_axis.link);
wl_list_remove(&server->seat.cursor_frame.link);
wl_list_remove(&server->seat.request_cursor.link);
wl_list_remove(&server->seat.request_set_shape.link);
wl_list_remove(&server->seat.request_set_selection.link);
wl_list_remove(&server->seat.request_set_primary_selection.link);
diyac_touch_drop(&server->seat);
} }

View File

@@ -6,4 +6,8 @@
void diyac_init_cursor_manager(struct diyac_server * server); void diyac_init_cursor_manager(struct diyac_server * server);
void diyac_reset_cursor_mode(struct diyac_server *server); void diyac_reset_cursor_mode(struct diyac_server *server);
void diyac_cursor_focus(struct diyac_server *server); void diyac_cursor_focus(struct diyac_server *server);
void diyac_finish_cusor_manager(struct diyac_server* server);
void diyac_cursor_emulate_move(struct diyac_seat *seat, struct wlr_input_device *device,double dx, double dy, uint32_t time_msec);
void diyac_cursor_emulate_move_absolute(struct diyac_seat *seat, struct wlr_input_device *device,double x, double y, uint32_t time_msec);
void diyac_cursor_emulate_button(struct diyac_seat *seat, uint32_t button, enum wl_pointer_button_state state, uint32_t time_msec);
#endif #endif

View File

@@ -181,6 +181,7 @@ int main(int argc, char *argv[])
wlr_single_pixel_buffer_manager_v1_create(server.wl_display); wlr_single_pixel_buffer_manager_v1_create(server.wl_display);
wlr_fractional_scale_manager_v1_create(server.wl_display, 1); wlr_fractional_scale_manager_v1_create(server.wl_display, 1);
diya_init_idle_manager(server.wl_display); diya_init_idle_manager(server.wl_display);
server.relative_pointer_manager = wlr_relative_pointer_manager_v1_create(server.wl_display);
/* Set up xdg-shell version 6 The xdg-shell is a Wayland protocol which is /* Set up xdg-shell version 6 The xdg-shell is a Wayland protocol which is
* used for application windows. For more detail on shells, refer to * used for application windows. For more detail on shells, refer to
@@ -277,6 +278,7 @@ int main(int argc, char *argv[])
{ {
wl_event_source_remove(server.proc_mon); wl_event_source_remove(server.proc_mon);
} }
diyac_finish_cusor_manager(&server);
wl_list_remove(&server.new_xdg_toplevel.link); wl_list_remove(&server.new_xdg_toplevel.link);
wl_list_remove(&server.new_layer_surface.link); wl_list_remove(&server.new_layer_surface.link);
wl_list_remove(&server.new_output.link); wl_list_remove(&server.new_output.link);

15
diyac.h
View File

@@ -20,6 +20,7 @@
#include <wlr/types/wlr_virtual_keyboard_v1.h> #include <wlr/types/wlr_virtual_keyboard_v1.h>
#include <wlr/types/wlr_output_power_management_v1.h> #include <wlr/types/wlr_output_power_management_v1.h>
#include <wlr/types/wlr_output_management_v1.h> #include <wlr/types/wlr_output_management_v1.h>
#include <wlr/types/wlr_relative_pointer_v1.h>
#define LAYER_TREE_SZ 4 #define LAYER_TREE_SZ 4
@@ -69,8 +70,19 @@ struct diyac_seat
struct wl_listener cursor_button; struct wl_listener cursor_button;
struct wl_listener cursor_axis; struct wl_listener cursor_axis;
struct wl_listener cursor_frame; struct wl_listener cursor_frame;
/*touch*/
struct wl_listener touch_down;
struct wl_listener touch_up;
struct wl_listener touch_motion;
struct wl_listener touch_frame;
struct wl_list touch_points; /* struct touch_point.link */
/*TODO: wlr_pointer_gestures_v1*/
struct wl_listener new_input; struct wl_listener new_input;
struct wl_listener request_cursor; struct wl_listener request_cursor;
struct wl_listener request_set_shape;
struct wl_listener request_set_selection; struct wl_listener request_set_selection;
struct wl_listener request_set_primary_selection; struct wl_listener request_set_primary_selection;
// struct wl_list keyboards; // struct wl_list keyboards;
@@ -160,7 +172,8 @@ struct diyac_server
struct wlr_output_power_manager_v1 *output_power_manager; struct wlr_output_power_manager_v1 *output_power_manager;
struct wl_listener output_power_manager_set_mode; struct wl_listener output_power_manager_set_mode;
struct wlr_relative_pointer_manager_v1 *relative_pointer_manager;
struct wlr_output_manager_v1 *output_manager; struct wlr_output_manager_v1 *output_manager;
struct wl_listener output_manager_test; struct wl_listener output_manager_test;
struct wl_listener output_manager_apply; struct wl_listener output_manager_apply;

View File

@@ -33,6 +33,9 @@ wayland_targets=[]
wl_protocols = [ wl_protocols = [
wl_protocol_dir / 'stable/xdg-shell/xdg-shell', wl_protocol_dir / 'stable/xdg-shell/xdg-shell',
wl_protocol_dir / 'staging/cursor-shape/cursor-shape-v1',
# we don't really support tablet v2, it is here because of curosr-shape-v1 depends on it
wl_protocol_dir / 'stable/tablet/tablet-v2',
'protocol/wlr-layer-shell-unstable-v1', 'protocol/wlr-layer-shell-unstable-v1',
'protocol/wlr-output-power-management-unstable-v1', 'protocol/wlr-output-power-management-unstable-v1',
] ]
@@ -63,6 +66,7 @@ src = [
'layer.c', 'layer.c',
'session.c', 'session.c',
'idle.c', 'idle.c',
'touch.c',
wayland_targets wayland_targets
] ]

19
node.c
View File

@@ -37,7 +37,7 @@ struct diyac_view *diyac_view_from_node(struct wlr_scene_node *wlr_scene_node)
return (struct diyac_view *)node_descriptor->data; return (struct diyac_view *)node_descriptor->data;
} }
struct diyac_node_descriptor * diyac_node_at(struct diyac_server* server, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy) struct diyac_node_descriptor *diyac_node_at(struct diyac_server *server, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy)
{ {
/* This returns the topmost node in the scene at the given layout coords. /* This returns the topmost node in the scene at the given layout coords.
* We only care about surface nodes as we are specifically looking for a * We only care about surface nodes as we are specifically looking for a
@@ -65,4 +65,21 @@ struct diyac_node_descriptor * diyac_node_at(struct diyac_server* server, double
tree = tree->node.parent; tree = tree->node.parent;
} }
return tree->node.data; return tree->node.data;
}
struct wlr_surface *diyac_wlr_surface_from_node(struct wlr_scene_node *node)
{
struct wlr_scene_buffer *buffer;
struct wlr_scene_surface *scene_surface;
if (node && node->type == WLR_SCENE_NODE_BUFFER)
{
buffer = wlr_scene_buffer_from_node(node);
scene_surface = wlr_scene_surface_try_from_buffer(buffer);
if (scene_surface)
{
return scene_surface->surface;
}
}
return NULL;
} }

1
node.h
View File

@@ -4,6 +4,7 @@
void diyac_node_descriptor_create(struct wlr_scene_node *scene_node,enum diyac_node_descriptor_type type, void *data); void diyac_node_descriptor_create(struct wlr_scene_node *scene_node,enum diyac_node_descriptor_type type, void *data);
struct diyac_layer_surface * diyac_layer_surface_from_node(struct wlr_scene_node *wlr_scene_node); struct diyac_layer_surface * diyac_layer_surface_from_node(struct wlr_scene_node *wlr_scene_node);
struct wlr_surface * diyac_wlr_surface_from_node(struct wlr_scene_node* node);
struct diyac_view *diyac_view_from_node(struct wlr_scene_node *wlr_scene_node); struct diyac_view *diyac_view_from_node(struct wlr_scene_node *wlr_scene_node);
struct diyac_node_descriptor * diyac_node_at(struct diyac_server* server, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy); struct diyac_node_descriptor * diyac_node_at(struct diyac_server* server, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy);
#endif #endif

324
seat.c
View File

@@ -4,34 +4,54 @@
#include <stdlib.h> #include <stdlib.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include <assert.h> #include <assert.h>
#include <strings.h>
#include <wlr/types/wlr_touch.h>
#include "seat.h" #include "seat.h"
#include "view.h" #include "view.h"
#include "output.h" #include "output.h"
#include "idle.h" #include "idle.h"
static void configure_keyboard(struct diyac_seat* seat, struct diyac_input* input, bool force) static struct wlr_output *output_by_name(struct diyac_server *server, const char *name)
{ {
(void) seat; assert(name);
struct diyac_output *output;
wl_list_for_each(output, &server->outputs, link)
{
if (!strcasecmp(output->wlr_output->name, name))
{
return output->wlr_output;
}
}
return NULL;
}
static void configure_keyboard(struct diyac_seat *seat, struct diyac_input *input, bool force)
{
(void)seat;
struct wlr_input_device *device = input->device; struct wlr_input_device *device = input->device;
assert(device->type == WLR_INPUT_DEVICE_KEYBOARD); assert(device->type == WLR_INPUT_DEVICE_KEYBOARD);
struct diyac_keyboard *keyboard = (struct diyac_keyboard *)input; struct diyac_keyboard *keyboard = (struct diyac_keyboard *)input;
struct wlr_keyboard *kb = wlr_keyboard_from_input_device(device); struct wlr_keyboard *kb = wlr_keyboard_from_input_device(device);
if(!keyboard->is_virtual || force) if (!keyboard->is_virtual || force)
{ {
/* /*
* Set layout based on environment variables XKB_DEFAULT_LAYOUT, * Set layout based on environment variables XKB_DEFAULT_LAYOUT,
* XKB_DEFAULT_OPTIONS, and friends. * XKB_DEFAULT_OPTIONS, and friends.
*/ */
struct xkb_rule_names rules = { 0 }; struct xkb_rule_names rules = {0};
struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
struct xkb_keymap *keymap = xkb_map_new_from_names(context, &rules,XKB_KEYMAP_COMPILE_NO_FLAGS); struct xkb_keymap *keymap = xkb_map_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
if (keymap) { if (keymap)
if (!wlr_keyboard_keymaps_match(kb->keymap, keymap)) { {
if (!wlr_keyboard_keymaps_match(kb->keymap, keymap))
{
wlr_keyboard_set_keymap(kb, keymap); wlr_keyboard_set_keymap(kb, keymap);
} }
xkb_keymap_unref(keymap); xkb_keymap_unref(keymap);
} else { }
else
{
wlr_log(WLR_ERROR, "Failed to create xkb keymap"); wlr_log(WLR_ERROR, "Failed to create xkb keymap");
} }
xkb_context_unref(context); xkb_context_unref(context);
@@ -39,11 +59,10 @@ static void configure_keyboard(struct diyac_seat* seat, struct diyac_input* inpu
wlr_keyboard_set_repeat_info(kb, 25, 600); wlr_keyboard_set_repeat_info(kb, 25, 600);
} }
static void keyboard_handle_modifiers( static void keyboard_handle_modifiers(
struct wl_listener *listener, void *data) struct wl_listener *listener, void *data)
{ {
(void) data; (void)data;
/* This event is raised when a modifier key, such as shift or alt, is /* This event is raised when a modifier key, such as shift or alt, is
* pressed. We simply communicate this to the client. */ * pressed. We simply communicate this to the client. */
struct diyac_keyboard *keyboard = struct diyac_keyboard *keyboard =
@@ -131,7 +150,7 @@ static void keyboard_handle_key(
static void input_handle_destroy(struct wl_listener *listener, void *data) static void input_handle_destroy(struct wl_listener *listener, void *data)
{ {
(void) data; (void)data;
/* This event is raised by the keyboard base wlr_input_device to signal /* This event is raised by the keyboard base wlr_input_device to signal
* the destruction of the wlr_keyboard. It will no longer receive events * the destruction of the wlr_keyboard. It will no longer receive events
* and should be destroyed. * and should be destroyed.
@@ -140,10 +159,10 @@ static void input_handle_destroy(struct wl_listener *listener, void *data)
wl_list_remove(&input->link); wl_list_remove(&input->link);
wl_list_remove(&input->destroy.link); wl_list_remove(&input->destroy.link);
if(input->device->type == WLR_INPUT_DEVICE_KEYBOARD) if (input->device->type == WLR_INPUT_DEVICE_KEYBOARD)
{ {
struct diyac_keyboard* keyboard = (struct diyac_keyboard*) input; struct diyac_keyboard *keyboard = (struct diyac_keyboard *)input;
if(keyboard->is_virtual) if (keyboard->is_virtual)
{ {
wlr_log(WLR_INFO, "Virtual keyboard destroyed"); wlr_log(WLR_INFO, "Virtual keyboard destroyed");
} }
@@ -153,8 +172,8 @@ static void input_handle_destroy(struct wl_listener *listener, void *data)
free(input); free(input);
} }
static struct diyac_input* server_new_keyboard(struct diyac_seat *seat, static struct diyac_input *server_new_keyboard(struct diyac_seat *seat,
struct wlr_input_device *device, bool is_virtual) struct wlr_input_device *device, bool is_virtual)
{ {
struct wlr_keyboard *wlr_keyboard = wlr_keyboard_from_input_device(device); struct wlr_keyboard *wlr_keyboard = wlr_keyboard_from_input_device(device);
@@ -168,36 +187,203 @@ static struct diyac_input* server_new_keyboard(struct diyac_seat *seat,
* Force configure default keyboard keymap for all new * Force configure default keyboard keymap for all new
* keyboard, including virtual keyboard * keyboard, including virtual keyboard
*/ */
configure_keyboard(seat,&keyboard->input, true); configure_keyboard(seat, &keyboard->input, true);
/* Here we set up listeners for keyboard events. */ /* Here we set up listeners for keyboard events. */
keyboard->modifiers.notify = keyboard_handle_modifiers; keyboard->modifiers.notify = keyboard_handle_modifiers;
wl_signal_add(&wlr_keyboard->events.modifiers, &keyboard->modifiers); wl_signal_add(&wlr_keyboard->events.modifiers, &keyboard->modifiers);
keyboard->key.notify = keyboard_handle_key; keyboard->key.notify = keyboard_handle_key;
wl_signal_add(&wlr_keyboard->events.key, &keyboard->key); wl_signal_add(&wlr_keyboard->events.key, &keyboard->key);
wlr_seat_set_keyboard(seat->wlr_seat, keyboard->wlr_keyboard); wlr_seat_set_keyboard(seat->wlr_seat, keyboard->wlr_keyboard);
/* And add the keyboard to our list of keyboards */ /* And add the keyboard to our list of keyboards */
//wl_list_insert(&server->seat.keyboards, &keyboard->link); // wl_list_insert(&server->seat.keyboards, &keyboard->link);
return (struct diyac_input*) keyboard; return (struct diyac_input *)keyboard;
} }
/*
static void configure_libinput(struct wlr_input_device *wlr_input_device)
{
if (!wlr_input_device) {
wlr_log(WLR_ERROR, "no wlr_input_device");
return;
}
struct input *input = wlr_input_device->data;
static struct diyac_input* server_new_pointer(struct diyac_seat *seat, if (!wlr_input_device_is_libinput(wlr_input_device)) {
struct wlr_input_device *device) input->scroll_factor = 1.0;
return;
}
struct libinput_device *libinput_dev =
wlr_libinput_get_device_handle(wlr_input_device);
if (!libinput_dev) {
wlr_log(WLR_ERROR, "no libinput_dev");
return;
}
struct libinput_category *dc = get_category(wlr_input_device);
assert(dc);
if (libinput_device_config_tap_get_finger_count(libinput_dev) <= 0) {
wlr_log(WLR_INFO, "tap unavailable");
} else {
wlr_log(WLR_INFO, "tap configured");
libinput_device_config_tap_set_enabled(libinput_dev, dc->tap);
libinput_device_config_tap_set_button_map(libinput_dev,
dc->tap_button_map);
}
if (libinput_device_config_tap_get_finger_count(libinput_dev) <= 0
|| dc->tap_and_drag < 0) {
wlr_log(WLR_INFO, "tap-and-drag not configured");
} else {
wlr_log(WLR_INFO, "tap-and-drag configured");
libinput_device_config_tap_set_drag_enabled(
libinput_dev, dc->tap_and_drag);
}
if (libinput_device_config_tap_get_finger_count(libinput_dev) <= 0
|| dc->drag_lock < 0) {
wlr_log(WLR_INFO, "drag lock not configured");
} else {
wlr_log(WLR_INFO, "drag lock configured");
libinput_device_config_tap_set_drag_lock_enabled(
libinput_dev, dc->drag_lock);
}
if (libinput_device_config_scroll_has_natural_scroll(libinput_dev) <= 0
|| dc->natural_scroll < 0) {
wlr_log(WLR_INFO, "natural scroll not configured");
} else {
wlr_log(WLR_INFO, "natural scroll configured");
libinput_device_config_scroll_set_natural_scroll_enabled(
libinput_dev, dc->natural_scroll);
}
if (libinput_device_config_left_handed_is_available(libinput_dev) <= 0
|| dc->left_handed < 0) {
wlr_log(WLR_INFO, "left-handed mode not configured");
} else {
wlr_log(WLR_INFO, "left-handed mode configured");
libinput_device_config_left_handed_set(libinput_dev,
dc->left_handed);
}
if (libinput_device_config_accel_is_available(libinput_dev) == 0) {
wlr_log(WLR_INFO, "pointer acceleration unavailable");
} else {
wlr_log(WLR_INFO, "pointer acceleration configured");
if (dc->pointer_speed >= -1) {
libinput_device_config_accel_set_speed(libinput_dev,
dc->pointer_speed);
}
if (dc->accel_profile > 0) {
libinput_device_config_accel_set_profile(libinput_dev,
dc->accel_profile);
}
}
if (libinput_device_config_middle_emulation_is_available(libinput_dev)
== 0 || dc->middle_emu < 0) {
wlr_log(WLR_INFO, "middle emulation not configured");
} else {
wlr_log(WLR_INFO, "middle emulation configured");
libinput_device_config_middle_emulation_set_enabled(
libinput_dev, dc->middle_emu);
}
if (libinput_device_config_dwt_is_available(libinput_dev) == 0
|| dc->dwt < 0) {
wlr_log(WLR_INFO, "dwt not configured");
} else {
wlr_log(WLR_INFO, "dwt configured");
libinput_device_config_dwt_set_enabled(libinput_dev, dc->dwt);
}
if ((dc->click_method != LIBINPUT_CONFIG_CLICK_METHOD_NONE
&& (libinput_device_config_click_get_methods(libinput_dev)
& dc->click_method) == 0)
|| dc->click_method < 0) {
wlr_log(WLR_INFO, "click method not configured");
} else {
wlr_log(WLR_INFO, "click method configured");
libinput_device_config_click_set_method(libinput_dev, dc->click_method);
}
if ((dc->send_events_mode != LIBINPUT_CONFIG_SEND_EVENTS_ENABLED
&& (libinput_device_config_send_events_get_modes(libinput_dev)
& dc->send_events_mode) == 0)
|| dc->send_events_mode < 0) {
wlr_log(WLR_INFO, "send events mode not configured");
} else {
wlr_log(WLR_INFO, "send events mode configured");
libinput_device_config_send_events_set_mode(libinput_dev, dc->send_events_mode);
}
if (libinput_device_config_calibration_has_matrix(libinput_dev) == 0
|| !dc->have_calibration_matrix) {
wlr_log(WLR_INFO, "calibration matrix not configured");
} else {
wlr_log(WLR_INFO, "calibration matrix configured");
libinput_device_config_calibration_set_matrix(libinput_dev, dc->calibration_matrix);
}
wlr_log(WLR_INFO, "scroll factor configured");
input->scroll_factor = dc->scroll_factor;
}
*/
static struct diyac_input *server_new_pointer(struct diyac_seat *seat, struct wlr_input_device *device)
{ {
struct diyac_input *input = calloc(1, sizeof(*input)); struct diyac_input *input = calloc(1, sizeof(*input));
input->device = device; input->device = device;
/* We don't do anything special with pointers. All of our pointer handling device->data = input;
* is proxied through wlr_cursor. On another compositor, you might take this /**
* opportunity to do libinput configuration on the device to set * @TODO:
* acceleration, etc. */ * configure pointer via libinput
*
*/
wlr_cursor_attach_input_device(seat->cursor, device); wlr_cursor_attach_input_device(seat->cursor, device);
struct wlr_pointer *pointer = wlr_pointer_from_input_device(device);
wlr_log(WLR_INFO, "map pointer to output %s", pointer->output_name);
struct wlr_output *output = NULL;
if (pointer->output_name)
{
output = output_by_name(seat->server, pointer->output_name);
}
wlr_cursor_map_input_to_output(seat->cursor, device, output);
wlr_cursor_map_input_to_region(seat->cursor, device, NULL);
return input; return input;
} }
static void seat_add_input(struct diyac_seat* seat, struct diyac_input* input) static struct diyac_input *server_new_touch(struct diyac_seat *seat, struct wlr_input_device *device)
{
struct diyac_input *input = calloc(1, sizeof(*input));
input->device = device;
device->data = input;
/**
* @TODO:
* configure touch device via libinput
*
*/
wlr_cursor_attach_input_device(seat->cursor, device);
struct wlr_touch *touch = wlr_touch_from_input_device(device);
wlr_log(WLR_INFO, "map pointer to output %s", touch->output_name);
struct wlr_output *output = NULL;
if (touch->output_name)
{
output = output_by_name(seat->server, touch->output_name);
}
wlr_cursor_map_input_to_output(seat->cursor, device, output);
wlr_cursor_map_input_to_region(seat->cursor, device, NULL);
return input;
}
static void seat_add_input(struct diyac_seat *seat, struct diyac_input *input)
{ {
input->seat = seat; input->seat = seat;
input->destroy.notify = input_handle_destroy; input->destroy.notify = input_handle_destroy;
@@ -210,8 +396,10 @@ static void seat_add_input(struct diyac_seat* seat, struct diyac_input* input)
* there are no pointer devices, so we always include that capability. */ * there are no pointer devices, so we always include that capability. */
uint32_t caps = 0; uint32_t caps = 0;
wl_list_for_each(input, &seat->inputs, link) { wl_list_for_each(input, &seat->inputs, link)
switch (input->device->type) { {
switch (input->device->type)
{
case WLR_INPUT_DEVICE_KEYBOARD: case WLR_INPUT_DEVICE_KEYBOARD:
caps |= WL_SEAT_CAPABILITY_KEYBOARD; caps |= WL_SEAT_CAPABILITY_KEYBOARD;
break; break;
@@ -235,7 +423,7 @@ static void server_new_input(struct wl_listener *listener, void *data)
struct diyac_seat *seat = struct diyac_seat *seat =
wl_container_of(listener, seat, new_input); wl_container_of(listener, seat, new_input);
struct wlr_input_device *device = data; struct wlr_input_device *device = data;
struct diyac_input* input = NULL; struct diyac_input *input = NULL;
switch (device->type) switch (device->type)
{ {
@@ -245,8 +433,10 @@ static void server_new_input(struct wl_listener *listener, void *data)
case WLR_INPUT_DEVICE_POINTER: case WLR_INPUT_DEVICE_POINTER:
input = server_new_pointer(seat, device); input = server_new_pointer(seat, device);
break; break;
default: case WLR_INPUT_DEVICE_TOUCH:
input = server_new_touch(seat, device);
break; break;
default:
return; return;
} }
seat_add_input(seat, input); seat_add_input(seat, input);
@@ -328,13 +518,17 @@ void diyac_init_seat(struct diyac_server *server)
server->seat.new_input.notify = server_new_input; server->seat.new_input.notify = server_new_input;
wl_signal_add(&server->backend->events.new_input, &server->seat.new_input); wl_signal_add(&server->backend->events.new_input, &server->seat.new_input);
server->seat.wlr_seat = wlr_seat_create(server->wl_display, "seat0"); server->seat.wlr_seat = wlr_seat_create(server->wl_display, "seat0");
wl_list_init(&server->seat.touch_points);
wl_list_init(&server->seat.inputs);
server->seat.request_cursor.notify = seat_request_cursor; server->seat.request_cursor.notify = seat_request_cursor;
wl_signal_add(&server->seat.wlr_seat->events.request_set_cursor, wl_signal_add(&server->seat.wlr_seat->events.request_set_cursor,
&server->seat.request_cursor); &server->seat.request_cursor);
server->seat.request_set_selection.notify = seat_request_set_selection; server->seat.request_set_selection.notify = seat_request_set_selection;
wl_signal_add(&server->seat.wlr_seat->events.request_set_selection, wl_signal_add(&server->seat.wlr_seat->events.request_set_selection,
&server->seat.request_set_selection); &server->seat.request_set_selection);
server->seat.request_set_selection.notify = request_set_selection_notify; server->seat.request_set_selection.notify = request_set_selection_notify;
wl_signal_add(&server->seat.wlr_seat->events.request_set_selection, wl_signal_add(&server->seat.wlr_seat->events.request_set_selection,
&server->seat.request_set_selection); &server->seat.request_set_selection);
@@ -346,7 +540,7 @@ void diyac_init_seat(struct diyac_server *server)
// virtual keyboard support // virtual keyboard support
server->seat.virtual_keyboard_manager = wlr_virtual_keyboard_manager_v1_create(server->wl_display); server->seat.virtual_keyboard_manager = wlr_virtual_keyboard_manager_v1_create(server->wl_display);
wl_signal_add(&server->seat.virtual_keyboard_manager->events.new_virtual_keyboard, wl_signal_add(&server->seat.virtual_keyboard_manager->events.new_virtual_keyboard,
&server->seat.virtual_keyboard_new); &server->seat.virtual_keyboard_new);
server->seat.virtual_keyboard_new.notify = new_virtual_keyboard; server->seat.virtual_keyboard_new.notify = new_virtual_keyboard;
} }
@@ -418,13 +612,13 @@ void diyac_seat_focus_layer(struct diyac_seat *seat, struct wlr_layer_surface_v1
return; return;
*/ */
} }
if(seat->focused_layer == layer) if (seat->focused_layer == layer)
{ {
return; return;
} }
if(seat->focused_layer) if (seat->focused_layer)
wlr_log(WLR_INFO,"Layer focus changed %s -> %s", seat->focused_layer->namespace, layer->namespace); wlr_log(WLR_INFO, "Layer focus changed %s -> %s", seat->focused_layer->namespace, layer->namespace);
// unfocus currently focus view // unfocus currently focus view
if (seat->server->active_view) if (seat->server->active_view)
{ {
@@ -440,20 +634,23 @@ void diyac_seat_focus_layer(struct diyac_seat *seat, struct wlr_layer_surface_v1
*/ */
} }
void diyac_seat_configure(struct diyac_server* server) void diyac_seat_configure(struct diyac_server *server)
{ {
struct diyac_seat* seat = &server->seat; struct diyac_seat *seat = &server->seat;
struct diyac_input* input = NULL; struct diyac_input *input = NULL;
wl_list_for_each(input, &seat->inputs, link) wl_list_for_each(input, &seat->inputs, link)
{ {
switch (input->device->type) { switch (input->device->type)
{
case WLR_INPUT_DEVICE_KEYBOARD: case WLR_INPUT_DEVICE_KEYBOARD:
configure_keyboard(seat, input, false); configure_keyboard(seat, input, false);
break; break;
case WLR_INPUT_DEVICE_POINTER: case WLR_INPUT_DEVICE_POINTER:
/*TODO: reconfigure libinput*/
break; break;
case WLR_INPUT_DEVICE_TOUCH: case WLR_INPUT_DEVICE_TOUCH:
/*TODO: reconfigure libinput*/
break; break;
default: default:
break; break;
@@ -461,31 +658,54 @@ void diyac_seat_configure(struct diyac_server* server)
} }
} }
void diyac_seat_focus_topmost(struct diyac_seat* seat, struct diyac_output* output) void diyac_seat_focus_topmost(struct diyac_seat *seat, struct diyac_output *output)
{ {
for(int i = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY; i >= ZWLR_LAYER_SHELL_V1_LAYER_TOP; i--) for (int i = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY; i >= ZWLR_LAYER_SHELL_V1_LAYER_TOP; i--)
{ {
struct wlr_scene_tree *scene = output->layer_tree[i]; struct wlr_scene_tree *scene = output->layer_tree[i];
struct wl_list *node_list = & scene->children; struct wl_list *node_list = &scene->children;
struct wlr_scene_node *node; struct wlr_scene_node *node;
wl_list_for_each_reverse(node,node_list, link) wl_list_for_each_reverse(node, node_list, link)
{ {
struct diyac_node_descriptor *node_descriptor = node->data; struct diyac_node_descriptor *node_descriptor = node->data;
if (!node->enabled || !node_descriptor || node_descriptor->type != DIYAC_NODE_LAYER_SURFACE) if (!node->enabled || !node_descriptor || node_descriptor->type != DIYAC_NODE_LAYER_SURFACE)
{ {
continue; continue;
} }
struct diyac_layer_surface* surface = (struct diyac_layer_surface *)node_descriptor->data; struct diyac_layer_surface *surface = (struct diyac_layer_surface *)node_descriptor->data;
struct wlr_layer_surface_v1* layer_surface = surface->scene_layer_surface->layer_surface; struct wlr_layer_surface_v1 *layer_surface = surface->scene_layer_surface->layer_surface;
if(!surface->mapped || layer_surface->current.keyboard_interactive == ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE) if (!surface->mapped || layer_surface->current.keyboard_interactive == ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE)
{ {
continue; continue;
} }
wlr_log(WLR_INFO," Update focus on layer: %s", layer_surface->namespace); wlr_log(WLR_INFO, " Update focus on layer: %s", layer_surface->namespace);
diyac_seat_focus_layer(seat, layer_surface); diyac_seat_focus_layer(seat, layer_surface);
return; return;
} }
} }
seat->focused_layer = NULL; seat->focused_layer = NULL;
diyac_focus_topmost_view(seat->server, true); diyac_focus_topmost_view(seat->server, true);
}
void diyac_seat_pointer_end_grab(struct diyac_seat *seat, struct wlr_surface *surface)
{
if (!surface || !wlr_seat_pointer_has_grab(seat->wlr_seat))
{
return;
}
struct wlr_xdg_surface *xdg_surface =
wlr_xdg_surface_try_from_wlr_surface(surface);
if (!xdg_surface || xdg_surface->role != WLR_XDG_SURFACE_ROLE_POPUP)
{
/*
* If we have an active popup grab (an open popup) and we are
* not on the popup itself, end that grab to close the popup.
* Contrary to pointer button notifications, a tablet/touch
* button notification sometimes doesn't end grabs automatically
* on button notifications in another client (observed in GTK4),
* so end the grab manually.
*/
wlr_seat_pointer_end_grab(seat->wlr_seat);
}
} }

2
seat.h
View File

@@ -7,6 +7,6 @@ void diyac_seat_focus_surface(struct diyac_seat *seat, struct wlr_surface *surfa
void diyac_seat_focus_lock_surface(struct diyac_seat *seat, struct wlr_surface *surface); void diyac_seat_focus_lock_surface(struct diyac_seat *seat, struct wlr_surface *surface);
void diyac_seat_focus_layer(struct diyac_seat *seat, struct wlr_layer_surface_v1 *layer); void diyac_seat_focus_layer(struct diyac_seat *seat, struct wlr_layer_surface_v1 *layer);
void diyac_seat_focus_topmost(struct diyac_seat* seat, struct diyac_output* output); void diyac_seat_focus_topmost(struct diyac_seat* seat, struct diyac_output* output);
void diyac_seat_pointer_end_grab(struct diyac_seat *seat, struct wlr_surface *surface);
void diyac_seat_configure(struct diyac_server* server); void diyac_seat_configure(struct diyac_server* server);
#endif #endif

184
touch.c Normal file
View File

@@ -0,0 +1,184 @@
#include <wayland-util.h>
#include <wlr/types/wlr_touch.h>
#include <linux/input-event-codes.h>
#include <stdlib.h>
#include "touch.h"
#include "idle.h"
#include "node.h"
#include "seat.h"
#include "cursor.h"
struct touch_point
{
int32_t touch_id;
uint32_t x_offset;
uint32_t y_offset;
struct wlr_surface *surface;
struct wl_list link; /* seat.touch_points */
};
static void seat_touch_down(struct wl_listener *listener, void *data)
{
struct diyac_seat *seat = wl_container_of(listener, seat, touch_down);
struct wlr_touch_down_event *event = data;
diya_idle_manager_notify_activity(seat->wlr_seat);
/* Compute layout => surface offset and save for this touch point */
struct touch_point *touch_point = calloc(1, sizeof(*touch_point));
double x_offset = 0.0, y_offset = 0.0;
// get surface from touch point
struct wlr_touch *touch = event->touch;
/* Convert coordinates: first [0, 1] => layout, then layout => surface */
double lx, ly;
wlr_cursor_absolute_to_layout_coords(seat->cursor, &touch->base, event->x, event->y, &lx, &ly);
double sx, sy;
struct wlr_scene_node *node = wlr_scene_node_at(&seat->server->scene->tree.node, lx, ly, &sx, &sy);
x_offset = lx - sx;
y_offset = ly - sy;
/* Find the surface and return it if it accepts touch events */
struct wlr_surface *surface = diyac_wlr_surface_from_node(node);
if (surface && !wlr_surface_accepts_touch(seat->wlr_seat, surface))
{
surface = NULL;
}
touch_point->surface = surface;
touch_point->touch_id = event->touch_id;
touch_point->x_offset = x_offset;
touch_point->y_offset = y_offset;
wl_list_insert(&seat->touch_points, &touch_point->link);
int touch_point_count = wl_list_length(&seat->touch_points);
/* hide the cursor when starting touch input */
// cursor_set_visible(seat, /* visible */ false);
if (touch_point->surface)
{
diyac_seat_pointer_end_grab(seat, touch_point->surface);
/* Clear focus to not interfere with touch input */
wlr_seat_pointer_notify_clear_focus(seat->wlr_seat);
if (touch_point_count == 1)
{
wlr_cursor_warp_absolute(seat->cursor, &event->touch->base, event->x, event->y);
}
wlr_seat_touch_notify_down(seat->wlr_seat, touch_point->surface, event->time_msec, event->touch_id, sx, sy);
}
else
{
if (touch_point_count == 1)
{
diyac_cursor_emulate_move_absolute(seat, &event->touch->base, event->x, event->y, event->time_msec);
}
diyac_cursor_emulate_button(seat, BTN_LEFT, WL_POINTER_BUTTON_STATE_PRESSED, event->time_msec);
}
}
static void seat_touch_up(struct wl_listener *listener, void *data)
{
struct diyac_seat *seat = wl_container_of(listener, seat, touch_up);
struct wlr_touch_up_event *event = data;
diya_idle_manager_notify_activity(seat->wlr_seat);
/* Remove the touch point from the seat */
struct touch_point *touch_point, *tmp;
wl_list_for_each_safe(touch_point, tmp, &seat->touch_points, link)
{
if (touch_point->touch_id == event->touch_id)
{
if (touch_point->surface)
{
wlr_seat_touch_notify_up(seat->wlr_seat, event->time_msec,
event->touch_id);
}
else
{
diyac_cursor_emulate_button(seat, BTN_LEFT, WL_POINTER_BUTTON_STATE_RELEASED, event->time_msec);
}
wl_list_remove(&touch_point->link);
free(touch_point);
break;
}
}
}
static void seat_touch_motion(struct wl_listener *listener, void *data)
{
struct diyac_seat *seat = wl_container_of(listener, seat, touch_motion);
struct wlr_touch_motion_event *event = data;
diya_idle_manager_notify_activity(seat->wlr_seat);
int touch_point_count = wl_list_length(&seat->touch_points);
/* Find existing touch point to determine initial offsets to subtract */
struct touch_point *touch_point;
wl_list_for_each(touch_point, &seat->touch_points, link)
{
if (touch_point->touch_id == event->touch_id)
{
if (touch_point->surface)
{
/* Convert coordinates: first [0, 1] => layout */
double lx, ly;
wlr_cursor_absolute_to_layout_coords(seat->cursor,
&event->touch->base, event->x, event->y, &lx, &ly);
/* Apply offsets to get surface coords before reporting event */
double sx = lx - touch_point->x_offset;
double sy = ly - touch_point->y_offset;
if (touch_point_count == 1)
{
wlr_cursor_warp_absolute(seat->cursor, &event->touch->base, event->x, event->y);
}
wlr_seat_touch_notify_motion(seat->wlr_seat, event->time_msec,
event->touch_id, sx, sy);
}
else
{
if (touch_point_count == 1)
{
diyac_cursor_emulate_move_absolute(seat, &event->touch->base, event->x, event->y, event->time_msec);
}
}
return;
}
}
}
static void seat_touch_frame(struct wl_listener *listener, void *data)
{
(void) data;
struct diyac_seat *seat = wl_container_of(listener, seat, touch_frame);
wlr_seat_touch_notify_frame(seat->wlr_seat);
}
void diyac_touch_init(struct diyac_seat *seat)
{
seat->touch_down.notify = seat_touch_down;
wl_signal_add(&seat->cursor->events.touch_down, &seat->touch_down);
seat->touch_up.notify = seat_touch_up;
wl_signal_add(&seat->cursor->events.touch_up, &seat->touch_up);
seat->touch_motion.notify = seat_touch_motion;
wl_signal_add(&seat->cursor->events.touch_motion, &seat->touch_motion);
seat->touch_frame.notify = seat_touch_frame;
wl_signal_add(&seat->cursor->events.touch_frame, &seat->touch_frame);
}
void diyac_touch_drop(struct diyac_seat *seat)
{
wl_list_remove(&seat->touch_points);
wl_list_remove(&seat->touch_down.link);
wl_list_remove(&seat->touch_up.link);
wl_list_remove(&seat->touch_motion.link);
wl_list_remove(&seat->touch_frame.link);
}

8
touch.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef DIYAC_TOUCH_H
#define DIYAC_TOUCH_H
#include "diyac.h"
void diyac_touch_init(struct diyac_seat *seat);
void diyac_touch_drop(struct diyac_seat *seat);
#endif