Mir: Add mouse grab support (requires mir 0.24)
authorBrandon Schaefer <brandon.schaefer@canonical.com>
Tue, 30 Aug 2016 12:58:00 -0700
changeset 102995a45e29a6ea7
parent 10298 06e7d0ccbc11
child 10300 c5300ace12f2
Mir: Add mouse grab support (requires mir 0.24)
src/video/mir/SDL_mirevents.c
src/video/mir/SDL_mirsym.h
src/video/mir/SDL_mirvideo.c
src/video/mir/SDL_mirwindow.c
src/video/mir/SDL_mirwindow.h
     1.1 --- a/src/video/mir/SDL_mirevents.c	Tue Aug 30 21:16:04 2016 +0200
     1.2 +++ b/src/video/mir/SDL_mirevents.c	Tue Aug 30 12:58:00 2016 -0700
     1.3 @@ -53,16 +53,6 @@
     1.4      }
     1.5  }
     1.6  
     1.7 -static void
     1.8 -CheckKeyboardFocus(SDL_Window* sdl_window)
     1.9 -{
    1.10 -    SDL_Window* keyboard_window = SDL_GetKeyboardFocus();
    1.11 -
    1.12 -    if (sdl_window && keyboard_window != sdl_window)
    1.13 -        SDL_SetKeyboardFocus(sdl_window);
    1.14 -}
    1.15 -
    1.16 -
    1.17  /* FIXME
    1.18     Mir still needs to implement its IM API, for now we assume
    1.19     a single key press produces a character.
    1.20 @@ -84,8 +74,6 @@
    1.21      if (action == mir_keyboard_action_up)
    1.22          key_state = SDL_RELEASED;
    1.23  
    1.24 -    CheckKeyboardFocus(window);
    1.25 -
    1.26      if (event_scancode < SDL_arraysize(xfree86_scancode_table2))
    1.27          sdl_scancode = xfree86_scancode_table2[event_scancode];
    1.28  
    1.29 @@ -220,13 +208,13 @@
    1.30              x = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_x);
    1.31              y = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_y);
    1.32  
    1.33 -            if (mouse && (mouse->x != x || mouse->y != y)) {
    1.34 +            if (mouse) {
    1.35                  if (mouse->relative_mode) {
    1.36                      int relative_x = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_relative_x);
    1.37                      int relative_y = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_relative_y);
    1.38                      HandleMouseMotion(sdl_window, relative_x, relative_y);
    1.39                  }
    1.40 -                else {
    1.41 +                else if (mouse->x != x || mouse->y != y) {
    1.42                      HandleMouseMotion(sdl_window, x, y);
    1.43                  }
    1.44              }
    1.45 @@ -279,6 +267,22 @@
    1.46          SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, new_w, new_h);
    1.47  }
    1.48  
    1.49 +static void
    1.50 +MIR_HandleSurface(MirSurfaceEvent const* surface_event, SDL_Window* window)
    1.51 +{
    1.52 +    MirSurfaceAttrib attrib = MIR_mir_surface_event_get_attribute(surface_event);
    1.53 +    int value               = MIR_mir_surface_event_get_attribute_value(surface_event);
    1.54 +
    1.55 +    if (attrib == mir_surface_attrib_focus) {
    1.56 +        if (value == mir_surface_focused) {
    1.57 +            SDL_SetKeyboardFocus(window);
    1.58 +        }
    1.59 +        else if (value == mir_surface_unfocused) {
    1.60 +            SDL_SetKeyboardFocus(NULL);
    1.61 +        }
    1.62 +    }
    1.63 +}
    1.64 +
    1.65  void
    1.66  MIR_HandleEvent(MirSurface* surface, MirEvent const* ev, void* context)
    1.67  {
    1.68 @@ -293,6 +297,9 @@
    1.69              case (mir_event_type_resize):
    1.70                  MIR_HandleResize(MIR_mir_event_get_resize_event(ev), window);
    1.71                  break;
    1.72 +            case (mir_event_type_surface):
    1.73 +                MIR_HandleSurface(MIR_mir_event_get_surface_event(ev), window);
    1.74 +                break;
    1.75              default:
    1.76                  break;
    1.77          }
     2.1 --- a/src/video/mir/SDL_mirsym.h	Tue Aug 30 21:16:04 2016 +0200
     2.2 +++ b/src/video/mir/SDL_mirsym.h	Tue Aug 30 12:58:00 2016 -0700
     2.3 @@ -53,6 +53,7 @@
     2.4  SDL_MIR_SYM(void,mir_surface_spec_set_max_height,(MirSurfaceSpec *spec, unsigned max_height))
     2.5  SDL_MIR_SYM(void,mir_surface_spec_set_type,(MirSurfaceSpec *spec, MirSurfaceType type))
     2.6  SDL_MIR_SYM(void,mir_surface_spec_set_state,(MirSurfaceSpec *spec, MirSurfaceState state))
     2.7 +SDL_MIR_SYM(void,mir_surface_spec_set_pointer_confinement,(MirSurfaceSpec *spec, MirPointerConfinementState state))
     2.8  SDL_MIR_SYM(void,mir_surface_apply_spec,(MirSurface *surface, MirSurfaceSpec *spec))
     2.9  SDL_MIR_SYM(void,mir_surface_get_parameters,(MirSurface *surface, MirSurfaceParameters *params))
    2.10  SDL_MIR_SYM(MirBufferStream*,mir_surface_get_buffer_stream,(MirSurface *surface))
    2.11 @@ -76,6 +77,7 @@
    2.12  SDL_MIR_SYM(MirKeyboardEvent const*,mir_input_event_get_keyboard_event,(MirInputEvent const *event))
    2.13  SDL_MIR_SYM(MirPointerEvent const*,mir_input_event_get_pointer_event,(MirInputEvent const *event))
    2.14  SDL_MIR_SYM(MirTouchEvent const*,mir_input_event_get_touch_event,(MirInputEvent const *event))
    2.15 +SDL_MIR_SYM(MirSurfaceEvent const*,mir_event_get_surface_event,(MirEvent const *event))
    2.16  SDL_MIR_SYM(unsigned int,mir_touch_event_point_count,(MirTouchEvent const *event))
    2.17  SDL_MIR_SYM(void,mir_connection_get_available_surface_formats,(MirConnection* connection, MirPixelFormat* formats, unsigned const int format_size, unsigned int *num_valid_formats))
    2.18  SDL_MIR_SYM(MirEGLNativeDisplayType,mir_connection_get_egl_native_display,(MirConnection *connection))
    2.19 @@ -94,6 +96,9 @@
    2.20  SDL_MIR_SYM(int,mir_resize_event_get_width,(MirResizeEvent const* resize_event))
    2.21  SDL_MIR_SYM(int,mir_resize_event_get_height,(MirResizeEvent const* resize_event))
    2.22  SDL_MIR_SYM(char const*,mir_connection_get_error_message,(MirConnection* connection))
    2.23 +SDL_MIR_SYM(MirSurfaceAttrib,mir_surface_event_get_attribute,(MirSurfaceEvent const* surface_event))
    2.24 +SDL_MIR_SYM(int,mir_surface_event_get_attribute_value,(MirSurfaceEvent const* surface_event))
    2.25 +SDL_MIR_SYM(void,mir_wait_for,(MirWaitHandle* handle))
    2.26  
    2.27  SDL_MIR_SYM_CONST(char const*,mir_omnidirectional_resize_cursor_name)
    2.28  SDL_MIR_SYM_CONST(char const*,mir_busy_cursor_name)
    2.29 @@ -103,6 +108,7 @@
    2.30  SDL_MIR_SYM_CONST(char const*,mir_horizontal_resize_cursor_name)
    2.31  SDL_MIR_SYM_CONST(char const*,mir_open_hand_cursor_name)
    2.32  SDL_MIR_SYM_CONST(char const*,mir_closed_hand_cursor_name)
    2.33 +SDL_MIR_SYM_CONST(char const*,mir_disabled_cursor_name)
    2.34  
    2.35  SDL_MIR_MODULE(XKBCOMMON)
    2.36  SDL_MIR_SYM(int,xkb_keysym_to_utf8,(xkb_keysym_t keysym, char *buffer, size_t size))
     3.1 --- a/src/video/mir/SDL_mirvideo.c	Tue Aug 30 21:16:04 2016 +0200
     3.2 +++ b/src/video/mir/SDL_mirvideo.c	Tue Aug 30 12:58:00 2016 -0700
     3.3 @@ -159,6 +159,7 @@
     3.4      device->SetWindowMinimumSize = MIR_SetWindowMinimumSize;
     3.5      device->SetWindowMaximumSize = MIR_SetWindowMaximumSize;
     3.6      device->SetWindowTitle       = MIR_SetWindowTitle;
     3.7 +    device->SetWindowGrab        = MIR_SetWindowGrab;
     3.8  
     3.9      device->CreateWindowFrom     = NULL;
    3.10      device->SetWindowIcon        = NULL;
    3.11 @@ -166,7 +167,6 @@
    3.12      device->SetWindowBordered    = NULL;
    3.13      device->SetWindowGammaRamp   = NULL;
    3.14      device->GetWindowGammaRamp   = NULL;
    3.15 -    device->SetWindowGrab        = NULL;
    3.16      device->OnWindowEnter        = NULL;
    3.17      device->SetWindowPosition    = NULL;
    3.18  
     4.1 --- a/src/video/mir/SDL_mirwindow.c	Tue Aug 30 21:16:04 2016 +0200
     4.2 +++ b/src/video/mir/SDL_mirwindow.c	Tue Aug 30 12:58:00 2016 -0700
     4.3 @@ -29,6 +29,7 @@
     4.4  
     4.5  #include "../SDL_egl_c.h"
     4.6  #include "../SDL_sysvideo.h"
     4.7 +#include "../../events/SDL_keyboard_c.h"
     4.8  
     4.9  #include "SDL_mirevents.h"
    4.10  #include "SDL_mirwindow.h"
    4.11 @@ -124,6 +125,9 @@
    4.12      MIR_mir_surface_spec_set_buffer_usage(spec, buffer_usage);
    4.13      MIR_mir_surface_spec_set_name(spec, "Mir surface");
    4.14  
    4.15 +    if (window->flags & SDL_WINDOW_INPUT_FOCUS)
    4.16 +        SDL_SetKeyboardFocus(window);
    4.17 +
    4.18      mir_window->surface = MIR_mir_surface_create_sync(spec);
    4.19      MIR_mir_surface_set_event_handler(mir_window->surface, MIR_HandleEvent, window);
    4.20  
    4.21 @@ -356,6 +360,25 @@
    4.22      MIR_mir_surface_spec_release(spec);
    4.23  }
    4.24  
    4.25 +void
    4.26 +MIR_SetWindowGrab(_THIS, SDL_Window* window, SDL_bool grabbed)
    4.27 +{
    4.28 +    MIR_Data*   mir_data   = _this->driverdata;
    4.29 +    MIR_Window* mir_window = window->driverdata;
    4.30 +    MirPointerConfinementState confined = mir_pointer_unconfined;
    4.31 +    MirSurfaceSpec* spec;
    4.32 +
    4.33 +    if (grabbed)
    4.34 +        confined = mir_pointer_confined_to_surface;
    4.35 +
    4.36 +    spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
    4.37 +    MIR_mir_surface_spec_set_pointer_confinement(spec, confined);
    4.38 +
    4.39 +    MIR_mir_surface_apply_spec(mir_window->surface, spec);
    4.40 +    MIR_mir_surface_spec_release(spec);
    4.41 +
    4.42 +}
    4.43 +
    4.44  #endif /* SDL_VIDEO_DRIVER_MIR */
    4.45  
    4.46  /* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/video/mir/SDL_mirwindow.h	Tue Aug 30 21:16:04 2016 +0200
     5.2 +++ b/src/video/mir/SDL_mirwindow.h	Tue Aug 30 12:58:00 2016 -0700
     5.3 @@ -78,6 +78,10 @@
     5.4  extern void
     5.5  MIR_SetWindowTitle(_THIS, SDL_Window* window);
     5.6  
     5.7 +extern void
     5.8 +MIR_SetWindowGrab(_THIS, SDL_Window* window, SDL_bool grabbed);
     5.9 +
    5.10 +
    5.11  #endif /* _SDL_mirwindow_h */
    5.12  
    5.13  /* vi: set ts=4 sw=4 expandtab: */