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_DISPLAY_SCREEN_H_
6 #define UI_DISPLAY_SCREEN_H_
7 
8 #include <set>
9 #include <vector>
10 
11 #include "base/macros.h"
12 #include "base/values.h"
13 #include "ui/display/display.h"
14 #include "ui/display/display_export.h"
15 #include "ui/gfx/gpu_extra_info.h"
16 #include "ui/gfx/native_widget_types.h"
17 
18 namespace base {
19 class TimeDelta;
20 }  // namespace base
21 
22 namespace gfx {
23 class Point;
24 class Rect;
25 }  // namespace gfx
26 
27 namespace display {
28 class DisplayObserver;
29 
30 // A utility class for getting various info about screen size, displays,
31 // cursor position, etc.
32 //
33 // Also, can notify DisplayObservers about global workspace changes. The
34 // availability of that functionality depends on a platform.
35 //
36 // Note that this class does not represent an individual display connected to a
37 // computer -- see the Display class for that. A single Screen object exists
38 // regardless of the number of connected displays.
39 class DISPLAY_EXPORT Screen {
40  public:
41   Screen();
42   virtual ~Screen();
43 
44   // Retrieves the single Screen object.
45   static Screen* GetScreen();
46 
47   // Sets the global screen. Returns the previously installed screen, if any.
48   // NOTE: this does not take ownership of |screen|. Tests must be sure to reset
49   // any state they install.
50   static Screen* SetScreenInstance(Screen* instance);
51 
52   // Returns the current absolute position of the mouse pointer.
53   virtual gfx::Point GetCursorScreenPoint() = 0;
54 
55   // Returns true if the cursor is directly over |window|.
56   virtual bool IsWindowUnderCursor(gfx::NativeWindow window) = 0;
57 
58   // Returns the window at the given screen coordinate |point|.
59   virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) = 0;
60 
61   // Finds the topmost visible chrome window at |screen_point|. This should
62   // return nullptr if |screen_point| is in another program's window which
63   // occludes the topmost chrome window. Ignores the windows in |ignore|, which
64   // contain windows such as the tab being dragged right now.
65   virtual gfx::NativeWindow GetLocalProcessWindowAtPoint(
66       const gfx::Point& point,
67       const std::set<gfx::NativeWindow>& ignore) = 0;
68 
69   // Returns the number of displays.
70   // Mirrored displays are excluded; this method is intended to return the
71   // number of distinct, usable displays.
72   virtual int GetNumDisplays() const = 0;
73 
74   // Returns the list of displays that are currently available.
75   virtual const std::vector<Display>& GetAllDisplays() const = 0;
76 
77   // Returns the display nearest the specified window.
78   // If the window is NULL or the window is not rooted to a display this will
79   // return the primary display.
80   virtual Display GetDisplayNearestWindow(gfx::NativeWindow window) const = 0;
81 
82   // Returns the display nearest the specified view. It may still use the window
83   // that contains the view (i.e. if a window is spread over two displays,
84   // the location of the view within that window won't influence the result).
85   virtual Display GetDisplayNearestView(gfx::NativeView view) const;
86 
87   // Returns the display nearest the specified DIP |point|.
88   virtual Display GetDisplayNearestPoint(const gfx::Point& point) const = 0;
89 
90   // Returns the display that most closely intersects the DIP rect |match_rect|.
91   virtual Display GetDisplayMatching(const gfx::Rect& match_rect) const = 0;
92 
93   // Returns the primary display. It is guaranteed that this will return a
94   // display with a valid display ID even if there is no display connected.
95   // A real display will be reported via DisplayObserver when it is connected.
96   virtual Display GetPrimaryDisplay() const = 0;
97 
98   // Returns a suggested display to use when creating a new window. On most
99   // platforms just returns the primary display.
100   Display GetDisplayForNewWindows() const;
101 
102   // Sets the suggested display to use when creating a new window.
103   void SetDisplayForNewWindows(int64_t display_id);
104 
105   // Suspends the platform-specific screensaver, if applicable.
106   virtual void SetScreenSaverSuspended(bool suspend);
107 
108   // Returns whether the screensaver is currently running.
109   virtual bool IsScreenSaverActive() const;
110 
111   // Calculates idle time.
112   virtual base::TimeDelta CalculateIdleTime() const;
113 
114   // Adds/Removes display observers.
115   virtual void AddObserver(DisplayObserver* observer) = 0;
116   virtual void RemoveObserver(DisplayObserver* observer) = 0;
117 
118   // Converts |screen_rect| to DIP coordinates in the context of |window|
119   // clamping to the enclosing rect if the coordinates do not fall on pixel
120   // boundaries. If |window| is null, the primary display is used as the
121   // context.
122   virtual gfx::Rect ScreenToDIPRectInWindow(gfx::NativeWindow window,
123                                             const gfx::Rect& screen_rect) const;
124 
125   // Converts |dip_rect| to screen coordinates in the context of |window|
126   // clamping to the enclosing rect if the coordinates do not fall on pixel
127   // boundaries. If |window| is null, the primary display is used as the
128   // context.
129   virtual gfx::Rect DIPToScreenRectInWindow(gfx::NativeWindow window,
130                                             const gfx::Rect& dip_rect) const;
131 
132   // Returns true if the display with |display_id| is found and returns that
133   // display in |display|. Otherwise returns false and |display| remains
134   // untouched.
135   bool GetDisplayWithDisplayId(int64_t display_id, Display* display) const;
136 
137   virtual void SetPanelRotationForTesting(int64_t display_id,
138                                           Display::Rotation rotation);
139 
140   // Depending on a platform, a client can listen to global workspace changes
141   // by implementing and setting self as a DisplayObserver. It is also possible
142   // to get current workspace through the GetCurrentWorkspace method.
143   virtual std::string GetCurrentWorkspace();
144 
145   // Returns human readable description of the window manager, desktop, and
146   // other system properties related to the compositing.
147   virtual base::Value GetGpuExtraInfoAsListValue(
148       const gfx::GpuExtraInfo& gpu_extra_info);
149 
150  private:
151   friend class ScopedDisplayForNewWindows;
152 
153   // Used to temporarily override the value from SetDisplayForNewWindows() by
154   // creating an instance of ScopedDisplayForNewWindows. Call with
155   // |kInvalidDisplayId| to unset.
156   void SetScopedDisplayForNewWindows(int64_t display_id);
157 
158   static gfx::NativeWindow GetWindowForView(gfx::NativeView view);
159 
160   int64_t display_id_for_new_windows_;
161   int64_t scoped_display_id_for_new_windows_ = display::kInvalidDisplayId;
162 
163   DISALLOW_COPY_AND_ASSIGN(Screen);
164 };
165 
166 Screen* CreateNativeScreen();
167 
168 }  // namespace display
169 
170 #endif  // UI_DISPLAY_SCREEN_H_
171