src/video/mir/SDL_mirevents.c
author bschaefer
Sun, 21 Feb 2016 15:19:35 -0800
changeset 10089 25fda20d0173
parent 9998 f67cf37e9cd4
child 10180 7356b2938fba
permissions -rw-r--r--
Fix API/ABI breakage in Mir 0.13/0.14.
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@8682
   139
    SDL_SendMouseMotion(sdl_window, 0, 0, x, y);
brandon@8682
   140
}
brandon@8682
   141
brandon@8682
   142
static void
icculus@8153
   143
HandleTouchPress(int device_id, int source_id, SDL_bool down, float x, float y, float pressure)
icculus@8153
   144
{
icculus@8153
   145
    SDL_SendTouch(device_id, source_id, down, x, y, pressure);
icculus@8153
   146
}
icculus@8153
   147
icculus@8153
   148
static void
icculus@8153
   149
HandleTouchMotion(int device_id, int source_id, float x, float y, float pressure)
icculus@8153
   150
{
icculus@8153
   151
    SDL_SendTouchMotion(device_id, source_id, x, y, pressure);
icculus@8153
   152
}
icculus@8153
   153
icculus@8153
   154
static void
icculus@8153
   155
HandleMouseScroll(SDL_Window* sdl_window, int hscroll, int vscroll)
icculus@8153
   156
{
urkle@9257
   157
    SDL_SendMouseWheel(sdl_window, 0, hscroll, vscroll, SDL_MOUSEWHEEL_NORMAL);
icculus@8153
   158
}
icculus@8153
   159
icculus@8153
   160
static void
icculus@8153
   161
AddTouchDevice(int device_id)
icculus@8153
   162
{
icculus@8153
   163
    if (SDL_AddTouch(device_id, "") < 0)
icculus@8153
   164
        SDL_SetError("Error: can't add touch %s, %d", __FILE__, __LINE__);
icculus@8153
   165
}
icculus@8153
   166
icculus@8153
   167
static void
bschaefer@10089
   168
HandleTouchEvent(MirTouchEvent const* touch, int device_id, SDL_Window* sdl_window)
icculus@8153
   169
{
bschaefer@10089
   170
    int i, point_count;
bschaefer@10089
   171
    point_count = MIR_mir_touch_event_point_count(touch);
icculus@8153
   172
bschaefer@10089
   173
    AddTouchDevice(device_id);
icculus@8153
   174
bschaefer@10089
   175
    for (i = 0; i < point_count; i++) {
bschaefer@10089
   176
        int id = MIR_mir_touch_event_id(touch, i);
icculus@8153
   177
bschaefer@10089
   178
        int width  = sdl_window->w;
bschaefer@10089
   179
        int height = sdl_window->h;
icculus@8153
   180
bschaefer@10089
   181
        float x = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_x);
bschaefer@10089
   182
        float y = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_y);
bschaefer@10089
   183
bschaefer@10089
   184
        float n_x = x / width;
bschaefer@10089
   185
        float n_y = y / height;
bschaefer@10089
   186
bschaefer@10089
   187
        float pressure = MIR_mir_touch_event_axis_value(touch, i, mir_touch_axis_pressure);
bschaefer@10089
   188
bschaefer@10089
   189
        switch (MIR_mir_touch_event_action(touch, i)) {
bschaefer@10089
   190
            case mir_touch_action_up:
bschaefer@10089
   191
                HandleTouchPress(device_id, id, SDL_FALSE, n_x, n_y, pressure);
bschaefer@10089
   192
                break;
bschaefer@10089
   193
            case mir_touch_action_down:
bschaefer@10089
   194
                HandleTouchPress(device_id, id, SDL_TRUE, n_x, n_y, pressure);
bschaefer@10089
   195
                break;
bschaefer@10089
   196
            case mir_touch_action_change:
bschaefer@10089
   197
                HandleTouchMotion(device_id, id, n_x, n_y, pressure);
bschaefer@10089
   198
                break;
bschaefer@10089
   199
        }
bschaefer@10089
   200
    }
bschaefer@10089
   201
}
bschaefer@10089
   202
bschaefer@10089
   203
static void
bschaefer@10089
   204
HandleMouseEvent(MirPointerEvent const* pointer, SDL_Window* sdl_window)
bschaefer@10089
   205
{
bschaefer@10089
   206
    SDL_SetMouseFocus(sdl_window);
bschaefer@10089
   207
bschaefer@10089
   208
    switch (MIR_mir_pointer_event_action(pointer)) {
bschaefer@10089
   209
        case mir_pointer_action_button_down:
bschaefer@10089
   210
            HandleMouseButton(sdl_window, SDL_PRESSED, pointer);
icculus@8153
   211
            break;
bschaefer@10089
   212
        case mir_pointer_action_button_up:
bschaefer@10089
   213
            HandleMouseButton(sdl_window, SDL_RELEASED, pointer);
icculus@8153
   214
            break;
bschaefer@10089
   215
        case mir_pointer_action_motion: {
bschaefer@10089
   216
            int x, y;
bschaefer@10089
   217
            int hscroll, vscroll;
bschaefer@10089
   218
            SDL_Mouse* mouse = SDL_GetMouse();
bschaefer@10089
   219
            x = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_x);
bschaefer@10089
   220
            y = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_y);
bschaefer@10089
   221
            hscroll = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_hscroll);
