Display and control your Android device
Split command into process and adb
Romain Vimont4bd9da4c93d3
diff --git a/app/meson.build b/app/meson.buildindex 28b9d141..d031e5cc 100644--- a/app/meson.build+++ b/app/meson.build@@ -1,7 +1,7 @@src = ['src/main.c',+ 'src/adb.c','src/cli.c',- 'src/command.c','src/control_msg.c','src/controller.c','src/decoder.c',@@ -21,6 +21,7 @@ src = ['src/tiny_xpm.c','src/video_buffer.c','src/util/net.c',+ 'src/util/process.c','src/util/str_util.c']@@ -76,10 +77,10 @@ endifcc = meson.get_compiler('c')if host_machine.system() == 'windows'- src += [ 'src/sys/win/command.c' ]+ src += [ 'src/sys/win/process.c' ]dependencies += cc.find_library('ws2_32')else- src += [ 'src/sys/unix/command.c' ]+ src += [ 'src/sys/unix/process.c' ]endifconf = configuration_data()diff --git a/app/src/command.c b/app/src/adb.csimilarity index 90%rename from app/src/command.crename to app/src/adb.cindex 81047b7a..339c854d 100644--- a/app/src/command.c+++ b/app/src/adb.c@@ -1,4 +1,4 @@-#include "command.h"+#include "adb.h"#include <assert.h>#include <stdio.h>@@ -70,7 +70,7 @@ show_adb_installation_msg() {{"pacman", "pacman -S android-tools"},};for (size_t i = 0; i < ARRAY_LEN(pkg_managers); ++i) {- if (cmd_search(pkg_managers[i].binary)) {+ if (search_executable(pkg_managers[i].binary)) {LOGI("You may install 'adb' by \"%s\"", pkg_managers[i].command);return;}@@ -118,7 +118,7 @@ adb_execute(const char *serial, const char *const adb_cmd[], size_t len) {memcpy(&cmd[i], adb_cmd, len * sizeof(const char *));cmd[len + i] = NULL;- enum process_result r = cmd_execute(cmd, &process);+ enum process_result r = process_execute(cmd, &process);if (r != PROCESS_SUCCESS) {show_adb_err_msg(r, cmd);return PROCESS_NONE;@@ -211,21 +211,3 @@ adb_install(const char *serial, const char *local) {return proc;}--bool-process_check_success(process_t proc, const char *name) {- if (proc == PROCESS_NONE) {- LOGE("Could not execute \"%s\"", name);- return false;- }- exit_code_t exit_code;- if (!cmd_simple_wait(proc, &exit_code)) {- if (exit_code != NO_EXIT_CODE) {- LOGE("\"%s\" returned with value %" PRIexitcode, name, exit_code);- } else {- LOGE("\"%s\" exited unexpectedly", name);- }- return false;- }- return true;-}diff --git a/app/src/adb.h b/app/src/adb.hnew file mode 100644index 00000000..52cdf823--- /dev/null+++ b/app/src/adb.h@@ -0,0 +1,34 @@+#ifndef SC_ADB_H+#define SC_ADB_H++#include <stdbool.h>+#include <inttypes.h>++#include "config.h"++#include "util/process.h"++process_t+adb_execute(const char *serial, const char *const adb_cmd[], size_t len);++process_t+adb_forward(const char *serial, uint16_t local_port,+ const char *device_socket_name);++process_t+adb_forward_remove(const char *serial, uint16_t local_port);++process_t+adb_reverse(const char *serial, const char *device_socket_name,+ uint16_t local_port);++process_t+adb_reverse_remove(const char *serial, const char *device_socket_name);++process_t+adb_push(const char *serial, const char *local, const char *remote);++process_t+adb_install(const char *serial, const char *local);++#endifdiff --git a/app/src/file_handler.c b/app/src/file_handler.cindex ba689404..502b3e96 100644--- a/app/src/file_handler.c+++ b/app/src/file_handler.c@@ -4,7 +4,7 @@#include <string.h>#include "config.h"-#include "command.h"+#include "adb.h"#include "util/lock.h"#include "util/log.h"@@ -176,10 +176,10 @@ file_handler_stop(struct file_handler *file_handler) {file_handler->stopped = true;cond_signal(file_handler->event_cond);if (file_handler->current_process != PROCESS_NONE) {- if (!cmd_terminate(file_handler->current_process)) {+ if (!process_terminate(file_handler->current_process)) {LOGW("Could not terminate install process");}- cmd_simple_wait(file_handler->current_process, NULL);+ process_simple_wait(file_handler->current_process, NULL);file_handler->current_process = PROCESS_NONE;}mutex_unlock(file_handler->mutex);diff --git a/app/src/file_handler.h b/app/src/file_handler.hindex 078d0ca5..71b58952 100644--- a/app/src/file_handler.h+++ b/app/src/file_handler.h@@ -6,7 +6,7 @@#include <SDL2/SDL_thread.h>#include "config.h"-#include "command.h"+#include "adb.h"#include "util/cbuf.h"typedef enum {diff --git a/app/src/scrcpy.c b/app/src/scrcpy.cindex 3d043b95..2d593234 100644--- a/app/src/scrcpy.c+++ b/app/src/scrcpy.c@@ -14,7 +14,6 @@#endif#include "config.h"-#include "command.h"#include "common.h"#include "compat.h"#include "controller.h"diff --git a/app/src/server.c b/app/src/server.cindex 217e7e41..2766b0e1 100644--- a/app/src/server.c+++ b/app/src/server.c@@ -10,7 +10,7 @@#include <SDL2/SDL_platform.h>#include "config.h"-#include "command.h"+#include "adb.h"#include "util/lock.h"#include "util/log.h"#include "util/net.h"@@ -392,7 +392,7 @@ server_init(struct server *server) {static intrun_wait_server(void *data) {struct server *server = data;- cmd_simple_wait(server->process, NULL); // ignore exit code+ process_simple_wait(server->process, NULL); // ignore exit codemutex_lock(server->mutex);server->process_terminated = true;@@ -447,8 +447,8 @@ server_start(struct server *server, const char *serial,server->wait_server_thread =SDL_CreateThread(run_wait_server, "wait-server", server);if (!server->wait_server_thread) {- cmd_terminate(server->process);- cmd_simple_wait(server->process, NULL); // ignore exit code+ process_terminate(server->process);+ process_simple_wait(server->process, NULL); // ignore exit codegoto error2;}@@ -554,7 +554,7 @@ server_stop(struct server *server) {// the process is already terminated, and the PID assigned to a new// process.LOGW("Killing the server...");- cmd_terminate(server->process);+ process_terminate(server->process);}SDL_WaitThread(server->wait_server_thread, NULL);diff --git a/app/src/server.h b/app/src/server.hindex c97a9153..48d8cd32 100644--- a/app/src/server.h+++ b/app/src/server.h@@ -7,7 +7,7 @@#include <SDL2/SDL_thread.h>#include "config.h"-#include "command.h"+#include "adb.h"#include "common.h"#include "scrcpy.h"#include "util/log.h"diff --git a/app/src/sys/unix/command.c b/app/src/sys/unix/process.csimilarity index 95%rename from app/src/sys/unix/command.crename to app/src/sys/unix/process.cindex c4c262e4..d029319d 100644--- a/app/src/sys/unix/command.c+++ b/app/src/sys/unix/process.c@@ -9,7 +9,7 @@# define _DARWIN_C_SOURCE // for strdup(), strtok_r(), memset_pattern4()#endif-#include "command.h"+#include "util/process.h"#include "config.h"@@ -27,7 +27,7 @@#include "util/log.h"bool-cmd_search(const char *file) {+search_executable(const char *file) {char *path = getenv("PATH");if (!path)return false;@@ -63,7 +63,7 @@ cmd_search(const char *file) {}enum process_result-cmd_execute(const char *const argv[], pid_t *pid) {+process_execute(const char *const argv[], pid_t *pid) {int fd[2];if (pipe(fd) == -1) {@@ -125,7 +125,7 @@ end:}bool-cmd_terminate(pid_t pid) {+process_terminate(pid_t pid) {if (pid <= 0) {LOGC("Requested to kill %d, this is an error. Please report the bug.\n",(int) pid);@@ -135,7 +135,7 @@ cmd_terminate(pid_t pid) {}bool-cmd_simple_wait(pid_t pid, int *exit_code) {+process_simple_wait(pid_t pid, int *exit_code) {int status;int code;if (waitpid(pid, &status, 0) == -1 || !WIFEXITED(status)) {diff --git a/app/src/sys/win/command.c b/app/src/sys/win/process.csimilarity index 93%rename from app/src/sys/win/command.crename to app/src/sys/win/process.cindex 7b483300..da776e8c 100644--- a/app/src/sys/win/command.c+++ b/app/src/sys/win/process.c@@ -1,4 +1,4 @@-#include "command.h"+#include "util/process.h"#include <sys/stat.h>@@ -21,7 +21,7 @@ build_cmd(char *cmd, size_t len, const char *const argv[]) {}enum process_result-cmd_execute(const char *const argv[], HANDLE *handle) {+process_execute(const char *const argv[], HANDLE *handle) {STARTUPINFOW si;PROCESS_INFORMATION pi;memset(&si, 0, sizeof(si));@@ -55,12 +55,12 @@ cmd_execute(const char *const argv[], HANDLE *handle) {}bool-cmd_terminate(HANDLE handle) {+process_terminate(HANDLE handle) {return TerminateProcess(handle, 1);}bool-cmd_simple_wait(HANDLE handle, DWORD *exit_code) {+process_simple_wait(HANDLE handle, DWORD *exit_code) {DWORD code;if (WaitForSingleObject(handle, INFINITE) != WAIT_OBJECT_0|| !GetExitCodeProcess(handle, &code)) {diff --git a/app/src/util/process.c b/app/src/util/process.cnew file mode 100644index 00000000..e4eb8282--- /dev/null+++ b/app/src/util/process.c@@ -0,0 +1,21 @@+#include "process.h"++#include "log.h"++bool+process_check_success(process_t proc, const char *name) {+ if (proc == PROCESS_NONE) {+ LOGE("Could not execute \"%s\"", name);+ return false;+ }+ exit_code_t exit_code;+ if (!process_simple_wait(proc, &exit_code)) {+ if (exit_code != NO_EXIT_CODE) {+ LOGE("\"%s\" returned with value %" PRIexitcode, name, exit_code);+ } else {+ LOGE("\"%s\" exited unexpectedly", name);+ }+ return false;+ }+ return true;+}diff --git a/app/src/command.h b/app/src/util/process.hsimilarity index 59%rename from app/src/command.hrename to app/src/util/process.hindex 7035139b..1e6f256b 100644--- a/app/src/command.h+++ b/app/src/util/process.h@@ -1,8 +1,9 @@-#ifndef COMMAND_H-#define COMMAND_H+#ifndef SC_PROCESS_H+#define SC_PROCESS_H#include <stdbool.h>-#include <inttypes.h>++#include "config.h"#ifdef _WIN32@@ -31,56 +32,35 @@#endif-#include "config.h"-enum process_result {PROCESS_SUCCESS,PROCESS_ERROR_GENERIC,PROCESS_ERROR_MISSING_BINARY,};-#ifndef __WINDOWS__-bool-cmd_search(const char *file);-#endif-+// execute the command and write the result to the output parameter "process"enum process_result-cmd_execute(const char *const argv[], process_t *process);+process_execute(const char *const argv[], process_t *process);+// kill the processbool-cmd_terminate(process_t pid);+process_terminate(process_t pid);+// wait and close the processbool-cmd_simple_wait(process_t pid, exit_code_t *exit_code);--process_t-adb_execute(const char *serial, const char *const adb_cmd[], size_t len);--process_t-adb_forward(const char *serial, uint16_t local_port,- const char *device_socket_name);--process_t-adb_forward_remove(const char *serial, uint16_t local_port);--process_t-adb_reverse(const char *serial, const char *device_socket_name,- uint16_t local_port);--process_t-adb_reverse_remove(const char *serial, const char *device_socket_name);--process_t-adb_push(const char *serial, const char *local, const char *remote);--process_t-adb_install(const char *serial, const char *local);+process_simple_wait(process_t pid, exit_code_t *exit_code);// convenience function to wait for a successful process execution// automatically log process errors with the provided process nameboolprocess_check_success(process_t proc, const char *name);+#ifndef _WIN32+// only used to find package manager, not implemented for Windows+bool+search_executable(const char *file);+#endif+// return the absolute path of the executable (the scrcpy binary)// may be NULL on error; to be freed by SDL_freechar *