Compare commits

..

3 Commits

Author SHA1 Message Date
870ced088e Name server parameters
The options values to configure the server were identified by their
command-line argument index.

Now that there are a lot of arguments, many of them being booleans, it
became unreadable and error-prone.

Identify the arguments by a key string instead.
2020-05-06 00:40:33 +02:00
a845ea0794 Allocate and format server command args
This paves the way to name all parameters.
2020-05-06 00:39:37 +02:00
a0d98fe4ae Add asprintf() compat function
Add sc_asprintf() similar to asprintf() which is not available
everywhere.
2020-05-06 00:18:52 +02:00
10 changed files with 240 additions and 177 deletions

View File

@ -210,6 +210,7 @@ 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,10 +7,6 @@
#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"
@ -49,18 +45,6 @@ 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) {
@ -72,14 +56,6 @@ 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;
} }
@ -136,7 +112,7 @@ event_watcher(void *data, SDL_Event *event) {
&& event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { && event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
// 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_render(&screen); screen_handle_window_event(&screen, &event->window);
} }
return 0; return 0;
} }

View File

@ -30,10 +30,10 @@ get_rotated_size(struct size size, int rotation) {
// get the window size in a struct size // get the window size in a struct size
static struct size static struct size
get_window_size(const struct screen *screen) { get_window_size(SDL_Window *window) {
int width; int width;
int height; int height;
SDL_GetWindowSize(screen->window, &width, &height); SDL_GetWindowSize(window, &width, &height);
struct size size; struct size size;
size.width = width; size.width = width;
@ -41,12 +41,31 @@ get_window_size(const struct screen *screen) {
return size; return size;
} }
// get the windowed window size
static struct size
get_windowed_window_size(const struct screen *screen) {
if (screen->fullscreen || screen->maximized) {
return screen->windowed_window_size;
}
return get_window_size(screen->window);
}
// apply the windowed window size if fullscreen and maximized are disabled
static void
apply_windowed_size(struct screen *screen) {
if (!screen->fullscreen && !screen->maximized) {
SDL_SetWindowSize(screen->window, screen->windowed_window_size.width,
screen->windowed_window_size.height);
}
}
// set the window size to be applied when fullscreen is disabled // set the window size to be applied when fullscreen is disabled
static void static void
set_window_size(struct screen *screen, struct size new_size) { set_window_size(struct screen *screen, struct size new_size) {
assert(!screen->fullscreen); // setting the window size during fullscreen is implementation defined,
assert(!screen->maximized); // so apply the resize only after fullscreen is disabled
SDL_SetWindowSize(screen->window, new_size.width, new_size.height); screen->windowed_window_size = new_size;
apply_windowed_size(screen);
} }
// get the preferred display bounds (i.e. the screen bounds with some margins) // get the preferred display bounds (i.e. the screen bounds with some margins)
@ -119,8 +138,8 @@ get_optimal_size(struct size current_size, struct size content_size) {
// same as get_optimal_size(), but read the current size from the window // same as get_optimal_size(), but read the current size from the window
static inline struct size static inline struct size
get_optimal_window_size(const struct screen *screen, struct size content_size) { get_optimal_window_size(const struct screen *screen, struct size content_size) {
struct size window_size = get_window_size(screen); struct size windowed_size = get_windowed_window_size(screen);
return get_optimal_size(window_size, content_size); return get_optimal_size(windowed_size, content_size);
} }
// initially, there is no current size, so use the frame size as current size // initially, there is no current size, so use the frame size as current size
@ -289,6 +308,8 @@ screen_init_rendering(struct screen *screen, const char *window_title,
return false; return false;
} }
screen->windowed_window_size = window_size;
return true; return true;
} }
@ -310,45 +331,6 @@ screen_destroy(struct screen *screen) {
} }
} }
static void
resize_for_content(struct screen *screen, struct size old_content_size,
struct size new_content_size) {
struct size window_size = get_window_size(screen);
struct size target_size = {
.width = (uint32_t) window_size.width * new_content_size.width
/ old_content_size.width,
.height = (uint32_t) window_size.height * new_content_size.height
/ old_content_size.height,
};
target_size = get_optimal_size(target_size, new_content_size);
set_window_size(screen, target_size);
}
static void
set_content_size(struct screen *screen, struct size new_content_size) {
if (!screen->fullscreen && !screen->maximized) {
resize_for_content(screen, screen->content_size, new_content_size);
} else if (!screen->resize_pending) {
// Store the windowed size to be able to compute the optimal size once
// fullscreen and maximized are disabled
screen->windowed_content_size = screen->content_size;
screen->resize_pending = true;
}
screen->content_size = new_content_size;
}
static void
apply_pending_resize(struct screen *screen) {
assert(!screen->fullscreen);
assert(!screen->maximized);
if (screen->resize_pending) {
resize_for_content(screen, screen->windowed_content_size,
screen->content_size);
screen->resize_pending = false;
}
}
void void
screen_set_rotation(struct screen *screen, unsigned rotation) { screen_set_rotation(struct screen *screen, unsigned rotation) {
assert(rotation < 4); assert(rotation < 4);
@ -356,6 +338,7 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
return; return;
} }
struct size old_content_size = screen->content_size;
struct size new_content_size = struct size new_content_size =
get_rotated_size(screen->frame_size, rotation); get_rotated_size(screen->frame_size, rotation);
@ -366,7 +349,17 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
return; return;
} }
set_content_size(screen, new_content_size); struct size windowed_size = get_windowed_window_size(screen);
struct size target_size = {
.width = (uint32_t) windowed_size.width * new_content_size.width
/ old_content_size.width,
.height = (uint32_t) windowed_size.height * new_content_size.height
/ old_content_size.height,
};
target_size = get_optimal_size(target_size, new_content_size);
set_window_size(screen, target_size);
screen->content_size = new_content_size;
screen->rotation = rotation; screen->rotation = rotation;
LOGI("Display rotation set to %u", rotation); LOGI("Display rotation set to %u", rotation);
@ -390,8 +383,19 @@ prepare_for_frame(struct screen *screen, struct size new_frame_size) {
// frame dimension changed, destroy texture // frame dimension changed, destroy texture
SDL_DestroyTexture(screen->texture); SDL_DestroyTexture(screen->texture);
set_content_size(screen, new_content_size); struct size content_size = screen->content_size;
struct size windowed_size = get_windowed_window_size(screen);
struct size target_size = {
(uint32_t) windowed_size.width * new_content_size.width
/ content_size.width,
(uint32_t) windowed_size.height * new_content_size.height
/ content_size.height,
};
target_size = get_optimal_size(target_size, new_content_size);
set_window_size(screen, target_size);
screen->frame_size = new_frame_size; screen->frame_size = new_frame_size;
screen->content_size = new_content_size;
LOGI("New texture: %" PRIu16 "x%" PRIu16, LOGI("New texture: %" PRIu16 "x%" PRIu16,
screen->frame_size.width, screen->frame_size.height); screen->frame_size.width, screen->frame_size.height);
@ -474,9 +478,7 @@ screen_switch_fullscreen(struct screen *screen) {
} }
screen->fullscreen = !screen->fullscreen; screen->fullscreen = !screen->fullscreen;
if (!screen->fullscreen && !screen->maximized) { apply_windowed_size(screen);
apply_pending_resize(screen);
}
LOGD("Switched to %s mode", screen->fullscreen ? "fullscreen" : "windowed"); LOGD("Switched to %s mode", screen->fullscreen ? "fullscreen" : "windowed");
screen_render(screen); screen_render(screen);
@ -517,26 +519,6 @@ screen_resize_to_pixel_perfect(struct screen *screen) {
content_size.height); content_size.height);
} }
static inline bool
is_fullscreen(const struct screen *screen) {
uint32_t flags = SDL_GetWindowFlags(screen->window);
return !!(flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP));
}
static void
update_fullscreen_state(struct screen *screen) {
// There is no SDL event to detect fullscreen changes, so store the
// state in a field and compare on every "size changed" event
bool fullscreen = is_fullscreen(screen);
if (fullscreen != screen->fullscreen) {
// Fullscreen state have changed
screen->fullscreen = fullscreen;
if (!fullscreen && !screen->maximized) {
apply_pending_resize(screen);
}
}
}
void void
screen_handle_window_event(struct screen *screen, screen_handle_window_event(struct screen *screen,
const SDL_WindowEvent *event) { const SDL_WindowEvent *event) {
@ -545,15 +527,36 @@ screen_handle_window_event(struct screen *screen,
screen_render(screen); screen_render(screen);
break; break;
case SDL_WINDOWEVENT_SIZE_CHANGED: case SDL_WINDOWEVENT_SIZE_CHANGED:
update_fullscreen_state(screen); if (!screen->fullscreen && !screen->maximized) {
// Backup the previous size: if we receive the MAXIMIZED event,
// then the new size must be ignored (it's the maximized size).
// We could not rely on the window flags due to race conditions
// (they could be updated asynchronously, at least on X11).
screen->windowed_window_size_backup =
screen->windowed_window_size;
// Save the windowed size, so that it is available once the
// window is maximized or fullscreen is enabled.
screen->windowed_window_size = get_window_size(screen->window);
}
screen_render(screen); screen_render(screen);
break; break;
case SDL_WINDOWEVENT_MAXIMIZED: case SDL_WINDOWEVENT_MAXIMIZED:
// The backup size must be non-nul.
assert(screen->windowed_window_size_backup.width);
assert(screen->windowed_window_size_backup.height);
// Revert the last size, it was updated while screen was maximized.
screen->windowed_window_size = screen->windowed_window_size_backup;
#ifdef DEBUG
// Reset the backup to invalid values to detect unexpected usage
screen->windowed_window_size_backup.width = 0;
screen->windowed_window_size_backup.height = 0;
#endif
screen->maximized = true; screen->maximized = true;
break; break;
case SDL_WINDOWEVENT_RESTORED: case SDL_WINDOWEVENT_RESTORED:
screen->maximized = false; screen->maximized = false;
apply_pending_resize(screen); apply_windowed_size(screen);
break; break;
} }
} }

