Add virtual pointer backend

pull/1/head
Andri Yngvason 2019-12-22 15:41:51 +00:00
parent 98829dba04
commit 82bdbb82c0
4 changed files with 177 additions and 1 deletions

41
include/pointer.h 100644
View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2019 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include <stdint.h>
#include <neatvnc.h>
#include "wlr-virtual-pointer-unstable-v1.h"
struct pointer {
struct nvnc* vnc;
struct zwlr_virtual_pointer_manager_v1* manager;
struct zwlr_virtual_pointer_v1* pointer;
enum nvnc_button_mask current_mask;
uint32_t current_x;
uint32_t current_y;
uint32_t width;
uint32_t height;
};
int pointer_init(struct pointer* self);
void pointer_destroy(struct pointer* self);
void pointer_set(struct pointer* self, uint32_t x, uint32_t y,
enum nvnc_button_mask button_mask);

View File

@ -46,6 +46,7 @@ sources = [
'src/shm.c',
'src/screencopy.c',
'src/output.c',
'src/pointer.c',
]
dependencies = [

View File

@ -33,12 +33,14 @@
#include "wlr-export-dmabuf-unstable-v1.h"
#include "wlr-screencopy-unstable-v1.h"
#include "wlr-virtual-pointer-unstable-v1.h"
#include "render.h"
#include "dmabuf.h"
#include "screencopy.h"
#include "strlcpy.h"
#include "logging.h"
#include "output.h"
#include "pointer.h"
enum frame_capture_backend_type {
FRAME_CAPTURE_BACKEND_NONE = 0,
@ -57,6 +59,7 @@ struct wayvnc {
struct dmabuf_capture dmabuf_backend;
struct screencopy screencopy_backend;
struct frame_capture* capture_backend;
struct pointer pointer_backend;
uv_poll_t wayland_poller;
uv_prepare_t flusher;
@ -138,6 +141,14 @@ static void registry_add(void* data, struct wl_registry* registry,
2);
return;
}
if (strcmp(interface, zwlr_virtual_pointer_manager_v1_interface.name) == 0) {
self->pointer_backend.manager =
wl_registry_bind(registry, id,
&zwlr_virtual_pointer_manager_v1_interface,
1);
return;
}
}
static void registry_remove(void* data, struct wl_registry* registry,
@ -157,7 +168,11 @@ static void registry_remove(void* data, struct wl_registry* registry,
void wayvnc_destroy(struct wayvnc* self)
{
output_list_destroy(&self->outputs);
zwlr_export_dmabuf_manager_v1_destroy(self->dmabuf_backend.manager);
zwlr_virtual_pointer_manager_v1_destroy(self->pointer_backend.manager);
if (self->dmabuf_backend.manager) {
pointer_destroy(&self->pointer_backend);
zwlr_export_dmabuf_manager_v1_destroy(self->dmabuf_backend.manager);
}
wl_display_disconnect(self->display);
}
@ -195,6 +210,14 @@ static int init_wayland(struct wayvnc* self)
self->screencopy_backend.frame_capture.on_done = on_capture_done;
self->screencopy_backend.frame_capture.userdata = self;
if (self->pointer_backend.manager) {
self->pointer_backend.vnc = self->nvnc;
pointer_init(&self->pointer_backend);
} else {
log_error("Compositor does not support %s.\n",
zwlr_virtual_pointer_manager_v1_interface.name);
}
return 0;
export_manager_failure:
@ -271,6 +294,17 @@ uint32_t fourcc_from_gl_format(uint32_t format)
return DRM_FORMAT_INVALID;
}
static void on_pointer_event(struct nvnc_client* client, uint16_t x, uint16_t y,
enum nvnc_button_mask button_mask)
{
// TODO: Have a seat per client
struct nvnc* nvnc = nvnc_get_server(client);
struct wayvnc* wayvnc = nvnc_get_userdata(nvnc);
pointer_set(&wayvnc->pointer_backend, x, y, button_mask);
}
int init_nvnc(struct wayvnc* self, const char* addr, uint16_t port)
{
self->nvnc = nvnc_open(addr, port);
@ -289,6 +323,9 @@ int init_nvnc(struct wayvnc* self, const char* addr, uint16_t port)
self->selected_output->height,
format);
if (self->pointer_backend.manager)
nvnc_set_pointer_fn(self->nvnc, on_pointer_event);
return 0;
}
@ -493,6 +530,12 @@ int main(int argc, char* argv[])
self.dmabuf_backend.fc.wl_output = out->wl_output;
self.screencopy_backend.frame_capture.wl_output = out->wl_output;
if (self.pointer_backend.manager) {
self.pointer_backend.width = out->width;
self.pointer_backend.height = out->height;
}
if (renderer_init(&self.renderer, self.selected_output->width,
self.selected_output->height) < 0) {
log_error("Failed to initialise renderer\n");

91
src/pointer.c 100644
View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2019 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <wayland-client-protocol.h>
#include <wayland-client.h>
#include <time.h>
#include <linux/input-event-codes.h>
#include "pointer.h"
#include "wlr-virtual-pointer-unstable-v1.h"
static inline uint32_t gettime_ms(void)
{
struct timespec ts = { 0 };
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000UL + ts.tv_nsec / 1000000UL;
}
int pointer_init(struct pointer* self)
{
self->pointer =
zwlr_virtual_pointer_manager_v1_create_virtual_pointer(self->manager, NULL);
zwlr_virtual_pointer_v1_axis_source(self->pointer,
WL_POINTER_AXIS_SOURCE_WHEEL);
return 0;
}
void pointer_destroy(struct pointer* self)
{
zwlr_virtual_pointer_v1_destroy(self->pointer);
}
static void pointer_set_button_mask(struct pointer* self, uint32_t t,
enum nvnc_button_mask mask)
{
enum nvnc_button_mask diff = self->current_mask ^ mask;
if (diff & NVNC_BUTTON_LEFT)
zwlr_virtual_pointer_v1_button(self->pointer, t, BTN_LEFT,
!!(mask & NVNC_BUTTON_LEFT));
if (diff & NVNC_BUTTON_MIDDLE)
zwlr_virtual_pointer_v1_button(self->pointer, t, BTN_MIDDLE,
!!(mask & NVNC_BUTTON_MIDDLE));
if (diff & NVNC_BUTTON_RIGHT)
zwlr_virtual_pointer_v1_button(self->pointer, t, BTN_RIGHT,
!!(mask & NVNC_BUTTON_RIGHT));
if ((diff & NVNC_SCROLL_UP) && !(mask & NVNC_SCROLL_UP))
zwlr_virtual_pointer_v1_axis(self->pointer, t,
WL_POINTER_AXIS_VERTICAL_SCROLL,
1);
if ((diff & NVNC_SCROLL_DOWN) && !(mask & NVNC_SCROLL_DOWN))
zwlr_virtual_pointer_v1_axis(self->pointer, t,
WL_POINTER_AXIS_VERTICAL_SCROLL,
1);
self->current_mask = mask;
}
void pointer_set(struct pointer* self, uint32_t x, uint32_t y,
enum nvnc_button_mask button_mask)
{
uint32_t t = gettime_ms();
if (x != self->current_x || y != self->current_y)
zwlr_virtual_pointer_v1_motion_absolute(self->pointer, t, x, y,
self->width,
self->height);
self->current_x = x;
self->current_y = y;
if (button_mask != self->current_mask)
pointer_set_button_mask(self, t, button_mask);
}