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

740 lines
21 KiB

  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2011 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #ifdef _WIN32
  21. #ifdef __MINGW32__
  22. #define _WIN32_IE 0x501
  23. #else
  24. #define _WIN32_IE 0x400
  25. #endif
  26. #endif
  27. #include "config.h"
  28. #include <cstdlib>
  29. #include <ctime>
  30. #include <cerrno>
  31. #include <cstdarg>
  32. #include <cctype>
  33. #ifdef HAVE_MALLOC_H
  34. #include <malloc.h>
  35. #endif
  36. #ifdef HAVE_DIRENT_H
  37. #include <dirent.h>
  38. #endif
  39. #ifdef HAVE_PROC_PIDPATH
  40. #include <libproc.h>
  41. #endif
  42. #ifdef __FreeBSD__
  43. #include <sys/types.h>
  44. #include <sys/sysctl.h>
  45. #endif
  46. #ifndef AL_NO_UID_DEFS
  47. #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
  48. #define INITGUID
  49. #include <windows.h>
  50. #ifdef HAVE_GUIDDEF_H
  51. #include <guiddef.h>
  52. #else
  53. #include <initguid.h>
  54. #endif
  55. DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
  56. DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
  57. DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
  58. DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
  59. DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
  60. DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
  61. DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
  62. DEFINE_GUID(IID_IAudioCaptureClient, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4,0xde, 0x18,0x5c,0x39,0x5c,0xd3,0x17);
  63. #ifdef HAVE_WASAPI
  64. #include <wtypes.h>
  65. #include <devpropdef.h>
  66. #include <propkeydef.h>
  67. DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
  68. DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
  69. DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID, 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23,0xe0, 0xc0,0xff,0xee,0x7f,0x0e, 4 );
  70. #endif
  71. #endif
  72. #endif /* AL_NO_UID_DEFS */
  73. #ifdef HAVE_DLFCN_H
  74. #include <dlfcn.h>
  75. #endif
  76. #ifdef HAVE_INTRIN_H
  77. #include <intrin.h>
  78. #endif
  79. #ifdef HAVE_CPUID_H
  80. #include <cpuid.h>
  81. #endif
  82. #ifdef HAVE_SSE_INTRINSICS
  83. #include <xmmintrin.h>
  84. #endif
  85. #ifdef HAVE_SYS_SYSCONF_H
  86. #include <sys/sysconf.h>
  87. #endif
  88. #ifdef HAVE_FLOAT_H
  89. #include <cfloat>
  90. #endif
  91. #ifdef HAVE_IEEEFP_H
  92. #include <ieeefp.h>
  93. #endif
  94. #ifndef _WIN32
  95. #include <sys/types.h>
  96. #include <sys/stat.h>
  97. #include <sys/mman.h>
  98. #include <fcntl.h>
  99. #include <unistd.h>
  100. #elif defined(_WIN32_IE)
  101. #include <shlobj.h>
  102. #endif
  103. #include <mutex>
  104. #include <vector>
  105. #include <string>
  106. #include <algorithm>
  107. #include "alMain.h"
  108. #include "alu.h"
  109. #include "cpu_caps.h"
  110. #include "fpu_modes.h"
  111. #include "vector.h"
  112. #include "compat.h"
  113. #include "threads.h"
  114. #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
  115. defined(_M_IX86) || defined(_M_X64))
  116. using reg_type = unsigned int;
  117. static inline void get_cpuid(int f, reg_type *regs)
  118. { __get_cpuid(f, &regs[0], &regs[1], &regs[2], &regs[3]); }
  119. #define CAN_GET_CPUID
  120. #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
  121. defined(_M_IX86) || defined(_M_X64))
  122. using reg_type = int;
  123. static inline void get_cpuid(int f, reg_type *regs)
  124. { (__cpuid)(regs, f); }
  125. #define CAN_GET_CPUID
  126. #endif
  127. int CPUCapFlags = 0;
  128. void FillCPUCaps(int capfilter)
  129. {
  130. int caps = 0;
  131. /* FIXME: We really should get this for all available CPUs in case different
  132. * CPUs have different caps (is that possible on one machine?). */
  133. #ifdef CAN_GET_CPUID
  134. union {
  135. reg_type regs[4];
  136. char str[sizeof(reg_type[4])];
  137. } cpuinf[3] = {{ { 0, 0, 0, 0 } }};
  138. get_cpuid(0, cpuinf[0].regs);
  139. if(cpuinf[0].regs[0] == 0)
  140. ERR("Failed to get CPUID\n");
  141. else
  142. {
  143. unsigned int maxfunc = cpuinf[0].regs[0];
  144. unsigned int maxextfunc;
  145. get_cpuid(0x80000000, cpuinf[0].regs);
  146. maxextfunc = cpuinf[0].regs[0];
  147. TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
  148. TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
  149. if(maxextfunc >= 0x80000004)
  150. {
  151. get_cpuid(0x80000002, cpuinf[0].regs);
  152. get_cpuid(0x80000003, cpuinf[1].regs);
  153. get_cpuid(0x80000004, cpuinf[2].regs);
  154. TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
  155. }
  156. if(maxfunc >= 1)
  157. {
  158. get_cpuid(1, cpuinf[0].regs);
  159. if((cpuinf[0].regs[3]&(1<<25)))
  160. caps |= CPU_CAP_SSE;
  161. if((caps&CPU_CAP_SSE) && (cpuinf[0].regs[3]&(1<<26)))
  162. caps |= CPU_CAP_SSE2;
  163. if((caps&CPU_CAP_SSE2) && (cpuinf[0].regs[2]&(1<<0)))
  164. caps |= CPU_CAP_SSE3;
  165. if((caps&CPU_CAP_SSE3) && (cpuinf[0].regs[2]&(1<<19)))
  166. caps |= CPU_CAP_SSE4_1;
  167. }
  168. }
  169. #else
  170. /* Assume support for whatever's supported if we can't check for it */
  171. #if defined(HAVE_SSE4_1)
  172. #warning "Assuming SSE 4.1 run-time support!"
  173. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
  174. #elif defined(HAVE_SSE3)
  175. #warning "Assuming SSE 3 run-time support!"
  176. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
  177. #elif defined(HAVE_SSE2)
  178. #warning "Assuming SSE 2 run-time support!"
  179. caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
  180. #elif defined(HAVE_SSE)
  181. #warning "Assuming SSE run-time support!"
  182. caps |= CPU_CAP_SSE;
  183. #endif
  184. #endif
  185. #ifdef HAVE_NEON
  186. FILE *file = fopen("/proc/cpuinfo", "rt");
  187. if(!file)
  188. ERR("Failed to open /proc/cpuinfo, cannot check for NEON support\n");
  189. else
  190. {
  191. std::string features;
  192. char buf[256];
  193. while(fgets(buf, sizeof(buf), file) != nullptr)
  194. {
  195. if(strncmp(buf, "Features\t:", 10) != 0)
  196. continue;
  197. features = buf+10;
  198. while(features.back() != '\n')
  199. {
  200. if(fgets(buf, sizeof(buf), file) == nullptr)
  201. break;
  202. features += buf;
  203. }
  204. break;
  205. }
  206. fclose(file);
  207. file = nullptr;
  208. if(!features.empty())
  209. {
  210. const char *str = features.c_str();
  211. while(isspace(str[0])) ++str;
  212. TRACE("Got features string:%s\n", str);
  213. while((str=strstr(str, "neon")) != nullptr)
  214. {
  215. if(isspace(*(str-1)) && (str[4] == 0 || isspace(str[4])))
  216. {
  217. caps |= CPU_CAP_NEON;
  218. break;
  219. }
  220. ++str;
  221. }
  222. }
  223. }
  224. #endif
  225. TRACE("Extensions:%s%s%s%s%s%s\n",
  226. ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
  227. ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
  228. ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
  229. ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
  230. ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +NEON" : " -NEON") : ""),
  231. ((!capfilter) ? " -none-" : "")
  232. );
  233. CPUCapFlags = caps & capfilter;
  234. }
  235. FPUCtl::FPUCtl()
  236. {
  237. #if defined(HAVE_SSE_INTRINSICS)
  238. this->sse_state = _mm_getcsr();
  239. unsigned int sseState = this->sse_state;
  240. sseState |= 0x8000; /* set flush-to-zero */
  241. sseState |= 0x0040; /* set denormals-are-zero */
  242. _mm_setcsr(sseState);
  243. #elif defined(__GNUC__) && defined(HAVE_SSE)
  244. if((CPUCapFlags&CPU_CAP_SSE))
  245. {
  246. __asm__ __volatile__("stmxcsr %0" : "=m" (*&this->sse_state));
  247. unsigned int sseState = this->sse_state;
  248. sseState |= 0x8000; /* set flush-to-zero */
  249. if((CPUCapFlags&CPU_CAP_SSE2))
  250. sseState |= 0x0040; /* set denormals-are-zero */
  251. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
  252. }
  253. #endif
  254. this->in_mode = true;
  255. }
  256. void FPUCtl::leave()
  257. {
  258. if(!this->in_mode) return;
  259. #if defined(HAVE_SSE_INTRINSICS)
  260. _mm_setcsr(this->sse_state);
  261. #elif defined(__GNUC__) && defined(HAVE_SSE)
  262. if((CPUCapFlags&CPU_CAP_SSE))
  263. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&this->sse_state));
  264. #endif
  265. this->in_mode = false;
  266. }
  267. #ifdef _WIN32
  268. const PathNamePair &GetProcBinary()
  269. {
  270. static PathNamePair ret;
  271. if(!ret.fname.empty() || !ret.path.empty())
  272. return ret;
  273. al::vector<WCHAR> fullpath(256);
  274. DWORD len;
  275. while((len=GetModuleFileNameW(nullptr, fullpath.data(), static_cast<DWORD>(fullpath.size()))) == fullpath.size())
  276. fullpath.resize(fullpath.size() << 1);
  277. if(len == 0)
  278. {
  279. ERR("Failed to get process name: error %lu\n", GetLastError());
  280. return ret;
  281. }
  282. fullpath.resize(len);
  283. if(fullpath.back() != 0)
  284. fullpath.push_back(0);
  285. auto sep = std::find(fullpath.rbegin()+1, fullpath.rend(), '\\');
  286. sep = std::find(fullpath.rbegin()+1, sep, '/');
  287. if(sep != fullpath.rend())
  288. {
  289. *sep = 0;
  290. ret.fname = wstr_to_utf8(&*sep + 1);
  291. ret.path = wstr_to_utf8(fullpath.data());
  292. }
  293. else
  294. ret.fname = wstr_to_utf8(fullpath.data());
  295. TRACE("Got: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
  296. return ret;
  297. }
  298. void *LoadLib(const char *name)
  299. {
  300. std::wstring wname{utf8_to_wstr(name)};
  301. return LoadLibraryW(wname.c_str());
  302. }
  303. void CloseLib(void *handle)
  304. { FreeLibrary(static_cast<HMODULE>(handle)); }
  305. void *GetSymbol(void *handle, const char *name)
  306. {
  307. void *ret{reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), name))};
  308. if(!ret) ERR("Failed to load %s\n", name);
  309. return ret;
  310. }
  311. void al_print(const char *type, const char *prefix, const char *func, const char *fmt, ...)
  312. {
  313. al::vector<char> dynmsg;
  314. char stcmsg[256];
  315. char *str{stcmsg};
  316. va_list args, args2;
  317. va_start(args, fmt);
  318. va_copy(args2, args);
  319. int msglen{std::vsnprintf(str, sizeof(stcmsg), fmt, args)};
  320. if(UNLIKELY(msglen >= 0 && static_cast<size_t>(msglen) >= sizeof(stcmsg)))
  321. {
  322. dynmsg.resize(static_cast<size_t>(msglen) + 1u);
  323. str = dynmsg.data();
  324. msglen = std::vsnprintf(str, dynmsg.size(), fmt, args2);
  325. }
  326. va_end(args2);
  327. va_end(args);
  328. std::wstring wstr{utf8_to_wstr(str)};
  329. fprintf(gLogFile, "AL lib: %s %s%s: %ls", type, prefix, func, wstr.c_str());
  330. fflush(gLogFile);
  331. }
  332. static inline int is_slash(int c)
  333. { return (c == '\\' || c == '/'); }
  334. static void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
  335. {
  336. std::string pathstr{path};
  337. pathstr += "\\*";
  338. pathstr += ext;
  339. TRACE("Searching %s\n", pathstr.c_str());
  340. std::wstring wpath{utf8_to_wstr(pathstr.c_str())};
  341. WIN32_FIND_DATAW fdata;
  342. HANDLE hdl{FindFirstFileW(wpath.c_str(), &fdata)};
  343. if(hdl != INVALID_HANDLE_VALUE)
  344. {
  345. size_t base = results->size();
  346. do {
  347. results->emplace_back();
  348. std::string &str = results->back();
  349. str = path;
  350. str += '\\';
  351. str += wstr_to_utf8(fdata.cFileName);
  352. TRACE("Got result %s\n", str.c_str());
  353. } while(FindNextFileW(hdl, &fdata));
  354. FindClose(hdl);
  355. std::sort(results->begin()+base, results->end());
  356. }
  357. }
  358. al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
  359. {
  360. static std::mutex search_lock;
  361. std::lock_guard<std::mutex> _{search_lock};
  362. /* If the path is absolute, use it directly. */
  363. al::vector<std::string> results;
  364. if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2]))
  365. {
  366. std::string path{subdir};
  367. std::replace(path.begin(), path.end(), '/', '\\');
  368. DirectorySearch(path.c_str(), ext, &results);
  369. return results;
  370. }
  371. if(subdir[0] == '\\' && subdir[1] == '\\' && subdir[2] == '?' && subdir[3] == '\\')
  372. {
  373. DirectorySearch(subdir, ext, &results);
  374. return results;
  375. }
  376. std::string path;
  377. /* Search the app-local directory. */
  378. WCHAR *cwdbuf{_wgetenv(L"ALSOFT_LOCAL_PATH")};
  379. if(cwdbuf && *cwdbuf != '\0')
  380. {
  381. path = wstr_to_utf8(cwdbuf);
  382. if(is_slash(path.back()))
  383. path.pop_back();
  384. }
  385. else if(!(cwdbuf=_wgetcwd(nullptr, 0)))
  386. path = ".";
  387. else
  388. {
  389. path = wstr_to_utf8(cwdbuf);
  390. if(is_slash(path.back()))
  391. path.pop_back();
  392. free(cwdbuf);
  393. }
  394. std::replace(path.begin(), path.end(), '/', '\\');
  395. DirectorySearch(path.c_str(), ext, &results);
  396. /* Search the local and global data dirs. */
  397. static constexpr int ids[2]{ CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
  398. for(int id : ids)
  399. {
  400. WCHAR buffer[MAX_PATH];
  401. if(SHGetSpecialFolderPathW(nullptr, buffer, id, FALSE) == FALSE)
  402. continue;
  403. path = wstr_to_utf8(buffer);
  404. if(!is_slash(path.back()))
  405. path += '\\';
  406. path += subdir;
  407. std::replace(path.begin(), path.end(), '/', '\\');
  408. DirectorySearch(path.c_str(), ext, &results);
  409. }
  410. return results;
  411. }
  412. void SetRTPriority(void)
  413. {
  414. bool failed = false;
  415. if(RTPrioLevel > 0)
  416. failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  417. if(failed) ERR("Failed to set priority level for thread\n");
  418. }
  419. #else
  420. const PathNamePair &GetProcBinary()
  421. {
  422. static PathNamePair ret;
  423. if(!ret.fname.empty() || !ret.path.empty())
  424. return ret;
  425. al::vector<char> pathname;
  426. #ifdef __FreeBSD__
  427. size_t pathlen;
  428. int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
  429. if(sysctl(mib, 4, nullptr, &pathlen, nullptr, 0) == -1)
  430. WARN("Failed to sysctl kern.proc.pathname: %s\n", strerror(errno));
  431. else
  432. {
  433. pathname.resize(pathlen + 1);
  434. sysctl(mib, 4, pathname.data(), &pathlen, nullptr, 0);
  435. pathname.resize(pathlen);
  436. }
  437. #endif
  438. #ifdef HAVE_PROC_PIDPATH
  439. if(pathname.empty())
  440. {
  441. char procpath[PROC_PIDPATHINFO_MAXSIZE]{};
  442. const pid_t pid{getpid()};
  443. if(proc_pidpath(pid, procpath, sizeof(procpath)) < 1)
  444. ERR("proc_pidpath(%d, ...) failed: %s\n", pid, strerror(errno));
  445. else
  446. pathname.insert(pathname.end(), procpath, procpath+strlen(procpath));
  447. }
  448. #endif
  449. if(pathname.empty())
  450. {
  451. pathname.resize(256);
  452. const char *selfname{"/proc/self/exe"};
  453. ssize_t len{readlink(selfname, pathname.data(), pathname.size())};
  454. if(len == -1 && errno == ENOENT)
  455. {
  456. selfname = "/proc/self/file";
  457. len = readlink(selfname, pathname.data(), pathname.size());
  458. }
  459. if(len == -1 && errno == ENOENT)
  460. {
  461. selfname = "/proc/curproc/exe";
  462. len = readlink(selfname, pathname.data(), pathname.size());
  463. }
  464. if(len == -1 && errno == ENOENT)
  465. {
  466. selfname = "/proc/curproc/file";
  467. len = readlink(selfname, pathname.data(), pathname.size());
  468. }
  469. while(len > 0 && static_cast<size_t>(len) == pathname.size())
  470. {
  471. pathname.resize(pathname.size() << 1);
  472. len = readlink(selfname, pathname.data(), pathname.size());
  473. }
  474. if(len <= 0)
  475. {
  476. WARN("Failed to readlink %s: %s\n", selfname, strerror(errno));
  477. return ret;
  478. }
  479. pathname.resize(len);
  480. }
  481. while(!pathname.empty() && pathname.back() == 0)
  482. pathname.pop_back();
  483. auto sep = std::find(pathname.crbegin(), pathname.crend(), '/');
  484. if(sep != pathname.crend())
  485. {
  486. ret.path = std::string(pathname.cbegin(), sep.base()-1);
  487. ret.fname = std::string(sep.base(), pathname.cend());
  488. }
  489. else
  490. ret.fname = std::string(pathname.cbegin(), pathname.cend());
  491. TRACE("Got: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
  492. return ret;
  493. }
  494. #ifdef HAVE_DLFCN_H
  495. void *LoadLib(const char *name)
  496. {
  497. dlerror();
  498. void *handle{dlopen(name, RTLD_NOW)};
  499. const char *err{dlerror()};
  500. if(err) handle = nullptr;
  501. return handle;
  502. }
  503. void CloseLib(void *handle)
  504. { dlclose(handle); }
  505. void *GetSymbol(void *handle, const char *name)
  506. {
  507. dlerror();
  508. void *sym{dlsym(handle, name)};
  509. const char *err{dlerror()};
  510. if(err)
  511. {
  512. WARN("Failed to load %s: %s\n", name, err);
  513. sym = nullptr;
  514. }
  515. return sym;
  516. }
  517. #endif /* HAVE_DLFCN_H */
  518. void al_print(const char *type, const char *prefix, const char *func, const char *fmt, ...)
  519. {
  520. va_list ap;
  521. va_start(ap, fmt);
  522. fprintf(gLogFile, "AL lib: %s %s%s: ", type, prefix, func);
  523. vfprintf(gLogFile, fmt, ap);
  524. va_end(ap);
  525. fflush(gLogFile);
  526. }
  527. static void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
  528. {
  529. TRACE("Searching %s for *%s\n", path, ext);
  530. DIR *dir{opendir(path)};
  531. if(dir != nullptr)
  532. {
  533. const size_t extlen = strlen(ext);
  534. size_t base = results->size();
  535. struct dirent *dirent;
  536. while((dirent=readdir(dir)) != nullptr)
  537. {
  538. if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
  539. continue;
  540. size_t len{strlen(dirent->d_name)};
  541. if(len <= extlen) continue;
  542. if(strcasecmp(dirent->d_name+len-extlen, ext) != 0)
  543. continue;
  544. results->emplace_back();
  545. std::string &str = results->back();
  546. str = path;
  547. if(str.back() != '/')
  548. str.push_back('/');
  549. str += dirent->d_name;
  550. TRACE("Got result %s\n", str.c_str());
  551. }
  552. closedir(dir);
  553. std::sort(results->begin()+base, results->end());
  554. }
  555. }
  556. al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
  557. {
  558. static std::mutex search_lock;
  559. std::lock_guard<std::mutex> _{search_lock};
  560. al::vector<std::string> results;
  561. if(subdir[0] == '/')
  562. {
  563. DirectorySearch(subdir, ext, &results);
  564. return results;
  565. }
  566. /* Search the app-local directory. */
  567. const char *str{getenv("ALSOFT_LOCAL_PATH")};
  568. if(str && *str != '\0')
  569. DirectorySearch(str, ext, &results);
  570. else
  571. {
  572. al::vector<char> cwdbuf(256);
  573. while(!getcwd(cwdbuf.data(), cwdbuf.size()))
  574. {
  575. if(errno != ERANGE)
  576. {
  577. cwdbuf.clear();
  578. break;
  579. }
  580. cwdbuf.resize(cwdbuf.size() << 1);
  581. }
  582. if(cwdbuf.empty())
  583. DirectorySearch(".", ext, &results);
  584. else
  585. {
  586. DirectorySearch(cwdbuf.data(), ext, &results);
  587. cwdbuf.clear();
  588. }
  589. }
  590. // Search local data dir
  591. if((str=getenv("XDG_DATA_HOME")) != nullptr && str[0] != '\0')
  592. {
  593. std::string path{str};
  594. if(path.back() != '/')
  595. path += '/';
  596. path += subdir;
  597. DirectorySearch(path.c_str(), ext, &results);
  598. }
  599. else if((str=getenv("HOME")) != nullptr && str[0] != '\0')
  600. {
  601. std::string path{str};
  602. if(path.back() == '/')
  603. path.pop_back();
  604. path += "/.local/share/";
  605. path += subdir;
  606. DirectorySearch(path.c_str(), ext, &results);
  607. }
  608. // Search global data dirs
  609. if((str=getenv("XDG_DATA_DIRS")) == nullptr || str[0] == '\0')
  610. str = "/usr/local/share/:/usr/share/";
  611. const char *next{str};
  612. while((str=next) != nullptr && str[0] != '\0')
  613. {
  614. next = strchr(str, ':');
  615. std::string path = (next ? std::string(str, next++) : std::string(str));
  616. if(path.empty()) continue;
  617. if(path.back() != '/')
  618. path += '/';
  619. path += subdir;
  620. DirectorySearch(path.c_str(), ext, &results);
  621. }
  622. return results;
  623. }
  624. void SetRTPriority()
  625. {
  626. bool failed = false;
  627. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
  628. if(RTPrioLevel > 0)
  629. {
  630. struct sched_param param;
  631. /* Use the minimum real-time priority possible for now (on Linux this
  632. * should be 1 for SCHED_RR) */
  633. param.sched_priority = sched_get_priority_min(SCHED_RR);
  634. failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
  635. }
  636. #else
  637. /* Real-time priority not available */
  638. failed = (RTPrioLevel>0);
  639. #endif
  640. if(failed)
  641. ERR("Failed to set priority level for thread\n");
  642. }
  643. #endif