Upstream version 7.36.149.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 #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/rect.h"
19 #include "ui/gfx/x/x11_atom_cache.h"
20 #include "ui/views/views_export.h"
21 #include "ui/views/widget/desktop_aura/desktop_window_tree_host.h"
22
23 namespace gfx {
24 class ImageSkia;
25 class ImageSkiaRep;
26 }
27
28 namespace views {
29 class DesktopDragDropClientAuraX11;
30 class DesktopDispatcherClient;
31 class DesktopWindowTreeHostObserverX11;
32 class X11DesktopWindowMoveClient;
33 class X11ScopedCapture;
34 class X11WindowEventFilter;
35
36 class VIEWS_EXPORT DesktopWindowTreeHostX11
37     : public DesktopWindowTreeHost,
38       public aura::WindowTreeHost,
39       public ui::EventSource,
40       public ui::PlatformEventDispatcher {
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) OVERRIDE;
75   virtual void OnNativeWidgetCreated(const Widget::InitParams& params) OVERRIDE;
76   virtual scoped_ptr<corewm::Tooltip> CreateTooltip() OVERRIDE;
77   virtual scoped_ptr<aura::client::DragDropClient>
78       CreateDragDropClient(DesktopNativeCursorManager* cursor_manager) OVERRIDE;
79   virtual void Close() OVERRIDE;
80   virtual void CloseNow() OVERRIDE;
81   virtual aura::WindowTreeHost* AsWindowTreeHost() OVERRIDE;
82   virtual void ShowWindowWithState(ui::WindowShowState show_state) OVERRIDE;
83   virtual void ShowMaximizedWithBounds(
84       const gfx::Rect& restored_bounds) OVERRIDE;
85   virtual bool IsVisible() const OVERRIDE;
86   virtual void SetSize(const gfx::Size& size) OVERRIDE;
87   virtual void StackAtTop() OVERRIDE;
88   virtual void CenterWindow(const gfx::Size& size) OVERRIDE;
89   virtual void GetWindowPlacement(
90       gfx::Rect* bounds,
91       ui::WindowShowState* show_state) const OVERRIDE;
92   virtual gfx::Rect GetWindowBoundsInScreen() const OVERRIDE;
93   virtual gfx::Rect GetClientAreaBoundsInScreen() const OVERRIDE;
94   virtual gfx::Rect GetRestoredBounds() const OVERRIDE;
95   virtual gfx::Rect GetWorkAreaBoundsInScreen() const OVERRIDE;
96   virtual void SetShape(gfx::NativeRegion native_region) OVERRIDE;
97   virtual void Activate() OVERRIDE;
98   virtual void Deactivate() OVERRIDE;
99   virtual bool IsActive() const OVERRIDE;
100   virtual void Maximize() OVERRIDE;
101   virtual void Minimize() OVERRIDE;
102   virtual void Restore() OVERRIDE;
103   virtual bool IsMaximized() const OVERRIDE;
104   virtual bool IsMinimized() const OVERRIDE;
105   virtual bool HasCapture() const OVERRIDE;
106   virtual void SetAlwaysOnTop(bool always_on_top) OVERRIDE;
107   virtual bool IsAlwaysOnTop() const OVERRIDE;
108   virtual void SetVisibleOnAllWorkspaces(bool always_visible) OVERRIDE;
109   virtual bool SetWindowTitle(const base::string16& title) OVERRIDE;
110   virtual void ClearNativeFocus() OVERRIDE;
111   virtual Widget::MoveLoopResult RunMoveLoop(
112       const gfx::Vector2d& drag_offset,
113       Widget::MoveLoopSource source,
114       Widget::MoveLoopEscapeBehavior escape_behavior) OVERRIDE;
115   virtual void EndMoveLoop() OVERRIDE;
116   virtual void SetVisibilityChangedAnimationsEnabled(bool value) OVERRIDE;
117   virtual bool ShouldUseNativeFrame() const OVERRIDE;
118   virtual bool ShouldWindowContentsBeTransparent() const 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 ui::EventSource* GetEventSource() OVERRIDE;
135   virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE;
136   virtual void Show() OVERRIDE;
137   virtual void Hide() OVERRIDE;
138   virtual gfx::Rect GetBounds() const OVERRIDE;
139   virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
140   virtual gfx::Point GetLocationOnNativeScreen() const OVERRIDE;
141   virtual void SetCapture() OVERRIDE;
142   virtual void ReleaseCapture() OVERRIDE;
143   virtual void PostNativeEvent(const base::NativeEvent& native_event) OVERRIDE;
144   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE;
145   virtual void SetCursorNative(gfx::NativeCursor cursor) OVERRIDE;
146   virtual void MoveCursorToNative(const gfx::Point& location) OVERRIDE;
147   virtual void OnCursorVisibilityChangedNative(bool show) OVERRIDE;
148
149   // Overridden frm ui::EventSource
150   virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
151
152 private:
153   // Initializes our X11 surface to draw on. This method performs all
154   // initialization related to talking to the X11 server.
155   void InitX11Window(const Widget::InitParams& params);
156
157   // Creates an aura::WindowEventDispatcher to contain the |content_window|,
158   // along with all aura client objects that direct behavior.
159   aura::WindowEventDispatcher* InitDispatcher(const Widget::InitParams& params);
160
161   // Sends a message to the x11 window manager, enabling or disabling the
162   // states |state1| and |state2|.
163   void SetWMSpecState(bool enabled, ::Atom state1, ::Atom state2);
164
165   // Checks if the window manager has set a specific state.
166   bool HasWMSpecProperty(const char* property) const;
167
168   // Sets whether the window's borders are provided by the window manager.
169   void SetUseNativeFrame(bool use_native_frame);
170
171   // Called when another DRWHL takes capture, or when capture is released
172   // entirely.
173   void OnCaptureReleased();
174
175   // Dispatches a mouse event, taking mouse capture into account. If a
176   // different host has capture, we translate the event to its coordinate space
177   // and dispatch it to that host instead.
178   void DispatchMouseEvent(ui::MouseEvent* event);
179
180   // Dispatches a touch event, taking capture into account. If a different host
181   // has capture, then touch-press events are translated to its coordinate space
182   // and dispatched to that host instead.
183   void DispatchTouchEvent(ui::TouchEvent* event);
184
185   // Resets the window region for the current widget bounds if necessary.
186   void ResetWindowRegion();
187
188   // Serializes an image to the format used by _NET_WM_ICON.
189   void SerializeImageRepresentation(const gfx::ImageSkiaRep& rep,
190                                     std::vector<unsigned long>* data);
191
192   // Returns an 8888 ARGB visual. Can return NULL if there is no matching
193   // visual on this display.
194   Visual* GetARGBVisual();
195
196   // See comment for variable open_windows_.
197   static std::list<XID>& open_windows();
198
199   // Map the window (shows it) taking into account the given |show_state|.
200   void MapWindow(ui::WindowShowState show_state);
201
202   void SetWindowTransparency();
203
204   // ui::PlatformEventDispatcher:
205   virtual bool CanDispatchEvent(const ui::PlatformEvent& event) OVERRIDE;
206   virtual uint32_t DispatchEvent(const ui::PlatformEvent& event) OVERRIDE;
207
208   base::WeakPtrFactory<DesktopWindowTreeHostX11> close_widget_factory_;
209
210   // X11 things
211   // The display and the native X window hosting the root window.
212   XDisplay* xdisplay_;
213   ::Window xwindow_;
214
215   // The native root window.
216   ::Window x_root_window_;
217
218   ui::X11AtomCache atom_cache_;
219
220   // Is the window mapped to the screen?
221   bool window_mapped_;
222
223   // The bounds of |xwindow_|.
224   gfx::Rect bounds_;
225
226   // Whenever the bounds are set, we keep the previous set of bounds around so
227   // we can have a better chance of getting the real |restored_bounds_|. Window
228   // managers tend to send a Configure message with the maximized bounds, and
229   // then set the window maximized property. (We don't rely on this for when we
230   // request that the window be maximized, only when we detect that some other
231   // process has requested that we become the maximized window.)
232   gfx::Rect previous_bounds_;
233
234   // The bounds of our window before we were maximized.
235   gfx::Rect restored_bounds_;
236
237   // The window manager state bits.
238   std::set< ::Atom> window_properties_;
239
240   // Local flag for fullscreen state to avoid a state mismatch between
241   // server and local window_properties_ during app-initiated fullscreen.
242   bool is_fullscreen_;
243
244   // True if the window should stay on top of most other windows.
245   bool is_always_on_top_;
246
247   // True if the window has title-bar / borders provided by the window manager.
248   bool use_native_frame_;
249
250   // Whether we used an ARGB visual for our window.
251   bool use_argb_visual_;
252
253   scoped_ptr<DesktopDispatcherClient> dispatcher_client_;
254
255   DesktopDragDropClientAuraX11* drag_drop_client_;
256
257   // Current Aura cursor.
258   gfx::NativeCursor current_cursor_;
259
260   scoped_ptr<X11WindowEventFilter> x11_window_event_filter_;
261   scoped_ptr<X11DesktopWindowMoveClient> x11_window_move_client_;
262
263   // TODO(beng): Consider providing an interface to DesktopNativeWidgetAura
264   //             instead of providing this route back to Widget.
265   internal::NativeWidgetDelegate* native_widget_delegate_;
266
267   DesktopNativeWidgetAura* desktop_native_widget_aura_;
268
269   aura::Window* content_window_;
270
271   // We can optionally have a parent which can order us to close, or own
272   // children who we're responsible for closing when we CloseNow().
273   DesktopWindowTreeHostX11* window_parent_;
274   std::set<DesktopWindowTreeHostX11*> window_children_;
275
276   ObserverList<DesktopWindowTreeHostObserverX11> observer_list_;
277
278   // Copy of custom window shape specified via SetShape(), if any.
279   ::Region custom_window_shape_;
280
281   // The current root window host that has capture. While X11 has something
282   // like Windows SetCapture()/ReleaseCapture(), it is entirely implicit and
283   // there are no notifications when this changes. We need to track this so we
284   // can notify widgets when they have lost capture, which controls a bunch of
285   // things in views like hiding menus.
286   static DesktopWindowTreeHostX11* g_current_capture;
287
288   // A list of all (top-level) windows that have been created but not yet
289   // destroyed.
290   static std::list<XID>* open_windows_;
291
292   scoped_ptr<X11ScopedCapture> x11_capture_;
293
294   base::string16 window_title_;
295
296   // Whether we currently are flashing our frame. This feature is implemented
297   // by setting the urgency hint with the window manager, which can draw
298   // attention to the window or completely ignore the hint. We stop flashing
299   // the frame when |xwindow_| gains focus or handles a mouse button event.
300   bool urgency_hint_set_;
301
302   DISALLOW_COPY_AND_ASSIGN(DesktopWindowTreeHostX11);
303 };
304
305 }  // namespace views
306
307 #endif  // UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_WINDOW_TREE_HOST_X11_H_