diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e9ec361 --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ +TOOLCHAIN_DIR = /opt/fsl-imx-wayland/5.10-hardknott +SDKTARGETSYSROOT = $(TOOLCHAIN_DIR)/sysroots/cortexa53-crypto-poky-linux + +CC_PATH = /opt/fsl-imx-wayland/5.10-hardknott/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux +CROSS_FREFIX = $(CC_PATH)/aarch64-poky-linux- +GCC = $(CROSS_FREFIX)gcc + +CFLAGS=-mcpu=cortex-a53 -march=armv8-a+crc+crypto -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security +#LDFLAGS=-lweston-client + +INCPATH=-I./protocol -I./shared -I./ \ + -I/opt/fsl-imx-wayland/5.10-hardknott/sysroots/cortexa53-crypto-poky-linux/usr/include/cairo\ + -I/opt/fsl-imx-wayland/5.10-hardknott/sysroots/cortexa53-crypto-poky-linux/usr/include/libweston-9\ + -I/opt/fsl-imx-wayland/5.10-hardknott/sysroots/cortexa53-crypto-poky-linux/usr/include/pixman-1\ + -I/opt/fsl-imx-wayland/5.10-hardknott/sysroots/cortexa53-crypto-poky-linux/usr/include/pango-1.0\ + -I/opt/fsl-imx-wayland/5.10-hardknott/sysroots/cortexa53-crypto-poky-linux/usr/include/glib-2.0\ + -I/opt/fsl-imx-wayland/5.10-hardknott/sysroots/cortexa53-crypto-poky-linux/usr/lib/glib-2.0/include\ + -I/opt/fsl-imx-wayland/5.10-hardknott/sysroots/cortexa53-crypto-poky-linux/usr/include/harfbuzz + + +all: + $(GCC) $(CFLAGS)--sysroot $(SDKTARGETSYSROOT) $(LDFLAGS) $(INCPATH) -o weston-screenshooter \ + screenshot.c \ + protocol/weston-screenshooter-protocol.c \ + shared/os-compatibility.c \ + shared/xalloc.c \ + shared/file-util.c \ + shared/cairo-util.c \ + window.c + +clean: + rm weston-screenshooter diff --git a/config.h b/config.h new file mode 100644 index 0000000..be0838d --- /dev/null +++ b/config.h @@ -0,0 +1,90 @@ +/* + * Autogenerated by the Meson build system. + * Do not edit, your changes will be lost. + */ + +#pragma once + +#define BINDIR "/usr/bin" + +#define BUILD_DRM_COMPOSITOR 1 + +#define BUILD_DRM_GBM 1 + +#define BUILD_FBDEV_COMPOSITOR 1 + +#define DATADIR "/usr/share" + +#define EGL_NO_X11 1 + +#define ENABLE_EGL 1 + +#define ENABLE_IMXG2D 1 + +#define ENABLE_IMXGPU 1 + +#define ENABLE_JUNIT_XML 1 + +#define ENABLE_OPENGL 1 + +#define HAVE_DBUS 1 + +#define HAVE_GBM_FD_IMPORT 1 + +#define HAVE_GBM_MODIFIERS 1 + +#define HAVE_INITGROUPS 1 + +#define HAVE_JPEG 1 + +#define HAVE_LINUX_SYNC_FILE_H 1 + +#define HAVE_MEMFD_CREATE 1 + +#define HAVE_MKOSTEMP 1 + +#define HAVE_PAM 1 + +#define HAVE_PANGO 1 + +#define HAVE_POSIX_FALLOCATE 1 + +#define HAVE_STRCHRNUL 1 + +#define HAVE_SYSTEMD_LOGIN 1 + +#define HAVE_SYSTEMD_LOGIN_209 1 + +#define HAVE_XKBCOMMON_COMPOSE 1 + +#define LIBEXECDIR "/usr/libexec" + +#define LIBWESTON_MODULEDIR "/usr/lib/libweston-9" + +#define MAJOR_IN_SYSMACROS 1 + +#define MESA_EGL_NO_X11_HEADERS 1 + +#define MODULEDIR "/usr/lib/weston" + +#define PACKAGE_BUGREPORT "https://gitlab.freedesktop.org/wayland/weston/issues/" + +#define PACKAGE_STRING "weston 9.0.0" + +#define PACKAGE_URL "https://wayland.freedesktop.org" + +#define PACKAGE_VERSION "9.0.0" + +#define TEST_GL_RENDERER 1 + +#define USE_RESIZE_POOL 1 + +#define VERSION "9.0.0" + +#define WESTON_NATIVE_BACKEND "drm-backend.so" + +#define WESTON_SHELL_CLIENT "weston-desktop-shell" + +#define _ALL_SOURCE 1 + +#define _GNU_SOURCE 1 diff --git a/env.sh b/env.sh new file mode 100644 index 0000000..60758d0 --- /dev/null +++ b/env.sh @@ -0,0 +1,3 @@ +cd /opt/fsl-imx-wayland/5.10-hardknott +source environment-setup-cortexa53-crypto-poky-linux +cd - diff --git a/protocol/pointer-constraints-unstable-v1-client-protocol.h b/protocol/pointer-constraints-unstable-v1-client-protocol.h new file mode 100644 index 0000000..773982b --- /dev/null +++ b/protocol/pointer-constraints-unstable-v1-client-protocol.h @@ -0,0 +1,662 @@ +/* Generated by wayland-scanner 1.19.0 */ + +#ifndef POINTER_CONSTRAINTS_UNSTABLE_V1_CLIENT_PROTOCOL_H +#define POINTER_CONSTRAINTS_UNSTABLE_V1_CLIENT_PROTOCOL_H + +#include +#include +#include "wayland-client.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @page page_pointer_constraints_unstable_v1 The pointer_constraints_unstable_v1 protocol + * protocol for constraining pointer motions + * + * @section page_desc_pointer_constraints_unstable_v1 Description + * + * This protocol specifies a set of interfaces used for adding constraints to + * the motion of a pointer. Possible constraints include confining pointer + * motions to a given region, or locking it to its current position. + * + * In order to constrain the pointer, a client must first bind the global + * interface "wp_pointer_constraints" which, if a compositor supports pointer + * constraints, is exposed by the registry. Using the bound global object, the + * client uses the request that corresponds to the type of constraint it wants + * to make. See wp_pointer_constraints for more details. + * + * Warning! The protocol described in this file is experimental and backward + * incompatible changes may be made. Backward compatible changes may be added + * together with the corresponding interface version bump. Backward + * incompatible changes are done by bumping the version number in the protocol + * and interface names and resetting the interface version. Once the protocol + * is to be declared stable, the 'z' prefix and the version number in the + * protocol and interface names are removed and the interface version number is + * reset. + * + * @section page_ifaces_pointer_constraints_unstable_v1 Interfaces + * - @subpage page_iface_zwp_pointer_constraints_v1 - constrain the movement of a pointer + * - @subpage page_iface_zwp_locked_pointer_v1 - receive relative pointer motion events + * - @subpage page_iface_zwp_confined_pointer_v1 - confined pointer object + * @section page_copyright_pointer_constraints_unstable_v1 Copyright + *
+ *
+ * Copyright © 2014      Jonas Ådahl
+ * Copyright © 2015      Red Hat Inc.
+ *
+ * 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.
+ * 
+ */ +struct wl_pointer; +struct wl_region; +struct wl_surface; +struct zwp_confined_pointer_v1; +struct zwp_locked_pointer_v1; +struct zwp_pointer_constraints_v1; + +#ifndef ZWP_POINTER_CONSTRAINTS_V1_INTERFACE +#define ZWP_POINTER_CONSTRAINTS_V1_INTERFACE +/** + * @page page_iface_zwp_pointer_constraints_v1 zwp_pointer_constraints_v1 + * @section page_iface_zwp_pointer_constraints_v1_desc Description + * + * The global interface exposing pointer constraining functionality. It + * exposes two requests: lock_pointer for locking the pointer to its + * position, and confine_pointer for locking the pointer to a region. + * + * The lock_pointer and confine_pointer requests create the objects + * wp_locked_pointer and wp_confined_pointer respectively, and the client can + * use these objects to interact with the lock. + * + * For any surface, only one lock or confinement may be active across all + * wl_pointer objects of the same seat. If a lock or confinement is requested + * when another lock or confinement is active or requested on the same surface + * and with any of the wl_pointer objects of the same seat, an + * 'already_constrained' error will be raised. + * @section page_iface_zwp_pointer_constraints_v1_api API + * See @ref iface_zwp_pointer_constraints_v1. + */ +/** + * @defgroup iface_zwp_pointer_constraints_v1 The zwp_pointer_constraints_v1 interface + * + * The global interface exposing pointer constraining functionality. It + * exposes two requests: lock_pointer for locking the pointer to its + * position, and confine_pointer for locking the pointer to a region. + * + * The lock_pointer and confine_pointer requests create the objects + * wp_locked_pointer and wp_confined_pointer respectively, and the client can + * use these objects to interact with the lock. + * + * For any surface, only one lock or confinement may be active across all + * wl_pointer objects of the same seat. If a lock or confinement is requested + * when another lock or confinement is active or requested on the same surface + * and with any of the wl_pointer objects of the same seat, an + * 'already_constrained' error will be raised. + */ +extern const struct wl_interface zwp_pointer_constraints_v1_interface; +#endif +#ifndef ZWP_LOCKED_POINTER_V1_INTERFACE +#define ZWP_LOCKED_POINTER_V1_INTERFACE +/** + * @page page_iface_zwp_locked_pointer_v1 zwp_locked_pointer_v1 + * @section page_iface_zwp_locked_pointer_v1_desc Description + * + * The wp_locked_pointer interface represents a locked pointer state. + * + * While the lock of this object is active, the wl_pointer objects of the + * associated seat will not emit any wl_pointer.motion events. + * + * This object will send the event 'locked' when the lock is activated. + * Whenever the lock is activated, it is guaranteed that the locked surface + * will already have received pointer focus and that the pointer will be + * within the region passed to the request creating this object. + * + * To unlock the pointer, send the destroy request. This will also destroy + * the wp_locked_pointer object. + * + * If the compositor decides to unlock the pointer the unlocked event is + * sent. See wp_locked_pointer.unlock for details. + * + * When unlocking, the compositor may warp the cursor position to the set + * cursor position hint. If it does, it will not result in any relative + * motion events emitted via wp_relative_pointer. + * + * If the surface the lock was requested on is destroyed and the lock is not + * yet activated, the wp_locked_pointer object is now defunct and must be + * destroyed. + * @section page_iface_zwp_locked_pointer_v1_api API + * See @ref iface_zwp_locked_pointer_v1. + */ +/** + * @defgroup iface_zwp_locked_pointer_v1 The zwp_locked_pointer_v1 interface + * + * The wp_locked_pointer interface represents a locked pointer state. + * + * While the lock of this object is active, the wl_pointer objects of the + * associated seat will not emit any wl_pointer.motion events. + * + * This object will send the event 'locked' when the lock is activated. + * Whenever the lock is activated, it is guaranteed that the locked surface + * will already have received pointer focus and that the pointer will be + * within the region passed to the request creating this object. + * + * To unlock the pointer, send the destroy request. This will also destroy + * the wp_locked_pointer object. + * + * If the compositor decides to unlock the pointer the unlocked event is + * sent. See wp_locked_pointer.unlock for details. + * + * When unlocking, the compositor may warp the cursor position to the set + * cursor position hint. If it does, it will not result in any relative + * motion events emitted via wp_relative_pointer. + * + * If the surface the lock was requested on is destroyed and the lock is not + * yet activated, the wp_locked_pointer object is now defunct and must be + * destroyed. + */ +extern const struct wl_interface zwp_locked_pointer_v1_interface; +#endif +#ifndef ZWP_CONFINED_POINTER_V1_INTERFACE +#define ZWP_CONFINED_POINTER_V1_INTERFACE +/** + * @page page_iface_zwp_confined_pointer_v1 zwp_confined_pointer_v1 + * @section page_iface_zwp_confined_pointer_v1_desc Description + * + * The wp_confined_pointer interface represents a confined pointer state. + * + * This object will send the event 'confined' when the confinement is + * activated. Whenever the confinement is activated, it is guaranteed that + * the surface the pointer is confined to will already have received pointer + * focus and that the pointer will be within the region passed to the request + * creating this object. It is up to the compositor to decide whether this + * requires some user interaction and if the pointer will warp to within the + * passed region if outside. + * + * To unconfine the pointer, send the destroy request. This will also destroy + * the wp_confined_pointer object. + * + * If the compositor decides to unconfine the pointer the unconfined event is + * sent. The wp_confined_pointer object is at this point defunct and should + * be destroyed. + * @section page_iface_zwp_confined_pointer_v1_api API + * See @ref iface_zwp_confined_pointer_v1. + */ +/** + * @defgroup iface_zwp_confined_pointer_v1 The zwp_confined_pointer_v1 interface + * + * The wp_confined_pointer interface represents a confined pointer state. + * + * This object will send the event 'confined' when the confinement is + * activated. Whenever the confinement is activated, it is guaranteed that + * the surface the pointer is confined to will already have received pointer + * focus and that the pointer will be within the region passed to the request + * creating this object. It is up to the compositor to decide whether this + * requires some user interaction and if the pointer will warp to within the + * passed region if outside. + * + * To unconfine the pointer, send the destroy request. This will also destroy + * the wp_confined_pointer object. + * + * If the compositor decides to unconfine the pointer the unconfined event is + * sent. The wp_confined_pointer object is at this point defunct and should + * be destroyed. + */ +extern const struct wl_interface zwp_confined_pointer_v1_interface; +#endif + +#ifndef ZWP_POINTER_CONSTRAINTS_V1_ERROR_ENUM +#define ZWP_POINTER_CONSTRAINTS_V1_ERROR_ENUM +/** + * @ingroup iface_zwp_pointer_constraints_v1 + * wp_pointer_constraints error values + * + * These errors can be emitted in response to wp_pointer_constraints + * requests. + */ +enum zwp_pointer_constraints_v1_error +{ + /** + * pointer constraint already requested on that surface + */ + ZWP_POINTER_CONSTRAINTS_V1_ERROR_ALREADY_CONSTRAINED = 1, +}; +#endif /* ZWP_POINTER_CONSTRAINTS_V1_ERROR_ENUM */ + +#ifndef ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ENUM +#define ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ENUM +/** + * @ingroup iface_zwp_pointer_constraints_v1 + * the pointer constraint may reactivate + * + * A persistent pointer constraint may again reactivate once it has + * been deactivated. See the corresponding deactivation event + * (wp_locked_pointer.unlocked and wp_confined_pointer.unconfined) for + * details. + */ +enum zwp_pointer_constraints_v1_lifetime +{ + ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT = 1, + ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT = 2, +}; +#endif /* ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ENUM */ + +#define ZWP_POINTER_CONSTRAINTS_V1_DESTROY 0 +#define ZWP_POINTER_CONSTRAINTS_V1_LOCK_POINTER 1 +#define ZWP_POINTER_CONSTRAINTS_V1_CONFINE_POINTER 2 + + +/** + * @ingroup iface_zwp_pointer_constraints_v1 + */ +#define ZWP_POINTER_CONSTRAINTS_V1_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_zwp_pointer_constraints_v1 + */ +#define ZWP_POINTER_CONSTRAINTS_V1_LOCK_POINTER_SINCE_VERSION 1 +/** + * @ingroup iface_zwp_pointer_constraints_v1 + */ +#define ZWP_POINTER_CONSTRAINTS_V1_CONFINE_POINTER_SINCE_VERSION 1 + +/** @ingroup iface_zwp_pointer_constraints_v1 */ +static inline void +zwp_pointer_constraints_v1_set_user_data(struct zwp_pointer_constraints_v1 *zwp_pointer_constraints_v1, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) zwp_pointer_constraints_v1, user_data); +} + +/** @ingroup iface_zwp_pointer_constraints_v1 */ +static inline void * +zwp_pointer_constraints_v1_get_user_data(struct zwp_pointer_constraints_v1 *zwp_pointer_constraints_v1) +{ + return wl_proxy_get_user_data((struct wl_proxy *) zwp_pointer_constraints_v1); +} + +static inline uint32_t +zwp_pointer_constraints_v1_get_version(struct zwp_pointer_constraints_v1 *zwp_pointer_constraints_v1) +{ + return wl_proxy_get_version((struct wl_proxy *) zwp_pointer_constraints_v1); +} + +/** + * @ingroup iface_zwp_pointer_constraints_v1 + * + * Used by the client to notify the server that it will no longer use this + * pointer constraints object. + */ +static inline void +zwp_pointer_constraints_v1_destroy(struct zwp_pointer_constraints_v1 *zwp_pointer_constraints_v1) +{ + wl_proxy_marshal((struct wl_proxy *) zwp_pointer_constraints_v1, + ZWP_POINTER_CONSTRAINTS_V1_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) zwp_pointer_constraints_v1); +} + +/** + * @ingroup iface_zwp_pointer_constraints_v1 + * + * The lock_pointer request lets the client request to disable movements of + * the virtual pointer (i.e. the cursor), effectively locking the pointer + * to a position. This request may not take effect immediately; in the + * future, when the compositor deems implementation-specific constraints + * are satisfied, the pointer lock will be activated and the compositor + * sends a locked event. + * + * The protocol provides no guarantee that the constraints are ever + * satisfied, and does not require the compositor to send an error if the + * constraints cannot ever be satisfied. It is thus possible to request a + * lock that will never activate. + * + * There may not be another pointer constraint of any kind requested or + * active on the surface for any of the wl_pointer objects of the seat of + * the passed pointer when requesting a lock. If there is, an error will be + * raised. See general pointer lock documentation for more details. + * + * The intersection of the region passed with this request and the input + * region of the surface is used to determine where the pointer must be + * in order for the lock to activate. It is up to the compositor whether to + * warp the pointer or require some kind of user interaction for the lock + * to activate. If the region is null the surface input region is used. + * + * A surface may receive pointer focus without the lock being activated. + * + * The request creates a new object wp_locked_pointer which is used to + * interact with the lock as well as receive updates about its state. See + * the the description of wp_locked_pointer for further information. + * + * Note that while a pointer is locked, the wl_pointer objects of the + * corresponding seat will not emit any wl_pointer.motion events, but + * relative motion events will still be emitted via wp_relative_pointer + * objects of the same seat. wl_pointer.axis and wl_pointer.button events + * are unaffected. + */ +static inline struct zwp_locked_pointer_v1 * +zwp_pointer_constraints_v1_lock_pointer(struct zwp_pointer_constraints_v1 *zwp_pointer_constraints_v1, struct wl_surface *surface, struct wl_pointer *pointer, struct wl_region *region, uint32_t lifetime) +{ + struct wl_proxy *id; + + id = wl_proxy_marshal_constructor((struct wl_proxy *) zwp_pointer_constraints_v1, + ZWP_POINTER_CONSTRAINTS_V1_LOCK_POINTER, &zwp_locked_pointer_v1_interface, NULL, surface, pointer, region, lifetime); + + return (struct zwp_locked_pointer_v1 *) id; +} + +/** + * @ingroup iface_zwp_pointer_constraints_v1 + * + * The confine_pointer request lets the client request to confine the + * pointer cursor to a given region. This request may not take effect + * immediately; in the future, when the compositor deems implementation- + * specific constraints are satisfied, the pointer confinement will be + * activated and the compositor sends a confined event. + * + * The intersection of the region passed with this request and the input + * region of the surface is used to determine where the pointer must be + * in order for the confinement to activate. It is up to the compositor + * whether to warp the pointer or require some kind of user interaction for + * the confinement to activate. If the region is null the surface input + * region is used. + * + * The request will create a new object wp_confined_pointer which is used + * to interact with the confinement as well as receive updates about its + * state. See the the description of wp_confined_pointer for further + * information. + */ +static inline struct zwp_confined_pointer_v1 * +zwp_pointer_constraints_v1_confine_pointer(struct zwp_pointer_constraints_v1 *zwp_pointer_constraints_v1, struct wl_surface *surface, struct wl_pointer *pointer, struct wl_region *region, uint32_t lifetime) +{ + struct wl_proxy *id; + + id = wl_proxy_marshal_constructor((struct wl_proxy *) zwp_pointer_constraints_v1, + ZWP_POINTER_CONSTRAINTS_V1_CONFINE_POINTER, &zwp_confined_pointer_v1_interface, NULL, surface, pointer, region, lifetime); + + return (struct zwp_confined_pointer_v1 *) id; +} + +/** + * @ingroup iface_zwp_locked_pointer_v1 + * @struct zwp_locked_pointer_v1_listener + */ +struct zwp_locked_pointer_v1_listener +{ + /** + * lock activation event + * + * Notification that the pointer lock of the seat's pointer is + * activated. + */ + void (*locked)(void *data, + struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1); + /** + * lock deactivation event + * + * Notification that the pointer lock of the seat's pointer is no + * longer active. If this is a oneshot pointer lock (see + * wp_pointer_constraints.lifetime) this object is now defunct and + * should be destroyed. If this is a persistent pointer lock (see + * wp_pointer_constraints.lifetime) this pointer lock may again + * reactivate in the future. + */ + void (*unlocked)(void *data, + struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1); +}; + +/** + * @ingroup iface_zwp_locked_pointer_v1 + */ +static inline int +zwp_locked_pointer_v1_add_listener(struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1, + const struct zwp_locked_pointer_v1_listener *listener, void *data) +{ + return wl_proxy_add_listener((struct wl_proxy *) zwp_locked_pointer_v1, + (void(* *) (void))listener, data); +} + +#define ZWP_LOCKED_POINTER_V1_DESTROY 0 +#define ZWP_LOCKED_POINTER_V1_SET_CURSOR_POSITION_HINT 1 +#define ZWP_LOCKED_POINTER_V1_SET_REGION 2 + +/** + * @ingroup iface_zwp_locked_pointer_v1 + */ +#define ZWP_LOCKED_POINTER_V1_LOCKED_SINCE_VERSION 1 +/** + * @ingroup iface_zwp_locked_pointer_v1 + */ +#define ZWP_LOCKED_POINTER_V1_UNLOCKED_SINCE_VERSION 1 + +/** + * @ingroup iface_zwp_locked_pointer_v1 + */ +#define ZWP_LOCKED_POINTER_V1_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_zwp_locked_pointer_v1 + */ +#define ZWP_LOCKED_POINTER_V1_SET_CURSOR_POSITION_HINT_SINCE_VERSION 1 +/** + * @ingroup iface_zwp_locked_pointer_v1 + */ +#define ZWP_LOCKED_POINTER_V1_SET_REGION_SINCE_VERSION 1 + +/** @ingroup iface_zwp_locked_pointer_v1 */ +static inline void +zwp_locked_pointer_v1_set_user_data(struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) zwp_locked_pointer_v1, user_data); +} + +/** @ingroup iface_zwp_locked_pointer_v1 */ +static inline void * +zwp_locked_pointer_v1_get_user_data(struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1) +{ + return wl_proxy_get_user_data((struct wl_proxy *) zwp_locked_pointer_v1); +} + +static inline uint32_t +zwp_locked_pointer_v1_get_version(struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1) +{ + return wl_proxy_get_version((struct wl_proxy *) zwp_locked_pointer_v1); +} + +/** + * @ingroup iface_zwp_locked_pointer_v1 + * + * Destroy the locked pointer object. If applicable, the compositor will + * unlock the pointer. + */ +static inline void +zwp_locked_pointer_v1_destroy(struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1) +{ + wl_proxy_marshal((struct wl_proxy *) zwp_locked_pointer_v1, + ZWP_LOCKED_POINTER_V1_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) zwp_locked_pointer_v1); +} + +/** + * @ingroup iface_zwp_locked_pointer_v1 + * + * Set the cursor position hint relative to the top left corner of the + * surface. + * + * If the client is drawing its own cursor, it should update the position + * hint to the position of its own cursor. A compositor may use this + * information to warp the pointer upon unlock in order to avoid pointer + * jumps. + * + * The cursor position hint is double buffered. The new hint will only take + * effect when the associated surface gets it pending state applied. See + * wl_surface.commit for details. + */ +static inline void +zwp_locked_pointer_v1_set_cursor_position_hint(struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1, wl_fixed_t surface_x, wl_fixed_t surface_y) +{ + wl_proxy_marshal((struct wl_proxy *) zwp_locked_pointer_v1, + ZWP_LOCKED_POINTER_V1_SET_CURSOR_POSITION_HINT, surface_x, surface_y); +} + +/** + * @ingroup iface_zwp_locked_pointer_v1 + * + * Set a new region used to lock the pointer. + * + * The new lock region is double-buffered. The new lock region will + * only take effect when the associated surface gets its pending state + * applied. See wl_surface.commit for details. + * + * For details about the lock region, see wp_locked_pointer. + */ +static inline void +zwp_locked_pointer_v1_set_region(struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1, struct wl_region *region) +{ + wl_proxy_marshal((struct wl_proxy *) zwp_locked_pointer_v1, + ZWP_LOCKED_POINTER_V1_SET_REGION, region); +} + +/** + * @ingroup iface_zwp_confined_pointer_v1 + * @struct zwp_confined_pointer_v1_listener + */ +struct zwp_confined_pointer_v1_listener +{ + /** + * pointer confined + * + * Notification that the pointer confinement of the seat's + * pointer is activated. + */ + void (*confined)(void *data, + struct zwp_confined_pointer_v1 *zwp_confined_pointer_v1); + /** + * pointer unconfined + * + * Notification that the pointer confinement of the seat's + * pointer is no longer active. If this is a oneshot pointer + * confinement (see wp_pointer_constraints.lifetime) this object is + * now defunct and should be destroyed. If this is a persistent + * pointer confinement (see wp_pointer_constraints.lifetime) this + * pointer confinement may again reactivate in the future. + */ + void (*unconfined)(void *data, + struct zwp_confined_pointer_v1 *zwp_confined_pointer_v1); +}; + +/** + * @ingroup iface_zwp_confined_pointer_v1 + */ +static inline int +zwp_confined_pointer_v1_add_listener(struct zwp_confined_pointer_v1 *zwp_confined_pointer_v1, + const struct zwp_confined_pointer_v1_listener *listener, void *data) +{ + return wl_proxy_add_listener((struct wl_proxy *) zwp_confined_pointer_v1, + (void(* *) (void))listener, data); +} + +#define ZWP_CONFINED_POINTER_V1_DESTROY 0 +#define ZWP_CONFINED_POINTER_V1_SET_REGION 1 + +/** + * @ingroup iface_zwp_confined_pointer_v1 + */ +#define ZWP_CONFINED_POINTER_V1_CONFINED_SINCE_VERSION 1 +/** + * @ingroup iface_zwp_confined_pointer_v1 + */ +#define ZWP_CONFINED_POINTER_V1_UNCONFINED_SINCE_VERSION 1 + +/** + * @ingroup iface_zwp_confined_pointer_v1 + */ +#define ZWP_CONFINED_POINTER_V1_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_zwp_confined_pointer_v1 + */ +#define ZWP_CONFINED_POINTER_V1_SET_REGION_SINCE_VERSION 1 + +/** @ingroup iface_zwp_confined_pointer_v1 */ +static inline void +zwp_confined_pointer_v1_set_user_data(struct zwp_confined_pointer_v1 *zwp_confined_pointer_v1, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) zwp_confined_pointer_v1, user_data); +} + +/** @ingroup iface_zwp_confined_pointer_v1 */ +static inline void * +zwp_confined_pointer_v1_get_user_data(struct zwp_confined_pointer_v1 *zwp_confined_pointer_v1) +{ + return wl_proxy_get_user_data((struct wl_proxy *) zwp_confined_pointer_v1); +} + +static inline uint32_t +zwp_confined_pointer_v1_get_version(struct zwp_confined_pointer_v1 *zwp_confined_pointer_v1) +{ + return wl_proxy_get_version((struct wl_proxy *) zwp_confined_pointer_v1); +} + +/** + * @ingroup iface_zwp_confined_pointer_v1 + * + * Destroy the confined pointer object. If applicable, the compositor will + * unconfine the pointer. + */ +static inline void +zwp_confined_pointer_v1_destroy(struct zwp_confined_pointer_v1 *zwp_confined_pointer_v1) +{ + wl_proxy_marshal((struct wl_proxy *) zwp_confined_pointer_v1, + ZWP_CONFINED_POINTER_V1_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) zwp_confined_pointer_v1); +} + +/** + * @ingroup iface_zwp_confined_pointer_v1 + * + * Set a new region used to confine the pointer. + * + * The new confine region is double-buffered. The new confine region will + * only take effect when the associated surface gets its pending state + * applied. See wl_surface.commit for details. + * + * If the confinement is active when the new confinement region is applied + * and the pointer ends up outside of newly applied region, the pointer may + * warped to a position within the new confinement region. If warped, a + * wl_pointer.motion event will be emitted, but no + * wp_relative_pointer.relative_motion event. + * + * The compositor may also, instead of using the new region, unconfine the + * pointer. + * + * For details about the confine region, see wp_confined_pointer. + */ +static inline void +zwp_confined_pointer_v1_set_region(struct zwp_confined_pointer_v1 *zwp_confined_pointer_v1, struct wl_region *region) +{ + wl_proxy_marshal((struct wl_proxy *) zwp_confined_pointer_v1, + ZWP_CONFINED_POINTER_V1_SET_REGION, region); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/protocol/pointer-constraints-unstable-v1-protocol.c b/protocol/pointer-constraints-unstable-v1-protocol.c new file mode 100644 index 0000000..9da919d --- /dev/null +++ b/protocol/pointer-constraints-unstable-v1-protocol.c @@ -0,0 +1,107 @@ +/* Generated by wayland-scanner 1.19.0 */ + +/* + * Copyright © 2014 Jonas Ådahl + * Copyright © 2015 Red Hat Inc. + * + * 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 +#include +#include "wayland-util.h" + +#ifndef __has_attribute +# define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ +#endif + +#if (__has_attribute(visibility) || defined(__GNUC__) && __GNUC__ >= 4) +#define WL_PRIVATE __attribute__ ((visibility("hidden"))) +#else +#define WL_PRIVATE +#endif + +extern const struct wl_interface wl_pointer_interface; +extern const struct wl_interface wl_region_interface; +extern const struct wl_interface wl_surface_interface; +extern const struct wl_interface zwp_confined_pointer_v1_interface; +extern const struct wl_interface zwp_locked_pointer_v1_interface; + +static const struct wl_interface *pointer_constraints_unstable_v1_types[] = { + NULL, + NULL, + &zwp_locked_pointer_v1_interface, + &wl_surface_interface, + &wl_pointer_interface, + &wl_region_interface, + NULL, + &zwp_confined_pointer_v1_interface, + &wl_surface_interface, + &wl_pointer_interface, + &wl_region_interface, + NULL, + &wl_region_interface, + &wl_region_interface, +}; + +static const struct wl_message zwp_pointer_constraints_v1_requests[] = { + { "destroy", "", pointer_constraints_unstable_v1_types + 0 }, + { "lock_pointer", "noo?ou", pointer_constraints_unstable_v1_types + 2 }, + { "confine_pointer", "noo?ou", pointer_constraints_unstable_v1_types + 7 }, +}; + +WL_PRIVATE const struct wl_interface zwp_pointer_constraints_v1_interface = { + "zwp_pointer_constraints_v1", 1, + 3, zwp_pointer_constraints_v1_requests, + 0, NULL, +}; + +static const struct wl_message zwp_locked_pointer_v1_requests[] = { + { "destroy", "", pointer_constraints_unstable_v1_types + 0 }, + { "set_cursor_position_hint", "ff", pointer_constraints_unstable_v1_types + 0 }, + { "set_region", "?o", pointer_constraints_unstable_v1_types + 12 }, +}; + +static const struct wl_message zwp_locked_pointer_v1_events[] = { + { "locked", "", pointer_constraints_unstable_v1_types + 0 }, + { "unlocked", "", pointer_constraints_unstable_v1_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface zwp_locked_pointer_v1_interface = { + "zwp_locked_pointer_v1", 1, + 3, zwp_locked_pointer_v1_requests, + 2, zwp_locked_pointer_v1_events, +}; + +static const struct wl_message zwp_confined_pointer_v1_requests[] = { + { "destroy", "", pointer_constraints_unstable_v1_types + 0 }, + { "set_region", "?o", pointer_constraints_unstable_v1_types + 13 }, +}; + +static const struct wl_message zwp_confined_pointer_v1_events[] = { + { "confined", "", pointer_constraints_unstable_v1_types + 0 }, + { "unconfined", "", pointer_constraints_unstable_v1_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface zwp_confined_pointer_v1_interface = { + "zwp_confined_pointer_v1", 1, + 2, zwp_confined_pointer_v1_requests, + 2, zwp_confined_pointer_v1_events, +}; diff --git a/protocol/relative-pointer-unstable-v1-client-protocol.h b/protocol/relative-pointer-unstable-v1-client-protocol.h new file mode 100644 index 0000000..eb2a581 --- /dev/null +++ b/protocol/relative-pointer-unstable-v1-client-protocol.h @@ -0,0 +1,302 @@ +/* Generated by wayland-scanner 1.19.0 */ + +#ifndef RELATIVE_POINTER_UNSTABLE_V1_CLIENT_PROTOCOL_H +#define RELATIVE_POINTER_UNSTABLE_V1_CLIENT_PROTOCOL_H + +#include +#include +#include "wayland-client.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @page page_relative_pointer_unstable_v1 The relative_pointer_unstable_v1 protocol + * protocol for relative pointer motion events + * + * @section page_desc_relative_pointer_unstable_v1 Description + * + * This protocol specifies a set of interfaces used for making clients able to + * receive relative pointer events not obstructed by barriers (such as the + * monitor edge or other pointer barriers). + * + * To start receiving relative pointer events, a client must first bind the + * global interface "wp_relative_pointer_manager" which, if a compositor + * supports relative pointer motion events, is exposed by the registry. After + * having created the relative pointer manager proxy object, the client uses + * it to create the actual relative pointer object using the + * "get_relative_pointer" request given a wl_pointer. The relative pointer + * motion events will then, when applicable, be transmitted via the proxy of + * the newly created relative pointer object. See the documentation of the + * relative pointer interface for more details. + * + * Warning! The protocol described in this file is experimental and backward + * incompatible changes may be made. Backward compatible changes may be added + * together with the corresponding interface version bump. Backward + * incompatible changes are done by bumping the version number in the protocol + * and interface names and resetting the interface version. Once the protocol + * is to be declared stable, the 'z' prefix and the version number in the + * protocol and interface names are removed and the interface version number is + * reset. + * + * @section page_ifaces_relative_pointer_unstable_v1 Interfaces + * - @subpage page_iface_zwp_relative_pointer_manager_v1 - get relative pointer objects + * - @subpage page_iface_zwp_relative_pointer_v1 - relative pointer object + * @section page_copyright_relative_pointer_unstable_v1 Copyright + *
+ *
+ * Copyright © 2014      Jonas Ådahl
+ * Copyright © 2015      Red Hat Inc.
+ *
+ * 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.
+ * 
+ */ +struct wl_pointer; +struct zwp_relative_pointer_manager_v1; +struct zwp_relative_pointer_v1; + +#ifndef ZWP_RELATIVE_POINTER_MANAGER_V1_INTERFACE +#define ZWP_RELATIVE_POINTER_MANAGER_V1_INTERFACE +/** + * @page page_iface_zwp_relative_pointer_manager_v1 zwp_relative_pointer_manager_v1 + * @section page_iface_zwp_relative_pointer_manager_v1_desc Description + * + * A global interface used for getting the relative pointer object for a + * given pointer. + * @section page_iface_zwp_relative_pointer_manager_v1_api API + * See @ref iface_zwp_relative_pointer_manager_v1. + */ +/** + * @defgroup iface_zwp_relative_pointer_manager_v1 The zwp_relative_pointer_manager_v1 interface + * + * A global interface used for getting the relative pointer object for a + * given pointer. + */ +extern const struct wl_interface zwp_relative_pointer_manager_v1_interface; +#endif +#ifndef ZWP_RELATIVE_POINTER_V1_INTERFACE +#define ZWP_RELATIVE_POINTER_V1_INTERFACE +/** + * @page page_iface_zwp_relative_pointer_v1 zwp_relative_pointer_v1 + * @section page_iface_zwp_relative_pointer_v1_desc Description + * + * A wp_relative_pointer object is an extension to the wl_pointer interface + * used for emitting relative pointer events. It shares the same focus as + * wl_pointer objects of the same seat and will only emit events when it has + * focus. + * @section page_iface_zwp_relative_pointer_v1_api API + * See @ref iface_zwp_relative_pointer_v1. + */ +/** + * @defgroup iface_zwp_relative_pointer_v1 The zwp_relative_pointer_v1 interface + * + * A wp_relative_pointer object is an extension to the wl_pointer interface + * used for emitting relative pointer events. It shares the same focus as + * wl_pointer objects of the same seat and will only emit events when it has + * focus. + */ +extern const struct wl_interface zwp_relative_pointer_v1_interface; +#endif + +#define ZWP_RELATIVE_POINTER_MANAGER_V1_DESTROY 0 +#define ZWP_RELATIVE_POINTER_MANAGER_V1_GET_RELATIVE_POINTER 1 + + +/** + * @ingroup iface_zwp_relative_pointer_manager_v1 + */ +#define ZWP_RELATIVE_POINTER_MANAGER_V1_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_zwp_relative_pointer_manager_v1 + */ +#define ZWP_RELATIVE_POINTER_MANAGER_V1_GET_RELATIVE_POINTER_SINCE_VERSION 1 + +/** @ingroup iface_zwp_relative_pointer_manager_v1 */ +static inline void +zwp_relative_pointer_manager_v1_set_user_data(struct zwp_relative_pointer_manager_v1 *zwp_relative_pointer_manager_v1, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) zwp_relative_pointer_manager_v1, user_data); +} + +/** @ingroup iface_zwp_relative_pointer_manager_v1 */ +static inline void * +zwp_relative_pointer_manager_v1_get_user_data(struct zwp_relative_pointer_manager_v1 *zwp_relative_pointer_manager_v1) +{ + return wl_proxy_get_user_data((struct wl_proxy *) zwp_relative_pointer_manager_v1); +} + +static inline uint32_t +zwp_relative_pointer_manager_v1_get_version(struct zwp_relative_pointer_manager_v1 *zwp_relative_pointer_manager_v1) +{ + return wl_proxy_get_version((struct wl_proxy *) zwp_relative_pointer_manager_v1); +} + +/** + * @ingroup iface_zwp_relative_pointer_manager_v1 + * + * Used by the client to notify the server that it will no longer use this + * relative pointer manager object. + */ +static inline void +zwp_relative_pointer_manager_v1_destroy(struct zwp_relative_pointer_manager_v1 *zwp_relative_pointer_manager_v1) +{ + wl_proxy_marshal((struct wl_proxy *) zwp_relative_pointer_manager_v1, + ZWP_RELATIVE_POINTER_MANAGER_V1_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) zwp_relative_pointer_manager_v1); +} + +/** + * @ingroup iface_zwp_relative_pointer_manager_v1 + * + * Create a relative pointer interface given a wl_pointer object. See the + * wp_relative_pointer interface for more details. + */ +static inline struct zwp_relative_pointer_v1 * +zwp_relative_pointer_manager_v1_get_relative_pointer(struct zwp_relative_pointer_manager_v1 *zwp_relative_pointer_manager_v1, struct wl_pointer *pointer) +{ + struct wl_proxy *id; + + id = wl_proxy_marshal_constructor((struct wl_proxy *) zwp_relative_pointer_manager_v1, + ZWP_RELATIVE_POINTER_MANAGER_V1_GET_RELATIVE_POINTER, &zwp_relative_pointer_v1_interface, NULL, pointer); + + return (struct zwp_relative_pointer_v1 *) id; +} + +/** + * @ingroup iface_zwp_relative_pointer_v1 + * @struct zwp_relative_pointer_v1_listener + */ +struct zwp_relative_pointer_v1_listener +{ + /** + * relative pointer motion + * + * Relative x/y pointer motion from the pointer of the seat + * associated with this object. + * + * A relative motion is in the same dimension as regular wl_pointer + * motion events, except they do not represent an absolute + * position. For example, moving a pointer from (x, y) to (x', y') + * would have the equivalent relative motion (x' - x, y' - y). If a + * pointer motion caused the absolute pointer position to be + * clipped by for example the edge of the monitor, the relative + * motion is unaffected by the clipping and will represent the + * unclipped motion. + * + * This event also contains non-accelerated motion deltas. The + * non-accelerated delta is, when applicable, the regular pointer + * motion delta as it was before having applied motion acceleration + * and other transformations such as normalization. + * + * Note that the non-accelerated delta does not represent 'raw' + * events as they were read from some device. Pointer motion + * acceleration is device- and configuration-specific and + * non-accelerated deltas and accelerated deltas may have the same + * value on some devices. + * + * Relative motions are not coupled to wl_pointer.motion events, + * and can be sent in combination with such events, but also + * independently. There may also be scenarios where + * wl_pointer.motion is sent, but there is no relative motion. The + * order of an absolute and relative motion event originating from + * the same physical motion is not guaranteed. + * + * If the client needs button events or focus state, it can receive + * them from a wl_pointer object of the same seat that the + * wp_relative_pointer object is associated with. + * @param utime_hi high 32 bits of a 64 bit timestamp with microsecond granularity + * @param utime_lo low 32 bits of a 64 bit timestamp with microsecond granularity + * @param dx the x component of the motion vector + * @param dy the y component of the motion vector + * @param dx_unaccel the x component of the unaccelerated motion vector + * @param dy_unaccel the y component of the unaccelerated motion vector + */ + void (*relative_motion)(void *data, + struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1, + uint32_t utime_hi, + uint32_t utime_lo, + wl_fixed_t dx, + wl_fixed_t dy, + wl_fixed_t dx_unaccel, + wl_fixed_t dy_unaccel); +}; + +/** + * @ingroup iface_zwp_relative_pointer_v1 + */ +static inline int +zwp_relative_pointer_v1_add_listener(struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1, + const struct zwp_relative_pointer_v1_listener *listener, void *data) +{ + return wl_proxy_add_listener((struct wl_proxy *) zwp_relative_pointer_v1, + (void(* *) (void))listener, data); +} + +#define ZWP_RELATIVE_POINTER_V1_DESTROY 0 + +/** + * @ingroup iface_zwp_relative_pointer_v1 + */ +#define ZWP_RELATIVE_POINTER_V1_RELATIVE_MOTION_SINCE_VERSION 1 + +/** + * @ingroup iface_zwp_relative_pointer_v1 + */ +#define ZWP_RELATIVE_POINTER_V1_DESTROY_SINCE_VERSION 1 + +/** @ingroup iface_zwp_relative_pointer_v1 */ +static inline void +zwp_relative_pointer_v1_set_user_data(struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) zwp_relative_pointer_v1, user_data); +} + +/** @ingroup iface_zwp_relative_pointer_v1 */ +static inline void * +zwp_relative_pointer_v1_get_user_data(struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1) +{ + return wl_proxy_get_user_data((struct wl_proxy *) zwp_relative_pointer_v1); +} + +static inline uint32_t +zwp_relative_pointer_v1_get_version(struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1) +{ + return wl_proxy_get_version((struct wl_proxy *) zwp_relative_pointer_v1); +} + +/** + * @ingroup iface_zwp_relative_pointer_v1 + */ +static inline void +zwp_relative_pointer_v1_destroy(struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1) +{ + wl_proxy_marshal((struct wl_proxy *) zwp_relative_pointer_v1, + ZWP_RELATIVE_POINTER_V1_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) zwp_relative_pointer_v1); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/protocol/relative-pointer-unstable-v1-protocol.c b/protocol/relative-pointer-unstable-v1-protocol.c new file mode 100644 index 0000000..5c4cd13 --- /dev/null +++ b/protocol/relative-pointer-unstable-v1-protocol.c @@ -0,0 +1,78 @@ +/* Generated by wayland-scanner 1.19.0 */ + +/* + * Copyright © 2014 Jonas Ådahl + * Copyright © 2015 Red Hat Inc. + * + * 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 +#include +#include "wayland-util.h" + +#ifndef __has_attribute +# define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ +#endif + +#if (__has_attribute(visibility) || defined(__GNUC__) && __GNUC__ >= 4) +#define WL_PRIVATE __attribute__ ((visibility("hidden"))) +#else +#define WL_PRIVATE +#endif + +extern const struct wl_interface wl_pointer_interface; +extern const struct wl_interface zwp_relative_pointer_v1_interface; + +static const struct wl_interface *relative_pointer_unstable_v1_types[] = { + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + &zwp_relative_pointer_v1_interface, + &wl_pointer_interface, +}; + +static const struct wl_message zwp_relative_pointer_manager_v1_requests[] = { + { "destroy", "", relative_pointer_unstable_v1_types + 0 }, + { "get_relative_pointer", "no", relative_pointer_unstable_v1_types + 6 }, +}; + +WL_PRIVATE const struct wl_interface zwp_relative_pointer_manager_v1_interface = { + "zwp_relative_pointer_manager_v1", 1, + 2, zwp_relative_pointer_manager_v1_requests, + 0, NULL, +}; + +static const struct wl_message zwp_relative_pointer_v1_requests[] = { + { "destroy", "", relative_pointer_unstable_v1_types + 0 }, +}; + +static const struct wl_message zwp_relative_pointer_v1_events[] = { + { "relative_motion", "uuffff", relative_pointer_unstable_v1_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface zwp_relative_pointer_v1_interface = { + "zwp_relative_pointer_v1", 1, + 1, zwp_relative_pointer_v1_requests, + 1, zwp_relative_pointer_v1_events, +}; diff --git a/protocol/text-cursor-position-client-protocol.h b/protocol/text-cursor-position-client-protocol.h new file mode 100644 index 0000000..f07a18d --- /dev/null +++ b/protocol/text-cursor-position-client-protocol.h @@ -0,0 +1,84 @@ +/* Generated by wayland-scanner 1.19.0 */ + +#ifndef TEXT_CURSOR_POSITION_CLIENT_PROTOCOL_H +#define TEXT_CURSOR_POSITION_CLIENT_PROTOCOL_H + +#include +#include +#include "wayland-client.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @page page_text_cursor_position The text_cursor_position protocol + * @section page_ifaces_text_cursor_position Interfaces + * - @subpage page_iface_text_cursor_position - + */ +struct text_cursor_position; +struct wl_surface; + +#ifndef TEXT_CURSOR_POSITION_INTERFACE +#define TEXT_CURSOR_POSITION_INTERFACE +/** + * @page page_iface_text_cursor_position text_cursor_position + * @section page_iface_text_cursor_position_api API + * See @ref iface_text_cursor_position. + */ +/** + * @defgroup iface_text_cursor_position The text_cursor_position interface + */ +extern const struct wl_interface text_cursor_position_interface; +#endif + +#define TEXT_CURSOR_POSITION_NOTIFY 0 + + +/** + * @ingroup iface_text_cursor_position + */ +#define TEXT_CURSOR_POSITION_NOTIFY_SINCE_VERSION 1 + +/** @ingroup iface_text_cursor_position */ +static inline void +text_cursor_position_set_user_data(struct text_cursor_position *text_cursor_position, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) text_cursor_position, user_data); +} + +/** @ingroup iface_text_cursor_position */ +static inline void * +text_cursor_position_get_user_data(struct text_cursor_position *text_cursor_position) +{ + return wl_proxy_get_user_data((struct wl_proxy *) text_cursor_position); +} + +static inline uint32_t +text_cursor_position_get_version(struct text_cursor_position *text_cursor_position) +{ + return wl_proxy_get_version((struct wl_proxy *) text_cursor_position); +} + +/** @ingroup iface_text_cursor_position */ +static inline void +text_cursor_position_destroy(struct text_cursor_position *text_cursor_position) +{ + wl_proxy_destroy((struct wl_proxy *) text_cursor_position); +} + +/** + * @ingroup iface_text_cursor_position + */ +static inline void +text_cursor_position_notify(struct text_cursor_position *text_cursor_position, struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y) +{ + wl_proxy_marshal((struct wl_proxy *) text_cursor_position, + TEXT_CURSOR_POSITION_NOTIFY, surface, x, y); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/protocol/text-cursor-position-protocol.c b/protocol/text-cursor-position-protocol.c new file mode 100644 index 0000000..55cc965 --- /dev/null +++ b/protocol/text-cursor-position-protocol.c @@ -0,0 +1,33 @@ +/* Generated by wayland-scanner 1.19.0 */ + +#include +#include +#include "wayland-util.h" + +#ifndef __has_attribute +# define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ +#endif + +#if (__has_attribute(visibility) || defined(__GNUC__) && __GNUC__ >= 4) +#define WL_PRIVATE __attribute__ ((visibility("hidden"))) +#else +#define WL_PRIVATE +#endif + +extern const struct wl_interface wl_surface_interface; + +static const struct wl_interface *text_cursor_position_types[] = { + &wl_surface_interface, + NULL, + NULL, +}; + +static const struct wl_message text_cursor_position_requests[] = { + { "notify", "off", text_cursor_position_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface text_cursor_position_interface = { + "text_cursor_position", 1, + 1, text_cursor_position_requests, + 0, NULL, +}; diff --git a/protocol/viewporter-client-protocol.h b/protocol/viewporter-client-protocol.h new file mode 100644 index 0000000..c9df82f --- /dev/null +++ b/protocol/viewporter-client-protocol.h @@ -0,0 +1,416 @@ +/* Generated by wayland-scanner 1.19.0 */ + +#ifndef VIEWPORTER_CLIENT_PROTOCOL_H +#define VIEWPORTER_CLIENT_PROTOCOL_H + +#include +#include +#include "wayland-client.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @page page_viewporter The viewporter protocol + * @section page_ifaces_viewporter Interfaces + * - @subpage page_iface_wp_viewporter - surface cropping and scaling + * - @subpage page_iface_wp_viewport - crop and scale interface to a wl_surface + * @section page_copyright_viewporter Copyright + *
+ *
+ * Copyright © 2013-2016 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.
+ * 
+ */ +struct wl_surface; +struct wp_viewport; +struct wp_viewporter; + +#ifndef WP_VIEWPORTER_INTERFACE +#define WP_VIEWPORTER_INTERFACE +/** + * @page page_iface_wp_viewporter wp_viewporter + * @section page_iface_wp_viewporter_desc Description + * + * The global interface exposing surface cropping and scaling + * capabilities is used to instantiate an interface extension for a + * wl_surface object. This extended interface will then allow + * cropping and scaling the surface contents, effectively + * disconnecting the direct relationship between the buffer and the + * surface size. + * @section page_iface_wp_viewporter_api API + * See @ref iface_wp_viewporter. + */ +/** + * @defgroup iface_wp_viewporter The wp_viewporter interface + * + * The global interface exposing surface cropping and scaling + * capabilities is used to instantiate an interface extension for a + * wl_surface object. This extended interface will then allow + * cropping and scaling the surface contents, effectively + * disconnecting the direct relationship between the buffer and the + * surface size. + */ +extern const struct wl_interface wp_viewporter_interface; +#endif +#ifndef WP_VIEWPORT_INTERFACE +#define WP_VIEWPORT_INTERFACE +/** + * @page page_iface_wp_viewport wp_viewport + * @section page_iface_wp_viewport_desc Description + * + * An additional interface to a wl_surface object, which allows the + * client to specify the cropping and scaling of the surface + * contents. + * + * This interface works with two concepts: the source rectangle (src_x, + * src_y, src_width, src_height), and the destination size (dst_width, + * dst_height). The contents of the source rectangle are scaled to the + * destination size, and content outside the source rectangle is ignored. + * This state is double-buffered, and is applied on the next + * wl_surface.commit. + * + * The two parts of crop and scale state are independent: the source + * rectangle, and the destination size. Initially both are unset, that + * is, no scaling is applied. The whole of the current wl_buffer is + * used as the source, and the surface size is as defined in + * wl_surface.attach. + * + * If the destination size is set, it causes the surface size to become + * dst_width, dst_height. The source (rectangle) is scaled to exactly + * this size. This overrides whatever the attached wl_buffer size is, + * unless the wl_buffer is NULL. If the wl_buffer is NULL, the surface + * has no content and therefore no size. Otherwise, the size is always + * at least 1x1 in surface local coordinates. + * + * If the source rectangle is set, it defines what area of the wl_buffer is + * taken as the source. If the source rectangle is set and the destination + * size is not set, then src_width and src_height must be integers, and the + * surface size becomes the source rectangle size. This results in cropping + * without scaling. If src_width or src_height are not integers and + * destination size is not set, the bad_size protocol error is raised when + * the surface state is applied. + * + * The coordinate transformations from buffer pixel coordinates up to + * the surface-local coordinates happen in the following order: + * 1. buffer_transform (wl_surface.set_buffer_transform) + * 2. buffer_scale (wl_surface.set_buffer_scale) + * 3. crop and scale (wp_viewport.set*) + * This means, that the source rectangle coordinates of crop and scale + * are given in the coordinates after the buffer transform and scale, + * i.e. in the coordinates that would be the surface-local coordinates + * if the crop and scale was not applied. + * + * If src_x or src_y are negative, the bad_value protocol error is raised. + * Otherwise, if the source rectangle is partially or completely outside of + * the non-NULL wl_buffer, then the out_of_buffer protocol error is raised + * when the surface state is applied. A NULL wl_buffer does not raise the + * out_of_buffer error. + * + * The x, y arguments of wl_surface.attach are applied as normal to + * the surface. They indicate how many pixels to remove from the + * surface size from the left and the top. In other words, they are + * still in the surface-local coordinate system, just like dst_width + * and dst_height are. + * + * If the wl_surface associated with the wp_viewport is destroyed, + * all wp_viewport requests except 'destroy' raise the protocol error + * no_surface. + * + * If the wp_viewport object is destroyed, the crop and scale + * state is removed from the wl_surface. The change will be applied + * on the next wl_surface.commit. + * @section page_iface_wp_viewport_api API + * See @ref iface_wp_viewport. + */ +/** + * @defgroup iface_wp_viewport The wp_viewport interface + * + * An additional interface to a wl_surface object, which allows the + * client to specify the cropping and scaling of the surface + * contents. + * + * This interface works with two concepts: the source rectangle (src_x, + * src_y, src_width, src_height), and the destination size (dst_width, + * dst_height). The contents of the source rectangle are scaled to the + * destination size, and content outside the source rectangle is ignored. + * This state is double-buffered, and is applied on the next + * wl_surface.commit. + * + * The two parts of crop and scale state are independent: the source + * rectangle, and the destination size. Initially both are unset, that + * is, no scaling is applied. The whole of the current wl_buffer is + * used as the source, and the surface size is as defined in + * wl_surface.attach. + * + * If the destination size is set, it causes the surface size to become + * dst_width, dst_height. The source (rectangle) is scaled to exactly + * this size. This overrides whatever the attached wl_buffer size is, + * unless the wl_buffer is NULL. If the wl_buffer is NULL, the surface + * has no content and therefore no size. Otherwise, the size is always + * at least 1x1 in surface local coordinates. + * + * If the source rectangle is set, it defines what area of the wl_buffer is + * taken as the source. If the source rectangle is set and the destination + * size is not set, then src_width and src_height must be integers, and the + * surface size becomes the source rectangle size. This results in cropping + * without scaling. If src_width or src_height are not integers and + * destination size is not set, the bad_size protocol error is raised when + * the surface state is applied. + * + * The coordinate transformations from buffer pixel coordinates up to + * the surface-local coordinates happen in the following order: + * 1. buffer_transform (wl_surface.set_buffer_transform) + * 2. buffer_scale (wl_surface.set_buffer_scale) + * 3. crop and scale (wp_viewport.set*) + * This means, that the source rectangle coordinates of crop and scale + * are given in the coordinates after the buffer transform and scale, + * i.e. in the coordinates that would be the surface-local coordinates + * if the crop and scale was not applied. + * + * If src_x or src_y are negative, the bad_value protocol error is raised. + * Otherwise, if the source rectangle is partially or completely outside of + * the non-NULL wl_buffer, then the out_of_buffer protocol error is raised + * when the surface state is applied. A NULL wl_buffer does not raise the + * out_of_buffer error. + * + * The x, y arguments of wl_surface.attach are applied as normal to + * the surface. They indicate how many pixels to remove from the + * surface size from the left and the top. In other words, they are + * still in the surface-local coordinate system, just like dst_width + * and dst_height are. + * + * If the wl_surface associated with the wp_viewport is destroyed, + * all wp_viewport requests except 'destroy' raise the protocol error + * no_surface. + * + * If the wp_viewport object is destroyed, the crop and scale + * state is removed from the wl_surface. The change will be applied + * on the next wl_surface.commit. + */ +extern const struct wl_interface wp_viewport_interface; +#endif + +#ifndef WP_VIEWPORTER_ERROR_ENUM +#define WP_VIEWPORTER_ERROR_ENUM +enum wp_viewporter_error +{ + /** + * the surface already has a viewport object associated + */ + WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS = 0, +}; +#endif /* WP_VIEWPORTER_ERROR_ENUM */ + +#define WP_VIEWPORTER_DESTROY 0 +#define WP_VIEWPORTER_GET_VIEWPORT 1 + + +/** + * @ingroup iface_wp_viewporter + */ +#define WP_VIEWPORTER_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_wp_viewporter + */ +#define WP_VIEWPORTER_GET_VIEWPORT_SINCE_VERSION 1 + +/** @ingroup iface_wp_viewporter */ +static inline void +wp_viewporter_set_user_data(struct wp_viewporter *wp_viewporter, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) wp_viewporter, user_data); +} + +/** @ingroup iface_wp_viewporter */ +static inline void * +wp_viewporter_get_user_data(struct wp_viewporter *wp_viewporter) +{ + return wl_proxy_get_user_data((struct wl_proxy *) wp_viewporter); +} + +static inline uint32_t +wp_viewporter_get_version(struct wp_viewporter *wp_viewporter) +{ + return wl_proxy_get_version((struct wl_proxy *) wp_viewporter); +} + +/** + * @ingroup iface_wp_viewporter + * + * Informs the server that the client will not be using this + * protocol object anymore. This does not affect any other objects, + * wp_viewport objects included. + */ +static inline void +wp_viewporter_destroy(struct wp_viewporter *wp_viewporter) +{ + wl_proxy_marshal((struct wl_proxy *) wp_viewporter, + WP_VIEWPORTER_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) wp_viewporter); +} + +/** + * @ingroup iface_wp_viewporter + * + * Instantiate an interface extension for the given wl_surface to + * crop and scale its content. If the given wl_surface already has + * a wp_viewport object associated, the viewport_exists + * protocol error is raised. + */ +static inline struct wp_viewport * +wp_viewporter_get_viewport(struct wp_viewporter *wp_viewporter, struct wl_surface *surface) +{ + struct wl_proxy *id; + + id = wl_proxy_marshal_constructor((struct wl_proxy *) wp_viewporter, + WP_VIEWPORTER_GET_VIEWPORT, &wp_viewport_interface, NULL, surface); + + return (struct wp_viewport *) id; +} + +#ifndef WP_VIEWPORT_ERROR_ENUM +#define WP_VIEWPORT_ERROR_ENUM +enum wp_viewport_error +{ + /** + * negative or zero values in width or height + */ + WP_VIEWPORT_ERROR_BAD_VALUE = 0, + /** + * destination size is not integer + */ + WP_VIEWPORT_ERROR_BAD_SIZE = 1, + /** + * source rectangle extends outside of the content area + */ + WP_VIEWPORT_ERROR_OUT_OF_BUFFER = 2, + /** + * the wl_surface was destroyed + */ + WP_VIEWPORT_ERROR_NO_SURFACE = 3, +}; +#endif /* WP_VIEWPORT_ERROR_ENUM */ + +#define WP_VIEWPORT_DESTROY 0 +#define WP_VIEWPORT_SET_SOURCE 1 +#define WP_VIEWPORT_SET_DESTINATION 2 + + +/** + * @ingroup iface_wp_viewport + */ +#define WP_VIEWPORT_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_wp_viewport + */ +#define WP_VIEWPORT_SET_SOURCE_SINCE_VERSION 1 +/** + * @ingroup iface_wp_viewport + */ +#define WP_VIEWPORT_SET_DESTINATION_SINCE_VERSION 1 + +/** @ingroup iface_wp_viewport */ +static inline void +wp_viewport_set_user_data(struct wp_viewport *wp_viewport, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) wp_viewport, user_data); +} + +/** @ingroup iface_wp_viewport */ +static inline void * +wp_viewport_get_user_data(struct wp_viewport *wp_viewport) +{ + return wl_proxy_get_user_data((struct wl_proxy *) wp_viewport); +} + +static inline uint32_t +wp_viewport_get_version(struct wp_viewport *wp_viewport) +{ + return wl_proxy_get_version((struct wl_proxy *) wp_viewport); +} + +/** + * @ingroup iface_wp_viewport + * + * The associated wl_surface's crop and scale state is removed. + * The change is applied on the next wl_surface.commit. + */ +static inline void +wp_viewport_destroy(struct wp_viewport *wp_viewport) +{ + wl_proxy_marshal((struct wl_proxy *) wp_viewport, + WP_VIEWPORT_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) wp_viewport); +} + +/** + * @ingroup iface_wp_viewport + * + * Set the source rectangle of the associated wl_surface. See + * wp_viewport for the description, and relation to the wl_buffer + * size. + * + * If all of x, y, width and height are -1.0, the source rectangle is + * unset instead. Any other set of values where width or height are zero + * or negative, or x or y are negative, raise the bad_value protocol + * error. + * + * The crop and scale state is double-buffered state, and will be + * applied on the next wl_surface.commit. + */ +static inline void +wp_viewport_set_source(struct wp_viewport *wp_viewport, wl_fixed_t x, wl_fixed_t y, wl_fixed_t width, wl_fixed_t height) +{ + wl_proxy_marshal((struct wl_proxy *) wp_viewport, + WP_VIEWPORT_SET_SOURCE, x, y, width, height); +} + +/** + * @ingroup iface_wp_viewport + * + * Set the destination size of the associated wl_surface. See + * wp_viewport for the description, and relation to the wl_buffer + * size. + * + * If width is -1 and height is -1, the destination size is unset + * instead. Any other pair of values for width and height that + * contains zero or negative values raises the bad_value protocol + * error. + * + * The crop and scale state is double-buffered state, and will be + * applied on the next wl_surface.commit. + */ +static inline void +wp_viewport_set_destination(struct wp_viewport *wp_viewport, int32_t width, int32_t height) +{ + wl_proxy_marshal((struct wl_proxy *) wp_viewport, + WP_VIEWPORT_SET_DESTINATION, width, height); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/protocol/viewporter-protocol.c b/protocol/viewporter-protocol.c new file mode 100644 index 0000000..2604e5b --- /dev/null +++ b/protocol/viewporter-protocol.c @@ -0,0 +1,73 @@ +/* Generated by wayland-scanner 1.19.0 */ + +/* + * Copyright © 2013-2016 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 +#include +#include "wayland-util.h" + +#ifndef __has_attribute +# define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ +#endif + +#if (__has_attribute(visibility) || defined(__GNUC__) && __GNUC__ >= 4) +#define WL_PRIVATE __attribute__ ((visibility("hidden"))) +#else +#define WL_PRIVATE +#endif + +extern const struct wl_interface wl_surface_interface; +extern const struct wl_interface wp_viewport_interface; + +static const struct wl_interface *viewporter_types[] = { + NULL, + NULL, + NULL, + NULL, + &wp_viewport_interface, + &wl_surface_interface, +}; + +static const struct wl_message wp_viewporter_requests[] = { + { "destroy", "", viewporter_types + 0 }, + { "get_viewport", "no", viewporter_types + 4 }, +}; + +WL_PRIVATE const struct wl_interface wp_viewporter_interface = { + "wp_viewporter", 1, + 2, wp_viewporter_requests, + 0, NULL, +}; + +static const struct wl_message wp_viewport_requests[] = { + { "destroy", "", viewporter_types + 0 }, + { "set_source", "ffff", viewporter_types + 0 }, + { "set_destination", "ii", viewporter_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface wp_viewport_interface = { + "wp_viewport", 1, + 3, wp_viewport_requests, + 0, NULL, +}; diff --git a/protocol/weston-screenshooter-client-protocol.h b/protocol/weston-screenshooter-client-protocol.h new file mode 100644 index 0000000..8a14b7d --- /dev/null +++ b/protocol/weston-screenshooter-client-protocol.h @@ -0,0 +1,112 @@ +/* Generated by wayland-scanner 1.19.0 */ + +#ifndef WESTON_SCREENSHOOTER_CLIENT_PROTOCOL_H +#define WESTON_SCREENSHOOTER_CLIENT_PROTOCOL_H + +#include +#include +#include "wayland-client.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @page page_weston_screenshooter The weston_screenshooter protocol + * @section page_ifaces_weston_screenshooter Interfaces + * - @subpage page_iface_weston_screenshooter - + */ +struct weston_screenshooter; +struct wl_buffer; +struct wl_output; + +#ifndef WESTON_SCREENSHOOTER_INTERFACE +#define WESTON_SCREENSHOOTER_INTERFACE +/** + * @page page_iface_weston_screenshooter weston_screenshooter + * @section page_iface_weston_screenshooter_api API + * See @ref iface_weston_screenshooter. + */ +/** + * @defgroup iface_weston_screenshooter The weston_screenshooter interface + */ +extern const struct wl_interface weston_screenshooter_interface; +#endif + +/** + * @ingroup iface_weston_screenshooter + * @struct weston_screenshooter_listener + */ +struct weston_screenshooter_listener +{ + /** + */ + void (*done)(void *data, + struct weston_screenshooter *weston_screenshooter); +}; + +/** + * @ingroup iface_weston_screenshooter + */ +static inline int +weston_screenshooter_add_listener(struct weston_screenshooter *weston_screenshooter, + const struct weston_screenshooter_listener *listener, void *data) +{ + return wl_proxy_add_listener((struct wl_proxy *) weston_screenshooter, + (void(* *) (void))listener, data); +} + +#define WESTON_SCREENSHOOTER_SHOOT 0 + +/** + * @ingroup iface_weston_screenshooter + */ +#define WESTON_SCREENSHOOTER_DONE_SINCE_VERSION 1 + +/** + * @ingroup iface_weston_screenshooter + */ +#define WESTON_SCREENSHOOTER_SHOOT_SINCE_VERSION 1 + +/** @ingroup iface_weston_screenshooter */ +static inline void +weston_screenshooter_set_user_data(struct weston_screenshooter *weston_screenshooter, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) weston_screenshooter, user_data); +} + +/** @ingroup iface_weston_screenshooter */ +static inline void * +weston_screenshooter_get_user_data(struct weston_screenshooter *weston_screenshooter) +{ + return wl_proxy_get_user_data((struct wl_proxy *) weston_screenshooter); +} + +static inline uint32_t +weston_screenshooter_get_version(struct weston_screenshooter *weston_screenshooter) +{ + return wl_proxy_get_version((struct wl_proxy *) weston_screenshooter); +} + +/** @ingroup iface_weston_screenshooter */ +static inline void +weston_screenshooter_destroy(struct weston_screenshooter *weston_screenshooter) +{ + wl_proxy_destroy((struct wl_proxy *) weston_screenshooter); +} + +/** + * @ingroup iface_weston_screenshooter + */ +static inline void +weston_screenshooter_shoot(struct weston_screenshooter *weston_screenshooter, struct wl_output *output, struct wl_buffer *buffer) +{ + wl_proxy_marshal((struct wl_proxy *) weston_screenshooter, + WESTON_SCREENSHOOTER_SHOOT, output, buffer); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/protocol/weston-screenshooter-protocol.c b/protocol/weston-screenshooter-protocol.c new file mode 100644 index 0000000..ab31a5c --- /dev/null +++ b/protocol/weston-screenshooter-protocol.c @@ -0,0 +1,37 @@ +/* Generated by wayland-scanner 1.19.0 */ + +#include +#include +#include "wayland-util.h" + +#ifndef __has_attribute +# define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ +#endif + +#if (__has_attribute(visibility) || defined(__GNUC__) && __GNUC__ >= 4) +#define WL_PRIVATE __attribute__ ((visibility("hidden"))) +#else +#define WL_PRIVATE +#endif + +extern const struct wl_interface wl_buffer_interface; +extern const struct wl_interface wl_output_interface; + +static const struct wl_interface *weston_screenshooter_types[] = { + &wl_output_interface, + &wl_buffer_interface, +}; + +static const struct wl_message weston_screenshooter_requests[] = { + { "shoot", "oo", weston_screenshooter_types + 0 }, +}; + +static const struct wl_message weston_screenshooter_events[] = { + { "done", "", weston_screenshooter_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface weston_screenshooter_interface = { + "weston_screenshooter", 1, + 1, weston_screenshooter_requests, + 1, weston_screenshooter_events, +}; diff --git a/protocol/xdg-shell-client-protocol.h b/protocol/xdg-shell-client-protocol.h new file mode 100644 index 0000000..a019de1 --- /dev/null +++ b/protocol/xdg-shell-client-protocol.h @@ -0,0 +1,1998 @@ +/* Generated by wayland-scanner 1.19.0 */ + +#ifndef XDG_SHELL_CLIENT_PROTOCOL_H +#define XDG_SHELL_CLIENT_PROTOCOL_H + +#include +#include +#include "wayland-client.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @page page_xdg_shell The xdg_shell protocol + * @section page_ifaces_xdg_shell Interfaces + * - @subpage page_iface_xdg_wm_base - create desktop-style surfaces + * - @subpage page_iface_xdg_positioner - child surface positioner + * - @subpage page_iface_xdg_surface - desktop user interface surface base interface + * - @subpage page_iface_xdg_toplevel - toplevel surface + * - @subpage page_iface_xdg_popup - short-lived, popup surfaces for menus + * @section page_copyright_xdg_shell Copyright + *
+ *
+ * Copyright © 2008-2013 Kristian Høgsberg
+ * Copyright © 2013      Rafael Antognolli
+ * Copyright © 2013      Jasper St. Pierre
+ * Copyright © 2010-2013 Intel Corporation
+ * Copyright © 2015-2017 Samsung Electronics Co., Ltd
+ * Copyright © 2015-2017 Red Hat Inc.
+ *
+ * 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.
+ * 
+ */ +struct wl_output; +struct wl_seat; +struct wl_surface; +struct xdg_popup; +struct xdg_positioner; +struct xdg_surface; +struct xdg_toplevel; +struct xdg_wm_base; + +#ifndef XDG_WM_BASE_INTERFACE +#define XDG_WM_BASE_INTERFACE +/** + * @page page_iface_xdg_wm_base xdg_wm_base + * @section page_iface_xdg_wm_base_desc Description + * + * The xdg_wm_base interface is exposed as a global object enabling clients + * to turn their wl_surfaces into windows in a desktop environment. It + * defines the basic functionality needed for clients and the compositor to + * create windows that can be dragged, resized, maximized, etc, as well as + * creating transient windows such as popup menus. + * @section page_iface_xdg_wm_base_api API + * See @ref iface_xdg_wm_base. + */ +/** + * @defgroup iface_xdg_wm_base The xdg_wm_base interface + * + * The xdg_wm_base interface is exposed as a global object enabling clients + * to turn their wl_surfaces into windows in a desktop environment. It + * defines the basic functionality needed for clients and the compositor to + * create windows that can be dragged, resized, maximized, etc, as well as + * creating transient windows such as popup menus. + */ +extern const struct wl_interface xdg_wm_base_interface; +#endif +#ifndef XDG_POSITIONER_INTERFACE +#define XDG_POSITIONER_INTERFACE +/** + * @page page_iface_xdg_positioner xdg_positioner + * @section page_iface_xdg_positioner_desc Description + * + * The xdg_positioner provides a collection of rules for the placement of a + * child surface relative to a parent surface. Rules can be defined to ensure + * the child surface remains within the visible area's borders, and to + * specify how the child surface changes its position, such as sliding along + * an axis, or flipping around a rectangle. These positioner-created rules are + * constrained by the requirement that a child surface must intersect with or + * be at least partially adjacent to its parent surface. + * + * See the various requests for details about possible rules. + * + * At the time of the request, the compositor makes a copy of the rules + * specified by the xdg_positioner. Thus, after the request is complete the + * xdg_positioner object can be destroyed or reused; further changes to the + * object will have no effect on previous usages. + * + * For an xdg_positioner object to be considered complete, it must have a + * non-zero size set by set_size, and a non-zero anchor rectangle set by + * set_anchor_rect. Passing an incomplete xdg_positioner object when + * positioning a surface raises an error. + * @section page_iface_xdg_positioner_api API + * See @ref iface_xdg_positioner. + */ +/** + * @defgroup iface_xdg_positioner The xdg_positioner interface + * + * The xdg_positioner provides a collection of rules for the placement of a + * child surface relative to a parent surface. Rules can be defined to ensure + * the child surface remains within the visible area's borders, and to + * specify how the child surface changes its position, such as sliding along + * an axis, or flipping around a rectangle. These positioner-created rules are + * constrained by the requirement that a child surface must intersect with or + * be at least partially adjacent to its parent surface. + * + * See the various requests for details about possible rules. + * + * At the time of the request, the compositor makes a copy of the rules + * specified by the xdg_positioner. Thus, after the request is complete the + * xdg_positioner object can be destroyed or reused; further changes to the + * object will have no effect on previous usages. + * + * For an xdg_positioner object to be considered complete, it must have a + * non-zero size set by set_size, and a non-zero anchor rectangle set by + * set_anchor_rect. Passing an incomplete xdg_positioner object when + * positioning a surface raises an error. + */ +extern const struct wl_interface xdg_positioner_interface; +#endif +#ifndef XDG_SURFACE_INTERFACE +#define XDG_SURFACE_INTERFACE +/** + * @page page_iface_xdg_surface xdg_surface + * @section page_iface_xdg_surface_desc Description + * + * An interface that may be implemented by a wl_surface, for + * implementations that provide a desktop-style user interface. + * + * It provides a base set of functionality required to construct user + * interface elements requiring management by the compositor, such as + * toplevel windows, menus, etc. The types of functionality are split into + * xdg_surface roles. + * + * Creating an xdg_surface does not set the role for a wl_surface. In order + * to map an xdg_surface, the client must create a role-specific object + * using, e.g., get_toplevel, get_popup. The wl_surface for any given + * xdg_surface can have at most one role, and may not be assigned any role + * not based on xdg_surface. + * + * A role must be assigned before any other requests are made to the + * xdg_surface object. + * + * The client must call wl_surface.commit on the corresponding wl_surface + * for the xdg_surface state to take effect. + * + * Creating an xdg_surface from a wl_surface which has a buffer attached or + * committed is a client error, and any attempts by a client to attach or + * manipulate a buffer prior to the first xdg_surface.configure call must + * also be treated as errors. + * + * Mapping an xdg_surface-based role surface is defined as making it + * possible for the surface to be shown by the compositor. Note that + * a mapped surface is not guaranteed to be visible once it is mapped. + * + * For an xdg_surface to be mapped by the compositor, the following + * conditions must be met: + * (1) the client has assigned an xdg_surface-based role to the surface + * (2) the client has set and committed the xdg_surface state and the + * role-dependent state to the surface + * (3) the client has committed a buffer to the surface + * + * A newly-unmapped surface is considered to have met condition (1) out + * of the 3 required conditions for mapping a surface if its role surface + * has not been destroyed. + * @section page_iface_xdg_surface_api API + * See @ref iface_xdg_surface. + */ +/** + * @defgroup iface_xdg_surface The xdg_surface interface + * + * An interface that may be implemented by a wl_surface, for + * implementations that provide a desktop-style user interface. + * + * It provides a base set of functionality required to construct user + * interface elements requiring management by the compositor, such as + * toplevel windows, menus, etc. The types of functionality are split into + * xdg_surface roles. + * + * Creating an xdg_surface does not set the role for a wl_surface. In order + * to map an xdg_surface, the client must create a role-specific object + * using, e.g., get_toplevel, get_popup. The wl_surface for any given + * xdg_surface can have at most one role, and may not be assigned any role + * not based on xdg_surface. + * + * A role must be assigned before any other requests are made to the + * xdg_surface object. + * + * The client must call wl_surface.commit on the corresponding wl_surface + * for the xdg_surface state to take effect. + * + * Creating an xdg_surface from a wl_surface which has a buffer attached or + * committed is a client error, and any attempts by a client to attach or + * manipulate a buffer prior to the first xdg_surface.configure call must + * also be treated as errors. + * + * Mapping an xdg_surface-based role surface is defined as making it + * possible for the surface to be shown by the compositor. Note that + * a mapped surface is not guaranteed to be visible once it is mapped. + * + * For an xdg_surface to be mapped by the compositor, the following + * conditions must be met: + * (1) the client has assigned an xdg_surface-based role to the surface + * (2) the client has set and committed the xdg_surface state and the + * role-dependent state to the surface + * (3) the client has committed a buffer to the surface + * + * A newly-unmapped surface is considered to have met condition (1) out + * of the 3 required conditions for mapping a surface if its role surface + * has not been destroyed. + */ +extern const struct wl_interface xdg_surface_interface; +#endif +#ifndef XDG_TOPLEVEL_INTERFACE +#define XDG_TOPLEVEL_INTERFACE +/** + * @page page_iface_xdg_toplevel xdg_toplevel + * @section page_iface_xdg_toplevel_desc Description + * + * This interface defines an xdg_surface role which allows a surface to, + * among other things, set window-like properties such as maximize, + * fullscreen, and minimize, set application-specific metadata like title and + * id, and well as trigger user interactive operations such as interactive + * resize and move. + * + * Unmapping an xdg_toplevel means that the surface cannot be shown + * by the compositor until it is explicitly mapped again. + * All active operations (e.g., move, resize) are canceled and all + * attributes (e.g. title, state, stacking, ...) are discarded for + * an xdg_toplevel surface when it is unmapped. + * + * Attaching a null buffer to a toplevel unmaps the surface. + * @section page_iface_xdg_toplevel_api API + * See @ref iface_xdg_toplevel. + */ +/** + * @defgroup iface_xdg_toplevel The xdg_toplevel interface + * + * This interface defines an xdg_surface role which allows a surface to, + * among other things, set window-like properties such as maximize, + * fullscreen, and minimize, set application-specific metadata like title and + * id, and well as trigger user interactive operations such as interactive + * resize and move. + * + * Unmapping an xdg_toplevel means that the surface cannot be shown + * by the compositor until it is explicitly mapped again. + * All active operations (e.g., move, resize) are canceled and all + * attributes (e.g. title, state, stacking, ...) are discarded for + * an xdg_toplevel surface when it is unmapped. + * + * Attaching a null buffer to a toplevel unmaps the surface. + */ +extern const struct wl_interface xdg_toplevel_interface; +#endif +#ifndef XDG_POPUP_INTERFACE +#define XDG_POPUP_INTERFACE +/** + * @page page_iface_xdg_popup xdg_popup + * @section page_iface_xdg_popup_desc Description + * + * A popup surface is a short-lived, temporary surface. It can be used to + * implement for example menus, popovers, tooltips and other similar user + * interface concepts. + * + * A popup can be made to take an explicit grab. See xdg_popup.grab for + * details. + * + * When the popup is dismissed, a popup_done event will be sent out, and at + * the same time the surface will be unmapped. See the xdg_popup.popup_done + * event for details. + * + * Explicitly destroying the xdg_popup object will also dismiss the popup and + * unmap the surface. Clients that want to dismiss the popup when another + * surface of their own is clicked should dismiss the popup using the destroy + * request. + * + * A newly created xdg_popup will be stacked on top of all previously created + * xdg_popup surfaces associated with the same xdg_toplevel. + * + * The parent of an xdg_popup must be mapped (see the xdg_surface + * description) before the xdg_popup itself. + * + * The client must call wl_surface.commit on the corresponding wl_surface + * for the xdg_popup state to take effect. + * @section page_iface_xdg_popup_api API + * See @ref iface_xdg_popup. + */ +/** + * @defgroup iface_xdg_popup The xdg_popup interface + * + * A popup surface is a short-lived, temporary surface. It can be used to + * implement for example menus, popovers, tooltips and other similar user + * interface concepts. + * + * A popup can be made to take an explicit grab. See xdg_popup.grab for + * details. + * + * When the popup is dismissed, a popup_done event will be sent out, and at + * the same time the surface will be unmapped. See the xdg_popup.popup_done + * event for details. + * + * Explicitly destroying the xdg_popup object will also dismiss the popup and + * unmap the surface. Clients that want to dismiss the popup when another + * surface of their own is clicked should dismiss the popup using the destroy + * request. + * + * A newly created xdg_popup will be stacked on top of all previously created + * xdg_popup surfaces associated with the same xdg_toplevel. + * + * The parent of an xdg_popup must be mapped (see the xdg_surface + * description) before the xdg_popup itself. + * + * The client must call wl_surface.commit on the corresponding wl_surface + * for the xdg_popup state to take effect. + */ +extern const struct wl_interface xdg_popup_interface; +#endif + +#ifndef XDG_WM_BASE_ERROR_ENUM +#define XDG_WM_BASE_ERROR_ENUM +enum xdg_wm_base_error +{ + /** + * given wl_surface has another role + */ + XDG_WM_BASE_ERROR_ROLE = 0, + /** + * xdg_wm_base was destroyed before children + */ + XDG_WM_BASE_ERROR_DEFUNCT_SURFACES = 1, + /** + * the client tried to map or destroy a non-topmost popup + */ + XDG_WM_BASE_ERROR_NOT_THE_TOPMOST_POPUP = 2, + /** + * the client specified an invalid popup parent surface + */ + XDG_WM_BASE_ERROR_INVALID_POPUP_PARENT = 3, + /** + * the client provided an invalid surface state + */ + XDG_WM_BASE_ERROR_INVALID_SURFACE_STATE = 4, + /** + * the client provided an invalid positioner + */ + XDG_WM_BASE_ERROR_INVALID_POSITIONER = 5, +}; +#endif /* XDG_WM_BASE_ERROR_ENUM */ + +/** + * @ingroup iface_xdg_wm_base + * @struct xdg_wm_base_listener + */ +struct xdg_wm_base_listener +{ + /** + * check if the client is alive + * + * The ping event asks the client if it's still alive. Pass the + * serial specified in the event back to the compositor by sending + * a "pong" request back with the specified serial. See + * xdg_wm_base.pong. + * + * Compositors can use this to determine if the client is still + * alive. It's unspecified what will happen if the client doesn't + * respond to the ping request, or in what timeframe. Clients + * should try to respond in a reasonable amount of time. + * + * A compositor is free to ping in any way it wants, but a client + * must always respond to any xdg_wm_base object it created. + * @param serial pass this to the pong request + */ + void (*ping)(void *data, + struct xdg_wm_base *xdg_wm_base, + uint32_t serial); +}; + +/** + * @ingroup iface_xdg_wm_base + */ +static inline int +xdg_wm_base_add_listener(struct xdg_wm_base *xdg_wm_base, + const struct xdg_wm_base_listener *listener, void *data) +{ + return wl_proxy_add_listener((struct wl_proxy *) xdg_wm_base, + (void(* *) (void))listener, data); +} + +#define XDG_WM_BASE_DESTROY 0 +#define XDG_WM_BASE_CREATE_POSITIONER 1 +#define XDG_WM_BASE_GET_XDG_SURFACE 2 +#define XDG_WM_BASE_PONG 3 + +/** + * @ingroup iface_xdg_wm_base + */ +#define XDG_WM_BASE_PING_SINCE_VERSION 1 + +/** + * @ingroup iface_xdg_wm_base + */ +#define XDG_WM_BASE_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_wm_base + */ +#define XDG_WM_BASE_CREATE_POSITIONER_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_wm_base + */ +#define XDG_WM_BASE_GET_XDG_SURFACE_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_wm_base + */ +#define XDG_WM_BASE_PONG_SINCE_VERSION 1 + +/** @ingroup iface_xdg_wm_base */ +static inline void +xdg_wm_base_set_user_data(struct xdg_wm_base *xdg_wm_base, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) xdg_wm_base, user_data); +} + +/** @ingroup iface_xdg_wm_base */ +static inline void * +xdg_wm_base_get_user_data(struct xdg_wm_base *xdg_wm_base) +{ + return wl_proxy_get_user_data((struct wl_proxy *) xdg_wm_base); +} + +static inline uint32_t +xdg_wm_base_get_version(struct xdg_wm_base *xdg_wm_base) +{ + return wl_proxy_get_version((struct wl_proxy *) xdg_wm_base); +} + +/** + * @ingroup iface_xdg_wm_base + * + * Destroy this xdg_wm_base object. + * + * Destroying a bound xdg_wm_base object while there are surfaces + * still alive created by this xdg_wm_base object instance is illegal + * and will result in a protocol error. + */ +static inline void +xdg_wm_base_destroy(struct xdg_wm_base *xdg_wm_base) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_wm_base, + XDG_WM_BASE_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) xdg_wm_base); +} + +/** + * @ingroup iface_xdg_wm_base + * + * Create a positioner object. A positioner object is used to position + * surfaces relative to some parent surface. See the interface description + * and xdg_surface.get_popup for details. + */ +static inline struct xdg_positioner * +xdg_wm_base_create_positioner(struct xdg_wm_base *xdg_wm_base) +{ + struct wl_proxy *id; + + id = wl_proxy_marshal_constructor((struct wl_proxy *) xdg_wm_base, + XDG_WM_BASE_CREATE_POSITIONER, &xdg_positioner_interface, NULL); + + return (struct xdg_positioner *) id; +} + +/** + * @ingroup iface_xdg_wm_base + * + * This creates an xdg_surface for the given surface. While xdg_surface + * itself is not a role, the corresponding surface may only be assigned + * a role extending xdg_surface, such as xdg_toplevel or xdg_popup. + * + * This creates an xdg_surface for the given surface. An xdg_surface is + * used as basis to define a role to a given surface, such as xdg_toplevel + * or xdg_popup. It also manages functionality shared between xdg_surface + * based surface roles. + * + * See the documentation of xdg_surface for more details about what an + * xdg_surface is and how it is used. + */ +static inline struct xdg_surface * +xdg_wm_base_get_xdg_surface(struct xdg_wm_base *xdg_wm_base, struct wl_surface *surface) +{ + struct wl_proxy *id; + + id = wl_proxy_marshal_constructor((struct wl_proxy *) xdg_wm_base, + XDG_WM_BASE_GET_XDG_SURFACE, &xdg_surface_interface, NULL, surface); + + return (struct xdg_surface *) id; +} + +/** + * @ingroup iface_xdg_wm_base + * + * A client must respond to a ping event with a pong request or + * the client may be deemed unresponsive. See xdg_wm_base.ping. + */ +static inline void +xdg_wm_base_pong(struct xdg_wm_base *xdg_wm_base, uint32_t serial) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_wm_base, + XDG_WM_BASE_PONG, serial); +} + +#ifndef XDG_POSITIONER_ERROR_ENUM +#define XDG_POSITIONER_ERROR_ENUM +enum xdg_positioner_error +{ + /** + * invalid input provided + */ + XDG_POSITIONER_ERROR_INVALID_INPUT = 0, +}; +#endif /* XDG_POSITIONER_ERROR_ENUM */ + +#ifndef XDG_POSITIONER_ANCHOR_ENUM +#define XDG_POSITIONER_ANCHOR_ENUM +enum xdg_positioner_anchor +{ + XDG_POSITIONER_ANCHOR_NONE = 0, + XDG_POSITIONER_ANCHOR_TOP = 1, + XDG_POSITIONER_ANCHOR_BOTTOM = 2, + XDG_POSITIONER_ANCHOR_LEFT = 3, + XDG_POSITIONER_ANCHOR_RIGHT = 4, + XDG_POSITIONER_ANCHOR_TOP_LEFT = 5, + XDG_POSITIONER_ANCHOR_BOTTOM_LEFT = 6, + XDG_POSITIONER_ANCHOR_TOP_RIGHT = 7, + XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT = 8, +}; +#endif /* XDG_POSITIONER_ANCHOR_ENUM */ + +#ifndef XDG_POSITIONER_GRAVITY_ENUM +#define XDG_POSITIONER_GRAVITY_ENUM +enum xdg_positioner_gravity +{ + XDG_POSITIONER_GRAVITY_NONE = 0, + XDG_POSITIONER_GRAVITY_TOP = 1, + XDG_POSITIONER_GRAVITY_BOTTOM = 2, + XDG_POSITIONER_GRAVITY_LEFT = 3, + XDG_POSITIONER_GRAVITY_RIGHT = 4, + XDG_POSITIONER_GRAVITY_TOP_LEFT = 5, + XDG_POSITIONER_GRAVITY_BOTTOM_LEFT = 6, + XDG_POSITIONER_GRAVITY_TOP_RIGHT = 7, + XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT = 8, +}; +#endif /* XDG_POSITIONER_GRAVITY_ENUM */ + +#ifndef XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_ENUM +#define XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_ENUM +/** + * @ingroup iface_xdg_positioner + * vertically resize the surface + * + * Resize the surface vertically so that it is completely unconstrained. + */ +enum xdg_positioner_constraint_adjustment +{ + XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_NONE = 0, + XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X = 1, + XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y = 2, + XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X = 4, + XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y = 8, + XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X = 16, + XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y = 32, +}; +#endif /* XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_ENUM */ + +#define XDG_POSITIONER_DESTROY 0 +#define XDG_POSITIONER_SET_SIZE 1 +#define XDG_POSITIONER_SET_ANCHOR_RECT 2 +#define XDG_POSITIONER_SET_ANCHOR 3 +#define XDG_POSITIONER_SET_GRAVITY 4 +#define XDG_POSITIONER_SET_CONSTRAINT_ADJUSTMENT 5 +#define XDG_POSITIONER_SET_OFFSET 6 +#define XDG_POSITIONER_SET_REACTIVE 7 +#define XDG_POSITIONER_SET_PARENT_SIZE 8 +#define XDG_POSITIONER_SET_PARENT_CONFIGURE 9 + + +/** + * @ingroup iface_xdg_positioner + */ +#define XDG_POSITIONER_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_positioner + */ +#define XDG_POSITIONER_SET_SIZE_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_positioner + */ +#define XDG_POSITIONER_SET_ANCHOR_RECT_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_positioner + */ +#define XDG_POSITIONER_SET_ANCHOR_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_positioner + */ +#define XDG_POSITIONER_SET_GRAVITY_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_positioner + */ +#define XDG_POSITIONER_SET_CONSTRAINT_ADJUSTMENT_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_positioner + */ +#define XDG_POSITIONER_SET_OFFSET_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_positioner + */ +#define XDG_POSITIONER_SET_REACTIVE_SINCE_VERSION 3 +/** + * @ingroup iface_xdg_positioner + */ +#define XDG_POSITIONER_SET_PARENT_SIZE_SINCE_VERSION 3 +/** + * @ingroup iface_xdg_positioner + */ +#define XDG_POSITIONER_SET_PARENT_CONFIGURE_SINCE_VERSION 3 + +/** @ingroup iface_xdg_positioner */ +static inline void +xdg_positioner_set_user_data(struct xdg_positioner *xdg_positioner, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) xdg_positioner, user_data); +} + +/** @ingroup iface_xdg_positioner */ +static inline void * +xdg_positioner_get_user_data(struct xdg_positioner *xdg_positioner) +{ + return wl_proxy_get_user_data((struct wl_proxy *) xdg_positioner); +} + +static inline uint32_t +xdg_positioner_get_version(struct xdg_positioner *xdg_positioner) +{ + return wl_proxy_get_version((struct wl_proxy *) xdg_positioner); +} + +/** + * @ingroup iface_xdg_positioner + * + * Notify the compositor that the xdg_positioner will no longer be used. + */ +static inline void +xdg_positioner_destroy(struct xdg_positioner *xdg_positioner) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_positioner, + XDG_POSITIONER_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) xdg_positioner); +} + +/** + * @ingroup iface_xdg_positioner + * + * Set the size of the surface that is to be positioned with the positioner + * object. The size is in surface-local coordinates and corresponds to the + * window geometry. See xdg_surface.set_window_geometry. + * + * If a zero or negative size is set the invalid_input error is raised. + */ +static inline void +xdg_positioner_set_size(struct xdg_positioner *xdg_positioner, int32_t width, int32_t height) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_positioner, + XDG_POSITIONER_SET_SIZE, width, height); +} + +/** + * @ingroup iface_xdg_positioner + * + * Specify the anchor rectangle within the parent surface that the child + * surface will be placed relative to. The rectangle is relative to the + * window geometry as defined by xdg_surface.set_window_geometry of the + * parent surface. + * + * When the xdg_positioner object is used to position a child surface, the + * anchor rectangle may not extend outside the window geometry of the + * positioned child's parent surface. + * + * If a negative size is set the invalid_input error is raised. + */ +static inline void +xdg_positioner_set_anchor_rect(struct xdg_positioner *xdg_positioner, int32_t x, int32_t y, int32_t width, int32_t height) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_positioner, + XDG_POSITIONER_SET_ANCHOR_RECT, x, y, width, height); +} + +/** + * @ingroup iface_xdg_positioner + * + * Defines the anchor point for the anchor rectangle. The specified anchor + * is used derive an anchor point that the child surface will be + * positioned relative to. If a corner anchor is set (e.g. 'top_left' or + * 'bottom_right'), the anchor point will be at the specified corner; + * otherwise, the derived anchor point will be centered on the specified + * edge, or in the center of the anchor rectangle if no edge is specified. + */ +static inline void +xdg_positioner_set_anchor(struct xdg_positioner *xdg_positioner, uint32_t anchor) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_positioner, + XDG_POSITIONER_SET_ANCHOR, anchor); +} + +/** + * @ingroup iface_xdg_positioner + * + * Defines in what direction a surface should be positioned, relative to + * the anchor point of the parent surface. If a corner gravity is + * specified (e.g. 'bottom_right' or 'top_left'), then the child surface + * will be placed towards the specified gravity; otherwise, the child + * surface will be centered over the anchor point on any axis that had no + * gravity specified. + */ +static inline void +xdg_positioner_set_gravity(struct xdg_positioner *xdg_positioner, uint32_t gravity) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_positioner, + XDG_POSITIONER_SET_GRAVITY, gravity); +} + +/** + * @ingroup iface_xdg_positioner + * + * Specify how the window should be positioned if the originally intended + * position caused the surface to be constrained, meaning at least + * partially outside positioning boundaries set by the compositor. The + * adjustment is set by constructing a bitmask describing the adjustment to + * be made when the surface is constrained on that axis. + * + * If no bit for one axis is set, the compositor will assume that the child + * surface should not change its position on that axis when constrained. + * + * If more than one bit for one axis is set, the order of how adjustments + * are applied is specified in the corresponding adjustment descriptions. + * + * The default adjustment is none. + */ +static inline void +xdg_positioner_set_constraint_adjustment(struct xdg_positioner *xdg_positioner, uint32_t constraint_adjustment) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_positioner, + XDG_POSITIONER_SET_CONSTRAINT_ADJUSTMENT, constraint_adjustment); +} + +/** + * @ingroup iface_xdg_positioner + * + * Specify the surface position offset relative to the position of the + * anchor on the anchor rectangle and the anchor on the surface. For + * example if the anchor of the anchor rectangle is at (x, y), the surface + * has the gravity bottom|right, and the offset is (ox, oy), the calculated + * surface position will be (x + ox, y + oy). The offset position of the + * surface is the one used for constraint testing. See + * set_constraint_adjustment. + * + * An example use case is placing a popup menu on top of a user interface + * element, while aligning the user interface element of the parent surface + * with some user interface element placed somewhere in the popup surface. + */ +static inline void +xdg_positioner_set_offset(struct xdg_positioner *xdg_positioner, int32_t x, int32_t y) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_positioner, + XDG_POSITIONER_SET_OFFSET, x, y); +} + +/** + * @ingroup iface_xdg_positioner + * + * When set reactive, the surface is reconstrained if the conditions used + * for constraining changed, e.g. the parent window moved. + * + * If the conditions changed and the popup was reconstrained, an + * xdg_popup.configure event is sent with updated geometry, followed by an + * xdg_surface.configure event. + */ +static inline void +xdg_positioner_set_reactive(struct xdg_positioner *xdg_positioner) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_positioner, + XDG_POSITIONER_SET_REACTIVE); +} + +/** + * @ingroup iface_xdg_positioner + * + * Set the parent window geometry the compositor should use when + * positioning the popup. The compositor may use this information to + * determine the future state the popup should be constrained using. If + * this doesn't match the dimension of the parent the popup is eventually + * positioned against, the behavior is undefined. + * + * The arguments are given in the surface-local coordinate space. + */ +static inline void +xdg_positioner_set_parent_size(struct xdg_positioner *xdg_positioner, int32_t parent_width, int32_t parent_height) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_positioner, + XDG_POSITIONER_SET_PARENT_SIZE, parent_width, parent_height); +} + +/** + * @ingroup iface_xdg_positioner + * + * Set the serial of a xdg_surface.configure event this positioner will be + * used in response to. The compositor may use this information together + * with set_parent_size to determine what future state the popup should be + * constrained using. + */ +static inline void +xdg_positioner_set_parent_configure(struct xdg_positioner *xdg_positioner, uint32_t serial) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_positioner, + XDG_POSITIONER_SET_PARENT_CONFIGURE, serial); +} + +#ifndef XDG_SURFACE_ERROR_ENUM +#define XDG_SURFACE_ERROR_ENUM +enum xdg_surface_error +{ + XDG_SURFACE_ERROR_NOT_CONSTRUCTED = 1, + XDG_SURFACE_ERROR_ALREADY_CONSTRUCTED = 2, + XDG_SURFACE_ERROR_UNCONFIGURED_BUFFER = 3, +}; +#endif /* XDG_SURFACE_ERROR_ENUM */ + +/** + * @ingroup iface_xdg_surface + * @struct xdg_surface_listener + */ +struct xdg_surface_listener +{ + /** + * suggest a surface change + * + * The configure event marks the end of a configure sequence. A + * configure sequence is a set of one or more events configuring + * the state of the xdg_surface, including the final + * xdg_surface.configure event. + * + * Where applicable, xdg_surface surface roles will during a + * configure sequence extend this event as a latched state sent as + * events before the xdg_surface.configure event. Such events + * should be considered to make up a set of atomically applied + * configuration states, where the xdg_surface.configure commits + * the accumulated state. + * + * Clients should arrange their surface for the new states, and + * then send an ack_configure request with the serial sent in this + * configure event at some point before committing the new surface. + * + * If the client receives multiple configure events before it can + * respond to one, it is free to discard all but the last event it + * received. + * @param serial serial of the configure event + */ + void (*configure)(void *data, + struct xdg_surface *xdg_surface, + uint32_t serial); +}; + +/** + * @ingroup iface_xdg_surface + */ +static inline int +xdg_surface_add_listener(struct xdg_surface *xdg_surface, + const struct xdg_surface_listener *listener, void *data) +{ + return wl_proxy_add_listener((struct wl_proxy *) xdg_surface, + (void(* *) (void))listener, data); +} + +#define XDG_SURFACE_DESTROY 0 +#define XDG_SURFACE_GET_TOPLEVEL 1 +#define XDG_SURFACE_GET_POPUP 2 +#define XDG_SURFACE_SET_WINDOW_GEOMETRY 3 +#define XDG_SURFACE_ACK_CONFIGURE 4 + +/** + * @ingroup iface_xdg_surface + */ +#define XDG_SURFACE_CONFIGURE_SINCE_VERSION 1 + +/** + * @ingroup iface_xdg_surface + */ +#define XDG_SURFACE_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_surface + */ +#define XDG_SURFACE_GET_TOPLEVEL_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_surface + */ +#define XDG_SURFACE_GET_POPUP_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_surface + */ +#define XDG_SURFACE_SET_WINDOW_GEOMETRY_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_surface + */ +#define XDG_SURFACE_ACK_CONFIGURE_SINCE_VERSION 1 + +/** @ingroup iface_xdg_surface */ +static inline void +xdg_surface_set_user_data(struct xdg_surface *xdg_surface, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) xdg_surface, user_data); +} + +/** @ingroup iface_xdg_surface */ +static inline void * +xdg_surface_get_user_data(struct xdg_surface *xdg_surface) +{ + return wl_proxy_get_user_data((struct wl_proxy *) xdg_surface); +} + +static inline uint32_t +xdg_surface_get_version(struct xdg_surface *xdg_surface) +{ + return wl_proxy_get_version((struct wl_proxy *) xdg_surface); +} + +/** + * @ingroup iface_xdg_surface + * + * Destroy the xdg_surface object. An xdg_surface must only be destroyed + * after its role object has been destroyed. + */ +static inline void +xdg_surface_destroy(struct xdg_surface *xdg_surface) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_surface, + XDG_SURFACE_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) xdg_surface); +} + +/** + * @ingroup iface_xdg_surface + * + * This creates an xdg_toplevel object for the given xdg_surface and gives + * the associated wl_surface the xdg_toplevel role. + * + * See the documentation of xdg_toplevel for more details about what an + * xdg_toplevel is and how it is used. + */ +static inline struct xdg_toplevel * +xdg_surface_get_toplevel(struct xdg_surface *xdg_surface) +{ + struct wl_proxy *id; + + id = wl_proxy_marshal_constructor((struct wl_proxy *) xdg_surface, + XDG_SURFACE_GET_TOPLEVEL, &xdg_toplevel_interface, NULL); + + return (struct xdg_toplevel *) id; +} + +/** + * @ingroup iface_xdg_surface + * + * This creates an xdg_popup object for the given xdg_surface and gives + * the associated wl_surface the xdg_popup role. + * + * If null is passed as a parent, a parent surface must be specified using + * some other protocol, before committing the initial state. + * + * See the documentation of xdg_popup for more details about what an + * xdg_popup is and how it is used. + */ +static inline struct xdg_popup * +xdg_surface_get_popup(struct xdg_surface *xdg_surface, struct xdg_surface *parent, struct xdg_positioner *positioner) +{ + struct wl_proxy *id; + + id = wl_proxy_marshal_constructor((struct wl_proxy *) xdg_surface, + XDG_SURFACE_GET_POPUP, &xdg_popup_interface, NULL, parent, positioner); + + return (struct xdg_popup *) id; +} + +/** + * @ingroup iface_xdg_surface + * + * The window geometry of a surface is its "visible bounds" from the + * user's perspective. Client-side decorations often have invisible + * portions like drop-shadows which should be ignored for the + * purposes of aligning, placing and constraining windows. + * + * The window geometry is double buffered, and will be applied at the + * time wl_surface.commit of the corresponding wl_surface is called. + * + * When maintaining a position, the compositor should treat the (x, y) + * coordinate of the window geometry as the top left corner of the window. + * A client changing the (x, y) window geometry coordinate should in + * general not alter the position of the window. + * + * Once the window geometry of the surface is set, it is not possible to + * unset it, and it will remain the same until set_window_geometry is + * called again, even if a new subsurface or buffer is attached. + * + * If never set, the value is the full bounds of the surface, + * including any subsurfaces. This updates dynamically on every + * commit. This unset is meant for extremely simple clients. + * + * The arguments are given in the surface-local coordinate space of + * the wl_surface associated with this xdg_surface. + * + * The width and height must be greater than zero. Setting an invalid size + * will raise an error. When applied, the effective window geometry will be + * the set window geometry clamped to the bounding rectangle of the + * combined geometry of the surface of the xdg_surface and the associated + * subsurfaces. + */ +static inline void +xdg_surface_set_window_geometry(struct xdg_surface *xdg_surface, int32_t x, int32_t y, int32_t width, int32_t height) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_surface, + XDG_SURFACE_SET_WINDOW_GEOMETRY, x, y, width, height); +} + +/** + * @ingroup iface_xdg_surface + * + * When a configure event is received, if a client commits the + * surface in response to the configure event, then the client + * must make an ack_configure request sometime before the commit + * request, passing along the serial of the configure event. + * + * For instance, for toplevel surfaces the compositor might use this + * information to move a surface to the top left only when the client has + * drawn itself for the maximized or fullscreen state. + * + * If the client receives multiple configure events before it + * can respond to one, it only has to ack the last configure event. + * + * A client is not required to commit immediately after sending + * an ack_configure request - it may even ack_configure several times + * before its next surface commit. + * + * A client may send multiple ack_configure requests before committing, but + * only the last request sent before a commit indicates which configure + * event the client really is responding to. + */ +static inline void +xdg_surface_ack_configure(struct xdg_surface *xdg_surface, uint32_t serial) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_surface, + XDG_SURFACE_ACK_CONFIGURE, serial); +} + +#ifndef XDG_TOPLEVEL_RESIZE_EDGE_ENUM +#define XDG_TOPLEVEL_RESIZE_EDGE_ENUM +/** + * @ingroup iface_xdg_toplevel + * edge values for resizing + * + * These values are used to indicate which edge of a surface + * is being dragged in a resize operation. + */ +enum xdg_toplevel_resize_edge +{ + XDG_TOPLEVEL_RESIZE_EDGE_NONE = 0, + XDG_TOPLEVEL_RESIZE_EDGE_TOP = 1, + XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM = 2, + XDG_TOPLEVEL_RESIZE_EDGE_LEFT = 4, + XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT = 5, + XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT = 6, + XDG_TOPLEVEL_RESIZE_EDGE_RIGHT = 8, + XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT = 9, + XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT = 10, +}; +#endif /* XDG_TOPLEVEL_RESIZE_EDGE_ENUM */ + +#ifndef XDG_TOPLEVEL_STATE_ENUM +#define XDG_TOPLEVEL_STATE_ENUM +/** + * @ingroup iface_xdg_toplevel + * the surface is tiled + * + * The window is currently in a tiled layout and the bottom edge is + * considered to be adjacent to another part of the tiling grid. + */ +enum xdg_toplevel_state +{ + /** + * the surface is maximized + */ + XDG_TOPLEVEL_STATE_MAXIMIZED = 1, + /** + * the surface is fullscreen + */ + XDG_TOPLEVEL_STATE_FULLSCREEN = 2, + /** + * the surface is being resized + */ + XDG_TOPLEVEL_STATE_RESIZING = 3, + /** + * the surface is now activated + */ + XDG_TOPLEVEL_STATE_ACTIVATED = 4, + /** + * @since 2 + */ + XDG_TOPLEVEL_STATE_TILED_LEFT = 5, + /** + * @since 2 + */ + XDG_TOPLEVEL_STATE_TILED_RIGHT = 6, + /** + * @since 2 + */ + XDG_TOPLEVEL_STATE_TILED_TOP = 7, + /** + * @since 2 + */ + XDG_TOPLEVEL_STATE_TILED_BOTTOM = 8, +}; +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_STATE_TILED_LEFT_SINCE_VERSION 2 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_STATE_TILED_RIGHT_SINCE_VERSION 2 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_STATE_TILED_TOP_SINCE_VERSION 2 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_STATE_TILED_BOTTOM_SINCE_VERSION 2 +#endif /* XDG_TOPLEVEL_STATE_ENUM */ + +/** + * @ingroup iface_xdg_toplevel + * @struct xdg_toplevel_listener + */ +struct xdg_toplevel_listener +{ + /** + * suggest a surface change + * + * This configure event asks the client to resize its toplevel + * surface or to change its state. The configured state should not + * be applied immediately. See xdg_surface.configure for details. + * + * The width and height arguments specify a hint to the window + * about how its surface should be resized in window geometry + * coordinates. See set_window_geometry. + * + * If the width or height arguments are zero, it means the client + * should decide its own window dimension. This may happen when the + * compositor needs to configure the state of the surface but + * doesn't have any information about any previous or expected + * dimension. + * + * The states listed in the event specify how the width/height + * arguments should be interpreted, and possibly how it should be + * drawn. + * + * Clients must send an ack_configure in response to this event. + * See xdg_surface.configure and xdg_surface.ack_configure for + * details. + */ + void (*configure)(void *data, + struct xdg_toplevel *xdg_toplevel, + int32_t width, + int32_t height, + struct wl_array *states); + /** + * surface wants to be closed + * + * The close event is sent by the compositor when the user wants + * the surface to be closed. This should be equivalent to the user + * clicking the close button in client-side decorations, if your + * application has any. + * + * This is only a request that the user intends to close the + * window. The client may choose to ignore this request, or show a + * dialog to ask the user to save their data, etc. + */ + void (*close)(void *data, + struct xdg_toplevel *xdg_toplevel); +}; + +/** + * @ingroup iface_xdg_toplevel + */ +static inline int +xdg_toplevel_add_listener(struct xdg_toplevel *xdg_toplevel, + const struct xdg_toplevel_listener *listener, void *data) +{ + return wl_proxy_add_listener((struct wl_proxy *) xdg_toplevel, + (void(* *) (void))listener, data); +} + +#define XDG_TOPLEVEL_DESTROY 0 +#define XDG_TOPLEVEL_SET_PARENT 1 +#define XDG_TOPLEVEL_SET_TITLE 2 +#define XDG_TOPLEVEL_SET_APP_ID 3 +#define XDG_TOPLEVEL_SHOW_WINDOW_MENU 4 +#define XDG_TOPLEVEL_MOVE 5 +#define XDG_TOPLEVEL_RESIZE 6 +#define XDG_TOPLEVEL_SET_MAX_SIZE 7 +#define XDG_TOPLEVEL_SET_MIN_SIZE 8 +#define XDG_TOPLEVEL_SET_MAXIMIZED 9 +#define XDG_TOPLEVEL_UNSET_MAXIMIZED 10 +#define XDG_TOPLEVEL_SET_FULLSCREEN 11 +#define XDG_TOPLEVEL_UNSET_FULLSCREEN 12 +#define XDG_TOPLEVEL_SET_MINIMIZED 13 + +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_CONFIGURE_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_CLOSE_SINCE_VERSION 1 + +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_SET_PARENT_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_SET_TITLE_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_SET_APP_ID_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_SHOW_WINDOW_MENU_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_MOVE_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_RESIZE_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_SET_MAX_SIZE_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_SET_MIN_SIZE_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_SET_MAXIMIZED_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_UNSET_MAXIMIZED_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_SET_FULLSCREEN_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_UNSET_FULLSCREEN_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_toplevel + */ +#define XDG_TOPLEVEL_SET_MINIMIZED_SINCE_VERSION 1 + +/** @ingroup iface_xdg_toplevel */ +static inline void +xdg_toplevel_set_user_data(struct xdg_toplevel *xdg_toplevel, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) xdg_toplevel, user_data); +} + +/** @ingroup iface_xdg_toplevel */ +static inline void * +xdg_toplevel_get_user_data(struct xdg_toplevel *xdg_toplevel) +{ + return wl_proxy_get_user_data((struct wl_proxy *) xdg_toplevel); +} + +static inline uint32_t +xdg_toplevel_get_version(struct xdg_toplevel *xdg_toplevel) +{ + return wl_proxy_get_version((struct wl_proxy *) xdg_toplevel); +} + +/** + * @ingroup iface_xdg_toplevel + * + * This request destroys the role surface and unmaps the surface; + * see "Unmapping" behavior in interface section for details. + */ +static inline void +xdg_toplevel_destroy(struct xdg_toplevel *xdg_toplevel) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) xdg_toplevel); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Set the "parent" of this surface. This surface should be stacked + * above the parent surface and all other ancestor surfaces. + * + * Parent windows should be set on dialogs, toolboxes, or other + * "auxiliary" surfaces, so that the parent is raised when the dialog + * is raised. + * + * Setting a null parent for a child window removes any parent-child + * relationship for the child. Setting a null parent for a window which + * currently has no parent is a no-op. + * + * If the parent is unmapped then its children are managed as + * though the parent of the now-unmapped parent has become the + * parent of this surface. If no parent exists for the now-unmapped + * parent then the children are managed as though they have no + * parent surface. + */ +static inline void +xdg_toplevel_set_parent(struct xdg_toplevel *xdg_toplevel, struct xdg_toplevel *parent) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_SET_PARENT, parent); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Set a short title for the surface. + * + * This string may be used to identify the surface in a task bar, + * window list, or other user interface elements provided by the + * compositor. + * + * The string must be encoded in UTF-8. + */ +static inline void +xdg_toplevel_set_title(struct xdg_toplevel *xdg_toplevel, const char *title) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_SET_TITLE, title); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Set an application identifier for the surface. + * + * The app ID identifies the general class of applications to which + * the surface belongs. The compositor can use this to group multiple + * surfaces together, or to determine how to launch a new application. + * + * For D-Bus activatable applications, the app ID is used as the D-Bus + * service name. + * + * The compositor shell will try to group application surfaces together + * by their app ID. As a best practice, it is suggested to select app + * ID's that match the basename of the application's .desktop file. + * For example, "org.freedesktop.FooViewer" where the .desktop file is + * "org.freedesktop.FooViewer.desktop". + * + * Like other properties, a set_app_id request can be sent after the + * xdg_toplevel has been mapped to update the property. + * + * See the desktop-entry specification [0] for more details on + * application identifiers and how they relate to well-known D-Bus + * names and .desktop files. + * + * [0] http://standards.freedesktop.org/desktop-entry-spec/ + */ +static inline void +xdg_toplevel_set_app_id(struct xdg_toplevel *xdg_toplevel, const char *app_id) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_SET_APP_ID, app_id); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Clients implementing client-side decorations might want to show + * a context menu when right-clicking on the decorations, giving the + * user a menu that they can use to maximize or minimize the window. + * + * This request asks the compositor to pop up such a window menu at + * the given position, relative to the local surface coordinates of + * the parent surface. There are no guarantees as to what menu items + * the window menu contains. + * + * This request must be used in response to some sort of user action + * like a button press, key press, or touch down event. + */ +static inline void +xdg_toplevel_show_window_menu(struct xdg_toplevel *xdg_toplevel, struct wl_seat *seat, uint32_t serial, int32_t x, int32_t y) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_SHOW_WINDOW_MENU, seat, serial, x, y); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Start an interactive, user-driven move of the surface. + * + * This request must be used in response to some sort of user action + * like a button press, key press, or touch down event. The passed + * serial is used to determine the type of interactive move (touch, + * pointer, etc). + * + * The server may ignore move requests depending on the state of + * the surface (e.g. fullscreen or maximized), or if the passed serial + * is no longer valid. + * + * If triggered, the surface will lose the focus of the device + * (wl_pointer, wl_touch, etc) used for the move. It is up to the + * compositor to visually indicate that the move is taking place, such as + * updating a pointer cursor, during the move. There is no guarantee + * that the device focus will return when the move is completed. + */ +static inline void +xdg_toplevel_move(struct xdg_toplevel *xdg_toplevel, struct wl_seat *seat, uint32_t serial) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_MOVE, seat, serial); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Start a user-driven, interactive resize of the surface. + * + * This request must be used in response to some sort of user action + * like a button press, key press, or touch down event. The passed + * serial is used to determine the type of interactive resize (touch, + * pointer, etc). + * + * The server may ignore resize requests depending on the state of + * the surface (e.g. fullscreen or maximized). + * + * If triggered, the client will receive configure events with the + * "resize" state enum value and the expected sizes. See the "resize" + * enum value for more details about what is required. The client + * must also acknowledge configure events using "ack_configure". After + * the resize is completed, the client will receive another "configure" + * event without the resize state. + * + * If triggered, the surface also will lose the focus of the device + * (wl_pointer, wl_touch, etc) used for the resize. It is up to the + * compositor to visually indicate that the resize is taking place, + * such as updating a pointer cursor, during the resize. There is no + * guarantee that the device focus will return when the resize is + * completed. + * + * The edges parameter specifies how the surface should be resized, + * and is one of the values of the resize_edge enum. The compositor + * may use this information to update the surface position for + * example when dragging the top left corner. The compositor may also + * use this information to adapt its behavior, e.g. choose an + * appropriate cursor image. + */ +static inline void +xdg_toplevel_resize(struct xdg_toplevel *xdg_toplevel, struct wl_seat *seat, uint32_t serial, uint32_t edges) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_RESIZE, seat, serial, edges); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Set a maximum size for the window. + * + * The client can specify a maximum size so that the compositor does + * not try to configure the window beyond this size. + * + * The width and height arguments are in window geometry coordinates. + * See xdg_surface.set_window_geometry. + * + * Values set in this way are double-buffered. They will get applied + * on the next commit. + * + * The compositor can use this information to allow or disallow + * different states like maximize or fullscreen and draw accurate + * animations. + * + * Similarly, a tiling window manager may use this information to + * place and resize client windows in a more effective way. + * + * The client should not rely on the compositor to obey the maximum + * size. The compositor may decide to ignore the values set by the + * client and request a larger size. + * + * If never set, or a value of zero in the request, means that the + * client has no expected maximum size in the given dimension. + * As a result, a client wishing to reset the maximum size + * to an unspecified state can use zero for width and height in the + * request. + * + * Requesting a maximum size to be smaller than the minimum size of + * a surface is illegal and will result in a protocol error. + * + * The width and height must be greater than or equal to zero. Using + * strictly negative values for width and height will result in a + * protocol error. + */ +static inline void +xdg_toplevel_set_max_size(struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_SET_MAX_SIZE, width, height); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Set a minimum size for the window. + * + * The client can specify a minimum size so that the compositor does + * not try to configure the window below this size. + * + * The width and height arguments are in window geometry coordinates. + * See xdg_surface.set_window_geometry. + * + * Values set in this way are double-buffered. They will get applied + * on the next commit. + * + * The compositor can use this information to allow or disallow + * different states like maximize or fullscreen and draw accurate + * animations. + * + * Similarly, a tiling window manager may use this information to + * place and resize client windows in a more effective way. + * + * The client should not rely on the compositor to obey the minimum + * size. The compositor may decide to ignore the values set by the + * client and request a smaller size. + * + * If never set, or a value of zero in the request, means that the + * client has no expected minimum size in the given dimension. + * As a result, a client wishing to reset the minimum size + * to an unspecified state can use zero for width and height in the + * request. + * + * Requesting a minimum size to be larger than the maximum size of + * a surface is illegal and will result in a protocol error. + * + * The width and height must be greater than or equal to zero. Using + * strictly negative values for width and height will result in a + * protocol error. + */ +static inline void +xdg_toplevel_set_min_size(struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_SET_MIN_SIZE, width, height); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Maximize the surface. + * + * After requesting that the surface should be maximized, the compositor + * will respond by emitting a configure event. Whether this configure + * actually sets the window maximized is subject to compositor policies. + * The client must then update its content, drawing in the configured + * state. The client must also acknowledge the configure when committing + * the new content (see ack_configure). + * + * It is up to the compositor to decide how and where to maximize the + * surface, for example which output and what region of the screen should + * be used. + * + * If the surface was already maximized, the compositor will still emit + * a configure event with the "maximized" state. + * + * If the surface is in a fullscreen state, this request has no direct + * effect. It may alter the state the surface is returned to when + * unmaximized unless overridden by the compositor. + */ +static inline void +xdg_toplevel_set_maximized(struct xdg_toplevel *xdg_toplevel) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_SET_MAXIMIZED); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Unmaximize the surface. + * + * After requesting that the surface should be unmaximized, the compositor + * will respond by emitting a configure event. Whether this actually + * un-maximizes the window is subject to compositor policies. + * If available and applicable, the compositor will include the window + * geometry dimensions the window had prior to being maximized in the + * configure event. The client must then update its content, drawing it in + * the configured state. The client must also acknowledge the configure + * when committing the new content (see ack_configure). + * + * It is up to the compositor to position the surface after it was + * unmaximized; usually the position the surface had before maximizing, if + * applicable. + * + * If the surface was already not maximized, the compositor will still + * emit a configure event without the "maximized" state. + * + * If the surface is in a fullscreen state, this request has no direct + * effect. It may alter the state the surface is returned to when + * unmaximized unless overridden by the compositor. + */ +static inline void +xdg_toplevel_unset_maximized(struct xdg_toplevel *xdg_toplevel) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_UNSET_MAXIMIZED); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Make the surface fullscreen. + * + * After requesting that the surface should be fullscreened, the + * compositor will respond by emitting a configure event. Whether the + * client is actually put into a fullscreen state is subject to compositor + * policies. The client must also acknowledge the configure when + * committing the new content (see ack_configure). + * + * The output passed by the request indicates the client's preference as + * to which display it should be set fullscreen on. If this value is NULL, + * it's up to the compositor to choose which display will be used to map + * this surface. + * + * If the surface doesn't cover the whole output, the compositor will + * position the surface in the center of the output and compensate with + * with border fill covering the rest of the output. The content of the + * border fill is undefined, but should be assumed to be in some way that + * attempts to blend into the surrounding area (e.g. solid black). + * + * If the fullscreened surface is not opaque, the compositor must make + * sure that other screen content not part of the same surface tree (made + * up of subsurfaces, popups or similarly coupled surfaces) are not + * visible below the fullscreened surface. + */ +static inline void +xdg_toplevel_set_fullscreen(struct xdg_toplevel *xdg_toplevel, struct wl_output *output) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_SET_FULLSCREEN, output); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Make the surface no longer fullscreen. + * + * After requesting that the surface should be unfullscreened, the + * compositor will respond by emitting a configure event. + * Whether this actually removes the fullscreen state of the client is + * subject to compositor policies. + * + * Making a surface unfullscreen sets states for the surface based on the following: + * * the state(s) it may have had before becoming fullscreen + * * any state(s) decided by the compositor + * * any state(s) requested by the client while the surface was fullscreen + * + * The compositor may include the previous window geometry dimensions in + * the configure event, if applicable. + * + * The client must also acknowledge the configure when committing the new + * content (see ack_configure). + */ +static inline void +xdg_toplevel_unset_fullscreen(struct xdg_toplevel *xdg_toplevel) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_UNSET_FULLSCREEN); +} + +/** + * @ingroup iface_xdg_toplevel + * + * Request that the compositor minimize your surface. There is no + * way to know if the surface is currently minimized, nor is there + * any way to unset minimization on this surface. + * + * If you are looking to throttle redrawing when minimized, please + * instead use the wl_surface.frame event for this, as this will + * also work with live previews on windows in Alt-Tab, Expose or + * similar compositor features. + */ +static inline void +xdg_toplevel_set_minimized(struct xdg_toplevel *xdg_toplevel) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_toplevel, + XDG_TOPLEVEL_SET_MINIMIZED); +} + +#ifndef XDG_POPUP_ERROR_ENUM +#define XDG_POPUP_ERROR_ENUM +enum xdg_popup_error +{ + /** + * tried to grab after being mapped + */ + XDG_POPUP_ERROR_INVALID_GRAB = 0, +}; +#endif /* XDG_POPUP_ERROR_ENUM */ + +/** + * @ingroup iface_xdg_popup + * @struct xdg_popup_listener + */ +struct xdg_popup_listener +{ + /** + * configure the popup surface + * + * This event asks the popup surface to configure itself given + * the configuration. The configured state should not be applied + * immediately. See xdg_surface.configure for details. + * + * The x and y arguments represent the position the popup was + * placed at given the xdg_positioner rule, relative to the upper + * left corner of the window geometry of the parent surface. + * + * For version 2 or older, the configure event for an xdg_popup is + * only ever sent once for the initial configuration. Starting with + * version 3, it may be sent again if the popup is setup with an + * xdg_positioner with set_reactive requested, or in response to + * xdg_popup.reposition requests. + * @param x x position relative to parent surface window geometry + * @param y y position relative to parent surface window geometry + * @param width window geometry width + * @param height window geometry height + */ + void (*configure)(void *data, + struct xdg_popup *xdg_popup, + int32_t x, + int32_t y, + int32_t width, + int32_t height); + /** + * popup interaction is done + * + * The popup_done event is sent out when a popup is dismissed by + * the compositor. The client should destroy the xdg_popup object + * at this point. + */ + void (*popup_done)(void *data, + struct xdg_popup *xdg_popup); + /** + * signal the completion of a repositioned request + * + * The repositioned event is sent as part of a popup + * configuration sequence, together with xdg_popup.configure and + * lastly xdg_surface.configure to notify the completion of a + * reposition request. + * + * The repositioned event is to notify about the completion of a + * xdg_popup.reposition request. The token argument is the token + * passed in the xdg_popup.reposition request. + * + * Immediately after this event is emitted, xdg_popup.configure and + * xdg_surface.configure will be sent with the updated size and + * position, as well as a new configure serial. + * + * The client should optionally update the content of the popup, + * but must acknowledge the new popup configuration for the new + * position to take effect. See xdg_surface.ack_configure for + * details. + * @param token reposition request token + * @since 3 + */ + void (*repositioned)(void *data, + struct xdg_popup *xdg_popup, + uint32_t token); +}; + +/** + * @ingroup iface_xdg_popup + */ +static inline int +xdg_popup_add_listener(struct xdg_popup *xdg_popup, + const struct xdg_popup_listener *listener, void *data) +{ + return wl_proxy_add_listener((struct wl_proxy *) xdg_popup, + (void(* *) (void))listener, data); +} + +#define XDG_POPUP_DESTROY 0 +#define XDG_POPUP_GRAB 1 +#define XDG_POPUP_REPOSITION 2 + +/** + * @ingroup iface_xdg_popup + */ +#define XDG_POPUP_CONFIGURE_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_popup + */ +#define XDG_POPUP_POPUP_DONE_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_popup + */ +#define XDG_POPUP_REPOSITIONED_SINCE_VERSION 3 + +/** + * @ingroup iface_xdg_popup + */ +#define XDG_POPUP_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_popup + */ +#define XDG_POPUP_GRAB_SINCE_VERSION 1 +/** + * @ingroup iface_xdg_popup + */ +#define XDG_POPUP_REPOSITION_SINCE_VERSION 3 + +/** @ingroup iface_xdg_popup */ +static inline void +xdg_popup_set_user_data(struct xdg_popup *xdg_popup, void *user_data) +{ + wl_proxy_set_user_data((struct wl_proxy *) xdg_popup, user_data); +} + +/** @ingroup iface_xdg_popup */ +static inline void * +xdg_popup_get_user_data(struct xdg_popup *xdg_popup) +{ + return wl_proxy_get_user_data((struct wl_proxy *) xdg_popup); +} + +static inline uint32_t +xdg_popup_get_version(struct xdg_popup *xdg_popup) +{ + return wl_proxy_get_version((struct wl_proxy *) xdg_popup); +} + +/** + * @ingroup iface_xdg_popup + * + * This destroys the popup. Explicitly destroying the xdg_popup + * object will also dismiss the popup, and unmap the surface. + * + * If this xdg_popup is not the "topmost" popup, a protocol error + * will be sent. + */ +static inline void +xdg_popup_destroy(struct xdg_popup *xdg_popup) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_popup, + XDG_POPUP_DESTROY); + + wl_proxy_destroy((struct wl_proxy *) xdg_popup); +} + +/** + * @ingroup iface_xdg_popup + * + * This request makes the created popup take an explicit grab. An explicit + * grab will be dismissed when the user dismisses the popup, or when the + * client destroys the xdg_popup. This can be done by the user clicking + * outside the surface, using the keyboard, or even locking the screen + * through closing the lid or a timeout. + * + * If the compositor denies the grab, the popup will be immediately + * dismissed. + * + * This request must be used in response to some sort of user action like a + * button press, key press, or touch down event. The serial number of the + * event should be passed as 'serial'. + * + * The parent of a grabbing popup must either be an xdg_toplevel surface or + * another xdg_popup with an explicit grab. If the parent is another + * xdg_popup it means that the popups are nested, with this popup now being + * the topmost popup. + * + * Nested popups must be destroyed in the reverse order they were created + * in, e.g. the only popup you are allowed to destroy at all times is the + * topmost one. + * + * When compositors choose to dismiss a popup, they may dismiss every + * nested grabbing popup as well. When a compositor dismisses popups, it + * will follow the same dismissing order as required from the client. + * + * The parent of a grabbing popup must either be another xdg_popup with an + * active explicit grab, or an xdg_popup or xdg_toplevel, if there are no + * explicit grabs already taken. + * + * If the topmost grabbing popup is destroyed, the grab will be returned to + * the parent of the popup, if that parent previously had an explicit grab. + * + * If the parent is a grabbing popup which has already been dismissed, this + * popup will be immediately dismissed. If the parent is a popup that did + * not take an explicit grab, an error will be raised. + * + * During a popup grab, the client owning the grab will receive pointer + * and touch events for all their surfaces as normal (similar to an + * "owner-events" grab in X11 parlance), while the top most grabbing popup + * will always have keyboard focus. + */ +static inline void +xdg_popup_grab(struct xdg_popup *xdg_popup, struct wl_seat *seat, uint32_t serial) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_popup, + XDG_POPUP_GRAB, seat, serial); +} + +/** + * @ingroup iface_xdg_popup + * + * Reposition an already-mapped popup. The popup will be placed given the + * details in the passed xdg_positioner object, and a + * xdg_popup.repositioned followed by xdg_popup.configure and + * xdg_surface.configure will be emitted in response. Any parameters set + * by the previous positioner will be discarded. + * + * The passed token will be sent in the corresponding + * xdg_popup.repositioned event. The new popup position will not take + * effect until the corresponding configure event is acknowledged by the + * client. See xdg_popup.repositioned for details. The token itself is + * opaque, and has no other special meaning. + * + * If multiple reposition requests are sent, the compositor may skip all + * but the last one. + * + * If the popup is repositioned in response to a configure event for its + * parent, the client should send an xdg_positioner.set_parent_configure + * and possibly a xdg_positioner.set_parent_size request to allow the + * compositor to properly constrain the popup. + * + * If the popup is repositioned together with a parent that is being + * resized, but not in response to a configure event, the client should + * send a xdg_positioner.set_parent_size request. + */ +static inline void +xdg_popup_reposition(struct xdg_popup *xdg_popup, struct xdg_positioner *positioner, uint32_t token) +{ + wl_proxy_marshal((struct wl_proxy *) xdg_popup, + XDG_POPUP_REPOSITION, positioner, token); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/protocol/xdg-shell-protocol.c b/protocol/xdg-shell-protocol.c new file mode 100644 index 0000000..d0bf0ac --- /dev/null +++ b/protocol/xdg-shell-protocol.c @@ -0,0 +1,180 @@ +/* Generated by wayland-scanner 1.19.0 */ + +/* + * Copyright © 2008-2013 Kristian Høgsberg + * Copyright © 2013 Rafael Antognolli + * Copyright © 2013 Jasper St. Pierre + * Copyright © 2010-2013 Intel Corporation + * Copyright © 2015-2017 Samsung Electronics Co., Ltd + * Copyright © 2015-2017 Red Hat Inc. + * + * 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 +#include +#include "wayland-util.h" + +#ifndef __has_attribute +# define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ +#endif + +#if (__has_attribute(visibility) || defined(__GNUC__) && __GNUC__ >= 4) +#define WL_PRIVATE __attribute__ ((visibility("hidden"))) +#else +#define WL_PRIVATE +#endif + +extern const struct wl_interface wl_output_interface; +extern const struct wl_interface wl_seat_interface; +extern const struct wl_interface wl_surface_interface; +extern const struct wl_interface xdg_popup_interface; +extern const struct wl_interface xdg_positioner_interface; +extern const struct wl_interface xdg_surface_interface; +extern const struct wl_interface xdg_toplevel_interface; + +static const struct wl_interface *xdg_shell_types[] = { + NULL, + NULL, + NULL, + NULL, + &xdg_positioner_interface, + &xdg_surface_interface, + &wl_surface_interface, + &xdg_toplevel_interface, + &xdg_popup_interface, + &xdg_surface_interface, + &xdg_positioner_interface, + &xdg_toplevel_interface, + &wl_seat_interface, + NULL, + NULL, + NULL, + &wl_seat_interface, + NULL, + &wl_seat_interface, + NULL, + NULL, + &wl_output_interface, + &wl_seat_interface, + NULL, + &xdg_positioner_interface, + NULL, +}; + +static const struct wl_message xdg_wm_base_requests[] = { + { "destroy", "", xdg_shell_types + 0 }, + { "create_positioner", "n", xdg_shell_types + 4 }, + { "get_xdg_surface", "no", xdg_shell_types + 5 }, + { "pong", "u", xdg_shell_types + 0 }, +}; + +static const struct wl_message xdg_wm_base_events[] = { + { "ping", "u", xdg_shell_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface xdg_wm_base_interface = { + "xdg_wm_base", 3, + 4, xdg_wm_base_requests, + 1, xdg_wm_base_events, +}; + +static const struct wl_message xdg_positioner_requests[] = { + { "destroy", "", xdg_shell_types + 0 }, + { "set_size", "ii", xdg_shell_types + 0 }, + { "set_anchor_rect", "iiii", xdg_shell_types + 0 }, + { "set_anchor", "u", xdg_shell_types + 0 }, + { "set_gravity", "u", xdg_shell_types + 0 }, + { "set_constraint_adjustment", "u", xdg_shell_types + 0 }, + { "set_offset", "ii", xdg_shell_types + 0 }, + { "set_reactive", "3", xdg_shell_types + 0 }, + { "set_parent_size", "3ii", xdg_shell_types + 0 }, + { "set_parent_configure", "3u", xdg_shell_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface xdg_positioner_interface = { + "xdg_positioner", 3, + 10, xdg_positioner_requests, + 0, NULL, +}; + +static const struct wl_message xdg_surface_requests[] = { + { "destroy", "", xdg_shell_types + 0 }, + { "get_toplevel", "n", xdg_shell_types + 7 }, + { "get_popup", "n?oo", xdg_shell_types + 8 }, + { "set_window_geometry", "iiii", xdg_shell_types + 0 }, + { "ack_configure", "u", xdg_shell_types + 0 }, +}; + +static const struct wl_message xdg_surface_events[] = { + { "configure", "u", xdg_shell_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface xdg_surface_interface = { + "xdg_surface", 3, + 5, xdg_surface_requests, + 1, xdg_surface_events, +}; + +static const struct wl_message xdg_toplevel_requests[] = { + { "destroy", "", xdg_shell_types + 0 }, + { "set_parent", "?o", xdg_shell_types + 11 }, + { "set_title", "s", xdg_shell_types + 0 }, + { "set_app_id", "s", xdg_shell_types + 0 }, + { "show_window_menu", "ouii", xdg_shell_types + 12 }, + { "move", "ou", xdg_shell_types + 16 }, + { "resize", "ouu", xdg_shell_types + 18 }, + { "set_max_size", "ii", xdg_shell_types + 0 }, + { "set_min_size", "ii", xdg_shell_types + 0 }, + { "set_maximized", "", xdg_shell_types + 0 }, + { "unset_maximized", "", xdg_shell_types + 0 }, + { "set_fullscreen", "?o", xdg_shell_types + 21 }, + { "unset_fullscreen", "", xdg_shell_types + 0 }, + { "set_minimized", "", xdg_shell_types + 0 }, +}; + +static const struct wl_message xdg_toplevel_events[] = { + { "configure", "iia", xdg_shell_types + 0 }, + { "close", "", xdg_shell_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface xdg_toplevel_interface = { + "xdg_toplevel", 3, + 14, xdg_toplevel_requests, + 2, xdg_toplevel_events, +}; + +static const struct wl_message xdg_popup_requests[] = { + { "destroy", "", xdg_shell_types + 0 }, + { "grab", "ou", xdg_shell_types + 22 }, + { "reposition", "3ou", xdg_shell_types + 24 }, +}; + +static const struct wl_message xdg_popup_events[] = { + { "configure", "iiii", xdg_shell_types + 0 }, + { "popup_done", "", xdg_shell_types + 0 }, + { "repositioned", "3u", xdg_shell_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface xdg_popup_interface = { + "xdg_popup", 3, + 3, xdg_popup_requests, + 3, xdg_popup_events, +}; diff --git a/screenshot.c b/screenshot.c new file mode 100644 index 0000000..d586310 --- /dev/null +++ b/screenshot.c @@ -0,0 +1,1061 @@ +/* + * Copyright © 2008 Kristian Høgsberg + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "weston-screenshooter-client-protocol.h" +#include "shared/os-compatibility.h" +#include "shared/xalloc.h" +#include "shared/file-util.h" +#include "shared/cairo-util.h" +#include "shared/helpers.h" +#include "window.h" + +/* The screenshooter is a good example of a custom object exposed by + * the compositor and serves as a test bed for implementing client + * side marshalling outside libwayland.so */ + +int isloop = 1; +struct image *pimage; + +struct screenshooter_output +{ + struct wl_output *output; + struct wl_buffer *buffer; + int width, height, offset_x, offset_y; + void *data; + struct wl_list link; +}; + +struct buffer_size +{ + int width, height; + + int min_x, min_y; + int max_x, max_y; +}; + +struct screenshooter_data +{ + struct wl_shm *shm; + struct wl_list output_list; + + struct weston_screenshooter *screenshooter; + int buffer_copy_done; +}; + +struct image +{ + struct window *window; + struct widget *widget; + struct display *display; + char *filename; + cairo_surface_t *image; + int fullscreen; + int *image_counter; + int32_t width, height; + + struct + { + double x; + double y; + } pointer; + bool button_pressed; + + bool initialized; + cairo_matrix_t matrix; +}; + +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 xdg_wm_base *xdg_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 data_device_manager_version; + struct wp_viewporter *viewporter; +}; + +static void handler_sigint(int sig) +{ + isloop = 0; +} + +static void +display_handle_geometry(void *data, + struct wl_output *wl_output, + int x, + int y, + int physical_width, + int physical_height, + int subpixel, + const char *make, + const char *model, + int transform) +{ + struct screenshooter_output *output; + + output = wl_output_get_user_data(wl_output); + + if (wl_output == output->output) + { + output->offset_x = x; + output->offset_y = y; + } +} + +static void +display_handle_mode(void *data, + struct wl_output *wl_output, + uint32_t flags, + int width, + int height, + int refresh) +{ + struct screenshooter_output *output; + + output = wl_output_get_user_data(wl_output); + + if ((wl_output == output->output) && (flags & WL_OUTPUT_MODE_CURRENT)) + { + output->width = width; + output->height = height; + } +} + +static const struct wl_output_listener output_listener = { + display_handle_geometry, + display_handle_mode +}; + +static void +screenshot_done(void *data, struct weston_screenshooter *screenshooter) +{ + struct screenshooter_data *sh_data = data; + sh_data->buffer_copy_done = 1; +} + +static const struct weston_screenshooter_listener screenshooter_listener = { + screenshot_done +}; + +static void +handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) +{ + static struct screenshooter_output *output; + struct screenshooter_data *sh_data = data; + + if (strcmp(interface, "wl_output") == 0) + { + output = xmalloc(sizeof *output); + output->output = wl_registry_bind(registry, name, + &wl_output_interface, 1); + wl_list_insert(&sh_data->output_list, &output->link); + wl_output_add_listener(output->output, &output_listener, output); + } + else if (strcmp(interface, "wl_shm") == 0) + { + sh_data->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); + } + else if (strcmp(interface, "weston_screenshooter") == 0) + { + sh_data->screenshooter = wl_registry_bind(registry, name, + &weston_screenshooter_interface, + 1); + } +} + +static void +handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) +{ + /* XXX: unimplemented */ +} + +static const struct wl_registry_listener registry_listener = { + handle_global, + handle_global_remove +}; + +static struct wl_buffer * +screenshot_create_shm_buffer(int width, int height, void **data_out, + struct wl_shm *shm) +{ + struct wl_shm_pool *pool; + struct wl_buffer *buffer; + int fd, size, stride; + void *data; + + stride = width * 4; + size = stride * height; + + // printf("screenshot_create_shm_buffer width[%d] height[%d] stride[%d] size[%d]\n",width,height,stride,size); + fd = os_create_anonymous_file(size); + if (fd < 0) + { + fprintf(stderr, "creating a buffer file for %d B failed: %s\n", + size, strerror(errno)); + return NULL; + } + + data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (data == MAP_FAILED) + { + fprintf(stderr, "mmap failed: %s\n", strerror(errno)); + close(fd); + return NULL; + } + + pool = wl_shm_create_pool(shm, fd, size); + close(fd); + buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, + WL_SHM_FORMAT_XRGB8888); + wl_shm_pool_destroy(pool); + + *data_out = data; + + return buffer; +} + + +static double +get_scale(struct image *image) +{ + assert(image->matrix.xy == 0.0 && + image->matrix.yx == 0.0 && + image->matrix.xx == image->matrix.yy); + return image->matrix.xx; +} + +static void +clamp_view(struct image *image) +{ + struct rectangle allocation; + double scale = get_scale(image); + double sw, sh; + // printf("clamp scale[%f]\n",scale); + // printf("clamp image->width[%d] height[%d]\n",image->width,image->height); + + sw = image->width * scale; + sh = image->height * scale; + widget_get_allocation(image->widget, &allocation); + + // printf("clamp sw[%f] sh[%f]\n",sw,sh); + // printf("clamp allocation.width[%d] height[%d]\n"),allocation.width,allocation.height; + + if (sw < allocation.width) + { + image->matrix.x0 = + (allocation.width - image->width * scale) / 2; + } + else + { + if (image->matrix.x0 > 0.0) + { + image->matrix.x0 = 0.0; + } + if (sw + image->matrix.x0 < allocation.width) + { + image->matrix.x0 = allocation.width - sw; + } + } + + if (sh < allocation.height) + { + image->matrix.y0 = + (allocation.height - image->height * scale) / 2; + } + else + { + if (image->matrix.y0 > 0.0) + { + image->matrix.y0 = 0.0; + } + if (sh + image->matrix.y0 < allocation.height) + { + image->matrix.y0 = allocation.height - sh; + } + } + + // printf("clamp matrix x0:%f y0:%fxx:%f xy:%f yx:%f yy:%f\n",image->matrix.x0,image->matrix.y0, image->matrix.xx,image->matrix.xy,image->matrix.yx,image->matrix.yy); + // printf("x0[%f] y0[%f]\n",image->matrix.x0, image->matrix.y0); +} + +static void +redraw_handler(struct widget *widget, void *data) +{ + struct image *image = data; + struct rectangle allocation; + cairo_t *cr; + cairo_surface_t *surface; + double width, height, doc_aspect, window_aspect, scale; + cairo_matrix_t matrix; + cairo_matrix_t translate; + + surface = window_get_surface(image->window); + cr = cairo_create(surface); + widget_get_allocation(image->widget, &allocation); + // printf("allocation x[%d] y[%d] width[%d] height[%d]\n", allocation.x, allocation.y, allocation.width,allocation.height); + + cairo_rectangle(cr, allocation.x, allocation.y, + allocation.width, allocation.height); + cairo_clip(cr); + cairo_push_group(cr); + cairo_translate(cr, allocation.x, allocation.y); + + cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); + // cairo_set_source_rgba(cr, 1, 0, 1, 0.5); // magenta transparency 50% + cairo_set_source_rgba(cr, 0, 0, 0, 0); + cairo_paint(cr); + + if (!image->initialized) + { + image->initialized = true; + width = cairo_image_surface_get_width(image->image); + height = cairo_image_surface_get_height(image->image); + + doc_aspect = width / height; + window_aspect = (double) allocation.width / allocation.height; + if (doc_aspect < window_aspect) + { + scale = allocation.height / height; + } + else + { + scale = allocation.width / width; + } + + // printf("doc_aspect[%f]\n",doc_aspect); + // printf("window_aspect[%f]\n",window_aspect); + // printf("scale[%f]\n",scale); + + image->width = width; + image->height = height; + cairo_matrix_init_scale(&image->matrix, scale, scale); + + clamp_view(image); + } + + matrix = image->matrix; + cairo_matrix_init_translate(&translate, allocation.x, allocation.y); + // printf("init matrix x0:%f y0:%f xx:%f xy:%f yx:%f yy:%f\n",matrix.x0,matrix.y0,matrix.xx,matrix.xy,matrix.yx,matrix.yy); + cairo_matrix_multiply(&matrix, &matrix, &translate); + // printf("mult matrix x0:%f y0:%f xx:%f xy:%f yx:%f yy:%f\n",matrix.x0,matrix.y0,matrix.xx,matrix.xy,matrix.yx,matrix.yy); + cairo_set_matrix(cr, &matrix); + + cairo_set_source_surface(cr, image->image, 0, 0); + cairo_set_operator(cr, CAIRO_OPERATOR_OVER); + cairo_paint(cr); + + cairo_pop_group_to_source(cr); + cairo_paint(cr); + cairo_destroy(cr); + + cairo_surface_destroy(surface); +} + +static void +resize_handler(struct widget *widget, + int32_t width, int32_t height, void *data) +{ + struct image *image = data; + + clamp_view(image); +} + +static void +keyboard_focus_handler(struct window *window, + struct input *device, void *data) +{ + struct image *image = data; + + window_schedule_redraw(image->window); +} + +static int +enter_handler(struct widget *widget, + struct input *input, + float x, float y, void *data) +{ + struct image *image = data; + struct rectangle allocation; + + widget_get_allocation(image->widget, &allocation); + x -= allocation.x; + y -= allocation.y; + + image->pointer.x = x; + image->pointer.y = y; + + return 1; +} + +static void +move_viewport(struct image *image, double dx, double dy) +{ + double scale = get_scale(image); + + if (!image->initialized) + { + return; + } + + cairo_matrix_translate(&image->matrix, -dx/scale, -dy/scale); + clamp_view(image); + + window_schedule_redraw(image->window); +} + +static int +motion_handler(struct widget *widget, + struct input *input, uint32_t time, + float x, float y, void *data) +{ + struct image *image = data; + struct rectangle allocation; + + widget_get_allocation(image->widget, &allocation); + x -= allocation.x; + y -= allocation.y; + + if (image->button_pressed) + { + move_viewport(image, image->pointer.x - x, + image->pointer.y - y); + } + + image->pointer.x = x; + image->pointer.y = y; + + return image->button_pressed ? CURSOR_DRAGGING : CURSOR_LEFT_PTR; +} + +static void +button_handler(struct widget *widget, + struct input *input, uint32_t time, + uint32_t button, + enum wl_pointer_button_state state, + void *data) +{ + struct image *image = data; + + if (button == BTN_LEFT) + { + image->button_pressed = + state == WL_POINTER_BUTTON_STATE_PRESSED; + + if (state == WL_POINTER_BUTTON_STATE_PRESSED) + { + input_set_pointer_image(input, CURSOR_DRAGGING); + } + else + { + input_set_pointer_image(input, CURSOR_LEFT_PTR); + } + } +} + +static void +zoom(struct image *image, double scale) +{ + double x = image->pointer.x; + double y = image->pointer.y; + cairo_matrix_t scale_matrix; + + if (!image->initialized) + { + return; + } + + if ((get_scale(image) * scale > 20.0) || + (get_scale(image) * scale < 0.02)) + { + return; + } + + cairo_matrix_init_identity(&scale_matrix); + cairo_matrix_translate(&scale_matrix, x, y); + cairo_matrix_scale(&scale_matrix, scale, scale); + cairo_matrix_translate(&scale_matrix, -x, -y); + + cairo_matrix_multiply(&image->matrix, &image->matrix, &scale_matrix); + clamp_view(image); +} + +static void +key_handler(struct window *window, struct input *input, uint32_t time, + uint32_t key, uint32_t sym, enum wl_keyboard_key_state state, + void *data) +{ + struct image *image = data; + int transform; + + if (state == WL_KEYBOARD_KEY_STATE_RELEASED) + { + return; + } + + // printf("XKB_KEY_f[%x] XKB_KEY_t[%x]\n",XKB_KEY_f,XKB_KEY_t); + // printf("sym[%x]\n",sym); + + switch (sym) + { +/* + case XKB_KEY_minus: + zoom(image, 0.8); + window_schedule_redraw(image->window); + break; + case XKB_KEY_equal: + case XKB_KEY_plus: + zoom(image, 1.2); + window_schedule_redraw(image->window); + break; + + case XKB_KEY_1: + image->matrix.xx = 1.0; + image->matrix.xy = 0.0; + image->matrix.yx = 0.0; + image->matrix.yy = 1.0; + clamp_view(image); + window_schedule_redraw(image->window); + break; + + case XKB_KEY_2: + image->matrix.xx = 0.5; + image->matrix.xy = 0.0; + image->matrix.yx = 0.0; + image->matrix.yy = 0.5; + clamp_view(image); + window_schedule_redraw(image->window); + break; + */ + case XKB_KEY_f: + image->fullscreen ^= 1; + printf("image->fullscreen[%d]\n",image->fullscreen); + + window_set_fullscreen(image->window, image->fullscreen); + clamp_view(image); + window_schedule_redraw(image->window); + break; + +/* + case XKB_KEY_t: + transform = window_get_buffer_transform (image->window); + printf("transform[%d]\n",transform); + transform = (transform + 1) % 8; + window_set_buffer_transform(image->window, transform); + window_schedule_redraw(image->window); + break; + */ + case XKB_KEY_q: + exit(0); + break; + } +} + +static void +axis_handler(struct widget *widget, struct input *input, uint32_t time, + uint32_t axis, wl_fixed_t value, void *data) +{ + struct image *image = data; + + if ((axis == WL_POINTER_AXIS_VERTICAL_SCROLL) && + (input_get_modifiers(input) == MOD_CONTROL_MASK)) + { + /* set zoom level to 2% per 10 axis units */ + zoom(image, (1.0 - wl_fixed_to_double(value) / 500.0)); + + window_schedule_redraw(image->window); + } + else if (input_get_modifiers(input) == 0) + { + if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) + { + move_viewport(image, 0, wl_fixed_to_double(value)); + } + else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) + { + move_viewport(image, wl_fixed_to_double(value), 0); + } + } +} + +static void +fullscreen_handler(struct window *window, void *data) +{ + struct image *image = data; + + // image->fullscreen ^= 1; + window_set_fullscreen(window, image->fullscreen); +} + +int is_pass_close_handler = 0; +static void +close_handler(void *data) +{ + struct image *image = data; + + *image->image_counter -= 1; + + if (*image->image_counter == 0) + { + display_exit(image->display); + } + + widget_destroy(image->widget); + window_destroy(image->window); + + free(image); + + is_pass_close_handler = 1; +} + +static char * +get_screenshot(const struct buffer_size *buff_size, + struct wl_list *output_list) +{ + int output_stride, buffer_stride, i; + cairo_surface_t *surface; + char *data, *d, *s; + struct screenshooter_output *output, *next; + FILE *fp; + char filepath[PATH_MAX]; + + buffer_stride = buff_size->width * 4; + + // printf("buffer_stride[%d] * buff_size->height[%d] = %d\n",buffer_stride, buff_size->height, buffer_stride * buff_size->height); + data = xmalloc(1024 * 4 * 768); + if (!data) + { + return; + } + + wl_list_for_each_safe(output, next, output_list, link) + { + if (output->offset_x == 0) + { + output_stride = 4; + d = data + 1024 * 4 * 767 + 1023 * 4; + s = output->data; + + for (i = 0; i < 1024*768; i++) + { + memcpy(d, s, output_stride); + d -= output_stride; + s += output_stride; + } + + wl_buffer_destroy(output->buffer); + int ret = munmap(output->data, 1024*4*768); + // printf("munmap error[%d]\n",ret); + // printf("%s\n",strerror(errno)); + // free(output->buffer); + } + + wl_registry_destroy(output->output); + free(output); + } + + // fp = file_create_dated("/tmp", "raw-", + // ".png", filepath, sizeof(filepath)); + // if (fp) { + // fwrite(data, 1, 1024 * 4 * 768, fp); + // fclose (fp); + // } + + return data; +} + +static struct image * +image_create(struct display *display, const char *filename, + int *image_counter, char *cap_img) +{ + struct image *image; + char *b, *copy, title[512]; + + image = zalloc(sizeof *image); + if (image == NULL) + { + return image; + } + + sprintf(title,"weston_clone_app"); + image->filename = strdup(title); + + image->image = cairo_image_surface_create_for_data(cap_img, CAIRO_FORMAT_ARGB32, 1024, 768, 1024*4); + double width = cairo_image_surface_get_width(image->image); + double height = cairo_image_surface_get_height(image->image); + printf("1 width[%f] height[%f]\n",width,height); + + if (!image->image) + { + free(image->filename); + free(image); + + printf("image->image failed\n"); + return NULL; + } + + image->window = window_create(display); + image->widget = window_frame_create(image->window, image); + window_set_title(image->window, title); + image->display = display; + image->image_counter = image_counter; + *image_counter += 1; + image->initialized = false; + + window_set_user_data(image->window, image); + widget_set_redraw_handler(image->widget, redraw_handler); + // widget_set_resize_handler(image->widget, resize_handler); + // window_set_keyboard_focus_handler(image->window, keyboard_focus_handler); + // window_set_fullscreen_handler(image->window, fullscreen_handler); + window_set_close_handler(image->window, close_handler); + + // widget_set_enter_handler(image->widget, enter_handler); + // widget_set_motion_handler(image->widget, motion_handler); + // widget_set_button_handler(image->widget, button_handler); + // widget_set_axis_handler(image->widget, axis_handler); + window_set_key_handler(image->window, key_handler); + widget_schedule_resize(image->widget, 600, 400); + + return image; +} + +static void display_run_nonloop(struct display *display) +{ + struct task *task; + struct epoll_event ep[16]; + int i, count, ret; + int idx = 0; + + display->running = 1; + // while (1) + { + while (!wl_list_empty(&display->deferred_list)) + { + task = container_of(display->deferred_list.prev, + struct task, link); + wl_list_remove(&task->link); + task->run(task, 0); + } + + wl_display_dispatch_pending(display->display); + + ret = wl_display_flush(display->display); + if ((ret < 0) && (errno == EAGAIN)) + { + ep[0].events = + EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP; + ep[0].data.ptr = &display->display_task; + + epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD, + display->display_fd, &ep[0]); + } + else if (ret < 0) + { + // break; + return; + } + + count = epoll_wait(display->epoll_fd, + ep, ARRAY_LENGTH(ep), -1); + for (i = 0; i < count; i++) + { + task = ep[i].data.ptr; + task->run(task, ep[i].events); + } + } +} + +static int +screenshot_set_buffer_size(struct buffer_size *buff_size, struct wl_list *output_list) +{ + struct screenshooter_output *output; + buff_size->min_x = buff_size->min_y = INT_MAX; + buff_size->max_x = buff_size->max_y = INT_MIN; + int position = 0; + + wl_list_for_each_reverse(output, output_list, link) + { + output->offset_x = position; + position += output->width; + } + + wl_list_for_each(output, output_list, link) + { + buff_size->min_x = MIN(buff_size->min_x, output->offset_x); + buff_size->min_y = MIN(buff_size->min_y, output->offset_y); + buff_size->max_x = + MAX(buff_size->max_x, output->offset_x + output->width); + buff_size->max_y = + MAX(buff_size->max_y, output->offset_y + output->height); + } + + if ((buff_size->max_x <= buff_size->min_x) || + (buff_size->max_y <= buff_size->min_y)) + { + return -1; + } + + buff_size->width = buff_size->max_x - buff_size->min_x; + buff_size->height = buff_size->max_y - buff_size->min_y; + + return 0; +} + +char *get_weston_screen_image() +{ + struct wl_display *display_cap; // screenshooter variable + struct wl_registry *registry; + struct screenshooter_output *output; + struct buffer_size buff_size = {}; + struct screenshooter_data sh_data = {}; + char *cap_img = NULL; + + // start of screenshoot + display_cap = wl_display_connect(NULL); + if (display_cap == NULL) + { + fprintf(stderr, "failed to create display: %s\n", + strerror(errno)); + return -1; + } + + wl_list_init(&sh_data.output_list); + registry = wl_display_get_registry(display_cap); + wl_registry_add_listener(registry, ®istry_listener, &sh_data); + wl_display_dispatch(display_cap); + // printf("0.1 sh_data.shm[%p]\n",sh_data.shm); + wl_display_roundtrip(display_cap); + if (sh_data.screenshooter == NULL) + { + fprintf(stderr, "display doesn't support screenshooter\n"); + return -1; + } + weston_screenshooter_add_listener(sh_data.screenshooter, + &screenshooter_listener, + &sh_data); + if (screenshot_set_buffer_size(&buff_size, &sh_data.output_list)) + { + printf("screenshot_set_buffer_size fail\n"); + // continue; + return -1; + } + + int idx = 0; + wl_list_for_each(output, &sh_data.output_list, link) + { + if ((output->offset_x == 0) && + ((output->width == 1024) && (output->height == 768))) + { + // printf("output->offset_x[%d]\n",output->offset_x); + output->buffer = + screenshot_create_shm_buffer(output->width, + output->height, + &output->data, + sh_data.shm); + if (output->buffer == NULL) + { + continue; + } + + weston_screenshooter_shoot(sh_data.screenshooter, + output->output, + output->buffer); + + sh_data.buffer_copy_done = 0; + wl_display_roundtrip(display_cap); + usleep(35000); + + while (!sh_data.buffer_copy_done) + { + wl_display_roundtrip(display_cap); + usleep(1000); + } + } + } + // end of screenshoot + + if (buff_size.height == 768) + { + cap_img = get_screenshot(&buff_size, &sh_data.output_list); + } + else + { + struct screenshooter_output *output, *next; + wl_list_for_each_safe(output, next, &sh_data.output_list, link) + { + int ret = munmap(output->data, 1024*4*768); + // printf("munmap error[%d]\n",ret); + // printf("%s\n",strerror(errno)); + + wl_registry_destroy(output->output); + wl_list_remove(&output->link); + wl_buffer_destroy(output->buffer); + // free(output->buffer); + free(output); + } + } + + wl_registry_destroy(sh_data.shm); + wl_registry_destroy(sh_data.screenshooter); + + wl_registry_destroy(registry); + wl_display_disconnect(display_cap); + + return cap_img; +} + +int main(int argc, char *argv[]) +{ + struct display *d; // image.c variable + int i; + int image_counter = 0; + char *cap_img = NULL; + struct timespec start_time, end_time; + + d = display_create(&argc, argv); + cap_img = get_weston_screen_image(); + pimage = image_create(d, argv[i], &image_counter,cap_img); + + // full screen with 1x zoom scale + window_set_fullscreen(pimage->window, pimage->fullscreen = 1); + pimage->matrix.xx = 1.0; + pimage->matrix.xy = 0.0; + pimage->matrix.yx = 0.0; + pimage->matrix.yy = 1.0; + clamp_view(pimage); + window_schedule_redraw(pimage->window); + display_run_nonloop(d); + + struct sigaction sigint; + sigint.sa_handler = handler_sigint; + sigemptyset(&sigint.sa_mask); + sigint.sa_flags = SA_RESETHAND; + sigaction(SIGINT, &sigint, NULL); + + clock_gettime(CLOCK_MONOTONIC, &end_time); + memcpy(&start_time, &end_time, sizeof(struct timespec)); + + int loop = 0; + long long usec; + while (isloop) + { + // ui loop + display_run_nonloop(d); // 20 msec + + // reload image + free(cap_img); + cap_img = get_weston_screen_image(); // 80 msec + cairo_surface_destroy(pimage->image); // 70 usec + pimage->image = cairo_image_surface_create_for_data(cap_img, CAIRO_FORMAT_ARGB32,1024, 768, 1024*4); + + // ui redraw request + window_schedule_redraw(pimage->window); + + // fps calc. + clock_gettime(CLOCK_MONOTONIC, &end_time); + usec = (end_time.tv_sec - start_time.tv_sec) * 1000000 + (end_time.tv_nsec - start_time.tv_nsec) / 1000; + // printf("%d : %lld usec / %lld fps\n", loop++, usec, 1000000UL / usec); + memcpy(&start_time, &end_time, sizeof(struct timespec)); + } + + free(cap_img); + if (!is_pass_close_handler) + { + widget_destroy(pimage->widget); + window_destroy(pimage->window); + } + cairo_surface_destroy(pimage->image); + free(pimage); + + display_destroy(d); + + return 0; +} diff --git a/shared/cairo-util.c b/shared/cairo-util.c new file mode 100644 index 0000000..bfd418e --- /dev/null +++ b/shared/cairo-util.c @@ -0,0 +1,737 @@ +/* + * Copyright © 2008 Kristian Høgsberg + * Copyright © 2012 Intel Corporation + * + * 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 +#include +#include +#include +#include +#include +#include +#include "cairo-util.h" + +#include "shared/helpers.h" +#include "image-loader.h" +#include + +#ifdef HAVE_PANGO +#include +#endif + +void +surface_flush_device(cairo_surface_t *surface) +{ + cairo_device_t *device; + + device = cairo_surface_get_device(surface); + if (device) + { + cairo_device_flush(device); + } +} + +static int +blur_surface(cairo_surface_t *surface, int margin) +{ + int32_t width, height, stride, x, y, z, w; + uint8_t *src, *dst; + uint32_t *s, *d, a, p; + int i, j, k, size, half; + uint32_t kernel[71]; + double f; + + size = ARRAY_LENGTH(kernel); + width = cairo_image_surface_get_width(surface); + height = cairo_image_surface_get_height(surface); + stride = cairo_image_surface_get_stride(surface); + src = cairo_image_surface_get_data(surface); + + dst = malloc(height * stride); + if (dst == NULL) + { + return -1; + } + + half = size / 2; + a = 0; + for (i = 0; i < size; i++) + { + f = (i - half); + kernel[i] = exp(-f * f / ARRAY_LENGTH(kernel)) * 10000; + a += kernel[i]; + } + + for (i = 0; i < height; i++) + { + s = (uint32_t *) (src + i * stride); + d = (uint32_t *) (dst + i * stride); + for (j = 0; j < width; j++) + { + if ((margin < j) && (j < width - margin)) + { + d[j] = s[j]; + continue; + } + + x = 0; + y = 0; + z = 0; + w = 0; + for (k = 0; k < size; k++) + { + if ((j - half + k < 0) || (j - half + k >= width)) + { + continue; + } + p = s[j - half + k]; + + x += (p >> 24) * kernel[k]; + y += ((p >> 16) & 0xff) * kernel[k]; + z += ((p >> 8) & 0xff) * kernel[k]; + w += (p & 0xff) * kernel[k]; + } + d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a; + } + } + + for (i = 0; i < height; i++) + { + s = (uint32_t *) (dst + i * stride); + d = (uint32_t *) (src + i * stride); + for (j = 0; j < width; j++) + { + if ((margin <= i) && (i < height - margin)) + { + d[j] = s[j]; + continue; + } + + x = 0; + y = 0; + z = 0; + w = 0; + for (k = 0; k < size; k++) + { + if ((i - half + k < 0) || (i - half + k >= height)) + { + continue; + } + s = (uint32_t *) (dst + (i - half + k) * stride); + p = s[j]; + + x += (p >> 24) * kernel[k]; + y += ((p >> 16) & 0xff) * kernel[k]; + z += ((p >> 8) & 0xff) * kernel[k]; + w += (p & 0xff) * kernel[k]; + } + d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a; + } + } + + free(dst); + cairo_surface_mark_dirty(surface); + + return 0; +} + +void +render_shadow(cairo_t *cr, cairo_surface_t *surface, + int x, int y, int width, int height, int margin, int top_margin) +{ + cairo_pattern_t *pattern; + cairo_matrix_t matrix; + int i, fx, fy, shadow_height, shadow_width; + + cairo_set_source_rgba(cr, 0, 0, 0, 0.45); + cairo_set_operator(cr, CAIRO_OPERATOR_OVER); + pattern = cairo_pattern_create_for_surface(surface); + cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST); + + for (i = 0; i < 4; i++) + { + /* when fy is set, then we are working with lower corners, + * when fx is set, then we are working with right corners + * + * 00 ------- 01 + * | | + * | | + * 10 ------- 11 + */ + fx = i & 1; + fy = i >> 1; + + cairo_matrix_init_translate(&matrix, + -x + fx * (128 - width), + -y + fy * (128 - height)); + cairo_pattern_set_matrix(pattern, &matrix); + + shadow_width = margin; + shadow_height = fy ? margin : top_margin; + + /* if the shadows together are greater than the surface, we need + * to fix it - set the shadow size to the half of + * the size of surface. Also handle the case when the size is + * not divisible by 2. In that case we need one part of the + * shadow to be one pixel greater. !fy or !fx, respectively, + * will do the work. + */ + if (height < 2 * shadow_height) + { + shadow_height = (height + !fy) / 2; + } + + if (width < 2 * shadow_width) + { + shadow_width = (width + !fx) / 2; + } + + cairo_reset_clip(cr); + cairo_rectangle(cr, + x + fx * (width - shadow_width), + y + fy * (height - shadow_height), + shadow_width, shadow_height); + cairo_clip(cr); + cairo_mask(cr, pattern); + } + + + shadow_width = width - 2 * margin; + shadow_height = top_margin; + if (height < 2 * shadow_height) + { + shadow_height = height / 2; + } + + if ((shadow_width > 0) && shadow_height) + { + /* Top stretch */ + cairo_matrix_init_translate(&matrix, 60, 0); + cairo_matrix_scale(&matrix, 8.0 / width, 1); + cairo_matrix_translate(&matrix, -x - width / 2, -y); + cairo_pattern_set_matrix(pattern, &matrix); + cairo_rectangle(cr, x + margin, y, shadow_width, shadow_height); + + cairo_reset_clip(cr); + cairo_rectangle(cr, + x + margin, y, + shadow_width, shadow_height); + cairo_clip(cr); + cairo_mask(cr, pattern); + + /* Bottom stretch */ + cairo_matrix_translate(&matrix, 0, -height + 128); + cairo_pattern_set_matrix(pattern, &matrix); + + cairo_reset_clip(cr); + cairo_rectangle(cr, x + margin, y + height - margin, + shadow_width, margin); + cairo_clip(cr); + cairo_mask(cr, pattern); + } + + shadow_width = margin; + if (width < 2 * shadow_width) + { + shadow_width = width / 2; + } + + shadow_height = height - margin - top_margin; + + /* if height is smaller than sum of margins, + * then the shadow is already done by the corners */ + if ((shadow_height > 0) && shadow_width) + { + /* Left stretch */ + cairo_matrix_init_translate(&matrix, 0, 60); + cairo_matrix_scale(&matrix, 1, 8.0 / height); + cairo_matrix_translate(&matrix, -x, -y - height / 2); + cairo_pattern_set_matrix(pattern, &matrix); + cairo_reset_clip(cr); + cairo_rectangle(cr, x, y + top_margin, + shadow_width, shadow_height); + cairo_clip(cr); + cairo_mask(cr, pattern); + + /* Right stretch */ + cairo_matrix_translate(&matrix, -width + 128, 0); + cairo_pattern_set_matrix(pattern, &matrix); + cairo_rectangle(cr, x + width - shadow_width, y + top_margin, + shadow_width, shadow_height); + cairo_reset_clip(cr); + cairo_clip(cr); + cairo_mask(cr, pattern); + } + + cairo_pattern_destroy(pattern); + cairo_reset_clip(cr); +} + +void +tile_source(cairo_t *cr, cairo_surface_t *surface, + int x, int y, int width, int height, int margin, int top_margin) +{ + cairo_pattern_t *pattern; + cairo_matrix_t matrix; + int i, fx, fy, vmargin; + + cairo_set_operator(cr, CAIRO_OPERATOR_OVER); + pattern = cairo_pattern_create_for_surface(surface); + cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST); + cairo_set_source(cr, pattern); + cairo_pattern_destroy(pattern); + + for (i = 0; i < 4; i++) + { + fx = i & 1; + fy = i >> 1; + + cairo_matrix_init_translate(&matrix, + -x + fx * (128 - width), + -y + fy * (128 - height)); + cairo_pattern_set_matrix(pattern, &matrix); + + if (fy) + { + vmargin = margin; + } + else + { + vmargin = top_margin; + } + + cairo_rectangle(cr, + x + fx * (width - margin), + y + fy * (height - vmargin), + margin, vmargin); + cairo_fill(cr); + } + + /* Top stretch */ + cairo_matrix_init_translate(&matrix, 60, 0); + cairo_matrix_scale(&matrix, 8.0 / (width - 2 * margin), 1); + cairo_matrix_translate(&matrix, -x - width / 2, -y); + cairo_pattern_set_matrix(pattern, &matrix); + cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin); + cairo_fill(cr); + + /* Bottom stretch */ + cairo_matrix_translate(&matrix, 0, -height + 128); + cairo_pattern_set_matrix(pattern, &matrix); + cairo_rectangle(cr, x + margin, y + height - margin, + width - 2 * margin, margin); + cairo_fill(cr); + + /* Left stretch */ + cairo_matrix_init_translate(&matrix, 0, 60); + cairo_matrix_scale(&matrix, 1, 8.0 / (height - margin - top_margin)); + cairo_matrix_translate(&matrix, -x, -y - height / 2); + cairo_pattern_set_matrix(pattern, &matrix); + cairo_rectangle(cr, x, y + top_margin, + margin, height - margin - top_margin); + cairo_fill(cr); + + /* Right stretch */ + cairo_matrix_translate(&matrix, -width + 128, 0); + cairo_pattern_set_matrix(pattern, &matrix); + cairo_rectangle(cr, x + width - margin, y + top_margin, + margin, height - margin - top_margin); + cairo_fill(cr); +} + +void +rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) +{ + cairo_move_to(cr, x0, y0 + radius); + cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); + cairo_line_to(cr, x1 - radius, y0); + cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); + cairo_line_to(cr, x1, y1 - radius); + cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); + cairo_line_to(cr, x0 + radius, y1); + cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); + cairo_close_path(cr); +} + +cairo_surface_t * +load_cairo_surface(const char *filename) +{ + pixman_image_t *image; + int width, height, stride; + void *data; + + image = load_image(filename); + if (image == NULL) + { + return NULL; + } + + data = pixman_image_get_data(image); + width = pixman_image_get_width(image); + height = pixman_image_get_height(image); + stride = pixman_image_get_stride(image); + + return cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, + width, height, stride); +} + +void +theme_set_background_source(struct theme *t, cairo_t *cr, uint32_t flags) +{ + cairo_pattern_t *pattern; + + if (flags & THEME_FRAME_ACTIVE) + { + pattern = cairo_pattern_create_linear(16, 16, 16, 112); + cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0); + cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8); + cairo_set_source(cr, pattern); + cairo_pattern_destroy(pattern); + } + else + { + cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1); + } +} + +struct theme * +theme_create(void) +{ + struct theme *t; + cairo_t *cr; + + t = malloc(sizeof *t); + if (t == NULL) + { + return NULL; + } + + t->margin = 32; + t->width = 6; + t->titlebar_height = 27; + t->frame_radius = 3; + t->shadow = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 128, 128); + cr = cairo_create(t->shadow); + cairo_set_operator(cr, CAIRO_OPERATOR_OVER); + cairo_set_source_rgba(cr, 0, 0, 0, 1); + rounded_rect(cr, 32, 32, 96, 96, t->frame_radius); + cairo_fill(cr); + if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) + { + goto err_shadow; + } + cairo_destroy(cr); + if (blur_surface(t->shadow, 64) == -1) + { + goto err_shadow; + } + + t->active_frame = + cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 128, 128); + cr = cairo_create(t->active_frame); + cairo_set_operator(cr, CAIRO_OPERATOR_OVER); + + theme_set_background_source(t, cr, THEME_FRAME_ACTIVE); + rounded_rect(cr, 0, 0, 128, 128, t->frame_radius); + cairo_fill(cr); + + if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) + { + goto err_active_frame; + } + + cairo_destroy(cr); + + t->inactive_frame = + cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 128, 128); + cr = cairo_create(t->inactive_frame); + cairo_set_operator(cr, CAIRO_OPERATOR_OVER); + theme_set_background_source(t, cr, 0); + rounded_rect(cr, 0, 0, 128, 128, t->frame_radius); + cairo_fill(cr); + + if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) + { + goto err_inactive_frame; + } + + cairo_destroy(cr); + + return t; + +err_inactive_frame: + cairo_surface_destroy(t->inactive_frame); +err_active_frame: + cairo_surface_destroy(t->active_frame); +err_shadow: + cairo_surface_destroy(t->shadow); + free(t); + return NULL; +} + +void +theme_destroy(struct theme *t) +{ + cairo_surface_destroy(t->active_frame); + cairo_surface_destroy(t->inactive_frame); + cairo_surface_destroy(t->shadow); + free(t); +} + +#ifdef HAVE_PANGO +static PangoLayout * +create_layout(cairo_t *cr, const char *title) +{ + PangoLayout *layout; + PangoFontDescription *desc; + + layout = pango_cairo_create_layout(cr); + if (title) + { + pango_layout_set_text(layout, title, -1); + desc = pango_font_description_from_string("Sans Bold 10"); + pango_layout_set_font_description(layout, desc); + pango_font_description_free(desc); + } + pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); + pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT); + pango_layout_set_auto_dir(layout, FALSE); + pango_layout_set_single_paragraph_mode(layout, TRUE); + pango_layout_set_width(layout, -1); + + return layout; +} +#endif + +#ifdef HAVE_PANGO +#define SHOW_TEXT(cr) \ + pango_cairo_show_layout(cr, title_layout) +#else +#define SHOW_TEXT(cr) \ + cairo_show_text(cr, title) +#endif + +void +theme_render_frame(struct theme *t, + cairo_t *cr, int width, int height, + const char *title, cairo_rectangle_int_t *title_rect, + struct wl_list *buttons, uint32_t flags) +{ + cairo_surface_t *source; + int x, y, margin, top_margin; + int text_width, text_height; + + cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); + cairo_set_source_rgba(cr, 0, 0, 0, 0); + cairo_paint(cr); + + if (flags & THEME_FRAME_MAXIMIZED) + { + margin = 0; + } + else + { + render_shadow(cr, t->shadow, + 2, 2, width + 8, height + 8, + 64, 64); + margin = t->margin; + } + + if (flags & THEME_FRAME_ACTIVE) + { + source = t->active_frame; + } + else + { + source = t->inactive_frame; + } + + if (title || !wl_list_empty(buttons)) + { + top_margin = t->titlebar_height; + } + else + { + top_margin = t->width; + } + + tile_source(cr, source, + margin, margin, + width - margin * 2, height - margin * 2, + t->width, top_margin); + + if (title || !wl_list_empty(buttons)) + { + + cairo_rectangle(cr, title_rect->x, title_rect->y, + title_rect->width, title_rect->height); + cairo_clip(cr); + cairo_set_operator(cr, CAIRO_OPERATOR_OVER); + +#ifdef HAVE_PANGO + PangoLayout *title_layout; + PangoRectangle logical; + + title_layout = create_layout(cr, title); + + pango_layout_get_pixel_extents(title_layout, NULL, &logical); + text_width = MIN(title_rect->width, logical.width); + text_height = logical.height; + if (text_width < logical.width) + { + pango_layout_set_width(title_layout, text_width * PANGO_SCALE); + } + +#else + cairo_text_extents_t extents; + cairo_font_extents_t font_extents; + + cairo_select_font_face(cr, "sans", + CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_BOLD); + cairo_set_font_size(cr, 14); + cairo_text_extents(cr, title, &extents); + cairo_font_extents(cr, &font_extents); + text_width = extents.width; + text_height = font_extents.descent - font_extents.ascent; +#endif + + x = (width - text_width) / 2; + y = margin + (t->titlebar_height - text_height) / 2; + if (x < title_rect->x) + { + x = title_rect->x; + } + else if (x + text_width > (title_rect->x + title_rect->width)) + { + x = (title_rect->x + title_rect->width) - text_width; + } + + if (flags & THEME_FRAME_ACTIVE) + { + cairo_move_to(cr, x + 1, y + 1); + cairo_set_source_rgb(cr, 1, 1, 1); + SHOW_TEXT(cr); + cairo_move_to(cr, x, y); + cairo_set_source_rgb(cr, 0, 0, 0); + SHOW_TEXT(cr); + } + else + { + cairo_move_to(cr, x, y); + cairo_set_source_rgb(cr, 0.4, 0.4, 0.4); + SHOW_TEXT(cr); + } + } +} + +enum theme_location +theme_get_location(struct theme *t, int x, int y, + int width, int height, int flags) +{ + int vlocation, hlocation, location; + int margin, top_margin, grip_size; + + if (flags & THEME_FRAME_MAXIMIZED) + { + margin = 0; + grip_size = 0; + } + else + { + margin = t->margin; + grip_size = 8; + } + + if (flags & THEME_FRAME_NO_TITLE) + { + top_margin = t->width; + } + else + { + top_margin = t->titlebar_height; + } + + if (x < margin) + { + hlocation = THEME_LOCATION_EXTERIOR; + } + else if (x < margin + grip_size) + { + hlocation = THEME_LOCATION_RESIZING_LEFT; + } + else if (x < width - margin - grip_size) + { + hlocation = THEME_LOCATION_INTERIOR; + } + else if (x < width - margin) + { + hlocation = THEME_LOCATION_RESIZING_RIGHT; + } + else + { + hlocation = THEME_LOCATION_EXTERIOR; + } + + if (y < margin) + { + vlocation = THEME_LOCATION_EXTERIOR; + } + else if (y < margin + grip_size) + { + vlocation = THEME_LOCATION_RESIZING_TOP; + } + else if (y < height - margin - grip_size) + { + vlocation = THEME_LOCATION_INTERIOR; + } + else if (y < height - margin) + { + vlocation = THEME_LOCATION_RESIZING_BOTTOM; + } + else + { + vlocation = THEME_LOCATION_EXTERIOR; + } + + location = vlocation | hlocation; + if (location & THEME_LOCATION_EXTERIOR) + { + location = THEME_LOCATION_EXTERIOR; + } + if ((location == THEME_LOCATION_INTERIOR) && + (y < margin + top_margin)) + { + location = THEME_LOCATION_TITLEBAR; + } + else if (location == THEME_LOCATION_INTERIOR) + { + location = THEME_LOCATION_CLIENT_AREA; + } + + return location; +} diff --git a/shared/cairo-util.h b/shared/cairo-util.h new file mode 100644 index 0000000..11d2568 --- /dev/null +++ b/shared/cairo-util.h @@ -0,0 +1,239 @@ +/* + * Copyright © 2008 Kristian Høgsberg + * + * 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. + */ + +#ifndef _CAIRO_UTIL_H +#define _CAIRO_UTIL_H + +#include +#include + +#include +#include + +void +surface_flush_device(cairo_surface_t *surface); + +void +render_shadow(cairo_t *cr, cairo_surface_t *surface, + int x, int y, int width, int height, int margin, int top_margin); + +void +tile_source(cairo_t *cr, cairo_surface_t *surface, + int x, int y, int width, int height, int margin, int top_margin); + +void +rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius); + +cairo_surface_t * +load_cairo_surface(const char *filename); + +struct theme +{ + cairo_surface_t *active_frame; + cairo_surface_t *inactive_frame; + cairo_surface_t *shadow; + int frame_radius; + int margin; + int width; + int titlebar_height; +}; + +struct theme * +theme_create(void); +void +theme_destroy(struct theme *t); + +enum +{ + THEME_FRAME_ACTIVE = 1, + THEME_FRAME_MAXIMIZED = 2, + THEME_FRAME_NO_TITLE = 4 +}; + +void +theme_set_background_source(struct theme *t, cairo_t *cr, uint32_t flags); +void +theme_render_frame(struct theme *t, + cairo_t *cr, int width, int height, + const char *title, cairo_rectangle_int_t *title_rect, + struct wl_list *buttons, uint32_t flags); + +enum theme_location +{ + THEME_LOCATION_INTERIOR = 0, + THEME_LOCATION_RESIZING_TOP = 1, + THEME_LOCATION_RESIZING_BOTTOM = 2, + THEME_LOCATION_RESIZING_LEFT = 4, + THEME_LOCATION_RESIZING_TOP_LEFT = 5, + THEME_LOCATION_RESIZING_BOTTOM_LEFT = 6, + THEME_LOCATION_RESIZING_RIGHT = 8, + THEME_LOCATION_RESIZING_TOP_RIGHT = 9, + THEME_LOCATION_RESIZING_BOTTOM_RIGHT = 10, + THEME_LOCATION_RESIZING_MASK = 15, + THEME_LOCATION_EXTERIOR = 16, + THEME_LOCATION_TITLEBAR = 17, + THEME_LOCATION_CLIENT_AREA = 18, +}; + +enum theme_location +theme_get_location(struct theme *t, int x, int y, int width, int height, int flags); + +struct frame; + +enum frame_status +{ + FRAME_STATUS_NONE = 0, + FRAME_STATUS_REPAINT = 0x1, + FRAME_STATUS_MINIMIZE = 0x2, + FRAME_STATUS_MAXIMIZE = 0x4, + FRAME_STATUS_CLOSE = 0x8, + FRAME_STATUS_MENU = 0x10, + FRAME_STATUS_RESIZE = 0x20, + FRAME_STATUS_MOVE = 0x40, + FRAME_STATUS_ALL = 0x7f +}; + +enum frame_flag +{ + FRAME_FLAG_ACTIVE = 0x1, + FRAME_FLAG_MAXIMIZED = 0x2 +}; + +enum +{ + FRAME_BUTTON_NONE = 0, + FRAME_BUTTON_CLOSE = 0x1, + FRAME_BUTTON_MAXIMIZE = 0x2, + FRAME_BUTTON_MINIMIZE = 0x4, + FRAME_BUTTON_ALL = 0x7 +}; + +struct frame * +frame_create(struct theme *t, int32_t width, int32_t height, uint32_t buttons, + const char *title, cairo_surface_t *icon); + +void +frame_destroy(struct frame *frame); + +/* May set FRAME_STATUS_REPAINT */ +int +frame_set_title(struct frame *frame, const char *title); + +/* May set FRAME_STATUS_REPAINT */ +void +frame_set_icon(struct frame *frame, cairo_surface_t *icon); + +/* May set FRAME_STATUS_REPAINT */ +void +frame_set_flag(struct frame *frame, enum frame_flag flag); + +/* May set FRAME_STATUS_REPAINT */ +void +frame_unset_flag(struct frame *frame, enum frame_flag flag); + +/* May set FRAME_STATUS_REPAINT */ +void +frame_resize(struct frame *frame, int32_t width, int32_t height); + +/* May set FRAME_STATUS_REPAINT */ +void +frame_resize_inside(struct frame *frame, int32_t width, int32_t height); + +int32_t +frame_width(struct frame *frame); + +int32_t +frame_height(struct frame *frame); + +void +frame_interior(struct frame *frame, int32_t *x, int32_t *y, + int32_t *width, int32_t *height); +void +frame_input_rect(struct frame *frame, int32_t *x, int32_t *y, + int32_t *width, int32_t *height); +void +frame_opaque_rect(struct frame *frame, int32_t *x, int32_t *y, + int32_t *width, int32_t *height); + +int +frame_get_shadow_margin(struct frame *frame); + +uint32_t +frame_status(struct frame *frame); + +void +frame_status_clear(struct frame *frame, enum frame_status status); + +/* May set FRAME_STATUS_REPAINT */ +enum theme_location +frame_pointer_enter(struct frame *frame, void *pointer, int x, int y); + +/* May set FRAME_STATUS_REPAINT */ +enum theme_location +frame_pointer_motion(struct frame *frame, void *pointer, int x, int y); + +/* May set FRAME_STATUS_REPAINT */ +void +frame_pointer_leave(struct frame *frame, void *pointer); + +/* Call to indicate that a button has been pressed/released. The return + * value for a button release will be the same as for the corresponding + * press. This allows you to more easily track grabs. If you want the + * actual location, simply keep the location from the last + * frame_pointer_motion call. + * + * May set: + * FRAME_STATUS_MINIMIZE + * FRAME_STATUS_MAXIMIZE + * FRAME_STATUS_CLOSE + * FRAME_STATUS_MENU + * FRAME_STATUS_RESIZE + * FRAME_STATUS_MOVE + */ +enum theme_location +frame_pointer_button(struct frame *frame, void *pointer, + uint32_t button, enum wl_pointer_button_state state); + +enum theme_location +frame_touch_down(struct frame *frame, void *data, int32_t id, int x, int y); + +void +frame_touch_up(struct frame *frame, void *data, int32_t id); + +enum theme_location +frame_double_click(struct frame *frame, void *pointer, + uint32_t button, enum wl_pointer_button_state state); + +void +frame_double_touch_down(struct frame *frame, void *data, int32_t id, + int x, int y); + +void +frame_double_touch_up(struct frame *frame, void *data, int32_t id); + +void +frame_repaint(struct frame *frame, cairo_t *cr); + +#endif diff --git a/shared/config-parser.c b/shared/config-parser.c new file mode 100644 index 0000000..0dfbda0 --- /dev/null +++ b/shared/config-parser.c @@ -0,0 +1,629 @@ +/* + * Copyright © 2011 Intel Corporation + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include "helpers.h" +#include "string-helpers.h" + +struct weston_config_entry +{ + char *key; + char *value; + struct wl_list link; +}; + +struct weston_config_section +{ + char *name; + struct wl_list entry_list; + struct wl_list link; +}; + +struct weston_config +{ + struct wl_list section_list; + char path[PATH_MAX]; +}; + +static int +open_config_file(struct weston_config *c, const char *name) +{ + const char *config_dir = getenv("XDG_CONFIG_HOME"); + const char *home_dir = getenv("HOME"); + const char *config_dirs = getenv("XDG_CONFIG_DIRS"); + const char *p, *next; + int fd; + + if (name[0] == '/') + { + snprintf(c->path, sizeof c->path, "%s", name); + return open(name, O_RDONLY | O_CLOEXEC); + } + + /* Precedence is given to config files in the home directory, + * then to directories listed in XDG_CONFIG_DIRS. */ + + /* $XDG_CONFIG_HOME */ + if (config_dir) + { + snprintf(c->path, sizeof c->path, "%s/%s", config_dir, name); + fd = open(c->path, O_RDONLY | O_CLOEXEC); + if (fd >= 0) + { + return fd; + } + } + + /* $HOME/.config */ + if (home_dir) + { + snprintf(c->path, sizeof c->path, + "%s/.config/%s", home_dir, name); + fd = open(c->path, O_RDONLY | O_CLOEXEC); + if (fd >= 0) + { + return fd; + } + } + + /* For each $XDG_CONFIG_DIRS: weston/ */ + if (!config_dirs) + { + config_dirs = "/etc/xdg"; /* See XDG base dir spec. */ + + } + for (p = config_dirs; *p != '\0'; p = next) + { + next = strchrnul(p, ':'); + snprintf(c->path, sizeof c->path, + "%.*s/weston/%s", (int) (next - p), p, name); + fd = open(c->path, O_RDONLY | O_CLOEXEC); + if (fd >= 0) + { + return fd; + } + + if (*next == ':') + { + next++; + } + } + + return -1; +} + +static struct weston_config_entry * +config_section_get_entry(struct weston_config_section *section, + const char *key) +{ + struct weston_config_entry *e; + + if (section == NULL) + { + return NULL; + } + wl_list_for_each(e, §ion->entry_list, link) + if (strcmp(e->key, key) == 0) + { + return e; + } + + return NULL; +} + +WL_EXPORT +struct weston_config_section * +weston_config_get_section(struct weston_config *config, const char *section, + const char *key, const char *value) +{ + struct weston_config_section *s; + struct weston_config_entry *e; + + if (config == NULL) + { + return NULL; + } + wl_list_for_each(s, &config->section_list, link) + { + if (strcmp(s->name, section) != 0) + { + continue; + } + if (key == NULL) + { + return s; + } + e = config_section_get_entry(s, key); + if (e && (strcmp(e->value, value) == 0)) + { + return s; + } + } + + return NULL; +} + +WL_EXPORT +int +weston_config_section_get_int(struct weston_config_section *section, + const char *key, + int32_t *value, int32_t default_value) +{ + struct weston_config_entry *entry; + + entry = config_section_get_entry(section, key); + if (entry == NULL) + { + *value = default_value; + errno = ENOENT; + return -1; + } + + if (!safe_strtoint(entry->value, value)) + { + *value = default_value; + return -1; + } + + return 0; +} + +WL_EXPORT +int +weston_config_section_get_uint(struct weston_config_section *section, + const char *key, + uint32_t *value, uint32_t default_value) +{ + long int ret; + struct weston_config_entry *entry; + char *end; + + entry = config_section_get_entry(section, key); + if (entry == NULL) + { + *value = default_value; + errno = ENOENT; + return -1; + } + + errno = 0; + ret = strtol(entry->value, &end, 0); + if ((errno != 0) || (end == entry->value) || (*end != '\0')) + { + *value = default_value; + errno = EINVAL; + return -1; + } + + /* check range */ + if ((ret < 0) || (ret > INT_MAX)) + { + *value = default_value; + errno = ERANGE; + return -1; + } + + *value = ret; + + return 0; +} + +WL_EXPORT +int +weston_config_section_get_color(struct weston_config_section *section, + const char *key, + uint32_t *color, uint32_t default_color) +{ + struct weston_config_entry *entry; + int len; + char *end; + + entry = config_section_get_entry(section, key); + if (entry == NULL) + { + *color = default_color; + errno = ENOENT; + return -1; + } + + len = strlen(entry->value); + if ((len == 1) && (entry->value[0] == '0')) + { + *color = 0; + return 0; + } + else if ((len != 8) && (len != 10)) + { + *color = default_color; + errno = EINVAL; + return -1; + } + + errno = 0; + *color = strtoul(entry->value, &end, 16); + if ((errno != 0) || (end == entry->value) || (*end != '\0')) + { + *color = default_color; + errno = EINVAL; + return -1; + } + + return 0; +} + +WL_EXPORT +int +weston_config_section_get_double(struct weston_config_section *section, + const char *key, + double *value, double default_value) +{ + struct weston_config_entry *entry; + char *end; + + entry = config_section_get_entry(section, key); + if (entry == NULL) + { + *value = default_value; + errno = ENOENT; + return -1; + } + + *value = strtod(entry->value, &end); + if (*end != '\0') + { + *value = default_value; + errno = EINVAL; + return -1; + } + + return 0; +} + +WL_EXPORT +int +weston_config_section_get_string(struct weston_config_section *section, + const char *key, + char **value, const char *default_value) +{ + struct weston_config_entry *entry; + + entry = config_section_get_entry(section, key); + if (entry == NULL) + { + if (default_value) + { + *value = strdup(default_value); + } + else + { + *value = NULL; + } + errno = ENOENT; + return -1; + } + + *value = strdup(entry->value); + + return 0; +} + +WL_EXPORT +int +weston_config_section_get_bool(struct weston_config_section *section, + const char *key, + bool *value, bool default_value) +{ + struct weston_config_entry *entry; + + entry = config_section_get_entry(section, key); + if (entry == NULL) + { + *value = default_value; + errno = ENOENT; + return -1; + } + + if (strcmp(entry->value, "false") == 0) + { + *value = false; + } + else if (strcmp(entry->value, "true") == 0) + { + *value = true; + } + else + { + *value = default_value; + errno = EINVAL; + return -1; + } + + return 0; +} + +WL_EXPORT +const char * +weston_config_get_name_from_env(void) +{ + const char *name; + + name = getenv(WESTON_CONFIG_FILE_ENV_VAR); + if (name) + { + return name; + } + + return "weston.ini"; +} + +WL_EXPORT +void +weston_config_set_env(struct weston_config_section *section) +{ + struct weston_config_entry *e; + + if (section == NULL) + { + return; + } + wl_list_for_each(e, §ion->entry_list, link) + { + setenv(e->key, e->value, 1); + } +} + +static struct weston_config_section * +config_add_section(struct weston_config *config, const char *name) +{ + struct weston_config_section *section; + + section = malloc(sizeof *section); + if (section == NULL) + { + return NULL; + } + + section->name = strdup(name); + if (section->name == NULL) + { + free(section); + return NULL; + } + + wl_list_init(§ion->entry_list); + wl_list_insert(config->section_list.prev, §ion->link); + + return section; +} + +static struct weston_config_entry * +section_add_entry(struct weston_config_section *section, + const char *key, const char *value) +{ + struct weston_config_entry *entry; + + entry = malloc(sizeof *entry); + if (entry == NULL) + { + return NULL; + } + + entry->key = strdup(key); + if (entry->key == NULL) + { + free(entry); + return NULL; + } + + entry->value = strdup(value); + if (entry->value == NULL) + { + free(entry->key); + free(entry); + return NULL; + } + + wl_list_insert(section->entry_list.prev, &entry->link); + + return entry; +} + +WL_EXPORT +struct weston_config * +weston_config_parse(const char *name) +{ + FILE *fp; + char line[512], *p; + struct stat filestat; + struct weston_config *config; + struct weston_config_section *section = NULL; + int i, fd; + + config = malloc(sizeof *config); + if (config == NULL) + { + return NULL; + } + + wl_list_init(&config->section_list); + + fd = open_config_file(config, name); + if (fd == -1) + { + free(config); + return NULL; + } + + if ((fstat(fd, &filestat) < 0) || + !S_ISREG(filestat.st_mode)) + { + close(fd); + free(config); + return NULL; + } + + fp = fdopen(fd, "r"); + if (fp == NULL) + { + free(config); + return NULL; + } + + while (fgets(line, sizeof line, fp)) + { + switch (line[0]) + { + case '#': + case '\n': + continue; + + case '[': + p = strchr(&line[1], ']'); + if (!p || (p[1] != '\n')) + { + fprintf(stderr, "malformed " + "section header: %s\n", line); + fclose(fp); + weston_config_destroy(config); + return NULL; + } + p[0] = '\0'; + section = config_add_section(config, &line[1]); + continue; + + default: + p = strchr(line, '='); + if (!p || (p == line) || !section) + { + fprintf(stderr, "malformed " + "config line: %s\n", line); + fclose(fp); + weston_config_destroy(config); + return NULL; + } + + p[0] = '\0'; + p++; + while (isspace(*p)) + { + p++; + } + i = strlen(p); + while (i > 0 && isspace(p[i - 1])) + { + p[i - 1] = '\0'; + i--; + } + section_add_entry(section, line, p); + continue; + } + } + + fclose(fp); + + return config; +} + +WL_EXPORT +const char * +weston_config_get_full_path(struct weston_config *config) +{ + return config == NULL ? NULL : config->path; +} + +WL_EXPORT +int +weston_config_next_section(struct weston_config *config, + struct weston_config_section **section, + const char **name) +{ + if (config == NULL) + { + return 0; + } + + if (*section == NULL) + { + *section = container_of(config->section_list.next, + struct weston_config_section, link); + } + else + { + *section = container_of((*section)->link.next, + struct weston_config_section, link); + } + + if (&(*section)->link == &config->section_list) + { + return 0; + } + + *name = (*section)->name; + + return 1; +} + +WL_EXPORT +void +weston_config_destroy(struct weston_config *config) +{ + struct weston_config_section *s, *next_s; + struct weston_config_entry *e, *next_e; + + if (config == NULL) + { + return; + } + + wl_list_for_each_safe(s, next_s, &config->section_list, link) + { + wl_list_for_each_safe(e, next_e, &s->entry_list, link) + { + free(e->key); + free(e->value); + free(e); + } + free(s->name); + free(s); + } + + free(config); +} diff --git a/shared/fd-util.h b/shared/fd-util.h new file mode 100644 index 0000000..5b85b63 --- /dev/null +++ b/shared/fd-util.h @@ -0,0 +1,62 @@ +/* + * Copyright © 2018 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. + */ + +#ifndef FD_UTIL_H +#define FD_UTIL_H + +#include + +static inline void +fd_update(int *fd, int new_fd) +{ + if (*fd == new_fd) + { + return; + } + if (*fd >= 0) + { + close(*fd); + } + *fd = new_fd; +} + +static inline void +fd_move(int *dest, int *src) +{ + if (dest == src) + { + return; + } + fd_update(dest, *src); + *src = -1; +} + +static inline void +fd_clear(int *fd) +{ + fd_update(fd, -1); +} + +#endif /* FD_UTIL_H */ diff --git a/shared/file-util.c b/shared/file-util.c new file mode 100644 index 0000000..6e9369d --- /dev/null +++ b/shared/file-util.c @@ -0,0 +1,161 @@ +/* + * Copyright © 2015 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 +#include +#include +#include +#include +#include +#include + +#include "file-util.h" + +static int +current_time_str(char *str, size_t len, const char *fmt) +{ + time_t t; + struct tm *t_local; + int ret; + + t = time(NULL); + t_local = localtime(&t); + if (!t_local) + { + errno = ETIME; + return -1; + } + + ret = strftime(str, len, fmt, t_local); + if (ret == 0) + { + errno = ETIME; + return -1; + } + + return ret; +} + +static int +create_file_excl(const char *fname) +{ + return open(fname, O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 00666); +} + +/** Create a unique file with date and time in the name + * + * \param path File path + * \param prefix File name prefix. + * \param suffix File name suffix. + * \param[out] name_out Buffer for the resulting file name. + * \param name_len Number of bytes usable in name_out. + * \return stdio FILE pointer, or NULL on failure. + * + * Create and open a new file with the name concatenated from + * path/prefix, date and time, and suffix. If a file with this name + * already exists, an counter number is added to the end of the + * date and time sub-string. The counter is increased until a free file + * name is found. + * + * Once creating the file succeeds, the name of the file is in name_out. + * On failure, the contents of name_out are undefined and errno is set. + */ +FILE * +file_create_dated(const char *path, const char *prefix, const char *suffix, + char *name_out, size_t name_len) +{ + char timestr[128]; + int ret; + int fd; + int cnt = 0; + int with_path; + + with_path = path && path[0]; + + if (current_time_str(timestr, sizeof(timestr), "%F_%H-%M-%S") < 0) + { + return NULL; + } + + ret = snprintf(name_out, name_len, "%s%s%s%s%s", + with_path ? path : "", with_path ? "/" : "", + prefix, timestr, suffix); + if ((ret < 0) || ((size_t) ret >= name_len)) + { + errno = ENOBUFS; + return NULL; + } + + fd = create_file_excl(name_out); + + while (fd == -1 && errno == EEXIST) + { + cnt++; + + ret = snprintf(name_out, name_len, "%s%s%s%s-%d%s", + with_path ? path : "", with_path ? "/" : "", + prefix, timestr, cnt, suffix); + if ((ret < 0) || ((size_t) ret >= name_len)) + { + errno = ENOBUFS; + return NULL; + } + + fd = create_file_excl(name_out); + } + + if (fd == -1) + { + return NULL; + } + + return fdopen(fd, "w"); +} + +char * +file_name_with_datadir(const char *filename) +{ + const char *base = getenv("WESTON_DATA_DIR"); + char *out; + int len; + + if (base) + { + len = asprintf(&out, "%s/%s", base, filename); + } + else + { + len = asprintf(&out, "%s/weston/%s", DATADIR, filename); + } + + if (len == -1) + { + return NULL; + } + + return out; +} diff --git a/shared/file-util.h b/shared/file-util.h new file mode 100644 index 0000000..18eda95 --- /dev/null +++ b/shared/file-util.h @@ -0,0 +1,46 @@ +/* + * Copyright © 2015 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. + */ + +#ifndef WESTON_FILE_UTIL_H +#define WESTON_FILE_UTIL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +FILE * +file_create_dated(const char *path, const char *prefix, const char *suffix, + char *name_out, size_t name_len); + +char * +file_name_with_datadir(const char *); + +#ifdef __cplusplus +} +#endif + +#endif /* WESTON_FILE_UTIL_H */ diff --git a/shared/frame.c b/shared/frame.c new file mode 100644 index 0000000..ffa2025 --- /dev/null +++ b/shared/frame.c @@ -0,0 +1,1227 @@ +/* + * Copyright © 2008 Kristian Høgsberg + * Copyright © 2012-2013 Collabora, Ltd. + * Copyright © 2013 Jason Ekstrand + * + * 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 +#include +#include +#include +#include + +#include "cairo-util.h" +#include "shared/file-util.h" + +enum frame_button_flags +{ + FRAME_BUTTON_ALIGN_RIGHT = 0x1, + FRAME_BUTTON_DECORATED = 0x2, + FRAME_BUTTON_CLICK_DOWN = 0x4, +}; + +struct frame_button +{ + struct frame *frame; + struct wl_list link; /* buttons_list */ + + cairo_surface_t *icon; + enum frame_button_flags flags; + int hover_count; + int press_count; + + struct + { + int x, y; + int width, height; + } allocation; + + enum frame_status status_effect; +}; + +struct frame_pointer_button +{ + struct wl_list link; + uint32_t button; + enum theme_location press_location; + struct frame_button *frame_button; +}; + +struct frame_pointer +{ + struct wl_list link; + void *data; + + int x, y; + + struct frame_button *hover_button; + struct wl_list down_buttons; +}; + +struct frame_touch +{ + struct wl_list link; + void *data; + + int x, y; + + struct frame_button *button; +}; + +struct frame +{ + int32_t width, height; + char *title; + uint32_t flags; + struct theme *theme; + + struct + { + int32_t x, y; + int32_t width, height; + } interior; + int shadow_margin; + int opaque_margin; + int geometry_dirty; + + cairo_rectangle_int_t title_rect; + + uint32_t status; + + struct wl_list buttons; + struct wl_list pointers; + struct wl_list touches; +}; + +static struct frame_button * +frame_button_create_from_surface(struct frame *frame, cairo_surface_t *icon, + enum frame_status status_effect, + enum frame_button_flags flags) +{ + struct frame_button *button; + + button = calloc(1, sizeof *button); + if (!button) + { + return NULL; + } + + button->icon = icon; + button->frame = frame; + button->flags = flags; + button->status_effect = status_effect; + + wl_list_insert(frame->buttons.prev, &button->link); + + return button; +} + +static struct frame_button * +frame_button_create(struct frame *frame, const char *icon_name, + enum frame_status status_effect, + enum frame_button_flags flags) +{ + struct frame_button *button; + cairo_surface_t *icon; + + icon = cairo_image_surface_create_from_png(icon_name); + if (cairo_surface_status(icon) != CAIRO_STATUS_SUCCESS) + { + goto error; + } + + button = frame_button_create_from_surface(frame, icon, status_effect, + flags); + if (!button) + { + goto error; + } + + return button; + +error: + cairo_surface_destroy(icon); + return NULL; +} + +static void +frame_button_destroy(struct frame_button *button) +{ + cairo_surface_destroy(button->icon); + free(button); +} + +static void +frame_button_enter(struct frame_button *button) +{ + if (!button->hover_count) + { + button->frame->status |= FRAME_STATUS_REPAINT; + } + button->hover_count++; +} + +static void +frame_button_leave(struct frame_button *button, struct frame_pointer *pointer) +{ + button->hover_count--; + if (!button->hover_count) + { + button->frame->status |= FRAME_STATUS_REPAINT; + } +} + +static void +frame_button_press(struct frame_button *button) +{ + if (!button->press_count) + { + button->frame->status |= FRAME_STATUS_REPAINT; + } + button->press_count++; + + if (button->flags & FRAME_BUTTON_CLICK_DOWN) + { + button->frame->status |= button->status_effect; + } +} + +static void +frame_button_release(struct frame_button *button) +{ + button->press_count--; + if (button->press_count) + { + return; + } + + button->frame->status |= FRAME_STATUS_REPAINT; + + if (!(button->flags & FRAME_BUTTON_CLICK_DOWN)) + { + button->frame->status |= button->status_effect; + } +} + +static void +frame_button_cancel(struct frame_button *button) +{ + button->press_count--; + if (!button->press_count) + { + button->frame->status |= FRAME_STATUS_REPAINT; + } +} + +static void +frame_button_repaint(struct frame_button *button, cairo_t *cr) +{ + int x, y; + + if (!button->allocation.width) + { + return; + } + if (!button->allocation.height) + { + return; + } + + x = button->allocation.x; + y = button->allocation.y; + + cairo_save(cr); + + if (button->flags & FRAME_BUTTON_DECORATED) + { + cairo_set_line_width(cr, 1); + + cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); + cairo_rectangle(cr, x, y, 25, 16); + + cairo_stroke_preserve(cr); + + if (button->press_count) + { + cairo_set_source_rgb(cr, 0.7, 0.7, 0.7); + } + else if (button->hover_count) + { + cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); + } + else + { + cairo_set_source_rgb(cr, 0.88, 0.88, 0.88); + } + + cairo_fill(cr); + + x += 4; + } + + cairo_set_source_surface(cr, button->icon, x, y); + cairo_paint(cr); + + cairo_restore(cr); +} + +static struct frame_pointer * +frame_pointer_get(struct frame *frame, void *data) +{ + struct frame_pointer *pointer; + + wl_list_for_each(pointer, &frame->pointers, link) + if (pointer->data == data) + { + return pointer; + } + + pointer = calloc(1, sizeof *pointer); + if (!pointer) + { + return NULL; + } + + pointer->data = data; + wl_list_init(&pointer->down_buttons); + wl_list_insert(&frame->pointers, &pointer->link); + + return pointer; +} + +static void +frame_pointer_destroy(struct frame_pointer *pointer) +{ + wl_list_remove(&pointer->link); + free(pointer); +} + +static struct frame_touch * +frame_touch_get(struct frame *frame, void *data) +{ + struct frame_touch *touch; + + wl_list_for_each(touch, &frame->touches, link) + if (touch->data == data) + { + return touch; + } + + touch = calloc(1, sizeof *touch); + if (!touch) + { + return NULL; + } + + touch->data = data; + wl_list_insert(&frame->touches, &touch->link); + + return touch; +} + +static void +frame_touch_destroy(struct frame_touch *touch) +{ + wl_list_remove(&touch->link); + free(touch); +} + +void +frame_destroy(struct frame *frame) +{ + struct frame_button *button, *next; + struct frame_touch *touch, *next_touch; + struct frame_pointer *pointer, *next_pointer; + + wl_list_for_each_safe(button, next, &frame->buttons, link) + frame_button_destroy(button); + + wl_list_for_each_safe(touch, next_touch, &frame->touches, link) + frame_touch_destroy(touch); + + wl_list_for_each_safe(pointer, next_pointer, &frame->pointers, link) + frame_pointer_destroy(pointer); + + free(frame->title); + free(frame); +} + +struct frame * +frame_create(struct theme *t, int32_t width, int32_t height, uint32_t buttons, + const char *title, cairo_surface_t *icon) +{ + struct frame *frame; + struct frame_button *button; + + frame = calloc(1, sizeof *frame); + if (!frame) + { + return NULL; + } + + frame->width = width; + frame->height = height; + frame->flags = 0; + frame->theme = t; + frame->status = FRAME_STATUS_REPAINT; + frame->geometry_dirty = 1; + + wl_list_init(&frame->buttons); + wl_list_init(&frame->pointers); + wl_list_init(&frame->touches); + + if (title) + { + frame->title = strdup(title); + if (!frame->title) + { + goto free_frame; + } + } + + if (title) + { + if (icon) + { + button = frame_button_create_from_surface(frame, + icon, + FRAME_STATUS_MENU, + FRAME_BUTTON_CLICK_DOWN); + } + else + { + char *name = file_name_with_datadir("icon_window.png"); + + if (!name) + { + goto free_frame; + } + + button = frame_button_create(frame, + name, + FRAME_STATUS_MENU, + FRAME_BUTTON_CLICK_DOWN); + free(name); + } + if (!button) + { + goto free_frame; + } + } + + if (buttons & FRAME_BUTTON_CLOSE) + { + char *name = file_name_with_datadir("sign_close.png"); + + if (!name) + { + goto free_frame; + } + + button = frame_button_create(frame, + name, + FRAME_STATUS_CLOSE, + FRAME_BUTTON_ALIGN_RIGHT | + FRAME_BUTTON_DECORATED); + free(name); + if (!button) + { + goto free_frame; + } + } + + if (buttons & FRAME_BUTTON_MAXIMIZE) + { + char *name = file_name_with_datadir("sign_maximize.png"); + + if (!name) + { + goto free_frame; + } + + button = frame_button_create(frame, + name, + FRAME_STATUS_MAXIMIZE, + FRAME_BUTTON_ALIGN_RIGHT | + FRAME_BUTTON_DECORATED); + free(name); + if (!button) + { + goto free_frame; + } + } + + if (buttons & FRAME_BUTTON_MINIMIZE) + { + char *name = file_name_with_datadir("sign_minimize.png"); + + if (!name) + { + goto free_frame; + } + + button = frame_button_create(frame, + name, + FRAME_STATUS_MINIMIZE, + FRAME_BUTTON_ALIGN_RIGHT | + FRAME_BUTTON_DECORATED); + free(name); + if (!button) + { + goto free_frame; + } + } + + return frame; + +free_frame: + frame_destroy(frame); + return NULL; +} + +int +frame_set_title(struct frame *frame, const char *title) +{ + char *dup = NULL; + + if (title) + { + dup = strdup(title); + if (!dup) + { + return -1; + } + } + + free(frame->title); + frame->title = dup; + + frame->geometry_dirty = 1; + frame->status |= FRAME_STATUS_REPAINT; + + return 0; +} + +void +frame_set_icon(struct frame *frame, cairo_surface_t *icon) +{ + struct frame_button *button; + wl_list_for_each(button, &frame->buttons, link) + { + if (button->status_effect != FRAME_STATUS_MENU) + { + continue; + } + if (button->icon) + { + cairo_surface_destroy(button->icon); + } + button->icon = icon; + frame->status |= FRAME_STATUS_REPAINT; + } +} + +void +frame_set_flag(struct frame *frame, enum frame_flag flag) +{ + if (flag & FRAME_FLAG_MAXIMIZED && !(frame->flags & FRAME_FLAG_MAXIMIZED)) + { + frame->geometry_dirty = 1; + } + + frame->flags |= flag; + frame->status |= FRAME_STATUS_REPAINT; +} + +void +frame_unset_flag(struct frame *frame, enum frame_flag flag) +{ + if (flag & FRAME_FLAG_MAXIMIZED && frame->flags & FRAME_FLAG_MAXIMIZED) + { + frame->geometry_dirty = 1; + } + + frame->flags &= ~flag; + frame->status |= FRAME_STATUS_REPAINT; +} + +void +frame_resize(struct frame *frame, int32_t width, int32_t height) +{ + frame->width = width; + frame->height = height; + + frame->geometry_dirty = 1; + frame->status |= FRAME_STATUS_REPAINT; +} + +void +frame_resize_inside(struct frame *frame, int32_t width, int32_t height) +{ + struct theme *t = frame->theme; + int decoration_width, decoration_height, titlebar_height; + + if (frame->title || !wl_list_empty(&frame->buttons)) + { + titlebar_height = t->titlebar_height; + } + else + { + titlebar_height = t->width; + } + + if (frame->flags & FRAME_FLAG_MAXIMIZED) + { + decoration_width = t->width * 2; + decoration_height = t->width + titlebar_height; + } + else + { + decoration_width = (t->width + t->margin) * 2; + decoration_height = t->width + + titlebar_height + t->margin * 2; + } + + frame_resize(frame, width + decoration_width, + height + decoration_height); +} + +int32_t +frame_width(struct frame *frame) +{ + return frame->width; +} + +int32_t +frame_height(struct frame *frame) +{ + return frame->height; +} + +static void +frame_refresh_geometry(struct frame *frame) +{ + struct frame_button *button; + struct theme *t = frame->theme; + int x_l, x_r, y, w, h, titlebar_height; + int32_t decoration_width, decoration_height; + + if (!frame->geometry_dirty) + { + return; + } + + if (frame->title || !wl_list_empty(&frame->buttons)) + { + titlebar_height = t->titlebar_height; + } + else + { + titlebar_height = t->width; + } + + if (frame->flags & FRAME_FLAG_MAXIMIZED) + { + decoration_width = t->width * 2; + decoration_height = t->width + titlebar_height; + + frame->interior.x = t->width; + frame->interior.y = titlebar_height; + frame->interior.width = frame->width - decoration_width; + frame->interior.height = frame->height - decoration_height; + + frame->opaque_margin = 0; + frame->shadow_margin = 0; + } + else + { + decoration_width = (t->width + t->margin) * 2; + decoration_height = t->width + titlebar_height + t->margin * 2; + + frame->interior.x = t->width + t->margin; + frame->interior.y = titlebar_height + t->margin; + frame->interior.width = frame->width - decoration_width; + frame->interior.height = frame->height - decoration_height; + + frame->opaque_margin = t->margin + t->frame_radius; + frame->shadow_margin = t->margin; + } + + x_r = frame->width - t->width - frame->shadow_margin; + x_l = t->width + frame->shadow_margin; + y = t->width + frame->shadow_margin; + wl_list_for_each(button, &frame->buttons, link) + { + const int button_padding = 4; + w = cairo_image_surface_get_width(button->icon); + h = cairo_image_surface_get_height(button->icon); + + if (button->flags & FRAME_BUTTON_DECORATED) + { + w += 10; + } + + if (button->flags & FRAME_BUTTON_ALIGN_RIGHT) + { + x_r -= w; + + button->allocation.x = x_r; + button->allocation.y = y; + button->allocation.width = w + 1; + button->allocation.height = h + 1; + + x_r -= button_padding; + } + else + { + button->allocation.x = x_l; + button->allocation.y = y; + button->allocation.width = w + 1; + button->allocation.height = h + 1; + + x_l += w; + x_l += button_padding; + } + } + + frame->title_rect.x = x_l; + frame->title_rect.y = y; + frame->title_rect.width = x_r - x_l; + frame->title_rect.height = titlebar_height; + + frame->geometry_dirty = 0; +} + +void +frame_interior(struct frame *frame, int32_t *x, int32_t *y, + int32_t *width, int32_t *height) +{ + frame_refresh_geometry(frame); + + if (x) + { + *x = frame->interior.x; + } + if (y) + { + *y = frame->interior.y; + } + if (width) + { + *width = frame->interior.width; + } + if (height) + { + *height = frame->interior.height; + } +} + +void +frame_input_rect(struct frame *frame, int32_t *x, int32_t *y, + int32_t *width, int32_t *height) +{ + frame_refresh_geometry(frame); + + if (x) + { + *x = frame->shadow_margin; + } + if (y) + { + *y = frame->shadow_margin; + } + if (width) + { + *width = frame->width - frame->shadow_margin * 2; + } + if (height) + { + *height = frame->height - frame->shadow_margin * 2; + } +} + +void +frame_opaque_rect(struct frame *frame, int32_t *x, int32_t *y, + int32_t *width, int32_t *height) +{ + frame_refresh_geometry(frame); + + if (x) + { + *x = frame->opaque_margin; + } + if (y) + { + *y = frame->opaque_margin; + } + if (width) + { + *width = frame->width - frame->opaque_margin * 2; + } + if (height) + { + *height = frame->height - frame->opaque_margin * 2; + } +} + +int +frame_get_shadow_margin(struct frame *frame) +{ + frame_refresh_geometry(frame); + + return frame->shadow_margin; +} + +uint32_t +frame_status(struct frame *frame) +{ + return frame->status; +} + +void +frame_status_clear(struct frame *frame, enum frame_status status) +{ + frame->status &= ~status; +} + +static struct frame_button * +frame_find_button(struct frame *frame, int x, int y) +{ + struct frame_button *button; + int rel_x, rel_y; + + wl_list_for_each(button, &frame->buttons, link) + { + rel_x = x - button->allocation.x; + rel_y = y - button->allocation.y; + + if ((0 <= rel_x) && (rel_x < button->allocation.width) && + (0 <= rel_y) && (rel_y < button->allocation.height)) + { + return button; + } + } + + return NULL; +} + +enum theme_location +frame_pointer_enter(struct frame *frame, void *data, int x, int y) +{ + return frame_pointer_motion(frame, data, x, y); +} + +enum theme_location +frame_pointer_motion(struct frame *frame, void *data, int x, int y) +{ + struct frame_pointer *pointer = frame_pointer_get(frame, data); + struct frame_button *button = frame_find_button(frame, x, y); + enum theme_location location; + + location = theme_get_location(frame->theme, x, y, + frame->width, frame->height, + frame->flags & FRAME_FLAG_MAXIMIZED ? + THEME_FRAME_MAXIMIZED : 0); + if (!pointer) + { + return location; + } + + pointer->x = x; + pointer->y = y; + + if (pointer->hover_button == button) + { + return location; + } + + if (pointer->hover_button) + { + frame_button_leave(pointer->hover_button, pointer); + } + + pointer->hover_button = button; + + if (pointer->hover_button) + { + frame_button_enter(pointer->hover_button); + } + + return location; +} + +static void +frame_pointer_button_destroy(struct frame_pointer_button *button) +{ + wl_list_remove(&button->link); + free(button); +} + +static void +frame_pointer_button_press(struct frame *frame, struct frame_pointer *pointer, + struct frame_pointer_button *button) +{ + if (button->button == BTN_RIGHT) + { + if (button->press_location == THEME_LOCATION_TITLEBAR) + { + frame->status |= FRAME_STATUS_MENU; + } + + frame_pointer_button_destroy(button); + + } + else if (button->button == BTN_LEFT) + { + if (pointer->hover_button) + { + frame_button_press(pointer->hover_button); + } + else + { + switch (button->press_location) + { + case THEME_LOCATION_TITLEBAR: + frame->status |= FRAME_STATUS_MOVE; + + frame_pointer_button_destroy(button); + break; + + case THEME_LOCATION_RESIZING_TOP: + case THEME_LOCATION_RESIZING_BOTTOM: + case THEME_LOCATION_RESIZING_LEFT: + case THEME_LOCATION_RESIZING_RIGHT: + case THEME_LOCATION_RESIZING_TOP_LEFT: + case THEME_LOCATION_RESIZING_TOP_RIGHT: + case THEME_LOCATION_RESIZING_BOTTOM_LEFT: + case THEME_LOCATION_RESIZING_BOTTOM_RIGHT: + frame->status |= FRAME_STATUS_RESIZE; + + frame_pointer_button_destroy(button); + break; + + default: + break; + } + } + } +} + +static void +frame_pointer_button_release(struct frame *frame, struct frame_pointer *pointer, + struct frame_pointer_button *button) +{ + if ((button->button == BTN_LEFT) && button->frame_button) + { + if (button->frame_button == pointer->hover_button) + { + frame_button_release(button->frame_button); + } + else + { + frame_button_cancel(button->frame_button); + } + } +} + +static void +frame_pointer_button_cancel(struct frame *frame, struct frame_pointer *pointer, + struct frame_pointer_button *button) +{ + if (button->frame_button) + { + frame_button_cancel(button->frame_button); + } +} + +void +frame_pointer_leave(struct frame *frame, void *data) +{ + struct frame_pointer *pointer = frame_pointer_get(frame, data); + struct frame_pointer_button *button, *next; + if (!pointer) + { + return; + } + + if (pointer->hover_button) + { + frame_button_leave(pointer->hover_button, pointer); + } + + wl_list_for_each_safe(button, next, &pointer->down_buttons, link) + { + frame_pointer_button_cancel(frame, pointer, button); + frame_pointer_button_destroy(button); + } + + frame_pointer_destroy(pointer); +} + +enum theme_location +frame_pointer_button(struct frame *frame, void *data, + uint32_t btn, enum wl_pointer_button_state state) +{ + struct frame_pointer *pointer = frame_pointer_get(frame, data); + struct frame_pointer_button *button; + enum theme_location location = THEME_LOCATION_EXTERIOR; + + if (!pointer) + { + return location; + } + + location = theme_get_location(frame->theme, pointer->x, pointer->y, + frame->width, frame->height, + frame->flags & FRAME_FLAG_MAXIMIZED ? + THEME_FRAME_MAXIMIZED : 0); + + if (state == WL_POINTER_BUTTON_STATE_PRESSED) + { + button = malloc(sizeof *button); + if (!button) + { + return location; + } + + button->button = btn; + button->press_location = location; + button->frame_button = pointer->hover_button; + wl_list_insert(&pointer->down_buttons, &button->link); + + frame_pointer_button_press(frame, pointer, button); + } + else if (state == WL_POINTER_BUTTON_STATE_RELEASED) + { + button = NULL; + wl_list_for_each(button, &pointer->down_buttons, link) + if (button->button == btn) + { + break; + } + /* Make sure we didn't hit the end */ + if (&button->link == &pointer->down_buttons) + { + return location; + } + + location = button->press_location; + frame_pointer_button_release(frame, pointer, button); + frame_pointer_button_destroy(button); + } + + return location; +} + +enum theme_location +frame_touch_down(struct frame *frame, void *data, int32_t id, int x, int y) +{ + struct frame_touch *touch = frame_touch_get(frame, data); + struct frame_button *button = frame_find_button(frame, x, y); + enum theme_location location; + + location = theme_get_location(frame->theme, x, y, + frame->width, frame->height, + frame->flags & FRAME_FLAG_MAXIMIZED ? + THEME_FRAME_MAXIMIZED : 0); + + if (id > 0) + { + return location; + } + + if (touch && button) + { + touch->button = button; + frame_button_press(touch->button); + return location; + } + + switch (location) + { + case THEME_LOCATION_TITLEBAR: + frame->status |= FRAME_STATUS_MOVE; + break; + + case THEME_LOCATION_RESIZING_TOP: + case THEME_LOCATION_RESIZING_BOTTOM: + case THEME_LOCATION_RESIZING_LEFT: + case THEME_LOCATION_RESIZING_RIGHT: + case THEME_LOCATION_RESIZING_TOP_LEFT: + case THEME_LOCATION_RESIZING_TOP_RIGHT: + case THEME_LOCATION_RESIZING_BOTTOM_LEFT: + case THEME_LOCATION_RESIZING_BOTTOM_RIGHT: + frame->status |= FRAME_STATUS_RESIZE; + break; + + default: + break; + } + return location; +} + +void +frame_touch_up(struct frame *frame, void *data, int32_t id) +{ + struct frame_touch *touch = frame_touch_get(frame, data); + + if (id > 0) + { + return; + } + + if (touch && touch->button) + { + frame_button_release(touch->button); + frame_touch_destroy(touch); + } +} + +enum theme_location +frame_double_click(struct frame *frame, void *data, + uint32_t btn, enum wl_pointer_button_state state) +{ + struct frame_pointer *pointer = frame_pointer_get(frame, data); + struct frame_button *button; + enum theme_location location = THEME_LOCATION_EXTERIOR; + + location = theme_get_location(frame->theme, pointer->x, pointer->y, + frame->width, frame->height, + frame->flags & FRAME_FLAG_MAXIMIZED ? + THEME_FRAME_MAXIMIZED : 0); + + button = frame_find_button(frame, pointer->x, pointer->y); + + if ((location != THEME_LOCATION_TITLEBAR) || (btn != BTN_LEFT)) + { + return location; + } + + if (state == WL_POINTER_BUTTON_STATE_PRESSED) + { + if (button) + { + frame_button_press(button); + } + else + { + frame->status |= FRAME_STATUS_MAXIMIZE; + } + } + else if (state == WL_POINTER_BUTTON_STATE_RELEASED) + { + if (button) + { + frame_button_release(button); + } + } + + return location; +} + +void +frame_double_touch_down(struct frame *frame, void *data, int32_t id, + int x, int y) +{ + struct frame_touch *touch = frame_touch_get(frame, data); + struct frame_button *button = frame_find_button(frame, x, y); + enum theme_location location; + + if (touch && button) + { + touch->button = button; + frame_button_press(touch->button); + return; + } + + location = theme_get_location(frame->theme, x, y, + frame->width, frame->height, + frame->flags & FRAME_FLAG_MAXIMIZED ? + THEME_FRAME_MAXIMIZED : 0); + + switch (location) + { + case THEME_LOCATION_TITLEBAR: + frame->status |= FRAME_STATUS_MAXIMIZE; + break; + + case THEME_LOCATION_RESIZING_TOP: + case THEME_LOCATION_RESIZING_BOTTOM: + case THEME_LOCATION_RESIZING_LEFT: + case THEME_LOCATION_RESIZING_RIGHT: + case THEME_LOCATION_RESIZING_TOP_LEFT: + case THEME_LOCATION_RESIZING_TOP_RIGHT: + case THEME_LOCATION_RESIZING_BOTTOM_LEFT: + case THEME_LOCATION_RESIZING_BOTTOM_RIGHT: + frame->status |= FRAME_STATUS_RESIZE; + break; + + default: + break; + } +} + +void +frame_double_touch_up(struct frame *frame, void *data, int32_t id) +{ + struct frame_touch *touch = frame_touch_get(frame, data); + + if (touch && touch->button) + { + frame_button_release(touch->button); + frame_touch_destroy(touch); + } +} + +void +frame_repaint(struct frame *frame, cairo_t *cr) +{ + struct frame_button *button; + uint32_t flags = 0; + + frame_refresh_geometry(frame); + + if (frame->flags & FRAME_FLAG_MAXIMIZED) + { + flags |= THEME_FRAME_MAXIMIZED; + } + + if (frame->flags & FRAME_FLAG_ACTIVE) + { + flags |= THEME_FRAME_ACTIVE; + } + + cairo_save(cr); + theme_render_frame(frame->theme, cr, frame->width, frame->height, + frame->title, &frame->title_rect, + &frame->buttons, flags); + cairo_restore(cr); + + wl_list_for_each(button, &frame->buttons, link) + frame_button_repaint(button, cr); + + frame_status_clear(frame, FRAME_STATUS_REPAINT); +} diff --git a/shared/helpers.h b/shared/helpers.h new file mode 100644 index 0000000..3e501e9 --- /dev/null +++ b/shared/helpers.h @@ -0,0 +1,142 @@ +/* + * Copyright © 2015 Samsung Electronics Co., 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 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. + */ + +#ifndef WESTON_HELPERS_H +#define WESTON_HELPERS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file + * Simple misc helper macros. + */ + +/** + * Compile-time computation of number of items in a hardcoded array. + * + * @param a the array being measured. + * @return the number of items hardcoded into the array. + */ +#ifndef ARRAY_LENGTH +#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) +#endif + +/** + * Returns the smaller of two values. + * + * @param x the first item to compare. + * @param y the second item to compare. + * @return the value that evaluates to lesser than the other. + */ +#ifndef MIN +#define MIN(x,y) (((x) < (y)) ? (x) : (y)) +#endif + +/** + * Returns the bigger of two values. + * + * @param x the first item to compare. + * @param y the second item to compare. + * @return the value that evaluates to more than the other. + */ +#ifndef MAX +#define MAX(x,y) (((x) > (y)) ? (x) : (y)) +#endif + +/** + * Returns a pointer to the containing struct of a given member item. + * + * To demonstrate, the following example retrieves a pointer to + * `example_container` given only its `destroy_listener` member: + * + * @code + * struct example_container { + * struct wl_listener destroy_listener; + * // other members... + * }; + * + * void example_container_destroy(struct wl_listener *listener, void *data) + * { + * struct example_container *ctr; + * + * ctr = wl_container_of(listener, ctr, destroy_listener); + * // destroy ctr... + * } + * @endcode + * + * @param ptr A valid pointer to the contained item. + * + * @param type A pointer to the type of content that the list item + * stores. Type does not need be a valid pointer; a null or + * an uninitialised pointer will suffice. + * + * @param member The named location of ptr within the sample type. + * + * @return The container for the specified pointer. + */ +#ifndef container_of +#define container_of(ptr, type, member) \ + ({ \ + const __typeof__(((type *) 0)->member) *__mptr = (ptr); \ + (type *) ((char *) __mptr - offsetof(type,member)); }) +#endif + +/** + * Build-time static assertion support + * + * A build-time equivalent to assert(), will generate a compilation error + * if the supplied condition does not evaluate true. + * + * The following example demonstrates use of static_assert to ensure that + * arrays which are supposed to mirror each other have a consistent + * size. + * + * This is only a fallback definition; support must be provided by the + * compiler itself. + * + * @code + * int small[4]; + * long expanded[4]; + * + * static_assert(ARRAY_LENGTH(small) == ARRAY_LENGTH(expanded), + * "size mismatch between small and expanded arrays"); + * for (i = 0; i < ARRAY_LENGTH(small); i++) + * expanded[i] = small[4]; + * @endcode + * + * @param cond Expression to check for truth + * @param msg Message to print on failure + */ +#ifndef static_assert +# ifdef _Static_assert +# define static_assert(cond, msg) _Static_assert(cond, msg) +# else +# define static_assert(cond, msg) +# endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* WESTON_HELPERS_H */ diff --git a/shared/image-loader.c b/shared/image-loader.c new file mode 100644 index 0000000..09dc3c7 --- /dev/null +++ b/shared/image-loader.c @@ -0,0 +1,498 @@ +/* + * Copyright © 2008-2012 Kristian Høgsberg + * Copyright © 2012 Intel Corporation + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "shared/helpers.h" +#include "image-loader.h" + +#ifdef HAVE_JPEG +#include +#endif + +#ifdef HAVE_WEBP +#include +#endif + +static int +stride_for_width(int width) +{ + return width * 4; +} + +static void +pixman_image_destroy_func(pixman_image_t *image, void *data) +{ + free(data); +} + +#ifdef HAVE_JPEG + +static void +swizzle_row(JSAMPLE *row, JDIMENSION width) +{ + JSAMPLE *s; + uint32_t *d; + + s = row + (width - 1) * 3; + d = (uint32_t *) (row + (width - 1) * 4); + while (s >= row) + { + *d = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2] << 0); + s -= 3; + d--; + } +} + +static void +error_exit(j_common_ptr cinfo) +{ + longjmp(cinfo->client_data, 1); +} + +static pixman_image_t * +load_jpeg(FILE *fp) +{ + struct jpeg_decompress_struct cinfo; + struct jpeg_error_mgr jerr; + pixman_image_t *pixman_image = NULL; + unsigned int i; + int stride, first; + JSAMPLE *data, *rows[4]; + jmp_buf env; + + cinfo.err = jpeg_std_error(&jerr); + jerr.error_exit = error_exit; + cinfo.client_data = env; + if (setjmp(env)) + { + return NULL; + } + + jpeg_create_decompress(&cinfo); + + jpeg_stdio_src(&cinfo, fp); + + jpeg_read_header(&cinfo, TRUE); + + cinfo.out_color_space = JCS_RGB; + jpeg_start_decompress(&cinfo); + + stride = cinfo.output_width * 4; + data = malloc(stride * cinfo.output_height); + if (data == NULL) + { + fprintf(stderr, "couldn't allocate image data\n"); + return NULL; + } + + while (cinfo.output_scanline < cinfo.output_height) + { + first = cinfo.output_scanline; + for (i = 0; i < ARRAY_LENGTH(rows); i++) + { + rows[i] = data + (first + i) * stride; + } + + jpeg_read_scanlines(&cinfo, rows, ARRAY_LENGTH(rows)); + for (i = 0; first + i < cinfo.output_scanline; i++) + { + swizzle_row(rows[i], cinfo.output_width); + } + } + + jpeg_finish_decompress(&cinfo); + + jpeg_destroy_decompress(&cinfo); + + pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8, + cinfo.output_width, + cinfo.output_height, + (uint32_t *) data, stride); + + pixman_image_set_destroy_function(pixman_image, + pixman_image_destroy_func, data); + + return pixman_image; +} + +#else + +static pixman_image_t * +load_jpeg(FILE *fp) +{ + fprintf(stderr, "JPEG support disabled at compile-time\n"); + return NULL; +} + +#endif + +static inline int +multiply_alpha(int alpha, int color) +{ + int temp = (alpha * color) + 0x80; + + return (temp + (temp >> 8)) >> 8; +} + +static void +premultiply_data(png_structp png, + png_row_infop row_info, + png_bytep data) +{ + unsigned int i; + png_bytep p; + + for (i = 0, p = data; i < row_info->rowbytes; i += 4, p += 4) + { + uint32_t alpha = p[3]; + uint32_t w; + + if (alpha == 0) + { + w = 0; + } + else + { + uint32_t red = p[0]; + uint32_t green = p[1]; + uint32_t blue = p[2]; + + if (alpha != 0xff) + { + red = multiply_alpha(alpha, red); + green = multiply_alpha(alpha, green); + blue = multiply_alpha(alpha, blue); + } + w = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0); + } + + *(uint32_t *) p = w; + } +} + +static void +read_func(png_structp png, png_bytep data, png_size_t size) +{ + FILE *fp = png_get_io_ptr(png); + + if (fread(data, 1, size, fp) != size) + { + png_error(png, NULL); + } +} + +static void +png_error_callback(png_structp png, png_const_charp error_msg) +{ + longjmp(png_jmpbuf(png), 1); +} + +static pixman_image_t * +load_png(FILE *fp) +{ + png_struct *png; + png_info *info; + png_byte *volatile data = NULL; + png_byte **volatile row_pointers = NULL; + png_uint_32 width, height; + int depth, color_type, interlace, stride; + unsigned int i; + pixman_image_t *pixman_image = NULL; + + png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, + png_error_callback, NULL); + if (!png) + { + return NULL; + } + + info = png_create_info_struct(png); + if (!info) + { + png_destroy_read_struct(&png, &info, NULL); + return NULL; + } + + if (setjmp(png_jmpbuf(png))) + { + if (data) + { + free(data); + } + if (row_pointers) + { + free(row_pointers); + } + png_destroy_read_struct(&png, &info, NULL); + return NULL; + } + + png_set_read_fn(png, fp, read_func); + png_read_info(png, info); + png_get_IHDR(png, info, + &width, &height, &depth, + &color_type, &interlace, NULL, NULL); + + if (color_type == PNG_COLOR_TYPE_PALETTE) + { + png_set_palette_to_rgb(png); + } + + if (color_type == PNG_COLOR_TYPE_GRAY) + { + png_set_expand_gray_1_2_4_to_8(png); + } + + if (png_get_valid(png, info, PNG_INFO_tRNS)) + { + png_set_tRNS_to_alpha(png); + } + + if (depth == 16) + { + png_set_strip_16(png); + } + + if (depth < 8) + { + png_set_packing(png); + } + + if ((color_type == PNG_COLOR_TYPE_GRAY) || + (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) + { + png_set_gray_to_rgb(png); + } + + if (interlace != PNG_INTERLACE_NONE) + { + png_set_interlace_handling(png); + } + + png_set_filler(png, 0xff, PNG_FILLER_AFTER); + png_set_read_user_transform_fn(png, premultiply_data); + png_read_update_info(png, info); + png_get_IHDR(png, info, + &width, &height, &depth, + &color_type, &interlace, NULL, NULL); + + + stride = stride_for_width(width); + data = malloc(stride * height); + if (!data) + { + png_destroy_read_struct(&png, &info, NULL); + return NULL; + } + + row_pointers = malloc(height * sizeof row_pointers[0]); + if (row_pointers == NULL) + { + free(data); + png_destroy_read_struct(&png, &info, NULL); + return NULL; + } + + for (i = 0; i < height; i++) + { + row_pointers[i] = &data[i * stride]; + } + + png_read_image(png, row_pointers); + png_read_end(png, info); + + free(row_pointers); + png_destroy_read_struct(&png, &info, NULL); + + pixman_image = pixman_image_create_bits(PIXMAN_a8r8g8b8, + width, height, (uint32_t *) data, stride); + + pixman_image_set_destroy_function(pixman_image, + pixman_image_destroy_func, data); + + return pixman_image; +} + +#ifdef HAVE_WEBP + +static pixman_image_t * +load_webp(FILE *fp) +{ + WebPDecoderConfig config; + uint8_t buffer[16 * 1024]; + int len; + VP8StatusCode status; + WebPIDecoder *idec; + + if (!WebPInitDecoderConfig(&config)) + { + fprintf(stderr, "Library version mismatch!\n"); + return NULL; + } + + /* webp decoding api doesn't seem to specify a min size that's + usable for GetFeatures, but 256 works... */ + len = fread(buffer, 1, 256, fp); + status = WebPGetFeatures(buffer, len, &config.input); + if (status != VP8_STATUS_OK) + { + fprintf(stderr, "failed to parse webp header\n"); + WebPFreeDecBuffer(&config.output); + return NULL; + } + + config.output.colorspace = MODE_BGRA; + config.output.u.RGBA.stride = stride_for_width(config.input.width); + config.output.u.RGBA.size = + config.output.u.RGBA.stride * config.input.height; + config.output.u.RGBA.rgba = + malloc(config.output.u.RGBA.stride * config.input.height); + config.output.is_external_memory = 1; + if (!config.output.u.RGBA.rgba) + { + WebPFreeDecBuffer(&config.output); + return NULL; + } + + rewind(fp); + idec = WebPINewDecoder(&config.output); + if (!idec) + { + WebPFreeDecBuffer(&config.output); + return NULL; + } + + while (!feof(fp)) + { + len = fread(buffer, 1, sizeof buffer, fp); + status = WebPIAppend(idec, buffer, len); + if (status != VP8_STATUS_OK) + { + fprintf(stderr, "webp decode status %d\n", status); + WebPIDelete(idec); + WebPFreeDecBuffer(&config.output); + return NULL; + } + } + + WebPIDelete(idec); + WebPFreeDecBuffer(&config.output); + + return pixman_image_create_bits(PIXMAN_a8r8g8b8, + config.input.width, + config.input.height, + (uint32_t *) config.output.u.RGBA.rgba, + config.output.u.RGBA.stride); +} + +#else + +static pixman_image_t * +load_webp(FILE *fp) +{ + fprintf(stderr, "WebP support disabled at compile-time\n"); + return NULL; +} + +#endif + + +struct image_loader +{ + unsigned char header[4]; + int header_size; + pixman_image_t *(*load)(FILE *fp); +}; + +static const struct image_loader loaders[] = { + { { 0x89, 'P', 'N', 'G' }, 4, load_png }, + { { 0xff, 0xd8 },2, load_jpeg }, + { { 'R', 'I', 'F', 'F' }, 4, load_webp } +}; + +pixman_image_t * +load_image(const char *filename) +{ + pixman_image_t *image = NULL; + unsigned char header[4]; + FILE *fp; + unsigned int i; + + if (!filename || !*filename) + { + return NULL; + } + + fp = fopen(filename, "rb"); + if (!fp) + { + fprintf(stderr, "%s: %s\n", filename, strerror(errno)); + return NULL; + } + + if (fread(header, sizeof header, 1, fp) != 1) + { + fclose(fp); + fprintf(stderr, "%s: unable to read file header\n", filename); + return NULL; + } + + rewind(fp); + for (i = 0; i < ARRAY_LENGTH(loaders); i++) + { + if (memcmp(header, loaders[i].header, + loaders[i].header_size) == 0) + { + image = loaders[i].load(fp); + break; + } + } + + fclose(fp); + + if (i == ARRAY_LENGTH(loaders)) + { + fprintf(stderr, "%s: unrecognized file header " + "0x%02x 0x%02x 0x%02x 0x%02x\n", + filename, header[0], header[1], header[2], header[3]); + } + else if (!image) + { + /* load probably printed something, but just in case */ + fprintf(stderr, "%s: error reading image\n", filename); + } + + return image; +} diff --git a/shared/image-loader.h b/shared/image-loader.h new file mode 100644 index 0000000..7def60b --- /dev/null +++ b/shared/image-loader.h @@ -0,0 +1,34 @@ +/* + * Copyright © 2013 Intel Corporation + * + * 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. + */ + +#ifndef _IMAGE_LOADER_H +#define _IMAGE_LOADER_H + +#include + +pixman_image_t * +load_image(const char *filename); + +#endif diff --git a/shared/matrix.c b/shared/matrix.c new file mode 100644 index 0000000..30d03cf --- /dev/null +++ b/shared/matrix.c @@ -0,0 +1,307 @@ +/* + * Copyright © 2011 Intel Corporation + * 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. + */ + +#include "config.h" + +#include +#include +#include +#include + +#ifdef UNIT_TEST +#define WL_EXPORT +#else +#include +#endif + +#include + + +/* + * Matrices are stored in column-major order, that is the array indices are: + * 0 4 8 12 + * 1 5 9 13 + * 2 6 10 14 + * 3 7 11 15 + */ + +WL_EXPORT void +weston_matrix_init(struct weston_matrix *matrix) +{ + static const struct weston_matrix identity = { + .d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, + .type = 0, + }; + + memcpy(matrix, &identity, sizeof identity); +} + +/* m <- n * m, that is, m is multiplied on the LEFT. */ +WL_EXPORT void +weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n) +{ + struct weston_matrix tmp; + const float *row, *column; + div_t d; + int i, j; + + for (i = 0; i < 16; i++) + { + tmp.d[i] = 0; + d = div(i, 4); + row = m->d + d.quot * 4; + column = n->d + d.rem; + for (j = 0; j < 4; j++) + { + tmp.d[i] += row[j] * column[j * 4]; + } + } + tmp.type = m->type | n->type; + memcpy(m, &tmp, sizeof tmp); +} + +WL_EXPORT void +weston_matrix_translate(struct weston_matrix *matrix, float x, float y, float z) +{ + struct weston_matrix translate = { + .d = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }, + .type = WESTON_MATRIX_TRANSFORM_TRANSLATE, + }; + + weston_matrix_multiply(matrix, &translate); +} + +WL_EXPORT void +weston_matrix_scale(struct weston_matrix *matrix, float x, float y,float z) +{ + struct weston_matrix scale = { + .d = { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }, + .type = WESTON_MATRIX_TRANSFORM_SCALE, + }; + + weston_matrix_multiply(matrix, &scale); +} + +WL_EXPORT void +weston_matrix_rotate_xy(struct weston_matrix *matrix, float cos, float sin) +{ + struct weston_matrix translate = { + .d = { cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, + .type = WESTON_MATRIX_TRANSFORM_ROTATE, + }; + + weston_matrix_multiply(matrix, &translate); +} + +/* v <- m * v */ +WL_EXPORT void +weston_matrix_transform(struct weston_matrix *matrix, struct weston_vector *v) +{ + int i, j; + struct weston_vector t; + + for (i = 0; i < 4; i++) + { + t.f[i] = 0; + for (j = 0; j < 4; j++) + { + t.f[i] += v->f[j] * matrix->d[i + j * 4]; + } + } + + *v = t; +} + +static inline void +swap_rows(double *a, double *b) +{ + unsigned k; + double tmp; + + for (k = 0; k < 13; k += 4) + { + tmp = a[k]; + a[k] = b[k]; + b[k] = tmp; + } +} + +static inline void +swap_unsigned(unsigned *a, unsigned *b) +{ + unsigned tmp; + + tmp = *a; + *a = *b; + *b = tmp; +} + +static inline unsigned +find_pivot(double *column, unsigned k) +{ + unsigned p = k; + for (++k; k < 4; ++k) + { + if (fabs(column[p]) < fabs(column[k])) + { + p = k; + } + } + + return p; +} + +/* + * reference: Gene H. Golub and Charles F. van Loan. Matrix computations. + * 3rd ed. The Johns Hopkins University Press. 1996. + * LU decomposition, forward and back substitution: Chapter 3. + */ + +MATRIX_TEST_EXPORT inline int +matrix_invert(double *A, unsigned *p, const struct weston_matrix *matrix) +{ + unsigned i, j, k; + unsigned pivot; + double pv; + + for (i = 0; i < 4; ++i) + { + p[i] = i; + } + for (i = 16; i--;) + { + A[i] = matrix->d[i]; + } + + /* LU decomposition with partial pivoting */ + for (k = 0; k < 4; ++k) + { + pivot = find_pivot(&A[k * 4], k); + if (pivot != k) + { + swap_unsigned(&p[k], &p[pivot]); + swap_rows(&A[k], &A[pivot]); + } + + pv = A[k * 4 + k]; + if (fabs(pv) < 1e-9) + { + return -1; /* zero pivot, not invertible */ + + } + for (i = k + 1; i < 4; ++i) + { + A[i + k * 4] /= pv; + + for (j = k + 1; j < 4; ++j) + { + A[i + j * 4] -= A[i + k * 4] * A[k + j * 4]; + } + } + } + + return 0; +} + +MATRIX_TEST_EXPORT inline void +inverse_transform(const double *LU, const unsigned *p, float *v) +{ + /* Solve A * x = v, when we have P * A = L * U. + * P * A * x = P * v => L * U * x = P * v + * Let U * x = b, then L * b = P * v. + */ + double b[4]; + unsigned j; + + /* Forward substitution, column version, solves L * b = P * v */ + /* The diagonal of L is all ones, and not explicitly stored. */ + b[0] = v[p[0]]; + b[1] = (double) v[p[1]] - b[0] * LU[1 + 0 * 4]; + b[2] = (double) v[p[2]] - b[0] * LU[2 + 0 * 4]; + b[3] = (double) v[p[3]] - b[0] * LU[3 + 0 * 4]; + b[2] -= b[1] * LU[2 + 1 * 4]; + b[3] -= b[1] * LU[3 + 1 * 4]; + b[3] -= b[2] * LU[3 + 2 * 4]; + + /* backward substitution, column version, solves U * y = b */ +#if 1 + /* hand-unrolled, 25% faster for whole function */ + b[3] /= LU[3 + 3 * 4]; + b[0] -= b[3] * LU[0 + 3 * 4]; + b[1] -= b[3] * LU[1 + 3 * 4]; + b[2] -= b[3] * LU[2 + 3 * 4]; + + b[2] /= LU[2 + 2 * 4]; + b[0] -= b[2] * LU[0 + 2 * 4]; + b[1] -= b[2] * LU[1 + 2 * 4]; + + b[1] /= LU[1 + 1 * 4]; + b[0] -= b[1] * LU[0 + 1 * 4]; + + b[0] /= LU[0 + 0 * 4]; +#else + for (j = 3; j > 0; --j) + { + unsigned k; + b[j] /= LU[j + j * 4]; + for (k = 0; k < j; ++k) + { + b[k] -= b[j] * LU[k + j * 4]; + } + } + + b[0] /= LU[0 + 0 * 4]; +#endif + + /* the result */ + for (j = 0; j < 4; ++j) + { + v[j] = b[j]; + } +} + +WL_EXPORT int +weston_matrix_invert(struct weston_matrix *inverse, + const struct weston_matrix *matrix) +{ + double LU[16]; /* column-major */ + unsigned perm[4]; /* permutation */ + unsigned c; + + if (matrix_invert(LU, perm, matrix) < 0) + { + return -1; + } + + weston_matrix_init(inverse); + for (c = 0; c < 4; ++c) + { + inverse_transform(LU, perm, &inverse->d[c * 4]); + } + inverse->type = matrix->type; + + return 0; +} diff --git a/shared/meson.build b/shared/meson.build new file mode 100644 index 0000000..8073dcd --- /dev/null +++ b/shared/meson.build @@ -0,0 +1,84 @@ +srcs_libshared = [ + 'config-parser.c', + 'option-parser.c', + 'file-util.c', + 'os-compatibility.c', + 'xalloc.c', +] +deps_libshared = dep_wayland_client + +lib_libshared = static_library( + 'shared', + srcs_libshared, + include_directories: common_inc, + dependencies: deps_libshared, + pic: true, + install: false +) +dep_libshared = declare_dependency( + link_with: lib_libshared, + include_directories: public_inc, + dependencies: deps_libshared +) + +srcs_cairo_shared = [ + 'image-loader.c', + 'cairo-util.c', + 'frame.c', +] + +deps_cairo_shared = [ + dep_libshared, + dependency('cairo'), + dependency('libpng'), + dep_pixman, + dep_libm, +] + +dep_pango = dependency('pango', required: false) +dep_pangocairo = dependency('pangocairo', required: false) +dep_glib = dependency('glib-2.0', version: '>= 2.36', required: false) + +if dep_pango.found() and dep_pangocairo.found() and dep_glib.found() + deps_cairo_shared += [ dep_pango, dep_pangocairo, dep_glib ] + config_h.set('HAVE_PANGO', '1') +endif + +if get_option('image-jpeg') + dep_libjpeg = dependency('libjpeg', required: false) + if not dep_libjpeg.found() + dep_libjpeg = cc.find_library('jpeg', required: false) + endif + if not dep_libjpeg.found() + error('JPEG image loading requires libjpeg or jpeg, neither was found. Or, you can use \'-Dimage-jpeg=false\'.') + endif + deps_cairo_shared += dep_libjpeg + config_h.set('HAVE_JPEG', '1') +endif + +if get_option('image-webp') + dep_webp = dependency('libwebp', required: false) + if not dep_webp.found() + error('WEBP image loading requires libwebp which was not found. Or, you can use \'-Dimage-webp=false\'.') + endif + deps_cairo_shared += dep_webp + config_h.set('HAVE_WEBP', '1') +endif + +lib_cairo_shared = static_library( + 'cairo-shared', + srcs_cairo_shared, + include_directories: common_inc, + dependencies: deps_cairo_shared, + install: false +) +dep_lib_cairo_shared = declare_dependency( + link_with: lib_cairo_shared, + dependencies: deps_cairo_shared +) + +dep_matrix_c = declare_dependency( + sources: 'matrix.c', + include_directories: public_inc, + dependencies: dep_libm +) diff --git a/shared/option-parser.c b/shared/option-parser.c new file mode 100644 index 0000000..b382420 --- /dev/null +++ b/shared/option-parser.c @@ -0,0 +1,255 @@ +/* + * Copyright © 2012 Kristian Høgsberg + * + * 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 +#include +#include +#include +#include +#include +#include + +#include +#include "string-helpers.h" + +static bool +handle_option(const struct weston_option *option, char *value) +{ + char *p; + + switch (option->type) + { + case WESTON_OPTION_INTEGER: + if (!safe_strtoint(value, option->data)) + { + return false; + } + return true; + + case WESTON_OPTION_UNSIGNED_INTEGER: + errno = 0; + *(uint32_t *) option->data = strtoul(value, &p, 10); + if ((errno != 0) || (p == value) || (*p != '\0')) + { + return false; + } + return true; + + case WESTON_OPTION_STRING: + *(char **) option->data = strdup(value); + return true; + + default: + assert(0); + return false; + } +} + +static bool +long_option(const struct weston_option *options, int count, char *arg) +{ + int k, len; + + for (k = 0; k < count; k++) + { + if (!options[k].name) + { + continue; + } + + len = strlen(options[k].name); + if (strncmp(options[k].name, arg + 2, len) != 0) + { + continue; + } + + if (options[k].type == WESTON_OPTION_BOOLEAN) + { + if (!arg[len + 2]) + { + *(bool *) options[k].data = true; + + return true; + } + } + else if (arg[len+2] == '=') + { + return handle_option(options + k, arg + len + 3); + } + } + + return false; +} + +static bool +long_option_with_arg(const struct weston_option *options, int count, char *arg, + char *param) +{ + int k, len; + + for (k = 0; k < count; k++) + { + if (!options[k].name) + { + continue; + } + + len = strlen(options[k].name); + if (strncmp(options[k].name, arg + 2, len) != 0) + { + continue; + } + + /* Since long_option() should handle all booleans, we should + * never reach this + */ + assert(options[k].type != WESTON_OPTION_BOOLEAN); + + return handle_option(options + k, param); + } + + return false; +} + +static bool +short_option(const struct weston_option *options, int count, char *arg) +{ + int k; + + if (!arg[1]) + { + return false; + } + + for (k = 0; k < count; k++) + { + if (options[k].short_name != arg[1]) + { + continue; + } + + if (options[k].type == WESTON_OPTION_BOOLEAN) + { + if (!arg[2]) + { + *(bool *) options[k].data = true; + + return true; + } + } + else if (arg[2]) + { + return handle_option(options + k, arg + 2); + } + else + { + return false; + } + } + + return false; +} + +static bool +short_option_with_arg(const struct weston_option *options, int count, char *arg, char *param) +{ + int k; + + if (!arg[1]) + { + return false; + } + + for (k = 0; k < count; k++) + { + if (options[k].short_name != arg[1]) + { + continue; + } + + if (options[k].type == WESTON_OPTION_BOOLEAN) + { + continue; + } + + return handle_option(options + k, param); + } + + return false; +} + +int +parse_options(const struct weston_option *options, + int count, int *argc, char *argv[]) +{ + int i, j; + + for (i = 1, j = 1; i < *argc; i++) + { + if (argv[i][0] == '-') + { + if (argv[i][1] == '-') + { + /* Long option, e.g. --foo or --foo=bar */ + if (long_option(options, count, argv[i])) + { + continue; + } + + /* ...also handle --foo bar */ + if ((i + 1 < *argc) && + long_option_with_arg(options, count, + argv[i], argv[i+1])) + { + i++; + continue; + } + } + else + { + /* Short option, e.g -f or -f42 */ + if (short_option(options, count, argv[i])) + { + continue; + } + + /* ...also handle -f 42 */ + if ((i+1 < *argc) && + short_option_with_arg(options, count, argv[i], argv[i+1])) + { + i++; + continue; + } + } + } + argv[j++] = argv[i]; + } + argv[j] = NULL; + *argc = j; + + return j; +} diff --git a/shared/os-compatibility.c b/shared/os-compatibility.c new file mode 100644 index 0000000..ec86853 --- /dev/null +++ b/shared/os-compatibility.c @@ -0,0 +1,458 @@ +/* + * 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. + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "os-compatibility.h" + +#define READONLY_SEALS (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE) + +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; +} + +int +os_socketpair_cloexec(int domain, int type, int protocol, int *sv) +{ + int ret; + +#ifdef SOCK_CLOEXEC + ret = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv); + if ((ret == 0) || (errno != EINVAL)) + { + return ret; + } +#endif + + ret = socketpair(domain, type, protocol, sv); + if (ret < 0) + { + return ret; + } + + sv[0] = set_cloexec_or_close(sv[0]); + sv[1] = set_cloexec_or_close(sv[1]); + + if ((sv[0] != -1) && (sv[1] != -1)) + { + return 0; + } + + close(sv[0]); + close(sv[1]); + return -1; +} + +int +os_epoll_create_cloexec(void) +{ + int fd; + +#ifdef EPOLL_CLOEXEC + fd = epoll_create1(EPOLL_CLOEXEC); + if (fd >= 0) + { + return fd; + } + if (errno != EINVAL) + { + return -1; + } +#endif + + fd = epoll_create(1); + return set_cloexec_or_close(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. + * + * If the C library implements memfd_create(), it is used to create the + * file purely in memory, without any backing file name on the file + * system, and then sealing off the possibility of shrinking it. This + * can then be checked before accessing mmap()'ed file contents, to + * make sure SIGBUS can't happen. It also avoids requiring + * XDG_RUNTIME_DIR. + */ +int +os_create_anonymous_file(off_t size) +{ + static const char template[] = "/weston-shared-XXXXXX"; + const char *path; + char *name; + int fd; + int ret; + +#ifdef HAVE_MEMFD_CREATE + fd = memfd_create("weston-shared", MFD_CLOEXEC | MFD_ALLOW_SEALING); + if (fd >= 0) + { + /* We can add this seal before calling posix_fallocate(), as + * the file is currently zero-sized anyway. + * + * There is also no need to check for the return value, we + * couldn't do anything with it anyway. + */ + fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK); + } + else +#endif + { + 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; +} + +#ifndef HAVE_STRCHRNUL +char * +strchrnul(const char *s, int c) +{ + while (*s && *s != c) + { + s++; + } + return (char *) s; +} +#endif + +struct ro_anonymous_file +{ + int fd; + size_t size; +}; + +/** Create a new anonymous read-only file of the given size and the given data + * + * \param size The size of \p data. + * \param data The data of the file with the size \p size. + * \return A new \c ro_anonymous_file, or NULL on failure. + * + * The intended use-case is for sending mid-sized data from the compositor + * to clients. + * If the function fails errno is set. + */ +struct ro_anonymous_file * +os_ro_anonymous_file_create(size_t size, + const char *data) +{ + struct ro_anonymous_file *file; + void *map; + + file = zalloc(sizeof *file); + if (!file) + { + errno = ENOMEM; + return NULL; + } + + file->size = size; + file->fd = os_create_anonymous_file(size); + if (file->fd == -1) + { + goto err_free; + } + + map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0); + if (map == MAP_FAILED) + { + goto err_close; + } + + memcpy(map, data, size); + + munmap(map, size); + +#ifdef HAVE_MEMFD_CREATE + /* try to put seals on the file to make it read-only so that we can + * return the fd later directly when support_shared is not set. + * os_ro_anonymous_file_get_fd can handle the fd even if it is not + * sealed read-only and will instead create a new anonymous file on + * each invocation. + */ + fcntl(file->fd, F_ADD_SEALS, READONLY_SEALS); +#endif + + return file; + +err_close: + close(file->fd); +err_free: + free(file); + return NULL; +} + +/** Destroy an anonymous read-only file + * + * \param file The file to destroy. + */ +void +os_ro_anonymous_file_destroy(struct ro_anonymous_file *file) +{ + close(file->fd); + free(file); +} + +/** Get the size of an anonymous read-only file + * + * \param file The file to get the size of. + * \return The size of the file. + */ +size_t +os_ro_anonymous_file_size(struct ro_anonymous_file *file) +{ + return file->size; +} + +/** Returns a file descriptor for the given file, ready to be send to a client. + * + * \param file The file for which to get a file descriptor. + * \param mapmode Describes the ways in which the returned file descriptor can + * be used with mmap. + * \return A file descriptor for the given file that can be send to a client + * or -1 on failure. + * + * The returned file descriptor must not be shared between multiple clients. + * When \p mapmode is RO_ANONYMOUS_FILE_MAPMODE_PRIVATE the file descriptor is + * only guaranteed to be mmapable with \c MAP_PRIVATE, when \p mapmode is + * RO_ANONYMOUS_FILE_MAPMODE_SHARED the file descriptor can be mmaped with + * either MAP_PRIVATE or MAP_SHARED. + * When you're done with the fd you must call \c os_ro_anonymous_file_put_fd + * instead of calling \c close. + * If the function fails errno is set. + */ +int +os_ro_anonymous_file_get_fd(struct ro_anonymous_file *file, + enum ro_anonymous_file_mapmode mapmode) +{ + void *src, *dst; + int fd; + +#ifdef HAVE_MEMFD_CREATE + int seals = fcntl(file->fd, F_GET_SEALS); + + /* file was sealed for read-only and we don't have to support MAP_SHARED + * so we can simply pass the memfd fd + */ + if ((seals != -1) && (mapmode == RO_ANONYMOUS_FILE_MAPMODE_PRIVATE) && + ((seals & READONLY_SEALS) == READONLY_SEALS)) + { + return file->fd; + } +#endif + + /* for all other cases we create a new anonymous file that can be mapped + * with MAP_SHARED and copy the contents to it and return that instead + */ + fd = os_create_anonymous_file(file->size); + if (fd == -1) + { + return fd; + } + + src = mmap(NULL, file->size, PROT_READ, MAP_PRIVATE, file->fd, 0); + if (src == MAP_FAILED) + { + close(fd); + return -1; + } + + dst = mmap(NULL, file->size, PROT_WRITE, MAP_SHARED, fd, 0); + if (dst == MAP_FAILED) + { + close(fd); + munmap(src, file->size); + return -1; + } + + memcpy(dst, src, file->size); + munmap(src, file->size); + munmap(dst, file->size); + + return fd; +} + +/** Release a file descriptor returned by \c os_ro_anonymous_file_get_fd + * + * \param fd A file descriptor returned by \c os_ro_anonymous_file_get_fd. + * \return 0 on success, or -1 on failure. + * + * This function must be called for every file descriptor created with + * \c os_ro_anonymous_file_get_fd to not leake any resources. + * If the function fails errno is set. + */ +int +os_ro_anonymous_file_put_fd(int fd) +{ +#ifdef HAVE_MEMFD_CREATE + int seals = fcntl(fd, F_GET_SEALS); + if ((seals == -1) && (errno != EINVAL)) + { + return -1; + } + + /* The only case in which we do NOT have to close the file is when the file + * was sealed for read-only + */ + if ((seals != -1) && ((seals & READONLY_SEALS) == READONLY_SEALS)) + { + return 0; + } +#endif + + close(fd); + return 0; +} diff --git a/shared/os-compatibility.h b/shared/os-compatibility.h new file mode 100644 index 0000000..817892d --- /dev/null +++ b/shared/os-compatibility.h @@ -0,0 +1,75 @@ +/* + * 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. + */ + +#ifndef OS_COMPATIBILITY_H +#define OS_COMPATIBILITY_H + +#include "config.h" + +#include + +int +os_fd_set_cloexec(int fd); + +int +os_socketpair_cloexec(int domain, int type, int protocol, int *sv); + +int +os_epoll_create_cloexec(void); + +int +os_create_anonymous_file(off_t size); + +#ifndef HAVE_STRCHRNUL +char * +strchrnul(const char *s, int c); +#endif + +struct ro_anonymous_file; + +enum ro_anonymous_file_mapmode +{ + RO_ANONYMOUS_FILE_MAPMODE_PRIVATE, + RO_ANONYMOUS_FILE_MAPMODE_SHARED, +}; + +struct ro_anonymous_file * +os_ro_anonymous_file_create(size_t size, + const char *data); + +void +os_ro_anonymous_file_destroy(struct ro_anonymous_file *file); + +size_t +os_ro_anonymous_file_size(struct ro_anonymous_file *file); + +int +os_ro_anonymous_file_get_fd(struct ro_anonymous_file *file, + enum ro_anonymous_file_mapmode mapmode); + +int +os_ro_anonymous_file_put_fd(int fd); + +#endif /* OS_COMPATIBILITY_H */ diff --git a/shared/platform.h b/shared/platform.h new file mode 100644 index 0000000..7002a97 --- /dev/null +++ b/shared/platform.h @@ -0,0 +1,178 @@ +/* + * Copyright © 2015 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. + */ + +#ifndef WESTON_PLATFORM_H +#define WESTON_PLATFORM_H + +#include +#include + +#ifdef ENABLE_EGL +#include +#include +#include + +#include "weston-egl-ext.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef ENABLE_EGL + +static bool +weston_check_egl_extension(const char *extensions, const char *extension) +{ + size_t extlen = strlen(extension); + const char *end = extensions + strlen(extensions); + + while (extensions < end) + { + size_t n = 0; + + /* Skip whitespaces, if any */ + if (*extensions == ' ') + { + extensions++; + continue; + } + + n = strcspn(extensions, " "); + + /* Compare strings */ + if ((n == extlen) && (strncmp(extension, extensions, n) == 0)) + { + return true; /* Found */ + + } + extensions += n; + } + + /* Not found */ + return false; +} + +static inline void * +weston_platform_get_egl_proc_address(const char *address) +{ + const char *extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); + + if (extensions && + (weston_check_egl_extension(extensions, "EGL_EXT_platform_wayland") || + weston_check_egl_extension(extensions, "EGL_KHR_platform_wayland"))) + { + return (void *) eglGetProcAddress(address); + } + + return NULL; +} + +static inline EGLDisplay +weston_platform_get_egl_display(EGLenum platform, void *native_display, + const EGLint *attrib_list) +{ + static PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display = NULL; + + if (!get_platform_display) + { + get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC) + weston_platform_get_egl_proc_address( + "eglGetPlatformDisplayEXT"); + } + + if (get_platform_display) + { + return get_platform_display(platform, + native_display, attrib_list); + } + + return eglGetDisplay((EGLNativeDisplayType) native_display); +} + +static inline EGLSurface +weston_platform_create_egl_surface(EGLDisplay dpy, EGLConfig config, + void *native_window, + const EGLint *attrib_list) +{ + static PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC + create_platform_window = NULL; + + if (!create_platform_window) + { + create_platform_window = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) + weston_platform_get_egl_proc_address( + "eglCreatePlatformWindowSurfaceEXT"); + } + + if (create_platform_window) + { + return create_platform_window(dpy, config, + native_window, + attrib_list); + } + + return eglCreateWindowSurface(dpy, config, + (EGLNativeWindowType) native_window, + attrib_list); +} + +static inline EGLBoolean +weston_platform_destroy_egl_surface(EGLDisplay display, + EGLSurface surface) +{ + return eglDestroySurface(display, surface); +} + +#else /* ENABLE_EGL */ + +static inline void * +weston_platform_get_egl_display(int platform, void *native_display, + const int *attrib_list) +{ + return NULL; +} + +static inline void * +weston_platform_create_egl_surface(void *dpy, void *config, + void *native_window, + const int *attrib_list) +{ + return NULL; +} + +static inline unsigned int +weston_platform_destroy_egl_surface(void *display, + void *surface) +{ + return 1; +} +#endif /* ENABLE_EGL */ + +#ifdef __cplusplus +} +#endif + +#endif /* WESTON_PLATFORM_H */ diff --git a/shared/string-helpers.h b/shared/string-helpers.h new file mode 100644 index 0000000..dc0774e --- /dev/null +++ b/shared/string-helpers.h @@ -0,0 +1,75 @@ +/* + * Copyright © 2016 Samsung Electronics Co., 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. + */ + +#ifndef WESTON_STRING_HELPERS_H +#define WESTON_STRING_HELPERS_H + +#include +#include +#include +#include +#include + +/* Convert string to integer + * + * Parses a base-10 number from the given string. Checks that the + * string is not blank, contains only numerical characters, and is + * within the range of INT32_MIN to INT32_MAX. If the validation is + * successful the result is stored in *value; otherwise *value is + * unchanged and errno is set appropriately. + * + * \return true if the number parsed successfully, false on error + */ +static inline bool +safe_strtoint(const char *str, int32_t *value) +{ + long ret; + char *end; + + assert(str != NULL); + + errno = 0; + ret = strtol(str, &end, 10); + if (errno != 0) + { + return false; + } + else if ((end == str) || (*end != '\0')) + { + errno = EINVAL; + return false; + } + + if ((long) ((int32_t) ret) != ret) + { + errno = ERANGE; + return false; + } + *value = (int32_t) ret; + + return true; +} + +#endif /* WESTON_STRING_HELPERS_H */ diff --git a/shared/timespec-util.h b/shared/timespec-util.h new file mode 100644 index 0000000..2989f9d --- /dev/null +++ b/shared/timespec-util.h @@ -0,0 +1,263 @@ +/* + * Copyright © 2014 - 2015 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. + */ + +#ifndef TIMESPEC_UTIL_H +#define TIMESPEC_UTIL_H + +#include +#include +#include +#include + +#define NSEC_PER_SEC 1000000000 + +/* Subtract timespecs + * + * \param r[out] result: a - b + * \param a[in] operand + * \param b[in] operand + */ +static inline void +timespec_sub(struct timespec *r, + const struct timespec *a, const struct timespec *b) +{ + r->tv_sec = a->tv_sec - b->tv_sec; + r->tv_nsec = a->tv_nsec - b->tv_nsec; + if (r->tv_nsec < 0) + { + r->tv_sec--; + r->tv_nsec += NSEC_PER_SEC; + } +} + +/* Add a nanosecond value to a timespec + * + * \param r[out] result: a + b + * \param a[in] base operand as timespec + * \param b[in] operand in nanoseconds + */ +static inline void +timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b) +{ + r->tv_sec = a->tv_sec + (b / NSEC_PER_SEC); + r->tv_nsec = a->tv_nsec + (b % NSEC_PER_SEC); + + if (r->tv_nsec >= NSEC_PER_SEC) + { + r->tv_sec++; + r->tv_nsec -= NSEC_PER_SEC; + } + else if (r->tv_nsec < 0) + { + r->tv_sec--; + r->tv_nsec += NSEC_PER_SEC; + } +} + +/* Add a millisecond value to a timespec + * + * \param r[out] result: a + b + * \param a[in] base operand as timespec + * \param b[in] operand in milliseconds + */ +static inline void +timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b) +{ + timespec_add_nsec(r, a, b * 1000000); +} + +/* Convert timespec to nanoseconds + * + * \param a timespec + * \return nanoseconds + */ +static inline int64_t +timespec_to_nsec(const struct timespec *a) +{ + return (int64_t) a->tv_sec * NSEC_PER_SEC + a->tv_nsec; +} + +/* Subtract timespecs and return result in nanoseconds + * + * \param a[in] operand + * \param b[in] operand + * \return to_nanoseconds(a - b) + */ +static inline int64_t +timespec_sub_to_nsec(const struct timespec *a, const struct timespec *b) +{ + struct timespec r; + timespec_sub(&r, a, b); + return timespec_to_nsec(&r); +} + +/* Convert timespec to milliseconds + * + * \param a timespec + * \return milliseconds + * + * Rounding to integer milliseconds happens always down (floor()). + */ +static inline int64_t +timespec_to_msec(const struct timespec *a) +{ + return (int64_t) a->tv_sec * 1000 + a->tv_nsec / 1000000; +} + +/* Subtract timespecs and return result in milliseconds + * + * \param a[in] operand + * \param b[in] operand + * \return to_milliseconds(a - b) + */ +static inline int64_t +timespec_sub_to_msec(const struct timespec *a, const struct timespec *b) +{ + return timespec_sub_to_nsec(a, b) / 1000000; +} + +/* Convert timespec to microseconds + * + * \param a timespec + * \return microseconds + * + * Rounding to integer microseconds happens always down (floor()). + */ +static inline int64_t +timespec_to_usec(const struct timespec *a) +{ + return (int64_t) a->tv_sec * 1000000 + a->tv_nsec / 1000; +} + +/* Convert timespec to protocol data + * + * \param a timespec + * \param tv_sec_hi[out] the high bytes of the seconds part + * \param tv_sec_lo[out] the low bytes of the seconds part + * \param tv_nsec[out] the nanoseconds part + * + * The input timespec must be normalized (the nanoseconds part should + * be less than 1 second) and non-negative. + */ +static inline void +timespec_to_proto(const struct timespec *a, uint32_t *tv_sec_hi, + uint32_t *tv_sec_lo, uint32_t *tv_nsec) +{ + assert(a->tv_sec >= 0); + assert(a->tv_nsec >= 0 && a->tv_nsec < NSEC_PER_SEC); + + uint64_t sec64 = a->tv_sec; + + *tv_sec_hi = sec64 >> 32; + *tv_sec_lo = sec64 & 0xffffffff; + *tv_nsec = a->tv_nsec; +} + +/* Convert nanoseconds to timespec + * + * \param a timespec + * \param b nanoseconds + */ +static inline void +timespec_from_nsec(struct timespec *a, int64_t b) +{ + a->tv_sec = b / NSEC_PER_SEC; + a->tv_nsec = b % NSEC_PER_SEC; +} + +/* Convert microseconds to timespec + * + * \param a timespec + * \param b microseconds + */ +static inline void +timespec_from_usec(struct timespec *a, int64_t b) +{ + timespec_from_nsec(a, b * 1000); +} + +/* Convert milliseconds to timespec + * + * \param a timespec + * \param b milliseconds + */ +static inline void +timespec_from_msec(struct timespec *a, int64_t b) +{ + timespec_from_nsec(a, b * 1000000); +} + +/* Convert protocol data to timespec + * + * \param a[out] timespec + * \param tv_sec_hi the high bytes of seconds part + * \param tv_sec_lo the low bytes of seconds part + * \param tv_nsec the nanoseconds part + */ +static inline void +timespec_from_proto(struct timespec *a, uint32_t tv_sec_hi, + uint32_t tv_sec_lo, uint32_t tv_nsec) +{ + a->tv_sec = ((uint64_t) tv_sec_hi << 32) + tv_sec_lo; + a->tv_nsec = tv_nsec; +} + +/* Check if a timespec is zero + * + * \param a timespec + * \return whether the timespec is zero + */ +static inline bool +timespec_is_zero(const struct timespec *a) +{ + return a->tv_sec == 0 && a->tv_nsec == 0; +} + +/* Check if two timespecs are equal + * + * \param a[in] timespec to check + * \param b[in] timespec to check + * \return whether timespecs a and b are equal + */ +static inline bool +timespec_eq(const struct timespec *a, const struct timespec *b) +{ + return a->tv_sec == b->tv_sec && + a->tv_nsec == b->tv_nsec; +} + +/* Convert milli-Hertz to nanoseconds + * + * \param mhz frequency in mHz, not zero + * \return period in nanoseconds + */ +static inline int64_t +millihz_to_nsec(uint32_t mhz) +{ + assert(mhz > 0); + return 1000000000000LL / mhz; +} + +#endif /* TIMESPEC_UTIL_H */ diff --git a/shared/weston-egl-ext.h b/shared/weston-egl-ext.h new file mode 100644 index 0000000..f7cc13b --- /dev/null +++ b/shared/weston-egl-ext.h @@ -0,0 +1,221 @@ +/* + * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. + * All Rights Reserved. + * + * 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. + */ +/* Extensions used by Weston, copied from Mesa's eglmesaext.h, */ + +#ifndef WESTON_EGL_EXT_H +#define WESTON_EGL_EXT_H + +#ifdef ENABLE_EGL + +#ifndef EGL_WL_bind_wayland_display +#define EGL_WL_bind_wayland_display 1 + +struct wl_display; + +#ifdef EGL_EGLEXT_PROTOTYPES +EGLAPI EGLBoolean EGLAPIENTRY eglBindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display); +EGLAPI EGLBoolean EGLAPIENTRY eglUnbindWaylandDisplayWL(EGLDisplay dpy, struct wl_display *display); +#endif +typedef EGLBoolean (EGLAPIENTRYP PFNEGLBINDWAYLANDDISPLAYWL)(EGLDisplay dpy, struct wl_display *display); +typedef EGLBoolean (EGLAPIENTRYP PFNEGLUNBINDWAYLANDDISPLAYWL)(EGLDisplay dpy, struct wl_display *display); +#endif + +/* + * This is a little different to the tests shipped with EGL implementations, + * which wrap the entire thing in #ifndef EGL_WL_bind_wayland_display, then go + * on to define both BindWaylandDisplay and QueryWaylandBuffer. + * + * Unfortunately, some implementations (particularly the version of Mesa shipped + * in Ubuntu 12.04) define EGL_WL_bind_wayland_display, but then only provide + * prototypes for (Un)BindWaylandDisplay, completely omitting + * QueryWaylandBuffer. + * + * Detect this, and provide our own definitions if necessary. + */ +#ifndef EGL_WAYLAND_BUFFER_WL +#define EGL_WAYLAND_BUFFER_WL 0x31D5 /* eglCreateImageKHR target */ +#define EGL_WAYLAND_PLANE_WL 0x31D6 /* eglCreateImageKHR target */ + +#define EGL_TEXTURE_Y_U_V_WL 0x31D7 +#define EGL_TEXTURE_Y_UV_WL 0x31D8 +#define EGL_TEXTURE_Y_XUXV_WL 0x31D9 +#define EGL_TEXTURE_EXTERNAL_WL 0x31DA + +struct wl_resource; +#ifdef EGL_EGLEXT_PROTOTYPES +EGLAPI EGLBoolean EGLAPIENTRY eglQueryWaylandBufferWL(EGLDisplay dpy, struct wl_resource *buffer, EGLint attribute, EGLint *value); +#endif +typedef EGLBoolean (EGLAPIENTRYP PFNEGLQUERYWAYLANDBUFFERWL)(EGLDisplay dpy, struct wl_resource *buffer, EGLint attribute, EGLint *value); +#endif + +#ifndef EGL_WL_create_wayland_buffer_from_image +#define EGL_WL_create_wayland_buffer_from_image 1 + +#ifdef EGL_EGLEXT_PROTOTYPES +EGLAPI struct wl_buffer *EGLAPIENTRY eglCreateWaylandBufferFromImageWL(EGLDisplay dpy, EGLImageKHR image); +#endif +typedef struct wl_buffer * (EGLAPIENTRYP PFNEGLCREATEWAYLANDBUFFERFROMIMAGEWL)(EGLDisplay dpy, EGLImageKHR image); +#endif + +#ifndef EGL_TEXTURE_EXTERNAL_WL +#define EGL_TEXTURE_EXTERNAL_WL 0x31DA +#endif + +#ifndef EGL_BUFFER_AGE_EXT +#define EGL_BUFFER_AGE_EXT 0x313D +#endif + +#ifndef EGL_WAYLAND_Y_INVERTED_WL +#define EGL_WAYLAND_Y_INVERTED_WL 0x31DB /* eglQueryWaylandBufferWL attribute */ +#endif + +#ifndef GL_EXT_unpack_subimage +#define GL_EXT_unpack_subimage 1 +#define GL_UNPACK_ROW_LENGTH_EXT 0x0CF2 +#define GL_UNPACK_SKIP_ROWS_EXT 0x0CF3 +#define GL_UNPACK_SKIP_PIXELS_EXT 0x0CF4 +#endif /* GL_EXT_unpack_subimage */ + +/* Mesas gl2ext.h and probably Khronos upstream defined + * GL_EXT_unpack_subimage with non _EXT suffixed GL_UNPACK_* tokens. + * In case we're using that mess, manually define the _EXT versions + * of the tokens here.*/ +#if defined(GL_EXT_unpack_subimage) && !defined(GL_UNPACK_ROW_LENGTH_EXT) +#define GL_UNPACK_ROW_LENGTH_EXT 0x0CF2 +#define GL_UNPACK_SKIP_ROWS_EXT 0x0CF3 +#define GL_UNPACK_SKIP_PIXELS_EXT 0x0CF4 +#endif + +/* Define needed tokens from EGL_EXT_image_dma_buf_import extension + * here to avoid having to add ifdefs everywhere.*/ +#ifndef EGL_EXT_image_dma_buf_import +#define EGL_LINUX_DMA_BUF_EXT 0x3270 +#define EGL_LINUX_DRM_FOURCC_EXT 0x3271 +#define EGL_DMA_BUF_PLANE0_FD_EXT 0x3272 +#define EGL_DMA_BUF_PLANE0_OFFSET_EXT 0x3273 +#define EGL_DMA_BUF_PLANE0_PITCH_EXT 0x3274 +#define EGL_DMA_BUF_PLANE1_FD_EXT 0x3275 +#define EGL_DMA_BUF_PLANE1_OFFSET_EXT 0x3276 +#define EGL_DMA_BUF_PLANE1_PITCH_EXT 0x3277 +#define EGL_DMA_BUF_PLANE2_FD_EXT 0x3278 +#define EGL_DMA_BUF_PLANE2_OFFSET_EXT 0x3279 +#define EGL_DMA_BUF_PLANE2_PITCH_EXT 0x327A +#endif + +/* Define tokens from EGL_EXT_image_dma_buf_import_modifiers */ +#ifndef EGL_EXT_image_dma_buf_import_modifiers +#define EGL_EXT_image_dma_buf_import_modifiers 1 +#define EGL_DMA_BUF_PLANE3_FD_EXT 0x3440 +#define EGL_DMA_BUF_PLANE3_OFFSET_EXT 0x3441 +#define EGL_DMA_BUF_PLANE3_PITCH_EXT 0x3442 +#define EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT 0x3443 +#define EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT 0x3444 +#define EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT 0x3445 +#define EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT 0x3446 +#define EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT 0x3447 +#define EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT 0x3448 +#define EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT 0x3449 +#define EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT 0x344A +typedef EGLBoolean (EGLAPIENTRYP PFNEGLQUERYDMABUFFORMATSEXTPROC)(EGLDisplay dpy, EGLint max_formats, EGLint *formats, EGLint *num_formats); +typedef EGLBoolean (EGLAPIENTRYP PFNEGLQUERYDMABUFMODIFIERSEXTPROC)(EGLDisplay dpy, EGLint format, EGLint max_modifiers, EGLuint64KHR *modifiers, EGLBoolean *external_only, EGLint *num_modifiers); +#endif + +#ifndef EGL_EXT_swap_buffers_with_damage +#define EGL_EXT_swap_buffers_with_damage 1 +typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects); +#endif /* EGL_EXT_swap_buffers_with_damage */ + +#ifndef EGL_MESA_configless_context +#define EGL_MESA_configless_context 1 +#define EGL_NO_CONFIG_MESA ((EGLConfig) 0) +#endif + +#ifndef EGL_NO_CONFIG_KHR +#define EGL_NO_CONFIG_KHR ((EGLConfig) 0) +#endif + +#ifndef EGL_EXT_platform_base +#define EGL_EXT_platform_base 1 +typedef EGLDisplay (EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYEXTPROC)(EGLenum platform, void *native_display, const EGLint *attrib_list); +typedef EGLSurface (EGLAPIENTRYP PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)(EGLDisplay dpy, EGLConfig config, void *native_window, const EGLint *attrib_list); +typedef EGLSurface (EGLAPIENTRYP PFNEGLCREATEPLATFORMPIXMAPSURFACEEXTPROC)(EGLDisplay dpy, EGLConfig config, void *native_pixmap, const EGLint *attrib_list); +#endif /* EGL_EXT_platform_base */ + +#ifndef EGL_PLATFORM_GBM_KHR +#define EGL_PLATFORM_GBM_KHR 0x31D7 +#endif + +#ifndef EGL_PLATFORM_WAYLAND_KHR +#define EGL_PLATFORM_WAYLAND_KHR 0x31D8 +#endif + +#ifndef EGL_PLATFORM_X11_KHR +#define EGL_PLATFORM_X11_KHR 0x31D5 +#endif + +#ifndef EGL_PLATFORM_SURFACELESS_MESA +#define EGL_PLATFORM_SURFACELESS_MESA 0x31DD +#endif + +#ifndef EGL_KHR_cl_event2 +#define EGL_KHR_cl_event2 1 +typedef void *EGLSyncKHR; +#endif /* EGL_KHR_cl_event2 */ + +#ifndef EGL_NO_SYNC_KHR +#define EGL_NO_SYNC_KHR ((EGLSyncKHR) 0) +#endif + +#ifndef EGL_KHR_fence_sync +#define EGL_KHR_fence_sync 1 +typedef EGLSyncKHR (EGLAPIENTRYP PFNEGLCREATESYNCKHRPROC)(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list); +typedef EGLBoolean (EGLAPIENTRYP PFNEGLDESTROYSYNCKHRPROC)(EGLDisplay dpy, EGLSyncKHR sync); +#endif /* EGL_KHR_fence_sync */ + +#ifndef EGL_ANDROID_native_fence_sync +#define EGL_ANDROID_native_fence_sync 1 +typedef EGLint (EGLAPIENTRYP PFNEGLDUPNATIVEFENCEFDANDROIDPROC)(EGLDisplay dpy, EGLSyncKHR sync); +#endif /* EGL_ANDROID_native_fence_sync */ + +#ifndef EGL_SYNC_NATIVE_FENCE_ANDROID +#define EGL_SYNC_NATIVE_FENCE_ANDROID 0x3144 +#endif + +#ifndef EGL_NO_NATIVE_FENCE_FD_ANDROID +#define EGL_NO_NATIVE_FENCE_FD_ANDROID -1 +#endif + +#else /* ENABLE_EGL */ + +/* EGL platform definition are keept to allow compositor-xx.c to build */ +#define EGL_PLATFORM_GBM_KHR 0x31D7 +#define EGL_PLATFORM_WAYLAND_KHR 0x31D8 +#define EGL_PLATFORM_X11_KHR 0x31D5 +#define EGL_PLATFORM_SURFACELESS_MESA 0x31DD + +#endif /* ENABLE_EGL */ + +#endif diff --git a/shared/xalloc.c b/shared/xalloc.c new file mode 100644 index 0000000..c6188a7 --- /dev/null +++ b/shared/xalloc.c @@ -0,0 +1,55 @@ +/* + * Copyright © 2008 Kristian Høgsberg + * + * 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 +#include +#include +#include + +#include "xalloc.h" + +void * +fail_on_null(void *p, size_t size, char *file, int32_t line) +{ + if (p == NULL) + { + fprintf(stderr, "[%s] ", program_invocation_short_name); + if (file) + { + fprintf(stderr, "%s:%d: ", file, line); + } + fprintf(stderr, "out of memory"); + if (size) + { + fprintf(stderr, " (%zd)", size); + } + fprintf(stderr, "\n"); + exit(EXIT_FAILURE); + } + + return p; +} diff --git a/shared/xalloc.h b/shared/xalloc.h new file mode 100644 index 0000000..11c7a6b --- /dev/null +++ b/shared/xalloc.h @@ -0,0 +1,51 @@ +/* + * Copyright © 2008 Kristian Høgsberg + * + * 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. + */ + +#ifndef WESTON_XALLOC_H +#define WESTON_XALLOC_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#include + +void * +fail_on_null(void *p, size_t size, char *file, int32_t line); + +#define xmalloc(s) (fail_on_null(malloc(s), (s), __FILE__, __LINE__)) +#define xzalloc(s) (fail_on_null(zalloc(s), (s), __FILE__, __LINE__)) +#define xstrdup(s) (fail_on_null(strdup(s), 0, __FILE__, __LINE__)) +#define xrealloc(p, s) (fail_on_null(realloc(p, s), (s), __FILE__, __LINE__)) + +#ifdef __cplusplus +} +#endif + +#endif /* WESTON_XALLOC_H */ diff --git a/weston_clone_app.mk b/weston_clone_app.mk new file mode 100644 index 0000000..d6a3274 --- /dev/null +++ b/weston_clone_app.mk @@ -0,0 +1,34 @@ +######################################################### +# PPL make information +######################################################### +SRCDIRS += ${WESTON_CLONE_APP_DIR} +SRCDIRS += ${WESTON_CLONE_APP_PROTOCOL_DIR} +SRCDIRS += ${WESTON_CLONE_APP_SHARED_DIR} + +APP_INCDIRS += -I${WESTON_CLONE_APP_DIR} +APP_INCDIRS += -I${WESTON_CLONE_APP_PROTOCOL_DIR} +APP_INCDIRS += -I${WESTON_CLONE_APP_SHARED_DIR} +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/cairo +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/atk-1.0 +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/blkid +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/cairo +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/freetype2 +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/fribidi +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/gdk-pixbuf-2.0 +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/gio-unix-2.0 +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/glib-2.0 +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/gtk-3.0 +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/harfbuzz +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/libmount +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/libpng16 +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/libweston-9 +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/pango-1.0 +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/pixman-1 +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/uuid +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/include/weston +APP_INCDIRS += -I${SDKTARGETSYSROOT}/usr/lib/glib-2.0/include + +APP_OBJS += screenshot.o window.o +APP_OBJS += pointer-constraints-unstable-v1-protocol.o relative-pointer-unstable-v1-protocol.o text-cursor-position-protocol.o +APP_OBJS += viewporter-protocol.o weston-screenshooter-protocol.o xdg-shell-protocol.o +APP_OBJS += os-compatibility.o xalloc.o file-util.o cairo-util.o frame.o image-loader.o diff --git a/window.c b/window.c new file mode 100644 index 0000000..01a3a6a --- /dev/null +++ b/window.c @@ -0,0 +1,7610 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_CAIRO_EGL +#include + +#ifdef USE_CAIRO_GLESV2 +#include +#include +#else +#include +#endif +#include +#include + +#include +#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 +#ifdef HAVE_XKBCOMMON_COMPOSE +#include +#endif +#include + +#include +#include +#include "shared/cairo-util.h" +#include "shared/helpers.h" +#include "shared/xalloc.h" +#include +#include "xdg-shell-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 "shared/string-helpers.h" + +#include "window.h" +#include "viewporter-client-protocol.h" + +#define ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION 1 +#define ZWP_POINTER_CONSTRAINTS_V1_VERSION 1 + +#define DEFAULT_XCURSOR_SIZE 32 + +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 xdg_wm_base *xdg_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 data_device_manager_version; + struct wp_viewporter *viewporter; +}; + +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 wp_viewport *viewport; +}; + +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; + + 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 xdg_surface *xdg_surface; + struct xdg_toplevel *xdg_toplevel; + struct xdg_popup *xdg_popup; + + struct window *parent; + struct window *last_parent; + + 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; + 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 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; + int viewport_dest_width; + int viewport_dest_height; +}; + +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; + struct window *locked_window; + struct window *confined_window; + 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: %s\n", + size, strerror(errno)); + return NULL; + } + + *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (*data == MAP_FAILED) + { + fprintf(stderr, "mmap failed: %s\n", strerror(errno)); + 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; + int stride, length, offset; + void *map; + + data = malloc(sizeof *data); + if (data == NULL) + { + return NULL; + } + + stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, + 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_ARGB32, + rectangle->width, + rectangle->height, + stride); + + cairo_surface_set_user_data(surface, &shm_surface_data_key, + data, shm_surface_data_destroy); + + 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 = DEFAULT_XCURSOR_SIZE; + char *theme = NULL, *size_str; + unsigned int i, j; + struct wl_cursor *cursor; + + theme = getenv("XCURSOR_THEME"); + + size_str = getenv("XCURSOR_SIZE"); + if (size_str) + { + safe_strtoint(size_str, &size); + if (size <= 0) + { + size = DEFAULT_XCURSOR_SIZE; + } + } + + 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, theme); + weston_config_section_get_int(s, "cursor-size", &size, size); + 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) +{ + struct widget *widget = surface->widget; + 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; + } + + if (surface->viewport) + { + wp_viewport_set_destination(surface->viewport, + widget->viewport_dest_width, + widget->viewport_dest_height); + } + + 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 +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; + } + + 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) + { + struct touch_point *tp, *tmp; + + wl_list_for_each_safe(tp, tmp, + &input->touch_point_list, + link) + { + wl_list_remove(&tp->link); + free(tp); + } + + input->touch_focus = NULL; + } + if (input->pointer_focus == window) + { + input->pointer_focus = NULL; + } + if (input->keyboard_focus == window) + { + input->keyboard_focus = NULL; + } + if (input->locked_window == window) + { + input->locked_window = NULL; + } + if (input->confined_window == window) + { + input->confined_window = 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) + { + xdg_toplevel_destroy(window->xdg_toplevel); + } + if (window->xdg_popup) + { + xdg_popup_destroy(window->xdg_popup); + } + if (window->xdg_surface) + { + xdg_surface_destroy(window->xdg_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; + int alloc_x, alloc_y, width, height; + double scale; + + wl_list_for_each(child, &widget->child_list, link) + { + target = widget_find_widget(child, x, y); + if (target) + { + return target; + } + } + + alloc_x = widget->allocation.x; + alloc_y = widget->allocation.y; + width = widget->allocation.width; + height = widget->allocation.height; + + if ((widget->viewport_dest_width != -1) && + (widget->viewport_dest_height != -1)) + { + scale = widget->viewport_dest_width / (double) width; + alloc_x = alloc_x * scale; + width = widget->viewport_dest_width; + + scale = widget->viewport_dest_height / (double) height; + alloc_y = alloc_y * scale; + height = widget->viewport_dest_height; + } + + if ((alloc_x <= x) && (x < alloc_x + width) && + (alloc_y <= y) && (y < alloc_y + 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; + widget->viewport_dest_width = -1; + widget->viewport_dest_height = -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 + M_PI_2; + translate_x = 0; + translate_y = surface_width; + break; + + case WL_OUTPUT_TRANSFORM_FLIPPED_90: + angle = M_PI + M_PI_2; + translate_x = 0; + translate_y = 0; + 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_2; + translate_x = surface_height; + translate_y = 0; + break; + + case WL_OUTPUT_TRANSFORM_FLIPPED_270: + angle = M_PI_2; + translate_x = surface_height; + translate_y = surface_width; + 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; +} + +int +widget_set_viewport_destination(struct widget *widget, int width, int height) +{ + struct window *window = widget->window; + struct display *display = window->display; + struct surface *surface = widget->surface; + if (!display->viewporter) + { + return -1; + } + + if ((width == -1) && (height == -1)) + { + if (surface->viewport) + { + wp_viewport_destroy(surface->viewport); + surface->viewport = NULL; + } + + widget->viewport_dest_width = -1; + widget->viewport_dest_height = -1; + return 0; + } + + if (!surface->viewport) + { + surface->viewport = wp_viewporter_get_viewport(display->viewporter, + surface->surface); + if (!surface->viewport) + { + return -1; + } + } + + widget->viewport_dest_width = width; + widget->viewport_dest_height = height; + + return 0; +} + +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_rgba(cr, 1.0, 1.0, 1.0, 0.85); + cairo_move_to(cr, 10, 17); + cairo_set_font_size(cr, 14); + 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_set_font_size(cr, 14); + 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); + xdg_toplevel_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); + + xdg_toplevel_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); + if (!frame->frame) + { + free(frame); + return 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_PRIVATE, 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) + { + input->modifiers |= MOD_CONTROL_MASK; + } + if (mask & input->xkb.alt_mask) + { + input->modifiers |= MOD_ALT_MASK; + } + if (mask & input->xkb.shift_mask) + { + input->modifiers |= MOD_SHIFT_MASK; + } +} + +static void +set_repeat_info(struct input *input, int32_t rate, int32_t delay) +{ + input->repeat_rate_sec = input->repeat_rate_nsec = 0; + input->repeat_delay_sec = input->repeat_delay_nsec = 0; + + /* a rate of zero disables any repeating, regardless of the delay's + * value */ + if (rate == 0) + { + return; + } + + if (rate == 1) + { + input->repeat_rate_sec = 1; + } + else + { + input->repeat_rate_nsec = 1000000000 / rate; + } + + input->repeat_delay_sec = delay / 1000; + delay -= (input->repeat_delay_sec * 1000); + input->repeat_delay_nsec = delay * 1000 * 1000; +} + +static void +keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard, + int32_t rate, int32_t delay) +{ + struct input *input = data; + + set_repeat_info(input, rate, delay); +} + +static const struct wl_keyboard_listener keyboard_listener = { + keyboard_handle_keymap, + keyboard_handle_enter, + keyboard_handle_leave, + keyboard_handle_key, + keyboard_handle_modifiers, + keyboard_handle_repeat_info + +}; + +static void +touch_handle_down(void *data, struct wl_touch *wl_touch, + uint32_t serial, uint32_t time, struct wl_surface *surface, + int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) +{ + struct input *input = data; + struct widget *widget; + float sx = wl_fixed_to_double(x_w); + float sy = wl_fixed_to_double(y_w); + + if (!surface) + { + /* down event for a window we've just destroyed */ + return; + } + + input->display->serial = serial; + input->touch_focus = wl_surface_get_user_data(surface); + if (!input->touch_focus) + { + DBG("Failed to find to touch focus for surface %p\n", surface); + return; + } + + if (surface != input->touch_focus->main_surface->surface) + { + DBG("Ignoring input event from subsurface %p\n", surface); + input->touch_focus = NULL; + return; + } + + if (input->grab) + { + widget = input->grab; + } + else + { + widget = window_find_widget(input->touch_focus, + wl_fixed_to_double(x_w), + wl_fixed_to_double(y_w)); + } + if (widget) + { + struct touch_point *tp = xmalloc(sizeof *tp); + if (tp) + { + tp->id = id; + tp->widget = widget; + tp->x = sx; + tp->y = sy; + wl_list_insert(&input->touch_point_list, &tp->link); + + if (widget->touch_down_handler) + { + (*widget->touch_down_handler)(widget, input, + serial, time, id, + sx, sy, + widget->user_data); + } + } + } +} + +static void +touch_handle_up(void *data, struct wl_touch *wl_touch, + uint32_t serial, uint32_t time, int32_t id) +{ + struct input *input = data; + struct touch_point *tp, *tmp; + + if (!input->touch_focus) + { + DBG("No touch focus found for touch up event!\n"); + return; + } + + wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) + { + if (tp->id != id) + { + continue; + } + + if (tp->widget->touch_up_handler) + { + (*tp->widget->touch_up_handler)(tp->widget, input, serial, + time, id, + tp->widget->user_data); + } + + wl_list_remove(&tp->link); + free(tp); + + return; + } +} + +static void +touch_handle_motion(void *data, struct wl_touch *wl_touch, + uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) +{ + struct input *input = data; + struct touch_point *tp; + float sx = wl_fixed_to_double(x_w); + float sy = wl_fixed_to_double(y_w); + + DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list)); + + if (!input->touch_focus) + { + DBG("No touch focus found for touch motion event!\n"); + return; + } + + wl_list_for_each(tp, &input->touch_point_list, link) + { + if (tp->id != id) + { + continue; + } + + tp->x = sx; + tp->y = sy; + if (tp->widget->touch_motion_handler) + { + (*tp->widget->touch_motion_handler)(tp->widget, input, time, + id, sx, sy, + tp->widget->user_data); + } + return; + } +} + +static void +touch_handle_frame(void *data, struct wl_touch *wl_touch) +{ + struct input *input = data; + struct touch_point *tp, *tmp; + + DBG("touch_handle_frame\n"); + + if (!input->touch_focus) + { + DBG("No touch focus found for touch frame event!\n"); + return; + } + + wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) + { + if (tp->widget->touch_frame_handler) + { + (*tp->widget->touch_frame_handler)(tp->widget, input, + tp->widget->user_data); + } + } +} + +static void +touch_handle_cancel(void *data, struct wl_touch *wl_touch) +{ + struct input *input = data; + struct touch_point *tp, *tmp; + + DBG("touch_handle_cancel\n"); + + if (!input->touch_focus) + { + DBG("No touch focus found for touch cancel event!\n"); + return; + } + + wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) + { + if (tp->widget->touch_cancel_handler) + { + (*tp->widget->touch_cancel_handler)(tp->widget, input, + tp->widget->user_data); + } + + wl_list_remove(&tp->link); + free(tp); + } +} + +static void +touch_handle_shape(void *data, struct wl_touch *wl_touch, int32_t id, + wl_fixed_t major, wl_fixed_t minor) +{} + +static void +touch_handle_orientation(void *data, struct wl_touch *wl_touch, int32_t id, + wl_fixed_t orientation) +{} + +static const struct wl_touch_listener touch_listener = { + touch_handle_down, + touch_handle_up, + touch_handle_motion, + touch_handle_frame, + touch_handle_cancel, + touch_handle_shape, + touch_handle_orientation, +}; + +static void +seat_handle_capabilities(void *data, struct wl_seat *seat, + enum wl_seat_capability caps) +{ + struct input *input = data; + + if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) + { + input->pointer = wl_seat_get_pointer(seat); + wl_pointer_set_user_data(input->pointer, input); + wl_pointer_add_listener(input->pointer, &pointer_listener, + input); + } + else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) + { + if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) + { + wl_pointer_release(input->pointer); + } + else + { + wl_pointer_destroy(input->pointer); + } + input->pointer = NULL; + } + + if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) + { + input->keyboard = wl_seat_get_keyboard(seat); + wl_keyboard_set_user_data(input->keyboard, input); + wl_keyboard_add_listener(input->keyboard, &keyboard_listener, + input); + } + else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) + { + if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION) + { + wl_keyboard_release(input->keyboard); + } + else + { + wl_keyboard_destroy(input->keyboard); + } + input->keyboard = NULL; + } + + if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) + { + input->touch = wl_seat_get_touch(seat); + wl_touch_set_user_data(input->touch, input); + wl_touch_add_listener(input->touch, &touch_listener, input); + } + else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) + { + if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION) + { + wl_touch_release(input->touch); + } + else + { + wl_touch_destroy(input->touch); + } + input->touch = NULL; + } +} + +static void +seat_handle_name(void *data, struct wl_seat *seat, + const char *name) +{} + +static const struct wl_seat_listener seat_listener = { + seat_handle_capabilities, + seat_handle_name +}; + +void +input_get_position(struct input *input, int32_t *x, int32_t *y) +{ + *x = input->sx; + *y = input->sy; +} + +int +input_get_touch(struct input *input, int32_t id, float *x, float *y) +{ + struct touch_point *tp; + + wl_list_for_each(tp, &input->touch_point_list, link) + { + if (tp->id != id) + { + continue; + } + + *x = tp->x; + *y = tp->y; + return 0; + } + + return -1; +} + +struct display * +input_get_display(struct input *input) +{ + return input->display; +} + +struct wl_seat * +input_get_seat(struct input *input) +{ + return input->seat; +} + +uint32_t +input_get_modifiers(struct input *input) +{ + return input->modifiers; +} + +struct widget * +input_get_focus_widget(struct input *input) +{ + return input->focus_widget; +} + +struct data_offer +{ + struct wl_data_offer *offer; + struct input *input; + struct wl_array types; + int refcount; + + struct task io_task; + int fd; + data_func_t func; + int32_t x, y; + uint32_t dnd_action; + uint32_t source_actions; + void *user_data; +}; + +static void +data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type) +{ + struct data_offer *offer = data; + char **p; + + p = wl_array_add(&offer->types, sizeof *p); + *p = strdup(type); +} + +static void +data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions) +{ + struct data_offer *offer = data; + + offer->source_actions = source_actions; +} + +static void +data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action) +{ + struct data_offer *offer = data; + + offer->dnd_action = dnd_action; +} + +static const struct wl_data_offer_listener data_offer_listener = { + data_offer_offer, + data_offer_source_actions, + data_offer_action +}; + +static void +data_offer_destroy(struct data_offer *offer) +{ + char **p; + + offer->refcount--; + if (offer->refcount == 0) + { + wl_data_offer_destroy(offer->offer); + for (p = offer->types.data; *p; p++) + { + free(*p); + } + wl_array_release(&offer->types); + free(offer); + } +} + +static void +data_device_data_offer(void *data, + struct wl_data_device *data_device, + struct wl_data_offer *_offer) +{ + struct data_offer *offer; + + offer = xmalloc(sizeof *offer); + + wl_array_init(&offer->types); + offer->refcount = 1; + offer->input = data; + offer->offer = _offer; + wl_data_offer_add_listener(offer->offer, + &data_offer_listener, offer); +} + +static void +data_device_enter(void *data, struct wl_data_device *data_device, + uint32_t serial, struct wl_surface *surface, + wl_fixed_t x_w, wl_fixed_t y_w, + struct wl_data_offer *offer) +{ + struct input *input = data; + struct window *window; + void *types_data; + float x = wl_fixed_to_double(x_w); + float y = wl_fixed_to_double(y_w); + char **p; + + if (!surface) + { + /* enter event for a window we've just destroyed */ + return; + } + + window = wl_surface_get_user_data(surface); + input->drag_enter_serial = serial; + input->drag_focus = window, + input->drag_x = x; + input->drag_y = y; + + if (!input->touch_grab) + { + input->pointer_enter_serial = serial; + } + + if (offer) + { + input->drag_offer = wl_data_offer_get_user_data(offer); + + p = wl_array_add(&input->drag_offer->types, sizeof *p); + *p = NULL; + + types_data = input->drag_offer->types.data; + + if (input->display->data_device_manager_version >= + WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) + { + wl_data_offer_set_actions(offer, + WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | + WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE, + WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE); + } + } + else + { + input->drag_offer = NULL; + types_data = NULL; + } + + if (window->data_handler) + { + window->data_handler(window, input, x, y, types_data, + window->user_data); + } +} + +static void +data_device_leave(void *data, struct wl_data_device *data_device) +{ + struct input *input = data; + + if (input->drag_offer) + { + data_offer_destroy(input->drag_offer); + input->drag_offer = NULL; + } +} + +static void +data_device_motion(void *data, struct wl_data_device *data_device, + uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w) +{ + struct input *input = data; + struct window *window = input->drag_focus; + float x = wl_fixed_to_double(x_w); + float y = wl_fixed_to_double(y_w); + void *types_data; + + input->drag_x = x; + input->drag_y = y; + + if (input->drag_offer) + { + types_data = input->drag_offer->types.data; + } + else + { + types_data = NULL; + } + + if (window->data_handler) + { + window->data_handler(window, input, x, y, types_data, + window->user_data); + } +} + +static void +data_device_drop(void *data, struct wl_data_device *data_device) +{ + struct input *input = data; + struct window *window = input->drag_focus; + float x, y; + + x = input->drag_x; + y = input->drag_y; + + if (window->drop_handler) + { + window->drop_handler(window, input, + x, y, window->user_data); + } + + if (input->touch_grab) + { + touch_ungrab(input); + } +} + +static void +data_device_selection(void *data, + struct wl_data_device *wl_data_device, + struct wl_data_offer *offer) +{ + struct input *input = data; + char **p; + + if (input->selection_offer) + { + data_offer_destroy(input->selection_offer); + } + + if (offer) + { + input->selection_offer = wl_data_offer_get_user_data(offer); + p = wl_array_add(&input->selection_offer->types, sizeof *p); + *p = NULL; + } + else + { + input->selection_offer = NULL; + } +} + +static const struct wl_data_device_listener data_device_listener = { + data_device_data_offer, + data_device_enter, + data_device_leave, + data_device_motion, + data_device_drop, + data_device_selection +}; + +static void +input_set_pointer_image_index(struct input *input, int index) +{ + struct wl_buffer *buffer; + struct wl_cursor *cursor; + struct wl_cursor_image *image; + + if (!input->pointer) + { + return; + } + + cursor = input->display->cursors[input->current_cursor]; + if (!cursor) + { + return; + } + + if (index >= (int) cursor->image_count) + { + fprintf(stderr, "cursor index out of range\n"); + return; + } + + image = cursor->images[index]; + buffer = wl_cursor_image_get_buffer(image); + if (!buffer) + { + return; + } + + wl_surface_attach(input->pointer_surface, buffer, 0, 0); + wl_surface_damage(input->pointer_surface, 0, 0, + image->width, image->height); + wl_surface_commit(input->pointer_surface); + wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial, + input->pointer_surface, + image->hotspot_x, image->hotspot_y); +} + +static const struct wl_callback_listener pointer_surface_listener; + +static bool +input_set_pointer_special(struct input *input) +{ + if (input->current_cursor == CURSOR_BLANK) + { + wl_pointer_set_cursor(input->pointer, + input->pointer_enter_serial, + NULL, 0, 0); + return true; + } + + if (input->current_cursor == CURSOR_UNSET) + { + return true; + } + + return false; +} + +static void +schedule_pointer_image_update(struct input *input, + struct wl_cursor *cursor, + uint32_t duration, + bool force_frame) +{ + /* Some silly cursor sets have enormous pauses in them. In these + * cases it's better to use a timer even if it results in less + * accurate presentation, since it will save us having to set the + * same cursor image over and over again. + * + * This is really not the way we're supposed to time any kind of + * animation, but we're pretending it's OK here because we don't + * want animated cursors with long delays to needlessly hog CPU. + * + * We use force_frame to ensure we don't accumulate large timing + * errors by running off the wrong clock. + */ + if (!force_frame && (duration > 100)) + { + struct timespec tp; + + clock_gettime(CLOCK_MONOTONIC, &tp); + input->cursor_timer_start = tp.tv_sec * 1000 + + tp.tv_nsec / 1000000; + cursor_delay_timer_reset(input, duration); + return; + } + + /* for short durations we'll just spin on frame callbacks for + * accurate timing - the way any kind of timing sensitive animation + * should really be done. */ + input->cursor_frame_cb = wl_surface_frame(input->pointer_surface); + wl_callback_add_listener(input->cursor_frame_cb, + &pointer_surface_listener, input); + +} + +static void +pointer_surface_frame_callback(void *data, struct wl_callback *callback, + uint32_t time) +{ + struct input *input = data; + struct wl_cursor *cursor; + int i; + uint32_t duration; + bool force_frame = true; + + cancel_pointer_image_update(input); + + if (callback) + { + assert(callback == input->cursor_frame_cb); + wl_callback_destroy(callback); + input->cursor_frame_cb = NULL; + force_frame = false; + } + + if (!input->pointer) + { + return; + } + + if (input_set_pointer_special(input)) + { + return; + } + + cursor = input->display->cursors[input->current_cursor]; + if (!cursor) + { + return; + } + + /* FIXME We don't have the current time on the first call so we set + * the animation start to the time of the first frame callback. */ + if (time == 0) + { + input->cursor_anim_start = 0; + } + else if (input->cursor_anim_start == 0) + { + input->cursor_anim_start = time; + } + + input->cursor_anim_current = time; + + if ((time == 0) || (input->cursor_anim_start == 0)) + { + duration = 0; + i = 0; + } + else + { + i = wl_cursor_frame_and_duration( + cursor, + time - input->cursor_anim_start, + &duration); + } + + if (cursor->image_count > 1) + { + schedule_pointer_image_update(input, cursor, duration, + force_frame); + } + + input_set_pointer_image_index(input, i); +} + +static void +cursor_timer_func(struct toytimer *tt) +{ + struct input *input = container_of(tt, struct input, cursor_timer); + struct timespec tp; + struct wl_cursor *cursor; + uint32_t time; + + if (!input->cursor_timer_running) + { + return; + } + + cursor = input->display->cursors[input->current_cursor]; + if (!cursor) + { + return; + } + + clock_gettime(CLOCK_MONOTONIC, &tp); + time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start; + pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time); +} + +static const struct wl_callback_listener pointer_surface_listener = { + pointer_surface_frame_callback +}; + +void +input_set_pointer_image(struct input *input, int pointer) +{ + int force = 0; + + if (!input->pointer) + { + return; + } + + if (input->pointer_enter_serial > input->cursor_serial) + { + force = 1; + } + + if (!force && (pointer == input->current_cursor)) + { + return; + } + + input->current_cursor = pointer; + input->cursor_serial = input->pointer_enter_serial; + if (!input->cursor_frame_cb) + { + pointer_surface_frame_callback(input, NULL, 0); + } + else if (force && !input_set_pointer_special(input)) + { + /* The current frame callback may be stuck if, for instance, + * the set cursor request was processed by the server after + * this client lost the focus. In this case the cursor surface + * might not be mapped and the frame callback wouldn't ever + * complete. Send a set_cursor and attach to try to map the + * cursor surface again so that the callback will finish */ + input_set_pointer_image_index(input, 0); + } +} + +struct wl_data_device * +input_get_data_device(struct input *input) +{ + return input->data_device; +} + +void +input_set_selection(struct input *input, + struct wl_data_source *source, uint32_t time) +{ + if (input->data_device) + { + wl_data_device_set_selection(input->data_device, source, time); + } +} + +void +input_accept(struct input *input, const char *type) +{ + wl_data_offer_accept(input->drag_offer->offer, + input->drag_enter_serial, type); +} + +static void +offer_io_func(struct task *task, uint32_t events) +{ + struct data_offer *offer = + container_of(task, struct data_offer, io_task); + struct display *display = offer->input->display; + unsigned int len; + char buffer[4096]; + + len = read(offer->fd, buffer, sizeof buffer); + offer->func(buffer, len, + offer->x, offer->y, offer->user_data); + + if (len == 0) + { + if ((offer != offer->input->selection_offer) && + (display->data_device_manager_version >= + WL_DATA_OFFER_FINISH_SINCE_VERSION)) + { + wl_data_offer_finish(offer->offer); + } + close(offer->fd); + data_offer_destroy(offer); + } +} + +static void +data_offer_receive_data(struct data_offer *offer, const char *mime_type, + data_func_t func, void *user_data) +{ + int p[2]; + + if (pipe2(p, O_CLOEXEC) == -1) + { + return; + } + + wl_data_offer_receive(offer->offer, mime_type, p[1]); + close(p[1]); + + offer->io_task.run = offer_io_func; + offer->fd = p[0]; + offer->func = func; + offer->refcount++; + offer->user_data = user_data; + + display_watch_fd(offer->input->display, + offer->fd, EPOLLIN, &offer->io_task); +} + +void +input_receive_drag_data(struct input *input, const char *mime_type, + data_func_t func, void *data) +{ + data_offer_receive_data(input->drag_offer, mime_type, func, data); + input->drag_offer->x = input->drag_x; + input->drag_offer->y = input->drag_y; +} + +int +input_receive_drag_data_to_fd(struct input *input, + const char *mime_type, int fd) +{ + if (input->drag_offer) + { + wl_data_offer_receive(input->drag_offer->offer, mime_type, fd); + } + + return 0; +} + +int +input_receive_selection_data(struct input *input, const char *mime_type, + data_func_t func, void *data) +{ + char **p; + + if (input->selection_offer == NULL) + { + return -1; + } + + for (p = input->selection_offer->types.data; *p; p++) + { + if (strcmp(mime_type, *p) == 0) + { + break; + } + } + + if (*p == NULL) + { + return -1; + } + + data_offer_receive_data(input->selection_offer, + mime_type, func, data); + return 0; +} + +int +input_receive_selection_data_to_fd(struct input *input, + const char *mime_type, int fd) +{ + if (input->selection_offer) + { + wl_data_offer_receive(input->selection_offer->offer, + mime_type, fd); + } + + return 0; +} + +void +window_move(struct window *window, struct input *input, uint32_t serial) +{ + if (!window->xdg_toplevel) + { + return; + } + + xdg_toplevel_move(window->xdg_toplevel, input->seat, serial); +} + +static void +surface_set_synchronized(struct surface *surface) +{ + if (!surface->subsurface) + { + return; + } + + if (surface->synchronized) + { + return; + } + + wl_subsurface_set_sync(surface->subsurface); + surface->synchronized = 1; +} + +static void +surface_set_synchronized_default(struct surface *surface) +{ + if (!surface->subsurface) + { + return; + } + + if (surface->synchronized == surface->synchronized_default) + { + return; + } + + if (surface->synchronized_default) + { + wl_subsurface_set_sync(surface->subsurface); + } + else + { + wl_subsurface_set_desync(surface->subsurface); + } + + surface->synchronized = surface->synchronized_default; +} + +static void +surface_resize(struct surface *surface) +{ + struct widget *widget = surface->widget; + struct wl_compositor *compositor = widget->window->display->compositor; + + if (surface->input_region) + { + wl_region_destroy(surface->input_region); + surface->input_region = NULL; + } + + if (surface->opaque_region) + { + wl_region_destroy(surface->opaque_region); + } + + surface->opaque_region = wl_compositor_create_region(compositor); + + if (widget->resize_handler) + { + widget->resize_handler(widget, + widget->allocation.width, + widget->allocation.height, + widget->user_data); + } + + if (surface->subsurface && + ((surface->allocation.x != widget->allocation.x) || + (surface->allocation.y != widget->allocation.y))) + { + wl_subsurface_set_position(surface->subsurface, + widget->allocation.x, + widget->allocation.y); + } + if ((surface->allocation.width != widget->allocation.width) || + (surface->allocation.height != widget->allocation.height)) + { + window_schedule_redraw(widget->window); + } + surface->allocation = widget->allocation; + + if (widget->opaque) + { + wl_region_add(surface->opaque_region, 0, 0, + widget->allocation.width, + widget->allocation.height); + } +} + +static void +window_do_resize(struct window *window) +{ + struct surface *surface; + + widget_set_allocation(window->main_surface->widget, + window->pending_allocation.x, + window->pending_allocation.y, + window->pending_allocation.width, + window->pending_allocation.height); + + surface_resize(window->main_surface); + + /* The main surface is in the list, too. Main surface's + * resize_handler is responsible for calling widget_set_allocation() + * on all sub-surface root widgets, so they will be resized + * properly. + */ + wl_list_for_each(surface, &window->subsurface_list, link) + { + if (surface == window->main_surface) + { + continue; + } + + surface_set_synchronized(surface); + surface_resize(surface); + } + + if (!window->fullscreen && !window->maximized) + { + window->saved_allocation = window->pending_allocation; + } + + if (window->confined && window->confined_widget) + { + struct wl_compositor *compositor = window->display->compositor; + struct wl_region *region; + struct widget *widget = window->confined_widget; + + region = wl_compositor_create_region(compositor); + wl_region_add(region, + widget->allocation.x, + widget->allocation.y, + widget->allocation.width, + widget->allocation.height); + zwp_confined_pointer_v1_set_region(window->confined_pointer, + region); + wl_region_destroy(region); + } +} + +static void +idle_resize(struct window *window) +{ + window->resize_needed = 0; + window->redraw_needed = 1; + + DBG("from %dx%d to %dx%d\n", + window->main_surface->server_allocation.width, + window->main_surface->server_allocation.height, + window->pending_allocation.width, + window->pending_allocation.height); + + window_do_resize(window); +} + +static void +undo_resize(struct window *window) +{ + window->pending_allocation.width = + window->main_surface->server_allocation.width; + window->pending_allocation.height = + window->main_surface->server_allocation.height; + + DBG("back to %dx%d\n", + window->main_surface->server_allocation.width, + window->main_surface->server_allocation.height); + + window_do_resize(window); + + if ((window->pending_allocation.width == 0) && + (window->pending_allocation.height == 0)) + { + fprintf(stderr, "Error: Could not draw a surface, " + "most likely due to insufficient disk space in " + "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR")); + exit(EXIT_FAILURE); + } +} + +void +window_schedule_resize(struct window *window, int width, int height) +{ + /* We should probably get these numbers from the theme. */ + const int min_width = 200, min_height = 200; + + window->pending_allocation.x = 0; + window->pending_allocation.y = 0; + window->pending_allocation.width = width; + window->pending_allocation.height = height; + + if (window->min_allocation.width == 0) + { + if ((width < min_width) && window->frame) + { + window->min_allocation.width = min_width; + } + else + { + window->min_allocation.width = width; + } + if ((height < min_height) && window->frame) + { + window->min_allocation.height = min_height; + } + else + { + window->min_allocation.height = height; + } + } + + if (window->pending_allocation.width < window->min_allocation.width) + { + window->pending_allocation.width = window->min_allocation.width; + } + if (window->pending_allocation.height < window->min_allocation.height) + { + window->pending_allocation.height = window->min_allocation.height; + } + + window->resize_needed = 1; + window_schedule_redraw(window); +} + +void +widget_schedule_resize(struct widget *widget, int32_t width, int32_t height) +{ + window_schedule_resize(widget->window, width, height); +} + +static int +window_get_shadow_margin(struct window *window) +{ + if (window->frame && !window->fullscreen) + { + return frame_get_shadow_margin(window->frame->frame); + } + else + { + return 0; + } +} + +void +window_inhibit_redraw(struct window *window) +{ + window->redraw_inhibited = 1; + wl_list_remove(&window->redraw_task.link); + wl_list_init(&window->redraw_task.link); + window->redraw_task_scheduled = 0; +} + +void +window_uninhibit_redraw(struct window *window) +{ + window->redraw_inhibited = 0; + if (window->redraw_needed || window->resize_needed) + { + window_schedule_redraw_task(window); + } +} + +static void +xdg_surface_handle_configure(void *data, + struct xdg_surface *xdg_surface, + uint32_t serial) +{ + struct window *window = data; + + xdg_surface_ack_configure(window->xdg_surface, serial); + + if (window->state_changed_handler) + { + window->state_changed_handler(window, window->user_data); + } + + window_uninhibit_redraw(window); +} + +static const struct xdg_surface_listener xdg_surface_listener = { + xdg_surface_handle_configure +}; + +static void +xdg_toplevel_handle_configure(void *data, struct xdg_toplevel *xdg_toplevel, + int32_t width, int32_t height, + struct wl_array *states) +{ + struct window *window = data; + uint32_t *p; + + window->maximized = 0; + window->fullscreen = 0; + window->resizing = 0; + window->focused = 0; + + wl_array_for_each(p, states) + { + uint32_t state = *p; + switch (state) + { + case XDG_TOPLEVEL_STATE_MAXIMIZED: + window->maximized = 1; + break; + + case XDG_TOPLEVEL_STATE_FULLSCREEN: + window->fullscreen = 1; + break; + + case XDG_TOPLEVEL_STATE_RESIZING: + window->resizing = 1; + break; + + case XDG_TOPLEVEL_STATE_ACTIVATED: + window->focused = 1; + break; + + default: + /* Unknown state */ + break; + } + } + + if (window->frame) + { + if (window->maximized) + { + frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED); + } + else + { + frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED); + } + + if (window->focused) + { + frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE); + } + else + { + frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE); + } + } + + if ((width > 0) && (height > 0)) + { + /* The width / height params are for window geometry, + * but window_schedule_resize takes allocation. Add + * on the shadow margin to get the difference. */ + int margin = window_get_shadow_margin(window); + + window_schedule_resize(window, + width + margin * 2, + height + margin * 2); + } + else if ((window->saved_allocation.width > 0) && + (window->saved_allocation.height > 0)) + { + window_schedule_resize(window, + window->saved_allocation.width, + window->saved_allocation.height); + } +} + +static void +xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_surface) +{ + struct window *window = data; + window_close(window); +} + +static const struct xdg_toplevel_listener xdg_toplevel_listener = { + xdg_toplevel_handle_configure, + xdg_toplevel_handle_close, +}; + +static void +window_sync_parent(struct window *window) +{ + struct xdg_toplevel *parent_toplevel; + + if (!window->xdg_surface) + { + return; + } + + if (window->parent == window->last_parent) + { + return; + } + + if (window->parent) + { + parent_toplevel = window->parent->xdg_toplevel; + } + else + { + parent_toplevel = NULL; + } + + xdg_toplevel_set_parent(window->xdg_toplevel, parent_toplevel); + window->last_parent = window->parent; +} + +static void +window_get_geometry(struct window *window, struct rectangle *geometry) +{ + if (window->frame && !window->fullscreen) + { + frame_input_rect(window->frame->frame, + &geometry->x, + &geometry->y, + &geometry->width, + &geometry->height); + } + else + { + window_get_allocation(window, geometry); + } +} + +static void +window_sync_geometry(struct window *window) +{ + struct rectangle geometry; + + if (!window->xdg_surface) + { + return; + } + + window_get_geometry(window, &geometry); + if ((geometry.x == window->last_geometry.x) && + (geometry.y == window->last_geometry.y) && + (geometry.width == window->last_geometry.width) && + (geometry.height == window->last_geometry.height)) + { + return; + } + + xdg_surface_set_window_geometry(window->xdg_surface, + geometry.x, + geometry.y, + geometry.width, + geometry.height); + window->last_geometry = geometry; +} + +static void +window_flush(struct window *window) +{ + struct surface *surface; + + assert(!window->redraw_inhibited); + + if (!window->custom) + { + if (window->xdg_surface) + { + window_sync_geometry(window); + } + if (window->xdg_toplevel) + { + window_sync_parent(window); + } + } + + wl_list_for_each(surface, &window->subsurface_list, link) + { + if (surface == window->main_surface) + { + continue; + } + + surface_flush(surface); + } + + surface_flush(window->main_surface); +} + +static void +menu_destroy(struct menu *menu) +{ + widget_destroy(menu->widget); + window_destroy(menu->window); + frame_destroy(menu->frame); + free(menu); +} + +void +window_get_allocation(struct window *window, + struct rectangle *allocation) +{ + *allocation = window->main_surface->allocation; +} + +static void +widget_redraw(struct widget *widget) +{ + struct widget *child; + + if (widget->redraw_handler) + { + widget->redraw_handler(widget, widget->user_data); + } + wl_list_for_each(child, &widget->child_list, link) + widget_redraw(child); +} + +static void +frame_callback(void *data, struct wl_callback *callback, uint32_t time) +{ + struct surface *surface = data; + + assert(callback == surface->frame_cb); + DBG_OBJ(callback, "done\n"); + wl_callback_destroy(callback); + surface->frame_cb = NULL; + + surface->last_time = time; + + if (surface->redraw_needed || surface->window->redraw_needed) + { + DBG_OBJ(surface->surface, "window_schedule_redraw_task\n"); + window_schedule_redraw_task(surface->window); + } +} + +static const struct wl_callback_listener listener = { + frame_callback +}; + +static int +surface_redraw(struct surface *surface) +{ + DBG_OBJ(surface->surface, "begin\n"); + + if (!surface->window->redraw_needed && !surface->redraw_needed) + { + return 0; + } + + /* Whole-window redraw forces a redraw even if the previous has + * not yet hit the screen. + */ + if (surface->frame_cb) + { + if (!surface->window->redraw_needed) + { + return 0; + } + + DBG_OBJ(surface->frame_cb, "cancelled\n"); + wl_callback_destroy(surface->frame_cb); + } + + if (surface->widget->use_cairo && + !widget_get_cairo_surface(surface->widget)) + { + DBG_OBJ(surface->surface, "cancelled due to buffer failure\n"); + return -1; + } + + surface->frame_cb = wl_surface_frame(surface->surface); + wl_callback_add_listener(surface->frame_cb, &listener, surface); + DBG_OBJ(surface->frame_cb, "new\n"); + + surface->redraw_needed = 0; + DBG_OBJ(surface->surface, "-> widget_redraw\n"); + widget_redraw(surface->widget); + DBG_OBJ(surface->surface, "done\n"); + return 0; +} + +static void +idle_redraw(struct task *task, uint32_t events) +{ + struct window *window = container_of(task, struct window, redraw_task); + struct surface *surface; + int failed = 0; + int resized = 0; + + DBG(" --------- \n"); + + wl_list_init(&window->redraw_task.link); + window->redraw_task_scheduled = 0; + + if (window->resize_needed) + { + /* throttle resizing to the main surface display */ + if (window->main_surface->frame_cb) + { + DBG_OBJ(window->main_surface->frame_cb, "pending\n"); + return; + } + + idle_resize(window); + resized = 1; + } + + if (surface_redraw(window->main_surface) < 0) + { + /* + * Only main_surface failure will cause us to undo the resize. + * If sub-surfaces fail, they will just be broken with old + * content. + */ + failed = 1; + } + else + { + wl_list_for_each(surface, &window->subsurface_list, link) + { + if (surface == window->main_surface) + { + continue; + } + + surface_redraw(surface); + } + } + + window->redraw_needed = 0; + window_flush(window); + + wl_list_for_each(surface, &window->subsurface_list, link) + surface_set_synchronized_default(surface); + + if (resized && failed) + { + /* Restore widget tree to correspond to what is on screen. */ + undo_resize(window); + } +} + +static void +window_schedule_redraw_task(struct window *window) +{ + if (window->redraw_inhibited) + { + return; + } + + if (!window->redraw_task_scheduled) + { + window->redraw_task.run = idle_redraw; + display_defer(window->display, &window->redraw_task); + window->redraw_task_scheduled = 1; + } +} + +void +window_schedule_redraw(struct window *window) +{ + struct surface *surface; + + DBG_OBJ(window->main_surface->surface, "window %p\n", window); + + wl_list_for_each(surface, &window->subsurface_list, link) + surface->redraw_needed = 1; + + window_schedule_redraw_task(window); +} + +int +window_is_fullscreen(struct window *window) +{ + return window->fullscreen; +} + +void +window_set_fullscreen(struct window *window, int fullscreen) +{ + if (!window->xdg_toplevel) + { + return; + } + + if (window->fullscreen == fullscreen) + { + return; + } + + if (fullscreen) + { + xdg_toplevel_set_fullscreen(window->xdg_toplevel, NULL); + } + else + { + xdg_toplevel_unset_fullscreen(window->xdg_toplevel); + } +} + +int +window_is_maximized(struct window *window) +{ + return window->maximized; +} + +void +window_set_maximized(struct window *window, int maximized) +{ + if (!window->xdg_toplevel) + { + return; + } + + if (window->maximized == maximized) + { + return; + } + + if (maximized) + { + xdg_toplevel_set_maximized(window->xdg_toplevel); + } + else + { + xdg_toplevel_unset_maximized(window->xdg_toplevel); + } +} + +int +window_is_resizing(struct window *window) +{ + return window->resizing; +} + +void +window_set_minimized(struct window *window) +{ + if (!window->xdg_toplevel) + { + return; + } + + xdg_toplevel_set_minimized(window->xdg_toplevel); +} + +void +window_set_user_data(struct window *window, void *data) +{ + window->user_data = data; +} + +void * +window_get_user_data(struct window *window) +{ + return window->user_data; +} + +void +window_set_key_handler(struct window *window, + window_key_handler_t handler) +{ + window->key_handler = handler; +} + +void +window_set_keyboard_focus_handler(struct window *window, + window_keyboard_focus_handler_t handler) +{ + window->keyboard_focus_handler = handler; +} + +void +window_set_data_handler(struct window *window, window_data_handler_t handler) +{ + window->data_handler = handler; +} + +void +window_set_drop_handler(struct window *window, window_drop_handler_t handler) +{ + window->drop_handler = handler; +} + +void +window_set_close_handler(struct window *window, + window_close_handler_t handler) +{ + window->close_handler = handler; +} + +void +window_set_fullscreen_handler(struct window *window, + window_fullscreen_handler_t handler) +{ + window->fullscreen_handler = handler; +} + +void +window_set_output_handler(struct window *window, + window_output_handler_t handler) +{ + window->output_handler = handler; +} + +void +window_set_state_changed_handler(struct window *window, + window_state_changed_handler_t handler) +{ + window->state_changed_handler = handler; +} + +void +window_set_pointer_locked_handler(struct window *window, + locked_pointer_locked_handler_t locked, + locked_pointer_unlocked_handler_t unlocked) +{ + window->pointer_unlocked_handler = unlocked; + window->pointer_locked_handler = locked; +} + +void +window_set_pointer_confined_handler(struct window *window, + confined_pointer_confined_handler_t confined, + confined_pointer_unconfined_handler_t unconfined) +{ + window->pointer_confined_handler = confined; + window->pointer_unconfined_handler = unconfined; +} + +void +window_set_locked_pointer_motion_handler(struct window *window, + window_locked_pointer_motion_handler_t handler) +{ + window->locked_pointer_motion_handler = handler; +} + +void +window_set_title(struct window *window, const char *title) +{ + free(window->title); + window->title = strdup(title); + if (window->frame) + { + frame_set_title(window->frame->frame, title); + widget_schedule_redraw(window->frame->widget); + } + if (window->xdg_toplevel) + { + xdg_toplevel_set_title(window->xdg_toplevel, title); + } +} + +const char * +window_get_title(struct window *window) +{ + return window->title; +} + +void +window_set_text_cursor_position(struct window *window, int32_t x, int32_t y) +{ + struct text_cursor_position *text_cursor_position = + window->display->text_cursor_position; + + if (!text_cursor_position) + { + return; + } + + text_cursor_position_notify(text_cursor_position, + window->main_surface->surface, + wl_fixed_from_int(x), + wl_fixed_from_int(y)); +} + +static void +relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer, + uint32_t utime_hi, + uint32_t utime_lo, + wl_fixed_t dx, + wl_fixed_t dy, + wl_fixed_t dx_unaccel, + wl_fixed_t dy_unaccel) +{ + struct input *input = data; + struct window *window = input->pointer_focus; + uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000; + + if (window->locked_pointer_motion_handler && + window->pointer_locked) + { + window->locked_pointer_motion_handler( + window, input, ms, + wl_fixed_to_double(dx), + wl_fixed_to_double(dy), + window->user_data); + } +} + +static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = { + relative_pointer_handle_motion, +}; + +static void +locked_pointer_locked(void *data, + struct zwp_locked_pointer_v1 *locked_pointer) +{ + struct input *input = data; + struct window *window = input->locked_window; + + if (!window) + { + return; + } + + window->pointer_locked = true; + + if (window->pointer_locked_handler) + { + window->pointer_locked_handler(window, + input, + window->user_data); + } +} + +static void +locked_pointer_unlocked(void *data, + struct zwp_locked_pointer_v1 *locked_pointer) +{ + struct input *input = data; + struct window *window = input->locked_window; + + if (!window) + { + return; + } + + window_unlock_pointer(window); + + input->locked_window = NULL; + + if (window->pointer_unlocked_handler) + { + window->pointer_unlocked_handler(window, + input, + window->user_data); + } +} + +static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = { + locked_pointer_locked, + locked_pointer_unlocked, +}; + +int +window_lock_pointer(struct window *window, struct input *input) +{ + struct zwp_relative_pointer_manager_v1 *relative_pointer_manager = + window->display->relative_pointer_manager; + struct zwp_pointer_constraints_v1 *pointer_constraints = + window->display->pointer_constraints; + struct zwp_relative_pointer_v1 *relative_pointer; + struct zwp_locked_pointer_v1 *locked_pointer; + + if (!window->display->relative_pointer_manager) + { + return -1; + } + + if (!window->display->pointer_constraints) + { + return -1; + } + + if (window->locked_pointer) + { + return -1; + } + + if (window->confined_pointer) + { + return -1; + } + + if (!input->pointer) + { + return -1; + } + + relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer( + relative_pointer_manager, input->pointer); + zwp_relative_pointer_v1_add_listener(relative_pointer, + &relative_pointer_listener, + input); + + locked_pointer = + zwp_pointer_constraints_v1_lock_pointer(pointer_constraints, + window->main_surface->surface, + input->pointer, + NULL, + ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT); + zwp_locked_pointer_v1_add_listener(locked_pointer, + &locked_pointer_listener, + input); + + window->locked_pointer = locked_pointer; + window->relative_pointer = relative_pointer; + input->locked_window = window; + + return 0; +} + +void +window_unlock_pointer(struct window *window) +{ + if (!window->locked_pointer) + { + return; + } + + zwp_locked_pointer_v1_destroy(window->locked_pointer); + zwp_relative_pointer_v1_destroy(window->relative_pointer); + window->locked_pointer = NULL; + window->relative_pointer = NULL; + window->pointer_locked = false; +} + +void +widget_set_locked_pointer_cursor_hint(struct widget *widget, + float x, float y) +{ + struct window *window = widget->window; + + if (!window->locked_pointer) + { + return; + } + + zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer, + wl_fixed_from_double(x), + wl_fixed_from_double(y)); + wl_surface_commit(window->main_surface->surface); +} + +static void +confined_pointer_confined(void *data, + struct zwp_confined_pointer_v1 *confined_pointer) +{ + struct input *input = data; + struct window *window = input->confined_window; + + if (!window) + { + return; + } + + window->confined = true; + + if (window->pointer_confined_handler) + { + window->pointer_confined_handler(window, + input, + window->user_data); + } +} + +static void +confined_pointer_unconfined(void *data, + struct zwp_confined_pointer_v1 *confined_pointer) +{ + struct input *input = data; + struct window *window = input->confined_window; + + if (!window) + { + return; + } + + window_unconfine_pointer(window); + + window->confined = false; + input->confined_window = NULL; + + if (window->pointer_unconfined_handler) + { + window->pointer_unconfined_handler(window, + input, + window->user_data); + } +} + +static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = { + confined_pointer_confined, + confined_pointer_unconfined, +}; + +int +window_confine_pointer_to_rectangles(struct window *window, + struct input *input, + struct rectangle *rectangles, + int num_rectangles) +{ + struct zwp_pointer_constraints_v1 *pointer_constraints = + window->display->pointer_constraints; + struct zwp_confined_pointer_v1 *confined_pointer; + struct wl_compositor *compositor = window->display->compositor; + struct wl_region *region = NULL; + int i; + + if (!window->display->pointer_constraints) + { + return -1; + } + + if (window->locked_pointer) + { + return -1; + } + + if (window->confined_pointer) + { + return -1; + } + + if (!input->pointer) + { + return -1; + } + + if (num_rectangles >= 1) + { + region = wl_compositor_create_region(compositor); + for (i = 0; i < num_rectangles; i++) + { + wl_region_add(region, + rectangles[i].x, + rectangles[i].y, + rectangles[i].width, + rectangles[i].height); + } + } + + confined_pointer = + zwp_pointer_constraints_v1_confine_pointer(pointer_constraints, + window->main_surface->surface, + input->pointer, + region, + ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT); + if (region) + { + wl_region_destroy(region); + } + + zwp_confined_pointer_v1_add_listener(confined_pointer, + &confined_pointer_listener, + input); + + window->confined_pointer = confined_pointer; + window->confined_widget = NULL; + input->confined_window = window; + + return 0; +} + +void +window_update_confine_rectangles(struct window *window, + struct rectangle *rectangles, + int num_rectangles) +{ + struct wl_compositor *compositor = window->display->compositor; + struct wl_region *region; + int i; + + region = wl_compositor_create_region(compositor); + for (i = 0; i < num_rectangles; i++) + { + wl_region_add(region, + rectangles[i].x, + rectangles[i].y, + rectangles[i].width, + rectangles[i].height); + } + + zwp_confined_pointer_v1_set_region(window->confined_pointer, region); + + wl_region_destroy(region); +} + +int +window_confine_pointer_to_widget(struct window *window, + struct widget *widget, + struct input *input) +{ + int ret; + + if (widget) + { + ret = window_confine_pointer_to_rectangles(window, + input, + &widget->allocation, + 1); + window->confined_widget = widget; + return ret; + } + else + { + return window_confine_pointer_to_rectangles(window, + input, + NULL, + 0); + } +} + +void +window_unconfine_pointer(struct window *window) +{ + if (!window->confined_pointer) + { + return; + } + + zwp_confined_pointer_v1_destroy(window->confined_pointer); + window->confined_pointer = NULL; + window->confined = false; +} + +static void +surface_enter(void *data, + struct wl_surface *wl_surface, struct wl_output *wl_output) +{ + struct window *window = data; + struct output *output; + struct output *output_found = NULL; + struct window_output *window_output; + + wl_list_for_each(output, &window->display->output_list, link) + { + if (output->output == wl_output) + { + output_found = output; + break; + } + } + + if (!output_found) + { + return; + } + + window_output = xmalloc(sizeof *window_output); + window_output->output = output_found; + + wl_list_insert(&window->window_output_list, &window_output->link); + + if (window->output_handler) + { + window->output_handler(window, output_found, 1, + window->user_data); + } +} + +static void +surface_leave(void *data, + struct wl_surface *wl_surface, struct wl_output *output) +{ + struct window *window = data; + struct window_output *window_output; + struct window_output *window_output_found = NULL; + + wl_list_for_each(window_output, &window->window_output_list, link) + { + if (window_output->output->output == output) + { + window_output_found = window_output; + break; + } + } + + if (window_output_found) + { + wl_list_remove(&window_output_found->link); + + if (window->output_handler) + { + window->output_handler(window, window_output->output, + 0, window->user_data); + } + + free(window_output_found); + } +} + +static const struct wl_surface_listener surface_listener = { + surface_enter, + surface_leave +}; + +static struct surface * +surface_create(struct window *window) +{ + struct display *display = window->display; + struct surface *surface; + + surface = xzalloc(sizeof *surface); + surface->window = window; + surface->surface = wl_compositor_create_surface(display->compositor); + surface->buffer_scale = 1; + wl_surface_add_listener(surface->surface, &surface_listener, window); + + wl_list_insert(&window->subsurface_list, &surface->link); + surface->viewport = NULL; + + return surface; +} + +static enum window_buffer_type +get_preferred_buffer_type(struct display *display) +{ +#ifdef HAVE_CAIRO_EGL + if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL")) + { + return WINDOW_BUFFER_TYPE_EGL_WINDOW; + } +#endif + + return WINDOW_BUFFER_TYPE_SHM; +} + +static struct window * +window_create_internal(struct display *display, int custom) +{ + struct window *window; + struct surface *surface; + + window = xzalloc(sizeof *window); + wl_list_init(&window->subsurface_list); + window->display = display; + + surface = surface_create(window); + window->main_surface = surface; + + assert(custom || display->xdg_shell); + + window->custom = custom; + + surface->buffer_type = get_preferred_buffer_type(display); + + wl_surface_set_user_data(surface->surface, window); + wl_list_insert(display->window_list.prev, &window->link); + wl_list_init(&window->redraw_task.link); + + wl_list_init(&window->window_output_list); + + return window; +} + +struct window * +window_create(struct display *display) +{ + struct window *window; + + window = window_create_internal(display, 0); + + if (window->display->xdg_shell) + { + window->xdg_surface = + xdg_wm_base_get_xdg_surface(window->display->xdg_shell, + window->main_surface->surface); + fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__); + + xdg_surface_add_listener(window->xdg_surface, + &xdg_surface_listener, window); + + window->xdg_toplevel = + xdg_surface_get_toplevel(window->xdg_surface); + fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__); + + xdg_toplevel_add_listener(window->xdg_toplevel, + &xdg_toplevel_listener, window); + + window_inhibit_redraw(window); + + wl_surface_commit(window->main_surface->surface); + } + + return window; +} + +struct window * +window_create_custom(struct display *display) +{ + return window_create_internal(display, 1); +} + +void +window_set_parent(struct window *window, + struct window *parent_window) +{ + window->parent = parent_window; + window_sync_parent(window); +} + +struct window * +window_get_parent(struct window *window) +{ + return window->parent; +} + +static void +menu_set_item(struct menu *menu, int sy) +{ + int32_t x, y, width, height; + int next; + + frame_interior(menu->frame, &x, &y, &width, &height); + next = (sy - y) / 20; + if (menu->current != next) + { + menu->current = next; + widget_schedule_redraw(menu->widget); + } +} + +static int +menu_motion_handler(struct widget *widget, + struct input *input, uint32_t time, + float x, float y, void *data) +{ + struct menu *menu = data; + + if (widget == menu->widget) + { + menu_set_item(data, y); + } + + return CURSOR_LEFT_PTR; +} + +static int +menu_enter_handler(struct widget *widget, + struct input *input, float x, float y, void *data) +{ + struct menu *menu = data; + + if (widget == menu->widget) + { + menu_set_item(data, y); + } + + return CURSOR_LEFT_PTR; +} + +static void +menu_leave_handler(struct widget *widget, struct input *input, void *data) +{ + struct menu *menu = data; + + if (widget == menu->widget) + { + menu_set_item(data, -200); + } +} + +static void +menu_button_handler(struct widget *widget, + struct input *input, uint32_t time, + uint32_t button, enum wl_pointer_button_state state, + void *data) +{ + struct menu *menu = data; + + if ((state == WL_POINTER_BUTTON_STATE_RELEASED) && + ((menu->release_count > 0) || (time - menu->time > 500))) + { + /* Either release after press-drag-release or + * click-motion-click. */ + menu->func(menu->user_data, input, menu->current); + input_ungrab(menu->input); + menu_destroy(menu); + } + else if (state == WL_POINTER_BUTTON_STATE_RELEASED) + { + menu->release_count++; + } +} + +static void +menu_touch_up_handler(struct widget *widget, + struct input *input, + uint32_t serial, + uint32_t time, + int32_t id, + void *data) +{ + struct menu *menu = data; + + input_ungrab(input); + menu_destroy(menu); +} + +static void +menu_redraw_handler(struct widget *widget, void *data) +{ + cairo_t *cr; + struct menu *menu = data; + int32_t x, y, width, height, i; + + cr = widget_cairo_create(widget); + + frame_repaint(menu->frame, cr); + frame_interior(menu->frame, &x, &y, &width, &height); + + theme_set_background_source(menu->window->display->theme, + cr, THEME_FRAME_ACTIVE); + cairo_rectangle(cr, x, y, width, height); + cairo_fill(cr); + + cairo_select_font_face(cr, "sans", + CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cr, 12); + + for (i = 0; i < menu->count; i++) + { + if (i == menu->current) + { + cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); + cairo_rectangle(cr, x, y + i * 20, width, 20); + cairo_fill(cr); + cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); + cairo_move_to(cr, x + 10, y + i * 20 + 16); + cairo_show_text(cr, menu->entries[i]); + } + else + { + cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); + cairo_move_to(cr, x + 10, y + i * 20 + 16); + cairo_show_text(cr, menu->entries[i]); + } + } + + cairo_destroy(cr); +} + +static void +xdg_popup_handle_configure(void *data, + struct xdg_popup *xdg_popup, + int32_t x, + int32_t y, + int32_t width, + int32_t height) +{} + +static void +xdg_popup_handle_popup_done(void *data, struct xdg_popup *xdg_popup) +{ + struct window *window = data; + struct menu *menu = window->main_surface->widget->user_data; + + input_ungrab(menu->input); + menu_destroy(menu); +} + +static const struct xdg_popup_listener xdg_popup_listener = { + xdg_popup_handle_configure, + xdg_popup_handle_popup_done, +}; + +static struct menu * +create_menu(struct display *display, + struct input *input, uint32_t time, + menu_func_t func, const char **entries, int count, + void *user_data) +{ + struct window *window; + struct menu *menu; + + menu = malloc(sizeof *menu); + if (!menu) + { + return NULL; + } + + window = window_create_internal(display, 0); + if (!window) + { + free(menu); + return NULL; + } + + menu->window = window; + menu->user_data = user_data; + menu->widget = window_add_widget(menu->window, menu); + menu->frame = frame_create(window->display->theme, 0, 0, + FRAME_BUTTON_NONE, NULL, NULL); + fail_on_null(menu->frame, 0, __FILE__, __LINE__); + menu->entries = entries; + menu->count = count; + menu->release_count = 0; + menu->current = -1; + menu->time = time; + menu->func = func; + menu->input = input; + + input_ungrab(input); + + widget_set_redraw_handler(menu->widget, menu_redraw_handler); + widget_set_enter_handler(menu->widget, menu_enter_handler); + widget_set_leave_handler(menu->widget, menu_leave_handler); + widget_set_motion_handler(menu->widget, menu_motion_handler); + widget_set_button_handler(menu->widget, menu_button_handler); + widget_set_touch_up_handler(menu->widget, menu_touch_up_handler); + + input_grab(input, menu->widget, 0); + frame_resize_inside(menu->frame, 200, count * 20); + frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE); + window_schedule_resize(window, frame_width(menu->frame), + frame_height(menu->frame)); + + return menu; +} + +static struct xdg_positioner * +create_simple_positioner(struct display *display, + int x, int y, int w, int h) +{ + struct xdg_positioner *positioner; + + positioner = xdg_wm_base_create_positioner(display->xdg_shell); + fail_on_null(positioner, 0, __FILE__, __LINE__); + xdg_positioner_set_anchor_rect(positioner, x, y, 1, 1); + xdg_positioner_set_size(positioner, w, h); + xdg_positioner_set_anchor(positioner, + XDG_POSITIONER_ANCHOR_TOP_LEFT); + xdg_positioner_set_gravity(positioner, + XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT); + + return positioner; +} + +void +window_show_menu(struct display *display, + struct input *input, uint32_t time, struct window *parent, + int32_t x, int32_t y, + menu_func_t func, const char **entries, int count) +{ + struct menu *menu; + struct window *window; + int32_t ix, iy; + struct rectangle parent_geometry; + struct xdg_positioner *positioner; + + menu = create_menu(display, input, time, func, entries, count, parent); + + if (menu == NULL) + { + return; + } + + window = menu->window; + + window_set_buffer_scale(menu->window, window_get_buffer_scale(parent)); + window_set_buffer_transform(menu->window, window_get_buffer_transform(parent)); + + window->x = x; + window->y = y; + + frame_interior(menu->frame, &ix, &iy, NULL, NULL); + window_get_geometry(parent, &parent_geometry); + + if (!display->xdg_shell) + { + return; + } + + window->xdg_surface = + xdg_wm_base_get_xdg_surface(display->xdg_shell, + window->main_surface->surface); + fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__); + + xdg_surface_add_listener(window->xdg_surface, + &xdg_surface_listener, window); + + positioner = create_simple_positioner(display, + window->x - (ix + parent_geometry.x), + window->y - (iy + parent_geometry.y), + frame_width(menu->frame), + frame_height(menu->frame)); + window->xdg_popup = xdg_surface_get_popup(window->xdg_surface, + parent->xdg_surface, + positioner); + fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__); + xdg_positioner_destroy(positioner); + xdg_popup_grab(window->xdg_popup, input->seat, + display_get_serial(window->display)); + xdg_popup_add_listener(window->xdg_popup, + &xdg_popup_listener, window); + + window_inhibit_redraw(window); + + wl_surface_commit(window->main_surface->surface); +} + +void +window_set_buffer_type(struct window *window, enum window_buffer_type type) +{ + window->main_surface->buffer_type = type; +} + +enum window_buffer_type +window_get_buffer_type(struct window *window) +{ + return window->main_surface->buffer_type; +} + +struct widget * +window_add_subsurface(struct window *window, void *data, + enum subsurface_mode default_mode) +{ + struct widget *widget; + struct surface *surface; + struct wl_surface *parent; + struct wl_subcompositor *subcompo = window->display->subcompositor; + + surface = surface_create(window); + surface->buffer_type = window_get_buffer_type(window); + widget = widget_create(window, surface, data); + wl_list_init(&widget->link); + surface->widget = widget; + + parent = window->main_surface->surface; + surface->subsurface = wl_subcompositor_get_subsurface(subcompo, + surface->surface, + parent); + surface->synchronized = 1; + + switch (default_mode) + { + case SUBSURFACE_SYNCHRONIZED: + surface->synchronized_default = 1; + break; + + case SUBSURFACE_DESYNCHRONIZED: + surface->synchronized_default = 0; + break; + + default: + assert(!"bad enum subsurface_mode"); + } + + window->resize_needed = 1; + window_schedule_redraw(window); + + return widget; +} + +static void +display_handle_geometry(void *data, + struct wl_output *wl_output, + int x, int y, + int physical_width, + int physical_height, + int subpixel, + const char *make, + const char *model, + int transform) +{ + struct output *output = data; + + output->allocation.x = x; + output->allocation.y = y; + output->transform = transform; + + if (output->make) + { + free(output->make); + } + output->make = strdup(make); + + if (output->model) + { + free(output->model); + } + output->model = strdup(model); +} + +static void +display_handle_done(void *data, + struct wl_output *wl_output) +{} + +static void +display_handle_scale(void *data, + struct wl_output *wl_output, + int32_t scale) +{ + struct output *output = data; + + output->scale = scale; +} + +static void +display_handle_mode(void *data, + struct wl_output *wl_output, + uint32_t flags, + int width, + int height, + int refresh) +{ + struct output *output = data; + struct display *display = output->display; + + if (flags & WL_OUTPUT_MODE_CURRENT) + { + output->allocation.width = width; + output->allocation.height = height; + if (display->output_configure_handler) + { + (*display->output_configure_handler)( + output, display->user_data); + } + } +} + +static const struct wl_output_listener output_listener = { + display_handle_geometry, + display_handle_mode, + display_handle_done, + display_handle_scale +}; + +static void +display_add_output(struct display *d, uint32_t id) +{ + struct output *output; + + output = xzalloc(sizeof *output); + output->display = d; + output->scale = 1; + output->output = + wl_registry_bind(d->registry, id, &wl_output_interface, 2); + output->server_output_id = id; + wl_list_insert(d->output_list.prev, &output->link); + + wl_output_add_listener(output->output, &output_listener, output); +} + +static void +output_destroy(struct output *output) +{ + if (output->destroy_handler) + { + (*output->destroy_handler)(output, output->user_data); + } + + wl_output_destroy(output->output); + wl_list_remove(&output->link); + free(output); +} + +static void +display_destroy_output(struct display *d, uint32_t id) +{ + struct output *output; + + wl_list_for_each(output, &d->output_list, link) + { + if (output->server_output_id == id) + { + output_destroy(output); + break; + } + } +} + +void +display_set_global_handler(struct display *display, + display_global_handler_t handler) +{ + struct global *global; + + display->global_handler = handler; + if (!handler) + { + return; + } + + wl_list_for_each(global, &display->global_list, link) + display->global_handler(display, + global->name, global->interface, + global->version, display->user_data); +} + +void +display_set_global_handler_remove(struct display *display, + display_global_handler_t remove_handler) +{ + display->global_handler_remove = remove_handler; + if (!remove_handler) + { + return; + } +} + +void +display_set_output_configure_handler(struct display *display, + display_output_handler_t handler) +{ + struct output *output; + + display->output_configure_handler = handler; + if (!handler) + { + return; + } + + wl_list_for_each(output, &display->output_list, link) + { + if ((output->allocation.width == 0) && + (output->allocation.height == 0)) + { + continue; + } + + (*display->output_configure_handler)(output, + display->user_data); + } +} + +void +output_set_user_data(struct output *output, void *data) +{ + output->user_data = data; +} + +void * +output_get_user_data(struct output *output) +{ + return output->user_data; +} + +void +output_set_destroy_handler(struct output *output, + display_output_handler_t handler) +{ + output->destroy_handler = handler; + /* FIXME: implement this, once we have way to remove outputs */ +} + +void +output_get_allocation(struct output *output, struct rectangle *base) +{ + struct rectangle allocation = output->allocation; + + switch (output->transform) + { + case WL_OUTPUT_TRANSFORM_90: + case WL_OUTPUT_TRANSFORM_270: + case WL_OUTPUT_TRANSFORM_FLIPPED_90: + case WL_OUTPUT_TRANSFORM_FLIPPED_270: + /* Swap width and height */ + allocation.width = output->allocation.height; + allocation.height = output->allocation.width; + break; + } + + *base = allocation; +} + +struct wl_output * +output_get_wl_output(struct output *output) +{ + return output->output; +} + +enum wl_output_transform +output_get_transform(struct output *output) +{ + return output->transform; +} + +uint32_t +output_get_scale(struct output *output) +{ + return output->scale; +} + +const char * +output_get_make(struct output *output) +{ + return output->make; +} + +const char * +output_get_model(struct output *output) +{ + return output->model; +} + +static void +fini_xkb(struct input *input) +{ + xkb_state_unref(input->xkb.state); + xkb_keymap_unref(input->xkb.keymap); +} + +static void +display_add_input(struct display *d, uint32_t id, int display_seat_version) +{ + struct input *input; + int seat_version = MIN(display_seat_version, 7); + + input = xzalloc(sizeof *input); + input->display = d; + input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, + seat_version); + input->touch_focus = NULL; + input->pointer_focus = NULL; + input->keyboard_focus = NULL; + input->seat_version = seat_version; + + wl_list_init(&input->touch_point_list); + wl_list_insert(d->input_list.prev, &input->link); + + wl_seat_add_listener(input->seat, &seat_listener, input); + wl_seat_set_user_data(input->seat, input); + + if (d->data_device_manager) + { + input->data_device = + wl_data_device_manager_get_data_device(d->data_device_manager, + input->seat); + wl_data_device_add_listener(input->data_device, + &data_device_listener, + input); + } + + input->pointer_surface = wl_compositor_create_surface(d->compositor); + + toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d, + cursor_timer_func); + + set_repeat_info(input, 40, 400); + toytimer_init(&input->repeat_timer, CLOCK_MONOTONIC, d, + keyboard_repeat_func); +} + +static void +display_add_data_device(struct display *d, uint32_t id, int ddm_version) +{ + struct input *input; + + d->data_device_manager_version = MIN(ddm_version, 3); + d->data_device_manager = + wl_registry_bind(d->registry, id, + &wl_data_device_manager_interface, + d->data_device_manager_version); + + wl_list_for_each(input, &d->input_list, link) + { + if (!input->data_device) + { + input->data_device = + wl_data_device_manager_get_data_device(d->data_device_manager, + input->seat); + wl_data_device_add_listener(input->data_device, + &data_device_listener, + input); + } + } +} + +static void +input_destroy(struct input *input) +{ + input_remove_keyboard_focus(input); + input_remove_pointer_focus(input); + + if (input->drag_offer) + { + data_offer_destroy(input->drag_offer); + } + + if (input->selection_offer) + { + data_offer_destroy(input->selection_offer); + } + + if (input->data_device) + { + if (input->display->data_device_manager_version >= 2) + { + wl_data_device_release(input->data_device); + } + else + { + wl_data_device_destroy(input->data_device); + } + } + if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) + { + if (input->touch) + { + wl_touch_release(input->touch); + } + if (input->pointer) + { + wl_pointer_release(input->pointer); + } + if (input->keyboard) + { + wl_keyboard_release(input->keyboard); + } + } + else + { + if (input->touch) + { + wl_touch_destroy(input->touch); + } + if (input->pointer) + { + wl_pointer_destroy(input->pointer); + } + if (input->keyboard) + { + wl_keyboard_destroy(input->keyboard); + } + } + + fini_xkb(input); + + wl_surface_destroy(input->pointer_surface); + + wl_list_remove(&input->link); + wl_seat_destroy(input->seat); + toytimer_fini(&input->repeat_timer); + toytimer_fini(&input->cursor_timer); + free(input); +} + +static void +xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial) +{ + xdg_wm_base_pong(shell, serial); +} + +static const struct xdg_wm_base_listener wm_base_listener = { + xdg_wm_base_ping, +}; + +static void +registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, + const char *interface, uint32_t version) +{ + struct display *d = data; + struct global *global; + + global = xmalloc(sizeof *global); + global->name = id; + global->interface = strdup(interface); + global->version = version; + wl_list_insert(d->global_list.prev, &global->link); + + if (strcmp(interface, "wl_compositor") == 0) + { + d->compositor = wl_registry_bind(registry, id, + &wl_compositor_interface, 3); + } + else if (strcmp(interface, "wl_output") == 0) + { + display_add_output(d, id); + } + else if (strcmp(interface, "wl_seat") == 0) + { + display_add_input(d, id, version); + } + else if ((strcmp(interface, "zwp_relative_pointer_manager_v1") == 0) && + (version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION)) + { + d->relative_pointer_manager = + wl_registry_bind(registry, id, + &zwp_relative_pointer_manager_v1_interface, + 1); + } + else if ((strcmp(interface, "zwp_pointer_constraints_v1") == 0) && + (version == ZWP_POINTER_CONSTRAINTS_V1_VERSION)) + { + d->pointer_constraints = + wl_registry_bind(registry, id, + &zwp_pointer_constraints_v1_interface, + 1); + } + else if (strcmp(interface, "wl_shm") == 0) + { + d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1); + } + else if (strcmp(interface, "wl_data_device_manager") == 0) + { + display_add_data_device(d, id, version); + } + else if (strcmp(interface, "xdg_wm_base") == 0) + { + d->xdg_shell = wl_registry_bind(registry, id, + &xdg_wm_base_interface, 1); + xdg_wm_base_add_listener(d->xdg_shell, &wm_base_listener, d); + } + else if (strcmp(interface, "text_cursor_position") == 0) + { + d->text_cursor_position = + wl_registry_bind(registry, id, + &text_cursor_position_interface, 1); + } + else if (strcmp(interface, "wl_subcompositor") == 0) + { + d->subcompositor = + wl_registry_bind(registry, id, + &wl_subcompositor_interface, 1); + } + else if (!strcmp(interface, "wp_viewporter")) + { + d->viewporter = + wl_registry_bind(registry, id, + &wp_viewporter_interface, 1); + } + + if (d->global_handler) + { + d->global_handler(d, id, interface, version, d->user_data); + } +} + +static void +registry_handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) +{ + struct display *d = data; + struct global *global; + struct global *tmp; + + wl_list_for_each_safe(global, tmp, &d->global_list, link) + { + if (global->name != name) + { + continue; + } + + if (strcmp(global->interface, "wl_output") == 0) + { + display_destroy_output(d, name); + } + + /* XXX: Should destroy remaining bound globals */ + + if (d->global_handler_remove) + { + d->global_handler_remove(d, name, global->interface, + global->version, d->user_data); + } + + wl_list_remove(&global->link); + free(global->interface); + free(global); + } +} + +void * +display_bind(struct display *display, uint32_t name, + const struct wl_interface *interface, uint32_t version) +{ + return wl_registry_bind(display->registry, name, interface, version); +} + +static const struct wl_registry_listener registry_listener = { + registry_handle_global, + registry_handle_global_remove +}; + +#ifdef HAVE_CAIRO_EGL +static int +init_egl(struct display *d) +{ + EGLint major, minor; + EGLint n; + +#ifdef USE_CAIRO_GLESV2 +# define GL_BIT EGL_OPENGL_ES2_BIT +#else +# define GL_BIT EGL_OPENGL_BIT +#endif + + static const EGLint argb_cfg_attribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_ALPHA_SIZE, 1, + EGL_DEPTH_SIZE, 1, + EGL_RENDERABLE_TYPE, GL_BIT, + EGL_NONE + }; + +#ifdef USE_CAIRO_GLESV2 + static const EGLint context_attribs[] = { + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_NONE + }; + EGLint api = EGL_OPENGL_ES_API; +#else + EGLint *context_attribs = NULL; + EGLint api = EGL_OPENGL_API; +#endif + + d->dpy = + weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR, + d->display, NULL); + + if (!eglInitialize(d->dpy, &major, &minor)) + { + fprintf(stderr, "failed to initialize EGL\n"); + return -1; + } + + if (!eglBindAPI(api)) + { + fprintf(stderr, "failed to bind EGL client API\n"); + return -1; + } + + if (!eglChooseConfig(d->dpy, argb_cfg_attribs, + &d->argb_config, 1, &n) || (n != 1)) + { + fprintf(stderr, "failed to choose argb EGL config\n"); + return -1; + } + + d->argb_ctx = eglCreateContext(d->dpy, d->argb_config, + EGL_NO_CONTEXT, context_attribs); + if (d->argb_ctx == NULL) + { + fprintf(stderr, "failed to create EGL context\n"); + return -1; + } + + d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx); + if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) + { + fprintf(stderr, "failed to get cairo EGL argb device\n"); + return -1; + } + + return 0; +} + +static void +fini_egl(struct display *display) +{ + cairo_device_destroy(display->argb_device); + + eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, + EGL_NO_CONTEXT); + + eglTerminate(display->dpy); + eglReleaseThread(); +} +#endif + +static void +init_dummy_surface(struct display *display) +{ + int len; + void *data; + + len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1); + data = xmalloc(len); + display->dummy_surface = + cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, + 1, 1, len); + display->dummy_surface_data = data; +} + +static void +handle_display_data(struct task *task, uint32_t events) +{ + struct display *display = + container_of(task, struct display, display_task); + struct epoll_event ep; + int ret; + + display->display_fd_events = events; + + if (events & EPOLLERR || events & EPOLLHUP) + { + display_exit(display); + return; + } + + if (events & EPOLLIN) + { + ret = wl_display_dispatch(display->display); + if (ret == -1) + { + display_exit(display); + return; + } + } + + if (events & EPOLLOUT) + { + ret = wl_display_flush(display->display); + if (ret == 0) + { + ep.events = EPOLLIN | EPOLLERR | EPOLLHUP; + ep.data.ptr = &display->display_task; + epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD, + display->display_fd, &ep); + } + else if ((ret == -1) && (errno != EAGAIN)) + { + display_exit(display); + return; + } + } +} + +static void +log_handler(const char *format, va_list args) +{ + vfprintf(stderr, format, args); +} + +struct display * +display_create(int *argc, char *argv[]) +{ + struct display *d; + + wl_log_set_handler_client(log_handler); + + d = zalloc(sizeof *d); + if (d == NULL) + { + return NULL; + } + + d->display = wl_display_connect(NULL); + if (d->display == NULL) + { + fprintf(stderr, "failed to connect to Wayland display: %s\n", + strerror(errno)); + free(d); + return NULL; + } + + d->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (d->xkb_context == NULL) + { + fprintf(stderr, "Failed to create XKB context\n"); + free(d); + return NULL; + } + + d->epoll_fd = os_epoll_create_cloexec(); + d->display_fd = wl_display_get_fd(d->display); + d->display_task.run = handle_display_data; + display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP, + &d->display_task); + + wl_list_init(&d->deferred_list); + wl_list_init(&d->input_list); + wl_list_init(&d->output_list); + wl_list_init(&d->global_list); + + d->registry = wl_display_get_registry(d->display); + wl_registry_add_listener(d->registry, ®istry_listener, d); + + if (wl_display_roundtrip(d->display) < 0) + { + fprintf(stderr, "Failed to process Wayland connection: %s\n", + strerror(errno)); + return NULL; + } + +#ifdef HAVE_CAIRO_EGL + if (init_egl(d) < 0) + { + fprintf(stderr, "EGL does not seem to work, " + "falling back to software rendering and wl_shm.\n"); + } +#endif + + create_cursors(d); + + d->theme = theme_create(); + + wl_list_init(&d->window_list); + + init_dummy_surface(d); + + return d; +} + +static void +display_destroy_outputs(struct display *display) +{ + struct output *tmp; + struct output *output; + + wl_list_for_each_safe(output, tmp, &display->output_list, link) + output_destroy(output); +} + +static void +display_destroy_inputs(struct display *display) +{ + struct input *tmp; + struct input *input; + + wl_list_for_each_safe(input, tmp, &display->input_list, link) + input_destroy(input); +} + +void +display_destroy(struct display *display) +{ + if (!wl_list_empty(&display->window_list)) + { + fprintf(stderr, "toytoolkit warning: %d windows exist.\n", + wl_list_length(&display->window_list)); + } + + if (!wl_list_empty(&display->deferred_list)) + { + fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n"); + } + + cairo_surface_destroy(display->dummy_surface); + free(display->dummy_surface_data); + + display_destroy_outputs(display); + display_destroy_inputs(display); + + xkb_context_unref(display->xkb_context); + + theme_destroy(display->theme); + destroy_cursors(display); + +#ifdef HAVE_CAIRO_EGL + if (display->argb_device) + { + fini_egl(display); + } +#endif + + if (display->subcompositor) + { + wl_subcompositor_destroy(display->subcompositor); + } + + if (display->xdg_shell) + { + xdg_wm_base_destroy(display->xdg_shell); + } + + if (display->shm) + { + wl_shm_destroy(display->shm); + } + + if (display->data_device_manager) + { + wl_data_device_manager_destroy(display->data_device_manager); + } + + wl_compositor_destroy(display->compositor); + wl_registry_destroy(display->registry); + + close(display->epoll_fd); + + if (!(display->display_fd_events & EPOLLERR) && + !(display->display_fd_events & EPOLLHUP)) + { + wl_display_flush(display->display); + } + + wl_display_disconnect(display->display); + free(display); +} + +void +display_set_user_data(struct display *display, void *data) +{ + display->user_data = data; +} + +void * +display_get_user_data(struct display *display) +{ + return display->user_data; +} + +struct wl_display * +display_get_display(struct display *display) +{ + return display->display; +} + +int +display_has_subcompositor(struct display *display) +{ + if (display->subcompositor) + { + return 1; + } + + wl_display_roundtrip(display->display); + + return display->subcompositor != NULL; +} + +cairo_device_t * +display_get_cairo_device(struct display *display) +{ + return display->argb_device; +} + +struct output * +display_get_output(struct display *display) +{ + if (wl_list_empty(&display->output_list)) + { + return NULL; + } + + return container_of(display->output_list.next, struct output, link); +} + +struct wl_compositor * +display_get_compositor(struct display *display) +{ + return display->compositor; +} + +uint32_t +display_get_serial(struct display *display) +{ + return display->serial; +} + +EGLDisplay +display_get_egl_display(struct display *d) +{ + return d->dpy; +} + +struct wl_data_source * +display_create_data_source(struct display *display) +{ + if (display->data_device_manager) + { + return wl_data_device_manager_create_data_source(display->data_device_manager); + } + else + { + return NULL; + } +} + +EGLConfig +display_get_argb_egl_config(struct display *d) +{ + return d->argb_config; +} + +int +display_acquire_window_surface(struct display *display, + struct window *window, + EGLContext ctx) +{ + struct surface *surface = window->main_surface; + + if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW) + { + return -1; + } + + widget_get_cairo_surface(window->main_surface->widget); + return surface->toysurface->acquire(surface->toysurface, ctx); +} + +void +display_release_window_surface(struct display *display, + struct window *window) +{ + struct surface *surface = window->main_surface; + + if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW) + { + return; + } + + surface->toysurface->release(surface->toysurface); +} + +void +display_defer(struct display *display, struct task *task) +{ + wl_list_insert(&display->deferred_list, &task->link); +} + +void +display_watch_fd(struct display *display, + int fd, uint32_t events, struct task *task) +{ + struct epoll_event ep; + + ep.events = events; + ep.data.ptr = task; + epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep); +} + +void +display_unwatch_fd(struct display *display, int fd) +{ + epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL); +} + +void +display_run(struct display *display) +{ + struct task *task; + struct epoll_event ep[16]; + int i, count, ret; + + display->running = 1; + while (1) + { + while (!wl_list_empty(&display->deferred_list)) + { + task = container_of(display->deferred_list.prev, + struct task, link); + wl_list_remove(&task->link); + task->run(task, 0); + } + + wl_display_dispatch_pending(display->display); + + if (!display->running) + { + break; + } + + ret = wl_display_flush(display->display); + if ((ret < 0) && (errno == EAGAIN)) + { + ep[0].events = + EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP; + ep[0].data.ptr = &display->display_task; + + epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD, + display->display_fd, &ep[0]); + } + else if (ret < 0) + { + break; + } + + count = epoll_wait(display->epoll_fd, + ep, ARRAY_LENGTH(ep), -1); + for (i = 0; i < count; i++) + { + task = ep[i].data.ptr; + task->run(task, ep[i].events); + } + } +} + +void +display_exit(struct display *display) +{ + display->running = 0; +} + +int +display_get_data_device_manager_version(struct display *display) +{ + return display->data_device_manager_version; +} + +void +keysym_modifiers_add(struct wl_array *modifiers_map, + const char *name) +{ + size_t len = strlen(name) + 1; + char *p; + + p = wl_array_add(modifiers_map, len); + + if (p == NULL) + { + return; + } + + strncpy(p, name, len); +} + +static xkb_mod_index_t +keysym_modifiers_get_index(struct wl_array *modifiers_map, + const char *name) +{ + xkb_mod_index_t index = 0; + char *p = modifiers_map->data; + + while ((const char *) p < (const char *) (modifiers_map->data + modifiers_map->size)) + { + if (strcmp(p, name) == 0) + { + return index; + } + + index++; + p += strlen(p) + 1; + } + + return XKB_MOD_INVALID; +} + +xkb_mod_mask_t +keysym_modifiers_get_mask(struct wl_array *modifiers_map, + const char *name) +{ + xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name); + + if (index == XKB_MOD_INVALID) + { + return XKB_MOD_INVALID; + } + + return 1 << index; +} + +static void +toytimer_fire(struct task *tsk, uint32_t events) +{ + uint64_t e; + struct toytimer *tt; + + tt = container_of(tsk, struct toytimer, tsk); + + if (events != EPOLLIN) + { + fprintf(stderr, "unexpected timerfd events %x\n", events); + } + + if (!(events & EPOLLIN)) + { + return; + } + + if (read(tt->fd, &e, sizeof e) != sizeof e) + { + /* If we change the timer between the fd becoming + * readable and getting here, there'll be nothing to + * read and we get EAGAIN. */ + if (errno != EAGAIN) + { + fprintf(stderr, "timer read failed: %s\n", + strerror(errno)); + } + return; + } + + tt->callback(tt); +} + +void +toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display, + toytimer_cb callback) +{ + memset(tt, 0, sizeof *tt); + + tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK); + if (tt->fd == -1) + { + fprintf(stderr, "creating timer failed: %s\n", + strerror(errno)); + abort(); + } + + tt->display = display; + tt->callback = callback; + tt->tsk.run = toytimer_fire; + display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk); +} + +void +toytimer_fini(struct toytimer *tt) +{ + display_unwatch_fd(tt->display, tt->fd); + close(tt->fd); + tt->fd = -1; +} + +void +toytimer_arm(struct toytimer *tt, const struct itimerspec *its) +{ + int ret; + + ret = timerfd_settime(tt->fd, 0, its, NULL); + if (ret < 0) + { + fprintf(stderr, "timer setup failed: %s\n", strerror(errno)); + abort(); + } +} + +#define USEC_PER_SEC 1000000 + +void +toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec) +{ + struct itimerspec its; + + its.it_interval.tv_sec = 0; + its.it_interval.tv_nsec = 0; + its.it_value.tv_sec = usec / USEC_PER_SEC; + its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000; + toytimer_arm(tt, &its); +} + +void +toytimer_disarm(struct toytimer *tt) +{ + struct itimerspec its = {}; + + toytimer_arm(tt, &its); +} diff --git a/window.h b/window.h new file mode 100644 index 0000000..9543dbc --- /dev/null +++ b/window.h @@ -0,0 +1,750 @@ +/* + * Copyright © 2008 Kristian Høgsberg + * + * 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. + */ + +#ifndef _WINDOW_H_ +#define _WINDOW_H_ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include "shared/platform.h" + +struct window; +struct widget; +struct display; +struct input; +struct output; + +struct task +{ + void (*run)(struct task *task, uint32_t events); + struct wl_list link; +}; + +struct rectangle +{ + int32_t x; + int32_t y; + int32_t width; + int32_t height; +}; + +struct display * +display_create(int *argc, char *argv[]); + +void +display_destroy(struct display *display); + +void +display_set_user_data(struct display *display, void *data); + +void * +display_get_user_data(struct display *display); + +struct wl_display * +display_get_display(struct display *display); + +int +display_has_subcompositor(struct display *display); + +cairo_device_t * +display_get_cairo_device(struct display *display); + +struct wl_compositor * +display_get_compositor(struct display *display); + +struct output * +display_get_output(struct display *display); + +uint32_t +display_get_serial(struct display *display); + +typedef void (*display_global_handler_t)(struct display *display, + uint32_t name, + const char *interface, + uint32_t version, void *data); + +void +display_set_global_handler(struct display *display, + display_global_handler_t handler); +void +display_set_global_handler_remove(struct display *display, + display_global_handler_t remove_handler); +void * +display_bind(struct display *display, uint32_t name, + const struct wl_interface *interface, uint32_t version); + +typedef void (*display_output_handler_t)(struct output *output, void *data); + +/* + * The output configure handler is called, when a new output is connected + * and we know its current mode, or when the current mode changes. + * Test and set the output user data in your handler to know, if the + * output is new. Note: 'data' in the configure handler is the display + * user data. + */ +void +display_set_output_configure_handler(struct display *display, + display_output_handler_t handler); + +struct wl_data_source * +display_create_data_source(struct display *display); + +#ifdef EGL_NO_DISPLAY +EGLDisplay +display_get_egl_display(struct display *d); + +EGLConfig +display_get_argb_egl_config(struct display *d); + +int +display_acquire_window_surface(struct display *display, + struct window *window, + EGLContext ctx); +void +display_release_window_surface(struct display *display, + struct window *window); +#endif + +#define SURFACE_OPAQUE 0x01 +#define SURFACE_SHM 0x02 + +#define SURFACE_HINT_RESIZE 0x10 + +cairo_surface_t * +display_create_surface(struct display *display, + struct wl_surface *surface, + struct rectangle *rectangle, + uint32_t flags); + +struct wl_buffer * +display_get_buffer_for_surface(struct display *display, + cairo_surface_t *surface); + +struct wl_cursor_image * +display_get_pointer_image(struct display *display, int pointer); + +void +display_defer(struct display *display, struct task *task); + +void +display_watch_fd(struct display *display, + int fd, uint32_t events, struct task *task); + +void +display_unwatch_fd(struct display *display, int fd); + +void +display_run(struct display *d); + +void +display_exit(struct display *d); + +int +display_get_data_device_manager_version(struct display *d); + +enum cursor_type +{ + CURSOR_BOTTOM_LEFT, + CURSOR_BOTTOM_RIGHT, + CURSOR_BOTTOM, + CURSOR_DRAGGING, + CURSOR_LEFT_PTR, + CURSOR_LEFT, + CURSOR_RIGHT, + CURSOR_TOP_LEFT, + CURSOR_TOP_RIGHT, + CURSOR_TOP, + CURSOR_IBEAM, + CURSOR_HAND1, + CURSOR_WATCH, + CURSOR_DND_MOVE, + CURSOR_DND_COPY, + CURSOR_DND_FORBIDDEN, + + CURSOR_BLANK +}; + +typedef void (*window_key_handler_t)(struct window *window, struct input *input, + uint32_t time, uint32_t key, uint32_t unicode, + enum wl_keyboard_key_state state, void *data); + +typedef void (*window_keyboard_focus_handler_t)(struct window *window, + struct input *device, void *data); + +typedef void (*window_data_handler_t)(struct window *window, + struct input *input, + float x, float y, + const char **types, + void *data); + +typedef void (*window_drop_handler_t)(struct window *window, + struct input *input, + int32_t x, int32_t y, void *data); + +typedef void (*window_close_handler_t)(void *data); +typedef void (*window_fullscreen_handler_t)(struct window *window, void *data); + +typedef void (*window_output_handler_t)(struct window *window, struct output *output, + int enter, void *data); +typedef void (*window_state_changed_handler_t)(struct window *window, + void *data); + + +typedef void (*window_locked_pointer_motion_handler_t)(struct window *window, + struct input *input, + uint32_t time, + float x, float y, + void *data); + +typedef void (*locked_pointer_locked_handler_t)(struct window *window, + struct input *input, + void *data); + +typedef void (*locked_pointer_unlocked_handler_t)(struct window *window, + struct input *input, + void *data); + +typedef void (*confined_pointer_confined_handler_t)(struct window *window, + struct input *input, + void *data); + +typedef void (*confined_pointer_unconfined_handler_t)(struct window *window, + struct input *input, + void *data); + +typedef void (*widget_resize_handler_t)(struct widget *widget, + int32_t width, int32_t height, + void *data); +typedef void (*widget_redraw_handler_t)(struct widget *widget, void *data); + +typedef int (*widget_enter_handler_t)(struct widget *widget, + struct input *input, + float x, float y, void *data); +typedef void (*widget_leave_handler_t)(struct widget *widget, + struct input *input, void *data); +typedef int (*widget_motion_handler_t)(struct widget *widget, + struct input *input, uint32_t time, + float x, float y, void *data); +typedef void (*widget_button_handler_t)(struct widget *widget, + struct input *input, uint32_t time, + uint32_t button, + enum wl_pointer_button_state state, + void *data); +typedef void (*widget_touch_down_handler_t)(struct widget *widget, + struct input *input, + uint32_t serial, + uint32_t time, + int32_t id, + float x, + float y, + void *data); +typedef void (*widget_touch_up_handler_t)(struct widget *widget, + struct input *input, + uint32_t serial, + uint32_t time, + int32_t id, + void *data); +typedef void (*widget_touch_motion_handler_t)(struct widget *widget, + struct input *input, + uint32_t time, + int32_t id, + float x, + float y, + void *data); +typedef void (*widget_touch_frame_handler_t)(struct widget *widget, + struct input *input, void *data); +typedef void (*widget_touch_cancel_handler_t)(struct widget *widget, + struct input *input, void *data); +typedef void (*widget_axis_handler_t)(struct widget *widget, + struct input *input, uint32_t time, + uint32_t axis, + wl_fixed_t value, + void *data); + +typedef void (*widget_pointer_frame_handler_t)(struct widget *widget, + struct input *input, + void *data); + +typedef void (*widget_axis_source_handler_t)(struct widget *widget, + struct input *input, + uint32_t source, + void *data); + +typedef void (*widget_axis_stop_handler_t)(struct widget *widget, + struct input *input, + uint32_t time, + uint32_t axis, + void *data); + +typedef void (*widget_axis_discrete_handler_t)(struct widget *widget, + struct input *input, + uint32_t axis, + int32_t discrete, + void *data); + +struct window * +window_create(struct display *display); +struct window * +window_create_custom(struct display *display); + +void +window_set_parent(struct window *window, struct window *parent_window); +struct window * +window_get_parent(struct window *window); + +int +window_has_focus(struct window *window); + +typedef void (*menu_func_t)(void *data, struct input *input, int index); + +void +window_show_menu(struct display *display, + struct input *input, uint32_t time, struct window *parent, + int32_t x, int32_t y, + menu_func_t func, const char **entries, int count); + +void +window_show_frame_menu(struct window *window, + struct input *input, uint32_t time); + +int +window_get_buffer_transform(struct window *window); + +void +window_set_buffer_transform(struct window *window, + enum wl_output_transform transform); + +uint32_t +window_get_buffer_scale(struct window *window); + +void +window_set_buffer_scale(struct window *window, + int32_t scale); + +uint32_t +window_get_output_scale(struct window *window); + +void +window_destroy(struct window *window); + +struct widget * +window_add_widget(struct window *window, void *data); + +enum subsurface_mode +{ + SUBSURFACE_SYNCHRONIZED, + SUBSURFACE_DESYNCHRONIZED +}; + +struct widget * +window_add_subsurface(struct window *window, void *data, + enum subsurface_mode default_mode); + +typedef void (*data_func_t)(void *data, size_t len, + int32_t x, int32_t y, void *user_data); + +struct display * +window_get_display(struct window *window); +void +window_move(struct window *window, struct input *input, uint32_t time); +void +window_get_allocation(struct window *window, struct rectangle *allocation); +void +window_schedule_redraw(struct window *window); +void +window_schedule_resize(struct window *window, int width, int height); + +int +window_lock_pointer(struct window *window, struct input *input); + +void +window_unlock_pointer(struct window *window); + +void +widget_set_locked_pointer_cursor_hint(struct widget *widget, + float x, float y); + +int +window_confine_pointer_to_rectangles(struct window *window, + struct input *input, + struct rectangle *rectangles, + int num_rectangles); + +void +window_update_confine_rectangles(struct window *window, + struct rectangle *rectangles, + int num_rectangles); + +int +window_confine_pointer_to_widget(struct window *window, + struct widget *widget, + struct input *input); + +void +window_unconfine_pointer(struct window *window); + +cairo_surface_t * +window_get_surface(struct window *window); + +struct wl_surface * +window_get_wl_surface(struct window *window); + +struct wl_subsurface * +widget_get_wl_subsurface(struct widget *widget); + +enum window_buffer_type +{ + WINDOW_BUFFER_TYPE_EGL_WINDOW, + WINDOW_BUFFER_TYPE_SHM, +}; + +void +display_surface_damage(struct display *display, cairo_surface_t *cairo_surface, + int32_t x, int32_t y, int32_t width, int32_t height); + +void +window_set_buffer_type(struct window *window, enum window_buffer_type type); + +enum window_buffer_type +window_get_buffer_type(struct window *window); + +int +window_is_fullscreen(struct window *window); + +void +window_set_fullscreen(struct window *window, int fullscreen); + +int +window_is_maximized(struct window *window); + +void +window_set_maximized(struct window *window, int maximized); + +int +window_is_resizing(struct window *window); + +void +window_set_minimized(struct window *window); + +void +window_set_user_data(struct window *window, void *data); + +void * +window_get_user_data(struct window *window); + +void +window_set_key_handler(struct window *window, + window_key_handler_t handler); + +void +window_set_keyboard_focus_handler(struct window *window, + window_keyboard_focus_handler_t handler); + +void +window_set_data_handler(struct window *window, + window_data_handler_t handler); + +void +window_set_drop_handler(struct window *window, + window_drop_handler_t handler); + +void +window_set_close_handler(struct window *window, + window_close_handler_t handler); +void +window_set_fullscreen_handler(struct window *window, + window_fullscreen_handler_t handler); +void +window_set_output_handler(struct window *window, + window_output_handler_t handler); +void +window_set_state_changed_handler(struct window *window, + window_state_changed_handler_t handler); + +void +window_set_pointer_locked_handler(struct window *window, + locked_pointer_locked_handler_t locked, + locked_pointer_unlocked_handler_t unlocked); + +void +window_set_pointer_confined_handler(struct window *window, + confined_pointer_confined_handler_t confined, + confined_pointer_unconfined_handler_t unconfined); + +void +window_set_locked_pointer_motion_handler( + struct window *window, window_locked_pointer_motion_handler_t handler); + +void +window_set_title(struct window *window, const char *title); + +const char * +window_get_title(struct window *window); + +void +window_set_text_cursor_position(struct window *window, int32_t x, int32_t y); + +int +widget_set_tooltip(struct widget *parent, char *entry, float x, float y); + +void +widget_destroy_tooltip(struct widget *parent); + +struct widget * +widget_add_widget(struct widget *parent, void *data); + +void +widget_destroy(struct widget *widget); +void +widget_set_default_cursor(struct widget *widget, int cursor); +void +widget_get_allocation(struct widget *widget, struct rectangle *allocation); + +void +widget_set_allocation(struct widget *widget, + int32_t x, int32_t y, int32_t width, int32_t height); +void +widget_set_size(struct widget *widget, int32_t width, int32_t height); +void +widget_set_transparent(struct widget *widget, int transparent); +void +widget_schedule_resize(struct widget *widget, int32_t width, int32_t height); + +void * +widget_get_user_data(struct widget *widget); + +cairo_t * +widget_cairo_create(struct widget *widget); + +struct wl_surface * +widget_get_wl_surface(struct widget *widget); + +uint32_t +widget_get_last_time(struct widget *widget); + +void +widget_input_region_add(struct widget *widget, const struct rectangle *rect); + +void +widget_set_redraw_handler(struct widget *widget, + widget_redraw_handler_t handler); +void +widget_set_resize_handler(struct widget *widget, + widget_resize_handler_t handler); +void +widget_set_enter_handler(struct widget *widget, + widget_enter_handler_t handler); +void +widget_set_leave_handler(struct widget *widget, + widget_leave_handler_t handler); +void +widget_set_motion_handler(struct widget *widget, + widget_motion_handler_t handler); +void +widget_set_button_handler(struct widget *widget, + widget_button_handler_t handler); +void +widget_set_touch_down_handler(struct widget *widget, + widget_touch_down_handler_t handler); +void +widget_set_touch_up_handler(struct widget *widget, + widget_touch_up_handler_t handler); +void +widget_set_touch_motion_handler(struct widget *widget, + widget_touch_motion_handler_t handler); +void +widget_set_touch_frame_handler(struct widget *widget, + widget_touch_frame_handler_t handler); +void +widget_set_touch_cancel_handler(struct widget *widget, + widget_touch_cancel_handler_t handler); +void +widget_set_axis_handler(struct widget *widget, + widget_axis_handler_t handler); +void +widget_set_pointer_frame_handler(struct widget *widget, + widget_pointer_frame_handler_t 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); + +void +window_inhibit_redraw(struct window *window); +void +window_uninhibit_redraw(struct window *window); +void +widget_schedule_redraw(struct widget *widget); +void +widget_set_use_cairo(struct widget *widget, int use_cairo); + +/* + * Sets the viewport destination for the widget's surface + * return 0 on success and -1 on failure. Set width and height to + * -1 to reset the viewport. + */ +int +widget_set_viewport_destination(struct widget *widget, int width, int height); + +struct widget * +window_frame_create(struct window *window, void *data); + +void +window_frame_set_child_size(struct widget *widget, int child_width, + int child_height); + +void +input_set_pointer_image(struct input *input, int pointer); + +void +input_get_position(struct input *input, int32_t *x, int32_t *y); + +int +input_get_touch(struct input *input, int32_t id, float *x, float *y); + +#define MOD_SHIFT_MASK 0x01 +#define MOD_ALT_MASK 0x02 +#define MOD_CONTROL_MASK 0x04 + +uint32_t +input_get_modifiers(struct input *input); + +void +touch_grab(struct input *input, int32_t touch_id); + +void +touch_ungrab(struct input *input); + +void +input_grab(struct input *input, struct widget *widget, uint32_t button); + +void +input_ungrab(struct input *input); + +struct widget * +input_get_focus_widget(struct input *input); + +struct display * +input_get_display(struct input *input); + +struct wl_seat * +input_get_seat(struct input *input); + +struct wl_data_device * +input_get_data_device(struct input *input); + +void +input_set_selection(struct input *input, + struct wl_data_source *source, uint32_t time); + +void +input_accept(struct input *input, const char *type); + + +void +input_receive_drag_data(struct input *input, const char *mime_type, + data_func_t func, void *user_data); +int +input_receive_drag_data_to_fd(struct input *input, + const char *mime_type, int fd); + +int +input_receive_selection_data(struct input *input, const char *mime_type, + data_func_t func, void *data); +int +input_receive_selection_data_to_fd(struct input *input, + const char *mime_type, int fd); + +void +output_set_user_data(struct output *output, void *data); + +void * +output_get_user_data(struct output *output); + +void +output_set_destroy_handler(struct output *output, + display_output_handler_t handler); + +void +output_get_allocation(struct output *output, struct rectangle *allocation); + +struct wl_output * +output_get_wl_output(struct output *output); + +enum wl_output_transform +output_get_transform(struct output *output); + +uint32_t +output_get_scale(struct output *output); + +const char * +output_get_make(struct output *output); + +const char * +output_get_model(struct output *output); + +void +keysym_modifiers_add(struct wl_array *modifiers_map, + const char *name); + +xkb_mod_mask_t +keysym_modifiers_get_mask(struct wl_array *modifiers_map, + const char *name); + +struct toytimer; +typedef void (*toytimer_cb)(struct toytimer *); + +struct toytimer +{ + struct display *display; + struct task tsk; + int fd; + toytimer_cb callback; +}; + +void +toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display, + toytimer_cb callback); + +void +toytimer_fini(struct toytimer *tt); + +void +toytimer_arm(struct toytimer *tt, const struct itimerspec *its); + +void +toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec); + +void +toytimer_disarm(struct toytimer *tt); + +#endif