src/video/emscripten/SDL_emscriptenevents.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Fri, 06 Mar 2015 21:26:37 +0100
changeset 9377 4c29dc2baaa8
parent 9352 2cbf791a601c
child 9378 71743b696c06
permissions -rw-r--r--
Emscripten: Partially fixed not removing key event callbacks if hint was used.

The key event callbacks were always removed from the same target although it is
possible to set them to different targets using the hint. This is only a partial
fix because it assumes that the hint is not changed to a different value later.
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/html5.h>
icculus@9278
    28
icculus@9278
    29
#include "../../events/SDL_events_c.h"
icculus@9278
    30
#include "../../events/SDL_keyboard_c.h"
icculus@9278
    31
#include "../../events/SDL_touch_c.h"
icculus@9278
    32
icculus@9278
    33
#include "SDL_emscriptenevents.h"
icculus@9278
    34
#include "SDL_emscriptenvideo.h"
icculus@9278
    35
icculus@9278
    36
#include "SDL_hints.h"
icculus@9278
    37
icculus@9278
    38
#define FULLSCREEN_MASK ( SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN )
icculus@9278
    39
icculus@9278
    40
/*
icculus@9278
    41
.which to scancode
icculus@9278
    42
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Constants
icculus@9278
    43
*/
icculus@9278
    44
