Merge pull request #87 from nyorain/wlbcursor

Implement wayland backend cursor
This commit is contained in:
Drew DeVault 2017-08-14 18:21:09 -04:00 committed by GitHub
commit 5ca88af557
5 changed files with 268 additions and 1 deletions

View File

@ -22,6 +22,7 @@ backend_files = files(
'wayland/output.c',
'wayland/registry.c',
'wayland/wl_seat.c',
'wayland/os-compatibility.c',
)
if systemd.found()

View File

@ -0,0 +1,154 @@
/*
* Copyright © 2012 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define _XOPEN_SOURCE 700
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/epoll.h>
#include <string.h>
#include <stdlib.h>
int
os_fd_set_cloexec(int fd)
{
long flags;
if (fd == -1)
return -1;
flags = fcntl(fd, F_GETFD);
if (flags == -1)
return -1;
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
return -1;
return 0;
}
static int
set_cloexec_or_close(int fd)
{
if (os_fd_set_cloexec(fd) != 0) {
close(fd);
return -1;
}
return fd;
}
static int
create_tmpfile_cloexec(char *tmpname)
{
int fd;
#ifdef HAVE_MKOSTEMP
fd = mkostemp(tmpname, O_CLOEXEC);
if (fd >= 0)
unlink(tmpname);
#else
fd = mkstemp(tmpname);
if (fd >= 0) {
fd = set_cloexec_or_close(fd);
unlink(tmpname);
}
#endif
return fd;
}
/*
* Create a new, unique, anonymous file of the given size, and
* return the file descriptor for it. The file descriptor is set
* CLOEXEC. The file is immediately suitable for mmap()'ing
* the given size at offset zero.
*
* The file should not have a permanent backing store like a disk,
* but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
*
* The file name is deleted from the file system.
*
* The file is suitable for buffer sharing between processes by
* transmitting the file descriptor over Unix sockets using the
* SCM_RIGHTS methods.
*
* If the C library implements posix_fallocate(), it is used to
* guarantee that disk space is available for the file at the
* given size. If disk space is insufficient, errno is set to ENOSPC.
* If posix_fallocate() is not supported, program may receive
* SIGBUS on accessing mmap()'ed file contents instead.
*/
int
os_create_anonymous_file(off_t size)
{
static const char template[] = "/wlroots-shared-XXXXXX";
const char *path;
char *name;
int fd;
int ret;
path = getenv("XDG_RUNTIME_DIR");
if (!path) {
errno = ENOENT;
return -1;
}
name = malloc(strlen(path) + sizeof(template));
if (!name)
return -1;
strcpy(name, path);
strcat(name, template);
fd = create_tmpfile_cloexec(name);
free(name);
if (fd < 0)
return -1;
#ifdef HAVE_POSIX_FALLOCATE
do {
ret = posix_fallocate(fd, 0, size);
} while (ret == EINTR);
if (ret != 0) {
close(fd);
errno = ret;
return -1;
}
#else
do {
ret = ftruncate(fd, size);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
close(fd);
return -1;
}
#endif
return fd;
}

View File

