summaryrefslogtreecommitdiff
path: root/tier0/xbox/xbox_win32stubs.cpp
blob: 95d0b5365ff483d65670af420151ceebc72ae0fd (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: XBox win32 replacements - Mocks trivial windows flow
//
//=============================================================================//

#include "pch_tier0.h"
#include "xbox/xbox_win32stubs.h"
#include "tier0/memdbgon.h"

// On the 360, threads can run on any of 6 logical processors
DWORD g_dwProcessAffinityMask = 0x3F;

#define HWND_MAGIC	0x12345678

struct xWndClass_t
{
	char*			pClassName;
	WNDPROC			wndProc;
	xWndClass_t*	pNext;
};

struct xWnd_t
{
	xWndClass_t*	pWndClass;
	int				x;
	int				y;
	int				w;
	int				h;
	long			windowLongs[GWL_MAX];
	int				show;
	int				nMagic;
	xWnd_t*			pNext;
};

static xWndClass_t*	g_pWndClasses;
static xWnd_t*		g_pWnds;
static HWND			g_focusWindow;
 
inline bool IsWndValid( HWND hWnd )
{
	if ( !hWnd || ((xWnd_t*)hWnd)->nMagic != HWND_MAGIC )
		return false;
	return true;
}

int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
	XBX_Error( lpText );
	Assert( 0 );

	return (0);
}

LONG GetWindowLong(HWND hWnd, int nIndex)
{
	LONG	oldLong;

	if ( !IsWndValid( hWnd ) )
		return 0;

	switch (nIndex)
	{
		case GWL_WNDPROC:
		case GWL_USERDATA:
		case GWL_STYLE:
		case GWL_EXSTYLE:
			oldLong = ((xWnd_t*)hWnd)->windowLongs[nIndex];
			break;
		default:
			// not implemented
			Assert( 0 );
			return 0;
	}

	return oldLong;
}

LONG_PTR GetWindowLongPtr(HWND hWnd, int nIndex)
{
	UINT idx;

	switch ( nIndex )
	{
	case GWLP_WNDPROC:
		idx = GWL_WNDPROC;
		break;
	case GWLP_USERDATA:
		idx = GWL_USERDATA;
		break;
	default:
		// not implemented
		Assert(0);
		return 0;
	}

	return GetWindowLong( hWnd, idx );
}

LONG_PTR GetWindowLongPtrW(HWND hWnd, int nIndex)
{
	AssertMsg( false, "GetWindowLongPtrW does not exist on Xbox 360." );
	return GetWindowLongPtr( hWnd, nIndex );
}


LONG SetWindowLong(HWND hWnd, int nIndex, LONG dwNewLong)
{
	LONG	oldLong;

	if ( !IsWndValid( hWnd ) )
		return 0;

	switch ( nIndex )
	{
		case GWL_WNDPROC:
		case GWL_USERDATA:
		case GWL_STYLE:
			oldLong = ((xWnd_t*)hWnd)->windowLongs[nIndex];
			((xWnd_t*)hWnd)->windowLongs[nIndex] = dwNewLong;
			break;
		default:
			// not implemented
			Assert( 0 );
			return 0;
	}

	return oldLong;
}

LONG_PTR SetWindowLongPtr(HWND hWnd, int nIndex, LONG_PTR dwNewLong)
{
	UINT idx;

	switch ( nIndex )
	{
	case GWLP_WNDPROC:
		idx = GWL_WNDPROC;
		break;
	case GWLP_USERDATA:
		idx = GWL_USERDATA;
		break;
	default:
		// not implemented
		Assert( 0 );
		return 0;
	}

	return SetWindowLong( hWnd, idx, dwNewLong );
}

LONG_PTR SetWindowLongPtrW(HWND hWnd, int nIndex, LONG_PTR dwNewLong)
{
	AssertMsg( false, "SetWindowLongPtrW does not exist on Xbox 360." );
	return SetWindowLongPtr( hWnd, nIndex, dwNewLong  );
}

