desktop_display_info_loader_mac.mm 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2021 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.
  4. #include "remoting/host/desktop_display_info_loader.h"
  5. #include <Cocoa/Cocoa.h>
  6. #include "base/check.h"
  7. namespace remoting {
  8. namespace {
  9. constexpr int kDefaultScreenDpi = 96;
  10. class DesktopDisplayInfoLoaderMac : public DesktopDisplayInfoLoader {
  11. public:
  12. DesktopDisplayInfoLoaderMac() = default;
  13. ~DesktopDisplayInfoLoaderMac() override = default;
  14. DesktopDisplayInfo GetCurrentDisplayInfo() override;
  15. };
  16. DesktopDisplayInfo DesktopDisplayInfoLoaderMac::GetCurrentDisplayInfo() {
  17. DesktopDisplayInfo result;
  18. NSArray* screens = [NSScreen screens];
  19. DCHECK(screens);
  20. // Each display origin is the bottom left corner, so we need to record the
  21. // height of the main display (#0) so that we can adjust the origin of
  22. // the secondary displays.
  23. int main_display_height = 0;
  24. for (NSUInteger i = 0; i < [screens count]; ++i) {
  25. NSScreen* screen = screens[i];
  26. NSDictionary* device = [screen deviceDescription];
  27. CGDirectDisplayID id =
  28. static_cast<CGDirectDisplayID>([device[@"NSScreenNumber"] intValue]);
  29. NSRect bounds = [screen frame];
  30. int x = bounds.origin.x;
  31. int y = bounds.origin.y;
  32. int height = bounds.size.height;
  33. bool is_default = false;
  34. if (i == 0) {
  35. DCHECK(x == 0);
  36. DCHECK(y == 0);
  37. is_default = true;
  38. main_display_height = height;
  39. }
  40. DisplayGeometry info;
  41. info.id = id;
  42. info.x = x;
  43. // Convert origin from lower left to upper left (based on main display).
  44. info.y = main_display_height - y - height;
  45. info.width = bounds.size.width;
  46. info.height = height;
  47. info.dpi = (int)(kDefaultScreenDpi * [screen backingScaleFactor]);
  48. info.bpp = 24;
  49. info.is_default = is_default;
  50. result.AddDisplay(info);
  51. }
  52. return result;
  53. }
  54. } // namespace
  55. // static
  56. std::unique_ptr<DesktopDisplayInfoLoader> DesktopDisplayInfoLoader::Create() {
  57. return std::make_unique<DesktopDisplayInfoLoaderMac>();
  58. }
  59. } // namespace remoting