static const SDL_Scancode emscripten_scancode_table[] = {
icculus@9278
    45
    /*  0 */    SDL_SCANCODE_UNKNOWN,
icculus@9278
    46
    /*  1 */    SDL_SCANCODE_UNKNOWN,
icculus@9278
    47
    /*  2 */    SDL_SCANCODE_UNKNOWN,
icculus@9278
    48
    /*  3 */    SDL_SCANCODE_CANCEL,
icculus@9278
    49
    /*  4 */    SDL_SCANCODE_UNKNOWN,
icculus@9278
    50
    /*  5 */    SDL_SCANCODE_UNKNOWN,
icculus@9278
    51
    /*  6 */    SDL_SCANCODE_HELP,
icculus@9278
    52
    /*  7 */    SDL_SCANCODE_UNKNOWN,
icculus@9278
    53
    /*  8 */    SDL_SCANCODE_BACKSPACE,
icculus@9278
    54
    /*  9 */    SDL_SCANCODE_TAB,
icculus@9278
    55
    /*  10 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    56
    /*  11 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    57
    /*  12 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    58
    /*  13 */   SDL_SCANCODE_RETURN,
icculus@9278
    59
    /*  14 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    60
    /*  15 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    61
    /*  16 */   SDL_SCANCODE_LSHIFT,
icculus@9278
    62
    /*  17 */   SDL_SCANCODE_LCTRL,
icculus@9278
    63
    /*  18 */   SDL_SCANCODE_LALT,
icculus@9278
    64
    /*  19 */   SDL_SCANCODE_PAUSE,
icculus@9278
    65
    /*  20 */   SDL_SCANCODE_CAPSLOCK,
icculus@9278
    66
    /*  21 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    67
    /*  22 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    68
    /*  23 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    69
    /*  24 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    70
    /*  25 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    71
    /*  26 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    72
    /*  27 */   SDL_SCANCODE_ESCAPE,
icculus@9278
    73
    /*  28 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    74
    /*  29 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    75
    /*  30 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    76
    /*  31 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    77
    /*  32 */   SDL_SCANCODE_SPACE,
icculus@9278
    78
    /*  33 */   SDL_SCANCODE_PAGEUP,
icculus@9278
    79
    /*  34 */   SDL_SCANCODE_PAGEDOWN,
icculus@9278
    80
    /*  35 */   SDL_SCANCODE_END,
icculus@9278
    81
    /*  36 */   SDL_SCANCODE_HOME,
icculus@9278
    82
    /*  37 */   SDL_SCANCODE_LEFT,
icculus@9278
    83
    /*  38 */   SDL_SCANCODE_UP,
icculus@9278
    84
    /*  39 */   SDL_SCANCODE_RIGHT,
icculus@9278
    85
    /*  40 */   SDL_SCANCODE_DOWN,
icculus@9278
    86
    /*  41 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    87
    /*  42 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    88
    /*  43 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    89
    /*  44 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    90
    /*  45 */   SDL_SCANCODE_INSERT,
icculus@9278
    91
    /*  46 */   SDL_SCANCODE_DELETE,
icculus@9278
    92
    /*  47 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
    93
    /*  48 */   SDL_SCANCODE_0,
icculus@9278
    94
    /*  49 */   SDL_SCANCODE_1,
icculus@9278
    95
    /*  50 */   SDL_SCANCODE_2,
icculus@9278
    96
    /*  51 */   SDL_SCANCODE_3,
icculus@9278
    97
    /*  52 */   SDL_SCANCODE_4,
icculus@9278
    98
    /*  53 */   SDL_SCANCODE_5,
icculus@9278
    99
    /*  54 */   SDL_SCANCODE_6,
icculus@9278
   100
    /*  55 */   SDL_SCANCODE_7,
icculus@9278
   101
    /*  56 */   SDL_SCANCODE_8,
icculus@9278
   102
    /*  57 */   SDL_SCANCODE_9,
icculus@9278
   103
    /*  58 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   104
    /*  59 */   SDL_SCANCODE_SEMICOLON,
icculus@9278
   105
    /*  60 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   106
    /*  61 */   SDL_SCANCODE_EQUALS,
icculus@9278
   107
    /*  62 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   108
    /*  63 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   109
    /*  64 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   110
    /*  65 */   SDL_SCANCODE_A,
icculus@9278
   111
    /*  66 */   SDL_SCANCODE_B,
icculus@9278
   112
    /*  67 */   SDL_SCANCODE_C,
icculus@9278
   113
    /*  68 */   SDL_SCANCODE_D,
icculus@9278
   114
    /*  69 */   SDL_SCANCODE_E,
icculus@9278
   115
    /*  70 */   SDL_SCANCODE_F,
icculus@9278
   116
    /*  71 */   SDL_SCANCODE_G,
icculus@9278
   117
    /*  72 */   SDL_SCANCODE_H,
icculus@9278
   118
    /*  73 */   SDL_SCANCODE_I,
icculus@9278
   119
    /*  74 */   SDL_SCANCODE_J,
icculus@9278
   120
    /*  75 */   SDL_SCANCODE_K,
icculus@9278
   121
    /*  76 */   SDL_SCANCODE_L,
icculus@9278
   122
    /*  77 */   SDL_SCANCODE_M,
icculus@9278
   123
    /*  78 */   SDL_SCANCODE_N,
icculus@9278
   124
    /*  79 */   SDL_SCANCODE_O,
icculus@9278
   125
    /*  80 */   SDL_SCANCODE_P,
icculus@9278
   126
    /*  81 */   SDL_SCANCODE_Q,
icculus@9278
   127
    /*  82 */   SDL_SCANCODE_R,
icculus@9278
   128
    /*  83 */   SDL_SCANCODE_S,
icculus@9278
   129
    /*  84 */   SDL_SCANCODE_T,
icculus@9278
   130
    /*  85 */   SDL_SCANCODE_U,
icculus@9278
   131
    /*  86 */   SDL_SCANCODE_V,
icculus@9278
   132
    /*  87 */   SDL_SCANCODE_W,
icculus@9278
   133
    /*  88 */   SDL_SCANCODE_X,
icculus@9278
   134
    /*  89 */   SDL_SCANCODE_Y,
icculus@9278
   135
    /*  90 */   SDL_SCANCODE_Z,
icculus@9278
   136
    /*  91 */   SDL_SCANCODE_LGUI,
icculus@9278
   137
    /*  92 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   138
    /*  93 */   SDL_SCANCODE_APPLICATION,
icculus@9278
   139
    /*  94 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   140
    /*  95 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   141
    /*  96 */   SDL_SCANCODE_KP_0,
icculus@9278
   142
    /*  97 */   SDL_SCANCODE_KP_1,
icculus@9278
   143
    /*  98 */   SDL_SCANCODE_KP_2,
icculus@9278
   144
    /*  99 */   SDL_SCANCODE_KP_3,
icculus@9278
   145
    /* 100 */   SDL_SCANCODE_KP_4,
icculus@9278
   146
    /* 101 */   SDL_SCANCODE_KP_5,
icculus@9278
   147
    /* 102 */   SDL_SCANCODE_KP_6,
icculus@9278
   148
    /* 103 */   SDL_SCANCODE_KP_7,
icculus@9278
   149
    /* 104 */   SDL_SCANCODE_KP_8,
icculus@9278
   150
    /* 105 */   SDL_SCANCODE_KP_9,
icculus@9278
   151
    /* 106 */   SDL_SCANCODE_KP_MULTIPLY,
icculus@9278
   152
    /* 107 */   SDL_SCANCODE_KP_PLUS,
icculus@9278
   153
    /* 108 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   154
    /* 109 */   SDL_SCANCODE_KP_MINUS,
icculus@9278
   155
    /* 110 */   SDL_SCANCODE_KP_PERIOD,
icculus@9278
   156
    /* 111 */   SDL_SCANCODE_KP_DIVIDE,
icculus@9278
   157
    /* 112 */   SDL_SCANCODE_F1,
icculus@9278
   158
    /* 113 */   SDL_SCANCODE_F2,
icculus@9278
   159
    /* 114 */   SDL_SCANCODE_F3,
icculus@9278
   160
    /* 115 */   SDL_SCANCODE_F4,
icculus@9278
   161
    /* 116 */   SDL_SCANCODE_F5,
icculus@9278
   162
    /* 117 */   SDL_SCANCODE_F6,
icculus@9278
   163
    /* 118 */   SDL_SCANCODE_F7,
icculus@9278
   164
    /* 119 */   SDL_SCANCODE_F8,
icculus@9278
   165
    /* 120 */   SDL_SCANCODE_F9,
icculus@9278
   166
    /* 121 */   SDL_SCANCODE_F10,
icculus@9278
   167
    /* 122 */   SDL_SCANCODE_F11,
icculus@9278
   168
    /* 123 */   SDL_SCANCODE_F12,
icculus@9278
   169
    /* 124 */   SDL_SCANCODE_F13,
icculus@9278
   170
    /* 125 */   SDL_SCANCODE_F14,
icculus@9278
   171
    /* 126 */   SDL_SCANCODE_F15,
icculus@9278
   172
    /* 127 */   SDL_SCANCODE_F16,
icculus@9278
   173
    /* 128 */   SDL_SCANCODE_F17,
icculus@9278
   174
    /* 129 */   SDL_SCANCODE_F18,
icculus@9278
   175
    /* 130 */   SDL_SCANCODE_F19,
icculus@9278
   176
    /* 131 */   SDL_SCANCODE_F20,
icculus@9278
   177
    /* 132 */   SDL_SCANCODE_F21,
icculus@9278
   178
    /* 133 */   SDL_SCANCODE_F22,
icculus@9278
   179
    /* 134 */   SDL_SCANCODE_F23,
icculus@9278
   180
    /* 135 */   SDL_SCANCODE_F24,
icculus@9278
   181
    /* 136 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   182
    /* 137 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   183
    /* 138 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   184
    /* 139 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   185
    /* 140 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   186
    /* 141 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   187
    /* 142 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   188
    /* 143 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   189
    /* 144 */   SDL_SCANCODE_NUMLOCKCLEAR,
icculus@9278
   190
    /* 145 */   SDL_SCANCODE_SCROLLLOCK,
icculus@9278
   191
    /* 146 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   192
    /* 147 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   193
    /* 148 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   194
    /* 149 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   195
    /* 150 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   196
    /* 151 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   197
    /* 152 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   198
    /* 153 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   199
    /* 154 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   200
    /* 155 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   201
    /* 156 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   202
    /* 157 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   203
    /* 158 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   204
    /* 159 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   205
    /* 160 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   206
    /* 161 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   207
    /* 162 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   208
    /* 163 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   209
    /* 164 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   210
    /* 165 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   211
    /* 166 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   212
    /* 167 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   213
    /* 168 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   214
    /* 169 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   215
    /* 170 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   216
    /* 171 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   217
    /* 172 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   218
    /* 173 */   SDL_SCANCODE_MINUS, /*FX*/
icculus@9278
   219
    /* 174 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   220
    /* 175 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   221
    /* 176 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   222
    /* 177 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   223
    /* 178 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   224
    /* 179 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   225
    /* 180 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   226
    /* 181 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   227
    /* 182 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   228
    /* 183 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   229
    /* 184 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   230
    /* 185 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   231
    /* 186 */   SDL_SCANCODE_SEMICOLON, /*IE, Chrome, D3E legacy*/
icculus@9278
   232
    /* 187 */   SDL_SCANCODE_EQUALS, /*IE, Chrome, D3E legacy*/
icculus@9278
   233
    /* 188 */   SDL_SCANCODE_COMMA,
icculus@9278
   234
    /* 189 */   SDL_SCANCODE_MINUS, /*IE, Chrome, D3E legacy*/
icculus@9278
   235
    /* 190 */   SDL_SCANCODE_PERIOD,
icculus@9278
   236
    /* 191 */   SDL_SCANCODE_SLASH,
icculus@9278
   237
    /* 192 */   SDL_SCANCODE_GRAVE, /*FX, D3E legacy (SDL_SCANCODE_APOSTROPHE in IE/Chrome)*/
icculus@9278
   238
    /* 193 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   239
    /* 194 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   240
    /* 195 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   241
    /* 196 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   242
    /* 197 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   243
    /* 198 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   244
    /* 199 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   245
    /* 200 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   246
    /* 201 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   247
    /* 202 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   248
    /* 203 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   249
    /* 204 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   250
    /* 205 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   251
    /* 206 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   252
    /* 207 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   253
    /* 208 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   254
    /* 209 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   255
    /* 210 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   256
    /* 211 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   257
    /* 212 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   258
    /* 213 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   259
    /* 214 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   260
    /* 215 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   261
    /* 216 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   262
    /* 217 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   263
    /* 218 */   SDL_SCANCODE_UNKNOWN,
icculus@9278
   264
    /* 219 */   SDL_SCANCODE_LEFTBRACKET,
icculus@9278
   265
    /* 220 */   SDL_SCANCODE_BACKSLASH,
icculus@9278
   266
    /* 221 */   SDL_SCANCODE_RIGHTBRACKET,
icculus@9278
   267
    /* 222 */   SDL_SCANCODE_APOSTROPHE, /*FX, D3E legacy*/
icculus@9278
   268
};
icculus@9278
   269
