Skip to content

Commit 9887faa

Browse files
committed
EMCC: Emscripten version wip
- updated keyboard handling
1 parent bd3708a commit 9887faa

File tree

4 files changed

+124
-14
lines changed

4 files changed

+124
-14
lines changed

src/platform/emcc/keymap.h

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,89 @@ const int KEYMAP[][2] = {
3535
{DOM_VK_F9, SB_KEY_F(9)},
3636
{DOM_VK_F10, SB_KEY_F(10)},
3737
{DOM_VK_F11, SB_KEY_F(11)},
38-
{DOM_VK_F12, SB_KEY_F(12)}
38+
{DOM_VK_F12, SB_KEY_F(12)},
39+
{DOM_VK_CIRCUMFLEX, '^'},
40+
{DOM_VK_EXCLAMATION, '!'},
41+
{DOM_VK_DOUBLE_QUOTE, '"'},
42+
{DOM_VK_HASH, '#'},
43+
{DOM_VK_DOLLAR, '$'},
44+
{DOM_VK_PERCENT, '%'},
45+
{DOM_VK_AMPERSAND, '&'},
46+
{DOM_VK_UNDERSCORE, '_'},
47+
{DOM_VK_OPEN_PAREN, '('},
48+
{DOM_VK_CLOSE_PAREN, ')'},
49+
{DOM_VK_ASTERISK, '*'},
50+
{DOM_VK_PLUS, '+'},
51+
{DOM_VK_PIPE, '|'},
52+
{DOM_VK_HYPHEN_MINUS, '-'},
53+
{DOM_VK_OPEN_CURLY_BRACKET, '{'},
54+
{DOM_VK_CLOSE_CURLY_BRACKET, '}'},
55+
{DOM_VK_TILDE, '~'},
56+
{DOM_VK_COMMA, ','},
57+
{DOM_VK_PERIOD, '.'},
58+
{DOM_VK_SLASH, '/'},
59+
{DOM_VK_BACK_QUOTE, '`'},
60+
{DOM_VK_OPEN_BRACKET, '['},
61+
{DOM_VK_BACK_SLASH, '\\'},
62+
{DOM_VK_CLOSE_BRACKET, ']'},
63+
{DOM_VK_QUOTE, '\''},
64+
{0xBD, '-'},
65+
{0xBB, '='},
66+
{0xBA, ';'},
3967
};
4068

69+
const int SHIFT_KEYMAP[][2] = {
70+
{DOM_VK_0, ')'},
71+
{DOM_VK_1, '!'},
72+
{DOM_VK_2, '@'},
73+
{DOM_VK_3, '#'},
74+
{DOM_VK_4, '$'},
75+
{DOM_VK_5, '%'},
76+
{DOM_VK_6, '^'},
77+
{DOM_VK_7, '&'},
78+
{DOM_VK_8, '*'},
79+
{DOM_VK_9, '('},
80+
{DOM_VK_COLON, ':'},
81+
{DOM_VK_EQUALS, '+'},
82+
{DOM_VK_A, 'a'},
83+
{DOM_VK_B, 'b'},
84+
{DOM_VK_C, 'c'},
85+
{DOM_VK_D, 'd'},
86+
{DOM_VK_E, 'e'},
87+
{DOM_VK_F, 'f'},
88+
{DOM_VK_G, 'g'},
89+
{DOM_VK_H, 'h'},
90+
{DOM_VK_I, 'i'},
91+
{DOM_VK_J, 'j'},
92+
{DOM_VK_K, 'k'},
93+
{DOM_VK_L, 'l'},
94+
{DOM_VK_M, 'm'},
95+
{DOM_VK_N, 'n'},
96+
{DOM_VK_O, 'o'},
97+
{DOM_VK_P, 'p'},
98+
{DOM_VK_Q, 'q'},
99+
{DOM_VK_R, 'r'},
100+
{DOM_VK_S, 's'},
101+
{DOM_VK_T, 't'},
102+
{DOM_VK_U, 'u'},
103+
{DOM_VK_V, 'v'},
104+
{DOM_VK_W, 'w'},
105+
{DOM_VK_X, 'x'},
106+
{DOM_VK_Y, 'y'},
107+
{DOM_VK_Z, 'z'},
108+
{'`', '~'},
109+
{'-', '_'},
110+
{'=', '+'},
111+
{'[', '{'},
112+
{']', '}'},
113+
{'\\', '|'},
114+
{'\'', '"'},
115+
{';', ':'},
116+
{',', '<'},
117+
{'.', '>'},
118+
{'/', '?'},
119+
};
120+
121+
const int SHIFT_KEYMAP_LEN = sizeof(SHIFT_KEYMAP) / sizeof(SHIFT_KEYMAP[0]);
41122
const int KEYMAP_LEN = sizeof(KEYMAP) / sizeof(KEYMAP[0]);
42123

src/platform/emcc/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void init() {
4141
opt_command[0] = '\0';
4242
opt_file_permitted = 0;
4343
opt_graphics = 1;
44-
opt_ide = 0;
44+
opt_ide = IDE_INTERNAL;
4545
opt_modpath[0] = '\0';
4646
opt_nosave = 1;
4747
opt_pref_height = 0;

src/platform/emcc/runtime.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define EVENT_TYPE_RESIZE 103
2727

2828
Runtime *runtime;
29+
String clipboard;
2930

3031
MAEvent *getMotionEvent(int type, const EmscriptenMouseEvent *event) {
3132
MAEvent *result = new MAEvent();
@@ -111,6 +112,10 @@ void Runtime::browseFile(const char *url) {
111112
}, url);
112113
}
113114

