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>
11 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class.
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"
30 class DesktopDragDropClientAuraX11;
31 class DesktopDispatcherClient;
32 class DesktopWindowTreeHostObserverX11;
33 class X11DesktopWindowMoveClient;
34 class X11ScopedCapture;
35 class X11WindowEventFilter;
37 class VIEWS_EXPORT DesktopWindowTreeHostX11 :
38 public DesktopWindowTreeHost,
39 public aura::WindowTreeHost,
40 public ui::EventSource,
41 public base::MessagePumpDispatcher {
43 DesktopWindowTreeHostX11(
44 internal::NativeWidgetDelegate* native_widget_delegate,
45 DesktopNativeWidgetAura* desktop_native_widget_aura);
46 virtual ~DesktopWindowTreeHostX11();
48 // A way of converting an X11 |xid| host window into a |content_window_|.
49 static aura::Window* GetContentWindowForXID(XID xid);
51 // A way of converting an X11 |xid| host window into this object.
52 static DesktopWindowTreeHostX11* GetHostForXID(XID xid);
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();
59 // Returns the current bounds in terms of the X11 Root Window.
60 gfx::Rect GetX11RootWindowBounds() const;
62 // Called by X11DesktopHandler to notify us that the native windowing system
63 // has changed our activation.
64 void HandleNativeWidgetActivationChanged(bool active);
66 void AddObserver(views::DesktopWindowTreeHostObserverX11* observer);
67 void RemoveObserver(views::DesktopWindowTreeHostObserverX11* observer);
69 // Deallocates the internal list of open windows.
70 static void CleanUpWindowList();
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(
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;
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;
158 // Overridden frm ui::EventSource
159 virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
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);
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);
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();
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);
179 // Checks if the window manager has set a specific state.
180 bool HasWMSpecProperty(const char* property) const;
182 // Sets whether the window's borders are provided by the window manager.
183 void SetUseNativeFrame(bool use_native_frame);
185 // Called when another DRWHL takes capture, or when capture is released
187 void OnCaptureReleased();
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);
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);
199 // Resets the window region for the current widget bounds if necessary.
200 void ResetWindowRegion();
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);
206 // See comment for variable open_windows_.
207 static std::list<XID>& open_windows();
209 // Map the window (shows it) taking into account the given |show_state|.
210 void MapWindow(ui::WindowShowState show_state);
212 // Overridden from Dispatcher:
213 virtual uint32_t Dispatch(const base::NativeEvent& event) OVERRIDE;
215 base::WeakPtrFactory<DesktopWindowTreeHostX11> close_widget_factory_;
218 // The display and the native X window hosting the root window.
222 // The native root window.
223 ::Window x_root_window_;
225 ui::X11AtomCache atom_cache_;
227 // Is the window mapped to the screen?
230 // The bounds of |xwindow_|.
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_;
241 // The bounds of our window before we were maximized.
242 gfx::Rect restored_bounds_;
244 // The window manager state bits.
245 std::set< ::Atom> window_properties_;
247 // Local flag for fullscreen state to avoid a state mismatch between
248 // server and local window_properties_ during app-initiated fullscreen.
251 // True if the window should stay on top of most other windows.
252 bool is_always_on_top_;
254 // True if the window has title-bar / borders provided by the window manager.
255 bool use_native_frame_;
257 // We are owned by the RootWindow, but we have to have a back pointer to it.
258 aura::RootWindow* root_window_;
260 scoped_ptr<DesktopDispatcherClient> dispatcher_client_;
262 DesktopDragDropClientAuraX11* drag_drop_client_;
264 // Current Aura cursor.
265 gfx::NativeCursor current_cursor_;
267 scoped_ptr<X11WindowEventFilter> x11_window_event_filter_;
268 scoped_ptr<X11DesktopWindowMoveClient> x11_window_move_client_;
270 // TODO(beng): Consider providing an interface to DesktopNativeWidgetAura
271 // instead of providing this route back to Widget.
272 internal::NativeWidgetDelegate* native_widget_delegate_;
274 DesktopNativeWidgetAura* desktop_native_widget_aura_;
276 aura::Window* content_window_;
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_;
283 ObserverList<DesktopWindowTreeHostObserverX11> observer_list_;
285 // Copy of custom window shape specified via SetShape(), if any.
286 ::Region custom_window_shape_;
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;
295 // A list of all (top-level) windows that have been created but not yet
297 static std::list<XID>* open_windows_;
299 scoped_ptr<X11ScopedCapture> x11_capture_;
301 base::string16 window_title_;
303 DISALLOW_COPY_AND_ASSIGN(DesktopWindowTreeHostX11);
308 #endif // UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_WINDOW_TREE_HOST_X11_H_