xwayland: Move protocol <-> stage calculations to Xwayland manager

Will make it more convenient to use without a MetaWindow at hand.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/4169>
This commit is contained in:
Jonas Ådahl 2024-12-05 15:03:46 +01:00 committed by Marge Bot
parent 64847aa50f
commit 796ee28b1c
3 changed files with 105 additions and 48 deletions

View File

@ -328,29 +328,10 @@ meta_window_xwayland_stage_to_protocol (MetaWindow *window,
MetaWaylandCompositor *wayland_compositor =
meta_context_get_wayland_compositor (context);
MetaXWaylandManager *xwayland_manager = &wayland_compositor->xwayland_manager;
int scale;
scale = meta_xwayland_get_effective_scale (xwayland_manager);
if (protocol_x)
*protocol_x = stage_x * scale;
if (protocol_y)
*protocol_y = stage_y * scale;
}
static int
scale_and_handle_overflow (int protocol,
float scale,
float (* rounding_function) (float value))
{
float value;
value = rounding_function (protocol * scale);
if (value >= (float) INT_MAX)
return INT_MAX;
else if (value <= (float) INT_MIN)
return INT_MIN;
else
return (int) value;
meta_xwayland_stage_to_protocol_point (xwayland_manager,
stage_x, stage_y,
protocol_x, protocol_y);
}
static void
@ -366,33 +347,11 @@ meta_window_xwayland_protocol_to_stage (MetaWindow *window,
MetaWaylandCompositor *wayland_compositor =
meta_context_get_wayland_compositor (context);
MetaXWaylandManager *xwayland_manager = &wayland_compositor->xwayland_manager;
int xwayland_scale;
float scale;
xwayland_scale = meta_xwayland_get_effective_scale (xwayland_manager);
scale = 1.0f / xwayland_scale;
switch (rounding_strategy)
{
case MTK_ROUNDING_STRATEGY_SHRINK:
if (stage_x)
*stage_x = scale_and_handle_overflow (protocol_x, scale, floorf);
if (stage_y)
*stage_y = scale_and_handle_overflow (protocol_y, scale, floorf);
break;
case MTK_ROUNDING_STRATEGY_GROW:
if (stage_x)
*stage_x = scale_and_handle_overflow (protocol_x, scale, ceilf);
if (stage_y)
*stage_y = scale_and_handle_overflow (protocol_y, scale, ceilf);
break;
case MTK_ROUNDING_STRATEGY_ROUND:
if (stage_x)
*stage_x = scale_and_handle_overflow (protocol_x, scale, roundf);
if (stage_y)
*stage_y = scale_and_handle_overflow (protocol_y, scale, roundf);
break;
}
meta_xwayland_protocol_to_stage (xwayland_manager,
protocol_x, protocol_y,
stage_x, stage_y,
rounding_strategy);
}
static void

View File

@ -1360,6 +1360,87 @@ meta_xwayland_get_effective_scale (MetaXWaylandManager *manager)
return 1;
}
void
meta_xwayland_stage_to_protocol_point (MetaXWaylandManager *manager,
int stage_x,
int stage_y,
int *protocol_x,
int *protocol_y)
{
int scale;
scale = meta_xwayland_get_effective_scale (manager);
if (protocol_x)
*protocol_x = stage_x * scale;
if (protocol_y)
*protocol_y = stage_y * scale;
}
void meta_xwayland_stage_to_protocol_rect (MetaXWaylandManager *manager,
const MtkRectangle *stage_rect,
MtkRectangle *protocol_rect)
{
meta_xwayland_stage_to_protocol_point (manager,
stage_rect->x, stage_rect->y,
&protocol_rect->x, &protocol_rect->y);
meta_xwayland_stage_to_protocol_point (manager,
stage_rect->width, stage_rect->height,
&protocol_rect->width, &protocol_rect->height);
}
static int
scale_and_handle_overflow (int protocol,
float scale,
float (* rounding_function) (float value))
{
float value;
value = rounding_function (protocol * scale);
if (value >= (float) INT_MAX)
return INT_MAX;
else if (value <= (float) INT_MIN)
return INT_MIN;
else
return (int) value;
}
void
meta_xwayland_protocol_to_stage (MetaXWaylandManager *xwayland_manager,
int protocol_x,
int protocol_y,
int *stage_x,
int *stage_y,
MtkRoundingStrategy rounding_strategy)
{
int xwayland_scale;
float scale;
xwayland_scale = meta_xwayland_get_effective_scale (xwayland_manager);
scale = 1.0f / xwayland_scale;
switch (rounding_strategy)
{
case MTK_ROUNDING_STRATEGY_SHRINK:
if (stage_x)
*stage_x = scale_and_handle_overflow (protocol_x, scale, floorf);
if (stage_y)
*stage_y = scale_and_handle_overflow (protocol_y, scale, floorf);
break;
case MTK_ROUNDING_STRATEGY_GROW:
if (stage_x)
*stage_x = scale_and_handle_overflow (protocol_x, scale, ceilf);
if (stage_y)
*stage_y = scale_and_handle_overflow (protocol_y, scale, ceilf);
break;
case MTK_ROUNDING_STRATEGY_ROUND:
if (stage_x)
*stage_x = scale_and_handle_overflow (protocol_x, scale, roundf);
if (stage_y)
*stage_y = scale_and_handle_overflow (protocol_y, scale, roundf);
break;
}
}
int
meta_xwayland_get_x11_ui_scaling_factor (MetaXWaylandManager *manager)
{

View File

@ -51,4 +51,21 @@ gboolean meta_xwayland_signal (MetaXWaylandManager *manager,
int meta_xwayland_get_effective_scale (MetaXWaylandManager *manager);
void meta_xwayland_stage_to_protocol_point (MetaXWaylandManager *manager,
int stage_x,
int stage_y,
int *protocol_x,
int *protocol_y);
void meta_xwayland_stage_to_protocol_rect (MetaXWaylandManager *manager,
const MtkRectangle *stage_rect,
MtkRectangle *protocol_rect);
void meta_xwayland_protocol_to_stage (MetaXWaylandManager *manager,
int protocol_x,
int protocol_y,
int *stage_x,
int *stage_y,
MtkRoundingStrategy rounding_strategy);
int meta_xwayland_get_x11_ui_scaling_factor (MetaXWaylandManager *manager);