@ -3,12 +3,17 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <wayland-client.h>
#include <GLES3/gl3.h>
#include <wlr/interfaces/wlr_output.h>
#include <wlr/util/log.h>
#include "backend/wayland.h"
int os_create_anonymous_file(off_t size);
static struct wl_callback_listener frame_listener;
static void surface_frame_callback(void *data, struct wl_callback *cb, uint32_t time) {
@ -46,9 +51,86 @@ static void wlr_wl_output_transform(struct wlr_output *_output,
output->wlr_output.transform = transform;
}
static bool wlr_wl_output_set_cursor(struct wlr_output *_output,
const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height) {
struct wlr_wl_backend_output *output = (struct wlr_wl_backend_output *)_output;
struct wlr_wl_backend *backend = output->backend;
stride *= 4; // stride is given in pixels, we need it in bytes
if (!backend->shm || !backend->pointer) {
wlr_log(L_INFO, "cannot set cursor, no shm or pointer");
return false;
}
if (!output->cursor_surface) {
output->cursor_surface = wl_compositor_create_surface(output->backend->compositor);
}
uint32_t size = stride * height;
if (output->cursor_buf_size != size) {
if (output->cursor_buffer) {
wl_buffer_destroy(output->cursor_buffer);
}
if (size > output->cursor_buf_size) {
if (output->cursor_pool) {
wl_shm_pool_destroy(output->cursor_pool);
output->cursor_pool = NULL;
munmap(output->cursor_data, output->cursor_buf_size);
}
}
if (!output->cursor_pool) {
int fd = os_create_anonymous_file(size);
if (fd < 0) {
wlr_log_errno(L_INFO, "creating anonymous file for cursor buffer failed");
return false;
}
output->cursor_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (output->cursor_data == MAP_FAILED) {
close(fd);
wlr_log_errno(L_INFO, "mmap failed");
return false;
}
output->cursor_pool = wl_shm_create_pool(backend->shm, fd, size);
close(fd);
}
output->cursor_buffer = wl_shm_pool_create_buffer(output->cursor_pool,
0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
output->cursor_buf_size = size;
}
memcpy(output->cursor_data, buf, size);
wl_surface_attach(output->cursor_surface, output->cursor_buffer, 0, 0);
wl_surface_damage(output->cursor_surface, 0, 0, width, height);
wl_surface_commit(output->cursor_surface);
wlr_wl_output_update_cursor(output, output->enter_serial);
return true;
}
static void wlr_wl_output_destroy(struct wlr_output *_output) {
struct wlr_wl_backend_output *output = (struct wlr_wl_backend_output *)_output;
wl_signal_emit(&output->backend->backend.events.output_remove, &output->wlr_output);
if (output->cursor_buf_size != 0) {
assert(output->cursor_data);
assert(output->cursor_buffer);
assert(output->cursor_pool);
wl_buffer_destroy(output->cursor_buffer);
munmap(output->cursor_data, output->cursor_buf_size);
wl_shm_pool_destroy(output->cursor_pool);
}
if (output->cursor_surface) {
wl_surface_destroy(output->cursor_surface);
}
if (output->frame_callback) {
wl_callback_destroy(output->frame_callback);
}
@ -59,11 +141,25 @@ static void wlr_wl_output_destroy(struct wlr_output *_output) {
free(output);
}
void wlr_wl_output_update_cursor(struct wlr_wl_backend_output *output, uint32_t serial) {
if (output->cursor_surface && output->backend->pointer && serial) {
wl_pointer_set_cursor(output->backend->pointer, serial,
output->cursor_surface, 0, 0);
}
}
bool wlr_wl_output_move_cursor(struct wlr_output *_output, int x, int y) {
// TODO: only return true if x == current x and y == current y
return true;
}
static struct wlr_output_impl output_impl = {
.transform = wlr_wl_output_transform,
.destroy = wlr_wl_output_destroy,
.make_current = wlr_wl_output_make_current,
.swap_buffers = wlr_wl_output_swap_buffers,
.set_cursor = wlr_wl_output_set_cursor,
.move_cursor = wlr_wl_output_move_cursor
};
void handle_ping(void* data, struct wl_shell_surface* ssurface, uint32_t serial) {

View File

@ -23,6 +23,8 @@ static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
wlr_wl_output_for_surface(wlr_wl_dev->backend, surface);
assert(output);
wlr_wl_pointer->current_output = output;
wlr_wl_pointer->current_output->enter_serial = serial;
wlr_wl_output_update_cursor(wlr_wl_pointer->current_output, serial);
}
static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
@ -30,8 +32,11 @@ static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
struct wlr_input_device *dev = data;
assert(dev && dev->pointer);
struct wlr_wl_pointer *wlr_wl_pointer = (struct wlr_wl_pointer *)dev->pointer;
if (wlr_wl_pointer->current_output) {
wlr_wl_pointer->current_output->enter_serial = 0;
wlr_wl_pointer->current_output = NULL;
}
}
static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
@ -227,6 +232,7 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
wlr_pointer_init(wlr_device->pointer, NULL);
wlr_wl_device->resource = wl_pointer;
wl_signal_emit(&backend->backend.events.input_add, wlr_device);
backend->pointer = wl_pointer;
}
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
wlr_log(L_DEBUG, "seat %p offered keyboard", (void*) wl_seat);

View File

@ -27,6 +27,7 @@ struct wlr_wl_backend {
struct wl_shell *shell;
struct wl_shm *shm;
struct wl_seat *seat;
struct wl_pointer *pointer;
char *seat_name;
};
@ -38,6 +39,14 @@ struct wlr_wl_backend_output {
struct wl_shell_surface *shell_surface;
struct wl_egl_window *egl_window;
struct wl_callback *frame_callback;
struct wl_shm_pool *cursor_pool;
void *cursor_buffer; // actually a (client-side) struct wl_buffer*
uint8_t *cursor_data;
struct wl_surface *cursor_surface;
uint32_t cursor_buf_size;
uint32_t enter_serial;
void *egl_surface;
};
@ -55,6 +64,7 @@ struct wlr_wl_pointer {
};
void wlr_wl_registry_poll(struct wlr_wl_backend *backend);
void wlr_wl_output_update_cursor(struct wlr_wl_backend_output *output, uint32_t serial);
struct wlr_wl_backend_output *wlr_wl_output_for_surface(
struct wlr_wl_backend *backend, struct wl_surface *surface);