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