src/video/mir/SDL_mirevents.c
author Brandon Schaefer
Tue, 07 Jun 2016 09:01:23 -0700
changeset 10180 7356b2938fba
parent 10089 25fda20d0173
child 10299 5a45e29a6ea7
permissions -rw-r--r--
MIR: Support relative mouse mode
icculus@8153
     1
/*
icculus@8153
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
icculus@8153
     4
icculus@8153
     5
  This software is provided 'as-is', without any express or implied
icculus@8153
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@8153
     7
  arising from the use of this software.
icculus@8153
     8
icculus@8153
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@8153
    10
  including commercial applications, and to alter it and redistribute it
icculus@8153
    11
  freely, subject to the following restrictions:
icculus@8153
    12
icculus@8153
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@8153
    14
     claim that you wrote the original software. If you use this software
icculus@8153
    15
     in a product, an acknowledgment in the product documentation would be
icculus@8153
    16
     appreciated but is not required.
icculus@8153
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@8153
    18
     misrepresented as being the original software.
icculus@8153
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@8153
    20
*/
icculus@8153
    21
icculus@8153
    22
/*
icculus@8153
    23
  Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
icculus@8153
    24
*/
icculus@8153
    25
icculus@8154
    26
#include "../../SDL_internal.h"
icculus@8154
    27
icculus@8154
    28
#if SDL_VIDEO_DRIVER_MIR
icculus@8153
    29
icculus@8153
    30
#include "../../events/SDL_events_c.h"
icculus@8153
    31
#include "../../events/SDL_keyboard_c.h"
icculus@8153
    32
#include "../../events/SDL_touch_c.h"
icculus@8153
    33
#include "../../events/scancodes_xfree86.h"
icculus@8153
    34
icculus@8153
    35
#include "SDL_mirevents.h"
icculus@8153
    36
#include "SDL_mirwindow.h"
icculus@8153
    37
icculus@8153
    38
#include <xkbcommon/xkbcommon.h>
icculus@8153
    39
icculus@8159
    40
#include "SDL_mirdyn.h"
icculus@8159
    41
icculus@8153
    42
static void
icculus@8153
    43
HandleKeyText(int32_t key_code)
icculus@8153
    44
{
icculus@8153
    45
    char text[8];
icculus@8153
    46
    int size = 0;
icculus@8153
    47
icculus@8159
    48
    size = MIR_xkb_keysym_to_utf8(key_code, text, sizeof text);
icculus@8153
    49
icculus@8153
    50
    if (size > 0) {
icculus@8153
    51
        text[size] = '\0';
icculus@8153
    52
        SDL_SendKeyboardText(text);
icculus@8153
    53
    }
icculus@8153
    54
}
icculus@8153
    55
icculus@8153
    56
static void
icculus@8153
    57
CheckKeyboardFocus(SDL_Window* sdl_window)
icculus@8153
    58
{
icculus@8153
    59
    SDL_Window* keyboard_window = SDL_GetKeyboardFocus();
icculus@8153
    60
bschaefer@10089
    61
    if (sdl_window && keyboard_window != sdl_window)
icculus@8153
    62
        SDL_SetKeyboardFocus(sdl_window);
icculus@8153
    63
}
icculus@8153
    64
icculus@8153
    65
icculus@8153
    66
/* FIXME
icculus@8153
    67
   Mir still needs to implement its IM API, for now we assume
icculus@8153
    68
   a single key press produces a character.
icculus@8153
    69
*/
icculus@8153
    70
static void
bschaefer@10089
    71
