From 57d6f6f19e3088dcb8e202acade8c39a80075b4a Mon Sep 17 00:00:00 2001 From: David Rosca Date: Sun, 18 Jul 2021 12:05:47 +0200 Subject: [PATCH] Add `output modeline` command Only works with DRM backend. --- include/sway/commands.h | 1 + include/sway/config.h | 2 ++ sway/commands/output.c | 1 + sway/commands/output/mode.c | 58 +++++++++++++++++++++++++++++++++++++ sway/config/output.c | 23 ++++++++++++++- sway/sway-output.5.scd | 10 +++++++ 6 files changed, 94 insertions(+), 1 deletion(-) diff --git a/include/sway/commands.h b/include/sway/commands.h index 29a6bec3..4be40870 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_modeline; 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 d6c29fe6..52867fa6 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "../include/config.h" #include "list.h" #include "swaynag.h" @@ -257,6 +258,7 @@ struct output_config { int width, height; float refresh_rate; int custom_mode; + drmModeModeInfo drm_mode; int x, y; float scale; enum scale_filter_mode scale_filter; diff --git a/sway/commands/output.c b/sway/commands/output.c index 4418f23f..d8ef2885 100644 --- a/sway/commands/output.c +++ b/sway/commands/output.c @@ -15,6 +15,7 @@ static const struct cmd_handler output_handlers[] = { { "enable", output_cmd_enable }, { "max_render_time", output_cmd_max_render_time }, { "mode", output_cmd_mode }, + { "modeline", output_cmd_modeline }, { "pos", output_cmd_position }, { "position", output_cmd_position }, { "res", output_cmd_mode }, diff --git a/sway/commands/output/mode.c b/sway/commands/output/mode.c index 5b710713..019d625a 100644 --- a/sway/commands/output/mode.c +++ b/sway/commands/output/mode.c @@ -20,6 +20,9 @@ struct cmd_results *output_cmd_mode(int argc, char **argv) { output->custom_mode = 0; } + // Reset custom modeline, if any + output->drm_mode.type = 0; + char *end; output->width = strtol(*argv, &end, 10); if (*end) { @@ -58,3 +61,58 @@ struct cmd_results *output_cmd_mode(int argc, char **argv) { return NULL; } +static bool parse_modeline(char **argv, drmModeModeInfo *mode) { + mode->type = DRM_MODE_TYPE_USERDEF; + mode->clock = strtof(argv[0], NULL) * 1000; + mode->hdisplay = strtol(argv[1], NULL, 10); + mode->hsync_start = strtol(argv[2], NULL, 10); + mode->hsync_end = strtol(argv[3], NULL, 10); + mode->htotal = strtol(argv[4], NULL, 10); + mode->vdisplay = strtol(argv[5], NULL, 10); + mode->vsync_start = strtol(argv[6], NULL, 10); + mode->vsync_end = strtol(argv[7], NULL, 10); + mode->vtotal = strtol(argv[8], NULL, 10); + + mode->vrefresh = mode->clock * 1000.0 * 1000.0 + / mode->htotal / mode->vtotal; + if (strcasecmp(argv[9], "+hsync") == 0) { + mode->flags |= DRM_MODE_FLAG_PHSYNC; + } else if (strcasecmp(argv[9], "-hsync") == 0) { + mode->flags |= DRM_MODE_FLAG_NHSYNC; + } else { + return false; + } + + if (strcasecmp(argv[10], "+vsync") == 0) { + mode->flags |= DRM_MODE_FLAG_PVSYNC; + } else if (strcasecmp(argv[10], "-vsync") == 0) { + mode->flags |= DRM_MODE_FLAG_NVSYNC; + } else { + return false; + } + + snprintf(mode->name, sizeof(mode->name), "%dx%d@%d", + mode->hdisplay, mode->vdisplay, mode->vrefresh / 1000); + + return true; +} + +struct cmd_results *output_cmd_modeline(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 modeline argument."); + } + + struct output_config *output = config->handler_context.output_config; + + if (argc != 11 || !parse_modeline(argv, &output->drm_mode)) { + return cmd_results_new(CMD_INVALID, "Invalid modeline"); + } + + config->handler_context.leftovers.argc = argc - 12; + config->handler_context.leftovers.argv = argv + 12; + return NULL; +} + diff --git a/sway/config/output.c b/sway/config/output.c index 6224fc10..9fff79fd 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "sway/config.h" #include "sway/input/cursor.h" #include "sway/output.h" @@ -58,6 +59,7 @@ struct output_config *new_output_config(const char *name) { oc->width = oc->height = -1; oc->refresh_rate = -1; oc->custom_mode = -1; + oc->drm_mode.type = -1; oc->x = oc->y = -1; oc->scale = -1; oc->scale_filter = SCALE_FILTER_DEFAULT; @@ -99,6 +101,9 @@ void merge_output_config(struct output_config *dst, struct output_config *src) { if (src->custom_mode != -1) { dst->custom_mode = src->custom_mode; } + if (src->drm_mode.type != (uint32_t) -1) { + memcpy(&dst->drm_mode, &src->drm_mode, sizeof(src->drm_mode)); + } if (src->transform != -1) { dst->transform = src->transform; } @@ -271,6 +276,18 @@ static void set_mode(struct wlr_output *output, int width, int height, wlr_output_set_mode(output, best); } +static void set_modeline(struct wlr_output *output, drmModeModeInfo *drm_mode) { + if (!wlr_output_is_drm(output)) { + sway_log(SWAY_ERROR, "Modeline can only be set to DRM output"); + return; + } + sway_log(SWAY_DEBUG, "Assigning custom modeline to %s", output->name); + struct wlr_output_mode *mode = wlr_drm_connector_add_mode(output, drm_mode); + if (mode) { + wlr_output_set_mode(output, mode); + } +} + /* Some manufacturers hardcode the aspect-ratio of the output in the physical * size field. */ static bool phys_size_is_aspect_ratio(struct wlr_output *output) { @@ -351,7 +368,11 @@ static void queue_output_config(struct output_config *oc, sway_log(SWAY_DEBUG, "Turning on output %s", wlr_output->name); wlr_output_enable(wlr_output, true); - if (oc && oc->width > 0 && oc->height > 0) { + if (oc && oc->drm_mode.type != 0 && oc->drm_mode.type != (uint32_t) -1) { + sway_log(SWAY_DEBUG, "Set %s modeline", + wlr_output->name); + set_modeline(wlr_output, &oc->drm_mode); + } else if (oc && oc->width > 0 && oc->height > 0) { sway_log(SWAY_DEBUG, "Set %s mode to %dx%d (%f Hz)", wlr_output->name, oc->width, oc->height, oc->refresh_rate); set_mode(wlr_output, oc->width, oc->height, diff --git a/sway/sway-output.5.scd b/sway/sway-output.5.scd index 2828c841..55d8f719 100644 --- a/sway/sway-output.5.scd +++ b/sway/sway-output.5.scd @@ -40,6 +40,16 @@ must be separated by one space. For example: output HDMI-A-1 mode 1920x1080@60Hz +*output* modeline + Configures the specified output to use the given modeline. It can be + generated using *cvt*(1) and *gtf*(1) commands. See *xorg.conf*(5). + Only supported on DRM backend. + + Example: + + output HDMI-A-1 modeline 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync + + *output* position|pos Places the specified output at the specific position in the global coordinate space. The cursor may only be moved between immediately