|
5 | 5 | #include <libavformat/avformat.h>
|
6 | 6 | #include <SDL2/SDL.h>
|
7 | 7 |
|
| 8 | +#ifdef __linux__ |
| 9 | +#include <sys/stat.h> |
| 10 | +#endif |
| 11 | + |
8 | 12 | #include "config.h"
|
9 | 13 | #include "log.h"
|
10 | 14 |
|
@@ -255,6 +259,53 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
|
255 | 259 | return SDL_TRUE;
|
256 | 260 | }
|
257 | 261 |
|
| 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 | + |
258 | 309 | int main(int argc, char *argv[]) {
|
259 | 310 | #ifdef __WINDOWS__
|
260 | 311 | // disable buffering, we want logs immediately
|
@@ -286,6 +337,11 @@ int main(int argc, char *argv[]) {
|
286 | 337 | return 0;
|
287 | 338 | }
|
288 | 339 |
|
| 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 | + |
289 | 345 | #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
|
290 | 346 | av_register_all();
|
291 | 347 | #endif
|
|
0 commit comments