View File

@ -21,12 +21,11 @@ struct screen {
struct sc_opengl gl; struct sc_opengl gl;
struct size frame_size; struct size frame_size;
struct size content_size; // rotated frame_size struct size content_size; // rotated frame_size
// The window size the last time it was not maximized or fullscreen.
bool resize_pending; // resize requested while fullscreen or maximized struct size windowed_window_size;
// The content size the last time the window was not maximized or // Since we receive the event SIZE_CHANGED before MAXIMIZED, we must be
// fullscreen (meaningful only when resize_pending is true) // able to revert the size to its non-maximized value.
struct size windowed_content_size; struct size windowed_window_size_backup;
// client rotation: 0, 1, 2 or 3 (x90 degrees counterclockwise) // client rotation: 0, 1, 2 or 3 (x90 degrees counterclockwise)
unsigned rotation; unsigned rotation;
bool has_frame; bool has_frame;
@ -50,8 +49,11 @@ struct screen {
.width = 0, \ .width = 0, \
.height = 0, \ .height = 0, \
}, \ }, \
.resize_pending = false, \ .windowed_window_size = { \
.windowed_content_size = { \ .width = 0, \
.height = 0, \
}, \
.windowed_window_size_backup = { \
.width = 0, \ .width = 0, \
.height = 0, \ .height = 0, \
}, \ }, \

