diff --git a/BUILD.md b/BUILD.md index e79cc6ca..d61518dc 100644 --- a/BUILD.md +++ b/BUILD.md @@ -234,10 +234,10 @@ You can then [run](README.md#run) _scrcpy_. ## Prebuilt server - - [`scrcpy-server-v1.6.jar`][direct-scrcpy-server] - _(SHA-256: 08df924bf6d10943df9eaacfff548a99871ebfca4641f8c7ddddb73f27cb905b)_ + - [`scrcpy-server-v1.7.jar`][direct-scrcpy-server] + _(SHA-256: ee86ec8424f7dc50cacdf927312bdb46e0aa0d68611da584dc4b16d8057bc25e)_ -[direct-scrcpy-server]: https://github.com/Genymobile/scrcpy/releases/download/v1.6/scrcpy-server-v1.6.jar +[direct-scrcpy-server]: https://github.com/Genymobile/scrcpy/releases/download/v1.7/scrcpy-server-v1.7.jar Download the prebuilt server somewhere, and specify its path during the Meson configuration: diff --git a/README.md b/README.md index aca991b6..1fdb2c83 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# scrcpy (v1.6) +# scrcpy (v1.7) This application provides display and control of Android devices connected on USB (or [over TCP/IP][article-tcpip]). It does not require any _root_ access. @@ -47,13 +47,13 @@ For Gentoo, an [Ebuild] is available: [`scrcpy/`][ebuild-link]. For Windows, for simplicity, prebuilt archives with all the dependencies (including `adb`) are available: - - [`scrcpy-win32-v1.6.zip`][direct-win32] - _(SHA-256: 4ca0c5924ab2ebf19b70f6598b2e546f65ba469a72ded2d1b213df3380fb46b1)_ - - [`scrcpy-win64-v1.6.zip`][direct-win64] - _(SHA-256: f66b7eace8dd6537a9a27176fd824704a284d8e82077ccc903344396043f90c9)_ + - [`scrcpy-win32-v1.7.zip`][direct-win32] + _(SHA-256: 98ae36f2da0b8212c07066fd93139650554274f863d4cee0781501a0c84f7c23)_ + - [`scrcpy-win64-v1.7.zip`][direct-win64] + _(SHA-256: b41416547521062f19e3f3f539e89a70e713bd086e69ef1b29c128993f7aa462)_ -[direct-win32]: https://github.com/Genymobile/scrcpy/releases/download/v1.6/scrcpy-win32-v1.6.zip -[direct-win64]: https://github.com/Genymobile/scrcpy/releases/download/v1.6/scrcpy-win64-v1.6.zip +[direct-win32]: https://github.com/Genymobile/scrcpy/releases/download/v1.7/scrcpy-win32-v1.7.zip +[direct-win64]: https://github.com/Genymobile/scrcpy/releases/download/v1.7/scrcpy-win64-v1.7.zip You can also [build the app manually][BUILD]. @@ -162,7 +162,7 @@ It is possible to record the screen while mirroring: ```bash scrcpy --record file.mp4 -scrcpy -r file.mp4 +scrcpy -r file.mkv ``` "Skipped frames" are recorded, even if they are not displayed in real time (for @@ -196,6 +196,16 @@ scrcpy -f # short version Fullscreen can then be toggled dynamically with `Ctrl`+`f`. +### Always on top + +The window of app can always be above others by: + +```bash +scrcpy --always-on-top +scrcpy -T # short version +``` + + ### Show touches For presentations, it may be useful to show physical touches (on the physical diff --git a/app/src/command.h b/app/src/command.h index 98b3a881..6264adf4 100644 --- a/app/src/command.h +++ b/app/src/command.h @@ -3,33 +3,33 @@ #include #include -#include -// #ifdef _WIN32 + +# include // not needed here, but must never be included AFTER windows.h +# include # define PRIexitcode "lu" +// # ifdef _WIN64 # define PRIsizet PRIu64 # else # define PRIsizet PRIu32 # endif -#else -# define PRIsizet "zu" -# define PRIexitcode "d" -#endif - -#ifdef __WINDOWS__ -# include // not needed here, but must never be included AFTER windows.h -# include # define PROCESS_NONE NULL typedef HANDLE process_t; typedef DWORD exit_code_t; + #else + # include +# define PRIsizet "zu" +# define PRIexitcode "d" # define PROCESS_NONE -1 typedef pid_t process_t; typedef int exit_code_t; + #endif + # define NO_EXIT_CODE -1 enum process_result { diff --git a/app/src/compat.h b/app/src/compat.h new file mode 100644 index 00000000..fd68d61a --- /dev/null +++ b/app/src/compat.h @@ -0,0 +1,46 @@ +#ifndef COMPAT_H +#define COMPAT_H + +#include +#include + +// In ffmpeg/doc/APIchanges: +// 2016-04-11 - 6f69f7a / 9200514 - lavf 57.33.100 / 57.5.0 - avformat.h +// Add AVStream.codecpar, deprecate AVStream.codec. +#if (LIBAVFORMAT_VERSION_MICRO >= 100 /* FFmpeg */ && \ + LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)) \ + || (LIBAVFORMAT_VERSION_MICRO < 100 && /* Libav */ \ + LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 5, 0)) +# define SCRCPY_LAVF_HAS_NEW_CODEC_PARAMS_API +#endif + +// In ffmpeg/doc/APIchanges: +// 2018-02-06 - 0694d87024 - lavf 58.9.100 - avformat.h +// Deprecate use of av_register_input_format(), av_register_output_format(), +// av_register_all(), av_iformat_next(), av_oformat_next(). +// Add av_demuxer_iterate(), and av_muxer_iterate(). +#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 9, 100) +# define SCRCPY_LAVF_HAS_NEW_MUXER_ITERATOR_API +#else +# define SCRCPY_LAVF_REQUIRES_REGISTER_ALL +#endif + +// In ffmpeg/doc/APIchanges: +// 2016-04-21 - 7fc329e - lavc 57.37.100 - avcodec.h +// Add a new audio/video encoding and decoding API with decoupled input +// and output -- avcodec_send_packet(), avcodec_receive_frame(), +// avcodec_send_frame() and avcodec_receive_packet(). +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 100) +# define SCRCPY_LAVF_HAS_NEW_ENCODING_DECODING_API +#endif + +#if SDL_VERSION_ATLEAST(2, 0, 5) +// +# define SCRCPY_SDL_HAS_HINT_MOUSE_FOCUS_CLICKTHROUGH +// +# define SCRCPY_SDL_HAS_GET_DISPLAY_USABLE_BOUNDS +// +# define SCRCPY_SDL_HAS_WINDOW_ALWAYS_ON_TOP +#endif + +#endif diff --git a/app/src/decoder.c b/app/src/decoder.c index fd429147..9b3ca2f1 100644 --- a/app/src/decoder.c +++ b/app/src/decoder.c @@ -8,6 +8,7 @@ #include #include +#include "compat.h" #include "config.h" #include "buffer_util.h" #include "events.h" @@ -220,7 +221,7 @@ static int run_decoder(void *data) { while (!av_read_frame(format_ctx, &packet)) { // the new decoding/encoding API has been introduced by: // -#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 0) +#ifdef SCRCPY_LAVF_HAS_NEW_ENCODING_DECODING_API int ret; if ((ret = avcodec_send_packet(codec_ctx, &packet)) < 0) { LOGE("Could not send video packet: %d", ret); @@ -284,7 +285,8 @@ run_quit: run_finally_close_input: avformat_close_input(&format_ctx); run_finally_free_avio_ctx: - av_freep(&avio_ctx); + av_free(avio_ctx->buffer); + av_free(avio_ctx); run_finally_free_format_ctx: avformat_free_context(format_ctx); run_finally_close_codec: diff --git a/app/src/log.h b/app/src/log.h index 7e5435c5..5955c7fb 100644 --- a/app/src/log.h +++ b/app/src/log.h @@ -3,6 +3,7 @@ #include +#define LOGV(...) SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define LOGD(...) SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define LOGI(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define LOGW(...) SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) diff --git a/app/src/main.c b/app/src/main.c index 0603fef5..35bb74ea 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -5,13 +5,16 @@ #include #include +#include "compat.h" #include "config.h" #include "log.h" +#include "recorder.h" struct args { const char *serial; const char *crop; const char *record_filename; + enum recorder_format record_format; SDL_bool fullscreen; SDL_bool help; SDL_bool version; @@ -19,6 +22,7 @@ struct args { Uint16 port; Uint16 max_size; Uint32 bit_rate; + SDL_bool always_on_top; }; static void usage(const char *arg0) { @@ -41,6 +45,9 @@ static void usage(const char *arg0) { " -f, --fullscreen\n" " Start in fullscreen.\n" "\n" + " -F, --record-format\n" + " Force recording format (either mp4 or mkv).\n" + "\n" " -h, --help\n" " Print this help.\n" "\n" @@ -56,6 +63,8 @@ static void usage(const char *arg0) { "\n" " -r, --record file.mp4\n" " Record screen to file.\n" + " The format is determined by the -F/--record-format option if\n" + " set, or by the file extension (.mp4 or .mkv).\n" "\n" " -s, --serial\n" " The device serial number. Mandatory only if several devices\n" @@ -65,6 +74,9 @@ static void usage(const char *arg0) { " Enable \"show touches\" on start, disable on quit.\n" " It only shows physical touches (not clicks from scrcpy).\n" "\n" + " -T, --always-on-top\n" + " Make scrcpy window always on top (above other windows).\n" + "\n" " -v, --version\n" " Print the version of scrcpy.\n" "\n" @@ -204,22 +216,54 @@ static SDL_bool parse_port(char *optarg, Uint16 *port) { return SDL_TRUE; } +static SDL_bool +parse_record_format(const char *optarg, enum recorder_format *format) { + if (!strcmp(optarg, "mp4")) { + *format = RECORDER_FORMAT_MP4; + return SDL_TRUE; + } + if (!strcmp(optarg, "mkv")) { + *format = RECORDER_FORMAT_MKV; + return SDL_TRUE; + } + LOGE("Unsupported format: %s (expected mp4 or mkv)", optarg); + return SDL_FALSE; +} + +static enum recorder_format +guess_record_format(const char *filename) { + size_t len = strlen(filename); + if (len < 4) { + return 0; + } + const char *ext = &filename[len - 4]; + if (!strcmp(ext, ".mp4")) { + return RECORDER_FORMAT_MP4; + } + if (!strcmp(ext, ".mkv")) { + return RECORDER_FORMAT_MKV; + } + return 0; +} + static SDL_bool parse_args(struct args *args, int argc, char *argv[]) { static const struct option long_options[] = { - {"bit-rate", required_argument, NULL, 'b'}, - {"crop", required_argument, NULL, 'c'}, - {"fullscreen", no_argument, NULL, 'f'}, - {"help", no_argument, NULL, 'h'}, - {"max-size", required_argument, NULL, 'm'}, - {"port", required_argument, NULL, 'p'}, - {"record", required_argument, NULL, 'r'}, - {"serial", required_argument, NULL, 's'}, - {"show-touches", no_argument, NULL, 't'}, - {"version", no_argument, NULL, 'v'}, - {NULL, 0, NULL, 0 }, + {"always-on-top", no_argument, NULL, 'T'}, + {"bit-rate", required_argument, NULL, 'b'}, + {"crop", required_argument, NULL, 'c'}, + {"fullscreen", no_argument, NULL, 'f'}, + {"help", no_argument, NULL, 'h'}, + {"max-size", required_argument, NULL, 'm'}, + {"port", required_argument, NULL, 'p'}, + {"record", required_argument, NULL, 'r'}, + {"record-format", required_argument, NULL, 'f'}, + {"serial", required_argument, NULL, 's'}, + {"show-touches", no_argument, NULL, 't'}, + {"version", no_argument, NULL, 'v'}, + {NULL, 0, NULL, 0 }, }; int c; - while ((c = getopt_long(argc, argv, "b:c:fhm:p:r:s:tv", long_options, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "b:c:fF:hm:p:r:s:tTv", long_options, NULL)) != -1) { switch (c) { case 'b': if (!parse_bit_rate(optarg, &args->bit_rate)) { @@ -232,6 +276,11 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) { case 'f': args->fullscreen = SDL_TRUE; break; + case 'F': + if (!parse_record_format(optarg, &args->record_format)) { + return SDL_FALSE; + } + break; case 'h': args->help = SDL_TRUE; break; @@ -254,6 +303,9 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) { case 't': args->show_touches = SDL_TRUE; break; + case 'T': + args->always_on_top = SDL_TRUE; + break; case 'v': args->version = SDL_TRUE; break; @@ -268,6 +320,21 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) { LOGE("Unexpected additional argument: %s", argv[index]); return SDL_FALSE; } + + if (args->record_format && !args->record_filename) { + LOGE("Record format specified without recording"); + return SDL_FALSE; + } + + if (args->record_filename && !args->record_format) { + args->record_format = guess_record_format(args->record_filename); + if (!args->record_format) { + LOGE("No format specified for \"%s\" (try with -F mkv)", + args->record_filename); + return SDL_FALSE; + } + } + return SDL_TRUE; } @@ -282,12 +349,14 @@ int main(int argc, char *argv[]) { .serial = NULL, .crop = NULL, .record_filename = NULL, + .record_format = 0, .help = SDL_FALSE, .version = SDL_FALSE, .show_touches = SDL_FALSE, .port = DEFAULT_LOCAL_PORT, .max_size = DEFAULT_MAX_SIZE, .bit_rate = DEFAULT_BIT_RATE, + .always_on_top = SDL_FALSE, }; if (!parse_args(&args, argc, argv)) { return 1; @@ -303,7 +372,7 @@ int main(int argc, char *argv[]) { return 0; } -#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100) +#ifdef SCRCPY_LAVF_REQUIRES_REGISTER_ALL av_register_all(); #endif @@ -320,10 +389,12 @@ int main(int argc, char *argv[]) { .crop = args.crop, .port = args.port, .record_filename = args.record_filename, + .record_format = args.record_format, .max_size = args.max_size, .bit_rate = args.bit_rate, .show_touches = args.show_touches, .fullscreen = args.fullscreen, + .always_on_top = args.always_on_top, }; int res = scrcpy(&options) ? 0 : 1; diff --git a/app/src/recorder.c b/app/src/recorder.c index 2e846e91..2d47487f 100644 --- a/app/src/recorder.c +++ b/app/src/recorder.c @@ -1,27 +1,33 @@ #include "recorder.h" #include +#include +#include "compat.h" #include "config.h" #include "log.h" -static const AVOutputFormat *find_mp4_muxer(void) { -#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 9, 100) +static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us + +static const AVOutputFormat *find_muxer(const char *name) { +#ifdef SCRCPY_LAVF_HAS_NEW_MUXER_ITERATOR_API void *opaque = NULL; #endif const AVOutputFormat *oformat = NULL; do { -#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 9, 100) +#ifdef SCRCPY_LAVF_HAS_NEW_MUXER_ITERATOR_API oformat = av_muxer_iterate(&opaque); #else oformat = av_oformat_next(oformat); #endif // until null or with name "mp4" - } while (oformat && strcmp(oformat->name, "mp4")); + } while (oformat && strcmp(oformat->name, name)); return oformat; } -SDL_bool recorder_init(struct recorder *recorder, const char *filename, +SDL_bool recorder_init(struct recorder *recorder, + const char *filename, + enum recorder_format format, struct size declared_frame_size) { recorder->filename = SDL_strdup(filename); if (!recorder->filename) { @@ -29,7 +35,9 @@ SDL_bool recorder_init(struct recorder *recorder, const char *filename, return SDL_FALSE; } + recorder->format = format; recorder->declared_frame_size = declared_frame_size; + recorder->header_written = SDL_FALSE; return SDL_TRUE; } @@ -38,10 +46,21 @@ void recorder_destroy(struct recorder *recorder) { SDL_free(recorder->filename); } +static const char * +recorder_get_format_name(enum recorder_format format) { + switch (format) { + case RECORDER_FORMAT_MP4: return "mp4"; + case RECORDER_FORMAT_MKV: return "matroska"; + default: return NULL; + } +} + SDL_bool recorder_open(struct recorder *recorder, AVCodec *input_codec) { - const AVOutputFormat *mp4 = find_mp4_muxer(); - if (!mp4) { - LOGE("Could not find mp4 muxer"); + const char *format_name = recorder_get_format_name(recorder->format); + SDL_assert(format_name); + const AVOutputFormat *format = find_muxer(format_name); + if (!format) { + LOGE("Could not find muxer"); return SDL_FALSE; } @@ -55,7 +74,7 @@ SDL_bool recorder_open(struct recorder *recorder, AVCodec *input_codec) { // returns (on purpose) a pointer-to-const, but AVFormatContext.oformat // still expects a pointer-to-non-const (it has not be updated accordingly) // - recorder->ctx->oformat = (AVOutputFormat *) mp4; + recorder->ctx->oformat = (AVOutputFormat *) format; AVStream *ostream = avformat_new_stream(recorder->ctx, input_codec); if (!ostream) { @@ -63,13 +82,7 @@ SDL_bool recorder_open(struct recorder *recorder, AVCodec *input_codec) { return SDL_FALSE; } -// In ffmpeg/doc/APIchanges: -// 2016-04-11 - 6f69f7a / 9200514 - lavf 57.33.100 / 57.5.0 - avformat.h -// Add AVStream.codecpar, deprecate AVStream.codec. -#if (LIBAVFORMAT_VERSION_MICRO >= 100 /* FFmpeg */ && \ - LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)) \ - || (LIBAVFORMAT_VERSION_MICRO < 100 && /* Libav */ \ - LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 5, 0)) +#ifdef SCRCPY_LAVF_HAS_NEW_CODEC_PARAMS_API ostream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; ostream->codecpar->codec_id = input_codec->id; ostream->codecpar->format = AV_PIX_FMT_YUV420P; @@ -82,7 +95,6 @@ SDL_bool recorder_open(struct recorder *recorder, AVCodec *input_codec) { ostream->codec->width = recorder->declared_frame_size.width; ostream->codec->height = recorder->declared_frame_size.height; #endif - ostream->time_base = (AVRational) {1, 1000000}; // timestamps in us int ret = avio_open(&recorder->ctx->pb, recorder->filename, AVIO_FLAG_WRITE); @@ -93,13 +105,7 @@ SDL_bool recorder_open(struct recorder *recorder, AVCodec *input_codec) { return SDL_FALSE; } - ret = avformat_write_header(recorder->ctx, NULL); - if (ret < 0) { - LOGE("Failed to write header to %s", recorder->filename); - avio_closep(&recorder->ctx->pb); - avformat_free_context(recorder->ctx); - return SDL_FALSE; - } + LOGI("Recording started to %s file: %s", format_name, recorder->filename); return SDL_TRUE; } @@ -111,8 +117,59 @@ void recorder_close(struct recorder *recorder) { } avio_close(recorder->ctx->pb); avformat_free_context(recorder->ctx); + + const char *format_name = recorder_get_format_name(recorder->format); + LOGI("Recording complete to %s file: %s", format_name, recorder->filename); +} + +static SDL_bool +recorder_write_header(struct recorder *recorder, AVPacket *packet) { + AVStream *ostream = recorder->ctx->streams[0]; + + uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t)); + if (!extradata) { + LOGC("Cannot allocate extradata"); + return SDL_FALSE; + } + + // copy the first packet to the extra data + memcpy(extradata, packet->data, packet->size); + +#ifdef SCRCPY_LAVF_HAS_NEW_CODEC_PARAMS_API + ostream->codecpar->extradata = extradata; + ostream->codecpar->extradata_size = packet->size; +#else + ostream->codec->extradata = extradata; + ostream->codec->extradata_size = packet->size; +#endif + + int ret = avformat_write_header(recorder->ctx, NULL); + if (ret < 0) { + LOGE("Failed to write header to %s", recorder->filename); + SDL_free(extradata); + avio_closep(&recorder->ctx->pb); + avformat_free_context(recorder->ctx); + return SDL_FALSE; + } + + return SDL_TRUE; +} + +static void +recorder_rescale_packet(struct recorder *recorder, AVPacket *packet) { + AVStream *ostream = recorder->ctx->streams[0]; + av_packet_rescale_ts(packet, SCRCPY_TIME_BASE, ostream->time_base); } SDL_bool recorder_write(struct recorder *recorder, AVPacket *packet) { + if (!recorder->header_written) { + SDL_bool ok = recorder_write_header(recorder, packet); + if (!ok) { + return SDL_FALSE; + } + recorder->header_written = SDL_TRUE; + } + + recorder_rescale_packet(recorder, packet); return av_write_frame(recorder->ctx, packet) >= 0; } diff --git a/app/src/recorder.h b/app/src/recorder.h index 9cb17fe0..203c51b4 100644 --- a/app/src/recorder.h +++ b/app/src/recorder.h @@ -6,14 +6,24 @@ #include "common.h" -struct recorder { - char *filename; - AVFormatContext *ctx; - struct size declared_frame_size; +enum recorder_format { + RECORDER_FORMAT_MP4 = 1, + RECORDER_FORMAT_MKV, }; -SDL_bool recorder_init(struct recorder *recoder, const char *filename, +struct recorder { + char *filename; + enum recorder_format format; + AVFormatContext *ctx; + struct size declared_frame_size; + SDL_bool header_written; +}; + +SDL_bool recorder_init(struct recorder *recoder, + const char *filename, + enum recorder_format format, struct size declared_frame_size); + void recorder_destroy(struct recorder *recorder); SDL_bool recorder_open(struct recorder *recorder, AVCodec *input_codec); diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 0e9bcba0..9a1c29d5 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -139,6 +139,40 @@ static void wait_show_touches(process_t process) { process_check_success(process, "show_touches"); } +static SDL_LogPriority sdl_priority_from_av_level(int level) { + switch (level) { + case AV_LOG_PANIC: + case AV_LOG_FATAL: + return SDL_LOG_PRIORITY_CRITICAL; + case AV_LOG_ERROR: + return SDL_LOG_PRIORITY_ERROR; + case AV_LOG_WARNING: + return SDL_LOG_PRIORITY_WARN; + case AV_LOG_INFO: + return SDL_LOG_PRIORITY_INFO; + } + // do not forward others, which are too verbose + return 0; +} + +static void +av_log_callback(void *avcl, int level, const char *fmt, va_list vl) { + SDL_LogPriority priority = sdl_priority_from_av_level(level); + if (priority == 0) { + return; + } + char *local_fmt = SDL_malloc(strlen(fmt) + 10); + if (!local_fmt) { + LOGC("Cannot allocate string"); + return; + } + // strcpy is safe here, the destination is large enough + strcpy(local_fmt, "[FFmpeg] "); + strcpy(local_fmt + 9, fmt); + SDL_LogMessageV(SDL_LOG_CATEGORY_VIDEO, priority, local_fmt, vl); + SDL_free(local_fmt); +} + SDL_bool scrcpy(const struct scrcpy_options *options) { SDL_bool send_frame_meta = !!options->record_filename; if (!server_start(&server, options->serial, options->port, @@ -195,7 +229,10 @@ SDL_bool scrcpy(const struct scrcpy_options *options) { struct recorder *rec = NULL; if (options->record_filename) { - if (!recorder_init(&recorder, options->record_filename, frame_size)) { + if (!recorder_init(&recorder, + options->record_filename, + options->record_format, + frame_size)) { ret = SDL_FALSE; server_stop(&server); goto finally_destroy_file_handler; @@ -203,6 +240,8 @@ SDL_bool scrcpy(const struct scrcpy_options *options) { rec = &recorder; } + av_log_set_callback(av_log_callback); + decoder_init(&decoder, &frames, device_socket, rec); // now we consumed the header values, the socket receives the video stream @@ -223,7 +262,7 @@ SDL_bool scrcpy(const struct scrcpy_options *options) { goto finally_destroy_controller; } - if (!screen_init_rendering(&screen, device_name, frame_size)) { + if (!screen_init_rendering(&screen, device_name, frame_size, options->always_on_top)) { ret = SDL_FALSE; goto finally_stop_and_join_controller; } diff --git a/app/src/scrcpy.h b/app/src/scrcpy.h index 89945e6c..aba1f336 100644 --- a/app/src/scrcpy.h +++ b/app/src/scrcpy.h @@ -2,16 +2,19 @@ #define SCRCPY_H #include +#include struct scrcpy_options { const char *serial; const char *crop; const char *record_filename; + enum recorder_format record_format; Uint16 port; Uint16 max_size; Uint32 bit_rate; SDL_bool show_touches; SDL_bool fullscreen; + SDL_bool always_on_top; }; SDL_bool scrcpy(const struct scrcpy_options *options); diff --git a/app/src/screen.c b/app/src/screen.c index 703def32..1632e27f 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -3,6 +3,7 @@ #include #include +#include "compat.h" #include "icon.xpm" #include "lock_util.h" #include "log.h" @@ -23,7 +24,7 @@ SDL_bool sdl_init_and_configure(void) { LOGW("Could not enable bilinear filtering"); } -#if SDL_VERSION_ATLEAST(2, 0, 5) +#ifdef SCRCPY_SDL_HAS_HINT_MOUSE_FOCUS_CLICKTHROUGH // Handle a click to gain focus as any other click if (!SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1")) { LOGW("Could not enable mouse focus clickthrough"); @@ -71,7 +72,7 @@ static void set_window_size(struct screen *screen, struct size new_size) { // get the preferred display bounds (i.e. the screen bounds with some margins) static SDL_bool get_preferred_display_bounds(struct size *bounds) { SDL_Rect rect; -#if SDL_VERSION_ATLEAST(2, 0, 5) +#ifdef SCRCPY_SDL_HAS_GET_DISPLAY_USABLE_BOUNDS # define GET_DISPLAY_BOUNDS(i, r) SDL_GetDisplayUsableBounds((i), (r)) #else # define GET_DISPLAY_BOUNDS(i, r) SDL_GetDisplayBounds((i), (r)) @@ -144,7 +145,10 @@ static inline SDL_Texture *create_texture(SDL_Renderer *renderer, struct size fr frame_size.width, frame_size.height); } -SDL_bool screen_init_rendering(struct screen *screen, const char *device_name, struct size frame_size) { +SDL_bool screen_init_rendering(struct screen *screen, + const char *device_name, + struct size frame_size, + SDL_bool always_on_top) { screen->frame_size = frame_size; struct size window_size = get_initial_optimal_size(frame_size); @@ -152,6 +156,15 @@ SDL_bool screen_init_rendering(struct screen *screen, const char *device_name, s #ifdef HIDPI_SUPPORT window_flags |= SDL_WINDOW_ALLOW_HIGHDPI; #endif + if (always_on_top) { +#ifdef SCRCPY_SDL_HAS_WINDOW_ALWAYS_ON_TOP + window_flags |= SDL_WINDOW_ALWAYS_ON_TOP; +#else + LOGW("The 'always on top' flag is not available " + "(compile with SDL >= 2.0.5 to enable it)"); +#endif + } + screen->window = SDL_CreateWindow(device_name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_size.width, window_size.height, window_flags); if (!screen->window) { diff --git a/app/src/screen.h b/app/src/screen.h index 13103eaa..05729a12 100644 --- a/app/src/screen.h +++ b/app/src/screen.h @@ -43,7 +43,8 @@ void screen_init(struct screen *screen); // initialize screen, create window, renderer and texture (window is hidden) SDL_bool screen_init_rendering(struct screen *screen, const char *device_name, - struct size frame_size); + struct size frame_size, + SDL_bool always_on_top); // show the window void screen_show_window(struct screen *screen); diff --git a/app/src/str_util.c b/app/src/str_util.c index f234346e..892883a6 100644 --- a/app/src/str_util.c +++ b/app/src/str_util.c @@ -3,6 +3,11 @@ #include #include +#ifdef _WIN32 +# include +# include +#endif + size_t xstrncpy(char *dest, const char *src, size_t n) { size_t i; for (i = 0; i < n - 1 && src[i] != '\0'; ++i) @@ -47,3 +52,22 @@ char *strquote(const char *src) { quoted[len + 2] = '\0'; return quoted; } + +#ifdef _WIN32 + +wchar_t *utf8_to_wide_char(const char *utf8) { + int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0); + if (!len) { + return NULL; + } + + wchar_t *wide = malloc(len * sizeof(wchar_t)); + if (!wide) { + return NULL; + } + + MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wide, len); + return wide; +} + +#endif diff --git a/app/src/str_util.h b/app/src/str_util.h index 9433f768..6f4004f1 100644 --- a/app/src/str_util.h +++ b/app/src/str_util.h @@ -20,4 +20,10 @@ size_t xstrjoin(char *dst, const char *const tokens[], char sep, size_t n); // returns the new allocated string, to be freed by the caller char *strquote(const char *src); +#ifdef _WIN32 +// convert a UTF-8 string to a wchar_t string +// returns the new allocated string, to be freed by the caller +wchar_t *utf8_to_wide_char(const char *utf8); +#endif + #endif diff --git a/app/src/sys/win/command.c b/app/src/sys/win/command.c index d0f189f7..5e9876cd 100644 --- a/app/src/sys/win/command.c +++ b/app/src/sys/win/command.c @@ -18,7 +18,7 @@ static int build_cmd(char *cmd, size_t len, const char *const argv[]) { } enum process_result cmd_execute(const char *path, const char *const argv[], HANDLE *handle) { - STARTUPINFO si; + STARTUPINFOW si; PROCESS_INFORMATION pi; memset(&si, 0, sizeof(si)); si.cb = sizeof(si); @@ -29,12 +29,19 @@ enum process_result cmd_execute(const char *path, const char *const argv[], HAND return PROCESS_ERROR_GENERIC; } + wchar_t *wide = utf8_to_wide_char(cmd); + if (!wide) { + LOGC("Cannot allocate wide char string"); + return PROCESS_ERROR_GENERIC; + } + #ifdef WINDOWS_NOCONSOLE int flags = CREATE_NO_WINDOW; #else int flags = 0; #endif - if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi)) { + if (!CreateProcessW(NULL, wide, NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi)) { + free(wide); *handle = NULL; if (GetLastError() == ERROR_FILE_NOT_FOUND) { return PROCESS_ERROR_MISSING_BINARY; @@ -42,6 +49,7 @@ enum process_result cmd_execute(const char *path, const char *const argv[], HAND return PROCESS_ERROR_GENERIC; } + free(wide); *handle = pi.hProcess; return PROCESS_SUCCESS; } diff --git a/meson.build b/meson.build index 236a146f..ee97b08a 100644 --- a/meson.build +++ b/meson.build @@ -1,5 +1,5 @@ project('scrcpy', 'c', - version: '1.6', + version: '1.7', meson_version: '>= 0.37', default_options: 'c_std=c11') diff --git a/server/build.gradle b/server/build.gradle index e0ae4ab4..0a807865 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -6,8 +6,8 @@ android { applicationId "com.genymobile.scrcpy" minSdkVersion 21 targetSdkVersion 27 - versionCode 7 - versionName "1.6" + versionCode 8 + versionName "1.7" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/server/src/main/java/com/genymobile/scrcpy/ControlEventReader.java b/server/src/main/java/com/genymobile/scrcpy/ControlEventReader.java index e47ca309..28e9503a 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ControlEventReader.java +++ b/server/src/main/java/com/genymobile/scrcpy/ControlEventReader.java @@ -9,8 +9,8 @@ import java.nio.charset.StandardCharsets; public class ControlEventReader { private static final int KEYCODE_PAYLOAD_LENGTH = 9; - private static final int MOUSE_PAYLOAD_LENGTH = 13; - private static final int SCROLL_PAYLOAD_LENGTH = 16; + private static final int MOUSE_PAYLOAD_LENGTH = 17; + private static final int SCROLL_PAYLOAD_LENGTH = 20; private static final int COMMAND_PAYLOAD_LENGTH = 1; public static final int TEXT_MAX_LENGTH = 300;