bschaefer@10089
   222
            vscroll = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_vscroll);
bschaefer@10089
   223
bschaefer@10089
   224
            if (mouse && (mouse->x != x || mouse->y != y))
bschaefer@10089
   225
                HandleMouseMotion(sdl_window, x, y);
bschaefer@10089
   226
            if (vscroll != 0 || hscroll != 0)
bschaefer@10089
   227
                HandleMouseScroll(sdl_window, hscroll, vscroll);
bschaefer@10089
   228
        }
bschaefer@10089
   229
            break;
bschaefer@10089
   230
        case mir_pointer_action_leave:
bschaefer@10089
   231
            SDL_SetMouseFocus(NULL);
bschaefer@10089
   232
            break;
bschaefer@10089
   233
        case mir_pointer_action_enter:
bschaefer@10089
   234
        default:
bschaefer@10089
   235
            break;
bschaefer@10089
   236
    }
bschaefer@10089
   237
}
bschaefer@10089
   238
bschaefer@10089
   239
static void
bschaefer@10089
   240
MIR_HandleInput(MirInputEvent const* input_event, SDL_Window* window)
bschaefer@10089
   241
{
bschaefer@10089
   242
    switch (MIR_mir_input_event_get_type(input_event)) {
bschaefer@10089
   243
        case (mir_input_event_type_key):
bschaefer@10089
   244
            HandleKeyEvent(MIR_mir_input_event_get_keyboard_event(input_event), window);
bschaefer@10089
   245
            break;
bschaefer@10089
   246
        case (mir_input_event_type_pointer):
bschaefer@10089
   247
            HandleMouseEvent(MIR_mir_input_event_get_pointer_event(input_event), window);
bschaefer@10089
   248
            break;
bschaefer@10089
   249
        case (mir_input_event_type_touch):
bschaefer@10089
   250
            HandleTouchEvent(MIR_mir_input_event_get_touch_event(input_event),
bschaefer@10089
   251
                             MIR_mir_input_event_get_device_id(input_event),
bschaefer@10089
   252
                             window);
icculus@8153
   253
            break;
icculus@8153
   254
        default:
icculus@8153
   255
            break;
icculus@8153
   256
    }
icculus@8153
   257
}
icculus@8153
   258
icculus@8153
   259
static void
bschaefer@10089
   260
MIR_HandleResize(MirResizeEvent const* resize_event, SDL_Window* window)
icculus@8153
   261
{
bschaefer@10089
   262
    int new_w = MIR_mir_resize_event_get_width (resize_event);
bschaefer@10089
   263
    int new_h = MIR_mir_resize_event_get_height(resize_event);
icculus@8153
   264
bschaefer@10089
   265
    int old_w = window->w;
bschaefer@10089
   266
    int old_h = window->h;
icculus@8153
   267
bschaefer@10089
   268
    if (new_w != old_w || new_h != old_h)
bschaefer@10089
   269
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, new_w, new_h);
icculus@8153
   270
}
icculus@8153
   271
icculus@8153
   272
void
bschaefer@10089
   273
MIR_HandleEvent(MirSurface* surface, MirEvent const* ev, void* context)
icculus@8153
   274
{
bschaefer@10089
   275
    MirEventType event_type = MIR_mir_event_get_type(ev);
bschaefer@10089
   276
    SDL_Window* window      = (SDL_Window*)context;
bschaefer@10089
   277
bschaefer@10089
   278
    if (window) {
bschaefer@10089
   279
        switch (event_type) {
bschaefer@10089
   280
            case (mir_event_type_input):
bschaefer@10089
   281
                MIR_HandleInput(MIR_mir_event_get_input_event(ev), window);
bschaefer@10089
   282
                break;
bschaefer@10089
   283
            case (mir_event_type_resize):
bschaefer@10089
   284
                MIR_HandleResize(MIR_mir_event_get_resize_event(ev), window);
bschaefer@10089
   285
                break;
bschaefer@10089
   286
            default:
bschaefer@10089
   287
                break;
bschaefer@10089
   288
        }
icculus@8153
   289
    }
icculus@8153
   290
}
icculus@8154
   291
icculus@8154
   292
#endif /* SDL_VIDEO_DRIVER_MIR */
icculus@8154
   293
icculus@8154
   294
/* vi: set ts=4 sw=4 expandtab: */