From b882322f7371b16acd53677c4a3adbaaed0aef77 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 8 Oct 2018 18:49:48 +0200 Subject: [PATCH 1/4] Work around Os.write() not updating position ByteBuffer position is not updated as expected by Os.write() on old Android versions. Count the remaining bytes manually. Fixes . --- server/src/main/java/com/genymobile/scrcpy/IO.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/IO.java b/server/src/main/java/com/genymobile/scrcpy/IO.java index bfd48be2..f1f05105 100644 --- a/server/src/main/java/com/genymobile/scrcpy/IO.java +++ b/server/src/main/java/com/genymobile/scrcpy/IO.java @@ -14,9 +14,18 @@ public class IO { } public static void writeFully(FileDescriptor fd, ByteBuffer from) throws IOException { - while (from.hasRemaining()) { + // ByteBuffer position is not updated as expected by Os.write() on old Android versions, so + // count the remaining bytes manually. + // See . + int remaining = from.remaining(); + while (remaining > 0) { try { - Os.write(fd, from); + int w = Os.write(fd, from); + if (BuildConfig.DEBUG && w < 0) { + // w should not be negative, since an exception is thrown on error + throw new AssertionError("Os.write() returned a negative value (" + w + ")"); + } + remaining -= w; } catch (ErrnoException e) { if (e.errno != OsConstants.EINTR) { throw new IOException(e); From c20245630ed946340be76b1e6c6e937beaa77bd5 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 14 Oct 2018 18:52:10 +0200 Subject: [PATCH 2/4] Factorize Windows command building Extract command line building to a separate method. --- app/src/sys/win/command.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/sys/win/command.c b/app/src/sys/win/command.c index 1a14d89a..d0f189f7 100644 --- a/app/src/sys/win/command.c +++ b/app/src/sys/win/command.c @@ -4,20 +4,27 @@ #include "log.h" #include "str_util.h" +static int build_cmd(char *cmd, size_t len, const char *const argv[]) { + // Windows command-line parsing is WTF: + // + // only make it work for this very specific program + // (don't handle escaping nor quotes) + size_t ret = xstrjoin(cmd, argv, ' ', len); + if (ret >= len) { + LOGE("Command too long (%" PRIsizet " chars)", len - 1); + return -1; + } + return 0; +} + enum process_result cmd_execute(const char *path, const char *const argv[], HANDLE *handle) { STARTUPINFO si; PROCESS_INFORMATION pi; memset(&si, 0, sizeof(si)); si.cb = sizeof(si); - // Windows command-line parsing is WTF: - // - // only make it work for this very specific program - // (don't handle escaping nor quotes) char cmd[256]; - size_t ret = xstrjoin(cmd, argv, ' ', sizeof(cmd)); - if (ret >= sizeof(cmd)) { - LOGE("Command too long (%" PRIsizet " chars)", sizeof(cmd) - 1); + if (build_cmd(cmd, sizeof(cmd), argv)) { *handle = NULL; return PROCESS_ERROR_GENERIC; } From 0b92b9335821cf4536565289efc1c943b5a6dd64 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 24 Oct 2018 18:51:02 +0200 Subject: [PATCH 3/4] Capture Alt and Meta keys Alt and Meta keys should not be forwarded to the device. For now, they are not used for shortcuts, but they could be. --- app/src/input_manager.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/src/input_manager.c b/app/src/input_manager.c index af84c8f3..075ac01a 100644 --- a/app/src/input_manager.c +++ b/app/src/input_manager.c @@ -151,6 +151,14 @@ void input_manager_process_text_input(struct input_manager *input_manager, void input_manager_process_key(struct input_manager *input_manager, const SDL_KeyboardEvent *event) { SDL_bool ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL); + SDL_bool alt = event->keysym.mod & (KMOD_LALT | KMOD_RALT); + SDL_bool meta = event->keysym.mod & (KMOD_LGUI | KMOD_RGUI); + + if (alt | meta) { + // no shortcut involves Alt or Meta, and they should not be forwarded + // to the device + return; + } // capture all Ctrl events if (ctrl) { From 96056e3213b9f142cc39672186290b7495c1e0dd Mon Sep 17 00:00:00 2001 From: yuchenlin Date: Sat, 27 Oct 2018 20:07:22 +0800 Subject: [PATCH 4/4] input_manager: fix potential memory leak on text Fix potential memory leak when controller_push_event failed. Signed-off-by: yuchenlin --- app/src/input_manager.c | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/input_manager.c b/app/src/input_manager.c index 075ac01a..f43eda89 100644 --- a/app/src/input_manager.c +++ b/app/src/input_manager.c @@ -144,6 +144,7 @@ void input_manager_process_text_input(struct input_manager *input_manager, return; } if (!controller_push_event(input_manager->controller, &control_event)) { + SDL_free(control_event.text_event.text); LOGW("Cannot send text event"); } }