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