0/**********************************************************************************************
1*
2* rcore_web_emscripten - Functions to manage window, graphics device and inputs
3*
4* PLATFORM: WEB - EMSCRIPTEN
5* - HTML5 (WebAssembly)
6*
7* LIMITATIONS:
8* - TBD
9*
10* POSSIBLE IMPROVEMENTS:
11* - TBD
12*
13* ADDITIONAL NOTES:
14* - TRACELOG() function is located in raylib [utils] module
15*
16* CONFIGURATION:
17* #define RCORE_PLATFORM_CUSTOM_FLAG
18* Custom flag for rcore on target platform -not used-
19*
20* DEPENDENCIES:
21* - emscripten: Allow interaction between browser API and C
22* - gestures: Gestures system for touch-ready devices (or simulated from mouse inputs)
23*
24*
25* LICENSE: zlib/libpng
26*
27* Copyright (c) 2025 Ramon Santamaria (@raysan5) and contributors
28*
29* This software is provided "as-is", without any express or implied warranty. In no event
30* will the authors be held liable for any damages arising from the use of this software.
31*
32* Permission is granted to anyone to use this software for any purpose, including commercial
33* applications, and to alter it and redistribute it freely, subject to the following restrictions:
34*
35* 1. The origin of this software must not be misrepresented; you must not claim that you
36* wrote the original software. If you use this software in a product, an acknowledgment
37* in the product documentation would be appreciated but is not required.
38*
39* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
40* as being the original software.
41*
42* 3. This notice may not be removed or altered from any source distribution.
43*
44**********************************************************************************************/
46#include <emscripten/emscripten.h> // Emscripten functionality for C
47#include <emscripten/html5.h> // Emscripten HTML5 library
49#include <sys/time.h> // Required for: timespec, nanosleep(), select() - POSIX
51//----------------------------------------------------------------------------------
52// Defines and Macros
53//----------------------------------------------------------------------------------
54#if (_POSIX_C_SOURCE < 199309L)
55 #undef _POSIX_C_SOURCE
56 #define _POSIX_C_SOURCE 199309L // Required for: CLOCK_MONOTONIC if compiled with c99 without gnu ext.
57#endif
59//----------------------------------------------------------------------------------
60// Types and Structures Definition
61//----------------------------------------------------------------------------------
62typedef struct {
63 char canvasId[64]; // Current canvas id
64 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE glContext; // OpenGL context
65 unsigned int *pixels; // Pointer to pixel data buffer (RGBA 32bit format)
66} PlatformData;
68//----------------------------------------------------------------------------------
69// Global Variables Definition
70//----------------------------------------------------------------------------------
71extern CoreData CORE; // Global CORE state context
73static PlatformData platform = { 0 }; // Platform specific data
75//----------------------------------------------------------------------------------
76// Global Variables Definition
77//----------------------------------------------------------------------------------
78static const char cursorLUT[11][12] = {
79 "default", // 0 MOUSE_CURSOR_DEFAULT
80 "default", // 1 MOUSE_CURSOR_ARROW
81 "text", // 2 MOUSE_CURSOR_IBEAM
82 "crosshair", // 3 MOUSE_CURSOR_CROSSHAIR
83 "pointer", // 4 MOUSE_CURSOR_POINTING_HAND
84 "ew-resize", // 5 MOUSE_CURSOR_RESIZE_EW
85 "ns-resize", // 6 MOUSE_CURSOR_RESIZE_NS
86 "nwse-resize", // 7 MOUSE_CURSOR_RESIZE_NWSE
87 "nesw-resize", // 8 MOUSE_CURSOR_RESIZE_NESW
88 "move", // 9 MOUSE_CURSOR_RESIZE_ALL
89 "not-allowed" // 10 MOUSE_CURSOR_NOT_ALLOWED
90};
92//----------------------------------------------------------------------------------
93// Module Internal Functions Declaration
94//----------------------------------------------------------------------------------
95int InitPlatform(void); // Initialize platform (graphics, inputs and more)
96void ClosePlatform(void); // Close platform
98// Emscripten window callback events
99static EM_BOOL EmscriptenResizeCallback(int eventType, const EmscriptenUiEvent *event, void *userData);
100static EM_BOOL EmscriptenFocusCallback(int eventType, const EmscriptenFocusEvent *focusEvent, void *userData);
101static EM_BOOL EmscriptenVisibilityChangeCallback(int eventType, const EmscriptenVisibilityChangeEvent *visibilityChangeEvent, void *userData);
102static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *event, void *userData);
103// TODO: Implement GLFW3 alternative for drop callback, runs when drop files into browser/canvas
104//static void WindowDropCallback(GLFWwindow *window, int count, const char **paths);
106// Emscripten input callback events
107static EM_BOOL EmscriptenKeyboardCallback(int eventType, const EmscriptenKeyboardEvent *keyboardEvent, void *userData);
108static EM_BOOL EmscriptenMouseCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
109static EM_BOOL EmscriptenMouseMoveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
110static EM_BOOL EmscriptenMouseWheelCallback(int eventType, const EmscriptenWheelEvent *wheelEvent, void *userData);
111static EM_BOOL EmscriptenPointerlockCallback(int eventType, const EmscriptenPointerlockChangeEvent *pointerlockChangeEvent, void *userData);
112static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData);
113static EM_BOOL EmscriptenGamepadCallback(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData);
115// JS: Set the canvas id provided by the module configuration
116EM_JS(void, SetCanvasIdJs, (char *out, int outSize), {
117 var canvasId = "#" + Module.canvas.id;
118 stringToUTF8(canvasId, out, outSize);
119});
121//----------------------------------------------------------------------------------
122// Module Functions Declaration
123//----------------------------------------------------------------------------------
124// NOTE: Functions declaration is provided by raylib.h
126//----------------------------------------------------------------------------------
127// Module Functions Definition: Window and Graphics Device
128//----------------------------------------------------------------------------------
130// Check if application should close
131// This will always return false on a web-build as web builds have no control over this functionality
132// Sleep is handled in EndDrawing() for synchronous code
133bool WindowShouldClose(void)
134{
135 // Emscripten Asyncify is required to run synchronous code in asynchronous JS
136 // REF: https://emscripten.org/docs/porting/asyncify.html
138 // WindowShouldClose() is not called on a web-ready raylib application if using emscripten_set_main_loop()
139 // and encapsulating one frame execution on a UpdateDrawFrame() function,
140 // allowing the browser to manage execution asynchronously
142 // Optionally we can manage the time we give-control-back-to-browser if required,
143 // but it seems below line could generate stuttering on some browsers
144 emscripten_sleep(12);
146 return false;
147}
149// Toggle fullscreen mode
150void ToggleFullscreen(void)
151{
152 bool enterFullscreen = false;
154 const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0);
155 if (wasFullscreen)
156 {
157 if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE)) enterFullscreen = false;
158 else if (FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE)) enterFullscreen = true;
159 else
160 {
161 const int canvasWidth = EM_ASM_INT( { return Module.canvas.width; }, 0);
162 const int canvasStyleWidth = EM_ASM_INT( { return parseInt(Module.canvas.style.width); }, 0);
163 if (canvasStyleWidth > canvasWidth) enterFullscreen = false;
164 else enterFullscreen = true;
165 }
167 EM_ASM(document.exitFullscreen(););
169 FLAG_CLEAR(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
170 FLAG_CLEAR(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE);
171 }
172 else enterFullscreen = true;
174 if (enterFullscreen)
175 {
176 // NOTE: The setTimeouts handle the browser mode change delay
177 EM_ASM
178 (
179 setTimeout(function()
180 {
181 Module.requestFullscreen(false, false);
182 }, 100);
183 );
185 FLAG_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
186 }
188 // NOTE: Old notes below:
189 /*
190 EM_ASM
191 (
192 // This strategy works well while using raylib minimal web shell for emscripten,
193 // it re-scales the canvas to fullscreen using monitor resolution, for tools this
194 // is a good strategy but maybe games prefer to keep current canvas resolution and
195 // display it in fullscreen, adjusting monitor resolution if possible
196 if (document.fullscreenElement) document.exitFullscreen();
197 else Module.requestFullscreen(true, true); //false, true);
198 );
199 */
200 // EM_ASM(Module.requestFullscreen(false, false););
201 /*
202 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
203 {
204 // Option 1: Request fullscreen for the canvas element
205 // This option does not seem to work at all:
206 // emscripten_request_pointerlock() and emscripten_request_fullscreen() are affected by web security,
207 // the user must click once on the canvas to hide the pointer or transition to full screen
208 //emscripten_request_fullscreen("#canvas", false);
210 // Option 2: Request fullscreen for the canvas element with strategy
211 // This option does not seem to work at all
212 // REF: https://github.com/emscripten-core/emscripten/issues/5124
213 // EmscriptenFullscreenStrategy strategy = {
214 // .scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH, //EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT,
215 // .canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF,
216 // .filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT,
217 // .canvasResizedCallback = EmscriptenWindowResizedCallback,
218 // .canvasResizedCallbackUserData = NULL
219 // };
220 //emscripten_request_fullscreen_strategy("#canvas", EM_FALSE, &strategy);
222 // Option 3: Request fullscreen for the canvas element with strategy
223 // It works as expected but only inside the browser (client area)
224 EmscriptenFullscreenStrategy strategy = {
225 .scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT,
226 .canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF,
227 .filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT,
228 .canvasResizedCallback = EmscriptenWindowResizedCallback,
229 .canvasResizedCallbackUserData = NULL
230 };
231 emscripten_enter_soft_fullscreen("#canvas", &strategy);
233 int width = 0;
234 int height = 0;
235 emscripten_get_canvas_element_size("#canvas", &width, &height);
236 TRACELOG(LOG_WARNING, "Emscripten: Enter fullscreen: Canvas size: %i x %i", width, height);
238 FLAG_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
239 }
240 else
241 {
242 //emscripten_exit_fullscreen();
243 //emscripten_exit_soft_fullscreen();
245 int width, height;
246 emscripten_get_canvas_element_size("#canvas", &width, &height);
247 TRACELOG(LOG_WARNING, "Emscripten: Exit fullscreen: Canvas size: %i x %i", width, height);
249 FLAG_CLEAR(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
250 }
251 */
252}
254// Toggle borderless windowed mode
255void ToggleBorderlessWindowed(void)
256{
257 bool enterBorderless = false;
259 const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0);
260 if (wasFullscreen)
261 {
262 if (FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE)) enterBorderless = false;
263 else if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE)) enterBorderless = true;
264 else
265 {
266 const int canvasWidth = EM_ASM_INT( { return Module.canvas.width; }, 0);
267 const int screenWidth = EM_ASM_INT( { return screen.width; }, 0);
268 if (screenWidth == canvasWidth) enterBorderless = false;
269 else enterBorderless = true;
270 }
272 EM_ASM(document.exitFullscreen(););
274 FLAG_CLEAR(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
275 FLAG_CLEAR(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE);
276 }
277 else enterBorderless = true;
279 if (enterBorderless)
280 {
281 // 1. The setTimeouts handle the browser mode change delay
282 // 2. The style unset handles the possibility of a width="value%" like on the default shell.html file
283 EM_ASM
284 (
285 setTimeout(function()
286 {
287 Module.requestFullscreen(false, true);
288 setTimeout(function()
289 {
290 canvas.style.width="unset";
291 }, 100);
292 }, 100);
293 );
294 FLAG_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE);
295 }
296}
298// Set window state: maximized, if resizable
299void MaximizeWindow(void)
300{
301 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE) && !FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED))
302 {
303 const int tabWidth = EM_ASM_INT( return window.innerWidth; );
304 const int tabHeight = EM_ASM_INT( return window.innerHeight; );
306 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED);
307 }
308}
310// Set window state: minimized
311void MinimizeWindow(void)
312{
313 TRACELOG(LOG_WARNING, "MinimizeWindow() not available on target platform");
314}
316// Restore window from being minimized/maximized
317void RestoreWindow(void)
318{
319 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE) && FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED))
320 {
321 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED);
322 }
323}
325// Set window configuration state using flags
326void SetWindowState(unsigned int flags)
327{
328 if (!CORE.Window.ready) TRACELOG(LOG_WARNING, "WINDOW: SetWindowState does nothing before window initialization, Use \"SetConfigFlags\" instead");
330 // Check previous state and requested state to apply required changes
331 // NOTE: In most cases the functions already change the flags internally
333 // State change: FLAG_VSYNC_HINT
334 if (FLAG_IS_SET(flags, FLAG_VSYNC_HINT))
335 {
336 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_VSYNC_HINT) not available on target platform");
337 }
339 // State change: FLAG_BORDERLESS_WINDOWED_MODE
340 if (FLAG_IS_SET(flags, FLAG_BORDERLESS_WINDOWED_MODE))
341 {
342 // NOTE: Window state flag updated inside ToggleBorderlessWindowed() function
343 const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0);
344 if (wasFullscreen)
345 {
346 const int canvasWidth = EM_ASM_INT( { return Module.canvas.width; }, 0);
347 const int canvasStyleWidth = EM_ASM_INT( { return parseInt(Module.canvas.style.width); }, 0);
348 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE)) || canvasStyleWidth > canvasWidth) ToggleBorderlessWindowed();
349 }
350 else ToggleBorderlessWindowed();
351 }
353 // State change: FLAG_FULLSCREEN_MODE
354 if (FLAG_IS_SET(flags, FLAG_FULLSCREEN_MODE))
355 {
356 // NOTE: Window state flag updated inside ToggleFullscreen() function
357 const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0);
358 if (wasFullscreen)
359 {
360 const int canvasWidth = EM_ASM_INT( { return Module.canvas.width; }, 0);
361 const int screenWidth = EM_ASM_INT( { return screen.width; }, 0);
362 if (FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE) || (screenWidth == canvasWidth)) ToggleFullscreen();
363 }
364 else ToggleFullscreen();
365 }
367 // State change: FLAG_WINDOW_RESIZABLE
368 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE) != FLAG_IS_SET(flags, FLAG_WINDOW_RESIZABLE)) && FLAG_IS_SET(flags, FLAG_WINDOW_RESIZABLE))
369 {
370 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE);
371 }
373 // State change: FLAG_WINDOW_UNDECORATED
374 if (FLAG_IS_SET(flags, FLAG_WINDOW_UNDECORATED))
375 {
376 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_WINDOW_UNDECORATED) not available on target platform");
377 }
379 // State change: FLAG_WINDOW_HIDDEN
380 if (FLAG_IS_SET(flags, FLAG_WINDOW_HIDDEN))
381 {
382 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_WINDOW_HIDDEN) not available on target platform");
383 }
385 // State change: FLAG_WINDOW_MINIMIZED
386 if (FLAG_IS_SET(flags, FLAG_WINDOW_MINIMIZED))
387 {
388 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_WINDOW_MINIMIZED) not available on target platform");
389 }
391 // State change: FLAG_WINDOW_MAXIMIZED
392 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED) != FLAG_IS_SET(flags, FLAG_WINDOW_MAXIMIZED)) && FLAG_IS_SET(flags, FLAG_WINDOW_MAXIMIZED))
393 {
394 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE))
395 {
396 const int tabWidth = EM_ASM_INT( return window.innerWidth; );
397 const int tabHeight = EM_ASM_INT( return window.innerHeight; );
399 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED);
400 }
401 }
403 // State change: FLAG_WINDOW_UNFOCUSED
404 if (FLAG_IS_SET(flags, FLAG_WINDOW_UNFOCUSED))
405 {
406 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_WINDOW_UNFOCUSED) not available on target platform");
407 }
409 // State change: FLAG_WINDOW_TOPMOST
410 if (FLAG_IS_SET(flags, FLAG_WINDOW_TOPMOST))
411 {
412 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_WINDOW_TOPMOST) not available on target platform");
413 }
415 // State change: FLAG_WINDOW_ALWAYS_RUN
416 if (FLAG_IS_SET(flags, FLAG_WINDOW_ALWAYS_RUN))
417 {
418 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_WINDOW_ALWAYS_RUN) not available on target platform");
419 }
421 // The following states can not be changed after window creation
422 // NOTE: Review for PLATFORM_WEB
424 // State change: FLAG_WINDOW_TRANSPARENT
425 if (FLAG_IS_SET(flags, FLAG_WINDOW_TRANSPARENT))
426 {
427 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_WINDOW_TRANSPARENT) not available on target platform");
428 }
430 // State change: FLAG_WINDOW_HIGHDPI
431 if (FLAG_IS_SET(flags, FLAG_WINDOW_HIGHDPI))
432 {
433 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_WINDOW_HIGHDPI) not available on target platform");
434 }
436 // State change: FLAG_WINDOW_MOUSE_PASSTHROUGH
437 if (FLAG_IS_SET(flags, FLAG_WINDOW_MOUSE_PASSTHROUGH))
438 {
439 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_WINDOW_MOUSE_PASSTHROUGH) not available on target platform");
440 }
442 // State change: FLAG_MSAA_4X_HINT
443 if (FLAG_IS_SET(flags, FLAG_MSAA_4X_HINT))
444 {
445 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_MSAA_4X_HINT) not available on target platform");
446 }
448 // State change: FLAG_INTERLACED_HINT
449 if (FLAG_IS_SET(flags, FLAG_INTERLACED_HINT))
450 {
451 TRACELOG(LOG_WARNING, "SetWindowState(FLAG_INTERLACED_HINT) not available on target platform");
452 }
453}
455// Clear window configuration state flags
456void ClearWindowState(unsigned int flags)
457{
458 // Check previous state and requested state to apply required changes
459 // NOTE: In most cases the functions already change the flags internally
461 // State change: FLAG_VSYNC_HINT
462 if (FLAG_IS_SET(flags, FLAG_VSYNC_HINT))
463 {
464 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_VSYNC_HINT) not available on target platform");
465 }
467 // State change: FLAG_BORDERLESS_WINDOWED_MODE
468 if (FLAG_IS_SET(flags, FLAG_BORDERLESS_WINDOWED_MODE))
469 {
470 const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0);
471 if (wasFullscreen)
472 {
473 const int canvasWidth = EM_ASM_INT( { return Module.canvas.width; }, 0);
474 const int screenWidth = EM_ASM_INT( { return screen.width; }, 0);
475 if (FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE) || (screenWidth == canvasWidth)) EM_ASM(document.exitFullscreen(););
476 }
478 FLAG_CLEAR(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE);
479 }
481 // State change: FLAG_FULLSCREEN_MODE
482 if (FLAG_IS_SET(flags, FLAG_FULLSCREEN_MODE))
483 {
484 const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0);
485 if (wasFullscreen)
486 {
487 const int canvasWidth = EM_ASM_INT( { return Module.canvas.width; }, 0);
488 const int canvasStyleWidth = EM_ASM_INT( { return parseInt(Module.canvas.style.width); }, 0);
489 if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE) || (canvasStyleWidth > canvasWidth)) EM_ASM(document.exitFullscreen(););
490 }
492 FLAG_CLEAR(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
493 }
495 // State change: FLAG_WINDOW_RESIZABLE
496 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE) && FLAG_IS_SET(flags, FLAG_WINDOW_RESIZABLE))
497 {
498 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_RESIZABLE);
499 }
501 // State change: FLAG_WINDOW_HIDDEN
502 if (FLAG_IS_SET(flags, FLAG_WINDOW_HIDDEN))
503 {
504 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_WINDOW_HIDDEN) not available on target platform");
505 }
507 // State change: FLAG_WINDOW_MINIMIZED
508 if (FLAG_IS_SET(flags, FLAG_WINDOW_MINIMIZED))
509 {
510 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_WINDOW_MINIMIZED) not available on target platform");
511 }
513 // State change: FLAG_WINDOW_MAXIMIZED
514 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED) && FLAG_IS_SET(flags, FLAG_WINDOW_MAXIMIZED))
515 {
516 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE))
517 {
518 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED);
519 }
520 }
522 // State change: FLAG_WINDOW_UNDECORATED
523 if (FLAG_IS_SET(flags, FLAG_WINDOW_UNDECORATED))
524 {
525 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_WINDOW_UNDECORATED) not available on target platform");
526 }
528 // State change: FLAG_WINDOW_UNFOCUSED
529 if (FLAG_IS_SET(flags, FLAG_WINDOW_UNFOCUSED))
530 {
531 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_WINDOW_UNFOCUSED) not available on target platform");
532 }
534 // State change: FLAG_WINDOW_TOPMOST
535 if (FLAG_IS_SET(flags, FLAG_WINDOW_TOPMOST))
536 {
537 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_WINDOW_TOPMOST) not available on target platform");
538 }
540 // State change: FLAG_WINDOW_ALWAYS_RUN
541 if (FLAG_IS_SET(flags, FLAG_WINDOW_ALWAYS_RUN))
542 {
543 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_WINDOW_ALWAYS_RUN) not available on target platform");
544 }
546 // The following states can not be changed after window creation
547 // NOTE: Review for PLATFORM_WEB
549 // State change: FLAG_WINDOW_TRANSPARENT
550 if (FLAG_IS_SET(flags, FLAG_WINDOW_TRANSPARENT))
551 {
552 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_WINDOW_TRANSPARENT) not available on target platform");
553 }
555 // State change: FLAG_WINDOW_HIGHDPI
556 if (FLAG_IS_SET(flags, FLAG_WINDOW_HIGHDPI))
557 {
558 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_WINDOW_HIGHDPI) not available on target platform");
559 }
561 // State change: FLAG_WINDOW_MOUSE_PASSTHROUGH
562 if (FLAG_IS_SET(flags, FLAG_WINDOW_MOUSE_PASSTHROUGH))
563 {
564 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_WINDOW_MOUSE_PASSTHROUGH) not available on target platform");
565 }
567 // State change: FLAG_MSAA_4X_HINT
568 if (FLAG_IS_SET(flags, FLAG_MSAA_4X_HINT))
569 {
570 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_MSAA_4X_HINT) not available on target platform");
571 }
573 // State change: FLAG_INTERLACED_HINT
574 if (FLAG_IS_SET(flags, FLAG_INTERLACED_HINT))
575 {
576 TRACELOG(LOG_WARNING, "ClearWindowState(FLAG_INTERLACED_HINT) not available on target platform");
577 }
578}
580// Set icon for window
581void SetWindowIcon(Image image)
582{
583 TRACELOG(LOG_WARNING, "SetWindowIcon() not available on target platform");
584}
586// Set icon for window, multiple images
587void SetWindowIcons(Image *images, int count)
588{
589 TRACELOG(LOG_WARNING, "SetWindowIcons() not available on target platform");
590}
592// Set title for window
593void SetWindowTitle(const char *title)
594{
595 CORE.Window.title = title;
596 emscripten_set_window_title(title);
597}
599// Set window position on screen (windowed mode)
600void SetWindowPosition(int x, int y)
601{
602 TRACELOG(LOG_WARNING, "SetWindowPosition() not available on target platform");
603}
605// Set monitor for the current window
606void SetWindowMonitor(int monitor)
607{
608 TRACELOG(LOG_WARNING, "SetWindowMonitor() not available on target platform");
609}
611// Set window minimum dimensions (FLAG_WINDOW_RESIZABLE)
612void SetWindowMinSize(int width, int height)
613{
614 CORE.Window.screenMin.width = width;
615 CORE.Window.screenMin.height = height;
617 // Trigger the resize event once to update the window minimum width and height
618 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE) != 0) EmscriptenResizeCallback(EMSCRIPTEN_EVENT_RESIZE, NULL, NULL);
619}
621// Set window maximum dimensions (FLAG_WINDOW_RESIZABLE)
622void SetWindowMaxSize(int width, int height)
623{
624 CORE.Window.screenMax.width = width;
625 CORE.Window.screenMax.height = height;
627 // Trigger the resize event once to update the window maximum width and height
628 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE) != 0) EmscriptenResizeCallback(EMSCRIPTEN_EVENT_RESIZE, NULL, NULL);
629}
631// Set window dimensions
632void SetWindowSize(int width, int height)
633{
634 // When resizing the canvas, several elements must be considered:
635 // - CSS canvas size: Web layout size, logical pixels
636 // - Canvas contained framebuffer resolution
637 // * Browser monitor, device pixel ratio (HighDPI)
639 double canvasCssWidth = 0.0;
640 double canvasCssHeight = 0.0;
641 emscripten_get_element_css_size(platform.canvasId, &canvasCssWidth, &canvasCssHeight);
643 // NOTE: emscripten_get_canvas_element_size() returns canvas framebuffer size, not CSS canvas size
645 // Get device pixel ratio
646 // TODO: Should DPI be considered at this point?
647 double dpr = emscripten_get_device_pixel_ratio();
649 // Set canvas framebuffer size
650 emscripten_set_canvas_element_size(platform.canvasId, width*dpr, height*dpr);
652 // Set canvas CSS size
653 // TODO: Consider canvas CSS style if already scaled 100%
654 EM_ASM({ Module.canvas.style.width = $0; }, width*dpr);
655 EM_ASM({ Module.canvas.style.height = $0; }, height*dpr);
657 SetupViewport(width*dpr, height*dpr); // Reset viewport and projection matrix for new size
658}
660// Set window opacity, value opacity is between 0.0 and 1.0
661void SetWindowOpacity(float opacity)
662{
663 if (opacity >= 1.0f) opacity = 1.0f;
664 else if (opacity <= 0.0f) opacity = 0.0f;
666 EM_ASM({ Module.canvas.style.opacity = $0; }, opacity);
667}
669// Set window focused
670void SetWindowFocused(void)
671{
672 TRACELOG(LOG_WARNING, "SetWindowFocused() not available on target platform");
673}
675// Get native window handle
676void *GetWindowHandle(void)
677{
678 TRACELOG(LOG_WARNING, "GetWindowHandle() not implemented on target platform");
679 return NULL;
680}
682// Get number of monitors
683int GetMonitorCount(void)
684{
685 TRACELOG(LOG_WARNING, "GetMonitorCount() not implemented on target platform");
686 return 1;
687}
689// Get current monitor where window is placed
690int GetCurrentMonitor(void)
691{
692 TRACELOG(LOG_WARNING, "GetCurrentMonitor() not implemented on target platform");
693 return 0;
694}
696// Get selected monitor position
697Vector2 GetMonitorPosition(int monitor)
698{
699 TRACELOG(LOG_WARNING, "GetMonitorPosition() not implemented on target platform");
700 return (Vector2){ 0, 0 };
701}
703// Get selected monitor width (currently used by monitor)
704int GetMonitorWidth(int monitor)
705{
706 // Get the width of the user's entire screen in CSS logical pixels,
707 // no physical pixels, it would require multiplying by device pixel ratio
708 // NOTE: Returned value is limited to the current monitor where the browser window is located
709 int width = 0;
710 width = EM_ASM_INT( { return window.screen.width; }, 0);
711 return width;
712}
714// Get selected monitor height (currently used by monitor)
715int GetMonitorHeight(int monitor)
716{
717 // Get the height of the user's entire screen in CSS logical pixels,
718 // no physical pixels, it would require multiplying by device pixel ratio
719 // NOTE: Returned value is limited to the current monitor where the browser window is located
720 int height = 0;
721 height = EM_ASM_INT( { return window.screen.height; }, 0);
722 return height;
723}
725// Get selected monitor physical width in millimetres
726int GetMonitorPhysicalWidth(int monitor)
727{
728 TRACELOG(LOG_WARNING, "GetMonitorPhysicalWidth() not implemented on target platform");
729 return 0;
730}
732// Get selected monitor physical height in millimetres
733int GetMonitorPhysicalHeight(int monitor)
734{
735 TRACELOG(LOG_WARNING, "GetMonitorPhysicalHeight() not implemented on target platform");
736 return 0;
737}
739// Get selected monitor refresh rate
740int GetMonitorRefreshRate(int monitor)
741{
742 TRACELOG(LOG_WARNING, "GetMonitorRefreshRate() not implemented on target platform");
743 return 0;
744}
746// Get the human-readable, UTF-8 encoded name of the selected monitor
747const char *GetMonitorName(int monitor)
748{
749 TRACELOG(LOG_WARNING, "GetMonitorName() not implemented on target platform");
750 return "";
751}
753// Get window position XY on monitor
754Vector2 GetWindowPosition(void)
755{
756 // Browser window position, top-left corner relative to the physical screen origin, expressed in CSS logical pixels
757 // NOTE: Returned position is relative to the current monitor where the browser window is located
758 Vector2 position = { 0, 0 };
759 position.x = (float)EM_ASM_INT( { return window.screenX; }, 0);
760 position.y = (float)EM_ASM_INT( { return window.screenY; }, 0);
761 return position;
762}
764// Get current monitor device pixel ratio
765Vector2 GetWindowScaleDPI(void)
766{
767 // Get device pixel ratio
768 // NOTE: Returned scale is relative to the current monitor where the browser window is located
769 Vector2 scale = { 1.0f, 1.0f };
770 scale.x = (float)EM_ASM_DOUBLE( { return window.devicePixelRatio; } );
771 scale.y = scale.x;
772 return scale;
773}
775// Set clipboard text content
776void SetClipboardText(const char *text)
777{
778 // Security check to (partially) avoid malicious code
779 if (strchr(text, '\'') != NULL) TRACELOG(LOG_WARNING, "SYSTEM: Provided Clipboard could be potentially malicious, avoid [\'] character");
780 else EM_ASM({ navigator.clipboard.writeText(UTF8ToString($0)); }, text);
781}
783// Get clipboard text content
784// NOTE: returned string is allocated and freed by GLFW
785const char *GetClipboardText(void)
786{
787/*
788 // Accessing clipboard data from browser is tricky due to security reasons
789 // The method to use is navigator.clipboard.readText() but this is an asynchronous method
790 // that will return at some moment after the function is called with the required data
791 emscripten_run_script_string("navigator.clipboard.readText() \
792 .then(text => { document.getElementById('clipboard').innerText = text; console.log('Pasted content: ', text); }) \
793 .catch(err => { console.error('Failed to read clipboard contents: ', err); });"
794 );
796 // The main issue is getting that data, one approach could be using ASYNCIFY and wait
797 // for the data but it requires adding Asyncify emscripten library on compilation
799 // Another approach could be just copy the data in a HTML text field and try to retrieve it
800 // later on if available... and clean it for future accesses
801*/
802 return NULL;
803}
805// Get clipboard image
806Image GetClipboardImage(void)
807{
808 Image image = { 0 };
810 // NOTE: In theory, the new navigator.clipboard.read() can be used to return arbitrary data from clipboard (2024)
811 // REF: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/read
812 TRACELOG(LOG_WARNING, "GetClipboardImage() not implemented on target platform");
814 return image;
815}
817// Show mouse cursor
818void ShowCursor(void)
819{
820 if (CORE.Input.Mouse.cursorHidden)
821 {
822 EM_ASM( { Module.canvas.style.cursor = UTF8ToString($0); }, cursorLUT[CORE.Input.Mouse.cursor]);
824 CORE.Input.Mouse.cursorHidden = false;
825 }
826}
828// Hides mouse cursor
829void HideCursor(void)
830{
831 if (!CORE.Input.Mouse.cursorHidden)
832 {
833 EM_ASM(Module.canvas.style.cursor = 'none';);
835 CORE.Input.Mouse.cursorHidden = true;
836 }
837}
839// Enables cursor (unlock cursor)
840void EnableCursor(void)
841{
842 emscripten_exit_pointerlock();
844 // Set cursor position in the middle
845 SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
847 // NOTE: CORE.Input.Mouse.cursorLocked handled by EmscriptenPointerlockCallback()
848}
850// Disables cursor (lock cursor)
851void DisableCursor(void)
852{
853 emscripten_request_pointerlock(platform.canvasId, 1);
855 // Set cursor position in the middle
856 SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
858 // NOTE: CORE.Input.Mouse.cursorLocked handled by EmscriptenPointerlockCallback()
859}
861// Swap back buffer with front buffer (screen drawing)
862void SwapScreenBuffer(void)
863{
864#if defined(GRAPHICS_API_OPENGL_11_SOFTWARE)
865 // Update framebuffer
866 rlCopyFramebuffer(0, 0, CORE.Window.render.width, CORE.Window.render.height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, platform.pixels);
868 // Copy framebuffer data into canvas
869 EM_ASM({
870 const width = $0;
871 const height = $1;
872 const ptr = $2;
874 // Get canvas and 2d context created
875 const canvas = Module.canvas;
876 //const canvas = Module['canvas'];
877 const ctx = canvas.getContext('2d');
879 if (!Module.__img || (Module.__img.width !== width) || (Module.__img.height !== height)) {
880 Module.__img = ctx.createImageData(width, height);
881 }
883 const src = HEAPU8.subarray(ptr, ptr + width*height*4); // RGBA (4 bytes)
884 Module.__img.data.set(src);
885 ctx.putImageData(Module.__img, 0, 0);
887 }, CORE.Window.screen.width, CORE.Window.screen.height, platform.pixels);
888#endif
889}
891//----------------------------------------------------------------------------------
892// Module Functions Definition: Misc
893//----------------------------------------------------------------------------------
895// Get elapsed time measure in seconds since InitTimer()
896double GetTime(void)
897{
898 double time = 0.0;
899 /*
900 struct timespec ts = { 0 };
901 clock_gettime(CLOCK_MONOTONIC, &ts);
902 unsigned long long int nanoSeconds = (unsigned long long int)ts.tv_sec*1000000000LLU + (unsigned long long int)ts.tv_nsec;
903 time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer()
904 */
905 time = emscripten_get_now()*1000.0;
907 return time;
908}
910// Open URL with default system browser (if available)
911// NOTE: This function is only safe to use if you control the URL given
912// A user could craft a malicious string performing another action
913// Only call this function yourself not with user input or make sure to check the string yourself
914void OpenURL(const char *url)
915{
916 // Security check to (partially) avoid malicious code on target platform
917 if (strchr(url, '\'') != NULL) TRACELOG(LOG_WARNING, "SYSTEM: Provided URL could be potentially malicious, avoid [\'] character");
918 else emscripten_run_script(TextFormat("window.open('%s', '_blank')", url));
919}
921//----------------------------------------------------------------------------------
922// Module Functions Definition: Inputs
923//----------------------------------------------------------------------------------
925// Set internal gamepad mappings
926int SetGamepadMappings(const char *mappings)
927{
928 TRACELOG(LOG_INFO, "SetGamepadMappings not implemented in rcore_web.c");
930 return 0;
931}
933// Set gamepad vibration
934void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration)
935{
936 if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (duration > 0.0f))
937 {
938 if (leftMotor < 0.0f) leftMotor = 0.0f;
939 if (leftMotor > 1.0f) leftMotor = 1.0f;
940 if (rightMotor < 0.0f) rightMotor = 0.0f;
941 if (rightMotor > 1.0f) rightMotor = 1.0f;
942 if (duration > MAX_GAMEPAD_VIBRATION_TIME) duration = MAX_GAMEPAD_VIBRATION_TIME;
943 duration *= 1000.0f; // Convert duration to ms
945 // NOTE: [2024.10.21] Current browser support:
946 // - vibrationActuator API: Chrome, Edge, Opera, Safari, Android Chrome, Android Webview
947 // - hapticActuators API: Firefox
948 EM_ASM({
949 try { navigator.getGamepads()[$0].vibrationActuator.playEffect('dual-rumble', { startDelay: 0, duration: $3, weakMagnitude: $1, strongMagnitude: $2 }); }
950 catch (e)
951 {
952 try { navigator.getGamepads()[$0].hapticActuators[0].pulse($2, $3); }
953 catch (e) { }
954 }
955 }, gamepad, leftMotor, rightMotor, duration);
956 }
957}
959// Set mouse position XY
960void SetMousePosition(int x, int y)
961{
962 // WARNING: Not supported by browser for security reasons
963}
965// Set mouse cursor
966void SetMouseCursor(int cursor)
967{
968 if (CORE.Input.Mouse.cursor != cursor)
969 {
970 if (!CORE.Input.Mouse.cursorLocked) EM_ASM( { Module.canvas.style.cursor = UTF8ToString($0); }, cursorLUT[cursor]);
971 CORE.Input.Mouse.cursor = cursor;
972 }
973}
975// Get physical key name
976const char *GetKeyName(int key)
977{
978 // TODO: Browser can definitely provide a key name e->key
979 TRACELOG(LOG_WARNING, "GetKeyName() not implemented on target platform");
980 return "";
981}
983// Register all input events
984void PollInputEvents(void)
985{
986#if defined(SUPPORT_GESTURES_SYSTEM)
987 // NOTE: Gestures update must be called every frame to reset gestures correctly
988 // because ProcessGestureEvent() is just called on an event, not every frame
989 UpdateGestures();
990#endif
992 // Reset keys/chars pressed registered
993 CORE.Input.Keyboard.keyPressedQueueCount = 0;
994 CORE.Input.Keyboard.charPressedQueueCount = 0;
996 // Reset last gamepad button/axis registered state
997 CORE.Input.Gamepad.lastButtonPressed = 0; // GAMEPAD_BUTTON_UNKNOWN
998 //CORE.Input.Gamepad.axisCount = 0;
1000 // Keyboard/Mouse input polling (automatically managed by GLFW3 through callback)
1002 // Register previous keys states
1003 for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)
1004 {
1005 CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
1006 CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
1007 }
1009 // Register previous mouse states
1010 for (int i = 0; i < MAX_MOUSE_BUTTONS; i++) CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
1012 // Register previous mouse wheel state
1013 CORE.Input.Mouse.previousWheelMove = CORE.Input.Mouse.currentWheelMove;
1014 CORE.Input.Mouse.currentWheelMove = (Vector2){ 0.0f, 0.0f };
1016 // Register previous mouse position
1017 CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
1019 // Register previous touch states
1020 for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i];
1022 // Reset touch positions
1023 // TODO: It resets on target platform the mouse position and not filled again until a move-event,
1024 // so, if mouse is not moved it returns a (0, 0) position... this behaviour should be reviewed!
1025 //for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.position[i] = (Vector2){ 0, 0 };
1027 // Get number of gamepads connected
1028 int numGamepads = 0;
1029 if (emscripten_sample_gamepad_data() == EMSCRIPTEN_RESULT_SUCCESS) numGamepads = emscripten_get_num_gamepads();
1031 for (int i = 0; (i < numGamepads) && (i < MAX_GAMEPADS); i++)
1032 {
1033 // Register previous gamepad button states
1034 for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) CORE.Input.Gamepad.previousButtonState[i][k] = CORE.Input.Gamepad.currentButtonState[i][k];
1036 EmscriptenGamepadEvent gamepadState = { 0 };
1037 int result = emscripten_get_gamepad_status(i, &gamepadState);
1039 if (result == EMSCRIPTEN_RESULT_SUCCESS)
1040 {
1041 // Register buttons data for every connected gamepad
1042 for (int j = 0; (j < gamepadState.numButtons) && (j < MAX_GAMEPAD_BUTTONS); j++)
1043 {
1044 GamepadButton button = -1;
1046 // Gamepad Buttons reference: https://www.w3.org/TR/gamepad/#gamepad-interface
1047 switch (j)
1048 {
1049 case 0: button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN; break;
1050 case 1: button = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT; break;
1051 case 2: button = GAMEPAD_BUTTON_RIGHT_FACE_LEFT; break;
1052 case 3: button = GAMEPAD_BUTTON_RIGHT_FACE_UP; break;
1053 case 4: button = GAMEPAD_BUTTON_LEFT_TRIGGER_1; break;
1054 case 5: button = GAMEPAD_BUTTON_RIGHT_TRIGGER_1; break;
1055 case 6: button = GAMEPAD_BUTTON_LEFT_TRIGGER_2; break;
1056 case 7: button = GAMEPAD_BUTTON_RIGHT_TRIGGER_2; break;
1057 case 8: button = GAMEPAD_BUTTON_MIDDLE_LEFT; break;
1058 case 9: button = GAMEPAD_BUTTON_MIDDLE_RIGHT; break;
1059 case 10: button = GAMEPAD_BUTTON_LEFT_THUMB; break;
1060 case 11: button = GAMEPAD_BUTTON_RIGHT_THUMB; break;
1061 case 12: button = GAMEPAD_BUTTON_LEFT_FACE_UP; break;
1062 case 13: button = GAMEPAD_BUTTON_LEFT_FACE_DOWN; break;
1063 case 14: button = GAMEPAD_BUTTON_LEFT_FACE_LEFT; break;
1064 case 15: button = GAMEPAD_BUTTON_LEFT_FACE_RIGHT; break;
1065 default: break;
1066 }
1068 if (button + 1 != 0) // Check for valid button
1069 {
1070 if (gamepadState.digitalButton[j] == 1)
1071 {
1072 CORE.Input.Gamepad.currentButtonState[i][button] = 1;
1073 CORE.Input.Gamepad.lastButtonPressed = button;
1074 }
1075 else CORE.Input.Gamepad.currentButtonState[i][button] = 0;
1076 }
1078 //TRACELOGD("INPUT: Gamepad %d, button %d: Digital: %d, Analog: %g", gamepadState.index, j, gamepadState.digitalButton[j], gamepadState.analogButton[j]);
1079 }
1081 // Register axis data for every connected gamepad
1082 for (int j = 0; (j < gamepadState.numAxes) && (j < MAX_GAMEPAD_AXES); j++)
1083 {
1084 CORE.Input.Gamepad.axisState[i][j] = gamepadState.axis[j];
1085 }
1087 CORE.Input.Gamepad.axisCount[i] = gamepadState.numAxes;
1088 }
1089 }
1091 CORE.Window.resizedLastFrame = false;
1092}
1094//----------------------------------------------------------------------------------
1095// Module Internal Functions Definition
1096//----------------------------------------------------------------------------------
1098// Initialize platform: graphics, inputs and more
1099int InitPlatform(void)
1100{
1101 SetCanvasIdJs(platform.canvasId, 64); // Get the current canvas id
1103 // Initialize graphic device: display/window and graphic context
1104 //----------------------------------------------------------------------------
1105 emscripten_set_canvas_element_size(platform.canvasId, CORE.Window.screen.width, CORE.Window.screen.height);
1106 EmscriptenWebGLContextAttributes attribs = { 0 };
1107 emscripten_webgl_init_context_attributes(&attribs);
1108 attribs.alpha = EM_TRUE;
1109 attribs.depth = EM_TRUE;
1110 attribs.stencil = EM_FALSE;
1111 attribs.antialias = EM_FALSE;
1113 // Check window creation flags
1114 // Disable FLAG_WINDOW_MINIMIZED, not supported
1115 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED)) FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MINIMIZED);
1117 // Disable FLAG_WINDOW_MAXIMIZED, not supported
1118 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED)) FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED);
1120 // Disable FLAG_WINDOW_TOPMOST, not supported
1121 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TOPMOST)) FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_TOPMOST);
1123 // NOTE: Some other flags are not supported on HTML5
1125 // TODO: Scale content area based on the monitor content scale where window is placed on
1127 // Request MSAA (usually x4 on WebGL 1.0)
1128 if (FLAG_IS_SET(CORE.Window.flags, FLAG_MSAA_4X_HINT)) attribs.antialias = EM_TRUE;
1130 // Check selection OpenGL version
1131 if (rlGetVersion() == RL_OPENGL_11_SOFTWARE)
1132 {
1133 // Avoid creating a WebGL canvas, create 2d canvas for software rendering
1134 emscripten_set_canvas_element_size(platform.canvasId, CORE.Window.screen.width, CORE.Window.screen.height);
1135 EM_ASM({
1136 const canvas = document.getElementById(platform.canvasId);
1137 Module.canvas = canvas;
1138 });
1140 // Load memory framebuffer with desired screen size
1141 platform.pixels = (unsigned int *)RL_CALLOC(CORE.Window.screen.width*CORE.Window.screen.height, sizeof(unsigned int));
1142 }
1143 else if (rlGetVersion() == RL_OPENGL_ES_20) // Request OpenGL ES 2.0 context --> WebGL 1.0
1144 {
1145 attribs.majorVersion = 1; // WebGL 1.0 requested
1146 attribs.minorVersion = 0;
1148 // Create WebGL context
1149 platform.glContext = emscripten_webgl_create_context(platform.canvasId, &attribs);
1150 if (platform.glContext == 0) return 0;
1152 emscripten_webgl_make_context_current(platform.glContext);
1153 }
1154 else if (rlGetVersion() == RL_OPENGL_ES_30) // Request OpenGL ES 3.0 context --> WebGL 2.0
1155 {
1156 attribs.majorVersion = 2; // WebGL 2.0 requested
1157 attribs.minorVersion = 0;
1159 // Create WebGL context
1160 platform.glContext = emscripten_webgl_create_context(platform.canvasId, &attribs);
1161 if (platform.glContext == 0) return 0;
1163 emscripten_webgl_make_context_current(platform.glContext);
1164 }
1166 // NOTE: Getting video modes is not implemented in emscripten GLFW3 version
1167 CORE.Window.display.width = CORE.Window.screen.width;
1168 CORE.Window.display.height = CORE.Window.screen.height;
1169 CORE.Window.render.width = CORE.Window.screen.width;
1170 CORE.Window.render.height = CORE.Window.screen.height;
1172 // Set default window title
1173 emscripten_set_window_title((CORE.Window.title != 0)? CORE.Window.title : " ");
1175 // Check context activation
1176 if ((platform.glContext != 0) || (platform.pixels != NULL))
1177 {
1178 CORE.Window.ready = true;
1180 int fbWidth = CORE.Window.screen.width;
1181 int fbHeight = CORE.Window.screen.height;
1183 CORE.Window.render.width = fbWidth;
1184 CORE.Window.render.height = fbHeight;
1185 CORE.Window.currentFbo.width = fbWidth;
1186 CORE.Window.currentFbo.height = fbHeight;
1188 TRACELOG(LOG_INFO, "DISPLAY: Device initialized successfully");
1189 TRACELOG(LOG_INFO, " > Display size: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
1190 TRACELOG(LOG_INFO, " > Screen size: %i x %i", CORE.Window.screen.width, CORE.Window.screen.height);
1191 TRACELOG(LOG_INFO, " > Render size: %i x %i", CORE.Window.render.width, CORE.Window.render.height);
1192 TRACELOG(LOG_INFO, " > Viewport offsets: %i, %i", CORE.Window.renderOffset.x, CORE.Window.renderOffset.y);
1193 }
1194 else
1195 {
1196 TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize graphics device");
1197 return -1;
1198 }
1200 // Load OpenGL extensions
1201 // NOTE: GL procedures address loader is required to load extensions
1202 if (platform.glContext != 0) rlLoadExtensions(emscripten_webgl_get_proc_address);
1203 //----------------------------------------------------------------------------
1205 // Initialize events callbacks
1206 //----------------------------------------------------------------------------
1207 // Setup window/canvas events callbacks
1208 emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenFullscreenChangeCallback);
1209 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenResizeCallback);
1210 emscripten_set_blur_callback(platform.canvasId, NULL, 1, EmscriptenFocusCallback);
1211 emscripten_set_focus_callback(platform.canvasId, NULL, 1, EmscriptenFocusCallback);
1212 emscripten_set_visibilitychange_callback(NULL, 1, EmscriptenVisibilityChangeCallback);
1214 // Setup input events
1215 emscripten_set_keypress_callback(platform.canvasId, NULL, 1, EmscriptenKeyboardCallback);
1216 emscripten_set_keydown_callback(platform.canvasId, NULL, 1, EmscriptenKeyboardCallback);
1217 emscripten_set_keyup_callback(platform.canvasId, NULL, 1, EmscriptenKeyboardCallback);
1219 emscripten_set_click_callback(platform.canvasId, NULL, 1, EmscriptenMouseCallback);
1220 //emscripten_set_dblclick_callback(platform.canvasId, NULL, 1, EmscriptenMouseCallback);
1221 emscripten_set_mousedown_callback(platform.canvasId, NULL, 1, EmscriptenMouseCallback);
1222 emscripten_set_mouseup_callback(platform.canvasId, NULL, 1, EmscriptenMouseCallback);
1223 emscripten_set_mousemove_callback(platform.canvasId, NULL, 1, EmscriptenMouseCallback);
1224 emscripten_set_mousemove_callback(platform.canvasId, NULL, 1, EmscriptenMouseMoveCallback);
1225 emscripten_set_wheel_callback(platform.canvasId, NULL, 1, EmscriptenMouseWheelCallback);
1226 emscripten_set_pointerlockchange_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenPointerlockCallback);
1228 emscripten_set_touchstart_callback(platform.canvasId, NULL, 1, EmscriptenTouchCallback);
1229 emscripten_set_touchend_callback(platform.canvasId, NULL, 1, EmscriptenTouchCallback);
1230 emscripten_set_touchmove_callback(platform.canvasId, NULL, 1, EmscriptenTouchCallback);
1231 emscripten_set_touchcancel_callback(platform.canvasId, NULL, 1, EmscriptenTouchCallback);
1233 emscripten_set_gamepadconnected_callback(NULL, 1, EmscriptenGamepadCallback);
1234 emscripten_set_gamepaddisconnected_callback(NULL, 1, EmscriptenGamepadCallback);
1236 // Trigger resize callback to force initial size
1237 EmscriptenResizeCallback(EMSCRIPTEN_EVENT_RESIZE, NULL, NULL);
1238 //----------------------------------------------------------------------------
1240 // Initialize timing system
1241 //----------------------------------------------------------------------------
1242 InitTimer();
1243 //----------------------------------------------------------------------------
1245 // Initialize storage system
1246 //----------------------------------------------------------------------------
1247 CORE.Storage.basePath = GetWorkingDirectory();
1248 //----------------------------------------------------------------------------
1250 TRACELOG(LOG_INFO, "PLATFORM: WEB: Initialized successfully");
1252 return 0;
1253}
1255// Close platform
1256// NOTE: Platform closing is managed by browser, so,
1257// this function is actually not required, but still
1258// implementing some logic behaviour
1259void ClosePlatform(void)
1260{
1261 if (platform.pixels != NULL) RL_FREE(platform.pixels);
1262 if (platform.glContext != 0) emscripten_webgl_destroy_context(platform.glContext);
1263}
1265// Emscripten callback functions, called on specific browser events
1266//-------------------------------------------------------------------------------------------------------
1267// Emscripten: Called on resize event
1268static EM_BOOL EmscriptenResizeCallback(int eventType, const EmscriptenUiEvent *event, void *userData)
1269{
1270 // Don't resize non-resizeable windows
1271 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE)) return 1;
1272/*
1273 // Set current screen size
1274 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
1275 {
1276 Vector2 windowScaleDPI = GetWindowScaleDPI();
1278 CORE.Window.screen.width = (unsigned int)(width/windowScaleDPI.x);
1279 CORE.Window.screen.height = (unsigned int)(height/windowScaleDPI.y);
1280 }
1281 else
1282 {
1283 CORE.Window.screen.width = width;
1284 CORE.Window.screen.height = height;
1285 }
1286*/
1287 // This event is called whenever the window changes sizes,
1288 // so the size of the canvas object is explicitly retrieved below
1289 int width = EM_ASM_INT( return window.innerWidth; );
1290 int height = EM_ASM_INT( return window.innerHeight; );
1292 if (width < (int)CORE.Window.screenMin.width) width = CORE.Window.screenMin.width;
1293 else if ((width > (int)CORE.Window.screenMax.width) && (CORE.Window.screenMax.width > 0)) width = CORE.Window.screenMax.width;
1295 if (height < (int)CORE.Window.screenMin.height) height = CORE.Window.screenMin.height;
1296 else if ((height > (int)CORE.Window.screenMax.height) && (CORE.Window.screenMax.height > 0)) height = CORE.Window.screenMax.height;
1298 emscripten_set_canvas_element_size(platform.canvasId, width, height);
1300 SetupViewport(width, height); // Reset viewport and projection matrix for new size
1302 CORE.Window.currentFbo.width = width;
1303 CORE.Window.currentFbo.height = height;
1304 CORE.Window.resizedLastFrame = true;
1306 if (IsWindowFullscreen()) return 1;
1308 // Set current screen size
1309 CORE.Window.screen.width = width;
1310 CORE.Window.screen.height = height;
1312 // NOTE: Postprocessing texture is not scaled to new size
1314 return 0;
1315}
1317// Emscripten: Called on windows focus change events
1318static EM_BOOL EmscriptenFocusCallback(int eventType, const EmscriptenFocusEvent *focusEvent, void *userData)
1319{
1320 EM_BOOL consumed = 1;
1322 switch (eventType)
1323 {
1324 case EMSCRIPTEN_EVENT_BLUR: FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED); break; // The canvas lost focus
1325 case EMSCRIPTEN_EVENT_FOCUS: FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED); break;
1326 default: consumed = 0; break;
1327 }
1329 return consumed;
1330}
1332// Emscripten: Called on visibility change events
1333static EM_BOOL EmscriptenVisibilityChangeCallback(int eventType, const EmscriptenVisibilityChangeEvent *visibilityChangeEvent, void *userData)
1334{
1335 if (visibilityChangeEvent->hidden) FLAG_SET(CORE.Window.flags, FLAG_WINDOW_HIDDEN); // The window was hidden
1336 else FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_HIDDEN); // The window was restored
1338 return 1; // The event was consumed by the callback handler
1339}
1341// Emscripten: Called on fullscreen change events
1342// TODO: Review fullscreen strategy
1343static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *event, void *userData)
1344{
1345 // NOTE: Reset the fullscreen flags if the user left fullscreen manually by pressing the Escape key
1346 const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0);
1347 if (!wasFullscreen)
1348 {
1349 FLAG_CLEAR(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
1350 FLAG_CLEAR(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE);
1351 }
1353 return 1; // The event was consumed by the callback handler
1354}
1356/*
1357// GLFW3: Called on file-drop over the window
1358// TODO: Implement Emscripten (or HTML5/JS) alternative
1359static void WindowDropCallback(GLFWwindow *window, int count, const char **paths)
1360{
1361 if (count > 0)
1362 {
1363 // In case previous dropped filepaths have not been freed, we free them
1364 if (CORE.Window.dropFileCount > 0)
1365 {
1366 for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++) RL_FREE(CORE.Window.dropFilepaths[i]);
1368 RL_FREE(CORE.Window.dropFilepaths);
1370 CORE.Window.dropFileCount = 0;
1371 CORE.Window.dropFilepaths = NULL;
1372 }
1374 // WARNING: Paths are freed by GLFW when the callback returns, we must keep an internal copy
1375 CORE.Window.dropFileCount = count;
1376 CORE.Window.dropFilepaths = (char **)RL_CALLOC(CORE.Window.dropFileCount, sizeof(char *));
1378 for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++)
1379 {
1380 CORE.Window.dropFilepaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char));
1381 strncpy(CORE.Window.dropFilepaths[i], paths[i], MAX_FILEPATH_LENGTH - 1);
1382 }
1383 }
1384}
1385*/
1387// Emscripten: Called on key events
1388// TODO: keyCodes should be mapped to raylib/GLFW3 Key values
1389static EM_BOOL EmscriptenKeyboardCallback(int eventType, const EmscriptenKeyboardEvent *keyboardEvent, void *userData)
1390{
1391 switch (eventType)
1392 {
1393 case EMSCRIPTEN_EVENT_KEYPRESS:
1394 {
1395 if (keyboardEvent->repeat) CORE.Input.Keyboard.keyRepeatInFrame[keyboardEvent->keyCode] = 1;
1396 } break;
1397 case EMSCRIPTEN_EVENT_KEYDOWN:
1398 {
1399 CORE.Input.Keyboard.currentKeyState[keyboardEvent->keyCode] = 1;
1400 } break;
1401 case EMSCRIPTEN_EVENT_KEYUP:
1402 {
1403 CORE.Input.Keyboard.currentKeyState[keyboardEvent->keyCode] = 0;
1404 } break;
1405 default: break;
1406 }
1408 // TODO: Add char codes
1409 //unsigned int charCode
1410 // Check if there is space available in the queue for characters to be added
1411 /*
1412 if (CORE.Input.Keyboard.charPressedQueueCount < MAX_CHAR_PRESSED_QUEUE)
1413 {
1414 // Add character to the queue
1415 CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = keyboardEvent->charCode;
1416 CORE.Input.Keyboard.charPressedQueueCount++;
1417 }
1418 */
1419 /*
1420 // Check if there is space available in the key queue
1421 if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (eventType == EMSCRIPTEN_EVENT_KEYPRESS))
1422 {
1423 // Add character to the queue
1424 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = keyboardEvent->keyCode;
1425 CORE.Input.Keyboard.keyPressedQueueCount++;
1426 }
1428 // Check the exit key to set close window
1429 //if ((keyboardEvent->keyCode == CORE.Input.Keyboard.exitKey) && (eventType == EMSCRIPTEN_EVENT_KEYPRESS)) CORE.Window.shouldClose = true;
1430 */
1432 return 1; // The event was consumed by the callback handler
1433}
1435// Emscripten: Called on mouse input events
1436static EM_BOOL EmscriptenMouseCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
1437{
1438 switch (eventType)
1439 {
1440 case EMSCRIPTEN_EVENT_MOUSEENTER: CORE.Input.Mouse.cursorOnScreen = true; break;
1441 case EMSCRIPTEN_EVENT_MOUSELEAVE: CORE.Input.Mouse.cursorOnScreen = false; break;
1442 case EMSCRIPTEN_EVENT_MOUSEDOWN:
1443 {
1444 // NOTE: Emscripten and raylib buttons indices are not aligned
1445 if (mouseEvent->button == 0) CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_LEFT] = 1;
1446 else if (mouseEvent->button == 1) CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_MIDDLE] = 1;
1447 else if (mouseEvent->button == 2) CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_RIGHT] = 1;
1449 //CORE.Input.Touch.currentTouchState[button] = action;
1450 } break;
1451 case EMSCRIPTEN_EVENT_MOUSEUP:
1452 {
1453 if (mouseEvent->button == 0) CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_LEFT] = 0;
1454 else if (mouseEvent->button == 1) CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_MIDDLE] = 0;
1455 else if (mouseEvent->button == 2) CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_RIGHT] = 0;
1456 } break;
1457 default: break;
1458 }
1460#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
1461 // Process mouse events as touches to be able to use mouse-gestures
1462 GestureEvent gestureEvent = { 0 };
1464 // Register touch actions
1465 if ((CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_LEFT] == 1) && (CORE.Input.Mouse.previousButtonState[MOUSE_BUTTON_LEFT] == 0)) gestureEvent.touchAction = TOUCH_ACTION_DOWN;
1466 else if ((CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_LEFT] == 0) && (CORE.Input.Mouse.previousButtonState[MOUSE_BUTTON_LEFT] == 1)) gestureEvent.touchAction = TOUCH_ACTION_UP;
1468 // NOTE: TOUCH_ACTION_MOVE event is registered in MouseMoveCallback()
1470 // Assign a pointer ID
1471 gestureEvent.pointId[0] = 0;
1473 // Register touch points count
1474 gestureEvent.pointCount = 1;
1476 // Register touch points position, only one point registered
1477 gestureEvent.position[0] = GetMousePosition();
1479 // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
1480 gestureEvent.position[0].x /= (float)GetScreenWidth();
1481 gestureEvent.position[0].y /= (float)GetScreenHeight();
1483 // Gesture data is sent to gestures-system for processing
1484 // Prevent calling ProcessGestureEvent() when Emscripten is present and there's a touch gesture, so EmscriptenTouchCallback() can handle it itself
1485 if (GetMouseX() != 0 || GetMouseY() != 0) ProcessGestureEvent(gestureEvent);
1486#endif
1488 return 1; // The event was consumed by the callback handler
1489}
1491// Emscripten: Called on mouse move events
1492static EM_BOOL EmscriptenMouseMoveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
1493{
1494 if (CORE.Input.Mouse.cursorLocked)
1495 {
1496 CORE.Input.Mouse.previousPosition.x = CORE.Input.Mouse.lockedPosition.x - mouseEvent->movementX;
1497 CORE.Input.Mouse.previousPosition.y = CORE.Input.Mouse.lockedPosition.y - mouseEvent->movementY;
1498 }
1499 else
1500 {
1501 // Get mouse position in canvas CSS pixels
1502 float mouseCssX = (float)mouseEvent->canvasX;
1503 float mouseCssY = (float)mouseEvent->canvasY;
1505 // Get canvas sizes
1506 double cssWidth = 0.0;
1507 double cssHeight = 0.0;
1508 emscripten_get_element_css_size(platform.canvasId, &cssWidth, &cssHeight);
1510 int fbWidth = 0;
1511 int fbHeight = 0;
1512 emscripten_get_canvas_element_size(platform.canvasId, &fbWidth, &fbHeight);
1514 // Convert CSS to framebuffer coordinates
1515 float scaleX = (float)fbWidth/(float)cssWidth;
1516 float scaleY = (float)fbHeight/(float)cssHeight;
1518 int mouseX = (int)(mouseCssX*scaleX);
1519 int mouseY = (int)(mouseCssY*scaleY);
1521 CORE.Input.Mouse.currentPosition.x = mouseX;//(float)mouseEvent->canvasX;
1522 CORE.Input.Mouse.currentPosition.y = mouseY;//(float)mouseEvent->canvasY;
1524 // Shorter alternative:
1525 //double dpr = emscripten_get_device_pixel_ratio();
1526 //int mouseX = (int)(e->canvasX*dpr);
1527 //int mouseY = (int)(e->canvasY*dpr);
1529 CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
1530 }
1532#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
1533 // Process mouse events as touches to be able to use mouse-gestures
1534 GestureEvent gestureEvent = { 0 };
1536 gestureEvent.touchAction = TOUCH_ACTION_MOVE;
1538 // Assign a pointer ID
1539 gestureEvent.pointId[0] = 0;
1541 // Register touch points count
1542 gestureEvent.pointCount = 1;
1544 // Register touch points position, only one point registered
1545 gestureEvent.position[0] = CORE.Input.Touch.position[0];
1547 // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
1548 gestureEvent.position[0].x /= (float)GetScreenWidth();
1549 gestureEvent.position[0].y /= (float)GetScreenHeight();
1551 // Gesture data is sent to gestures-system for processing
1552 ProcessGestureEvent(gestureEvent);
1553#endif
1555 return 1; // The event was consumed by the callback handler
1556}
1558// Emscripten: Called on mouse wheel events
1559static EM_BOOL EmscriptenMouseWheelCallback(int eventType, const EmscriptenWheelEvent *wheelEvent, void *userData)
1560{
1561 if (eventType == EMSCRIPTEN_EVENT_WHEEL)
1562 {
1563 CORE.Input.Mouse.currentWheelMove.x = (float)wheelEvent->deltaX;
1564 CORE.Input.Mouse.currentWheelMove.y = (float)wheelEvent->deltaY;
1565 }
1567 return 1; // The event was consumed by the callback handler
1568}
1570// Emscripten: Called on pointer lock events
1571static EM_BOOL EmscriptenPointerlockCallback(int eventType, const EmscriptenPointerlockChangeEvent *pointerlockChangeEvent, void *userData)
1572{
1573 CORE.Input.Mouse.cursorLocked = EM_ASM_INT( { if (document.pointerLockElement) return 1; }, 0);
1575 if (CORE.Input.Mouse.cursorLocked)
1576 {
1577 CORE.Input.Mouse.lockedPosition = CORE.Input.Mouse.currentPosition;
1578 CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.lockedPosition;
1579 }
1581 return 1; // The event was consumed by the callback handler
1582}
1584// Emscripten: Called on connect/disconnect gamepads events
1585static EM_BOOL EmscriptenGamepadCallback(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
1586{
1587 /*
1588 TRACELOGD("%s: timeStamp: %g, connected: %d, index: %ld, numAxes: %d, numButtons: %d, id: \"%s\", mapping: \"%s\"",
1589 eventType != 0? emscripten_event_type_to_string(eventType) : "Gamepad state",
1590 gamepadEvent->timestamp, gamepadEvent->connected, gamepadEvent->index, gamepadEvent->numAxes, gamepadEvent->numButtons, gamepadEvent->id, gamepadEvent->mapping);
1592 for (int i = 0; i < gamepadEvent->numAxes; i++) TRACELOGD("Axis %d: %g", i, gamepadEvent->axis[i]);
1593 for (int i = 0; i < gamepadEvent->numButtons; i++) TRACELOGD("Button %d: Digital: %d, Analog: %g", i, gamepadEvent->digitalButton[i], gamepadEvent->analogButton[i]);
1594 */
1596 if (gamepadEvent->connected && (gamepadEvent->index < MAX_GAMEPADS))
1597 {
1598 CORE.Input.Gamepad.ready[gamepadEvent->index] = true;
1599 snprintf(CORE.Input.Gamepad.name[gamepadEvent->index], MAX_GAMEPAD_NAME_LENGTH, "%s", gamepadEvent->id);
1600 }
1601 else CORE.Input.Gamepad.ready[gamepadEvent->index] = false;
1603 return 1; // The event was consumed by the callback handler
1604}
1606// Emscripten: Called on touch input events
1607static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
1608{
1609 // Register touch points count
1610 CORE.Input.Touch.pointCount = touchEvent->numTouches;
1612 double canvasWidth = 0.0;
1613 double canvasHeight = 0.0;
1614 // NOTE: emscripten_get_canvas_element_size() returns canvas.width and canvas.height but
1615 // we are looking for actual CSS size: canvas.style.width and canvas.style.height
1616 // EMSCRIPTEN_RESULT res = emscripten_get_canvas_element_size("#canvas", &canvasWidth, &canvasHeight);
1617 emscripten_get_element_css_size(platform.canvasId, &canvasWidth, &canvasHeight);
1619 for (int i = 0; (i < CORE.Input.Touch.pointCount) && (i < MAX_TOUCH_POINTS); i++)
1620 {
1621 // Register touch points id
1622 CORE.Input.Touch.pointId[i] = touchEvent->touches[i].identifier;
1624 // Register touch points position
1625 CORE.Input.Touch.position[i] = (Vector2){touchEvent->touches[i].targetX, touchEvent->touches[i].targetY};
1627 // Normalize gestureEvent.position[x] for CORE.Window.screen.width and CORE.Window.screen.height
1628 CORE.Input.Touch.position[i].x *= ((float)GetScreenWidth()/(float)canvasWidth);
1629 CORE.Input.Touch.position[i].y *= ((float)GetScreenHeight()/(float)canvasHeight);
1631 if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) CORE.Input.Touch.currentTouchState[i] = 1;
1632 else if (eventType == EMSCRIPTEN_EVENT_TOUCHEND) CORE.Input.Touch.currentTouchState[i] = 0;
1633 }
1635 // Update mouse position if we detect a single touch
1636 if (CORE.Input.Touch.pointCount == 1)
1637 {
1638 CORE.Input.Mouse.currentPosition.x = CORE.Input.Touch.position[0].x;
1639 CORE.Input.Mouse.currentPosition.y = CORE.Input.Touch.position[0].y;
1640 }
1642#if defined(SUPPORT_GESTURES_SYSTEM)
1643 GestureEvent gestureEvent = { 0 };
1644 gestureEvent.pointCount = CORE.Input.Touch.pointCount;
1646 // Register touch actions
1647 if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) gestureEvent.touchAction = TOUCH_ACTION_DOWN;
1648 else if (eventType == EMSCRIPTEN_EVENT_TOUCHEND) gestureEvent.touchAction = TOUCH_ACTION_UP;
1649 else if (eventType == EMSCRIPTEN_EVENT_TOUCHMOVE) gestureEvent.touchAction = TOUCH_ACTION_MOVE;
1650 else if (eventType == EMSCRIPTEN_EVENT_TOUCHCANCEL) gestureEvent.touchAction = TOUCH_ACTION_CANCEL;
1652 for (int i = 0; (i < gestureEvent.pointCount) && (i < MAX_TOUCH_POINTS); i++)
1653 {
1654 gestureEvent.pointId[i] = CORE.Input.Touch.pointId[i];
1655 gestureEvent.position[i] = CORE.Input.Touch.position[i];
1657 // Normalize gestureEvent.position[i]
1658 gestureEvent.position[i].x /= (float)GetScreenWidth();
1659 gestureEvent.position[i].y /= (float)GetScreenHeight();
1660 }
1662 // Gesture data is sent to gestures system for processing
1663 ProcessGestureEvent(gestureEvent);
1664#endif
1666 if (eventType == EMSCRIPTEN_EVENT_TOUCHEND)
1667 {
1668 // Identify the EMSCRIPTEN_EVENT_TOUCHEND and remove it from the list
1669 for (int i = 0; i < CORE.Input.Touch.pointCount; i++)
1670 {
1671 if (touchEvent->touches[i].isChanged)
1672 {
1673 // Move all touch points one position up
1674 for (int j = i; j < CORE.Input.Touch.pointCount - 1; j++)
1675 {
1676 CORE.Input.Touch.pointId[j] = CORE.Input.Touch.pointId[j + 1];
1677 CORE.Input.Touch.position[j] = CORE.Input.Touch.position[j + 1];
1678 }
1679 // Decrease touch points count to remove the last one
1680 CORE.Input.Touch.pointCount--;
1681 break;
1682 }
1683 }
1684 // Clamp pointCount to avoid negative values
1685 if (CORE.Input.Touch.pointCount < 0) CORE.Input.Touch.pointCount = 0;
1686 }
1688 return 1; // The event was consumed by the callback handler
1689}
1690//-------------------------------------------------------------------------------------------------------
1692// EOF
index : raylib-jai
Bindings from https://solarium.technology