Skip to content

Commit 348479b

Browse files
committed
Update
Ensure SDL_FinishWindowCreation is still calle on Emscripten Ensure drop paths are unique for each window Use videodata instead of vdata Remove unnecessary call to SDL_ShowWindow in testmouse
1 parent 352b550 commit 348479b

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed

src/video/SDL_video.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,6 +2578,14 @@ bool SDL_RecreateWindow(SDL_Window *window, SDL_WindowFlags flags)
25782578
bool need_vulkan_load = false;
25792579
SDL_WindowFlags graphics_flags;
25802580

2581+
#ifdef SDL_PLATFORM_EMSCRIPTEN
2582+
// Don't actually recreate the window in Emscripten because the SDL_WindowData's
2583+
// canvas_id and keyboard_element would be lost if destroyed
2584+
const bool recreate_window = false;
2585+
#else
2586+
const bool recreate_window = true;
2587+
#endif
2588+
25812589
// ensure no more than one of these flags is set
25822590
graphics_flags = flags & (SDL_WINDOW_OPENGL | SDL_WINDOW_METAL | SDL_WINDOW_VULKAN);
25832591
if (graphics_flags & (graphics_flags - 1)) {
@@ -2646,13 +2654,9 @@ bool SDL_RecreateWindow(SDL_Window *window, SDL_WindowFlags flags)
26462654
SDL_Vulkan_UnloadLibrary();
26472655
}
26482656

