Skip to content

Commit 6b82897

Browse files
committed
EMCC: Emscripten version wip
1 parent 8187893 commit 6b82897

File tree

11 files changed

+217
-201
lines changed

11 files changed

+217
-201
lines changed

src/lib/maapi.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ void maHideVirtualKeyboard(void);
324324
*/
325325
int maGetEvent(MAEvent *event);
326326

327+
/**
328+
* Push an event onto the queue
329+
*/
330+
void maPushEvent(MAEvent *event);
331+
327332
/**
328333
* Suspends execution until there is an event in the buffer,
329334
* or \a timeout milliseconds have passed. A timeout <= 0 is considered infinite.

src/platform/android/jni/runtime.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,10 @@ int maGetEvent(MAEvent *event) {
11791179
return result;
11801180
}
11811181

1182+
void maPushEvent(MAEvent *maEvent) {
1183+
runtime->pushEvent(maEvent);
1184+
}
1185+
11821186
void maWait(int timeout) {
11831187
runtime->pause(timeout);
11841188
}

src/platform/emcc/Makefile.am

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ sbasic_html_SOURCES = \
3838
../../ui/image.cpp \
3939
../../ui/strlib.cpp \
4040
../../ui/audio.cpp \
41-
runtime.cpp \
42-
canvas.cpp \
41+
runtime.h runtime.cpp \
42+
canvas.h canvas.cpp \
4343
main.cpp
4444

45-
sbasic_html_LDFLAGS = -O3 -lembind --shell-file shell.html -s ASYNCIFY=1
45+
sbasic_html_LDFLAGS = -O3 -lembind --shell-file shell.html -s ASYNCIFY=1 --preload-file basic
4646

4747
sbasic_html_LDADD = -L$(top_srcdir)/src/common -lsb_common @PACKAGE_LIBS@
4848

src/platform/emcc/canvas.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ EM_JS(int, get_text_size, (int id, const char *str, const char *face), {
7575
var s = UTF8ToString(str);
7676
var metrics = ctx.measureText(s);
7777
var height = metrics.fontBoundingBoxAscent + metrics.fontBoundingBoxDescent;
78-
var result = (metrics.width << 16) + height;
79-
return result;
78+
var width = metrics.width;
79+
return (width << 16) + height;
8080
});
8181

8282
EM_JS(void, draw_arc, (int id, int xc, int yc, double r, double start, double end, double aspect), {
@@ -159,16 +159,18 @@ strlib::String get_color() {
159159
return result;
160160
}
161161

162-
strlib::String get_face() {
163-
strlib::String result;
164-
if (font->_italic) {
165-
result.append("italic ");
162+
Font::Font(int size, bool bold, bool italic) :
163+
_size(size),
164+
_bold(bold),
165+
_italic(italic) {
166+
_face.empty();
167+
if (italic) {
168+
_face.append("italic ");
166169
}
167-
if (font->_bold) {
168-
result.append("bold ");
170+
if (bold) {
171+
_face.append("bold ");
169172
}
170-
result.append(font->_size).append("px monospace");
171-
return result;
173+
_face.append(size).append("px monospace");
172174
}
173175

174176
Canvas::Canvas() :
@@ -247,7 +249,7 @@ void maSetClipRect(int left, int top, int width, int height) {
247249
MAExtent maGetTextSize(const char *str) {
248250
MAExtent result;
249251
if (str && str[0]) {
250-
result = (MAExtent)get_text_size(drawTarget ? drawTarget->_id : -1, str, get_face());
252+
result = (MAExtent)get_text_size(drawTarget ? drawTarget->_id : -1, str, font->_face.c_str());
251253
} else {
252254
result = 0;
253255
}
@@ -331,7 +333,7 @@ void maFillRect(int left, int top, int width, int height) {
331333

332334
void maDrawText(int left, int top, const char *str, int length) {
333335
if (str && str[0] && drawTarget) {
334-
draw_text(drawTarget->_id, left, top, str, length, get_color(), get_face());
336+
draw_text(drawTarget->_id, left, top, str, length, get_color(), font->_face.c_str());
335337
}
336338
}
337339

src/platform/emcc/runtime.cpp

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "config.h"
1010

1111
#include <emscripten.h>
12-
#include <emscripten/html5.h>
1312
#include "include/osd.h"
1413
#include "common/smbas.h"
1514
#include "lib/maapi.h"
@@ -41,26 +40,12 @@ MAEvent *getKeyPressedEvent(int keycode, int nativeKey) {
4140
}
4241

4342
EM_BOOL mouse_callback(int eventType, const EmscriptenMouseEvent *e, void *userData) {
44-
// printf("%s, screen: (%ld,%ld), client: (%ld,%ld),%s%s%s%s button: %hu, buttons: %hu, movement: (%ld,%ld), target: (%ld, %ld)\n",
45-
// emscripten_event_type_to_string(eventType), e->screenX, e->screenY, e->clientX, e->clientY,
46-
// e->ctrlKey ? " CTRL" : "", e->shiftKey ? " SHIFT" : "", e->altKey ? " ALT" : "", e->metaKey ? " META" : "",
47-
// e->button, e->buttons, e->movementX, e->movementY, e->targetX, e->targetY);
43+
runtime->handleMouse(eventType, e);
44+
return 0;
45+
}
4846

49-
switch (eventType) {
50-
case EMSCRIPTEN_EVENT_DBLCLICK:
51-
case EMSCRIPTEN_EVENT_CLICK:
52-
//
53-
break;
54-
case EMSCRIPTEN_EVENT_MOUSEDOWN:
55-
runtime->pushEvent(getMotionEvent(EVENT_TYPE_POINTER_PRESSED, e));
56-
break;
57-
case EMSCRIPTEN_EVENT_MOUSEMOVE:
58-
runtime->pushEvent(getMotionEvent(EVENT_TYPE_POINTER_DRAGGED, e));
59-
break;
60-
case EMSCRIPTEN_EVENT_MOUSEUP:
61-
runtime->pushEvent(getMotionEvent(EVENT_TYPE_POINTER_RELEASED, e));
62-
break;
63-
}
47+
EM_BOOL key_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) {
48+
runtime->handleKeyboard(eventType, e);
6449
return 0;
6550
}
6651

@@ -69,11 +54,10 @@ Runtime::Runtime() :
6954
logEntered();
7055
runtime = this;
7156

72-
emscripten_set_click_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 1, mouse_callback);
73-
emscripten_set_mousedown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 1, mouse_callback);
74-
emscripten_set_mouseup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 1, mouse_callback);
75-
emscripten_set_dblclick_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 1, mouse_callback);
76-
emscripten_set_mousemove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 1, mouse_callback);
57+
emscripten_set_mousedown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, true, mouse_callback);
58+
emscripten_set_mouseup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, true, mouse_callback);
59+
emscripten_set_mousemove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, true, mouse_callback);
60+
emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, true, key_callback);
7761

7862
MAExtent screenSize = maGetScrSize();
7963
_output = new AnsiWidget(EXTENT_X(screenSize), EXTENT_Y(screenSize));
@@ -87,11 +71,12 @@ Runtime::Runtime() :
8771

8872
Runtime::~Runtime() {
8973
logEntered();
74+
emscripten_html5_remove_all_event_listeners();
9075
delete _output;
9176
delete _eventQueue;
92-
runtime = NULL;
93-
_output = NULL;
94-
_eventQueue = NULL;
77+
runtime = nullptr;
78+
_output = nullptr;
79+
_eventQueue = nullptr;
9580
}
9681

9782
void Runtime::alert(const char *title, const char *message) {
@@ -126,15 +111,40 @@ char *Runtime::loadResource(const char *fileName) {
126111
return buffer;
127112
}
128113

129-
void Runtime::optionsBox(StringList *items) {
114+
void Runtime::handleKeyboard(int eventType, const EmscriptenKeyboardEvent *e) {
115+
trace("eventType: %d [%s %s %s] [%d %d %d] %s%s%s%s ",
116+
eventType,
117+
e->key, e->code, e->charValue,
118+
e->charCode, e->keyCode, e->which,
119+
e->ctrlKey ? " CTRL" : "", e->shiftKey ? " SHIFT" : "", e->altKey ? " ALT" : "", e->metaKey ? " META" : "");
130120

131121
}
132122

123+
void Runtime::handleMouse(int eventType, const EmscriptenMouseEvent *e) {
124+
switch (eventType) {
125+
case EMSCRIPTEN_EVENT_MOUSEDOWN:
126+
if (e->button == 2) {
127+
_menuX = e->clientX;
128+
_menuY = e->clientY;
129+
showMenu();
130+
} else {
131+
pushEvent(getMotionEvent(EVENT_TYPE_POINTER_PRESSED, e));
132+
}
133+
break;
134+
case EMSCRIPTEN_EVENT_MOUSEMOVE:
135+
pushEvent(getMotionEvent(EVENT_TYPE_POINTER_DRAGGED, e));
136+
break;
137+
case EMSCRIPTEN_EVENT_MOUSEUP:
138+
pushEvent(getMotionEvent(EVENT_TYPE_POINTER_RELEASED, e));
139+
break;
140+
}
141+
}
142+
133143
void Runtime::pause(int timeout) {
134144
if (timeout == -1) {
135145
if (hasEvent()) {
136146
MAEvent *event = popEvent();
137-
processEvent(*event);
147+
handleEvent(*event);
138148
delete event;
139149
}
140150
} else {
@@ -144,7 +154,7 @@ void Runtime::pause(int timeout) {
144154
break;
145155
} else if (hasEvent()) {
146156
MAEvent *event = popEvent();
147-
processEvent(*event);
157+
handleEvent(*event);
148158
delete event;
149159
}
150160
emscripten_sleep(WAIT_INTERVAL);
@@ -157,17 +167,6 @@ void Runtime::pause(int timeout) {
157167

158168
}
159169

160-
void Runtime::processEvent(MAEvent &event) {
161-
switch (event.type) {
162-
case EVENT_TYPE_KEY_PRESSED:
163-
//handleKeyEvent(event);
164-
break;
165-
default:
166-
handleEvent(event);
167-
break;
168-
}
169-
}
170-
171170
MAEvent Runtime::processEvents(int waitFlag) {
172171
switch (waitFlag) {
173172
case 1:
@@ -188,7 +187,7 @@ MAEvent Runtime::processEvents(int waitFlag) {
188187
MAEvent event;
189188
if (hasEvent()) {
190189
MAEvent *nextEvent = popEvent();
191-
processEvent(*nextEvent);
190+
handleEvent(*nextEvent);
192191
event = *nextEvent;
193192
delete nextEvent;
194193
} else {
@@ -237,6 +236,10 @@ int maGetEvent(MAEvent *event) {
237236
return result;
238237
}
239238

239+
void maPushEvent(MAEvent *maEvent) {
240+
runtime->pushEvent(maEvent);
241+
}
242+
240243
void maWait(int timeout) {
241244
runtime->pause(timeout);
242245
}

src/platform/emcc/runtime.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#pragma once
1010

1111
#include "ui/system.h"
12+
#include <emscripten/html5.h>
1213

1314
struct Runtime : public System {
1415
Runtime();
@@ -23,15 +24,15 @@ struct Runtime : public System {
2324
void enableCursor(bool enabled) {}
2425
int handle(int event);
2526
char *loadResource(const char *fileName);
26-
void optionsBox(StringList *items);
2727
void onRunCompleted() {}
2828
void saveWindowRect() {}
29+
void handleKeyboard(int eventType, const EmscriptenKeyboardEvent *e);
30+
void handleMouse(int eventType, const EmscriptenMouseEvent *e);
2931
bool hasEvent() { return _eventQueue && _eventQueue->size() > 0; }
3032
void pause(int timeout);
3133
void pushEvent(MAEvent *event) { _eventQueue->push(event); }
3234
MAEvent *popEvent() { return _eventQueue->pop(); }
3335
MAEvent processEvents(int waitFlag);
34-
void processEvent(MAEvent &event);
3536
bool run(const char *bas) { return execute(bas); }
3637
void runShell();
3738
void resize(int w, int h);

src/platform/fltk/runtime.cxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ int maGetEvent(MAEvent *event) {
311311
return result;
312312
}
313313

314+
void maPushEvent(MAEvent *maEvent) {
315+
// not implemented
316+
}
317+
314318
void maWait(int timeout) {
315319
if (timeout == -1) {
316320
Fl::wait();

0 commit comments

Comments
 (0)