glew_head.c 7.52 KB
Newer Older
1
#ifndef GLEW_INCLUDE
mem's avatar
mem committed
2
#include <GL/glew.h>
3
4
5
#else
#include GLEW_INCLUDE
#endif
6

Julian Squires's avatar
Julian Squires committed
7
8
9
#if defined(GLEW_OSMESA)
#  define GLAPI extern
#  include <GL/osmesa.h>
10
11
#elif defined(GLEW_EGL)
#  include <GL/eglew.h>
Julian Squires's avatar
Julian Squires committed
12
#elif defined(_WIN32)
13
14
15
16
17
18
19
20
/*
 * If NOGDI is defined, wingdi.h won't be included by windows.h, and thus
 * wglGetProcAddress won't be declared. It will instead be implicitly declared,
 * potentially incorrectly, which we don't want.
 */
#  if defined(NOGDI)
#    undef NOGDI
#  endif
ikits's avatar
ikits committed
21
#  include <GL/wglew.h>
22
#elif !defined(__ANDROID__) && !defined(__native_client__) && !defined(__HAIKU__) && (!defined(__APPLE__) || defined(GLEW_APPLE_GLX))
ikits's avatar
ikits committed
23
#  include <GL/glxew.h>
ikits's avatar
ikits committed
24
#endif
mem's avatar
mem committed
25

26
27
#include <stddef.h>  /* For size_t */

28
29
#if defined(GLEW_EGL)
#elif defined(GLEW_REGAL)
30
31
32
33
34

/* In GLEW_REGAL mode we call direcly into the linked
   libRegal.so glGetProcAddressREGAL for looking up
   the GL function pointers. */

35
36
37
38
39
40
41
42
43
#  undef glGetProcAddressREGAL
#  ifdef WIN32
extern void *  __stdcall glGetProcAddressREGAL(const GLchar *name);
static void * (__stdcall * regalGetProcAddress) (const GLchar *) = glGetProcAddressREGAL;
#    else
extern void * glGetProcAddressREGAL(const GLchar *name);
static void * (*regalGetProcAddress) (const GLchar *) = glGetProcAddressREGAL;
#  endif
#  define glGetProcAddressREGAL GLEW_GET_FUN(__glewGetProcAddressREGAL)
44

45
#elif defined(__sgi) || defined (__sun) || defined(__HAIKU__) || defined(GLEW_APPLE_GLX)
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

void* dlGetProcAddress (const GLubyte* name)
{
  static void* h = NULL;
  static void* gpa;

  if (h == NULL)
  {
    if ((h = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL)) == NULL) return NULL;
    gpa = dlsym(h, "glXGetProcAddress");
  }

  if (gpa != NULL)
    return ((void*(*)(const GLubyte*))gpa)(name);
  else
    return dlsym(h, (const char*)name);
}
#endif /* __sgi || __sun || GLEW_APPLE_GLX */

ikits's avatar
ikits committed
68
#if defined(__APPLE__)
ikits's avatar
ikits committed
69
70
#include <stdlib.h>
#include <string.h>
71
72
73
74
75
76
77
78
79
#include <AvailabilityMacros.h>

#ifdef MAC_OS_X_VERSION_10_3

#include <dlfcn.h>

void* NSGLGetProcAddress (const GLubyte *name)
{
  static void* image = NULL;
80
  void* addr;
81
  if (NULL == image)
82
83
84
  {
    image = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
  }
85
  if( !image ) return NULL;
86
  addr = dlsym(image, (const char*)name);
87
88
89
90
91
92
  if( addr ) return addr;
#ifdef GLEW_APPLE_GLX
  return dlGetProcAddress( name ); // try next for glx symbols
#else
  return NULL;
#endif
93
94
95
96
}
#else

#include <mach-o/dyld.h>
ikits's avatar
ikits committed
97

