Add pointer events

qemu-extended-key-event
Andri Yngvason 2020-07-10 12:58:29 +00:00
parent a8e2a84668
commit fe17fc2e68
4 changed files with 259 additions and 1 deletions

32
include/pointer.h 100644
View File

@ -0,0 +1,32 @@
#pragma once
#include <wayland-client.h>
enum pointer_button_mask {
POINTER_BUTTON_LEFT = 1 << 0,
POINTER_BUTTON_MIDDLE = 1 << 1,
POINTER_BUTTON_RIGHT = 1 << 2,
// TODO: More buttons
};
struct pointer {
struct wl_pointer* wl_pointer;
struct wl_list link;
enum pointer_button_mask pressed;
wl_fixed_t x, y;
};
struct pointer_collection {
struct wl_list pointers;
void (*on_frame)(struct pointer_collection*, struct pointer*);
void* userdata;
};
struct pointer_collection* pointer_collection_new(void);
void pointer_collection_destroy(struct pointer_collection*);
int pointer_collection_add_wl_pointer(struct pointer_collection* self,
struct wl_pointer* wl_pointer);
void pointer_collection_remove_wl_pointer(struct pointer_collection* self,
struct wl_pointer* wl_pointer);

View File

@ -46,6 +46,7 @@ sources = [
'src/main.c',
'src/shm.c',
'src/seat.c',
'src/pointer.c',
'src/strlcpy.c',
]

View File

