Roll src/third_party/skia d32087a:1052f51
[chromium-blink-merge.git] / ui / views / widget / desktop_aura / desktop_window_tree_host_x11.h
blob73e9e3e6fa7d4b15eb2c0516afe973292397b108
1 // Copyright (c) 2012 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.
5 #ifndef UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_WINDOW_TREE_HOST_X11_H_
6 #define UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_WINDOW_TREE_HOST_X11_H_
8 #include <X11/extensions/shape.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
12 #include "base/basictypes.h"
13 #include "base/cancelable_callback.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "ui/aura/window_tree_host.h"
17 #include "ui/base/cursor/cursor_loader_x11.h"
18 #include "ui/events/platform/platform_event_dispatcher.h"
19 #include "ui/gfx/geometry/insets.h"
20 #include "ui/gfx/geometry/rect.h"
21 #include "ui/gfx/geometry/size.h"
22 #include "ui/gfx/x/x11_atom_cache.h"
23 #include "ui/views/views_export.h"
24 #include "ui/views/widget/desktop_aura/desktop_window_tree_host.h"
26 namespace gfx {
27 class ImageSkia;
28 class ImageSkiaRep;
31 namespace ui {
32 class EventHandler;
35 namespace views {
36 class DesktopDragDropClientAuraX11;
37 class DesktopDispatcherClient;
38 class DesktopWindowTreeHostObserverX11;
39 class X11DesktopWindowMoveClient;
40 class X11WindowEventFilter;
42 class VIEWS_EXPORT DesktopWindowTreeHostX11
43 : public DesktopWindowTreeHost,
44 public aura::WindowTreeHost,
45 public ui::PlatformEventDispatcher {
46 public:
47 DesktopWindowTreeHostX11(
48 internal::NativeWidgetDelegate* native_widget_delegate,
49 DesktopNativeWidgetAura* desktop_native_widget_aura);
50 ~DesktopWindowTreeHostX11() override;
52 // A way of converting an X11 |xid| host window into a |content_window_|.
53 static aura::Window* GetContentWindowForXID(XID xid);
55 // A way of converting an X11 |xid| host window into this object.
56 static DesktopWindowTreeHostX11* GetHostForXID(XID xid);
58 // Get all open top-level windows. This includes windows that may not be
59 // visible. This list is sorted in their stacking order, i.e. the first window
60 // is the topmost window.
61 static std::vector<aura::Window*> GetAllOpenWindows();
63 // Returns the current bounds in terms of the X11 Root Window.
64 gfx::Rect GetX11RootWindowBounds() const;
66 // Returns the current bounds in terms of the X11 Root Window including the
67 // borders provided by the window manager (if any).
68 gfx::Rect GetX11RootWindowOuterBounds() const;
70 // Returns the window shape if the window is not rectangular. Returns NULL
71 // otherwise.
72 ::Region GetWindowShape() const;
74 // Called by X11DesktopHandler to notify us that the native windowing system
75 // has changed our activation.
76 void HandleNativeWidgetActivationChanged(bool active);
78 void AddObserver(views::DesktopWindowTreeHostObserverX11* observer);
79 void RemoveObserver(views::DesktopWindowTreeHostObserverX11* observer);
81 // Swaps the current handler for events in the non client view with |handler|.
82 void SwapNonClientEventHandler(scoped_ptr<ui::EventHandler> handler);
84 // Runs the |func| callback for each content-window, and deallocates the
85 // internal list of open windows.
86 static void CleanUpWindowList(void (*func)(aura::Window* window));
88 protected:
89 // Overridden from DesktopWindowTreeHost:
90 void Init(aura::Window* content_window,
91 const Widget::InitParams& params) override;
92 void OnNativeWidgetCreated(const Widget::InitParams& params) override;
93 scoped_ptr<corewm::Tooltip> CreateTooltip() override;
94 scoped_ptr<aura::client::DragDropClient> CreateDragDropClient(
95 DesktopNativeCursorManager* cursor_manager) override;
96 void Close() override;
97 void CloseNow() override;
98 aura::WindowTreeHost* AsWindowTreeHost() override;
99 void ShowWindowWithState(ui::WindowShowState show_state) override;
100 void ShowMaximizedWithBounds(const gfx::Rect& restored_bounds) override;
101 bool IsVisible() const override;
102 void SetSize(const gfx::Size& requested_size) override;
103 void StackAbove(aura::Window* window) override;
104 void StackAtTop() override;
105 void CenterWindow(const gfx::Size& size) override;
106 void GetWindowPlacement(gfx::Rect* bounds,
107 ui::WindowShowState* show_state) const override;
108 gfx::Rect GetWindowBoundsInScreen() const override;
109 gfx::Rect GetClientAreaBoundsInScreen() const override;
110 gfx::Rect GetRestoredBounds() const override;
111 gfx::Rect GetWorkAreaBoundsInScreen() const override;
112 void SetShape(SkRegion* native_region) override;
113 void Activate() override;
114 void Deactivate() override;
115 bool IsActive() const override;
116 void Maximize() override;
117 void Minimize() override;
118 void Restore() override;
119 bool IsMaximized() const override;
120 bool IsMinimized() const override;
121 bool HasCapture() const override;
122 void SetAlwaysOnTop(bool always_on_top) override;
123 bool IsAlwaysOnTop() const override;
124 void SetVisibleOnAllWorkspaces(bool always_visible) override;
125 bool SetWindowTitle(const base::string16& title) override;
126 void ClearNativeFocus() override;
127 Widget::MoveLoopResult RunMoveLoop(
128 const gfx::Vector2d& drag_offset,
129 Widget::MoveLoopSource source,
130 Widget::MoveLoopEscapeBehavior escape_behavior) override;
131 void EndMoveLoop() override;
132 void SetVisibilityChangedAnimationsEnabled(bool value) override;
133 bool ShouldUseNativeFrame() const override;
134 bool ShouldWindowContentsBeTransparent() const override;
135 void FrameTypeChanged() override;
136 void SetFullscreen(bool fullscreen) override;
137 bool IsFullscreen() const override;
138 void SetOpacity(unsigned char opacity) override;
139 void SetWindowIcons(const gfx::ImageSkia& window_icon,
140 const gfx::ImageSkia& app_icon) override;
141 void InitModalType(ui::ModalType modal_type) override;
142 void FlashFrame(bool flash_frame) override;
143 void OnRootViewLayout() override;
144 void OnNativeWidgetFocus() override;
145 void OnNativeWidgetBlur() override;
146 bool IsAnimatingClosed() const override;
147 bool IsTranslucentWindowOpacitySupported() const override;
148 void SizeConstraintsChanged() override;
150 // Overridden from aura::WindowTreeHost:
151 gfx::Transform GetRootTransform() const override;
152 ui::EventSource* GetEventSource() override;
153 gfx::AcceleratedWidget GetAcceleratedWidget() override;
154 void ShowImpl() override;
155 void HideImpl() override;
156 gfx::Rect GetBounds() const override;
157 void SetBounds(const gfx::Rect& requested_bounds_in_pixels) override;
158 gfx::Point GetLocationOnNativeScreen() const override;
159 void SetCapture() override;
160 void ReleaseCapture() override;
161 void SetCursorNative(gfx::NativeCursor cursor) override;
162 void MoveCursorToNative(const gfx::Point& location) override;
163 void OnCursorVisibilityChangedNative(bool show) override;
165 private:
166 friend class DesktopWindowTreeHostX11HighDPITest;
167 // Initializes our X11 surface to draw on. This method performs all
168 // initialization related to talking to the X11 server.
169 void InitX11Window(const Widget::InitParams& params);
171 // Creates an aura::WindowEventDispatcher to contain the |content_window|,
172 // along with all aura client objects that direct behavior.
173 aura::WindowEventDispatcher* InitDispatcher(const Widget::InitParams& params);
175 // Adjusts |requested_size| to avoid the WM "feature" where setting the
176 // window size to the monitor size causes the WM to set the EWMH for
177 // fullscreen.
178 gfx::Size AdjustSize(const gfx::Size& requested_size);
180 // Called when |xwindow_|'s _NET_WM_STATE property is updated.
181 void OnWMStateUpdated();
183 // Called when |xwindow_|'s _NET_FRAME_EXTENTS property is updated.
184 void OnFrameExtentsUpdated();
186 // Updates |xwindow_|'s minimum and maximum size.
187 void UpdateMinAndMaxSize();
189 // Updates |xwindow_|'s _NET_WM_USER_TIME if |xwindow_| is active.
190 void UpdateWMUserTime(const ui::PlatformEvent& event);
192 // Sends a message to the x11 window manager, enabling or disabling the
193 // states |state1| and |state2|.
194 void SetWMSpecState(bool enabled, ::Atom state1, ::Atom state2);
196 // Checks if the window manager has set a specific state.
197 bool HasWMSpecProperty(const char* property) const;
199 // Sets whether the window's borders are provided by the window manager.
200 void SetUseNativeFrame(bool use_native_frame);
202 // Dispatches a mouse event, taking mouse capture into account. If a
203 // different host has capture, we translate the event to its coordinate space
204 // and dispatch it to that host instead.
205 void DispatchMouseEvent(ui::MouseEvent* event);
207 // Dispatches a touch event, taking capture into account. If a different host
208 // has capture, then touch-press events are translated to its coordinate space
209 // and dispatched to that host instead.
210 void DispatchTouchEvent(ui::TouchEvent* event);
212 // Dispatches a key event.
213 void DispatchKeyEvent(ui::KeyEvent* event);
215 // Updates the location of |located_event| to be in |host|'s coordinate system
216 // so that it can be dispatched to |host|.
217 void ConvertEventToDifferentHost(ui::LocatedEvent* located_event,
218 DesktopWindowTreeHostX11* host);
220 // Resets the window region for the current widget bounds if necessary.
221 void ResetWindowRegion();
223 // Serializes an image to the format used by _NET_WM_ICON.
224 void SerializeImageRepresentation(const gfx::ImageSkiaRep& rep,
225 std::vector<unsigned long>* data);
227 // Returns an 8888 ARGB visual. Can return NULL if there is no matching
228 // visual on this display.
229 Visual* GetARGBVisual();
231 // See comment for variable open_windows_.
232 static std::list<XID>& open_windows();
234 // Map the window (shows it) taking into account the given |show_state|.
235 void MapWindow(ui::WindowShowState show_state);
237 void SetWindowTransparency();
239 // Relayout the widget's client and non-client views.
240 void Relayout();
242 // ui::PlatformEventDispatcher:
243 bool CanDispatchEvent(const ui::PlatformEvent& event) override;
244 uint32_t DispatchEvent(const ui::PlatformEvent& event) override;
246 void DelayedResize(const gfx::Size& size_in_pixels);
248 gfx::Rect GetWorkAreaBoundsInPixels() const;
249 gfx::Rect ToDIPRect(const gfx::Rect& rect_in_pixels) const;
250 gfx::Rect ToPixelRect(const gfx::Rect& rect_in_dip) const;
252 // X11 things
253 // The display and the native X window hosting the root window.
254 XDisplay* xdisplay_;
255 ::Window xwindow_;
257 // The native root window.
258 ::Window x_root_window_;
260 ui::X11AtomCache atom_cache_;
262 // Is the window mapped to the screen?
263 bool window_mapped_;
265 // The bounds of |xwindow_|.
266 gfx::Rect bounds_in_pixels_;
268 // Whenever the bounds are set, we keep the previous set of bounds around so
269 // we can have a better chance of getting the real
270 // |restored_bounds_in_pixels_|. Window managers tend to send a Configure
271 // message with the maximized bounds, and then set the window maximized
272 // property. (We don't rely on this for when we request that the window be
273 // maximized, only when we detect that some other process has requested that
274 // we become the maximized window.)
275 gfx::Rect previous_bounds_in_pixels_;
277 // The bounds of our window before we were maximized.
278 gfx::Rect restored_bounds_in_pixels_;
280 // |xwindow_|'s minimum size.
281 gfx::Size min_size_in_pixels_;
283 // |xwindow_|'s maximum size.
284 gfx::Size max_size_in_pixels_;
286 // The window manager state bits.
287 std::set< ::Atom> window_properties_;
289 // Whether |xwindow_| was requested to be fullscreen via SetFullscreen().
290 bool is_fullscreen_;
292 // True if the window should stay on top of most other windows.
293 bool is_always_on_top_;
295 // True if the window has title-bar / borders provided by the window manager.
296 bool use_native_frame_;
298 // True if a Maximize() call should be done after mapping the window.
299 bool should_maximize_after_map_;
301 // Whether we used an ARGB visual for our window.
302 bool use_argb_visual_;
304 DesktopDragDropClientAuraX11* drag_drop_client_;
306 scoped_ptr<ui::EventHandler> x11_non_client_event_filter_;
307 scoped_ptr<X11DesktopWindowMoveClient> x11_window_move_client_;
309 // TODO(beng): Consider providing an interface to DesktopNativeWidgetAura
310 // instead of providing this route back to Widget.
311 internal::NativeWidgetDelegate* native_widget_delegate_;
313 DesktopNativeWidgetAura* desktop_native_widget_aura_;
315 aura::Window* content_window_;
317 // We can optionally have a parent which can order us to close, or own
318 // children who we're responsible for closing when we CloseNow().
319 DesktopWindowTreeHostX11* window_parent_;
320 std::set<DesktopWindowTreeHostX11*> window_children_;
322 base::ObserverList<DesktopWindowTreeHostObserverX11> observer_list_;
324 // The window shape if the window is non-rectangular.
325 gfx::XScopedPtr<_XRegion, gfx::XObjectDeleter<_XRegion, int, XDestroyRegion>>
326 window_shape_;
328 // Whether |window_shape_| was set via SetShape().
329 bool custom_window_shape_;
331 // The size of the window manager provided borders (if any).
332 gfx::Insets native_window_frame_borders_in_pixels_;
334 // The current DesktopWindowTreeHostX11 which has capture. Set synchronously
335 // when capture is requested via SetCapture().
336 static DesktopWindowTreeHostX11* g_current_capture;
338 // A list of all (top-level) windows that have been created but not yet
339 // destroyed.
340 static std::list<XID>* open_windows_;
342 base::string16 window_title_;
344 // Whether we currently are flashing our frame. This feature is implemented
345 // by setting the urgency hint with the window manager, which can draw
346 // attention to the window or completely ignore the hint. We stop flashing
347 // the frame when |xwindow_| gains focus or handles a mouse button event.
348 bool urgency_hint_set_;
350 base::CancelableCallback<void()> delayed_resize_task_;
352 base::WeakPtrFactory<DesktopWindowTreeHostX11> close_widget_factory_;
354 DISALLOW_COPY_AND_ASSIGN(DesktopWindowTreeHostX11);
357 } // namespace views
359 #endif // UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_WINDOW_TREE_HOST_X11_H_