1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 #ifndef DEVICE_VR_WINDOWS_MIXED_REALITY_WRAPPERS_WMR_INPUT_MANAGER_H_
5 #define DEVICE_VR_WINDOWS_MIXED_REALITY_WRAPPERS_WMR_INPUT_MANAGER_H_
6 
7 #include <windows.perception.h>
8 #include <windows.ui.input.spatial.h>
9 #include <wrl.h>
10 
11 #include <memory>
12 #include <vector>
13 
14 #include "base/callback_list.h"
15 #include "base/macros.h"
16 
17 namespace device {
18 class WMRInputSourceState;
19 class WMRInputSourceEventArgs {
20  public:
21   virtual ~WMRInputSourceEventArgs() = default;
22 
23   virtual ABI::Windows::UI::Input::Spatial::SpatialInteractionPressKind
24   PressKind() const = 0;
25   virtual std::unique_ptr<WMRInputSourceState> State() const = 0;
26 };
27 
28 class WMRInputSourceEventArgsImpl : public WMRInputSourceEventArgs {
29  public:
30   explicit WMRInputSourceEventArgsImpl(
31       Microsoft::WRL::ComPtr<
32           ABI::Windows::UI::Input::Spatial::ISpatialInteractionSourceEventArgs>
33           args);
34   ~WMRInputSourceEventArgsImpl() override;
35 
36   ABI::Windows::UI::Input::Spatial::SpatialInteractionPressKind PressKind()
37       const override;
38   std::unique_ptr<WMRInputSourceState> State() const override;
39 
40  private:
41   Microsoft::WRL::ComPtr<
42       ABI::Windows::UI::Input::Spatial::ISpatialInteractionSourceEventArgs>
43       args_;
44   Microsoft::WRL::ComPtr<
45       ABI::Windows::UI::Input::Spatial::ISpatialInteractionSourceEventArgs2>
46       args2_;
47 };
48 
49 class WMRInputManager {
50  public:
51   using InputEventCallbackList =
52       base::CallbackList<void(const WMRInputSourceEventArgs&)>;
53   using InputEventCallback =
54       base::RepeatingCallback<void(const WMRInputSourceEventArgs&)>;
55 
56   virtual ~WMRInputManager() = default;
57 
58   virtual std::vector<std::unique_ptr<WMRInputSourceState>>
59   GetDetectedSourcesAtTimestamp(
60       Microsoft::WRL::ComPtr<ABI::Windows::Perception::IPerceptionTimestamp>
61           timestamp) = 0;
62 
63   virtual std::unique_ptr<InputEventCallbackList::Subscription>
64   AddPressedCallback(const InputEventCallback& cb) = 0;
65 
66   virtual std::unique_ptr<InputEventCallbackList::Subscription>
67   AddReleasedCallback(const InputEventCallback& cb) = 0;
68 };
69 
70 class WMRInputManagerImpl : public WMRInputManager {
71  public:
72   using InputEventCallbackList =
73       base::CallbackList<void(const WMRInputSourceEventArgs&)>;
74   using InputEventCallback =
75       base::RepeatingCallback<void(const WMRInputSourceEventArgs&)>;
76 
77   explicit WMRInputManagerImpl(
78       Microsoft::WRL::ComPtr<
79           ABI::Windows::UI::Input::Spatial::ISpatialInteractionManager>
80           manager);
81   ~WMRInputManagerImpl() override;
82 
83   std::vector<std::unique_ptr<WMRInputSourceState>>
84   GetDetectedSourcesAtTimestamp(
85       Microsoft::WRL::ComPtr<ABI::Windows::Perception::IPerceptionTimestamp>
86           timestamp) override;
87 
88   std::unique_ptr<InputEventCallbackList::Subscription> AddPressedCallback(
89       const InputEventCallback& cb) override;
90 
91   std::unique_ptr<InputEventCallbackList::Subscription> AddReleasedCallback(
92       const InputEventCallback& cb) override;
93 
94  private:
95   void SubscribeEvents();
96   void UnsubscribeEvents();
97 
98   HRESULT OnSourcePressed(
99       ABI::Windows::UI::Input::Spatial::ISpatialInteractionManager* sender,
100       ABI::Windows::UI::Input::Spatial::ISpatialInteractionSourceEventArgs*
101           args);
102   HRESULT OnSourceReleased(
103       ABI::Windows::UI::Input::Spatial::ISpatialInteractionManager* sender,
104       ABI::Windows::UI::Input::Spatial::ISpatialInteractionSourceEventArgs*
105           args);
106 
107   Microsoft::WRL::ComPtr<
108       ABI::Windows::UI::Input::Spatial::ISpatialInteractionManager>
109       manager_;
110 
111   EventRegistrationToken pressed_token_;
112   EventRegistrationToken released_token_;
113   InputEventCallbackList pressed_callback_list_;
114   InputEventCallbackList released_callback_list_;
115 
116   DISALLOW_COPY_AND_ASSIGN(WMRInputManagerImpl);
117 };
118 }  // namespace device
119 
120 #endif  // DEVICE_VR_WINDOWS_MIXED_REALITY_WRAPPERS_WMR_INPUT_MANAGER_H_
121