View File

@ -231,22 +231,17 @@ 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) {
char max_size_string[6]; process_t result = PROCESS_NONE;
char bit_rate_string[11];
char max_fps_string[6]; char *cmd[128];
char lock_video_orientation_string[3]; int i = 0;
char display_id_string[6]; cmd[i++] = "shell";
sprintf(max_size_string, "%"PRIu16, params->max_size); cmd[i++] = "CLASSPATH=" DEVICE_SERVER_PATH;
sprintf(bit_rate_string, "%"PRIu32, params->bit_rate); cmd[i++] = "app_process";
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="
@ -254,23 +249,38 @@ 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
"com.genymobile.scrcpy.Server", cmd[i++] = "/"; // unused
SCRCPY_VERSION, cmd[i++] = "com.genymobile.scrcpy.Server";
max_size_string, cmd[i++] = SCRCPY_VERSION;
bit_rate_string,
max_fps_string, int dyn_index = i; // from there, the strings are allocated
lock_video_orientation_string, #define ADD_PARAM(fmt, ...) \
server->tunnel_forward ? "true" : "false", cmd[i] = sc_asprintf(fmt, ## __VA_ARGS__); \
params->crop ? params->crop : "-", if (!cmd[i++]) { \
"true", // always send frame meta (packet boundaries + timestamp) goto end; \
params->control ? "true" : "false", }
display_id_string,
params->show_touches ? "true" : "false", #define STRBOOL(p) (p ? "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 "...");
@ -282,7 +292,14 @@ execute_server(struct server *server, const struct server_params *params) {
// Port: 5005 // Port: 5005
// Then click on "Debug" // Then click on "Debug"
#endif #endif
return adb_execute(server->serial, cmd, sizeof(cmd) / sizeof(cmd[0])); result = adb_execute(server->serial, (const char **) cmd, i);
end:
for (int j = i; j > dyn_index; --j) {
free(cmd[j - 1]);
}
return result;
} }
static socket_t static socket_t

View File

@ -1,7 +1,9 @@
#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>
@ -195,3 +197,33 @@ 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,8 +1,10 @@
#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"
@ -57,4 +59,13 @@ 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

@ -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_TOUCHSCREEN, 0); InputDevice.SOURCE_MOUSE, 0);
return injectEvent(event); return injectEvent(event);
} }

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; private int lockedVideoOrientation = -1;
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

