<div dir="ltr">A couple of doc comments below, but the protocol otherwise looks pretty good.  Again, I've only glanced at the implementation.<br><div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 2, 2014 at 5:49 AM, Jonas Ådahl <span dir="ltr"><<a href="mailto:jadahl@gmail.com" target="_blank">jadahl@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This patch introduces a new protocol for locking and confining a<br>
pointer. It consists of a new global object with two requests; one for<br>
locking the surface to a position, one for confining the pointer to a<br>
given region.<br>
<br>
See pointer-lock.xml for details of the protocol.<br>
<br>
In this patch, only the locking part is fully implemented as in<br>
specified in the protocol, while confinement is only implemented for<br>
when the union of the passed region and the input region of the confined<br>
surface is a single rectangle.<br>
<br>
Note that the interfaces are prefixed with an underscore in order to<br>
avoid future incompatibilities with a future stable interface with an<br>
equivalent name.<br>
<br>
Signed-off-by: Jonas Ådahl <<a href="mailto:jadahl@gmail.com">jadahl@gmail.com</a>><br>
---<br>
<br>
What should the serial in locked_pointer_set_cursor_position_hint be<br>
checked against? It doesn't seem to make sense to use the same method as<br>
in pointer_set_cursor. As far as I understand, it should be equal to the<br>
last serial sent via that pointer device, but I can't see we do that<br>
anywhere else. What am I missing?<br>
<br>
<br>
 Makefile.am               |   3 +<br>
 protocol/pointer-lock.xml | 208 ++++++++++++++++<br>
 src/compositor.c          |   4 +<br>
 src/compositor.h          |  21 ++<br>
 src/input.c               | 589 ++++++++++++++++++++++++++++++++++++++++++++--<br>
 5 files changed, 806 insertions(+), 19 deletions(-)<br>
 create mode 100644 protocol/pointer-lock.xml<br>
<br>
diff --git a/Makefile.am b/Makefile.am<br>
index 860564d..8adc343 100644<br>
--- a/Makefile.am<br>
+++ b/Makefile.am<br>
@@ -82,6 +82,8 @@ nodist_weston_SOURCES =                                       \<br>
        protocol/presentation_timing-server-protocol.h  \<br>
        protocol/scaler-protocol.c                      \<br>
        protocol/scaler-server-protocol.h               \<br>
+       protocol/pointer-lock-protocol.c                \<br>
+       protocol/pointer-lock-server-protocol.h         \<br>
        protocol/relative-pointer-protocol.c            \<br>
        protocol/relative-pointer-server-protocol.h<br>
<br>
@@ -1006,6 +1008,7 @@ EXTRA_DIST +=                                     \<br>
        protocol/fullscreen-shell.xml           \<br>
        protocol/presentation_timing.xml        \<br>
        protocol/scaler.xml                     \<br>
+       protocol/pointer-lock.xml               \<br>
        protocol/relative-pointer.xml<br>
<br>
 man_MANS = weston.1 weston.ini.5<br>
