SDL  2.0
SDL_emscriptenmouse.c
Go to the documentation of this file.
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 {
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) {
47  SDL_free(cursor);
48  return NULL;
49  }
50 
51  curdata->system_cursor = "default";
52  cursor->driverdata = curdata;
53  }
54  else {
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 {
73  Emscripten_CursorData *curdata;
74  const char *cursor_name = NULL;
75 
76  switch(id) {
78  cursor_name = "default";
79  break;
81  cursor_name = "text";
82  break;
84  cursor_name = "wait";
85  break;
87  cursor_name = "crosshair";
88  break;
90  cursor_name = "progress";
91  break;
93  cursor_name = "nwse-resize";
94  break;
96  cursor_name = "nesw-resize";
97  break;
99  cursor_name = "ew-resize";
100  break;
102  cursor_name = "ns-resize";
103  break;
105  break;
107  cursor_name = "not-allowed";
108  break;
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
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
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 
SDL_Mouse * SDL_GetMouse(void)
Definition: SDL_mouse.c:66
int(* ShowCursor)(SDL_Cursor *cursor)
Definition: SDL_mouse_c.h:52
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
int(* SetRelativeMouseMode)(SDL_bool enabled)
Definition: SDL_mouse_c.h:67
void Emscripten_FiniMouse()
static SDL_Window * window
SDL_Cursor *(* CreateCursor)(SDL_Surface *surface, int hot_x, int hot_y)
Definition: SDL_mouse_c.h:46
void * SDL_calloc(size_t nmemb, size_t size)
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
void SDL_free(void *mem)
SDL_SystemCursor
Cursor types for SDL_CreateSystemCursor().
Definition: SDL_mouse.h:46
void SDL_SetDefaultCursor(SDL_Cursor *cursor)
Definition: SDL_mouse.c:55
GLenum GLenum GLsizei const GLuint GLboolean enabled
SDL_Cursor * cursor
void(* FreeCursor)(SDL_Cursor *cursor)
Definition: SDL_mouse_c.h:58
#define SDL_assert(condition)
Definition: SDL_assert.h:167
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_bool
Definition: SDL_stdinc.h:130
#define SDL_GetMouseFocus
The type used to identify a window.
Definition: SDL_sysvideo.h:71
void(* WarpMouse)(SDL_Window *window, int x, int y)
Definition: SDL_mouse_c.h:61
SDL_Cursor *(* CreateSystemCursor)(SDL_SystemCursor id)
Definition: SDL_mouse_c.h:49
void * driverdata
Definition: SDL_mouse_c.h:33
#define SDL_Unsupported()
Definition: SDL_error.h:53
void Emscripten_InitMouse()
SDL_Cursor * def_cursor
Definition: SDL_mouse_c.h:92