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 static SDL_Cursor*
62 Emscripten_CreateCursor(SDL_Surface* sruface, int hot_x, int hot_y)
63 {
64  return Emscripten_CreateDefaultCursor();
65 }
66 
67 static SDL_Cursor*
68 Emscripten_CreateSystemCursor(SDL_SystemCursor id)
69 {
71  Emscripten_CursorData *curdata;
72  const char *cursor_name = NULL;
73 
74  switch(id) {
76  cursor_name = "default";
77  break;
79  cursor_name = "text";
80  break;
82  cursor_name = "wait";
83  break;
85  cursor_name = "crosshair";
86  break;
88  cursor_name = "progress";
89  break;
91  cursor_name = "nwse-resize";
92  break;
94  cursor_name = "nesw-resize";
95  break;
97  cursor_name = "ew-resize";
98  break;
100  cursor_name = "ns-resize";
101  break;
103  break;
105  cursor_name = "not-allowed";
106  break;
108  cursor_name = "pointer";
109  break;
110  default:
111  SDL_assert(0);
112  return NULL;
113  }
114 
115  cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
116  if (!cursor) {
117  SDL_OutOfMemory();
118  return NULL;
119  }
120  curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
121  if (!curdata) {
122  SDL_OutOfMemory();
123  SDL_free(cursor);
124  return NULL;
125  }
126 
127  curdata->system_cursor = cursor_name;
128  cursor->driverdata = curdata;
129 
130  return cursor;
131 }
132 
133 static void
134 Emscripten_FreeCursor(SDL_Cursor* cursor)
135 {
136  Emscripten_CursorData *curdata;
137  if (cursor) {
138  curdata = (Emscripten_CursorData *) cursor->driverdata;
139 
140  if (curdata != NULL) {
141  SDL_free(cursor->driverdata);
142  }
143 
144  SDL_free(cursor);
145  }
146 }
147 
148 static int
149 Emscripten_ShowCursor(SDL_Cursor* cursor)
150 {
151  Emscripten_CursorData *curdata;
152  if (SDL_GetMouseFocus() != NULL) {
153  if(cursor && cursor->driverdata) {
154  curdata = (Emscripten_CursorData *) cursor->driverdata;
155 
156  if(curdata->system_cursor) {
157  EM_ASM_INT({
158  if (Module['canvas']) {
159  Module['canvas'].style['cursor'] = Module['Pointer_stringify']($0);
160  }
161  return 0;
162  }, curdata->system_cursor);
163  }
164  }
165  else {
166  EM_ASM(
167  if (Module['canvas']) {
168  Module['canvas'].style['cursor'] = 'none';
169  }
170  );
171  }
172  }
173  return 0;
174 }
175 
176 static void
177 Emscripten_WarpMouse(SDL_Window* window, int x, int y)
178 {
179  SDL_Unsupported();
180 }
181 
182 static int
183 Emscripten_SetRelativeMouseMode(SDL_bool enabled)
184 {
185  /* TODO: pointer lock isn't actually enabled yet */
186  if(enabled) {
187  if(emscripten_request_pointerlock(NULL, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
188  return 0;
189  }
190  } else {
191  if(emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
192  return 0;
193  }
194  }
195  return -1;
196 }
197 
198 void
200 {
201  SDL_Mouse* mouse = SDL_GetMouse();
202 
203  mouse->CreateCursor = Emscripten_CreateCursor;
204  mouse->ShowCursor = Emscripten_ShowCursor;
205  mouse->FreeCursor = Emscripten_FreeCursor;
206  mouse->WarpMouse = Emscripten_WarpMouse;
207  mouse->CreateSystemCursor = Emscripten_CreateSystemCursor;
208  mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;
209 
210  SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
211 }
212 
213 void
215 {
216  SDL_Mouse* mouse = SDL_GetMouse();
217 
218  Emscripten_FreeCursor(mouse->def_cursor);
219  mouse->def_cursor = NULL;
220 
221  mouse->CreateCursor = NULL;
222  mouse->ShowCursor = NULL;
223  mouse->FreeCursor = NULL;
224  mouse->WarpMouse = NULL;
225  mouse->CreateSystemCursor = NULL;
226  mouse->SetRelativeMouseMode = NULL;
227 }
228 
229 #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
230 
231 /* vi: set ts=4 sw=4 expandtab: */
232 
SDL_Mouse * SDL_GetMouse(void)
Definition: SDL_mouse.c:66
int(* ShowCursor)(SDL_Cursor *cursor)
Definition: SDL_mouse_c.h:52
int(* SetRelativeMouseMode)(SDL_bool enabled)
Definition: SDL_mouse_c.h:67
SDL_Window * window
void Emscripten_FiniMouse()
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
SDL_bool
Definition: SDL_stdinc.h:126
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)
void SDL_free(void *mem)
SDL_SystemCursor
Cursor types for SDL_CreateSystemCursor.
Definition: SDL_mouse.h:46
SDL_Cursor * cursor
Definition: testwm2.c:40
void SDL_SetDefaultCursor(SDL_Cursor *cursor)
Definition: SDL_mouse.c:55
GLenum GLenum GLsizei const GLuint GLboolean enabled
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
#define SDL_GetMouseFocus
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
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