mirror of
https://github.com/Genymobile/scrcpy.git
synced 2026-03-16 17:14:26 +01:00
Compare commits
14 Commits
settings
...
adb_serial
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e512b5bd69 | ||
|
|
b7328a75e3 | ||
|
|
7516c0d1c5 | ||
|
|
6b4761820f | ||
|
|
e83562a6fe | ||
|
|
921044e8a4 | ||
|
|
b49f4e795e | ||
|
|
3eb8202adf | ||
|
|
83f66ea06c | ||
|
|
3cc0921e61 | ||
|
|
d8358ce857 | ||
|
|
4bc5baeeb5 | ||
|
|
0672f7e405 | ||
|
|
6d6a436563 |
@@ -111,7 +111,7 @@ show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {
|
||||
|
||||
sc_pid
|
||||
adb_execute_p(const char *serial, const char *const adb_cmd[],
|
||||
size_t len, sc_pipe *pin, sc_pipe *pout, sc_pipe *perr) {
|
||||
size_t len, sc_pipe *pout) {
|
||||
int i;
|
||||
sc_pid pid;
|
||||
|
||||
@@ -132,7 +132,7 @@ adb_execute_p(const char *serial, const char *const adb_cmd[],
|
||||
memcpy(&argv[i], adb_cmd, len * sizeof(const char *));
|
||||
argv[len + i] = NULL;
|
||||
enum sc_process_result r =
|
||||
sc_process_execute_p(argv, &pid, pin, pout, perr);
|
||||
sc_process_execute_p(argv, &pid, NULL, pout, NULL);
|
||||
if (r != SC_PROCESS_SUCCESS) {
|
||||
show_adb_err_msg(r, argv);
|
||||
pid = SC_PROCESS_NONE;
|
||||
@@ -144,7 +144,7 @@ adb_execute_p(const char *serial, const char *const adb_cmd[],
|
||||
|
||||
sc_pid
|
||||
adb_execute(const char *serial, const char *const adb_cmd[], size_t len) {
|
||||
return adb_execute_p(serial, adb_cmd, len, NULL, NULL, NULL);
|
||||
return adb_execute_p(serial, adb_cmd, len, NULL);
|
||||
}
|
||||
|
||||
sc_pid
|
||||
@@ -233,45 +233,8 @@ adb_install(const char *serial, const char *local) {
|
||||
return pid;
|
||||
}
|
||||
|
||||
static ssize_t
|
||||
adb_execute_for_output(const char *serial, const char *const adb_cmd[],
|
||||
size_t adb_cmd_len, char *buf, size_t buf_len,
|
||||
const char *name) {
|
||||
sc_pipe pout;
|
||||
sc_pid pid = adb_execute_p(serial, adb_cmd, adb_cmd_len, NULL, &pout, NULL);
|
||||
|
||||
ssize_t r = sc_pipe_read_all(pout, buf, buf_len);
|
||||
sc_pipe_close(pout);
|
||||
|
||||
if (!sc_process_check_success(pid, name, true)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static size_t
|
||||
truncate_first_line(char *data, size_t len) {
|
||||
data[len - 1] = '\0';
|
||||
char *eol = strpbrk(data, "\r\n");
|
||||
if (eol) {
|
||||
*eol = '\0';
|
||||
len = eol - data;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
char *
|
||||
adb_get_serialno(void) {
|
||||
char buf[128];
|
||||
|
||||
sc_pid
|
||||
adb_get_serialno(sc_pipe *pout) {
|
||||
const char *const adb_cmd[] = {"get-serialno"};
|
||||
ssize_t r = adb_execute_for_output(NULL, adb_cmd, ARRAY_LEN(adb_cmd),
|
||||
buf, sizeof(buf), "get-serialno");
|
||||
if (r <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
truncate_first_line(buf, r);
|
||||
return strdup(buf);
|
||||
return adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), pout);
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ sc_pid
|
||||
adb_execute(const char *serial, const char *const adb_cmd[], size_t len);
|
||||
|
||||
sc_pid
|
||||
adb_execute_p(const char *serial, const char *const adb_cmd[],
|
||||
size_t len, sc_pipe *pin, sc_pipe *pout, sc_pipe *perr);
|
||||
adb_execute_p(const char *serial, const char *const adb_cmd[], size_t len,
|
||||
sc_pipe *pout);
|
||||
|
||||
sc_pid
|
||||
adb_forward(const char *serial, uint16_t local_port,
|
||||
@@ -35,8 +35,8 @@ adb_push(const char *serial, const char *local, const char *remote);
|
||||
sc_pid
|
||||
adb_install(const char *serial, const char *local);
|
||||
|
||||
// Return the result of "adb get-serialno".
|
||||
char *
|
||||
adb_get_serialno(void);
|
||||
// Execute "adb get-serialno" and give a pipe to get the result
|
||||
sc_pid
|
||||
adb_get_serialno(sc_pipe *pout);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -394,8 +394,11 @@ scrcpy(struct scrcpy_options *options) {
|
||||
// It is necessarily initialized here, since the device is connected
|
||||
struct sc_server_info *info = &s->server.info;
|
||||
|
||||
const char *serial = s->server.params.serial;
|
||||
assert(serial);
|
||||
|
||||
if (options->display && options->control) {
|
||||
if (!file_handler_init(&s->file_handler, options->serial,
|
||||
if (!file_handler_init(&s->file_handler, serial,
|
||||
options->push_target)) {
|
||||
goto end;
|
||||
}
|
||||
@@ -516,21 +519,7 @@ scrcpy(struct scrcpy_options *options) {
|
||||
#ifdef HAVE_AOA_HID
|
||||
bool aoa_hid_ok = false;
|
||||
|
||||
char *serialno = NULL;
|
||||
|
||||
const char *serial = options->serial;
|
||||
if (!serial) {
|
||||
serialno = adb_get_serialno();
|
||||
if (!serialno) {
|
||||
LOGE("Could not get device serial");
|
||||
goto aoa_hid_end;
|
||||
}
|
||||
serial = serialno;
|
||||
LOGI("Device serial: %s", serial);
|
||||
}
|
||||
|
||||
bool ok = sc_aoa_init(&s->aoa, serial);
|
||||
free(serialno);
|
||||
if (!ok) {
|
||||
goto aoa_hid_end;
|
||||
}
|
||||
|
||||
@@ -422,12 +422,59 @@ sc_server_on_terminated(void *userdata) {
|
||||
LOGD("Server terminated");
|
||||
}
|
||||
|
||||
static char *
|
||||
sc_server_get_serialno(struct sc_intr *intr) {
|
||||
sc_pipe pout;
|
||||
sc_pid pid = adb_get_serialno(&pout);
|
||||
if (pid == SC_PROCESS_NONE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char buf[128];
|
||||
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
||||
sc_pipe_close(pout);
|
||||
|
||||
bool ok = sc_process_check_success_intr(intr, pid, "adb get-serialno");
|
||||
sc_process_close(pid);
|
||||
if (!ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sc_str_truncate(buf, r, " \r\n");
|
||||
|
||||
return strdup(buf);
|
||||
}
|
||||
|
||||
static bool
|
||||
sc_server_fill_serial(struct sc_server *server) {
|
||||
// Retrieve the actual device immediately if not provided, so that all
|
||||
// future adb commands are executed for this specific device, even if other
|
||||
// devices are connected afterwards (without "more than one
|
||||
// device/emulator" error)
|
||||
if (!server->params.serial) {
|
||||
// The serial is owned by sc_server_params, and will be freed on destroy
|
||||
server->params.serial = sc_server_get_serialno(&server->intr);
|
||||
if (!server->params.serial) {
|
||||
LOGE("Could not get device serial");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static int
|
||||
run_server(void *data) {
|
||||
struct sc_server *server = data;
|
||||
|
||||
if (!sc_server_fill_serial(server)) {
|
||||
goto error_connection_failed;
|
||||
}
|
||||
|
||||
const struct sc_server_params *params = &server->params;
|
||||
|
||||
LOGD("Device serial: %s", params->serial);
|
||||
|
||||
bool ok = push_server(&server->intr, params->serial);
|
||||
if (!ok) {
|
||||
goto error_connection_failed;
|
||||
|
||||
@@ -14,3 +14,31 @@ sc_process_check_success_intr(struct sc_intr *intr, sc_pid pid,
|
||||
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data,
|
||||
size_t len) {
|
||||
if (!sc_intr_set_process(intr, pid)) {
|
||||
// Already interrupted
|
||||
return false;
|
||||
}
|
||||
|
||||
ssize_t ret = sc_pipe_read(pipe, data, len);
|
||||
|
||||
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
sc_pipe_read_all_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe,
|
||||
char *data, size_t len) {
|
||||
if (!sc_intr_set_process(intr, pid)) {
|
||||
// Already interrupted
|
||||
return false;
|
||||
}
|
||||
|
||||
ssize_t ret = sc_pipe_read_all(pipe, data, len);
|
||||
|
||||
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -10,4 +10,12 @@ bool
|
||||
sc_process_check_success_intr(struct sc_intr *intr, sc_pid pid,
|
||||
const char *name);
|
||||
|
||||
ssize_t
|
||||
sc_pipe_read_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe, char *data,
|
||||
size_t len);
|
||||
|
||||
ssize_t
|
||||
sc_pipe_read_all_intr(struct sc_intr *intr, sc_pid pid, sc_pipe pipe,
|
||||
char *data, size_t len);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -291,3 +291,14 @@ error:
|
||||
free(buf.s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t
|
||||
sc_str_truncate(char *data, size_t len, const char *endchars) {
|
||||
data[len - 1] = '\0';
|
||||
char *eol = strpbrk(data, endchars);
|
||||
if (eol) {
|
||||
*eol = '\0';
|
||||
len = eol - data;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -103,4 +103,15 @@ sc_str_from_wchars(const wchar_t *s);
|
||||
char *
|
||||
sc_str_wrap_lines(const char *input, unsigned columns, unsigned indent);
|
||||
|
||||
/**
|
||||
* Truncate the data after any of the characters from `endchars`
|
||||
*
|
||||
* An '\0' is always written at the end of the data, even if no newline
|
||||
* character is encountered.
|
||||
*
|
||||
* Return the size of the resulting line.
|
||||
*/
|
||||
size_t
|
||||
sc_str_truncate(char *data, size_t len, const char *endchars);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -337,6 +337,26 @@ static void test_wrap_lines(void) {
|
||||
free(formatted);
|
||||
}
|
||||
|
||||
static void test_truncate(void) {
|
||||
char s[] = "hello\nworld\n!";
|
||||
size_t line_len = sc_str_truncate(s, sizeof(s), "\n");
|
||||
|
||||
assert(line_len == 5);
|
||||
assert(!strcmp("hello", s));
|
||||
|
||||
char s2[] = "hello\r\nworkd\r\n!";
|
||||
line_len = sc_str_truncate(s2, sizeof(s2), "\n\r");
|
||||
|
||||
assert(line_len == 5);
|
||||
assert(!strcmp("hello", s));
|
||||
|
||||
char s3[] = "hello world\n!";
|
||||
line_len = sc_str_truncate(s3, sizeof(s3), " \n\r");
|
||||
|
||||
assert(line_len == 5);
|
||||
assert(!strcmp("hello", s3));
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
@@ -356,5 +376,6 @@ int main(int argc, char *argv[]) {
|
||||
test_parse_integer_with_suffix();
|
||||
test_strlist_contains();
|
||||
test_wrap_lines();
|
||||
test_truncate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user