From b35733edb6df2a00b6af9b1c98627d344c377963 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 22 Jan 2019 09:43:17 +0100 Subject: [PATCH 01/16] Fix expected mouse event sizes Commit fefb9816a99e6f8fa59befcfb70ba87112f90a8d modified mouse events serialization. The server-side parsing was updated to correctly read the position, but the expected size of these events was not updated. As a result, the server might try to parse incomplete events, leading to BufferUnderflowException. Fixes . --- .../main/java/com/genymobile/scrcpy/ControlEventReader.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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; From eca82e09c3d3127cab247bfd94795da887a547cc Mon Sep 17 00:00:00 2001 From: Yu-Chen Lin Date: Sun, 27 Jan 2019 19:04:22 +0800 Subject: [PATCH 02/16] app: add always_on_top It is very convenient when I play mobile game and watch video at the same time. Tested on Linux mint Cinnamon as well as Windows 10. Signed-off-by: Yu-Chen Lin --- README.md | 10 ++++++++++ app/src/main.c | 34 ++++++++++++++++++++++------------ app/src/scrcpy.c | 2 +- app/src/scrcpy.h | 1 + app/src/screen.c | 9 ++++++++- app/src/screen.h | 3 ++- 6 files changed, 44 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index aca991b6..5c6960d0 100644 --- a/README.md +++ b/README.md @@ -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/main.c b/app/src/main.c index 0603fef5..67ae8b4d 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -19,6 +19,7 @@ struct args { Uint16 port; Uint16 max_size; Uint32 bit_rate; + SDL_bool always_on_top; }; static void usage(const char *arg0) { @@ -65,6 +66,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" @@ -206,20 +210,21 @@ static SDL_bool parse_port(char *optarg, Uint16 *port) { 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'}, + {"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:fhm:p:r:s:tTv", long_options, NULL)) != -1) { switch (c) { case 'b': if (!parse_bit_rate(optarg, &args->bit_rate)) { @@ -254,6 +259,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; @@ -288,6 +296,7 @@ int main(int argc, char *argv[]) { .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; @@ -324,6 +333,7 @@ int main(int argc, char *argv[]) { .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/scrcpy.c b/app/src/scrcpy.c index 0e9bcba0..0ae4ed6c 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -223,7 +223,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..1906467d 100644 --- a/app/src/scrcpy.h +++ b/app/src/scrcpy.h @@ -12,6 +12,7 @@ struct scrcpy_options { 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..42f3114a 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -144,7 +144,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 +155,10 @@ 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) { + window_flags |= SDL_WINDOW_ALWAYS_ON_TOP; + } + 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); From 1a5ba5950413136bd5b96052c7eb8313994dbc07 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 9 Feb 2019 12:26:07 +0100 Subject: [PATCH 03/16] Fix memory leak on close The buffer associated to the AVIOContext must be freed. --- app/src/decoder.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/decoder.c b/app/src/decoder.c index fd429147..6e074e7a 100644 --- a/app/src/decoder.c +++ b/app/src/decoder.c @@ -284,7 +284,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: From c11905b8606f257b9c1e421ef755c7240fcb036f Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 9 Feb 2019 12:05:39 +0100 Subject: [PATCH 04/16] Add log verbose macro This was the only log priority missing. --- app/src/log.h | 1 + 1 file changed, 1 insertion(+) 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__) From ee3cba57a82ab04767b3d0e2637e2397584bff62 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 9 Feb 2019 12:06:21 +0100 Subject: [PATCH 05/16] Forward FFmpeg logs FFmpeg logs can be useful to understand the cause of issues. --- app/src/scrcpy.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 0ae4ed6c..6c986653 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, @@ -203,6 +237,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 From c8f0805b899bbe90f25101697ac05088d70c49fb Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 9 Feb 2019 12:54:12 +0100 Subject: [PATCH 06/16] Write header file with correct extradata When recording, the header must be written with extradata set to the content of the very first packet. Suggested-by: Steve Lhomme Fixes Fixes --- app/src/recorder.c | 68 ++++++++++++++++++++++++++++++++++++---------- app/src/recorder.h | 1 + 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/app/src/recorder.c b/app/src/recorder.c index 2e846e91..2b9b210b 100644 --- a/app/src/recorder.c +++ b/app/src/recorder.c @@ -5,6 +5,16 @@ #include "config.h" #include "log.h" +// 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 LAVF_NEW_CODEC_API +#endif + static const AVOutputFormat *find_mp4_muxer(void) { #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 9, 100) void *opaque = NULL; @@ -30,6 +40,7 @@ SDL_bool recorder_init(struct recorder *recorder, const char *filename, } recorder->declared_frame_size = declared_frame_size; + recorder->header_written = SDL_FALSE; return SDL_TRUE; } @@ -63,13 +74,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 LAVF_NEW_CODEC_API ostream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; ostream->codecpar->codec_id = input_codec->id; ostream->codecpar->format = AV_PIX_FMT_YUV420P; @@ -93,14 +98,6 @@ 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; - } - return SDL_TRUE; } @@ -113,6 +110,47 @@ void recorder_close(struct recorder *recorder) { avformat_free_context(recorder->ctx); } +static SDL_bool +recorder_write_header(struct recorder *recorder, AVPacket *packet) { + AVStream *ostream = recorder->ctx->streams[0]; + + uint8_t *extradata = SDL_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 LAVF_NEW_CODEC_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; +} + 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; + } + return av_write_frame(recorder->ctx, packet) >= 0; } diff --git a/app/src/recorder.h b/app/src/recorder.h index 9cb17fe0..4959c7ab 100644 --- a/app/src/recorder.h +++ b/app/src/recorder.h @@ -10,6 +10,7 @@ struct recorder { char *filename; AVFormatContext *ctx; struct size declared_frame_size; + SDL_bool header_written; }; SDL_bool recorder_init(struct recorder *recoder, const char *filename, From 1aaad6ba35701f85d2500b0a23c573b2ee9ae62e Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 9 Feb 2019 15:08:36 +0100 Subject: [PATCH 07/16] Rescale packet timestamp to container time base Some containers force their own time base. For example, matroska overwrite time_base to (AVRational) {1, 1000}. Therefore, rescale our packet timestamps to the output stream time base. Suggested-by: Steve Lhomme --- app/src/recorder.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/src/recorder.c b/app/src/recorder.c index 2b9b210b..e3ed9d4f 100644 --- a/app/src/recorder.c +++ b/app/src/recorder.c @@ -15,6 +15,8 @@ # define LAVF_NEW_CODEC_API #endif +static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us + static const AVOutputFormat *find_mp4_muxer(void) { #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 9, 100) void *opaque = NULL; @@ -87,7 +89,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); @@ -143,6 +144,12 @@ recorder_write_header(struct recorder *recorder, AVPacket *packet) { 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); @@ -152,5 +159,6 @@ SDL_bool recorder_write(struct recorder *recorder, AVPacket *packet) { recorder->header_written = SDL_TRUE; } + recorder_rescale_packet(recorder, packet); return av_write_frame(recorder->ctx, packet) >= 0; } From 0ed23739529b3651c640a75a065d081ac9f4d588 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 9 Feb 2019 15:20:07 +0100 Subject: [PATCH 08/16] Support recording to MKV Implement recording to Matroska files. The format to use is determined by the option -F/--record-format if set, or by the file extension (".mp4" or ".mkv"). --- README.md | 2 +- app/src/main.c | 62 +++++++++++++++++++++++++++++++++++++++++++++- app/src/recorder.c | 29 ++++++++++++++++------ app/src/recorder.h | 11 +++++++- app/src/scrcpy.c | 5 +++- app/src/scrcpy.h | 2 ++ 6 files changed, 100 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5c6960d0..40569d71 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/src/main.c b/app/src/main.c index 67ae8b4d..412dd40a 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -7,11 +7,13 @@ #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; @@ -42,6 +44,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" @@ -57,6 +62,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" @@ -208,6 +215,36 @@ 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[] = { {"always-on-top", no_argument, NULL, 'T'}, @@ -218,13 +255,14 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) { {"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:tTv", 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)) { @@ -237,6 +275,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; @@ -276,6 +319,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; } @@ -290,6 +348,7 @@ 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, @@ -329,6 +388,7 @@ 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, diff --git a/app/src/recorder.c b/app/src/recorder.c index e3ed9d4f..24e02ff8 100644 --- a/app/src/recorder.c +++ b/app/src/recorder.c @@ -1,6 +1,7 @@ #include "recorder.h" #include +#include #include "config.h" #include "log.h" @@ -17,7 +18,7 @@ static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us -static const AVOutputFormat *find_mp4_muxer(void) { +static const AVOutputFormat *find_muxer(const char *name) { #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 9, 100) void *opaque = NULL; #endif @@ -29,11 +30,13 @@ static const AVOutputFormat *find_mp4_muxer(void) { 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) { @@ -41,6 +44,7 @@ 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; @@ -51,10 +55,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; } @@ -68,7 +83,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) { diff --git a/app/src/recorder.h b/app/src/recorder.h index 4959c7ab..203c51b4 100644 --- a/app/src/recorder.h +++ b/app/src/recorder.h @@ -6,15 +6,24 @@ #include "common.h" +enum recorder_format { + RECORDER_FORMAT_MP4 = 1, + RECORDER_FORMAT_MKV, +}; + 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, +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 6c986653..9a1c29d5 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -229,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; diff --git a/app/src/scrcpy.h b/app/src/scrcpy.h index 1906467d..aba1f336 100644 --- a/app/src/scrcpy.h +++ b/app/src/scrcpy.h @@ -2,11 +2,13 @@ #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; From b23cacfc1abd65f01022fda5b590059b3c4b29b7 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 10 Feb 2019 11:29:34 +0100 Subject: [PATCH 09/16] Add recording logs Log when recording is started and stopped. --- app/src/recorder.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/recorder.c b/app/src/recorder.c index 24e02ff8..ea5a84d8 100644 --- a/app/src/recorder.c +++ b/app/src/recorder.c @@ -114,6 +114,8 @@ SDL_bool recorder_open(struct recorder *recorder, AVCodec *input_codec) { return SDL_FALSE; } + LOGI("Recording started to %s file: %s", format_name, recorder->filename); + return SDL_TRUE; } @@ -124,6 +126,9 @@ 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 From 477c0a2cabbe59f9c9617a14c5d841be95dfe9c6 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 10 Feb 2019 12:53:03 +0100 Subject: [PATCH 10/16] Create process with wide chars on Windows Windows does not support UTF-8, so pushing a file with non-ASCII characters failed. Convert the UTF-8 command line to a wide characters string and call CreateProcessW(). Fixes --- app/src/str_util.c | 24 ++++++++++++++++++++++++ app/src/str_util.h | 6 ++++++ app/src/sys/win/command.c | 12 ++++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) 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; } From 6c40dbd27dbc5c3e9b6904eda4c9283f54374007 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 10 Feb 2019 13:16:55 +0100 Subject: [PATCH 11/16] Regroup Windows-ifdefs in command.h --- app/src/command.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) 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 { From f5f4e6b1c5f8a07fa53c27bf81f02b8bf0380e92 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 16 Feb 2019 00:50:14 +0100 Subject: [PATCH 12/16] Allocate extradata with av_malloc() The extradata buffer is owned by libav, so it must be allocated with av_malloc(), not SDL_malloc(). This fixes a crash on Windows during avformat_free_context(). --- app/src/recorder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/recorder.c b/app/src/recorder.c index ea5a84d8..d69953dc 100644 --- a/app/src/recorder.c +++ b/app/src/recorder.c @@ -135,7 +135,7 @@ static SDL_bool recorder_write_header(struct recorder *recorder, AVPacket *packet) { AVStream *ostream = recorder->ctx->streams[0]; - uint8_t *extradata = SDL_malloc(packet->size * sizeof(uint8_t)); + uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t)); if (!extradata) { LOGC("Cannot allocate extradata"); return SDL_FALSE; From b7472a545e82d178d677400b348dd92514b61d78 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 16 Feb 2019 00:53:19 +0100 Subject: [PATCH 13/16] Bump version to 1.7 --- meson.build | 2 +- server/build.gradle | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 { From 3fc11ee4650421c6b79c29362d35bc9e76565e5e Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 16 Feb 2019 01:06:28 +0100 Subject: [PATCH 14/16] Update links to v1.7 in README and BUILD --- BUILD.md | 6 +++--- README.md | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) 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 40569d71..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]. From 751600a7f99fb9910e96f0f29be427622c932441 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 16 Feb 2019 15:04:32 +0100 Subject: [PATCH 15/16] Move all compat ifdefs definitions to compat.h This allows to give a proper name to features requirements. --- app/src/compat.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ app/src/decoder.c | 3 ++- app/src/main.c | 3 ++- app/src/recorder.c | 19 +++++-------------- app/src/screen.c | 5 +++-- 5 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 app/src/compat.h diff --git a/app/src/compat.h b/app/src/compat.h new file mode 100644 index 00000000..4f709af6 --- /dev/null +++ b/app/src/compat.h @@ -0,0 +1,44 @@ +#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 +#endif + +#endif diff --git a/app/src/decoder.c b/app/src/decoder.c index 6e074e7a..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); diff --git a/app/src/main.c b/app/src/main.c index 412dd40a..35bb74ea 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -5,6 +5,7 @@ #include #include +#include "compat.h" #include "config.h" #include "log.h" #include "recorder.h" @@ -371,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 diff --git a/app/src/recorder.c b/app/src/recorder.c index d69953dc..2d47487f 100644 --- a/app/src/recorder.c +++ b/app/src/recorder.c @@ -3,28 +3,19 @@ #include #include +#include "compat.h" #include "config.h" #include "log.h" -// 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 LAVF_NEW_CODEC_API -#endif - static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us static const AVOutputFormat *find_muxer(const char *name) { -#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 9, 100) +#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); @@ -91,7 +82,7 @@ SDL_bool recorder_open(struct recorder *recorder, AVCodec *input_codec) { return SDL_FALSE; } -#ifdef LAVF_NEW_CODEC_API +#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; @@ -144,7 +135,7 @@ recorder_write_header(struct recorder *recorder, AVPacket *packet) { // copy the first packet to the extra data memcpy(extradata, packet->data, packet->size); -#ifdef LAVF_NEW_CODEC_API +#ifdef SCRCPY_LAVF_HAS_NEW_CODEC_PARAMS_API ostream->codecpar->extradata = extradata; ostream->codecpar->extradata_size = packet->size; #else diff --git a/app/src/screen.c b/app/src/screen.c index 42f3114a..4e9752c2 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)) From 1c1fe5ec53173fcccb6767a3c45b816610a8c72a Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 16 Feb 2019 15:21:14 +0100 Subject: [PATCH 16/16] Use "always on top" only for SDL >= 2.0.5 The flag SDL_WINDOW_ALWAYS_ON_TOP is available since SDL 2.0.5. Do not use it if SDL is older, to fix compilation failure. Fixes --- app/src/compat.h | 2 ++ app/src/screen.c | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/app/src/compat.h b/app/src/compat.h index 4f709af6..fd68d61a 100644 --- a/app/src/compat.h +++ b/app/src/compat.h @@ -39,6 +39,8 @@ # 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/screen.c b/app/src/screen.c index 4e9752c2..1632e27f 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -157,7 +157,12 @@ SDL_bool screen_init_rendering(struct screen *screen, 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,