Compare commits

..

5 Commits

Author SHA1 Message Date
Romain Vimont
fba7f77a7e wip-copypaste 2020-05-20 20:05:29 +02:00
Romain Vimont
a85848a541 Fix Windows Ctrl Handler declaration
The handler function signature must include the calling convention
declaration.

Ref: <https://stackoverflow.com/questions/61717439/why-calling-setconsolectrlhandler-triggers-a-warning>
2020-05-11 01:32:54 +02:00
Romain Vimont
28abd98f7f Properly handle Ctrl+C on Windows
By default, Ctrl+C just kills the process on Windows. This caused
corrupted video files on recording.

Handle Ctrl+C properly to clean up properly.

Fixes #818 <https://github.com/Genymobile/scrcpy/issues/818>
2020-05-08 14:54:33 +02:00
Romain Vimont
e2d5f0e7fc Send scroll events as a touchscreen
Scroll events were sent with a mouse input device. When scrolling on a
list, this could cause the whole list to be focused, and drawn with the
focus color as background.

Send scroll events with a touchscreen input device instead (like motion
events).

Fixes #1362 <https://github.com/Genymobile/scrcpy/issues/1362>
2020-05-07 15:08:11 +02:00
Romain Vimont
ead7ee4a03 Revert "Improve resizing workaround"
This reverts commit 92cb3a6661, which
broke the fullscreen/maximized restoration size on Windows.

Fixes #1346 <https://github.com/Genymobile/scrcpy/issues/1346>
2020-05-06 01:10:25 +02:00
12 changed files with 166 additions and 156 deletions

View File

