Don't lock the device state when preparing a new effect state
[openal-soft.git] / alc / events.cpp
blob1010a33840ad5d8d791d3bea0b3f1ff68330f21b
2 #include "config.h"
4 #include "events.h"
6 #include "alspan.h"
7 #include "core/logging.h"
8 #include "device.h"
11 namespace {
13 ALCenum EnumFromEventType(const alc::EventType type)
15 switch(type)
17 case alc::EventType::DefaultDeviceChanged: return ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT;
18 case alc::EventType::DeviceAdded: return ALC_EVENT_TYPE_DEVICE_ADDED_SOFT;
19 case alc::EventType::DeviceRemoved: return ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT;
20 case alc::EventType::Count: break;
22 throw std::runtime_error{"Invalid EventType: "+std::to_string(al::to_underlying(type))};
25 } // namespace
27 namespace alc {
29 std::optional<alc::EventType> GetEventType(ALCenum type)
31 switch(type)
33 case ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT: return alc::EventType::DefaultDeviceChanged;
34 case ALC_EVENT_TYPE_DEVICE_ADDED_SOFT: return alc::EventType::DeviceAdded;
35 case ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT: return alc::EventType::DeviceRemoved;
37 return std::nullopt;
40 void Event(EventType eventType, DeviceType deviceType, ALCdevice *device, std::string_view message) noexcept
42 auto eventlock = std::unique_lock{EventMutex};
43 if(EventCallback && EventsEnabled.test(al::to_underlying(eventType)))
44 EventCallback(EnumFromEventType(eventType), al::to_underlying(deviceType), device,
45 static_cast<ALCsizei>(message.length()), message.data(), EventUserPtr);
48 } // namespace alc
50 FORCE_ALIGN ALCboolean ALC_APIENTRY alcEventControlSOFT(ALCsizei count, const ALCenum *events,
51 ALCboolean enable) noexcept
53 if(enable != ALC_FALSE && enable != ALC_TRUE)
55 alcSetError(nullptr, ALC_INVALID_ENUM);
56 return ALC_FALSE;
58 if(count < 0)
60 alcSetError(nullptr, ALC_INVALID_VALUE);
61 return ALC_FALSE;
63 if(count == 0)
64 return ALC_TRUE;
65 if(!events)
67 alcSetError(nullptr, ALC_INVALID_VALUE);
68 return ALC_FALSE;
71 alc::EventBitSet eventSet{0};
72 for(ALCenum type : al::span{events, static_cast<ALCuint>(count)})
74 auto etype = alc::GetEventType(type);
75 if(!etype)
77 WARN("Invalid event type: 0x%04x\n", type);
78 alcSetError(nullptr, ALC_INVALID_ENUM);
79 return ALC_FALSE;
81 eventSet.set(al::to_underlying(*etype));
84 auto eventlock = std::unique_lock{alc::EventMutex};
85 if(enable) alc::EventsEnabled |= eventSet;
86 else alc::EventsEnabled &= ~eventSet;
87 return ALC_TRUE;
90 FORCE_ALIGN void ALC_APIENTRY alcEventCallbackSOFT(ALCEVENTPROCTYPESOFT callback, void *userParam) noexcept
92 auto eventlock = std::unique_lock{alc::EventMutex};
93 alc::EventCallback = callback;
94 alc::EventUserPtr = userParam;