2 Simple DirectMedia Layer
3 Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
23 #include "../../SDL_internal.h"
25 #if SDL_VIDEO_DRIVER_EMSCRIPTEN
27 #include <emscripten/emscripten.h>
28 #include <emscripten/html5.h>
30 #include "SDL_emscriptenmouse.h"
32 #include "../../events/SDL_mouse_c.h"
33 #include "SDL_assert.h"
36 Emscripten_CreateCursorFromString(const char* cursor_str)
39 Emscripten_CursorData *curdata;
41 cursor = SDL_calloc(1, sizeof(SDL_Cursor));
43 curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
50 curdata->system_cursor = cursor_str;
51 cursor->driverdata = curdata;
61 Emscripten_CreateDefaultCursor()
63 return Emscripten_CreateCursorFromString("default");
68 Emscripten_CreateCursor(SDL_Surface* sruface, int hot_x, int hot_y)
70 return Emscripten_CreateDefaultCursor();
75 Emscripten_CreateSystemCursor(SDL_SystemCursor id)
77 const char *cursor_name = NULL;
80 case SDL_SYSTEM_CURSOR_ARROW:
81 cursor_name = "default";
83 case SDL_SYSTEM_CURSOR_IBEAM:
86 case SDL_SYSTEM_CURSOR_WAIT:
89 case SDL_SYSTEM_CURSOR_CROSSHAIR:
90 cursor_name = "crosshair";
92 case SDL_SYSTEM_CURSOR_WAITARROW:
93 cursor_name = "progress";
95 case SDL_SYSTEM_CURSOR_SIZENWSE:
96 cursor_name = "nwse-resize";
98 case SDL_SYSTEM_CURSOR_SIZENESW:
99 cursor_name = "nesw-resize";
101 case SDL_SYSTEM_CURSOR_SIZEWE:
102 cursor_name = "ew-resize";
104 case SDL_SYSTEM_CURSOR_SIZENS:
105 cursor_name = "ns-resize";
107 case SDL_SYSTEM_CURSOR_SIZEALL:
109 case SDL_SYSTEM_CURSOR_NO:
110 cursor_name = "not-allowed";
112 case SDL_SYSTEM_CURSOR_HAND:
113 cursor_name = "pointer";
120 return Emscripten_CreateCursorFromString(cursor_name);
124 Emscripten_FreeCursor(SDL_Cursor* cursor)
126 Emscripten_CursorData *curdata;
128 curdata = (Emscripten_CursorData *) cursor->driverdata;
130 if (curdata != NULL) {
131 SDL_free(cursor->driverdata);
139 Emscripten_ShowCursor(SDL_Cursor* cursor)
141 Emscripten_CursorData *curdata;
142 if (SDL_GetMouseFocus() != NULL) {
143 if(cursor && cursor->driverdata) {
144 curdata = (Emscripten_CursorData *) cursor->driverdata;
146 if(curdata->system_cursor) {
148 if (Module['canvas']) {
149 Module['canvas'].style['cursor'] = Module['Pointer_stringify']($0);
152 }, curdata->system_cursor);
157 if (Module['canvas']) {
158 Module['canvas'].style['cursor'] = 'none';
167 Emscripten_WarpMouse(SDL_Window* window, int x, int y)
173 Emscripten_SetRelativeMouseMode(SDL_bool enabled)
175 /* TODO: pointer lock isn't actually enabled yet */
177 if(emscripten_request_pointerlock(NULL, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
181 if(emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
189 Emscripten_InitMouse()
191 SDL_Mouse* mouse = SDL_GetMouse();
194 mouse->CreateCursor = Emscripten_CreateCursor;
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;
202 SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
206 Emscripten_FiniMouse()
208 SDL_Mouse* mouse = SDL_GetMouse();
210 Emscripten_FreeCursor(mouse->def_cursor);
211 mouse->def_cursor = NULL;
213 mouse->CreateCursor = NULL;
214 mouse->ShowCursor = NULL;
215 mouse->FreeCursor = NULL;
216 mouse->WarpMouse = NULL;
217 mouse->CreateSystemCursor = NULL;
218 mouse->SetRelativeMouseMode = NULL;
221 #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
223 /* vi: set ts=4 sw=4 expandtab: */