FreeRDP
SDL3/sdl_pointer.cpp
1 
20 #include <freerdp/config.h>
21 
22 #include <freerdp/gdi/gdi.h>
23 
24 #include "sdl_pointer.hpp"
25 #include "sdl_freerdp.hpp"
26 #include "sdl_touch.hpp"
27 #include "sdl_utils.hpp"
28 
29 #include <SDL3/SDL_mouse.h>
30 
31 typedef struct
32 {
33  rdpPointer pointer;
34  SDL_Cursor* cursor;
35  SDL_Surface* image;
36  size_t size;
37  void* data;
38 } sdlPointer;
39 
40 static BOOL sdl_Pointer_New(rdpContext* context, rdpPointer* pointer)
41 {
42  auto ptr = reinterpret_cast<sdlPointer*>(pointer);
43 
44  WINPR_ASSERT(context);
45  if (!ptr)
46  return FALSE;
47 
48  rdpGdi* gdi = context->gdi;
49  WINPR_ASSERT(gdi);
50 
51  ptr->size = 4ull * pointer->width * pointer->height;
52  ptr->data = winpr_aligned_malloc(ptr->size, 16);
53 
54  if (!ptr->data)
55  return FALSE;
56 
57  auto data = static_cast<BYTE*>(ptr->data);
58  if (!freerdp_image_copy_from_pointer_data(
59  data, gdi->dstFormat, 0, 0, 0, pointer->width, pointer->height, pointer->xorMaskData,
60  pointer->lengthXorMask, pointer->andMaskData, pointer->lengthAndMask, pointer->xorBpp,
61  &context->gdi->palette))
62  {
63  winpr_aligned_free(ptr->data);
64  return FALSE;
65  }
66 
67  return TRUE;
68 }
69 
70 static void sdl_Pointer_Clear(sdlPointer* ptr)
71 {
72  WINPR_ASSERT(ptr);
73  SDL_DestroyCursor(ptr->cursor);
74  SDL_DestroySurface(ptr->image);
75  ptr->cursor = nullptr;
76  ptr->image = nullptr;
77 }
78 
79 static void sdl_Pointer_Free(rdpContext* context, rdpPointer* pointer)
80 {
81  auto ptr = reinterpret_cast<sdlPointer*>(pointer);
82  WINPR_UNUSED(context);
83 
84  if (ptr)
85  {
86  sdl_Pointer_Clear(ptr);
87  winpr_aligned_free(ptr->data);
88  ptr->data = nullptr;
89  }
90 }
91 
92 static BOOL sdl_Pointer_SetDefault(rdpContext* context)
93 {
94  WINPR_UNUSED(context);
95 
96  return sdl_push_user_event(SDL_EVENT_USER_POINTER_DEFAULT);
97 }
98 
99 static BOOL sdl_Pointer_Set(rdpContext* context, rdpPointer* pointer)
100 {
101  auto sdl = get_context(context);
102 
103  return sdl_push_user_event(SDL_EVENT_USER_POINTER_SET, pointer, sdl);
104 }
105 
106 BOOL sdl_Pointer_Set_Process(SDL_UserEvent* uptr)
107 {
108  INT32 w = 0;
109  INT32 h = 0;
110  INT32 x = 0;
111  INT32 y = 0;
112  INT32 sw = 0;
113  INT32 sh = 0;
114 
115  WINPR_ASSERT(uptr);
116 
117  auto sdl = static_cast<SdlContext*>(uptr->data2);
118  WINPR_ASSERT(sdl);
119 
120  auto context = sdl->context();
121  auto ptr = static_cast<sdlPointer*>(uptr->data1);
122  WINPR_ASSERT(ptr);
123 
124  rdpPointer* pointer = &ptr->pointer;
125 
126  rdpGdi* gdi = context->gdi;
127  WINPR_ASSERT(gdi);
128 
129  x = static_cast<INT32>(pointer->xPos);
130  y = static_cast<INT32>(pointer->yPos);
131  sw = w = static_cast<INT32>(pointer->width);
132  sh = h = static_cast<INT32>(pointer->height);
133 
134  SDL_Window* window = SDL_GetMouseFocus();
135  if (!window)
136  return sdl_Pointer_SetDefault(context);
137 
138  const Uint32 id = SDL_GetWindowID(window);
139 
140  if (!sdl_scale_coordinates(sdl, id, &x, &y, FALSE, FALSE) ||
141  !sdl_scale_coordinates(sdl, id, &sw, &sh, FALSE, FALSE))
142  return FALSE;
143 
144  sdl_Pointer_Clear(ptr);
145 
146  ptr->image = SDL_CreateSurface(sw, sh, sdl->sdl_pixel_format);
147  if (!ptr->image)
148  return FALSE;
149 
150  SDL_LockSurface(ptr->image);
151  auto pixels = static_cast<BYTE*>(ptr->image->pixels);
152  auto data = static_cast<const BYTE*>(ptr->data);
153  const BOOL rc = freerdp_image_scale(
154  pixels, gdi->dstFormat, static_cast<UINT32>(ptr->image->pitch), 0, 0,
155  static_cast<UINT32>(ptr->image->w), static_cast<UINT32>(ptr->image->h), data,
156  gdi->dstFormat, 0, 0, 0, static_cast<UINT32>(w), static_cast<UINT32>(h));
157  SDL_UnlockSurface(ptr->image);
158  if (!rc)
159  return FALSE;
160 
161  ptr->cursor = SDL_CreateColorCursor(ptr->image, x, y);
162  if (!ptr->cursor)
163  return FALSE;
164 
165  SDL_SetCursor(ptr->cursor);
166  SDL_ShowCursor();
167  return TRUE;
168 }
169 
170 static BOOL sdl_Pointer_SetNull(rdpContext* context)
171 {
172  WINPR_UNUSED(context);
173 
174  return sdl_push_user_event(SDL_EVENT_USER_POINTER_NULL);
175 }
176 
177 static BOOL sdl_Pointer_SetPosition(rdpContext* context, UINT32 x, UINT32 y)
178 {
179  WINPR_UNUSED(context);
180  WINPR_ASSERT(context);
181 
182  return sdl_push_user_event(SDL_EVENT_USER_POINTER_POSITION, x, y);
183 }
184 
185 BOOL sdl_register_pointer(rdpGraphics* graphics)
186 {
187  const rdpPointer pointer = { sizeof(sdlPointer),
188  sdl_Pointer_New,
189  sdl_Pointer_Free,
190  sdl_Pointer_Set,
191  sdl_Pointer_SetNull,
192  sdl_Pointer_SetDefault,
193  sdl_Pointer_SetPosition,
194  {},
195  0,
196  0,
197  0,
198  0,
199  0,
200  0,
201  0,
202  nullptr,
203  nullptr,
204  {} };
205  graphics_register_pointer(graphics, &pointer);
206  return TRUE;
207 }