src/video/emscripten/SDL_emscriptenevents.c
changeset 9278 8900afb78a19
child 9352 2cbf791a601c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/emscripten/SDL_emscriptenevents.c	Thu Dec 18 00:19:52 2014 -0500
     1.3 @@ -0,0 +1,638 @@
     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/html5.h>
    1.31 +
    1.32 +#include "../../events/SDL_events_c.h"
    1.33 +#include "../../events/SDL_keyboard_c.h"
    1.34 +#include "../../events/SDL_touch_c.h"
    1.35 +
    1.36 +#include "SDL_emscriptenevents.h"
    1.37 +#include "SDL_emscriptenvideo.h"
    1.38 +
    1.39 +#include "SDL_hints.h"
    1.40 +
    1.41 +#define FULLSCREEN_MASK ( SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN )
    1.42 +
    1.43 +/*
    1.44 +.which to scancode
    1.45 +https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Constants
    1.46 +*/
    1.47 +static const SDL_Scancode emscripten_scancode_table[] = {
    1.48 +    /*  0 */    SDL_SCANCODE_UNKNOWN,
    1.49 +    /*  1 */    SDL_SCANCODE_UNKNOWN,
    1.50 +    /*  2 */    SDL_SCANCODE_UNKNOWN,
    1.51 +    /*  3 */    SDL_SCANCODE_CANCEL,
    1.52 +    /*  4 */    SDL_SCANCODE_UNKNOWN,
    1.53 +    /*  5 */    SDL_SCANCODE_UNKNOWN,
    1.54 +    /*  6 */    SDL_SCANCODE_HELP,
    1.55 +    /*  7 */    SDL_SCANCODE_UNKNOWN,
    1.56 +    /*  8 */    SDL_SCANCODE_BACKSPACE,
    1.57 +    /*  9 */    SDL_SCANCODE_TAB,
    1.58 +    /*  10 */   SDL_SCANCODE_UNKNOWN,
    1.59 +    /*  11 */   SDL_SCANCODE_UNKNOWN,
    1.60 +    /*  12 */   SDL_SCANCODE_UNKNOWN,
    1.61 +    /*  13 */   SDL_SCANCODE_RETURN,
    1.62 +    /*  14 */   SDL_SCANCODE_UNKNOWN,
    1.63 +    /*  15 */   SDL_SCANCODE_UNKNOWN,
    1.64 +    /*  16 */   SDL_SCANCODE_LSHIFT,
    1.65 +    /*  17 */   SDL_SCANCODE_LCTRL,
    1.66 +    /*  18 */   SDL_SCANCODE_LALT,
    1.67 +    /*  19 */   SDL_SCANCODE_PAUSE,
    1.68 +    /*  20 */   SDL_SCANCODE_CAPSLOCK,
    1.69 +    /*  21 */   SDL_SCANCODE_UNKNOWN,
    1.70 +    /*  22 */   SDL_SCANCODE_UNKNOWN,
    1.71 +    /*  23 */   SDL_SCANCODE_UNKNOWN,
    1.72 +    /*  24 */   SDL_SCANCODE_UNKNOWN,
    1.73 +    /*  25 */   SDL_SCANCODE_UNKNOWN,
    1.74 +    /*  26 */   SDL_SCANCODE_UNKNOWN,
    1.75 +    /*  27 */   SDL_SCANCODE_ESCAPE,
    1.76 +    /*  28 */   SDL_SCANCODE_UNKNOWN,
    1.77 +    /*  29 */   SDL_SCANCODE_UNKNOWN,
    1.78 +    /*  30 */   SDL_SCANCODE_UNKNOWN,
    1.79 +    /*  31 */   SDL_SCANCODE_UNKNOWN,
    1.80 +    /*  32 */   SDL_SCANCODE_SPACE,
    1.81 +    /*  33 */   SDL_SCANCODE_PAGEUP,
    1.82 +    /*  34 */   SDL_SCANCODE_PAGEDOWN,
    1.83 +    /*  35 */   SDL_SCANCODE_END,
    1.84 +    /*  36 */   SDL_SCANCODE_HOME,
    1.85 +    /*  37 */   SDL_SCANCODE_LEFT,
    1.86 +    /*  38 */   SDL_SCANCODE_UP,
    1.87 +    /*  39 */   SDL_SCANCODE_RIGHT,
    1.88 +    /*  40 */   SDL_SCANCODE_DOWN,
    1.89 +    /*  41 */   SDL_SCANCODE_UNKNOWN,
    1.90 +    /*  42 */   SDL_SCANCODE_UNKNOWN,
    1.91 +    /*  43 */   SDL_SCANCODE_UNKNOWN,
    1.92 +    /*  44 */   SDL_SCANCODE_UNKNOWN,
    1.93 +    /*  45 */   SDL_SCANCODE_INSERT,
    1.94 +    /*  46 */   SDL_SCANCODE_DELETE,
    1.95 +    /*  47 */   SDL_SCANCODE_UNKNOWN,
    1.96 +    /*  48 */   SDL_SCANCODE_0,
    1.97 +    /*  49 */   SDL_SCANCODE_1,
    1.98 +    /*  50 */   SDL_SCANCODE_2,
    1.99 +    /*  51 */   SDL_SCANCODE_3,
   1.100 +    /*  52 */   SDL_SCANCODE_4,
   1.101 +    /*  53 */   SDL_SCANCODE_5,
   1.102 +    /*  54 */   SDL_SCANCODE_6,
   1.103 +    /*  55 */   SDL_SCANCODE_7,
   1.104 +    /*  56 */   SDL_SCANCODE_8,
   1.105 +    /*  57 */   SDL_SCANCODE_9,
   1.106 +    /*  58 */   SDL_SCANCODE_UNKNOWN,
   1.107 +    /*  59 */   SDL_SCANCODE_SEMICOLON,
   1.108 +    /*  60 */   SDL_SCANCODE_UNKNOWN,
   1.109 +    /*  61 */   SDL_SCANCODE_EQUALS,
   1.110 +    /*  62 */   SDL_SCANCODE_UNKNOWN,
   1.111 +    /*  63 */   SDL_SCANCODE_UNKNOWN,
   1.112 +    /*  64 */   SDL_SCANCODE_UNKNOWN,
   1.113 +    /*  65 */   SDL_SCANCODE_A,
   1.114 +    /*  66 */   SDL_SCANCODE_B,
   1.115 +    /*  67 */   SDL_SCANCODE_C,
   1.116 +    /*  68 */   SDL_SCANCODE_D,
   1.117 +    /*  69 */   SDL_SCANCODE_E,
   1.118 +    /*  70 */   SDL_SCANCODE_F,
   1.119 +    /*  71 */   SDL_SCANCODE_G,
   1.120 +    /*  72 */   SDL_SCANCODE_H,
   1.121 +    /*  73 */   SDL_SCANCODE_I,
   1.122 +    /*  74 */   SDL_SCANCODE_J,
   1.123 +    /*  75 */   SDL_SCANCODE_K,
   1.124 +    /*  76 */   SDL_SCANCODE_L,
   1.125 +    /*  77 */   SDL_SCANCODE_M,
   1.126 +    /*  78 */   SDL_SCANCODE_N,
   1.127 +    /*  79 */   SDL_SCANCODE_O,
   1.128 +    /*  80 */   SDL_SCANCODE_P,
   1.129 +    /*  81 */   SDL_SCANCODE_Q,
   1.130 +    /*  82 */   SDL_SCANCODE_R,
   1.131 +    /*  83 */   SDL_SCANCODE_S,
   1.132 +    /*  84 */   SDL_SCANCODE_T,
   1.133 +    /*  85 */   SDL_SCANCODE_U,
   1.134 +    /*  86 */   SDL_SCANCODE_V,
   1.135 +    /*  87 */   SDL_SCANCODE_W,
   1.136 +    /*  88 */   SDL_SCANCODE_X,
   1.137 +    /*  89 */   SDL_SCANCODE_Y,
   1.138 +    /*  90 */   SDL_SCANCODE_Z,
   1.139 +    /*  91 */   SDL_SCANCODE_LGUI,
   1.140 +    /*  92 */   SDL_SCANCODE_UNKNOWN,
   1.141 +    /*  93 */   SDL_SCANCODE_APPLICATION,
   1.142 +    /*  94 */   SDL_SCANCODE_UNKNOWN,
   1.143 +    /*  95 */   SDL_SCANCODE_UNKNOWN,
   1.144 +    /*  96 */   SDL_SCANCODE_KP_0,
   1.145 +    /*  97 */   SDL_SCANCODE_KP_1,
   1.146 +    /*  98 */   SDL_SCANCODE_KP_2,
   1.147 +    /*  99 */   SDL_SCANCODE_KP_3,
   1.148 +    /* 100 */   SDL_SCANCODE_KP_4,
   1.149 +    /* 101 */   SDL_SCANCODE_KP_5,
   1.150 +    /* 102 */   SDL_SCANCODE_KP_6,
   1.151 +    /* 103 */   SDL_SCANCODE_KP_7,
   1.152 +    /* 104 */   SDL_SCANCODE_KP_8,
   1.153 +    /* 105 */   SDL_SCANCODE_KP_9,
   1.154 +    /* 106 */   SDL_SCANCODE_KP_MULTIPLY,
   1.155 +    /* 107 */   SDL_SCANCODE_KP_PLUS,
   1.156 +    /* 108 */   SDL_SCANCODE_UNKNOWN,
   1.157 +    /* 109 */   SDL_SCANCODE_KP_MINUS,
   1.158 +    /* 110 */   SDL_SCANCODE_KP_PERIOD,
   1.159 +    /* 111 */   SDL_SCANCODE_KP_DIVIDE,
   1.160 +    /* 112 */   SDL_SCANCODE_F1,
   1.161 +    /* 113 */   SDL_SCANCODE_F2,
   1.162 +    /* 114 */   SDL_SCANCODE_F3,
   1.163 +    /* 115 */   SDL_SCANCODE_F4,
   1.164 +    /* 116 */   SDL_SCANCODE_F5,
   1.165 +    /* 117 */   SDL_SCANCODE_F6,
   1.166 +    /* 118 */   SDL_SCANCODE_F7,
   1.167 +    /* 119 */   SDL_SCANCODE_F8,
   1.168 +    /* 120 */   SDL_SCANCODE_F9,
   1.169 +    /* 121 */   SDL_SCANCODE_F10,
   1.170 +    /* 122 */   SDL_SCANCODE_F11,
   1.171 +    /* 123 */   SDL_SCANCODE_F12,
   1.172 +    /* 124 */   SDL_SCANCODE_F13,
   1.173 +    /* 125 */   SDL_SCANCODE_F14,
   1.174 +    /* 126 */   SDL_SCANCODE_F15,
   1.175 +    /* 127 */   SDL_SCANCODE_F16,
   1.176 +    /* 128 */   SDL_SCANCODE_F17,
   1.177 +    /* 129 */   SDL_SCANCODE_F18,
   1.178 +    /* 130 */   SDL_SCANCODE_F19,
   1.179 +    /* 131 */   SDL_SCANCODE_F20,
   1.180 +    /* 132 */   SDL_SCANCODE_F21,
   1.181 +    /* 133 */   SDL_SCANCODE_F22,
   1.182 +    /* 134 */   SDL_SCANCODE_F23,
   1.183 +    /* 135 */   SDL_SCANCODE_F24,
   1.184 +    /* 136 */   SDL_SCANCODE_UNKNOWN,
   1.185 +    /* 137 */   SDL_SCANCODE_UNKNOWN,
   1.186 +    /* 138 */   SDL_SCANCODE_UNKNOWN,
   1.187 +    /* 139 */   SDL_SCANCODE_UNKNOWN,
   1.188 +    /* 140 */   SDL_SCANCODE_UNKNOWN,
   1.189 +    /* 141 */   SDL_SCANCODE_UNKNOWN,
   1.190 +    /* 142 */   SDL_SCANCODE_UNKNOWN,
   1.191 +    /* 143 */   SDL_SCANCODE_UNKNOWN,
   1.192 +    /* 144 */   SDL_SCANCODE_NUMLOCKCLEAR,
   1.193 +    /* 145 */   SDL_SCANCODE_SCROLLLOCK,
   1.194 +    /* 146 */   SDL_SCANCODE_UNKNOWN,
   1.195 +    /* 147 */   SDL_SCANCODE_UNKNOWN,
   1.196 +    /* 148 */   SDL_SCANCODE_UNKNOWN,
   1.197 +    /* 149 */   SDL_SCANCODE_UNKNOWN,
   1.198 +    /* 150 */   SDL_SCANCODE_UNKNOWN,
   1.199 +    /* 151 */   SDL_SCANCODE_UNKNOWN,
   1.200 +    /* 152 */   SDL_SCANCODE_UNKNOWN,
   1.201 +    /* 153 */   SDL_SCANCODE_UNKNOWN,
   1.202 +    /* 154 */   SDL_SCANCODE_UNKNOWN,
   1.203 +    /* 155 */   SDL_SCANCODE_UNKNOWN,
   1.204 +    /* 156 */   SDL_SCANCODE_UNKNOWN,
   1.205 +    /* 157 */   SDL_SCANCODE_UNKNOWN,
   1.206 +    /* 158 */   SDL_SCANCODE_UNKNOWN,
   1.207 +    /* 159 */   SDL_SCANCODE_UNKNOWN,
   1.208 +    /* 160 */   SDL_SCANCODE_UNKNOWN,
   1.209 +    /* 161 */   SDL_SCANCODE_UNKNOWN,
   1.210 +    /* 162 */   SDL_SCANCODE_UNKNOWN,
   1.211 +    /* 163 */   SDL_SCANCODE_UNKNOWN,
   1.212 +    /* 164 */   SDL_SCANCODE_UNKNOWN,
   1.213 +    /* 165 */   SDL_SCANCODE_UNKNOWN,
   1.214 +    /* 166 */   SDL_SCANCODE_UNKNOWN,
   1.215 +    /* 167 */   SDL_SCANCODE_UNKNOWN,
   1.216 +    /* 168 */   SDL_SCANCODE_UNKNOWN,
   1.217 +    /* 169 */   SDL_SCANCODE_UNKNOWN,
   1.218 +    /* 170 */   SDL_SCANCODE_UNKNOWN,
   1.219 +    /* 171 */   SDL_SCANCODE_UNKNOWN,
   1.220 +    /* 172 */   SDL_SCANCODE_UNKNOWN,
   1.221 +    /* 173 */   SDL_SCANCODE_MINUS, /*FX*/
   1.222 +    /* 174 */   SDL_SCANCODE_UNKNOWN,
   1.223 +    /* 175 */   SDL_SCANCODE_UNKNOWN,
   1.224 +    /* 176 */   SDL_SCANCODE_UNKNOWN,
   1.225 +    /* 177 */   SDL_SCANCODE_UNKNOWN,
   1.226 +    /* 178 */   SDL_SCANCODE_UNKNOWN,
   1.227 +    /* 179 */   SDL_SCANCODE_UNKNOWN,
   1.228 +    /* 180 */   SDL_SCANCODE_UNKNOWN,
   1.229 +    /* 181 */   SDL_SCANCODE_UNKNOWN,
   1.230 +    /* 182 */   SDL_SCANCODE_UNKNOWN,
   1.231 +    /* 183 */   SDL_SCANCODE_UNKNOWN,
   1.232 +    /* 184 */   SDL_SCANCODE_UNKNOWN,
   1.233 +    /* 185 */   SDL_SCANCODE_UNKNOWN,
   1.234 +    /* 186 */   SDL_SCANCODE_SEMICOLON, /*IE, Chrome, D3E legacy*/
   1.235 +    /* 187 */   SDL_SCANCODE_EQUALS, /*IE, Chrome, D3E legacy*/
   1.236 +    /* 188 */   SDL_SCANCODE_COMMA,
   1.237 +    /* 189 */   SDL_SCANCODE_MINUS, /*IE, Chrome, D3E legacy*/
   1.238 +    /* 190 */   SDL_SCANCODE_PERIOD,
   1.239 +    /* 191 */   SDL_SCANCODE_SLASH,
   1.240 +    /* 192 */   SDL_SCANCODE_GRAVE, /*FX, D3E legacy (SDL_SCANCODE_APOSTROPHE in IE/Chrome)*/
   1.241 +    /* 193 */   SDL_SCANCODE_UNKNOWN,
   1.242 +    /* 194 */   SDL_SCANCODE_UNKNOWN,
   1.243 +    /* 195 */   SDL_SCANCODE_UNKNOWN,
   1.244 +    /* 196 */   SDL_SCANCODE_UNKNOWN,
   1.245 +    /* 197 */   SDL_SCANCODE_UNKNOWN,
   1.246 +    /* 198 */   SDL_SCANCODE_UNKNOWN,
   1.247 +    /* 199 */   SDL_SCANCODE_UNKNOWN,
   1.248 +    /* 200 */   SDL_SCANCODE_UNKNOWN,
   1.249 +    /* 201 */   SDL_SCANCODE_UNKNOWN,
   1.250 +    /* 202 */   SDL_SCANCODE_UNKNOWN,
   1.251 +    /* 203 */   SDL_SCANCODE_UNKNOWN,
   1.252 +    /* 204 */   SDL_SCANCODE_UNKNOWN,
   1.253 +    /* 205 */   SDL_SCANCODE_UNKNOWN,
   1.254 +    /* 206 */   SDL_SCANCODE_UNKNOWN,
   1.255 +    /* 207 */   SDL_SCANCODE_UNKNOWN,
   1.256 +    /* 208 */   SDL_SCANCODE_UNKNOWN,
   1.257 +    /* 209 */   SDL_SCANCODE_UNKNOWN,
   1.258 +    /* 210 */   SDL_SCANCODE_UNKNOWN,
   1.259 +    /* 211 */   SDL_SCANCODE_UNKNOWN,
   1.260 +    /* 212 */   SDL_SCANCODE_UNKNOWN,
   1.261 +    /* 213 */   SDL_SCANCODE_UNKNOWN,
   1.262 +    /* 214 */   SDL_SCANCODE_UNKNOWN,
   1.263 +    /* 215 */   SDL_SCANCODE_UNKNOWN,
   1.264 +    /* 216 */   SDL_SCANCODE_UNKNOWN,
   1.265 +    /* 217 */   SDL_SCANCODE_UNKNOWN,
   1.266 +    /* 218 */   SDL_SCANCODE_UNKNOWN,
   1.267 +    /* 219 */   SDL_SCANCODE_LEFTBRACKET,
   1.268 +    /* 220 */   SDL_SCANCODE_BACKSLASH,
   1.269 +    /* 221 */   SDL_SCANCODE_RIGHTBRACKET,
   1.270 +    /* 222 */   SDL_SCANCODE_APOSTROPHE, /*FX, D3E legacy*/
   1.271 +};
   1.272 +
   1.273 +
   1.274 +/* "borrowed" from SDL_windowsevents.c */
   1.275 +int
   1.276 +Emscripten_ConvertUTF32toUTF8(Uint32 codepoint, char * text)
   1.277 +{
   1.278 +    if (codepoint <= 0x7F) {
   1.279 +        text[0] = (char) codepoint;
   1.280 +        text[1] = '\0';
   1.281 +    } else if (codepoint <= 0x7FF) {
   1.282 +        text[0] = 0xC0 | (char) ((codepoint >> 6) & 0x1F);
   1.283 +        text[1] = 0x80 | (char) (codepoint & 0x3F);
   1.284 +        text[2] = '\0';
   1.285 +    } else if (codepoint <= 0xFFFF) {
   1.286 +        text[0] = 0xE0 | (char) ((codepoint >> 12) & 0x0F);
   1.287 +        text[1] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
   1.288 +        text[2] = 0x80 | (char) (codepoint & 0x3F);
   1.289 +        text[3] = '\0';
   1.290 +    } else if (codepoint <= 0x10FFFF) {
   1.291 +        text[0] = 0xF0 | (char) ((codepoint >> 18) & 0x0F);
   1.292 +        text[1] = 0x80 | (char) ((codepoint >> 12) & 0x3F);
   1.293 +        text[2] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
   1.294 +        text[3] = 0x80 | (char) (codepoint & 0x3F);
   1.295 +        text[4] = '\0';
   1.296 +    } else {
   1.297 +        return SDL_FALSE;
   1.298 +    }
   1.299 +    return SDL_TRUE;
   1.300 +}
   1.301 +
   1.302 +int
   1.303 +Emscripten_HandleMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
   1.304 +{
   1.305 +    SDL_WindowData *window_data = userData;
   1.306 +    int mx = mouseEvent->canvasX, my = mouseEvent->canvasY;
   1.307 +    EmscriptenPointerlockChangeEvent pointerlock_status;
   1.308 +
   1.309 +    /* check for pointer lock */
   1.310 +    emscripten_get_pointerlock_status(&pointerlock_status);
   1.311 +
   1.312 +    if (pointerlock_status.isActive) {
   1.313 +        mx = mouseEvent->movementX;
   1.314 +        my = mouseEvent->movementY;
   1.315 +    }
   1.316 +
   1.317 +    /* rescale (in case canvas is being scaled)*/
   1.318 +    double client_w, client_h;
   1.319 +    emscripten_get_element_css_size(NULL, &client_w, &client_h);
   1.320 +
   1.321 +    mx = mx * (window_data->window->w / (client_w * window_data->pixel_ratio));
   1.322 +    my = my * (window_data->window->h / (client_h * window_data->pixel_ratio));
   1.323 +
   1.324 +    SDL_SendMouseMotion(window_data->window, 0, pointerlock_status.isActive, mx, my);
   1.325 +    return 0;
   1.326 +}
   1.327 +
   1.328 +int
   1.329 +Emscripten_HandleMouseButton(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
   1.330 +{
   1.331 +    SDL_WindowData *window_data = userData;
   1.332 +    uint32_t sdl_button;
   1.333 +    switch (mouseEvent->button) {
   1.334 +        case 0:
   1.335 +            sdl_button = SDL_BUTTON_LEFT;
   1.336 +            break;
   1.337 +        case 1:
   1.338 +            sdl_button = SDL_BUTTON_MIDDLE;
   1.339 +            break;
   1.340 +        case 2:
   1.341 +            sdl_button = SDL_BUTTON_RIGHT;
   1.342 +            break;
   1.343 +        default:
   1.344 +            return 0;
   1.345 +    }
   1.346 +    SDL_SendMouseButton(window_data->window, 0, eventType == EMSCRIPTEN_EVENT_MOUSEDOWN ? SDL_PRESSED : SDL_RELEASED, sdl_button);
   1.347 +    return 1;
   1.348 +}
   1.349 +
   1.350 +int
   1.351 +Emscripten_HandleMouseFocus(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
   1.352 +{
   1.353 +    SDL_WindowData *window_data = userData;
   1.354 +    SDL_SendWindowEvent(window_data->window, eventType == EMSCRIPTEN_EVENT_MOUSEENTER ? SDL_WINDOWEVENT_ENTER : SDL_WINDOWEVENT_LEAVE, 0, 0);
   1.355 +    return 1;
   1.356 +}
   1.357 +
   1.358 +int
   1.359 +Emscripten_HandleWheel(int eventType, const EmscriptenWheelEvent *wheelEvent, void *userData)
   1.360 +{
   1.361 +    SDL_WindowData *window_data = userData;
   1.362 +    SDL_SendMouseWheel(window_data->window, 0, wheelEvent->deltaX, -wheelEvent->deltaY, SDL_MOUSEWHEEL_NORMAL);
   1.363 +    return 1;
   1.364 +}
   1.365 +
   1.366 +int
   1.367 +Emscripten_HandleFocus(int eventType, const EmscriptenFocusEvent *wheelEvent, void *userData)
   1.368 +{
   1.369 +    SDL_WindowData *window_data = userData;
   1.370 +    SDL_SendWindowEvent(window_data->window, eventType == EMSCRIPTEN_EVENT_FOCUS ? SDL_WINDOWEVENT_FOCUS_GAINED : SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
   1.371 +    return 1;
   1.372 +}
   1.373 +
   1.374 +int
   1.375 +Emscripten_HandleTouch(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
   1.376 +{
   1.377 +    /*SDL_WindowData *window_data = userData;*/
   1.378 +    int i;
   1.379 +
   1.380 +    SDL_TouchID deviceId = 0;
   1.381 +    if (!SDL_GetTouch(deviceId)) {
   1.382 +        if (SDL_AddTouch(deviceId, "") < 0) {
   1.383 +             return 0;
   1.384 +        }
   1.385 +    }
   1.386 +
   1.387 +    for (i = 0; i < touchEvent->numTouches; i++) {
   1.388 +        long x, y, id;
   1.389 +
   1.390 +        if (!touchEvent->touches[i].isChanged)
   1.391 +            continue;
   1.392 +
   1.393 +        id = touchEvent->touches[i].identifier;
   1.394 +        x = touchEvent->touches[i].canvasX;
   1.395 +        y = touchEvent->touches[i].canvasY;
   1.396 +
   1.397 +        if (eventType == EMSCRIPTEN_EVENT_TOUCHMOVE) {
   1.398 +            SDL_SendTouchMotion(deviceId, id, x, y, 1.0f);
   1.399 +        } else if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) {
   1.400 +            SDL_SendTouch(deviceId, id, SDL_TRUE, x, y, 1.0f);
   1.401 +        } else {
   1.402 +            SDL_SendTouch(deviceId, id, SDL_FALSE, x, y, 1.0f);
   1.403 +        }
   1.404 +    }
   1.405 +
   1.406 +
   1.407 +    return 1;
   1.408 +}
   1.409 +
   1.410 +int
   1.411 +Emscripten_HandleKey(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
   1.412 +{
   1.413 +    Uint32 scancode;
   1.414 +
   1.415 +    /* .keyCode is deprecated, but still the most reliable way to get keys */
   1.416 +    if (keyEvent->keyCode < SDL_arraysize(emscripten_scancode_table)) {
   1.417 +        scancode = emscripten_scancode_table[keyEvent->keyCode];
   1.418 +
   1.419 +        if (scancode != SDL_SCANCODE_UNKNOWN) {
   1.420 +
   1.421 +            if (keyEvent->location == DOM_KEY_LOCATION_RIGHT) {
   1.422 +                switch (scancode) {
   1.423 +                    case SDL_SCANCODE_LSHIFT:
   1.424 +                        scancode = SDL_SCANCODE_RSHIFT;
   1.425 +                        break;
   1.426 +                    case SDL_SCANCODE_LCTRL:
   1.427 +                        scancode = SDL_SCANCODE_RCTRL;
   1.428 +                        break;
   1.429 +                    case SDL_SCANCODE_LALT:
   1.430 +                        scancode = SDL_SCANCODE_RALT;
   1.431 +                        break;
   1.432 +                    case SDL_SCANCODE_LGUI:
   1.433 +                        scancode = SDL_SCANCODE_RGUI;
   1.434 +                        break;
   1.435 +                }
   1.436 +            }
   1.437 +            SDL_SendKeyboardKey(eventType == EMSCRIPTEN_EVENT_KEYDOWN ?
   1.438 +                                SDL_PRESSED : SDL_RELEASED, scancode);
   1.439 +        }
   1.440 +    }
   1.441 +
   1.442 +    /* if we prevent keydown, we won't get keypress
   1.443 +     * also we need to ALWAYS prevent backspace and tab otherwise chrome takes action and does bad navigation UX
   1.444 +     */
   1.445 +    return SDL_GetEventState(SDL_TEXTINPUT) != SDL_ENABLE || eventType != EMSCRIPTEN_EVENT_KEYDOWN
   1.446 +            || keyEvent->keyCode == 8 /* backspace */ || keyEvent->keyCode == 9 /* tab */;
   1.447 +}
   1.448 +
   1.449 +int
   1.450 +Emscripten_HandleKeyPress(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
   1.451 +{
   1.452 +    char text[5];
   1.453 +    Emscripten_ConvertUTF32toUTF8(keyEvent->charCode, text);
   1.454 +    SDL_SendKeyboardText(text);
   1.455 +    return 1;
   1.456 +}
   1.457 +
   1.458 +int
   1.459 +Emscripten_HandleFullscreenChange(int eventType, const EmscriptenFullscreenChangeEvent *fullscreenChangeEvent, void *userData)
   1.460 +{
   1.461 +    /*make sure this is actually our element going fullscreen*/
   1.462 +    if(SDL_strcmp(fullscreenChangeEvent->id, "SDLFullscreenElement") != 0)
   1.463 +        return 0;
   1.464 +
   1.465 +    SDL_WindowData *window_data = userData;
   1.466 +    if(fullscreenChangeEvent->isFullscreen)
   1.467 +    {
   1.468 +        SDL_bool is_desktop_fullscreen;
   1.469 +        window_data->window->flags |= window_data->requested_fullscreen_mode;
   1.470 +
   1.471 +        if(!window_data->requested_fullscreen_mode)
   1.472 +            window_data->window->flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; /*we didn't reqest fullscreen*/
   1.473 +
   1.474 +        window_data->requested_fullscreen_mode = 0;
   1.475 +
   1.476 +        is_desktop_fullscreen = (window_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
   1.477 +
   1.478 +        /*update size*/
   1.479 +        if(window_data->window->flags & SDL_WINDOW_RESIZABLE || is_desktop_fullscreen)
   1.480 +        {
   1.481 +            emscripten_set_canvas_size(fullscreenChangeEvent->screenWidth, fullscreenChangeEvent->screenHeight);
   1.482 +            SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, fullscreenChangeEvent->screenWidth, fullscreenChangeEvent->screenHeight);
   1.483 +        }
   1.484 +        else
   1.485 +        {
   1.486 +            /*preserve ratio*/
   1.487 +            double w = window_data->window->w;
   1.488 +            double h = window_data->window->h;
   1.489 +            double factor = SDL_min(fullscreenChangeEvent->screenWidth / w, fullscreenChangeEvent->screenHeight / h);
   1.490 +            emscripten_set_element_css_size(NULL, w * factor, h * factor);
   1.491 +        }
   1.492 +    }
   1.493 +    else
   1.494 +    {
   1.495 +        EM_ASM({
   1.496 +            //un-reparent canvas (similar to Module.requestFullscreen)
   1.497 +            var canvas = Module['canvas'];
   1.498 +            if(canvas.parentNode.id == "SDLFullscreenElement") {
   1.499 +                var canvasContainer = canvas.parentNode;
   1.500 +                canvasContainer.parentNode.insertBefore(canvas, canvasContainer);
   1.501 +                canvasContainer.parentNode.removeChild(canvasContainer);
   1.502 +            }
   1.503 +        });
   1.504 +        double unscaled_w = window_data->windowed_width / window_data->pixel_ratio;
   1.505 +        double unscaled_h = window_data->windowed_height / window_data->pixel_ratio;
   1.506 +        emscripten_set_canvas_size(window_data->windowed_width, window_data->windowed_height);
   1.507 +
   1.508 +        if (!window_data->external_size && window_data->pixel_ratio != 1.0f) {
   1.509 +            emscripten_set_element_css_size(NULL, unscaled_w, unscaled_h);
   1.510 +        }
   1.511 +
   1.512 +        SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, unscaled_w, unscaled_h);
   1.513 +
   1.514 +        window_data->window->flags &= ~FULLSCREEN_MASK;
   1.515 +    }
   1.516 +
   1.517 +    return 0;
   1.518 +}
   1.519 +
   1.520 +int
   1.521 +Emscripten_HandleResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
   1.522 +{
   1.523 +    SDL_WindowData *window_data = userData;
   1.524 +    if(window_data->window->flags & FULLSCREEN_MASK)
   1.525 +    {
   1.526 +        SDL_bool is_desktop_fullscreen = (window_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
   1.527 +
   1.528 +        if(window_data->window->flags & SDL_WINDOW_RESIZABLE || is_desktop_fullscreen)
   1.529 +        {
   1.530 +            emscripten_set_canvas_size(uiEvent->windowInnerWidth * window_data->pixel_ratio, uiEvent->windowInnerHeight * window_data->pixel_ratio);
   1.531 +            SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, uiEvent->windowInnerWidth, uiEvent->windowInnerHeight);
   1.532 +        }
   1.533 +    }
   1.534 +    else
   1.535 +    {
   1.536 +        /* this will only work if the canvas size is set through css */
   1.537 +        if(window_data->window->flags & SDL_WINDOW_RESIZABLE)
   1.538 +        {
   1.539 +            double w = window_data->window->w;
   1.540 +            double h = window_data->window->h;
   1.541 +
   1.542 +            if(window_data->external_size) {
   1.543 +                emscripten_get_element_css_size(NULL, &w, &h);
   1.544 +            }
   1.545 +
   1.546 +            emscripten_set_canvas_size(w * window_data->pixel_ratio, h * window_data->pixel_ratio);
   1.547 +
   1.548 +            /* set_canvas_size unsets this */
   1.549 +            if (!window_data->external_size && window_data->pixel_ratio != 1.0f) {
   1.550 +                emscripten_set_element_css_size(NULL, w, h);
   1.551 +            }
   1.552 +
   1.553 +            SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
   1.554 +        }
   1.555 +    }
   1.556 +
   1.557 +    return 0;
   1.558 +}
   1.559 +
   1.560 +int
   1.561 +Emscripten_HandleVisibilityChange(int eventType, const EmscriptenVisibilityChangeEvent *visEvent, void *userData)
   1.562 +{
   1.563 +    SDL_WindowData *window_data = userData;
   1.564 +    SDL_SendWindowEvent(window_data->window, visEvent->hidden ? SDL_WINDOWEVENT_HIDDEN : SDL_WINDOWEVENT_SHOWN, 0, 0);
   1.565 +    return 0;
   1.566 +}
   1.567 +
   1.568 +void
   1.569 +Emscripten_RegisterEventHandlers(SDL_WindowData *data)
   1.570 +{
   1.571 +    /* There is only one window and that window is the canvas */
   1.572 +    emscripten_set_mousemove_callback("#canvas", data, 0, Emscripten_HandleMouseMove);
   1.573 +
   1.574 +    emscripten_set_mousedown_callback("#canvas", data, 0, Emscripten_HandleMouseButton);
   1.575 +    emscripten_set_mouseup_callback("#canvas", data, 0, Emscripten_HandleMouseButton);
   1.576 +
   1.577 +    emscripten_set_mouseenter_callback("#canvas", data, 0, Emscripten_HandleMouseFocus);
   1.578 +    emscripten_set_mouseleave_callback("#canvas", data, 0, Emscripten_HandleMouseFocus);
   1.579 +
   1.580 +    emscripten_set_wheel_callback("#canvas", data, 0, Emscripten_HandleWheel);
   1.581 +
   1.582 +    emscripten_set_focus_callback("#canvas", data, 0, Emscripten_HandleFocus);
   1.583 +    emscripten_set_blur_callback("#canvas", data, 0, Emscripten_HandleFocus);
   1.584 +
   1.585 +    emscripten_set_touchstart_callback("#canvas", data, 0, Emscripten_HandleTouch);
   1.586 +    emscripten_set_touchend_callback("#canvas", data, 0, Emscripten_HandleTouch);
   1.587 +    emscripten_set_touchmove_callback("#canvas", data, 0, Emscripten_HandleTouch);
   1.588 +    emscripten_set_touchcancel_callback("#canvas", data, 0, Emscripten_HandleTouch);
   1.589 +
   1.590 +    /* Keyboard events are awkward */
   1.591 +    const char *keyElement = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
   1.592 +    if (!keyElement) keyElement = "#window";
   1.593 +
   1.594 +    emscripten_set_keydown_callback(keyElement, data, 0, Emscripten_HandleKey);
   1.595 +    emscripten_set_keyup_callback(keyElement, data, 0, Emscripten_HandleKey);
   1.596 +    emscripten_set_keypress_callback(keyElement, data, 0, Emscripten_HandleKeyPress);
   1.597 +
   1.598 +    emscripten_set_fullscreenchange_callback("#document", data, 0, Emscripten_HandleFullscreenChange);
   1.599 +
   1.600 +    emscripten_set_resize_callback("#window", data, 0, Emscripten_HandleResize);
   1.601 +
   1.602 +    emscripten_set_visibilitychange_callback(data, 0, Emscripten_HandleVisibilityChange);
   1.603 +}
   1.604 +
   1.605 +void
   1.606 +Emscripten_UnregisterEventHandlers(SDL_WindowData *data)
   1.607 +{
   1.608 +    /* only works due to having one window */
   1.609 +    emscripten_set_mousemove_callback("#canvas", NULL, 0, NULL);
   1.610 +
   1.611 +    emscripten_set_mousedown_callback("#canvas", NULL, 0, NULL);
   1.612 +    emscripten_set_mouseup_callback("#canvas", NULL, 0, NULL);
   1.613 +
   1.614 +    emscripten_set_mouseenter_callback("#canvas", NULL, 0, NULL);
   1.615 +    emscripten_set_mouseleave_callback("#canvas", NULL, 0, NULL);
   1.616 +
   1.617 +    emscripten_set_wheel_callback("#canvas", NULL, 0, NULL);
   1.618 +
   1.619 +    emscripten_set_focus_callback("#canvas", NULL, 0, NULL);
   1.620 +    emscripten_set_blur_callback("#canvas", NULL, 0, NULL);
   1.621 +
   1.622 +    emscripten_set_touchstart_callback("#canvas", NULL, 0, NULL);
   1.623 +    emscripten_set_touchend_callback("#canvas", NULL, 0, NULL);
   1.624 +    emscripten_set_touchmove_callback("#canvas", NULL, 0, NULL);
   1.625 +    emscripten_set_touchcancel_callback("#canvas", NULL, 0, NULL);
   1.626 +
   1.627 +    emscripten_set_keydown_callback("#window", NULL, 0, NULL);
   1.628 +    emscripten_set_keyup_callback("#window", NULL, 0, NULL);
   1.629 +
   1.630 +    emscripten_set_keypress_callback("#window", NULL, 0, NULL);
   1.631 +
   1.632 +    emscripten_set_fullscreenchange_callback("#document", NULL, 0, NULL);
   1.633 +
   1.634 +    emscripten_set_resize_callback("#window", NULL, 0, NULL);
   1.635 +
   1.636 +    emscripten_set_visibilitychange_callback(NULL, 0, NULL);
   1.637 +}
   1.638 +
   1.639 +#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
   1.640 +
   1.641 +/* vi: set ts=4 sw=4 expandtab: */