Skip to content

Commit

Permalink
Load ADB value using sc_get_env()
Browse files Browse the repository at this point in the history
Contrary to getenv(), the result of sc_get_env() is encoded in UTF-8 on
all platforms.

Since it is allocated, it requires an explicit init() and destroy()
functions.
  • Loading branch information
rom1v committed Dec 1, 2024
1 parent fd13cb3 commit 8701ee9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
29 changes: 23 additions & 6 deletions app/src/adb/adb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions app/src/adb/adb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 11 additions & 1 deletion app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,21 +485,29 @@ 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;
}

ok = sc_intr_init(&server->intr);
if (!ok) {
sc_cond_destroy(&server->cond_stopped);
sc_mutex_destroy(&server->mutex);
sc_adb_destroy();
return false;
}

Expand Down Expand Up @@ -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();
}
11 changes: 8 additions & 3 deletions app/src/usb/scrcpy_otg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 8701ee9

Please sign in to comment.