Wise&mystical  1.0
Project about Europe
Loading...
Searching...
No Matches
init.c
Go to the documentation of this file.
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
30#include "internal.h"
31#include "mappings.h"
32
33#include <string.h>
34#include <stdlib.h>
35#include <stdio.h>
36#include <stdarg.h>
37#include <assert.h>
38
39
40// The global variables below comprise all mutable global data in GLFW
41//
42// Any other global variable is a bug
43
44// Global state shared between compilation units of GLFW
45//
47
48// These are outside of _glfw so they can be used before initialization and
49// after termination
50//
51static _GLFWerror _glfwMainThreadError;
52static GLFWerrorfun _glfwErrorCallback;
53static _GLFWinitconfig _glfwInitHints =
54{
55 GLFW_TRUE, // hat buttons
56 GLFW_ANGLE_PLATFORM_TYPE_NONE, // ANGLE backend
57 {
58 GLFW_TRUE, // macOS menu bar
59 GLFW_TRUE // macOS bundle chdir
60 },
61 {
62 GLFW_TRUE, // X11 XCB Vulkan surface
63 },
64};
65
66// Terminate the library
67//
68static void terminate(void)
69{
70 int i;
71
72 memset(&_glfw.callbacks, 0, sizeof(_glfw.callbacks));
73
74 while (_glfw.windowListHead)
76
77 while (_glfw.cursorListHead)
79
80 for (i = 0; i < _glfw.monitorCount; i++)
81 {
82 _GLFWmonitor* monitor = _glfw.monitors[i];
83 if (monitor->originalRamp.size)
84 _glfwPlatformSetGammaRamp(monitor, &monitor->originalRamp);
85 _glfwFreeMonitor(monitor);
86 }
87
88 free(_glfw.monitors);
91
92 free(_glfw.mappings);
95
99
101
102 while (_glfw.errorListHead)
103 {
105 _glfw.errorListHead = error->next;
106 free(error);
107 }
108
112
113 memset(&_glfw, 0, sizeof(_glfw));
114}
115
116
120
121char* _glfw_strdup(const char* source)
122{
123 const size_t length = strlen(source);
124 char* result = calloc(length + 1, 1);
125 strcpy(result, source);
126 return result;
127}
128
129float _glfw_fminf(float a, float b)
130{
131 if (a != a)
132 return b;
133 else if (b != b)
134 return a;
135 else if (a < b)
136 return a;
137 else
138 return b;
139}
140
141float _glfw_fmaxf(float a, float b)
142{
143 if (a != a)
144 return b;
145 else if (b != b)
146 return a;
147 else if (a > b)
148 return a;
149 else
150 return b;
151}
152
153
157
158// Notifies shared code of an error
159//
160void _glfwInputError(int code, const char* format, ...)
161{
162 _GLFWerror* error;
163 char description[_GLFW_MESSAGE_SIZE];
164
165 if (format)
166 {
167 va_list vl;
168
169 va_start(vl, format);
170 vsnprintf(description, sizeof(description), format, vl);
171 va_end(vl);
172
173 description[sizeof(description) - 1] = '\0';
174 }
175 else
176 {
177 if (code == GLFW_NOT_INITIALIZED)
178 strcpy(description, "The GLFW library is not initialized");
179 else if (code == GLFW_NO_CURRENT_CONTEXT)
180 strcpy(description, "There is no current context");
181 else if (code == GLFW_INVALID_ENUM)
182 strcpy(description, "Invalid argument for enum parameter");
183 else if (code == GLFW_INVALID_VALUE)
184 strcpy(description, "Invalid value for parameter");
185 else if (code == GLFW_OUT_OF_MEMORY)
186 strcpy(description, "Out of memory");
187 else if (code == GLFW_API_UNAVAILABLE)
188 strcpy(description, "The requested API is unavailable");
189 else if (code == GLFW_VERSION_UNAVAILABLE)
190 strcpy(description, "The requested API version is unavailable");
191 else if (code == GLFW_PLATFORM_ERROR)
192 strcpy(description, "A platform-specific error occurred");
193 else if (code == GLFW_FORMAT_UNAVAILABLE)
194 strcpy(description, "The requested format is unavailable");
195 else if (code == GLFW_NO_WINDOW_CONTEXT)
196 strcpy(description, "The specified window has no context");
197 else if (code == GLFW_CURSOR_UNAVAILABLE)
198 strcpy(description, "The specified cursor shape is unavailable");
199 else if (code == GLFW_FEATURE_UNAVAILABLE)
200 strcpy(description, "The requested feature cannot be implemented for this platform");
201 else if (code == GLFW_FEATURE_UNIMPLEMENTED)
202 strcpy(description, "The requested feature has not yet been implemented for this platform");
203 else
204 strcpy(description, "ERROR: UNKNOWN GLFW ERROR");
205 }
206
207 if (_glfw.initialized)
208 {
210 if (!error)
211 {
212 error = calloc(1, sizeof(_GLFWerror));
215 error->next = _glfw.errorListHead;
216 _glfw.errorListHead = error;
218 }
219 }
220 else
221 error = &_glfwMainThreadError;
222
223 error->code = code;
224 strcpy(error->description, description);
225
226 if (_glfwErrorCallback)
227 _glfwErrorCallback(code, description);
228}
229
230
234
236{
237 if (_glfw.initialized)
238 return GLFW_TRUE;
239
240 memset(&_glfw, 0, sizeof(_glfw));
241 _glfw.hints.init = _glfwInitHints;
242
243 if (!_glfwPlatformInit())
244 {
245 terminate();
246 return GLFW_FALSE;
247 }
248
252 {
253 terminate();
254 return GLFW_FALSE;
255 }
256
257 _glfwPlatformSetTls(&_glfw.errorSlot, &_glfwMainThreadError);
258
261
263
264 {
265 int i;
266
267 for (i = 0; _glfwDefaultMappings[i]; i++)
268 {
270 {
271 terminate();
272 return GLFW_FALSE;
273 }
274 }
275 }
276
277 return GLFW_TRUE;
278}
279
281{
282 if (!_glfw.initialized)
283 return;
284
285 terminate();
286}
287
288GLFWAPI void glfwInitHint(int hint, int value)
289{
290 switch (hint)
291 {
293 _glfwInitHints.hatButtons = value;
294 return;
296 _glfwInitHints.angleType = value;
297 return;
299 _glfwInitHints.ns.chdir = value;
300 return;
302 _glfwInitHints.ns.menubar = value;
303 return;
305 _glfwInitHints.x11.xcbVulkanSurface = value;
306 return;
307 }
308
310 "Invalid init hint 0x%08X", hint);
311}
312
313GLFWAPI void glfwGetVersion(int* major, int* minor, int* rev)
314{
315 if (major != NULL)
316 *major = GLFW_VERSION_MAJOR;
317 if (minor != NULL)
318 *minor = GLFW_VERSION_MINOR;
319 if (rev != NULL)
321}
322
324{
326}
327
328GLFWAPI int glfwGetError(const char** description)
329{
330 _GLFWerror* error;
331 int code = GLFW_NO_ERROR;
332
333 if (description)
334 *description = NULL;
335
336 if (_glfw.initialized)
338 else
339 error = &_glfwMainThreadError;
340
341 if (error)
342 {
343 code = error->code;
344 error->code = GLFW_NO_ERROR;
345 if (description && code)
346 *description = error->description;
347 }
348
349 return code;
350}
351
353{
354 _GLFW_SWAP_POINTERS(_glfwErrorCallback, cbfun);
355 return cbfun;
356}
357
const char * _glfwPlatformGetVersionString(void)
Definition: cocoa_init.m:612
void _glfwPlatformTerminate(void)
Definition: cocoa_init.m:566
int _glfwPlatformInit(void)
Definition: cocoa_init.m:492
void _glfwPlatformTerminateJoysticks(void)
void _glfwPlatformSetGammaRamp(_GLFWmonitor *monitor, const GLFWgammaramp *ramp)
uint64_t _glfwPlatformGetTimerValue(void)
Definition: cocoa_time.c:53
#define GLFWAPI
Definition: glfw3.h:269
#define GLFW_ANGLE_PLATFORM_TYPE_NONE
Definition: glfw3.h:1116
#define GLFW_CURSOR_UNAVAILABLE
The specified cursor shape is not available.
Definition: glfw3.h:796
#define GLFW_FORMAT_UNAVAILABLE
The requested format is not supported or available.
Definition: glfw3.h:777
#define GLFW_NOT_INITIALIZED
GLFW has not been initialized.
Definition: glfw3.h:677
#define GLFW_FEATURE_UNAVAILABLE
The requested feature is not provided by the platform.
Definition: glfw3.h:810
#define GLFW_API_UNAVAILABLE
GLFW could not find support for the requested API on the system.
Definition: glfw3.h:730
#define GLFW_FEATURE_UNIMPLEMENTED
The requested feature is not implemented for the platform.
Definition: glfw3.h:823
#define GLFW_INVALID_ENUM
One of the arguments to the function was an invalid enum value.
Definition: glfw3.h:695
#define GLFW_OUT_OF_MEMORY
A memory allocation failed.
Definition: glfw3.h:714
#define GLFW_NO_CURRENT_CONTEXT
No context is current for this thread.
Definition: glfw3.h:687
#define GLFW_INVALID_VALUE
One of the arguments to the function was an invalid value.
Definition: glfw3.h:706
#define GLFW_NO_WINDOW_CONTEXT
The specified window does not have an OpenGL or OpenGL ES context.
Definition: glfw3.h:785
#define GLFW_VERSION_UNAVAILABLE
The requested OpenGL or OpenGL ES version is not available.
Definition: glfw3.h:747
#define GLFW_PLATFORM_ERROR
A platform-specific error occurred that does not match any of the more specific categories.
Definition: glfw3.h:758
#define GLFW_NO_ERROR
No error has occurred.
Definition: glfw3.h:668
GLFWAPI void glfwGetVersion(int *major, int *minor, int *rev)
Retrieves the version of the GLFW library.
Definition: init.c:313
#define GLFW_TRUE
One.
Definition: glfw3.h:310
#define GLFW_VERSION_MAJOR
The major version number of the GLFW header.
Definition: glfw3.h:285
#define GLFW_COCOA_MENUBAR
macOS specific init hint.
Definition: glfw3.h:1254
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:352
#define GLFW_X11_XCB_VULKAN_SURFACE
X11 specific init hint.
Definition: glfw3.h:1259
void(* GLFWerrorfun)(int, const char *)
The function pointer type for error callbacks.
Definition: glfw3.h:1355
GLFWAPI int glfwInit(void)
GLFW API functions.
Definition: init.c:235
#define GLFW_VERSION_REVISION
The revision number of the GLFW header.
Definition: glfw3.h:299
#define GLFW_COCOA_CHDIR_RESOURCES
macOS specific init hint.
Definition: glfw3.h:1249
#define GLFW_JOYSTICK_HAT_BUTTONS
Joystick hat buttons init hint.
Definition: glfw3.h:1239
GLFWAPI void glfwInitHint(int hint, int value)
Sets the specified init hint to the desired value.
Definition: init.c:288
#define GLFW_FALSE
Zero.
Definition: glfw3.h:319
GLFWAPI const char * glfwGetVersionString(void)
Returns a string describing the compile-time configuration.
Definition: init.c:323
#define GLFW_ANGLE_PLATFORM_TYPE
ANGLE rendering backend init hint.
Definition: glfw3.h:1244
GLFWAPI int glfwGetError(const char **description)
Returns and clears the last error for the calling thread.
Definition: init.c:328
#define GLFW_VERSION_MINOR
The minor version number of the GLFW header.
Definition: glfw3.h:292
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:280
GLFWAPI void glfwDestroyCursor(GLFWcursor *cursor)
Destroys a cursor.
Definition: input.c:800
GLFWAPI int glfwUpdateGamepadMappings(const char *string)
Adds the specified SDL_GameControllerDB gamepad mappings.
Definition: input.c:1156
struct GLFWcursor GLFWcursor
Opaque cursor object.
Definition: glfw3.h:1331
struct GLFWwindow GLFWwindow
Opaque window object.
Definition: glfw3.h:1319
GLFWAPI void glfwDefaultWindowHints(void)
Resets all window hints to their default values.
Definition: window.c:255
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:453
float _glfw_fmaxf(float a, float b)
Definition: init.c:141
_GLFWlibrary _glfw
Definition: init.c:46
void _glfwInputError(int code, const char *format,...)
Definition: init.c:160
float _glfw_fminf(float a, float b)
Definition: init.c:129
char * _glfw_strdup(const char *source)
Definition: init.c:121
void _glfwPlatformLockMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:94
void _glfwPlatformSetTls(_GLFWtls *tls, void *value)
Definition: posix_thread.c:68
void _glfwPlatformDestroyTls(_GLFWtls *tls)
Definition: posix_thread.c:55
void _glfwPlatformDestroyMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:87
void _glfwFreeMonitor(_GLFWmonitor *monitor)
Definition: monitor.c:180
GLFWbool _glfwPlatformCreateMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:74
#define _GLFW_SWAP_POINTERS(x, y)
Definition: internal.h:222
void * _glfwPlatformGetTls(_GLFWtls *tls)
Definition: posix_thread.c:62
void _glfwTerminateVulkan(void)
Definition: vulkan.c:157
void _glfwPlatformUnlockMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:100
#define _GLFW_MESSAGE_SIZE
Definition: internal.h:59
GLFWbool _glfwPlatformCreateTls(_GLFWtls *tls)
Definition: posix_thread.c:40
const char * _glfwDefaultMappings[]
Definition: mappings.h:61
#define NULL
Definition: miniaudio.h:3718
char description[_GLFW_MESSAGE_SIZE]
Definition: internal.h:236
_GLFWerror * next
Definition: internal.h:234
int code
Definition: internal.h:235
struct _GLFWinitconfig::@16 ns
GLFWbool xcbVulkanSurface
Definition: internal.h:252
GLFWbool chdir
Definition: internal.h:249
struct _GLFWinitconfig::@17 x11
GLFWbool hatButtons
Definition: internal.h:245
GLFWbool menubar
Definition: internal.h:248
struct _GLFWlibrary::@26 callbacks
_GLFWmapping * mappings
Definition: internal.h:548
uint64_t offset
Definition: internal.h:556
_GLFWmutex errorLock
Definition: internal.h:553
_GLFWtls errorSlot
Definition: internal.h:551
_GLFWerror * errorListHead
Definition: internal.h:539
struct _GLFWlibrary::@24 timer
_GLFWwindow * windowListHead
Definition: internal.h:541
_GLFWinitconfig init
Definition: internal.h:532
_GLFWcursor * cursorListHead
Definition: internal.h:540
int mappingCount
Definition: internal.h:549
_GLFWtls contextSlot
Definition: internal.h:552
struct _GLFWlibrary::@23 hints
_GLFWmonitor ** monitors
Definition: internal.h:543
int monitorCount
Definition: internal.h:544
GLFWbool initialized
Definition: internal.h:529
GLFWgammaramp originalRamp
Definition: internal.h:452
unsigned int size
Definition: glfw3.h:1838