1 | //======================================================================== |
2 | // GLFW 3.4 - www.glfw.org |
3 | //------------------------------------------------------------------------ |
4 | // Copyright (c) 2002-2006 Marcus Geelnard |
5 | // Copyright (c) 2006-2018 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 | // Please use C89 style variable declarations in this file because VS 2010 |
28 | //======================================================================== |
29 | |
30 | #include "internal.h" |
31 | |
32 | // These construct a string literal from individual numeric constants |
33 | #define _GLFW_CONCAT_VERSION(m, n, r) #m "." #n "." #r |
34 | #define _GLFW_MAKE_VERSION(m, n, r) _GLFW_CONCAT_VERSION(m, n, r) |
35 | |
36 | ////////////////////////////////////////////////////////////////////////// |
37 | ////// GLFW internal API ////// |
38 | ////////////////////////////////////////////////////////////////////////// |
39 | |
40 | static const struct |
41 | { |
42 | int ID; |
43 | GLFWbool (*connect)(int,_GLFWplatform*); |
44 | } supportedPlatforms[] = |
45 | { |
46 | #if defined(_GLFW_WIN32) |
47 | { GLFW_PLATFORM_WIN32, _glfwConnectWin32 }, |
48 | #endif |
49 | #if defined(_GLFW_COCOA) |
50 | { GLFW_PLATFORM_COCOA, _glfwConnectCocoa }, |
51 | #endif |
52 | #if defined(_GLFW_X11) |
53 | { GLFW_PLATFORM_X11, _glfwConnectX11 }, |
54 | #endif |
55 | #if defined(_GLFW_WAYLAND) |
56 | { GLFW_PLATFORM_WAYLAND, _glfwConnectWayland }, |
57 | #endif |
58 | }; |
59 | |
60 | GLFWbool _glfwSelectPlatform(int desiredID, _GLFWplatform* platform) |
61 | { |
62 | const size_t count = sizeof(supportedPlatforms) / sizeof(supportedPlatforms[0]); |
63 | size_t i; |
64 | |
65 | if (desiredID != GLFW_ANY_PLATFORM && |
66 | desiredID != GLFW_PLATFORM_WIN32 && |
67 | desiredID != GLFW_PLATFORM_COCOA && |
68 | desiredID != GLFW_PLATFORM_WAYLAND && |
69 | desiredID != GLFW_PLATFORM_X11 && |
70 | desiredID != GLFW_PLATFORM_NULL) |
71 | { |
72 | _glfwInputError(GLFW_INVALID_ENUM, format: "Invalid platform ID 0x%08X" , desiredID); |
73 | return GLFW_FALSE; |
74 | } |
75 | |
76 | // Only allow the Null platform if specifically requested |
77 | if (desiredID == GLFW_PLATFORM_NULL) |
78 | return _glfwConnectNull(platformID: desiredID, platform); |
79 | else if (count == 0) |
80 | { |
81 | _glfwInputError(GLFW_PLATFORM_UNAVAILABLE, format: "This binary only supports the Null platform" ); |
82 | return GLFW_FALSE; |
83 | } |
84 | |
85 | if (desiredID == GLFW_ANY_PLATFORM) |
86 | { |
87 | // If there is exactly one platform available for auto-selection, let it emit the |
88 | // error on failure as the platform-specific error description may be more helpful |
89 | if (count == 1) |
90 | return supportedPlatforms[0].connect(supportedPlatforms[0].ID, platform); |
91 | |
92 | for (i = 0; i < count; i++) |
93 | { |
94 | if (supportedPlatforms[i].connect(desiredID, platform)) |
95 | return GLFW_TRUE; |
96 | } |
97 | |
98 | _glfwInputError(GLFW_PLATFORM_UNAVAILABLE, format: "Failed to detect any supported platform" ); |
99 | } |
100 | else |
101 | { |
102 | for (i = 0; i < count; i++) |
103 | { |
104 | if (supportedPlatforms[i].ID == desiredID) |
105 | return supportedPlatforms[i].connect(desiredID, platform); |
106 | } |
107 | |
108 | _glfwInputError(GLFW_PLATFORM_UNAVAILABLE, format: "The requested platform is not supported" ); |
109 | } |
110 | |
111 | return GLFW_FALSE; |
112 | } |
113 | |
114 | ////////////////////////////////////////////////////////////////////////// |
115 | ////// GLFW public API ////// |
116 | ////////////////////////////////////////////////////////////////////////// |
117 | |
118 | GLFWAPI int glfwGetPlatform(void) |
119 | { |
120 | _GLFW_REQUIRE_INIT_OR_RETURN(0); |
121 | return _glfw.platform.platformID; |
122 | } |
123 | |
124 | GLFWAPI int glfwPlatformSupported(int platformID) |
125 | { |
126 | const size_t count = sizeof(supportedPlatforms) / sizeof(supportedPlatforms[0]); |
127 | size_t i; |
128 | |
129 | if (platformID != GLFW_PLATFORM_WIN32 && |
130 | platformID != GLFW_PLATFORM_COCOA && |
131 | platformID != GLFW_PLATFORM_WAYLAND && |
132 | platformID != GLFW_PLATFORM_X11 && |
133 | platformID != GLFW_PLATFORM_NULL) |
134 | { |
135 | _glfwInputError(GLFW_INVALID_ENUM, format: "Invalid platform ID 0x%08X" , platformID); |
136 | return GLFW_FALSE; |
137 | } |
138 | |
139 | if (platformID == GLFW_PLATFORM_NULL) |
140 | return GLFW_TRUE; |
141 | |
142 | for (i = 0; i < count; i++) |
143 | { |
144 | if (platformID == supportedPlatforms[i].ID) |
145 | return GLFW_TRUE; |
146 | } |
147 | |
148 | return GLFW_FALSE; |
149 | } |
150 | |
151 | GLFWAPI const char* glfwGetVersionString(void) |
152 | { |
153 | return _GLFW_MAKE_VERSION(GLFW_VERSION_MAJOR, |
154 | GLFW_VERSION_MINOR, |
155 | GLFW_VERSION_REVISION) |
156 | #if defined(_GLFW_WIN32) |
157 | " Win32 WGL" |
158 | #endif |
159 | #if defined(_GLFW_COCOA) |
160 | " Cocoa NSGL" |
161 | #endif |
162 | #if defined(_GLFW_WAYLAND) |
163 | " Wayland" |
164 | #endif |
165 | #if defined(_GLFW_X11) |
166 | " X11 GLX" |
167 | #endif |
168 | " Null" |
169 | " EGL" |
170 | " OSMesa" |
171 | #if defined(__MINGW64_VERSION_MAJOR) |
172 | " MinGW-w64" |
173 | #elif defined(__MINGW32__) |
174 | " MinGW" |
175 | #elif defined(_MSC_VER) |
176 | " VisualC" |
177 | #endif |
178 | #if defined(_GLFW_USE_HYBRID_HPG) || defined(_GLFW_USE_OPTIMUS_HPG) |
179 | " hybrid-GPU" |
180 | #endif |
181 | #if defined(_POSIX_MONOTONIC_CLOCK) |
182 | " monotonic" |
183 | #endif |
184 | #if defined(_GLFW_BUILD_DLL) |
185 | #if defined(_WIN32) |
186 | " DLL" |
187 | #elif defined(__APPLE__) |
188 | " dynamic" |
189 | #else |
190 | " shared" |
191 | #endif |
192 | #endif |
193 | ; |
194 | } |
195 | |
196 | |