From 25296ae16710c0c7bf69e60f4e1aa952e0443075 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 10 Feb 2022 08:42:15 +0100 Subject: [PATCH] Kill adb daemon in OTG mode on Windows On Windows, it is not possible to open a USB device from several process, so HID events may only work if no adb daemon is running. PR #3011 --- app/src/adb/adb.c | 8 ++++++++ app/src/adb/adb.h | 3 +++ app/src/usb/scrcpy_otg.c | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/app/src/adb/adb.c b/app/src/adb/adb.c index 8742c4d0..4ddb93f3 100644 --- a/app/src/adb/adb.c +++ b/app/src/adb/adb.c @@ -204,6 +204,14 @@ sc_adb_start_server(struct sc_intr *intr, unsigned flags) { return process_check_success_intr(intr, pid, "adb start-server", flags); } +bool +sc_adb_kill_server(struct sc_intr *intr, unsigned flags) { + const char *const argv[] = SC_ADB_COMMAND("kill-server"); + + sc_pid pid = sc_adb_execute(argv, flags); + return process_check_success_intr(intr, pid, "adb kill-server", flags); +} + bool sc_adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port, const char *device_socket_name, unsigned flags) { diff --git a/app/src/adb/adb.h b/app/src/adb/adb.h index 6ea6e897..10e8f293 100644 --- a/app/src/adb/adb.h +++ b/app/src/adb/adb.h @@ -36,6 +36,9 @@ sc_adb_execute(const char *const argv[], unsigned flags); bool sc_adb_start_server(struct sc_intr *intr, unsigned flags); +bool +sc_adb_kill_server(struct sc_intr *intr, unsigned flags); + bool sc_adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port, const char *device_socket_name, unsigned flags); diff --git a/app/src/usb/scrcpy_otg.c b/app/src/usb/scrcpy_otg.c index 1eaa2537..1c53410e 100644 --- a/app/src/usb/scrcpy_otg.c +++ b/app/src/usb/scrcpy_otg.c @@ -2,6 +2,7 @@ #include +#include "adb/adb.h" #include "events.h" #include "screen_otg.h" #include "util/log.h" @@ -75,6 +76,15 @@ scrcpy_otg(struct scrcpy_options *options) { bool aoa_started = false; bool aoa_initialized = false; +#ifdef _WIN32 + // On Windows, only one process could open a USB device + // + LOGI("Killing adb daemon (if any)..."); + unsigned flags = SC_ADB_NO_STDOUT | SC_ADB_NO_STDERR | SC_ADB_NO_LOGERR; + // uninterruptible (intr == NULL), but in practice it's very quick + sc_adb_kill_server(NULL, flags); +#endif + static const struct sc_usb_callbacks cbs = { .on_disconnected = sc_usb_on_disconnected, };