[PATCH v3 weston] Introduce pointer locking and confinement protocol

Jonas Ådahl jadahl at gmail.com
Thu Jun 25 21:38:00 PDT 2015


This patch introduces a new protocol for locking and confining a
pointer. It consists of a new global object with two requests; one for
locking the surface to a position, one for confining the pointer to a
given region.

See pointer-lock.xml for details of the protocol.

In this patch, only the locking part is fully implemented as in
specified in the protocol, while confinement is only implemented for
when the union of the passed region and the input region of the confined
surface is a single rectangle.

Note that the interfaces are prefixed with an underscore in order to
avoid future incompatibilities with a future stable interface with an
equivalent name.

Signed-off-by: Jonas Ådahl <jadahl at gmail.com>
---

Changes since v2:

 * Updated copyright.
 * Updated to the fixed license text.
 * Clarified that locks are on made on a certain surface from a given
   seat. The same surface may have many parallel locks; one per seat.
 * Added an error enum.
 * Changed the wording explaining that constraints for activating a lock
   is compositor specific.
 * Clarified that there is no guarantee that a lock will ever be
   activated. No error will be raised if the compositor will never
   activate the lock.
 * Changed wording related to pointer events (make them speak protocol,
   not abstract events).
 * Clarify what happens if a surface is destroyed.
 * Fixed various typos.
 * Implemented support for per-seat locks.
 * Clarified what happens when effective lock/confine regions change.
 * Added wl_locked_pointer.set_region and wl_confined_pointer.set_region
   (see below).
 * Implemented support for above mentioned new requests.
 * Changed the order of requests (put destroy first).
 * Added 'unstable protocol' warning and explained its semantics.

This version adds support for changing the lock/confine region after the
lock was created.

This is so that a client can change the lock region without being
exposed to a race condition. Depending on the type of lock
(lock/confine). For exampe, a client who maximizes wants to keep the
pending lock or pending/active confinement intact can use the set_region
request. The region is double buffered, meaning it is synchronized with
the surface input region.

For wl_locked_pointer, since a changed effective region has no effect on
the lock after it being activated, the set_region request only has effect
before the lock is activated. As such, it affects the region that is used
to activate the lock.

For pending wl_confined_pointer lock, the effect is the same as for
wl_locked_pointer. But for an already activated confinement, it affects the
pointer, i.e. warps the pointer to somewhere within the region.


Jonas


 Makefile.am               |   3 +
 protocol/pointer-lock.xml | 275 +++++++++++++++++
 src/compositor.c          |  11 +
 src/compositor.h          |  38 +++
 src/input.c               | 751 ++++++++++++++++++++++++++++++++++++++++++++--
 5 files changed, 1061 insertions(+), 17 deletions(-)
 create mode 100644 protocol/pointer-lock.xml

diff --git a/Makefile.am b/Makefile.am
index 70c436f..201b780 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -106,6 +106,8 @@ nodist_weston_SOURCES =					\
 	protocol/presentation_timing-server-protocol.h	\
 	protocol/scaler-protocol.c			\
 	protocol/scaler-server-protocol.h		\
+	protocol/pointer-lock-protocol.c		\
+	protocol/pointer-lock-server-protocol.h		\
 	protocol/relative-pointer-protocol.c		\
 	protocol/relative-pointer-server-protocol.h
 
@@ -1186,6 +1188,7 @@ EXTRA_DIST +=					\
 	protocol/scaler.xml			\
 	protocol/ivi-application.xml		\
 	protocol/ivi-hmi-controller.xml		\
+	protocol/pointer-lock.xml		\
 	protocol/relative-pointer.xml
 
 #
