From 4668638ee10764e8341c0770d548c0b74de69a8d Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 1 May 2020 23:49:37 +0200 Subject: [PATCH] Handle "show touches" on the device-side Now that the server can access the Android settings and clean up properly, handle the "show touches" option from the server. The initial state is now correctly restored, even on device disconnection. --- README.md | 3 +- app/scrcpy.1 | 2 +- app/src/cli.c | 3 +- app/src/scrcpy.c | 39 +------------------ app/src/server.c | 1 + app/src/server.h | 1 + .../java/com/genymobile/scrcpy/CleanUp.java | 22 +++++++---- .../java/com/genymobile/scrcpy/Options.java | 9 +++++ .../java/com/genymobile/scrcpy/Server.java | 21 ++++++++-- 9 files changed, 50 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 20b777b0..ea5731a7 100644 --- a/README.md +++ b/README.md @@ -429,7 +429,8 @@ device). Android provides this feature in _Developers options_. -_Scrcpy_ provides an option to enable this feature on start and disable on exit: +_Scrcpy_ provides an option to enable this feature on start and restore the +initial value on exit: ```bash scrcpy --show-touches diff --git a/app/scrcpy.1 b/app/scrcpy.1 index 673cd11d..15200460 100644 --- a/app/scrcpy.1 +++ b/app/scrcpy.1 @@ -136,7 +136,7 @@ Turn the device screen off immediately. .TP .B \-t, \-\-show\-touches -Enable "show touches" on start, disable on quit. +Enable "show touches" on start, restore the initial value on exit.. It only shows physical touches (not clicks from scrcpy). diff --git a/app/src/cli.c b/app/src/cli.c index 13351dee..d6d3c63e 100644 --- a/app/src/cli.c +++ b/app/src/cli.c @@ -130,7 +130,8 @@ scrcpy_print_usage(const char *arg0) { " Turn the device screen off immediately.\n" "\n" " -t, --show-touches\n" - " Enable \"show touches\" on start, disable on quit.\n" + " Enable \"show touches\" on start, restore the initial value\n" + " on exit.\n" " It only shows physical touches (not clicks from scrcpy).\n" "\n" " -v, --version\n" diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index d557b208..015dec08 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -229,21 +229,6 @@ event_loop(bool display, bool control) { return false; } -static process_t -set_show_touches_enabled(const char *serial, bool enabled) { - const char *value = enabled ? "1" : "0"; - const char *const adb_cmd[] = { - "shell", "settings", "put", "system", "show_touches", value - }; - return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); -} - -static void -wait_show_touches(process_t process) { - // reap the process, ignore the result - process_check_success(process, "show_touches"); -} - static SDL_LogPriority sdl_priority_from_av_level(int level) { switch (level) { @@ -292,19 +277,12 @@ scrcpy(const struct scrcpy_options *options) { .lock_video_orientation = options->lock_video_orientation, .control = options->control, .display_id = options->display_id, + .show_touches = options->show_touches, }; if (!server_start(&server, options->serial, ¶ms)) { return false; } - process_t proc_show_touches = PROCESS_NONE; - bool show_touches_waited; - if (options->show_touches) { - LOGI("Enable show_touches"); - proc_show_touches = set_show_touches_enabled(options->serial, true); - show_touches_waited = false; - } - bool ret = false; bool fps_counter_initialized = false; @@ -421,11 +399,6 @@ scrcpy(const struct scrcpy_options *options) { } } - if (options->show_touches) { - wait_show_touches(proc_show_touches); - show_touches_waited = true; - } - input_manager.prefer_text = options->prefer_text; ret = event_loop(options->display, options->control); @@ -482,16 +455,6 @@ end: fps_counter_destroy(&fps_counter); } - if (options->show_touches) { - if (!show_touches_waited) { - // wait the process which enabled "show touches" - wait_show_touches(proc_show_touches); - } - LOGI("Disable show_touches"); - proc_show_touches = set_show_touches_enabled(options->serial, false); - wait_show_touches(proc_show_touches); - } - server_destroy(&server); return ret; diff --git a/app/src/server.c b/app/src/server.c index b102f0c2..5399d269 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -268,6 +268,7 @@ execute_server(struct server *server, const struct server_params *params) { "true", // always send frame meta (packet boundaries + timestamp) params->control ? "true" : "false", display_id_string, + params->show_touches ? "true" : "false", }; #ifdef SERVER_DEBUGGER LOGI("Server debugger waiting for a client on device port " diff --git a/app/src/server.h b/app/src/server.h index a2ecdefc..33e99bea 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -51,6 +51,7 @@ struct server_params { int8_t lock_video_orientation; bool control; uint16_t display_id; + bool show_touches; }; // init default values diff --git a/server/src/main/java/com/genymobile/scrcpy/CleanUp.java b/server/src/main/java/com/genymobile/scrcpy/CleanUp.java index ca16cc3d..ccbca275 100644 --- a/server/src/main/java/com/genymobile/scrcpy/CleanUp.java +++ b/server/src/main/java/com/genymobile/scrcpy/CleanUp.java @@ -19,19 +19,18 @@ public final class CleanUp { // not instantiable } - public static void configure() throws IOException { - // TODO - boolean needProcess = false; + public static void configure(boolean disableShowTouches) throws IOException { + boolean needProcess = disableShowTouches; if (needProcess) { - startProcess(); + startProcess(disableShowTouches); } else { // There is no additional clean up to do when scrcpy dies unlinkSelf(); } } - private static void startProcess() throws IOException { - String[] cmd = {"app_process", "/", CleanUp.class.getName()}; + private static void startProcess(boolean disableShowTouches) throws IOException { + String[] cmd = {"app_process", "/", CleanUp.class.getName(), String.valueOf(disableShowTouches)}; ProcessBuilder builder = new ProcessBuilder(cmd); builder.environment().put("CLASSPATH", SERVER_PATH); @@ -57,6 +56,15 @@ public final class CleanUp { } Ln.i("Cleaning up"); - // TODO + + boolean disableShowTouches = Boolean.parseBoolean(args[0]); + + if (disableShowTouches) { + ServiceManager serviceManager = new ServiceManager(); + try (ContentProvider settings = serviceManager.getActivityManager().createSettingsProvider()) { + Ln.i("Disabling \"show touches\""); + settings.putValue(ContentProvider.TABLE_SYSTEM, "show_touches", "0"); + } + } } } diff --git a/server/src/main/java/com/genymobile/scrcpy/Options.java b/server/src/main/java/com/genymobile/scrcpy/Options.java index 2a4bba33..838416c8 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Options.java +++ b/server/src/main/java/com/genymobile/scrcpy/Options.java @@ -12,6 +12,7 @@ public class Options { private boolean sendFrameMeta; // send PTS so that the client may record properly private boolean control; private int displayId; + private boolean showTouches; public int getMaxSize() { return maxSize; @@ -84,4 +85,12 @@ public class Options { public void setDisplayId(int displayId) { this.displayId = displayId; } + + public boolean getShowTouches() { + return showTouches; + } + + public void setShowTouches(boolean showTouches) { + this.showTouches = showTouches; + } } diff --git a/server/src/main/java/com/genymobile/scrcpy/Server.java b/server/src/main/java/com/genymobile/scrcpy/Server.java index 4ac961de..43ab0f55 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Server.java +++ b/server/src/main/java/com/genymobile/scrcpy/Server.java @@ -1,5 +1,7 @@ package com.genymobile.scrcpy; +import com.genymobile.scrcpy.wrappers.ContentProvider; + import android.graphics.Rect; import android.media.MediaCodec; import android.os.Build; @@ -17,7 +19,16 @@ public final class Server { Ln.i("Device: " + Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")"); final Device device = new Device(options); - CleanUp.configure(); + boolean mustDisableShowTouchesOnCleanUp = false; + if (options.getShowTouches()) { + try (ContentProvider settings = device.createSettingsProvider()) { + String oldValue = settings.getAndPutValue(ContentProvider.TABLE_SYSTEM, "show_touches", "1"); + // If "show touches" was disabled, it must be disabled back on clean up + mustDisableShowTouchesOnCleanUp = !"1".equals(oldValue); + } + } + + CleanUp.configure(mustDisableShowTouchesOnCleanUp); boolean tunnelForward = options.isTunnelForward(); try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward)) { @@ -80,8 +91,9 @@ public final class Server { "The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")"); } - if (args.length != 10) { - throw new IllegalArgumentException("Expecting 10 parameters"); + final int expectedParameters = 11; + if (args.length != expectedParameters) { + throw new IllegalArgumentException("Expecting " + expectedParameters + " parameters"); } Options options = new Options(); @@ -114,6 +126,9 @@ public final class Server { int displayId = Integer.parseInt(args[9]); options.setDisplayId(displayId); + boolean showTouches = Boolean.parseBoolean(args[10]); + options.setShowTouches(showTouches); + return options; }