src/video/wayland/SDL_waylandevents.c
changeset 8135 dd4729596096
parent 8116 f7c2f71251e5
child 8149 681eb46b8ac4
     1.1 --- a/src/video/wayland/SDL_waylandevents.c	Tue Jan 28 09:13:46 2014 -0300
     1.2 +++ b/src/video/wayland/SDL_waylandevents.c	Tue Jan 28 11:39:37 2014 -0300
     1.3 @@ -86,10 +86,19 @@
     1.4          /* enter event for a window we've just destroyed */
     1.5          return;
     1.6      }
     1.7 +    
     1.8 +    /* This handler will be called twice in Wayland 1.4
     1.9 +     * Once for the window surface which has valid user data
    1.10 +     * and again for the mouse cursor surface which does not have valid user data
    1.11 +     * We ignore the later
    1.12 +     */
    1.13  
    1.14 -    input->pointer_focus = wl_surface_get_user_data(surface);
    1.15 -    window = input->pointer_focus;
    1.16 -    SDL_SetMouseFocus(window->sdlwindow);
    1.17 +    window = (SDL_WindowData *)wl_surface_get_user_data(surface);
    1.18 +    
    1.19 +    if (window) {
    1.20 +        input->pointer_focus = window;
    1.21 +        SDL_SetMouseFocus(window->sdlwindow);
    1.22 +    }
    1.23  }
    1.24  
    1.25  static void
    1.26 @@ -98,8 +107,10 @@
    1.27  {
    1.28      struct SDL_WaylandInput *input = data;
    1.29  
    1.30 -    SDL_SetMouseFocus(NULL);
    1.31 -    input->pointer_focus = NULL;
    1.32 +    if (input->pointer_focus) {
    1.33 +        SDL_SetMouseFocus(NULL);
    1.34 +        input->pointer_focus = NULL;
    1.35 +    }
    1.36  }
    1.37  
    1.38  static void
    1.39 @@ -110,8 +121,9 @@
    1.40      SDL_WindowData *window = input->pointer_focus;
    1.41      int sx = wl_fixed_to_int(sx_w);
    1.42      int sy = wl_fixed_to_int(sy_w);
    1.43 -
    1.44 -    SDL_SendMouseMotion(window->sdlwindow, 0, 0, sx, sy);
    1.45 +    if (input->pointer_focus) {
    1.46 +        SDL_SendMouseMotion(window->sdlwindow, 0, 0, sx, sy);
    1.47 +    }
    1.48  }
    1.49  
    1.50  static void
    1.51 @@ -122,29 +134,31 @@
    1.52      SDL_WindowData *window = input->pointer_focus;
    1.53      enum wl_pointer_button_state state = state_w;
    1.54      uint32_t sdl_button;
    1.55 +    
    1.56 +    if  (input->pointer_focus) {
    1.57 +        switch (button) {
    1.58 +            case BTN_LEFT:
    1.59 +                sdl_button = SDL_BUTTON_LEFT;
    1.60 +                break;
    1.61 +            case BTN_MIDDLE:
    1.62 +                sdl_button = SDL_BUTTON_MIDDLE;
    1.63 +                break;
    1.64 +            case BTN_RIGHT:
    1.65 +                sdl_button = SDL_BUTTON_RIGHT;
    1.66 +                break;
    1.67 +            case BTN_SIDE:
    1.68 +                sdl_button = SDL_BUTTON_X1;
    1.69 +                break;
    1.70 +            case BTN_EXTRA:
    1.71 +                sdl_button = SDL_BUTTON_X2;
    1.72 +                break;
    1.73 +            default:
    1.74 +                return;
    1.75 +        }
    1.76  
    1.77 -    switch (button) {
    1.78 -    case BTN_LEFT:
    1.79 -        sdl_button = SDL_BUTTON_LEFT;
    1.80 -        break;
    1.81 -    case BTN_MIDDLE:
    1.82 -        sdl_button = SDL_BUTTON_MIDDLE;
    1.83 -        break;
    1.84 -    case BTN_RIGHT:
    1.85 -        sdl_button = SDL_BUTTON_RIGHT;
    1.86 -        break;
    1.87 -    case BTN_SIDE:
    1.88 -        sdl_button = SDL_BUTTON_X1;
    1.89 -        break;
    1.90 -    case BTN_EXTRA:
    1.91 -        sdl_button = SDL_BUTTON_X2;
    1.92 -        break;
    1.93 -    default:
    1.94 -        return;
    1.95 +        SDL_SendMouseButton(window->sdlwindow, 0,
    1.96 +                            state ? SDL_PRESSED : SDL_RELEASED, sdl_button);
    1.97      }
    1.98 -
    1.99 -    SDL_SendMouseButton(window->sdlwindow, 0,
   1.100 -                        state ? SDL_PRESSED : SDL_RELEASED, sdl_button);
   1.101  }
   1.102  
   1.103  static void
   1.104 @@ -156,20 +170,22 @@
   1.105      enum wl_pointer_axis a = axis;
   1.106      int x, y;
   1.107  
   1.108 -    switch (a) {
   1.109 -    case WL_POINTER_AXIS_VERTICAL_SCROLL:
   1.110 -        x = 0;
   1.111 -        y = wl_fixed_to_int(value);
   1.112 -        break;
   1.113 -    case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
   1.114 -        x = wl_fixed_to_int(value);
   1.115 -        y = 0;
   1.116 -        break;
   1.117 -    default:
   1.118 -        return;
   1.119 +    if (input->pointer_focus) {
   1.120 +        switch (a) {
   1.121 +            case WL_POINTER_AXIS_VERTICAL_SCROLL:
   1.122 +                x = 0;
   1.123 +                y = wl_fixed_to_int(value);
   1.124 +                break;
   1.125 +            case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
   1.126 +                x = wl_fixed_to_int(value);
   1.127 +                y = 0;
   1.128 +                break;
   1.129 +            default:
   1.130 +                return;
   1.131 +        }
   1.132 +
   1.133 +        SDL_SendMouseWheel(window->sdlwindow, 0, x, y);
   1.134      }
   1.135 -
   1.136 -    SDL_SendMouseWheel(window->sdlwindow, 0, x, y);
   1.137  }
   1.138  
   1.139  static const struct wl_pointer_listener pointer_listener = {
   1.140 @@ -234,7 +250,9 @@
   1.141  
   1.142      input->keyboard_focus = window;
   1.143      window->keyboard_device = input;
   1.144 -    SDL_SetKeyboardFocus(window->sdlwindow);
   1.145 +    if (window) {
   1.146 +        SDL_SetKeyboardFocus(window->sdlwindow);
   1.147 +    }
   1.148  }
   1.149  
   1.150  static void
   1.151 @@ -340,11 +358,10 @@
   1.152  {
   1.153      struct SDL_WaylandInput *input;
   1.154  
   1.155 -    input = malloc(sizeof *input);
   1.156 +    input = SDL_calloc(1, sizeof *input);
   1.157      if (input == NULL)
   1.158          return;
   1.159  
   1.160 -    memset(input, 0, sizeof *input);
   1.161      input->display = d;
   1.162      input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
   1.163  
   1.164 @@ -378,7 +395,7 @@
   1.165      if (input->xkb.keymap)
   1.166          WAYLAND_xkb_keymap_unref(input->xkb.keymap);
   1.167  
   1.168 -    free(input);
   1.169 +    SDL_free(input);
   1.170      d->input = NULL;
   1.171  }
   1.172