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