🛠️🐜 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.

204 lines
6.9 KiB

  1. #include "config.h"
  2. #include <algorithm>
  3. #include "AL/alc.h"
  4. #include "AL/al.h"
  5. #include "AL/alext.h"
  6. #include "alMain.h"
  7. #include "alcontext.h"
  8. #include "alError.h"
  9. #include "alAuxEffectSlot.h"
  10. #include "ringbuffer.h"
  11. #include "threads.h"
  12. #include "alexcpt.h"
  13. static int EventThread(ALCcontext *context)
  14. {
  15. RingBuffer *ring{context->AsyncEvents.get()};
  16. bool quitnow{false};
  17. while(LIKELY(!quitnow))
  18. {
  19. auto evt_data = ring->getReadVector().first;
  20. if(evt_data.len == 0)
  21. {
  22. context->EventSem.wait();
  23. continue;
  24. }
  25. std::lock_guard<std::mutex> _{context->EventCbLock};
  26. do {
  27. auto &evt = *reinterpret_cast<AsyncEvent*>(evt_data.buf);
  28. evt_data.buf += sizeof(AsyncEvent);
  29. evt_data.len -= 1;
  30. /* This automatically destructs the event object and advances the
  31. * ringbuffer's read offset at the end of scope.
  32. */
  33. const struct EventAutoDestructor {
  34. AsyncEvent &evt_;
  35. RingBuffer *ring_;
  36. ~EventAutoDestructor()
  37. {
  38. evt_.~AsyncEvent();
  39. ring_->readAdvance(1);
  40. }
  41. } _{evt, ring};
  42. quitnow = evt.EnumType == EventType_KillThread;
  43. if(UNLIKELY(quitnow)) break;
  44. if(evt.EnumType == EventType_ReleaseEffectState)
  45. {
  46. evt.u.mEffectState->DecRef();
  47. continue;
  48. }
  49. ALbitfieldSOFT enabledevts{context->EnabledEvts.load(std::memory_order_acquire)};
  50. if(!context->EventCb) continue;
  51. if(evt.EnumType == EventType_SourceStateChange)
  52. {
  53. if(!(enabledevts&EventType_SourceStateChange))
  54. continue;
  55. std::string msg{"Source ID " + std::to_string(evt.u.srcstate.id)};
  56. msg += " state has changed to ";
  57. msg += (evt.u.srcstate.state==AL_INITIAL) ? "AL_INITIAL" :
  58. (evt.u.srcstate.state==AL_PLAYING) ? "AL_PLAYING" :
  59. (evt.u.srcstate.state==AL_PAUSED) ? "AL_PAUSED" :
  60. (evt.u.srcstate.state==AL_STOPPED) ? "AL_STOPPED" : "<unknown>";
  61. context->EventCb(AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT, evt.u.srcstate.id,
  62. evt.u.srcstate.state, static_cast<ALsizei>(msg.length()), msg.c_str(),
  63. context->EventParam
  64. );
  65. }
  66. else if(evt.EnumType == EventType_BufferCompleted)
  67. {
  68. if(!(enabledevts&EventType_BufferCompleted))
  69. continue;
  70. std::string msg{std::to_string(evt.u.bufcomp.count)};
  71. if(evt.u.bufcomp.count == 1) msg += " buffer completed";
  72. else msg += " buffers completed";
  73. context->EventCb(AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT, evt.u.bufcomp.id,
  74. evt.u.bufcomp.count, static_cast<ALsizei>(msg.length()), msg.c_str(),
  75. context->EventParam
  76. );
  77. }
  78. else if((enabledevts&evt.EnumType) == evt.EnumType)
  79. context->EventCb(evt.u.user.type, evt.u.user.id, evt.u.user.param,
  80. static_cast<ALsizei>(strlen(evt.u.user.msg)), evt.u.user.msg,
  81. context->EventParam
  82. );
  83. } while(evt_data.len != 0);
  84. }
  85. return 0;
  86. }
  87. void StartEventThrd(ALCcontext *ctx)
  88. {
  89. try {
  90. ctx->EventThread = std::thread(EventThread, ctx);
  91. }
  92. catch(std::exception& e) {
  93. ERR("Failed to start event thread: %s\n", e.what());
  94. }
  95. catch(...) {
  96. ERR("Failed to start event thread! Expect problems.\n");
  97. }
  98. }
  99. void StopEventThrd(ALCcontext *ctx)
  100. {
  101. static constexpr AsyncEvent kill_evt{EventType_KillThread};
  102. RingBuffer *ring{ctx->AsyncEvents.get()};
  103. auto evt_data = ring->getWriteVector().first;
  104. if(evt_data.len == 0)
  105. {
  106. do {
  107. std::this_thread::yield();
  108. evt_data = ring->getWriteVector().first;
  109. } while(evt_data.len == 0);
  110. }
  111. new (evt_data.buf) AsyncEvent{kill_evt};
  112. ring->writeAdvance(1);
  113. ctx->EventSem.post();
  114. if(ctx->EventThread.joinable())
  115. ctx->EventThread.join();
  116. }
  117. AL_API void AL_APIENTRY alEventControlSOFT(ALsizei count, const ALenum *types, ALboolean enable)
  118. START_API_FUNC
  119. {
  120. ContextRef context{GetContextRef()};
  121. if(UNLIKELY(!context)) return;
  122. if(count < 0) SETERR_RETURN(context.get(), AL_INVALID_VALUE,, "Controlling %d events", count);
  123. if(count == 0) return;
  124. if(!types) SETERR_RETURN(context.get(), AL_INVALID_VALUE,, "NULL pointer");
  125. ALbitfieldSOFT flags{0};
  126. const ALenum *types_end = types+count;
  127. auto bad_type = std::find_if_not(types, types_end,
  128. [&flags](ALenum type) noexcept -> bool
  129. {
  130. if(type == AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT)
  131. flags |= EventType_BufferCompleted;
  132. else if(type == AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT)
  133. flags |= EventType_SourceStateChange;
  134. else if(type == AL_EVENT_TYPE_ERROR_SOFT)
  135. flags |= EventType_Error;
  136. else if(type == AL_EVENT_TYPE_PERFORMANCE_SOFT)
  137. flags |= EventType_Performance;
  138. else if(type == AL_EVENT_TYPE_DEPRECATED_SOFT)
  139. flags |= EventType_Deprecated;
  140. else if(type == AL_EVENT_TYPE_DISCONNECTED_SOFT)
  141. flags |= EventType_Disconnected;
  142. else
  143. return false;
  144. return true;
  145. }
  146. );
  147. if(bad_type != types_end)
  148. SETERR_RETURN(context.get(), AL_INVALID_ENUM,, "Invalid event type 0x%04x", *bad_type);
  149. if(enable)
  150. {
  151. ALbitfieldSOFT enabledevts{context->EnabledEvts.load(std::memory_order_relaxed)};
  152. while(context->EnabledEvts.compare_exchange_weak(enabledevts, enabledevts|flags,
  153. std::memory_order_acq_rel, std::memory_order_acquire) == 0)
  154. {
  155. /* enabledevts is (re-)filled with the current value on failure, so
  156. * just try again.
  157. */
  158. }
  159. }
  160. else
  161. {
  162. ALbitfieldSOFT enabledevts{context->EnabledEvts.load(std::memory_order_relaxed)};
  163. while(context->EnabledEvts.compare_exchange_weak(enabledevts, enabledevts&~flags,
  164. std::memory_order_acq_rel, std::memory_order_acquire) == 0)
  165. {
  166. }
  167. /* Wait to ensure the event handler sees the changed flags before
  168. * returning.
  169. */
  170. std::lock_guard<std::mutex>{context->EventCbLock};
  171. }
  172. }
  173. END_API_FUNC
  174. AL_API void AL_APIENTRY alEventCallbackSOFT(ALEVENTPROCSOFT callback, void *userParam)
  175. START_API_FUNC
  176. {
  177. ContextRef context{GetContextRef()};
  178. if(UNLIKELY(!context)) return;
  179. std::lock_guard<std::mutex> _{context->PropLock};
  180. std::lock_guard<std::mutex> __{context->EventCbLock};
  181. context->EventCb = callback;
  182. context->EventParam = userParam;
  183. }
  184. END_API_FUNC