1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef nsWindowBase_h_
7 #define nsWindowBase_h_
8 
9 #include "mozilla/EventForwards.h"
10 #include "nsBaseWidget.h"
11 #include "nsClassHashtable.h"
12 
13 #include <windows.h>
14 #include "touchinjection_sdk80.h"
15 
16 /*
17  * nsWindowBase - Base class of common methods other classes need to access
18  * in both win32 and winrt window classes.
19  */
20 class nsWindowBase : public nsBaseWidget {
21  public:
22   typedef mozilla::WidgetEventTime WidgetEventTime;
23 
24   /*
25    * Return the HWND or null for this widget.
26    */
GetWindowHandle()27   HWND GetWindowHandle() {
28     return static_cast<HWND>(GetNativeData(NS_NATIVE_WINDOW));
29   }
30 
31   /*
32    * Return the parent window, if it exists.
33    */
34   virtual nsWindowBase* GetParentWindowBase(bool aIncludeOwner) = 0;
35 
36   /*
37    * Return true if this is a top level widget.
38    */
39   virtual bool IsTopLevelWidget() = 0;
40 
41   /*
42    * Init a standard gecko event for this widget.
43    * @param aEvent the event to initialize.
44    * @param aPoint message position in physical coordinates.
45    */
46   virtual void InitEvent(mozilla::WidgetGUIEvent& aEvent,
47                          LayoutDeviceIntPoint* aPoint = nullptr) = 0;
48 
49   /*
50    * Returns WidgetEventTime instance which is initialized with current message
51    * time.
52    */
53   virtual WidgetEventTime CurrentMessageWidgetEventTime() const = 0;
54 
55   /*
56    * Dispatch a gecko event for this widget.
57    * Returns true if it's consumed.  Otherwise, false.
58    */
59   virtual bool DispatchWindowEvent(mozilla::WidgetGUIEvent* aEvent) = 0;
60 
61   /*
62    * Dispatch a gecko keyboard event for this widget. This
63    * is called by KeyboardLayout to dispatch gecko events.
64    * Returns true if it's consumed.  Otherwise, false.
65    */
66   virtual bool DispatchKeyboardEvent(mozilla::WidgetKeyboardEvent* aEvent) = 0;
67 
68   /*
69    * Dispatch a gecko wheel event for this widget. This
70    * is called by ScrollHandler to dispatch gecko events.
71    * Returns true if it's consumed.  Otherwise, false.
72    */
73   virtual bool DispatchWheelEvent(mozilla::WidgetWheelEvent* aEvent) = 0;
74 
75   /*
76    * Dispatch a gecko content command event for this widget. This
77    * is called by ScrollHandler to dispatch gecko events.
78    * Returns true if it's consumed.  Otherwise, false.
79    */
80   virtual bool DispatchContentCommandEvent(
81       mozilla::WidgetContentCommandEvent* aEvent) = 0;
82 
83   /*
84    * Touch input injection apis
85    */
86   virtual nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
87                                               TouchPointerState aPointerState,
88                                               LayoutDeviceIntPoint aPoint,
89                                               double aPointerPressure,
90                                               uint32_t aPointerOrientation,
91                                               nsIObserver* aObserver) override;
92   virtual nsresult ClearNativeTouchSequence(nsIObserver* aObserver) override;
93 
94   virtual nsresult SynthesizeNativePenInput(
95       uint32_t aPointerId, TouchPointerState aPointerState,
96       LayoutDeviceIntPoint aPoint, double aPressure, uint32_t aRotation,
97       int32_t aTiltX, int32_t aTiltY, nsIObserver* aObserver) override;
98 
99   /*
100    * WM_APPCOMMAND common handler.
101    * Sends events via NativeKey::HandleAppCommandMessage().
102    */
103   virtual bool HandleAppCommandMsg(const MSG& aAppCommandMsg,
104                                    LRESULT* aRetValue);
105 
InputContextRef()106   const InputContext& InputContextRef() const { return mInputContext; }
107 
108  protected:
109   virtual int32_t LogToPhys(double aValue) = 0;
110   void ChangedDPI();
111 
112   static bool InitTouchInjection();
113   bool InjectTouchPoint(uint32_t aId, LayoutDeviceIntPoint& aPoint,
114                         POINTER_FLAGS aFlags, uint32_t aPressure = 1024,
115                         uint32_t aOrientation = 90);
116 
117   class PointerInfo {
118    public:
119     enum class PointerType : uint8_t {
120       TOUCH,
121       PEN,
122     };
123 
PointerInfo(int32_t aPointerId,LayoutDeviceIntPoint & aPoint,PointerType aType)124     PointerInfo(int32_t aPointerId, LayoutDeviceIntPoint& aPoint,
125                 PointerType aType)
126         : mPointerId(aPointerId), mPosition(aPoint), mType(aType) {}
127 
128     int32_t mPointerId;
129     LayoutDeviceIntPoint mPosition;
130     PointerType mType;
131   };
132 
133   nsClassHashtable<nsUint32HashKey, PointerInfo> mActivePointers;
134   static bool sTouchInjectInitialized;
135   static InjectTouchInputPtr sInjectTouchFuncPtr;
136 
137   // This is used by SynthesizeNativeTouchPoint to maintain state between
138   // multiple synthesized points, in the case where we can't call InjectTouch
139   // directly.
140   mozilla::UniquePtr<mozilla::MultiTouchInput> mSynthesizedTouchInput;
141 
142  protected:
143   InputContext mInputContext;
144 };
145 
146 #endif  // nsWindowBase_h_
147