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