HandleKeyEvent(MirKeyboardEvent const* key_event, SDL_Window* window)
icculus@8153
    72
{
bschaefer@10089
    73
    xkb_keysym_t key_code;
bschaefer@10089
    74
    Uint8 key_state;
bschaefer@10089
    75
    int event_scancode;
bschaefer@10089
    76
    uint32_t sdl_scancode = SDL_SCANCODE_UNKNOWN;
bschaefer@10089
    77
bschaefer@10089
    78
    MirKeyboardAction action = MIR_mir_keyboard_event_action(key_event);
bschaefer@10089
    79
bschaefer@10089
    80
    key_state      = SDL_PRESSED;
bschaefer@10089
    81
    key_code       = MIR_mir_keyboard_event_key_code(key_event);
bschaefer@10089
    82
    event_scancode = MIR_mir_keyboard_event_scan_code(key_event);
bschaefer@10089
    83
bschaefer@10089
    84
    if (action == mir_keyboard_action_up)
bschaefer@10089
    85
        key_state = SDL_RELEASED;
icculus@8153
    86
icculus@8153
    87
    CheckKeyboardFocus(window);
icculus@8153
    88
bschaefer@10089
    89
    if (event_scancode < SDL_arraysize(xfree86_scancode_table2))
bschaefer@10089
    90
        sdl_scancode = xfree86_scancode_table2[event_scancode];
icculus@8153
    91
bschaefer@10089
    92
    if (sdl_scancode != SDL_SCANCODE_UNKNOWN)
bschaefer@10089
    93
        SDL_SendKeyboardKey(key_state, sdl_scancode);
icculus@8153
    94
icculus@8153
    95
    if (key_state == SDL_PRESSED)
bschaefer@10089
    96
        HandleKeyText(key_code);
icculus@8153
    97
}
icculus@8153
    98
icculus@8153
    99
static void
bschaefer@10089
   100
HandleMouseButton(SDL_Window* sdl_window, Uint8 state, MirPointerEvent const* pointer)
icculus@8153
   101
{
bschaefer@10089
   102
    uint32_t sdl_button           = SDL_BUTTON_LEFT;
bschaefer@10089
   103
    MirPointerButton button_state = mir_pointer_button_primary;
bschaefer@10089
   104
bschaefer@10089
   105
    static uint32_t old_button_states = 0;
bschaefer@10089
   106
    uint32_t new_button_states = MIR_mir_pointer_event_buttons(pointer);
bschaefer@10089
   107
bschaefer@10089
   108
    // XOR on our old button states vs our new states to get the newley pressed/released button
bschaefer@10089
   109
    button_state = new_button_states ^ old_button_states;
icculus@8153
   110
icculus@8153
   111
    switch (button_state) {
bschaefer@10089
   112
        case mir_pointer_button_primary:
icculus@8153
   113
            sdl_button = SDL_BUTTON_LEFT;
icculus@8153
   114
            break;
bschaefer@10089
   115
        case mir_pointer_button_secondary:
icculus@8153
   116
            sdl_button = SDL_BUTTON_RIGHT;
icculus@8153
   117
            break;
bschaefer@10089
   118
        case mir_pointer_button_tertiary:
icculus@8153
   119
            sdl_button = SDL_BUTTON_MIDDLE;
icculus@8153
   120
            break;
bschaefer@10089
   121
        case mir_pointer_button_forward:
icculus@8153
   122
            sdl_button = SDL_BUTTON_X1;
icculus@8153
   123
            break;
bschaefer@10089
   124
        case mir_pointer_button_back:
icculus@8153
   125
            sdl_button = SDL_BUTTON_X2;
icculus@8153
   126
            break;
icculus@8153
   127
        default:
icculus@8153
   128
            break;
icculus@8153
   129
    }
icculus@8153
   130
bschaefer@10089
   131
    old_button_states = new_button_states;
bschaefer@10089
   132
icculus@8153
   133
    SDL_SendMouseButton(sdl_window, 0, state, sdl_button);
icculus@8153
   134
}
icculus@8153
   135
icculus@8153
   136
static void
brandon@8682
   137
HandleMouseMotion(SDL_Window* sdl_window, int x, int y)
brandon@8682
   138
{
brandon@10180
   139
    SDL_Mouse* mouse = SDL_GetMouse();
brandon@10180
   140
    SDL_SendMouseMotion(sdl_window, 0, mouse->relative_mode, x, y);
brandon@8682
   141
}
brandon@8682
   142
brandon@8682
   143
static void
icculus@8153
   144
HandleTouchPress(int device_id, int source_id, SDL_bool down, float x, float y, float pressure)
icculus@8153
   145
{
icculus@8153
   146
    SDL_SendTouch(device_id, source_id, down, x, y, pressure);
icculus@8153
   147
}
icculus@8153
   148
icculus@8153
   149
static void
icculus@8153
   150
HandleTouchMotion(int device_id, int source_id, float x, float y, float pressure)
icculus@8153
   151
{
icculus@8153
   152
    SDL_SendTouchMotion(device_id, source_id, x, y, pressure);
icculus@8153
   153
}
icculus@8153
   154
icculus@8153
   155
static void
icculus@8153
   156
