init.c
Go to the documentation of this file.
1 //========================================================================
2 // GLFW 3.3 - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2016 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 
28 #include "internal.h"
29 #include "mappings.h"
30 
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <assert.h>
36 
37 
38 // The global variables below comprise all mutable global data in GLFW
39 //
40 // Any other global variable is a bug
41 
42 // Global state shared between compilation units of GLFW
43 //
45 
46 // These are outside of _glfw so they can be used before initialization and
47 // after termination
48 //
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)
70 
71  while (_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 
92 
93  _glfw.initialized = GLFW_FALSE;
94 
95  while (_glfw.errorListHead)
96  {
98  _glfw.errorListHead = error->next;
99  free(error);
100  }
101 
105 
106  memset(&_glfw, 0, sizeof(_glfw));
107 }
108 
109 
113 
114 char* _glfw_strdup(const char* source)
115 {
116  const size_t length = strlen(source);
117  char* result = calloc(length + 1, 1);
118  strcpy(result, source);
119  return result;
120 }
121 
122 float _glfw_fminf(float a, float b)
123 {
124  if (a != a)
125  return b;
126  else if (b != b)
127  return a;
128  else if (a < b)
129  return a;
130  else
131  return b;
132 }
133 
134 float _glfw_fmaxf(float a, float b)
135 {
136  if (a != a)
137  return b;
138  else if (b != b)
139  return a;
140  else if (a > b)
141  return a;
142  else
143  return b;
144 }
145 
146 
150 
151 // Notifies shared code of an error
152 //
153 void _glfwInputError(int code, const char* format, ...)
154 {
155  _GLFWerror* error;
156  char description[_GLFW_MESSAGE_SIZE];
157 
158  if (format)
159  {
160  va_list vl;
161 
162  va_start(vl, format);
163  vsnprintf(description, sizeof(description), format, vl);
164  va_end(vl);
165 
166  description[sizeof(description) - 1] = '\0';
167  }
168  else
169  {
170  if (code == GLFW_NOT_INITIALIZED)
171  strcpy(description, "The GLFW library is not initialized");
172  else if (code == GLFW_NO_CURRENT_CONTEXT)
173  strcpy(description, "There is no current context");
174  else if (code == GLFW_INVALID_ENUM)
175  strcpy(description, "Invalid argument for enum parameter");
176  else if (code == GLFW_INVALID_VALUE)
177  strcpy(description, "Invalid value for parameter");
178  else if (code == GLFW_OUT_OF_MEMORY)
179  strcpy(description, "Out of memory");
180  else if (code == GLFW_API_UNAVAILABLE)
181  strcpy(description, "The requested API is unavailable");
182  else if (code == GLFW_VERSION_UNAVAILABLE)
183  strcpy(description, "The requested API version is unavailable");
184  else if (code == GLFW_PLATFORM_ERROR)
185  strcpy(description, "A platform-specific error occurred");
186  else if (code == GLFW_FORMAT_UNAVAILABLE)
187  strcpy(description, "The requested format is unavailable");
188  else if (code == GLFW_NO_WINDOW_CONTEXT)
189  strcpy(description, "The specified window has no context");
190  else
191  strcpy(description, "ERROR: UNKNOWN GLFW ERROR");
192  }
193 
194  if (_glfw.initialized)
195  {
196  error = _glfwPlatformGetTls(&_glfw.errorSlot);
197  if (!error)
198  {
199  error = calloc(1, sizeof(_GLFWerror));
200  _glfwPlatformSetTls(&_glfw.errorSlot, error);
202  error->next = _glfw.errorListHead;
203  _glfw.errorListHead = error;
205  }
206  }
207  else
208  error = &_glfwMainThreadError;
209 
210  error->code = code;
211  strcpy(error->description, description);
212 
213  if (_glfwErrorCallback)
214  _glfwErrorCallback(code, description);
215 }
216 
217 
221 
222 GLFWAPI int glfwInit(void)
223 {
224  if (_glfw.initialized)
225  return GLFW_TRUE;
226 
227  memset(&_glfw, 0, sizeof(_glfw));
228  _glfw.hints.init = _glfwInitHints;
229 
230  if (!_glfwPlatformInit())
231  {
232  terminate();
233  return GLFW_FALSE;
234  }
235 
236  if (!_glfwPlatformCreateMutex(&_glfw.errorLock) ||
239  {
240  terminate();
241  return GLFW_FALSE;
242  }
243 
244  _glfwPlatformSetTls(&_glfw.errorSlot, &_glfwMainThreadError);
245 
246  _glfw.initialized = GLFW_TRUE;
248 
250 
251  {
252  int i;
253 
254  for (i = 0; _glfwDefaultMappings[i]; i++)
255  {
257  {
258  terminate();
259  return GLFW_FALSE;
260  }
261  }
262  }
263 
264  return GLFW_TRUE;
265 }
266 
268 {
269  if (!_glfw.initialized)
270  return;
271 
272  terminate();
273 }
274 
275 GLFWAPI void glfwInitHint(int hint, int value)
276 {
277  switch (hint)
278  {
280  _glfwInitHints.hatButtons = value;
281  return;
283  _glfwInitHints.ns.chdir = value;
284  return;
285  case GLFW_COCOA_MENUBAR:
286  _glfwInitHints.ns.menubar = value;
287  return;
288  }
289 
291  "Invalid init hint 0x%08X", hint);
292 }
293 
294 GLFWAPI void glfwGetVersion(int* major, int* minor, int* rev)
295 {
296  if (major != NULL)
297  *major = GLFW_VERSION_MAJOR;
298  if (minor != NULL)
299  *minor = GLFW_VERSION_MINOR;
300  if (rev != NULL)
301  *rev = GLFW_VERSION_REVISION;
302 }
303 
304 GLFWAPI const char* glfwGetVersionString(void)
305 {
307 }
308 
309 GLFWAPI int glfwGetError(const char** description)
310 {
311  _GLFWerror* error;
312  int code = GLFW_NO_ERROR;
313 
314  if (description)
315  *description = NULL;
316 
317  if (_glfw.initialized)
318  error = _glfwPlatformGetTls(&_glfw.errorSlot);
319  else
320  error = &_glfwMainThreadError;
321 
322  if (error)
323  {
324  code = error->code;
325  error->code = GLFW_NO_ERROR;
326  if (description && code)
327  *description = error->description;
328  }
329 
330  return code;
331 }
332 
334 {
336  return cbfun;
337 }
338 
_GLFWtls errorSlot
Definition: internal.h:536
GLFWbool initialized
Definition: internal.h:515
char description[_GLFW_MESSAGE_SIZE]
Definition: internal.h:233
void _glfwPlatformDestroyTls(_GLFWtls *tls)
Definition: posix_thread.c:53
static GLFWerrorfun _glfwErrorCallback
Definition: init.c:50
#define GLFW_JOYSTICK_HAT_BUTTONS
Joystick hat buttons init hint.
Definition: glfw3.h:1067
float _glfw_fminf(float a, float b)
Definition: init.c:122
void _glfwPlatformDestroyMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:85
#define GLFWAPI
Definition: glfw3.h:240
_GLFWerror * next
Definition: internal.h:231
_GLFWtls contextSlot
Definition: internal.h:537
_GLFWinitconfig init
Definition: internal.h:518
int code
Definition: internal.h:232
const char * _glfwDefaultMappings[]
Definition: mappings.h:61
#define GLFW_COCOA_MENUBAR
macOS specific init hint.
Definition: glfw3.h:1077
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:333
#define GLFW_FALSE
Zero.
Definition: glfw3.h:287
#define GLFW_NO_WINDOW_CONTEXT
The specified window does not have an OpenGL or OpenGL ES context.
Definition: glfw3.h:753
char * _glfw_strdup(const char *source)
Definition: init.c:114
GLenum GLuint GLenum GLsizei length
Definition: gl.h:1033
_GLFWerror * errorListHead
Definition: internal.h:525
GLFWAPI int glfwGetError(const char **description)
Returns and clears the last error for the calling thread.
Definition: init.c:309
static _GLFWinitconfig _glfwInitHints
Definition: init.c:51
#define GLFW_API_UNAVAILABLE
GLFW could not find support for the requested API on the system.
Definition: glfw3.h:698
void _glfwTerminateVulkan(void)
Definition: vulkan.c:151
#define GLFW_FORMAT_UNAVAILABLE
The requested format is not supported or available.
Definition: glfw3.h:745
_GLFWwindow * windowListHead
Definition: internal.h:527
struct GLFWwindow GLFWwindow
Opaque window object.
Definition: glfw3.h:1137
#define _GLFW_SWAP_POINTERS(x, y)
Definition: internal.h:219
_GLFWcursor * cursorListHead
Definition: internal.h:526
#define GLFW_NO_CURRENT_CONTEXT
No context is current for this thread.
Definition: glfw3.h:655
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:222
int _glfwPlatformInit(void)
Definition: null_init.c:35
struct _GLFWlibrary::@6 hints
#define GLFW_PLATFORM_ERROR
A platform-specific error occurred that does not match any of the more specific categories.
Definition: glfw3.h:726
void _glfwFreeMonitor(_GLFWmonitor *monitor)
Definition: monitor.c:175
_GLFWlibrary _glfw
Definition: init.c:44
struct _GLFWinitconfig::@1 ns
int monitorCount
Definition: internal.h:530
GLFWAPI const char * glfwGetVersionString(void)
Returns a string describing the compile-time configuration.
Definition: init.c:304
GLFWAPI int glfwUpdateGamepadMappings(const char *string)
Adds the specified SDL_GameControllerDB gamepad mappings.
Definition: input.c:1077
void _glfwPlatformSetGammaRamp(_GLFWmonitor *monitor, const GLFWgammaramp *ramp)
Definition: null_monitor.c:66
#define GLFW_NO_ERROR
No error has occurred.
Definition: glfw3.h:636
GLFWAPI void glfwGetVersion(int *major, int *minor, int *rev)
Retrieves the version of the GLFW library.
Definition: init.c:294
GLFWAPI void glfwDefaultWindowHints(void)
Resets all window hints to their default values.
Definition: window.c:252
void _glfwPlatformLockMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:92
unsigned int size
Definition: glfw3.h:1572
void _glfwPlatformUnlockMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:98
void _glfwInputError(int code, const char *format,...)
Definition: init.c:153
GLFWbool _glfwPlatformCreateMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:72
const char * _glfwPlatformGetVersionString(void)
Definition: null_init.c:46
GLFWbool _glfwPlatformCreateTls(_GLFWtls *tls)
Definition: posix_thread.c:38
struct _GLFWlibrary::@7 timer
#define GLFW_VERSION_UNAVAILABLE
The requested OpenGL or OpenGL ES version is not available.
Definition: glfw3.h:715
uint64_t _glfwPlatformGetTimerValue(void)
Definition: cocoa_time.c:51
GLFWgammaramp originalRamp
Definition: internal.h:438
GLFWbool hatButtons
Definition: internal.h:242
#define GLFW_TRUE
One.
Definition: glfw3.h:279
#define GLFW_INVALID_ENUM
One of the arguments to the function was an invalid enum value.
Definition: glfw3.h:663
#define GLFW_INVALID_VALUE
One of the arguments to the function was an invalid value.
Definition: glfw3.h:674
void _glfwPlatformSetTls(_GLFWtls *tls, void *value)
Definition: posix_thread.c:66
#define GLFW_VERSION_REVISION
The revision number of the GLFW library.
Definition: glfw3.h:269
GLFWAPI void glfwDestroyCursor(GLFWcursor *cursor)
Destroys a cursor.
Definition: input.c:743
GLFWbool chdir
Definition: internal.h:245
struct _GLFWlibrary::@9 callbacks
void _glfwPlatformTerminate(void)
Definition: null_init.c:41
#define GLFW_OUT_OF_MEMORY
A memory allocation failed.
Definition: glfw3.h:682
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:444
uint64_t offset
Definition: internal.h:541
#define GLFW_VERSION_MINOR
The minor version number of the GLFW library.
Definition: glfw3.h:262
struct GLFWcursor GLFWcursor
Opaque cursor object.
Definition: glfw3.h:1149
float _glfw_fmaxf(float a, float b)
Definition: init.c:134
#define GLFW_NOT_INITIALIZED
GLFW has not been initialized.
Definition: glfw3.h:645
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:267
void * _glfwPlatformGetTls(_GLFWtls *tls)
Definition: posix_thread.c:60
static _GLFWerror _glfwMainThreadError
Definition: init.c:49
static void terminate(void)
Definition: init.c:62
#define GLFW_VERSION_MAJOR
The major version number of the GLFW library.
Definition: glfw3.h:255
_GLFWmonitor ** monitors
Definition: internal.h:529
#define _GLFW_MESSAGE_SIZE
Definition: internal.h:60
_GLFWmapping * mappings
Definition: internal.h:533
GLFWbool menubar
Definition: internal.h:244
error
Error code indicating why parse failed.
Definition: sajson.h:643
int mappingCount
Definition: internal.h:534
_GLFWmutex errorLock
Definition: internal.h:538
void(* GLFWerrorfun)(int, const char *)
The function signature for error callbacks.
Definition: glfw3.h:1165
GLFWAPI void glfwInitHint(int hint, int value)
Sets the specified init hint to the desired value.
Definition: init.c:275
#define GLFW_COCOA_CHDIR_RESOURCES
macOS specific init hint.
Definition: glfw3.h:1072


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21