98
void* NSGLGetProcAddress (const GLubyte *name)
ikits's avatar
ikits committed
99
{
ikits's avatar
ikits committed
100
  static const struct mach_header* image = NULL;
ikits's avatar
ikits committed
101
  NSSymbol symbol;
102
  char* symbolName;
ikits's avatar
ikits committed
103
104
105
106
  if (NULL == image)
  {
    image = NSAddImage("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", NSADDIMAGE_OPTION_RETURN_ON_ERROR);
  }
ikits's avatar
ikits committed
107
  /* prepend a '_' for the Unix C symbol mangling convention */
108
109
  symbolName = malloc(strlen((const char*)name) + 2);
  strcpy(symbolName+1, (const char*)name);
ikits's avatar
ikits committed
110
111
  symbolName[0] = '_';
  symbol = NULL;
ikits's avatar
ikits committed
112
113
114
  /* if (NSIsSymbolNameDefined(symbolName))
	 symbol = NSLookupAndBindSymbol(symbolName); */
  symbol = image ? NSLookupSymbolInImage(image, symbolName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR) : NULL;
ikits's avatar
ikits committed
115
  free(symbolName);
116
117
118
119
120
121
  if( symbol ) return NSAddressOfSymbol(symbol);
#ifdef GLEW_APPLE_GLX
  return dlGetProcAddress( name ); // try next for glx symbols
#else
  return NULL;
#endif
ikits's avatar
ikits committed
122
}
123
#endif /* MAC_OS_X_VERSION_10_3 */
ikits's avatar
ikits committed
124
#endif /* __APPLE__ */
125

126
127
128
/*
 * Define glewGetProcAddress.
 */
129
130
131
#if defined(GLEW_REGAL)
#  define glewGetProcAddress(name) regalGetProcAddress((const GLchar *)name)
#elif defined(GLEW_OSMESA)
Julian Squires's avatar
Julian Squires committed
132
#  define glewGetProcAddress(name) OSMesaGetProcAddress((const char *)name)
133
134
#elif defined(GLEW_EGL)
#  define glewGetProcAddress(name) eglGetProcAddress((const char *)name)
135
#elif defined(_WIN32)
136
#  define glewGetProcAddress(name) wglGetProcAddress((LPCSTR)name)
137
138
#elif defined(__APPLE__) && !defined(GLEW_APPLE_GLX)
#  define glewGetProcAddress(name) NSGLGetProcAddress(name)
139
#elif defined(__sgi) || defined(__sun) || defined(__HAIKU__)
140
141
142
#  define glewGetProcAddress(name) dlGetProcAddress(name)
#elif defined(__ANDROID__)
#  define glewGetProcAddress(name) NULL /* TODO */
143
144
#elif defined(__native_client__)
#  define glewGetProcAddress(name) NULL /* TODO */
145
146
#else /* __linux */
#  define glewGetProcAddress(name) (*glXGetProcAddressARB)(name)
147
148
#endif

ikits's avatar
ikits committed
149
/*
150
 * Redefine GLEW_GET_VAR etc without const cast
ikits's avatar
ikits committed
151
 */
152
153
154
155
156
157

#undef GLEW_GET_VAR
# define GLEW_GET_VAR(x) (x)

#ifdef WGLEW_GET_VAR
# undef WGLEW_GET_VAR
158
# define WGLEW_GET_VAR(x) (x)
159
160
161
162
#endif /* WGLEW_GET_VAR */

#ifdef GLXEW_GET_VAR
# undef GLXEW_GET_VAR
163
# define GLXEW_GET_VAR(x) (x)
164
#endif /* GLXEW_GET_VAR */
ikits's avatar
ikits committed
165

166
167
168
169
170
#ifdef EGLEW_GET_VAR
# undef EGLEW_GET_VAR
# define EGLEW_GET_VAR(x) (x)
#endif /* EGLEW_GET_VAR */

171
172
173
174
/*
 * GLEW, just like OpenGL or GLU, does not rely on the standard C library.
 * These functions implement the functionality required in this file.
 */
175

ikits's avatar
ikits committed
176
static GLuint _glewStrLen (const GLubyte* s)
177
178
{
  GLuint i=0;
ikits's avatar
ikits committed
179
180
  if (s == NULL) return 0;
  while (s[i] != '\0') i++;
181
182
  return i;
}
mem's avatar
mem committed
183

ikits's avatar
ikits committed
184
static GLuint _glewStrCLen (const GLubyte* s, GLubyte c)
185
186
{
  GLuint i=0;
ikits's avatar
ikits committed
187
188
  if (s == NULL) return 0;
  while (s[i] != '\0' && s[i] != c) i++;
189
  return i;
190
}
191

