From 388fce5a1fa1c6f073a8f84f7e5b6469bbcc273c Mon Sep 17 00:00:00 2001 From: Daniel De Graaf Date: Mon, 9 Nov 2020 21:00:59 -0500 Subject: [PATCH] output: add phys_scale to adjust EDID-reported size Some monitors report an invalid size in their EDID. While sway ignores these sizes (see phys_size_is_aspect_ratio), some other applications do not. Instead of requiring a change to the monitor, allow specifying a scaling factor in the configuration to bring the physical dimensions in line with reality. --- include/sway/commands.h | 1 + include/sway/config.h | 1 + sway/commands/output.c | 1 + sway/commands/output/phys_scale.c | 22 ++++++++++++++++++++++ sway/config/output.c | 11 +++++++++++ sway/meson.build | 1 + 6 files changed, 37 insertions(+) create mode 100644 sway/commands/output/phys_scale.c diff --git a/include/sway/commands.h b/include/sway/commands.h index 964b36611..f5280fe55 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -282,6 +282,7 @@ sway_cmd output_cmd_dpms; sway_cmd output_cmd_enable; sway_cmd output_cmd_max_render_time; sway_cmd output_cmd_mode; +sway_cmd output_cmd_phys_scale; sway_cmd output_cmd_position; sway_cmd output_cmd_scale; sway_cmd output_cmd_scale_filter; diff --git a/include/sway/config.h b/include/sway/config.h index 59f22ae2b..963cc2b2a 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -259,6 +259,7 @@ struct output_config { int custom_mode; int x, y; float scale; + float phys_scale; enum scale_filter_mode scale_filter; int32_t transform; enum wl_output_subpixel subpixel; diff --git a/sway/commands/output.c b/sway/commands/output.c index 5186a2ba1..4eff8875c 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -15,6 +15,7 @@ static struct cmd_handler output_handlers[] = { { "enable", output_cmd_enable }, { "max_render_time", output_cmd_max_render_time }, { "mode", output_cmd_mode }, + { "phys_scale", output_cmd_phys_scale }, { "pos", output_cmd_position }, { "position", output_cmd_position }, { "res", output_cmd_mode }, diff --git a/sway/commands/output/phys_scale.c b/sway/commands/output/phys_scale.c new file mode 100644 index 000000000..7f3bfb22d --- /dev/null +++ b/sway/commands/output/phys_scale.c @@ -0,0 +1,22 @@ +#include +#include "sway/commands.h" +#include "sway/config.h" + +struct cmd_results *output_cmd_phys_scale(int argc, char **argv) { + if (!config->handler_context.output_config) { + return cmd_results_new(CMD_FAILURE, "Missing output config"); + } + if (!argc) { + return cmd_results_new(CMD_INVALID, "Missing phys_scale argument."); + } + + char *end; + config->handler_context.output_config->phys_scale = strtof(*argv, &end); + if (*end) { + return cmd_results_new(CMD_INVALID, "Invalid scale."); + } + + config->handler_context.leftovers.argc = argc - 1; + config->handler_context.leftovers.argv = argv + 1; + return NULL; +} diff --git a/sway/config/output.c b/sway/config/output.c index b59cabd4e..a2b2e9517 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -59,6 +59,7 @@ struct output_config *new_output_config(const char *name) { oc->refresh_rate = -1; oc->custom_mode = -1; oc->x = oc->y = -1; + oc->phys_scale = -1; oc->scale = -1; oc->scale_filter = SCALE_FILTER_DEFAULT; oc->transform = -1; @@ -84,6 +85,9 @@ void merge_output_config(struct output_config *dst, struct output_config *src) { if (src->y != -1) { dst->y = src->y; } + if (src->phys_scale != -1) { + dst->phys_scale = src->phys_scale; + } if (src->scale != -1) { dst->scale = src->scale; } @@ -369,6 +373,13 @@ static void queue_output_config(struct output_config *oc, wlr_output_set_transform(wlr_output, oc->transform); } + if (oc && oc->phys_scale > 0) { + wlr_output->phys_width = wlr_output->phys_width * oc->phys_scale; + wlr_output->phys_height = wlr_output->phys_height * oc->phys_scale; + sway_log(SWAY_DEBUG, "Scaling %s physical sizes by %f to %dx%d mm", oc->name, + oc->phys_scale, wlr_output->phys_width, wlr_output->phys_height); + } + // Apply the scale last before the commit, because the scale auto-detection // reads the pending output size float scale; diff --git a/sway/meson.build b/sway/meson.build index 6e138101f..3b612c66c 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -186,6 +186,7 @@ sway_sources = files( 'commands/output/enable.c', 'commands/output/max_render_time.c', 'commands/output/mode.c', + 'commands/output/phys_scale.c', 'commands/output/position.c', 'commands/output/scale.c', 'commands/output/scale_filter.c',