-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathsys_mainwind.cpp
1744 lines (1470 loc) · 42.4 KB
/
sys_mainwind.cpp
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//===========================================================================//
#if defined( USE_SDL )
#undef PROTECTED_THINGS_ENABLE
#include "SDL.h"
#include "SDL_syswm.h"
#if defined( OSX )
#define DONT_DEFINE_BOOL
#include <objc/message.h>
#endif
#endif
#if defined( WIN32 ) && !defined( _X360 ) && !defined( DX_TO_GL_ABSTRACTION )
#include "winlite.h"
#include "xbox/xboxstubs.h"
#endif
#if defined( IS_WINDOWS_PC ) && !defined( USE_SDL )
#include <winsock.h>
#elif defined(_X360)
// nothing to include for 360
#elif defined(OSX)
#elif defined(POSIX) || defined(_WIN32)
#include "tier0/dynfunction.h"
#else
#error
#endif
#include "appframework/ilaunchermgr.h"
#include "igame.h"
#include "cl_main.h"
#include "host.h"
#include "quakedef.h"
#include "tier0/vcrmode.h"
#include "tier0/icommandline.h"
#include "ivideomode.h"
#include "gl_matsysiface.h"
#include "cdll_engine_int.h"
#include "vgui_baseui_interface.h"
#include "iengine.h"
#include "keys.h"
#include "VGuiMatSurface/IMatSystemSurface.h"
#include "tier3/tier3.h"
#include "sound.h"
#include "vgui_controls/Controls.h"
#include "vgui_controls/MessageDialog.h"
#include "sys_dll.h"
#include "inputsystem/iinputsystem.h"
#include "inputsystem/ButtonCode.h"
#ifdef WIN32
#undef WIN32_LEAN_AND_MEAN
#include "unicode/unicode.h"
#endif
#include "GameUI/IGameUI.h"
#include "matchmaking.h"
#include "sv_main.h"
#include "video/ivideoservices.h"
#include "sys.h"
#include "materialsystem/imaterial.h"
#if defined( _X360 )
#include "xbox/xbox_win32stubs.h"
#include "hl2orange.spa.h"
#endif
#if defined( LINUX )
#include "snd_dev_sdl.h"
#endif
#ifdef DBGFLAG_ASSERT
#define AssertExit( _exp ) Assert( _exp )
#define AssertExitF( _exp ) Assert( _exp )
#else
#define AssertExit( _exp ) if ( !( _exp ) ) return;
#define AssertExitF( _exp ) if ( !( _exp ) ) return false;
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
void S_BlockSound (void);
void S_UnblockSound (void);
void ClearIOStates( void );
//-----------------------------------------------------------------------------
// Game input events
//-----------------------------------------------------------------------------
enum GameInputEventType_t
{
IE_Close = IE_FirstAppEvent,
IE_WindowMove,
IE_AppActivated,
};
#ifdef WIN32
static IUnicodeWindows *unicode = NULL;
#endif
//-----------------------------------------------------------------------------
// Purpose: Main game interface, including message pump and window creation
//-----------------------------------------------------------------------------
class CGame : public IGame
{
public:
CGame( void );
virtual ~CGame( void );
bool Init( void *pvInstance );
bool Shutdown( void );
bool CreateGameWindow( void );
void DestroyGameWindow();
void SetGameWindow( void* hWnd );
// This is used in edit mode to override the default wnd proc associated w/
bool InputAttachToGameWindow();
void InputDetachFromGameWindow();
void PlayStartupVideos( void );
// This is the SDL_Window* under SDL, HWND otherwise.
void* GetMainWindow( void );
// This will be the HWND under D3D + Windows (both with and without SDL), SDL_Window* everywhere else.
void* GetMainDeviceWindow( void );
// This will be the HWND under Windows, the WindowRef under Mac, and (for now) NULL on Linux
void* GetMainWindowPlatformSpecificHandle( void );
void** GetMainWindowAddress( void );
void GetDesktopInfo( int &width, int &height, int &refreshrate );
void SetWindowXY( int x, int y );
void SetWindowSize( int w, int h );
void GetWindowRect( int *x, int *y, int *w, int *h );
bool IsActiveApp( void );
void SetCanPostActivateEvents( bool bEnable );
bool CanPostActivateEvents();
public:
#ifdef USE_SDL
void SetMainWindow( SDL_Window* window );
#else
#ifdef WIN32
LRESULT WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
#endif
void SetMainWindow( HWND window );
#endif
void SetActiveApp( bool active );
bool LoadUnicode();
void UnloadUnicode();
// Message handlers.
public:
void HandleMsg_WindowMove( const InputEvent_t &event );
void HandleMsg_ActivateApp( const InputEvent_t &event );
void HandleMsg_Close( const InputEvent_t &event );
// Call the appropriate HandleMsg_ function.
void DispatchInputEvent( const InputEvent_t &event );
// Dispatch all the queued up messages.
virtual void DispatchAllStoredGameMessages();
private:
void AppActivate( bool fActive );
void PlayVideoAndWait( const char *filename, bool bNeedHealthWarning = false); // plays a video file and waits till it's done to return. Can be interrupted by user.
private:
void AttachToWindow();
void DetachFromWindow();
#ifndef _X360
static const wchar_t CLASSNAME[];
#else
static const char CLASSNAME[];
#endif
bool m_bExternallySuppliedWindow;
#if defined( WIN32 )
HWND m_hWindow;
#if !defined( USE_SDL )
HINSTANCE m_hInstance;
// Stores a wndproc to chain message calls to
WNDPROC m_ChainedWindowProc;
RECT m_rcLastRestoredClientRect;
#endif
#endif
#if defined( USE_SDL )
SDL_Window *m_pSDLWindow;
#endif
int m_x;
int m_y;
int m_width;
int m_height;
bool m_bActiveApp;
CSysModule *m_hUnicodeModule;
bool m_bCanPostActivateEvents;
int m_iDesktopWidth, m_iDesktopHeight, m_iDesktopRefreshRate;
void UpdateDesktopInformation();
#ifdef WIN32
void UpdateDesktopInformation( WPARAM wParam, LPARAM lParam );
#endif
};
static CGame g_Game;
IGame *game = ( IGame * )&g_Game;
#if !defined( _X360 )
const wchar_t CGame::CLASSNAME[] = L"Valve001";
#else
const char CGame::CLASSNAME[] = "Valve001";
#endif
// In VCR playback mode, it sleeps this amount each frame.
int g_iVCRPlaybackSleepInterval = 0;
// During VCR playback, if this is true, then it'll pause at the end of each frame.
bool g_bVCRSingleStep = false;
bool g_bWaitingForStepKeyUp = false; // Used to prevent it from running frames while you hold the S key down.
bool g_bShowVCRPlaybackDisplay = true;
// These are all the windows messages that can change game state.
// See CGame::WindowProc for a description of how they work.
struct GameMessageHandler_t
{
int m_nEventType;
void (CGame::*pFn)( const InputEvent_t &event );
};
GameMessageHandler_t g_GameMessageHandlers[] =
{
{ IE_AppActivated, &CGame::HandleMsg_ActivateApp },
{ IE_WindowMove, &CGame::HandleMsg_WindowMove },
{ IE_Close, &CGame::HandleMsg_Close },
{ IE_Quit, &CGame::HandleMsg_Close },
};
void CGame::AppActivate( bool fActive )
{
// If text mode, force it to be active.
if ( g_bTextMode )
{
fActive = true;
}
// Don't bother if we're already in the correct state
if ( IsActiveApp() == fActive )
return;
// Don't let video modes changes queue up another activate event
SetCanPostActivateEvents( false );
#ifndef SWDS
if ( videomode )
{
if ( fActive )
{
videomode->RestoreVideo();
}
else
{
videomode->ReleaseVideo();
}
}
if ( host_initialized )
{
if ( fActive )
{
// Clear keyboard states (should be cleared already but...)
// VGui_ActivateMouse will reactivate the mouse soon.
ClearIOStates();
UpdateMaterialSystemConfig();
}
else
{
// Clear keyboard input and deactivate the mouse while we're away.
ClearIOStates();
if ( g_ClientDLL )
{
g_ClientDLL->IN_DeactivateMouse();
}
}
}
#endif // SWDS
SetActiveApp( fActive );
#ifdef _XBOX
if ( host_initialized )
{
ClearIOStates();
if ( fActive )
{
UpdateMaterialSystemConfig();
}
}
SetActiveApp( fActive );
#endif
// Allow queueing of activation events
SetCanPostActivateEvents( true );
}
void CGame::HandleMsg_WindowMove( const InputEvent_t &event )
{
game->SetWindowXY( event.m_nData, event.m_nData2 );
#ifndef SWDS
videomode->UpdateWindowPosition();
#endif
}
void CGame::HandleMsg_ActivateApp( const InputEvent_t &event )
{
AppActivate( event.m_nData ? true : false );
}
void CGame::HandleMsg_Close( const InputEvent_t &event )
{
if ( eng->GetState() == IEngine::DLL_ACTIVE )
{
eng->SetQuitting( IEngine::QUIT_TODESKTOP );
}
}
void CGame::DispatchInputEvent( const InputEvent_t &event )
{
switch( event.m_nType & 0xFFFF )
{
// Handle button events specially,
// since we have all manner of crazy filtering going on when dealing with them
case IE_ButtonPressed:
case IE_ButtonDoubleClicked:
case IE_ButtonReleased:
Key_Event( event );
break;
case IE_FingerDown:
case IE_FingerUp:
case IE_FingerMotion:
if( g_ClientDLL )
g_ClientDLL->IN_TouchEvent( event.m_nType, event.m_nData, event.m_nData2, event.m_nData3 );
default:
// Let vgui have the first whack at events
if ( g_pMatSystemSurface && g_pMatSystemSurface->HandleInputEvent( event ) )
break;
for ( int i=0; i < ARRAYSIZE( g_GameMessageHandlers ); i++ )
{
if ( g_GameMessageHandlers[i].m_nEventType == event.m_nType )
{
(this->*g_GameMessageHandlers[i].pFn)( event );
break;
}
}
break;
}
}
void CGame::DispatchAllStoredGameMessages()
{
#if !defined( NO_VCR )
if ( VCRGetMode() == VCR_Playback )
{
InputEvent_t event;
while ( VCRHook_PlaybackGameMsg( &event ) )
{
event.m_nTick = g_pInputSystem->GetPollTick();
DispatchInputEvent( event );
}
}
else
{
int nEventCount = g_pInputSystem->GetEventCount();
const InputEvent_t* pEvents = g_pInputSystem->GetEventData( );
for ( int i = 0; i < nEventCount; ++i )
{
VCRHook_RecordGameMsg( pEvents[i] );
DispatchInputEvent( pEvents[i] );
}
VCRHook_RecordEndGameMsg();
}
#else
int nEventCount = g_pInputSystem->GetEventCount();
const InputEvent_t* pEvents = g_pInputSystem->GetEventData( );
for ( int i = 0; i < nEventCount; ++i )
{
DispatchInputEvent( pEvents[i] );
}
#endif
}
void VCR_EnterPausedState()
{
// Turn this off in case they're in single-step mode.
g_bVCRSingleStep = false;
#ifdef WIN32
// This is cheesy, but GetAsyncKeyState is blocked (in protected_things.h)
// from being accidentally used, so we get it through it by getting its pointer directly.
static HINSTANCE hInst = LoadLibrary( "user32.dll" );
if ( !hInst )
return;
typedef SHORT (WINAPI *GetAsyncKeyStateFn)( int vKey );
static GetAsyncKeyStateFn pfn = (GetAsyncKeyStateFn)GetProcAddress( hInst, "GetAsyncKeyState" );
if ( !pfn )
return;
// In this mode, we enter a wait state where we only pay attention to R and Q.
while ( 1 )
{
if ( pfn( 'R' ) & 0x8000 )
break;
if ( pfn( 'Q' ) & 0x8000 )
TerminateProcess( GetCurrentProcess(), 1 );
if ( pfn( 'S' ) & 0x8000 )
{
if ( !g_bWaitingForStepKeyUp )
{
// Do a single step.
g_bVCRSingleStep = true;
g_bWaitingForStepKeyUp = true; // Don't do another single step until they release the S key.
break;
}
}
else
{
// Ok, they released the S key, so we'll process it next time the key goes down.
g_bWaitingForStepKeyUp = false;
}
Sleep( 2 );
}
#else
Assert( !"Impl me" );
#endif
}
#ifdef WIN32
void VCR_HandlePlaybackMessages(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
if ( uMsg == WM_KEYDOWN )
{
if ( wParam == VK_SUBTRACT || wParam == 0xbd )
{
g_iVCRPlaybackSleepInterval += 5;
}
else if ( wParam == VK_ADD || wParam == 0xbb )
{
g_iVCRPlaybackSleepInterval -= 5;
}
else if ( toupper( wParam ) == 'Q' )
{
TerminateProcess( GetCurrentProcess(), 1 );
}
else if ( toupper( wParam ) == 'P' )
{
VCR_EnterPausedState();
}
else if ( toupper( wParam ) == 'S' && !g_bVCRSingleStep )
{
g_bWaitingForStepKeyUp = true;
VCR_EnterPausedState();
}
else if ( toupper( wParam ) == 'D' )
{
g_bShowVCRPlaybackDisplay = !g_bShowVCRPlaybackDisplay;
}
g_iVCRPlaybackSleepInterval = clamp( g_iVCRPlaybackSleepInterval, 0, 500 );
}
}
//-----------------------------------------------------------------------------
// Calls the default window procedure
// FIXME: It would be nice to remove the need for this, which we can do
// if we can make unicode work when running inside hammer.
//-----------------------------------------------------------------------------
static LRESULT WINAPI CallDefaultWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if ( unicode )
return unicode->DefWindowProcW( hWnd, uMsg, wParam, lParam );
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
#endif
//-----------------------------------------------------------------------------
// Purpose: The user has accepted an invitation to a game, we need to detect if
// it's TF2 and restart properly if it is
//-----------------------------------------------------------------------------
void XBX_HandleInvite( DWORD nUserId )
{
#ifdef _X360
// Grab our invite info
XINVITE_INFO inviteInfo;
DWORD dwError = XInviteGetAcceptedInfo( nUserId, &inviteInfo );
if ( dwError != ERROR_SUCCESS )
return;
// We only care if we're asked to join an Orange Box session
if ( inviteInfo.dwTitleID != TITLEID_THE_ORANGE_BOX )
{
// Do the normal "we've been invited to a game" behavior
return;
}
// Otherwise, launch depending on our current MOD
if ( !Q_stricmp( GetCurrentMod(), "tf" ) )
{
// We're already running TF2, so just join the session
if ( nUserId != XBX_GetPrimaryUserId() )
{
// Switch users, the other had the invite
XBX_SetPrimaryUserId( nUserId );
}
// Kick off our join
g_pMatchmaking->JoinInviteSession( &(inviteInfo.hostInfo) );
}
else
{
// Save off our session ID for later retrieval
// NOTE: We may need to actually save off the inviter's XID and search for them later on if we took too long or the
// session they were a part of went away
XBX_SetInviteSessionId( inviteInfo.hostInfo.sessionID );
XBX_SetInvitedUserId( nUserId );
// Quit via the menu path "QuitNoConfirm"
EngineVGui()->SystemNotification( SYSTEMNOTIFY_INVITE_SHUTDOWN );
}
#endif //_X360
}
#if defined( WIN32 ) && !defined( USE_SDL )
//-----------------------------------------------------------------------------
// Main windows procedure
//-----------------------------------------------------------------------------
LRESULT CGame::WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT lRet = 0;
HDC hdc;
PAINTSTRUCT ps;
//
// NOTE: the way this function works is to handle all messages that just call through to
// Windows or provide data to it.
//
// Any messages that change the engine's internal state (like key events) are stored in a list
// and processed at the end of the frame. This is necessary for VCR mode to work correctly because
// Windows likes to pump messages during some of its API calls like SetWindowPos, and unless we add
// custom code around every Windows API call so VCR mode can trap the wndproc calls, VCR mode can't
// reproduce the calls to the wndproc.
//
if ( eng->GetQuitting() != IEngine::QUIT_NOTQUITTING )
return CallWindowProc( m_ChainedWindowProc, hWnd, uMsg, wParam, lParam );
// If we're playing back, listen to a couple input things used to drive VCR mode
if ( VCRGetMode() == VCR_Playback )
{
VCR_HandlePlaybackMessages( hWnd, uMsg, wParam, lParam );
}
//
// Note: NO engine state should be changed in here while in VCR record or playback.
// We can send whatever we want to Windows, but if we change its state in here instead of
// in DispatchAllStoredGameMessages, the playback may not work because Windows messages
// are not deterministic, so you might get different messages during playback than you did during record.
//
InputEvent_t event;
memset( &event, 0, sizeof(event) );
event.m_nTick = g_pInputSystem->GetPollTick();
switch ( uMsg )
{
case WM_CREATE:
::SetForegroundWindow( hWnd );
break;
case WM_ACTIVATEAPP:
{
if ( CanPostActivateEvents() )
{
bool bActivated = ( wParam == 1 );
event.m_nType = IE_AppActivated;
event.m_nData = bActivated;
g_pInputSystem->PostUserEvent( event );
}
}
break;
case WM_POWERBROADCAST:
// Don't go into Sleep mode when running engine, we crash on resume for some reason (as
// do half of the apps I have running usually anyway...)
if ( wParam == PBT_APMQUERYSUSPEND )
{
Msg( "OS requested hibernation, ignoring request.\n" );
return BROADCAST_QUERY_DENY;
}
lRet = CallWindowProc( m_ChainedWindowProc, hWnd, uMsg, wParam, lParam );
break;
case WM_SYSCOMMAND:
if ( ( wParam == SC_MONITORPOWER ) || ( wParam == SC_KEYMENU ) || ( wParam == SC_SCREENSAVE ) )
return lRet;
if ( wParam == SC_CLOSE )
{
#if !defined( NO_VCR )
// handle the close message, but make sure
// it's not because we accidently hit ALT-F4
if ( HIBYTE(VCRHook_GetKeyState(VK_LMENU)) || HIBYTE(VCRHook_GetKeyState(VK_RMENU) ) )
return lRet;
#endif
Cbuf_Clear();
Cbuf_AddText( "quit\n" );
}
#ifndef SWDS
if ( VCRGetMode() == VCR_Disabled )
{
S_BlockSound();
S_ClearBuffer();
}
#endif
lRet = CallWindowProc( m_ChainedWindowProc, hWnd, uMsg, wParam, lParam );
#ifndef SWDS
if ( VCRGetMode() == VCR_Disabled )
{
S_UnblockSound();
}
#endif
break;
case WM_CLOSE:
// Handle close messages
event.m_nType = IE_Close;
g_pInputSystem->PostUserEvent( event );
return 0;
case WM_MOVE:
event.m_nType = IE_WindowMove;
event.m_nData = (short)LOWORD(lParam);
event.m_nData2 = (short)HIWORD(lParam);
g_pInputSystem->PostUserEvent( event );
break;
case WM_SIZE:
if ( wParam != SIZE_MINIMIZED )
{
// Update restored client rect
::GetClientRect( hWnd, &m_rcLastRestoredClientRect );
}
else
{
#ifndef _X360
// Fix the window rect to have same client area as it used to have
// before it got minimized
RECT rcWindow;
::GetWindowRect( hWnd, &rcWindow );
rcWindow.right = rcWindow.left + m_rcLastRestoredClientRect.right;
rcWindow.bottom = rcWindow.top + m_rcLastRestoredClientRect.bottom;
::AdjustWindowRect( &rcWindow, ::GetWindowLong( hWnd, GWL_STYLE ), FALSE );
::MoveWindow( hWnd, rcWindow.left, rcWindow.top,
rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top, FALSE );
#endif
}
break;
case WM_SYSCHAR:
// keep Alt-Space from happening
break;
case WM_COPYDATA:
// Hammer -> engine remote console command.
// Return true to indicate that the message was handled.
Cbuf_AddText( (const char *)(((COPYDATASTRUCT *)lParam)->lpData) );
Cbuf_AddText( "\n" );
lRet = 1;
break;
#if defined( _X360 )
case WM_XREMOTECOMMAND:
Cbuf_AddText( (const char*)lParam );
Cbuf_AddText( "\n" );
break;
case WM_SYS_STORAGEDEVICESCHANGED:
if ( !EngineVGui()->IsGameUIVisible() )
{
EngineVGui()->ActivateGameUI();
}
EngineVGui()->SystemNotification( SYSTEMNOTIFY_STORAGEDEVICES_CHANGED );
break;
case WM_XMP_PLAYBACKCONTROLLERCHANGED:
S_EnableMusic( lParam != 0 );
break;
case WM_LIVE_INVITE_ACCEPTED:
XBX_HandleInvite( LOWORD( lParam ) );
break;
case WM_SYS_SIGNINCHANGED:
if ( XUserGetSigninState( XBX_GetPrimaryUserId() ) == eXUserSigninState_NotSignedIn )
{
// X360TBD: User signed out - pause the game?
}
EngineVGui()->SystemNotification( lParam ? SYSTEMNOTIFY_USER_SIGNEDIN : SYSTEMNOTIFY_USER_SIGNEDOUT );
break;
case WM_SYS_UI:
if ( lParam )
{
// Don't activate it if it's already active (a sub window may be active)
// Multiplayer doesn't want the UI to appear, since it can't pause anyway
if ( !EngineVGui()->IsGameUIVisible() && g_ServerGlobalVariables.maxClients == 1 )
{
Cbuf_AddText( "gameui_activate" );
}
}
EngineVGui()->SystemNotification( lParam ? SYSTEMNOTIFY_XUIOPENING : SYSTEMNOTIFY_XUICLOSED );
break;
case WM_SYS_MUTELISTCHANGED:
g_pMatchmaking->UpdateMuteList();
break;
case WM_SYS_INPUTDEVICESCHANGED:
{
XINPUT_CAPABILITIES caps;
if ( XInputGetCapabilities( XBX_GetPrimaryUserId(), XINPUT_FLAG_GAMEPAD, &caps ) == ERROR_DEVICE_NOT_CONNECTED )
{
if ( EngineVGui()->IsGameUIVisible() == false )
{
EngineVGui()->ActivateGameUI();
}
}
}
break;
#endif
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
RECT rcClient;
GetClientRect( hWnd, &rcClient );
#ifndef SWDS
// Only renders stuff if running -noshaderapi
if ( videomode )
{
videomode->DrawNullBackground( hdc, rcClient.right, rcClient.bottom );
}
#endif
EndPaint(hWnd, &ps);
break;
#ifndef _X360
case WM_DISPLAYCHANGE:
if ( !m_iDesktopHeight || !m_iDesktopWidth )
{
UpdateDesktopInformation( wParam, lParam );
}
break;
#endif
case WM_IME_NOTIFY:
switch ( wParam )
{
default:
break;
#ifndef SWDS
case 14:
if ( !videomode->IsWindowedMode() )
return 0;
break;
#endif
}
return CallWindowProc( m_ChainedWindowProc, hWnd, uMsg, wParam, lParam);
default:
lRet = CallWindowProc( m_ChainedWindowProc, hWnd, uMsg, wParam, lParam );
break;
}
// return 0 if handled message, 1 if not
return lRet;
}
#elif defined(POSIX) || defined(_WIN32)
#else
#error
#endif
#if defined( WIN32 ) && !defined( USE_SDL )
//-----------------------------------------------------------------------------
// Creates the game window
//-----------------------------------------------------------------------------
static LRESULT WINAPI HLEngineWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
return g_Game.WindowProc( hWnd, uMsg, wParam, lParam );
}
#define DEFAULT_EXE_ICON 101
static void DoSomeSocketStuffInOrderToGetZoneAlarmToNoticeUs( void )
{
#ifdef IS_WINDOWS_PC
WSAData wsaData;
if ( ! WSAStartup( 0x0101, &wsaData ) )
{
SOCKET tmpSocket = socket( AF_INET, SOCK_DGRAM, 0 );
if ( tmpSocket != INVALID_SOCKET )
{
char Options[]={ 1 };
setsockopt( tmpSocket, SOL_SOCKET, SO_BROADCAST, Options, sizeof(Options));
char pszHostName[256];
gethostname( pszHostName, sizeof( pszHostName ) );
hostent *hInfo = gethostbyname( pszHostName );
if ( hInfo )
{
sockaddr_in myIpAddress;
memset( &myIpAddress, 0, sizeof( myIpAddress ) );
myIpAddress.sin_family = AF_INET;
myIpAddress.sin_port = htons( 27015 ); // our normal server port
myIpAddress.sin_addr.S_un.S_un_b.s_b1 = hInfo->h_addr_list[0][0];
myIpAddress.sin_addr.S_un.S_un_b.s_b2 = hInfo->h_addr_list[0][1];
myIpAddress.sin_addr.S_un.S_un_b.s_b3 = hInfo->h_addr_list[0][2];
myIpAddress.sin_addr.S_un.S_un_b.s_b4 = hInfo->h_addr_list[0][3];
if ( bind( tmpSocket, ( sockaddr * ) &myIpAddress, sizeof( myIpAddress ) ) != -1 )
{
if ( sendto( tmpSocket, pszHostName, 1, 0, ( sockaddr *) &myIpAddress, sizeof( myIpAddress ) ) == -1 )
{
// error?
}
}
}
closesocket( tmpSocket );
}
WSACleanup();
}
#endif
}
#endif
bool CGame::CreateGameWindow( void )
{
// get the window name
char windowName[256];
windowName[0] = 0;
KeyValues *modinfo = new KeyValues("ModInfo");
if (modinfo->LoadFromFile(g_pFileSystem, "gameinfo.txt"))
{
Q_strncpy( windowName, modinfo->GetString("game"), sizeof(windowName) );
}
if (!windowName[0])
{
Q_strncpy( windowName, "HALF-LIFE 2", sizeof(windowName) );
}
if ( IsOpenGL() )
{
#ifdef TOGLES
V_strcat( windowName, " - OpenGLES", sizeof( windowName ) );
#else
V_strcat( windowName, " - OpenGL", sizeof( windowName ) );
#endif
}
#if PIX_ENABLE || defined( PIX_INSTRUMENTATION )
// PIX_ENABLE/PIX_INSTRUMENTATION is a big slowdown (that should never be checked in, but sometimes is by accident), so add this to the Window title too.
V_strcat( windowName, " - PIX_ENABLE", sizeof( windowName ) );
#endif
const char *p = CommandLine()->ParmValue( "-window_name_suffix", "" );
if ( p && V_strlen( p ) )
{
V_strcat( windowName, " - ", sizeof( windowName ) );
V_strcat( windowName, p, sizeof( windowName ) );
}
#if defined( WIN32 ) && !defined( USE_SDL )
#ifndef SWDS
if ( IsPC() )
{
if ( !LoadUnicode() )
{
return false;
}
}
#if !defined( _X360 )
WNDCLASSW wc;
#else
WNDCLASS wc;
#endif
memset( &wc, 0, sizeof( wc ) );
wc.style = CS_OWNDC | CS_DBLCLKS;
wc.lpfnWndProc = static_cast<WNDPROC>(CallDefaultWindowProc);
wc.hInstance = m_hInstance;
wc.lpszClassName = CLASSNAME;
// find the icon file in the filesystem
if ( IsPC() )
{
char localPath[ MAX_PATH ];
if ( g_pFileSystem->GetLocalPath( "resource/game.ico", localPath, sizeof(localPath) ) )
{
g_pFileSystem->GetLocalCopy( localPath );
wc.hIcon = (HICON)::LoadImage(NULL, localPath, IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
}
else
{
wc.hIcon = (HICON)LoadIcon( GetModuleHandle( 0 ), MAKEINTRESOURCE( DEFAULT_EXE_ICON ) );
}
}
#ifndef SWDS
char const *pszGameType = modinfo->GetString( "type" );
if ( pszGameType && Q_stristr( pszGameType, "multiplayer" ) )
DoSomeSocketStuffInOrderToGetZoneAlarmToNoticeUs();
#endif
wchar_t uc[512];
if ( IsPC() )
{
::MultiByteToWideChar(CP_UTF8, 0, windowName, -1, uc, sizeof( uc ) / sizeof(wchar_t));
}
modinfo->deleteThis();
modinfo = NULL;
// Oops, we didn't clean up the class registration from last cycle which
// might mean that the wndproc pointer is bogus
#ifndef _X360
unicode->UnregisterClassW( CLASSNAME, m_hInstance );
// Register it again
unicode->RegisterClassW( &wc );
#else
RegisterClass( &wc );
#endif
// Note, it's hidden
DWORD style = WS_POPUP | WS_CLIPSIBLINGS;
// Give it a frame if we want a border
if ( videomode->IsWindowedMode() )
{
if( !CommandLine()->FindParm( "-noborder" ) )
{