diff --git a/app/src/main.c b/app/src/main.c index 7327e07d3e..f5012d33e6 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 " \n"); @@ -108,6 +111,8 @@ main_scrcpy(int argc, char *argv[]) { getchar(); } + sc_term_set_title(""); + return ret; } diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 7738800abc..2fedd7b516 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -40,6 +40,7 @@ #include "util/acksync.h" #include "util/log.h" #include "util/rand.h" +#include "util/term.h" #include "util/timeout.h" #include "util/tick.h" #ifdef HAVE_V4L2 @@ -493,6 +494,14 @@ scrcpy(struct scrcpy_options *options) { const char *serial = s->server.serial; assert(serial); + // Update the terminal tab title now that we know the device name. + // Use --window-title if provided, otherwise fall back to the device name. + const char *title_suffix = + options->window_title ? options->window_title : info->device_name; + char term_title[256]; + snprintf(term_title, sizeof(term_title), "scrcpy - %s", title_suffix); + sc_term_set_title(term_title); + struct sc_file_pusher *fp = NULL; if (options->window && options->control) { @@ -1009,5 +1018,7 @@ scrcpy(struct scrcpy_options *options) { sc_server_destroy(&s->server); + sc_term_set_title(""); + return ret; } diff --git a/app/src/usb/scrcpy_otg.c b/app/src/usb/scrcpy_otg.c index ba2768b315..7c5f4395f4 100644 --- a/app/src/usb/scrcpy_otg.c +++ b/app/src/usb/scrcpy_otg.c @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -16,6 +17,7 @@ #include "usb/keyboard_aoa.h" #include "usb/mouse_aoa.h" #include "util/log.h" +#include "util/term.h" struct scrcpy_otg { struct sc_usb usb; @@ -182,6 +184,13 @@ scrcpy_otg(struct scrcpy_options *options) { window_title = usb_device.product ? usb_device.product : "scrcpy"; } + // Update the terminal tab title now that we know the device name. + if (options->window_title || usb_device.product) { + char term_title[256]; + snprintf(term_title, sizeof(term_title), "scrcpy - %s", window_title); + sc_term_set_title(term_title); + } + struct sc_screen_params params = { .video = false, .camera = false, @@ -270,5 +279,7 @@ scrcpy_otg(struct scrcpy_options *options) { sc_screen_destroy(&s->screen); } + sc_term_set_title(""); + return ret; } diff --git a/app/src/util/term.c b/app/src/util/term.c index ff6bc4b13f..2423a763b7 100644 --- a/app/src/util/term.c +++ b/app/src/util/term.c @@ -1,8 +1,10 @@ #include "term.h" #include +#include #ifdef _WIN32 +# include # include #else # include @@ -49,3 +51,24 @@ sc_term_get_size(unsigned *rows, unsigned *cols) { return true; #endif } + +void +sc_term_set_title(const char *title) { +#ifdef _WIN32 + // Fallback for legacy Windows console hosts (cmd.exe / conhost) that do + // not process VT escape sequences. + SetConsoleTitleA(title); + + if (!_isatty(_fileno(stdout))) { + return; + } +#else + if (!isatty(STDOUT_FILENO)) { + return; + } +#endif + // OSC 0 sets both the icon name and the window/tab title; recognized by + // Windows Terminal, macOS Terminal, xterm, and most modern emulators. + printf("\033]0;%s\007", title); + fflush(stdout); +} diff --git a/app/src/util/term.h b/app/src/util/term.h index 0211bcb4cc..d235bdc6d8 100644 --- a/app/src/util/term.h +++ b/app/src/util/term.h @@ -18,4 +18,18 @@ bool sc_term_get_size(unsigned *rows, unsigned *cols); +/** + * Set the terminal tab/window title. + * + * Emits an OSC 0 escape sequence (\033]0;\007) recognized by Windows + * Terminal, macOS Terminal, xterm, and most modern terminal emulators. + * On Windows, also calls SetConsoleTitleA() as a fallback for legacy hosts + * (cmd.exe, conhost) that do not process VT sequences. + * + * Pass an empty string ("") to clear the title and let the shell restore its + * own title on the next prompt. + */ +void +sc_term_set_title(const char *title); + #endif