init.c 12 KB

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