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