Skip to content

Commit 101b75f

Browse files
committed
output: add color_format command
Allow configuring the output color format (RGB/YPbPr) via the output config. Exposes the value in IPC JSON output.
1 parent 0bf8731 commit 101b75f

8 files changed

Lines changed: 63 additions & 0 deletions

File tree

include/sway/commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ sway_cmd input_cmd_xkb_variant;
286286
sway_cmd output_cmd_adaptive_sync;
287287
sway_cmd output_cmd_allow_tearing;
288288
sway_cmd output_cmd_background;
289+
sway_cmd output_cmd_color_format;
289290
sway_cmd output_cmd_color_profile;
290291
sway_cmd output_cmd_disable;
291292
sway_cmd output_cmd_dpms;

include/sway/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <wlr/types/wlr_tablet_tool.h>
99
#include <wlr/util/box.h>
1010
#include <wlr/render/color.h>
11+
#include <wlr/types/wlr_output.h>
1112
#include <xkbcommon/xkbcommon.h>
1213
#include <xf86drmMode.h>
1314
#include "../include/config.h"
@@ -294,6 +295,7 @@ struct output_config {
294295
int max_render_time; // In milliseconds
295296
int adaptive_sync;
296297
enum render_bit_depth render_bit_depth;
298+
enum wlr_output_color_format color_format;
297299
enum color_profile color_profile;
298300
struct wlr_color_transform *color_transform;
299301
int allow_tearing;

sway/commands/output.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ static const struct cmd_handler output_handlers[] = {
1111
{ "allow_tearing", output_cmd_allow_tearing },
1212
{ "background", output_cmd_background },
1313
{ "bg", output_cmd_background },
14+
{ "color_format", output_cmd_color_format },
1415
{ "color_profile", output_cmd_color_profile },
1516
{ "disable", output_cmd_disable },
1617
{ "dpms", output_cmd_dpms },
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <strings.h>
2+
#include "sway/commands.h"
3+
#include "sway/config.h"
4+
5+
struct cmd_results *output_cmd_color_format(int argc, char **argv) {
6+
if (!config->handler_context.output_config) {
7+
return cmd_results_new(CMD_FAILURE, "Missing output config");
8+
}
9+
if (!argc) {
10+
return cmd_results_new(CMD_INVALID, "Missing color format argument.");
11+
}
12+
13+
if (strcmp(*argv, "auto") == 0) {
14+
config->handler_context.output_config->color_format =
15+
WLR_OUTPUT_COLOR_FORMAT_AUTO;
16+
} else if (strcmp(*argv, "rgb") == 0) {
17+
config->handler_context.output_config->color_format =
18+
WLR_OUTPUT_COLOR_FORMAT_RGB444;
19+
} else if (strcmp(*argv, "yuv444") == 0) {
20+
config->handler_context.output_config->color_format =
21+
WLR_OUTPUT_COLOR_FORMAT_YCBCR444;
22+
} else if (strcmp(*argv, "yuv422") == 0) {
23+
config->handler_context.output_config->color_format =
24+
WLR_OUTPUT_COLOR_FORMAT_YCBCR422;
25+
} else if (strcmp(*argv, "yuv420") == 0) {
26+
config->handler_context.output_config->color_format =
27+
WLR_OUTPUT_COLOR_FORMAT_YCBCR420;
28+
} else {
29+
return cmd_results_new(CMD_INVALID,
30+
"Invalid color format. Must be a value in (auto|rgb|yuv444|yuv422|yuv420).");
31+
}
32+
33+
config->handler_context.leftovers.argc = argc - 1;
34+
config->handler_context.leftovers.argv = argv + 1;
35+
return NULL;
36+
}

sway/config/output.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct output_config *new_output_config(const char *name) {
7575
oc->max_render_time = -1;
7676
oc->adaptive_sync = -1;
7777
oc->render_bit_depth = RENDER_BIT_DEPTH_DEFAULT;
78+
oc->color_format = WLR_OUTPUT_COLOR_FORMAT_AUTO;
7879
oc->color_profile = COLOR_PROFILE_DEFAULT;
7980
oc->color_transform = NULL;
8081
oc->power = -1;
@@ -130,6 +131,9 @@ static void supersede_output_config(struct output_config *dst, struct output_con
130131
if (src->render_bit_depth != RENDER_BIT_DEPTH_DEFAULT) {
131132
dst->render_bit_depth = RENDER_BIT_DEPTH_DEFAULT;
132133
}
134+
if (src->color_format != WLR_OUTPUT_COLOR_FORMAT_AUTO) {
135+
dst->color_format = WLR_OUTPUT_COLOR_FORMAT_AUTO;
136+
}
133137
if (src->color_profile != COLOR_PROFILE_DEFAULT) {
134138
if (dst->color_transform) {
135139
wlr_color_transform_unref(dst->color_transform);
@@ -207,6 +211,9 @@ static void merge_output_config(struct output_config *dst, struct output_config
207211
if (src->render_bit_depth != RENDER_BIT_DEPTH_DEFAULT) {
208212
dst->render_bit_depth = src->render_bit_depth;
209213
}
214+
if (src->color_format != WLR_OUTPUT_COLOR_FORMAT_AUTO) {
215+
dst->color_format = src->color_format;
216+
}
210217
if (src->color_profile != COLOR_PROFILE_DEFAULT) {
211218
if (src->color_transform) {
212219
wlr_color_transform_ref(src->color_transform);
@@ -553,6 +560,9 @@ static void queue_output_config(struct output_config *oc,
553560
} else {
554561
wlr_output_state_set_render_format(pending, DRM_FORMAT_XRGB8888);
555562
}
563+
if (oc && oc->color_format != WLR_OUTPUT_COLOR_FORMAT_AUTO) {
564+
wlr_output_state_set_color_format(pending, oc->color_format);
565+
}
556566

557567
bool hdr = oc && oc->hdr == 1;
558568
bool color_profile = oc && (oc->color_transform != NULL

sway/desktop/output.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ static struct output_config *output_config_for_config_head(
628628
oc->transform = config_head->state.transform;
629629
oc->scale = config_head->state.scale;
630630
oc->adaptive_sync = config_head->state.adaptive_sync_enabled;
631+
oc->color_format = config_head->state.color_format;
631632
return oc;
632633
}
633634

sway/ipc-json.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,17 @@ static void ipc_json_describe_enabled_output(struct sway_output *output,
410410
json_object_object_add(object, "max_render_time", json_object_new_int(output->max_render_time));
411411
json_object_object_add(object, "allow_tearing", json_object_new_boolean(output->allow_tearing));
412412
json_object_object_add(object, "hdr", json_object_new_boolean(output->hdr));
413+
414+
const char *color_format_str = NULL;
415+
switch (wlr_output->color_format) {
416+
case WLR_OUTPUT_COLOR_FORMAT_AUTO: color_format_str = "auto"; break;
417+
case WLR_OUTPUT_COLOR_FORMAT_RGB444: color_format_str = "rgb"; break;
418+
case WLR_OUTPUT_COLOR_FORMAT_YCBCR444: color_format_str = "yuv444"; break;
419+
case WLR_OUTPUT_COLOR_FORMAT_YCBCR422: color_format_str = "yuv422"; break;
420+
case WLR_OUTPUT_COLOR_FORMAT_YCBCR420: color_format_str = "yuv420"; break;
421+
}
422+
json_object_object_add(object, "color_format",
423+
json_object_new_string(color_format_str));
413424
}
414425

415426
json_object *ipc_json_describe_disabled_output(struct sway_output *output) {

sway/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ sway_sources = files(
192192
'commands/output/adaptive_sync.c',
193193
'commands/output/allow_tearing.c',
194194
'commands/output/background.c',
195+
'commands/output/color_format.c',
195196
'commands/output/disable.c',
196197
'commands/output/dpms.c',
197198
'commands/output/enable.c',

0 commit comments

Comments
 (0)