init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #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 = { 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. .hatButtons = true,
  49. .angleType = GLFW_ANGLE_PLATFORM_TYPE_NONE,
  50. .debugKeyboard = false,
  51. .debugRendering = false,
  52. .ns = {
  53. .menubar = true, // macOS menu bar
  54. .chdir = true // macOS bundle chdir
  55. }
  56. };
  57. // Terminate the library
  58. //
  59. static void terminate(void)
  60. {
  61. int i;
  62. memset(&_glfw.callbacks, 0, sizeof(_glfw.callbacks));
  63. _glfw_free_clipboard_data(&_glfw.clipboard);
  64. _glfw_free_clipboard_data(&_glfw.primary);
  65. while (_glfw.windowListHead)
  66. glfwDestroyWindow((GLFWwindow*) _glfw.windowListHead);
  67. while (_glfw.cursorListHead)
  68. glfwDestroyCursor((GLFWcursor*) _glfw.cursorListHead);
  69. for (i = 0; i < _glfw.monitorCount; i++)
  70. {
  71. _GLFWmonitor* monitor = _glfw.monitors[i];
  72. if (monitor->originalRamp.size)
  73. _glfwPlatformSetGammaRamp(monitor, &monitor->originalRamp);
  74. _glfwFreeMonitor(monitor);
  75. }
  76. free(_glfw.monitors);
  77. _glfw.monitors = NULL;
  78. _glfw.monitorCount = 0;
  79. free(_glfw.mappings);
  80. _glfw.mappings = NULL;
  81. _glfw.mappingCount = 0;
  82. _glfwTerminateVulkan();
  83. _glfwPlatformTerminateJoysticks();
  84. _glfwPlatformTerminate();
  85. _glfw.initialized = false;
  86. while (_glfw.errorListHead)
  87. {
  88. _GLFWerror* error = _glfw.errorListHead;
  89. _glfw.errorListHead = error->next;
  90. free(error);
  91. }
  92. _glfwPlatformDestroyTls(&_glfw.contextSlot);
  93. _glfwPlatformDestroyTls(&_glfw.errorSlot);
  94. _glfwPlatformDestroyMutex(&_glfw.errorLock);
  95. memset(&_glfw, 0, sizeof(_glfw));
  96. }
  97. //////////////////////////////////////////////////////////////////////////
  98. ////// GLFW internal API //////
  99. //////////////////////////////////////////////////////////////////////////
  100. char* _glfw_strdup(const char* source)
  101. {
  102. const size_t length = strlen(source);
  103. char* result = malloc(length + 1);
  104. memcpy(result, source, length);
  105. result[length] = 0;
  106. return result;
  107. }
  108. //////////////////////////////////////////////////////////////////////////
  109. ////// GLFW event API //////
  110. //////////////////////////////////////////////////////////////////////////
  111. // Notifies shared code of an error
  112. //
  113. void _glfwInputError(int code, const char* format, ...)
  114. {
  115. _GLFWerror* error;
  116. char description[_GLFW_MESSAGE_SIZE];
  117. if (format)
  118. {
  119. va_list vl;
  120. va_start(vl, format);
  121. vsnprintf(description, sizeof(description), format, vl);
  122. va_end(vl);
  123. description[sizeof(description) - 1] = '\0';
  124. }
  125. else
  126. {
  127. if (code == GLFW_NOT_INITIALIZED)
  128. strcpy(description, "The GLFW library is not initialized");
  129. else if (code == GLFW_NO_CURRENT_CONTEXT)
  130. strcpy(description, "There is no current context");
  131. else if (code == GLFW_INVALID_ENUM)
  132. strcpy(description, "Invalid argument for enum parameter");
  133. else if (code == GLFW_INVALID_VALUE)
  134. strcpy(description, "Invalid value for parameter");
  135. else if (code == GLFW_OUT_OF_MEMORY)
  136. strcpy(description, "Out of memory");
  137. else if (code == GLFW_API_UNAVAILABLE)
  138. strcpy(description, "The requested API is unavailable");
  139. else if (code == GLFW_VERSION_UNAVAILABLE)
  140. strcpy(description, "The requested API version is unavailable");
  141. else if (code == GLFW_PLATFORM_ERROR)
  142. strcpy(description, "A platform-specific error occurred");
  143. else if (code == GLFW_FORMAT_UNAVAILABLE)
  144. strcpy(description, "The requested format is unavailable");
  145. else if (code == GLFW_NO_WINDOW_CONTEXT)
  146. strcpy(description, "The specified window has no context");
  147. else if (code == GLFW_FEATURE_UNAVAILABLE)
  148. strcpy(description, "The requested feature cannot be implemented for this platform");
  149. else if (code == GLFW_FEATURE_UNIMPLEMENTED)
  150. strcpy(description, "The requested feature has not yet been implemented for this platform");
  151. else
  152. strcpy(description, "ERROR: UNKNOWN GLFW ERROR");
  153. }
  154. if (_glfw.initialized)
  155. {
  156. error = _glfwPlatformGetTls(&_glfw.errorSlot);
  157. if (!error)
  158. {
  159. error = calloc(1, sizeof(_GLFWerror));
  160. _glfwPlatformSetTls(&_glfw.errorSlot, error);
  161. _glfwPlatformLockMutex(&_glfw.errorLock);
  162. error->next = _glfw.errorListHead;
  163. _glfw.errorListHead = error;
  164. _glfwPlatformUnlockMutex(&_glfw.errorLock);
  165. }
  166. }
  167. else
  168. error = &_glfwMainThreadError;
  169. error->code = code;
  170. strcpy(error->description, description);
  171. if (_glfwErrorCallback)
  172. _glfwErrorCallback(code, description);
  173. }
  174. void
  175. _glfwDebug(const char *format, ...) {
  176. if (format)
  177. {
  178. va_list vl;
  179. fprintf(stderr, "[%.4f] ", monotonic_t_to_s_double(glfwGetTime()));
  180. va_start(vl, format);
  181. vfprintf(stderr, format, vl);
  182. va_end(vl);
  183. fprintf(stderr, "\n");
  184. }
  185. }
  186. //////////////////////////////////////////////////////////////////////////
  187. ////// GLFW public API //////
  188. //////////////////////////////////////////////////////////////////////////
  189. GLFWAPI int glfwInit(monotonic_t start_time)
  190. {
  191. if (_glfw.initialized)
  192. return true;
  193. monotonic_start_time = start_time;
  194. memset(&_glfw, 0, sizeof(_glfw));
  195. _glfw.hints.init = _glfwInitHints;
  196. _glfw.ignoreOSKeyboardProcessing = false;
  197. if (!_glfwPlatformInit())
  198. {
  199. terminate();
  200. return false;
  201. }
  202. if (!_glfwPlatformCreateMutex(&_glfw.errorLock) ||
  203. !_glfwPlatformCreateTls(&_glfw.errorSlot) ||
  204. !_glfwPlatformCreateTls(&_glfw.contextSlot))
  205. {
  206. terminate();
  207. return false;
  208. }
  209. _glfwPlatformSetTls(&_glfw.errorSlot, &_glfwMainThreadError);
  210. _glfw.initialized = true;
  211. glfwDefaultWindowHints();
  212. {
  213. int i;
  214. for (i = 0; _glfwDefaultMappings[i]; i++)
  215. {
  216. if (!glfwUpdateGamepadMappings(_glfwDefaultMappings[i]))
  217. {
  218. terminate();
  219. return false;
  220. }
  221. }
  222. }
  223. return true;
  224. }
  225. GLFWAPI void glfwTerminate(void)
  226. {
  227. if (!_glfw.initialized)
  228. return;
  229. terminate();
  230. }
  231. GLFWAPI void glfwInitHint(int hint, int value)
  232. {
  233. switch (hint)
  234. {
  235. case GLFW_JOYSTICK_HAT_BUTTONS:
  236. _glfwInitHints.hatButtons = value;
  237. return;
  238. case GLFW_ANGLE_PLATFORM_TYPE:
  239. _glfwInitHints.angleType = value;
  240. return;
  241. case GLFW_DEBUG_KEYBOARD:
  242. _glfwInitHints.debugKeyboard = value;
  243. return;
  244. case GLFW_DEBUG_RENDERING:
  245. _glfwInitHints.debugRendering = value;
  246. return;
  247. case GLFW_COCOA_CHDIR_RESOURCES:
  248. _glfwInitHints.ns.chdir = value;
  249. return;
  250. case GLFW_COCOA_MENUBAR:
  251. _glfwInitHints.ns.menubar = value;
  252. return;
  253. }
  254. _glfwInputError(GLFW_INVALID_ENUM,
  255. "Invalid init hint 0x%08X", hint);
  256. }
  257. GLFWAPI void glfwGetVersion(int* major, int* minor, int* rev)
  258. {
  259. if (major != NULL)
  260. *major = GLFW_VERSION_MAJOR;
  261. if (minor != NULL)
  262. *minor = GLFW_VERSION_MINOR;
  263. if (rev != NULL)
  264. *rev = GLFW_VERSION_REVISION;
  265. }
  266. GLFWAPI const char* glfwGetVersionString(void)
  267. {
  268. return _glfwPlatformGetVersionString();
  269. }
  270. GLFWAPI int glfwGetError(const char** description)
  271. {
  272. _GLFWerror* error;
  273. int code = GLFW_NO_ERROR;
  274. if (description)
  275. *description = NULL;
  276. if (_glfw.initialized)
  277. error = _glfwPlatformGetTls(&_glfw.errorSlot);
  278. else
  279. error = &_glfwMainThreadError;
  280. if (error)
  281. {
  282. code = error->code;
  283. error->code = GLFW_NO_ERROR;
  284. if (description && code)
  285. *description = error->description;
  286. }
  287. return code;
  288. }
  289. GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
  290. {
  291. _GLFW_SWAP_POINTERS(_glfwErrorCallback, cbfun);
  292. return cbfun;
  293. }
  294. GLFWAPI void glfwRunMainLoop(GLFWtickcallback callback, void *data)
  295. {
  296. _GLFW_REQUIRE_INIT();
  297. _glfwPlatformRunMainLoop(callback, data);
  298. }
  299. GLFWAPI void glfwStopMainLoop(void) {
  300. _GLFW_REQUIRE_INIT();
  301. _glfwPlatformStopMainLoop();
  302. }
  303. GLFWAPI unsigned long long glfwAddTimer(
  304. monotonic_t interval, bool repeats, GLFWuserdatafun callback,
  305. void *callback_data, GLFWuserdatafun free_callback)
  306. {
  307. return _glfwPlatformAddTimer(interval, repeats, callback, callback_data, free_callback);
  308. }
  309. GLFWAPI void glfwUpdateTimer(unsigned long long timer_id, monotonic_t interval, bool enabled) {
  310. _glfwPlatformUpdateTimer(timer_id, interval, enabled);
  311. }
  312. GLFWAPI void glfwRemoveTimer(unsigned long long timer_id) {
  313. _glfwPlatformRemoveTimer(timer_id);
  314. }
  315. GLFWAPI GLFWapplicationclosefun glfwSetApplicationCloseCallback(GLFWapplicationclosefun cbfun)
  316. {
  317. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  318. _GLFW_SWAP_POINTERS(_glfw.callbacks.application_close, cbfun);
  319. return cbfun;
  320. }
  321. GLFWAPI GLFWapplicationclosefun glfwSetSystemColorThemeChangeCallback(GLFWsystemcolorthemechangefun cbfun)
  322. {
  323. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  324. _GLFW_SWAP_POINTERS(_glfw.callbacks.system_color_theme_change, cbfun);
  325. return cbfun;
  326. }
  327. GLFWAPI GLFWdrawtextfun glfwSetDrawTextFunction(GLFWdrawtextfun cbfun)
  328. {
  329. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  330. _GLFW_SWAP_POINTERS(_glfw.callbacks.draw_text, cbfun);
  331. return cbfun;
  332. }
  333. GLFWAPI GLFWcurrentselectionfun glfwSetCurrentSelectionCallback(GLFWcurrentselectionfun cbfun)
  334. {
  335. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  336. _GLFW_SWAP_POINTERS(_glfw.callbacks.get_current_selection, cbfun);
  337. return cbfun;
  338. }
  339. GLFWAPI GLFWhascurrentselectionfun glfwSetHasCurrentSelectionCallback(GLFWhascurrentselectionfun cbfun)
  340. {
  341. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  342. _GLFW_SWAP_POINTERS(_glfw.callbacks.has_current_selection, cbfun);
  343. return cbfun;
  344. }
  345. GLFWAPI GLFWimecursorpositionfun glfwSetIMECursorPositionCallback(GLFWimecursorpositionfun cbfun)
  346. {
  347. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  348. _GLFW_SWAP_POINTERS(_glfw.callbacks.get_ime_cursor_position, cbfun);
  349. return cbfun;
  350. }