From 66ec2528930ef85d238152d95d9b220b70c94f11 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 25 Mar 2018 15:23:00 +0200 Subject: [PATCH] Add an option to enable "show touches" Add -t/--show-touches option to show physical touches while scrcpy is running. See . --- README.md | 6 ++++++ app/src/command.c | 3 +-- app/src/common.h | 1 + app/src/main.c | 27 +++++++++++++++++++-------- app/src/scrcpy.c | 21 ++++++++++++++++++++- app/src/scrcpy.h | 1 + 6 files changed, 48 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 761f520f..fa55b92f 100644 --- a/README.md +++ b/README.md @@ -247,6 +247,12 @@ If several devices are listed in `adb devices`, you must specify the _serial_: scrcpy -s 0123456789abcdef ``` +To show physical touches while scrcpy is running: + +```bash +scrcpy -t +``` + To run without installing: ```bash diff --git a/app/src/command.c b/app/src/command.c index 246d1515..2bbea4ba 100644 --- a/app/src/command.c +++ b/app/src/command.c @@ -4,10 +4,9 @@ #include #include +#include "common.h" #include "log.h" -#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) - static const char *adb_command; static inline const char *get_adb_command() { diff --git a/app/src/common.h b/app/src/common.h index 2b5c0238..9f5f9c6e 100644 --- a/app/src/common.h +++ b/app/src/common.h @@ -3,6 +3,7 @@ #include +#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) #define MIN(X,Y) (X) < (Y) ? (X) : (Y) #define MAX(X,Y) (X) > (Y) ? (X) : (Y) diff --git a/app/src/main.c b/app/src/main.c index dcb1a841..6b972772 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -12,6 +12,7 @@ struct args { const char *serial; SDL_bool help; SDL_bool version; + SDL_bool show_touches; Uint16 port; Uint16 max_size; Uint32 bit_rate; @@ -45,6 +46,10 @@ static void usage(const char *arg0) { " The device serial number. Mandatory only if several devices\n" " are connected to adb.\n" "\n" + " -t, --show-touches\n" + " Enable \"show touches\" on start, disable on quit.\n" + " It only shows physical touches (not clicks from scrcpy).\n" + "\n" " -v, --version\n" " Print the version of scrcpy.\n" "\n" @@ -183,16 +188,17 @@ static SDL_bool parse_port(char *optarg, Uint16 *port) { static SDL_bool parse_args(struct args *args, int argc, char *argv[]) { static const struct option long_options[] = { - {"bit-rate", required_argument, NULL, 'b'}, - {"help", no_argument, NULL, 'h'}, - {"max-size", required_argument, NULL, 'm'}, - {"port", required_argument, NULL, 'p'}, - {"serial", required_argument, NULL, 's'}, - {"version", no_argument, NULL, 'v'}, - {NULL, 0, NULL, 0 }, + {"bit-rate", required_argument, NULL, 'b'}, + {"help", no_argument, NULL, 'h'}, + {"max-size", required_argument, NULL, 'm'}, + {"port", required_argument, NULL, 'p'}, + {"serial", required_argument, NULL, 's'}, + {"show-touches", no_argument, NULL, 't'}, + {"version", no_argument, NULL, 'v'}, + {NULL, 0, NULL, 0 }, }; int c; - while ((c = getopt_long(argc, argv, "b:hm:p:s:v", long_options, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "b:hm:p:s:tv", long_options, NULL)) != -1) { switch (c) { case 'b': if (!parse_bit_rate(optarg, &args->bit_rate)) { @@ -215,6 +221,9 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) { case 's': args->serial = optarg; break; + case 't': + args->show_touches = SDL_TRUE; + break; case 'v': args->version = SDL_TRUE; break; @@ -243,6 +252,7 @@ int main(int argc, char *argv[]) { .serial = NULL, .help = SDL_FALSE, .version = SDL_FALSE, + .show_touches = SDL_FALSE, .port = DEFAULT_LOCAL_PORT, .max_size = DEFAULT_MAX_SIZE, .bit_rate = DEFAULT_BIT_RATE, @@ -276,6 +286,7 @@ int main(int argc, char *argv[]) { .port = args.port, .max_size = args.max_size, .bit_rate = args.bit_rate, + .show_touches = args.show_touches, }; int res = scrcpy(&options) ? 0 : 1; diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 7df07906..6436afc7 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -106,6 +106,15 @@ static void event_loop(void) { } } +static SDL_bool set_show_touches_enabled(const char *serial, SDL_bool enabled) { + const char *value = enabled ? "1" : "0"; + const char *const adb_cmd[] = { + "shell", "settings", "put", "system", "show_touches", value + }; + process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); + return process_check_success(proc, "show_touches"); +} + SDL_bool scrcpy(const struct scrcpy_options *options) { if (!server_start(&server, options->serial, options->port, options->max_size, options->bit_rate)) { @@ -173,9 +182,19 @@ SDL_bool scrcpy(const struct scrcpy_options *options) { goto finally_stop_and_join_controller; } - event_loop(); + if (options->show_touches) { + LOGI("Enable show_touches"); + set_show_touches_enabled(options->serial, SDL_TRUE); + } + event_loop(); LOGD("quit..."); + + if (options->show_touches) { + LOGI("Disable show_touches"); + set_show_touches_enabled(options->serial, SDL_FALSE); + } + screen_destroy(&screen); finally_stop_and_join_controller: controller_stop(&controller); diff --git a/app/src/scrcpy.h b/app/src/scrcpy.h index f4b4cc90..583cd608 100644 --- a/app/src/scrcpy.h +++ b/app/src/scrcpy.h @@ -8,6 +8,7 @@ struct scrcpy_options { Uint16 port; Uint16 max_size; Uint32 bit_rate; + SDL_bool show_touches; }; SDL_bool scrcpy(const struct scrcpy_options *options);