wayland: add support for SDL_SetWindowGrab
authorTudor Brindus <me@tbrindus.ca>
Fri, 17 Apr 2020 13:55:44 -0400
changeset 138054709c1dfeabb
parent 13804 ccd76043c80e
child 13806 ed5f41281cb0
wayland: add support for SDL_SetWindowGrab
src/video/wayland/SDL_waylandevents.c
src/video/wayland/SDL_waylandevents_c.h
src/video/wayland/SDL_waylandvideo.c
src/video/wayland/SDL_waylandwindow.c
src/video/wayland/SDL_waylandwindow.h
     1.1 --- a/src/video/wayland/SDL_waylandevents.c	Mon May 11 14:36:23 2020 -0700
     1.2 +++ b/src/video/wayland/SDL_waylandevents.c	Fri Apr 17 13:55:44 2020 -0400
     1.3 @@ -65,6 +65,7 @@
     1.4      struct wl_keyboard *keyboard;
     1.5      SDL_WaylandDataDevice *data_device;
     1.6      struct zwp_relative_pointer_v1 *relative_pointer;
     1.7 +    struct zwp_confined_pointer_v1 *confined_pointer;
     1.8      SDL_WindowData *pointer_focus;
     1.9      SDL_WindowData *keyboard_focus;
    1.10  
    1.11 @@ -1263,6 +1264,64 @@
    1.12      return 0;
    1.13  }
    1.14  
    1.15 +static void
    1.16 +confined_pointer_confined(void *data,
    1.17 +                          struct zwp_confined_pointer_v1 *confined_pointer)
    1.18 +{
    1.19 +}
    1.20 +
    1.21 +static void
    1.22 +confined_pointer_unconfined(void *data,
    1.23 +                            struct zwp_confined_pointer_v1 *confined_pointer)
    1.24 +{
    1.25 +}
    1.26 +
    1.27 +static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
    1.28 +    confined_pointer_confined,
    1.29 +    confined_pointer_unconfined,
    1.30 +};
    1.31 +
    1.32 +int Wayland_input_confine_pointer(SDL_Window *window, struct SDL_WaylandInput *input)
    1.33 +{
    1.34 +    SDL_WindowData *w = window->driverdata;
    1.35 +    SDL_VideoData *d = input->display;
    1.36 +    struct zwp_confined_pointer_v1 *confined_pointer;
    1.37 +
    1.38 +    if (!d->pointer_constraints)
    1.39 +        return -1;
    1.40 +
    1.41 +    if (!input->pointer)
    1.42 +        return -1;
    1.43 +
    1.44 +    /* A confine may already be active, in which case we should destroy it and
    1.45 +     * create a new one. */
    1.46 +    if (input->confined_pointer)
    1.47 +        Wayland_input_unconfine_pointer(input);
    1.48 +
    1.49 +    confined_pointer =
    1.50 +        zwp_pointer_constraints_v1_confine_pointer(d->pointer_constraints,
    1.51 +                                                   w->surface,
    1.52 +                                                   input->pointer,
    1.53 +                                                   NULL,
    1.54 +                                                   ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
    1.55 +    zwp_confined_pointer_v1_add_listener(confined_pointer,
    1.56 +                                         &confined_pointer_listener,
    1.57 +                                         window);
    1.58 +
    1.59 +    input->confined_pointer = confined_pointer;
    1.60 +    return 0;
    1.61 +}
    1.62 +
    1.63 +int Wayland_input_unconfine_pointer(struct SDL_WaylandInput *input)
    1.64 +{
    1.65 +    if (input->confined_pointer) {
    1.66 +        zwp_confined_pointer_v1_destroy(input->confined_pointer);
    1.67 +        input->confined_pointer = NULL;
    1.68 +    }
    1.69 +
    1.70 +    return 0;
    1.71 +}
    1.72 +
    1.73  #endif /* SDL_VIDEO_DRIVER_WAYLAND */
    1.74  
    1.75  /* vi: set ts=4 sw=4 expandtab: */
     2.1 --- a/src/video/wayland/SDL_waylandevents_c.h	Mon May 11 14:36:23 2020 -0700
     2.2 +++ b/src/video/wayland/SDL_waylandevents_c.h	Fri Apr 17 13:55:44 2020 -0400
     2.3 @@ -43,6 +43,9 @@
     2.4  extern int Wayland_input_lock_pointer(struct SDL_WaylandInput *input);
     2.5  extern int Wayland_input_unlock_pointer(struct SDL_WaylandInput *input);
     2.6  
     2.7 +extern int Wayland_input_confine_pointer(SDL_Window *window, struct SDL_WaylandInput *input);
     2.8 +extern int Wayland_input_unconfine_pointer(struct SDL_WaylandInput *input);
     2.9 +
    2.10  extern void Wayland_display_add_relative_pointer_manager(SDL_VideoData *d, uint32_t id);
    2.11  extern void Wayland_display_destroy_relative_pointer_manager(SDL_VideoData *d);
    2.12  
     3.1 --- a/src/video/wayland/SDL_waylandvideo.c	Mon May 11 14:36:23 2020 -0700
     3.2 +++ b/src/video/wayland/SDL_waylandvideo.c	Fri Apr 17 13:55:44 2020 -0400
     3.3 @@ -184,6 +184,7 @@
     3.4      device->ShowWindow = Wayland_ShowWindow;
     3.5      device->SetWindowFullscreen = Wayland_SetWindowFullscreen;
     3.6      device->MaximizeWindow = Wayland_MaximizeWindow;
     3.7 +    device->SetWindowGrab = Wayland_SetWindowGrab;
     3.8      device->RestoreWindow = Wayland_RestoreWindow;
     3.9      device->SetWindowBordered = Wayland_SetWindowBordered;
    3.10      device->SetWindowSize = Wayland_SetWindowSize;
     4.1 --- a/src/video/wayland/SDL_waylandwindow.c	Mon May 11 14:36:23 2020 -0700
     4.2 +++ b/src/video/wayland/SDL_waylandwindow.c	Fri Apr 17 13:55:44 2020 -0400
     4.3 @@ -628,6 +628,17 @@
     4.4      WAYLAND_wl_display_flush( viddata->display );
     4.5  }
     4.6  
     4.7 +void
     4.8 +Wayland_SetWindowGrab(_THIS, SDL_Window *window, SDL_bool grabbed)
     4.9 +{
    4.10 +    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    4.11 +
    4.12 +    if (grabbed)
    4.13 +        Wayland_input_confine_pointer(window, data->input);
    4.14 +    else
    4.15 +        Wayland_input_unconfine_pointer(data->input);
    4.16 +}
    4.17 +
    4.18  int Wayland_CreateWindow(_THIS, SDL_Window *window)
    4.19  {
    4.20      SDL_WindowData *data;
     5.1 --- a/src/video/wayland/SDL_waylandwindow.h	Mon May 11 14:36:23 2020 -0700
     5.2 +++ b/src/video/wayland/SDL_waylandwindow.h	Fri Apr 17 13:55:44 2020 -0400
     5.3 @@ -88,6 +88,7 @@
     5.4                                          SDL_VideoDisplay * _display,
     5.5                                          SDL_bool fullscreen);
     5.6  extern void Wayland_MaximizeWindow(_THIS, SDL_Window * window);
     5.7 +extern void Wayland_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
     5.8  extern void Wayland_RestoreWindow(_THIS, SDL_Window * window);
     5.9  extern void Wayland_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered);
    5.10  extern int Wayland_CreateWindow(_THIS, SDL_Window *window);