4 #include "devicemanager.h"
19 static inline void GetDeviceProc(T
**func
, ALCdevice
*device
, const char *name
)
20 { *func
= reinterpret_cast<T
*>(alcGetProcAddress(device
, name
)); }
23 ALCboolean (ALC_APIENTRY
*ALDeviceManager::SetThreadContext
)(ALCcontext
*);
25 ALDeviceManager
&ALDeviceManager::get()
27 static ALDeviceManager singleton
;
31 ALDeviceManager::ALDeviceManager()
33 if(alcIsExtensionPresent(0, "ALC_EXT_thread_local_context"))
34 GetDeviceProc(&SetThreadContext
, 0, "alcSetThreadContext");
37 ALDeviceManager::~ALDeviceManager()
42 bool ALDeviceManager::queryExtension(const String
&name
) const
44 return alcIsExtensionPresent(nullptr, name
.c_str());
47 Vector
<String
> ALDeviceManager::enumerate(DeviceEnumeration type
) const
50 if(type
== DeviceEnumeration::Full
&& !alcIsExtensionPresent(nullptr, "ALC_ENUMERATE_ALL_EXT"))
51 type
= DeviceEnumeration::Basic
;
52 const ALCchar
*names
= alcGetString(nullptr, (ALenum
)type
);
53 while(names
&& *names
)
55 list
.emplace_back(names
);
56 names
+= strlen(names
)+1;
61 String
ALDeviceManager::defaultDeviceName(DefaultDeviceType type
) const
63 if(type
== DefaultDeviceType::Full
&& !alcIsExtensionPresent(nullptr, "ALC_ENUMERATE_ALL_EXT"))
64 type
= DefaultDeviceType::Basic
;
65 const ALCchar
*name
= alcGetString(nullptr, (ALenum
)type
);
66 return name
? String(name
) : String();
70 Device
ALDeviceManager::openPlayback(const String
&name
)
72 ALCdevice
*dev
= alcOpenDevice(name
.c_str());
76 throw std::runtime_error("Failed to open default device");
77 throw std::runtime_error("Failed to open device \""+name
+"\"");
79 mDevices
.emplace_back(MakeUnique
<ALDevice
>(dev
));
80 return Device(mDevices
.back().get());
83 Device
ALDeviceManager::openPlayback(const String
&name
, const std::nothrow_t
&)
85 ALCdevice
*dev
= alcOpenDevice(name
.c_str());
86 if(!dev
) return Device();
87 mDevices
.emplace_back(MakeUnique
<ALDevice
>(dev
));
88 return Device(mDevices
.back().get());
91 void ALDeviceManager::removeDevice(ALDevice
*dev
)
93 auto iter
= std::find_if(mDevices
.begin(), mDevices
.end(),
94 [dev
](const UniquePtr
<ALDevice
> &entry
) -> bool
95 { return entry
.get() == dev
; }
97 if(iter
!= mDevices
.end()) mDevices
.erase(iter
);
101 DeviceManager
DeviceManager::get()
102 { return DeviceManager(&ALDeviceManager::get()); }
103 DECL_THUNK1(bool, DeviceManager
, queryExtension
, const, const String
&)
104 DECL_THUNK1(Vector
<String
>, DeviceManager
, enumerate
, const, DeviceEnumeration
)
105 DECL_THUNK1(String
, DeviceManager
, defaultDeviceName
, const, DefaultDeviceType
)
106 DECL_THUNK1(Device
, DeviceManager
, openPlayback
,, const String
&)
107 DECL_THUNK2(Device
, DeviceManager
, openPlayback
,, const String
&, const std::nothrow_t
&)
108 Device
DeviceManager::openPlayback(const std::nothrow_t
&)
109 { return openPlayback(String(), std::nothrow
); }