blob: b64e96cf5db0eb675b7055e3cee1a6f4dd32941f [file] [log] [blame]
/*
* Copyright © 2008 Kristian Høgsberg
* Copyright © 2012-2013 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.
*/
#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
#include <assert.h>
#include <time.h>
#include <cairo.h>
#include <sys/mman.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <stdbool.h>
#ifdef HAVE_CAIRO_EGL
#include <wayland-egl.h>
#ifdef USE_CAIRO_GLESV2
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#else
#include <GL/gl.h>
#endif
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <cairo-gl.h>
#elif !defined(ENABLE_EGL) /* platform.h defines these if EGL is enabled */
typedef void *EGLDisplay;
typedef void *EGLConfig;
typedef void *EGLContext;
#define EGL_NO_DISPLAY ((EGLDisplay)0)
#endif /* no HAVE_CAIRO_EGL */
#include <xkbcommon/xkbcommon.h>
#ifdef HAVE_XKBCOMMON_COMPOSE
#include <xkbcommon/xkbcommon-compose.h>
#endif
#include <wayland-cursor.h>
#include <linux/input.h>
#include <wayland-client.h>
#include "shared/cairo-util.h"
#include "shared/helpers.h"
#include "shared/xalloc.h"
#include "shared/zalloc.h"
#include "xdg-shell-unstable-v6-client-protocol.h"
#include "text-cursor-position-client-protocol.h"
#include "pointer-constraints-unstable-v1-client-protocol.h"
#include "relative-pointer-unstable-v1-client-protocol.h"
#include "shared/os-compatibility.h"
#include "window.h"
#include <sys/types.h>
#include "ivi-application-client-protocol.h"
#define IVI_SURFACE_ID 9000
#define ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION 1
#define ZWP_POINTER_CONSTRAINTS_V1_VERSION 1
struct shm_pool;
struct global {
uint32_t name;
char *interface;
uint32_t version;
struct wl_list link;
};
struct display {
struct wl_display *display;
struct wl_registry *registry;
struct wl_compositor *compositor;
struct wl_subcompositor *subcompositor;
struct wl_shm *shm;
struct wl_data_device_manager *data_device_manager;
struct text_cursor_position *text_cursor_position;
struct zxdg_shell_v6 *xdg_shell;
struct ivi_application *ivi_application; /* ivi style shell */
struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
struct zwp_pointer_constraints_v1 *pointer_constraints;
EGLDisplay dpy;
EGLConfig argb_config;
EGLContext argb_ctx;
cairo_device_t *argb_device;
uint32_t serial;
int display_fd;
uint32_t display_fd_events;
struct task display_task;
int epoll_fd;
struct wl_list deferred_list;
int running;
struct wl_list global_list;
struct wl_list window_list;
struct wl_list input_list;
struct wl_list output_list;
struct theme *theme;
struct wl_cursor_theme *cursor_theme;
struct wl_cursor **cursors;
display_output_handler_t output_configure_handler;
display_global_handler_t global_handler;
display_global_handler_t global_handler_remove;
void *user_data;
struct xkb_context *xkb_context;
/* A hack to get text extents for tooltips */
cairo_surface_t *dummy_surface;
void *dummy_surface_data;
int has_rgb565;
int data_device_manager_version;
};
struct window_output {
struct output *output;
struct wl_list link;
};
struct toysurface {
/*
* Prepare the surface for drawing. Ensure there is a surface
* of the right size available for rendering, and return it.
* dx,dy are the x,y of wl_surface.attach.
* width,height are the new buffer size.
* If flags has SURFACE_HINT_RESIZE set, the user is
* doing continuous resizing.
* Returns the Cairo surface to draw to.
*/
cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
int32_t width, int32_t height, uint32_t flags,
enum wl_output_transform buffer_transform, int32_t buffer_scale);
/*
* Post the surface to the server, returning the server allocation
* rectangle. The Cairo surface from prepare() must be destroyed
* after calling this.
*/
void (*swap)(struct toysurface *base,
enum wl_output_transform buffer_transform, int32_t buffer_scale,
struct rectangle *server_allocation);
/*
* Make the toysurface current with the given EGL context.
* Returns 0 on success, and negative on failure.
*/
int (*acquire)(struct toysurface *base, EGLContext ctx);
/*
* Release the toysurface from the EGL context, returning control
* to Cairo.
*/
void (*release)(struct toysurface *base);
/*
* Destroy the toysurface, including the Cairo surface, any
* backing storage, and the Wayland protocol objects.
*/
void (*destroy)(struct toysurface *base);
};
struct surface {
struct window *window;
struct wl_surface *surface;
struct wl_subsurface *subsurface;
int synchronized;
int synchronized_default;
struct toysurface *toysurface;
struct widget *widget;
int redraw_needed;
struct wl_callback *frame_cb;
uint32_t last_time;
struct rectangle allocation;
struct rectangle server_allocation;
struct wl_region *input_region;
struct wl_region *opaque_region;
enum window_buffer_type buffer_type;
enum wl_output_transform buffer_transform;
int32_t buffer_scale;
cairo_surface_t *cairo_surface;
struct wl_list link;
};
struct window {
struct display *display;
struct wl_list window_output_list;
char *title;
struct rectangle saved_allocation;
struct rectangle min_allocation;
struct rectangle pending_allocation;
struct rectangle last_geometry;
int x, y;
int redraw_inhibited;
int redraw_needed;
int redraw_task_scheduled;
struct task redraw_task;
int resize_needed;
int custom;
int focused;
int resizing;
int fullscreen;
int maximized;
enum preferred_format preferred_format;
window_key_handler_t key_handler;
window_keyboard_focus_handler_t keyboard_focus_handler;
window_data_handler_t data_handler;
window_drop_handler_t drop_handler;
window_close_handler_t close_handler;
window_fullscreen_handler_t fullscreen_handler;
window_output_handler_t output_handler;
window_state_changed_handler_t state_changed_handler;
window_locked_pointer_motion_handler_t locked_pointer_motion_handler;
struct surface *main_surface;
struct zxdg_surface_v6 *xdg_surface;
struct zxdg_toplevel_v6 *xdg_toplevel;
struct zxdg_popup_v6 *xdg_popup;
struct window *parent;
struct window *last_parent;
struct ivi_surface *ivi_surface;
struct window_frame *frame;
/* struct surface::link, contains also main_surface */
struct wl_list subsurface_list;
struct zwp_relative_pointer_v1 *relative_pointer;
struct zwp_locked_pointer_v1 *locked_pointer;
struct input *locked_input;
bool pointer_locked;
locked_pointer_locked_handler_t pointer_locked_handler;
locked_pointer_unlocked_handler_t pointer_unlocked_handler;
confined_pointer_confined_handler_t pointer_confined_handler;
confined_pointer_unconfined_handler_t pointer_unconfined_handler;
struct zwp_confined_pointer_v1 *confined_pointer;
struct input *confined_input;
struct widget *confined_widget;
bool confined;
void *user_data;
struct wl_list link;
};
struct widget {
struct window *window;
struct surface *surface;
struct tooltip *tooltip;
struct wl_list child_list;
struct wl_list link;
struct rectangle allocation;
widget_resize_handler_t resize_handler;
widget_redraw_handler_t redraw_handler;
widget_enter_handler_t enter_handler;
widget_leave_handler_t leave_handler;
widget_motion_handler_t motion_handler;
widget_button_handler_t button_handler;
widget_touch_down_handler_t touch_down_handler;
widget_touch_up_handler_t touch_up_handler;
widget_touch_motion_handler_t touch_motion_handler;
widget_touch_frame_handler_t touch_frame_handler;
widget_touch_cancel_handler_t touch_cancel_handler;
widget_axis_handler_t axis_handler;
widget_pointer_frame_handler_t pointer_frame_handler;
widget_axis_source_handler_t axis_source_handler;
widget_axis_stop_handler_t axis_stop_handler;
widget_axis_discrete_handler_t axis_discrete_handler;
void *user_data;
int opaque;
int tooltip_count;
int default_cursor;
/* If this is set to false then no cairo surface will be
* created before redrawing the surface. This is useful if the
* redraw handler is going to do completely custom rendering
* such as using EGL directly */
int use_cairo;
};
struct touch_point {
int32_t id;
float x, y;
struct widget *widget;
struct wl_list link;
};
struct input {
struct display *display;
struct wl_seat *seat;
struct wl_pointer *pointer;
struct wl_keyboard *keyboard;
struct wl_touch *touch;
struct wl_list touch_point_list;
struct window *pointer_focus;
struct window *keyboard_focus;
struct window *touch_focus;
int current_cursor;
uint32_t cursor_anim_start;
struct wl_callback *cursor_frame_cb;
uint32_t cursor_timer_start;
uint32_t cursor_anim_current;
struct toytimer cursor_timer;
bool cursor_timer_running;
struct wl_surface *pointer_surface;
uint32_t modifiers;
uint32_t pointer_enter_serial;
uint32_t cursor_serial;
float sx, sy;
struct wl_list link;
struct widget *focus_widget;
struct widget *grab;
uint32_t grab_button;
struct wl_data_device *data_device;
struct data_offer *drag_offer;
struct data_offer *selection_offer;
uint32_t touch_grab;
int32_t touch_grab_id;
float drag_x, drag_y;
struct window *drag_focus;
uint32_t drag_enter_serial;
struct {
struct xkb_keymap *keymap;
struct xkb_state *state;
#ifdef HAVE_XKBCOMMON_COMPOSE
struct xkb_compose_table *compose_table;
struct xkb_compose_state *compose_state;
#endif
xkb_mod_mask_t control_mask;
xkb_mod_mask_t alt_mask;
xkb_mod_mask_t shift_mask;
} xkb;
int32_t repeat_rate_sec;
int32_t repeat_rate_nsec;
int32_t repeat_delay_sec;
int32_t repeat_delay_nsec;
struct toytimer repeat_timer;
uint32_t repeat_sym;
uint32_t repeat_key;
uint32_t repeat_time;
int seat_version;
};
struct output {
struct display *display;
struct wl_output *output;
uint32_t server_output_id;
struct rectangle allocation;
struct wl_list link;
int transform;
int scale;
char *make;
char *model;
display_output_handler_t destroy_handler;
void *user_data;
};
struct window_frame {
struct widget *widget;
struct widget *child;
struct frame *frame;
uint32_t last_time;
uint32_t did_double, double_click;
int32_t last_id, double_id;
};
struct menu {
void *user_data;
struct window *window;
struct widget *widget;
struct input *input;
struct frame *frame;
const char **entries;
uint32_t time;
int current;
int count;
int release_count;
menu_func_t func;
};
struct tooltip {
struct widget *parent;
struct widget *widget;
char *entry;
struct toytimer timer;
float x, y;
};
struct shm_pool {
struct wl_shm_pool *pool;
size_t size;
size_t used;
void *data;
};
enum {
CURSOR_DEFAULT = 100,
CURSOR_UNSET
};
static const cairo_user_data_key_t shm_surface_data_key;
/* #define DEBUG */
#ifdef DEBUG
static void
debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
__attribute__ ((format (printf, 4, 5)));
static void
debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
{
va_list ap;
struct timeval tv;
gettimeofday(&tv, NULL);
fprintf(stderr, "%8ld.%03ld ",
(long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
if (proxy)
fprintf(stderr, "%s@%d ",
wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
/*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
fprintf(stderr, "%s ", func);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
#define DBG(fmt, ...) \
debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
#define DBG_OBJ(obj, fmt, ...) \
debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
#else
#define DBG(...) do {} while (0)
#define DBG_OBJ(...) do {} while (0)
#endif
static void
surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
{
int32_t tmp;
switch (buffer_transform) {
case WL_OUTPUT_TRANSFORM_90:
case WL_OUTPUT_TRANSFORM_270:
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
tmp = *width;
*width = *height;
*height = tmp;
break;
default:
break;
}
*width *= buffer_scale;
*height *= buffer_scale;
}
static void
buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
{
int32_t tmp;
switch (buffer_transform) {
case WL_OUTPUT_TRANSFORM_90:
case WL_OUTPUT_TRANSFORM_270:
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
tmp = *width;
*width = *height;
*height = tmp;
break;
default:
break;
}
*width /= buffer_scale;
*height /= buffer_scale;
}
#ifdef HAVE_CAIRO_EGL
struct egl_window_surface {
struct toysurface base;
cairo_surface_t *cairo_surface;
struct display *display;
struct wl_surface *surface;
struct wl_egl_window *egl_window;
EGLSurface egl_surface;
};
static struct egl_window_surface *
to_egl_window_surface(struct toysurface *base)
{
return container_of(base, struct egl_window_surface, base);
}
static cairo_surface_t *
egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
int32_t width, int32_t height, uint32_t flags,
enum wl_output_transform buffer_transform, int32_t buffer_scale)
{
struct egl_window_surface *surface = to_egl_window_surface(base);
surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
cairo_gl_surface_set_size(surface->cairo_surface, width, height);
return cairo_surface_reference(surface->cairo_surface);
}
static void
egl_window_surface_swap(struct toysurface *base,
enum wl_output_transform buffer_transform, int32_t buffer_scale,
struct rectangle *server_allocation)
{
struct egl_window_surface *surface = to_egl_window_surface(base);
cairo_gl_surface_swapbuffers(surface->cairo_surface);
wl_egl_window_get_attached_size(surface->egl_window,
&server_allocation->width,
&server_allocation->height);
buffer_to_surface_size (buffer_transform, buffer_scale,
&server_allocation->width,
&server_allocation->height);
}
static int
egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
{
struct egl_window_surface *surface = to_egl_window_surface(base);
cairo_device_t *device;
device = cairo_surface_get_device(surface->cairo_surface);
if (!device)
return -1;
if (!ctx) {
if (device == surface->display->argb_device)
ctx = surface->display->argb_ctx;
else
assert(0);
}
cairo_device_flush(device);
cairo_device_acquire(device);
if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
surface->egl_surface, ctx))
fprintf(stderr, "failed to make surface current\n");
return 0;
}
static void
egl_window_surface_release(struct toysurface *base)
{
struct egl_window_surface *surface = to_egl_window_surface(base);
cairo_device_t *device;
device = cairo_surface_get_device(surface->cairo_surface);
if (!device)
return;
if (!eglMakeCurrent(surface->display->dpy,
EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
fprintf(stderr, "failed to make context current\n");
cairo_device_release(device);
}
static void
egl_window_surface_destroy(struct toysurface *base)
{
struct egl_window_surface *surface = to_egl_window_surface(base);
struct display *d = surface->display;
cairo_surface_destroy(surface->cairo_surface);
weston_platform_destroy_egl_surface(d->dpy, surface->egl_surface);
wl_egl_window_destroy(surface->egl_window);
surface->surface = NULL;
free(surface);
}
static struct toysurface *
egl_window_surface_create(struct display *display,
struct wl_surface *wl_surface,
uint32_t flags,
struct rectangle *rectangle)
{
struct egl_window_surface *surface;
if (display->dpy == EGL_NO_DISPLAY)
return NULL;
surface = zalloc(sizeof *surface);
if (!surface)
return NULL;
surface->base.prepare = egl_window_surface_prepare;
surface->base.swap = egl_window_surface_swap;
surface->base.acquire = egl_window_surface_acquire;
surface->base.release = egl_window_surface_release;
surface->base.destroy = egl_window_surface_destroy;
surface->display = display;
surface->surface = wl_surface;
surface->egl_window = wl_egl_window_create(surface->surface,
rectangle->width,
rectangle->height);
surface->egl_surface =
weston_platform_create_egl_surface(display->dpy,
display->argb_config,
surface->egl_window, NULL);
surface->cairo_surface =
cairo_gl_surface_create_for_egl(display->argb_device,
surface->egl_surface,
rectangle->width,
rectangle->height);
return &surface->base;
}
#else
static struct toysurface *
egl_window_surface_create(struct display *display,
struct wl_surface *wl_surface,
uint32_t flags,
struct rectangle *rectangle)
{
return NULL;
}
#endif
struct shm_surface_data {
struct wl_buffer *buffer;
struct shm_pool *pool;
};
struct wl_buffer *
display_get_buffer_for_surface(struct display *display,
cairo_surface_t *surface)
{
struct shm_surface_data *data;
data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
return data->buffer;
}
static void
shm_pool_destroy(struct shm_pool *pool);
static void
shm_surface_data_destroy(void *p)
{
struct shm_surface_data *data = p;
wl_buffer_destroy(data->buffer);
if (data->pool)
shm_pool_destroy(data->pool);
free(data);
}
static struct wl_shm_pool *
make_shm_pool(struct display *display, int size, void **data)
{
struct wl_shm_pool *pool;
int fd;
fd = os_create_anonymous_file(size);
if (fd < 0) {
fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
size);
return NULL;
}
*data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (*data == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
return NULL;
}
pool = wl_shm_create_pool(display->shm, fd, size);
close(fd);
return pool;
}
static struct shm_pool *
shm_pool_create(struct display *display, size_t size)
{
struct shm_pool *pool = malloc(sizeof *pool);
if (!pool)
return NULL;
pool->pool = make_shm_pool(display, size, &pool->data);
if (!pool->pool) {
free(pool);
return NULL;
}
pool->size = size;
pool->used = 0;
return pool;
}
static void *
shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
{
if (pool->used + size > pool->size)
return NULL;
*offset = pool->used;
pool->used += size;
return (char *) pool->data + *offset;
}
/* destroy the pool. this does not unmap the memory though */
static void
shm_pool_destroy(struct shm_pool *pool)
{
munmap(pool->data, pool->size);
wl_shm_pool_destroy(pool->pool);
free(pool);
}
/* Start allocating from the beginning of the pool again */
static void
shm_pool_reset(struct shm_pool *pool)
{
pool->used = 0;
}
static int
data_length_for_shm_surface(struct rectangle *rect)
{
int stride;
stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
rect->width);
return stride * rect->height;
}
static cairo_surface_t *
display_create_shm_surface_from_pool(struct display *display,
struct rectangle *rectangle,
uint32_t flags, struct shm_pool *pool)
{
struct shm_surface_data *data;
uint32_t format;
cairo_surface_t *surface;
cairo_format_t cairo_format;
int stride, length, offset;
void *map;
data = malloc(sizeof *data);
if (data == NULL)
return NULL;
if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
cairo_format = CAIRO_FORMAT_RGB16_565;
else
cairo_format = CAIRO_FORMAT_ARGB32;
stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
length = stride * rectangle->height;
data->pool = NULL;
map = shm_pool_allocate(pool, length, &offset);
if (!map) {
free(data);
return NULL;
}
surface = cairo_image_surface_create_for_data (map,
cairo_format,
rectangle->width,
rectangle->height,
stride);
cairo_surface_set_user_data(surface, &shm_surface_data_key,
data, shm_surface_data_destroy);
if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
format = WL_SHM_FORMAT_RGB565;
else {
if (flags & SURFACE_OPAQUE)
format = WL_SHM_FORMAT_XRGB8888;
else
format = WL_SHM_FORMAT_ARGB8888;
}
data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
rectangle->width,
rectangle->height,
stride, format);
return surface;
}
static cairo_surface_t *
display_create_shm_surface(struct display *display,
struct rectangle *rectangle, uint32_t flags,
struct shm_pool *alternate_pool,
struct shm_surface_data **data_ret)
{
struct shm_surface_data *data;
struct shm_pool *pool;
cairo_surface_t *surface;
if (alternate_pool) {
shm_pool_reset(alternate_pool);
surface = display_create_shm_surface_from_pool(display,
rectangle,
flags,
alternate_pool);
if (surface) {
data = cairo_surface_get_user_data(surface,
&shm_surface_data_key);
goto out;
}
}
pool = shm_pool_create(display,
data_length_for_shm_surface(rectangle));
if (!pool)
return NULL;
surface =
display_create_shm_surface_from_pool(display, rectangle,
flags, pool);
if (!surface) {
shm_pool_destroy(pool);
return NULL;
}
/* make sure we destroy the pool when the surface is destroyed */
data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
data->pool = pool;
out:
if (data_ret)
*data_ret = data;
return surface;
}
static int
check_size(struct rectangle *rect)
{
if (rect->width && rect->height)
return 0;
fprintf(stderr, "tried to create surface of "
"width: %d, height: %d\n", rect->width, rect->height);
return -1;
}
cairo_surface_t *
display_create_surface(struct display *display,
struct wl_surface *surface,
struct rectangle *rectangle,
uint32_t flags)
{
if (check_size(rectangle) < 0)
return NULL;
assert(flags & SURFACE_SHM);
return display_create_shm_surface(display, rectangle, flags,
NULL, NULL);
}
struct shm_surface_leaf {
cairo_surface_t *cairo_surface;
/* 'data' is automatically destroyed, when 'cairo_surface' is */
struct shm_surface_data *data;
struct shm_pool *resize_pool;
int busy;
};
static void
shm_surface_leaf_release(struct shm_surface_leaf *leaf)
{
if (leaf->cairo_surface)
cairo_surface_destroy(leaf->cairo_surface);
/* leaf->data already destroyed via cairo private */
if (leaf->resize_pool)
shm_pool_destroy(leaf->resize_pool);
memset(leaf, 0, sizeof *leaf);
}
#define MAX_LEAVES 3
struct shm_surface {
struct toysurface base;
struct display *display;
struct wl_surface *surface;
uint32_t flags;
int dx, dy;
struct shm_surface_leaf leaf[MAX_LEAVES];
struct shm_surface_leaf *current;
};
static struct shm_surface *
to_shm_surface(struct toysurface *base)
{
return container_of(base, struct shm_surface, base);
}
static void
shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
{
#ifdef DEBUG
struct shm_surface_leaf *leaf;
char bufs[MAX_LEAVES + 1];
int i;
for (i = 0; i < MAX_LEAVES; i++) {
leaf = &surface->leaf[i];
if (leaf->busy)
bufs[i] = 'b';
else if (leaf->cairo_surface)
bufs[i] = 'a';
else
bufs[i] = ' ';
}
bufs[MAX_LEAVES] = '\0';
DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
#endif
}
static void
shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
{
struct shm_surface *surface = data;
struct shm_surface_leaf *leaf;
int i;
int free_found;
shm_surface_buffer_state_debug(surface, "buffer_release before");
for (i = 0; i < MAX_LEAVES; i++) {
leaf = &surface->leaf[i];
if (leaf->data && leaf->data->buffer == buffer) {
leaf->busy = 0;
break;
}
}
assert(i < MAX_LEAVES && "unknown buffer released");
/* Leave one free leaf with storage, release others */
free_found = 0;
for (i = 0; i < MAX_LEAVES; i++) {
leaf = &surface->leaf[i];
if (!leaf->cairo_surface || leaf->busy)
continue;
if (!free_found)
free_found = 1;
else
shm_surface_leaf_release(leaf);
}
shm_surface_buffer_state_debug(surface, "buffer_release after");
}
static const struct wl_buffer_listener shm_surface_buffer_listener = {
shm_surface_buffer_release
};
static cairo_surface_t *
shm_surface_prepare(struct toysurface *base, int dx, int dy,
int32_t width, int32_t height, uint32_t flags,
enum wl_output_transform buffer_transform, int32_t buffer_scale)
{
int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
struct shm_surface *surface = to_shm_surface(base);
struct rectangle rect = { 0};
struct shm_surface_leaf *leaf = NULL;
int i;
surface->dx = dx;
surface->dy = dy;
/* pick a free buffer, preferably one that already has storage */
for (i = 0; i < MAX_LEAVES; i++) {
if (surface->leaf[i].busy)
continue;
if (!leaf || surface->leaf[i].cairo_surface)
leaf = &surface->leaf[i];
}
DBG_OBJ(surface->surface, "pick leaf %d\n",
(int)(leaf - &surface->leaf[0]));
if (!leaf) {
fprintf(stderr, "%s: all buffers are held by the server.\n",
__func__);
exit(1);
return NULL;
}
if (!resize_hint && leaf->resize_pool) {
cairo_surface_destroy(leaf->cairo_surface);
leaf->cairo_surface = NULL;
shm_pool_destroy(leaf->resize_pool);
leaf->resize_pool = NULL;
}
surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
if (leaf->cairo_surface &&
cairo_image_surface_get_width(leaf->cairo_surface) == width &&
cairo_image_surface_get_height(leaf->cairo_surface) == height)
goto out;
if (leaf->cairo_surface)
cairo_surface_destroy(leaf->cairo_surface);
#ifdef USE_RESIZE_POOL
if (resize_hint && !leaf->resize_pool) {
/* Create a big pool to allocate from, while continuously
* resizing. Mmapping a new pool in the server
* is relatively expensive, so reusing a pool performs
* better, but may temporarily reserve unneeded memory.
*/
/* We should probably base this number on the output size. */
leaf->resize_pool = shm_pool_create(surface->display,
6 * 1024 * 1024);
}
#endif
rect.width = width;
rect.height = height;
leaf->cairo_surface =
display_create_shm_surface(surface->display, &rect,
surface->flags,
leaf->resize_pool,
&leaf->data);
if (!leaf->cairo_surface)
return NULL;
wl_buffer_add_listener(leaf->data->buffer,
&shm_surface_buffer_listener, surface);
out:
surface->current = leaf;
return cairo_surface_reference(leaf->cairo_surface);
}
static void
shm_surface_swap(struct toysurface *base,
enum wl_output_transform buffer_transform, int32_t buffer_scale,
struct rectangle *server_allocation)
{
struct shm_surface *surface = to_shm_surface(base);
struct shm_surface_leaf *leaf = surface->current;
server_allocation->width =
cairo_image_surface_get_width(leaf->cairo_surface);
server_allocation->height =
cairo_image_surface_get_height(leaf->cairo_surface);
buffer_to_surface_size (buffer_transform, buffer_scale,
&server_allocation->width,
&server_allocation->height);
wl_surface_attach(surface->surface, leaf->data->buffer,
surface->dx, surface->dy);
wl_surface_damage(surface->surface, 0, 0,
server_allocation->width, server_allocation->height);
wl_surface_commit(surface->surface);
DBG_OBJ(surface->surface, "leaf %d busy\n",
(int)(leaf - &surface->leaf[0]));
leaf->busy = 1;
surface->current = NULL;
}
static int
shm_surface_acquire(struct toysurface *base, EGLContext ctx)
{
return -1;
}
static void
shm_surface_release(struct toysurface *base)
{
}
static void
shm_surface_destroy(struct toysurface *base)
{
struct shm_surface *surface = to_shm_surface(base);
int i;
for (i = 0; i < MAX_LEAVES; i++)
shm_surface_leaf_release(&surface->leaf[i]);
free(surface);
}
static struct toysurface *
shm_surface_create(struct display *display, struct wl_surface *wl_surface,
uint32_t flags, struct rectangle *rectangle)
{
struct shm_surface *surface;
DBG_OBJ(wl_surface, "\n");
surface = xzalloc(sizeof *surface);
surface->base.prepare = shm_surface_prepare;
surface->base.swap = shm_surface_swap;
surface->base.acquire = shm_surface_acquire;
surface->base.release = shm_surface_release;
surface->base.destroy = shm_surface_destroy;
surface->display = display;
surface->surface = wl_surface;
surface->flags = flags;
return &surface->base;
}
/*
* The following correspondences between file names and cursors was copied
* from: https://bugs.kde.org/attachment.cgi?id=67313
*/
static const char *bottom_left_corners[] = {
"bottom_left_corner",
"sw-resize",
"size_bdiag"
};
static const char *bottom_right_corners[] = {
"bottom_right_corner",
"se-resize",
"size_fdiag"
};
static const char *bottom_sides[] = {
"bottom_side",
"s-resize",
"size_ver"
};
static const char *grabbings[] = {
"grabbing",
"closedhand",
"208530c400c041818281048008011002"
};
static const char *left_ptrs[] = {
"left_ptr",
"default",
"top_left_arrow",
"left-arrow"
};
static const char *left_sides[] = {
"left_side",
"w-resize",
"size_hor"
};
static const char *right_sides[] = {
"right_side",
"e-resize",
"size_hor"
};
static const char *top_left_corners[] = {
"top_left_corner",
"nw-resize",
"size_fdiag"
};
static const char *top_right_corners[] = {
"top_right_corner",
"ne-resize",
"size_bdiag"
};
static const char *top_sides[] = {
"top_side",
"n-resize",
"size_ver"
};
static const char *xterms[] = {
"xterm",
"ibeam",
"text"
};
static const char *hand1s[] = {
"hand1",
"pointer",
"pointing_hand",
"e29285e634086352946a0e7090d73106"
};
static const char *watches[] = {
"watch",
"wait",
"0426c94ea35c87780ff01dc239897213"
};
static const char *move_draggings[] = {
"dnd-move"
};
static const char *copy_draggings[] = {
"dnd-copy"
};
static const char *forbidden_draggings[] = {
"dnd-none",
"dnd-no-drop"
};
struct cursor_alternatives {
const char **names;
size_t count;
};
static const struct cursor_alternatives cursors[] = {
{bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
{bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
{bottom_sides, ARRAY_LENGTH(bottom_sides)},
{grabbings, ARRAY_LENGTH(grabbings)},
{left_ptrs, ARRAY_LENGTH(left_ptrs)},
{left_sides, ARRAY_LENGTH(left_sides)},
{right_sides, ARRAY_LENGTH(right_sides)},
{top_left_corners, ARRAY_LENGTH(top_left_corners)},
{top_right_corners, ARRAY_LENGTH(top_right_corners)},
{top_sides, ARRAY_LENGTH(top_sides)},
{xterms, ARRAY_LENGTH(xterms)},
{hand1s, ARRAY_LENGTH(hand1s)},
{watches, ARRAY_LENGTH(watches)},
{move_draggings, ARRAY_LENGTH(move_draggings)},
{copy_draggings, ARRAY_LENGTH(copy_draggings)},
{forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
};
static void
create_cursors(struct display *display)
{
const char *config_file;
struct weston_config *config;
struct weston_config_section *s;
int size;
char *theme = NULL;
unsigned int i, j;
struct wl_cursor *cursor;
config_file = weston_config_get_name_from_env();
config = weston_config_parse(config_file);
s = weston_config_get_section(config, "shell", NULL, NULL);
weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
weston_config_section_get_int(s, "cursor-size", &size, 32);
weston_config_destroy(config);
display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
if (!display->cursor_theme) {
fprintf(stderr, "could not load theme '%s'\n", theme);
return;
}
free(theme);
display->cursors =
xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
cursor = NULL;
for (j = 0; !cursor && j < cursors[i].count; ++j)
cursor = wl_cursor_theme_get_cursor(
display->cursor_theme, cursors[i].names[j]);
if (!cursor)
fprintf(stderr, "could not load cursor '%s'\n",
cursors[i].names[0]);
display->cursors[i] = cursor;
}
}
static void
destroy_cursors(struct display *display)
{
wl_cursor_theme_destroy(display->cursor_theme);
free(display->cursors);
}
struct wl_cursor_image *
display_get_pointer_image(struct display *display, int pointer)
{
struct wl_cursor *cursor = display->cursors[pointer];
return cursor ? cursor->images[0] : NULL;
}
static void
surface_flush(struct surface *surface)
{
if (!surface->cairo_surface)
return;
if (surface->opaque_region) {
wl_surface_set_opaque_region(surface->surface,
surface->opaque_region);
wl_region_destroy(surface->opaque_region);
surface->opaque_region = NULL;
}
if (surface->input_region) {
wl_surface_set_input_region(surface->surface,
surface->input_region);
wl_region_destroy(surface->input_region);
surface->input_region = NULL;
}
surface->toysurface->swap(surface->toysurface,
surface->buffer_transform, surface->buffer_scale,
&surface->server_allocation);
cairo_surface_destroy(surface->cairo_surface);
surface->cairo_surface = NULL;
}
int
window_has_focus(struct window *window)
{
return window->focused;
}
static void
window_close(struct window *window)
{
if (window->close_handler)
window->close_handler(window->user_data);
else
display_exit(window->display);
}
struct display *
window_get_display(struct window *window)
{
return window->display;
}
static void
handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
int32_t width, int32_t height)
{
struct window *window = data;
window_schedule_resize(window, width, height);
}
static const struct ivi_surface_listener ivi_surface_listener = {
handle_ivi_surface_configure,
};
static void
surface_create_surface(struct surface *surface, uint32_t flags)
{
struct display *display = surface->window->display;
struct rectangle allocation = surface->allocation;
if (!surface->toysurface && display->dpy &&
surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
surface->toysurface =
egl_window_surface_create(display,
surface->surface,
flags,
&allocation);
}
if (!surface->toysurface)
surface->toysurface = shm_surface_create(display,
surface->surface,
flags, &allocation);
surface->cairo_surface = surface->toysurface->prepare(
surface->toysurface, 0, 0,
allocation.width, allocation.height, flags,
surface->buffer_transform, surface->buffer_scale);
}
static void
window_create_main_surface(struct window *window)
{
struct surface *surface = window->main_surface;
uint32_t flags = 0;
if (window->resizing)
flags |= SURFACE_HINT_RESIZE;
if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
flags |= SURFACE_HINT_RGB565;
surface_create_surface(surface, flags);
}
int
window_get_buffer_transform(struct window *window)
{
return window->main_surface->buffer_transform;
}
void
window_set_buffer_transform(struct window *window,
enum wl_output_transform transform)
{
window->main_surface->buffer_transform = transform;
wl_surface_set_buffer_transform(window->main_surface->surface,
transform);
}
void
window_set_buffer_scale(struct window *window,
int32_t scale)
{
window->main_surface->buffer_scale = scale;
wl_surface_set_buffer_scale(window->main_surface->surface,
scale);
}
uint32_t
window_get_buffer_scale(struct window *window)
{
return window->main_surface->buffer_scale;
}
uint32_t
window_get_output_scale(struct window *window)
{
struct window_output *window_output;
struct window_output *window_output_tmp;
int scale = 1;
wl_list_for_each_safe(window_output, window_output_tmp,
&window->window_output_list, link) {
if (window_output->output->scale > scale)
scale = window_output->output->scale;
}
return scale;
}
static void window_frame_destroy(struct window_frame *frame);
static void
surface_destroy(struct surface *surface)
{
if (surface->frame_cb)
wl_callback_destroy(surface->frame_cb);
if (surface->input_region)
wl_region_destroy(surface->input_region);
if (surface->opaque_region)
wl_region_destroy(surface->opaque_region);
if (surface->subsurface)
wl_subsurface_destroy(surface->subsurface);
wl_surface_destroy(surface->surface);
if (surface->toysurface)
surface->toysurface->destroy(surface->toysurface);
wl_list_remove(&surface->link);
free(surface);
}
void
window_destroy(struct window *window)
{
struct display *display = window->display;
struct input *input;
struct window_output *window_output;
struct window_output *window_output_tmp;
wl_list_remove(&window->redraw_task.link);
wl_list_for_each(input, &display->input_list, link) {
if (input->touch_focus == window)
input->touch_focus = NULL;
if (input->pointer_focus == window)
input->pointer_focus = NULL;
if (input->keyboard_focus == window)
input->keyboard_focus = NULL;
if (input->focus_widget &&
input->focus_widget->window == window)
input->focus_widget = NULL;
}
wl_list_for_each_safe(window_output, window_output_tmp,
&window->window_output_list, link) {
free (window_output);
}
if (window->frame)
window_frame_destroy(window->frame);
if (window->xdg_toplevel)
zxdg_toplevel_v6_destroy(window->xdg_toplevel);
if (window->xdg_popup)
zxdg_popup_v6_destroy(window->xdg_popup);
if (window->xdg_surface)
zxdg_surface_v6_destroy(window->xdg_surface);
if (window->ivi_surface)
ivi_surface_destroy(window->ivi_surface);
surface_destroy(window->main_surface);
wl_list_remove(&window->link);
free(window->title);
free(window);
}
static struct widget *
widget_find_widget(struct widget *widget, int32_t x, int32_t y)
{
struct widget *child, *target;
wl_list_for_each(child, &widget->child_list, link) {
target = widget_find_widget(child, x, y);
if (target)
return target;
}
if (widget->allocation.x <= x &&
x < widget->allocation.x + widget->allocation.width &&
widget->allocation.y <= y &&
y < widget->allocation.y + widget->allocation.height) {
return widget;
}
return NULL;
}
static struct widget *
window_find_widget(struct window *window, int32_t x, int32_t y)
{
struct surface *surface;
struct widget *widget;
wl_list_for_each(surface, &window->subsurface_list, link) {
widget = widget_find_widget(surface->widget, x, y);
if (widget)
return widget;
}
return NULL;
}
static struct widget *
widget_create(struct window *window, struct surface *surface, void *data)
{
struct widget *widget;
widget = xzalloc(sizeof *widget);
widget->window = window;
widget->surface = surface;
widget->user_data = data;
widget->allocation = surface->allocation;
wl_list_init(&widget->child_list);
widget->opaque = 0;
widget->tooltip = NULL;
widget->tooltip_count = 0;
widget->default_cursor = CURSOR_LEFT_PTR;
widget->use_cairo = 1;
return widget;
}
struct widget *
window_add_widget(struct window *window, void *data)
{
struct widget *widget;
widget = widget_create(window, window->main_surface, data);
wl_list_init(&widget->link);
window->main_surface->widget = widget;
return widget;
}
struct widget *
widget_add_widget(struct widget *parent, void *data)
{
struct widget *widget;
widget = widget_create(parent->window, parent->surface, data);
wl_list_insert(parent->child_list.prev, &widget->link);
return widget;
}
void
widget_destroy(struct widget *widget)
{
struct display *display = widget->window->display;
struct surface *surface = widget->surface;
struct input *input;
/* Destroy the sub-surface along with the root widget */
if (surface->widget == widget && surface->subsurface)
surface_destroy(widget->surface);
if (widget->tooltip)
widget_destroy_tooltip(widget);
wl_list_for_each(input, &display->input_list, link) {
if (input->focus_widget == widget)
input->focus_widget = NULL;
}
wl_list_remove(&widget->link);
free(widget);
}
void
widget_set_default_cursor(struct widget *widget, int cursor)
{
widget->default_cursor = cursor;
}
void
widget_get_allocation(struct widget *widget, struct rectangle *allocation)
{
*allocation = widget->allocation;
}
void
widget_set_size(struct widget *widget, int32_t width, int32_t height)
{
widget->allocation.width = width;
widget->allocation.height = height;
}
void
widget_set_allocation(struct widget *widget,
int32_t x, int32_t y, int32_t width, int32_t height)
{
widget->allocation.x = x;
widget->allocation.y = y;
widget_set_size(widget, width, height);
}
void
widget_set_transparent(struct widget *widget, int transparent)
{
widget->opaque = !transparent;
}
void *
widget_get_user_data(struct widget *widget)
{
return widget->user_data;
}
static cairo_surface_t *
widget_get_cairo_surface(struct widget *widget)
{
struct surface *surface = widget->surface;
struct window *window = widget->window;
assert(widget->use_cairo);
if (!surface->cairo_surface) {
if (surface == window->main_surface)
window_create_main_surface(window);
else
surface_create_surface(surface, 0);
}
return surface->cairo_surface;
}
static void
widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
{
struct surface *surface = widget->surface;
double angle;
cairo_matrix_t m;
enum wl_output_transform transform;
int surface_width, surface_height;
int translate_x, translate_y;
int32_t scale;
surface_width = surface->allocation.width;
surface_height = surface->allocation.height;
transform = surface->buffer_transform;
scale = surface->buffer_scale;
switch (transform) {
case WL_OUTPUT_TRANSFORM_FLIPPED:
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
break;
default:
cairo_matrix_init_identity(&m);
break;
}
switch (transform) {
case WL_OUTPUT_TRANSFORM_NORMAL:
default:
angle = 0;
translate_x = 0;
translate_y = 0;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED:
angle = 0;
translate_x = surface_width;
translate_y = 0;
break;
case WL_OUTPUT_TRANSFORM_90:
angle = M_PI_2;
translate_x = surface_height;
translate_y = 0;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
angle = M_PI_2;
translate_x = surface_height;
translate_y = surface_width;
break;
case WL_OUTPUT_TRANSFORM_180:
angle = M_PI;
translate_x = surface_width;
translate_y = surface_height;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
angle = M_PI;
translate_x = 0;
translate_y = surface_height;
break;
case WL_OUTPUT_TRANSFORM_270:
angle = M_PI + M_PI_2;
translate_x = 0;
translate_y = surface_width;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
angle = M_PI + M_PI_2;
translate_x = 0;
translate_y = 0;
break;
}
cairo_scale(cr, scale, scale);
cairo_translate(cr, translate_x, translate_y);
cairo_rotate(cr, angle);
cairo_transform(cr, &m);
}
cairo_t *
widget_cairo_create(struct widget *widget)
{
struct surface *surface = widget->surface;
cairo_surface_t *cairo_surface;
cairo_t *cr;
cairo_surface = widget_get_cairo_surface(widget);
cr = cairo_create(cairo_surface);
widget_cairo_update_transform(widget, cr);
cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
return cr;
}
struct wl_surface *
widget_get_wl_surface(struct widget *widget)
{
return widget->surface->surface;
}
struct wl_subsurface *
widget_get_wl_subsurface(struct widget *widget)
{
return widget->surface->subsurface;
}
uint32_t
widget_get_last_time(struct widget *widget)
{
return widget->surface->last_time;
}
void
widget_input_region_add(struct widget *widget, const struct rectangle *rect)
{
struct wl_compositor *comp = widget->window->display->compositor;
struct surface *surface = widget->surface;
if (!surface->input_region)
surface->input_region = wl_compositor_create_region(comp);
if (rect) {
wl_region_add(surface->input_region,
rect->x, rect->y, rect->width, rect->height);
}
}
void
widget_set_resize_handler(struct widget *widget,
widget_resize_handler_t handler)
{
widget->resize_handler = handler;
}
void
widget_set_redraw_handler(struct widget *widget,
widget_redraw_handler_t handler)
{
widget->redraw_handler = handler;
}
void
widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
{
widget->enter_handler = handler;
}
void
widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
{
widget->leave_handler = handler;
}
void
widget_set_motion_handler(struct widget *widget,
widget_motion_handler_t handler)
{
widget->motion_handler = handler;
}
void
widget_set_button_handler(struct widget *widget,
widget_button_handler_t handler)
{
widget->button_handler = handler;
}
void
widget_set_touch_up_handler(struct widget *widget,
widget_touch_up_handler_t handler)
{
widget->touch_up_handler = handler;
}
void
widget_set_touch_down_handler(struct widget *widget,
widget_touch_down_handler_t handler)
{
widget->touch_down_handler = handler;
}
void
widget_set_touch_motion_handler(struct widget *widget,
widget_touch_motion_handler_t handler)
{
widget->touch_motion_handler = handler;
}
void
widget_set_touch_frame_handler(struct widget *widget,
widget_touch_frame_handler_t handler)
{
widget->touch_frame_handler = handler;
}
void
widget_set_touch_cancel_handler(struct widget *widget,
widget_touch_cancel_handler_t handler)
{
widget->touch_cancel_handler = handler;
}
void
widget_set_axis_handler(struct widget *widget,
widget_axis_handler_t handler)
{
widget->axis_handler = handler;
}
void
widget_set_pointer_frame_handler(struct widget *widget,
widget_pointer_frame_handler_t handler)
{
widget->pointer_frame_handler = handler;
}
void
widget_set_axis_handlers(struct widget *widget,
widget_axis_handler_t axis_handler,
widget_axis_source_handler_t axis_source_handler,
widget_axis_stop_handler_t axis_stop_handler,
widget_axis_discrete_handler_t axis_discrete_handler)
{
widget->axis_handler = axis_handler;
widget->axis_source_handler = axis_source_handler;
widget->axis_stop_handler = axis_stop_handler;
widget->axis_discrete_handler = axis_discrete_handler;
}
static void
window_schedule_redraw_task(struct window *window);
void
widget_schedule_redraw(struct widget *widget)
{
DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
widget->surface->redraw_needed = 1;
window_schedule_redraw_task(widget->window);
}
void
widget_set_use_cairo(struct widget *widget,
int use_cairo)
{
widget->use_cairo = use_cairo;
}
cairo_surface_t *
window_get_surface(struct window *window)
{
cairo_surface_t *cairo_surface;
cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
return cairo_surface_reference(cairo_surface);
}
struct wl_surface *
window_get_wl_surface(struct window *window)
{
return window->main_surface->surface;
}
static void
tooltip_redraw_handler(struct widget *widget, void *data)
{
cairo_t *cr;
const int32_t r = 3;
struct tooltip *tooltip = data;
int32_t width, height;
cr = widget_cairo_create(widget);
cairo_translate(cr, widget->allocation.x, widget->allocation.y);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
cairo_paint(cr);
width = widget->allocation.width;
height = widget->allocation.height;
rounded_rect(cr, 0, 0, width, height, r);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
cairo_fill(cr);
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_move_to(cr, 10, 16);
cairo_show_text(cr, tooltip->entry);
cairo_destroy(cr);
}
static cairo_text_extents_t
get_text_extents(struct display *display, struct tooltip *tooltip)
{
cairo_t *cr;
cairo_text_extents_t extents;
/* Use the dummy_surface because the tooltip's surface was not
* created yet, and parent does not have a valid surface
* outside repaint, either.
*/
cr = cairo_create(display->dummy_surface);
cairo_text_extents(cr, tooltip->entry, &extents);
cairo_destroy(cr);
return extents;
}
static int
window_create_tooltip(struct tooltip *tooltip)
{
struct widget *parent = tooltip->parent;
struct display *display = parent->window->display;
const int offset_y = 27;
const int margin = 3;
cairo_text_extents_t extents;
if (tooltip->widget)
return 0;
tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
extents = get_text_extents(display, tooltip);
widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
widget_set_allocation(tooltip->widget,
tooltip->x, tooltip->y + offset_y,
extents.width + 20, 20 + margin * 2);
return 0;
}
void
widget_destroy_tooltip(struct widget *parent)
{
struct tooltip *tooltip = parent->tooltip;
parent->tooltip_count = 0;
if (!tooltip)
return;
if (tooltip->widget) {
widget_destroy(tooltip->widget);
tooltip->widget = NULL;
}
toytimer_fini(&tooltip->timer);
free(tooltip->entry);
free(tooltip);
parent->tooltip = NULL;
}
static void
tooltip_func(struct toytimer *tt)
{
struct tooltip *tooltip = container_of(tt, struct tooltip, timer);
window_create_tooltip(tooltip);
}
#define TOOLTIP_TIMEOUT 500
static int
tooltip_timer_reset(struct tooltip *tooltip)
{
toytimer_arm_once_usec(&tooltip->timer, TOOLTIP_TIMEOUT * 1000);
return 0;
}
int
widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
{
struct tooltip *tooltip = parent->tooltip;
parent->tooltip_count++;
if (tooltip) {
tooltip->x = x;
tooltip->y = y;
tooltip_timer_reset(tooltip);
return 0;
}
/* the handler might be triggered too fast via input device motion, so
* we need this check here to make sure tooltip is fully initialized */
if (parent->tooltip_count > 1)
return 0;
tooltip = malloc(sizeof *tooltip);
if (!tooltip)
return -1;
parent->tooltip = tooltip;
tooltip->parent = parent;
tooltip->widget = NULL;
tooltip->x = x;
tooltip->y = y;
tooltip->entry = strdup(entry);
toytimer_init(&tooltip->timer, CLOCK_MONOTONIC,
parent->window->display, tooltip_func);
tooltip_timer_reset(tooltip);
return 0;
}
static void
frame_resize_handler(struct widget *widget,
int32_t width, int32_t height, void *data)
{
struct window_frame *frame = data;
struct widget *child = frame->child;
struct rectangle interior;
struct rectangle input;
struct rectangle opaque;
if (widget->window->fullscreen) {
interior.x = 0;
interior.y = 0;
interior.width = width;
interior.height = height;
} else {
frame_resize(frame->frame, width, height);
frame_interior(frame->frame, &interior.x, &interior.y,
&interior.width, &interior.height);
}
widget_set_allocation(child, interior.x, interior.y,
interior.width, interior.height);
if (child->resize_handler) {
child->resize_handler(child, interior.width, interior.height,
child->user_data);
if (widget->window->fullscreen) {
width = child->allocation.width;
height = child->allocation.height;
} else {
frame_resize_inside(frame->frame,
child->allocation.width,
child->allocation.height);
width = frame_width(frame->frame);
height = frame_height(frame->frame);
}
}
widget_set_allocation(widget, 0, 0, width, height);
widget->surface->input_region =
wl_compositor_create_region(widget->window->display->compositor);
if (!widget->window->fullscreen) {
frame_input_rect(frame->frame, &input.x, &input.y,
&input.width, &input.height);
wl_region_add(widget->surface->input_region,
input.x, input.y, input.width, input.height);
} else {
wl_region_add(widget->surface->input_region, 0, 0, width, height);
}
widget_set_allocation(widget, 0, 0, width, height);
if (child->opaque) {
if (!widget->window->fullscreen) {
frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
&opaque.width, &opaque.height);
wl_region_add(widget->surface->opaque_region,
opaque.x, opaque.y,
opaque.width, opaque.height);
} else {
wl_region_add(widget->surface->opaque_region,
0, 0, width, height);
}
}
widget_schedule_redraw(widget);
}
static void
frame_redraw_handler(struct widget *widget, void *data)
{
cairo_t *cr;
struct window_frame *frame = data;
struct window *window = widget->window;
if (window->fullscreen)
return;
cr = widget_cairo_create(widget);
frame_repaint(frame->frame, cr);
cairo_destroy(cr);
}
static int
frame_get_pointer_image_for_location(struct window_frame *frame,
enum theme_location location)
{
struct window *window = frame->widget->window;
if (window->custom)
return CURSOR_LEFT_PTR;
switch (location) {
case THEME_LOCATION_RESIZING_TOP:
return CURSOR_TOP;
case THEME_LOCATION_RESIZING_BOTTOM:
return CURSOR_BOTTOM;
case THEME_LOCATION_RESIZING_LEFT:
return CURSOR_LEFT;
case THEME_LOCATION_RESIZING_RIGHT:
return CURSOR_RIGHT;
case THEME_LOCATION_RESIZING_TOP_LEFT:
return CURSOR_TOP_LEFT;
case THEME_LOCATION_RESIZING_TOP_RIGHT:
return CURSOR_TOP_RIGHT;
case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
return CURSOR_BOTTOM_LEFT;
case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
return CURSOR_BOTTOM_RIGHT;
case THEME_LOCATION_EXTERIOR:
case THEME_LOCATION_TITLEBAR:
default:
return CURSOR_LEFT_PTR;
}
}
static void
frame_menu_func(void *data, struct input *input, int index)
{
struct window *window = data;
switch (index) {
case 0: /* close */
window_close(window);
break;
case 1: /* fullscreen */
/* we don't have a way to get out of fullscreen for now */
if (window->fullscreen_handler)
window->fullscreen_handler(window, window->user_data);
break;
}
}
void
window_show_frame_menu(struct window *window,
struct input *input, uint32_t time)
{
int32_t x, y;
int count;
static const char *entries[] = {
"Close",
"Fullscreen"
};
if (window->fullscreen_handler)
count = ARRAY_LENGTH(entries);
else
count = ARRAY_LENGTH(entries) - 1;
input_get_position(input, &x, &y);
window_show_menu(window->display, input, time, window,
x - 10, y - 10, frame_menu_func, entries, count);
}
static int
frame_enter_handler(struct widget *widget,
struct input *input, float x, float y, void *data)
{
struct window_frame *frame = data;
enum theme_location location;
location = frame_pointer_enter(frame->frame, input, x, y);
if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
widget_schedule_redraw(frame->widget);
return frame_get_pointer_image_for_location(data, location);
}
static int
frame_motion_handler(struct widget *widget,
struct input *input, uint32_t time,
float x, float y, void *data)
{
struct window_frame *frame = data;
enum theme_location location;
location = frame_pointer_motion(frame->frame, input, x, y);
if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
widget_schedule_redraw(frame->widget);
return frame_get_pointer_image_for_location(data, location);
}
static void
frame_leave_handler(struct widget *widget,
struct input *input, void *data)
{
struct window_frame *frame = data;
frame_pointer_leave(frame->frame, input);
if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
widget_schedule_redraw(frame->widget);
}
static void
frame_handle_status(struct window_frame *frame, struct input *input,
uint32_t time, enum theme_location location)
{
struct window *window = frame->widget->window;
uint32_t status;
status = frame_status(frame->frame);
if (status & FRAME_STATUS_REPAINT)
widget_schedule_redraw(frame->widget);
if (status & FRAME_STATUS_MINIMIZE) {
window_set_minimized(window);
frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
}
if (status & FRAME_STATUS_MENU) {
window_show_frame_menu(window, input, time);
frame_status_clear(frame->frame, FRAME_STATUS_MENU);
}
if (status & FRAME_STATUS_MAXIMIZE) {
window_set_maximized(window, !window->maximized);
frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
}
if (status & FRAME_STATUS_CLOSE) {
window_close(window);
return;
}
if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
input_ungrab(input);
zxdg_toplevel_v6_move(window->xdg_toplevel,
input_get_seat(input),
window->display->serial);
frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
}
if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
input_ungrab(input);
zxdg_toplevel_v6_resize(window->xdg_toplevel,
input_get_seat(input),
window->display->serial,
location);
frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
}
}
#define DOUBLE_CLICK_PERIOD 250
static void
frame_button_handler(struct widget *widget,
struct input *input, uint32_t time,
uint32_t button, enum wl_pointer_button_state state,
void *data)
{
struct window_frame *frame = data;
enum theme_location location;
frame->double_click = 0;
if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
frame->double_click = 1;
frame->did_double = 1;
} else
frame->did_double = 0;
frame->last_time = time;
} else if (frame->did_double == 1) {
frame->double_click = 1;
frame->did_double = 0;
}
if (frame->double_click)
location = frame_double_click(frame->frame, input,
button, state);
else
location = frame_pointer_button(frame->frame, input,
button, state);
frame_handle_status(frame, input, time, location);
}
static void
frame_touch_down_handler(struct widget *widget, struct input *input,
uint32_t serial, uint32_t time, int32_t id,
float x, float y, void *data)
{
struct window_frame *frame = data;
frame->double_click = 0;
if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
frame->last_id == id) {
frame->double_click = 1;
frame->did_double = 1;
frame->double_id = id;
} else
frame->did_double = 0;
frame->last_time = time;
frame->last_id = id;
if (frame->double_click)
frame_double_touch_down(frame->frame, input, id, x, y);
else
frame_touch_down(frame->frame, input, id, x, y);
frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
}
static void
frame_touch_up_handler(struct widget *widget,
struct input *input, uint32_t serial, uint32_t time,
int32_t id, void *data)
{
struct window_frame *frame = data;
if (frame->double_id == id && frame->did_double) {
frame->did_double = 0;
frame->double_id = 0;
frame_double_touch_up(frame->frame, input, id);
} else
frame_touch_up(frame->frame, input, id);
frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
}
struct widget *
window_frame_create(struct window *window, void *data)
{
struct window_frame *frame;
uint32_t buttons;
if (window->custom) {
buttons = FRAME_BUTTON_NONE;
} else {
buttons = FRAME_BUTTON_ALL;
}
frame = xzalloc(sizeof *frame);
frame->frame = frame_create(window->display->theme, 0, 0,
buttons, window->title, NULL);
frame->widget = window_add_widget(window, frame);
frame->child = widget_add_widget(frame->widget, data);
widget_set_redraw_handler(frame->widget, frame_redraw_handler);
widget_set_resize_handler(frame->widget, frame_resize_handler);
widget_set_enter_handler(frame->widget, frame_enter_handler);
widget_set_leave_handler(frame->widget, frame_leave_handler);
widget_set_motion_handler(frame->widget, frame_motion_handler);
widget_set_button_handler(frame->widget, frame_button_handler);
widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
window->frame = frame;
return frame->child;
}
void
window_frame_set_child_size(struct widget *widget, int child_width,
int child_height)
{
struct display *display = widget->window->display;
struct theme *t = display->theme;
int decoration_width, decoration_height;
int width, height;
int margin = widget->window->maximized ? 0 : t->margin;
if (!widget->window->fullscreen) {
decoration_width = (t->width + margin) * 2;
decoration_height = t->width +
t->titlebar_height + margin * 2;
width = child_width + decoration_width;
height = child_height + decoration_height;
} else {
width = child_width;
height = child_height;
}
window_schedule_resize(widget->window, width, height);
}
static void
window_frame_destroy(struct window_frame *frame)
{
frame_destroy(frame->frame);
/* frame->child must be destroyed by the application */
widget_destroy(frame->widget);
free(frame);
}
static void
input_set_focus_widget(struct input *input, struct widget *focus,
float x, float y)
{
struct widget *old, *widget;
int cursor;
if (focus == input->focus_widget)
return;
old = input->focus_widget;
if (old) {
widget = old;
if (input->grab)
widget = input->grab;
if (widget->leave_handler)
widget->leave_handler(old, input, widget->user_data);
input->focus_widget = NULL;
}
if (focus) {
widget = focus;
if (input->grab)
widget = input->grab;
input->focus_widget = focus;
if (widget->enter_handler)
cursor = widget->enter_handler(focus, input, x, y,
widget->user_data);
else
cursor = widget->default_cursor;
input_set_pointer_image(input, cursor);
}
}
void
touch_grab(struct input *input, int32_t touch_id)
{
input->touch_grab = 1;
input->touch_grab_id = touch_id;
}
void
touch_ungrab(struct input *input)
{
struct touch_point *tp, *tmp;
input->touch_grab = 0;
wl_list_for_each_safe(tp, tmp,
&input->touch_point_list, link) {
if (tp->id != input->touch_grab_id)
continue;
wl_list_remove(&tp->link);
free(tp);
return;
}
}
void
input_grab(struct input *input, struct widget *widget, uint32_t button)
{
input->grab = widget;
input->grab_button = button;
input_set_focus_widget(input, widget, input->sx, input->sy);
}
void
input_ungrab(struct input *input)
{
struct widget *widget;
input->grab = NULL;
if (input->pointer_focus) {
widget = window_find_widget(input->pointer_focus,
input->sx, input->sy);
input_set_focus_widget(input, widget, input->sx, input->sy);
}
}
static void
cursor_delay_timer_reset(struct input *input, uint32_t duration)
{
if (!duration)
input->cursor_timer_running = false;
else
input->cursor_timer_running = true;
toytimer_arm_once_usec(&input->cursor_timer, duration * 1000);
}
static void cancel_pointer_image_update(struct input *input)
{
if (input->cursor_timer_running)
cursor_delay_timer_reset(input, 0);
}
static void
input_remove_pointer_focus(struct input *input)
{
struct window *window = input->pointer_focus;
if (!window)
return;
input_set_focus_widget(input, NULL, 0, 0);
input->pointer_focus = NULL;
input->current_cursor = CURSOR_UNSET;
cancel_pointer_image_update(input);
}
static void
pointer_handle_enter(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface,
wl_fixed_t sx_w, wl_fixed_t sy_w)
{
struct input *input = data;
struct window *window;
struct widget *widget;
float sx = wl_fixed_to_double(sx_w);
float sy = wl_fixed_to_double(sy_w);
if (!surface) {
/* enter event for a window we've just destroyed */
return;
}
window = wl_surface_get_user_data(surface);
if (surface != window->main_surface->surface) {
DBG("Ignoring input event from subsurface %p\n", surface);
return;
}
input->display->serial = serial;
input->pointer_enter_serial = serial;
input->pointer_focus = window;
input->sx = sx;
input->sy = sy;
widget = window_find_widget(window, sx, sy);
input_set_focus_widget(input, widget, sx, sy);
}
static void
pointer_handle_leave(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface)
{
struct input *input = data;
input->display->serial = serial;
input_remove_pointer_focus(input);
}
static void
pointer_handle_motion(void *data, struct wl_pointer *pointer,
uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
{
struct input *input = data;
struct window *window = input->pointer_focus;
struct widget *widget;
int cursor;
float sx = wl_fixed_to_double(sx_w);
float sy = wl_fixed_to_double(sy_w);
if (!window)
return;
input->sx = sx;
input->sy = sy;
/* when making the window smaller - e.g. after an unmaximise we might
* still have a pending motion event that the compositor has picked
* based on the old surface dimensions. However, if we have an active
* grab, we expect to see input from outside the window anyway.
*/
if (!input->grab && (sx < window->main_surface->allocation.x ||
sy < window->main_surface->allocation.y ||
sx > window->main_surface->allocation.width ||
sy > window->main_surface->allocation.height))
return;
if (!(input->grab && input->grab_button)) {
widget = window_find_widget(window, sx, sy);
input_set_focus_widget(input, widget, sx, sy);
}
if (input->grab)
widget = input->grab;
else
widget = input->focus_widget;
if (widget) {
if (widget->motion_handler)
cursor = widget->motion_handler(input->focus_widget,
input, time, sx, sy,
widget->user_data);
else
cursor = widget->default_cursor;
} else
cursor = CURSOR_LEFT_PTR;
input_set_pointer_image(input, cursor);
}
static void
pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
uint32_t time, uint32_t button, uint32_t state_w)
{
struct input *input = data;
struct widget *widget;
enum wl_pointer_button_state state = state_w;
input->display->serial = serial;
if (input->focus_widget && input->grab == NULL &&
state == WL_POINTER_BUTTON_STATE_PRESSED)
input_grab(input, input->focus_widget, button);
widget = input->grab;
if (widget && widget->button_handler)
(*widget->button_handler)(widget,
input, time,
button, state,
input->grab->user_data);
if (input->grab && input->grab_button == button &&
state == WL_POINTER_BUTTON_STATE_RELEASED)
input_ungrab(input);
}
static void
pointer_handle_axis(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis, wl_fixed_t value)
{
struct input *input = data;
struct widget *widget;
widget = input->focus_widget;
if (input->grab)
widget = input->grab;
if (widget && widget->axis_handler)
(*widget->axis_handler)(widget,
input, time,
axis, value,
widget->user_data);
}
static void
pointer_handle_frame(void *data, struct wl_pointer *pointer)
{
struct input *input = data;
struct widget *widget;
widget = input->focus_widget;
if (input->grab)
widget = input->grab;
if (widget && widget->pointer_frame_handler)
(*widget->pointer_frame_handler)(widget,
input,
widget->user_data);
}
static void
pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
uint32_t source)
{
struct input *input = data;
struct widget *widget;
widget = input->focus_widget;
if (input->grab)
widget = input->grab;
if (widget && widget->axis_source_handler)
(*widget->axis_source_handler)(widget,
input,
source,
widget->user_data);
}
static void
pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis)
{
struct input *input = data;
struct widget *widget;
widget = input->focus_widget;
if (input->grab)
widget = input->grab;
if (widget && widget->axis_stop_handler)
(*widget->axis_stop_handler)(widget,
input, time,
axis,
widget->user_data);
}
static void
pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
uint32_t axis, int32_t discrete)
{
struct input *input = data;
struct widget *widget;
widget = input->focus_widget;
if (input->grab)
widget = input->grab;
if (widget && widget->axis_discrete_handler)
(*widget->axis_discrete_handler)(widget,
input,
axis,
discrete,
widget->user_data);
}
static const struct wl_pointer_listener pointer_listener = {
pointer_handle_enter,
pointer_handle_leave,
pointer_handle_motion,
pointer_handle_button,
pointer_handle_axis,
pointer_handle_frame,
pointer_handle_axis_source,
pointer_handle_axis_stop,
pointer_handle_axis_discrete,
};
static void
input_remove_keyboard_focus(struct input *input)
{
struct window *window = input->keyboard_focus;
toytimer_disarm(&input->repeat_timer);
if (!window)
return;
if (window->keyboard_focus_handler)
(*window->keyboard_focus_handler)(window, NULL,
window->user_data);
input->keyboard_focus = NULL;
}
static void
keyboard_repeat_func(struct toytimer *tt)
{
struct input *input = container_of(tt, struct input, repeat_timer);
struct window *window = input->keyboard_focus;
if (window && window->key_handler) {
(*window->key_handler)(window, input, input->repeat_time,
input->repeat_key, input->repeat_sym,
WL_KEYBOARD_KEY_STATE_PRESSED,
window->user_data);
}
}
static void
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
uint32_t format, int fd, uint32_t size)
{
struct input *input = data;
struct xkb_keymap *keymap;
struct xkb_state *state;
#ifdef HAVE_XKBCOMMON_COMPOSE
struct xkb_compose_table *compose_table;
struct xkb_compose_state *compose_state;
#endif
char *locale;
char *map_str;
if (!data) {
close(fd);
return;
}
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
return;
}
map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (map_str == MAP_FAILED) {
close(fd);
return;
}
/* Set up XKB keymap */
keymap = xkb_keymap_new_from_string(input->display->xkb_context,
map_str,
XKB_KEYMAP_FORMAT_TEXT_V1,
0);
munmap(map_str, size);
close(fd);
if (!keymap) {
fprintf(stderr, "failed to compile keymap\n");
return;
}
/* Set up XKB state */
state = xkb_state_new(keymap);
if (!state) {
fprintf(stderr, "failed to create XKB state\n");
xkb_keymap_unref(keymap);
return;
}
/* Look up the preferred locale, falling back to "C" as default */
if (!(locale = getenv("LC_ALL")))
if (!(locale = getenv("LC_CTYPE")))
if (!(locale = getenv("LANG")))
locale = "C";
/* Set up XKB compose table */
#ifdef HAVE_XKBCOMMON_COMPOSE
compose_table =
xkb_compose_table_new_from_locale(input->display->xkb_context,
locale,
XKB_COMPOSE_COMPILE_NO_FLAGS);
if (compose_table) {
/* Set up XKB compose state */
compose_state = xkb_compose_state_new(compose_table,
XKB_COMPOSE_STATE_NO_FLAGS);
if (compose_state) {
xkb_compose_state_unref(input->xkb.compose_state);
xkb_compose_table_unref(input->xkb.compose_table);
input->xkb.compose_state = compose_state;
input->xkb.compose_table = compose_table;
} else {
fprintf(stderr, "could not create XKB compose state. "
"Disabiling compose.\n");
xkb_compose_table_unref(compose_table);
compose_table = NULL;
}
} else {
fprintf(stderr, "could not create XKB compose table for locale '%s'. "
"Disabiling compose\n", locale);
}
#endif
xkb_keymap_unref(input->xkb.keymap);
xkb_state_unref(input->xkb.state);
input->xkb.keymap = keymap;
input->xkb.state = state;
input->xkb.control_mask =
1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
input->xkb.alt_mask =
1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
input->xkb.shift_mask =
1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
}
static void
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface,
struct wl_array *keys)
{
struct input *input = data;
struct window *window;
if (!surface) {
/* enter event for a window we've just destroyed */
return;
}
input->display->serial = serial;
input->keyboard_focus = wl_surface_get_user_data(surface);
window = input->keyboard_focus;
if (window->keyboard_focus_handler)
(*window->keyboard_focus_handler)(window,
input, window->user_data);
}
static void
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface)
{
struct input *input = data;
input->display->serial = serial;
input_remove_keyboard_focus(input);
}
/* Translate symbols appropriately if a compose sequence is being entered */
static xkb_keysym_t
process_key_press(xkb_keysym_t sym, struct input *input)
{
#ifdef HAVE_XKBCOMMON_COMPOSE
if (!input->xkb.compose_state)
return sym;
if (sym == XKB_KEY_NoSymbol)
return sym;
if (xkb_compose_state_feed(input->xkb.compose_state,
sym) != XKB_COMPOSE_FEED_ACCEPTED)
return sym;
switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
case XKB_COMPOSE_COMPOSING:
return XKB_KEY_NoSymbol;
case XKB_COMPOSE_COMPOSED:
return xkb_compose_state_get_one_sym(input->xkb.compose_state);
case XKB_COMPOSE_CANCELLED:
return XKB_KEY_NoSymbol;
case XKB_COMPOSE_NOTHING:
return sym;
default:
return sym;
}
#else
return sym;
#endif
}
static void
keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t time, uint32_t key,
uint32_t state_w)
{
struct input *input = data;
struct window *window = input->keyboard_focus;
uint32_t code, num_syms;
enum wl_keyboard_key_state state = state_w;
const xkb_keysym_t *syms;
xkb_keysym_t sym;
struct itimerspec its;
input->display->serial = serial;
code = key + 8;
if (!window || !input->xkb.state)
return;
/* We only use input grabs for pointer events for now, so just
* ignore key presses if a grab is active. We expand the key
* event delivery mechanism to route events to widgets to
* properly handle key grabs. In the meantime, this prevents
* key event delivery while a grab is active. */
if (input->grab && input->grab_button == 0)
return;
num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
sym = XKB_KEY_NoSymbol;
if (num_syms == 1)
sym = syms[0];
if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
window_set_maximized(window, !window->maximized);
} else if (sym == XKB_KEY_F11 &&
window->fullscreen_handler &&
state == WL_KEYBOARD_KEY_STATE_PRESSED) {
window->fullscreen_handler(window, window->user_data);
} else if (sym == XKB_KEY_F4 &&
input->modifiers == MOD_ALT_MASK &&
state == WL_KEYBOARD_KEY_STATE_PRESSED) {
window_close(window);
} else if (window->key_handler) {
if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
sym = process_key_press(sym, input);
(*window->key_handler)(window, input, time, key,
sym, state, window->user_data);
}
if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
key == input->repeat_key) {
toytimer_disarm(&input->repeat_timer);
} else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
xkb_keymap_key_repeats(input->xkb.keymap, code)) {
input->repeat_sym = sym;
input->repeat_key = key;
input->repeat_time = time;
its.it_interval.tv_sec = input->repeat_rate_sec;
its.it_interval.tv_nsec = input->repeat_rate_nsec;
its.it_value.tv_sec = input->repeat_delay_sec;
its.it_value.tv_nsec = input->repeat_delay_nsec;
toytimer_arm(&input->repeat_timer, &its);
}
}
static void
keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t mods_depressed,
uint32_t mods_latched, uint32_t mods_locked,
uint32_t group)
{
struct input *input = data;
xkb_mod_mask_t mask;
/* If we're not using a keymap, then we don't handle PC-style modifiers */
if (!input->xkb.keymap)
return;
xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
mods_locked, 0, 0, group);
mask = xkb_state_serialize_mods(input->xkb.state,
XKB_STATE_MODS_DEPRESSED |
XKB_STATE_MODS_LATCHED);
input->modifiers = 0;
if (mask & input->xkb.control_mask)