Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / views / widget / desktop_aura / desktop_window_tree_host_x11.h
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 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class.
12 #undef RootWindow
13
14 #include "base/basictypes.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "ui/aura/window_tree_host.h"
18 #include "ui/base/cursor/cursor_loader_x11.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
24 namespace gfx {
25 class ImageSkia;
26 class ImageSkiaRep;
27 }
28
29 namespace views {
30 class DesktopDragDropClientAuraX11;
31 class DesktopDispatcherClient;
32 class DesktopWindowTreeHostObserverX11;
33 class X11DesktopWindowMoveClient;
34 class X11ScopedCapture;
35 class X11WindowEventFilter;
36
37 class VIEWS_EXPORT DesktopWindowTreeHostX11 :
38     public DesktopWindowTreeHost,
39     public aura::WindowTreeHost,
40     public ui::EventSource,
41     public base::MessagePumpDispatcher {
42  public:
43   DesktopWindowTreeHostX11(
44       internal::NativeWidgetDelegate* native_widget_delegate,
45       DesktopNativeWidgetAura* desktop_native_widget_aura);
46   virtual ~DesktopWindowTreeHostX11();
47
48   // A way of converting an X11 |xid| host window into a |content_window_|.
49   static aura::Window* GetContentWindowForXID(XID xid);
50
51   // A way of converting an X11 |xid| host window into this object.
52   static DesktopWindowTreeHostX11* GetHostForXID(XID xid);
53
54   // Get all open top-level windows. This includes windows that may not be
55   // visible. This list is sorted in their stacking order, i.e. the first window
56   // is the topmost window.
57   static std::vector<aura::Window*> GetAllOpenWindows();
58
59   // Returns the current bounds in terms of the X11 Root Window.
60   gfx::Rect GetX11RootWindowBounds() const;
61
62   // Called by X11DesktopHandler to notify us that the native windowing system
63   // has changed our activation.
64   void HandleNativeWidgetActivationChanged(bool active);
65
66   void AddObserver(views::DesktopWindowTreeHostObserverX11* observer);
67   void RemoveObserver(views::DesktopWindowTreeHostObserverX11* observer);
68
69   // Deallocates the internal list of open windows.
70   static void CleanUpWindowList();
71
72  protected:
73   // Overridden from DesktopWindowTreeHost:
74   virtual void Init(aura::Window* content_window,
75                     const Widget::InitParams& params,
76                     aura::RootWindow::CreateParams* rw_create_params) OVERRIDE;
77   virtual void OnRootWindowCreated(aura::RootWindow* root,
78                                    const Widget::InitParams& params) OVERRIDE;
79   virtual scoped_ptr<corewm::Tooltip> CreateTooltip() OVERRIDE;
80   virtual scoped_ptr<aura::client::DragDropClient>
81       CreateDragDropClient(DesktopNativeCursorManager* cursor_manager) OVERRIDE;
82   virtual void Close() OVERRIDE;
83   virtual void CloseNow() OVERRIDE;
84   virtual aura::WindowTreeHost* AsWindowTreeHost() OVERRIDE;
85   virtual void ShowWindowWithState(ui::WindowShowState show_state) OVERRIDE;
86   virtual void ShowMaximizedWithBounds(
87       const gfx::Rect& restored_bounds) OVERRIDE;
88   virtual bool IsVisible() const OVERRIDE;
89   virtual void SetSize(const gfx::Size& size) OVERRIDE;
90   virtual void StackAtTop() OVERRIDE;
91   virtual void CenterWindow(const gfx::Size& size) OVERRIDE;
92   virtual void GetWindowPlacement(
93       gfx::Rect* bounds,
94       ui::WindowShowState* show_state) const OVERRIDE;
95   virtual gfx::Rect GetWindowBoundsInScreen() const OVERRIDE;
96   virtual gfx::Rect GetClientAreaBoundsInScreen() const OVERRIDE;
97   virtual gfx::Rect GetRestoredBounds() const OVERRIDE;
98   virtual gfx::Rect GetWorkAreaBoundsInScreen() const OVERRIDE;
99   virtual void SetShape(gfx::NativeRegion native_region) OVERRIDE;
100   virtual void Activate() OVERRIDE;
101   virtual void Deactivate() OVERRIDE;
102   virtual bool IsActive() const OVERRIDE;
103   virtual void Maximize() OVERRIDE;
104   virtual void Minimize() OVERRIDE;
105   virtual void Restore() OVERRIDE;
106   virtual bool IsMaximized() const OVERRIDE;
107   virtual bool IsMinimized() const OVERRIDE;
108   virtual bool HasCapture() const OVERRIDE;
109   virtual void SetAlwaysOnTop(bool always_on_top) OVERRIDE;
110   virtual bool IsAlwaysOnTop() const OVERRIDE;
111   virtual bool SetWindowTitle(const base::string16& title) OVERRIDE;
112   virtual void ClearNativeFocus() OVERRIDE;
113   virtual Widget::MoveLoopResult RunMoveLoop(
114       const gfx::Vector2d& drag_offset,
115       Widget::MoveLoopSource source,
116       Widget::MoveLoopEscapeBehavior escape_behavior) OVERRIDE;
117   virtual void EndMoveLoop() OVERRIDE;
118   virtual void SetVisibilityChangedAnimationsEnabled(bool value) OVERRIDE;
119   virtual bool ShouldUseNativeFrame() const OVERRIDE;
120   virtual bool ShouldWindowContentsBeTransparent() const OVERRIDE;
121   virtual void FrameTypeChanged() OVERRIDE;
122   virtual NonClientFrameView* CreateNonClientFrameView() OVERRIDE;
123   virtual void SetFullscreen(bool fullscreen) OVERRIDE;
124   virtual bool IsFullscreen() const OVERRIDE;
125   virtual void SetOpacity(unsigned char opacity) OVERRIDE;
126   virtual void SetWindowIcons(const gfx::ImageSkia& window_icon,
127                               const gfx::ImageSkia& app_icon) OVERRIDE;
128   virtual void InitModalType(ui::ModalType modal_type) OVERRIDE;
129   virtual void FlashFrame(bool flash_frame) OVERRIDE;
130   virtual void OnRootViewLayout() const OVERRIDE;
131   virtual void OnNativeWidgetFocus() OVERRIDE;
132   virtual void OnNativeWidgetBlur() OVERRIDE;
133   virtual bool IsAnimatingClosed() const OVERRIDE;
134
135   // Overridden from aura::WindowTreeHost:
136   virtual aura::RootWindow* GetRootWindow() OVERRIDE;
137   virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE;
138   virtual void Show() OVERRIDE;
139   virtual void Hide() OVERRIDE;
140   virtual void ToggleFullScreen() OVERRIDE;
141   virtual gfx::Rect GetBounds() const OVERRIDE;
142   virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
143   virtual gfx::Insets GetInsets() const OVERRIDE;
144   virtual void SetInsets(const gfx::Insets& insets) OVERRIDE;
145   virtual gfx::Point GetLocationOnNativeScreen() const OVERRIDE;
146   virtual void SetCapture() OVERRIDE;
147   virtual void ReleaseCapture() OVERRIDE;
148   virtual bool QueryMouseLocation(gfx::Point* location_return) OVERRIDE;
149   virtual bool ConfineCursorToRootWindow() OVERRIDE;
150   virtual void UnConfineCursor() OVERRIDE;
151   virtual void PostNativeEvent(const base::NativeEvent& native_event) OVERRIDE;
152   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE;
153   virtual void PrepareForShutdown() OVERRIDE;
154   virtual void SetCursorNative(gfx::NativeCursor cursor) OVERRIDE;
155   virtual void MoveCursorToNative(const gfx::Point& location) OVERRIDE;
156   virtual void OnCursorVisibilityChangedNative(bool show) OVERRIDE;
157
158   // Overridden frm ui::EventSource
159   virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
160
161 private:
162   // Initializes our X11 surface to draw on. This method performs all
163   // initialization related to talking to the X11 server.
164   void InitX11Window(const Widget::InitParams& params);
165
166   // Creates an aura::RootWindow to contain the |content_window|, along with
167   // all aura client objects that direct behavior.
168   aura::RootWindow* InitRootWindow(const Widget::InitParams& params);
169
170   // Returns true if there's an X window manager present... in most cases.  Some
171   // window managers (notably, ion3) don't implement enough of ICCCM for us to
172   // detect that they're there.
173   bool IsWindowManagerPresent();
174
175   // Sends a message to the x11 window manager, enabling or disabling the
176   // states |state1| and |state2|.
177   void SetWMSpecState(bool enabled, ::Atom state1, ::Atom state2);
178
179   // Checks if the window manager has set a specific state.
180   bool HasWMSpecProperty(const char* property) const;
181
182   // Sets whether the window's borders are provided by the window manager.
183   void SetUseNativeFrame(bool use_native_frame);
184
185   // Called when another DRWHL takes capture, or when capture is released
186   // entirely.
187   void OnCaptureReleased();
188
189   // Dispatches a mouse event, taking mouse capture into account. If a
190   // different host has capture, we translate the event to its coordinate space
191   // and dispatch it to that host instead.
192   void DispatchMouseEvent(ui::MouseEvent* event);
193
194   // Dispatches a touch event, taking capture into account. If a different host
195   // has capture, then touch-press events are translated to its coordinate space
196   // and dispatched to that host instead.
197   void DispatchTouchEvent(ui::TouchEvent* event);
198
199   // Resets the window region for the current widget bounds if necessary.
200   void ResetWindowRegion();
201
202   // Serializes an image to the format used by _NET_WM_ICON.
203   void SerializeImageRepresentation(const gfx::ImageSkiaRep& rep,
204                                     std::vector<unsigned long>* data);
205
206   // See comment for variable open_windows_.
207   static std::list<XID>& open_windows();
208
209   // Map the window (shows it) taking into account the given |show_state|.
210   void MapWindow(ui::WindowShowState show_state);
211
212   // Overridden from Dispatcher:
213   virtual uint32_t Dispatch(const base::NativeEvent& event) OVERRIDE;
214
215   base::WeakPtrFactory<DesktopWindowTreeHostX11> close_widget_factory_;
216
217   // X11 things
218   // The display and the native X window hosting the root window.
219   XDisplay* xdisplay_;
220   ::Window xwindow_;
221
222   // The native root window.
223   ::Window x_root_window_;
224
225   ui::X11AtomCache atom_cache_;
226
227   // Is the window mapped to the screen?
228   bool window_mapped_;
229
230   // The bounds of |xwindow_|.
231   gfx::Rect bounds_;
232
233   // Whenever the bounds are set, we keep the previous set of bounds around so
234   // we can have a better chance of getting the real |restored_bounds_|. Window
235   // managers tend to send a Configure message with the maximized bounds, and
236   // then set the window maximized property. (We don't rely on this for when we
237   // request that the window be maximized, only when we detect that some other
238   // process has requested that we become the maximized window.)
239   gfx::Rect previous_bounds_;
240
241   // The bounds of our window before we were maximized.
242   gfx::Rect restored_bounds_;
243
244   // The window manager state bits.
245   std::set< ::Atom> window_properties_;
246
247   // Local flag for fullscreen state to avoid a state mismatch between
248   // server and local window_properties_ during app-initiated fullscreen.
249   bool is_fullscreen_;
250
251   // True if the window should stay on top of most other windows.
252   bool is_always_on_top_;
253
254   // True if the window has title-bar / borders provided by the window manager.
255   bool use_native_frame_;
256
257   // We are owned by the RootWindow, but we have to have a back pointer to it.
258   aura::RootWindow* root_window_;
259
260   scoped_ptr<DesktopDispatcherClient> dispatcher_client_;
261
262   DesktopDragDropClientAuraX11* drag_drop_client_;
263
264   // Current Aura cursor.
265   gfx::NativeCursor current_cursor_;
266
267   scoped_ptr<X11WindowEventFilter> x11_window_event_filter_;
268   scoped_ptr<X11DesktopWindowMoveClient> x11_window_move_client_;
269
270   // TODO(beng): Consider providing an interface to DesktopNativeWidgetAura
271   //             instead of providing this route back to Widget.
272   internal::NativeWidgetDelegate* native_widget_delegate_;
273
274   DesktopNativeWidgetAura* desktop_native_widget_aura_;
275
276   aura::Window* content_window_;
277
278   // We can optionally have a parent which can order us to close, or own
279   // children who we're responsible for closing when we CloseNow().
280   DesktopWindowTreeHostX11* window_parent_;
281   std::set<DesktopWindowTreeHostX11*> window_children_;
282
283   ObserverList<DesktopWindowTreeHostObserverX11> observer_list_;
284
285   // Copy of custom window shape specified via SetShape(), if any.
286   ::Region custom_window_shape_;
287
288   // The current root window host that has capture. While X11 has something
289   // like Windows SetCapture()/ReleaseCapture(), it is entirely implicit and
290   // there are no notifications when this changes. We need to track this so we
291   // can notify widgets when they have lost capture, which controls a bunch of
292   // things in views like hiding menus.
293   static DesktopWindowTreeHostX11* g_current_capture;
294
295   // A list of all (top-level) windows that have been created but not yet
296   // destroyed.
297   static std::list<XID>* open_windows_;
298
299   scoped_ptr<X11ScopedCapture> x11_capture_;
300
301   base::string16 window_title_;
302
303   DISALLOW_COPY_AND_ASSIGN(DesktopWindowTreeHostX11);
304 };
305
306 }  // namespace views
307
308 #endif  // UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_WINDOW_TREE_HOST_X11_H_