Skip to content

Display and control your Android device

Set terminal tab title to scrcpy and device name

zxmpg415bcfbf6f2b53
diff --git a/app/src/main.c b/app/src/main.c
index 7327e07d..f5012d33 100644
--- a/app/src/main.c
+++ b/app/src/main.c
@@ -16,6 +16,7 @@
#endif
#include "util/log.h"
#include "util/net.h"
+#include "util/term.h"
#include "version.h"
#ifdef _WIN32
@@ -32,6 +33,8 @@ main_scrcpy(int argc, char *argv[]) {
setbuf(stderr, NULL);
#endif
+ sc_term_set_title("scrcpy");
+
printf("scrcpy " SCRCPY_VERSION
" <https://github.com/Genymobile/scrcpy>\n");
@@ -108,6 +111,8 @@ end:
getchar();
}
+ sc_term_set_title("");
+
return ret;
}
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c
index 705390fe..6fa4ba7e 100644
--- a/app/src/scrcpy.c
+++ b/app/src/scrcpy.c
@@ -40,6 +40,8 @@
#include "util/acksync.h"
#include "util/log.h"
#include "util/rand.h"
+#include "util/str.h"
+#include "util/term.h"
#include "util/timeout.h"
#include "util/tick.h"
#ifdef HAVE_V4L2
@@ -304,6 +306,17 @@ init_sdl_gamepads(void) {
SDL_free(joysticks);
}
+static void
+set_terminal_title_with_prefix(const char *value) {
+ char title[128];
+ memcpy(title, "scrcpy - ", 9);
+ size_t trunc_len = sc_str_utf8_truncation_index(value, 128 - 9 - 1);
+ assert(trunc_len <= 128 - 9 - 1);
+ memcpy(&title[9], value, trunc_len);
+ title[9 + trunc_len] = '\0';
+ sc_term_set_title(title);
+}
+
enum scrcpy_exit_code
scrcpy(struct scrcpy_options *options) {
static struct scrcpy scrcpy;
@@ -494,6 +507,12 @@ scrcpy(struct scrcpy_options *options) {
// It is necessarily initialized here, since the device is connected
struct sc_server_info *info = &s->server.info;
+ const char *window_title =
+ options->window_title ? options->window_title : info->device_name;
+ assert(window_title);
+
+ set_terminal_title_with_prefix(window_title);
+
const char *serial = s->server.serial;
assert(serial);
@@ -734,9 +753,6 @@ aoa_complete:
assert(options->control == !!controller);
if (options->window) {
- const char *window_title =
- options->window_title ? options->window_title : info->device_name;
-
struct sc_screen_params screen_params = {
.video = options->video_playback,
.camera = options->video_source == SC_VIDEO_SOURCE_CAMERA,
diff --git a/app/src/usb/scrcpy_otg.c b/app/src/usb/scrcpy_otg.c
index ba2768b3..de1d9fd6 100644
--- a/app/src/usb/scrcpy_otg.c
+++ b/app/src/usb/scrcpy_otg.c
@@ -16,6 +16,8 @@
#include "usb/keyboard_aoa.h"
#include "usb/mouse_aoa.h"
#include "util/log.h"
+#include "util/str.h"
+#include "util/term.h"
struct scrcpy_otg {
struct sc_usb usb;
@@ -58,6 +60,17 @@ event_loop(struct scrcpy_otg *s) {
return SCRCPY_EXIT_FAILURE;
}
+static void
+set_terminal_title_with_prefix(const char *value) {
+ char title[128];
+ memcpy(title, "scrcpy - ", 9);
+ size_t trunc_len = sc_str_utf8_truncation_index(value, 128 - 9 - 1);
+ assert(trunc_len <= 128 - 9 - 1);
+ memcpy(&title[9], value, trunc_len);
+ title[9 + trunc_len] = '\0';
+ sc_term_set_title(title);
+}
+
enum scrcpy_exit_code
scrcpy_otg(struct scrcpy_options *options) {
static struct scrcpy_otg scrcpy_otg;
@@ -179,7 +192,13 @@ scrcpy_otg(struct scrcpy_options *options) {
const char *window_title = options->window_title;
if (!window_title) {
- window_title = usb_device.product ? usb_device.product : "scrcpy";
+ window_title = usb_device.product; // might still be NULL
+ }
+
+ if (window_title) {
+ set_terminal_title_with_prefix(window_title);
+ } else {
+ window_title = "scrcpy";
}
struct sc_screen_params params = {
diff --git a/app/src/util/strbuf.h b/app/src/util/strbuf.h
index 1878df2f..25eb11ff 100644
--- a/app/src/util/strbuf.h
+++ b/app/src/util/strbuf.h
@@ -6,6 +6,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
+#include <sys/types.h>
struct sc_strbuf {
char *s;
@@ -53,6 +54,10 @@ sc_strbuf_append_str(struct sc_strbuf *buf, const char *s) {
return sc_strbuf_append(buf, s, strlen(s));
}
+ssize_t
+sc_strbuf_append_str_truncate_utf8(struct sc_strbuf *buf, const char *utf8,
+ size_t max_len);
+
/**
* Append a static string
*
diff --git a/app/src/util/term.c b/app/src/util/term.c
index ff6bc4b1..72ebc111 100644
--- a/app/src/util/term.c
+++ b/app/src/util/term.c
@@ -1,9 +1,13 @@
#include "term.h"
#include <assert.h>
+#include <stdio.h>
#ifdef _WIN32
# include <windows.h>
+# include <io.h>
+# include "util/log.h"
+# include "util/str.h"
#else
# include <unistd.h>
# include <sys/ioctl.h>
@@ -49,3 +53,34 @@ sc_term_get_size(unsigned *rows, unsigned *cols) {
return true;
#endif
}
+
+static inline bool
+sc_term_stdout_isatty(void) {
+#ifdef _WIN32
+ return _isatty(_fileno(stdout));
+#else
+ return isatty(STDOUT_FILENO);
+#endif
+}
+
+void
+sc_term_set_title(const char *title) {
+ if (!sc_term_stdout_isatty()) {
+ return;
+ }
+
+#ifdef _WIN32
+ wchar_t *wtitle = sc_str_to_wchars(title);
+ if (wtitle) {
+ SetConsoleTitleW(wtitle);
+ free(wtitle);
+ } else {
+ LOGW("Could not convert title to wchar");
+ SetConsoleTitleA(title);
+ }
+#else
+ // Emit an OSC 0 escape sequence recognized by most terminals
+ printf("\033]0;%s\007", title);
+ fflush(stdout);
+#endif
+}
diff --git a/app/src/util/term.h b/app/src/util/term.h
index 0211bcb4..62ed12f6 100644
--- a/app/src/util/term.h
+++ b/app/src/util/term.h
@@ -18,4 +18,10 @@
bool
sc_term_get_size(unsigned *rows, unsigned *cols);
+/**
+ * Set the terminal tab/window title
+ */
+void
+sc_term_set_title(const char *title);
+
#endif