init.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //========================================================================
  2. // GLFW 3.4 - www.glfw.org
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2002-2006 Marcus Geelnard
  5. // Copyright (c) 2006-2018 Camilla Löwy <[email protected]>
  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. #include "internal.h"
  30. #include "mappings.h"
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <stdarg.h>
  35. #include <assert.h>
  36. // The global variables below comprise all mutable global data in GLFW
  37. //
  38. // Any other global variable is a bug
  39. // Global state shared between compilation units of GLFW
  40. //
  41. _GLFWlibrary _glfw = { GLFW_FALSE };
  42. // These are outside of _glfw so they can be used before initialization and
  43. // after termination
  44. //
  45. static _GLFWerror _glfwMainThreadError;
  46. static GLFWerrorfun _glfwErrorCallback;
  47. static _GLFWinitconfig _glfwInitHints =
  48. {
  49. GLFW_TRUE, // hat buttons
  50. {
  51. GLFW_TRUE, // macOS menu bar
  52. GLFW_TRUE // macOS bundle chdir
  53. }
  54. };
  55. // Terminate the library
  56. //
  57. static void _glfw_terminate(void)
  58. {
  59. int i;
  60. memset(&_glfw.callbacks, 0, sizeof(_glfw.callbacks));
  61. while (_glfw.windowListHead)
  62. glfwDestroyWindow((GLFWwindow*) _glfw.windowListHead);
  63. while (_glfw.cursorListHead)
  64. glfwDestroyCursor((GLFWcursor*) _glfw.cursorListHead);
  65. for (i = 0; i < _glfw.monitorCount; i++)
  66. {
  67. _GLFWmonitor* monitor = _glfw.monitors[i];
  68. if (monitor->originalRamp.size)
  69. _glfwPlatformSetGammaRamp(monitor, &monitor->originalRamp);
  70. _glfwFreeMonitor(monitor);
  71. }
  72. free(_glfw.monitors);
  73. _glfw.monitors = NULL;
  74. _glfw.monitorCount = 0;
  75. free(_glfw.mappings);
  76. _glfw.mappings = NULL;
  77. _glfw.mappingCount = 0;
  78. _glfwTerminateVulkan();
  79. _glfwPlatformTerminate();
  80. _glfw.initialized = GLFW_FALSE;
  81. while (_glfw.errorListHead)
  82. {
  83. _GLFWerror* error = _glfw.errorListHead;
  84. _glfw.errorListHead = error->next;
  85. free(error);
  86. }
  87. _glfwPlatformDestroyTls(&_glfw.contextSlot);
  88. _glfwPlatformDestroyTls(&_glfw.errorSlot);
  89. _glfwPlatformDestroyMutex(&_glfw.errorLock);
  90. memset(&_glfw, 0, sizeof(_glfw));
  91. }
  92. //////////////////////////////////////////////////////////////////////////
  93. ////// GLFW internal API //////
  94. //////////////////////////////////////////////////////////////////////////
  95. char* _glfw_strdup(const char* source)
  96. {
  97. const size_t length = strlen(source);
  98. char* result = (char*)calloc(length + 1, 1);
  99. strcpy(result, source);
  100. return result;
  101. }
  102. float _glfw_fminf(float a, float b)
  103. {
  104. if (a != a)
  105. return b;
  106. else if (b != b)
  107. return a;
  108. else if (a < b)
  109. return a;
  110. else
  111. return b;
  112. }
  113. float _glfw_fmaxf(float a, float b)
  114. {
  115. if (a != a)
  116. return b;
  117. else if (b != b)
  118. return a;
  119. else if (a > b)
  120. return a;
  121. else
  122. return b;
  123. }
  124. //////////////////////////////////////////////////////////////////////////
  125. ////// GLFW event API //////
  126. //////////////////////////////////////////////////////////////////////////
  127. // Notifies shared code of an error
  128. //
  129. void _glfwInputError(int code, const char* format, ...)
  130. {
  131. _GLFWerror* error;
  132. char description[_GLFW_MESSAGE_SIZE];
  133. if (format)
  134. {
  135. va_list vl;
  136. va_start(vl, format);
  137. vsnprintf(description, sizeof(description), format, vl);
  138. va_end(vl);
  139. description[sizeof(description) - 1] = '\0';
  140. }
  141. else
  142. {
  143. if (code == GLFW_NOT_INITIALIZED)
  144. strcpy(description, "The GLFW library is not initialized");
  145. else if (code == GLFW_NO_CURRENT_CONTEXT)
  146. strcpy(description, "There is no current context");
  147. else if (code == GLFW_INVALID_ENUM)
  148. strcpy(description, "Invalid argument for enum parameter");
  149. else if (code == GLFW_INVALID_VALUE)
  150. strcpy(description, "Invalid value for parameter");
  151. else if (code == GLFW_OUT_OF_MEMORY)
  152. strcpy(description, "Out of memory");
  153. else if (code == GLFW_API_UNAVAILABLE)
  154. strcpy(description, "The requested API is unavailable");
  155. else if (code == GLFW_VERSION_UNAVAILABLE)
  156. strcpy(description, "The requested API version is unavailable");
  157. else if (code == GLFW_PLATFORM_ERROR)
  158. strcpy(description, "A platform-specific error occurred");
  159. else if (code == GLFW_FORMAT_UNAVAILABLE)
  160. strcpy(description, "The requested format is unavailable");
  161. else if (code == GLFW_NO_WINDOW_CONTEXT)
  162. strcpy(description, "The specified window has no context");
  163. else if (code == GLFW_CURSOR_UNAVAILABLE)
  164. strcpy(description, "The specified cursor shape is unavailable");
  165. else
  166. strcpy(description, "ERROR: UNKNOWN GLFW ERROR");
  167. }
  168. if (_glfw.initialized)
  169. {
  170. error = (_GLFWerror*)_glfwPlatformGetTls(&_glfw.errorSlot);
  171. if (!error)
  172. {
  173. error = (_GLFWerror*)calloc(1, sizeof(_GLFWerror));
  174. _glfwPlatformSetTls(&_glfw.errorSlot, error);
  175. _glfwPlatformLockMutex(&_glfw.errorLock);
  176. error->next = _glfw.errorListHead;
  177. _glfw.errorListHead = error;
  178. _glfwPlatformUnlockMutex(&_glfw.errorLock);
  179. }
  180. }
  181. else
  182. error = &_glfwMainThreadError;
  183. error->code = code;
  184. strcpy(error->description, description);
  185. if (_glfwErrorCallback)
  186. _glfwErrorCallback(code, description);
  187. }
  188. //////////////////////////////////////////////////////////////////////////
  189. ////// GLFW public API //////
  190. //////////////////////////////////////////////////////////////////////////
  191. GLFWAPI int glfwInit(void)
  192. {
  193. if (_glfw.initialized)
  194. return GLFW_TRUE;
  195. memset(&_glfw, 0, sizeof(_glfw));
  196. _glfw.hints.init = _glfwInitHints;
  197. if (!_glfwPlatformInit())
  198. {
  199. _glfw_terminate();
  200. return GLFW_FALSE;
  201. }
  202. if (!_glfwPlatformCreateMutex(&_glfw.errorLock) ||
  203. !_glfwPlatformCreateTls(&_glfw.errorSlot) ||
  204. !_glfwPlatformCreateTls(&_glfw.contextSlot))
  205. {
  206. _glfw_terminate();
  207. return GLFW_FALSE;
  208. }
  209. _glfwPlatformSetTls(&_glfw.errorSlot, &_glfwMainThreadError);
  210. _glfw.initialized = GLFW_TRUE;
  211. _glfw.timer.offset = _glfwPlatformGetTimerValue();
  212. glfwDefaultWindowHints();
  213. {
  214. int i;
  215. for (i = 0; _glfwDefaultMappings[i]; i++)
  216. {
  217. if (!glfwUpdateGamepadMappings(_glfwDefaultMappings[i]))
  218. {
  219. _glfw_terminate();
  220. return GLFW_FALSE;
  221. }
  222. }
  223. }
  224. return GLFW_TRUE;
  225. }
  226. GLFWAPI void glfwTerminate(void)
  227. {
  228. if (!_glfw.initialized)
  229. return;
  230. _glfw_terminate();
  231. }
  232. GLFWAPI void glfwInitHint(int hint, int value)
  233. {
  234. switch (hint)
  235. {
  236. case GLFW_JOYSTICK_HAT_BUTTONS:
  237. _glfwInitHints.hatButtons = value;
  238. return;
  239. case GLFW_COCOA_CHDIR_RESOURCES:
  240. _glfwInitHints.ns.chdir = value;
  241. return;
  242. case GLFW_COCOA_MENUBAR:
  243. _glfwInitHints.ns.menubar = value;
  244. return;
  245. }
  246. _glfwInputError(GLFW_INVALID_ENUM,
  247. "Invalid init hint 0x%08X", hint);
  248. }
  249. GLFWAPI void glfwGetVersion(int* major, int* minor, int* rev)
  250. {
  251. if (major != NULL)
  252. *major = GLFW_VERSION_MAJOR;
  253. if (minor != NULL)
  254. *minor = GLFW_VERSION_MINOR;
  255. if (rev != NULL)
  256. *rev = GLFW_VERSION_REVISION;
  257. }
  258. GLFWAPI const char* glfwGetVersionString(void)
  259. {
  260. return _glfwPlatformGetVersionString();
  261. }
  262. GLFWAPI int glfwGetError(const char** description)
  263. {
  264. _GLFWerror* error;
  265. int code = GLFW_NO_ERROR;
  266. if (description)
  267. *description = NULL;
  268. if (_glfw.initialized)
  269. error = (_GLFWerror*)_glfwPlatformGetTls(&_glfw.errorSlot);
  270. else
  271. error = &_glfwMainThreadError;
  272. if (error)
  273. {
  274. code = error->code;
  275. error->code = GLFW_NO_ERROR;
  276. if (description && code)
  277. *description = error->description;
  278. }
  279. return code;
  280. }
  281. GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
  282. {
  283. _GLFW_SWAP_POINTERS(GLFWerrorfun, _glfwErrorCallback, cbfun);
  284. return cbfun;
  285. }