🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
7.1 KiB

  1. #ifndef ROUTER_ROUTER_H
  2. #define ROUTER_ROUTER_H
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <windows.h>
  5. #include <winnt.h>
  6. #include <stdio.h>
  7. #include <atomic>
  8. #include <mutex>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "AL/alc.h"
  13. #include "AL/al.h"
  14. #include "AL/alext.h"
  15. #define MAKE_ALC_VER(major, minor) (((major)<<8) | (minor))
  16. struct DriverIface {
  17. std::wstring Name;
  18. HMODULE Module{nullptr};
  19. int ALCVer{0};
  20. LPALCCREATECONTEXT alcCreateContext{nullptr};
  21. LPALCMAKECONTEXTCURRENT alcMakeContextCurrent{nullptr};
  22. LPALCPROCESSCONTEXT alcProcessContext{nullptr};
  23. LPALCSUSPENDCONTEXT alcSuspendContext{nullptr};
  24. LPALCDESTROYCONTEXT alcDestroyContext{nullptr};
  25. LPALCGETCURRENTCONTEXT alcGetCurrentContext{nullptr};
  26. LPALCGETCONTEXTSDEVICE alcGetContextsDevice{nullptr};
  27. LPALCOPENDEVICE alcOpenDevice{nullptr};
  28. LPALCCLOSEDEVICE alcCloseDevice{nullptr};
  29. LPALCGETERROR alcGetError{nullptr};
  30. LPALCISEXTENSIONPRESENT alcIsExtensionPresent{nullptr};
  31. LPALCGETPROCADDRESS alcGetProcAddress{nullptr};
  32. LPALCGETENUMVALUE alcGetEnumValue{nullptr};
  33. LPALCGETSTRING alcGetString{nullptr};
  34. LPALCGETINTEGERV alcGetIntegerv{nullptr};
  35. LPALCCAPTUREOPENDEVICE alcCaptureOpenDevice{nullptr};
  36. LPALCCAPTURECLOSEDEVICE alcCaptureCloseDevice{nullptr};
  37. LPALCCAPTURESTART alcCaptureStart{nullptr};
  38. LPALCCAPTURESTOP alcCaptureStop{nullptr};
  39. LPALCCAPTURESAMPLES alcCaptureSamples{nullptr};
  40. PFNALCSETTHREADCONTEXTPROC alcSetThreadContext{nullptr};
  41. PFNALCGETTHREADCONTEXTPROC alcGetThreadContext{nullptr};
  42. LPALENABLE alEnable{nullptr};
  43. LPALDISABLE alDisable{nullptr};
  44. LPALISENABLED alIsEnabled{nullptr};
  45. LPALGETSTRING alGetString{nullptr};
  46. LPALGETBOOLEANV alGetBooleanv{nullptr};
  47. LPALGETINTEGERV alGetIntegerv{nullptr};
  48. LPALGETFLOATV alGetFloatv{nullptr};
  49. LPALGETDOUBLEV alGetDoublev{nullptr};
  50. LPALGETBOOLEAN alGetBoolean{nullptr};
  51. LPALGETINTEGER alGetInteger{nullptr};
  52. LPALGETFLOAT alGetFloat{nullptr};
  53. LPALGETDOUBLE alGetDouble{nullptr};
  54. LPALGETERROR alGetError{nullptr};
  55. LPALISEXTENSIONPRESENT alIsExtensionPresent{nullptr};
  56. LPALGETPROCADDRESS alGetProcAddress{nullptr};
  57. LPALGETENUMVALUE alGetEnumValue{nullptr};
  58. LPALLISTENERF alListenerf{nullptr};
  59. LPALLISTENER3F alListener3f{nullptr};
  60. LPALLISTENERFV alListenerfv{nullptr};
  61. LPALLISTENERI alListeneri{nullptr};
  62. LPALLISTENER3I alListener3i{nullptr};
  63. LPALLISTENERIV alListeneriv{nullptr};
  64. LPALGETLISTENERF alGetListenerf{nullptr};
  65. LPALGETLISTENER3F alGetListener3f{nullptr};
  66. LPALGETLISTENERFV alGetListenerfv{nullptr};
  67. LPALGETLISTENERI alGetListeneri{nullptr};
  68. LPALGETLISTENER3I alGetListener3i{nullptr};
  69. LPALGETLISTENERIV alGetListeneriv{nullptr};
  70. LPALGENSOURCES alGenSources{nullptr};
  71. LPALDELETESOURCES alDeleteSources{nullptr};
  72. LPALISSOURCE alIsSource{nullptr};
  73. LPALSOURCEF alSourcef{nullptr};
  74. LPALSOURCE3F alSource3f{nullptr};
  75. LPALSOURCEFV alSourcefv{nullptr};
  76. LPALSOURCEI alSourcei{nullptr};
  77. LPALSOURCE3I alSource3i{nullptr};
  78. LPALSOURCEIV alSourceiv{nullptr};
  79. LPALGETSOURCEF alGetSourcef{nullptr};
  80. LPALGETSOURCE3F alGetSource3f{nullptr};
  81. LPALGETSOURCEFV alGetSourcefv{nullptr};
  82. LPALGETSOURCEI alGetSourcei{nullptr};
  83. LPALGETSOURCE3I alGetSource3i{nullptr};
  84. LPALGETSOURCEIV alGetSourceiv{nullptr};
  85. LPALSOURCEPLAYV alSourcePlayv{nullptr};
  86. LPALSOURCESTOPV alSourceStopv{nullptr};
  87. LPALSOURCEREWINDV alSourceRewindv{nullptr};
  88. LPALSOURCEPAUSEV alSourcePausev{nullptr};
  89. LPALSOURCEPLAY alSourcePlay{nullptr};
  90. LPALSOURCESTOP alSourceStop{nullptr};
  91. LPALSOURCEREWIND alSourceRewind{nullptr};
  92. LPALSOURCEPAUSE alSourcePause{nullptr};
  93. LPALSOURCEQUEUEBUFFERS alSourceQueueBuffers{nullptr};
  94. LPALSOURCEUNQUEUEBUFFERS alSourceUnqueueBuffers{nullptr};
  95. LPALGENBUFFERS alGenBuffers{nullptr};
  96. LPALDELETEBUFFERS alDeleteBuffers{nullptr};
  97. LPALISBUFFER alIsBuffer{nullptr};
  98. LPALBUFFERF alBufferf{nullptr};
  99. LPALBUFFER3F alBuffer3f{nullptr};
  100. LPALBUFFERFV alBufferfv{nullptr};
  101. LPALBUFFERI alBufferi{nullptr};
  102. LPALBUFFER3I alBuffer3i{nullptr};
  103. LPALBUFFERIV alBufferiv{nullptr};
  104. LPALGETBUFFERF alGetBufferf{nullptr};
  105. LPALGETBUFFER3F alGetBuffer3f{nullptr};
  106. LPALGETBUFFERFV alGetBufferfv{nullptr};
  107. LPALGETBUFFERI alGetBufferi{nullptr};
  108. LPALGETBUFFER3I alGetBuffer3i{nullptr};
  109. LPALGETBUFFERIV alGetBufferiv{nullptr};
  110. LPALBUFFERDATA alBufferData{nullptr};
  111. LPALDOPPLERFACTOR alDopplerFactor{nullptr};
  112. LPALDOPPLERVELOCITY alDopplerVelocity{nullptr};
  113. LPALSPEEDOFSOUND alSpeedOfSound{nullptr};
  114. LPALDISTANCEMODEL alDistanceModel{nullptr};
  115. template<typename T>
  116. DriverIface(T&& name, HMODULE mod)
  117. : Name(std::forward<T>(name)), Module(mod)
  118. { }
  119. ~DriverIface()
  120. {
  121. if(Module)
  122. FreeLibrary(Module);
  123. Module = nullptr;
  124. }
  125. };
  126. extern std::vector<DriverIface> DriverList;
  127. extern thread_local DriverIface *ThreadCtxDriver;
  128. extern std::atomic<DriverIface*> CurrentCtxDriver;
  129. /* HACK: MinGW generates bad code when accessing an extern thread_local object.
  130. * Add a wrapper function for it that only accesses it where it's defined.
  131. */
  132. #ifdef __MINGW32__
  133. DriverIface *GetThreadDriver() noexcept;
  134. void SetThreadDriver(DriverIface *driver) noexcept;
  135. #else
  136. inline DriverIface *GetThreadDriver() noexcept { return ThreadCtxDriver; }
  137. inline void SetThreadDriver(DriverIface *driver) noexcept { ThreadCtxDriver = driver; }
  138. #endif
  139. class PtrIntMap {
  140. void **mKeys{nullptr};
  141. /* Shares memory with keys. */
  142. int *mValues{nullptr};
  143. ALsizei mSize{0};
  144. ALsizei mCapacity{0};
  145. std::mutex mLock;
  146. public:
  147. PtrIntMap() = default;
  148. ~PtrIntMap();
  149. ALenum insert(void *key, int value);
  150. int removeByKey(void *key);
  151. int lookupByKey(void *key);
  152. };
  153. enum LogLevel {
  154. LogLevel_None = 0,
  155. LogLevel_Error = 1,
  156. LogLevel_Warn = 2,
  157. LogLevel_Trace = 3,
  158. };
  159. extern enum LogLevel LogLevel;
  160. extern FILE *LogFile;
  161. #define TRACE(...) do { \
  162. if(LogLevel >= LogLevel_Trace) \
  163. { \
  164. fprintf(LogFile, "AL Router (II): " __VA_ARGS__); \
  165. fflush(LogFile); \
  166. } \
  167. } while(0)
  168. #define WARN(...) do { \
  169. if(LogLevel >= LogLevel_Warn) \
  170. { \
  171. fprintf(LogFile, "AL Router (WW): " __VA_ARGS__); \
  172. fflush(LogFile); \
  173. } \
  174. } while(0)
  175. #define ERR(...) do { \
  176. if(LogLevel >= LogLevel_Error) \
  177. { \
  178. fprintf(LogFile, "AL Router (EE): " __VA_ARGS__); \
  179. fflush(LogFile); \
  180. } \
  181. } while(0)
  182. #endif /* ROUTER_ROUTER_H */