diff --git a/protocol/pointer-lock.xml b/protocol/pointer-lock.xml<br>
new file mode 100644<br>
index 0000000..3233ede<br>
--- /dev/null<br>
+++ b/protocol/pointer-lock.xml<br>
@@ -0,0 +1,208 @@<br>
+<?xml version="1.0" encoding="UTF-8"?><br>
+<protocol name="pointer_lock"><br>
+<br>
+  <copyright><br>
+    Copyright © 2014      Jonas Ådahl<br>
+<br>
+    Permission to use, copy, modify, distribute, and sell this<br>
+    software and its documentation for any purpose is hereby granted<br>
+    without fee, provided that the above copyright notice appear in<br>
+    all copies and that both that copyright notice and this permission<br>
+    notice appear in supporting documentation, and that the name of<br>
+    the copyright holders not be used in advertising or publicity<br>
+    pertaining to distribution of the software without specific,<br>
+    written prior permission.  The copyright holders make no<br>
+    representations about the suitability of this software for any<br>
+    purpose.  It is provided "as is" without express or implied<br>
+    warranty.<br>
+<br>
+    THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS<br>
+    SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND<br>
+    FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY<br>
+    SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES<br>
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN<br>
+    AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,<br>
+    ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF<br>
+    THIS SOFTWARE.<br>
+  </copyright><br>
+<br>
+  <interface name="_wl_pointer_lock" version="1"><br>
+    <description summary="lock pointer to a surface"><br>
+      The global interface exposing pointer locking functionality. It exposes<br>
+      two requests; lock_pointer for locking the pointer to its position, and<br>
+      confine_pointer for locking the pointer to a region.<br>
+<br>
+      The lock_pointer and confine_pointer creates the objects wl_locked_pointer<br>
+      and wl_confined_pointer respectively, and the client can use these objects<br>
+      to interact with the lock.<br>
+<br>
+      There may not be another lock of any kind active when requesting a lock,<br>
+      and if there is, an error will be raised.<br>
+    </description><br>
+<br>
+    <request name="lock_pointer"><br>
+      <description summary="lock pointer to a position"><br>
+        The lock_pointer request lets the client disable absolute pointer<br>
+        movements, locking the pointer to a position.<br>
+<br>
+        There may not be another lock of any kind active when requesting a lock,<br>
+        and if there is, an error will be raised.<br>
+<br>
+        The intersection of the region passed with this request and the input<br>
+        region of the surface is used to determine where the pointer must be<br>
+        in order for the lock to activate. It is up to the compositor to warp<br>
+        the pointer, or require some kind of user interaction for the lock to<br>
+        activate. If the region is null, then an infinit region is used.<br></blockquote><div><br>It might be better to say "the input region is used".  The infinite region will be clampped to this region so it's effectively the same.  However, that makes it blatantly clear to clients and implementers that they can't use this interface to globally lock and steal the cursor.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+        The request will create a new object wl_locked_pointer which is used to<br>
+        interact with the lock as well as receive updates about its state. See<br>
+        the the description of wl_locked_pointer for further information.<br>
+<br>
+        Note that while a locked pointer doesn't move its absolute position, it<br>
+        may still emit relative motion events via the wl_relative_pointer<br>
+        object.<br>
+      </description><br>
+<br>
+      <arg name="id" type="new_id" interface="_wl_locked_pointer"/><br>
+      <arg name="surface" type="object" interface="wl_surface"<br>
+           summary="surface to lock pointer to"/><br>
+      <arg name="seat" type="object" interface="wl_seat"<br>
+           summary="seat where the pointer should be locked"/><br>
+      <arg name="region" type="object" interface="wl_region" allow-null="true"<br>
+           summary="region of surface"/><br>
+    </request><br>
+<br>
+    <request name="confine_pointer"><br>
+      <description summary="confine pointer to a region"><br>
+        The confine_pointer request lets the client confine the pointer cursor<br>
+        to a given region.<br>
+<br>
+        The intersection of the region passed with this request and the input<br>
+        region of the surface is used to determine where the pointer must be<br>
+        in order for the confinement to activate. It is up to the compositor to<br>
+        warp the pointer, or require some kind of user interaction for the<br>
+        confinement to activate. If the region is null, then an infinite region<br>
+        is used.<br></blockquote><div><br></div><div>Same comment as above.  Input region might be better<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+        The request will create a new object wl_confined_pointer which is used<br>
+        to interact with the confinement as well as receive updates about its<br>
+        state. See the the description of wl_confined_pointer for further<br>
+        information.<br>
+      </description><br>
+<br>
+      <arg name="id" type="new_id" interface="_wl_confined_pointer"/><br>
+      <arg name="surface" type="object" interface="wl_surface"<br>
+           summary="surface to lock pointer to"/><br>
+      <arg name="seat" type="object" interface="wl_seat"<br>
+           summary="seat where the pointer should be locked"/><br>
+      <arg name="region" type="object" interface="wl_region" allow-null="true"<br>
+           summary="region of surface"/><br>
+    </request><br>
+<br>
+  </interface><br>
+<br>
+  <interface name="_wl_locked_pointer" version="1"><br>
+    <description summary="receive relative pointer motion events"><br>
+      The wl_locked_pointer interface represents a locked pointer state.<br>
+<br>
+      While the lock of this object is active, the pointer of the associated<br>
+      seat will not move.<br>
+<br>
+      This object will send the event 'locked' when the lock is activated.<br>
+      Whenever the lock is activated, it is guaranteed that the locked surface<br>
+      will already have received pointer focus and that the pointer will be<br>
+      within the region passed to the request creating this object.<br>
+<br>
+      To unlock the pointer, send the destroy request. This will also destroy<br>
+      the wl_locked_pointer object.<br>
+<br>
+      If the compositor decides to unlock the pointer the unlocked event is sent.<br>
+      The wl_locked_pointer object is at this point defunct and should be<br>
+      destoryed.<br></blockquote><div><br></div><div>"destroyed"<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+      When unlocking, the compositor may or may not take the cursor position<br>
+      hint provided using the set_cursor_position_hint request and warp the<br>
+      pointer. If it does, it will not result in any relative motion events.<br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+    </description><br>
+<br>
+    <request name="set_cursor_position_hint"><br>
+      <description summary="set the pointer cursor position hint"><br>
+        Set the cursor position hint relative to the top left corner of the<br>
+        surface.<br></blockquote><div><br></div><div>I think we want a bit more description here.  In particular, if clients are using relative motion to draw their own cursor, they should be periodically updating the hint.  We should also tell compositors what they can/should do with it.  It's not 100% clear from your description what the hint does.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+      </description><br>
+<br>
+      <arg name="serial" type="uint" summary="serial of the enter event"/><br></blockquote><div><br></div><div>I think we may want this to be the serial of the "locked" event.  It shouldn't matter much since once it's locked it shouldn't ever leave until after the lock is broken.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+      <arg name="surface_x" type="fixed"<br>
+           summary="x coordinate in surface-relative coordinates"/><br>
+      <arg name="surface_y" type="fixed"<br>
+           summary="y coordinate in surface-relative coordinates"/><br>
+    </request><br>
+<br>
+    <request name="destroy" type="destructor"><br>
+      <description summary="destroy the locked pointer object"><br>
+        Destroy the locked pointer object. The compositor will unlock the<br>
+        pointer.<br>
+      </description><br>
+    </request><br>
+<br>
+    <event name="locked"><br>
+      <description summary="enter event"><br>
+        Notification that the pointer lock of this seat's pointer is activated.<br>
+      </description><br>
+<br>
+      <arg name="serial" type="uint"/><br>
+    </event><br>
+<br>
+    <event name="unlocked"><br>
+      <description summary="leave event"><br>
+        Notification that the pointer lock of seat's pointer is no longer<br>
+        active. This object is no defunct and should be destroyed.<br>
+      </description><br>
+    </event><br>
+  </interface><br>
+<br>
+  <interface name="_wl_confined_pointer" version="1"><br>
+    <description summary="confined pointer object"><br>
+      The wl_confined_pointer interface represents a confined pointer state.<br>
+<br>
+      This object will send the event 'confined' when the confinement is<br>
+      activated. Whenever the confinement is activated, it is guaranteed that<br>
+      the surface the pointer is confined to will already have received pointer<br>
+      focus and that the pointer will be within the region passed to the request<br>
+      creating this object. It is up to the compositor to decide whether this<br>
+      requires some user interaction and if the pointer will warp to within the<br>
+      passed region if outside.<br>
+<br>
+      To unconfine the pointer, send the destroy request. This will also destroy<br>
+      the wl_confined_pointer object.<br>
+<br>
+      If the compositor decides to unconfine the pointer the unconfined event is<br>
+      sent. The wl_confined_pointer object is at this point defunct and should<br>
+      be destoryed.<br>
+    </description><br>
+<br>
+    <request name="destroy" type="destructor"><br>
+      <description summary="destroy the confined pointer object"><br>
+        Destroy the confined pointer object. The compositor will unconfine the<br>
+        pointer.<br>
+      </description><br>
+    </request><br>
+<br>
+    <event name="confined"><br>
+      <description summary="enter event"><br>
+        Notification that the pointer confinement of this seat's pointer is<br>
+        activated.<br>
+      </description><br>
+<br>
+      <arg name="serial" type="uint"/><br>
+    </event><br>
+<br>
+    <event name="unconfined"><br>
+      <description summary="leave event"><br>
+        Notification that the pointer confinement of seat's pointer is no<br>
+        longer active. This object is no defunct and should be destroyed.<br>
+      </description><br>
+    </event><br>
+  </interface><br>
+<br>
+</protocol><br>
diff --git a/src/compositor.c b/src/compositor.c<br>
index 767cb26..242bdf6 100644<br>
--- a/src/compositor.c<br>
+++ b/src/compositor.c<br>
@@ -645,6 +645,8 @@ weston_surface_create(struct weston_compositor *compositor)<br>
        wl_list_init(&surface->subsurface_list);<br>
        wl_list_init(&surface->subsurface_list_pending);<br>
