1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/video/emscripten/SDL_emscriptenmouse.c Thu Dec 18 00:19:52 2014 -0500
1.3 @@ -0,0 +1,218 @@
1.4 +/*
1.5 + Simple DirectMedia Layer
1.6 + Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
1.7 +
1.8 + This software is provided 'as-is', without any express or implied
1.9 + warranty. In no event will the authors be held liable for any damages
1.10 + arising from the use of this software.
1.11 +
1.12 + Permission is granted to anyone to use this software for any purpose,
1.13 + including commercial applications, and to alter it and redistribute it
1.14 + freely, subject to the following restrictions:
1.15 +
1.16 + 1. The origin of this software must not be misrepresented; you must not
1.17 + claim that you wrote the original software. If you use this software
1.18 + in a product, an acknowledgment in the product documentation would be
1.19 + appreciated but is not required.
1.20 + 2. Altered source versions must be plainly marked as such, and must not be
1.21 + misrepresented as being the original software.
1.22 + 3. This notice may not be removed or altered from any source distribution.
1.23 +*/
1.24 +
1.25 +
1.26 +#include "../../SDL_internal.h"
1.27 +
1.28 +#if SDL_VIDEO_DRIVER_EMSCRIPTEN
1.29 +
1.30 +#include <emscripten/emscripten.h>
1.31 +#include <emscripten/html5.h>
1.32 +
1.33 +#include "SDL_emscriptenmouse.h"
1.34 +
1.35 +#include "../../events/SDL_mouse_c.h"
1.36 +#include "SDL_assert.h"
1.37 +
1.38 +
1.39 +static SDL_Cursor*
1.40 +Emscripten_CreateDefaultCursor()
1.41 +{
1.42 + SDL_Cursor* cursor;
1.43 + Emscripten_CursorData *curdata;
1.44 +
1.45 + cursor = SDL_calloc(1, sizeof(SDL_Cursor));
1.46 + if (cursor) {
1.47 + curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
1.48 +
1.49 + curdata->system_cursor = "default";
1.50 + cursor->driverdata = curdata;
1.51 + }
1.52 + else {
1.53 + SDL_OutOfMemory();
1.54 + }
1.55 +
1.56 + return cursor;
1.57 +}
1.58 +
1.59 +static SDL_Cursor*
1.60 +Emscripten_CreateCursor(SDL_Surface* sruface, int hot_x, int hot_y)
1.61 +{
1.62 + return Emscripten_CreateDefaultCursor();
1.63 +}
1.64 +
1.65 +static SDL_Cursor*
1.66 +Emscripten_CreateSystemCursor(SDL_SystemCursor id)
1.67 +{
1.68 + SDL_Cursor *cursor;
1.69 + Emscripten_CursorData *curdata;
1.70 + const char *cursor_name = NULL;
1.71 +
1.72 + switch(id) {
1.73 + case SDL_SYSTEM_CURSOR_ARROW:
1.74 + cursor_name = "default";
1.75 + break;
1.76 + case SDL_SYSTEM_CURSOR_IBEAM:
1.77 + cursor_name = "text";
1.78 + break;
1.79 + case SDL_SYSTEM_CURSOR_WAIT:
1.80 + cursor_name = "wait";
1.81 + break;
1.82 + case SDL_SYSTEM_CURSOR_CROSSHAIR:
1.83 + cursor_name = "crosshair";
1.84 + break;
1.85 + case SDL_SYSTEM_CURSOR_WAITARROW:
1.86 + cursor_name = "progress";
1.87 + break;
1.88 + case SDL_SYSTEM_CURSOR_SIZENWSE:
1.89 + cursor_name = "nwse-resize";
1.90 + break;
1.91 + case SDL_SYSTEM_CURSOR_SIZENESW:
1.92 + cursor_name = "nesw-resize";
1.93 + break;
1.94 + case SDL_SYSTEM_CURSOR_SIZEWE:
1.95 + cursor_name = "ew-resize";
1.96 + break;
1.97 + case SDL_SYSTEM_CURSOR_SIZENS:
1.98 + cursor_name = "ns-resize";
1.99 + break;
1.100 + case SDL_SYSTEM_CURSOR_SIZEALL:
1.101 + break;
1.102 + case SDL_SYSTEM_CURSOR_NO:
1.103 + cursor_name = "not-allowed";
1.104 + break;
1.105 + case SDL_SYSTEM_CURSOR_HAND:
1.106 + cursor_name = "pointer";
1.107 + break;
1.108 + default:
1.109 + SDL_assert(0);
1.110 + return NULL;
1.111 + }
1.112 +
1.113 + cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
1.114 + curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
1.115 +
1.116 + curdata->system_cursor = cursor_name;
1.117 + cursor->driverdata = curdata;
1.118 +
1.119 + return cursor;
1.120 +}
1.121 +
1.122 +static void
1.123 +Emscripten_FreeCursor(SDL_Cursor* cursor)
1.124 +{
1.125 + Emscripten_CursorData *curdata;
1.126 + if (cursor) {
1.127 + curdata = (Emscripten_CursorData *) cursor->driverdata;
1.128 +
1.129 + if (curdata != NULL) {
1.130 + SDL_free(cursor->driverdata);
1.131 + }
1.132 +
1.133 + SDL_free(cursor);
1.134 + }
1.135 +}
1.136 +
1.137 +static int
1.138 +Emscripten_ShowCursor(SDL_Cursor* cursor)
1.139 +{
1.140 + Emscripten_CursorData *curdata;
1.141 + if (SDL_GetMouseFocus() != NULL) {
1.142 + if(cursor && cursor->driverdata) {
1.143 + curdata = (Emscripten_CursorData *) cursor->driverdata;
1.144 +
1.145 + if(curdata->system_cursor) {
1.146 + EM_ASM_INT({
1.147 + if (Module['canvas']) {
1.148 + Module['canvas'].style['cursor'] = Module['Pointer_stringify']($0);
1.149 + }
1.150 + return 0;
1.151 + }, curdata->system_cursor);
1.152 + }
1.153 + }
1.154 + else {
1.155 + EM_ASM(
1.156 + if (Module['canvas']) {
1.157 + Module['canvas'].style['cursor'] = 'none';
1.158 + }
1.159 + );
1.160 + }
1.161 + }
1.162 + return 0;
1.163 +}
1.164 +
1.165 +static void
1.166 +Emscripten_WarpMouse(SDL_Window* window, int x, int y)
1.167 +{
1.168 + SDL_Unsupported();
1.169 +}
1.170 +
1.171 +static int
1.172 +Emscripten_SetRelativeMouseMode(SDL_bool enabled)
1.173 +{
1.174 + /* TODO: pointer lock isn't actually enabled yet */
1.175 + if(enabled) {
1.176 + if(emscripten_request_pointerlock(NULL, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
1.177 + return 0;
1.178 + }
1.179 + } else {
1.180 + if(emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
1.181 + return 0;
1.182 + }
1.183 + }
1.184 + return -1;
1.185 +}
1.186 +
1.187 +void
1.188 +Emscripten_InitMouse()
1.189 +{
1.190 + SDL_Mouse* mouse = SDL_GetMouse();
1.191 +
1.192 + mouse->CreateCursor = Emscripten_CreateCursor;
1.193 + mouse->ShowCursor = Emscripten_ShowCursor;
1.194 + mouse->FreeCursor = Emscripten_FreeCursor;
1.195 + mouse->WarpMouse = Emscripten_WarpMouse;
1.196 + mouse->CreateSystemCursor = Emscripten_CreateSystemCursor;
1.197 + mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;
1.198 +
1.199 + SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
1.200 +}
1.201 +
1.202 +void
1.203 +Emscripten_FiniMouse()
1.204 +{
1.205 + SDL_Mouse* mouse = SDL_GetMouse();
1.206 +
1.207 + Emscripten_FreeCursor(mouse->def_cursor);
1.208 + mouse->def_cursor = NULL;
1.209 +
1.210 + mouse->CreateCursor = NULL;
1.211 + mouse->ShowCursor = NULL;
1.212 + mouse->FreeCursor = NULL;
1.213 + mouse->WarpMouse = NULL;
1.214 + mouse->CreateSystemCursor = NULL;
1.215 + mouse->SetRelativeMouseMode = NULL;
1.216 +}
1.217 +
1.218 +#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
1.219 +
1.220 +/* vi: set ts=4 sw=4 expandtab: */
1.221 +