diff --git a/protocol/pointer-lock.xml b/protocol/pointer-lock.xml
new file mode 100644
index 0000000..ee5e274
--- /dev/null
+++ b/protocol/pointer-lock.xml
@@ -0,0 +1,275 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<protocol name="pointer_lock">
+
+  <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.
+  </copyright>
+
+  <interface name="_wl_pointer_lock" version="1">
+    <description summary="lock pointer to a surface">
+      The global interface exposing pointer locking 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 creates the objects wl_locked_pointer
+      and wl_confined_pointer respectively, and the client can use these objects
+      to interact with the lock.
+
+      For any surface, only one lock or confinement per seat may be active at
+      any time. If a lock or confinement is requested when another lock or
+      confinement is active on that surface and seat, an 'already_locked' error
+      will be raised.
+
+      Warning! The protocol described in this file is experimental. Each version
+      of this protocol should be considered incompatible with any other version,
+      and a client binding to a version different to the one advertised will be
+      terminated. When the protocol is stabalized, backward compatibility is
+      guaranteed, the '_' prefix will be removed from the name and the version
+      will be reset to 1.
+    </description>
+
+    <enum name="error">
+      <description summary="wl_pointer_lock error values">
+        These errors can be emitted in response to wl_pointer_lock requests.
+      </description>
+      <entry name="already_locked" value="0" summary="pointer was already locked or confined"/>
+    </enum>
+
+    <request name="lock_pointer">
+      <description summary="lock pointer to a position">
+        The lock_pointer request lets the client disable absolute pointer
+        movements, locking the pointer to a position. 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 lock of any kind requested or active on the
+        surface for the seat when requesting a lock, and 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 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 will create a new object wl_locked_pointer which is used to
+        interact with the lock as well as receive updates about its state. See
+        the the description of wl_locked_pointer for further information.
+
+        Note that while a pointer is locked, the wl_pointer objects of the
+        corresponding seat will not emit any motion events, but relative motion
+        events will still be emitted via wl_relative_pointer objects of the
+        same seat.
+      </description>
+
+      <arg name="id" type="new_id" interface="_wl_locked_pointer"/>
+      <arg name="surface" type="object" interface="wl_surface"
+           summary="surface to lock pointer to"/>
+      <arg name="seat" type="object" interface="wl_seat"
+           summary="seat where the pointer should be locked"/>
+      <arg name="region" type="object" interface="wl_region" allow-null="true"
+           summary="region of surface"/>
+    </request>
+
+    <request name="confine_pointer">
+      <description summary="confine pointer to a region">
+        The confine_pointer request lets the client confine the pointer cursor
+        to a given region.
+
+        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 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 wl_confined_pointer which is used
+        to interact with the confinement as well as receive updates about its
+        state. See the the description of wl_confined_pointer for further
+        information.
+      </description>
+
+      <arg name="id" type="new_id" interface="_wl_confined_pointer"/>
+      <arg name="surface" type="object" interface="wl_surface"
+           summary="surface to lock pointer to"/>
+      <arg name="seat" type="object" interface="wl_seat"
+           summary="seat where the pointer should be locked"/>
+      <arg name="region" type="object" interface="wl_region" allow-null="true"
+           summary="region of surface"/>
+    </request>
+  </interface>
+
+  <interface name="_wl_locked_pointer" version="1">
+    <description summary="receive relative pointer motion events">
+      The wl_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 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 wl_locked_pointer object.
+
+      If the compositor decides to unlock the pointer the unlocked event is
+      sent. The wl_locked_pointer object is at this point defunct and should be
+      destroyed.
+
+      When unlocking, the compositor may take the cursor position pointer. If
+      it does, it will not result in any relative motion events emitted via
+      wl_relative_motion.
+
+      If the surface lock was requested on is destroyed and the lock is not yet
+      activated, the wl_locked_pointer object is now defunct and must be
+      destroyed.
+    </description>
+
+    <request name="destroy" type="destructor">
+      <description summary="destroy the locked pointer object">
+        Destroy the locked pointer object. The compositor will unlock the
+        pointer.
+      </description>
+    </request>
+
+    <request name="set_cursor_position_hint">
+      <description summary="set the pointer cursor position hint">
+        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.
+      </description>
+
+      <arg name="surface_x" type="fixed"
+           summary="x coordinate in surface-relative coordinates"/>
+      <arg name="surface_y" type="fixed"
+           summary="y coordinate in surface-relative coordinates"/>
+    </request>
+
+    <request name="set_region">
+      <description summary="set a new lock region">
+        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.
+
+        The new region has no effect on a lock that has already been activated.
+
+        For details about the lock region, see wl_locked_pointer.
+      </description>
+
+      <arg name="region" type="object" interface="wl_region" allow-null="true"
+           summary="region of surface"/>
+    </request>
+
+    <event name="locked">
+      <description summary="lock activation event">
+        Notification that the pointer lock of this seat's pointer is activated.
+      </description>
+    </event>
+
+    <event name="unlocked">
+      <description summary="lock deactivation event">
+        Notification that the pointer lock of seat's pointer is no longer
+        active. This object is now defunct and should be destroyed.
+      </description>
+    </event>
+  </interface>
+
+  <interface name="_wl_confined_pointer" version="1">
+    <description summary="confined pointer object">
+      The wl_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 wl_confined_pointer object.
+
+      If the compositor decides to unconfine the pointer the unconfined event is
+      sent. The wl_confined_pointer object is at this point defunct and should
+      be destroyed.
+    </description>
+
+    <request name="destroy" type="destructor">
+      <description summary="destroy the confined pointer object">
+        Destroy the confined pointer object. The compositor will unconfine the
+        pointer.
+      </description>
+    </request>
+
+    <request name="set_region">
+      <description summary="set a new confine region">
+        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 is
+        warped to a position within the new confinement region. If warped, a
+        wl_pointer.motion event will be emitted, but no
+        wl_relative_pointer.relative_motion event.
+
+        For details about the confine region, see wl_confined_pointer.
+      </description>
+
+      <arg name="region" type="object" interface="wl_region" allow-null="true"
+           summary="region of surface"/>
+    </request>
+
+    <event name="confined">
+      <description summary="enter event">
+        Notification that the pointer confinement of this seat's pointer is
+        activated.
+      </description>
+    </event>
+
+    <event name="unconfined">
+      <description summary="leave event">
+        Notification that the pointer confinement of seat's pointer is no
+        longer active. This object is no defunct and should be destroyed.
+      </description>
+    </event>
+  </interface>
+
+</protocol>
diff --git a/src/compositor.c b/src/compositor.c
index b462531..296be31 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -641,6 +641,7 @@ weston_surface_create(struct weston_compositor *compositor)
 		return NULL;
 
 	wl_signal_init(&surface->destroy_signal);
+	wl_signal_init(&surface->commit_signal);
 
 	surface->compositor = compositor;
 	surface->ref_count = 1;
@@ -667,6 +668,8 @@ weston_surface_create(struct weston_compositor *compositor)
 	weston_matrix_init(&surface->buffer_to_surface_matrix);
 	weston_matrix_init(&surface->surface_to_buffer_matrix);
 
+	wl_list_init(&surface->pointer_locks);
+
 	return surface;
 }
 
