src/video/wayland/SDL_waylandevents.c
changeset 10304 ee83e0b4a36f
parent 10154 fae27a079fcb
child 10583 974f8ebcb819
     1.1 --- a/src/video/wayland/SDL_waylandevents.c	Thu Jun 23 18:39:05 2016 +0800
     1.2 +++ b/src/video/wayland/SDL_waylandevents.c	Thu Sep 01 01:26:56 2016 -0700
     1.3 @@ -25,6 +25,7 @@
     1.4  
     1.5  #include "SDL_stdinc.h"
     1.6  #include "SDL_assert.h"
     1.7 +#include "SDL_log.h"
     1.8  
     1.9  #include "../../events/SDL_sysevents.h"
    1.10  #include "../../events/SDL_events_c.h"
    1.11 @@ -36,6 +37,9 @@
    1.12  
    1.13  #include "SDL_waylanddyn.h"
    1.14  
    1.15 +#include "pointer-constraints-unstable-v1-client-protocol.h"
    1.16 +#include "relative-pointer-unstable-v1-client-protocol.h"
    1.17 +
    1.18  #include <linux/input.h>
    1.19  #include <sys/select.h>
    1.20  #include <sys/mman.h>
    1.21 @@ -48,13 +52,17 @@
    1.22      struct wl_seat *seat;
    1.23      struct wl_pointer *pointer;
    1.24      struct wl_keyboard *keyboard;
    1.25 +    struct zwp_relative_pointer_v1 *relative_pointer;
    1.26      SDL_WindowData *pointer_focus;
    1.27      SDL_WindowData *keyboard_focus;
    1.28  
    1.29      /* Last motion location */
    1.30      wl_fixed_t sx_w;
    1.31      wl_fixed_t sy_w;
    1.32 -    
    1.33 +
    1.34 +    double dx_frac;
    1.35 +    double dy_frac;
    1.36 +
    1.37      struct {
    1.38          struct xkb_keymap *keymap;
    1.39          struct xkb_state *state;
    1.40 @@ -170,10 +178,9 @@
    1.41  }
    1.42  
    1.43  static void
    1.44 -pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
    1.45 -                      uint32_t time, uint32_t button, uint32_t state_w)
    1.46 +pointer_handle_button_common(struct SDL_WaylandInput *input, uint32_t serial,
    1.47 +                             uint32_t time, uint32_t button, uint32_t state_w)
    1.48  {
    1.49 -    struct SDL_WaylandInput *input = data;
    1.50      SDL_WindowData *window = input->pointer_focus;
    1.51      enum wl_pointer_button_state state = state_w;
    1.52      uint32_t sdl_button;
    1.53 @@ -182,7 +189,7 @@
    1.54          switch (button) {
    1.55              case BTN_LEFT:
    1.56                  sdl_button = SDL_BUTTON_LEFT;
    1.57 -                if (ProcessHitTest(data, serial)) {
    1.58 +                if (ProcessHitTest(input, serial)) {
    1.59                      return;  /* don't pass this event on to app. */
    1.60                  }
    1.61                  break;
    1.62 @@ -208,10 +215,18 @@
    1.63  }
    1.64  
    1.65  static void
    1.66 -pointer_handle_axis(void *data, struct wl_pointer *pointer,
    1.67 -                    uint32_t time, uint32_t axis, wl_fixed_t value)
    1.68 +pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
    1.69 +                      uint32_t time, uint32_t button, uint32_t state_w)
    1.70  {
    1.71      struct SDL_WaylandInput *input = data;
    1.72 +
    1.73 +    pointer_handle_button_common(input, serial, time, button, state_w);
    1.74 +}
    1.75 +
    1.76 +static void
    1.77 +pointer_handle_axis_common(struct SDL_WaylandInput *input,
    1.78 +                           uint32_t time, uint32_t axis, wl_fixed_t value)
    1.79 +{
    1.80      SDL_WindowData *window = input->pointer_focus;
    1.81      enum wl_pointer_axis a = axis;
    1.82      int x, y;
    1.83 @@ -234,6 +249,15 @@
    1.84      }
    1.85  }
    1.86  
    1.87 +static void
    1.88 +pointer_handle_axis(void *data, struct wl_pointer *pointer,
    1.89 +                    uint32_t time, uint32_t axis, wl_fixed_t value)
    1.90 +{
    1.91 +    struct SDL_WaylandInput *input = data;
    1.92 +
    1.93 +    pointer_handle_axis_common(input, time, axis, value);
    1.94 +}
    1.95 +
    1.96  static const struct wl_pointer_listener pointer_listener = {
    1.97      pointer_handle_enter,
    1.98      pointer_handle_leave,
    1.99 @@ -453,6 +477,164 @@
   1.100      d->input = NULL;
   1.101  }
   1.102  
   1.103 +void Wayland_display_add_relative_pointer_manager(SDL_VideoData *d, uint32_t id)
   1.104 +{
   1.105 +    d->relative_pointer_manager =
   1.106 +        wl_registry_bind(d->registry, id,
   1.107 +                         &zwp_relative_pointer_manager_v1_interface, 1);
   1.108 +}
   1.109 +
   1.110 +void Wayland_display_destroy_relative_pointer_manager(SDL_VideoData *d)
   1.111 +{
   1.112 +    if (d->relative_pointer_manager)
   1.113 +        zwp_relative_pointer_manager_v1_destroy(d->relative_pointer_manager);
   1.114 +}
   1.115 +
   1.116 +void Wayland_display_add_pointer_constraints(SDL_VideoData *d, uint32_t id)
   1.117 +{
   1.118 +    d->pointer_constraints =
   1.119 +        wl_registry_bind(d->registry, id,
   1.120 +                         &zwp_pointer_constraints_v1_interface, 1);
   1.121 +}
   1.122 +
   1.123 +void Wayland_display_destroy_pointer_constraints(SDL_VideoData *d)
   1.124 +{
   1.125 +    if (d->pointer_constraints)
   1.126 +        zwp_pointer_constraints_v1_destroy(d->pointer_constraints);
   1.127 +}
   1.128 +
   1.129 +static void
   1.130 +relative_pointer_handle_relative_motion(void *data,
   1.131 +                                        struct zwp_relative_pointer_v1 *pointer,
   1.132 +                                        uint32_t time_hi,
   1.133 +                                        uint32_t time_lo,
   1.134 +                                        wl_fixed_t dx_w,
   1.135 +                                        wl_fixed_t dy_w,
   1.136 +                                        wl_fixed_t dx_unaccel_w,
   1.137 +                                        wl_fixed_t dy_unaccel_w)
   1.138 +{
   1.139 +    struct SDL_WaylandInput *input = data;
   1.140 +    SDL_VideoData *d = input->display;
   1.141 +    SDL_WindowData *window = input->pointer_focus;
   1.142 +    double dx_unaccel;
   1.143 +    double dy_unaccel;
   1.144 +    double dx;
   1.145 +    double dy;
   1.146 +
   1.147 +    dx_unaccel = wl_fixed_to_double(dx_unaccel_w);
   1.148 +    dy_unaccel = wl_fixed_to_double(dy_unaccel_w);
   1.149 +
   1.150 +    /* Add left over fraction from last event. */
   1.151 +    dx_unaccel += input->dx_frac;
   1.152 +    dy_unaccel += input->dy_frac;
   1.153 +
   1.154 +    input->dx_frac = modf(dx_unaccel, &dx);
   1.155 +    input->dy_frac = modf(dy_unaccel, &dy);
   1.156 +
   1.157 +    if (input->pointer_focus && d->relative_mouse_mode) {
   1.158 +        SDL_SendMouseMotion(window->sdlwindow, 0, 1, (int)dx, (int)dy);
   1.159 +    }
   1.160 +}
   1.161 +
   1.162 +static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
   1.163 +    relative_pointer_handle_relative_motion,
   1.164 +};
   1.165 +
   1.166 +static void
   1.167 +locked_pointer_locked(void *data,
   1.168 +                      struct zwp_locked_pointer_v1 *locked_pointer)
   1.169 +{
   1.170 +}
   1.171 +
   1.172 +static void
   1.173 +locked_pointer_unlocked(void *data,
   1.174 +                        struct zwp_locked_pointer_v1 *locked_pointer)
   1.175 +{
   1.176 +}
   1.177 +
   1.178 +static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
   1.179 +    locked_pointer_locked,
   1.180 +    locked_pointer_unlocked,
   1.181 +};
   1.182 +
   1.183 +static void
   1.184 +lock_pointer_to_window(SDL_Window *window,
   1.185 +                       struct SDL_WaylandInput *input)
   1.186 +{
   1.187 +    SDL_WindowData *w = window->driverdata;
   1.188 +    SDL_VideoData *d = input->display;
   1.189 +    struct zwp_locked_pointer_v1 *locked_pointer;
   1.190 +
   1.191 +    if (w->locked_pointer)
   1.192 +        return;
   1.193 +
   1.194 +    locked_pointer =
   1.195 +        zwp_pointer_constraints_v1_lock_pointer(d->pointer_constraints,
   1.196 +                                                w->surface,
   1.197 +                                                input->pointer,
   1.198 +                                                NULL,
   1.199 +                                                ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
   1.200 +    zwp_locked_pointer_v1_add_listener(locked_pointer,
   1.201 +                                       &locked_pointer_listener,
   1.202 +                                       window);
   1.203 +
   1.204 +    w->locked_pointer = locked_pointer;
   1.205 +}
   1.206 +
   1.207 +int Wayland_input_lock_pointer(struct SDL_WaylandInput *input)
   1.208 +{
   1.209 +    SDL_VideoDevice *vd = SDL_GetVideoDevice();
   1.210 +    SDL_VideoData *d = input->display;
   1.211 +    SDL_Window *window;
   1.212 +    struct zwp_relative_pointer_v1 *relative_pointer;
   1.213 +
   1.214 +    if (!d->relative_pointer_manager)
   1.215 +        return -1;
   1.216 +
   1.217 +    if (!d->pointer_constraints)
   1.218 +        return -1;
   1.219 +
   1.220 +    if (!input->relative_pointer) {
   1.221 +        relative_pointer =
   1.222 +            zwp_relative_pointer_manager_v1_get_relative_pointer(
   1.223 +                d->relative_pointer_manager,
   1.224 +                input->pointer);
   1.225 +        zwp_relative_pointer_v1_add_listener(relative_pointer,
   1.226 +                                             &relative_pointer_listener,
   1.227 +                                             input);
   1.228 +        input->relative_pointer = relative_pointer;
   1.229 +    }
   1.230 +
   1.231 +    for (window = vd->windows; window; window = window->next)
   1.232 +        lock_pointer_to_window(window, input);
   1.233 +
   1.234 +    d->relative_mouse_mode = 1;
   1.235 +
   1.236 +    return 0;
   1.237 +}
   1.238 +
   1.239 +int Wayland_input_unlock_pointer(struct SDL_WaylandInput *input)
   1.240 +{
   1.241 +    SDL_VideoDevice *vd = SDL_GetVideoDevice();
   1.242 +    SDL_VideoData *d = input->display;
   1.243 +    SDL_Window *window;
   1.244 +    SDL_WindowData *w;
   1.245 +
   1.246 +    for (window = vd->windows; window; window = window->next) {
   1.247 +        w = window->driverdata;
   1.248 +        if (w->locked_pointer)
   1.249 +            zwp_locked_pointer_v1_destroy(w->locked_pointer);
   1.250 +        w->locked_pointer = NULL;
   1.251 +    }
   1.252 +
   1.253 +    zwp_relative_pointer_v1_destroy(input->relative_pointer);
   1.254 +    input->relative_pointer = NULL;
   1.255 +
   1.256 +    d->relative_mouse_mode = 0;
   1.257 +
   1.258 +    return 0;
   1.259 +}
   1.260 +
   1.261  #endif /* SDL_VIDEO_DRIVER_WAYLAND */
   1.262  
   1.263  /* vi: set ts=4 sw=4 expandtab: */