FreeRDP
SDL2/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 <SDL_mouse.h>
30 
31 using sdlPointer = struct
32 {
33  rdpPointer pointer;
34  SDL_Cursor* cursor;
35  SDL_Surface* image;
36  size_t size;
37  void* data;
38 };
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_FreeCursor(ptr->cursor);
74  SDL_FreeSurface(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_USEREVENT_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_USEREVENT_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  const DWORD bpp = FreeRDPGetBitsPerPixel(gdi->dstFormat);
147  ptr->image =
148  SDL_CreateRGBSurfaceWithFormat(0, sw, sh, static_cast<int>(bpp), sdl->sdl_pixel_format);
149  if (!ptr->image)
150  return FALSE;
151 
152  SDL_LockSurface(ptr->image);
153  auto pixels = static_cast<BYTE*>(ptr->image->pixels);
154  auto data = static_cast<const BYTE*>(ptr->data);
155  const BOOL rc = freerdp_image_scale(
156  pixels, gdi->dstFormat, static_cast<UINT32>(ptr->image->pitch), 0, 0,
157  static_cast<UINT32>(ptr->image->w), static_cast<UINT32>(ptr->image->h), data,
158  gdi->dstFormat, 0, 0, 0, static_cast<UINT32>(w), static_cast<UINT32>(h));
159  SDL_UnlockSurface(ptr->image);
160  if (!rc)
161  return FALSE;
162 
163  ptr->cursor = SDL_CreateColorCursor(ptr->image, x, y);
164  if (!ptr->cursor)
165  return FALSE;
166 
167  SDL_SetCursor(ptr->cursor);
168  SDL_ShowCursor(SDL_ENABLE);
169  return TRUE;
170 }
171 
172 static BOOL sdl_Pointer_SetNull(rdpContext* context)
173 {
174  WINPR_UNUSED(context);
175 
176  return sdl_push_user_event(SDL_USEREVENT_POINTER_NULL);
177 }
178 
179 static BOOL sdl_Pointer_SetPosition(rdpContext* context, UINT32 x, UINT32 y)
180 {
181  WINPR_UNUSED(context);
182  WINPR_ASSERT(context);
183 
184  return sdl_push_user_event(SDL_USEREVENT_POINTER_POSITION, x, y);
185 }
186 
187 BOOL sdl_register_pointer(rdpGraphics* graphics)
188 {
189  const rdpPointer pointer = { sizeof(sdlPointer),
190  sdl_Pointer_New,
191  sdl_Pointer_Free,
192  sdl_Pointer_Set,
193  sdl_Pointer_SetNull,
194  sdl_Pointer_SetDefault,
195  sdl_Pointer_SetPosition,
196  {},
197  0,
198  0,
199  0,
200  0,
201  0,
202  0,
203  0,
204  nullptr,
205  nullptr,
206  {} };
207  graphics_register_pointer(graphics, &pointer);
208  return TRUE;
209 }