File: x11_cursor_loader.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rw-r--r-- 3,080 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_BASE_X_X11_CURSOR_LOADER_H_
#define UI_BASE_X_X11_CURSOR_LOADER_H_

#include <string_view>

#include "base/component_export.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/x/x11_cursor.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/x/property_cache.h"
#include "ui/gfx/x/render.h"
#include "ui/gfx/x/xproto.h"

namespace ui {

// This is a port of libxcursor.
class COMPONENT_EXPORT(UI_BASE_X) XCursorLoader {
 public:
  struct Image {
    SkBitmap bitmap;
    gfx::Point hotspot;
    base::TimeDelta frame_delay;
  };

  XCursorLoader(x11::Connection* connection,
                base::RepeatingClosure on_cursor_config_changed);

  ~XCursorLoader();

  // Loads a system cursor for the given |names|. The cursor is loaded
  // asynchronously, but some cursor (possibly a fallback) is always guaranteed
  // to be loaded.
  scoped_refptr<X11Cursor> LoadCursor(const std::vector<std::string>& names);

  // Synchronously loads a cursor for the given |bitmap| or |images|. nullptr
  // may be returned if image cursors are not supported.
  scoped_refptr<X11Cursor> CreateCursor(const std::vector<Image>& images);
  scoped_refptr<X11Cursor> CreateCursor(const SkBitmap& bitmap,
                                        const gfx::Point& hotspot);

 private:
  friend class XCursorLoaderTest;

  void LoadCursorImpl(scoped_refptr<X11Cursor> cursor,
                      const std::vector<std::string>& names,
                      const std::vector<XCursorLoader::Image>& images);

  uint32_t GetPreferredCursorSize() const;

  // Populate the |rm_*| variables from the value of the RESOURCE_MANAGER
  // property on the root window.
  void ParseXResources(std::string_view resources);

  uint16_t CursorNamesToChar(const std::vector<std::string>& names) const;

  bool SupportsCreateCursor() const;
  bool SupportsCreateAnimCursor() const;

  void OnPropertyChanged(x11::Atom property,
                         const x11::GetPropertyResponse& value);

  raw_ptr<x11::Connection> connection_ = nullptr;

  base::RepeatingClosure on_cursor_config_changed_;

  x11::Font cursor_font_ = x11::Font::None;

  x11::Render::PictFormat pict_format_{};

  x11::PropertyCache rm_cache_;
  // Values obtained from the RESOURCE_MANAGER property on the root window.
  std::string rm_xcursor_theme_;
  unsigned int rm_xcursor_size_ = 0;
  unsigned int rm_xft_dpi_ = 0;

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<XCursorLoader> weak_factory_{this};
};

COMPONENT_EXPORT(UI_BASE_X)
std::vector<XCursorLoader::Image> ParseCursorFile(
    scoped_refptr<base::RefCountedMemory> file,
    uint32_t preferred_size);

}  // namespace ui

#endif  // UI_BASE_X_X11_CURSOR_LOADER_H_