helpers.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 <stdlib.h>
  29. #include <time.h>
  30. #include <errno.h>
  31. #include <stdarg.h>
  32. #include <ctype.h>
  33. #ifdef HAVE_MALLOC_H
  34. #include <malloc.h>
  35. #endif
  36. #ifdef HAVE_DIRENT_H
  37. #include <dirent.h>
  38. #endif
  39. #ifndef AL_NO_UID_DEFS
  40. #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
  41. #define INITGUID
  42. #include <windows.h>
  43. #ifdef HAVE_GUIDDEF_H
  44. #include <guiddef.h>
  45. #else
  46. #include <initguid.h>
  47. #endif
  48. DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
  49. DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
  50. DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
  51. DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
  52. DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
  53. DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
  54. DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
  55. DEFINE_GUID(IID_IAudioCaptureClient, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4,0xde, 0x18,0x5c,0x39,0x5c,0xd3,0x17);
  56. #ifdef HAVE_MMDEVAPI
  57. #include <wtypes.h>
  58. #include <devpropdef.h>
  59. #include <propkeydef.h>
  60. DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
  61. DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
  62. DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID, 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23,0xe0, 0xc0,0xff,0xee,0x7f,0x0e, 4 );
  63. #endif
  64. #endif
  65. #endif /* AL_NO_UID_DEFS */
  66. #ifdef HAVE_DLFCN_H
  67. #include <dlfcn.h>
  68. #endif
  69. #ifdef HAVE_INTRIN_H
  70. #include <intrin.h>
  71. #endif
  72. #ifdef HAVE_CPUID_H
  73. #include <cpuid.h>
  74. #endif
  75. #ifdef HAVE_SYS_SYSCONF_H
  76. #include <sys/sysconf.h>
  77. #endif
  78. #ifdef HAVE_FLOAT_H
  79. #include <float.h>
  80. #endif
  81. #ifdef HAVE_IEEEFP_H
  82. #include <ieeefp.h>
  83. #endif
  84. #ifndef _WIN32
  85. #include <sys/types.h>
  86. #include <sys/stat.h>
  87. #include <sys/mman.h>
  88. #include <fcntl.h>
  89. #include <unistd.h>
  90. #elif defined(_WIN32_IE)
  91. #include <shlobj.h>
  92. #endif
  93. #include "alMain.h"
  94. #include "alu.h"
  95. #include "atomic.h"
  96. #include "uintmap.h"
  97. #include "vector.h"
  98. #include "alstring.h"
  99. #include "compat.h"
  100. #include "threads.h"
  101. extern inline ALuint NextPowerOf2(ALuint value);
  102. extern inline ALint fastf2i(ALfloat f);
  103. extern inline ALuint fastf2u(ALfloat f);
  104. ALuint CPUCapFlags = 0;
  105. void FillCPUCaps(ALuint capfilter)
  106. {
  107. ALuint caps = 0;
  108. /* FIXME: We really should get this for all available CPUs in case different
  109. * CPUs have different caps (is that possible on one machine?). */
  110. #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
  111. defined(_M_IX86) || defined(_M_X64))
  112. union {
  113. unsigned int regs[4];
  114. char str[sizeof(unsigned int[4])];
  115. } cpuinf[3];
  116. if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  117. ERR("Failed to get CPUID\n");
  118. else
  119. {
  120. unsigned int maxfunc = cpuinf[0].regs[0];
  121. unsigned int maxextfunc = 0;
  122. if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  123. maxextfunc = cpuinf[0].regs[0];
  124. TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
  125. TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
  126. if(maxextfunc >= 0x80000004 &&
  127. __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
  128. __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
  129. __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
  130. TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
  131. if(maxfunc >= 1 &&
  132. __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  133. {
  134. if((cpuinf[0].regs[3]&(1<<25)))
  135. {
  136. caps |= CPU_CAP_SSE;
  137. if((cpuinf[0].regs[3]&(1<<26)))
  138. {
  139. caps |= CPU_CAP_SSE2;
  140. if((cpuinf[0].regs[2]&(1<<0)))
  141. {
  142. caps |= CPU_CAP_SSE3;
  143. if((cpuinf[0].regs[2]&(1<<19)))
  144. caps |= CPU_CAP_SSE4_1;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
  151. defined(_M_IX86) || defined(_M_X64))
  152. union {
  153. int regs[4];
  154. char str[sizeof(int[4])];
  155. } cpuinf[3];
  156. (__cpuid)(cpuinf[0].regs, 0);
  157. if(cpuinf[0].regs[0] == 0)
  158. ERR("Failed to get CPUID\n");
  159. else
  160. {
  161. unsigned int maxfunc = cpuinf[0].regs[0];
  162. unsigned int maxextfunc;
  163. (__cpuid)(cpuinf[0].regs, 0x80000000);
  164. maxextfunc = cpuinf[0].regs[0];
  165. TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
  166. TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
  167. if(maxextfunc >= 0x80000004)
  168. {
  169. (__cpuid)(cpuinf[0].regs, 0x80000002);
  170. (__cpuid)(cpuinf[1].regs, 0x80000003);
  171. (__cpuid)(cpuinf[2].regs, 0x80000004);
  172. TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
  173. }
  174. if(maxfunc >= 1)
  175. {
  176. (__cpuid)(cpuinf[0].regs, 1);
  177. if((cpuinf[0].regs[3]&(1<<25)))
  178. {
  179. caps |= CPU_CAP_SSE;
  180. if((cpuinf[0].regs[3]&(1<<26)))
  181. {
  182. caps |= CPU_CAP_SSE2;
  183. if((cpuinf[0].regs[2]&(1<<0)))
  184. {
  185. caps |= CPU_CAP_SSE3;
  186. if((cpuinf[0].regs[2]&(1<<19)))
  187. caps |= CPU_CAP_SSE4_1;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. #else
  194. /* Assume support for whatever's supported if we can't check for it */
  195. #if defined(HAVE_SSE4_1)
  196. #warning "Assuming SSE 4.1 run-time support!"
  197. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
  198. #elif defined(HAVE_SSE3)
  199. #warning "Assuming SSE 3 run-time support!"
  200. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
  201. #elif defined(HAVE_SSE2)
  202. #warning "Assuming SSE 2 run-time support!"
  203. caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
  204. #elif defined(HAVE_SSE)
  205. #warning "Assuming SSE run-time support!"
  206. caps |= CPU_CAP_SSE;
  207. #endif
  208. #endif
  209. #ifdef HAVE_NEON
  210. FILE *file = fopen("/proc/cpuinfo", "rt");
  211. if(!file)
  212. ERR("Failed to open /proc/cpuinfo, cannot check for NEON support\n");
  213. else
  214. {
  215. char buf[256];
  216. while(fgets(buf, sizeof(buf), file) != NULL)
  217. {
  218. char *str;
  219. if(strncmp(buf, "Features\t:", 10) != 0)
  220. continue;
  221. TRACE("Got features string:%s\n", buf+10);
  222. str = buf;
  223. while((str=strstr(str, "neon")) != NULL)
  224. {
  225. if(isspace(*(str-1)) && (str[4] == 0 || isspace(str[4])))
  226. {
  227. caps |= CPU_CAP_NEON;
  228. break;
  229. }
  230. str++;
  231. }
  232. break;
  233. }
  234. fclose(file);
  235. file = NULL;
  236. }
  237. #endif
  238. TRACE("Extensions:%s%s%s%s%s%s\n",
  239. ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
  240. ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
  241. ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
  242. ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
  243. ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +Neon" : " -Neon") : ""),
  244. ((!capfilter) ? " -none-" : "")
  245. );
  246. CPUCapFlags = caps & capfilter;
  247. }
  248. void SetMixerFPUMode(FPUCtl *ctl)
  249. {
  250. #ifdef HAVE_FENV_H
  251. fegetenv(STATIC_CAST(fenv_t, ctl));
  252. #if defined(__GNUC__) && defined(HAVE_SSE)
  253. /* FIXME: Some fegetenv implementations can get the SSE environment too?
  254. * How to tell when it does? */
  255. if((CPUCapFlags&CPU_CAP_SSE))
  256. __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
  257. #endif
  258. #ifdef FE_TOWARDZERO
  259. fesetround(FE_TOWARDZERO);
  260. #endif
  261. #if defined(__GNUC__) && defined(HAVE_SSE)
  262. if((CPUCapFlags&CPU_CAP_SSE))
  263. {
  264. int sseState = ctl->sse_state;
  265. sseState |= 0x6000; /* set round-to-zero */
  266. sseState |= 0x8000; /* set flush-to-zero */
  267. if((CPUCapFlags&CPU_CAP_SSE2))
  268. sseState |= 0x0040; /* set denormals-are-zero */
  269. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
  270. }
  271. #endif
  272. #elif defined(HAVE___CONTROL87_2)
  273. int mode;
  274. __control87_2(0, 0, &ctl->state, NULL);
  275. __control87_2(_RC_CHOP, _MCW_RC, &mode, NULL);
  276. #ifdef HAVE_SSE
  277. if((CPUCapFlags&CPU_CAP_SSE))
  278. {
  279. __control87_2(0, 0, NULL, &ctl->sse_state);
  280. __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
  281. }
  282. #endif
  283. #elif defined(HAVE__CONTROLFP)
  284. ctl->state = _controlfp(0, 0);
  285. (void)_controlfp(_RC_CHOP, _MCW_RC);
  286. #endif
  287. }
  288. void RestoreFPUMode(const FPUCtl *ctl)
  289. {
  290. #ifdef HAVE_FENV_H
  291. fesetenv(STATIC_CAST(fenv_t, ctl));
  292. #if defined(__GNUC__) && defined(HAVE_SSE)
  293. if((CPUCapFlags&CPU_CAP_SSE))
  294. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
  295. #endif
  296. #elif defined(HAVE___CONTROL87_2)
  297. int mode;
  298. __control87_2(ctl->state, _MCW_RC, &mode, NULL);
  299. #ifdef HAVE_SSE
  300. if((CPUCapFlags&CPU_CAP_SSE))
  301. __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
  302. #endif
  303. #elif defined(HAVE__CONTROLFP)
  304. _controlfp(ctl->state, _MCW_RC);
  305. #endif
  306. }
  307. static int StringSortCompare(const void *str1, const void *str2)
  308. {
  309. return al_string_cmp(*(const_al_string*)str1, *(const_al_string*)str2);
  310. }
  311. #ifdef _WIN32
  312. static WCHAR *strrchrW(WCHAR *str, WCHAR ch)
  313. {
  314. WCHAR *ret = NULL;
  315. while(*str)
  316. {
  317. if(*str == ch)
  318. ret = str;
  319. ++str;
  320. }
  321. return ret;
  322. }
  323. al_string GetProcPath(void)
  324. {
  325. al_string ret = AL_STRING_INIT_STATIC();
  326. WCHAR *pathname, *sep;
  327. DWORD pathlen;
  328. DWORD len;
  329. pathlen = 256;
  330. pathname = malloc(pathlen * sizeof(pathname[0]));
  331. while(pathlen > 0 && (len=GetModuleFileNameW(NULL, pathname, pathlen)) == pathlen)
  332. {
  333. free(pathname);
  334. pathlen <<= 1;
  335. pathname = malloc(pathlen * sizeof(pathname[0]));
  336. }
  337. if(len == 0)
  338. {
  339. free(pathname);
  340. ERR("Failed to get process name: error %lu\n", GetLastError());
  341. return ret;
  342. }
  343. pathname[len] = 0;
  344. if((sep = strrchrW(pathname, '\\')))
  345. {
  346. WCHAR *sep2 = strrchrW(pathname, '/');
  347. if(sep2) *sep2 = 0;
  348. else *sep = 0;
  349. }
  350. else if((sep = strrchrW(pathname, '/')))
  351. *sep = 0;
  352. al_string_copy_wcstr(&ret, pathname);
  353. free(pathname);
  354. TRACE("Got: %s\n", al_string_get_cstr(ret));
  355. return ret;
  356. }
  357. static WCHAR *FromUTF8(const char *str)
  358. {
  359. WCHAR *out = NULL;
  360. int len;
  361. if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
  362. {
  363. out = calloc(sizeof(WCHAR), len);
  364. MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
  365. }
  366. return out;
  367. }
  368. void *LoadLib(const char *name)
  369. {
  370. HANDLE hdl = NULL;
  371. WCHAR *wname;
  372. wname = FromUTF8(name);
  373. if(!wname)
  374. ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
  375. else
  376. {
  377. hdl = LoadLibraryW(wname);
  378. free(wname);
  379. }
  380. return hdl;
  381. }
  382. void CloseLib(void *handle)
  383. { FreeLibrary((HANDLE)handle); }
  384. void *GetSymbol(void *handle, const char *name)
  385. {
  386. void *ret;
  387. ret = (void*)GetProcAddress((HANDLE)handle, name);
  388. if(ret == NULL)
  389. ERR("Failed to load %s\n", name);
  390. return ret;
  391. }
  392. WCHAR *strdupW(const WCHAR *str)
  393. {
  394. const WCHAR *n;
  395. WCHAR *ret;
  396. size_t len;
  397. n = str;
  398. while(*n) n++;
  399. len = n - str;
  400. ret = calloc(sizeof(WCHAR), len+1);
  401. if(ret != NULL)
  402. memcpy(ret, str, sizeof(WCHAR)*len);
  403. return ret;
  404. }
  405. FILE *al_fopen(const char *fname, const char *mode)
  406. {
  407. WCHAR *wname=NULL, *wmode=NULL;
  408. FILE *file = NULL;
  409. wname = FromUTF8(fname);
  410. wmode = FromUTF8(mode);
  411. if(!wname)
  412. ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
  413. else if(!wmode)
  414. ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
  415. else
  416. file = _wfopen(wname, wmode);
  417. free(wname);
  418. free(wmode);
  419. return file;
  420. }
  421. void al_print(const char *type, const char *func, const char *fmt, ...)
  422. {
  423. char str[1024];
  424. WCHAR *wstr;
  425. va_list ap;
  426. va_start(ap, fmt);
  427. vsnprintf(str, sizeof(str), fmt, ap);
  428. va_end(ap);
  429. str[sizeof(str)-1] = 0;
  430. wstr = FromUTF8(str);
  431. if(!wstr)
  432. fprintf(LogFile, "AL lib: %s %s: <UTF-8 error> %s", type, func, str);
  433. else
  434. {
  435. fprintf(LogFile, "AL lib: %s %s: %ls", type, func, wstr);
  436. free(wstr);
  437. wstr = NULL;
  438. }
  439. fflush(LogFile);
  440. }
  441. static inline int is_slash(int c)
  442. { return (c == '\\' || c == '/'); }
  443. static void DirectorySearch(const char *path, const char *ext, vector_al_string *results)
  444. {
  445. al_string pathstr = AL_STRING_INIT_STATIC();
  446. WIN32_FIND_DATAW fdata;
  447. WCHAR *wpath;
  448. HANDLE hdl;
  449. al_string_copy_cstr(&pathstr, path);
  450. al_string_append_cstr(&pathstr, "\\*");
  451. al_string_append_cstr(&pathstr, ext);
  452. TRACE("Searching %s\n", al_string_get_cstr(pathstr));
  453. wpath = FromUTF8(al_string_get_cstr(pathstr));
  454. hdl = FindFirstFileW(wpath, &fdata);
  455. if(hdl != INVALID_HANDLE_VALUE)
  456. {
  457. size_t base = VECTOR_SIZE(*results);
  458. do {
  459. al_string str = AL_STRING_INIT_STATIC();
  460. al_string_copy_cstr(&str, path);
  461. al_string_append_char(&str, '\\');
  462. al_string_append_wcstr(&str, fdata.cFileName);
  463. TRACE("Got result %s\n", al_string_get_cstr(str));
  464. VECTOR_PUSH_BACK(*results, str);
  465. } while(FindNextFileW(hdl, &fdata));
  466. FindClose(hdl);
  467. if(VECTOR_SIZE(*results) > base)
  468. qsort(VECTOR_BEGIN(*results)+base, VECTOR_SIZE(*results)-base,
  469. sizeof(VECTOR_FRONT(*results)), StringSortCompare);
  470. }
  471. free(wpath);
  472. al_string_deinit(&pathstr);
  473. }
  474. vector_al_string SearchDataFiles(const char *ext, const char *subdir)
  475. {
  476. static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
  477. static RefCount search_lock;
  478. vector_al_string results = VECTOR_INIT_STATIC();
  479. size_t i;
  480. while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
  481. althrd_yield();
  482. /* If the path is absolute, use it directly. */
  483. if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2]))
  484. {
  485. al_string path = AL_STRING_INIT_STATIC();
  486. al_string_copy_cstr(&path, subdir);
  487. #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
  488. VECTOR_FOR_EACH(char, path, FIX_SLASH);
  489. #undef FIX_SLASH
  490. DirectorySearch(al_string_get_cstr(path), ext, &results);
  491. al_string_deinit(&path);
  492. }
  493. else if(subdir[0] == '\\' && subdir[1] == '\\' && subdir[2] == '?' && subdir[3] == '\\')
  494. DirectorySearch(subdir, ext, &results);
  495. else
  496. {
  497. al_string path = AL_STRING_INIT_STATIC();
  498. WCHAR *cwdbuf;
  499. /* Search the app-local directory. */
  500. if((cwdbuf=_wgetenv(L"ALSOFT_LOCAL_PATH")) && *cwdbuf != '\0')
  501. {
  502. al_string_copy_wcstr(&path, cwdbuf);
  503. if(is_slash(VECTOR_BACK(path)))
  504. {
  505. VECTOR_POP_BACK(path);
  506. *VECTOR_END(path) = 0;
  507. }
  508. }
  509. else if(!(cwdbuf=_wgetcwd(NULL, 0)))
  510. al_string_copy_cstr(&path, ".");
  511. else
  512. {
  513. al_string_copy_wcstr(&path, cwdbuf);
  514. if(is_slash(VECTOR_BACK(path)))
  515. {
  516. VECTOR_POP_BACK(path);
  517. *VECTOR_END(path) = 0;
  518. }
  519. free(cwdbuf);
  520. }
  521. #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
  522. VECTOR_FOR_EACH(char, path, FIX_SLASH);
  523. #undef FIX_SLASH
  524. DirectorySearch(al_string_get_cstr(path), ext, &results);
  525. /* Search the local and global data dirs. */
  526. for(i = 0;i < COUNTOF(ids);i++)
  527. {
  528. WCHAR buffer[PATH_MAX];
  529. if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) != FALSE)
  530. {
  531. al_string_copy_wcstr(&path, buffer);
  532. if(!is_slash(VECTOR_BACK(path)))
  533. al_string_append_char(&path, '\\');
  534. al_string_append_cstr(&path, subdir);
  535. #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
  536. VECTOR_FOR_EACH(char, path, FIX_SLASH);
  537. #undef FIX_SLASH
  538. DirectorySearch(al_string_get_cstr(path), ext, &results);
  539. }
  540. }
  541. al_string_deinit(&path);
  542. }
  543. ATOMIC_STORE(&search_lock, 0);
  544. return results;
  545. }
  546. struct FileMapping MapFileToMem(const char *fname)
  547. {
  548. struct FileMapping ret = { NULL, NULL, NULL, 0 };
  549. MEMORY_BASIC_INFORMATION meminfo;
  550. HANDLE file, fmap;
  551. WCHAR *wname;
  552. void *ptr;
  553. wname = FromUTF8(fname);
  554. file = CreateFileW(wname, GENERIC_READ, FILE_SHARE_READ, NULL,
  555. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  556. if(file == INVALID_HANDLE_VALUE)
  557. {
  558. ERR("Failed to open %s: %lu\n", fname, GetLastError());
  559. free(wname);
  560. return ret;
  561. }
  562. free(wname);
  563. wname = NULL;
  564. fmap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
  565. if(!fmap)
  566. {
  567. ERR("Failed to create map for %s: %lu\n", fname, GetLastError());
  568. CloseHandle(file);
  569. return ret;
  570. }
  571. ptr = MapViewOfFile(fmap, FILE_MAP_READ, 0, 0, 0);
  572. if(!ptr)
  573. {
  574. ERR("Failed to map %s: %lu\n", fname, GetLastError());
  575. CloseHandle(fmap);
  576. CloseHandle(file);
  577. return ret;
  578. }
  579. if(VirtualQuery(ptr, &meminfo, sizeof(meminfo)) != sizeof(meminfo))
  580. {
  581. ERR("Failed to get map size for %s: %lu\n", fname, GetLastError());
  582. UnmapViewOfFile(ptr);
  583. CloseHandle(fmap);
  584. CloseHandle(file);
  585. return ret;
  586. }
  587. ret.file = file;
  588. ret.fmap = fmap;
  589. ret.ptr = ptr;
  590. ret.len = meminfo.RegionSize;
  591. return ret;
  592. }
  593. void UnmapFileMem(const struct FileMapping *mapping)
  594. {
  595. UnmapViewOfFile(mapping->ptr);
  596. CloseHandle(mapping->fmap);
  597. CloseHandle(mapping->file);
  598. }
  599. #else
  600. al_string GetProcPath(void)
  601. {
  602. al_string ret = AL_STRING_INIT_STATIC();
  603. const char *fname;
  604. char *pathname, *sep;
  605. size_t pathlen;
  606. ssize_t len;
  607. pathlen = 256;
  608. pathname = malloc(pathlen);
  609. fname = "/proc/self/exe";
  610. len = readlink(fname, pathname, pathlen);
  611. if(len == -1 && errno == ENOENT)
  612. {
  613. fname = "/proc/self/file";
  614. len = readlink(fname, pathname, pathlen);
  615. }
  616. while(len > 0 && (size_t)len == pathlen)
  617. {
  618. free(pathname);
  619. pathlen <<= 1;
  620. pathname = malloc(pathlen);
  621. len = readlink(fname, pathname, pathlen);
  622. }
  623. if(len <= 0)
  624. {
  625. free(pathname);
  626. WARN("Failed to readlink %s: %s\n", fname, strerror(errno));
  627. return ret;
  628. }
  629. pathname[len] = 0;
  630. sep = strrchr(pathname, '/');
  631. if(sep)
  632. al_string_copy_range(&ret, pathname, sep);
  633. else
  634. al_string_copy_cstr(&ret, pathname);
  635. free(pathname);
  636. TRACE("Got: %s\n", al_string_get_cstr(ret));
  637. return ret;
  638. }
  639. #ifdef HAVE_DLFCN_H
  640. void *LoadLib(const char *name)
  641. {
  642. const char *err;
  643. void *handle;
  644. dlerror();
  645. handle = dlopen(name, RTLD_NOW);
  646. if((err=dlerror()) != NULL)
  647. handle = NULL;
  648. return handle;
  649. }
  650. void CloseLib(void *handle)
  651. { dlclose(handle); }
  652. void *GetSymbol(void *handle, const char *name)
  653. {
  654. const char *err;
  655. void *sym;
  656. dlerror();
  657. sym = dlsym(handle, name);
  658. if((err=dlerror()) != NULL)
  659. {
  660. WARN("Failed to load %s: %s\n", name, err);
  661. sym = NULL;
  662. }
  663. return sym;
  664. }
  665. #endif /* HAVE_DLFCN_H */
  666. void al_print(const char *type, const char *func, const char *fmt, ...)
  667. {
  668. va_list ap;
  669. va_start(ap, fmt);
  670. fprintf(LogFile, "AL lib: %s %s: ", type, func);
  671. vfprintf(LogFile, fmt, ap);
  672. va_end(ap);
  673. fflush(LogFile);
  674. }
  675. static void DirectorySearch(const char *path, const char *ext, vector_al_string *results)
  676. {
  677. size_t extlen = strlen(ext);
  678. DIR *dir;
  679. TRACE("Searching %s for *%s\n", path, ext);
  680. dir = opendir(path);
  681. if(dir != NULL)
  682. {
  683. size_t base = VECTOR_SIZE(*results);
  684. struct dirent *dirent;
  685. while((dirent=readdir(dir)) != NULL)
  686. {
  687. al_string str;
  688. size_t len;
  689. if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
  690. continue;
  691. len = strlen(dirent->d_name);
  692. if(!(len > extlen))
  693. continue;
  694. if(strcasecmp(dirent->d_name+len-extlen, ext) != 0)
  695. continue;
  696. AL_STRING_INIT(str);
  697. al_string_copy_cstr(&str, path);
  698. if(VECTOR_BACK(str) != '/')
  699. al_string_append_char(&str, '/');
  700. al_string_append_cstr(&str, dirent->d_name);
  701. TRACE("Got result %s\n", al_string_get_cstr(str));
  702. VECTOR_PUSH_BACK(*results, str);
  703. }
  704. closedir(dir);
  705. if(VECTOR_SIZE(*results) > base)
  706. qsort(VECTOR_BEGIN(*results)+base, VECTOR_SIZE(*results)-base,
  707. sizeof(VECTOR_FRONT(*results)), StringSortCompare);
  708. }
  709. }
  710. vector_al_string SearchDataFiles(const char *ext, const char *subdir)
  711. {
  712. static RefCount search_lock;
  713. vector_al_string results = VECTOR_INIT_STATIC();
  714. while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
  715. althrd_yield();
  716. if(subdir[0] == '/')
  717. DirectorySearch(subdir, ext, &results);
  718. else
  719. {
  720. al_string path = AL_STRING_INIT_STATIC();
  721. const char *str, *next;
  722. char cwdbuf[PATH_MAX];
  723. /* Search the app-local directory. */
  724. if((str=getenv("ALSOFT_LOCAL_PATH")) && *str != '\0')
  725. DirectorySearch(str, ext, &results);
  726. else if(getcwd(cwdbuf, sizeof(cwdbuf)))
  727. DirectorySearch(cwdbuf, ext, &results);
  728. else
  729. DirectorySearch(".", ext, &results);
  730. // Search local data dir
  731. if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
  732. {
  733. al_string_copy_cstr(&path, str);
  734. if(VECTOR_BACK(path) != '/')
  735. al_string_append_char(&path, '/');
  736. al_string_append_cstr(&path, subdir);
  737. DirectorySearch(al_string_get_cstr(path), ext, &results);
  738. }
  739. else if((str=getenv("HOME")) != NULL && str[0] != '\0')
  740. {
  741. al_string_copy_cstr(&path, str);
  742. if(VECTOR_BACK(path) == '/')
  743. {
  744. VECTOR_POP_BACK(path);
  745. *VECTOR_END(path) = 0;
  746. }
  747. al_string_append_cstr(&path, "/.local/share/");
  748. al_string_append_cstr(&path, subdir);
  749. DirectorySearch(al_string_get_cstr(path), ext, &results);
  750. }
  751. // Search global data dirs
  752. if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
  753. str = "/usr/local/share/:/usr/share/";
  754. next = str;
  755. while((str=next) != NULL && str[0] != '\0')
  756. {
  757. next = strchr(str, ':');
  758. if(!next)
  759. al_string_copy_cstr(&path, str);
  760. else
  761. {
  762. al_string_copy_range(&path, str, next);
  763. ++next;
  764. }
  765. if(!al_string_empty(path))
  766. {
  767. if(VECTOR_BACK(path) != '/')
  768. al_string_append_char(&path, '/');
  769. al_string_append_cstr(&path, subdir);
  770. DirectorySearch(al_string_get_cstr(path), ext, &results);
  771. }
  772. }
  773. al_string_deinit(&path);
  774. }
  775. ATOMIC_STORE(&search_lock, 0);
  776. return results;
  777. }
  778. struct FileMapping MapFileToMem(const char *fname)
  779. {
  780. struct FileMapping ret = { -1, NULL, 0 };
  781. struct stat sbuf;
  782. void *ptr;
  783. int fd;
  784. fd = open(fname, O_RDONLY, 0);
  785. if(fd == -1)
  786. {
  787. ERR("Failed to open %s: (%d) %s\n", fname, errno, strerror(errno));
  788. return ret;
  789. }
  790. if(fstat(fd, &sbuf) == -1)
  791. {
  792. ERR("Failed to stat %s: (%d) %s\n", fname, errno, strerror(errno));
  793. close(fd);
  794. return ret;
  795. }
  796. ptr = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  797. if(ptr == MAP_FAILED)
  798. {
  799. ERR("Failed to map %s: (%d) %s\n", fname, errno, strerror(errno));
  800. close(fd);
  801. return ret;
  802. }
  803. ret.fd = fd;
  804. ret.ptr = ptr;
  805. ret.len = sbuf.st_size;
  806. return ret;
  807. }
  808. void UnmapFileMem(const struct FileMapping *mapping)
  809. {
  810. munmap(mapping->ptr, mapping->len);
  811. close(mapping->fd);
  812. }
  813. #endif
  814. void SetRTPriority(void)
  815. {
  816. ALboolean failed = AL_FALSE;
  817. #ifdef _WIN32
  818. if(RTPrioLevel > 0)
  819. failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  820. #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
  821. if(RTPrioLevel > 0)
  822. {
  823. struct sched_param param;
  824. /* Use the minimum real-time priority possible for now (on Linux this
  825. * should be 1 for SCHED_RR) */
  826. param.sched_priority = sched_get_priority_min(SCHED_RR);
  827. failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
  828. }
  829. #else
  830. /* Real-time priority not available */
  831. failed = (RTPrioLevel>0);
  832. #endif
  833. if(failed)
  834. ERR("Failed to set priority level for thread\n");
  835. }
  836. extern inline void al_string_deinit(al_string *str);
  837. extern inline size_t al_string_length(const_al_string str);
  838. extern inline ALboolean al_string_empty(const_al_string str);
  839. extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
  840. void al_string_clear(al_string *str)
  841. {
  842. if(!al_string_empty(*str))
  843. {
  844. /* Reserve one more character than the total size of the string. This
  845. * is to ensure we have space to add a null terminator in the string
  846. * data so it can be used as a C-style string.
  847. */
  848. VECTOR_RESIZE(*str, 0, 1);
  849. VECTOR_ELEM(*str, 0) = 0;
  850. }
  851. }
  852. static inline int al_string_compare(const al_string_char_type *str1, size_t str1len,
  853. const al_string_char_type *str2, size_t str2len)
  854. {
  855. size_t complen = (str1len < str2len) ? str1len : str2len;
  856. int ret = memcmp(str1, str2, complen);
  857. if(ret == 0)
  858. {
  859. if(str1len > str2len) return 1;
  860. if(str1len < str2len) return -1;
  861. }
  862. return ret;
  863. }
  864. int al_string_cmp(const_al_string str1, const_al_string str2)
  865. {
  866. return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
  867. &VECTOR_FRONT(str2), al_string_length(str2));
  868. }
  869. int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
  870. {
  871. return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
  872. str2, strlen(str2));
  873. }
  874. void al_string_copy(al_string *str, const_al_string from)
  875. {
  876. size_t len = al_string_length(from);
  877. size_t i;
  878. VECTOR_RESIZE(*str, len, len+1);
  879. for(i = 0;i < len;i++)
  880. VECTOR_ELEM(*str, i) = VECTOR_ELEM(from, i);
  881. VECTOR_ELEM(*str, i) = 0;
  882. }
  883. void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
  884. {
  885. size_t len = strlen(from);
  886. size_t i;
  887. VECTOR_RESIZE(*str, len, len+1);
  888. for(i = 0;i < len;i++)
  889. VECTOR_ELEM(*str, i) = from[i];
  890. VECTOR_ELEM(*str, i) = 0;
  891. }
  892. void al_string_copy_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
  893. {
  894. size_t len = to - from;
  895. size_t i;
  896. VECTOR_RESIZE(*str, len, len+1);
  897. for(i = 0;i < len;i++)
  898. VECTOR_ELEM(*str, i) = from[i];
  899. VECTOR_ELEM(*str, i) = 0;
  900. }
  901. void al_string_append_char(al_string *str, const al_string_char_type c)
  902. {
  903. size_t len = al_string_length(*str);
  904. VECTOR_RESIZE(*str, len, len+2);
  905. VECTOR_PUSH_BACK(*str, c);
  906. VECTOR_ELEM(*str, len+1) = 0;
  907. }
  908. void al_string_append_cstr(al_string *str, const al_string_char_type *from)
  909. {
  910. size_t len = strlen(from);
  911. if(len != 0)
  912. {
  913. size_t base = al_string_length(*str);
  914. size_t i;
  915. VECTOR_RESIZE(*str, base+len, base+len+1);
  916. for(i = 0;i < len;i++)
  917. VECTOR_ELEM(*str, base+i) = from[i];
  918. VECTOR_ELEM(*str, base+i) = 0;
  919. }
  920. }
  921. void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
  922. {
  923. size_t len = to - from;
  924. if(len != 0)
  925. {
  926. size_t base = al_string_length(*str);
  927. size_t i;
  928. VECTOR_RESIZE(*str, base+len, base+len+1);
  929. for(i = 0;i < len;i++)
  930. VECTOR_ELEM(*str, base+i) = from[i];
  931. VECTOR_ELEM(*str, base+i) = 0;
  932. }
  933. }
  934. #ifdef _WIN32
  935. void al_string_copy_wcstr(al_string *str, const wchar_t *from)
  936. {
  937. int len;
  938. if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
  939. {
  940. VECTOR_RESIZE(*str, len-1, len);
  941. WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
  942. VECTOR_ELEM(*str, len-1) = 0;
  943. }
  944. }
  945. void al_string_append_wcstr(al_string *str, const wchar_t *from)
  946. {
  947. int len;
  948. if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
  949. {
  950. size_t base = al_string_length(*str);
  951. VECTOR_RESIZE(*str, base+len-1, base+len);
  952. WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_ELEM(*str, base), len, NULL, NULL);
  953. VECTOR_ELEM(*str, base+len-1) = 0;
  954. }
  955. }
  956. void al_string_append_wrange(al_string *str, const wchar_t *from, const wchar_t *to)
  957. {
  958. int len;
  959. if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), NULL, 0, NULL, NULL)) > 0)
  960. {
  961. size_t base = al_string_length(*str);
  962. VECTOR_RESIZE(*str, base+len, base+len+1);
  963. WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_ELEM(*str, base), len+1, NULL, NULL);
  964. VECTOR_ELEM(*str, base+len) = 0;
  965. }
  966. }
  967. #endif