icculus@9278
   270
icculus@9278
   271
/* "borrowed" from SDL_windowsevents.c */
icculus@9278
   272
int
icculus@9278
   273
Emscripten_ConvertUTF32toUTF8(Uint32 codepoint, char * text)
icculus@9278
   274
{
icculus@9278
   275
    if (codepoint <= 0x7F) {
icculus@9278
   276
        text[0] = (char) codepoint;
icculus@9278
   277
        text[1] = '\0';
icculus@9278
   278
    } else if (codepoint <= 0x7FF) {
icculus@9278
   279
        text[0] = 0xC0 | (char) ((codepoint >> 6) & 0x1F);
icculus@9278
   280
        text[1] = 0x80 | (char) (codepoint & 0x3F);
icculus@9278
   281
        text[2] = '\0';
icculus@9278
   282
    } else if (codepoint <= 0xFFFF) {
icculus@9278
   283
        text[0] = 0xE0 | (char) ((codepoint >> 12) & 0x0F);
icculus@9278
   284
        text[1] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
icculus@9278
   285
        text[2] = 0x80 | (char) (codepoint & 0x3F);
icculus@9278
   286
        text[3] = '\0';
icculus@9278
   287
    } else if (codepoint <= 0x10FFFF) {
icculus@9278
   288
        text[0] = 0xF0 | (char) ((codepoint >> 18) & 0x0F);
icculus@9278
   289
        text[1] = 0x80 | (char) ((codepoint >> 12) & 0x3F);
icculus@9278
   290
        text[2] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
icculus@9278
   291
        text[3] = 0x80 | (char) (codepoint & 0x3F);
icculus@9278
   292
        text[4] = '\0';
icculus@9278
   293
    } else {
icculus@9278
   294
        return SDL_FALSE;
icculus@9278
   295
    }
icculus@9278
   296
    return SDL_TRUE;
icculus@9278
   297
}
icculus@9278
   298
