Avoid using ATOMIC macros
[openal-soft.git] / Alc / helpers.cpp
bloba78e3d83045427b71f0bec721872a7fdbf18a369
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.
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.
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
21 #ifdef _WIN32
22 #ifdef __MINGW32__
23 #define _WIN32_IE 0x501
24 #else
25 #define _WIN32_IE 0x400
26 #endif
27 #endif
29 #include "config.h"
31 #include <stdlib.h>
32 #include <time.h>
33 #include <errno.h>
34 #include <stdarg.h>
35 #include <ctype.h>
36 #ifdef HAVE_MALLOC_H
37 #include <malloc.h>
38 #endif
39 #ifdef HAVE_DIRENT_H
40 #include <dirent.h>
41 #endif
42 #ifdef HAVE_PROC_PIDPATH
43 #include <libproc.h>
44 #endif
46 #ifdef __FreeBSD__
47 #include <sys/types.h>
48 #include <sys/sysctl.h>
49 #endif
51 #ifndef AL_NO_UID_DEFS
52 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
53 #define INITGUID
54 #include <windows.h>
55 #ifdef HAVE_GUIDDEF_H
56 #include <guiddef.h>
57 #else
58 #include <initguid.h>
59 #endif
61 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
62 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
64 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
66 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
67 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
68 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
69 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
70 DEFINE_GUID(IID_IAudioCaptureClient, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4,0xde, 0x18,0x5c,0x39,0x5c,0xd3,0x17);
72 #ifdef HAVE_WASAPI
73 #include <wtypes.h>
74 #include <devpropdef.h>
75 #include <propkeydef.h>
76 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
77 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
78 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID, 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23,0xe0, 0xc0,0xff,0xee,0x7f,0x0e, 4 );
79 #endif
80 #endif
81 #endif /* AL_NO_UID_DEFS */
83 #ifdef HAVE_DLFCN_H
84 #include <dlfcn.h>
85 #endif
86 #ifdef HAVE_INTRIN_H
87 #include <intrin.h>
88 #endif
89 #ifdef HAVE_CPUID_H
90 #include <cpuid.h>
91 #endif
92 #ifdef HAVE_SYS_SYSCONF_H
93 #include <sys/sysconf.h>
94 #endif
95 #ifdef HAVE_FLOAT_H
96 #include <float.h>
97 #endif
98 #ifdef HAVE_IEEEFP_H
99 #include <ieeefp.h>
100 #endif
102 #ifndef _WIN32
103 #include <sys/types.h>
104 #include <sys/stat.h>
105 #include <sys/mman.h>
106 #include <fcntl.h>
107 #include <unistd.h>
108 #elif defined(_WIN32_IE)
109 #include <shlobj.h>
110 #endif
112 #include <mutex>
113 #include <vector>
114 #include <string>
115 #include <algorithm>
117 #include "alMain.h"
118 #include "alu.h"
119 #include "cpu_caps.h"
120 #include "fpu_modes.h"
121 #include "uintmap.h"
122 #include "vector.h"
123 #include "compat.h"
124 #include "threads.h"
127 #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
128 defined(_M_IX86) || defined(_M_X64))
129 typedef unsigned int reg_type;
130 static inline void get_cpuid(int f, reg_type *regs)
131 { __get_cpuid(f, &regs[0], &regs[1], &regs[2], &regs[3]); }
132 #define CAN_GET_CPUID
133 #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
134 defined(_M_IX86) || defined(_M_X64))
135 typedef int reg_type;
136 static inline void get_cpuid(int f, reg_type *regs)
137 { (__cpuid)(regs, f); }
138 #define CAN_GET_CPUID
139 #endif
141 int CPUCapFlags = 0;
143 void FillCPUCaps(int capfilter)
145 int caps = 0;
147 /* FIXME: We really should get this for all available CPUs in case different
148 * CPUs have different caps (is that possible on one machine?). */
149 #ifdef CAN_GET_CPUID
150 union {
151 reg_type regs[4];
152 char str[sizeof(reg_type[4])];
153 } cpuinf[3] = {{ { 0, 0, 0, 0 } }};
155 get_cpuid(0, cpuinf[0].regs);
156 if(cpuinf[0].regs[0] == 0)
157 ERR("Failed to get CPUID\n");
158 else
160 unsigned int maxfunc = cpuinf[0].regs[0];
161 unsigned int maxextfunc;
163 get_cpuid(0x80000000, cpuinf[0].regs);
164 maxextfunc = cpuinf[0].regs[0];
166 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
168 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
169 if(maxextfunc >= 0x80000004)
171 get_cpuid(0x80000002, cpuinf[0].regs);
172 get_cpuid(0x80000003, cpuinf[1].regs);
173 get_cpuid(0x80000004, cpuinf[2].regs);
174 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
177 if(maxfunc >= 1)
179 get_cpuid(1, cpuinf[0].regs);
180 if((cpuinf[0].regs[3]&(1<<25)))
181 caps |= CPU_CAP_SSE;
182 if((caps&CPU_CAP_SSE) && (cpuinf[0].regs[3]&(1<<26)))
183 caps |= CPU_CAP_SSE2;
184 if((caps&CPU_CAP_SSE2) && (cpuinf[0].regs[2]&(1<<0)))
185 caps |= CPU_CAP_SSE3;
186 if((caps&CPU_CAP_SSE3) && (cpuinf[0].regs[2]&(1<<19)))
187 caps |= CPU_CAP_SSE4_1;
190 #else
191 /* Assume support for whatever's supported if we can't check for it */
192 #if defined(HAVE_SSE4_1)
193 #warning "Assuming SSE 4.1 run-time support!"
194 caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
195 #elif defined(HAVE_SSE3)
196 #warning "Assuming SSE 3 run-time support!"
197 caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
198 #elif defined(HAVE_SSE2)
199 #warning "Assuming SSE 2 run-time support!"
200 caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
201 #elif defined(HAVE_SSE)
202 #warning "Assuming SSE run-time support!"
203 caps |= CPU_CAP_SSE;
204 #endif
205 #endif
206 #ifdef HAVE_NEON
207 FILE *file = fopen("/proc/cpuinfo", "rt");
208 if(!file)
209 ERR("Failed to open /proc/cpuinfo, cannot check for NEON support\n");
210 else
212 std::string features;
213 char buf[256];
215 while(fgets(buf, sizeof(buf), file) != nullptr)
217 if(strncmp(buf, "Features\t:", 10) != 0)
218 continue;
220 features = buf+10;
221 while(features.back() != '\n')
223 if(fgets(buf, sizeof(buf), file) == nullptr)
224 break;
225 features += buf;
227 break;
229 fclose(file);
230 file = nullptr;
232 if(!features.empty())
234 const char *str = features.c_str();
235 while(isspace(str[0])) ++str;
237 TRACE("Got features string:%s\n", str);
238 while((str=strstr(str, "neon")) != nullptr)
240 if(isspace(*(str-1)) && (str[4] == 0 || isspace(str[4])))
242 caps |= CPU_CAP_NEON;
243 break;
245 ++str;
249 #endif
251 TRACE("Extensions:%s%s%s%s%s%s\n",
252 ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
253 ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
254 ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
255 ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
256 ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +NEON" : " -NEON") : ""),
257 ((!capfilter) ? " -none-" : "")
259 CPUCapFlags = caps & capfilter;
263 void SetMixerFPUMode(FPUCtl *ctl)
265 #if defined(__GNUC__) && defined(HAVE_SSE)
266 if((CPUCapFlags&CPU_CAP_SSE))
268 __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
269 unsigned int sseState = ctl->sse_state;
270 sseState |= 0x8000; /* set flush-to-zero */
271 if((CPUCapFlags&CPU_CAP_SSE2))
272 sseState |= 0x0040; /* set denormals-are-zero */
273 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
276 #elif defined(HAVE___CONTROL87_2)
278 __control87_2(0, 0, &ctl->state, &ctl->sse_state);
279 _control87(_DN_FLUSH, _MCW_DN);
281 #elif defined(HAVE__CONTROLFP)
283 ctl->state = _controlfp(0, 0);
284 _controlfp(_DN_FLUSH, _MCW_DN);
285 #endif
288 void RestoreFPUMode(const FPUCtl *ctl)
290 #if defined(__GNUC__) && defined(HAVE_SSE)
291 if((CPUCapFlags&CPU_CAP_SSE))
292 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
294 #elif defined(HAVE___CONTROL87_2)
296 unsigned int mode;
297 __control87_2(ctl->state, _MCW_DN, &mode, nullptr);
298 __control87_2(ctl->sse_state, _MCW_DN, nullptr, &mode);
300 #elif defined(HAVE__CONTROLFP)
302 _controlfp(ctl->state, _MCW_DN);
303 #endif
307 #ifdef _WIN32
309 PathNamePair GetProcBinary()
311 PathNamePair ret;
313 std::vector<WCHAR> fullpath(256);
314 DWORD len;
315 while((len=GetModuleFileNameW(nullptr, fullpath.data(), fullpath.size())) == fullpath.size())
316 fullpath.resize(fullpath.size() << 1);
317 if(len == 0)
319 ERR("Failed to get process name: error %lu\n", GetLastError());
320 return ret;
323 fullpath.resize(len);
324 if(fullpath.back() != 0)
325 fullpath.push_back(0);
327 auto sep = std::find(fullpath.rbegin()+1, fullpath.rend(), '\\');
328 sep = std::find(fullpath.rbegin()+1, sep, '/');
329 if(sep != fullpath.rend())
331 *sep = 0;
332 ret.fname = wstr_to_utf8(&*sep + 1);
333 ret.path = wstr_to_utf8(fullpath.data());
335 else
336 ret.fname = wstr_to_utf8(fullpath.data());
338 TRACE("Got: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
339 return ret;
343 void *LoadLib(const char *name)
345 std::wstring wname{utf8_to_wstr(name)};
346 return LoadLibraryW(wname.c_str());
348 void CloseLib(void *handle)
349 { FreeLibrary(static_cast<HMODULE>(handle)); }
350 void *GetSymbol(void *handle, const char *name)
352 void *ret{reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), name))};
353 if(!ret) ERR("Failed to load %s\n", name);
354 return ret;
358 void al_print(const char *type, const char *func, const char *fmt, ...)
360 char str[1024];
361 va_list ap;
363 va_start(ap, fmt);
364 vsnprintf(str, sizeof(str), fmt, ap);
365 va_end(ap);
366 str[sizeof(str)-1] = 0;
368 std::wstring wstr{utf8_to_wstr(str)};
369 fprintf(LogFile, "AL lib: %s %s: %ls", type, func, wstr.c_str());
370 fflush(LogFile);
374 static inline int is_slash(int c)
375 { return (c == '\\' || c == '/'); }
377 static void DirectorySearch(const char *path, const char *ext, std::vector<std::string> *const results)
379 std::string pathstr{path};
380 pathstr += "\\*";
381 pathstr += ext;
382 TRACE("Searching %s\n", pathstr.c_str());
384 std::wstring wpath{utf8_to_wstr(pathstr.c_str())};
385 WIN32_FIND_DATAW fdata;
386 HANDLE hdl{FindFirstFileW(wpath.c_str(), &fdata)};
387 if(hdl != INVALID_HANDLE_VALUE)
389 size_t base = results->size();
390 do {
391 results->emplace_back();
392 std::string &str = results->back();
393 str = path;
394 str += '\\';
395 str += wstr_to_utf8(fdata.cFileName);
396 TRACE("Got result %s\n", str.c_str());
397 } while(FindNextFileW(hdl, &fdata));
398 FindClose(hdl);
400 std::sort(results->begin()+base, results->end());
404 std::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
406 static std::mutex search_lock;
407 std::lock_guard<std::mutex> _{search_lock};
409 /* If the path is absolute, use it directly. */
410 std::vector<std::string> results;
411 if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2]))
413 std::string path{subdir};
414 std::replace(path.begin(), path.end(), '/', '\\');
415 DirectorySearch(path.c_str(), ext, &results);
416 return results;
418 if(subdir[0] == '\\' && subdir[1] == '\\' && subdir[2] == '?' && subdir[3] == '\\')
420 DirectorySearch(subdir, ext, &results);
421 return results;
424 std::string path;
426 /* Search the app-local directory. */
427 WCHAR *cwdbuf{_wgetenv(L"ALSOFT_LOCAL_PATH")};
428 if(cwdbuf && *cwdbuf != '\0')
430 path = wstr_to_utf8(cwdbuf);
431 if(is_slash(path.back()))
432 path.pop_back();
434 else if(!(cwdbuf=_wgetcwd(nullptr, 0)))
435 path = ".";
436 else
438 path = wstr_to_utf8(cwdbuf);
439 if(is_slash(path.back()))
440 path.pop_back();
441 free(cwdbuf);
443 std::replace(path.begin(), path.end(), '/', '\\');
444 DirectorySearch(path.c_str(), ext, &results);
446 /* Search the local and global data dirs. */
447 static constexpr int ids[2]{ CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
448 for(int id : ids)
450 WCHAR buffer[MAX_PATH];
451 if(SHGetSpecialFolderPathW(nullptr, buffer, id, FALSE) == FALSE)
452 continue;
454 path = wstr_to_utf8(buffer);
455 if(!is_slash(path.back()))
456 path += '\\';
457 path += subdir;
458 std::replace(path.begin(), path.end(), '/', '\\');
460 DirectorySearch(path.c_str(), ext, &results);
463 return results;
466 void SetRTPriority(void)
468 bool failed = false;
469 if(RTPrioLevel > 0)
470 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
471 if(failed) ERR("Failed to set priority level for thread\n");
474 #else
476 PathNamePair GetProcBinary()
478 PathNamePair ret;
479 std::vector<char> pathname;
481 #ifdef __FreeBSD__
482 size_t pathlen;
483 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
484 if(sysctl(mib, 4, nullptr, &pathlen, nullptr, 0) == -1)
485 WARN("Failed to sysctl kern.proc.pathname: %s\n", strerror(errno));
486 else
488 pathname.resize(pathlen + 1);
489 sysctl(mib, 4, pathname.data(), &pathlen, nullptr, 0);
490 pathname.resize(pathlen);
492 #endif
493 #ifdef HAVE_PROC_PIDPATH
494 if(pathname.empty())
496 char procpath[PROC_PIDPATHINFO_MAXSIZE]{};
497 const pid_t pid{getpid()};
498 if(proc_pidpath(pid, procpath, sizeof(procpath)) < 1)
499 ERR("proc_pidpath(%d, ...) failed: %s\n", pid, strerror(errno));
500 else
501 pathname.insert(pathname.end(), procpath, procpath+strlen(procpath));
503 #endif
504 if(pathname.empty())
506 pathname.resize(256);
508 const char *selfname{"/proc/self/exe"};
509 ssize_t len{readlink(selfname, pathname.data(), pathname.size())};
510 if(len == -1 && errno == ENOENT)
512 selfname = "/proc/self/file";
513 len = readlink(selfname, pathname.data(), pathname.size());
515 if(len == -1 && errno == ENOENT)
517 selfname = "/proc/curproc/exe";
518 len = readlink(selfname, pathname.data(), pathname.size());
520 if(len == -1 && errno == ENOENT)
522 selfname = "/proc/curproc/file";
523 len = readlink(selfname, pathname.data(), pathname.size());
526 while(len > 0 && (size_t)len == pathname.size())
528 pathname.resize(pathname.size() << 1);
529 len = readlink(selfname, pathname.data(), pathname.size());
531 if(len <= 0)
533 WARN("Failed to readlink %s: %s\n", selfname, strerror(errno));
534 return ret;
537 pathname.resize(len);
539 while(!pathname.empty() && pathname.back() == 0)
540 pathname.pop_back();
542 auto sep = std::find(pathname.crbegin(), pathname.crend(), '/');
543 if(sep != pathname.crend())
545 ret.path = std::string(pathname.cbegin(), sep.base()-1);
546 ret.fname = std::string(sep.base(), pathname.cend());
548 else
549 ret.fname = std::string(pathname.cbegin(), pathname.cend());
551 TRACE("Got: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
552 return ret;
556 #ifdef HAVE_DLFCN_H
558 void *LoadLib(const char *name)
560 dlerror();
561 void *handle{dlopen(name, RTLD_NOW)};
562 const char *err{dlerror()};
563 if(err) handle = nullptr;
564 return handle;
566 void CloseLib(void *handle)
567 { dlclose(handle); }
568 void *GetSymbol(void *handle, const char *name)
570 dlerror();
571 void *sym{dlsym(handle, name)};
572 const char *err{dlerror()};
573 if(err)
575 WARN("Failed to load %s: %s\n", name, err);
576 sym = nullptr;
578 return sym;
581 #endif /* HAVE_DLFCN_H */
583 void al_print(const char *type, const char *func, const char *fmt, ...)
585 va_list ap;
587 va_start(ap, fmt);
588 fprintf(LogFile, "AL lib: %s %s: ", type, func);
589 vfprintf(LogFile, fmt, ap);
590 va_end(ap);
592 fflush(LogFile);
596 static void DirectorySearch(const char *path, const char *ext, std::vector<std::string> *const results)
598 TRACE("Searching %s for *%s\n", path, ext);
599 DIR *dir{opendir(path)};
600 if(dir != nullptr)
602 const size_t extlen = strlen(ext);
603 size_t base = results->size();
605 struct dirent *dirent;
606 while((dirent=readdir(dir)) != nullptr)
608 if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
609 continue;
611 size_t len{strlen(dirent->d_name)};
612 if(len <= extlen) continue;
613 if(strcasecmp(dirent->d_name+len-extlen, ext) != 0)
614 continue;
616 results->emplace_back();
617 std::string &str = results->back();
618 str = path;
619 if(str.back() != '/')
620 str.push_back('/');
621 str += dirent->d_name;
622 TRACE("Got result %s\n", str.c_str());
624 closedir(dir);
626 std::sort(results->begin()+base, results->end());
630 std::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
632 static std::mutex search_lock;
633 std::lock_guard<std::mutex> _{search_lock};
635 std::vector<std::string> results;
636 if(subdir[0] == '/')
638 DirectorySearch(subdir, ext, &results);
639 return results;
642 /* Search the app-local directory. */
643 const char *str{getenv("ALSOFT_LOCAL_PATH")};
644 if(str && *str != '\0')
645 DirectorySearch(str, ext, &results);
646 else
648 std::vector<char> cwdbuf(256);
649 while(!getcwd(cwdbuf.data(), cwdbuf.size()))
651 if(errno != ERANGE)
653 cwdbuf.clear();
654 break;
656 cwdbuf.resize(cwdbuf.size() << 1);
658 if(cwdbuf.empty())
659 DirectorySearch(".", ext, &results);
660 else
662 DirectorySearch(cwdbuf.data(), ext, &results);
663 cwdbuf.clear();
667 // Search local data dir
668 if((str=getenv("XDG_DATA_HOME")) != nullptr && str[0] != '\0')
670 std::string path{str};
671 if(path.back() != '/')
672 path += '/';
673 path += subdir;
674 DirectorySearch(path.c_str(), ext, &results);
676 else if((str=getenv("HOME")) != nullptr && str[0] != '\0')
678 std::string path{str};
679 if(path.back() == '/')
680 path.pop_back();
681 path += "/.local/share/";
682 path += subdir;
683 DirectorySearch(path.c_str(), ext, &results);
686 // Search global data dirs
687 if((str=getenv("XDG_DATA_DIRS")) == nullptr || str[0] == '\0')
688 str = "/usr/local/share/:/usr/share/";
690 const char *next{str};
691 while((str=next) != nullptr && str[0] != '\0')
693 next = strchr(str, ':');
695 std::string path = (next ? std::string(str, next++) : std::string(str));
696 if(path.empty()) continue;
698 if(path.back() != '/')
699 path += '/';
700 path += subdir;
702 DirectorySearch(path.c_str(), ext, &results);
705 return results;
708 void SetRTPriority(void)
710 bool failed = false;
711 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
712 if(RTPrioLevel > 0)
714 struct sched_param param;
715 /* Use the minimum real-time priority possible for now (on Linux this
716 * should be 1 for SCHED_RR) */
717 param.sched_priority = sched_get_priority_min(SCHED_RR);
718 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
720 #else
721 /* Real-time priority not available */
722 failed = (RTPrioLevel>0);
723 #endif
724 if(failed)
725 ERR("Failed to set priority level for thread\n");
728 #endif