-
Notifications
You must be signed in to change notification settings - Fork 692
/
InputServices.cpp
14976 lines (13183 loc) · 606 KB
/
InputServices.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 (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
// IInputManager implementation
//
// Synchronous Input events are fired without taking the reentrancy guard,
// to allow the application's input event handlers to call API like the
// following, which pump messages and cause reentrancy:
// CoreDispatcher.ProcessEvents
// CoWaitForMultipleHandles
//
// For example, tick or other input events may be pumped while InputManager
// is in a synchronous callout. So InputManager needs to be hardened against
// reentrancy. It should ensure that objects are alive and that state is
// re-validated after these callouts return.
#include "precomp.h"
#include "InputServices.h"
#include "PointerAnimationUsingKeyFrames.h"
#include "DirectManipulationServiceSharedState.h"
#include "XboxUtility.h"
#include <RuntimeEnabledFeatures.h>
#include <DependencyLocator.h>
#include "Timer.h"
#include "TimeSpan.h"
#include "ContextRequestedEventArgs.h"
#include <KeyboardUtility.h>
#include <UIThreadScheduler.h>
#include "EnumDefs.h"
#include "XamlIslandRoot.h"
#include "TextCommon.h"
#include "KeyboardAcceleratorUtility.h"
#include <XamlOneCoreTransforms.h>
#include "InitialFocusSIPSuspender.h"
#include "FocusLockOverrideGuard.h"
#include "JupiterWindow.h"
#include <DXamlServices.h>
#include <FocusManagerLostFocusEventArgs.h>
#include <FocusManagerGotFocusEventArgs.h>
#include <CaretBrowsingGlobal.h>
#include <FeatureFlags.h>
#include <FocusSelection.h>
#include "RootScale.h"
#include "DirectManipulationService.h"
#include "isapipresent.h"
#include <ReentrancyGuard.h>
#include <FrameworkUdk/Containment.h>
// Bug 45792810: Explorer first frame - DesktopWindowXamlSource spends 30ms on RoGetActivationFactory
// Bug 46468883: [1.4 servicing] Explorer first frame - DesktopWindowXamlSource spends 30ms on RoGetActivationFactory
#define WINAPPSDK_CHANGEID_46468883 46468883
#undef max
#undef min
using namespace DirectUI;
using namespace Focus;
// Define as 1 (i.e. XCP_TRACE_OUTPUT_MSG) to get DirectManipulation debug outputs, and 0 otherwise
#define DMIM_DBG 0
//#define DM_DEBUG
// Define as 1 (i.e. XCP_TRACE_OUTPUT_MSG) to get DirectManipulation verbose debug outputs, and 0 otherwise
#define DMIMv_DBG 0
//#define TIEIM_DBG
#define ExitOnSetContactFailure(x) if (x) { goto Cleanup; }
using namespace RuntimeFeatureBehavior;
CInputServices::CInputServices(_In_ CCoreServices *pCoreService)
: m_pVisualTree(pCoreService->GetMainVisualTree())
{
Init(pCoreService);
}
void
CInputServices::Init(_In_ CCoreServices *pCoreService)
{
m_qpcFirstPointerUpSinceLastFrame = 0;
m_pTextCompositionTargetDO = NULL;
XCP_WEAK(&m_pCoreService);
m_pCoreService = static_cast<CCoreServices*>(pCoreService);
m_pEventManager = m_pCoreService->GetEventManager();
m_pEventManager->AddRef();
m_bStylusInvertedOnDown = FALSE;
m_ptStylusPosLast.x = 0.0;
m_ptStylusPosLast.y = 0.0;
m_fStylusPressureFactorLast = 0.0;
m_pViewports = NULL;
m_pCrossSlideViewports = NULL;
m_pSecondaryContentRelationshipsToBeApplied = NULL;
m_pDMServices = NULL;
m_pDMCrossSlideService = NULL;
m_DMServiceSharedState = std::make_shared<DirectManipulationServiceSharedState>();
m_cCrossSlideContainers = 0;
m_hWnd = NULL;
if (!static_cast<CCoreServices*>(pCoreService)->IsTSF3Enabled())
{
// initialize to current input language
m_inputLang = GetKeyboardLayout(0);
}
#ifdef DM_DEBUG
EvaluateInfoTracingStatuses();
#endif // DM_DEBUG
}
CInputServices::~CInputServices()
{
Reset();
}
void CInputServices::Reset()
{
ReleaseInterface(m_pEventManager);
ReleaseInterface(m_pDMCrossSlideService);
IGNOREHR(DeleteDMViewports());
IGNOREHR(DeleteDMCrossSlideViewports());
IGNOREHR(DeleteSecondaryContentRelationshipsToBeApplied());
IGNOREHR(DeleteDMServices());
DeleteDMContainersNeedingInitialization();
// Cleanup all create pointer objects
DestroyPointerObjects();
m_pCoreService = nullptr;
}
void CInputServices::ResetCrossSlideService()
{
if (m_pDMCrossSlideService)
{
m_pDMCrossSlideService->DeactivateDirectManipulationManager();
ReleaseInterface(m_pDMCrossSlideService);
}
}
void CInputServices::SetApplicationHwnd(_In_ HWND hWnd)
{
XHANDLE previousHWnd = m_hWnd;
m_hWnd = static_cast<XHANDLE>(hWnd);
if (previousHWnd && previousHWnd != m_hWnd)
{
// The cross slide service directly references the hwnd we created it with
// so if the hwnd changes, we need to recreate the service.
ResetCrossSlideService();
}
}
//------------------------------------------------------------------------
//
// Method: DestroyPointerObjects
//
// Synopsis:
// Destroy all created pointer objects.
//
//------------------------------------------------------------------------
void
CInputServices::DestroyPointerObjects()
{
// Destroy all interaction engines
m_interactionManager.DestroyAllInteractionEngine();
// Clean up the interaction map chain
for (xchainedmap<XUINT32, CUIElement*>::const_iterator it = m_mapInteraction.begin();
it != m_mapInteraction.end();
++it)
{
CUIElement* pInteractionElement = (*it).second;
if (pInteractionElement)
{
ReleaseInterface(pInteractionElement);
}
}
m_mapInteraction.Clear();
m_mapPointerDownTracker.clear();
// Clean up the manipulation container map chain
m_mapManipulationContainer.Clear();
// Clean up pointer state
for (auto pair : m_mapPointerState)
{
std::shared_ptr<CPointerState> pointerState = pair.second;
if (pointerState)
{
pointerState->Reset();
}
}
m_mapPointerState.Clear();
// Clean up PointerEnter state chained map
m_mapPointerEnterFromElement.clear();
m_mapPointerNodeDirtyFromElement.clear();
// Clean up pointer exited state.
for (xchainedmap<PointerExitedStateKey, CPointerExitedState*>::const_iterator it = m_mapPointerExitedState.begin();
it != m_mapPointerExitedState.end();
++it)
{
CPointerExitedState* pPointerExitedState = (*it).second;
if (pPointerExitedState)
{
IGNOREHR(pPointerExitedState->SetExitedDO(NULL));
IGNOREHR(pPointerExitedState->SetEnteredDO(NULL));
delete pPointerExitedState;
}
}
m_mapPointerExitedState.Clear();
for (const xref_ptr<CContentRoot>& contentRoot: m_pCoreService->GetContentRootCoordinator()->GetContentRoots())
{
contentRoot->GetInputManager().GetPointerInputProcessor().SetPrimaryPointerId(-1);
// Unregister InputPane that clean InputPane handler, interaction and root SV.
contentRoot->GetInputManager().DestroyInputPaneHandler();
}
}
//------------------------------------------------------------------------
//
// Method: ObjectLeavingTree
//
// Synopsis:
// Currently called when the provided DO leaves the tree.
// Handles pointer and drag drop cleanup
//
//------------------------------------------------------------------------
_Check_return_ HRESULT
CInputServices::ObjectLeavingTree(_In_ CDependencyObject *object)
{
// Handle pointer state cleanup.
IFC_RETURN(CleanPointerElementObject(object));
// If we have an active drag, then we need to see if this "leave" invalidates
// our dragEnterDO. The dragEnterDO is the lowest element in the ancestor that
// has recorded a drag enter. If this item is leaving the tree, then we need
// fix up the dragEnterDO. This is complicated by the fact that it may not be
// our dragEnterDO that is leaving the tree, but one of its ancestors.
// So, if we need to fix up the dragEnterDO and this element was in the drag enter
// chain and its parent is still alive, we will set the current dragEnterDO to the
// parent.
if (CContentRoot* contentRoot = VisualTree::GetContentRootForElement(object))
{
xref_ptr<CDependencyObject>& dragEnterDO = contentRoot->GetInputManager().GetDragDropProcessor().GetDragEnterDONoRef();
if (dragEnterDO && object->HasDragEnter())
{
CDependencyObject* parent = object->GetParentInternal();
if (parent)
{
if (parent->IsActive())
{
// Our parent is still in the tree so it becomes the new dragEnterDO
dragEnterDO = parent;
ASSERT(parent->HasDragEnter());
}
else if (parent->OfTypeByIndex<KnownTypeIndex::RootVisual>())
{
// Our parent is the root visual, so we don't need a dragEnterDO
// Note, RootVisual is never active so it won't get included above.
dragEnterDO = nullptr;
}
}
}
ASSERT(dragEnterDO != object);
}
return S_OK;
}
//------------------------------------------------------------------------
//
// Method: CleanPointerElementObject
//
// Synopsis:
// Currently called when the provided DO leaves the tree.
// Cleans cached structures used for raising raw pointer, gestures
// manipulation events.
// Conditionally raises PointerCaptureLost, PointerEnter and
// PointerLeave events.
//
//------------------------------------------------------------------------
_Check_return_ HRESULT
CInputServices::CleanPointerElementObject(_In_ CDependencyObject *pObject)
{
std::shared_ptr<CPointerState> pointerState;
XUINT32 pointerId = 0;
CUIElement* pUIElement = NULL;
for (auto it = m_mapPointerState.begin();
it != m_mapPointerState.end();
++it)
{
pointerState = (*it).second;
if (pointerState)
{
CDependencyObject* pPointerEnterDO = NULL;
CDependencyObject* pPointerCaptureDO = NULL;
pointerId = pointerState->GetPointerId();
pPointerEnterDO = pointerState->GetEnterDO();
pPointerCaptureDO = pointerState->GetCaptureDO();
if (pObject == pPointerEnterDO || pObject == pPointerCaptureDO)
{
if (pObject == pPointerCaptureDO)
{
// Do not fire the PointerCaptureLost event while processing Tick.
// The processing tick can clean up the elements that is collected by GC.
if (m_pCoreService)
{
IXcpBrowserHost *pBrowserHost = m_pCoreService->GetBrowserHost();
ITickableFrameScheduler *pFrameScheduler = pBrowserHost->GetFrameScheduler();
if (pFrameScheduler != NULL && pFrameScheduler->IsInTick() == FALSE)
{
CContentRoot* contentRoot = VisualTree::GetContentRootForElement(pPointerCaptureDO);
IFC_RETURN(contentRoot->GetInputManager().GetPointerInputProcessor().ReleasePointerCapture(pPointerCaptureDO, pointerState->GetCapturePointer()));
}
}
}
if (pObject == pPointerEnterDO)
{
IFC_RETURN(ProcessPointerExitedEventByPointerEnteredElementStateChange(pPointerEnterDO, pointerState));
}
}
m_mapPointerEnterFromElement.erase(pObject);
m_mapPointerNodeDirtyFromElement.erase(pObject);
if (m_mapInteraction.ContainsKey(pointerId))
{
CUIElement *pInteractionElement = NULL;
IFC_RETURN(m_mapInteraction.Get(pointerId, pInteractionElement));
if (pObject == pInteractionElement)
{
IFC_RETURN(m_mapInteraction.Remove(pointerId, pInteractionElement));
ReleaseInterface(pInteractionElement);
}
}
PointerDownTrackerMap::iterator itFind = m_mapPointerDownTracker.find(pointerId);
if (itFind != m_mapPointerDownTracker.end())
{
CUIElement *pTrackedElement = itFind->second;
if (pObject == pTrackedElement)
{
m_mapPointerDownTracker.erase(itFind);
}
}
// Do not remove pointerState from m_mapPointerState that will be removed when
// the specified pointer Id is completed by XCP_POINTERLEAVE, XCP_POINTERCAPTURECHANGED or XCP_POINTERSUSPENDED.
pointerId = 0;
pointerState = nullptr;
}
}
pUIElement = do_pointer_cast<CUIElement>(pObject);
if (pUIElement)
{
// Remove the manipulation container that is associated with the leaving element.
if (m_mapManipulationContainer.ContainsKey(pUIElement))
{
CUIElement *pManipulationContainer = NULL;
IFC_RETURN(m_mapManipulationContainer.Remove(pUIElement, pManipulationContainer));
}
// Remove the potential interaction engine associated with the leaving element
// so that it is no longer pegged.
m_interactionManager.DestroyInteractionEngine(pUIElement);
}
return S_OK;
}
//------------------------------------------------------------------------
//
// Method: ProcessPointerExitedEventByPointerEnteredElementStateChange
//
// Synopsis:
// Process PointerExited event when the pointer entered element is leaving
// the tree, disabled or collapsed.
//
//------------------------------------------------------------------------
_Check_return_ HRESULT
CInputServices::ProcessPointerExitedEventByPointerEnteredElementStateChange(
_In_ CDependencyObject* pElementDO,
_In_opt_ std::shared_ptr<CPointerState> pointerState)
{
HRESULT hr = S_OK;
const CRootVisual *pRootVisual = NULL;
CDependencyObject* pElementRoot = NULL;
CDependencyObject* pElementParent = NULL;
CDependencyObject* pPointerEnteredDO = NULL;
CDependencyObject* pTemplatedParent = NULL;
CDependencyObject* pNewPointerEnteredDO = NULL;
CFrameworkElement* pPointerEnteredFE = NULL;
CUIElement* pPointerEnteredUIE = NULL;
CUIElement* pNewPointerEnteredUIE = NULL;
CUIElement* pElementUIE = NULL;
CPointerExitedState* pPointerExitedState = NULL;
XHANDLE hWindow = NULL;
CPointer *pPointer = NULL;
CPointerEventArgs *pPointerArgs = NULL;
XUINT32 pointerId = 0;
XUINT32 modifierKeys = 0;
bool bIsAncestor = false;
bool bFoundPointerState = pointerState != nullptr;
CREATEPARAMETERS cp(m_pCoreService);
CContentRoot* contentRoot = VisualTree::GetContentRootForElement(pElementDO);
CCoreServices *pCoreService = m_pCoreService;
IFCPTR(pCoreService);
pCoreService->AddRef();
IFCPTR(pElementDO);
IFCPTR(m_pEventManager);
pElementParent = pElementDO;
pRootVisual = VisualTree::GetRootForElement(pElementDO);
if (pRootVisual)
{
while (pElementParent && pElementParent != pRootVisual)
{
pElementRoot = pElementParent;
pElementParent = pElementParent->GetParentInternal();
}
}
// Do not process if it is in the middle of reset visual tree.
if (m_pCoreService->IsInResetVisualTree() || !pElementRoot || !pElementRoot->IsActive() || pElementRoot->IsProcessingEnterLeave())
{
goto Cleanup;
}
// Get the current pointer state that hold the pointer entered element.
if (pointerState == nullptr)
{
for (auto it = m_mapPointerState.begin();
it != m_mapPointerState.end();
++it)
{
pointerState = (*it).second;
if (pointerState)
{
bIsAncestor = false;
pointerId = pointerState->GetPointerId();
pPointerEnteredDO = pointerState->GetEnterDO();
if (pPointerEnteredDO)
{
pPointerEnteredUIE = do_pointer_cast<CUIElement>(pPointerEnteredDO);
pElementUIE = do_pointer_cast<CUIElement>(pElementDO);
if (pPointerEnteredUIE && pElementUIE)
{
bIsAncestor = pElementUIE->IsAncestorOf(pPointerEnteredUIE);
}
if (pElementDO == pPointerEnteredDO || bIsAncestor)
{
bFoundPointerState = TRUE;
break;
}
}
}
}
}
else
{
pointerId = pointerState->GetPointerId();
pPointerEnteredDO = pointerState->GetEnterDO();
}
if (bFoundPointerState)
{
ASSERT(pPointerEnteredDO && pointerState && pointerId);
// The use of hWindow and onCorrectThread is a hold over from previous versions where we could get pointer events for different threads.
// This doesn't seem to be currently possible, so we end up always being on the correct thread. However, there still seems to be some
// additional changes to lifted input coming where this might be needed again.
const bool onCorrectThread = (!hWindow || ::GetWindowThreadProcessId(static_cast<HWND>(hWindow), nullptr /*dwProcessId*/) == ::GetCurrentThreadId());
bool isPointerInfoValid = false;
PointerInfo pointerInfo = {};
wrl::ComPtr<ixp::IPointerPoint> pointerPoint;
if (onCorrectThread)
{
CXamlIslandRoot* pIslandRoot = contentRoot->GetXamlIslandRootNoRef();
if (pIslandRoot)
{
pointerPoint = pIslandRoot->GetPreviousPointerPoint();
}
else
{
CJupiterWindow* jupiterWindow = DirectUI::DXamlServices::GetCurrentJupiterWindow();
pointerPoint = jupiterWindow->GetInputSiteAdapterPointerPoint();
}
if (pointerPoint)
{
isPointerInfoValid = SUCCEEDED(GetPointerInfoFromPointerPoint(pointerPoint.Get(), &pointerInfo));
}
}
// Get the current pointer information and fire PointerExited event.
if (isPointerInfoValid)
{
// Get the new entered element to stop the bubbling PointerExit.
pPointerEnteredFE = do_pointer_cast<CFrameworkElement>(pPointerEnteredDO);
if (pPointerEnteredFE)
{
pTemplatedParent = pPointerEnteredFE->GetTemplatedParent();
while (pTemplatedParent)
{
pPointerEnteredFE = do_pointer_cast<CFrameworkElement>(pTemplatedParent);
if (pPointerEnteredFE && pPointerEnteredFE->GetTemplatedParent())
{
pTemplatedParent = pPointerEnteredFE->GetTemplatedParent();
}
else
{
break;
}
}
if (pTemplatedParent)
{
pNewPointerEnteredDO = pTemplatedParent->GetParentInternal();
}
}
// Set the new entered element as the current entered element's parent.
if (!pNewPointerEnteredDO)
{
pNewPointerEnteredDO = pPointerEnteredDO->GetParentInternal();
}
if (pNewPointerEnteredDO)
{
// Ensure the new entered element is enabled and hit-test visible.
pNewPointerEnteredUIE = do_pointer_cast<CUIElement>(pNewPointerEnteredDO);
while (pNewPointerEnteredUIE)
{
if (!(pNewPointerEnteredUIE->IsHitTestVisible()) ||
!(pNewPointerEnteredUIE->IsEnabled()))
{
pNewPointerEnteredDO = static_cast<CDependencyObject*>(pNewPointerEnteredDO)->GetParentInternal();
pNewPointerEnteredUIE = do_pointer_cast<CUIElement>(pNewPointerEnteredDO);
}
else
{
break;
}
}
}
if (pointerInfo.m_pointerInputType == XcpPointerInputTypeMouse && !contentRoot->GetInputManager().GetPointerInputProcessor().IsProcessingPointerInput())
{
// If the entered element is changing the visual state that used with the mouse pointer input device and
// it is not the input stack call, we need to process PointerExited now asynchronously.
//
// Raise PointerExited event on the pointer entered element that leave the tree, visibility collapsed or disabled.
// Raise PointerEntered event if the pointer is positioned to the new contact element. For example, collapse of
// the original pointer entered element.
// Create the pointer event arg.
pPointerArgs = new CPointerEventArgs(pCoreService);
pPointerArgs->SetGlobalPoint(pointerState->GetLastPosition());
// Set the original source element
IFC(pPointerArgs->put_Source(pPointerEnteredDO));
// Set the Pointer object
IFC(CPointer::Create((CDependencyObject**)&pPointer, &cp));
IFC(pPointer->SetPointerFromPointerInfo(pointerInfo));
pPointerArgs->m_pPointer = pPointer;
pPointerArgs->m_pPointerPoint = pointerPoint.Get();
pPointer = NULL;
// Get the current key modifiers and set to PointerArgs.
IFC(gps->GetKeyboardModifiersState(&modifierKeys));
IFC(ContentRootInput::PointerInputProcessor::SetPointerKeyModifiers(modifierKeys, pPointerArgs));
if (pNewPointerEnteredDO)
{
IFC(contentRoot->GetInputManager().GetPointerInputProcessor().ProcessPointerEnterLeave(
pNewPointerEnteredDO,
pPointerEnteredDO,
pointerId,
pPointerArgs,
FALSE /* bSkipLeave */,
FALSE /* bForceRaisePointerEntered */,
TRUE /* bIgnoreHitTestVisibleForPointerExited */,
TRUE /*bAsyncEvent*/));
}
else
{
IFC(contentRoot->GetInputManager().GetPointerInputProcessor().ProcessPointerLeave(pPointerEnteredDO, pointerId, pPointerArgs, TRUE /*bAsyncEvent*/));
}
}
else
{
// Save the pointer exited state information to process PointerExited event
// on the next WM_POINTERXXX input stack.
PointerExitedStateKey key = { pointerId, pPointerEnteredDO };
if (m_mapPointerExitedState.ContainsKey(key))
{
IFC(m_mapPointerExitedState.Get(key, pPointerExitedState));
}
else
{
pPointerExitedState = new CPointerExitedState(pointerId);
IFC(m_mapPointerExitedState.Add(key, pPointerExitedState));
}
if (pPointerExitedState->GetExitedDONoRef() == NULL)
{
IFC(pPointerExitedState->SetExitedDO(pPointerEnteredDO));
}
if (pNewPointerEnteredDO && pPointerExitedState->GetEnteredDONoRef() == NULL)
{
IFC(pPointerExitedState->SetEnteredDO(static_cast<CDependencyObject*>(pNewPointerEnteredDO)));
}
// In case of having pNewPointerEnteredDO, we need to ensure the PointerExited firing element's
// managed object life time by calling PegManagedPeer() while processing PointerExited event.
// If pNewPointerEnteredDO is null, we will only fire PointerExited event once to the current
// pointer exited DO so we don't need to call PegManagedPeer().
// The pointer exited DO doesn't call PegManagedPeer() here since it is already pegged by
// calling SetEnteredDO() above.
if (pNewPointerEnteredDO)
{
auto peggedPointerExitedDOs = pPointerExitedState->GetPeggedPointerExitedDOs();
pElementParent = pPointerEnteredDO;
while (pElementParent)
{
IFC(pElementParent->PegManagedPeer(TRUE /* isShutdownException */));
peggedPointerExitedDOs->push_back(xref::get_weakref(pElementParent));
pElementParent = pElementParent->GetParentInternal();
if (pElementParent == pNewPointerEnteredDO)
{
break;
}
}
}
}
// Set the pointer entered element.
IFC(pointerState->SetEnterDO(pNewPointerEnteredDO ? static_cast<CDependencyObject*>(pNewPointerEnteredDO) : NULL));
}
}
Cleanup:
ReleaseInterface(pPointer);
ReleaseInterface(pPointerArgs);
ReleaseInterface(pCoreService);
RRETURN(hr);
}
//------------------------------------------------------------------------
//
// Method: NotifyWindowDestroyed
//
// Synopsis:
// Update the destroyed window handle on PointerState.
//
//------------------------------------------------------------------------
void
CInputServices::NotifyWindowDestroyed(
_In_ XHANDLE hDestroyedWindow)
{
for (auto it = m_mapPointerState.begin();
it != m_mapPointerState.end();
++it)
{
std::shared_ptr<CPointerState> pointerState = (*it).second;
if (pointerState && pointerState->GetWindowHandle() == hDestroyedWindow)
{
pointerState->SetWindowHandle(NULL);
}
}
}
//------------------------------------------------------------------------
//
// Method: DestroyInteractionEngine
//
// Synopsis:
// Destroy the specified interaction engine on the element.
//
//------------------------------------------------------------------------
void
CInputServices::DestroyInteractionEngine(
_In_ CUIElement* pDestroyElement)
{
m_interactionManager.DestroyInteractionEngine(pDestroyElement);
}
XUINT32 CInputServices::AddRef()
{
return ++m_ref;
}
XUINT32 CInputServices::Release()
{
int ref = --m_ref;
if (ref == 0)
{
delete this;
}
return ref;
}
//------------------------------------------------------------------------
//
// Method: ProcessInput
//
// Synopsis:
// This is what handles the actual input.
//------------------------------------------------------------------------
_Check_return_
HRESULT CInputServices::ProcessInput(_In_ InputMessage *pMsg, _In_ CContentRoot* contentRoot, _Out_ XINT32 *handled)
{
//validate pointers
IFCPTR_RETURN(pMsg);
IFCPTR_RETURN(handled);
// Initialize handled
*handled = FALSE;
bool shouldPlayInteractionSound = false;
switch (pMsg->m_msgID)
{
case XCP_DEACTIVATE:
// If we lose activation, then we need to reset the m_fKeyDownHandled flag
// so that if we receive a XCP_CHAR it isn't ignored
contentRoot->GetInputManager().SetKeyDownHandled(false);
contentRoot->GetInputManager().SetNoCandidateDirectionPerTick(FocusNavigationDirection::None);
__fallthrough;
case XCP_ACTIVATE:
{
const bool shiftPressed = ((pMsg->m_modifierKeys & KEY_MODIFIER_SHIFT) != 0);
const bool isActivating = (pMsg->m_msgID == XCP_ACTIVATE);
IFC_RETURN(contentRoot->GetInputManager().ProcessWindowActivation(shiftPressed, isActivating));
}
break;
case XCP_POINTERDOWN:
case XCP_POINTERUPDATE:
case XCP_POINTERUP:
case XCP_POINTERENTER:
case XCP_POINTERLEAVE:
case XCP_POINTERWHEELCHANGED:
case XCP_POINTERCAPTURECHANGED:
case XCP_POINTERSUSPENDED:
contentRoot->GetInputManager().SetShouldAllRequestFocusSound(true);
IFC_RETURN(contentRoot->GetInputManager().GetPointerInputProcessor().ProcessPointerInput(pMsg, handled));
contentRoot->GetInputManager().SetShouldAllRequestFocusSound(false);
shouldPlayInteractionSound = true;
break;
case XCP_DMPOINTERHITTEST:
IFC_RETURN(ProcessDirectManipulationPointerHitTest(pMsg, contentRoot, handled));
break;
case XCP_KEYUP:
case XCP_KEYDOWN:
case XCP_CHAR:
case XCP_DEADCHAR:
{
bool bHandled = false;
IFC_RETURN(contentRoot->GetInputManager().ProcessKeyboardInput(
pMsg->m_platformKeyCode,
pMsg->m_physicalKeyStatus,
pMsg->m_msgID,
nullptr /* deviceId */,
pMsg->m_bIsSecondaryMessage,
pMsg->m_hPlatformPacket,
&bHandled));
*handled = bHandled;
break;
}
case XCP_GOTFOCUS:
case XCP_LOSTFOCUS:
IFC_RETURN(contentRoot->GetInputManager().ProcessFocusInput(pMsg, handled));
break;
case XCP_CONTEXTMENU:
{
bool bHandled = false;
IFC_RETURN(contentRoot->GetInputManager().RaiseRightTappedEventFromContextMenu(&bHandled));
*handled = bHandled;
break;
}
case XCP_INPUTLANGCHANGE:
IFC_RETURN(ProcessInputLanguageChange(pMsg, contentRoot, handled));
break;
case XCP_WINDOWMOVE:
IFC_RETURN(ProcessWindowMove(pMsg, contentRoot));
break;
case XCP_NULL:
default:
ASSERT(FALSE);
break;
}
if (shouldPlayInteractionSound)
{
// Play the interaction sound if there is a requested sound during processing input
IFC_RETURN(FxCallbacks::ElementSoundPlayerService_PlayInteractionSound());
}
return S_OK;
}
//------------------------------------------------------------------------
//
// Method: CreatePointerCaptureLostEventArgs
//
// Synopsis:
// Create the argument we will be sending to listeners of the
// PointerCaptureLost event.
//
//------------------------------------------------------------------------
_Check_return_ HRESULT
CInputServices::CreatePointerCaptureLostEventArgs(
_In_ CDependencyObject *pSenderObject,
_In_ XPOINTF pointLast,
_In_ CPointer* pPointer,
_Out_ CPointerEventArgs **ppPointerEventArgs)
{
HRESULT hr = S_OK;
CPointerEventArgs* pArgs = NULL;
CCoreServices *pCoreService = m_pCoreService;
IFCPTR(pCoreService);
pCoreService->AddRef();
IFCPTR(pSenderObject);
IFCPTR(pPointer);
IFCPTR(ppPointerEventArgs);
*ppPointerEventArgs = NULL;
pArgs = new CPointerEventArgs(pCoreService);
pArgs->SetGlobalPoint(pointLast);
// Set the source
IFC(pArgs->put_Source(pSenderObject));
// Set Pointer object
pArgs->m_pPointer = pPointer;
pPointer->AddRef();
*ppPointerEventArgs = pArgs;
pArgs = NULL;
Cleanup:
ReleaseInterface(pArgs);
ReleaseInterface(pCoreService);
RRETURN(hr);
}
//------------------------------------------------------------------------
//
// Method: SetCursor
//
// Synopsis:
// If the InputCursor is null, create an InputCursor
// with a specific MouseCursor. If InputCursor is not null,
// update the XamlIslandRoot cursor with InputCursor.
//------------------------------------------------------------------------
_Check_return_ HRESULT
CInputServices::SetCursor(MouseCursor eMouseCursor,
_In_opt_ mui::IInputCursor* inputCursor,
_In_ wrl::ComPtr<mui::IInputPointerSource> inputPointerSource)
{
if (eMouseCursor == MouseCursorUnset)
{
// Windows doesn't need to do anything in this case
return S_OK;
}
wrl::ComPtr<mui::IInputSystemCursor> pCursor;
if (!inputCursor)
{
if (!m_inputSystemCursorStatics)
{
if (WinAppSdk::Containment::IsChangeEnabled<WINAPPSDK_CHANGEID_46468883>())
{
m_inputSystemCursorStatics = ActivationFactoryCache::GetActivationFactoryCache()->GetInputSystemCursorStatics();
}
else
{
IFCFAILFAST(wf::GetActivationFactory(
wrl_wrappers::HStringReference(RuntimeClass_Microsoft_UI_Input_InputSystemCursor).Get(),
&m_inputSystemCursorStatics));
}
}
switch (eMouseCursor)
{
case MouseCursorArrow:
m_inputSystemCursorStatics->Create(mui::InputSystemCursorShape_Arrow, &pCursor);
break;
case MouseCursorHand:
m_inputSystemCursorStatics->Create(mui::InputSystemCursorShape_Hand, &pCursor);
break;
case MouseCursorWait:
m_inputSystemCursorStatics->Create(mui::InputSystemCursorShape_Wait, &pCursor);
break;
case MouseCursorIBeam:
m_inputSystemCursorStatics->Create(mui::InputSystemCursorShape_IBeam, &pCursor);
break;
case MouseCursorSizeNS:
m_inputSystemCursorStatics->Create(mui::InputSystemCursorShape_SizeNorthSouth, &pCursor);
break;
case MouseCursorSizeWE:
m_inputSystemCursorStatics->Create(mui::InputSystemCursorShape_SizeWestEast, &pCursor);
break;
case MouseCursorSizeNESW:
m_inputSystemCursorStatics->Create(mui::InputSystemCursorShape_SizeNortheastSouthwest, &pCursor);
break;
case MouseCursorSizeNWSE:
m_inputSystemCursorStatics->Create(mui::InputSystemCursorShape_SizeNorthwestSoutheast, &pCursor);
break;
case MouseCursorPin:
m_inputSystemCursorStatics->Create(mui::InputSystemCursorShape_Pin, &pCursor);
break;
case MouseCursorPerson:
m_inputSystemCursorStatics->Create(mui::InputSystemCursorShape_Person, &pCursor);
break;
case MouseCursorNone:
pCursor = nullptr;
break;
case MouseCursorDefault:
// fall back to IDC_ARROW as the default cursor
m_inputSystemCursorStatics->Create(mui::InputSystemCursorShape_Arrow, &pCursor);
break;
}
}
wrl::ComPtr<mui::IInputCursor> inputCursorToSet;
if (inputCursor)
{
inputCursorToSet = inputCursor;
}
else
{
pCursor.As(&inputCursorToSet);
}
inputPointerSource->put_Cursor(inputCursorToSet.Get());
return S_OK;
}
//------------------------------------------------------------------------
//
// Method: UpdateCursor
//
// Synopsis:
// Set the cursor to whatever would be appropriate given the element
// that the mouse is over, or the element that has captured the mouse
// taking into account whether the element has inherited a Cursor value
// from an ancestor.
//------------------------------------------------------------------------
_Check_return_ HRESULT
CInputServices::UpdateCursor(_In_ CDependencyObject* pVisualInTargetIsland, _In_ XINT32 bUnset)
{
MouseCursor eNewMouseCursor = MouseCursorDefault;
wrl::ComPtr<mui::IInputPointerSource> pInputPointerSource = nullptr;
CDependencyObject *pVisual = NULL;