@@ -210,7 +210,6 @@ To disable mirroring while recording:
scrcpy --no-display --record file.mp4 scrcpy --no-display --record file.mp4
scrcpy -Nr file.mkv scrcpy -Nr file.mkv
# interrupt recording with Ctrl+C # interrupt recording with Ctrl+C
# Ctrl+C does not terminate properly on Windows, so disconnect the device
``` ```
"Skipped frames" are recorded, even if they are not displayed in real time (for "Skipped frames" are recorded, even if they are not displayed in real time (for

View File

@@ -7,6 +7,10 @@
#include <sys/time.h> #include <sys/time.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#ifdef _WIN32
# include <windows.h>
#endif
#include "config.h" #include "config.h"
#include "command.h" #include "command.h"
#include "common.h" #include "common.h"
@@ -45,6 +49,18 @@ static struct input_manager input_manager = {
.prefer_text = false, // initialized later .prefer_text = false, // initialized later
}; };
#ifdef _WIN32
BOOL WINAPI windows_ctrl_handler(DWORD ctrl_type) {
if (ctrl_type == CTRL_C_EVENT) {
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
return TRUE;
}
return FALSE;
}
#endif // _WIN32
// init SDL and set appropriate hints // init SDL and set appropriate hints
static bool static bool
sdl_init_and_configure(bool display, const char *render_driver) { sdl_init_and_configure(bool display, const char *render_driver) {
@@ -56,6 +72,14 @@ sdl_init_and_configure(bool display, const char *render_driver) {
atexit(SDL_Quit); atexit(SDL_Quit);
#ifdef _WIN32
// Clean up properly on Ctrl+C on Windows
bool ok = SetConsoleCtrlHandler(windows_ctrl_handler, TRUE);
if (!ok) {
LOGW("Could not set Ctrl+C handler");
}
#endif // _WIN32
if (!display) { if (!display) {
return true; return true;
} }
@@ -109,10 +133,10 @@ static int
event_watcher(void *data, SDL_Event *event) { event_watcher(void *data, SDL_Event *event) {
(void) data; (void) data;
if (event->type == SDL_WINDOWEVENT if (event->type == SDL_WINDOWEVENT
&& event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { && event->window.event == SDL_WINDOWEVENT_RESIZED) {
// In practice, it seems to always be called from the same thread in // In practice, it seems to always be called from the same thread in
// that specific case. Anyway, it's just a workaround. // that specific case. Anyway, it's just a workaround.
screen_handle_window_event(&screen, &event->window); screen_render(&screen);
} }
return 0; return 0;
} }

View File

@@ -231,17 +231,22 @@ enable_tunnel_any_port(struct server *server, struct port_range port_range) {
static process_t static process_t
execute_server(struct server *server, const struct server_params *params) { execute_server(struct server *server, const struct server_params *params) {
process_t result = PROCESS_NONE; char max_size_string[6];
char bit_rate_string[11];
char *cmd[128]; char max_fps_string[6];
int i = 0; char lock_video_orientation_string[3];
cmd[i++] = "shell"; char display_id_string[6];
cmd[i++] = "CLASSPATH=" DEVICE_SERVER_PATH; sprintf(max_size_string, "%"PRIu16, params->max_size);
cmd[i++] = "app_process"; sprintf(bit_rate_string, "%"PRIu32, params->bit_rate);
sprintf(max_fps_string, "%"PRIu16, params->max_fps);
sprintf(lock_video_orientation_string, "%"PRIi8, params->lock_video_orientation);
sprintf(display_id_string, "%"PRIu16, params->display_id);
const char *const cmd[] = {
"shell",
"CLASSPATH=" DEVICE_SERVER_PATH,
"app_process",
#ifdef SERVER_DEBUGGER #ifdef SERVER_DEBUGGER
# define SERVER_DEBUGGER_PORT "5005" # define SERVER_DEBUGGER_PORT "5005"
cmd[i++] =
# ifdef SERVER_DEBUGGER_METHOD_NEW # ifdef SERVER_DEBUGGER_METHOD_NEW
/* Android 9 and above */ /* Android 9 and above */
"-XjdwpProvider:internal -XjdwpOptions:transport=dt_socket,suspend=y,server=y,address=" "-XjdwpProvider:internal -XjdwpOptions:transport=dt_socket,suspend=y,server=y,address="
@@ -249,38 +254,23 @@ execute_server(struct server *server, const struct server_params *params) {
/* Android 8 and below */ /* Android 8 and below */
"-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=" "-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address="
# endif # endif
SERVER_DEBUGGER_PORT; SERVER_DEBUGGER_PORT,
#endif #endif
"/", // unused
cmd[i++] = "/"; // unused "com.genymobile.scrcpy.Server",
cmd[i++] = "com.genymobile.scrcpy.Server"; SCRCPY_VERSION,
cmd[i++] = SCRCPY_VERSION; max_size_string,
bit_rate_string,
int dyn_index = i; // from there, the strings are allocated max_fps_string,
#define ADD_PARAM(fmt, ...) \ lock_video_orientation_string,
cmd[i] = sc_asprintf(fmt, ## __VA_ARGS__); \ server->tunnel_forward ? "true" : "false",
if (!cmd[i++]) { \ params->crop ? params->crop : "-",
goto end; \ "true", // always send frame meta (packet boundaries + timestamp)
} params->control ? "true" : "false",
display_id_string,
#define STRBOOL(p) (p ? "true" : "false") params->show_touches ? "true" : "false",
params->stay_awake ? "true" : "false",
ADD_PARAM("max_size=%"PRIu16, params->max_size); };
ADD_PARAM("bit_rate=%"PRIu32, params->bit_rate);
ADD_PARAM("max_fps=%"PRIu16, params->max_fps);
ADD_PARAM("lock_video_orientation=%"PRIi8, params->lock_video_orientation);
ADD_PARAM("tunnel_forward=%s", STRBOOL(server->tunnel_forward));
ADD_PARAM("crop=%s", params->crop ? params->crop : "");
// always send frame meta (packet boundaries + timestamp)
ADD_PARAM("send_frame_meta=true");
ADD_PARAM("control=%s", STRBOOL(params->control));
ADD_PARAM("display_id=%"PRIu16, params->display_id);
ADD_PARAM("show_touches=%s", STRBOOL(params->show_touches));
ADD_PARAM("stay_awake=%s", STRBOOL(params->stay_awake));
#undef ADD_PARAM
#undef STRBOOL
#ifdef SERVER_DEBUGGER #ifdef SERVER_DEBUGGER
LOGI("Server debugger waiting for a client on device port " LOGI("Server debugger waiting for a client on device port "
SERVER_DEBUGGER_PORT "..."); SERVER_DEBUGGER_PORT "...");
@@ -292,14 +282,7 @@ execute_server(struct server *server, const struct server_params *params) {
// Port: 5005 // Port: 5005
// Then click on "Debug" // Then click on "Debug"
#endif #endif
result = adb_execute(server->serial, (const char **) cmd, i); return adb_execute(server->serial, cmd, sizeof(cmd) / sizeof(cmd[0]));
end:
for (int j = i; j > dyn_index; --j) {
free(cmd[j - 1]);
}
return result;
} }
static socket_t static socket_t

View File

@@ -1,9 +1,7 @@
#include "str_util.h" #include "str_util.h"
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -197,33 +195,3 @@ utf8_from_wide_char(const wchar_t *ws) {
} }
#endif #endif
char *
sc_asprintf(const char *fmt, ...) {
va_list va;
va_start(va, fmt);
char *s = sc_vasprintf(fmt, va);
va_end(va);
return s;
}
char *
sc_vasprintf(const char *fmt, va_list ap) {
va_list va;
va_copy(va, ap);
int len = vsnprintf(NULL, 0, fmt, va);
va_end(va);
char *str = malloc(len + 1);
if (!str) {
return NULL;
}
va_copy(va, ap);
int len2 = vsprintf(str, fmt, va);
(void) len2;
assert(len == len2);
va_end(va);
return str;
}

View File

@@ -1,10 +1,8 @@
#ifndef STRUTIL_H #ifndef STRUTIL_H
#define STRUTIL_H #define STRUTIL_H
#include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h>
#include "config.h" #include "config.h"
@@ -59,13 +57,4 @@ char *
utf8_from_wide_char(const wchar_t *s); utf8_from_wide_char(const wchar_t *s);
#endif #endif
// compatibility function similar to asprintf()
// (but returning the resulting string for convenience)
char *
sc_asprintf(const char *fmt, ...)
__attribute__((format(printf, 1, 2)));
char *
sc_vasprintf(const char *fmt, va_list ap);
#endif #endif

View File

@@ -0,0 +1,24 @@
/**
* Copyright (c) 2008, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.content;
/**
* {@hide}
*/
oneway interface IOnPrimaryClipChangedListener {
void dispatchPrimaryClipChanged();
}

View File

@@ -215,7 +215,7 @@ public class Controller {
MotionEvent event = MotionEvent MotionEvent event = MotionEvent
.obtain(lastTouchDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, 0, 1f, 1f, DEVICE_ID_VIRTUAL, 0, .obtain(lastTouchDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, 0, 1f, 1f, DEVICE_ID_VIRTUAL, 0,
InputDevice.SOURCE_MOUSE, 0); InputDevice.SOURCE_TOUCHSCREEN, 0);
return injectEvent(event); return injectEvent(event);
} }

View File

@@ -6,6 +6,7 @@ import com.genymobile.scrcpy.wrappers.ServiceManager;
import com.genymobile.scrcpy.wrappers.SurfaceControl; import com.genymobile.scrcpy.wrappers.SurfaceControl;
import com.genymobile.scrcpy.wrappers.WindowManager; import com.genymobile.scrcpy.wrappers.WindowManager;
import android.content.IOnPrimaryClipChangedListener;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
@@ -66,6 +67,14 @@ public final class Device {
} }
}, displayId); }, displayId);
serviceManager.getClipboardManager().addPrimaryClipChangedListener(new IOnPrimaryClipChangedListener.Stub() {
@Override
public void dispatchPrimaryClipChanged() {
String s = getClipboardText();
Ln.i("clipboard changed = " + s);
}
});
if ((displayInfoFlags & DisplayInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS) == 0) { if ((displayInfoFlags & DisplayInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS) == 0) {
Ln.w("Display doesn't have FLAG_SUPPORTS_PROTECTED_BUFFERS flag, mirroring can be restricted"); Ln.w("Display doesn't have FLAG_SUPPORTS_PROTECTED_BUFFERS flag, mirroring can be restricted");
} }

View File

@@ -6,7 +6,7 @@ public class Options {
private int maxSize; private int maxSize;
private int bitRate; private int bitRate;
private int maxFps; private int maxFps;
private int lockedVideoOrientation = -1; private int lockedVideoOrientation;
private boolean tunnelForward; private boolean tunnelForward;
private Rect crop; private Rect crop;
private boolean sendFrameMeta; // send PTS so that the client may record properly private boolean sendFrameMeta; // send PTS so that the client may record properly

View File

@@ -171,7 +171,7 @@ public class ScreenEncoder implements Device.RotationListener {
} }
private static IBinder createDisplay() { private static IBinder createDisplay() {
return SurfaceControl.createDisplay("scrcpy", true); return SurfaceControl.createDisplay("scrcpy", false);
} }
private static void configure(MediaCodec codec, MediaFormat format) { private static void configure(MediaCodec codec, MediaFormat format) {

View File

@@ -109,73 +109,52 @@ public final class Server {
"The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")"); "The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")");
} }
final int expectedParameters = 12;
if (args.length != expectedParameters) {
throw new IllegalArgumentException("Expecting " + expectedParameters + " parameters");
}
Options options = new Options(); Options options = new Options();
for (int i = 1; i < args.length; ++i) { int maxSize = Integer.parseInt(args[1]) & ~7; // multiple of 8
String arg = args[i]; options.setMaxSize(maxSize);
int equalIndex = arg.indexOf('=');
if (equalIndex == -1) {
throw new IllegalArgumentException("Invalid key=value pair: \"" + arg + "\"");
}
String key = arg.substring(0, equalIndex);
String value = arg.substring(equalIndex + 1);
switch (key) { int bitRate = Integer.parseInt(args[2]);
case "max_size": options.setBitRate(bitRate);
int maxSize = Integer.parseInt(value) & ~7; // multiple of 8
options.setMaxSize(maxSize); int maxFps = Integer.parseInt(args[3]);
break; options.setMaxFps(maxFps);
case "bit_rate":
int bitRate = Integer.parseInt(value); int lockedVideoOrientation = Integer.parseInt(args[4]);
options.setBitRate(bitRate); options.setLockedVideoOrientation(lockedVideoOrientation);
break;
case "max_fps": // use "adb forward" instead of "adb tunnel"? (so the server must listen)
int maxFps = Integer.parseInt(value); boolean tunnelForward = Boolean.parseBoolean(args[5]);
options.setMaxFps(maxFps); options.setTunnelForward(tunnelForward);
break;
case "lock_video_orientation": Rect crop = parseCrop(args[6]);
int lockedVideoOrientation = Integer.parseInt(value); options.setCrop(crop);
options.setLockedVideoOrientation(lockedVideoOrientation);
break; boolean sendFrameMeta = Boolean.parseBoolean(args[7]);
case "tunnel_forward": options.setSendFrameMeta(sendFrameMeta);
// use "adb forward" instead of "adb tunnel"? (so the server must listen)
boolean tunnelForward = Boolean.parseBoolean(value); boolean control = Boolean.parseBoolean(args[8]);
options.setTunnelForward(tunnelForward); options.setControl(control);
break;
case "crop": int displayId = Integer.parseInt(args[9]);
Rect crop = parseCrop(value); options.setDisplayId(displayId);
options.setCrop(crop);
break; boolean showTouches = Boolean.parseBoolean(args[10]);
case "send_frame_meta": options.setShowTouches(showTouches);
boolean sendFrameMeta = Boolean.parseBoolean(value);
options.setSendFrameMeta(sendFrameMeta); boolean stayAwake = Boolean.parseBoolean(args[11]);
break; options.setStayAwake(stayAwake);
case "control":
boolean control = Boolean.parseBoolean(value);
options.setControl(control);
break;
case "display_id":
int displayId = Integer.parseInt(value);
options.setDisplayId(displayId);
break;
case "show_touches":
boolean showTouches = Boolean.parseBoolean(value);
options.setShowTouches(showTouches);
break;
case "stay_awake":
boolean stayAwake = Boolean.parseBoolean(value);
options.setStayAwake(stayAwake);
break;
default:
throw new IllegalArgumentException("Unknown parameter: " + key);
}
}
return options; return options;
} }
private static Rect parseCrop(String crop) { private static Rect parseCrop(String crop) {
if (crop.isEmpty()) { if ("-".equals(crop)) {
return null; return null;
} }
// input format: "width:height:x:y" // input format: "width:height:x:y"

View File

@@ -3,6 +3,7 @@ package com.genymobile.scrcpy.wrappers;
import com.genymobile.scrcpy.Ln; import com.genymobile.scrcpy.Ln;
import android.content.ClipData; import android.content.ClipData;
import android.content.IOnPrimaryClipChangedListener;
import android.os.Build; import android.os.Build;
import android.os.IInterface; import android.os.IInterface;
@@ -13,6 +14,7 @@ public class ClipboardManager {
private final IInterface manager; private final IInterface manager;
private Method getPrimaryClipMethod; private Method getPrimaryClipMethod;
private Method setPrimaryClipMethod; private Method setPrimaryClipMethod;
private Method addPrimaryClipChangedListener;
public ClipboardManager(IInterface manager) { public ClipboardManager(IInterface manager) {
this.manager = manager; this.manager = manager;
@@ -81,4 +83,37 @@ public class ClipboardManager {
return false; return false;
} }
} }
private static void addPrimaryClipChangedListener(Method method, IInterface manager, IOnPrimaryClipChangedListener listener)
throws InvocationTargetException, IllegalAccessException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
method.invoke(manager, listener, ServiceManager.PACKAGE_NAME);
} else {
method.invoke(manager, listener, ServiceManager.PACKAGE_NAME, ServiceManager.USER_ID);
}
}
private Method getAddPrimaryClipChangedListener() throws NoSuchMethodException {
if (addPrimaryClipChangedListener == null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
addPrimaryClipChangedListener = manager.getClass()
.getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class);
} else {
addPrimaryClipChangedListener = manager.getClass()
.getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class, int.class);
}
}
return addPrimaryClipChangedListener;
}
public boolean addPrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) {
try {
Method method = getAddPrimaryClipChangedListener();
addPrimaryClipChangedListener(method, manager, listener);
return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
Ln.e("Could not invoke method", e);
return false;
}
}
} }