Compare commits
38 commits
Author | SHA1 | Date | |
---|---|---|---|
f56be59a0c | |||
64400c8740 | |||
46f7cd31b9 | |||
d290b44248 | |||
2d6a96776c | |||
6da6d905c2 | |||
b25404ee4b | |||
4cfc1cd70a | |||
2fc80eae2d | |||
3fdbd994e0 | |||
ce225f019a | |||
b7559744a7 | |||
afb5a5e80f | |||
84334cf7db | |||
0ba2686e1d | |||
55648d4d64 | |||
13fd693b50 | |||
0426ae885c | |||
ea454e9cee | |||
cb65531533 | |||
9619ade706 | |||
f2781a8b6d | |||
443cb14d6e | |||
b30c3a429f | |||
632bd5697b | |||
de50846905 | |||
ee93d2aac1 | |||
c29a0bf675 | |||
411bb0d18e | |||
cc0902b13c | |||
48b572c272 | |||
94feae71f2 | |||
67170437f1 | |||
9cb14b5166 | |||
9cb8766220 | |||
fd4ec784e0 | |||
52cebe1597 | |||
6a27062f48 |
63
README.md
63
README.md
|
@ -412,21 +412,66 @@ autoadb scrcpy -s '{}'
|
|||
|
||||
[AutoAdb]: https://github.com/rom1v/autoadb
|
||||
|
||||
#### SSH tunnel
|
||||
#### Tunnels
|
||||
|
||||
To connect to a remote device, it is possible to connect a local `adb` client to
|
||||
a remote `adb` server (provided they use the same version of the _adb_
|
||||
protocol):
|
||||
protocol).
|
||||
|
||||
##### Remote ADB server
|
||||
|
||||
To connect to a remote ADB server, make the server listen on all interfaces:
|
||||
|
||||
```bash
|
||||
adb kill-server # kill the local adb server on 5037
|
||||
ssh -CN -L5037:localhost:5037 -R27183:localhost:27183 your_remote_computer
|
||||
adb kill-server
|
||||
adb -a nodaemon server start
|
||||
# keep this open
|
||||
```
|
||||
|
||||
From another terminal:
|
||||
**Warning: all communications between clients and ADB server are unencrypted.**
|
||||
|
||||
Suppose that this server is accessible at 192.168.1.2. Then, from another
|
||||
terminal, run scrcpy:
|
||||
|
||||
```bash
|
||||
export ADB_SERVER_SOCKET=tcp:192.168.1.2:5037
|
||||
scrcpy --tunnel-host=192.168.1.2
|
||||
```
|
||||
|
||||
By default, scrcpy uses the local port used for `adb forward` tunnel
|
||||
establishment (typically `27183`, see `--port`). It is also possible to force a
|
||||
different tunnel port (it may be useful in more complex situations, when more
|
||||
redirections are involved):
|
||||
|
||||
```
|
||||
scrcpy --tunnel-port=1234
|
||||
```
|
||||
|
||||
|
||||
##### SSH tunnel
|
||||
|
||||
To communicate with a remote ADB server securely, it is preferable to use a SSH
|
||||
tunnel.
|
||||
|
||||
First, make sure the ADB server is running on the remote computer:
|
||||
|
||||
```bash
|
||||
adb start-server
|
||||
```
|
||||
|
||||
Then, establish a SSH tunnel:
|
||||
|
||||
```bash
|
||||
# local 5038 --> remote 5037
|
||||
# local 27183 <-- remote 27183
|
||||
ssh -CN -L5038:localhost:5037 -R27183:localhost:27183 your_remote_computer
|
||||
# keep this open
|
||||
```
|
||||
|
||||
From another terminal, run scrcpy:
|
||||
|
||||
```bash
|
||||
export ADB_SERVER_SOCKET=tcp:localhost:5038
|
||||
scrcpy
|
||||
```
|
||||
|
||||
|
@ -434,14 +479,16 @@ To avoid enabling remote port forwarding, you could force a forward connection
|
|||
instead (notice the `-L` instead of `-R`):
|
||||
|
||||
```bash
|
||||
adb kill-server # kill the local adb server on 5037
|
||||
ssh -CN -L5037:localhost:5037 -L27183:localhost:27183 your_remote_computer
|
||||
# local 5038 --> remote 5037
|
||||
# local 27183 --> remote 27183
|
||||
ssh -CN -L5038:localhost:5037 -L27183:localhost:27183 your_remote_computer
|
||||
# keep this open
|
||||
```
|
||||
|
||||
From another terminal:
|
||||
From another terminal, run scrcpy:
|
||||
|
||||
```bash
|
||||
export ADB_SERVER_SOCKET=tcp:localhost:5038
|
||||
scrcpy --force-adb-forward
|
||||
```
|
||||
|
||||
|
|
|
@ -199,6 +199,7 @@ if get_option('buildtype') == 'debug'
|
|||
'tests/test_cli.c',
|
||||
'src/cli.c',
|
||||
'src/options.c',
|
||||
'src/util/net.c',
|
||||
'src/util/str.c',
|
||||
'src/util/strbuf.c',
|
||||
'src/util/term.c',
|
||||
|
|
12
app/scrcpy.1
12
app/scrcpy.1
|
@ -203,6 +203,18 @@ Enable "show touches" on start, restore the initial value on exit.
|
|||
|
||||
It only shows physical touches (not clicks from scrcpy).
|
||||
|
||||
.TP
|
||||
.BI "\-\-tunnel\-host " ip
|
||||
Set the IP address of the adb tunnel to reach the scrcpy server. This option automatically enables --force-adb-forward.
|
||||
|
||||
Default is localhost.
|
||||
|
||||
.TP
|
||||
.BI "\-\-tunnel\-port " port
|
||||
Set the TCP port of the adb tunnel to reach the scrcpy server. This option automatically enables --force-adb-forward.
|
||||
|
||||
Default is 0 (not forced): the local port used for establishing the tunnel will be used.
|
||||
|
||||
.TP
|
||||
.BI "\-\-v4l2-sink " /dev/videoN
|
||||
Output to v4l2loopback device.
|
||||
|
|
131
app/src/adb.c
131
app/src/adb.c
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include "util/file.h"
|
||||
#include "util/log.h"
|
||||
#include "util/process_intr.h"
|
||||
#include "util/str.h"
|
||||
|
||||
static const char *adb_command;
|
||||
|
@ -109,9 +110,9 @@ show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {
|
|||
free(buf);
|
||||
}
|
||||
|
||||
sc_pid
|
||||
static 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 +133,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,12 +145,12 @@ 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
|
||||
adb_forward(const char *serial, uint16_t local_port,
|
||||
const char *device_socket_name) {
|
||||
static sc_pid
|
||||
adb_exec_forward(const char *serial, uint16_t local_port,
|
||||
const char *device_socket_name) {
|
||||
char local[4 + 5 + 1]; // tcp:PORT
|
||||
char remote[108 + 14 + 1]; // localabstract:NAME
|
||||
sprintf(local, "tcp:%" PRIu16, local_port);
|
||||
|
@ -158,17 +159,17 @@ adb_forward(const char *serial, uint16_t local_port,
|
|||
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
||||
}
|
||||
|
||||
sc_pid
|
||||
adb_forward_remove(const char *serial, uint16_t local_port) {
|
||||
static sc_pid
|
||||
adb_exec_forward_remove(const char *serial, uint16_t local_port) {
|
||||
char local[4 + 5 + 1]; // tcp:PORT
|
||||
sprintf(local, "tcp:%" PRIu16, local_port);
|
||||
const char *const adb_cmd[] = {"forward", "--remove", local};
|
||||
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
||||
}
|
||||
|
||||
sc_pid
|
||||
adb_reverse(const char *serial, const char *device_socket_name,
|
||||
uint16_t local_port) {
|
||||
static sc_pid
|
||||
adb_exec_reverse(const char *serial, const char *device_socket_name,
|
||||
uint16_t local_port) {
|
||||
char local[4 + 5 + 1]; // tcp:PORT
|
||||
char remote[108 + 14 + 1]; // localabstract:NAME
|
||||
sprintf(local, "tcp:%" PRIu16, local_port);
|
||||
|
@ -177,16 +178,16 @@ adb_reverse(const char *serial, const char *device_socket_name,
|
|||
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
||||
}
|
||||
|
||||
sc_pid
|
||||
adb_reverse_remove(const char *serial, const char *device_socket_name) {
|
||||
static sc_pid
|
||||
adb_exec_reverse_remove(const char *serial, const char *device_socket_name) {
|
||||
char remote[108 + 14 + 1]; // localabstract:NAME
|
||||
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
||||
const char *const adb_cmd[] = {"reverse", "--remove", remote};
|
||||
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
||||
}
|
||||
|
||||
sc_pid
|
||||
adb_push(const char *serial, const char *local, const char *remote) {
|
||||
static sc_pid
|
||||
adb_exec_push(const char *serial, const char *local, const char *remote) {
|
||||
#ifdef __WINDOWS__
|
||||
// Windows will parse the string, so the paths must be quoted
|
||||
// (see sys/win/command.c)
|
||||
|
@ -212,8 +213,8 @@ adb_push(const char *serial, const char *local, const char *remote) {
|
|||
return pid;
|
||||
}
|
||||
|
||||
sc_pid
|
||||
adb_install(const char *serial, const char *local) {
|
||||
static sc_pid
|
||||
adb_exec_install(const char *serial, const char *local) {
|
||||
#ifdef __WINDOWS__
|
||||
// Windows will parse the string, so the local name must be quoted
|
||||
// (see sys/win/command.c)
|
||||
|
@ -233,45 +234,75 @@ 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 sc_pid
|
||||
adb_exec_get_serialno(sc_pipe *pout) {
|
||||
const char *const adb_cmd[] = {"get-serialno"};
|
||||
return adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), pout);
|
||||
}
|
||||
|
||||
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;
|
||||
bool
|
||||
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
||||
const char *device_socket_name) {
|
||||
sc_pid pid = adb_exec_forward(serial, local_port, device_socket_name);
|
||||
return sc_process_check_success_intr(intr, pid, "adb forward", true);
|
||||
}
|
||||
|
||||
bool
|
||||
adb_forward_remove(struct sc_intr *intr, const char *serial,
|
||||
uint16_t local_port) {
|
||||
sc_pid pid = adb_exec_forward_remove(serial, local_port);
|
||||
return sc_process_check_success_intr(intr, pid, "adb forward --remove",
|
||||
true);
|
||||
}
|
||||
|
||||
bool
|
||||
adb_reverse(struct sc_intr *intr, const char *serial,
|
||||
const char *device_socket_name, uint16_t local_port) {
|
||||
sc_pid pid = adb_exec_reverse(serial, device_socket_name, local_port);
|
||||
return sc_process_check_success_intr(intr, pid, "adb reverse", true);
|
||||
}
|
||||
|
||||
bool
|
||||
adb_reverse_remove(struct sc_intr *intr, const char *serial,
|
||||
const char *device_socket_name) {
|
||||
sc_pid pid = adb_exec_reverse_remove(serial, device_socket_name);
|
||||
return sc_process_check_success_intr(intr, pid, "adb reverse --remove",
|
||||
true);
|
||||
}
|
||||
|
||||
bool
|
||||
adb_push(struct sc_intr *intr, const char *serial, const char *local,
|
||||
const char *remote) {
|
||||
sc_pid pid = adb_exec_push(serial, local, remote);
|
||||
return sc_process_check_success_intr(intr, pid, "adb push", true);
|
||||
}
|
||||
|
||||
bool
|
||||
adb_install(struct sc_intr *intr, const char *serial, const char *local) {
|
||||
sc_pid pid = adb_exec_install(serial, local);
|
||||
return sc_process_check_success_intr(intr, pid, "adb install", true);
|
||||
}
|
||||
|
||||
char *
|
||||
adb_get_serialno(void) {
|
||||
char buf[128];
|
||||
|
||||
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) {
|
||||
adb_get_serialno(struct sc_intr *intr) {
|
||||
sc_pipe pout;
|
||||
sc_pid pid = adb_exec_get_serialno(&pout);
|
||||
if (pid == SC_PROCESS_NONE) {
|
||||
LOGE("Could not execute \"adb get-serialno\"");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
truncate_first_line(buf, r);
|
||||
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", true);
|
||||
if (!ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sc_str_truncate(buf, r, " \r\n");
|
||||
|
||||
return strdup(buf);
|
||||
}
|
||||
|
|
|
@ -6,37 +6,40 @@
|
|||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "util/process.h"
|
||||
#include "util/intr.h"
|
||||
|
||||
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);
|
||||
|
||||
sc_pid
|
||||
adb_forward(const char *serial, uint16_t local_port,
|
||||
bool
|
||||
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
||||
const char *device_socket_name);
|
||||
|
||||
sc_pid
|
||||
adb_forward_remove(const char *serial, uint16_t local_port);
|
||||
bool
|
||||
adb_forward_remove(struct sc_intr *intr, const char *serial,
|
||||
uint16_t local_port);
|
||||
|
||||
sc_pid
|
||||
adb_reverse(const char *serial, const char *device_socket_name,
|
||||
uint16_t local_port);
|
||||
bool
|
||||
adb_reverse(struct sc_intr *intr, const char *serial,
|
||||
const char *device_socket_name, uint16_t local_port);
|
||||
|
||||
sc_pid
|
||||
adb_reverse_remove(const char *serial, const char *device_socket_name);
|
||||
bool
|
||||
adb_reverse_remove(struct sc_intr *intr, const char *serial,
|
||||
const char *device_socket_name);
|
||||
|
||||
sc_pid
|
||||
adb_push(const char *serial, const char *local, const char *remote);
|
||||
bool
|
||||
adb_push(struct sc_intr *intr, const char *serial, const char *local,
|
||||
const char *remote);
|
||||
|
||||
sc_pid
|
||||
adb_install(const char *serial, const char *local);
|
||||
bool
|
||||
adb_install(struct sc_intr *intr, const char *serial, const char *local);
|
||||
|
||||
// Return the result of "adb get-serialno".
|
||||
/**
|
||||
* Execute `adb get-serialno`
|
||||
*
|
||||
* Return the result, to be freed by the caller, or NULL on error.
|
||||
*/
|
||||
char *
|
||||
adb_get_serialno(void);
|
||||
adb_get_serialno(struct sc_intr *intr);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -9,33 +9,6 @@
|
|||
|
||||
#define SC_SOCKET_NAME "scrcpy"
|
||||
|
||||
static bool
|
||||
enable_tunnel_reverse(struct sc_intr *intr, const char *serial,
|
||||
uint16_t local_port) {
|
||||
sc_pid pid = adb_reverse(serial, SC_SOCKET_NAME, local_port);
|
||||
return sc_process_check_success_intr(intr, pid, "adb reverse");
|
||||
}
|
||||
|
||||
static bool
|
||||
disable_tunnel_reverse(struct sc_intr *intr, const char *serial) {
|
||||
sc_pid pid = adb_reverse_remove(serial, SC_SOCKET_NAME);
|
||||
return sc_process_check_success_intr(intr, pid, "adb reverse --remove");
|
||||
}
|
||||
|
||||
static bool
|
||||
enable_tunnel_forward(struct sc_intr *intr, const char *serial,
|
||||
uint16_t local_port) {
|
||||
sc_pid pid = adb_forward(serial, local_port, SC_SOCKET_NAME);
|
||||
return sc_process_check_success_intr(intr, pid, "adb forward");
|
||||
}
|
||||
|
||||
static bool
|
||||
disable_tunnel_forward(struct sc_intr *intr, const char *serial,
|
||||
uint16_t local_port) {
|
||||
sc_pid pid = adb_forward_remove(serial, local_port);
|
||||
return sc_process_check_success_intr(intr, pid, "adb forward --remove");
|
||||
}
|
||||
|
||||
static bool
|
||||
listen_on_port(struct sc_intr *intr, sc_socket socket, uint16_t port) {
|
||||
return net_listen_intr(intr, socket, IPV4_LOCALHOST, port, 1);
|
||||
|
@ -47,7 +20,7 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
|
|||
struct sc_port_range port_range) {
|
||||
uint16_t port = port_range.first;
|
||||
for (;;) {
|
||||
if (!enable_tunnel_reverse(intr, serial, port)) {
|
||||
if (!adb_reverse(intr, serial, SC_SOCKET_NAME, port)) {
|
||||
// the command itself failed, it will fail on any port
|
||||
return false;
|
||||
}
|
||||
|
@ -78,7 +51,7 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
|
|||
}
|
||||
|
||||
// failure, disable tunnel and try another port
|
||||
if (!disable_tunnel_reverse(intr, serial)) {
|
||||
if (!adb_reverse_remove(intr, serial, SC_SOCKET_NAME)) {
|
||||
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
|
||||
}
|
||||
|
||||
|
@ -108,7 +81,7 @@ enable_tunnel_forward_any_port(struct sc_adb_tunnel *tunnel,
|
|||
|
||||
uint16_t port = port_range.first;
|
||||
for (;;) {
|
||||
if (enable_tunnel_forward(intr, serial, port)) {
|
||||
if (adb_forward(intr, serial, port, SC_SOCKET_NAME)) {
|
||||
// success
|
||||
tunnel->local_port = port;
|
||||
tunnel->enabled = true;
|
||||
|
@ -173,9 +146,9 @@ sc_adb_tunnel_close(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
|
|||
|
||||
bool ret;
|
||||
if (tunnel->forward) {
|
||||
ret = disable_tunnel_forward(intr, serial, tunnel->local_port);
|
||||
ret = adb_forward_remove(intr, serial, tunnel->local_port);
|
||||
} else {
|
||||
ret = disable_tunnel_reverse(intr, serial);
|
||||
ret = adb_reverse_remove(intr, serial, SC_SOCKET_NAME);
|
||||
|
||||
assert(tunnel->server_socket != SC_SOCKET_NONE);
|
||||
if (!net_close(tunnel->server_socket)) {
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include "options.h"
|
||||
#include "util/log.h"
|
||||
#include "util/net.h"
|
||||
#include "util/str.h"
|
||||
#include "util/strbuf.h"
|
||||
#include "util/term.h"
|
||||
|
@ -46,6 +47,8 @@
|
|||
#define OPT_V4L2_SINK 1027
|
||||
#define OPT_DISPLAY_BUFFER 1028
|
||||
#define OPT_V4L2_BUFFER 1029
|
||||
#define OPT_TUNNEL_HOST 1030
|
||||
#define OPT_TUNNEL_PORT 1031
|
||||
|
||||
struct sc_option {
|
||||
char shortopt;
|
||||
|
@ -330,6 +333,25 @@ static const struct sc_option options[] = {
|
|||
"on exit.\n"
|
||||
"It only shows physical touches (not clicks from scrcpy).",
|
||||
},
|
||||
{
|
||||
.longopt_id = OPT_TUNNEL_HOST,
|
||||
.longopt = "tunnel-host",
|
||||
.argdesc = "ip",
|
||||
.text = "Set the IP address of the adb tunnel to reach the scrcpy "
|
||||
"server. This option automatically enables "
|
||||
"--force-adb-forward.\n"
|
||||
"Default is localhost.",
|
||||
},
|
||||
{
|
||||
.longopt_id = OPT_TUNNEL_PORT,
|
||||
.longopt = "tunnel-port",
|
||||
.argdesc = "port",
|
||||
.text = "Set the TCP port of the adb tunnel to reach the scrcpy "
|
||||
"server. This option automatically enables "
|
||||
"--force-adb-forward.\n"
|
||||
"Default is 0 (not forced): the local port used for "
|
||||
"establishing the tunnel will be used.",
|
||||
},
|
||||
#ifdef HAVE_V4L2
|
||||
{
|
||||
.longopt_id = OPT_V4L2_SINK,
|
||||
|
@ -569,6 +591,10 @@ sc_getopt_adapter_create_longopts(void) {
|
|||
size_t out_idx = 0;
|
||||
for (size_t i = 0; i < ARRAY_LEN(options); ++i) {
|
||||
const struct sc_option *in = &options[i];
|
||||
|
||||
// If longopt_id is set, then longopt must be set
|
||||
assert(!in->longopt_id || in->longopt);
|
||||
|
||||
if (!in->longopt) {
|
||||
// The longopts array must only contain long options
|
||||
continue;
|
||||
|
@ -671,12 +697,12 @@ print_option_usage_header(const struct sc_option *opt) {
|
|||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "\n %s\n", buf.s);
|
||||
printf("\n %s\n", buf.s);
|
||||
free(buf.s);
|
||||
return;
|
||||
|
||||
error:
|
||||
fprintf(stderr, "<ERROR>\n");
|
||||
printf("<ERROR>\n");
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -692,11 +718,11 @@ print_option_usage(const struct sc_option *opt, unsigned cols) {
|
|||
|
||||
char *text = sc_str_wrap_lines(opt->text, cols, 8);
|
||||
if (!text) {
|
||||
fprintf(stderr, "<ERROR>\n");
|
||||
printf("<ERROR>\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s\n", text);
|
||||
printf("%s\n", text);
|
||||
free(text);
|
||||
}
|
||||
|
||||
|
@ -707,11 +733,11 @@ print_shortcuts_intro(unsigned cols) {
|
|||
"(left) Alt or (left) Super, but it can be configured by "
|
||||
"--shortcut-mod (see above).", cols, 4);
|
||||
if (!intro) {
|
||||
fprintf(stderr, "<ERROR>\n");
|
||||
printf("<ERROR>\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s\n", intro);
|
||||
printf("%s\n", intro);
|
||||
free(intro);
|
||||
}
|
||||
|
||||
|
@ -721,21 +747,21 @@ print_shortcut(const struct sc_shortcut *shortcut, unsigned cols) {
|
|||
assert(shortcut->shortcuts[0]); // At least one shortcut
|
||||
assert(shortcut->text);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
printf("\n");
|
||||
|
||||
unsigned i = 0;
|
||||
while (shortcut->shortcuts[i]) {
|
||||
fprintf(stderr, " %s\n", shortcut->shortcuts[i]);
|
||||
printf(" %s\n", shortcut->shortcuts[i]);
|
||||
++i;
|
||||
};
|
||||
|
||||
char *text = sc_str_wrap_lines(shortcut->text, cols, 8);
|
||||
if (!text) {
|
||||
fprintf(stderr, "<ERROR>\n");
|
||||
printf("<ERROR>\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s\n", text);
|
||||
printf("%s\n", text);
|
||||
free(text);
|
||||
}
|
||||
|
||||
|
@ -759,14 +785,14 @@ scrcpy_print_usage(const char *arg0) {
|
|||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "Usage: %s [options]\n\n"
|
||||
"Options:\n", arg0);
|
||||
printf("Usage: %s [options]\n\n"
|
||||
"Options:\n", arg0);
|
||||
for (size_t i = 0; i < ARRAY_LEN(options); ++i) {
|
||||
print_option_usage(&options[i], cols);
|
||||
}
|
||||
|
||||
// Print shortcuts section
|
||||
fprintf(stderr, "\nShortcuts:\n\n");
|
||||
printf("\nShortcuts:\n\n");
|
||||
print_shortcuts_intro(cols);
|
||||
for (size_t i = 0; i < ARRAY_LEN(shortcuts); ++i) {
|
||||
print_shortcut(&shortcuts[i], cols);
|
||||
|
@ -1123,6 +1149,21 @@ parse_record_format(const char *optarg, enum sc_record_format *format) {
|
|||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
parse_ip(const char *optarg, uint32_t *ipv4) {
|
||||
return net_parse_ipv4(optarg, ipv4);
|
||||
}
|
||||
|
||||
static bool
|
||||
parse_port(const char *optarg, uint16_t *port) {
|
||||
long value;
|
||||
if (!parse_integer_arg(optarg, &value, false, 0, 0xFFFF, "port")) {
|
||||
return false;
|
||||
}
|
||||
*port = (uint16_t) value;
|
||||
return true;
|
||||
}
|
||||
|
||||
static enum sc_record_format
|
||||
guess_record_format(const char *filename) {
|
||||
size_t len = strlen(filename);
|
||||
|
@ -1195,6 +1236,16 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||
return false;
|
||||
}
|
||||
break;
|
||||
case OPT_TUNNEL_HOST:
|
||||
if (!parse_ip(optarg, &opts->tunnel_host)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case OPT_TUNNEL_PORT:
|
||||
if (!parse_port(optarg, &opts->tunnel_port)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'n':
|
||||
opts->control = false;
|
||||
break;
|
||||
|
@ -1354,6 +1405,12 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
|
|||
}
|
||||
#endif
|
||||
|
||||
if ((opts->tunnel_host || opts->tunnel_port) && !opts->force_adb_forward) {
|
||||
LOGI("Tunnel host/port is set, "
|
||||
"--force-adb-forward automatically enabled.");
|
||||
opts->force_adb_forward = true;
|
||||
}
|
||||
|
||||
int index = optind;
|
||||
if (index < argc) {
|
||||
LOGE("Unexpected additional argument: %s", argv[index]);
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "adb.h"
|
||||
#include "util/log.h"
|
||||
#include "util/process_intr.h"
|
||||
|
||||
#define DEFAULT_PUSH_TARGET "/sdcard/Download/"
|
||||
|
||||
|
@ -16,6 +17,7 @@ file_handler_request_destroy(struct file_handler_request *req) {
|
|||
bool
|
||||
file_handler_init(struct file_handler *file_handler, const char *serial,
|
||||
const char *push_target) {
|
||||
assert(serial);
|
||||
|
||||
cbuf_init(&file_handler->queue);
|
||||
|
||||
|
@ -30,23 +32,26 @@ file_handler_init(struct file_handler *file_handler, const char *serial,
|
|||
return false;
|
||||
}
|
||||
|
||||
if (serial) {
|
||||
file_handler->serial = strdup(serial);
|
||||
if (!file_handler->serial) {
|
||||
LOGW("Could not strdup serial");
|
||||
sc_cond_destroy(&file_handler->event_cond);
|
||||
sc_mutex_destroy(&file_handler->mutex);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
file_handler->serial = NULL;
|
||||
ok = sc_intr_init(&file_handler->intr);
|
||||
if (!ok) {
|
||||
LOGE("Could not create intr");
|
||||
sc_cond_destroy(&file_handler->event_cond);
|
||||
sc_mutex_destroy(&file_handler->mutex);
|
||||
}
|
||||
|
||||
file_handler->serial = strdup(serial);
|
||||
if (!file_handler->serial) {
|
||||
LOGE("Could not strdup serial");
|
||||
sc_intr_destroy(&file_handler->intr);
|
||||
sc_cond_destroy(&file_handler->event_cond);
|
||||
sc_mutex_destroy(&file_handler->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
// lazy initialization
|
||||
file_handler->initialized = false;
|
||||
|
||||
file_handler->stopped = false;
|
||||
file_handler->current_process = SC_PROCESS_NONE;
|
||||
|
||||
file_handler->push_target = push_target ? push_target : DEFAULT_PUSH_TARGET;
|
||||
|
||||
|
@ -57,6 +62,7 @@ void
|
|||
file_handler_destroy(struct file_handler *file_handler) {
|
||||
sc_cond_destroy(&file_handler->event_cond);
|
||||
sc_mutex_destroy(&file_handler->mutex);
|
||||
sc_intr_destroy(&file_handler->intr);
|
||||
free(file_handler->serial);
|
||||
|
||||
struct file_handler_request req;
|
||||
|
@ -65,16 +71,6 @@ file_handler_destroy(struct file_handler *file_handler) {
|
|||
}
|
||||
}
|
||||
|
||||
static sc_pid
|
||||
install_apk(const char *serial, const char *file) {
|
||||
return adb_install(serial, file);
|
||||
}
|
||||
|
||||
static sc_pid
|
||||
push_file(const char *serial, const char *file, const char *push_target) {
|
||||
return adb_push(serial, file, push_target);
|
||||
}
|
||||
|
||||
bool
|
||||
file_handler_request(struct file_handler *file_handler,
|
||||
file_handler_action_t action, char *file) {
|
||||
|
@ -106,10 +102,16 @@ file_handler_request(struct file_handler *file_handler,
|
|||
static int
|
||||
run_file_handler(void *data) {
|
||||
struct file_handler *file_handler = data;
|
||||
struct sc_intr *intr = &file_handler->intr;
|
||||
|
||||
const char *serial = file_handler->serial;
|
||||
assert(serial);
|
||||
|
||||
const char *push_target = file_handler->push_target;
|
||||
assert(push_target);
|
||||
|
||||
for (;;) {
|
||||
sc_mutex_lock(&file_handler->mutex);
|
||||
file_handler->current_process = SC_PROCESS_NONE;
|
||||
while (!file_handler->stopped && cbuf_is_empty(&file_handler->queue)) {
|
||||
sc_cond_wait(&file_handler->event_cond, &file_handler->mutex);
|
||||
}
|
||||
|
@ -122,43 +124,26 @@ run_file_handler(void *data) {
|
|||
bool non_empty = cbuf_take(&file_handler->queue, &req);
|
||||
assert(non_empty);
|
||||
(void) non_empty;
|
||||
|
||||
sc_pid pid;
|
||||
if (req.action == ACTION_INSTALL_APK) {
|
||||
LOGI("Installing %s...", req.file);
|
||||
pid = install_apk(file_handler->serial, req.file);
|
||||
} else {
|
||||
LOGI("Pushing %s...", req.file);
|
||||
pid = push_file(file_handler->serial, req.file,
|
||||
file_handler->push_target);
|
||||
}
|
||||
file_handler->current_process = pid;
|
||||
sc_mutex_unlock(&file_handler->mutex);
|
||||
|
||||
if (req.action == ACTION_INSTALL_APK) {
|
||||
if (sc_process_check_success(pid, "adb install", false)) {
|
||||
LOGI("Installing %s...", req.file);
|
||||
bool ok = adb_install(intr, serial, req.file);
|
||||
if (ok) {
|
||||
LOGI("%s successfully installed", req.file);
|
||||
} else {
|
||||
LOGE("Failed to install %s", req.file);
|
||||
}
|
||||
} else {
|
||||
if (sc_process_check_success(pid, "adb push", false)) {
|
||||
LOGI("%s successfully pushed to %s", req.file,
|
||||
file_handler->push_target);
|
||||
LOGI("Pushing %s...", req.file);
|
||||
bool ok = adb_push(intr, serial, req.file, push_target);
|
||||
if (ok) {
|
||||
LOGI("%s successfully pushed to %s", req.file, push_target);
|
||||
} else {
|
||||
LOGE("Failed to push %s to %s", req.file,
|
||||
file_handler->push_target);
|
||||
LOGE("Failed to push %s to %s", req.file, push_target);
|
||||
}
|
||||
}
|
||||
|
||||
sc_mutex_lock(&file_handler->mutex);
|
||||
// Close the process (it is necessarily already terminated)
|
||||
// Execute this call with mutex locked to avoid race conditions with
|
||||
// file_handler_stop()
|
||||
sc_process_close(file_handler->current_process);
|
||||
file_handler->current_process = SC_PROCESS_NONE;
|
||||
sc_mutex_unlock(&file_handler->mutex);
|
||||
|
||||
file_handler_request_destroy(&req);
|
||||
}
|
||||
return 0;
|
||||
|
@ -183,11 +168,7 @@ file_handler_stop(struct file_handler *file_handler) {
|
|||
sc_mutex_lock(&file_handler->mutex);
|
||||
file_handler->stopped = true;
|
||||
sc_cond_signal(&file_handler->event_cond);
|
||||
if (file_handler->current_process != SC_PROCESS_NONE) {
|
||||
if (!sc_process_terminate(file_handler->current_process)) {
|
||||
LOGW("Could not terminate push/install process");
|
||||
}
|
||||
}
|
||||
sc_intr_interrupt(&file_handler->intr);
|
||||
sc_mutex_unlock(&file_handler->mutex);
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "adb.h"
|
||||
#include "util/cbuf.h"
|
||||
#include "util/thread.h"
|
||||
#include "util/intr.h"
|
||||
|
||||
typedef enum {
|
||||
ACTION_INSTALL_APK,
|
||||
|
@ -29,8 +30,9 @@ struct file_handler {
|
|||
sc_cond event_cond;
|
||||
bool stopped;
|
||||
bool initialized;
|
||||
sc_pid current_process;
|
||||
struct file_handler_request_queue queue;
|
||||
|
||||
struct sc_intr intr;
|
||||
};
|
||||
|
||||
bool
|
||||
|
|
|
@ -47,6 +47,9 @@ main(int argc, char *argv[]) {
|
|||
setbuf(stderr, NULL);
|
||||
#endif
|
||||
|
||||
printf("scrcpy " SCRCPY_VERSION
|
||||
" <https://github.com/Genymobile/scrcpy>\n");
|
||||
|
||||
struct scrcpy_cli_args args = {
|
||||
.opts = scrcpy_options_default,
|
||||
.help = false,
|
||||
|
@ -73,8 +76,6 @@ main(int argc, char *argv[]) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
LOGI("scrcpy " SCRCPY_VERSION " <https://github.com/Genymobile/scrcpy>");
|
||||
|
||||
#ifdef SCRCPY_LAVF_REQUIRES_REGISTER_ALL
|
||||
av_register_all();
|
||||
#endif
|
||||
|
|
|
@ -19,6 +19,8 @@ const struct scrcpy_options scrcpy_options_default = {
|
|||
.first = DEFAULT_LOCAL_PORT_RANGE_FIRST,
|
||||
.last = DEFAULT_LOCAL_PORT_RANGE_LAST,
|
||||
},
|
||||
.tunnel_host = 0,
|
||||
.tunnel_port = 0,
|
||||
.shortcut_mods = {
|
||||
.data = {SC_MOD_LALT, SC_MOD_LSUPER},
|
||||
.count = 2,
|
||||
|
|
|
@ -77,6 +77,8 @@ struct scrcpy_options {
|
|||
enum sc_record_format record_format;
|
||||
enum sc_keyboard_input_mode keyboard_input_mode;
|
||||
struct sc_port_range port_range;
|
||||
uint32_t tunnel_host;
|
||||
uint16_t tunnel_port;
|
||||
struct sc_shortcut_mods shortcut_mods;
|
||||
uint16_t max_size;
|
||||
uint32_t bit_rate;
|
||||
|
|
|
@ -345,6 +345,8 @@ scrcpy(struct scrcpy_options *options) {
|
|||
.log_level = options->log_level,
|
||||
.crop = options->crop,
|
||||
.port_range = options->port_range,
|
||||
.tunnel_host = options->tunnel_host,
|
||||
.tunnel_port = options->tunnel_port,
|
||||
.max_size = options->max_size,
|
||||
.bit_rate = options->bit_rate,
|
||||
.max_fps = options->max_fps,
|
||||
|
@ -394,8 +396,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 +521,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;
|
||||
}
|
||||
|
|
|
@ -112,9 +112,9 @@ push_server(struct sc_intr *intr, const char *serial) {
|
|||
free(server_path);
|
||||
return false;
|
||||
}
|
||||
sc_pid pid = adb_push(serial, server_path, SC_DEVICE_SERVER_PATH);
|
||||
bool ok = adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH);
|
||||
free(server_path);
|
||||
return sc_process_check_success_intr(intr, pid, "adb push");
|
||||
return ok;
|
||||
}
|
||||
|
||||
static const char *
|
||||
|
@ -202,8 +202,9 @@ execute_server(struct sc_server *server,
|
|||
}
|
||||
|
||||
static bool
|
||||
connect_and_read_byte(struct sc_intr *intr, sc_socket socket, uint16_t port) {
|
||||
bool ok = net_connect_intr(intr, socket, IPV4_LOCALHOST, port);
|
||||
connect_and_read_byte(struct sc_intr *intr, sc_socket socket,
|
||||
uint32_t tunnel_host, uint16_t tunnel_port) {
|
||||
bool ok = net_connect_intr(intr, socket, tunnel_host, tunnel_port);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
@ -220,13 +221,13 @@ connect_and_read_byte(struct sc_intr *intr, sc_socket socket, uint16_t port) {
|
|||
}
|
||||
|
||||
static sc_socket
|
||||
connect_to_server(struct sc_server *server, uint32_t attempts, sc_tick delay) {
|
||||
uint16_t port = server->tunnel.local_port;
|
||||
connect_to_server(struct sc_server *server, uint32_t attempts, sc_tick delay,
|
||||
uint32_t host, uint16_t port) {
|
||||
do {
|
||||
LOGD("Remaining connection attempts: %d", (int) attempts);
|
||||
sc_socket socket = net_socket();
|
||||
if (socket != SC_SOCKET_NONE) {
|
||||
bool ok = connect_and_read_byte(&server->intr, socket, port);
|
||||
bool ok = connect_and_read_byte(&server->intr, socket, host, port);
|
||||
if (ok) {
|
||||
// it worked!
|
||||
return socket;
|
||||
|
@ -234,6 +235,12 @@ connect_to_server(struct sc_server *server, uint32_t attempts, sc_tick delay) {
|
|||
|
||||
net_close(socket);
|
||||
}
|
||||
|
||||
if (sc_intr_is_interrupted(&server->intr)) {
|
||||
// Stop immediately
|
||||
break;
|
||||
}
|
||||
|
||||
if (attempts) {
|
||||
sc_mutex_lock(&server->mutex);
|
||||
sc_tick deadline = sc_tick_now() + delay;
|
||||
|
@ -346,9 +353,20 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
|
|||
goto fail;
|
||||
}
|
||||
} else {
|
||||
uint32_t tunnel_host = server->params.tunnel_host;
|
||||
if (!tunnel_host) {
|
||||
tunnel_host = IPV4_LOCALHOST;
|
||||
}
|
||||
|
||||
uint16_t tunnel_port = server->params.tunnel_port;
|
||||
if (!tunnel_port) {
|
||||
tunnel_port = tunnel->local_port;
|
||||
}
|
||||
|
||||
uint32_t attempts = 100;
|
||||
sc_tick delay = SC_TICK_FROM_MS(100);
|
||||
video_socket = connect_to_server(server, attempts, delay);
|
||||
video_socket = connect_to_server(server, attempts, delay, tunnel_host,
|
||||
tunnel_port);
|
||||
if (video_socket == SC_SOCKET_NONE) {
|
||||
goto fail;
|
||||
}
|
||||
|
@ -358,8 +376,8 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
|
|||
if (control_socket == SC_SOCKET_NONE) {
|
||||
goto fail;
|
||||
}
|
||||
bool ok = net_connect_intr(&server->intr, control_socket,
|
||||
IPV4_LOCALHOST, tunnel->local_port);
|
||||
bool ok = net_connect_intr(&server->intr, control_socket, tunnel_host,
|
||||
tunnel_port);
|
||||
if (!ok) {
|
||||
goto fail;
|
||||
}
|
||||
|
@ -416,12 +434,36 @@ sc_server_on_terminated(void *userdata) {
|
|||
LOGD("Server terminated");
|
||||
}
|
||||
|
||||
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 = adb_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;
|
||||
|
|
|
@ -29,6 +29,8 @@ struct sc_server_params {
|
|||
const char *codec_options;
|
||||
const char *encoder_name;
|
||||
struct sc_port_range port_range;
|
||||
uint32_t tunnel_host;
|
||||
uint16_t tunnel_port;
|
||||
uint16_t max_size;
|
||||
uint32_t bit_rate;
|
||||
uint16_t max_fps;
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
// <https://devblogs.microsoft.com/oldnewthing/20111216-00/?p=8873>
|
||||
#define _WIN32_WINNT 0x0600 // For extended process API
|
||||
|
||||
#include "util/process.h"
|
||||
|
||||
#include <processthreadsapi.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "util/log.h"
|
||||
|
@ -26,6 +31,9 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
|||
HANDLE *pin, HANDLE *pout, HANDLE *perr) {
|
||||
enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC;
|
||||
|
||||
// Add 1 per non-NULL pointer
|
||||
unsigned handle_count = !!pin + !!pout + !!perr;
|
||||
|
||||
SECURITY_ATTRIBUTES sa;
|
||||
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||
sa.lpSecurityDescriptor = NULL;
|
||||
|
@ -65,45 +73,94 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
|||
}
|
||||
}
|
||||
|
||||
STARTUPINFOW si;
|
||||
STARTUPINFOEXW si;
|
||||
PROCESS_INFORMATION pi;
|
||||
memset(&si, 0, sizeof(si));
|
||||
si.cb = sizeof(si);
|
||||
if (pin || pout || perr) {
|
||||
si.dwFlags = STARTF_USESTDHANDLES;
|
||||
si.StartupInfo.cb = sizeof(si);
|
||||
HANDLE handles[3];
|
||||
|
||||
LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL;
|
||||
if (handle_count) {
|
||||
si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
|
||||
if (pin) {
|
||||
si.hStdInput = stdin_read_handle;
|
||||
si.StartupInfo.hStdInput = stdin_read_handle;
|
||||
}
|
||||
if (pout) {
|
||||
si.hStdOutput = stdout_write_handle;
|
||||
si.StartupInfo.hStdOutput = stdout_write_handle;
|
||||
}
|
||||
if (perr) {
|
||||
si.hStdError = stderr_write_handle;
|
||||
si.StartupInfo.hStdError = stderr_write_handle;
|
||||
}
|
||||
|
||||
SIZE_T size;
|
||||
// Call it once to know the required buffer size
|
||||
BOOL ok =
|
||||
InitializeProcThreadAttributeList(NULL, 1, 0, &size)
|
||||
|| GetLastError() == ERROR_INSUFFICIENT_BUFFER;
|
||||
if (!ok) {
|
||||
goto error_close_stderr;
|
||||
}
|
||||
|
||||
lpAttributeList = malloc(size);
|
||||
if (!lpAttributeList) {
|
||||
goto error_close_stderr;
|
||||
}
|
||||
|
||||
ok = InitializeProcThreadAttributeList(lpAttributeList, 1, 0, &size);
|
||||
if (!ok) {
|
||||
free(lpAttributeList);
|
||||
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,
|
||||
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
|
||||
handles, handle_count * sizeof(HANDLE),
|
||||
NULL, NULL);
|
||||
if (!ok) {
|
||||
goto error_free_attribute_list;
|
||||
}
|
||||
|
||||
si.lpAttributeList = lpAttributeList;
|
||||
}
|
||||
|
||||
char *cmd = malloc(CMD_MAX_LEN);
|
||||
if (!cmd || !build_cmd(cmd, CMD_MAX_LEN, argv)) {
|
||||
*handle = NULL;
|
||||
goto error_close_stderr;
|
||||
goto error_free_attribute_list;
|
||||
}
|
||||
|
||||
wchar_t *wide = sc_str_to_wchars(cmd);
|
||||
free(cmd);
|
||||
if (!wide) {
|
||||
LOGC("Could not allocate wide char string");
|
||||
goto error_close_stderr;
|
||||
goto error_free_attribute_list;
|
||||
}
|
||||
|
||||
if (!CreateProcessW(NULL, wide, NULL, NULL, TRUE, 0, NULL, NULL, &si,
|
||||
&pi)) {
|
||||
free(wide);
|
||||
*handle = NULL;
|
||||
|
||||
BOOL bInheritHandles = handle_count > 0;
|
||||
DWORD dwCreationFlags = handle_count > 0 ? EXTENDED_STARTUPINFO_PRESENT : 0;
|
||||
BOOL ok = CreateProcessW(NULL, wide, NULL, NULL, bInheritHandles,
|
||||
dwCreationFlags, NULL, NULL, &si.StartupInfo, &pi);
|
||||
free(wide);
|
||||
if (!ok) {
|
||||
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
|
||||
ret = SC_PROCESS_ERROR_MISSING_BINARY;
|
||||
}
|
||||
goto error_close_stderr;
|
||||
goto error_free_attribute_list;
|
||||
}
|
||||
|
||||
if (lpAttributeList) {
|
||||
DeleteProcThreadAttributeList(lpAttributeList);
|
||||
free(lpAttributeList);
|
||||
}
|
||||
|
||||
// These handles are used by the child process, close them for this process
|
||||
|
@ -117,11 +174,15 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
|
|||
CloseHandle(stderr_write_handle);
|
||||
}
|
||||
|
||||
free(wide);
|
||||
*handle = pi.hProcess;
|
||||
|
||||
return SC_PROCESS_SUCCESS;
|
||||
|
||||
error_free_attribute_list:
|
||||
if (lpAttributeList) {
|
||||
DeleteProcThreadAttributeList(lpAttributeList);
|
||||
free(lpAttributeList);
|
||||
}
|
||||
error_close_stderr:
|
||||
if (perr) {
|
||||
CloseHandle(*perr);
|
||||
|
@ -168,7 +229,7 @@ sc_process_close(HANDLE handle) {
|
|||
}
|
||||
|
||||
ssize_t
|
||||
sc_read_pipe(HANDLE pipe, char *data, size_t len) {
|
||||
sc_pipe_read(HANDLE pipe, char *data, size_t len) {
|
||||
DWORD r;
|
||||
if (!ReadFile(pipe, data, len, &r, NULL)) {
|
||||
return -1;
|
||||
|
@ -177,7 +238,7 @@ sc_read_pipe(HANDLE pipe, char *data, size_t len) {
|
|||
}
|
||||
|
||||
void
|
||||
sc_close_pipe(HANDLE pipe) {
|
||||
sc_pipe_close(HANDLE pipe) {
|
||||
if (!CloseHandle(pipe)) {
|
||||
LOGW("Cannot close pipe");
|
||||
}
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
// For inet_pton() on Windows
|
||||
#define _WIN32_WINNT 0x0600
|
||||
#define WINVER 0x0600
|
||||
|
||||
#include "net.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
@ -7,6 +11,7 @@
|
|||
#include "log.h"
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
# include <ws2tcpip.h>
|
||||
typedef int socklen_t;
|
||||
typedef SOCKET sc_raw_socket;
|
||||
#else
|
||||
|
@ -225,3 +230,15 @@ net_close(sc_socket socket) {
|
|||
return !close(raw_sock);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
net_parse_ipv4(const char *s, uint32_t *ipv4) {
|
||||
struct in_addr addr;
|
||||
if (!inet_pton(AF_INET, s, &addr)) {
|
||||
LOGE("Invalid IPv4 address: %s", s);
|
||||
return false;
|
||||
}
|
||||
|
||||
*ipv4 = ntohl(addr.s_addr);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -68,4 +68,10 @@ net_interrupt(sc_socket socket);
|
|||
bool
|
||||
net_close(sc_socket socket);
|
||||
|
||||
/**
|
||||
* Parse `ip` "xxx.xxx.xxx.xxx" to an IPv4 host representation
|
||||
*/
|
||||
bool
|
||||
net_parse_ipv4(const char *ip, uint32_t *ipv4);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
bool
|
||||
sc_process_check_success_intr(struct sc_intr *intr, sc_pid pid,
|
||||
const char *name) {
|
||||
const char *name, bool close) {
|
||||
if (!sc_intr_set_process(intr, pid)) {
|
||||
// Already interrupted
|
||||
return false;
|
||||
|
@ -11,6 +11,40 @@ sc_process_check_success_intr(struct sc_intr *intr, sc_pid pid,
|
|||
// Always pass close=false, interrupting would be racy otherwise
|
||||
bool ret = sc_process_check_success(pid, name, false);
|
||||
|
||||
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
||||
|
||||
if (close) {
|
||||
// Close separately
|
||||
sc_process_close(pid);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,14 @@
|
|||
|
||||
bool
|
||||
sc_process_check_success_intr(struct sc_intr *intr, sc_pid pid,
|
||||
const char *name);
|
||||
const char *name, bool close);
|
||||
|
||||
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,11 @@ error:
|
|||
free(buf.s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t
|
||||
sc_str_truncate(char *data, size_t len, const char *endchars) {
|
||||
data[len - 1] = '\0';
|
||||
size_t idx = strcspn(data, endchars);
|
||||
data[idx] = '\0';
|
||||
return idx;
|
||||
}
|
||||
|
|
|
@ -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,32 @@ static void test_wrap_lines(void) {
|
|||
free(formatted);
|
||||
}
|
||||
|
||||
static void test_truncate(void) {
|
||||
char s[] = "hello\nworld\n!";
|
||||
size_t len = sc_str_truncate(s, sizeof(s), "\n");
|
||||
|
||||
assert(len == 5);
|
||||
assert(!strcmp("hello", s));
|
||||
|
||||
char s2[] = "hello\r\nworkd\r\n!";
|
||||
len = sc_str_truncate(s2, sizeof(s2), "\n\r");
|
||||
|
||||
assert(len == 5);
|
||||
assert(!strcmp("hello", s));
|
||||
|
||||
char s3[] = "hello world\n!";
|
||||
len = sc_str_truncate(s3, sizeof(s3), " \n\r");
|
||||
|
||||
assert(len == 5);
|
||||
assert(!strcmp("hello", s3));
|
||||
|
||||
char s4[] = "hello ";
|
||||
len = sc_str_truncate(s4, sizeof(s4), " \n\r");
|
||||
|
||||
assert(len == 5);
|
||||
assert(!strcmp("hello", s4));
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
@ -356,5 +382,6 @@ int main(int argc, char *argv[]) {
|
|||
test_parse_integer_with_suffix();
|
||||
test_strlist_contains();
|
||||
test_wrap_lines();
|
||||
test_truncate();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.genymobile.scrcpy;
|
||||
|
||||
import com.genymobile.scrcpy.wrappers.ContentProvider;
|
||||
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
||||
|
||||
import android.os.Parcel;
|
||||
|
@ -166,14 +165,21 @@ public final class CleanUp {
|
|||
|
||||
if (config.disableShowTouches || config.restoreStayOn != -1) {
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
try (ContentProvider settings = serviceManager.getActivityManager().createSettingsProvider()) {
|
||||
if (config.disableShowTouches) {
|
||||
Ln.i("Disabling \"show touches\"");
|
||||
settings.putValue(ContentProvider.TABLE_SYSTEM, "show_touches", "0");
|
||||
Settings settings = new Settings(serviceManager);
|
||||
if (config.disableShowTouches) {
|
||||
Ln.i("Disabling \"show touches\"");
|
||||
try {
|
||||
settings.putValue(Settings.TABLE_SYSTEM, "show_touches", "0");
|
||||
} catch (SettingsException e) {
|
||||
Ln.e("Could not restore \"show_touches\"", e);
|
||||
}
|
||||
if (config.restoreStayOn != -1) {
|
||||
Ln.i("Restoring \"stay awake\"");
|
||||
settings.putValue(ContentProvider.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(config.restoreStayOn));
|
||||
}
|
||||
if (config.restoreStayOn != -1) {
|
||||
Ln.i("Restoring \"stay awake\"");
|
||||
try {
|
||||
settings.putValue(Settings.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(config.restoreStayOn));
|
||||
} catch (SettingsException e) {
|
||||
Ln.e("Could not restore \"stay_on_while_plugged_in\"", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
33
server/src/main/java/com/genymobile/scrcpy/Command.java
Normal file
33
server/src/main/java/com/genymobile/scrcpy/Command.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package com.genymobile.scrcpy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
public final class Command {
|
||||
private Command() {
|
||||
// not instantiable
|
||||
}
|
||||
|
||||
public static void exec(String... cmd) throws IOException, InterruptedException {
|
||||
Process process = Runtime.getRuntime().exec(cmd);
|
||||
int exitCode = process.waitFor();
|
||||
if (exitCode != 0) {
|
||||
throw new IOException("Command " + Arrays.toString(cmd) + " returned with value " + exitCode);
|
||||
}
|
||||
}
|
||||
|
||||
public static String execReadLine(String... cmd) throws IOException, InterruptedException {
|
||||
String result = null;
|
||||
Process process = Runtime.getRuntime().exec(cmd);
|
||||
Scanner scanner = new Scanner(process.getInputStream());
|
||||
if (scanner.hasNextLine()) {
|
||||
result = scanner.nextLine();
|
||||
}
|
||||
int exitCode = process.waitFor();
|
||||
if (exitCode != 0) {
|
||||
throw new IOException("Command " + Arrays.toString(cmd) + " returned with value " + exitCode);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
package com.genymobile.scrcpy;
|
||||
|
||||
import com.genymobile.scrcpy.wrappers.ClipboardManager;
|
||||
import com.genymobile.scrcpy.wrappers.ContentProvider;
|
||||
import com.genymobile.scrcpy.wrappers.InputManager;
|
||||
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
||||
import com.genymobile.scrcpy.wrappers.SurfaceControl;
|
||||
|
@ -29,6 +28,7 @@ public final class Device {
|
|||
public static final int LOCK_VIDEO_ORIENTATION_INITIAL = -2;
|
||||
|
||||
private static final ServiceManager SERVICE_MANAGER = new ServiceManager();
|
||||
private static final Settings SETTINGS = new Settings(SERVICE_MANAGER);
|
||||
|
||||
public interface RotationListener {
|
||||
void onRotationChanged(int rotation);
|
||||
|
@ -296,7 +296,7 @@ public final class Device {
|
|||
}
|
||||
}
|
||||
|
||||
public static ContentProvider createSettingsProvider() {
|
||||
return SERVICE_MANAGER.getActivityManager().createSettingsProvider();
|
||||
public static Settings getSettings() {
|
||||
return SETTINGS;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,13 +57,20 @@ public final class Ln {
|
|||
}
|
||||
}
|
||||
|
||||
public static void w(String message) {
|
||||
public static void w(String message, Throwable throwable) {
|
||||
if (isEnabled(Level.WARN)) {
|
||||
Log.w(TAG, message);
|
||||
Log.w(TAG, message, throwable);
|
||||
System.out.println(PREFIX + "WARN: " + message);
|
||||
if (throwable != null) {
|
||||
throwable.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void w(String message) {
|
||||
w(message, null);
|
||||
}
|
||||
|
||||
public static void e(String message, Throwable throwable) {
|
||||
if (isEnabled(Level.ERROR)) {
|
||||
Log.e(TAG, message, throwable);
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package com.genymobile.scrcpy;
|
||||
|
||||
import com.genymobile.scrcpy.wrappers.ContentProvider;
|
||||
|
||||
import android.graphics.Rect;
|
||||
import android.media.MediaCodec;
|
||||
import android.media.MediaCodecInfo;
|
||||
|
@ -19,24 +17,25 @@ public final class Server {
|
|||
// not instantiable
|
||||
}
|
||||
|
||||
private static void scrcpy(Options options) throws IOException {
|
||||
Ln.i("Device: " + Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")");
|
||||
final Device device = new Device(options);
|
||||
List<CodecOption> codecOptions = CodecOption.parse(options.getCodecOptions());
|
||||
|
||||
private static void initAndCleanUp(Options options) {
|
||||
boolean mustDisableShowTouchesOnCleanUp = false;
|
||||
int restoreStayOn = -1;
|
||||
if (options.getShowTouches() || options.getStayAwake()) {
|
||||
try (ContentProvider settings = Device.createSettingsProvider()) {
|
||||
if (options.getShowTouches()) {
|
||||
String oldValue = settings.getAndPutValue(ContentProvider.TABLE_SYSTEM, "show_touches", "1");
|
||||
Settings settings = Device.getSettings();
|
||||
if (options.getShowTouches()) {
|
||||
try {
|
||||
String oldValue = settings.getAndPutValue(Settings.TABLE_SYSTEM, "show_touches", "1");
|
||||
// If "show touches" was disabled, it must be disabled back on clean up
|
||||
mustDisableShowTouchesOnCleanUp = !"1".equals(oldValue);
|
||||
} catch (SettingsException e) {
|
||||
Ln.e("Could not change \"show_touches\"", e);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.getStayAwake()) {
|
||||
int stayOn = BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB | BatteryManager.BATTERY_PLUGGED_WIRELESS;
|
||||
String oldValue = settings.getAndPutValue(ContentProvider.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(stayOn));
|
||||
if (options.getStayAwake()) {
|
||||
int stayOn = BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB | BatteryManager.BATTERY_PLUGGED_WIRELESS;
|
||||
try {
|
||||
String oldValue = settings.getAndPutValue(Settings.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(stayOn));
|
||||
try {
|
||||
restoreStayOn = Integer.parseInt(oldValue);
|
||||
if (restoreStayOn == stayOn) {
|
||||
|
@ -46,11 +45,25 @@ public final class Server {
|
|||
} catch (NumberFormatException e) {
|
||||
restoreStayOn = 0;
|
||||
}
|
||||
} catch (SettingsException e) {
|
||||
Ln.e("Could not change \"stay_on_while_plugged_in\"", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, true, options.getPowerOffScreenOnClose());
|
||||
try {
|
||||
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, true, options.getPowerOffScreenOnClose());
|
||||
} catch (IOException e) {
|
||||
Ln.e("Could not configure cleanup", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void scrcpy(Options options) throws IOException {
|
||||
Ln.i("Device: " + Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")");
|
||||
final Device device = new Device(options);
|
||||
List<CodecOption> codecOptions = CodecOption.parse(options.getCodecOptions());
|
||||
|
||||
Thread initThread = startInitThread(options);
|
||||
|
||||
boolean tunnelForward = options.isTunnelForward();
|
||||
|
||||
|
@ -82,6 +95,7 @@ public final class Server {
|
|||
// this is expected on close
|
||||
Ln.d("Screen streaming stopped");
|
||||
} finally {
|
||||
initThread.interrupt();
|
||||
if (controllerThread != null) {
|
||||
controllerThread.interrupt();
|
||||
}
|
||||
|
@ -92,6 +106,17 @@ public final class Server {
|
|||
}
|
||||
}
|
||||
|
||||
private static Thread startInitThread(final Options options) {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
initAndCleanUp(options);
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
return thread;
|
||||
}
|
||||
|
||||
private static Thread startController(final Controller controller) {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
|
|
84
server/src/main/java/com/genymobile/scrcpy/Settings.java
Normal file
84
server/src/main/java/com/genymobile/scrcpy/Settings.java
Normal file
|
@ -0,0 +1,84 @@
|
|||
package com.genymobile.scrcpy;
|
||||
|
||||
import com.genymobile.scrcpy.wrappers.ContentProvider;
|
||||
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
||||
|
||||
import android.os.Build;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Settings {
|
||||
|
||||
public static final String TABLE_SYSTEM = ContentProvider.TABLE_SYSTEM;
|
||||
public static final String TABLE_SECURE = ContentProvider.TABLE_SECURE;
|
||||
public static final String TABLE_GLOBAL = ContentProvider.TABLE_GLOBAL;
|
||||
|
||||
private final ServiceManager serviceManager;
|
||||
|
||||
public Settings(ServiceManager serviceManager) {
|
||||
this.serviceManager = serviceManager;
|
||||
}
|
||||
|
||||
private static void execSettingsPut(String table, String key, String value) throws SettingsException {
|
||||
try {
|
||||
Command.exec("settings", "put", table, key, value);
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new SettingsException("put", table, key, value, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String execSettingsGet(String table, String key) throws SettingsException {
|
||||
try {
|
||||
return Command.execReadLine("settings", "get", table, key);
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new SettingsException("get", table, key, null, e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getValue(String table, String key) throws SettingsException {
|
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
|
||||
// on Android >= 12, it always fails: <https://github.com/Genymobile/scrcpy/issues/2788>
|
||||
try (ContentProvider provider = serviceManager.getActivityManager().createSettingsProvider()) {
|
||||
return provider.getValue(table, key);
|
||||
} catch (SettingsException e) {
|
||||
Ln.w("Could not get settings value via ContentProvider, fallback to settings process", e);
|
||||
}
|
||||
}
|
||||
|
||||
return execSettingsGet(table, key);
|
||||
}
|
||||
|
||||
public void putValue(String table, String key, String value) throws SettingsException {
|
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
|
||||
// on Android >= 12, it always fails: <https://github.com/Genymobile/scrcpy/issues/2788>
|
||||
try (ContentProvider provider = serviceManager.getActivityManager().createSettingsProvider()) {
|
||||
provider.putValue(table, key, value);
|
||||
} catch (SettingsException e) {
|
||||
Ln.w("Could not put settings value via ContentProvider, fallback to settings process", e);
|
||||
}
|
||||
}
|
||||
|
||||
execSettingsPut(table, key, value);
|
||||
}
|
||||
|
||||
public String getAndPutValue(String table, String key, String value) throws SettingsException {
|
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
|
||||
// on Android >= 12, it always fails: <https://github.com/Genymobile/scrcpy/issues/2788>
|
||||
try (ContentProvider provider = serviceManager.getActivityManager().createSettingsProvider()) {
|
||||
String oldValue = provider.getValue(table, key);
|
||||
if (!value.equals(oldValue)) {
|
||||
provider.putValue(table, key, value);
|
||||
}
|
||||
return oldValue;
|
||||
} catch (SettingsException e) {
|
||||
Ln.w("Could not get and put settings value via ContentProvider, fallback to settings process", e);
|
||||
}
|
||||
}
|
||||
|
||||
String oldValue = getValue(table, key);
|
||||
if (!value.equals(oldValue)) {
|
||||
putValue(table, key, value);
|
||||
}
|
||||
return oldValue;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.genymobile.scrcpy;
|
||||
|
||||
public class SettingsException extends Exception {
|
||||
private static String createMessage(String method, String table, String key, String value) {
|
||||
return "Could not access settings: " + method + " " + table + " " + key + (value != null ? " " + value : "");
|
||||
}
|
||||
|
||||
public SettingsException(String method, String table, String key, String value, Throwable cause) {
|
||||
super(createMessage(method, table, key, value), cause);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.genymobile.scrcpy.wrappers;
|
||||
|
||||
import com.genymobile.scrcpy.Ln;
|
||||
import com.genymobile.scrcpy.SettingsException;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.os.Bundle;
|
||||
|
@ -87,7 +88,8 @@ public class ContentProvider implements Closeable {
|
|||
return attributionSource;
|
||||
}
|
||||
|
||||
private Bundle call(String callMethod, String arg, Bundle extras) {
|
||||
private Bundle call(String callMethod, String arg, Bundle extras)
|
||||
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||
try {
|
||||
Method method = getCallMethod();
|
||||
Object[] args;
|
||||
|
@ -108,7 +110,7 @@ public class ContentProvider implements Closeable {
|
|||
return (Bundle) method.invoke(provider, args);
|
||||
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | ClassNotFoundException | InstantiationException e) {
|
||||
Ln.e("Could not invoke method", e);
|
||||
return null;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -142,30 +144,31 @@ public class ContentProvider implements Closeable {
|
|||
}
|
||||
}
|
||||
|
||||
public String getValue(String table, String key) {
|
||||
public String getValue(String table, String key) throws SettingsException {
|
||||
String method = getGetMethod(table);
|
||||
Bundle arg = new Bundle();
|
||||
arg.putInt(CALL_METHOD_USER_KEY, ServiceManager.USER_ID);
|
||||
Bundle bundle = call(method, key, arg);
|
||||
if (bundle == null) {
|
||||
return null;
|
||||
try {
|
||||
Bundle bundle = call(method, key, arg);
|
||||
if (bundle == null) {
|
||||
return null;
|
||||
}
|
||||
return bundle.getString("value");
|
||||
} catch (Exception e) {
|
||||
throw new SettingsException(table, "get", key, null, e);
|
||||
}
|
||||
return bundle.getString("value");
|
||||
|
||||
}
|
||||
|
||||
public void putValue(String table, String key, String value) {
|
||||
public void putValue(String table, String key, String value) throws SettingsException {
|
||||
String method = getPutMethod(table);
|
||||
Bundle arg = new Bundle();
|
||||
arg.putInt(CALL_METHOD_USER_KEY, ServiceManager.USER_ID);
|
||||
arg.putString(NAME_VALUE_TABLE_VALUE, value);
|
||||
call(method, key, arg);
|
||||
}
|
||||
|
||||
public String getAndPutValue(String table, String key, String value) {
|
||||
String oldValue = getValue(table, key);
|
||||
if (!value.equals(oldValue)) {
|
||||
putValue(table, key, value);
|
||||
try {
|
||||
call(method, key, arg);
|
||||
} catch (Exception e) {
|
||||
throw new SettingsException(table, "put", key, value, e);
|
||||
}
|
||||
return oldValue;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue