src/video/android/SDL_androidmouse.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jan 2018 10:03:25 -0800
changeset 11811 5d94cb6b24d3
parent 11237 11c69137bfd5
child 11931 d479188e0155
permissions -rw-r--r--
Updated copyright for 2018
joseba@9438
     1
/*
joseba@9438
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
joseba@9438
     4
joseba@9438
     5
  This software is provided 'as-is', without any express or implied
joseba@9438
     6
  warranty.  In no event will the authors be held liable for any damages
joseba@9438
     7
  arising from the use of this software.
joseba@9438
     8
joseba@9438
     9
  Permission is granted to anyone to use this software for any purpose,
joseba@9438
    10
  including commercial applications, and to alter it and redistribute it
joseba@9438
    11
  freely, subject to the following restrictions:
joseba@9438
    12
joseba@9438
    13
  1. The origin of this software must not be misrepresented; you must not
joseba@9438
    14
     claim that you wrote the original software. If you use this software
joseba@9438
    15
     in a product, an acknowledgment in the product documentation would be
joseba@9438
    16
     appreciated but is not required.
joseba@9438
    17
  2. Altered source versions must be plainly marked as such, and must not be
joseba@9438
    18
     misrepresented as being the original software.
joseba@9438
    19
  3. This notice may not be removed or altered from any source distribution.
joseba@9438
    20
*/
joseba@9438
    21
joseba@9438
    22
#include "../../SDL_internal.h"
joseba@9438
    23
joseba@9438
    24
#if SDL_VIDEO_DRIVER_ANDROID
joseba@9438
    25
joseba@9438
    26
#include "SDL_androidmouse.h"
joseba@9438
    27
joseba@9438
    28
#include "SDL_events.h"
joseba@9438
    29
#include "../../events/SDL_mouse_c.h"
joseba@9438
    30
joseba@9438
    31
#include "../../core/android/SDL_android.h"
joseba@9438
    32
slouken@11237
    33
/* See Android's MotionEvent class for constants */
joseba@9438
    34
#define ACTION_DOWN 0
joseba@9438
    35
#define ACTION_UP 1
philipp@10295
    36
#define ACTION_MOVE 2
joseba@9438
    37
#define ACTION_HOVER_MOVE 7
joseba@9438
    38
#define ACTION_SCROLL 8
joseba@9438
    39
#define BUTTON_PRIMARY 1
joseba@9438
    40
#define BUTTON_SECONDARY 2
joseba@9438
    41
#define BUTTON_TERTIARY 4
philipp@10049
    42
#define BUTTON_BACK 8
philipp@10049
    43
#define BUTTON_FORWARD 16
joseba@9438
    44
slouken@11237
    45
/* Last known Android mouse button state (includes all buttons) */
slouken@11237
    46
static int last_state;
philipp@10051
    47
philipp@10051
    48
void
philipp@10051
    49
Android_InitMouse(void)
philipp@10051
    50
{
slouken@11237
    51
    last_state = 0;
philipp@10051
    52
}
philipp@10051
    53
slouken@11237
    54
/* Translate Android mouse button state to SDL mouse button */
slouken@11237
    55
static Uint8
slouken@11237
    56
TranslateButton(int state)
slouken@11237
    57
{
slouken@11237
    58
    if (state & BUTTON_PRIMARY) {
slouken@11237
    59
        return SDL_BUTTON_LEFT;
slouken@11237
    60
    } else if (state & BUTTON_SECONDARY) {
slouken@11237
    61
        return SDL_BUTTON_RIGHT;
slouken@11237
    62
    } else if (state & BUTTON_TERTIARY) {
slouken@11237
    63
        return SDL_BUTTON_MIDDLE;
slouken@11237
    64
    } else if (state & BUTTON_FORWARD) {
slouken@11237
    65
        return SDL_BUTTON_X1;
slouken@11237
    66
    } else if (state & BUTTON_BACK) {
slouken@11237
    67
        return SDL_BUTTON_X2;
slouken@11237
    68
    } else {
slouken@11237
    69
        return 0;
slouken@11237
    70
    }
slouken@11237
    71
}
slouken@11237
    72
slouken@11237
    73
void
slouken@11237
    74
Android_OnMouse(int state, int action, float x, float y)
slouken@11237
    75
{
slouken@11237
    76
    int changes;
slouken@11237
    77
    Uint8 button;
slouken@11237
    78
joseba@9438
    79
    if (!Android_Window) {
joseba@9438
    80
        return;
joseba@9438
    81
    }
joseba@9438
    82
joseba@9438
    83
    switch(action) {
joseba@9438
    84
        case ACTION_DOWN:
slouken@11237
    85
            changes = state & ~last_state;
slouken@11237
    86
            button = TranslateButton(changes);
slouken@11237
    87
            last_state = state;
joseba@9438
    88
            SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
slouken@11237
    89
            SDL_SendMouseButton(Android_Window, 0, SDL_PRESSED, button);
joseba@9438
    90
            break;
joseba@9438
    91
joseba@9438
    92
        case ACTION_UP:
slouken@11237
    93
            changes = last_state & ~state;
slouken@11237
    94
            button = TranslateButton(changes);
slouken@11237
    95
            last_state = state;
joseba@9438
    96
            SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
slouken@11237
    97
            SDL_SendMouseButton(Android_Window, 0, SDL_RELEASED, button);
joseba@9438
    98
            break;
joseba@9438
    99
philipp@10295
   100
        case ACTION_MOVE:
joseba@9438
   101
        case ACTION_HOVER_MOVE:
joseba@9438
   102
            SDL_SendMouseMotion(Android_Window, 0, 0, x, y);
joseba@9438
   103
            break;
joseba@9438
   104
joseba@9438
   105
        case ACTION_SCROLL:
joseba@9439
   106
            SDL_SendMouseWheel(Android_Window, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
joseba@9438
   107
            break;
joseba@9438
   108
joseba@9438
   109
        default:
joseba@9438
   110
            break;
joseba@9438
   111
    }
joseba@9438
   112
}
joseba@9438
   113
joseba@9438
   114
#endif /* SDL_VIDEO_DRIVER_ANDROID */
joseba@9438
   115
joseba@9438
   116
/* vi: set ts=4 sw=4 expandtab: */
joseba@9438
   117