1/*
2 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "modules/desktop_capture/mac/desktop_configuration.h"
12
13#include <math.h>
14#include <algorithm>
15#include <Cocoa/Cocoa.h>
16
17namespace webrtc {
18
19namespace {
20
21DesktopRect NSRectToDesktopRect(const NSRect& ns_rect) {
22  return DesktopRect::MakeLTRB(
23      static_cast<int>(floor(ns_rect.origin.x)),
24      static_cast<int>(floor(ns_rect.origin.y)),
25      static_cast<int>(ceil(ns_rect.origin.x + ns_rect.size.width)),
26      static_cast<int>(ceil(ns_rect.origin.y + ns_rect.size.height)));
27}
28
29// Inverts the position of |rect| from bottom-up coordinates to top-down,
30// relative to |bounds|.
31void InvertRectYOrigin(const DesktopRect& bounds,
32                       DesktopRect* rect) {
33  assert(bounds.top() == 0);
34  *rect = DesktopRect::MakeXYWH(
35      rect->left(), bounds.bottom() - rect->bottom(),
36      rect->width(), rect->height());
37}
38
39MacDisplayConfiguration GetConfigurationForScreen(NSScreen* screen) {
40  MacDisplayConfiguration display_config;
41
42  // Fetch the NSScreenNumber, which is also the CGDirectDisplayID.
43  NSDictionary* device_description = [screen deviceDescription];
44  display_config.id = static_cast<CGDirectDisplayID>(
45      [[device_description objectForKey:@"NSScreenNumber"] intValue]);
46
47  // Determine the display's logical & physical dimensions.
48  NSRect ns_bounds = [screen frame];
49  display_config.bounds = NSRectToDesktopRect(ns_bounds);
50
51  // If the host is running Mac OS X 10.7+ or later, query the scaling factor
52  // between logical and physical (aka "backing") pixels, otherwise assume 1:1.
53  if ([screen respondsToSelector:@selector(backingScaleFactor)] &&
54      [screen respondsToSelector:@selector(convertRectToBacking:)]) {
55    display_config.dip_to_pixel_scale = [screen backingScaleFactor];
56    NSRect ns_pixel_bounds = [screen convertRectToBacking: ns_bounds];
57    display_config.pixel_bounds = NSRectToDesktopRect(ns_pixel_bounds);
58  } else {
59    display_config.pixel_bounds = display_config.bounds;
60  }
61
62  return display_config;
63}
64
65}  // namespace
66
67MacDisplayConfiguration::MacDisplayConfiguration() = default;
68MacDisplayConfiguration::MacDisplayConfiguration(
69    const MacDisplayConfiguration& other) = default;
70MacDisplayConfiguration::MacDisplayConfiguration(
71    MacDisplayConfiguration&& other) = default;
72MacDisplayConfiguration::~MacDisplayConfiguration() = default;
73
74MacDisplayConfiguration& MacDisplayConfiguration::operator=(
75    const MacDisplayConfiguration& other) = default;
76MacDisplayConfiguration& MacDisplayConfiguration::operator=(
77    MacDisplayConfiguration&& other) = default;
78
79MacDesktopConfiguration::MacDesktopConfiguration() = default;
80MacDesktopConfiguration::MacDesktopConfiguration(
81    const MacDesktopConfiguration& other) = default;
82MacDesktopConfiguration::MacDesktopConfiguration(
83    MacDesktopConfiguration&& other) = default;
84MacDesktopConfiguration::~MacDesktopConfiguration() = default;
85
86MacDesktopConfiguration& MacDesktopConfiguration::operator=(
87    const MacDesktopConfiguration& other) = default;
88MacDesktopConfiguration& MacDesktopConfiguration::operator=(
89    MacDesktopConfiguration&& other) = default;
90
91// static
92MacDesktopConfiguration MacDesktopConfiguration::GetCurrent(Origin origin) {
93  MacDesktopConfiguration desktop_config;
94
95  NSArray* screens = [NSScreen screens];
96  assert(screens);
97
98  // Iterator over the monitors, adding the primary monitor and monitors whose
99  // DPI match that of the primary monitor.
100  for (NSUInteger i = 0; i < [screens count]; ++i) {
101    MacDisplayConfiguration display_config =
102        GetConfigurationForScreen([screens objectAtIndex: i]);
103
104    if (i == 0)
105      desktop_config.dip_to_pixel_scale = display_config.dip_to_pixel_scale;
106
107    // Cocoa uses bottom-up coordinates, so if the caller wants top-down then
108    // we need to invert the positions of secondary monitors relative to the
109    // primary one (the primary monitor's position is (0,0) in both systems).
110    if (i > 0 && origin == TopLeftOrigin) {
111      InvertRectYOrigin(desktop_config.displays[0].bounds,
112                        &display_config.bounds);
113      // |display_bounds| is density dependent, so we need to convert the
114      // primay monitor's position into the secondary monitor's density context.
115      float scaling_factor = display_config.dip_to_pixel_scale /
116          desktop_config.displays[0].dip_to_pixel_scale;
117      DesktopRect primary_bounds = DesktopRect::MakeLTRB(
118          desktop_config.displays[0].pixel_bounds.left() * scaling_factor,
119          desktop_config.displays[0].pixel_bounds.top() * scaling_factor,
120          desktop_config.displays[0].pixel_bounds.right() * scaling_factor,
121          desktop_config.displays[0].pixel_bounds.bottom() * scaling_factor);
122      InvertRectYOrigin(primary_bounds, &display_config.pixel_bounds);
123    }
124
125    // Add the display to the configuration.
126    desktop_config.displays.push_back(display_config);
127
128    // Update the desktop bounds to account for this display, unless the current
129    // display uses different DPI settings.
130    if (display_config.dip_to_pixel_scale ==
131        desktop_config.dip_to_pixel_scale) {
132      desktop_config.bounds.UnionWith(display_config.bounds);
133      desktop_config.pixel_bounds.UnionWith(display_config.pixel_bounds);
134    }
135  }
136
137  return desktop_config;
138}
139
140// For convenience of comparing MacDisplayConfigurations in
141// MacDesktopConfiguration::Equals.
142bool operator==(const MacDisplayConfiguration& left,
143                const MacDisplayConfiguration& right) {
144  return left.id == right.id &&
145      left.bounds.equals(right.bounds) &&
146      left.pixel_bounds.equals(right.pixel_bounds) &&
147      left.dip_to_pixel_scale == right.dip_to_pixel_scale;
148}
149
150bool MacDesktopConfiguration::Equals(const MacDesktopConfiguration& other) {
151  return bounds.equals(other.bounds) &&
152      pixel_bounds.equals(other.pixel_bounds) &&
153      dip_to_pixel_scale == other.dip_to_pixel_scale &&
154      displays == other.displays;
155}
156
157// Finds the display configuration with the specified id.
158const MacDisplayConfiguration*
159MacDesktopConfiguration::FindDisplayConfigurationById(
160    CGDirectDisplayID id) {
161  for (MacDisplayConfigurations::const_iterator it = displays.begin();
162      it != displays.end(); ++it) {
163    if (it->id == id)
164      return &(*it);
165  }
166  return NULL;
167}
168
169}  // namespace webrtc
170