loader.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Dynamically loads the vorbis shared library / dll
  3. // Currently only get the pointer to the function to get the library version
  4. //
  5. #include "loader.h"
  6. typedef void (*alProc)(void);
  7. //
  8. // Windows --------------------------------------------------------------------
  9. //
  10. #ifdef _WIN32
  11. #define WIN32_LEAN_AND_MEAN 1
  12. #include <windows.h>
  13. static HMODULE libvb;
  14. static int open_libvb(void) {
  15. libvb = LoadLibraryA("libvorbis.dll");
  16. if (libvb == NULL) {
  17. return -1;
  18. }
  19. return 0;
  20. }
  21. static void close_libvb(void) {
  22. FreeLibrary(libvb);
  23. }
  24. static alProc get_proc(const char *proc) {
  25. return (alProc) GetProcAddress(libvb, proc);
  26. }
  27. //
  28. // Mac --------------------------------------------------------------------
  29. //
  30. #elif defined(__APPLE__)
  31. #include <dlfcn.h>
  32. static void *libvb;
  33. static int open_libvb(void) {
  34. libvb = dlopen("/System/Library/Frameworks/libvorbis.framework/libvorbis", RTLD_LAZY | RTLD_GLOBAL);
  35. if (!libvb) {
  36. return -1;
  37. }
  38. return 0;
  39. }
  40. static void close_libvb(void) {
  41. dlclose(libvb);
  42. }
  43. static void* get_proc(const char *proc) {
  44. void* res;
  45. *(void **)(&res) = dlsym(libvb, proc);
  46. return res;
  47. }
  48. //
  49. // Linux --------------------------------------------------------------------
  50. //
  51. #else
  52. #include <dlfcn.h>
  53. static void *libvb;
  54. static char* lib_names[] = {
  55. "libvorbis.so",
  56. "libvorbis.so.0",
  57. NULL
  58. };
  59. static int open_libvb(void) {
  60. int i = 0;
  61. while (lib_names[i] != NULL) {
  62. libvb = dlopen(lib_names[i], RTLD_LAZY | RTLD_GLOBAL);
  63. if (libvb != NULL) {
  64. dlerror(); // clear errors
  65. return 0;
  66. }
  67. i++;
  68. }
  69. return -1;
  70. }
  71. static void close_libvb(void) {
  72. dlclose(libvb);
  73. }
  74. static alProc get_proc(const char *proc) {
  75. return dlsym(libvb, proc);
  76. }
  77. #endif
  78. // Prototypes of local functions
  79. static void load_procs(void);
  80. // Pointers to functions loaded from shared library
  81. LPVORBISVERSIONSTRING p_vorbis_version_string;
  82. // Load functions from shared library
  83. int vorbis_load() {
  84. int res = open_libvb();
  85. if (res) {
  86. return res;
  87. }
  88. load_procs();
  89. return 0;
  90. }
  91. static void load_procs(void) {
  92. p_vorbis_version_string = (LPVORBISVERSIONSTRING)get_proc("vorbis_version_string");
  93. }
  94. //
  95. // Go code cannot directly call the vorbis file function pointers loaded dynamically
  96. // The following C functions call the corresponding function pointers and can be
  97. // called by Go code.
  98. //
  99. const char *vorbis_version_string(void) {
  100. return p_vorbis_version_string();
  101. }