glx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * SPDX-License-Identifier: (WTFPL OR CC0-1.0) AND Apache-2.0
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <glad/glx.h>
  8. #ifndef GLAD_IMPL_UTIL_C_
  9. #define GLAD_IMPL_UTIL_C_
  10. #ifdef _MSC_VER
  11. #define GLAD_IMPL_UTIL_SSCANF sscanf_s
  12. #else
  13. #define GLAD_IMPL_UTIL_SSCANF sscanf
  14. #endif
  15. #endif /* GLAD_IMPL_UTIL_C_ */
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. int GLAD_GLX_VERSION_1_0 = 0;
  20. int GLAD_GLX_VERSION_1_1 = 0;
  21. int GLAD_GLX_VERSION_1_2 = 0;
  22. int GLAD_GLX_VERSION_1_3 = 0;
  23. int GLAD_GLX_VERSION_1_4 = 0;
  24. int GLAD_GLX_ARB_create_context = 0;
  25. int GLAD_GLX_ARB_create_context_profile = 0;
  26. int GLAD_GLX_ARB_get_proc_address = 0;
  27. int GLAD_GLX_EXT_swap_control = 0;
  28. int GLAD_GLX_MESA_swap_control = 0;
  29. int GLAD_GLX_SGI_swap_control = 0;
  30. PFNGLXCHOOSEFBCONFIGPROC glad_glXChooseFBConfig = NULL;
  31. PFNGLXCHOOSEVISUALPROC glad_glXChooseVisual = NULL;
  32. PFNGLXCOPYCONTEXTPROC glad_glXCopyContext = NULL;
  33. PFNGLXCREATECONTEXTPROC glad_glXCreateContext = NULL;
  34. PFNGLXCREATECONTEXTATTRIBSARBPROC glad_glXCreateContextAttribsARB = NULL;
  35. PFNGLXCREATEGLXPIXMAPPROC glad_glXCreateGLXPixmap = NULL;
  36. PFNGLXCREATENEWCONTEXTPROC glad_glXCreateNewContext = NULL;
  37. PFNGLXCREATEPBUFFERPROC glad_glXCreatePbuffer = NULL;
  38. PFNGLXCREATEPIXMAPPROC glad_glXCreatePixmap = NULL;
  39. PFNGLXCREATEWINDOWPROC glad_glXCreateWindow = NULL;
  40. PFNGLXDESTROYCONTEXTPROC glad_glXDestroyContext = NULL;
  41. PFNGLXDESTROYGLXPIXMAPPROC glad_glXDestroyGLXPixmap = NULL;
  42. PFNGLXDESTROYPBUFFERPROC glad_glXDestroyPbuffer = NULL;
  43. PFNGLXDESTROYPIXMAPPROC glad_glXDestroyPixmap = NULL;
  44. PFNGLXDESTROYWINDOWPROC glad_glXDestroyWindow = NULL;
  45. PFNGLXGETCLIENTSTRINGPROC glad_glXGetClientString = NULL;
  46. PFNGLXGETCONFIGPROC glad_glXGetConfig = NULL;
  47. PFNGLXGETCURRENTCONTEXTPROC glad_glXGetCurrentContext = NULL;
  48. PFNGLXGETCURRENTDISPLAYPROC glad_glXGetCurrentDisplay = NULL;
  49. PFNGLXGETCURRENTDRAWABLEPROC glad_glXGetCurrentDrawable = NULL;
  50. PFNGLXGETCURRENTREADDRAWABLEPROC glad_glXGetCurrentReadDrawable = NULL;
  51. PFNGLXGETFBCONFIGATTRIBPROC glad_glXGetFBConfigAttrib = NULL;
  52. PFNGLXGETFBCONFIGSPROC glad_glXGetFBConfigs = NULL;
  53. PFNGLXGETPROCADDRESSPROC glad_glXGetProcAddress = NULL;
  54. PFNGLXGETPROCADDRESSARBPROC glad_glXGetProcAddressARB = NULL;
  55. PFNGLXGETSELECTEDEVENTPROC glad_glXGetSelectedEvent = NULL;
  56. PFNGLXGETSWAPINTERVALMESAPROC glad_glXGetSwapIntervalMESA = NULL;
  57. PFNGLXGETVISUALFROMFBCONFIGPROC glad_glXGetVisualFromFBConfig = NULL;
  58. PFNGLXISDIRECTPROC glad_glXIsDirect = NULL;
  59. PFNGLXMAKECONTEXTCURRENTPROC glad_glXMakeContextCurrent = NULL;
  60. PFNGLXMAKECURRENTPROC glad_glXMakeCurrent = NULL;
  61. PFNGLXQUERYCONTEXTPROC glad_glXQueryContext = NULL;
  62. PFNGLXQUERYDRAWABLEPROC glad_glXQueryDrawable = NULL;
  63. PFNGLXQUERYEXTENSIONPROC glad_glXQueryExtension = NULL;
  64. PFNGLXQUERYEXTENSIONSSTRINGPROC glad_glXQueryExtensionsString = NULL;
  65. PFNGLXQUERYSERVERSTRINGPROC glad_glXQueryServerString = NULL;
  66. PFNGLXQUERYVERSIONPROC glad_glXQueryVersion = NULL;
  67. PFNGLXSELECTEVENTPROC glad_glXSelectEvent = NULL;
  68. PFNGLXSWAPBUFFERSPROC glad_glXSwapBuffers = NULL;
  69. PFNGLXSWAPINTERVALEXTPROC glad_glXSwapIntervalEXT = NULL;
  70. PFNGLXSWAPINTERVALMESAPROC glad_glXSwapIntervalMESA = NULL;
  71. PFNGLXSWAPINTERVALSGIPROC glad_glXSwapIntervalSGI = NULL;
  72. PFNGLXUSEXFONTPROC glad_glXUseXFont = NULL;
  73. PFNGLXWAITGLPROC glad_glXWaitGL = NULL;
  74. PFNGLXWAITXPROC glad_glXWaitX = NULL;
  75. static void glad_glx_load_GLX_VERSION_1_0( GLADuserptrloadfunc load, void* userptr) {
  76. if(!GLAD_GLX_VERSION_1_0) return;
  77. glad_glXChooseVisual = (PFNGLXCHOOSEVISUALPROC) load(userptr, "glXChooseVisual");
  78. glad_glXCopyContext = (PFNGLXCOPYCONTEXTPROC) load(userptr, "glXCopyContext");
  79. glad_glXCreateContext = (PFNGLXCREATECONTEXTPROC) load(userptr, "glXCreateContext");
  80. glad_glXCreateGLXPixmap = (PFNGLXCREATEGLXPIXMAPPROC) load(userptr, "glXCreateGLXPixmap");
  81. glad_glXDestroyContext = (PFNGLXDESTROYCONTEXTPROC) load(userptr, "glXDestroyContext");
  82. glad_glXDestroyGLXPixmap = (PFNGLXDESTROYGLXPIXMAPPROC) load(userptr, "glXDestroyGLXPixmap");
  83. glad_glXGetConfig = (PFNGLXGETCONFIGPROC) load(userptr, "glXGetConfig");
  84. glad_glXGetCurrentContext = (PFNGLXGETCURRENTCONTEXTPROC) load(userptr, "glXGetCurrentContext");
  85. glad_glXGetCurrentDrawable = (PFNGLXGETCURRENTDRAWABLEPROC) load(userptr, "glXGetCurrentDrawable");
  86. glad_glXIsDirect = (PFNGLXISDIRECTPROC) load(userptr, "glXIsDirect");
  87. glad_glXMakeCurrent = (PFNGLXMAKECURRENTPROC) load(userptr, "glXMakeCurrent");
  88. glad_glXQueryExtension = (PFNGLXQUERYEXTENSIONPROC) load(userptr, "glXQueryExtension");
  89. glad_glXQueryVersion = (PFNGLXQUERYVERSIONPROC) load(userptr, "glXQueryVersion");
  90. glad_glXSwapBuffers = (PFNGLXSWAPBUFFERSPROC) load(userptr, "glXSwapBuffers");
  91. glad_glXUseXFont = (PFNGLXUSEXFONTPROC) load(userptr, "glXUseXFont");
  92. glad_glXWaitGL = (PFNGLXWAITGLPROC) load(userptr, "glXWaitGL");
  93. glad_glXWaitX = (PFNGLXWAITXPROC) load(userptr, "glXWaitX");
  94. }
  95. static void glad_glx_load_GLX_VERSION_1_1( GLADuserptrloadfunc load, void* userptr) {
  96. if(!GLAD_GLX_VERSION_1_1) return;
  97. glad_glXGetClientString = (PFNGLXGETCLIENTSTRINGPROC) load(userptr, "glXGetClientString");
  98. glad_glXQueryExtensionsString = (PFNGLXQUERYEXTENSIONSSTRINGPROC) load(userptr, "glXQueryExtensionsString");
  99. glad_glXQueryServerString = (PFNGLXQUERYSERVERSTRINGPROC) load(userptr, "glXQueryServerString");
  100. }
  101. static void glad_glx_load_GLX_VERSION_1_2( GLADuserptrloadfunc load, void* userptr) {
  102. if(!GLAD_GLX_VERSION_1_2) return;
  103. glad_glXGetCurrentDisplay = (PFNGLXGETCURRENTDISPLAYPROC) load(userptr, "glXGetCurrentDisplay");
  104. }
  105. static void glad_glx_load_GLX_VERSION_1_3( GLADuserptrloadfunc load, void* userptr) {
  106. if(!GLAD_GLX_VERSION_1_3) return;
  107. glad_glXChooseFBConfig = (PFNGLXCHOOSEFBCONFIGPROC) load(userptr, "glXChooseFBConfig");
  108. glad_glXCreateNewContext = (PFNGLXCREATENEWCONTEXTPROC) load(userptr, "glXCreateNewContext");
  109. glad_glXCreatePbuffer = (PFNGLXCREATEPBUFFERPROC) load(userptr, "glXCreatePbuffer");
  110. glad_glXCreatePixmap = (PFNGLXCREATEPIXMAPPROC) load(userptr, "glXCreatePixmap");
  111. glad_glXCreateWindow = (PFNGLXCREATEWINDOWPROC) load(userptr, "glXCreateWindow");
  112. glad_glXDestroyPbuffer = (PFNGLXDESTROYPBUFFERPROC) load(userptr, "glXDestroyPbuffer");
  113. glad_glXDestroyPixmap = (PFNGLXDESTROYPIXMAPPROC) load(userptr, "glXDestroyPixmap");
  114. glad_glXDestroyWindow = (PFNGLXDESTROYWINDOWPROC) load(userptr, "glXDestroyWindow");
  115. glad_glXGetCurrentReadDrawable = (PFNGLXGETCURRENTREADDRAWABLEPROC) load(userptr, "glXGetCurrentReadDrawable");
  116. glad_glXGetFBConfigAttrib = (PFNGLXGETFBCONFIGATTRIBPROC) load(userptr, "glXGetFBConfigAttrib");
  117. glad_glXGetFBConfigs = (PFNGLXGETFBCONFIGSPROC) load(userptr, "glXGetFBConfigs");
  118. glad_glXGetSelectedEvent = (PFNGLXGETSELECTEDEVENTPROC) load(userptr, "glXGetSelectedEvent");
  119. glad_glXGetVisualFromFBConfig = (PFNGLXGETVISUALFROMFBCONFIGPROC) load(userptr, "glXGetVisualFromFBConfig");
  120. glad_glXMakeContextCurrent = (PFNGLXMAKECONTEXTCURRENTPROC) load(userptr, "glXMakeContextCurrent");
  121. glad_glXQueryContext = (PFNGLXQUERYCONTEXTPROC) load(userptr, "glXQueryContext");
  122. glad_glXQueryDrawable = (PFNGLXQUERYDRAWABLEPROC) load(userptr, "glXQueryDrawable");
  123. glad_glXSelectEvent = (PFNGLXSELECTEVENTPROC) load(userptr, "glXSelectEvent");
  124. }
  125. static void glad_glx_load_GLX_VERSION_1_4( GLADuserptrloadfunc load, void* userptr) {
  126. if(!GLAD_GLX_VERSION_1_4) return;
  127. glad_glXGetProcAddress = (PFNGLXGETPROCADDRESSPROC) load(userptr, "glXGetProcAddress");
  128. }
  129. static void glad_glx_load_GLX_ARB_create_context( GLADuserptrloadfunc load, void* userptr) {
  130. if(!GLAD_GLX_ARB_create_context) return;
  131. glad_glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC) load(userptr, "glXCreateContextAttribsARB");
  132. }
  133. static void glad_glx_load_GLX_ARB_get_proc_address( GLADuserptrloadfunc load, void* userptr) {
  134. if(!GLAD_GLX_ARB_get_proc_address) return;
  135. glad_glXGetProcAddressARB = (PFNGLXGETPROCADDRESSARBPROC) load(userptr, "glXGetProcAddressARB");
  136. }
  137. static void glad_glx_load_GLX_EXT_swap_control( GLADuserptrloadfunc load, void* userptr) {
  138. if(!GLAD_GLX_EXT_swap_control) return;
  139. glad_glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC) load(userptr, "glXSwapIntervalEXT");
  140. }
  141. static void glad_glx_load_GLX_MESA_swap_control( GLADuserptrloadfunc load, void* userptr) {
  142. if(!GLAD_GLX_MESA_swap_control) return;
  143. glad_glXGetSwapIntervalMESA = (PFNGLXGETSWAPINTERVALMESAPROC) load(userptr, "glXGetSwapIntervalMESA");
  144. glad_glXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC) load(userptr, "glXSwapIntervalMESA");
  145. }
  146. static void glad_glx_load_GLX_SGI_swap_control( GLADuserptrloadfunc load, void* userptr) {
  147. if(!GLAD_GLX_SGI_swap_control) return;
  148. glad_glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC) load(userptr, "glXSwapIntervalSGI");
  149. }
  150. static int glad_glx_has_extension(Display *display, int screen, const char *ext) {
  151. #ifndef GLX_VERSION_1_1
  152. GLAD_UNUSED(display);
  153. GLAD_UNUSED(screen);
  154. GLAD_UNUSED(ext);
  155. #else
  156. const char *terminator;
  157. const char *loc;
  158. const char *extensions;
  159. if (glXQueryExtensionsString == NULL) {
  160. return 0;
  161. }
  162. extensions = glXQueryExtensionsString(display, screen);
  163. if(extensions == NULL || ext == NULL) {
  164. return 0;
  165. }
  166. while(1) {
  167. loc = strstr(extensions, ext);
  168. if(loc == NULL)
  169. break;
  170. terminator = loc + strlen(ext);
  171. if((loc == extensions || *(loc - 1) == ' ') &&
  172. (*terminator == ' ' || *terminator == '\0')) {
  173. return 1;
  174. }
  175. extensions = terminator;
  176. }
  177. #endif
  178. return 0;
  179. }
  180. static GLADapiproc glad_glx_get_proc_from_userptr(void *userptr, const char* name) {
  181. return (GLAD_GNUC_EXTENSION (GLADapiproc (*)(const char *name)) userptr)(name);
  182. }
  183. static int glad_glx_find_extensions(Display *display, int screen) {
  184. GLAD_GLX_ARB_create_context = glad_glx_has_extension(display, screen, "GLX_ARB_create_context");
  185. GLAD_GLX_ARB_create_context_profile = glad_glx_has_extension(display, screen, "GLX_ARB_create_context_profile");
  186. GLAD_GLX_ARB_get_proc_address = glad_glx_has_extension(display, screen, "GLX_ARB_get_proc_address");
  187. GLAD_GLX_EXT_swap_control = glad_glx_has_extension(display, screen, "GLX_EXT_swap_control");
  188. GLAD_GLX_MESA_swap_control = glad_glx_has_extension(display, screen, "GLX_MESA_swap_control");
  189. GLAD_GLX_SGI_swap_control = glad_glx_has_extension(display, screen, "GLX_SGI_swap_control");
  190. return 1;
  191. }
  192. static int glad_glx_find_core_glx(Display **display, int *screen) {
  193. int major = 0, minor = 0;
  194. if(*display == NULL) {
  195. #ifdef GLAD_GLX_NO_X11
  196. GLAD_UNUSED(screen);
  197. return 0;
  198. #else
  199. *display = XOpenDisplay(0);
  200. if (*display == NULL) {
  201. return 0;
  202. }
  203. *screen = XScreenNumberOfScreen(XDefaultScreenOfDisplay(*display));
  204. #endif
  205. }
  206. glXQueryVersion(*display, &major, &minor);
  207. GLAD_GLX_VERSION_1_0 = (major == 1 && minor >= 0) || major > 1;
  208. GLAD_GLX_VERSION_1_1 = (major == 1 && minor >= 1) || major > 1;
  209. GLAD_GLX_VERSION_1_2 = (major == 1 && minor >= 2) || major > 1;
  210. GLAD_GLX_VERSION_1_3 = (major == 1 && minor >= 3) || major > 1;
  211. GLAD_GLX_VERSION_1_4 = (major == 1 && minor >= 4) || major > 1;
  212. return GLAD_MAKE_VERSION(major, minor);
  213. }
  214. int gladLoadGLXUserPtr(Display *display, int screen, GLADuserptrloadfunc load, void *userptr) {
  215. int version;
  216. glXQueryVersion = (PFNGLXQUERYVERSIONPROC) load(userptr, "glXQueryVersion");
  217. if(glXQueryVersion == NULL) return 0;
  218. version = glad_glx_find_core_glx(&display, &screen);
  219. glad_glx_load_GLX_VERSION_1_0(load, userptr);
  220. glad_glx_load_GLX_VERSION_1_1(load, userptr);
  221. glad_glx_load_GLX_VERSION_1_2(load, userptr);
  222. glad_glx_load_GLX_VERSION_1_3(load, userptr);
  223. glad_glx_load_GLX_VERSION_1_4(load, userptr);
  224. if (!glad_glx_find_extensions(display, screen)) return 0;
  225. glad_glx_load_GLX_ARB_create_context(load, userptr);
  226. glad_glx_load_GLX_ARB_get_proc_address(load, userptr);
  227. glad_glx_load_GLX_EXT_swap_control(load, userptr);
  228. glad_glx_load_GLX_MESA_swap_control(load, userptr);
  229. glad_glx_load_GLX_SGI_swap_control(load, userptr);
  230. return version;
  231. }
  232. int gladLoadGLX(Display *display, int screen, GLADloadfunc load) {
  233. return gladLoadGLXUserPtr(display, screen, glad_glx_get_proc_from_userptr, GLAD_GNUC_EXTENSION (void*) load);
  234. }
  235. #ifdef GLAD_GLX
  236. #ifndef GLAD_LOADER_LIBRARY_C_
  237. #define GLAD_LOADER_LIBRARY_C_
  238. #include <stddef.h>
  239. #include <stdlib.h>
  240. #if GLAD_PLATFORM_WIN32
  241. #include <windows.h>
  242. #else
  243. #include <dlfcn.h>
  244. #endif
  245. static void* glad_get_dlopen_handle(const char *lib_names[], int length) {
  246. void *handle = NULL;
  247. int i;
  248. for (i = 0; i < length; ++i) {
  249. #if GLAD_PLATFORM_WIN32
  250. #if GLAD_PLATFORM_UWP
  251. size_t buffer_size = (strlen(lib_names[i]) + 1) * sizeof(WCHAR);
  252. LPWSTR buffer = (LPWSTR) malloc(buffer_size);
  253. if (buffer != NULL) {
  254. int ret = MultiByteToWideChar(CP_ACP, 0, lib_names[i], -1, buffer, buffer_size);
  255. if (ret != 0) {
  256. handle = (void*) LoadPackagedLibrary(buffer, 0);
  257. }
  258. free((void*) buffer);
  259. }
  260. #else
  261. handle = (void*) LoadLibraryA(lib_names[i]);
  262. #endif
  263. #else
  264. handle = dlopen(lib_names[i], RTLD_LAZY | RTLD_LOCAL);
  265. #endif
  266. if (handle != NULL) {
  267. return handle;
  268. }
  269. }
  270. return NULL;
  271. }
  272. static void glad_close_dlopen_handle(void* handle) {
  273. if (handle != NULL) {
  274. #if GLAD_PLATFORM_WIN32
  275. FreeLibrary((HMODULE) handle);
  276. #else
  277. dlclose(handle);
  278. #endif
  279. }
  280. }
  281. static GLADapiproc glad_dlsym_handle(void* handle, const char *name) {
  282. if (handle == NULL) {
  283. return NULL;
  284. }
  285. #if GLAD_PLATFORM_WIN32
  286. return (GLADapiproc) GetProcAddress((HMODULE) handle, name);
  287. #else
  288. return GLAD_GNUC_EXTENSION (GLADapiproc) dlsym(handle, name);
  289. #endif
  290. }
  291. #endif /* GLAD_LOADER_LIBRARY_C_ */
  292. typedef void* (GLAD_API_PTR *GLADglxprocaddrfunc)(const char*);
  293. static GLADapiproc glad_glx_get_proc(void *userptr, const char *name) {
  294. return GLAD_GNUC_EXTENSION ((GLADapiproc (*)(const char *name)) userptr)(name);
  295. }
  296. static void* _glx_handle;
  297. static void* glad_glx_dlopen_handle(void) {
  298. static const char *NAMES[] = {
  299. #if defined __CYGWIN__
  300. "libGL-1.so",
  301. #endif
  302. "libGL.so.1",
  303. "libGL.so"
  304. };
  305. if (_glx_handle == NULL) {
  306. _glx_handle = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
  307. }
  308. return _glx_handle;
  309. }
  310. int gladLoaderLoadGLX(Display *display, int screen) {
  311. int version = 0;
  312. void *handle = NULL;
  313. int did_load = 0;
  314. GLADglxprocaddrfunc loader;
  315. did_load = _glx_handle == NULL;
  316. handle = glad_glx_dlopen_handle();
  317. if (handle != NULL) {
  318. loader = (GLADglxprocaddrfunc) glad_dlsym_handle(handle, "glXGetProcAddressARB");
  319. if (loader != NULL) {
  320. version = gladLoadGLXUserPtr(display, screen, glad_glx_get_proc, GLAD_GNUC_EXTENSION (void*) loader);
  321. }
  322. if (!version && did_load) {
  323. gladLoaderUnloadGLX();
  324. }
  325. }
  326. return version;
  327. }
  328. void gladLoaderUnloadGLX() {
  329. if (_glx_handle != NULL) {
  330. glad_close_dlopen_handle(_glx_handle);
  331. _glx_handle = NULL;
  332. }
  333. }
  334. #endif /* GLAD_GLX */
  335. #ifdef __cplusplus
  336. }
  337. #endif