22#include <freerdp/config.h>
32#include <winpr/assert.h>
35#include <freerdp/log.h>
37#define TAG CLIENT_TAG("sdl")
39#include "sdl_monitor.hpp"
40#include "sdl_context.hpp"
54 MONITOR_INFO* monitors;
63 if (!SDL_Init(SDL_INIT_VIDEO))
65 WLog_Print(
sdl->getWLog(), WLOG_ERROR,
"SDL_Init failed: %s", SDL_GetError());
70 auto ids = SDL_GetDisplays(&nmonitors);
72 printf(
"listing %d monitors:\n", nmonitors);
73 for (
int i = 0; i < nmonitors; i++)
77 const auto brc = SDL_GetDisplayBounds(
id, &rect);
78 const char* name = SDL_GetDisplayName(
id);
82 printf(
" %s [%u] [%s] %dx%d\t+%d+%d\n", (i == 0) ?
"*" :
" ",
id, name, rect.w, rect.h,
90[[nodiscard]]
static BOOL sdl_apply_mon_max_size(
SdlContext*
sdl, UINT32* pMaxWidth,
98 WINPR_ASSERT(pMaxWidth);
99 WINPR_ASSERT(pMaxHeight);
101 auto settings =
sdl->context()->settings;
102 WINPR_ASSERT(settings);
106 auto monitor =
static_cast<const rdpMonitor*
>(
107 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, x));
109 left = std::min(monitor->x, left);
110 top = std::min(monitor->y, top);
111 right = std::max(monitor->x + monitor->width, right);
112 bottom = std::max(monitor->y + monitor->height, bottom);
115 const int32_t w = right - left;
116 const int32_t h = bottom - top;
118 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, w);
119 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, h);
123[[nodiscard]]
static BOOL sdl_apply_max_size(
SdlContext*
sdl, UINT32* pMaxWidth, UINT32* pMaxHeight)
126 WINPR_ASSERT(pMaxWidth);
127 WINPR_ASSERT(pMaxHeight);
129 auto settings =
sdl->context()->settings;
130 WINPR_ASSERT(settings);
137 auto monitor =
static_cast<const rdpMonitor*
>(
138 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorDefArray, x));
145 if (SDL_GetDisplayBounds(monitor->orig_screen, &rect))
147 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, rect.w);
148 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, rect.h);
153 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, monitor->width);
154 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, monitor->height);
159 SDL_GetDisplayUsableBounds(monitor->orig_screen, &rect);
160 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, rect.w);
161 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, rect.h);
166 SDL_GetDisplayUsableBounds(monitor->orig_screen, &rect);
168 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, rect.w);
169 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, rect.h);
172 *pMaxWidth = (WINPR_ASSERTING_INT_CAST(uint32_t, rect.w) *
177 *pMaxHeight = (WINPR_ASSERTING_INT_CAST(uint32_t, rect.h) *
191[[nodiscard]]
static BOOL sdl_apply_display_properties(
SdlContext*
sdl)
195 rdpSettings* settings =
sdl->context()->settings;
196 WINPR_ASSERT(settings);
198 std::vector<rdpMonitor> monitors;
204 if (
sdl->monitorIds().empty())
206 const auto id =
sdl->monitorIds().front();
207 auto monitor =
sdl->getDisplay(
id);
208 monitor.is_primary =
true;
211 monitors.emplace_back(monitor);
217 for (
const auto&
id :
sdl->monitorIds())
219 const auto monitor =
sdl->getDisplay(
id);
220 monitors.emplace_back(monitor);
226 for (
auto& monitor : monitors)
227 monitor.is_primary = FALSE;
228 monitors.at(0).is_primary = TRUE;
233 if (!monitors.empty() && std::none_of(monitors.cbegin(), monitors.cend(),
234 [](
const rdpMonitor& m) { return m.is_primary; }))
235 monitors.at(0).is_primary =
true;
240[[nodiscard]]
static BOOL sdl_detect_single_window(
SdlContext*
sdl, UINT32* pMaxWidth,
244 WINPR_ASSERT(pMaxWidth);
245 WINPR_ASSERT(pMaxHeight);
247 rdpSettings* settings =
sdl->context()->settings;
248 WINPR_ASSERT(settings);
259 SDL_DisplayID
id = 0;
260 const auto& ids =
sdl->monitorIds();
265 sdl->setMonitorIds({
id });
268 if (!sdl_apply_display_properties(
sdl))
270 return sdl_apply_max_size(
sdl, pMaxWidth, pMaxHeight);
272 return sdl_apply_mon_max_size(
sdl, pMaxWidth, pMaxHeight);
275BOOL sdl_detect_monitors(
SdlContext*
sdl, UINT32* pMaxWidth, UINT32* pMaxHeight)
278 WINPR_ASSERT(pMaxWidth);
279 WINPR_ASSERT(pMaxHeight);
281 rdpSettings* settings =
sdl->context()->settings;
282 WINPR_ASSERT(settings);
284 const auto& ids =
sdl->getDisplayIds();
296 sdl->setMonitorIds(ids);
299 sdl->setMonitorIds({ ids.front() });
308 "Found %" PRIu32
" monitor IDs, but only have %" PRIuz
" monitors connected",
313 std::vector<SDL_DisplayID> used;
314 for (
size_t x = 0; x < nr; x++)
316 auto cur =
static_cast<const UINT32*
>(
317 freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorIds, x));
320 SDL_DisplayID
id = *cur;
323 if (std::find(ids.begin(), ids.end(),
id) == ids.end())
325 WLog_ERR(TAG,
"Supplied monitor ID[%" PRIuz
"]=%" PRIu32
" is invalid", x,
id);
330 if (std::find(used.begin(), used.end(),
id) != used.end())
332 WLog_ERR(TAG,
"Duplicate monitor ID[%" PRIuz
"]=%" PRIu32
" detected", x,
id);
337 sdl->setMonitorIds(used);
340 if (!sdl_apply_display_properties(
sdl))
343 auto size =
static_cast<uint32_t
>(
sdl->monitorIds().size());
347 return sdl_detect_single_window(
sdl, pMaxWidth, pMaxHeight);
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_monitor_def_array_sorted(rdpSettings *settings, const rdpMonitor *monitors, size_t count)
Sort monitor array according to:
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.