philipp@9352
   299
EM_BOOL
icculus@9278
   300
Emscripten_HandleMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
icculus@9278
   301
{
icculus@9278
   302
    SDL_WindowData *window_data = userData;
icculus@9278
   303
    int mx = mouseEvent->canvasX, my = mouseEvent->canvasY;
icculus@9278
   304
    EmscriptenPointerlockChangeEvent pointerlock_status;
icculus@9278
   305
icculus@9278
   306
    /* check for pointer lock */
icculus@9278
   307
    emscripten_get_pointerlock_status(&pointerlock_status);
icculus@9278
   308
icculus@9278
   309
    if (pointerlock_status.isActive) {
icculus@9278
   310
        mx = mouseEvent->movementX;
icculus@9278
   311
        my = mouseEvent->movementY;
icculus@9278
   312
    }
icculus@9278
   313
icculus@9278
   314
    /* rescale (in case canvas is being scaled)*/
icculus@9278
   315
    double client_w, client_h;
icculus@9278
   316
    emscripten_get_element_css_size(NULL, &client_w, &client_h);
icculus@9278
   317
icculus@9278
   318
    mx = mx * (window_data->window->w / (client_w * window_data->pixel_ratio));
icculus@9278
   319
    my = my * (window_data->window->h / (client_h * window_data->pixel_ratio));
icculus@9278
   320
icculus@9278
   321
    SDL_SendMouseMotion(window_data->window, 0, pointerlock_status.isActive, mx, my);
icculus@9278
   322
    return 0;
icculus@9278
   323
}
icculus@9278
   324
philipp@9352
   325
EM_BOOL
icculus@9278
   326
