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 #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"
29 class DesktopDragDropClientAuraX11;
30 class DesktopDispatcherClient;
31 class DesktopWindowTreeHostObserverX11;
32 class X11DesktopWindowMoveClient;
33 class X11ScopedCapture;
34 class X11WindowEventFilter;
36 class VIEWS_EXPORT DesktopWindowTreeHostX11
37 : public DesktopWindowTreeHost,
38 public aura::WindowTreeHost,
39 public ui::EventSource,
40 public ui::PlatformEventDispatcher {
42 DesktopWindowTreeHostX11(
43 internal::NativeWidgetDelegate* native_widget_delegate,
44 DesktopNativeWidgetAura* desktop_native_widget_aura);
45 virtual ~DesktopWindowTreeHostX11();
47 // A way of converting an X11 |xid| host window into a |content_window_|.
48 static aura::Window* GetContentWindowForXID(XID xid);
50 // A way of converting an X11 |xid| host window into this object.
51 static DesktopWindowTreeHostX11* GetHostForXID(XID xid);
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();
58 // Returns the current bounds in terms of the X11 Root Window.
59 gfx::Rect GetX11RootWindowBounds() const;
61 // Called by X11DesktopHandler to notify us that the native windowing system
62 // has changed our activation.
63 void HandleNativeWidgetActivationChanged(bool active);
65 void AddObserver(views::DesktopWindowTreeHostObserverX11* observer);
66 void RemoveObserver(views::DesktopWindowTreeHostObserverX11* observer);
68 // Deallocates the internal list of open windows.
69 static void CleanUpWindowList();
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(
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;
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;
149 // Overridden frm ui::EventSource
150 virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
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);
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);
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);
165 // Checks if the window manager has set a specific state.
166 bool HasWMSpecProperty(const char* property) const;
168 // Sets whether the window's borders are provided by the window manager.
169 void SetUseNativeFrame(bool use_native_frame);
171 // Called when another DRWHL takes capture, or when capture is released
173 void OnCaptureReleased();
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);
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);
185 // Resets the window region for the current widget bounds if necessary.
186 void ResetWindowRegion();
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);
192 // Returns an 8888 ARGB visual. Can return NULL if there is no matching
193 // visual on this display.
194 Visual* GetARGBVisual();
196 // See comment for variable open_windows_.
197 static std::list<XID>& open_windows();
199 // Map the window (shows it) taking into account the given |show_state|.
200 void MapWindow(ui::WindowShowState show_state);
202 void SetWindowTransparency();
204 // ui::PlatformEventDispatcher:
205 virtual bool CanDispatchEvent(const ui::PlatformEvent& event) OVERRIDE;
206 virtual uint32_t DispatchEvent(const ui::PlatformEvent& event) OVERRIDE;
208 base::WeakPtrFactory<DesktopWindowTreeHostX11> close_widget_factory_;
211 // The display and the native X window hosting the root window.
215 // The native root window.
216 ::Window x_root_window_;
218 ui::X11AtomCache atom_cache_;
220 // Is the window mapped to the screen?
223 // The bounds of |xwindow_|.
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_;
234 // The bounds of our window before we were maximized.
235 gfx::Rect restored_bounds_;
237 // The window manager state bits.
238 std::set< ::Atom> window_properties_;
240 // Local flag for fullscreen state to avoid a state mismatch between
241 // server and local window_properties_ during app-initiated fullscreen.
244 // True if the window should stay on top of most other windows.
245 bool is_always_on_top_;
247 // True if the window has title-bar / borders provided by the window manager.
248 bool use_native_frame_;
250 // Whether we used an ARGB visual for our window.
251 bool use_argb_visual_;
253 scoped_ptr<DesktopDispatcherClient> dispatcher_client_;
255 DesktopDragDropClientAuraX11* drag_drop_client_;
257 // Current Aura cursor.
258 gfx::NativeCursor current_cursor_;
260 scoped_ptr<X11WindowEventFilter> x11_window_event_filter_;
261 scoped_ptr<X11DesktopWindowMoveClient> x11_window_move_client_;
263 // TODO(beng): Consider providing an interface to DesktopNativeWidgetAura
264 // instead of providing this route back to Widget.
265 internal::NativeWidgetDelegate* native_widget_delegate_;
267 DesktopNativeWidgetAura* desktop_native_widget_aura_;
269 aura::Window* content_window_;
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_;
276 ObserverList<DesktopWindowTreeHostObserverX11> observer_list_;
278 // Copy of custom window shape specified via SetShape(), if any.
279 ::Region custom_window_shape_;
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;
288 // A list of all (top-level) windows that have been created but not yet
290 static std::list<XID>* open_windows_;
292 scoped_ptr<X11ScopedCapture> x11_capture_;
294 base::string16 window_title_;
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_;
302 DISALLOW_COPY_AND_ASSIGN(DesktopWindowTreeHostX11);
307 #endif // UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_WINDOW_TREE_HOST_X11_H_