1848b8605Smrg/* 2848b8605Smrg Copyright (c) 2008 Apple Inc. 3848b8605Smrg 4848b8605Smrg Permission is hereby granted, free of charge, to any person 5848b8605Smrg obtaining a copy of this software and associated documentation files 6848b8605Smrg (the "Software"), to deal in the Software without restriction, 7848b8605Smrg including without limitation the rights to use, copy, modify, merge, 8848b8605Smrg publish, distribute, sublicense, and/or sell copies of the Software, 9848b8605Smrg and to permit persons to whom the Software is furnished to do so, 10848b8605Smrg subject to the following conditions: 11848b8605Smrg 12848b8605Smrg The above copyright notice and this permission notice shall be 13848b8605Smrg included in all copies or substantial portions of the Software. 14848b8605Smrg 15848b8605Smrg THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16848b8605Smrg EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17848b8605Smrg MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18848b8605Smrg NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT 19848b8605Smrg HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20848b8605Smrg WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21848b8605Smrg OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22848b8605Smrg DEALINGS IN THE SOFTWARE. 23848b8605Smrg 24848b8605Smrg Except as contained in this notice, the name(s) of the above 25848b8605Smrg copyright holders shall not be used in advertising or otherwise to 26848b8605Smrg promote the sale, use or other dealings in this Software without 27848b8605Smrg prior written authorization. 28848b8605Smrg*/ 29848b8605Smrg 30848b8605Smrg#include <stdbool.h> 31848b8605Smrg#include <stdio.h> 32848b8605Smrg#include <stdlib.h> 33848b8605Smrg#include <dlfcn.h> 34848b8605Smrg 35848b8605Smrg#include "apple_cgl.h" 36848b8605Smrg#include "apple_glx.h" 37848b8605Smrg 38848b8605Smrg#ifndef OPENGL_FRAMEWORK_PATH 39848b8605Smrg#define OPENGL_FRAMEWORK_PATH "/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL" 40848b8605Smrg#endif 41848b8605Smrg 42848b8605Smrgstatic void *dl_handle = NULL; 43848b8605Smrg 44848b8605Smrgstruct apple_cgl_api apple_cgl; 45848b8605Smrg 46848b8605Smrgstatic bool initialized = false; 47848b8605Smrg 48848b8605Smrgstatic void * 49848b8605Smrgsym(void *h, const char *name) 50848b8605Smrg{ 51848b8605Smrg void *s; 52848b8605Smrg 53848b8605Smrg s = dlsym(h, name); 54848b8605Smrg 55848b8605Smrg if (NULL == s) { 56848b8605Smrg fprintf(stderr, "error: %s\n", dlerror()); 57848b8605Smrg abort(); 58848b8605Smrg } 59848b8605Smrg 60848b8605Smrg return s; 61848b8605Smrg} 62848b8605Smrg 63848b8605Smrgvoid 64848b8605Smrgapple_cgl_init(void) 65848b8605Smrg{ 66848b8605Smrg void *h; 67848b8605Smrg const char *opengl_framework_path; 68848b8605Smrg 69848b8605Smrg if (initialized) 70848b8605Smrg return; 71848b8605Smrg 72848b8605Smrg opengl_framework_path = getenv("OPENGL_FRAMEWORK_PATH"); 73848b8605Smrg if (!opengl_framework_path) { 74848b8605Smrg opengl_framework_path = OPENGL_FRAMEWORK_PATH; 75848b8605Smrg } 76848b8605Smrg 77848b8605Smrg (void) dlerror(); /*drain dlerror */ 78848b8605Smrg h = dlopen(opengl_framework_path, RTLD_NOW); 79848b8605Smrg 80848b8605Smrg if (NULL == h) { 81848b8605Smrg fprintf(stderr, "error: unable to dlopen %s : %s\n", 82848b8605Smrg opengl_framework_path, dlerror()); 83848b8605Smrg abort(); 84848b8605Smrg } 85848b8605Smrg 86848b8605Smrg dl_handle = h; 87848b8605Smrg 88848b8605Smrg apple_cgl.get_version = sym(h, "CGLGetVersion"); 89848b8605Smrg 90848b8605Smrg apple_cgl.get_version(&apple_cgl.version_major, &apple_cgl.version_minor); 91848b8605Smrg 92848b8605Smrg apple_glx_diagnostic("CGL major %d minor %d\n", apple_cgl.version_major, apple_cgl.version_minor); 93848b8605Smrg 94848b8605Smrg if (1 != apple_cgl.version_major) { 95848b8605Smrg fprintf(stderr, "WARNING: the CGL major version has changed!\n" 96848b8605Smrg "libGL may be incompatible!\n"); 97848b8605Smrg } 98848b8605Smrg 99848b8605Smrg apple_cgl.choose_pixel_format = sym(h, "CGLChoosePixelFormat"); 100848b8605Smrg apple_cgl.destroy_pixel_format = sym(h, "CGLDestroyPixelFormat"); 101848b8605Smrg 102848b8605Smrg apple_cgl.clear_drawable = sym(h, "CGLClearDrawable"); 103848b8605Smrg apple_cgl.flush_drawable = sym(h, "CGLFlushDrawable"); 104848b8605Smrg 105848b8605Smrg apple_cgl.create_context = sym(h, "CGLCreateContext"); 106848b8605Smrg apple_cgl.destroy_context = sym(h, "CGLDestroyContext"); 107848b8605Smrg 108848b8605Smrg apple_cgl.set_current_context = sym(h, "CGLSetCurrentContext"); 109848b8605Smrg apple_cgl.get_current_context = sym(h, "CGLGetCurrentContext"); 110848b8605Smrg apple_cgl.error_string = sym(h, "CGLErrorString"); 111848b8605Smrg 112848b8605Smrg apple_cgl.set_off_screen = sym(h, "CGLSetOffScreen"); 113848b8605Smrg 114848b8605Smrg apple_cgl.copy_context = sym(h, "CGLCopyContext"); 115848b8605Smrg 116848b8605Smrg apple_cgl.create_pbuffer = sym(h, "CGLCreatePBuffer"); 117848b8605Smrg apple_cgl.destroy_pbuffer = sym(h, "CGLDestroyPBuffer"); 118848b8605Smrg apple_cgl.set_pbuffer = sym(h, "CGLSetPBuffer"); 119848b8605Smrg 120848b8605Smrg initialized = true; 121848b8605Smrg} 122848b8605Smrg 123848b8605Smrgvoid * 124848b8605Smrgapple_cgl_get_dl_handle(void) 125848b8605Smrg{ 126848b8605Smrg return dl_handle; 127848b8605Smrg} 128