| 1 | #if defined(SOKOL_IMPL) && !defined(SOKOL_APP_IMPL) |
| 2 | #define SOKOL_APP_IMPL |
| 3 | #endif |
| 4 | #ifndef SOKOL_APP_INCLUDED |
| 5 | /* |
| 6 | sokol_app.h -- cross-platform application wrapper |
| 7 | |
| 8 | Project URL: https://github.com/floooh/sokol |
| 9 | |
| 10 | Do this: |
| 11 | #define SOKOL_IMPL or |
| 12 | #define SOKOL_APP_IMPL |
| 13 | before you include this file in *one* C or C++ file to create the |
| 14 | implementation. |
| 15 | |
| 16 | In the same place define one of the following to select the 3D-API |
| 17 | which should be initialized by sokol_app.h (this must also match |
| 18 | the backend selected for sokol_gfx.h if both are used in the same |
| 19 | project): |
| 20 | |
| 21 | #define SOKOL_GLCORE |
| 22 | #define SOKOL_GLES3 |
| 23 | #define SOKOL_D3D11 |
| 24 | #define SOKOL_METAL |
| 25 | #define SOKOL_WGPU |
| 26 | #define SOKOL_NOAPI |
| 27 | |
| 28 | Optionally provide the following defines with your own implementations: |
| 29 | |
| 30 | SOKOL_ASSERT(c) - your own assert macro (default: assert(c)) |
| 31 | SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false)) |
| 32 | SOKOL_WIN32_FORCE_MAIN - define this on Win32 to use a main() entry point instead of WinMain |
| 33 | SOKOL_NO_ENTRY - define this if sokol_app.h shouldn't "hijack" the main() function |
| 34 | SOKOL_APP_API_DECL - public function declaration prefix (default: extern) |
| 35 | SOKOL_API_DECL - same as SOKOL_APP_API_DECL |
| 36 | SOKOL_API_IMPL - public function implementation prefix (default: -) |
| 37 | |
| 38 | Optionally define the following to force debug checks and validations |
| 39 | even in release mode: |
| 40 | |
| 41 | SOKOL_DEBUG - by default this is defined if _DEBUG is defined |
| 42 | |
| 43 | If sokol_app.h is compiled as a DLL, define the following before |
| 44 | including the declaration or implementation: |
| 45 | |
| 46 | SOKOL_DLL |
| 47 | |
| 48 | On Windows, SOKOL_DLL will define SOKOL_APP_API_DECL as __declspec(dllexport) |
| 49 | or __declspec(dllimport) as needed. |
| 50 | |
| 51 | On Linux, SOKOL_GLCORE can use either GLX or EGL. |
| 52 | GLX is default, set SOKOL_FORCE_EGL to override. |
| 53 | |
| 54 | For example code, see https://github.com/floooh/sokol-samples/tree/master/sapp |
| 55 | |
| 56 | Portions of the Windows and Linux GL initialization, event-, icon- etc... code |
| 57 | have been taken from GLFW (http://www.glfw.org/) |
| 58 | |
| 59 | iOS onscreen keyboard support 'inspired' by libgdx. |
| 60 | |
| 61 | Link with the following system libraries: |
| 62 | |
| 63 | - on macOS with Metal: Cocoa, QuartzCore, Metal, MetalKit |
| 64 | - on macOS with GL: Cocoa, QuartzCore, OpenGL |
| 65 | - on iOS with Metal: Foundation, UIKit, Metal, MetalKit |
| 66 | - on iOS with GL: Foundation, UIKit, OpenGLES, GLKit |
| 67 | - on Linux with EGL: X11, Xi, Xcursor, EGL, GL (or GLESv2), dl, pthread, m(?) |
| 68 | - on Linux with GLX: X11, Xi, Xcursor, GL, dl, pthread, m(?) |
| 69 | - on Android: GLESv3, EGL, log, android |
| 70 | - on Windows with the MSVC or Clang toolchains: no action needed, libs are defined in-source via pragma-comment-lib |
| 71 | - on Windows with MINGW/MSYS2 gcc: compile with '-mwin32' so that _WIN32 is defined |
| 72 | - link with the following libs: -lkernel32 -luser32 -lshell32 |
| 73 | - additionally with the GL backend: -lgdi32 |
| 74 | - additionally with the D3D11 backend: -ld3d11 -ldxgi |
| 75 | |
| 76 | On Linux, you also need to use the -pthread compiler and linker option, otherwise weird |
| 77 | things will happen, see here for details: https://github.com/floooh/sokol/issues/376 |
| 78 | |
| 79 | On macOS and iOS, the implementation must be compiled as Objective-C. |
| 80 | |
| 81 | FEATURE OVERVIEW |
| 82 | ================ |
| 83 | sokol_app.h provides a minimalistic cross-platform API which |
| 84 | implements the 'application-wrapper' parts of a 3D application: |
| 85 | |
| 86 | - a common application entry function |
| 87 | - creates a window and 3D-API context/device with a 'default framebuffer' |
| 88 | - makes the rendered frame visible |
| 89 | - provides keyboard-, mouse- and low-level touch-events |
| 90 | - platforms: MacOS, iOS, HTML5, Win32, Linux/RaspberryPi, Android |
| 91 | - 3D-APIs: Metal, D3D11, GL3.2, GLES3, WebGL, WebGL2, NOAPI |
| 92 | |
| 93 | FEATURE/PLATFORM MATRIX |
| 94 | ======================= |
| 95 | | Windows | macOS | Linux | iOS | Android | HTML5 |
| 96 | --------------------+---------+-------+-------+-------+---------+-------- |
| 97 | gl 3.x | YES | YES | YES | --- | --- | --- |
| 98 | gles3/webgl2 | --- | --- | YES(2)| YES | YES | YES |
| 99 | metal | --- | YES | --- | YES | --- | --- |
| 100 | d3d11 | YES | --- | --- | --- | --- | --- |
| 101 | noapi | YES | TODO | TODO | --- | TODO | --- |
| 102 | KEY_DOWN | YES | YES | YES | SOME | TODO | YES |
| 103 | KEY_UP | YES | YES | YES | SOME | TODO | YES |
| 104 | CHAR | YES | YES | YES | YES | TODO | YES |
| 105 | MOUSE_DOWN | YES | YES | YES | --- | --- | YES |
| 106 | MOUSE_UP | YES | YES | YES | --- | --- | YES |
| 107 | MOUSE_SCROLL | YES | YES | YES | --- | --- | YES |
| 108 | MOUSE_MOVE | YES | YES | YES | --- | --- | YES |
| 109 | MOUSE_ENTER | YES | YES | YES | --- | --- | YES |
| 110 | MOUSE_LEAVE | YES | YES | YES | --- | --- | YES |
| 111 | TOUCHES_BEGAN | --- | --- | --- | YES | YES | YES |
| 112 | TOUCHES_MOVED | --- | --- | --- | YES | YES | YES |
| 113 | TOUCHES_ENDED | --- | --- | --- | YES | YES | YES |
| 114 | TOUCHES_CANCELLED | --- | --- | --- | YES | YES | YES |
| 115 | RESIZED | YES | YES | YES | YES | YES | YES |
| 116 | ICONIFIED | YES | YES | YES | --- | --- | --- |
| 117 | RESTORED | YES | YES | YES | --- | --- | --- |
| 118 | FOCUSED | YES | YES | YES | --- | --- | YES |
| 119 | UNFOCUSED | YES | YES | YES | --- | --- | YES |
| 120 | SUSPENDED | --- | --- | --- | YES | YES | TODO |
| 121 | RESUMED | --- | --- | --- | YES | YES | TODO |
| 122 | QUIT_REQUESTED | YES | YES | YES | --- | --- | YES |
| 123 | IME | TODO | TODO? | TODO | ??? | TODO | ??? |
| 124 | key repeat flag | YES | YES | YES | --- | --- | YES |
| 125 | windowed | YES | YES | YES | --- | --- | YES |
| 126 | fullscreen | YES | YES | YES | YES | YES | --- |
| 127 | mouse hide | YES | YES | YES | --- | --- | YES |
| 128 | mouse lock | YES | YES | YES | --- | --- | YES |
| 129 | set cursor type | YES | YES | YES | --- | --- | YES |
| 130 | screen keyboard | --- | --- | --- | YES | TODO | YES |
| 131 | swap interval | YES | YES | YES | YES | TODO | YES |
| 132 | high-dpi | YES | YES | TODO | YES | YES | YES |
| 133 | clipboard | YES | YES | TODO | --- | --- | YES |
| 134 | MSAA | YES | YES | YES | YES | YES | YES |
| 135 | drag'n'drop | YES | YES | YES | --- | --- | YES |
| 136 | window icon | YES | YES(1)| YES | --- | --- | YES |
| 137 | |
| 138 | (1) macOS has no regular window icons, instead the dock icon is changed |
| 139 | (2) supported with EGL only (not GLX) |
| 140 | |
| 141 | STEP BY STEP |
| 142 | ============ |
| 143 | --- Add a sokol_main() function to your code which returns a sapp_desc structure |
| 144 | with initialization parameters and callback function pointers. This |
| 145 | function is called very early, usually at the start of the |
| 146 | platform's entry function (e.g. main or WinMain). You should do as |
| 147 | little as possible here, since the rest of your code might be called |
| 148 | from another thread (this depends on the platform): |
| 149 | |
| 150 | sapp_desc sokol_main(int argc, char* argv[]) { |
| 151 | return (sapp_desc) { |
| 152 | .width = 640, |
| 153 | .height = 480, |
| 154 | .init_cb = my_init_func, |
| 155 | .frame_cb = my_frame_func, |
| 156 | .cleanup_cb = my_cleanup_func, |
| 157 | .event_cb = my_event_func, |
| 158 | ... |
| 159 | }; |
| 160 | } |
| 161 | |
| 162 | To get any logging output in case of errors you need to provide a log |
| 163 | callback. The easiest way is via sokol_log.h: |
| 164 | |
| 165 | #include "sokol_log.h" |
| 166 | |
| 167 | sapp_desc sokol_main(int argc, char* argv[]) { |
| 168 | return (sapp_desc) { |
| 169 | ... |
| 170 | .logger.func = slog_func, |
| 171 | }; |
| 172 | } |
| 173 | |
| 174 | There are many more setup parameters, but these are the most important. |
| 175 | For a complete list search for the sapp_desc structure declaration |
| 176 | below. |
| 177 | |
| 178 | DO NOT call any sokol-app function from inside sokol_main(), since |
| 179 | sokol-app will not be initialized at this point. |
| 180 | |
| 181 | The .width and .height parameters are the preferred size of the 3D |
| 182 | rendering canvas. The actual size may differ from this depending on |
| 183 | platform and other circumstances. Also the canvas size may change at |
| 184 | any time (for instance when the user resizes the application window, |
| 185 | or rotates the mobile device). You can just keep .width and .height |
| 186 | zero-initialized to open a default-sized window (what "default-size" |
| 187 | exactly means is platform-specific, but usually it's a size that covers |
| 188 | most of, but not all, of the display). |
| 189 | |
| 190 | All provided function callbacks will be called from the same thread, |
| 191 | but this may be different from the thread where sokol_main() was called. |
| 192 | |
| 193 | .init_cb (void (*)(void)) |
| 194 | This function is called once after the application window, |
| 195 | 3D rendering context and swap chain have been created. The |
| 196 | function takes no arguments and has no return value. |
| 197 | .frame_cb (void (*)(void)) |
| 198 | This is the per-frame callback, which is usually called 60 |
| 199 | times per second. This is where your application would update |
| 200 | most of its state and perform all rendering. |
| 201 | .cleanup_cb (void (*)(void)) |
| 202 | The cleanup callback is called once right before the application |
| 203 | quits. |
| 204 | .event_cb (void (*)(const sapp_event* event)) |
| 205 | The event callback is mainly for input handling, but is also |
| 206 | used to communicate other types of events to the application. Keep the |
| 207 | event_cb struct member zero-initialized if your application doesn't require |
| 208 | event handling. |
| 209 | |
| 210 | As you can see, those 'standard callbacks' don't have a user_data |
| 211 | argument, so any data that needs to be preserved between callbacks |
| 212 | must live in global variables. If keeping state in global variables |
| 213 | is not an option, there's an alternative set of callbacks with |
| 214 | an additional user_data pointer argument: |
| 215 | |
| 216 | .user_data (void*) |
| 217 | The user-data argument for the callbacks below |
| 218 | .init_userdata_cb (void (*)(void* user_data)) |
| 219 | .frame_userdata_cb (void (*)(void* user_data)) |
| 220 | .cleanup_userdata_cb (void (*)(void* user_data)) |
| 221 | .event_userdata_cb (void(*)(const sapp_event* event, void* user_data)) |
| 222 | |
| 223 | The function sapp_userdata() can be used to query the user_data |
| 224 | pointer provided in the sapp_desc struct. |
| 225 | |
| 226 | You can also call sapp_query_desc() to get a copy of the |
| 227 | original sapp_desc structure. |
| 228 | |
| 229 | NOTE that there's also an alternative compile mode where sokol_app.h |
| 230 | doesn't "hijack" the main() function. Search below for SOKOL_NO_ENTRY. |
| 231 | |
| 232 | --- Implement the initialization callback function (init_cb), this is called |
| 233 | once after the rendering surface, 3D API and swap chain have been |
| 234 | initialized by sokol_app. All sokol-app functions can be called |
| 235 | from inside the initialization callback, the most useful functions |
| 236 | at this point are: |
| 237 | |
| 238 | int sapp_width(void) |
| 239 | int sapp_height(void) |
| 240 | Returns the current width and height of the default framebuffer in pixels, |
| 241 | this may change from one frame to the next, and it may be different |
| 242 | from the initial size provided in the sapp_desc struct. |
| 243 | |
| 244 | float sapp_widthf(void) |
| 245 | float sapp_heightf(void) |
| 246 | These are alternatives to sapp_width() and sapp_height() which return |
| 247 | the default framebuffer size as float values instead of integer. This |
| 248 | may help to prevent casting back and forth between int and float |
| 249 | in more strongly typed languages than C and C++. |
| 250 | |
| 251 | double sapp_frame_duration(void) |
| 252 | Returns the frame duration in seconds averaged over a number of |
| 253 | frames to smooth out any jittering spikes. |
| 254 | |
| 255 | int sapp_color_format(void) |
| 256 | int sapp_depth_format(void) |
| 257 | The color and depth-stencil pixelformats of the default framebuffer, |
| 258 | as integer values which are compatible with sokol-gfx's |
| 259 | sg_pixel_format enum (so that they can be plugged directly in places |
| 260 | where sg_pixel_format is expected). Possible values are: |
| 261 | |
| 262 | 23 == SG_PIXELFORMAT_RGBA8 |
| 263 | 28 == SG_PIXELFORMAT_BGRA8 |
| 264 | 42 == SG_PIXELFORMAT_DEPTH |
| 265 | 43 == SG_PIXELFORMAT_DEPTH_STENCIL |
| 266 | |
| 267 | int sapp_sample_count(void) |
| 268 | Return the MSAA sample count of the default framebuffer. |
| 269 | |
| 270 | const void* sapp_metal_get_device(void) |
| 271 | const void* sapp_metal_get_current_drawable(void) |
| 272 | const void* sapp_metal_get_depth_stencil_texture(void) |
| 273 | const void* sapp_metal_get_msaa_color_texture(void) |
| 274 | If the Metal backend has been selected, these functions return pointers |
| 275 | to various Metal API objects required for rendering, otherwise |
| 276 | they return a null pointer. These void pointers are actually |
| 277 | Objective-C ids converted with a (ARC) __bridge cast so that |
| 278 | the ids can be tunnel through C code. Also note that the returned |
| 279 | pointers to the renderpass-descriptor and drawable may change from one |
| 280 | frame to the next, only the Metal device object is guaranteed to |
| 281 | stay the same. |
| 282 | |
| 283 | const void* sapp_macos_get_window(void) |
| 284 | On macOS, get the NSWindow object pointer, otherwise a null pointer. |
| 285 | Before being used as Objective-C object, the void* must be converted |
| 286 | back with a (ARC) __bridge cast. |
| 287 | |
| 288 | const void* sapp_ios_get_window(void) |
| 289 | On iOS, get the UIWindow object pointer, otherwise a null pointer. |
| 290 | Before being used as Objective-C object, the void* must be converted |
| 291 | back with a (ARC) __bridge cast. |
| 292 | |
| 293 | const void* sapp_d3d11_get_device(void) |
| 294 | const void* sapp_d3d11_get_device_context(void) |
| 295 | const void* sapp_d3d11_get_render_view(void) |
| 296 | const void* sapp_d3d11_get_resolve_view(void); |
| 297 | const void* sapp_d3d11_get_depth_stencil_view(void) |
| 298 | Similar to the sapp_metal_* functions, the sapp_d3d11_* functions |
| 299 | return pointers to D3D11 API objects required for rendering, |
| 300 | only if the D3D11 backend has been selected. Otherwise they |
| 301 | return a null pointer. Note that the returned pointers to the |
| 302 | render-target-view and depth-stencil-view may change from one |
| 303 | frame to the next! |
| 304 | |
| 305 | const void* sapp_win32_get_hwnd(void) |
| 306 | On Windows, get the window's HWND, otherwise a null pointer. The |
| 307 | HWND has been cast to a void pointer in order to be tunneled |
| 308 | through code which doesn't include Windows.h. |
| 309 | |
| 310 | const void* sapp_wgpu_get_device(void) |
| 311 | const void* sapp_wgpu_get_render_view(void) |
| 312 | const void* sapp_wgpu_get_resolve_view(void) |
| 313 | const void* sapp_wgpu_get_depth_stencil_view(void) |
| 314 | These are the WebGPU-specific functions to get the WebGPU |
| 315 | objects and values required for rendering. If sokol_app.h |
| 316 | is not compiled with SOKOL_WGPU, these functions return null. |
| 317 | |
| 318 | uint32_t sapp_gl_get_framebuffer(void) |
| 319 | This returns the 'default framebuffer' of the GL context. |
| 320 | Typically this will be zero. |
| 321 | |
| 322 | int sapp_gl_get_major_version(void) |
| 323 | int sapp_gl_get_minor_version(void) |
| 324 | Returns the major and minor version of the GL context |
| 325 | (only for SOKOL_GLCORE, all other backends return zero here, including SOKOL_GLES3) |
| 326 | |
| 327 | const void* sapp_android_get_native_activity(void); |
| 328 | On Android, get the native activity ANativeActivity pointer, otherwise |
| 329 | a null pointer. |
| 330 | |
| 331 | --- Implement the frame-callback function, this function will be called |
| 332 | on the same thread as the init callback, but might be on a different |
| 333 | thread than the sokol_main() function. Note that the size of |
| 334 | the rendering framebuffer might have changed since the frame callback |
| 335 | was called last. Call the functions sapp_width() and sapp_height() |
| 336 | each frame to get the current size. |
| 337 | |
| 338 | --- Optionally implement the event-callback to handle input events. |
| 339 | sokol-app provides the following type of input events: |
| 340 | - a 'virtual key' was pressed down or released |
| 341 | - a single text character was entered (provided as UTF-32 code point) |
| 342 | - a mouse button was pressed down or released (left, right, middle) |
| 343 | - mouse-wheel or 2D scrolling events |
| 344 | - the mouse was moved |
| 345 | - the mouse has entered or left the application window boundaries |
| 346 | - low-level, portable multi-touch events (began, moved, ended, cancelled) |
| 347 | - the application window was resized, iconified or restored |
| 348 | - the application was suspended or restored (on mobile platforms) |
| 349 | - the user or application code has asked to quit the application |
| 350 | - a string was pasted to the system clipboard |
| 351 | - one or more files have been dropped onto the application window |
| 352 | |
| 353 | To explicitly 'consume' an event and prevent that the event is |
| 354 | forwarded for further handling to the operating system, call |
| 355 | sapp_consume_event() from inside the event handler (NOTE that |
| 356 | this behaviour is currently only implemented for some HTML5 |
| 357 | events, support for other platforms and event types will |
| 358 | be added as needed, please open a GitHub ticket and/or provide |
| 359 | a PR if needed). |
| 360 | |
| 361 | NOTE: Do *not* call any 3D API rendering functions in the event |
| 362 | callback function, since the 3D API context may not be active when the |
| 363 | event callback is called (it may work on some platforms and 3D APIs, |
| 364 | but not others, and the exact behaviour may change between |
| 365 | sokol-app versions). |
| 366 | |
| 367 | --- Implement the cleanup-callback function, this is called once |
| 368 | after the user quits the application (see the section |
| 369 | "APPLICATION QUIT" for detailed information on quitting |
| 370 | behaviour, and how to intercept a pending quit - for instance to show a |
| 371 | "Really Quit?" dialog box). Note that the cleanup-callback isn't |
| 372 | guaranteed to be called on the web and mobile platforms. |
| 373 | |
| 374 | MOUSE CURSOR TYPE AND VISIBILITY |
| 375 | ================================ |
| 376 | You can show and hide the mouse cursor with |
| 377 | |
| 378 | void sapp_show_mouse(bool show) |
| 379 | |
| 380 | And to get the current shown status: |
| 381 | |
| 382 | bool sapp_mouse_shown(void) |
| 383 | |
| 384 | NOTE that hiding the mouse cursor is different and independent from |
| 385 | the MOUSE/POINTER LOCK feature which will also hide the mouse pointer when |
| 386 | active (MOUSE LOCK is described below). |
| 387 | |
| 388 | To change the mouse cursor to one of several predefined types, call |
| 389 | the function: |
| 390 | |
| 391 | void sapp_set_mouse_cursor(sapp_mouse_cursor cursor) |
| 392 | |
| 393 | Setting the default mouse cursor SAPP_MOUSECURSOR_DEFAULT will restore |
| 394 | the standard look. |
| 395 | |
| 396 | To get the currently active mouse cursor type, call: |
| 397 | |
| 398 | sapp_mouse_cursor sapp_get_mouse_cursor(void) |
| 399 | |
| 400 | MOUSE LOCK (AKA POINTER LOCK, AKA MOUSE CAPTURE) |
| 401 | ================================================ |
| 402 | In normal mouse mode, no mouse movement events are reported when the |
| 403 | mouse leaves the windows client area or hits the screen border (whether |
| 404 | it's one or the other depends on the platform), and the mouse move events |
| 405 | (SAPP_EVENTTYPE_MOUSE_MOVE) contain absolute mouse positions in |
| 406 | framebuffer pixels in the sapp_event items mouse_x and mouse_y, and |
| 407 | relative movement in framebuffer pixels in the sapp_event items mouse_dx |
| 408 | and mouse_dy. |
| 409 | |
| 410 | To get continuous mouse movement (also when the mouse leaves the window |
| 411 | client area or hits the screen border), activate mouse-lock mode |
| 412 | by calling: |
| 413 | |
| 414 | sapp_lock_mouse(true) |
| 415 | |
| 416 | When mouse lock is activated, the mouse pointer is hidden, the |
| 417 | reported absolute mouse position (sapp_event.mouse_x/y) appears |
| 418 | frozen, and the relative mouse movement in sapp_event.mouse_dx/dy |
| 419 | no longer has a direct relation to framebuffer pixels but instead |
| 420 | uses "raw mouse input" (what "raw mouse input" exactly means also |
| 421 | differs by platform). |
| 422 | |
| 423 | To deactivate mouse lock and return to normal mouse mode, call |
| 424 | |
| 425 | sapp_lock_mouse(false) |
| 426 | |
| 427 | And finally, to check if mouse lock is currently active, call |
| 428 | |
| 429 | if (sapp_mouse_locked()) { ... } |
| 430 | |
| 431 | On native platforms, the sapp_lock_mouse() and sapp_mouse_locked() |
| 432 | functions work as expected (mouse lock is activated or deactivated |
| 433 | immediately when sapp_lock_mouse() is called, and sapp_mouse_locked() |
| 434 | also immediately returns the new state after sapp_lock_mouse() |
| 435 | is called. |
| 436 | |
| 437 | On the web platform, sapp_lock_mouse() and sapp_mouse_locked() behave |
| 438 | differently, as dictated by the limitations of the HTML5 Pointer Lock API: |
| 439 | |
| 440 | - sapp_lock_mouse(true) can be called at any time, but it will |
| 441 | only take effect in a 'short-lived input event handler of a specific |
| 442 | type', meaning when one of the following events happens: |
| 443 | - SAPP_EVENTTYPE_MOUSE_DOWN |
| 444 | - SAPP_EVENTTYPE_MOUSE_UP |
| 445 | - SAPP_EVENTTYPE_MOUSE_SCROLL |
| 446 | - SAPP_EVENTTYPE_KEY_UP |
| 447 | - SAPP_EVENTTYPE_KEY_DOWN |
| 448 | - The mouse lock/unlock action on the web platform is asynchronous, |
| 449 | this means that sapp_mouse_locked() won't immediately return |
| 450 | the new status after calling sapp_lock_mouse(), instead the |
| 451 | reported status will only change when the pointer lock has actually |
| 452 | been activated or deactivated in the browser. |
| 453 | - On the web, mouse lock can be deactivated by the user at any time |
| 454 | by pressing the Esc key. When this happens, sokol_app.h behaves |
| 455 | the same as if sapp_lock_mouse(false) is called. |
| 456 | |
| 457 | For things like camera manipulation it's most straightforward to lock |
| 458 | and unlock the mouse right from the sokol_app.h event handler, for |
| 459 | instance the following code enters and leaves mouse lock when the |
| 460 | left mouse button is pressed and released, and then uses the relative |
| 461 | movement information to manipulate a camera (taken from the |
| 462 | cgltf-sapp.c sample in the sokol-samples repository |
| 463 | at https://github.com/floooh/sokol-samples): |
| 464 | |
| 465 | static void input(const sapp_event* ev) { |
| 466 | switch (ev->type) { |
| 467 | case SAPP_EVENTTYPE_MOUSE_DOWN: |
| 468 | if (ev->mouse_button == SAPP_MOUSEBUTTON_LEFT) { |
| 469 | sapp_lock_mouse(true); |
| 470 | } |
| 471 | break; |
| 472 | |
| 473 | case SAPP_EVENTTYPE_MOUSE_UP: |
| 474 | if (ev->mouse_button == SAPP_MOUSEBUTTON_LEFT) { |
| 475 | sapp_lock_mouse(false); |
| 476 | } |
| 477 | break; |
| 478 | |
| 479 | case SAPP_EVENTTYPE_MOUSE_MOVE: |
| 480 | if (sapp_mouse_locked()) { |
| 481 | cam_orbit(&state.camera, ev->mouse_dx * 0.25f, ev->mouse_dy * 0.25f); |
| 482 | } |
| 483 | break; |
| 484 | |
| 485 | default: |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | CLIPBOARD SUPPORT |
| 491 | ================= |
| 492 | Applications can send and receive UTF-8 encoded text data from and to the |
| 493 | system clipboard. By default, clipboard support is disabled and |
| 494 | must be enabled at startup via the following sapp_desc struct |
| 495 | members: |
| 496 | |
| 497 | sapp_desc.enable_clipboard - set to true to enable clipboard support |
| 498 | sapp_desc.clipboard_size - size of the internal clipboard buffer in bytes |
| 499 | |
| 500 | Enabling the clipboard will dynamically allocate a clipboard buffer |
| 501 | for UTF-8 encoded text data of the requested size in bytes, the default |
| 502 | size is 8 KBytes. Strings that don't fit into the clipboard buffer |
| 503 | (including the terminating zero) will be silently clipped, so it's |
| 504 | important that you provide a big enough clipboard size for your |
| 505 | use case. |
| 506 | |
| 507 | To send data to the clipboard, call sapp_set_clipboard_string() with |
| 508 | a pointer to an UTF-8 encoded, null-terminated C-string. |
| 509 | |
| 510 | NOTE that on the HTML5 platform, sapp_set_clipboard_string() must be |
| 511 | called from inside a 'short-lived event handler', and there are a few |
| 512 | other HTML5-specific caveats to workaround. You'll basically have to |
| 513 | tinker until it works in all browsers :/ (maybe the situation will |
| 514 | improve when all browsers agree on and implement the new |
| 515 | HTML5 navigator.clipboard API). |
| 516 | |
| 517 | To get data from the clipboard, check for the SAPP_EVENTTYPE_CLIPBOARD_PASTED |
| 518 | event in your event handler function, and then call sapp_get_clipboard_string() |
| 519 | to obtain the pasted UTF-8 encoded text. |
| 520 | |
| 521 | NOTE that behaviour of sapp_get_clipboard_string() is slightly different |
| 522 | depending on platform: |
| 523 | |
| 524 | - on the HTML5 platform, the internal clipboard buffer will only be updated |
| 525 | right before the SAPP_EVENTTYPE_CLIPBOARD_PASTED event is sent, |
| 526 | and sapp_get_clipboard_string() will simply return the current content |
| 527 | of the clipboard buffer |
| 528 | - on 'native' platforms, the call to sapp_get_clipboard_string() will |
| 529 | update the internal clipboard buffer with the most recent data |
| 530 | from the system clipboard |
| 531 | |
| 532 | Portable code should check for the SAPP_EVENTTYPE_CLIPBOARD_PASTED event, |
| 533 | and then call sapp_get_clipboard_string() right in the event handler. |
| 534 | |
| 535 | The SAPP_EVENTTYPE_CLIPBOARD_PASTED event will be generated by sokol-app |
| 536 | as follows: |
| 537 | |
| 538 | - on macOS: when the Cmd+V key is pressed down |
| 539 | - on HTML5: when the browser sends a 'paste' event to the global 'window' object |
| 540 | - on all other platforms: when the Ctrl+V key is pressed down |
| 541 | |
| 542 | DRAG AND DROP SUPPORT |
| 543 | ===================== |
| 544 | PLEASE NOTE: the drag'n'drop feature works differently on WASM/HTML5 |
| 545 | and on the native desktop platforms (Win32, Linux and macOS) because |
| 546 | of security-related restrictions in the HTML5 drag'n'drop API. The |
| 547 | WASM/HTML5 specifics are described at the end of this documentation |
| 548 | section: |
| 549 | |
| 550 | Like clipboard support, drag'n'drop support must be explicitly enabled |
| 551 | at startup in the sapp_desc struct. |
| 552 | |
| 553 | sapp_desc sokol_main(void) { |
| 554 | return (sapp_desc) { |
| 555 | .enable_dragndrop = true, // default is false |
| 556 | ... |
| 557 | }; |
| 558 | } |
| 559 | |
| 560 | You can also adjust the maximum number of files that are accepted |
| 561 | in a drop operation, and the maximum path length in bytes if needed: |
| 562 | |
| 563 | sapp_desc sokol_main(void) { |
| 564 | return (sapp_desc) { |
| 565 | .enable_dragndrop = true, // default is false |
| 566 | .max_dropped_files = 8, // default is 1 |
| 567 | .max_dropped_file_path_length = 8192, // in bytes, default is 2048 |
| 568 | ... |
| 569 | }; |
| 570 | } |
| 571 | |
| 572 | When drag'n'drop is enabled, the event callback will be invoked with an |
| 573 | event of type SAPP_EVENTTYPE_FILES_DROPPED whenever the user drops files on |
| 574 | the application window. |
| 575 | |
| 576 | After the SAPP_EVENTTYPE_FILES_DROPPED is received, you can query the |
| 577 | number of dropped files, and their absolute paths by calling separate |
| 578 | functions: |
| 579 | |
| 580 | void on_event(const sapp_event* ev) { |
| 581 | if (ev->type == SAPP_EVENTTYPE_FILES_DROPPED) { |
| 582 | |
| 583 | // the mouse position where the drop happened |
| 584 | float x = ev->mouse_x; |
| 585 | float y = ev->mouse_y; |
| 586 | |
| 587 | // get the number of files and their paths like this: |
| 588 | const int num_dropped_files = sapp_get_num_dropped_files(); |
| 589 | for (int i = 0; i < num_dropped_files; i++) { |
| 590 | const char* path = sapp_get_dropped_file_path(i); |
| 591 | ... |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | The returned file paths are UTF-8 encoded strings. |
| 597 | |
| 598 | You can call sapp_get_num_dropped_files() and sapp_get_dropped_file_path() |
| 599 | anywhere, also outside the event handler callback, but be aware that the |
| 600 | file path strings will be overwritten with the next drop operation. |
| 601 | |
| 602 | In any case, sapp_get_dropped_file_path() will never return a null pointer, |
| 603 | instead an empty string "" will be returned if the drag'n'drop feature |
| 604 | hasn't been enabled, the last drop-operation failed, or the file path index |
| 605 | is out of range. |
| 606 | |
| 607 | Drag'n'drop caveats: |
| 608 | |
| 609 | - if more files are dropped in a single drop-action |
| 610 | than sapp_desc.max_dropped_files, the additional |
| 611 | files will be silently ignored |
| 612 | - if any of the file paths is longer than |
| 613 | sapp_desc.max_dropped_file_path_length (in number of bytes, after UTF-8 |
| 614 | encoding) the entire drop operation will be silently ignored (this |
| 615 | needs some sort of error feedback in the future) |
| 616 | - no mouse positions are reported while the drag is in |
| 617 | process, this may change in the future |
| 618 | |
| 619 | Drag'n'drop on HTML5/WASM: |
| 620 | |
| 621 | The HTML5 drag'n'drop API doesn't return file paths, but instead |
| 622 | black-box 'file objects' which must be used to load the content |
| 623 | of dropped files. This is the reason why sokol_app.h adds two |
| 624 | HTML5-specific functions to the drag'n'drop API: |
| 625 | |
| 626 | uint32_t sapp_html5_get_dropped_file_size(int index) |
| 627 | Returns the size in bytes of a dropped file. |
| 628 | |
| 629 | void sapp_html5_fetch_dropped_file(const sapp_html5_fetch_request* request) |
| 630 | Asynchronously loads the content of a dropped file into a |
| 631 | provided memory buffer (which must be big enough to hold |
| 632 | the file content) |
| 633 | |
| 634 | To start loading the first dropped file after an SAPP_EVENTTYPE_FILES_DROPPED |
| 635 | event is received: |
| 636 | |
| 637 | sapp_html5_fetch_dropped_file(&(sapp_html5_fetch_request){ |
| 638 | .dropped_file_index = 0, |
| 639 | .callback = fetch_cb |
| 640 | .buffer = { |
| 641 | .ptr = buf, |
| 642 | .size = sizeof(buf) |
| 643 | }, |
| 644 | .user_data = ... |
| 645 | }); |
| 646 | |
| 647 | Make sure that the memory pointed to by 'buf' stays valid until the |
| 648 | callback function is called! |
| 649 | |
| 650 | As result of the asynchronous loading operation (no matter if succeeded or |
| 651 | failed) the 'fetch_cb' function will be called: |
| 652 | |
| 653 | void fetch_cb(const sapp_html5_fetch_response* response) { |
| 654 | // IMPORTANT: check if the loading operation actually succeeded: |
| 655 | if (response->succeeded) { |
| 656 | // the size of the loaded file: |
| 657 | const size_t num_bytes = response->data.size; |
| 658 | // and the pointer to the data (same as 'buf' in the fetch-call): |
| 659 | const void* ptr = response->data.ptr; |
| 660 | } |
| 661 | else { |
| 662 | // on error check the error code: |
| 663 | switch (response->error_code) { |
| 664 | case SAPP_HTML5_FETCH_ERROR_BUFFER_TOO_SMALL: |
| 665 | ... |
| 666 | break; |
| 667 | case SAPP_HTML5_FETCH_ERROR_OTHER: |
| 668 | ... |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | Check the droptest-sapp example for a real-world example which works |
| 675 | both on native platforms and the web: |
| 676 | |
| 677 | https://github.com/floooh/sokol-samples/blob/master/sapp/droptest-sapp.c |
| 678 | |
| 679 | HIGH-DPI RENDERING |
| 680 | ================== |
| 681 | You can set the sapp_desc.high_dpi flag during initialization to request |
| 682 | a full-resolution framebuffer on HighDPI displays. The default behaviour |
| 683 | is sapp_desc.high_dpi=false, this means that the application will |
| 684 | render to a lower-resolution framebuffer on HighDPI displays and the |
| 685 | rendered content will be upscaled by the window system composer. |
| 686 | |
| 687 | In a HighDPI scenario, you still request the same window size during |
| 688 | sokol_main(), but the framebuffer sizes returned by sapp_width() |
| 689 | and sapp_height() will be scaled up according to the DPI scaling |
| 690 | ratio. |
| 691 | |
| 692 | Note that on some platforms the DPI scaling factor may change at any |
| 693 | time (for instance when a window is moved from a high-dpi display |
| 694 | to a low-dpi display). |
| 695 | |
| 696 | To query the current DPI scaling factor, call the function: |
| 697 | |
| 698 | float sapp_dpi_scale(void); |
| 699 | |
| 700 | For instance on a Retina Mac, returning the following sapp_desc |
| 701 | struct from sokol_main(): |
| 702 | |
| 703 | sapp_desc sokol_main(void) { |
| 704 | return (sapp_desc) { |
| 705 | .width = 640, |
| 706 | .height = 480, |
| 707 | .high_dpi = true, |
| 708 | ... |
| 709 | }; |
| 710 | } |
| 711 | |
| 712 | ...the functions the functions sapp_width(), sapp_height() |
| 713 | and sapp_dpi_scale() will return the following values: |
| 714 | |
| 715 | sapp_width: 1280 |
| 716 | sapp_height: 960 |
| 717 | sapp_dpi_scale: 2.0 |
| 718 | |
| 719 | If the high_dpi flag is false, or you're not running on a Retina display, |
| 720 | the values would be: |
| 721 | |
| 722 | sapp_width: 640 |
| 723 | sapp_height: 480 |
| 724 | sapp_dpi_scale: 1.0 |
| 725 | |
| 726 | If the window is moved from the Retina display to a low-dpi external display, |
| 727 | the values would change as follows: |
| 728 | |
| 729 | sapp_width: 1280 => 640 |
| 730 | sapp_height: 960 => 480 |
| 731 | sapp_dpi_scale: 2.0 => 1.0 |
| 732 | |
| 733 | Currently there is no event associated with a DPI change, but an |
| 734 | SAPP_EVENTTYPE_RESIZED will be sent as a side effect of the |
| 735 | framebuffer size changing. |
| 736 | |
| 737 | Per-monitor DPI is currently supported on macOS and Windows. |
| 738 | |
| 739 | APPLICATION QUIT |
| 740 | ================ |
| 741 | Without special quit handling, a sokol_app.h application will quit |
| 742 | 'gracefully' when the user clicks the window close-button unless a |
| 743 | platform's application model prevents this (e.g. on web or mobile). |
| 744 | 'Graceful exit' means that the application-provided cleanup callback will |
| 745 | be called before the application quits. |
| 746 | |
| 747 | On native desktop platforms sokol_app.h provides more control over the |
| 748 | application-quit-process. It's possible to initiate a 'programmatic quit' |
| 749 | from the application code, and a quit initiated by the application user can |
| 750 | be intercepted (for instance to show a custom dialog box). |
| 751 | |
| 752 | This 'programmatic quit protocol' is implemented through 3 functions |
| 753 | and 1 event: |
| 754 | |
| 755 | - sapp_quit(): This function simply quits the application without |
| 756 | giving the user a chance to intervene. Usually this might |
| 757 | be called when the user clicks the 'Ok' button in a 'Really Quit?' |
| 758 | dialog box |
| 759 | - sapp_request_quit(): Calling sapp_request_quit() will send the |
| 760 | event SAPP_EVENTTYPE_QUIT_REQUESTED to the applications event handler |
| 761 | callback, giving the user code a chance to intervene and cancel the |
| 762 | pending quit process (for instance to show a 'Really Quit?' dialog |
| 763 | box). If the event handler callback does nothing, the application |
| 764 | will be quit as usual. To prevent this, call the function |
| 765 | sapp_cancel_quit() from inside the event handler. |
| 766 | - sapp_cancel_quit(): Cancels a pending quit request, either initiated |
| 767 | by the user clicking the window close button, or programmatically |
| 768 | by calling sapp_request_quit(). The only place where calling this |
| 769 | function makes sense is from inside the event handler callback when |
| 770 | the SAPP_EVENTTYPE_QUIT_REQUESTED event has been received. |
| 771 | - SAPP_EVENTTYPE_QUIT_REQUESTED: this event is sent when the user |
| 772 | clicks the window's close button or application code calls the |
| 773 | sapp_request_quit() function. The event handler callback code can handle |
| 774 | this event by calling sapp_cancel_quit() to cancel the quit. |
| 775 | If the event is ignored, the application will quit as usual. |
| 776 | |
| 777 | On the web platform, the quit behaviour differs from native platforms, |
| 778 | because of web-specific restrictions: |
| 779 | |
| 780 | A `programmatic quit` initiated by calling sapp_quit() or |
| 781 | sapp_request_quit() will work as described above: the cleanup callback is |
| 782 | called, platform-specific cleanup is performed (on the web |
| 783 | this means that JS event handlers are unregistered), and then |
| 784 | the request-animation-loop will be exited. However that's all. The |
| 785 | web page itself will continue to exist (e.g. it's not possible to |
| 786 | programmatically close the browser tab). |
| 787 | |
| 788 | On the web it's also not possible to run custom code when the user |
| 789 | closes a browser tab, so it's not possible to prevent this with a |
| 790 | fancy custom dialog box. |
| 791 | |
| 792 | Instead the standard "Leave Site?" dialog box can be activated (or |
| 793 | deactivated) with the following function: |
| 794 | |
| 795 | sapp_html5_ask_leave_site(bool ask); |
| 796 | |
| 797 | The initial state of the associated internal flag can be provided |
| 798 | at startup via sapp_desc.html5_ask_leave_site. |
| 799 | |
| 800 | This feature should only be used sparingly in critical situations - for |
| 801 | instance when the user would loose data - since popping up modal dialog |
| 802 | boxes is considered quite rude in the web world. Note that there's no way |
| 803 | to customize the content of this dialog box or run any code as a result |
| 804 | of the user's decision. Also note that the user must have interacted with |
| 805 | the site before the dialog box will appear. These are all security measures |
| 806 | to prevent fishing. |
| 807 | |
| 808 | The Dear ImGui HighDPI sample contains example code of how to |
| 809 | implement a 'Really Quit?' dialog box with Dear ImGui (native desktop |
| 810 | platforms only), and for showing the hardwired "Leave Site?" dialog box |
| 811 | when running on the web platform: |
| 812 | |
| 813 | https://floooh.github.io/sokol-html5/wasm/imgui-highdpi-sapp.html |
| 814 | |
| 815 | FULLSCREEN |
| 816 | ========== |
| 817 | If the sapp_desc.fullscreen flag is true, sokol-app will try to create |
| 818 | a fullscreen window on platforms with a 'proper' window system |
| 819 | (mobile devices will always use fullscreen). The implementation details |
| 820 | depend on the target platform, in general sokol-app will use a |
| 821 | 'soft approach' which doesn't interfere too much with the platform's |
| 822 | window system (for instance borderless fullscreen window instead of |
| 823 | a 'real' fullscreen mode). Such details might change over time |
| 824 | as sokol-app is adapted for different needs. |
| 825 | |
| 826 | The most important effect of fullscreen mode to keep in mind is that |
| 827 | the requested canvas width and height will be ignored for the initial |
| 828 | window size, calling sapp_width() and sapp_height() will instead return |
| 829 | the resolution of the fullscreen canvas (however the provided size |
| 830 | might still be used for the non-fullscreen window, in case the user can |
| 831 | switch back from fullscreen- to windowed-mode). |
| 832 | |
| 833 | To toggle fullscreen mode programmatically, call sapp_toggle_fullscreen(). |
| 834 | |
| 835 | To check if the application window is currently in fullscreen mode, |
| 836 | call sapp_is_fullscreen(). |
| 837 | |
| 838 | WINDOW ICON SUPPORT |
| 839 | =================== |
| 840 | Some sokol_app.h backends allow to change the window icon programmatically: |
| 841 | |
| 842 | - on Win32: the small icon in the window's title bar, and the |
| 843 | bigger icon in the task bar |
| 844 | - on Linux: highly dependent on the used window manager, but usually |
| 845 | the window's title bar icon and/or the task bar icon |
| 846 | - on HTML5: the favicon shown in the page's browser tab |
| 847 | |
| 848 | NOTE that it is not possible to set the actual application icon which is |
| 849 | displayed by the operating system on the desktop or 'home screen'. Those |
| 850 | icons must be provided 'traditionally' through operating-system-specific |
| 851 | resources which are associated with the application (sokol_app.h might |
| 852 | later support setting the window icon from platform specific resource data |
| 853 | though). |
| 854 | |
| 855 | There are two ways to set the window icon: |
| 856 | |
| 857 | - at application start in the sokol_main() function by initializing |
| 858 | the sapp_desc.icon nested struct |
| 859 | - or later by calling the function sapp_set_icon() |
| 860 | |
| 861 | As a convenient shortcut, sokol_app.h comes with a builtin default-icon |
| 862 | (a rainbow-colored 'S', which at least looks a bit better than the Windows |
| 863 | default icon for applications), which can be activated like this: |
| 864 | |
| 865 | At startup in sokol_main(): |
| 866 | |
| 867 | sapp_desc sokol_main(...) { |
| 868 | return (sapp_desc){ |
| 869 | ... |
| 870 | icon.sokol_default = true |
| 871 | }; |
| 872 | } |
| 873 | |
| 874 | Or later by calling: |
| 875 | |
| 876 | sapp_set_icon(&(sapp_icon_desc){ .sokol_default = true }); |
| 877 | |
| 878 | NOTE that a completely zero-initialized sapp_icon_desc struct will not |
| 879 | update the window icon in any way. This is an 'escape hatch' so that you |
| 880 | can handle the window icon update yourself (or if you do this already, |
| 881 | sokol_app.h won't get in your way, in this case just leave the |
| 882 | sapp_desc.icon struct zero-initialized). |
| 883 | |
| 884 | Providing your own icon images works exactly like in GLFW (down to the |
| 885 | data format): |
| 886 | |
| 887 | You provide one or more 'candidate images' in different sizes, and the |
| 888 | sokol_app.h platform backends pick the best match for the specific backend |
| 889 | and icon type. |
| 890 | |
| 891 | For each candidate image, you need to provide: |
| 892 | |
| 893 | - the width in pixels |
| 894 | - the height in pixels |
| 895 | - and the actual pixel data in RGBA8 pixel format (e.g. 0xFFCC8844 |
| 896 | on a little-endian CPU means: alpha=0xFF, blue=0xCC, green=0x88, red=0x44) |
| 897 | |
| 898 | For instance, if you have 3 candidate images (small, medium, big) of |
| 899 | sizes 16x16, 32x32 and 64x64 the corresponding sapp_icon_desc struct is setup |
| 900 | like this: |
| 901 | |
| 902 | // the actual pixel data (RGBA8, origin top-left) |
| 903 | const uint32_t small[16][16] = { ... }; |
| 904 | const uint32_t medium[32][32] = { ... }; |
| 905 | const uint32_t big[64][64] = { ... }; |
| 906 | |
| 907 | const sapp_icon_desc icon_desc = { |
| 908 | .images = { |
| 909 | { .width = 16, .height = 16, .pixels = SAPP_RANGE(small) }, |
| 910 | { .width = 32, .height = 32, .pixels = SAPP_RANGE(medium) }, |
| 911 | // ...or without the SAPP_RANGE helper macro: |
| 912 | { .width = 64, .height = 64, .pixels = { .ptr=big, .size=sizeof(big) } } |
| 913 | } |
| 914 | }; |
| 915 | |
| 916 | An sapp_icon_desc struct initialized like this can then either be applied |
| 917 | at application start in sokol_main: |
| 918 | |
| 919 | sapp_desc sokol_main(...) { |
| 920 | return (sapp_desc){ |
| 921 | ... |
| 922 | icon = icon_desc |
| 923 | }; |
| 924 | } |
| 925 | |
| 926 | ...or later by calling sapp_set_icon(): |
| 927 | |
| 928 | sapp_set_icon(&icon_desc); |
| 929 | |
| 930 | Some window icon caveats: |
| 931 | |
| 932 | - once the window icon has been updated, there's no way to go back to |
| 933 | the platform's default icon, this is because some platforms (Linux |
| 934 | and HTML5) don't switch the icon visual back to the default even if |
| 935 | the custom icon is deleted or removed |
| 936 | - on HTML5, if the sokol_app.h icon doesn't show up in the browser |
| 937 | tab, check that there's no traditional favicon 'link' element |
| 938 | is defined in the page's index.html, sokol_app.h will only |
| 939 | append a new favicon link element, but not delete any manually |
| 940 | defined favicon in the page |
| 941 | |
| 942 | For an example and test of the window icon feature, check out the |
| 943 | 'icon-sapp' sample on the sokol-samples git repository. |
| 944 | |
| 945 | ONSCREEN KEYBOARD |
| 946 | ================= |
| 947 | On some platforms which don't provide a physical keyboard, sokol-app |
| 948 | can display the platform's integrated onscreen keyboard for text |
| 949 | input. To request that the onscreen keyboard is shown, call |
| 950 | |
| 951 | sapp_show_keyboard(true); |
| 952 | |
| 953 | Likewise, to hide the keyboard call: |
| 954 | |
| 955 | sapp_show_keyboard(false); |
| 956 | |
| 957 | Note that onscreen keyboard functionality is no longer supported |
| 958 | on the browser platform (the previous hacks and workarounds to make browser |
| 959 | keyboards work for on web applications that don't use HTML UIs |
| 960 | never really worked across browsers). |
| 961 | |
| 962 | INPUT EVENT BUBBLING ON THE WEB PLATFORM |
| 963 | ======================================== |
| 964 | By default, input event bubbling on the web platform is configured in |
| 965 | a way that makes the most sense for 'full-canvas' apps that cover the |
| 966 | entire browser client window area: |
| 967 | |
| 968 | - mouse, touch and wheel events do not bubble up, this prevents various |
| 969 | ugly side events, like: |
| 970 | - HTML text overlays being selected on double- or triple-click into |
| 971 | the canvas |
| 972 | - 'scroll bumping' even when the canvas covers the entire client area |
| 973 | - key_up/down events for 'character keys' *do* bubble up (otherwise |
| 974 | the browser will not generate UNICODE character events) |
| 975 | - all other key events *do not* bubble up by default (this prevents side effects |
| 976 | like F1 opening help, or F7 starting 'caret browsing') |
| 977 | - character events do no bubble up (although I haven't noticed any side effects |
| 978 | otherwise) |
| 979 | |
| 980 | Event bubbling can be enabled for input event categories during initialization |
| 981 | in the sapp_desc struct: |
| 982 | |
| 983 | sapp_desc sokol_main(int argc, char* argv[]) { |
| 984 | return (sapp_desc){ |
| 985 | //... |
| 986 | .html5_bubble_mouse_events = true, |
| 987 | .html5_bubble_touch_events = true, |
| 988 | .html5_bubble_wheel_events = true, |
| 989 | .html5_bubble_key_events = true, |
| 990 | .html5_bubble_char_events = true, |
| 991 | }; |
| 992 | } |
| 993 | |
| 994 | This basically opens the floodgates lets *all* input events bubble up to the browser. |
| 995 | To prevent individual events from bubbling, call sapp_consume_event() from within |
| 996 | the sokol_app.h event callback. |
| 997 | |
| 998 | OPTIONAL: DON'T HIJACK main() (#define SOKOL_NO_ENTRY) |
| 999 | ====================================================== |
| 1000 | NOTE: SOKOL_NO_ENTRY and sapp_run() is currently not supported on Android. |
| 1001 | |
| 1002 | In its default configuration, sokol_app.h "hijacks" the platform's |
| 1003 | standard main() function. This was done because different platforms |
| 1004 | have different entry point conventions which are not compatible with |
| 1005 | C's main() (for instance WinMain on Windows has completely different |
| 1006 | arguments). However, this "main hijacking" posed a problem for |
| 1007 | usage scenarios like integrating sokol_app.h with other languages than |
| 1008 | C or C++, so an alternative SOKOL_NO_ENTRY mode has been added |
| 1009 | in which the user code provides the platform's main function: |
| 1010 | |
| 1011 | - define SOKOL_NO_ENTRY before including the sokol_app.h implementation |
| 1012 | - do *not* provide a sokol_main() function |
| 1013 | - instead provide the standard main() function of the platform |
| 1014 | - from the main function, call the function ```sapp_run()``` which |
| 1015 | takes a pointer to an ```sapp_desc``` structure. |
| 1016 | - from here on```sapp_run()``` takes over control and calls the provided |
| 1017 | init-, frame-, event- and cleanup-callbacks just like in the default model. |
| 1018 | |
| 1019 | sapp_run() behaves differently across platforms: |
| 1020 | |
| 1021 | - on some platforms, sapp_run() will return when the application quits |
| 1022 | - on other platforms, sapp_run() will never return, even when the |
| 1023 | application quits (the operating system is free to simply terminate |
| 1024 | the application at any time) |
| 1025 | - on Emscripten specifically, sapp_run() will return immediately while |
| 1026 | the frame callback keeps being called |
| 1027 | |
| 1028 | This different behaviour of sapp_run() essentially means that there shouldn't |
| 1029 | be any code *after* sapp_run(), because that may either never be called, or in |
| 1030 | case of Emscripten will be called at an unexpected time (at application start). |
| 1031 | |
| 1032 | An application also should not depend on the cleanup-callback being called |
| 1033 | when cross-platform compatibility is required. |
| 1034 | |
| 1035 | Since sapp_run() returns immediately on Emscripten you shouldn't activate |
| 1036 | the 'EXIT_RUNTIME' linker option (this is disabled by default when compiling |
| 1037 | for the browser target), since the C/C++ exit runtime would be called immediately at |
| 1038 | application start, causing any global objects to be destroyed and global |
| 1039 | variables to be zeroed. |
| 1040 | |
| 1041 | WINDOWS CONSOLE OUTPUT |
| 1042 | ====================== |
| 1043 | On Windows, regular windowed applications don't show any stdout/stderr text |
| 1044 | output, which can be a bit of a hassle for printf() debugging or generally |
| 1045 | logging text to the console. Also, console output by default uses a local |
| 1046 | codepage setting and thus international UTF-8 encoded text is printed |
| 1047 | as garbage. |
| 1048 | |
| 1049 | To help with these issues, sokol_app.h can be configured at startup |
| 1050 | via the following Windows-specific sapp_desc flags: |
| 1051 | |
| 1052 | sapp_desc.win32_console_utf8 (default: false) |
| 1053 | When set to true, the output console codepage will be switched |
| 1054 | to UTF-8 (and restored to the original codepage on exit) |
| 1055 | |
| 1056 | sapp_desc.win32_console_attach (default: false) |
| 1057 | When set to true, stdout and stderr will be attached to the |
| 1058 | console of the parent process (if the parent process actually |
| 1059 | has a console). This means that if the application was started |
| 1060 | in a command line window, stdout and stderr output will be printed |
| 1061 | to the terminal, just like a regular command line program. But if |
| 1062 | the application is started via double-click, it will behave like |
| 1063 | a regular UI application, and stdout/stderr will not be visible. |
| 1064 | |
| 1065 | sapp_desc.win32_console_create (default: false) |
| 1066 | When set to true, a new console window will be created and |
| 1067 | stdout/stderr will be redirected to that console window. It |
| 1068 | doesn't matter if the application is started from the command |
| 1069 | line or via double-click. |
| 1070 | |
| 1071 | MEMORY ALLOCATION OVERRIDE |
| 1072 | ========================== |
| 1073 | You can override the memory allocation functions at initialization time |
| 1074 | like this: |
| 1075 | |
| 1076 | void* my_alloc(size_t size, void* user_data) { |
| 1077 | return malloc(size); |
| 1078 | } |
| 1079 | |
| 1080 | void my_free(void* ptr, void* user_data) { |
| 1081 | free(ptr); |
| 1082 | } |
| 1083 | |
| 1084 | sapp_desc sokol_main(int argc, char* argv[]) { |
| 1085 | return (sapp_desc){ |
| 1086 | // ... |
| 1087 | .allocator = { |
| 1088 | .alloc_fn = my_alloc, |
| 1089 | .free_fn = my_free, |
| 1090 | .user_data = ..., |
| 1091 | } |
| 1092 | }; |
| 1093 | } |
| 1094 | |
| 1095 | If no overrides are provided, malloc and free will be used. |
| 1096 | |
| 1097 | This only affects memory allocation calls done by sokol_app.h |
| 1098 | itself though, not any allocations in OS libraries. |
| 1099 | |
| 1100 | |
| 1101 | ERROR REPORTING AND LOGGING |
| 1102 | =========================== |
| 1103 | To get any logging information at all you need to provide a logging callback in the setup call |
| 1104 | the easiest way is to use sokol_log.h: |
| 1105 | |
| 1106 | #include "sokol_log.h" |
| 1107 | |
| 1108 | sapp_desc sokol_main(int argc, char* argv[]) { |
| 1109 | return (sapp_desc) { |
| 1110 | ... |
| 1111 | .logger.func = slog_func, |
| 1112 | }; |
| 1113 | } |
| 1114 | |
| 1115 | To override logging with your own callback, first write a logging function like this: |
| 1116 | |
| 1117 | void my_log(const char* tag, // e.g. 'sapp' |
| 1118 | uint32_t log_level, // 0=panic, 1=error, 2=warn, 3=info |
| 1119 | uint32_t log_item_id, // SAPP_LOGITEM_* |
| 1120 | const char* message_or_null, // a message string, may be nullptr in release mode |
| 1121 | uint32_t line_nr, // line number in sokol_app.h |
| 1122 | const char* filename_or_null, // source filename, may be nullptr in release mode |
| 1123 | void* user_data) |
| 1124 | { |
| 1125 | ... |
| 1126 | } |
| 1127 | |
| 1128 | ...and then setup sokol-app like this: |
| 1129 | |
| 1130 | sapp_desc sokol_main(int argc, char* argv[]) { |
| 1131 | return (sapp_desc) { |
| 1132 | ... |
| 1133 | .logger = { |
| 1134 | .func = my_log, |
| 1135 | .user_data = my_user_data, |
| 1136 | } |
| 1137 | }; |
| 1138 | } |
| 1139 | |
| 1140 | The provided logging function must be reentrant (e.g. be callable from |
| 1141 | different threads). |
| 1142 | |
| 1143 | If you don't want to provide your own custom logger it is highly recommended to use |
| 1144 | the standard logger in sokol_log.h instead, otherwise you won't see any warnings or |
| 1145 | errors. |
| 1146 | |
| 1147 | |
| 1148 | TEMP NOTE DUMP |
| 1149 | ============== |
| 1150 | - sapp_desc needs a bool whether to initialize depth-stencil surface |
| 1151 | - the Android implementation calls cleanup_cb() and destroys the egl context in onDestroy |
| 1152 | at the latest but should do it earlier, in onStop, as an app is "killable" after onStop |
| 1153 | on Android Honeycomb and later (it can't be done at the moment as the app may be started |
| 1154 | again after onStop and the sokol lifecycle does not yet handle context teardown/bringup) |
| 1155 | |
| 1156 | |
| 1157 | LICENSE |
| 1158 | ======= |
| 1159 | zlib/libpng license |
| 1160 | |
| 1161 | Copyright (c) 2018 Andre Weissflog |
| 1162 | |
| 1163 | This software is provided 'as-is', without any express or implied warranty. |
| 1164 | In no event will the authors be held liable for any damages arising from the |
| 1165 | use of this software. |
| 1166 | |
| 1167 | Permission is granted to anyone to use this software for any purpose, |
| 1168 | including commercial applications, and to alter it and redistribute it |
| 1169 | freely, subject to the following restrictions: |
| 1170 | |
| 1171 | 1. The origin of this software must not be misrepresented; you must not |
| 1172 | claim that you wrote the original software. If you use this software in a |
| 1173 | product, an acknowledgment in the product documentation would be |
| 1174 | appreciated but is not required. |
| 1175 | |
| 1176 | 2. Altered source versions must be plainly marked as such, and must not |
| 1177 | be misrepresented as being the original software. |
| 1178 | |
| 1179 | 3. This notice may not be removed or altered from any source |
| 1180 | distribution. |
| 1181 | */ |
| 1182 | #define SOKOL_APP_INCLUDED (1) |
| 1183 | #include <stddef.h> // size_t |
| 1184 | #include <stdint.h> |
| 1185 | #include <stdbool.h> |
| 1186 | |
| 1187 | #if defined(SOKOL_API_DECL) && !defined(SOKOL_APP_API_DECL) |
| 1188 | #define SOKOL_APP_API_DECL SOKOL_API_DECL |
| 1189 | #endif |
| 1190 | #ifndef SOKOL_APP_API_DECL |
| 1191 | #if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_APP_IMPL) |
| 1192 | #define SOKOL_APP_API_DECL __declspec(dllexport) |
| 1193 | #elif defined(_WIN32) && defined(SOKOL_DLL) |
| 1194 | #define SOKOL_APP_API_DECL __declspec(dllimport) |
| 1195 | #else |
| 1196 | #define SOKOL_APP_API_DECL extern |
| 1197 | #endif |
| 1198 | #endif |
| 1199 | |
| 1200 | #ifdef __cplusplus |
| 1201 | extern "C" { |
| 1202 | #endif |
| 1203 | |
| 1204 | /* misc constants */ |
| 1205 | enum { |
| 1206 | SAPP_MAX_TOUCHPOINTS = 8, |
| 1207 | SAPP_MAX_MOUSEBUTTONS = 3, |
| 1208 | SAPP_MAX_KEYCODES = 512, |
| 1209 | SAPP_MAX_ICONIMAGES = 8, |
| 1210 | }; |
| 1211 | |
| 1212 | /* |
| 1213 | sapp_event_type |
| 1214 | |
| 1215 | The type of event that's passed to the event handler callback |
| 1216 | in the sapp_event.type field. These are not just "traditional" |
| 1217 | input events, but also notify the application about state changes |
| 1218 | or other user-invoked actions. |
| 1219 | */ |
| 1220 | typedef enum sapp_event_type { |
| 1221 | SAPP_EVENTTYPE_INVALID, |
| 1222 | SAPP_EVENTTYPE_KEY_DOWN, |
| 1223 | SAPP_EVENTTYPE_KEY_UP, |
| 1224 | SAPP_EVENTTYPE_CHAR, |
| 1225 | SAPP_EVENTTYPE_MOUSE_DOWN, |
| 1226 | SAPP_EVENTTYPE_MOUSE_UP, |
| 1227 | SAPP_EVENTTYPE_MOUSE_SCROLL, |
| 1228 | SAPP_EVENTTYPE_MOUSE_MOVE, |
| 1229 | SAPP_EVENTTYPE_MOUSE_ENTER, |
| 1230 | SAPP_EVENTTYPE_MOUSE_LEAVE, |
| 1231 | SAPP_EVENTTYPE_TOUCHES_BEGAN, |
| 1232 | SAPP_EVENTTYPE_TOUCHES_MOVED, |
| 1233 | SAPP_EVENTTYPE_TOUCHES_ENDED, |
| 1234 | SAPP_EVENTTYPE_TOUCHES_CANCELLED, |
| 1235 | SAPP_EVENTTYPE_RESIZED, |
| 1236 | SAPP_EVENTTYPE_ICONIFIED, |
| 1237 | SAPP_EVENTTYPE_RESTORED, |
| 1238 | SAPP_EVENTTYPE_FOCUSED, |
| 1239 | SAPP_EVENTTYPE_UNFOCUSED, |
| 1240 | SAPP_EVENTTYPE_SUSPENDED, |
| 1241 | SAPP_EVENTTYPE_RESUMED, |
| 1242 | SAPP_EVENTTYPE_QUIT_REQUESTED, |
| 1243 | SAPP_EVENTTYPE_CLIPBOARD_PASTED, |
| 1244 | SAPP_EVENTTYPE_FILES_DROPPED, |
| 1245 | _SAPP_EVENTTYPE_NUM, |
| 1246 | _SAPP_EVENTTYPE_FORCE_U32 = 0x7FFFFFFF |
| 1247 | } sapp_event_type; |
| 1248 | |
| 1249 | /* |
| 1250 | sapp_keycode |
| 1251 | |
| 1252 | The 'virtual keycode' of a KEY_DOWN or KEY_UP event in the |
| 1253 | struct field sapp_event.key_code. |
| 1254 | |
| 1255 | Note that the keycode values are identical with GLFW. |
| 1256 | */ |
| 1257 | typedef enum sapp_keycode { |
| 1258 | SAPP_KEYCODE_INVALID = 0, |
| 1259 | SAPP_KEYCODE_SPACE = 32, |
| 1260 | SAPP_KEYCODE_APOSTROPHE = 39, /* ' */ |
| 1261 | SAPP_KEYCODE_COMMA = 44, /* , */ |
| 1262 | SAPP_KEYCODE_MINUS = 45, /* - */ |
| 1263 | SAPP_KEYCODE_PERIOD = 46, /* . */ |
| 1264 | SAPP_KEYCODE_SLASH = 47, /* / */ |
| 1265 | SAPP_KEYCODE_0 = 48, |
| 1266 | SAPP_KEYCODE_1 = 49, |
| 1267 | SAPP_KEYCODE_2 = 50, |
| 1268 | SAPP_KEYCODE_3 = 51, |
| 1269 | SAPP_KEYCODE_4 = 52, |
| 1270 | SAPP_KEYCODE_5 = 53, |
| 1271 | SAPP_KEYCODE_6 = 54, |
| 1272 | SAPP_KEYCODE_7 = 55, |
| 1273 | SAPP_KEYCODE_8 = 56, |
| 1274 | SAPP_KEYCODE_9 = 57, |
| 1275 | SAPP_KEYCODE_SEMICOLON = 59, /* ; */ |
| 1276 | SAPP_KEYCODE_EQUAL = 61, /* = */ |
| 1277 | SAPP_KEYCODE_A = 65, |
| 1278 | SAPP_KEYCODE_B = 66, |
| 1279 | SAPP_KEYCODE_C = 67, |
| 1280 | SAPP_KEYCODE_D = 68, |
| 1281 | SAPP_KEYCODE_E = 69, |
| 1282 | SAPP_KEYCODE_F = 70, |
| 1283 | SAPP_KEYCODE_G = 71, |
| 1284 | SAPP_KEYCODE_H = 72, |
| 1285 | SAPP_KEYCODE_I = 73, |
| 1286 | SAPP_KEYCODE_J = 74, |
| 1287 | SAPP_KEYCODE_K = 75, |
| 1288 | SAPP_KEYCODE_L = 76, |
| 1289 | SAPP_KEYCODE_M = 77, |
| 1290 | SAPP_KEYCODE_N = 78, |
| 1291 | SAPP_KEYCODE_O = 79, |
| 1292 | SAPP_KEYCODE_P = 80, |
| 1293 | SAPP_KEYCODE_Q = 81, |
| 1294 | SAPP_KEYCODE_R = 82, |
| 1295 | SAPP_KEYCODE_S = 83, |
| 1296 | SAPP_KEYCODE_T = 84, |
| 1297 | SAPP_KEYCODE_U = 85, |
| 1298 | SAPP_KEYCODE_V = 86, |
| 1299 | SAPP_KEYCODE_W = 87, |
| 1300 | SAPP_KEYCODE_X = 88, |
| 1301 | SAPP_KEYCODE_Y = 89, |
| 1302 | SAPP_KEYCODE_Z = 90, |
| 1303 | SAPP_KEYCODE_LEFT_BRACKET = 91, /* [ */ |
| 1304 | SAPP_KEYCODE_BACKSLASH = 92, /* \ */ |
| 1305 | SAPP_KEYCODE_RIGHT_BRACKET = 93, /* ] */ |
| 1306 | SAPP_KEYCODE_GRAVE_ACCENT = 96, /* ` */ |
| 1307 | SAPP_KEYCODE_WORLD_1 = 161, /* non-US #1 */ |
| 1308 | SAPP_KEYCODE_WORLD_2 = 162, /* non-US #2 */ |
| 1309 | SAPP_KEYCODE_ESCAPE = 256, |
| 1310 | SAPP_KEYCODE_ENTER = 257, |
| 1311 | SAPP_KEYCODE_TAB = 258, |
| 1312 | SAPP_KEYCODE_BACKSPACE = 259, |
| 1313 | SAPP_KEYCODE_INSERT = 260, |
| 1314 | SAPP_KEYCODE_DELETE = 261, |
| 1315 | SAPP_KEYCODE_RIGHT = 262, |
| 1316 | SAPP_KEYCODE_LEFT = 263, |
| 1317 | SAPP_KEYCODE_DOWN = 264, |
| 1318 | SAPP_KEYCODE_UP = 265, |
| 1319 | SAPP_KEYCODE_PAGE_UP = 266, |
| 1320 | SAPP_KEYCODE_PAGE_DOWN = 267, |
| 1321 | SAPP_KEYCODE_HOME = 268, |
| 1322 | SAPP_KEYCODE_END = 269, |
| 1323 | SAPP_KEYCODE_CAPS_LOCK = 280, |
| 1324 | SAPP_KEYCODE_SCROLL_LOCK = 281, |
| 1325 | SAPP_KEYCODE_NUM_LOCK = 282, |
| 1326 | SAPP_KEYCODE_PRINT_SCREEN = 283, |
| 1327 | SAPP_KEYCODE_PAUSE = 284, |
| 1328 | SAPP_KEYCODE_F1 = 290, |
| 1329 | SAPP_KEYCODE_F2 = 291, |
| 1330 | SAPP_KEYCODE_F3 = 292, |
| 1331 | SAPP_KEYCODE_F4 = 293, |
| 1332 | SAPP_KEYCODE_F5 = 294, |
| 1333 | SAPP_KEYCODE_F6 = 295, |
| 1334 | SAPP_KEYCODE_F7 = 296, |
| 1335 | SAPP_KEYCODE_F8 = 297, |
| 1336 | SAPP_KEYCODE_F9 = 298, |
| 1337 | SAPP_KEYCODE_F10 = 299, |
| 1338 | SAPP_KEYCODE_F11 = 300, |
| 1339 | SAPP_KEYCODE_F12 = 301, |
| 1340 | SAPP_KEYCODE_F13 = 302, |
| 1341 | SAPP_KEYCODE_F14 = 303, |
| 1342 | SAPP_KEYCODE_F15 = 304, |
| 1343 | SAPP_KEYCODE_F16 = 305, |
| 1344 | SAPP_KEYCODE_F17 = 306, |
| 1345 | SAPP_KEYCODE_F18 = 307, |
| 1346 | SAPP_KEYCODE_F19 = 308, |
| 1347 | SAPP_KEYCODE_F20 = 309, |
| 1348 | SAPP_KEYCODE_F21 = 310, |
| 1349 | SAPP_KEYCODE_F22 = 311, |
| 1350 | SAPP_KEYCODE_F23 = 312, |
| 1351 | SAPP_KEYCODE_F24 = 313, |
| 1352 | SAPP_KEYCODE_F25 = 314, |
| 1353 | SAPP_KEYCODE_KP_0 = 320, |
| 1354 | SAPP_KEYCODE_KP_1 = 321, |
| 1355 | SAPP_KEYCODE_KP_2 = 322, |
| 1356 | SAPP_KEYCODE_KP_3 = 323, |
| 1357 | SAPP_KEYCODE_KP_4 = 324, |
| 1358 | SAPP_KEYCODE_KP_5 = 325, |
| 1359 | SAPP_KEYCODE_KP_6 = 326, |
| 1360 | SAPP_KEYCODE_KP_7 = 327, |
| 1361 | SAPP_KEYCODE_KP_8 = 328, |
| 1362 | SAPP_KEYCODE_KP_9 = 329, |
| 1363 | SAPP_KEYCODE_KP_DECIMAL = 330, |
| 1364 | SAPP_KEYCODE_KP_DIVIDE = 331, |
| 1365 | SAPP_KEYCODE_KP_MULTIPLY = 332, |
| 1366 | SAPP_KEYCODE_KP_SUBTRACT = 333, |
| 1367 | SAPP_KEYCODE_KP_ADD = 334, |
| 1368 | SAPP_KEYCODE_KP_ENTER = 335, |
| 1369 | SAPP_KEYCODE_KP_EQUAL = 336, |
| 1370 | SAPP_KEYCODE_LEFT_SHIFT = 340, |
| 1371 | SAPP_KEYCODE_LEFT_CONTROL = 341, |
| 1372 | SAPP_KEYCODE_LEFT_ALT = 342, |
| 1373 | SAPP_KEYCODE_LEFT_SUPER = 343, |
| 1374 | SAPP_KEYCODE_RIGHT_SHIFT = 344, |
| 1375 | SAPP_KEYCODE_RIGHT_CONTROL = 345, |
| 1376 | SAPP_KEYCODE_RIGHT_ALT = 346, |
| 1377 | SAPP_KEYCODE_RIGHT_SUPER = 347, |
| 1378 | SAPP_KEYCODE_MENU = 348, |
| 1379 | } sapp_keycode; |
| 1380 | |
| 1381 | /* |
| 1382 | Android specific 'tool type' enum for touch events. This lets the |
| 1383 | application check what type of input device was used for |
| 1384 | touch events. |
| 1385 | |
| 1386 | NOTE: the values must remain in sync with the corresponding |
| 1387 | Android SDK type, so don't change those. |
| 1388 | |
| 1389 | See https://developer.android.com/reference/android/view/MotionEvent#TOOL_TYPE_UNKNOWN |
| 1390 | */ |
| 1391 | typedef enum sapp_android_tooltype { |
| 1392 | SAPP_ANDROIDTOOLTYPE_UNKNOWN = 0, // TOOL_TYPE_UNKNOWN |
| 1393 | SAPP_ANDROIDTOOLTYPE_FINGER = 1, // TOOL_TYPE_FINGER |
| 1394 | SAPP_ANDROIDTOOLTYPE_STYLUS = 2, // TOOL_TYPE_STYLUS |
| 1395 | SAPP_ANDROIDTOOLTYPE_MOUSE = 3, // TOOL_TYPE_MOUSE |
| 1396 | } sapp_android_tooltype; |
| 1397 | |
| 1398 | /* |
| 1399 | sapp_touchpoint |
| 1400 | |
| 1401 | Describes a single touchpoint in a multitouch event (TOUCHES_BEGAN, |
| 1402 | TOUCHES_MOVED, TOUCHES_ENDED). |
| 1403 | |
| 1404 | Touch points are stored in the nested array sapp_event.touches[], |
| 1405 | and the number of touches is stored in sapp_event.num_touches. |
| 1406 | */ |
| 1407 | typedef struct sapp_touchpoint { |
| 1408 | uintptr_t identifier; |
| 1409 | float pos_x; |
| 1410 | float pos_y; |
| 1411 | sapp_android_tooltype android_tooltype; // only valid on Android |
| 1412 | bool changed; |
| 1413 | } sapp_touchpoint; |
| 1414 | |
| 1415 | /* |
| 1416 | sapp_mousebutton |
| 1417 | |
| 1418 | The currently pressed mouse button in the events MOUSE_DOWN |
| 1419 | and MOUSE_UP, stored in the struct field sapp_event.mouse_button. |
| 1420 | */ |
| 1421 | typedef enum sapp_mousebutton { |
| 1422 | SAPP_MOUSEBUTTON_LEFT = 0x0, |
| 1423 | SAPP_MOUSEBUTTON_RIGHT = 0x1, |
| 1424 | SAPP_MOUSEBUTTON_MIDDLE = 0x2, |
| 1425 | SAPP_MOUSEBUTTON_INVALID = 0x100, |
| 1426 | } sapp_mousebutton; |
| 1427 | |
| 1428 | /* |
| 1429 | These are currently pressed modifier keys (and mouse buttons) which are |
| 1430 | passed in the event struct field sapp_event.modifiers. |
| 1431 | */ |
| 1432 | enum { |
| 1433 | SAPP_MODIFIER_SHIFT = 0x1, // left or right shift key |
| 1434 | SAPP_MODIFIER_CTRL = 0x2, // left or right control key |
| 1435 | SAPP_MODIFIER_ALT = 0x4, // left or right alt key |
| 1436 | SAPP_MODIFIER_SUPER = 0x8, // left or right 'super' key |
| 1437 | SAPP_MODIFIER_LMB = 0x100, // left mouse button |
| 1438 | SAPP_MODIFIER_RMB = 0x200, // right mouse button |
| 1439 | SAPP_MODIFIER_MMB = 0x400, // middle mouse button |
| 1440 | }; |
| 1441 | |
| 1442 | /* |
| 1443 | sapp_event |
| 1444 | |
| 1445 | This is an all-in-one event struct passed to the event handler |
| 1446 | user callback function. Note that it depends on the event |
| 1447 | type what struct fields actually contain useful values, so you |
| 1448 | should first check the event type before reading other struct |
| 1449 | fields. |
| 1450 | */ |
| 1451 | typedef struct sapp_event { |
| 1452 | uint64_t frame_count; // current frame counter, always valid, useful for checking if two events were issued in the same frame |
| 1453 | sapp_event_type type; // the event type, always valid |
| 1454 | sapp_keycode key_code; // the virtual key code, only valid in KEY_UP, KEY_DOWN |
| 1455 | uint32_t char_code; // the UTF-32 character code, only valid in CHAR events |
| 1456 | bool key_repeat; // true if this is a key-repeat event, valid in KEY_UP, KEY_DOWN and CHAR |
| 1457 | uint32_t modifiers; // current modifier keys, valid in all key-, char- and mouse-events |
| 1458 | sapp_mousebutton mouse_button; // mouse button that was pressed or released, valid in MOUSE_DOWN, MOUSE_UP |
| 1459 | float mouse_x; // current horizontal mouse position in pixels, always valid except during mouse lock |
| 1460 | float mouse_y; // current vertical mouse position in pixels, always valid except during mouse lock |
| 1461 | float mouse_dx; // relative horizontal mouse movement since last frame, always valid |
| 1462 | float mouse_dy; // relative vertical mouse movement since last frame, always valid |
| 1463 | float scroll_x; // horizontal mouse wheel scroll distance, valid in MOUSE_SCROLL events |
| 1464 | float scroll_y; // vertical mouse wheel scroll distance, valid in MOUSE_SCROLL events |
| 1465 | int num_touches; // number of valid items in the touches[] array |
| 1466 | sapp_touchpoint touches[SAPP_MAX_TOUCHPOINTS]; // current touch points, valid in TOUCHES_BEGIN, TOUCHES_MOVED, TOUCHES_ENDED |
| 1467 | int window_width; // current window- and framebuffer sizes in pixels, always valid |
| 1468 | int window_height; |
| 1469 | int framebuffer_width; // = window_width * dpi_scale |
| 1470 | int framebuffer_height; // = window_height * dpi_scale |
| 1471 | } sapp_event; |
| 1472 | |
| 1473 | /* |
| 1474 | sg_range |
| 1475 | |
| 1476 | A general pointer/size-pair struct and constructor macros for passing binary blobs |
| 1477 | into sokol_app.h. |
| 1478 | */ |
| 1479 | typedef struct sapp_range { |
| 1480 | const void* ptr; |
| 1481 | size_t size; |
| 1482 | } sapp_range; |
| 1483 | // disabling this for every includer isn't great, but the warnings are also quite pointless |
| 1484 | #if defined(_MSC_VER) |
| 1485 | #pragma warning(disable:4221) /* /W4 only: nonstandard extension used: 'x': cannot be initialized using address of automatic variable 'y' */ |
| 1486 | #pragma warning(disable:4204) /* VS2015: nonstandard extension used: non-constant aggregate initializer */ |
| 1487 | #endif |
| 1488 | #if defined(__cplusplus) |
| 1489 | #define SAPP_RANGE(x) sapp_range{ &x, sizeof(x) } |
| 1490 | #else |
| 1491 | #define SAPP_RANGE(x) (sapp_range){ &x, sizeof(x) } |
| 1492 | #endif |
| 1493 | |
| 1494 | /* |
| 1495 | sapp_image_desc |
| 1496 | |
| 1497 | This is used to describe image data to sokol_app.h (at first, window |
| 1498 | icons, later maybe cursor images). |
| 1499 | |
| 1500 | Note that the actual image pixel format depends on the use case: |
| 1501 | |
| 1502 | - window icon pixels are RGBA8 |
| 1503 | */ |
| 1504 | typedef struct sapp_image_desc { |
| 1505 | int width; |
| 1506 | int height; |
| 1507 | sapp_range pixels; |
| 1508 | } sapp_image_desc; |
| 1509 | |
| 1510 | /* |
| 1511 | sapp_icon_desc |
| 1512 | |
| 1513 | An icon description structure for use in sapp_desc.icon and |
| 1514 | sapp_set_icon(). |
| 1515 | |
| 1516 | When setting a custom image, the application can provide a number of |
| 1517 | candidates differing in size, and sokol_app.h will pick the image(s) |
| 1518 | closest to the size expected by the platform's window system. |
| 1519 | |
| 1520 | To set sokol-app's default icon, set .sokol_default to true. |
| 1521 | |
| 1522 | Otherwise provide candidate images of different sizes in the |
| 1523 | images[] array. |
| 1524 | |
| 1525 | If both the sokol_default flag is set to true, any image candidates |
| 1526 | will be ignored and the sokol_app.h default icon will be set. |
| 1527 | */ |
| 1528 | typedef struct sapp_icon_desc { |
| 1529 | bool sokol_default; |
| 1530 | sapp_image_desc images[SAPP_MAX_ICONIMAGES]; |
| 1531 | } sapp_icon_desc; |
| 1532 | |
| 1533 | /* |
| 1534 | sapp_allocator |
| 1535 | |
| 1536 | Used in sapp_desc to provide custom memory-alloc and -free functions |
| 1537 | to sokol_app.h. If memory management should be overridden, both the |
| 1538 | alloc_fn and free_fn function must be provided (e.g. it's not valid to |
| 1539 | override one function but not the other). |
| 1540 | */ |
| 1541 | typedef struct sapp_allocator { |
| 1542 | void* (*alloc_fn)(size_t size, void* user_data); |
| 1543 | void (*free_fn)(void* ptr, void* user_data); |
| 1544 | void* user_data; |
| 1545 | } sapp_allocator; |
| 1546 | |
| 1547 | /* |
| 1548 | sapp_log_item |
| 1549 | |
| 1550 | Log items are defined via X-Macros and expanded to an enum |
| 1551 | 'sapp_log_item', and in debug mode to corresponding |
| 1552 | human readable error messages. |
| 1553 | */ |
| 1554 | #define _SAPP_LOG_ITEMS \ |
| 1555 | _SAPP_LOGITEM_XMACRO(OK, "Ok") \ |
| 1556 | _SAPP_LOGITEM_XMACRO(MALLOC_FAILED, "memory allocation failed") \ |
| 1557 | _SAPP_LOGITEM_XMACRO(MACOS_INVALID_NSOPENGL_PROFILE, "macos: invalid NSOpenGLProfile (valid choices are 1.0, 3.2 and 4.1)") \ |
| 1558 | _SAPP_LOGITEM_XMACRO(WIN32_LOAD_OPENGL32_DLL_FAILED, "failed loading opengl32.dll") \ |
| 1559 | _SAPP_LOGITEM_XMACRO(WIN32_CREATE_HELPER_WINDOW_FAILED, "failed to create helper window") \ |
| 1560 | _SAPP_LOGITEM_XMACRO(WIN32_HELPER_WINDOW_GETDC_FAILED, "failed to get helper window DC") \ |
| 1561 | _SAPP_LOGITEM_XMACRO(WIN32_DUMMY_CONTEXT_SET_PIXELFORMAT_FAILED, "failed to set pixel format for dummy GL context") \ |
| 1562 | _SAPP_LOGITEM_XMACRO(WIN32_CREATE_DUMMY_CONTEXT_FAILED, "failed to create dummy GL context") \ |
| 1563 | _SAPP_LOGITEM_XMACRO(WIN32_DUMMY_CONTEXT_MAKE_CURRENT_FAILED, "failed to make dummy GL context current") \ |
| 1564 | _SAPP_LOGITEM_XMACRO(WIN32_GET_PIXELFORMAT_ATTRIB_FAILED, "failed to get WGL pixel format attribute") \ |
| 1565 | _SAPP_LOGITEM_XMACRO(WIN32_WGL_FIND_PIXELFORMAT_FAILED, "failed to find matching WGL pixel format") \ |
| 1566 | _SAPP_LOGITEM_XMACRO(WIN32_WGL_DESCRIBE_PIXELFORMAT_FAILED, "failed to get pixel format descriptor") \ |
| 1567 | _SAPP_LOGITEM_XMACRO(WIN32_WGL_SET_PIXELFORMAT_FAILED, "failed to set selected pixel format") \ |
| 1568 | _SAPP_LOGITEM_XMACRO(WIN32_WGL_ARB_CREATE_CONTEXT_REQUIRED, "ARB_create_context required") \ |
| 1569 | _SAPP_LOGITEM_XMACRO(WIN32_WGL_ARB_CREATE_CONTEXT_PROFILE_REQUIRED, "ARB_create_context_profile required") \ |
| 1570 | _SAPP_LOGITEM_XMACRO(WIN32_WGL_OPENGL_3_2_NOT_SUPPORTED, "OpenGL 3.2 not supported by GL driver (ERROR_INVALID_VERSION_ARB)") \ |
| 1571 | _SAPP_LOGITEM_XMACRO(WIN32_WGL_OPENGL_PROFILE_NOT_SUPPORTED, "requested OpenGL profile not support by GL driver (ERROR_INVALID_PROFILE_ARB)") \ |
| 1572 | _SAPP_LOGITEM_XMACRO(WIN32_WGL_INCOMPATIBLE_DEVICE_CONTEXT, "CreateContextAttribsARB failed with ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB") \ |
| 1573 | _SAPP_LOGITEM_XMACRO(WIN32_WGL_CREATE_CONTEXT_ATTRIBS_FAILED_OTHER, "CreateContextAttribsARB failed for other reason") \ |
| 1574 | _SAPP_LOGITEM_XMACRO(WIN32_D3D11_CREATE_DEVICE_AND_SWAPCHAIN_WITH_DEBUG_FAILED, "D3D11CreateDeviceAndSwapChain() with D3D11_CREATE_DEVICE_DEBUG failed, retrying without debug flag.") \ |
| 1575 | _SAPP_LOGITEM_XMACRO(WIN32_D3D11_GET_IDXGIFACTORY_FAILED, "could not obtain IDXGIFactory object") \ |
| 1576 | _SAPP_LOGITEM_XMACRO(WIN32_D3D11_GET_IDXGIADAPTER_FAILED, "could not obtain IDXGIAdapter object") \ |
| 1577 | _SAPP_LOGITEM_XMACRO(WIN32_D3D11_QUERY_INTERFACE_IDXGIDEVICE1_FAILED, "could not obtain IDXGIDevice1 interface") \ |
| 1578 | _SAPP_LOGITEM_XMACRO(WIN32_REGISTER_RAW_INPUT_DEVICES_FAILED_MOUSE_LOCK, "RegisterRawInputDevices() failed (on mouse lock)") \ |
| 1579 | _SAPP_LOGITEM_XMACRO(WIN32_REGISTER_RAW_INPUT_DEVICES_FAILED_MOUSE_UNLOCK, "RegisterRawInputDevices() failed (on mouse unlock)") \ |
| 1580 | _SAPP_LOGITEM_XMACRO(WIN32_GET_RAW_INPUT_DATA_FAILED, "GetRawInputData() failed") \ |
| 1581 | _SAPP_LOGITEM_XMACRO(LINUX_GLX_LOAD_LIBGL_FAILED, "failed to load libGL") \ |
| 1582 | _SAPP_LOGITEM_XMACRO(LINUX_GLX_LOAD_ENTRY_POINTS_FAILED, "failed to load GLX entry points") \ |
| 1583 | _SAPP_LOGITEM_XMACRO(LINUX_GLX_EXTENSION_NOT_FOUND, "GLX extension not found") \ |
| 1584 | _SAPP_LOGITEM_XMACRO(LINUX_GLX_QUERY_VERSION_FAILED, "failed to query GLX version") \ |
| 1585 | _SAPP_LOGITEM_XMACRO(LINUX_GLX_VERSION_TOO_LOW, "GLX version too low (need at least 1.3)") \ |
| 1586 | _SAPP_LOGITEM_XMACRO(LINUX_GLX_NO_GLXFBCONFIGS, "glXGetFBConfigs() returned no configs") \ |
| 1587 | _SAPP_LOGITEM_XMACRO(LINUX_GLX_NO_SUITABLE_GLXFBCONFIG, "failed to find a suitable GLXFBConfig") \ |
| 1588 | _SAPP_LOGITEM_XMACRO(LINUX_GLX_GET_VISUAL_FROM_FBCONFIG_FAILED, "glXGetVisualFromFBConfig failed") \ |
| 1589 | _SAPP_LOGITEM_XMACRO(LINUX_GLX_REQUIRED_EXTENSIONS_MISSING, "GLX extensions ARB_create_context and ARB_create_context_profile missing") \ |
| 1590 | _SAPP_LOGITEM_XMACRO(LINUX_GLX_CREATE_CONTEXT_FAILED, "Failed to create GL context via glXCreateContextAttribsARB") \ |
| 1591 | _SAPP_LOGITEM_XMACRO(LINUX_GLX_CREATE_WINDOW_FAILED, "glXCreateWindow() failed") \ |
| 1592 | _SAPP_LOGITEM_XMACRO(LINUX_X11_CREATE_WINDOW_FAILED, "XCreateWindow() failed") \ |
| 1593 | _SAPP_LOGITEM_XMACRO(LINUX_EGL_BIND_OPENGL_API_FAILED, "eglBindAPI(EGL_OPENGL_API) failed") \ |
| 1594 | _SAPP_LOGITEM_XMACRO(LINUX_EGL_BIND_OPENGL_ES_API_FAILED, "eglBindAPI(EGL_OPENGL_ES_API) failed") \ |
| 1595 | _SAPP_LOGITEM_XMACRO(LINUX_EGL_GET_DISPLAY_FAILED, "eglGetDisplay() failed") \ |
| 1596 | _SAPP_LOGITEM_XMACRO(LINUX_EGL_INITIALIZE_FAILED, "eglInitialize() failed") \ |
| 1597 | _SAPP_LOGITEM_XMACRO(LINUX_EGL_NO_CONFIGS, "eglChooseConfig() returned no configs") \ |
| 1598 | _SAPP_LOGITEM_XMACRO(LINUX_EGL_NO_NATIVE_VISUAL, "eglGetConfigAttrib() for EGL_NATIVE_VISUAL_ID failed") \ |
| 1599 | _SAPP_LOGITEM_XMACRO(LINUX_EGL_GET_VISUAL_INFO_FAILED, "XGetVisualInfo() failed") \ |
| 1600 | _SAPP_LOGITEM_XMACRO(LINUX_EGL_CREATE_WINDOW_SURFACE_FAILED, "eglCreateWindowSurface() failed") \ |
| 1601 | _SAPP_LOGITEM_XMACRO(LINUX_EGL_CREATE_CONTEXT_FAILED, "eglCreateContext() failed") \ |
| 1602 | _SAPP_LOGITEM_XMACRO(LINUX_EGL_MAKE_CURRENT_FAILED, "eglMakeCurrent() failed") \ |
| 1603 | _SAPP_LOGITEM_XMACRO(LINUX_X11_OPEN_DISPLAY_FAILED, "XOpenDisplay() failed") \ |
| 1604 | _SAPP_LOGITEM_XMACRO(LINUX_X11_QUERY_SYSTEM_DPI_FAILED, "failed to query system dpi value, assuming default 96.0") \ |
| 1605 | _SAPP_LOGITEM_XMACRO(LINUX_X11_DROPPED_FILE_URI_WRONG_SCHEME, "dropped file URL doesn't start with 'file://'") \ |
| 1606 | _SAPP_LOGITEM_XMACRO(ANDROID_UNSUPPORTED_INPUT_EVENT_INPUT_CB, "unsupported input event encountered in _sapp_android_input_cb()") \ |
| 1607 | _SAPP_LOGITEM_XMACRO(ANDROID_UNSUPPORTED_INPUT_EVENT_MAIN_CB, "unsupported input event encountered in _sapp_android_main_cb()") \ |
| 1608 | _SAPP_LOGITEM_XMACRO(ANDROID_READ_MSG_FAILED, "failed to read message in _sapp_android_main_cb()") \ |
| 1609 | _SAPP_LOGITEM_XMACRO(ANDROID_WRITE_MSG_FAILED, "failed to write message in _sapp_android_msg") \ |
| 1610 | _SAPP_LOGITEM_XMACRO(ANDROID_MSG_CREATE, "MSG_CREATE") \ |
| 1611 | _SAPP_LOGITEM_XMACRO(ANDROID_MSG_RESUME, "MSG_RESUME") \ |
| 1612 | _SAPP_LOGITEM_XMACRO(ANDROID_MSG_PAUSE, "MSG_PAUSE") \ |
| 1613 | _SAPP_LOGITEM_XMACRO(ANDROID_MSG_FOCUS, "MSG_FOCUS") \ |
| 1614 | _SAPP_LOGITEM_XMACRO(ANDROID_MSG_NO_FOCUS, "MSG_NO_FOCUS") \ |
| 1615 | _SAPP_LOGITEM_XMACRO(ANDROID_MSG_SET_NATIVE_WINDOW, "MSG_SET_NATIVE_WINDOW") \ |
| 1616 | _SAPP_LOGITEM_XMACRO(ANDROID_MSG_SET_INPUT_QUEUE, "MSG_SET_INPUT_QUEUE") \ |
| 1617 | _SAPP_LOGITEM_XMACRO(ANDROID_MSG_DESTROY, "MSG_DESTROY") \ |
| 1618 | _SAPP_LOGITEM_XMACRO(ANDROID_UNKNOWN_MSG, "unknown msg type received") \ |
| 1619 | _SAPP_LOGITEM_XMACRO(ANDROID_LOOP_THREAD_STARTED, "loop thread started") \ |
| 1620 | _SAPP_LOGITEM_XMACRO(ANDROID_LOOP_THREAD_DONE, "loop thread done") \ |
| 1621 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONSTART, "NativeActivity onStart()") \ |
| 1622 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONRESUME, "NativeActivity onResume") \ |
| 1623 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONSAVEINSTANCESTATE, "NativeActivity onSaveInstanceState") \ |
| 1624 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONWINDOWFOCUSCHANGED, "NativeActivity onWindowFocusChanged") \ |
| 1625 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONPAUSE, "NativeActivity onPause") \ |
| 1626 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONSTOP, "NativeActivity onStop()") \ |
| 1627 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONNATIVEWINDOWCREATED, "NativeActivity onNativeWindowCreated") \ |
| 1628 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONNATIVEWINDOWDESTROYED, "NativeActivity onNativeWindowDestroyed") \ |
| 1629 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONINPUTQUEUECREATED, "NativeActivity onInputQueueCreated") \ |
| 1630 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONINPUTQUEUEDESTROYED, "NativeActivity onInputQueueDestroyed") \ |
| 1631 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONCONFIGURATIONCHANGED, "NativeActivity onConfigurationChanged") \ |
| 1632 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONLOWMEMORY, "NativeActivity onLowMemory") \ |
| 1633 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONDESTROY, "NativeActivity onDestroy") \ |
| 1634 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_DONE, "NativeActivity done") \ |
| 1635 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_ONCREATE, "NativeActivity onCreate") \ |
| 1636 | _SAPP_LOGITEM_XMACRO(ANDROID_CREATE_THREAD_PIPE_FAILED, "failed to create thread pipe") \ |
| 1637 | _SAPP_LOGITEM_XMACRO(ANDROID_NATIVE_ACTIVITY_CREATE_SUCCESS, "NativeActivity successfully created") \ |
| 1638 | _SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_SURFACE_FAILED, "wgpu: failed to create surface for swapchain") \ |
| 1639 | _SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_SWAPCHAIN_FAILED, "wgpu: failed to create swapchain object") \ |
| 1640 | _SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_DEPTH_STENCIL_TEXTURE_FAILED, "wgpu: failed to create depth-stencil texture for swapchain") \ |
| 1641 | _SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_DEPTH_STENCIL_VIEW_FAILED, "wgpu: failed to create view object for swapchain depth-stencil texture") \ |
| 1642 | _SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_MSAA_TEXTURE_FAILED, "wgpu: failed to create msaa texture for swapchain") \ |
| 1643 | _SAPP_LOGITEM_XMACRO(WGPU_SWAPCHAIN_CREATE_MSAA_VIEW_FAILED, "wgpu: failed to create view object for swapchain msaa texture") \ |
| 1644 | _SAPP_LOGITEM_XMACRO(WGPU_REQUEST_DEVICE_STATUS_ERROR, "wgpu: requesting device failed with status 'error'") \ |
| 1645 | _SAPP_LOGITEM_XMACRO(WGPU_REQUEST_DEVICE_STATUS_UNKNOWN, "wgpu: requesting device failed with status 'unknown'") \ |
| 1646 | _SAPP_LOGITEM_XMACRO(WGPU_REQUEST_ADAPTER_STATUS_UNAVAILABLE, "wgpu: requesting adapter failed with 'unavailable'") \ |
| 1647 | _SAPP_LOGITEM_XMACRO(WGPU_REQUEST_ADAPTER_STATUS_ERROR, "wgpu: requesting adapter failed with status 'error'") \ |
| 1648 | _SAPP_LOGITEM_XMACRO(WGPU_REQUEST_ADAPTER_STATUS_UNKNOWN, "wgpu: requesting adapter failed with status 'unknown'") \ |
| 1649 | _SAPP_LOGITEM_XMACRO(WGPU_CREATE_INSTANCE_FAILED, "wgpu: failed to create instance") \ |
| 1650 | _SAPP_LOGITEM_XMACRO(IMAGE_DATA_SIZE_MISMATCH, "image data size mismatch (must be width*height*4 bytes)") \ |
| 1651 | _SAPP_LOGITEM_XMACRO(DROPPED_FILE_PATH_TOO_LONG, "dropped file path too long (sapp_desc.max_dropped_filed_path_length)") \ |
| 1652 | _SAPP_LOGITEM_XMACRO(CLIPBOARD_STRING_TOO_BIG, "clipboard string didn't fit into clipboard buffer") \ |
| 1653 | |
| 1654 | #define _SAPP_LOGITEM_XMACRO(item,msg) SAPP_LOGITEM_##item, |
| 1655 | typedef enum sapp_log_item { |
| 1656 | _SAPP_LOG_ITEMS |
| 1657 | } sapp_log_item; |
| 1658 | #undef _SAPP_LOGITEM_XMACRO |
| 1659 | |
| 1660 | /* |
| 1661 | sapp_logger |
| 1662 | |
| 1663 | Used in sapp_desc to provide a logging function. Please be aware that |
| 1664 | without logging function, sokol-app will be completely silent, e.g. it will |
| 1665 | not report errors or warnings. For maximum error verbosity, compile in |
| 1666 | debug mode (e.g. NDEBUG *not* defined) and install a logger (for instance |
| 1667 | the standard logging function from sokol_log.h). |
| 1668 | */ |
| 1669 | typedef struct sapp_logger { |
| 1670 | void (*func)( |
| 1671 | const char* tag, // always "sapp" |
| 1672 | uint32_t log_level, // 0=panic, 1=error, 2=warning, 3=info |
| 1673 | uint32_t log_item_id, // SAPP_LOGITEM_* |
| 1674 | const char* message_or_null, // a message string, may be nullptr in release mode |
| 1675 | uint32_t line_nr, // line number in sokol_app.h |
| 1676 | const char* filename_or_null, // source filename, may be nullptr in release mode |
| 1677 | void* user_data); |
| 1678 | void* user_data; |
| 1679 | } sapp_logger; |
| 1680 | |
| 1681 | typedef struct sapp_desc { |
| 1682 | void (*init_cb)(void); // these are the user-provided callbacks without user data |
| 1683 | void (*frame_cb)(void); |
| 1684 | void (*cleanup_cb)(void); |
| 1685 | void (*event_cb)(const sapp_event*); |
| 1686 | |
| 1687 | void* user_data; // these are the user-provided callbacks with user data |
| 1688 | void (*init_userdata_cb)(void*); |
| 1689 | void (*frame_userdata_cb)(void*); |
| 1690 | void (*cleanup_userdata_cb)(void*); |
| 1691 | void (*event_userdata_cb)(const sapp_event*, void*); |
| 1692 | |
| 1693 | int width; // the preferred width of the window / canvas |
| 1694 | int height; // the preferred height of the window / canvas |
| 1695 | int sample_count; // MSAA sample count |
| 1696 | int swap_interval; // the preferred swap interval (ignored on some platforms) |
| 1697 | bool high_dpi; // whether the rendering canvas is full-resolution on HighDPI displays |
| 1698 | bool fullscreen; // whether the window should be created in fullscreen mode |
| 1699 | bool alpha; // whether the framebuffer should have an alpha channel (ignored on some platforms) |
| 1700 | const char* window_title; // the window title as UTF-8 encoded string |
| 1701 | bool enable_clipboard; // enable clipboard access, default is false |
| 1702 | int clipboard_size; // max size of clipboard content in bytes |
| 1703 | bool enable_dragndrop; // enable file dropping (drag'n'drop), default is false |
| 1704 | int max_dropped_files; // max number of dropped files to process (default: 1) |
| 1705 | int max_dropped_file_path_length; // max length in bytes of a dropped UTF-8 file path (default: 2048) |
| 1706 | sapp_icon_desc icon; // the initial window icon to set |
| 1707 | sapp_allocator allocator; // optional memory allocation overrides (default: malloc/free) |
| 1708 | sapp_logger logger; // logging callback override (default: NO LOGGING!) |
| 1709 | |
| 1710 | // backend-specific options |
| 1711 | int gl_major_version; // override GL major and minor version (the default GL version is 3.2) |
| 1712 | int gl_minor_version; |
| 1713 | bool win32_console_utf8; // if true, set the output console codepage to UTF-8 |
| 1714 | bool win32_console_create; // if true, attach stdout/stderr to a new console window |
| 1715 | bool win32_console_attach; // if true, attach stdout/stderr to parent process |
| 1716 | const char* html5_canvas_name; // the name (id) of the HTML5 canvas element, default is "canvas" |
| 1717 | bool html5_canvas_resize; // if true, the HTML5 canvas size is set to sapp_desc.width/height, otherwise canvas size is tracked |
| 1718 | bool html5_preserve_drawing_buffer; // HTML5 only: whether to preserve default framebuffer content between frames |
| 1719 | bool html5_premultiplied_alpha; // HTML5 only: whether the rendered pixels use premultiplied alpha convention |
| 1720 | bool html5_ask_leave_site; // initial state of the internal html5_ask_leave_site flag (see sapp_html5_ask_leave_site()) |
| 1721 | bool html5_bubble_mouse_events; // if true, mouse events will bubble up to the web page |
| 1722 | bool html5_bubble_touch_events; // same for touch events |
| 1723 | bool html5_bubble_wheel_events; // same for wheel events |
| 1724 | bool html5_bubble_key_events; // if true, bubble up *all* key events to browser, not just key events that represent characters |
| 1725 | bool html5_bubble_char_events; // if true, bubble up character events to browser |
| 1726 | bool html5_use_emsc_set_main_loop; // if true, use emscripten_set_main_loop() instead of emscripten_request_animation_frame_loop() |
| 1727 | bool html5_emsc_set_main_loop_simulate_infinite_loop; // this will be passed as the simulate_infinite_loop arg to emscripten_set_main_loop() |
| 1728 | bool ios_keyboard_resizes_canvas; // if true, showing the iOS keyboard shrinks the canvas |
| 1729 | } sapp_desc; |
| 1730 | |
| 1731 | /* HTML5 specific: request and response structs for |
| 1732 | asynchronously loading dropped-file content. |
| 1733 | */ |
| 1734 | typedef enum sapp_html5_fetch_error { |
| 1735 | SAPP_HTML5_FETCH_ERROR_NO_ERROR, |
| 1736 | SAPP_HTML5_FETCH_ERROR_BUFFER_TOO_SMALL, |
| 1737 | SAPP_HTML5_FETCH_ERROR_OTHER, |
| 1738 | } sapp_html5_fetch_error; |
| 1739 | |
| 1740 | typedef struct sapp_html5_fetch_response { |
| 1741 | bool succeeded; // true if the loading operation has succeeded |
| 1742 | sapp_html5_fetch_error error_code; |
| 1743 | int file_index; // index of the dropped file (0..sapp_get_num_dropped_filed()-1) |
| 1744 | sapp_range data; // pointer and size of the fetched data (data.ptr == buffer.ptr, data.size <= buffer.size) |
| 1745 | sapp_range buffer; // the user-provided buffer ptr/size pair (buffer.ptr == data.ptr, buffer.size >= data.size) |
| 1746 | void* user_data; // user-provided user data pointer |
| 1747 | } sapp_html5_fetch_response; |
| 1748 | |
| 1749 | typedef struct sapp_html5_fetch_request { |
| 1750 | int dropped_file_index; // 0..sapp_get_num_dropped_files()-1 |
| 1751 | void (*callback)(const sapp_html5_fetch_response*); // response callback function pointer (required) |
| 1752 | sapp_range buffer; // ptr/size of a memory buffer to load the data into |
| 1753 | void* user_data; // optional userdata pointer |
| 1754 | } sapp_html5_fetch_request; |
| 1755 | |
| 1756 | /* |
| 1757 | sapp_mouse_cursor |
| 1758 | |
| 1759 | Predefined cursor image definitions, set with sapp_set_mouse_cursor(sapp_mouse_cursor cursor) |
| 1760 | */ |
| 1761 | typedef enum sapp_mouse_cursor { |
| 1762 | SAPP_MOUSECURSOR_DEFAULT = 0, // equivalent with system default cursor |
| 1763 | SAPP_MOUSECURSOR_ARROW, |
| 1764 | SAPP_MOUSECURSOR_IBEAM, |
| 1765 | SAPP_MOUSECURSOR_CROSSHAIR, |
| 1766 | SAPP_MOUSECURSOR_POINTING_HAND, |
| 1767 | SAPP_MOUSECURSOR_RESIZE_EW, |
| 1768 | SAPP_MOUSECURSOR_RESIZE_NS, |
| 1769 | SAPP_MOUSECURSOR_RESIZE_NWSE, |
| 1770 | SAPP_MOUSECURSOR_RESIZE_NESW, |
| 1771 | SAPP_MOUSECURSOR_RESIZE_ALL, |
| 1772 | SAPP_MOUSECURSOR_NOT_ALLOWED, |
| 1773 | _SAPP_MOUSECURSOR_NUM, |
| 1774 | } sapp_mouse_cursor; |
| 1775 | |
| 1776 | /* user-provided functions */ |
| 1777 | extern sapp_desc sokol_main(int argc, char* argv[]); |
| 1778 | |
| 1779 | /* returns true after sokol-app has been initialized */ |
| 1780 | SOKOL_APP_API_DECL bool sapp_isvalid(void); |
| 1781 | /* returns the current framebuffer width in pixels */ |
| 1782 | SOKOL_APP_API_DECL int sapp_width(void); |
| 1783 | /* same as sapp_width(), but returns float */ |
| 1784 | SOKOL_APP_API_DECL float sapp_widthf(void); |
| 1785 | /* returns the current framebuffer height in pixels */ |
| 1786 | SOKOL_APP_API_DECL int sapp_height(void); |
| 1787 | /* same as sapp_height(), but returns float */ |
| 1788 | SOKOL_APP_API_DECL float sapp_heightf(void); |
| 1789 | /* get default framebuffer color pixel format */ |
| 1790 | SOKOL_APP_API_DECL int sapp_color_format(void); |
| 1791 | /* get default framebuffer depth pixel format */ |
| 1792 | SOKOL_APP_API_DECL int sapp_depth_format(void); |
| 1793 | /* get default framebuffer sample count */ |
| 1794 | SOKOL_APP_API_DECL int sapp_sample_count(void); |
| 1795 | /* returns true when high_dpi was requested and actually running in a high-dpi scenario */ |
| 1796 | SOKOL_APP_API_DECL bool sapp_high_dpi(void); |
| 1797 | /* returns the dpi scaling factor (window pixels to framebuffer pixels) */ |
| 1798 | SOKOL_APP_API_DECL float sapp_dpi_scale(void); |
| 1799 | /* show or hide the mobile device onscreen keyboard */ |
| 1800 | SOKOL_APP_API_DECL void sapp_show_keyboard(bool show); |
| 1801 | /* return true if the mobile device onscreen keyboard is currently shown */ |
| 1802 | SOKOL_APP_API_DECL bool sapp_keyboard_shown(void); |
| 1803 | /* query fullscreen mode */ |
| 1804 | SOKOL_APP_API_DECL bool sapp_is_fullscreen(void); |
| 1805 | /* toggle fullscreen mode */ |
| 1806 | SOKOL_APP_API_DECL void sapp_toggle_fullscreen(void); |
| 1807 | /* show or hide the mouse cursor */ |
| 1808 | SOKOL_APP_API_DECL void sapp_show_mouse(bool show); |
| 1809 | /* show or hide the mouse cursor */ |
| 1810 | SOKOL_APP_API_DECL bool sapp_mouse_shown(void); |
| 1811 | /* enable/disable mouse-pointer-lock mode */ |
| 1812 | SOKOL_APP_API_DECL void sapp_lock_mouse(bool lock); |
| 1813 | /* return true if in mouse-pointer-lock mode (this may toggle a few frames later) */ |
| 1814 | SOKOL_APP_API_DECL bool sapp_mouse_locked(void); |
| 1815 | /* set mouse cursor type */ |
| 1816 | SOKOL_APP_API_DECL void sapp_set_mouse_cursor(sapp_mouse_cursor cursor); |
| 1817 | /* get current mouse cursor type */ |
| 1818 | SOKOL_APP_API_DECL sapp_mouse_cursor sapp_get_mouse_cursor(void); |
| 1819 | /* return the userdata pointer optionally provided in sapp_desc */ |
| 1820 | SOKOL_APP_API_DECL void* sapp_userdata(void); |
| 1821 | /* return a copy of the sapp_desc structure */ |
| 1822 | SOKOL_APP_API_DECL sapp_desc sapp_query_desc(void); |
| 1823 | /* initiate a "soft quit" (sends SAPP_EVENTTYPE_QUIT_REQUESTED) */ |
| 1824 | SOKOL_APP_API_DECL void sapp_request_quit(void); |
| 1825 | /* cancel a pending quit (when SAPP_EVENTTYPE_QUIT_REQUESTED has been received) */ |
| 1826 | SOKOL_APP_API_DECL void sapp_cancel_quit(void); |
| 1827 | /* initiate a "hard quit" (quit application without sending SAPP_EVENTTYPE_QUIT_REQUESTED) */ |
| 1828 | SOKOL_APP_API_DECL void sapp_quit(void); |
| 1829 | /* call from inside event callback to consume the current event (don't forward to platform) */ |
| 1830 | SOKOL_APP_API_DECL void sapp_consume_event(void); |
| 1831 | /* get the current frame counter (for comparison with sapp_event.frame_count) */ |
| 1832 | SOKOL_APP_API_DECL uint64_t sapp_frame_count(void); |
| 1833 | /* get an averaged/smoothed frame duration in seconds */ |
| 1834 | SOKOL_APP_API_DECL double sapp_frame_duration(void); |
| 1835 | /* write string into clipboard */ |
| 1836 | SOKOL_APP_API_DECL void sapp_set_clipboard_string(const char* str); |
| 1837 | /* read string from clipboard (usually during SAPP_EVENTTYPE_CLIPBOARD_PASTED) */ |
| 1838 | SOKOL_APP_API_DECL const char* sapp_get_clipboard_string(void); |
| 1839 | /* set the window title (only on desktop platforms) */ |
| 1840 | SOKOL_APP_API_DECL void sapp_set_window_title(const char* str); |
| 1841 | /* set the window icon (only on Windows and Linux) */ |
| 1842 | SOKOL_APP_API_DECL void sapp_set_icon(const sapp_icon_desc* icon_desc); |
| 1843 | /* gets the total number of dropped files (after an SAPP_EVENTTYPE_FILES_DROPPED event) */ |
| 1844 | SOKOL_APP_API_DECL int sapp_get_num_dropped_files(void); |
| 1845 | /* gets the dropped file paths */ |
| 1846 | SOKOL_APP_API_DECL const char* sapp_get_dropped_file_path(int index); |
| 1847 | |
| 1848 | /* special run-function for SOKOL_NO_ENTRY (in standard mode this is an empty stub) */ |
| 1849 | SOKOL_APP_API_DECL void sapp_run(const sapp_desc* desc); |
| 1850 | |
| 1851 | /* EGL: get EGLDisplay object */ |
| 1852 | SOKOL_APP_API_DECL const void* sapp_egl_get_display(void); |
| 1853 | /* EGL: get EGLContext object */ |
| 1854 | SOKOL_APP_API_DECL const void* sapp_egl_get_context(void); |
| 1855 | |
| 1856 | /* HTML5: enable or disable the hardwired "Leave Site?" dialog box */ |
| 1857 | SOKOL_APP_API_DECL void sapp_html5_ask_leave_site(bool ask); |
| 1858 | /* HTML5: get byte size of a dropped file */ |
| 1859 | SOKOL_APP_API_DECL uint32_t sapp_html5_get_dropped_file_size(int index); |
| 1860 | /* HTML5: asynchronously load the content of a dropped file */ |
| 1861 | SOKOL_APP_API_DECL void sapp_html5_fetch_dropped_file(const sapp_html5_fetch_request* request); |
| 1862 | |
| 1863 | /* Metal: get bridged pointer to Metal device object */ |
| 1864 | SOKOL_APP_API_DECL const void* sapp_metal_get_device(void); |
| 1865 | /* Metal: get bridged pointer to MTKView's current drawable of type CAMetalDrawable */ |
| 1866 | SOKOL_APP_API_DECL const void* sapp_metal_get_current_drawable(void); |
| 1867 | /* Metal: get bridged pointer to MTKView's depth-stencil texture of type MTLTexture */ |
| 1868 | SOKOL_APP_API_DECL const void* sapp_metal_get_depth_stencil_texture(void); |
| 1869 | /* Metal: get bridged pointer to MTKView's msaa-color-texture of type MTLTexture (may be null) */ |
| 1870 | SOKOL_APP_API_DECL const void* sapp_metal_get_msaa_color_texture(void); |
| 1871 | /* macOS: get bridged pointer to macOS NSWindow */ |
| 1872 | SOKOL_APP_API_DECL const void* sapp_macos_get_window(void); |
| 1873 | /* iOS: get bridged pointer to iOS UIWindow */ |
| 1874 | SOKOL_APP_API_DECL const void* sapp_ios_get_window(void); |
| 1875 | |
| 1876 | /* D3D11: get pointer to ID3D11Device object */ |
| 1877 | SOKOL_APP_API_DECL const void* sapp_d3d11_get_device(void); |
| 1878 | /* D3D11: get pointer to ID3D11DeviceContext object */ |
| 1879 | SOKOL_APP_API_DECL const void* sapp_d3d11_get_device_context(void); |
| 1880 | /* D3D11: get pointer to IDXGISwapChain object */ |
| 1881 | SOKOL_APP_API_DECL const void* sapp_d3d11_get_swap_chain(void); |
| 1882 | /* D3D11: get pointer to ID3D11RenderTargetView object for rendering */ |
| 1883 | SOKOL_APP_API_DECL const void* sapp_d3d11_get_render_view(void); |
| 1884 | /* D3D11: get pointer ID3D11RenderTargetView object for msaa-resolve (may return null) */ |
| 1885 | SOKOL_APP_API_DECL const void* sapp_d3d11_get_resolve_view(void); |
| 1886 | /* D3D11: get pointer ID3D11DepthStencilView */ |
| 1887 | SOKOL_APP_API_DECL const void* sapp_d3d11_get_depth_stencil_view(void); |
| 1888 | /* Win32: get the HWND window handle */ |
| 1889 | SOKOL_APP_API_DECL const void* sapp_win32_get_hwnd(void); |
| 1890 | |
| 1891 | /* WebGPU: get WGPUDevice handle */ |
| 1892 | SOKOL_APP_API_DECL const void* sapp_wgpu_get_device(void); |
| 1893 | /* WebGPU: get swapchain's WGPUTextureView handle for rendering */ |
| 1894 | SOKOL_APP_API_DECL const void* sapp_wgpu_get_render_view(void); |
| 1895 | /* WebGPU: get swapchain's MSAA-resolve WGPUTextureView (may return null) */ |
| 1896 | SOKOL_APP_API_DECL const void* sapp_wgpu_get_resolve_view(void); |
| 1897 | /* WebGPU: get swapchain's WGPUTextureView for the depth-stencil surface */ |
| 1898 | SOKOL_APP_API_DECL const void* sapp_wgpu_get_depth_stencil_view(void); |
| 1899 | |
| 1900 | /* GL: get framebuffer object */ |
| 1901 | SOKOL_APP_API_DECL uint32_t sapp_gl_get_framebuffer(void); |
| 1902 | /* GL: get major version (only valid for desktop GL) */ |
| 1903 | SOKOL_APP_API_DECL int sapp_gl_get_major_version(void); |
| 1904 | /* GL: get minor version (only valid for desktop GL) */ |
| 1905 | SOKOL_APP_API_DECL int sapp_gl_get_minor_version(void); |
| 1906 | |
| 1907 | /* Android: get native activity handle */ |
| 1908 | SOKOL_APP_API_DECL const void* sapp_android_get_native_activity(void); |
| 1909 | |
| 1910 | #ifdef __cplusplus |
| 1911 | } /* extern "C" */ |
| 1912 | |
| 1913 | /* reference-based equivalents for C++ */ |
| 1914 | inline void sapp_run(const sapp_desc& desc) { return sapp_run(&desc); } |
| 1915 | |
| 1916 | #endif |
| 1917 | |
| 1918 | // this WinRT specific hack is required when wWinMain is in a static library |
| 1919 | #if defined(_MSC_VER) && defined(UNICODE) |
| 1920 | #include <winapifamily.h> |
| 1921 | #if defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
| 1922 | #pragma comment(linker, "/include:wWinMain") |
| 1923 | #endif |
| 1924 | #endif |
| 1925 | |
| 1926 | #endif // SOKOL_APP_INCLUDED |
| 1927 | |
| 1928 | // ██ ███ ███ ██████ ██ ███████ ███ ███ ███████ ███ ██ ████████ █████ ████████ ██ ██████ ███ ██ |
| 1929 | // ██ ████ ████ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ |
| 1930 | // ██ ██ ████ ██ ██████ ██ █████ ██ ████ ██ █████ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ |
| 1931 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 1932 | // ██ ██ ██ ██ ███████ ███████ ██ ██ ███████ ██ ████ ██ ██ ██ ██ ██ ██████ ██ ████ |
| 1933 | // |
| 1934 | // >>implementation |
| 1935 | #ifdef SOKOL_APP_IMPL |
| 1936 | #define SOKOL_APP_IMPL_INCLUDED (1) |
| 1937 | |
| 1938 | #if defined(SOKOL_MALLOC) || defined(SOKOL_CALLOC) || defined(SOKOL_FREE) |
| 1939 | #error "SOKOL_MALLOC/CALLOC/FREE macros are no longer supported, please use sapp_desc.allocator to override memory allocation functions" |
| 1940 | #endif |
| 1941 | |
| 1942 | #include <stdlib.h> // malloc, free |
| 1943 | #include <string.h> // memset |
| 1944 | #include <stddef.h> // size_t |
| 1945 | #include <math.h> // roundf |
| 1946 | |
| 1947 | // helper macros |
| 1948 | #define _sapp_def(val, def) (((val) == 0) ? (def) : (val)) |
| 1949 | #define _sapp_absf(a) (((a)<0.0f)?-(a):(a)) |
| 1950 | |
| 1951 | #define _SAPP_MAX_TITLE_LENGTH (128) |
| 1952 | #define _SAPP_FALLBACK_DEFAULT_WINDOW_WIDTH (640) |
| 1953 | #define _SAPP_FALLBACK_DEFAULT_WINDOW_HEIGHT (480) |
| 1954 | // NOTE: the pixel format values *must* be compatible with sg_pixel_format |
| 1955 | #define _SAPP_PIXELFORMAT_RGBA8 (23) |
| 1956 | #define _SAPP_PIXELFORMAT_BGRA8 (28) |
| 1957 | #define _SAPP_PIXELFORMAT_DEPTH (43) |
| 1958 | #define _SAPP_PIXELFORMAT_DEPTH_STENCIL (44) |
| 1959 | |
| 1960 | // check if the config defines are alright |
| 1961 | #if defined(__APPLE__) |
| 1962 | // see https://clang.llvm.org/docs/LanguageExtensions.html#automatic-reference-counting |
| 1963 | #if !defined(__cplusplus) |
| 1964 | #if __has_feature(objc_arc) && !__has_feature(objc_arc_fields) |
| 1965 | #error "sokol_app.h requires __has_feature(objc_arc_field) if ARC is enabled (use a more recent compiler version)" |
| 1966 | #endif |
| 1967 | #endif |
| 1968 | #define _SAPP_APPLE (1) |
| 1969 | #include <TargetConditionals.h> |
| 1970 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 1971 | /* MacOS */ |
| 1972 | #define _SAPP_MACOS (1) |
| 1973 | #if !defined(SOKOL_METAL) && !defined(SOKOL_GLCORE) |
| 1974 | #error("sokol_app.h: unknown 3D API selected for MacOS, must be SOKOL_METAL or SOKOL_GLCORE") |
| 1975 | #endif |
| 1976 | #else |
| 1977 | /* iOS or iOS Simulator */ |
| 1978 | #define _SAPP_IOS (1) |
| 1979 | #if !defined(SOKOL_METAL) && !defined(SOKOL_GLES3) |
| 1980 | #error("sokol_app.h: unknown 3D API selected for iOS, must be SOKOL_METAL or SOKOL_GLES3") |
| 1981 | #endif |
| 1982 | #endif |
| 1983 | #elif defined(__EMSCRIPTEN__) |
| 1984 | /* emscripten (asm.js or wasm) */ |
| 1985 | #define _SAPP_EMSCRIPTEN (1) |
| 1986 | #if !defined(SOKOL_GLES3) && !defined(SOKOL_WGPU) |
| 1987 | #error("sokol_app.h: unknown 3D API selected for emscripten, must be SOKOL_GLES3 or SOKOL_WGPU") |
| 1988 | #endif |
| 1989 | #elif defined(_WIN32) |
| 1990 | /* Windows (D3D11 or GL) */ |
| 1991 | #define _SAPP_WIN32 (1) |
| 1992 | #if !defined(SOKOL_D3D11) && !defined(SOKOL_GLCORE) && !defined(SOKOL_NOAPI) |
| 1993 | #error("sokol_app.h: unknown 3D API selected for Win32, must be SOKOL_D3D11, SOKOL_GLCORE or SOKOL_NOAPI") |
| 1994 | #endif |
| 1995 | #elif defined(__ANDROID__) |
| 1996 | /* Android */ |
| 1997 | #define _SAPP_ANDROID (1) |
| 1998 | #if !defined(SOKOL_GLES3) |
| 1999 | #error("sokol_app.h: unknown 3D API selected for Android, must be SOKOL_GLES3") |
| 2000 | #endif |
| 2001 | #if defined(SOKOL_NO_ENTRY) |
| 2002 | #error("sokol_app.h: SOKOL_NO_ENTRY is not supported on Android") |
| 2003 | #endif |
| 2004 | #elif defined(__linux__) || defined(__unix__) |
| 2005 | /* Linux */ |
| 2006 | #define _SAPP_LINUX (1) |
| 2007 | #if defined(SOKOL_GLCORE) |
| 2008 | #if !defined(SOKOL_FORCE_EGL) |
| 2009 | #define _SAPP_GLX (1) |
| 2010 | #endif |
| 2011 | #define GL_GLEXT_PROTOTYPES |
| 2012 | #include <GL/gl.h> |
| 2013 | #elif defined(SOKOL_GLES3) |
| 2014 | #include <GLES3/gl3.h> |
| 2015 | #include <GLES3/gl3ext.h> |
| 2016 | #else |
| 2017 | #error("sokol_app.h: unknown 3D API selected for Linux, must be SOKOL_GLCORE, SOKOL_GLES3") |
| 2018 | #endif |
| 2019 | #else |
| 2020 | #error "sokol_app.h: Unknown platform" |
| 2021 | #endif |
| 2022 | |
| 2023 | #if defined(SOKOL_GLCORE) || defined(SOKOL_GLES3) |
| 2024 | #define _SAPP_ANY_GL (1) |
| 2025 | #endif |
| 2026 | |
| 2027 | #ifndef SOKOL_API_IMPL |
| 2028 | #define SOKOL_API_IMPL |
| 2029 | #endif |
| 2030 | #ifndef SOKOL_DEBUG |
| 2031 | #ifndef NDEBUG |
| 2032 | #define SOKOL_DEBUG |
| 2033 | #endif |
| 2034 | #endif |
| 2035 | #ifndef SOKOL_ASSERT |
| 2036 | #include <assert.h> |
| 2037 | #define SOKOL_ASSERT(c) assert(c) |
| 2038 | #endif |
| 2039 | #ifndef SOKOL_UNREACHABLE |
| 2040 | #define SOKOL_UNREACHABLE SOKOL_ASSERT(false) |
| 2041 | #endif |
| 2042 | |
| 2043 | #ifndef _SOKOL_PRIVATE |
| 2044 | #if defined(__GNUC__) || defined(__clang__) |
| 2045 | #define _SOKOL_PRIVATE __attribute__((unused)) static |
| 2046 | #else |
| 2047 | #define _SOKOL_PRIVATE static |
| 2048 | #endif |
| 2049 | #endif |
| 2050 | #ifndef _SOKOL_UNUSED |
| 2051 | #define _SOKOL_UNUSED(x) (void)(x) |
| 2052 | #endif |
| 2053 | |
| 2054 | #if defined(_SAPP_APPLE) |
| 2055 | #if defined(SOKOL_METAL) |
| 2056 | #import <Metal/Metal.h> |
| 2057 | #import <MetalKit/MetalKit.h> |
| 2058 | #endif |
| 2059 | #if defined(_SAPP_MACOS) |
| 2060 | #if defined(_SAPP_ANY_GL) |
| 2061 | #ifndef GL_SILENCE_DEPRECATION |
| 2062 | #define GL_SILENCE_DEPRECATION |
| 2063 | #endif |
| 2064 | #include <Cocoa/Cocoa.h> |
| 2065 | #include <OpenGL/gl3.h> |
| 2066 | #endif |
| 2067 | #elif defined(_SAPP_IOS) |
| 2068 | #import <UIKit/UIKit.h> |
| 2069 | #if defined(_SAPP_ANY_GL) |
| 2070 | #import <GLKit/GLKit.h> |
| 2071 | #include <OpenGLES/ES3/gl.h> |
| 2072 | #endif |
| 2073 | #endif |
| 2074 | #include <AvailabilityMacros.h> |
| 2075 | #include <mach/mach_time.h> |
| 2076 | #elif defined(_SAPP_EMSCRIPTEN) |
| 2077 | #if defined(SOKOL_WGPU) |
| 2078 | #include <webgpu/webgpu.h> |
| 2079 | #endif |
| 2080 | #if defined(SOKOL_GLES3) |
| 2081 | #include <GLES3/gl3.h> |
| 2082 | #endif |
| 2083 | #include <emscripten/emscripten.h> |
| 2084 | #include <emscripten/html5.h> |
| 2085 | #elif defined(_SAPP_WIN32) |
| 2086 | #ifdef _MSC_VER |
| 2087 | #pragma warning(push) |
| 2088 | #pragma warning(disable:4201) /* nonstandard extension used: nameless struct/union */ |
| 2089 | #pragma warning(disable:4204) /* nonstandard extension used: non-constant aggregate initializer */ |
| 2090 | #pragma warning(disable:4054) /* 'type cast': from function pointer */ |
| 2091 | #pragma warning(disable:4055) /* 'type cast': from data pointer */ |
| 2092 | #pragma warning(disable:4505) /* unreferenced local function has been removed */ |
| 2093 | #pragma warning(disable:4115) /* /W4: 'ID3D11ModuleInstance': named type definition in parentheses (in d3d11.h) */ |
| 2094 | #endif |
| 2095 | #ifndef WIN32_LEAN_AND_MEAN |
| 2096 | #define WIN32_LEAN_AND_MEAN |
| 2097 | #endif |
| 2098 | #ifndef NOMINMAX |
| 2099 | #define NOMINMAX |
| 2100 | #endif |
| 2101 | #include <windows.h> |
| 2102 | #include <windowsx.h> |
| 2103 | #include <shellapi.h> |
| 2104 | #if !defined(SOKOL_NO_ENTRY) // if SOKOL_NO_ENTRY is defined, it's the applications' responsibility to use the right subsystem |
| 2105 | #if defined(SOKOL_WIN32_FORCE_MAIN) |
| 2106 | #pragma comment (linker, "/subsystem:console") |
| 2107 | #else |
| 2108 | #pragma comment (linker, "/subsystem:windows") |
| 2109 | #endif |
| 2110 | #endif |
| 2111 | #include <stdio.h> /* freopen_s() */ |
| 2112 | #include <wchar.h> /* wcslen() */ |
| 2113 | |
| 2114 | #pragma comment (lib, "kernel32") |
| 2115 | #pragma comment (lib, "user32") |
| 2116 | #pragma comment (lib, "shell32") /* CommandLineToArgvW, DragQueryFileW, DragFinished */ |
| 2117 | #pragma comment (lib, "gdi32") |
| 2118 | #if defined(SOKOL_D3D11) |
| 2119 | #pragma comment (lib, "dxgi") |
| 2120 | #pragma comment (lib, "d3d11") |
| 2121 | #endif |
| 2122 | |
| 2123 | #if defined(SOKOL_D3D11) |
| 2124 | #ifndef D3D11_NO_HELPERS |
| 2125 | #define D3D11_NO_HELPERS |
| 2126 | #endif |
| 2127 | #include <d3d11.h> |
| 2128 | #include <dxgi.h> |
| 2129 | // DXGI_SWAP_EFFECT_FLIP_DISCARD is only defined in newer Windows SDKs, so don't depend on it |
| 2130 | #define _SAPP_DXGI_SWAP_EFFECT_FLIP_DISCARD (4) |
| 2131 | #endif |
| 2132 | #ifndef WM_MOUSEHWHEEL /* see https://github.com/floooh/sokol/issues/138 */ |
| 2133 | #define WM_MOUSEHWHEEL (0x020E) |
| 2134 | #endif |
| 2135 | #ifndef WM_DPICHANGED |
| 2136 | #define WM_DPICHANGED (0x02E0) |
| 2137 | #endif |
| 2138 | #elif defined(_SAPP_ANDROID) |
| 2139 | #include <pthread.h> |
| 2140 | #include <unistd.h> |
| 2141 | #include <time.h> |
| 2142 | #include <android/native_activity.h> |
| 2143 | #include <android/looper.h> |
| 2144 | #include <EGL/egl.h> |
| 2145 | #include <GLES3/gl3.h> |
| 2146 | #elif defined(_SAPP_LINUX) |
| 2147 | #define GL_GLEXT_PROTOTYPES |
| 2148 | #include <X11/Xlib.h> |
| 2149 | #include <X11/Xutil.h> |
| 2150 | #include <X11/XKBlib.h> |
| 2151 | #include <X11/keysym.h> |
| 2152 | #include <X11/Xresource.h> |
| 2153 | #include <X11/Xatom.h> |
| 2154 | #include <X11/extensions/XInput2.h> |
| 2155 | #include <X11/Xcursor/Xcursor.h> |
| 2156 | #include <X11/cursorfont.h> /* XC_* font cursors */ |
| 2157 | #include <X11/Xmd.h> /* CARD32 */ |
| 2158 | #if !defined(_SAPP_GLX) |
| 2159 | #include <EGL/egl.h> |
| 2160 | #endif |
| 2161 | #include <dlfcn.h> /* dlopen, dlsym, dlclose */ |
| 2162 | #include <limits.h> /* LONG_MAX */ |
| 2163 | #include <pthread.h> /* only used a linker-guard, search for _sapp_linux_run() and see first comment */ |
| 2164 | #include <time.h> |
| 2165 | #endif |
| 2166 | |
| 2167 | #if defined(_SAPP_APPLE) |
| 2168 | // this is ARC compatible |
| 2169 | #if defined(__cplusplus) |
| 2170 | #define _SAPP_CLEAR_ARC_STRUCT(type, item) { item = type(); } |
| 2171 | #else |
| 2172 | #define _SAPP_CLEAR_ARC_STRUCT(type, item) { item = (type) { 0 }; } |
| 2173 | #endif |
| 2174 | #else |
| 2175 | #define _SAPP_CLEAR_ARC_STRUCT(type, item) { _sapp_clear(&item, sizeof(item)); } |
| 2176 | #endif |
| 2177 | |
| 2178 | |
| 2179 | // ███████ ██████ █████ ███ ███ ███████ ████████ ██ ███ ███ ██ ███ ██ ██████ |
| 2180 | // ██ ██ ██ ██ ██ ████ ████ ██ ██ ██ ████ ████ ██ ████ ██ ██ |
| 2181 | // █████ ██████ ███████ ██ ████ ██ █████ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ███ |
| 2182 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 2183 | // ██ ██ ██ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ████ ██████ |
| 2184 | // |
| 2185 | // >>frame timing |
| 2186 | #define _SAPP_RING_NUM_SLOTS (256) |
| 2187 | typedef struct { |
| 2188 | int head; |
| 2189 | int tail; |
| 2190 | double buf[_SAPP_RING_NUM_SLOTS]; |
| 2191 | } _sapp_ring_t; |
| 2192 | |
| 2193 | _SOKOL_PRIVATE int _sapp_ring_idx(int i) { |
| 2194 | return i % _SAPP_RING_NUM_SLOTS; |
| 2195 | } |
| 2196 | |
| 2197 | _SOKOL_PRIVATE void _sapp_ring_init(_sapp_ring_t* ring) { |
| 2198 | ring->head = 0; |
| 2199 | ring->tail = 0; |
| 2200 | } |
| 2201 | |
| 2202 | _SOKOL_PRIVATE bool _sapp_ring_full(_sapp_ring_t* ring) { |
| 2203 | return _sapp_ring_idx(ring->head + 1) == ring->tail; |
| 2204 | } |
| 2205 | |
| 2206 | _SOKOL_PRIVATE bool _sapp_ring_empty(_sapp_ring_t* ring) { |
| 2207 | return ring->head == ring->tail; |
| 2208 | } |
| 2209 | |
| 2210 | _SOKOL_PRIVATE int _sapp_ring_count(_sapp_ring_t* ring) { |
| 2211 | int count; |
| 2212 | if (ring->head >= ring->tail) { |
| 2213 | count = ring->head - ring->tail; |
| 2214 | } |
| 2215 | else { |
| 2216 | count = (ring->head + _SAPP_RING_NUM_SLOTS) - ring->tail; |
| 2217 | } |
| 2218 | SOKOL_ASSERT((count >= 0) && (count < _SAPP_RING_NUM_SLOTS)); |
| 2219 | return count; |
| 2220 | } |
| 2221 | |
| 2222 | _SOKOL_PRIVATE void _sapp_ring_enqueue(_sapp_ring_t* ring, double val) { |
| 2223 | SOKOL_ASSERT(!_sapp_ring_full(ring)); |
| 2224 | ring->buf[ring->head] = val; |
| 2225 | ring->head = _sapp_ring_idx(ring->head + 1); |
| 2226 | } |
| 2227 | |
| 2228 | _SOKOL_PRIVATE double _sapp_ring_dequeue(_sapp_ring_t* ring) { |
| 2229 | SOKOL_ASSERT(!_sapp_ring_empty(ring)); |
| 2230 | double val = ring->buf[ring->tail]; |
| 2231 | ring->tail = _sapp_ring_idx(ring->tail + 1); |
| 2232 | return val; |
| 2233 | } |
| 2234 | |
| 2235 | /* |
| 2236 | NOTE: |
| 2237 | |
| 2238 | Q: Why not use CAMetalDrawable.presentedTime on macOS and iOS? |
| 2239 | A: The value appears to be highly unstable during the first few |
| 2240 | seconds, sometimes several frames are dropped in sequence, or |
| 2241 | switch between 120 and 60 Hz for a few frames. Simply measuring |
| 2242 | and averaging the frame time yielded a more stable frame duration. |
| 2243 | Maybe switching to CVDisplayLink would yield better results. |
| 2244 | Until then just measure the time. |
| 2245 | */ |
| 2246 | typedef struct { |
| 2247 | #if defined(_SAPP_APPLE) |
| 2248 | struct { |
| 2249 | mach_timebase_info_data_t timebase; |
| 2250 | uint64_t start; |
| 2251 | } mach; |
| 2252 | #elif defined(_SAPP_EMSCRIPTEN) |
| 2253 | // empty |
| 2254 | #elif defined(_SAPP_WIN32) |
| 2255 | struct { |
| 2256 | LARGE_INTEGER freq; |
| 2257 | LARGE_INTEGER start; |
| 2258 | } win; |
| 2259 | #else // Linux, Android, ... |
| 2260 | #ifdef CLOCK_MONOTONIC |
| 2261 | #define _SAPP_CLOCK_MONOTONIC CLOCK_MONOTONIC |
| 2262 | #else |
| 2263 | // on some embedded platforms, CLOCK_MONOTONIC isn't defined |
| 2264 | #define _SAPP_CLOCK_MONOTONIC (1) |
| 2265 | #endif |
| 2266 | struct { |
| 2267 | uint64_t start; |
| 2268 | } posix; |
| 2269 | #endif |
| 2270 | } _sapp_timestamp_t; |
| 2271 | |
| 2272 | _SOKOL_PRIVATE int64_t _sapp_int64_muldiv(int64_t value, int64_t numer, int64_t denom) { |
| 2273 | int64_t q = value / denom; |
| 2274 | int64_t r = value % denom; |
| 2275 | return q * numer + r * numer / denom; |
| 2276 | } |
| 2277 | |
| 2278 | _SOKOL_PRIVATE void _sapp_timestamp_init(_sapp_timestamp_t* ts) { |
| 2279 | #if defined(_SAPP_APPLE) |
| 2280 | mach_timebase_info(&ts->mach.timebase); |
| 2281 | ts->mach.start = mach_absolute_time(); |
| 2282 | #elif defined(_SAPP_EMSCRIPTEN) |
| 2283 | (void)ts; |
| 2284 | #elif defined(_SAPP_WIN32) |
| 2285 | QueryPerformanceFrequency(&ts->win.freq); |
| 2286 | QueryPerformanceCounter(&ts->win.start); |
| 2287 | #else |
| 2288 | struct timespec tspec; |
| 2289 | clock_gettime(_SAPP_CLOCK_MONOTONIC, &tspec); |
| 2290 | ts->posix.start = (uint64_t)tspec.tv_sec*1000000000 + (uint64_t)tspec.tv_nsec; |
| 2291 | #endif |
| 2292 | } |
| 2293 | |
| 2294 | _SOKOL_PRIVATE double _sapp_timestamp_now(_sapp_timestamp_t* ts) { |
| 2295 | #if defined(_SAPP_APPLE) |
| 2296 | const uint64_t traw = mach_absolute_time() - ts->mach.start; |
| 2297 | const uint64_t now = (uint64_t) _sapp_int64_muldiv((int64_t)traw, (int64_t)ts->mach.timebase.numer, (int64_t)ts->mach.timebase.denom); |
| 2298 | return (double)now / 1000000000.0; |
| 2299 | #elif defined(_SAPP_EMSCRIPTEN) |
| 2300 | (void)ts; |
| 2301 | SOKOL_ASSERT(false); |
| 2302 | return 0.0; |
| 2303 | #elif defined(_SAPP_WIN32) |
| 2304 | LARGE_INTEGER qpc; |
| 2305 | QueryPerformanceCounter(&qpc); |
| 2306 | const uint64_t now = (uint64_t)_sapp_int64_muldiv(qpc.QuadPart - ts->win.start.QuadPart, 1000000000, ts->win.freq.QuadPart); |
| 2307 | return (double)now / 1000000000.0; |
| 2308 | #else |
| 2309 | struct timespec tspec; |
| 2310 | clock_gettime(_SAPP_CLOCK_MONOTONIC, &tspec); |
| 2311 | const uint64_t now = ((uint64_t)tspec.tv_sec*1000000000 + (uint64_t)tspec.tv_nsec) - ts->posix.start; |
| 2312 | return (double)now / 1000000000.0; |
| 2313 | #endif |
| 2314 | } |
| 2315 | |
| 2316 | typedef struct { |
| 2317 | double last; |
| 2318 | double accum; |
| 2319 | double avg; |
| 2320 | int spike_count; |
| 2321 | int num; |
| 2322 | _sapp_timestamp_t timestamp; |
| 2323 | _sapp_ring_t ring; |
| 2324 | } _sapp_timing_t; |
| 2325 | |
| 2326 | _SOKOL_PRIVATE void _sapp_timing_reset(_sapp_timing_t* t) { |
| 2327 | t->last = 0.0; |
| 2328 | t->accum = 0.0; |
| 2329 | t->spike_count = 0; |
| 2330 | t->num = 0; |
| 2331 | _sapp_ring_init(&t->ring); |
| 2332 | } |
| 2333 | |
| 2334 | _SOKOL_PRIVATE void _sapp_timing_init(_sapp_timing_t* t) { |
| 2335 | t->avg = 1.0 / 60.0; // dummy value until first actual value is available |
| 2336 | _sapp_timing_reset(t); |
| 2337 | _sapp_timestamp_init(&t->timestamp); |
| 2338 | } |
| 2339 | |
| 2340 | _SOKOL_PRIVATE void _sapp_timing_put(_sapp_timing_t* t, double dur) { |
| 2341 | // arbitrary upper limit to ignore outliers (e.g. during window resizing, or debugging) |
| 2342 | double min_dur = 0.0; |
| 2343 | double max_dur = 0.1; |
| 2344 | // if we have enough samples for a useful average, use a much tighter 'valid window' |
| 2345 | if (_sapp_ring_full(&t->ring)) { |
| 2346 | min_dur = t->avg * 0.8; |
| 2347 | max_dur = t->avg * 1.2; |
| 2348 | } |
| 2349 | if ((dur < min_dur) || (dur > max_dur)) { |
| 2350 | t->spike_count++; |
| 2351 | // if there have been many spikes in a row, the display refresh rate |
| 2352 | // might have changed, so a timing reset is needed |
| 2353 | if (t->spike_count > 20) { |
| 2354 | _sapp_timing_reset(t); |
| 2355 | } |
| 2356 | return; |
| 2357 | } |
| 2358 | if (_sapp_ring_full(&t->ring)) { |
| 2359 | double old_val = _sapp_ring_dequeue(&t->ring); |
| 2360 | t->accum -= old_val; |
| 2361 | t->num -= 1; |
| 2362 | } |
| 2363 | _sapp_ring_enqueue(&t->ring, dur); |
| 2364 | t->accum += dur; |
| 2365 | t->num += 1; |
| 2366 | SOKOL_ASSERT(t->num > 0); |
| 2367 | t->avg = t->accum / t->num; |
| 2368 | t->spike_count = 0; |
| 2369 | } |
| 2370 | |
| 2371 | _SOKOL_PRIVATE void _sapp_timing_discontinuity(_sapp_timing_t* t) { |
| 2372 | t->last = 0.0; |
| 2373 | } |
| 2374 | |
| 2375 | _SOKOL_PRIVATE void _sapp_timing_measure(_sapp_timing_t* t) { |
| 2376 | const double now = _sapp_timestamp_now(&t->timestamp); |
| 2377 | if (t->last > 0.0) { |
| 2378 | double dur = now - t->last; |
| 2379 | _sapp_timing_put(t, dur); |
| 2380 | } |
| 2381 | t->last = now; |
| 2382 | } |
| 2383 | |
| 2384 | _SOKOL_PRIVATE void _sapp_timing_external(_sapp_timing_t* t, double now) { |
| 2385 | if (t->last > 0.0) { |
| 2386 | double dur = now - t->last; |
| 2387 | _sapp_timing_put(t, dur); |
| 2388 | } |
| 2389 | t->last = now; |
| 2390 | } |
| 2391 | |
| 2392 | _SOKOL_PRIVATE double _sapp_timing_get_avg(_sapp_timing_t* t) { |
| 2393 | return t->avg; |
| 2394 | } |
| 2395 | |
| 2396 | // ███████ ████████ ██████ ██ ██ ██████ ████████ ███████ |
| 2397 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 2398 | // ███████ ██ ██████ ██ ██ ██ ██ ███████ |
| 2399 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 2400 | // ███████ ██ ██ ██ ██████ ██████ ██ ███████ |
| 2401 | // |
| 2402 | // >> structs |
| 2403 | #if defined(_SAPP_MACOS) |
| 2404 | @interface _sapp_macos_app_delegate : NSObject<NSApplicationDelegate> |
| 2405 | @end |
| 2406 | @interface _sapp_macos_window : NSWindow |
| 2407 | @end |
| 2408 | @interface _sapp_macos_window_delegate : NSObject<NSWindowDelegate> |
| 2409 | @end |
| 2410 | #if defined(SOKOL_METAL) |
| 2411 | @interface _sapp_macos_view : MTKView |
| 2412 | @end |
| 2413 | #elif defined(SOKOL_GLCORE) |
| 2414 | @interface _sapp_macos_view : NSOpenGLView |
| 2415 | - (void)timerFired:(id)sender; |
| 2416 | @end |
| 2417 | #endif // SOKOL_GLCORE |
| 2418 | |
| 2419 | typedef struct { |
| 2420 | uint32_t flags_changed_store; |
| 2421 | uint8_t mouse_buttons; |
| 2422 | NSWindow* window; |
| 2423 | NSTrackingArea* tracking_area; |
| 2424 | id keyup_monitor; |
| 2425 | _sapp_macos_app_delegate* app_dlg; |
| 2426 | _sapp_macos_window_delegate* win_dlg; |
| 2427 | _sapp_macos_view* view; |
| 2428 | NSCursor* cursors[_SAPP_MOUSECURSOR_NUM]; |
| 2429 | #if defined(SOKOL_METAL) |
| 2430 | id<MTLDevice> mtl_device; |
| 2431 | #endif |
| 2432 | } _sapp_macos_t; |
| 2433 | |
| 2434 | #endif // _SAPP_MACOS |
| 2435 | |
| 2436 | #if defined(_SAPP_IOS) |
| 2437 | |
| 2438 | @interface _sapp_app_delegate : NSObject<UIApplicationDelegate> |
| 2439 | @end |
| 2440 | @interface _sapp_textfield_dlg : NSObject<UITextFieldDelegate> |
| 2441 | - (void)keyboardWasShown:(NSNotification*)notif; |
| 2442 | - (void)keyboardWillBeHidden:(NSNotification*)notif; |
| 2443 | - (void)keyboardDidChangeFrame:(NSNotification*)notif; |
| 2444 | @end |
| 2445 | #if defined(SOKOL_METAL) |
| 2446 | @interface _sapp_ios_view : MTKView; |
| 2447 | @end |
| 2448 | #else |
| 2449 | @interface _sapp_ios_view : GLKView |
| 2450 | @end |
| 2451 | #endif |
| 2452 | |
| 2453 | typedef struct { |
| 2454 | UIWindow* window; |
| 2455 | _sapp_ios_view* view; |
| 2456 | UITextField* textfield; |
| 2457 | _sapp_textfield_dlg* textfield_dlg; |
| 2458 | #if defined(SOKOL_METAL) |
| 2459 | UIViewController* view_ctrl; |
| 2460 | id<MTLDevice> mtl_device; |
| 2461 | #else |
| 2462 | GLKViewController* view_ctrl; |
| 2463 | EAGLContext* eagl_ctx; |
| 2464 | #endif |
| 2465 | bool suspended; |
| 2466 | } _sapp_ios_t; |
| 2467 | |
| 2468 | #endif // _SAPP_IOS |
| 2469 | |
| 2470 | #if defined(_SAPP_EMSCRIPTEN) |
| 2471 | |
| 2472 | #if defined(SOKOL_WGPU) |
| 2473 | typedef struct { |
| 2474 | WGPUInstance instance; |
| 2475 | WGPUAdapter adapter; |
| 2476 | WGPUDevice device; |
| 2477 | WGPUTextureFormat render_format; |
| 2478 | WGPUSurface surface; |
| 2479 | WGPUSwapChain swapchain; |
| 2480 | WGPUTexture msaa_tex; |
| 2481 | WGPUTextureView msaa_view; |
| 2482 | WGPUTexture depth_stencil_tex; |
| 2483 | WGPUTextureView depth_stencil_view; |
| 2484 | WGPUTextureView swapchain_view; |
| 2485 | bool async_init_done; |
| 2486 | } _sapp_wgpu_t; |
| 2487 | #endif |
| 2488 | |
| 2489 | typedef struct { |
| 2490 | bool mouse_lock_requested; |
| 2491 | uint16_t mouse_buttons; |
| 2492 | } _sapp_emsc_t; |
| 2493 | #endif // _SAPP_EMSCRIPTEN |
| 2494 | |
| 2495 | #if defined(SOKOL_D3D11) && defined(_SAPP_WIN32) |
| 2496 | typedef struct { |
| 2497 | ID3D11Device* device; |
| 2498 | ID3D11DeviceContext* device_context; |
| 2499 | ID3D11Texture2D* rt; |
| 2500 | ID3D11RenderTargetView* rtv; |
| 2501 | ID3D11Texture2D* msaa_rt; |
| 2502 | ID3D11RenderTargetView* msaa_rtv; |
| 2503 | ID3D11Texture2D* ds; |
| 2504 | ID3D11DepthStencilView* dsv; |
| 2505 | DXGI_SWAP_CHAIN_DESC swap_chain_desc; |
| 2506 | IDXGISwapChain* swap_chain; |
| 2507 | IDXGIDevice1* dxgi_device; |
| 2508 | bool use_dxgi_frame_stats; |
| 2509 | UINT sync_refresh_count; |
| 2510 | } _sapp_d3d11_t; |
| 2511 | #endif |
| 2512 | |
| 2513 | #if defined(_SAPP_WIN32) |
| 2514 | |
| 2515 | #ifndef DPI_ENUMS_DECLARED |
| 2516 | typedef enum PROCESS_DPI_AWARENESS |
| 2517 | { |
| 2518 | PROCESS_DPI_UNAWARE = 0, |
| 2519 | PROCESS_SYSTEM_DPI_AWARE = 1, |
| 2520 | PROCESS_PER_MONITOR_DPI_AWARE = 2 |
| 2521 | } PROCESS_DPI_AWARENESS; |
| 2522 | typedef enum MONITOR_DPI_TYPE { |
| 2523 | MDT_EFFECTIVE_DPI = 0, |
| 2524 | MDT_ANGULAR_DPI = 1, |
| 2525 | MDT_RAW_DPI = 2, |
| 2526 | MDT_DEFAULT = MDT_EFFECTIVE_DPI |
| 2527 | } MONITOR_DPI_TYPE; |
| 2528 | #endif // DPI_ENUMS_DECLARED |
| 2529 | |
| 2530 | typedef struct { |
| 2531 | bool aware; |
| 2532 | float content_scale; |
| 2533 | float window_scale; |
| 2534 | float mouse_scale; |
| 2535 | } _sapp_win32_dpi_t; |
| 2536 | |
| 2537 | typedef struct { |
| 2538 | HWND hwnd; |
| 2539 | HMONITOR hmonitor; |
| 2540 | HDC dc; |
| 2541 | HICON big_icon; |
| 2542 | HICON small_icon; |
| 2543 | HCURSOR cursors[_SAPP_MOUSECURSOR_NUM]; |
| 2544 | UINT orig_codepage; |
| 2545 | LONG mouse_locked_x, mouse_locked_y; |
| 2546 | RECT stored_window_rect; // used to restore window pos/size when toggling fullscreen => windowed |
| 2547 | bool is_win10_or_greater; |
| 2548 | bool in_create_window; |
| 2549 | bool iconified; |
| 2550 | bool mouse_tracked; |
| 2551 | uint8_t mouse_capture_mask; |
| 2552 | _sapp_win32_dpi_t dpi; |
| 2553 | bool raw_input_mousepos_valid; |
| 2554 | LONG raw_input_mousepos_x; |
| 2555 | LONG raw_input_mousepos_y; |
| 2556 | uint8_t raw_input_data[256]; |
| 2557 | } _sapp_win32_t; |
| 2558 | |
| 2559 | #if defined(SOKOL_GLCORE) |
| 2560 | #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000 |
| 2561 | #define WGL_SUPPORT_OPENGL_ARB 0x2010 |
| 2562 | #define WGL_DRAW_TO_WINDOW_ARB 0x2001 |
| 2563 | #define WGL_PIXEL_TYPE_ARB 0x2013 |
| 2564 | #define WGL_TYPE_RGBA_ARB 0x202b |
| 2565 | #define WGL_ACCELERATION_ARB 0x2003 |
| 2566 | #define WGL_NO_ACCELERATION_ARB 0x2025 |
| 2567 | #define WGL_RED_BITS_ARB 0x2015 |
| 2568 | #define WGL_GREEN_BITS_ARB 0x2017 |
| 2569 | #define WGL_BLUE_BITS_ARB 0x2019 |
| 2570 | #define WGL_ALPHA_BITS_ARB 0x201b |
| 2571 | #define WGL_DEPTH_BITS_ARB 0x2022 |
| 2572 | #define WGL_STENCIL_BITS_ARB 0x2023 |
| 2573 | #define WGL_DOUBLE_BUFFER_ARB 0x2011 |
| 2574 | #define WGL_SAMPLES_ARB 0x2042 |
| 2575 | #define WGL_CONTEXT_DEBUG_BIT_ARB 0x00000001 |
| 2576 | #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002 |
| 2577 | #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126 |
| 2578 | #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001 |
| 2579 | #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091 |
| 2580 | #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092 |
| 2581 | #define WGL_CONTEXT_FLAGS_ARB 0x2094 |
| 2582 | #define ERROR_INVALID_VERSION_ARB 0x2095 |
| 2583 | #define ERROR_INVALID_PROFILE_ARB 0x2096 |
| 2584 | #define ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB 0x2054 |
| 2585 | typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC)(int); |
| 2586 | typedef BOOL (WINAPI * PFNWGLGETPIXELFORMATATTRIBIVARBPROC)(HDC,int,int,UINT,const int*,int*); |
| 2587 | typedef const char* (WINAPI * PFNWGLGETEXTENSIONSSTRINGEXTPROC)(void); |
| 2588 | typedef const char* (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC)(HDC); |
| 2589 | typedef HGLRC (WINAPI * PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC,HGLRC,const int*); |
| 2590 | typedef HGLRC (WINAPI * PFN_wglCreateContext)(HDC); |
| 2591 | typedef BOOL (WINAPI * PFN_wglDeleteContext)(HGLRC); |
| 2592 | typedef PROC (WINAPI * PFN_wglGetProcAddress)(LPCSTR); |
| 2593 | typedef HDC (WINAPI * PFN_wglGetCurrentDC)(void); |
| 2594 | typedef BOOL (WINAPI * PFN_wglMakeCurrent)(HDC,HGLRC); |
| 2595 | |
| 2596 | typedef struct { |
| 2597 | HINSTANCE opengl32; |
| 2598 | HGLRC gl_ctx; |
| 2599 | PFN_wglCreateContext CreateContext; |
| 2600 | PFN_wglDeleteContext DeleteContext; |
| 2601 | PFN_wglGetProcAddress GetProcAddress; |
| 2602 | PFN_wglGetCurrentDC GetCurrentDC; |
| 2603 | PFN_wglMakeCurrent MakeCurrent; |
| 2604 | PFNWGLSWAPINTERVALEXTPROC SwapIntervalEXT; |
| 2605 | PFNWGLGETPIXELFORMATATTRIBIVARBPROC GetPixelFormatAttribivARB; |
| 2606 | PFNWGLGETEXTENSIONSSTRINGEXTPROC GetExtensionsStringEXT; |
| 2607 | PFNWGLGETEXTENSIONSSTRINGARBPROC GetExtensionsStringARB; |
| 2608 | PFNWGLCREATECONTEXTATTRIBSARBPROC CreateContextAttribsARB; |
| 2609 | // special case glGetIntegerv |
| 2610 | void (WINAPI *GetIntegerv)(uint32_t pname, int32_t* data); |
| 2611 | bool ext_swap_control; |
| 2612 | bool arb_multisample; |
| 2613 | bool arb_pixel_format; |
| 2614 | bool arb_create_context; |
| 2615 | bool arb_create_context_profile; |
| 2616 | HWND msg_hwnd; |
| 2617 | HDC msg_dc; |
| 2618 | } _sapp_wgl_t; |
| 2619 | #endif // SOKOL_GLCORE |
| 2620 | |
| 2621 | #endif // _SAPP_WIN32 |
| 2622 | |
| 2623 | #if defined(_SAPP_ANDROID) |
| 2624 | typedef enum { |
| 2625 | _SOKOL_ANDROID_MSG_CREATE, |
| 2626 | _SOKOL_ANDROID_MSG_RESUME, |
| 2627 | _SOKOL_ANDROID_MSG_PAUSE, |
| 2628 | _SOKOL_ANDROID_MSG_FOCUS, |
| 2629 | _SOKOL_ANDROID_MSG_NO_FOCUS, |
| 2630 | _SOKOL_ANDROID_MSG_SET_NATIVE_WINDOW, |
| 2631 | _SOKOL_ANDROID_MSG_SET_INPUT_QUEUE, |
| 2632 | _SOKOL_ANDROID_MSG_DESTROY, |
| 2633 | } _sapp_android_msg_t; |
| 2634 | |
| 2635 | typedef struct { |
| 2636 | pthread_t thread; |
| 2637 | pthread_mutex_t mutex; |
| 2638 | pthread_cond_t cond; |
| 2639 | int read_from_main_fd; |
| 2640 | int write_from_main_fd; |
| 2641 | } _sapp_android_pt_t; |
| 2642 | |
| 2643 | typedef struct { |
| 2644 | ANativeWindow* window; |
| 2645 | AInputQueue* input; |
| 2646 | } _sapp_android_resources_t; |
| 2647 | |
| 2648 | typedef struct { |
| 2649 | ANativeActivity* activity; |
| 2650 | _sapp_android_pt_t pt; |
| 2651 | _sapp_android_resources_t pending; |
| 2652 | _sapp_android_resources_t current; |
| 2653 | ALooper* looper; |
| 2654 | bool is_thread_started; |
| 2655 | bool is_thread_stopping; |
| 2656 | bool is_thread_stopped; |
| 2657 | bool has_created; |
| 2658 | bool has_resumed; |
| 2659 | bool has_focus; |
| 2660 | EGLConfig config; |
| 2661 | EGLDisplay display; |
| 2662 | EGLContext context; |
| 2663 | EGLSurface surface; |
| 2664 | } _sapp_android_t; |
| 2665 | |
| 2666 | #endif // _SAPP_ANDROID |
| 2667 | |
| 2668 | #if defined(_SAPP_LINUX) |
| 2669 | |
| 2670 | #define _SAPP_X11_XDND_VERSION (5) |
| 2671 | |
| 2672 | #define GLX_VENDOR 1 |
| 2673 | #define GLX_RGBA_BIT 0x00000001 |
| 2674 | #define GLX_WINDOW_BIT 0x00000001 |
| 2675 | #define GLX_DRAWABLE_TYPE 0x8010 |
| 2676 | #define GLX_RENDER_TYPE 0x8011 |
| 2677 | #define GLX_DOUBLEBUFFER 5 |
| 2678 | #define GLX_RED_SIZE 8 |
| 2679 | #define GLX_GREEN_SIZE 9 |
| 2680 | #define GLX_BLUE_SIZE 10 |
| 2681 | #define GLX_ALPHA_SIZE 11 |
| 2682 | #define GLX_DEPTH_SIZE 12 |
| 2683 | #define GLX_STENCIL_SIZE 13 |
| 2684 | #define GLX_SAMPLES 0x186a1 |
| 2685 | #define GLX_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001 |
| 2686 | #define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126 |
| 2687 | #define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002 |
| 2688 | #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 |
| 2689 | #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 |
| 2690 | #define GLX_CONTEXT_FLAGS_ARB 0x2094 |
| 2691 | |
| 2692 | typedef XID GLXWindow; |
| 2693 | typedef XID GLXDrawable; |
| 2694 | typedef struct __GLXFBConfig* GLXFBConfig; |
| 2695 | typedef struct __GLXcontext* GLXContext; |
| 2696 | typedef void (*__GLXextproc)(void); |
| 2697 | |
| 2698 | typedef int (*PFNGLXGETFBCONFIGATTRIBPROC)(Display*,GLXFBConfig,int,int*); |
| 2699 | typedef const char* (*PFNGLXGETCLIENTSTRINGPROC)(Display*,int); |
| 2700 | typedef Bool (*PFNGLXQUERYEXTENSIONPROC)(Display*,int*,int*); |
| 2701 | typedef Bool (*PFNGLXQUERYVERSIONPROC)(Display*,int*,int*); |
| 2702 | typedef void (*PFNGLXDESTROYCONTEXTPROC)(Display*,GLXContext); |
| 2703 | typedef Bool (*PFNGLXMAKECURRENTPROC)(Display*,GLXDrawable,GLXContext); |
| 2704 | typedef void (*PFNGLXSWAPBUFFERSPROC)(Display*,GLXDrawable); |
| 2705 | typedef const char* (*PFNGLXQUERYEXTENSIONSSTRINGPROC)(Display*,int); |
| 2706 | typedef GLXFBConfig* (*PFNGLXGETFBCONFIGSPROC)(Display*,int,int*); |
| 2707 | typedef __GLXextproc (* PFNGLXGETPROCADDRESSPROC)(const char *procName); |
| 2708 | typedef void (*PFNGLXSWAPINTERVALEXTPROC)(Display*,GLXDrawable,int); |
| 2709 | typedef XVisualInfo* (*PFNGLXGETVISUALFROMFBCONFIGPROC)(Display*,GLXFBConfig); |
| 2710 | typedef GLXWindow (*PFNGLXCREATEWINDOWPROC)(Display*,GLXFBConfig,Window,const int*); |
| 2711 | typedef void (*PFNGLXDESTROYWINDOWPROC)(Display*,GLXWindow); |
| 2712 | |
| 2713 | typedef int (*PFNGLXSWAPINTERVALMESAPROC)(int); |
| 2714 | typedef GLXContext (*PFNGLXCREATECONTEXTATTRIBSARBPROC)(Display*,GLXFBConfig,GLXContext,Bool,const int*); |
| 2715 | |
| 2716 | typedef struct { |
| 2717 | bool available; |
| 2718 | int major_opcode; |
| 2719 | int event_base; |
| 2720 | int error_base; |
| 2721 | int major; |
| 2722 | int minor; |
| 2723 | } _sapp_xi_t; |
| 2724 | |
| 2725 | typedef struct { |
| 2726 | int version; |
| 2727 | Window source; |
| 2728 | Atom format; |
| 2729 | Atom XdndAware; |
| 2730 | Atom XdndEnter; |
| 2731 | Atom XdndPosition; |
| 2732 | Atom XdndStatus; |
| 2733 | Atom XdndActionCopy; |
| 2734 | Atom XdndDrop; |
| 2735 | Atom XdndFinished; |
| 2736 | Atom XdndSelection; |
| 2737 | Atom XdndTypeList; |
| 2738 | Atom text_uri_list; |
| 2739 | } _sapp_xdnd_t; |
| 2740 | |
| 2741 | typedef struct { |
| 2742 | uint8_t mouse_buttons; |
| 2743 | Display* display; |
| 2744 | int screen; |
| 2745 | Window root; |
| 2746 | Colormap colormap; |
| 2747 | Window window; |
| 2748 | Cursor hidden_cursor; |
| 2749 | Cursor cursors[_SAPP_MOUSECURSOR_NUM]; |
| 2750 | int window_state; |
| 2751 | float dpi; |
| 2752 | unsigned char error_code; |
| 2753 | Atom UTF8_STRING; |
| 2754 | Atom WM_PROTOCOLS; |
| 2755 | Atom WM_DELETE_WINDOW; |
| 2756 | Atom WM_STATE; |
| 2757 | Atom NET_WM_NAME; |
| 2758 | Atom NET_WM_ICON_NAME; |
| 2759 | Atom NET_WM_ICON; |
| 2760 | Atom NET_WM_STATE; |
| 2761 | Atom NET_WM_STATE_FULLSCREEN; |
| 2762 | _sapp_xi_t xi; |
| 2763 | _sapp_xdnd_t xdnd; |
| 2764 | } _sapp_x11_t; |
| 2765 | |
| 2766 | #if defined(_SAPP_GLX) |
| 2767 | |
| 2768 | typedef struct { |
| 2769 | void* libgl; |
| 2770 | int major; |
| 2771 | int minor; |
| 2772 | int event_base; |
| 2773 | int error_base; |
| 2774 | GLXContext ctx; |
| 2775 | GLXWindow window; |
| 2776 | |
| 2777 | // GLX 1.3 functions |
| 2778 | PFNGLXGETFBCONFIGSPROC GetFBConfigs; |
| 2779 | PFNGLXGETFBCONFIGATTRIBPROC GetFBConfigAttrib; |
| 2780 | PFNGLXGETCLIENTSTRINGPROC GetClientString; |
| 2781 | PFNGLXQUERYEXTENSIONPROC QueryExtension; |
| 2782 | PFNGLXQUERYVERSIONPROC QueryVersion; |
| 2783 | PFNGLXDESTROYCONTEXTPROC DestroyContext; |
| 2784 | PFNGLXMAKECURRENTPROC MakeCurrent; |
| 2785 | PFNGLXSWAPBUFFERSPROC SwapBuffers; |
| 2786 | PFNGLXQUERYEXTENSIONSSTRINGPROC QueryExtensionsString; |
| 2787 | PFNGLXGETVISUALFROMFBCONFIGPROC GetVisualFromFBConfig; |
| 2788 | PFNGLXCREATEWINDOWPROC CreateWindow; |
| 2789 | PFNGLXDESTROYWINDOWPROC DestroyWindow; |
| 2790 | |
| 2791 | // GLX 1.4 and extension functions |
| 2792 | PFNGLXGETPROCADDRESSPROC GetProcAddress; |
| 2793 | PFNGLXGETPROCADDRESSPROC GetProcAddressARB; |
| 2794 | PFNGLXSWAPINTERVALEXTPROC SwapIntervalEXT; |
| 2795 | PFNGLXSWAPINTERVALMESAPROC SwapIntervalMESA; |
| 2796 | PFNGLXCREATECONTEXTATTRIBSARBPROC CreateContextAttribsARB; |
| 2797 | |
| 2798 | // special case glGetIntegerv |
| 2799 | void (*GetIntegerv)(uint32_t pname, int32_t* data); |
| 2800 | |
| 2801 | // extension availability |
| 2802 | bool EXT_swap_control; |
| 2803 | bool MESA_swap_control; |
| 2804 | bool ARB_multisample; |
| 2805 | bool ARB_create_context; |
| 2806 | bool ARB_create_context_profile; |
| 2807 | } _sapp_glx_t; |
| 2808 | |
| 2809 | #else |
| 2810 | |
| 2811 | typedef struct { |
| 2812 | EGLDisplay display; |
| 2813 | EGLContext context; |
| 2814 | EGLSurface surface; |
| 2815 | } _sapp_egl_t; |
| 2816 | |
| 2817 | #endif // _SAPP_GLX |
| 2818 | #endif // _SAPP_LINUX |
| 2819 | |
| 2820 | #if defined(_SAPP_ANY_GL) |
| 2821 | typedef struct { |
| 2822 | uint32_t framebuffer; |
| 2823 | } _sapp_gl_t; |
| 2824 | #endif |
| 2825 | |
| 2826 | typedef struct { |
| 2827 | bool enabled; |
| 2828 | int buf_size; |
| 2829 | char* buffer; |
| 2830 | } _sapp_clipboard_t; |
| 2831 | |
| 2832 | typedef struct { |
| 2833 | bool enabled; |
| 2834 | int max_files; |
| 2835 | int max_path_length; |
| 2836 | int num_files; |
| 2837 | int buf_size; |
| 2838 | char* buffer; |
| 2839 | } _sapp_drop_t; |
| 2840 | |
| 2841 | typedef struct { |
| 2842 | float x, y; |
| 2843 | float dx, dy; |
| 2844 | bool shown; |
| 2845 | bool locked; |
| 2846 | bool pos_valid; |
| 2847 | sapp_mouse_cursor current_cursor; |
| 2848 | } _sapp_mouse_t; |
| 2849 | |
| 2850 | typedef struct { |
| 2851 | sapp_desc desc; |
| 2852 | bool valid; |
| 2853 | bool fullscreen; |
| 2854 | bool first_frame; |
| 2855 | bool init_called; |
| 2856 | bool cleanup_called; |
| 2857 | bool quit_requested; |
| 2858 | bool quit_ordered; |
| 2859 | bool event_consumed; |
| 2860 | bool html5_ask_leave_site; |
| 2861 | bool onscreen_keyboard_shown; |
| 2862 | int window_width; |
| 2863 | int window_height; |
| 2864 | int framebuffer_width; |
| 2865 | int framebuffer_height; |
| 2866 | int sample_count; |
| 2867 | int swap_interval; |
| 2868 | float dpi_scale; |
| 2869 | uint64_t frame_count; |
| 2870 | _sapp_timing_t timing; |
| 2871 | sapp_event event; |
| 2872 | _sapp_mouse_t mouse; |
| 2873 | _sapp_clipboard_t clipboard; |
| 2874 | _sapp_drop_t drop; |
| 2875 | sapp_icon_desc default_icon_desc; |
| 2876 | uint32_t* default_icon_pixels; |
| 2877 | #if defined(_SAPP_MACOS) |
| 2878 | _sapp_macos_t macos; |
| 2879 | #elif defined(_SAPP_IOS) |
| 2880 | _sapp_ios_t ios; |
| 2881 | #elif defined(_SAPP_EMSCRIPTEN) |
| 2882 | _sapp_emsc_t emsc; |
| 2883 | #if defined(SOKOL_WGPU) |
| 2884 | _sapp_wgpu_t wgpu; |
| 2885 | #endif |
| 2886 | #elif defined(_SAPP_WIN32) |
| 2887 | _sapp_win32_t win32; |
| 2888 | #if defined(SOKOL_D3D11) |
| 2889 | _sapp_d3d11_t d3d11; |
| 2890 | #elif defined(SOKOL_GLCORE) |
| 2891 | _sapp_wgl_t wgl; |
| 2892 | #endif |
| 2893 | #elif defined(_SAPP_ANDROID) |
| 2894 | _sapp_android_t android; |
| 2895 | #elif defined(_SAPP_LINUX) |
| 2896 | _sapp_x11_t x11; |
| 2897 | #if defined(_SAPP_GLX) |
| 2898 | _sapp_glx_t glx; |
| 2899 | #else |
| 2900 | _sapp_egl_t egl; |
| 2901 | #endif |
| 2902 | #endif |
| 2903 | #if defined(_SAPP_ANY_GL) |
| 2904 | _sapp_gl_t gl; |
| 2905 | #endif |
| 2906 | char html5_canvas_selector[_SAPP_MAX_TITLE_LENGTH]; |
| 2907 | char window_title[_SAPP_MAX_TITLE_LENGTH]; // UTF-8 |
| 2908 | wchar_t window_title_wide[_SAPP_MAX_TITLE_LENGTH]; // UTF-32 or UCS-2 */ |
| 2909 | sapp_keycode keycodes[SAPP_MAX_KEYCODES]; |
| 2910 | } _sapp_t; |
| 2911 | static _sapp_t _sapp; |
| 2912 | |
| 2913 | // ██ ██████ ██████ ██████ ██ ███ ██ ██████ |
| 2914 | // ██ ██ ██ ██ ██ ██ ████ ██ ██ |
| 2915 | // ██ ██ ██ ██ ███ ██ ███ ██ ██ ██ ██ ██ ███ |
| 2916 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 2917 | // ███████ ██████ ██████ ██████ ██ ██ ████ ██████ |
| 2918 | // |
| 2919 | // >>logging |
| 2920 | #if defined(SOKOL_DEBUG) |
| 2921 | #define _SAPP_LOGITEM_XMACRO(item,msg) #item ": " msg, |
| 2922 | static const char* _sapp_log_messages[] = { |
| 2923 | _SAPP_LOG_ITEMS |
| 2924 | }; |
| 2925 | #undef _SAPP_LOGITEM_XMACRO |
| 2926 | #endif // SOKOL_DEBUG |
| 2927 | |
| 2928 | #define _SAPP_PANIC(code) _sapp_log(SAPP_LOGITEM_ ##code, 0, 0, __LINE__) |
| 2929 | #define _SAPP_ERROR(code) _sapp_log(SAPP_LOGITEM_ ##code, 1, 0, __LINE__) |
| 2930 | #define _SAPP_WARN(code) _sapp_log(SAPP_LOGITEM_ ##code, 2, 0, __LINE__) |
| 2931 | #define _SAPP_INFO(code) _sapp_log(SAPP_LOGITEM_ ##code, 3, 0, __LINE__) |
| 2932 | |
| 2933 | static void _sapp_log(sapp_log_item log_item, uint32_t log_level, const char* msg, uint32_t line_nr) { |
| 2934 | if (_sapp.desc.logger.func) { |
| 2935 | const char* filename = 0; |
| 2936 | #if defined(SOKOL_DEBUG) |
| 2937 | filename = __FILE__; |
| 2938 | if (0 == msg) { |
| 2939 | msg = _sapp_log_messages[log_item]; |
| 2940 | } |
| 2941 | #endif |
| 2942 | _sapp.desc.logger.func("sapp", log_level, log_item, msg, line_nr, filename, _sapp.desc.logger.user_data); |
| 2943 | } |
| 2944 | else { |
| 2945 | // for log level PANIC it would be 'undefined behaviour' to continue |
| 2946 | if (log_level == 0) { |
| 2947 | abort(); |
| 2948 | } |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | // ███ ███ ███████ ███ ███ ██████ ██████ ██ ██ |
| 2953 | // ████ ████ ██ ████ ████ ██ ██ ██ ██ ██ ██ |
| 2954 | // ██ ████ ██ █████ ██ ████ ██ ██ ██ ██████ ████ |
| 2955 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 2956 | // ██ ██ ███████ ██ ██ ██████ ██ ██ ██ |
| 2957 | // |
| 2958 | // >>memory |
| 2959 | _SOKOL_PRIVATE void _sapp_clear(void* ptr, size_t size) { |
| 2960 | SOKOL_ASSERT(ptr && (size > 0)); |
| 2961 | memset(ptr, 0, size); |
| 2962 | } |
| 2963 | |
| 2964 | _SOKOL_PRIVATE void* _sapp_malloc(size_t size) { |
| 2965 | SOKOL_ASSERT(size > 0); |
| 2966 | void* ptr; |
| 2967 | if (_sapp.desc.allocator.alloc_fn) { |
| 2968 | ptr = _sapp.desc.allocator.alloc_fn(size, _sapp.desc.allocator.user_data); |
| 2969 | } else { |
| 2970 | ptr = malloc(size); |
| 2971 | } |
| 2972 | if (0 == ptr) { |
| 2973 | _SAPP_PANIC(MALLOC_FAILED); |
| 2974 | } |
| 2975 | return ptr; |
| 2976 | } |
| 2977 | |
| 2978 | _SOKOL_PRIVATE void* _sapp_malloc_clear(size_t size) { |
| 2979 | void* ptr = _sapp_malloc(size); |
| 2980 | _sapp_clear(ptr, size); |
| 2981 | return ptr; |
| 2982 | } |
| 2983 | |
| 2984 | _SOKOL_PRIVATE void _sapp_free(void* ptr) { |
| 2985 | if (_sapp.desc.allocator.free_fn) { |
| 2986 | _sapp.desc.allocator.free_fn(ptr, _sapp.desc.allocator.user_data); |
| 2987 | } |
| 2988 | else { |
| 2989 | free(ptr); |
| 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | // ██ ██ ███████ ██ ██████ ███████ ██████ ███████ |
| 2994 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 2995 | // ███████ █████ ██ ██████ █████ ██████ ███████ |
| 2996 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 2997 | // ██ ██ ███████ ███████ ██ ███████ ██ ██ ███████ |
| 2998 | // |
| 2999 | // >>helpers |
| 3000 | _SOKOL_PRIVATE void _sapp_call_init(void) { |
| 3001 | if (_sapp.desc.init_cb) { |
| 3002 | _sapp.desc.init_cb(); |
| 3003 | } |
| 3004 | else if (_sapp.desc.init_userdata_cb) { |
| 3005 | _sapp.desc.init_userdata_cb(_sapp.desc.user_data); |
| 3006 | } |
| 3007 | _sapp.init_called = true; |
| 3008 | } |
| 3009 | |
| 3010 | _SOKOL_PRIVATE void _sapp_call_frame(void) { |
| 3011 | if (_sapp.init_called && !_sapp.cleanup_called) { |
| 3012 | if (_sapp.desc.frame_cb) { |
| 3013 | _sapp.desc.frame_cb(); |
| 3014 | } |
| 3015 | else if (_sapp.desc.frame_userdata_cb) { |
| 3016 | _sapp.desc.frame_userdata_cb(_sapp.desc.user_data); |
| 3017 | } |
| 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | _SOKOL_PRIVATE void _sapp_call_cleanup(void) { |
| 3022 | if (!_sapp.cleanup_called) { |
| 3023 | if (_sapp.desc.cleanup_cb) { |
| 3024 | _sapp.desc.cleanup_cb(); |
| 3025 | } |
| 3026 | else if (_sapp.desc.cleanup_userdata_cb) { |
| 3027 | _sapp.desc.cleanup_userdata_cb(_sapp.desc.user_data); |
| 3028 | } |
| 3029 | _sapp.cleanup_called = true; |
| 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | _SOKOL_PRIVATE bool _sapp_call_event(const sapp_event* e) { |
| 3034 | if (!_sapp.cleanup_called) { |
| 3035 | if (_sapp.desc.event_cb) { |
| 3036 | _sapp.desc.event_cb(e); |
| 3037 | } |
| 3038 | else if (_sapp.desc.event_userdata_cb) { |
| 3039 | _sapp.desc.event_userdata_cb(e, _sapp.desc.user_data); |
| 3040 | } |
| 3041 | } |
| 3042 | if (_sapp.event_consumed) { |
| 3043 | _sapp.event_consumed = false; |
| 3044 | return true; |
| 3045 | } |
| 3046 | else { |
| 3047 | return false; |
| 3048 | } |
| 3049 | } |
| 3050 | |
| 3051 | _SOKOL_PRIVATE char* _sapp_dropped_file_path_ptr(int index) { |
| 3052 | SOKOL_ASSERT(_sapp.drop.buffer); |
| 3053 | SOKOL_ASSERT((index >= 0) && (index <= _sapp.drop.max_files)); |
| 3054 | int offset = index * _sapp.drop.max_path_length; |
| 3055 | SOKOL_ASSERT(offset < _sapp.drop.buf_size); |
| 3056 | return &_sapp.drop.buffer[offset]; |
| 3057 | } |
| 3058 | |
| 3059 | /* Copy a string into a fixed size buffer with guaranteed zero- |
| 3060 | termination. |
| 3061 | |
| 3062 | Return false if the string didn't fit into the buffer and had to be clamped. |
| 3063 | |
| 3064 | FIXME: Currently UTF-8 strings might become invalid if the string |
| 3065 | is clamped, because the last zero-byte might be written into |
| 3066 | the middle of a multi-byte sequence. |
| 3067 | */ |
| 3068 | _SOKOL_PRIVATE bool _sapp_strcpy(const char* src, char* dst, int max_len) { |
| 3069 | SOKOL_ASSERT(src && dst && (max_len > 0)); |
| 3070 | char* const end = &(dst[max_len-1]); |
| 3071 | char c = 0; |
| 3072 | for (int i = 0; i < max_len; i++) { |
| 3073 | c = *src; |
| 3074 | if (c != 0) { |
| 3075 | src++; |
| 3076 | } |
| 3077 | *dst++ = c; |
| 3078 | } |
| 3079 | /* truncated? */ |
| 3080 | if (c != 0) { |
| 3081 | *end = 0; |
| 3082 | return false; |
| 3083 | } |
| 3084 | else { |
| 3085 | return true; |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | _SOKOL_PRIVATE sapp_desc _sapp_desc_defaults(const sapp_desc* desc) { |
| 3090 | SOKOL_ASSERT((desc->allocator.alloc_fn && desc->allocator.free_fn) || (!desc->allocator.alloc_fn && !desc->allocator.free_fn)); |
| 3091 | sapp_desc res = *desc; |
| 3092 | res.sample_count = _sapp_def(res.sample_count, 1); |
| 3093 | res.swap_interval = _sapp_def(res.swap_interval, 1); |
| 3094 | // NOTE: can't patch the default for gl_major_version and gl_minor_version |
| 3095 | // independently, because a desired version 4.0 would be patched to 4.2 |
| 3096 | // (or expressed differently: zero is a valid value for gl_minor_version |
| 3097 | // and can't be used to indicate 'default') |
| 3098 | if (0 == res.gl_major_version) { |
| 3099 | #if defined(_SAPP_APPLE) |
| 3100 | res.gl_major_version = 4; |
| 3101 | res.gl_minor_version = 1; |
| 3102 | #else |
| 3103 | res.gl_major_version = 4; |
| 3104 | res.gl_minor_version = 3; |
| 3105 | #endif |
| 3106 | } |
| 3107 | res.html5_canvas_name = _sapp_def(res.html5_canvas_name, "canvas"); |
| 3108 | res.clipboard_size = _sapp_def(res.clipboard_size, 8192); |
| 3109 | res.max_dropped_files = _sapp_def(res.max_dropped_files, 1); |
| 3110 | res.max_dropped_file_path_length = _sapp_def(res.max_dropped_file_path_length, 2048); |
| 3111 | res.window_title = _sapp_def(res.window_title, "sokol_app"); |
| 3112 | return res; |
| 3113 | } |
| 3114 | |
| 3115 | _SOKOL_PRIVATE void _sapp_init_state(const sapp_desc* desc) { |
| 3116 | SOKOL_ASSERT(desc); |
| 3117 | SOKOL_ASSERT(desc->width >= 0); |
| 3118 | SOKOL_ASSERT(desc->height >= 0); |
| 3119 | SOKOL_ASSERT(desc->sample_count >= 0); |
| 3120 | SOKOL_ASSERT(desc->swap_interval >= 0); |
| 3121 | SOKOL_ASSERT(desc->clipboard_size >= 0); |
| 3122 | SOKOL_ASSERT(desc->max_dropped_files >= 0); |
| 3123 | SOKOL_ASSERT(desc->max_dropped_file_path_length >= 0); |
| 3124 | _SAPP_CLEAR_ARC_STRUCT(_sapp_t, _sapp); |
| 3125 | _sapp.desc = _sapp_desc_defaults(desc); |
| 3126 | _sapp.first_frame = true; |
| 3127 | // NOTE: _sapp.desc.width/height may be 0! Platform backends need to deal with this |
| 3128 | _sapp.window_width = _sapp.desc.width; |
| 3129 | _sapp.window_height = _sapp.desc.height; |
| 3130 | _sapp.framebuffer_width = _sapp.window_width; |
| 3131 | _sapp.framebuffer_height = _sapp.window_height; |
| 3132 | _sapp.sample_count = _sapp.desc.sample_count; |
| 3133 | _sapp.swap_interval = _sapp.desc.swap_interval; |
| 3134 | _sapp.html5_canvas_selector[0] = '#'; |
| 3135 | _sapp_strcpy(_sapp.desc.html5_canvas_name, &_sapp.html5_canvas_selector[1], sizeof(_sapp.html5_canvas_selector) - 1); |
| 3136 | _sapp.desc.html5_canvas_name = &_sapp.html5_canvas_selector[1]; |
| 3137 | _sapp.html5_ask_leave_site = _sapp.desc.html5_ask_leave_site; |
| 3138 | _sapp.clipboard.enabled = _sapp.desc.enable_clipboard; |
| 3139 | if (_sapp.clipboard.enabled) { |
| 3140 | _sapp.clipboard.buf_size = _sapp.desc.clipboard_size; |
| 3141 | _sapp.clipboard.buffer = (char*) _sapp_malloc_clear((size_t)_sapp.clipboard.buf_size); |
| 3142 | } |
| 3143 | _sapp.drop.enabled = _sapp.desc.enable_dragndrop; |
| 3144 | if (_sapp.drop.enabled) { |
| 3145 | _sapp.drop.max_files = _sapp.desc.max_dropped_files; |
| 3146 | _sapp.drop.max_path_length = _sapp.desc.max_dropped_file_path_length; |
| 3147 | _sapp.drop.buf_size = _sapp.drop.max_files * _sapp.drop.max_path_length; |
| 3148 | _sapp.drop.buffer = (char*) _sapp_malloc_clear((size_t)_sapp.drop.buf_size); |
| 3149 | } |
| 3150 | _sapp_strcpy(_sapp.desc.window_title, _sapp.window_title, sizeof(_sapp.window_title)); |
| 3151 | _sapp.desc.window_title = _sapp.window_title; |
| 3152 | _sapp.dpi_scale = 1.0f; |
| 3153 | _sapp.fullscreen = _sapp.desc.fullscreen; |
| 3154 | _sapp.mouse.shown = true; |
| 3155 | _sapp_timing_init(&_sapp.timing); |
| 3156 | } |
| 3157 | |
| 3158 | _SOKOL_PRIVATE void _sapp_discard_state(void) { |
| 3159 | if (_sapp.clipboard.enabled) { |
| 3160 | SOKOL_ASSERT(_sapp.clipboard.buffer); |
| 3161 | _sapp_free((void*)_sapp.clipboard.buffer); |
| 3162 | } |
| 3163 | if (_sapp.drop.enabled) { |
| 3164 | SOKOL_ASSERT(_sapp.drop.buffer); |
| 3165 | _sapp_free((void*)_sapp.drop.buffer); |
| 3166 | } |
| 3167 | if (_sapp.default_icon_pixels) { |
| 3168 | _sapp_free((void*)_sapp.default_icon_pixels); |
| 3169 | } |
| 3170 | _SAPP_CLEAR_ARC_STRUCT(_sapp_t, _sapp); |
| 3171 | } |
| 3172 | |
| 3173 | _SOKOL_PRIVATE void _sapp_init_event(sapp_event_type type) { |
| 3174 | _sapp_clear(&_sapp.event, sizeof(_sapp.event)); |
| 3175 | _sapp.event.type = type; |
| 3176 | _sapp.event.frame_count = _sapp.frame_count; |
| 3177 | _sapp.event.mouse_button = SAPP_MOUSEBUTTON_INVALID; |
| 3178 | _sapp.event.window_width = _sapp.window_width; |
| 3179 | _sapp.event.window_height = _sapp.window_height; |
| 3180 | _sapp.event.framebuffer_width = _sapp.framebuffer_width; |
| 3181 | _sapp.event.framebuffer_height = _sapp.framebuffer_height; |
| 3182 | _sapp.event.mouse_x = _sapp.mouse.x; |
| 3183 | _sapp.event.mouse_y = _sapp.mouse.y; |
| 3184 | _sapp.event.mouse_dx = _sapp.mouse.dx; |
| 3185 | _sapp.event.mouse_dy = _sapp.mouse.dy; |
| 3186 | } |
| 3187 | |
| 3188 | _SOKOL_PRIVATE bool _sapp_events_enabled(void) { |
| 3189 | /* only send events when an event callback is set, and the init function was called */ |
| 3190 | return (_sapp.desc.event_cb || _sapp.desc.event_userdata_cb) && _sapp.init_called; |
| 3191 | } |
| 3192 | |
| 3193 | _SOKOL_PRIVATE sapp_keycode _sapp_translate_key(int scan_code) { |
| 3194 | if ((scan_code >= 0) && (scan_code < SAPP_MAX_KEYCODES)) { |
| 3195 | return _sapp.keycodes[scan_code]; |
| 3196 | } |
| 3197 | else { |
| 3198 | return SAPP_KEYCODE_INVALID; |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | _SOKOL_PRIVATE void _sapp_clear_drop_buffer(void) { |
| 3203 | if (_sapp.drop.enabled) { |
| 3204 | SOKOL_ASSERT(_sapp.drop.buffer); |
| 3205 | _sapp_clear(_sapp.drop.buffer, (size_t)_sapp.drop.buf_size); |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | _SOKOL_PRIVATE void _sapp_frame(void) { |
| 3210 | if (_sapp.first_frame) { |
| 3211 | _sapp.first_frame = false; |
| 3212 | _sapp_call_init(); |
| 3213 | } |
| 3214 | _sapp_call_frame(); |
| 3215 | _sapp.frame_count++; |
| 3216 | } |
| 3217 | |
| 3218 | _SOKOL_PRIVATE bool _sapp_image_validate(const sapp_image_desc* desc) { |
| 3219 | SOKOL_ASSERT(desc->width > 0); |
| 3220 | SOKOL_ASSERT(desc->height > 0); |
| 3221 | SOKOL_ASSERT(desc->pixels.ptr != 0); |
| 3222 | SOKOL_ASSERT(desc->pixels.size > 0); |
| 3223 | const size_t wh_size = (size_t)(desc->width * desc->height) * sizeof(uint32_t); |
| 3224 | if (wh_size != desc->pixels.size) { |
| 3225 | _SAPP_ERROR(IMAGE_DATA_SIZE_MISMATCH); |
| 3226 | return false; |
| 3227 | } |
| 3228 | return true; |
| 3229 | } |
| 3230 | |
| 3231 | _SOKOL_PRIVATE int _sapp_image_bestmatch(const sapp_image_desc image_descs[], int num_images, int width, int height) { |
| 3232 | int least_diff = 0x7FFFFFFF; |
| 3233 | int least_index = 0; |
| 3234 | for (int i = 0; i < num_images; i++) { |
| 3235 | int diff = (image_descs[i].width * image_descs[i].height) - (width * height); |
| 3236 | if (diff < 0) { |
| 3237 | diff = -diff; |
| 3238 | } |
| 3239 | if (diff < least_diff) { |
| 3240 | least_diff = diff; |
| 3241 | least_index = i; |
| 3242 | } |
| 3243 | } |
| 3244 | return least_index; |
| 3245 | } |
| 3246 | |
| 3247 | _SOKOL_PRIVATE int _sapp_icon_num_images(const sapp_icon_desc* desc) { |
| 3248 | int index = 0; |
| 3249 | for (; index < SAPP_MAX_ICONIMAGES; index++) { |
| 3250 | if (0 == desc->images[index].pixels.ptr) { |
| 3251 | break; |
| 3252 | } |
| 3253 | } |
| 3254 | return index; |
| 3255 | } |
| 3256 | |
| 3257 | _SOKOL_PRIVATE bool _sapp_validate_icon_desc(const sapp_icon_desc* desc, int num_images) { |
| 3258 | SOKOL_ASSERT(num_images <= SAPP_MAX_ICONIMAGES); |
| 3259 | for (int i = 0; i < num_images; i++) { |
| 3260 | const sapp_image_desc* img_desc = &desc->images[i]; |
| 3261 | if (!_sapp_image_validate(img_desc)) { |
| 3262 | return false; |
| 3263 | } |
| 3264 | } |
| 3265 | return true; |
| 3266 | } |
| 3267 | |
| 3268 | _SOKOL_PRIVATE void _sapp_setup_default_icon(void) { |
| 3269 | SOKOL_ASSERT(0 == _sapp.default_icon_pixels); |
| 3270 | |
| 3271 | const int num_icons = 3; |
| 3272 | const int icon_sizes[3] = { 16, 32, 64 }; // must be multiple of 8! |
| 3273 | |
| 3274 | // allocate a pixel buffer for all icon pixels |
| 3275 | int all_num_pixels = 0; |
| 3276 | for (int i = 0; i < num_icons; i++) { |
| 3277 | all_num_pixels += icon_sizes[i] * icon_sizes[i]; |
| 3278 | } |
| 3279 | _sapp.default_icon_pixels = (uint32_t*) _sapp_malloc_clear((size_t)all_num_pixels * sizeof(uint32_t)); |
| 3280 | |
| 3281 | // initialize default_icon_desc struct |
| 3282 | uint32_t* dst = _sapp.default_icon_pixels; |
| 3283 | const uint32_t* dst_end = dst + all_num_pixels; |
| 3284 | (void)dst_end; // silence unused warning in release mode |
| 3285 | for (int i = 0; i < num_icons; i++) { |
| 3286 | const int dim = (int) icon_sizes[i]; |
| 3287 | const int num_pixels = dim * dim; |
| 3288 | sapp_image_desc* img_desc = &_sapp.default_icon_desc.images[i]; |
| 3289 | img_desc->width = dim; |
| 3290 | img_desc->height = dim; |
| 3291 | img_desc->pixels.ptr = dst; |
| 3292 | img_desc->pixels.size = (size_t)num_pixels * sizeof(uint32_t); |
| 3293 | dst += num_pixels; |
| 3294 | } |
| 3295 | SOKOL_ASSERT(dst == dst_end); |
| 3296 | |
| 3297 | // Amstrad CPC font 'S' |
| 3298 | const uint8_t tile[8] = { |
| 3299 | 0x3C, |
| 3300 | 0x66, |
| 3301 | 0x60, |
| 3302 | 0x3C, |
| 3303 | 0x06, |
| 3304 | 0x66, |
| 3305 | 0x3C, |
| 3306 | 0x00, |
| 3307 | }; |
| 3308 | // rainbow colors |
| 3309 | const uint32_t colors[8] = { |
| 3310 | 0xFF4370FF, |
| 3311 | 0xFF26A7FF, |
| 3312 | 0xFF58EEFF, |
| 3313 | 0xFF57E1D4, |
| 3314 | 0xFF65CC9C, |
| 3315 | 0xFF6ABB66, |
| 3316 | 0xFFF5A542, |
| 3317 | 0xFFC2577E, |
| 3318 | }; |
| 3319 | dst = _sapp.default_icon_pixels; |
| 3320 | const uint32_t blank = 0x00FFFFFF; |
| 3321 | const uint32_t shadow = 0xFF000000; |
| 3322 | for (int i = 0; i < num_icons; i++) { |
| 3323 | const int dim = icon_sizes[i]; |
| 3324 | SOKOL_ASSERT((dim % 8) == 0); |
| 3325 | const int scale = dim / 8; |
| 3326 | for (int ty = 0, y = 0; ty < 8; ty++) { |
| 3327 | const uint32_t color = colors[ty]; |
| 3328 | for (int sy = 0; sy < scale; sy++, y++) { |
| 3329 | uint8_t bits = tile[ty]; |
| 3330 | for (int tx = 0, x = 0; tx < 8; tx++, bits<<=1) { |
| 3331 | uint32_t pixel = (0 == (bits & 0x80)) ? blank : color; |
| 3332 | for (int sx = 0; sx < scale; sx++, x++) { |
| 3333 | SOKOL_ASSERT(dst < dst_end); |
| 3334 | *dst++ = pixel; |
| 3335 | } |
| 3336 | } |
| 3337 | } |
| 3338 | } |
| 3339 | } |
| 3340 | SOKOL_ASSERT(dst == dst_end); |
| 3341 | |
| 3342 | // right shadow |
| 3343 | dst = _sapp.default_icon_pixels; |
| 3344 | for (int i = 0; i < num_icons; i++) { |
| 3345 | const int dim = icon_sizes[i]; |
| 3346 | for (int y = 0; y < dim; y++) { |
| 3347 | uint32_t prev_color = blank; |
| 3348 | for (int x = 0; x < dim; x++) { |
| 3349 | const int dst_index = y * dim + x; |
| 3350 | const uint32_t cur_color = dst[dst_index]; |
| 3351 | if ((cur_color == blank) && (prev_color != blank)) { |
| 3352 | dst[dst_index] = shadow; |
| 3353 | } |
| 3354 | prev_color = cur_color; |
| 3355 | } |
| 3356 | } |
| 3357 | dst += dim * dim; |
| 3358 | } |
| 3359 | SOKOL_ASSERT(dst == dst_end); |
| 3360 | |
| 3361 | // bottom shadow |
| 3362 | dst = _sapp.default_icon_pixels; |
| 3363 | for (int i = 0; i < num_icons; i++) { |
| 3364 | const int dim = icon_sizes[i]; |
| 3365 | for (int x = 0; x < dim; x++) { |
| 3366 | uint32_t prev_color = blank; |
| 3367 | for (int y = 0; y < dim; y++) { |
| 3368 | const int dst_index = y * dim + x; |
| 3369 | const uint32_t cur_color = dst[dst_index]; |
| 3370 | if ((cur_color == blank) && (prev_color != blank)) { |
| 3371 | dst[dst_index] = shadow; |
| 3372 | } |
| 3373 | prev_color = cur_color; |
| 3374 | } |
| 3375 | } |
| 3376 | dst += dim * dim; |
| 3377 | } |
| 3378 | SOKOL_ASSERT(dst == dst_end); |
| 3379 | } |
| 3380 | |
| 3381 | // █████ ██████ ██████ ██ ███████ |
| 3382 | // ██ ██ ██ ██ ██ ██ ██ ██ |
| 3383 | // ███████ ██████ ██████ ██ █████ |
| 3384 | // ██ ██ ██ ██ ██ ██ |
| 3385 | // ██ ██ ██ ██ ███████ ███████ |
| 3386 | // |
| 3387 | // >>apple |
| 3388 | #if defined(_SAPP_APPLE) |
| 3389 | |
| 3390 | #if __has_feature(objc_arc) |
| 3391 | #define _SAPP_OBJC_RELEASE(obj) { obj = nil; } |
| 3392 | #else |
| 3393 | #define _SAPP_OBJC_RELEASE(obj) { [obj release]; obj = nil; } |
| 3394 | #endif |
| 3395 | |
| 3396 | // ███ ███ █████ ██████ ██████ ███████ |
| 3397 | // ████ ████ ██ ██ ██ ██ ██ ██ |
| 3398 | // ██ ████ ██ ███████ ██ ██ ██ ███████ |
| 3399 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 3400 | // ██ ██ ██ ██ ██████ ██████ ███████ |
| 3401 | // |
| 3402 | // >>macos |
| 3403 | #if defined(_SAPP_MACOS) |
| 3404 | |
| 3405 | _SOKOL_PRIVATE void _sapp_macos_init_keytable(void) { |
| 3406 | _sapp.keycodes[0x1D] = SAPP_KEYCODE_0; |
| 3407 | _sapp.keycodes[0x12] = SAPP_KEYCODE_1; |
| 3408 | _sapp.keycodes[0x13] = SAPP_KEYCODE_2; |
| 3409 | _sapp.keycodes[0x14] = SAPP_KEYCODE_3; |
| 3410 | _sapp.keycodes[0x15] = SAPP_KEYCODE_4; |
| 3411 | _sapp.keycodes[0x17] = SAPP_KEYCODE_5; |
| 3412 | _sapp.keycodes[0x16] = SAPP_KEYCODE_6; |
| 3413 | _sapp.keycodes[0x1A] = SAPP_KEYCODE_7; |
| 3414 | _sapp.keycodes[0x1C] = SAPP_KEYCODE_8; |
| 3415 | _sapp.keycodes[0x19] = SAPP_KEYCODE_9; |
| 3416 | _sapp.keycodes[0x00] = SAPP_KEYCODE_A; |
| 3417 | _sapp.keycodes[0x0B] = SAPP_KEYCODE_B; |
| 3418 | _sapp.keycodes[0x08] = SAPP_KEYCODE_C; |
| 3419 | _sapp.keycodes[0x02] = SAPP_KEYCODE_D; |
| 3420 | _sapp.keycodes[0x0E] = SAPP_KEYCODE_E; |
| 3421 | _sapp.keycodes[0x03] = SAPP_KEYCODE_F; |
| 3422 | _sapp.keycodes[0x05] = SAPP_KEYCODE_G; |
| 3423 | _sapp.keycodes[0x04] = SAPP_KEYCODE_H; |
| 3424 | _sapp.keycodes[0x22] = SAPP_KEYCODE_I; |
| 3425 | _sapp.keycodes[0x26] = SAPP_KEYCODE_J; |
| 3426 | _sapp.keycodes[0x28] = SAPP_KEYCODE_K; |
| 3427 | _sapp.keycodes[0x25] = SAPP_KEYCODE_L; |
| 3428 | _sapp.keycodes[0x2E] = SAPP_KEYCODE_M; |
| 3429 | _sapp.keycodes[0x2D] = SAPP_KEYCODE_N; |
| 3430 | _sapp.keycodes[0x1F] = SAPP_KEYCODE_O; |
| 3431 | _sapp.keycodes[0x23] = SAPP_KEYCODE_P; |
| 3432 | _sapp.keycodes[0x0C] = SAPP_KEYCODE_Q; |
| 3433 | _sapp.keycodes[0x0F] = SAPP_KEYCODE_R; |
| 3434 | _sapp.keycodes[0x01] = SAPP_KEYCODE_S; |
| 3435 | _sapp.keycodes[0x11] = SAPP_KEYCODE_T; |
| 3436 | _sapp.keycodes[0x20] = SAPP_KEYCODE_U; |
| 3437 | _sapp.keycodes[0x09] = SAPP_KEYCODE_V; |
| 3438 | _sapp.keycodes[0x0D] = SAPP_KEYCODE_W; |
| 3439 | _sapp.keycodes[0x07] = SAPP_KEYCODE_X; |
| 3440 | _sapp.keycodes[0x10] = SAPP_KEYCODE_Y; |
| 3441 | _sapp.keycodes[0x06] = SAPP_KEYCODE_Z; |
| 3442 | _sapp.keycodes[0x27] = SAPP_KEYCODE_APOSTROPHE; |
| 3443 | _sapp.keycodes[0x2A] = SAPP_KEYCODE_BACKSLASH; |
| 3444 | _sapp.keycodes[0x2B] = SAPP_KEYCODE_COMMA; |
| 3445 | _sapp.keycodes[0x18] = SAPP_KEYCODE_EQUAL; |
| 3446 | _sapp.keycodes[0x32] = SAPP_KEYCODE_GRAVE_ACCENT; |
| 3447 | _sapp.keycodes[0x21] = SAPP_KEYCODE_LEFT_BRACKET; |
| 3448 | _sapp.keycodes[0x1B] = SAPP_KEYCODE_MINUS; |
| 3449 | _sapp.keycodes[0x2F] = SAPP_KEYCODE_PERIOD; |
| 3450 | _sapp.keycodes[0x1E] = SAPP_KEYCODE_RIGHT_BRACKET; |
| 3451 | _sapp.keycodes[0x29] = SAPP_KEYCODE_SEMICOLON; |
| 3452 | _sapp.keycodes[0x2C] = SAPP_KEYCODE_SLASH; |
| 3453 | _sapp.keycodes[0x0A] = SAPP_KEYCODE_WORLD_1; |
| 3454 | _sapp.keycodes[0x33] = SAPP_KEYCODE_BACKSPACE; |
| 3455 | _sapp.keycodes[0x39] = SAPP_KEYCODE_CAPS_LOCK; |
| 3456 | _sapp.keycodes[0x75] = SAPP_KEYCODE_DELETE; |
| 3457 | _sapp.keycodes[0x7D] = SAPP_KEYCODE_DOWN; |
| 3458 | _sapp.keycodes[0x77] = SAPP_KEYCODE_END; |
| 3459 | _sapp.keycodes[0x24] = SAPP_KEYCODE_ENTER; |
| 3460 | _sapp.keycodes[0x35] = SAPP_KEYCODE_ESCAPE; |
| 3461 | _sapp.keycodes[0x7A] = SAPP_KEYCODE_F1; |
| 3462 | _sapp.keycodes[0x78] = SAPP_KEYCODE_F2; |
| 3463 | _sapp.keycodes[0x63] = SAPP_KEYCODE_F3; |
| 3464 | _sapp.keycodes[0x76] = SAPP_KEYCODE_F4; |
| 3465 | _sapp.keycodes[0x60] = SAPP_KEYCODE_F5; |
| 3466 | _sapp.keycodes[0x61] = SAPP_KEYCODE_F6; |
| 3467 | _sapp.keycodes[0x62] = SAPP_KEYCODE_F7; |
| 3468 | _sapp.keycodes[0x64] = SAPP_KEYCODE_F8; |
| 3469 | _sapp.keycodes[0x65] = SAPP_KEYCODE_F9; |
| 3470 | _sapp.keycodes[0x6D] = SAPP_KEYCODE_F10; |
| 3471 | _sapp.keycodes[0x67] = SAPP_KEYCODE_F11; |
| 3472 | _sapp.keycodes[0x6F] = SAPP_KEYCODE_F12; |
| 3473 | _sapp.keycodes[0x69] = SAPP_KEYCODE_F13; |
| 3474 | _sapp.keycodes[0x6B] = SAPP_KEYCODE_F14; |
| 3475 | _sapp.keycodes[0x71] = SAPP_KEYCODE_F15; |
| 3476 | _sapp.keycodes[0x6A] = SAPP_KEYCODE_F16; |
| 3477 | _sapp.keycodes[0x40] = SAPP_KEYCODE_F17; |
| 3478 | _sapp.keycodes[0x4F] = SAPP_KEYCODE_F18; |
| 3479 | _sapp.keycodes[0x50] = SAPP_KEYCODE_F19; |
| 3480 | _sapp.keycodes[0x5A] = SAPP_KEYCODE_F20; |
| 3481 | _sapp.keycodes[0x73] = SAPP_KEYCODE_HOME; |
| 3482 | _sapp.keycodes[0x72] = SAPP_KEYCODE_INSERT; |
| 3483 | _sapp.keycodes[0x7B] = SAPP_KEYCODE_LEFT; |
| 3484 | _sapp.keycodes[0x3A] = SAPP_KEYCODE_LEFT_ALT; |
| 3485 | _sapp.keycodes[0x3B] = SAPP_KEYCODE_LEFT_CONTROL; |
| 3486 | _sapp.keycodes[0x38] = SAPP_KEYCODE_LEFT_SHIFT; |
| 3487 | _sapp.keycodes[0x37] = SAPP_KEYCODE_LEFT_SUPER; |
| 3488 | _sapp.keycodes[0x6E] = SAPP_KEYCODE_MENU; |
| 3489 | _sapp.keycodes[0x47] = SAPP_KEYCODE_NUM_LOCK; |
| 3490 | _sapp.keycodes[0x79] = SAPP_KEYCODE_PAGE_DOWN; |
| 3491 | _sapp.keycodes[0x74] = SAPP_KEYCODE_PAGE_UP; |
| 3492 | _sapp.keycodes[0x7C] = SAPP_KEYCODE_RIGHT; |
| 3493 | _sapp.keycodes[0x3D] = SAPP_KEYCODE_RIGHT_ALT; |
| 3494 | _sapp.keycodes[0x3E] = SAPP_KEYCODE_RIGHT_CONTROL; |
| 3495 | _sapp.keycodes[0x3C] = SAPP_KEYCODE_RIGHT_SHIFT; |
| 3496 | _sapp.keycodes[0x36] = SAPP_KEYCODE_RIGHT_SUPER; |
| 3497 | _sapp.keycodes[0x31] = SAPP_KEYCODE_SPACE; |
| 3498 | _sapp.keycodes[0x30] = SAPP_KEYCODE_TAB; |
| 3499 | _sapp.keycodes[0x7E] = SAPP_KEYCODE_UP; |
| 3500 | _sapp.keycodes[0x52] = SAPP_KEYCODE_KP_0; |
| 3501 | _sapp.keycodes[0x53] = SAPP_KEYCODE_KP_1; |
| 3502 | _sapp.keycodes[0x54] = SAPP_KEYCODE_KP_2; |
| 3503 | _sapp.keycodes[0x55] = SAPP_KEYCODE_KP_3; |
| 3504 | _sapp.keycodes[0x56] = SAPP_KEYCODE_KP_4; |
| 3505 | _sapp.keycodes[0x57] = SAPP_KEYCODE_KP_5; |
| 3506 | _sapp.keycodes[0x58] = SAPP_KEYCODE_KP_6; |
| 3507 | _sapp.keycodes[0x59] = SAPP_KEYCODE_KP_7; |
| 3508 | _sapp.keycodes[0x5B] = SAPP_KEYCODE_KP_8; |
| 3509 | _sapp.keycodes[0x5C] = SAPP_KEYCODE_KP_9; |
| 3510 | _sapp.keycodes[0x45] = SAPP_KEYCODE_KP_ADD; |
| 3511 | _sapp.keycodes[0x41] = SAPP_KEYCODE_KP_DECIMAL; |
| 3512 | _sapp.keycodes[0x4B] = SAPP_KEYCODE_KP_DIVIDE; |
| 3513 | _sapp.keycodes[0x4C] = SAPP_KEYCODE_KP_ENTER; |
| 3514 | _sapp.keycodes[0x51] = SAPP_KEYCODE_KP_EQUAL; |
| 3515 | _sapp.keycodes[0x43] = SAPP_KEYCODE_KP_MULTIPLY; |
| 3516 | _sapp.keycodes[0x4E] = SAPP_KEYCODE_KP_SUBTRACT; |
| 3517 | } |
| 3518 | |
| 3519 | _SOKOL_PRIVATE void _sapp_macos_discard_state(void) { |
| 3520 | // NOTE: it's safe to call [release] on a nil object |
| 3521 | if (_sapp.macos.keyup_monitor != nil) { |
| 3522 | [NSEvent removeMonitor:_sapp.macos.keyup_monitor]; |
| 3523 | // NOTE: removeMonitor also releases the object |
| 3524 | _sapp.macos.keyup_monitor = nil; |
| 3525 | } |
| 3526 | _SAPP_OBJC_RELEASE(_sapp.macos.tracking_area); |
| 3527 | _SAPP_OBJC_RELEASE(_sapp.macos.app_dlg); |
| 3528 | _SAPP_OBJC_RELEASE(_sapp.macos.win_dlg); |
| 3529 | _SAPP_OBJC_RELEASE(_sapp.macos.view); |
| 3530 | #if defined(SOKOL_METAL) |
| 3531 | _SAPP_OBJC_RELEASE(_sapp.macos.mtl_device); |
| 3532 | #endif |
| 3533 | _SAPP_OBJC_RELEASE(_sapp.macos.window); |
| 3534 | } |
| 3535 | |
| 3536 | // undocumented methods for creating cursors (see GLFW 3.4 and imgui_impl_osx.mm) |
| 3537 | @interface NSCursor() |
| 3538 | + (id)_windowResizeNorthWestSouthEastCursor; |
| 3539 | + (id)_windowResizeNorthEastSouthWestCursor; |
| 3540 | + (id)_windowResizeNorthSouthCursor; |
| 3541 | + (id)_windowResizeEastWestCursor; |
| 3542 | @end |
| 3543 | |
| 3544 | _SOKOL_PRIVATE void _sapp_macos_init_cursors(void) { |
| 3545 | _sapp.macos.cursors[SAPP_MOUSECURSOR_DEFAULT] = nil; // not a bug |
| 3546 | _sapp.macos.cursors[SAPP_MOUSECURSOR_ARROW] = [NSCursor arrowCursor]; |
| 3547 | _sapp.macos.cursors[SAPP_MOUSECURSOR_IBEAM] = [NSCursor IBeamCursor]; |
| 3548 | _sapp.macos.cursors[SAPP_MOUSECURSOR_CROSSHAIR] = [NSCursor crosshairCursor]; |
| 3549 | _sapp.macos.cursors[SAPP_MOUSECURSOR_POINTING_HAND] = [NSCursor pointingHandCursor]; |
| 3550 | _sapp.macos.cursors[SAPP_MOUSECURSOR_RESIZE_EW] = [NSCursor respondsToSelector:@selector(_windowResizeEastWestCursor)] ? [NSCursor _windowResizeEastWestCursor] : [NSCursor resizeLeftRightCursor]; |
| 3551 | _sapp.macos.cursors[SAPP_MOUSECURSOR_RESIZE_NS] = [NSCursor respondsToSelector:@selector(_windowResizeNorthSouthCursor)] ? [NSCursor _windowResizeNorthSouthCursor] : [NSCursor resizeUpDownCursor]; |
| 3552 | _sapp.macos.cursors[SAPP_MOUSECURSOR_RESIZE_NWSE] = [NSCursor respondsToSelector:@selector(_windowResizeNorthWestSouthEastCursor)] ? [NSCursor _windowResizeNorthWestSouthEastCursor] : [NSCursor closedHandCursor]; |
| 3553 | _sapp.macos.cursors[SAPP_MOUSECURSOR_RESIZE_NESW] = [NSCursor respondsToSelector:@selector(_windowResizeNorthEastSouthWestCursor)] ? [NSCursor _windowResizeNorthEastSouthWestCursor] : [NSCursor closedHandCursor]; |
| 3554 | _sapp.macos.cursors[SAPP_MOUSECURSOR_RESIZE_ALL] = [NSCursor closedHandCursor]; |
| 3555 | _sapp.macos.cursors[SAPP_MOUSECURSOR_NOT_ALLOWED] = [NSCursor operationNotAllowedCursor]; |
| 3556 | } |
| 3557 | |
| 3558 | _SOKOL_PRIVATE void _sapp_macos_run(const sapp_desc* desc) { |
| 3559 | _sapp_init_state(desc); |
| 3560 | _sapp_macos_init_keytable(); |
| 3561 | [NSApplication sharedApplication]; |
| 3562 | |
| 3563 | // set the application dock icon as early as possible, otherwise |
| 3564 | // the dummy icon will be visible for a short time |
| 3565 | sapp_set_icon(&_sapp.desc.icon); |
| 3566 | _sapp.macos.app_dlg = [[_sapp_macos_app_delegate alloc] init]; |
| 3567 | NSApp.delegate = _sapp.macos.app_dlg; |
| 3568 | |
| 3569 | // workaround for "no key-up sent while Cmd is pressed" taken from GLFW: |
| 3570 | NSEvent* (^keyup_monitor)(NSEvent*) = ^NSEvent* (NSEvent* event) { |
| 3571 | if ([event modifierFlags] & NSEventModifierFlagCommand) { |
| 3572 | [[NSApp keyWindow] sendEvent:event]; |
| 3573 | } |
| 3574 | return event; |
| 3575 | }; |
| 3576 | _sapp.macos.keyup_monitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyUp handler:keyup_monitor]; |
| 3577 | |
| 3578 | [NSApp run]; |
| 3579 | // NOTE: [NSApp run] never returns, instead cleanup code |
| 3580 | // must be put into applicationWillTerminate |
| 3581 | } |
| 3582 | |
| 3583 | /* MacOS entry function */ |
| 3584 | #if !defined(SOKOL_NO_ENTRY) |
| 3585 | int main(int argc, char* argv[]) { |
| 3586 | sapp_desc desc = sokol_main(argc, argv); |
| 3587 | _sapp_macos_run(&desc); |
| 3588 | return 0; |
| 3589 | } |
| 3590 | #endif /* SOKOL_NO_ENTRY */ |
| 3591 | |
| 3592 | _SOKOL_PRIVATE uint32_t _sapp_macos_mods(NSEvent* ev) { |
| 3593 | const NSEventModifierFlags f = (ev == nil) ? NSEvent.modifierFlags : ev.modifierFlags; |
| 3594 | const NSUInteger b = NSEvent.pressedMouseButtons; |
| 3595 | uint32_t m = 0; |
| 3596 | if (f & NSEventModifierFlagShift) { |
| 3597 | m |= SAPP_MODIFIER_SHIFT; |
| 3598 | } |
| 3599 | if (f & NSEventModifierFlagControl) { |
| 3600 | m |= SAPP_MODIFIER_CTRL; |
| 3601 | } |
| 3602 | if (f & NSEventModifierFlagOption) { |
| 3603 | m |= SAPP_MODIFIER_ALT; |
| 3604 | } |
| 3605 | if (f & NSEventModifierFlagCommand) { |
| 3606 | m |= SAPP_MODIFIER_SUPER; |
| 3607 | } |
| 3608 | if (0 != (b & (1<<0))) { |
| 3609 | m |= SAPP_MODIFIER_LMB; |
| 3610 | } |
| 3611 | if (0 != (b & (1<<1))) { |
| 3612 | m |= SAPP_MODIFIER_RMB; |
| 3613 | } |
| 3614 | if (0 != (b & (1<<2))) { |
| 3615 | m |= SAPP_MODIFIER_MMB; |
| 3616 | } |
| 3617 | return m; |
| 3618 | } |
| 3619 | |
| 3620 | _SOKOL_PRIVATE void _sapp_macos_mouse_event(sapp_event_type type, sapp_mousebutton btn, uint32_t mod) { |
| 3621 | if (_sapp_events_enabled()) { |
| 3622 | _sapp_init_event(type); |
| 3623 | _sapp.event.mouse_button = btn; |
| 3624 | _sapp.event.modifiers = mod; |
| 3625 | _sapp_call_event(&_sapp.event); |
| 3626 | } |
| 3627 | } |
| 3628 | |
| 3629 | _SOKOL_PRIVATE void _sapp_macos_key_event(sapp_event_type type, sapp_keycode key, bool repeat, uint32_t mod) { |
| 3630 | if (_sapp_events_enabled()) { |
| 3631 | _sapp_init_event(type); |
| 3632 | _sapp.event.key_code = key; |
| 3633 | _sapp.event.key_repeat = repeat; |
| 3634 | _sapp.event.modifiers = mod; |
| 3635 | _sapp_call_event(&_sapp.event); |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | _SOKOL_PRIVATE void _sapp_macos_app_event(sapp_event_type type) { |
| 3640 | if (_sapp_events_enabled()) { |
| 3641 | _sapp_init_event(type); |
| 3642 | _sapp_call_event(&_sapp.event); |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | /* NOTE: unlike the iOS version of this function, the macOS version |
| 3647 | can dynamically update the DPI scaling factor when a window is moved |
| 3648 | between HighDPI / LowDPI screens. |
| 3649 | */ |
| 3650 | _SOKOL_PRIVATE void _sapp_macos_update_dimensions(void) { |
| 3651 | if (_sapp.desc.high_dpi) { |
| 3652 | _sapp.dpi_scale = [_sapp.macos.window screen].backingScaleFactor; |
| 3653 | } |
| 3654 | else { |
| 3655 | _sapp.dpi_scale = 1.0f; |
| 3656 | } |
| 3657 | _sapp.macos.view.layer.contentsScale = _sapp.dpi_scale; // NOTE: needed because we set layerContentsPlacement to a non-scaling value in windowWillStartLiveResize. |
| 3658 | const NSRect bounds = [_sapp.macos.view bounds]; |
| 3659 | _sapp.window_width = (int)roundf(bounds.size.width); |
| 3660 | _sapp.window_height = (int)roundf(bounds.size.height); |
| 3661 | #if defined(SOKOL_METAL) |
| 3662 | _sapp.framebuffer_width = (int)roundf(bounds.size.width * _sapp.dpi_scale); |
| 3663 | _sapp.framebuffer_height = (int)roundf(bounds.size.height * _sapp.dpi_scale); |
| 3664 | const CGSize fb_size = _sapp.macos.view.drawableSize; |
| 3665 | const int cur_fb_width = (int)roundf(fb_size.width); |
| 3666 | const int cur_fb_height = (int)roundf(fb_size.height); |
| 3667 | const bool dim_changed = (_sapp.framebuffer_width != cur_fb_width) || |
| 3668 | (_sapp.framebuffer_height != cur_fb_height); |
| 3669 | #elif defined(SOKOL_GLCORE) |
| 3670 | const int cur_fb_width = (int)roundf(bounds.size.width * _sapp.dpi_scale); |
| 3671 | const int cur_fb_height = (int)roundf(bounds.size.height * _sapp.dpi_scale); |
| 3672 | const bool dim_changed = (_sapp.framebuffer_width != cur_fb_width) || |
| 3673 | (_sapp.framebuffer_height != cur_fb_height); |
| 3674 | _sapp.framebuffer_width = cur_fb_width; |
| 3675 | _sapp.framebuffer_height = cur_fb_height; |
| 3676 | #endif |
| 3677 | if (_sapp.framebuffer_width == 0) { |
| 3678 | _sapp.framebuffer_width = 1; |
| 3679 | } |
| 3680 | if (_sapp.framebuffer_height == 0) { |
| 3681 | _sapp.framebuffer_height = 1; |
| 3682 | } |
| 3683 | if (_sapp.window_width == 0) { |
| 3684 | _sapp.window_width = 1; |
| 3685 | } |
| 3686 | if (_sapp.window_height == 0) { |
| 3687 | _sapp.window_height = 1; |
| 3688 | } |
| 3689 | if (dim_changed) { |
| 3690 | #if defined(SOKOL_METAL) |
| 3691 | CGSize drawable_size = { (CGFloat) _sapp.framebuffer_width, (CGFloat) _sapp.framebuffer_height }; |
| 3692 | _sapp.macos.view.drawableSize = drawable_size; |
| 3693 | #else |
| 3694 | // nothing to do for GL? |
| 3695 | #endif |
| 3696 | if (!_sapp.first_frame) { |
| 3697 | _sapp_macos_app_event(SAPP_EVENTTYPE_RESIZED); |
| 3698 | } |
| 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | _SOKOL_PRIVATE void _sapp_macos_toggle_fullscreen(void) { |
| 3703 | /* NOTE: the _sapp.fullscreen flag is also notified by the |
| 3704 | windowDidEnterFullscreen / windowDidExitFullscreen |
| 3705 | event handlers |
| 3706 | */ |
| 3707 | _sapp.fullscreen = !_sapp.fullscreen; |
| 3708 | [_sapp.macos.window toggleFullScreen:nil]; |
| 3709 | } |
| 3710 | |
| 3711 | _SOKOL_PRIVATE void _sapp_macos_set_clipboard_string(const char* str) { |
| 3712 | @autoreleasepool { |
| 3713 | NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; |
| 3714 | [pasteboard declareTypes:@[NSPasteboardTypeString] owner:nil]; |
| 3715 | [pasteboard setString:@(str) forType:NSPasteboardTypeString]; |
| 3716 | } |
| 3717 | } |
| 3718 | |
| 3719 | _SOKOL_PRIVATE const char* _sapp_macos_get_clipboard_string(void) { |
| 3720 | SOKOL_ASSERT(_sapp.clipboard.buffer); |
| 3721 | @autoreleasepool { |
| 3722 | _sapp.clipboard.buffer[0] = 0; |
| 3723 | NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; |
| 3724 | if (![[pasteboard types] containsObject:NSPasteboardTypeString]) { |
| 3725 | return _sapp.clipboard.buffer; |
| 3726 | } |
| 3727 | NSString* str = [pasteboard stringForType:NSPasteboardTypeString]; |
| 3728 | if (!str) { |
| 3729 | return _sapp.clipboard.buffer; |
| 3730 | } |
| 3731 | _sapp_strcpy([str UTF8String], _sapp.clipboard.buffer, _sapp.clipboard.buf_size); |
| 3732 | } |
| 3733 | return _sapp.clipboard.buffer; |
| 3734 | } |
| 3735 | |
| 3736 | _SOKOL_PRIVATE void _sapp_macos_update_window_title(void) { |
| 3737 | [_sapp.macos.window setTitle: [NSString stringWithUTF8String:_sapp.window_title]]; |
| 3738 | } |
| 3739 | |
| 3740 | _SOKOL_PRIVATE void _sapp_macos_mouse_update_from_nspoint(NSPoint mouse_pos, bool clear_dxdy) { |
| 3741 | if (!_sapp.mouse.locked) { |
| 3742 | float new_x = mouse_pos.x * _sapp.dpi_scale; |
| 3743 | float new_y = _sapp.framebuffer_height - (mouse_pos.y * _sapp.dpi_scale) - 1; |
| 3744 | if (clear_dxdy) { |
| 3745 | _sapp.mouse.dx = 0.0f; |
| 3746 | _sapp.mouse.dy = 0.0f; |
| 3747 | } |
| 3748 | else if (_sapp.mouse.pos_valid) { |
| 3749 | // don't update dx/dy in the very first update |
| 3750 | _sapp.mouse.dx = new_x - _sapp.mouse.x; |
| 3751 | _sapp.mouse.dy = new_y - _sapp.mouse.y; |
| 3752 | } |
| 3753 | _sapp.mouse.x = new_x; |
| 3754 | _sapp.mouse.y = new_y; |
| 3755 | _sapp.mouse.pos_valid = true; |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | _SOKOL_PRIVATE void _sapp_macos_mouse_update_from_nsevent(NSEvent* event, bool clear_dxdy) { |
| 3760 | _sapp_macos_mouse_update_from_nspoint(event.locationInWindow, clear_dxdy); |
| 3761 | } |
| 3762 | |
| 3763 | _SOKOL_PRIVATE void _sapp_macos_show_mouse(bool visible) { |
| 3764 | /* NOTE: this function is only called when the mouse visibility actually changes */ |
| 3765 | if (visible) { |
| 3766 | CGDisplayShowCursor(kCGDirectMainDisplay); |
| 3767 | } |
| 3768 | else { |
| 3769 | CGDisplayHideCursor(kCGDirectMainDisplay); |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | _SOKOL_PRIVATE void _sapp_macos_lock_mouse(bool lock) { |
| 3774 | if (lock == _sapp.mouse.locked) { |
| 3775 | return; |
| 3776 | } |
| 3777 | _sapp.mouse.dx = 0.0f; |
| 3778 | _sapp.mouse.dy = 0.0f; |
| 3779 | _sapp.mouse.locked = lock; |
| 3780 | /* |
| 3781 | NOTE that this code doesn't warp the mouse cursor to the window |
| 3782 | center as everybody else does it. This lead to a spike in the |
| 3783 | *second* mouse-moved event after the warp happened. The |
| 3784 | mouse centering doesn't seem to be required (mouse-moved events |
| 3785 | are reported correctly even when the cursor is at an edge of the screen). |
| 3786 | |
| 3787 | NOTE also that the hide/show of the mouse cursor should properly |
| 3788 | stack with calls to sapp_show_mouse() |
| 3789 | */ |
| 3790 | if (_sapp.mouse.locked) { |
| 3791 | CGAssociateMouseAndMouseCursorPosition(NO); |
| 3792 | [NSCursor hide]; |
| 3793 | } |
| 3794 | else { |
| 3795 | [NSCursor unhide]; |
| 3796 | CGAssociateMouseAndMouseCursorPosition(YES); |
| 3797 | } |
| 3798 | } |
| 3799 | |
| 3800 | _SOKOL_PRIVATE void _sapp_macos_update_cursor(sapp_mouse_cursor cursor, bool shown) { |
| 3801 | // show/hide cursor only if visibility status has changed (required because show/hide stacks) |
| 3802 | if (shown != _sapp.mouse.shown) { |
| 3803 | if (shown) { |
| 3804 | [NSCursor unhide]; |
| 3805 | } |
| 3806 | else { |
| 3807 | [NSCursor hide]; |
| 3808 | } |
| 3809 | } |
| 3810 | // update cursor type |
| 3811 | SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM)); |
| 3812 | if (_sapp.macos.cursors[cursor]) { |
| 3813 | [_sapp.macos.cursors[cursor] set]; |
| 3814 | } |
| 3815 | else { |
| 3816 | [[NSCursor arrowCursor] set]; |
| 3817 | } |
| 3818 | } |
| 3819 | |
| 3820 | _SOKOL_PRIVATE void _sapp_macos_set_icon(const sapp_icon_desc* icon_desc, int num_images) { |
| 3821 | NSDockTile* dock_tile = NSApp.dockTile; |
| 3822 | const int wanted_width = (int) dock_tile.size.width; |
| 3823 | const int wanted_height = (int) dock_tile.size.height; |
| 3824 | const int img_index = _sapp_image_bestmatch(icon_desc->images, num_images, wanted_width, wanted_height); |
| 3825 | const sapp_image_desc* img_desc = &icon_desc->images[img_index]; |
| 3826 | |
| 3827 | CGColorSpaceRef cg_color_space = CGColorSpaceCreateDeviceRGB(); |
| 3828 | CFDataRef cf_data = CFDataCreate(kCFAllocatorDefault, (const UInt8*)img_desc->pixels.ptr, (CFIndex)img_desc->pixels.size); |
| 3829 | CGDataProviderRef cg_data_provider = CGDataProviderCreateWithCFData(cf_data); |
| 3830 | CGImageRef cg_img = CGImageCreate( |
| 3831 | (size_t)img_desc->width, // width |
| 3832 | (size_t)img_desc->height, // height |
| 3833 | 8, // bitsPerComponent |
| 3834 | 32, // bitsPerPixel |
| 3835 | (size_t)img_desc->width * 4,// bytesPerRow |
| 3836 | cg_color_space, // space |
| 3837 | kCGImageAlphaLast | kCGImageByteOrderDefault, // bitmapInfo |
| 3838 | cg_data_provider, // provider |
| 3839 | NULL, // decode |
| 3840 | false, // shouldInterpolate |
| 3841 | kCGRenderingIntentDefault); |
| 3842 | CFRelease(cf_data); |
| 3843 | CGDataProviderRelease(cg_data_provider); |
| 3844 | CGColorSpaceRelease(cg_color_space); |
| 3845 | |
| 3846 | NSImage* ns_image = [[NSImage alloc] initWithCGImage:cg_img size:dock_tile.size]; |
| 3847 | dock_tile.contentView = [NSImageView imageViewWithImage:ns_image]; |
| 3848 | [dock_tile display]; |
| 3849 | _SAPP_OBJC_RELEASE(ns_image); |
| 3850 | CGImageRelease(cg_img); |
| 3851 | } |
| 3852 | |
| 3853 | _SOKOL_PRIVATE void _sapp_macos_frame(void) { |
| 3854 | _sapp_frame(); |
| 3855 | if (_sapp.quit_requested || _sapp.quit_ordered) { |
| 3856 | [_sapp.macos.window performClose:nil]; |
| 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | @implementation _sapp_macos_app_delegate |
| 3861 | - (void)applicationDidFinishLaunching:(NSNotification*)aNotification { |
| 3862 | _SOKOL_UNUSED(aNotification); |
| 3863 | _sapp_macos_init_cursors(); |
| 3864 | if ((_sapp.window_width == 0) || (_sapp.window_height == 0)) { |
| 3865 | // use 4/5 of screen size as default size |
| 3866 | NSRect screen_rect = NSScreen.mainScreen.frame; |
| 3867 | if (_sapp.window_width == 0) { |
| 3868 | _sapp.window_width = (int)roundf((screen_rect.size.width * 4.0f) / 5.0f); |
| 3869 | } |
| 3870 | if (_sapp.window_height == 0) { |
| 3871 | _sapp.window_height = (int)roundf((screen_rect.size.height * 4.0f) / 5.0f); |
| 3872 | } |
| 3873 | } |
| 3874 | const NSUInteger style = |
| 3875 | NSWindowStyleMaskTitled | |
| 3876 | NSWindowStyleMaskClosable | |
| 3877 | NSWindowStyleMaskMiniaturizable | |
| 3878 | NSWindowStyleMaskResizable; |
| 3879 | NSRect window_rect = NSMakeRect(0, 0, _sapp.window_width, _sapp.window_height); |
| 3880 | _sapp.macos.window = [[_sapp_macos_window alloc] |
| 3881 | initWithContentRect:window_rect |
| 3882 | styleMask:style |
| 3883 | backing:NSBackingStoreBuffered |
| 3884 | defer:NO]; |
| 3885 | _sapp.macos.window.releasedWhenClosed = NO; // this is necessary for proper cleanup in applicationWillTerminate |
| 3886 | _sapp.macos.window.title = [NSString stringWithUTF8String:_sapp.window_title]; |
| 3887 | _sapp.macos.window.acceptsMouseMovedEvents = YES; |
| 3888 | _sapp.macos.window.restorable = YES; |
| 3889 | |
| 3890 | _sapp.macos.win_dlg = [[_sapp_macos_window_delegate alloc] init]; |
| 3891 | _sapp.macos.window.delegate = _sapp.macos.win_dlg; |
| 3892 | #if defined(SOKOL_METAL) |
| 3893 | NSInteger max_fps = 60; |
| 3894 | #if (__MAC_OS_X_VERSION_MAX_ALLOWED >= 120000) |
| 3895 | if (@available(macOS 12.0, *)) { |
| 3896 | max_fps = [NSScreen.mainScreen maximumFramesPerSecond]; |
| 3897 | } |
| 3898 | #endif |
| 3899 | _sapp.macos.mtl_device = MTLCreateSystemDefaultDevice(); |
| 3900 | _sapp.macos.view = [[_sapp_macos_view alloc] init]; |
| 3901 | [_sapp.macos.view updateTrackingAreas]; |
| 3902 | _sapp.macos.view.preferredFramesPerSecond = max_fps / _sapp.swap_interval; |
| 3903 | _sapp.macos.view.device = _sapp.macos.mtl_device; |
| 3904 | _sapp.macos.view.colorPixelFormat = MTLPixelFormatBGRA8Unorm; |
| 3905 | _sapp.macos.view.depthStencilPixelFormat = MTLPixelFormatDepth32Float_Stencil8; |
| 3906 | _sapp.macos.view.sampleCount = (NSUInteger) _sapp.sample_count; |
| 3907 | _sapp.macos.view.autoResizeDrawable = false; |
| 3908 | _sapp.macos.window.contentView = _sapp.macos.view; |
| 3909 | [_sapp.macos.window makeFirstResponder:_sapp.macos.view]; |
| 3910 | _sapp.macos.view.layer.magnificationFilter = kCAFilterNearest; |
| 3911 | #elif defined(SOKOL_GLCORE) |
| 3912 | NSOpenGLPixelFormatAttribute attrs[32]; |
| 3913 | int i = 0; |
| 3914 | attrs[i++] = NSOpenGLPFAAccelerated; |
| 3915 | attrs[i++] = NSOpenGLPFADoubleBuffer; |
| 3916 | attrs[i++] = NSOpenGLPFAOpenGLProfile; |
| 3917 | const int glVersion = _sapp.desc.gl_major_version * 10 + _sapp.desc.gl_minor_version; |
| 3918 | switch(glVersion) { |
| 3919 | case 10: attrs[i++] = NSOpenGLProfileVersionLegacy; break; |
| 3920 | case 32: attrs[i++] = NSOpenGLProfileVersion3_2Core; break; |
| 3921 | case 41: attrs[i++] = NSOpenGLProfileVersion4_1Core; break; |
| 3922 | default: |
| 3923 | _SAPP_PANIC(MACOS_INVALID_NSOPENGL_PROFILE); |
| 3924 | } |
| 3925 | attrs[i++] = NSOpenGLPFAColorSize; attrs[i++] = 24; |
| 3926 | attrs[i++] = NSOpenGLPFAAlphaSize; attrs[i++] = 8; |
| 3927 | attrs[i++] = NSOpenGLPFADepthSize; attrs[i++] = 24; |
| 3928 | attrs[i++] = NSOpenGLPFAStencilSize; attrs[i++] = 8; |
| 3929 | if (_sapp.sample_count > 1) { |
| 3930 | attrs[i++] = NSOpenGLPFAMultisample; |
| 3931 | attrs[i++] = NSOpenGLPFASampleBuffers; attrs[i++] = 1; |
| 3932 | attrs[i++] = NSOpenGLPFASamples; attrs[i++] = (NSOpenGLPixelFormatAttribute)_sapp.sample_count; |
| 3933 | } |
| 3934 | else { |
| 3935 | attrs[i++] = NSOpenGLPFASampleBuffers; attrs[i++] = 0; |
| 3936 | } |
| 3937 | attrs[i++] = 0; |
| 3938 | NSOpenGLPixelFormat* glpixelformat_obj = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs]; |
| 3939 | SOKOL_ASSERT(glpixelformat_obj != nil); |
| 3940 | |
| 3941 | _sapp.macos.view = [[_sapp_macos_view alloc] |
| 3942 | initWithFrame:window_rect |
| 3943 | pixelFormat:glpixelformat_obj]; |
| 3944 | _SAPP_OBJC_RELEASE(glpixelformat_obj); |
| 3945 | [_sapp.macos.view updateTrackingAreas]; |
| 3946 | if (_sapp.desc.high_dpi) { |
| 3947 | [_sapp.macos.view setWantsBestResolutionOpenGLSurface:YES]; |
| 3948 | } |
| 3949 | else { |
| 3950 | [_sapp.macos.view setWantsBestResolutionOpenGLSurface:NO]; |
| 3951 | } |
| 3952 | |
| 3953 | _sapp.macos.window.contentView = _sapp.macos.view; |
| 3954 | [_sapp.macos.window makeFirstResponder:_sapp.macos.view]; |
| 3955 | |
| 3956 | NSTimer* timer_obj = [NSTimer timerWithTimeInterval:0.001 |
| 3957 | target:_sapp.macos.view |
| 3958 | selector:@selector(timerFired:) |
| 3959 | userInfo:nil |
| 3960 | repeats:YES]; |
| 3961 | [[NSRunLoop currentRunLoop] addTimer:timer_obj forMode:NSDefaultRunLoopMode]; |
| 3962 | timer_obj = nil; |
| 3963 | #endif |
| 3964 | [_sapp.macos.window center]; |
| 3965 | _sapp.valid = true; |
| 3966 | if (_sapp.fullscreen) { |
| 3967 | /* ^^^ on GL, this already toggles a rendered frame, so set the valid flag before */ |
| 3968 | [_sapp.macos.window toggleFullScreen:self]; |
| 3969 | } |
| 3970 | NSApp.activationPolicy = NSApplicationActivationPolicyRegular; |
| 3971 | [NSApp activateIgnoringOtherApps:YES]; |
| 3972 | [_sapp.macos.window makeKeyAndOrderFront:nil]; |
| 3973 | _sapp_macos_update_dimensions(); |
| 3974 | [NSEvent setMouseCoalescingEnabled:NO]; |
| 3975 | |
| 3976 | // workaround for window not being focused during a long init callback |
| 3977 | // for details see: https://github.com/floooh/sokol/pull/982 |
| 3978 | // also see: https://gitlab.gnome.org/GNOME/gtk/-/issues/2342 |
| 3979 | NSEvent *focusevent = [NSEvent otherEventWithType:NSEventTypeAppKitDefined |
| 3980 | location:NSZeroPoint |
| 3981 | modifierFlags:0x40 |
| 3982 | timestamp:0 |
| 3983 | windowNumber:0 |
| 3984 | context:nil |
| 3985 | subtype:NSEventSubtypeApplicationActivated |
| 3986 | data1:0 |
| 3987 | data2:0]; |
| 3988 | [NSApp postEvent:focusevent atStart:YES]; |
| 3989 | } |
| 3990 | |
| 3991 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender { |
| 3992 | _SOKOL_UNUSED(sender); |
| 3993 | return YES; |
| 3994 | } |
| 3995 | |
| 3996 | - (void)applicationWillTerminate:(NSNotification*)notification { |
| 3997 | _SOKOL_UNUSED(notification); |
| 3998 | _sapp_call_cleanup(); |
| 3999 | _sapp_macos_discard_state(); |
| 4000 | _sapp_discard_state(); |
| 4001 | } |
| 4002 | @end |
| 4003 | |
| 4004 | @implementation _sapp_macos_window_delegate |
| 4005 | - (BOOL)windowShouldClose:(id)sender { |
| 4006 | _SOKOL_UNUSED(sender); |
| 4007 | /* only give user-code a chance to intervene when sapp_quit() wasn't already called */ |
| 4008 | if (!_sapp.quit_ordered) { |
| 4009 | /* if window should be closed and event handling is enabled, give user code |
| 4010 | a chance to intervene via sapp_cancel_quit() |
| 4011 | */ |
| 4012 | _sapp.quit_requested = true; |
| 4013 | _sapp_macos_app_event(SAPP_EVENTTYPE_QUIT_REQUESTED); |
| 4014 | /* user code hasn't intervened, quit the app */ |
| 4015 | if (_sapp.quit_requested) { |
| 4016 | _sapp.quit_ordered = true; |
| 4017 | } |
| 4018 | } |
| 4019 | if (_sapp.quit_ordered) { |
| 4020 | return YES; |
| 4021 | } |
| 4022 | else { |
| 4023 | return NO; |
| 4024 | } |
| 4025 | } |
| 4026 | |
| 4027 | #if defined(SOKOL_METAL) |
| 4028 | - (void)windowWillStartLiveResize:(NSNotification *)notification { |
| 4029 | // Work around the MTKView resizing glitch by "anchoring" the layer to the window corner opposite |
| 4030 | // to the currently manipulated corner (or edge). This prevents the content stretching back and |
| 4031 | // forth during resizing. This is a workaround for this issue: https://github.com/floooh/sokol/issues/700 |
| 4032 | // Can be removed if/when migrating to CAMetalLayer: https://github.com/floooh/sokol/issues/727 |
| 4033 | bool resizing_from_left = _sapp.mouse.x < _sapp.window_width/2; |
| 4034 | bool resizing_from_top = _sapp.mouse.y < _sapp.window_height/2; |
| 4035 | NSViewLayerContentsPlacement placement; |
| 4036 | if (resizing_from_left) { |
| 4037 | placement = resizing_from_top ? NSViewLayerContentsPlacementBottomRight : NSViewLayerContentsPlacementTopRight; |
| 4038 | } else { |
| 4039 | placement = resizing_from_top ? NSViewLayerContentsPlacementBottomLeft : NSViewLayerContentsPlacementTopLeft; |
| 4040 | } |
| 4041 | _sapp.macos.view.layerContentsPlacement = placement; |
| 4042 | } |
| 4043 | #endif |
| 4044 | |
| 4045 | - (void)windowDidResize:(NSNotification*)notification { |
| 4046 | _SOKOL_UNUSED(notification); |
| 4047 | _sapp_macos_update_dimensions(); |
| 4048 | } |
| 4049 | |
| 4050 | - (void)windowDidChangeScreen:(NSNotification*)notification { |
| 4051 | _SOKOL_UNUSED(notification); |
| 4052 | _sapp_timing_reset(&_sapp.timing); |
| 4053 | _sapp_macos_update_dimensions(); |
| 4054 | } |
| 4055 | |
| 4056 | - (void)windowDidMiniaturize:(NSNotification*)notification { |
| 4057 | _SOKOL_UNUSED(notification); |
| 4058 | _sapp_macos_app_event(SAPP_EVENTTYPE_ICONIFIED); |
| 4059 | } |
| 4060 | |
| 4061 | - (void)windowDidDeminiaturize:(NSNotification*)notification { |
| 4062 | _SOKOL_UNUSED(notification); |
| 4063 | _sapp_macos_app_event(SAPP_EVENTTYPE_RESTORED); |
| 4064 | } |
| 4065 | |
| 4066 | - (void)windowDidBecomeKey:(NSNotification*)notification { |
| 4067 | _SOKOL_UNUSED(notification); |
| 4068 | _sapp_macos_app_event(SAPP_EVENTTYPE_FOCUSED); |
| 4069 | } |
| 4070 | |
| 4071 | - (void)windowDidResignKey:(NSNotification*)notification { |
| 4072 | _SOKOL_UNUSED(notification); |
| 4073 | _sapp_macos_app_event(SAPP_EVENTTYPE_UNFOCUSED); |
| 4074 | } |
| 4075 | |
| 4076 | - (void)windowDidEnterFullScreen:(NSNotification*)notification { |
| 4077 | _SOKOL_UNUSED(notification); |
| 4078 | _sapp.fullscreen = true; |
| 4079 | } |
| 4080 | |
| 4081 | - (void)windowDidExitFullScreen:(NSNotification*)notification { |
| 4082 | _SOKOL_UNUSED(notification); |
| 4083 | _sapp.fullscreen = false; |
| 4084 | } |
| 4085 | @end |
| 4086 | |
| 4087 | @implementation _sapp_macos_window |
| 4088 | - (instancetype)initWithContentRect:(NSRect)contentRect |
| 4089 | styleMask:(NSWindowStyleMask)style |
| 4090 | backing:(NSBackingStoreType)backingStoreType |
| 4091 | defer:(BOOL)flag { |
| 4092 | if (self = [super initWithContentRect:contentRect styleMask:style backing:backingStoreType defer:flag]) { |
| 4093 | #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101300 |
| 4094 | [self registerForDraggedTypes:[NSArray arrayWithObject:NSPasteboardTypeFileURL]]; |
| 4095 | #endif |
| 4096 | } |
| 4097 | return self; |
| 4098 | } |
| 4099 | |
| 4100 | - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender { |
| 4101 | return NSDragOperationCopy; |
| 4102 | } |
| 4103 | |
| 4104 | - (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender { |
| 4105 | return NSDragOperationCopy; |
| 4106 | } |
| 4107 | |
| 4108 | - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { |
| 4109 | #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101300 |
| 4110 | NSPasteboard *pboard = [sender draggingPasteboard]; |
| 4111 | if ([pboard.types containsObject:NSPasteboardTypeFileURL]) { |
| 4112 | _sapp_clear_drop_buffer(); |
| 4113 | _sapp.drop.num_files = ((int)pboard.pasteboardItems.count > _sapp.drop.max_files) ? _sapp.drop.max_files : (int)pboard.pasteboardItems.count; |
| 4114 | bool drop_failed = false; |
| 4115 | for (int i = 0; i < _sapp.drop.num_files; i++) { |
| 4116 | NSURL *fileUrl = [NSURL fileURLWithPath:[pboard.pasteboardItems[(NSUInteger)i] stringForType:NSPasteboardTypeFileURL]]; |
| 4117 | if (!_sapp_strcpy(fileUrl.standardizedURL.path.UTF8String, _sapp_dropped_file_path_ptr(i), _sapp.drop.max_path_length)) { |
| 4118 | _SAPP_ERROR(DROPPED_FILE_PATH_TOO_LONG); |
| 4119 | drop_failed = true; |
| 4120 | break; |
| 4121 | } |
| 4122 | } |
| 4123 | if (!drop_failed) { |
| 4124 | if (_sapp_events_enabled()) { |
| 4125 | _sapp_macos_mouse_update_from_nspoint(sender.draggingLocation, true); |
| 4126 | _sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED); |
| 4127 | _sapp.event.modifiers = _sapp_macos_mods(nil); |
| 4128 | _sapp_call_event(&_sapp.event); |
| 4129 | } |
| 4130 | } |
| 4131 | else { |
| 4132 | _sapp_clear_drop_buffer(); |
| 4133 | _sapp.drop.num_files = 0; |
| 4134 | } |
| 4135 | return YES; |
| 4136 | } |
| 4137 | #endif |
| 4138 | return NO; |
| 4139 | } |
| 4140 | @end |
| 4141 | |
| 4142 | @implementation _sapp_macos_view |
| 4143 | #if defined(SOKOL_GLCORE) |
| 4144 | - (void)timerFired:(id)sender { |
| 4145 | _SOKOL_UNUSED(sender); |
| 4146 | [self setNeedsDisplay:YES]; |
| 4147 | } |
| 4148 | - (void)prepareOpenGL { |
| 4149 | [super prepareOpenGL]; |
| 4150 | GLint swapInt = 1; |
| 4151 | NSOpenGLContext* ctx = [_sapp.macos.view openGLContext]; |
| 4152 | [ctx setValues:&swapInt forParameter:NSOpenGLContextParameterSwapInterval]; |
| 4153 | [ctx makeCurrentContext]; |
| 4154 | } |
| 4155 | #endif |
| 4156 | |
| 4157 | _SOKOL_PRIVATE void _sapp_macos_poll_input_events(void) { |
| 4158 | /* |
| 4159 | |
| 4160 | NOTE: late event polling temporarily out-commented to check if this |
| 4161 | causes infrequent and almost impossible to reproduce problems with the |
| 4162 | window close events, see: |
| 4163 | https://github.com/floooh/sokol/pull/483#issuecomment-805148815 |
| 4164 | |
| 4165 | |
| 4166 | const NSEventMask mask = NSEventMaskLeftMouseDown | |
| 4167 | NSEventMaskLeftMouseUp| |
| 4168 | NSEventMaskRightMouseDown | |
| 4169 | NSEventMaskRightMouseUp | |
| 4170 | NSEventMaskMouseMoved | |
| 4171 | NSEventMaskLeftMouseDragged | |
| 4172 | NSEventMaskRightMouseDragged | |
| 4173 | NSEventMaskMouseEntered | |
| 4174 | NSEventMaskMouseExited | |
| 4175 | NSEventMaskKeyDown | |
| 4176 | NSEventMaskKeyUp | |
| 4177 | NSEventMaskCursorUpdate | |
| 4178 | NSEventMaskScrollWheel | |
| 4179 | NSEventMaskTabletPoint | |
| 4180 | NSEventMaskTabletProximity | |
| 4181 | NSEventMaskOtherMouseDown | |
| 4182 | NSEventMaskOtherMouseUp | |
| 4183 | NSEventMaskOtherMouseDragged | |
| 4184 | NSEventMaskPressure | |
| 4185 | NSEventMaskDirectTouch; |
| 4186 | @autoreleasepool { |
| 4187 | for (;;) { |
| 4188 | // NOTE: using NSDefaultRunLoopMode here causes stuttering in the GL backend, |
| 4189 | // see: https://github.com/floooh/sokol/issues/486 |
| 4190 | NSEvent* event = [NSApp nextEventMatchingMask:mask untilDate:nil inMode:NSEventTrackingRunLoopMode dequeue:YES]; |
| 4191 | if (event == nil) { |
| 4192 | break; |
| 4193 | } |
| 4194 | [NSApp sendEvent:event]; |
| 4195 | } |
| 4196 | } |
| 4197 | */ |
| 4198 | } |
| 4199 | |
| 4200 | - (void)drawRect:(NSRect)rect { |
| 4201 | _SOKOL_UNUSED(rect); |
| 4202 | #if defined(_SAPP_ANY_GL) |
| 4203 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer); |
| 4204 | #endif |
| 4205 | _sapp_timing_measure(&_sapp.timing); |
| 4206 | /* Catch any last-moment input events */ |
| 4207 | _sapp_macos_poll_input_events(); |
| 4208 | @autoreleasepool { |
| 4209 | _sapp_macos_frame(); |
| 4210 | } |
| 4211 | #if defined(_SAPP_ANY_GL) |
| 4212 | [[_sapp.macos.view openGLContext] flushBuffer]; |
| 4213 | #endif |
| 4214 | } |
| 4215 | |
| 4216 | - (BOOL)isOpaque { |
| 4217 | return YES; |
| 4218 | } |
| 4219 | - (BOOL)canBecomeKeyView { |
| 4220 | return YES; |
| 4221 | } |
| 4222 | - (BOOL)acceptsFirstResponder { |
| 4223 | return YES; |
| 4224 | } |
| 4225 | - (void)updateTrackingAreas { |
| 4226 | if (_sapp.macos.tracking_area != nil) { |
| 4227 | [self removeTrackingArea:_sapp.macos.tracking_area]; |
| 4228 | _SAPP_OBJC_RELEASE(_sapp.macos.tracking_area); |
| 4229 | } |
| 4230 | const NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | |
| 4231 | NSTrackingActiveInKeyWindow | |
| 4232 | NSTrackingEnabledDuringMouseDrag | |
| 4233 | NSTrackingCursorUpdate | |
| 4234 | NSTrackingInVisibleRect | |
| 4235 | NSTrackingAssumeInside; |
| 4236 | _sapp.macos.tracking_area = [[NSTrackingArea alloc] initWithRect:[self bounds] options:options owner:self userInfo:nil]; |
| 4237 | [self addTrackingArea:_sapp.macos.tracking_area]; |
| 4238 | [super updateTrackingAreas]; |
| 4239 | } |
| 4240 | |
| 4241 | // helper function to make GL context active |
| 4242 | static void _sapp_gl_make_current(void) { |
| 4243 | #if defined(SOKOL_GLCORE) |
| 4244 | [[_sapp.macos.view openGLContext] makeCurrentContext]; |
| 4245 | #endif |
| 4246 | } |
| 4247 | |
| 4248 | - (void)mouseEntered:(NSEvent*)event { |
| 4249 | _sapp_gl_make_current(); |
| 4250 | _sapp_macos_mouse_update_from_nsevent(event, true); |
| 4251 | /* don't send mouse enter/leave while dragging (so that it behaves the same as |
| 4252 | on Windows while SetCapture is active |
| 4253 | */ |
| 4254 | if (0 == _sapp.macos.mouse_buttons) { |
| 4255 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_ENTER, SAPP_MOUSEBUTTON_INVALID, _sapp_macos_mods(event)); |
| 4256 | } |
| 4257 | } |
| 4258 | - (void)mouseExited:(NSEvent*)event { |
| 4259 | _sapp_gl_make_current(); |
| 4260 | _sapp_macos_mouse_update_from_nsevent(event, true); |
| 4261 | if (0 == _sapp.macos.mouse_buttons) { |
| 4262 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_LEAVE, SAPP_MOUSEBUTTON_INVALID, _sapp_macos_mods(event)); |
| 4263 | } |
| 4264 | } |
| 4265 | - (void)mouseDown:(NSEvent*)event { |
| 4266 | _sapp_gl_make_current(); |
| 4267 | _sapp_macos_mouse_update_from_nsevent(event, false); |
| 4268 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_LEFT, _sapp_macos_mods(event)); |
| 4269 | _sapp.macos.mouse_buttons |= (1<<SAPP_MOUSEBUTTON_LEFT); |
| 4270 | } |
| 4271 | - (void)mouseUp:(NSEvent*)event { |
| 4272 | _sapp_gl_make_current(); |
| 4273 | _sapp_macos_mouse_update_from_nsevent(event, false); |
| 4274 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_LEFT, _sapp_macos_mods(event)); |
| 4275 | _sapp.macos.mouse_buttons &= ~(1<<SAPP_MOUSEBUTTON_LEFT); |
| 4276 | } |
| 4277 | - (void)rightMouseDown:(NSEvent*)event { |
| 4278 | _sapp_gl_make_current(); |
| 4279 | _sapp_macos_mouse_update_from_nsevent(event, false); |
| 4280 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_RIGHT, _sapp_macos_mods(event)); |
| 4281 | _sapp.macos.mouse_buttons |= (1<<SAPP_MOUSEBUTTON_RIGHT); |
| 4282 | } |
| 4283 | - (void)rightMouseUp:(NSEvent*)event { |
| 4284 | _sapp_gl_make_current(); |
| 4285 | _sapp_macos_mouse_update_from_nsevent(event, false); |
| 4286 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_RIGHT, _sapp_macos_mods(event)); |
| 4287 | _sapp.macos.mouse_buttons &= ~(1<<SAPP_MOUSEBUTTON_RIGHT); |
| 4288 | } |
| 4289 | - (void)otherMouseDown:(NSEvent*)event { |
| 4290 | _sapp_gl_make_current(); |
| 4291 | _sapp_macos_mouse_update_from_nsevent(event, false); |
| 4292 | if (2 == event.buttonNumber) { |
| 4293 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_MIDDLE, _sapp_macos_mods(event)); |
| 4294 | _sapp.macos.mouse_buttons |= (1<<SAPP_MOUSEBUTTON_MIDDLE); |
| 4295 | } |
| 4296 | } |
| 4297 | - (void)otherMouseUp:(NSEvent*)event { |
| 4298 | _sapp_gl_make_current(); |
| 4299 | _sapp_macos_mouse_update_from_nsevent(event, false); |
| 4300 | if (2 == event.buttonNumber) { |
| 4301 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_MIDDLE, _sapp_macos_mods(event)); |
| 4302 | _sapp.macos.mouse_buttons &= (1<<SAPP_MOUSEBUTTON_MIDDLE); |
| 4303 | } |
| 4304 | } |
| 4305 | - (void)otherMouseDragged:(NSEvent*)event { |
| 4306 | _sapp_gl_make_current(); |
| 4307 | _sapp_macos_mouse_update_from_nsevent(event, false); |
| 4308 | if (2 == event.buttonNumber) { |
| 4309 | if (_sapp.mouse.locked) { |
| 4310 | _sapp.mouse.dx = [event deltaX]; |
| 4311 | _sapp.mouse.dy = [event deltaY]; |
| 4312 | } |
| 4313 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID, _sapp_macos_mods(event)); |
| 4314 | } |
| 4315 | } |
| 4316 | - (void)mouseMoved:(NSEvent*)event { |
| 4317 | _sapp_gl_make_current(); |
| 4318 | _sapp_macos_mouse_update_from_nsevent(event, false); |
| 4319 | if (_sapp.mouse.locked) { |
| 4320 | _sapp.mouse.dx = [event deltaX]; |
| 4321 | _sapp.mouse.dy = [event deltaY]; |
| 4322 | } |
| 4323 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID , _sapp_macos_mods(event)); |
| 4324 | } |
| 4325 | - (void)mouseDragged:(NSEvent*)event { |
| 4326 | _sapp_gl_make_current(); |
| 4327 | _sapp_macos_mouse_update_from_nsevent(event, false); |
| 4328 | if (_sapp.mouse.locked) { |
| 4329 | _sapp.mouse.dx = [event deltaX]; |
| 4330 | _sapp.mouse.dy = [event deltaY]; |
| 4331 | } |
| 4332 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID , _sapp_macos_mods(event)); |
| 4333 | } |
| 4334 | - (void)rightMouseDragged:(NSEvent*)event { |
| 4335 | _sapp_gl_make_current(); |
| 4336 | _sapp_macos_mouse_update_from_nsevent(event, false); |
| 4337 | if (_sapp.mouse.locked) { |
| 4338 | _sapp.mouse.dx = [event deltaX]; |
| 4339 | _sapp.mouse.dy = [event deltaY]; |
| 4340 | } |
| 4341 | _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID, _sapp_macos_mods(event)); |
| 4342 | } |
| 4343 | - (void)scrollWheel:(NSEvent*)event { |
| 4344 | _sapp_gl_make_current(); |
| 4345 | _sapp_macos_mouse_update_from_nsevent(event, true); |
| 4346 | if (_sapp_events_enabled()) { |
| 4347 | float dx = (float) event.scrollingDeltaX; |
| 4348 | float dy = (float) event.scrollingDeltaY; |
| 4349 | if (event.hasPreciseScrollingDeltas) { |
| 4350 | dx *= 0.1; |
| 4351 | dy *= 0.1; |
| 4352 | } |
| 4353 | if ((_sapp_absf(dx) > 0.0f) || (_sapp_absf(dy) > 0.0f)) { |
| 4354 | _sapp_init_event(SAPP_EVENTTYPE_MOUSE_SCROLL); |
| 4355 | _sapp.event.modifiers = _sapp_macos_mods(event); |
| 4356 | _sapp.event.scroll_x = dx; |
| 4357 | _sapp.event.scroll_y = dy; |
| 4358 | _sapp_call_event(&_sapp.event); |
| 4359 | } |
| 4360 | } |
| 4361 | } |
| 4362 | - (void)keyDown:(NSEvent*)event { |
| 4363 | if (_sapp_events_enabled()) { |
| 4364 | _sapp_gl_make_current(); |
| 4365 | const uint32_t mods = _sapp_macos_mods(event); |
| 4366 | const sapp_keycode key_code = _sapp_translate_key(event.keyCode); |
| 4367 | _sapp_macos_key_event(SAPP_EVENTTYPE_KEY_DOWN, key_code, event.isARepeat, mods); |
| 4368 | const NSString* chars = event.characters; |
| 4369 | const NSUInteger len = chars.length; |
| 4370 | if (len > 0) { |
| 4371 | _sapp_init_event(SAPP_EVENTTYPE_CHAR); |
| 4372 | _sapp.event.modifiers = mods; |
| 4373 | for (NSUInteger i = 0; i < len; i++) { |
| 4374 | const unichar codepoint = [chars characterAtIndex:i]; |
| 4375 | if ((codepoint & 0xFF00) == 0xF700) { |
| 4376 | continue; |
| 4377 | } |
| 4378 | _sapp.event.char_code = codepoint; |
| 4379 | _sapp.event.key_repeat = event.isARepeat; |
| 4380 | _sapp_call_event(&_sapp.event); |
| 4381 | } |
| 4382 | } |
| 4383 | /* if this is a Cmd+V (paste), also send a CLIPBOARD_PASTE event */ |
| 4384 | if (_sapp.clipboard.enabled && (mods == SAPP_MODIFIER_SUPER) && (key_code == SAPP_KEYCODE_V)) { |
| 4385 | _sapp_init_event(SAPP_EVENTTYPE_CLIPBOARD_PASTED); |
| 4386 | _sapp_call_event(&_sapp.event); |
| 4387 | } |
| 4388 | } |
| 4389 | } |
| 4390 | - (void)keyUp:(NSEvent*)event { |
| 4391 | _sapp_gl_make_current(); |
| 4392 | _sapp_macos_key_event(SAPP_EVENTTYPE_KEY_UP, |
| 4393 | _sapp_translate_key(event.keyCode), |
| 4394 | event.isARepeat, |
| 4395 | _sapp_macos_mods(event)); |
| 4396 | } |
| 4397 | - (void)flagsChanged:(NSEvent*)event { |
| 4398 | const uint32_t old_f = _sapp.macos.flags_changed_store; |
| 4399 | const uint32_t new_f = (uint32_t)event.modifierFlags; |
| 4400 | _sapp.macos.flags_changed_store = new_f; |
| 4401 | sapp_keycode key_code = SAPP_KEYCODE_INVALID; |
| 4402 | bool down = false; |
| 4403 | if ((new_f ^ old_f) & NSEventModifierFlagShift) { |
| 4404 | key_code = SAPP_KEYCODE_LEFT_SHIFT; |
| 4405 | down = 0 != (new_f & NSEventModifierFlagShift); |
| 4406 | } |
| 4407 | if ((new_f ^ old_f) & NSEventModifierFlagControl) { |
| 4408 | key_code = SAPP_KEYCODE_LEFT_CONTROL; |
| 4409 | down = 0 != (new_f & NSEventModifierFlagControl); |
| 4410 | } |
| 4411 | if ((new_f ^ old_f) & NSEventModifierFlagOption) { |
| 4412 | key_code = SAPP_KEYCODE_LEFT_ALT; |
| 4413 | down = 0 != (new_f & NSEventModifierFlagOption); |
| 4414 | } |
| 4415 | if ((new_f ^ old_f) & NSEventModifierFlagCommand) { |
| 4416 | key_code = SAPP_KEYCODE_LEFT_SUPER; |
| 4417 | down = 0 != (new_f & NSEventModifierFlagCommand); |
| 4418 | } |
| 4419 | if (key_code != SAPP_KEYCODE_INVALID) { |
| 4420 | _sapp_macos_key_event(down ? SAPP_EVENTTYPE_KEY_DOWN : SAPP_EVENTTYPE_KEY_UP, |
| 4421 | key_code, |
| 4422 | false, |
| 4423 | _sapp_macos_mods(event)); |
| 4424 | } |
| 4425 | } |
| 4426 | @end |
| 4427 | |
| 4428 | #endif // macOS |
| 4429 | |
| 4430 | // ██ ██████ ███████ |
| 4431 | // ██ ██ ██ ██ |
| 4432 | // ██ ██ ██ ███████ |
| 4433 | // ██ ██ ██ ██ |
| 4434 | // ██ ██████ ███████ |
| 4435 | // |
| 4436 | // >>ios |
| 4437 | #if defined(_SAPP_IOS) |
| 4438 | |
| 4439 | _SOKOL_PRIVATE void _sapp_ios_discard_state(void) { |
| 4440 | // NOTE: it's safe to call [release] on a nil object |
| 4441 | _SAPP_OBJC_RELEASE(_sapp.ios.textfield_dlg); |
| 4442 | _SAPP_OBJC_RELEASE(_sapp.ios.textfield); |
| 4443 | #if defined(SOKOL_METAL) |
| 4444 | _SAPP_OBJC_RELEASE(_sapp.ios.view_ctrl); |
| 4445 | _SAPP_OBJC_RELEASE(_sapp.ios.mtl_device); |
| 4446 | #else |
| 4447 | _SAPP_OBJC_RELEASE(_sapp.ios.view_ctrl); |
| 4448 | _SAPP_OBJC_RELEASE(_sapp.ios.eagl_ctx); |
| 4449 | #endif |
| 4450 | _SAPP_OBJC_RELEASE(_sapp.ios.view); |
| 4451 | _SAPP_OBJC_RELEASE(_sapp.ios.window); |
| 4452 | } |
| 4453 | |
| 4454 | _SOKOL_PRIVATE void _sapp_ios_run(const sapp_desc* desc) { |
| 4455 | _sapp_init_state(desc); |
| 4456 | static int argc = 1; |
| 4457 | static char* argv[] = { (char*)"sokol_app" }; |
| 4458 | UIApplicationMain(argc, argv, nil, NSStringFromClass([_sapp_app_delegate class])); |
| 4459 | } |
| 4460 | |
| 4461 | /* iOS entry function */ |
| 4462 | #if !defined(SOKOL_NO_ENTRY) |
| 4463 | int main(int argc, char* argv[]) { |
| 4464 | sapp_desc desc = sokol_main(argc, argv); |
| 4465 | _sapp_ios_run(&desc); |
| 4466 | return 0; |
| 4467 | } |
| 4468 | #endif /* SOKOL_NO_ENTRY */ |
| 4469 | |
| 4470 | _SOKOL_PRIVATE void _sapp_ios_app_event(sapp_event_type type) { |
| 4471 | if (_sapp_events_enabled()) { |
| 4472 | _sapp_init_event(type); |
| 4473 | _sapp_call_event(&_sapp.event); |
| 4474 | } |
| 4475 | } |
| 4476 | |
| 4477 | _SOKOL_PRIVATE void _sapp_ios_touch_event(sapp_event_type type, NSSet<UITouch *>* touches, UIEvent* event) { |
| 4478 | if (_sapp_events_enabled()) { |
| 4479 | _sapp_init_event(type); |
| 4480 | NSEnumerator* enumerator = event.allTouches.objectEnumerator; |
| 4481 | UITouch* ios_touch; |
| 4482 | while ((ios_touch = [enumerator nextObject])) { |
| 4483 | if ((_sapp.event.num_touches + 1) < SAPP_MAX_TOUCHPOINTS) { |
| 4484 | CGPoint ios_pos = [ios_touch locationInView:_sapp.ios.view]; |
| 4485 | sapp_touchpoint* cur_point = &_sapp.event.touches[_sapp.event.num_touches++]; |
| 4486 | cur_point->identifier = (uintptr_t) ios_touch; |
| 4487 | cur_point->pos_x = ios_pos.x * _sapp.dpi_scale; |
| 4488 | cur_point->pos_y = ios_pos.y * _sapp.dpi_scale; |
| 4489 | cur_point->changed = [touches containsObject:ios_touch]; |
| 4490 | } |
| 4491 | } |
| 4492 | if (_sapp.event.num_touches > 0) { |
| 4493 | _sapp_call_event(&_sapp.event); |
| 4494 | } |
| 4495 | } |
| 4496 | } |
| 4497 | |
| 4498 | _SOKOL_PRIVATE void _sapp_ios_update_dimensions(void) { |
| 4499 | CGRect screen_rect = UIScreen.mainScreen.bounds; |
| 4500 | _sapp.framebuffer_width = (int)roundf(screen_rect.size.width * _sapp.dpi_scale); |
| 4501 | _sapp.framebuffer_height = (int)roundf(screen_rect.size.height * _sapp.dpi_scale); |
| 4502 | _sapp.window_width = (int)roundf(screen_rect.size.width); |
| 4503 | _sapp.window_height = (int)roundf(screen_rect.size.height); |
| 4504 | int cur_fb_width, cur_fb_height; |
| 4505 | #if defined(SOKOL_METAL) |
| 4506 | const CGSize fb_size = _sapp.ios.view.drawableSize; |
| 4507 | cur_fb_width = (int)roundf(fb_size.width); |
| 4508 | cur_fb_height = (int)roundf(fb_size.height); |
| 4509 | #else |
| 4510 | cur_fb_width = (int)roundf(_sapp.ios.view.drawableWidth); |
| 4511 | cur_fb_height = (int)roundf(_sapp.ios.view.drawableHeight); |
| 4512 | #endif |
| 4513 | const bool dim_changed = (_sapp.framebuffer_width != cur_fb_width) || |
| 4514 | (_sapp.framebuffer_height != cur_fb_height); |
| 4515 | if (dim_changed) { |
| 4516 | #if defined(SOKOL_METAL) |
| 4517 | const CGSize drawable_size = { (CGFloat) _sapp.framebuffer_width, (CGFloat) _sapp.framebuffer_height }; |
| 4518 | _sapp.ios.view.drawableSize = drawable_size; |
| 4519 | #else |
| 4520 | // nothing to do here, GLKView correctly respects the view's contentScaleFactor |
| 4521 | #endif |
| 4522 | if (!_sapp.first_frame) { |
| 4523 | _sapp_ios_app_event(SAPP_EVENTTYPE_RESIZED); |
| 4524 | } |
| 4525 | } |
| 4526 | } |
| 4527 | |
| 4528 | _SOKOL_PRIVATE void _sapp_ios_frame(void) { |
| 4529 | _sapp_ios_update_dimensions(); |
| 4530 | _sapp_frame(); |
| 4531 | } |
| 4532 | |
| 4533 | _SOKOL_PRIVATE void _sapp_ios_show_keyboard(bool shown) { |
| 4534 | /* if not happened yet, create an invisible text field */ |
| 4535 | if (nil == _sapp.ios.textfield) { |
| 4536 | _sapp.ios.textfield_dlg = [[_sapp_textfield_dlg alloc] init]; |
| 4537 | _sapp.ios.textfield = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 100, 50)]; |
| 4538 | _sapp.ios.textfield.keyboardType = UIKeyboardTypeDefault; |
| 4539 | _sapp.ios.textfield.returnKeyType = UIReturnKeyDefault; |
| 4540 | _sapp.ios.textfield.autocapitalizationType = UITextAutocapitalizationTypeNone; |
| 4541 | _sapp.ios.textfield.autocorrectionType = UITextAutocorrectionTypeNo; |
| 4542 | _sapp.ios.textfield.spellCheckingType = UITextSpellCheckingTypeNo; |
| 4543 | _sapp.ios.textfield.hidden = YES; |
| 4544 | _sapp.ios.textfield.text = @"x"; |
| 4545 | _sapp.ios.textfield.delegate = _sapp.ios.textfield_dlg; |
| 4546 | [_sapp.ios.view_ctrl.view addSubview:_sapp.ios.textfield]; |
| 4547 | |
| 4548 | [[NSNotificationCenter defaultCenter] addObserver:_sapp.ios.textfield_dlg |
| 4549 | selector:@selector(keyboardWasShown:) |
| 4550 | name:UIKeyboardDidShowNotification object:nil]; |
| 4551 | [[NSNotificationCenter defaultCenter] addObserver:_sapp.ios.textfield_dlg |
| 4552 | selector:@selector(keyboardWillBeHidden:) |
| 4553 | name:UIKeyboardWillHideNotification object:nil]; |
| 4554 | [[NSNotificationCenter defaultCenter] addObserver:_sapp.ios.textfield_dlg |
| 4555 | selector:@selector(keyboardDidChangeFrame:) |
| 4556 | name:UIKeyboardDidChangeFrameNotification object:nil]; |
| 4557 | } |
| 4558 | if (shown) { |
| 4559 | /* setting the text field as first responder brings up the onscreen keyboard */ |
| 4560 | [_sapp.ios.textfield becomeFirstResponder]; |
| 4561 | } |
| 4562 | else { |
| 4563 | [_sapp.ios.textfield resignFirstResponder]; |
| 4564 | } |
| 4565 | } |
| 4566 | |
| 4567 | @implementation _sapp_app_delegate |
| 4568 | - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { |
| 4569 | CGRect screen_rect = UIScreen.mainScreen.bounds; |
| 4570 | _sapp.ios.window = [[UIWindow alloc] initWithFrame:screen_rect]; |
| 4571 | _sapp.window_width = (int)roundf(screen_rect.size.width); |
| 4572 | _sapp.window_height = (int)roundf(screen_rect.size.height); |
| 4573 | if (_sapp.desc.high_dpi) { |
| 4574 | _sapp.dpi_scale = (float) UIScreen.mainScreen.nativeScale; |
| 4575 | } |
| 4576 | else { |
| 4577 | _sapp.dpi_scale = 1.0f; |
| 4578 | } |
| 4579 | _sapp.framebuffer_width = (int)roundf(_sapp.window_width * _sapp.dpi_scale); |
| 4580 | _sapp.framebuffer_height = (int)roundf(_sapp.window_height * _sapp.dpi_scale); |
| 4581 | NSInteger max_fps = UIScreen.mainScreen.maximumFramesPerSecond; |
| 4582 | #if defined(SOKOL_METAL) |
| 4583 | _sapp.ios.mtl_device = MTLCreateSystemDefaultDevice(); |
| 4584 | _sapp.ios.view = [[_sapp_ios_view alloc] init]; |
| 4585 | _sapp.ios.view.preferredFramesPerSecond = max_fps / _sapp.swap_interval; |
| 4586 | _sapp.ios.view.device = _sapp.ios.mtl_device; |
| 4587 | _sapp.ios.view.colorPixelFormat = MTLPixelFormatBGRA8Unorm; |
| 4588 | _sapp.ios.view.depthStencilPixelFormat = MTLPixelFormatDepth32Float_Stencil8; |
| 4589 | _sapp.ios.view.sampleCount = (NSUInteger)_sapp.sample_count; |
| 4590 | /* NOTE: iOS MTKView seems to ignore thew view's contentScaleFactor |
| 4591 | and automatically renders at Retina resolution. We'll disable |
| 4592 | autoResize and instead do the resizing in _sapp_ios_update_dimensions() |
| 4593 | */ |
| 4594 | _sapp.ios.view.autoResizeDrawable = false; |
| 4595 | _sapp.ios.view.userInteractionEnabled = YES; |
| 4596 | _sapp.ios.view.multipleTouchEnabled = YES; |
| 4597 | _sapp.ios.view_ctrl = [[UIViewController alloc] init]; |
| 4598 | _sapp.ios.view_ctrl.modalPresentationStyle = UIModalPresentationFullScreen; |
| 4599 | _sapp.ios.view_ctrl.view = _sapp.ios.view; |
| 4600 | _sapp.ios.window.rootViewController = _sapp.ios.view_ctrl; |
| 4601 | #else |
| 4602 | _sapp.ios.eagl_ctx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; |
| 4603 | _sapp.ios.view = [[_sapp_ios_view alloc] initWithFrame:screen_rect]; |
| 4604 | _sapp.ios.view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888; |
| 4605 | _sapp.ios.view.drawableDepthFormat = GLKViewDrawableDepthFormat24; |
| 4606 | _sapp.ios.view.drawableStencilFormat = GLKViewDrawableStencilFormatNone; |
| 4607 | GLKViewDrawableMultisample msaa = _sapp.sample_count > 1 ? GLKViewDrawableMultisample4X : GLKViewDrawableMultisampleNone; |
| 4608 | _sapp.ios.view.drawableMultisample = msaa; |
| 4609 | _sapp.ios.view.context = _sapp.ios.eagl_ctx; |
| 4610 | _sapp.ios.view.enableSetNeedsDisplay = NO; |
| 4611 | _sapp.ios.view.userInteractionEnabled = YES; |
| 4612 | _sapp.ios.view.multipleTouchEnabled = YES; |
| 4613 | // on GLKView, contentScaleFactor appears to work just fine! |
| 4614 | if (_sapp.desc.high_dpi) { |
| 4615 | _sapp.ios.view.contentScaleFactor = _sapp.dpi_scale; |
| 4616 | } |
| 4617 | else { |
| 4618 | _sapp.ios.view.contentScaleFactor = 1.0; |
| 4619 | } |
| 4620 | _sapp.ios.view_ctrl = [[GLKViewController alloc] init]; |
| 4621 | _sapp.ios.view_ctrl.view = _sapp.ios.view; |
| 4622 | _sapp.ios.view_ctrl.preferredFramesPerSecond = max_fps / _sapp.swap_interval; |
| 4623 | _sapp.ios.window.rootViewController = _sapp.ios.view_ctrl; |
| 4624 | #endif |
| 4625 | [_sapp.ios.window makeKeyAndVisible]; |
| 4626 | |
| 4627 | _sapp.valid = true; |
| 4628 | return YES; |
| 4629 | } |
| 4630 | |
| 4631 | - (void)applicationWillResignActive:(UIApplication *)application { |
| 4632 | if (!_sapp.ios.suspended) { |
| 4633 | _sapp.ios.suspended = true; |
| 4634 | _sapp_ios_app_event(SAPP_EVENTTYPE_SUSPENDED); |
| 4635 | } |
| 4636 | } |
| 4637 | |
| 4638 | - (void)applicationDidBecomeActive:(UIApplication *)application { |
| 4639 | if (_sapp.ios.suspended) { |
| 4640 | _sapp.ios.suspended = false; |
| 4641 | _sapp_ios_app_event(SAPP_EVENTTYPE_RESUMED); |
| 4642 | } |
| 4643 | } |
| 4644 | |
| 4645 | /* NOTE: this method will rarely ever be called, iOS application |
| 4646 | which are terminated by the user are usually killed via signal 9 |
| 4647 | by the operating system. |
| 4648 | */ |
| 4649 | - (void)applicationWillTerminate:(UIApplication *)application { |
| 4650 | _SOKOL_UNUSED(application); |
| 4651 | _sapp_call_cleanup(); |
| 4652 | _sapp_ios_discard_state(); |
| 4653 | _sapp_discard_state(); |
| 4654 | } |
| 4655 | @end |
| 4656 | |
| 4657 | @implementation _sapp_textfield_dlg |
| 4658 | - (void)keyboardWasShown:(NSNotification*)notif { |
| 4659 | _sapp.onscreen_keyboard_shown = true; |
| 4660 | /* query the keyboard's size, and modify the content view's size */ |
| 4661 | if (_sapp.desc.ios_keyboard_resizes_canvas) { |
| 4662 | NSDictionary* info = notif.userInfo; |
| 4663 | CGFloat kbd_h = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; |
| 4664 | CGRect view_frame = UIScreen.mainScreen.bounds; |
| 4665 | view_frame.size.height -= kbd_h; |
| 4666 | _sapp.ios.view.frame = view_frame; |
| 4667 | } |
| 4668 | } |
| 4669 | - (void)keyboardWillBeHidden:(NSNotification*)notif { |
| 4670 | _sapp.onscreen_keyboard_shown = false; |
| 4671 | if (_sapp.desc.ios_keyboard_resizes_canvas) { |
| 4672 | _sapp.ios.view.frame = UIScreen.mainScreen.bounds; |
| 4673 | } |
| 4674 | } |
| 4675 | - (void)keyboardDidChangeFrame:(NSNotification*)notif { |
| 4676 | /* this is for the case when the screen rotation changes while the keyboard is open */ |
| 4677 | if (_sapp.onscreen_keyboard_shown && _sapp.desc.ios_keyboard_resizes_canvas) { |
| 4678 | NSDictionary* info = notif.userInfo; |
| 4679 | CGFloat kbd_h = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; |
| 4680 | CGRect view_frame = UIScreen.mainScreen.bounds; |
| 4681 | view_frame.size.height -= kbd_h; |
| 4682 | _sapp.ios.view.frame = view_frame; |
| 4683 | } |
| 4684 | } |
| 4685 | - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string { |
| 4686 | if (_sapp_events_enabled()) { |
| 4687 | const NSUInteger len = string.length; |
| 4688 | if (len > 0) { |
| 4689 | for (NSUInteger i = 0; i < len; i++) { |
| 4690 | unichar c = [string characterAtIndex:i]; |
| 4691 | if (c >= 32) { |
| 4692 | /* ignore surrogates for now */ |
| 4693 | if ((c < 0xD800) || (c > 0xDFFF)) { |
| 4694 | _sapp_init_event(SAPP_EVENTTYPE_CHAR); |
| 4695 | _sapp.event.char_code = c; |
| 4696 | _sapp_call_event(&_sapp.event); |
| 4697 | } |
| 4698 | } |
| 4699 | if (c <= 32) { |
| 4700 | sapp_keycode k = SAPP_KEYCODE_INVALID; |
| 4701 | switch (c) { |
| 4702 | case 10: k = SAPP_KEYCODE_ENTER; break; |
| 4703 | case 32: k = SAPP_KEYCODE_SPACE; break; |
| 4704 | default: break; |
| 4705 | } |
| 4706 | if (k != SAPP_KEYCODE_INVALID) { |
| 4707 | _sapp_init_event(SAPP_EVENTTYPE_KEY_DOWN); |
| 4708 | _sapp.event.key_code = k; |
| 4709 | _sapp_call_event(&_sapp.event); |
| 4710 | _sapp_init_event(SAPP_EVENTTYPE_KEY_UP); |
| 4711 | _sapp.event.key_code = k; |
| 4712 | _sapp_call_event(&_sapp.event); |
| 4713 | } |
| 4714 | } |
| 4715 | } |
| 4716 | } |
| 4717 | else { |
| 4718 | /* this was a backspace */ |
| 4719 | _sapp_init_event(SAPP_EVENTTYPE_KEY_DOWN); |
| 4720 | _sapp.event.key_code = SAPP_KEYCODE_BACKSPACE; |
| 4721 | _sapp_call_event(&_sapp.event); |
| 4722 | _sapp_init_event(SAPP_EVENTTYPE_KEY_UP); |
| 4723 | _sapp.event.key_code = SAPP_KEYCODE_BACKSPACE; |
| 4724 | _sapp_call_event(&_sapp.event); |
| 4725 | } |
| 4726 | } |
| 4727 | return NO; |
| 4728 | } |
| 4729 | @end |
| 4730 | |
| 4731 | @implementation _sapp_ios_view |
| 4732 | - (void)drawRect:(CGRect)rect { |
| 4733 | _SOKOL_UNUSED(rect); |
| 4734 | #if defined(_SAPP_ANY_GL) |
| 4735 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer); |
| 4736 | #endif |
| 4737 | _sapp_timing_measure(&_sapp.timing); |
| 4738 | @autoreleasepool { |
| 4739 | _sapp_ios_frame(); |
| 4740 | } |
| 4741 | } |
| 4742 | - (BOOL)isOpaque { |
| 4743 | return YES; |
| 4744 | } |
| 4745 | - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent*)event { |
| 4746 | _sapp_ios_touch_event(SAPP_EVENTTYPE_TOUCHES_BEGAN, touches, event); |
| 4747 | } |
| 4748 | - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent*)event { |
| 4749 | _sapp_ios_touch_event(SAPP_EVENTTYPE_TOUCHES_MOVED, touches, event); |
| 4750 | } |
| 4751 | - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent*)event { |
| 4752 | _sapp_ios_touch_event(SAPP_EVENTTYPE_TOUCHES_ENDED, touches, event); |
| 4753 | } |
| 4754 | - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent*)event { |
| 4755 | _sapp_ios_touch_event(SAPP_EVENTTYPE_TOUCHES_CANCELLED, touches, event); |
| 4756 | } |
| 4757 | @end |
| 4758 | #endif /* TARGET_OS_IPHONE */ |
| 4759 | |
| 4760 | #endif /* _SAPP_APPLE */ |
| 4761 | |
| 4762 | // ███████ ███ ███ ███████ ██████ ██████ ██ ██████ ████████ ███████ ███ ██ |
| 4763 | // ██ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ |
| 4764 | // █████ ██ ████ ██ ███████ ██ ██████ ██ ██████ ██ █████ ██ ██ ██ |
| 4765 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 4766 | // ███████ ██ ██ ███████ ██████ ██ ██ ██ ██ ██ ███████ ██ ████ |
| 4767 | // |
| 4768 | // >>emscripten |
| 4769 | #if defined(_SAPP_EMSCRIPTEN) |
| 4770 | |
| 4771 | #if defined(EM_JS_DEPS) |
| 4772 | EM_JS_DEPS(sokol_app, "$withStackSave,$stringToUTF8OnStack"); |
| 4773 | #endif |
| 4774 | |
| 4775 | #ifdef __cplusplus |
| 4776 | extern "C" { |
| 4777 | #endif |
| 4778 | |
| 4779 | typedef void (*_sapp_html5_fetch_callback) (const sapp_html5_fetch_response*); |
| 4780 | |
| 4781 | EMSCRIPTEN_KEEPALIVE void _sapp_emsc_onpaste(const char* str) { |
| 4782 | if (_sapp.clipboard.enabled) { |
| 4783 | _sapp_strcpy(str, _sapp.clipboard.buffer, _sapp.clipboard.buf_size); |
| 4784 | if (_sapp_events_enabled()) { |
| 4785 | _sapp_init_event(SAPP_EVENTTYPE_CLIPBOARD_PASTED); |
| 4786 | _sapp_call_event(&_sapp.event); |
| 4787 | } |
| 4788 | } |
| 4789 | } |
| 4790 | |
| 4791 | /* https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload */ |
| 4792 | EMSCRIPTEN_KEEPALIVE int _sapp_html5_get_ask_leave_site(void) { |
| 4793 | return _sapp.html5_ask_leave_site ? 1 : 0; |
| 4794 | } |
| 4795 | |
| 4796 | EMSCRIPTEN_KEEPALIVE void _sapp_emsc_begin_drop(int num) { |
| 4797 | if (!_sapp.drop.enabled) { |
| 4798 | return; |
| 4799 | } |
| 4800 | if (num < 0) { |
| 4801 | num = 0; |
| 4802 | } |
| 4803 | if (num > _sapp.drop.max_files) { |
| 4804 | num = _sapp.drop.max_files; |
| 4805 | } |
| 4806 | _sapp.drop.num_files = num; |
| 4807 | _sapp_clear_drop_buffer(); |
| 4808 | } |
| 4809 | |
| 4810 | EMSCRIPTEN_KEEPALIVE void _sapp_emsc_drop(int i, const char* name) { |
| 4811 | /* NOTE: name is only the filename part, not a path */ |
| 4812 | if (!_sapp.drop.enabled) { |
| 4813 | return; |
| 4814 | } |
| 4815 | if (0 == name) { |
| 4816 | return; |
| 4817 | } |
| 4818 | SOKOL_ASSERT(_sapp.drop.num_files <= _sapp.drop.max_files); |
| 4819 | if ((i < 0) || (i >= _sapp.drop.num_files)) { |
| 4820 | return; |
| 4821 | } |
| 4822 | if (!_sapp_strcpy(name, _sapp_dropped_file_path_ptr(i), _sapp.drop.max_path_length)) { |
| 4823 | _SAPP_ERROR(DROPPED_FILE_PATH_TOO_LONG); |
| 4824 | _sapp.drop.num_files = 0; |
| 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | EMSCRIPTEN_KEEPALIVE void _sapp_emsc_end_drop(int x, int y, int mods) { |
| 4829 | if (!_sapp.drop.enabled) { |
| 4830 | return; |
| 4831 | } |
| 4832 | if (0 == _sapp.drop.num_files) { |
| 4833 | /* there was an error copying the filenames */ |
| 4834 | _sapp_clear_drop_buffer(); |
| 4835 | return; |
| 4836 | |
| 4837 | } |
| 4838 | if (_sapp_events_enabled()) { |
| 4839 | _sapp.mouse.x = (float)x * _sapp.dpi_scale; |
| 4840 | _sapp.mouse.y = (float)y * _sapp.dpi_scale; |
| 4841 | _sapp.mouse.dx = 0.0f; |
| 4842 | _sapp.mouse.dy = 0.0f; |
| 4843 | _sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED); |
| 4844 | // see sapp_js_add_dragndrop_listeners for mods constants |
| 4845 | if (mods & 1) { _sapp.event.modifiers |= SAPP_MODIFIER_SHIFT; } |
| 4846 | if (mods & 2) { _sapp.event.modifiers |= SAPP_MODIFIER_CTRL; } |
| 4847 | if (mods & 4) { _sapp.event.modifiers |= SAPP_MODIFIER_ALT; } |
| 4848 | if (mods & 8) { _sapp.event.modifiers |= SAPP_MODIFIER_SUPER; } |
| 4849 | _sapp_call_event(&_sapp.event); |
| 4850 | } |
| 4851 | } |
| 4852 | |
| 4853 | EMSCRIPTEN_KEEPALIVE void _sapp_emsc_invoke_fetch_cb(int index, int success, int error_code, _sapp_html5_fetch_callback callback, uint32_t fetched_size, void* buf_ptr, uint32_t buf_size, void* user_data) { |
| 4854 | sapp_html5_fetch_response response; |
| 4855 | _sapp_clear(&response, sizeof(response)); |
| 4856 | response.succeeded = (0 != success); |
| 4857 | response.error_code = (sapp_html5_fetch_error) error_code; |
| 4858 | response.file_index = index; |
| 4859 | response.data.ptr = buf_ptr; |
| 4860 | response.data.size = fetched_size; |
| 4861 | response.buffer.ptr = buf_ptr; |
| 4862 | response.buffer.size = buf_size; |
| 4863 | response.user_data = user_data; |
| 4864 | callback(&response); |
| 4865 | } |
| 4866 | |
| 4867 | #ifdef __cplusplus |
| 4868 | } /* extern "C" */ |
| 4869 | #endif |
| 4870 | |
| 4871 | EM_JS(void, sapp_js_add_beforeunload_listener, (void), { |
| 4872 | Module.sokol_beforeunload = (event) => { |
| 4873 | if (__sapp_html5_get_ask_leave_site() != 0) { |
| 4874 | event.preventDefault(); |
| 4875 | event.returnValue = ' '; |
| 4876 | } |
| 4877 | }; |
| 4878 | window.addEventListener('beforeunload', Module.sokol_beforeunload); |
| 4879 | }); |
| 4880 | |
| 4881 | EM_JS(void, sapp_js_remove_beforeunload_listener, (void), { |
| 4882 | window.removeEventListener('beforeunload', Module.sokol_beforeunload); |
| 4883 | }); |
| 4884 | |
| 4885 | EM_JS(void, sapp_js_add_clipboard_listener, (void), { |
| 4886 | Module.sokol_paste = (event) => { |
| 4887 | const pasted_str = event.clipboardData.getData('text'); |
| 4888 | withStackSave(() => { |
| 4889 | const cstr = stringToUTF8OnStack(pasted_str); |
| 4890 | __sapp_emsc_onpaste(cstr); |
| 4891 | }); |
| 4892 | }; |
| 4893 | window.addEventListener('paste', Module.sokol_paste); |
| 4894 | }); |
| 4895 | |
| 4896 | EM_JS(void, sapp_js_remove_clipboard_listener, (void), { |
| 4897 | window.removeEventListener('paste', Module.sokol_paste); |
| 4898 | }); |
| 4899 | |
| 4900 | EM_JS(void, sapp_js_write_clipboard, (const char* c_str), { |
| 4901 | const str = UTF8ToString(c_str); |
| 4902 | const ta = document.createElement('textarea'); |
| 4903 | ta.setAttribute('autocomplete', 'off'); |
| 4904 | ta.setAttribute('autocorrect', 'off'); |
| 4905 | ta.setAttribute('autocapitalize', 'off'); |
| 4906 | ta.setAttribute('spellcheck', 'false'); |
| 4907 | ta.style.left = -100 + 'px'; |
| 4908 | ta.style.top = -100 + 'px'; |
| 4909 | ta.style.height = 1; |
| 4910 | ta.style.width = 1; |
| 4911 | ta.value = str; |
| 4912 | document.body.appendChild(ta); |
| 4913 | ta.select(); |
| 4914 | document.execCommand('copy'); |
| 4915 | document.body.removeChild(ta); |
| 4916 | }); |
| 4917 | |
| 4918 | _SOKOL_PRIVATE void _sapp_emsc_set_clipboard_string(const char* str) { |
| 4919 | sapp_js_write_clipboard(str); |
| 4920 | } |
| 4921 | |
| 4922 | EM_JS(void, sapp_js_add_dragndrop_listeners, (const char* canvas_name_cstr), { |
| 4923 | Module.sokol_drop_files = []; |
| 4924 | const canvas_name = UTF8ToString(canvas_name_cstr); |
| 4925 | const canvas = document.getElementById(canvas_name); |
| 4926 | Module.sokol_dragenter = (event) => { |
| 4927 | event.stopPropagation(); |
| 4928 | event.preventDefault(); |
| 4929 | }; |
| 4930 | Module.sokol_dragleave = (event) => { |
| 4931 | event.stopPropagation(); |
| 4932 | event.preventDefault(); |
| 4933 | }; |
| 4934 | Module.sokol_dragover = (event) => { |
| 4935 | event.stopPropagation(); |
| 4936 | event.preventDefault(); |
| 4937 | }; |
| 4938 | Module.sokol_drop = (event) => { |
| 4939 | event.stopPropagation(); |
| 4940 | event.preventDefault(); |
| 4941 | const files = event.dataTransfer.files; |
| 4942 | Module.sokol_dropped_files = files; |
| 4943 | __sapp_emsc_begin_drop(files.length); |
| 4944 | for (let i = 0; i < files.length; i++) { |
| 4945 | withStackSave(() => { |
| 4946 | const cstr = stringToUTF8OnStack(files[i].name); |
| 4947 | __sapp_emsc_drop(i, cstr); |
| 4948 | }); |
| 4949 | } |
| 4950 | let mods = 0; |
| 4951 | if (event.shiftKey) { mods |= 1; } |
| 4952 | if (event.ctrlKey) { mods |= 2; } |
| 4953 | if (event.altKey) { mods |= 4; } |
| 4954 | if (event.metaKey) { mods |= 8; } |
| 4955 | // FIXME? see computation of targetX/targetY in emscripten via getClientBoundingRect |
| 4956 | __sapp_emsc_end_drop(event.clientX, event.clientY, mods); |
| 4957 | }; |
| 4958 | canvas.addEventListener('dragenter', Module.sokol_dragenter, false); |
| 4959 | canvas.addEventListener('dragleave', Module.sokol_dragleave, false); |
| 4960 | canvas.addEventListener('dragover', Module.sokol_dragover, false); |
| 4961 | canvas.addEventListener('drop', Module.sokol_drop, false); |
| 4962 | }); |
| 4963 | |
| 4964 | EM_JS(uint32_t, sapp_js_dropped_file_size, (int index), { |
| 4965 | \x2F\x2A\x2A @suppress {missingProperties} \x2A\x2F |
| 4966 | const files = Module.sokol_dropped_files; |
| 4967 | if ((index < 0) || (index >= files.length)) { |
| 4968 | return 0; |
| 4969 | } |
| 4970 | else { |
| 4971 | return files[index].size; |
| 4972 | } |
| 4973 | }); |
| 4974 | |
| 4975 | EM_JS(void, sapp_js_fetch_dropped_file, (int index, _sapp_html5_fetch_callback callback, void* buf_ptr, uint32_t buf_size, void* user_data), { |
| 4976 | const reader = new FileReader(); |
| 4977 | reader.onload = (loadEvent) => { |
| 4978 | const content = loadEvent.target.result; |
| 4979 | if (content.byteLength > buf_size) { |
| 4980 | // SAPP_HTML5_FETCH_ERROR_BUFFER_TOO_SMALL |
| 4981 | __sapp_emsc_invoke_fetch_cb(index, 0, 1, callback, 0, buf_ptr, buf_size, user_data); |
| 4982 | } |
| 4983 | else { |
| 4984 | HEAPU8.set(new Uint8Array(content), buf_ptr); |
| 4985 | __sapp_emsc_invoke_fetch_cb(index, 1, 0, callback, content.byteLength, buf_ptr, buf_size, user_data); |
| 4986 | } |
| 4987 | }; |
| 4988 | reader.onerror = () => { |
| 4989 | // SAPP_HTML5_FETCH_ERROR_OTHER |
| 4990 | __sapp_emsc_invoke_fetch_cb(index, 0, 2, callback, 0, buf_ptr, buf_size, user_data); |
| 4991 | }; |
| 4992 | \x2F\x2A\x2A @suppress {missingProperties} \x2A\x2F |
| 4993 | const files = Module.sokol_dropped_files; |
| 4994 | reader.readAsArrayBuffer(files[index]); |
| 4995 | }); |
| 4996 | |
| 4997 | EM_JS(void, sapp_js_remove_dragndrop_listeners, (const char* canvas_name_cstr), { |
| 4998 | const canvas_name = UTF8ToString(canvas_name_cstr); |
| 4999 | const canvas = document.getElementById(canvas_name); |
| 5000 | canvas.removeEventListener('dragenter', Module.sokol_dragenter); |
| 5001 | canvas.removeEventListener('dragleave', Module.sokol_dragleave); |
| 5002 | canvas.removeEventListener('dragover', Module.sokol_dragover); |
| 5003 | canvas.removeEventListener('drop', Module.sokol_drop); |
| 5004 | }); |
| 5005 | |
| 5006 | EM_JS(void, sapp_js_init, (const char* c_str_target), { |
| 5007 | // lookup and store canvas object by name |
| 5008 | const target_str = UTF8ToString(c_str_target); |
| 5009 | Module.sapp_emsc_target = document.getElementById(target_str); |
| 5010 | if (!Module.sapp_emsc_target) { |
| 5011 | console.log("sokol_app.h: invalid target:" + target_str); |
| 5012 | } |
| 5013 | if (!Module.sapp_emsc_target.requestPointerLock) { |
| 5014 | console.log("sokol_app.h: target doesn't support requestPointerLock:" + target_str); |
| 5015 | } |
| 5016 | }); |
| 5017 | |
| 5018 | _SOKOL_PRIVATE EM_BOOL _sapp_emsc_pointerlockchange_cb(int emsc_type, const EmscriptenPointerlockChangeEvent* emsc_event, void* user_data) { |
| 5019 | _SOKOL_UNUSED(emsc_type); |
| 5020 | _SOKOL_UNUSED(user_data); |
| 5021 | _sapp.mouse.locked = emsc_event->isActive; |
| 5022 | return EM_TRUE; |
| 5023 | } |
| 5024 | |
| 5025 | _SOKOL_PRIVATE EM_BOOL _sapp_emsc_pointerlockerror_cb(int emsc_type, const void* reserved, void* user_data) { |
| 5026 | _SOKOL_UNUSED(emsc_type); |
| 5027 | _SOKOL_UNUSED(reserved); |
| 5028 | _SOKOL_UNUSED(user_data); |
| 5029 | _sapp.mouse.locked = false; |
| 5030 | _sapp.emsc.mouse_lock_requested = false; |
| 5031 | return true; |
| 5032 | } |
| 5033 | |
| 5034 | EM_JS(void, sapp_js_request_pointerlock, (void), { |
| 5035 | if (Module.sapp_emsc_target) { |
| 5036 | if (Module.sapp_emsc_target.requestPointerLock) { |
| 5037 | Module.sapp_emsc_target.requestPointerLock(); |
| 5038 | } |
| 5039 | } |
| 5040 | }); |
| 5041 | |
| 5042 | EM_JS(void, sapp_js_exit_pointerlock, (void), { |
| 5043 | if (document.exitPointerLock) { |
| 5044 | document.exitPointerLock(); |
| 5045 | } |
| 5046 | }); |
| 5047 | |
| 5048 | _SOKOL_PRIVATE void _sapp_emsc_lock_mouse(bool lock) { |
| 5049 | if (lock) { |
| 5050 | /* request mouse-lock during event handler invocation (see _sapp_emsc_update_mouse_lock_state) */ |
| 5051 | _sapp.emsc.mouse_lock_requested = true; |
| 5052 | } |
| 5053 | else { |
| 5054 | /* NOTE: the _sapp.mouse_locked state will be set in the pointerlockchange callback */ |
| 5055 | _sapp.emsc.mouse_lock_requested = false; |
| 5056 | sapp_js_exit_pointerlock(); |
| 5057 | } |
| 5058 | } |
| 5059 | |
| 5060 | /* called from inside event handlers to check if mouse lock had been requested, |
| 5061 | and if yes, actually enter mouse lock. |
| 5062 | */ |
| 5063 | _SOKOL_PRIVATE void _sapp_emsc_update_mouse_lock_state(void) { |
| 5064 | if (_sapp.emsc.mouse_lock_requested) { |
| 5065 | _sapp.emsc.mouse_lock_requested = false; |
| 5066 | sapp_js_request_pointerlock(); |
| 5067 | } |
| 5068 | } |
| 5069 | |
| 5070 | // set mouse cursor type |
| 5071 | EM_JS(void, sapp_js_set_cursor, (int cursor_type, int shown), { |
| 5072 | if (Module.sapp_emsc_target) { |
| 5073 | let cursor; |
| 5074 | if (shown === 0) { |
| 5075 | cursor = "none"; |
| 5076 | } |
| 5077 | else switch (cursor_type) { |
| 5078 | case 0: cursor = "auto"; break; // SAPP_MOUSECURSOR_DEFAULT |
| 5079 | case 1: cursor = "default"; break; // SAPP_MOUSECURSOR_ARROW |
| 5080 | case 2: cursor = "text"; break; // SAPP_MOUSECURSOR_IBEAM |
| 5081 | case 3: cursor = "crosshair"; break; // SAPP_MOUSECURSOR_CROSSHAIR |
| 5082 | case 4: cursor = "pointer"; break; // SAPP_MOUSECURSOR_POINTING_HAND |
| 5083 | case 5: cursor = "ew-resize"; break; // SAPP_MOUSECURSOR_RESIZE_EW |
| 5084 | case 6: cursor = "ns-resize"; break; // SAPP_MOUSECURSOR_RESIZE_NS |
| 5085 | case 7: cursor = "nwse-resize"; break; // SAPP_MOUSECURSOR_RESIZE_NWSE |
| 5086 | case 8: cursor = "nesw-resize"; break; // SAPP_MOUSECURSOR_RESIZE_NESW |
| 5087 | case 9: cursor = "all-scroll"; break; // SAPP_MOUSECURSOR_RESIZE_ALL |
| 5088 | case 10: cursor = "not-allowed"; break; // SAPP_MOUSECURSOR_NOT_ALLOWED |
| 5089 | default: cursor = "auto"; break; |
| 5090 | } |
| 5091 | Module.sapp_emsc_target.style.cursor = cursor; |
| 5092 | } |
| 5093 | }); |
| 5094 | |
| 5095 | _SOKOL_PRIVATE void _sapp_emsc_update_cursor(sapp_mouse_cursor cursor, bool shown) { |
| 5096 | SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM)); |
| 5097 | sapp_js_set_cursor((int)cursor, shown ? 1 : 0); |
| 5098 | } |
| 5099 | |
| 5100 | /* JS helper functions to update browser tab favicon */ |
| 5101 | EM_JS(void, sapp_js_clear_favicon, (void), { |
| 5102 | const link = document.getElementById('sokol-app-favicon'); |
| 5103 | if (link) { |
| 5104 | document.head.removeChild(link); |
| 5105 | } |
| 5106 | }); |
| 5107 | |
| 5108 | EM_JS(void, sapp_js_set_favicon, (int w, int h, const uint8_t* pixels), { |
| 5109 | const canvas = document.createElement('canvas'); |
| 5110 | canvas.width = w; |
| 5111 | canvas.height = h; |
| 5112 | const ctx = canvas.getContext('2d'); |
| 5113 | const img_data = ctx.createImageData(w, h); |
| 5114 | img_data.data.set(HEAPU8.subarray(pixels, pixels + w*h*4)); |
| 5115 | ctx.putImageData(img_data, 0, 0); |
| 5116 | const new_link = document.createElement('link'); |
| 5117 | new_link.id = 'sokol-app-favicon'; |
| 5118 | new_link.rel = 'shortcut icon'; |
| 5119 | new_link.href = canvas.toDataURL(); |
| 5120 | document.head.appendChild(new_link); |
| 5121 | }); |
| 5122 | |
| 5123 | _SOKOL_PRIVATE void _sapp_emsc_set_icon(const sapp_icon_desc* icon_desc, int num_images) { |
| 5124 | SOKOL_ASSERT((num_images > 0) && (num_images <= SAPP_MAX_ICONIMAGES)); |
| 5125 | sapp_js_clear_favicon(); |
| 5126 | // find the best matching image candidate for 16x16 pixels |
| 5127 | int img_index = _sapp_image_bestmatch(icon_desc->images, num_images, 16, 16); |
| 5128 | const sapp_image_desc* img_desc = &icon_desc->images[img_index]; |
| 5129 | sapp_js_set_favicon(img_desc->width, img_desc->height, (const uint8_t*) img_desc->pixels.ptr); |
| 5130 | } |
| 5131 | |
| 5132 | _SOKOL_PRIVATE uint32_t _sapp_emsc_mouse_button_mods(uint16_t buttons) { |
| 5133 | uint32_t m = 0; |
| 5134 | if (0 != (buttons & (1<<0))) { m |= SAPP_MODIFIER_LMB; } |
| 5135 | if (0 != (buttons & (1<<1))) { m |= SAPP_MODIFIER_RMB; } // not a bug |
| 5136 | if (0 != (buttons & (1<<2))) { m |= SAPP_MODIFIER_MMB; } // not a bug |
| 5137 | return m; |
| 5138 | } |
| 5139 | |
| 5140 | _SOKOL_PRIVATE uint32_t _sapp_emsc_mouse_event_mods(const EmscriptenMouseEvent* ev) { |
| 5141 | uint32_t m = 0; |
| 5142 | if (ev->ctrlKey) { m |= SAPP_MODIFIER_CTRL; } |
| 5143 | if (ev->shiftKey) { m |= SAPP_MODIFIER_SHIFT; } |
| 5144 | if (ev->altKey) { m |= SAPP_MODIFIER_ALT; } |
| 5145 | if (ev->metaKey) { m |= SAPP_MODIFIER_SUPER; } |
| 5146 | m |= _sapp_emsc_mouse_button_mods(_sapp.emsc.mouse_buttons); |
| 5147 | return m; |
| 5148 | } |
| 5149 | |
| 5150 | _SOKOL_PRIVATE uint32_t _sapp_emsc_key_event_mods(const EmscriptenKeyboardEvent* ev) { |
| 5151 | uint32_t m = 0; |
| 5152 | if (ev->ctrlKey) { m |= SAPP_MODIFIER_CTRL; } |
| 5153 | if (ev->shiftKey) { m |= SAPP_MODIFIER_SHIFT; } |
| 5154 | if (ev->altKey) { m |= SAPP_MODIFIER_ALT; } |
| 5155 | if (ev->metaKey) { m |= SAPP_MODIFIER_SUPER; } |
| 5156 | m |= _sapp_emsc_mouse_button_mods(_sapp.emsc.mouse_buttons); |
| 5157 | return m; |
| 5158 | } |
| 5159 | |
| 5160 | _SOKOL_PRIVATE uint32_t _sapp_emsc_touch_event_mods(const EmscriptenTouchEvent* ev) { |
| 5161 | uint32_t m = 0; |
| 5162 | if (ev->ctrlKey) { m |= SAPP_MODIFIER_CTRL; } |
| 5163 | if (ev->shiftKey) { m |= SAPP_MODIFIER_SHIFT; } |
| 5164 | if (ev->altKey) { m |= SAPP_MODIFIER_ALT; } |
| 5165 | if (ev->metaKey) { m |= SAPP_MODIFIER_SUPER; } |
| 5166 | m |= _sapp_emsc_mouse_button_mods(_sapp.emsc.mouse_buttons); |
| 5167 | return m; |
| 5168 | } |
| 5169 | |
| 5170 | #if defined(SOKOL_WGPU) |
| 5171 | _SOKOL_PRIVATE void _sapp_emsc_wgpu_size_changed(void); |
| 5172 | #endif |
| 5173 | |
| 5174 | _SOKOL_PRIVATE EM_BOOL _sapp_emsc_size_changed(int event_type, const EmscriptenUiEvent* ui_event, void* user_data) { |
| 5175 | _SOKOL_UNUSED(event_type); |
| 5176 | _SOKOL_UNUSED(user_data); |
| 5177 | double w, h; |
| 5178 | emscripten_get_element_css_size(_sapp.html5_canvas_selector, &w, &h); |
| 5179 | /* The above method might report zero when toggling HTML5 fullscreen, |
| 5180 | in that case use the window's inner width reported by the |
| 5181 | emscripten event. This works ok when toggling *into* fullscreen |
| 5182 | but doesn't properly restore the previous canvas size when switching |
| 5183 | back from fullscreen. |
| 5184 | |
| 5185 | In general, due to the HTML5's fullscreen API's flaky nature it is |
| 5186 | recommended to use 'soft fullscreen' (stretching the WebGL canvas |
| 5187 | over the browser windows client rect) with a CSS definition like this: |
| 5188 | |
| 5189 | position: absolute; |
| 5190 | top: 0px; |
| 5191 | left: 0px; |
| 5192 | margin: 0px; |
| 5193 | border: 0; |
| 5194 | width: 100%; |
| 5195 | height: 100%; |
| 5196 | overflow: hidden; |
| 5197 | display: block; |
| 5198 | */ |
| 5199 | if (w < 1.0) { |
| 5200 | w = ui_event->windowInnerWidth; |
| 5201 | } |
| 5202 | else { |
| 5203 | _sapp.window_width = (int)roundf(w); |
| 5204 | } |
| 5205 | if (h < 1.0) { |
| 5206 | h = ui_event->windowInnerHeight; |
| 5207 | } |
| 5208 | else { |
| 5209 | _sapp.window_height = (int)roundf(h); |
| 5210 | } |
| 5211 | if (_sapp.desc.high_dpi) { |
| 5212 | _sapp.dpi_scale = emscripten_get_device_pixel_ratio(); |
| 5213 | } |
| 5214 | _sapp.framebuffer_width = (int)roundf(w * _sapp.dpi_scale); |
| 5215 | _sapp.framebuffer_height = (int)roundf(h * _sapp.dpi_scale); |
| 5216 | SOKOL_ASSERT((_sapp.framebuffer_width > 0) && (_sapp.framebuffer_height > 0)); |
| 5217 | emscripten_set_canvas_element_size(_sapp.html5_canvas_selector, _sapp.framebuffer_width, _sapp.framebuffer_height); |
| 5218 | #if defined(SOKOL_WGPU) |
| 5219 | // on WebGPU: recreate size-dependent rendering surfaces |
| 5220 | _sapp_emsc_wgpu_size_changed(); |
| 5221 | #endif |
| 5222 | if (_sapp_events_enabled()) { |
| 5223 | _sapp_init_event(SAPP_EVENTTYPE_RESIZED); |
| 5224 | _sapp_call_event(&_sapp.event); |
| 5225 | } |
| 5226 | return true; |
| 5227 | } |
| 5228 | |
| 5229 | _SOKOL_PRIVATE EM_BOOL _sapp_emsc_mouse_cb(int emsc_type, const EmscriptenMouseEvent* emsc_event, void* user_data) { |
| 5230 | _SOKOL_UNUSED(user_data); |
| 5231 | bool consume_event = !_sapp.desc.html5_bubble_mouse_events; |
| 5232 | _sapp.emsc.mouse_buttons = emsc_event->buttons; |
| 5233 | if (_sapp.mouse.locked) { |
| 5234 | _sapp.mouse.dx = (float) emsc_event->movementX; |
| 5235 | _sapp.mouse.dy = (float) emsc_event->movementY; |
| 5236 | } else { |
| 5237 | float new_x = emsc_event->targetX * _sapp.dpi_scale; |
| 5238 | float new_y = emsc_event->targetY * _sapp.dpi_scale; |
| 5239 | if (_sapp.mouse.pos_valid) { |
| 5240 | _sapp.mouse.dx = new_x - _sapp.mouse.x; |
| 5241 | _sapp.mouse.dy = new_y - _sapp.mouse.y; |
| 5242 | } |
| 5243 | _sapp.mouse.x = new_x; |
| 5244 | _sapp.mouse.y = new_y; |
| 5245 | _sapp.mouse.pos_valid = true; |
| 5246 | } |
| 5247 | if (_sapp_events_enabled() && (emsc_event->button >= 0) && (emsc_event->button < SAPP_MAX_MOUSEBUTTONS)) { |
| 5248 | sapp_event_type type; |
| 5249 | bool is_button_event = false; |
| 5250 | bool clear_dxdy = false; |
| 5251 | switch (emsc_type) { |
| 5252 | case EMSCRIPTEN_EVENT_MOUSEDOWN: |
| 5253 | type = SAPP_EVENTTYPE_MOUSE_DOWN; |
| 5254 | is_button_event = true; |
| 5255 | break; |
| 5256 | case EMSCRIPTEN_EVENT_MOUSEUP: |
| 5257 | type = SAPP_EVENTTYPE_MOUSE_UP; |
| 5258 | is_button_event = true; |
| 5259 | break; |
| 5260 | case EMSCRIPTEN_EVENT_MOUSEMOVE: |
| 5261 | type = SAPP_EVENTTYPE_MOUSE_MOVE; |
| 5262 | break; |
| 5263 | case EMSCRIPTEN_EVENT_MOUSEENTER: |
| 5264 | type = SAPP_EVENTTYPE_MOUSE_ENTER; |
| 5265 | clear_dxdy = true; |
| 5266 | break; |
| 5267 | case EMSCRIPTEN_EVENT_MOUSELEAVE: |
| 5268 | type = SAPP_EVENTTYPE_MOUSE_LEAVE; |
| 5269 | clear_dxdy = true; |
| 5270 | break; |
| 5271 | default: |
| 5272 | type = SAPP_EVENTTYPE_INVALID; |
| 5273 | break; |
| 5274 | } |
| 5275 | if (clear_dxdy) { |
| 5276 | _sapp.mouse.dx = 0.0f; |
| 5277 | _sapp.mouse.dy = 0.0f; |
| 5278 | } |
| 5279 | if (type != SAPP_EVENTTYPE_INVALID) { |
| 5280 | _sapp_init_event(type); |
| 5281 | _sapp.event.modifiers = _sapp_emsc_mouse_event_mods(emsc_event); |
| 5282 | if (is_button_event) { |
| 5283 | switch (emsc_event->button) { |
| 5284 | case 0: _sapp.event.mouse_button = SAPP_MOUSEBUTTON_LEFT; break; |
| 5285 | case 1: _sapp.event.mouse_button = SAPP_MOUSEBUTTON_MIDDLE; break; |
| 5286 | case 2: _sapp.event.mouse_button = SAPP_MOUSEBUTTON_RIGHT; break; |
| 5287 | default: _sapp.event.mouse_button = (sapp_mousebutton)emsc_event->button; break; |
| 5288 | } |
| 5289 | } else { |
| 5290 | _sapp.event.mouse_button = SAPP_MOUSEBUTTON_INVALID; |
| 5291 | } |
| 5292 | consume_event |= _sapp_call_event(&_sapp.event); |
| 5293 | } |
| 5294 | // mouse lock can only be activated in mouse button events (not in move, enter or leave) |
| 5295 | if (is_button_event) { |
| 5296 | _sapp_emsc_update_mouse_lock_state(); |
| 5297 | } |
| 5298 | } |
| 5299 | return consume_event; |
| 5300 | } |
| 5301 | |
| 5302 | _SOKOL_PRIVATE EM_BOOL _sapp_emsc_wheel_cb(int emsc_type, const EmscriptenWheelEvent* emsc_event, void* user_data) { |
| 5303 | _SOKOL_UNUSED(emsc_type); |
| 5304 | _SOKOL_UNUSED(user_data); |
| 5305 | bool consume_event = !_sapp.desc.html5_bubble_wheel_events; |
| 5306 | _sapp.emsc.mouse_buttons = emsc_event->mouse.buttons; |
| 5307 | if (_sapp_events_enabled()) { |
| 5308 | _sapp_init_event(SAPP_EVENTTYPE_MOUSE_SCROLL); |
| 5309 | _sapp.event.modifiers = _sapp_emsc_mouse_event_mods(&emsc_event->mouse); |
| 5310 | /* see https://github.com/floooh/sokol/issues/339 */ |
| 5311 | float scale; |
| 5312 | switch (emsc_event->deltaMode) { |
| 5313 | case DOM_DELTA_PIXEL: scale = -0.04f; break; |
| 5314 | case DOM_DELTA_LINE: scale = -1.33f; break; |
| 5315 | case DOM_DELTA_PAGE: scale = -10.0f; break; // FIXME: this is a guess |
| 5316 | default: scale = -0.1f; break; // shouldn't happen |
| 5317 | } |
| 5318 | _sapp.event.scroll_x = scale * (float)emsc_event->deltaX; |
| 5319 | _sapp.event.scroll_y = scale * (float)emsc_event->deltaY; |
| 5320 | consume_event |= _sapp_call_event(&_sapp.event); |
| 5321 | } |
| 5322 | _sapp_emsc_update_mouse_lock_state(); |
| 5323 | return consume_event; |
| 5324 | } |
| 5325 | |
| 5326 | static struct { |
| 5327 | const char* str; |
| 5328 | sapp_keycode code; |
| 5329 | } _sapp_emsc_keymap[] = { |
| 5330 | { "Backspace", SAPP_KEYCODE_BACKSPACE }, |
| 5331 | { "Tab", SAPP_KEYCODE_TAB }, |
| 5332 | { "Enter", SAPP_KEYCODE_ENTER }, |
| 5333 | { "ShiftLeft", SAPP_KEYCODE_LEFT_SHIFT }, |
| 5334 | { "ShiftRight", SAPP_KEYCODE_RIGHT_SHIFT }, |
| 5335 | { "ControlLeft", SAPP_KEYCODE_LEFT_CONTROL }, |
| 5336 | { "ControlRight", SAPP_KEYCODE_RIGHT_CONTROL }, |
| 5337 | { "AltLeft", SAPP_KEYCODE_LEFT_ALT }, |
| 5338 | { "AltRight", SAPP_KEYCODE_RIGHT_ALT }, |
| 5339 | { "Pause", SAPP_KEYCODE_PAUSE }, |
| 5340 | { "CapsLock", SAPP_KEYCODE_CAPS_LOCK }, |
| 5341 | { "Escape", SAPP_KEYCODE_ESCAPE }, |
| 5342 | { "Space", SAPP_KEYCODE_SPACE }, |
| 5343 | { "PageUp", SAPP_KEYCODE_PAGE_UP }, |
| 5344 | { "PageDown", SAPP_KEYCODE_PAGE_DOWN }, |
| 5345 | { "End", SAPP_KEYCODE_END }, |
| 5346 | { "Home", SAPP_KEYCODE_HOME }, |
| 5347 | { "ArrowLeft", SAPP_KEYCODE_LEFT }, |
| 5348 | { "ArrowUp", SAPP_KEYCODE_UP }, |
| 5349 | { "ArrowRight", SAPP_KEYCODE_RIGHT }, |
| 5350 | { "ArrowDown", SAPP_KEYCODE_DOWN }, |
| 5351 | { "PrintScreen", SAPP_KEYCODE_PRINT_SCREEN }, |
| 5352 | { "Insert", SAPP_KEYCODE_INSERT }, |
| 5353 | { "Delete", SAPP_KEYCODE_DELETE }, |
| 5354 | { "Digit0", SAPP_KEYCODE_0 }, |
| 5355 | { "Digit1", SAPP_KEYCODE_1 }, |
| 5356 | { "Digit2", SAPP_KEYCODE_2 }, |
| 5357 | { "Digit3", SAPP_KEYCODE_3 }, |
| 5358 | { "Digit4", SAPP_KEYCODE_4 }, |
| 5359 | { "Digit5", SAPP_KEYCODE_5 }, |
| 5360 | { "Digit6", SAPP_KEYCODE_6 }, |
| 5361 | { "Digit7", SAPP_KEYCODE_7 }, |
| 5362 | { "Digit8", SAPP_KEYCODE_8 }, |
| 5363 | { "Digit9", SAPP_KEYCODE_9 }, |
| 5364 | { "KeyA", SAPP_KEYCODE_A }, |
| 5365 | { "KeyB", SAPP_KEYCODE_B }, |
| 5366 | { "KeyC", SAPP_KEYCODE_C }, |
| 5367 | { "KeyD", SAPP_KEYCODE_D }, |
| 5368 | { "KeyE", SAPP_KEYCODE_E }, |
| 5369 | { "KeyF", SAPP_KEYCODE_F }, |
| 5370 | { "KeyG", SAPP_KEYCODE_G }, |
| 5371 | { "KeyH", SAPP_KEYCODE_H }, |
| 5372 | { "KeyI", SAPP_KEYCODE_I }, |
| 5373 | { "KeyJ", SAPP_KEYCODE_J }, |
| 5374 | { "KeyK", SAPP_KEYCODE_K }, |
| 5375 | { "KeyL", SAPP_KEYCODE_L }, |
| 5376 | { "KeyM", SAPP_KEYCODE_M }, |
| 5377 | { "KeyN", SAPP_KEYCODE_N }, |
| 5378 | { "KeyO", SAPP_KEYCODE_O }, |
| 5379 | { "KeyP", SAPP_KEYCODE_P }, |
| 5380 | { "KeyQ", SAPP_KEYCODE_Q }, |
| 5381 | { "KeyR", SAPP_KEYCODE_R }, |
| 5382 | { "KeyS", SAPP_KEYCODE_S }, |
| 5383 | { "KeyT", SAPP_KEYCODE_T }, |
| 5384 | { "KeyU", SAPP_KEYCODE_U }, |
| 5385 | { "KeyV", SAPP_KEYCODE_V }, |
| 5386 | { "KeyW", SAPP_KEYCODE_W }, |
| 5387 | { "KeyX", SAPP_KEYCODE_X }, |
| 5388 | { "KeyY", SAPP_KEYCODE_Y }, |
| 5389 | { "KeyZ", SAPP_KEYCODE_Z }, |
| 5390 | { "MetaLeft", SAPP_KEYCODE_LEFT_SUPER }, |
| 5391 | { "MetaRight", SAPP_KEYCODE_RIGHT_SUPER }, |
| 5392 | { "Numpad0", SAPP_KEYCODE_KP_0 }, |
| 5393 | { "Numpad1", SAPP_KEYCODE_KP_1 }, |
| 5394 | { "Numpad2", SAPP_KEYCODE_KP_2 }, |
| 5395 | { "Numpad3", SAPP_KEYCODE_KP_3 }, |
| 5396 | { "Numpad4", SAPP_KEYCODE_KP_4 }, |
| 5397 | { "Numpad5", SAPP_KEYCODE_KP_5 }, |
| 5398 | { "Numpad6", SAPP_KEYCODE_KP_6 }, |
| 5399 | { "Numpad7", SAPP_KEYCODE_KP_7 }, |
| 5400 | { "Numpad8", SAPP_KEYCODE_KP_8 }, |
| 5401 | { "Numpad9", SAPP_KEYCODE_KP_9 }, |
| 5402 | { "NumpadMultiply", SAPP_KEYCODE_KP_MULTIPLY }, |
| 5403 | { "NumpadAdd", SAPP_KEYCODE_KP_ADD }, |
| 5404 | { "NumpadSubtract", SAPP_KEYCODE_KP_SUBTRACT }, |
| 5405 | { "NumpadDecimal", SAPP_KEYCODE_KP_DECIMAL }, |
| 5406 | { "NumpadDivide", SAPP_KEYCODE_KP_DIVIDE }, |
| 5407 | { "F1", SAPP_KEYCODE_F1 }, |
| 5408 | { "F2", SAPP_KEYCODE_F2 }, |
| 5409 | { "F3", SAPP_KEYCODE_F3 }, |
| 5410 | { "F4", SAPP_KEYCODE_F4 }, |
| 5411 | { "F5", SAPP_KEYCODE_F5 }, |
| 5412 | { "F6", SAPP_KEYCODE_F6 }, |
| 5413 | { "F7", SAPP_KEYCODE_F7 }, |
| 5414 | { "F8", SAPP_KEYCODE_F8 }, |
| 5415 | { "F9", SAPP_KEYCODE_F9 }, |
| 5416 | { "F10", SAPP_KEYCODE_F10 }, |
| 5417 | { "F11", SAPP_KEYCODE_F11 }, |
| 5418 | { "F12", SAPP_KEYCODE_F12 }, |
| 5419 | { "NumLock", SAPP_KEYCODE_NUM_LOCK }, |
| 5420 | { "ScrollLock", SAPP_KEYCODE_SCROLL_LOCK }, |
| 5421 | { "Semicolon", SAPP_KEYCODE_SEMICOLON }, |
| 5422 | { "Equal", SAPP_KEYCODE_EQUAL }, |
| 5423 | { "Comma", SAPP_KEYCODE_COMMA }, |
| 5424 | { "Minus", SAPP_KEYCODE_MINUS }, |
| 5425 | { "Period", SAPP_KEYCODE_PERIOD }, |
| 5426 | { "Slash", SAPP_KEYCODE_SLASH }, |
| 5427 | { "Backquote", SAPP_KEYCODE_GRAVE_ACCENT }, |
| 5428 | { "BracketLeft", SAPP_KEYCODE_LEFT_BRACKET }, |
| 5429 | { "Backslash", SAPP_KEYCODE_BACKSLASH }, |
| 5430 | { "BracketRight", SAPP_KEYCODE_RIGHT_BRACKET }, |
| 5431 | { "Quote", SAPP_KEYCODE_GRAVE_ACCENT }, // FIXME: ??? |
| 5432 | { 0, SAPP_KEYCODE_INVALID }, |
| 5433 | }; |
| 5434 | |
| 5435 | _SOKOL_PRIVATE sapp_keycode _sapp_emsc_translate_key(const char* str) { |
| 5436 | int i = 0; |
| 5437 | const char* keystr; |
| 5438 | while (( keystr = _sapp_emsc_keymap[i].str )) { |
| 5439 | if (0 == strcmp(str, keystr)) { |
| 5440 | return _sapp_emsc_keymap[i].code; |
| 5441 | } |
| 5442 | i += 1; |
| 5443 | } |
| 5444 | return SAPP_KEYCODE_INVALID; |
| 5445 | } |
| 5446 | |
| 5447 | // returns true if the key code is a 'character key', this is used to decide |
| 5448 | // if a key event needs to bubble up to create a char event |
| 5449 | _SOKOL_PRIVATE bool _sapp_emsc_is_char_key(sapp_keycode key_code) { |
| 5450 | return key_code < SAPP_KEYCODE_WORLD_1; |
| 5451 | } |
| 5452 | |
| 5453 | _SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboardEvent* emsc_event, void* user_data) { |
| 5454 | _SOKOL_UNUSED(user_data); |
| 5455 | bool consume_event = false; |
| 5456 | if (_sapp_events_enabled()) { |
| 5457 | sapp_event_type type; |
| 5458 | switch (emsc_type) { |
| 5459 | case EMSCRIPTEN_EVENT_KEYDOWN: |
| 5460 | type = SAPP_EVENTTYPE_KEY_DOWN; |
| 5461 | break; |
| 5462 | case EMSCRIPTEN_EVENT_KEYUP: |
| 5463 | type = SAPP_EVENTTYPE_KEY_UP; |
| 5464 | break; |
| 5465 | case EMSCRIPTEN_EVENT_KEYPRESS: |
| 5466 | type = SAPP_EVENTTYPE_CHAR; |
| 5467 | break; |
| 5468 | default: |
| 5469 | type = SAPP_EVENTTYPE_INVALID; |
| 5470 | break; |
| 5471 | } |
| 5472 | if (type != SAPP_EVENTTYPE_INVALID) { |
| 5473 | bool send_keyup_followup = false; |
| 5474 | _sapp_init_event(type); |
| 5475 | _sapp.event.key_repeat = emsc_event->repeat; |
| 5476 | _sapp.event.modifiers = _sapp_emsc_key_event_mods(emsc_event); |
| 5477 | if (type == SAPP_EVENTTYPE_CHAR) { |
| 5478 | // NOTE: charCode doesn't appear to be supported on Android Chrome |
| 5479 | _sapp.event.char_code = emsc_event->charCode; |
| 5480 | consume_event |= !_sapp.desc.html5_bubble_char_events; |
| 5481 | } else { |
| 5482 | if (0 != emsc_event->code[0]) { |
| 5483 | // This code path is for desktop browsers which send untranslated 'physical' key code strings |
| 5484 | // (which is what we actually want for key events) |
| 5485 | _sapp.event.key_code = _sapp_emsc_translate_key(emsc_event->code); |
| 5486 | } else { |
| 5487 | // This code path is for mobile browsers which only send localized key code |
| 5488 | // strings. Note that the translation will only work for a small subset |
| 5489 | // of localization-agnostic keys (like Enter, arrow keys, etc...), but |
| 5490 | // regular alpha-numeric keys will all result in an SAPP_KEYCODE_INVALID) |
| 5491 | _sapp.event.key_code = _sapp_emsc_translate_key(emsc_event->key); |
| 5492 | } |
| 5493 | |
| 5494 | // Special hack for macOS: if the Super key is pressed, macOS doesn't |
| 5495 | // send keyUp events. As a workaround, to prevent keys from |
| 5496 | // "sticking", we'll send a keyup event following a keydown |
| 5497 | // when the SUPER key is pressed |
| 5498 | if ((type == SAPP_EVENTTYPE_KEY_DOWN) && |
| 5499 | (_sapp.event.key_code != SAPP_KEYCODE_LEFT_SUPER) && |
| 5500 | (_sapp.event.key_code != SAPP_KEYCODE_RIGHT_SUPER) && |
| 5501 | (_sapp.event.modifiers & SAPP_MODIFIER_SUPER)) |
| 5502 | { |
| 5503 | send_keyup_followup = true; |
| 5504 | } |
| 5505 | |
| 5506 | // 'character key events' will always need to bubble up, otherwise the browser |
| 5507 | // wouldn't be able to generate character events. |
| 5508 | if (!_sapp_emsc_is_char_key(_sapp.event.key_code)) { |
| 5509 | consume_event |= !_sapp.desc.html5_bubble_key_events; |
| 5510 | } |
| 5511 | } |
| 5512 | consume_event |= _sapp_call_event(&_sapp.event); |
| 5513 | if (send_keyup_followup) { |
| 5514 | _sapp.event.type = SAPP_EVENTTYPE_KEY_UP; |
| 5515 | consume_event |= _sapp_call_event(&_sapp.event); |
| 5516 | } |
| 5517 | } |
| 5518 | } |
| 5519 | _sapp_emsc_update_mouse_lock_state(); |
| 5520 | return consume_event; |
| 5521 | } |
| 5522 | |
| 5523 | _SOKOL_PRIVATE EM_BOOL _sapp_emsc_touch_cb(int emsc_type, const EmscriptenTouchEvent* emsc_event, void* user_data) { |
| 5524 | _SOKOL_UNUSED(user_data); |
| 5525 | bool consume_event = !_sapp.desc.html5_bubble_touch_events; |
| 5526 | if (_sapp_events_enabled()) { |
| 5527 | sapp_event_type type; |
| 5528 | switch (emsc_type) { |
| 5529 | case EMSCRIPTEN_EVENT_TOUCHSTART: |
| 5530 | type = SAPP_EVENTTYPE_TOUCHES_BEGAN; |
| 5531 | break; |
| 5532 | case EMSCRIPTEN_EVENT_TOUCHMOVE: |
| 5533 | type = SAPP_EVENTTYPE_TOUCHES_MOVED; |
| 5534 | break; |
| 5535 | case EMSCRIPTEN_EVENT_TOUCHEND: |
| 5536 | type = SAPP_EVENTTYPE_TOUCHES_ENDED; |
| 5537 | break; |
| 5538 | case EMSCRIPTEN_EVENT_TOUCHCANCEL: |
| 5539 | type = SAPP_EVENTTYPE_TOUCHES_CANCELLED; |
| 5540 | break; |
| 5541 | default: |
| 5542 | type = SAPP_EVENTTYPE_INVALID; |
| 5543 | break; |
| 5544 | } |
| 5545 | if (type != SAPP_EVENTTYPE_INVALID) { |
| 5546 | _sapp_init_event(type); |
| 5547 | _sapp.event.modifiers = _sapp_emsc_touch_event_mods(emsc_event); |
| 5548 | _sapp.event.num_touches = emsc_event->numTouches; |
| 5549 | if (_sapp.event.num_touches > SAPP_MAX_TOUCHPOINTS) { |
| 5550 | _sapp.event.num_touches = SAPP_MAX_TOUCHPOINTS; |
| 5551 | } |
| 5552 | for (int i = 0; i < _sapp.event.num_touches; i++) { |
| 5553 | const EmscriptenTouchPoint* src = &emsc_event->touches[i]; |
| 5554 | sapp_touchpoint* dst = &_sapp.event.touches[i]; |
| 5555 | dst->identifier = (uintptr_t)src->identifier; |
| 5556 | dst->pos_x = src->targetX * _sapp.dpi_scale; |
| 5557 | dst->pos_y = src->targetY * _sapp.dpi_scale; |
| 5558 | dst->changed = src->isChanged; |
| 5559 | } |
| 5560 | consume_event |= _sapp_call_event(&_sapp.event); |
| 5561 | } |
| 5562 | } |
| 5563 | return consume_event; |
| 5564 | } |
| 5565 | |
| 5566 | _SOKOL_PRIVATE EM_BOOL _sapp_emsc_focus_cb(int emsc_type, const EmscriptenFocusEvent* emsc_event, void* user_data) { |
| 5567 | _SOKOL_UNUSED(emsc_type); |
| 5568 | _SOKOL_UNUSED(emsc_event); |
| 5569 | _SOKOL_UNUSED(user_data); |
| 5570 | if (_sapp_events_enabled()) { |
| 5571 | _sapp_init_event(SAPP_EVENTTYPE_FOCUSED); |
| 5572 | _sapp_call_event(&_sapp.event); |
| 5573 | } |
| 5574 | return true; |
| 5575 | } |
| 5576 | |
| 5577 | _SOKOL_PRIVATE EM_BOOL _sapp_emsc_blur_cb(int emsc_type, const EmscriptenFocusEvent* emsc_event, void* user_data) { |
| 5578 | _SOKOL_UNUSED(emsc_type); |
| 5579 | _SOKOL_UNUSED(emsc_event); |
| 5580 | _SOKOL_UNUSED(user_data); |
| 5581 | if (_sapp_events_enabled()) { |
| 5582 | _sapp_init_event(SAPP_EVENTTYPE_UNFOCUSED); |
| 5583 | _sapp_call_event(&_sapp.event); |
| 5584 | } |
| 5585 | return true; |
| 5586 | } |
| 5587 | |
| 5588 | #if defined(SOKOL_GLES3) |
| 5589 | _SOKOL_PRIVATE EM_BOOL _sapp_emsc_webgl_context_cb(int emsc_type, const void* reserved, void* user_data) { |
| 5590 | _SOKOL_UNUSED(reserved); |
| 5591 | _SOKOL_UNUSED(user_data); |
| 5592 | sapp_event_type type; |
| 5593 | switch (emsc_type) { |
| 5594 | case EMSCRIPTEN_EVENT_WEBGLCONTEXTLOST: type = SAPP_EVENTTYPE_SUSPENDED; break; |
| 5595 | case EMSCRIPTEN_EVENT_WEBGLCONTEXTRESTORED: type = SAPP_EVENTTYPE_RESUMED; break; |
| 5596 | default: type = SAPP_EVENTTYPE_INVALID; break; |
| 5597 | } |
| 5598 | if (_sapp_events_enabled() && (SAPP_EVENTTYPE_INVALID != type)) { |
| 5599 | _sapp_init_event(type); |
| 5600 | _sapp_call_event(&_sapp.event); |
| 5601 | } |
| 5602 | return true; |
| 5603 | } |
| 5604 | |
| 5605 | _SOKOL_PRIVATE void _sapp_emsc_webgl_init(void) { |
| 5606 | EmscriptenWebGLContextAttributes attrs; |
| 5607 | emscripten_webgl_init_context_attributes(&attrs); |
| 5608 | attrs.alpha = _sapp.desc.alpha; |
| 5609 | attrs.depth = true; |
| 5610 | attrs.stencil = true; |
| 5611 | attrs.antialias = _sapp.sample_count > 1; |
| 5612 | attrs.premultipliedAlpha = _sapp.desc.html5_premultiplied_alpha; |
| 5613 | attrs.preserveDrawingBuffer = _sapp.desc.html5_preserve_drawing_buffer; |
| 5614 | attrs.enableExtensionsByDefault = true; |
| 5615 | attrs.majorVersion = 2; |
| 5616 | EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context(_sapp.html5_canvas_selector, &attrs); |
| 5617 | // FIXME: error message? |
| 5618 | emscripten_webgl_make_context_current(ctx); |
| 5619 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer); |
| 5620 | |
| 5621 | // FIXME: remove PVRTC support here and in sokol-gfx at some point |
| 5622 | // some WebGL extension are not enabled automatically by emscripten |
| 5623 | emscripten_webgl_enable_extension(ctx, "WEBKIT_WEBGL_compressed_texture_pvrtc"); |
| 5624 | } |
| 5625 | #endif |
| 5626 | |
| 5627 | #if defined(SOKOL_WGPU) |
| 5628 | |
| 5629 | _SOKOL_PRIVATE void _sapp_emsc_wgpu_create_swapchain(void) { |
| 5630 | SOKOL_ASSERT(_sapp.wgpu.instance); |
| 5631 | SOKOL_ASSERT(_sapp.wgpu.device); |
| 5632 | SOKOL_ASSERT(0 == _sapp.wgpu.surface); |
| 5633 | SOKOL_ASSERT(0 == _sapp.wgpu.swapchain); |
| 5634 | SOKOL_ASSERT(0 == _sapp.wgpu.msaa_tex); |
| 5635 | SOKOL_ASSERT(0 == _sapp.wgpu.msaa_view); |
| 5636 | SOKOL_ASSERT(0 == _sapp.wgpu.depth_stencil_tex); |
| 5637 | SOKOL_ASSERT(0 == _sapp.wgpu.depth_stencil_view); |
| 5638 | SOKOL_ASSERT(0 == _sapp.wgpu.swapchain_view); |
| 5639 | |
| 5640 | WGPUSurfaceDescriptorFromCanvasHTMLSelector canvas_desc; |
| 5641 | _sapp_clear(&canvas_desc, sizeof(canvas_desc)); |
| 5642 | canvas_desc.chain.sType = WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector; |
| 5643 | canvas_desc.selector = _sapp.html5_canvas_selector; |
| 5644 | WGPUSurfaceDescriptor surf_desc; |
| 5645 | _sapp_clear(&surf_desc, sizeof(surf_desc)); |
| 5646 | surf_desc.nextInChain = &canvas_desc.chain; |
| 5647 | _sapp.wgpu.surface = wgpuInstanceCreateSurface(_sapp.wgpu.instance, &surf_desc); |
| 5648 | if (0 == _sapp.wgpu.surface) { |
| 5649 | _SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_SURFACE_FAILED); |
| 5650 | } |
| 5651 | _sapp.wgpu.render_format = wgpuSurfaceGetPreferredFormat(_sapp.wgpu.surface, _sapp.wgpu.adapter); |
| 5652 | |
| 5653 | WGPUSwapChainDescriptor sc_desc; |
| 5654 | _sapp_clear(&sc_desc, sizeof(sc_desc)); |
| 5655 | sc_desc.usage = WGPUTextureUsage_RenderAttachment; |
| 5656 | sc_desc.format = _sapp.wgpu.render_format; |
| 5657 | sc_desc.width = (uint32_t)_sapp.framebuffer_width; |
| 5658 | sc_desc.height = (uint32_t)_sapp.framebuffer_height; |
| 5659 | sc_desc.presentMode = WGPUPresentMode_Fifo; |
| 5660 | _sapp.wgpu.swapchain = wgpuDeviceCreateSwapChain(_sapp.wgpu.device, _sapp.wgpu.surface, &sc_desc); |
| 5661 | if (0 == _sapp.wgpu.swapchain) { |
| 5662 | _SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_SWAPCHAIN_FAILED); |
| 5663 | } |
| 5664 | |
| 5665 | WGPUTextureDescriptor ds_desc; |
| 5666 | _sapp_clear(&ds_desc, sizeof(ds_desc)); |
| 5667 | ds_desc.usage = WGPUTextureUsage_RenderAttachment; |
| 5668 | ds_desc.dimension = WGPUTextureDimension_2D; |
| 5669 | ds_desc.size.width = (uint32_t)_sapp.framebuffer_width; |
| 5670 | ds_desc.size.height = (uint32_t)_sapp.framebuffer_height; |
| 5671 | ds_desc.size.depthOrArrayLayers = 1; |
| 5672 | ds_desc.format = WGPUTextureFormat_Depth32FloatStencil8; |
| 5673 | ds_desc.mipLevelCount = 1; |
| 5674 | ds_desc.sampleCount = (uint32_t)_sapp.sample_count; |
| 5675 | _sapp.wgpu.depth_stencil_tex = wgpuDeviceCreateTexture(_sapp.wgpu.device, &ds_desc); |
| 5676 | if (0 == _sapp.wgpu.depth_stencil_tex) { |
| 5677 | _SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_DEPTH_STENCIL_TEXTURE_FAILED); |
| 5678 | } |
| 5679 | _sapp.wgpu.depth_stencil_view = wgpuTextureCreateView(_sapp.wgpu.depth_stencil_tex, 0); |
| 5680 | if (0 == _sapp.wgpu.depth_stencil_view) { |
| 5681 | _SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_DEPTH_STENCIL_VIEW_FAILED); |
| 5682 | } |
| 5683 | |
| 5684 | if (_sapp.sample_count > 1) { |
| 5685 | WGPUTextureDescriptor msaa_desc; |
| 5686 | _sapp_clear(&msaa_desc, sizeof(msaa_desc)); |
| 5687 | msaa_desc.usage = WGPUTextureUsage_RenderAttachment; |
| 5688 | msaa_desc.dimension = WGPUTextureDimension_2D; |
| 5689 | msaa_desc.size.width = (uint32_t)_sapp.framebuffer_width; |
| 5690 | msaa_desc.size.height = (uint32_t)_sapp.framebuffer_height; |
| 5691 | msaa_desc.size.depthOrArrayLayers = 1; |
| 5692 | msaa_desc.format = _sapp.wgpu.render_format; |
| 5693 | msaa_desc.mipLevelCount = 1; |
| 5694 | msaa_desc.sampleCount = (uint32_t)_sapp.sample_count; |
| 5695 | _sapp.wgpu.msaa_tex = wgpuDeviceCreateTexture(_sapp.wgpu.device, &msaa_desc); |
| 5696 | if (0 == _sapp.wgpu.msaa_tex) { |
| 5697 | _SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_MSAA_TEXTURE_FAILED); |
| 5698 | } |
| 5699 | _sapp.wgpu.msaa_view = wgpuTextureCreateView(_sapp.wgpu.msaa_tex, 0); |
| 5700 | if (0 == _sapp.wgpu.msaa_view) { |
| 5701 | _SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_MSAA_VIEW_FAILED); |
| 5702 | } |
| 5703 | } |
| 5704 | } |
| 5705 | |
| 5706 | _SOKOL_PRIVATE void _sapp_emsc_wgpu_discard_swapchain(void) { |
| 5707 | if (_sapp.wgpu.msaa_view) { |
| 5708 | wgpuTextureViewRelease(_sapp.wgpu.msaa_view); |
| 5709 | _sapp.wgpu.msaa_view = 0; |
| 5710 | } |
| 5711 | if (_sapp.wgpu.msaa_tex) { |
| 5712 | wgpuTextureRelease(_sapp.wgpu.msaa_tex); |
| 5713 | _sapp.wgpu.msaa_tex = 0; |
| 5714 | } |
| 5715 | if (_sapp.wgpu.depth_stencil_view) { |
| 5716 | wgpuTextureViewRelease(_sapp.wgpu.depth_stencil_view); |
| 5717 | _sapp.wgpu.depth_stencil_view = 0; |
| 5718 | } |
| 5719 | if (_sapp.wgpu.depth_stencil_tex) { |
| 5720 | wgpuTextureRelease(_sapp.wgpu.depth_stencil_tex); |
| 5721 | _sapp.wgpu.depth_stencil_tex = 0; |
| 5722 | } |
| 5723 | if (_sapp.wgpu.swapchain) { |
| 5724 | wgpuSwapChainRelease(_sapp.wgpu.swapchain); |
| 5725 | _sapp.wgpu.swapchain = 0; |
| 5726 | } |
| 5727 | if (_sapp.wgpu.surface) { |
| 5728 | wgpuSurfaceRelease(_sapp.wgpu.surface); |
| 5729 | _sapp.wgpu.surface = 0; |
| 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | _SOKOL_PRIVATE void _sapp_emsc_wgpu_size_changed(void) { |
| 5734 | _sapp_emsc_wgpu_discard_swapchain(); |
| 5735 | _sapp_emsc_wgpu_create_swapchain(); |
| 5736 | } |
| 5737 | |
| 5738 | _SOKOL_PRIVATE void _sapp_emsc_wgpu_request_device_cb(WGPURequestDeviceStatus status, WGPUDevice device, const char* msg, void* userdata) { |
| 5739 | _SOKOL_UNUSED(msg); |
| 5740 | _SOKOL_UNUSED(userdata); |
| 5741 | SOKOL_ASSERT(!_sapp.wgpu.async_init_done); |
| 5742 | if (status != WGPURequestDeviceStatus_Success) { |
| 5743 | if (status == WGPURequestDeviceStatus_Error) { |
| 5744 | _SAPP_PANIC(WGPU_REQUEST_DEVICE_STATUS_ERROR); |
| 5745 | } else { |
| 5746 | _SAPP_PANIC(WGPU_REQUEST_DEVICE_STATUS_UNKNOWN); |
| 5747 | } |
| 5748 | } |
| 5749 | SOKOL_ASSERT(device); |
| 5750 | _sapp.wgpu.device = device; |
| 5751 | _sapp_emsc_wgpu_create_swapchain(); |
| 5752 | _sapp.wgpu.async_init_done = true; |
| 5753 | } |
| 5754 | |
| 5755 | _SOKOL_PRIVATE void _sapp_emsc_wgpu_request_adapter_cb(WGPURequestAdapterStatus status, WGPUAdapter adapter, const char* msg, void* userdata) { |
| 5756 | _SOKOL_UNUSED(msg); |
| 5757 | _SOKOL_UNUSED(userdata); |
| 5758 | if (status != WGPURequestAdapterStatus_Success) { |
| 5759 | switch (status) { |
| 5760 | case WGPURequestAdapterStatus_Unavailable: _SAPP_PANIC(WGPU_REQUEST_ADAPTER_STATUS_UNAVAILABLE); break; |
| 5761 | case WGPURequestAdapterStatus_Error: _SAPP_PANIC(WGPU_REQUEST_ADAPTER_STATUS_ERROR); break; |
| 5762 | default: _SAPP_PANIC(WGPU_REQUEST_ADAPTER_STATUS_UNKNOWN); break; |
| 5763 | } |
| 5764 | } |
| 5765 | SOKOL_ASSERT(adapter); |
| 5766 | _sapp.wgpu.adapter = adapter; |
| 5767 | size_t cur_feature_index = 1; |
| 5768 | WGPUFeatureName requiredFeatures[8] = { |
| 5769 | WGPUFeatureName_Depth32FloatStencil8, |
| 5770 | }; |
| 5771 | // check for optional features we're interested in |
| 5772 | // FIXME: ASTC texture compression |
| 5773 | if (wgpuAdapterHasFeature(adapter, WGPUFeatureName_TextureCompressionBC)) { |
| 5774 | requiredFeatures[cur_feature_index++] = WGPUFeatureName_TextureCompressionBC; |
| 5775 | } else if (wgpuAdapterHasFeature(adapter, WGPUFeatureName_TextureCompressionETC2)) { |
| 5776 | requiredFeatures[cur_feature_index++] = WGPUFeatureName_TextureCompressionETC2; |
| 5777 | } |
| 5778 | |
| 5779 | WGPUDeviceDescriptor dev_desc; |
| 5780 | _sapp_clear(&dev_desc, sizeof(dev_desc)); |
| 5781 | dev_desc.requiredFeatureCount = cur_feature_index; |
| 5782 | dev_desc.requiredFeatures = requiredFeatures, |
| 5783 | wgpuAdapterRequestDevice(adapter, &dev_desc, _sapp_emsc_wgpu_request_device_cb, 0); |
| 5784 | } |
| 5785 | |
| 5786 | _SOKOL_PRIVATE void _sapp_emsc_wgpu_init(void) { |
| 5787 | SOKOL_ASSERT(0 == _sapp.wgpu.instance); |
| 5788 | SOKOL_ASSERT(!_sapp.wgpu.async_init_done); |
| 5789 | _sapp.wgpu.instance = wgpuCreateInstance(0); |
| 5790 | if (0 == _sapp.wgpu.instance) { |
| 5791 | _SAPP_PANIC(WGPU_CREATE_INSTANCE_FAILED); |
| 5792 | } |
| 5793 | // FIXME: power preference? |
| 5794 | wgpuInstanceRequestAdapter(_sapp.wgpu.instance, 0, _sapp_emsc_wgpu_request_adapter_cb, 0); |
| 5795 | } |
| 5796 | |
| 5797 | _SOKOL_PRIVATE void _sapp_emsc_wgpu_frame(void) { |
| 5798 | if (_sapp.wgpu.async_init_done) { |
| 5799 | _sapp.wgpu.swapchain_view = wgpuSwapChainGetCurrentTextureView(_sapp.wgpu.swapchain); |
| 5800 | _sapp_frame(); |
| 5801 | wgpuTextureViewRelease(_sapp.wgpu.swapchain_view); |
| 5802 | _sapp.wgpu.swapchain_view = 0; |
| 5803 | } |
| 5804 | } |
| 5805 | #endif // SOKOL_WGPU |
| 5806 | |
| 5807 | _SOKOL_PRIVATE void _sapp_emsc_register_eventhandlers(void) { |
| 5808 | // NOTE: HTML canvas doesn't receive input focus, this is why key event handlers are added |
| 5809 | // to the window object (this could be worked around by adding a "tab index" to the |
| 5810 | // canvas) |
| 5811 | emscripten_set_mousedown_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_mouse_cb); |
| 5812 | emscripten_set_mouseup_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_mouse_cb); |
| 5813 | emscripten_set_mousemove_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_mouse_cb); |
| 5814 | emscripten_set_mouseenter_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_mouse_cb); |
| 5815 | emscripten_set_mouseleave_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_mouse_cb); |
| 5816 | emscripten_set_wheel_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_wheel_cb); |
| 5817 | emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, _sapp_emsc_key_cb); |
| 5818 | emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, _sapp_emsc_key_cb); |
| 5819 | emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, _sapp_emsc_key_cb); |
| 5820 | emscripten_set_touchstart_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_touch_cb); |
| 5821 | emscripten_set_touchmove_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_touch_cb); |
| 5822 | emscripten_set_touchend_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_touch_cb); |
| 5823 | emscripten_set_touchcancel_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_touch_cb); |
| 5824 | emscripten_set_pointerlockchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, true, _sapp_emsc_pointerlockchange_cb); |
| 5825 | emscripten_set_pointerlockerror_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, true, _sapp_emsc_pointerlockerror_cb); |
| 5826 | emscripten_set_focus_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, _sapp_emsc_focus_cb); |
| 5827 | emscripten_set_blur_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, _sapp_emsc_blur_cb); |
| 5828 | sapp_js_add_beforeunload_listener(); |
| 5829 | if (_sapp.clipboard.enabled) { |
| 5830 | sapp_js_add_clipboard_listener(); |
| 5831 | } |
| 5832 | if (_sapp.drop.enabled) { |
| 5833 | sapp_js_add_dragndrop_listeners(&_sapp.html5_canvas_selector[1]); |
| 5834 | } |
| 5835 | #if defined(SOKOL_GLES3) |
| 5836 | emscripten_set_webglcontextlost_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_webgl_context_cb); |
| 5837 | emscripten_set_webglcontextrestored_callback(_sapp.html5_canvas_selector, 0, true, _sapp_emsc_webgl_context_cb); |
| 5838 | #endif |
| 5839 | } |
| 5840 | |
| 5841 | _SOKOL_PRIVATE void _sapp_emsc_unregister_eventhandlers(void) { |
| 5842 | emscripten_set_mousedown_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5843 | emscripten_set_mouseup_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5844 | emscripten_set_mousemove_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5845 | emscripten_set_mouseenter_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5846 | emscripten_set_mouseleave_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5847 | emscripten_set_wheel_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5848 | emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0); |
| 5849 | emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0); |
| 5850 | emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0); |
| 5851 | emscripten_set_touchstart_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5852 | emscripten_set_touchmove_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5853 | emscripten_set_touchend_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5854 | emscripten_set_touchcancel_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5855 | emscripten_set_pointerlockchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, true, 0); |
| 5856 | emscripten_set_pointerlockerror_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, true, 0); |
| 5857 | emscripten_set_focus_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0); |
| 5858 | emscripten_set_blur_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0); |
| 5859 | if (!_sapp.desc.html5_canvas_resize) { |
| 5860 | emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, true, 0); |
| 5861 | } |
| 5862 | sapp_js_remove_beforeunload_listener(); |
| 5863 | if (_sapp.clipboard.enabled) { |
| 5864 | sapp_js_remove_clipboard_listener(); |
| 5865 | } |
| 5866 | if (_sapp.drop.enabled) { |
| 5867 | sapp_js_remove_dragndrop_listeners(&_sapp.html5_canvas_selector[1]); |
| 5868 | } |
| 5869 | #if defined(SOKOL_GLES3) |
| 5870 | emscripten_set_webglcontextlost_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5871 | emscripten_set_webglcontextrestored_callback(_sapp.html5_canvas_selector, 0, true, 0); |
| 5872 | #endif |
| 5873 | } |
| 5874 | |
| 5875 | _SOKOL_PRIVATE EM_BOOL _sapp_emsc_frame_animation_loop(double time, void* userData) { |
| 5876 | _SOKOL_UNUSED(userData); |
| 5877 | _sapp_timing_external(&_sapp.timing, time / 1000.0); |
| 5878 | |
| 5879 | #if defined(SOKOL_WGPU) |
| 5880 | _sapp_emsc_wgpu_frame(); |
| 5881 | #else |
| 5882 | _sapp_frame(); |
| 5883 | #endif |
| 5884 | |
| 5885 | // quit-handling |
| 5886 | if (_sapp.quit_requested) { |
| 5887 | _sapp_init_event(SAPP_EVENTTYPE_QUIT_REQUESTED); |
| 5888 | _sapp_call_event(&_sapp.event); |
| 5889 | if (_sapp.quit_requested) { |
| 5890 | _sapp.quit_ordered = true; |
| 5891 | } |
| 5892 | } |
| 5893 | if (_sapp.quit_ordered) { |
| 5894 | _sapp_emsc_unregister_eventhandlers(); |
| 5895 | _sapp_call_cleanup(); |
| 5896 | _sapp_discard_state(); |
| 5897 | return EM_FALSE; |
| 5898 | } |
| 5899 | return EM_TRUE; |
| 5900 | } |
| 5901 | |
| 5902 | _SOKOL_PRIVATE void _sapp_emsc_frame_main_loop(void) { |
| 5903 | const double time = emscripten_performance_now(); |
| 5904 | if (!_sapp_emsc_frame_animation_loop(time, 0)) { |
| 5905 | emscripten_cancel_main_loop(); |
| 5906 | } |
| 5907 | } |
| 5908 | |
| 5909 | _SOKOL_PRIVATE void _sapp_emsc_run(const sapp_desc* desc) { |
| 5910 | _sapp_init_state(desc); |
| 5911 | sapp_js_init(&_sapp.html5_canvas_selector[1]); |
| 5912 | double w, h; |
| 5913 | if (_sapp.desc.html5_canvas_resize) { |
| 5914 | w = (double) _sapp_def(_sapp.desc.width, _SAPP_FALLBACK_DEFAULT_WINDOW_WIDTH); |
| 5915 | h = (double) _sapp_def(_sapp.desc.height, _SAPP_FALLBACK_DEFAULT_WINDOW_HEIGHT); |
| 5916 | } |
| 5917 | else { |
| 5918 | emscripten_get_element_css_size(_sapp.html5_canvas_selector, &w, &h); |
| 5919 | emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, false, _sapp_emsc_size_changed); |
| 5920 | } |
| 5921 | if (_sapp.desc.high_dpi) { |
| 5922 | _sapp.dpi_scale = emscripten_get_device_pixel_ratio(); |
| 5923 | } |
| 5924 | _sapp.window_width = (int)roundf(w); |
| 5925 | _sapp.window_height = (int)roundf(h); |
| 5926 | _sapp.framebuffer_width = (int)roundf(w * _sapp.dpi_scale); |
| 5927 | _sapp.framebuffer_height = (int)roundf(h * _sapp.dpi_scale); |
| 5928 | emscripten_set_canvas_element_size(_sapp.html5_canvas_selector, _sapp.framebuffer_width, _sapp.framebuffer_height); |
| 5929 | #if defined(SOKOL_GLES3) |
| 5930 | _sapp_emsc_webgl_init(); |
| 5931 | #elif defined(SOKOL_WGPU) |
| 5932 | _sapp_emsc_wgpu_init(); |
| 5933 | #endif |
| 5934 | _sapp.valid = true; |
| 5935 | _sapp_emsc_register_eventhandlers(); |
| 5936 | sapp_set_icon(&desc->icon); |
| 5937 | |
| 5938 | // start the frame loop |
| 5939 | if (_sapp.desc.html5_use_emsc_set_main_loop) { |
| 5940 | emscripten_set_main_loop(_sapp_emsc_frame_main_loop, 0, _sapp.desc.html5_emsc_set_main_loop_simulate_infinite_loop); |
| 5941 | } else { |
| 5942 | emscripten_request_animation_frame_loop(_sapp_emsc_frame_animation_loop, 0); |
| 5943 | } |
| 5944 | // NOT A BUG: do not call _sapp_discard_state() here, instead this is |
| 5945 | // called in _sapp_emsc_frame() when the application is ordered to quit |
| 5946 | } |
| 5947 | |
| 5948 | #if !defined(SOKOL_NO_ENTRY) |
| 5949 | int main(int argc, char* argv[]) { |
| 5950 | sapp_desc desc = sokol_main(argc, argv); |
| 5951 | _sapp_emsc_run(&desc); |
| 5952 | return 0; |
| 5953 | } |
| 5954 | #endif /* SOKOL_NO_ENTRY */ |
| 5955 | #endif /* _SAPP_EMSCRIPTEN */ |
| 5956 | |
| 5957 | // ██████ ██ ██ ██ ███████ ██ ██████ ███████ ██████ ███████ |
| 5958 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 5959 | // ██ ███ ██ ███████ █████ ██ ██████ █████ ██████ ███████ |
| 5960 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 5961 | // ██████ ███████ ██ ██ ███████ ███████ ██ ███████ ██ ██ ███████ |
| 5962 | // |
| 5963 | // >>gl helpers |
| 5964 | #if defined(SOKOL_GLCORE) |
| 5965 | typedef struct { |
| 5966 | int red_bits; |
| 5967 | int green_bits; |
| 5968 | int blue_bits; |
| 5969 | int alpha_bits; |
| 5970 | int depth_bits; |
| 5971 | int stencil_bits; |
| 5972 | int samples; |
| 5973 | bool doublebuffer; |
| 5974 | uintptr_t handle; |
| 5975 | } _sapp_gl_fbconfig; |
| 5976 | |
| 5977 | _SOKOL_PRIVATE void _sapp_gl_init_fbconfig(_sapp_gl_fbconfig* fbconfig) { |
| 5978 | _sapp_clear(fbconfig, sizeof(_sapp_gl_fbconfig)); |
| 5979 | /* -1 means "don't care" */ |
| 5980 | fbconfig->red_bits = -1; |
| 5981 | fbconfig->green_bits = -1; |
| 5982 | fbconfig->blue_bits = -1; |
| 5983 | fbconfig->alpha_bits = -1; |
| 5984 | fbconfig->depth_bits = -1; |
| 5985 | fbconfig->stencil_bits = -1; |
| 5986 | fbconfig->samples = -1; |
| 5987 | } |
| 5988 | |
| 5989 | typedef struct { |
| 5990 | int least_missing; |
| 5991 | int least_color_diff; |
| 5992 | int least_extra_diff; |
| 5993 | bool best_match; |
| 5994 | } _sapp_gl_fbselect; |
| 5995 | |
| 5996 | _SOKOL_PRIVATE void _sapp_gl_init_fbselect(_sapp_gl_fbselect* fbselect) { |
| 5997 | _sapp_clear(fbselect, sizeof(_sapp_gl_fbselect)); |
| 5998 | fbselect->least_missing = 1000000; |
| 5999 | fbselect->least_color_diff = 10000000; |
| 6000 | fbselect->least_extra_diff = 10000000; |
| 6001 | fbselect->best_match = false; |
| 6002 | } |
| 6003 | |
| 6004 | // NOTE: this is used only in the WGL code path |
| 6005 | _SOKOL_PRIVATE bool _sapp_gl_select_fbconfig(_sapp_gl_fbselect* fbselect, const _sapp_gl_fbconfig* desired, const _sapp_gl_fbconfig* current) { |
| 6006 | int missing = 0; |
| 6007 | if (desired->doublebuffer != current->doublebuffer) { |
| 6008 | return false; |
| 6009 | } |
| 6010 | |
| 6011 | if ((desired->alpha_bits > 0) && (current->alpha_bits == 0)) { |
| 6012 | missing++; |
| 6013 | } |
| 6014 | if ((desired->depth_bits > 0) && (current->depth_bits == 0)) { |
| 6015 | missing++; |
| 6016 | } |
| 6017 | if ((desired->stencil_bits > 0) && (current->stencil_bits == 0)) { |
| 6018 | missing++; |
| 6019 | } |
| 6020 | if ((desired->samples > 0) && (current->samples == 0)) { |
| 6021 | /* Technically, several multisampling buffers could be |
| 6022 | involved, but that's a lower level implementation detail and |
| 6023 | not important to us here, so we count them as one |
| 6024 | */ |
| 6025 | missing++; |
| 6026 | } |
| 6027 | |
| 6028 | /* These polynomials make many small channel size differences matter |
| 6029 | less than one large channel size difference |
| 6030 | Calculate color channel size difference value |
| 6031 | */ |
| 6032 | int color_diff = 0; |
| 6033 | if (desired->red_bits != -1) { |
| 6034 | color_diff += (desired->red_bits - current->red_bits) * (desired->red_bits - current->red_bits); |
| 6035 | } |
| 6036 | if (desired->green_bits != -1) { |
| 6037 | color_diff += (desired->green_bits - current->green_bits) * (desired->green_bits - current->green_bits); |
| 6038 | } |
| 6039 | if (desired->blue_bits != -1) { |
| 6040 | color_diff += (desired->blue_bits - current->blue_bits) * (desired->blue_bits - current->blue_bits); |
| 6041 | } |
| 6042 | |
| 6043 | /* Calculate non-color channel size difference value */ |
| 6044 | int extra_diff = 0; |
| 6045 | if (desired->alpha_bits != -1) { |
| 6046 | extra_diff += (desired->alpha_bits - current->alpha_bits) * (desired->alpha_bits - current->alpha_bits); |
| 6047 | } |
| 6048 | if (desired->depth_bits != -1) { |
| 6049 | extra_diff += (desired->depth_bits - current->depth_bits) * (desired->depth_bits - current->depth_bits); |
| 6050 | } |
| 6051 | if (desired->stencil_bits != -1) { |
| 6052 | extra_diff += (desired->stencil_bits - current->stencil_bits) * (desired->stencil_bits - current->stencil_bits); |
| 6053 | } |
| 6054 | if (desired->samples != -1) { |
| 6055 | extra_diff += (desired->samples - current->samples) * (desired->samples - current->samples); |
| 6056 | } |
| 6057 | |
| 6058 | /* Figure out if the current one is better than the best one found so far |
| 6059 | Least number of missing buffers is the most important heuristic, |
| 6060 | then color buffer size match and lastly size match for other buffers |
| 6061 | */ |
| 6062 | bool new_closest = false; |
| 6063 | if (missing < fbselect->least_missing) { |
| 6064 | new_closest = true; |
| 6065 | } else if (missing == fbselect->least_missing) { |
| 6066 | if ((color_diff < fbselect->least_color_diff) || |
| 6067 | ((color_diff == fbselect->least_color_diff) && (extra_diff < fbselect->least_extra_diff))) |
| 6068 | { |
| 6069 | new_closest = true; |
| 6070 | } |
| 6071 | } |
| 6072 | if (new_closest) { |
| 6073 | fbselect->least_missing = missing; |
| 6074 | fbselect->least_color_diff = color_diff; |
| 6075 | fbselect->least_extra_diff = extra_diff; |
| 6076 | fbselect->best_match = (missing | color_diff | extra_diff) == 0; |
| 6077 | } |
| 6078 | return new_closest; |
| 6079 | } |
| 6080 | |
| 6081 | // NOTE: this is used only in the GLX code path |
| 6082 | _SOKOL_PRIVATE const _sapp_gl_fbconfig* _sapp_gl_choose_fbconfig(const _sapp_gl_fbconfig* desired, const _sapp_gl_fbconfig* alternatives, int count) { |
| 6083 | int missing, least_missing = 1000000; |
| 6084 | int color_diff, least_color_diff = 10000000; |
| 6085 | int extra_diff, least_extra_diff = 10000000; |
| 6086 | const _sapp_gl_fbconfig* current; |
| 6087 | const _sapp_gl_fbconfig* closest = 0; |
| 6088 | for (int i = 0; i < count; i++) { |
| 6089 | current = alternatives + i; |
| 6090 | if (desired->doublebuffer != current->doublebuffer) { |
| 6091 | continue; |
| 6092 | } |
| 6093 | missing = 0; |
| 6094 | if (desired->alpha_bits > 0 && current->alpha_bits == 0) { |
| 6095 | missing++; |
| 6096 | } |
| 6097 | if (desired->depth_bits > 0 && current->depth_bits == 0) { |
| 6098 | missing++; |
| 6099 | } |
| 6100 | if (desired->stencil_bits > 0 && current->stencil_bits == 0) { |
| 6101 | missing++; |
| 6102 | } |
| 6103 | if (desired->samples > 0 && current->samples == 0) { |
| 6104 | /* Technically, several multisampling buffers could be |
| 6105 | involved, but that's a lower level implementation detail and |
| 6106 | not important to us here, so we count them as one |
| 6107 | */ |
| 6108 | missing++; |
| 6109 | } |
| 6110 | |
| 6111 | /* These polynomials make many small channel size differences matter |
| 6112 | less than one large channel size difference |
| 6113 | Calculate color channel size difference value |
| 6114 | */ |
| 6115 | color_diff = 0; |
| 6116 | if (desired->red_bits != -1) { |
| 6117 | color_diff += (desired->red_bits - current->red_bits) * (desired->red_bits - current->red_bits); |
| 6118 | } |
| 6119 | if (desired->green_bits != -1) { |
| 6120 | color_diff += (desired->green_bits - current->green_bits) * (desired->green_bits - current->green_bits); |
| 6121 | } |
| 6122 | if (desired->blue_bits != -1) { |
| 6123 | color_diff += (desired->blue_bits - current->blue_bits) * (desired->blue_bits - current->blue_bits); |
| 6124 | } |
| 6125 | |
| 6126 | /* Calculate non-color channel size difference value */ |
| 6127 | extra_diff = 0; |
| 6128 | if (desired->alpha_bits != -1) { |
| 6129 | extra_diff += (desired->alpha_bits - current->alpha_bits) * (desired->alpha_bits - current->alpha_bits); |
| 6130 | } |
| 6131 | if (desired->depth_bits != -1) { |
| 6132 | extra_diff += (desired->depth_bits - current->depth_bits) * (desired->depth_bits - current->depth_bits); |
| 6133 | } |
| 6134 | if (desired->stencil_bits != -1) { |
| 6135 | extra_diff += (desired->stencil_bits - current->stencil_bits) * (desired->stencil_bits - current->stencil_bits); |
| 6136 | } |
| 6137 | if (desired->samples != -1) { |
| 6138 | extra_diff += (desired->samples - current->samples) * (desired->samples - current->samples); |
| 6139 | } |
| 6140 | |
| 6141 | /* Figure out if the current one is better than the best one found so far |
| 6142 | Least number of missing buffers is the most important heuristic, |
| 6143 | then color buffer size match and lastly size match for other buffers |
| 6144 | */ |
| 6145 | if (missing < least_missing) { |
| 6146 | closest = current; |
| 6147 | } |
| 6148 | else if (missing == least_missing) { |
| 6149 | if ((color_diff < least_color_diff) || |
| 6150 | (color_diff == least_color_diff && extra_diff < least_extra_diff)) |
| 6151 | { |
| 6152 | closest = current; |
| 6153 | } |
| 6154 | } |
| 6155 | if (current == closest) { |
| 6156 | least_missing = missing; |
| 6157 | least_color_diff = color_diff; |
| 6158 | least_extra_diff = extra_diff; |
| 6159 | } |
| 6160 | } |
| 6161 | return closest; |
| 6162 | } |
| 6163 | #endif |
| 6164 | |
| 6165 | // ██ ██ ██ ███ ██ ██████ ██████ ██ ██ ███████ |
| 6166 | // ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ |
| 6167 | // ██ █ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █ ██ ███████ |
| 6168 | // ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ |
| 6169 | // ███ ███ ██ ██ ████ ██████ ██████ ███ ███ ███████ |
| 6170 | // |
| 6171 | // >>windows |
| 6172 | #if defined(_SAPP_WIN32) |
| 6173 | _SOKOL_PRIVATE bool _sapp_win32_utf8_to_wide(const char* src, wchar_t* dst, int dst_num_bytes) { |
| 6174 | SOKOL_ASSERT(src && dst && (dst_num_bytes > 1)); |
| 6175 | _sapp_clear(dst, (size_t)dst_num_bytes); |
| 6176 | const int dst_chars = dst_num_bytes / (int)sizeof(wchar_t); |
| 6177 | const int dst_needed = MultiByteToWideChar(CP_UTF8, 0, src, -1, 0, 0); |
| 6178 | if ((dst_needed > 0) && (dst_needed < dst_chars)) { |
| 6179 | MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, dst_chars); |
| 6180 | return true; |
| 6181 | } |
| 6182 | else { |
| 6183 | /* input string doesn't fit into destination buffer */ |
| 6184 | return false; |
| 6185 | } |
| 6186 | } |
| 6187 | |
| 6188 | _SOKOL_PRIVATE void _sapp_win32_app_event(sapp_event_type type) { |
| 6189 | if (_sapp_events_enabled()) { |
| 6190 | _sapp_init_event(type); |
| 6191 | _sapp_call_event(&_sapp.event); |
| 6192 | } |
| 6193 | } |
| 6194 | |
| 6195 | _SOKOL_PRIVATE void _sapp_win32_init_keytable(void) { |
| 6196 | /* same as GLFW */ |
| 6197 | _sapp.keycodes[0x00B] = SAPP_KEYCODE_0; |
| 6198 | _sapp.keycodes[0x002] = SAPP_KEYCODE_1; |
| 6199 | _sapp.keycodes[0x003] = SAPP_KEYCODE_2; |
| 6200 | _sapp.keycodes[0x004] = SAPP_KEYCODE_3; |
| 6201 | _sapp.keycodes[0x005] = SAPP_KEYCODE_4; |
| 6202 | _sapp.keycodes[0x006] = SAPP_KEYCODE_5; |
| 6203 | _sapp.keycodes[0x007] = SAPP_KEYCODE_6; |
| 6204 | _sapp.keycodes[0x008] = SAPP_KEYCODE_7; |
| 6205 | _sapp.keycodes[0x009] = SAPP_KEYCODE_8; |
| 6206 | _sapp.keycodes[0x00A] = SAPP_KEYCODE_9; |
| 6207 | _sapp.keycodes[0x01E] = SAPP_KEYCODE_A; |
| 6208 | _sapp.keycodes[0x030] = SAPP_KEYCODE_B; |
| 6209 | _sapp.keycodes[0x02E] = SAPP_KEYCODE_C; |
| 6210 | _sapp.keycodes[0x020] = SAPP_KEYCODE_D; |
| 6211 | _sapp.keycodes[0x012] = SAPP_KEYCODE_E; |
| 6212 | _sapp.keycodes[0x021] = SAPP_KEYCODE_F; |
| 6213 | _sapp.keycodes[0x022] = SAPP_KEYCODE_G; |
| 6214 | _sapp.keycodes[0x023] = SAPP_KEYCODE_H; |
| 6215 | _sapp.keycodes[0x017] = SAPP_KEYCODE_I; |
| 6216 | _sapp.keycodes[0x024] = SAPP_KEYCODE_J; |
| 6217 | _sapp.keycodes[0x025] = SAPP_KEYCODE_K; |
| 6218 | _sapp.keycodes[0x026] = SAPP_KEYCODE_L; |
| 6219 | _sapp.keycodes[0x032] = SAPP_KEYCODE_M; |
| 6220 | _sapp.keycodes[0x031] = SAPP_KEYCODE_N; |
| 6221 | _sapp.keycodes[0x018] = SAPP_KEYCODE_O; |
| 6222 | _sapp.keycodes[0x019] = SAPP_KEYCODE_P; |
| 6223 | _sapp.keycodes[0x010] = SAPP_KEYCODE_Q; |
| 6224 | _sapp.keycodes[0x013] = SAPP_KEYCODE_R; |
| 6225 | _sapp.keycodes[0x01F] = SAPP_KEYCODE_S; |
| 6226 | _sapp.keycodes[0x014] = SAPP_KEYCODE_T; |
| 6227 | _sapp.keycodes[0x016] = SAPP_KEYCODE_U; |
| 6228 | _sapp.keycodes[0x02F] = SAPP_KEYCODE_V; |
| 6229 | _sapp.keycodes[0x011] = SAPP_KEYCODE_W; |
| 6230 | _sapp.keycodes[0x02D] = SAPP_KEYCODE_X; |
| 6231 | _sapp.keycodes[0x015] = SAPP_KEYCODE_Y; |
| 6232 | _sapp.keycodes[0x02C] = SAPP_KEYCODE_Z; |
| 6233 | _sapp.keycodes[0x028] = SAPP_KEYCODE_APOSTROPHE; |
| 6234 | _sapp.keycodes[0x02B] = SAPP_KEYCODE_BACKSLASH; |
| 6235 | _sapp.keycodes[0x033] = SAPP_KEYCODE_COMMA; |
| 6236 | _sapp.keycodes[0x00D] = SAPP_KEYCODE_EQUAL; |
| 6237 | _sapp.keycodes[0x029] = SAPP_KEYCODE_GRAVE_ACCENT; |
| 6238 | _sapp.keycodes[0x01A] = SAPP_KEYCODE_LEFT_BRACKET; |
| 6239 | _sapp.keycodes[0x00C] = SAPP_KEYCODE_MINUS; |
| 6240 | _sapp.keycodes[0x034] = SAPP_KEYCODE_PERIOD; |
| 6241 | _sapp.keycodes[0x01B] = SAPP_KEYCODE_RIGHT_BRACKET; |
| 6242 | _sapp.keycodes[0x027] = SAPP_KEYCODE_SEMICOLON; |
| 6243 | _sapp.keycodes[0x035] = SAPP_KEYCODE_SLASH; |
| 6244 | _sapp.keycodes[0x056] = SAPP_KEYCODE_WORLD_2; |
| 6245 | _sapp.keycodes[0x00E] = SAPP_KEYCODE_BACKSPACE; |
| 6246 | _sapp.keycodes[0x153] = SAPP_KEYCODE_DELETE; |
| 6247 | _sapp.keycodes[0x14F] = SAPP_KEYCODE_END; |
| 6248 | _sapp.keycodes[0x01C] = SAPP_KEYCODE_ENTER; |
| 6249 | _sapp.keycodes[0x001] = SAPP_KEYCODE_ESCAPE; |
| 6250 | _sapp.keycodes[0x147] = SAPP_KEYCODE_HOME; |
| 6251 | _sapp.keycodes[0x152] = SAPP_KEYCODE_INSERT; |
| 6252 | _sapp.keycodes[0x15D] = SAPP_KEYCODE_MENU; |
| 6253 | _sapp.keycodes[0x151] = SAPP_KEYCODE_PAGE_DOWN; |
| 6254 | _sapp.keycodes[0x149] = SAPP_KEYCODE_PAGE_UP; |
| 6255 | _sapp.keycodes[0x045] = SAPP_KEYCODE_PAUSE; |
| 6256 | _sapp.keycodes[0x146] = SAPP_KEYCODE_PAUSE; |
| 6257 | _sapp.keycodes[0x039] = SAPP_KEYCODE_SPACE; |
| 6258 | _sapp.keycodes[0x00F] = SAPP_KEYCODE_TAB; |
| 6259 | _sapp.keycodes[0x03A] = SAPP_KEYCODE_CAPS_LOCK; |
| 6260 | _sapp.keycodes[0x145] = SAPP_KEYCODE_NUM_LOCK; |
| 6261 | _sapp.keycodes[0x046] = SAPP_KEYCODE_SCROLL_LOCK; |
| 6262 | _sapp.keycodes[0x03B] = SAPP_KEYCODE_F1; |
| 6263 | _sapp.keycodes[0x03C] = SAPP_KEYCODE_F2; |
| 6264 | _sapp.keycodes[0x03D] = SAPP_KEYCODE_F3; |
| 6265 | _sapp.keycodes[0x03E] = SAPP_KEYCODE_F4; |
| 6266 | _sapp.keycodes[0x03F] = SAPP_KEYCODE_F5; |
| 6267 | _sapp.keycodes[0x040] = SAPP_KEYCODE_F6; |
| 6268 | _sapp.keycodes[0x041] = SAPP_KEYCODE_F7; |
| 6269 | _sapp.keycodes[0x042] = SAPP_KEYCODE_F8; |
| 6270 | _sapp.keycodes[0x043] = SAPP_KEYCODE_F9; |
| 6271 | _sapp.keycodes[0x044] = SAPP_KEYCODE_F10; |
| 6272 | _sapp.keycodes[0x057] = SAPP_KEYCODE_F11; |
| 6273 | _sapp.keycodes[0x058] = SAPP_KEYCODE_F12; |
| 6274 | _sapp.keycodes[0x064] = SAPP_KEYCODE_F13; |
| 6275 | _sapp.keycodes[0x065] = SAPP_KEYCODE_F14; |
| 6276 | _sapp.keycodes[0x066] = SAPP_KEYCODE_F15; |
| 6277 | _sapp.keycodes[0x067] = SAPP_KEYCODE_F16; |
| 6278 | _sapp.keycodes[0x068] = SAPP_KEYCODE_F17; |
| 6279 | _sapp.keycodes[0x069] = SAPP_KEYCODE_F18; |
| 6280 | _sapp.keycodes[0x06A] = SAPP_KEYCODE_F19; |
| 6281 | _sapp.keycodes[0x06B] = SAPP_KEYCODE_F20; |
| 6282 | _sapp.keycodes[0x06C] = SAPP_KEYCODE_F21; |
| 6283 | _sapp.keycodes[0x06D] = SAPP_KEYCODE_F22; |
| 6284 | _sapp.keycodes[0x06E] = SAPP_KEYCODE_F23; |
| 6285 | _sapp.keycodes[0x076] = SAPP_KEYCODE_F24; |
| 6286 | _sapp.keycodes[0x038] = SAPP_KEYCODE_LEFT_ALT; |
| 6287 | _sapp.keycodes[0x01D] = SAPP_KEYCODE_LEFT_CONTROL; |
| 6288 | _sapp.keycodes[0x02A] = SAPP_KEYCODE_LEFT_SHIFT; |
| 6289 | _sapp.keycodes[0x15B] = SAPP_KEYCODE_LEFT_SUPER; |
| 6290 | _sapp.keycodes[0x137] = SAPP_KEYCODE_PRINT_SCREEN; |
| 6291 | _sapp.keycodes[0x138] = SAPP_KEYCODE_RIGHT_ALT; |
| 6292 | _sapp.keycodes[0x11D] = SAPP_KEYCODE_RIGHT_CONTROL; |
| 6293 | _sapp.keycodes[0x036] = SAPP_KEYCODE_RIGHT_SHIFT; |
| 6294 | _sapp.keycodes[0x15C] = SAPP_KEYCODE_RIGHT_SUPER; |
| 6295 | _sapp.keycodes[0x150] = SAPP_KEYCODE_DOWN; |
| 6296 | _sapp.keycodes[0x14B] = SAPP_KEYCODE_LEFT; |
| 6297 | _sapp.keycodes[0x14D] = SAPP_KEYCODE_RIGHT; |
| 6298 | _sapp.keycodes[0x148] = SAPP_KEYCODE_UP; |
| 6299 | _sapp.keycodes[0x052] = SAPP_KEYCODE_KP_0; |
| 6300 | _sapp.keycodes[0x04F] = SAPP_KEYCODE_KP_1; |
| 6301 | _sapp.keycodes[0x050] = SAPP_KEYCODE_KP_2; |
| 6302 | _sapp.keycodes[0x051] = SAPP_KEYCODE_KP_3; |
| 6303 | _sapp.keycodes[0x04B] = SAPP_KEYCODE_KP_4; |
| 6304 | _sapp.keycodes[0x04C] = SAPP_KEYCODE_KP_5; |
| 6305 | _sapp.keycodes[0x04D] = SAPP_KEYCODE_KP_6; |
| 6306 | _sapp.keycodes[0x047] = SAPP_KEYCODE_KP_7; |
| 6307 | _sapp.keycodes[0x048] = SAPP_KEYCODE_KP_8; |
| 6308 | _sapp.keycodes[0x049] = SAPP_KEYCODE_KP_9; |
| 6309 | _sapp.keycodes[0x04E] = SAPP_KEYCODE_KP_ADD; |
| 6310 | _sapp.keycodes[0x053] = SAPP_KEYCODE_KP_DECIMAL; |
| 6311 | _sapp.keycodes[0x135] = SAPP_KEYCODE_KP_DIVIDE; |
| 6312 | _sapp.keycodes[0x11C] = SAPP_KEYCODE_KP_ENTER; |
| 6313 | _sapp.keycodes[0x037] = SAPP_KEYCODE_KP_MULTIPLY; |
| 6314 | _sapp.keycodes[0x04A] = SAPP_KEYCODE_KP_SUBTRACT; |
| 6315 | } |
| 6316 | #endif // _SAPP_WIN32 |
| 6317 | |
| 6318 | #if defined(_SAPP_WIN32) |
| 6319 | |
| 6320 | #if defined(SOKOL_D3D11) |
| 6321 | |
| 6322 | #if defined(__cplusplus) |
| 6323 | #define _sapp_d3d11_Release(self) (self)->Release() |
| 6324 | #define _sapp_win32_refiid(iid) iid |
| 6325 | #else |
| 6326 | #define _sapp_d3d11_Release(self) (self)->lpVtbl->Release(self) |
| 6327 | #define _sapp_win32_refiid(iid) &iid |
| 6328 | #endif |
| 6329 | |
| 6330 | #define _SAPP_SAFE_RELEASE(obj) if (obj) { _sapp_d3d11_Release(obj); obj=0; } |
| 6331 | |
| 6332 | |
| 6333 | static const IID _sapp_IID_ID3D11Texture2D = { 0x6f15aaf2,0xd208,0x4e89, {0x9a,0xb4,0x48,0x95,0x35,0xd3,0x4f,0x9c} }; |
| 6334 | static const IID _sapp_IID_IDXGIDevice1 = { 0x77db970f,0x6276,0x48ba, {0xba,0x28,0x07,0x01,0x43,0xb4,0x39,0x2c} }; |
| 6335 | static const IID _sapp_IID_IDXGIFactory = { 0x7b7166ec,0x21c7,0x44ae, {0xb2,0x1a,0xc9,0xae,0x32,0x1a,0xe3,0x69} }; |
| 6336 | |
| 6337 | static inline HRESULT _sapp_dxgi_GetBuffer(IDXGISwapChain* self, UINT Buffer, REFIID riid, void** ppSurface) { |
| 6338 | #if defined(__cplusplus) |
| 6339 | return self->GetBuffer(Buffer, riid, ppSurface); |
| 6340 | #else |
| 6341 | return self->lpVtbl->GetBuffer(self, Buffer, riid, ppSurface); |
| 6342 | #endif |
| 6343 | } |
| 6344 | |
| 6345 | static inline HRESULT _sapp_d3d11_QueryInterface(ID3D11Device* self, REFIID riid, void** ppvObject) { |
| 6346 | #if defined(__cplusplus) |
| 6347 | return self->QueryInterface(riid, ppvObject); |
| 6348 | #else |
| 6349 | return self->lpVtbl->QueryInterface(self, riid, ppvObject); |
| 6350 | #endif |
| 6351 | } |
| 6352 | |
| 6353 | static inline HRESULT _sapp_d3d11_CreateRenderTargetView(ID3D11Device* self, ID3D11Resource *pResource, const D3D11_RENDER_TARGET_VIEW_DESC* pDesc, ID3D11RenderTargetView** ppRTView) { |
| 6354 | #if defined(__cplusplus) |
| 6355 | return self->CreateRenderTargetView(pResource, pDesc, ppRTView); |
| 6356 | #else |
| 6357 | return self->lpVtbl->CreateRenderTargetView(self, pResource, pDesc, ppRTView); |
| 6358 | #endif |
| 6359 | } |
| 6360 | |
| 6361 | static inline HRESULT _sapp_d3d11_CreateTexture2D(ID3D11Device* self, const D3D11_TEXTURE2D_DESC* pDesc, const D3D11_SUBRESOURCE_DATA* pInitialData, ID3D11Texture2D** ppTexture2D) { |
| 6362 | #if defined(__cplusplus) |
| 6363 | return self->CreateTexture2D(pDesc, pInitialData, ppTexture2D); |
| 6364 | #else |
| 6365 | return self->lpVtbl->CreateTexture2D(self, pDesc, pInitialData, ppTexture2D); |
| 6366 | #endif |
| 6367 | } |
| 6368 | |
| 6369 | static inline HRESULT _sapp_d3d11_CreateDepthStencilView(ID3D11Device* self, ID3D11Resource* pResource, const D3D11_DEPTH_STENCIL_VIEW_DESC* pDesc, ID3D11DepthStencilView** ppDepthStencilView) { |
| 6370 | #if defined(__cplusplus) |
| 6371 | return self->CreateDepthStencilView(pResource, pDesc, ppDepthStencilView); |
| 6372 | #else |
| 6373 | return self->lpVtbl->CreateDepthStencilView(self, pResource, pDesc, ppDepthStencilView); |
| 6374 | #endif |
| 6375 | } |
| 6376 | |
| 6377 | static inline HRESULT _sapp_dxgi_ResizeBuffers(IDXGISwapChain* self, UINT BufferCount, UINT Width, UINT Height, DXGI_FORMAT NewFormat, UINT SwapChainFlags) { |
| 6378 | #if defined(__cplusplus) |
| 6379 | return self->ResizeBuffers(BufferCount, Width, Height, NewFormat, SwapChainFlags); |
| 6380 | #else |
| 6381 | return self->lpVtbl->ResizeBuffers(self, BufferCount, Width, Height, NewFormat, SwapChainFlags); |
| 6382 | #endif |
| 6383 | } |
| 6384 | |
| 6385 | static inline HRESULT _sapp_dxgi_Present(IDXGISwapChain* self, UINT SyncInterval, UINT Flags) { |
| 6386 | #if defined(__cplusplus) |
| 6387 | return self->Present(SyncInterval, Flags); |
| 6388 | #else |
| 6389 | return self->lpVtbl->Present(self, SyncInterval, Flags); |
| 6390 | #endif |
| 6391 | } |
| 6392 | |
| 6393 | static inline HRESULT _sapp_dxgi_GetFrameStatistics(IDXGISwapChain* self, DXGI_FRAME_STATISTICS* pStats) { |
| 6394 | #if defined(__cplusplus) |
| 6395 | return self->GetFrameStatistics(pStats); |
| 6396 | #else |
| 6397 | return self->lpVtbl->GetFrameStatistics(self, pStats); |
| 6398 | #endif |
| 6399 | } |
| 6400 | |
| 6401 | static inline HRESULT _sapp_dxgi_SetMaximumFrameLatency(IDXGIDevice1* self, UINT MaxLatency) { |
| 6402 | #if defined(__cplusplus) |
| 6403 | return self->SetMaximumFrameLatency(MaxLatency); |
| 6404 | #else |
| 6405 | return self->lpVtbl->SetMaximumFrameLatency(self, MaxLatency); |
| 6406 | #endif |
| 6407 | } |
| 6408 | |
| 6409 | static inline HRESULT _sapp_dxgi_GetAdapter(IDXGIDevice1* self, IDXGIAdapter** pAdapter) { |
| 6410 | #if defined(__cplusplus) |
| 6411 | return self->GetAdapter(pAdapter); |
| 6412 | #else |
| 6413 | return self->lpVtbl->GetAdapter(self, pAdapter); |
| 6414 | #endif |
| 6415 | } |
| 6416 | |
| 6417 | static inline HRESULT _sapp_dxgi_GetParent(IDXGIObject* self, REFIID riid, void** ppParent) { |
| 6418 | #if defined(__cplusplus) |
| 6419 | return self->GetParent(riid, ppParent); |
| 6420 | #else |
| 6421 | return self->lpVtbl->GetParent(self, riid, ppParent); |
| 6422 | #endif |
| 6423 | } |
| 6424 | |
| 6425 | static inline HRESULT _sapp_dxgi_MakeWindowAssociation(IDXGIFactory* self, HWND WindowHandle, UINT Flags) { |
| 6426 | #if defined(__cplusplus) |
| 6427 | return self->MakeWindowAssociation(WindowHandle, Flags); |
| 6428 | #else |
| 6429 | return self->lpVtbl->MakeWindowAssociation(self, WindowHandle, Flags); |
| 6430 | #endif |
| 6431 | } |
| 6432 | |
| 6433 | _SOKOL_PRIVATE void _sapp_d3d11_create_device_and_swapchain(void) { |
| 6434 | DXGI_SWAP_CHAIN_DESC* sc_desc = &_sapp.d3d11.swap_chain_desc; |
| 6435 | sc_desc->BufferDesc.Width = (UINT)_sapp.framebuffer_width; |
| 6436 | sc_desc->BufferDesc.Height = (UINT)_sapp.framebuffer_height; |
| 6437 | sc_desc->BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; |
| 6438 | sc_desc->BufferDesc.RefreshRate.Numerator = 60; |
| 6439 | sc_desc->BufferDesc.RefreshRate.Denominator = 1; |
| 6440 | sc_desc->OutputWindow = _sapp.win32.hwnd; |
| 6441 | sc_desc->Windowed = true; |
| 6442 | if (_sapp.win32.is_win10_or_greater) { |
| 6443 | sc_desc->BufferCount = 2; |
| 6444 | sc_desc->SwapEffect = (DXGI_SWAP_EFFECT) _SAPP_DXGI_SWAP_EFFECT_FLIP_DISCARD; |
| 6445 | _sapp.d3d11.use_dxgi_frame_stats = true; |
| 6446 | } |
| 6447 | else { |
| 6448 | sc_desc->BufferCount = 1; |
| 6449 | sc_desc->SwapEffect = DXGI_SWAP_EFFECT_DISCARD; |
| 6450 | _sapp.d3d11.use_dxgi_frame_stats = false; |
| 6451 | } |
| 6452 | sc_desc->SampleDesc.Count = 1; |
| 6453 | sc_desc->SampleDesc.Quality = 0; |
| 6454 | sc_desc->BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| 6455 | UINT create_flags = D3D11_CREATE_DEVICE_SINGLETHREADED | D3D11_CREATE_DEVICE_BGRA_SUPPORT; |
| 6456 | #if defined(SOKOL_DEBUG) |
| 6457 | create_flags |= D3D11_CREATE_DEVICE_DEBUG; |
| 6458 | #endif |
| 6459 | D3D_FEATURE_LEVEL feature_level; |
| 6460 | HRESULT hr = D3D11CreateDeviceAndSwapChain( |
| 6461 | NULL, /* pAdapter (use default) */ |
| 6462 | D3D_DRIVER_TYPE_HARDWARE, /* DriverType */ |
| 6463 | NULL, /* Software */ |
| 6464 | create_flags, /* Flags */ |
| 6465 | NULL, /* pFeatureLevels */ |
| 6466 | 0, /* FeatureLevels */ |
| 6467 | D3D11_SDK_VERSION, /* SDKVersion */ |
| 6468 | sc_desc, /* pSwapChainDesc */ |
| 6469 | &_sapp.d3d11.swap_chain, /* ppSwapChain */ |
| 6470 | &_sapp.d3d11.device, /* ppDevice */ |
| 6471 | &feature_level, /* pFeatureLevel */ |
| 6472 | &_sapp.d3d11.device_context); /* ppImmediateContext */ |
| 6473 | _SOKOL_UNUSED(hr); |
| 6474 | #if defined(SOKOL_DEBUG) |
| 6475 | if (!SUCCEEDED(hr)) { |
| 6476 | // if initialization with D3D11_CREATE_DEVICE_DEBUG fails, this could be because the |
| 6477 | // 'D3D11 debug layer' stopped working, indicated by the error message: |
| 6478 | // === |
| 6479 | // D3D11CreateDevice: Flags (0x2) were specified which require the D3D11 SDK Layers for Windows 10, but they are not present on the system. |
| 6480 | // These flags must be removed, or the Windows 10 SDK must be installed. |
| 6481 | // Flags include: D3D11_CREATE_DEVICE_DEBUG |
| 6482 | // === |
| 6483 | // |
| 6484 | // ...just retry with the DEBUG flag switched off |
| 6485 | _SAPP_ERROR(WIN32_D3D11_CREATE_DEVICE_AND_SWAPCHAIN_WITH_DEBUG_FAILED); |
| 6486 | create_flags &= ~D3D11_CREATE_DEVICE_DEBUG; |
| 6487 | hr = D3D11CreateDeviceAndSwapChain( |
| 6488 | NULL, /* pAdapter (use default) */ |
| 6489 | D3D_DRIVER_TYPE_HARDWARE, /* DriverType */ |
| 6490 | NULL, /* Software */ |
| 6491 | create_flags, /* Flags */ |
| 6492 | NULL, /* pFeatureLevels */ |
| 6493 | 0, /* FeatureLevels */ |
| 6494 | D3D11_SDK_VERSION, /* SDKVersion */ |
| 6495 | sc_desc, /* pSwapChainDesc */ |
| 6496 | &_sapp.d3d11.swap_chain, /* ppSwapChain */ |
| 6497 | &_sapp.d3d11.device, /* ppDevice */ |
| 6498 | &feature_level, /* pFeatureLevel */ |
| 6499 | &_sapp.d3d11.device_context); /* ppImmediateContext */ |
| 6500 | } |
| 6501 | #endif |
| 6502 | SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.swap_chain && _sapp.d3d11.device && _sapp.d3d11.device_context); |
| 6503 | |
| 6504 | // minimize frame latency, disable Alt-Enter |
| 6505 | hr = _sapp_d3d11_QueryInterface(_sapp.d3d11.device, _sapp_win32_refiid(_sapp_IID_IDXGIDevice1), (void**)&_sapp.d3d11.dxgi_device); |
| 6506 | if (SUCCEEDED(hr) && _sapp.d3d11.dxgi_device) { |
| 6507 | _sapp_dxgi_SetMaximumFrameLatency(_sapp.d3d11.dxgi_device, 1); |
| 6508 | IDXGIAdapter* dxgi_adapter = 0; |
| 6509 | hr = _sapp_dxgi_GetAdapter(_sapp.d3d11.dxgi_device, &dxgi_adapter); |
| 6510 | if (SUCCEEDED(hr) && dxgi_adapter) { |
| 6511 | IDXGIFactory* dxgi_factory = 0; |
| 6512 | hr = _sapp_dxgi_GetParent((IDXGIObject*)dxgi_adapter, _sapp_win32_refiid(_sapp_IID_IDXGIFactory), (void**)&dxgi_factory); |
| 6513 | if (SUCCEEDED(hr)) { |
| 6514 | _sapp_dxgi_MakeWindowAssociation(dxgi_factory, _sapp.win32.hwnd, DXGI_MWA_NO_ALT_ENTER|DXGI_MWA_NO_PRINT_SCREEN); |
| 6515 | _SAPP_SAFE_RELEASE(dxgi_factory); |
| 6516 | } |
| 6517 | else { |
| 6518 | _SAPP_ERROR(WIN32_D3D11_GET_IDXGIFACTORY_FAILED); |
| 6519 | } |
| 6520 | _SAPP_SAFE_RELEASE(dxgi_adapter); |
| 6521 | } |
| 6522 | else { |
| 6523 | _SAPP_ERROR(WIN32_D3D11_GET_IDXGIADAPTER_FAILED); |
| 6524 | } |
| 6525 | } |
| 6526 | else { |
| 6527 | _SAPP_PANIC(WIN32_D3D11_QUERY_INTERFACE_IDXGIDEVICE1_FAILED); |
| 6528 | } |
| 6529 | } |
| 6530 | |
| 6531 | _SOKOL_PRIVATE void _sapp_d3d11_destroy_device_and_swapchain(void) { |
| 6532 | _SAPP_SAFE_RELEASE(_sapp.d3d11.swap_chain); |
| 6533 | _SAPP_SAFE_RELEASE(_sapp.d3d11.dxgi_device); |
| 6534 | _SAPP_SAFE_RELEASE(_sapp.d3d11.device_context); |
| 6535 | _SAPP_SAFE_RELEASE(_sapp.d3d11.device); |
| 6536 | } |
| 6537 | |
| 6538 | _SOKOL_PRIVATE void _sapp_d3d11_create_default_render_target(void) { |
| 6539 | SOKOL_ASSERT(0 == _sapp.d3d11.rt); |
| 6540 | SOKOL_ASSERT(0 == _sapp.d3d11.rtv); |
| 6541 | SOKOL_ASSERT(0 == _sapp.d3d11.msaa_rt); |
| 6542 | SOKOL_ASSERT(0 == _sapp.d3d11.msaa_rtv); |
| 6543 | SOKOL_ASSERT(0 == _sapp.d3d11.ds); |
| 6544 | SOKOL_ASSERT(0 == _sapp.d3d11.dsv); |
| 6545 | |
| 6546 | HRESULT hr; |
| 6547 | |
| 6548 | /* view for the swapchain-created framebuffer */ |
| 6549 | hr = _sapp_dxgi_GetBuffer(_sapp.d3d11.swap_chain, 0, _sapp_win32_refiid(_sapp_IID_ID3D11Texture2D), (void**)&_sapp.d3d11.rt); |
| 6550 | SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.rt); |
| 6551 | hr = _sapp_d3d11_CreateRenderTargetView(_sapp.d3d11.device, (ID3D11Resource*)_sapp.d3d11.rt, NULL, &_sapp.d3d11.rtv); |
| 6552 | SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.rtv); |
| 6553 | |
| 6554 | /* common desc for MSAA and depth-stencil texture */ |
| 6555 | D3D11_TEXTURE2D_DESC tex_desc; |
| 6556 | _sapp_clear(&tex_desc, sizeof(tex_desc)); |
| 6557 | tex_desc.Width = (UINT)_sapp.framebuffer_width; |
| 6558 | tex_desc.Height = (UINT)_sapp.framebuffer_height; |
| 6559 | tex_desc.MipLevels = 1; |
| 6560 | tex_desc.ArraySize = 1; |
| 6561 | tex_desc.Usage = D3D11_USAGE_DEFAULT; |
| 6562 | tex_desc.BindFlags = D3D11_BIND_RENDER_TARGET; |
| 6563 | tex_desc.SampleDesc.Count = (UINT) _sapp.sample_count; |
| 6564 | tex_desc.SampleDesc.Quality = (UINT) (_sapp.sample_count > 1 ? D3D11_STANDARD_MULTISAMPLE_PATTERN : 0); |
| 6565 | |
| 6566 | /* create MSAA texture and view if antialiasing requested */ |
| 6567 | if (_sapp.sample_count > 1) { |
| 6568 | tex_desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; |
| 6569 | hr = _sapp_d3d11_CreateTexture2D(_sapp.d3d11.device, &tex_desc, NULL, &_sapp.d3d11.msaa_rt); |
| 6570 | SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.msaa_rt); |
| 6571 | hr = _sapp_d3d11_CreateRenderTargetView(_sapp.d3d11.device, (ID3D11Resource*)_sapp.d3d11.msaa_rt, NULL, &_sapp.d3d11.msaa_rtv); |
| 6572 | SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.msaa_rtv); |
| 6573 | } |
| 6574 | |
| 6575 | /* texture and view for the depth-stencil-surface */ |
| 6576 | tex_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; |
| 6577 | tex_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL; |
| 6578 | hr = _sapp_d3d11_CreateTexture2D(_sapp.d3d11.device, &tex_desc, NULL, &_sapp.d3d11.ds); |
| 6579 | SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.ds); |
| 6580 | hr = _sapp_d3d11_CreateDepthStencilView(_sapp.d3d11.device, (ID3D11Resource*)_sapp.d3d11.ds, NULL, &_sapp.d3d11.dsv); |
| 6581 | SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.dsv); |
| 6582 | } |
| 6583 | |
| 6584 | _SOKOL_PRIVATE void _sapp_d3d11_destroy_default_render_target(void) { |
| 6585 | _SAPP_SAFE_RELEASE(_sapp.d3d11.rt); |
| 6586 | _SAPP_SAFE_RELEASE(_sapp.d3d11.rtv); |
| 6587 | _SAPP_SAFE_RELEASE(_sapp.d3d11.msaa_rt); |
| 6588 | _SAPP_SAFE_RELEASE(_sapp.d3d11.msaa_rtv); |
| 6589 | _SAPP_SAFE_RELEASE(_sapp.d3d11.ds); |
| 6590 | _SAPP_SAFE_RELEASE(_sapp.d3d11.dsv); |
| 6591 | } |
| 6592 | |
| 6593 | _SOKOL_PRIVATE void _sapp_d3d11_resize_default_render_target(void) { |
| 6594 | if (_sapp.d3d11.swap_chain) { |
| 6595 | _sapp_d3d11_destroy_default_render_target(); |
| 6596 | _sapp_dxgi_ResizeBuffers(_sapp.d3d11.swap_chain, _sapp.d3d11.swap_chain_desc.BufferCount, (UINT)_sapp.framebuffer_width, (UINT)_sapp.framebuffer_height, DXGI_FORMAT_B8G8R8A8_UNORM, 0); |
| 6597 | _sapp_d3d11_create_default_render_target(); |
| 6598 | } |
| 6599 | } |
| 6600 | |
| 6601 | _SOKOL_PRIVATE void _sapp_d3d11_present(bool do_not_wait) { |
| 6602 | UINT flags = 0; |
| 6603 | if (_sapp.win32.is_win10_or_greater && do_not_wait) { |
| 6604 | /* this hack/workaround somewhat improves window-movement and -sizing |
| 6605 | responsiveness when rendering is controlled via WM_TIMER during window |
| 6606 | move and resize on NVIDIA cards on Win10 with recent drivers. |
| 6607 | */ |
| 6608 | flags = DXGI_PRESENT_DO_NOT_WAIT; |
| 6609 | } |
| 6610 | _sapp_dxgi_Present(_sapp.d3d11.swap_chain, (UINT)_sapp.swap_interval, flags); |
| 6611 | } |
| 6612 | |
| 6613 | #endif /* SOKOL_D3D11 */ |
| 6614 | |
| 6615 | #if defined(SOKOL_GLCORE) |
| 6616 | _SOKOL_PRIVATE void _sapp_wgl_init(void) { |
| 6617 | _sapp.wgl.opengl32 = LoadLibraryA("opengl32.dll"); |
| 6618 | if (!_sapp.wgl.opengl32) { |
| 6619 | _SAPP_PANIC(WIN32_LOAD_OPENGL32_DLL_FAILED); |
| 6620 | } |
| 6621 | SOKOL_ASSERT(_sapp.wgl.opengl32); |
| 6622 | _sapp.wgl.CreateContext = (PFN_wglCreateContext)(void*) GetProcAddress(_sapp.wgl.opengl32, "wglCreateContext"); |
| 6623 | SOKOL_ASSERT(_sapp.wgl.CreateContext); |
| 6624 | _sapp.wgl.DeleteContext = (PFN_wglDeleteContext)(void*) GetProcAddress(_sapp.wgl.opengl32, "wglDeleteContext"); |
| 6625 | SOKOL_ASSERT(_sapp.wgl.DeleteContext); |
| 6626 | _sapp.wgl.GetProcAddress = (PFN_wglGetProcAddress)(void*) GetProcAddress(_sapp.wgl.opengl32, "wglGetProcAddress"); |
| 6627 | SOKOL_ASSERT(_sapp.wgl.GetProcAddress); |
| 6628 | _sapp.wgl.GetCurrentDC = (PFN_wglGetCurrentDC)(void*) GetProcAddress(_sapp.wgl.opengl32, "wglGetCurrentDC"); |
| 6629 | SOKOL_ASSERT(_sapp.wgl.GetCurrentDC); |
| 6630 | _sapp.wgl.MakeCurrent = (PFN_wglMakeCurrent)(void*) GetProcAddress(_sapp.wgl.opengl32, "wglMakeCurrent"); |
| 6631 | SOKOL_ASSERT(_sapp.wgl.MakeCurrent); |
| 6632 | _sapp.wgl.GetIntegerv = (void(WINAPI*)(uint32_t, int32_t*)) GetProcAddress(_sapp.wgl.opengl32, "glGetIntegerv"); |
| 6633 | SOKOL_ASSERT(_sapp.wgl.GetIntegerv); |
| 6634 | |
| 6635 | _sapp.wgl.msg_hwnd = CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, |
| 6636 | L"SOKOLAPP", |
| 6637 | L"sokol-app message window", |
| 6638 | WS_CLIPSIBLINGS|WS_CLIPCHILDREN, |
| 6639 | 0, 0, 1, 1, |
| 6640 | NULL, NULL, |
| 6641 | GetModuleHandleW(NULL), |
| 6642 | NULL); |
| 6643 | if (!_sapp.wgl.msg_hwnd) { |
| 6644 | _SAPP_PANIC(WIN32_CREATE_HELPER_WINDOW_FAILED); |
| 6645 | } |
| 6646 | SOKOL_ASSERT(_sapp.wgl.msg_hwnd); |
| 6647 | ShowWindow(_sapp.wgl.msg_hwnd, SW_HIDE); |
| 6648 | MSG msg; |
| 6649 | while (PeekMessageW(&msg, _sapp.wgl.msg_hwnd, 0, 0, PM_REMOVE)) { |
| 6650 | TranslateMessage(&msg); |
| 6651 | DispatchMessageW(&msg); |
| 6652 | } |
| 6653 | _sapp.wgl.msg_dc = GetDC(_sapp.wgl.msg_hwnd); |
| 6654 | if (!_sapp.wgl.msg_dc) { |
| 6655 | _SAPP_PANIC(WIN32_HELPER_WINDOW_GETDC_FAILED); |
| 6656 | } |
| 6657 | } |
| 6658 | |
| 6659 | _SOKOL_PRIVATE void _sapp_wgl_shutdown(void) { |
| 6660 | SOKOL_ASSERT(_sapp.wgl.opengl32 && _sapp.wgl.msg_hwnd); |
| 6661 | DestroyWindow(_sapp.wgl.msg_hwnd); _sapp.wgl.msg_hwnd = 0; |
| 6662 | FreeLibrary(_sapp.wgl.opengl32); _sapp.wgl.opengl32 = 0; |
| 6663 | } |
| 6664 | |
| 6665 | _SOKOL_PRIVATE bool _sapp_wgl_has_ext(const char* ext, const char* extensions) { |
| 6666 | SOKOL_ASSERT(ext && extensions); |
| 6667 | const char* start = extensions; |
| 6668 | while (true) { |
| 6669 | const char* where = strstr(start, ext); |
| 6670 | if (!where) { |
| 6671 | return false; |
| 6672 | } |
| 6673 | const char* terminator = where + strlen(ext); |
| 6674 | if ((where == start) || (*(where - 1) == ' ')) { |
| 6675 | if (*terminator == ' ' || *terminator == '\0') { |
| 6676 | break; |
| 6677 | } |
| 6678 | } |
| 6679 | start = terminator; |
| 6680 | } |
| 6681 | return true; |
| 6682 | } |
| 6683 | |
| 6684 | _SOKOL_PRIVATE bool _sapp_wgl_ext_supported(const char* ext) { |
| 6685 | SOKOL_ASSERT(ext); |
| 6686 | if (_sapp.wgl.GetExtensionsStringEXT) { |
| 6687 | const char* extensions = _sapp.wgl.GetExtensionsStringEXT(); |
| 6688 | if (extensions) { |
| 6689 | if (_sapp_wgl_has_ext(ext, extensions)) { |
| 6690 | return true; |
| 6691 | } |
| 6692 | } |
| 6693 | } |
| 6694 | if (_sapp.wgl.GetExtensionsStringARB) { |
| 6695 | const char* extensions = _sapp.wgl.GetExtensionsStringARB(_sapp.wgl.GetCurrentDC()); |
| 6696 | if (extensions) { |
| 6697 | if (_sapp_wgl_has_ext(ext, extensions)) { |
| 6698 | return true; |
| 6699 | } |
| 6700 | } |
| 6701 | } |
| 6702 | return false; |
| 6703 | } |
| 6704 | |
| 6705 | _SOKOL_PRIVATE void _sapp_wgl_load_extensions(void) { |
| 6706 | SOKOL_ASSERT(_sapp.wgl.msg_dc); |
| 6707 | PIXELFORMATDESCRIPTOR pfd; |
| 6708 | _sapp_clear(&pfd, sizeof(pfd)); |
| 6709 | pfd.nSize = sizeof(pfd); |
| 6710 | pfd.nVersion = 1; |
| 6711 | pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; |
| 6712 | pfd.iPixelType = PFD_TYPE_RGBA; |
| 6713 | pfd.cColorBits = 24; |
| 6714 | if (!SetPixelFormat(_sapp.wgl.msg_dc, ChoosePixelFormat(_sapp.wgl.msg_dc, &pfd), &pfd)) { |
| 6715 | _SAPP_PANIC(WIN32_DUMMY_CONTEXT_SET_PIXELFORMAT_FAILED); |
| 6716 | } |
| 6717 | HGLRC rc = _sapp.wgl.CreateContext(_sapp.wgl.msg_dc); |
| 6718 | if (!rc) { |
| 6719 | _SAPP_PANIC(WIN32_CREATE_DUMMY_CONTEXT_FAILED); |
| 6720 | } |
| 6721 | if (!_sapp.wgl.MakeCurrent(_sapp.wgl.msg_dc, rc)) { |
| 6722 | _SAPP_PANIC(WIN32_DUMMY_CONTEXT_MAKE_CURRENT_FAILED); |
| 6723 | } |
| 6724 | _sapp.wgl.GetExtensionsStringEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC)(void*) _sapp.wgl.GetProcAddress("wglGetExtensionsStringEXT"); |
| 6725 | _sapp.wgl.GetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)(void*) _sapp.wgl.GetProcAddress("wglGetExtensionsStringARB"); |
| 6726 | _sapp.wgl.CreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)(void*) _sapp.wgl.GetProcAddress("wglCreateContextAttribsARB"); |
| 6727 | _sapp.wgl.SwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)(void*) _sapp.wgl.GetProcAddress("wglSwapIntervalEXT"); |
| 6728 | _sapp.wgl.GetPixelFormatAttribivARB = (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)(void*) _sapp.wgl.GetProcAddress("wglGetPixelFormatAttribivARB"); |
| 6729 | _sapp.wgl.arb_multisample = _sapp_wgl_ext_supported("WGL_ARB_multisample"); |
| 6730 | _sapp.wgl.arb_create_context = _sapp_wgl_ext_supported("WGL_ARB_create_context"); |
| 6731 | _sapp.wgl.arb_create_context_profile = _sapp_wgl_ext_supported("WGL_ARB_create_context_profile"); |
| 6732 | _sapp.wgl.ext_swap_control = _sapp_wgl_ext_supported("WGL_EXT_swap_control"); |
| 6733 | _sapp.wgl.arb_pixel_format = _sapp_wgl_ext_supported("WGL_ARB_pixel_format"); |
| 6734 | _sapp.wgl.MakeCurrent(_sapp.wgl.msg_dc, 0); |
| 6735 | _sapp.wgl.DeleteContext(rc); |
| 6736 | } |
| 6737 | |
| 6738 | _SOKOL_PRIVATE int _sapp_wgl_attrib(int pixel_format, int attrib) { |
| 6739 | SOKOL_ASSERT(_sapp.wgl.arb_pixel_format); |
| 6740 | int value = 0; |
| 6741 | if (!_sapp.wgl.GetPixelFormatAttribivARB(_sapp.win32.dc, pixel_format, 0, 1, &attrib, &value)) { |
| 6742 | _SAPP_PANIC(WIN32_GET_PIXELFORMAT_ATTRIB_FAILED); |
| 6743 | } |
| 6744 | return value; |
| 6745 | } |
| 6746 | |
| 6747 | _SOKOL_PRIVATE void _sapp_wgl_attribiv(int pixel_format, int num_attribs, const int* attribs, int* results) { |
| 6748 | SOKOL_ASSERT(_sapp.wgl.arb_pixel_format); |
| 6749 | if (!_sapp.wgl.GetPixelFormatAttribivARB(_sapp.win32.dc, pixel_format, 0, num_attribs, attribs, results)) { |
| 6750 | _SAPP_PANIC(WIN32_GET_PIXELFORMAT_ATTRIB_FAILED); |
| 6751 | } |
| 6752 | } |
| 6753 | |
| 6754 | _SOKOL_PRIVATE int _sapp_wgl_find_pixel_format(void) { |
| 6755 | SOKOL_ASSERT(_sapp.win32.dc); |
| 6756 | SOKOL_ASSERT(_sapp.wgl.arb_pixel_format); |
| 6757 | |
| 6758 | #define _sapp_wgl_num_query_tags (12) |
| 6759 | const int query_tags[_sapp_wgl_num_query_tags] = { |
| 6760 | WGL_SUPPORT_OPENGL_ARB, |
| 6761 | WGL_DRAW_TO_WINDOW_ARB, |
| 6762 | WGL_PIXEL_TYPE_ARB, |
| 6763 | WGL_ACCELERATION_ARB, |
| 6764 | WGL_DOUBLE_BUFFER_ARB, |
| 6765 | WGL_RED_BITS_ARB, |
| 6766 | WGL_GREEN_BITS_ARB, |
| 6767 | WGL_BLUE_BITS_ARB, |
| 6768 | WGL_ALPHA_BITS_ARB, |
| 6769 | WGL_DEPTH_BITS_ARB, |
| 6770 | WGL_STENCIL_BITS_ARB, |
| 6771 | WGL_SAMPLES_ARB, |
| 6772 | }; |
| 6773 | const int result_support_opengl_index = 0; |
| 6774 | const int result_draw_to_window_index = 1; |
| 6775 | const int result_pixel_type_index = 2; |
| 6776 | const int result_acceleration_index = 3; |
| 6777 | const int result_double_buffer_index = 4; |
| 6778 | const int result_red_bits_index = 5; |
| 6779 | const int result_green_bits_index = 6; |
| 6780 | const int result_blue_bits_index = 7; |
| 6781 | const int result_alpha_bits_index = 8; |
| 6782 | const int result_depth_bits_index = 9; |
| 6783 | const int result_stencil_bits_index = 10; |
| 6784 | const int result_samples_index = 11; |
| 6785 | |
| 6786 | int query_results[_sapp_wgl_num_query_tags] = {0}; |
| 6787 | // Drop the last item if multisample extension is not supported. |
| 6788 | // If in future querying with multiple extensions, will have to shuffle index values to have active extensions on the end. |
| 6789 | int query_count = _sapp_wgl_num_query_tags; |
| 6790 | if (!_sapp.wgl.arb_multisample) { |
| 6791 | query_count = _sapp_wgl_num_query_tags - 1; |
| 6792 | } |
| 6793 | |
| 6794 | int native_count = _sapp_wgl_attrib(1, WGL_NUMBER_PIXEL_FORMATS_ARB); |
| 6795 | |
| 6796 | _sapp_gl_fbconfig desired; |
| 6797 | _sapp_gl_init_fbconfig(&desired); |
| 6798 | desired.red_bits = 8; |
| 6799 | desired.green_bits = 8; |
| 6800 | desired.blue_bits = 8; |
| 6801 | desired.alpha_bits = 8; |
| 6802 | desired.depth_bits = 24; |
| 6803 | desired.stencil_bits = 8; |
| 6804 | desired.doublebuffer = true; |
| 6805 | desired.samples = (_sapp.sample_count > 1) ? _sapp.sample_count : 0; |
| 6806 | |
| 6807 | int pixel_format = 0; |
| 6808 | |
| 6809 | _sapp_gl_fbselect fbselect; |
| 6810 | _sapp_gl_init_fbselect(&fbselect); |
| 6811 | for (int i = 0; i < native_count; i++) { |
| 6812 | const int n = i + 1; |
| 6813 | _sapp_wgl_attribiv(n, query_count, query_tags, query_results); |
| 6814 | |
| 6815 | if (query_results[result_support_opengl_index] == 0 |
| 6816 | || query_results[result_draw_to_window_index] == 0 |
| 6817 | || query_results[result_pixel_type_index] != WGL_TYPE_RGBA_ARB |
| 6818 | || query_results[result_acceleration_index] == WGL_NO_ACCELERATION_ARB) |
| 6819 | { |
| 6820 | continue; |
| 6821 | } |
| 6822 | |
| 6823 | _sapp_gl_fbconfig u; |
| 6824 | _sapp_clear(&u, sizeof(u)); |
| 6825 | u.red_bits = query_results[result_red_bits_index]; |
| 6826 | u.green_bits = query_results[result_green_bits_index]; |
| 6827 | u.blue_bits = query_results[result_blue_bits_index]; |
| 6828 | u.alpha_bits = query_results[result_alpha_bits_index]; |
| 6829 | u.depth_bits = query_results[result_depth_bits_index]; |
| 6830 | u.stencil_bits = query_results[result_stencil_bits_index]; |
| 6831 | u.doublebuffer = 0 != query_results[result_double_buffer_index]; |
| 6832 | u.samples = query_results[result_samples_index]; // NOTE: If arb_multisample is not supported - just takes the default 0 |
| 6833 | |
| 6834 | // Test if this pixel format is better than the previous one |
| 6835 | if (_sapp_gl_select_fbconfig(&fbselect, &desired, &u)) { |
| 6836 | pixel_format = (uintptr_t)n; |
| 6837 | |
| 6838 | // Early exit if matching as good as possible |
| 6839 | if (fbselect.best_match) { |
| 6840 | break; |
| 6841 | } |
| 6842 | } |
| 6843 | } |
| 6844 | |
| 6845 | return pixel_format; |
| 6846 | } |
| 6847 | |
| 6848 | _SOKOL_PRIVATE void _sapp_wgl_create_context(void) { |
| 6849 | int pixel_format = _sapp_wgl_find_pixel_format(); |
| 6850 | if (0 == pixel_format) { |
| 6851 | _SAPP_PANIC(WIN32_WGL_FIND_PIXELFORMAT_FAILED); |
| 6852 | } |
| 6853 | PIXELFORMATDESCRIPTOR pfd; |
| 6854 | if (!DescribePixelFormat(_sapp.win32.dc, pixel_format, sizeof(pfd), &pfd)) { |
| 6855 | _SAPP_PANIC(WIN32_WGL_DESCRIBE_PIXELFORMAT_FAILED); |
| 6856 | } |
| 6857 | if (!SetPixelFormat(_sapp.win32.dc, pixel_format, &pfd)) { |
| 6858 | _SAPP_PANIC(WIN32_WGL_SET_PIXELFORMAT_FAILED); |
| 6859 | } |
| 6860 | if (!_sapp.wgl.arb_create_context) { |
| 6861 | _SAPP_PANIC(WIN32_WGL_ARB_CREATE_CONTEXT_REQUIRED); |
| 6862 | } |
| 6863 | if (!_sapp.wgl.arb_create_context_profile) { |
| 6864 | _SAPP_PANIC(WIN32_WGL_ARB_CREATE_CONTEXT_PROFILE_REQUIRED); |
| 6865 | } |
| 6866 | const int attrs[] = { |
| 6867 | WGL_CONTEXT_MAJOR_VERSION_ARB, _sapp.desc.gl_major_version, |
| 6868 | WGL_CONTEXT_MINOR_VERSION_ARB, _sapp.desc.gl_minor_version, |
| 6869 | #if defined(SOKOL_DEBUG) |
| 6870 | WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB | WGL_CONTEXT_DEBUG_BIT_ARB, |
| 6871 | #else |
| 6872 | WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, |
| 6873 | #endif |
| 6874 | WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, |
| 6875 | 0, 0 |
| 6876 | }; |
| 6877 | _sapp.wgl.gl_ctx = _sapp.wgl.CreateContextAttribsARB(_sapp.win32.dc, 0, attrs); |
| 6878 | if (!_sapp.wgl.gl_ctx) { |
| 6879 | const DWORD err = GetLastError(); |
| 6880 | if (err == (0xc0070000 | ERROR_INVALID_VERSION_ARB)) { |
| 6881 | _SAPP_PANIC(WIN32_WGL_OPENGL_3_2_NOT_SUPPORTED); |
| 6882 | } |
| 6883 | else if (err == (0xc0070000 | ERROR_INVALID_PROFILE_ARB)) { |
| 6884 | _SAPP_PANIC(WIN32_WGL_OPENGL_PROFILE_NOT_SUPPORTED); |
| 6885 | } |
| 6886 | else if (err == (0xc0070000 | ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB)) { |
| 6887 | _SAPP_PANIC(WIN32_WGL_INCOMPATIBLE_DEVICE_CONTEXT); |
| 6888 | } |
| 6889 | else { |
| 6890 | _SAPP_PANIC(WIN32_WGL_CREATE_CONTEXT_ATTRIBS_FAILED_OTHER); |
| 6891 | } |
| 6892 | } |
| 6893 | _sapp.wgl.MakeCurrent(_sapp.win32.dc, _sapp.wgl.gl_ctx); |
| 6894 | if (_sapp.wgl.ext_swap_control) { |
| 6895 | /* FIXME: DwmIsCompositionEnabled() (see GLFW) */ |
| 6896 | _sapp.wgl.SwapIntervalEXT(_sapp.swap_interval); |
| 6897 | } |
| 6898 | const uint32_t gl_framebuffer_binding = 0x8CA6; |
| 6899 | _sapp.wgl.GetIntegerv(gl_framebuffer_binding, (int32_t*)&_sapp.gl.framebuffer); |
| 6900 | } |
| 6901 | |
| 6902 | _SOKOL_PRIVATE void _sapp_wgl_destroy_context(void) { |
| 6903 | SOKOL_ASSERT(_sapp.wgl.gl_ctx); |
| 6904 | _sapp.wgl.DeleteContext(_sapp.wgl.gl_ctx); |
| 6905 | _sapp.wgl.gl_ctx = 0; |
| 6906 | } |
| 6907 | |
| 6908 | _SOKOL_PRIVATE void _sapp_wgl_swap_buffers(void) { |
| 6909 | SOKOL_ASSERT(_sapp.win32.dc); |
| 6910 | /* FIXME: DwmIsCompositionEnabled? (see GLFW) */ |
| 6911 | SwapBuffers(_sapp.win32.dc); |
| 6912 | } |
| 6913 | #endif /* SOKOL_GLCORE */ |
| 6914 | |
| 6915 | _SOKOL_PRIVATE bool _sapp_win32_wide_to_utf8(const wchar_t* src, char* dst, int dst_num_bytes) { |
| 6916 | SOKOL_ASSERT(src && dst && (dst_num_bytes > 1)); |
| 6917 | _sapp_clear(dst, (size_t)dst_num_bytes); |
| 6918 | const int bytes_needed = WideCharToMultiByte(CP_UTF8, 0, src, -1, NULL, 0, NULL, NULL); |
| 6919 | if (bytes_needed <= dst_num_bytes) { |
| 6920 | WideCharToMultiByte(CP_UTF8, 0, src, -1, dst, dst_num_bytes, NULL, NULL); |
| 6921 | return true; |
| 6922 | } |
| 6923 | else { |
| 6924 | return false; |
| 6925 | } |
| 6926 | } |
| 6927 | |
| 6928 | /* updates current window and framebuffer size from the window's client rect, returns true if size has changed */ |
| 6929 | _SOKOL_PRIVATE bool _sapp_win32_update_dimensions(void) { |
| 6930 | RECT rect; |
| 6931 | if (GetClientRect(_sapp.win32.hwnd, &rect)) { |
| 6932 | float window_width = (float)(rect.right - rect.left) / _sapp.win32.dpi.window_scale; |
| 6933 | float window_height = (float)(rect.bottom - rect.top) / _sapp.win32.dpi.window_scale; |
| 6934 | _sapp.window_width = (int)roundf(window_width); |
| 6935 | _sapp.window_height = (int)roundf(window_height); |
| 6936 | int fb_width = (int)roundf(window_width * _sapp.win32.dpi.content_scale); |
| 6937 | int fb_height = (int)roundf(window_height * _sapp.win32.dpi.content_scale); |
| 6938 | /* prevent a framebuffer size of 0 when window is minimized */ |
| 6939 | if (0 == fb_width) { |
| 6940 | fb_width = 1; |
| 6941 | } |
| 6942 | if (0 == fb_height) { |
| 6943 | fb_height = 1; |
| 6944 | } |
| 6945 | if ((fb_width != _sapp.framebuffer_width) || (fb_height != _sapp.framebuffer_height)) { |
| 6946 | _sapp.framebuffer_width = fb_width; |
| 6947 | _sapp.framebuffer_height = fb_height; |
| 6948 | return true; |
| 6949 | } |
| 6950 | } |
| 6951 | else { |
| 6952 | _sapp.window_width = _sapp.window_height = 1; |
| 6953 | _sapp.framebuffer_width = _sapp.framebuffer_height = 1; |
| 6954 | } |
| 6955 | return false; |
| 6956 | } |
| 6957 | |
| 6958 | _SOKOL_PRIVATE void _sapp_win32_set_fullscreen(bool fullscreen, UINT swp_flags) { |
| 6959 | HMONITOR monitor = MonitorFromWindow(_sapp.win32.hwnd, MONITOR_DEFAULTTONEAREST); |
| 6960 | MONITORINFO minfo; |
| 6961 | _sapp_clear(&minfo, sizeof(minfo)); |
| 6962 | minfo.cbSize = sizeof(MONITORINFO); |
| 6963 | GetMonitorInfo(monitor, &minfo); |
| 6964 | const RECT mr = minfo.rcMonitor; |
| 6965 | const int monitor_w = mr.right - mr.left; |
| 6966 | const int monitor_h = mr.bottom - mr.top; |
| 6967 | |
| 6968 | const DWORD win_ex_style = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; |
| 6969 | DWORD win_style; |
| 6970 | RECT rect = { 0, 0, 0, 0 }; |
| 6971 | |
| 6972 | _sapp.fullscreen = fullscreen; |
| 6973 | if (!_sapp.fullscreen) { |
| 6974 | win_style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX; |
| 6975 | rect = _sapp.win32.stored_window_rect; |
| 6976 | } |
| 6977 | else { |
| 6978 | GetWindowRect(_sapp.win32.hwnd, &_sapp.win32.stored_window_rect); |
| 6979 | win_style = WS_POPUP | WS_SYSMENU | WS_VISIBLE; |
| 6980 | rect.left = mr.left; |
| 6981 | rect.top = mr.top; |
| 6982 | rect.right = rect.left + monitor_w; |
| 6983 | rect.bottom = rect.top + monitor_h; |
| 6984 | AdjustWindowRectEx(&rect, win_style, FALSE, win_ex_style); |
| 6985 | } |
| 6986 | const int win_w = rect.right - rect.left; |
| 6987 | const int win_h = rect.bottom - rect.top; |
| 6988 | const int win_x = rect.left; |
| 6989 | const int win_y = rect.top; |
| 6990 | SetWindowLongPtr(_sapp.win32.hwnd, GWL_STYLE, win_style); |
| 6991 | SetWindowPos(_sapp.win32.hwnd, HWND_TOP, win_x, win_y, win_w, win_h, swp_flags | SWP_FRAMECHANGED); |
| 6992 | } |
| 6993 | |
| 6994 | _SOKOL_PRIVATE void _sapp_win32_toggle_fullscreen(void) { |
| 6995 | _sapp_win32_set_fullscreen(!_sapp.fullscreen, SWP_SHOWWINDOW); |
| 6996 | } |
| 6997 | |
| 6998 | _SOKOL_PRIVATE void _sapp_win32_init_cursor(sapp_mouse_cursor cursor) { |
| 6999 | SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM)); |
| 7000 | // NOTE: the OCR_* constants are only defined if OEMRESOURCE is defined |
| 7001 | // before windows.h is included, but we can't guarantee that because |
| 7002 | // the sokol_app.h implementation may be included with other implementations |
| 7003 | // in the same compilation unit |
| 7004 | int id = 0; |
| 7005 | switch (cursor) { |
| 7006 | case SAPP_MOUSECURSOR_ARROW: id = 32512; break; // OCR_NORMAL |
| 7007 | case SAPP_MOUSECURSOR_IBEAM: id = 32513; break; // OCR_IBEAM |
| 7008 | case SAPP_MOUSECURSOR_CROSSHAIR: id = 32515; break; // OCR_CROSS |
| 7009 | case SAPP_MOUSECURSOR_POINTING_HAND: id = 32649; break; // OCR_HAND |
| 7010 | case SAPP_MOUSECURSOR_RESIZE_EW: id = 32644; break; // OCR_SIZEWE |
| 7011 | case SAPP_MOUSECURSOR_RESIZE_NS: id = 32645; break; // OCR_SIZENS |
| 7012 | case SAPP_MOUSECURSOR_RESIZE_NWSE: id = 32642; break; // OCR_SIZENWSE |
| 7013 | case SAPP_MOUSECURSOR_RESIZE_NESW: id = 32643; break; // OCR_SIZENESW |
| 7014 | case SAPP_MOUSECURSOR_RESIZE_ALL: id = 32646; break; // OCR_SIZEALL |
| 7015 | case SAPP_MOUSECURSOR_NOT_ALLOWED: id = 32648; break; // OCR_NO |
| 7016 | default: break; |
| 7017 | } |
| 7018 | if (id != 0) { |
| 7019 | _sapp.win32.cursors[cursor] = (HCURSOR)LoadImageW(NULL, MAKEINTRESOURCEW(id), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE|LR_SHARED); |
| 7020 | } |
| 7021 | // fallback: default cursor |
| 7022 | if (0 == _sapp.win32.cursors[cursor]) { |
| 7023 | // 32512 => IDC_ARROW |
| 7024 | _sapp.win32.cursors[cursor] = LoadCursorW(NULL, MAKEINTRESOURCEW(32512)); |
| 7025 | } |
| 7026 | SOKOL_ASSERT(0 != _sapp.win32.cursors[cursor]); |
| 7027 | } |
| 7028 | |
| 7029 | _SOKOL_PRIVATE void _sapp_win32_init_cursors(void) { |
| 7030 | for (int i = 0; i < _SAPP_MOUSECURSOR_NUM; i++) { |
| 7031 | _sapp_win32_init_cursor((sapp_mouse_cursor)i); |
| 7032 | } |
| 7033 | } |
| 7034 | |
| 7035 | _SOKOL_PRIVATE bool _sapp_win32_cursor_in_content_area(void) { |
| 7036 | POINT pos; |
| 7037 | if (!GetCursorPos(&pos)) { |
| 7038 | return false; |
| 7039 | } |
| 7040 | if (WindowFromPoint(pos) != _sapp.win32.hwnd) { |
| 7041 | return false; |
| 7042 | } |
| 7043 | RECT area; |
| 7044 | GetClientRect(_sapp.win32.hwnd, &area); |
| 7045 | ClientToScreen(_sapp.win32.hwnd, (POINT*)&area.left); |
| 7046 | ClientToScreen(_sapp.win32.hwnd, (POINT*)&area.right); |
| 7047 | return PtInRect(&area, pos) == TRUE; |
| 7048 | } |
| 7049 | |
| 7050 | _SOKOL_PRIVATE void _sapp_win32_update_cursor(sapp_mouse_cursor cursor, bool shown, bool skip_area_test) { |
| 7051 | // NOTE: when called from WM_SETCURSOR, the area test would be redundant |
| 7052 | if (!skip_area_test) { |
| 7053 | if (!_sapp_win32_cursor_in_content_area()) { |
| 7054 | return; |
| 7055 | } |
| 7056 | } |
| 7057 | if (!shown) { |
| 7058 | SetCursor(NULL); |
| 7059 | } |
| 7060 | else { |
| 7061 | SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM)); |
| 7062 | SOKOL_ASSERT(0 != _sapp.win32.cursors[cursor]); |
| 7063 | SetCursor(_sapp.win32.cursors[cursor]); |
| 7064 | } |
| 7065 | } |
| 7066 | |
| 7067 | _SOKOL_PRIVATE void _sapp_win32_capture_mouse(uint8_t btn_mask) { |
| 7068 | if (0 == _sapp.win32.mouse_capture_mask) { |
| 7069 | SetCapture(_sapp.win32.hwnd); |
| 7070 | } |
| 7071 | _sapp.win32.mouse_capture_mask |= btn_mask; |
| 7072 | } |
| 7073 | |
| 7074 | _SOKOL_PRIVATE void _sapp_win32_release_mouse(uint8_t btn_mask) { |
| 7075 | if (0 != _sapp.win32.mouse_capture_mask) { |
| 7076 | _sapp.win32.mouse_capture_mask &= ~btn_mask; |
| 7077 | if (0 == _sapp.win32.mouse_capture_mask) { |
| 7078 | ReleaseCapture(); |
| 7079 | } |
| 7080 | } |
| 7081 | } |
| 7082 | |
| 7083 | _SOKOL_PRIVATE void _sapp_win32_lock_mouse(bool lock) { |
| 7084 | if (lock == _sapp.mouse.locked) { |
| 7085 | return; |
| 7086 | } |
| 7087 | _sapp.mouse.dx = 0.0f; |
| 7088 | _sapp.mouse.dy = 0.0f; |
| 7089 | _sapp.mouse.locked = lock; |
| 7090 | _sapp_win32_release_mouse(0xFF); |
| 7091 | if (_sapp.mouse.locked) { |
| 7092 | /* store the current mouse position, so it can be restored when unlocked */ |
| 7093 | POINT pos; |
| 7094 | BOOL res = GetCursorPos(&pos); |
| 7095 | SOKOL_ASSERT(res); _SOKOL_UNUSED(res); |
| 7096 | _sapp.win32.mouse_locked_x = pos.x; |
| 7097 | _sapp.win32.mouse_locked_y = pos.y; |
| 7098 | |
| 7099 | /* while the mouse is locked, make the mouse cursor invisible and |
| 7100 | confine the mouse movement to a small rectangle inside our window |
| 7101 | (so that we don't miss any mouse up events) |
| 7102 | */ |
| 7103 | RECT client_rect = { |
| 7104 | _sapp.win32.mouse_locked_x, |
| 7105 | _sapp.win32.mouse_locked_y, |
| 7106 | _sapp.win32.mouse_locked_x, |
| 7107 | _sapp.win32.mouse_locked_y |
| 7108 | }; |
| 7109 | ClipCursor(&client_rect); |
| 7110 | |
| 7111 | /* make the mouse cursor invisible, this will stack with sapp_show_mouse() */ |
| 7112 | ShowCursor(FALSE); |
| 7113 | |
| 7114 | /* enable raw input for mouse, starts sending WM_INPUT messages to WinProc (see GLFW) */ |
| 7115 | const RAWINPUTDEVICE rid = { |
| 7116 | 0x01, // usUsagePage: HID_USAGE_PAGE_GENERIC |
| 7117 | 0x02, // usUsage: HID_USAGE_GENERIC_MOUSE |
| 7118 | 0, // dwFlags |
| 7119 | _sapp.win32.hwnd // hwndTarget |
| 7120 | }; |
| 7121 | if (!RegisterRawInputDevices(&rid, 1, sizeof(rid))) { |
| 7122 | _SAPP_ERROR(WIN32_REGISTER_RAW_INPUT_DEVICES_FAILED_MOUSE_LOCK); |
| 7123 | } |
| 7124 | /* in case the raw mouse device only supports absolute position reporting, |
| 7125 | we need to skip the dx/dy compution for the first WM_INPUT event |
| 7126 | */ |
| 7127 | _sapp.win32.raw_input_mousepos_valid = false; |
| 7128 | } |
| 7129 | else { |
| 7130 | /* disable raw input for mouse */ |
| 7131 | const RAWINPUTDEVICE rid = { 0x01, 0x02, RIDEV_REMOVE, NULL }; |
| 7132 | if (!RegisterRawInputDevices(&rid, 1, sizeof(rid))) { |
| 7133 | _SAPP_ERROR(WIN32_REGISTER_RAW_INPUT_DEVICES_FAILED_MOUSE_UNLOCK); |
| 7134 | } |
| 7135 | |
| 7136 | /* let the mouse roam freely again */ |
| 7137 | ClipCursor(NULL); |
| 7138 | ShowCursor(TRUE); |
| 7139 | |
| 7140 | /* restore the 'pre-locked' mouse position */ |
| 7141 | BOOL res = SetCursorPos(_sapp.win32.mouse_locked_x, _sapp.win32.mouse_locked_y); |
| 7142 | SOKOL_ASSERT(res); _SOKOL_UNUSED(res); |
| 7143 | } |
| 7144 | } |
| 7145 | |
| 7146 | _SOKOL_PRIVATE bool _sapp_win32_update_monitor(void) { |
| 7147 | const HMONITOR cur_monitor = MonitorFromWindow(_sapp.win32.hwnd, MONITOR_DEFAULTTONULL); |
| 7148 | if (cur_monitor != _sapp.win32.hmonitor) { |
| 7149 | _sapp.win32.hmonitor = cur_monitor; |
| 7150 | return true; |
| 7151 | } |
| 7152 | else { |
| 7153 | return false; |
| 7154 | } |
| 7155 | } |
| 7156 | |
| 7157 | _SOKOL_PRIVATE uint32_t _sapp_win32_mods(void) { |
| 7158 | uint32_t mods = 0; |
| 7159 | if (GetKeyState(VK_SHIFT) & (1<<15)) { |
| 7160 | mods |= SAPP_MODIFIER_SHIFT; |
| 7161 | } |
| 7162 | if (GetKeyState(VK_CONTROL) & (1<<15)) { |
| 7163 | mods |= SAPP_MODIFIER_CTRL; |
| 7164 | } |
| 7165 | if (GetKeyState(VK_MENU) & (1<<15)) { |
| 7166 | mods |= SAPP_MODIFIER_ALT; |
| 7167 | } |
| 7168 | if ((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & (1<<15)) { |
| 7169 | mods |= SAPP_MODIFIER_SUPER; |
| 7170 | } |
| 7171 | const bool swapped = (TRUE == GetSystemMetrics(SM_SWAPBUTTON)); |
| 7172 | if (GetAsyncKeyState(VK_LBUTTON)) { |
| 7173 | mods |= swapped ? SAPP_MODIFIER_RMB : SAPP_MODIFIER_LMB; |
| 7174 | } |
| 7175 | if (GetAsyncKeyState(VK_RBUTTON)) { |
| 7176 | mods |= swapped ? SAPP_MODIFIER_LMB : SAPP_MODIFIER_RMB; |
| 7177 | } |
| 7178 | if (GetAsyncKeyState(VK_MBUTTON)) { |
| 7179 | mods |= SAPP_MODIFIER_MMB; |
| 7180 | } |
| 7181 | return mods; |
| 7182 | } |
| 7183 | |
| 7184 | _SOKOL_PRIVATE void _sapp_win32_mouse_update(LPARAM lParam) { |
| 7185 | if (!_sapp.mouse.locked) { |
| 7186 | const float new_x = (float)GET_X_LPARAM(lParam) * _sapp.win32.dpi.mouse_scale; |
| 7187 | const float new_y = (float)GET_Y_LPARAM(lParam) * _sapp.win32.dpi.mouse_scale; |
| 7188 | if (_sapp.mouse.pos_valid) { |
| 7189 | // don't update dx/dy in the very first event |
| 7190 | _sapp.mouse.dx = new_x - _sapp.mouse.x; |
| 7191 | _sapp.mouse.dy = new_y - _sapp.mouse.y; |
| 7192 | } |
| 7193 | _sapp.mouse.x = new_x; |
| 7194 | _sapp.mouse.y = new_y; |
| 7195 | _sapp.mouse.pos_valid = true; |
| 7196 | } |
| 7197 | } |
| 7198 | |
| 7199 | _SOKOL_PRIVATE void _sapp_win32_mouse_event(sapp_event_type type, sapp_mousebutton btn) { |
| 7200 | if (_sapp_events_enabled()) { |
| 7201 | _sapp_init_event(type); |
| 7202 | _sapp.event.modifiers = _sapp_win32_mods(); |
| 7203 | _sapp.event.mouse_button = btn; |
| 7204 | _sapp_call_event(&_sapp.event); |
| 7205 | } |
| 7206 | } |
| 7207 | |
| 7208 | _SOKOL_PRIVATE void _sapp_win32_scroll_event(float x, float y) { |
| 7209 | if (_sapp_events_enabled()) { |
| 7210 | _sapp_init_event(SAPP_EVENTTYPE_MOUSE_SCROLL); |
| 7211 | _sapp.event.modifiers = _sapp_win32_mods(); |
| 7212 | _sapp.event.scroll_x = -x / 30.0f; |
| 7213 | _sapp.event.scroll_y = y / 30.0f; |
| 7214 | _sapp_call_event(&_sapp.event); |
| 7215 | } |
| 7216 | } |
| 7217 | |
| 7218 | _SOKOL_PRIVATE void _sapp_win32_key_event(sapp_event_type type, int vk, bool repeat) { |
| 7219 | if (_sapp_events_enabled() && (vk < SAPP_MAX_KEYCODES)) { |
| 7220 | _sapp_init_event(type); |
| 7221 | _sapp.event.modifiers = _sapp_win32_mods(); |
| 7222 | _sapp.event.key_code = _sapp.keycodes[vk]; |
| 7223 | _sapp.event.key_repeat = repeat; |
| 7224 | _sapp_call_event(&_sapp.event); |
| 7225 | /* check if a CLIPBOARD_PASTED event must be sent too */ |
| 7226 | if (_sapp.clipboard.enabled && |
| 7227 | (type == SAPP_EVENTTYPE_KEY_DOWN) && |
| 7228 | (_sapp.event.modifiers == SAPP_MODIFIER_CTRL) && |
| 7229 | (_sapp.event.key_code == SAPP_KEYCODE_V)) |
| 7230 | { |
| 7231 | _sapp_init_event(SAPP_EVENTTYPE_CLIPBOARD_PASTED); |
| 7232 | _sapp_call_event(&_sapp.event); |
| 7233 | } |
| 7234 | } |
| 7235 | } |
| 7236 | |
| 7237 | _SOKOL_PRIVATE void _sapp_win32_char_event(uint32_t c, bool repeat) { |
| 7238 | if (_sapp_events_enabled() && (c >= 32)) { |
| 7239 | _sapp_init_event(SAPP_EVENTTYPE_CHAR); |
| 7240 | _sapp.event.modifiers = _sapp_win32_mods(); |
| 7241 | _sapp.event.char_code = c; |
| 7242 | _sapp.event.key_repeat = repeat; |
| 7243 | _sapp_call_event(&_sapp.event); |
| 7244 | } |
| 7245 | } |
| 7246 | |
| 7247 | _SOKOL_PRIVATE void _sapp_win32_dpi_changed(HWND hWnd, LPRECT proposed_win_rect) { |
| 7248 | /* called on WM_DPICHANGED, which will only be sent to the application |
| 7249 | if sapp_desc.high_dpi is true and the Windows version is recent enough |
| 7250 | to support DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 |
| 7251 | */ |
| 7252 | SOKOL_ASSERT(_sapp.desc.high_dpi); |
| 7253 | HINSTANCE user32 = LoadLibraryA("user32.dll"); |
| 7254 | if (!user32) { |
| 7255 | return; |
| 7256 | } |
| 7257 | typedef UINT(WINAPI * GETDPIFORWINDOW_T)(HWND hwnd); |
| 7258 | GETDPIFORWINDOW_T fn_getdpiforwindow = (GETDPIFORWINDOW_T)(void*)GetProcAddress(user32, "GetDpiForWindow"); |
| 7259 | if (fn_getdpiforwindow) { |
| 7260 | UINT dpix = fn_getdpiforwindow(_sapp.win32.hwnd); |
| 7261 | // NOTE: for high-dpi apps, mouse_scale remains one |
| 7262 | _sapp.win32.dpi.window_scale = (float)dpix / 96.0f; |
| 7263 | _sapp.win32.dpi.content_scale = _sapp.win32.dpi.window_scale; |
| 7264 | _sapp.dpi_scale = _sapp.win32.dpi.window_scale; |
| 7265 | SetWindowPos(hWnd, 0, |
| 7266 | proposed_win_rect->left, |
| 7267 | proposed_win_rect->top, |
| 7268 | proposed_win_rect->right - proposed_win_rect->left, |
| 7269 | proposed_win_rect->bottom - proposed_win_rect->top, |
| 7270 | SWP_NOZORDER | SWP_NOACTIVATE); |
| 7271 | } |
| 7272 | FreeLibrary(user32); |
| 7273 | } |
| 7274 | |
| 7275 | _SOKOL_PRIVATE void _sapp_win32_files_dropped(HDROP hdrop) { |
| 7276 | if (!_sapp.drop.enabled) { |
| 7277 | return; |
| 7278 | } |
| 7279 | _sapp_clear_drop_buffer(); |
| 7280 | bool drop_failed = false; |
| 7281 | const int count = (int) DragQueryFileW(hdrop, 0xffffffff, NULL, 0); |
| 7282 | _sapp.drop.num_files = (count > _sapp.drop.max_files) ? _sapp.drop.max_files : count; |
| 7283 | for (UINT i = 0; i < (UINT)_sapp.drop.num_files; i++) { |
| 7284 | const UINT num_chars = DragQueryFileW(hdrop, i, NULL, 0) + 1; |
| 7285 | WCHAR* buffer = (WCHAR*) _sapp_malloc_clear(num_chars * sizeof(WCHAR)); |
| 7286 | DragQueryFileW(hdrop, i, buffer, num_chars); |
| 7287 | if (!_sapp_win32_wide_to_utf8(buffer, _sapp_dropped_file_path_ptr((int)i), _sapp.drop.max_path_length)) { |
| 7288 | _SAPP_ERROR(DROPPED_FILE_PATH_TOO_LONG); |
| 7289 | drop_failed = true; |
| 7290 | } |
| 7291 | _sapp_free(buffer); |
| 7292 | } |
| 7293 | DragFinish(hdrop); |
| 7294 | if (!drop_failed) { |
| 7295 | if (_sapp_events_enabled()) { |
| 7296 | _sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED); |
| 7297 | _sapp.event.modifiers = _sapp_win32_mods(); |
| 7298 | _sapp_call_event(&_sapp.event); |
| 7299 | } |
| 7300 | } |
| 7301 | else { |
| 7302 | _sapp_clear_drop_buffer(); |
| 7303 | _sapp.drop.num_files = 0; |
| 7304 | } |
| 7305 | } |
| 7306 | |
| 7307 | _SOKOL_PRIVATE void _sapp_win32_timing_measure(void) { |
| 7308 | #if defined(SOKOL_D3D11) |
| 7309 | // on D3D11, use the more precise DXGI timestamp |
| 7310 | if (_sapp.d3d11.use_dxgi_frame_stats) { |
| 7311 | DXGI_FRAME_STATISTICS dxgi_stats; |
| 7312 | _sapp_clear(&dxgi_stats, sizeof(dxgi_stats)); |
| 7313 | HRESULT hr = _sapp_dxgi_GetFrameStatistics(_sapp.d3d11.swap_chain, &dxgi_stats); |
| 7314 | if (SUCCEEDED(hr)) { |
| 7315 | if (dxgi_stats.SyncRefreshCount != _sapp.d3d11.sync_refresh_count) { |
| 7316 | if ((_sapp.d3d11.sync_refresh_count + 1) != dxgi_stats.SyncRefreshCount) { |
| 7317 | _sapp_timing_discontinuity(&_sapp.timing); |
| 7318 | } |
| 7319 | _sapp.d3d11.sync_refresh_count = dxgi_stats.SyncRefreshCount; |
| 7320 | LARGE_INTEGER qpc = dxgi_stats.SyncQPCTime; |
| 7321 | const uint64_t now = (uint64_t)_sapp_int64_muldiv(qpc.QuadPart - _sapp.timing.timestamp.win.start.QuadPart, 1000000000, _sapp.timing.timestamp.win.freq.QuadPart); |
| 7322 | _sapp_timing_external(&_sapp.timing, (double)now / 1000000000.0); |
| 7323 | } |
| 7324 | return; |
| 7325 | } |
| 7326 | } |
| 7327 | // fallback if swap model isn't "flip-discard" or GetFrameStatistics failed for another reason |
| 7328 | _sapp_timing_measure(&_sapp.timing); |
| 7329 | #endif |
| 7330 | #if defined(SOKOL_GLCORE) |
| 7331 | _sapp_timing_measure(&_sapp.timing); |
| 7332 | #endif |
| 7333 | #if defined(SOKOL_NOAPI) |
| 7334 | _sapp_timing_measure(&_sapp.timing); |
| 7335 | #endif |
| 7336 | } |
| 7337 | |
| 7338 | _SOKOL_PRIVATE LRESULT CALLBACK _sapp_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
| 7339 | if (!_sapp.win32.in_create_window) { |
| 7340 | switch (uMsg) { |
| 7341 | case WM_CLOSE: |
| 7342 | /* only give user a chance to intervene when sapp_quit() wasn't already called */ |
| 7343 | if (!_sapp.quit_ordered) { |
| 7344 | /* if window should be closed and event handling is enabled, give user code |
| 7345 | a change to intervene via sapp_cancel_quit() |
| 7346 | */ |
| 7347 | _sapp.quit_requested = true; |
| 7348 | _sapp_win32_app_event(SAPP_EVENTTYPE_QUIT_REQUESTED); |
| 7349 | /* if user code hasn't intervened, quit the app */ |
| 7350 | if (_sapp.quit_requested) { |
| 7351 | _sapp.quit_ordered = true; |
| 7352 | } |
| 7353 | } |
| 7354 | if (_sapp.quit_ordered) { |
| 7355 | PostQuitMessage(0); |
| 7356 | } |
| 7357 | return 0; |
| 7358 | case WM_SYSCOMMAND: |
| 7359 | switch (wParam & 0xFFF0) { |
| 7360 | case SC_SCREENSAVE: |
| 7361 | case SC_MONITORPOWER: |
| 7362 | if (_sapp.fullscreen) { |
| 7363 | /* disable screen saver and blanking in fullscreen mode */ |
| 7364 | return 0; |
| 7365 | } |
| 7366 | break; |
| 7367 | case SC_KEYMENU: |
| 7368 | /* user trying to access menu via ALT */ |
| 7369 | return 0; |
| 7370 | } |
| 7371 | break; |
| 7372 | case WM_ERASEBKGND: |
| 7373 | return 1; |
| 7374 | case WM_SIZE: |
| 7375 | { |
| 7376 | const bool iconified = wParam == SIZE_MINIMIZED; |
| 7377 | if (iconified != _sapp.win32.iconified) { |
| 7378 | _sapp.win32.iconified = iconified; |
| 7379 | if (iconified) { |
| 7380 | _sapp_win32_app_event(SAPP_EVENTTYPE_ICONIFIED); |
| 7381 | } |
| 7382 | else { |
| 7383 | _sapp_win32_app_event(SAPP_EVENTTYPE_RESTORED); |
| 7384 | } |
| 7385 | } |
| 7386 | } |
| 7387 | break; |
| 7388 | case WM_SETFOCUS: |
| 7389 | _sapp_win32_app_event(SAPP_EVENTTYPE_FOCUSED); |
| 7390 | break; |
| 7391 | case WM_KILLFOCUS: |
| 7392 | /* if focus is lost for any reason, and we're in mouse locked mode, disable mouse lock */ |
| 7393 | if (_sapp.mouse.locked) { |
| 7394 | _sapp_win32_lock_mouse(false); |
| 7395 | } |
| 7396 | _sapp_win32_app_event(SAPP_EVENTTYPE_UNFOCUSED); |
| 7397 | break; |
| 7398 | case WM_SETCURSOR: |
| 7399 | if (LOWORD(lParam) == HTCLIENT) { |
| 7400 | _sapp_win32_update_cursor(_sapp.mouse.current_cursor, _sapp.mouse.shown, true); |
| 7401 | return TRUE; |
| 7402 | } |
| 7403 | break; |
| 7404 | case WM_DPICHANGED: |
| 7405 | { |
| 7406 | /* Update window's DPI and size if its moved to another monitor with a different DPI |
| 7407 | Only sent if DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 is used. |
| 7408 | */ |
| 7409 | _sapp_win32_dpi_changed(hWnd, (LPRECT)lParam); |
| 7410 | break; |
| 7411 | } |
| 7412 | case WM_LBUTTONDOWN: |
| 7413 | _sapp_win32_mouse_update(lParam); |
| 7414 | _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_LEFT); |
| 7415 | _sapp_win32_capture_mouse(1<<SAPP_MOUSEBUTTON_LEFT); |
| 7416 | break; |
| 7417 | case WM_RBUTTONDOWN: |
| 7418 | _sapp_win32_mouse_update(lParam); |
| 7419 | _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_RIGHT); |
| 7420 | _sapp_win32_capture_mouse(1<<SAPP_MOUSEBUTTON_RIGHT); |
| 7421 | break; |
| 7422 | case WM_MBUTTONDOWN: |
| 7423 | _sapp_win32_mouse_update(lParam); |
| 7424 | _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_MIDDLE); |
| 7425 | _sapp_win32_capture_mouse(1<<SAPP_MOUSEBUTTON_MIDDLE); |
| 7426 | break; |
| 7427 | case WM_LBUTTONUP: |
| 7428 | _sapp_win32_mouse_update(lParam); |
| 7429 | _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_LEFT); |
| 7430 | _sapp_win32_release_mouse(1<<SAPP_MOUSEBUTTON_LEFT); |
| 7431 | break; |
| 7432 | case WM_RBUTTONUP: |
| 7433 | _sapp_win32_mouse_update(lParam); |
| 7434 | _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_RIGHT); |
| 7435 | _sapp_win32_release_mouse(1<<SAPP_MOUSEBUTTON_RIGHT); |
| 7436 | break; |
| 7437 | case WM_MBUTTONUP: |
| 7438 | _sapp_win32_mouse_update(lParam); |
| 7439 | _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_MIDDLE); |
| 7440 | _sapp_win32_release_mouse(1<<SAPP_MOUSEBUTTON_MIDDLE); |
| 7441 | break; |
| 7442 | case WM_MOUSEMOVE: |
| 7443 | if (!_sapp.mouse.locked) { |
| 7444 | _sapp_win32_mouse_update(lParam); |
| 7445 | if (!_sapp.win32.mouse_tracked) { |
| 7446 | _sapp.win32.mouse_tracked = true; |
| 7447 | TRACKMOUSEEVENT tme; |
| 7448 | _sapp_clear(&tme, sizeof(tme)); |
| 7449 | tme.cbSize = sizeof(tme); |
| 7450 | tme.dwFlags = TME_LEAVE; |
| 7451 | tme.hwndTrack = _sapp.win32.hwnd; |
| 7452 | TrackMouseEvent(&tme); |
| 7453 | _sapp.mouse.dx = 0.0f; |
| 7454 | _sapp.mouse.dy = 0.0f; |
| 7455 | _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_ENTER, SAPP_MOUSEBUTTON_INVALID); |
| 7456 | } |
| 7457 | _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID); |
| 7458 | } |
| 7459 | break; |
| 7460 | case WM_INPUT: |
| 7461 | /* raw mouse input during mouse-lock */ |
| 7462 | if (_sapp.mouse.locked) { |
| 7463 | HRAWINPUT ri = (HRAWINPUT) lParam; |
| 7464 | UINT size = sizeof(_sapp.win32.raw_input_data); |
| 7465 | // see: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getrawinputdata |
| 7466 | if ((UINT)-1 == GetRawInputData(ri, RID_INPUT, &_sapp.win32.raw_input_data, &size, sizeof(RAWINPUTHEADER))) { |
| 7467 | _SAPP_ERROR(WIN32_GET_RAW_INPUT_DATA_FAILED); |
| 7468 | break; |
| 7469 | } |
| 7470 | const RAWINPUT* raw_mouse_data = (const RAWINPUT*) &_sapp.win32.raw_input_data; |
| 7471 | if (raw_mouse_data->data.mouse.usFlags & MOUSE_MOVE_ABSOLUTE) { |
| 7472 | /* mouse only reports absolute position |
| 7473 | NOTE: This code is untested and will most likely behave wrong in Remote Desktop sessions. |
| 7474 | (such remote desktop sessions are setting the MOUSE_MOVE_ABSOLUTE flag). |
| 7475 | See: https://github.com/floooh/sokol/issues/806 and |
| 7476 | https://github.com/microsoft/DirectXTK/commit/ef56b63f3739381e451f7a5a5bd2c9779d2a7555) |
| 7477 | */ |
| 7478 | LONG new_x = raw_mouse_data->data.mouse.lLastX; |
| 7479 | LONG new_y = raw_mouse_data->data.mouse.lLastY; |
| 7480 | if (_sapp.win32.raw_input_mousepos_valid) { |
| 7481 | _sapp.mouse.dx = (float) (new_x - _sapp.win32.raw_input_mousepos_x); |
| 7482 | _sapp.mouse.dy = (float) (new_y - _sapp.win32.raw_input_mousepos_y); |
| 7483 | } |
| 7484 | _sapp.win32.raw_input_mousepos_x = new_x; |
| 7485 | _sapp.win32.raw_input_mousepos_y = new_y; |
| 7486 | _sapp.win32.raw_input_mousepos_valid = true; |
| 7487 | } |
| 7488 | else { |
| 7489 | /* mouse reports movement delta (this seems to be the common case) */ |
| 7490 | _sapp.mouse.dx = (float) raw_mouse_data->data.mouse.lLastX; |
| 7491 | _sapp.mouse.dy = (float) raw_mouse_data->data.mouse.lLastY; |
| 7492 | } |
| 7493 | _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID); |
| 7494 | } |
| 7495 | break; |
| 7496 | |
| 7497 | case WM_MOUSELEAVE: |
| 7498 | if (!_sapp.mouse.locked) { |
| 7499 | _sapp.mouse.dx = 0.0f; |
| 7500 | _sapp.mouse.dy = 0.0f; |
| 7501 | _sapp.win32.mouse_tracked = false; |
| 7502 | _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_LEAVE, SAPP_MOUSEBUTTON_INVALID); |
| 7503 | } |
| 7504 | break; |
| 7505 | case WM_MOUSEWHEEL: |
| 7506 | _sapp_win32_scroll_event(0.0f, (float)((SHORT)HIWORD(wParam))); |
| 7507 | break; |
| 7508 | case WM_MOUSEHWHEEL: |
| 7509 | _sapp_win32_scroll_event((float)((SHORT)HIWORD(wParam)), 0.0f); |
| 7510 | break; |
| 7511 | case WM_CHAR: |
| 7512 | _sapp_win32_char_event((uint32_t)wParam, !!(lParam&0x40000000)); |
| 7513 | break; |
| 7514 | case WM_KEYDOWN: |
| 7515 | case WM_SYSKEYDOWN: |
| 7516 | _sapp_win32_key_event(SAPP_EVENTTYPE_KEY_DOWN, (int)(HIWORD(lParam)&0x1FF), !!(lParam&0x40000000)); |
| 7517 | break; |
| 7518 | case WM_KEYUP: |
| 7519 | case WM_SYSKEYUP: |
| 7520 | _sapp_win32_key_event(SAPP_EVENTTYPE_KEY_UP, (int)(HIWORD(lParam)&0x1FF), false); |
| 7521 | break; |
| 7522 | case WM_ENTERSIZEMOVE: |
| 7523 | SetTimer(_sapp.win32.hwnd, 1, USER_TIMER_MINIMUM, NULL); |
| 7524 | break; |
| 7525 | case WM_EXITSIZEMOVE: |
| 7526 | KillTimer(_sapp.win32.hwnd, 1); |
| 7527 | break; |
| 7528 | case WM_TIMER: |
| 7529 | _sapp_win32_timing_measure(); |
| 7530 | _sapp_frame(); |
| 7531 | #if defined(SOKOL_D3D11) |
| 7532 | // present with DXGI_PRESENT_DO_NOT_WAIT |
| 7533 | _sapp_d3d11_present(true); |
| 7534 | #endif |
| 7535 | #if defined(SOKOL_GLCORE) |
| 7536 | _sapp_wgl_swap_buffers(); |
| 7537 | #endif |
| 7538 | /* NOTE: resizing the swap-chain during resize leads to a substantial |
| 7539 | memory spike (hundreds of megabytes for a few seconds). |
| 7540 | |
| 7541 | if (_sapp_win32_update_dimensions()) { |
| 7542 | #if defined(SOKOL_D3D11) |
| 7543 | _sapp_d3d11_resize_default_render_target(); |
| 7544 | #endif |
| 7545 | _sapp_win32_app_event(SAPP_EVENTTYPE_RESIZED); |
| 7546 | } |
| 7547 | */ |
| 7548 | break; |
| 7549 | case WM_NCLBUTTONDOWN: |
| 7550 | /* workaround for half-second pause when starting to move window |
| 7551 | see: https://gamedev.net/forums/topic/672094-keeping-things-moving-during-win32-moveresize-events/5254386/ |
| 7552 | */ |
| 7553 | if (SendMessage(_sapp.win32.hwnd, WM_NCHITTEST, wParam, lParam) == HTCAPTION) { |
| 7554 | POINT point; |
| 7555 | GetCursorPos(&point); |
| 7556 | ScreenToClient(_sapp.win32.hwnd, &point); |
| 7557 | PostMessage(_sapp.win32.hwnd, WM_MOUSEMOVE, 0, ((uint32_t)point.x)|(((uint32_t)point.y) << 16)); |
| 7558 | } |
| 7559 | break; |
| 7560 | case WM_DROPFILES: |
| 7561 | _sapp_win32_files_dropped((HDROP)wParam); |
| 7562 | break; |
| 7563 | case WM_DISPLAYCHANGE: |
| 7564 | // refresh rate might have changed |
| 7565 | _sapp_timing_reset(&_sapp.timing); |
| 7566 | break; |
| 7567 | |
| 7568 | default: |
| 7569 | break; |
| 7570 | } |
| 7571 | } |
| 7572 | return DefWindowProcW(hWnd, uMsg, wParam, lParam); |
| 7573 | } |
| 7574 | |
| 7575 | _SOKOL_PRIVATE void _sapp_win32_create_window(void) { |
| 7576 | WNDCLASSW wndclassw; |
| 7577 | _sapp_clear(&wndclassw, sizeof(wndclassw)); |
| 7578 | wndclassw.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; |
| 7579 | wndclassw.lpfnWndProc = (WNDPROC) _sapp_win32_wndproc; |
| 7580 | wndclassw.hInstance = GetModuleHandleW(NULL); |
| 7581 | wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 7582 | wndclassw.hIcon = LoadIcon(NULL, IDI_WINLOGO); |
| 7583 | wndclassw.lpszClassName = L"SOKOLAPP"; |
| 7584 | RegisterClassW(&wndclassw); |
| 7585 | |
| 7586 | /* NOTE: regardless whether fullscreen is requested or not, a regular |
| 7587 | windowed-mode window will always be created first (however in hidden |
| 7588 | mode, so that no windowed-mode window pops up before the fullscreen window) |
| 7589 | */ |
| 7590 | const DWORD win_ex_style = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; |
| 7591 | RECT rect = { 0, 0, 0, 0 }; |
| 7592 | DWORD win_style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX; |
| 7593 | rect.right = (int) ((float)_sapp.window_width * _sapp.win32.dpi.window_scale); |
| 7594 | rect.bottom = (int) ((float)_sapp.window_height * _sapp.win32.dpi.window_scale); |
| 7595 | const bool use_default_width = 0 == _sapp.window_width; |
| 7596 | const bool use_default_height = 0 == _sapp.window_height; |
| 7597 | AdjustWindowRectEx(&rect, win_style, FALSE, win_ex_style); |
| 7598 | const int win_width = rect.right - rect.left; |
| 7599 | const int win_height = rect.bottom - rect.top; |
| 7600 | _sapp.win32.in_create_window = true; |
| 7601 | _sapp.win32.hwnd = CreateWindowExW( |
| 7602 | win_ex_style, // dwExStyle |
| 7603 | L"SOKOLAPP", // lpClassName |
| 7604 | _sapp.window_title_wide, // lpWindowName |
| 7605 | win_style, // dwStyle |
| 7606 | CW_USEDEFAULT, // X |
| 7607 | SW_HIDE, // Y (NOTE: CW_USEDEFAULT is not used for position here, but internally calls ShowWindow! |
| 7608 | use_default_width ? CW_USEDEFAULT : win_width, // nWidth |
| 7609 | use_default_height ? CW_USEDEFAULT : win_height, // nHeight (NOTE: if width is CW_USEDEFAULT, height is actually ignored) |
| 7610 | NULL, // hWndParent |
| 7611 | NULL, // hMenu |
| 7612 | GetModuleHandle(NULL), // hInstance |
| 7613 | NULL); // lParam |
| 7614 | _sapp.win32.in_create_window = false; |
| 7615 | _sapp.win32.dc = GetDC(_sapp.win32.hwnd); |
| 7616 | _sapp.win32.hmonitor = MonitorFromWindow(_sapp.win32.hwnd, MONITOR_DEFAULTTONULL); |
| 7617 | SOKOL_ASSERT(_sapp.win32.dc); |
| 7618 | |
| 7619 | /* this will get the actual windowed-mode window size, if fullscreen |
| 7620 | is requested, the set_fullscreen function will then capture the |
| 7621 | current window rectangle, which then might be used later to |
| 7622 | restore the window position when switching back to windowed |
| 7623 | */ |
| 7624 | _sapp_win32_update_dimensions(); |
| 7625 | if (_sapp.fullscreen) { |
| 7626 | _sapp_win32_set_fullscreen(_sapp.fullscreen, SWP_HIDEWINDOW); |
| 7627 | _sapp_win32_update_dimensions(); |
| 7628 | } |
| 7629 | ShowWindow(_sapp.win32.hwnd, SW_SHOW); |
| 7630 | DragAcceptFiles(_sapp.win32.hwnd, 1); |
| 7631 | } |
| 7632 | |
| 7633 | _SOKOL_PRIVATE void _sapp_win32_destroy_window(void) { |
| 7634 | DestroyWindow(_sapp.win32.hwnd); _sapp.win32.hwnd = 0; |
| 7635 | UnregisterClassW(L"SOKOLAPP", GetModuleHandleW(NULL)); |
| 7636 | } |
| 7637 | |
| 7638 | _SOKOL_PRIVATE void _sapp_win32_destroy_icons(void) { |
| 7639 | if (_sapp.win32.big_icon) { |
| 7640 | DestroyIcon(_sapp.win32.big_icon); |
| 7641 | _sapp.win32.big_icon = 0; |
| 7642 | } |
| 7643 | if (_sapp.win32.small_icon) { |
| 7644 | DestroyIcon(_sapp.win32.small_icon); |
| 7645 | _sapp.win32.small_icon = 0; |
| 7646 | } |
| 7647 | } |
| 7648 | |
| 7649 | _SOKOL_PRIVATE void _sapp_win32_init_console(void) { |
| 7650 | if (_sapp.desc.win32_console_create || _sapp.desc.win32_console_attach) { |
| 7651 | BOOL con_valid = FALSE; |
| 7652 | if (_sapp.desc.win32_console_create) { |
| 7653 | con_valid = AllocConsole(); |
| 7654 | } |
| 7655 | else if (_sapp.desc.win32_console_attach) { |
| 7656 | con_valid = AttachConsole(ATTACH_PARENT_PROCESS); |
| 7657 | } |
| 7658 | if (con_valid) { |
| 7659 | FILE* res_fp = 0; |
| 7660 | errno_t err; |
| 7661 | err = freopen_s(&res_fp, "CON", "w", stdout); |
| 7662 | (void)err; |
| 7663 | err = freopen_s(&res_fp, "CON", "w", stderr); |
| 7664 | (void)err; |
| 7665 | } |
| 7666 | } |
| 7667 | if (_sapp.desc.win32_console_utf8) { |
| 7668 | _sapp.win32.orig_codepage = GetConsoleOutputCP(); |
| 7669 | SetConsoleOutputCP(CP_UTF8); |
| 7670 | } |
| 7671 | } |
| 7672 | |
| 7673 | _SOKOL_PRIVATE void _sapp_win32_restore_console(void) { |
| 7674 | if (_sapp.desc.win32_console_utf8) { |
| 7675 | SetConsoleOutputCP(_sapp.win32.orig_codepage); |
| 7676 | } |
| 7677 | } |
| 7678 | |
| 7679 | _SOKOL_PRIVATE void _sapp_win32_init_dpi(void) { |
| 7680 | |
| 7681 | DECLARE_HANDLE(DPI_AWARENESS_CONTEXT_T); |
| 7682 | typedef BOOL(WINAPI * SETPROCESSDPIAWARE_T)(void); |
| 7683 | typedef bool (WINAPI * SETPROCESSDPIAWARENESSCONTEXT_T)(DPI_AWARENESS_CONTEXT_T); // since Windows 10, version 1703 |
| 7684 | typedef HRESULT(WINAPI * SETPROCESSDPIAWARENESS_T)(PROCESS_DPI_AWARENESS); |
| 7685 | typedef HRESULT(WINAPI * GETDPIFORMONITOR_T)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*); |
| 7686 | |
| 7687 | SETPROCESSDPIAWARE_T fn_setprocessdpiaware = 0; |
| 7688 | SETPROCESSDPIAWARENESS_T fn_setprocessdpiawareness = 0; |
| 7689 | GETDPIFORMONITOR_T fn_getdpiformonitor = 0; |
| 7690 | SETPROCESSDPIAWARENESSCONTEXT_T fn_setprocessdpiawarenesscontext =0; |
| 7691 | |
| 7692 | HINSTANCE user32 = LoadLibraryA("user32.dll"); |
| 7693 | if (user32) { |
| 7694 | fn_setprocessdpiaware = (SETPROCESSDPIAWARE_T)(void*) GetProcAddress(user32, "SetProcessDPIAware"); |
| 7695 | fn_setprocessdpiawarenesscontext = (SETPROCESSDPIAWARENESSCONTEXT_T)(void*) GetProcAddress(user32, "SetProcessDpiAwarenessContext"); |
| 7696 | } |
| 7697 | HINSTANCE shcore = LoadLibraryA("shcore.dll"); |
| 7698 | if (shcore) { |
| 7699 | fn_setprocessdpiawareness = (SETPROCESSDPIAWARENESS_T)(void*) GetProcAddress(shcore, "SetProcessDpiAwareness"); |
| 7700 | fn_getdpiformonitor = (GETDPIFORMONITOR_T)(void*) GetProcAddress(shcore, "GetDpiForMonitor"); |
| 7701 | } |
| 7702 | /* |
| 7703 | NOTE on SetProcessDpiAware() vs SetProcessDpiAwareness() vs SetProcessDpiAwarenessContext(): |
| 7704 | |
| 7705 | These are different attempts to get DPI handling on Windows right, from oldest |
| 7706 | to newest. SetProcessDpiAwarenessContext() is required for the new |
| 7707 | DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 method. |
| 7708 | */ |
| 7709 | if (fn_setprocessdpiawareness) { |
| 7710 | if (_sapp.desc.high_dpi) { |
| 7711 | /* app requests HighDPI rendering, first try the Win10 Creator Update per-monitor-dpi awareness, |
| 7712 | if that fails, fall back to system-dpi-awareness |
| 7713 | */ |
| 7714 | _sapp.win32.dpi.aware = true; |
| 7715 | DPI_AWARENESS_CONTEXT_T per_monitor_aware_v2 = (DPI_AWARENESS_CONTEXT_T)-4; |
| 7716 | if (!(fn_setprocessdpiawarenesscontext && fn_setprocessdpiawarenesscontext(per_monitor_aware_v2))) { |
| 7717 | // fallback to system-dpi-aware |
| 7718 | fn_setprocessdpiawareness(PROCESS_SYSTEM_DPI_AWARE); |
| 7719 | } |
| 7720 | } |
| 7721 | else { |
| 7722 | /* if the app didn't request HighDPI rendering, let Windows do the upscaling */ |
| 7723 | _sapp.win32.dpi.aware = false; |
| 7724 | fn_setprocessdpiawareness(PROCESS_DPI_UNAWARE); |
| 7725 | } |
| 7726 | } |
| 7727 | else if (fn_setprocessdpiaware) { |
| 7728 | // fallback for Windows 7 |
| 7729 | _sapp.win32.dpi.aware = true; |
| 7730 | fn_setprocessdpiaware(); |
| 7731 | } |
| 7732 | /* get dpi scale factor for main monitor */ |
| 7733 | if (fn_getdpiformonitor && _sapp.win32.dpi.aware) { |
| 7734 | POINT pt = { 1, 1 }; |
| 7735 | HMONITOR hm = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST); |
| 7736 | UINT dpix, dpiy; |
| 7737 | HRESULT hr = fn_getdpiformonitor(hm, MDT_EFFECTIVE_DPI, &dpix, &dpiy); |
| 7738 | _SOKOL_UNUSED(hr); |
| 7739 | SOKOL_ASSERT(SUCCEEDED(hr)); |
| 7740 | /* clamp window scale to an integer factor */ |
| 7741 | _sapp.win32.dpi.window_scale = (float)dpix / 96.0f; |
| 7742 | } |
| 7743 | else { |
| 7744 | _sapp.win32.dpi.window_scale = 1.0f; |
| 7745 | } |
| 7746 | if (_sapp.desc.high_dpi) { |
| 7747 | _sapp.win32.dpi.content_scale = _sapp.win32.dpi.window_scale; |
| 7748 | _sapp.win32.dpi.mouse_scale = 1.0f; |
| 7749 | } |
| 7750 | else { |
| 7751 | _sapp.win32.dpi.content_scale = 1.0f; |
| 7752 | _sapp.win32.dpi.mouse_scale = 1.0f / _sapp.win32.dpi.window_scale; |
| 7753 | } |
| 7754 | _sapp.dpi_scale = _sapp.win32.dpi.content_scale; |
| 7755 | if (user32) { |
| 7756 | FreeLibrary(user32); |
| 7757 | } |
| 7758 | if (shcore) { |
| 7759 | FreeLibrary(shcore); |
| 7760 | } |
| 7761 | } |
| 7762 | |
| 7763 | _SOKOL_PRIVATE bool _sapp_win32_set_clipboard_string(const char* str) { |
| 7764 | SOKOL_ASSERT(str); |
| 7765 | SOKOL_ASSERT(_sapp.win32.hwnd); |
| 7766 | SOKOL_ASSERT(_sapp.clipboard.enabled && (_sapp.clipboard.buf_size > 0)); |
| 7767 | |
| 7768 | if (!OpenClipboard(_sapp.win32.hwnd)) { |
| 7769 | return false; |
| 7770 | } |
| 7771 | |
| 7772 | HANDLE object = 0; |
| 7773 | wchar_t* wchar_buf = 0; |
| 7774 | |
| 7775 | const SIZE_T wchar_buf_size = (SIZE_T)_sapp.clipboard.buf_size * sizeof(wchar_t); |
| 7776 | object = GlobalAlloc(GMEM_MOVEABLE, wchar_buf_size); |
| 7777 | if (NULL == object) { |
| 7778 | goto error; |
| 7779 | } |
| 7780 | wchar_buf = (wchar_t*) GlobalLock(object); |
| 7781 | if (NULL == wchar_buf) { |
| 7782 | goto error; |
| 7783 | } |
| 7784 | if (!_sapp_win32_utf8_to_wide(str, wchar_buf, (int)wchar_buf_size)) { |
| 7785 | goto error; |
| 7786 | } |
| 7787 | GlobalUnlock(object); |
| 7788 | wchar_buf = 0; |
| 7789 | EmptyClipboard(); |
| 7790 | // NOTE: when successful, SetClipboardData() takes ownership of memory object! |
| 7791 | if (NULL == SetClipboardData(CF_UNICODETEXT, object)) { |
| 7792 | goto error; |
| 7793 | } |
| 7794 | CloseClipboard(); |
| 7795 | return true; |
| 7796 | |
| 7797 | error: |
| 7798 | if (wchar_buf) { |
| 7799 | GlobalUnlock(object); |
| 7800 | } |
| 7801 | if (object) { |
| 7802 | GlobalFree(object); |
| 7803 | } |
| 7804 | CloseClipboard(); |
| 7805 | return false; |
| 7806 | } |
| 7807 | |
| 7808 | _SOKOL_PRIVATE const char* _sapp_win32_get_clipboard_string(void) { |
| 7809 | SOKOL_ASSERT(_sapp.clipboard.enabled && _sapp.clipboard.buffer); |
| 7810 | SOKOL_ASSERT(_sapp.win32.hwnd); |
| 7811 | if (!OpenClipboard(_sapp.win32.hwnd)) { |
| 7812 | /* silently ignore any errors and just return the current |
| 7813 | content of the local clipboard buffer |
| 7814 | */ |
| 7815 | return _sapp.clipboard.buffer; |
| 7816 | } |
| 7817 | HANDLE object = GetClipboardData(CF_UNICODETEXT); |
| 7818 | if (!object) { |
| 7819 | CloseClipboard(); |
| 7820 | return _sapp.clipboard.buffer; |
| 7821 | } |
| 7822 | const wchar_t* wchar_buf = (const wchar_t*) GlobalLock(object); |
| 7823 | if (!wchar_buf) { |
| 7824 | CloseClipboard(); |
| 7825 | return _sapp.clipboard.buffer; |
| 7826 | } |
| 7827 | if (!_sapp_win32_wide_to_utf8(wchar_buf, _sapp.clipboard.buffer, _sapp.clipboard.buf_size)) { |
| 7828 | _SAPP_ERROR(CLIPBOARD_STRING_TOO_BIG); |
| 7829 | } |
| 7830 | GlobalUnlock(object); |
| 7831 | CloseClipboard(); |
| 7832 | return _sapp.clipboard.buffer; |
| 7833 | } |
| 7834 | |
| 7835 | _SOKOL_PRIVATE void _sapp_win32_update_window_title(void) { |
| 7836 | _sapp_win32_utf8_to_wide(_sapp.window_title, _sapp.window_title_wide, sizeof(_sapp.window_title_wide)); |
| 7837 | SetWindowTextW(_sapp.win32.hwnd, _sapp.window_title_wide); |
| 7838 | } |
| 7839 | |
| 7840 | _SOKOL_PRIVATE HICON _sapp_win32_create_icon_from_image(const sapp_image_desc* desc) { |
| 7841 | BITMAPV5HEADER bi; |
| 7842 | _sapp_clear(&bi, sizeof(bi)); |
| 7843 | bi.bV5Size = sizeof(bi); |
| 7844 | bi.bV5Width = desc->width; |
| 7845 | bi.bV5Height = -desc->height; // NOTE the '-' here to indicate that origin is top-left |
| 7846 | bi.bV5Planes = 1; |
| 7847 | bi.bV5BitCount = 32; |
| 7848 | bi.bV5Compression = BI_BITFIELDS; |
| 7849 | bi.bV5RedMask = 0x00FF0000; |
| 7850 | bi.bV5GreenMask = 0x0000FF00; |
| 7851 | bi.bV5BlueMask = 0x000000FF; |
| 7852 | bi.bV5AlphaMask = 0xFF000000; |
| 7853 | |
| 7854 | uint8_t* target = 0; |
| 7855 | const uint8_t* source = (const uint8_t*)desc->pixels.ptr; |
| 7856 | |
| 7857 | HDC dc = GetDC(NULL); |
| 7858 | HBITMAP color = CreateDIBSection(dc, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&target, NULL, (DWORD)0); |
| 7859 | ReleaseDC(NULL, dc); |
| 7860 | if (0 == color) { |
| 7861 | return NULL; |
| 7862 | } |
| 7863 | SOKOL_ASSERT(target); |
| 7864 | |
| 7865 | HBITMAP mask = CreateBitmap(desc->width, desc->height, 1, 1, NULL); |
| 7866 | if (0 == mask) { |
| 7867 | DeleteObject(color); |
| 7868 | return NULL; |
| 7869 | } |
| 7870 | |
| 7871 | for (int i = 0; i < (desc->width*desc->height); i++) { |
| 7872 | target[0] = source[2]; |
| 7873 | target[1] = source[1]; |
| 7874 | target[2] = source[0]; |
| 7875 | target[3] = source[3]; |
| 7876 | target += 4; |
| 7877 | source += 4; |
| 7878 | } |
| 7879 | |
| 7880 | ICONINFO icon_info; |
| 7881 | _sapp_clear(&icon_info, sizeof(icon_info)); |
| 7882 | icon_info.fIcon = true; |
| 7883 | icon_info.xHotspot = 0; |
| 7884 | icon_info.yHotspot = 0; |
| 7885 | icon_info.hbmMask = mask; |
| 7886 | icon_info.hbmColor = color; |
| 7887 | HICON icon_handle = CreateIconIndirect(&icon_info); |
| 7888 | DeleteObject(color); |
| 7889 | DeleteObject(mask); |
| 7890 | |
| 7891 | return icon_handle; |
| 7892 | } |
| 7893 | |
| 7894 | _SOKOL_PRIVATE void _sapp_win32_set_icon(const sapp_icon_desc* icon_desc, int num_images) { |
| 7895 | SOKOL_ASSERT((num_images > 0) && (num_images <= SAPP_MAX_ICONIMAGES)); |
| 7896 | |
| 7897 | int big_img_index = _sapp_image_bestmatch(icon_desc->images, num_images, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)); |
| 7898 | int sml_img_index = _sapp_image_bestmatch(icon_desc->images, num_images, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)); |
| 7899 | HICON big_icon = _sapp_win32_create_icon_from_image(&icon_desc->images[big_img_index]); |
| 7900 | HICON sml_icon = _sapp_win32_create_icon_from_image(&icon_desc->images[sml_img_index]); |
| 7901 | |
| 7902 | // if icon creation or lookup has failed for some reason, leave the currently set icon untouched |
| 7903 | if (0 != big_icon) { |
| 7904 | SendMessage(_sapp.win32.hwnd, WM_SETICON, ICON_BIG, (LPARAM) big_icon); |
| 7905 | if (0 != _sapp.win32.big_icon) { |
| 7906 | DestroyIcon(_sapp.win32.big_icon); |
| 7907 | } |
| 7908 | _sapp.win32.big_icon = big_icon; |
| 7909 | } |
| 7910 | if (0 != sml_icon) { |
| 7911 | SendMessage(_sapp.win32.hwnd, WM_SETICON, ICON_SMALL, (LPARAM) sml_icon); |
| 7912 | if (0 != _sapp.win32.small_icon) { |
| 7913 | DestroyIcon(_sapp.win32.small_icon); |
| 7914 | } |
| 7915 | _sapp.win32.small_icon = sml_icon; |
| 7916 | } |
| 7917 | } |
| 7918 | |
| 7919 | /* don't laugh, but this seems to be the easiest and most robust |
| 7920 | way to check if we're running on Win10 |
| 7921 | |
| 7922 | From: https://github.com/videolan/vlc/blob/232fb13b0d6110c4d1b683cde24cf9a7f2c5c2ea/modules/video_output/win32/d3d11_swapchain.c#L263 |
| 7923 | */ |
| 7924 | _SOKOL_PRIVATE bool _sapp_win32_is_win10_or_greater(void) { |
| 7925 | HMODULE h = GetModuleHandleW(L"kernel32.dll"); |
| 7926 | if (NULL != h) { |
| 7927 | return (NULL != GetProcAddress(h, "GetSystemCpuSetInformation")); |
| 7928 | } |
| 7929 | else { |
| 7930 | return false; |
| 7931 | } |
| 7932 | } |
| 7933 | |
| 7934 | _SOKOL_PRIVATE void _sapp_win32_run(const sapp_desc* desc) { |
| 7935 | _sapp_init_state(desc); |
| 7936 | _sapp_win32_init_console(); |
| 7937 | _sapp.win32.is_win10_or_greater = _sapp_win32_is_win10_or_greater(); |
| 7938 | _sapp_win32_init_keytable(); |
| 7939 | _sapp_win32_utf8_to_wide(_sapp.window_title, _sapp.window_title_wide, sizeof(_sapp.window_title_wide)); |
| 7940 | _sapp_win32_init_dpi(); |
| 7941 | _sapp_win32_init_cursors(); |
| 7942 | _sapp_win32_create_window(); |
| 7943 | sapp_set_icon(&desc->icon); |
| 7944 | #if defined(SOKOL_D3D11) |
| 7945 | _sapp_d3d11_create_device_and_swapchain(); |
| 7946 | _sapp_d3d11_create_default_render_target(); |
| 7947 | #endif |
| 7948 | #if defined(SOKOL_GLCORE) |
| 7949 | _sapp_wgl_init(); |
| 7950 | _sapp_wgl_load_extensions(); |
| 7951 | _sapp_wgl_create_context(); |
| 7952 | #endif |
| 7953 | _sapp.valid = true; |
| 7954 | |
| 7955 | bool done = false; |
| 7956 | while (!(done || _sapp.quit_ordered)) { |
| 7957 | _sapp_win32_timing_measure(); |
| 7958 | MSG msg; |
| 7959 | while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) { |
| 7960 | if (WM_QUIT == msg.message) { |
| 7961 | done = true; |
| 7962 | continue; |
| 7963 | } |
| 7964 | else { |
| 7965 | TranslateMessage(&msg); |
| 7966 | DispatchMessageW(&msg); |
| 7967 | } |
| 7968 | } |
| 7969 | _sapp_frame(); |
| 7970 | #if defined(SOKOL_D3D11) |
| 7971 | _sapp_d3d11_present(false); |
| 7972 | if (IsIconic(_sapp.win32.hwnd)) { |
| 7973 | Sleep((DWORD)(16 * _sapp.swap_interval)); |
| 7974 | } |
| 7975 | #endif |
| 7976 | #if defined(SOKOL_GLCORE) |
| 7977 | _sapp_wgl_swap_buffers(); |
| 7978 | #endif |
| 7979 | /* check for window resized, this cannot happen in WM_SIZE as it explodes memory usage */ |
| 7980 | if (_sapp_win32_update_dimensions()) { |
| 7981 | #if defined(SOKOL_D3D11) |
| 7982 | _sapp_d3d11_resize_default_render_target(); |
| 7983 | #endif |
| 7984 | _sapp_win32_app_event(SAPP_EVENTTYPE_RESIZED); |
| 7985 | } |
| 7986 | /* check if the window monitor has changed, need to reset timing because |
| 7987 | the new monitor might have a different refresh rate |
| 7988 | */ |
| 7989 | if (_sapp_win32_update_monitor()) { |
| 7990 | _sapp_timing_reset(&_sapp.timing); |
| 7991 | } |
| 7992 | if (_sapp.quit_requested) { |
| 7993 | PostMessage(_sapp.win32.hwnd, WM_CLOSE, 0, 0); |
| 7994 | } |
| 7995 | } |
| 7996 | _sapp_call_cleanup(); |
| 7997 | |
| 7998 | #if defined(SOKOL_D3D11) |
| 7999 | _sapp_d3d11_destroy_default_render_target(); |
| 8000 | _sapp_d3d11_destroy_device_and_swapchain(); |
| 8001 | #else |
| 8002 | _sapp_wgl_destroy_context(); |
| 8003 | _sapp_wgl_shutdown(); |
| 8004 | #endif |
| 8005 | _sapp_win32_destroy_window(); |
| 8006 | _sapp_win32_destroy_icons(); |
| 8007 | _sapp_win32_restore_console(); |
| 8008 | _sapp_discard_state(); |
| 8009 | } |
| 8010 | |
| 8011 | _SOKOL_PRIVATE char** _sapp_win32_command_line_to_utf8_argv(LPWSTR w_command_line, int* o_argc) { |
| 8012 | int argc = 0; |
| 8013 | char** argv = 0; |
| 8014 | char* args; |
| 8015 | |
| 8016 | LPWSTR* w_argv = CommandLineToArgvW(w_command_line, &argc); |
| 8017 | if (w_argv == NULL) { |
| 8018 | // FIXME: chicken egg problem, can't report errors before sokol_main() is called! |
| 8019 | } else { |
| 8020 | size_t size = wcslen(w_command_line) * 4; |
| 8021 | argv = (char**) _sapp_malloc_clear(((size_t)argc + 1) * sizeof(char*) + size); |
| 8022 | SOKOL_ASSERT(argv); |
| 8023 | args = (char*) &argv[argc + 1]; |
| 8024 | int n; |
| 8025 | for (int i = 0; i < argc; ++i) { |
| 8026 | n = WideCharToMultiByte(CP_UTF8, 0, w_argv[i], -1, args, (int)size, NULL, NULL); |
| 8027 | if (n == 0) { |
| 8028 | // FIXME: chicken egg problem, can't report errors before sokol_main() is called! |
| 8029 | break; |
| 8030 | } |
| 8031 | argv[i] = args; |
| 8032 | size -= (size_t)n; |
| 8033 | args += n; |
| 8034 | } |
| 8035 | LocalFree(w_argv); |
| 8036 | } |
| 8037 | *o_argc = argc; |
| 8038 | return argv; |
| 8039 | } |
| 8040 | |
| 8041 | #if !defined(SOKOL_NO_ENTRY) |
| 8042 | #if defined(SOKOL_WIN32_FORCE_MAIN) |
| 8043 | int main(int argc, char* argv[]) { |
| 8044 | sapp_desc desc = sokol_main(argc, argv); |
| 8045 | _sapp_win32_run(&desc); |
| 8046 | return 0; |
| 8047 | } |
| 8048 | #else |
| 8049 | int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) { |
| 8050 | _SOKOL_UNUSED(hInstance); |
| 8051 | _SOKOL_UNUSED(hPrevInstance); |
| 8052 | _SOKOL_UNUSED(lpCmdLine); |
| 8053 | _SOKOL_UNUSED(nCmdShow); |
| 8054 | int argc_utf8 = 0; |
| 8055 | char** argv_utf8 = _sapp_win32_command_line_to_utf8_argv(GetCommandLineW(), &argc_utf8); |
| 8056 | sapp_desc desc = sokol_main(argc_utf8, argv_utf8); |
| 8057 | _sapp_win32_run(&desc); |
| 8058 | _sapp_free(argv_utf8); |
| 8059 | return 0; |
| 8060 | } |
| 8061 | #endif /* SOKOL_WIN32_FORCE_MAIN */ |
| 8062 | #endif /* SOKOL_NO_ENTRY */ |
| 8063 | |
| 8064 | #ifdef _MSC_VER |
| 8065 | #pragma warning(pop) |
| 8066 | #endif |
| 8067 | |
| 8068 | #endif /* _SAPP_WIN32 */ |
| 8069 | |
| 8070 | // █████ ███ ██ ██████ ██████ ██████ ██ ██████ |
| 8071 | // ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 8072 | // ███████ ██ ██ ██ ██ ██ ██████ ██ ██ ██ ██ ██ |
| 8073 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 8074 | // ██ ██ ██ ████ ██████ ██ ██ ██████ ██ ██████ |
| 8075 | // |
| 8076 | // >>android |
| 8077 | #if defined(_SAPP_ANDROID) |
| 8078 | |
| 8079 | /* android loop thread */ |
| 8080 | _SOKOL_PRIVATE bool _sapp_android_init_egl(void) { |
| 8081 | SOKOL_ASSERT(_sapp.android.display == EGL_NO_DISPLAY); |
| 8082 | SOKOL_ASSERT(_sapp.android.context == EGL_NO_CONTEXT); |
| 8083 | |
| 8084 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 8085 | if (display == EGL_NO_DISPLAY) { |
| 8086 | return false; |
| 8087 | } |
| 8088 | if (eglInitialize(display, NULL, NULL) == EGL_FALSE) { |
| 8089 | return false; |
| 8090 | } |
| 8091 | EGLint alpha_size = _sapp.desc.alpha ? 8 : 0; |
| 8092 | const EGLint cfg_attributes[] = { |
| 8093 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 8094 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, |
| 8095 | EGL_RED_SIZE, 8, |
| 8096 | EGL_GREEN_SIZE, 8, |
| 8097 | EGL_BLUE_SIZE, 8, |
| 8098 | EGL_ALPHA_SIZE, alpha_size, |
| 8099 | EGL_DEPTH_SIZE, 16, |
| 8100 | EGL_STENCIL_SIZE, 0, |
| 8101 | EGL_NONE, |
| 8102 | }; |
| 8103 | EGLConfig available_cfgs[32]; |
| 8104 | EGLint cfg_count; |
| 8105 | eglChooseConfig(display, cfg_attributes, available_cfgs, 32, &cfg_count); |
| 8106 | SOKOL_ASSERT(cfg_count > 0); |
| 8107 | SOKOL_ASSERT(cfg_count <= 32); |
| 8108 | |
| 8109 | /* find config with 8-bit rgb buffer if available, ndk sample does not trust egl spec */ |
| 8110 | EGLConfig config; |
| 8111 | bool exact_cfg_found = false; |
| 8112 | for (int i = 0; i < cfg_count; ++i) { |
| 8113 | EGLConfig c = available_cfgs[i]; |
| 8114 | EGLint r, g, b, a, d; |
| 8115 | if (eglGetConfigAttrib(display, c, EGL_RED_SIZE, &r) == EGL_TRUE && |
| 8116 | eglGetConfigAttrib(display, c, EGL_GREEN_SIZE, &g) == EGL_TRUE && |
| 8117 | eglGetConfigAttrib(display, c, EGL_BLUE_SIZE, &b) == EGL_TRUE && |
| 8118 | eglGetConfigAttrib(display, c, EGL_ALPHA_SIZE, &a) == EGL_TRUE && |
| 8119 | eglGetConfigAttrib(display, c, EGL_DEPTH_SIZE, &d) == EGL_TRUE && |
| 8120 | r == 8 && g == 8 && b == 8 && (alpha_size == 0 || a == alpha_size) && d == 16) { |
| 8121 | exact_cfg_found = true; |
| 8122 | config = c; |
| 8123 | break; |
| 8124 | } |
| 8125 | } |
| 8126 | if (!exact_cfg_found) { |
| 8127 | config = available_cfgs[0]; |
| 8128 | } |
| 8129 | |
| 8130 | EGLint ctx_attributes[] = { |
| 8131 | EGL_CONTEXT_CLIENT_VERSION, 3, |
| 8132 | EGL_NONE, |
| 8133 | }; |
| 8134 | EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, ctx_attributes); |
| 8135 | if (context == EGL_NO_CONTEXT) { |
| 8136 | return false; |
| 8137 | } |
| 8138 | |
| 8139 | _sapp.android.config = config; |
| 8140 | _sapp.android.display = display; |
| 8141 | _sapp.android.context = context; |
| 8142 | return true; |
| 8143 | } |
| 8144 | |
| 8145 | _SOKOL_PRIVATE void _sapp_android_cleanup_egl(void) { |
| 8146 | if (_sapp.android.display != EGL_NO_DISPLAY) { |
| 8147 | eglMakeCurrent(_sapp.android.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 8148 | if (_sapp.android.surface != EGL_NO_SURFACE) { |
| 8149 | eglDestroySurface(_sapp.android.display, _sapp.android.surface); |
| 8150 | _sapp.android.surface = EGL_NO_SURFACE; |
| 8151 | } |
| 8152 | if (_sapp.android.context != EGL_NO_CONTEXT) { |
| 8153 | eglDestroyContext(_sapp.android.display, _sapp.android.context); |
| 8154 | _sapp.android.context = EGL_NO_CONTEXT; |
| 8155 | } |
| 8156 | eglTerminate(_sapp.android.display); |
| 8157 | _sapp.android.display = EGL_NO_DISPLAY; |
| 8158 | } |
| 8159 | } |
| 8160 | |
| 8161 | _SOKOL_PRIVATE bool _sapp_android_init_egl_surface(ANativeWindow* window) { |
| 8162 | SOKOL_ASSERT(_sapp.android.display != EGL_NO_DISPLAY); |
| 8163 | SOKOL_ASSERT(_sapp.android.context != EGL_NO_CONTEXT); |
| 8164 | SOKOL_ASSERT(_sapp.android.surface == EGL_NO_SURFACE); |
| 8165 | SOKOL_ASSERT(window); |
| 8166 | |
| 8167 | /* TODO: set window flags */ |
| 8168 | /* ANativeActivity_setWindowFlags(activity, AWINDOW_FLAG_KEEP_SCREEN_ON, 0); */ |
| 8169 | |
| 8170 | /* create egl surface and make it current */ |
| 8171 | EGLSurface surface = eglCreateWindowSurface(_sapp.android.display, _sapp.android.config, window, NULL); |
| 8172 | if (surface == EGL_NO_SURFACE) { |
| 8173 | return false; |
| 8174 | } |
| 8175 | if (eglMakeCurrent(_sapp.android.display, surface, surface, _sapp.android.context) == EGL_FALSE) { |
| 8176 | return false; |
| 8177 | } |
| 8178 | _sapp.android.surface = surface; |
| 8179 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer); |
| 8180 | return true; |
| 8181 | } |
| 8182 | |
| 8183 | _SOKOL_PRIVATE void _sapp_android_cleanup_egl_surface(void) { |
| 8184 | if (_sapp.android.display == EGL_NO_DISPLAY) { |
| 8185 | return; |
| 8186 | } |
| 8187 | eglMakeCurrent(_sapp.android.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 8188 | if (_sapp.android.surface != EGL_NO_SURFACE) { |
| 8189 | eglDestroySurface(_sapp.android.display, _sapp.android.surface); |
| 8190 | _sapp.android.surface = EGL_NO_SURFACE; |
| 8191 | } |
| 8192 | } |
| 8193 | |
| 8194 | _SOKOL_PRIVATE void _sapp_android_app_event(sapp_event_type type) { |
| 8195 | if (_sapp_events_enabled()) { |
| 8196 | _sapp_init_event(type); |
| 8197 | _sapp_call_event(&_sapp.event); |
| 8198 | } |
| 8199 | } |
| 8200 | |
| 8201 | _SOKOL_PRIVATE void _sapp_android_update_dimensions(ANativeWindow* window, bool force_update) { |
| 8202 | SOKOL_ASSERT(_sapp.android.display != EGL_NO_DISPLAY); |
| 8203 | SOKOL_ASSERT(_sapp.android.context != EGL_NO_CONTEXT); |
| 8204 | SOKOL_ASSERT(_sapp.android.surface != EGL_NO_SURFACE); |
| 8205 | SOKOL_ASSERT(window); |
| 8206 | |
| 8207 | const int32_t win_w = ANativeWindow_getWidth(window); |
| 8208 | const int32_t win_h = ANativeWindow_getHeight(window); |
| 8209 | SOKOL_ASSERT(win_w >= 0 && win_h >= 0); |
| 8210 | const bool win_changed = (win_w != _sapp.window_width) || (win_h != _sapp.window_height); |
| 8211 | _sapp.window_width = win_w; |
| 8212 | _sapp.window_height = win_h; |
| 8213 | if (win_changed || force_update) { |
| 8214 | if (!_sapp.desc.high_dpi) { |
| 8215 | const int32_t buf_w = win_w / 2; |
| 8216 | const int32_t buf_h = win_h / 2; |
| 8217 | EGLint format; |
| 8218 | EGLBoolean egl_result = eglGetConfigAttrib(_sapp.android.display, _sapp.android.config, EGL_NATIVE_VISUAL_ID, &format); |
| 8219 | SOKOL_ASSERT(egl_result == EGL_TRUE); _SOKOL_UNUSED(egl_result); |
| 8220 | /* NOTE: calling ANativeWindow_setBuffersGeometry() with the same dimensions |
| 8221 | as the ANativeWindow size results in weird display artefacts, that's |
| 8222 | why it's only called when the buffer geometry is different from |
| 8223 | the window size |
| 8224 | */ |
| 8225 | int32_t result = ANativeWindow_setBuffersGeometry(window, buf_w, buf_h, format); |
| 8226 | SOKOL_ASSERT(result == 0); _SOKOL_UNUSED(result); |
| 8227 | } |
| 8228 | } |
| 8229 | |
| 8230 | /* query surface size */ |
| 8231 | EGLint fb_w, fb_h; |
| 8232 | EGLBoolean egl_result_w = eglQuerySurface(_sapp.android.display, _sapp.android.surface, EGL_WIDTH, &fb_w); |
| 8233 | EGLBoolean egl_result_h = eglQuerySurface(_sapp.android.display, _sapp.android.surface, EGL_HEIGHT, &fb_h); |
| 8234 | SOKOL_ASSERT(egl_result_w == EGL_TRUE); _SOKOL_UNUSED(egl_result_w); |
| 8235 | SOKOL_ASSERT(egl_result_h == EGL_TRUE); _SOKOL_UNUSED(egl_result_h); |
| 8236 | const bool fb_changed = (fb_w != _sapp.framebuffer_width) || (fb_h != _sapp.framebuffer_height); |
| 8237 | _sapp.framebuffer_width = fb_w; |
| 8238 | _sapp.framebuffer_height = fb_h; |
| 8239 | _sapp.dpi_scale = (float)_sapp.framebuffer_width / (float)_sapp.window_width; |
| 8240 | if (win_changed || fb_changed || force_update) { |
| 8241 | if (!_sapp.first_frame) { |
| 8242 | _sapp_android_app_event(SAPP_EVENTTYPE_RESIZED); |
| 8243 | } |
| 8244 | } |
| 8245 | } |
| 8246 | |
| 8247 | _SOKOL_PRIVATE void _sapp_android_cleanup(void) { |
| 8248 | if (_sapp.android.surface != EGL_NO_SURFACE) { |
| 8249 | /* egl context is bound, cleanup gracefully */ |
| 8250 | if (_sapp.init_called && !_sapp.cleanup_called) { |
| 8251 | _sapp_call_cleanup(); |
| 8252 | } |
| 8253 | } |
| 8254 | /* always try to cleanup by destroying egl context */ |
| 8255 | _sapp_android_cleanup_egl(); |
| 8256 | } |
| 8257 | |
| 8258 | _SOKOL_PRIVATE void _sapp_android_shutdown(void) { |
| 8259 | /* try to cleanup while we still have a surface and can call cleanup_cb() */ |
| 8260 | _sapp_android_cleanup(); |
| 8261 | /* request exit */ |
| 8262 | ANativeActivity_finish(_sapp.android.activity); |
| 8263 | } |
| 8264 | |
| 8265 | _SOKOL_PRIVATE void _sapp_android_frame(void) { |
| 8266 | SOKOL_ASSERT(_sapp.android.display != EGL_NO_DISPLAY); |
| 8267 | SOKOL_ASSERT(_sapp.android.context != EGL_NO_CONTEXT); |
| 8268 | SOKOL_ASSERT(_sapp.android.surface != EGL_NO_SURFACE); |
| 8269 | _sapp_timing_measure(&_sapp.timing); |
| 8270 | _sapp_android_update_dimensions(_sapp.android.current.window, false); |
| 8271 | _sapp_frame(); |
| 8272 | eglSwapBuffers(_sapp.android.display, _sapp.android.surface); |
| 8273 | } |
| 8274 | |
| 8275 | _SOKOL_PRIVATE bool _sapp_android_touch_event(const AInputEvent* e) { |
| 8276 | if (AInputEvent_getType(e) != AINPUT_EVENT_TYPE_MOTION) { |
| 8277 | return false; |
| 8278 | } |
| 8279 | if (!_sapp_events_enabled()) { |
| 8280 | return false; |
| 8281 | } |
| 8282 | int32_t action_idx = AMotionEvent_getAction(e); |
| 8283 | int32_t action = action_idx & AMOTION_EVENT_ACTION_MASK; |
| 8284 | sapp_event_type type = SAPP_EVENTTYPE_INVALID; |
| 8285 | switch (action) { |
| 8286 | case AMOTION_EVENT_ACTION_DOWN: |
| 8287 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 8288 | type = SAPP_EVENTTYPE_TOUCHES_BEGAN; |
| 8289 | break; |
| 8290 | case AMOTION_EVENT_ACTION_MOVE: |
| 8291 | type = SAPP_EVENTTYPE_TOUCHES_MOVED; |
| 8292 | break; |
| 8293 | case AMOTION_EVENT_ACTION_UP: |
| 8294 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 8295 | type = SAPP_EVENTTYPE_TOUCHES_ENDED; |
| 8296 | break; |
| 8297 | case AMOTION_EVENT_ACTION_CANCEL: |
| 8298 | type = SAPP_EVENTTYPE_TOUCHES_CANCELLED; |
| 8299 | break; |
| 8300 | default: |
| 8301 | break; |
| 8302 | } |
| 8303 | if (type == SAPP_EVENTTYPE_INVALID) { |
| 8304 | return false; |
| 8305 | } |
| 8306 | int32_t idx = action_idx >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 8307 | _sapp_init_event(type); |
| 8308 | _sapp.event.num_touches = (int)AMotionEvent_getPointerCount(e); |
| 8309 | if (_sapp.event.num_touches > SAPP_MAX_TOUCHPOINTS) { |
| 8310 | _sapp.event.num_touches = SAPP_MAX_TOUCHPOINTS; |
| 8311 | } |
| 8312 | for (int32_t i = 0; i < _sapp.event.num_touches; i++) { |
| 8313 | sapp_touchpoint* dst = &_sapp.event.touches[i]; |
| 8314 | dst->identifier = (uintptr_t)AMotionEvent_getPointerId(e, (size_t)i); |
| 8315 | dst->pos_x = (AMotionEvent_getX(e, (size_t)i) / _sapp.window_width) * _sapp.framebuffer_width; |
| 8316 | dst->pos_y = (AMotionEvent_getY(e, (size_t)i) / _sapp.window_height) * _sapp.framebuffer_height; |
| 8317 | dst->android_tooltype = (sapp_android_tooltype) AMotionEvent_getToolType(e, (size_t)i); |
| 8318 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 8319 | action == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 8320 | dst->changed = (i == idx); |
| 8321 | } else { |
| 8322 | dst->changed = true; |
| 8323 | } |
| 8324 | } |
| 8325 | _sapp_call_event(&_sapp.event); |
| 8326 | return true; |
| 8327 | } |
| 8328 | |
| 8329 | _SOKOL_PRIVATE bool _sapp_android_key_event(const AInputEvent* e) { |
| 8330 | if (AInputEvent_getType(e) != AINPUT_EVENT_TYPE_KEY) { |
| 8331 | return false; |
| 8332 | } |
| 8333 | if (AKeyEvent_getKeyCode(e) == AKEYCODE_BACK) { |
| 8334 | /* FIXME: this should be hooked into a "really quit?" mechanism |
| 8335 | so the app can ask the user for confirmation, this is currently |
| 8336 | generally missing in sokol_app.h |
| 8337 | */ |
| 8338 | _sapp_android_shutdown(); |
| 8339 | return true; |
| 8340 | } |
| 8341 | return false; |
| 8342 | } |
| 8343 | |
| 8344 | _SOKOL_PRIVATE int _sapp_android_input_cb(int fd, int events, void* data) { |
| 8345 | _SOKOL_UNUSED(fd); |
| 8346 | _SOKOL_UNUSED(data); |
| 8347 | if ((events & ALOOPER_EVENT_INPUT) == 0) { |
| 8348 | _SAPP_ERROR(ANDROID_UNSUPPORTED_INPUT_EVENT_INPUT_CB); |
| 8349 | return 1; |
| 8350 | } |
| 8351 | SOKOL_ASSERT(_sapp.android.current.input); |
| 8352 | AInputEvent* event = NULL; |
| 8353 | while (AInputQueue_getEvent(_sapp.android.current.input, &event) >= 0) { |
| 8354 | if (AInputQueue_preDispatchEvent(_sapp.android.current.input, event) != 0) { |
| 8355 | continue; |
| 8356 | } |
| 8357 | int32_t handled = 0; |
| 8358 | if (_sapp_android_touch_event(event) || _sapp_android_key_event(event)) { |
| 8359 | handled = 1; |
| 8360 | } |
| 8361 | AInputQueue_finishEvent(_sapp.android.current.input, event, handled); |
| 8362 | } |
| 8363 | return 1; |
| 8364 | } |
| 8365 | |
| 8366 | _SOKOL_PRIVATE int _sapp_android_main_cb(int fd, int events, void* data) { |
| 8367 | _SOKOL_UNUSED(data); |
| 8368 | if ((events & ALOOPER_EVENT_INPUT) == 0) { |
| 8369 | _SAPP_ERROR(ANDROID_UNSUPPORTED_INPUT_EVENT_MAIN_CB); |
| 8370 | return 1; |
| 8371 | } |
| 8372 | |
| 8373 | _sapp_android_msg_t msg; |
| 8374 | if (read(fd, &msg, sizeof(msg)) != sizeof(msg)) { |
| 8375 | _SAPP_ERROR(ANDROID_READ_MSG_FAILED); |
| 8376 | return 1; |
| 8377 | } |
| 8378 | |
| 8379 | pthread_mutex_lock(&_sapp.android.pt.mutex); |
| 8380 | switch (msg) { |
| 8381 | case _SOKOL_ANDROID_MSG_CREATE: |
| 8382 | { |
| 8383 | _SAPP_INFO(ANDROID_MSG_CREATE); |
| 8384 | SOKOL_ASSERT(!_sapp.valid); |
| 8385 | bool result = _sapp_android_init_egl(); |
| 8386 | SOKOL_ASSERT(result); _SOKOL_UNUSED(result); |
| 8387 | _sapp.valid = true; |
| 8388 | _sapp.android.has_created = true; |
| 8389 | } |
| 8390 | break; |
| 8391 | case _SOKOL_ANDROID_MSG_RESUME: |
| 8392 | _SAPP_INFO(ANDROID_MSG_RESUME); |
| 8393 | _sapp.android.has_resumed = true; |
| 8394 | _sapp_android_app_event(SAPP_EVENTTYPE_RESUMED); |
| 8395 | break; |
| 8396 | case _SOKOL_ANDROID_MSG_PAUSE: |
| 8397 | _SAPP_INFO(ANDROID_MSG_PAUSE); |
| 8398 | _sapp.android.has_resumed = false; |
| 8399 | _sapp_android_app_event(SAPP_EVENTTYPE_SUSPENDED); |
| 8400 | break; |
| 8401 | case _SOKOL_ANDROID_MSG_FOCUS: |
| 8402 | _SAPP_INFO(ANDROID_MSG_FOCUS); |
| 8403 | _sapp.android.has_focus = true; |
| 8404 | break; |
| 8405 | case _SOKOL_ANDROID_MSG_NO_FOCUS: |
| 8406 | _SAPP_INFO(ANDROID_MSG_NO_FOCUS); |
| 8407 | _sapp.android.has_focus = false; |
| 8408 | break; |
| 8409 | case _SOKOL_ANDROID_MSG_SET_NATIVE_WINDOW: |
| 8410 | _SAPP_INFO(ANDROID_MSG_SET_NATIVE_WINDOW); |
| 8411 | if (_sapp.android.current.window != _sapp.android.pending.window) { |
| 8412 | if (_sapp.android.current.window != NULL) { |
| 8413 | _sapp_android_cleanup_egl_surface(); |
| 8414 | } |
| 8415 | if (_sapp.android.pending.window != NULL) { |
| 8416 | if (_sapp_android_init_egl_surface(_sapp.android.pending.window)) { |
| 8417 | _sapp_android_update_dimensions(_sapp.android.pending.window, true); |
| 8418 | } else { |
| 8419 | _sapp_android_shutdown(); |
| 8420 | } |
| 8421 | } |
| 8422 | } |
| 8423 | _sapp.android.current.window = _sapp.android.pending.window; |
| 8424 | break; |
| 8425 | case _SOKOL_ANDROID_MSG_SET_INPUT_QUEUE: |
| 8426 | _SAPP_INFO(ANDROID_MSG_SET_INPUT_QUEUE); |
| 8427 | if (_sapp.android.current.input != _sapp.android.pending.input) { |
| 8428 | if (_sapp.android.current.input != NULL) { |
| 8429 | AInputQueue_detachLooper(_sapp.android.current.input); |
| 8430 | } |
| 8431 | if (_sapp.android.pending.input != NULL) { |
| 8432 | AInputQueue_attachLooper( |
| 8433 | _sapp.android.pending.input, |
| 8434 | _sapp.android.looper, |
| 8435 | ALOOPER_POLL_CALLBACK, |
| 8436 | _sapp_android_input_cb, |
| 8437 | NULL); /* data */ |
| 8438 | } |
| 8439 | } |
| 8440 | _sapp.android.current.input = _sapp.android.pending.input; |
| 8441 | break; |
| 8442 | case _SOKOL_ANDROID_MSG_DESTROY: |
| 8443 | _SAPP_INFO(ANDROID_MSG_DESTROY); |
| 8444 | _sapp_android_cleanup(); |
| 8445 | _sapp.valid = false; |
| 8446 | _sapp.android.is_thread_stopping = true; |
| 8447 | break; |
| 8448 | default: |
| 8449 | _SAPP_WARN(ANDROID_UNKNOWN_MSG); |
| 8450 | break; |
| 8451 | } |
| 8452 | pthread_cond_broadcast(&_sapp.android.pt.cond); /* signal "received" */ |
| 8453 | pthread_mutex_unlock(&_sapp.android.pt.mutex); |
| 8454 | return 1; |
| 8455 | } |
| 8456 | |
| 8457 | _SOKOL_PRIVATE bool _sapp_android_should_update(void) { |
| 8458 | bool is_in_front = _sapp.android.has_resumed && _sapp.android.has_focus; |
| 8459 | bool has_surface = _sapp.android.surface != EGL_NO_SURFACE; |
| 8460 | return is_in_front && has_surface; |
| 8461 | } |
| 8462 | |
| 8463 | _SOKOL_PRIVATE void _sapp_android_show_keyboard(bool shown) { |
| 8464 | SOKOL_ASSERT(_sapp.valid); |
| 8465 | /* This seems to be broken in the NDK, but there is (a very cumbersome) workaround... */ |
| 8466 | if (shown) { |
| 8467 | ANativeActivity_showSoftInput(_sapp.android.activity, ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED); |
| 8468 | } else { |
| 8469 | ANativeActivity_hideSoftInput(_sapp.android.activity, ANATIVEACTIVITY_HIDE_SOFT_INPUT_NOT_ALWAYS); |
| 8470 | } |
| 8471 | } |
| 8472 | |
| 8473 | _SOKOL_PRIVATE void* _sapp_android_loop(void* arg) { |
| 8474 | _SOKOL_UNUSED(arg); |
| 8475 | _SAPP_INFO(ANDROID_LOOP_THREAD_STARTED); |
| 8476 | |
| 8477 | _sapp.android.looper = ALooper_prepare(0 /* or ALOOPER_PREPARE_ALLOW_NON_CALLBACKS*/); |
| 8478 | ALooper_addFd(_sapp.android.looper, |
| 8479 | _sapp.android.pt.read_from_main_fd, |
| 8480 | ALOOPER_POLL_CALLBACK, |
| 8481 | ALOOPER_EVENT_INPUT, |
| 8482 | _sapp_android_main_cb, |
| 8483 | NULL); /* data */ |
| 8484 | |
| 8485 | /* signal start to main thread */ |
| 8486 | pthread_mutex_lock(&_sapp.android.pt.mutex); |
| 8487 | _sapp.android.is_thread_started = true; |
| 8488 | pthread_cond_broadcast(&_sapp.android.pt.cond); |
| 8489 | pthread_mutex_unlock(&_sapp.android.pt.mutex); |
| 8490 | |
| 8491 | /* main loop */ |
| 8492 | while (!_sapp.android.is_thread_stopping) { |
| 8493 | /* sokol frame */ |
| 8494 | if (_sapp_android_should_update()) { |
| 8495 | _sapp_android_frame(); |
| 8496 | } |
| 8497 | |
| 8498 | /* process all events (or stop early if app is requested to quit) */ |
| 8499 | bool process_events = true; |
| 8500 | while (process_events && !_sapp.android.is_thread_stopping) { |
| 8501 | bool block_until_event = !_sapp.android.is_thread_stopping && !_sapp_android_should_update(); |
| 8502 | process_events = ALooper_pollOnce(block_until_event ? -1 : 0, NULL, NULL, NULL) == ALOOPER_POLL_CALLBACK; |
| 8503 | } |
| 8504 | } |
| 8505 | |
| 8506 | /* cleanup thread */ |
| 8507 | if (_sapp.android.current.input != NULL) { |
| 8508 | AInputQueue_detachLooper(_sapp.android.current.input); |
| 8509 | } |
| 8510 | |
| 8511 | /* the following causes heap corruption on exit, why?? |
| 8512 | ALooper_removeFd(_sapp.android.looper, _sapp.android.pt.read_from_main_fd); |
| 8513 | ALooper_release(_sapp.android.looper);*/ |
| 8514 | |
| 8515 | /* signal "destroyed" */ |
| 8516 | pthread_mutex_lock(&_sapp.android.pt.mutex); |
| 8517 | _sapp.android.is_thread_stopped = true; |
| 8518 | pthread_cond_broadcast(&_sapp.android.pt.cond); |
| 8519 | pthread_mutex_unlock(&_sapp.android.pt.mutex); |
| 8520 | |
| 8521 | _SAPP_INFO(ANDROID_LOOP_THREAD_DONE); |
| 8522 | return NULL; |
| 8523 | } |
| 8524 | |
| 8525 | /* android main/ui thread */ |
| 8526 | _SOKOL_PRIVATE void _sapp_android_msg(_sapp_android_msg_t msg) { |
| 8527 | if (write(_sapp.android.pt.write_from_main_fd, &msg, sizeof(msg)) != sizeof(msg)) { |
| 8528 | _SAPP_ERROR(ANDROID_WRITE_MSG_FAILED); |
| 8529 | } |
| 8530 | } |
| 8531 | |
| 8532 | _SOKOL_PRIVATE void _sapp_android_on_start(ANativeActivity* activity) { |
| 8533 | _SOKOL_UNUSED(activity); |
| 8534 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONSTART); |
| 8535 | } |
| 8536 | |
| 8537 | _SOKOL_PRIVATE void _sapp_android_on_resume(ANativeActivity* activity) { |
| 8538 | _SOKOL_UNUSED(activity); |
| 8539 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONRESUME); |
| 8540 | _sapp_android_msg(_SOKOL_ANDROID_MSG_RESUME); |
| 8541 | } |
| 8542 | |
| 8543 | _SOKOL_PRIVATE void* _sapp_android_on_save_instance_state(ANativeActivity* activity, size_t* out_size) { |
| 8544 | _SOKOL_UNUSED(activity); |
| 8545 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONSAVEINSTANCESTATE); |
| 8546 | *out_size = 0; |
| 8547 | return NULL; |
| 8548 | } |
| 8549 | |
| 8550 | _SOKOL_PRIVATE void _sapp_android_on_window_focus_changed(ANativeActivity* activity, int has_focus) { |
| 8551 | _SOKOL_UNUSED(activity); |
| 8552 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONWINDOWFOCUSCHANGED); |
| 8553 | if (has_focus) { |
| 8554 | _sapp_android_msg(_SOKOL_ANDROID_MSG_FOCUS); |
| 8555 | } else { |
| 8556 | _sapp_android_msg(_SOKOL_ANDROID_MSG_NO_FOCUS); |
| 8557 | } |
| 8558 | } |
| 8559 | |
| 8560 | _SOKOL_PRIVATE void _sapp_android_on_pause(ANativeActivity* activity) { |
| 8561 | _SOKOL_UNUSED(activity); |
| 8562 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONPAUSE); |
| 8563 | _sapp_android_msg(_SOKOL_ANDROID_MSG_PAUSE); |
| 8564 | } |
| 8565 | |
| 8566 | _SOKOL_PRIVATE void _sapp_android_on_stop(ANativeActivity* activity) { |
| 8567 | _SOKOL_UNUSED(activity); |
| 8568 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONSTOP); |
| 8569 | } |
| 8570 | |
| 8571 | _SOKOL_PRIVATE void _sapp_android_msg_set_native_window(ANativeWindow* window) { |
| 8572 | pthread_mutex_lock(&_sapp.android.pt.mutex); |
| 8573 | _sapp.android.pending.window = window; |
| 8574 | _sapp_android_msg(_SOKOL_ANDROID_MSG_SET_NATIVE_WINDOW); |
| 8575 | while (_sapp.android.current.window != window) { |
| 8576 | pthread_cond_wait(&_sapp.android.pt.cond, &_sapp.android.pt.mutex); |
| 8577 | } |
| 8578 | pthread_mutex_unlock(&_sapp.android.pt.mutex); |
| 8579 | } |
| 8580 | |
| 8581 | _SOKOL_PRIVATE void _sapp_android_on_native_window_created(ANativeActivity* activity, ANativeWindow* window) { |
| 8582 | _SOKOL_UNUSED(activity); |
| 8583 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONNATIVEWINDOWCREATED); |
| 8584 | _sapp_android_msg_set_native_window(window); |
| 8585 | } |
| 8586 | |
| 8587 | _SOKOL_PRIVATE void _sapp_android_on_native_window_destroyed(ANativeActivity* activity, ANativeWindow* window) { |
| 8588 | _SOKOL_UNUSED(activity); |
| 8589 | _SOKOL_UNUSED(window); |
| 8590 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONNATIVEWINDOWDESTROYED); |
| 8591 | _sapp_android_msg_set_native_window(NULL); |
| 8592 | } |
| 8593 | |
| 8594 | _SOKOL_PRIVATE void _sapp_android_msg_set_input_queue(AInputQueue* input) { |
| 8595 | pthread_mutex_lock(&_sapp.android.pt.mutex); |
| 8596 | _sapp.android.pending.input = input; |
| 8597 | _sapp_android_msg(_SOKOL_ANDROID_MSG_SET_INPUT_QUEUE); |
| 8598 | while (_sapp.android.current.input != input) { |
| 8599 | pthread_cond_wait(&_sapp.android.pt.cond, &_sapp.android.pt.mutex); |
| 8600 | } |
| 8601 | pthread_mutex_unlock(&_sapp.android.pt.mutex); |
| 8602 | } |
| 8603 | |
| 8604 | _SOKOL_PRIVATE void _sapp_android_on_input_queue_created(ANativeActivity* activity, AInputQueue* queue) { |
| 8605 | _SOKOL_UNUSED(activity); |
| 8606 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONINPUTQUEUECREATED); |
| 8607 | _sapp_android_msg_set_input_queue(queue); |
| 8608 | } |
| 8609 | |
| 8610 | _SOKOL_PRIVATE void _sapp_android_on_input_queue_destroyed(ANativeActivity* activity, AInputQueue* queue) { |
| 8611 | _SOKOL_UNUSED(activity); |
| 8612 | _SOKOL_UNUSED(queue); |
| 8613 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONINPUTQUEUEDESTROYED); |
| 8614 | _sapp_android_msg_set_input_queue(NULL); |
| 8615 | } |
| 8616 | |
| 8617 | _SOKOL_PRIVATE void _sapp_android_on_config_changed(ANativeActivity* activity) { |
| 8618 | _SOKOL_UNUSED(activity); |
| 8619 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONCONFIGURATIONCHANGED); |
| 8620 | /* see android:configChanges in manifest */ |
| 8621 | } |
| 8622 | |
| 8623 | _SOKOL_PRIVATE void _sapp_android_on_low_memory(ANativeActivity* activity) { |
| 8624 | _SOKOL_UNUSED(activity); |
| 8625 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONLOWMEMORY); |
| 8626 | } |
| 8627 | |
| 8628 | _SOKOL_PRIVATE void _sapp_android_on_destroy(ANativeActivity* activity) { |
| 8629 | /* |
| 8630 | * For some reason even an empty app using nativeactivity.h will crash (WIN DEATH) |
| 8631 | * on my device (Moto X 2nd gen) when the app is removed from the task view |
| 8632 | * (TaskStackView: onTaskViewDismissed). |
| 8633 | * |
| 8634 | * However, if ANativeActivity_finish() is explicitly called from for example |
| 8635 | * _sapp_android_on_stop(), the crash disappears. Is this a bug in NativeActivity? |
| 8636 | */ |
| 8637 | _SOKOL_UNUSED(activity); |
| 8638 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONDESTROY); |
| 8639 | |
| 8640 | /* send destroy msg */ |
| 8641 | pthread_mutex_lock(&_sapp.android.pt.mutex); |
| 8642 | _sapp_android_msg(_SOKOL_ANDROID_MSG_DESTROY); |
| 8643 | while (!_sapp.android.is_thread_stopped) { |
| 8644 | pthread_cond_wait(&_sapp.android.pt.cond, &_sapp.android.pt.mutex); |
| 8645 | } |
| 8646 | pthread_mutex_unlock(&_sapp.android.pt.mutex); |
| 8647 | |
| 8648 | /* clean up main thread */ |
| 8649 | pthread_cond_destroy(&_sapp.android.pt.cond); |
| 8650 | pthread_mutex_destroy(&_sapp.android.pt.mutex); |
| 8651 | |
| 8652 | close(_sapp.android.pt.read_from_main_fd); |
| 8653 | close(_sapp.android.pt.write_from_main_fd); |
| 8654 | |
| 8655 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_DONE); |
| 8656 | |
| 8657 | /* this is a bit naughty, but causes a clean restart of the app (static globals are reset) */ |
| 8658 | exit(0); |
| 8659 | } |
| 8660 | |
| 8661 | JNIEXPORT |
| 8662 | void ANativeActivity_onCreate(ANativeActivity* activity, void* saved_state, size_t saved_state_size) { |
| 8663 | _SOKOL_UNUSED(saved_state); |
| 8664 | _SOKOL_UNUSED(saved_state_size); |
| 8665 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_ONCREATE); |
| 8666 | |
| 8667 | // the NativeActity pointer needs to be available inside sokol_main() |
| 8668 | // (see https://github.com/floooh/sokol/issues/708), however _sapp_init_state() |
| 8669 | // will clear the global _sapp_t struct, so we need to initialize the native |
| 8670 | // activity pointer twice, once before sokol_main() and once after _sapp_init_state() |
| 8671 | _sapp_clear(&_sapp, sizeof(_sapp)); |
| 8672 | _sapp.android.activity = activity; |
| 8673 | sapp_desc desc = sokol_main(0, NULL); |
| 8674 | _sapp_init_state(&desc); |
| 8675 | _sapp.android.activity = activity; |
| 8676 | |
| 8677 | int pipe_fd[2]; |
| 8678 | if (pipe(pipe_fd) != 0) { |
| 8679 | _SAPP_ERROR(ANDROID_CREATE_THREAD_PIPE_FAILED); |
| 8680 | return; |
| 8681 | } |
| 8682 | _sapp.android.pt.read_from_main_fd = pipe_fd[0]; |
| 8683 | _sapp.android.pt.write_from_main_fd = pipe_fd[1]; |
| 8684 | |
| 8685 | pthread_mutex_init(&_sapp.android.pt.mutex, NULL); |
| 8686 | pthread_cond_init(&_sapp.android.pt.cond, NULL); |
| 8687 | |
| 8688 | pthread_attr_t attr; |
| 8689 | pthread_attr_init(&attr); |
| 8690 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 8691 | pthread_create(&_sapp.android.pt.thread, &attr, _sapp_android_loop, 0); |
| 8692 | pthread_attr_destroy(&attr); |
| 8693 | |
| 8694 | /* wait until main loop has started */ |
| 8695 | pthread_mutex_lock(&_sapp.android.pt.mutex); |
| 8696 | while (!_sapp.android.is_thread_started) { |
| 8697 | pthread_cond_wait(&_sapp.android.pt.cond, &_sapp.android.pt.mutex); |
| 8698 | } |
| 8699 | pthread_mutex_unlock(&_sapp.android.pt.mutex); |
| 8700 | |
| 8701 | /* send create msg */ |
| 8702 | pthread_mutex_lock(&_sapp.android.pt.mutex); |
| 8703 | _sapp_android_msg(_SOKOL_ANDROID_MSG_CREATE); |
| 8704 | while (!_sapp.android.has_created) { |
| 8705 | pthread_cond_wait(&_sapp.android.pt.cond, &_sapp.android.pt.mutex); |
| 8706 | } |
| 8707 | pthread_mutex_unlock(&_sapp.android.pt.mutex); |
| 8708 | |
| 8709 | /* register for callbacks */ |
| 8710 | activity->callbacks->onStart = _sapp_android_on_start; |
| 8711 | activity->callbacks->onResume = _sapp_android_on_resume; |
| 8712 | activity->callbacks->onSaveInstanceState = _sapp_android_on_save_instance_state; |
| 8713 | activity->callbacks->onWindowFocusChanged = _sapp_android_on_window_focus_changed; |
| 8714 | activity->callbacks->onPause = _sapp_android_on_pause; |
| 8715 | activity->callbacks->onStop = _sapp_android_on_stop; |
| 8716 | activity->callbacks->onDestroy = _sapp_android_on_destroy; |
| 8717 | activity->callbacks->onNativeWindowCreated = _sapp_android_on_native_window_created; |
| 8718 | /* activity->callbacks->onNativeWindowResized = _sapp_android_on_native_window_resized; */ |
| 8719 | /* activity->callbacks->onNativeWindowRedrawNeeded = _sapp_android_on_native_window_redraw_needed; */ |
| 8720 | activity->callbacks->onNativeWindowDestroyed = _sapp_android_on_native_window_destroyed; |
| 8721 | activity->callbacks->onInputQueueCreated = _sapp_android_on_input_queue_created; |
| 8722 | activity->callbacks->onInputQueueDestroyed = _sapp_android_on_input_queue_destroyed; |
| 8723 | /* activity->callbacks->onContentRectChanged = _sapp_android_on_content_rect_changed; */ |
| 8724 | activity->callbacks->onConfigurationChanged = _sapp_android_on_config_changed; |
| 8725 | activity->callbacks->onLowMemory = _sapp_android_on_low_memory; |
| 8726 | |
| 8727 | _SAPP_INFO(ANDROID_NATIVE_ACTIVITY_CREATE_SUCCESS); |
| 8728 | |
| 8729 | /* NOT A BUG: do NOT call sapp_discard_state() */ |
| 8730 | } |
| 8731 | |
| 8732 | #endif /* _SAPP_ANDROID */ |
| 8733 | |
| 8734 | // ██ ██ ███ ██ ██ ██ ██ ██ |
| 8735 | // ██ ██ ████ ██ ██ ██ ██ ██ |
| 8736 | // ██ ██ ██ ██ ██ ██ ██ ███ |
| 8737 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 8738 | // ███████ ██ ██ ████ ██████ ██ ██ |
| 8739 | // |
| 8740 | // >>linux |
| 8741 | #if defined(_SAPP_LINUX) |
| 8742 | |
| 8743 | /* see GLFW's xkb_unicode.c */ |
| 8744 | static const struct _sapp_x11_codepair { |
| 8745 | uint16_t keysym; |
| 8746 | uint16_t ucs; |
| 8747 | } _sapp_x11_keysymtab[] = { |
| 8748 | { 0x01a1, 0x0104 }, |
| 8749 | { 0x01a2, 0x02d8 }, |
| 8750 | { 0x01a3, 0x0141 }, |
| 8751 | { 0x01a5, 0x013d }, |
| 8752 | { 0x01a6, 0x015a }, |
| 8753 | { 0x01a9, 0x0160 }, |
| 8754 | { 0x01aa, 0x015e }, |
| 8755 | { 0x01ab, 0x0164 }, |
| 8756 | { 0x01ac, 0x0179 }, |
| 8757 | { 0x01ae, 0x017d }, |
| 8758 | { 0x01af, 0x017b }, |
| 8759 | { 0x01b1, 0x0105 }, |
| 8760 | { 0x01b2, 0x02db }, |
| 8761 | { 0x01b3, 0x0142 }, |
| 8762 | { 0x01b5, 0x013e }, |
| 8763 | { 0x01b6, 0x015b }, |
| 8764 | { 0x01b7, 0x02c7 }, |
| 8765 | { 0x01b9, 0x0161 }, |
| 8766 | { 0x01ba, 0x015f }, |
| 8767 | { 0x01bb, 0x0165 }, |
| 8768 | { 0x01bc, 0x017a }, |
| 8769 | { 0x01bd, 0x02dd }, |
| 8770 | { 0x01be, 0x017e }, |
| 8771 | { 0x01bf, 0x017c }, |
| 8772 | { 0x01c0, 0x0154 }, |
| 8773 | { 0x01c3, 0x0102 }, |
| 8774 | { 0x01c5, 0x0139 }, |
| 8775 | { 0x01c6, 0x0106 }, |
| 8776 | { 0x01c8, 0x010c }, |
| 8777 | { 0x01ca, 0x0118 }, |
| 8778 | { 0x01cc, 0x011a }, |
| 8779 | { 0x01cf, 0x010e }, |
| 8780 | { 0x01d0, 0x0110 }, |
| 8781 | { 0x01d1, 0x0143 }, |
| 8782 | { 0x01d2, 0x0147 }, |
| 8783 | { 0x01d5, 0x0150 }, |
| 8784 | { 0x01d8, 0x0158 }, |
| 8785 | { 0x01d9, 0x016e }, |
| 8786 | { 0x01db, 0x0170 }, |
| 8787 | { 0x01de, 0x0162 }, |
| 8788 | { 0x01e0, 0x0155 }, |
| 8789 | { 0x01e3, 0x0103 }, |
| 8790 | { 0x01e5, 0x013a }, |
| 8791 | { 0x01e6, 0x0107 }, |
| 8792 | { 0x01e8, 0x010d }, |
| 8793 | { 0x01ea, 0x0119 }, |
| 8794 | { 0x01ec, 0x011b }, |
| 8795 | { 0x01ef, 0x010f }, |
| 8796 | { 0x01f0, 0x0111 }, |
| 8797 | { 0x01f1, 0x0144 }, |
| 8798 | { 0x01f2, 0x0148 }, |
| 8799 | { 0x01f5, 0x0151 }, |
| 8800 | { 0x01f8, 0x0159 }, |
| 8801 | { 0x01f9, 0x016f }, |
| 8802 | { 0x01fb, 0x0171 }, |
| 8803 | { 0x01fe, 0x0163 }, |
| 8804 | { 0x01ff, 0x02d9 }, |
| 8805 | { 0x02a1, 0x0126 }, |
| 8806 | { 0x02a6, 0x0124 }, |
| 8807 | { 0x02a9, 0x0130 }, |
| 8808 | { 0x02ab, 0x011e }, |
| 8809 | { 0x02ac, 0x0134 }, |
| 8810 | { 0x02b1, 0x0127 }, |
| 8811 | { 0x02b6, 0x0125 }, |
| 8812 | { 0x02b9, 0x0131 }, |
| 8813 | { 0x02bb, 0x011f }, |
| 8814 | { 0x02bc, 0x0135 }, |
| 8815 | { 0x02c5, 0x010a }, |
| 8816 | { 0x02c6, 0x0108 }, |
| 8817 | { 0x02d5, 0x0120 }, |
| 8818 | { 0x02d8, 0x011c }, |
| 8819 | { 0x02dd, 0x016c }, |
| 8820 | { 0x02de, 0x015c }, |
| 8821 | { 0x02e5, 0x010b }, |
| 8822 | { 0x02e6, 0x0109 }, |
| 8823 | { 0x02f5, 0x0121 }, |
| 8824 | { 0x02f8, 0x011d }, |
| 8825 | { 0x02fd, 0x016d }, |
| 8826 | { 0x02fe, 0x015d }, |
| 8827 | { 0x03a2, 0x0138 }, |
| 8828 | { 0x03a3, 0x0156 }, |
| 8829 | { 0x03a5, 0x0128 }, |
| 8830 | { 0x03a6, 0x013b }, |
| 8831 | { 0x03aa, 0x0112 }, |
| 8832 | { 0x03ab, 0x0122 }, |
| 8833 | { 0x03ac, 0x0166 }, |
| 8834 | { 0x03b3, 0x0157 }, |
| 8835 | { 0x03b5, 0x0129 }, |
| 8836 | { 0x03b6, 0x013c }, |
| 8837 | { 0x03ba, 0x0113 }, |
| 8838 | { 0x03bb, 0x0123 }, |
| 8839 | { 0x03bc, 0x0167 }, |
| 8840 | { 0x03bd, 0x014a }, |
| 8841 | { 0x03bf, 0x014b }, |
| 8842 | { 0x03c0, 0x0100 }, |
| 8843 | { 0x03c7, 0x012e }, |
| 8844 | { 0x03cc, 0x0116 }, |
| 8845 | { 0x03cf, 0x012a }, |
| 8846 | { 0x03d1, 0x0145 }, |
| 8847 | { 0x03d2, 0x014c }, |
| 8848 | { 0x03d3, 0x0136 }, |
| 8849 | { 0x03d9, 0x0172 }, |
| 8850 | { 0x03dd, 0x0168 }, |
| 8851 | { 0x03de, 0x016a }, |
| 8852 | { 0x03e0, 0x0101 }, |
| 8853 | { 0x03e7, 0x012f }, |
| 8854 | { 0x03ec, 0x0117 }, |
| 8855 | { 0x03ef, 0x012b }, |
| 8856 | { 0x03f1, 0x0146 }, |
| 8857 | { 0x03f2, 0x014d }, |
| 8858 | { 0x03f3, 0x0137 }, |
| 8859 | { 0x03f9, 0x0173 }, |
| 8860 | { 0x03fd, 0x0169 }, |
| 8861 | { 0x03fe, 0x016b }, |
| 8862 | { 0x047e, 0x203e }, |
| 8863 | { 0x04a1, 0x3002 }, |
| 8864 | { 0x04a2, 0x300c }, |
| 8865 | { 0x04a3, 0x300d }, |
| 8866 | { 0x04a4, 0x3001 }, |
| 8867 | { 0x04a5, 0x30fb }, |
| 8868 | { 0x04a6, 0x30f2 }, |
| 8869 | { 0x04a7, 0x30a1 }, |
| 8870 | { 0x04a8, 0x30a3 }, |
| 8871 | { 0x04a9, 0x30a5 }, |
| 8872 | { 0x04aa, 0x30a7 }, |
| 8873 | { 0x04ab, 0x30a9 }, |
| 8874 | { 0x04ac, 0x30e3 }, |
| 8875 | { 0x04ad, 0x30e5 }, |
| 8876 | { 0x04ae, 0x30e7 }, |
| 8877 | { 0x04af, 0x30c3 }, |
| 8878 | { 0x04b0, 0x30fc }, |
| 8879 | { 0x04b1, 0x30a2 }, |
| 8880 | { 0x04b2, 0x30a4 }, |
| 8881 | { 0x04b3, 0x30a6 }, |
| 8882 | { 0x04b4, 0x30a8 }, |
| 8883 | { 0x04b5, 0x30aa }, |
| 8884 | { 0x04b6, 0x30ab }, |
| 8885 | { 0x04b7, 0x30ad }, |
| 8886 | { 0x04b8, 0x30af }, |
| 8887 | { 0x04b9, 0x30b1 }, |
| 8888 | { 0x04ba, 0x30b3 }, |
| 8889 | { 0x04bb, 0x30b5 }, |
| 8890 | { 0x04bc, 0x30b7 }, |
| 8891 | { 0x04bd, 0x30b9 }, |
| 8892 | { 0x04be, 0x30bb }, |
| 8893 | { 0x04bf, 0x30bd }, |
| 8894 | { 0x04c0, 0x30bf }, |
| 8895 | { 0x04c1, 0x30c1 }, |
| 8896 | { 0x04c2, 0x30c4 }, |
| 8897 | { 0x04c3, 0x30c6 }, |
| 8898 | { 0x04c4, 0x30c8 }, |
| 8899 | { 0x04c5, 0x30ca }, |
| 8900 | { 0x04c6, 0x30cb }, |
| 8901 | { 0x04c7, 0x30cc }, |
| 8902 | { 0x04c8, 0x30cd }, |
| 8903 | { 0x04c9, 0x30ce }, |
| 8904 | { 0x04ca, 0x30cf }, |
| 8905 | { 0x04cb, 0x30d2 }, |
| 8906 | { 0x04cc, 0x30d5 }, |
| 8907 | { 0x04cd, 0x30d8 }, |
| 8908 | { 0x04ce, 0x30db }, |
| 8909 | { 0x04cf, 0x30de }, |
| 8910 | { 0x04d0, 0x30df }, |
| 8911 | { 0x04d1, 0x30e0 }, |
| 8912 | { 0x04d2, 0x30e1 }, |
| 8913 | { 0x04d3, 0x30e2 }, |
| 8914 | { 0x04d4, 0x30e4 }, |
| 8915 | { 0x04d5, 0x30e6 }, |
| 8916 | { 0x04d6, 0x30e8 }, |
| 8917 | { 0x04d7, 0x30e9 }, |
| 8918 | { 0x04d8, 0x30ea }, |
| 8919 | { 0x04d9, 0x30eb }, |
| 8920 | { 0x04da, 0x30ec }, |
| 8921 | { 0x04db, 0x30ed }, |
| 8922 | { 0x04dc, 0x30ef }, |
| 8923 | { 0x04dd, 0x30f3 }, |
| 8924 | { 0x04de, 0x309b }, |
| 8925 | { 0x04df, 0x309c }, |
| 8926 | { 0x05ac, 0x060c }, |
| 8927 | { 0x05bb, 0x061b }, |
| 8928 | { 0x05bf, 0x061f }, |
| 8929 | { 0x05c1, 0x0621 }, |
| 8930 | { 0x05c2, 0x0622 }, |
| 8931 | { 0x05c3, 0x0623 }, |
| 8932 | { 0x05c4, 0x0624 }, |
| 8933 | { 0x05c5, 0x0625 }, |
| 8934 | { 0x05c6, 0x0626 }, |
| 8935 | { 0x05c7, 0x0627 }, |
| 8936 | { 0x05c8, 0x0628 }, |
| 8937 | { 0x05c9, 0x0629 }, |
| 8938 | { 0x05ca, 0x062a }, |
| 8939 | { 0x05cb, 0x062b }, |
| 8940 | { 0x05cc, 0x062c }, |
| 8941 | { 0x05cd, 0x062d }, |
| 8942 | { 0x05ce, 0x062e }, |
| 8943 | { 0x05cf, 0x062f }, |
| 8944 | { 0x05d0, 0x0630 }, |
| 8945 | { 0x05d1, 0x0631 }, |
| 8946 | { 0x05d2, 0x0632 }, |
| 8947 | { 0x05d3, 0x0633 }, |
| 8948 | { 0x05d4, 0x0634 }, |
| 8949 | { 0x05d5, 0x0635 }, |
| 8950 | { 0x05d6, 0x0636 }, |
| 8951 | { 0x05d7, 0x0637 }, |
| 8952 | { 0x05d8, 0x0638 }, |
| 8953 | { 0x05d9, 0x0639 }, |
| 8954 | { 0x05da, 0x063a }, |
| 8955 | { 0x05e0, 0x0640 }, |
| 8956 | { 0x05e1, 0x0641 }, |
| 8957 | { 0x05e2, 0x0642 }, |
| 8958 | { 0x05e3, 0x0643 }, |
| 8959 | { 0x05e4, 0x0644 }, |
| 8960 | { 0x05e5, 0x0645 }, |
| 8961 | { 0x05e6, 0x0646 }, |
| 8962 | { 0x05e7, 0x0647 }, |
| 8963 | { 0x05e8, 0x0648 }, |
| 8964 | { 0x05e9, 0x0649 }, |
| 8965 | { 0x05ea, 0x064a }, |
| 8966 | { 0x05eb, 0x064b }, |
| 8967 | { 0x05ec, 0x064c }, |
| 8968 | { 0x05ed, 0x064d }, |
| 8969 | { 0x05ee, 0x064e }, |
| 8970 | { 0x05ef, 0x064f }, |
| 8971 | { 0x05f0, 0x0650 }, |
| 8972 | { 0x05f1, 0x0651 }, |
| 8973 | { 0x05f2, 0x0652 }, |
| 8974 | { 0x06a1, 0x0452 }, |
| 8975 | { 0x06a2, 0x0453 }, |
| 8976 | { 0x06a3, 0x0451 }, |
| 8977 | { 0x06a4, 0x0454 }, |
| 8978 | { 0x06a5, 0x0455 }, |
| 8979 | { 0x06a6, 0x0456 }, |
| 8980 | { 0x06a7, 0x0457 }, |
| 8981 | { 0x06a8, 0x0458 }, |
| 8982 | { 0x06a9, 0x0459 }, |
| 8983 | { 0x06aa, 0x045a }, |
| 8984 | { 0x06ab, 0x045b }, |
| 8985 | { 0x06ac, 0x045c }, |
| 8986 | { 0x06ae, 0x045e }, |
| 8987 | { 0x06af, 0x045f }, |
| 8988 | { 0x06b0, 0x2116 }, |
| 8989 | { 0x06b1, 0x0402 }, |
| 8990 | { 0x06b2, 0x0403 }, |
| 8991 | { 0x06b3, 0x0401 }, |
| 8992 | { 0x06b4, 0x0404 }, |
| 8993 | { 0x06b5, 0x0405 }, |
| 8994 | { 0x06b6, 0x0406 }, |
| 8995 | { 0x06b7, 0x0407 }, |
| 8996 | { 0x06b8, 0x0408 }, |
| 8997 | { 0x06b9, 0x0409 }, |
| 8998 | { 0x06ba, 0x040a }, |
| 8999 | { 0x06bb, 0x040b }, |
| 9000 | { 0x06bc, 0x040c }, |
| 9001 | { 0x06be, 0x040e }, |
| 9002 | { 0x06bf, 0x040f }, |
| 9003 | { 0x06c0, 0x044e }, |
| 9004 | { 0x06c1, 0x0430 }, |
| 9005 | { 0x06c2, 0x0431 }, |
| 9006 | { 0x06c3, 0x0446 }, |
| 9007 | { 0x06c4, 0x0434 }, |
| 9008 | { 0x06c5, 0x0435 }, |
| 9009 | { 0x06c6, 0x0444 }, |
| 9010 | { 0x06c7, 0x0433 }, |
| 9011 | { 0x06c8, 0x0445 }, |
| 9012 | { 0x06c9, 0x0438 }, |
| 9013 | { 0x06ca, 0x0439 }, |
| 9014 | { 0x06cb, 0x043a }, |
| 9015 | { 0x06cc, 0x043b }, |
| 9016 | { 0x06cd, 0x043c }, |
| 9017 | { 0x06ce, 0x043d }, |
| 9018 | { 0x06cf, 0x043e }, |
| 9019 | { 0x06d0, 0x043f }, |
| 9020 | { 0x06d1, 0x044f }, |
| 9021 | { 0x06d2, 0x0440 }, |
| 9022 | { 0x06d3, 0x0441 }, |
| 9023 | { 0x06d4, 0x0442 }, |
| 9024 | { 0x06d5, 0x0443 }, |
| 9025 | { 0x06d6, 0x0436 }, |
| 9026 | { 0x06d7, 0x0432 }, |
| 9027 | { 0x06d8, 0x044c }, |
| 9028 | { 0x06d9, 0x044b }, |
| 9029 | { 0x06da, 0x0437 }, |
| 9030 | { 0x06db, 0x0448 }, |
| 9031 | { 0x06dc, 0x044d }, |
| 9032 | { 0x06dd, 0x0449 }, |
| 9033 | { 0x06de, 0x0447 }, |
| 9034 | { 0x06df, 0x044a }, |
| 9035 | { 0x06e0, 0x042e }, |
| 9036 | { 0x06e1, 0x0410 }, |
| 9037 | { 0x06e2, 0x0411 }, |
| 9038 | { 0x06e3, 0x0426 }, |
| 9039 | { 0x06e4, 0x0414 }, |
| 9040 | { 0x06e5, 0x0415 }, |
| 9041 | { 0x06e6, 0x0424 }, |
| 9042 | { 0x06e7, 0x0413 }, |
| 9043 | { 0x06e8, 0x0425 }, |
| 9044 | { 0x06e9, 0x0418 }, |
| 9045 | { 0x06ea, 0x0419 }, |
| 9046 | { 0x06eb, 0x041a }, |
| 9047 | { 0x06ec, 0x041b }, |
| 9048 | { 0x06ed, 0x041c }, |
| 9049 | { 0x06ee, 0x041d }, |
| 9050 | { 0x06ef, 0x041e }, |
| 9051 | { 0x06f0, 0x041f }, |
| 9052 | { 0x06f1, 0x042f }, |
| 9053 | { 0x06f2, 0x0420 }, |
| 9054 | { 0x06f3, 0x0421 }, |
| 9055 | { 0x06f4, 0x0422 }, |
| 9056 | { 0x06f5, 0x0423 }, |
| 9057 | { 0x06f6, 0x0416 }, |
| 9058 | { 0x06f7, 0x0412 }, |
| 9059 | { 0x06f8, 0x042c }, |
| 9060 | { 0x06f9, 0x042b }, |
| 9061 | { 0x06fa, 0x0417 }, |
| 9062 | { 0x06fb, 0x0428 }, |
| 9063 | { 0x06fc, 0x042d }, |
| 9064 | { 0x06fd, 0x0429 }, |
| 9065 | { 0x06fe, 0x0427 }, |
| 9066 | { 0x06ff, 0x042a }, |
| 9067 | { 0x07a1, 0x0386 }, |
| 9068 | { 0x07a2, 0x0388 }, |
| 9069 | { 0x07a3, 0x0389 }, |
| 9070 | { 0x07a4, 0x038a }, |
| 9071 | { 0x07a5, 0x03aa }, |
| 9072 | { 0x07a7, 0x038c }, |
| 9073 | { 0x07a8, 0x038e }, |
| 9074 | { 0x07a9, 0x03ab }, |
| 9075 | { 0x07ab, 0x038f }, |
| 9076 | { 0x07ae, 0x0385 }, |
| 9077 | { 0x07af, 0x2015 }, |
| 9078 | { 0x07b1, 0x03ac }, |
| 9079 | { 0x07b2, 0x03ad }, |
| 9080 | { 0x07b3, 0x03ae }, |
| 9081 | { 0x07b4, 0x03af }, |
| 9082 | { 0x07b5, 0x03ca }, |
| 9083 | { 0x07b6, 0x0390 }, |
| 9084 | { 0x07b7, 0x03cc }, |
| 9085 | { 0x07b8, 0x03cd }, |
| 9086 | { 0x07b9, 0x03cb }, |
| 9087 | { 0x07ba, 0x03b0 }, |
| 9088 | { 0x07bb, 0x03ce }, |
| 9089 | { 0x07c1, 0x0391 }, |
| 9090 | { 0x07c2, 0x0392 }, |
| 9091 | { 0x07c3, 0x0393 }, |
| 9092 | { 0x07c4, 0x0394 }, |
| 9093 | { 0x07c5, 0x0395 }, |
| 9094 | { 0x07c6, 0x0396 }, |
| 9095 | { 0x07c7, 0x0397 }, |
| 9096 | { 0x07c8, 0x0398 }, |
| 9097 | { 0x07c9, 0x0399 }, |
| 9098 | { 0x07ca, 0x039a }, |
| 9099 | { 0x07cb, 0x039b }, |
| 9100 | { 0x07cc, 0x039c }, |
| 9101 | { 0x07cd, 0x039d }, |
| 9102 | { 0x07ce, 0x039e }, |
| 9103 | { 0x07cf, 0x039f }, |
| 9104 | { 0x07d0, 0x03a0 }, |
| 9105 | { 0x07d1, 0x03a1 }, |
| 9106 | { 0x07d2, 0x03a3 }, |
| 9107 | { 0x07d4, 0x03a4 }, |
| 9108 | { 0x07d5, 0x03a5 }, |
| 9109 | { 0x07d6, 0x03a6 }, |
| 9110 | { 0x07d7, 0x03a7 }, |
| 9111 | { 0x07d8, 0x03a8 }, |
| 9112 | { 0x07d9, 0x03a9 }, |
| 9113 | { 0x07e1, 0x03b1 }, |
| 9114 | { 0x07e2, 0x03b2 }, |
| 9115 | { 0x07e3, 0x03b3 }, |
| 9116 | { 0x07e4, 0x03b4 }, |
| 9117 | { 0x07e5, 0x03b5 }, |
| 9118 | { 0x07e6, 0x03b6 }, |
| 9119 | { 0x07e7, 0x03b7 }, |
| 9120 | { 0x07e8, 0x03b8 }, |
| 9121 | { 0x07e9, 0x03b9 }, |
| 9122 | { 0x07ea, 0x03ba }, |
| 9123 | { 0x07eb, 0x03bb }, |
| 9124 | { 0x07ec, 0x03bc }, |
| 9125 | { 0x07ed, 0x03bd }, |
| 9126 | { 0x07ee, 0x03be }, |
| 9127 | { 0x07ef, 0x03bf }, |
| 9128 | { 0x07f0, 0x03c0 }, |
| 9129 | { 0x07f1, 0x03c1 }, |
| 9130 | { 0x07f2, 0x03c3 }, |
| 9131 | { 0x07f3, 0x03c2 }, |
| 9132 | { 0x07f4, 0x03c4 }, |
| 9133 | { 0x07f5, 0x03c5 }, |
| 9134 | { 0x07f6, 0x03c6 }, |
| 9135 | { 0x07f7, 0x03c7 }, |
| 9136 | { 0x07f8, 0x03c8 }, |
| 9137 | { 0x07f9, 0x03c9 }, |
| 9138 | { 0x08a1, 0x23b7 }, |
| 9139 | { 0x08a2, 0x250c }, |
| 9140 | { 0x08a3, 0x2500 }, |
| 9141 | { 0x08a4, 0x2320 }, |
| 9142 | { 0x08a5, 0x2321 }, |
| 9143 | { 0x08a6, 0x2502 }, |
| 9144 | { 0x08a7, 0x23a1 }, |
| 9145 | { 0x08a8, 0x23a3 }, |
| 9146 | { 0x08a9, 0x23a4 }, |
| 9147 | { 0x08aa, 0x23a6 }, |
| 9148 | { 0x08ab, 0x239b }, |
| 9149 | { 0x08ac, 0x239d }, |
| 9150 | { 0x08ad, 0x239e }, |
| 9151 | { 0x08ae, 0x23a0 }, |
| 9152 | { 0x08af, 0x23a8 }, |
| 9153 | { 0x08b0, 0x23ac }, |
| 9154 | { 0x08bc, 0x2264 }, |
| 9155 | { 0x08bd, 0x2260 }, |
| 9156 | { 0x08be, 0x2265 }, |
| 9157 | { 0x08bf, 0x222b }, |
| 9158 | { 0x08c0, 0x2234 }, |
| 9159 | { 0x08c1, 0x221d }, |
| 9160 | { 0x08c2, 0x221e }, |
| 9161 | { 0x08c5, 0x2207 }, |
| 9162 | { 0x08c8, 0x223c }, |
| 9163 | { 0x08c9, 0x2243 }, |
| 9164 | { 0x08cd, 0x21d4 }, |
| 9165 | { 0x08ce, 0x21d2 }, |
| 9166 | { 0x08cf, 0x2261 }, |
| 9167 | { 0x08d6, 0x221a }, |
| 9168 | { 0x08da, 0x2282 }, |
| 9169 | { 0x08db, 0x2283 }, |
| 9170 | { 0x08dc, 0x2229 }, |
| 9171 | { 0x08dd, 0x222a }, |
| 9172 | { 0x08de, 0x2227 }, |
| 9173 | { 0x08df, 0x2228 }, |
| 9174 | { 0x08ef, 0x2202 }, |
| 9175 | { 0x08f6, 0x0192 }, |
| 9176 | { 0x08fb, 0x2190 }, |
| 9177 | { 0x08fc, 0x2191 }, |
| 9178 | { 0x08fd, 0x2192 }, |
| 9179 | { 0x08fe, 0x2193 }, |
| 9180 | { 0x09e0, 0x25c6 }, |
| 9181 | { 0x09e1, 0x2592 }, |
| 9182 | { 0x09e2, 0x2409 }, |
| 9183 | { 0x09e3, 0x240c }, |
| 9184 | { 0x09e4, 0x240d }, |
| 9185 | { 0x09e5, 0x240a }, |
| 9186 | { 0x09e8, 0x2424 }, |
| 9187 | { 0x09e9, 0x240b }, |
| 9188 | { 0x09ea, 0x2518 }, |
| 9189 | { 0x09eb, 0x2510 }, |
| 9190 | { 0x09ec, 0x250c }, |
| 9191 | { 0x09ed, 0x2514 }, |
| 9192 | { 0x09ee, 0x253c }, |
| 9193 | { 0x09ef, 0x23ba }, |
| 9194 | { 0x09f0, 0x23bb }, |
| 9195 | { 0x09f1, 0x2500 }, |
| 9196 | { 0x09f2, 0x23bc }, |
| 9197 | { 0x09f3, 0x23bd }, |
| 9198 | { 0x09f4, 0x251c }, |
| 9199 | { 0x09f5, 0x2524 }, |
| 9200 | { 0x09f6, 0x2534 }, |
| 9201 | { 0x09f7, 0x252c }, |
| 9202 | { 0x09f8, 0x2502 }, |
| 9203 | { 0x0aa1, 0x2003 }, |
| 9204 | { 0x0aa2, 0x2002 }, |
| 9205 | { 0x0aa3, 0x2004 }, |
| 9206 | { 0x0aa4, 0x2005 }, |
| 9207 | { 0x0aa5, 0x2007 }, |
| 9208 | { 0x0aa6, 0x2008 }, |
| 9209 | { 0x0aa7, 0x2009 }, |
| 9210 | { 0x0aa8, 0x200a }, |
| 9211 | { 0x0aa9, 0x2014 }, |
| 9212 | { 0x0aaa, 0x2013 }, |
| 9213 | { 0x0aae, 0x2026 }, |
| 9214 | { 0x0aaf, 0x2025 }, |
| 9215 | { 0x0ab0, 0x2153 }, |
| 9216 | { 0x0ab1, 0x2154 }, |
| 9217 | { 0x0ab2, 0x2155 }, |
| 9218 | { 0x0ab3, 0x2156 }, |
| 9219 | { 0x0ab4, 0x2157 }, |
| 9220 | { 0x0ab5, 0x2158 }, |
| 9221 | { 0x0ab6, 0x2159 }, |
| 9222 | { 0x0ab7, 0x215a }, |
| 9223 | { 0x0ab8, 0x2105 }, |
| 9224 | { 0x0abb, 0x2012 }, |
| 9225 | { 0x0abc, 0x2329 }, |
| 9226 | { 0x0abe, 0x232a }, |
| 9227 | { 0x0ac3, 0x215b }, |
| 9228 | { 0x0ac4, 0x215c }, |
| 9229 | { 0x0ac5, 0x215d }, |
| 9230 | { 0x0ac6, 0x215e }, |
| 9231 | { 0x0ac9, 0x2122 }, |
| 9232 | { 0x0aca, 0x2613 }, |
| 9233 | { 0x0acc, 0x25c1 }, |
| 9234 | { 0x0acd, 0x25b7 }, |
| 9235 | { 0x0ace, 0x25cb }, |
| 9236 | { 0x0acf, 0x25af }, |
| 9237 | { 0x0ad0, 0x2018 }, |
| 9238 | { 0x0ad1, 0x2019 }, |
| 9239 | { 0x0ad2, 0x201c }, |
| 9240 | { 0x0ad3, 0x201d }, |
| 9241 | { 0x0ad4, 0x211e }, |
| 9242 | { 0x0ad6, 0x2032 }, |
| 9243 | { 0x0ad7, 0x2033 }, |
| 9244 | { 0x0ad9, 0x271d }, |
| 9245 | { 0x0adb, 0x25ac }, |
| 9246 | { 0x0adc, 0x25c0 }, |
| 9247 | { 0x0add, 0x25b6 }, |
| 9248 | { 0x0ade, 0x25cf }, |
| 9249 | { 0x0adf, 0x25ae }, |
| 9250 | { 0x0ae0, 0x25e6 }, |
| 9251 | { 0x0ae1, 0x25ab }, |
| 9252 | { 0x0ae2, 0x25ad }, |
| 9253 | { 0x0ae3, 0x25b3 }, |
| 9254 | { 0x0ae4, 0x25bd }, |
| 9255 | { 0x0ae5, 0x2606 }, |
| 9256 | { 0x0ae6, 0x2022 }, |
| 9257 | { 0x0ae7, 0x25aa }, |
| 9258 | { 0x0ae8, 0x25b2 }, |
| 9259 | { 0x0ae9, 0x25bc }, |
| 9260 | { 0x0aea, 0x261c }, |
| 9261 | { 0x0aeb, 0x261e }, |
| 9262 | { 0x0aec, 0x2663 }, |
| 9263 | { 0x0aed, 0x2666 }, |
| 9264 | { 0x0aee, 0x2665 }, |
| 9265 | { 0x0af0, 0x2720 }, |
| 9266 | { 0x0af1, 0x2020 }, |
| 9267 | { 0x0af2, 0x2021 }, |
| 9268 | { 0x0af3, 0x2713 }, |
| 9269 | { 0x0af4, 0x2717 }, |
| 9270 | { 0x0af5, 0x266f }, |
| 9271 | { 0x0af6, 0x266d }, |
| 9272 | { 0x0af7, 0x2642 }, |
| 9273 | { 0x0af8, 0x2640 }, |
| 9274 | { 0x0af9, 0x260e }, |
| 9275 | { 0x0afa, 0x2315 }, |
| 9276 | { 0x0afb, 0x2117 }, |
| 9277 | { 0x0afc, 0x2038 }, |
| 9278 | { 0x0afd, 0x201a }, |
| 9279 | { 0x0afe, 0x201e }, |
| 9280 | { 0x0ba3, 0x003c }, |
| 9281 | { 0x0ba6, 0x003e }, |
| 9282 | { 0x0ba8, 0x2228 }, |
| 9283 | { 0x0ba9, 0x2227 }, |
| 9284 | { 0x0bc0, 0x00af }, |
| 9285 | { 0x0bc2, 0x22a5 }, |
| 9286 | { 0x0bc3, 0x2229 }, |
| 9287 | { 0x0bc4, 0x230a }, |
| 9288 | { 0x0bc6, 0x005f }, |
| 9289 | { 0x0bca, 0x2218 }, |
| 9290 | { 0x0bcc, 0x2395 }, |
| 9291 | { 0x0bce, 0x22a4 }, |
| 9292 | { 0x0bcf, 0x25cb }, |
| 9293 | { 0x0bd3, 0x2308 }, |
| 9294 | { 0x0bd6, 0x222a }, |
| 9295 | { 0x0bd8, 0x2283 }, |
| 9296 | { 0x0bda, 0x2282 }, |
| 9297 | { 0x0bdc, 0x22a2 }, |
| 9298 | { 0x0bfc, 0x22a3 }, |
| 9299 | { 0x0cdf, 0x2017 }, |
| 9300 | { 0x0ce0, 0x05d0 }, |
| 9301 | { 0x0ce1, 0x05d1 }, |
| 9302 | { 0x0ce2, 0x05d2 }, |
| 9303 | { 0x0ce3, 0x05d3 }, |
| 9304 | { 0x0ce4, 0x05d4 }, |
| 9305 | { 0x0ce5, 0x05d5 }, |
| 9306 | { 0x0ce6, 0x05d6 }, |
| 9307 | { 0x0ce7, 0x05d7 }, |
| 9308 | { 0x0ce8, 0x05d8 }, |
| 9309 | { 0x0ce9, 0x05d9 }, |
| 9310 | { 0x0cea, 0x05da }, |
| 9311 | { 0x0ceb, 0x05db }, |
| 9312 | { 0x0cec, 0x05dc }, |
| 9313 | { 0x0ced, 0x05dd }, |
| 9314 | { 0x0cee, 0x05de }, |
| 9315 | { 0x0cef, 0x05df }, |
| 9316 | { 0x0cf0, 0x05e0 }, |
| 9317 | { 0x0cf1, 0x05e1 }, |
| 9318 | { 0x0cf2, 0x05e2 }, |
| 9319 | { 0x0cf3, 0x05e3 }, |
| 9320 | { 0x0cf4, 0x05e4 }, |
| 9321 | { 0x0cf5, 0x05e5 }, |
| 9322 | { 0x0cf6, 0x05e6 }, |
| 9323 | { 0x0cf7, 0x05e7 }, |
| 9324 | { 0x0cf8, 0x05e8 }, |
| 9325 | { 0x0cf9, 0x05e9 }, |
| 9326 | { 0x0cfa, 0x05ea }, |
| 9327 | { 0x0da1, 0x0e01 }, |
| 9328 | { 0x0da2, 0x0e02 }, |
| 9329 | { 0x0da3, 0x0e03 }, |
| 9330 | { 0x0da4, 0x0e04 }, |
| 9331 | { 0x0da5, 0x0e05 }, |
| 9332 | { 0x0da6, 0x0e06 }, |
| 9333 | { 0x0da7, 0x0e07 }, |
| 9334 | { 0x0da8, 0x0e08 }, |
| 9335 | { 0x0da9, 0x0e09 }, |
| 9336 | { 0x0daa, 0x0e0a }, |
| 9337 | { 0x0dab, 0x0e0b }, |
| 9338 | { 0x0dac, 0x0e0c }, |
| 9339 | { 0x0dad, 0x0e0d }, |
| 9340 | { 0x0dae, 0x0e0e }, |
| 9341 | { 0x0daf, 0x0e0f }, |
| 9342 | { 0x0db0, 0x0e10 }, |
| 9343 | { 0x0db1, 0x0e11 }, |
| 9344 | { 0x0db2, 0x0e12 }, |
| 9345 | { 0x0db3, 0x0e13 }, |
| 9346 | { 0x0db4, 0x0e14 }, |
| 9347 | { 0x0db5, 0x0e15 }, |
| 9348 | { 0x0db6, 0x0e16 }, |
| 9349 | { 0x0db7, 0x0e17 }, |
| 9350 | { 0x0db8, 0x0e18 }, |
| 9351 | { 0x0db9, 0x0e19 }, |
| 9352 | { 0x0dba, 0x0e1a }, |
| 9353 | { 0x0dbb, 0x0e1b }, |
| 9354 | { 0x0dbc, 0x0e1c }, |
| 9355 | { 0x0dbd, 0x0e1d }, |
| 9356 | { 0x0dbe, 0x0e1e }, |
| 9357 | { 0x0dbf, 0x0e1f }, |
| 9358 | { 0x0dc0, 0x0e20 }, |
| 9359 | { 0x0dc1, 0x0e21 }, |
| 9360 | { 0x0dc2, 0x0e22 }, |
| 9361 | { 0x0dc3, 0x0e23 }, |
| 9362 | { 0x0dc4, 0x0e24 }, |
| 9363 | { 0x0dc5, 0x0e25 }, |
| 9364 | { 0x0dc6, 0x0e26 }, |
| 9365 | { 0x0dc7, 0x0e27 }, |
| 9366 | { 0x0dc8, 0x0e28 }, |
| 9367 | { 0x0dc9, 0x0e29 }, |
| 9368 | { 0x0dca, 0x0e2a }, |
| 9369 | { 0x0dcb, 0x0e2b }, |
| 9370 | { 0x0dcc, 0x0e2c }, |
| 9371 | { 0x0dcd, 0x0e2d }, |
| 9372 | { 0x0dce, 0x0e2e }, |
| 9373 | { 0x0dcf, 0x0e2f }, |
| 9374 | { 0x0dd0, 0x0e30 }, |
| 9375 | { 0x0dd1, 0x0e31 }, |
| 9376 | { 0x0dd2, 0x0e32 }, |
| 9377 | { 0x0dd3, 0x0e33 }, |
| 9378 | { 0x0dd4, 0x0e34 }, |
| 9379 | { 0x0dd5, 0x0e35 }, |
| 9380 | { 0x0dd6, 0x0e36 }, |
| 9381 | { 0x0dd7, 0x0e37 }, |
| 9382 | { 0x0dd8, 0x0e38 }, |
| 9383 | { 0x0dd9, 0x0e39 }, |
| 9384 | { 0x0dda, 0x0e3a }, |
| 9385 | { 0x0ddf, 0x0e3f }, |
| 9386 | { 0x0de0, 0x0e40 }, |
| 9387 | { 0x0de1, 0x0e41 }, |
| 9388 | { 0x0de2, 0x0e42 }, |
| 9389 | { 0x0de3, 0x0e43 }, |
| 9390 | { 0x0de4, 0x0e44 }, |
| 9391 | { 0x0de5, 0x0e45 }, |
| 9392 | { 0x0de6, 0x0e46 }, |
| 9393 | { 0x0de7, 0x0e47 }, |
| 9394 | { 0x0de8, 0x0e48 }, |
| 9395 | { 0x0de9, 0x0e49 }, |
| 9396 | { 0x0dea, 0x0e4a }, |
| 9397 | { 0x0deb, 0x0e4b }, |
| 9398 | { 0x0dec, 0x0e4c }, |
| 9399 | { 0x0ded, 0x0e4d }, |
| 9400 | { 0x0df0, 0x0e50 }, |
| 9401 | { 0x0df1, 0x0e51 }, |
| 9402 | { 0x0df2, 0x0e52 }, |
| 9403 | { 0x0df3, 0x0e53 }, |
| 9404 | { 0x0df4, 0x0e54 }, |
| 9405 | { 0x0df5, 0x0e55 }, |
| 9406 | { 0x0df6, 0x0e56 }, |
| 9407 | { 0x0df7, 0x0e57 }, |
| 9408 | { 0x0df8, 0x0e58 }, |
| 9409 | { 0x0df9, 0x0e59 }, |
| 9410 | { 0x0ea1, 0x3131 }, |
| 9411 | { 0x0ea2, 0x3132 }, |
| 9412 | { 0x0ea3, 0x3133 }, |
| 9413 | { 0x0ea4, 0x3134 }, |
| 9414 | { 0x0ea5, 0x3135 }, |
| 9415 | { 0x0ea6, 0x3136 }, |
| 9416 | { 0x0ea7, 0x3137 }, |
| 9417 | { 0x0ea8, 0x3138 }, |
| 9418 | { 0x0ea9, 0x3139 }, |
| 9419 | { 0x0eaa, 0x313a }, |
| 9420 | { 0x0eab, 0x313b }, |
| 9421 | { 0x0eac, 0x313c }, |
| 9422 | { 0x0ead, 0x313d }, |
| 9423 | { 0x0eae, 0x313e }, |
| 9424 | { 0x0eaf, 0x313f }, |
| 9425 | { 0x0eb0, 0x3140 }, |
| 9426 | { 0x0eb1, 0x3141 }, |
| 9427 | { 0x0eb2, 0x3142 }, |
| 9428 | { 0x0eb3, 0x3143 }, |
| 9429 | { 0x0eb4, 0x3144 }, |
| 9430 | { 0x0eb5, 0x3145 }, |
| 9431 | { 0x0eb6, 0x3146 }, |
| 9432 | { 0x0eb7, 0x3147 }, |
| 9433 | { 0x0eb8, 0x3148 }, |
| 9434 | { 0x0eb9, 0x3149 }, |
| 9435 | { 0x0eba, 0x314a }, |
| 9436 | { 0x0ebb, 0x314b }, |
| 9437 | { 0x0ebc, 0x314c }, |
| 9438 | { 0x0ebd, 0x314d }, |
| 9439 | { 0x0ebe, 0x314e }, |
| 9440 | { 0x0ebf, 0x314f }, |
| 9441 | { 0x0ec0, 0x3150 }, |
| 9442 | { 0x0ec1, 0x3151 }, |
| 9443 | { 0x0ec2, 0x3152 }, |
| 9444 | { 0x0ec3, 0x3153 }, |
| 9445 | { 0x0ec4, 0x3154 }, |
| 9446 | { 0x0ec5, 0x3155 }, |
| 9447 | { 0x0ec6, 0x3156 }, |
| 9448 | { 0x0ec7, 0x3157 }, |
| 9449 | { 0x0ec8, 0x3158 }, |
| 9450 | { 0x0ec9, 0x3159 }, |
| 9451 | { 0x0eca, 0x315a }, |
| 9452 | { 0x0ecb, 0x315b }, |
| 9453 | { 0x0ecc, 0x315c }, |
| 9454 | { 0x0ecd, 0x315d }, |
| 9455 | { 0x0ece, 0x315e }, |
| 9456 | { 0x0ecf, 0x315f }, |
| 9457 | { 0x0ed0, 0x3160 }, |
| 9458 | { 0x0ed1, 0x3161 }, |
| 9459 | { 0x0ed2, 0x3162 }, |
| 9460 | { 0x0ed3, 0x3163 }, |
| 9461 | { 0x0ed4, 0x11a8 }, |
| 9462 | { 0x0ed5, 0x11a9 }, |
| 9463 | { 0x0ed6, 0x11aa }, |
| 9464 | { 0x0ed7, 0x11ab }, |
| 9465 | { 0x0ed8, 0x11ac }, |
| 9466 | { 0x0ed9, 0x11ad }, |
| 9467 | { 0x0eda, 0x11ae }, |
| 9468 | { 0x0edb, 0x11af }, |
| 9469 | { 0x0edc, 0x11b0 }, |
| 9470 | { 0x0edd, 0x11b1 }, |
| 9471 | { 0x0ede, 0x11b2 }, |
| 9472 | { 0x0edf, 0x11b3 }, |
| 9473 | { 0x0ee0, 0x11b4 }, |
| 9474 | { 0x0ee1, 0x11b5 }, |
| 9475 | { 0x0ee2, 0x11b6 }, |
| 9476 | { 0x0ee3, 0x11b7 }, |
| 9477 | { 0x0ee4, 0x11b8 }, |
| 9478 | { 0x0ee5, 0x11b9 }, |
| 9479 | { 0x0ee6, 0x11ba }, |
| 9480 | { 0x0ee7, 0x11bb }, |
| 9481 | { 0x0ee8, 0x11bc }, |
| 9482 | { 0x0ee9, 0x11bd }, |
| 9483 | { 0x0eea, 0x11be }, |
| 9484 | { 0x0eeb, 0x11bf }, |
| 9485 | { 0x0eec, 0x11c0 }, |
| 9486 | { 0x0eed, 0x11c1 }, |
| 9487 | { 0x0eee, 0x11c2 }, |
| 9488 | { 0x0eef, 0x316d }, |
| 9489 | { 0x0ef0, 0x3171 }, |
| 9490 | { 0x0ef1, 0x3178 }, |
| 9491 | { 0x0ef2, 0x317f }, |
| 9492 | { 0x0ef3, 0x3181 }, |
| 9493 | { 0x0ef4, 0x3184 }, |
| 9494 | { 0x0ef5, 0x3186 }, |
| 9495 | { 0x0ef6, 0x318d }, |
| 9496 | { 0x0ef7, 0x318e }, |
| 9497 | { 0x0ef8, 0x11eb }, |
| 9498 | { 0x0ef9, 0x11f0 }, |
| 9499 | { 0x0efa, 0x11f9 }, |
| 9500 | { 0x0eff, 0x20a9 }, |
| 9501 | { 0x13a4, 0x20ac }, |
| 9502 | { 0x13bc, 0x0152 }, |
| 9503 | { 0x13bd, 0x0153 }, |
| 9504 | { 0x13be, 0x0178 }, |
| 9505 | { 0x20ac, 0x20ac }, |
| 9506 | { 0xfe50, '`' }, |
| 9507 | { 0xfe51, 0x00b4 }, |
| 9508 | { 0xfe52, '^' }, |
| 9509 | { 0xfe53, '~' }, |
| 9510 | { 0xfe54, 0x00af }, |
| 9511 | { 0xfe55, 0x02d8 }, |
| 9512 | { 0xfe56, 0x02d9 }, |
| 9513 | { 0xfe57, 0x00a8 }, |
| 9514 | { 0xfe58, 0x02da }, |
| 9515 | { 0xfe59, 0x02dd }, |
| 9516 | { 0xfe5a, 0x02c7 }, |
| 9517 | { 0xfe5b, 0x00b8 }, |
| 9518 | { 0xfe5c, 0x02db }, |
| 9519 | { 0xfe5d, 0x037a }, |
| 9520 | { 0xfe5e, 0x309b }, |
| 9521 | { 0xfe5f, 0x309c }, |
| 9522 | { 0xfe63, '/' }, |
| 9523 | { 0xfe64, 0x02bc }, |
| 9524 | { 0xfe65, 0x02bd }, |
| 9525 | { 0xfe66, 0x02f5 }, |
| 9526 | { 0xfe67, 0x02f3 }, |
| 9527 | { 0xfe68, 0x02cd }, |
| 9528 | { 0xfe69, 0xa788 }, |
| 9529 | { 0xfe6a, 0x02f7 }, |
| 9530 | { 0xfe6e, ',' }, |
| 9531 | { 0xfe6f, 0x00a4 }, |
| 9532 | { 0xfe80, 'a' }, /* XK_dead_a */ |
| 9533 | { 0xfe81, 'A' }, /* XK_dead_A */ |
| 9534 | { 0xfe82, 'e' }, /* XK_dead_e */ |
| 9535 | { 0xfe83, 'E' }, /* XK_dead_E */ |
| 9536 | { 0xfe84, 'i' }, /* XK_dead_i */ |
| 9537 | { 0xfe85, 'I' }, /* XK_dead_I */ |
| 9538 | { 0xfe86, 'o' }, /* XK_dead_o */ |
| 9539 | { 0xfe87, 'O' }, /* XK_dead_O */ |
| 9540 | { 0xfe88, 'u' }, /* XK_dead_u */ |
| 9541 | { 0xfe89, 'U' }, /* XK_dead_U */ |
| 9542 | { 0xfe8a, 0x0259 }, |
| 9543 | { 0xfe8b, 0x018f }, |
| 9544 | { 0xfe8c, 0x00b5 }, |
| 9545 | { 0xfe90, '_' }, |
| 9546 | { 0xfe91, 0x02c8 }, |
| 9547 | { 0xfe92, 0x02cc }, |
| 9548 | { 0xff80 /*XKB_KEY_KP_Space*/, ' ' }, |
| 9549 | { 0xff95 /*XKB_KEY_KP_7*/, 0x0037 }, |
| 9550 | { 0xff96 /*XKB_KEY_KP_4*/, 0x0034 }, |
| 9551 | { 0xff97 /*XKB_KEY_KP_8*/, 0x0038 }, |
| 9552 | { 0xff98 /*XKB_KEY_KP_6*/, 0x0036 }, |
| 9553 | { 0xff99 /*XKB_KEY_KP_2*/, 0x0032 }, |
| 9554 | { 0xff9a /*XKB_KEY_KP_9*/, 0x0039 }, |
| 9555 | { 0xff9b /*XKB_KEY_KP_3*/, 0x0033 }, |
| 9556 | { 0xff9c /*XKB_KEY_KP_1*/, 0x0031 }, |
| 9557 | { 0xff9d /*XKB_KEY_KP_5*/, 0x0035 }, |
| 9558 | { 0xff9e /*XKB_KEY_KP_0*/, 0x0030 }, |
| 9559 | { 0xffaa /*XKB_KEY_KP_Multiply*/, '*' }, |
| 9560 | { 0xffab /*XKB_KEY_KP_Add*/, '+' }, |
| 9561 | { 0xffac /*XKB_KEY_KP_Separator*/, ',' }, |
| 9562 | { 0xffad /*XKB_KEY_KP_Subtract*/, '-' }, |
| 9563 | { 0xffae /*XKB_KEY_KP_Decimal*/, '.' }, |
| 9564 | { 0xffaf /*XKB_KEY_KP_Divide*/, '/' }, |
| 9565 | { 0xffb0 /*XKB_KEY_KP_0*/, 0x0030 }, |
| 9566 | { 0xffb1 /*XKB_KEY_KP_1*/, 0x0031 }, |
| 9567 | { 0xffb2 /*XKB_KEY_KP_2*/, 0x0032 }, |
| 9568 | { 0xffb3 /*XKB_KEY_KP_3*/, 0x0033 }, |
| 9569 | { 0xffb4 /*XKB_KEY_KP_4*/, 0x0034 }, |
| 9570 | { 0xffb5 /*XKB_KEY_KP_5*/, 0x0035 }, |
| 9571 | { 0xffb6 /*XKB_KEY_KP_6*/, 0x0036 }, |
| 9572 | { 0xffb7 /*XKB_KEY_KP_7*/, 0x0037 }, |
| 9573 | { 0xffb8 /*XKB_KEY_KP_8*/, 0x0038 }, |
| 9574 | { 0xffb9 /*XKB_KEY_KP_9*/, 0x0039 }, |
| 9575 | { 0xffbd /*XKB_KEY_KP_Equal*/, '=' } |
| 9576 | }; |
| 9577 | |
| 9578 | _SOKOL_PRIVATE int _sapp_x11_error_handler(Display* display, XErrorEvent* event) { |
| 9579 | _SOKOL_UNUSED(display); |
| 9580 | _sapp.x11.error_code = event->error_code; |
| 9581 | return 0; |
| 9582 | } |
| 9583 | |
| 9584 | _SOKOL_PRIVATE void _sapp_x11_grab_error_handler(void) { |
| 9585 | _sapp.x11.error_code = Success; |
| 9586 | XSetErrorHandler(_sapp_x11_error_handler); |
| 9587 | } |
| 9588 | |
| 9589 | _SOKOL_PRIVATE void _sapp_x11_release_error_handler(void) { |
| 9590 | XSync(_sapp.x11.display, False); |
| 9591 | XSetErrorHandler(NULL); |
| 9592 | } |
| 9593 | |
| 9594 | _SOKOL_PRIVATE void _sapp_x11_init_extensions(void) { |
| 9595 | _sapp.x11.UTF8_STRING = XInternAtom(_sapp.x11.display, "UTF8_STRING", False); |
| 9596 | _sapp.x11.WM_PROTOCOLS = XInternAtom(_sapp.x11.display, "WM_PROTOCOLS", False); |
| 9597 | _sapp.x11.WM_DELETE_WINDOW = XInternAtom(_sapp.x11.display, "WM_DELETE_WINDOW", False); |
| 9598 | _sapp.x11.WM_STATE = XInternAtom(_sapp.x11.display, "WM_STATE", False); |
| 9599 | _sapp.x11.NET_WM_NAME = XInternAtom(_sapp.x11.display, "_NET_WM_NAME", False); |
| 9600 | _sapp.x11.NET_WM_ICON_NAME = XInternAtom(_sapp.x11.display, "_NET_WM_ICON_NAME", False); |
| 9601 | _sapp.x11.NET_WM_ICON = XInternAtom(_sapp.x11.display, "_NET_WM_ICON", False); |
| 9602 | _sapp.x11.NET_WM_STATE = XInternAtom(_sapp.x11.display, "_NET_WM_STATE", False); |
| 9603 | _sapp.x11.NET_WM_STATE_FULLSCREEN = XInternAtom(_sapp.x11.display, "_NET_WM_STATE_FULLSCREEN", False); |
| 9604 | if (_sapp.drop.enabled) { |
| 9605 | _sapp.x11.xdnd.XdndAware = XInternAtom(_sapp.x11.display, "XdndAware", False); |
| 9606 | _sapp.x11.xdnd.XdndEnter = XInternAtom(_sapp.x11.display, "XdndEnter", False); |
| 9607 | _sapp.x11.xdnd.XdndPosition = XInternAtom(_sapp.x11.display, "XdndPosition", False); |
| 9608 | _sapp.x11.xdnd.XdndStatus = XInternAtom(_sapp.x11.display, "XdndStatus", False); |
| 9609 | _sapp.x11.xdnd.XdndActionCopy = XInternAtom(_sapp.x11.display, "XdndActionCopy", False); |
| 9610 | _sapp.x11.xdnd.XdndDrop = XInternAtom(_sapp.x11.display, "XdndDrop", False); |
| 9611 | _sapp.x11.xdnd.XdndFinished = XInternAtom(_sapp.x11.display, "XdndFinished", False); |
| 9612 | _sapp.x11.xdnd.XdndSelection = XInternAtom(_sapp.x11.display, "XdndSelection", False); |
| 9613 | _sapp.x11.xdnd.XdndTypeList = XInternAtom(_sapp.x11.display, "XdndTypeList", False); |
| 9614 | _sapp.x11.xdnd.text_uri_list = XInternAtom(_sapp.x11.display, "text/uri-list", False); |
| 9615 | } |
| 9616 | |
| 9617 | /* check Xi extension for raw mouse input */ |
| 9618 | if (XQueryExtension(_sapp.x11.display, "XInputExtension", &_sapp.x11.xi.major_opcode, &_sapp.x11.xi.event_base, &_sapp.x11.xi.error_base)) { |
| 9619 | _sapp.x11.xi.major = 2; |
| 9620 | _sapp.x11.xi.minor = 0; |
| 9621 | if (XIQueryVersion(_sapp.x11.display, &_sapp.x11.xi.major, &_sapp.x11.xi.minor) == Success) { |
| 9622 | _sapp.x11.xi.available = true; |
| 9623 | } |
| 9624 | } |
| 9625 | } |
| 9626 | |
| 9627 | _SOKOL_PRIVATE void _sapp_x11_query_system_dpi(void) { |
| 9628 | /* from GLFW: |
| 9629 | |
| 9630 | NOTE: Default to the display-wide DPI as we don't currently have a policy |
| 9631 | for which monitor a window is considered to be on |
| 9632 | |
| 9633 | _sapp.x11.dpi = DisplayWidth(_sapp.x11.display, _sapp.x11.screen) * |
| 9634 | 25.4f / DisplayWidthMM(_sapp.x11.display, _sapp.x11.screen); |
| 9635 | |
| 9636 | NOTE: Basing the scale on Xft.dpi where available should provide the most |
| 9637 | consistent user experience (matches Qt, Gtk, etc), although not |
| 9638 | always the most accurate one |
| 9639 | */ |
| 9640 | bool dpi_ok = false; |
| 9641 | char* rms = XResourceManagerString(_sapp.x11.display); |
| 9642 | if (rms) { |
| 9643 | XrmDatabase db = XrmGetStringDatabase(rms); |
| 9644 | if (db) { |
| 9645 | XrmValue value; |
| 9646 | char* type = NULL; |
| 9647 | if (XrmGetResource(db, "Xft.dpi", "Xft.Dpi", &type, &value)) { |
| 9648 | if (type && strcmp(type, "String") == 0) { |
| 9649 | _sapp.x11.dpi = atof(value.addr); |
| 9650 | dpi_ok = true; |
| 9651 | } |
| 9652 | } |
| 9653 | XrmDestroyDatabase(db); |
| 9654 | } |
| 9655 | } |
| 9656 | // fallback if querying DPI had failed: assume the standard DPI 96.0f |
| 9657 | if (!dpi_ok) { |
| 9658 | _sapp.x11.dpi = 96.0f; |
| 9659 | _SAPP_WARN(LINUX_X11_QUERY_SYSTEM_DPI_FAILED); |
| 9660 | } |
| 9661 | } |
| 9662 | |
| 9663 | #if defined(_SAPP_GLX) |
| 9664 | |
| 9665 | _SOKOL_PRIVATE bool _sapp_glx_has_ext(const char* ext, const char* extensions) { |
| 9666 | SOKOL_ASSERT(ext); |
| 9667 | const char* start = extensions; |
| 9668 | while (true) { |
| 9669 | const char* where = strstr(start, ext); |
| 9670 | if (!where) { |
| 9671 | return false; |
| 9672 | } |
| 9673 | const char* terminator = where + strlen(ext); |
| 9674 | if ((where == start) || (*(where - 1) == ' ')) { |
| 9675 | if (*terminator == ' ' || *terminator == '\0') { |
| 9676 | break; |
| 9677 | } |
| 9678 | } |
| 9679 | start = terminator; |
| 9680 | } |
| 9681 | return true; |
| 9682 | } |
| 9683 | |
| 9684 | _SOKOL_PRIVATE bool _sapp_glx_extsupported(const char* ext, const char* extensions) { |
| 9685 | if (extensions) { |
| 9686 | return _sapp_glx_has_ext(ext, extensions); |
| 9687 | } |
| 9688 | else { |
| 9689 | return false; |
| 9690 | } |
| 9691 | } |
| 9692 | |
| 9693 | _SOKOL_PRIVATE void* _sapp_glx_getprocaddr(const char* procname) |
| 9694 | { |
| 9695 | if (_sapp.glx.GetProcAddress) { |
| 9696 | return (void*) _sapp.glx.GetProcAddress(procname); |
| 9697 | } |
| 9698 | else if (_sapp.glx.GetProcAddressARB) { |
| 9699 | return (void*) _sapp.glx.GetProcAddressARB(procname); |
| 9700 | } |
| 9701 | else { |
| 9702 | return dlsym(_sapp.glx.libgl, procname); |
| 9703 | } |
| 9704 | } |
| 9705 | |
| 9706 | _SOKOL_PRIVATE void _sapp_glx_init(void) { |
| 9707 | const char* sonames[] = { "libGL.so.1", "libGL.so", 0 }; |
| 9708 | for (int i = 0; sonames[i]; i++) { |
| 9709 | _sapp.glx.libgl = dlopen(sonames[i], RTLD_LAZY|RTLD_GLOBAL); |
| 9710 | if (_sapp.glx.libgl) { |
| 9711 | break; |
| 9712 | } |
| 9713 | } |
| 9714 | if (!_sapp.glx.libgl) { |
| 9715 | _SAPP_PANIC(LINUX_GLX_LOAD_LIBGL_FAILED); |
| 9716 | } |
| 9717 | _sapp.glx.GetFBConfigs = (PFNGLXGETFBCONFIGSPROC) dlsym(_sapp.glx.libgl, "glXGetFBConfigs"); |
| 9718 | _sapp.glx.GetFBConfigAttrib = (PFNGLXGETFBCONFIGATTRIBPROC) dlsym(_sapp.glx.libgl, "glXGetFBConfigAttrib"); |
| 9719 | _sapp.glx.GetClientString = (PFNGLXGETCLIENTSTRINGPROC) dlsym(_sapp.glx.libgl, "glXGetClientString"); |
| 9720 | _sapp.glx.QueryExtension = (PFNGLXQUERYEXTENSIONPROC) dlsym(_sapp.glx.libgl, "glXQueryExtension"); |
| 9721 | _sapp.glx.QueryVersion = (PFNGLXQUERYVERSIONPROC) dlsym(_sapp.glx.libgl, "glXQueryVersion"); |
| 9722 | _sapp.glx.DestroyContext = (PFNGLXDESTROYCONTEXTPROC) dlsym(_sapp.glx.libgl, "glXDestroyContext"); |
| 9723 | _sapp.glx.MakeCurrent = (PFNGLXMAKECURRENTPROC) dlsym(_sapp.glx.libgl, "glXMakeCurrent"); |
| 9724 | _sapp.glx.SwapBuffers = (PFNGLXSWAPBUFFERSPROC) dlsym(_sapp.glx.libgl, "glXSwapBuffers"); |
| 9725 | _sapp.glx.QueryExtensionsString = (PFNGLXQUERYEXTENSIONSSTRINGPROC) dlsym(_sapp.glx.libgl, "glXQueryExtensionsString"); |
| 9726 | _sapp.glx.CreateWindow = (PFNGLXCREATEWINDOWPROC) dlsym(_sapp.glx.libgl, "glXCreateWindow"); |
| 9727 | _sapp.glx.DestroyWindow = (PFNGLXDESTROYWINDOWPROC) dlsym(_sapp.glx.libgl, "glXDestroyWindow"); |
| 9728 | _sapp.glx.GetProcAddress = (PFNGLXGETPROCADDRESSPROC) dlsym(_sapp.glx.libgl, "glXGetProcAddress"); |
| 9729 | _sapp.glx.GetProcAddressARB = (PFNGLXGETPROCADDRESSPROC) dlsym(_sapp.glx.libgl, "glXGetProcAddressARB"); |
| 9730 | _sapp.glx.GetVisualFromFBConfig = (PFNGLXGETVISUALFROMFBCONFIGPROC) dlsym(_sapp.glx.libgl, "glXGetVisualFromFBConfig"); |
| 9731 | if (!_sapp.glx.GetFBConfigs || |
| 9732 | !_sapp.glx.GetFBConfigAttrib || |
| 9733 | !_sapp.glx.GetClientString || |
| 9734 | !_sapp.glx.QueryExtension || |
| 9735 | !_sapp.glx.QueryVersion || |
| 9736 | !_sapp.glx.DestroyContext || |
| 9737 | !_sapp.glx.MakeCurrent || |
| 9738 | !_sapp.glx.SwapBuffers || |
| 9739 | !_sapp.glx.QueryExtensionsString || |
| 9740 | !_sapp.glx.CreateWindow || |
| 9741 | !_sapp.glx.DestroyWindow || |
| 9742 | !_sapp.glx.GetProcAddress || |
| 9743 | !_sapp.glx.GetProcAddressARB || |
| 9744 | !_sapp.glx.GetVisualFromFBConfig) |
| 9745 | { |
| 9746 | _SAPP_PANIC(LINUX_GLX_LOAD_ENTRY_POINTS_FAILED); |
| 9747 | } |
| 9748 | |
| 9749 | if (!_sapp.glx.QueryExtension(_sapp.x11.display, &_sapp.glx.error_base, &_sapp.glx.event_base)) { |
| 9750 | _SAPP_PANIC(LINUX_GLX_EXTENSION_NOT_FOUND); |
| 9751 | } |
| 9752 | if (!_sapp.glx.QueryVersion(_sapp.x11.display, &_sapp.glx.major, &_sapp.glx.minor)) { |
| 9753 | _SAPP_PANIC(LINUX_GLX_QUERY_VERSION_FAILED); |
| 9754 | } |
| 9755 | if (_sapp.glx.major == 1 && _sapp.glx.minor < 3) { |
| 9756 | _SAPP_PANIC(LINUX_GLX_VERSION_TOO_LOW); |
| 9757 | } |
| 9758 | const char* exts = _sapp.glx.QueryExtensionsString(_sapp.x11.display, _sapp.x11.screen); |
| 9759 | if (_sapp_glx_extsupported("GLX_EXT_swap_control", exts)) { |
| 9760 | _sapp.glx.SwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC) _sapp_glx_getprocaddr("glXSwapIntervalEXT"); |
| 9761 | _sapp.glx.EXT_swap_control = 0 != _sapp.glx.SwapIntervalEXT; |
| 9762 | } |
| 9763 | if (_sapp_glx_extsupported("GLX_MESA_swap_control", exts)) { |
| 9764 | _sapp.glx.SwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC) _sapp_glx_getprocaddr("glXSwapIntervalMESA"); |
| 9765 | _sapp.glx.MESA_swap_control = 0 != _sapp.glx.SwapIntervalMESA; |
| 9766 | } |
| 9767 | _sapp.glx.ARB_multisample = _sapp_glx_extsupported("GLX_ARB_multisample", exts); |
| 9768 | if (_sapp_glx_extsupported("GLX_ARB_create_context", exts)) { |
| 9769 | _sapp.glx.CreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC) _sapp_glx_getprocaddr("glXCreateContextAttribsARB"); |
| 9770 | _sapp.glx.ARB_create_context = 0 != _sapp.glx.CreateContextAttribsARB; |
| 9771 | } |
| 9772 | _sapp.glx.ARB_create_context_profile = _sapp_glx_extsupported("GLX_ARB_create_context_profile", exts); |
| 9773 | } |
| 9774 | |
| 9775 | _SOKOL_PRIVATE int _sapp_glx_attrib(GLXFBConfig fbconfig, int attrib) { |
| 9776 | int value; |
| 9777 | _sapp.glx.GetFBConfigAttrib(_sapp.x11.display, fbconfig, attrib, &value); |
| 9778 | return value; |
| 9779 | } |
| 9780 | |
| 9781 | _SOKOL_PRIVATE GLXFBConfig _sapp_glx_choosefbconfig(void) { |
| 9782 | GLXFBConfig* native_configs; |
| 9783 | _sapp_gl_fbconfig* usable_configs; |
| 9784 | const _sapp_gl_fbconfig* closest; |
| 9785 | int i, native_count, usable_count; |
| 9786 | const char* vendor; |
| 9787 | bool trust_window_bit = true; |
| 9788 | |
| 9789 | /* HACK: This is a (hopefully temporary) workaround for Chromium |
| 9790 | (VirtualBox GL) not setting the window bit on any GLXFBConfigs |
| 9791 | */ |
| 9792 | vendor = _sapp.glx.GetClientString(_sapp.x11.display, GLX_VENDOR); |
| 9793 | if (vendor && strcmp(vendor, "Chromium") == 0) { |
| 9794 | trust_window_bit = false; |
| 9795 | } |
| 9796 | |
| 9797 | native_configs = _sapp.glx.GetFBConfigs(_sapp.x11.display, _sapp.x11.screen, &native_count); |
| 9798 | if (!native_configs || !native_count) { |
| 9799 | _SAPP_PANIC(LINUX_GLX_NO_GLXFBCONFIGS); |
| 9800 | } |
| 9801 | |
| 9802 | usable_configs = (_sapp_gl_fbconfig*) _sapp_malloc_clear((size_t)native_count * sizeof(_sapp_gl_fbconfig)); |
| 9803 | usable_count = 0; |
| 9804 | for (i = 0; i < native_count; i++) { |
| 9805 | const GLXFBConfig n = native_configs[i]; |
| 9806 | _sapp_gl_fbconfig* u = usable_configs + usable_count; |
| 9807 | _sapp_gl_init_fbconfig(u); |
| 9808 | |
| 9809 | /* Only consider RGBA GLXFBConfigs */ |
| 9810 | if (0 == (_sapp_glx_attrib(n, GLX_RENDER_TYPE) & GLX_RGBA_BIT)) { |
| 9811 | continue; |
| 9812 | } |
| 9813 | /* Only consider window GLXFBConfigs */ |
| 9814 | if (0 == (_sapp_glx_attrib(n, GLX_DRAWABLE_TYPE) & GLX_WINDOW_BIT)) { |
| 9815 | if (trust_window_bit) { |
| 9816 | continue; |
| 9817 | } |
| 9818 | } |
| 9819 | u->red_bits = _sapp_glx_attrib(n, GLX_RED_SIZE); |
| 9820 | u->green_bits = _sapp_glx_attrib(n, GLX_GREEN_SIZE); |
| 9821 | u->blue_bits = _sapp_glx_attrib(n, GLX_BLUE_SIZE); |
| 9822 | u->alpha_bits = _sapp_glx_attrib(n, GLX_ALPHA_SIZE); |
| 9823 | u->depth_bits = _sapp_glx_attrib(n, GLX_DEPTH_SIZE); |
| 9824 | u->stencil_bits = _sapp_glx_attrib(n, GLX_STENCIL_SIZE); |
| 9825 | if (_sapp_glx_attrib(n, GLX_DOUBLEBUFFER)) { |
| 9826 | u->doublebuffer = true; |
| 9827 | } |
| 9828 | if (_sapp.glx.ARB_multisample) { |
| 9829 | u->samples = _sapp_glx_attrib(n, GLX_SAMPLES); |
| 9830 | } |
| 9831 | u->handle = (uintptr_t) n; |
| 9832 | usable_count++; |
| 9833 | } |
| 9834 | _sapp_gl_fbconfig desired; |
| 9835 | _sapp_gl_init_fbconfig(&desired); |
| 9836 | desired.red_bits = 8; |
| 9837 | desired.green_bits = 8; |
| 9838 | desired.blue_bits = 8; |
| 9839 | desired.alpha_bits = 8; |
| 9840 | desired.depth_bits = 24; |
| 9841 | desired.stencil_bits = 8; |
| 9842 | desired.doublebuffer = true; |
| 9843 | desired.samples = _sapp.sample_count > 1 ? _sapp.sample_count : 0; |
| 9844 | closest = _sapp_gl_choose_fbconfig(&desired, usable_configs, usable_count); |
| 9845 | GLXFBConfig result = 0; |
| 9846 | if (closest) { |
| 9847 | result = (GLXFBConfig) closest->handle; |
| 9848 | } |
| 9849 | XFree(native_configs); |
| 9850 | _sapp_free(usable_configs); |
| 9851 | return result; |
| 9852 | } |
| 9853 | |
| 9854 | _SOKOL_PRIVATE void _sapp_glx_choose_visual(Visual** visual, int* depth) { |
| 9855 | GLXFBConfig native = _sapp_glx_choosefbconfig(); |
| 9856 | if (0 == native) { |
| 9857 | _SAPP_PANIC(LINUX_GLX_NO_SUITABLE_GLXFBCONFIG); |
| 9858 | } |
| 9859 | XVisualInfo* result = _sapp.glx.GetVisualFromFBConfig(_sapp.x11.display, native); |
| 9860 | if (!result) { |
| 9861 | _SAPP_PANIC(LINUX_GLX_GET_VISUAL_FROM_FBCONFIG_FAILED); |
| 9862 | } |
| 9863 | *visual = result->visual; |
| 9864 | *depth = result->depth; |
| 9865 | XFree(result); |
| 9866 | } |
| 9867 | |
| 9868 | _SOKOL_PRIVATE void _sapp_glx_make_current(void) { |
| 9869 | _sapp.glx.MakeCurrent(_sapp.x11.display, _sapp.glx.window, _sapp.glx.ctx); |
| 9870 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer); |
| 9871 | } |
| 9872 | |
| 9873 | _SOKOL_PRIVATE void _sapp_glx_create_context(void) { |
| 9874 | GLXFBConfig native = _sapp_glx_choosefbconfig(); |
| 9875 | if (0 == native){ |
| 9876 | _SAPP_PANIC(LINUX_GLX_NO_SUITABLE_GLXFBCONFIG); |
| 9877 | } |
| 9878 | if (!(_sapp.glx.ARB_create_context && _sapp.glx.ARB_create_context_profile)) { |
| 9879 | _SAPP_PANIC(LINUX_GLX_REQUIRED_EXTENSIONS_MISSING); |
| 9880 | } |
| 9881 | _sapp_x11_grab_error_handler(); |
| 9882 | const int attribs[] = { |
| 9883 | GLX_CONTEXT_MAJOR_VERSION_ARB, _sapp.desc.gl_major_version, |
| 9884 | GLX_CONTEXT_MINOR_VERSION_ARB, _sapp.desc.gl_minor_version, |
| 9885 | GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB, |
| 9886 | GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, |
| 9887 | 0, 0 |
| 9888 | }; |
| 9889 | _sapp.glx.ctx = _sapp.glx.CreateContextAttribsARB(_sapp.x11.display, native, NULL, True, attribs); |
| 9890 | if (!_sapp.glx.ctx) { |
| 9891 | _SAPP_PANIC(LINUX_GLX_CREATE_CONTEXT_FAILED); |
| 9892 | } |
| 9893 | _sapp_x11_release_error_handler(); |
| 9894 | _sapp.glx.window = _sapp.glx.CreateWindow(_sapp.x11.display, native, _sapp.x11.window, NULL); |
| 9895 | if (!_sapp.glx.window) { |
| 9896 | _SAPP_PANIC(LINUX_GLX_CREATE_WINDOW_FAILED); |
| 9897 | } |
| 9898 | _sapp_glx_make_current(); |
| 9899 | } |
| 9900 | |
| 9901 | _SOKOL_PRIVATE void _sapp_glx_destroy_context(void) { |
| 9902 | if (_sapp.glx.window) { |
| 9903 | _sapp.glx.DestroyWindow(_sapp.x11.display, _sapp.glx.window); |
| 9904 | _sapp.glx.window = 0; |
| 9905 | } |
| 9906 | if (_sapp.glx.ctx) { |
| 9907 | _sapp.glx.DestroyContext(_sapp.x11.display, _sapp.glx.ctx); |
| 9908 | _sapp.glx.ctx = 0; |
| 9909 | } |
| 9910 | } |
| 9911 | |
| 9912 | _SOKOL_PRIVATE void _sapp_glx_swap_buffers(void) { |
| 9913 | _sapp.glx.SwapBuffers(_sapp.x11.display, _sapp.glx.window); |
| 9914 | } |
| 9915 | |
| 9916 | _SOKOL_PRIVATE void _sapp_glx_swapinterval(int interval) { |
| 9917 | if (_sapp.glx.EXT_swap_control) { |
| 9918 | _sapp.glx.SwapIntervalEXT(_sapp.x11.display, _sapp.glx.window, interval); |
| 9919 | } |
| 9920 | else if (_sapp.glx.MESA_swap_control) { |
| 9921 | _sapp.glx.SwapIntervalMESA(interval); |
| 9922 | } |
| 9923 | } |
| 9924 | |
| 9925 | #endif /* _SAPP_GLX */ |
| 9926 | |
| 9927 | _SOKOL_PRIVATE void _sapp_x11_send_event(Atom type, int a, int b, int c, int d, int e) { |
| 9928 | XEvent event; |
| 9929 | _sapp_clear(&event, sizeof(event)); |
| 9930 | |
| 9931 | event.type = ClientMessage; |
| 9932 | event.xclient.window = _sapp.x11.window; |
| 9933 | event.xclient.format = 32; |
| 9934 | event.xclient.message_type = type; |
| 9935 | event.xclient.data.l[0] = a; |
| 9936 | event.xclient.data.l[1] = b; |
| 9937 | event.xclient.data.l[2] = c; |
| 9938 | event.xclient.data.l[3] = d; |
| 9939 | event.xclient.data.l[4] = e; |
| 9940 | |
| 9941 | XSendEvent(_sapp.x11.display, _sapp.x11.root, |
| 9942 | False, |
| 9943 | SubstructureNotifyMask | SubstructureRedirectMask, |
| 9944 | &event); |
| 9945 | } |
| 9946 | |
| 9947 | _SOKOL_PRIVATE void _sapp_x11_query_window_size(void) { |
| 9948 | XWindowAttributes attribs; |
| 9949 | XGetWindowAttributes(_sapp.x11.display, _sapp.x11.window, &attribs); |
| 9950 | _sapp.window_width = attribs.width; |
| 9951 | _sapp.window_height = attribs.height; |
| 9952 | _sapp.framebuffer_width = _sapp.window_width; |
| 9953 | _sapp.framebuffer_height = _sapp.window_height; |
| 9954 | } |
| 9955 | |
| 9956 | _SOKOL_PRIVATE void _sapp_x11_set_fullscreen(bool enable) { |
| 9957 | /* NOTE: this function must be called after XMapWindow (which happens in _sapp_x11_show_window()) */ |
| 9958 | if (_sapp.x11.NET_WM_STATE && _sapp.x11.NET_WM_STATE_FULLSCREEN) { |
| 9959 | if (enable) { |
| 9960 | const int _NET_WM_STATE_ADD = 1; |
| 9961 | _sapp_x11_send_event(_sapp.x11.NET_WM_STATE, |
| 9962 | _NET_WM_STATE_ADD, |
| 9963 | _sapp.x11.NET_WM_STATE_FULLSCREEN, |
| 9964 | 0, 1, 0); |
| 9965 | } |
| 9966 | else { |
| 9967 | const int _NET_WM_STATE_REMOVE = 0; |
| 9968 | _sapp_x11_send_event(_sapp.x11.NET_WM_STATE, |
| 9969 | _NET_WM_STATE_REMOVE, |
| 9970 | _sapp.x11.NET_WM_STATE_FULLSCREEN, |
| 9971 | 0, 1, 0); |
| 9972 | } |
| 9973 | } |
| 9974 | XFlush(_sapp.x11.display); |
| 9975 | } |
| 9976 | |
| 9977 | _SOKOL_PRIVATE void _sapp_x11_create_hidden_cursor(void) { |
| 9978 | SOKOL_ASSERT(0 == _sapp.x11.hidden_cursor); |
| 9979 | const int w = 16; |
| 9980 | const int h = 16; |
| 9981 | XcursorImage* img = XcursorImageCreate(w, h); |
| 9982 | SOKOL_ASSERT(img && (img->width == 16) && (img->height == 16) && img->pixels); |
| 9983 | img->xhot = 0; |
| 9984 | img->yhot = 0; |
| 9985 | const size_t num_bytes = (size_t)(w * h) * sizeof(XcursorPixel); |
| 9986 | _sapp_clear(img->pixels, num_bytes); |
| 9987 | _sapp.x11.hidden_cursor = XcursorImageLoadCursor(_sapp.x11.display, img); |
| 9988 | XcursorImageDestroy(img); |
| 9989 | } |
| 9990 | |
| 9991 | _SOKOL_PRIVATE void _sapp_x11_create_standard_cursor(sapp_mouse_cursor cursor, const char* name, const char* theme, int size, uint32_t fallback_native) { |
| 9992 | SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM)); |
| 9993 | SOKOL_ASSERT(_sapp.x11.display); |
| 9994 | if (theme) { |
| 9995 | XcursorImage* img = XcursorLibraryLoadImage(name, theme, size); |
| 9996 | if (img) { |
| 9997 | _sapp.x11.cursors[cursor] = XcursorImageLoadCursor(_sapp.x11.display, img); |
| 9998 | XcursorImageDestroy(img); |
| 9999 | } |
| 10000 | } |
| 10001 | if (0 == _sapp.x11.cursors[cursor]) { |
| 10002 | _sapp.x11.cursors[cursor] = XCreateFontCursor(_sapp.x11.display, fallback_native); |
| 10003 | } |
| 10004 | } |
| 10005 | |
| 10006 | _SOKOL_PRIVATE void _sapp_x11_create_cursors(void) { |
| 10007 | SOKOL_ASSERT(_sapp.x11.display); |
| 10008 | const char* cursor_theme = XcursorGetTheme(_sapp.x11.display); |
| 10009 | const int size = XcursorGetDefaultSize(_sapp.x11.display); |
| 10010 | _sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_ARROW, "default", cursor_theme, size, XC_left_ptr); |
| 10011 | _sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_IBEAM, "text", cursor_theme, size, XC_xterm); |
| 10012 | _sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_CROSSHAIR, "crosshair", cursor_theme, size, XC_crosshair); |
| 10013 | _sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_POINTING_HAND, "pointer", cursor_theme, size, XC_hand2); |
| 10014 | _sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_RESIZE_EW, "ew-resize", cursor_theme, size, XC_sb_h_double_arrow); |
| 10015 | _sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_RESIZE_NS, "ns-resize", cursor_theme, size, XC_sb_v_double_arrow); |
| 10016 | _sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_RESIZE_NWSE, "nwse-resize", cursor_theme, size, 0); |
| 10017 | _sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_RESIZE_NESW, "nesw-resize", cursor_theme, size, 0); |
| 10018 | _sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_RESIZE_ALL, "all-scroll", cursor_theme, size, XC_fleur); |
| 10019 | _sapp_x11_create_standard_cursor(SAPP_MOUSECURSOR_NOT_ALLOWED, "no-allowed", cursor_theme, size, 0); |
| 10020 | _sapp_x11_create_hidden_cursor(); |
| 10021 | } |
| 10022 | |
| 10023 | _SOKOL_PRIVATE void _sapp_x11_destroy_cursors(void) { |
| 10024 | SOKOL_ASSERT(_sapp.x11.display); |
| 10025 | if (_sapp.x11.hidden_cursor) { |
| 10026 | XFreeCursor(_sapp.x11.display, _sapp.x11.hidden_cursor); |
| 10027 | _sapp.x11.hidden_cursor = 0; |
| 10028 | } |
| 10029 | for (int i = 0; i < _SAPP_MOUSECURSOR_NUM; i++) { |
| 10030 | if (_sapp.x11.cursors[i]) { |
| 10031 | XFreeCursor(_sapp.x11.display, _sapp.x11.cursors[i]); |
| 10032 | _sapp.x11.cursors[i] = 0; |
| 10033 | } |
| 10034 | } |
| 10035 | } |
| 10036 | |
| 10037 | _SOKOL_PRIVATE void _sapp_x11_toggle_fullscreen(void) { |
| 10038 | _sapp.fullscreen = !_sapp.fullscreen; |
| 10039 | _sapp_x11_set_fullscreen(_sapp.fullscreen); |
| 10040 | _sapp_x11_query_window_size(); |
| 10041 | } |
| 10042 | |
| 10043 | _SOKOL_PRIVATE void _sapp_x11_update_cursor(sapp_mouse_cursor cursor, bool shown) { |
| 10044 | SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM)); |
| 10045 | if (shown) { |
| 10046 | if (_sapp.x11.cursors[cursor]) { |
| 10047 | XDefineCursor(_sapp.x11.display, _sapp.x11.window, _sapp.x11.cursors[cursor]); |
| 10048 | } |
| 10049 | else { |
| 10050 | XUndefineCursor(_sapp.x11.display, _sapp.x11.window); |
| 10051 | } |
| 10052 | } |
| 10053 | else { |
| 10054 | XDefineCursor(_sapp.x11.display, _sapp.x11.window, _sapp.x11.hidden_cursor); |
| 10055 | } |
| 10056 | XFlush(_sapp.x11.display); |
| 10057 | } |
| 10058 | |
| 10059 | _SOKOL_PRIVATE void _sapp_x11_lock_mouse(bool lock) { |
| 10060 | if (lock == _sapp.mouse.locked) { |
| 10061 | return; |
| 10062 | } |
| 10063 | _sapp.mouse.dx = 0.0f; |
| 10064 | _sapp.mouse.dy = 0.0f; |
| 10065 | _sapp.mouse.locked = lock; |
| 10066 | if (_sapp.mouse.locked) { |
| 10067 | if (_sapp.x11.xi.available) { |
| 10068 | XIEventMask em; |
| 10069 | unsigned char mask[XIMaskLen(XI_RawMotion)] = { 0 }; // XIMaskLen is a macro |
| 10070 | em.deviceid = XIAllMasterDevices; |
| 10071 | em.mask_len = sizeof(mask); |
| 10072 | em.mask = mask; |
| 10073 | XISetMask(mask, XI_RawMotion); |
| 10074 | XISelectEvents(_sapp.x11.display, _sapp.x11.root, &em, 1); |
| 10075 | } |
| 10076 | XGrabPointer(_sapp.x11.display, // display |
| 10077 | _sapp.x11.window, // grab_window |
| 10078 | True, // owner_events |
| 10079 | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, // event_mask |
| 10080 | GrabModeAsync, // pointer_mode |
| 10081 | GrabModeAsync, // keyboard_mode |
| 10082 | _sapp.x11.window, // confine_to |
| 10083 | _sapp.x11.hidden_cursor, // cursor |
| 10084 | CurrentTime); // time |
| 10085 | } |
| 10086 | else { |
| 10087 | if (_sapp.x11.xi.available) { |
| 10088 | XIEventMask em; |
| 10089 | unsigned char mask[] = { 0 }; |
| 10090 | em.deviceid = XIAllMasterDevices; |
| 10091 | em.mask_len = sizeof(mask); |
| 10092 | em.mask = mask; |
| 10093 | XISelectEvents(_sapp.x11.display, _sapp.x11.root, &em, 1); |
| 10094 | } |
| 10095 | XWarpPointer(_sapp.x11.display, None, _sapp.x11.window, 0, 0, 0, 0, (int) _sapp.mouse.x, _sapp.mouse.y); |
| 10096 | XUngrabPointer(_sapp.x11.display, CurrentTime); |
| 10097 | } |
| 10098 | XFlush(_sapp.x11.display); |
| 10099 | } |
| 10100 | |
| 10101 | _SOKOL_PRIVATE void _sapp_x11_update_window_title(void) { |
| 10102 | Xutf8SetWMProperties(_sapp.x11.display, |
| 10103 | _sapp.x11.window, |
| 10104 | _sapp.window_title, _sapp.window_title, |
| 10105 | NULL, 0, NULL, NULL, NULL); |
| 10106 | XChangeProperty(_sapp.x11.display, _sapp.x11.window, |
| 10107 | _sapp.x11.NET_WM_NAME, _sapp.x11.UTF8_STRING, 8, |
| 10108 | PropModeReplace, |
| 10109 | (unsigned char*)_sapp.window_title, |
| 10110 | strlen(_sapp.window_title)); |
| 10111 | XChangeProperty(_sapp.x11.display, _sapp.x11.window, |
| 10112 | _sapp.x11.NET_WM_ICON_NAME, _sapp.x11.UTF8_STRING, 8, |
| 10113 | PropModeReplace, |
| 10114 | (unsigned char*)_sapp.window_title, |
| 10115 | strlen(_sapp.window_title)); |
| 10116 | XFlush(_sapp.x11.display); |
| 10117 | } |
| 10118 | |
| 10119 | _SOKOL_PRIVATE void _sapp_x11_set_icon(const sapp_icon_desc* icon_desc, int num_images) { |
| 10120 | SOKOL_ASSERT((num_images > 0) && (num_images <= SAPP_MAX_ICONIMAGES)); |
| 10121 | int long_count = 0; |
| 10122 | for (int i = 0; i < num_images; i++) { |
| 10123 | const sapp_image_desc* img_desc = &icon_desc->images[i]; |
| 10124 | long_count += 2 + (img_desc->width * img_desc->height); |
| 10125 | } |
| 10126 | long* icon_data = (long*) _sapp_malloc_clear((size_t)long_count * sizeof(long)); |
| 10127 | SOKOL_ASSERT(icon_data); |
| 10128 | long* dst = icon_data; |
| 10129 | for (int img_index = 0; img_index < num_images; img_index++) { |
| 10130 | const sapp_image_desc* img_desc = &icon_desc->images[img_index]; |
| 10131 | const uint8_t* src = (const uint8_t*) img_desc->pixels.ptr; |
| 10132 | *dst++ = img_desc->width; |
| 10133 | *dst++ = img_desc->height; |
| 10134 | const int num_pixels = img_desc->width * img_desc->height; |
| 10135 | for (int pixel_index = 0; pixel_index < num_pixels; pixel_index++) { |
| 10136 | *dst++ = ((long)(src[pixel_index * 4 + 0]) << 16) | |
| 10137 | ((long)(src[pixel_index * 4 + 1]) << 8) | |
| 10138 | ((long)(src[pixel_index * 4 + 2]) << 0) | |
| 10139 | ((long)(src[pixel_index * 4 + 3]) << 24); |
| 10140 | } |
| 10141 | } |
| 10142 | XChangeProperty(_sapp.x11.display, _sapp.x11.window, |
| 10143 | _sapp.x11.NET_WM_ICON, |
| 10144 | XA_CARDINAL, 32, |
| 10145 | PropModeReplace, |
| 10146 | (unsigned char*)icon_data, |
| 10147 | long_count); |
| 10148 | _sapp_free(icon_data); |
| 10149 | XFlush(_sapp.x11.display); |
| 10150 | } |
| 10151 | |
| 10152 | _SOKOL_PRIVATE void _sapp_x11_create_window(Visual* visual, int depth) { |
| 10153 | _sapp.x11.colormap = XCreateColormap(_sapp.x11.display, _sapp.x11.root, visual, AllocNone); |
| 10154 | XSetWindowAttributes wa; |
| 10155 | _sapp_clear(&wa, sizeof(wa)); |
| 10156 | const uint32_t wamask = CWBorderPixel | CWColormap | CWEventMask; |
| 10157 | wa.colormap = _sapp.x11.colormap; |
| 10158 | wa.border_pixel = 0; |
| 10159 | wa.event_mask = StructureNotifyMask | KeyPressMask | KeyReleaseMask | |
| 10160 | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | |
| 10161 | ExposureMask | FocusChangeMask | VisibilityChangeMask | |
| 10162 | EnterWindowMask | LeaveWindowMask | PropertyChangeMask; |
| 10163 | |
| 10164 | int display_width = DisplayWidth(_sapp.x11.display, _sapp.x11.screen); |
| 10165 | int display_height = DisplayHeight(_sapp.x11.display, _sapp.x11.screen); |
| 10166 | int window_width = _sapp.window_width; |
| 10167 | int window_height = _sapp.window_height; |
| 10168 | if (0 == window_width) { |
| 10169 | window_width = (display_width * 4) / 5; |
| 10170 | } |
| 10171 | if (0 == window_height) { |
| 10172 | window_height = (display_height * 4) / 5; |
| 10173 | } |
| 10174 | int window_xpos = (display_width - window_width) / 2; |
| 10175 | int window_ypos = (display_height - window_height) / 2; |
| 10176 | if (window_xpos < 0) { |
| 10177 | window_xpos = 0; |
| 10178 | } |
| 10179 | if (window_ypos < 0) { |
| 10180 | window_ypos = 0; |
| 10181 | } |
| 10182 | _sapp_x11_grab_error_handler(); |
| 10183 | _sapp.x11.window = XCreateWindow(_sapp.x11.display, |
| 10184 | _sapp.x11.root, |
| 10185 | window_xpos, |
| 10186 | window_ypos, |
| 10187 | (uint32_t)window_width, |
| 10188 | (uint32_t)window_height, |
| 10189 | 0, /* border width */ |
| 10190 | depth, /* color depth */ |
| 10191 | InputOutput, |
| 10192 | visual, |
| 10193 | wamask, |
| 10194 | &wa); |
| 10195 | _sapp_x11_release_error_handler(); |
| 10196 | if (!_sapp.x11.window) { |
| 10197 | _SAPP_PANIC(LINUX_X11_CREATE_WINDOW_FAILED); |
| 10198 | } |
| 10199 | Atom protocols[] = { |
| 10200 | _sapp.x11.WM_DELETE_WINDOW |
| 10201 | }; |
| 10202 | XSetWMProtocols(_sapp.x11.display, _sapp.x11.window, protocols, 1); |
| 10203 | |
| 10204 | XSizeHints* hints = XAllocSizeHints(); |
| 10205 | hints->flags = (PWinGravity | PPosition | PSize); |
| 10206 | hints->win_gravity = StaticGravity; |
| 10207 | hints->x = window_xpos; |
| 10208 | hints->y = window_ypos; |
| 10209 | hints->width = window_width; |
| 10210 | hints->height = window_height; |
| 10211 | XSetWMNormalHints(_sapp.x11.display, _sapp.x11.window, hints); |
| 10212 | XFree(hints); |
| 10213 | |
| 10214 | /* announce support for drag'n'drop */ |
| 10215 | if (_sapp.drop.enabled) { |
| 10216 | const Atom version = _SAPP_X11_XDND_VERSION; |
| 10217 | XChangeProperty(_sapp.x11.display, _sapp.x11.window, _sapp.x11.xdnd.XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*) &version, 1); |
| 10218 | } |
| 10219 | _sapp_x11_update_window_title(); |
| 10220 | _sapp_x11_query_window_size(); |
| 10221 | } |
| 10222 | |
| 10223 | _SOKOL_PRIVATE void _sapp_x11_destroy_window(void) { |
| 10224 | if (_sapp.x11.window) { |
| 10225 | XUnmapWindow(_sapp.x11.display, _sapp.x11.window); |
| 10226 | XDestroyWindow(_sapp.x11.display, _sapp.x11.window); |
| 10227 | _sapp.x11.window = 0; |
| 10228 | } |
| 10229 | if (_sapp.x11.colormap) { |
| 10230 | XFreeColormap(_sapp.x11.display, _sapp.x11.colormap); |
| 10231 | _sapp.x11.colormap = 0; |
| 10232 | } |
| 10233 | XFlush(_sapp.x11.display); |
| 10234 | } |
| 10235 | |
| 10236 | _SOKOL_PRIVATE bool _sapp_x11_window_visible(void) { |
| 10237 | XWindowAttributes wa; |
| 10238 | XGetWindowAttributes(_sapp.x11.display, _sapp.x11.window, &wa); |
| 10239 | return wa.map_state == IsViewable; |
| 10240 | } |
| 10241 | |
| 10242 | _SOKOL_PRIVATE void _sapp_x11_show_window(void) { |
| 10243 | if (!_sapp_x11_window_visible()) { |
| 10244 | XMapWindow(_sapp.x11.display, _sapp.x11.window); |
| 10245 | XRaiseWindow(_sapp.x11.display, _sapp.x11.window); |
| 10246 | XFlush(_sapp.x11.display); |
| 10247 | } |
| 10248 | } |
| 10249 | |
| 10250 | _SOKOL_PRIVATE void _sapp_x11_hide_window(void) { |
| 10251 | XUnmapWindow(_sapp.x11.display, _sapp.x11.window); |
| 10252 | XFlush(_sapp.x11.display); |
| 10253 | } |
| 10254 | |
| 10255 | _SOKOL_PRIVATE unsigned long _sapp_x11_get_window_property(Window window, Atom property, Atom type, unsigned char** value) { |
| 10256 | Atom actualType; |
| 10257 | int actualFormat; |
| 10258 | unsigned long itemCount, bytesAfter; |
| 10259 | XGetWindowProperty(_sapp.x11.display, |
| 10260 | window, |
| 10261 | property, |
| 10262 | 0, |
| 10263 | LONG_MAX, |
| 10264 | False, |
| 10265 | type, |
| 10266 | &actualType, |
| 10267 | &actualFormat, |
| 10268 | &itemCount, |
| 10269 | &bytesAfter, |
| 10270 | value); |
| 10271 | return itemCount; |
| 10272 | } |
| 10273 | |
| 10274 | _SOKOL_PRIVATE int _sapp_x11_get_window_state(void) { |
| 10275 | int result = WithdrawnState; |
| 10276 | struct { |
| 10277 | CARD32 state; |
| 10278 | Window icon; |
| 10279 | } *state = NULL; |
| 10280 | |
| 10281 | if (_sapp_x11_get_window_property(_sapp.x11.window, _sapp.x11.WM_STATE, _sapp.x11.WM_STATE, (unsigned char**)&state) >= 2) { |
| 10282 | result = (int)state->state; |
| 10283 | } |
| 10284 | if (state) { |
| 10285 | XFree(state); |
| 10286 | } |
| 10287 | return result; |
| 10288 | } |
| 10289 | |
| 10290 | _SOKOL_PRIVATE uint32_t _sapp_x11_key_modifier_bit(sapp_keycode key) { |
| 10291 | switch (key) { |
| 10292 | case SAPP_KEYCODE_LEFT_SHIFT: |
| 10293 | case SAPP_KEYCODE_RIGHT_SHIFT: |
| 10294 | return SAPP_MODIFIER_SHIFT; |
| 10295 | case SAPP_KEYCODE_LEFT_CONTROL: |
| 10296 | case SAPP_KEYCODE_RIGHT_CONTROL: |
| 10297 | return SAPP_MODIFIER_CTRL; |
| 10298 | case SAPP_KEYCODE_LEFT_ALT: |
| 10299 | case SAPP_KEYCODE_RIGHT_ALT: |
| 10300 | return SAPP_MODIFIER_ALT; |
| 10301 | case SAPP_KEYCODE_LEFT_SUPER: |
| 10302 | case SAPP_KEYCODE_RIGHT_SUPER: |
| 10303 | return SAPP_MODIFIER_SUPER; |
| 10304 | default: |
| 10305 | return 0; |
| 10306 | } |
| 10307 | } |
| 10308 | |
| 10309 | _SOKOL_PRIVATE uint32_t _sapp_x11_button_modifier_bit(sapp_mousebutton btn) { |
| 10310 | switch (btn) { |
| 10311 | case SAPP_MOUSEBUTTON_LEFT: return SAPP_MODIFIER_LMB; |
| 10312 | case SAPP_MOUSEBUTTON_RIGHT: return SAPP_MODIFIER_RMB; |
| 10313 | case SAPP_MOUSEBUTTON_MIDDLE: return SAPP_MODIFIER_MMB; |
| 10314 | default: return 0; |
| 10315 | } |
| 10316 | } |
| 10317 | |
| 10318 | _SOKOL_PRIVATE uint32_t _sapp_x11_mods(uint32_t x11_mods) { |
| 10319 | uint32_t mods = 0; |
| 10320 | if (x11_mods & ShiftMask) { |
| 10321 | mods |= SAPP_MODIFIER_SHIFT; |
| 10322 | } |
| 10323 | if (x11_mods & ControlMask) { |
| 10324 | mods |= SAPP_MODIFIER_CTRL; |
| 10325 | } |
| 10326 | if (x11_mods & Mod1Mask) { |
| 10327 | mods |= SAPP_MODIFIER_ALT; |
| 10328 | } |
| 10329 | if (x11_mods & Mod4Mask) { |
| 10330 | mods |= SAPP_MODIFIER_SUPER; |
| 10331 | } |
| 10332 | if (x11_mods & Button1Mask) { |
| 10333 | mods |= SAPP_MODIFIER_LMB; |
| 10334 | } |
| 10335 | if (x11_mods & Button2Mask) { |
| 10336 | mods |= SAPP_MODIFIER_MMB; |
| 10337 | } |
| 10338 | if (x11_mods & Button3Mask) { |
| 10339 | mods |= SAPP_MODIFIER_RMB; |
| 10340 | } |
| 10341 | return mods; |
| 10342 | } |
| 10343 | |
| 10344 | _SOKOL_PRIVATE void _sapp_x11_app_event(sapp_event_type type) { |
| 10345 | if (_sapp_events_enabled()) { |
| 10346 | _sapp_init_event(type); |
| 10347 | _sapp_call_event(&_sapp.event); |
| 10348 | } |
| 10349 | } |
| 10350 | |
| 10351 | _SOKOL_PRIVATE sapp_mousebutton _sapp_x11_translate_button(const XEvent* event) { |
| 10352 | switch (event->xbutton.button) { |
| 10353 | case Button1: return SAPP_MOUSEBUTTON_LEFT; |
| 10354 | case Button2: return SAPP_MOUSEBUTTON_MIDDLE; |
| 10355 | case Button3: return SAPP_MOUSEBUTTON_RIGHT; |
| 10356 | default: return SAPP_MOUSEBUTTON_INVALID; |
| 10357 | } |
| 10358 | } |
| 10359 | |
| 10360 | _SOKOL_PRIVATE void _sapp_x11_mouse_update(int x, int y, bool clear_dxdy) { |
| 10361 | if (!_sapp.mouse.locked) { |
| 10362 | const float new_x = (float) x; |
| 10363 | const float new_y = (float) y; |
| 10364 | if (clear_dxdy) { |
| 10365 | _sapp.mouse.dx = 0.0f; |
| 10366 | _sapp.mouse.dy = 0.0f; |
| 10367 | } else if (_sapp.mouse.pos_valid) { |
| 10368 | _sapp.mouse.dx = new_x - _sapp.mouse.x; |
| 10369 | _sapp.mouse.dy = new_y - _sapp.mouse.y; |
| 10370 | } |
| 10371 | _sapp.mouse.x = new_x; |
| 10372 | _sapp.mouse.y = new_y; |
| 10373 | _sapp.mouse.pos_valid = true; |
| 10374 | } |
| 10375 | } |
| 10376 | |
| 10377 | _SOKOL_PRIVATE void _sapp_x11_mouse_event(sapp_event_type type, sapp_mousebutton btn, uint32_t mods) { |
| 10378 | if (_sapp_events_enabled()) { |
| 10379 | _sapp_init_event(type); |
| 10380 | _sapp.event.mouse_button = btn; |
| 10381 | _sapp.event.modifiers = mods; |
| 10382 | _sapp_call_event(&_sapp.event); |
| 10383 | } |
| 10384 | } |
| 10385 | |
| 10386 | _SOKOL_PRIVATE void _sapp_x11_scroll_event(float x, float y, uint32_t mods) { |
| 10387 | if (_sapp_events_enabled()) { |
| 10388 | _sapp_init_event(SAPP_EVENTTYPE_MOUSE_SCROLL); |
| 10389 | _sapp.event.modifiers = mods; |
| 10390 | _sapp.event.scroll_x = x; |
| 10391 | _sapp.event.scroll_y = y; |
| 10392 | _sapp_call_event(&_sapp.event); |
| 10393 | } |
| 10394 | } |
| 10395 | |
| 10396 | _SOKOL_PRIVATE void _sapp_x11_key_event(sapp_event_type type, sapp_keycode key, bool repeat, uint32_t mods) { |
| 10397 | if (_sapp_events_enabled()) { |
| 10398 | _sapp_init_event(type); |
| 10399 | _sapp.event.key_code = key; |
| 10400 | _sapp.event.key_repeat = repeat; |
| 10401 | _sapp.event.modifiers = mods; |
| 10402 | _sapp_call_event(&_sapp.event); |
| 10403 | /* check if a CLIPBOARD_PASTED event must be sent too */ |
| 10404 | if (_sapp.clipboard.enabled && |
| 10405 | (type == SAPP_EVENTTYPE_KEY_DOWN) && |
| 10406 | (_sapp.event.modifiers == SAPP_MODIFIER_CTRL) && |
| 10407 | (_sapp.event.key_code == SAPP_KEYCODE_V)) |
| 10408 | { |
| 10409 | _sapp_init_event(SAPP_EVENTTYPE_CLIPBOARD_PASTED); |
| 10410 | _sapp_call_event(&_sapp.event); |
| 10411 | } |
| 10412 | } |
| 10413 | } |
| 10414 | |
| 10415 | _SOKOL_PRIVATE void _sapp_x11_char_event(uint32_t chr, bool repeat, uint32_t mods) { |
| 10416 | if (_sapp_events_enabled()) { |
| 10417 | _sapp_init_event(SAPP_EVENTTYPE_CHAR); |
| 10418 | _sapp.event.char_code = chr; |
| 10419 | _sapp.event.key_repeat = repeat; |
| 10420 | _sapp.event.modifiers = mods; |
| 10421 | _sapp_call_event(&_sapp.event); |
| 10422 | } |
| 10423 | } |
| 10424 | |
| 10425 | _SOKOL_PRIVATE sapp_keycode _sapp_x11_translate_key(int scancode) { |
| 10426 | int dummy; |
| 10427 | KeySym* keysyms = XGetKeyboardMapping(_sapp.x11.display, scancode, 1, &dummy); |
| 10428 | SOKOL_ASSERT(keysyms); |
| 10429 | KeySym keysym = keysyms[0]; |
| 10430 | XFree(keysyms); |
| 10431 | switch (keysym) { |
| 10432 | case XK_Escape: return SAPP_KEYCODE_ESCAPE; |
| 10433 | case XK_Tab: return SAPP_KEYCODE_TAB; |
| 10434 | case XK_Shift_L: return SAPP_KEYCODE_LEFT_SHIFT; |
| 10435 | case XK_Shift_R: return SAPP_KEYCODE_RIGHT_SHIFT; |
| 10436 | case XK_Control_L: return SAPP_KEYCODE_LEFT_CONTROL; |
| 10437 | case XK_Control_R: return SAPP_KEYCODE_RIGHT_CONTROL; |
| 10438 | case XK_Meta_L: |
| 10439 | case XK_Alt_L: return SAPP_KEYCODE_LEFT_ALT; |
| 10440 | case XK_Mode_switch: /* Mapped to Alt_R on many keyboards */ |
| 10441 | case XK_ISO_Level3_Shift: /* AltGr on at least some machines */ |
| 10442 | case XK_Meta_R: |
| 10443 | case XK_Alt_R: return SAPP_KEYCODE_RIGHT_ALT; |
| 10444 | case XK_Super_L: return SAPP_KEYCODE_LEFT_SUPER; |
| 10445 | case XK_Super_R: return SAPP_KEYCODE_RIGHT_SUPER; |
| 10446 | case XK_Menu: return SAPP_KEYCODE_MENU; |
| 10447 | case XK_Num_Lock: return SAPP_KEYCODE_NUM_LOCK; |
| 10448 | case XK_Caps_Lock: return SAPP_KEYCODE_CAPS_LOCK; |
| 10449 | case XK_Print: return SAPP_KEYCODE_PRINT_SCREEN; |
| 10450 | case XK_Scroll_Lock: return SAPP_KEYCODE_SCROLL_LOCK; |
| 10451 | case XK_Pause: return SAPP_KEYCODE_PAUSE; |
| 10452 | case XK_Delete: return SAPP_KEYCODE_DELETE; |
| 10453 | case XK_BackSpace: return SAPP_KEYCODE_BACKSPACE; |
| 10454 | case XK_Return: return SAPP_KEYCODE_ENTER; |
| 10455 | case XK_Home: return SAPP_KEYCODE_HOME; |
| 10456 | case XK_End: return SAPP_KEYCODE_END; |
| 10457 | case XK_Page_Up: return SAPP_KEYCODE_PAGE_UP; |
| 10458 | case XK_Page_Down: return SAPP_KEYCODE_PAGE_DOWN; |
| 10459 | case XK_Insert: return SAPP_KEYCODE_INSERT; |
| 10460 | case XK_Left: return SAPP_KEYCODE_LEFT; |
| 10461 | case XK_Right: return SAPP_KEYCODE_RIGHT; |
| 10462 | case XK_Down: return SAPP_KEYCODE_DOWN; |
| 10463 | case XK_Up: return SAPP_KEYCODE_UP; |
| 10464 | case XK_F1: return SAPP_KEYCODE_F1; |
| 10465 | case XK_F2: return SAPP_KEYCODE_F2; |
| 10466 | case XK_F3: return SAPP_KEYCODE_F3; |
| 10467 | case XK_F4: return SAPP_KEYCODE_F4; |
| 10468 | case XK_F5: return SAPP_KEYCODE_F5; |
| 10469 | case XK_F6: return SAPP_KEYCODE_F6; |
| 10470 | case XK_F7: return SAPP_KEYCODE_F7; |
| 10471 | case XK_F8: return SAPP_KEYCODE_F8; |
| 10472 | case XK_F9: return SAPP_KEYCODE_F9; |
| 10473 | case XK_F10: return SAPP_KEYCODE_F10; |
| 10474 | case XK_F11: return SAPP_KEYCODE_F11; |
| 10475 | case XK_F12: return SAPP_KEYCODE_F12; |
| 10476 | case XK_F13: return SAPP_KEYCODE_F13; |
| 10477 | case XK_F14: return SAPP_KEYCODE_F14; |
| 10478 | case XK_F15: return SAPP_KEYCODE_F15; |
| 10479 | case XK_F16: return SAPP_KEYCODE_F16; |
| 10480 | case XK_F17: return SAPP_KEYCODE_F17; |
| 10481 | case XK_F18: return SAPP_KEYCODE_F18; |
| 10482 | case XK_F19: return SAPP_KEYCODE_F19; |
| 10483 | case XK_F20: return SAPP_KEYCODE_F20; |
| 10484 | case XK_F21: return SAPP_KEYCODE_F21; |
| 10485 | case XK_F22: return SAPP_KEYCODE_F22; |
| 10486 | case XK_F23: return SAPP_KEYCODE_F23; |
| 10487 | case XK_F24: return SAPP_KEYCODE_F24; |
| 10488 | case XK_F25: return SAPP_KEYCODE_F25; |
| 10489 | |
| 10490 | case XK_KP_Divide: return SAPP_KEYCODE_KP_DIVIDE; |
| 10491 | case XK_KP_Multiply: return SAPP_KEYCODE_KP_MULTIPLY; |
| 10492 | case XK_KP_Subtract: return SAPP_KEYCODE_KP_SUBTRACT; |
| 10493 | case XK_KP_Add: return SAPP_KEYCODE_KP_ADD; |
| 10494 | |
| 10495 | case XK_KP_Insert: return SAPP_KEYCODE_KP_0; |
| 10496 | case XK_KP_End: return SAPP_KEYCODE_KP_1; |
| 10497 | case XK_KP_Down: return SAPP_KEYCODE_KP_2; |
| 10498 | case XK_KP_Page_Down: return SAPP_KEYCODE_KP_3; |
| 10499 | case XK_KP_Left: return SAPP_KEYCODE_KP_4; |
| 10500 | case XK_KP_Begin: return SAPP_KEYCODE_KP_5; |
| 10501 | case XK_KP_Right: return SAPP_KEYCODE_KP_6; |
| 10502 | case XK_KP_Home: return SAPP_KEYCODE_KP_7; |
| 10503 | case XK_KP_Up: return SAPP_KEYCODE_KP_8; |
| 10504 | case XK_KP_Page_Up: return SAPP_KEYCODE_KP_9; |
| 10505 | case XK_KP_Delete: return SAPP_KEYCODE_KP_DECIMAL; |
| 10506 | case XK_KP_Equal: return SAPP_KEYCODE_KP_EQUAL; |
| 10507 | case XK_KP_Enter: return SAPP_KEYCODE_KP_ENTER; |
| 10508 | |
| 10509 | case XK_a: return SAPP_KEYCODE_A; |
| 10510 | case XK_b: return SAPP_KEYCODE_B; |
| 10511 | case XK_c: return SAPP_KEYCODE_C; |
| 10512 | case XK_d: return SAPP_KEYCODE_D; |
| 10513 | case XK_e: return SAPP_KEYCODE_E; |
| 10514 | case XK_f: return SAPP_KEYCODE_F; |
| 10515 | case XK_g: return SAPP_KEYCODE_G; |
| 10516 | case XK_h: return SAPP_KEYCODE_H; |
| 10517 | case XK_i: return SAPP_KEYCODE_I; |
| 10518 | case XK_j: return SAPP_KEYCODE_J; |
| 10519 | case XK_k: return SAPP_KEYCODE_K; |
| 10520 | case XK_l: return SAPP_KEYCODE_L; |
| 10521 | case XK_m: return SAPP_KEYCODE_M; |
| 10522 | case XK_n: return SAPP_KEYCODE_N; |
| 10523 | case XK_o: return SAPP_KEYCODE_O; |
| 10524 | case XK_p: return SAPP_KEYCODE_P; |
| 10525 | case XK_q: return SAPP_KEYCODE_Q; |
| 10526 | case XK_r: return SAPP_KEYCODE_R; |
| 10527 | case XK_s: return SAPP_KEYCODE_S; |
| 10528 | case XK_t: return SAPP_KEYCODE_T; |
| 10529 | case XK_u: return SAPP_KEYCODE_U; |
| 10530 | case XK_v: return SAPP_KEYCODE_V; |
| 10531 | case XK_w: return SAPP_KEYCODE_W; |
| 10532 | case XK_x: return SAPP_KEYCODE_X; |
| 10533 | case XK_y: return SAPP_KEYCODE_Y; |
| 10534 | case XK_z: return SAPP_KEYCODE_Z; |
| 10535 | case XK_1: return SAPP_KEYCODE_1; |
| 10536 | case XK_2: return SAPP_KEYCODE_2; |
| 10537 | case XK_3: return SAPP_KEYCODE_3; |
| 10538 | case XK_4: return SAPP_KEYCODE_4; |
| 10539 | case XK_5: return SAPP_KEYCODE_5; |
| 10540 | case XK_6: return SAPP_KEYCODE_6; |
| 10541 | case XK_7: return SAPP_KEYCODE_7; |
| 10542 | case XK_8: return SAPP_KEYCODE_8; |
| 10543 | case XK_9: return SAPP_KEYCODE_9; |
| 10544 | case XK_0: return SAPP_KEYCODE_0; |
| 10545 | case XK_space: return SAPP_KEYCODE_SPACE; |
| 10546 | case XK_minus: return SAPP_KEYCODE_MINUS; |
| 10547 | case XK_equal: return SAPP_KEYCODE_EQUAL; |
| 10548 | case XK_bracketleft: return SAPP_KEYCODE_LEFT_BRACKET; |
| 10549 | case XK_bracketright: return SAPP_KEYCODE_RIGHT_BRACKET; |
| 10550 | case XK_backslash: return SAPP_KEYCODE_BACKSLASH; |
| 10551 | case XK_semicolon: return SAPP_KEYCODE_SEMICOLON; |
| 10552 | case XK_apostrophe: return SAPP_KEYCODE_APOSTROPHE; |
| 10553 | case XK_grave: return SAPP_KEYCODE_GRAVE_ACCENT; |
| 10554 | case XK_comma: return SAPP_KEYCODE_COMMA; |
| 10555 | case XK_period: return SAPP_KEYCODE_PERIOD; |
| 10556 | case XK_slash: return SAPP_KEYCODE_SLASH; |
| 10557 | case XK_less: return SAPP_KEYCODE_WORLD_1; /* At least in some layouts... */ |
| 10558 | default: return SAPP_KEYCODE_INVALID; |
| 10559 | } |
| 10560 | } |
| 10561 | |
| 10562 | _SOKOL_PRIVATE int32_t _sapp_x11_keysym_to_unicode(KeySym keysym) { |
| 10563 | int min = 0; |
| 10564 | int max = sizeof(_sapp_x11_keysymtab) / sizeof(struct _sapp_x11_codepair) - 1; |
| 10565 | int mid; |
| 10566 | |
| 10567 | /* First check for Latin-1 characters (1:1 mapping) */ |
| 10568 | if ((keysym >= 0x0020 && keysym <= 0x007e) || |
| 10569 | (keysym >= 0x00a0 && keysym <= 0x00ff)) |
| 10570 | { |
| 10571 | return keysym; |
| 10572 | } |
| 10573 | |
| 10574 | /* Also check for directly encoded 24-bit UCS characters */ |
| 10575 | if ((keysym & 0xff000000) == 0x01000000) { |
| 10576 | return keysym & 0x00ffffff; |
| 10577 | } |
| 10578 | |
| 10579 | /* Binary search in table */ |
| 10580 | while (max >= min) { |
| 10581 | mid = (min + max) / 2; |
| 10582 | if (_sapp_x11_keysymtab[mid].keysym < keysym) { |
| 10583 | min = mid + 1; |
| 10584 | } |
| 10585 | else if (_sapp_x11_keysymtab[mid].keysym > keysym) { |
| 10586 | max = mid - 1; |
| 10587 | } |
| 10588 | else { |
| 10589 | return _sapp_x11_keysymtab[mid].ucs; |
| 10590 | } |
| 10591 | } |
| 10592 | |
| 10593 | /* No matching Unicode value found */ |
| 10594 | return -1; |
| 10595 | } |
| 10596 | |
| 10597 | _SOKOL_PRIVATE bool _sapp_x11_parse_dropped_files_list(const char* src) { |
| 10598 | SOKOL_ASSERT(src); |
| 10599 | SOKOL_ASSERT(_sapp.drop.buffer); |
| 10600 | |
| 10601 | _sapp_clear_drop_buffer(); |
| 10602 | _sapp.drop.num_files = 0; |
| 10603 | |
| 10604 | /* |
| 10605 | src is (potentially percent-encoded) string made of one or multiple paths |
| 10606 | separated by \r\n, each path starting with 'file://' |
| 10607 | */ |
| 10608 | bool err = false; |
| 10609 | int src_count = 0; |
| 10610 | char src_chr = 0; |
| 10611 | char* dst_ptr = _sapp.drop.buffer; |
| 10612 | const char* dst_end_ptr = dst_ptr + (_sapp.drop.max_path_length - 1); // room for terminating 0 |
| 10613 | while (0 != (src_chr = *src++)) { |
| 10614 | src_count++; |
| 10615 | char dst_chr = 0; |
| 10616 | /* check leading 'file://' */ |
| 10617 | if (src_count <= 7) { |
| 10618 | if (((src_count == 1) && (src_chr != 'f')) || |
| 10619 | ((src_count == 2) && (src_chr != 'i')) || |
| 10620 | ((src_count == 3) && (src_chr != 'l')) || |
| 10621 | ((src_count == 4) && (src_chr != 'e')) || |
| 10622 | ((src_count == 5) && (src_chr != ':')) || |
| 10623 | ((src_count == 6) && (src_chr != '/')) || |
| 10624 | ((src_count == 7) && (src_chr != '/'))) |
| 10625 | { |
| 10626 | _SAPP_ERROR(LINUX_X11_DROPPED_FILE_URI_WRONG_SCHEME); |
| 10627 | err = true; |
| 10628 | break; |
| 10629 | } |
| 10630 | } |
| 10631 | else if (src_chr == '\r') { |
| 10632 | // skip |
| 10633 | } |
| 10634 | else if (src_chr == '\n') { |
| 10635 | src_count = 0; |
| 10636 | _sapp.drop.num_files++; |
| 10637 | // too many files is not an error |
| 10638 | if (_sapp.drop.num_files >= _sapp.drop.max_files) { |
| 10639 | break; |
| 10640 | } |
| 10641 | dst_ptr = _sapp.drop.buffer + _sapp.drop.num_files * _sapp.drop.max_path_length; |
| 10642 | dst_end_ptr = dst_ptr + (_sapp.drop.max_path_length - 1); |
| 10643 | } |
| 10644 | else if ((src_chr == '%') && src[0] && src[1]) { |
| 10645 | // a percent-encoded byte (most likely UTF-8 multibyte sequence) |
| 10646 | const char digits[3] = { src[0], src[1], 0 }; |
| 10647 | src += 2; |
| 10648 | dst_chr = (char) strtol(digits, 0, 16); |
| 10649 | } |
| 10650 | else { |
| 10651 | dst_chr = src_chr; |
| 10652 | } |
| 10653 | if (dst_chr) { |
| 10654 | // dst_end_ptr already has adjustment for terminating zero |
| 10655 | if (dst_ptr < dst_end_ptr) { |
| 10656 | *dst_ptr++ = dst_chr; |
| 10657 | } |
| 10658 | else { |
| 10659 | _SAPP_ERROR(DROPPED_FILE_PATH_TOO_LONG); |
| 10660 | err = true; |
| 10661 | break; |
| 10662 | } |
| 10663 | } |
| 10664 | } |
| 10665 | if (err) { |
| 10666 | _sapp_clear_drop_buffer(); |
| 10667 | _sapp.drop.num_files = 0; |
| 10668 | return false; |
| 10669 | } |
| 10670 | else { |
| 10671 | return true; |
| 10672 | } |
| 10673 | } |
| 10674 | |
| 10675 | // XLib manual says keycodes are in the range [8, 255] inclusive. |
| 10676 | // https://tronche.com/gui/x/xlib/input/keyboard-encoding.html |
| 10677 | static bool _sapp_x11_keycodes[256]; |
| 10678 | |
| 10679 | _SOKOL_PRIVATE void _sapp_x11_process_event(XEvent* event) { |
| 10680 | Bool filtered = XFilterEvent(event, None); |
| 10681 | switch (event->type) { |
| 10682 | case GenericEvent: |
| 10683 | if (_sapp.mouse.locked && _sapp.x11.xi.available) { |
| 10684 | if (event->xcookie.extension == _sapp.x11.xi.major_opcode) { |
| 10685 | if (XGetEventData(_sapp.x11.display, &event->xcookie)) { |
| 10686 | if (event->xcookie.evtype == XI_RawMotion) { |
| 10687 | XIRawEvent* re = (XIRawEvent*) event->xcookie.data; |
| 10688 | if (re->valuators.mask_len) { |
| 10689 | const double* values = re->raw_values; |
| 10690 | if (XIMaskIsSet(re->valuators.mask, 0)) { |
| 10691 | _sapp.mouse.dx = (float) *values; |
| 10692 | values++; |
| 10693 | } |
| 10694 | if (XIMaskIsSet(re->valuators.mask, 1)) { |
| 10695 | _sapp.mouse.dy = (float) *values; |
| 10696 | } |
| 10697 | _sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID, _sapp_x11_mods(event->xmotion.state)); |
| 10698 | } |
| 10699 | } |
| 10700 | XFreeEventData(_sapp.x11.display, &event->xcookie); |
| 10701 | } |
| 10702 | } |
| 10703 | } |
| 10704 | break; |
| 10705 | case FocusIn: |
| 10706 | // NOTE: ignoring NotifyGrab and NotifyUngrab is same behaviour as GLFW |
| 10707 | if ((event->xfocus.mode != NotifyGrab) && (event->xfocus.mode != NotifyUngrab)) { |
| 10708 | _sapp_x11_app_event(SAPP_EVENTTYPE_FOCUSED); |
| 10709 | } |
| 10710 | break; |
| 10711 | case FocusOut: |
| 10712 | /* if focus is lost for any reason, and we're in mouse locked mode, disable mouse lock */ |
| 10713 | if (_sapp.mouse.locked) { |
| 10714 | _sapp_x11_lock_mouse(false); |
| 10715 | } |
| 10716 | // NOTE: ignoring NotifyGrab and NotifyUngrab is same behaviour as GLFW |
| 10717 | if ((event->xfocus.mode != NotifyGrab) && (event->xfocus.mode != NotifyUngrab)) { |
| 10718 | _sapp_x11_app_event(SAPP_EVENTTYPE_UNFOCUSED); |
| 10719 | } |
| 10720 | break; |
| 10721 | case KeyPress: |
| 10722 | { |
| 10723 | int keycode = (int)event->xkey.keycode; |
| 10724 | const sapp_keycode key = _sapp_x11_translate_key(keycode); |
| 10725 | bool repeat = _sapp_x11_keycodes[keycode & 0xFF]; |
| 10726 | _sapp_x11_keycodes[keycode & 0xFF] = true; |
| 10727 | uint32_t mods = _sapp_x11_mods(event->xkey.state); |
| 10728 | // X11 doesn't set modifier bit on key down, so emulate that |
| 10729 | mods |= _sapp_x11_key_modifier_bit(key); |
| 10730 | if (key != SAPP_KEYCODE_INVALID) { |
| 10731 | _sapp_x11_key_event(SAPP_EVENTTYPE_KEY_DOWN, key, repeat, mods); |
| 10732 | } |
| 10733 | KeySym keysym; |
| 10734 | XLookupString(&event->xkey, NULL, 0, &keysym, NULL); |
| 10735 | int32_t chr = _sapp_x11_keysym_to_unicode(keysym); |
| 10736 | if (chr > 0) { |
| 10737 | _sapp_x11_char_event((uint32_t)chr, repeat, mods); |
| 10738 | } |
| 10739 | } |
| 10740 | break; |
| 10741 | case KeyRelease: |
| 10742 | { |
| 10743 | int keycode = (int)event->xkey.keycode; |
| 10744 | const sapp_keycode key = _sapp_x11_translate_key(keycode); |
| 10745 | _sapp_x11_keycodes[keycode & 0xFF] = false; |
| 10746 | if (key != SAPP_KEYCODE_INVALID) { |
| 10747 | uint32_t mods = _sapp_x11_mods(event->xkey.state); |
| 10748 | // X11 doesn't clear modifier bit on key up, so emulate that |
| 10749 | mods &= ~_sapp_x11_key_modifier_bit(key); |
| 10750 | _sapp_x11_key_event(SAPP_EVENTTYPE_KEY_UP, key, false, mods); |
| 10751 | } |
| 10752 | } |
| 10753 | break; |
| 10754 | case ButtonPress: |
| 10755 | { |
| 10756 | _sapp_x11_mouse_update(event->xbutton.x, event->xbutton.y, false); |
| 10757 | const sapp_mousebutton btn = _sapp_x11_translate_button(event); |
| 10758 | uint32_t mods = _sapp_x11_mods(event->xbutton.state); |
| 10759 | // X11 doesn't set modifier bit on button down, so emulate that |
| 10760 | mods |= _sapp_x11_button_modifier_bit(btn); |
| 10761 | if (btn != SAPP_MOUSEBUTTON_INVALID) { |
| 10762 | _sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, btn, mods); |
| 10763 | _sapp.x11.mouse_buttons |= (1 << btn); |
| 10764 | } |
| 10765 | else { |
| 10766 | /* might be a scroll event */ |
| 10767 | switch (event->xbutton.button) { |
| 10768 | case 4: _sapp_x11_scroll_event(0.0f, 1.0f, mods); break; |
| 10769 | case 5: _sapp_x11_scroll_event(0.0f, -1.0f, mods); break; |
| 10770 | case 6: _sapp_x11_scroll_event(1.0f, 0.0f, mods); break; |
| 10771 | case 7: _sapp_x11_scroll_event(-1.0f, 0.0f, mods); break; |
| 10772 | } |
| 10773 | } |
| 10774 | } |
| 10775 | break; |
| 10776 | case ButtonRelease: |
| 10777 | { |
| 10778 | _sapp_x11_mouse_update(event->xbutton.x, event->xbutton.y, false); |
| 10779 | const sapp_mousebutton btn = _sapp_x11_translate_button(event); |
| 10780 | if (btn != SAPP_MOUSEBUTTON_INVALID) { |
| 10781 | uint32_t mods = _sapp_x11_mods(event->xbutton.state); |
| 10782 | // X11 doesn't clear modifier bit on button up, so emulate that |
| 10783 | mods &= ~_sapp_x11_button_modifier_bit(btn); |
| 10784 | _sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, btn, mods); |
| 10785 | _sapp.x11.mouse_buttons &= ~(1 << btn); |
| 10786 | } |
| 10787 | } |
| 10788 | break; |
| 10789 | case EnterNotify: |
| 10790 | /* don't send enter/leave events while mouse button held down */ |
| 10791 | if (0 == _sapp.x11.mouse_buttons) { |
| 10792 | _sapp_x11_mouse_update(event->xcrossing.x, event->xcrossing.y, true); |
| 10793 | _sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_ENTER, SAPP_MOUSEBUTTON_INVALID, _sapp_x11_mods(event->xcrossing.state)); |
| 10794 | } |
| 10795 | break; |
| 10796 | case LeaveNotify: |
| 10797 | if (0 == _sapp.x11.mouse_buttons) { |
| 10798 | _sapp_x11_mouse_update(event->xcrossing.x, event->xcrossing.y, true); |
| 10799 | _sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_LEAVE, SAPP_MOUSEBUTTON_INVALID, _sapp_x11_mods(event->xcrossing.state)); |
| 10800 | } |
| 10801 | break; |
| 10802 | case MotionNotify: |
| 10803 | if (!_sapp.mouse.locked) { |
| 10804 | _sapp_x11_mouse_update(event->xmotion.x, event->xmotion.y, false); |
| 10805 | _sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID, _sapp_x11_mods(event->xmotion.state)); |
| 10806 | } |
| 10807 | break; |
| 10808 | case ConfigureNotify: |
| 10809 | if ((event->xconfigure.width != _sapp.window_width) || (event->xconfigure.height != _sapp.window_height)) { |
| 10810 | _sapp.window_width = event->xconfigure.width; |
| 10811 | _sapp.window_height = event->xconfigure.height; |
| 10812 | _sapp.framebuffer_width = _sapp.window_width; |
| 10813 | _sapp.framebuffer_height = _sapp.window_height; |
| 10814 | _sapp_x11_app_event(SAPP_EVENTTYPE_RESIZED); |
| 10815 | } |
| 10816 | break; |
| 10817 | case PropertyNotify: |
| 10818 | if (event->xproperty.state == PropertyNewValue) { |
| 10819 | if (event->xproperty.atom == _sapp.x11.WM_STATE) { |
| 10820 | const int state = _sapp_x11_get_window_state(); |
| 10821 | if (state != _sapp.x11.window_state) { |
| 10822 | _sapp.x11.window_state = state; |
| 10823 | if (state == IconicState) { |
| 10824 | _sapp_x11_app_event(SAPP_EVENTTYPE_ICONIFIED); |
| 10825 | } |
| 10826 | else if (state == NormalState) { |
| 10827 | _sapp_x11_app_event(SAPP_EVENTTYPE_RESTORED); |
| 10828 | } |
| 10829 | } |
| 10830 | } |
| 10831 | } |
| 10832 | break; |
| 10833 | case ClientMessage: |
| 10834 | if (filtered) { |
| 10835 | return; |
| 10836 | } |
| 10837 | if (event->xclient.message_type == _sapp.x11.WM_PROTOCOLS) { |
| 10838 | const Atom protocol = (Atom)event->xclient.data.l[0]; |
| 10839 | if (protocol == _sapp.x11.WM_DELETE_WINDOW) { |
| 10840 | _sapp.quit_requested = true; |
| 10841 | } |
| 10842 | } |
| 10843 | else if (event->xclient.message_type == _sapp.x11.xdnd.XdndEnter) { |
| 10844 | const bool is_list = 0 != (event->xclient.data.l[1] & 1); |
| 10845 | _sapp.x11.xdnd.source = (Window)event->xclient.data.l[0]; |
| 10846 | _sapp.x11.xdnd.version = event->xclient.data.l[1] >> 24; |
| 10847 | _sapp.x11.xdnd.format = None; |
| 10848 | if (_sapp.x11.xdnd.version > _SAPP_X11_XDND_VERSION) { |
| 10849 | return; |
| 10850 | } |
| 10851 | uint32_t count = 0; |
| 10852 | Atom* formats = 0; |
| 10853 | if (is_list) { |
| 10854 | count = _sapp_x11_get_window_property(_sapp.x11.xdnd.source, _sapp.x11.xdnd.XdndTypeList, XA_ATOM, (unsigned char**)&formats); |
| 10855 | } |
| 10856 | else { |
| 10857 | count = 3; |
| 10858 | formats = (Atom*) event->xclient.data.l + 2; |
| 10859 | } |
| 10860 | for (uint32_t i = 0; i < count; i++) { |
| 10861 | if (formats[i] == _sapp.x11.xdnd.text_uri_list) { |
| 10862 | _sapp.x11.xdnd.format = _sapp.x11.xdnd.text_uri_list; |
| 10863 | break; |
| 10864 | } |
| 10865 | } |
| 10866 | if (is_list && formats) { |
| 10867 | XFree(formats); |
| 10868 | } |
| 10869 | } |
| 10870 | else if (event->xclient.message_type == _sapp.x11.xdnd.XdndDrop) { |
| 10871 | if (_sapp.x11.xdnd.version > _SAPP_X11_XDND_VERSION) { |
| 10872 | return; |
| 10873 | } |
| 10874 | Time time = CurrentTime; |
| 10875 | if (_sapp.x11.xdnd.format) { |
| 10876 | if (_sapp.x11.xdnd.version >= 1) { |
| 10877 | time = (Time)event->xclient.data.l[2]; |
| 10878 | } |
| 10879 | XConvertSelection(_sapp.x11.display, |
| 10880 | _sapp.x11.xdnd.XdndSelection, |
| 10881 | _sapp.x11.xdnd.format, |
| 10882 | _sapp.x11.xdnd.XdndSelection, |
| 10883 | _sapp.x11.window, |
| 10884 | time); |
| 10885 | } |
| 10886 | else if (_sapp.x11.xdnd.version >= 2) { |
| 10887 | XEvent reply; |
| 10888 | _sapp_clear(&reply, sizeof(reply)); |
| 10889 | reply.type = ClientMessage; |
| 10890 | reply.xclient.window = _sapp.x11.xdnd.source; |
| 10891 | reply.xclient.message_type = _sapp.x11.xdnd.XdndFinished; |
| 10892 | reply.xclient.format = 32; |
| 10893 | reply.xclient.data.l[0] = (long)_sapp.x11.window; |
| 10894 | reply.xclient.data.l[1] = 0; // drag was rejected |
| 10895 | reply.xclient.data.l[2] = None; |
| 10896 | XSendEvent(_sapp.x11.display, _sapp.x11.xdnd.source, False, NoEventMask, &reply); |
| 10897 | XFlush(_sapp.x11.display); |
| 10898 | } |
| 10899 | } |
| 10900 | else if (event->xclient.message_type == _sapp.x11.xdnd.XdndPosition) { |
| 10901 | /* drag operation has moved over the window |
| 10902 | FIXME: we could track the mouse position here, but |
| 10903 | this isn't implemented on other platforms either so far |
| 10904 | */ |
| 10905 | if (_sapp.x11.xdnd.version > _SAPP_X11_XDND_VERSION) { |
| 10906 | return; |
| 10907 | } |
| 10908 | XEvent reply; |
| 10909 | _sapp_clear(&reply, sizeof(reply)); |
| 10910 | reply.type = ClientMessage; |
| 10911 | reply.xclient.window = _sapp.x11.xdnd.source; |
| 10912 | reply.xclient.message_type = _sapp.x11.xdnd.XdndStatus; |
| 10913 | reply.xclient.format = 32; |
| 10914 | reply.xclient.data.l[0] = (long)_sapp.x11.window; |
| 10915 | if (_sapp.x11.xdnd.format) { |
| 10916 | /* reply that we are ready to copy the dragged data */ |
| 10917 | reply.xclient.data.l[1] = 1; // accept with no rectangle |
| 10918 | if (_sapp.x11.xdnd.version >= 2) { |
| 10919 | reply.xclient.data.l[4] = (long)_sapp.x11.xdnd.XdndActionCopy; |
| 10920 | } |
| 10921 | } |
| 10922 | XSendEvent(_sapp.x11.display, _sapp.x11.xdnd.source, False, NoEventMask, &reply); |
| 10923 | XFlush(_sapp.x11.display); |
| 10924 | } |
| 10925 | break; |
| 10926 | case SelectionNotify: |
| 10927 | if (event->xselection.property == _sapp.x11.xdnd.XdndSelection) { |
| 10928 | char* data = 0; |
| 10929 | uint32_t result = _sapp_x11_get_window_property(event->xselection.requestor, |
| 10930 | event->xselection.property, |
| 10931 | event->xselection.target, |
| 10932 | (unsigned char**) &data); |
| 10933 | if (_sapp.drop.enabled && result) { |
| 10934 | if (_sapp_x11_parse_dropped_files_list(data)) { |
| 10935 | _sapp.mouse.dx = 0.0f; |
| 10936 | _sapp.mouse.dy = 0.0f; |
| 10937 | if (_sapp_events_enabled()) { |
| 10938 | // FIXME: Figure out how to get modifier key state here. |
| 10939 | // The XSelection event has no 'state' item, and |
| 10940 | // XQueryKeymap() always returns a zeroed array. |
| 10941 | _sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED); |
| 10942 | _sapp_call_event(&_sapp.event); |
| 10943 | } |
| 10944 | } |
| 10945 | } |
| 10946 | if (_sapp.x11.xdnd.version >= 2) { |
| 10947 | XEvent reply; |
| 10948 | _sapp_clear(&reply, sizeof(reply)); |
| 10949 | reply.type = ClientMessage; |
| 10950 | reply.xclient.window = _sapp.x11.xdnd.source; |
| 10951 | reply.xclient.message_type = _sapp.x11.xdnd.XdndFinished; |
| 10952 | reply.xclient.format = 32; |
| 10953 | reply.xclient.data.l[0] = (long)_sapp.x11.window; |
| 10954 | reply.xclient.data.l[1] = result; |
| 10955 | reply.xclient.data.l[2] = (long)_sapp.x11.xdnd.XdndActionCopy; |
| 10956 | XSendEvent(_sapp.x11.display, _sapp.x11.xdnd.source, False, NoEventMask, &reply); |
| 10957 | XFlush(_sapp.x11.display); |
| 10958 | } |
| 10959 | } |
| 10960 | break; |
| 10961 | case DestroyNotify: |
| 10962 | break; |
| 10963 | } |
| 10964 | } |
| 10965 | |
| 10966 | #if !defined(_SAPP_GLX) |
| 10967 | |
| 10968 | _SOKOL_PRIVATE void _sapp_egl_init(void) { |
| 10969 | #if defined(SOKOL_GLCORE) |
| 10970 | if (!eglBindAPI(EGL_OPENGL_API)) { |
| 10971 | _SAPP_PANIC(LINUX_EGL_BIND_OPENGL_API_FAILED); |
| 10972 | } |
| 10973 | #else |
| 10974 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 10975 | _SAPP_PANIC(LINUX_EGL_BIND_OPENGL_ES_API_FAILED); |
| 10976 | } |
| 10977 | #endif |
| 10978 | |
| 10979 | _sapp.egl.display = eglGetDisplay((EGLNativeDisplayType)_sapp.x11.display); |
| 10980 | if (EGL_NO_DISPLAY == _sapp.egl.display) { |
| 10981 | _SAPP_PANIC(LINUX_EGL_GET_DISPLAY_FAILED); |
| 10982 | } |
| 10983 | |
| 10984 | EGLint major, minor; |
| 10985 | if (!eglInitialize(_sapp.egl.display, &major, &minor)) { |
| 10986 | _SAPP_PANIC(LINUX_EGL_INITIALIZE_FAILED); |
| 10987 | } |
| 10988 | |
| 10989 | EGLint sample_count = _sapp.desc.sample_count > 1 ? _sapp.desc.sample_count : 0; |
| 10990 | EGLint alpha_size = _sapp.desc.alpha ? 8 : 0; |
| 10991 | const EGLint config_attrs[] = { |
| 10992 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 10993 | #if defined(SOKOL_GLCORE) |
| 10994 | EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, |
| 10995 | #elif defined(SOKOL_GLES3) |
| 10996 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, |
| 10997 | #endif |
| 10998 | EGL_RED_SIZE, 8, |
| 10999 | EGL_GREEN_SIZE, 8, |
| 11000 | EGL_BLUE_SIZE, 8, |
| 11001 | EGL_ALPHA_SIZE, alpha_size, |
| 11002 | EGL_DEPTH_SIZE, 24, |
| 11003 | EGL_STENCIL_SIZE, 8, |
| 11004 | EGL_SAMPLE_BUFFERS, _sapp.desc.sample_count > 1 ? 1 : 0, |
| 11005 | EGL_SAMPLES, sample_count, |
| 11006 | EGL_NONE, |
| 11007 | }; |
| 11008 | |
| 11009 | EGLConfig egl_configs[32]; |
| 11010 | EGLint config_count; |
| 11011 | if (!eglChooseConfig(_sapp.egl.display, config_attrs, egl_configs, 32, &config_count) || config_count == 0) { |
| 11012 | _SAPP_PANIC(LINUX_EGL_NO_CONFIGS); |
| 11013 | } |
| 11014 | |
| 11015 | EGLConfig config = egl_configs[0]; |
| 11016 | for (int i = 0; i < config_count; ++i) { |
| 11017 | EGLConfig c = egl_configs[i]; |
| 11018 | EGLint r, g, b, a, d, s, n; |
| 11019 | if (eglGetConfigAttrib(_sapp.egl.display, c, EGL_RED_SIZE, &r) && |
| 11020 | eglGetConfigAttrib(_sapp.egl.display, c, EGL_GREEN_SIZE, &g) && |
| 11021 | eglGetConfigAttrib(_sapp.egl.display, c, EGL_BLUE_SIZE, &b) && |
| 11022 | eglGetConfigAttrib(_sapp.egl.display, c, EGL_ALPHA_SIZE, &a) && |
| 11023 | eglGetConfigAttrib(_sapp.egl.display, c, EGL_DEPTH_SIZE, &d) && |
| 11024 | eglGetConfigAttrib(_sapp.egl.display, c, EGL_STENCIL_SIZE, &s) && |
| 11025 | eglGetConfigAttrib(_sapp.egl.display, c, EGL_SAMPLES, &n) && |
| 11026 | (r == 8) && (g == 8) && (b == 8) && (a == alpha_size) && (d == 24) && (s == 8) && (n == sample_count)) { |
| 11027 | config = c; |
| 11028 | break; |
| 11029 | } |
| 11030 | } |
| 11031 | |
| 11032 | EGLint visual_id; |
| 11033 | if (!eglGetConfigAttrib(_sapp.egl.display, config, EGL_NATIVE_VISUAL_ID, &visual_id)) { |
| 11034 | _SAPP_PANIC(LINUX_EGL_NO_NATIVE_VISUAL); |
| 11035 | } |
| 11036 | |
| 11037 | XVisualInfo visual_info_template; |
| 11038 | _sapp_clear(&visual_info_template, sizeof(visual_info_template)); |
| 11039 | visual_info_template.visualid = (VisualID)visual_id; |
| 11040 | |
| 11041 | int num_visuals; |
| 11042 | XVisualInfo* visual_info = XGetVisualInfo(_sapp.x11.display, VisualIDMask, &visual_info_template, &num_visuals); |
| 11043 | if (!visual_info) { |
| 11044 | _SAPP_PANIC(LINUX_EGL_GET_VISUAL_INFO_FAILED); |
| 11045 | } |
| 11046 | |
| 11047 | _sapp_x11_create_window(visual_info->visual, visual_info->depth); |
| 11048 | XFree(visual_info); |
| 11049 | |
| 11050 | _sapp.egl.surface = eglCreateWindowSurface(_sapp.egl.display, config, (EGLNativeWindowType)_sapp.x11.window, NULL); |
| 11051 | if (EGL_NO_SURFACE == _sapp.egl.surface) { |
| 11052 | _SAPP_PANIC(LINUX_EGL_CREATE_WINDOW_SURFACE_FAILED); |
| 11053 | } |
| 11054 | |
| 11055 | EGLint ctx_attrs[] = { |
| 11056 | #if defined(SOKOL_GLCORE) |
| 11057 | EGL_CONTEXT_MAJOR_VERSION, _sapp.desc.gl_major_version, |
| 11058 | EGL_CONTEXT_MINOR_VERSION, _sapp.desc.gl_minor_version, |
| 11059 | EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, |
| 11060 | #elif defined(SOKOL_GLES3) |
| 11061 | EGL_CONTEXT_CLIENT_VERSION, 3, |
| 11062 | #endif |
| 11063 | EGL_NONE, |
| 11064 | }; |
| 11065 | |
| 11066 | _sapp.egl.context = eglCreateContext(_sapp.egl.display, config, EGL_NO_CONTEXT, ctx_attrs); |
| 11067 | if (EGL_NO_CONTEXT == _sapp.egl.context) { |
| 11068 | _SAPP_PANIC(LINUX_EGL_CREATE_CONTEXT_FAILED); |
| 11069 | } |
| 11070 | |
| 11071 | if (!eglMakeCurrent(_sapp.egl.display, _sapp.egl.surface, _sapp.egl.surface, _sapp.egl.context)) { |
| 11072 | _SAPP_PANIC(LINUX_EGL_MAKE_CURRENT_FAILED); |
| 11073 | } |
| 11074 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_sapp.gl.framebuffer); |
| 11075 | |
| 11076 | eglSwapInterval(_sapp.egl.display, _sapp.swap_interval); |
| 11077 | } |
| 11078 | |
| 11079 | _SOKOL_PRIVATE void _sapp_egl_destroy(void) { |
| 11080 | if (_sapp.egl.display != EGL_NO_DISPLAY) { |
| 11081 | eglMakeCurrent(_sapp.egl.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 11082 | |
| 11083 | if (_sapp.egl.context != EGL_NO_CONTEXT) { |
| 11084 | eglDestroyContext(_sapp.egl.display, _sapp.egl.context); |
| 11085 | _sapp.egl.context = EGL_NO_CONTEXT; |
| 11086 | } |
| 11087 | |
| 11088 | if (_sapp.egl.surface != EGL_NO_SURFACE) { |
| 11089 | eglDestroySurface(_sapp.egl.display, _sapp.egl.surface); |
| 11090 | _sapp.egl.surface = EGL_NO_SURFACE; |
| 11091 | } |
| 11092 | |
| 11093 | eglTerminate(_sapp.egl.display); |
| 11094 | _sapp.egl.display = EGL_NO_DISPLAY; |
| 11095 | } |
| 11096 | } |
| 11097 | |
| 11098 | #endif /* _SAPP_GLX */ |
| 11099 | |
| 11100 | _SOKOL_PRIVATE void _sapp_linux_run(const sapp_desc* desc) { |
| 11101 | /* The following lines are here to trigger a linker error instead of an |
| 11102 | obscure runtime error if the user has forgotten to add -pthread to |
| 11103 | the compiler or linker options. They have no other purpose. |
| 11104 | */ |
| 11105 | pthread_attr_t pthread_attr; |
| 11106 | pthread_attr_init(&pthread_attr); |
| 11107 | pthread_attr_destroy(&pthread_attr); |
| 11108 | |
| 11109 | _sapp_init_state(desc); |
| 11110 | _sapp.x11.window_state = NormalState; |
| 11111 | |
| 11112 | XInitThreads(); |
| 11113 | XrmInitialize(); |
| 11114 | _sapp.x11.display = XOpenDisplay(NULL); |
| 11115 | if (!_sapp.x11.display) { |
| 11116 | _SAPP_PANIC(LINUX_X11_OPEN_DISPLAY_FAILED); |
| 11117 | } |
| 11118 | _sapp.x11.screen = DefaultScreen(_sapp.x11.display); |
| 11119 | _sapp.x11.root = DefaultRootWindow(_sapp.x11.display); |
| 11120 | XkbSetDetectableAutoRepeat(_sapp.x11.display, true, NULL); |
| 11121 | _sapp_x11_query_system_dpi(); |
| 11122 | _sapp.dpi_scale = _sapp.x11.dpi / 96.0f; |
| 11123 | _sapp_x11_init_extensions(); |
| 11124 | _sapp_x11_create_cursors(); |
| 11125 | #if defined(_SAPP_GLX) |
| 11126 | _sapp_glx_init(); |
| 11127 | Visual* visual = 0; |
| 11128 | int depth = 0; |
| 11129 | _sapp_glx_choose_visual(&visual, &depth); |
| 11130 | _sapp_x11_create_window(visual, depth); |
| 11131 | _sapp_glx_create_context(); |
| 11132 | _sapp_glx_swapinterval(_sapp.swap_interval); |
| 11133 | #else |
| 11134 | _sapp_egl_init(); |
| 11135 | #endif |
| 11136 | sapp_set_icon(&desc->icon); |
| 11137 | _sapp.valid = true; |
| 11138 | _sapp_x11_show_window(); |
| 11139 | if (_sapp.fullscreen) { |
| 11140 | _sapp_x11_set_fullscreen(true); |
| 11141 | } |
| 11142 | |
| 11143 | XFlush(_sapp.x11.display); |
| 11144 | while (!_sapp.quit_ordered) { |
| 11145 | _sapp_timing_measure(&_sapp.timing); |
| 11146 | int count = XPending(_sapp.x11.display); |
| 11147 | while (count--) { |
| 11148 | XEvent event; |
| 11149 | XNextEvent(_sapp.x11.display, &event); |
| 11150 | _sapp_x11_process_event(&event); |
| 11151 | } |
| 11152 | _sapp_frame(); |
| 11153 | #if defined(_SAPP_GLX) |
| 11154 | _sapp_glx_swap_buffers(); |
| 11155 | #else |
| 11156 | eglSwapBuffers(_sapp.egl.display, _sapp.egl.surface); |
| 11157 | #endif |
| 11158 | XFlush(_sapp.x11.display); |
| 11159 | /* handle quit-requested, either from window or from sapp_request_quit() */ |
| 11160 | if (_sapp.quit_requested && !_sapp.quit_ordered) { |
| 11161 | /* give user code a chance to intervene */ |
| 11162 | _sapp_x11_app_event(SAPP_EVENTTYPE_QUIT_REQUESTED); |
| 11163 | /* if user code hasn't intervened, quit the app */ |
| 11164 | if (_sapp.quit_requested) { |
| 11165 | _sapp.quit_ordered = true; |
| 11166 | } |
| 11167 | } |
| 11168 | } |
| 11169 | _sapp_call_cleanup(); |
| 11170 | #if defined(_SAPP_GLX) |
| 11171 | _sapp_glx_destroy_context(); |
| 11172 | #else |
| 11173 | _sapp_egl_destroy(); |
| 11174 | #endif |
| 11175 | _sapp_x11_destroy_window(); |
| 11176 | _sapp_x11_destroy_cursors(); |
| 11177 | XCloseDisplay(_sapp.x11.display); |
| 11178 | _sapp_discard_state(); |
| 11179 | } |
| 11180 | |
| 11181 | #if !defined(SOKOL_NO_ENTRY) |
| 11182 | int main(int argc, char* argv[]) { |
| 11183 | sapp_desc desc = sokol_main(argc, argv); |
| 11184 | _sapp_linux_run(&desc); |
| 11185 | return 0; |
| 11186 | } |
| 11187 | #endif /* SOKOL_NO_ENTRY */ |
| 11188 | #endif /* _SAPP_LINUX */ |
| 11189 | |
| 11190 | // ██████ ██ ██ ██████ ██ ██ ██████ |
| 11191 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 11192 | // ██████ ██ ██ ██████ ██ ██ ██ |
| 11193 | // ██ ██ ██ ██ ██ ██ ██ ██ |
| 11194 | // ██ ██████ ██████ ███████ ██ ██████ |
| 11195 | // |
| 11196 | // >>public |
| 11197 | #if defined(SOKOL_NO_ENTRY) |
| 11198 | SOKOL_API_IMPL void sapp_run(const sapp_desc* desc) { |
| 11199 | SOKOL_ASSERT(desc); |
| 11200 | #if defined(_SAPP_MACOS) |
| 11201 | _sapp_macos_run(desc); |
| 11202 | #elif defined(_SAPP_IOS) |
| 11203 | _sapp_ios_run(desc); |
| 11204 | #elif defined(_SAPP_EMSCRIPTEN) |
| 11205 | _sapp_emsc_run(desc); |
| 11206 | #elif defined(_SAPP_WIN32) |
| 11207 | _sapp_win32_run(desc); |
| 11208 | #elif defined(_SAPP_LINUX) |
| 11209 | _sapp_linux_run(desc); |
| 11210 | #else |
| 11211 | #error "sapp_run() not supported on this platform" |
| 11212 | #endif |
| 11213 | } |
| 11214 | |
| 11215 | /* this is just a stub so the linker doesn't complain */ |
| 11216 | sapp_desc sokol_main(int argc, char* argv[]) { |
| 11217 | _SOKOL_UNUSED(argc); |
| 11218 | _SOKOL_UNUSED(argv); |
| 11219 | sapp_desc desc; |
| 11220 | _sapp_clear(&desc, sizeof(desc)); |
| 11221 | return desc; |
| 11222 | } |
| 11223 | #else |
| 11224 | /* likewise, in normal mode, sapp_run() is just an empty stub */ |
| 11225 | SOKOL_API_IMPL void sapp_run(const sapp_desc* desc) { |
| 11226 | _SOKOL_UNUSED(desc); |
| 11227 | } |
| 11228 | #endif |
| 11229 | |
| 11230 | SOKOL_API_IMPL bool sapp_isvalid(void) { |
| 11231 | return _sapp.valid; |
| 11232 | } |
| 11233 | |
| 11234 | SOKOL_API_IMPL void* sapp_userdata(void) { |
| 11235 | return _sapp.desc.user_data; |
| 11236 | } |
| 11237 | |
| 11238 | SOKOL_API_IMPL sapp_desc sapp_query_desc(void) { |
| 11239 | return _sapp.desc; |
| 11240 | } |
| 11241 | |
| 11242 | SOKOL_API_IMPL uint64_t sapp_frame_count(void) { |
| 11243 | return _sapp.frame_count; |
| 11244 | } |
| 11245 | |
| 11246 | SOKOL_API_IMPL double sapp_frame_duration(void) { |
| 11247 | return _sapp_timing_get_avg(&_sapp.timing); |
| 11248 | } |
| 11249 | |
| 11250 | SOKOL_API_IMPL int sapp_width(void) { |
| 11251 | return (_sapp.framebuffer_width > 0) ? _sapp.framebuffer_width : 1; |
| 11252 | } |
| 11253 | |
| 11254 | SOKOL_API_IMPL float sapp_widthf(void) { |
| 11255 | return (float)sapp_width(); |
| 11256 | } |
| 11257 | |
| 11258 | SOKOL_API_IMPL int sapp_height(void) { |
| 11259 | return (_sapp.framebuffer_height > 0) ? _sapp.framebuffer_height : 1; |
| 11260 | } |
| 11261 | |
| 11262 | SOKOL_API_IMPL float sapp_heightf(void) { |
| 11263 | return (float)sapp_height(); |
| 11264 | } |
| 11265 | |
| 11266 | SOKOL_API_IMPL int sapp_color_format(void) { |
| 11267 | #if defined(_SAPP_EMSCRIPTEN) && defined(SOKOL_WGPU) |
| 11268 | switch (_sapp.wgpu.render_format) { |
| 11269 | case WGPUTextureFormat_RGBA8Unorm: |
| 11270 | return _SAPP_PIXELFORMAT_RGBA8; |
| 11271 | case WGPUTextureFormat_BGRA8Unorm: |
| 11272 | return _SAPP_PIXELFORMAT_BGRA8; |
| 11273 | default: |
| 11274 | SOKOL_UNREACHABLE; |
| 11275 | return 0; |
| 11276 | } |
| 11277 | #elif defined(SOKOL_METAL) || defined(SOKOL_D3D11) |
| 11278 | return _SAPP_PIXELFORMAT_BGRA8; |
| 11279 | #else |
| 11280 | return _SAPP_PIXELFORMAT_RGBA8; |
| 11281 | #endif |
| 11282 | } |
| 11283 | |
| 11284 | SOKOL_API_IMPL int sapp_depth_format(void) { |
| 11285 | return _SAPP_PIXELFORMAT_DEPTH_STENCIL; |
| 11286 | } |
| 11287 | |
| 11288 | SOKOL_API_IMPL int sapp_sample_count(void) { |
| 11289 | return _sapp.sample_count; |
| 11290 | } |
| 11291 | |
| 11292 | SOKOL_API_IMPL bool sapp_high_dpi(void) { |
| 11293 | return _sapp.desc.high_dpi && (_sapp.dpi_scale >= 1.5f); |
| 11294 | } |
| 11295 | |
| 11296 | SOKOL_API_IMPL float sapp_dpi_scale(void) { |
| 11297 | return _sapp.dpi_scale; |
| 11298 | } |
| 11299 | |
| 11300 | SOKOL_API_IMPL const void* sapp_egl_get_display(void) { |
| 11301 | SOKOL_ASSERT(_sapp.valid); |
| 11302 | #if defined(_SAPP_ANDROID) |
| 11303 | return _sapp.android.display; |
| 11304 | #elif defined(_SAPP_LINUX) && !defined(_SAPP_GLX) |
| 11305 | return _sapp.egl.display; |
| 11306 | #else |
| 11307 | return 0; |
| 11308 | #endif |
| 11309 | } |
| 11310 | |
| 11311 | SOKOL_API_IMPL const void* sapp_egl_get_context(void) { |
| 11312 | SOKOL_ASSERT(_sapp.valid); |
| 11313 | #if defined(_SAPP_ANDROID) |
| 11314 | return _sapp.android.context; |
| 11315 | #elif defined(_SAPP_LINUX) && !defined(_SAPP_GLX) |
| 11316 | return _sapp.egl.context; |
| 11317 | #else |
| 11318 | return 0; |
| 11319 | #endif |
| 11320 | } |
| 11321 | |
| 11322 | SOKOL_API_IMPL void sapp_show_keyboard(bool show) { |
| 11323 | #if defined(_SAPP_IOS) |
| 11324 | _sapp_ios_show_keyboard(show); |
| 11325 | #elif defined(_SAPP_ANDROID) |
| 11326 | _sapp_android_show_keyboard(show); |
| 11327 | #else |
| 11328 | _SOKOL_UNUSED(show); |
| 11329 | #endif |
| 11330 | } |
| 11331 | |
| 11332 | SOKOL_API_IMPL bool sapp_keyboard_shown(void) { |
| 11333 | return _sapp.onscreen_keyboard_shown; |
| 11334 | } |
| 11335 | |
| 11336 | SOKOL_API_IMPL bool sapp_is_fullscreen(void) { |
| 11337 | return _sapp.fullscreen; |
| 11338 | } |
| 11339 | |
| 11340 | SOKOL_API_IMPL void sapp_toggle_fullscreen(void) { |
| 11341 | #if defined(_SAPP_MACOS) |
| 11342 | _sapp_macos_toggle_fullscreen(); |
| 11343 | #elif defined(_SAPP_WIN32) |
| 11344 | _sapp_win32_toggle_fullscreen(); |
| 11345 | #elif defined(_SAPP_LINUX) |
| 11346 | _sapp_x11_toggle_fullscreen(); |
| 11347 | #endif |
| 11348 | } |
| 11349 | |
| 11350 | /* NOTE that sapp_show_mouse() does not "stack" like the Win32 or macOS API functions! */ |
| 11351 | SOKOL_API_IMPL void sapp_show_mouse(bool show) { |
| 11352 | if (_sapp.mouse.shown != show) { |
| 11353 | #if defined(_SAPP_MACOS) |
| 11354 | _sapp_macos_update_cursor(_sapp.mouse.current_cursor, show); |
| 11355 | #elif defined(_SAPP_WIN32) |
| 11356 | _sapp_win32_update_cursor(_sapp.mouse.current_cursor, show, false); |
| 11357 | #elif defined(_SAPP_LINUX) |
| 11358 | _sapp_x11_update_cursor(_sapp.mouse.current_cursor, show); |
| 11359 | #elif defined(_SAPP_EMSCRIPTEN) |
| 11360 | _sapp_emsc_update_cursor(_sapp.mouse.current_cursor, show); |
| 11361 | #endif |
| 11362 | _sapp.mouse.shown = show; |
| 11363 | } |
| 11364 | } |
| 11365 | |
| 11366 | SOKOL_API_IMPL bool sapp_mouse_shown(void) { |
| 11367 | return _sapp.mouse.shown; |
| 11368 | } |
| 11369 | |
| 11370 | SOKOL_API_IMPL void sapp_lock_mouse(bool lock) { |
| 11371 | #if defined(_SAPP_MACOS) |
| 11372 | _sapp_macos_lock_mouse(lock); |
| 11373 | #elif defined(_SAPP_EMSCRIPTEN) |
| 11374 | _sapp_emsc_lock_mouse(lock); |
| 11375 | #elif defined(_SAPP_WIN32) |
| 11376 | _sapp_win32_lock_mouse(lock); |
| 11377 | #elif defined(_SAPP_LINUX) |
| 11378 | _sapp_x11_lock_mouse(lock); |
| 11379 | #else |
| 11380 | _sapp.mouse.locked = lock; |
| 11381 | #endif |
| 11382 | } |
| 11383 | |
| 11384 | SOKOL_API_IMPL bool sapp_mouse_locked(void) { |
| 11385 | return _sapp.mouse.locked; |
| 11386 | } |
| 11387 | |
| 11388 | SOKOL_API_IMPL void sapp_set_mouse_cursor(sapp_mouse_cursor cursor) { |
| 11389 | SOKOL_ASSERT((cursor >= 0) && (cursor < _SAPP_MOUSECURSOR_NUM)); |
| 11390 | if (_sapp.mouse.current_cursor != cursor) { |
| 11391 | #if defined(_SAPP_MACOS) |
| 11392 | _sapp_macos_update_cursor(cursor, _sapp.mouse.shown); |
| 11393 | #elif defined(_SAPP_WIN32) |
| 11394 | _sapp_win32_update_cursor(cursor, _sapp.mouse.shown, false); |
| 11395 | #elif defined(_SAPP_LINUX) |
| 11396 | _sapp_x11_update_cursor(cursor, _sapp.mouse.shown); |
| 11397 | #elif defined(_SAPP_EMSCRIPTEN) |
| 11398 | _sapp_emsc_update_cursor(cursor, _sapp.mouse.shown); |
| 11399 | #endif |
| 11400 | _sapp.mouse.current_cursor = cursor; |
| 11401 | } |
| 11402 | } |
| 11403 | |
| 11404 | SOKOL_API_IMPL sapp_mouse_cursor sapp_get_mouse_cursor(void) { |
| 11405 | return _sapp.mouse.current_cursor; |
| 11406 | } |
| 11407 | |
| 11408 | SOKOL_API_IMPL void sapp_request_quit(void) { |
| 11409 | _sapp.quit_requested = true; |
| 11410 | } |
| 11411 | |
| 11412 | SOKOL_API_IMPL void sapp_cancel_quit(void) { |
| 11413 | _sapp.quit_requested = false; |
| 11414 | } |
| 11415 | |
| 11416 | SOKOL_API_IMPL void sapp_quit(void) { |
| 11417 | _sapp.quit_ordered = true; |
| 11418 | } |
| 11419 | |
| 11420 | SOKOL_API_IMPL void sapp_consume_event(void) { |
| 11421 | _sapp.event_consumed = true; |
| 11422 | } |
| 11423 | |
| 11424 | /* NOTE: on HTML5, sapp_set_clipboard_string() must be called from within event handler! */ |
| 11425 | SOKOL_API_IMPL void sapp_set_clipboard_string(const char* str) { |
| 11426 | if (!_sapp.clipboard.enabled) { |
| 11427 | return; |
| 11428 | } |
| 11429 | SOKOL_ASSERT(str); |
| 11430 | #if defined(_SAPP_MACOS) |
| 11431 | _sapp_macos_set_clipboard_string(str); |
| 11432 | #elif defined(_SAPP_EMSCRIPTEN) |
| 11433 | _sapp_emsc_set_clipboard_string(str); |
| 11434 | #elif defined(_SAPP_WIN32) |
| 11435 | _sapp_win32_set_clipboard_string(str); |
| 11436 | #else |
| 11437 | /* not implemented */ |
| 11438 | #endif |
| 11439 | _sapp_strcpy(str, _sapp.clipboard.buffer, _sapp.clipboard.buf_size); |
| 11440 | } |
| 11441 | |
| 11442 | SOKOL_API_IMPL const char* sapp_get_clipboard_string(void) { |
| 11443 | if (!_sapp.clipboard.enabled) { |
| 11444 | return ""; |
| 11445 | } |
| 11446 | #if defined(_SAPP_MACOS) |
| 11447 | return _sapp_macos_get_clipboard_string(); |
| 11448 | #elif defined(_SAPP_EMSCRIPTEN) |
| 11449 | return _sapp.clipboard.buffer; |
| 11450 | #elif defined(_SAPP_WIN32) |
| 11451 | return _sapp_win32_get_clipboard_string(); |
| 11452 | #else |
| 11453 | /* not implemented */ |
| 11454 | return _sapp.clipboard.buffer; |
| 11455 | #endif |
| 11456 | } |
| 11457 | |
| 11458 | SOKOL_API_IMPL void sapp_set_window_title(const char* title) { |
| 11459 | SOKOL_ASSERT(title); |
| 11460 | _sapp_strcpy(title, _sapp.window_title, sizeof(_sapp.window_title)); |
| 11461 | #if defined(_SAPP_MACOS) |
| 11462 | _sapp_macos_update_window_title(); |
| 11463 | #elif defined(_SAPP_WIN32) |
| 11464 | _sapp_win32_update_window_title(); |
| 11465 | #elif defined(_SAPP_LINUX) |
| 11466 | _sapp_x11_update_window_title(); |
| 11467 | #endif |
| 11468 | } |
| 11469 | |
| 11470 | SOKOL_API_IMPL void sapp_set_icon(const sapp_icon_desc* desc) { |
| 11471 | SOKOL_ASSERT(desc); |
| 11472 | if (desc->sokol_default) { |
| 11473 | if (0 == _sapp.default_icon_pixels) { |
| 11474 | _sapp_setup_default_icon(); |
| 11475 | } |
| 11476 | SOKOL_ASSERT(0 != _sapp.default_icon_pixels); |
| 11477 | desc = &_sapp.default_icon_desc; |
| 11478 | } |
| 11479 | const int num_images = _sapp_icon_num_images(desc); |
| 11480 | if (num_images == 0) { |
| 11481 | return; |
| 11482 | } |
| 11483 | SOKOL_ASSERT((num_images > 0) && (num_images <= SAPP_MAX_ICONIMAGES)); |
| 11484 | if (!_sapp_validate_icon_desc(desc, num_images)) { |
| 11485 | return; |
| 11486 | } |
| 11487 | #if defined(_SAPP_MACOS) |
| 11488 | _sapp_macos_set_icon(desc, num_images); |
| 11489 | #elif defined(_SAPP_WIN32) |
| 11490 | _sapp_win32_set_icon(desc, num_images); |
| 11491 | #elif defined(_SAPP_LINUX) |
| 11492 | _sapp_x11_set_icon(desc, num_images); |
| 11493 | #elif defined(_SAPP_EMSCRIPTEN) |
| 11494 | _sapp_emsc_set_icon(desc, num_images); |
| 11495 | #endif |
| 11496 | } |
| 11497 | |
| 11498 | SOKOL_API_IMPL int sapp_get_num_dropped_files(void) { |
| 11499 | SOKOL_ASSERT(_sapp.drop.enabled); |
| 11500 | return _sapp.drop.num_files; |
| 11501 | } |
| 11502 | |
| 11503 | SOKOL_API_IMPL const char* sapp_get_dropped_file_path(int index) { |
| 11504 | SOKOL_ASSERT(_sapp.drop.enabled); |
| 11505 | SOKOL_ASSERT((index >= 0) && (index < _sapp.drop.num_files)); |
| 11506 | SOKOL_ASSERT(_sapp.drop.buffer); |
| 11507 | if (!_sapp.drop.enabled) { |
| 11508 | return ""; |
| 11509 | } |
| 11510 | if ((index < 0) || (index >= _sapp.drop.max_files)) { |
| 11511 | return ""; |
| 11512 | } |
| 11513 | return (const char*) _sapp_dropped_file_path_ptr(index); |
| 11514 | } |
| 11515 | |
| 11516 | SOKOL_API_IMPL uint32_t sapp_html5_get_dropped_file_size(int index) { |
| 11517 | SOKOL_ASSERT(_sapp.drop.enabled); |
| 11518 | SOKOL_ASSERT((index >= 0) && (index < _sapp.drop.num_files)); |
| 11519 | #if defined(_SAPP_EMSCRIPTEN) |
| 11520 | if (!_sapp.drop.enabled) { |
| 11521 | return 0; |
| 11522 | } |
| 11523 | return sapp_js_dropped_file_size(index); |
| 11524 | #else |
| 11525 | (void)index; |
| 11526 | return 0; |
| 11527 | #endif |
| 11528 | } |
| 11529 | |
| 11530 | SOKOL_API_IMPL void sapp_html5_fetch_dropped_file(const sapp_html5_fetch_request* request) { |
| 11531 | SOKOL_ASSERT(_sapp.drop.enabled); |
| 11532 | SOKOL_ASSERT(request); |
| 11533 | SOKOL_ASSERT(request->callback); |
| 11534 | SOKOL_ASSERT(request->buffer.ptr); |
| 11535 | SOKOL_ASSERT(request->buffer.size > 0); |
| 11536 | #if defined(_SAPP_EMSCRIPTEN) |
| 11537 | const int index = request->dropped_file_index; |
| 11538 | sapp_html5_fetch_error error_code = SAPP_HTML5_FETCH_ERROR_NO_ERROR; |
| 11539 | if ((index < 0) || (index >= _sapp.drop.num_files)) { |
| 11540 | error_code = SAPP_HTML5_FETCH_ERROR_OTHER; |
| 11541 | } |
| 11542 | if (sapp_html5_get_dropped_file_size(index) > request->buffer.size) { |
| 11543 | error_code = SAPP_HTML5_FETCH_ERROR_BUFFER_TOO_SMALL; |
| 11544 | } |
| 11545 | if (SAPP_HTML5_FETCH_ERROR_NO_ERROR != error_code) { |
| 11546 | _sapp_emsc_invoke_fetch_cb(index, |
| 11547 | false, // success |
| 11548 | (int)error_code, |
| 11549 | request->callback, |
| 11550 | 0, // fetched_size |
| 11551 | (void*)request->buffer.ptr, |
| 11552 | request->buffer.size, |
| 11553 | request->user_data); |
| 11554 | } |
| 11555 | else { |
| 11556 | sapp_js_fetch_dropped_file(index, |
| 11557 | request->callback, |
| 11558 | (void*)request->buffer.ptr, |
| 11559 | request->buffer.size, |
| 11560 | request->user_data); |
| 11561 | } |
| 11562 | #else |
| 11563 | (void)request; |
| 11564 | #endif |
| 11565 | } |
| 11566 | |
| 11567 | SOKOL_API_IMPL const void* sapp_metal_get_device(void) { |
| 11568 | SOKOL_ASSERT(_sapp.valid); |
| 11569 | #if defined(SOKOL_METAL) |
| 11570 | #if defined(_SAPP_MACOS) |
| 11571 | const void* obj = (__bridge const void*) _sapp.macos.mtl_device; |
| 11572 | #else |
| 11573 | const void* obj = (__bridge const void*) _sapp.ios.mtl_device; |
| 11574 | #endif |
| 11575 | SOKOL_ASSERT(obj); |
| 11576 | return obj; |
| 11577 | #else |
| 11578 | return 0; |
| 11579 | #endif |
| 11580 | } |
| 11581 | |
| 11582 | SOKOL_API_IMPL const void* sapp_metal_get_current_drawable(void) { |
| 11583 | SOKOL_ASSERT(_sapp.valid); |
| 11584 | #if defined(SOKOL_METAL) |
| 11585 | #if defined(_SAPP_MACOS) |
| 11586 | const void* obj = (__bridge const void*) [_sapp.macos.view currentDrawable]; |
| 11587 | #else |
| 11588 | const void* obj = (__bridge const void*) [_sapp.ios.view currentDrawable]; |
| 11589 | #endif |
| 11590 | SOKOL_ASSERT(obj); |
| 11591 | return obj; |
| 11592 | #else |
| 11593 | return 0; |
| 11594 | #endif |
| 11595 | } |
| 11596 | |
| 11597 | SOKOL_API_IMPL const void* sapp_metal_get_depth_stencil_texture(void) { |
| 11598 | SOKOL_ASSERT(_sapp.valid); |
| 11599 | #if defined(SOKOL_METAL) |
| 11600 | #if defined(_SAPP_MACOS) |
| 11601 | const void* obj = (__bridge const void*) [_sapp.macos.view depthStencilTexture]; |
| 11602 | #else |
| 11603 | const void* obj = (__bridge const void*) [_sapp.ios.view depthStencilTexture]; |
| 11604 | #endif |
| 11605 | return obj; |
| 11606 | #else |
| 11607 | return 0; |
| 11608 | #endif |
| 11609 | } |
| 11610 | |
| 11611 | SOKOL_API_IMPL const void* sapp_metal_get_msaa_color_texture(void) { |
| 11612 | SOKOL_ASSERT(_sapp.valid); |
| 11613 | #if defined(SOKOL_METAL) |
| 11614 | #if defined(_SAPP_MACOS) |
| 11615 | const void* obj = (__bridge const void*) [_sapp.macos.view multisampleColorTexture]; |
| 11616 | #else |
| 11617 | const void* obj = (__bridge const void*) [_sapp.ios.view multisampleColorTexture]; |
| 11618 | #endif |
| 11619 | return obj; |
| 11620 | #else |
| 11621 | return 0; |
| 11622 | #endif |
| 11623 | } |
| 11624 | |
| 11625 | SOKOL_API_IMPL const void* sapp_macos_get_window(void) { |
| 11626 | #if defined(_SAPP_MACOS) |
| 11627 | const void* obj = (__bridge const void*) _sapp.macos.window; |
| 11628 | SOKOL_ASSERT(obj); |
| 11629 | return obj; |
| 11630 | #else |
| 11631 | return 0; |
| 11632 | #endif |
| 11633 | } |
| 11634 | |
| 11635 | SOKOL_API_IMPL const void* sapp_ios_get_window(void) { |
| 11636 | #if defined(_SAPP_IOS) |
| 11637 | const void* obj = (__bridge const void*) _sapp.ios.window; |
| 11638 | SOKOL_ASSERT(obj); |
| 11639 | return obj; |
| 11640 | #else |
| 11641 | return 0; |
| 11642 | #endif |
| 11643 | } |
| 11644 | |
| 11645 | SOKOL_API_IMPL const void* sapp_d3d11_get_device(void) { |
| 11646 | SOKOL_ASSERT(_sapp.valid); |
| 11647 | #if defined(SOKOL_D3D11) |
| 11648 | return _sapp.d3d11.device; |
| 11649 | #else |
| 11650 | return 0; |
| 11651 | #endif |
| 11652 | } |
| 11653 | |
| 11654 | SOKOL_API_IMPL const void* sapp_d3d11_get_device_context(void) { |
| 11655 | SOKOL_ASSERT(_sapp.valid); |
| 11656 | #if defined(SOKOL_D3D11) |
| 11657 | return _sapp.d3d11.device_context; |
| 11658 | #else |
| 11659 | return 0; |
| 11660 | #endif |
| 11661 | } |
| 11662 | |
| 11663 | SOKOL_API_IMPL const void* sapp_d3d11_get_swap_chain(void) { |
| 11664 | SOKOL_ASSERT(_sapp.valid); |
| 11665 | #if defined(SOKOL_D3D11) |
| 11666 | return _sapp.d3d11.swap_chain; |
| 11667 | #else |
| 11668 | return 0; |
| 11669 | #endif |
| 11670 | } |
| 11671 | |
| 11672 | SOKOL_API_IMPL const void* sapp_d3d11_get_render_view(void) { |
| 11673 | SOKOL_ASSERT(_sapp.valid); |
| 11674 | #if defined(SOKOL_D3D11) |
| 11675 | if (_sapp.sample_count > 1) { |
| 11676 | SOKOL_ASSERT(_sapp.d3d11.msaa_rtv); |
| 11677 | return _sapp.d3d11.msaa_rtv; |
| 11678 | } else { |
| 11679 | SOKOL_ASSERT(_sapp.d3d11.rtv); |
| 11680 | return _sapp.d3d11.rtv; |
| 11681 | } |
| 11682 | #else |
| 11683 | return 0; |
| 11684 | #endif |
| 11685 | } |
| 11686 | |
| 11687 | SOKOL_API_IMPL const void* sapp_d3d11_get_resolve_view(void) { |
| 11688 | SOKOL_ASSERT(_sapp.valid); |
| 11689 | #if defined(SOKOL_D3D11) |
| 11690 | if (_sapp.sample_count > 1) { |
| 11691 | SOKOL_ASSERT(_sapp.d3d11.rtv); |
| 11692 | return _sapp.d3d11.rtv; |
| 11693 | } else { |
| 11694 | return 0; |
| 11695 | } |
| 11696 | #else |
| 11697 | return 0; |
| 11698 | #endif |
| 11699 | } |
| 11700 | |
| 11701 | SOKOL_API_IMPL const void* sapp_d3d11_get_depth_stencil_view(void) { |
| 11702 | SOKOL_ASSERT(_sapp.valid); |
| 11703 | #if defined(SOKOL_D3D11) |
| 11704 | return _sapp.d3d11.dsv; |
| 11705 | #else |
| 11706 | return 0; |
| 11707 | #endif |
| 11708 | } |
| 11709 | |
| 11710 | SOKOL_API_IMPL const void* sapp_win32_get_hwnd(void) { |
| 11711 | SOKOL_ASSERT(_sapp.valid); |
| 11712 | #if defined(_SAPP_WIN32) |
| 11713 | return _sapp.win32.hwnd; |
| 11714 | #else |
| 11715 | return 0; |
| 11716 | #endif |
| 11717 | } |
| 11718 | |
| 11719 | SOKOL_API_IMPL const void* sapp_wgpu_get_device(void) { |
| 11720 | SOKOL_ASSERT(_sapp.valid); |
| 11721 | #if defined(_SAPP_EMSCRIPTEN) && defined(SOKOL_WGPU) |
| 11722 | return (const void*) _sapp.wgpu.device; |
| 11723 | #else |
| 11724 | return 0; |
| 11725 | #endif |
| 11726 | } |
| 11727 | |
| 11728 | SOKOL_API_IMPL const void* sapp_wgpu_get_render_view(void) { |
| 11729 | SOKOL_ASSERT(_sapp.valid); |
| 11730 | #if defined(_SAPP_EMSCRIPTEN) && defined(SOKOL_WGPU) |
| 11731 | if (_sapp.sample_count > 1) { |
| 11732 | SOKOL_ASSERT(_sapp.wgpu.msaa_view); |
| 11733 | return (const void*) _sapp.wgpu.msaa_view; |
| 11734 | } else { |
| 11735 | SOKOL_ASSERT(_sapp.wgpu.swapchain_view); |
| 11736 | return (const void*) _sapp.wgpu.swapchain_view; |
| 11737 | } |
| 11738 | #else |
| 11739 | return 0; |
| 11740 | #endif |
| 11741 | } |
| 11742 | |
| 11743 | SOKOL_API_IMPL const void* sapp_wgpu_get_resolve_view(void) { |
| 11744 | SOKOL_ASSERT(_sapp.valid); |
| 11745 | #if defined(_SAPP_EMSCRIPTEN) && defined(SOKOL_WGPU) |
| 11746 | if (_sapp.sample_count > 1) { |
| 11747 | SOKOL_ASSERT(_sapp.wgpu.swapchain_view); |
| 11748 | return (const void*) _sapp.wgpu.swapchain_view; |
| 11749 | } else { |
| 11750 | return 0; |
| 11751 | } |
| 11752 | #else |
| 11753 | return 0; |
| 11754 | #endif |
| 11755 | } |
| 11756 | |
| 11757 | SOKOL_API_IMPL const void* sapp_wgpu_get_depth_stencil_view(void) { |
| 11758 | SOKOL_ASSERT(_sapp.valid); |
| 11759 | #if defined(_SAPP_EMSCRIPTEN) && defined(SOKOL_WGPU) |
| 11760 | return (const void*) _sapp.wgpu.depth_stencil_view; |
| 11761 | #else |
| 11762 | return 0; |
| 11763 | #endif |
| 11764 | } |
| 11765 | |
| 11766 | SOKOL_API_IMPL uint32_t sapp_gl_get_framebuffer(void) { |
| 11767 | SOKOL_ASSERT(_sapp.valid); |
| 11768 | #if defined(_SAPP_ANY_GL) |
| 11769 | return _sapp.gl.framebuffer; |
| 11770 | #else |
| 11771 | return 0; |
| 11772 | #endif |
| 11773 | } |
| 11774 | |
| 11775 | SOKOL_API_IMPL int sapp_gl_get_major_version(void) { |
| 11776 | SOKOL_ASSERT(_sapp.valid); |
| 11777 | #if defined(SOKOL_GLCORE) |
| 11778 | return _sapp.desc.gl_major_version; |
| 11779 | #else |
| 11780 | return 0; |
| 11781 | #endif |
| 11782 | } |
| 11783 | |
| 11784 | SOKOL_API_IMPL int sapp_gl_get_minor_version(void) { |
| 11785 | SOKOL_ASSERT(_sapp.valid); |
| 11786 | #if defined(SOKOL_GLCORE) |
| 11787 | return _sapp.desc.gl_minor_version; |
| 11788 | #else |
| 11789 | return 0; |
| 11790 | #endif |
| 11791 | } |
| 11792 | |
| 11793 | SOKOL_API_IMPL const void* sapp_android_get_native_activity(void) { |
| 11794 | // NOTE: _sapp.valid is not asserted here because sapp_android_get_native_activity() |
| 11795 | // needs to be callable from within sokol_main() (see: https://github.com/floooh/sokol/issues/708) |
| 11796 | #if defined(_SAPP_ANDROID) |
| 11797 | return (void*)_sapp.android.activity; |
| 11798 | #else |
| 11799 | return 0; |
| 11800 | #endif |
| 11801 | } |
| 11802 | |
| 11803 | SOKOL_API_IMPL void sapp_html5_ask_leave_site(bool ask) { |
| 11804 | _sapp.html5_ask_leave_site = ask; |
| 11805 | } |
| 11806 | |
| 11807 | #endif /* SOKOL_APP_IMPL */ |