1
0

wasabi_std.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. #include "wasabi_std.h"
  2. //#include <wasabicfg.h>
  3. #include <bfc/parse/pathparse.h>
  4. #ifdef WIN32
  5. #define WIN32_LEAN_AND_MEAN
  6. #include <shlobj.h>
  7. #include <multimon.h>
  8. #if !defined(HMONITOR_DECLARED) && (WINVER < 0x500)
  9. DECLARE_HANDLE(HMONITOR);
  10. #define HMONITOR_DECLARED
  11. #endif
  12. #include <objbase.h>
  13. #include <shellapi.h> // for ShellExecute
  14. #include <math.h>
  15. #include <mbctype.h>
  16. #include <sys/stat.h>
  17. #ifndef SPI_GETWHEELSCROLLLINES
  18. # define SPI_GETWHEELSCROLLLINES 104
  19. #endif
  20. #ifndef SPI_GETLISTBOXSMOOTHSCROLLING
  21. # define SPI_GETLISTBOXSMOOTHSCROLLING 0x1006
  22. #endif
  23. #endif // WIN32
  24. #include <bfc/bfc_assert.h>
  25. #include <bfc/ptrlist.h>
  26. #include <time.h> // CUT if possible... maybe make bfc/platform/time.h
  27. #ifdef __APPLE__
  28. #include <sys/time.h>
  29. #include <sys/stat.h>
  30. #include <sys/types.h>
  31. #include <pthread.h>
  32. #include <unistd.h>
  33. #endif
  34. #ifdef _WIN32
  35. #include <malloc.h>
  36. #include <shlwapi.h>
  37. #endif
  38. static HANDLE wasabiHeap = NULL;
  39. void *MALLOC(size_t size)
  40. {
  41. //ASSERT(wasabiHeap != NULL);
  42. ASSERT(size > 0);
  43. //return HeapAlloc(wasabiHeap, 0, size);
  44. return malloc(size);
  45. }
  46. wchar_t *WMALLOC(size_t size)
  47. {
  48. // ASSERT(wasabiHeap != NULL);
  49. ASSERT(size > 0);
  50. size *= sizeof(wchar_t);
  51. // return (wchar_t *)HeapAlloc(wasabiHeap, 0, size);
  52. return (wchar_t*)malloc(size);
  53. }
  54. void *CALLOC(size_t records, size_t recordsize) {
  55. // ASSERT(wasabiHeap != NULL);
  56. ASSERT(records > 0 && recordsize > 0);
  57. // return HeapAlloc(wasabiHeap, HEAP_ZERO_MEMORY, records * recordsize);
  58. return calloc(records, recordsize);
  59. }
  60. void FREE(void *ptr) {
  61. if (ptr == NULL)
  62. return;
  63. //HeapFree(wasabiHeap, 0, ptr);
  64. free(ptr);
  65. ptr = NULL;
  66. }
  67. // Note: MEMCPY allows dest and src to overlap
  68. void MEMCPY(void *dest, const void *src, size_t n) {
  69. ASSERT(dest != NULL);
  70. ASSERT(src != NULL);
  71. ASSERT(n >= 0);
  72. memmove(dest, src, n);
  73. }
  74. void *MEMDUP(const void *src, size_t n) {
  75. void *ret;
  76. ASSERT(n >= 0);
  77. if (src == NULL || n == 0) return NULL;
  78. ret = MALLOC(n);
  79. if (ret == NULL) return NULL;
  80. MEMCPY(ret, src, n);
  81. return ret;
  82. }
  83. void *REALLOC(void *ptr, size_t size) {
  84. //ASSERT(wasabiHeap != NULL);
  85. ASSERT(size >= 0);
  86. if (ptr == NULL) {
  87. return realloc(0, size);
  88. //return HeapAlloc(wasabiHeap, 0, size);
  89. } else {
  90. return realloc(ptr, size);
  91. //return HeapReAlloc(wasabiHeap, 0, ptr, size);
  92. }
  93. }
  94. static wchar_t TOUPPERANDSLASH(wchar_t a)
  95. {
  96. if (a=='\\')
  97. a = '/';
  98. else
  99. a = TOUPPERW(a);
  100. return a;
  101. }
  102. int PATHEQL(const wchar_t *str1, const wchar_t *str2) {
  103. if (str1 == NULL) {
  104. if (str2 == NULL) return TRUE;
  105. return FALSE;
  106. }
  107. while (TOUPPERANDSLASH(*str1) == TOUPPERANDSLASH(*str2) && *str1 != 0 && *str2 != 0) str1++, str2++;
  108. return *str1 == *str2;
  109. }
  110. static int rand_inited;
  111. static double divider=0.;
  112. void Wasabi::Std::Initialize()
  113. {
  114. //if (wasabiHeap == NULL) {
  115. // wasabiHeap = HeapCreate(0, 0, 0);
  116. // srandom();
  117. // LARGE_INTEGER ll;
  118. // QueryPerformanceFrequency(&ll);
  119. // divider = (double)ll.QuadPart;
  120. //}
  121. if (0 == divider)
  122. {
  123. srandom();
  124. LARGE_INTEGER ll;
  125. QueryPerformanceFrequency(&ll);
  126. divider = (double)ll.QuadPart;
  127. }
  128. }
  129. void Wasabi::Std::getMousePos(POINT *p) {
  130. ASSERT(p != NULL);
  131. #ifdef WIN32
  132. GetCursorPos(p);
  133. #elif defined(__APPLE__)
  134. Point pt;
  135. GetMouse(&pt);
  136. p->x = pt.v;
  137. p->y = pt.h;
  138. #else
  139. Window w1, w2;
  140. int a, b;
  141. unsigned int c;
  142. int x, y;
  143. XQueryPointer( Linux::getDisplay(), Linux::RootWin(), &w1, &w2,
  144. &x, &y, &a, &b, &c );
  145. p->x = x;
  146. p->y = y;
  147. #endif
  148. }
  149. void Wasabi::Std::getMousePos(int *x, int *y) {
  150. POINT p;
  151. getMousePos(&p);
  152. if (x != NULL) *x = p.x;
  153. if (y != NULL) *y = p.y;
  154. }
  155. void Wasabi::Std::getMousePos(long *x, long *y) {
  156. getMousePos((int *)x, (int *)y);
  157. }
  158. void Wasabi::Std::setMousePos(POINT *p) {
  159. ASSERT(p != NULL);
  160. #ifdef WIN32
  161. SetCursorPos(p->x, p->y);
  162. #elif defined(__APPLE__)
  163. CGWarpMouseCursorPosition(HIPointFromPOINT(p));
  164. #else
  165. POINT p2;
  166. getMousePos( &p2 );
  167. XWarpPointer( Linux::getDisplay(), None, None,
  168. 0, 0, 1, 1, p->x - p2.x, p->y - p2.y );
  169. #endif
  170. }
  171. void Wasabi::Std::setMousePos(int x, int y) {
  172. POINT p={x,y};
  173. setMousePos(&p);
  174. }
  175. void Wasabi::Std::getViewport(RECT *r, POINT *p, int full)
  176. {
  177. #ifdef _WIN32
  178. getViewport(r, p, NULL, (HWND)0, full);
  179. #elif defined(__APPLE__)
  180. CGDirectDisplayID display;
  181. CGDisplayCount count;
  182. if (CGGetDisplaysWithPoint(HIPointFromPOINT(p), 1, &display, &count) == kCGErrorSuccess)
  183. {
  184. HIRect rect = CGDisplayBounds(display);
  185. *r = RECTFromHIRect(&rect);
  186. // TODO: cut out dock if full == 0 maybe GetAvailableWindowPositioningBounds if we can get the GDHandle
  187. }
  188. #endif
  189. }
  190. void Wasabi::Std::getViewport(RECT *r, RECT *sr, int full)
  191. {
  192. #ifdef _WIN32
  193. getViewport(r, NULL, sr, (HWND)0, full);
  194. #elif defined(__APPLE__)
  195. CGDirectDisplayID display;
  196. CGDisplayCount count;
  197. if (CGGetDisplaysWithRect(HIRectFromRECT(sr), 1, &display, &count) == kCGErrorSuccess)
  198. {
  199. HIRect rect = CGDisplayBounds(display);
  200. *r = RECTFromHIRect(&rect);
  201. // TODO: cut out dock if full == 0 maybe GetAvailableWindowPositioningBounds if we can get the GDHandle
  202. }
  203. #endif
  204. }
  205. void Wasabi::Std::getViewport(RECT *r, OSWINDOWHANDLE wnd, int full)
  206. {
  207. #ifdef _WIN32
  208. getViewport(r, NULL, NULL, wnd, full);
  209. #elif defined(__APPLE__)
  210. GDHandle gd;
  211. Rect gdr;
  212. GetWindowGreatestAreaDevice(wnd, kWindowStructureRgn, &gd, &gdr);
  213. if (full)
  214. {
  215. CGDirectDisplayID display = QDGetCGDirectDisplayID(gd);
  216. HIRect rect = CGDisplayBounds(display);
  217. *r = RECTFromHIRect(&rect);
  218. }
  219. else
  220. {
  221. // TODO: maybe use GetAvailableWindowPositioningBounds instead
  222. r->left = gdr.left;
  223. r->top = gdr.top;
  224. r->right = gdr.right;
  225. r->bottom = gdr.bottom;
  226. }
  227. #endif
  228. }
  229. #ifdef _WIN32
  230. static HINSTANCE user32_instance = 0;
  231. BOOL (WINAPI *Gmi)(HMONITOR mon, LPMONITORINFO lpmi) = 0;
  232. #endif
  233. void Wasabi::Std::getViewport(RECT *r, POINT *p, RECT *sr, OSWINDOWHANDLE wnd, int full) {
  234. ASSERT(r != NULL);
  235. #ifdef WIN32
  236. if (p || sr || wnd)
  237. {
  238. if (!user32_instance)
  239. user32_instance=LoadLibraryW(L"user32.dll");
  240. if (user32_instance)
  241. {
  242. HMONITOR (WINAPI *Mfp)(POINT pt, DWORD dwFlags) = (HMONITOR (WINAPI *)(POINT,DWORD)) GetProcAddress(user32_instance,"MonitorFromPoint");
  243. HMONITOR (WINAPI *Mfr)(LPCRECT lpcr, DWORD dwFlags) = (HMONITOR (WINAPI *)(LPCRECT, DWORD)) GetProcAddress(user32_instance, "MonitorFromRect");
  244. HMONITOR (WINAPI *Mfw)(HWND wnd, DWORD dwFlags)=(HMONITOR (WINAPI *)(HWND, DWORD)) GetProcAddress(user32_instance, "MonitorFromWindow");
  245. if (!Gmi)
  246. Gmi = (BOOL (WINAPI *)(HMONITOR,LPMONITORINFO)) GetProcAddress(user32_instance,"GetMonitorInfoW");
  247. if (Mfp && Mfr && Mfw && Gmi)
  248. {
  249. HMONITOR hm;
  250. if ( p )
  251. hm = Mfp( *p, MONITOR_DEFAULTTONEAREST );
  252. else if ( sr )
  253. hm = Mfr( sr, MONITOR_DEFAULTTONEAREST );
  254. else if ( wnd )
  255. hm = Mfw( wnd, MONITOR_DEFAULTTONEAREST );
  256. if (hm)
  257. {
  258. MONITORINFOEXW mi;
  259. ZERO(mi);
  260. mi.cbSize=sizeof(mi);
  261. if (Gmi(hm,&mi))
  262. {
  263. if(!full)
  264. *r=mi.rcWork;
  265. else
  266. *r=mi.rcMonitor;
  267. return;
  268. }
  269. }
  270. }
  271. }
  272. }
  273. SystemParametersInfoW(SPI_GETWORKAREA,0,r,0);
  274. #endif
  275. #ifdef LINUX
  276. r->left = r->top = 0;
  277. r->right = Wasabi::Std::getScreenWidth();
  278. r->bottom = Wasabi::Std::getScreenHeight();
  279. #endif
  280. }
  281. #ifdef WIN32
  282. class ViewportEnumerator {
  283. public:
  284. HMONITOR hm;
  285. #ifdef WIN32
  286. int monitor_n;
  287. #endif
  288. int counter;
  289. };
  290. static BOOL CALLBACK enumViewportsProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
  291. ViewportEnumerator *ve = reinterpret_cast<ViewportEnumerator *>(dwData);
  292. ASSERT(ve != NULL);
  293. if (ve->counter == ve->monitor_n) {
  294. ve->hm = hMonitor;
  295. return FALSE;
  296. }
  297. ve->counter++;
  298. return TRUE;
  299. }
  300. #endif
  301. void Wasabi::Std::srandom(unsigned int key) {
  302. if (key == 0) key = (unsigned int)Std::getTimeStamp();
  303. ::srand(key);
  304. }
  305. int Wasabi::Std::random(int max) {
  306. // ASSERT(max <= RAND_MAX);
  307. int ret = ::rand();
  308. if (max != RAND_MAX+1) ret %= max;
  309. return ret;
  310. }
  311. unsigned int Wasabi::Std::random32(unsigned int max) {
  312. if (!rand_inited++) Wasabi::Std::srandom();
  313. unsigned int val=0;
  314. for (int i = 0; i < sizeof(unsigned int); i++) {
  315. val <<= 8;
  316. val |= ((::rand()>>2) & 0xff);
  317. }
  318. if (max != 0xffffffff) val %= max;
  319. return val;
  320. }
  321. #ifdef _WIN32 // PORT ME
  322. void Wasabi::Std::usleep(int ms) {
  323. Sleep(ms);
  324. //INLINE
  325. }
  326. #endif
  327. __time64_t Wasabi::Std::getTimeStamp() {
  328. __time64_t time64;
  329. _time64(&time64);
  330. return time64;
  331. }
  332. stdtimevalms Wasabi::Std::getTimeStampMS() {
  333. #ifdef WIN32
  334. LARGE_INTEGER ll;
  335. if (!QueryPerformanceCounter(&ll)) return getTickCount() / 1000.f;
  336. stdtimevalms ret = (stdtimevalms)ll.QuadPart;
  337. return ret /= divider;
  338. #else
  339. return getTickCount() / 1000.f;
  340. #endif
  341. }
  342. uint32_t Wasabi::Std::getTickCount()
  343. {
  344. #ifdef _WIN32
  345. return GetTickCount();
  346. #elif defined(__APPLE__)
  347. struct timeval newtime;
  348. gettimeofday(&newtime, 0);
  349. return newtime.tv_sec*1000 + newtime.tv_usec/1000;
  350. #endif
  351. //INLINE
  352. }
  353. #ifdef _WIN32 // PORT ME
  354. void Wasabi::Std::ensureVisible(RECT *r) {
  355. RECT sr;
  356. POINT p={(r->right+r->left)/2,(r->bottom+r->top)/2};
  357. Wasabi::Std::getViewport(&sr,&p);
  358. int w = r->right-r->left;
  359. int h = r->bottom-r->top;
  360. if (r->bottom > sr.bottom) {
  361. r->bottom = sr.bottom;
  362. r->top = r->bottom-h;
  363. }
  364. if (r->right > sr.right) {
  365. r->right = sr.right;
  366. r->left = r->right-w;
  367. }
  368. if (r->left < sr.left) {
  369. r->left = sr.left;
  370. r->right = r->left+w;
  371. }
  372. if (r->top < sr.top) {
  373. r->top = sr.top;
  374. r->bottom = r->top+h;
  375. }
  376. }
  377. #endif
  378. int Wasabi::Std::getScreenWidth()
  379. {
  380. #ifdef WIN32
  381. RECT r;
  382. SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
  383. return r.right-r.left;
  384. #elif defined(__APPLE__)
  385. CGDirectDisplayID mainID = CGMainDisplayID();
  386. return CGDisplayPixelsWide(mainID);
  387. #elif defined(LINUX)
  388. return DisplayWidth(Linux::getDisplay(), Linux::getScreenNum());
  389. #endif
  390. }
  391. int Wasabi::Std::getScreenHeight()
  392. {
  393. #ifdef WIN32
  394. RECT r;
  395. SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
  396. return r.bottom-r.top;
  397. #elif defined(__APPLE__)
  398. CGDirectDisplayID mainID = CGMainDisplayID();
  399. return CGDisplayPixelsHigh(mainID);
  400. #else
  401. return DisplayHeight( Linux::getDisplay(), Linux::getScreenNum() );
  402. #endif
  403. }
  404. #ifndef __APPLE__ // PORT ME
  405. int Wasabi::Std::messageBox(const wchar_t *txt, const wchar_t *title, int flags) {
  406. #ifdef WIN32
  407. return MessageBoxW(NULL, txt, title, flags);
  408. #else
  409. OutputDebugString(txt);
  410. #endif
  411. }
  412. #endif
  413. int Wasabi::Std::getDoubleClickDelay() {
  414. #ifdef WIN32
  415. return GetDoubleClickTime();
  416. #elif defined(__APPLE__)
  417. return GetDblTime();
  418. #endif
  419. }
  420. #undef GetSystemMetrics //FG> DUH!
  421. int Wasabi::Std::getDoubleClickX() {
  422. #ifdef WIN32
  423. return GetSystemMetrics(SM_CYDOUBLECLK);
  424. #else
  425. return 5;
  426. #endif
  427. }
  428. int Wasabi::Std::getDoubleClickY() {
  429. #ifdef WIN32
  430. return GetSystemMetrics(SM_CXDOUBLECLK);
  431. #else
  432. return 5;
  433. #endif
  434. }
  435. int Wasabi::Std::osparam_getScrollLines() {
  436. int ret = 3;
  437. #ifdef WIN32
  438. SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &ret, 0);
  439. #endif
  440. return ret;
  441. }
  442. int Wasabi::Std::osparam_getSmoothScroll() {
  443. int ret = 1;
  444. #ifdef WIN32
  445. SystemParametersInfo(SPI_GETLISTBOXSMOOTHSCROLLING, 0, &ret, 0);
  446. #endif
  447. return ret;
  448. }
  449. const wchar_t Wasabi::Std::dirChar() {
  450. #ifdef WIN32
  451. return '\\';
  452. #else
  453. return '/';
  454. #endif
  455. }
  456. const char * Wasabi::Std::dirCharStr() {
  457. #ifdef WIN32
  458. return "\\";
  459. #else
  460. return "/";
  461. #endif
  462. };
  463. const wchar_t * Wasabi::Std::dirCharStrW()
  464. {
  465. #ifdef WIN32
  466. return L"\\";
  467. #else
  468. return L"/";
  469. #endif
  470. };
  471. int Wasabi::Std::isDirChar(int thechar, int allow_multiple_platforms) {
  472. if (thechar == DIRCHAR) return 1;
  473. if (allow_multiple_platforms) {
  474. #ifdef WIN32
  475. if (thechar == '/') return 1;
  476. #else
  477. if (thechar == '\\') return 1;
  478. #endif
  479. }
  480. return 0;
  481. }
  482. const wchar_t * Wasabi::Std::matchAllFiles() {
  483. #ifdef WIN32
  484. return L"*.*";
  485. #else
  486. return L"*";
  487. #endif
  488. }
  489. const wchar_t * Wasabi::Std::dotDir() {
  490. return L".";
  491. }
  492. const wchar_t * Wasabi::Std::dotDotDir()
  493. {
  494. return L"..";
  495. }
  496. #ifdef _WIN32 // PORT ME
  497. bool Wasabi::Std::isRootPath(const wchar_t *path)
  498. {
  499. return PathIsRootW(path)==TRUE;
  500. }
  501. #endif
  502. int Wasabi::Std::switchChar() {
  503. #ifdef WIN32
  504. return '/';
  505. #else
  506. return '-';
  507. #endif
  508. }
  509. int Wasabi::Std::enumViewports( int monitor_n, RECT *r, int full )
  510. {
  511. ASSERT( r != NULL );
  512. #ifdef WIN32
  513. if ( !user32_instance )
  514. {
  515. user32_instance = LoadLibraryW( L"user32.dll" );
  516. }
  517. if ( user32_instance )
  518. {
  519. if ( !Gmi )
  520. {
  521. Gmi = ( BOOL( WINAPI * )( HMONITOR, LPMONITORINFO ) ) GetProcAddress( user32_instance, "GetMonitorInfoW" );
  522. }
  523. BOOL( WINAPI * Edm )( HDC hdc, LPCRECT clip, MONITORENUMPROC proc, LPARAM dwdata ) = ( BOOL( WINAPI * )( HDC, LPCRECT, MONITORENUMPROC, LPARAM ) ) GetProcAddress( user32_instance, "EnumDisplayMonitors" );
  524. if ( Gmi && Edm )
  525. {
  526. ViewportEnumerator ve;
  527. ve.monitor_n = monitor_n;
  528. ve.hm = NULL;
  529. ve.counter = 0;
  530. Edm( NULL, NULL, enumViewportsProc, reinterpret_cast<LPARAM>( &ve ) );
  531. HMONITOR hm = ve.hm;
  532. if ( hm )
  533. {
  534. MONITORINFOEXW mi;
  535. ZERO( mi );
  536. mi.cbSize = sizeof( mi );
  537. if ( Gmi( hm, &mi ) )
  538. {
  539. if ( !full )
  540. *r = mi.rcWork;
  541. else
  542. *r = mi.rcMonitor;
  543. return 1;
  544. }
  545. }
  546. }
  547. }
  548. SystemParametersInfoW( SPI_GETWORKAREA, 0, r, 0 );
  549. return 0;
  550. #elif defined(__APPLE__)
  551. CGDirectDisplayID monitors[ 256 ]; // TODO: allocate dynamically
  552. CGDisplayCount count;
  553. if ( monitor_n >= 256 )
  554. return 0;
  555. CGGetActiveDisplayList( 256, monitors, &count );
  556. if ( count <= monitor_n )
  557. return 0;
  558. HIRect rect = CGDisplayBounds( monitors[ monitor_n ] );
  559. *r = RECTFromHIRect( &rect );
  560. // TODO: cut out dock if full == 0 maybe GetAvailableWindowPositioningBounds if we can get the GDHandle
  561. #elif defined(LINUX)
  562. if ( monitor_n > 0 )
  563. return 0;
  564. r->left = r->top = 0;
  565. r->right = Wasabi::Std::getScreenWidth();
  566. r->bottom = Wasabi::Std::getScreenHeight();
  567. return 1;
  568. #endif
  569. }
  570. const char * Wasabi::Std::scanstr_back(const char *str, const char *toscan, const char *defval) {
  571. int strl = STRLEN(str);
  572. const char *s=str+strl-1;
  573. if (strl < 1) return defval;
  574. if (STRLEN(toscan) < 1) return defval;
  575. while (1) {
  576. const char *t=toscan;
  577. while (t && *t) if (*t++ == *s) return s;
  578. #ifdef _WIN32
  579. t=CharPrevA(str,s);
  580. #else
  581. t = s-1;
  582. #endif
  583. if (t==s) return defval;
  584. s=t;
  585. }
  586. }
  587. const wchar_t * Wasabi::Std::extension(const wchar_t *fn)
  588. {
  589. #ifdef _WIN32 // PORT ME
  590. wchar_t *x = PathFindExtensionW(fn);
  591. if (x && *x)
  592. return CharNextW(x);
  593. else
  594. return x;
  595. #else
  596. #warning port me
  597. return 0;
  598. #endif
  599. }
  600. const wchar_t * Wasabi::Std::filename(const wchar_t *fn)
  601. {
  602. #ifdef _WIN32
  603. return PathFindFileNameW(fn);
  604. #else
  605. #warning port me
  606. return 0;
  607. #endif
  608. }
  609. bool Wasabi::Std::isMatchPattern(const wchar_t *p)
  610. {
  611. while (p && *p) {
  612. switch (*p++) {
  613. case '?':
  614. case '*':
  615. case '[':
  616. case '\\':
  617. return TRUE;
  618. }
  619. }
  620. return FALSE;
  621. }
  622. bool Wasabi::Std::isValidMatchPattern(const wchar_t *p, int *error_type)
  623. {
  624. /* init error_type */
  625. *error_type = PATTERN_VALID;
  626. /* loop through pattern to EOS */
  627. while (p && *p) {
  628. /* determine pattern type */
  629. switch (*p) {
  630. /* check literal escape, it cannot be at end of pattern */
  631. case '\\':
  632. if (!*++p) {
  633. *error_type = PATTERN_ESC;
  634. return FALSE;
  635. }
  636. p++;
  637. break;
  638. /* the [..] construct must be well formed */
  639. case '[':
  640. p++;
  641. /* if the next character is ']' then bad pattern */
  642. if (*p == ']') {
  643. *error_type = PATTERN_EMPTY;
  644. return FALSE;
  645. }
  646. /* if end of pattern here then bad pattern */
  647. if (!*p) {
  648. *error_type = PATTERN_CLOSE;
  649. return FALSE;
  650. }
  651. /* loop to end of [..] construct */
  652. while (p && *p != ']') {
  653. /* check for literal escape */
  654. if (*p == '\\') {
  655. p++;
  656. /* if end of pattern here then bad pattern */
  657. if (!*p++) {
  658. *error_type = PATTERN_ESC;
  659. return FALSE;
  660. }
  661. } else p++;
  662. /* if end of pattern here then bad pattern */
  663. if (!*p) {
  664. *error_type = PATTERN_CLOSE;
  665. return FALSE;
  666. }
  667. /* if this a range */
  668. if (*p == '-') {
  669. /* we must have an end of range */
  670. if (!*++p || *p == ']') {
  671. *error_type = PATTERN_RANGE;
  672. return FALSE;
  673. } else {
  674. /* check for literal escape */
  675. if (*p == '\\') p++;
  676. /* if end of pattern here then bad pattern */
  677. if (!*p++) {
  678. *error_type = PATTERN_ESC;
  679. return FALSE;
  680. }
  681. }
  682. }
  683. }
  684. break;
  685. /* all other characters are valid pattern elements */
  686. case '*':
  687. case '?':
  688. default:
  689. p++; /* "normal" character */
  690. break;
  691. }
  692. }
  693. return TRUE;
  694. }
  695. #ifdef _WIN32 // PORT ME
  696. int Wasabi::Std::matche(register const wchar_t *p, register const wchar_t *t)
  697. {
  698. register wchar_t range_start, range_end; /* start and end in range */
  699. BOOLEAN invert; /* is this [..] or [!..] */
  700. BOOLEAN member_match; /* have I matched the [..] construct? */
  701. BOOLEAN loop; /* should I terminate? */
  702. for ( ; *p; p++, t++)
  703. {
  704. /* if this is the end of the text then this is the end of the match */
  705. if (!*t) {
  706. return ( *p == '*' && *++p == '\0' ) ? MATCH_VALID : MATCH_ABORT;
  707. }
  708. /* determine and react to pattern type */
  709. switch (*p) {
  710. case '?': /* single any character match */
  711. break;
  712. case '*': /* multiple any character match */
  713. return matche_after_star (p, t);
  714. /* [..] construct, single member/exclusion character match */
  715. case '[': {
  716. /* move to beginning of range */
  717. p++;
  718. /* check if this is a member match or exclusion match */
  719. invert = FALSE;
  720. if (*p == '!' || *p == '^') {
  721. invert = TRUE;
  722. p++;
  723. }
  724. /* if closing bracket here or at range start then we have a malformed pattern */
  725. if (*p == ']') return MATCH_PATTERN;
  726. member_match = FALSE;
  727. loop = TRUE;
  728. while (loop) {
  729. /* if end of construct then loop is done */
  730. if (*p == ']') {
  731. loop = FALSE;
  732. continue;
  733. }
  734. /* matching a '!', '^', '-', '\' or a ']' */
  735. if (*p == '\\') range_start = range_end = *++p;
  736. else range_start = range_end = *p;
  737. /* if end of pattern then bad pattern (Missing ']') */
  738. if (!*p) return MATCH_PATTERN;
  739. /* check for range bar */
  740. if (*++p == '-') {
  741. /* get the range end */
  742. range_end = *++p;
  743. /* if end of pattern or construct then bad pattern */
  744. if (range_end == '\0' || range_end == ']') return MATCH_PATTERN;
  745. /* special character range end */
  746. if (range_end == '\\') {
  747. range_end = *++p;
  748. /* if end of text then we have a bad pattern */
  749. if (!range_end) return MATCH_PATTERN;
  750. }
  751. /* move just beyond this range */
  752. p++;
  753. }
  754. /* if the text character is in range then match found.
  755. make sure the range letters have the proper
  756. relationship to one another before comparison */
  757. if (range_start < range_end) {
  758. if (*t >= range_start && *t <= range_end) {
  759. member_match = TRUE;
  760. loop = FALSE;
  761. }
  762. } else {
  763. if (*t >= range_end && *t <= range_start) {
  764. member_match = TRUE;
  765. loop = FALSE;
  766. }
  767. }
  768. }
  769. /* if there was a match in an exclusion set then no match */
  770. /* if there was no match in a member set then no match */
  771. if ((invert && member_match) || !(invert || member_match)) return MATCH_RANGE;
  772. /* if this is not an exclusion then skip the rest of the [...] construct that already matched. */
  773. if (member_match) {
  774. while (p && *p != ']') {
  775. /* bad pattern (Missing ']') */
  776. if (!*p) return MATCH_PATTERN;
  777. /* skip exact match */
  778. if (*p == '\\') {
  779. p++;
  780. /* if end of text then we have a bad pattern */
  781. if (!*p) return MATCH_PATTERN;
  782. }
  783. /* move to next pattern char */
  784. p++;
  785. }
  786. }
  787. break;
  788. }
  789. case '\\': /* next character is quoted and must match exactly */
  790. /* move pattern pointer to quoted char and fall through */
  791. p++;
  792. /* if end of text then we have a bad pattern */
  793. if (!*p) return MATCH_PATTERN;
  794. /* must match this character exactly */
  795. default:
  796. if (TOUPPERW(*p) != TOUPPERW(*t)) return MATCH_LITERAL;
  797. }
  798. }
  799. /* if end of text not reached then the pattern fails */
  800. if (*t) return MATCH_END;
  801. else return MATCH_VALID;
  802. }
  803. int Wasabi::Std::matche_after_star(register const wchar_t *p, register const wchar_t *t)
  804. {
  805. register int match = 0;
  806. register wchar_t nextp;
  807. /* pass over existing ? and * in pattern */
  808. while ( *p == '?' || *p == '*' )
  809. {
  810. /* take one char for each ? and + */
  811. if (*p == '?') {
  812. /* if end of text then no match */
  813. if (!*t++) return MATCH_ABORT;
  814. }
  815. /* move to next char in pattern */
  816. p++;
  817. }
  818. /* if end of pattern we have matched regardless of text left */
  819. if (!*p) return MATCH_VALID;
  820. /* get the next character to match which must be a literal or '[' */
  821. nextp = *p;
  822. if (nextp == '\\') {
  823. nextp = p[1];
  824. /* if end of text then we have a bad pattern */
  825. if (!nextp) return MATCH_PATTERN;
  826. }
  827. /* Continue until we run out of text or definite result seen */
  828. do {
  829. /* a precondition for matching is that the next character
  830. in the pattern match the next character in the text or that
  831. the next pattern char is the beginning of a range. Increment
  832. text pointer as we go here */
  833. if (TOUPPERW(nextp) == TOUPPERW(*t) || nextp == '[') match = matche(p, t);
  834. /* if the end of text is reached then no match */
  835. if (!*t++) match = MATCH_ABORT;
  836. } while ( match != MATCH_VALID && match != MATCH_ABORT && match != MATCH_PATTERN);
  837. /* return result */
  838. return match;
  839. }
  840. bool Wasabi::Std::match(const wchar_t *p, const wchar_t *t)
  841. {
  842. int error_type;
  843. error_type = matche(p,t);
  844. return (error_type == MATCH_VALID ) ? TRUE : FALSE;
  845. }
  846. #endif
  847. #ifndef _NOSTUDIO
  848. int Wasabi::Std::getCurDir(wchar_t *str, int maxlen) {
  849. ASSERT(str != NULL);
  850. #ifdef WIN32
  851. int retval = 0;
  852. retval = GetCurrentDirectoryW(maxlen, str);
  853. return retval;
  854. #else
  855. #warning port me
  856. return 0;
  857. //return getcwd( str, maxlen ) != NULL;
  858. #endif
  859. }
  860. int Wasabi::Std::setCurDir(const wchar_t *str) {
  861. ASSERT(str != NULL);
  862. #ifdef WIN32
  863. int retval = 0;
  864. retval = SetCurrentDirectoryW(str);
  865. return retval;
  866. #else
  867. #warning port me
  868. return 0;
  869. // return chdir( str );
  870. #endif
  871. }
  872. int Wasabi::Std::getNumCPUs() {
  873. #ifdef WIN32
  874. SYSTEM_INFO si;
  875. ZERO(si);
  876. GetSystemInfo(&si);
  877. return si.dwNumberOfProcessors;
  878. #else
  879. #ifdef PLATFORM_WARNINGS
  880. #warning Wasabi::Std::getNumCPUs not implemented on LINUX
  881. #endif
  882. return 1;
  883. #endif
  884. }
  885. THREADID Wasabi::Std::getCurrentThreadId() {
  886. #ifdef WIN32
  887. return GetCurrentThreadId();
  888. #else
  889. return pthread_self();
  890. #endif
  891. }
  892. #ifdef WIN32
  893. void Wasabi::Std::setThreadPriority(int delta, HANDLE thread_handle)
  894. {
  895. if (thread_handle == NULL) thread_handle = GetCurrentThread();
  896. int v = THREAD_PRIORITY_NORMAL;
  897. switch (delta) {
  898. case -32767: v = THREAD_PRIORITY_IDLE; break;
  899. case -2: v = THREAD_PRIORITY_LOWEST; break;
  900. case -1: v = THREAD_PRIORITY_BELOW_NORMAL; break;
  901. case 1: v = THREAD_PRIORITY_ABOVE_NORMAL; break;
  902. case 2: v = THREAD_PRIORITY_HIGHEST; break;
  903. case 32767: v = THREAD_PRIORITY_TIME_CRITICAL; break;
  904. }
  905. SetThreadPriority(thread_handle, v);
  906. }
  907. #else
  908. #ifdef PLATFORM_WARNINGS
  909. #warning Wasabi::Std::setThreadPriority not implemented on LINUX
  910. #endif
  911. #endif
  912. String Wasabi::Std::getLastErrorString(int _err) {
  913. String ret;
  914. if (_err == -1) {
  915. #ifdef WIN32
  916. _err = GetLastError();
  917. #else
  918. #ifdef PLATFORM_WARNINGS
  919. #warning Wasabi::Std::getLastErrorString not implemented on LINUX
  920. #endif
  921. #endif
  922. }
  923. #ifdef WIN32
  924. char *sysbuf;
  925. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  926. NULL, _err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *)&sysbuf, 0, NULL);
  927. ret = sysbuf;
  928. LocalFree(sysbuf);
  929. #endif
  930. return ret;
  931. }
  932. // gets the system font path
  933. void Wasabi::Std::getFontPath(int bufsize, wchar_t *dst)
  934. {
  935. ASSERT(dst != NULL);
  936. #ifdef WIN32
  937. wchar_t path[MAX_PATH]=L"";
  938. SHGetSpecialFolderPathW(NULL, path, CSIDL_FONTS, FALSE);
  939. WCSCPYN(dst, path, bufsize);
  940. #else
  941. #ifdef PLATFORM_WARNINGS
  942. #warning Wasabi::Std::getFontPath not implemented on LINUX
  943. #endif
  944. #endif
  945. }
  946. const wchar_t wasabi_default_fontnameW[] = WASABI_DEFAULT_FONTNAMEW;
  947. int default_font_scale = 100;
  948. wchar_t default_font[256] =
  949. #ifdef WIN32
  950. WASABI_DEFAULT_FONTNAMEW L".ttf";
  951. #elif defined(__APPLE__)
  952. WASABI_DEFAULT_FONTNAMEW;
  953. #elif defined(LINUX)
  954. // even tho this isn't the way we'll port this, the style is fun.
  955. "-*-arial-medium-r-*--10-*-*-*-*-*-*-*";
  956. #endif
  957. // gets the filename of a font file guaranteed to be in the system font path.
  958. void Wasabi::Std::getDefaultFont(int bufsize, wchar_t *dst)
  959. {
  960. ASSERT(dst != NULL);
  961. WCSCPYN(dst, default_font, bufsize);
  962. }
  963. void Wasabi::Std::setDefaultFont(const wchar_t *newdefaultfont)
  964. {
  965. WCSCPYN(default_font, newdefaultfont, 256);
  966. }
  967. int Wasabi::Std::getDefaultFontScale() {
  968. return default_font_scale;
  969. }
  970. void Wasabi::Std::setDefaultFontScale(int scale) {
  971. default_font_scale = scale;
  972. }
  973. #ifndef __APPLE__ // PORT ME
  974. int Wasabi::Std::createDirectory(const wchar_t *dirname)
  975. {
  976. #ifdef WIN32
  977. if(!CreateDirectoryW(dirname,NULL))
  978. #else
  979. if(mkdir(dirname, 0755))
  980. #endif
  981. {
  982. // create all the path
  983. PathParserW pp(dirname);
  984. int l = pp.getNumStrings();
  985. for(int i=2;i<=l;i++)
  986. {
  987. StringW dir;
  988. for(int j=0;j<i;j++)
  989. dir.AppendFolder(pp.enumString(j));
  990. #ifdef WIN32
  991. CreateDirectoryW(dir,NULL);
  992. #else
  993. mkdir(dir, 0755);
  994. #endif
  995. }
  996. }
  997. return 1;
  998. }
  999. #endif
  1000. int Wasabi::Std::getFileInfos(const char *filename, fileInfoStruct *infos) {
  1001. #ifdef WIN32
  1002. HANDLE h;
  1003. WIN32_FIND_DATAA fd = {0};
  1004. if((h=FindFirstFileA(filename, &fd))==INVALID_HANDLE_VALUE) return 0;
  1005. infos->fileSizeHigh=fd.nFileSizeHigh;
  1006. infos->fileSizeLow=fd.nFileSizeLow;
  1007. struct _stati64 statbuf;
  1008. if (_stati64(filename, &statbuf) == -1) return 0;
  1009. infos->lastWriteTime = statbuf.st_mtime;
  1010. infos->readonly = fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY;
  1011. FindClose(h);
  1012. return 1;
  1013. #else
  1014. struct stat st;
  1015. int ret = stat( filename, &st );
  1016. if ( ret != 0 )
  1017. return 0;
  1018. infos->fileSizeHigh = 0;
  1019. infos->fileSizeLow = st.st_size;
  1020. infos->lastWriteTime = st.st_mtime;
  1021. return 1;
  1022. #endif
  1023. }
  1024. void Wasabi::Std::shellExec(const wchar_t *cmd, const wchar_t *params)
  1025. {
  1026. #ifdef WIN32
  1027. ShellExecuteW(NULL, NULL, cmd, params, L".", SW_SHOWNORMAL);
  1028. #else
  1029. system(StringPrintf("%S %S", cmd, params));
  1030. #endif
  1031. }
  1032. #endif // nostudio
  1033. void MEMFILL32(void *lptr, unsigned long val, unsigned int n) {
  1034. if (n == 0) return;
  1035. #if defined(WIN32) && !defined(_WIN64)
  1036. __asm {
  1037. mov eax, val
  1038. mov edi, lptr
  1039. mov ecx, n
  1040. rep stosd
  1041. };
  1042. #elif defined(GCC)
  1043. //http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html ;)
  1044. asm ("cld\n\t"
  1045. "rep\n\t"
  1046. "stosl"
  1047. : /* no output registers */
  1048. : "c" (n), "a" (val), "D" (lptr)
  1049. : "%ecx", "%edi" );
  1050. #else
  1051. uint32_t *ptr32 = (uint32_t *)lptr;
  1052. for (unsigned int i=0;i!=n;i++)
  1053. {
  1054. ptr32[i] = val;
  1055. }
  1056. #endif
  1057. }
  1058. void MEMFILL(unsigned short *ptr, unsigned short val, unsigned int n) {
  1059. if (n == 0) return;
  1060. unsigned long v = (unsigned long)val | ((unsigned long)val << 16);
  1061. int r = n & 1;
  1062. MEMFILL32(ptr, v, n/2);
  1063. if (r) ptr[n-1] = val;
  1064. }
  1065. void MEMCPY32(void *dest, const void *src, size_t words)
  1066. {
  1067. // TODO: write fast asm version of this
  1068. memcpy(dest, src, words*4);
  1069. }
  1070. void MEMCPY_(void *dest, const void *src, size_t n)
  1071. {
  1072. memcpy(dest, src, n);
  1073. }