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 
126 
127 // Notifies shared code of an error
128 //
129 void _glfwInputError(int code, const char* format, ...)
130 {
131  _GLFWerror* error;
132  char description[_GLFW_MESSAGE_SIZE];
133 
134  if (format)
135  {
136  va_list vl;
137 
138  va_start(vl, format);
139  vsnprintf(description, sizeof(description), format, vl);
140  va_end(vl);
141 
142  description[sizeof(description) - 1] = '\0';
143  }
144  else
145  {
146  if (code == GLFW_NOT_INITIALIZED)
147  strcpy(description, "The GLFW library is not initialized");
148  else if (code == GLFW_NO_CURRENT_CONTEXT)
149  strcpy(description, "There is no current context");
150  else if (code == GLFW_INVALID_ENUM)
151  strcpy(description, "Invalid argument for enum parameter");
152  else if (code == GLFW_INVALID_VALUE)
153  strcpy(description, "Invalid value for parameter");
154  else if (code == GLFW_OUT_OF_MEMORY)
155  strcpy(description, "Out of memory");
156  else if (code == GLFW_API_UNAVAILABLE)
157  strcpy(description, "The requested API is unavailable");
158  else if (code == GLFW_VERSION_UNAVAILABLE)
159  strcpy(description, "The requested API version is unavailable");
160  else if (code == GLFW_PLATFORM_ERROR)
161  strcpy(description, "A platform-specific error occurred");
162  else if (code == GLFW_FORMAT_UNAVAILABLE)
163  strcpy(description, "The requested format is unavailable");
164  else if (code == GLFW_NO_WINDOW_CONTEXT)
165  strcpy(description, "The specified window has no context");
166  else
167  strcpy(description, "ERROR: UNKNOWN GLFW ERROR");
168  }
169 
170  if (_glfw.initialized)
171  {
172  error = _glfwPlatformGetTls(&_glfw.errorSlot);
173  if (!error)
174  {
175  error = calloc(1, sizeof(_GLFWerror));
176  _glfwPlatformSetTls(&_glfw.errorSlot, error);
178  error->next = _glfw.errorListHead;
179  _glfw.errorListHead = error;
181  }
182  }
183  else
184  error = &_glfwMainThreadError;
185 
186  error->code = code;
187  strcpy(error->description, description);
188 
189  if (_glfwErrorCallback)
190  _glfwErrorCallback(code, description);
191 }
192 
193 
197 
198 GLFWAPI int glfwInit(void)
199 {
200  if (_glfw.initialized)
201  return GLFW_TRUE;
202 
203  memset(&_glfw, 0, sizeof(_glfw));
204  _glfw.hints.init = _glfwInitHints;
205 
206  if (!_glfwPlatformInit())
207  {
208  terminate();
209  return GLFW_FALSE;
210  }
211 
212  if (!_glfwPlatformCreateMutex(&_glfw.errorLock) ||
215  {
216  terminate();
217  return GLFW_FALSE;
218  }
219 
220  _glfwPlatformSetTls(&_glfw.errorSlot, &_glfwMainThreadError);
221 
222  _glfw.initialized = GLFW_TRUE;
224 
226 
227  {
228  int i;
229 
230  for (i = 0; _glfwDefaultMappings[i]; i++)
231  {
233  {
234  terminate();
235  return GLFW_FALSE;
236  }
237  }
238  }
239 
240  return GLFW_TRUE;
241 }
242 
244 {
245  if (!_glfw.initialized)
246  return;
247 
248  terminate();
249 }
250 
251 GLFWAPI void glfwInitHint(int hint, int value)
252 {
253  switch (hint)
254  {
256  _glfwInitHints.hatButtons = value;
257  return;
259  _glfwInitHints.ns.chdir = value;
260  return;
261  case GLFW_COCOA_MENUBAR:
262  _glfwInitHints.ns.menubar = value;
263  return;
264  }
265 
267  "Invalid init hint 0x%08X", hint);
268 }
269 
270 GLFWAPI void glfwGetVersion(int* major, int* minor, int* rev)
271 {
272  if (major != NULL)
273  *major = GLFW_VERSION_MAJOR;
274  if (minor != NULL)
275  *minor = GLFW_VERSION_MINOR;
276  if (rev != NULL)
277  *rev = GLFW_VERSION_REVISION;
278 }
279 
280 GLFWAPI const char* glfwGetVersionString(void)
281 {
283 }
284 
285 GLFWAPI int glfwGetError(const char** description)
286 {
287  _GLFWerror* error;
288  int code = GLFW_NO_ERROR;
289 
290  if (description)
291  *description = NULL;
292 
293  if (_glfw.initialized)
294  error = _glfwPlatformGetTls(&_glfw.errorSlot);
295  else
296  error = &_glfwMainThreadError;
297 
298  if (error)
299  {
300  code = error->code;
301  error->code = GLFW_NO_ERROR;
302  if (description && code)
303  *description = error->description;
304  }
305 
306  return code;
307 }
308 
310 {
312  return cbfun;
313 }
314 
_GLFWtls errorSlot
Definition: internal.h:535
GLFWbool initialized
Definition: internal.h:514
char description[_GLFW_MESSAGE_SIZE]
Definition: internal.h:232
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
void _glfwPlatformDestroyMutex(_GLFWmutex *mutex)
Definition: posix_thread.c:85
#define GLFWAPI
Definition: glfw3.h:240
_GLFWerror * next
Definition: internal.h:230
_GLFWtls contextSlot
Definition: internal.h:536
_GLFWinitconfig init
Definition: internal.h:517
GLfloat value
int code
Definition: internal.h:231
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:309
#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
_GLFWerror * errorListHead
Definition: internal.h:524
GLFWAPI int glfwGetError(const char **description)
Returns and clears the last error for the calling thread.
Definition: init.c:285
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: src/vulkan.c:151
#define GLFW_FORMAT_UNAVAILABLE
The requested format is not supported or available.
Definition: glfw3.h:745
_GLFWwindow * windowListHead
Definition: internal.h:526
#define _GLFW_SWAP_POINTERS(x, y)
Definition: internal.h:218
_GLFWcursor * cursorListHead
Definition: internal.h:525
#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:198
int _glfwPlatformInit(void)
Definition: null_init.c:35
#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 _GLFWlibrary::@27 hints
int monitorCount
Definition: internal.h:529
GLFWAPI const char * glfwGetVersionString(void)
Returns a string describing the compile-time configuration.
Definition: init.c:280
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:65
#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:270
GLFWAPI void glfwDefaultWindowHints(void)
Resets all window hints to their default values.
Definition: window.c:252
GLint GLint GLsizei GLint GLenum format
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:129
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
#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:437
GLFWbool hatButtons
Definition: internal.h:241
#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:244
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:540
#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
#define GLFW_NOT_INITIALIZED
GLFW has not been initialized.
Definition: glfw3.h:645
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
struct _GLFWlibrary::@28 timer
void * _glfwPlatformGetTls(_GLFWtls *tls)
Definition: posix_thread.c:60
GLsizei GLsizei GLchar * source
struct _GLFWinitconfig::@22 ns
static _GLFWerror _glfwMainThreadError
Definition: init.c:49
static void terminate(void)
Definition: init.c:62
struct _GLFWlibrary::@30 callbacks
#define GLFW_VERSION_MAJOR
The major version number of the GLFW library.
Definition: glfw3.h:255
#define NULL
Definition: tinycthread.c:47
int i
_GLFWmonitor ** monitors
Definition: internal.h:528
GLenum GLuint GLenum GLsizei length
#define _GLFW_MESSAGE_SIZE
Definition: internal.h:59
_GLFWmapping * mappings
Definition: internal.h:532
GLFWbool menubar
Definition: internal.h:243
GLuint64EXT * result
Definition: glext.h:10921
int mappingCount
Definition: internal.h:533
struct GLFWwindow GLFWwindow
_GLFWmutex errorLock
Definition: internal.h:537
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:251
#define GLFW_COCOA_CHDIR_RESOURCES
macOS specific init hint.
Definition: glfw3.h:1072


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:17