Skip to content

Commit 8b7f834

Browse files
authored
SDL2 Pthread Proxy APIs (#9336)
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
1 parent f00f197 commit 8b7f834

4 files changed

Lines changed: 60 additions & 1 deletion

File tree

site/source/docs/api_reference/emscripten.h.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,20 @@ Functions
492492
:rtype: double
493493
:return: The pixel ratio or 1.0 if not supported.
494494
495+
.. c:function:: char *emscripten_get_window_title()
496+
497+
Returns the window title.
498+
499+
The returned string will be valid until the next call of the function
500+
501+
.. c:function:: void emscripten_set_window_title(char *title)
502+
503+
Sets the window title.
504+
505+
.. c:function:: void emscripten_get_screen_size(int *width, int *height)
506+
507+
Returns the width and height of the screen.
508+
495509
.. c:function:: void emscripten_hide_mouse(void)
496510
497511
Hide the OS mouse cursor over the canvas.

src/library_browser.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,36 @@ var LibraryBrowser = {
13811381
exit(status);
13821382
},
13831383

1384+
emscripten_get_window_title__proxy: 'sync',
1385+
emscripten_get_window_title__sig: 'iv',
1386+
emscripten_get_window_title: function() {
1387+
var buflen = 256;
1388+
1389+
if (!_emscripten_get_window_title.buffer) {
1390+
_emscripten_get_window_title.buffer = _malloc(buflen);
1391+
}
1392+
1393+
writeAsciiToMemory(
1394+
document.title.slice(0, buflen - 1),
1395+
_emscripten_get_window_title.buffer
1396+
);
1397+
1398+
return _emscripten_get_window_title.buffer;
1399+
},
1400+
1401+
emscripten_set_window_title__proxy: 'sync',
1402+
emscripten_set_window_title__sig: 'vi',
1403+
emscripten_set_window_title: function(title) {
1404+
setWindowTitle(AsciiToString(title));
1405+
},
1406+
1407+
emscripten_get_screen_size__proxy: 'sync',
1408+
emscripten_get_screen_size__sig: 'vii',
1409+
emscripten_get_screen_size: function(width, height) {
1410+
{{{ makeSetValue('width', '0', 'screen.width', 'i32') }}};
1411+
{{{ makeSetValue('height', '0', 'screen.height', 'i32') }}};
1412+
},
1413+
13841414
emscripten_hide_mouse__proxy: 'sync',
13851415
emscripten_hide_mouse__sig: 'v',
13861416
emscripten_hide_mouse: function() {

system/include/emscripten/emscripten.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ extern void emscripten_force_exit(int status);
134134

135135
double emscripten_get_device_pixel_ratio(void);
136136

137+
char *emscripten_get_window_title();
138+
void emscripten_set_window_title(const char *);
139+
void emscripten_get_screen_size(int *width, int *height);
137140
void emscripten_hide_mouse(void);
138141
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")));
139142
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")));

tests/test_html5.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,19 @@ int main()
413413
ret = emscripten_set_webglcontextrestored_callback("#canvas", 0, 1, webglcontext_callback);
414414
TEST_RESULT(emscripten_set_webglcontextrestored_callback);
415415

416-
/* For the events to function, one must either call emscripten_set_main_loop or enable Module.noExitRuntime by some other means.
416+
char *source_window_title = "test window title";
417+
emscripten_set_window_title(source_window_title);
418+
char *current_window_title = emscripten_get_window_title();
419+
ret = (strcmp(source_window_title, current_window_title) == 0 \
420+
? EMSCRIPTEN_RESULT_SUCCESS : EMSCRIPTEN_RESULT_FAILED);
421+
TEST_RESULT(emscripten_get_window_title);
422+
423+
int width, height;
424+
emscripten_get_screen_size(&width, &height);
425+
ret = (width && height) ? EMSCRIPTEN_RESULT_SUCCESS : EMSCRIPTEN_RESULT_FAILED;
426+
TEST_RESULT(emscripten_get_screen_size);
427+
428+
/* For the events to function, one must either call emscripten_set_main_loop or enable Module.noExitRuntime by some other means.
417429
Otherwise the application will exit after leaving main(), and the atexit handlers will clean up all event hooks (by design). */
418430
EM_ASM(noExitRuntime = true);
419431

0 commit comments

Comments
 (0)