192
static GLuint _glewStrCopy(char *d, const char *s, char c)
193
{
194
195
196
197
198
  GLuint i=0;
  if (s == NULL) return 0;
  while (s[i] != '\0' && s[i] != c) { d[i] = s[i]; i++; }
  d[i] = '\0';
  return i;
199
200
}

201
#if !defined(GLEW_OSMESA)
202
#if !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
ikits's avatar
ikits committed
203
static GLboolean _glewStrSame (const GLubyte* a, const GLubyte* b, GLuint n)
204
205
{
  GLuint i=0;
ikits's avatar
ikits committed
206
207
208
  if(a == NULL || b == NULL)
    return (a == NULL && b == NULL && n == 0) ? GL_TRUE : GL_FALSE;
  while (i < n && a[i] != '\0' && b[i] != '\0' && a[i] == b[i]) i++;
209
210
  return i == n ? GL_TRUE : GL_FALSE;
}
211
#endif
212
#endif
213

214
static GLboolean _glewStrSame1 (const GLubyte** a, GLuint* na, const GLubyte* b, GLuint nb)
ikits's avatar
ikits committed
215
216
217
{
  while (*na > 0 && (**a == ' ' || **a == '\n' || **a == '\r' || **a == '\t'))
  {
ikits's avatar
ikits committed
218
219
    (*a)++;
    (*na)--;
ikits's avatar
ikits committed
220
221
222
223
  }
  if(*na >= nb)
  {
    GLuint i=0;
ikits's avatar
ikits committed
224
    while (i < nb && (*a)+i != NULL && b+i != NULL && (*a)[i] == b[i]) i++;
nigels's avatar
nigels committed
225
226
227
228
229
230
    if(i == nb)
    {
      *a = *a + nb;
      *na = *na - nb;
      return GL_TRUE;
    }
ikits's avatar
ikits committed
231
232
233
234
  }
  return GL_FALSE;
}

235
static GLboolean _glewStrSame2 (const GLubyte** a, GLuint* na, const GLubyte* b, GLuint nb)
236
237
238
239
{
  if(*na >= nb)
  {
    GLuint i=0;
ikits's avatar
ikits committed
240
    while (i < nb && (*a)+i != NULL && b+i != NULL && (*a)[i] == b[i]) i++;
nigels's avatar
nigels committed
241
242
243
244
245
246
    if(i == nb)
    {
      *a = *a + nb;
      *na = *na - nb;
      return GL_TRUE;
    }
247
248
249
250
  }
  return GL_FALSE;
}

251
static GLboolean _glewStrSame3 (const GLubyte** a, GLuint* na, const GLubyte* b, GLuint nb)
252
{
ikits's avatar
ikits committed
253
  if(*na >= nb)
254
255
  {
    GLuint i=0;
ikits's avatar
ikits committed
256
257
    while (i < nb && (*a)+i != NULL && b+i != NULL && (*a)[i] == b[i]) i++;
    if (i == nb && (*na == nb || (*a)[i] == ' ' || (*a)[i] == '\n' || (*a)[i] == '\r' || (*a)[i] == '\t'))
ikits's avatar
ikits committed
258
259
260
261
262
    {
      *a = *a + nb;
      *na = *na - nb;
      return GL_TRUE;
    }
263
264
265
  }
  return GL_FALSE;
}
266
267
268
269
270
271
272

/*
 * Search for name in the extensions string. Use of strstr()
 * is not sufficient because extension names can be prefixes of
 * other extension names. Could use strtok() but the constant
 * string returned by glGetString might be in read-only memory.
 */
273
#if !defined(GLEW_OSMESA)
274
#if !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
275
276
277
278
279
280
281
282
283
284
285
286
287
static GLboolean _glewSearchExtension (const char* name, const GLubyte *start, const GLubyte *end)
{
  const GLubyte* p;
  GLuint len = _glewStrLen((const GLubyte*)name);
  p = start;
  while (p < end)
  {
    GLuint n = _glewStrCLen(p, ' ');
    if (len == n && _glewStrSame((const GLubyte*)name, p, n)) return GL_TRUE;
    p += n+1;
  }
  return GL_FALSE;
}
288
#endif
289
#endif