1b877906bSopenharmony_ci# Introduction to the API {#intro_guide} 2b877906bSopenharmony_ci 3b877906bSopenharmony_ci[TOC] 4b877906bSopenharmony_ci 5b877906bSopenharmony_ciThis guide introduces the basic concepts of GLFW and describes initialization, 6b877906bSopenharmony_cierror handling and API guarantees and limitations. For a broad but shallow 7b877906bSopenharmony_citutorial, see @ref quick_guide instead. For details on a specific function in 8b877906bSopenharmony_cithis category, see the @ref init. 9b877906bSopenharmony_ci 10b877906bSopenharmony_ciThere are also guides for the other areas of GLFW. 11b877906bSopenharmony_ci 12b877906bSopenharmony_ci - @ref window_guide 13b877906bSopenharmony_ci - @ref context_guide 14b877906bSopenharmony_ci - @ref vulkan_guide 15b877906bSopenharmony_ci - @ref monitor_guide 16b877906bSopenharmony_ci - @ref input_guide 17b877906bSopenharmony_ci 18b877906bSopenharmony_ci 19b877906bSopenharmony_ci## Initialization and termination {#intro_init} 20b877906bSopenharmony_ci 21b877906bSopenharmony_ciBefore most GLFW functions may be called, the library must be initialized. 22b877906bSopenharmony_ciThis initialization checks what features are available on the machine, 23b877906bSopenharmony_cienumerates monitors, initializes the timer and performs any required 24b877906bSopenharmony_ciplatform-specific initialization. 25b877906bSopenharmony_ci 26b877906bSopenharmony_ciOnly the following functions may be called before the library has been 27b877906bSopenharmony_cisuccessfully initialized, and only from the main thread. 28b877906bSopenharmony_ci 29b877906bSopenharmony_ci - @ref glfwGetVersion 30b877906bSopenharmony_ci - @ref glfwGetVersionString 31b877906bSopenharmony_ci - @ref glfwPlatformSupported 32b877906bSopenharmony_ci - @ref glfwGetError 33b877906bSopenharmony_ci - @ref glfwSetErrorCallback 34b877906bSopenharmony_ci - @ref glfwInitHint 35b877906bSopenharmony_ci - @ref glfwInitAllocator 36b877906bSopenharmony_ci - @ref glfwInitVulkanLoader 37b877906bSopenharmony_ci - @ref glfwInit 38b877906bSopenharmony_ci - @ref glfwTerminate 39b877906bSopenharmony_ci 40b877906bSopenharmony_ciCalling any other function before successful initialization will cause a @ref 41b877906bSopenharmony_ciGLFW_NOT_INITIALIZED error. 42b877906bSopenharmony_ci 43b877906bSopenharmony_ci 44b877906bSopenharmony_ci### Initializing GLFW {#intro_init_init} 45b877906bSopenharmony_ci 46b877906bSopenharmony_ciThe library is initialized with @ref glfwInit, which returns `GLFW_FALSE` if an 47b877906bSopenharmony_cierror occurred. 48b877906bSopenharmony_ci 49b877906bSopenharmony_ci```c 50b877906bSopenharmony_ciif (!glfwInit()) 51b877906bSopenharmony_ci{ 52b877906bSopenharmony_ci // Handle initialization failure 53b877906bSopenharmony_ci} 54b877906bSopenharmony_ci``` 55b877906bSopenharmony_ci 56b877906bSopenharmony_ciIf any part of initialization fails, any parts that succeeded are terminated as 57b877906bSopenharmony_ciif @ref glfwTerminate had been called. The library only needs to be initialized 58b877906bSopenharmony_cionce and additional calls to an already initialized library will return 59b877906bSopenharmony_ci`GLFW_TRUE` immediately. 60b877906bSopenharmony_ci 61b877906bSopenharmony_ciOnce the library has been successfully initialized, it should be terminated 62b877906bSopenharmony_cibefore the application exits. Modern systems are very good at freeing resources 63b877906bSopenharmony_ciallocated by programs that exit, but GLFW sometimes has to change global system 64b877906bSopenharmony_cisettings and these might not be restored without termination. 65b877906bSopenharmony_ci 66b877906bSopenharmony_ci@macos When the library is initialized the main menu and dock icon are created. 67b877906bSopenharmony_ciThese are not desirable for a command-line only program. The creation of the 68b877906bSopenharmony_cimain menu and dock icon can be disabled with the @ref GLFW_COCOA_MENUBAR init 69b877906bSopenharmony_cihint. 70b877906bSopenharmony_ci 71b877906bSopenharmony_ci 72b877906bSopenharmony_ci### Initialization hints {#init_hints} 73b877906bSopenharmony_ci 74b877906bSopenharmony_ciInitialization hints are set before @ref glfwInit and affect how the library 75b877906bSopenharmony_cibehaves until termination. Hints are set with @ref glfwInitHint. 76b877906bSopenharmony_ci 77b877906bSopenharmony_ci```c 78b877906bSopenharmony_ciglfwInitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE); 79b877906bSopenharmony_ci``` 80b877906bSopenharmony_ci 81b877906bSopenharmony_ciThe values you set hints to are never reset by GLFW, but they only take effect 82b877906bSopenharmony_ciduring initialization. Once GLFW has been initialized, any values you set will 83b877906bSopenharmony_cibe ignored until the library is terminated and initialized again. 84b877906bSopenharmony_ci 85b877906bSopenharmony_ciSome hints are platform specific. These may be set on any platform but they 86b877906bSopenharmony_ciwill only affect their specific platform. Other platforms will ignore them. 87b877906bSopenharmony_ciSetting these hints requires no platform specific headers or functions. 88b877906bSopenharmony_ci 89b877906bSopenharmony_ci 90b877906bSopenharmony_ci#### Shared init hints {#init_hints_shared} 91b877906bSopenharmony_ci 92b877906bSopenharmony_ci@anchor GLFW_PLATFORM 93b877906bSopenharmony_ci__GLFW_PLATFORM__ specifies the platform to use for windowing and input. 94b877906bSopenharmony_ciPossible values are `GLFW_ANY_PLATFORM`, `GLFW_PLATFORM_WIN32`, 95b877906bSopenharmony_ci`GLFW_PLATFORM_COCOA`, `GLFW_PLATFORM_WAYLAND`, `GLFW_PLATFORM_X11` and 96b877906bSopenharmony_ci`GLFW_PLATFORM_NULL`. The default value is `GLFW_ANY_PLATFORM`, which will 97b877906bSopenharmony_cichoose any platform the library includes support for except for the Null 98b877906bSopenharmony_cibackend. 99b877906bSopenharmony_ci 100b877906bSopenharmony_ci 101b877906bSopenharmony_ci@anchor GLFW_JOYSTICK_HAT_BUTTONS 102b877906bSopenharmony_ci__GLFW_JOYSTICK_HAT_BUTTONS__ specifies whether to also expose joystick hats as 103b877906bSopenharmony_cibuttons, for compatibility with earlier versions of GLFW that did not have @ref 104b877906bSopenharmony_ciglfwGetJoystickHats. Possible values are `GLFW_TRUE` and `GLFW_FALSE`. 105b877906bSopenharmony_ci 106b877906bSopenharmony_ci@anchor GLFW_ANGLE_PLATFORM_TYPE_hint 107b877906bSopenharmony_ci__GLFW_ANGLE_PLATFORM_TYPE__ specifies the platform type (rendering backend) to 108b877906bSopenharmony_cirequest when using OpenGL ES and EGL via [ANGLE][]. If the requested platform 109b877906bSopenharmony_citype is unavailable, ANGLE will use its default. Possible values are one of 110b877906bSopenharmony_ci`GLFW_ANGLE_PLATFORM_TYPE_NONE`, `GLFW_ANGLE_PLATFORM_TYPE_OPENGL`, 111b877906bSopenharmony_ci`GLFW_ANGLE_PLATFORM_TYPE_OPENGLES`, `GLFW_ANGLE_PLATFORM_TYPE_D3D9`, 112b877906bSopenharmony_ci`GLFW_ANGLE_PLATFORM_TYPE_D3D11`, `GLFW_ANGLE_PLATFORM_TYPE_VULKAN` and 113b877906bSopenharmony_ci`GLFW_ANGLE_PLATFORM_TYPE_METAL`. 114b877906bSopenharmony_ci 115b877906bSopenharmony_ci[ANGLE]: https://chromium.googlesource.com/angle/angle/ 116b877906bSopenharmony_ci 117b877906bSopenharmony_ciThe ANGLE platform type is specified via the `EGL_ANGLE_platform_angle` 118b877906bSopenharmony_ciextension. This extension is not used if this hint is 119b877906bSopenharmony_ci`GLFW_ANGLE_PLATFORM_TYPE_NONE`, which is the default value. 120b877906bSopenharmony_ci 121b877906bSopenharmony_ci 122b877906bSopenharmony_ci#### macOS specific init hints {#init_hints_osx} 123b877906bSopenharmony_ci 124b877906bSopenharmony_ci@anchor GLFW_COCOA_CHDIR_RESOURCES_hint 125b877906bSopenharmony_ci__GLFW_COCOA_CHDIR_RESOURCES__ specifies whether to set the current directory to 126b877906bSopenharmony_cithe application to the `Contents/Resources` subdirectory of the application's 127b877906bSopenharmony_cibundle, if present. Possible values are `GLFW_TRUE` and `GLFW_FALSE`. This is 128b877906bSopenharmony_ciignored on other platforms. 129b877906bSopenharmony_ci 130b877906bSopenharmony_ci@anchor GLFW_COCOA_MENUBAR_hint 131b877906bSopenharmony_ci__GLFW_COCOA_MENUBAR__ specifies whether to create the menu bar and dock icon 132b877906bSopenharmony_ciwhen GLFW is initialized. This applies whether the menu bar is created from 133b877906bSopenharmony_cia nib or manually by GLFW. Possible values are `GLFW_TRUE` and `GLFW_FALSE`. 134b877906bSopenharmony_ciThis is ignored on other platforms. 135b877906bSopenharmony_ci 136b877906bSopenharmony_ci 137b877906bSopenharmony_ci#### Wayland specific init hints {#init_hints_wayland} 138b877906bSopenharmony_ci 139b877906bSopenharmony_ci@anchor GLFW_WAYLAND_LIBDECOR_hint 140b877906bSopenharmony_ci__GLFW_WAYLAND_LIBDECOR__ specifies whether to use [libdecor][] for window 141b877906bSopenharmony_cidecorations where available. Possible values are `GLFW_WAYLAND_PREFER_LIBDECOR` 142b877906bSopenharmony_ciand `GLFW_WAYLAND_DISABLE_LIBDECOR`. This is ignored on other platforms. 143b877906bSopenharmony_ci 144b877906bSopenharmony_ci[libdecor]: https://gitlab.freedesktop.org/libdecor/libdecor 145b877906bSopenharmony_ci 146b877906bSopenharmony_ci 147b877906bSopenharmony_ci#### X11 specific init hints {#init_hints_x11} 148b877906bSopenharmony_ci 149b877906bSopenharmony_ci@anchor GLFW_X11_XCB_VULKAN_SURFACE_hint 150b877906bSopenharmony_ci__GLFW_X11_XCB_VULKAN_SURFACE__ specifies whether to prefer the 151b877906bSopenharmony_ci`VK_KHR_xcb_surface` extension for creating Vulkan surfaces, or whether to use 152b877906bSopenharmony_cithe `VK_KHR_xlib_surface` extension. Possible values are `GLFW_TRUE` and 153b877906bSopenharmony_ci`GLFW_FALSE`. This is ignored on other platforms. 154b877906bSopenharmony_ci 155b877906bSopenharmony_ci 156b877906bSopenharmony_ci#### Supported and default values {#init_hints_values} 157b877906bSopenharmony_ci 158b877906bSopenharmony_ciInitialization hint | Default value | Supported values 159b877906bSopenharmony_ci-------------------------------- | ------------------------------- | ---------------- 160b877906bSopenharmony_ci@ref GLFW_PLATFORM | `GLFW_ANY_PLATFORM` | `GLFW_ANY_PLATFORM`, `GLFW_PLATFORM_WIN32`, `GLFW_PLATFORM_COCOA`, `GLFW_PLATFORM_WAYLAND`, `GLFW_PLATFORM_X11` or `GLFW_PLATFORM_NULL` 161b877906bSopenharmony_ci@ref GLFW_JOYSTICK_HAT_BUTTONS | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE` 162b877906bSopenharmony_ci@ref GLFW_ANGLE_PLATFORM_TYPE | `GLFW_ANGLE_PLATFORM_TYPE_NONE` | `GLFW_ANGLE_PLATFORM_TYPE_NONE`, `GLFW_ANGLE_PLATFORM_TYPE_OPENGL`, `GLFW_ANGLE_PLATFORM_TYPE_OPENGLES`, `GLFW_ANGLE_PLATFORM_TYPE_D3D9`, `GLFW_ANGLE_PLATFORM_TYPE_D3D11`, `GLFW_ANGLE_PLATFORM_TYPE_VULKAN` or `GLFW_ANGLE_PLATFORM_TYPE_METAL` 163b877906bSopenharmony_ci@ref GLFW_COCOA_CHDIR_RESOURCES | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE` 164b877906bSopenharmony_ci@ref GLFW_COCOA_MENUBAR | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE` 165b877906bSopenharmony_ci@ref GLFW_WAYLAND_LIBDECOR | `GLFW_WAYLAND_PREFER_LIBDECOR` | `GLFW_WAYLAND_PREFER_LIBDECOR` or `GLFW_WAYLAND_DISABLE_LIBDECOR` 166b877906bSopenharmony_ci@ref GLFW_X11_XCB_VULKAN_SURFACE | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE` 167b877906bSopenharmony_ci 168b877906bSopenharmony_ci 169b877906bSopenharmony_ci### Runtime platform selection {#platform} 170b877906bSopenharmony_ci 171b877906bSopenharmony_ciGLFW can be compiled for more than one platform (window system) at once. This lets 172b877906bSopenharmony_cia single library binary support both Wayland and X11 on Linux and other Unix-like systems. 173b877906bSopenharmony_ci 174b877906bSopenharmony_ciYou can control platform selection via the @ref GLFW_PLATFORM initialization hint. By 175b877906bSopenharmony_cidefault, this is set to @ref GLFW_ANY_PLATFORM, which will look for supported window 176b877906bSopenharmony_cisystems in order of priority and select the first one it finds. It can also be set to any 177b877906bSopenharmony_cispecific platform to have GLFW only look for that one. 178b877906bSopenharmony_ci 179b877906bSopenharmony_ci```c 180b877906bSopenharmony_ciglfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11); 181b877906bSopenharmony_ci``` 182b877906bSopenharmony_ci 183b877906bSopenharmony_ciThis mechanism also provides the Null platform, which is always supported but needs to be 184b877906bSopenharmony_ciexplicitly requested. This platform is effectively a stub, emulating a window system on 185b877906bSopenharmony_cia single 1080p monitor, but will not interact with any actual window system. 186b877906bSopenharmony_ci 187b877906bSopenharmony_ci```c 188b877906bSopenharmony_ciglfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_NULL); 189b877906bSopenharmony_ci``` 190b877906bSopenharmony_ci 191b877906bSopenharmony_ciYou can test whether a library binary was compiled with support for a specific platform 192b877906bSopenharmony_ciwith @ref glfwPlatformSupported. 193b877906bSopenharmony_ci 194b877906bSopenharmony_ci```c 195b877906bSopenharmony_ciif (glfwPlatformSupported(GLFW_PLATFORM_WAYLAND)) 196b877906bSopenharmony_ci glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND); 197b877906bSopenharmony_ci``` 198b877906bSopenharmony_ci 199b877906bSopenharmony_ciOnce GLFW has been initialized, you can query which platform was selected with @ref 200b877906bSopenharmony_ciglfwGetPlatform. 201b877906bSopenharmony_ci 202b877906bSopenharmony_ci```c 203b877906bSopenharmony_ciint platform = glfwGetPlatform(); 204b877906bSopenharmony_ci``` 205b877906bSopenharmony_ci 206b877906bSopenharmony_ciIf you are using any [native access functions](@ref native), especially on Linux and other 207b877906bSopenharmony_ciUnix-like systems, then you may need to check that you are calling the ones matching the 208b877906bSopenharmony_ciselected platform. 209b877906bSopenharmony_ci 210b877906bSopenharmony_ci 211b877906bSopenharmony_ci### Custom heap memory allocator {#init_allocator} 212b877906bSopenharmony_ci 213b877906bSopenharmony_ciThe heap memory allocator can be customized before initialization with @ref 214b877906bSopenharmony_ciglfwInitAllocator. 215b877906bSopenharmony_ci 216b877906bSopenharmony_ci```c 217b877906bSopenharmony_ciGLFWallocator allocator; 218b877906bSopenharmony_ciallocator.allocate = my_malloc; 219b877906bSopenharmony_ciallocator.reallocate = my_realloc; 220b877906bSopenharmony_ciallocator.deallocate = my_free; 221b877906bSopenharmony_ciallocator.user = NULL; 222b877906bSopenharmony_ci 223b877906bSopenharmony_ciglfwInitAllocator(&allocator); 224b877906bSopenharmony_ci``` 225b877906bSopenharmony_ci 226b877906bSopenharmony_ciThe allocator will be made active at the beginning of initialization and will be used by 227b877906bSopenharmony_ciGLFW until the library has been fully terminated. Any allocator set after initialization 228b877906bSopenharmony_ciwill be picked up only at the next initialization. 229b877906bSopenharmony_ci 230b877906bSopenharmony_ciThe allocator will only be used for allocations that would have been made with 231b877906bSopenharmony_cithe C standard library. Memory allocations that must be made with platform 232b877906bSopenharmony_cispecific APIs will still use those. 233b877906bSopenharmony_ci 234b877906bSopenharmony_ciThe allocation function must have a signature matching @ref GLFWallocatefun. It receives 235b877906bSopenharmony_cithe desired size, in bytes, and the user pointer passed to @ref glfwInitAllocator and 236b877906bSopenharmony_cireturns the address to the allocated memory block. 237b877906bSopenharmony_ci 238b877906bSopenharmony_ci```c 239b877906bSopenharmony_civoid* my_malloc(size_t size, void* user) 240b877906bSopenharmony_ci{ 241b877906bSopenharmony_ci ... 242b877906bSopenharmony_ci} 243b877906bSopenharmony_ci``` 244b877906bSopenharmony_ci 245b877906bSopenharmony_ciThe documentation for @ref GLFWallocatefun also lists the requirements and limitations for 246b877906bSopenharmony_cian allocation function. If the active one does not meet all of these, GLFW may fail. 247b877906bSopenharmony_ci 248b877906bSopenharmony_ciThe reallocation function must have a function signature matching @ref GLFWreallocatefun. 249b877906bSopenharmony_ciIt receives the memory block to be reallocated, the new desired size, in bytes, and the user 250b877906bSopenharmony_cipointer passed to @ref glfwInitAllocator and returns the address to the resized memory 251b877906bSopenharmony_ciblock. 252b877906bSopenharmony_ci 253b877906bSopenharmony_ci```c 254b877906bSopenharmony_civoid* my_realloc(void* block, size_t size, void* user) 255b877906bSopenharmony_ci{ 256b877906bSopenharmony_ci ... 257b877906bSopenharmony_ci} 258b877906bSopenharmony_ci``` 259b877906bSopenharmony_ci 260b877906bSopenharmony_ciThe documentation for @ref GLFWreallocatefun also lists the requirements and limitations 261b877906bSopenharmony_cifor a reallocation function. If the active one does not meet all of these, GLFW may fail. 262b877906bSopenharmony_ci 263b877906bSopenharmony_ciThe deallocation function must have a function signature matching @ref GLFWdeallocatefun. 264b877906bSopenharmony_ciIt receives the memory block to be deallocated and the user pointer passed to @ref 265b877906bSopenharmony_ciglfwInitAllocator. 266b877906bSopenharmony_ci 267b877906bSopenharmony_ci```c 268b877906bSopenharmony_civoid my_free(void* block, void* user) 269b877906bSopenharmony_ci{ 270b877906bSopenharmony_ci ... 271b877906bSopenharmony_ci} 272b877906bSopenharmony_ci``` 273b877906bSopenharmony_ci 274b877906bSopenharmony_ciThe documentation for @ref GLFWdeallocatefun also lists the requirements and limitations 275b877906bSopenharmony_cifor a deallocation function. If the active one does not meet all of these, GLFW may fail. 276b877906bSopenharmony_ci 277b877906bSopenharmony_ci 278b877906bSopenharmony_ci### Terminating GLFW {#intro_init_terminate} 279b877906bSopenharmony_ci 280b877906bSopenharmony_ciBefore your application exits, you should terminate the GLFW library if it has 281b877906bSopenharmony_cibeen initialized. This is done with @ref glfwTerminate. 282b877906bSopenharmony_ci 283b877906bSopenharmony_ci```c 284b877906bSopenharmony_ciglfwTerminate(); 285b877906bSopenharmony_ci``` 286b877906bSopenharmony_ci 287b877906bSopenharmony_ciThis will destroy any remaining window, monitor and cursor objects, restore any 288b877906bSopenharmony_cimodified gamma ramps, re-enable the screensaver if it had been disabled and free 289b877906bSopenharmony_ciany other resources allocated by GLFW. 290b877906bSopenharmony_ci 291b877906bSopenharmony_ciOnce the library is terminated, it is as if it had never been initialized, therefore 292b877906bSopenharmony_ciyou will need to initialize it again before being able to use GLFW. If the 293b877906bSopenharmony_cilibrary was not initialized or had already been terminated, it returns 294b877906bSopenharmony_ciimmediately. 295b877906bSopenharmony_ci 296b877906bSopenharmony_ci 297b877906bSopenharmony_ci## Error handling {#error_handling} 298b877906bSopenharmony_ci 299b877906bSopenharmony_ciSome GLFW functions have return values that indicate an error, but this is often 300b877906bSopenharmony_cinot very helpful when trying to figure out what happened or why it occurred. 301b877906bSopenharmony_ciOther functions have no return value reserved for errors, so error notification 302b877906bSopenharmony_cineeds a separate channel. Finally, far from all GLFW functions have return 303b877906bSopenharmony_civalues. 304b877906bSopenharmony_ci 305b877906bSopenharmony_ciThe last [error code](@ref errors) for the calling thread can be queried at any 306b877906bSopenharmony_citime with @ref glfwGetError. 307b877906bSopenharmony_ci 308b877906bSopenharmony_ci```c 309b877906bSopenharmony_ciint code = glfwGetError(NULL); 310b877906bSopenharmony_ci 311b877906bSopenharmony_ciif (code != GLFW_NO_ERROR) 312b877906bSopenharmony_ci handle_error(code); 313b877906bSopenharmony_ci``` 314b877906bSopenharmony_ci 315b877906bSopenharmony_ciIf no error has occurred since the last call, @ref GLFW_NO_ERROR (zero) is 316b877906bSopenharmony_cireturned. The error is cleared before the function returns. 317b877906bSopenharmony_ci 318b877906bSopenharmony_ciThe error code indicates the general category of the error. Some error codes, 319b877906bSopenharmony_cisuch as @ref GLFW_NOT_INITIALIZED has only a single meaning, whereas others like 320b877906bSopenharmony_ci@ref GLFW_PLATFORM_ERROR are used for many different errors. 321b877906bSopenharmony_ci 322b877906bSopenharmony_ciGLFW often has more information about an error than its general category. You 323b877906bSopenharmony_cican retrieve a UTF-8 encoded human-readable description along with the error 324b877906bSopenharmony_cicode. If no error has occurred since the last call, the description is set to 325b877906bSopenharmony_ci`NULL`. 326b877906bSopenharmony_ci 327b877906bSopenharmony_ci```c 328b877906bSopenharmony_ciconst char* description; 329b877906bSopenharmony_ciint code = glfwGetError(&description); 330b877906bSopenharmony_ci 331b877906bSopenharmony_ciif (description) 332b877906bSopenharmony_ci display_error_message(code, description); 333b877906bSopenharmony_ci``` 334b877906bSopenharmony_ci 335b877906bSopenharmony_ciThe retrieved description string is only valid until the next error occurs. 336b877906bSopenharmony_ciThis means you must make a copy of it if you want to keep it. 337b877906bSopenharmony_ci 338b877906bSopenharmony_ciYou can also set an error callback, which will be called each time an error 339b877906bSopenharmony_cioccurs. It is set with @ref glfwSetErrorCallback. 340b877906bSopenharmony_ci 341b877906bSopenharmony_ci```c 342b877906bSopenharmony_ciglfwSetErrorCallback(error_callback); 343b877906bSopenharmony_ci``` 344b877906bSopenharmony_ci 345b877906bSopenharmony_ciThe error callback receives the same error code and human-readable description 346b877906bSopenharmony_cireturned by @ref glfwGetError. 347b877906bSopenharmony_ci 348b877906bSopenharmony_ci```c 349b877906bSopenharmony_civoid error_callback(int code, const char* description) 350b877906bSopenharmony_ci{ 351b877906bSopenharmony_ci display_error_message(code, description); 352b877906bSopenharmony_ci} 353b877906bSopenharmony_ci``` 354b877906bSopenharmony_ci 355b877906bSopenharmony_ciThe error callback is called after the error is stored, so calling @ref 356b877906bSopenharmony_ciglfwGetError from within the error callback returns the same values as the 357b877906bSopenharmony_cicallback argument. 358b877906bSopenharmony_ci 359b877906bSopenharmony_ciThe description string passed to the callback is only valid until the error 360b877906bSopenharmony_cicallback returns. This means you must make a copy of it if you want to keep it. 361b877906bSopenharmony_ci 362b877906bSopenharmony_ci__Reported errors are never fatal.__ As long as GLFW was successfully 363b877906bSopenharmony_ciinitialized, it will remain initialized and in a safe state until terminated 364b877906bSopenharmony_ciregardless of how many errors occur. If an error occurs during initialization 365b877906bSopenharmony_cithat causes @ref glfwInit to fail, any part of the library that was initialized 366b877906bSopenharmony_ciwill be safely terminated. 367b877906bSopenharmony_ci 368b877906bSopenharmony_ciDo not rely on a currently invalid call to generate a specific error, as in the 369b877906bSopenharmony_cifuture that same call may generate a different error or become valid. 370b877906bSopenharmony_ci 371b877906bSopenharmony_ci 372b877906bSopenharmony_ci## Coordinate systems {#coordinate_systems} 373b877906bSopenharmony_ci 374b877906bSopenharmony_ciGLFW has two primary coordinate systems: the _virtual screen_ and the window 375b877906bSopenharmony_ci_content area_ or _content area_. Both use the same unit: _virtual screen 376b877906bSopenharmony_cicoordinates_, or just _screen coordinates_, which don't necessarily correspond 377b877906bSopenharmony_cito pixels. 378b877906bSopenharmony_ci 379b877906bSopenharmony_ci<img src="spaces.svg" width="90%" /> 380b877906bSopenharmony_ci 381b877906bSopenharmony_ciBoth the virtual screen and the content area coordinate systems have the X-axis 382b877906bSopenharmony_cipointing to the right and the Y-axis pointing down. 383b877906bSopenharmony_ci 384b877906bSopenharmony_ciWindow and monitor positions are specified as the position of the upper-left 385b877906bSopenharmony_cicorners of their content areas relative to the virtual screen, while cursor 386b877906bSopenharmony_cipositions are specified relative to a window's content area. 387b877906bSopenharmony_ci 388b877906bSopenharmony_ciBecause the origin of the window's content area coordinate system is also the 389b877906bSopenharmony_cipoint from which the window position is specified, you can translate content 390b877906bSopenharmony_ciarea coordinates to the virtual screen by adding the window position. The 391b877906bSopenharmony_ciwindow frame, when present, extends out from the content area but does not 392b877906bSopenharmony_ciaffect the window position. 393b877906bSopenharmony_ci 394b877906bSopenharmony_ciAlmost all positions and sizes in GLFW are measured in screen coordinates 395b877906bSopenharmony_cirelative to one of the two origins above. This includes cursor positions, 396b877906bSopenharmony_ciwindow positions and sizes, window frame sizes, monitor positions and video mode 397b877906bSopenharmony_ciresolutions. 398b877906bSopenharmony_ci 399b877906bSopenharmony_ciTwo exceptions are the [monitor physical size](@ref monitor_size), which is 400b877906bSopenharmony_cimeasured in millimetres, and [framebuffer size](@ref window_fbsize), which is 401b877906bSopenharmony_cimeasured in pixels. 402b877906bSopenharmony_ci 403b877906bSopenharmony_ciPixels and screen coordinates may map 1:1 on your machine, but they won't on 404b877906bSopenharmony_cievery other machine, for example on a Mac with a Retina display. The ratio 405b877906bSopenharmony_cibetween screen coordinates and pixels may also change at run-time depending on 406b877906bSopenharmony_ciwhich monitor the window is currently considered to be on. 407b877906bSopenharmony_ci 408b877906bSopenharmony_ci 409b877906bSopenharmony_ci## Guarantees and limitations {#guarantees_limitations} 410b877906bSopenharmony_ci 411b877906bSopenharmony_ciThis section describes the conditions under which GLFW can be expected to 412b877906bSopenharmony_cifunction, barring bugs in the operating system or drivers. Use of GLFW outside 413b877906bSopenharmony_cithese limits may work on some platforms, or on some machines, or some of the 414b877906bSopenharmony_citime, or on some versions of GLFW, but it may break at any time and this will 415b877906bSopenharmony_cinot be considered a bug. 416b877906bSopenharmony_ci 417b877906bSopenharmony_ci 418b877906bSopenharmony_ci### Pointer lifetimes {#lifetime} 419b877906bSopenharmony_ci 420b877906bSopenharmony_ciGLFW will never free any pointer you provide to it, and you must never free any 421b877906bSopenharmony_cipointer it provides to you. 422b877906bSopenharmony_ci 423b877906bSopenharmony_ciMany GLFW functions return pointers to dynamically allocated structures, strings 424b877906bSopenharmony_cior arrays, and some callbacks are provided with strings or arrays. These are 425b877906bSopenharmony_cialways managed by GLFW and should never be freed by the application. The 426b877906bSopenharmony_cilifetime of these pointers is documented for each GLFW function and callback. 427b877906bSopenharmony_ciIf you need to keep this data, you must copy it before its lifetime expires. 428b877906bSopenharmony_ci 429b877906bSopenharmony_ciMany GLFW functions accept pointers to structures or strings allocated by the 430b877906bSopenharmony_ciapplication. These are never freed by GLFW and are always the responsibility of 431b877906bSopenharmony_cithe application. If GLFW needs to keep the data in these structures or strings, 432b877906bSopenharmony_ciit is copied before the function returns. 433b877906bSopenharmony_ci 434b877906bSopenharmony_ciPointer lifetimes are guaranteed not to be shortened in future minor or patch 435b877906bSopenharmony_cireleases. 436b877906bSopenharmony_ci 437b877906bSopenharmony_ci 438b877906bSopenharmony_ci### Reentrancy {#reentrancy} 439b877906bSopenharmony_ci 440b877906bSopenharmony_ciGLFW event processing and object destruction are not reentrant. This means that 441b877906bSopenharmony_cithe following functions must not be called from any callback function: 442b877906bSopenharmony_ci 443b877906bSopenharmony_ci - @ref glfwDestroyWindow 444b877906bSopenharmony_ci - @ref glfwDestroyCursor 445b877906bSopenharmony_ci - @ref glfwPollEvents 446b877906bSopenharmony_ci - @ref glfwWaitEvents 447b877906bSopenharmony_ci - @ref glfwWaitEventsTimeout 448b877906bSopenharmony_ci - @ref glfwTerminate 449b877906bSopenharmony_ci 450b877906bSopenharmony_ciThese functions may be made reentrant in future minor or patch releases, but 451b877906bSopenharmony_cifunctions not on this list will not be made non-reentrant. 452b877906bSopenharmony_ci 453b877906bSopenharmony_ci 454b877906bSopenharmony_ci### Thread safety {#thread_safety} 455b877906bSopenharmony_ci 456b877906bSopenharmony_ciMost GLFW functions must only be called from the main thread (the thread that 457b877906bSopenharmony_cicalls main), but some may be called from any thread once the library has been 458b877906bSopenharmony_ciinitialized. Before initialization the whole library is thread-unsafe. 459b877906bSopenharmony_ci 460b877906bSopenharmony_ciThe reference documentation for every GLFW function states whether it is limited 461b877906bSopenharmony_cito the main thread. 462b877906bSopenharmony_ci 463b877906bSopenharmony_ciInitialization, termination, event processing and the creation and 464b877906bSopenharmony_cidestruction of windows, cursors and OpenGL and OpenGL ES contexts are all 465b877906bSopenharmony_cirestricted to the main thread due to limitations of one or several platforms. 466b877906bSopenharmony_ci 467b877906bSopenharmony_ciBecause event processing must be performed on the main thread, all callbacks 468b877906bSopenharmony_ciexcept for the error callback will only be called on that thread. The error 469b877906bSopenharmony_cicallback may be called on any thread, as any GLFW function may generate errors. 470b877906bSopenharmony_ci 471b877906bSopenharmony_ciThe error code and description may be queried from any thread. 472b877906bSopenharmony_ci 473b877906bSopenharmony_ci - @ref glfwGetError 474b877906bSopenharmony_ci 475b877906bSopenharmony_ciEmpty events may be posted from any thread. 476b877906bSopenharmony_ci 477b877906bSopenharmony_ci - @ref glfwPostEmptyEvent 478b877906bSopenharmony_ci 479b877906bSopenharmony_ciThe window user pointer and close flag may be read and written from any thread, 480b877906bSopenharmony_cibut this is not synchronized by GLFW. 481b877906bSopenharmony_ci 482b877906bSopenharmony_ci - @ref glfwGetWindowUserPointer 483b877906bSopenharmony_ci - @ref glfwSetWindowUserPointer 484b877906bSopenharmony_ci - @ref glfwWindowShouldClose 485b877906bSopenharmony_ci - @ref glfwSetWindowShouldClose 486b877906bSopenharmony_ci 487b877906bSopenharmony_ciThese functions for working with OpenGL and OpenGL ES contexts may be called 488b877906bSopenharmony_cifrom any thread, but the window object is not synchronized by GLFW. 489b877906bSopenharmony_ci 490b877906bSopenharmony_ci - @ref glfwMakeContextCurrent 491b877906bSopenharmony_ci - @ref glfwGetCurrentContext 492b877906bSopenharmony_ci - @ref glfwSwapBuffers 493b877906bSopenharmony_ci - @ref glfwSwapInterval 494b877906bSopenharmony_ci - @ref glfwExtensionSupported 495b877906bSopenharmony_ci - @ref glfwGetProcAddress 496b877906bSopenharmony_ci 497b877906bSopenharmony_ciThe raw timer functions may be called from any thread. 498b877906bSopenharmony_ci 499b877906bSopenharmony_ci - @ref glfwGetTimerFrequency 500b877906bSopenharmony_ci - @ref glfwGetTimerValue 501b877906bSopenharmony_ci 502b877906bSopenharmony_ciThe regular timer may be used from any thread, but reading and writing the timer 503b877906bSopenharmony_cioffset is not synchronized by GLFW. 504b877906bSopenharmony_ci 505b877906bSopenharmony_ci - @ref glfwGetTime 506b877906bSopenharmony_ci - @ref glfwSetTime 507b877906bSopenharmony_ci 508b877906bSopenharmony_ciLibrary version information may be queried from any thread. 509b877906bSopenharmony_ci 510b877906bSopenharmony_ci - @ref glfwGetVersion 511b877906bSopenharmony_ci - @ref glfwGetVersionString 512b877906bSopenharmony_ci 513b877906bSopenharmony_ciPlatform information may be queried from any thread. 514b877906bSopenharmony_ci 515b877906bSopenharmony_ci - @ref glfwPlatformSupported 516b877906bSopenharmony_ci - @ref glfwGetPlatform 517b877906bSopenharmony_ci 518b877906bSopenharmony_ciAll Vulkan related functions may be called from any thread. 519b877906bSopenharmony_ci 520b877906bSopenharmony_ci - @ref glfwVulkanSupported 521b877906bSopenharmony_ci - @ref glfwGetRequiredInstanceExtensions 522b877906bSopenharmony_ci - @ref glfwGetInstanceProcAddress 523b877906bSopenharmony_ci - @ref glfwGetPhysicalDevicePresentationSupport 524b877906bSopenharmony_ci - @ref glfwCreateWindowSurface 525b877906bSopenharmony_ci 526b877906bSopenharmony_ciGLFW uses synchronization objects internally only to manage the per-thread 527b877906bSopenharmony_cicontext and error states. Additional synchronization is left to the 528b877906bSopenharmony_ciapplication. 529b877906bSopenharmony_ci 530b877906bSopenharmony_ciFunctions that may currently be called from any thread will always remain so, 531b877906bSopenharmony_cibut functions that are currently limited to the main thread may be updated to 532b877906bSopenharmony_ciallow calls from any thread in future releases. 533b877906bSopenharmony_ci 534b877906bSopenharmony_ci 535b877906bSopenharmony_ci### Version compatibility {#compatibility} 536b877906bSopenharmony_ci 537b877906bSopenharmony_ciGLFW uses [Semantic Versioning](https://semver.org/). This guarantees source 538b877906bSopenharmony_ciand binary backward compatibility with earlier minor versions of the API. This 539b877906bSopenharmony_cimeans that you can drop in a newer version of the library and existing programs 540b877906bSopenharmony_ciwill continue to compile and existing binaries will continue to run. 541b877906bSopenharmony_ci 542b877906bSopenharmony_ciOnce a function or constant has been added, the signature of that function or 543b877906bSopenharmony_civalue of that constant will remain unchanged until the next major version of 544b877906bSopenharmony_ciGLFW. No compatibility of any kind is guaranteed between major versions. 545b877906bSopenharmony_ci 546b877906bSopenharmony_ciUndocumented behavior, i.e. behavior that is not described in the documentation, 547b877906bSopenharmony_cimay change at any time until it is documented. 548b877906bSopenharmony_ci 549b877906bSopenharmony_ciIf the reference documentation and the implementation differ, the reference 550b877906bSopenharmony_cidocumentation will almost always take precedence and the implementation will be 551b877906bSopenharmony_cifixed in the next release. The reference documentation will also take 552b877906bSopenharmony_ciprecedence over anything stated in a guide. 553b877906bSopenharmony_ci 554b877906bSopenharmony_ci 555b877906bSopenharmony_ci### Event order {#event_order} 556b877906bSopenharmony_ci 557b877906bSopenharmony_ciThe order of arrival of related events is not guaranteed to be consistent 558b877906bSopenharmony_ciacross platforms. The exception is synthetic key and mouse button release 559b877906bSopenharmony_cievents, which are always delivered after the window defocus event. 560b877906bSopenharmony_ci 561b877906bSopenharmony_ci 562b877906bSopenharmony_ci## Version management {#intro_version} 563b877906bSopenharmony_ci 564b877906bSopenharmony_ciGLFW provides mechanisms for identifying what version of GLFW your application 565b877906bSopenharmony_ciwas compiled against as well as what version it is currently running against. 566b877906bSopenharmony_ciIf you are loading GLFW dynamically (not just linking dynamically), you can use 567b877906bSopenharmony_cithis to verify that the library binary is compatible with your application. 568b877906bSopenharmony_ci 569b877906bSopenharmony_ci 570b877906bSopenharmony_ci### Compile-time version {#intro_version_compile} 571b877906bSopenharmony_ci 572b877906bSopenharmony_ciThe compile-time version of GLFW is provided by the GLFW header with the 573b877906bSopenharmony_ci`GLFW_VERSION_MAJOR`, `GLFW_VERSION_MINOR` and `GLFW_VERSION_REVISION` macros. 574b877906bSopenharmony_ci 575b877906bSopenharmony_ci```c 576b877906bSopenharmony_ciprintf("Compiled against GLFW %i.%i.%i\n", 577b877906bSopenharmony_ci GLFW_VERSION_MAJOR, 578b877906bSopenharmony_ci GLFW_VERSION_MINOR, 579b877906bSopenharmony_ci GLFW_VERSION_REVISION); 580b877906bSopenharmony_ci``` 581b877906bSopenharmony_ci 582b877906bSopenharmony_ci 583b877906bSopenharmony_ci### Run-time version {#intro_version_runtime} 584b877906bSopenharmony_ci 585b877906bSopenharmony_ciThe run-time version can be retrieved with @ref glfwGetVersion, a function that 586b877906bSopenharmony_cimay be called regardless of whether GLFW is initialized. 587b877906bSopenharmony_ci 588b877906bSopenharmony_ci```c 589b877906bSopenharmony_ciint major, minor, revision; 590b877906bSopenharmony_ciglfwGetVersion(&major, &minor, &revision); 591b877906bSopenharmony_ci 592b877906bSopenharmony_ciprintf("Running against GLFW %i.%i.%i\n", major, minor, revision); 593b877906bSopenharmony_ci``` 594b877906bSopenharmony_ci 595b877906bSopenharmony_ci 596b877906bSopenharmony_ci### Version string {#intro_version_string} 597b877906bSopenharmony_ci 598b877906bSopenharmony_ciGLFW 3 also provides a compile-time generated version string that describes the 599b877906bSopenharmony_civersion, platform, compiler and any platform-specific compile-time options. 600b877906bSopenharmony_ciThis is primarily intended for submitting bug reports, to allow developers to 601b877906bSopenharmony_cisee which code paths are enabled in a binary. 602b877906bSopenharmony_ci 603b877906bSopenharmony_ciThe version string is returned by @ref glfwGetVersionString, a function that may 604b877906bSopenharmony_cibe called regardless of whether GLFW is initialized. 605b877906bSopenharmony_ci 606b877906bSopenharmony_ci__Do not use the version string__ to parse the GLFW library version. The @ref 607b877906bSopenharmony_ciglfwGetVersion function already provides the version of the running library 608b877906bSopenharmony_cibinary. 609b877906bSopenharmony_ci 610b877906bSopenharmony_ci__Do not use the version string__ to parse what platforms are supported. The @ref 611b877906bSopenharmony_ciglfwPlatformSupported function lets you query platform support. 612b877906bSopenharmony_ci 613b877906bSopenharmony_ci__GLFW 3.4:__ The format of this string was changed to support the addition of 614b877906bSopenharmony_ci[runtime platform selection](@ref platform). 615b877906bSopenharmony_ci 616b877906bSopenharmony_ciThe format of the string is as follows: 617b877906bSopenharmony_ci - The version of GLFW 618b877906bSopenharmony_ci - For each supported platform: 619b877906bSopenharmony_ci - The name of the window system API 620b877906bSopenharmony_ci - The name of the window system specific context creation API, if applicable 621b877906bSopenharmony_ci - The names of the always supported context creation APIs EGL and OSMesa 622b877906bSopenharmony_ci - Any additional compile-time options, APIs and (on Windows) what compiler was used 623b877906bSopenharmony_ci 624b877906bSopenharmony_ciFor example, compiling GLFW 3.5 with MinGW as a DLL for Windows, may result in a version string 625b877906bSopenharmony_cilike this: 626b877906bSopenharmony_ci 627b877906bSopenharmony_ci```c 628b877906bSopenharmony_ci3.5.0 Win32 WGL Null EGL OSMesa MinGW DLL 629b877906bSopenharmony_ci``` 630b877906bSopenharmony_ci 631b877906bSopenharmony_ciCompiling GLFW as a static library for Linux, with both Wayland and X11 enabled, may 632b877906bSopenharmony_ciresult in a version string like this: 633b877906bSopenharmony_ci 634b877906bSopenharmony_ci```c 635b877906bSopenharmony_ci3.5.0 Wayland X11 GLX Null EGL OSMesa monotonic 636b877906bSopenharmony_ci``` 637b877906bSopenharmony_ci 638