FreeRDP
Loading...
Searching...
No Matches
SDL3/sdl_pointer.cpp
1
19
20#include <algorithm>
21#include <cmath>
22
23#include <freerdp/config.h>
24
25#include <freerdp/gdi/gdi.h>
26
27#include "sdl_pointer.hpp"
28#include "sdl_context.hpp"
29#include "sdl_touch.hpp"
30#include "sdl_utils.hpp"
31
32#include <SDL3/SDL_mouse.h>
33
34struct sdlPointer
35{
36 rdpPointer pointer{};
37 SDL_Cursor* cursor = nullptr;
38 SDL_Surface* image = nullptr;
39 size_t size = 0;
40 BYTE* data = nullptr;
41
42 sdlPointer(const sdlPointer& other) = delete;
43 sdlPointer(sdlPointer&& other) = delete;
44 auto operator=(const sdlPointer& other) = delete;
45 auto operator=(sdlPointer&& other) = delete;
46 ~sdlPointer() = delete;
47
48 bool update(rdpContext* context)
49 {
50 assert(context);
51 assert(context->gdi);
52
53 size = 4ull * pointer.width * pointer.height;
54 winpr_aligned_free(data);
55 data = static_cast<BYTE*>(winpr_aligned_calloc(1, size, 16));
56
57 if (!data)
58 return false;
59
60 return freerdp_image_copy_from_pointer_data(
61 data, context->gdi->dstFormat, 0, 0, 0, pointer.width, pointer.height,
62 pointer.xorMaskData, pointer.lengthXorMask, pointer.andMaskData, pointer.lengthAndMask,
63 pointer.xorBpp, &context->gdi->palette);
64 }
65};
66
67[[nodiscard]] static BOOL sdl_Pointer_New(rdpContext* context, rdpPointer* pointer)
68{
69 auto ptr = reinterpret_cast<sdlPointer*>(pointer);
70 auto sdl = get_context(context);
71
72 WINPR_ASSERT(sdl);
73 std::unique_lock lock(sdl->lock());
74 if (!ptr)
75 return FALSE;
76 sdl->pointers().push_back(pointer);
77
78 return ptr->update(context);
79}
80
81static void sdl_Pointer_Clear(sdlPointer* ptr)
82{
83 WINPR_ASSERT(ptr);
84 SDL_DestroyCursor(ptr->cursor);
85 SDL_DestroySurface(ptr->image);
86 ptr->cursor = nullptr;
87 ptr->image = nullptr;
88}
89
90static void sdl_Pointer_Free(WINPR_ATTR_UNUSED rdpContext* context, rdpPointer* pointer)
91{
92 auto sdl = get_context(context);
93
94 WINPR_ASSERT(sdl);
95 std::unique_lock lock(sdl->lock());
96 auto it = std::remove(sdl->pointers().begin(), sdl->pointers().end(), pointer);
97 sdl->pointers().erase(it, sdl->pointers().end());
98
99 sdl_Pointer_FreeCopy(pointer);
100}
101
102void sdl_Pointer_FreeCopy(rdpPointer* pointer)
103{
104 auto ptr = reinterpret_cast<sdlPointer*>(pointer);
105
106 if (!ptr)
107 return;
108
109 sdl_Pointer_Clear(ptr);
110 winpr_aligned_free(ptr->data);
111 ptr->data = nullptr;
112}
113
114[[nodiscard]] static BOOL sdl_Pointer_SetDefault(rdpContext* context)
115{
116 WINPR_UNUSED(context);
117
118 return sdl_push_user_event(SDL_EVENT_USER_POINTER_DEFAULT);
119}
120
121[[nodiscard]] static BOOL sdl_Pointer_Set(rdpContext* context, rdpPointer* pointer)
122{
123 WINPR_UNUSED(context);
124 return sdl_push_user_event(SDL_EVENT_USER_POINTER_SET, pointer);
125}
126
127bool sdl_Pointer_Set_Process(SdlContext* sdl)
128{
129 WINPR_ASSERT(sdl);
130
131 std::unique_lock lock(sdl->lock());
132 auto context = sdl->context();
133 auto pointer = sdl->cursor();
134 auto ptr = reinterpret_cast<sdlPointer*>(pointer);
135 if (!ptr)
136 return true;
137
138 rdpGdi* gdi = context->gdi;
139 WINPR_ASSERT(gdi);
140
141 auto ix = static_cast<float>(pointer->xPos);
142 auto iy = static_cast<float>(pointer->yPos);
143 auto isw = static_cast<float>(pointer->width);
144 auto ish = static_cast<float>(pointer->height);
145
146 auto window = SDL_GetMouseFocus();
147 if (!window)
148 return sdl_Pointer_SetDefault(context);
149
150 const Uint32 id = SDL_GetWindowID(window);
151
152 const SDL_FRect orig{ ix, iy, isw, ish };
153 const auto pos = sdl->pixelToScreen(id, orig, true);
154 WLog_Print(sdl->getWLog(), WLOG_DEBUG, "cursor scale: pixel:%s, display:%s",
155 sdl::utils::toString(orig).c_str(), sdl::utils::toString(pos).c_str());
156
157 sdl_Pointer_Clear(ptr);
158
159 ptr->image =
160 SDL_CreateSurface(static_cast<int>(orig.w), static_cast<int>(orig.h), sdl->pixelFormat());
161 if (!ptr->image)
162 {
163 WLog_Print(sdl->getWLog(), WLOG_ERROR, "SDL_CreateSurface failed");
164 return false;
165 }
166
167 auto data = static_cast<const BYTE*>(ptr->data);
168 if (data)
169 {
170 if (!SDL_LockSurface(ptr->image))
171 {
172 WLog_Print(sdl->getWLog(), WLOG_ERROR, "SDL_LockSurface failed");
173 return false;
174 }
175
176 auto pixels = static_cast<BYTE*>(ptr->image->pixels);
177 const BOOL rc = freerdp_image_scale(
178 pixels, gdi->dstFormat, static_cast<UINT32>(ptr->image->pitch), 0, 0,
179 static_cast<UINT32>(ptr->image->w), static_cast<UINT32>(ptr->image->h), data,
180 gdi->dstFormat, 0, 0, 0, static_cast<UINT32>(isw), static_cast<UINT32>(ish));
181 SDL_UnlockSurface(ptr->image);
182 if (!rc)
183 {
184 WLog_Print(sdl->getWLog(), WLOG_ERROR, "freerdp_image_scale failed");
185 return false;
186 }
187 }
188
189 // create a cursor image in 100% display scale to trick SDL into creating the cursor with the
190 // correct size
191 auto fw = sdl->getFirstWindow();
192 if (!fw)
193 {
194 WLog_Print(sdl->getWLog(), WLOG_ERROR, "sdl->getFirstWindow() nullptr");
195 return false;
196 }
197
198 /* SDL cursor backends do not all interpret the surface size the same way: the Windows
199 * backend multiplies it by SDL_GetDisplayContentScale() when
200 * SDL_HINT_MOUSE_DPI_SCALE_CURSORS is enabled (we enable it in sdl_freerdp.cpp), while the
201 * Wayland, X11 and Cocoa backends use it as is. The code above assumes the window pixel
202 * density and the display content scale are equal, which does not hold on Windows: window
203 * sizes are tracked in physical pixels there, so SDL reports a pixel density of 1 on a 200%
204 * display while the display content scale is 2, and the Windows backend scales the cursor
205 * bitmap up a second time, displaying it twice as large as the rest of the session.
206 * Compensate for that backends own scaling so the final cursor still covers
207 * orig * (pos / orig) points, in line with the remote desktop. */
208 float surfaceRatio = 1.0f;
209 float displayScale = 1.0f;
210 if (sdl::utils::isWindowsDriver())
211 {
212 displayScale = SDL_GetDisplayContentScale(SDL_GetDisplayForWindow(window));
213 const float pixelDensity = SDL_GetWindowPixelDensity(window);
214 if ((displayScale > 0.0f) && (pixelDensity > 0.0f))
215 surfaceRatio = pixelDensity / displayScale;
216 }
217 if (std::fabs(surfaceRatio - 1.0f) > 0.01f)
218 WLog_Print(sdl->getWLog(), WLOG_DEBUG,
219 "cursor surface scaled by %.2f (driver %s, display scale %.2f)",
220 static_cast<double>(surfaceRatio), SDL_GetCurrentVideoDriver(),
221 static_cast<double>(displayScale));
222
223 const auto w = static_cast<int>(std::lround(pos.w * surfaceRatio));
224 const auto h = static_cast<int>(std::lround(pos.h * surfaceRatio));
225 if ((w <= 0) || (h <= 0))
226 return true;
227
228 std::unique_ptr<SDL_Surface, void (*)(SDL_Surface*)> normal{
229 SDL_CreateSurface(w, h, ptr->image->format), SDL_DestroySurface
230 };
231 if (!normal)
232 return true;
233 if (!SDL_BlitSurfaceScaled(ptr->image, nullptr, normal.get(), nullptr,
234 SDL_ScaleMode::SDL_SCALEMODE_LINEAR))
235 {
236 WLog_Print(sdl->getWLog(), WLOG_ERROR, "SDL_BlitSurfaceScaled failed");
237 return false;
238 }
239
240 /* The alternate image is the full resolution cursor. SDL backends that honour
241 * SDL_HINT_MOUSE_DPI_SCALE_CURSORS pick it for high DPI displays and look up the hotspot of
242 * the image they picked, so it needs one of its own: the hotspot of the scaled down copy
243 * does not apply to an image that is `display scale` times larger. Without this the Windows
244 * backend builds a cursor bitmap that is displayed shifted right and down by
245 * (hotspot * (1 - 1 / scale)), which is invisible for cursors anchored in the top left
246 * corner (e.g. the arrow) but clearly wrong for the ones anchored in the middle (e.g. the
247 * Excel cell cursor). */
248 {
249 const auto props = SDL_GetSurfaceProperties(ptr->image);
250 const auto hx = static_cast<int>(ix);
251 const auto hy = static_cast<int>(iy);
252 if (!SDL_SetNumberProperty(props, SDL_PROP_SURFACE_HOTSPOT_X_NUMBER, hx) ||
253 !SDL_SetNumberProperty(props, SDL_PROP_SURFACE_HOTSPOT_Y_NUMBER, hy))
254 WLog_Print(sdl->getWLog(), WLOG_WARN, "SDL_SetNumberProperty(hotspot %d,%d) failed", hx,
255 hy);
256 }
257
258 if (!SDL_AddSurfaceAlternateImage(normal.get(), ptr->image))
259 {
260 WLog_Print(sdl->getWLog(), WLOG_ERROR, "SDL_AddSurfaceAlternateImage failed");
261 return false;
262 }
263
264 auto x = static_cast<int>(std::lround(pos.x * surfaceRatio));
265 auto y = static_cast<int>(std::lround(pos.y * surfaceRatio));
266 if (x >= w)
267 x = w - 1;
268 if (y >= h)
269 y = h - 1;
270
271 ptr->cursor = SDL_CreateColorCursor(normal.get(), x, y);
272 if (!ptr->cursor)
273 {
274 WLog_Print(sdl->getWLog(), WLOG_ERROR, "SDL_CreateColorCursor(display:%s, pixel:%s} failed",
275 sdl::utils::toString(pos).c_str(), sdl::utils::toString(orig).c_str());
276 return false;
277 }
278
279 if (!SDL_SetCursor(ptr->cursor))
280 {
281 WLog_Print(sdl->getWLog(), WLOG_ERROR, "SDL_SetCursor failed");
282 return false;
283 }
284 if (!SDL_ShowCursor())
285 {
286 WLog_Print(sdl->getWLog(), WLOG_ERROR, "SDL_ShowCursor failed");
287 return false;
288 }
289 sdl->setHasCursor(true);
290 return true;
291}
292
293[[nodiscard]] static BOOL sdl_Pointer_SetNull(rdpContext* context)
294{
295 WINPR_UNUSED(context);
296
297 return sdl_push_user_event(SDL_EVENT_USER_POINTER_NULL);
298}
299
300[[nodiscard]] static BOOL sdl_Pointer_SetPosition(rdpContext* context, UINT32 x, UINT32 y)
301{
302 WINPR_UNUSED(context);
303 WINPR_ASSERT(context);
304
305 return sdl_push_user_event(SDL_EVENT_USER_POINTER_POSITION, x, y);
306}
307
308bool sdl_register_pointer(rdpGraphics* graphics)
309{
310 const rdpPointer pointer = { sizeof(sdlPointer),
311 sdl_Pointer_New,
312 sdl_Pointer_Free,
313 sdl_Pointer_Set,
314 sdl_Pointer_SetNull,
315 sdl_Pointer_SetDefault,
316 sdl_Pointer_SetPosition,
317 {},
318 0,
319 0,
320 0,
321 0,
322 0,
323 0,
324 0,
325 nullptr,
326 nullptr,
327 {} };
328 graphics_register_pointer(graphics, &pointer);
329 return true;
330}
331
332rdpPointer* sdl_Pointer_Copy(const rdpPointer* pointer)
333{
334 auto ptr = reinterpret_cast<const sdlPointer*>(pointer);
335 if (!pointer)
336 return nullptr;
337
338 auto copy = static_cast<sdlPointer*>(calloc(1, sizeof(sdlPointer)));
339 if (!copy)
340 return nullptr;
341
342 copy->pointer.xPos = pointer->xPos;
343 copy->pointer.yPos = pointer->yPos;
344 copy->pointer.width = pointer->width;
345 copy->pointer.height = pointer->height;
346 copy->pointer.xorBpp = pointer->xorBpp;
347 if (ptr->size > 0)
348 {
349 copy->data = static_cast<BYTE*>(winpr_aligned_calloc(1, ptr->size, 32));
350 if (!copy->data)
351 {
352 free(copy);
353 return nullptr;
354 }
355 copy->size = ptr->size;
356 memcpy(copy->data, ptr->data, copy->size);
357 }
358 return &copy->pointer;
359}
360
361void sdl_PointerFreeCopyAll(rdpPointer* pointer)
362{
363 sdl_Pointer_FreeCopy(pointer);
364 free(pointer);
365}