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