forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrontPanelManager.cpp
431 lines (342 loc) · 14.1 KB
/
FrontPanelManager.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
//--------------------------------------------------------------------------------------
// FrontPanelManager.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "FrontPanelManager.h"
#include "TopLevelScreen.h"
#include "QuickActionScreen.h"
#include "QuickActionMappingScreen.h"
#include "FontViewerScreen.h"
#include "GPURenderScreen.h"
using namespace ATG;
using Microsoft::WRL::ComPtr;
using ButtonState = FrontPanelInput::ButtonStateTracker::ButtonState;
FrontPanelManager::FrontPanelManager()
: m_currentPanelScreen(nullptr)
{
if (IsXboxFrontPanelAvailable())
{
// Get the default front panel
DX::ThrowIfFailed(GetDefaultXboxFrontPanel(m_frontPanelControl.ReleaseAndGetAddressOf()));
// Initialize the FrontPanelDisplay object
m_frontPanelDisplay = std::make_unique<FrontPanelDisplay>(m_frontPanelControl.Get());
// Initialize the FrontPanelInput object
m_frontPanelInput = std::make_unique<FrontPanelInput>(m_frontPanelControl.Get());
// Initialize the button assignments
for (int i = 0; i < m_buttonActionAssignments.size(); ++i)
{
m_buttonActionAssignments[i] = nullptr;
}
}
}
void FrontPanelManager::CreateScreens()
{
// Early out if there is no front panel
if (!m_frontPanelControl)
return;
// Create the demo screens
auto& buttonActions = CreateChild<TopLevelScreen>(
L"Quick Actions",
L"1",
L"Scroll down to see the action assignment\n"
L"for each of the 5 front panel buttons.");
auto& fontViewer = CreateChild<TopLevelScreen>(
L"CPU Fonts",
L"2",
L"Scroll down to see the different font styles and\n"
L"scroll left and right to change the font size.");
auto& gpuRender = CreateChild<TopLevelScreen>(
L"GPU to Front Panel",
L"3",
L"Scroll down to use the GPU to render for the\n"
L"Front Panel.");
auto& buttonMappings = CreateChild<TopLevelScreen>(
L"Button Mappings",
L"4",
L"Scroll down to change the action assigment\n"
L"for each of the 5 front panel buttons.\n");
auto& quickAction1 = CreateChild<QuickActionScreen>(XBOX_FRONT_PANEL_BUTTONS_BUTTON1);
auto& quickAction2 = CreateChild<QuickActionScreen>(XBOX_FRONT_PANEL_BUTTONS_BUTTON2);
auto& quickAction3 = CreateChild<QuickActionScreen>(XBOX_FRONT_PANEL_BUTTONS_BUTTON3);
auto& quickAction4 = CreateChild<QuickActionScreen>(XBOX_FRONT_PANEL_BUTTONS_BUTTON4);
auto& quickAction5 = CreateChild<QuickActionScreen>(XBOX_FRONT_PANEL_BUTTONS_BUTTON5);
auto& fontViewArial = CreateChild<FontViewerScreen>(L"Arial Narrow", 16, L"assets\\ArialNarrow16.rasterfont");
fontViewArial.AddFontFile(12, L"assets\\ArialNarrow12.rasterfont");
fontViewArial.AddFontFile(24, L"assets\\ArialNarrow24.rasterfont");
fontViewArial.AddFontFile(32, L"assets\\ArialNarrow32.rasterfont");
fontViewArial.AddFontFile(64, L"assets\\ArialNarrow64.rasterfont");
auto& fontViewLucida = CreateChild<FontViewerScreen>(L"Lucida Console", 16, L"assets\\LucidaConsole16.rasterfont");
fontViewLucida.AddFontFile(12, L"assets\\LucidaConsole12.rasterfont");
fontViewLucida.AddFontFile(24, L"assets\\LucidaConsole24.rasterfont");
fontViewLucida.AddFontFile(32, L"assets\\LucidaConsole32.rasterfont");
fontViewLucida.AddFontFile(64, L"assets\\LucidaConsole64.rasterfont");
auto& fontViewSegoe = CreateChild<FontViewerScreen>(L"Segoe UI", 16, L"assets\\Segoe_UI16.rasterfont");
fontViewSegoe.AddFontFile(12, L"assets\\Segoe_UI12.rasterfont");
fontViewSegoe.AddFontFile(24, L"assets\\Segoe_UI24.rasterfont");
fontViewSegoe.AddFontFile(32, L"assets\\Segoe_UI32.rasterfont");
fontViewSegoe.AddFontFile(64, L"assets\\Segoe_UI64.rasterfont");
auto& fontViewSegoeBold = CreateChild<FontViewerScreen>(L"Segoe UI Bold", 16, L"assets\\Segoe_UI_bold16.rasterfont");
fontViewSegoeBold.AddFontFile(12, L"assets\\Segoe_UI_bold12.rasterfont");
fontViewSegoeBold.AddFontFile(24, L"assets\\Segoe_UI_bold24.rasterfont");
fontViewSegoeBold.AddFontFile(32, L"assets\\Segoe_UI_bold32.rasterfont");
fontViewSegoeBold.AddFontFile(64, L"assets\\Segoe_UI_bold64.rasterfont");
auto& gpuRenderScreen = CreateChild<GPURenderScreen>();
auto& quickActionMapper = CreateChild<QuickActionMappingScreen>();
buttonActions.SetLeftNeighbor(&buttonMappings);
buttonActions.SetRightNeighbor(&fontViewer);
buttonActions.SetDownNeighbor(&quickAction1);
fontViewer.SetLeftNeighbor(&buttonActions);
fontViewer.SetRightNeighbor(&gpuRender);
fontViewer.SetDownNeighbor(&fontViewArial);
fontViewArial.SetUpNeighbor(&fontViewer);
fontViewArial.SetDownNeighbor(&fontViewLucida);
fontViewLucida.SetUpNeighbor(&fontViewArial);
fontViewLucida.SetDownNeighbor(&fontViewSegoe);
fontViewSegoe.SetUpNeighbor(&fontViewLucida);
fontViewSegoe.SetDownNeighbor(&fontViewSegoeBold);
fontViewSegoeBold.SetUpNeighbor(&fontViewSegoe);
gpuRender.SetLeftNeighbor(&fontViewer);
gpuRender.SetRightNeighbor(&buttonMappings);
gpuRender.SetDownNeighbor(&gpuRenderScreen);
gpuRenderScreen.SetUpNeighbor(&gpuRender);
buttonMappings.SetLeftNeighbor(&gpuRender);
buttonMappings.SetRightNeighbor(&buttonActions);
buttonMappings.SetDownNeighbor(&quickActionMapper);
quickAction1.SetUpNeighbor(&buttonActions);
quickAction1.SetLeftNeighbor(&quickAction5);
quickAction1.SetRightNeighbor(&quickAction2);
quickAction2.SetUpNeighbor(&buttonActions);
quickAction2.SetLeftNeighbor(&quickAction1);
quickAction2.SetRightNeighbor(&quickAction3);
quickAction3.SetUpNeighbor(&buttonActions);
quickAction3.SetLeftNeighbor(&quickAction2);
quickAction3.SetRightNeighbor(&quickAction4);
quickAction4.SetUpNeighbor(&buttonActions);
quickAction4.SetLeftNeighbor(&quickAction3);
quickAction4.SetRightNeighbor(&quickAction5);
quickAction5.SetUpNeighbor(&buttonActions);
quickAction5.SetLeftNeighbor(&quickAction4);
quickAction5.SetRightNeighbor(&quickAction1);
quickActionMapper.SetUpNeighbor(&buttonMappings);
buttonActions.Resume(nullptr);
}
void FrontPanelManager::Update(DX::StepTimer const & timer)
{
// Early out if there is no front panel
if (!m_frontPanelControl)
return;
auto fpInput = m_frontPanelInput->GetState();
m_frontPanelInputButtons.Update(fpInput);
if (m_frontPanelInputButtons.buttonsChanged)
{
if(m_frontPanelInputButtons.button1 == ButtonState::PRESSED)
OnButtonPressed(XBOX_FRONT_PANEL_BUTTONS_BUTTON1);
if (m_frontPanelInputButtons.button2 == ButtonState::PRESSED)
OnButtonPressed(XBOX_FRONT_PANEL_BUTTONS_BUTTON2);
if (m_frontPanelInputButtons.button3 == ButtonState::PRESSED)
OnButtonPressed(XBOX_FRONT_PANEL_BUTTONS_BUTTON3);
if (m_frontPanelInputButtons.button4 == ButtonState::PRESSED)
OnButtonPressed(XBOX_FRONT_PANEL_BUTTONS_BUTTON4);
if (m_frontPanelInputButtons.button5 == ButtonState::PRESSED)
OnButtonPressed(XBOX_FRONT_PANEL_BUTTONS_BUTTON5);
if (m_frontPanelInputButtons.dpadLeft == ButtonState::PRESSED)
OnButtonPressed(XBOX_FRONT_PANEL_BUTTONS_LEFT);
if (m_frontPanelInputButtons.dpadRight == ButtonState::PRESSED)
OnButtonPressed(XBOX_FRONT_PANEL_BUTTONS_RIGHT);
if (m_frontPanelInputButtons.dpadUp == ButtonState::PRESSED)
OnButtonPressed(XBOX_FRONT_PANEL_BUTTONS_UP);
if (m_frontPanelInputButtons.dpadDown == ButtonState::PRESSED)
OnButtonPressed(XBOX_FRONT_PANEL_BUTTONS_DOWN);
if (m_frontPanelInputButtons.buttonSelect == ButtonState::PRESSED)
OnButtonPressed(XBOX_FRONT_PANEL_BUTTONS_SELECT);
}
m_currentPanelScreen->Update(timer);
}
void FrontPanelManager::Navigate(const PanelScreen & navToChild)
{
// Early out if there is no front panel
if (!m_frontPanelControl)
return;
SetAssignedLigts();
for (int i = 0; i < m_children.size(); ++i)
{
if (m_children[i].get() == &navToChild)
{
m_currentPanelScreen = m_children[i].get();
return;
}
}
}
bool FrontPanelManager::OnButtonPressed(XBOX_FRONT_PANEL_BUTTONS whichButton)
{
// Give the current screen first chance to handle the button press
if (m_currentPanelScreen->OnButtonPressed(whichButton))
{
return true;
}
// Otherwise handle it ourselves
switch (whichButton)
{
default:
break;
case XBOX_FRONT_PANEL_BUTTONS_SELECT:
// Capture the front panel display (written in a number of file formats)
m_frontPanelDisplay->SaveDDSToFile(L"D:\\FrontPanelScreen.dds");
m_frontPanelDisplay->SaveWICToFile(L"D:\\FrontPanelScreen.bmp", GUID_ContainerFormatBmp);
m_frontPanelDisplay->SaveWICToFile(L"D:\\FrontPanelScreen.gif", GUID_ContainerFormatGif);
m_frontPanelDisplay->SaveWICToFile(L"D:\\FrontPanelScreen.jpg", GUID_ContainerFormatJpeg);
m_frontPanelDisplay->SaveWICToFile(L"D:\\FrontPanelScreen.png", GUID_ContainerFormatPng);
m_frontPanelDisplay->SaveWICToFile(L"D:\\FrontPanelScreen.tif", GUID_ContainerFormatTiff);
#ifdef _DEBUG
OutputDebugStringA("Screenshot of front panel display written to development drive.\n");
#endif
break;
case XBOX_FRONT_PANEL_BUTTONS_BUTTON1:
case XBOX_FRONT_PANEL_BUTTONS_BUTTON2:
case XBOX_FRONT_PANEL_BUTTONS_BUTTON3:
case XBOX_FRONT_PANEL_BUTTONS_BUTTON4:
case XBOX_FRONT_PANEL_BUTTONS_BUTTON5:
return InvokeButtonAction(int(GetIndexForButtonId(whichButton)));;
}
return false;
}
bool FrontPanelManager::InvokeButtonAction(int buttonId)
{
const ButtonAction *action = m_buttonActionAssignments[buttonId];
if (action)
{
(*action).Invoke();
return true;
}
return false;
}
void FrontPanelManager::SetAssignedLigts() const
{
auto& fpi = FrontPanelInput::Get();
fpi.SetLightStates(GetAssignedLights());
}
void FrontPanelManager::CreateDeviceDependentResources(DX::DeviceResources * deviceResources)
{
// Early out if there is no front panel
if (!m_frontPanelControl)
return;
for (int i = 0; i < m_children.size(); ++i)
{
m_children[i]->CreateDeviceDependentResources(deviceResources, m_frontPanelControl.Get());
}
}
void FrontPanelManager::CreateWindowSizeDependentResources(DX::DeviceResources * deviceResources)
{
// Early out if there is no front panel
if (!m_frontPanelControl)
return;
for (int i = 0; i < m_children.size(); ++i)
{
m_children[i]->CreateWindowSizeDependentResources(deviceResources);
}
}
void FrontPanelManager::GPURender(DX::DeviceResources * deviceResources)
{
// Early out if there is no front panel
if (!m_frontPanelControl)
return;
m_currentPanelScreen->GPURender(deviceResources);
}
size_t FrontPanelManager::GetIndexForButtonId(XBOX_FRONT_PANEL_BUTTONS buttonId)
{
switch (buttonId)
{
default:
break;
case XBOX_FRONT_PANEL_BUTTONS_BUTTON1:
// TODO: perform action for button1
return 0;
case XBOX_FRONT_PANEL_BUTTONS_BUTTON2:
// TODO: perform the action for button2
return 1;
case XBOX_FRONT_PANEL_BUTTONS_BUTTON3:
// TODO: perform the action for button3
return 2;
case XBOX_FRONT_PANEL_BUTTONS_BUTTON4:
// TODO: perform the action for button4
return 3;
case XBOX_FRONT_PANEL_BUTTONS_BUTTON5:
// TODO: perform the action for button5
return 4;
}
throw std::out_of_range("Invalid button Id");
}
size_t FrontPanelManager::ButtonActionCount() const
{
return m_buttonActions.size();
}
const FrontPanelManager::ActionRecord &FrontPanelManager::operator[](size_t idx) const
{
return *m_buttonActions[idx];
}
const FrontPanelManager::ActionRecord &FrontPanelManager::CreateButtonAction(const wchar_t * name, const wchar_t * description, std::function<void()> op)
{
unsigned id = unsigned(m_buttonActions.size());
ButtonAction *action = new ButtonAction;
action->id = id;
action->name = name;
action->description = description;
action->Invoke = op;
m_buttonActions.emplace_back(action);
return *m_buttonActions[id];
}
bool FrontPanelManager::IsActionAssigned(XBOX_FRONT_PANEL_BUTTONS buttonId) const
{
size_t id = GetIndexForButtonId(buttonId);
const ButtonAction *action = m_buttonActionAssignments[id];
return action != nullptr;
}
const FrontPanelManager::ActionRecord &FrontPanelManager::GetActionAssignment(XBOX_FRONT_PANEL_BUTTONS buttonId) const
{
size_t id = GetIndexForButtonId(buttonId);
const ButtonAction *action = m_buttonActionAssignments[id];
if (action == nullptr)
{
throw std::exception("No action assignment for button");
}
return *action;
}
void FrontPanelManager::AssignActionToButton(const ActionRecord &action, XBOX_FRONT_PANEL_BUTTONS buttonId)
{
if (action.id >= m_buttonActions.size())
{
throw std::range_error("Invalid button action ID");
}
size_t id = GetIndexForButtonId(buttonId);
m_buttonActionAssignments[id] = m_buttonActions[action.id].get();
SetAssignedLigts();
}
void FrontPanelManager::ClearActionAssignment(XBOX_FRONT_PANEL_BUTTONS buttonId)
{
size_t id = GetIndexForButtonId(buttonId);
m_buttonActionAssignments[id] = nullptr;
SetAssignedLigts();
}
XBOX_FRONT_PANEL_LIGHTS FrontPanelManager::GetAssignedLights() const
{
XBOX_FRONT_PANEL_LIGHTS lights = XBOX_FRONT_PANEL_LIGHTS_NONE;
for (int i = 0; i < m_buttonActionAssignments.size(); ++i)
{
if (m_buttonActionAssignments[i] != nullptr)
{
lights = static_cast<XBOX_FRONT_PANEL_LIGHTS>(lights | XBOX_FRONT_PANEL_LIGHTS_LIGHT1 << i);
}
}
return lights;
}
void FrontPanelManager::AddChild(PanelScreen * child)
{
m_children.emplace_back(child);
if (m_currentPanelScreen == nullptr)
{
m_currentPanelScreen = child;
}
}