| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | #include "internal.h" |
| 30 | |
| 31 | #include <string.h> |
| 32 | #include <stdlib.h> |
| 33 | |
| 34 | |
| 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 | |
| 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 | |
| 85 | |
| 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 | |
| 97 | |
| 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 | |
| 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 | |