src/video/mir/SDL_mirevents.c
changeset 10089 25fda20d0173
parent 9998 f67cf37e9cd4
child 10180 7356b2938fba
     1.1 --- a/src/video/mir/SDL_mirevents.c	Sun Feb 21 17:21:29 2016 -0500
     1.2 +++ b/src/video/mir/SDL_mirevents.c	Sun Feb 21 15:19:35 2016 -0800
     1.3 @@ -58,7 +58,7 @@
     1.4  {
     1.5      SDL_Window* keyboard_window = SDL_GetKeyboardFocus();
     1.6  
     1.7 -    if (keyboard_window != sdl_window)
     1.8 +    if (sdl_window && keyboard_window != sdl_window)
     1.9          SDL_SetKeyboardFocus(sdl_window);
    1.10  }
    1.11  
    1.12 @@ -68,51 +68,68 @@
    1.13     a single key press produces a character.
    1.14  */
    1.15  static void
    1.16 -HandleKeyEvent(MirKeyEvent const ev, SDL_Window* window)
    1.17 +HandleKeyEvent(MirKeyboardEvent const* key_event, SDL_Window* window)
    1.18  {
    1.19 -    uint32_t scancode = SDL_SCANCODE_UNKNOWN;
    1.20 -    Uint8 key_state = ev.action == mir_key_action_up ? SDL_RELEASED : SDL_PRESSED;
    1.21 +    xkb_keysym_t key_code;
    1.22 +    Uint8 key_state;
    1.23 +    int event_scancode;
    1.24 +    uint32_t sdl_scancode = SDL_SCANCODE_UNKNOWN;
    1.25 +
    1.26 +    MirKeyboardAction action = MIR_mir_keyboard_event_action(key_event);
    1.27 +
    1.28 +    key_state      = SDL_PRESSED;
    1.29 +    key_code       = MIR_mir_keyboard_event_key_code(key_event);
    1.30 +    event_scancode = MIR_mir_keyboard_event_scan_code(key_event);
    1.31 +
    1.32 +    if (action == mir_keyboard_action_up)
    1.33 +        key_state = SDL_RELEASED;
    1.34  
    1.35      CheckKeyboardFocus(window);
    1.36  
    1.37 -    if (ev.scan_code < SDL_arraysize(xfree86_scancode_table2))
    1.38 -        scancode = xfree86_scancode_table2[ev.scan_code];
    1.39 +    if (event_scancode < SDL_arraysize(xfree86_scancode_table2))
    1.40 +        sdl_scancode = xfree86_scancode_table2[event_scancode];
    1.41  
    1.42 -    if (scancode != SDL_SCANCODE_UNKNOWN)
    1.43 -        SDL_SendKeyboardKey(key_state, scancode);
    1.44 +    if (sdl_scancode != SDL_SCANCODE_UNKNOWN)
    1.45 +        SDL_SendKeyboardKey(key_state, sdl_scancode);
    1.46  
    1.47      if (key_state == SDL_PRESSED)
    1.48 -        HandleKeyText(ev.key_code);
    1.49 +        HandleKeyText(key_code);
    1.50  }
    1.51  
    1.52  static void
    1.53 -HandleMouseButton(SDL_Window* sdl_window, Uint8 state, MirMotionButton button_state)
    1.54 +HandleMouseButton(SDL_Window* sdl_window, Uint8 state, MirPointerEvent const* pointer)
    1.55  {
    1.56 -    static uint32_t last_sdl_button;
    1.57 -    uint32_t sdl_button;
    1.58 +    uint32_t sdl_button           = SDL_BUTTON_LEFT;
    1.59 +    MirPointerButton button_state = mir_pointer_button_primary;
    1.60 +
    1.61 +    static uint32_t old_button_states = 0;
    1.62 +    uint32_t new_button_states = MIR_mir_pointer_event_buttons(pointer);
    1.63 +
    1.64 +    // XOR on our old button states vs our new states to get the newley pressed/released button
    1.65 +    button_state = new_button_states ^ old_button_states;
    1.66  
    1.67      switch (button_state) {
    1.68 -        case mir_motion_button_primary:
    1.69 +        case mir_pointer_button_primary:
    1.70              sdl_button = SDL_BUTTON_LEFT;
    1.71              break;
    1.72 -        case mir_motion_button_secondary:
    1.73 +        case mir_pointer_button_secondary:
    1.74              sdl_button = SDL_BUTTON_RIGHT;
    1.75              break;
    1.76 -        case mir_motion_button_tertiary:
    1.77 +        case mir_pointer_button_tertiary:
    1.78              sdl_button = SDL_BUTTON_MIDDLE;
    1.79              break;
    1.80 -        case mir_motion_button_forward:
    1.81 +        case mir_pointer_button_forward:
    1.82              sdl_button = SDL_BUTTON_X1;
    1.83              break;
    1.84 -        case mir_motion_button_back:
    1.85 +        case mir_pointer_button_back:
    1.86              sdl_button = SDL_BUTTON_X2;
    1.87              break;
    1.88          default:
    1.89 -            sdl_button = last_sdl_button;
    1.90              break;
    1.91      }
    1.92  
    1.93 -    last_sdl_button = sdl_button;
    1.94 +    old_button_states = new_button_states;
    1.95 +
    1.96      SDL_SendMouseButton(sdl_window, 0, state, sdl_button);
    1.97  }
    1.98  
    1.99 @@ -148,34 +165,91 @@
   1.100  }
   1.101  
   1.102  static void
   1.103 -HandleTouchEvent(MirMotionEvent const motion, int cord_index, SDL_Window* sdl_window)
   1.104 +HandleTouchEvent(MirTouchEvent const* touch, int device_id, SDL_Window* sdl_window)
   1.105  {
   1.106 -    int device_id = motion.device_id;
   1.107 -    int id = motion.pointer_coordinates[cord_index].id;
   1.108 +    int i, point_count;
   1.109 +    point_count = MIR_mir_touch_event_point_count(touch);
   1.110  
   1.111 -    int width  = sdl_window->w;
   1.112 -    int height = sdl_window->h;
   1.113 -    float x   = motion.pointer_coordinates[cord_index].x;
   1.114 -    float y   = motion.pointer_coordinates[cord_index].y;
   1.115 +    AddTouchDevice(device_id);
   1.116  
   1.117 -    float n_x = x / width;
   1.118 -    float n_y = y / height;
   1.119 -    float pressure = motion.pointer_coordinates[cord_index].pressure;
   1.120 +    for (i = 0; i < point_count; i++) {
   1.121 +        int id = MIR_mir_touch_event_id(touch, i);
   1.122  
   1.123 -    AddTouchDevice(motion.device_id);
   1.124 +        int width  = sdl_window->w;
   1.125 +        int height = sdl_window->h;
   1.126  
   1.127 -    switch (motion.action) {
   1.128 -        case mir_motion_action_down:
   1.129 -        case mir_motion_action_pointer_down:
   1.130 -            HandleTouchPress(device_id, id, SDL_TRUE, n_x, n_y, pressure);
   1.131 +        float x = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_x);
   1.132 +        float y = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_y);
   1.133 +
   1.134 +        float n_x = x / width;
   1.135 +        float n_y = y / height;
   1.136 +
   1.137 +        float pressure = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_pressure);
   1.138 +
   1.139 +        switch (MIR_mir_touch_event_action(touch, i)) {
   1.140 +            case mir_touch_action_up:
   1.141 +                HandleTouchPress(device_id, id, SDL_FALSE, n_x, n_y, pressure);
   1.142 +                break;
   1.143 +            case mir_touch_action_down:
   1.144 +                HandleTouchPress(device_id, id, SDL_TRUE, n_x, n_y, pressure);
   1.145 +                break;
   1.146 +            case mir_touch_action_change:
   1.147 +                HandleTouchMotion(device_id, id, n_x, n_y, pressure);
   1.148 +                break;
   1.149 +        }
   1.150 +    }
   1.151 +}
   1.152 +
   1.153 +static void
   1.154 +HandleMouseEvent(MirPointerEvent const* pointer, SDL_Window* sdl_window)
   1.155 +{
   1.156 +    SDL_SetMouseFocus(sdl_window);
   1.157 +
   1.158 +    switch (MIR_mir_pointer_event_action(pointer)) {
   1.159 +        case mir_pointer_action_button_down:
   1.160 +            HandleMouseButton(sdl_window, SDL_PRESSED, pointer);
   1.161              break;
   1.162 -        case mir_motion_action_up:
   1.163 -        case mir_motion_action_pointer_up:
   1.164 -            HandleTouchPress(device_id, id, SDL_FALSE, n_x, n_y, pressure);
   1.165 +        case mir_pointer_action_button_up:
   1.166 +            HandleMouseButton(sdl_window, SDL_RELEASED, pointer);
   1.167              break;
   1.168 -        case mir_motion_action_hover_move:
   1.169 -        case mir_motion_action_move:
   1.170 -            HandleTouchMotion(device_id, id, n_x, n_y, pressure);
   1.171 +        case mir_pointer_action_motion: {
   1.172 +            int x, y;
   1.173 +            int hscroll, vscroll;
   1.174 +            SDL_Mouse* mouse = SDL_GetMouse();
   1.175 +            x = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_x);
   1.176 +            y = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_y);
   1.177 +            hscroll = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_hscroll);
   1.178 +            vscroll = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_vscroll);
   1.179 +
   1.180 +            if (mouse && (mouse->x != x || mouse->y != y))
   1.181 +                HandleMouseMotion(sdl_window, x, y);
   1.182 +            if (vscroll != 0 || hscroll != 0)
   1.183 +                HandleMouseScroll(sdl_window, hscroll, vscroll);
   1.184 +        }
   1.185 +            break;
   1.186 +        case mir_pointer_action_leave:
   1.187 +            SDL_SetMouseFocus(NULL);
   1.188 +            break;
   1.189 +        case mir_pointer_action_enter:
   1.190 +        default:
   1.191 +            break;
   1.192 +    }
   1.193 +}
   1.194 +
   1.195 +static void
   1.196 +MIR_HandleInput(MirInputEvent const* input_event, SDL_Window* window)
   1.197 +{
   1.198 +    switch (MIR_mir_input_event_get_type(input_event)) {
   1.199 +        case (mir_input_event_type_key):
   1.200 +            HandleKeyEvent(MIR_mir_input_event_get_keyboard_event(input_event), window);
   1.201 +            break;
   1.202 +        case (mir_input_event_type_pointer):
   1.203 +            HandleMouseEvent(MIR_mir_input_event_get_pointer_event(input_event), window);
   1.204 +            break;
   1.205 +        case (mir_input_event_type_touch):
   1.206 +            HandleTouchEvent(MIR_mir_input_event_get_touch_event(input_event),
   1.207 +                             MIR_mir_input_event_get_device_id(input_event),
   1.208 +                             window);
   1.209              break;
   1.210          default:
   1.211              break;
   1.212 @@ -183,69 +257,35 @@
   1.213  }
   1.214  
   1.215  static void
   1.216 -HandleMouseEvent(MirMotionEvent const motion, int cord_index, SDL_Window* sdl_window)
   1.217 +MIR_HandleResize(MirResizeEvent const* resize_event, SDL_Window* window)
   1.218  {
   1.219 -    SDL_SetMouseFocus(sdl_window);
   1.220 +    int new_w = MIR_mir_resize_event_get_width (resize_event);
   1.221 +    int new_h = MIR_mir_resize_event_get_height(resize_event);
   1.222  
   1.223 -    switch (motion.action) {
   1.224 -        case mir_motion_action_down:
   1.225 -        case mir_motion_action_pointer_down:
   1.226 -            HandleMouseButton(sdl_window, SDL_PRESSED, motion.button_state);
   1.227 -            break;
   1.228 -        case mir_motion_action_up:
   1.229 -        case mir_motion_action_pointer_up:
   1.230 -            HandleMouseButton(sdl_window, SDL_RELEASED, motion.button_state);
   1.231 -            break;
   1.232 -        case mir_motion_action_hover_move:
   1.233 -        case mir_motion_action_move:
   1.234 -            HandleMouseMotion(sdl_window,
   1.235 -                              motion.pointer_coordinates[cord_index].x,
   1.236 -                              motion.pointer_coordinates[cord_index].y);
   1.237 -            break;
   1.238 -        case mir_motion_action_outside:
   1.239 -            SDL_SetMouseFocus(NULL);
   1.240 -            break;
   1.241 -        case mir_motion_action_scroll:
   1.242 -            HandleMouseScroll(sdl_window,
   1.243 -                              motion.pointer_coordinates[cord_index].hscroll,
   1.244 -                              motion.pointer_coordinates[cord_index].vscroll);
   1.245 -            break;
   1.246 -        case mir_motion_action_cancel:
   1.247 -        case mir_motion_action_hover_enter:
   1.248 -        case mir_motion_action_hover_exit:
   1.249 -            break;
   1.250 -        default:
   1.251 -            break;
   1.252 -    }
   1.253 -}
   1.254 +    int old_w = window->w;
   1.255 +    int old_h = window->h;
   1.256  
   1.257 -static void
   1.258 -HandleMotionEvent(MirMotionEvent const motion, SDL_Window* sdl_window)
   1.259 -{
   1.260 -    int cord_index;
   1.261 -    for (cord_index = 0; cord_index < motion.pointer_count; cord_index++) {
   1.262 -        if (motion.pointer_coordinates[cord_index].tool_type == mir_motion_tool_type_finger) {
   1.263 -            HandleTouchEvent(motion, cord_index, sdl_window);
   1.264 -        }
   1.265 -        else {
   1.266 -            HandleMouseEvent(motion, cord_index, sdl_window);
   1.267 -        }
   1.268 -    }
   1.269 +    if (new_w != old_w || new_h != old_h)
   1.270 +        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, new_w, new_h);
   1.271  }
   1.272  
   1.273  void
   1.274 -MIR_HandleInput(MirSurface* surface, MirEvent const* ev, void* context)
   1.275 +MIR_HandleEvent(MirSurface* surface, MirEvent const* ev, void* context)
   1.276  {
   1.277 -    SDL_Window* window = (SDL_Window*)context;
   1.278 -    switch (ev->type) {
   1.279 -        case (mir_event_type_key):
   1.280 -            HandleKeyEvent(ev->key, window);
   1.281 -            break;
   1.282 -        case (mir_event_type_motion):
   1.283 -            HandleMotionEvent(ev->motion, window);
   1.284 -            break;
   1.285 -        default:
   1.286 -            break;
   1.287 +    MirEventType event_type = MIR_mir_event_get_type(ev);
   1.288 +    SDL_Window* window      = (SDL_Window*)context;
   1.289 +
   1.290 +    if (window) {
   1.291 +        switch (event_type) {
   1.292 +            case (mir_event_type_input):
   1.293 +                MIR_HandleInput(MIR_mir_event_get_input_event(ev), window);
   1.294 +                break;
   1.295 +            case (mir_event_type_resize):
   1.296 +                MIR_HandleResize(MIR_mir_event_get_resize_event(ev), window);
   1.297 +                break;
   1.298 +            default:
   1.299 +                break;
   1.300 +        }
   1.301      }
   1.302  }
   1.303