display_controller.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 ASH_DISPLAY_DISPLAY_CONTROLLER_H_
6#define ASH_DISPLAY_DISPLAY_CONTROLLER_H_
7
8#include <map>
9#include <vector>
10
11#include "ash/ash_export.h"
12#include "base/basictypes.h"
13#include "base/compiler_specific.h"
14#include "base/gtest_prod_util.h"
15#include "base/observer_list.h"
16#include "ui/aura/display_observer.h"
17#include "ui/aura/display_manager.h"
18#include "ui/gfx/display.h"
19
20namespace aura {
21class Display;
22class RootWindow;
23}
24
25namespace base {
26class Value;
27template <typename T> class JSONValueConverter;
28}
29
30namespace ash {
31namespace internal {
32class RootWindowController;
33}
34
35struct ASH_EXPORT DisplayLayout {
36  // Layout options where the secondary display should be positioned.
37  enum Position {
38    TOP,
39    RIGHT,
40    BOTTOM,
41    LEFT
42  };
43
44  DisplayLayout();
45  DisplayLayout(Position position, int offset);
46
47  // Returns an inverted display layout.
48  DisplayLayout Invert() const WARN_UNUSED_RESULT;
49
50  // Converter functions to/from base::Value.
51  static bool ConvertFromValue(const base::Value& value, DisplayLayout* layout);
52  static bool ConvertToValue(const DisplayLayout& layout, base::Value* value);
53
54  // This method is used by base::JSONValueConverter, you don't need to call
55  // this directly. Instead consider using converter functions above.
56  static void RegisterJSONConverter(
57      base::JSONValueConverter<DisplayLayout>* converter);
58
59  Position position;
60
61  // The offset of the position of the secondary display.  The offset is
62  // based on the top/left edge of the primary display.
63  int offset;
64
65  // Returns string representation of the layout for debugging/testing.
66  std::string ToString() const;
67};
68
69// DisplayController owns and maintains RootWindows for each attached
70// display, keeping them in sync with display configuration changes.
71class ASH_EXPORT DisplayController : public aura::DisplayObserver {
72 public:
73  class ASH_EXPORT Observer {
74   public:
75    // Invoked when the display configuration change is requested,
76    // but before the change is applied to aura/ash.
77    virtual void OnDisplayConfigurationChanging() = 0;
78
79   protected:
80    virtual ~Observer() {}
81  };
82
83  DisplayController();
84  virtual ~DisplayController();
85
86  // Gets primary display. This information is stored in global
87  // object as this can be accessed after Shell is closed.
88  static const gfx::Display& GetPrimaryDisplay();
89
90  // Initializes primary display.
91  void InitPrimaryDisplay();
92
93  // Initialize secondary displays.
94  void InitSecondaryDisplays();
95
96  // Add/Remove observers.
97  void AddObserver(Observer* observer);
98  void RemoveObserver(Observer* observer);
99
100  // Returns the root window for primary display.
101  aura::RootWindow* GetPrimaryRootWindow();
102
103  // Returns the root window for |display_id|.
104  aura::RootWindow* GetRootWindowForDisplayId(int64 id);
105
106  // Sets the ID of the primary display.  If the display is not connected, it
107  // will switch the primary display when connected.
108  void SetPrimaryDisplayId(int64 id);
109
110  // Sets primary display. This re-assigns the current root
111  // window to given |display|.
112  void SetPrimaryDisplay(const gfx::Display& display);
113
114  // Returns the secondary display.
115  gfx::Display* GetSecondaryDisplay();
116
117  // Closes all child windows in the all root windows.
118  void CloseChildWindows();
119
120  // Returns all root windows. In non extended desktop mode, this
121  // returns the primary root window only.
122  std::vector<aura::RootWindow*> GetAllRootWindows();
123
124  // Returns all oot window controllers. In non extended desktop
125  // mode, this return a RootWindowController for the primary root window only.
126  std::vector<internal::RootWindowController*> GetAllRootWindowControllers();
127
128  // Returns the current overscan insets for the specified |display_id|. See
129  // multi_display_manager.h for the details.
130  gfx::Insets GetOverscanInsets(int64 display_id) const;
131
132  const DisplayLayout& default_display_layout() const {
133    return default_display_layout_;
134  }
135  void SetDefaultDisplayLayout(const DisplayLayout& layout);
136
137  // Sets/gets the display layout for the specified display or display
138  // name.  Getter returns the default value in case it doesn't have
139  // its own layout yet.
140  void SetLayoutForDisplayName(const std::string& name,
141                               const DisplayLayout& layout);
142  const DisplayLayout& GetLayoutForDisplay(const gfx::Display& display) const;
143
144  // Returns the display layout used for current secondary display.
145  const DisplayLayout& GetCurrentDisplayLayout() const;
146
147  // aura::DisplayObserver overrides:
148  virtual void OnDisplayBoundsChanged(
149      const gfx::Display& display) OVERRIDE;
150  virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE;
151  virtual void OnDisplayRemoved(const gfx::Display& display) OVERRIDE;
152
153 private:
154  // Creates a root window for |display| and stores it in the |root_windows_|
155  // map.
156  aura::RootWindow* AddRootWindowForDisplay(const gfx::Display& display);
157
158  void UpdateDisplayBoundsForLayout();
159
160  void NotifyDisplayConfigurationChanging();
161
162  // The mapping from display ID to its root window.
163  std::map<int64, aura::RootWindow*> root_windows_;
164
165  // The default display layout.
166  DisplayLayout default_display_layout_;
167
168  // Per-device display layout.
169  std::map<std::string, DisplayLayout> secondary_layouts_;
170
171  // The ID of the display which should be primary when connected.
172  // kInvalidDisplayID if no such preference is specified.
173  int64 desired_primary_display_id_;
174
175  ObserverList<Observer> observers_;
176
177  DISALLOW_COPY_AND_ASSIGN(DisplayController);
178};
179
180}  // namespace ash
181
182#endif  // ASH_DISPLAY_DISPLAY_CONTROLLER_H_
183