src/video/emscripten/SDL_emscriptenmouse.c
author Ryan C. Gordon
Mon, 23 Jan 2017 12:06:10 -0500
changeset 10837 c2f241c2f6ad
parent 10737 3406a0f8b041
child 10897 19203215ca8e
permissions -rw-r--r--
audio: Fix same bug as last commit, but for _mm_bslli_si128 vs _mm_slli_si128.
     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 
    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 /*
    62 static SDL_Cursor*
    63 Emscripten_CreateCursor(SDL_Surface* sruface, int hot_x, int hot_y)
    64 {
    65     return Emscripten_CreateDefaultCursor();
    66 }
    67 */
    68 
    69 static SDL_Cursor*
    70 Emscripten_CreateSystemCursor(SDL_SystemCursor id)
    71 {
    72     SDL_Cursor *cursor;
    73     Emscripten_CursorData *curdata;
    74     const char *cursor_name = NULL;
    75 
    76     switch(id) {
    77         case SDL_SYSTEM_CURSOR_ARROW:
    78             cursor_name = "default";
    79             break;
    80         case SDL_SYSTEM_CURSOR_IBEAM:
    81             cursor_name = "text";
    82             break;
    83         case SDL_SYSTEM_CURSOR_WAIT:
    84             cursor_name = "wait";
    85             break;
    86         case SDL_SYSTEM_CURSOR_CROSSHAIR:
    87             cursor_name = "crosshair";
    88             break;
    89         case SDL_SYSTEM_CURSOR_WAITARROW:
    90             cursor_name = "progress";
    91             break;
    92         case SDL_SYSTEM_CURSOR_SIZENWSE:
    93             cursor_name = "nwse-resize";
    94             break;
    95         case SDL_SYSTEM_CURSOR_SIZENESW:
    96             cursor_name = "nesw-resize";
    97             break;
    98         case SDL_SYSTEM_CURSOR_SIZEWE:
    99             cursor_name = "ew-resize";
   100             break;
   101         case SDL_SYSTEM_CURSOR_SIZENS:
   102             cursor_name = "ns-resize";
   103             break;
   104         case SDL_SYSTEM_CURSOR_SIZEALL:
   105             break;
   106         case SDL_SYSTEM_CURSOR_NO:
   107             cursor_name = "not-allowed";
   108             break;
   109         case SDL_SYSTEM_CURSOR_HAND:
   110             cursor_name = "pointer";
   111             break;
   112         default:
   113             SDL_assert(0);
   114             return NULL;
   115     }
   116 
   117     cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
   118     if (!cursor) {
   119         SDL_OutOfMemory();
   120         return NULL;
   121     }
   122     curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
   123     if (!curdata) {
   124         SDL_OutOfMemory();
   125         SDL_free(cursor);
   126         return NULL;
   127     }
   128 
   129     curdata->system_cursor = cursor_name;
   130     cursor->driverdata = curdata;
   131 
   132     return cursor;
   133 }
   134 
   135 static void
   136 Emscripten_FreeCursor(SDL_Cursor* cursor)
   137 {
   138     Emscripten_CursorData *curdata;
   139     if (cursor) {
   140         curdata = (Emscripten_CursorData *) cursor->driverdata;
   141 
   142         if (curdata != NULL) {
   143             SDL_free(cursor->driverdata);
   144         }
   145 
   146         SDL_free(cursor);
   147     }
   148 }
   149 
   150 static int
   151 Emscripten_ShowCursor(SDL_Cursor* cursor)
   152 {
   153     Emscripten_CursorData *curdata;
   154     if (SDL_GetMouseFocus() != NULL) {
   155         if(cursor && cursor->driverdata) {
   156             curdata = (Emscripten_CursorData *) cursor->driverdata;
   157 
   158             if(curdata->system_cursor) {
   159                 EM_ASM_INT({
   160                     if (Module['canvas']) {
   161                         Module['canvas'].style['cursor'] = Module['Pointer_stringify']($0);
   162                     }
   163                     return 0;
   164                 }, curdata->system_cursor);
   165             }
   166         }
   167         else {
   168             EM_ASM(
   169                 if (Module['canvas']) {
   170                     Module['canvas'].style['cursor'] = 'none';
   171                 }
   172             );
   173         }
   174     }
   175     return 0;
   176 }
   177 
   178 static void
   179 Emscripten_WarpMouse(SDL_Window* window, int x, int y)
   180 {
   181     SDL_Unsupported();
   182 }
   183 
   184 static int
   185 Emscripten_SetRelativeMouseMode(SDL_bool enabled)
   186 {
   187     /* TODO: pointer lock isn't actually enabled yet */
   188     if(enabled) {
   189         if(emscripten_request_pointerlock(NULL, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
   190             return 0;
   191         }
   192     } else {
   193         if(emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
   194             return 0;
   195         }
   196     }
   197     return -1;
   198 }
   199 
   200 void
   201 Emscripten_InitMouse()
   202 {
   203     SDL_Mouse* mouse = SDL_GetMouse();
   204 
   205 /*
   206     mouse->CreateCursor         = Emscripten_CreateCursor;
   207 */ 
   208     mouse->ShowCursor           = Emscripten_ShowCursor;
   209     mouse->FreeCursor           = Emscripten_FreeCursor;
   210     mouse->WarpMouse            = Emscripten_WarpMouse;
   211     mouse->CreateSystemCursor   = Emscripten_CreateSystemCursor;
   212     mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;
   213 
   214     SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
   215 }
   216 
   217 void
   218 Emscripten_FiniMouse()
   219 {
   220     SDL_Mouse* mouse = SDL_GetMouse();
   221 
   222     Emscripten_FreeCursor(mouse->def_cursor);
   223     mouse->def_cursor = NULL;
   224 
   225     mouse->CreateCursor         = NULL;
   226     mouse->ShowCursor           = NULL;
   227     mouse->FreeCursor           = NULL;
   228     mouse->WarpMouse            = NULL;
   229     mouse->CreateSystemCursor   = NULL;
   230     mouse->SetRelativeMouseMode = NULL;
   231 }
   232 
   233 #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
   234 
   235 /* vi: set ts=4 sw=4 expandtab: */
   236