2649-
// Don't actually recreate the window in Emscripten because the SDL_WindowData's
2650-
// canvas_id and keyboard_element would be lost if destroyed
2651-
#ifndef SDL_PLATFORM_EMSCRIPTEN
2652-
if (_this->DestroyWindow && !(flags & SDL_WINDOW_EXTERNAL)) {
2657+
if (recreate_window && _this->DestroyWindow && !(flags & SDL_WINDOW_EXTERNAL)) {
26532658
_this->DestroyWindow(_this, window);
26542659
}
2655-
#endif
26562660

26572661
if (need_gl_load) {
26582662
if (!SDL_GL_LoadLibrary(NULL)) {
@@ -2680,12 +2684,7 @@ bool SDL_RecreateWindow(SDL_Window *window, SDL_WindowFlags flags)
26802684
window->w = window->windowed.w = window->floating.w;
26812685
window->h = window->windowed.h = window->floating.h;
26822686

2683-
#ifdef SDL_PLATFORM_EMSCRIPTEN
2684-
// Prevent -Wunused-but-set-variable
2685-
(void)loaded_opengl;
2686-
(void)loaded_vulkan;
2687-
#else
2688-
if (!_this->CreateSDLWindow(_this, window, 0)) {
2687+
if (recreate_window && !_this->CreateSDLWindow(_this, window, 0)) {
26892688
if (loaded_opengl) {
26902689
SDL_GL_UnloadLibrary();
26912690
window->flags &= ~SDL_WINDOW_OPENGL;
@@ -2696,7 +2695,6 @@ bool SDL_RecreateWindow(SDL_Window *window, SDL_WindowFlags flags)
26962695
}
26972696
return false;
26982697
}
2699-
#endif
27002698
}
27012699

27022700
if (flags & SDL_WINDOW_EXTERNAL) {
@@ -2727,9 +2725,7 @@ bool SDL_RecreateWindow(SDL_Window *window, SDL_WindowFlags flags)
27272725
_this->SetWindowHitTest(window, true);
27282726
}
27292727

2730-
#ifndef SDL_PLATFORM_EMSCRIPTEN
27312728
SDL_FinishWindowCreation(window, flags);
2732-
#endif
27332729

27342730
return true;
27352731
}

src/video/emscripten/SDL_emscriptenevents.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -954,14 +954,18 @@ static void Emscripten_set_drag_event_callbacks(SDL_WindowData *data)
954954
target.addEventListener("dragover", window_data.eventHandlerDropDragover);
955955

956956
window_data.drop_count = 0;
957-
try
958-
{
959-
FS.mkdir("/tmp/filedrop")
960-
}
961-
catch(e)
962-
{
963-
// Throws if the directory already exists
957+
function safeCreateDir(dir){
958+
try
959+
{
960+
FS.mkdir(dir)
961+
}
962+
catch(e)
963+
{
964+
// Throws if the directory already exists
965+
}
964966
}
967+
safeCreateDir("/tmp/filedrop");
968+
safeCreateDir(`/tmp/filedrop/${id}/`);
965969

966970
window_data.eventHandlerDropDrop = function(event) {
967971
event.preventDefault();
@@ -975,7 +979,7 @@ static void Emscripten_set_drag_event_callbacks(SDL_WindowData *data)
975979
const file_reader = new FileReader();
976980
file_reader.readAsArrayBuffer(file);
977981
file_reader.onload = function(event) {
978-
const fs_dropdir = `/tmp/filedrop/${window_data.drop_count}`;
982+
const fs_dropdir = `/tmp/filedrop/${id}/${window_data.drop_count}`;
979983
window_data.drop_count += 1;
980984

981985
const fs_filepath = `${fs_dropdir}/${file.name}`;
@@ -1032,7 +1036,7 @@ static void Emscripten_unset_drag_event_callbacks(SDL_WindowData *data)
10321036
}
10331037
}
10341038

1035-
const path = "/tmp/filedrop";
1039+
const path = `/tmp/filedrop/${id}/`;
10361040
function recursive_remove(dirpath) {
10371041
FS.readdir(dirpath).forEach((filename) => {
10381042
const p = `${dirpath}/${filename}`;

src/video/emscripten/SDL_emscriptenvideo.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ static int pending_swap_interval = -1;
6060

6161
static void Emscripten_DeleteDevice(SDL_VideoDevice *device)
6262
{
63-
SDL_VideoData *vdata = device->internal;
64-
SDL_DestroyProperties(vdata->window_map);
63+
SDL_VideoData *videodata = device->internal;
64+
SDL_DestroyProperties(videodata->window_map);
6565
SDL_free(device);
6666
}
6767

@@ -147,12 +147,12 @@ static SDL_VideoDevice *Emscripten_CreateDevice(void)
147147
return NULL;
148148
}
149149

150-
vdata = (SDL_VideoData *)SDL_calloc(1, sizeof(SDL_VideoData));
151-
if (!vdata) {
150+
videodata = (SDL_VideoData *)SDL_calloc(1, sizeof(SDL_VideoData));
151+
if (!videodata) {
152152
return NULL;
153153
}
154-
vdata->window_map = SDL_CreateProperties();
155-
device->internal = vdata;
154+
videodata->window_map = SDL_CreateProperties();
155+
device->internal = videodata;
156156

157157
/* Firefox sends blur event which would otherwise prevent full screen
158158
* when the user clicks to allow full screen.
@@ -468,7 +468,7 @@ EMSCRIPTEN_KEEPALIVE void requestFullscreenThroughSDL(SDL_Window *window)
468468
static bool Emscripten_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID props)
469469
{
470470
SDL_WindowData *wdata;
471-
SDL_VideoData *vdata;
471+
SDL_VideoData *videodata;
472472
double scaled_w, scaled_h;
473473
double css_w, css_h;
474474
const char *selector;
@@ -485,6 +485,11 @@ static bool Emscripten_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window,
485485
selector = SDL_GetStringProperty(props, SDL_PROP_WINDOW_CREATE_EMSCRIPTEN_CANVAS_ID, "#canvas");
486486
}
487487

488+
if (!*selector || selector[0] != '#') {
489+
SDL_SetError("Canvas ID must begin with a '#' character.");
490+
return false;
491+
}
492+
488493
// If an element with this id already exists, it should be verified that it is to an HTML5CanvasElement
489494
canvas_exists = MAIN_THREAD_EM_ASM_INT({
490495
var id = UTF8ToString($0);
@@ -577,8 +582,8 @@ static bool Emscripten_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window,
577582
}
578583

579584
// Ensure that there isn't another window that is using this canvas ID
580-
vdata = _this->internal;
581-
if (SDL_GetBooleanProperty(vdata->window_map, selector, false)) {
585+
videodata = _this->internal;
586+
if (SDL_GetBooleanProperty(videodata->window_map, selector, false)) {
582587
SDL_SetError("A window already exists that refers to canvas '%s'", selector);
583588
return false;
584589
}
@@ -645,10 +650,14 @@ static bool Emscripten_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window,
645650
SDL_SetStringProperty(window->props, SDL_PROP_WINDOW_EMSCRIPTEN_CANVAS_ID_STRING, wdata->canvas_id);
646651
SDL_SetStringProperty(window->props, SDL_PROP_WINDOW_EMSCRIPTEN_KEYBOARD_ELEMENT_STRING, wdata->keyboard_element);
647652

648-
if (!SDL_SetBooleanProperty(vdata->window_map, wdata->canvas_id, true)) {
653+
if (!SDL_SetBooleanProperty(videodata->window_map, wdata->canvas_id, true)) {
649654
return false;
650655
}
651656

657+
if (window->flags & SDL_WINDOW_HIDDEN) {
658+
Emscripten_HideWindow(_this, window);
659+
}
660+
652661
// Window has been successfully created
653662
return true;
654663
}
@@ -687,7 +696,7 @@ static void Emscripten_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window
687696
static void Emscripten_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
688697
{
689698
SDL_WindowData *data;
690-
SDL_VideoData *vdata;
699+
SDL_VideoData *videodata;
691700

692701
if (window->internal) {
693702
data = window->internal;
@@ -709,8 +718,8 @@ static void Emscripten_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
709718
}
710719
}, data->canvas_id);
711720

712-
vdata = _this->internal;
713-
SDL_SetBooleanProperty(vdata->window_map, data->canvas_id, false);
721+
videodata = _this->internal;
722+
SDL_SetBooleanProperty(videodata->window_map, data->canvas_id, false);
714723
SDL_free(data->canvas_id);
715724

716725
SDL_free(data->keyboard_element);

test/testmouse.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,6 @@ int main(int argc, char *argv[])
332332
return 0;
333333
}
334334

335-
/* Window won't show on Emscripten without this */
336-
SDL_ShowWindow(window);
337-
338335
/* Main render loop */
339336
#ifdef SDL_PLATFORM_EMSCRIPTEN
340337
emscripten_set_main_loop_arg(loop, &loop_data, 0, 1);

0 commit comments

Comments
 (0)