FreeRDP
sdl_clip.hpp
1 
21 #pragma once
22 
23 #include <utility>
24 #include <vector>
25 #include <atomic>
26 #include <queue>
27 #include <map>
28 
29 #include <winpr/wtypes.h>
30 #include <freerdp/freerdp.h>
31 #include <freerdp/client/client_cliprdr_file.h>
32 #include <SDL3/SDL.h>
33 
34 #include "sdl_types.hpp"
35 #include "sdl_utils.hpp"
36 
39 {
40  public:
41  ClipRequest(UINT32 format, const std::string& mime);
42  ClipRequest(const ClipRequest& other) = default;
43  ClipRequest(ClipRequest&& other) = default;
44  ~ClipRequest() = default;
45 
46  ClipRequest& operator=(const ClipRequest& other) = delete;
47  ClipRequest& operator=(ClipRequest&& other) = delete;
48 
49  [[nodiscard]] uint32_t format() const;
50  [[nodiscard]] std::string formatstr() const;
51  [[nodiscard]] std::string mime() const;
52  [[nodiscard]] bool success() const;
53  void setSuccess(bool status);
54 
55  private:
56  uint32_t _format;
57  std::string _mime;
58  bool _success;
59 };
60 
62 {
63  public:
64  CliprdrFormat(uint32_t formatID, const char* formatName) : _formatID(formatID)
65  {
66  if (formatName)
67  _formatName = formatName;
68  }
69 
70  [[nodiscard]] uint32_t formatId() const
71  {
72  return _formatID;
73  }
74 
75  [[nodiscard]] const char* formatName() const
76  {
77  if (_formatName.empty())
78  return nullptr;
79  return _formatName.c_str();
80  }
81 
82  private:
83  uint32_t _formatID;
84  std::string _formatName;
85 };
86 
88 class sdlClip
89 {
90  public:
91  explicit sdlClip(SdlContext* sdl);
92  virtual ~sdlClip();
93 
94  BOOL init(CliprdrClientContext* clip);
95  BOOL uninit(CliprdrClientContext* clip);
96 
97  bool handle_update(const SDL_ClipboardEvent& ev);
98 
99  private:
100  UINT SendClientCapabilities();
101  void clearServerFormats();
102  UINT SendFormatListResponse(BOOL status);
103  UINT SendDataResponse(const BYTE* data, size_t size);
104  UINT SendDataRequest(uint32_t formatID, const std::string& mime);
105 
106  std::string getServerFormat(uint32_t id);
107  uint32_t serverIdForMime(const std::string& mime);
108 
109  static UINT MonitorReady(CliprdrClientContext* context,
110  const CLIPRDR_MONITOR_READY* monitorReady);
111 
112  static UINT ReceiveServerCapabilities(CliprdrClientContext* context,
113  const CLIPRDR_CAPABILITIES* capabilities);
114  static UINT ReceiveServerFormatList(CliprdrClientContext* context,
115  const CLIPRDR_FORMAT_LIST* formatList);
116  static UINT ReceiveFormatListResponse(CliprdrClientContext* context,
117  const CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse);
118  static std::shared_ptr<BYTE> ReceiveFormatDataRequestHandle(
119  sdlClip* clipboard, const CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest, uint32_t& len);
120  static UINT ReceiveFormatDataRequest(CliprdrClientContext* context,
121  const CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest);
122  static UINT ReceiveFormatDataResponse(CliprdrClientContext* context,
123  const CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse);
124 
125  static const void* SDLCALL ClipDataCb(void* userdata, const char* mime_type, size_t* size);
126  static void SDLCALL ClipCleanCb(void* userdata);
127 
128  static bool mime_is_file(const std::string& mime);
129  static bool mime_is_text(const std::string& mime);
130  static bool mime_is_image(const std::string& mime);
131  static bool mime_is_html(const std::string& mime);
132 
133  SdlContext* _sdl = nullptr;
134  CliprdrFileContext* _file = nullptr;
135  CliprdrClientContext* _ctx = nullptr;
136  wLog* _log = nullptr;
137  wClipboard* _system = nullptr;
138  std::atomic<bool> _sync = false;
139  HANDLE _event;
140 
141  std::vector<CliprdrFormat> _serverFormats;
142  CriticalSection _lock;
143 
144  std::queue<ClipRequest> _request_queue;
145 
146  struct cache_entry
147  {
148  cache_entry(size_t len, std::shared_ptr<void> p) : size(len), ptr(std::move(p))
149  {
150  }
151 
152  size_t size;
153  std::shared_ptr<void> ptr;
154  };
155  std::map<std::string, cache_entry> _cache_data;
156 };
a clipboard format request
Definition: sdl_clip.hpp:39
object that handles clipboard context for the SDL3 client
Definition: sdl_clip.hpp:89