Debian Codegraph @

src/platforms/rcore_desktop_glfw.c

raw symbols SCIP
1 /**********************************************************************************************
2 *
3 * rcore_desktop_glfw - Functions to manage window, graphics device and inputs
4 *
5 * PLATFORM: DESKTOP: GLFW
6 * - Windows (Win32, Win64)
7 * - Linux (X11/Wayland desktop mode)
8 * - FreeBSD, OpenBSD, NetBSD, DragonFly (X11 desktop)
9 * - OSX/macOS (x64, arm64)
10 *
11 * LIMITATIONS:
12 * - Limitation 01
13 * - Limitation 02
14 *
15 * POSSIBLE IMPROVEMENTS:
16 * - Improvement 01
17 * - Improvement 02
18 *
19 * CONFIGURATION:
20 * #define RCORE_PLATFORM_CUSTOM_FLAG
21 * Custom flag for rcore on target platform -not used-
22 *
23 * DEPENDENCIES:
24 * - rglfw: Manage graphic device, OpenGL context and inputs (Windows, Linux, OSX/macOS, FreeBSD...)
25 * - gestures: Gestures system for touch-ready devices (or simulated from mouse inputs)
26 *
27 *
28 * LICENSE: zlib/libpng
29 *
30 * Copyright (c) 2013-2026 Ramon Santamaria (@raysan5) and contributors
31 *
32 * This software is provided "as-is", without any express or implied warranty. In no event
33 * will the authors be held liable for any damages arising from the use of this software.
34 *
35 * Permission is granted to anyone to use this software for any purpose, including commercial
36 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
37 *
38 * 1. The origin of this software must not be misrepresented; you must not claim that you
39 * wrote the original software. If you use this software in a product, an acknowledgment
40 * in the product documentation would be appreciated but is not required.
41 *
42 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
43 * as being the original software.
44 *
45 * 3. This notice may not be removed or altered from any source distribution.
46 *
47 **********************************************************************************************/
48
49 #define GLFW_INCLUDE_NONE // Disable the standard OpenGL header inclusion on GLFW3
50 // NOTE: Already provided by rlgl implementation (on glad.h)
51 #include "GLFW/glfw3.h" // GLFW3 library: Windows, OpenGL context and Input management
52 // NOTE: GLFW3 already includes gl.h (OpenGL) headers
53
54 // Support retrieving native window handlers
55 #if defined(_WIN32)
56 #if !defined(HWND) && !defined(_MSVC_LANG)
57 #define HWND void*
58 #elif !defined(HWND) && defined(_MSVC_LANG)
59 typedef struct HWND__ *HWND;
60 #endif
61
62 #include "../external/win32_clipboard.h" // Clipboard image copy-paste
63
64 #define GLFW_EXPOSE_NATIVE_WIN32
65 #define GLFW_NATIVE_INCLUDE_NONE // To avoid some symbols re-definition in windows.h
66 #include "GLFW/glfw3native.h"
67
68 #if SUPPORT_WINMM_HIGHRES_TIMER && !SUPPORT_BUSY_WAIT_LOOP
69 // NOTE: Those functions require linking with winmm library
70 //#pragma warning(disable: 4273)
71 __declspec(dllimport) unsigned int __stdcall timeEndPeriod(unsigned int uPeriod);
72 //#pragma warning(default: 4273)
73 #endif
74 #endif
75 #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
76 #include <sys/time.h> // Required for: timespec, nanosleep(), select() - POSIX
77
78 #if defined(_GLFW_X11) || defined(_GLFW_WAYLAND)
79 // Set appropriate expose macros based on available backends
80 #if defined(_GLFW_X11)
81 #define GLFW_EXPOSE_NATIVE_X11
82 #define Font X11Font // Hack to fix 'Font' name collision
83 // The definition and references to the X11 Font type will be replaced by 'X11Font'
84 // Works as long as the current file consistently references any X11 Font as X11Font
85 // Since it is never referenced (as of writing), this does not pose an issue
86 #endif
87
88 #if defined(_GLFW_WAYLAND)
89 #define GLFW_EXPOSE_NATIVE_WAYLAND
90 #endif
91
92 #include "GLFW/glfw3native.h" // Include native header only once, regardless of how many backends are defined
93 // Required for: glfwGetX11Window() and glfwGetWaylandWindow()
94 #if defined(_GLFW_X11) // Clean up X11-specific hacks
95 #undef Font // Revert hack and allow normal raylib Font usage
96 #endif
97 #endif
98 #endif
99 #if defined(__APPLE__)
100 #include <unistd.h> // Required for: usleep()
101
102 //#define GLFW_EXPOSE_NATIVE_COCOA // WARNING: Fails due to type redefinition
103 void *glfwGetCocoaWindow(GLFWwindow *handle);
104 #include "GLFW/glfw3native.h" // Required for: glfwGetCocoaWindow()
105 #endif
106
107 #include <stddef.h> // Required for: size_t
108
109 //----------------------------------------------------------------------------------
110 // Types and Structures Definition
111 //----------------------------------------------------------------------------------
112 typedef struct {
113 GLFWwindow *handle; // GLFW window handle (graphic device)
114 } PlatformData;
115
116 //----------------------------------------------------------------------------------
117 // Global Variables Definition
118 //----------------------------------------------------------------------------------
119 extern CoreData CORE; // Global CORE state context
120
121 static PlatformData platform = { 0 }; // Platform specific data
122
123 //----------------------------------------------------------------------------------
124 // Module Internal Functions Declaration
125 //----------------------------------------------------------------------------------
126 int InitPlatform(void); // Initialize platform (graphics, inputs and more)
127 void ClosePlatform(void); // Close platform
128
129 // Error callback event
130 static void ErrorCallback(int error, const char *description); // GLFW3 Error Callback, runs on GLFW3 error
131
132 // Window callbacks events
133 static void WindowSizeCallback(GLFWwindow *window, int width, int height); // GLFW3 WindowSize Callback, runs when window is resized
134 static void FramebufferSizeCallback(GLFWwindow *window, int width, int height); // GLFW3 FramebufferSize Callback, runs when window is resized
135 static void WindowContentScaleCallback(GLFWwindow *window, float scalex, float scaley); // GLFW3 Window Content Scale Callback, runs when window changes scale
136 static void WindowPosCallback(GLFWwindow *window, int x, int y); // GLFW3 WindowPos Callback, runs when window is moved
137 static void WindowIconifyCallback(GLFWwindow *window, int iconified); // GLFW3 WindowIconify Callback, runs when window is minimized/restored
138 static void WindowMaximizeCallback(GLFWwindow *window, int maximized); // GLFW3 Window Maximize Callback, runs when window is maximized
139 static void WindowFocusCallback(GLFWwindow *window, int focused); // GLFW3 WindowFocus Callback, runs when window get/lose focus
140 static void WindowDropCallback(GLFWwindow *window, int count, const char **paths); // GLFW3 Window Drop Callback, runs when drop files into window
141
142 // Input callbacks events
143 static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); // GLFW3 Keyboard Callback, runs on key pressed
144 static void CharCallback(GLFWwindow *window, unsigned int codepoint); // GLFW3 Char Callback, runs on key pressed (get codepoint value)
145 static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods); // GLFW3 Mouse Button Callback, runs on mouse button pressed
146 static void MouseCursorPosCallback(GLFWwindow *window, double x, double y); // GLFW3 Cursor Position Callback, runs on mouse move
147 static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset); // GLFW3 Scrolling Callback, runs on mouse wheel
148 static void CursorEnterCallback(GLFWwindow *window, int enter); // GLFW3 Cursor Enter Callback, cursor enters client area
149 static void JoystickCallback(int jid, int event); // GLFW3 Joystick Connected/Disconnected Callback
150
151 // Memory allocator wrappers [used by glfwInitAllocator()]
152 static void *AllocateWrapper(size_t size, void *user); // GLFW3 GLFWallocatefun, wrapps around RL_CALLOC macro
153 static void *ReallocateWrapper(void *block, size_t size, void *user); // GLFW3 GLFWreallocatefun, wrapps around RL_REALLOC macro
154 static void DeallocateWrapper(void *block, void *user); // GLFW3 GLFWdeallocatefun, wraps around RL_FREE macro
155
156 //----------------------------------------------------------------------------------
157 // Module Functions Declaration
158 //----------------------------------------------------------------------------------
159 // NOTE: Functions declaration is provided by raylib.h
160
161 //----------------------------------------------------------------------------------
162 // Module Functions Definition: Window and Graphics Device
163 //----------------------------------------------------------------------------------
164
165 // Check if application should close
166 // NOTE: By default, if KEY_ESCAPE pressed or window close icon clicked
167 bool WindowShouldClose(void)
168 {
169 if (CORE.Window.ready) return CORE.Window.shouldClose;
170 else return true;
171 }
172
173 // Toggle fullscreen mode
174 void ToggleFullscreen(void)
175 {
176 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
177 {
178 // Store previous screen data (in case exiting fullscreen)
179 CORE.Window.previousPosition = CORE.Window.position;
180 CORE.Window.previousScreen = CORE.Window.screen;
181
182 // Use current monitor the window is on to get fullscreen required size
183 int monitorCount = 0;
184 int monitorIndex = GetCurrentMonitor();
185 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
186 GLFWmonitor *monitor = (monitorIndex < monitorCount)? monitors[monitorIndex] : NULL;
187
188 if (monitor != NULL)
189 {
190 // Get current monitor video mode
191 const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitorIndex]);
192 CORE.Window.display.width = mode->width;
193 CORE.Window.display.height = mode->height;
194
195 CORE.Window.position = (Point){ 0, 0 };
196 CORE.Window.screen = CORE.Window.display;
197
198 // Set fullscreen flag to be processed on FramebufferSizeCallback() accordingly
199 FLAG_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
200
201 #if defined(_GLFW_X11) || defined(_GLFW_WAYLAND)
202 // NOTE: X11 requires undecorating the window before switching to
203 // fullscreen to avoid issues with framebuffer scaling
204 glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_FALSE);
205 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
206 #endif
207 // WARNING: This function launches FramebufferSizeCallback()
208 glfwSetWindowMonitor(platform.handle, monitor, 0, 0, CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
209 }
210 else TRACELOG(LOG_WARNING, "GLFW: Failed to get monitor");
211 }
212 else
213 {
214 // Restore previous window position and size
215 CORE.Window.position = CORE.Window.previousPosition;
216 CORE.Window.screen = CORE.Window.previousScreen;
217
218 // Set fullscreen flag to be processed on FramebufferSizeCallback() accordingly
219 // and considered by GetWindowScaleDPI()
220 FLAG_CLEAR(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
221
222 #if !defined(__APPLE__) && !defined(_GLFW_WAYLAND)
223 // Make sure to restore render size considering HighDPI scaling
224 // NOTE: On Wayland, GLFW_SCALE_FRAMEBUFFER handles scaling, skip manual resize
225 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
226 {
227 Vector2 scaleDpi = GetWindowScaleDPI();
228 CORE.Window.screen.width = (unsigned int)(CORE.Window.screen.width*scaleDpi.x);
229 CORE.Window.screen.height = (unsigned int)(CORE.Window.screen.height*scaleDpi.y);
230 }
231 #endif
232
233 // WARNING: This function launches FramebufferSizeCallback()
234 glfwSetWindowMonitor(platform.handle, NULL, CORE.Window.position.x, CORE.Window.position.y,
235 CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
236
237 #if defined(_GLFW_X11) || defined(_GLFW_WAYLAND)
238 // NOTE: X11 requires restoring the decorated window after switching from
239 // fullscreen to avoid issues with framebuffer scaling
240 glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_TRUE);
241 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
242 #endif
243 }
244
245 // Try to enable GPU V-Sync, so frames are limited to screen refresh rate (60Hz -> 60 FPS)
246 // NOTE: V-Sync can be enabled by graphic driver configuration
247 if (FLAG_IS_SET(CORE.Window.flags, FLAG_VSYNC_HINT)) glfwSwapInterval(1);
248 }
249
250 // Toggle borderless windowed mode
251 void ToggleBorderlessWindowed(void)
252 {
253 // Leave fullscreen before attempting to set borderless windowed mode
254 // NOTE: Fullscreen already saves the previous position so it does not need to be set again later
255 if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE)) ToggleFullscreen();
256
257 int monitorCount = 0;
258 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
259 const int monitor = GetCurrentMonitor();
260
261 if ((monitor >= 0) && (monitor < monitorCount))
262 {
263 const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]);
264
265 if (mode != NULL)
266 {
267 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE))
268 {
269 // Store screen position and size
270 // NOTE: If it was on fullscreen, screen position was already stored, so skip setting it here
271 CORE.Window.previousPosition = CORE.Window.position;
272 CORE.Window.previousScreen = CORE.Window.screen;
273
274 // Set undecorated flag
275 glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_FALSE);
276 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
277
278 // Get monitor position and size
279 glfwGetMonitorPos(monitors[monitor], &CORE.Window.position.x, &CORE.Window.position.y);
280 CORE.Window.screen.width = mode->width;
281 CORE.Window.screen.height = mode->height;
282
283 // Set screen position and size
284 glfwSetWindowMonitor(platform.handle, monitors[monitor], CORE.Window.position.x, CORE.Window.position.y,
285 CORE.Window.screen.width, CORE.Window.screen.height, mode->refreshRate);
286
287 // Refocus window
288 glfwFocusWindow(platform.handle);
289
290 FLAG_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE);
291 }
292 else
293 {
294 // Restore previous screen values
295 CORE.Window.position = CORE.Window.previousPosition;
296 CORE.Window.screen = CORE.Window.previousScreen;
297
298 // Remove undecorated flag
299 glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_TRUE);
300 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
301
302 #if !defined(__APPLE__) && !defined(_GLFW_WAYLAND)
303 // Make sure to restore size considering HighDPI scaling
304 // NOTE: On Wayland, GLFW_SCALE_FRAMEBUFFER handles scaling, skip manual resize
305 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
306 {
307 Vector2 scaleDpi = GetWindowScaleDPI();
308 CORE.Window.screen.width = (unsigned int)(CORE.Window.screen.width*scaleDpi.x);
309 CORE.Window.screen.height = (unsigned int)(CORE.Window.screen.height*scaleDpi.y);
310 }
311 #endif
312
313 // Return to previous screen size and position
314 glfwSetWindowMonitor(platform.handle, NULL, CORE.Window.position.x, CORE.Window.position.y,
315 CORE.Window.screen.width, CORE.Window.screen.height, mode->refreshRate);
316
317 // Refocus window
318 glfwFocusWindow(platform.handle);
319
320 FLAG_CLEAR(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE);
321 }
322 }
323 else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
324 }
325 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
326 }
327
328 // Set window state: maximized, if resizable
329 void MaximizeWindow(void)
330 {
331 if (glfwGetWindowAttrib(platform.handle, GLFW_RESIZABLE) == GLFW_TRUE)
332 {
333 glfwMaximizeWindow(platform.handle);
334 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED);
335 }
336 }
337
338 // Set window state: minimized
339 void MinimizeWindow(void)
340 {
341 // NOTE: Following function launches callback that sets appropriate flag!
342 glfwIconifyWindow(platform.handle);
343 }
344
345 // Restore window from being minimized/maximized
346 void RestoreWindow(void)
347 {
348 if (glfwGetWindowAttrib(platform.handle, GLFW_RESIZABLE) == GLFW_TRUE)
349 {
350 // Restores the specified window if it was previously iconified (minimized) or maximized
351 glfwRestoreWindow(platform.handle);
352 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MINIMIZED);
353 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED);
354 }
355 }
356
357 // Set window configuration state using flags
358 void SetWindowState(unsigned int flags)
359 {
360 if (!CORE.Window.ready) TRACELOG(LOG_WARNING, "WINDOW: SetWindowState does nothing before window initialization, Use \"SetConfigFlags\" instead");
361
362 // Check previous state and requested state to apply required changes
363 // NOTE: In most cases the functions already change the flags internally
364
365 // State change: FLAG_VSYNC_HINT
366 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_VSYNC_HINT) != FLAG_IS_SET(flags, FLAG_VSYNC_HINT)) && FLAG_IS_SET(flags, FLAG_VSYNC_HINT))
367 {
368 glfwSwapInterval(1);
369 FLAG_SET(CORE.Window.flags, FLAG_VSYNC_HINT);
370 }
371
372 // State change: FLAG_BORDERLESS_WINDOWED_MODE
373 // NOTE: This must be handled before FLAG_FULLSCREEN_MODE because ToggleBorderlessWindowed() needs to get some fullscreen values if fullscreen is running
374 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE) != FLAG_IS_SET(flags, FLAG_BORDERLESS_WINDOWED_MODE)) && FLAG_IS_SET(flags, FLAG_BORDERLESS_WINDOWED_MODE))
375 {
376 ToggleBorderlessWindowed(); // NOTE: Window state flag updated inside function
377 }
378
379 // State change: FLAG_FULLSCREEN_MODE
380 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE) != FLAG_IS_SET(flags, FLAG_FULLSCREEN_MODE)) && FLAG_IS_SET(flags, FLAG_FULLSCREEN_MODE))
381 {
382 ToggleFullscreen(); // NOTE: Window state flag updated inside function
383 }
384
385 // State change: FLAG_WINDOW_RESIZABLE
386 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE) != FLAG_IS_SET(flags, FLAG_WINDOW_RESIZABLE)) && FLAG_IS_SET(flags, FLAG_WINDOW_RESIZABLE))
387 {
388 glfwSetWindowAttrib(platform.handle, GLFW_RESIZABLE, GLFW_TRUE);
389 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE);
390 }
391
392 // State change: FLAG_WINDOW_UNDECORATED
393 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED) != FLAG_IS_SET(flags, FLAG_WINDOW_UNDECORATED)) && FLAG_IS_SET(flags, FLAG_WINDOW_UNDECORATED))
394 {
395 glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_FALSE);
396 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
397 }
398
399 // State change: FLAG_WINDOW_HIDDEN
400 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIDDEN) != FLAG_IS_SET(flags, FLAG_WINDOW_HIDDEN)) && FLAG_IS_SET(flags, FLAG_WINDOW_HIDDEN))
401 {
402 glfwHideWindow(platform.handle);
403 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_HIDDEN);
404 }
405
406 // State change: FLAG_WINDOW_MINIMIZED
407 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED) != FLAG_IS_SET(flags, FLAG_WINDOW_MINIMIZED)) && FLAG_IS_SET(flags, FLAG_WINDOW_MINIMIZED))
408 {
409 //GLFW_ICONIFIED
410 MinimizeWindow(); // NOTE: Window state flag updated inside function
411 }
412
413 // State change: FLAG_WINDOW_MAXIMIZED
414 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED) != FLAG_IS_SET(flags, FLAG_WINDOW_MAXIMIZED)) && FLAG_IS_SET(flags, FLAG_WINDOW_MAXIMIZED))
415 {
416 //GLFW_MAXIMIZED
417 MaximizeWindow(); // NOTE: Window state flag updated inside function
418 }
419
420 // State change: FLAG_WINDOW_UNFOCUSED
421 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED) != FLAG_IS_SET(flags, FLAG_WINDOW_UNFOCUSED)) && FLAG_IS_SET(flags, FLAG_WINDOW_UNFOCUSED))
422 {
423 glfwSetWindowAttrib(platform.handle, GLFW_FOCUS_ON_SHOW, GLFW_FALSE);
424 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED);
425 }
426
427 // State change: FLAG_WINDOW_TOPMOST
428 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TOPMOST) != FLAG_IS_SET(flags, FLAG_WINDOW_TOPMOST)) && FLAG_IS_SET(flags, FLAG_WINDOW_TOPMOST))
429 {
430 glfwSetWindowAttrib(platform.handle, GLFW_FLOATING, GLFW_TRUE);
431 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_TOPMOST);
432 }
433
434 // State change: FLAG_WINDOW_ALWAYS_RUN
435 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_ALWAYS_RUN) != FLAG_IS_SET(flags, FLAG_WINDOW_ALWAYS_RUN)) && FLAG_IS_SET(flags, FLAG_WINDOW_ALWAYS_RUN))
436 {
437 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_ALWAYS_RUN);
438 }
439
440 // The following states can not be changed after window creation
441
442 // State change: FLAG_WINDOW_TRANSPARENT
443 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TRANSPARENT) != FLAG_IS_SET(flags, FLAG_WINDOW_TRANSPARENT)) && FLAG_IS_SET(flags, FLAG_WINDOW_TRANSPARENT))
444 {
445 TRACELOG(LOG_WARNING, "WINDOW: Framebuffer transparency can only be configured before window initialization");
446 }
447
448 // State change: FLAG_WINDOW_HIGHDPI
449 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI) != FLAG_IS_SET(flags, FLAG_WINDOW_HIGHDPI)) && FLAG_IS_SET(flags, FLAG_WINDOW_HIGHDPI))
450 {
451 TRACELOG(LOG_WARNING, "WINDOW: High DPI can only be configured before window initialization");
452 }
453
454 // State change: FLAG_WINDOW_MOUSE_PASSTHROUGH
455 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MOUSE_PASSTHROUGH) != FLAG_IS_SET(flags, FLAG_WINDOW_MOUSE_PASSTHROUGH)) && FLAG_IS_SET(flags, FLAG_WINDOW_MOUSE_PASSTHROUGH))
456 {
457 glfwSetWindowAttrib(platform.handle, GLFW_MOUSE_PASSTHROUGH, GLFW_TRUE);
458 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MOUSE_PASSTHROUGH);
459 }
460
461 // State change: FLAG_MSAA_4X_HINT
462 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_MSAA_4X_HINT) != FLAG_IS_SET(flags, FLAG_MSAA_4X_HINT)) && FLAG_IS_SET(flags, FLAG_MSAA_4X_HINT))
463 {
464 TRACELOG(LOG_WARNING, "WINDOW: MSAA can only be configured before window initialization");
465 }
466
467 // State change: FLAG_INTERLACED_HINT
468 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_INTERLACED_HINT) != FLAG_IS_SET(flags, FLAG_INTERLACED_HINT)) && FLAG_IS_SET(flags, FLAG_INTERLACED_HINT))
469 {
470 TRACELOG(LOG_WARNING, "WINDOW: Interlaced mode can only be configured before window initialization");
471 }
472 }
473
474 // Clear window configuration state flags
475 void ClearWindowState(unsigned int flags)
476 {
477 // Check previous state and requested state to apply required changes
478 // NOTE: In most cases the functions already change the flags internally
479
480 // State change: FLAG_VSYNC_HINT
481 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_VSYNC_HINT)) && (FLAG_IS_SET(flags, FLAG_VSYNC_HINT)))
482 {
483 glfwSwapInterval(0);
484 FLAG_CLEAR(CORE.Window.flags, FLAG_VSYNC_HINT);
485 }
486
487 // State change: FLAG_BORDERLESS_WINDOWED_MODE
488 // NOTE: This must be handled before FLAG_FULLSCREEN_MODE because ToggleBorderlessWindowed() needs to get some fullscreen values if fullscreen is running
489 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE)) && (FLAG_IS_SET(flags, FLAG_BORDERLESS_WINDOWED_MODE)))
490 {
491 ToggleBorderlessWindowed(); // NOTE: Window state flag updated inside function
492 }
493
494 // State change: FLAG_FULLSCREEN_MODE
495 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE)) && (FLAG_IS_SET(flags, FLAG_FULLSCREEN_MODE)))
496 {
497 ToggleFullscreen(); // NOTE: Window state flag updated inside function
498 }
499
500 // State change: FLAG_WINDOW_RESIZABLE
501 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE)) && (FLAG_IS_SET(flags, FLAG_WINDOW_RESIZABLE)))
502 {
503 glfwSetWindowAttrib(platform.handle, GLFW_RESIZABLE, GLFW_FALSE);
504 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_RESIZABLE);
505 }
506
507 // State change: FLAG_WINDOW_HIDDEN
508 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIDDEN)) && (FLAG_IS_SET(flags, FLAG_WINDOW_HIDDEN)))
509 {
510 glfwShowWindow(platform.handle);
511 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_HIDDEN);
512 }
513
514 // State change: FLAG_WINDOW_MINIMIZED
515 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED)) && (FLAG_IS_SET(flags, FLAG_WINDOW_MINIMIZED)))
516 {
517 RestoreWindow(); // NOTE: Window state flag updated inside function
518 }
519
520 // State change: FLAG_WINDOW_MAXIMIZED
521 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED)) && (FLAG_IS_SET(flags, FLAG_WINDOW_MAXIMIZED)))
522 {
523 RestoreWindow(); // NOTE: Window state flag updated inside function
524 }
525
526 // State change: FLAG_WINDOW_UNDECORATED
527 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED)) && (FLAG_IS_SET(flags, FLAG_WINDOW_UNDECORATED)))
528 {
529 glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_TRUE);
530 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
531 }
532
533 // State change: FLAG_WINDOW_UNFOCUSED
534 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED)) && (FLAG_IS_SET(flags, FLAG_WINDOW_UNFOCUSED)))
535 {
536 glfwSetWindowAttrib(platform.handle, GLFW_FOCUS_ON_SHOW, GLFW_TRUE);
537 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED);
538 }
539
540 // State change: FLAG_WINDOW_TOPMOST
541 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TOPMOST)) && (FLAG_IS_SET(flags, FLAG_WINDOW_TOPMOST)))
542 {
543 glfwSetWindowAttrib(platform.handle, GLFW_FLOATING, GLFW_FALSE);
544 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_TOPMOST);
545 }
546
547 // State change: FLAG_WINDOW_ALWAYS_RUN
548 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_ALWAYS_RUN)) && (FLAG_IS_SET(flags, FLAG_WINDOW_ALWAYS_RUN)))
549 {
550 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_ALWAYS_RUN);
551 }
552
553 // The following states can not be changed after window creation
554
555 // State change: FLAG_WINDOW_TRANSPARENT
556 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TRANSPARENT)) && (FLAG_IS_SET(flags, FLAG_WINDOW_TRANSPARENT)))
557 {
558 TRACELOG(LOG_WARNING, "WINDOW: Framebuffer transparency can only be configured before window initialization");
559 }
560
561 // State change: FLAG_WINDOW_HIGHDPI
562 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI)) && (FLAG_IS_SET(flags, FLAG_WINDOW_HIGHDPI)))
563 {
564 TRACELOG(LOG_WARNING, "WINDOW: High DPI can only be configured before window initialization");
565 }
566
567 // State change: FLAG_WINDOW_MOUSE_PASSTHROUGH
568 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MOUSE_PASSTHROUGH)) && (FLAG_IS_SET(flags, FLAG_WINDOW_MOUSE_PASSTHROUGH)))
569 {
570 glfwSetWindowAttrib(platform.handle, GLFW_MOUSE_PASSTHROUGH, GLFW_FALSE);
571 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MOUSE_PASSTHROUGH);
572 }
573
574 // State change: FLAG_MSAA_4X_HINT
575 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_MSAA_4X_HINT)) && (FLAG_IS_SET(flags, FLAG_MSAA_4X_HINT)))
576 {
577 TRACELOG(LOG_WARNING, "WINDOW: MSAA can only be configured before window initialization");
578 }
579
580 // State change: FLAG_INTERLACED_HINT
581 if ((FLAG_IS_SET(CORE.Window.flags, FLAG_INTERLACED_HINT)) && (FLAG_IS_SET(flags, FLAG_INTERLACED_HINT)))
582 {
583 TRACELOG(LOG_WARNING, "RPI: Interlaced mode can only be configured before window initialization");
584 }
585 }
586
587 // Set icon for window
588 // NOTE 1: Image must be in RGBA format, 8bit per channel
589 // NOTE 2: Image is scaled by the OS for all required sizes
590 void SetWindowIcon(Image image)
591 {
592 if (image.data == NULL)
593 {
594 // Revert to the default window icon, pass in an empty image array
595 glfwSetWindowIcon(platform.handle, 0, NULL);
596 }
597 else
598 {
599 if (image.format == PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
600 {
601 GLFWimage icon[1] = { 0 };
602
603 icon[0].width = image.width;
604 icon[0].height = image.height;
605 icon[0].pixels = (unsigned char *)image.data;
606
607 // NOTE 1: Only one image icon supported
608 // NOTE 2: The specified image data is copied before this function returns
609 glfwSetWindowIcon(platform.handle, 1, icon);
610 }
611 else TRACELOG(LOG_WARNING, "GLFW: Window icon image must be in R8G8B8A8 pixel format");
612 }
613 }
614
615 // Set icon for window, multiple images
616 // NOTE 1: Images must be in RGBA format, 8bit per channel
617 // NOTE 2: The multiple images are used depending on provided sizes
618 // Standard Windows icon sizes: 256, 128, 96, 64, 48, 32, 24, 16
619 void SetWindowIcons(Image *images, int count)
620 {
621 if ((images == NULL) || (count <= 0))
622 {
623 // Revert to the default window icon, pass in an empty image array
624 glfwSetWindowIcon(platform.handle, 0, NULL);
625 }
626 else
627 {
628 int valid = 0;
629 GLFWimage *icons = (GLFWimage *)RL_CALLOC(count, sizeof(GLFWimage));
630
631 for (int i = 0; i < count; i++)
632 {
633 if (images[i].format == PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
634 {
635 icons[valid].width = images[i].width;
636 icons[valid].height = images[i].height;
637 icons[valid].pixels = (unsigned char *)images[i].data;
638
639 valid++;
640 }
641 else TRACELOG(LOG_WARNING, "GLFW: Window icon image must be in R8G8B8A8 pixel format");
642 }
643 // NOTE: Images data is copied internally before this function returns
644 glfwSetWindowIcon(platform.handle, valid, icons);
645
646 RL_FREE(icons);
647 }
648 }
649
650 // Set title for window
651 void SetWindowTitle(const char *title)
652 {
653 CORE.Window.title = title;
654 glfwSetWindowTitle(platform.handle, title);
655 }
656
657 // Set window position on screen (windowed mode)
658 void SetWindowPosition(int x, int y)
659 {
660 // Update CORE.Window.position as well
661 CORE.Window.position.x = x;
662 CORE.Window.position.y = y;
663 glfwSetWindowPos(platform.handle, x, y);
664 }
665
666 // Set monitor for the current window
667 void SetWindowMonitor(int monitor)
668 {
669 int monitorCount = 0;
670 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
671
672 if ((monitor >= 0) && (monitor < monitorCount))
673 {
674 if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
675 {
676 TRACELOG(LOG_INFO, "GLFW: Selected fullscreen monitor: [%i] %s", monitor, glfwGetMonitorName(monitors[monitor]));
677
678 const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]);
679 glfwSetWindowMonitor(platform.handle, monitors[monitor], 0, 0, mode->width, mode->height, mode->refreshRate);
680 }
681 else
682 {
683 TRACELOG(LOG_INFO, "GLFW: Selected monitor: [%i] %s", monitor, glfwGetMonitorName(monitors[monitor]));
684
685 // Here the render width has to be used again in case high dpi flag is enabled
686 const int screenWidth = CORE.Window.render.width;
687 const int screenHeight = CORE.Window.render.height;
688 int monitorWorkareaX = 0;
689 int monitorWorkareaY = 0;
690 int monitorWorkareaWidth = 0;
691 int monitorWorkareaHeight = 0;
692 glfwGetMonitorWorkarea(monitors[monitor], &monitorWorkareaX, &monitorWorkareaY, &monitorWorkareaWidth, &monitorWorkareaHeight);
693
694 // If the screen size is larger than the monitor workarea, anchor it on the top left corner, otherwise, center it
695 if ((screenWidth >= monitorWorkareaWidth) || (screenHeight >= monitorWorkareaHeight)) glfwSetWindowPos(platform.handle, monitorWorkareaX, monitorWorkareaY);
696 else
697 {
698 const int x = monitorWorkareaX + (monitorWorkareaWidth/2) - (screenWidth/2);
699 const int y = monitorWorkareaY + (monitorWorkareaHeight/2) - (screenHeight/2);
700 glfwSetWindowPos(platform.handle, x, y);
701 }
702 }
703 }
704 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
705 }
706
707 // Set window minimum dimensions (FLAG_WINDOW_RESIZABLE)
708 void SetWindowMinSize(int width, int height)
709 {
710 CORE.Window.screenMin.width = width;
711 CORE.Window.screenMin.height = height;
712
713 int minWidth = (CORE.Window.screenMin.width == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMin.width;
714 int minHeight = (CORE.Window.screenMin.height == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMin.height;
715 int maxWidth = (CORE.Window.screenMax.width == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMax.width;
716 int maxHeight = (CORE.Window.screenMax.height == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMax.height;
717
718 glfwSetWindowSizeLimits(platform.handle, minWidth, minHeight, maxWidth, maxHeight);
719 }
720
721 // Set window maximum dimensions (FLAG_WINDOW_RESIZABLE)
722 void SetWindowMaxSize(int width, int height)
723 {
724 CORE.Window.screenMax.width = width;
725 CORE.Window.screenMax.height = height;
726
727 int minWidth = (CORE.Window.screenMin.width == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMin.width;
728 int minHeight = (CORE.Window.screenMin.height == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMin.height;
729 int maxWidth = (CORE.Window.screenMax.width == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMax.width;
730 int maxHeight = (CORE.Window.screenMax.height == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMax.height;
731
732 glfwSetWindowSizeLimits(platform.handle, minWidth, minHeight, maxWidth, maxHeight);
733 }
734
735 // Set window dimensions
736 void SetWindowSize(int width, int height)
737 {
738 CORE.Window.screen.width = width;
739 CORE.Window.screen.height = height;
740
741 glfwSetWindowSize(platform.handle, width, height);
742 }
743
744 // Set window opacity, value opacity is between 0.0 and 1.0
745 void SetWindowOpacity(float opacity)
746 {
747 if (opacity >= 1.0f) opacity = 1.0f;
748 else if (opacity <= 0.0f) opacity = 0.0f;
749 glfwSetWindowOpacity(platform.handle, opacity);
750 }
751
752 // Set window focused
753 void SetWindowFocused(void)
754 {
755 glfwFocusWindow(platform.handle);
756 }
757
758 #if defined(__linux__) && defined(_GLFW_X11)
759 // Local storage for the window handle returned by glfwGetX11Window
760 // This is needed as X11 handles are integers and may not fit inside a pointer depending on platform
761 // Storing the handle locally and returning a pointer in GetWindowHandle allows the code to work regardless of pointer width
762 static XID X11WindowHandle;
763 #endif
764 // Get native window handle
765 void *GetWindowHandle(void)
766 {
767 #if defined(_WIN32)
768 // NOTE: Returned handle is: void *HWND (windows.h)
769 return glfwGetWin32Window(platform.handle);
770 #endif
771 #if defined(__linux__)
772 #if defined(_GLFW_WAYLAND)
773 #if defined(_GLFW_X11)
774 int platformID = glfwGetPlatform();
775 if (platformID == GLFW_PLATFORM_WAYLAND)
776 {
777 return glfwGetWaylandWindow(platform.handle);
778 }
779 else
780 {
781 X11WindowHandle = glfwGetX11Window(platform.handle);
782 return &X11WindowHandle;
783 }
784 #else
785 return glfwGetWaylandWindow(platform.handle);
786 #endif
787 #elif defined(_GLFW_X11)
788 // Store the window handle localy and return a pointer to the variable instead
789 // Reasoning detailed in the declaration of X11WindowHandle
790 X11WindowHandle = glfwGetX11Window(platform.handle);
791 return &X11WindowHandle;
792 #endif
793 #endif
794 #if defined(__APPLE__)
795 // NOTE: Returned handle is: (objc_object *)
796 return (void *)glfwGetCocoaWindow(platform.handle);
797 #endif
798
799 return NULL;
800 }
801
802 // Get number of monitors
803 int GetMonitorCount(void)
804 {
805 int monitorCount = 0;
806
807 glfwGetMonitors(&monitorCount);
808
809 return monitorCount;
810 }
811
812 // Get current monitor where window is placed
813 int GetCurrentMonitor(void)
814 {
815 int index = 0;
816 int monitorCount = 0;
817 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
818 GLFWmonitor *monitor = NULL;
819
820 if (monitorCount >= 1)
821 {
822 if (IsWindowFullscreen())
823 {
824 // Get the handle of the monitor that the specified window is in full screen on
825 monitor = glfwGetWindowMonitor(platform.handle);
826
827 for (int i = 0; i < monitorCount; i++)
828 {
829 if (monitors[i] == monitor)
830 {
831 index = i;
832 break;
833 }
834 }
835 }
836 else
837 {
838 // In case the window is between two monitors, below logic is used
839 // to try to detect the "current monitor" for that window, note that
840 // this is probably an overengineered solution for a side case
841 // trying to match SDL behaviour
842
843 int closestDist = 0x7FFFFFFF;
844
845 // Window center position
846 int wcx = 0;
847 int wcy = 0;
848
849 glfwGetWindowPos(platform.handle, &wcx, &wcy);
850 wcx += (int)CORE.Window.screen.width/2;
851 wcy += (int)CORE.Window.screen.height/2;
852
853 for (int i = 0; i < monitorCount; i++)
854 {
855 // Monitor top-left position
856 int mx = 0;
857 int my = 0;
858
859 monitor = monitors[i];
860 glfwGetMonitorPos(monitor, &mx, &my);
861 const GLFWvidmode *mode = glfwGetVideoMode(monitor);
862
863 if (mode)
864 {
865 const int right = mx + mode->width - 1;
866 const int bottom = my + mode->height - 1;
867
868 if ((wcx >= mx) &&
869 (wcx <= right) &&
870 (wcy >= my) &&
871 (wcy <= bottom))
872 {
873 index = i;
874 break;
875 }
876
877 int xclosest = wcx;
878 if (wcx < mx) xclosest = mx;
879 else if (wcx > right) xclosest = right;
880
881 int yclosest = wcy;
882 if (wcy < my) yclosest = my;
883 else if (wcy > bottom) yclosest = bottom;
884
885 int dx = wcx - xclosest;
886 int dy = wcy - yclosest;
887 int dist = (dx*dx) + (dy*dy);
888 if (dist < closestDist)
889 {
890 index = i;
891 closestDist = dist;
892 }
893 }
894 else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
895 }
896 }
897 }
898
899 return index;
900 }
901
902 // Get selected monitor position
903 Vector2 GetMonitorPosition(int monitor)
904 {
905 int monitorCount = 0;
906 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
907
908 if ((monitor >= 0) && (monitor < monitorCount))
909 {
910 int x = 0;
911 int y = 0;
912 glfwGetMonitorPos(monitors[monitor], &x, &y);
913
914 return (Vector2){ (float)x, (float)y };
915 }
916 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
917 return (Vector2){ 0, 0 };
918 }
919
920 // Get selected monitor width (currently used by monitor)
921 int GetMonitorWidth(int monitor)
922 {
923 int width = 0;
924 int monitorCount = 0;
925 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
926
927 if ((monitor >= 0) && (monitor < monitorCount))
928 {
929 const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]);
930
931 if (mode) width = mode->width;
932 else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
933 }
934 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
935
936 return width;
937 }
938
939 // Get selected monitor height (currently used by monitor)
940 int GetMonitorHeight(int monitor)
941 {
942 int height = 0;
943 int monitorCount = 0;
944 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
945
946 if ((monitor >= 0) && (monitor < monitorCount))
947 {
948 const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]);
949
950 if (mode) height = mode->height;
951 else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
952 }
953 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
954
955 return height;
956 }
957
958 // Get selected monitor physical width in millimetres
959 int GetMonitorPhysicalWidth(int monitor)
960 {
961 int width = 0;
962 int monitorCount = 0;
963 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
964
965 if ((monitor >= 0) && (monitor < monitorCount)) glfwGetMonitorPhysicalSize(monitors[monitor], &width, NULL);
966 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
967
968 return width;
969 }
970
971 // Get selected monitor physical height in millimetres
972 int GetMonitorPhysicalHeight(int monitor)
973 {
974 int height = 0;
975 int monitorCount = 0;
976 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
977
978 if ((monitor >= 0) && (monitor < monitorCount)) glfwGetMonitorPhysicalSize(monitors[monitor], NULL, &height);
979 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
980
981 return height;
982 }
983
984 // Get selected monitor refresh rate
985 int GetMonitorRefreshRate(int monitor)
986 {
987 int refresh = 0;
988 int monitorCount = 0;
989 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
990
991 if ((monitor >= 0) && (monitor < monitorCount))
992 {
993 const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]);
994
995 if (mode) refresh = mode->refreshRate;
996 else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
997
998 }
999 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
1000
1001 return refresh;
1002 }
1003
1004 // Get the human-readable, UTF-8 encoded name of the selected monitor
1005 const char *GetMonitorName(int monitor)
1006 {
1007 int monitorCount = 0;
1008 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
1009
1010 if ((monitor >= 0) && (monitor < monitorCount))
1011 {
1012 return glfwGetMonitorName(monitors[monitor]);
1013 }
1014 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
1015 return "";
1016 }
1017
1018 // Get window position XY on monitor
1019 Vector2 GetWindowPosition(void)
1020 {
1021 return (Vector2){ (float)CORE.Window.position.x, (float)CORE.Window.position.y };
1022 }
1023
1024 // Get window scale DPI factor for current monitor
1025 Vector2 GetWindowScaleDPI(void)
1026 {
1027 Vector2 scale = { 1.0f, 1.0f };
1028 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI) && !FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
1029 glfwGetWindowContentScale(platform.handle, &scale.x, &scale.y);
1030 return scale;
1031 }
1032
1033 // Set clipboard text content
1034 void SetClipboardText(const char *text)
1035 {
1036 glfwSetClipboardString(platform.handle, text);
1037 }
1038
1039 // Get clipboard text content
1040 // NOTE: returned string is allocated and freed by GLFW
1041 const char *GetClipboardText(void)
1042 {
1043 return glfwGetClipboardString(platform.handle);
1044 }
1045
1046 #if SUPPORT_CLIPBOARD_IMAGE && defined(__linux__) && defined(_GLFW_X11)
1047 #include <X11/Xlib.h>
1048 #include <X11/Xatom.h>
1049 #endif
1050
1051 // Get clipboard image
1052 Image GetClipboardImage(void)
1053 {
1054 Image image = { 0 };
1055
1056 #if SUPPORT_CLIPBOARD_IMAGE && SUPPORT_MODULE_RTEXTURES
1057 #if defined(_WIN32)
1058 unsigned long long int dataSize = 0;
1059 void *bmpData = NULL;
1060 int width = 0;
1061 int height = 0;
1062
1063 bmpData = (void *)Win32GetClipboardImageData(&width, &height, &dataSize);
1064
1065 if (bmpData == NULL) TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data.");
1066 else image = LoadImageFromMemory(".bmp", (const unsigned char *)bmpData, (int)dataSize);
1067
1068 #elif defined(__linux__) && defined(_GLFW_X11)
1069
1070 // REF: https://github.com/ColleagueRiley/Clipboard-Copy-Paste/blob/main/x11.c
1071 Display *dpy = XOpenDisplay(NULL);
1072 if (!dpy) return image;
1073
1074 Window root = DefaultRootWindow(dpy);
1075 Window win = XCreateSimpleWindow(
1076 dpy, // The connection to the X Server
1077 root, // The 'Parent' window (usually the desktop/root)
1078 0, 0, // X and Y position on the screen
1079 1, 1, // Width and Height (1x1 pixel)
1080 0, // Border width
1081 0, // Border color
1082 0 // Background color
1083 );
1084
1085 Atom clipboard = XInternAtom(dpy, "CLIPBOARD", False);
1086 Atom targetType = XInternAtom(dpy, "image/png", False); // Ask for PNG
1087 Atom property = XInternAtom(dpy, "RAYLIB_CLIPBOARD_MANAGER", False);
1088
1089 // Request the data: "Convert whatever is in CLIPBOARD to image/png and put it in RAYLIB_CLIPBOARD_MANAGER"
1090 XConvertSelection(dpy, clipboard, targetType, property, win, CurrentTime);
1091
1092 // Wait for the SelectionNotify event
1093 XEvent ev = { 0 };
1094 XNextEvent(dpy, &ev);
1095
1096 Atom actualType = { 0 };
1097 int actualFormat = 0;
1098 unsigned long nitems = 0;
1099 unsigned long bytesAfter = 0;
1100 unsigned char *data = NULL;
1101
1102 // Read the data from our ghost window's property
1103 XGetWindowProperty(dpy, win, property, 0, ~0L, False, AnyPropertyType,
1104 &actualType, &actualFormat, &nitems, &bytesAfter, &data);
1105
1106 if (data != NULL)
1107 {
1108 image = LoadImageFromMemory(".png", data, (int)nitems);
1109 XFree(data);
1110 }
1111
1112 XDestroyWindow(dpy, win);
1113 XCloseDisplay(dpy);
1114 #else
1115 TRACELOG(LOG_WARNING, "GetClipboardImage() not implemented on target platform");
1116 #endif // defined(_WIN32)
1117 #else
1118 TRACELOG(LOG_WARNING, "Clipboard image: SUPPORT_CLIPBOARD_IMAGE requires SUPPORT_MODULE_RTEXTURES to work properly");
1119 #endif // SUPPORT_CLIPBOARD_IMAGE
1120
1121 return image;
1122 }
1123
1124 // Show mouse cursor
1125 void ShowCursor(void)
1126 {
1127 glfwSetInputMode(platform.handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
1128
1129 CORE.Input.Mouse.cursorHidden = false;
1130 }
1131
1132 // Hides mouse cursor
1133 void HideCursor(void)
1134 {
1135 glfwSetInputMode(platform.handle, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
1136
1137 CORE.Input.Mouse.cursorHidden = true;
1138 }
1139
1140 // Enables cursor (unlock cursor)
1141 void EnableCursor(void)
1142 {
1143 glfwSetInputMode(platform.handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
1144
1145 // Set cursor position in the middle
1146 SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
1147
1148 if (glfwRawMouseMotionSupported()) glfwSetInputMode(platform.handle, GLFW_RAW_MOUSE_MOTION, GLFW_FALSE);
1149
1150 CORE.Input.Mouse.cursorHidden = false;
1151 CORE.Input.Mouse.cursorLocked = false;
1152 }
1153
1154 // Disables cursor (lock cursor)
1155 void DisableCursor(void)
1156 {
1157 // Reset mouse position within the window area before disabling cursor
1158 SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
1159
1160 glfwSetInputMode(platform.handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
1161
1162 if (glfwRawMouseMotionSupported()) glfwSetInputMode(platform.handle, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
1163
1164 CORE.Input.Mouse.cursorHidden = true;
1165 CORE.Input.Mouse.cursorLocked = true;
1166 }
1167
1168 // Swap back buffer with front buffer (screen drawing)
1169 void SwapScreenBuffer(void)
1170 {
1171 glfwSwapBuffers(platform.handle);
1172 }
1173
1174 //----------------------------------------------------------------------------------
1175 // Module Functions Definition: Misc
1176 //----------------------------------------------------------------------------------
1177
1178 // Get elapsed time measure in seconds since InitTimer()
1179 double GetTime(void)
1180 {
1181 double time = glfwGetTime(); // Elapsed time since glfwInit()
1182
1183 return time;
1184 }
1185
1186 // Open URL with default system browser (if available)
1187 // NOTE: This function is only safe to use if you control the URL given
1188 // A user could craft a malicious string performing another action
1189 // Only call this function yourself not with user input or make sure to check the string yourself
1190 // REF: https://github.com/raysan5/raylib/issues/686
1191 void OpenURL(const char *url)
1192 {
1193 // Security check to (partially) avoid malicious code
1194 if (strchr(url, '\'') != NULL) TRACELOG(LOG_WARNING, "SYSTEM: Provided URL could be potentially malicious, avoid [\'] character");
1195 else
1196 {
1197 char *cmd = (char *)RL_CALLOC(strlen(url) + 32, sizeof(char));
1198 #if defined(_WIN32)
1199 sprintf(cmd, "explorer \"%s\"", url);
1200 #endif
1201 #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
1202 sprintf(cmd, "xdg-open '%s'", url); // Alternatives: firefox, x-www-browser
1203 #endif
1204 #if defined(__APPLE__)
1205 sprintf(cmd, "open '%s'", url);
1206 #endif
1207 int result = system(cmd);
1208 if (result == -1) TRACELOG(LOG_WARNING, "OpenURL() child process could not be created");
1209 RL_FREE(cmd);
1210 }
1211 }
1212
1213 //----------------------------------------------------------------------------------
1214 // Module Functions Definition: Inputs
1215 //----------------------------------------------------------------------------------
1216
1217 // Set internal gamepad mappings
1218 int SetGamepadMappings(const char *mappings)
1219 {
1220 return glfwUpdateGamepadMappings(mappings);
1221 }
1222
1223 // Set gamepad vibration
1224 void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration)
1225 {
1226 TRACELOG(LOG_WARNING, "SetGamepadVibration() not available on target platform");
1227 }
1228
1229 // Set mouse position XY
1230 void SetMousePosition(int x, int y)
1231 {
1232 CORE.Input.Mouse.currentPosition = (Vector2){ (float)x, (float)y };
1233 CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
1234
1235 // NOTE: emscripten not implemented
1236 glfwSetCursorPos(platform.handle, CORE.Input.Mouse.currentPosition.x, CORE.Input.Mouse.currentPosition.y);
1237 }
1238
1239 // Set mouse cursor
1240 void SetMouseCursor(int cursor)
1241 {
1242 CORE.Input.Mouse.cursor = cursor;
1243 if (cursor == MOUSE_CURSOR_DEFAULT) glfwSetCursor(platform.handle, NULL);
1244 else
1245 {
1246 // NOTE: Mapping internal GLFW enum values to MouseCursor enum values
1247 glfwSetCursor(platform.handle, glfwCreateStandardCursor(0x00036000 + cursor));
1248 }
1249 }
1250
1251 // Get physical key name
1252 const char *GetKeyName(int key)
1253 {
1254 return glfwGetKeyName(key, glfwGetKeyScancode(key));
1255 }
1256
1257 // Register all input events
1258 void PollInputEvents(void)
1259 {
1260 #if SUPPORT_GESTURES_SYSTEM
1261 // NOTE: Gestures update must be called every frame to reset gestures correctly
1262 // because ProcessGestureEvent() is called on an event, not every frame
1263 UpdateGestures();
1264 #endif
1265
1266 // Reset keys/chars pressed registered
1267 CORE.Input.Keyboard.keyPressedQueueCount = 0;
1268 CORE.Input.Keyboard.charPressedQueueCount = 0;
1269
1270 // Reset last gamepad button/axis registered state
1271 CORE.Input.Gamepad.lastButtonPressed = GAMEPAD_BUTTON_UNKNOWN;
1272 //CORE.Input.Gamepad.axisCount = 0;
1273
1274 // Keyboard/Mouse input polling (automatically managed by GLFW3 through callback)
1275
1276 // Register previous keys states
1277 for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)
1278 {
1279 CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
1280 CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
1281 }
1282
1283 // Register previous mouse states
1284 for (int i = 0; i < MAX_MOUSE_BUTTONS; i++) CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
1285
1286 // Register previous mouse wheel state
1287 CORE.Input.Mouse.previousWheelMove = CORE.Input.Mouse.currentWheelMove;
1288 CORE.Input.Mouse.currentWheelMove = (Vector2){ 0.0f, 0.0f };
1289
1290 // Register previous mouse position
1291 CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
1292
1293 // Register previous touch states
1294 for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i];
1295
1296 // Reset touch positions
1297 //for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.position[i] = (Vector2){ 0, 0 };
1298
1299 // Map touch position to mouse position for convenience
1300 // WARNING: If the target desktop device supports touch screen, this behaviour should be reviewed!
1301 // TODO: GLFW does not support multi-touch input yet
1302 // REF: https://www.codeproject.com/Articles/668404/Programming-for-Multi-Touch
1303 // REF: https://docs.microsoft.com/en-us/windows/win32/wintouch/getting-started-with-multi-touch-messages
1304 CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
1305
1306 // Check if gamepads are ready
1307 // NOTE: Doing it here in case of disconnection
1308 for (int i = 0; i < MAX_GAMEPADS; i++)
1309 {
1310 if (glfwJoystickPresent(i)) CORE.Input.Gamepad.ready[i] = true;
1311 else CORE.Input.Gamepad.ready[i] = false;
1312 }
1313
1314 // Register gamepads buttons events
1315 for (int i = 0; i < MAX_GAMEPADS; i++)
1316 {
1317 if (CORE.Input.Gamepad.ready[i]) // Check if gamepad is available
1318 {
1319 // Register previous gamepad states
1320 for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) CORE.Input.Gamepad.previousButtonState[i][k] = CORE.Input.Gamepad.currentButtonState[i][k];
1321
1322 // Get current gamepad state
1323 // NOTE: There is no callback available, getting it manually
1324 GLFWgamepadstate state = { 0 };
1325 int result = glfwGetGamepadState(i, &state); // This remaps all gamepads so they have their buttons mapped like an xbox controller
1326 if (result == GLFW_FALSE) // No joystick is connected, no gamepad mapping or an error occurred
1327 {
1328 // Setting axes to expected resting value instead of GLFW 0.0f default when gamepad is not connected
1329 state.axes[GAMEPAD_AXIS_LEFT_TRIGGER] = -1.0f;
1330 state.axes[GAMEPAD_AXIS_RIGHT_TRIGGER] = -1.0f;
1331 }
1332
1333 const unsigned char *buttons = state.buttons;
1334
1335 for (int k = 0; (buttons != NULL) && (k < MAX_GAMEPAD_BUTTONS); k++)
1336 {
1337 int button = -1; // GamepadButton enum values assigned
1338
1339 switch (k)
1340 {
1341 case GLFW_GAMEPAD_BUTTON_Y: button = GAMEPAD_BUTTON_RIGHT_FACE_UP; break;
1342 case GLFW_GAMEPAD_BUTTON_B: button = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT; break;
1343 case GLFW_GAMEPAD_BUTTON_A: button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN; break;
1344 case GLFW_GAMEPAD_BUTTON_X: button = GAMEPAD_BUTTON_RIGHT_FACE_LEFT; break;
1345
1346 case GLFW_GAMEPAD_BUTTON_LEFT_BUMPER: button = GAMEPAD_BUTTON_LEFT_TRIGGER_1; break;
1347 case GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER: button = GAMEPAD_BUTTON_RIGHT_TRIGGER_1; break;
1348
1349 case GLFW_GAMEPAD_BUTTON_BACK: button = GAMEPAD_BUTTON_MIDDLE_LEFT; break;
1350 case GLFW_GAMEPAD_BUTTON_GUIDE: button = GAMEPAD_BUTTON_MIDDLE; break;
1351 case GLFW_GAMEPAD_BUTTON_START: button = GAMEPAD_BUTTON_MIDDLE_RIGHT; break;
1352
1353 case GLFW_GAMEPAD_BUTTON_DPAD_UP: button = GAMEPAD_BUTTON_LEFT_FACE_UP; break;
1354 case GLFW_GAMEPAD_BUTTON_DPAD_RIGHT: button = GAMEPAD_BUTTON_LEFT_FACE_RIGHT; break;
1355 case GLFW_GAMEPAD_BUTTON_DPAD_DOWN: button = GAMEPAD_BUTTON_LEFT_FACE_DOWN; break;
1356 case GLFW_GAMEPAD_BUTTON_DPAD_LEFT: button = GAMEPAD_BUTTON_LEFT_FACE_LEFT; break;
1357
1358 case GLFW_GAMEPAD_BUTTON_LEFT_THUMB: button = GAMEPAD_BUTTON_LEFT_THUMB; break;
1359 case GLFW_GAMEPAD_BUTTON_RIGHT_THUMB: button = GAMEPAD_BUTTON_RIGHT_THUMB; break;
1360 default: break;
1361 }
1362
1363 if (button != -1) // Check for valid button
1364 {
1365 if (buttons[k] == GLFW_PRESS)
1366 {
1367 CORE.Input.Gamepad.currentButtonState[i][button] = 1;
1368 CORE.Input.Gamepad.lastButtonPressed = button;
1369 }
1370 else CORE.Input.Gamepad.currentButtonState[i][button] = 0;
1371 }
1372 }
1373
1374 // Get current state of axes
1375 const float *axes = state.axes;
1376
1377 for (int k = 0; (axes != NULL) && (k < GLFW_GAMEPAD_AXIS_LAST + 1); k++)
1378 {
1379 CORE.Input.Gamepad.axisState[i][k] = axes[k];
1380 }
1381
1382 // Register buttons for 2nd triggers (because GLFW doesn't count these as buttons but rather as axes)
1383 if (CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_LEFT_TRIGGER] > 0.1f)
1384 {
1385 CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_LEFT_TRIGGER_2] = 1;
1386 CORE.Input.Gamepad.lastButtonPressed = GAMEPAD_BUTTON_LEFT_TRIGGER_2;
1387 }
1388 else CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_LEFT_TRIGGER_2] = 0;
1389 if (CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_RIGHT_TRIGGER] > 0.1f)
1390 {
1391 CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_RIGHT_TRIGGER_2] = 1;
1392 CORE.Input.Gamepad.lastButtonPressed = GAMEPAD_BUTTON_RIGHT_TRIGGER_2;
1393 }
1394 else CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_RIGHT_TRIGGER_2] = 0;
1395
1396 CORE.Input.Gamepad.axisCount[i] = GLFW_GAMEPAD_AXIS_LAST + 1;
1397 }
1398 }
1399
1400 CORE.Window.resizedLastFrame = false;
1401
1402 if ((CORE.Window.eventWaiting) ||
1403 (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED) && !FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_ALWAYS_RUN)))
1404 {
1405 glfwWaitEvents(); // Wait for in input events before continue (drawing is paused)
1406 CORE.Time.previous = GetTime();
1407 }
1408 else glfwPollEvents(); // Poll input events: keyboard/mouse/window events (callbacks) -> Update keys state
1409
1410 CORE.Window.shouldClose = glfwWindowShouldClose(platform.handle);
1411
1412 // Reset close status for next frame
1413 glfwSetWindowShouldClose(platform.handle, GLFW_FALSE);
1414 }
1415
1416 //----------------------------------------------------------------------------------
1417 // Module Internal Functions Definition
1418 //----------------------------------------------------------------------------------
1419 // Function wrappers around RL_*ALLOC macros, used by glfwInitAllocator() inside of InitPlatform()
1420 // GLFWallocator expects function pointers with specific signatures to be provided
1421 // REF: https://www.glfw.org/docs/latest/intro_guide.html#init_allocator
1422 static void *AllocateWrapper(size_t size, void *user)
1423 {
1424 (void)user;
1425 return RL_CALLOC(size, 1);
1426 }
1427 static void *ReallocateWrapper(void *block, size_t size, void *user)
1428 {
1429 (void)user;
1430 return RL_REALLOC(block, size);
1431 }
1432 static void DeallocateWrapper(void *block, void *user)
1433 {
1434 (void)user;
1435 RL_FREE(block);
1436 }
1437
1438 // Initialize platform: graphics, inputs and more
1439 int InitPlatform(void)
1440 {
1441 glfwSetErrorCallback(ErrorCallback);
1442
1443 const GLFWallocator allocator = {
1444 .allocate = AllocateWrapper,
1445 .reallocate = ReallocateWrapper,
1446 .deallocate = DeallocateWrapper,
1447 .user = NULL, // RL_*ALLOC macros are not capable of handling user-provided data
1448 };
1449
1450 glfwInitAllocator(&allocator);
1451
1452 #if defined(__APPLE__)
1453 glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, GLFW_FALSE);
1454 #endif
1455 // Initialize GLFW internal global state
1456 int result = glfwInit();
1457 if (result == GLFW_FALSE) { TRACELOG(LOG_WARNING, "GLFW: Failed to initialize GLFW"); return -1; }
1458
1459 // Initialize graphic device: display/window and graphic context
1460 //----------------------------------------------------------------------------
1461 glfwDefaultWindowHints(); // Set default windows hints
1462 //glfwWindowHint(GLFW_RED_BITS, 8); // Framebuffer red color component bits
1463 //glfwWindowHint(GLFW_GREEN_BITS, 8); // Framebuffer green color component bits
1464 //glfwWindowHint(GLFW_BLUE_BITS, 8); // Framebuffer blue color component bits
1465 //glfwWindowHint(GLFW_ALPHA_BITS, 8); // Framebuffer alpha color component bits
1466 //glfwWindowHint(GLFW_DEPTH_BITS, 24); // Depthbuffer bits
1467 //glfwWindowHint(GLFW_REFRESH_RATE, 0); // Refresh rate for fullscreen window
1468 //glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API); // OpenGL API to use. Alternative: GLFW_OPENGL_ES_API
1469 //glfwWindowHint(GLFW_AUX_BUFFERS, 0); // Number of auxiliar buffers
1470
1471 // Disable GlFW auto iconify behaviour
1472 // Auto Iconify automatically minimizes (iconifies) the window if the window loses focus
1473 // additionally auto iconify restores the hardware resolution of the monitor if the window that loses focus is a fullscreen window
1474 glfwWindowHint(GLFW_AUTO_ICONIFY, 0);
1475
1476 // Window flags requested before initialization to be applied after initialization
1477 unsigned int requestedWindowFlags = CORE.Window.flags;
1478
1479 // Check window creation flags
1480 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIDDEN)) glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // Visible window
1481 else glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE); // Window initially hidden
1482
1483 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED)) glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); // Border and buttons on Window
1484 else glfwWindowHint(GLFW_DECORATED, GLFW_TRUE); // Decorated window
1485
1486 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE)) glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // Resizable window
1487 else glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // Avoid window being resizable
1488
1489 // Disable FLAG_WINDOW_MINIMIZED, not supported on initialization
1490 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED)) FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MINIMIZED);
1491
1492 // Disable FLAG_WINDOW_MAXIMIZED, not supported on initialization
1493 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED)) FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED);
1494
1495 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED)) glfwWindowHint(GLFW_FOCUSED, GLFW_FALSE);
1496 else glfwWindowHint(GLFW_FOCUSED, GLFW_TRUE);
1497
1498 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TOPMOST)) glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
1499 else glfwWindowHint(GLFW_FLOATING, GLFW_FALSE);
1500
1501 // NOTE: Some GLFW flags are not supported on HTML5
1502 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TRANSPARENT)) glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); // Transparent framebuffer
1503 else glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_FALSE); // Opaque framebuffer
1504
1505 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
1506 {
1507 #if defined(__APPLE__)
1508 glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_FALSE);
1509 #endif
1510 // Resize window content area based on the monitor content scale
1511 // NOTE: This hint only has an effect on platforms where screen coordinates and
1512 // pixels always map 1:1 such as Windows and X11
1513 // On platforms like macOS the resolution of the framebuffer is changed independently of the window size
1514 glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
1515 #if defined(__APPLE__)
1516 glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE);
1517 #endif
1518 }
1519 else
1520 {
1521 glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_FALSE);
1522 #if defined(__APPLE__)
1523 glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_FALSE);
1524 #endif
1525 #if defined(_GLFW_WAYLAND) && !defined(_GLFW_X11)
1526 // GLFW 3.4+ defaults GLFW_SCALE_FRAMEBUFFER to TRUE,
1527 // causing framebuffer/window size mismatch on Wayland with display scaling
1528 glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_FALSE);
1529 #endif
1530 }
1531
1532 // Mouse passthrough
1533 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MOUSE_PASSTHROUGH)) glfwWindowHint(GLFW_MOUSE_PASSTHROUGH, GLFW_TRUE);
1534 else glfwWindowHint(GLFW_MOUSE_PASSTHROUGH, GLFW_FALSE);
1535
1536 if (FLAG_IS_SET(CORE.Window.flags, FLAG_MSAA_4X_HINT))
1537 {
1538 // NOTE: MSAA is only enabled for main framebuffer, not user-created FBOs
1539 TRACELOG(LOG_INFO, "DISPLAY: Trying to enable MSAA x4");
1540 glfwWindowHint(GLFW_SAMPLES, 4); // Tries to enable multisampling x4 (MSAA), default is 0
1541 }
1542
1543 // NOTE: When asking for an OpenGL context version, most drivers provide the highest supported version
1544 // with backward compatibility to older OpenGL versions
1545 // For example, if using OpenGL 1.1, driver can provide a 4.3 backwards compatible context
1546
1547 // Check selection OpenGL version
1548 if (rlGetVersion() == RL_OPENGL_21)
1549 {
1550 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); // Choose OpenGL major version (just hint)
1551 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); // Choose OpenGL minor version (just hint)
1552 }
1553 else if (rlGetVersion() == RL_OPENGL_33)
1554 {
1555 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Choose OpenGL major version (just hint)
1556 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Choose OpenGL minor version (just hint)
1557 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Profiles Hint: Only 3.3 and above!
1558 // Values: GLFW_OPENGL_CORE_PROFILE, GLFW_OPENGL_ANY_PROFILE, GLFW_OPENGL_COMPAT_PROFILE
1559 #if defined(__APPLE__)
1560 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); // OSX Requires forward compatibility
1561 #else
1562 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_FALSE); // Forward Compatibility Hint: Only 3.3 and above!
1563 #endif
1564 //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); // Request OpenGL DEBUG context
1565 }
1566 else if (rlGetVersion() == RL_OPENGL_43)
1567 {
1568 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // Choose OpenGL major version (just hint)
1569 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Choose OpenGL minor version (just hint)
1570 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
1571 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_FALSE);
1572 #if RLGL_ENABLE_OPENGL_DEBUG_CONTEXT
1573 glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); // Enable OpenGL Debug Context
1574 #endif
1575 }
1576 else if (rlGetVersion() == RL_OPENGL_ES_20) // Request OpenGL ES 2.0 context
1577 {
1578 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
1579 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
1580 glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
1581 glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
1582 }
1583 else if (rlGetVersion() == RL_OPENGL_ES_30) // Request OpenGL ES 3.0 context
1584 {
1585 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
1586 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
1587 glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
1588 glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
1589 }
1590
1591 // NOTE: GLFW 3.4+ defers initialization of the Joystick subsystem on the first call to any Joystick related functions
1592 // Forcing this initialization here avoids doing it on PollInputEvents() called by EndDrawing() after first frame has been drawn
1593 // The initialization will still happen and possible delays still occur, but before the window is shown, which is a nicer experience
1594 // REF: https://github.com/raysan5/raylib/issues/1554
1595 glfwSetJoystickCallback(NULL);
1596
1597 if ((CORE.Window.screen.width == 0) || (CORE.Window.screen.height == 0)) FLAG_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
1598
1599 // Init window in fullscreen mode if requested
1600 // NOTE: Keeping original screen size for toggle
1601 if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
1602 {
1603 // NOTE: Fullscreen applications default to the primary monitor
1604 GLFWmonitor *monitor = glfwGetPrimaryMonitor();
1605 if (!monitor)
1606 {
1607 TRACELOG(LOG_WARNING, "GLFW: Failed to get primary monitor");
1608 return -1;
1609 }
1610
1611 // Set dimensions from monitor
1612 const GLFWvidmode *mode = glfwGetVideoMode(monitor);
1613
1614 // Default display resolution to that of the current mode
1615 CORE.Window.display.width = mode->width;
1616 CORE.Window.display.height = mode->height;
1617
1618 // Check if user requested some screen size
1619 if ((CORE.Window.screen.width == 0) || (CORE.Window.screen.height == 0))
1620 {
1621 // Set some default screen size in case user decides to exit fullscreen mode
1622 CORE.Window.previousScreen.width = 800;
1623 CORE.Window.previousScreen.height = 450;
1624 CORE.Window.previousPosition.x = CORE.Window.display.width/2 - 800/2;
1625 CORE.Window.previousPosition.y = CORE.Window.display.height/2 - 450/2;
1626
1627 // Set screen width/height to the display width/height
1628 if (CORE.Window.screen.width == 0) CORE.Window.screen.width = CORE.Window.display.width;
1629 if (CORE.Window.screen.height == 0) CORE.Window.screen.height = CORE.Window.display.height;
1630 }
1631 else
1632 {
1633 CORE.Window.previousScreen = CORE.Window.screen;
1634 CORE.Window.screen = CORE.Window.display;
1635 }
1636
1637 platform.handle = glfwCreateWindow(CORE.Window.screen.width, CORE.Window.screen.height, (CORE.Window.title != 0)? CORE.Window.title : " ", monitor, NULL);
1638 if (!platform.handle)
1639 {
1640 glfwTerminate();
1641 TRACELOG(LOG_WARNING, "GLFW: Failed to initialize Window");
1642 return -1;
1643 }
1644 }
1645 else
1646 {
1647 // Default to at least one pixel in size, as creation with a zero dimension is not allowed
1648 if (CORE.Window.screen.width == 0) CORE.Window.screen.width = 1;
1649 if (CORE.Window.screen.height == 0) CORE.Window.screen.height = 1;
1650
1651 platform.handle = glfwCreateWindow(CORE.Window.screen.width, CORE.Window.screen.height, (CORE.Window.title != 0)? CORE.Window.title : " ", NULL, NULL);
1652 if (!platform.handle)
1653 {
1654 glfwTerminate();
1655 TRACELOG(LOG_WARNING, "GLFW: Failed to initialize Window");
1656 return -1;
1657 }
1658
1659 // After the window was created, determine the monitor that the window manager assigned
1660 // Derive display sizes and, if possible, window size in case it was zero at beginning
1661
1662 int monitorCount = 0;
1663 int monitorIndex = GetCurrentMonitor();
1664 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
1665
1666 if (monitorIndex < monitorCount)
1667 {
1668 GLFWmonitor *monitor = monitors[monitorIndex];
1669 const GLFWvidmode *mode = glfwGetVideoMode(monitor);
1670
1671 // Default display resolution to that of the current mode
1672 CORE.Window.display.width = mode->width;
1673 CORE.Window.display.height = mode->height;
1674
1675 // Set screen width/height to the display width/height if they are 0
1676 if (CORE.Window.screen.width == 0) CORE.Window.screen.width = CORE.Window.display.width;
1677 if (CORE.Window.screen.height == 0) CORE.Window.screen.height = CORE.Window.display.height;
1678
1679 glfwSetWindowSize(platform.handle, CORE.Window.screen.width, CORE.Window.screen.height);
1680 }
1681 else
1682 {
1683 // The monitor for the window-manager-created window can not be determined, so it can not be centered
1684 glfwTerminate();
1685 TRACELOG(LOG_WARNING, "GLFW: Failed to determine Monitor to center Window");
1686 return -1;
1687 }
1688
1689 // NOTE: Not considering scale factor now, considered below
1690 CORE.Window.render.width = CORE.Window.screen.width;
1691 CORE.Window.render.height = CORE.Window.screen.height;
1692 }
1693
1694 glfwMakeContextCurrent(platform.handle);
1695 result = glfwGetError(NULL);
1696 if ((result != GLFW_NO_WINDOW_CONTEXT) && (result != GLFW_PLATFORM_ERROR)) CORE.Window.ready = true; // Checking context activation
1697
1698 if (CORE.Window.ready)
1699 {
1700 // Setup additional windows configs and register required window size info
1701
1702 glfwSwapInterval(0); // No V-Sync by default
1703
1704 // Try to enable GPU V-Sync, so frames are limited to screen refresh rate (60Hz -> 60 FPS)
1705 // NOTE: V-Sync can be enabled by graphic driver configuration, it doesn't need
1706 // to be activated on web platforms since VSync is enforced there
1707 if (FLAG_IS_SET(CORE.Window.flags, FLAG_VSYNC_HINT))
1708 {
1709 // WARNING: It seems to hit a critical render path in Intel HD Graphics
1710 glfwSwapInterval(1);
1711 TRACELOG(LOG_INFO, "DISPLAY: Trying to enable VSYNC");
1712 }
1713
1714 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
1715 {
1716 // Set screen size to logical pixel size, considering content scaling
1717 Vector2 scaleDpi = GetWindowScaleDPI();
1718 CORE.Window.render.width = (int)(CORE.Window.screen.width*scaleDpi.x);
1719 CORE.Window.render.height = (int)(CORE.Window.screen.height*scaleDpi.y);
1720 //TRACELOG(LOG_INFO, "DPI SCALING: %.2f, %.2f", scaleDpi.x, scaleDpi.y);
1721
1722 // Screen scaling matrix is required in case desired screen area is different from display area
1723 CORE.Window.screenScale = MatrixScale(scaleDpi.x, scaleDpi.y, 1.0f);
1724
1725 // NOTE: On APPLE platforms system manage window and input scaling
1726 // Framebuffer scaling is activated with: glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE);
1727
1728 #if !defined(__APPLE__)
1729 if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
1730 {
1731 // On Wayland, GLFW_SCALE_FRAMEBUFFER handles scaling; read actual framebuffer size
1732 // instead of resizing the window (which would double-scale)
1733 int fbWidth = 0;
1734 int fbHeight = 0;
1735 glfwGetFramebufferSize(platform.handle, &fbWidth, &fbHeight);
1736
1737 CORE.Window.render.width = fbWidth;
1738 CORE.Window.render.height = fbHeight;
1739 }
1740 else
1741 {
1742 // Mouse input scaling for the new screen size
1743 SetMouseScale(1.0f/scaleDpi.x, 1.0f/scaleDpi.y);
1744
1745 // Force window size (and framebuffer) refresh
1746 glfwSetWindowSize(platform.handle, CORE.Window.render.width, CORE.Window.render.height);
1747 }
1748 #endif
1749 }
1750 else CORE.Window.render = CORE.Window.screen;
1751
1752 // Current active framebuffer size is main framebuffer size
1753 CORE.Window.currentFbo = CORE.Window.render;
1754
1755 TRACELOG(LOG_INFO, "DISPLAY: Device initialized successfully %s",
1756 FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI)? "(HighDPI)" : "");
1757 TRACELOG(LOG_INFO, " > Display size: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
1758 TRACELOG(LOG_INFO, " > Screen size: %i x %i", CORE.Window.screen.width, CORE.Window.screen.height);
1759 TRACELOG(LOG_INFO, " > Render size: %i x %i", CORE.Window.render.width, CORE.Window.render.height);
1760 TRACELOG(LOG_INFO, " > Viewport offsets: %i, %i", CORE.Window.renderOffset.x, CORE.Window.renderOffset.y);
1761 //TRACELOG(LOG_INFO, " > Content Scaling: %.2f, %.2f", scaleDpi.x, scaleDpi.y);
1762
1763 // Try to center window on screen but avoiding window-bar outside of screen
1764 int monitorCount = 0;
1765 int monitorIndex = GetCurrentMonitor();
1766 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
1767 GLFWmonitor *monitor = monitors[monitorIndex];
1768
1769 int monitorX = 0;
1770 int monitorY = 0;
1771 int monitorWidth = 0;
1772 int monitorHeight = 0;
1773 glfwGetMonitorWorkarea(monitor, &monitorX, &monitorY, &monitorWidth, &monitorHeight);
1774
1775 // NOTE: It seems on macOS monitor size is not correct
1776 //TRACELOG(LOG_WARNING, "Monitor info: [%i, %i, %i, %i]", monitorX, monitorY, monitorWidth, monitorHeight);
1777
1778 // Center window into current monitor
1779 #if defined(__APPLE__)
1780 CORE.Window.position.x = monitorX + (monitorWidth - CORE.Window.screen.width)/2;
1781 CORE.Window.position.y = monitorY + (monitorHeight - CORE.Window.screen.height)/2;
1782 #else
1783 CORE.Window.position.x = monitorX + (monitorWidth - CORE.Window.render.width)/2;
1784 CORE.Window.position.y = monitorY + (monitorHeight - CORE.Window.render.height)/2;
1785 #endif
1786 SetWindowPosition(CORE.Window.position.x, CORE.Window.position.y);
1787
1788 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED)) MinimizeWindow();
1789 }
1790 else
1791 {
1792 TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize graphics device");
1793 return -1;
1794 }
1795
1796 // Apply window flags requested previous to initialization
1797 SetWindowState(requestedWindowFlags);
1798
1799 // Load OpenGL extensions
1800 // NOTE: GL procedures address loader is required to load extensions
1801 rlLoadExtensions(glfwGetProcAddress);
1802 //----------------------------------------------------------------------------
1803
1804 // Initialize input events callbacks
1805 //----------------------------------------------------------------------------
1806 // Set window callback events
1807 glfwSetWindowSizeCallback(platform.handle, WindowSizeCallback); // NOTE: Resizing is not enabled by default
1808 glfwSetFramebufferSizeCallback(platform.handle, FramebufferSizeCallback);
1809 glfwSetWindowPosCallback(platform.handle, WindowPosCallback);
1810 glfwSetWindowMaximizeCallback(platform.handle, WindowMaximizeCallback);
1811 glfwSetWindowIconifyCallback(platform.handle, WindowIconifyCallback);
1812 glfwSetWindowFocusCallback(platform.handle, WindowFocusCallback);
1813 glfwSetDropCallback(platform.handle, WindowDropCallback);
1814 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI)) glfwSetWindowContentScaleCallback(platform.handle, WindowContentScaleCallback);
1815
1816 // Set input callback events
1817 glfwSetKeyCallback(platform.handle, KeyCallback);
1818 glfwSetCharCallback(platform.handle, CharCallback);
1819 glfwSetMouseButtonCallback(platform.handle, MouseButtonCallback);
1820 glfwSetCursorPosCallback(platform.handle, MouseCursorPosCallback); // Track mouse position changes
1821 glfwSetScrollCallback(platform.handle, MouseScrollCallback);
1822 glfwSetCursorEnterCallback(platform.handle, CursorEnterCallback);
1823 glfwSetJoystickCallback(JoystickCallback);
1824 glfwSetInputMode(platform.handle, GLFW_LOCK_KEY_MODS, GLFW_TRUE); // Enable lock keys modifiers (CAPS, NUM)
1825
1826 // Retrieve gamepad names
1827 for (int i = 0; i < MAX_GAMEPADS; i++)
1828 {
1829 // WARNING: If glfwGetJoystickName() is longer than MAX_GAMEPAD_NAME_LENGTH,
1830 // only copying up to (MAX_GAMEPAD_NAME_LENGTH - 1)
1831 if (glfwJoystickPresent(i))
1832 {
1833 CORE.Input.Gamepad.ready[i] = true;
1834 CORE.Input.Gamepad.axisCount[i] = GLFW_GAMEPAD_AXIS_LAST + 1;
1835 strncpy(CORE.Input.Gamepad.name[i], glfwGetJoystickName(i), MAX_GAMEPAD_NAME_LENGTH - 1);
1836 }
1837 }
1838 //----------------------------------------------------------------------------
1839
1840 // Initialize timing system
1841 //----------------------------------------------------------------------------
1842 InitTimer();
1843 //----------------------------------------------------------------------------
1844
1845 // Initialize storage system
1846 //----------------------------------------------------------------------------
1847 CORE.Storage.basePath = GetWorkingDirectory();
1848 //----------------------------------------------------------------------------
1849
1850 #if defined(__NetBSD__)
1851 // Workaround for NetBSD
1852 char *glfwPlatform = "X11 (NetBSD)";
1853 #else
1854 char *glfwPlatform = "";
1855 switch (glfwGetPlatform())
1856 {
1857 case GLFW_PLATFORM_WIN32: glfwPlatform = "Win32"; break;
1858 case GLFW_PLATFORM_COCOA: glfwPlatform = "Cocoa"; break;
1859 case GLFW_PLATFORM_WAYLAND: glfwPlatform = "Wayland"; break;
1860 case GLFW_PLATFORM_X11: glfwPlatform = "X11"; break;
1861 case GLFW_PLATFORM_NULL: glfwPlatform = "Null"; break;
1862 default: break;
1863 }
1864 #endif
1865
1866 TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (GLFW - %s): Initialized successfully", glfwPlatform);
1867
1868 return 0;
1869 }
1870
1871 // Close platform
1872 void ClosePlatform(void)
1873 {
1874 glfwDestroyWindow(platform.handle);
1875 glfwTerminate();
1876
1877 #if defined(_WIN32) && SUPPORT_WINMM_HIGHRES_TIMER && !SUPPORT_BUSY_WAIT_LOOP
1878 timeEndPeriod(1); // Restore time period
1879 #endif
1880 }
1881
1882 //----------------------------------------------------------------------------------
1883 // Module Internal Functions Definition
1884 // NOTE: Those functions are only required for current platform
1885 //----------------------------------------------------------------------------------
1886
1887 // GLFW3: Error callback, runs on GLFW3 error
1888 static void ErrorCallback(int error, const char *description)
1889 {
1890 TRACELOG(LOG_WARNING, "GLFW: Error: %i Description: %s", error, description);
1891 }
1892
1893 // GLFW3: Window size change callback, runs when window is resized
1894 // NOTE: Window resizing not enabled by default, use SetConfigFlags()
1895 static void WindowSizeCallback(GLFWwindow *window, int width, int height)
1896 {
1897 // Nothing to do for now on window resize...
1898 //TRACELOG(LOG_INFO, "GLFW3: Window size callback called [%i,%i]", width, height);
1899 }
1900
1901 // GLFW3: Framebuffer size change callback, runs when framebuffer is resized
1902 // WARNING: If FLAG_WINDOW_HIGHDPI is set, WindowContentScaleCallback() is called before this function
1903 static void FramebufferSizeCallback(GLFWwindow *window, int width, int height)
1904 {
1905 //TRACELOG(LOG_INFO, "GLFW3: Window framebuffer size callback called [%i,%i]", width, height);
1906
1907 // WARNING: On window minimization, callback is called with 0 values,
1908 // but internal screen values should not be changed, it breaks things
1909 if ((width == 0) || (height == 0)) return;
1910
1911 // Reset viewport and projection matrix for new size
1912 // NOTE: Stores current render size: CORE.Window.render
1913 SetupViewport(width, height);
1914
1915 // Set render size
1916 CORE.Window.render.width = width;
1917 CORE.Window.render.height = height;
1918 CORE.Window.currentFbo = CORE.Window.render;
1919 CORE.Window.resizedLastFrame = true;
1920
1921 if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
1922 {
1923 // On fullscreen mode, strategy is ignoring high-dpi and
1924 // use the all available display size
1925
1926 // Set screen size to render size (physical pixel size)
1927 CORE.Window.screen.width = width;
1928 CORE.Window.screen.height = height;
1929 CORE.Window.screenScale = MatrixScale(1.0f, 1.0f, 1.0f);
1930 SetMouseScale(1.0f, 1.0f);
1931
1932 // On Wayland with GLFW_SCALE_FRAMEBUFFER, the framebuffer is still scaled in fullscreen, use logical window size as screen and apply screenScale
1933 #if defined(_GLFW_WAYLAND) && !defined(_GLFW_X11)
1934 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
1935 {
1936 int winWidth = 0;
1937 int winHeight = 0;
1938 glfwGetWindowSize(platform.handle, &winWidth, &winHeight);
1939
1940 if ((winWidth != width) || (winHeight != height))
1941 {
1942 CORE.Window.screen.width = winWidth;
1943 CORE.Window.screen.height = winHeight;
1944 float scaleX = (float)width/winWidth;
1945 float scaleY = (float)height/winHeight;
1946
1947 CORE.Window.screenScale = MatrixScale(scaleX, scaleY, 1.0f);
1948 }
1949 }
1950 #endif
1951 }
1952 else // Window mode (including borderless window)
1953 {
1954 // Check if render size was actually scaled for high-dpi
1955 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
1956 {
1957 // Set screen size to logical pixel size, considering content scaling
1958 Vector2 scaleDpi = GetWindowScaleDPI();
1959 CORE.Window.screen.width = (int)((float)width/scaleDpi.x);
1960 CORE.Window.screen.height = (int)((float)height/scaleDpi.y);
1961 CORE.Window.screenScale = MatrixScale(scaleDpi.x, scaleDpi.y, 1.0f);
1962 #if !defined(__APPLE__) && !defined(_GLFW_WAYLAND)
1963 // On macOS and Linux-Wayland, mouse coords are already in logical space
1964 SetMouseScale(1.0f/scaleDpi.x, 1.0f/scaleDpi.y);
1965 #endif
1966 }
1967 else
1968 {
1969 // Set screen size to render size (physical pixel size)
1970 CORE.Window.screen.width = width;
1971 CORE.Window.screen.height = height;
1972 }
1973 }
1974
1975 // WARNING: If using a render texture, it is not scaled to new size
1976 }
1977
1978 // GLFW3: Window content scale callback, runs on monitor content scale change detected
1979 // WARNING: If FLAG_WINDOW_HIGHDPI is not set, this function is not called
1980 static void WindowContentScaleCallback(GLFWwindow *window, float scalex, float scaley)
1981 {
1982 //TRACELOG(LOG_INFO, "GLFW3: Window content scale changed, scale: [%.2f,%.2f]", scalex, scaley);
1983
1984 CORE.Window.render.width = (int)((float)CORE.Window.screen.width*scalex);
1985 CORE.Window.render.height = (int)((float)CORE.Window.screen.height*scaley);
1986 CORE.Window.currentFbo = CORE.Window.render;
1987
1988 // NOTE: On APPLE platforms system should manage window/input scaling and also framebuffer scaling
1989 // Framebuffer scaling is activated with: glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE);
1990 CORE.Window.screenScale = MatrixScale(scalex, scaley, 1.0f);
1991
1992 #if !defined(__APPLE__) && !defined(_GLFW_WAYLAND)
1993 // On macOS and Linux-Wayland, mouse coords are already in logical space
1994 SetMouseScale(1.0f/scalex, 1.0f/scaley);
1995 #endif
1996 }
1997
1998 // GLFW3: Window position callback, runs when window position changes
1999 static void WindowPosCallback(GLFWwindow *window, int x, int y)
2000 {
2001 // Set current window position
2002 CORE.Window.position.x = x;
2003 CORE.Window.position.y = y;
2004 }
2005
2006 // GLFW3: Window iconify callback, runs when window is minimized/restored
2007 static void WindowIconifyCallback(GLFWwindow *window, int iconified)
2008 {
2009 if (iconified) FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED); // The window was iconified
2010 else FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MINIMIZED); // The window was restored
2011 }
2012
2013 // GLFW3: Window maximize callback, runs when window is maximized/restored
2014 static void WindowMaximizeCallback(GLFWwindow *window, int maximized)
2015 {
2016 if (maximized) FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED); // The window was maximized
2017 else FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED); // The window was restored
2018 }
2019
2020 // GLFW3: Window focus callback, runs when window get/lose focus
2021 static void WindowFocusCallback(GLFWwindow *window, int focused)
2022 {
2023 if (focused) FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED); // The window was focused
2024 else FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED); // The window lost focus
2025 }
2026
2027 // GLFW3: Window drop callback, runs when files are dropped into window
2028 static void WindowDropCallback(GLFWwindow *window, int count, const char **paths)
2029 {
2030 if (count > 0)
2031 {
2032 // In case previous dropped filepaths have not been freed, free them
2033 if (CORE.Window.dropFileCount > 0)
2034 {
2035 for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++) RL_FREE(CORE.Window.dropFilepaths[i]);
2036
2037 RL_FREE(CORE.Window.dropFilepaths);
2038
2039 CORE.Window.dropFileCount = 0;
2040 CORE.Window.dropFilepaths = NULL;
2041 }
2042
2043 // WARNING: Paths are freed by GLFW when the callback returns, keeping an internal copy
2044 CORE.Window.dropFileCount = count;
2045 CORE.Window.dropFilepaths = (char **)RL_CALLOC(CORE.Window.dropFileCount, sizeof(char *));
2046
2047 for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++)
2048 {
2049 CORE.Window.dropFilepaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char));
2050 strncpy(CORE.Window.dropFilepaths[i], paths[i], MAX_FILEPATH_LENGTH - 1);
2051 }
2052 }
2053 }
2054
2055 // GLFW3: Keyboard callback, runs on key pressed
2056 static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
2057 {
2058 if (key < 0) return; // Security check, macOS fn key generates -1
2059
2060 // WARNING: GLFW could return GLFW_REPEAT, it needs to be considered as 1
2061 // to work properly with our implementation (IsKeyDown/IsKeyUp checks)
2062 if (action == GLFW_RELEASE) CORE.Input.Keyboard.currentKeyState[key] = 0;
2063 else if (action == GLFW_PRESS) CORE.Input.Keyboard.currentKeyState[key] = 1;
2064 else if (action == GLFW_REPEAT) CORE.Input.Keyboard.keyRepeatInFrame[key] = 1;
2065
2066 // WARNING: Check if CAPS/NUM key modifiers are enabled and force down state for those keys
2067 if (((key == KEY_CAPS_LOCK) && (FLAG_IS_SET(mods, GLFW_MOD_CAPS_LOCK))) ||
2068 ((key == KEY_NUM_LOCK) && (FLAG_IS_SET(mods, GLFW_MOD_NUM_LOCK)))) CORE.Input.Keyboard.currentKeyState[key] = 1;
2069
2070 // Check if there is space available in the key queue
2071 if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (action == GLFW_PRESS))
2072 {
2073 // Add character to the queue
2074 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key;
2075 CORE.Input.Keyboard.keyPressedQueueCount++;
2076 }
2077
2078 // Check the exit key to set close window
2079 if ((key == CORE.Input.Keyboard.exitKey) && (action == GLFW_PRESS)) glfwSetWindowShouldClose(platform.handle, GLFW_TRUE);
2080 }
2081
2082 // GLFW3: Char callback, runs on key pressed to get unicode codepoint value
2083 static void CharCallback(GLFWwindow *window, unsigned int codepoint)
2084 {
2085 // NOTE: Registers any key down considering OS keyboard layout but
2086 // does not detect action events, those should be managed by user...
2087 // REF: https://github.com/glfw/glfw/issues/668#issuecomment-166794907
2088 // REF: https://www.glfw.org/docs/latest/input_guide.html#input_char
2089
2090 // Check if there is space available in the queue
2091 if (CORE.Input.Keyboard.charPressedQueueCount < MAX_CHAR_PRESSED_QUEUE)
2092 {
2093 // Add character to the queue
2094 CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = codepoint;
2095 CORE.Input.Keyboard.charPressedQueueCount++;
2096 }
2097 }
2098
2099 // GLFW3: Mouse button callback, runs on mouse button pressed
2100 static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
2101 {
2102 // WARNING: GLFW could only return GLFW_PRESS (1) or GLFW_RELEASE (0) for now,
2103 // but future releases may add more actions (i.e. GLFW_REPEAT)
2104 CORE.Input.Mouse.currentButtonState[button] = action;
2105 CORE.Input.Touch.currentTouchState[button] = action;
2106
2107 #if SUPPORT_GESTURES_SYSTEM && SUPPORT_MOUSE_GESTURES
2108 // Process mouse events as touches to be able to use mouse-gestures
2109 GestureEvent gestureEvent = { 0 };
2110
2111 // Register touch actions
2112 if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) gestureEvent.touchAction = TOUCH_ACTION_DOWN;
2113 else if ((CORE.Input.Mouse.currentButtonState[button] == 0) && (CORE.Input.Mouse.previousButtonState[button] == 1)) gestureEvent.touchAction = TOUCH_ACTION_UP;
2114
2115 // NOTE: TOUCH_ACTION_MOVE event is registered in MouseCursorPosCallback()
2116
2117 // Assign a pointer ID
2118 gestureEvent.pointId[0] = 0;
2119
2120 // Register touch points count
2121 gestureEvent.pointCount = 1;
2122
2123 // Register touch points position, only one point registered
2124 gestureEvent.position[0] = GetMousePosition();
2125
2126 // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
2127 gestureEvent.position[0].x /= (float)GetScreenWidth();
2128 gestureEvent.position[0].y /= (float)GetScreenHeight();
2129
2130 // Gesture data is sent to gestures-system for processing
2131 ProcessGestureEvent(gestureEvent);
2132 #endif
2133 }
2134
2135 // GLFW3: Cursor position callback, runs on mouse movement
2136 static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
2137 {
2138 CORE.Input.Mouse.currentPosition.x = (float)x;
2139 CORE.Input.Mouse.currentPosition.y = (float)y;
2140 CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
2141
2142 #if SUPPORT_GESTURES_SYSTEM && SUPPORT_MOUSE_GESTURES
2143 // Process mouse events as touches to be able to use mouse-gestures
2144 GestureEvent gestureEvent = { 0 };
2145
2146 gestureEvent.touchAction = TOUCH_ACTION_MOVE;
2147
2148 // Assign a pointer ID
2149 gestureEvent.pointId[0] = 0;
2150
2151 // Register touch points count
2152 gestureEvent.pointCount = 1;
2153
2154 // Register touch points position, only one point registered
2155 gestureEvent.position[0] = CORE.Input.Touch.position[0];
2156
2157 // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
2158 gestureEvent.position[0].x /= (float)GetScreenWidth();
2159 gestureEvent.position[0].y /= (float)GetScreenHeight();
2160
2161 // Gesture data is sent to gestures-system for processing
2162 ProcessGestureEvent(gestureEvent);
2163 #endif
2164 }
2165
2166 // GLFW3: Mouse wheel scroll callback, runs on mouse wheel changes
2167 static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset)
2168 {
2169 CORE.Input.Mouse.currentWheelMove = (Vector2){ (float)xoffset, (float)yoffset };
2170 }
2171
2172 // GLFW3: Cursor ennter callback, when cursor enters the window
2173 static void CursorEnterCallback(GLFWwindow *window, int enter)
2174 {
2175 if (enter) CORE.Input.Mouse.cursorOnScreen = true;
2176 else CORE.Input.Mouse.cursorOnScreen = false;
2177 }
2178
2179 // GLFW3: Joystick connected/disconnected callback
2180 static void JoystickCallback(int jid, int event)
2181 {
2182 if (jid < MAX_GAMEPADS)
2183 {
2184 if (event == GLFW_CONNECTED)
2185 {
2186 // WARNING: If glfwGetJoystickName() is longer than MAX_GAMEPAD_NAME_LENGTH,
2187 // only copy up to (MAX_GAMEPAD_NAME_LENGTH -1) to destination string
2188 memset(CORE.Input.Gamepad.name[jid], 0, MAX_GAMEPAD_NAME_LENGTH);
2189 strncpy(CORE.Input.Gamepad.name[jid], glfwGetJoystickName(jid), MAX_GAMEPAD_NAME_LENGTH - 1);
2190 }
2191 else if (event == GLFW_DISCONNECTED)
2192 {
2193 memset(CORE.Input.Gamepad.name[jid], 0, MAX_GAMEPAD_NAME_LENGTH);
2194 }
2195 }
2196 }
2197
2198 #ifdef _WIN32
2199 # define WIN32_CLIPBOARD_IMPLEMENTATION
2200 # include "../external/win32_clipboard.h"
2201 #endif
2202 // EOF