Skip to content

Display and control your Android device

Add option to change the background color

Romain Vimontb4d5dd753bb1
diff --git a/app/data/bash-completion/scrcpy b/app/data/bash-completion/scrcpy
index 0cae593a..e8cae851 100644
--- a/app/data/bash-completion/scrcpy
+++ b/app/data/bash-completion/scrcpy
@@ -12,6 +12,7 @@ _scrcpy() {
--audio-source=
--audio-output-buffer=
-b --video-bit-rate=
+ --background-color=
--camera-ar=
--camera-id=
--camera-facing=
@@ -201,6 +202,7 @@ _scrcpy() {
|--audio-codec-options \
|--audio-encoder \
|--audio-output-buffer \
+ |--background-color \
|--camera-ar \
|--camera-id \
|--camera-fps \
diff --git a/app/data/zsh-completion/_scrcpy b/app/data/zsh-completion/_scrcpy
index 0f320414..8896e738 100644
--- a/app/data/zsh-completion/_scrcpy
+++ b/app/data/zsh-completion/_scrcpy
@@ -19,6 +19,7 @@ arguments=(
'--audio-source=[Select the audio source]:source:(output playback mic mic-unprocessed mic-camcorder mic-voice-recognition mic-voice-communication voice-call voice-call-uplink voice-call-downlink voice-performance)'
'--audio-output-buffer=[Configure the size of the SDL audio output buffer (in milliseconds)]'
{-b,--video-bit-rate=}'[Encode the video at the given bit-rate]'
+ '--background-color=[Set the background color]'
'--camera-ar=[Select the camera size by its aspect ratio]'
'--camera-high-speed=[Enable high-speed camera capture mode]'
'--camera-id=[Specify the camera id to mirror]'
diff --git a/app/scrcpy.1 b/app/scrcpy.1
index 69d814cb..e6518251 100644
--- a/app/scrcpy.1
+++ b/app/scrcpy.1
@@ -97,6 +97,12 @@ Encode the video at the given bit rate, expressed in bits/s. Unit suffixes are s
Default is 8M (8000000).
+.TP
+.BI "\-\-background-color " hexcolor
+Set the background color, encoded as hexadecimal color code (#RGB or #RRGGBB).
+
+Default is #000 (black).
+
.TP
.BI "\-\-camera\-ar " ar
Select the camera size by its aspect ratio (+/- 10%).
diff --git a/app/src/cli.c b/app/src/cli.c
index 39576d8d..155a6019 100644
--- a/app/src/cli.c
+++ b/app/src/cli.c
@@ -106,6 +106,7 @@ enum {
OPT_MIN_SIZE_ALIGNMENT,
OPT_NO_WINDOW_ASPECT_RATIO_LOCK,
OPT_KEEP_ACTIVE,
+ OPT_BACKGROUND_COLOR,
};
struct sc_option {
@@ -253,6 +254,14 @@ static const struct sc_option options[] = {
"Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n"
"Default is 8M (8000000).",
},
+ {
+ .longopt_id = OPT_BACKGROUND_COLOR,
+ .longopt = "background-color",
+ .argdesc = "hexcolor",
+ .text = "Set the background color, encoded as hexadecimal color code "
+ "(#RGB or #RRGGBB).\n"
+ "Default is #000 (black).",
+ },
{
.longopt_id = OPT_CAMERA_AR,
.longopt = "camera-ar",
@@ -2332,6 +2341,74 @@ parse_mouse_bindings(const char *s, struct sc_mouse_bindings *mb) {
return true;
}
+static bool
+parse_hex_char(char c, uint8_t *value) {
+ if (c >= '0' && c <= '9') {
+ *value = c - '0';
+ return true;
+ }
+ if (c >= 'a' && c <= 'f') {
+ *value = c - 'a' + 10;
+ return true;
+ }
+ if (c >= 'A' && c <= 'F') {
+ *value = c - 'A' + 10;
+ return true;
+ }
+ return false;
+}
+
+static bool
+parse_hex_byte(const char *s, uint8_t *value) {
+ uint8_t left, right;
+ bool ok = parse_hex_char(s[0], &left)
+ && parse_hex_char(s[1], &right);
+ if (!ok) {
+ return false;
+ }
+ *value = left << 4 | right;
+ return true;
+}
+
+static bool
+parse_hex_color(const char *s, uint32_t *color) {
+ if (s[0] == '#') {
+ // Accept with and without a leading '#'
+ ++s;
+ }
+
+ size_t len = strlen(s);
+ if (len != 3 && len != 6) {
+ LOGE("Invalid hexadecimal color code (expected #RGB or #RRGGBB): "
+ "%s", s);
+ return false;
+ }
+
+ uint8_t rgb[3];
+ if (len == 3) {
+ for (size_t i = 0; i < 3; ++i) {
+ bool ok = parse_hex_char(s[i], &rgb[i]);
+ if (!ok) {
+ LOGE("Invalid hexadecimal color code: %s", s);
+ return false;
+ }
+ rgb[i] *= 0x11;
+ }
+ } else {
+ assert(len == 6);
+ for (size_t i = 0; i < 3; ++i) {
+ bool ok = parse_hex_byte(&s[2*i], &rgb[i]);
+ if (!ok) {
+ LOGE("Invalid hexadecimal color code: %s", s);
+ return false;
+ }
+ }
+ }
+
+ *color = rgb[0] << 16 | rgb[1] << 8 | rgb[2];
+ return true;
+}
+
static bool
parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
const char *optstring, const struct option *longopts) {
@@ -2768,6 +2845,11 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
case OPT_KEEP_ACTIVE:
opts->keep_active = true;
break;
+ case OPT_BACKGROUND_COLOR:
+ if (!parse_hex_color(optarg, &opts->background_color)) {
+ return false;
+ }
+ break;
default:
// getopt prints the error message on stderr
return false;
diff --git a/app/src/options.c b/app/src/options.c
index 95d02388..d99c65d3 100644
--- a/app/src/options.c
+++ b/app/src/options.c
@@ -64,6 +64,7 @@ const struct scrcpy_options scrcpy_options_default = {
.window_width = 0,
.window_height = 0,
.display_id = 0,
+ .background_color = 0, // black
.video_buffer = 0,
.audio_buffer = -1, // depends on the audio format,
.audio_output_buffer = SC_TICK_FROM_MS(10),
diff --git a/app/src/options.h b/app/src/options.h
index 9a37ec6f..1046bb14 100644
--- a/app/src/options.h
+++ b/app/src/options.h
@@ -274,6 +274,7 @@ struct scrcpy_options {
uint16_t window_width;
uint16_t window_height;
uint32_t display_id;
+ uint32_t background_color;
sc_tick video_buffer;
sc_tick audio_buffer;
sc_tick audio_output_buffer;
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c
index 0e63d2ab..87fdc7ba 100644
--- a/app/src/scrcpy.c
+++ b/app/src/scrcpy.c
@@ -813,6 +813,7 @@ aoa_complete:
.window_y = options->window_y,
.window_width = options->window_width,
.window_height = options->window_height,
+ .background_color = options->background_color,
.window_aspect_ratio_lock = options->window_aspect_ratio_lock,
.window_borderless = options->window_borderless,
.orientation = options->display_orientation,
diff --git a/app/src/screen.c b/app/src/screen.c
index 683d3e5f..4bcb0c08 100644
--- a/app/src/screen.c
+++ b/app/src/screen.c
@@ -218,7 +218,8 @@ sc_screen_render(struct sc_screen *screen, bool update_content_rect) {
}
SDL_Renderer *renderer = screen->renderer;
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
+ struct sc_screen_bg_color bg = screen->bg;
+ SDL_SetRenderDrawColor(renderer, bg.r, bg.g, bg.b, 0);
sc_sdl_render_clear(renderer);
bool ok = false;
@@ -394,6 +395,10 @@ sc_screen_init(struct sc_screen *screen,
screen->camera = params->camera;
screen->window_aspect_ratio_lock = params->window_aspect_ratio_lock;
+ screen->bg.r = (params->background_color >> 16) & 0xFF;
+ screen->bg.g = (params->background_color >> 8) & 0xFF;
+ screen->bg.b = params->background_color & 0xFF;
+
screen->req.x = params->window_x;
screen->req.y = params->window_y;
screen->req.width = params->window_width;
diff --git a/app/src/screen.h b/app/src/screen.h
index e04abefe..68b597d2 100644
--- a/app/src/screen.h
+++ b/app/src/screen.h
@@ -38,6 +38,12 @@ struct sc_screen {
bool camera;
bool window_aspect_ratio_lock;
+ struct sc_screen_bg_color {
+ uint8_t r;
+ uint8_t g;
+ uint8_t b;
+ } bg;
+
struct sc_texture tex;
struct sc_input_manager im;
struct sc_mouse_capture mc; // only used in mouse relative mode
@@ -107,6 +113,8 @@ struct sc_screen_params {
uint16_t window_width;
uint16_t window_height;
+ uint32_t background_color;
+
bool window_aspect_ratio_lock;
bool window_borderless;
diff --git a/doc/window.md b/doc/window.md
index 8e201064..15601fb2 100644
--- a/doc/window.md
+++ b/doc/window.md
@@ -32,6 +32,28 @@ behavior:
scrcpy --no-window-aspect-ratio-lock
```
+## Background color
+
+To maintain the device aspect ratio (when using `--no-window-aspect-ratio-lock`
+or in fullscreen mode), padding is added around the device. By default, it is
+black.
+
+This can be changed:
+
+```bash
+# Use the hexadecimal color code #234567
+# (r, g, b) = (0x23, 0x45, 0x67) = (35, 69, 103)
+scrcpy --fullscreen --background-color=#234567
+
+# The leading '#' is optional
+scrcpy --fullscreen --background-color=234567
+
+# Hexadecimal color shorthand (3-digit form) is supported
+scrcpy --fullscreen --background-color=#567
+# is equivalent to:
+scrcpy --fullscreen --background-color=#556677
+```
+
## Borderless
To disable window decorations: