Emscripten: Store canvas id in WindowData Also replace all hardcoded uses of "#canvas" or NULL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
diff --git a/src/video/emscripten/SDL_emscriptenevents.c b/src/video/emscripten/SDL_emscriptenevents.c
index ca13e10..5d5f88f 100644
--- a/src/video/emscripten/SDL_emscriptenevents.c
+++ b/src/video/emscripten/SDL_emscriptenevents.c
@@ -317,7 +317,7 @@ Emscripten_HandleMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent
/* rescale (in case canvas is being scaled)*/
double client_w, client_h, xscale, yscale;
- emscripten_get_element_css_size(NULL, &client_w, &client_h);
+ emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
xscale = window_data->window->w / client_w;
yscale = window_data->window->h / client_h;
@@ -374,7 +374,7 @@ Emscripten_HandleMouseButton(int eventType, const EmscriptenMouseEvent *mouseEve
SDL_SendMouseButton(window_data->window, 0, sdl_button_state, sdl_button);
/* Do not consume the event if the mouse is outside of the canvas. */
- emscripten_get_element_css_size(NULL, &css_w, &css_h);
+ emscripten_get_element_css_size(window_data->canvas_id, &css_w, &css_h);
if (mouseEvent->canvasX < 0 || mouseEvent->canvasX >= css_w ||
mouseEvent->canvasY < 0 || mouseEvent->canvasY >= css_h) {
return 0;
@@ -394,7 +394,7 @@ Emscripten_HandleMouseFocus(int eventType, const EmscriptenMouseEvent *mouseEven
if (!isPointerLocked) {
/* rescale (in case canvas is being scaled)*/
double client_w, client_h;
- emscripten_get_element_css_size(NULL, &client_w, &client_h);
+ emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
mx = mx * (window_data->window->w / client_w);
my = my * (window_data->window->h / client_h);
@@ -440,7 +440,7 @@ Emscripten_HandleTouch(int eventType, const EmscriptenTouchEvent *touchEvent, vo
return 0;
}
- emscripten_get_element_css_size(NULL, &client_w, &client_h);
+ emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
for (i = 0; i < touchEvent->numTouches; i++) {
SDL_FingerID id;
@@ -587,14 +587,14 @@ Emscripten_HandleResize(int eventType, const EmscriptenUiEvent *uiEvent, void *u
double h = window_data->window->h;
if(window_data->external_size) {
- emscripten_get_element_css_size(NULL, &w, &h);
+ emscripten_get_element_css_size(window_data->canvas_id, &w, &h);
}
- emscripten_set_canvas_element_size(NULL, w * window_data->pixel_ratio, h * window_data->pixel_ratio);
+ emscripten_set_canvas_element_size(window_data->canvas_id, w * window_data->pixel_ratio, h * window_data->pixel_ratio);
/* set_canvas_size unsets this */
if (!window_data->external_size && window_data->pixel_ratio != 1.0f) {
- emscripten_set_element_css_size(NULL, w, h);
+ emscripten_set_element_css_size(window_data->canvas_id, w, h);
}
if (force) {
@@ -619,7 +619,7 @@ Emscripten_HandleCanvasResize(int eventType, const void *reserved, void *userDat
if(window_data->fullscreen_resize)
{
double css_w, css_h;
- emscripten_get_element_css_size(NULL, &css_w, &css_h);
+ emscripten_get_element_css_size(window_data->canvas_id, &css_w, &css_h);
SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
}
@@ -640,23 +640,23 @@ Emscripten_RegisterEventHandlers(SDL_WindowData *data)
const char *keyElement;
/* There is only one window and that window is the canvas */
- emscripten_set_mousemove_callback("#canvas", data, 0, Emscripten_HandleMouseMove);
+ emscripten_set_mousemove_callback(data->canvas_id, data, 0, Emscripten_HandleMouseMove);
- emscripten_set_mousedown_callback("#canvas", data, 0, Emscripten_HandleMouseButton);
+ emscripten_set_mousedown_callback(data->canvas_id, data, 0, Emscripten_HandleMouseButton);
emscripten_set_mouseup_callback("#document", data, 0, Emscripten_HandleMouseButton);
- emscripten_set_mouseenter_callback("#canvas", data, 0, Emscripten_HandleMouseFocus);
- emscripten_set_mouseleave_callback("#canvas", data, 0, Emscripten_HandleMouseFocus);
+ emscripten_set_mouseenter_callback(data->canvas_id, data, 0, Emscripten_HandleMouseFocus);
+ emscripten_set_mouseleave_callback(data->canvas_id, data, 0, Emscripten_HandleMouseFocus);
- emscripten_set_wheel_callback("#canvas", data, 0, Emscripten_HandleWheel);
+ emscripten_set_wheel_callback(data->canvas_id, data, 0, Emscripten_HandleWheel);
emscripten_set_focus_callback("#window", data, 0, Emscripten_HandleFocus);
emscripten_set_blur_callback("#window", data, 0, Emscripten_HandleFocus);
- emscripten_set_touchstart_callback("#canvas", data, 0, Emscripten_HandleTouch);
- emscripten_set_touchend_callback("#canvas", data, 0, Emscripten_HandleTouch);
- emscripten_set_touchmove_callback("#canvas", data, 0, Emscripten_HandleTouch);
- emscripten_set_touchcancel_callback("#canvas", data, 0, Emscripten_HandleTouch);
+ emscripten_set_touchstart_callback(data->canvas_id, data, 0, Emscripten_HandleTouch);
+ emscripten_set_touchend_callback(data->canvas_id, data, 0, Emscripten_HandleTouch);
+ emscripten_set_touchmove_callback(data->canvas_id, data, 0, Emscripten_HandleTouch);
+ emscripten_set_touchcancel_callback(data->canvas_id, data, 0, Emscripten_HandleTouch);
emscripten_set_pointerlockchange_callback("#document", data, 0, Emscripten_HandlePointerLockChange);
@@ -681,23 +681,23 @@ Emscripten_UnregisterEventHandlers(SDL_WindowData *data)
const char *target;
/* only works due to having one window */
- emscripten_set_mousemove_callback("#canvas", NULL, 0, NULL);
+ emscripten_set_mousemove_callback(data->canvas_id, NULL, 0, NULL);
- emscripten_set_mousedown_callback("#canvas", NULL, 0, NULL);
+ emscripten_set_mousedown_callback(data->canvas_id, NULL, 0, NULL);
emscripten_set_mouseup_callback("#document", NULL, 0, NULL);
- emscripten_set_mouseenter_callback("#canvas", NULL, 0, NULL);
- emscripten_set_mouseleave_callback("#canvas", NULL, 0, NULL);
+ emscripten_set_mouseenter_callback(data->canvas_id, NULL, 0, NULL);
+ emscripten_set_mouseleave_callback(data->canvas_id, NULL, 0, NULL);
- emscripten_set_wheel_callback("#canvas", NULL, 0, NULL);
+ emscripten_set_wheel_callback(data->canvas_id, NULL, 0, NULL);
emscripten_set_focus_callback("#window", NULL, 0, NULL);
emscripten_set_blur_callback("#window", NULL, 0, NULL);
- emscripten_set_touchstart_callback("#canvas", NULL, 0, NULL);
- emscripten_set_touchend_callback("#canvas", NULL, 0, NULL);
- emscripten_set_touchmove_callback("#canvas", NULL, 0, NULL);
- emscripten_set_touchcancel_callback("#canvas", NULL, 0, NULL);
+ emscripten_set_touchstart_callback(data->canvas_id, NULL, 0, NULL);
+ emscripten_set_touchend_callback(data->canvas_id, NULL, 0, NULL);
+ emscripten_set_touchmove_callback(data->canvas_id, NULL, 0, NULL);
+ emscripten_set_touchcancel_callback(data->canvas_id, NULL, 0, NULL);
emscripten_set_pointerlockchange_callback("#document", NULL, 0, NULL);
diff --git a/src/video/emscripten/SDL_emscriptenvideo.c b/src/video/emscripten/SDL_emscriptenvideo.c
index 5131786..458d67a 100644
--- a/src/video/emscripten/SDL_emscriptenvideo.c
+++ b/src/video/emscripten/SDL_emscriptenvideo.c
@@ -196,6 +196,8 @@ Emscripten_CreateWindow(_THIS, SDL_Window * window)
return SDL_OutOfMemory();
}
+ wdata->canvas_id = SDL_strdup("#canvas");
+
if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
wdata->pixel_ratio = emscripten_get_device_pixel_ratio();
} else {
@@ -206,8 +208,8 @@ Emscripten_CreateWindow(_THIS, SDL_Window * window)
scaled_h = SDL_floor(window->h * wdata->pixel_ratio);
/* set a fake size to check if there is any CSS sizing the canvas */
- emscripten_set_canvas_element_size(NULL, 1, 1);
- emscripten_get_element_css_size(NULL, &css_w, &css_h);
+ emscripten_set_canvas_element_size(wdata->canvas_id, 1, 1);
+ emscripten_get_element_css_size(wdata->canvas_id, &css_w, &css_h);
wdata->external_size = SDL_floor(css_w) != 1 || SDL_floor(css_h) != 1;
@@ -218,14 +220,13 @@ Emscripten_CreateWindow(_THIS, SDL_Window * window)
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
}
-
- emscripten_set_canvas_element_size(NULL, scaled_w, scaled_h);
+ emscripten_set_canvas_element_size(wdata->canvas_id, scaled_w, scaled_h);
/* if the size is not being controlled by css, we need to scale down for hidpi */
if (!wdata->external_size) {
if (wdata->pixel_ratio != 1.0f) {
/*scale canvas down*/
- emscripten_set_element_css_size(NULL, window->w, window->h);
+ emscripten_set_element_css_size(wdata->canvas_id, window->w, window->h);
}
}
@@ -269,11 +270,11 @@ static void Emscripten_SetWindowSize(_THIS, SDL_Window * window)
if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
data->pixel_ratio = emscripten_get_device_pixel_ratio();
}
- emscripten_set_canvas_element_size(NULL, window->w * data->pixel_ratio, window->h * data->pixel_ratio);
+ emscripten_set_canvas_element_size(data->canvas_id, window->w * data->pixel_ratio, window->h * data->pixel_ratio);
/*scale canvas down*/
if (!data->external_size && data->pixel_ratio != 1.0f) {
- emscripten_set_element_css_size(NULL, window->w, window->h);
+ emscripten_set_element_css_size(data->canvas_id, window->w, window->h);
}
}
}
@@ -295,7 +296,8 @@ Emscripten_DestroyWindow(_THIS, SDL_Window * window)
#endif
/* We can't destroy the canvas, so resize it to zero instead */
- emscripten_set_canvas_element_size(NULL, 0, 0);
+ emscripten_set_canvas_element_size(data->canvas_id, 0, 0);
+ SDL_free(data->canvas_id);
SDL_free(window->driverdata);
window->driverdata = NULL;
@@ -332,7 +334,7 @@ Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * di
data->requested_fullscreen_mode = window->flags & (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
data->fullscreen_resize = is_desktop_fullscreen;
- res = emscripten_request_fullscreen_strategy(NULL, 1, &strategy);
+ res = emscripten_request_fullscreen_strategy(data->canvas_id, 1, &strategy);
if(res != EMSCRIPTEN_RESULT_SUCCESS && res != EMSCRIPTEN_RESULT_DEFERRED) {
/* unset flags, fullscreen failed */
window->flags &= ~(SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
diff --git a/src/video/emscripten/SDL_emscriptenvideo.h b/src/video/emscripten/SDL_emscriptenvideo.h
index feca1ba..60383c7 100644
--- a/src/video/emscripten/SDL_emscriptenvideo.h
+++ b/src/video/emscripten/SDL_emscriptenvideo.h
@@ -40,6 +40,8 @@ typedef struct SDL_WindowData
SDL_Window *window;
SDL_Surface *surface;
+ char *canvas_id;
+
float pixel_ratio;
SDL_bool external_size;