icculus@9278
|
1 |
/*
|
icculus@9278
|
2 |
Simple DirectMedia Layer
|
icculus@9278
|
3 |
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
|
icculus@9278
|
4 |
|
icculus@9278
|
5 |
This software is provided 'as-is', without any express or implied
|
icculus@9278
|
6 |
warranty. In no event will the authors be held liable for any damages
|
icculus@9278
|
7 |
arising from the use of this software.
|
icculus@9278
|
8 |
|
icculus@9278
|
9 |
Permission is granted to anyone to use this software for any purpose,
|
icculus@9278
|
10 |
including commercial applications, and to alter it and redistribute it
|
icculus@9278
|
11 |
freely, subject to the following restrictions:
|
icculus@9278
|
12 |
|
icculus@9278
|
13 |
1. The origin of this software must not be misrepresented; you must not
|
icculus@9278
|
14 |
claim that you wrote the original software. If you use this software
|
icculus@9278
|
15 |
in a product, an acknowledgment in the product documentation would be
|
icculus@9278
|
16 |
appreciated but is not required.
|
icculus@9278
|
17 |
2. Altered source versions must be plainly marked as such, and must not be
|
icculus@9278
|
18 |
misrepresented as being the original software.
|
icculus@9278
|
19 |
3. This notice may not be removed or altered from any source distribution.
|
icculus@9278
|
20 |
*/
|
icculus@9278
|
21 |
|
icculus@9278
|
22 |
|
icculus@9278
|
23 |
#include "../../SDL_internal.h"
|
icculus@9278
|
24 |
|
icculus@9278
|
25 |
#if SDL_VIDEO_DRIVER_EMSCRIPTEN
|
icculus@9278
|
26 |
|
icculus@9278
|
27 |
#include <emscripten/emscripten.h>
|
icculus@9278
|
28 |
#include <emscripten/html5.h>
|
icculus@9278
|
29 |
|
icculus@9278
|
30 |
#include "SDL_emscriptenmouse.h"
|
icculus@9278
|
31 |
|
icculus@9278
|
32 |
#include "../../events/SDL_mouse_c.h"
|
icculus@9278
|
33 |
#include "SDL_assert.h"
|
icculus@9278
|
34 |
|
icculus@9278
|
35 |
|
icculus@9278
|
36 |
static SDL_Cursor*
|
icculus@9278
|
37 |
Emscripten_CreateDefaultCursor()
|
icculus@9278
|
38 |
{
|
icculus@9278
|
39 |
SDL_Cursor* cursor;
|
icculus@9278
|
40 |
Emscripten_CursorData *curdata;
|
icculus@9278
|
41 |
|
icculus@9278
|
42 |
cursor = SDL_calloc(1, sizeof(SDL_Cursor));
|
icculus@9278
|
43 |
if (cursor) {
|
icculus@9278
|
44 |
curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
|
philipp@9298
|
45 |
if (!curdata) {
|
philipp@9298
|
46 |
SDL_OutOfMemory();
|
philipp@9298
|
47 |
SDL_free(cursor);
|
philipp@9298
|
48 |
return NULL;
|
philipp@9298
|
49 |
}
|
icculus@9278
|
50 |
|
icculus@9278
|
51 |
curdata->system_cursor = "default";
|
icculus@9278
|
52 |
cursor->driverdata = curdata;
|
icculus@9278
|
53 |
}
|
icculus@9278
|
54 |
else {
|
icculus@9278
|
55 |
SDL_OutOfMemory();
|
icculus@9278
|
56 |
}
|
icculus@9278
|
57 |
|
icculus@9278
|
58 |
return cursor;
|
icculus@9278
|
59 |
}
|
icculus@9278
|
60 |
|
icculus@9278
|
61 |
static SDL_Cursor*
|
icculus@9278
|
62 |
Emscripten_CreateCursor(SDL_Surface* sruface, int hot_x, int hot_y)
|
icculus@9278
|
63 |
{
|
icculus@9278
|
64 |
return Emscripten_CreateDefaultCursor();
|
icculus@9278
|
65 |
}
|
icculus@9278
|
66 |
|
icculus@9278
|
67 |
static SDL_Cursor*
|
icculus@9278
|
68 |
Emscripten_CreateSystemCursor(SDL_SystemCursor id)
|
icculus@9278
|
69 |
{
|
icculus@9278
|
70 |
SDL_Cursor *cursor;
|
icculus@9278
|
71 |
Emscripten_CursorData *curdata;
|
icculus@9278
|
72 |
const char *cursor_name = NULL;
|
icculus@9278
|
73 |
|
icculus@9278
|
74 |
switch(id) {
|
icculus@9278
|
75 |
case SDL_SYSTEM_CURSOR_ARROW:
|
icculus@9278
|
76 |
cursor_name = "default";
|
icculus@9278
|
77 |
break;
|
icculus@9278
|
78 |
case SDL_SYSTEM_CURSOR_IBEAM:
|
icculus@9278
|
79 |
cursor_name = "text";
|
icculus@9278
|
80 |
break;
|
icculus@9278
|
81 |
case SDL_SYSTEM_CURSOR_WAIT:
|
icculus@9278
|
82 |
cursor_name = "wait";
|
icculus@9278
|
83 |
break;
|
icculus@9278
|
84 |
case SDL_SYSTEM_CURSOR_CROSSHAIR:
|
icculus@9278
|
85 |
cursor_name = "crosshair";
|
icculus@9278
|
86 |
break;
|
icculus@9278
|
87 |
case SDL_SYSTEM_CURSOR_WAITARROW:
|
icculus@9278
|
88 |
cursor_name = "progress";
|
icculus@9278
|
89 |
break;
|
icculus@9278
|
90 |
case SDL_SYSTEM_CURSOR_SIZENWSE:
|
icculus@9278
|
91 |
cursor_name = "nwse-resize";
|
icculus@9278
|
92 |
break;
|
icculus@9278
|
93 |
case SDL_SYSTEM_CURSOR_SIZENESW:
|
icculus@9278
|
94 |
cursor_name = "nesw-resize";
|
icculus@9278
|
95 |
break;
|
icculus@9278
|
96 |
case SDL_SYSTEM_CURSOR_SIZEWE:
|
icculus@9278
|
97 |
cursor_name = "ew-resize";
|
icculus@9278
|
98 |
break;
|
icculus@9278
|
99 |
case SDL_SYSTEM_CURSOR_SIZENS:
|
icculus@9278
|
100 |
cursor_name = "ns-resize";
|
icculus@9278
|
101 |
break;
|
icculus@9278
|
102 |
case SDL_SYSTEM_CURSOR_SIZEALL:
|
icculus@9278
|
103 |
break;
|
icculus@9278
|
104 |
case SDL_SYSTEM_CURSOR_NO:
|
icculus@9278
|
105 |
cursor_name = "not-allowed";
|
icculus@9278
|
106 |
break;
|
icculus@9278
|
107 |
case SDL_SYSTEM_CURSOR_HAND:
|
icculus@9278
|
108 |
cursor_name = "pointer";
|
icculus@9278
|
109 |
break;
|
icculus@9278
|
110 |
default:
|
icculus@9278
|
111 |
SDL_assert(0);
|
icculus@9278
|
112 |
return NULL;
|
icculus@9278
|
113 |
}
|
icculus@9278
|
114 |
|
icculus@9278
|
115 |
cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
|
philipp@9298
|
116 |
if (!cursor) {
|
philipp@9298
|
117 |
SDL_OutOfMemory();
|
philipp@9298
|
118 |
return NULL;
|
philipp@9298
|
119 |
}
|
icculus@9278
|
120 |
curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
|
philipp@9298
|
121 |
if (!curdata) {
|
philipp@9298
|
122 |
SDL_OutOfMemory();
|
philipp@9298
|
123 |
SDL_free(cursor);
|
philipp@9298
|
124 |
return NULL;
|
philipp@9298
|
125 |
}
|
icculus@9278
|
126 |
|
icculus@9278
|
127 |
curdata->system_cursor = cursor_name;
|
icculus@9278
|
128 |
cursor->driverdata = curdata;
|
icculus@9278
|
129 |
|
icculus@9278
|
130 |
return cursor;
|
icculus@9278
|
131 |
}
|
icculus@9278
|
132 |
|
icculus@9278
|
133 |
static void
|
icculus@9278
|
134 |
Emscripten_FreeCursor(SDL_Cursor* cursor)
|
icculus@9278
|
135 |
{
|
icculus@9278
|
136 |
Emscripten_CursorData *curdata;
|
icculus@9278
|
137 |
if (cursor) {
|
icculus@9278
|
138 |
curdata = (Emscripten_CursorData *) cursor->driverdata;
|
icculus@9278
|
139 |
|
icculus@9278
|
140 |
if (curdata != NULL) {
|
icculus@9278
|
141 |
SDL_free(cursor->driverdata);
|
icculus@9278
|
142 |
}
|
icculus@9278
|
143 |
|
icculus@9278
|
144 |
SDL_free(cursor);
|
icculus@9278
|
145 |
}
|
icculus@9278
|
146 |
}
|
icculus@9278
|
147 |
|
icculus@9278
|
148 |
static int
|
icculus@9278
|
149 |
Emscripten_ShowCursor(SDL_Cursor* cursor)
|
icculus@9278
|
150 |
{
|
icculus@9278
|
151 |
Emscripten_CursorData *curdata;
|
icculus@9278
|
152 |
if (SDL_GetMouseFocus() != NULL) {
|
icculus@9278
|
153 |
if(cursor && cursor->driverdata) {
|
icculus@9278
|
154 |
curdata = (Emscripten_CursorData *) cursor->driverdata;
|
icculus@9278
|
155 |
|
icculus@9278
|
156 |
if(curdata->system_cursor) {
|
icculus@9278
|
157 |
EM_ASM_INT({
|
icculus@9278
|
158 |
if (Module['canvas']) {
|
icculus@9278
|
159 |
Module['canvas'].style['cursor'] = Module['Pointer_stringify']($0);
|
icculus@9278
|
160 |
}
|
icculus@9278
|
161 |
return 0;
|
icculus@9278
|
162 |
}, curdata->system_cursor);
|
icculus@9278
|
163 |
}
|
icculus@9278
|
164 |
}
|
icculus@9278
|
165 |
else {
|
icculus@9278
|
166 |
EM_ASM(
|
icculus@9278
|
167 |
if (Module['canvas']) {
|
icculus@9278
|
168 |
Module['canvas'].style['cursor'] = 'none';
|
icculus@9278
|
169 |
}
|
icculus@9278
|
170 |
);
|
icculus@9278
|
171 |
}
|
icculus@9278
|
172 |
}
|
icculus@9278
|
173 |
return 0;
|
icculus@9278
|
174 |
}
|
icculus@9278
|
175 |
|
icculus@9278
|
176 |
static void
|
icculus@9278
|
177 |
Emscripten_WarpMouse(SDL_Window* window, int x, int y)
|
icculus@9278
|
178 |
{
|
icculus@9278
|
179 |
SDL_Unsupported();
|
icculus@9278
|
180 |
}
|
icculus@9278
|
181 |
|
icculus@9278
|
182 |
static int
|
icculus@9278
|
183 |
Emscripten_SetRelativeMouseMode(SDL_bool enabled)
|
icculus@9278
|
184 |
{
|
icculus@9278
|
185 |
/* TODO: pointer lock isn't actually enabled yet */
|
icculus@9278
|
186 |
if(enabled) {
|
icculus@9278
|
187 |
if(emscripten_request_pointerlock(NULL, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
|
icculus@9278
|
188 |
return 0;
|
icculus@9278
|
189 |
}
|
icculus@9278
|
190 |
} else {
|
icculus@9278
|
191 |
if(emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
|
icculus@9278
|
192 |
return 0;
|
icculus@9278
|
193 |
}
|
icculus@9278
|
194 |
}
|
icculus@9278
|
195 |
return -1;
|
icculus@9278
|
196 |
}
|
icculus@9278
|
197 |
|
icculus@9278
|
198 |
void
|
icculus@9278
|
199 |
Emscripten_InitMouse()
|
icculus@9278
|
200 |
{
|
icculus@9278
|
201 |
SDL_Mouse* mouse = SDL_GetMouse();
|
icculus@9278
|
202 |
|
icculus@9278
|
203 |
mouse->CreateCursor = Emscripten_CreateCursor;
|
icculus@9278
|
204 |
mouse->ShowCursor = Emscripten_ShowCursor;
|
icculus@9278
|
205 |
mouse->FreeCursor = Emscripten_FreeCursor;
|
icculus@9278
|
206 |
mouse->WarpMouse = Emscripten_WarpMouse;
|
icculus@9278
|
207 |
mouse->CreateSystemCursor = Emscripten_CreateSystemCursor;
|
icculus@9278
|
208 |
mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;
|
icculus@9278
|
209 |
|
icculus@9278
|
210 |
SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
|
icculus@9278
|
211 |
}
|
icculus@9278
|
212 |
|
icculus@9278
|
213 |
void
|
icculus@9278
|
214 |
Emscripten_FiniMouse()
|
icculus@9278
|
215 |
{
|
icculus@9278
|
216 |
SDL_Mouse* mouse = SDL_GetMouse();
|
icculus@9278
|
217 |
|
icculus@9278
|
218 |
Emscripten_FreeCursor(mouse->def_cursor);
|
icculus@9278
|
219 |
mouse->def_cursor = NULL;
|
icculus@9278
|
220 |
|
icculus@9278
|
221 |
mouse->CreateCursor = NULL;
|
icculus@9278
|
222 |
mouse->ShowCursor = NULL;
|
icculus@9278
|
223 |
mouse->FreeCursor = NULL;
|
icculus@9278
|
224 |
mouse->WarpMouse = NULL;
|
icculus@9278
|
225 |
mouse->CreateSystemCursor = NULL;
|
icculus@9278
|
226 |
mouse->SetRelativeMouseMode = NULL;
|
icculus@9278
|
227 |
}
|
icculus@9278
|
228 |
|
icculus@9278
|
229 |
#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
|
icculus@9278
|
230 |
|
icculus@9278
|
231 |
/* vi: set ts=4 sw=4 expandtab: */
|
icculus@9278
|
232 |
|