Skip to content

Commit 5456e0a

Browse files
committed
check adb runnable before starting scrcpy
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]>
1 parent 3b5e542 commit 5456e0a

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

app/src/main.c

+56
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <libavformat/avformat.h>
66
#include <SDL2/SDL.h>
77

8+
#ifdef __linux__
9+
#include <sys/stat.h>
10+
#endif
11+
812
#include "config.h"
913
#include "log.h"
1014

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

262+
static SDL_bool adb_runnable() {
263+
#ifdef __WINDOWS__
264+
// Let windows ok now
265+
return SDL_TRUE;
266+
#elif __linux__
267+
const char *adb_command = getenv("ADB");
268+
const char *path = getenv("PATH");
269+
char *s = NULL;
270+
char *s_head = NULL;
271+
char *p = NULL;
272+
char adb_path[PATH_MAX] = {0};
273+
struct stat st;
274+
SDL_bool runnable = SDL_FALSE;
275+
276+
if (adb_command) {
277+
// if we have ADB env, we try it.
278+
runnable = ((stat(adb_command, &st) == 0) && st.st_mode & S_IXUSR);
279+
} else {
280+
// otherwise, we parse PATH env, and try every possible.
281+
s = SDL_malloc((strlen(path) + 1) * sizeof(char));
282+
if (!s) {
283+
LOGE("Insufficient memory.\n");
284+
return SDL_FALSE;
285+
}
286+
s_head = strcpy(s, path);
287+
do {
288+
memset(adb_path, 0, sizeof(adb_path));
289+
p = strchr(s, ':');
290+
if (p != NULL) {
291+
p[0] = 0;
292+
}
293+
snprintf(adb_path, sizeof(adb_path), "%s/adb", s);
294+
runnable = ((stat(adb_path, &st) == 0) && st.st_mode & S_IXUSR);
295+
s = p + 1;
296+
} while (p != NULL && !runnable);
297+
}
298+
299+
if (s_head) {
300+
free(s_head);
301+
}
302+
return runnable;
303+
#else
304+
// if we don't know what does scrcpy be compiled to, pass it.
305+
return SDL_TRUE;
306+
#endif
307+
}
308+
258309
int main(int argc, char *argv[]) {
259310
#ifdef __WINDOWS__
260311
// disable buffering, we want logs immediately
@@ -286,6 +337,11 @@ int main(int argc, char *argv[]) {
286337
return 0;
287338
}
288339

340+
if (!adb_runnable()) {
341+
LOGE("Missing adb. You need adb, accessible from your PATH to execute %s.\n", argv[0]);
342+
return 1;
343+
}
344+
289345
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
290346
av_register_all();
291347
#endif

0 commit comments

Comments
 (0)