@ -109,52 +109,73 @@ 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();
int maxSize = Integer.parseInt(args[1]) & ~7; // multiple of 8 for (int i = 1; i < args.length; ++i) {
String arg = args[i];
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) {
case "max_size":
int maxSize = Integer.parseInt(value) & ~7; // multiple of 8
options.setMaxSize(maxSize); options.setMaxSize(maxSize);
break;
int bitRate = Integer.parseInt(args[2]); case "bit_rate":
int bitRate = Integer.parseInt(value);
options.setBitRate(bitRate); options.setBitRate(bitRate);
break;
int maxFps = Integer.parseInt(args[3]); case "max_fps":
int maxFps = Integer.parseInt(value);
options.setMaxFps(maxFps); options.setMaxFps(maxFps);
break;
int lockedVideoOrientation = Integer.parseInt(args[4]); case "lock_video_orientation":
int lockedVideoOrientation = Integer.parseInt(value);
options.setLockedVideoOrientation(lockedVideoOrientation); options.setLockedVideoOrientation(lockedVideoOrientation);
break;
case "tunnel_forward":
// use "adb forward" instead of "adb tunnel"? (so the server must listen) // use "adb forward" instead of "adb tunnel"? (so the server must listen)
boolean tunnelForward = Boolean.parseBoolean(args[5]); boolean tunnelForward = Boolean.parseBoolean(value);
options.setTunnelForward(tunnelForward); options.setTunnelForward(tunnelForward);
break;
Rect crop = parseCrop(args[6]); case "crop":
Rect crop = parseCrop(value);
options.setCrop(crop); options.setCrop(crop);
break;
boolean sendFrameMeta = Boolean.parseBoolean(args[7]); case "send_frame_meta":
boolean sendFrameMeta = Boolean.parseBoolean(value);
options.setSendFrameMeta(sendFrameMeta); options.setSendFrameMeta(sendFrameMeta);
break;
boolean control = Boolean.parseBoolean(args[8]); case "control":
boolean control = Boolean.parseBoolean(value);
options.setControl(control); options.setControl(control);
break;
int displayId = Integer.parseInt(args[9]); case "display_id":
int displayId = Integer.parseInt(value);
options.setDisplayId(displayId); options.setDisplayId(displayId);
break;
boolean showTouches = Boolean.parseBoolean(args[10]); case "show_touches":
boolean showTouches = Boolean.parseBoolean(value);
options.setShowTouches(showTouches); options.setShowTouches(showTouches);
break;
boolean stayAwake = Boolean.parseBoolean(args[11]); case "stay_awake":
boolean stayAwake = Boolean.parseBoolean(value);
options.setStayAwake(stayAwake); 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 ("-".equals(crop)) { if (crop.isEmpty()) {
return null; return null;
} }
// input format: "width:height:x:y" // input format: "width:height:x:y"