Wasm again

97 views
Skip to first unread message

Mo_Al_

unread,
Dec 11, 2020, 6:08:35 PM12/11/20
to fltk.coredev
Hello
I was able to get the PicoSDL driver to target wasm:

The changes I had to do are attached in the patch. Some are quite intrusive, like adding the Fl_PicoSDL_Window_Driver as a friend class to Fl_Window.

Keyboards events aren't handled, it's missing the mapping between SDL and FLTK keys.

To build wasm fltk app:
- Install emscripten
- cd fltk
- git apply /path/to/wasm.patch
- emcmake cmake -Bbin -DFLTK_BUILD_TEST=OFF && cmake --build bin --parallel
- cp bin/libs/libfltk* $EMSDK/upstream/emscripten/cache/wasm/
- mkdir $EMSDK/upstream/emscripten/system/include/FL
- cp bin/FL/* $EMSDK/upstream/emscripten/system/include/FL/
- cp FL/* $EMSDK/upstream/emscripten/system/include/FL/

Now you can build with em++ or emcmake whatever.
The emscripten_set_main_loop_arg() needs to be called. The sample program is something like:
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Box.H>
#include <emscripten.h>

void main_loop(void *) {
    Fl::wait();
}

void cb(Fl_Widget *w, void *data) {
    ((Fl_Box*)data)->color(FL_RED);
    Fl::redraw();
}

int main() {
    auto win = new Fl_Window(600, 400);
    auto box = new Fl_Box(40, 40, 520, 220, "Hello from FLTK 1.4");
    box->box(FL_SHADOW_BOX);
    box->labelsize(16);
    auto but = new Fl_Button(260, 300, 80, 40, "Click me");
    win->end();
    win->show();
    but->callback(cb, box);
    emscripten_set_main_loop_arg(main_loop, NULL, 0, true);
}

Using em++, this can be built:
em++ -o fltk.html main.cpp -s WASM=1 -s USE_SDL=2 -s USE_FREETYPE=1 -lfltk --shell-file shell_minimal.html

wasm.patch

Mo_Al_

unread,
Dec 11, 2020, 6:42:27 PM12/11/20
to fltk.coredev
updated patch
The previous didn't track the Fl_PicoSDL_Image_Surface_Driver.cxx
The generated .html file can be run using:
emrun fltk.html
Or through any server application.
Using cmake, you would have to pass " -s WASM=1 -s USE_SDL=2 -s USE_FREETYPE=1" as CMAKE_CXX_FLAGS and  "--shell-file shell_minimal.html" as CMAKE_EXE_LINKER_FLAGS. Then:
target_link_libraries(myapp PRIVATE fltk)
set_target_properties(myapp PROPERTIES SUFFIX .html)
wasm.patch

Daniel G.

unread,
Dec 23, 2020, 7:58:43 PM12/23/20
to fltk.coredev
Hi,

did you try this aswell: https://github.com/ghaerr/microwindows
it can be used as drop in replacement for libX11 and understands many backends.
I used this for android some time ago, but there seems to be webasm support aswell.

BR
Giri

Mo_Al_

unread,
Dec 24, 2020, 8:13:09 AM12/24/20
to fltk.coredev
Hi
I was able to use microwindows (libNX11 and libnano-X) with older versions of fltk (pre 1.34), however with 1.4 the JS runtime throws an exception on some X11 stubs and function signature mismatch. 
This also needed some changes in the fltk/src/CMakeLists.txt (make passing the X11 driver sources the default instead of win32). Also changing the -lX11 in the options.cmake LDFLAGS to -lNX11.
Checking the faq in the microwindows repo, it says currently fltk doesn't run using emscripten.

Daniel G.

unread,
Jan 2, 2021, 2:56:01 PM1/2/21
to fltk.coredev
When I did my experiments i was using fltk-1.3.4-1 and fltk-1.3.4. Make sure to #undef XRANDR as described in the article shared by Ian MacArthur.

I am not sure if that FAQ is a trustworthy source of information. When I tried the android stuff it was horribly out of date (and it has not changed much since then).

Daniel G.

unread,
Feb 2, 2023, 2:50:03 PM2/2/23
to fltk.coredev
Hi,

FYI I just managed to get wasm to work with microwindows:  Adjust the signatures of some stubs by nwrkbiz · Pull Request #72 · ghaerr/microwindows (github.com)

What is the source version of your patch? May it be applied to the 1.3.8. version?

BR,
Giri

may64...@gmail.com schrieb am Donnerstag, 24. Dezember 2020 um 14:13:09 UTC+1:

melcher....@googlemail.com

unread,
Feb 2, 2023, 2:51:31 PM2/2/23
to fltk.coredev
This is very cool! I wish I wasn't so busy with core and fluid so I could get Pico to run again... . 

Mo_Al_

unread,
Feb 3, 2023, 7:14:46 AM2/3/23
to fltk.coredev
Hi
At the time it was done on 1.4, when the experimental drivers were still there. So that would make it revision: https://github.com/fltk/fltk/commit/b275ff07158e80d1744ddb2f6c51094a87cf079a
Reply all
Reply to author
Forward
0 new messages