Document some source methods
[alure.git] / src / devicemanager.cpp
blob3761cf9869ecdf19189aba8b4fb55f67ca423834
2 #include "config.h"
4 #include "devicemanager.h"
5 #include "device.h"
7 #include <string.h>
9 #include <algorithm>
10 #include <stdexcept>
12 #include "alc.h"
13 #include "al.h"
15 namespace alure
18 template<typename T>
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 DeviceManager *DeviceManager::get()
27 return &ALDeviceManager::get();
31 ALDeviceManager &ALDeviceManager::get()
33 static ALDeviceManager singleton;
34 return singleton;
38 ALDeviceManager::ALDeviceManager()
40 if(alcIsExtensionPresent(0, "ALC_EXT_thread_local_context"))
41 GetDeviceProc(&SetThreadContext, 0, "alcSetThreadContext");
44 bool ALDeviceManager::queryExtension(const char *extname) const
46 return alcIsExtensionPresent(0, extname);
49 std::vector<std::string> ALDeviceManager::enumerate(DeviceEnumeration type) const
51 std::vector<std::string> list;
52 if(type == DevEnum_Complete && !alcIsExtensionPresent(0, "ALC_ENUMERATE_ALL_EXT"))
53 type = DevEnum_Basic;
54 const ALCchar *names = alcGetString(0, type);
55 while(names && *names)
57 list.push_back(names);
58 names += strlen(names)+1;
60 return list;
63 std::string ALDeviceManager::defaultDeviceName(DefaultDeviceType type) const
65 if(type == DefaultDevType_Complete && !alcIsExtensionPresent(0, "ALC_ENUMERATE_ALL_EXT"))
66 type = DefaultDevType_Basic;
67 const ALCchar *name = alcGetString(0, type);
68 return std::string(name ? name : "");
72 Device *ALDeviceManager::openPlayback(const std::string &name)
74 ALCdevice *dev = alcOpenDevice(name.c_str());
75 if(!dev)
77 if(name.empty())
78 throw std::runtime_error("Failed to open default device");
79 throw std::runtime_error("Failed to open device \""+name+"\"");
81 return new ALDevice(dev);