Skip to content

Display and control your Android device

Load ADB value using sc_get_env()

Romain Vimontbeee42fb065a
diff --git a/app/src/adb/adb.c b/app/src/adb/adb.c
index b3e90b2f..4bb209be 100644
--- a/app/src/adb/adb.c
+++ b/app/src/adb/adb.c
@@ -7,6 +7,7 @@
#include "adb_device.h"
#include "adb_parser.h"
+#include "util/env.h"
#include "util/file.h"
#include "util/log.h"
#include "util/process_intr.h"
@@ -24,15 +25,31 @@
*/
#define SC_ADB_COMMAND(...) { sc_adb_get_executable(), __VA_ARGS__, NULL }
-static const char *adb_executable;
+static char *adb_executable;
-const char *
-sc_adb_get_executable(void) {
+bool
+sc_adb_init(void) {
+ adb_executable = sc_get_env("ADB");
+ if (adb_executable) {
+ return true;
+ }
+
+ adb_executable = strdup("adb");
if (!adb_executable) {
- adb_executable = getenv("ADB");
- if (!adb_executable)
- adb_executable = "adb";
+ LOG_OOM();
+ return false;
}
+
+ return true;
+}
+
+void
+sc_adb_destroy(void) {
+ free(adb_executable);
+}
+
+const char *
+sc_adb_get_executable(void) {
return adb_executable;
}
diff --git a/app/src/adb/adb.h b/app/src/adb/adb.h
index 0292dea1..43310fb9 100644
--- a/app/src/adb/adb.h
+++ b/app/src/adb/adb.h
@@ -15,6 +15,12 @@
#define SC_ADB_SILENT (SC_ADB_NO_STDOUT | SC_ADB_NO_STDERR | SC_ADB_NO_LOGERR)
+bool
+sc_adb_init(void);
+
+void
+sc_adb_destroy(void);
+
const char *
sc_adb_get_executable(void);
diff --git a/app/src/server.c b/app/src/server.c
index fe55baa2..923b5671 100644
--- a/app/src/server.c
+++ b/app/src/server.c
@@ -485,14 +485,21 @@ sc_server_init(struct sc_server *server, const struct sc_server_params *params,
// end of the program
server->params = *params;
- bool ok = sc_mutex_init(&server->mutex);
+ bool ok = sc_adb_init();
if (!ok) {
return false;
}
+ ok = sc_mutex_init(&server->mutex);
+ if (!ok) {
+ sc_adb_destroy();
+ return false;
+ }
+
ok = sc_cond_init(&server->cond_stopped);
if (!ok) {
sc_mutex_destroy(&server->mutex);
+ sc_adb_destroy();
return false;
}
@@ -500,6 +507,7 @@ sc_server_init(struct sc_server *server, const struct sc_server_params *params,
if (!ok) {
sc_cond_destroy(&server->cond_stopped);
sc_mutex_destroy(&server->mutex);
+ sc_adb_destroy();
return false;
}
@@ -1141,4 +1149,6 @@ sc_server_destroy(struct sc_server *server) {
sc_intr_destroy(&server->intr);
sc_cond_destroy(&server->cond_stopped);
sc_mutex_destroy(&server->mutex);
+
+ sc_adb_destroy();
}
diff --git a/app/src/usb/scrcpy_otg.c b/app/src/usb/scrcpy_otg.c
index 1a7e9544..6ef2fc2a 100644
--- a/app/src/usb/scrcpy_otg.c
+++ b/app/src/usb/scrcpy_otg.c
@@ -95,9 +95,14 @@ scrcpy_otg(struct scrcpy_options *options) {
// On Windows, only one process could open a USB device
// <https://github.com/Genymobile/scrcpy/issues/2773>
LOGI("Killing adb server (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);
+ if (sc_adb_init()) {
+ 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);
+ sc_adb_destroy();
+ } else {
+ LOGW("Could not call adb executable, adb server not killed");
+ }
#endif
static const struct sc_usb_callbacks cbs = {