FreeRDP
Loading...
Searching...
No Matches
sdl_input_mapping.hpp
1
19
20#pragma once
21
22#include <algorithm>
23#include <cstddef>
24#include <optional>
25#include <vector>
26
27#include <SDL3/SDL_rect.h>
28
29/* Pointer mapping uses the monitor layout as its only reference.
30 *
31 * The monitor layout sent to the server is relative to the primary monitor, so monitors left of
32 * or above it have negative coordinates. Mouse input is relative to the top left of the layout's
33 * bounding box instead. Every window of the session, a desktop window per monitor, the single
34 * desktop window or a RemoteApp window, shows a part of that layout, and is described the same
35 * way by SdlInputWindow. */
37{
38 /* Window bounds on the local desktop, in points. Empty if the platform cannot report window
39 * positions (Wayland); such a window is never chosen for positions outside its source. */
40 SDL_Rect local{};
41 /* Window pixels per point. */
42 float density = 1.0f;
43 /* Window pixels per remote pixel, for smart-sizing; 1 without local scaling. */
44 SDL_FPoint scale{ 1.0f, 1.0f };
45 /* Top left of the area the window shows, in monitor layout coordinates. */
46 SDL_Point layout{};
47};
48
49/* Captured coordinates are relative to the source window even when the pointer
50 * is over another window. Rebase in logical desktop units, before applying the
51 * destination renderer's pixel scale. Bounds are half-open at shared edges. */
52[[nodiscard]] inline std::optional<SDL_FPoint>
53sdl_pointer_in_window(const SDL_FPoint& pos, const SDL_Rect& source, const SDL_Rect& target)
54{
55 if (source.w <= 0 || source.h <= 0 || target.w <= 0 || target.h <= 0)
56 return std::nullopt;
57
58 const SDL_FPoint local{ pos.x + static_cast<float>(source.x) - static_cast<float>(target.x),
59 pos.y + static_cast<float>(source.y) - static_cast<float>(target.y) };
60 if (local.x >= 0 && local.y >= 0 && local.x < static_cast<float>(target.w) &&
61 local.y < static_cast<float>(target.h))
62 return local;
63 return std::nullopt;
64}
65
66/* Top left of the bounding box of a monitor layout. The primary monitor is at 0/0, so the result
67 * is never positive. */
68[[nodiscard]] inline SDL_Point sdl_layout_input_origin(const std::vector<SDL_Rect>& monitors)
69{
70 SDL_Point origin{};
71 for (const auto& monitor : monitors)
72 {
73 origin.x = std::min(origin.x, monitor.x);
74 origin.y = std::min(origin.y, monitor.y);
75 }
76 return origin;
77}
78
79/* Map a position relative to windows[source], in points, to mouse input coordinates.
80 *
81 * A position outside the source window, as SDL reports during a captured drag, is mapped with the
82 * window under it, so that window's pixel density and layout position apply. Outside all windows
83 * with known bounds, the source window's mapping is kept. */
84[[nodiscard]] inline std::optional<SDL_FPoint>
85sdl_window_to_input(const std::vector<SdlInputWindow>& windows, size_t source,
86 const SDL_FPoint& pos, const SDL_Point& origin)
87{
88 if (source >= windows.size())
89 return std::nullopt;
90
91 const auto* target = &windows[source];
92 auto local = pos;
93 const auto& bounds = target->local;
94 if ((bounds.w > 0) && (bounds.h > 0) && !sdl_pointer_in_window(pos, bounds, bounds))
95 {
96 for (const auto& window : windows)
97 {
98 if (const auto point = sdl_pointer_in_window(pos, bounds, window.local))
99 {
100 target = &window;
101 local = *point;
102 break;
103 }
104 }
105 }
106
107 if ((target->density <= 0.0f) || (target->scale.x <= 0.0f) || (target->scale.y <= 0.0f))
108 return std::nullopt;
109
110 const auto x = static_cast<float>(target->layout.x - origin.x) +
111 local.x * target->density / target->scale.x;
112 const auto y = static_cast<float>(target->layout.y - origin.y) +
113 local.y * target->density / target->scale.y;
114 return SDL_FPoint{ x, y };
115}
116
117/* Map a relative motion in window points to mouse input units, with the window's pixel density
118 * and local scaling. */
119[[nodiscard]] inline SDL_FPoint sdl_window_delta_to_input(const SdlInputWindow& window,
120 const SDL_FPoint& delta)
121{
122 if ((window.density <= 0.0f) || (window.scale.x <= 0.0f) || (window.scale.y <= 0.0f))
123 return delta;
124 return SDL_FPoint{ delta.x * window.density / window.scale.x,
125 delta.y * window.density / window.scale.y };
126}