Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/screen.h" | 5 #include "ui/gfx/screen.h" |
| 6 | 6 |
| 7 #import <ApplicationServices/ApplicationServices.h> | 7 #import <ApplicationServices/ApplicationServices.h> |
| 8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/mac/sdk_forward_declarations.h" | 13 #include "base/mac/sdk_forward_declarations.h" |
| 14 #include "base/timer/timer.h" | |
| 14 #include "ui/gfx/display.h" | 15 #include "ui/gfx/display.h" |
| 16 #include "ui/gfx/screen_display_observer_delegate.h" | |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 20 // The delay to handle the display configuration changes. | |
| 21 // See comments in ScreenMac::HandleDisplayReconfiguration. | |
| 22 const int64 kConfigureDelayMs = 500; | |
| 23 | |
| 18 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) { | 24 gfx::Rect ConvertCoordinateSystem(NSRect ns_rect) { |
| 19 // Primary monitor is defined as the monitor with the menubar, | 25 // Primary monitor is defined as the monitor with the menubar, |
| 20 // which is always at index 0. | 26 // which is always at index 0. |
| 21 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0]; | 27 NSScreen* primary_screen = [[NSScreen screens] objectAtIndex:0]; |
| 22 float primary_screen_height = [primary_screen frame].size.height; | 28 float primary_screen_height = [primary_screen frame].size.height; |
| 23 gfx::Rect rect(NSRectToCGRect(ns_rect)); | 29 gfx::Rect rect(NSRectToCGRect(ns_rect)); |
| 24 rect.set_y(primary_screen_height - rect.y() - rect.height()); | 30 rect.set_y(primary_screen_height - rect.y() - rect.height()); |
| 25 return rect; | 31 return rect; |
| 26 } | 32 } |
| 27 | 33 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 39 max_area = area; | 45 max_area = area; |
| 40 max_screen = screen; | 46 max_screen = screen; |
| 41 } | 47 } |
| 42 } | 48 } |
| 43 | 49 |
| 44 return max_screen; | 50 return max_screen; |
| 45 } | 51 } |
| 46 | 52 |
| 47 gfx::Display GetDisplayForScreen(NSScreen* screen) { | 53 gfx::Display GetDisplayForScreen(NSScreen* screen) { |
| 48 NSRect frame = [screen frame]; | 54 NSRect frame = [screen frame]; |
| 49 // TODO(oshima): Implement ID and Observer. | |
| 50 gfx::Display display(0, gfx::Rect(NSRectToCGRect(frame))); | |
| 51 | 55 |
| 56 CGDirectDisplayID display_id = [[[screen deviceDescription] | |
| 57 objectForKey:@"NSScreenNumber"] unsignedIntValue]; | |
| 58 | |
| 59 gfx::Display display(display_id, gfx::Rect(NSRectToCGRect(frame))); | |
| 52 NSRect visible_frame = [screen visibleFrame]; | 60 NSRect visible_frame = [screen visibleFrame]; |
| 53 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; | 61 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; |
| 54 | 62 |
| 55 // Convert work area's coordinate systems. | 63 // Convert work area's coordinate systems. |
| 56 if ([screen isEqual:primary]) { | 64 if ([screen isEqual:primary]) { |
| 57 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame)); | 65 gfx::Rect work_area = gfx::Rect(NSRectToCGRect(visible_frame)); |
| 58 work_area.set_y(frame.size.height - visible_frame.origin.y - | 66 work_area.set_y(frame.size.height - visible_frame.origin.y - |
| 59 visible_frame.size.height); | 67 visible_frame.size.height); |
| 60 display.set_work_area(work_area); | 68 display.set_work_area(work_area); |
| 61 } else { | 69 } else { |
| 62 display.set_bounds(ConvertCoordinateSystem(frame)); | 70 display.set_bounds(ConvertCoordinateSystem(frame)); |
| 63 display.set_work_area(ConvertCoordinateSystem(visible_frame)); | 71 display.set_work_area(ConvertCoordinateSystem(visible_frame)); |
| 64 } | 72 } |
| 65 CGFloat scale; | 73 CGFloat scale; |
| 66 if ([screen respondsToSelector:@selector(backingScaleFactor)]) | 74 if ([screen respondsToSelector:@selector(backingScaleFactor)]) |
| 67 scale = [screen backingScaleFactor]; | 75 scale = [screen backingScaleFactor]; |
| 68 else | 76 else |
| 69 scale = [screen userSpaceScaleFactor]; | 77 scale = [screen userSpaceScaleFactor]; |
| 70 display.set_device_scale_factor(scale); | 78 display.set_device_scale_factor(scale); |
| 79 // CGDisplayRotation returns a double. Display::SetRotationAsDegree will | |
| 80 // handle the unexpected situations were the angle is not a multiple of 90. | |
| 81 display.SetRotationAsDegree(static_cast<int>(CGDisplayRotation(display_id))); | |
| 71 return display; | 82 return display; |
| 72 } | 83 } |
| 73 | 84 |
| 74 class ScreenMac : public gfx::Screen { | 85 class ScreenMac : public gfx::Screen { |
| 75 public: | 86 public: |
| 76 ScreenMac() {} | 87 ScreenMac() { |
| 88 displays_ = BuildDisplaysFromQuartz(); | |
| 89 | |
| 90 CGDisplayRegisterReconfigurationCallback( | |
| 91 ScreenMac::DisplayReconfigurationCallBack, this); | |
| 92 } | |
| 77 | 93 |
| 78 virtual bool IsDIPEnabled() OVERRIDE { | 94 virtual bool IsDIPEnabled() OVERRIDE { |
| 79 return true; | 95 return true; |
| 80 } | 96 } |
| 81 | 97 |
| 82 virtual gfx::Point GetCursorScreenPoint() OVERRIDE { | 98 virtual gfx::Point GetCursorScreenPoint() OVERRIDE { |
| 83 NSPoint mouseLocation = [NSEvent mouseLocation]; | 99 NSPoint mouseLocation = [NSEvent mouseLocation]; |
| 84 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. | 100 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. |
| 85 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | 101 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 86 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; | 102 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 97 NOTIMPLEMENTED(); | 113 NOTIMPLEMENTED(); |
| 98 return gfx::NativeWindow(); | 114 return gfx::NativeWindow(); |
| 99 } | 115 } |
| 100 | 116 |
| 101 virtual int GetNumDisplays() const OVERRIDE { | 117 virtual int GetNumDisplays() const OVERRIDE { |
| 102 return GetAllDisplays().size(); | 118 return GetAllDisplays().size(); |
| 103 | 119 |
| 104 } | 120 } |
| 105 | 121 |
| 106 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE { | 122 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE { |
| 107 // Don't just return all online displays. This would include displays | 123 return 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. | |
| 111 | |
| 112 // It would be ridiculous to have this many displays connected, but | |
| 113 // CGDirectDisplayID is just an integer, so supporting up to this many | |
| 114 // doesn't hurt. | |
| 115 CGDirectDisplayID online_displays[128]; | |
| 116 CGDisplayCount online_display_count = 0; | |
| 117 if (CGGetOnlineDisplayList(arraysize(online_displays), | |
| 118 online_displays, | |
| 119 &online_display_count) != kCGErrorSuccess) { | |
| 120 return std::vector<gfx::Display>(1, GetPrimaryDisplay()); | |
| 121 } | |
| 122 | |
| 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; | |
| 130 } | |
| 131 | |
| 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)); | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 if (!displays.size()) | |
| 150 return std::vector<gfx::Display>(1, GetPrimaryDisplay()); | |
| 151 | |
| 152 return displays; | |
| 153 } | 124 } |
| 154 | 125 |
| 155 virtual gfx::Display GetDisplayNearestWindow( | 126 virtual gfx::Display GetDisplayNearestWindow( |
| 156 gfx::NativeView view) const OVERRIDE { | 127 gfx::NativeView view) const OVERRIDE { |
| 157 NSWindow* window = nil; | 128 NSWindow* window = nil; |
| 158 #if !defined(USE_AURA) | 129 #if !defined(USE_AURA) |
| 159 window = [view window]; | 130 window = [view window]; |
| 160 #endif | 131 #endif |
| 161 if (!window) | 132 if (!window) |
| 162 return GetPrimaryDisplay(); | 133 return GetPrimaryDisplay(); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 190 // Returns the primary display. | 161 // Returns the primary display. |
| 191 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE { | 162 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE { |
| 192 // Primary display is defined as the display with the menubar, | 163 // Primary display is defined as the display with the menubar, |
| 193 // which is always at index 0. | 164 // which is always at index 0. |
| 194 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; | 165 NSScreen* primary = [[NSScreen screens] objectAtIndex:0]; |
| 195 gfx::Display display = GetDisplayForScreen(primary); | 166 gfx::Display display = GetDisplayForScreen(primary); |
| 196 return display; | 167 return display; |
| 197 } | 168 } |
| 198 | 169 |
| 199 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE { | 170 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE { |
| 200 // TODO(oshima): crbug.com/122863. | 171 observer_delegate_.AddObserver(observer); |
| 201 } | 172 } |
| 202 | 173 |
| 203 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE { | 174 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE { |
| 204 // TODO(oshima): crbug.com/122863. | 175 observer_delegate_.RemoveObserver(observer); |
| 176 } | |
| 177 | |
| 178 static void DisplayReconfigurationCallBack(CGDirectDisplayID display, | |
| 179 CGDisplayChangeSummaryFlags flags, | |
| 180 void* userInfo) { | |
| 181 if (flags & kCGDisplayBeginConfigurationFlag) | |
| 182 return; | |
| 183 | |
| 184 static_cast<ScreenMac*>(userInfo)->HandleDisplayReconfiguration(); | |
| 185 } | |
| 186 | |
| 187 void HandleDisplayReconfiguration() { | |
| 188 // Given that we need to rebuild the list of displays, we want to coalesce | |
| 189 // the events. For that, we use a timer that will be reset every time we get | |
| 190 // a new event and will be fulfilled kConfigureDelayMs after the latest. | |
| 191 if (configure_timer_.get() && configure_timer_->IsRunning()) { | |
| 192 configure_timer_->Reset(); | |
| 193 return; | |
| 194 } | |
| 195 | |
| 196 configure_timer_.reset(new base::OneShotTimer<ScreenMac>()); | |
|
oshima
2014/07/09 17:12:51
Any reason not to use RepeatingTimer?
mlamouri (slow - plz ping)
2014/07/10 12:54:34
There is no repeating pattern here. The timer is o
| |
| 197 configure_timer_->Start( | |
| 198 FROM_HERE, | |
| 199 base::TimeDelta::FromMilliseconds(kConfigureDelayMs), | |
| 200 this, | |
| 201 &ScreenMac::ConfigureTimerFired); | |
| 205 } | 202 } |
| 206 | 203 |
| 207 private: | 204 private: |
| 205 void ConfigureTimerFired() { | |
| 206 std::vector<gfx::Display> old_displays = displays_; | |
| 207 displays_ = BuildDisplaysFromQuartz(); | |
| 208 | |
| 209 observer_delegate_.NotifyDisplaysChanged(old_displays, displays_); | |
| 210 } | |
| 211 | |
| 212 std::vector<gfx::Display> BuildDisplaysFromQuartz() const { | |
| 213 // Don't just return all online displays. This would include displays | |
| 214 // that mirror other displays, which are not desired in this list. It's | |
| 215 // tempting to use the count returned by CGGetActiveDisplayList, but active | |
| 216 // displays exclude sleeping displays, and those are desired. | |
| 217 | |
| 218 // It would be ridiculous to have this many displays connected, but | |
| 219 // CGDirectDisplayID is just an integer, so supporting up to this many | |
| 220 // doesn't hurt. | |
| 221 CGDirectDisplayID online_displays[128]; | |
| 222 CGDisplayCount online_display_count = 0; | |
| 223 if (CGGetOnlineDisplayList(arraysize(online_displays), | |
| 224 online_displays, | |
| 225 &online_display_count) != kCGErrorSuccess) { | |
| 226 return std::vector<gfx::Display>(1, GetPrimaryDisplay()); | |
| 227 } | |
| 228 | |
| 229 typedef std::map<int64, NSScreen*> ScreenIdsToScreensMap; | |
| 230 ScreenIdsToScreensMap screen_ids_to_screens; | |
| 231 for (NSScreen* screen in [NSScreen screens]) { | |
| 232 NSDictionary* screen_device_description = [screen deviceDescription]; | |
| 233 int64 screen_id = [[screen_device_description | |
| 234 objectForKey:@"NSScreenNumber"] unsignedIntValue]; | |
| 235 screen_ids_to_screens[screen_id] = screen; | |
| 236 } | |
| 237 | |
| 238 std::vector<gfx::Display> displays; | |
| 239 for (CGDisplayCount online_display_index = 0; | |
| 240 online_display_index < online_display_count; | |
| 241 ++online_display_index) { | |
| 242 CGDirectDisplayID online_display = online_displays[online_display_index]; | |
| 243 if (CGDisplayMirrorsDisplay(online_display) == kCGNullDirectDisplay) { | |
| 244 // If this display doesn't mirror any other, include it in the list. | |
| 245 // The primary display in a mirrored set will be counted, but those that | |
| 246 // mirror it will not be. | |
| 247 ScreenIdsToScreensMap::iterator foundScreen = | |
| 248 screen_ids_to_screens.find(online_display); | |
| 249 if (foundScreen != screen_ids_to_screens.end()) { | |
| 250 displays.push_back(GetDisplayForScreen(foundScreen->second)); | |
| 251 } | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 if (!displays.size()) | |
| 256 return std::vector<gfx::Display>(1, GetPrimaryDisplay()); | |
| 257 | |
| 258 return displays; | |
| 259 } | |
| 260 | |
| 261 // The displays currently attached to the device. | |
| 262 std::vector<gfx::Display> displays_; | |
| 263 | |
| 264 // The timer to delay configuring outputs. See also the comments in | |
| 265 // HandleDisplayReconfiguration(). | |
| 266 scoped_ptr<base::OneShotTimer<ScreenMac> > configure_timer_; | |
| 267 | |
| 268 ScreenDisplayObserverDelegate observer_delegate_; | |
| 269 | |
| 208 DISALLOW_COPY_AND_ASSIGN(ScreenMac); | 270 DISALLOW_COPY_AND_ASSIGN(ScreenMac); |
| 209 }; | 271 }; |
| 210 | 272 |
| 211 } // namespace | 273 } // namespace |
| 212 | 274 |
| 213 namespace gfx { | 275 namespace gfx { |
| 214 | 276 |
| 215 #if !defined(USE_AURA) | 277 #if !defined(USE_AURA) |
| 216 Screen* CreateNativeScreen() { | 278 Screen* CreateNativeScreen() { |
| 217 return new ScreenMac; | 279 return new ScreenMac; |
| 218 } | 280 } |
| 219 #endif | 281 #endif |
| 220 | 282 |
| 221 } | 283 } |
| OLD | NEW |