diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-12 14:27:29 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-13 09:35:20 +0000 |
commit | c30a6232df03e1efbd9f3b226777b07e087a1122 (patch) | |
tree | e992f45784689f373bcc38d1b79a239ebe17ee23 /chromium/ui/base/x/x11_util.h | |
parent | 7b5b123ac58f58ffde0f4f6e488bcd09aa4decd3 (diff) | |
download | qtwebengine-chromium-85-based.tar.gz |
BASELINE: Update Chromium to 85.0.4183.14085-based
Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/ui/base/x/x11_util.h')
-rw-r--r-- | chromium/ui/base/x/x11_util.h | 311 |
1 files changed, 258 insertions, 53 deletions
diff --git a/chromium/ui/base/x/x11_util.h b/chromium/ui/base/x/x11_util.h index ff1dd2c5635..4c7c574a921 100644 --- a/chromium/ui/base/x/x11_util.h +++ b/chromium/ui/base/x/x11_util.h @@ -21,10 +21,12 @@ #include "base/containers/flat_set.h" #include "base/macros.h" #include "base/memory/ref_counted_memory.h" +#include "ui/base/cursor/mojom/cursor_type.mojom-forward.h" #include "ui/events/event_constants.h" #include "ui/events/keycodes/keyboard_codes.h" #include "ui/events/platform_event.h" #include "ui/gfx/icc_profile.h" +#include "ui/gfx/x/event.h" #include "ui/gfx/x/x11_types.h" typedef unsigned long Cursor; @@ -38,9 +40,195 @@ class SkBitmap; namespace ui { +enum WmState : uint32_t { + WM_STATE_WITHDRAWN = 0, + WM_STATE_NORMAL = 1, + WM_STATE_ICONIC = 3, +}; + +enum SizeHintsFlags : int32_t { + SIZE_HINT_US_POSITION = 1 << 0, + SIZE_HINT_US_SIZE = 1 << 1, + SIZE_HINT_P_POSITION = 1 << 2, + SIZE_HINT_P_SIZE = 1 << 3, + SIZE_HINT_P_MIN_SIZE = 1 << 4, + SIZE_HINT_P_MAX_SIZE = 1 << 5, + SIZE_HINT_P_RESIZE_INC = 1 << 6, + SIZE_HINT_P_ASPECT = 1 << 7, + SIZE_HINT_BASE_SIZE = 1 << 8, + SIZE_HINT_P_WIN_GRAVITY = 1 << 9, +}; + +struct SizeHints { + // User specified flags + int32_t flags; + // User-specified position + int32_t x, y; + // User-specified size + int32_t width, height; + // Program-specified minimum size + int32_t min_width, min_height; + // Program-specified maximum size + int32_t max_width, max_height; + // Program-specified resize increments + int32_t width_inc, height_inc; + // Program-specified minimum aspect ratios + int32_t min_aspect_num, min_aspect_den; + // Program-specified maximum aspect ratios + int32_t max_aspect_num, max_aspect_den; + // Program-specified base size + int32_t base_width, base_height; + // Program-specified window gravity + uint32_t win_gravity; +}; + +enum WmHintsFlags : uint32_t { + WM_HINT_INPUT = 1L << 0, + WM_HINT_STATE = 1L << 1, + WM_HINT_ICON_PIXMAP = 1L << 2, + WM_HINT_ICON_WINDOW = 1L << 3, + WM_HINT_ICON_POSITION = 1L << 4, + WM_HINT_ICON_MASK = 1L << 5, + WM_HINT_WINDOW_GROUP = 1L << 6, + // 1L << 7 doesn't have any defined meaning + WM_HINT_X_URGENCY = 1L << 8 +}; + +struct WmHints { + // Marks which fields in this structure are defined + int32_t flags; + // Does this application rely on the window manager to get keyboard input? + uint32_t input; + // See below + int32_t initial_state; + // Pixmap to be used as icon + xcb_pixmap_t icon_pixmap; + // Window to be used as icon + xcb_window_t icon_window; + // Initial position of icon + int32_t icon_x, icon_y; + // Icon mask bitmap + xcb_pixmap_t icon_mask; + // Identifier of related window group + xcb_window_t window_group; +}; + // These functions use the default display and this /must/ be called from // the UI thread. Thus, they don't support multiple displays. +template <typename T> +bool GetArrayProperty(x11::Window window, + x11::Atom name, + std::vector<T>* value, + x11::Atom* out_type = nullptr, + size_t amount = 0) { + static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4, ""); + + size_t bytes = amount * sizeof(T); + // The length field specifies the maximum amount of data we would like the + // server to give us. It's specified in units of 4 bytes, so divide by 4. + // Add 3 before division to round up. + size_t length = (bytes + 3) / 4; + using lentype = decltype(x11::GetPropertyRequest::long_length); + auto response = + x11::Connection::Get() + ->GetProperty( + {.window = static_cast<x11::Window>(window), + .property = name, + .long_length = + amount ? length : std::numeric_limits<lentype>::max()}) + .Sync(); + if (!response || response->format != CHAR_BIT * sizeof(T)) + return false; + + DCHECK_EQ(response->format / CHAR_BIT * response->value_len, + response->value.size()); + value->resize(response->value_len); + memcpy(value->data(), response->value.data(), response->value.size()); + if (out_type) + *out_type = response->type; + return true; +} + +template <typename T> +bool GetProperty(x11::Window window, const x11::Atom name, T* value) { + std::vector<T> values; + if (!GetArrayProperty(window, name, &values, nullptr, 1) || values.empty()) + return false; + *value = values[0]; + return true; +} + +template <typename T> +void SetArrayProperty(x11::Window window, + x11::Atom name, + x11::Atom type, + const std::vector<T>& values) { + static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4, ""); + std::vector<uint8_t> data(sizeof(T) * values.size()); + memcpy(data.data(), values.data(), sizeof(T) * values.size()); + x11::Connection::Get()->ChangeProperty( + {.window = static_cast<x11::Window>(window), + .property = name, + .type = type, + .format = CHAR_BIT * sizeof(T), + .data_len = values.size(), + .data = data}); +} + +template <typename T> +void SetProperty(x11::Window window, + x11::Atom name, + x11::Atom type, + const T& value) { + SetArrayProperty(window, name, type, std::vector<T>{value}); +} + +template <typename T> +void SendEvent(const T& event, x11::Window target, x11::EventMask mask) { + static_assert(T::type_id > 0, "T must be an x11::*Event type"); + auto event_bytes = x11::Write(event); + DCHECK_LE(event_bytes.size(), 32ul); + event_bytes.resize(32); + + x11::SendEventRequest send_event{false, target, mask}; + std::copy(event_bytes.begin(), event_bytes.end(), send_event.event.begin()); + x11::Connection::Get()->SendEvent(send_event); +} + +COMPONENT_EXPORT(UI_BASE_X) +void DeleteProperty(x11::Window window, x11::Atom name); + +COMPONENT_EXPORT(UI_BASE_X) +bool GetWmNormalHints(x11::Window window, SizeHints* hints); + +COMPONENT_EXPORT(UI_BASE_X) +void SetWmNormalHints(x11::Window window, const SizeHints& hints); + +COMPONENT_EXPORT(UI_BASE_X) +bool GetWmHints(x11::Window window, WmHints* hints); + +COMPONENT_EXPORT(UI_BASE_X) +void SetWmHints(x11::Window window, const WmHints& hints); + +COMPONENT_EXPORT(UI_BASE_X) +void WithdrawWindow(x11::Window window); + +COMPONENT_EXPORT(UI_BASE_X) +void RaiseWindow(x11::Window window); + +COMPONENT_EXPORT(UI_BASE_X) +void LowerWindow(x11::Window window); + +COMPONENT_EXPORT(UI_BASE_X) +void DefineCursor(x11::Window window, x11::Cursor cursor); + +COMPONENT_EXPORT(UI_BASE_X) +x11::Window CreateDummyWindow(const std::string& name = ""); + +COMPONENT_EXPORT(UI_BASE_X) +x11::KeyCode KeysymToKeycode(x11::Connection* connection, x11::KeySym keysym); + // These functions cache their results --------------------------------- // Returns true if the system supports XINPUT2. @@ -73,11 +261,16 @@ COMPONENT_EXPORT(UI_BASE_X) XcursorImage* SkBitmapToXcursorImage(const SkBitmap& bitmap, const gfx::Point& hotspot); +// Loads and returns an X11 cursor, trying to find one that matches |type|. If +// unavailable, x11::None is returned. +COMPONENT_EXPORT(UI_BASE_X) +::Cursor LoadCursorFromType(mojom::CursorType type); + // Coalesce all pending motion events (touch or mouse) that are at the top of // the queue, and return the number eliminated, storing the last one in // |last_event|. COMPONENT_EXPORT(UI_BASE_X) -int CoalescePendingMotionEvents(const XEvent* xev, XEvent* last_event); +int CoalescePendingMotionEvents(const x11::Event* xev, x11::Event* last_event); // Hides the host cursor. COMPONENT_EXPORT(UI_BASE_X) void HideHostCursor(); @@ -87,7 +280,7 @@ COMPONENT_EXPORT(UI_BASE_X)::Cursor CreateInvisibleCursor(); // Sets whether |window| should use the OS window frame. COMPONENT_EXPORT(UI_BASE_X) -void SetUseOSWindowFrame(XID window, bool use_os_window_frame); +void SetUseOSWindowFrame(x11::Window window, bool use_os_window_frame); // These functions do not cache their results -------------------------- @@ -95,122 +288,123 @@ void SetUseOSWindowFrame(XID window, bool use_os_window_frame); COMPONENT_EXPORT(UI_BASE_X) bool IsShapeExtensionAvailable(); // Get the X window id for the default root window -COMPONENT_EXPORT(UI_BASE_X) XID GetX11RootWindow(); +COMPONENT_EXPORT(UI_BASE_X) x11::Window GetX11RootWindow(); // Returns the user's current desktop. COMPONENT_EXPORT(UI_BASE_X) bool GetCurrentDesktop(int* desktop); -enum HideTitlebarWhenMaximized { +enum HideTitlebarWhenMaximized : uint32_t { SHOW_TITLEBAR_WHEN_MAXIMIZED = 0, HIDE_TITLEBAR_WHEN_MAXIMIZED = 1, }; // Sets _GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED on |window|. COMPONENT_EXPORT(UI_BASE_X) -void SetHideTitlebarWhenMaximizedProperty(XID window, +void SetHideTitlebarWhenMaximizedProperty(x11::Window window, HideTitlebarWhenMaximized property); // Clears all regions of X11's default root window by filling black pixels. COMPONENT_EXPORT(UI_BASE_X) void ClearX11DefaultRootWindow(); // Returns true if |window| is visible. -COMPONENT_EXPORT(UI_BASE_X) bool IsWindowVisible(XID window); +COMPONENT_EXPORT(UI_BASE_X) bool IsWindowVisible(x11::Window window); // Returns the inner bounds of |window| (excluding the non-client area). COMPONENT_EXPORT(UI_BASE_X) -bool GetInnerWindowBounds(XID window, gfx::Rect* rect); +bool GetInnerWindowBounds(x11::Window window, gfx::Rect* rect); // Returns the non-client area extents of |window|. This is a negative inset; it // represents the negative size of the window border on all sides. // InnerWindowBounds.Inset(WindowExtents) = OuterWindowBounds. // Returns false if the window manager does not provide extents information. COMPONENT_EXPORT(UI_BASE_X) -bool GetWindowExtents(XID window, gfx::Insets* extents); +bool GetWindowExtents(x11::Window window, gfx::Insets* extents); // Returns the outer bounds of |window| (including the non-client area). COMPONENT_EXPORT(UI_BASE_X) -bool GetOuterWindowBounds(XID window, gfx::Rect* rect); +bool GetOuterWindowBounds(x11::Window window, gfx::Rect* rect); // Returns true if |window| contains the point |screen_loc|. COMPONENT_EXPORT(UI_BASE_X) -bool WindowContainsPoint(XID window, gfx::Point screen_loc); +bool WindowContainsPoint(x11::Window window, gfx::Point screen_loc); // Return true if |window| has any property with |property_name|. COMPONENT_EXPORT(UI_BASE_X) -bool PropertyExists(XID window, const std::string& property_name); +bool PropertyExists(x11::Window window, const std::string& property_name); // Returns the raw bytes from a property with minimal // interpretation. |out_data| should be freed by XFree() after use. COMPONENT_EXPORT(UI_BASE_X) -bool GetRawBytesOfProperty(XID window, - XAtom property, - scoped_refptr<base::RefCountedMemory>* out_data, - size_t* out_data_items, - XAtom* out_type); +bool GetRawBytesOfProperty(x11::Window window, + x11::Atom property, + std::vector<uint8_t>* out_data, + x11::Atom* out_type); // Get the value of an int, int array, atom array or string property. On // success, true is returned and the value is stored in |value|. // -// TODO(erg): Once we remove the gtk port and are 100% aura, all of these -// should accept an XAtom instead of a string. -COMPONENT_EXPORT(UI_BASE_X) -bool GetIntProperty(XID window, const std::string& property_name, int* value); +// These functions should no longer be used. TODO(thomasanderson): migrate +// existing callers to {Set,Get}{,Array}Property<> instead. COMPONENT_EXPORT(UI_BASE_X) -bool GetXIDProperty(XID window, const std::string& property_name, XID* value); +bool GetIntProperty(x11::Window window, + const std::string& property_name, + int32_t* value); COMPONENT_EXPORT(UI_BASE_X) -bool GetIntArrayProperty(XID window, +bool GetIntArrayProperty(x11::Window window, const std::string& property_name, - std::vector<int>* value); + std::vector<int32_t>* value); COMPONENT_EXPORT(UI_BASE_X) -bool GetAtomArrayProperty(XID window, +bool GetAtomArrayProperty(x11::Window window, const std::string& property_name, - std::vector<XAtom>* value); + std::vector<x11::Atom>* value); COMPONENT_EXPORT(UI_BASE_X) -bool GetStringProperty(XID window, +bool GetStringProperty(x11::Window window, const std::string& property_name, std::string* value); -// These setters all make round trips. COMPONENT_EXPORT(UI_BASE_X) -bool SetIntProperty(XID window, +void SetIntProperty(x11::Window window, const std::string& name, const std::string& type, - int value); + int32_t value); COMPONENT_EXPORT(UI_BASE_X) -bool SetIntArrayProperty(XID window, +void SetIntArrayProperty(x11::Window window, const std::string& name, const std::string& type, - const std::vector<int>& value); + const std::vector<int32_t>& value); COMPONENT_EXPORT(UI_BASE_X) -bool SetAtomProperty(XID window, +void SetAtomProperty(x11::Window window, const std::string& name, const std::string& type, - XAtom value); + x11::Atom value); COMPONENT_EXPORT(UI_BASE_X) -bool SetAtomArrayProperty(XID window, +void SetAtomArrayProperty(x11::Window window, const std::string& name, const std::string& type, - const std::vector<XAtom>& value); + const std::vector<x11::Atom>& value); COMPONENT_EXPORT(UI_BASE_X) -bool SetStringProperty(XID window, - XAtom property, - XAtom type, +void SetStringProperty(x11::Window window, + x11::Atom property, + x11::Atom type, const std::string& value); // Sets the WM_CLASS attribute for a given X11 window. COMPONENT_EXPORT(UI_BASE_X) -void SetWindowClassHint(XDisplay* display, - XID window, +void SetWindowClassHint(x11::Connection* connection, + x11::Window window, const std::string& res_name, const std::string& res_class); // Sets the WM_WINDOW_ROLE attribute for a given X11 window. COMPONENT_EXPORT(UI_BASE_X) -void SetWindowRole(XDisplay* display, XID window, const std::string& role); +void SetWindowRole(x11::Window window, const std::string& role); // Sends a message to the x11 window manager, enabling or disabling the // states |state1| and |state2|. COMPONENT_EXPORT(UI_BASE_X) -void SetWMSpecState(XID window, bool enabled, XAtom state1, XAtom state2); +void SetWMSpecState(x11::Window window, + bool enabled, + x11::Atom state1, + x11::Atom state2); // Sends a NET_WM_MOVERESIZE message to the x11 window manager, enabling the // move/resize mode. As per NET_WM_MOVERESIZE spec, |location| is the position @@ -218,15 +412,16 @@ void SetWMSpecState(XID window, bool enabled, XAtom state1, XAtom state2); // |direction| indicates whether this is a move or resize event, and if it is a // resize event, which edges of the window the size grip applies to. COMPONENT_EXPORT(UI_BASE_X) -void DoWMMoveResize(XDisplay* display, - XID root_window, - XID window, +void DoWMMoveResize(x11::Connection* connection, + x11::Window root_window, + x11::Window window, const gfx::Point& location_px, int direction); // Checks if the window manager has set a specific state. COMPONENT_EXPORT(UI_BASE_X) -bool HasWMSpecProperty(const base::flat_set<XAtom>& properties, XAtom atom); +bool HasWMSpecProperty(const base::flat_set<x11::Atom>& properties, + x11::Atom atom); // Determine whether we should default to native decorations or the custom // frame based on the currently-running window manager. @@ -235,7 +430,8 @@ COMPONENT_EXPORT(UI_BASE_X) bool GetCustomFramePrefDefault(); static const int kAllDesktops = -1; // Queries the desktop |window| is on, kAllDesktops if sticky. Returns false if // property not found. -COMPONENT_EXPORT(UI_BASE_X) bool GetWindowDesktop(XID window, int* desktop); +COMPONENT_EXPORT(UI_BASE_X) +bool GetWindowDesktop(x11::Window window, int* desktop); // Translates an X11 error code into a printable string. COMPONENT_EXPORT(UI_BASE_X) @@ -245,12 +441,12 @@ std::string GetX11ErrorString(XDisplay* display, int err); // the main display. class EnumerateWindowsDelegate { public: - // |xid| is the X Window ID of the enumerated window. Return true to stop + // |window| is the X Window ID of the enumerated window. Return true to stop // further iteration. - virtual bool ShouldStopIterating(XID xid) = 0; + virtual bool ShouldStopIterating(x11::Window window) = 0; protected: - virtual ~EnumerateWindowsDelegate() {} + virtual ~EnumerateWindowsDelegate() = default; }; // Enumerates all windows in the current display. Will recurse into child @@ -265,7 +461,7 @@ void EnumerateTopLevelWindows(ui::EnumerateWindowsDelegate* delegate); // Returns all children windows of a given window in top-to-bottom stacking // order. COMPONENT_EXPORT(UI_BASE_X) -bool GetXWindowStack(XID window, std::vector<XID>* windows); +bool GetXWindowStack(x11::Window window, std::vector<x11::Window>* windows); enum WindowManagerName { WM_OTHER, // We were able to obtain the WM's name, but there is @@ -316,10 +512,10 @@ COMPONENT_EXPORT(UI_BASE_X) bool IsCompositingManagerPresent(); COMPONENT_EXPORT(UI_BASE_X) void SetDefaultX11ErrorHandlers(); // Returns true if a given window is in full-screen mode. -COMPONENT_EXPORT(UI_BASE_X) bool IsX11WindowFullScreen(XID window); +COMPONENT_EXPORT(UI_BASE_X) bool IsX11WindowFullScreen(x11::Window window); // Returns true if the window manager supports the given hint. -COMPONENT_EXPORT(UI_BASE_X) bool WmSupportsHint(XAtom atom); +COMPONENT_EXPORT(UI_BASE_X) bool WmSupportsHint(x11::Atom atom); // Returns the ICCProfile corresponding to |monitor| using XGetWindowProperty. COMPONENT_EXPORT(UI_BASE_X) @@ -333,6 +529,15 @@ COMPONENT_EXPORT(UI_BASE_X) bool IsSyncExtensionAvailable(); COMPONENT_EXPORT(UI_BASE_X) SkColorType ColorTypeForVisual(void* visual); +COMPONENT_EXPORT(UI_BASE_X) +x11::Future<void> SendClientMessage( + x11::Window window, + x11::Window target, + x11::Atom type, + const std::array<uint32_t, 5> data, + x11::EventMask event_mask = x11::EventMask::SubstructureNotify | + x11::EventMask::SubstructureRedirect); + // Manages a piece of X11 allocated memory as a RefCountedMemory segment. This // object takes ownership over the passed in memory and will free it with the // X11 allocator when done. |