]> git.sesse.net Git - pistorm/blob - raylib/external/glfw/src/null_window.c
3e44664102d9f988f0068dd19d4a7b6f984f8d5e
[pistorm] / raylib / external / glfw / src / null_window.c
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>
6 //
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.
10 //
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:
14 //
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.
19 //
20 // 2. Altered source versions must be plainly marked as such, and must not
21 //    be misrepresented as being the original software.
22 //
23 // 3. This notice may not be removed or altered from any source
24 //    distribution.
25 //
26 //========================================================================
27 // It is fine to use C99 in this file because it will not be built with VS
28 //========================================================================
29
30 #include "internal.h"
31
32 #include <stdlib.h>
33
34 static void applySizeLimits(_GLFWwindow* window, int* width, int* height)
35 {
36     if (window->numer != GLFW_DONT_CARE && window->denom != GLFW_DONT_CARE)
37     {
38         const float ratio = (float) window->numer / (float) window->denom;
39         *height = (int) (*width / ratio);
40     }
41
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;
46
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;
51 }
52
53 static void fitToMonitor(_GLFWwindow* window)
54 {
55     GLFWvidmode mode;
56     _glfwPlatformGetVideoMode(window->monitor, &mode);
57     _glfwPlatformGetMonitorPos(window->monitor,
58                                &window->null.xpos,
59                                &window->null.ypos);
60     window->null.width = mode.width;
61     window->null.height = mode.height;
62 }
63
64 static void acquireMonitor(_GLFWwindow* window)
65 {
66     _glfwInputMonitorWindow(window->monitor, window);
67 }
68
69 static void releaseMonitor(_GLFWwindow* window)
70 {
71     if (window->monitor->window != window)
72         return;
73
74     _glfwInputMonitorWindow(window->monitor, NULL);
75 }
76
77 static int createNativeWindow(_GLFWwindow* window,
78                               const _GLFWwndconfig* wndconfig,
79                               const _GLFWfbconfig* fbconfig)
80 {
81     if (window->monitor)
82         fitToMonitor(window);
83     else
84     {
85         window->null.xpos = 17;
86         window->null.ypos = 17;
87         window->null.width = wndconfig->width;
88         window->null.height = wndconfig->height;
89     }
90
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;
97
98     return GLFW_TRUE;
99 }
100
101
102 //////////////////////////////////////////////////////////////////////////
103 //////                       GLFW platform API                      //////
104 //////////////////////////////////////////////////////////////////////////
105
106 int _glfwPlatformCreateWindow(_GLFWwindow* window,
107                               const _GLFWwndconfig* wndconfig,
108                               const _GLFWctxconfig* ctxconfig,
109                               const _GLFWfbconfig* fbconfig)
110 {
111     if (!createNativeWindow(window, wndconfig, fbconfig))
112         return GLFW_FALSE;
113
114     if (ctxconfig->client != GLFW_NO_API)
115     {
116         if (ctxconfig->source == GLFW_NATIVE_CONTEXT_API ||
117             ctxconfig->source == GLFW_OSMESA_CONTEXT_API)
118         {
119             if (!_glfwInitOSMesa())
120                 return GLFW_FALSE;
121             if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig))
122                 return GLFW_FALSE;
123         }
124         else
125         {
126             _glfwInputError(GLFW_API_UNAVAILABLE, "Null: EGL not available");
127             return GLFW_FALSE;
128         }
129     }
130
131     if (window->monitor)
132     {
133         _glfwPlatformShowWindow(window);
134         _glfwPlatformFocusWindow(window);
135         acquireMonitor(window);
136     }
137
138     return GLFW_TRUE;
139 }
140
141 void _glfwPlatformDestroyWindow(_GLFWwindow* window)
142 {
143     if (window->monitor)
144         releaseMonitor(window);
145
146     if (_glfw.null.focusedWindow == window)
147         _glfw.null.focusedWindow = NULL;
148
149     if (window->context.destroy)
150         window->context.destroy(window);
151 }
152
153 void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
154 {
155 }
156
157 void _glfwPlatformSetWindowIcon(_GLFWwindow* window, int count,
158                                 const GLFWimage* images)
159 {
160 }
161
162 void _glfwPlatformSetWindowMonitor(_GLFWwindow* window,
163                                    _GLFWmonitor* monitor,
164                                    int xpos, int ypos,
165                                    int width, int height,
166                                    int refreshRate)
167 {
168     if (window->monitor == monitor)
169     {
170         if (!monitor)
171         {
172             _glfwPlatformSetWindowPos(window, xpos, ypos);
173             _glfwPlatformSetWindowSize(window, width, height);
174         }
175
176         return;
177     }
178
179     if (window->monitor)
180         releaseMonitor(window);
181
182     _glfwInputWindowMonitor(window, monitor);
183
184     if (window->monitor)
185     {
186         window->null.visible = GLFW_TRUE;
187         acquireMonitor(window);
188         fitToMonitor(window);
189     }
190     else
191     {
192         _glfwPlatformSetWindowPos(window, xpos, ypos);
193         _glfwPlatformSetWindowSize(window, width, height);
194     }
195 }
196
197 void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos)
198 {
199     if (xpos)
200         *xpos = window->null.xpos;
201     if (ypos)
202         *ypos = window->null.ypos;
203 }
204
205 void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos)
206 {
207     if (window->monitor)
208         return;
209
210     if (window->null.xpos != xpos || window->null.ypos != ypos)
211     {
212         window->null.xpos = xpos;
213         window->null.ypos = ypos;
214         _glfwInputWindowPos(window, xpos, ypos);
215     }
216 }
217
218 void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
219 {
220     if (width)
221         *width = window->null.width;
222     if (height)
223         *height = window->null.height;
224 }
225
226 void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
227 {
228     if (window->monitor)
229         return;
230
231     if (window->null.width != width || window->null.height != height)
232     {
233         window->null.width = width;
234         window->null.height = height;
235         _glfwInputWindowSize(window, width, height);
236         _glfwInputFramebufferSize(window, width, height);
237     }
238 }
239
240 void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window,
241                                       int minwidth, int minheight,
242                                       int maxwidth, int maxheight)
243 {
244     int width = window->null.width;
245     int height = window->null.height;
246     applySizeLimits(window, &width, &height);
247     _glfwPlatformSetWindowSize(window, width, height);
248 }
249
250 void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n, int d)
251 {
252     int width = window->null.width;
253     int height = window->null.height;
254     applySizeLimits(window, &width, &height);
255     _glfwPlatformSetWindowSize(window, width, height);
256 }
257
258 void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height)
259 {
260     if (width)
261         *width = window->null.width;
262     if (height)
263         *height = window->null.height;
264 }
265
266 void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window,
267                                      int* left, int* top,
268                                      int* right, int* bottom)
269 {
270     if (window->null.decorated && !window->monitor)
271     {
272         if (left)
273             *left = 1;
274         if (top)
275             *top = 10;
276         if (right)
277             *right = 1;
278         if (bottom)
279             *bottom = 1;
280     }
281     else
282     {
283         if (left)
284             *left = 0;
285         if (top)
286             *top = 0;
287         if (right)
288             *right = 0;
289         if (bottom)
290             *bottom = 0;
291     }
292 }
293
294 void _glfwPlatformGetWindowContentScale(_GLFWwindow* window,
295                                         float* xscale, float* yscale)
296 {
297     if (xscale)
298         *xscale = 1.f;
299     if (yscale)
300         *yscale = 1.f;
301 }
302
303 void _glfwPlatformIconifyWindow(_GLFWwindow* window)
304 {
305     if (_glfw.null.focusedWindow == window)
306     {
307         _glfw.null.focusedWindow = NULL;
308         _glfwInputWindowFocus(window, GLFW_FALSE);
309     }
310
311     if (!window->null.iconified)
312     {
313         window->null.iconified = GLFW_TRUE;
314         _glfwInputWindowIconify(window, GLFW_TRUE);
315
316         if (window->monitor)
317             releaseMonitor(window);
318     }
319 }
320
321 void _glfwPlatformRestoreWindow(_GLFWwindow* window)
322 {
323     if (window->null.iconified)
324     {
325         window->null.iconified = GLFW_FALSE;
326         _glfwInputWindowIconify(window, GLFW_FALSE);
327
328         if (window->monitor)
329             acquireMonitor(window);
330     }
331     else if (window->null.maximized)
332     {
333         window->null.maximized = GLFW_FALSE;
334         _glfwInputWindowMaximize(window, GLFW_FALSE);
335     }
336 }
337
338 void _glfwPlatformMaximizeWindow(_GLFWwindow* window)
339 {
340     if (!window->null.maximized)
341     {
342         window->null.maximized = GLFW_TRUE;
343         _glfwInputWindowMaximize(window, GLFW_TRUE);
344     }
345 }
346
347 int _glfwPlatformWindowMaximized(_GLFWwindow* window)
348 {
349     return window->null.maximized;
350 }
351
352 int _glfwPlatformWindowHovered(_GLFWwindow* window)
353 {
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;
358 }
359
360 int _glfwPlatformFramebufferTransparent(_GLFWwindow* window)
361 {
362     return window->null.transparent;
363 }
364
365 void _glfwPlatformSetWindowResizable(_GLFWwindow* window, GLFWbool enabled)
366 {
367     window->null.resizable = enabled;
368 }
369
370 void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, GLFWbool enabled)
371 {
372     window->null.decorated = enabled;
373 }
374
375 void _glfwPlatformSetWindowFloating(_GLFWwindow* window, GLFWbool enabled)
376 {
377     window->null.floating = enabled;
378 }
379
380 void _glfwPlatformSetWindowMousePassthrough(_GLFWwindow* window, GLFWbool enabled)
381 {
382 }
383
384 float _glfwPlatformGetWindowOpacity(_GLFWwindow* window)
385 {
386     return window->null.opacity;
387 }
388
389 void _glfwPlatformSetWindowOpacity(_GLFWwindow* window, float opacity)
390 {
391     window->null.opacity = opacity;
392 }
393
394 void _glfwPlatformSetRawMouseMotion(_GLFWwindow *window, GLFWbool enabled)
395 {
396 }
397
398 GLFWbool _glfwPlatformRawMouseMotionSupported(void)
399 {
400     return GLFW_TRUE;
401 }
402
403 void _glfwPlatformShowWindow(_GLFWwindow* window)
404 {
405     window->null.visible = GLFW_TRUE;
406 }
407
408 void _glfwPlatformRequestWindowAttention(_GLFWwindow* window)
409 {
410 }
411
412 void _glfwPlatformHideWindow(_GLFWwindow* window)
413 {
414     if (_glfw.null.focusedWindow == window)
415     {
416         _glfw.null.focusedWindow = NULL;
417         _glfwInputWindowFocus(window, GLFW_FALSE);
418     }
419
420     window->null.visible = GLFW_FALSE;
421 }
422
423 void _glfwPlatformFocusWindow(_GLFWwindow* window)
424 {
425     if (_glfw.null.focusedWindow == window)
426         return;
427
428     if (!window->null.visible)
429         return;
430
431     _GLFWwindow* previous = _glfw.null.focusedWindow;
432     _glfw.null.focusedWindow = window;
433
434     if (previous)
435     {
436         _glfwInputWindowFocus(previous, GLFW_FALSE);
437         if (previous->monitor && previous->autoIconify)
438             _glfwPlatformIconifyWindow(previous);
439     }
440
441     _glfwInputWindowFocus(window, GLFW_TRUE);
442 }
443
444 int _glfwPlatformWindowFocused(_GLFWwindow* window)
445 {
446     return _glfw.null.focusedWindow == window;
447 }
448
449 int _glfwPlatformWindowIconified(_GLFWwindow* window)
450 {
451     return window->null.iconified;
452 }
453
454 int _glfwPlatformWindowVisible(_GLFWwindow* window)
455 {
456     return window->null.visible;
457 }
458
459 void _glfwPlatformPollEvents(void)
460 {
461 }
462
463 void _glfwPlatformWaitEvents(void)
464 {
465 }
466
467 void _glfwPlatformWaitEventsTimeout(double timeout)
468 {
469 }
470
471 void _glfwPlatformPostEmptyEvent(void)
472 {
473 }
474
475 void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
476 {
477     if (xpos)
478         *xpos = _glfw.null.xcursor - window->null.xpos;
479     if (ypos)
480         *ypos = _glfw.null.ycursor - window->null.ypos;
481 }
482
483 void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
484 {
485     _glfw.null.xcursor = window->null.xpos + (int) x;
486     _glfw.null.ycursor = window->null.ypos + (int) y;
487 }
488
489 void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
490 {
491 }
492
493 int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
494                               const GLFWimage* image,
495                               int xhot, int yhot)
496 {
497     return GLFW_TRUE;
498 }
499
500 int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, int shape)
501 {
502     return GLFW_TRUE;
503 }
504
505 void _glfwPlatformDestroyCursor(_GLFWcursor* cursor)
506 {
507 }
508
509 void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
510 {
511 }
512
513 void _glfwPlatformSetClipboardString(const char* string)
514 {
515     char* copy = _glfw_strdup(string);
516     free(_glfw.null.clipboardString);
517     _glfw.null.clipboardString = copy;
518 }
519
520 const char* _glfwPlatformGetClipboardString(void)
521 {
522     return _glfw.null.clipboardString;
523 }
524
525 const char* _glfwPlatformGetScancodeName(int scancode)
526 {
527     switch (scancode)
528     {
529         case GLFW_KEY_APOSTROPHE:
530             return "'";
531         case GLFW_KEY_COMMA:
532             return ",";
533         case GLFW_KEY_MINUS:
534         case GLFW_KEY_KP_SUBTRACT:
535             return "-";
536         case GLFW_KEY_PERIOD:
537         case GLFW_KEY_KP_DECIMAL:
538             return ".";
539         case GLFW_KEY_SLASH:
540         case GLFW_KEY_KP_DIVIDE:
541             return "/";
542         case GLFW_KEY_SEMICOLON:
543             return ";";
544         case GLFW_KEY_EQUAL:
545         case GLFW_KEY_KP_EQUAL:
546             return "=";
547         case GLFW_KEY_LEFT_BRACKET:
548             return "[";
549         case GLFW_KEY_RIGHT_BRACKET:
550             return "]";
551         case GLFW_KEY_KP_MULTIPLY:
552             return "*";
553         case GLFW_KEY_KP_ADD:
554             return "+";
555         case GLFW_KEY_BACKSLASH:
556         case GLFW_KEY_WORLD_1:
557         case GLFW_KEY_WORLD_2:
558             return "\\";
559         case GLFW_KEY_0:
560         case GLFW_KEY_KP_0:
561             return "0";
562         case GLFW_KEY_1:
563         case GLFW_KEY_KP_1:
564             return "1";
565         case GLFW_KEY_2:
566         case GLFW_KEY_KP_2:
567             return "2";
568         case GLFW_KEY_3:
569         case GLFW_KEY_KP_3:
570             return "3";
571         case GLFW_KEY_4:
572         case GLFW_KEY_KP_4:
573             return "4";
574         case GLFW_KEY_5:
575         case GLFW_KEY_KP_5:
576             return "5";
577         case GLFW_KEY_6:
578         case GLFW_KEY_KP_6:
579             return "6";
580         case GLFW_KEY_7:
581         case GLFW_KEY_KP_7:
582             return "7";
583         case GLFW_KEY_8:
584         case GLFW_KEY_KP_8:
585             return "8";
586         case GLFW_KEY_9:
587         case GLFW_KEY_KP_9:
588             return "9";
589         case GLFW_KEY_A:
590             return "a";
591         case GLFW_KEY_B:
592             return "b";
593         case GLFW_KEY_C:
594             return "c";
595         case GLFW_KEY_D:
596             return "d";
597         case GLFW_KEY_E:
598             return "e";
599         case GLFW_KEY_F:
600             return "f";
601         case GLFW_KEY_G:
602             return "g";
603         case GLFW_KEY_H:
604             return "h";
605         case GLFW_KEY_I:
606             return "i";
607         case GLFW_KEY_J:
608             return "j";
609         case GLFW_KEY_K:
610             return "k";
611         case GLFW_KEY_L:
612             return "l";
613         case GLFW_KEY_M:
614             return "m";
615         case GLFW_KEY_N:
616             return "n";
617         case GLFW_KEY_O:
618             return "o";
619         case GLFW_KEY_P:
620             return "p";
621         case GLFW_KEY_Q:
622             return "q";
623         case GLFW_KEY_R:
624             return "r";
625         case GLFW_KEY_S:
626             return "s";
627         case GLFW_KEY_T:
628             return "t";
629         case GLFW_KEY_U:
630             return "u";
631         case GLFW_KEY_V:
632             return "v";
633         case GLFW_KEY_W:
634             return "w";
635         case GLFW_KEY_X:
636             return "x";
637         case GLFW_KEY_Y:
638             return "y";
639         case GLFW_KEY_Z:
640             return "z";
641     }
642
643     return NULL;
644 }
645
646 int _glfwPlatformGetKeyScancode(int key)
647 {
648     return key;
649 }
650
651 void _glfwPlatformGetRequiredInstanceExtensions(char** extensions)
652 {
653 }
654
655 int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance,
656                                                       VkPhysicalDevice device,
657                                                       uint32_t queuefamily)
658 {
659     return GLFW_FALSE;
660 }
661
662 VkResult _glfwPlatformCreateWindowSurface(VkInstance instance,
663                                           _GLFWwindow* window,
664                                           const VkAllocationCallbacks* allocator,
665                                           VkSurfaceKHR* surface)
666 {
667     // This seems like the most appropriate error to return here
668     return VK_ERROR_EXTENSION_NOT_PRESENT;
669 }
670