Emscripten_HandleMouseButton(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
icculus@9278
   327
{
icculus@9278
   328
    SDL_WindowData *window_data = userData;
icculus@9278
   329
    uint32_t sdl_button;
icculus@9278
   330
    switch (mouseEvent->button) {
icculus@9278
   331
        case 0:
icculus@9278
   332
            sdl_button = SDL_BUTTON_LEFT;
icculus@9278
   333
            break;
icculus@9278
   334
        case 1:
icculus@9278
   335
            sdl_button = SDL_BUTTON_MIDDLE;
icculus@9278
   336
            break;
icculus@9278
   337
        case 2:
icculus@9278
   338
            sdl_button = SDL_BUTTON_RIGHT;
icculus@9278
   339
            break;
icculus@9278
   340
        default:
icculus@9278
   341
            return 0;
icculus@9278
   342
    }
icculus@9278
   343
    SDL_SendMouseButton(window_data->window, 0, eventType == EMSCRIPTEN_EVENT_MOUSEDOWN ? SDL_PRESSED : SDL_RELEASED, sdl_button);
icculus@9278
   344
    return 1;
icculus@9278
   345
}
icculus@9278
   346
philipp@9352
   347
EM_BOOL
icculus@9278
   348
Emscripten_HandleMouseFocus(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
icculus@9278
   349
{
icculus@9278
   350
    SDL_WindowData *window_data = userData;
icculus@9278
   351
    SDL_SendWindowEvent(window_data->window, eventType == EMSCRIPTEN_EVENT_MOUSEENTER ? SDL_WINDOWEVENT_ENTER : SDL_WINDOWEVENT_LEAVE, 0, 0);
icculus@9278
   352
    return 1;
icculus@9278
   353
}
icculus@9278
   354
philipp@9352
   355
EM_BOOL
icculus@9278
   356
Emscripten_HandleWheel(int eventType, const EmscriptenWheelEvent *wheelEvent, void *userData)
icculus@9278
   357
{
icculus@9278
   358
    SDL_WindowData *window_data = userData;
icculus@9278
   359
    SDL_SendMouseWheel(window_data->window, 0, wheelEvent->deltaX, -wheelEvent->deltaY, SDL_MOUSEWHEEL_NORMAL);
icculus@9278
   360
    return 1;
icculus@9278
   361
}
icculus@9278
   362
philipp@9352
   363
EM_BOOL
icculus@9278
   364
Emscripten_HandleFocus(int eventType, const EmscriptenFocusEvent *wheelEvent, void *userData)
icculus@9278
   365
{
icculus@9278
   366
    SDL_WindowData *window_data = userData;
icculus@9278
   367
    SDL_SendWindowEvent(window_data->window, eventType == EMSCRIPTEN_EVENT_FOCUS ? SDL_WINDOWEVENT_FOCUS_GAINED : SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
icculus@9278
   368
    return 1;
icculus@9278
   369
}
icculus@9278
   370
philipp@9352
   371
EM_BOOL
icculus@9278
   372
Emscripten_HandleTouch(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
icculus@9278
   373
{
icculus@9278
   374
    /*SDL_WindowData *window_data = userData;*/
icculus@9278
   375
    int i;
icculus@9278
   376
icculus@9278
   377
    SDL_TouchID deviceId = 0;
icculus@9278
   378
    if (!SDL_GetTouch(deviceId)) {
icculus@9278
   379
        if (SDL_AddTouch(deviceId, "") < 0) {
icculus@9278
   380
             return 0;
icculus@9278
   381
        }
icculus@9278
   382
    }
icculus@9278
   383
icculus@9278
   384
    for (i = 0; i < touchEvent->numTouches; i++) {
icculus@9278
   385
        long x, y, id;
icculus@9278
   386
icculus@9278
   387
        if (!touchEvent->touches[i].isChanged)
icculus@9278
   388
            continue;
icculus@9278
   389
icculus@9278
   390
        id = touchEvent->touches[i].identifier;
icculus@9278
   391
        x = touchEvent->touches[i].canvasX;
icculus@9278
   392
        y = touchEvent->touches[i].canvasY;
icculus@9278
   393
icculus@9278
   394
        if (eventType == EMSCRIPTEN_EVENT_TOUCHMOVE) {
icculus@9278
   395
            SDL_SendTouchMotion(deviceId, id, x, y, 1.0f);
icculus@9278
   396
        } else if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) {
icculus@9278
   397
            SDL_SendTouch(deviceId, id, SDL_TRUE, x, y, 1.0f);
icculus@9278
   398
        } else {
icculus@9278
   399
            SDL_SendTouch(deviceId, id, SDL_FALSE, x, y, 1.0f);
icculus@9278
   400
        }
icculus@9278
   401
    }
icculus@9278
   402
icculus@9278
   403
icculus@9278
   404
    return 1;
icculus@9278
   405
}
icculus@9278
   406
philipp@9352
   407
EM_BOOL
icculus@9278
   408
Emscripten_HandleKey(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
icculus@9278
   409
{
icculus@9278
   410
    Uint32 scancode;
icculus@9278
   411
icculus@9278
   412
    /* .keyCode is deprecated, but still the most reliable way to get keys */
icculus@9278
   413
    if (keyEvent->keyCode < SDL_arraysize(emscripten_scancode_table)) {
icculus@9278
   414
        scancode = emscripten_scancode_table[keyEvent->keyCode];
icculus@9278
   415
icculus@9278
   416
        if (scancode != SDL_SCANCODE_UNKNOWN) {
icculus@9278
   417
icculus@9278
   418
            if (keyEvent->location == DOM_KEY_LOCATION_RIGHT) {
icculus@9278
   419
                switch (scancode) {
icculus@9278
   420
                    case SDL_SCANCODE_LSHIFT:
icculus@9278
   421
                        scancode = SDL_SCANCODE_RSHIFT;
icculus@9278
   422
                        break;
icculus@9278
   423
                    case SDL_SCANCODE_LCTRL:
icculus@9278
   424
                        scancode = SDL_SCANCODE_RCTRL;
icculus@9278
   425
                        break;
icculus@9278
   426
                    case SDL_SCANCODE_LALT:
icculus@9278
   427
                        scancode = SDL_SCANCODE_RALT;
icculus@9278
   428
                        break;
icculus@9278
   429
                    case SDL_SCANCODE_LGUI:
icculus@9278
   430
                        scancode = SDL_SCANCODE_RGUI;
icculus@9278
   431
                        break;
icculus@9278
   432
                }
icculus@9278
   433
            }
icculus@9278
   434
            SDL_SendKeyboardKey(eventType == EMSCRIPTEN_EVENT_KEYDOWN ?
icculus@9278
   435
                                SDL_PRESSED : SDL_RELEASED, scancode);
icculus@9278
   436
        }
icculus@9278
   437
    }
icculus@9278
   438
icculus@9278
   439
    /* if we prevent keydown, we won't get keypress
icculus@9278
   440
     * also we need to ALWAYS prevent backspace and tab otherwise chrome takes action and does bad navigation UX
icculus@9278
   441
     */
icculus@9278
   442
    return SDL_GetEventState(SDL_TEXTINPUT) != SDL_ENABLE || eventType != EMSCRIPTEN_EVENT_KEYDOWN
icculus@9278
   443
            || keyEvent->keyCode == 8 /* backspace */ || keyEvent->keyCode == 9 /* tab */;
icculus@9278
   444
}
icculus@9278
   445
philipp@9352
   446
EM_BOOL
icculus@9278
   447
Emscripten_HandleKeyPress(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
icculus@9278
   448
{
icculus@9278
   449
    char text[5];
icculus@9278
   450
    Emscripten_ConvertUTF32toUTF8(keyEvent->charCode, text);
icculus@9278
   451
    SDL_SendKeyboardText(text);
icculus@9278
   452
    return 1;
icculus@9278
   453
}
icculus@9278
   454
philipp@9352
   455
EM_BOOL
icculus@9278
   456
Emscripten_HandleFullscreenChange(int eventType, const EmscriptenFullscreenChangeEvent *fullscreenChangeEvent, void *userData)
icculus@9278
   457
{
icculus@9278
   458
    /*make sure this is actually our element going fullscreen*/
icculus@9278
   459
    if(SDL_strcmp(fullscreenChangeEvent->id, "SDLFullscreenElement") != 0)
icculus@9278
   460
        return 0;
icculus@9278
   461
icculus@9278
   462
    SDL_WindowData *window_data = userData;
icculus@9278
   463
    if(fullscreenChangeEvent->isFullscreen)
icculus@9278
   464
    {
icculus@9278
   465
        SDL_bool is_desktop_fullscreen;
icculus@9278
   466
        window_data->window->flags |= window_data->requested_fullscreen_mode;
icculus@9278
   467
icculus@9278
   468
        if(!window_data->requested_fullscreen_mode)
icculus@9278
   469
            window_data->window->flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; /*we didn't reqest fullscreen*/
icculus@9278
   470
icculus@9278
   471
        window_data->requested_fullscreen_mode = 0;
icculus@9278
   472
icculus@9278
   473
        is_desktop_fullscreen = (window_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
icculus@9278
   474
icculus@9278
   475
        /*update size*/
icculus@9278
   476
        if(window_data->window->flags & SDL_WINDOW_RESIZABLE || is_desktop_fullscreen)
icculus@9278
   477
        {
icculus@9278
   478
            emscripten_set_canvas_size(fullscreenChangeEvent->screenWidth, fullscreenChangeEvent->screenHeight);
icculus@9278
   479
            SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, fullscreenChangeEvent->screenWidth, fullscreenChangeEvent->screenHeight);
icculus@9278
   480
        }
icculus@9278
   481
        else
icculus@9278
   482
        {
icculus@9278
   483
            /*preserve ratio*/
icculus@9278
   484
            double w = window_data->window->w;
icculus@9278
   485
            double h = window_data->window->h;
icculus@9278
   486
            double factor = SDL_min(fullscreenChangeEvent->screenWidth / w, fullscreenChangeEvent->screenHeight / h);
icculus@9278
   487
            emscripten_set_element_css_size(NULL, w * factor, h * factor);
icculus@9278
   488
        }
icculus@9278
   489
    }
icculus@9278
   490
    else
icculus@9278
   491
    {
icculus@9278
   492
        EM_ASM({
icculus@9278
   493
            //un-reparent canvas (similar to Module.requestFullscreen)
icculus@9278
   494
            var canvas = Module['canvas'];
icculus@9278
   495
            if(canvas.parentNode.id == "SDLFullscreenElement") {
icculus@9278
   496
                var canvasContainer = canvas.parentNode;
icculus@9278
   497
                canvasContainer.parentNode.insertBefore(canvas, canvasContainer);
icculus@9278
   498
                canvasContainer.parentNode.removeChild(canvasContainer);
icculus@9278
   499
            }
icculus@9278
   500
        });
icculus@9278
   501
        double unscaled_w = window_data->windowed_width / window_data->pixel_ratio;
icculus@9278
   502
        double unscaled_h = window_data->windowed_height / window_data->pixel_ratio;
icculus@9278
   503
        emscripten_set_canvas_size(window_data->windowed_width, window_data->windowed_height);
icculus@9278
   504
icculus@9278
   505
        if (!window_data->external_size && window_data->pixel_ratio != 1.0f) {
icculus@9278
   506
            emscripten_set_element_css_size(NULL, unscaled_w, unscaled_h);
icculus@9278
   507
        }
icculus@9278
   508
icculus@9278
   509
        SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, unscaled_w, unscaled_h);
icculus@9278
   510
icculus@9278
   511
        window_data->window->flags &= ~FULLSCREEN_MASK;
icculus@9278
   512
    }
icculus@9278
   513
icculus@9278
   514
    return 0;
icculus@9278
   515
}
icculus@9278
   516
philipp@9352
   517
EM_BOOL
icculus@9278
   518
Emscripten_HandleResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
icculus@9278
   519
{
icculus@9278
   520
    SDL_WindowData *window_data = userData;
icculus@9278
   521
    if(window_data->window->flags & FULLSCREEN_MASK)
icculus@9278
   522
    {
icculus@9278
   523
        SDL_bool is_desktop_fullscreen = (window_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
icculus@9278
   524
icculus@9278
   525
        if(window_data->window->flags & SDL_WINDOW_RESIZABLE || is_desktop_fullscreen)
icculus@9278
   526
        {
icculus@9278
   527
            emscripten_set_canvas_size(uiEvent->windowInnerWidth * window_data->pixel_ratio, uiEvent->windowInnerHeight * window_data->pixel_ratio);
icculus@9278
   528
            SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, uiEvent->windowInnerWidth, uiEvent->windowInnerHeight);
icculus@9278
   529
        }
icculus@9278
   530
    }
icculus@9278
   531
    else
icculus@9278
   532
    {
icculus@9278
   533
        /* this will only work if the canvas size is set through css */
icculus@9278
   534
        if(window_data->window->flags & SDL_WINDOW_RESIZABLE)
icculus@9278
   535
        {
icculus@9278
   536
            double w = window_data->window->w;
icculus@9278
   537
            double h = window_data->window->h;
icculus@9278
   538
icculus@9278
   539
            if(window_data->external_size) {
icculus@9278
   540
                emscripten_get_element_css_size(NULL, &w, &h);
icculus@9278
   541
            }
icculus@9278
   542
icculus@9278
   543
            emscripten_set_canvas_size(w * window_data->pixel_ratio, h * window_data->pixel_ratio);
icculus@9278
   544
icculus@9278
   545
            /* set_canvas_size unsets this */
icculus@9278
   546
            if (!window_data->external_size && window_data->pixel_ratio != 1.0f) {
icculus@9278
   547
                emscripten_set_element_css_size(NULL, w, h);
icculus@9278
   548
            }
icculus@9278
   549
icculus@9278
   550
            SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
icculus@9278
   551
        }
icculus@9278
   552
    }
icculus@9278
   553
icculus@9278
   554
    return 0;
icculus@9278
   555
}
icculus@9278
   556
philipp@9352
   557
EM_BOOL
icculus@9278
   558
Emscripten_HandleVisibilityChange(int eventType, const EmscriptenVisibilityChangeEvent *visEvent, void *userData)
icculus@9278
   559
{
icculus@9278
   560
    SDL_WindowData *window_data = userData;
icculus@9278
   561
    SDL_SendWindowEvent(window_data->window, visEvent->hidden ? SDL_WINDOWEVENT_HIDDEN : SDL_WINDOWEVENT_SHOWN, 0, 0);
icculus@9278
   562
    return 0;
icculus@9278
   563
}
icculus@9278
   564
icculus@9278
   565
void
icculus@9278
   566
Emscripten_RegisterEventHandlers(SDL_WindowData *data)
icculus@9278
   567
{
icculus@9278
   568
    /* There is only one window and that window is the canvas */
icculus@9278
   569
    emscripten_set_mousemove_callback("#canvas", data, 0, Emscripten_HandleMouseMove);
icculus@9278
   570
icculus@9278
   571
    emscripten_set_mousedown_callback("#canvas", data, 0, Emscripten_HandleMouseButton);
icculus@9278
   572
    emscripten_set_mouseup_callback("#canvas", data, 0, Emscripten_HandleMouseButton);
icculus@9278
   573
icculus@9278
   574
    emscripten_set_mouseenter_callback("#canvas", data, 0, Emscripten_HandleMouseFocus);
icculus@9278
   575
    emscripten_set_mouseleave_callback("#canvas", data, 0, Emscripten_HandleMouseFocus);
icculus@9278
   576
icculus@9278
   577
    emscripten_set_wheel_callback("#canvas", data, 0, Emscripten_HandleWheel);
icculus@9278
   578
icculus@9278
   579
    emscripten_set_focus_callback("#canvas", data, 0, Emscripten_HandleFocus);
icculus@9278
   580
    emscripten_set_blur_callback("#canvas", data, 0, Emscripten_HandleFocus);
icculus@9278
   581
icculus@9278
   582
    emscripten_set_touchstart_callback("#canvas", data, 0, Emscripten_HandleTouch);
icculus@9278
   583
    emscripten_set_touchend_callback("#canvas", data, 0, Emscripten_HandleTouch);
icculus@9278
   584
    emscripten_set_touchmove_callback("#canvas", data, 0, Emscripten_HandleTouch);
icculus@9278
   585
    emscripten_set_touchcancel_callback("#canvas", data, 0, Emscripten_HandleTouch);
icculus@9278
   586
icculus@9278
   587
    /* Keyboard events are awkward */
icculus@9278
   588
    const char *keyElement = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
icculus@9278
   589
    if (!keyElement) keyElement = "#window";
icculus@9278
   590
icculus@9278
   591
    emscripten_set_keydown_callback(keyElement, data, 0, Emscripten_HandleKey);
icculus@9278
   592
    emscripten_set_keyup_callback(keyElement, data, 0, Emscripten_HandleKey);
icculus@9278
   593
    emscripten_set_keypress_callback(keyElement, data, 0, Emscripten_HandleKeyPress);
icculus@9278
   594
icculus@9278
   595
    emscripten_set_fullscreenchange_callback("#document", data, 0, Emscripten_HandleFullscreenChange);
icculus@9278
   596
icculus@9278
   597
    emscripten_set_resize_callback("#window", data, 0, Emscripten_HandleResize);
icculus@9278
   598
icculus@9278
   599
    emscripten_set_visibilitychange_callback(data, 0, Emscripten_HandleVisibilityChange);
icculus@9278
   600
}
icculus@9278
   601
icculus@9278
   602
void
icculus@9278
   603
Emscripten_UnregisterEventHandlers(SDL_WindowData *data)
icculus@9278
   604
{
icculus@9278
   605
    /* only works due to having one window */
icculus@9278
   606
    emscripten_set_mousemove_callback("#canvas", NULL, 0, NULL);
icculus@9278
   607
icculus@9278
   608
    emscripten_set_mousedown_callback("#canvas", NULL, 0, NULL);
icculus@9278
   609
    emscripten_set_mouseup_callback("#canvas", NULL, 0, NULL);
icculus@9278
   610
icculus@9278
   611
    emscripten_set_mouseenter_callback("#canvas", NULL, 0, NULL);
icculus@9278
   612
    emscripten_set_mouseleave_callback("#canvas", NULL, 0, NULL);
icculus@9278
   613
icculus@9278
   614
    emscripten_set_wheel_callback("#canvas", NULL, 0, NULL);
icculus@9278
   615
icculus@9278
   616
    emscripten_set_focus_callback("#canvas", NULL, 0, NULL);
icculus@9278
   617
    emscripten_set_blur_callback("#canvas", NULL, 0, NULL);
icculus@9278
   618
icculus@9278
   619
    emscripten_set_touchstart_callback("#canvas", NULL, 0, NULL);
icculus@9278
   620
    emscripten_set_touchend_callback("#canvas", NULL, 0, NULL);
icculus@9278
   621
    emscripten_set_touchmove_callback("#canvas", NULL, 0, NULL);
icculus@9278
   622
    emscripten_set_touchcancel_callback("#canvas", NULL, 0, NULL);
icculus@9278
   623
philipp@9377
   624
    const char *target = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
philipp@9377
   625
    if (!target) {
philipp@9377
   626
        target = "#window";
philipp@9377
   627
    }
icculus@9278
   628
philipp@9377
   629
    emscripten_set_keydown_callback(target, NULL, 0, NULL);
philipp@9377
   630
    emscripten_set_keyup_callback(target, NULL, 0, NULL);
philipp@9377
   631
philipp@9377
   632
    emscripten_set_keypress_callback(target, NULL, 0, NULL);
icculus@9278
   633
icculus@9278
   634
    emscripten_set_fullscreenchange_callback("#document", NULL, 0, NULL);
icculus@9278
   635
icculus@9278
   636
    emscripten_set_resize_callback("#window", NULL, 0, NULL);
icculus@9278
   637
icculus@9278
   638
    emscripten_set_visibilitychange_callback(NULL, 0, NULL);
icculus@9278
   639
}
icculus@9278
   640
icculus@9278
   641
#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
icculus@9278
   642
icculus@9278
   643
/* vi: set ts=4 sw=4 expandtab: */