-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathmoonlight_xbox_dxMain.cpp
More file actions
1073 lines (923 loc) · 39.7 KB
/
Copy pathmoonlight_xbox_dxMain.cpp
File metadata and controls
1073 lines (923 loc) · 39.7 KB
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
#include "moonlight_xbox_dxMain.h"
#include "pch.h"
#include <Pages/AppPage.xaml.h>
#include <Pages/HostSelectorPage.xaml.h>
#include <Pages/StreamPage.xaml.h>
#include <Streaming\FFMpegDecoder.h>
#include "../Plot/ImGuiPlots.h"
#include "Common\DirectXHelper.h"
#include "State\GamepadState.h"
#include "Utils.hpp"
#include <algorithm>
#include <cmath>
using namespace moonlight_xbox_dx;
using namespace Concurrency;
using namespace DirectX;
using namespace Platform::Collections;
using namespace Windows::Foundation;
using namespace Windows::Gaming::Input;
using namespace Windows::System::Threading;
using namespace Windows::UI::ViewManagement::Core;
extern "C" {
#include <Common/ModalDialog.xaml.h>
#include <Limelight.h>
}
// Loads and initializes application assets when the application is loaded.
moonlight_xbox_dxMain::moonlight_xbox_dxMain(const std::shared_ptr<DX::DeviceResources> &deviceResources, StreamPage ^ streamPage, MoonlightClient *client, StreamConfiguration ^ configuration)
: m_deviceResources(deviceResources),
m_pointerLocationX(0.0f),
m_streamPage(streamPage),
moonlightClient(client) {
Platform::String ^ appName = configuration->appName ? "'" + configuration->appName + "'" : "App";
DISPATCH_UI(([streamPage, appName]() {
streamPage->m_stepText->Text = "Starting " + appName;
}));
client->OnFailed = ([this, appName](int status, int error, char *message) {
std::string msgCopy = message ? message : std::string();
auto self = this;
DISPATCH_UI(([msgCopy, self, appName]() {
auto showErrorDialog = std::make_shared<std::function<void()>>();
auto showLogsDialog = std::make_shared<std::function<void()>>();
*showErrorDialog = [self, msgCopy, showLogsDialog, appName]() {
auto dialog1 = ref new Windows::UI::Xaml::Controls::ContentDialog();
dialog1->Title = L"Failed to start " + appName;
dialog1->Content = Utils::StringFromStdString(msgCopy);
dialog1->PrimaryButtonText = L"OK";
dialog1->SecondaryButtonText = L"Show Logs";
concurrency::create_task(::moonlight_xbox_dx::ModalDialog::ShowOnceAsync(dialog1)).then([self, showLogsDialog](concurrency::task<Windows::UI::Xaml::Controls::ContentDialogResult> t) {
auto result = t.get();
if (result == Windows::UI::Xaml::Controls::ContentDialogResult::Primary) {
self->StopRenderLoop();
self->ExitStreamPage();
} else if (result == Windows::UI::Xaml::Controls::ContentDialogResult::Secondary) {
(*showLogsDialog)();
}
});
};
*showLogsDialog = [self, showErrorDialog]() {
auto dialog2 = ref new Windows::UI::Xaml::Controls::ContentDialog();
std::wstring m_text = L"";
std::vector<std::wstring> lines = Utils::GetLogLines();
for (int i = 0; i < (int)lines.size(); i++) {
// Get only the last 8 lines
// More than that cannot be fully viewed on the screen at the current scaling
if ((int)lines.size() - i <= 8) {
m_text += lines[i];
}
}
Utils::showLogs = true;
dialog2->MaxWidth = 600;
dialog2->Title = "Logs";
dialog2->Content = ref new Platform::String(m_text.c_str());
dialog2->PrimaryButtonText = L"OK";
dialog2->SecondaryButtonText = L"Show Error";
concurrency::create_task(::moonlight_xbox_dx::ModalDialog::ShowOnceAsync(dialog2)).then([self, showErrorDialog](concurrency::task<Windows::UI::Xaml::Controls::ContentDialogResult> t) {
auto result = t.get();
if (result == Windows::UI::Xaml::Controls::ContentDialogResult::Primary) {
self->StopRenderLoop();
self->ExitStreamPage();
} else if (result == Windows::UI::Xaml::Controls::ContentDialogResult::Secondary) {
(*showErrorDialog)();
}
});
};
// Start by showing the error dialog
(*showErrorDialog)();
}));
});
client->OnStatusUpdate = ([streamPage](int status) {
std::string msg = LiGetFormattedStageName(status);
DISPATCH_UI(([streamPage, msg]() {
streamPage->m_stepText->Text = Utils::StringFromStdString(msg);
}));
});
// Register to be notified if the Device is lost or recreated
m_deviceResources->RegisterDeviceNotify(this);
m_sceneRenderer = std::make_shared<VideoRenderer>(m_deviceResources, moonlightClient, configuration);
client->OnCompleted = ([this, streamPage, configuration]() {
concurrency::create_task([this]() {
while (this->m_sceneRenderer && !this->m_sceneRenderer->IsLoadingComplete() && !this->moonlightClient->IsConnectionTerminated()) {
Sleep(50);
}
}).then([this, streamPage, configuration](concurrency::task<void> t) {
if (this->m_sceneRenderer && this->m_sceneRenderer->IsLoadingSuccessful()) {
DISPATCH_UI(([streamPage]() {
Sleep(500);
streamPage->m_progressRing->IsActive = false;
streamPage->m_progressView->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}));
}
});
});
client->SetHDR = ([this](bool v) {
concurrency::create_task([this]() {
while (this->m_sceneRenderer && !this->m_sceneRenderer->IsLoadingComplete() && !this->moonlightClient->IsConnectionTerminated()) {
Sleep(50);
}
}).then([this, v](concurrency::task<void> t) {
this->m_sceneRenderer->SetHDR(v);
});
});
m_LogRenderer = std::make_unique<LogRenderer>(m_deviceResources);
m_statsTextRenderer = std::make_unique<StatsRenderer>(m_deviceResources);
m_statsTextRenderer->SetVisible(configuration->enableStats);
// Reset Stats since it may have data from a prior stream
Stats::instance().Reset();
// We're now connected and can register for gamepad events
for (int i = 0; i < MAX_GAMEPADS; i++) {
GamepadState &state = m_GamepadState[i];
state.Reset();
}
// Force disable graphs on Xbox One at 4K, they run too slowly
// XXX can we run them at a lower resolution?
if (IsXboxOne() && m_deviceResources->GetPixelHeight() >= 2160) {
configuration->enableGraphs = false;
}
m_deviceResources->SetShowImGui(configuration->enableGraphs);
ImGuiPlots::instance().setEnabled(configuration->enableGraphs);
client->OnRumble = ([this](unsigned short controllerNumber, unsigned short lowFreqMotor, unsigned short highFreqMotor) {
auto &state = this->FindGamepadStateByHostId(controllerNumber);
if (state.controller == nullptr) return;
auto gamepads = Gamepad::Gamepads;
if (state.localId >= gamepads->Size) return;
auto gamepad = gamepads->GetAt(state.localId);
float normalizedLow = lowFreqMotor / (float)(256 * 256);
float normalizedHigh = highFreqMotor / (float)(256 * 256);
GamepadVibration v = gamepad->Vibration;
v.LeftMotor = normalizedLow;
v.RightMotor = normalizedHigh;
gamepad->Vibration = v;
});
client->OnTriggerRumble = ([this](unsigned short controllerNumber, unsigned short leftTriggerMotor, unsigned short rightTriggerMotor) {
auto &state = this->FindGamepadStateByHostId(controllerNumber);
if (state.controller == nullptr) return;
auto gamepads = Gamepad::Gamepads;
if (state.localId >= gamepads->Size) return;
auto gamepad = gamepads->GetAt(state.localId);
float normalizedLeft = leftTriggerMotor / (float)(256 * 256);
float normalizedRight = rightTriggerMotor / (float)(256 * 256);
GamepadVibration v = gamepad->Vibration;
v.LeftTrigger = normalizedLeft;
v.RightTrigger = normalizedRight;
gamepad->Vibration = v;
});
m_timer.SetFixedTimeStep(false);
double refreshRate = m_deviceResources->GetUWPRefreshRate();
m_deviceResources->SetRefreshRate(refreshRate);
m_deviceResources->SetFrameRate(configuration->FPS);
// Force refresh of connected gamepads because OnGamepadAdded may not always be called if we reconnect
streamPage->RequestRefreshGamepads();
}
moonlight_xbox_dxMain::~moonlight_xbox_dxMain() {
// Deregister device notification
m_deviceResources->RegisterDeviceNotify(nullptr);
}
void moonlight_xbox_dxMain::CreateDeviceDependentResources() {
}
// Updates application state when the window size changes (e.g. device orientation change)
void moonlight_xbox_dxMain::CreateWindowSizeDependentResources() {
m_sceneRenderer->CreateWindowSizeDependentResources();
m_LogRenderer->CreateWindowSizeDependentResources();
m_statsTextRenderer->CreateWindowSizeDependentResources();
}
void moonlight_xbox_dxMain::StartRenderLoop() {
// If the animation render loop is already running then do not start another thread.
if (m_renderLoopWorker != nullptr && m_renderLoopWorker->Status == AsyncStatus::Started) {
return;
}
// Create a task that will be run on a background thread.
auto workItemHandler = ref new WorkItemHandler([this](IAsyncAction ^ action) {
if (!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL)) {
Utils::Logf("Failed to set render thread priority: %d\n", GetLastError());
}
int64_t t0 = 0, t1 = 0, t2 = 0, t3 = 0;
int64_t lastFramePts = 0, lastPresentTime = 0;
double frametimeMs = 0.0, hostFrametimeMs = 0.0;
const double bufferMs = 1.5; // safety wait time to avoid missing deadline
const double alphaUp = 0.25; // react faster when renderMs spikes upward
const double alphaDown = 0.05; // decay slowly when renderMs drops
double ewmaRenderMs = 3.0; // Initial guess for render cost
// Calculate the updated frame and render once per vertical blanking interval.
while (action->Status == AsyncStatus::Started && !moonlightClient->IsConnectionTerminated()) {
// Get overall deadline we must hit by the Present for this frame
int64_t deadline = Pacer::instance().getNextVBlankQpc(&t0);
// wait for a frame + avg render time + safety buffer
double maxWaitMs = std::max(0.0, QpcToMs(deadline - t0) - ewmaRenderMs - bufferMs);
Pacer::instance().waitForFrame(maxWaitMs);
t1 = QpcNow();
{
critical_section::scoped_lock lock(m_criticalSection);
Update();
bool rendered = false;
{
// ffmpeg and Render both use the same D3D context
auto guard = FFMpegDecoder::Lock();
rendered = Render();
t2 = QpcNow();
}
// Whether we rendered a new frame or not, wait until vblank for pacing
// This is out of the lock and won't block the decoder
bool hitDeadline = Pacer::instance().waitBeforePresent(deadline);
t3 = QpcNow();
if (!rendered) {
// (Immediate pacing mode only) We're receiving a lower framerate
// and no frame was available, we don't call Present here and the
// previous frame will be re-displayed by DWM. On Xbox One this may cause
// corrupted frames or tearing.
continue;
}
{
// lock is required around Present
auto guard = FFMpegDecoder::Lock();
m_deviceResources->Present();
}
// Graph frametime only for new frames
bool isRepeatFrame = true;
int64_t currentFramePts = Pacer::instance().getCurrentFramePts();
if (currentFramePts != lastFramePts) {
if (lastPresentTime > 0) {
hostFrametimeMs = ((double)currentFramePts - lastFramePts) / 90.0;
frametimeMs = QpcToMs(t3 - lastPresentTime);
ImGuiPlots::instance().observeFloat(PLOT_FRAMETIME, static_cast<float>(frametimeMs));
}
lastPresentTime = t3;
lastFramePts = currentFramePts;
isRepeatFrame = false;
}
// Weighted avg of time spent in Render(), more weight given to a slower render time
// If we missed our present deadline this frame, aggressively weight this higher so maxWaitMs is smaller.
// This is clamped to the deadline to prevent outliers
double renderMs = QpcToMs(t2 - t1);
double clampedRenderMs = std::clamp(renderMs, 0.0, QpcToMs(deadline - t0));
double alpha = (clampedRenderMs > ewmaRenderMs) ? alphaUp : alphaDown;
if (!hitDeadline) alpha *= 2.0;
ewmaRenderMs = (clampedRenderMs * alpha) + (ewmaRenderMs * (1.0 - alpha));
// Track high-level render loop stats
double preWaitMs = QpcToMs(t1 - t0);
double beforePresentMs = QpcToMs(t3 - t2);
Stats::instance().SubmitRenderStats(preWaitMs, renderMs, beforePresentMs, hitDeadline);
FQLog("render loop %.3fms %s%s%s pts:%.3fs frametime(c:%02.3fms h:%02.3fms) (Deadline %.3fms PreWait %.3fms (max %.3fms) + Render %.3fms (avg %.3f) + Present %.3fms)\n",
QpcToMs(t3 - t0), // loop time
hitDeadline ? " " : "M", // missed deadline?
isRepeatFrame ? "R" : " ", // repeated frame?
preWaitMs > maxWaitMs + bufferMs ? "W" : " ", // we waited too long for a frame (including buffer)
(double)currentFramePts / 90000.0, // host's timestamp (in seconds)
frametimeMs, // effective client frametime not counting repeated frames
hostFrametimeMs, // host frametime
QpcToMs(deadline - t0), // deadline time window until next vblank
preWaitMs, // prewait (time spent waiting for new frame to arrive)
maxWaitMs, // max wait allowed this frame
renderMs, // render time this frame
ewmaRenderMs, // average of render time used to control prewait
beforePresentMs); // wait time to align present to vblank
}
}
// we've lost the connection, clean up
StopRenderLoop(); // also stops input
Disconnect();
DISPATCH_UI([this]() {
ExitStreamPage();
});
});
m_renderLoopWorker = ThreadPool::RunAsync(workItemHandler, WorkItemPriority::High, WorkItemOptions::TimeSliced);
if (m_inputLoopWorker != nullptr && m_inputLoopWorker->Status == AsyncStatus::Started) {
return;
}
auto inputItemHandler = ref new WorkItemHandler([this](IAsyncAction ^ action) {
const int pollingHz = 500;
const int64_t pollIntervalQpc = MsToQpc(1000.0 / pollingHz);
int64_t lastProcessInput = 0;
while (action->Status == AsyncStatus::Started) {
int64_t now = QpcNow();
if (now - lastProcessInput >= pollIntervalQpc) {
lastProcessInput = now;
ProcessInput();
if (m_streamPage->ShouldRefreshGamepads()) {
// Process added/removed gamepads
RefreshGamepads();
}
} else {
const int64_t nextPoll = lastProcessInput + pollIntervalQpc;
SleepUntilQpc(nextPoll, 500);
}
}
});
// Run task on a dedicated high priority background thread.
m_inputLoopWorker = ThreadPool::RunAsync(inputItemHandler, WorkItemPriority::High, WorkItemOptions::TimeSliced);
moonlightClient->OnCompleted(); // hide Initializing spinny
}
void moonlight_xbox_dxMain::StopRenderLoop() {
m_renderLoopWorker->Cancel();
m_inputLoopWorker->Cancel();
}
// Updates the application state once per frame.
void moonlight_xbox_dxMain::Update() {
// Update scene objects.
m_timer.Tick([&]() {
m_sceneRenderer->Update(m_timer);
m_LogRenderer->Update(m_timer);
m_statsTextRenderer->Update(m_timer);
});
}
// Gamepad handling
static inline bool isPressed(GamepadButtons buttons, GamepadButtons b) {
return (buttons & b) == b;
}
// new button press
static inline bool PressedEdge(GamepadReading &r, GamepadReading &p, GamepadButtons b) {
return isPressed(r.Buttons, b) && !isPressed(p.Buttons, b);
}
// new button release
static inline bool ReleaseEdge(GamepadReading &r, GamepadReading &p, GamepadButtons b) {
return !isPressed(r.Buttons, b) && isPressed(p.Buttons, b);
}
static inline GamepadReading EmptyReading() {
return GamepadReading{};
}
namespace {
const double kDeadzone = 0.10; // radial, in stick units
const double kSaturation = 0.95; // magnitude treated as full deflection
const double kExponent = 3.0; // response curve; higher = finer control near center
const double kLinearBlend = 0.06; // see ResponseCurve()
const double kMaxSpeed = 2200.0; // px/sec at full deflection, before sensitivity
const double kSmoothMs = 35.0; // ease-in time constant for the stick magnitude
const double kPrecision = 0.2; // velocity scale while the precision modifier is held
const double kMaxScrollRate = 2000.0; // scroll units/sec at full deflection (120 == one notch)
const double kScrollExponent = 2.0;
const double kScrollSendMs = 8.0; // don't emit scroll events faster than this
const double kMaxDtSec = 0.1; // beyond this we assume a stall, not real motion
// Elapsed seconds since the previous integration, or 0 if this poll can't be trusted.
double PointerDt(int64_t now, int64_t &last) {
const int64_t previous = last;
last = now;
if (previous == 0) {
return 0.0; // first poll after entering mouse mode
}
const double dt = QpcToMs(now - previous) / 1000.0;
return (dt > 0.0 && dt <= kMaxDtSec) ? dt : 0.0;
}
// Rescales a raw magnitude so the response rises continuously from zero at the edge of
// the deadzone, rather than jumping straight to some minimum speed.
double NormalizeMagnitude(double mag) {
if (mag <= kDeadzone) {
return 0.0;
}
return std::min((mag - kDeadzone) / (kSaturation - kDeadzone), 1.0);
}
double ResponseCurve(double t, double exponent) {
return kLinearBlend * t + (1.0 - kLinearBlend) * std::pow(t, exponent);
}
// Splits an accumulator into the whole part to send and the remainder to keep.
short TakeWholePart(double &accum) {
const double whole = std::trunc(accum);
accum -= whole;
return (short)std::clamp(whole, -32768.0, 32767.0);
}
} // namespace
// Integrates the pointer for one poll. Returns false when there is nothing to send.
static bool UpdatePointer(GamepadState &state, double stickX, double stickY,
double sensitivity, double precisionScale,
short &outX, short &outY) {
outX = outY = 0;
const double dt = PointerDt(QpcNow(), state.mouseLastQpc);
if (dt == 0.0) {
return false;
}
// Radial deadzone, so a diagonal push isn't sqrt(2) faster than a cardinal one.
const double mag = std::sqrt(stickX * stickX + stickY * stickY);
double target = 0.0, dirX = 0.0, dirY = 0.0;
if (mag > kDeadzone) {
target = NormalizeMagnitude(mag);
dirX = stickX / mag;
dirY = stickY / mag;
}
// Ease in to filter out thumb tremor near the deadzone, but snap to zero on release so
// the cursor lands where you let go instead of gliding past it.
if (target < state.mouseSmoothMag) {
state.mouseSmoothMag = target;
} else {
state.mouseSmoothMag += (target - state.mouseSmoothMag) * (1.0 - std::exp(-(dt * 1000.0) / kSmoothMs));
}
// Keep the remainder
if (state.mouseSmoothMag <= 0.0) {
return false;
}
const double speed = kMaxSpeed * sensitivity * precisionScale * ResponseCurve(state.mouseSmoothMag, kExponent);
state.mouseAccumX += dirX * speed * dt;
state.mouseAccumY += -dirY * speed * dt; // stick Y is up-positive, screen Y is down-positive
outX = TakeWholePart(state.mouseAccumX);
outY = TakeWholePart(state.mouseAccumY);
return (outX != 0 || outY != 0);
}
// Integrates both scroll axes for one poll. Returns false when there is nothing to send.
static bool UpdateScroll(GamepadState &state, double stickX, double stickY,
double sensitivity, short &outV, short &outH) {
outV = outH = 0;
const int64_t now = QpcNow();
const double dt = PointerDt(now, state.scrollLastQpc);
if (dt == 0.0) {
return false;
}
// Per-axis here rather than radial: vertical and horizontal scroll are independent
// wheels on the host, and treating them as one vector makes it hard to scroll straight.
const double rateV = NormalizeMagnitude(std::abs(stickY));
const double rateH = NormalizeMagnitude(std::abs(stickX));
if (rateV == 0.0 && rateH == 0.0) {
return false; // remainder is kept, same as the pointer
}
const double scale = kMaxScrollRate * sensitivity * dt;
if (rateV > 0.0) {
state.scrollAccumV += std::copysign(ResponseCurve(rateV, kScrollExponent) * scale, stickY);
}
if (rateH > 0.0) {
state.scrollAccumH += std::copysign(ResponseCurve(rateH, kScrollExponent) * scale, stickX);
}
// A real wheel emits a few dozen events/sec; without this the accumulator would happily
// push 500/sec down the control stream while the stick is fully deflected.
if (state.scrollLastSendQpc != 0 && QpcToMs(now - state.scrollLastSendQpc) < kScrollSendMs) {
return false;
}
state.scrollLastSendQpc = now;
outV = TakeWholePart(state.scrollAccumV);
outH = TakeWholePart(state.scrollAccumH);
return (outV != 0 || outH != 0);
}
// Process all input from the user before updating game state
void moonlight_xbox_dxMain::ProcessInput() {
auto gamepads = Windows::Gaming::Input::Gamepad::Gamepads;
uint16_t gamepadCount = gamepads->Size;
moonlightClient->SetGamepadCount(gamepadCount);
for (UINT i = 0; i < gamepadCount; i++) {
auto &state = this->FindGamepadState(i);
auto result = state.GetComboResult(50); // hold buttons for a short time for View + Menu combo
if (result.comboTriggered) {
DISPATCH_UI(([this]() {
Windows::UI::Xaml::Controls::Flyout::ShowAttachedFlyout(m_streamPage->m_flyoutButton);
}));
// send an empty controller packet, otherwise Sunshine may see View being kept held down,
// triggering the "Home/Guide Button Emulation Timeout" to send a Guide button press after a few seconds.
SendGamepadReadingForState(state, EmptyReading());
// disable future input until the flyout is closed
insideFlyout = true;
continue;
}
if (insideFlyout) {
state.reading = EmptyReading();
state.previousReading = EmptyReading();
continue;
}
// GetComboResult() will have masked off our combo buttons if they are pending
auto reading = result.maskedReading;
auto prevReading = state.previousReading;
// If mouse mode is enabled the gamepad acts as a mouse, instead we pass the raw events to the host
if (keyboardMode) {
auto appState = GetApplicationState();
double multiplier = ((double)appState->MouseSensitivity) / ((double)4.0f);
// B to close
if (PressedEdge(reading, prevReading, GamepadButtons::B)) {
if (GetApplicationState()->EnableKeyboard) {
m_streamPage->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this]() {
m_streamPage->m_keyboardView->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}));
keyboardMode = false;
} else {
CoreInputView::GetForCurrentView()->TryHide();
}
}
// X to backspace
if (PressedEdge(reading, prevReading, GamepadButtons::X)) {
moonlightClient->KeyDown((unsigned short)Windows::System::VirtualKey::Back, 0);
} else if (ReleaseEdge(reading, prevReading, GamepadButtons::X)) {
moonlightClient->KeyUp((unsigned short)Windows::System::VirtualKey::Back, 0);
}
// Y to Space
if (PressedEdge(reading, prevReading, GamepadButtons::Y)) {
moonlightClient->KeyDown((unsigned short)Windows::System::VirtualKey::Space, 0);
} else if (ReleaseEdge(reading, prevReading, GamepadButtons::Y)) {
moonlightClient->KeyUp((unsigned short)Windows::System::VirtualKey::Space, 0);
}
// LB to Left
if (PressedEdge(reading, prevReading, GamepadButtons::LeftShoulder)) {
moonlightClient->KeyDown((unsigned short)Windows::System::VirtualKey::Left, 0);
} else if (ReleaseEdge(reading, prevReading, GamepadButtons::LeftShoulder)) {
moonlightClient->KeyUp((unsigned short)Windows::System::VirtualKey::Left, 0);
}
// RB to Right
if (PressedEdge(reading, prevReading, GamepadButtons::RightShoulder)) {
moonlightClient->KeyDown((unsigned short)Windows::System::VirtualKey::Right, 0);
} else if (ReleaseEdge(reading, prevReading, GamepadButtons::RightShoulder)) {
moonlightClient->KeyUp((unsigned short)Windows::System::VirtualKey::Right, 0);
}
// Start to Enter
if (PressedEdge(reading, prevReading, GamepadButtons::Menu)) {
moonlightClient->KeyDown((unsigned short)Windows::System::VirtualKey::Enter, 0);
} else if (ReleaseEdge(reading, prevReading, GamepadButtons::Menu)) {
moonlightClient->KeyUp((unsigned short)Windows::System::VirtualKey::Enter, 0);
}
// Move with right stick
if (isPressed(reading.Buttons, GamepadButtons::LeftThumbstick)) {
short scrollV, scrollH;
if (UpdateScroll(state, reading.RightThumbstickX, reading.RightThumbstickY, multiplier, scrollV, scrollH)) {
if (scrollV != 0) moonlightClient->SendScroll((float)scrollV);
if (scrollH != 0) moonlightClient->SendScrollH((float)scrollH);
}
} else {
// Move with right stick instead of the left one in KB mode. LB/RB are already
// taken by the arrow keys here, so there's no precision modifier in this mode.
short mouseX, mouseY;
if (UpdatePointer(state, reading.RightThumbstickX, reading.RightThumbstickY, multiplier, 1.0, mouseX, mouseY)) {
moonlightClient->SendMousePosition((float)mouseX, (float)mouseY);
}
}
if (reading.LeftTrigger > 0.25 && state.previousReading.LeftTrigger < 0.25) {
moonlightClient->SendMousePressed(BUTTON_LEFT);
} else if (reading.LeftTrigger < 0.25 && state.previousReading.LeftTrigger > 0.25) {
moonlightClient->SendMouseReleased(BUTTON_LEFT);
}
if (reading.RightTrigger > 0.25 && state.previousReading.RightTrigger < 0.25) {
moonlightClient->SendMousePressed(BUTTON_RIGHT);
} else if (reading.RightTrigger < 0.25 && state.previousReading.RightTrigger > 0.25) {
moonlightClient->SendMouseReleased(BUTTON_RIGHT);
}
} else if (mouseMode) {
auto appState = GetApplicationState();
// Position. Hold RB to slow the cursor down for precise targeting -- the opposite
// hand from the pointer stick, so the modifier doesn't disturb your aim.
double multiplier = ((double)appState->MouseSensitivity) / ((double)4.0f);
double precision = isPressed(reading.Buttons, GamepadButtons::RightShoulder) ? kPrecision : 1.0;
short mouseX, mouseY;
if (UpdatePointer(state, reading.LeftThumbstickX, reading.LeftThumbstickY, multiplier, precision, mouseX, mouseY)) {
moonlightClient->SendMousePosition((float)mouseX, (float)mouseY);
}
// Left Click (A or LT)
if (PressedEdge(reading, prevReading, GamepadButtons::A) || (reading.LeftTrigger > 0.25 && state.previousReading.LeftTrigger < 0.25)) {
moonlightClient->SendMousePressed(BUTTON_LEFT);
} else if (ReleaseEdge(reading, prevReading, GamepadButtons::A) || (reading.LeftTrigger < 0.25 && state.previousReading.LeftTrigger > 0.25)) {
moonlightClient->SendMouseReleased(BUTTON_LEFT);
}
// Right Click (X or RT)
if (PressedEdge(reading, prevReading, GamepadButtons::X) || (reading.RightTrigger > 0.25 && state.previousReading.RightTrigger < 0.25)) {
moonlightClient->SendMousePressed(BUTTON_RIGHT);
} else if (ReleaseEdge(reading, prevReading, GamepadButtons::X) || (reading.RightTrigger < 0.25 && state.previousReading.RightTrigger > 0.25)) {
moonlightClient->SendMouseReleased(BUTTON_RIGHT);
}
// Keyboard (Y)
if (PressedEdge(reading, prevReading, GamepadButtons::Y)) {
if (GetApplicationState()->EnableKeyboard) {
m_streamPage->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this]() {
m_streamPage->m_keyboardView->Visibility = Windows::UI::Xaml::Visibility::Visible;
}));
keyboardMode = true;
} else {
CoreInputView::GetForCurrentView()->TryShow(CoreInputViewKind::Keyboard);
}
}
// Scroll
short scrollV, scrollH;
if (UpdateScroll(state, reading.RightThumbstickX, reading.RightThumbstickY, multiplier, scrollV, scrollH)) {
if (scrollV != 0) moonlightClient->SendScroll((float)scrollV);
if (scrollH != 0) moonlightClient->SendScrollH((float)scrollH);
}
// Xbox/Guide Button (B)
if (PressedEdge(reading, prevReading, GamepadButtons::B)) {
moonlightClient->SendGuide(state.hostId, true);
} else if (ReleaseEdge(reading, prevReading, GamepadButtons::B)) {
moonlightClient->SendGuide(state.hostId, false);
}
} else {
// Uncomment to debug gamepad state
// if (state.hasGamepadReadingChanged()) state.DumpState();
SendGamepadReadingForState(state, reading);
}
state.previousReading = reading;
}
}
void moonlight_xbox_dxMain::SetGuideButtonDown(uint32_t hostId, bool isDown) {
auto &state = FindGamepadStateByHostId(hostId);
state.SetGuideButtonDown(isDown);
}
uint16_t moonlight_xbox_dxMain::MakeActiveMask() {
uint16_t activeMask = 0;
for (int i = 0; i < MAX_GAMEPADS; ++i) {
if (m_GamepadState[i].controller != nullptr) {
activeMask |= (1 << m_GamepadState[i].hostId);
}
}
return activeMask;
}
void moonlight_xbox_dxMain::DumpGamepads() {
// list all controllers with their connected status
for (int i = 0; i < MAX_GAMEPADS; ++i) {
if (m_GamepadState[i].controller != nullptr) {
Utils::Logf(" Gamepad #%d: hostId %d\n", m_GamepadState[i].localId, m_GamepadState[i].hostId);
}
}
}
void moonlight_xbox_dxMain::RefreshGamepads() {
auto gamepads = Gamepad::Gamepads;
const int count = gamepads->Size;
const int64_t now = QpcNow();
// For all connected Gamepads, ensure our mapping is correct
for (int localId = 0; localId < count; ++localId) {
auto gamepad = gamepads->GetAt(localId);
bool found = false;
// Do we know about this gamepad already?
for (int i = 0; i < MAX_GAMEPADS; ++i) {
if (m_GamepadState[i].controller == gamepad) {
auto &state = m_GamepadState[i];
// update localId and send arrival packet if necessary
state.localId = localId;
state.lastRefreshedQpc = now;
if (!state.didSendArrival) {
SendGamepadArrival(state);
state.didSendArrival = true;
Utils::Logf("RefreshGamepads: sent arrival packet for Gamepad #%d\n", localId);
}
found = true;
break;
}
}
// It's a new gamepad
if (!found) {
for (int i = 0; i < MAX_GAMEPADS; ++i) {
if (m_GamepadState[i].controller == nullptr) {
// Save the new controller at the first open slot
auto &state = m_GamepadState[i];
state.Reset();
state.controller = gamepad;
state.localId = localId;
state.hostId = i;
state.lastRefreshedQpc = now;
state.reading = EmptyReading();
state.previousReading = EmptyReading();
SendGamepadArrival(state);
state.didSendArrival = true;
Utils::Logf("RefreshGamepads: added new Gamepad #%d in host slot %d\n", state.localId, state.hostId);
break;
}
}
}
}
// Lastly, remove any leftover controllers that are no longer connected
for (int i = 0; i < MAX_GAMEPADS; ++i) {
auto &state = m_GamepadState[i];
if (state.controller != nullptr && state.lastRefreshedQpc != now) {
// Send a disconnect packet and reset this state slot
uint16_t activeMaskMinus = MakeActiveMask();
activeMaskMinus &= ~(1 << state.hostId);
LiSendMultiControllerEvent(state.hostId, activeMaskMinus, 0, 0, 0, 0, 0, 0, 0);
Utils::Logf("RefreshGamepads: removed Gamepad #%d from host slot %d\n", state.localId, state.hostId);
state.Reset();
}
}
}
void moonlight_xbox_dxMain::SendGamepadArrival(GamepadState &state) {
// Only ever send this once
if (state.didSendArrival) return;
uint8_t type = IsXbox() ? LI_CTYPE_XBOX : LI_CTYPE_UNKNOWN;
uint32_t supportedButtonFlags = A_FLAG | B_FLAG | X_FLAG | Y_FLAG | BACK_FLAG | PLAY_FLAG | LS_CLK_FLAG | RS_CLK_FLAG | UP_FLAG | DOWN_FLAG | LEFT_FLAG | RIGHT_FLAG | LB_FLAG | RB_FLAG;
uint32_t capabilities = LI_CCAP_ANALOG_TRIGGERS | LI_CCAP_RUMBLE | LI_CCAP_TRIGGER_RUMBLE;
int rc = LiSendControllerArrivalEvent(state.hostId, MakeActiveMask(), type, supportedButtonFlags, capabilities);
if (rc != 0) {
Utils::Logf("LiSendControllerArrivalEvent error: %d\n", rc);
}
}
// Renders the current frame according to the current application state.
// Returns true if the frame was rendered and is ready to be displayed.
bool moonlight_xbox_dxMain::Render() {
// Don't try to render anything before the first Update.
if (m_timer.GetFrameCount() == 0) {
return false;
}
// Render the scene objects.
bool showImGui = m_deviceResources->GetShowImGui();
// ImGui setup and update handling (which we don't use)
if (showImGui) {
ImGui_ImplDX11_NewFrame();
ImGui_ImplUwp_NewFrame(m_deviceResources->GetPixelWidth(), m_deviceResources->GetPixelHeight());
ImGui::NewFrame();
}
bool shouldPresent = Pacer::instance().renderOnMainThread(m_sceneRenderer);
if (shouldPresent) {
// avoid useless rendering without an underlying frame change
m_LogRenderer->Render();
m_statsTextRenderer->Render(showImGui);
}
if (showImGui) {
ImGui::EndFrame();
if (shouldPresent) {
RenderImGui();
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
}
}
return shouldPresent;
}
// Set this to true to use the ImGui demo/debug tools.
// Normal ImGui code can be used anywhere in the other Render() methods.
void moonlight_xbox_dxMain::RenderImGui() {
bool show_demo_window = false;
bool show_metrics = false;
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if (show_demo_window) {
ImGui::ShowDemoWindow(&show_demo_window);
}
if (show_metrics) {
ImGui::ShowMetricsWindow(&show_metrics);
}
}
// Notifies renderers that device resources need to be released.
void moonlight_xbox_dxMain::OnDeviceLost() {
m_sceneRenderer->ReleaseDeviceDependentResources();
m_LogRenderer->ReleaseDeviceDependentResources();
m_statsTextRenderer->ReleaseDeviceDependentResources();
}
// Notifies renderers that device resources may now be recreated.
void moonlight_xbox_dxMain::OnDeviceRestored() {
m_sceneRenderer->CreateDeviceDependentResources();
m_LogRenderer->CreateDeviceDependentResources();
m_statsTextRenderer->CreateDeviceDependentResources();
CreateDeviceDependentResources();
CreateWindowSizeDependentResources();
}
void moonlight_xbox_dxMain::SetFlyoutOpened(bool value) {
insideFlyout = value;
}
void moonlight_xbox_dxMain::Disconnect() {
moonlightClient->StopStreaming();
m_sceneRenderer->Stop();
}
void moonlight_xbox_dxMain::CloseApp() {
moonlightClient->StopApp();
}
void moonlight_xbox_dxMain::ExitStreamPage() {
// If a frontend launched us with a launchOnExit return URI, go back to it and exit
auto state = GetApplicationState();
Platform::String ^ returnUri = state->launchOnExitUri;
if (returnUri != nullptr && !returnUri->IsEmpty()) {
state->launchOnExitUri = nullptr;
try {
auto uri = ref new Windows::Foundation::Uri(returnUri);
concurrency::create_task(Windows::System::Launcher::LaunchUriAsync(uri)).then([](concurrency::task<bool> t) {
try {
if (t.get()) {
Windows::ApplicationModel::Core::CoreApplication::Exit();
} else {
Utils::Log("ExitStreamPage: failed to launch the return URI\n");
}
} catch (...) {
Utils::Log("ExitStreamPage: failed to launch the return URI\n");
}
});
} catch (...) {
Utils::Log("ExitStreamPage: the return URI is not a valid URI\n");
}
// Keep navigating back below so the app is in a sane state if the launch fails
}
bool reachedAppPage = false;
try {
auto rootFrame = dynamic_cast<Windows::UI::Xaml::Controls::Frame ^>(Windows::UI::Xaml::Window::Current->Content);
if (!rootFrame) return;
auto current = dynamic_cast<AppPage ^>(rootFrame->Content);
if (current != nullptr) {
reachedAppPage = true;
}
try {
rootFrame->GoBack();
} catch (...) {
Utils::Log("ExitStreamPage: Failed to GoBack()\n");
}
if (!reachedAppPage) {
if (dynamic_cast<AppPage ^>(rootFrame->Content) != nullptr) reachedAppPage = true;
}
if (!reachedAppPage) {
try {
rootFrame->Navigate(Windows::UI::Xaml::Interop::TypeName(HostSelectorPage::typeid));
} catch (...) {
rootFrame->Content = nullptr;
Utils::Log("ExitStreamPage: Failed to return to HostSelectorPage\n");
}
}
} catch (...) {
Utils::Log("ExitStreamPage: An error occurred\n");
}
}
void moonlight_xbox_dxMain::OnKeyDown(unsigned short virtualKey, char modifiers) {
if (this == nullptr || moonlightClient == nullptr) return;
moonlightClient->KeyDown(virtualKey, modifiers);
}
void moonlight_xbox_dxMain::OnKeyUp(unsigned short virtualKey, char modifiers) {
if (this == nullptr || moonlightClient == nullptr) return;
moonlightClient->KeyUp(virtualKey, modifiers);
}
void moonlight_xbox_dxMain::SendGuideButton(int duration) {
concurrency::create_async([duration, this]() {
// We change the state of the fake guide button, which will be included in the regular controller packets
auto &state = FindFirstGamepad();
SetGuideButtonDown(state.hostId, true);
Sleep(duration);
SetGuideButtonDown(state.hostId, false);
});
}
void moonlight_xbox_dxMain::SendWinAltB() {
// Win-Alt-B = Toggle HDR
concurrency::create_async([this]() {
moonlightClient->KeyDown((unsigned short)Windows::System::VirtualKey::LeftWindows, 0);
moonlightClient->KeyDown((unsigned short)Windows::System::VirtualKey::Menu, 0);
moonlightClient->KeyDown((unsigned short)Windows::System::VirtualKey::B, 0);
Sleep(100);
moonlightClient->KeyUp((unsigned short)Windows::System::VirtualKey::B, 0);
moonlightClient->KeyUp((unsigned short)Windows::System::VirtualKey::Menu, 0);
moonlightClient->KeyUp((unsigned short)Windows::System::VirtualKey::LeftWindows, 0);
});
}
bool moonlight_xbox_dxMain::ToggleLogs() {
bool visible = m_LogRenderer->GetVisible();
DISPATCH_UI([&] {
m_LogRenderer->ToggleVisible();
});
return visible ? false : true;
}
bool moonlight_xbox_dxMain::ToggleStats() {
bool visible = m_statsTextRenderer->GetVisible();
DISPATCH_UI([&] {
m_statsTextRenderer->ToggleVisible();
});
return visible ? false : true;
}
/// Gamepad Handling
GamepadState &moonlight_xbox_dxMain::FindGamepadState(uint32_t localId) {
int i = 0;
for (i = 0; i < MAX_GAMEPADS; i++) {
if (m_GamepadState[i].controller != nullptr && m_GamepadState[i].localId == localId) {
return m_GamepadState[i];
}
}
static GamepadState nullState;
return nullState;
}