Skip to content

Commit

Permalink
SDL2 Pthread Proxy APIs (#9336)
Browse files Browse the repository at this point in the history
This PR implements various C APIs for SDL2 to work with PROXY_TO_PTHREAD=1 as discussed here: emscripten-ports/SDL2#77

* create C API for setting window title

* create C API for getting window title

* tests: test_html5: test emscripten_{get,set}window_title

* create C API for getting screen size

* tests: test_html5: test emscripten_get_screen_size
  • Loading branch information
jakogut authored Aug 3, 2020
1 parent f00f197 commit 8b7f834
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
14 changes: 14 additions & 0 deletions site/source/docs/api_reference/emscripten.h.rst
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,20 @@ Functions
:rtype: double
:return: The pixel ratio or 1.0 if not supported.
.. c:function:: char *emscripten_get_window_title()
Returns the window title.
The returned string will be valid until the next call of the function
.. c:function:: void emscripten_set_window_title(char *title)
Sets the window title.
.. c:function:: void emscripten_get_screen_size(int *width, int *height)
Returns the width and height of the screen.
.. c:function:: void emscripten_hide_mouse(void)
Hide the OS mouse cursor over the canvas.
Expand Down
30 changes: 30 additions & 0 deletions src/library_browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,36 @@ var LibraryBrowser = {
exit(status);
},

emscripten_get_window_title__proxy: 'sync',
emscripten_get_window_title__sig: 'iv',
emscripten_get_window_title: function() {
var buflen = 256;

if (!_emscripten_get_window_title.buffer) {
_emscripten_get_window_title.buffer = _malloc(buflen);
}

writeAsciiToMemory(
document.title.slice(0, buflen - 1),
_emscripten_get_window_title.buffer
);

return _emscripten_get_window_title.buffer;
},

emscripten_set_window_title__proxy: 'sync',
emscripten_set_window_title__sig: 'vi',
emscripten_set_window_title: function(title) {
setWindowTitle(AsciiToString(title));
},

emscripten_get_screen_size__proxy: 'sync',
emscripten_get_screen_size__sig: 'vii',
emscripten_get_screen_size: function(width, height) {
{{{ makeSetValue('width', '0', 'screen.width', 'i32') }}};
{{{ makeSetValue('height', '0', 'screen.height', 'i32') }}};
},

emscripten_hide_mouse__proxy: 'sync',
emscripten_hide_mouse__sig: 'v',
emscripten_hide_mouse: function() {
Expand Down
3 changes: 3 additions & 0 deletions system/include/emscripten/emscripten.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ extern void emscripten_force_exit(int status);

double emscripten_get_device_pixel_ratio(void);

char *emscripten_get_window_title();
void emscripten_set_window_title(const char *);
void emscripten_get_screen_size(int *width, int *height);
void emscripten_hide_mouse(void);
void emscripten_set_canvas_size(int width, int height) __attribute__((deprecated("This variant does not allow specifying the target canvas", "Use emscripten_set_canvas_element_size() instead")));
void emscripten_get_canvas_size(int *width, int *height, int *isFullscreen) __attribute__((deprecated("This variant does not allow specifying the target canvas", "Use emscripten_get_canvas_element_size() and emscripten_get_fullscreen_status() instead")));
Expand Down
14 changes: 13 additions & 1 deletion tests/test_html5.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,19 @@ int main()
ret = emscripten_set_webglcontextrestored_callback("#canvas", 0, 1, webglcontext_callback);
TEST_RESULT(emscripten_set_webglcontextrestored_callback);

/* For the events to function, one must either call emscripten_set_main_loop or enable Module.noExitRuntime by some other means.
char *source_window_title = "test window title";
emscripten_set_window_title(source_window_title);
char *current_window_title = emscripten_get_window_title();
ret = (strcmp(source_window_title, current_window_title) == 0 \
? EMSCRIPTEN_RESULT_SUCCESS : EMSCRIPTEN_RESULT_FAILED);
TEST_RESULT(emscripten_get_window_title);

int width, height;
emscripten_get_screen_size(&width, &height);
ret = (width && height) ? EMSCRIPTEN_RESULT_SUCCESS : EMSCRIPTEN_RESULT_FAILED;
TEST_RESULT(emscripten_get_screen_size);

/* For the events to function, one must either call emscripten_set_main_loop or enable Module.noExitRuntime by some other means.
Otherwise the application will exit after leaving main(), and the atexit handlers will clean up all event hooks (by design). */
EM_ASM(noExitRuntime = true);

Expand Down

0 comments on commit 8b7f834

Please sign in to comment.