Add an option to enable "show touches"

Add -t/--show-touches option to show physical touches while scrcpy is
running.

See <https://github.com/Genymobile/scrcpy/issues/96>.
This commit is contained in:
Romain Vimont 2018-03-25 15:23:00 +02:00
parent b13d25b9f4
commit 66ec252893
6 changed files with 48 additions and 11 deletions

View file

@ -247,6 +247,12 @@ If several devices are listed in `adb devices`, you must specify the _serial_:
scrcpy -s 0123456789abcdef scrcpy -s 0123456789abcdef
``` ```
To show physical touches while scrcpy is running:
```bash
scrcpy -t
```
To run without installing: To run without installing:
```bash ```bash

View file

@ -4,10 +4,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "common.h"
#include "log.h" #include "log.h"
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
static const char *adb_command; static const char *adb_command;
static inline const char *get_adb_command() { static inline const char *get_adb_command() {

View file

@ -3,6 +3,7 @@
#include <SDL2/SDL_stdinc.h> #include <SDL2/SDL_stdinc.h>
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
#define MIN(X,Y) (X) < (Y) ? (X) : (Y) #define MIN(X,Y) (X) < (Y) ? (X) : (Y)
#define MAX(X,Y) (X) > (Y) ? (X) : (Y) #define MAX(X,Y) (X) > (Y) ? (X) : (Y)

View file

@ -12,6 +12,7 @@ struct args {
const char *serial; const char *serial;
SDL_bool help; SDL_bool help;
SDL_bool version; SDL_bool version;
SDL_bool show_touches;
Uint16 port; Uint16 port;
Uint16 max_size; Uint16 max_size;
Uint32 bit_rate; Uint32 bit_rate;
@ -45,6 +46,10 @@ static void usage(const char *arg0) {
" The device serial number. Mandatory only if several devices\n" " The device serial number. Mandatory only if several devices\n"
" are connected to adb.\n" " are connected to adb.\n"
"\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" " -v, --version\n"
" Print the version of scrcpy.\n" " Print the version of scrcpy.\n"
"\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 SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
static const struct option long_options[] = { static const struct option long_options[] = {
{"bit-rate", required_argument, NULL, 'b'}, {"bit-rate", required_argument, NULL, 'b'},
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"max-size", required_argument, NULL, 'm'}, {"max-size", required_argument, NULL, 'm'},
{"port", required_argument, NULL, 'p'}, {"port", required_argument, NULL, 'p'},
{"serial", required_argument, NULL, 's'}, {"serial", required_argument, NULL, 's'},
{"version", no_argument, NULL, 'v'}, {"show-touches", no_argument, NULL, 't'},
{NULL, 0, NULL, 0 }, {"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0 },
}; };
int c; 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) { switch (c) {
case 'b': case 'b':
if (!parse_bit_rate(optarg, &args->bit_rate)) { 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': case 's':
args->serial = optarg; args->serial = optarg;
break; break;
case 't':
args->show_touches = SDL_TRUE;
break;
case 'v': case 'v':
args->version = SDL_TRUE; args->version = SDL_TRUE;
break; break;
@ -243,6 +252,7 @@ int main(int argc, char *argv[]) {
.serial = NULL, .serial = NULL,
.help = SDL_FALSE, .help = SDL_FALSE,
.version = SDL_FALSE, .version = SDL_FALSE,
.show_touches = SDL_FALSE,
.port = DEFAULT_LOCAL_PORT, .port = DEFAULT_LOCAL_PORT,
.max_size = DEFAULT_MAX_SIZE, .max_size = DEFAULT_MAX_SIZE,
.bit_rate = DEFAULT_BIT_RATE, .bit_rate = DEFAULT_BIT_RATE,
@ -276,6 +286,7 @@ int main(int argc, char *argv[]) {
.port = args.port, .port = args.port,
.max_size = args.max_size, .max_size = args.max_size,
.bit_rate = args.bit_rate, .bit_rate = args.bit_rate,
.show_touches = args.show_touches,
}; };
int res = scrcpy(&options) ? 0 : 1; int res = scrcpy(&options) ? 0 : 1;

View file

@ -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) { SDL_bool scrcpy(const struct scrcpy_options *options) {
if (!server_start(&server, options->serial, options->port, if (!server_start(&server, options->serial, options->port,
options->max_size, options->bit_rate)) { options->max_size, options->bit_rate)) {
@ -173,9 +182,19 @@ SDL_bool scrcpy(const struct scrcpy_options *options) {
goto finally_stop_and_join_controller; 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..."); LOGD("quit...");
if (options->show_touches) {
LOGI("Disable show_touches");
set_show_touches_enabled(options->serial, SDL_FALSE);
}
screen_destroy(&screen); screen_destroy(&screen);
finally_stop_and_join_controller: finally_stop_and_join_controller:
controller_stop(&controller); controller_stop(&controller);

View file

@ -8,6 +8,7 @@ struct scrcpy_options {
Uint16 port; Uint16 port;
Uint16 max_size; Uint16 max_size;
Uint32 bit_rate; Uint32 bit_rate;
SDL_bool show_touches;
}; };
SDL_bool scrcpy(const struct scrcpy_options *options); SDL_bool scrcpy(const struct scrcpy_options *options);