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
|
/************************************************************************************
Filename : Win32_Platform.cpp
Content : Win32 implementation of Platform app infrastructure
Created : September 6, 2012
Authors : Andrew Reisse
Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
#include "Kernel/OVR_System.h"
#include "Kernel/OVR_Array.h"
#include "Kernel/OVR_String.h"
#include "Win32_Platform.h"
#include "Win32_Gamepad.h"
#include "../Render/Render_Device.h"
namespace OVR { namespace Platform { namespace Win32 {
PlatformCore::PlatformCore(Application* app, HINSTANCE hinst)
: Platform::PlatformCore(app), hWnd(NULL), hInstance(hinst), Quit(0), MMode(Mouse_Normal),
Cursor(0), Modifiers(0), WindowTitle("App")
{
pGamepadManager = *new Win32::GamepadManager();
}
PlatformCore::~PlatformCore()
{
}
bool PlatformCore::SetupWindow(int w, int h)
{
WNDCLASS wc;
memset(&wc, 0, sizeof(wc));
wc.lpszClassName = L"OVRAppWindow";
wc.style = CS_OWNDC;
wc.lpfnWndProc = systemWindowProc;
wc.cbWndExtra = sizeof(PlatformCore*);
RegisterClass(&wc);
Width = w;
Height = h;
RECT winSize;
winSize.left = winSize.top = 0;
winSize.right = Width;
winSize.bottom = Height;
AdjustWindowRect(&winSize, WS_OVERLAPPEDWINDOW, false);
hWnd = CreateWindowA("OVRAppWindow", WindowTitle.ToCStr(), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
// 1950, 10,
winSize.right-winSize.left, winSize.bottom-winSize.top,
NULL, NULL, hInstance, (LPVOID)this);
Modifiers = 0;
Cursor = LoadCursor(NULL, IDC_CROSS);
// Initialize Window center in screen coordinates
POINT center = { Width / 2, Height / 2 };
::ClientToScreen(hWnd, ¢er);
WindowCenter = center;
if (MMode == Mouse_Relative)
{
::SetCursorPos(WindowCenter.x, WindowCenter.y);
ShowCursor(FALSE);
}
::SetFocus(hWnd);
return (hWnd != NULL);
}
void PlatformCore::DestroyWindow()
{
// Release renderer.
pRender.Clear();
// Release gamepad.
pGamepadManager.Clear();
// Release window resources.
::DestroyWindow(hWnd);
UnregisterClass(L"OVRAppWindow", hInstance);
hWnd = 0;
Width = Height = 0;
//DestroyCursor(Cursor);
Cursor = 0;
}
void PlatformCore::ShowWindow(bool visible)
{
::ShowWindow(hWnd, visible ? SW_SHOW : SW_HIDE);
}
void PlatformCore::SetMouseMode(MouseMode mm)
{
if (mm == MMode)
return;
if (hWnd)
{
if (mm == Mouse_Relative)
{
ShowCursor(FALSE);
::SetCursorPos(WindowCenter.x, WindowCenter.y);
}
else
{
if (MMode == Mouse_Relative)
ShowCursor(TRUE);
}
}
MMode = mm;
}
void PlatformCore::GetWindowSize(int* w, int* h) const
{
*w = Width;
*h = Height;
}
void PlatformCore::SetWindowTitle(const char* title)
{
WindowTitle = title;
if (hWnd)
::SetWindowTextA(hWnd, title);
}
static UByte KeyMap[][2] =
{
{ VK_BACK, Key_Backspace },
{ VK_TAB, Key_Tab },
{ VK_CLEAR, Key_Clear },
{ VK_RETURN, Key_Return },
{ VK_SHIFT, Key_Shift },
{ VK_CONTROL, Key_Control },
{ VK_MENU, Key_Alt },
{ VK_PAUSE, Key_Pause },
{ VK_CAPITAL, Key_CapsLock },
{ VK_ESCAPE, Key_Escape },
{ VK_SPACE, Key_Space },
{ VK_PRIOR, Key_PageUp },
{ VK_NEXT, Key_PageDown },
{ VK_END, Key_End },
{ VK_HOME, Key_Home },
{ VK_LEFT, Key_Left },
{ VK_UP, Key_Up },
{ VK_RIGHT, Key_Right },
{ VK_DOWN, Key_Down },
{ VK_INSERT, Key_Insert },
{ VK_DELETE, Key_Delete },
{ VK_HELP, Key_Help },
{ VK_NUMLOCK, Key_NumLock },
{ VK_SCROLL, Key_ScrollLock },
{ VK_OEM_1, Key_Semicolon },
{ VK_OEM_PLUS, Key_Equal },
{ VK_OEM_COMMA, Key_Comma },
{ VK_OEM_MINUS, Key_Minus },
{ VK_OEM_PERIOD,Key_Period },
{ VK_OEM_2, Key_Slash },
{ VK_OEM_3, Key_Bar },
{ VK_OEM_4, Key_BracketLeft },
{ VK_OEM_5, Key_Backslash },
{ VK_OEM_6, Key_BracketRight },
{ VK_OEM_7, Key_Quote },
{ VK_OEM_AX, Key_OEM_AX }, // 'AX' key on Japanese AX keyboard.
{ VK_OEM_102, Key_OEM_102 }, // "<>" or "\|" on RT 102-key keyboard.
{ VK_ICO_HELP, Key_ICO_HELP },
{ VK_ICO_00, Key_ICO_00 }
};
KeyCode MapVKToKeyCode(unsigned vk)
{
unsigned key = Key_None;
if ((vk >= '0') && (vk <= '9'))
{
key = vk - '0' + Key_Num0;
}
else if ((vk >= 'A') && (vk <= 'Z'))
{
key = vk - 'A' + Key_A;
}
else if ((vk >= VK_NUMPAD0) && (vk <= VK_DIVIDE))
{
key = vk - VK_NUMPAD0 + Key_KP_0;
}
else if ((vk >= VK_F1) && (vk <= VK_F15))
{
key = vk - VK_F1 + Key_F1;
}
else
{
for (unsigned i = 0; i< (sizeof(KeyMap) / sizeof(KeyMap[1])); i++)
{
if (vk == KeyMap[i][0])
{
key = KeyMap[i][1];
break;
}
}
}
return (KeyCode)key;
}
LRESULT CALLBACK PlatformCore::systemWindowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
PlatformCore* self;
// WM_NCCREATE should be the first message to come it; use it to set class pointer.
if (msg == WM_NCCREATE)
{
self = static_cast<PlatformCore*>(((LPCREATESTRUCT)lp)->lpCreateParams);
if (self)
{
SetWindowLongPtr(hwnd, 0, (LONG_PTR)self);
self->hWnd = hwnd;
}
}
else
{
self = (PlatformCore*)(UPInt)GetWindowLongPtr(hwnd, 0);
}
return self ? self->WindowProc(msg, wp, lp) :
DefWindowProc(hwnd, msg, wp, lp);
}
LRESULT PlatformCore::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
{
KeyCode keyCode;
switch (msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
return 0;
case WM_SETCURSOR:
::SetCursor(Cursor);
return 0;
case WM_MOUSEMOVE:
if (MMode == Mouse_Relative)
{
POINT newPos = { LOWORD(lp), HIWORD(lp) };
::ClientToScreen(hWnd, &newPos);
if ((newPos.x == WindowCenter.x) && (newPos.y == WindowCenter.y))
break;
::SetCursorPos(WindowCenter.x, WindowCenter.y);
LONG dx = newPos.x - WindowCenter.x;
LONG dy = newPos.y - WindowCenter.y;
pApp->OnMouseMove(dx, dy, Mod_MouseRelative);
}
else
{
pApp->OnMouseMove(LOWORD(lp), HIWORD(lp), 0);
}
break;
case WM_MOVE:
{
RECT r;
GetClientRect(hWnd, &r);
WindowCenter.x = r.right/2;
WindowCenter.y = r.bottom/2;
::ClientToScreen(hWnd, &WindowCenter);
}
break;
case WM_KEYDOWN:
switch (wp)
{
case VK_CONTROL: Modifiers |= Mod_Control; break;
case VK_MENU: Modifiers |= Mod_Alt; break;
case VK_SHIFT: Modifiers |= Mod_Shift; break;
case VK_LWIN:
case VK_RWIN: Modifiers |= Mod_Meta; break;
}
if ((keyCode = MapVKToKeyCode((unsigned)wp)) != Key_None)
pApp->OnKey(keyCode, 0, true, Modifiers);
if (keyCode == Key_Escape && MMode == Mouse_Relative)
{
MMode = Mouse_RelativeEscaped;
ShowCursor(TRUE);
}
break;
case WM_KEYUP:
if ((keyCode = MapVKToKeyCode((unsigned)wp)) != Key_None)
pApp->OnKey(keyCode, 0, false, Modifiers);
switch (wp)
{
case VK_CONTROL: Modifiers &= ~Mod_Control; break;
case VK_MENU: Modifiers &= ~Mod_Alt; break;
case VK_SHIFT: Modifiers &= ~Mod_Shift; break;
case VK_LWIN:
case VK_RWIN: Modifiers &= ~Mod_Meta; break;
}
break;
case WM_LBUTTONDOWN:
//App->OnMouseButton(0,
::SetCapture(hWnd);
if (MMode == Mouse_RelativeEscaped)
{
::SetCursorPos(WindowCenter.x, WindowCenter.y);
::ShowCursor(FALSE);
MMode = Mouse_Relative;
}
break;
case WM_LBUTTONUP:
ReleaseCapture();
break;
case WM_SETFOCUS:
// Do NOT restore the Relative mode here, since calling SetCursorPos
// would screw up titlebar window dragging.
// Let users click in the center instead to resume.
break;
case WM_KILLFOCUS:
if (MMode == Mouse_Relative)
{
MMode = Mouse_RelativeEscaped;
ShowCursor(TRUE);
}
break;
case WM_SIZE:
// Change window size as long as we're not being minimized.
if (wp != SIZE_MINIMIZED)
{
Width = LOWORD(lp);
Height = HIWORD(lp);
if (pRender)
pRender->SetWindowSize(Width, Height);
pApp->OnResize(Width,Height);
}
break;
case WM_STYLECHANGING:
// Resize the window. This is needed because the size includes any present system controls, and
// windows does not adjust it when changing to fullscreen.
{
STYLESTRUCT* pss = (STYLESTRUCT*)lp;
RECT winSize;
winSize.left = winSize.top = 0;
winSize.right = Width;
winSize.bottom = Height;
int w = winSize.right-winSize.left;
int h = winSize.bottom-winSize.top;
AdjustWindowRect(&winSize, pss->styleNew, false);
::SetWindowPos(hWnd, NULL, 0, 0, w, h, SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
}
break;
case WM_QUIT:
case WM_CLOSE:
pApp->OnQuitRequest();
return false;
}
return DefWindowProc(hWnd, msg, wp, lp);
}
int PlatformCore::Run()
{
while (!Quit)
{
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
pApp->OnIdle();
// Keep sleeping when we're minimized.
if (IsIconic(hWnd))
{
Sleep(10);
}
}
}
return ExitCode;
}
RenderDevice* PlatformCore::SetupGraphics(const SetupGraphicsDeviceSet& setupGraphicsDesc,
const char* type, const Render::RendererParams& rp)
{
const SetupGraphicsDeviceSet* setupDesc = setupGraphicsDesc.PickSetupDevice(type);
OVR_ASSERT(setupDesc);
pRender = *setupDesc->pCreateDevice(rp, (void*)hWnd);
if (pRender)
pRender->SetWindowSize(Width, Height);
::ShowWindow(hWnd, SW_RESTORE);
return pRender.GetPtr();
}
void PlatformCore::PlayMusicFile(const char *fileName)
{
PlaySoundA(fileName, NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);
}
//-----------------------------------------------------------------------------
// Used to capture all the active monitor handles
struct MonitorSet
{
enum { MaxMonitors = 8 };
HMONITOR Monitors[MaxMonitors];
int MonitorCount;
int PrimaryCount;
};
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC, LPRECT, LPARAM dwData)
{
MonitorSet* monitorSet = (MonitorSet*)dwData;
if (monitorSet->MonitorCount > MonitorSet::MaxMonitors)
return FALSE;
monitorSet->Monitors[monitorSet->MonitorCount] = hMonitor;
monitorSet->MonitorCount++;
return TRUE;
};
// Returns the number of active screens for extended displays and 1 for mirrored display
int PlatformCore::GetDisplayCount()
{
// Get all the monitor handles
MonitorSet monitors;
monitors.MonitorCount = 0;
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&monitors);
// Count the primary monitors
int primary = 0;
MONITORINFOEX info;
for (int m=0; m < monitors.MonitorCount; m++)
{
info.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(monitors.Monitors[m], &info);
if (info.dwFlags & MONITORINFOF_PRIMARY)
primary++;
}
if (primary > 1)
return 1; // Regard mirrored displays as a single screen
else
return monitors.MonitorCount; // Return all extended displays
}
//-----------------------------------------------------------------------------
// Returns the device name for the given screen index or empty string for invalid index
// The zero index will always return the primary screen name
Render::DisplayId PlatformCore::GetDisplay(int screen)
{
// Get all the monitor handles
MonitorSet monitors;
monitors.MonitorCount = 0;
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&monitors);
String screen_name;
// Get the name of the suppled screen index with the requirement
// that screen 0 is the primary monitor
int non_primary_count = 0;
MONITORINFOEX info;
for (int m=0; m < monitors.MonitorCount; m++)
{
info.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(monitors.Monitors[m], &info);
if (info.dwFlags & MONITORINFOF_PRIMARY)
{
if (screen == 0)
{
screen_name = info.szDevice;
break;
}
}
else
{
non_primary_count++;
if (screen == non_primary_count)
{
screen_name = info.szDevice;
break;
}
}
}
return screen_name;
}
}}}
OVR::Platform::Application* g_app;
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE prevInst, LPSTR inArgs, int show)
{
using namespace OVR;
using namespace OVR::Platform;
OVR_UNUSED2(prevInst, show);
// CreateApplication must be the first call since it does OVR::System::Initialize.
g_app = Application::CreateApplication();
Win32::PlatformCore* platform = new Win32::PlatformCore(g_app, hinst);
// The platform attached to an app will be deleted by DestroyApplication.
g_app->SetPlatformCore(platform);
int exitCode = 0;
// Nested scope for container destructors to shutdown before DestroyApplication.
{
Array<String> args;
Array<char*> argv;
argv.PushBack("app");
const char* p = inArgs;
const char* pstart = inArgs;
while (*p)
{
if (*p == ' ')
{
args.PushBack(String(pstart, p - pstart));
while (*p == ' ')
p++;
pstart = p;
}
else
{
p++;
}
}
if (p != pstart)
args.PushBack(String(pstart, p - pstart));
// FIXME memory leak of the command line arguments here
for (UPInt i = 0; i < args.GetSize(); i++)
argv.PushBack(strdup(args[i].ToCStr()));
exitCode = g_app->OnStartup((int)argv.GetSize(), &argv[0]);
if (!exitCode)
exitCode = platform->Run();
}
// No OVR functions involving memory are allowed after this.
Application::DestroyApplication(g_app);
g_app = 0;
OVR_DEBUG_STATEMENT(_CrtDumpMemoryLeaks());
return exitCode;
}
|