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 #include "ui/gfx/screen.h"
7 #import <ApplicationServices/ApplicationServices.h>
8 #import <Cocoa/Cocoa.h>
12 #include "base/logging.h"
13 #include "base/mac/sdk_forward_declarations.h"
14 #include "ui/gfx/display.h"
18 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) {
19 // Primary monitor is defined as the monitor with the menubar,
20 // which is always at index 0.
21 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0];
22 float primary_screen_height = [primary_screen frame].size.height;
23 gfx::Rect rect(NSRectToCGRect(ns_rect));
24 rect.set_y(primary_screen_height - rect.y() - rect.height());
28 NSScreen* GetMatchingScreen(const gfx::Rect& match_rect) {
29 // Default to the monitor with the current keyboard focus, in case
30 // |match_rect| is not on any screen at all.
31 NSScreen* max_screen = [NSScreen mainScreen];
34 for (NSScreen* screen in [NSScreen screens]) {
35 gfx::Rect monitor_area = ConvertCoordinateSystem([screen frame]);
36 gfx::Rect intersection = gfx::IntersectRects(monitor_area, match_rect);
37 int area = intersection.width() * intersection.height();
38 if (area > max_area) {
47 gfx::Display GetDisplayForScreen(NSScreen* screen) {
48 NSRect frame = [screen frame];
49 // TODO(oshima): Implement ID and Observer.
50 gfx::Display display(0, gfx::Rect(NSRectToCGRect(frame)));
52 NSRect visible_frame = [screen visibleFrame];
53 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
55 // Convert work area's coordinate systems.
56 if ([screen isEqual:primary]) {
57 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame));
58 work_area.set_y(frame.size.height - visible_frame.origin.y -
59 visible_frame.size.height);
60 display.set_work_area(work_area);
62 display.set_bounds(ConvertCoordinateSystem(frame));
63 display.set_work_area(ConvertCoordinateSystem(visible_frame));
66 if ([screen respondsToSelector:@selector(backingScaleFactor)])
67 scale = [screen backingScaleFactor];
69 scale = [screen userSpaceScaleFactor];
70 display.set_device_scale_factor(scale);
74 class ScreenMac : public gfx::Screen {
78 virtual bool IsDIPEnabled() OVERRIDE {
82 virtual gfx::Point GetCursorScreenPoint() OVERRIDE {
83 NSPoint mouseLocation = [NSEvent mouseLocation];
84 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen.
85 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
86 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y;
87 return gfx::Point(mouseLocation.x, mouseLocation.y);
90 virtual gfx::NativeWindow GetWindowUnderCursor() OVERRIDE {
92 return gfx::NativeWindow();
95 virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point)
98 return gfx::NativeWindow();
101 virtual int GetNumDisplays() const OVERRIDE {
102 return GetAllDisplays().size();
106 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
107 // Don't just return all online displays. This would include displays
108 // that mirror other displays, which are not desired in this list. It's
109 // tempting to use the count returned by CGGetActiveDisplayList, but active
110 // displays exclude sleeping displays, and those are desired.
112 // It would be ridiculous to have this many displays connected, but
113 // CGDirectDisplayID is just an integer, so supporting up to this many
115 CGDirectDisplayID online_displays[128];
116 CGDisplayCount online_display_count = 0;
117 if (CGGetOnlineDisplayList(arraysize(online_displays),
119 &online_display_count) != kCGErrorSuccess) {
120 return std::vector<gfx::Display>(1, GetPrimaryDisplay());
123 typedef std::map<int64, NSScreen*> ScreenIdsToScreensMap;
124 ScreenIdsToScreensMap screen_ids_to_screens;
125 for (NSScreen* screen in [NSScreen screens]) {
126 NSDictionary* screen_device_description = [screen deviceDescription];
127 int64 screen_id = [[screen_device_description
128 objectForKey:@"NSScreenNumber"] unsignedIntValue];
129 screen_ids_to_screens[screen_id] = screen;
132 std::vector<gfx::Display> displays;
133 for (CGDisplayCount online_display_index = 0;
134 online_display_index < online_display_count;
135 ++online_display_index) {
136 CGDirectDisplayID online_display = online_displays[online_display_index];
137 if (CGDisplayMirrorsDisplay(online_display) == kCGNullDirectDisplay) {
138 // If this display doesn't mirror any other, include it in the list.
139 // The primary display in a mirrored set will be counted, but those that
140 // mirror it will not be.
141 ScreenIdsToScreensMap::iterator foundScreen =
142 screen_ids_to_screens.find(online_display);
143 if (foundScreen != screen_ids_to_screens.end()) {
144 displays.push_back(GetDisplayForScreen(foundScreen->second));
149 if (!displays.size())
150 return std::vector<gfx::Display>(1, GetPrimaryDisplay());
155 virtual gfx::Display GetDisplayNearestWindow(
156 gfx::NativeView view) const OVERRIDE {
157 NSWindow* window = nil;
158 #if !defined(USE_AURA)
159 window = [view window];
162 return GetPrimaryDisplay();
163 NSScreen* match_screen = [window screen];
165 return GetPrimaryDisplay();
166 return GetDisplayForScreen(match_screen);
169 virtual gfx::Display GetDisplayNearestPoint(
170 const gfx::Point& point) const OVERRIDE {
171 NSPoint ns_point = NSPointFromCGPoint(point.ToCGPoint());
173 NSArray* screens = [NSScreen screens];
174 NSScreen* primary = [screens objectAtIndex:0];
175 ns_point.y = NSMaxY([primary frame]) - ns_point.y;
176 for (NSScreen* screen in screens) {
177 if (NSMouseInRect(ns_point, [screen frame], NO))
178 return GetDisplayForScreen(screen);
180 return GetPrimaryDisplay();
183 // Returns the display that most closely intersects the provided bounds.
184 virtual gfx::Display GetDisplayMatching(
185 const gfx::Rect& match_rect) const OVERRIDE {
186 NSScreen* match_screen = GetMatchingScreen(match_rect);
187 return GetDisplayForScreen(match_screen);
190 // Returns the primary display.
191 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
192 // Primary display is defined as the display with the menubar,
193 // which is always at index 0.
194 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
195 gfx::Display display = GetDisplayForScreen(primary);
199 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {
200 // TODO(oshima): crbug.com/122863.
203 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {
204 // TODO(oshima): crbug.com/122863.
208 DISALLOW_COPY_AND_ASSIGN(ScreenMac);
215 #if !defined(USE_AURA)
216 Screen* CreateNativeScreen() {
217 return new ScreenMac;