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>
|
|
|
|
|
|
|
|
#define DEFAULT_LOCAL_PORT 27183
|
2018-02-01 19:18:06 +08:00
|
|
|
#define DEFAULT_MAX_SIZE 0
|
2018-01-23 23:32:29 +08:00
|
|
|
|
|
|
|
struct args {
|
|
|
|
const char *serial;
|
|
|
|
Uint16 port;
|
2018-02-01 19:18:06 +08:00
|
|
|
Uint16 max_size;
|
2018-01-23 23:32:29 +08:00
|
|
|
};
|
|
|
|
|
2018-02-01 17:46:42 +08:00
|
|
|
static int parse_args(struct args *args, int argc, char *argv[]) {
|
|
|
|
static const struct option long_options[] = {
|
|
|
|
{"port", required_argument, NULL, 'p'},
|
|
|
|
{"max-size", required_argument, NULL, 'm'},
|
|
|
|
{NULL, 0, NULL, 0 },
|
|
|
|
};
|
2018-01-23 23:32:29 +08:00
|
|
|
int c;
|
2018-02-01 17:46:42 +08:00
|
|
|
while ((c = getopt_long(argc, argv, "p:m:", long_options, NULL)) != -1) {
|
2018-01-23 23:32:29 +08:00
|
|
|
switch (c) {
|
2018-02-01 18:34:49 +08:00
|
|
|
case 'p': {
|
|
|
|
char *endptr;
|
|
|
|
long value = strtol(optarg, &endptr, 0);
|
|
|
|
if (*optarg == '\0' || *endptr != '\0') {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid port: %s\n", optarg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value & ~0xffff) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Port out of range: %ld\n", value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
args->port = (Uint16) value;
|
|
|
|
break;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
2018-02-01 18:34:49 +08:00
|
|
|
case 'm': {
|
|
|
|
char *endptr;
|
|
|
|
long value = strtol(optarg, &endptr, 0);
|
|
|
|
if (*optarg == '\0' || *endptr != '\0') {
|
2018-02-01 19:18:06 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid max size: %s\n", optarg);
|
2018-02-01 18:34:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value & ~0xffff) {
|
2018-02-01 19:18:06 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Max size must be between 0 and 65535: %ld\n", value);
|
2018-02-01 18:34:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2018-02-01 19:18:06 +08:00
|
|
|
args->max_size = (Uint16) value;
|
2018-02-01 18:34:49 +08:00
|
|
|
break;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
2018-02-01 18:34:49 +08:00
|
|
|
default:
|
|
|
|
// getopt prints the error message on stderr
|
2018-01-29 22:40:33 +08:00
|
|
|
return -1;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = optind;
|
|
|
|
if (index < argc) {
|
|
|
|
args->serial = argv[index++];
|
|
|
|
}
|
|
|
|
if (index < argc) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unexpected additional argument: %s\n", argv[index]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int res;
|
|
|
|
|
|
|
|
struct args args = {
|
|
|
|
.serial = NULL,
|
2018-02-01 19:18:06 +08:00
|
|
|
.max_size = DEFAULT_MAX_SIZE,
|
2018-01-23 23:32:29 +08:00
|
|
|
.port = DEFAULT_LOCAL_PORT,
|
|
|
|
};
|
|
|
|
if (parse_args(&args, argc, argv)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_register_all();
|
|
|
|
|
|
|
|
if (avformat_network_init()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG);
|
|
|
|
|
2018-02-01 19:18:06 +08:00
|
|
|
res = scrcpy(args.serial, args.port, args.max_size) ? 0 : 1;
|
2018-01-23 23:32:29 +08:00
|
|
|
|
|
|
|
avformat_network_deinit(); // ignore failure
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|