desktop_window_tree_host_x11.h revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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.
4
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_
7
8#include <X11/extensions/shape.h>
9#include <X11/Xlib.h>
10
11#include "base/basictypes.h"
12#include "base/memory/weak_ptr.h"
13#include "base/observer_list.h"
14#include "ui/aura/window_tree_host.h"
15#include "ui/base/cursor/cursor_loader_x11.h"
16#include "ui/events/event_source.h"
17#include "ui/events/platform/platform_event_dispatcher.h"
18#include "ui/gfx/insets.h"
19#include "ui/gfx/rect.h"
20#include "ui/gfx/x/x11_atom_cache.h"
21#include "ui/views/views_export.h"
22#include "ui/views/widget/desktop_aura/desktop_window_tree_host.h"
23
24namespace gfx {
25class ImageSkia;
26class ImageSkiaRep;
27}
28
29namespace ui {
30class EventHandler;
31}
32
33namespace views {
34class DesktopDragDropClientAuraX11;
35class DesktopDispatcherClient;
36class DesktopWindowTreeHostObserverX11;
37class X11DesktopWindowMoveClient;
38class X11ScopedCapture;
39class X11WindowEventFilter;
40
41class VIEWS_EXPORT DesktopWindowTreeHostX11
42    : public DesktopWindowTreeHost,
43      public aura::WindowTreeHost,
44      public ui::EventSource,
45      public ui::PlatformEventDispatcher {
46 public:
47  DesktopWindowTreeHostX11(
48      internal::NativeWidgetDelegate* native_widget_delegate,
49      DesktopNativeWidgetAura* desktop_native_widget_aura);
50  virtual ~DesktopWindowTreeHostX11();
51
52  // A way of converting an X11 |xid| host window into a |content_window_|.
53  static aura::Window* GetContentWindowForXID(XID xid);
54
55  // A way of converting an X11 |xid| host window into this object.
56  static DesktopWindowTreeHostX11* GetHostForXID(XID xid);
57
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();
62
63  // Returns the current bounds in terms of the X11 Root Window.
64  gfx::Rect GetX11RootWindowBounds() const;
65
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;
69
70  // Returns the window shape if the window is not rectangular. Returns NULL
71  // otherwise.
72  ::Region GetWindowShape() const;
73
74  // Called by X11DesktopHandler to notify us that the native windowing system
75  // has changed our activation.
76  void HandleNativeWidgetActivationChanged(bool active);
77
78  void AddObserver(views::DesktopWindowTreeHostObserverX11* observer);
79  void RemoveObserver(views::DesktopWindowTreeHostObserverX11* observer);
80
81  // Swaps the current handler for events in the non client view with |handler|.
82  void SwapNonClientEventHandler(scoped_ptr<ui::EventHandler> handler);
83
84  // Deallocates the internal list of open windows.
85  static void CleanUpWindowList();
86
87 protected:
88  // Overridden from DesktopWindowTreeHost:
89  virtual void Init(aura::Window* content_window,
90                    const Widget::InitParams& params) OVERRIDE;
91  virtual void OnNativeWidgetCreated(const Widget::InitParams& params) OVERRIDE;
92  virtual scoped_ptr<corewm::Tooltip> CreateTooltip() OVERRIDE;
93  virtual scoped_ptr<aura::client::DragDropClient>
94      CreateDragDropClient(DesktopNativeCursorManager* cursor_manager) OVERRIDE;
95  virtual void Close() OVERRIDE;
96  virtual void CloseNow() OVERRIDE;
97  virtual aura::WindowTreeHost* AsWindowTreeHost() OVERRIDE;
98  virtual void ShowWindowWithState(ui::WindowShowState show_state) OVERRIDE;
99  virtual void ShowMaximizedWithBounds(
100      const gfx::Rect& restored_bounds) OVERRIDE;
101  virtual bool IsVisible() const OVERRIDE;
102  virtual void SetSize(const gfx::Size& size) OVERRIDE;
103  virtual void StackAtTop() OVERRIDE;
104  virtual void CenterWindow(const gfx::Size& size) OVERRIDE;
105  virtual void GetWindowPlacement(
106      gfx::Rect* bounds,
107      ui::WindowShowState* show_state) const OVERRIDE;
108  virtual gfx::Rect GetWindowBoundsInScreen() const OVERRIDE;
109  virtual gfx::Rect GetClientAreaBoundsInScreen() const OVERRIDE;
110  virtual gfx::Rect GetRestoredBounds() const OVERRIDE;
111  virtual gfx::Rect GetWorkAreaBoundsInScreen() const OVERRIDE;
112  virtual void SetShape(gfx::NativeRegion native_region) OVERRIDE;
113  virtual void Activate() OVERRIDE;
114  virtual void Deactivate() OVERRIDE;
115  virtual bool IsActive() const OVERRIDE;
116  virtual void Maximize() OVERRIDE;
117  virtual void Minimize() OVERRIDE;
118  virtual void Restore() OVERRIDE;
119  virtual bool IsMaximized() const OVERRIDE;
120  virtual bool IsMinimized() const OVERRIDE;
121  virtual bool HasCapture() const OVERRIDE;
122  virtual void SetAlwaysOnTop(bool always_on_top) OVERRIDE;
123  virtual bool IsAlwaysOnTop() const OVERRIDE;
124  virtual void SetVisibleOnAllWorkspaces(bool always_visible) OVERRIDE;
125  virtual bool SetWindowTitle(const base::string16& title) OVERRIDE;
126  virtual void ClearNativeFocus() OVERRIDE;
127  virtual Widget::MoveLoopResult RunMoveLoop(
128      const gfx::Vector2d& drag_offset,
129      Widget::MoveLoopSource source,
130      Widget::MoveLoopEscapeBehavior escape_behavior) OVERRIDE;
131  virtual void EndMoveLoop() OVERRIDE;
132  virtual void SetVisibilityChangedAnimationsEnabled(bool value) OVERRIDE;
133  virtual bool ShouldUseNativeFrame() const OVERRIDE;
134  virtual bool ShouldWindowContentsBeTransparent() const OVERRIDE;
135  virtual void FrameTypeChanged() OVERRIDE;
136  virtual void SetFullscreen(bool fullscreen) OVERRIDE;
137  virtual bool IsFullscreen() const OVERRIDE;
138  virtual void SetOpacity(unsigned char opacity) OVERRIDE;
139  virtual void SetWindowIcons(const gfx::ImageSkia& window_icon,
140                              const gfx::ImageSkia& app_icon) OVERRIDE;
141  virtual void InitModalType(ui::ModalType modal_type) OVERRIDE;
142  virtual void FlashFrame(bool flash_frame) OVERRIDE;
143  virtual void OnRootViewLayout() const OVERRIDE;
144  virtual void OnNativeWidgetFocus() OVERRIDE;
145  virtual void OnNativeWidgetBlur() OVERRIDE;
146  virtual bool IsAnimatingClosed() const OVERRIDE;
147
148  // Overridden from aura::WindowTreeHost:
149  virtual ui::EventSource* GetEventSource() OVERRIDE;
150  virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE;
151  virtual void Show() OVERRIDE;
152  virtual void Hide() OVERRIDE;
153  virtual gfx::Rect GetBounds() const OVERRIDE;
154  virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
155  virtual gfx::Point GetLocationOnNativeScreen() const OVERRIDE;
156  virtual void SetCapture() OVERRIDE;
157  virtual void ReleaseCapture() OVERRIDE;
158  virtual void PostNativeEvent(const base::NativeEvent& native_event) OVERRIDE;
159  virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE;
160  virtual void SetCursorNative(gfx::NativeCursor cursor) OVERRIDE;
161  virtual void MoveCursorToNative(const gfx::Point& location) OVERRIDE;
162  virtual void OnCursorVisibilityChangedNative(bool show) OVERRIDE;
163
164  // Overridden frm ui::EventSource
165  virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
166
167 private:
168  // Initializes our X11 surface to draw on. This method performs all
169  // initialization related to talking to the X11 server.
170  void InitX11Window(const Widget::InitParams& params);
171
172  // Creates an aura::WindowEventDispatcher to contain the |content_window|,
173  // along with all aura client objects that direct behavior.
174  aura::WindowEventDispatcher* InitDispatcher(const Widget::InitParams& params);
175
176  // Called when |xwindow_|'s _NET_WM_STATE property is updated.
177  void OnWMStateUpdated();
178
179  // Called when |xwindow_|'s _NET_FRAME_EXTENTS property is updated.
180  void OnFrameExtentsUpdated();
181
182  // Updates |xwindow_|'s _NET_WM_USER_TIME if |xwindow_| is active.
183  void UpdateWMUserTime(const ui::PlatformEvent& event);
184
185  // Sends a message to the x11 window manager, enabling or disabling the
186  // states |state1| and |state2|.
187  void SetWMSpecState(bool enabled, ::Atom state1, ::Atom state2);
188
189  // Checks if the window manager has set a specific state.
190  bool HasWMSpecProperty(const char* property) const;
191
192  // Sets whether the window's borders are provided by the window manager.
193  void SetUseNativeFrame(bool use_native_frame);
194
195  // Called when another DRWHL takes capture, or when capture is released
196  // entirely.
197  void OnCaptureReleased();
198
199  // Dispatches a mouse event, taking mouse capture into account. If a
200  // different host has capture, we translate the event to its coordinate space
201  // and dispatch it to that host instead.
202  void DispatchMouseEvent(ui::MouseEvent* event);
203
204  // Dispatches a touch event, taking capture into account. If a different host
205  // has capture, then touch-press events are translated to its coordinate space
206  // and dispatched to that host instead.
207  void DispatchTouchEvent(ui::TouchEvent* event);
208
209  // Resets the window region for the current widget bounds if necessary.
210  void ResetWindowRegion();
211
212  // Serializes an image to the format used by _NET_WM_ICON.
213  void SerializeImageRepresentation(const gfx::ImageSkiaRep& rep,
214                                    std::vector<unsigned long>* data);
215
216  // Returns an 8888 ARGB visual. Can return NULL if there is no matching
217  // visual on this display.
218  Visual* GetARGBVisual();
219
220  // See comment for variable open_windows_.
221  static std::list<XID>& open_windows();
222
223  // Map the window (shows it) taking into account the given |show_state|.
224  void MapWindow(ui::WindowShowState show_state);
225
226  void SetWindowTransparency();
227
228  // Relayout the widget's client and non-client views.
229  void Relayout();
230
231  // ui::PlatformEventDispatcher:
232  virtual bool CanDispatchEvent(const ui::PlatformEvent& event) OVERRIDE;
233  virtual uint32_t DispatchEvent(const ui::PlatformEvent& event) OVERRIDE;
234
235  base::WeakPtrFactory<DesktopWindowTreeHostX11> close_widget_factory_;
236
237  // X11 things
238  // The display and the native X window hosting the root window.
239  XDisplay* xdisplay_;
240  ::Window xwindow_;
241
242  // The native root window.
243  ::Window x_root_window_;
244
245  ui::X11AtomCache atom_cache_;
246
247  // Is the window mapped to the screen?
248  bool window_mapped_;
249
250  // The bounds of |xwindow_|.
251  gfx::Rect bounds_;
252
253  // Whenever the bounds are set, we keep the previous set of bounds around so
254  // we can have a better chance of getting the real |restored_bounds_|. Window
255  // managers tend to send a Configure message with the maximized bounds, and
256  // then set the window maximized property. (We don't rely on this for when we
257  // request that the window be maximized, only when we detect that some other
258  // process has requested that we become the maximized window.)
259  gfx::Rect previous_bounds_;
260
261  // The bounds of our window before we were maximized.
262  gfx::Rect restored_bounds_;
263
264  // The window manager state bits.
265  std::set< ::Atom> window_properties_;
266
267  // Whether |xwindow_| was requested to be fullscreen via SetFullscreen().
268  bool is_fullscreen_;
269
270  // True if the window should stay on top of most other windows.
271  bool is_always_on_top_;
272
273  // True if the window has title-bar / borders provided by the window manager.
274  bool use_native_frame_;
275
276  // Whether we used an ARGB visual for our window.
277  bool use_argb_visual_;
278
279  scoped_ptr<DesktopDispatcherClient> dispatcher_client_;
280
281  DesktopDragDropClientAuraX11* drag_drop_client_;
282
283  // Current Aura cursor.
284  gfx::NativeCursor current_cursor_;
285
286  scoped_ptr<ui::EventHandler> x11_non_client_event_filter_;
287  scoped_ptr<X11DesktopWindowMoveClient> x11_window_move_client_;
288
289  // TODO(beng): Consider providing an interface to DesktopNativeWidgetAura
290  //             instead of providing this route back to Widget.
291  internal::NativeWidgetDelegate* native_widget_delegate_;
292
293  DesktopNativeWidgetAura* desktop_native_widget_aura_;
294
295  aura::Window* content_window_;
296
297  // We can optionally have a parent which can order us to close, or own
298  // children who we're responsible for closing when we CloseNow().
299  DesktopWindowTreeHostX11* window_parent_;
300  std::set<DesktopWindowTreeHostX11*> window_children_;
301
302  ObserverList<DesktopWindowTreeHostObserverX11> observer_list_;
303
304  // The window shape if the window is non-rectangular.
305  ::Region window_shape_;
306
307  // Whether |window_shape_| was set via SetShape().
308  bool custom_window_shape_;
309
310  // The size of the window manager provided borders (if any).
311  gfx::Insets native_window_frame_borders_;
312
313  // The current root window host that has capture. While X11 has something
314  // like Windows SetCapture()/ReleaseCapture(), it is entirely implicit and
315  // there are no notifications when this changes. We need to track this so we
316  // can notify widgets when they have lost capture, which controls a bunch of
317  // things in views like hiding menus.
318  static DesktopWindowTreeHostX11* g_current_capture;
319
320  // A list of all (top-level) windows that have been created but not yet
321  // destroyed.
322  static std::list<XID>* open_windows_;
323
324  scoped_ptr<X11ScopedCapture> x11_capture_;
325
326  base::string16 window_title_;
327
328  // Whether we currently are flashing our frame. This feature is implemented
329  // by setting the urgency hint with the window manager, which can draw
330  // attention to the window or completely ignore the hint. We stop flashing
331  // the frame when |xwindow_| gains focus or handles a mouse button event.
332  bool urgency_hint_set_;
333
334  DISALLOW_COPY_AND_ASSIGN(DesktopWindowTreeHostX11);
335};
336
337}  // namespace views
338
339#endif  // UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_WINDOW_TREE_HOST_X11_H_
340