SDL_emscriptenmouse.c
Go to the documentation of this file.
/*
Simple DirectMedia Layer
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_EMSCRIPTEN
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include "SDL_emscriptenmouse.h"
#include "../../events/SDL_mouse_c.h"
#include "SDL_assert.h"
static SDL_Cursor*
Emscripten_CreateDefaultCursor()
{
SDL_Cursor* cursor;
Emscripten_CursorData *curdata;
cursor = SDL_calloc(1, sizeof(SDL_Cursor));
if (cursor) {
curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
if (!curdata) {
SDL_OutOfMemory();
SDL_free(cursor);
return NULL;
}
curdata->system_cursor = "default";
cursor->driverdata = curdata;
}
else {
SDL_OutOfMemory();
}
return cursor;
}
static SDL_Cursor*
Emscripten_CreateCursor(SDL_Surface* sruface, int hot_x, int hot_y)
{
return Emscripten_CreateDefaultCursor();
}
static SDL_Cursor*
Emscripten_CreateSystemCursor(SDL_SystemCursor id)
{
SDL_Cursor *cursor;
Emscripten_CursorData *curdata;
const char *cursor_name = NULL;
switch(id) {
case SDL_SYSTEM_CURSOR_ARROW:
cursor_name = "default";
break;
case SDL_SYSTEM_CURSOR_IBEAM:
cursor_name = "text";
break;
case SDL_SYSTEM_CURSOR_WAIT:
cursor_name = "wait";
break;
case SDL_SYSTEM_CURSOR_CROSSHAIR:
cursor_name = "crosshair";
break;
case SDL_SYSTEM_CURSOR_WAITARROW:
cursor_name = "progress";
break;
case SDL_SYSTEM_CURSOR_SIZENWSE:
cursor_name = "nwse-resize";
break;
case SDL_SYSTEM_CURSOR_SIZENESW:
cursor_name = "nesw-resize";
break;
case SDL_SYSTEM_CURSOR_SIZEWE:
cursor_name = "ew-resize";
break;
case SDL_SYSTEM_CURSOR_SIZENS:
cursor_name = "ns-resize";
break;
case SDL_SYSTEM_CURSOR_SIZEALL:
break;
case SDL_SYSTEM_CURSOR_NO:
cursor_name = "not-allowed";
break;
case SDL_SYSTEM_CURSOR_HAND:
cursor_name = "pointer";
break;
default:
SDL_assert(0);
return NULL;
}
cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
if (!cursor) {
SDL_OutOfMemory();
return NULL;
}
curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
if (!curdata) {
SDL_OutOfMemory();
SDL_free(cursor);
return NULL;
}
curdata->system_cursor = cursor_name;
cursor->driverdata = curdata;
return cursor;
}
static void
Emscripten_FreeCursor(SDL_Cursor* cursor)
{
Emscripten_CursorData *curdata;
if (cursor) {
curdata = (Emscripten_CursorData *) cursor->driverdata;
if (curdata != NULL) {
SDL_free(cursor->driverdata);
}
SDL_free(cursor);
}
}
static int
Emscripten_ShowCursor(SDL_Cursor* cursor)
{
Emscripten_CursorData *curdata;
if (SDL_GetMouseFocus() != NULL) {
if(cursor && cursor->driverdata) {
curdata = (Emscripten_CursorData *) cursor->driverdata;
if(curdata->system_cursor) {
EM_ASM_INT({
if (Module['canvas']) {
Module['canvas'].style['cursor'] = Module['Pointer_stringify']($0);
}
return 0;
}, curdata->system_cursor);
}
}
else {
EM_ASM(
if (Module['canvas']) {
Module['canvas'].style['cursor'] = 'none';
}
);
}
}
return 0;
}
static void
Emscripten_WarpMouse(SDL_Window* window, int x, int y)
{
SDL_Unsupported();
}
static int
Emscripten_SetRelativeMouseMode(SDL_bool enabled)
{
/* TODO: pointer lock isn't actually enabled yet */
if(enabled) {
if(emscripten_request_pointerlock(NULL, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
return 0;
}
} else {
if(emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
return 0;
}
}
return -1;
}
void
Emscripten_InitMouse()
{
SDL_Mouse* mouse = SDL_GetMouse();
mouse->CreateCursor = Emscripten_CreateCursor;
mouse->ShowCursor = Emscripten_ShowCursor;
mouse->FreeCursor = Emscripten_FreeCursor;
mouse->WarpMouse = Emscripten_WarpMouse;
mouse->CreateSystemCursor = Emscripten_CreateSystemCursor;
mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;
SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
}
void
Emscripten_FiniMouse()
{
SDL_Mouse* mouse = SDL_GetMouse();
Emscripten_FreeCursor(mouse->def_cursor);
mouse->def_cursor = NULL;
mouse->CreateCursor = NULL;
mouse->ShowCursor = NULL;
mouse->FreeCursor = NULL;
mouse->WarpMouse = NULL;
mouse->CreateSystemCursor = NULL;
mouse->SetRelativeMouseMode = NULL;
}
#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
/* vi: set ts=4 sw=4 expandtab: */
SDL_GetMouse
SDL_Mouse * SDL_GetMouse(void)
Definition: SDL_mouse.c:66
SDL_Mouse::ShowCursor
int(* ShowCursor)(SDL_Cursor *cursor)
Definition: SDL_mouse_c.h:52
SDL_SYSTEM_CURSOR_ARROW
Definition: SDL_mouse.h:48
SDL_SYSTEM_CURSOR_HAND
Definition: SDL_mouse.h:59
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
SDL_Mouse::SetRelativeMouseMode
int(* SetRelativeMouseMode)(SDL_bool enabled)
Definition: SDL_mouse_c.h:67
window
SDL_Window * window
Definition: testautomation_render.c:26
Emscripten_FiniMouse
void Emscripten_FiniMouse()
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
SDL_emscriptenmouse.h
SDL_Cursor
Definition: SDL_mouse_c.h:30
SDL_SYSTEM_CURSOR_SIZEALL
Definition: SDL_mouse.h:57
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:126
SDL_SYSTEM_CURSOR_SIZEWE
Definition: SDL_mouse.h:55
SDL_Mouse::CreateCursor
SDL_Cursor *(* CreateCursor)(SDL_Surface *surface, int hot_x, int hot_y)
Definition: SDL_mouse_c.h:46
SDL_calloc
void * SDL_calloc(size_t nmemb, size_t size)
Emscripten_CursorData
Definition: SDL_emscriptenmouse.h:26
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
SDL_SYSTEM_CURSOR_SIZENWSE
Definition: SDL_mouse.h:53
SDL_free
void SDL_free(void *mem)
SDL_SystemCursor
SDL_SystemCursor
Cursor types for SDL_CreateSystemCursor.
Definition: SDL_mouse.h:46
cursor
SDL_Cursor * cursor
Definition: testwm2.c:40
SDL_SetDefaultCursor
void SDL_SetDefaultCursor(SDL_Cursor *cursor)
Definition: SDL_mouse.c:55
enabled
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: SDL_opengl_glext.h:2479
SDL_Mouse::FreeCursor
void(* FreeCursor)(SDL_Cursor *cursor)
Definition: SDL_mouse_c.h:58
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:167
SDL_SYSTEM_CURSOR_CROSSHAIR
Definition: SDL_mouse.h:51
NULL
#define NULL
Definition: begin_code.h:143
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_GetMouseFocus
#define SDL_GetMouseFocus
Definition: SDL_dynapi_overrides.h:251
SDL_SYSTEM_CURSOR_WAIT
Definition: SDL_mouse.h:50
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:71
SDL_SYSTEM_CURSOR_IBEAM
Definition: SDL_mouse.h:49
SDL_Mouse::WarpMouse
void(* WarpMouse)(SDL_Window *window, int x, int y)
Definition: SDL_mouse_c.h:61
SDL_SYSTEM_CURSOR_SIZENESW
Definition: SDL_mouse.h:54
SDL_Mouse
Definition: SDL_mouse_c.h:43
SDL_SYSTEM_CURSOR_NO
Definition: SDL_mouse.h:58
Emscripten_CursorData::system_cursor
const char * system_cursor
Definition: SDL_emscriptenmouse.h:28
SDL_SYSTEM_CURSOR_SIZENS
Definition: SDL_mouse.h:56
SDL_SYSTEM_CURSOR_WAITARROW
Definition: SDL_mouse.h:52
SDL_Mouse::CreateSystemCursor
SDL_Cursor *(* CreateSystemCursor)(SDL_SystemCursor id)
Definition: SDL_mouse_c.h:49
SDL_Cursor::driverdata
void * driverdata
Definition: SDL_mouse_c.h:33
SDL_Unsupported
#define SDL_Unsupported()
Definition: SDL_error.h:53
Emscripten_InitMouse
void Emscripten_InitMouse()
SDL_assert.h
SDL_Mouse::def_cursor
SDL_Cursor * def_cursor
Definition: SDL_mouse_c.h:92