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);
84  _glfw.monitorCount = 0;
85 
86  free(_glfw.mappings);
88  _glfw.mappingCount = 0;
89 
92 
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;
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  {
197  if (!error)
198  {
199  error = calloc(1, sizeof(_GLFWerror));
202  error->next = _glfw.errorListHead;
205  }
206  }
207  else
209 
210  error->code = code;
211  strcpy(error->description, description);
212 
213  if (_glfwErrorCallback)
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));
229 
230  if (!_glfwPlatformInit())
231  {
232  terminate();
233  return GLFW_FALSE;
234  }
235 
239  {
240  terminate();
241  return GLFW_FALSE;
242  }
243 
245 
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)
319  else
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 
_GLFWlibrary::timer
struct _GLFWlibrary::@7 timer
_glfw_fmaxf
float _glfw_fmaxf(float a, float b)
Definition: init.c:134
_GLFWlibrary::initialized
GLFWbool initialized
Definition: internal.h:515
_glfwDefaultMappings
const char * _glfwDefaultMappings[]
Definition: mappings.h:61
_GLFWlibrary::callbacks
struct _GLFWlibrary::@9 callbacks
NULL
#define NULL
glfwTerminate
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:267
_GLFWinitconfig
Definition: internal.h:240
_glfwMainThreadError
static _GLFWerror _glfwMainThreadError
Definition: init.c:49
_glfwPlatformTerminate
void _glfwPlatformTerminate(void)
Definition: null_init.c:41
glfwGetVersionString
const GLFWAPI char * glfwGetVersionString(void)
Returns a string describing the compile-time configuration.
Definition: init.c:304
GLFWerrorfun
void(* GLFWerrorfun)(int, const char *)
The function signature for error callbacks.
Definition: glfw3.h:1165
GLFW_INVALID_VALUE
#define GLFW_INVALID_VALUE
One of the arguments to the function was an invalid value.
Definition: glfw3.h:674
description
description
GLFW_FORMAT_UNAVAILABLE
#define GLFW_FORMAT_UNAVAILABLE
The requested format is not supported or available.
Definition: glfw3.h:745
GLFWwindow
struct GLFWwindow GLFWwindow
Opaque window object.
Definition: glfw3.h:1137
GLFWAPI
#define GLFWAPI
Definition: glfw3.h:240
_glfwPlatformLockMutex
void _glfwPlatformLockMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:92
_GLFWlibrary::hints
struct _GLFWlibrary::@6 hints
_glfwPlatformSetTls
void _glfwPlatformSetTls(_GLFWtls *tls, void *value)
Definition: posix_thread.c:66
sajson::error
error
Error code indicating why parse failed.
Definition: sajson.h:643
_glfwInputError
void _glfwInputError(int code, const char *format,...)
Definition: init.c:153
glfwSetErrorCallback
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:333
GLFW_VERSION_UNAVAILABLE
#define GLFW_VERSION_UNAVAILABLE
The requested OpenGL or OpenGL ES version is not available.
Definition: glfw3.h:715
GLFW_NO_WINDOW_CONTEXT
#define GLFW_NO_WINDOW_CONTEXT
The specified window does not have an OpenGL or OpenGL ES context.
Definition: glfw3.h:753
_GLFWlibrary::errorLock
_GLFWmutex errorLock
Definition: internal.h:538
_glfwFreeMonitor
void _glfwFreeMonitor(_GLFWmonitor *monitor)
Definition: monitor.c:175
glfwDefaultWindowHints
GLFWAPI void glfwDefaultWindowHints(void)
Resets all window hints to their default values.
Definition: window.c:252
_glfwPlatformSetGammaRamp
void _glfwPlatformSetGammaRamp(_GLFWmonitor *monitor, const GLFWgammaramp *ramp)
Definition: null_monitor.c:66
GLFW_API_UNAVAILABLE
#define GLFW_API_UNAVAILABLE
GLFW could not find support for the requested API on the system.
Definition: glfw3.h:698
_glfwPlatformDestroyMutex
void _glfwPlatformDestroyMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:85
_glfwErrorCallback
static GLFWerrorfun _glfwErrorCallback
Definition: init.c:50
GLFW_FALSE
#define GLFW_FALSE
Zero.
Definition: glfw3.h:287
GLFW_OUT_OF_MEMORY
#define GLFW_OUT_OF_MEMORY
A memory allocation failed.
Definition: glfw3.h:682
length
GLenum GLuint GLenum GLsizei length
Definition: gl.h:1033
GLFW_COCOA_CHDIR_RESOURCES
#define GLFW_COCOA_CHDIR_RESOURCES
macOS specific init hint.
Definition: glfw3.h:1072
_GLFWlibrary
Definition: internal.h:513
GLFW_TRUE
#define GLFW_TRUE
One.
Definition: glfw3.h:279
_glfwPlatformGetTls
void * _glfwPlatformGetTls(_GLFWtls *tls)
Definition: posix_thread.c:60
_GLFWlibrary::mappingCount
int mappingCount
Definition: internal.h:534
_GLFWlibrary::mappings
_GLFWmapping * mappings
Definition: internal.h:533
glfwUpdateGamepadMappings
GLFWAPI int glfwUpdateGamepadMappings(const char *string)
Adds the specified SDL_GameControllerDB gamepad mappings.
Definition: input.c:1077
GLFW_COCOA_MENUBAR
#define GLFW_COCOA_MENUBAR
macOS specific init hint.
Definition: glfw3.h:1077
_GLFWlibrary::init
_GLFWinitconfig init
Definition: internal.h:518
GLFWgammaramp::size
unsigned int size
Definition: glfw3.h:1572
glfwInit
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:222
terminate
static void terminate(void)
Definition: init.c:62
_GLFWinitconfig::hatButtons
GLFWbool hatButtons
Definition: internal.h:242
GLFW_VERSION_MINOR
#define GLFW_VERSION_MINOR
The minor version number of the GLFW library.
Definition: glfw3.h:262
_glfw_strdup
char * _glfw_strdup(const char *source)
Definition: init.c:114
GLFW_VERSION_REVISION
#define GLFW_VERSION_REVISION
The revision number of the GLFW library.
Definition: glfw3.h:269
_GLFWlibrary::contextSlot
_GLFWtls contextSlot
Definition: internal.h:537
_GLFWerror
Definition: internal.h:229
glfwDestroyWindow
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:444
GLFW_NOT_INITIALIZED
#define GLFW_NOT_INITIALIZED
GLFW has not been initialized.
Definition: glfw3.h:645
_GLFWinitconfig::chdir
GLFWbool chdir
Definition: internal.h:245
_glfwInitHints
static _GLFWinitconfig _glfwInitHints
Definition: init.c:51
GLFW_JOYSTICK_HAT_BUTTONS
#define GLFW_JOYSTICK_HAT_BUTTONS
Joystick hat buttons init hint.
Definition: glfw3.h:1067
_GLFW_SWAP_POINTERS
#define _GLFW_SWAP_POINTERS(x, y)
Definition: internal.h:219
internal.h
_GLFWlibrary::monitors
_GLFWmonitor ** monitors
Definition: internal.h:529
_glfw
_GLFWlibrary _glfw
Definition: init.c:44
_GLFWlibrary::errorListHead
_GLFWerror * errorListHead
Definition: internal.h:525
_glfwPlatformCreateMutex
GLFWbool _glfwPlatformCreateMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:72
_GLFWlibrary::offset
uint64_t offset
Definition: internal.h:541
_glfwPlatformInit
int _glfwPlatformInit(void)
Definition: null_init.c:35
mappings.h
GLFWcursor
struct GLFWcursor GLFWcursor
Opaque cursor object.
Definition: glfw3.h:1149
GLFW_PLATFORM_ERROR
#define GLFW_PLATFORM_ERROR
A platform-specific error occurred that does not match any of the more specific categories.
Definition: glfw3.h:726
_GLFWlibrary::monitorCount
int monitorCount
Definition: internal.h:530
GLFW_VERSION_MAJOR
#define GLFW_VERSION_MAJOR
The major version number of the GLFW library.
Definition: glfw3.h:255
_glfw_fminf
float _glfw_fminf(float a, float b)
Definition: init.c:122
_GLFWinitconfig::menubar
GLFWbool menubar
Definition: internal.h:244
GLFW_INVALID_ENUM
#define GLFW_INVALID_ENUM
One of the arguments to the function was an invalid enum value.
Definition: glfw3.h:663
_glfwPlatformGetVersionString
const char * _glfwPlatformGetVersionString(void)
Definition: null_init.c:46
_glfwPlatformDestroyTls
void _glfwPlatformDestroyTls(_GLFWtls *tls)
Definition: posix_thread.c:53
_glfwPlatformCreateTls
GLFWbool _glfwPlatformCreateTls(_GLFWtls *tls)
Definition: posix_thread.c:38
glfwInitHint
GLFWAPI void glfwInitHint(int hint, int value)
Sets the specified init hint to the desired value.
Definition: init.c:275
_glfwPlatformGetTimerValue
uint64_t _glfwPlatformGetTimerValue(void)
Definition: cocoa_time.c:51
_GLFWlibrary::windowListHead
_GLFWwindow * windowListHead
Definition: internal.h:527
_GLFWlibrary::cursorListHead
_GLFWcursor * cursorListHead
Definition: internal.h:526
glfwGetVersion
GLFWAPI void glfwGetVersion(int *major, int *minor, int *rev)
Retrieves the version of the GLFW library.
Definition: init.c:294
_GLFWmonitor::originalRamp
GLFWgammaramp originalRamp
Definition: internal.h:438
_GLFWmonitor
Definition: internal.h:423
GLFW_NO_CURRENT_CONTEXT
#define GLFW_NO_CURRENT_CONTEXT
No context is current for this thread.
Definition: glfw3.h:655
_GLFWlibrary::errorSlot
_GLFWtls errorSlot
Definition: internal.h:536
assert.h
_glfwPlatformUnlockMutex
void _glfwPlatformUnlockMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:98
_GLFWinitconfig::ns
struct _GLFWinitconfig::@1 ns
glfwDestroyCursor
GLFWAPI void glfwDestroyCursor(GLFWcursor *cursor)
Destroys a cursor.
Definition: input.c:743
_glfwTerminateVulkan
void _glfwTerminateVulkan(void)
Definition: vulkan.c:151
_GLFW_MESSAGE_SIZE
#define _GLFW_MESSAGE_SIZE
Definition: internal.h:60
GLFW_NO_ERROR
#define GLFW_NO_ERROR
No error has occurred.
Definition: glfw3.h:636
glfwGetError
GLFWAPI int glfwGetError(const char **description)
Returns and clears the last error for the calling thread.
Definition: init.c:309


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:08