2018-01-23 23:32:29 +08:00
|
|
|
#include "scrcpy.h"
|
|
|
|
|
2018-02-01 17:46:42 +08:00
|
|
|
#include <getopt.h>
|
2018-01-23 23:32:29 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
2018-02-07 19:02:15 +08:00
|
|
|
#include "config.h"
|
2018-02-13 17:10:18 +08:00
|
|
|
#include "log.h"
|
2018-01-23 23:32:29 +08:00
|
|
|
|
|
|
|
struct args {
|
|
|
|
const char *serial;
|
2018-02-01 18:44:09 +08:00
|
|
|
SDL_bool help;
|
2018-02-07 19:37:53 +08:00
|
|
|
SDL_bool version;
|
2018-03-25 21:23:00 +08:00
|
|
|
SDL_bool show_touches;
|
2018-01-23 23:32:29 +08:00
|
|
|
Uint16 port;
|
2018-02-01 19:18:06 +08:00
|
|
|
Uint16 max_size;
|
2018-02-01 23:36:50 +08:00
|
|
|
Uint32 bit_rate;
|
2018-01-23 23:32:29 +08:00
|
|
|
};
|
|
|
|
|
2018-02-01 18:44:09 +08:00
|
|
|
static void usage(const char *arg0) {
|
|
|
|
fprintf(stderr,
|
2018-02-15 01:57:29 +08:00
|
|
|
"Usage: %s [options]\n"
|
2018-02-01 18:44:09 +08:00
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
"\n"
|
2018-02-01 23:36:50 +08:00
|
|
|
" -b, --bit-rate value\n"
|
|
|
|
" Encode the video at the given bit-rate, expressed in bits/s.\n"
|
|
|
|
" Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n"
|
|
|
|
" Default is %d.\n"
|
|
|
|
"\n"
|
2018-02-01 18:44:09 +08:00
|
|
|
" -h, --help\n"
|
|
|
|
" Print this help.\n"
|
|
|
|
"\n"
|
|
|
|
" -m, --max-size value\n"
|
|
|
|
" Limit both the width and height of the video to value. The\n"
|
|
|
|
" other dimension is computed so that the device aspect-ratio\n"
|
|
|
|
" is preserved.\n"
|
|
|
|
" Default is %d%s.\n"
|
|
|
|
"\n"
|
|
|
|
" -p, --port port\n"
|
|
|
|
" Set the TCP port the client listens on.\n"
|
|
|
|
" Default is %d.\n"
|
|
|
|
"\n"
|
2018-02-15 01:57:29 +08:00
|
|
|
" -s, --serial\n"
|
|
|
|
" The device serial number. Mandatory only if several devices\n"
|
|
|
|
" are connected to adb.\n"
|
|
|
|
"\n"
|
2018-03-25 21:23:00 +08:00
|
|
|
" -t, --show-touches\n"
|
|
|
|
" Enable \"show touches\" on start, disable on quit.\n"
|
|
|
|
" It only shows physical touches (not clicks from scrcpy).\n"
|
|
|
|
"\n"
|
2018-02-07 19:37:53 +08:00
|
|
|
" -v, --version\n"
|
|
|
|
" Print the version of scrcpy.\n"
|
|
|
|
"\n"
|
2018-02-01 18:44:09 +08:00
|
|
|
"Shortcuts:\n"
|
|
|
|
"\n"
|
2018-02-02 22:10:44 +08:00
|
|
|
" Ctrl+f\n"
|
|
|
|
" switch fullscreen mode\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+g\n"
|
|
|
|
" resize window to 1:1 (pixel-perfect)\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+x\n"
|
2018-03-13 15:32:48 +08:00
|
|
|
" Double-click on black borders\n"
|
2018-02-15 21:00:57 +08:00
|
|
|
" resize window to remove black borders\n"
|
2018-02-02 22:10:44 +08:00
|
|
|
"\n"
|
|
|
|
" Ctrl+h\n"
|
|
|
|
" Home\n"
|
2018-03-10 07:40:55 +08:00
|
|
|
" Middle-click\n"
|
2018-02-02 22:10:44 +08:00
|
|
|
" click on HOME\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+b\n"
|
|
|
|
" Ctrl+Backspace\n"
|
2018-03-10 07:40:55 +08:00
|
|
|
" Right-click (when screen is on)\n"
|
2018-02-02 22:10:44 +08:00
|
|
|
" click on BACK\n"
|
|
|
|
"\n"
|
2018-03-23 17:10:24 +08:00
|
|
|
" Ctrl+s\n"
|
2018-02-02 22:10:44 +08:00
|
|
|
" click on APP_SWITCH\n"
|
|
|
|
"\n"
|
2018-03-23 17:10:24 +08:00
|
|
|
" Ctrl+m\n"
|
2018-03-21 09:04:40 +08:00
|
|
|
" click on MENU\n"
|
|
|
|
"\n"
|
2018-02-02 22:10:44 +08:00
|
|
|
" Ctrl+'+'\n"
|
|
|
|
" click on VOLUME_UP\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+'-'\n"
|
|
|
|
" click on VOLUME_DOWN\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+p\n"
|
|
|
|
" click on POWER (turn screen on/off)\n"
|
|
|
|
"\n"
|
2018-03-10 07:40:55 +08:00
|
|
|
" Right-click (when screen is off)\n"
|
2018-02-02 22:10:44 +08:00
|
|
|
" turn screen on\n"
|
2018-02-15 19:24:16 +08:00
|
|
|
"\n"
|
2018-03-07 22:29:33 +08:00
|
|
|
" Ctrl+v\n"
|
|
|
|
" paste computer clipboard to device\n"
|
|
|
|
"\n"
|
2018-02-15 19:24:16 +08:00
|
|
|
" Ctrl+i\n"
|
|
|
|
" enable/disable FPS counter (print frames/second in logs)\n"
|
2018-02-01 18:44:09 +08:00
|
|
|
"\n",
|
|
|
|
arg0,
|
2018-02-01 23:36:50 +08:00
|
|
|
DEFAULT_BIT_RATE,
|
2018-02-01 18:44:09 +08:00
|
|
|
DEFAULT_MAX_SIZE, DEFAULT_MAX_SIZE ? "" : " (unlimited)",
|
|
|
|
DEFAULT_LOCAL_PORT);
|
|
|
|
}
|
|
|
|
|
2018-02-07 22:01:19 +08:00
|
|
|
static void print_version(void) {
|
|
|
|
fprintf(stderr, "scrcpy v%s\n\n", SCRCPY_VERSION);
|
|
|
|
|
|
|
|
fprintf(stderr, "dependencies:\n");
|
|
|
|
fprintf(stderr, " - SDL %d.%d.%d\n", SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL);
|
|
|
|
fprintf(stderr, " - libavcodec %d.%d.%d\n", LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO);
|
|
|
|
fprintf(stderr, " - libavformat %d.%d.%d\n", LIBAVFORMAT_VERSION_MAJOR, LIBAVFORMAT_VERSION_MINOR, LIBAVFORMAT_VERSION_MICRO);
|
|
|
|
fprintf(stderr, " - libavutil %d.%d.%d\n", LIBAVUTIL_VERSION_MAJOR, LIBAVUTIL_VERSION_MINOR, LIBAVUTIL_VERSION_MICRO);
|
|
|
|
}
|
|
|
|
|
2018-02-15 01:53:16 +08:00
|
|
|
static SDL_bool parse_bit_rate(char *optarg, Uint32 *bit_rate) {
|
|
|
|
char *endptr;
|
|
|
|
if (*optarg == '\0') {
|
|
|
|
LOGE("Bit-rate parameter is empty");
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
long value = strtol(optarg, &endptr, 0);
|
|
|
|
int mul = 1;
|
|
|
|
if (*endptr != '\0') {
|
|
|
|
if (optarg == endptr) {
|
|
|
|
LOGE("Invalid bit-rate: %s", optarg);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
if ((*endptr == 'M' || *endptr == 'm') && endptr[1] == '\0') {
|
|
|
|
mul = 1000000;
|
|
|
|
} else if ((*endptr == 'K' || *endptr == 'k') && endptr[1] == '\0') {
|
|
|
|
mul = 1000;
|
|
|
|
} else {
|
|
|
|
LOGE("Invalid bit-rate unit: %s", optarg);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value < 0 || ((Uint32) -1) / mul < value) {
|
|
|
|
LOGE("Bitrate must be positive and less than 2^32: %s", optarg);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bit_rate = (Uint32) value * mul;
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_bool parse_max_size(char *optarg, Uint16 *max_size) {
|
|
|
|
char *endptr;
|
|
|
|
if (*optarg == '\0') {
|
|
|
|
LOGE("Max size parameter is empty");
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
long value = strtol(optarg, &endptr, 0);
|
|
|
|
if (*endptr != '\0') {
|
|
|
|
LOGE("Invalid max size: %s", optarg);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
if (value & ~0xffff) {
|
|
|
|
LOGE("Max size must be between 0 and 65535: %ld", value);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*max_size = (Uint16) value;
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_bool parse_port(char *optarg, Uint16 *port) {
|
|
|
|
char *endptr;
|
|
|
|
if (*optarg == '\0') {
|
|
|
|
LOGE("Invalid port parameter is empty");
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
long value = strtol(optarg, &endptr, 0);
|
|
|
|
if (*endptr != '\0') {
|
|
|
|
LOGE("Invalid port: %s", optarg);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
if (value & ~0xffff) {
|
|
|
|
LOGE("Port out of range: %ld", value);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*port = (Uint16) value;
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
2018-02-15 01:44:28 +08:00
|
|
|
static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
|
2018-02-01 17:46:42 +08:00
|
|
|
static const struct option long_options[] = {
|
2018-03-25 21:23:00 +08:00
|
|
|
{"bit-rate", required_argument, NULL, 'b'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"max-size", required_argument, NULL, 'm'},
|
|
|
|
{"port", required_argument, NULL, 'p'},
|
|
|
|
{"serial", required_argument, NULL, 's'},
|
|
|
|
{"show-touches", no_argument, NULL, 't'},
|
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
{NULL, 0, NULL, 0 },
|
2018-02-01 17:46:42 +08:00
|
|
|
};
|
2018-01-23 23:32:29 +08:00
|
|
|
int c;
|
2018-03-25 21:23:00 +08:00
|
|
|
while ((c = getopt_long(argc, argv, "b:hm:p:s:tv", long_options, NULL)) != -1) {
|
2018-01-23 23:32:29 +08:00
|
|
|
switch (c) {
|
2018-03-21 18:14:15 +08:00
|
|
|
case 'b':
|
2018-02-15 01:53:16 +08:00
|
|
|
if (!parse_bit_rate(optarg, &args->bit_rate)) {
|
2018-02-15 01:44:28 +08:00
|
|
|
return SDL_FALSE;
|
2018-02-01 23:31:59 +08:00
|
|
|
}
|
2018-02-15 01:39:46 +08:00
|
|
|
break;
|
2018-03-21 18:14:15 +08:00
|
|
|
case 'h':
|
2018-02-15 01:39:46 +08:00
|
|
|
args->help = SDL_TRUE;
|
2018-02-01 18:34:49 +08:00
|
|
|
break;
|
2018-03-21 18:14:15 +08:00
|
|
|
case 'm':
|
2018-02-15 01:53:16 +08:00
|
|
|
if (!parse_max_size(optarg, &args->max_size)) {
|
2018-02-15 01:44:28 +08:00
|
|
|
return SDL_FALSE;
|
2018-02-01 23:31:59 +08:00
|
|
|
}
|
2018-02-01 18:34:49 +08:00
|
|
|
break;
|
2018-03-21 18:14:15 +08:00
|
|
|
case 'p':
|
2018-02-15 01:53:16 +08:00
|
|
|
if (!parse_port(optarg, &args->port)) {
|
2018-02-15 01:44:28 +08:00
|
|
|
return SDL_FALSE;
|
2018-02-01 23:36:50 +08:00
|
|
|
}
|
2018-02-15 01:39:46 +08:00
|
|
|
break;
|
2018-03-21 18:14:15 +08:00
|
|
|
case 's':
|
2018-02-15 01:57:29 +08:00
|
|
|
args->serial = optarg;
|
|
|
|
break;
|
2018-03-25 21:23:00 +08:00
|
|
|
case 't':
|
|
|
|
args->show_touches = SDL_TRUE;
|
|
|
|
break;
|
2018-03-21 18:14:15 +08:00
|
|
|
case 'v':
|
2018-02-15 01:39:46 +08:00
|
|
|
args->version = SDL_TRUE;
|
2018-02-01 23:36:50 +08:00
|
|
|
break;
|
2018-02-01 18:34:49 +08:00
|
|
|
default:
|
|
|
|
// getopt prints the error message on stderr
|
2018-02-15 01:44:28 +08:00
|
|
|
return SDL_FALSE;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = optind;
|
|
|
|
if (index < argc) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGE("Unexpected additional argument: %s", argv[index]);
|
2018-02-15 01:44:28 +08:00
|
|
|
return SDL_FALSE;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
2018-02-15 01:44:28 +08:00
|
|
|
return SDL_TRUE;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2018-03-13 17:20:09 +08:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
// disable buffering, we want logs immediately
|
|
|
|
// even line buffering (setvbuf() with mode _IOLBF) is not sufficient
|
|
|
|
setbuf(stdout, NULL);
|
|
|
|
setbuf(stderr, NULL);
|
|
|
|
#endif
|
2018-01-23 23:32:29 +08:00
|
|
|
struct args args = {
|
|
|
|
.serial = NULL,
|
2018-02-01 23:36:50 +08:00
|
|
|
.help = SDL_FALSE,
|
2018-02-07 19:37:53 +08:00
|
|
|
.version = SDL_FALSE,
|
2018-03-25 21:23:00 +08:00
|
|
|
.show_touches = SDL_FALSE,
|
2018-01-23 23:32:29 +08:00
|
|
|
.port = DEFAULT_LOCAL_PORT,
|
2018-02-01 23:36:50 +08:00
|
|
|
.max_size = DEFAULT_MAX_SIZE,
|
|
|
|
.bit_rate = DEFAULT_BIT_RATE,
|
2018-01-23 23:32:29 +08:00
|
|
|
};
|
2018-02-15 01:44:28 +08:00
|
|
|
if (!parse_args(&args, argc, argv)) {
|
2018-01-23 23:32:29 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-02-01 18:44:09 +08:00
|
|
|
if (args.help) {
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-07 19:37:53 +08:00
|
|
|
if (args.version) {
|
2018-02-07 22:01:19 +08:00
|
|
|
print_version();
|
2018-02-07 19:37:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
av_register_all();
|
|
|
|
|
|
|
|
if (avformat_network_init()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-02-12 18:07:38 +08:00
|
|
|
#ifdef BUILD_DEBUG
|
2018-01-23 23:32:29 +08:00
|
|
|
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG);
|
2018-02-12 18:07:38 +08:00
|
|
|
#endif
|
2018-01-23 23:32:29 +08:00
|
|
|
|
2018-03-21 21:04:13 +08:00
|
|
|
struct scrcpy_options options = {
|
|
|
|
.serial = args.serial,
|
|
|
|
.port = args.port,
|
|
|
|
.max_size = args.max_size,
|
|
|
|
.bit_rate = args.bit_rate,
|
2018-03-25 21:23:00 +08:00
|
|
|
.show_touches = args.show_touches,
|
2018-03-21 21:04:13 +08:00
|
|
|
};
|
|
|
|
int res = scrcpy(&options) ? 0 : 1;
|
2018-01-23 23:32:29 +08:00
|
|
|
|
|
|
|
avformat_network_deinit(); // ignore failure
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|