window.c (32882B)
1 //======================================================================== 2 // GLFW 3.3 - www.glfw.org 3 //------------------------------------------------------------------------ 4 // Copyright (c) 2002-2006 Marcus Geelnard 5 // Copyright (c) 2006-2019 Camilla Löwy <elmindreda@glfw.org> 6 // Copyright (c) 2012 Torsten Walluhn <tw@mad-cad.net> 7 // 8 // This software is provided 'as-is', without any express or implied 9 // warranty. In no event will the authors be held liable for any damages 10 // arising from the use of this software. 11 // 12 // Permission is granted to anyone to use this software for any purpose, 13 // including commercial applications, and to alter it and redistribute it 14 // freely, subject to the following restrictions: 15 // 16 // 1. The origin of this software must not be misrepresented; you must not 17 // claim that you wrote the original software. If you use this software 18 // in a product, an acknowledgment in the product documentation would 19 // be appreciated but is not required. 20 // 21 // 2. Altered source versions must be plainly marked as such, and must not 22 // be misrepresented as being the original software. 23 // 24 // 3. This notice may not be removed or altered from any source 25 // distribution. 26 // 27 //======================================================================== 28 // Please use C89 style variable declarations in this file because VS 2010 29 //======================================================================== 30 31 #include "internal.h" 32 33 #include <assert.h> 34 #include <string.h> 35 #include <stdlib.h> 36 #include <float.h> 37 38 39 ////////////////////////////////////////////////////////////////////////// 40 ////// GLFW event API ////// 41 ////////////////////////////////////////////////////////////////////////// 42 43 // Notifies shared code that a window has lost or received input focus 44 // 45 void _glfwInputWindowFocus(_GLFWwindow* window, GLFWbool focused) 46 { 47 if (window->callbacks.focus) 48 window->callbacks.focus((GLFWwindow*) window, focused); 49 50 if (!focused) 51 { 52 int key, button; 53 54 for (key = 0; key <= GLFW_KEY_LAST; key++) 55 { 56 if (window->keys[key] == GLFW_PRESS) 57 { 58 const int scancode = _glfwPlatformGetKeyScancode(key); 59 _glfwInputKey(window, key, scancode, GLFW_RELEASE, 0); 60 } 61 } 62 63 for (button = 0; button <= GLFW_MOUSE_BUTTON_LAST; button++) 64 { 65 if (window->mouseButtons[button] == GLFW_PRESS) 66 _glfwInputMouseClick(window, button, GLFW_RELEASE, 0); 67 } 68 } 69 } 70 71 // Notifies shared code that a window has moved 72 // The position is specified in content area relative screen coordinates 73 // 74 void _glfwInputWindowPos(_GLFWwindow* window, int x, int y) 75 { 76 if (window->callbacks.pos) 77 window->callbacks.pos((GLFWwindow*) window, x, y); 78 } 79 80 // Notifies shared code that a window has been resized 81 // The size is specified in screen coordinates 82 // 83 void _glfwInputWindowSize(_GLFWwindow* window, int width, int height) 84 { 85 if (window->callbacks.size) 86 window->callbacks.size((GLFWwindow*) window, width, height); 87 } 88 89 // Notifies shared code that a window has been iconified or restored 90 // 91 void _glfwInputWindowIconify(_GLFWwindow* window, GLFWbool iconified) 92 { 93 if (window->callbacks.iconify) 94 window->callbacks.iconify((GLFWwindow*) window, iconified); 95 } 96 97 // Notifies shared code that a window has been maximized or restored 98 // 99 void _glfwInputWindowMaximize(_GLFWwindow* window, GLFWbool maximized) 100 { 101 if (window->callbacks.maximize) 102 window->callbacks.maximize((GLFWwindow*) window, maximized); 103 } 104 105 // Notifies shared code that a window framebuffer has been resized 106 // The size is specified in pixels 107 // 108 void _glfwInputFramebufferSize(_GLFWwindow* window, int width, int height) 109 { 110 if (window->callbacks.fbsize) 111 window->callbacks.fbsize((GLFWwindow*) window, width, height); 112 } 113 114 // Notifies shared code that a window content scale has changed 115 // The scale is specified as the ratio between the current and default DPI 116 // 117 void _glfwInputWindowContentScale(_GLFWwindow* window, float xscale, float yscale) 118 { 119 if (window->callbacks.scale) 120 window->callbacks.scale((GLFWwindow*) window, xscale, yscale); 121 } 122 123 // Notifies shared code that the window contents needs updating 124 // 125 void _glfwInputWindowDamage(_GLFWwindow* window) 126 { 127 if (window->callbacks.refresh) 128 window->callbacks.refresh((GLFWwindow*) window); 129 } 130 131 // Notifies shared code that the user wishes to close a window 132 // 133 void _glfwInputWindowCloseRequest(_GLFWwindow* window) 134 { 135 window->shouldClose = GLFW_TRUE; 136 137 if (window->callbacks.close) 138 window->callbacks.close((GLFWwindow*) window); 139 } 140 141 // Notifies shared code that a window has changed its desired monitor 142 // 143 void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor) 144 { 145 window->monitor = monitor; 146 } 147 148 ////////////////////////////////////////////////////////////////////////// 149 ////// GLFW public API ////// 150 ////////////////////////////////////////////////////////////////////////// 151 152 GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height, 153 const char* title, 154 GLFWmonitor* monitor, 155 GLFWwindow* share) 156 { 157 _GLFWfbconfig fbconfig; 158 _GLFWctxconfig ctxconfig; 159 _GLFWwndconfig wndconfig; 160 _GLFWwindow* window; 161 162 assert(title != NULL); 163 assert(width >= 0); 164 assert(height >= 0); 165 166 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 167 168 if (width <= 0 || height <= 0) 169 { 170 _glfwInputError(GLFW_INVALID_VALUE, 171 "Invalid window size %ix%i", 172 width, height); 173 174 return NULL; 175 } 176 177 fbconfig = _glfw.hints.framebuffer; 178 ctxconfig = _glfw.hints.context; 179 wndconfig = _glfw.hints.window; 180 181 wndconfig.width = width; 182 wndconfig.height = height; 183 wndconfig.title = title; 184 ctxconfig.share = (_GLFWwindow*) share; 185 186 if (!_glfwIsValidContextConfig(&ctxconfig)) 187 return NULL; 188 189 window = calloc(1, sizeof(_GLFWwindow)); 190 window->next = _glfw.windowListHead; 191 _glfw.windowListHead = window; 192 193 window->videoMode.width = width; 194 window->videoMode.height = height; 195 window->videoMode.redBits = fbconfig.redBits; 196 window->videoMode.greenBits = fbconfig.greenBits; 197 window->videoMode.blueBits = fbconfig.blueBits; 198 window->videoMode.refreshRate = _glfw.hints.refreshRate; 199 200 window->monitor = (_GLFWmonitor*) monitor; 201 window->resizable = wndconfig.resizable; 202 window->decorated = wndconfig.decorated; 203 window->autoIconify = wndconfig.autoIconify; 204 window->floating = wndconfig.floating; 205 window->focusOnShow = wndconfig.focusOnShow; 206 window->cursorMode = GLFW_CURSOR_NORMAL; 207 208 window->doublebuffer = fbconfig.doublebuffer; 209 210 window->minwidth = GLFW_DONT_CARE; 211 window->minheight = GLFW_DONT_CARE; 212 window->maxwidth = GLFW_DONT_CARE; 213 window->maxheight = GLFW_DONT_CARE; 214 window->numer = GLFW_DONT_CARE; 215 window->denom = GLFW_DONT_CARE; 216 217 // Open the actual window and create its context 218 if (!_glfwPlatformCreateWindow(window, &wndconfig, &ctxconfig, &fbconfig)) 219 { 220 glfwDestroyWindow((GLFWwindow*) window); 221 return NULL; 222 } 223 224 return (GLFWwindow*) window; 225 } 226 227 void glfwDefaultWindowHints(void) 228 { 229 _GLFW_REQUIRE_INIT(); 230 231 // The default is OpenGL with minimum version 1.0 232 memset(&_glfw.hints.context, 0, sizeof(_glfw.hints.context)); 233 _glfw.hints.context.client = GLFW_OPENGL_API; 234 _glfw.hints.context.source = GLFW_NATIVE_CONTEXT_API; 235 _glfw.hints.context.major = 1; 236 _glfw.hints.context.minor = 0; 237 238 // The default is a focused, visible, resizable window with decorations 239 memset(&_glfw.hints.window, 0, sizeof(_glfw.hints.window)); 240 _glfw.hints.window.resizable = GLFW_TRUE; 241 _glfw.hints.window.visible = GLFW_TRUE; 242 _glfw.hints.window.decorated = GLFW_TRUE; 243 _glfw.hints.window.focused = GLFW_TRUE; 244 _glfw.hints.window.autoIconify = GLFW_TRUE; 245 _glfw.hints.window.centerCursor = GLFW_TRUE; 246 _glfw.hints.window.focusOnShow = GLFW_TRUE; 247 248 // The default is 24 bits of color, 24 bits of depth and 8 bits of stencil, 249 // double buffered 250 memset(&_glfw.hints.framebuffer, 0, sizeof(_glfw.hints.framebuffer)); 251 _glfw.hints.framebuffer.redBits = 8; 252 _glfw.hints.framebuffer.greenBits = 8; 253 _glfw.hints.framebuffer.blueBits = 8; 254 _glfw.hints.framebuffer.alphaBits = 8; 255 _glfw.hints.framebuffer.depthBits = 24; 256 _glfw.hints.framebuffer.stencilBits = 8; 257 _glfw.hints.framebuffer.doublebuffer = GLFW_TRUE; 258 259 // The default is to select the highest available refresh rate 260 _glfw.hints.refreshRate = GLFW_DONT_CARE; 261 262 // The default is to use full Retina resolution framebuffers 263 _glfw.hints.window.ns.retina = GLFW_TRUE; 264 } 265 266 GLFWAPI void glfwWindowHint(int hint, int value) 267 { 268 _GLFW_REQUIRE_INIT(); 269 270 switch (hint) 271 { 272 case GLFW_RED_BITS: 273 _glfw.hints.framebuffer.redBits = value; 274 return; 275 case GLFW_GREEN_BITS: 276 _glfw.hints.framebuffer.greenBits = value; 277 return; 278 case GLFW_BLUE_BITS: 279 _glfw.hints.framebuffer.blueBits = value; 280 return; 281 case GLFW_ALPHA_BITS: 282 _glfw.hints.framebuffer.alphaBits = value; 283 return; 284 case GLFW_DEPTH_BITS: 285 _glfw.hints.framebuffer.depthBits = value; 286 return; 287 case GLFW_STENCIL_BITS: 288 _glfw.hints.framebuffer.stencilBits = value; 289 return; 290 case GLFW_ACCUM_RED_BITS: 291 _glfw.hints.framebuffer.accumRedBits = value; 292 return; 293 case GLFW_ACCUM_GREEN_BITS: 294 _glfw.hints.framebuffer.accumGreenBits = value; 295 return; 296 case GLFW_ACCUM_BLUE_BITS: 297 _glfw.hints.framebuffer.accumBlueBits = value; 298 return; 299 case GLFW_ACCUM_ALPHA_BITS: 300 _glfw.hints.framebuffer.accumAlphaBits = value; 301 return; 302 case GLFW_AUX_BUFFERS: 303 _glfw.hints.framebuffer.auxBuffers = value; 304 return; 305 case GLFW_STEREO: 306 _glfw.hints.framebuffer.stereo = value ? GLFW_TRUE : GLFW_FALSE; 307 return; 308 case GLFW_DOUBLEBUFFER: 309 _glfw.hints.framebuffer.doublebuffer = value ? GLFW_TRUE : GLFW_FALSE; 310 return; 311 case GLFW_TRANSPARENT_FRAMEBUFFER: 312 _glfw.hints.framebuffer.transparent = value ? GLFW_TRUE : GLFW_FALSE; 313 return; 314 case GLFW_SAMPLES: 315 _glfw.hints.framebuffer.samples = value; 316 return; 317 case GLFW_SRGB_CAPABLE: 318 _glfw.hints.framebuffer.sRGB = value ? GLFW_TRUE : GLFW_FALSE; 319 return; 320 case GLFW_RESIZABLE: 321 _glfw.hints.window.resizable = value ? GLFW_TRUE : GLFW_FALSE; 322 return; 323 case GLFW_DECORATED: 324 _glfw.hints.window.decorated = value ? GLFW_TRUE : GLFW_FALSE; 325 return; 326 case GLFW_FOCUSED: 327 _glfw.hints.window.focused = value ? GLFW_TRUE : GLFW_FALSE; 328 return; 329 case GLFW_AUTO_ICONIFY: 330 _glfw.hints.window.autoIconify = value ? GLFW_TRUE : GLFW_FALSE; 331 return; 332 case GLFW_FLOATING: 333 _glfw.hints.window.floating = value ? GLFW_TRUE : GLFW_FALSE; 334 return; 335 case GLFW_MAXIMIZED: 336 _glfw.hints.window.maximized = value ? GLFW_TRUE : GLFW_FALSE; 337 return; 338 case GLFW_VISIBLE: 339 _glfw.hints.window.visible = value ? GLFW_TRUE : GLFW_FALSE; 340 return; 341 case GLFW_COCOA_RETINA_FRAMEBUFFER: 342 _glfw.hints.window.ns.retina = value ? GLFW_TRUE : GLFW_FALSE; 343 return; 344 case GLFW_COCOA_GRAPHICS_SWITCHING: 345 _glfw.hints.context.nsgl.offline = value ? GLFW_TRUE : GLFW_FALSE; 346 return; 347 case GLFW_SCALE_TO_MONITOR: 348 _glfw.hints.window.scaleToMonitor = value ? GLFW_TRUE : GLFW_FALSE; 349 return; 350 case GLFW_CENTER_CURSOR: 351 _glfw.hints.window.centerCursor = value ? GLFW_TRUE : GLFW_FALSE; 352 return; 353 case GLFW_FOCUS_ON_SHOW: 354 _glfw.hints.window.focusOnShow = value ? GLFW_TRUE : GLFW_FALSE; 355 return; 356 case GLFW_CLIENT_API: 357 _glfw.hints.context.client = value; 358 return; 359 case GLFW_CONTEXT_CREATION_API: 360 _glfw.hints.context.source = value; 361 return; 362 case GLFW_CONTEXT_VERSION_MAJOR: 363 _glfw.hints.context.major = value; 364 return; 365 case GLFW_CONTEXT_VERSION_MINOR: 366 _glfw.hints.context.minor = value; 367 return; 368 case GLFW_CONTEXT_ROBUSTNESS: 369 _glfw.hints.context.robustness = value; 370 return; 371 case GLFW_OPENGL_FORWARD_COMPAT: 372 _glfw.hints.context.forward = value ? GLFW_TRUE : GLFW_FALSE; 373 return; 374 case GLFW_OPENGL_DEBUG_CONTEXT: 375 _glfw.hints.context.debug = value ? GLFW_TRUE : GLFW_FALSE; 376 return; 377 case GLFW_CONTEXT_NO_ERROR: 378 _glfw.hints.context.noerror = value ? GLFW_TRUE : GLFW_FALSE; 379 return; 380 case GLFW_OPENGL_PROFILE: 381 _glfw.hints.context.profile = value; 382 return; 383 case GLFW_CONTEXT_RELEASE_BEHAVIOR: 384 _glfw.hints.context.release = value; 385 return; 386 case GLFW_REFRESH_RATE: 387 _glfw.hints.refreshRate = value; 388 return; 389 } 390 391 _glfwInputError(GLFW_INVALID_ENUM, "Invalid window hint 0x%08X", hint); 392 } 393 394 GLFWAPI void glfwWindowHintString(int hint, const char* value) 395 { 396 assert(value != NULL); 397 398 _GLFW_REQUIRE_INIT(); 399 400 switch (hint) 401 { 402 case GLFW_COCOA_FRAME_NAME: 403 strncpy(_glfw.hints.window.ns.frameName, value, 404 sizeof(_glfw.hints.window.ns.frameName) - 1); 405 return; 406 case GLFW_X11_CLASS_NAME: 407 strncpy(_glfw.hints.window.x11.className, value, 408 sizeof(_glfw.hints.window.x11.className) - 1); 409 return; 410 case GLFW_X11_INSTANCE_NAME: 411 strncpy(_glfw.hints.window.x11.instanceName, value, 412 sizeof(_glfw.hints.window.x11.instanceName) - 1); 413 return; 414 } 415 416 _glfwInputError(GLFW_INVALID_ENUM, "Invalid window hint string 0x%08X", hint); 417 } 418 419 GLFWAPI void glfwDestroyWindow(GLFWwindow* handle) 420 { 421 _GLFWwindow* window = (_GLFWwindow*) handle; 422 423 _GLFW_REQUIRE_INIT(); 424 425 // Allow closing of NULL (to match the behavior of free) 426 if (window == NULL) 427 return; 428 429 // Clear all callbacks to avoid exposing a half torn-down window object 430 memset(&window->callbacks, 0, sizeof(window->callbacks)); 431 432 // The window's context must not be current on another thread when the 433 // window is destroyed 434 if (window == _glfwPlatformGetTls(&_glfw.contextSlot)) 435 glfwMakeContextCurrent(NULL); 436 437 _glfwPlatformDestroyWindow(window); 438 439 // Unlink window from global linked list 440 { 441 _GLFWwindow** prev = &_glfw.windowListHead; 442 443 while (*prev != window) 444 prev = &((*prev)->next); 445 446 *prev = window->next; 447 } 448 449 free(window); 450 } 451 452 GLFWAPI int glfwWindowShouldClose(GLFWwindow* handle) 453 { 454 _GLFWwindow* window = (_GLFWwindow*) handle; 455 assert(window != NULL); 456 457 _GLFW_REQUIRE_INIT_OR_RETURN(0); 458 return window->shouldClose; 459 } 460 461 GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* handle, int value) 462 { 463 _GLFWwindow* window = (_GLFWwindow*) handle; 464 assert(window != NULL); 465 466 _GLFW_REQUIRE_INIT(); 467 window->shouldClose = value; 468 } 469 470 GLFWAPI void glfwSetWindowTitle(GLFWwindow* handle, const char* title) 471 { 472 _GLFWwindow* window = (_GLFWwindow*) handle; 473 assert(window != NULL); 474 assert(title != NULL); 475 476 _GLFW_REQUIRE_INIT(); 477 _glfwPlatformSetWindowTitle(window, title); 478 } 479 480 GLFWAPI void glfwSetWindowIcon(GLFWwindow* handle, 481 int count, const GLFWimage* images) 482 { 483 int i; 484 _GLFWwindow* window = (_GLFWwindow*) handle; 485 486 assert(window != NULL); 487 assert(count >= 0); 488 assert(count == 0 || images != NULL); 489 490 _GLFW_REQUIRE_INIT(); 491 492 if (count < 0) 493 { 494 _glfwInputError(GLFW_INVALID_VALUE, "Invalid image count for window icon"); 495 return; 496 } 497 498 for (i = 0; i < count; i++) 499 { 500 assert(images[i].pixels != NULL); 501 502 if (images[i].width <= 0 || images[i].height <= 0) 503 { 504 _glfwInputError(GLFW_INVALID_VALUE, 505 "Invalid image dimensions for window icon"); 506 return; 507 } 508 } 509 510 _glfwPlatformSetWindowIcon(window, count, images); 511 } 512 513 GLFWAPI void glfwGetWindowPos(GLFWwindow* handle, int* xpos, int* ypos) 514 { 515 _GLFWwindow* window = (_GLFWwindow*) handle; 516 assert(window != NULL); 517 518 if (xpos) 519 *xpos = 0; 520 if (ypos) 521 *ypos = 0; 522 523 _GLFW_REQUIRE_INIT(); 524 _glfwPlatformGetWindowPos(window, xpos, ypos); 525 } 526 527 GLFWAPI void glfwSetWindowPos(GLFWwindow* handle, int xpos, int ypos) 528 { 529 _GLFWwindow* window = (_GLFWwindow*) handle; 530 assert(window != NULL); 531 532 _GLFW_REQUIRE_INIT(); 533 534 if (window->monitor) 535 return; 536 537 _glfwPlatformSetWindowPos(window, xpos, ypos); 538 } 539 540 GLFWAPI void glfwGetWindowSize(GLFWwindow* handle, int* width, int* height) 541 { 542 _GLFWwindow* window = (_GLFWwindow*) handle; 543 assert(window != NULL); 544 545 if (width) 546 *width = 0; 547 if (height) 548 *height = 0; 549 550 _GLFW_REQUIRE_INIT(); 551 _glfwPlatformGetWindowSize(window, width, height); 552 } 553 554 GLFWAPI void glfwSetWindowSize(GLFWwindow* handle, int width, int height) 555 { 556 _GLFWwindow* window = (_GLFWwindow*) handle; 557 assert(window != NULL); 558 assert(width >= 0); 559 assert(height >= 0); 560 561 _GLFW_REQUIRE_INIT(); 562 563 window->videoMode.width = width; 564 window->videoMode.height = height; 565 566 _glfwPlatformSetWindowSize(window, width, height); 567 } 568 569 GLFWAPI void glfwSetWindowSizeLimits(GLFWwindow* handle, 570 int minwidth, int minheight, 571 int maxwidth, int maxheight) 572 { 573 _GLFWwindow* window = (_GLFWwindow*) handle; 574 assert(window != NULL); 575 576 _GLFW_REQUIRE_INIT(); 577 578 if (minwidth != GLFW_DONT_CARE && minheight != GLFW_DONT_CARE) 579 { 580 if (minwidth < 0 || minheight < 0) 581 { 582 _glfwInputError(GLFW_INVALID_VALUE, 583 "Invalid window minimum size %ix%i", 584 minwidth, minheight); 585 return; 586 } 587 } 588 589 if (maxwidth != GLFW_DONT_CARE && maxheight != GLFW_DONT_CARE) 590 { 591 if (maxwidth < 0 || maxheight < 0 || 592 maxwidth < minwidth || maxheight < minheight) 593 { 594 _glfwInputError(GLFW_INVALID_VALUE, 595 "Invalid window maximum size %ix%i", 596 maxwidth, maxheight); 597 return; 598 } 599 } 600 601 window->minwidth = minwidth; 602 window->minheight = minheight; 603 window->maxwidth = maxwidth; 604 window->maxheight = maxheight; 605 606 if (window->monitor || !window->resizable) 607 return; 608 609 _glfwPlatformSetWindowSizeLimits(window, 610 minwidth, minheight, 611 maxwidth, maxheight); 612 } 613 614 GLFWAPI void glfwSetWindowAspectRatio(GLFWwindow* handle, int numer, int denom) 615 { 616 _GLFWwindow* window = (_GLFWwindow*) handle; 617 assert(window != NULL); 618 assert(numer != 0); 619 assert(denom != 0); 620 621 _GLFW_REQUIRE_INIT(); 622 623 if (numer != GLFW_DONT_CARE && denom != GLFW_DONT_CARE) 624 { 625 if (numer <= 0 || denom <= 0) 626 { 627 _glfwInputError(GLFW_INVALID_VALUE, 628 "Invalid window aspect ratio %i:%i", 629 numer, denom); 630 return; 631 } 632 } 633 634 window->numer = numer; 635 window->denom = denom; 636 637 if (window->monitor || !window->resizable) 638 return; 639 640 _glfwPlatformSetWindowAspectRatio(window, numer, denom); 641 } 642 643 GLFWAPI void glfwGetFramebufferSize(GLFWwindow* handle, int* width, int* height) 644 { 645 _GLFWwindow* window = (_GLFWwindow*) handle; 646 assert(window != NULL); 647 648 if (width) 649 *width = 0; 650 if (height) 651 *height = 0; 652 653 _GLFW_REQUIRE_INIT(); 654 _glfwPlatformGetFramebufferSize(window, width, height); 655 } 656 657 GLFWAPI void glfwGetWindowFrameSize(GLFWwindow* handle, 658 int* left, int* top, 659 int* right, int* bottom) 660 { 661 _GLFWwindow* window = (_GLFWwindow*) handle; 662 assert(window != NULL); 663 664 if (left) 665 *left = 0; 666 if (top) 667 *top = 0; 668 if (right) 669 *right = 0; 670 if (bottom) 671 *bottom = 0; 672 673 _GLFW_REQUIRE_INIT(); 674 _glfwPlatformGetWindowFrameSize(window, left, top, right, bottom); 675 } 676 677 GLFWAPI void glfwGetWindowContentScale(GLFWwindow* handle, 678 float* xscale, float* yscale) 679 { 680 _GLFWwindow* window = (_GLFWwindow*) handle; 681 assert(window != NULL); 682 683 if (xscale) 684 *xscale = 0.f; 685 if (yscale) 686 *yscale = 0.f; 687 688 _GLFW_REQUIRE_INIT(); 689 _glfwPlatformGetWindowContentScale(window, xscale, yscale); 690 } 691 692 GLFWAPI float glfwGetWindowOpacity(GLFWwindow* handle) 693 { 694 _GLFWwindow* window = (_GLFWwindow*) handle; 695 assert(window != NULL); 696 697 _GLFW_REQUIRE_INIT_OR_RETURN(1.f); 698 return _glfwPlatformGetWindowOpacity(window); 699 } 700 701 GLFWAPI void glfwSetWindowOpacity(GLFWwindow* handle, float opacity) 702 { 703 _GLFWwindow* window = (_GLFWwindow*) handle; 704 assert(window != NULL); 705 assert(opacity == opacity); 706 assert(opacity >= 0.f); 707 assert(opacity <= 1.f); 708 709 _GLFW_REQUIRE_INIT(); 710 711 if (opacity != opacity || opacity < 0.f || opacity > 1.f) 712 { 713 _glfwInputError(GLFW_INVALID_VALUE, "Invalid window opacity %f", opacity); 714 return; 715 } 716 717 _glfwPlatformSetWindowOpacity(window, opacity); 718 } 719 720 GLFWAPI void glfwIconifyWindow(GLFWwindow* handle) 721 { 722 _GLFWwindow* window = (_GLFWwindow*) handle; 723 assert(window != NULL); 724 725 _GLFW_REQUIRE_INIT(); 726 _glfwPlatformIconifyWindow(window); 727 } 728 729 GLFWAPI void glfwRestoreWindow(GLFWwindow* handle) 730 { 731 _GLFWwindow* window = (_GLFWwindow*) handle; 732 assert(window != NULL); 733 734 _GLFW_REQUIRE_INIT(); 735 _glfwPlatformRestoreWindow(window); 736 } 737 738 GLFWAPI void glfwMaximizeWindow(GLFWwindow* handle) 739 { 740 _GLFWwindow* window = (_GLFWwindow*) handle; 741 assert(window != NULL); 742 743 _GLFW_REQUIRE_INIT(); 744 745 if (window->monitor) 746 return; 747 748 _glfwPlatformMaximizeWindow(window); 749 } 750 751 GLFWAPI void glfwShowWindow(GLFWwindow* handle) 752 { 753 _GLFWwindow* window = (_GLFWwindow*) handle; 754 assert(window != NULL); 755 756 _GLFW_REQUIRE_INIT(); 757 758 if (window->monitor) 759 return; 760 761 _glfwPlatformShowWindow(window); 762 763 if (window->focusOnShow) 764 _glfwPlatformFocusWindow(window); 765 } 766 767 GLFWAPI void glfwRequestWindowAttention(GLFWwindow* handle) 768 { 769 _GLFWwindow* window = (_GLFWwindow*) handle; 770 assert(window != NULL); 771 772 _GLFW_REQUIRE_INIT(); 773 774 _glfwPlatformRequestWindowAttention(window); 775 } 776 777 GLFWAPI void glfwHideWindow(GLFWwindow* handle) 778 { 779 _GLFWwindow* window = (_GLFWwindow*) handle; 780 assert(window != NULL); 781 782 _GLFW_REQUIRE_INIT(); 783 784 if (window->monitor) 785 return; 786 787 _glfwPlatformHideWindow(window); 788 } 789 790 GLFWAPI void glfwFocusWindow(GLFWwindow* handle) 791 { 792 _GLFWwindow* window = (_GLFWwindow*) handle; 793 assert(window != NULL); 794 795 _GLFW_REQUIRE_INIT(); 796 797 _glfwPlatformFocusWindow(window); 798 } 799 800 GLFWAPI int glfwGetWindowAttrib(GLFWwindow* handle, int attrib) 801 { 802 _GLFWwindow* window = (_GLFWwindow*) handle; 803 assert(window != NULL); 804 805 _GLFW_REQUIRE_INIT_OR_RETURN(0); 806 807 switch (attrib) 808 { 809 case GLFW_FOCUSED: 810 return _glfwPlatformWindowFocused(window); 811 case GLFW_ICONIFIED: 812 return _glfwPlatformWindowIconified(window); 813 case GLFW_VISIBLE: 814 return _glfwPlatformWindowVisible(window); 815 case GLFW_MAXIMIZED: 816 return _glfwPlatformWindowMaximized(window); 817 case GLFW_HOVERED: 818 return _glfwPlatformWindowHovered(window); 819 case GLFW_FOCUS_ON_SHOW: 820 return window->focusOnShow; 821 case GLFW_TRANSPARENT_FRAMEBUFFER: 822 return _glfwPlatformFramebufferTransparent(window); 823 case GLFW_RESIZABLE: 824 return window->resizable; 825 case GLFW_DECORATED: 826 return window->decorated; 827 case GLFW_FLOATING: 828 return window->floating; 829 case GLFW_AUTO_ICONIFY: 830 return window->autoIconify; 831 case GLFW_CLIENT_API: 832 return window->context.client; 833 case GLFW_CONTEXT_CREATION_API: 834 return window->context.source; 835 case GLFW_CONTEXT_VERSION_MAJOR: 836 return window->context.major; 837 case GLFW_CONTEXT_VERSION_MINOR: 838 return window->context.minor; 839 case GLFW_CONTEXT_REVISION: 840 return window->context.revision; 841 case GLFW_CONTEXT_ROBUSTNESS: 842 return window->context.robustness; 843 case GLFW_OPENGL_FORWARD_COMPAT: 844 return window->context.forward; 845 case GLFW_OPENGL_DEBUG_CONTEXT: 846 return window->context.debug; 847 case GLFW_OPENGL_PROFILE: 848 return window->context.profile; 849 case GLFW_CONTEXT_RELEASE_BEHAVIOR: 850 return window->context.release; 851 case GLFW_CONTEXT_NO_ERROR: 852 return window->context.noerror; 853 } 854 855 _glfwInputError(GLFW_INVALID_ENUM, "Invalid window attribute 0x%08X", attrib); 856 return 0; 857 } 858 859 GLFWAPI void glfwSetWindowAttrib(GLFWwindow* handle, int attrib, int value) 860 { 861 _GLFWwindow* window = (_GLFWwindow*) handle; 862 assert(window != NULL); 863 864 _GLFW_REQUIRE_INIT(); 865 866 value = value ? GLFW_TRUE : GLFW_FALSE; 867 868 if (attrib == GLFW_AUTO_ICONIFY) 869 window->autoIconify = value; 870 else if (attrib == GLFW_RESIZABLE) 871 { 872 if (window->resizable == value) 873 return; 874 875 window->resizable = value; 876 if (!window->monitor) 877 _glfwPlatformSetWindowResizable(window, value); 878 } 879 else if (attrib == GLFW_DECORATED) 880 { 881 if (window->decorated == value) 882 return; 883 884 window->decorated = value; 885 if (!window->monitor) 886 _glfwPlatformSetWindowDecorated(window, value); 887 } 888 else if (attrib == GLFW_FLOATING) 889 { 890 if (window->floating == value) 891 return; 892 893 window->floating = value; 894 if (!window->monitor) 895 _glfwPlatformSetWindowFloating(window, value); 896 } 897 else if (attrib == GLFW_FOCUS_ON_SHOW) 898 window->focusOnShow = value; 899 else 900 _glfwInputError(GLFW_INVALID_ENUM, "Invalid window attribute 0x%08X", attrib); 901 } 902 903 GLFWAPI GLFWmonitor* glfwGetWindowMonitor(GLFWwindow* handle) 904 { 905 _GLFWwindow* window = (_GLFWwindow*) handle; 906 assert(window != NULL); 907 908 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 909 return (GLFWmonitor*) window->monitor; 910 } 911 912 GLFWAPI void glfwSetWindowMonitor(GLFWwindow* wh, 913 GLFWmonitor* mh, 914 int xpos, int ypos, 915 int width, int height, 916 int refreshRate) 917 { 918 _GLFWwindow* window = (_GLFWwindow*) wh; 919 _GLFWmonitor* monitor = (_GLFWmonitor*) mh; 920 assert(window != NULL); 921 assert(width >= 0); 922 assert(height >= 0); 923 924 _GLFW_REQUIRE_INIT(); 925 926 if (width <= 0 || height <= 0) 927 { 928 _glfwInputError(GLFW_INVALID_VALUE, 929 "Invalid window size %ix%i", 930 width, height); 931 return; 932 } 933 934 if (refreshRate < 0 && refreshRate != GLFW_DONT_CARE) 935 { 936 _glfwInputError(GLFW_INVALID_VALUE, 937 "Invalid refresh rate %i", 938 refreshRate); 939 return; 940 } 941 942 window->videoMode.width = width; 943 window->videoMode.height = height; 944 window->videoMode.refreshRate = refreshRate; 945 946 _glfwPlatformSetWindowMonitor(window, monitor, 947 xpos, ypos, width, height, 948 refreshRate); 949 } 950 951 GLFWAPI void glfwSetWindowUserPointer(GLFWwindow* handle, void* pointer) 952 { 953 _GLFWwindow* window = (_GLFWwindow*) handle; 954 assert(window != NULL); 955 956 _GLFW_REQUIRE_INIT(); 957 window->userPointer = pointer; 958 } 959 960 GLFWAPI void* glfwGetWindowUserPointer(GLFWwindow* handle) 961 { 962 _GLFWwindow* window = (_GLFWwindow*) handle; 963 assert(window != NULL); 964 965 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 966 return window->userPointer; 967 } 968 969 GLFWAPI GLFWwindowposfun glfwSetWindowPosCallback(GLFWwindow* handle, 970 GLFWwindowposfun cbfun) 971 { 972 _GLFWwindow* window = (_GLFWwindow*) handle; 973 assert(window != NULL); 974 975 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 976 _GLFW_SWAP_POINTERS(window->callbacks.pos, cbfun); 977 return cbfun; 978 } 979 980 GLFWAPI GLFWwindowsizefun glfwSetWindowSizeCallback(GLFWwindow* handle, 981 GLFWwindowsizefun cbfun) 982 { 983 _GLFWwindow* window = (_GLFWwindow*) handle; 984 assert(window != NULL); 985 986 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 987 _GLFW_SWAP_POINTERS(window->callbacks.size, cbfun); 988 return cbfun; 989 } 990 991 GLFWAPI GLFWwindowclosefun glfwSetWindowCloseCallback(GLFWwindow* handle, 992 GLFWwindowclosefun cbfun) 993 { 994 _GLFWwindow* window = (_GLFWwindow*) handle; 995 assert(window != NULL); 996 997 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 998 _GLFW_SWAP_POINTERS(window->callbacks.close, cbfun); 999 return cbfun; 1000 } 1001 1002 GLFWAPI GLFWwindowrefreshfun glfwSetWindowRefreshCallback(GLFWwindow* handle, 1003 GLFWwindowrefreshfun cbfun) 1004 { 1005 _GLFWwindow* window = (_GLFWwindow*) handle; 1006 assert(window != NULL); 1007 1008 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 1009 _GLFW_SWAP_POINTERS(window->callbacks.refresh, cbfun); 1010 return cbfun; 1011 } 1012 1013 GLFWAPI GLFWwindowfocusfun glfwSetWindowFocusCallback(GLFWwindow* handle, 1014 GLFWwindowfocusfun cbfun) 1015 { 1016 _GLFWwindow* window = (_GLFWwindow*) handle; 1017 assert(window != NULL); 1018 1019 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 1020 _GLFW_SWAP_POINTERS(window->callbacks.focus, cbfun); 1021 return cbfun; 1022 } 1023 1024 GLFWAPI GLFWwindowiconifyfun glfwSetWindowIconifyCallback(GLFWwindow* handle, 1025 GLFWwindowiconifyfun cbfun) 1026 { 1027 _GLFWwindow* window = (_GLFWwindow*) handle; 1028 assert(window != NULL); 1029 1030 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 1031 _GLFW_SWAP_POINTERS(window->callbacks.iconify, cbfun); 1032 return cbfun; 1033 } 1034 1035 GLFWAPI GLFWwindowmaximizefun glfwSetWindowMaximizeCallback(GLFWwindow* handle, 1036 GLFWwindowmaximizefun cbfun) 1037 { 1038 _GLFWwindow* window = (_GLFWwindow*) handle; 1039 assert(window != NULL); 1040 1041 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 1042 _GLFW_SWAP_POINTERS(window->callbacks.maximize, cbfun); 1043 return cbfun; 1044 } 1045 1046 GLFWAPI GLFWframebuffersizefun glfwSetFramebufferSizeCallback(GLFWwindow* handle, 1047 GLFWframebuffersizefun cbfun) 1048 { 1049 _GLFWwindow* window = (_GLFWwindow*) handle; 1050 assert(window != NULL); 1051 1052 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 1053 _GLFW_SWAP_POINTERS(window->callbacks.fbsize, cbfun); 1054 return cbfun; 1055 } 1056 1057 GLFWAPI GLFWwindowcontentscalefun glfwSetWindowContentScaleCallback(GLFWwindow* handle, 1058 GLFWwindowcontentscalefun cbfun) 1059 { 1060 _GLFWwindow* window = (_GLFWwindow*) handle; 1061 assert(window != NULL); 1062 1063 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 1064 _GLFW_SWAP_POINTERS(window->callbacks.scale, cbfun); 1065 return cbfun; 1066 } 1067 1068 GLFWAPI void glfwPollEvents(void) 1069 { 1070 _GLFW_REQUIRE_INIT(); 1071 _glfwPlatformPollEvents(); 1072 } 1073 1074 GLFWAPI void glfwWaitEvents(void) 1075 { 1076 _GLFW_REQUIRE_INIT(); 1077 _glfwPlatformWaitEvents(); 1078 } 1079 1080 GLFWAPI void glfwWaitEventsTimeout(double timeout) 1081 { 1082 _GLFW_REQUIRE_INIT(); 1083 assert(timeout == timeout); 1084 assert(timeout >= 0.0); 1085 assert(timeout <= DBL_MAX); 1086 1087 if (timeout != timeout || timeout < 0.0 || timeout > DBL_MAX) 1088 { 1089 _glfwInputError(GLFW_INVALID_VALUE, "Invalid time %f", timeout); 1090 return; 1091 } 1092 1093 _glfwPlatformWaitEventsTimeout(timeout); 1094 } 1095 1096 GLFWAPI void glfwPostEmptyEvent(void) 1097 { 1098 _GLFW_REQUIRE_INIT(); 1099 _glfwPlatformPostEmptyEvent(); 1100 } 1101