x11_cursor_loader.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2020 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_CURSOR_LOADER_H_
  5. #define UI_BASE_X_X11_CURSOR_LOADER_H_
  6. #include <unordered_map>
  7. #include "base/component_export.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted_memory.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/time/time.h"
  13. #include "base/version.h"
  14. #include "third_party/skia/include/core/SkBitmap.h"
  15. #include "ui/base/x/x11_cursor.h"
  16. #include "ui/gfx/geometry/point.h"
  17. #include "ui/gfx/x/render.h"
  18. #include "ui/gfx/x/xproto.h"
  19. namespace ui {
  20. // This is a port of libxcursor.
  21. class COMPONENT_EXPORT(UI_BASE_X) XCursorLoader {
  22. public:
  23. struct Image {
  24. SkBitmap bitmap;
  25. gfx::Point hotspot;
  26. base::TimeDelta frame_delay;
  27. };
  28. explicit XCursorLoader(x11::Connection* connection);
  29. ~XCursorLoader();
  30. // Loads a system cursor for the given |names|. The cursor is loaded
  31. // asynchronously, but some cursor (possibly a fallback) is always guaranteed
  32. // to be loaded.
  33. scoped_refptr<X11Cursor> LoadCursor(const std::vector<std::string>& names);
  34. // Synchronously loads a cursor for the given |bitmap| or |images|. nullptr
  35. // may be returned if image cursors are not supported.
  36. scoped_refptr<X11Cursor> CreateCursor(const std::vector<Image>& images);
  37. scoped_refptr<X11Cursor> CreateCursor(const SkBitmap& bitmap,
  38. const gfx::Point& hotspot);
  39. private:
  40. friend class XCursorLoaderTest;
  41. void LoadCursorImpl(scoped_refptr<X11Cursor> cursor,
  42. const std::vector<std::string>& names,
  43. const std::vector<XCursorLoader::Image>& images);
  44. uint32_t GetPreferredCursorSize() const;
  45. // Populate the |rm_*| variables from the value of the RESOURCE_MANAGER
  46. // property on the root window.
  47. void ParseXResources(base::StringPiece resources);
  48. uint16_t CursorNamesToChar(const std::vector<std::string>& names) const;
  49. bool SupportsCreateCursor() const;
  50. bool SupportsCreateAnimCursor() const;
  51. raw_ptr<x11::Connection> connection_ = nullptr;
  52. x11::Font cursor_font_ = x11::Font::None;
  53. x11::Render::PictFormat pict_format_{};
  54. // Values obtained from the RESOURCE_MANAGER property on the root window.
  55. std::string rm_xcursor_theme_;
  56. unsigned int rm_xcursor_size_ = 0;
  57. unsigned int rm_xft_dpi_ = 0;
  58. base::Version render_version_;
  59. std::unordered_map<std::string, uint16_t> cursor_name_to_char_;
  60. SEQUENCE_CHECKER(sequence_checker_);
  61. base::WeakPtrFactory<XCursorLoader> weak_factory_{this};
  62. };
  63. COMPONENT_EXPORT(UI_BASE_X)
  64. std::vector<XCursorLoader::Image> ParseCursorFile(
  65. scoped_refptr<base::RefCountedMemory> file,
  66. uint32_t preferred_size);
  67. } // namespace ui
  68. #endif // UI_BASE_X_X11_CURSOR_LOADER_H_