Compare commits

..

1 Commits

Author SHA1 Message Date
95769eb87b Configure feature test macros in meson
Refs #2807 <https://github.com/Genymobile/scrcpy/pull/2807>

Co-authored-by: RipleyTom <RipleyTom@users.noreply.github.com>
2021-11-20 23:08:55 +01:00
11 changed files with 82 additions and 139 deletions

View File

@ -44,11 +44,19 @@ if host_machine.system() == 'windows'
'src/sys/win/file.c', 'src/sys/win/file.c',
'src/sys/win/process.c', 'src/sys/win/process.c',
] ]
add_project_arguments('-D_WIN32_WINNT=0x0600', language: 'c')
add_project_arguments('-DWINVER=0x0600', language: 'c')
else else
src += [ src += [
'src/sys/unix/file.c', 'src/sys/unix/file.c',
'src/sys/unix/process.c', 'src/sys/unix/process.c',
] ]
add_project_arguments('-D_POSIX_C_SOURCE=200809L', language: 'c')
add_project_arguments('-D_XOPEN_SOURCE=700', language: 'c')
add_project_arguments('-D_GNU_SOURCE', language: 'c')
if host_machine.system() == 'darwin'
add_project_arguments('-D_DARWIN_C_SOURCE', language: 'c')
endif
endif endif
v4l2_support = host_machine.system() == 'linux' v4l2_support = host_machine.system() == 'linux'

View File