HWND CreateWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{
	// find classname
	xWndClass_t* pWndClass = g_pWndClasses;
	while ( pWndClass )
	{
		if ( !stricmp( lpClassName, pWndClass->pClassName ) )
			break;
		pWndClass = pWndClass->pNext;
	}
	if ( !pWndClass )
	{
		// no such class
		return (HWND)NULL;
	}

	// allocate and setup
	xWnd_t* pWnd = new xWnd_t;
	memset( pWnd, 0, sizeof(xWnd_t) );
	pWnd->pWndClass = pWndClass;
	pWnd->windowLongs[GWL_WNDPROC] = (LONG)pWndClass->wndProc;
	pWnd->windowLongs[GWL_STYLE] = dwStyle;
	pWnd->x = x;
	pWnd->y = y;
	pWnd->w = nWidth;
	pWnd->h = nHeight;
	pWnd->nMagic = HWND_MAGIC;

	// link into list
	pWnd->pNext = g_pWnds;
	g_pWnds = pWnd;

	// force the focus
	g_focusWindow = (HWND)pWnd;

	// send the expected message sequence
	SendMessage( (HWND)pWnd, WM_CREATE, 0, 0 );
	SendMessage( (HWND)pWnd, WM_ACTIVATEAPP, TRUE, 0 );

	return (HWND)pWnd;
}

