1 /* 2 Copyright (c) 2008 Apple Inc. 3 4 Permission is hereby granted, free of charge, to any person 5 obtaining a copy of this software and associated documentation files 6 (the "Software"), to deal in the Software without restriction, 7 including without limitation the rights to use, copy, modify, merge, 8 publish, distribute, sublicense, and/or sell copies of the Software, 9 and to permit persons to whom the Software is furnished to do so, 10 subject to the following conditions: 11 12 The above copyright notice and this permission notice shall be 13 included in all copies or substantial portions of the Software. 14 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT 19 HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 DEALINGS IN THE SOFTWARE. 23 24 Except as contained in this notice, the name(s) of the above 25 copyright holders shall not be used in advertising or otherwise to 26 promote the sale, use or other dealings in this Software without 27 prior written authorization. 28 */ 29 30 #include <stdbool.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <dlfcn.h> 34 35 #include "apple_cgl.h" 36 #include "apple_glx.h" 37 38 #ifndef OPENGL_FRAMEWORK_PATH 39 #define OPENGL_FRAMEWORK_PATH "/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL" 40 #endif 41 42 static void *dl_handle = NULL; 43 44 struct apple_cgl_api apple_cgl; 45 46 static bool initialized = false; 47 48 static void * 49 sym(void *h, const char *name) 50 { 51 void *s; 52 53 s = dlsym(h, name); 54 55 if (NULL == s) { 56 fprintf(stderr, "error: %s\n", dlerror()); 57 abort(); 58 } 59 60 return s; 61 } 62 63 void 64 apple_cgl_init(void) 65 { 66 void *h; 67 const char *opengl_framework_path; 68 69 if (initialized) 70 return; 71 72 opengl_framework_path = getenv("OPENGL_FRAMEWORK_PATH"); 73 if (!opengl_framework_path) { 74 opengl_framework_path = OPENGL_FRAMEWORK_PATH; 75 } 76 77 (void) dlerror(); /*drain dlerror */ 78 h = dlopen(opengl_framework_path, RTLD_NOW); 79 80 if (NULL == h) { 81 fprintf(stderr, "error: unable to dlopen %s : %s\n", 82 opengl_framework_path, dlerror()); 83 abort(); 84 } 85 86 dl_handle = h; 87 88 apple_cgl.get_version = sym(h, "CGLGetVersion"); 89 90 apple_cgl.get_version(&apple_cgl.version_major, &apple_cgl.version_minor); 91 92 apple_glx_diagnostic("CGL major %d minor %d\n", apple_cgl.version_major, apple_cgl.version_minor); 93 94 if (1 != apple_cgl.version_major) { 95 fprintf(stderr, "WARNING: the CGL major version has changed!\n" 96 "libGL may be incompatible!\n"); 97 } 98 99 apple_cgl.choose_pixel_format = sym(h, "CGLChoosePixelFormat"); 100 apple_cgl.destroy_pixel_format = sym(h, "CGLDestroyPixelFormat"); 101 102 apple_cgl.clear_drawable = sym(h, "CGLClearDrawable"); 103 apple_cgl.flush_drawable = sym(h, "CGLFlushDrawable"); 104 105 apple_cgl.create_context = sym(h, "CGLCreateContext"); 106 apple_cgl.destroy_context = sym(h, "CGLDestroyContext"); 107 108 apple_cgl.set_current_context = sym(h, "CGLSetCurrentContext"); 109 apple_cgl.get_current_context = sym(h, "CGLGetCurrentContext"); 110 apple_cgl.error_string = sym(h, "CGLErrorString"); 111 112 apple_cgl.set_off_screen = sym(h, "CGLSetOffScreen"); 113 114 apple_cgl.copy_context = sym(h, "CGLCopyContext"); 115 116 apple_cgl.create_pbuffer = sym(h, "CGLCreatePBuffer"); 117 apple_cgl.destroy_pbuffer = sym(h, "CGLDestroyPBuffer"); 118 apple_cgl.set_pbuffer = sym(h, "CGLSetPBuffer"); 119 120 initialized = true; 121 } 122 123 void * 124 apple_cgl_get_dl_handle(void) 125 { 126 return dl_handle; 127 } 128