HandleMouseScroll(SDL_Window* sdl_window, int hscroll, int vscroll)
icculus@8153
   157
{
urkle@9257
   158
    SDL_SendMouseWheel(sdl_window, 0, hscroll, vscroll, SDL_MOUSEWHEEL_NORMAL);
icculus@8153
   159
}
icculus@8153
   160
icculus@8153
   161
static void
icculus@8153
   162
AddTouchDevice(int device_id)
icculus@8153
   163
{
icculus@8153
   164
    if (SDL_AddTouch(device_id, "") < 0)
icculus@8153
   165
        SDL_SetError("Error: can't add touch %s, %d", __FILE__, __LINE__);
icculus@8153
   166
}
icculus@8153
   167
icculus@8153
   168
static void
bschaefer@10089
   169
HandleTouchEvent(MirTouchEvent const* touch, int device_id, SDL_Window* sdl_window)
icculus@8153
   170
{
bschaefer@10089
   171
    int i, point_count;
bschaefer@10089
   172
    point_count = MIR_mir_touch_event_point_count(touch);
bschaefer@10089
   173
bschaefer@10089
   174
    AddTouchDevice(device_id);
bschaefer@10089
   175
bschaefer@10089
   176
    for (i = 0; i < point_count; i++) {
bschaefer@10089
   177
        int id = MIR_mir_touch_event_id(touch, i);
bschaefer@10089
   178
bschaefer@10089
   179
        int width  = sdl_window->w;
bschaefer@10089
   180
        int height = sdl_window->h;
bschaefer@10089
   181
bschaefer@10089
   182
        float x = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_x);
bschaefer@10089
   183
        float y = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_y);
bschaefer@10089
   184
bschaefer@10089
   185
        float n_x = x / width;
bschaefer@10089
   186
        float n_y = y / height;
icculus@8153
   187
bschaefer@10089
   188
        float pressure = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_pressure);
icculus@8153
   189
bschaefer@10089
   190
        switch (MIR_mir_touch_event_action(touch, i)) {
bschaefer@10089
   191
            case mir_touch_action_up:
bschaefer@10089
   192
                HandleTouchPress(device_id, id, SDL_FALSE, n_x, n_y, pressure);
bschaefer@10089
   193
                break;
bschaefer@10089
   194
            case mir_touch_action_down:
bschaefer@10089
   195
                HandleTouchPress(device_id, id, SDL_TRUE, n_x, n_y, pressure);
bschaefer@10089
   196
                break;
bschaefer@10089
   197
            case mir_touch_action_change:
bschaefer@10089
   198
                HandleTouchMotion(device_id, id, n_x, n_y, pressure);
bschaefer@10089
   199
                break;
bschaefer@10089
   200
        }
bschaefer@10089
   201
    }
bschaefer@10089
   202
}
bschaefer@10089
   203
bschaefer@10089
   204
static void
bschaefer@10089
   205
HandleMouseEvent(MirPointerEvent const* pointer, SDL_Window* sdl_window)
bschaefer@10089
   206
{
bschaefer@10089
   207
    SDL_SetMouseFocus(sdl_window);
icculus@8153
   208
bschaefer@10089
   209
    switch (MIR_mir_pointer_event_action(pointer)) {
bschaefer@10089
   210
        case mir_pointer_action_button_down:
bschaefer@10089
   211
            HandleMouseButton(sdl_window, SDL_PRESSED, pointer);
bschaefer@10089
   212
            break;
bschaefer@10089
   213
        case mir_pointer_action_button_up:
bschaefer@10089
   214
            HandleMouseButton(sdl_window, SDL_RELEASED, pointer);
bschaefer@10089
   215
            break;
bschaefer@10089
   216
        case mir_pointer_action_motion: {
bschaefer@10089
   217
            int x, y;
bschaefer@10089
   218
            int hscroll, vscroll;
bschaefer@10089
   219
            SDL_Mouse* mouse = SDL_GetMouse();
bschaefer@10089
   220
            x = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_x);
bschaefer@10089
   221
            y = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_y);
brandon@10180
   222
brandon@10180
   223
            if (mouse && (mouse->x != x || mouse->y != y)) {
brandon@10180
   224
                if (mouse->relative_mode) {
brandon@10180
   225
                    int relative_x = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_relative_x);
brandon@10180
   226
                    int relative_y = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_relative_y);
brandon@10180
   227
                    HandleMouseMotion(sdl_window, relative_x, relative_y);
brandon@10180
   228
                }
