| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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_WINDOW_CACHE_H_ |
| 6 #define UI_BASE_X_X11_WINDOW_CACHE_H_ |
| 7 |
| 8 #include <xcb/xcb.h> |
| 9 |
| 10 #include <algorithm> |
| 11 #include <list> |
| 12 #include <unordered_map> |
| 13 |
| 14 #include "base/macros.h" |
| 15 #include "ui/base/x/ui_base_x_export.h" |
| 16 #include "ui/events/platform/platform_event_observer.h" |
| 17 #include "ui/gfx/x/x11_types.h" |
| 18 |
| 19 namespace ui { |
| 20 |
| 21 class X11EventSource; |
| 22 class XScopedEventSelector; |
| 23 |
| 24 // Keep tabs on the window tree starting from a root window. |
| 25 class UI_BASE_X_EXPORT XWindowCache : public PlatformEventObserver { |
| 26 private: |
| 27 struct GetWindowAttributesRequest; |
| 28 struct GetGeometryRequest; |
| 29 struct ListPropertiesRequest; |
| 30 struct QueryTreeRequest; |
| 31 struct GetPropertyRequest; |
| 32 |
| 33 public: |
| 34 class Window; |
| 35 class Property { |
| 36 public: |
| 37 ~Property(); |
| 38 |
| 39 XID name() const { return name_; } |
| 40 |
| 41 XID type() const { return type_; } |
| 42 uint8_t format() const { return format_; } |
| 43 int length() const { return length_; } |
| 44 const void* data() const { return data_; } |
| 45 |
| 46 private: |
| 47 friend class XWindowCache; |
| 48 Property(xcb_atom_t name, Window* window, XWindowCache* cache); |
| 49 |
| 50 // Ensures the Property is fully cached, and clients can get any |
| 51 // data from it they want. |this| will be destructed and |
| 52 // Validate() will return false if the window was destroyed before |
| 53 // it could be cached. Otherwise, returns true. |
| 54 bool Validate(); |
| 55 |
| 56 xcb_atom_t name_; |
| 57 |
| 58 GetPropertyRequest* property_request_; |
| 59 xcb_atom_t type_; |
| 60 // Format can be 8, 16, or 32, indicating how |data| should be |
| 61 // interpreted. |
| 62 uint8_t format_; |
| 63 int length_; |
| 64 uint8_t* data_; |
| 65 |
| 66 XWindowCache* cache_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(Property); |
| 69 }; |
| 70 |
| 71 class Window { |
| 72 public: |
| 73 // A container for children so clients can use range-based for loops, and we |
| 74 // don't have begin() and end() methods in Window. |
| 75 class Children { |
| 76 public: |
| 77 class iterator |
| 78 : public std::iterator<std::forward_iterator_tag, const Window*> { |
| 79 public: |
| 80 ~iterator(); |
| 81 iterator(const iterator& other); |
| 82 |
| 83 iterator& operator++(); |
| 84 bool operator==(const iterator& other) const; |
| 85 bool operator!=(const iterator& other) const; |
| 86 const Window* operator*() const; |
| 87 |
| 88 private: |
| 89 friend class Children; |
| 90 |
| 91 explicit iterator( |
| 92 std::list<std::unique_ptr<Window>>::const_iterator it, |
| 93 std::list<std::unique_ptr<Window>>::const_iterator end); |
| 94 |
| 95 void SkipInvalidChildren(); |
| 96 |
| 97 std::list<std::unique_ptr<Window>>::const_iterator it_, end_; |
| 98 }; |
| 99 |
| 100 Children(const Window* window); |
| 101 |
| 102 std::size_t size() const; |
| 103 |
| 104 Children::iterator begin() const; |
| 105 Children::iterator end() const; |
| 106 |
| 107 private: |
| 108 const Window* window_; |
| 109 }; |
| 110 |
| 111 ~Window(); |
| 112 |
| 113 XID id() const { return id_; } |
| 114 const Window* parent() const { return parent_; } |
| 115 |
| 116 bool override_redirect() const { return override_redirect_; } |
| 117 bool is_mapped() const { return is_mapped_; } |
| 118 |
| 119 int16_t x() const { return x_; } |
| 120 int16_t y() const { return y_; } |
| 121 uint16_t width() const { return width_; } |
| 122 uint16_t height() const { return height_; } |
| 123 uint16_t border_width() const { return border_width_; } |
| 124 |
| 125 // Callers should not hold onto the returned Property. Returns |
| 126 // nullptr if the property doesn't exist. |
| 127 const Property* GetProperty(XID name) const; |
| 128 |
| 129 Children GetChildren() const; |
| 130 |
| 131 private: |
| 132 friend class XWindowCache; |
| 133 Window(xcb_window_t id, Window* parent, XWindowCache* cache); |
| 134 |
| 135 // Ensures the Window is fully cached, and clients can get any |
| 136 // data from it they want. |this| will be destructed and |
| 137 // Validate() will return false if the window was destroyed before |
| 138 // it could be cached. Otherwise, returns true. |
| 139 bool Validate(); |
| 140 bool IsValid() const; |
| 141 |
| 142 xcb_window_t id_; |
| 143 Window* parent_; |
| 144 |
| 145 GetWindowAttributesRequest* attributes_request_; |
| 146 bool override_redirect_; |
| 147 // |is_mapped| is different from the map_state that is returned |
| 148 // by XGetWindowAttributes. map_state can be one of IsUnmapped, |
| 149 // IsUnviewable, or IsViewable. map_state is set to |
| 150 // IsUnviewable when the window is mapped but some ancestor |
| 151 // window is unmapped. If you need to check for this case, you |
| 152 // must do so manually. |
| 153 bool is_mapped_; |
| 154 |
| 155 GetGeometryRequest* geometry_request_; |
| 156 int16_t x_, y_; |
| 157 uint16_t width_, height_; |
| 158 uint16_t border_width_; |
| 159 |
| 160 ListPropertiesRequest* properties_request_; |
| 161 std::unordered_map<xcb_atom_t, std::unique_ptr<Property>> properties_; |
| 162 |
| 163 QueryTreeRequest* children_request_; |
| 164 std::list<std::unique_ptr<Window>> children_; |
| 165 |
| 166 std::unique_ptr<XScopedEventSelector> selected_events_; |
| 167 |
| 168 XWindowCache* cache_; |
| 169 |
| 170 DISALLOW_COPY_AND_ASSIGN(Window); |
| 171 }; |
| 172 |
| 173 XWindowCache(XDisplay* display, X11EventSource* event_source, XID root); |
| 174 ~XWindowCache() override; |
| 175 |
| 176 // Callers should not hold onto the returned Window. Returns |
| 177 // nullptr when window |id| is not cached. |
| 178 const Window* GetWindow(XID id) const; |
| 179 |
| 180 protected: |
| 181 // Overridden from PlatformEventDispatcher: |
| 182 void WillProcessEvent(const PlatformEvent& event) override; |
| 183 void DidProcessEvent(const PlatformEvent& event) override; |
| 184 |
| 185 private: |
| 186 friend class XWindowCacheTest; |
| 187 |
| 188 template <typename T> |
| 189 static void CacheWindowGeometryFromResponse(Window* window, |
| 190 const T& response); |
| 191 |
| 192 static auto FindChild(Window* parent, xcb_window_t child_id) |
| 193 -> decltype(parent->children_.begin()); |
| 194 |
| 195 // Clears all state and rebuilds the entire cache. |
| 196 void ResetCache(); |
| 197 void ResetCacheImpl(); |
| 198 |
| 199 // Creates and adds a new property to |window|. |
| 200 void CacheProperty(Window* window, xcb_atom_t atom); |
| 201 |
| 202 // Allocates/Deallocates a Window. Responsible for setting up and breaking |
| 203 // parent/child relationships. |
| 204 void CreateWindow(xcb_window_t id, XWindowCache::Window* parent); |
| 205 void DestroyWindow(Window* window); |
| 206 |
| 207 void ProcessEvent(const XEvent* event); |
| 208 |
| 209 Window* GetWindowInternal(XID id) const; |
| 210 |
| 211 XDisplay* display_; |
| 212 xcb_connection_t* connection_; |
| 213 xcb_window_t root_id_; |
| 214 |
| 215 X11EventSource* event_source_; |
| 216 |
| 217 std::unique_ptr<Window> root_; |
| 218 std::unordered_map<xcb_window_t, Window*> windows_; |
| 219 |
| 220 // Don't cache icon properties because they take up too much space and we |
| 221 // currently never use them. |
| 222 xcb_intern_atom_cookie_t net_wm_icon_cookie_; |
| 223 xcb_atom_t net_wm_icon_; |
| 224 }; |
| 225 |
| 226 } // namespace ui |
| 227 |
| 228 #endif // UI_BASE_X_X11_WINDOW_CACHE_H_ |
| OLD | NEW |