1 //========================================================================
2 // GLFW 3.4 - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2016 Google Inc.
5 // Copyright (c) 2016-2019 Camilla Löwy <elmindreda@glfw.org>
7 // This software is provided 'as-is', without any express or implied
8 // warranty. In no event will the authors be held liable for any damages
9 // arising from the use of this software.
11 // Permission is granted to anyone to use this software for any purpose,
12 // including commercial applications, and to alter it and redistribute it
13 // freely, subject to the following restrictions:
15 // 1. The origin of this software must not be misrepresented; you must not
16 // claim that you wrote the original software. If you use this software
17 // in a product, an acknowledgment in the product documentation would
18 // be appreciated but is not required.
20 // 2. Altered source versions must be plainly marked as such, and must not
21 // be misrepresented as being the original software.
23 // 3. This notice may not be removed or altered from any source
26 //========================================================================
27 // It is fine to use C99 in this file because it will not be built with VS
28 //========================================================================
34 static void applySizeLimits(_GLFWwindow* window, int* width, int* height)
36 if (window->numer != GLFW_DONT_CARE && window->denom != GLFW_DONT_CARE)
38 const float ratio = (float) window->numer / (float) window->denom;
39 *height = (int) (*width / ratio);
42 if (window->minwidth != GLFW_DONT_CARE && *width < window->minwidth)
43 *width = window->minwidth;
44 else if (window->maxwidth != GLFW_DONT_CARE && *width > window->maxwidth)
45 *width = window->maxwidth;
47 if (window->minheight != GLFW_DONT_CARE && *height < window->minheight)
48 *height = window->minheight;
49 else if (window->maxheight != GLFW_DONT_CARE && *height > window->maxheight)
50 *height = window->maxheight;
53 static void fitToMonitor(_GLFWwindow* window)
56 _glfwPlatformGetVideoMode(window->monitor, &mode);
57 _glfwPlatformGetMonitorPos(window->monitor,
60 window->null.width = mode.width;
61 window->null.height = mode.height;
64 static void acquireMonitor(_GLFWwindow* window)
66 _glfwInputMonitorWindow(window->monitor, window);
69 static void releaseMonitor(_GLFWwindow* window)
71 if (window->monitor->window != window)
74 _glfwInputMonitorWindow(window->monitor, NULL);
77 static int createNativeWindow(_GLFWwindow* window,
78 const _GLFWwndconfig* wndconfig,
79 const _GLFWfbconfig* fbconfig)
85 window->null.xpos = 17;
86 window->null.ypos = 17;
87 window->null.width = wndconfig->width;
88 window->null.height = wndconfig->height;
91 window->null.visible = wndconfig->visible;
92 window->null.decorated = wndconfig->decorated;
93 window->null.maximized = wndconfig->maximized;
94 window->null.floating = wndconfig->floating;
95 window->null.transparent = fbconfig->transparent;
96 window->null.opacity = 1.f;
102 //////////////////////////////////////////////////////////////////////////
103 ////// GLFW platform API //////
104 //////////////////////////////////////////////////////////////////////////
106 int _glfwPlatformCreateWindow(_GLFWwindow* window,
107 const _GLFWwndconfig* wndconfig,
108 const _GLFWctxconfig* ctxconfig,
109 const _GLFWfbconfig* fbconfig)
111 if (!createNativeWindow(window, wndconfig, fbconfig))
114 if (ctxconfig->client != GLFW_NO_API)
116 if (ctxconfig->source == GLFW_NATIVE_CONTEXT_API ||
117 ctxconfig->source == GLFW_OSMESA_CONTEXT_API)
119 if (!_glfwInitOSMesa())
121 if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig))
126 _glfwInputError(GLFW_API_UNAVAILABLE, "Null: EGL not available");
133 _glfwPlatformShowWindow(window);
134 _glfwPlatformFocusWindow(window);
135 acquireMonitor(window);
141 void _glfwPlatformDestroyWindow(_GLFWwindow* window)
144 releaseMonitor(window);
146 if (_glfw.null.focusedWindow == window)
147 _glfw.null.focusedWindow = NULL;
149 if (window->context.destroy)
150 window->context.destroy(window);
153 void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
157 void _glfwPlatformSetWindowIcon(_GLFWwindow* window, int count,
158 const GLFWimage* images)
162 void _glfwPlatformSetWindowMonitor(_GLFWwindow* window,
163 _GLFWmonitor* monitor,
165 int width, int height,
168 if (window->monitor == monitor)
172 _glfwPlatformSetWindowPos(window, xpos, ypos);
173 _glfwPlatformSetWindowSize(window, width, height);
180 releaseMonitor(window);
182 _glfwInputWindowMonitor(window, monitor);
186 window->null.visible = GLFW_TRUE;
187 acquireMonitor(window);
188 fitToMonitor(window);
192 _glfwPlatformSetWindowPos(window, xpos, ypos);
193 _glfwPlatformSetWindowSize(window, width, height);
197 void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos)
200 *xpos = window->null.xpos;
202 *ypos = window->null.ypos;
205 void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos)
210 if (window->null.xpos != xpos || window->null.ypos != ypos)
212 window->null.xpos = xpos;
213 window->null.ypos = ypos;
214 _glfwInputWindowPos(window, xpos, ypos);
218 void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
221 *width = window->null.width;
223 *height = window->null.height;
226 void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
231 if (window->null.width != width || window->null.height != height)
233 window->null.width = width;
234 window->null.height = height;
235 _glfwInputWindowSize(window, width, height);
236 _glfwInputFramebufferSize(window, width, height);
240 void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window,
241 int minwidth, int minheight,
242 int maxwidth, int maxheight)
244 int width = window->null.width;
245 int height = window->null.height;
246 applySizeLimits(window, &width, &height);
247 _glfwPlatformSetWindowSize(window, width, height);
250 void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n, int d)
252 int width = window->null.width;
253 int height = window->null.height;
254 applySizeLimits(window, &width, &height);
255 _glfwPlatformSetWindowSize(window, width, height);
258 void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height)
261 *width = window->null.width;
263 *height = window->null.height;
266 void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window,
268 int* right, int* bottom)
270 if (window->null.decorated && !window->monitor)
294 void _glfwPlatformGetWindowContentScale(_GLFWwindow* window,
295 float* xscale, float* yscale)
303 void _glfwPlatformIconifyWindow(_GLFWwindow* window)
305 if (_glfw.null.focusedWindow == window)
307 _glfw.null.focusedWindow = NULL;
308 _glfwInputWindowFocus(window, GLFW_FALSE);
311 if (!window->null.iconified)
313 window->null.iconified = GLFW_TRUE;
314 _glfwInputWindowIconify(window, GLFW_TRUE);
317 releaseMonitor(window);
321 void _glfwPlatformRestoreWindow(_GLFWwindow* window)
323 if (window->null.iconified)
325 window->null.iconified = GLFW_FALSE;
326 _glfwInputWindowIconify(window, GLFW_FALSE);
329 acquireMonitor(window);
331 else if (window->null.maximized)
333 window->null.maximized = GLFW_FALSE;
334 _glfwInputWindowMaximize(window, GLFW_FALSE);
338 void _glfwPlatformMaximizeWindow(_GLFWwindow* window)
340 if (!window->null.maximized)
342 window->null.maximized = GLFW_TRUE;
343 _glfwInputWindowMaximize(window, GLFW_TRUE);
347 int _glfwPlatformWindowMaximized(_GLFWwindow* window)
349 return window->null.maximized;
352 int _glfwPlatformWindowHovered(_GLFWwindow* window)
354 return _glfw.null.xcursor >= window->null.xpos &&
355 _glfw.null.ycursor >= window->null.ypos &&
356 _glfw.null.xcursor <= window->null.xpos + window->null.width - 1 &&
357 _glfw.null.ycursor <= window->null.ypos + window->null.height - 1;
360 int _glfwPlatformFramebufferTransparent(_GLFWwindow* window)
362 return window->null.transparent;
365 void _glfwPlatformSetWindowResizable(_GLFWwindow* window, GLFWbool enabled)
367 window->null.resizable = enabled;
370 void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, GLFWbool enabled)
372 window->null.decorated = enabled;
375 void _glfwPlatformSetWindowFloating(_GLFWwindow* window, GLFWbool enabled)
377 window->null.floating = enabled;
380 void _glfwPlatformSetWindowMousePassthrough(_GLFWwindow* window, GLFWbool enabled)
384 float _glfwPlatformGetWindowOpacity(_GLFWwindow* window)
386 return window->null.opacity;
389 void _glfwPlatformSetWindowOpacity(_GLFWwindow* window, float opacity)
391 window->null.opacity = opacity;
394 void _glfwPlatformSetRawMouseMotion(_GLFWwindow *window, GLFWbool enabled)
398 GLFWbool _glfwPlatformRawMouseMotionSupported(void)
403 void _glfwPlatformShowWindow(_GLFWwindow* window)
405 window->null.visible = GLFW_TRUE;
408 void _glfwPlatformRequestWindowAttention(_GLFWwindow* window)
412 void _glfwPlatformHideWindow(_GLFWwindow* window)
414 if (_glfw.null.focusedWindow == window)
416 _glfw.null.focusedWindow = NULL;
417 _glfwInputWindowFocus(window, GLFW_FALSE);
420 window->null.visible = GLFW_FALSE;
423 void _glfwPlatformFocusWindow(_GLFWwindow* window)
425 if (_glfw.null.focusedWindow == window)
428 if (!window->null.visible)
431 _GLFWwindow* previous = _glfw.null.focusedWindow;
432 _glfw.null.focusedWindow = window;
436 _glfwInputWindowFocus(previous, GLFW_FALSE);
437 if (previous->monitor && previous->autoIconify)
438 _glfwPlatformIconifyWindow(previous);
441 _glfwInputWindowFocus(window, GLFW_TRUE);
444 int _glfwPlatformWindowFocused(_GLFWwindow* window)
446 return _glfw.null.focusedWindow == window;
449 int _glfwPlatformWindowIconified(_GLFWwindow* window)
451 return window->null.iconified;
454 int _glfwPlatformWindowVisible(_GLFWwindow* window)
456 return window->null.visible;
459 void _glfwPlatformPollEvents(void)
463 void _glfwPlatformWaitEvents(void)
467 void _glfwPlatformWaitEventsTimeout(double timeout)
471 void _glfwPlatformPostEmptyEvent(void)
475 void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
478 *xpos = _glfw.null.xcursor - window->null.xpos;
480 *ypos = _glfw.null.ycursor - window->null.ypos;
483 void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
485 _glfw.null.xcursor = window->null.xpos + (int) x;
486 _glfw.null.ycursor = window->null.ypos + (int) y;
489 void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
493 int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
494 const GLFWimage* image,
500 int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, int shape)
505 void _glfwPlatformDestroyCursor(_GLFWcursor* cursor)
509 void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
513 void _glfwPlatformSetClipboardString(const char* string)
515 char* copy = _glfw_strdup(string);
516 free(_glfw.null.clipboardString);
517 _glfw.null.clipboardString = copy;
520 const char* _glfwPlatformGetClipboardString(void)
522 return _glfw.null.clipboardString;
525 const char* _glfwPlatformGetScancodeName(int scancode)
529 case GLFW_KEY_APOSTROPHE:
534 case GLFW_KEY_KP_SUBTRACT:
536 case GLFW_KEY_PERIOD:
537 case GLFW_KEY_KP_DECIMAL:
540 case GLFW_KEY_KP_DIVIDE:
542 case GLFW_KEY_SEMICOLON:
545 case GLFW_KEY_KP_EQUAL:
547 case GLFW_KEY_LEFT_BRACKET:
549 case GLFW_KEY_RIGHT_BRACKET:
551 case GLFW_KEY_KP_MULTIPLY:
553 case GLFW_KEY_KP_ADD:
555 case GLFW_KEY_BACKSLASH:
556 case GLFW_KEY_WORLD_1:
557 case GLFW_KEY_WORLD_2:
646 int _glfwPlatformGetKeyScancode(int key)
651 void _glfwPlatformGetRequiredInstanceExtensions(char** extensions)
655 int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance,
656 VkPhysicalDevice device,
657 uint32_t queuefamily)
662 VkResult _glfwPlatformCreateWindowSurface(VkInstance instance,
664 const VkAllocationCallbacks* allocator,
665 VkSurfaceKHR* surface)
667 // This seems like the most appropriate error to return here
668 return VK_ERROR_EXTENSION_NOT_PRESENT;