brandon@10180
   229
                else {
brandon@10180
   230
                    HandleMouseMotion(sdl_window, x, y);
brandon@10180
   231
                }
brandon@10180
   232
            }
brandon@10180
   233
bschaefer@10089
   234
            hscroll = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_hscroll);
bschaefer@10089
   235
            vscroll = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_vscroll);
bschaefer@10089
   236
            if (vscroll != 0 || hscroll != 0)
bschaefer@10089
   237
                HandleMouseScroll(sdl_window, hscroll, vscroll);
bschaefer@10089
   238
        }
icculus@8153
   239
            break;
bschaefer@10089
   240
        case mir_pointer_action_leave:
bschaefer@10089
   241
            SDL_SetMouseFocus(NULL);
bschaefer@10089
   242
            break;
bschaefer@10089
   243
        case mir_pointer_action_enter:
bschaefer@10089
   244
        default:
icculus@8153
   245
            break;
bschaefer@10089
   246
    }
bschaefer@10089
   247
}
bschaefer@10089
   248
bschaefer@10089
   249
static void
bschaefer@10089
   250
MIR_HandleInput(MirInputEvent const* input_event, SDL_Window* window)
bschaefer@10089
   251
{
bschaefer@10089
   252
    switch (MIR_mir_input_event_get_type(input_event)) {
bschaefer@10089
   253
        case (mir_input_event_type_key):
bschaefer@10089
   254
            HandleKeyEvent(MIR_mir_input_event_get_keyboard_event(input_event), window);
bschaefer@10089
   255
            break;
bschaefer@10089
   256
        case (mir_input_event_type_pointer):
bschaefer@10089
   257
            HandleMouseEvent(MIR_mir_input_event_get_pointer_event(input_event), window);
bschaefer@10089
   258
            break;
bschaefer@10089
   259
        case (mir_input_event_type_touch):
bschaefer@10089
   260
            HandleTouchEvent(MIR_mir_input_event_get_touch_event(input_event),
bschaefer@10089
   261
                             MIR_mir_input_event_get_device_id(input_event),
bschaefer@10089
   262
                             window);
icculus@8153
   263
            break;
icculus@8153
   264
        default:
icculus@8153
   265
            break;
icculus@8153
   266
    }
icculus@8153
   267
}
icculus@8153
   268
icculus@8153
   269
static void
bschaefer@10089
   270
MIR_HandleResize(MirResizeEvent const* resize_event, SDL_Window* window)
icculus@8153
   271
{
bschaefer@10089
   272
    int new_w = MIR_mir_resize_event_get_width (resize_event);
bschaefer@10089
   273
    int new_h = MIR_mir_resize_event_get_height(resize_event);
icculus@8153
   274
bschaefer@10089
   275
    int old_w = window->w;
bschaefer@10089
   276
    int old_h = window->h;
icculus@8153
   277
bschaefer@10089
   278
    if (new_w != old_w || new_h != old_h)
bschaefer@10089
   279
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, new_w, new_h);
icculus@8153
   280
}
icculus@8153
   281
icculus@8153
   282
void
bschaefer@10089
   283
MIR_HandleEvent(MirSurface* surface, MirEvent const* ev, void* context)
icculus@8153
   284
{
bschaefer@10089
   285
    MirEventType event_type = MIR_mir_event_get_type(ev);
bschaefer@10089
   286
    SDL_Window* window      = (SDL_Window*)context;
bschaefer@10089
   287
bschaefer@10089
   288
    if (window) {
bschaefer@10089
   289
        switch (event_type) {
bschaefer@10089
   290
            case (mir_event_type_input):
bschaefer@10089
   291
                MIR_HandleInput(MIR_mir_event_get_input_event(ev), window);
bschaefer@10089
   292
                break;
bschaefer@10089
   293
            case (mir_event_type_resize):
bschaefer@10089
   294
                MIR_HandleResize(MIR_mir_event_get_resize_event(ev), window);
bschaefer@10089
   295
                break;
bschaefer@10089
   296
            default:
bschaefer@10089
   297
                break;
bschaefer@10089
   298
        }
icculus@8153
   299
    }
icculus@8153
   300
}
icculus@8154
   301
icculus@8154
   302
#endif /* SDL_VIDEO_DRIVER_MIR */
icculus@8154
   303
icculus@8154
   304
/* vi: set ts=4 sw=4 expandtab: */