| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- //
- // Dynamically loads the vorbis shared library / dll
- // Currently only get the pointer to the function to get the library version
- //
- #include "loader.h"
- typedef void (*alProc)(void);
- //
- // Windows --------------------------------------------------------------------
- //
- #ifdef _WIN32
- #define WIN32_LEAN_AND_MEAN 1
- #include <windows.h>
- static HMODULE libvb;
- static int open_libvb(void) {
- libvb = LoadLibraryA("libvorbis.dll");
- if (libvb == NULL) {
- return -1;
- }
- return 0;
- }
- static void close_libvb(void) {
- FreeLibrary(libvb);
- }
- static alProc get_proc(const char *proc) {
- return (alProc) GetProcAddress(libvb, proc);
- }
- //
- // Mac --------------------------------------------------------------------
- //
- #elif defined(__APPLE__)
- #include <dlfcn.h>
- static void *libvb;
- static int open_libvb(void) {
- libvb = dlopen("/System/Library/Frameworks/libvorbis.framework/libvorbis", RTLD_LAZY | RTLD_GLOBAL);
- if (!libvb) {
- return -1;
- }
- return 0;
- }
- static void close_libvb(void) {
- dlclose(libvb);
- }
- static void* get_proc(const char *proc) {
- void* res;
- *(void **)(&res) = dlsym(libvb, proc);
- return res;
- }
- //
- // Linux --------------------------------------------------------------------
- //
- #else
- #include <dlfcn.h>
- static void *libvb;
- static char* lib_names[] = {
- "libvorbis.so",
- "libvorbis.so.0",
- NULL
- };
- static int open_libvb(void) {
- int i = 0;
- while (lib_names[i] != NULL) {
- libvb = dlopen(lib_names[i], RTLD_LAZY | RTLD_GLOBAL);
- if (libvb != NULL) {
- dlerror(); // clear errors
- return 0;
- }
- i++;
- }
- return -1;
- }
- static void close_libvb(void) {
- dlclose(libvb);
- }
- static alProc get_proc(const char *proc) {
- return dlsym(libvb, proc);
- }
- #endif
- // Prototypes of local functions
- static void load_procs(void);
- // Pointers to functions loaded from shared library
- LPVORBISVERSIONSTRING p_vorbis_version_string;
- // Load functions from shared library
- int vorbis_load() {
- int res = open_libvb();
- if (res) {
- return res;
- }
- load_procs();
- return 0;
- }
- static void load_procs(void) {
- p_vorbis_version_string = (LPVORBISVERSIONSTRING)get_proc("vorbis_version_string");
- }
- //
- // Go code cannot directly call the vorbis file function pointers loaded dynamically
- // The following C functions call the corresponding function pointers and can be
- // called by Go code.
- //
- const char *vorbis_version_string(void) {
- return p_vorbis_version_string();
- }
|