-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform_windows.c
1630 lines (1322 loc) · 44 KB
/
platform_windows.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#if PLATFORM_WINDOWS
#include "platform.h"
#include "gui.h"
// Much of this file was lifted from PhysicsFS, http://icculus.org/physfs/ ...
// I wrote all this code under the same open source license, and own the
// copyright on it anyhow, so transferring it here is "safe".
// Forcibly disable UNICODE, since we manage this ourselves.
#ifdef UNICODE
#undef UNICODE
#endif
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <shellapi.h>
// !!! FIXME: this is for stdio redirection crap.
#include <io.h>
#include <fcntl.h>
// is Win95/Win98/WinME? (no Unicode, etc)
static boolean osIsWin9x = false;
static uint32 osMajorVer = 0;
static uint32 osMinorVer = 0;
static uint32 osBuildVer = 0;
static uint32 startupTime = 0;
// These allocation macros are much more complicated in PhysicsFS.
#define smallAlloc(x) xmalloc(x)
#define smallFree(x) free(x)
// ...so is this.
#define BAIL_IF_MACRO(cond, err, ret) if (cond) return ret;
#define BAIL_MACRO(err, ret) return ret;
#define LOWORDER_UINT64(pos) ((uint32) (pos & 0xFFFFFFFF))
#define HIGHORDER_UINT64(pos) ((uint32) ((pos >> 32) & 0xFFFFFFFF))
// Users without the platform SDK don't have this defined. The original docs
// for SetFilePointer() just said to compare with 0xFFFFFFFF, so this should
// work as desired.
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
#endif
// just in case...
#ifndef INVALID_FILE_ATTRIBUTES
#define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
#endif
// Not defined before the Vista SDK.
#ifndef IO_REPARSE_TAG_SYMLINK
#define IO_REPARSE_TAG_SYMLINK 0xA000000C
#endif
// This is in shlobj.h, but we can't include it due to universal.h conflicts.
#ifndef CSIDL_PERSONAL
#define CSIDL_PERSONAL 0x0005
#endif
// This is in shlobj.h, but we can't include it due to universal.h conflicts.
#ifndef SHGFP_TYPE_CURRENT
#define SHGFP_TYPE_CURRENT 0x0000
#endif
// these utf-8 functions may move to mojosetup.c some day...
void utf8ToUcs2(const char *src, uint16 *dst, uint64 len)
{
len -= sizeof (uint16); // save room for null char.
while (len >= sizeof (uint16))
{
uint32 cp = utf8codepoint(&src);
if (cp == 0)
break;
else if (cp == UNICODE_BOGUS_CHAR_VALUE)
cp = UNICODE_BOGUS_CHAR_CODEPOINT;
// !!! FIXME: UTF-16 surrogates?
if (cp > 0xFFFF)
cp = UNICODE_BOGUS_CHAR_CODEPOINT;
*(dst++) = cp;
len -= sizeof (uint16);
} // while
*dst = 0;
} // utf8ToUcs2
static void utf8fromcodepoint(uint32 cp, char **_dst, uint64 *_len)
{
char *dst = *_dst;
uint64 len = *_len;
if (len == 0)
return;
if (cp > 0x10FFFF)
cp = UNICODE_BOGUS_CHAR_CODEPOINT;
else if ((cp == 0xFFFE) || (cp == 0xFFFF)) // illegal values.
cp = UNICODE_BOGUS_CHAR_CODEPOINT;
else
{
// There are seven "UTF-16 surrogates" that are illegal in UTF-8.
switch (cp)
{
case 0xD800:
case 0xDB7F:
case 0xDB80:
case 0xDBFF:
case 0xDC00:
case 0xDF80:
case 0xDFFF:
cp = UNICODE_BOGUS_CHAR_CODEPOINT;
} // switch
} // else
// Do the encoding...
if (cp < 0x80)
{
*(dst++) = (char) cp;
len--;
} // if
else if (cp < 0x800)
{
if (len < 2)
len = 0;
else
{
*(dst++) = (char) ((cp >> 6) | 128 | 64);
*(dst++) = (char) (cp & 0x3F) | 128;
len -= 2;
} // else
} // else if
else if (cp < 0x10000)
{
if (len < 3)
len = 0;
else
{
*(dst++) = (char) ((cp >> 12) | 128 | 64 | 32);
*(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
*(dst++) = (char) (cp & 0x3F) | 128;
len -= 3;
} // else
} // else if
else
{
if (len < 4)
len = 0;
else
{
*(dst++) = (char) ((cp >> 18) | 128 | 64 | 32 | 16);
*(dst++) = (char) ((cp >> 12) & 0x3F) | 128;
*(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
*(dst++) = (char) (cp & 0x3F) | 128;
len -= 4;
} // else if
} // else
*_dst = dst;
*_len = len;
} // utf8fromcodepoint
#define UTF8FROMTYPE(typ, src, dst, len) \
len--; \
while (len) \
{ \
const uint32 cp = (uint32) *(src++); \
if (cp == 0) break; \
utf8fromcodepoint(cp, &dst, &len); \
} \
*dst = '\0'; \
void utf8FromUcs2(const uint16 *src, char *dst, uint64 len)
{
UTF8FROMTYPE(uint64, src, dst, len);
} // utf8FromUcs4
#define UTF8_TO_UNICODE_STACK_MACRO(w_assignto, str) { \
if (str == NULL) \
w_assignto = NULL; \
else { \
const uint32 len = (uint32) ((strlen(str) * 4) + 1); \
w_assignto = (WCHAR *) smallAlloc(len); \
if (w_assignto != NULL) \
utf8ToUcs2(str, (uint16 *) w_assignto, len); \
} \
} \
static uint32 wStrLen(const WCHAR *wstr)
{
uint32 len = 0;
while (*(wstr++))
len++;
return len;
} // wStrLen
static char *unicodeToUtf8Heap(const WCHAR *w_str)
{
char *retval = NULL;
if (w_str != NULL)
{
void *ptr = NULL;
const uint32 len = (wStrLen(w_str) * 4) + 1;
retval = (char *) xmalloc(len);
utf8FromUcs2((const uint16 *) w_str, retval, len);
retval = xrealloc(retval, strlen(retval) + 1); // shrink.
} // if
return retval;
} // unicodeToUtf8Heap
static char *codepageToUtf8Heap(const char *cpstr)
{
char *retval = NULL;
if (cpstr != NULL)
{
const int len = (int) (strlen(cpstr) + 1);
WCHAR *wbuf = (WCHAR *) smallAlloc(len * sizeof (WCHAR));
BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cpstr, len, wbuf, len);
retval = (char *) xmalloc(len * 4);
utf8FromUcs2(wbuf, retval, len * 4);
smallFree(wbuf);
} // if
return retval;
} // codepageToUtf8Heap
// pointers for APIs that may not exist on some Windows versions...
static HANDLE libKernel32 = NULL;
static HANDLE libUserEnv = NULL;
static HANDLE libAdvApi32 = NULL;
static HANDLE libShell32 = NULL;
static DWORD (WINAPI *pGetModuleFileNameW)(HMODULE, LPWCH, DWORD);
static BOOL (WINAPI *pGetUserProfileDirectoryW)(HANDLE, LPWSTR, LPDWORD);
static BOOL (WINAPI *pGetUserNameW)(LPWSTR, LPDWORD);
static DWORD (WINAPI *pGetFileAttributesW)(LPCWSTR);
static HANDLE (WINAPI *pFindFirstFileW)(LPCWSTR, LPWIN32_FIND_DATAW);
static BOOL (WINAPI *pFindNextFileW)(HANDLE, LPWIN32_FIND_DATAW);
static DWORD (WINAPI *pGetCurrentDirectoryW)(DWORD, LPWSTR);
static BOOL (WINAPI *pDeleteFileW)(LPCWSTR);
static BOOL (WINAPI *pRemoveDirectoryW)(LPCWSTR);
static BOOL (WINAPI *pCreateDirectoryW)(LPCWSTR, LPSECURITY_ATTRIBUTES);
static BOOL (WINAPI *pGetFileAttributesExA)
(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
static BOOL (WINAPI *pGetFileAttributesExW)
(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
static DWORD (WINAPI *pFormatMessageW)
(DWORD, LPCVOID, DWORD, DWORD, LPWSTR, DWORD, va_list *);
static HANDLE (WINAPI *pCreateFileW)
(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
static HRESULT (WINAPI *pSHGetFolderPathA)(HWND, int, HANDLE, DWORD, LPTSTR);
static BOOL (WINAPI *pMoveFileW)(LPCWSTR, LPCWSTR);
static void (WINAPI *pOutputDebugStringW)(LPCWSTR);
// Fallbacks for missing Unicode functions on Win95/98/ME. These are filled
// into the function pointers if looking up the real Unicode entry points
// in the system DLLs fails, so they're never used on WinNT/XP/Vista/etc.
// They make an earnest effort to convert to/from UTF-8 and UCS-2 to
// the user's current codepage.
static BOOL WINAPI fallbackGetUserNameW(LPWSTR buf, LPDWORD len)
{
const DWORD cplen = *len;
char *cpstr = smallAlloc(cplen);
BOOL retval = GetUserNameA(cpstr, len);
if (buf != NULL)
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cpstr, cplen, buf, *len);
smallFree(cpstr);
return retval;
} // fallbackGetUserNameW
static DWORD WINAPI fallbackFormatMessageW(DWORD dwFlags, LPCVOID lpSource,
DWORD dwMessageId, DWORD dwLangId,
LPWSTR lpBuf, DWORD nSize,
va_list *Arguments)
{
char *cpbuf = (char *) smallAlloc(nSize);
DWORD retval = FormatMessageA(dwFlags, lpSource, dwMessageId, dwLangId,
cpbuf, nSize, Arguments);
if (retval > 0)
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,lpBuf,nSize);
smallFree(cpbuf);
return retval;
} // fallbackFormatMessageW
static DWORD WINAPI fallbackGetModuleFileNameW(HMODULE hMod, LPWCH lpBuf,
DWORD nSize)
{
char *cpbuf = (char *) smallAlloc(nSize);
DWORD retval = GetModuleFileNameA(hMod, cpbuf, nSize);
if (retval > 0)
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,lpBuf,nSize);
smallFree(cpbuf);
return retval;
} // fallbackGetModuleFileNameW
static DWORD WINAPI fallbackGetFileAttributesW(LPCWSTR fname)
{
DWORD retval = 0;
const int buflen = (int) (wStrLen(fname) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
retval = GetFileAttributesA(cpstr);
smallFree(cpstr);
return retval;
} // fallbackGetFileAttributesW
static DWORD WINAPI fallbackGetCurrentDirectoryW(DWORD buflen, LPWSTR buf)
{
DWORD retval = 0;
char *cpbuf = NULL;
if (buf != NULL)
cpbuf = (char *) smallAlloc(buflen);
retval = GetCurrentDirectoryA(buflen, cpbuf);
if (cpbuf != NULL)
{
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,buf,buflen);
smallFree(cpbuf);
} // if
return retval;
} // fallbackGetCurrentDirectoryW
static BOOL WINAPI fallbackRemoveDirectoryW(LPCWSTR dname)
{
BOOL retval = 0;
const int buflen = (int) (wStrLen(dname) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, dname, buflen, cpstr, buflen, NULL, NULL);
retval = RemoveDirectoryA(cpstr);
smallFree(cpstr);
return retval;
} // fallbackRemoveDirectoryW
static BOOL WINAPI fallbackCreateDirectoryW(LPCWSTR dname,
LPSECURITY_ATTRIBUTES attr)
{
BOOL retval = 0;
const int buflen = (int) (wStrLen(dname) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, dname, buflen, cpstr, buflen, NULL, NULL);
retval = CreateDirectoryA(cpstr, attr);
smallFree(cpstr);
return retval;
} // fallbackCreateDirectoryW
static BOOL WINAPI fallbackDeleteFileW(LPCWSTR fname)
{
BOOL retval = 0;
const int buflen = (int) (wStrLen(fname) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
retval = DeleteFileA(cpstr);
smallFree(cpstr);
return retval;
} // fallbackDeleteFileW
static HANDLE WINAPI fallbackCreateFileW(LPCWSTR fname,
DWORD dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttrs,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttrs, HANDLE hTemplFile)
{
HANDLE retval;
const int buflen = (int) (wStrLen(fname) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
retval = CreateFileA(cpstr, dwDesiredAccess, dwShareMode, lpSecurityAttrs,
dwCreationDisposition, dwFlagsAndAttrs, hTemplFile);
smallFree(cpstr);
return retval;
} // fallbackCreateFileW
static BOOL WINAPI fallbackMoveFileW(LPCWSTR src, LPCWSTR dst)
{
BOOL retval;
const int srcbuflen = (int) (wStrLen(src) + 1);
char *srccpstr = (char *) smallAlloc(srcbuflen);
const int dstbuflen = (int) (wStrLen(dst) + 1);
char *dstcpstr = (char *) smallAlloc(dstbuflen);
WideCharToMultiByte(CP_ACP,0,src,srcbuflen,srccpstr,srcbuflen,NULL,NULL);
WideCharToMultiByte(CP_ACP,0,dst,dstbuflen,dstcpstr,dstbuflen,NULL,NULL);
retval = MoveFileA(srccpstr, dstcpstr);
smallFree(srccpstr);
smallFree(dstcpstr);
return retval;
} // fallbackMoveFileW
static void WINAPI fallbackOutputDebugStringW(LPCWSTR str)
{
const int buflen = (int) (wStrLen(str) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, str, buflen, cpstr, buflen, NULL, NULL);
OutputDebugStringA(cpstr);
smallFree(cpstr);
} // fallbackOutputDebugStringW
// A blatant abuse of pointer casting...
static int symLookup(HMODULE dll, void **addr, const char *sym)
{
return ((*addr = GetProcAddress(dll, sym)) != NULL);
} // symLookup
static boolean findApiSymbols(void)
{
const boolean osHasUnicode = !osIsWin9x;
HMODULE dll = NULL;
#define LOOKUP_NOFALLBACK(x, reallyLook) { \
if (reallyLook) \
symLookup(dll, (void **) &p##x, #x); \
else \
p##x = NULL; \
}
#define LOOKUP(x, reallyLook) { \
if ((!reallyLook) || (!symLookup(dll, (void **) &p##x, #x))) \
p##x = fallback##x; \
}
// Apparently Win9x HAS the Unicode entry points, they just don't WORK.
// ...so don't look them up unless we're on NT+. (see osHasUnicode.)
dll = libUserEnv = LoadLibraryA("userenv.dll");
if (dll != NULL)
LOOKUP_NOFALLBACK(GetUserProfileDirectoryW, osHasUnicode);
// !!! FIXME: what do they call advapi32.dll on Win64?
dll = libAdvApi32 = LoadLibraryA("advapi32.dll");
if (dll != NULL)
LOOKUP(GetUserNameW, osHasUnicode);
// !!! FIXME: what do they call kernel32.dll on Win64?
dll = libKernel32 = LoadLibraryA("kernel32.dll");
if (dll != NULL)
{
LOOKUP_NOFALLBACK(GetFileAttributesExA, 1);
LOOKUP_NOFALLBACK(GetFileAttributesExW, osHasUnicode);
LOOKUP_NOFALLBACK(FindFirstFileW, osHasUnicode);
LOOKUP_NOFALLBACK(FindNextFileW, osHasUnicode);
LOOKUP(GetModuleFileNameW, osHasUnicode);
LOOKUP(FormatMessageW, osHasUnicode);
LOOKUP(GetFileAttributesW, osHasUnicode);
LOOKUP(GetCurrentDirectoryW, osHasUnicode);
LOOKUP(CreateDirectoryW, osHasUnicode);
LOOKUP(RemoveDirectoryW, osHasUnicode);
LOOKUP(CreateFileW, osHasUnicode);
LOOKUP(DeleteFileW, osHasUnicode);
LOOKUP(MoveFileW, osHasUnicode);
LOOKUP(OutputDebugStringW, osHasUnicode);
} // if
// !!! FIXME: what do they call shell32.dll on Win64?
dll = libShell32 = LoadLibraryA("shell32.dll");
if (dll != NULL)
LOOKUP_NOFALLBACK(SHGetFolderPathA, 1);
#undef LOOKUP_NOFALLBACK
#undef LOOKUP
return true;
} // findApiSymbols
// ok, now the actual platform layer implementation...
boolean MojoPlatform_istty(void)
{
return (GetConsoleWindow() != 0);
} // MojoPlatform_istty
boolean MojoPlatform_launchBrowser(const char *url)
{
// msdn says:
// "Returns a value greater than 32 if successful, or an error value that
// is less than or equal to 32 otherwise."
return (((int) ShellExecuteA(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL)) > 32);
} // MojoPlatform_launchBrowser
boolean MojoPlatform_installDesktopMenuItem(const char *data)
{
// !!! FIXME: write me.
STUBBED("desktop menu support");
return false;
} // MojoPlatform_installDesktopMenuItem
boolean MojoPlatform_uninstallDesktopMenuItem(const char *data)
{
// !!! FIXME: write me.
STUBBED("desktop menu support");
return false;
} // MojoPlatform_uninstallDesktopMenuItem
int MojoPlatform_exec(const char *cmd)
{
STUBBED("exec");
return 127;
} // MojoPlatform_exec
int MojoPlatform_runScript(const char *script, boolean devnull, const char **argv)
{
STUBBED("runScript");
return 0;
}
boolean MojoPlatform_spawnTerminal(void)
{
assert(!MojoPlatform_istty());
if (AllocConsole())
{
int hCrt;
FILE *hf;
hCrt = _open_osfhandle((long) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "w" );
*stdout = *hf;
setvbuf( stdout, NULL, _IONBF, 0 );
hCrt = _open_osfhandle((long) GetStdHandle(STD_ERROR_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "w" );
*stderr = *hf;
setvbuf( stderr, NULL, _IONBF, 0 );
hCrt = _open_osfhandle((long) GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "r" );
*stdin = *hf;
setvbuf( stdin, NULL, _IONBF, 0 );
return true;
} // if
return false;
} // MojoPlatform_spawnTerminal
uint8 *MojoPlatform_decodeImage(const uint8 *data, uint32 size,
uint32 *w, uint32 *h)
{
return NULL; // !!! FIXME: try IPicture?
} // MojoPlatform_decodeImage
char *MojoPlatform_currentWorkingDir(void)
{
char *retval = NULL;
WCHAR *wbuf = NULL;
DWORD buflen = 0;
buflen = pGetCurrentDirectoryW(buflen, NULL);
wbuf = (WCHAR *) smallAlloc((buflen + 2) * sizeof (WCHAR));
BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
pGetCurrentDirectoryW(buflen, wbuf);
if (wbuf[buflen - 2] == '\\')
wbuf[buflen - 1] = '\0'; // just in case...
else
{
wbuf[buflen - 1] = '\\';
wbuf[buflen] = '\0';
} // else
retval = unicodeToUtf8Heap(wbuf);
smallFree(wbuf);
return retval;
} // MojoPlatform_currentWorkingDir
char *MojoPlatform_readlink(const char *linkname)
{
STUBBED("should use symlinks (reparse points) on Vista");
return NULL;
} // MojoPlatform_readlink
// !!! FIXME: this code sort of sucks.
char *MojoPlatform_realpath(const char *path)
{
// !!! FIXME: this should return NULL if (path) doesn't exist?
// !!! FIXME: Need to handle symlinks in Vista...
// !!! FIXME: try GetFullPathName() instead?
// this function should be UTF-8 clean.
char *retval = NULL;
char *p = NULL;
if ((path == NULL) || (*path == '\0'))
return NULL;
retval = (char *) xmalloc(MAX_PATH);
// If in \\server\path format, it's already an absolute path.
// We'll need to check for "." and ".." dirs, though, just in case.
if ((path[0] == '\\') && (path[1] == '\\'))
strcpy(retval, path);
else
{
char *currentDir = MojoPlatform_currentWorkingDir();
if (currentDir == NULL)
{
free(retval);
return NULL;
} // if
if (path[1] == ':') // drive letter specified?
{
// Apparently, "D:mypath" is the same as "D:\\mypath" if
// D: is not the current drive. However, if D: is the
// current drive, then "D:mypath" is a relative path. Ugh.
if (path[2] == '\\') // maybe an absolute path?
strcpy(retval, path);
else // definitely an absolute path.
{
if (path[0] == currentDir[0]) // current drive; relative.
{
strcpy(retval, currentDir);
strcat(retval, path + 2);
} // if
else // not current drive; absolute.
{
retval[0] = path[0];
retval[1] = ':';
retval[2] = '\\';
strcpy(retval + 3, path + 2);
} // else
} // else
} // if
else // no drive letter specified.
{
if (path[0] == '\\') // absolute path.
{
retval[0] = currentDir[0];
retval[1] = ':';
strcpy(retval + 2, path);
} // if
else
{
strcpy(retval, currentDir);
strcat(retval, path);
} // else
} // else
free(currentDir);
} // else
// (whew.) Ok, now take out "." and ".." path entries...
p = retval;
while ( (p = strstr(p, "\\.")) != NULL)
{
// it's a "." entry that doesn't end the string.
if (p[2] == '\\')
memmove(p + 1, p + 3, strlen(p + 3) + 1);
// it's a "." entry that ends the string.
else if (p[2] == '\0')
p[0] = '\0';
// it's a ".." entry.
else if (p[2] == '.')
{
char *prevEntry = p - 1;
while ((prevEntry != retval) && (*prevEntry != '\\'))
prevEntry--;
if (prevEntry == retval) // make it look like a "." entry.
memmove(p + 1, p + 2, strlen(p + 2) + 1);
else
{
if (p[3] != '\0') // doesn't end string.
*prevEntry = '\0';
else // ends string.
memmove(prevEntry + 1, p + 4, strlen(p + 4) + 1);
p = prevEntry;
} // else
} // else if
else
{
p++; // look past current char.
} // else
} // while
// shrink the retval's memory block if possible...
return (char *) xrealloc(retval, strlen(retval) + 1);
} // MojoPlatform_realpath
char *MojoPlatform_appBinaryPath(void)
{
DWORD buflen = 64;
LPWSTR modpath = NULL;
char *retval = NULL;
DWORD rc = 0;
while (true)
{
void *ptr = xrealloc(modpath, buflen * sizeof(WCHAR));
modpath = (LPWSTR) ptr;
rc = pGetModuleFileNameW(NULL, modpath, buflen);
if (rc == 0)
{
free(modpath);
return NULL;
} // if
if (rc < buflen)
{
buflen = rc;
break;
} // if
buflen *= 2;
} // while
if (buflen > 0) // just in case...
{
WCHAR *ptr = (modpath + buflen) - 1;
while (ptr != modpath)
{
if (*ptr == '\\')
break;
ptr--;
} // while
if ((ptr != modpath) || (*ptr == '\\')) // should always be true...
{
*(ptr + 1) = '\0'; // chop off filename.
retval = unicodeToUtf8Heap(modpath);
} // else
} // else
free(modpath);
return retval;
} // MojoPlatform_appBinaryPath
// Try to make use of GetUserProfileDirectoryW(), which isn't available on
// some common variants of Win32. If we can't use this, we just punt and
// use the binary path for the user dir, too.
//
// On success, module-scope variable (userDir) will have a pointer to
// a malloc()'d string of the user's profile dir, and a non-zero value is
// returned. If we can't determine the profile dir, (userDir) will
// be NULL, and zero is returned.
char *MojoPlatform_homedir(void)
{
char *userDir = NULL;
// GetUserProfileDirectoryW() is only available on NT 4.0 and later.
// This means Win95/98/ME (and CE?) users have to do without, so for
// them, we'll default to the base directory when we can't get the
// function pointer. Since this is originally an NT API, we don't
// offer a non-Unicode fallback.
if (pGetUserProfileDirectoryW != NULL)
{
HANDLE accessToken = NULL; // Security handle to process
HANDLE processHandle = GetCurrentProcess();
if (OpenProcessToken(processHandle, TOKEN_QUERY, &accessToken))
{
DWORD psize = 0;
WCHAR dummy = 0;
LPWSTR wstr = NULL;
BOOL rc = 0;
// Should fail. Will write the size of the profile path in
// psize. Also note that the second parameter can't be
// NULL or the function fails.
rc = pGetUserProfileDirectoryW(accessToken, &dummy, &psize);
if (rc == 0) // this should always be true!
{
// Allocate memory for the profile directory
wstr = (LPWSTR) smallAlloc(psize * sizeof (WCHAR));
if (wstr != NULL)
{
if (pGetUserProfileDirectoryW(accessToken, wstr, &psize))
userDir = unicodeToUtf8Heap(wstr);
smallFree(wstr);
} // else
} // if
CloseHandle(accessToken);
} // if
} // if
if (userDir == NULL) // couldn't get profile for some reason.
{
// Might just be a non-NT system; try SHGetFolderPathA()...
if (pSHGetFolderPathA != NULL) // can be NULL if IE5+ isn't installed!
{
char shellPath[MAX_PATH];
HRESULT status = pSHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL,
SHGFP_TYPE_CURRENT, shellPath);
if (SUCCEEDED(status))
userDir = codepageToUtf8Heap(shellPath);
} // if
} // if
if (userDir == NULL) // Still nothing?!
{
// this either means we had a catastrophic failure, or we're on a
// Win95 system without at least Internet Explorer 5.0. Bleh!
userDir = xstrdup("C:\\My Documents"); // good luck with that.
} // if
return userDir;
} // MojoPlatform_homedir
// This implementation is a bit naive.
char *MojoPlatform_locale(void)
{
char lang[16];
char country[16];
const int langrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
LOCALE_SISO639LANGNAME,
lang, sizeof (lang));
const int ctryrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
LOCALE_SISO3166CTRYNAME,
country, sizeof (country));
// Win95 systems will fail, because they don't have LOCALE_SISO*NAME ...
if ((langrc != 0) && (ctryrc != 0))
return format("%0_%1", lang, country);
return NULL;
} // MojoPlatform_locale
char *MojoPlatform_osType(void)
{
if (osIsWin9x)
return xstrdup("win9x");
else
return xstrdup("winnt");
return NULL;
} // MojoPlatform_ostype
char *MojoPlatform_osVersion(void)
{
return format("%0.%1.%2",
numstr(osMajorVer),
numstr(osMinorVer),
numstr(osBuildVer));
} // MojoPlatform_osversion
char *MojoPlatform_osMachine(void)
{
// !!! FIXME: return "x86" for win32 and "x64" (bleh!) for win64.
return NULL;
} // MojoPlatform_osMachine
void MojoPlatform_sleep(uint32 ticks)
{
Sleep(ticks);
} // MojoPlatform_sleep
uint32 MojoPlatform_ticks(void)
{
return GetTickCount() - startupTime;
} // MojoPlatform_ticks
void MojoPlatform_die(void)
{
STUBBED("Win32 equivalent of _exit()?");
_exit(86);
} // MojoPlatform_die
boolean MojoPlatform_unlink(const char *fname)
{
int retval = 0;
LPWSTR wpath;
UTF8_TO_UNICODE_STACK_MACRO(wpath, fname);
if (pGetFileAttributesW(wpath) == FILE_ATTRIBUTE_DIRECTORY)
retval = (pRemoveDirectoryW(wpath) != 0);
else
retval = (pDeleteFileW(wpath) != 0);
smallFree(wpath);
return retval;
} // MojoPlatform_unlink
boolean MojoPlatform_symlink(const char *src, const char *dst)
{
STUBBED("Vista has symlink support");
return false;
} // MojoPlatform_symlink
boolean MojoPlatform_mkdir(const char *path, uint16 perms)
{
// !!! FIXME: error if already exists?
// !!! FIXME: perms?
WCHAR *wpath;
DWORD rc;
UTF8_TO_UNICODE_STACK_MACRO(wpath, path);
rc = pCreateDirectoryW(wpath, NULL);
smallFree(wpath);
return (rc != 0);
} // MojoPlatform_mkdir
boolean MojoPlatform_rename(const char *src, const char *dst)
{
WCHAR *srcwpath;
WCHAR *dstwpath;
BOOL rc;
MojoPlatform_unlink(dst); // to match Unix rename()...
UTF8_TO_UNICODE_STACK_MACRO(srcwpath, src);
UTF8_TO_UNICODE_STACK_MACRO(dstwpath, dst);
rc = pMoveFileW(srcwpath, dstwpath);
smallFree(srcwpath);
smallFree(dstwpath);
return (rc != 0);
} // MojoPlatform_rename
boolean MojoPlatform_exists(const char *dir, const char *fname)
{
WCHAR *wpath;
char *fullpath = NULL;
boolean retval = false;
if (fname == NULL)
fullpath = xstrdup(dir);
else
{
const size_t len = strlen(dir) + strlen(fname) + 1;
fullpath = (char *) xmalloc(len);
snprintf(fullpath, len, "%s\\%s", dir, fname);
} // else
UTF8_TO_UNICODE_STACK_MACRO(wpath, fullpath);
retval = (pGetFileAttributesW(wpath) != INVALID_FILE_ATTRIBUTES);
smallFree(wpath);
free(fullpath);
return retval;
} // MojoPlatform_exists
boolean MojoPlatform_writable(const char *fname)
{
boolean retval = false;
DWORD attr = 0;
WCHAR *wpath;
UTF8_TO_UNICODE_STACK_MACRO(wpath, fname);
attr = pGetFileAttributesW(wpath);
smallFree(wpath);
if (attr != INVALID_FILE_ATTRIBUTES)
retval = ((attr & FILE_ATTRIBUTE_READONLY) == 0);
return retval;
} // MojoPlatform_writable
boolean MojoPlatform_isdir(const char *dir)