@ -112,7 +112,7 @@ show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {
static sc_pid static sc_pid
adb_execute_p(const char *serial, const char *const adb_cmd[], adb_execute_p(const char *serial, const char *const adb_cmd[],
size_t len, unsigned inherit, sc_pipe *pout) { size_t len, sc_pipe *pout) {
int i; int i;
sc_pid pid; sc_pid pid;
@ -133,7 +133,7 @@ adb_execute_p(const char *serial, const char *const adb_cmd[],
memcpy(&argv[i], adb_cmd, len * sizeof(const char *)); memcpy(&argv[i], adb_cmd, len * sizeof(const char *));
argv[len + i] = NULL; argv[len + i] = NULL;
enum sc_process_result r = enum sc_process_result r =
sc_process_execute_p(argv, &pid, inherit, NULL, pout, NULL); sc_process_execute_p(argv, &pid, NULL, pout, NULL);
if (r != SC_PROCESS_SUCCESS) { if (r != SC_PROCESS_SUCCESS) {
show_adb_err_msg(r, argv); show_adb_err_msg(r, argv);
pid = SC_PROCESS_NONE; pid = SC_PROCESS_NONE;
@ -144,54 +144,50 @@ adb_execute_p(const char *serial, const char *const adb_cmd[],
} }
sc_pid sc_pid
adb_execute(const char *serial, const char *const adb_cmd[], size_t len, adb_execute(const char *serial, const char *const adb_cmd[], size_t len) {
unsigned inherit) { return adb_execute_p(serial, adb_cmd, len, NULL);
return adb_execute_p(serial, adb_cmd, len, inherit, NULL);
} }
static sc_pid static sc_pid
adb_exec_forward(const char *serial, uint16_t local_port, adb_exec_forward(const char *serial, uint16_t local_port,
const char *device_socket_name, unsigned inherit) { const char *device_socket_name) {
char local[4 + 5 + 1]; // tcp:PORT char local[4 + 5 + 1]; // tcp:PORT
char remote[108 + 14 + 1]; // localabstract:NAME char remote[108 + 14 + 1]; // localabstract:NAME
sprintf(local, "tcp:%" PRIu16, local_port); sprintf(local, "tcp:%" PRIu16, local_port);
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name); snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
const char *const adb_cmd[] = {"forward", local, remote}; const char *const adb_cmd[] = {"forward", local, remote};
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit); return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
} }
static sc_pid static sc_pid
adb_exec_forward_remove(const char *serial, uint16_t local_port, adb_exec_forward_remove(const char *serial, uint16_t local_port) {
unsigned inherit) {
char local[4 + 5 + 1]; // tcp:PORT char local[4 + 5 + 1]; // tcp:PORT
sprintf(local, "tcp:%" PRIu16, local_port); sprintf(local, "tcp:%" PRIu16, local_port);
const char *const adb_cmd[] = {"forward", "--remove", local}; const char *const adb_cmd[] = {"forward", "--remove", local};
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit); return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
} }
static sc_pid static sc_pid
adb_exec_reverse(const char *serial, const char *device_socket_name, adb_exec_reverse(const char *serial, const char *device_socket_name,
uint16_t local_port, unsigned inherit) { uint16_t local_port) {
char local[4 + 5 + 1]; // tcp:PORT char local[4 + 5 + 1]; // tcp:PORT
char remote[108 + 14 + 1]; // localabstract:NAME char remote[108 + 14 + 1]; // localabstract:NAME
sprintf(local, "tcp:%" PRIu16, local_port); sprintf(local, "tcp:%" PRIu16, local_port);
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name); snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
const char *const adb_cmd[] = {"reverse", remote, local}; const char *const adb_cmd[] = {"reverse", remote, local};
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit); return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
} }
static sc_pid static sc_pid
adb_exec_reverse_remove(const char *serial, const char *device_socket_name, adb_exec_reverse_remove(const char *serial, const char *device_socket_name) {
unsigned inherit) {
char remote[108 + 14 + 1]; // localabstract:NAME char remote[108 + 14 + 1]; // localabstract:NAME
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name); snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
const char *const adb_cmd[] = {"reverse", "--remove", remote}; const char *const adb_cmd[] = {"reverse", "--remove", remote};
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit); return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
} }
static sc_pid static sc_pid
adb_exec_push(const char *serial, const char *local, const char *remote, adb_exec_push(const char *serial, const char *local, const char *remote) {
unsigned inherit) {
#ifdef __WINDOWS__ #ifdef __WINDOWS__
// Windows will parse the string, so the paths must be quoted // Windows will parse the string, so the paths must be quoted
// (see sys/win/command.c) // (see sys/win/command.c)
@ -207,7 +203,7 @@ adb_exec_push(const char *serial, const char *local, const char *remote,
#endif #endif
const char *const adb_cmd[] = {"push", local, remote}; const char *const adb_cmd[] = {"push", local, remote};
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit); sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
#ifdef __WINDOWS__ #ifdef __WINDOWS__
free((void *) remote); free((void *) remote);
@ -218,7 +214,7 @@ adb_exec_push(const char *serial, const char *local, const char *remote,
} }
static sc_pid static sc_pid
adb_exec_install(const char *serial, const char *local, unsigned inherit) { adb_exec_install(const char *serial, const char *local) {
#ifdef __WINDOWS__ #ifdef __WINDOWS__
// Windows will parse the string, so the local name must be quoted // Windows will parse the string, so the local name must be quoted
// (see sys/win/command.c) // (see sys/win/command.c)
@ -229,7 +225,7 @@ adb_exec_install(const char *serial, const char *local, unsigned inherit) {
#endif #endif
const char *const adb_cmd[] = {"install", "-r", local}; const char *const adb_cmd[] = {"install", "-r", local};
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit); sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
#ifdef __WINDOWS__ #ifdef __WINDOWS__
free((void *) local); free((void *) local);
@ -239,62 +235,58 @@ adb_exec_install(const char *serial, const char *local, unsigned inherit) {
} }
static sc_pid static sc_pid
adb_exec_get_serialno(unsigned inherit, sc_pipe *pout) { adb_exec_get_serialno(sc_pipe *pout) {
const char *const adb_cmd[] = {"get-serialno"}; const char *const adb_cmd[] = {"get-serialno"};
return adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), inherit, pout); return adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), pout);
} }
bool bool
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port, adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
const char *device_socket_name, unsigned inherit) { const char *device_socket_name) {
sc_pid pid = sc_pid pid = adb_exec_forward(serial, local_port, device_socket_name);
adb_exec_forward(serial, local_port, device_socket_name, inherit);
return sc_process_check_success_intr(intr, pid, "adb forward", true); return sc_process_check_success_intr(intr, pid, "adb forward", true);
} }
bool bool
adb_forward_remove(struct sc_intr *intr, const char *serial, adb_forward_remove(struct sc_intr *intr, const char *serial,
uint16_t local_port, unsigned inherit) { uint16_t local_port) {
sc_pid pid = adb_exec_forward_remove(serial, local_port, inherit); sc_pid pid = adb_exec_forward_remove(serial, local_port);
return sc_process_check_success_intr(intr, pid, "adb forward --remove", return sc_process_check_success_intr(intr, pid, "adb forward --remove",
true); true);
} }
bool bool
adb_reverse(struct sc_intr *intr, const char *serial, adb_reverse(struct sc_intr *intr, const char *serial,
const char *device_socket_name, uint16_t local_port, const char *device_socket_name, uint16_t local_port) {
unsigned inherit) { sc_pid pid = adb_exec_reverse(serial, device_socket_name, local_port);
sc_pid pid =
adb_exec_reverse(serial, device_socket_name, local_port, inherit);
return sc_process_check_success_intr(intr, pid, "adb reverse", true); return sc_process_check_success_intr(intr, pid, "adb reverse", true);
} }
bool bool
adb_reverse_remove(struct sc_intr *intr, const char *serial, adb_reverse_remove(struct sc_intr *intr, const char *serial,
const char *device_socket_name, unsigned inherit) { const char *device_socket_name) {
sc_pid pid = adb_exec_reverse_remove(serial, device_socket_name, inherit); sc_pid pid = adb_exec_reverse_remove(serial, device_socket_name);
return sc_process_check_success_intr(intr, pid, "adb reverse --remove", return sc_process_check_success_intr(intr, pid, "adb reverse --remove",
true); true);
} }
bool bool
adb_push(struct sc_intr *intr, const char *serial, const char *local, adb_push(struct sc_intr *intr, const char *serial, const char *local,
const char *remote, unsigned inherit) { const char *remote) {
sc_pid pid = adb_exec_push(serial, local, remote, inherit); sc_pid pid = adb_exec_push(serial, local, remote);
return sc_process_check_success_intr(intr, pid, "adb push", true); return sc_process_check_success_intr(intr, pid, "adb push", true);
} }
bool bool
adb_install(struct sc_intr *intr, const char *serial, const char *local, adb_install(struct sc_intr *intr, const char *serial, const char *local) {
unsigned inherit) { sc_pid pid = adb_exec_install(serial, local);
sc_pid pid = adb_exec_install(serial, local, inherit);
return sc_process_check_success_intr(intr, pid, "adb install", true); return sc_process_check_success_intr(intr, pid, "adb install", true);
} }
char * char *
adb_get_serialno(struct sc_intr *intr, unsigned inherit) { adb_get_serialno(struct sc_intr *intr) {
sc_pipe pout; sc_pipe pout;
sc_pid pid = adb_exec_get_serialno(inherit, &pout); sc_pid pid = adb_exec_get_serialno(&pout);
if (pid == SC_PROCESS_NONE) { if (pid == SC_PROCESS_NONE) {
LOGE("Could not execute \"adb get-serialno\""); LOGE("Could not execute \"adb get-serialno\"");
return NULL; return NULL;

View File

@ -9,33 +9,30 @@
#include "util/intr.h" #include "util/intr.h"
sc_pid sc_pid
adb_execute(const char *serial, const char *const adb_cmd[], size_t len, adb_execute(const char *serial, const char *const adb_cmd[], size_t len);
unsigned inherit);
bool bool
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port, adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
const char *device_socket_name, unsigned inherit); const char *device_socket_name);
bool bool
adb_forward_remove(struct sc_intr *intr, const char *serial, adb_forward_remove(struct sc_intr *intr, const char *serial,
uint16_t local_port, unsigned inherit); uint16_t local_port);
bool bool
adb_reverse(struct sc_intr *intr, const char *serial, adb_reverse(struct sc_intr *intr, const char *serial,
const char *device_socket_name, uint16_t local_port, const char *device_socket_name, uint16_t local_port);
unsigned inherit);
bool bool
adb_reverse_remove(struct sc_intr *intr, const char *serial, adb_reverse_remove(struct sc_intr *intr, const char *serial,
const char *device_socket_name, unsigned inherit); const char *device_socket_name);
bool bool
adb_push(struct sc_intr *intr, const char *serial, const char *local, adb_push(struct sc_intr *intr, const char *serial, const char *local,
const char *remote, unsigned inherit); const char *remote);
bool bool
adb_install(struct sc_intr *intr, const char *serial, const char *local, adb_install(struct sc_intr *intr, const char *serial, const char *local);
unsigned inherit);
/** /**
* Execute `adb get-serialno` * Execute `adb get-serialno`
@ -43,6 +40,6 @@ adb_install(struct sc_intr *intr, const char *serial, const char *local,
* Return the result, to be freed by the caller, or NULL on error. * Return the result, to be freed by the caller, or NULL on error.
*/ */
char * char *
adb_get_serialno(struct sc_intr *intr, unsigned inherit); adb_get_serialno(struct sc_intr *intr);
#endif #endif

View File

@ -20,7 +20,7 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
struct sc_port_range port_range) { struct sc_port_range port_range) {
uint16_t port = port_range.first; uint16_t port = port_range.first;
for (;;) { for (;;) {
if (!adb_reverse(intr, serial, SC_SOCKET_NAME, port, SC_STDERR)) { if (!adb_reverse(intr, serial, SC_SOCKET_NAME, port)) {
// the command itself failed, it will fail on any port // the command itself failed, it will fail on any port
return false; return false;
} }
@ -51,7 +51,7 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
} }
// failure, disable tunnel and try another port // failure, disable tunnel and try another port
if (!adb_reverse_remove(intr, serial, SC_SOCKET_NAME, SC_STDERR)) { if (!adb_reverse_remove(intr, serial, SC_SOCKET_NAME)) {
LOGW("Could not remove reverse tunnel on port %" PRIu16, port); LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
} }
@ -81,7 +81,7 @@ enable_tunnel_forward_any_port(struct sc_adb_tunnel *tunnel,
uint16_t port = port_range.first; uint16_t port = port_range.first;
for (;;) { for (;;) {
if (adb_forward(intr, serial, port, SC_SOCKET_NAME, SC_STDERR)) { if (adb_forward(intr, serial, port, SC_SOCKET_NAME)) {
// success // success
tunnel->local_port = port; tunnel->local_port = port;
tunnel->enabled = true; tunnel->enabled = true;
@ -146,9 +146,9 @@ sc_adb_tunnel_close(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
bool ret; bool ret;
if (tunnel->forward) { if (tunnel->forward) {
ret = adb_forward_remove(intr, serial, tunnel->local_port, SC_STDERR); ret = adb_forward_remove(intr, serial, tunnel->local_port);
} else { } else {
ret = adb_reverse_remove(intr, serial, SC_SOCKET_NAME, SC_STDERR); ret = adb_reverse_remove(intr, serial, SC_SOCKET_NAME);
assert(tunnel->server_socket != SC_SOCKET_NONE); assert(tunnel->server_socket != SC_SOCKET_NONE);
if (!net_close(tunnel->server_socket)) { if (!net_close(tunnel->server_socket)) {

View File

@ -1,13 +1,6 @@
#ifndef COMPAT_H #ifndef COMPAT_H
#define COMPAT_H #define COMPAT_H
#define _POSIX_C_SOURCE 200809L
#define _XOPEN_SOURCE 700
#define _GNU_SOURCE
#ifdef __APPLE__
# define _DARWIN_C_SOURCE
#endif
#include <libavformat/version.h> #include <libavformat/version.h>
#include <SDL2/SDL_version.h> #include <SDL2/SDL_version.h>

View File

@ -128,8 +128,7 @@ run_file_handler(void *data) {
if (req.action == ACTION_INSTALL_APK) { if (req.action == ACTION_INSTALL_APK) {
LOGI("Installing %s...", req.file); LOGI("Installing %s...", req.file);
bool ok = bool ok = adb_install(intr, serial, req.file);
adb_install(intr, serial, req.file, SC_STDOUT | SC_STDERR);
if (ok) { if (ok) {
LOGI("%s successfully installed", req.file); LOGI("%s successfully installed", req.file);
} else { } else {
@ -137,8 +136,7 @@ run_file_handler(void *data) {
} }
} else { } else {
LOGI("Pushing %s...", req.file); LOGI("Pushing %s...", req.file);
bool ok = adb_push(intr, serial, req.file, push_target, bool ok = adb_push(intr, serial, req.file, push_target);
SC_STDOUT | SC_STDERR);
if (ok) { if (ok) {
LOGI("%s successfully pushed to %s", req.file, push_target); LOGI("%s successfully pushed to %s", req.file, push_target);
} else { } else {

View File

@ -112,8 +112,7 @@ push_server(struct sc_intr *intr, const char *serial) {
free(server_path); free(server_path);
return false; return false;
} }
bool ok = adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH, bool ok = adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH);
SC_STDERR);
free(server_path); free(server_path);
return ok; return ok;
} }
@ -199,8 +198,7 @@ execute_server(struct sc_server *server,
// Port: 5005 // Port: 5005
// Then click on "Debug" // Then click on "Debug"
#endif #endif
// Inherit both stdout and stderr (all server logs are printed to stdout) return adb_execute(serial, cmd, ARRAY_LEN(cmd));
return adb_execute(serial, cmd, ARRAY_LEN(cmd), SC_STDOUT | SC_STDERR);
} }
static bool static bool
@ -432,7 +430,7 @@ sc_server_fill_serial(struct sc_server *server) {
// device/emulator" error) // device/emulator" error)
if (!server->params.serial) { if (!server->params.serial) {
// The serial is owned by sc_server_params, and will be freed on destroy // The serial is owned by sc_server_params, and will be freed on destroy
server->params.serial = adb_get_serialno(&server->intr, SC_STDERR); server->params.serial = adb_get_serialno(&server->intr);
if (!server->params.serial) { if (!server->params.serial) {
LOGE("Could not get device serial"); LOGE("Could not get device serial");
return false; return false;
@ -459,8 +457,6 @@ run_server(void *data) {
goto error_connection_failed; goto error_connection_failed;
} }
LOGI("Server pushed");
ok = sc_adb_tunnel_open(&server->tunnel, &server->intr, params->serial, ok = sc_adb_tunnel_open(&server->tunnel, &server->intr, params->serial,
params->port_range, params->force_adb_forward); params->port_range, params->force_adb_forward);
if (!ok) { if (!ok) {

View File

@ -11,16 +11,8 @@
#include "util/log.h" #include "util/log.h"
enum sc_process_result enum sc_process_result
sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned inherit, sc_process_execute_p(const char *const argv[], sc_pid *pid,
int *pin, int *pout, int *perr) { int *pin, int *pout, int *perr) {
bool inherit_stdout = inherit & SC_STDOUT;
bool inherit_stderr = inherit & SC_STDERR;
// If pout is defined, then inherit MUST NOT contain SC_STDOUT.
assert(!pout || !inherit_stdout);
// If perr is defined, then inherit MUST NOT contain SC_STDERR.
assert(!perr || !inherit_stderr);
int in[2]; int in[2];
int out[2]; int out[2];
int err[2]; int err[2];
@ -98,31 +90,20 @@ sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned inherit,
} }
close(in[1]); close(in[1]);
} }
// Do not close stdin in the child process, this makes adb fail on
// Linux
if (pout) { if (pout) {
if (out[1] != STDOUT_FILENO) { if (out[1] != STDOUT_FILENO) {
dup2(out[1], STDOUT_FILENO); dup2(out[1], STDOUT_FILENO);
close(out[1]); close(out[1]);
} }
close(out[0]); close(out[0]);
} else if (!inherit_stdout) {
// Close stdout in the child process
close(STDOUT_FILENO);
} }
if (perr) { if (perr) {
if (err[1] != STDERR_FILENO) { if (err[1] != STDERR_FILENO) {
dup2(err[1], STDERR_FILENO); dup2(err[1], STDERR_FILENO);
close(err[1]); close(err[1]);
} }
close(err[0]); close(err[0]);
} else if (!inherit_stderr) {
// Close stderr in the child process
close(STDERR_FILENO);
} }
close(internal[0]); close(internal[0]);
enum sc_process_result err; enum sc_process_result err;
if (fcntl(internal[1], F_SETFD, FD_CLOEXEC) == 0) { if (fcntl(internal[1], F_SETFD, FD_CLOEXEC) == 0) {

View File

@ -1,6 +1,3 @@
// <https://devblogs.microsoft.com/oldnewthing/20111216-00/?p=8873>
#define _WIN32_WINNT 0x0600 // For extended process API
#include "util/process.h" #include "util/process.h"
#include <processthreadsapi.h> #include <processthreadsapi.h>
@ -27,22 +24,12 @@ build_cmd(char *cmd, size_t len, const char *const argv[]) {
} }
enum sc_process_result enum sc_process_result
sc_process_execute_p(const char *const argv[], HANDLE *handle, unsigned inherit, sc_process_execute_p(const char *const argv[], HANDLE *handle,
HANDLE *pin, HANDLE *pout, HANDLE *perr) { HANDLE *pin, HANDLE *pout, HANDLE *perr) {
bool inherit_stdout = inherit & SC_STDOUT; enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;
bool inherit_stderr = inherit & SC_STDERR;
// If pout is defined, then inherit MUST NOT contain SC_STDOUT.
assert(!pout || !inherit_stdout);
// If perr is defined, then inherit MUST NOT contain SC_STDERR.
assert(!perr || !inherit_stderr);
// Add 1 per non-NULL pointer // Add 1 per non-NULL pointer
unsigned handle_count = !!pin unsigned handle_count = !!pin + !!pout + !!perr;
+ (pout || inherit_stdout)
+ (perr || inherit_stderr);
enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;
SECURITY_ATTRIBUTES sa; SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.nLength = sizeof(SECURITY_ATTRIBUTES);
@ -90,25 +77,16 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle, unsigned inherit,
HANDLE handles[3]; HANDLE handles[3];
LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL; LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL;
// Must be set even if handle_count == 0, so that stdin, stdout and stderr
// are NOT inherited in that case
si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
if (handle_count) { if (handle_count) {
unsigned i = 0; si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
if (pin) { if (pin) {
si.StartupInfo.hStdInput = stdin_read_handle; si.StartupInfo.hStdInput = stdin_read_handle;
handles[i++] = si.StartupInfo.hStdInput;
} }
if (pout || inherit_stdout) { if (pout) {
si.StartupInfo.hStdOutput = pout ? stdout_write_handle si.StartupInfo.hStdOutput = stdout_write_handle;
: GetStdHandle(STD_OUTPUT_HANDLE);
handles[i++] = si.StartupInfo.hStdOutput;
} }
if (perr || inherit_stderr) { if (perr) {
si.StartupInfo.hStdError = perr ? stderr_write_handle si.StartupInfo.hStdError = stderr_write_handle;
: GetStdHandle(STD_ERROR_HANDLE);
handles[i++] = si.StartupInfo.hStdError;
} }
SIZE_T size; SIZE_T size;
@ -131,6 +109,17 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle, unsigned inherit,
goto error_close_stderr; goto error_close_stderr;
} }
// Explicitly pass the HANDLEs that must be inherited
unsigned i = 0;
if (pin) {
handles[i++] = stdin_read_handle;
}
if (pout) {
handles[i++] = stdout_write_handle;
}
if (perr) {
handles[i++] = stderr_write_handle;
}
ok = UpdateProcThreadAttribute(lpAttributeList, 0, ok = UpdateProcThreadAttribute(lpAttributeList, 0,
PROC_THREAD_ATTRIBUTE_HANDLE_LIST, PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
handles, handle_count * sizeof(HANDLE), handles, handle_count * sizeof(HANDLE),

View File

@ -5,8 +5,8 @@
#include "log.h" #include "log.h"
enum sc_process_result enum sc_process_result
sc_process_execute(const char *const argv[], sc_pid *pid, unsigned inherit) { sc_process_execute(const char *const argv[], sc_pid *pid) {
return sc_process_execute_p(argv, pid, inherit, NULL, NULL, NULL); return sc_process_execute_p(argv, pid, NULL, NULL, NULL);
} }
bool bool

View File

@ -67,31 +67,20 @@ enum sc_process_result {
SC_PROCESS_ERROR_MISSING_BINARY, SC_PROCESS_ERROR_MISSING_BINARY,
}; };
#define SC_STDOUT (1 << 0)
#define SC_STDERR (1 << 1)
/** /**
* Execute the command and write the process id to `pid` * Execute the command and write the process id to `pid`
*
* The parameter `inherit` is a OR of any of SC_STDOUT and SC_STDERR. It
* indicates if stdout and stderr must be inherited from the scrcpy process (in
* other words, if the process must output to the scrcpy console).
*/ */
enum sc_process_result enum sc_process_result
sc_process_execute(const char *const argv[], sc_pid *pid, unsigned inherit); sc_process_execute(const char *const argv[], sc_pid *pid);
/** /**
* Execute the command and write the process id to `pid` * Execute the command and write the process id to `pid`
* *
* If not NULL, provide a pipe for stdin (`pin`), stdout (`pout`) and stderr * If not NULL, provide a pipe for stdin (`pin`), stdout (`pout`) and stderr
* (`perr`). * (`perr`).
*
* The parameter `inherit` has the same semantics as in `sc_process_execute()`.
* If `pout` is not NULL, then `inherit` MUST NOT contain SC_STDOUT.
* If `perr` is not NULL, then `inherit` MUST NOT contain SC_STDERR.
*/ */
enum sc_process_result enum sc_process_result
sc_process_execute_p(const char *const argv[], sc_pid *pid, unsigned inherit, sc_process_execute_p(const char *const argv[], sc_pid *pid,
sc_pipe *pin, sc_pipe *pout, sc_pipe *perr); sc_pipe *pin, sc_pipe *pout, sc_pipe *perr);
/** /**