<br>
+       region_init_infinite(&surface->pointer_lock.region);<br>
+<br>
        return surface;<br>
 }<br>
<br>
@@ -1659,6 +1661,8 @@ weston_surface_destroy(struct weston_surface *surface)<br>
<br>
        weston_presentation_feedback_discard_list(&surface->feedback_list);<br>
<br>
+       pixman_region32_fini(&surface->pointer_lock.region);<br>
+<br>
        free(surface);<br>
 }<br>
<br>
diff --git a/src/compositor.h b/src/compositor.h<br>
index b373ecf..9a6fa7e 100644<br>
--- a/src/compositor.h<br>
+++ b/src/compositor.h<br>
@@ -339,6 +339,7 @@ struct weston_pointer {<br>
        struct wl_listener focus_resource_listener;<br>
        struct wl_signal focus_signal;<br>
        struct wl_signal motion_signal;<br>
+       struct wl_signal destroy_signal;<br>
<br>
        struct weston_view *sprite;<br>
        struct wl_listener sprite_destroy_listener;<br>
@@ -692,6 +693,8 @@ struct weston_compositor {<br>
        int exit_code;<br>
<br>
        unsigned int activate_serial;<br>
+<br>
+       struct wl_global *pointer_lock;<br>
 };<br>
<br>
 struct weston_buffer {<br>
@@ -943,6 +946,24 @@ struct weston_surface {<br>
         * and replace role_name with configure.<br>
         */<br>
        const char *role_name;<br>
+<br>
+       struct {<br>
+               struct weston_view *view;<br>
+               pixman_region32_t region;<br>
+               struct wl_resource *resource;<br>
+               struct weston_pointer_grab grab;<br>
+               struct weston_pointer *pointer;<br>
+<br>
+               bool hint_set;<br>
+               wl_fixed_t x_hint;<br>
+               wl_fixed_t y_hint;<br>
+<br>
+               struct wl_listener pointer_destroy_listener;<br>
+               struct wl_listener surface_destroy_listener;<br>
+<br>
+               struct wl_listener keyboard_focus_listener;<br>
+               struct wl_listener surface_activate_listener;<br>
+       } pointer_lock;<br>
 };<br>
<br>
 struct weston_subsurface {<br>
diff --git a/src/input.c b/src/input.c<br>
index 9d02eb8..63283f2 100644<br>
--- a/src/input.c<br>
+++ b/src/input.c<br>
@@ -22,6 +22,7 @@<br>
<br>
 #include "config.h"<br>
<br>
+#include <stdbool.h><br>
 #include <stdlib.h><br>
 #include <stdint.h><br>
 #include <string.h><br>
@@ -33,6 +34,7 @@<br>
<br>
 #include "../shared/os-compatibility.h"<br>
 #include "compositor.h"<br>
+#include "protocol/pointer-lock-server-protocol.h"<br>
 #include "protocol/relative-pointer-server-protocol.h"<br>
<br>
 static void<br>
@@ -42,6 +44,13 @@ empty_region(pixman_region32_t *region)<br>
        pixman_region32_init(region);<br>
 }<br>
<br>
+static void<br>
+region_init_infinite(pixman_region32_t *region)<br>
+{<br>
+       pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,<br>
+                                 UINT32_MAX, UINT32_MAX);<br>
+}<br>
+<br>
 static void unbind_resource(struct wl_resource *resource)<br>
 {<br>
        wl_list_remove(wl_resource_get_link(resource));<br>
@@ -227,12 +236,22 @@ weston_pointer_send_relative_motion(struct weston_pointer *pointer,<br>
 }<br>
<br>
 static void<br>
+weston_pointer_send_motion(struct weston_pointer *pointer, uint32_t time,<br>
+                          wl_fixed_t sx, wl_fixed_t sy)<br>
+{<br>
+       struct wl_list *resource_list;<br>
+       struct wl_resource *resource;<br>
+<br>
+       resource_list = &pointer->focus_resource_list;<br>
+       wl_resource_for_each(resource, resource_list)<br>
+               wl_pointer_send_motion(resource, time, sx, sy);<br>
+}<br>
+<br>
+static void<br>
 default_grab_pointer_motion(struct weston_pointer_grab *grab, uint32_t time,<br>
                            struct weston_pointer_motion_event *event)<br>
 {<br>
        struct weston_pointer *pointer = grab->pointer;<br>
-       struct wl_list *resource_list;<br>
-       struct wl_resource *resource;<br>
        wl_fixed_t x, y;<br>
        wl_fixed_t old_sx = pointer->sx;<br>
        wl_fixed_t old_sy = pointer->sy;<br>
@@ -246,40 +265,46 @@ default_grab_pointer_motion(struct weston_pointer_grab *grab, uint32_t time,<br>
        weston_pointer_move(pointer, event);<br>
<br>
        if (old_sx != pointer->sx || old_sy != pointer->sy) {<br>
-               resource_list = &pointer->focus_resource_list;<br>
-               wl_resource_for_each(resource, resource_list) {<br>
-                       wl_pointer_send_motion(resource, time,<br>
-                                              pointer->sx, pointer->sy);<br>
-               }<br>
+               weston_pointer_send_motion(pointer, time,<br>
+                                          pointer->sx, pointer->sy);<br>
        }<br>
<br>
        weston_pointer_send_relative_motion(pointer, time, event);<br>
 }<br>
<br>
 static void<br>
-default_grab_pointer_button(struct weston_pointer_grab *grab,<br>
-                           uint32_t time, uint32_t button, uint32_t state_w)<br>
+weston_pointer_send_button(struct weston_pointer *pointer,<br>
+                          uint32_t time, uint32_t button, uint32_t state_w)<br>
 {<br>
-       struct weston_pointer *pointer = grab->pointer;<br>
-       struct weston_compositor *compositor = pointer->seat->compositor;<br>
-       struct weston_view *view;<br>
        struct wl_resource *resource;<br>
        uint32_t serial;<br>
-       enum wl_pointer_button_state state = state_w;<br>
-       struct wl_display *display = compositor->wl_display;<br>
-       wl_fixed_t sx, sy;<br>
        struct wl_list *resource_list;<br>
+       struct wl_display *display = pointer->seat->compositor->wl_display;<br>
<br>
        resource_list = &pointer->focus_resource_list;<br>
        if (!wl_list_empty(resource_list)) {<br>
                serial = wl_display_next_serial(display);<br>
-               wl_resource_for_each(resource, resource_list)<br>
+               wl_resource_for_each(resource, resource_list) {<br>
                        wl_pointer_send_button(resource,<br>
                                               serial,<br>
                                               time,<br>
                                               button,<br>
                                               state_w);<br>
+               }<br>
        }<br>
+}<br>
+<br>
+static void<br>
+default_grab_pointer_button(struct weston_pointer_grab *grab,<br>
+                           uint32_t time, uint32_t button, uint32_t state_w)<br>
+{<br>
+       struct weston_pointer *pointer = grab->pointer;<br>
+       struct weston_compositor *compositor = pointer->seat->compositor;<br>
+       struct weston_view *view;<br>
+       enum wl_pointer_button_state state = state_w;<br>
+       wl_fixed_t sx, sy;<br>
+<br>
+       weston_pointer_send_button(pointer, time, button, state_w);<br>
<br>
        if (pointer->button_count == 0 &&<br>
            state == WL_POINTER_BUTTON_STATE_RELEASED) {<br>
@@ -292,10 +317,9 @@ default_grab_pointer_button(struct weston_pointer_grab *grab,<br>
 }<br>
<br>
 static void<br>
-default_grab_pointer_axis(struct weston_pointer_grab *grab,<br>
-                         uint32_t time, uint32_t axis, wl_fixed_t value)<br>
+weston_pointer_send_axis(struct weston_pointer *pointer,<br>
+                        uint32_t time, uint32_t axis, wl_fixed_t value)<br>
 {<br>
-       struct weston_pointer *pointer = grab->pointer;<br>
        struct wl_resource *resource;<br>
        struct wl_list *resource_list;<br>
<br>
@@ -305,6 +329,13 @@ default_grab_pointer_axis(struct weston_pointer_grab *grab,<br>
 }<br>
<br>
 static void<br>
+default_grab_pointer_axis(struct weston_pointer_grab *grab,<br>
+                         uint32_t time, uint32_t axis, wl_fixed_t value)<br>
+{<br>
+       weston_pointer_send_axis(grab->pointer, time, axis, value);<br>
+}<br>
+<br>
+static void<br>
 default_grab_pointer_cancel(struct weston_pointer_grab *grab)<br>
 {<br>
 }<br>
@@ -564,6 +595,7 @@ weston_pointer_create(struct weston_seat *seat)<br>
        wl_signal_init(&pointer->motion_signal);<br>
        wl_signal_init(&pointer->focus_signal);<br>
        wl_list_init(&pointer->focus_view_listener.link);<br>
+       wl_signal_init(&pointer->destroy_signal);<br>
<br>
        pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;<br>
<br>
@@ -582,6 +614,8 @@ weston_pointer_create(struct weston_seat *seat)<br>
 WL_EXPORT void<br>
 weston_pointer_destroy(struct weston_pointer *pointer)<br>
 {<br>
+       wl_signal_emit(&pointer->destroy_signal, pointer);<br>
+<br>
        if (pointer->sprite)<br>
                pointer_unmap_sprite(pointer);<br>
<br>
@@ -2513,6 +2547,518 @@ weston_seat_release(struct weston_seat *seat)<br>
        wl_signal_emit(&seat->destroy_signal, seat);<br>
 }<br>
<br>
+static const struct _wl_locked_pointer_interface locked_pointer_interface;<br>
+static const struct _wl_confined_pointer_interface confined_pointer_interface;<br>
+<br>
+static void<br>
+pointer_lock_notify_activated(struct weston_surface *surface)<br>
+{<br>
+       struct weston_compositor *compositor = surface->compositor;<br>
+       struct wl_resource *resource = surface->pointer_lock.resource;<br>
+       uint32_t serial;<br>
+<br>
+       if (wl_resource_instance_of(resource,<br>
+                                   &_wl_locked_pointer_interface,<br>
+                                   &locked_pointer_interface)) {<br>
+               serial = wl_display_next_serial(compositor->wl_display);<br>
+               _wl_locked_pointer_send_locked(resource, serial);<br>
+       } else if (wl_resource_instance_of(resource,<br>
+                                          &_wl_confined_pointer_interface,<br>
+                                          &confined_pointer_interface)) {<br>
+               serial = wl_display_next_serial(compositor->wl_display);<br>
+               _wl_confined_pointer_send_confined(resource, serial);<br>
+       }<br>
+}<br>
+<br>
+static void<br>
+pointer_lock_notify_deactivated(struct weston_surface *surface)<br>
+{<br>
+       struct wl_resource *resource = surface->pointer_lock.resource;<br>
+<br>
+       if (wl_resource_instance_of(resource,<br>
+                                   &_wl_locked_pointer_interface,<br>
+                                   &locked_pointer_interface)) {<br>
+               _wl_locked_pointer_send_unlocked(resource);<br>
+       } else if (wl_resource_instance_of(resource,<br>
+                                          &_wl_confined_pointer_interface,<br>
+                                          &confined_pointer_interface)) {<br>
+               _wl_confined_pointer_send_unconfined(resource);<br>
+       }<br>
+}<br>
+<br>
+static void<br>
+enable_pointer_lock(struct weston_view *view,<br>
+                   struct weston_pointer *pointer)<br>
+{<br>
+       struct weston_surface *surface = view->surface;<br>
+<br>
+       assert(surface->pointer_lock.view == NULL);<br>
+       surface->pointer_lock.view = view;<br>
+       pointer_lock_notify_activated(surface);<br>
+       weston_pointer_start_grab(pointer, &surface->pointer_lock.grab);<br>
+}<br>
+<br>
+static bool<br>
+is_pointer_lock_enabled(struct weston_surface *surface)<br>
+{<br>
+       return surface->pointer_lock.view != NULL;<br>
+}<br>
+<br>
+static void<br>
+disable_pointer_lock(struct weston_surface *surface)<br>
+{<br>
+       if (is_pointer_lock_enabled(surface)) {<br>
+               pointer_lock_notify_deactivated(surface);<br>
+               weston_pointer_end_grab(surface->pointer_lock.grab.pointer);<br>
+               surface->pointer_lock.view = NULL;<br>
+       }<br>
+<br>
+       surface->pointer_lock.resource = NULL;<br>
+       surface->pointer_lock.pointer = NULL;<br>
+<br>
+       surface->pointer_lock.hint_set = false;<br>
+<br>
+       wl_list_remove(&surface->pointer_lock.pointer_destroy_listener.link);<br>
+       wl_list_remove(&surface->pointer_lock.surface_destroy_listener.link);<br>
+       wl_list_remove(&surface->pointer_lock.surface_activate_listener.link);<br>
+}<br>
+<br>
+static bool<br>
+is_within_lock_region(struct weston_surface *surface,<br>
+                     wl_fixed_t sx, wl_fixed_t sy)<br>
+{<br>
+       pixman_region32_t lock_region;<br>
+       bool result;<br>
+<br>
+       pixman_region32_init(&lock_region);<br>
+       pixman_region32_intersect(&lock_region,<br>
+                                 &surface->input,<br>
+                                 &surface->pointer_lock.region);<br>
+       result = pixman_region32_contains_point(&lock_region,<br>
+                                               wl_fixed_to_int(sx),<br>
+                                               wl_fixed_to_int(sy),<br>
+                                               NULL);<br>
+       pixman_region32_fini(&lock_region);<br>
+<br>
+       return result;<br>
+}<br>
+<br>
+static void<br>
+maybe_enable_pointer_lock(struct weston_surface *surface)<br>
+{<br>
+       struct weston_view *vit;<br>
+       struct weston_view *view = NULL;<br>
+       struct weston_pointer *pointer = surface->pointer_lock.pointer;<br>
+       struct weston_seat *seat = pointer->seat;<br>
+       int32_t x, y;<br>
+<br>
+       /* Postpone if no view of the surface was most recently clicked. */<br>
+       wl_list_for_each(vit, &surface->views, surface_link) {<br>
+               if (vit->click_to_activate_serial ==<br>
+                   surface->compositor->activate_serial) {<br>
+                       view = vit;<br>
+               }<br>
+       }<br>
+       if (view == NULL)<br>
+               return;<br>
+<br>
+       /* Postpone if surface doesn't have keyboard focus. */<br>
+       if (seat->keyboard->focus != surface)<br>
+               return;<br>
+<br>
+       /* Postpone lock if the pointer is not within the lock region. */<br>
+       weston_view_from_global(view,<br>
+                               wl_fixed_to_int(pointer->x),<br>
+                               wl_fixed_to_int(pointer->y),<br>
+                               &x, &y);<br>
+       if (!is_within_lock_region(surface,<br>
+                                  wl_fixed_from_int(x),<br>
+                                  wl_fixed_from_int(y)))<br>
+               return;<br>
+<br>
+       enable_pointer_lock(view, pointer);<br>
+}<br>
+<br>
+static void<br>
+locked_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)<br>
+{<br>
+}<br>
+<br>
+static void<br>
+locked_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,<br>
+                                  uint32_t time,<br>
+                                  struct weston_pointer_motion_event *event)<br>
+{<br>
+       weston_pointer_send_relative_motion(grab->pointer, time, event);<br>
+}<br>
+<br>
+static void<br>
+locked_pointer_grab_pointer_button(struct weston_pointer_grab *grab,<br>
+                                  uint32_t time,<br>
+                                  uint32_t button,<br>
+                                  uint32_t state_w)<br>
+{<br>
+       weston_pointer_send_button(grab->pointer, time, button, state_w);<br>
+}<br>
+<br>
+static void<br>
+locked_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,<br>
+                                uint32_t time, uint32_t axis, wl_fixed_t value)<br>
+{<br>
+       weston_pointer_send_axis(grab->pointer, time, axis, value);<br>
+}<br>
+<br>
+static void<br>
+locked_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)<br>
+{<br>
+       struct weston_surface *surface =<br>
+               container_of(grab, struct weston_surface, pointer_lock.grab);<br>
+<br>
+       disable_pointer_lock(surface);<br>
+}<br>
+<br>
+static const struct weston_pointer_grab_interface<br>
+                               locked_pointer_grab_interface = {<br>
+       locked_pointer_grab_pointer_focus,<br>
+       locked_pointer_grab_pointer_motion,<br>
+       locked_pointer_grab_pointer_button,<br>
+       locked_pointer_grab_pointer_axis,<br>
+       locked_pointer_grab_pointer_cancel,<br>
+};<br>
+<br>
+static void<br>
+pointer_lock_lock_resource_destroyed(struct wl_resource *resource)<br>
+{<br>
+       struct weston_surface *surface = wl_resource_get_user_data(resource);<br>
+<br>
+       if (!surface->pointer_lock.resource)<br>
+               return;<br>
+<br>
+       disable_pointer_lock(surface);<br>
+}<br>
+<br>
+static void<br>
+pointer_lock_surface_activate(struct wl_listener *listener, void *data)<br>
+{<br>
+       struct weston_surface *focus = data;<br>
+       struct weston_surface *locked_surface =<br>
+               container_of(listener, struct weston_surface,<br>
+                            pointer_lock.surface_activate_listener);<br>
+<br>
+       if (focus == locked_surface && !is_pointer_lock_enabled(locked_surface))<br>
+               maybe_enable_pointer_lock(locked_surface);<br>
+       else if (focus != locked_surface &&<br>
+                is_pointer_lock_enabled(locked_surface))<br>
+               disable_pointer_lock(locked_surface);<br>
+}<br>
+<br>
+static void<br>
+pointer_lock_pointer_destroyed(struct wl_listener *listener, void *data)<br>
+{<br>
+       struct weston_surface *surface =<br>
+               container_of(listener, struct weston_surface,<br>
+                            pointer_lock.pointer_destroy_listener);<br>
+<br>
+       disable_pointer_lock(surface);<br>
+}<br>
+<br>
+static void<br>
+pointer_lock_surface_destroyed(struct wl_listener *listener, void *data)<br>
+{<br>
+       struct weston_surface *surface =<br>
+               container_of(listener, struct weston_surface,<br>
+                            pointer_lock.surface_destroy_listener);<br>
+<br>
+       disable_pointer_lock(surface);<br>
+}<br>
+<br>
+static void<br>
+init_pointer_lock(struct wl_resource *pointer_lock_resource,<br>
+                 uint32_t id,<br>
+                 struct weston_surface *surface,<br>
+                 struct weston_seat *seat,<br>
+                 struct weston_region *region,<br>
+                 const struct wl_interface *interface,<br>
+                 const void *implementation,<br>
+                 const struct weston_pointer_grab_interface *grab_interface)<br>
+{<br>
+       struct wl_client *client =<br>
+               wl_resource_get_client(pointer_lock_resource);<br>
+       struct weston_pointer *pointer = seat->pointer;<br>
+       struct wl_resource *cr;<br>
+<br>
+       if (surface->pointer_lock.resource) {<br>
+               wl_resource_post_error(pointer_lock_resource,<br>
+                                      WL_DISPLAY_ERROR_INVALID_OBJECT,<br>
+                                      "pointer already locked or confined");<br>
+               return;<br>
+       }<br>
+<br>
+        cr = wl_resource_create(client, interface,<br>
+                               wl_resource_get_version(pointer_lock_resource),<br>
+                               id);<br>
+       if (cr == NULL) {<br>
+               wl_client_post_no_memory(client);<br>
+               return;<br>
+       }<br>
+<br>
+       wl_resource_set_implementation(cr, implementation, surface,<br>
+                                      pointer_lock_lock_resource_destroyed);<br>
+<br>
+       surface->pointer_lock.pointer = pointer;<br>
+       surface->pointer_lock.resource = cr;<br>
+       surface->pointer_lock.grab.interface = grab_interface;<br>
+       if (region) {<br>
+               pixman_region32_copy(&surface->pointer_lock.region,<br>
+                                    &region->region);<br>
+       } else {<br>
+               pixman_region32_fini(&surface->pointer_lock.region);<br>
+               region_init_infinite(&surface->pointer_lock.region);<br>
+       }<br>
+<br>
+       surface->pointer_lock.surface_activate_listener.notify =<br>
+               pointer_lock_surface_activate;<br>
+       surface->pointer_lock.surface_destroy_listener.notify =<br>
+               pointer_lock_surface_destroyed;<br>
+       surface->pointer_lock.pointer_destroy_listener.notify =<br>
+               pointer_lock_pointer_destroyed;<br>
+<br>
+       wl_signal_add(&surface->compositor->activate_signal,<br>
+                     &surface->pointer_lock.surface_activate_listener);<br>
+       wl_signal_add(&seat->pointer->destroy_signal,<br>
+                     &surface->pointer_lock.pointer_destroy_listener);<br>
+       wl_signal_add(&surface->destroy_signal,<br>
+                     &surface->pointer_lock.surface_destroy_listener);<br>
+<br>
+       maybe_enable_pointer_lock(surface);<br>
+}<br>
+<br>
+<br>
+static void<br>
+locked_pointer_set_cursor_position_hint(struct wl_client *client,<br>
+                                       struct wl_resource *resource,<br>
+                                       uint32_t serial,<br>
+                                       wl_fixed_t surface_x,<br>
+                                       wl_fixed_t surface_y)<br>
+{<br>
+       struct weston_surface *surface = wl_resource_get_user_data(resource);<br>
+<br>
+       /* TODO: Check serial. */<br>
+<br>
+       surface->pointer_lock.hint_set = true;<br>
+       surface->pointer_lock.x_hint = surface_x;<br>
+       surface->pointer_lock.y_hint = surface_y;<br>
+}<br>
+<br>
+static void<br>
+locked_pointer_destroy(struct wl_client *client,<br>
+                      struct wl_resource *resource)<br>
+{<br>
+       struct weston_surface *surface = wl_resource_get_user_data(resource);<br>
+       wl_fixed_t x_hint = surface->pointer_lock.x_hint;<br>
+       wl_fixed_t y_hint = surface->pointer_lock.y_hint;<br>
+       wl_fixed_t x, y;<br>
+<br>
+       if (surface->pointer_lock.view &&<br>
+           surface->pointer_lock.hint_set &&<br>
+           is_within_lock_region(surface, x_hint, y_hint)) {<br>
+               weston_view_to_global_fixed(surface->pointer_lock.view,<br>
+                                           x_hint, y_hint,<br>
+                                           &x, &y);<br>
+               weston_pointer_move_to(surface->pointer_lock.pointer, x, y);<br>
+       }<br>
+       wl_resource_destroy(resource);<br>
+}<br>
+<br>
+static const struct _wl_locked_pointer_interface locked_pointer_interface = {<br>
+       locked_pointer_set_cursor_position_hint,<br>
+       locked_pointer_destroy,<br>
+};<br>
+<br>
+static void<br>
+pointer_lock_lock_pointer(struct wl_client *client,<br>
+                         struct wl_resource *resource,<br>
+                         uint32_t id,<br>
+                         struct wl_resource *surface_resource,<br>
+                         struct wl_resource *seat_resource,<br>
+                         struct wl_resource *region_resource)<br>
+{<br>
+       struct weston_surface *surface =<br>
+               wl_resource_get_user_data(surface_resource);<br>
+       struct weston_seat *seat = wl_resource_get_user_data(seat_resource);<br>
+       struct weston_region *region = region_resource ?<br>
+               wl_resource_get_user_data(region_resource) : NULL;<br>
+<br>
+       init_pointer_lock(resource, id, surface, seat, region,<br>
+                         &_wl_locked_pointer_interface,<br>
+                         &locked_pointer_interface,<br>
+                         &locked_pointer_grab_interface);<br>
+}<br>
+<br>
+static void<br>
+confined_pointer_grab_pointer_focus(struct weston_pointer_grab *grab)<br>
+{<br>
+}<br>
+<br>
+static void<br>
+weston_pointer_clamp_event_to_region(struct weston_pointer *pointer,<br>
+                                    struct weston_pointer_motion_event *event,<br>
+                                    pixman_region32_t *region,<br>
+                                    wl_fixed_t *clamped_x,<br>
+                                    wl_fixed_t *clamped_y)<br>
+{<br>
+       wl_fixed_t x, y;<br>
+       wl_fixed_t sx, sy;<br>
+       wl_fixed_t min_sx = wl_fixed_from_int(region->extents.x1);<br>
+       wl_fixed_t max_sx = wl_fixed_from_int(region->extents.x2 - 1);<br>
+       wl_fixed_t max_sy = wl_fixed_from_int(region->extents.y2 - 1);<br>
+       wl_fixed_t min_sy = wl_fixed_from_int(region->extents.y1);<br>
+<br>
+       weston_pointer_motion_to_abs(pointer, event, &x, &y);<br>
+       weston_view_from_global_fixed(pointer->focus, x, y, &sx, &sy);<br>
+<br>
+       if (sx < min_sx)<br>
+               sx = min_sx;<br>
+       else if (sx > max_sx)<br>
+               sx = max_sx;<br>
+<br>
+       if (sy < min_sy)<br>
+               sy = min_sy;<br>
+       else if (sy > max_sy)<br>
+               sy = max_sy;<br>
+<br>
+       weston_view_to_global_fixed(pointer->focus, sx, sy,<br>
+                                   clamped_x, clamped_y);<br>
+}<br>
+<br>
+static void<br>
+confined_pointer_grab_pointer_motion(struct weston_pointer_grab *grab,<br>
+                                    uint32_t time,<br>
+                                    struct weston_pointer_motion_event *event)<br>
+{<br>
+       struct weston_pointer *pointer = grab->pointer;<br>
+       struct weston_surface *surface;<br>
+       wl_fixed_t x, y;<br>
+       wl_fixed_t old_sx = pointer->sx;<br>
+       wl_fixed_t old_sy = pointer->sy;<br>
+       pixman_region32_t confine_region;<br>
+<br>
+       assert(pointer->focus);<br>
+       assert(pointer->focus->surface->pointer_lock.pointer == pointer);<br>
+<br>
+       surface = pointer->focus->surface;<br>
+<br>
+       pixman_region32_init(&confine_region);<br>
+       pixman_region32_intersect(&confine_region,<br>
+                                 &surface->input,<br>
+                                 &surface->pointer_lock.region);<br>
+       weston_pointer_clamp_event_to_region(pointer, event,<br>
+                                            &confine_region, &x, &y);<br>
+       weston_pointer_move_to(pointer, x, y);<br>
+       pixman_region32_fini(&confine_region);<br>
+<br>
+       weston_view_from_global_fixed(pointer->focus, x, y,<br>
+                                     &pointer->sx, &pointer->sy);<br>
+<br>
+       if (old_sx != pointer->sx || old_sy != pointer->sy) {<br>
+               weston_pointer_send_motion(pointer, time,<br>
+                                          pointer->sx, pointer->sy);<br>
+       }<br>
+<br>
+       weston_pointer_send_relative_motion(pointer, time, event);<br>
+}<br>
+<br>
+static void<br>
+confined_pointer_grab_pointer_button(struct weston_pointer_grab *grab,<br>
+                                    uint32_t time,<br>
+                                    uint32_t button,<br>
+                                    uint32_t state_w)<br>
+{<br>
+       weston_pointer_send_button(grab->pointer, time, button, state_w);<br>
+}<br>
+<br>
+static void<br>
+confined_pointer_grab_pointer_axis(struct weston_pointer_grab *grab,<br>
+                                  uint32_t time,<br>
+                                  uint32_t axis,<br>
+                                  wl_fixed_t value)<br>
+{<br>
+       weston_pointer_send_axis(grab->pointer, time, axis, value);<br>
+}<br>
+<br>
+static void<br>
+confined_pointer_grab_pointer_cancel(struct weston_pointer_grab *grab)<br>
+{<br>
+       struct weston_surface *surface =<br>
+               container_of(grab, struct weston_surface, pointer_lock.grab);<br>
+<br>
+       disable_pointer_lock(surface);<br>
+}<br>
+<br>
+static const struct weston_pointer_grab_interface<br>
+                               confined_pointer_grab_interface = {<br>
+       confined_pointer_grab_pointer_focus,<br>
+       confined_pointer_grab_pointer_motion,<br>
+       confined_pointer_grab_pointer_button,<br>
+       confined_pointer_grab_pointer_axis,<br>
+       confined_pointer_grab_pointer_cancel,<br>
+};<br>
+<br>
+static void<br>
+confined_pointer_destroy(struct wl_client *client,<br>
+                        struct wl_resource *resource)<br>
+{<br>
+       wl_resource_destroy(resource);<br>
+}<br>
+<br>
+static const struct _wl_confined_pointer_interface confined_pointer_interface = {<br>
+       confined_pointer_destroy,<br>
+};<br>
+<br>
+static void<br>
+pointer_lock_confine_pointer(struct wl_client *client,<br>
+                            struct wl_resource *resource,<br>
+                            uint32_t id,<br>
+                            struct wl_resource *surface_resource,<br>
+                            struct wl_resource *seat_resource,<br>
+                            struct wl_resource *region_resource)<br>
+{<br>
+       struct weston_surface *surface =<br>
+               wl_resource_get_user_data(surface_resource);<br>
+       struct weston_seat *seat = wl_resource_get_user_data(seat_resource);<br>
+       struct weston_region *region = region_resource ?<br>
+               wl_resource_get_user_data(region_resource) : NULL;<br>
+<br>
+       if ((region && pixman_region32_n_rects(&region->region) != 1) ||<br>
+           pixman_region32_n_rects(&surface->input) != 1) {<br>
+               weston_log("warning: confinement only implemented for"<br>
+                          "rectangular regions\n");<br>
+               return;<br>
+       }<br>
+<br>
+       init_pointer_lock(resource, id, surface, seat, region,<br>
+                         &_wl_confined_pointer_interface,<br>
+                         &confined_pointer_interface,<br>
+                         &confined_pointer_grab_interface);<br>
+}<br>
+<br>
+static const struct _wl_pointer_lock_interface pointer_lock_interface = {<br>
+       pointer_lock_lock_pointer,<br>
+       pointer_lock_confine_pointer,<br>
+};<br>
+<br>
+static void<br>
+bind_pointer_lock(struct wl_client *client, void *data,<br>
+                 uint32_t version, uint32_t id)<br>
+{<br>
+       struct wl_resource *resource;<br>
+<br>
+       resource = wl_resource_create(client, &_wl_pointer_lock_interface,<br>
+                                     1, id);<br>
+       wl_resource_set_implementation(resource, &pointer_lock_interface,<br>
+                                      NULL, NULL);<br>
+}<br>
+<br>
 int<br>
 weston_input_init(struct weston_compositor *compositor)<br>
 {<br>
@@ -2521,5 +3067,10 @@ weston_input_init(struct weston_compositor *compositor)<br>
                              compositor, bind_relative_pointer_manager))<br>
                return -1;<br>
<br>
+       if (!wl_global_create(compositor->wl_display,<br>
+                             &_wl_pointer_lock_interface, 1,<br>
+                             NULL, bind_pointer_lock))<br>
+               return -1;<br>
+<br>
        return 0;<br>
 }<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.8.5.1<br>
<br>
_______________________________________________<br>
wayland-devel mailing list<br>
<a href="mailto:wayland-devel@lists.freedesktop.org">wayland-devel@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/wayland-devel" target="_blank">http://lists.freedesktop.org/mailman/listinfo/wayland-devel</a><br>
</font></span></blockquote></div><br></div></div></div>