Skip to content

Commit

Permalink
check adb runnable before starting scrcpy
Browse files Browse the repository at this point in the history
There are many user who encounters missing adb.
To stop things happens again, we check it and show
sexy response to user.

Signed-off-by: yuchenlin <[email protected]>
  • Loading branch information
npes87184 committed Aug 21, 2018
1 parent 3b5e542 commit 5456e0a
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <libavformat/avformat.h>
#include <SDL2/SDL.h>

#ifdef __linux__
#include <sys/stat.h>
#endif

#include "config.h"
#include "log.h"

Expand Down Expand Up @@ -255,6 +259,53 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
return SDL_TRUE;
}

static SDL_bool adb_runnable() {
#ifdef __WINDOWS__
// Let windows ok now
return SDL_TRUE;
#elif __linux__
const char *adb_command = getenv("ADB");
const char *path = getenv("PATH");
char *s = NULL;
char *s_head = NULL;
char *p = NULL;
char adb_path[PATH_MAX] = {0};
struct stat st;
SDL_bool runnable = SDL_FALSE;

if (adb_command) {
// if we have ADB env, we try it.
runnable = ((stat(adb_command, &st) == 0) && st.st_mode & S_IXUSR);
} else {
// otherwise, we parse PATH env, and try every possible.
s = SDL_malloc((strlen(path) + 1) * sizeof(char));
if (!s) {
LOGE("Insufficient memory.\n");
return SDL_FALSE;
}
s_head = strcpy(s, path);
do {
memset(adb_path, 0, sizeof(adb_path));
p = strchr(s, ':');
if (p != NULL) {
p[0] = 0;
}
snprintf(adb_path, sizeof(adb_path), "%s/adb", s);
runnable = ((stat(adb_path, &st) == 0) && st.st_mode & S_IXUSR);
s = p + 1;
} while (p != NULL && !runnable);
}

if (s_head) {
free(s_head);
}
return runnable;
#else
// if we don't know what does scrcpy be compiled to, pass it.
return SDL_TRUE;
#endif
}

int main(int argc, char *argv[]) {
#ifdef __WINDOWS__
// disable buffering, we want logs immediately
Expand Down Expand Up @@ -286,6 +337,11 @@ int main(int argc, char *argv[]) {
return 0;
}

if (!adb_runnable()) {
LOGE("Missing adb. You need adb, accessible from your PATH to execute %s.\n", argv[0]);
return 1;
}

#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
av_register_all();
#endif
Expand Down

0 comments on commit 5456e0a

Please sign in to comment.