src/video/android/SDL_androidmouse.c
changeset 11931 d479188e0155
parent 11811 5d94cb6b24d3
child 11932 a05a909eeddb
     1.1 --- a/src/video/android/SDL_androidmouse.c	Mon Mar 12 18:41:06 2018 -0700
     1.2 +++ b/src/video/android/SDL_androidmouse.c	Thu Mar 15 18:22:48 2018 -0700
     1.3 @@ -42,12 +42,107 @@
     1.4  #define BUTTON_BACK 8
     1.5  #define BUTTON_FORWARD 16
     1.6  
     1.7 +typedef struct
     1.8 +{
     1.9 +    int custom_cursor;
    1.10 +    int system_cursor;
    1.11 +
    1.12 +} SDL_AndroidCursorData;
    1.13 +
    1.14  /* Last known Android mouse button state (includes all buttons) */
    1.15  static int last_state;
    1.16  
    1.17 +
    1.18 +static SDL_Cursor *
    1.19 +Android_WrapCursor(int custom_cursor, int system_cursor)
    1.20 +{
    1.21 +    SDL_Cursor *cursor;
    1.22 +
    1.23 +    cursor = SDL_calloc(1, sizeof(*cursor));
    1.24 +    if (cursor) {
    1.25 +        SDL_AndroidCursorData *data = (SDL_AndroidCursorData*)SDL_calloc(1, sizeof(*data));
    1.26 +        if (data) {
    1.27 +            data->custom_cursor = custom_cursor;
    1.28 +            data->system_cursor = system_cursor;
    1.29 +            cursor->driverdata = data;
    1.30 +        } else {
    1.31 +            SDL_free(cursor);
    1.32 +            cursor = NULL;
    1.33 +            SDL_OutOfMemory();
    1.34 +        }
    1.35 +    } else {
    1.36 +        SDL_OutOfMemory();
    1.37 +    }
    1.38 +
    1.39 +    return cursor;
    1.40 +}
    1.41 +
    1.42 +static SDL_Cursor *
    1.43 +Android_CreateDefaultCursor()
    1.44 +{
    1.45 +    return Android_WrapCursor(0, SDL_SYSTEM_CURSOR_ARROW);
    1.46 +}
    1.47 +
    1.48 +static SDL_Cursor *
    1.49 +Android_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
    1.50 +{
    1.51 +    int custom_cursor;
    1.52 +    SDL_Surface *converted;
    1.53 +
    1.54 +    converted = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0);
    1.55 +    if (!converted) {
    1.56 +        return NULL;
    1.57 +    }
    1.58 +    custom_cursor = Android_JNI_CreateCustomCursor(converted, hot_x, hot_y);
    1.59 +    SDL_FreeSurface(converted);
    1.60 +    if (!custom_cursor) {
    1.61 +        SDL_Unsupported();
    1.62 +        return NULL;
    1.63 +    }
    1.64 +    return Android_WrapCursor(custom_cursor, 0);
    1.65 +}
    1.66 +
    1.67 +static SDL_Cursor *
    1.68 +Android_CreateSystemCursor(SDL_SystemCursor id)
    1.69 +{
    1.70 +    return Android_WrapCursor(0, id);
    1.71 +}
    1.72 +
    1.73 +static void
    1.74 +Android_FreeCursor(SDL_Cursor * cursor)
    1.75 +{
    1.76 +    SDL_free(cursor->driverdata);
    1.77 +    SDL_free(cursor);
    1.78 +}
    1.79 +
    1.80 +static int
    1.81 +Android_ShowCursor(SDL_Cursor * cursor)
    1.82 +{
    1.83 +    if (cursor) {
    1.84 +        SDL_AndroidCursorData *data = (SDL_AndroidCursorData*)cursor->driverdata;
    1.85 +        if (data->custom_cursor) {
    1.86 +            Android_JNI_SetCustomCursor(data->custom_cursor);
    1.87 +        } else {
    1.88 +            Android_JNI_SetSystemCursor(data->system_cursor);
    1.89 +        }
    1.90 +    } else {
    1.91 +        Android_JNI_SetSystemCursor(-1);
    1.92 +    }
    1.93 +    return 0;
    1.94 +}
    1.95 +
    1.96  void
    1.97  Android_InitMouse(void)
    1.98  {
    1.99 +    SDL_Mouse *mouse = SDL_GetMouse();
   1.100 +
   1.101 +    mouse->CreateCursor = Android_CreateCursor;
   1.102 +    mouse->CreateSystemCursor = Android_CreateSystemCursor;
   1.103 +    mouse->ShowCursor = Android_ShowCursor;
   1.104 +    mouse->FreeCursor = Android_FreeCursor;
   1.105 +
   1.106 +    SDL_SetDefaultCursor(Android_CreateDefaultCursor());
   1.107 +
   1.108      last_state = 0;
   1.109  }
   1.110