@@ -1870,6 +1873,7 @@ weston_surface_destroy(struct weston_surface *surface)
 {
 	struct weston_frame_callback *cb, *next;
 	struct weston_view *ev, *nv;
+	struct weston_pointer_lock *pointer_lock, *next_pointer_lock;
 
 	if (--surface->ref_count > 0)
 		return;
@@ -1897,6 +1901,11 @@ weston_surface_destroy(struct weston_surface *surface)
 
 	weston_presentation_feedback_discard_list(&surface->feedback_list);
 
+	wl_list_for_each_safe(pointer_lock, next_pointer_lock,
+			      &surface->pointer_locks,
+			      link)
+		weston_pointer_lock_destroy(pointer_lock);
+
 	free(surface);
 }
 
@@ -2804,6 +2813,8 @@ weston_surface_commit_state(struct weston_surface *surface,
 	wl_list_insert_list(&surface->feedback_list,
 			    &state->feedback_list);
 	wl_list_init(&state->feedback_list);
+
+	wl_signal_emit(&surface->commit_signal, surface);
 }
 
 static void
diff --git a/src/compositor.h b/src/compositor.h
index 55cc88f..2322b33 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -63,6 +63,7 @@ struct shell_surface;
 struct weston_seat;
 struct weston_output;
 struct input_method;