115+
char *Runtime::getClipboardText() {
116+
return strdup(clipboard.c_str());
117+
}
118+
114119
char *Runtime::loadResource(const char *fileName) {
115120
logEntered();
116121
char *buffer = System::loadResource(fileName);
@@ -131,25 +136,42 @@ void Runtime::handleKeyboard(int eventType, const EmscriptenKeyboardEvent *e) {
131136
case DOM_VK_CAPS_LOCK:
132137
// ignore press without modifier key
133138
break;
139+
134140
default:
135141
for (int i = 0; i < KEYMAP_LEN; i++) {
136142
if (keyCode == KEYMAP[i][0]) {
137143
keyCode = KEYMAP[i][1];
138144
break;
139145
}
140146
}
147+
141148
if (e->ctrlKey && e->altKey) {
142149
pushEvent(getKeyPressedEvent(SB_KEY_CTRL_ALT(keyCode)));
143150
} else if (e->ctrlKey && e->shiftKey) {
144151
pushEvent(getKeyPressedEvent(SB_KEY_SHIFT_CTRL(keyCode)));
145152
} else if (e->altKey && e->shiftKey) {
146153
pushEvent(getKeyPressedEvent(SB_KEY_ALT_SHIFT(keyCode)));
147154
} else if (e->ctrlKey) {
155+
if (keyCode >= 'A' && keyCode <= 'Z') {
156+
keyCode += ('a' - 'A');
157+
}
148158
pushEvent(getKeyPressedEvent(SB_KEY_CTRL(keyCode)));
149159
} else if (e->altKey) {
150160
pushEvent(getKeyPressedEvent(SB_KEY_ALT(keyCode)));
151161
} else if (e->shiftKey) {
152-
pushEvent(getKeyPressedEvent(SB_KEY_SHIFT(keyCode)));
162+
bool shifted = false;
163+
for (int i = 0; i < SHIFT_KEYMAP_LEN; i++) {
164+
if (keyCode == SHIFT_KEYMAP[i][0]) {
165+
keyCode = SHIFT_KEYMAP[i][1];
166+
shifted = true;
167+
break;
168+
}
169+
}
170+
if (shifted) {
171+
pushEvent(getKeyPressedEvent(keyCode));
172+
} else {
173+
pushEvent(getKeyPressedEvent(SB_KEY_SHIFT(keyCode)));
174+
}
153175
} else {
154176
pushEvent(getKeyPressedEvent(keyCode));
155177
}
@@ -256,6 +278,10 @@ void Runtime::runShell() {
256278
}
257279
}
258280

281+
void Runtime::setClipboardText(const char *text) {
282+
clipboard = text;
283+
}
284+
259285
void Runtime::showCursor(CursorType cursorType) {
260286
static CursorType _cursorType;
261287
if (_cursorType != cursorType) {
@@ -366,11 +392,16 @@ void System::editSource(strlib::String loadPath, bool restoreOnExit) {
366392
case SB_KEY_F(11):
367393
case SB_KEY_F(12):
368394
case SB_KEY_MENU:
369-
case SB_KEY_ESCAPE:
370395
case SB_KEY_BREAK:
371396
// unhandled keys
372397
redraw = false;
373398
break;
399+
case SB_KEY_ESCAPE:
400+
widget = editWidget;
401+
helpWidget->hide();
402+
helpWidget->cancelMode();
403+
editWidget->setFocus(true);
404+
break;
374405
case SB_KEY_F(1):
375406
widget = helpWidget;
376407
helpWidget->createKeywordIndex();
@@ -379,22 +410,20 @@ void System::editSource(strlib::String loadPath, bool restoreOnExit) {
379410
showStatus = false;
380411
break;
381412
case SB_KEY_F(9):
413+
case SB_KEY_CTRL('r'):
382414
_state = kRunState;
383-
if (editWidget->isDirty()) {
384-
saveFile(editWidget, loadPath);
385-
}
386415
break;
387416
case SB_KEY_CTRL('s'):
388417
saveFile(editWidget, loadPath);
389418
break;
390419
case SB_KEY_CTRL('c'):
391420
case SB_KEY_CTRL('x'):
392421
text = widget->copy(event.key == (int)SB_KEY_CTRL('x'));
393-
if (text) {
394-
setClipboardText(text);
395-
free(text);
396-
}
397-
break;
422+
if (text) {
423+
setClipboardText(text);
424+
free(text);
425+
}
426+
break;
398427
case SB_KEY_CTRL('v'):
399428
text = getClipboardText();
400429
widget->paste(text);

src/platform/emcc/runtime.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct Runtime : public System {
1919
void alert(const char *title, const char *message);
2020
int ask(const char *title, const char *prompt, bool cancel=true);
2121
void browseFile(const char *url);
22-
char *getClipboardText() { return nullptr; }
22+
char *getClipboardText();
2323
int getFontSize() { return _output->getFontSize(); }
2424
void enableCursor(bool enabled) {}
2525
int handle(int event);
@@ -36,7 +36,7 @@ struct Runtime : public System {
3636
void processEvent(MAEvent &event);
3737
bool run(const char *bas) { return execute(bas); }
3838
void runShell();
39-
void setClipboardText(const char *text) {}
39+
void setClipboardText(const char *text);
4040
void setFontSize(int size);
4141
void setLoadBreak(const char *url) {}
4242
void setLoadPath(const char *url) {}

0 commit comments

Comments
 (0)