Bug 1608587 [wpt PR 21137] - Update wpt metadata, a=testonly
[gecko.git] / widget / headless / HeadlessWidget.h
blob0e7ab963fc23faa3073bf4ce0254e264326f4a77
1 /* -*- Mode: C++; tab-width: 2; 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/. */
6 #ifndef HEADLESSWIDGET_H
7 #define HEADLESSWIDGET_H
9 #include "mozilla/widget/InProcessCompositorWidget.h"
10 #include "nsBaseWidget.h"
11 #include "CompositorWidget.h"
12 #include "mozilla/dom/WheelEventBinding.h"
14 // The various synthesized event values are hardcoded to avoid pulling
15 // in the platform specific widget code.
16 #if defined(MOZ_WIDGET_GTK)
17 # define MOZ_HEADLESS_MOUSE_MOVE 3 // GDK_MOTION_NOTIFY
18 # define MOZ_HEADLESS_MOUSE_DOWN 4 // GDK_BUTTON_PRESS
19 # define MOZ_HEADLESS_MOUSE_UP 7 // GDK_BUTTON_RELEASE
20 # define MOZ_HEADLESS_SCROLL_MULTIPLIER 3
21 # define MOZ_HEADLESS_SCROLL_DELTA_MODE \
22 mozilla::dom::WheelEvent_Binding::DOM_DELTA_LINE
23 #elif defined(XP_WIN)
24 # define MOZ_HEADLESS_MOUSE_MOVE 1 // MOUSEEVENTF_MOVE
25 # define MOZ_HEADLESS_MOUSE_DOWN 2 // MOUSEEVENTF_LEFTDOWN
26 # define MOZ_HEADLESS_MOUSE_UP 4 // MOUSEEVENTF_LEFTUP
27 # define MOZ_HEADLESS_SCROLL_MULTIPLIER \
28 .025 // default scroll lines (3) / WHEEL_DELTA (120)
29 # define MOZ_HEADLESS_SCROLL_DELTA_MODE \
30 mozilla::dom::WheelEvent_Binding::DOM_DELTA_LINE
31 #elif defined(XP_MACOSX)
32 # define MOZ_HEADLESS_MOUSE_MOVE 5 // NSMouseMoved
33 # define MOZ_HEADLESS_MOUSE_DOWN 1 // NSLeftMouseDown
34 # define MOZ_HEADLESS_MOUSE_UP 2 // NSLeftMouseUp
35 # define MOZ_HEADLESS_SCROLL_MULTIPLIER 1
36 # define MOZ_HEADLESS_SCROLL_DELTA_MODE \
37 mozilla::dom::WheelEvent_Binding::DOM_DELTA_PIXEL
38 #elif defined(ANDROID)
39 # define MOZ_HEADLESS_MOUSE_MOVE 7 // ACTION_HOVER_MOVE
40 # define MOZ_HEADLESS_MOUSE_DOWN 5 // ACTION_POINTER_DOWN
41 # define MOZ_HEADLESS_MOUSE_UP 6 // ACTION_POINTER_UP
42 # define MOZ_HEADLESS_SCROLL_MULTIPLIER 1
43 # define MOZ_HEADLESS_SCROLL_DELTA_MODE \
44 mozilla::dom::WheelEvent_Binding::DOM_DELTA_LINE
45 #else
46 # define MOZ_HEADLESS_MOUSE_MOVE -1
47 # define MOZ_HEADLESS_MOUSE_DOWN -1
48 # define MOZ_HEADLESS_MOUSE_UP -1
49 # define MOZ_HEADLESS_SCROLL_MULTIPLIER -1
50 # define MOZ_HEADLESS_SCROLL_DELTA_MODE -1
51 #endif
53 namespace mozilla {
54 namespace widget {
56 class HeadlessWidget : public nsBaseWidget {
57 public:
58 HeadlessWidget();
60 NS_INLINE_DECL_REFCOUNTING_INHERITED(HeadlessWidget, nsBaseWidget)
62 void* GetNativeData(uint32_t aDataType) override {
63 // Headless widgets have no native data.
64 return nullptr;
67 virtual nsresult Create(nsIWidget* aParent, nsNativeWidget aNativeParent,
68 const LayoutDeviceIntRect& aRect,
69 nsWidgetInitData* aInitData = nullptr) override;
70 using nsBaseWidget::Create; // for Create signature not overridden here
71 virtual already_AddRefed<nsIWidget> CreateChild(
72 const LayoutDeviceIntRect& aRect, nsWidgetInitData* aInitData = nullptr,
73 bool aForceUseIWidgetParent = false) override;
75 virtual nsIWidget* GetTopLevelWidget() override;
77 virtual void GetCompositorWidgetInitData(
78 mozilla::widget::CompositorWidgetInitData* aInitData) override;
80 virtual void Destroy() override;
81 virtual void Show(bool aState) override;
82 virtual bool IsVisible() const override;
83 virtual void Move(double aX, double aY) override;
84 virtual void Resize(double aWidth, double aHeight, bool aRepaint) override;
85 virtual void Resize(double aX, double aY, double aWidth, double aHeight,
86 bool aRepaint) override;
87 virtual void SetSizeMode(nsSizeMode aMode) override;
88 virtual nsresult MakeFullScreen(bool aFullScreen,
89 nsIScreen* aTargetScreen = nullptr) override;
90 virtual void Enable(bool aState) override;
91 virtual bool IsEnabled() const override;
92 virtual void SetFocus(Raise, mozilla::dom::CallerType aCallerType) override;
93 virtual nsresult ConfigureChildren(
94 const nsTArray<Configuration>& aConfigurations) override {
95 MOZ_ASSERT_UNREACHABLE(
96 "Headless widgets do not support configuring children.");
97 return NS_ERROR_FAILURE;
99 virtual void Invalidate(const LayoutDeviceIntRect& aRect) override {
100 // TODO: see if we need to do anything here.
102 virtual nsresult SetTitle(const nsAString& title) override {
103 // Headless widgets have no title, so just ignore it.
104 return NS_OK;
106 virtual nsresult SetNonClientMargins(
107 LayoutDeviceIntMargin& margins) override {
108 // Headless widgets have no chrome margins, so just ignore the call.
109 return NS_OK;
111 virtual LayoutDeviceIntPoint WidgetToScreenOffset() override;
112 virtual void SetInputContext(const InputContext& aContext,
113 const InputContextAction& aAction) override {
114 mInputContext = aContext;
116 virtual InputContext GetInputContext() override { return mInputContext; }
118 virtual LayerManager* GetLayerManager(
119 PLayerTransactionChild* aShadowManager = nullptr,
120 LayersBackend aBackendHint = mozilla::layers::LayersBackend::LAYERS_NONE,
121 LayerManagerPersistence aPersistence = LAYER_MANAGER_CURRENT) override;
123 void SetCompositorWidgetDelegate(CompositorWidgetDelegate* delegate) override;
125 virtual MOZ_MUST_USE nsresult
126 AttachNativeKeyEvent(WidgetKeyboardEvent& aEvent) override;
127 virtual bool GetEditCommands(NativeKeyBindingsType aType,
128 const WidgetKeyboardEvent& aEvent,
129 nsTArray<CommandInt>& aCommands) override;
131 virtual nsresult DispatchEvent(WidgetGUIEvent* aEvent,
132 nsEventStatus& aStatus) override;
134 virtual nsresult SynthesizeNativeMouseEvent(LayoutDeviceIntPoint aPoint,
135 uint32_t aNativeMessage,
136 uint32_t aModifierFlags,
137 nsIObserver* aObserver) override;
138 virtual nsresult SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint,
139 nsIObserver* aObserver) override {
140 return SynthesizeNativeMouseEvent(aPoint, MOZ_HEADLESS_MOUSE_MOVE, 0,
141 aObserver);
144 virtual nsresult SynthesizeNativeMouseScrollEvent(
145 LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
146 double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
147 uint32_t aAdditionalFlags, nsIObserver* aObserver) override;
149 virtual nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
150 TouchPointerState aPointerState,
151 LayoutDeviceIntPoint aPoint,
152 double aPointerPressure,
153 uint32_t aPointerOrientation,
154 nsIObserver* aObserver) override;
156 private:
157 ~HeadlessWidget();
158 bool mEnabled;
159 bool mVisible;
160 bool mDestroyed;
161 nsIWidget* mTopLevel;
162 HeadlessCompositorWidget* mCompositorWidget;
163 // The size mode before entering fullscreen mode.
164 nsSizeMode mLastSizeMode;
165 // The last size mode set while the window was visible.
166 nsSizeMode mEffectiveSizeMode;
167 InputContext mInputContext;
168 mozilla::UniquePtr<mozilla::MultiTouchInput> mSynthesizedTouchInput;
169 // In headless there is no window manager to track window bounds
170 // across size mode changes, so we must track it to emulate.
171 LayoutDeviceIntRect mRestoreBounds;
172 void ApplySizeModeSideEffects();
173 // Similarly, we must track the active window ourselves in order
174 // to dispatch (de)activation events properly.
175 void RaiseWindow();
176 // The top level widgets are tracked for window ordering. They are
177 // stored in order of activation where the last element is always the
178 // currently active widget.
179 static StaticAutoPtr<nsTArray<HeadlessWidget*>> sActiveWindows;
180 // Get the most recently activated widget or null if there are none.
181 static already_AddRefed<HeadlessWidget> GetActiveWindow();
184 } // namespace widget
185 } // namespace mozilla
187 #endif