+struct weston_pointer_lock;
 
 enum weston_keyboard_modifier {
 	MODIFIER_CTRL = (1 << 0),
@@ -349,6 +350,7 @@ struct weston_pointer {
 	struct wl_listener focus_resource_listener;
 	struct wl_signal focus_signal;
 	struct wl_signal motion_signal;
+	struct wl_signal destroy_signal;
 
 	struct weston_view *sprite;
 	struct wl_listener sprite_destroy_listener;
@@ -421,6 +423,9 @@ void
 weston_pointer_set_default_grab(struct weston_pointer *pointer,
 		const struct weston_pointer_grab_interface *interface);
 
+void
+weston_pointer_lock_destroy(struct weston_pointer_lock *lock);
+
 struct weston_keyboard *
 weston_keyboard_create(void);
 void
@@ -723,6 +728,8 @@ struct weston_compositor {
 	int exit_code;
 
 	unsigned int activate_serial;
+
+	struct wl_global *pointer_lock;
 };
 
 struct weston_buffer {
@@ -916,10 +923,38 @@ struct weston_surface_state {
 	struct weston_buffer_viewport buffer_viewport;
 };
 
+struct weston_surface_activation_data {
+	struct weston_surface *surface;
+	struct weston_seat *seat;
+};
+
+struct weston_pointer_lock {
+	struct wl_list link;
+
+	struct weston_surface *surface;
+	struct weston_view *view;
+	pixman_region32_t region;
+	pixman_region32_t pending_region;
+	bool pending_region_set;
+	struct wl_resource *resource;
+	struct weston_pointer_grab grab;
+	struct weston_pointer *pointer;
+
+	bool hint_set;
+	wl_fixed_t x_hint;
+	wl_fixed_t y_hint;
+
+	struct wl_listener pointer_destroy_listener;
+	struct wl_listener surface_destroy_listener;
+	struct wl_listener surface_commit_listener;
+	struct wl_listener surface_activate_listener;
+};
+
 struct weston_surface {
 	struct wl_resource *resource;
 	struct wl_signal destroy_signal; /* callback argument: this surface */
 	struct weston_compositor *compositor;
+	struct wl_signal commit_signal;
 
 	/** Damage in local coordinates from the client, for tex upload. */
 	pixman_region32_t damage;
@@ -999,6 +1034,9 @@ struct weston_surface {
 	const char *role_name;
 
 	struct weston_timeline_object timeline;
+
+	/* An list of per seat pointer locks. */
+	struct wl_list pointer_locks;
 };
 
 struct weston_subsurface {
diff --git a/src/input.c b/src/input.c
index b48a6c9..4a78543 100644
--- a/src/input.c
+++ b/src/input.c
@@ -22,6 +22,7 @@
 
 #include "config.h"
 
+#include <stdbool.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
@@ -34,8 +35,17 @@
 #include "../shared/os-compatibility.h"
 #include "../shared/util.h"
 #include "compositor.h"
+#include "protocol/pointer-lock-server-protocol.h"
 #include "protocol/relative-pointer-server-protocol.h"
 
+enum pointer_lock_type {
+	POINTER_LOCK_TYPE_LOCK,
+	POINTER_LOCK_TYPE_CONFINE,
+};
+
+static void
+maybe_warp_confined_pointer(struct weston_pointer_lock *lock);
+
 static void
 empty_region(pixman_region32_t *region)
 {
@@ -43,6 +53,13 @@ empty_region(pixman_region32_t *region)
 	pixman_region32_init(region);
 }
 
+static void
+region_init_infinite(pixman_region32_t *region)
+{
+	pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
+				  UINT32_MAX, UINT32_MAX);
+}
+
 static void unbind_resource(struct wl_resource *resource)
 {
 	wl_list_remove(wl_resource_get_link(resource));
@@ -243,12 +260,22 @@ weston_pointer_send_relative_motion(struct weston_pointer *pointer,
 }
 
 static void
+weston_pointer_send_motion(struct weston_pointer *pointer, uint32_t time,
+			   wl_fixed_t sx, wl_fixed_t sy)
+{
+	struct wl_list *resource_list;
+	struct wl_resource *resource;
+
+	resource_list = &pointer->focus_resource_list;
+	wl_resource_for_each(resource, resource_list)
+		wl_pointer_send_motion(resource, time, sx, sy);
+}
+
+static void
 default_grab_pointer_motion(struct weston_pointer_grab *grab, uint32_t time,
 			    struct weston_pointer_motion_event *event)
 {
 	struct weston_pointer *pointer = grab->pointer;
-	struct wl_list *resource_list;
-	struct wl_resource *resource;
 	wl_fixed_t x, y;
 	wl_fixed_t old_sx = pointer->sx;
 	wl_fixed_t old_sy = pointer->sy;
@@ -262,40 +289,46 @@ default_grab_pointer_motion(struct weston_pointer_grab *grab, uint32_t time,
 	weston_pointer_move(pointer, event);
 
 	if (old_sx != pointer->sx || old_sy != pointer->sy) {
-		resource_list = &pointer->focus_resource_list;
-		wl_resource_for_each(resource, resource_list) {
-			wl_pointer_send_motion(resource, time,
-					       pointer->sx, pointer->sy);
-		}
+		weston_pointer_send_motion(pointer, time,
+					   pointer->sx, pointer->sy);
 	}
 
 	weston_pointer_send_relative_motion(pointer, time, event);
 }
 
 static void
-default_grab_pointer_button(struct weston_pointer_grab *grab,
-			    uint32_t time, uint32_t button, uint32_t state_w)
+weston_pointer_send_button(struct weston_pointer *pointer,
+			   uint32_t time, uint32_t button, uint32_t state_w)
 {
-	struct weston_pointer *pointer = grab->pointer;
-	struct weston_compositor *compositor = pointer->seat->compositor;
-	struct weston_view *view;
 	struct wl_resource *resource;
 	uint32_t serial;
-	enum wl_pointer_button_state state = state_w;
-	struct wl_display *display = compositor->wl_display;
-	wl_fixed_t sx, sy;
 	struct wl_list *resource_list;
+	struct wl_display *display = pointer->seat->compositor->wl_display;
 
 	resource_list = &pointer->focus_resource_list;
 	if (!wl_list_empty(resource_list)) {
 		serial = wl_display_next_serial(display);
-		wl_resource_for_each(resource, resource_list)
+		wl_resource_for_each(resource, resource_list) {
 			wl_pointer_send_button(resource,
 					       serial,
 					       time,
 					       button,
 					       state_w);
+		}
 	}
+}
+
+static void
+default_grab_pointer_button(struct weston_pointer_grab *grab,
+			    uint32_t time, uint32_t button, uint32_t state_w)
+{
+	struct weston_pointer *pointer = grab->pointer;
+	struct weston_compositor *compositor = pointer->seat->compositor;
+	struct weston_view *view;
+	enum wl_pointer_button_state state = state_w;
+	wl_fixed_t sx, sy;
+
+	weston_pointer_send_button(pointer, time, button, state_w);
 
 	if (pointer->button_count == 0 &&
 	    state == WL_POINTER_BUTTON_STATE_RELEASED) {
@@ -598,6 +631,7 @@ weston_pointer_create(struct weston_seat *seat)
 	wl_signal_init(&pointer->motion_signal);
 	wl_signal_init(&pointer->focus_signal);
 	wl_list_init(&pointer->focus_view_listener.link);
+	wl_signal_init(&pointer->destroy_signal);
 
 	pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
 
@@ -616,6 +650,8 @@ weston_pointer_create(struct weston_seat *seat)
 WL_EXPORT void
 weston_pointer_destroy(struct weston_pointer *pointer)
 {
+	wl_signal_emit(&pointer->destroy_signal, pointer);
+
 	if (pointer->sprite)
 		pointer_unmap_sprite(pointer);
 
@@ -1163,6 +1199,7 @@ weston_surface_activate(struct weston_surface *surface,
 			struct weston_seat *seat)
 {
 	struct weston_compositor *compositor = seat->compositor;
+	struct weston_surface_activation_data activation_data;
 
 	inc_activate_serial(compositor);
 
@@ -1171,7 +1208,11 @@ weston_surface_activate(struct weston_surface *surface,
 		wl_data_device_set_keyboard_focus(seat);
 	}
 
-	wl_signal_emit(&compositor->activate_signal, surface);
+	activation_data = (struct weston_surface_activation_data) {
+		.surface = surface,
+		.seat = seat,
+	};
+	wl_signal_emit(&compositor->activate_signal, &activation_data);
 }
 
 WL_EXPORT void
@@ -2571,6 +2612,677 @@ weston_seat_release(struct weston_seat *seat)
 	wl_signal_emit(&seat->destroy_signal, seat);
 }
 
+static const struct _wl_locked_pointer_interface locked_pointer_interface;
+static const struct _wl_confined_pointer_interface confined_pointer_interface;
+
+static enum pointer_lock_type
+pointer_lock_get_type(struct weston_pointer_lock *lock)
+{
+	if (wl_resource_instance_of(lock->resource,
+				    &_wl_locked_pointer_interface,
+				    &locked_pointer_interface)) {
+		return POINTER_LOCK_TYPE_LOCK;
+	} else if (wl_resource_instance_of(lock->resource,
+					   &_wl_confined_pointer_interface,
+					   &confined_pointer_interface)) {
+		return POINTER_LOCK_TYPE_CONFINE;
+	}
+
+	abort();
+	return 0;
+}
+
+static void
+pointer_lock_notify_activated(struct weston_pointer_lock *lock)
+{
+	struct wl_resource *resource = lock->resource;
+
+	switch (pointer_lock_get_type(lock)) {
+	case POINTER_LOCK_TYPE_LOCK:
+		_wl_locked_pointer_send_locked(resource);
+		break;
+	case POINTER_LOCK_TYPE_CONFINE:
+		_wl_confined_pointer_send_confined(resource);
+		break;
+	}
+}
+
+static void
+pointer_lock_notify_deactivated(struct weston_pointer_lock *lock)
+{
+	struct wl_resource *resource = lock->resource;
+
+	switch (pointer_lock_get_type(lock)) {
+	case POINTER_LOCK_TYPE_LOCK:
+		_wl_locked_pointer_send_unlocked(resource);
+		break;
+	case POINTER_LOCK_TYPE_CONFINE:
+		_wl_confined_pointer_send_unconfined(resource);
+		break;
+	}
+}
+
+static struct weston_pointer_lock *
+get_pointer_lock_for_pointer(struct weston_surface *surface,
+			     struct weston_pointer *pointer)
+{
+	struct weston_pointer_lock *lock;
+
+	wl_list_for_each(lock, &surface->pointer_locks, link) {
+		if (lock->pointer == pointer)
+			return lock;
+	}
+
+	return NULL;
+}
+
+static void
+enable_pointer_lock(struct weston_pointer_lock *lock,
+		    struct weston_view *view)
+{
+	assert(lock->view == NULL);
+	lock->view = view;
+	pointer_lock_notify_activated(lock);
+	weston_pointer_start_grab(lock->pointer, &lock->grab);
+}
+
+static bool
+is_pointer_lock_enabled(struct weston_pointer_lock *lock)
+{
+	return lock->view != NULL;
+}
+
+void
+weston_pointer_lock_destroy(struct weston_pointer_lock *lock)
+{
+	if (is_pointer_lock_enabled(lock)) {
+		pointer_lock_notify_deactivated(lock);
+		weston_pointer_end_grab(lock->grab.pointer);
+	}
+
+	wl_list_remove(&lock->pointer_destroy_listener.link);
+	wl_list_remove(&lock->surface_destroy_listener.link);
+	wl_list_remove(&lock->surface_commit_listener.link);
+	wl_list_remove(&lock->surface_activate_listener.link);
+
+	wl_resource_set_user_data(lock->resource, NULL);
+	pixman_region32_fini(&lock->region);
+	wl_list_remove(&lock->link);
+	free(lock);
+}
+
+static void
+disable_pointer_lock(struct weston_pointer_lock *lock)
+{
+	weston_pointer_lock_destroy(lock);
+}
+
+static bool
+is_within_lock_region(struct weston_pointer_lock *lock,
+		      wl_fixed_t sx, wl_fixed_t sy)
+{
+	struct weston_surface *surface = lock->surface;
+	pixman_region32_t lock_region;
+	bool result;
+
+	pixman_region32_init(&lock_region);
+	pixman_region32_intersect(&lock_region, &surface->input, &lock->region);
+	result = pixman_region32_contains_point(&lock_region,
+						wl_fixed_to_int(sx),
+						wl_fixed_to_int(sy),
+						NULL);
+	pixman_region32_fini(&lock_region);
+
+	return result;
+}
+
+static void
+maybe_enable_pointer_lock(struct weston_pointer_lock *lock)
+{
+	struct weston_surface *surface = lock->surface;
+	struct weston_view *vit;
+	struct weston_view *view = NULL;
+	struct weston_pointer *pointer = lock->pointer;
+	struct weston_seat *seat = pointer->seat;
+	int32_t x, y;
+
+	/* Postpone if no view of the surface was most recently clicked. */
+	wl_list_for_each(vit, &surface->views, surface_link) {
+		if (vit->click_to_activate_serial ==
+		    surface->compositor->activate_serial) {
+			view = vit;
+		}
+	}
+	if (view == NULL)
+		return;
+
+	/* Postpone if surface doesn't have keyboard focus. */
+	if (seat->keyboard->focus != surface)
+		return;
+
+	/* Postpone lock if the pointer is not within the lock region. */
+	weston_view_from_global(view,
+				wl_fixed_to_int(pointer->x),
+				wl_fixed_to_int(pointer->y),
+				&x, &y);
+	if (!is_within_lock_region(lock,
+				   wl_fixed_from_int(x),
+				   wl_fixed_from_int(y)))
+		return;
+
+	enable_pointer_lock(lock, view);
+}
+
+static void
+locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
+{
+}
+
+static void
+locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
+				   uint32_t time,
+				   struct weston_pointer_motion_event *event)
+{
+	weston_pointer_send_relative_motion(grab->pointer, time, event);
+}
+
+static void
+locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
+				   uint32_t time,
+				   uint32_t button,
+				   uint32_t state_w)
+{
+	weston_pointer_send_button(grab->pointer, time, button, state_w);
+}
+
+static void
+locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
+				 uint32_t time, uint32_t axis, wl_fixed_t value)
+{
+	weston_pointer_send_axis(grab->pointer, time, axis, value);
+}
+
+static void
+locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
+{
+	struct weston_pointer_lock *lock =
+		container_of(grab, struct weston_pointer_lock, grab);
+
+	disable_pointer_lock(lock);
+}
+
+static const struct weston_pointer_grab_interface
+				locked_pointer_grab_interface = {
+	locked_pointer_grab_pointer_focus,
+	locked_pointer_grab_pointer_motion,
+	locked_pointer_grab_pointer_button,
+	locked_pointer_grab_pointer_axis,
+	locked_pointer_grab_pointer_cancel,
+};
+
+static void
+pointer_lock_lock_resource_destroyed(struct wl_resource *resource)
+{
+	struct weston_pointer_lock *lock = wl_resource_get_user_data(resource);
+
+	if (!lock)
+		return;
+
+	disable_pointer_lock(lock);
+}
+
+static void
+pointer_lock_surface_activate(struct wl_listener *listener, void *data)
+{
+	struct weston_surface_activation_data *activation = data;
+	struct weston_pointer *pointer = activation->seat->pointer;
+	struct weston_surface *focus = activation->surface;
+	struct weston_pointer_lock *lock =
+		container_of(listener, struct weston_pointer_lock,
+			     surface_activate_listener);
+	bool is_lock_surface;
+
+	is_lock_surface = get_pointer_lock_for_pointer(focus, pointer) == lock;
+
+	if (is_lock_surface &&  !is_pointer_lock_enabled(lock))
+		maybe_enable_pointer_lock(lock);
+	else if (!is_lock_surface && is_pointer_lock_enabled(lock))
+		disable_pointer_lock(lock);
+}
+
+static void
+pointer_lock_pointer_destroyed(struct wl_listener *listener, void *data)
+{
+	struct weston_pointer_lock *lock =
+		container_of(listener, struct weston_pointer_lock,
+			     pointer_destroy_listener);
+
+	disable_pointer_lock(lock);
+}
+
+static void
+pointer_lock_surface_destroyed(struct wl_listener *listener, void *data)
+{
+	struct weston_pointer_lock *lock =
+		container_of(listener, struct weston_pointer_lock,
+			     surface_destroy_listener);
+
+	disable_pointer_lock(lock);
+}
+
+static void
+pointer_lock_surface_committed(struct wl_listener *listener, void *data)
+{
+	struct weston_pointer_lock *lock =
+		container_of(listener, struct weston_pointer_lock,
+			     surface_commit_listener);
+
+	if (lock->pending_region_set) {
+		lock->pending_region_set = false;
+		pixman_region32_copy(&lock->region, &lock->pending_region);
+		pixman_region32_fini(&lock->pending_region);
+		pixman_region32_init(&lock->pending_region);
+	}
+
+	if (pointer_lock_get_type(lock) == POINTER_LOCK_TYPE_CONFINE &&
+	    is_pointer_lock_enabled(lock))
+		maybe_warp_confined_pointer(lock);
+}
+
+static struct weston_pointer_lock *
+weston_pointer_lock_create(struct weston_surface *surface,
+			   struct weston_pointer *pointer,
+			   struct weston_region *region,
+			   struct wl_resource *cr,
+			   const struct weston_pointer_grab_interface *grab_interface)
+{
+	struct weston_pointer_lock *lock;
+
+	lock = zalloc(sizeof *lock);
+	if (!lock)
+		return NULL;
+
+	pixman_region32_init(&lock->region);
+	pixman_region32_init(&lock->pending_region);
+	wl_list_insert(&surface->pointer_locks, &lock->link);
+	lock->surface = surface;
+	lock->pointer = pointer;
+	lock->resource = cr;
+	lock->grab.interface = grab_interface;
+	if (region) {
+		pixman_region32_copy(&lock->region,
+				     &region->region);
+	} else {
+		pixman_region32_fini(&lock->region);
+		region_init_infinite(&lock->region);
+	}
+
+	lock->surface_activate_listener.notify = pointer_lock_surface_activate;
+	lock->surface_destroy_listener.notify = pointer_lock_surface_destroyed;
+	lock->surface_commit_listener.notify = pointer_lock_surface_committed;
+	lock->pointer_destroy_listener.notify = pointer_lock_pointer_destroyed;
+
+	wl_signal_add(&surface->compositor->activate_signal,
+		      &lock->surface_activate_listener);
+	wl_signal_add(&pointer->destroy_signal,
+		      &lock->pointer_destroy_listener);
+	wl_signal_add(&surface->destroy_signal,
+		      &lock->surface_destroy_listener);
+	wl_signal_add(&surface->commit_signal,
+		      &lock->surface_commit_listener);
+
+	return lock;
+}
+
+static void
+init_pointer_lock(struct wl_resource *pointer_lock_resource,
+		  uint32_t id,
+		  struct weston_surface *surface,
+		  struct weston_seat *seat,
+		  struct weston_region *region,
+		  const struct wl_interface *interface,
+		  const void *implementation,
+		  const struct weston_pointer_grab_interface *grab_interface)
+{
+	struct wl_client *client =
+		wl_resource_get_client(pointer_lock_resource);
+	struct weston_pointer *pointer = seat->pointer;
+	struct wl_resource *cr;
+	struct weston_pointer_lock *lock;
+
+	if (get_pointer_lock_for_pointer(surface, pointer)) {
+		wl_resource_post_error(pointer_lock_resource,
+				       WL_DISPLAY_ERROR_INVALID_OBJECT,
+				       "the pointer as already requested to be "
+				       "locked or confined on that surface");
+		return;
+	}
+
+        cr = wl_resource_create(client, interface,
+				wl_resource_get_version(pointer_lock_resource),
+				id);
+	if (cr == NULL) {
+		wl_client_post_no_memory(client);
+		return;
+	}
+
+	lock = weston_pointer_lock_create(surface, pointer, region,
+					  cr, grab_interface);
+	if (lock == NULL) {
+		wl_client_post_no_memory(client);
+		return;
+	}
+
+	wl_resource_set_implementation(cr, implementation, lock,
+				       pointer_lock_lock_resource_destroyed);
+
+	maybe_enable_pointer_lock(lock);
+}
+
+static void
+locked_pointer_destroy(struct wl_client *client,
+		       struct wl_resource *resource)
+{
+	struct weston_pointer_lock *lock = wl_resource_get_user_data(resource);
+	wl_fixed_t x_hint = lock->x_hint;
+	wl_fixed_t y_hint = lock->y_hint;
+	wl_fixed_t x, y;
+
+	if (lock->view && lock->hint_set &&
+	    is_within_lock_region(lock, x_hint, y_hint)) {
+		weston_view_to_global_fixed(lock->view,
+					    x_hint, y_hint,
+					    &x, &y);
+		weston_pointer_move_to(lock->pointer, x, y);
+	}
+	wl_resource_destroy(resource);
+}
+
+static void
+locked_pointer_set_cursor_position_hint(struct wl_client *client,
+					struct wl_resource *resource,
+					wl_fixed_t surface_x,
+					wl_fixed_t surface_y)
+{
+	struct weston_pointer_lock *lock = wl_resource_get_user_data(resource);
+
+	/* Ignore a set cursor hint that was already sent after the lock
+	 * was cancelled. */
+	if (!lock->resource ||
+	    lock->resource != resource)
+		return;
+
+	lock->hint_set = true;
+	lock->x_hint = surface_x;
+	lock->y_hint = surface_y;
+}
+
+static void
+locked_pointer_set_region(struct wl_client *client,
+			  struct wl_resource *resource,
+			  struct wl_resource *region_resource)
+{
+	struct weston_pointer_lock *lock = wl_resource_get_user_data(resource);
+	struct weston_region *region = region_resource ?
+		wl_resource_get_user_data(region_resource) : NULL;
+
+	if (region) {
+		pixman_region32_copy(&lock->pending_region, &region->region);
+	} else {
+		pixman_region32_fini(&lock->pending_region);
+		region_init_infinite(&lock->pending_region);
+	}
+	lock->pending_region_set = true;
+}
+
+
+static const struct _wl_locked_pointer_interface locked_pointer_interface = {
+	locked_pointer_destroy,
+	locked_pointer_set_cursor_position_hint,
+	locked_pointer_set_region,
+};
+
+static void
+pointer_lock_lock_pointer(struct wl_client *client,
+			  struct wl_resource *resource,
+			  uint32_t id,
+			  struct wl_resource *surface_resource,
+			  struct wl_resource *seat_resource,
+			  struct wl_resource *region_resource)
+{
+	struct weston_surface *surface =
+		wl_resource_get_user_data(surface_resource);
+	struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
+	struct weston_region *region = region_resource ?
+		wl_resource_get_user_data(region_resource) : NULL;
+
+	init_pointer_lock(resource, id, surface, seat, region,
+			  &_wl_locked_pointer_interface,
+			  &locked_pointer_interface,
+			  &locked_pointer_grab_interface);
+}
+
+static void
+confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)
+{
+}
+
+static void
+weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,
+				     struct weston_pointer_motion_event *event,
+				     pixman_region32_t *region,
+				     wl_fixed_t *clamped_x,
+				     wl_fixed_t *clamped_y)
+{
+	wl_fixed_t x, y;
+	wl_fixed_t sx, sy;
+	wl_fixed_t min_sx = wl_fixed_from_int(region->extents.x1);
+	wl_fixed_t max_sx = wl_fixed_from_int(region->extents.x2 - 1);
+	wl_fixed_t max_sy = wl_fixed_from_int(region->extents.y2 - 1);
+	wl_fixed_t min_sy = wl_fixed_from_int(region->extents.y1);
+
+	weston_pointer_motion_to_abs(pointer, event, &x, &y);
+	weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);
+
+	if (sx < min_sx)
+		sx = min_sx;
+	else if (sx > max_sx)
+		sx = max_sx;
+
+	if (sy < min_sy)
+		sy = min_sy;
+	else if (sy > max_sy)
+		sy = max_sy;
+
+	weston_view_to_global_fixed(pointer->focus, sx, sy,
+				    clamped_x, clamped_y);
+}
+
+static void
+maybe_warp_confined_pointer(struct weston_pointer_lock *lock)
+{
+	wl_fixed_t x;
+	wl_fixed_t y;
+	wl_fixed_t sx;
+	wl_fixed_t sy;
+
+	weston_view_from_global_fixed(lock->view,
+				      lock->pointer->x,
+				      lock->pointer->y,
+				      &sx,
+				      &sy);
+
+	if (!is_within_lock_region(lock, sx, sy)) {
+		pixman_region32_t *region = &lock->region;
+		wl_fixed_t min_sx = wl_fixed_from_int(region->extents.x1);
+		wl_fixed_t max_sx = wl_fixed_from_int(region->extents.x2 - 1);
+		wl_fixed_t max_sy = wl_fixed_from_int(region->extents.y2 - 1);
+		wl_fixed_t min_sy = wl_fixed_from_int(region->extents.y1);
+
+		if (sx < min_sx)
+			sx = min_sx;
+		else if (sx > max_sx)
+			sx = max_sx;
+
+		if (sy < min_sy)
+			sy = min_sy;
+		else if (sy > max_sy)
+			sy = max_sy;
+
+		weston_view_to_global_fixed(lock->view, sx, sy, &x, &y);
+		weston_pointer_move_to(lock->pointer, x, y);
+	}
+}
+
+static void
+confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,
+				     uint32_t time,
+				     struct weston_pointer_motion_event *event)
+{
+	struct weston_pointer_lock *lock =
+		container_of(grab, struct weston_pointer_lock, grab);
+	struct weston_pointer *pointer = grab->pointer;
+	struct weston_surface *surface;
+	wl_fixed_t x, y;
+	wl_fixed_t old_sx = pointer->sx;
+	wl_fixed_t old_sy = pointer->sy;
+	pixman_region32_t confine_region;
+
+	assert(pointer->focus);
+	assert(pointer->focus->surface == lock->surface);
+
+	surface = pointer->focus->surface;
+
+	pixman_region32_init(&confine_region);
+	pixman_region32_intersect(&confine_region,
+				  &surface->input,
+				  &lock->region);
+	weston_pointer_clamp_event_to_region(pointer, event,
+					     &confine_region, &x, &y);
+	weston_pointer_move_to(pointer, x, y);
+	pixman_region32_fini(&confine_region);
+
+	weston_view_from_global_fixed(pointer->focus, x, y,
+				      &pointer->sx, &pointer->sy);
+
+	if (old_sx != pointer->sx || old_sy != pointer->sy) {
+		weston_pointer_send_motion(pointer, time,
+					   pointer->sx, pointer->sy);
+	}
+
+	weston_pointer_send_relative_motion(pointer, time, event);
+}
+
+static void
+confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,
+				     uint32_t time,
+				     uint32_t button,
+				     uint32_t state_w)
+{
+	weston_pointer_send_button(grab->pointer, time, button, state_w);
+}
+
+static void
+confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,
+				   uint32_t time,
+				   uint32_t axis,
+				   wl_fixed_t value)
+{
+	weston_pointer_send_axis(grab->pointer, time, axis, value);
+}
+
+static void
+confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)
+{
+	struct weston_pointer_lock *lock =
+		container_of(grab, struct weston_pointer_lock, grab);
+
+	disable_pointer_lock(lock);
+}
+
+static const struct weston_pointer_grab_interface
+				confined_pointer_grab_interface = {
+	confined_pointer_grab_pointer_focus,
+	confined_pointer_grab_pointer_motion,
+	confined_pointer_grab_pointer_button,
+	confined_pointer_grab_pointer_axis,
+	confined_pointer_grab_pointer_cancel,
+};
+
+static void
+confined_pointer_destroy(struct wl_client *client,
+			 struct wl_resource *resource)
+{
+	wl_resource_destroy(resource);
+}
+
+static void
+confined_pointer_set_region(struct wl_client *client,
+			    struct wl_resource *resource,
+			    struct wl_resource *region_resource)
+{
+	struct weston_pointer_lock *lock = wl_resource_get_user_data(resource);
+	struct weston_region *region = region_resource ?
+		wl_resource_get_user_data(region_resource) : NULL;
+
+	if (region) {
+		pixman_region32_copy(&lock->pending_region, &region->region);
+	} else {
+		pixman_region32_fini(&lock->pending_region);
+		region_init_infinite(&lock->pending_region);
+	}
+	lock->pending_region_set = true;
+}
+
+static const struct _wl_confined_pointer_interface confined_pointer_interface = {
+	confined_pointer_destroy,
+	confined_pointer_set_region,
+};
+
+static void
+pointer_lock_confine_pointer(struct wl_client *client,
+			     struct wl_resource *resource,
+			     uint32_t id,
+			     struct wl_resource *surface_resource,
+			     struct wl_resource *seat_resource,
+			     struct wl_resource *region_resource)
+{
+	struct weston_surface *surface =
+		wl_resource_get_user_data(surface_resource);
+	struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
+	struct weston_region *region = region_resource ?
+		wl_resource_get_user_data(region_resource) : NULL;
+
+	if ((region && pixman_region32_n_rects(&region->region) != 1) ||
+	    pixman_region32_n_rects(&surface->input) != 1) {
+		weston_log("warning: confinement only implemented for"
+			   "rectangular regions\n");
+		return;
+	}
+
+	init_pointer_lock(resource, id, surface, seat, region,
+			  &_wl_confined_pointer_interface,
+			  &confined_pointer_interface,
+			  &confined_pointer_grab_interface);
+}
+
+static const struct _wl_pointer_lock_interface pointer_lock_interface = {
+	pointer_lock_lock_pointer,
+	pointer_lock_confine_pointer,
+};
+
+static void
+bind_pointer_lock(struct wl_client *client, void *data,
+		  uint32_t version, uint32_t id)
+{
+	struct wl_resource *resource;
+
+	resource = wl_resource_create(client, &_wl_pointer_lock_interface,
+				      1, id);
+	wl_resource_set_implementation(resource, &pointer_lock_interface,
+				       NULL, NULL);
+}
+
 int
 weston_input_init(struct weston_compositor *compositor)
 {
@@ -2579,5 +3291,10 @@ weston_input_init(struct weston_compositor *compositor)
 			      compositor, bind_relative_pointer_manager))
 		return -1;
 
+	if (!wl_global_create(compositor->wl_display,
+			      &_wl_pointer_lock_interface, 1,
+			      NULL, bind_pointer_lock))
+		return -1;
+
 	return 0;
 }
-- 
2.1.4



More information about the wayland-devel mailing list