HWND CreateWindowEx(DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{
	return CreateWindow( lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam );
}

BOOL DestroyWindow( HWND hWnd )
{
	if ( !IsWndValid( hWnd ) )
		return FALSE;

	xWnd_t*	pPrev = g_pWnds;
	xWnd_t*	pCur = g_pWnds;

	while ( pCur )
	{
		if ( pCur == (xWnd_t*)hWnd )
		{
			if ( pPrev == g_pWnds )
			{
				// at head of list, fixup
				g_pWnds = pCur->pNext;
			}
			else
			{
				// remove from chain
				pPrev->pNext = pCur->pNext;
			}
			pCur->nMagic = 0;
			delete pCur;
			break;
		}

		// advance through list
		pPrev = pCur;
		pCur = pCur->pNext;
	}

	return TRUE;
}

ATOM RegisterClassEx(CONST WNDCLASSEX *lpwcx)
{
	// create
	xWndClass_t* pWndClass = new xWndClass_t;
	memset(pWndClass, 0, sizeof(xWndClass_t));
	pWndClass->pClassName = new char[strlen(lpwcx->lpszClassName)+1];
	strcpy(pWndClass->pClassName, lpwcx->lpszClassName);
	pWndClass->wndProc = lpwcx->lpfnWndProc;

	// insert into list
	pWndClass->pNext = g_pWndClasses;
	g_pWndClasses = pWndClass;

	return (ATOM)pWndClass;
}

ATOM RegisterClass(CONST WNDCLASS *lpwc)
{
	// create
	xWndClass_t* pWndClass = new xWndClass_t;
	memset(pWndClass, 0, sizeof(xWndClass_t));
	pWndClass->pClassName = new char[strlen(lpwc->lpszClassName)+1];
	strcpy(pWndClass->pClassName, lpwc->lpszClassName);
	pWndClass->wndProc = lpwc->lpfnWndProc;

	// insert into list
	pWndClass->pNext = g_pWndClasses;
	g_pWndClasses = pWndClass;

	return (ATOM)pWndClass;
}

HWND GetFocus(VOID)
{
	if ( !IsWndValid( g_focusWindow ) )
		return NULL;

	return g_focusWindow;
}

HWND SetFocus( HWND hWnd )
{
	HWND hOldFocus = g_focusWindow;

	if ( IsWndValid( hWnd ) )
	{
		g_focusWindow = hWnd;
	}

	return hOldFocus;
}


LRESULT CallWindowProc(WNDPROC lpPrevWndFunc, HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	return (lpPrevWndFunc(hWnd, Msg, wParam, lParam));
}

int GetSystemMetrics(int nIndex)
{
	XVIDEO_MODE videoMode;
	XGetVideoMode( &videoMode );
	// default to having the backbuffer the same as the mode resolution.
	int nFrameBufferWidth, nFrameBufferHeight;
	nFrameBufferWidth =  videoMode.dwDisplayWidth;
	nFrameBufferHeight = videoMode.dwDisplayHeight;

	// override for cases where we need to have a different backbuffer either for memory reasons
	// or for dealing with anamorphic modes.
	if ( !videoMode.fIsWideScreen && videoMode.dwDisplayWidth == 640 && videoMode.dwDisplayHeight == 576 )
	{
		// PAL normal
		nFrameBufferWidth = 640;
		nFrameBufferHeight = 480;
	}
	else if ( videoMode.fIsWideScreen && videoMode.dwDisplayWidth == 640 && videoMode.dwDisplayHeight == 576 )
	{
		// PAL widescreen
		nFrameBufferWidth = 848;
		nFrameBufferHeight = 480;
	}
	else if ( videoMode.fIsWideScreen && videoMode.dwDisplayWidth == 640 && videoMode.dwDisplayHeight == 480 )
	{
		// anamorphic
		nFrameBufferWidth = 848;
		nFrameBufferHeight = 480;
	}
	else if ( videoMode.fIsWideScreen && videoMode.dwDisplayWidth == 1024 && videoMode.dwDisplayHeight == 768 )
	{
		// anamorphic
		nFrameBufferWidth = 1280;
		nFrameBufferHeight = 720;
	}
	else if ( videoMode.dwDisplayWidth == 1280 && videoMode.dwDisplayHeight == 760 )
	{
		nFrameBufferWidth = 1280;
		nFrameBufferHeight = 720;
	}
	else if ( videoMode.dwDisplayWidth == 1280 && videoMode.dwDisplayHeight == 768 )
	{
		nFrameBufferWidth = 1280;
		nFrameBufferHeight = 720;
	}
	else if ( !videoMode.fIsWideScreen && videoMode.dwDisplayWidth == 1280 && videoMode.dwDisplayHeight == 1024 )
	{
		nFrameBufferWidth = 1024;
		nFrameBufferHeight = 768;
	}
	else if ( videoMode.fIsWideScreen && videoMode.dwDisplayWidth == 1280 && videoMode.dwDisplayHeight == 1024 )
	{
		// anamorphic
		nFrameBufferWidth = 1280;
		nFrameBufferHeight = 720;
	}
	else if ( videoMode.dwDisplayWidth == 1360 && videoMode.dwDisplayHeight == 768 )
	{
		nFrameBufferWidth = 1280;
		nFrameBufferHeight = 720;
	}
	else if ( videoMode.dwDisplayWidth == 1920 && videoMode.dwDisplayHeight == 1080 )
	{
		nFrameBufferWidth = 1280;
		nFrameBufferHeight = 720;
	}

	switch ( nIndex )
	{
		case SM_CXFIXEDFRAME:
		case SM_CYFIXEDFRAME:
		case SM_CYSIZE:
			return 0;
		case SM_CXSCREEN:
			return nFrameBufferWidth;
		case SM_CYSCREEN:
			return nFrameBufferHeight;
	}

	// not implemented
	Assert( 0 );
	return 0;
}

BOOL ShowWindow(HWND hWnd, int nCmdShow)
{
	if ( !IsWndValid( hWnd ) )
		return FALSE;

	((xWnd_t*)hWnd)->show = nCmdShow;

	if ((nCmdShow == SW_SHOWDEFAULT) || (nCmdShow == SW_SHOWNORMAL) || (nCmdShow == SW_SHOW))
		g_focusWindow = hWnd;

	return TRUE;
}

LRESULT SendMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	if ( !IsWndValid( hWnd ) )
		return 0L;

	xWnd_t* pWnd    = (xWnd_t*)hWnd;
	WNDPROC wndProc = (WNDPROC)pWnd->windowLongs[GWL_WNDPROC];
	Assert( wndProc );
	LRESULT result  = wndProc(hWnd, Msg, wParam, lParam);

	return result;
}

LRESULT	SendMessageTimeout( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, UINT fuFlags, UINT uTimeout, PDWORD_PTR lpdwResult )
{
	*lpdwResult = SendMessage( hWnd, Msg, wParam, lParam );

	return -1;
}

BOOL GetClientRect(HWND hWnd, LPRECT lpRect)
{
	if ( !IsWndValid( hWnd ) )
		return FALSE;

	xWnd_t* pWnd = (xWnd_t*)hWnd;
	lpRect->left = 0;
	lpRect->top = 0;
	lpRect->right  = pWnd->w;
	lpRect->bottom = pWnd->h;

	return TRUE;
}

