init.c (11245B)
1 //======================================================================== 2 // GLFW 3.3 - 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 #include <string.h> 33 #include <stdlib.h> 34 #include <stdio.h> 35 #include <stdarg.h> 36 #include <assert.h> 37 38 39 // NOTE: The global variables below comprise all mutable global data in GLFW 40 // Any other mutable global variable is a bug 41 42 // This contains all mutable state shared between compilation units of GLFW 43 // 44 _GLFWlibrary _glfw = { GLFW_FALSE }; 45 46 // These are outside of _glfw so they can be used before initialization and 47 // after termination without special handling when _glfw is cleared to zero 48 // 49 static _GLFWerror _glfwMainThreadError; 50 static GLFWerrorfun _glfwErrorCallback; 51 static _GLFWinitconfig _glfwInitHints = 52 { 53 GLFW_TRUE, // hat buttons 54 { 55 GLFW_TRUE, // macOS menu bar 56 GLFW_TRUE // macOS bundle chdir 57 } 58 }; 59 60 // Terminate the library 61 // 62 static void terminate(void) 63 { 64 int i; 65 66 memset(&_glfw.callbacks, 0, sizeof(_glfw.callbacks)); 67 68 while (_glfw.windowListHead) 69 glfwDestroyWindow((GLFWwindow*) _glfw.windowListHead); 70 71 while (_glfw.cursorListHead) 72 glfwDestroyCursor((GLFWcursor*) _glfw.cursorListHead); 73 74 for (i = 0; i < _glfw.monitorCount; i++) 75 { 76 _GLFWmonitor* monitor = _glfw.monitors[i]; 77 if (monitor->originalRamp.size) 78 _glfwPlatformSetGammaRamp(monitor, &monitor->originalRamp); 79 _glfwFreeMonitor(monitor); 80 } 81 82 free(_glfw.monitors); 83 _glfw.monitors = NULL; 84 _glfw.monitorCount = 0; 85 86 free(_glfw.mappings); 87 _glfw.mappings = NULL; 88 _glfw.mappingCount = 0; 89 90 _glfwTerminateVulkan(); 91 _glfwPlatformTerminate(); 92 93 _glfw.initialized = GLFW_FALSE; 94 95 while (_glfw.errorListHead) 96 { 97 _GLFWerror* error = _glfw.errorListHead; 98 _glfw.errorListHead = error->next; 99 free(error); 100 } 101 102 _glfwPlatformDestroyTls(&_glfw.contextSlot); 103 _glfwPlatformDestroyTls(&_glfw.errorSlot); 104 _glfwPlatformDestroyMutex(&_glfw.errorLock); 105 106 memset(&_glfw, 0, sizeof(_glfw)); 107 } 108 109 110 ////////////////////////////////////////////////////////////////////////// 111 ////// GLFW internal API ////// 112 ////////////////////////////////////////////////////////////////////////// 113 114 // Encode a Unicode code point to a UTF-8 stream 115 // Based on cutef8 by Jeff Bezanson (Public Domain) 116 // 117 size_t _glfwEncodeUTF8(char* s, uint32_t codepoint) 118 { 119 size_t count = 0; 120 121 if (codepoint < 0x80) 122 s[count++] = (char) codepoint; 123 else if (codepoint < 0x800) 124 { 125 s[count++] = (codepoint >> 6) | 0xc0; 126 s[count++] = (codepoint & 0x3f) | 0x80; 127 } 128 else if (codepoint < 0x10000) 129 { 130 s[count++] = (codepoint >> 12) | 0xe0; 131 s[count++] = ((codepoint >> 6) & 0x3f) | 0x80; 132 s[count++] = (codepoint & 0x3f) | 0x80; 133 } 134 else if (codepoint < 0x110000) 135 { 136 s[count++] = (codepoint >> 18) | 0xf0; 137 s[count++] = ((codepoint >> 12) & 0x3f) | 0x80; 138 s[count++] = ((codepoint >> 6) & 0x3f) | 0x80; 139 s[count++] = (codepoint & 0x3f) | 0x80; 140 } 141 142 return count; 143 } 144 145 // Splits and translates a text/uri-list into separate file paths 146 // NOTE: This function destroys the provided string 147 // 148 char** _glfwParseUriList(char* text, int* count) 149 { 150 const char* prefix = "file://"; 151 char** paths = NULL; 152 char* line; 153 154 *count = 0; 155 156 while ((line = strtok(text, "\r\n"))) 157 { 158 char* path; 159 160 text = NULL; 161 162 if (line[0] == '#') 163 continue; 164 165 if (strncmp(line, prefix, strlen(prefix)) == 0) 166 { 167 line += strlen(prefix); 168 // TODO: Validate hostname 169 while (*line != '/') 170 line++; 171 } 172 173 (*count)++; 174 175 path = calloc(strlen(line) + 1, 1); 176 paths = realloc(paths, *count * sizeof(char*)); 177 paths[*count - 1] = path; 178 179 while (*line) 180 { 181 if (line[0] == '%' && line[1] && line[2]) 182 { 183 const char digits[3] = { line[1], line[2], '\0' }; 184 *path = (char) strtol(digits, NULL, 16); 185 line += 2; 186 } 187 else 188 *path = *line; 189 190 path++; 191 line++; 192 } 193 } 194 195 return paths; 196 } 197 198 char* _glfw_strdup(const char* source) 199 { 200 const size_t length = strlen(source); 201 char* result = calloc(length + 1, 1); 202 strcpy(result, source); 203 return result; 204 } 205 206 int _glfw_min(int a, int b) 207 { 208 return a < b ? a : b; 209 } 210 211 int _glfw_max(int a, int b) 212 { 213 return a > b ? a : b; 214 } 215 216 float _glfw_fminf(float a, float b) 217 { 218 if (a != a) 219 return b; 220 else if (b != b) 221 return a; 222 else if (a < b) 223 return a; 224 else 225 return b; 226 } 227 228 float _glfw_fmaxf(float a, float b) 229 { 230 if (a != a) 231 return b; 232 else if (b != b) 233 return a; 234 else if (a > b) 235 return a; 236 else 237 return b; 238 } 239 240 241 ////////////////////////////////////////////////////////////////////////// 242 ////// GLFW event API ////// 243 ////////////////////////////////////////////////////////////////////////// 244 245 // Notifies shared code of an error 246 // 247 void _glfwInputError(int code, const char* format, ...) 248 { 249 _GLFWerror* error; 250 char description[_GLFW_MESSAGE_SIZE]; 251 252 if (format) 253 { 254 va_list vl; 255 256 va_start(vl, format); 257 vsnprintf(description, sizeof(description), format, vl); 258 va_end(vl); 259 260 description[sizeof(description) - 1] = '\0'; 261 } 262 else 263 { 264 if (code == GLFW_NOT_INITIALIZED) 265 strcpy(description, "The GLFW library is not initialized"); 266 else if (code == GLFW_NO_CURRENT_CONTEXT) 267 strcpy(description, "There is no current context"); 268 else if (code == GLFW_INVALID_ENUM) 269 strcpy(description, "Invalid argument for enum parameter"); 270 else if (code == GLFW_INVALID_VALUE) 271 strcpy(description, "Invalid value for parameter"); 272 else if (code == GLFW_OUT_OF_MEMORY) 273 strcpy(description, "Out of memory"); 274 else if (code == GLFW_API_UNAVAILABLE) 275 strcpy(description, "The requested API is unavailable"); 276 else if (code == GLFW_VERSION_UNAVAILABLE) 277 strcpy(description, "The requested API version is unavailable"); 278 else if (code == GLFW_PLATFORM_ERROR) 279 strcpy(description, "A platform-specific error occurred"); 280 else if (code == GLFW_FORMAT_UNAVAILABLE) 281 strcpy(description, "The requested format is unavailable"); 282 else if (code == GLFW_NO_WINDOW_CONTEXT) 283 strcpy(description, "The specified window has no context"); 284 else 285 strcpy(description, "ERROR: UNKNOWN GLFW ERROR"); 286 } 287 288 if (_glfw.initialized) 289 { 290 error = _glfwPlatformGetTls(&_glfw.errorSlot); 291 if (!error) 292 { 293 error = calloc(1, sizeof(_GLFWerror)); 294 _glfwPlatformSetTls(&_glfw.errorSlot, error); 295 _glfwPlatformLockMutex(&_glfw.errorLock); 296 error->next = _glfw.errorListHead; 297 _glfw.errorListHead = error; 298 _glfwPlatformUnlockMutex(&_glfw.errorLock); 299 } 300 } 301 else 302 error = &_glfwMainThreadError; 303 304 error->code = code; 305 strcpy(error->description, description); 306 307 if (_glfwErrorCallback) 308 _glfwErrorCallback(code, description); 309 } 310 311 312 ////////////////////////////////////////////////////////////////////////// 313 ////// GLFW public API ////// 314 ////////////////////////////////////////////////////////////////////////// 315 316 GLFWAPI int glfwInit(void) 317 { 318 if (_glfw.initialized) 319 return GLFW_TRUE; 320 321 memset(&_glfw, 0, sizeof(_glfw)); 322 _glfw.hints.init = _glfwInitHints; 323 324 if (!_glfwPlatformInit()) 325 { 326 terminate(); 327 return GLFW_FALSE; 328 } 329 330 if (!_glfwPlatformCreateMutex(&_glfw.errorLock) || 331 !_glfwPlatformCreateTls(&_glfw.errorSlot) || 332 !_glfwPlatformCreateTls(&_glfw.contextSlot)) 333 { 334 terminate(); 335 return GLFW_FALSE; 336 } 337 338 _glfwPlatformSetTls(&_glfw.errorSlot, &_glfwMainThreadError); 339 340 _glfwInitGamepadMappings(); 341 342 _glfw.initialized = GLFW_TRUE; 343 _glfw.timer.offset = _glfwPlatformGetTimerValue(); 344 345 glfwDefaultWindowHints(); 346 return GLFW_TRUE; 347 } 348 349 GLFWAPI void glfwTerminate(void) 350 { 351 if (!_glfw.initialized) 352 return; 353 354 terminate(); 355 } 356 357 GLFWAPI void glfwInitHint(int hint, int value) 358 { 359 switch (hint) 360 { 361 case GLFW_JOYSTICK_HAT_BUTTONS: 362 _glfwInitHints.hatButtons = value; 363 return; 364 case GLFW_COCOA_CHDIR_RESOURCES: 365 _glfwInitHints.ns.chdir = value; 366 return; 367 case GLFW_COCOA_MENUBAR: 368 _glfwInitHints.ns.menubar = value; 369 return; 370 } 371 372 _glfwInputError(GLFW_INVALID_ENUM, 373 "Invalid init hint 0x%08X", hint); 374 } 375 376 GLFWAPI void glfwGetVersion(int* major, int* minor, int* rev) 377 { 378 if (major != NULL) 379 *major = GLFW_VERSION_MAJOR; 380 if (minor != NULL) 381 *minor = GLFW_VERSION_MINOR; 382 if (rev != NULL) 383 *rev = GLFW_VERSION_REVISION; 384 } 385 386 GLFWAPI const char* glfwGetVersionString(void) 387 { 388 return _glfwPlatformGetVersionString(); 389 } 390 391 GLFWAPI int glfwGetError(const char** description) 392 { 393 _GLFWerror* error; 394 int code = GLFW_NO_ERROR; 395 396 if (description) 397 *description = NULL; 398 399 if (_glfw.initialized) 400 error = _glfwPlatformGetTls(&_glfw.errorSlot); 401 else 402 error = &_glfwMainThreadError; 403 404 if (error) 405 { 406 code = error->code; 407 error->code = GLFW_NO_ERROR; 408 if (description && code) 409 *description = error->description; 410 } 411 412 return code; 413 } 414 415 GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun) 416 { 417 _GLFW_SWAP_POINTERS(_glfwErrorCallback, cbfun); 418 return cbfun; 419 } 420