src/video/emscripten/SDL_emscriptenmouse.c
author Charlie Birks
Fri, 17 Feb 2017 10:13:12 +0000
changeset 10897 19203215ca8e
parent 10737 3406a0f8b041
child 10898 5c01b36afb14
permissions -rw-r--r--
Emscripten: refactor cursor handling
     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 
    23 #include "../../SDL_internal.h"
    24 
    25 #if SDL_VIDEO_DRIVER_EMSCRIPTEN
    26 
    27 #include <emscripten/emscripten.h>
    28 #include <emscripten/html5.h>
    29 
    30 #include "SDL_emscriptenmouse.h"
    31 
    32 #include "../../events/SDL_mouse_c.h"
    33 #include "SDL_assert.h"
    34 
    35 static SDL_Cursor*
    36 Emscripten_CreateCursorFromString(const char* cursor_str)
    37 {
    38     SDL_Cursor* cursor;
    39     Emscripten_CursorData *curdata;
    40 
    41     cursor = SDL_calloc(1, sizeof(SDL_Cursor));
    42     if (cursor) {
    43         curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
    44         if (!curdata) {
    45             SDL_OutOfMemory();
    46             SDL_free(cursor);
    47             return NULL;
    48         }
    49 
    50         curdata->system_cursor = cursor_str;
    51         cursor->driverdata = curdata;
    52     }
    53     else {
    54         SDL_OutOfMemory();
    55     }
    56 
    57     return cursor;
    58 }
    59 
    60 static SDL_Cursor*
    61 Emscripten_CreateDefaultCursor()
    62 {
    63     return Emscripten_CreateCursorFromString("default");
    64 }
    65 
    66 /*
    67 static SDL_Cursor*
    68 Emscripten_CreateCursor(SDL_Surface* sruface, int hot_x, int hot_y)
    69 {
    70     return Emscripten_CreateDefaultCursor();
    71 }
    72 */
    73 
    74 static SDL_Cursor*
    75 Emscripten_CreateSystemCursor(SDL_SystemCursor id)
    76 {
    77     const char *cursor_name = NULL;
    78 
    79     switch(id) {
    80         case SDL_SYSTEM_CURSOR_ARROW:
    81             cursor_name = "default";
    82             break;
    83         case SDL_SYSTEM_CURSOR_IBEAM:
    84             cursor_name = "text";
    85             break;
    86         case SDL_SYSTEM_CURSOR_WAIT:
    87             cursor_name = "wait";
    88             break;
    89         case SDL_SYSTEM_CURSOR_CROSSHAIR:
    90             cursor_name = "crosshair";
    91             break;
    92         case SDL_SYSTEM_CURSOR_WAITARROW:
    93             cursor_name = "progress";
    94             break;
    95         case SDL_SYSTEM_CURSOR_SIZENWSE:
    96             cursor_name = "nwse-resize";
    97             break;
    98         case SDL_SYSTEM_CURSOR_SIZENESW:
    99             cursor_name = "nesw-resize";
   100             break;
   101         case SDL_SYSTEM_CURSOR_SIZEWE:
   102             cursor_name = "ew-resize";
   103             break;
   104         case SDL_SYSTEM_CURSOR_SIZENS:
   105             cursor_name = "ns-resize";
   106             break;
   107         case SDL_SYSTEM_CURSOR_SIZEALL:
   108             break;
   109         case SDL_SYSTEM_CURSOR_NO:
   110             cursor_name = "not-allowed";
   111             break;
   112         case SDL_SYSTEM_CURSOR_HAND:
   113             cursor_name = "pointer";
   114             break;
   115         default:
   116             SDL_assert(0);
   117             return NULL;
   118     }
   119 
   120     return Emscripten_CreateCursorFromString(cursor_name);
   121 }
   122 
   123 static void
   124 Emscripten_FreeCursor(SDL_Cursor* cursor)
   125 {
   126     Emscripten_CursorData *curdata;
   127     if (cursor) {
   128         curdata = (Emscripten_CursorData *) cursor->driverdata;
   129 
   130         if (curdata != NULL) {
   131             SDL_free(cursor->driverdata);
   132         }
   133 
   134         SDL_free(cursor);
   135     }
   136 }
   137 
   138 static int
   139 Emscripten_ShowCursor(SDL_Cursor* cursor)
   140 {
   141     Emscripten_CursorData *curdata;
   142     if (SDL_GetMouseFocus() != NULL) {
   143         if(cursor && cursor->driverdata) {
   144             curdata = (Emscripten_CursorData *) cursor->driverdata;
   145 
   146             if(curdata->system_cursor) {
   147                 EM_ASM_INT({
   148                     if (Module['canvas']) {
   149                         Module['canvas'].style['cursor'] = Module['Pointer_stringify']($0);
   150                     }
   151                     return 0;
   152                 }, curdata->system_cursor);
   153             }
   154         }
   155         else {
   156             EM_ASM(
   157                 if (Module['canvas']) {
   158                     Module['canvas'].style['cursor'] = 'none';
   159                 }
   160             );
   161         }
   162     }
   163     return 0;
   164 }
   165 
   166 static void
   167 Emscripten_WarpMouse(SDL_Window* window, int x, int y)
   168 {
   169     SDL_Unsupported();
   170 }
   171 
   172 static int
   173 Emscripten_SetRelativeMouseMode(SDL_bool enabled)
   174 {
   175     /* TODO: pointer lock isn't actually enabled yet */
   176     if(enabled) {
   177         if(emscripten_request_pointerlock(NULL, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
   178             return 0;
   179         }
   180     } else {
   181         if(emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
   182             return 0;
   183         }
   184     }
   185     return -1;
   186 }
   187 
   188 void
   189 Emscripten_InitMouse()
   190 {
   191     SDL_Mouse* mouse = SDL_GetMouse();
   192 
   193 /*
   194     mouse->CreateCursor         = Emscripten_CreateCursor;
   195 */ 
   196     mouse->ShowCursor           = Emscripten_ShowCursor;
   197     mouse->FreeCursor           = Emscripten_FreeCursor;
   198     mouse->WarpMouse            = Emscripten_WarpMouse;
   199     mouse->CreateSystemCursor   = Emscripten_CreateSystemCursor;
   200     mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;
   201 
   202     SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
   203 }
   204 
   205 void
   206 Emscripten_FiniMouse()
   207 {
   208     SDL_Mouse* mouse = SDL_GetMouse();
   209 
   210     Emscripten_FreeCursor(mouse->def_cursor);
   211     mouse->def_cursor = NULL;
   212 
   213     mouse->CreateCursor         = NULL;
   214     mouse->ShowCursor           = NULL;
   215     mouse->FreeCursor           = NULL;
   216     mouse->WarpMouse            = NULL;
   217     mouse->CreateSystemCursor   = NULL;
   218     mouse->SetRelativeMouseMode = NULL;
   219 }
   220 
   221 #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
   222 
   223 /* vi: set ts=4 sw=4 expandtab: */
   224