Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MorsGames
GitHub Repository: MorsGames/sm64plus
Path: blob/master/src/pc/controller/controller_emscripten_keyboard.c
7861 views
1
#ifdef TARGET_WEB
2
3
#include <string.h>
4
#include <emscripten/html5.h>
5
#include "macros.h"
6
#include "controller_keyboard.h"
7
8
static const struct {
9
const char *code;
10
int scancode;
11
} keymap_browser[] = {
12
{"Escape", 0x01},
13
{"Digit1", 0x02 },
14
{"Digit2", 0x03 },
15
{"Digit3", 0x04 },
16
{"Digit4", 0x05 },
17
{"Digit5", 0x06 },
18
{"Digit6", 0x07 },
19
{"Digit7", 0x08 },
20
{"Digit8", 0x09 },
21
{"Digit9", 0x0a },
22
{"Digit0", 0x0b },
23
{"Minus", 0x0c },
24
{"Equal", 0x0d },
25
{"Backspace", 0x0e },
26
{"Tab", 0x0f },
27
{"KeyQ", 0x10 },
28
{"KeyW", 0x11 },
29
{"KeyE", 0x12 },
30
{"KeyR", 0x13 },
31
{"KeyT", 0x14 },
32
{"KeyY", 0x15 },
33
{"KeyU", 0x16 },
34
{"KeyI", 0x17 },
35
{"KeyO", 0x18 },
36
{"KeyP", 0x19 },
37
{"BracketLeft", 0x1a },
38
{"BracketRight", 0x1b },
39
{"Enter", 0x1c },
40
{"ControlLeft", 0x1d },
41
{"KeyA", 0x1e },
42
{"KeyS", 0x1f },
43
{"KeyD", 0x20 },
44
{"KeyF", 0x21 },
45
{"KeyG", 0x22 },
46
{"KeyH", 0x23 },
47
{"KeyJ", 0x24 },
48
{"KeyK", 0x25 },
49
{"KeyL", 0x26 },
50
{"Semicolon", 0x27 },
51
{"Quote", 0x28 },
52
{"Backquote", 0x29 },
53
{"ShiftLeft", 0x2a },
54
{"Backslash", 0x2b },
55
{"KeyZ", 0x2c },
56
{"KeyX", 0x2d },
57
{"KeyC", 0x2e },
58
{"KeyV", 0x2f },
59
{"KeyB", 0x30 },
60
{"KeyN", 0x31 },
61
{"KeyM", 0x32 },
62
{"Comma", 0x33 },
63
{"Period", 0x34 },
64
{"Slash", 0x35 },
65
{"ShiftRight", 0x36 },
66
{"NumpadMultiply", 0x37 },
67
{"AltLeft", 0x38 },
68
{"Space", 0x39 },
69
{"CapsLock", 0x3a },
70
{"F1", 0x3b },
71
{"F2", 0x3c },
72
{"F3", 0x3d },
73
{"F4", 0x3e },
74
{"F5", 0x3f },
75
{"F6", 0x40 },
76
{"F7", 0x41 },
77
{"F8", 0x42 },
78
{"F9", 0x43 },
79
{"F10", 0x44 },
80
{"NumLock", 0x45 },
81
{"ScrollLock", 0x46 },
82
{"Numpad7", 0x47 },
83
{"Numpad8", 0x48 },
84
{"Numpad9", 0x49 },
85
{"NumpadSubtract", 0x4a },
86
{"Numpad4", 0x4b },
87
{"Numpad5", 0x4c },
88
{"Numpad6", 0x4d },
89
{"NumpadAdd", 0x4e },
90
{"Numpad1", 0x4f },
91
{"Numpad2", 0x50 },
92
{"Numpad3", 0x51 },
93
{"Numpad0", 0x52 },
94
{"NumpadDecimal", 0x53 },
95
{"PrintScreen", 0x54 },
96
// 0x55
97
{"IntlBackslash", 0x56 },
98
{"F11", 0x57 },
99
{"F12", 0x58 },
100
{"IntlRo", 0x59 },
101
//{"Katakana", 0 },
102
//{"Hiragana", 0 },
103
{"NumpadEnter", 0x11c },
104
{"ControlRight", 0x11d },
105
{"NumpadDivide", 0x135 },
106
{"AltRight", 0x138 },
107
{"Home", 0x147 },
108
{"ArrowUp", 0x148 },
109
{"PageUp", 0x149 },
110
{"ArrowLeft", 0x14b },
111
{"ArrowRight", 0x14d },
112
{"End", 0x14f },
113
{"ArrowDown", 0x150 },
114
{"PageDown", 0x151 },
115
{"Insert", 0x152 },
116
{"Delete", 0x153 },
117
{"Pause", 0x21d },
118
{"MetaLeft", 0x15b },
119
{"MetaRight", 0x15c },
120
{"ContextMenu", 0x15d },
121
};
122
123
static EM_BOOL controller_emscripten_keyboard_handler(int event_type, const EmscriptenKeyboardEvent *key_event, UNUSED void *user_data) {
124
for (size_t i = 0; i < sizeof(keymap_browser) / sizeof(keymap_browser[0]); i++) {
125
if (strcmp(key_event->code, keymap_browser[i].code) == 0) {
126
if (event_type == EMSCRIPTEN_EVENT_KEYDOWN) {
127
return keyboard_on_key_down(keymap_browser[i].scancode);
128
} else if (event_type == EMSCRIPTEN_EVENT_KEYUP) {
129
return keyboard_on_key_up(keymap_browser[i].scancode);
130
}
131
break;
132
}
133
}
134
return EM_FALSE;
135
}
136
137
static EM_BOOL controller_emscripten_keyboard_blur_handler(UNUSED int event_type, UNUSED const EmscriptenFocusEvent *focus_event, UNUSED void *user_data) {
138
keyboard_on_all_keys_up();
139
return EM_TRUE;
140
}
141
142
void controller_emscripten_keyboard_init(void) {
143
// Should be #window according to docs, but that crashes
144
const char *target = EMSCRIPTEN_EVENT_TARGET_WINDOW;
145
146
emscripten_set_keydown_callback(target, NULL, EM_FALSE, controller_emscripten_keyboard_handler);
147
emscripten_set_keyup_callback(target, NULL, EM_FALSE, controller_emscripten_keyboard_handler);
148
emscripten_set_blur_callback(target, NULL, EM_FALSE, controller_emscripten_keyboard_blur_handler);
149
}
150
151
#endif
152
153