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];
50 CGDirectDisplayID display_id = [[[screen deviceDescription]
51 objectForKey:@"NSScreenNumber"] unsignedIntValue];
53 gfx::Display display(display_id, gfx::Rect(NSRectToCGRect(frame)));
54 NSRect visible_frame = [screen visibleFrame];
55 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
57 // Convert work area's coordinate systems.
58 if ([screen isEqual:primary]) {
59 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame));
60 work_area.set_y(frame.size.height - visible_frame.origin.y -
61 visible_frame.size.height);
62 display.set_work_area(work_area);
64 display.set_bounds(ConvertCoordinateSystem(frame));
65 display.set_work_area(ConvertCoordinateSystem(visible_frame));
68 if ([screen respondsToSelector:@selector(backingScaleFactor)])
69 scale = [screen backingScaleFactor];
71 scale = [screen userSpaceScaleFactor];
72 display.set_device_scale_factor(scale);
73 // CGDisplayRotation returns a double. Display::SetRotationAsDegree will
74 // handle the unexpected situations were the angle is not a multiple of 90.
75 display.SetRotationAsDegree(static_cast<int>(CGDisplayRotation(display_id)));
79 class ScreenMac : public gfx::Screen {
83 virtual bool IsDIPEnabled() OVERRIDE {
87 virtual gfx::Point GetCursorScreenPoint() OVERRIDE {
88 NSPoint mouseLocation = [NSEvent mouseLocation];
89 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen.
90 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
91 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y;
92 return gfx::Point(mouseLocation.x, mouseLocation.y);
95 virtual gfx::NativeWindow GetWindowUnderCursor() OVERRIDE {
97 return gfx::NativeWindow();
100 virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point)
103 return gfx::NativeWindow();
106 virtual int GetNumDisplays() const OVERRIDE {
107 return GetAllDisplays().size();
111 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
112 // Don't just return all online displays. This would include displays
113 // that mirror other displays, which are not desired in this list. It's
114 // tempting to use the count returned by CGGetActiveDisplayList, but active
115 // displays exclude sleeping displays, and those are desired.
117 // It would be ridiculous to have this many displays connected, but
118 // CGDirectDisplayID is just an integer, so supporting up to this many
120 CGDirectDisplayID online_displays[128];
121 CGDisplayCount online_display_count = 0;
122 if (CGGetOnlineDisplayList(arraysize(online_displays),
124 &online_display_count) != kCGErrorSuccess) {
125 return std::vector<gfx::Display>(1, GetPrimaryDisplay());
128 typedef std::map<int64, NSScreen*> ScreenIdsToScreensMap;
129 ScreenIdsToScreensMap screen_ids_to_screens;
130 for (NSScreen* screen in [NSScreen screens]) {
131 NSDictionary* screen_device_description = [screen deviceDescription];
132 int64 screen_id = [[screen_device_description
133 objectForKey:@"NSScreenNumber"] unsignedIntValue];
134 screen_ids_to_screens[screen_id] = screen;
137 std::vector<gfx::Display> displays;
138 for (CGDisplayCount online_display_index = 0;
139 online_display_index < online_display_count;
140 ++online_display_index) {
141 CGDirectDisplayID online_display = online_displays[online_display_index];
142 if (CGDisplayMirrorsDisplay(online_display) == kCGNullDirectDisplay) {
143 // If this display doesn't mirror any other, include it in the list.
144 // The primary display in a mirrored set will be counted, but those that
145 // mirror it will not be.
146 ScreenIdsToScreensMap::iterator foundScreen =
147 screen_ids_to_screens.find(online_display);
148 if (foundScreen != screen_ids_to_screens.end()) {
149 displays.push_back(GetDisplayForScreen(foundScreen->second));
154 if (!displays.size())
155 return std::vector<gfx::Display>(1, GetPrimaryDisplay());
160 virtual gfx::Display GetDisplayNearestWindow(
161 gfx::NativeView view) const OVERRIDE {
162 NSWindow* window = nil;
163 #if !defined(USE_AURA)
164 window = [view window];
167 return GetPrimaryDisplay();
168 NSScreen* match_screen = [window screen];
170 return GetPrimaryDisplay();
171 return GetDisplayForScreen(match_screen);
174 virtual gfx::Display GetDisplayNearestPoint(
175 const gfx::Point& point) const OVERRIDE {
176 NSPoint ns_point = NSPointFromCGPoint(point.ToCGPoint());
178 NSArray* screens = [NSScreen screens];
179 NSScreen* primary = [screens objectAtIndex:0];
180 ns_point.y = NSMaxY([primary frame]) - ns_point.y;
181 for (NSScreen* screen in screens) {
182 if (NSMouseInRect(ns_point, [screen frame], NO))
183 return GetDisplayForScreen(screen);
185 return GetPrimaryDisplay();
188 // Returns the display that most closely intersects the provided bounds.
189 virtual gfx::Display GetDisplayMatching(
190 const gfx::Rect& match_rect) const OVERRIDE {
191 NSScreen* match_screen = GetMatchingScreen(match_rect);
192 return GetDisplayForScreen(match_screen);
195 // Returns the primary display.
196 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
197 // Primary display is defined as the display with the menubar,
198 // which is always at index 0.
199 NSScreen* primary = [[NSScreen screens] objectAtIndex:0];
200 gfx::Display display = GetDisplayForScreen(primary);
204 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {
205 // TODO(oshima): crbug.com/122863.
208 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {
209 // TODO(oshima): crbug.com/122863.
213 DISALLOW_COPY_AND_ASSIGN(ScreenMac);
220 #if !defined(USE_AURA)
221 Screen* CreateNativeScreen() {
222 return new ScreenMac;