@ -15,6 +15,7 @@
#include "xdg-shell.h"
#include "shm.h"
#include "seat.h"
#include "pointer.h"
struct buffer {
int width, height, stride;
@ -39,6 +40,7 @@ static struct wl_compositor* wl_compositor;
static struct wl_shm* wl_shm;
static struct xdg_wm_base* xdg_wm_base;
static struct wl_list seats;
struct pointer_collection* pointers;
static enum wl_shm_format wl_shm_format;
static bool have_format = false;
@ -47,6 +49,18 @@ static bool do_run = true;
struct window* window = NULL;
static void on_seat_capability_change(struct seat* seat)
{
if (seat->capabilities & WL_SEAT_CAPABILITY_POINTER) {
// TODO: Make sure this only happens once
struct wl_pointer* wl_pointer =
wl_seat_get_pointer(seat->wl_seat);
pointer_collection_add_wl_pointer(pointers, wl_pointer);
} else {
// TODO Remove
}
}
static void registry_add(void* data, struct wl_registry* registry, uint32_t id,
const char* interface, uint32_t version)
{
@ -59,7 +73,7 @@ static void registry_add(void* data, struct wl_registry* registry, uint32_t id,
wl_shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
} else if (strcmp(interface, "wl_seat") == 0) {
struct wl_seat* wl_seat;
wl_seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
wl_seat = wl_registry_bind(registry, id, &wl_seat_interface, 5);
struct seat* seat = seat_new(wl_seat, id);
if (!seat) {
@ -68,6 +82,7 @@ static void registry_add(void* data, struct wl_registry* registry, uint32_t id,
}
wl_list_insert(&seats, &seat->link);
seat->on_capability_change = on_seat_capability_change;
}
}
@ -311,6 +326,17 @@ static void window_destroy(struct window* w)
free(w);
}
void on_pointer_event(struct pointer_collection* collection,
struct pointer* pointer)
{
rfbClient* client = collection->userdata;
int x = wl_fixed_to_int(pointer->x);
int y = wl_fixed_to_int(pointer->y);
SendPointerEvent(client, x, y, pointer->pressed);
}
rfbBool rfb_client_alloc_fb(rfbClient* cl)
{
int stride = cl->width * 4; // TODO?
@ -452,6 +478,12 @@ int main(int argc, char* argv[])
if (init_wayland_event_handler() < 0)
goto event_handler_failure;
pointers = pointer_collection_new();
if (!pointers)
goto pointer_failure;
pointers->on_frame = on_pointer_event;
wl_registry = wl_display_get_registry(wl_display);
if (!wl_registry)
goto registry_failure;
@ -468,11 +500,14 @@ int main(int argc, char* argv[])
wl_shm_add_listener(wl_shm, &shm_listener, NULL);
xdg_wm_base_add_listener(xdg_wm_base, &xdg_wm_base_listener, NULL);
wl_display_roundtrip(wl_display);
wl_display_roundtrip(wl_display);
rfbClient* vnc = rfb_client_create(&argc, argv);
if (!vnc)
goto vnc_failure;
pointers->userdata = vnc;
wl_display_dispatch(wl_display);
while (do_run) {
@ -493,6 +528,8 @@ vnc_failure:
wl_registry_destroy(wl_registry);
registry_failure:
pointer_collection_destroy(pointers);
pointer_failure:
event_handler_failure:
wl_display_disconnect(wl_display);
display_failure:

188
src/pointer.c 100644
View File

@ -0,0 +1,188 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>
#include <wayland-client.h>
#include <linux/input-event-codes.h>
#include "pointer.h"
struct pointer* pointer_new(struct wl_pointer* wl_pointer)
{
struct pointer* self = calloc(1, sizeof(*self));
if (!self)
return NULL;
self->wl_pointer = wl_pointer;
return self;
}
void pointer_destroy(struct pointer* self)
{
wl_pointer_destroy(self->wl_pointer);
free(self);
}
struct pointer_collection* pointer_collection_new(void)
{
struct pointer_collection* self = calloc(1, sizeof(*self));
if (!self)
return NULL;
wl_list_init(&self->pointers);
return self;
}
void pointer_collection_destroy(struct pointer_collection* self)
{
struct pointer* pointer;
struct pointer* tmp;
wl_list_for_each_safe(pointer, tmp, &self->pointers, link) {
wl_list_remove(&pointer->link);
pointer_destroy(pointer);
}
free(self);
}
struct pointer* pointer_collection_find_wl_pointer(
struct pointer_collection* self, struct wl_pointer* wl_pointer)
{
struct pointer* pointer;
wl_list_for_each(pointer, &self->pointers, link)
if (pointer->wl_pointer == wl_pointer)
return pointer;
return NULL;
}
static void pointer_enter(void* data, struct wl_pointer* wl_pointer,
uint32_t serial, struct wl_surface* surface, wl_fixed_t x,
wl_fixed_t y)
{
// TODO
}
static void pointer_leave(void* data, struct wl_pointer* wl_pointer,
uint32_t serial, struct wl_surface* surface)
{
// TODO
}
static void pointer_motion(void* data, struct wl_pointer* wl_pointer,
uint32_t t, wl_fixed_t x, wl_fixed_t y)
{
struct pointer_collection* self = data;
struct pointer* pointer =
pointer_collection_find_wl_pointer(self, wl_pointer);
assert(pointer);
pointer->x = x;
pointer->y = y;
}
static void pointer_set_button_state(struct pointer* self,
enum pointer_button_mask button,
enum wl_pointer_button_state state)
{
if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
self->pressed |= button;
} else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
self->pressed &= ~button;
} else {
abort();
}
}
static void pointer_button(void* data, struct wl_pointer* wl_pointer,
uint32_t serial, uint32_t t, uint32_t button,
enum wl_pointer_button_state state)
{
struct pointer_collection* self = data;
struct pointer* pointer =
pointer_collection_find_wl_pointer(self, wl_pointer);
assert(pointer);
switch (button) {
case BTN_LEFT:
pointer_set_button_state(pointer, POINTER_BUTTON_LEFT, state);
break;
case BTN_RIGHT:
pointer_set_button_state(pointer, POINTER_BUTTON_RIGHT, state);
break;
case BTN_MIDDLE:
pointer_set_button_state(pointer, POINTER_BUTTON_MIDDLE, state);
break;
// TODO: More buttons
}
}
static void pointer_axis(void* data, struct wl_pointer* wl_pointer, uint32_t t,
enum wl_pointer_axis axis, wl_fixed_t value)
{
// TODO
}
static void pointer_axis_source(void* data, struct wl_pointer* wl_pointer,
enum wl_pointer_axis_source source)
{
// TODO
}
static void pointer_axis_stop(void* data, struct wl_pointer* wl_pointer,
uint32_t t, enum wl_pointer_axis axis)
{
// TODO
}
static void pointer_axis_discrete(void* data, struct wl_pointer* wl_pointer,
enum wl_pointer_axis axis, int steps)
{
// TODO
}
static void pointer_frame(void* data, struct wl_pointer* wl_pointer)
{
struct pointer_collection* self = data;
struct pointer* pointer =
pointer_collection_find_wl_pointer(self, wl_pointer);
self->on_frame(self, pointer);
}
static struct wl_pointer_listener pointer_listener = {
.enter = pointer_enter,
.leave = pointer_leave,
.motion = pointer_motion,
.button = pointer_button,
.axis = pointer_axis,
.axis_source = pointer_axis_source,
.axis_stop = pointer_axis_stop,
.axis_discrete = pointer_axis_discrete,
.frame = pointer_frame,
};
int pointer_collection_add_wl_pointer(struct pointer_collection* self,
struct wl_pointer* wl_pointer)
{
struct pointer* pointer = pointer_new(wl_pointer);
if (!pointer)
return -1;
wl_list_insert(&self->pointers, &pointer->link);
wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener, self);
return 0;
}
void pointer_collection_remove_wl_pointer(struct pointer_collection* self,
struct wl_pointer* wl_pointer)
{
struct pointer* pointer =
pointer_collection_find_wl_pointer(self, wl_pointer);
wl_list_remove(&pointer->link);
free(pointer);
}