x11_display_manager.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2019 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. #ifndef UI_BASE_X_X11_DISPLAY_MANAGER_H_
  5. #define UI_BASE_X_X11_DISPLAY_MANAGER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/cancelable_callback.h"
  9. #include "base/component_export.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "ui/base/x/x11_workspace_handler.h"
  12. #include "ui/display/display.h"
  13. #include "ui/display/display_change_notifier.h"
  14. #include "ui/gfx/geometry/point.h"
  15. #include "ui/gfx/x/event.h"
  16. namespace views {
  17. class DesktopScreenX11Test;
  18. }
  19. namespace ui {
  20. class X11ScreenOzoneTest;
  21. ////////////////////////////////////////////////////////////////////////////////
  22. // XDisplayManager class
  23. //
  24. // Responsible for fetching and maintaining list of |display::Display|s
  25. // representing X11 screens connected to the system. XRandR extension is used
  26. // when version >= 1.3 is available, otherwise it falls back to
  27. // |DefaultScreenOfDisplay| Xlib API.
  28. //
  29. // Scale Factor information and simple hooks are delegated to API clients
  30. // through |XDisplayManager::Delegate| interface. To get notifications about
  31. // dynamic display changes, clients must register |DisplayObserver| instances
  32. // and feed |XDisplayManager| with |x11::Event|s.
  33. //
  34. // All bounds and size values are assumed to be expressed in pixels.
  35. class COMPONENT_EXPORT(UI_BASE_X) XDisplayManager
  36. : public X11WorkspaceHandler::Delegate {
  37. public:
  38. class Delegate;
  39. explicit XDisplayManager(Delegate* delegate);
  40. XDisplayManager(const XDisplayManager&) = delete;
  41. XDisplayManager& operator=(const XDisplayManager&) = delete;
  42. ~XDisplayManager() override;
  43. void Init();
  44. bool IsXrandrAvailable() const;
  45. void OnEvent(const x11::Event& xev);
  46. void UpdateDisplayList();
  47. void DispatchDelayedDisplayListUpdate();
  48. display::Display GetPrimaryDisplay() const;
  49. void AddObserver(display::DisplayObserver* observer);
  50. void RemoveObserver(display::DisplayObserver* observer);
  51. const std::vector<display::Display>& displays() const { return displays_; }
  52. gfx::Point GetCursorLocation() const;
  53. // Returns current workspace.
  54. std::string GetCurrentWorkspace();
  55. private:
  56. friend class ui::X11ScreenOzoneTest;
  57. friend class views::DesktopScreenX11Test;
  58. void SetDisplayList(std::vector<display::Display> displays);
  59. void FetchDisplayList();
  60. // X11WorkspaceHandler override:
  61. void OnCurrentWorkspaceChanged(const std::string& new_workspace) override;
  62. const raw_ptr<Delegate> delegate_;
  63. std::vector<display::Display> displays_;
  64. display::DisplayChangeNotifier change_notifier_;
  65. const raw_ptr<x11::Connection> connection_;
  66. x11::Window x_root_window_;
  67. int64_t primary_display_index_ = 0;
  68. // XRandR version. MAJOR * 100 + MINOR. Zero if no xrandr is present.
  69. const int xrandr_version_;
  70. // The task which fetches/updates display list info asynchronously.
  71. base::CancelableOnceClosure update_task_;
  72. X11WorkspaceHandler workspace_handler_;
  73. };
  74. class COMPONENT_EXPORT(UI_BASE_X) XDisplayManager::Delegate {
  75. public:
  76. virtual ~Delegate() = default;
  77. virtual void OnXDisplayListUpdated() = 0;
  78. virtual float GetXDisplayScaleFactor() const = 0;
  79. };
  80. } // namespace ui
  81. #endif // UI_BASE_X_X11_DISPLAY_MANAGER_H_