int GetDeviceCaps(HDC hdc, int nIndex)
{
	switch (nIndex)
	{
		case HORZRES:
			return GetSystemMetrics( SM_CXSCREEN );
		case VERTRES:
			return GetSystemMetrics( SM_CYSCREEN );
		case VREFRESH:
			return 60;  
	}

	Assert( 0 );
	return 0;
}

BOOL SetWindowPos( HWND hWnd, HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT uFlags )
{
	if ( !IsWndValid( hWnd ) )
		return FALSE;

	xWnd_t* pWnd = (xWnd_t*)hWnd;

	if ( !( uFlags & SWP_NOMOVE ) )
	{
		pWnd->x = x;
		pWnd->y = y;
	}

	if ( !( uFlags & SWP_NOSIZE ) )
	{
		pWnd->w = cx;
		pWnd->h = cy;
	}

	return TRUE;
}

int XBX_unlink( const char* filename )
{	
	bool bSuccess = DeleteFile( filename ) != 0;
	if ( !bSuccess )
	{
		if ( GetLastError() == ERROR_FILE_NOT_FOUND )
		{
			// not a real failure
			return 0;
		}
	}
	// 0 = sucess, -1 = failure
	return bSuccess ? 0 : -1;
}

int XBX_mkdir( const char *pszDir )
{
	char			dirPath[MAX_PATH];
	char*			ptr;
	BOOL			bSuccess;

	// prime and skip to first seperator after the drive path
	// must create directory one path at a time
	bSuccess = false;
	strcpy( dirPath, pszDir );
	ptr = strchr( dirPath, '\\' );
	while ( ptr )
	{		
		ptr = strchr( ptr+1, '\\' );
		if ( ptr )
		{
			*ptr = '\0';
			bSuccess = CreateDirectory( dirPath, XBOX_DONTCARE );
			if ( !bSuccess && GetLastError() == ERROR_ALREADY_EXISTS )
			{
				// not a real error
				bSuccess = true;
			}
			*ptr = '\\';
		}
	}

	return ( bSuccess ? 0 : -1 );
}

char *XBX_getcwd( char *buf, size_t size )
{
	if ( !buf )
	{
		buf = (char*)malloc( 4 );
	}
	strncpy( buf, "D:", size );
	return buf;
}

int XBX_access( const char *path, int mode )
{
	if ( !path )
	{
		return -1;
	}

	// get the fatx attributes
	DWORD dwAttr = GetFileAttributes( path );
	if ( dwAttr == (DWORD)-1 )
	{
		return -1;
	}

	if ( mode == 0 )
	{
		// is file exist?
		return 0;
	}
	else if ( mode == 2 )
	{
		// is file write only?
		return -1;
	}
	else if ( mode == 4 )
	{
		// is file read only?
		if ( dwAttr & FILE_ATTRIBUTE_READONLY )
			return 0;
		else
			return -1;
	}
	else if ( mode == 6 )
	{
		// is file read and write?
		if ( !( dwAttr & FILE_ATTRIBUTE_READONLY ) )
			return 0;
		else
			return -1;
	}

	return -1;
}

DWORD XBX_GetCurrentDirectory( DWORD nBufferLength, LPTSTR lpBuffer )
{
	XBX_getcwd( lpBuffer, nBufferLength );
	return strlen( lpBuffer );
}

DWORD XBX_GetModuleFileName( HMODULE hModule, LPTSTR lpFilename, DWORD nSize )
{
	int		len;
	char	*pStr;
	char	*pEnd;
	char	xexName[MAX_PATH];

	if ( hModule == GetModuleHandle( NULL ) )
	{
		// isolate xex of command line
		pStr = GetCommandLine();
		if ( pStr )
		{
			// cull possible quotes around xex
			if ( pStr[0] == '\"' )
			{
				pStr++;
				pEnd = strchr( pStr, '\"' );
				if ( !pEnd )
				{
					// no ending matching quote
					return 0;
				}
			}
			else
			{
				// find possible first argument
				pEnd = strchr( lpFilename, ' ' );
				if ( !pEnd )
				{
					pEnd = pStr+strlen( pStr );
				}
			}
			len = pEnd-pStr;
			memcpy( xexName, pStr, len );
			xexName[len] = '\0';

			len = _snprintf( lpFilename, nSize, "D:\\%s", xexName );
			if ( len == -1 )
				lpFilename[nSize-1] = '\0';

			return strlen( lpFilename );
		}
	}
	return 0;
}