FreeRDP
Loading...
Searching...
No Matches
SDL3/sdl_window.cpp
1
20#include <limits>
21#include <sstream>
22#include <cmath>
23
24#include "sdl_window.hpp"
25#include "sdl_utils.hpp"
26
27#include <freerdp/utils/string.h>
28
29SdlWindow::SdlWindow(SDL_DisplayID id, const std::string& title, const SDL_Rect& rect,
30 [[maybe_unused]] Uint32 flags)
31 : _displayID(id)
32{
33 auto props = SDL_CreateProperties();
34 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title.c_str());
35 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, rect.x);
36 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, rect.y);
37 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, rect.w);
38 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, rect.h);
39
40 if (flags & SDL_WINDOW_HIGH_PIXEL_DENSITY)
41 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
42
43 if (flags & SDL_WINDOW_FULLSCREEN)
44 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN, true);
45
46 if (flags & SDL_WINDOW_BORDERLESS)
47 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN, true);
48
49 _window = SDL_CreateWindowWithProperties(props);
50 SDL_DestroyProperties(props);
51
52 auto sc = scale();
53 const int iscale = static_cast<int>(sc * 100.0f);
54 auto w = 100 * rect.w / iscale;
55 auto h = 100 * rect.h / iscale;
56 std::ignore = resize({ w, h });
57 SDL_SetHint(SDL_HINT_APP_NAME, "");
58 std::ignore = SDL_SyncWindow(_window);
59
60 _monitor = query(_window, id, true);
61}
62
63SdlWindow::SdlWindow(SdlWindow&& other) noexcept
64 : _window(other._window), _displayID(other._displayID), _offset_x(other._offset_x),
65 _offset_y(other._offset_y), _monitor(other._monitor)
66{
67 other._window = nullptr;
68}
69
70SdlWindow::~SdlWindow()
71{
72 SDL_DestroyWindow(_window);
73}
74
75SDL_WindowID SdlWindow::id() const
76{
77 if (!_window)
78 return 0;
79 return SDL_GetWindowID(_window);
80}
81
82SDL_DisplayID SdlWindow::displayIndex() const
83{
84 if (!_window)
85 return 0;
86 return SDL_GetDisplayForWindow(_window);
87}
88
89SDL_Rect SdlWindow::rect() const
90{
91 return rect(_window);
92}
93
94SDL_Rect SdlWindow::bounds() const
95{
96 SDL_Rect rect = {};
97 if (_window)
98 {
99 if (!SDL_GetWindowPosition(_window, &rect.x, &rect.y))
100 return {};
101 if (!SDL_GetWindowSize(_window, &rect.w, &rect.h))
102 return {};
103 }
104 return rect;
105}
106
107SDL_Window* SdlWindow::window() const
108{
109 return _window;
110}
111
112Sint32 SdlWindow::offsetX() const
113{
114 return _offset_x;
115}
116
117void SdlWindow::setOffsetX(Sint32 x)
118{
119 _offset_x = x;
120}
121
122void SdlWindow::setOffsetY(Sint32 y)
123{
124 _offset_y = y;
125}
126
127Sint32 SdlWindow::offsetY() const
128{
129 return _offset_y;
130}
131
132rdpMonitor SdlWindow::monitor(bool isPrimary) const
133{
134 auto m = _monitor;
135 if (isPrimary)
136 {
137 m.x = 0;
138 m.y = 0;
139 }
140 return m;
141}
142
143void SdlWindow::setMonitor(rdpMonitor monitor)
144{
145 _monitor = monitor;
146}
147
148float SdlWindow::scale() const
149{
150 return SDL_GetWindowDisplayScale(_window);
151}
152
153SDL_DisplayOrientation SdlWindow::orientation() const
154{
155 const auto did = displayIndex();
156 return SDL_GetCurrentDisplayOrientation(did);
157}
158
159bool SdlWindow::grabKeyboard(bool enable)
160{
161 if (!_window)
162 return false;
163 SDL_SetWindowKeyboardGrab(_window, enable);
164 return true;
165}
166
167bool SdlWindow::grabMouse(bool enable)
168{
169 if (!_window)
170 return false;
171 SDL_SetWindowMouseGrab(_window, enable);
172 return true;
173}
174
175void SdlWindow::setBordered(bool bordered)
176{
177 if (_window)
178 SDL_SetWindowBordered(_window, bordered);
179 std::ignore = SDL_SyncWindow(_window);
180}
181
182void SdlWindow::raise()
183{
184 SDL_RaiseWindow(_window);
185 std::ignore = SDL_SyncWindow(_window);
186}
187
188void SdlWindow::resizeable(bool use)
189{
190 SDL_SetWindowResizable(_window, use);
191 std::ignore = SDL_SyncWindow(_window);
192}
193
194void SdlWindow::fullscreen(bool enter, bool forceOriginalDisplay)
195{
196 if (enter && forceOriginalDisplay && _displayID != 0)
197 {
198 /* Move the window to the desired display. We should not wait
199 * for the window to be moved, because some backends can refuse
200 * the move. The intent of moving the window is enough for SDL
201 * to decide which display will be used for fullscreen. */
202 SDL_Rect rect = {};
203 std::ignore = SDL_GetDisplayBounds(_displayID, &rect);
204 std::ignore = SDL_SetWindowPosition(_window, rect.x, rect.y);
205 }
206 std::ignore = SDL_SetWindowFullscreen(_window, enter);
207 std::ignore = SDL_SyncWindow(_window);
208}
209
210void SdlWindow::minimize()
211{
212 SDL_MinimizeWindow(_window);
213 std::ignore = SDL_SyncWindow(_window);
214}
215
216bool SdlWindow::resize(const SDL_Point& size)
217{
218 return SDL_SetWindowSize(_window, size.x, size.y);
219}
220
221bool SdlWindow::drawRect(SDL_Surface* surface, SDL_Point offset, const SDL_Rect& srcRect)
222{
223 WINPR_ASSERT(surface);
224 SDL_Rect dstRect = { offset.x + srcRect.x, offset.y + srcRect.y, srcRect.w, srcRect.h };
225 return blit(surface, srcRect, dstRect);
226}
227
228bool SdlWindow::drawRects(SDL_Surface* surface, SDL_Point offset,
229 const std::vector<SDL_Rect>& rects)
230{
231 if (rects.empty())
232 {
233 return drawRect(surface, offset, { 0, 0, surface->w, surface->h });
234 }
235 for (auto& srcRect : rects)
236 {
237 if (!drawRect(surface, offset, srcRect))
238 return false;
239 }
240 return true;
241}
242
243bool SdlWindow::drawScaledRect(SDL_Surface* surface, const SDL_FPoint& scale,
244 const SDL_Rect& srcRect)
245{
246 SDL_Rect dstRect = srcRect;
247 dstRect.x = static_cast<Sint32>(static_cast<float>(dstRect.x) * scale.x);
248 dstRect.w = static_cast<Sint32>(static_cast<float>(dstRect.w) * scale.x);
249 dstRect.y = static_cast<Sint32>(static_cast<float>(dstRect.y) * scale.y);
250 dstRect.h = static_cast<Sint32>(static_cast<float>(dstRect.h) * scale.y);
251 return blit(surface, srcRect, dstRect);
252}
253
254bool SdlWindow::drawScaledRects(SDL_Surface* surface, const SDL_FPoint& scale,
255 const std::vector<SDL_Rect>& rects)
256{
257 if (rects.empty())
258 {
259 return drawScaledRect(surface, scale, { 0, 0, surface->w, surface->h });
260 }
261 for (const auto& srcRect : rects)
262 {
263 if (!drawScaledRect(surface, scale, srcRect))
264 return false;
265 }
266 return true;
267}
268
269bool SdlWindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
270{
271 return fill(_window, r, g, b, a);
272}
273
274bool SdlWindow::fill(SDL_Window* window, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
275{
276 auto surface = SDL_GetWindowSurface(window);
277 if (!surface)
278 return false;
279 SDL_Rect rect = { 0, 0, surface->w, surface->h };
280 auto color = SDL_MapSurfaceRGBA(surface, r, g, b, a);
281
282 return SDL_FillSurfaceRect(surface, &rect, color);
283}
284
285rdpMonitor SdlWindow::query(SDL_Window* window, SDL_DisplayID id, bool forceAsPrimary)
286{
287 if (!window)
288 return {};
289
290 const auto& r = rect(window, forceAsPrimary);
291 const float factor = SDL_GetWindowDisplayScale(window);
292 const float dpi = std::roundf(factor * 100.0f);
293
294 WINPR_ASSERT(r.w > 0);
295 WINPR_ASSERT(r.h > 0);
296
297 const auto primary = SDL_GetPrimaryDisplay();
298 const auto orientation = SDL_GetCurrentDisplayOrientation(id);
299 const auto rdp_orientation = sdl::utils::orientaion_to_rdp(orientation);
300
301 rdpMonitor monitor{};
302 monitor.orig_screen = id;
303 monitor.x = r.x;
304 monitor.y = r.y;
305 monitor.width = r.w;
306 monitor.height = r.h;
307 monitor.is_primary = forceAsPrimary || (id == primary);
308 monitor.attributes.desktopScaleFactor = static_cast<UINT32>(dpi);
309 monitor.attributes.deviceScaleFactor = 100;
310 monitor.attributes.orientation = rdp_orientation;
311 monitor.attributes.physicalWidth = WINPR_ASSERTING_INT_CAST(uint32_t, r.w);
312 monitor.attributes.physicalHeight = WINPR_ASSERTING_INT_CAST(uint32_t, r.h);
313
314 SDL_Log("monitor.orig_screen %" PRIu32, monitor.orig_screen);
315 SDL_Log("monitor.x %" PRId32, monitor.x);
316 SDL_Log("monitor.y %" PRId32, monitor.y);
317 SDL_Log("monitor.width %" PRId32, monitor.width);
318 SDL_Log("monitor.height %" PRId32, monitor.height);
319 SDL_Log("monitor.is_primary %" PRIu32, monitor.is_primary);
320 SDL_Log("monitor.attributes.desktopScaleFactor %" PRIu32,
321 monitor.attributes.desktopScaleFactor);
322 SDL_Log("monitor.attributes.deviceScaleFactor %" PRIu32, monitor.attributes.deviceScaleFactor);
323 SDL_Log("monitor.attributes.orientation %s",
324 freerdp_desktop_rotation_flags_to_string(monitor.attributes.orientation));
325 SDL_Log("monitor.attributes.physicalWidth %" PRIu32, monitor.attributes.physicalWidth);
326 SDL_Log("monitor.attributes.physicalHeight %" PRIu32, monitor.attributes.physicalHeight);
327 return monitor;
328}
329
330SDL_Rect SdlWindow::rect(SDL_Window* window, bool forceAsPrimary)
331{
332 SDL_Rect rect = {};
333 if (!window)
334 return {};
335
336 if (!forceAsPrimary)
337 {
338 if (!SDL_GetWindowPosition(window, &rect.x, &rect.y))
339 return {};
340 }
341
342 if (!SDL_GetWindowSizeInPixels(window, &rect.w, &rect.h))
343 return {};
344
345 return rect;
346}
347
348SdlWindow::HighDPIMode SdlWindow::isHighDPIWindowsMode(SDL_Window* window)
349{
350 if (!window)
351 return MODE_INVALID;
352
353 const auto id = SDL_GetDisplayForWindow(window);
354 if (id == 0)
355 return MODE_INVALID;
356
357 const auto cs = SDL_GetDisplayContentScale(id);
358 const auto ds = SDL_GetWindowDisplayScale(window);
359 const auto pd = SDL_GetWindowPixelDensity(window);
360
361 /* mac os x style, but no HighDPI display */
362 if ((cs == 1.0f) && (ds == 1.0f) && (pd == 1.0f))
363 return MODE_NONE;
364
365 /* mac os x style HighDPI */
366 if ((cs == 1.0f) && (ds > 1.0f) && (pd > 1.0f))
367 return MODE_MACOS;
368
369 /* rest is windows style */
370 return MODE_WINDOWS;
371}
372
373bool SdlWindow::blit(SDL_Surface* surface, const SDL_Rect& srcRect, SDL_Rect& dstRect)
374{
375 auto screen = SDL_GetWindowSurface(_window);
376 if (!screen || !surface)
377 return false;
378 if (!SDL_SetSurfaceClipRect(surface, &srcRect))
379 return true;
380 if (!SDL_SetSurfaceClipRect(screen, &dstRect))
381 return true;
382 if (!SDL_BlitSurfaceScaled(surface, &srcRect, screen, &dstRect, SDL_SCALEMODE_LINEAR))
383 {
384 SDL_LogError(SDL_LOG_CATEGORY_RENDER, "SDL_BlitScaled: %s", SDL_GetError());
385 return false;
386 }
387 return true;
388}
389
390void SdlWindow::updateSurface()
391{
392 SDL_UpdateWindowSurface(_window);
393}
394
395SdlWindow SdlWindow::create(SDL_DisplayID id, const std::string& title, Uint32 flags, Uint32 width,
396 Uint32 height)
397{
398 flags |= SDL_WINDOW_HIGH_PIXEL_DENSITY;
399
400 SDL_Rect rect = { static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(id)),
401 static_cast<int>(SDL_WINDOWPOS_CENTERED_DISPLAY(id)), static_cast<int>(width),
402 static_cast<int>(height) };
403
404 if ((flags & SDL_WINDOW_FULLSCREEN) != 0)
405 {
406 std::ignore = SDL_GetDisplayBounds(id, &rect);
407 }
408
409 SdlWindow window{ id, title, rect, flags };
410
411 if ((flags & (SDL_WINDOW_FULLSCREEN)) != 0)
412 {
413 window.setOffsetX(rect.x);
414 window.setOffsetY(rect.y);
415 }
416
417 return window;
418}
419
420static SDL_Window* createDummy(SDL_DisplayID id)
421{
422 const auto x = SDL_WINDOWPOS_CENTERED_DISPLAY(id);
423 const auto y = SDL_WINDOWPOS_CENTERED_DISPLAY(id);
424 const int w = 64;
425 const int h = 64;
426
427 auto props = SDL_CreateProperties();
428 std::stringstream ss;
429 ss << "SdlWindow::query(" << id << ")";
430 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, ss.str().c_str());
431 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, x);
432 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, y);
433 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, w);
434 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, h);
435
436 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
437 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN, true);
438 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_BORDERLESS_BOOLEAN, true);
439 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN, false);
440
441 auto window = SDL_CreateWindowWithProperties(props);
442 SDL_DestroyProperties(props);
443 return window;
444}
445
446rdpMonitor SdlWindow::query(SDL_DisplayID id, bool forceAsPrimary)
447{
448 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(id), SDL_DestroyWindow);
449 if (!window)
450 return {};
451
452 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
453 SDL_CreateRenderer(window.get(), nullptr), SDL_DestroyRenderer);
454
455 if (!SDL_SyncWindow(window.get()))
456 return {};
457
458 SDL_Event event{};
459 while (SDL_PollEvent(&event))
460 ;
461
462 return query(window.get(), id, forceAsPrimary);
463}
464
465SDL_Rect SdlWindow::rect(SDL_DisplayID id, bool forceAsPrimary)
466{
467 std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window(createDummy(id), SDL_DestroyWindow);
468 if (!window)
469 return {};
470
471 std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer(
472 SDL_CreateRenderer(window.get(), nullptr), SDL_DestroyRenderer);
473
474 if (!SDL_SyncWindow(window.get()))
475 return {};
476
477 SDL_Event event{};
478 while (SDL_PollEvent(&event))
479 ;
480
481 return rect(window.get(), forceAsPrimary);
482}
SdlWindow(const std::string &title, Sint32 startupX, Sint32 startupY, Sint32 width, Sint32 height, Uint32 flags)