1 /*
2  *  Copyright (c) 2014 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_monitor.h"
12 
13 #include "modules/desktop_capture/mac/desktop_configuration.h"
14 #include "rtc_base/logging.h"
15 #include "rtc_base/trace_event.h"
16 
17 namespace webrtc {
18 
DesktopConfigurationMonitor()19 DesktopConfigurationMonitor::DesktopConfigurationMonitor() {
20   CGError err = CGDisplayRegisterReconfigurationCallback(
21       DesktopConfigurationMonitor::DisplaysReconfiguredCallback, this);
22   if (err != kCGErrorSuccess)
23     RTC_LOG(LS_ERROR) << "CGDisplayRegisterReconfigurationCallback " << err;
24   MutexLock lock(&desktop_configuration_lock_);
25   desktop_configuration_ = MacDesktopConfiguration::GetCurrent(
26       MacDesktopConfiguration::TopLeftOrigin);
27 }
28 
~DesktopConfigurationMonitor()29 DesktopConfigurationMonitor::~DesktopConfigurationMonitor() {
30   CGError err = CGDisplayRemoveReconfigurationCallback(
31       DesktopConfigurationMonitor::DisplaysReconfiguredCallback, this);
32   if (err != kCGErrorSuccess)
33     RTC_LOG(LS_ERROR) << "CGDisplayRemoveReconfigurationCallback " << err;
34 }
35 
desktop_configuration()36 MacDesktopConfiguration DesktopConfigurationMonitor::desktop_configuration() {
37   MutexLock lock(&desktop_configuration_lock_);
38   return desktop_configuration_;
39 }
40 
41 // static
42 // This method may be called on any system thread.
DisplaysReconfiguredCallback(CGDirectDisplayID display,CGDisplayChangeSummaryFlags flags,void * user_parameter)43 void DesktopConfigurationMonitor::DisplaysReconfiguredCallback(
44     CGDirectDisplayID display,
45     CGDisplayChangeSummaryFlags flags,
46     void* user_parameter) {
47   DesktopConfigurationMonitor* monitor =
48       reinterpret_cast<DesktopConfigurationMonitor*>(user_parameter);
49   monitor->DisplaysReconfigured(display, flags);
50 }
51 
DisplaysReconfigured(CGDirectDisplayID display,CGDisplayChangeSummaryFlags flags)52 void DesktopConfigurationMonitor::DisplaysReconfigured(
53     CGDirectDisplayID display,
54     CGDisplayChangeSummaryFlags flags) {
55   TRACE_EVENT0("webrtc", "DesktopConfigurationMonitor::DisplaysReconfigured");
56   RTC_LOG(LS_INFO) << "DisplaysReconfigured: "
57                       "DisplayID "
58                    << display << "; ChangeSummaryFlags " << flags;
59 
60   if (flags & kCGDisplayBeginConfigurationFlag) {
61     reconfiguring_displays_.insert(display);
62     return;
63   }
64 
65   reconfiguring_displays_.erase(display);
66   if (reconfiguring_displays_.empty()) {
67     MutexLock lock(&desktop_configuration_lock_);
68     desktop_configuration_ = MacDesktopConfiguration::GetCurrent(
69         MacDesktopConfiguration::TopLeftOrigin);
70   }
71 }
72 
73 }  // namespace webrtc
74