Compare commits
18 Commits
server_thr
...
strbuf
Author | SHA1 | Date | |
---|---|---|---|
aad561bcdd | |||
1bd8e1caa3 | |||
a41b39b23b | |||
48fcfa96ab | |||
676fa73d2c | |||
58ea238fb2 | |||
13c4aa1a3b | |||
caf594c90e | |||
688477ff65 | |||
ac539e1312 | |||
a57c7d3a2b | |||
d2d18466d4 | |||
dae091e3ab | |||
4c4381de4c | |||
db484d82db | |||
0c9666b733 | |||
8bf28e9f53 | |||
06131ef634 |
@ -27,6 +27,7 @@ src = [
|
||||
'src/util/log.c',
|
||||
'src/util/net.c',
|
||||
'src/util/process.c',
|
||||
'src/util/strbuf.c',
|
||||
'src/util/str_util.c',
|
||||
'src/util/thread.c',
|
||||
'src/util/tick.c',
|
||||
@ -186,6 +187,7 @@ if get_option('buildtype') == 'debug'
|
||||
'tests/test_cli.c',
|
||||
'src/cli.c',
|
||||
'src/options.c',
|
||||
'src/util/strbuf.c',
|
||||
'src/util/str_util.c',
|
||||
]],
|
||||
['test_clock', [
|
||||
@ -195,6 +197,7 @@ if get_option('buildtype') == 'debug'
|
||||
['test_control_msg_serialize', [
|
||||
'tests/test_control_msg_serialize.c',
|
||||
'src/control_msg.c',
|
||||
'src/util/strbuf.c',
|
||||
'src/util/str_util.c',
|
||||
]],
|
||||
['test_device_msg_deserialize', [
|
||||
@ -204,8 +207,13 @@ if get_option('buildtype') == 'debug'
|
||||
['test_queue', [
|
||||
'tests/test_queue.c',
|
||||
]],
|
||||
['test_strbuf', [
|
||||
'tests/test_strbuf.c',
|
||||
'src/util/strbuf.c',
|
||||
]],
|
||||
['test_strutil', [
|
||||
'tests/test_strutil.c',
|
||||
'src/util/strbuf.c',
|
||||
'src/util/str_util.c',
|
||||
]],
|
||||
]
|
||||
|
236
app/src/cli.c
236
app/src/cli.c
@ -8,13 +8,243 @@
|
||||
|
||||
#include "options.h"
|
||||
#include "util/log.h"
|
||||
#include "util/strbuf.h"
|
||||
#include "util/str_util.h"
|
||||
|
||||
#define STR_IMPL_(x) #x
|
||||
#define STR(x) STR_IMPL_(x)
|
||||
|
||||
struct sc_option {
|
||||
char shortopt;
|
||||
const char *longopt;
|
||||
const char *argdesc;
|
||||
bool optional_arg;
|
||||
const char *text;
|
||||
};
|
||||
|
||||
static const struct sc_option options[] = {
|
||||
{
|
||||
.longopt = "always-on-top",
|
||||
.text = "Make scrcpy window always on top (above other windows).",
|
||||
},
|
||||
{
|
||||
.shortopt = 'b',
|
||||
.longopt = "bit-rate",
|
||||
.argdesc = "value",
|
||||
.text = "Encode the video at the gitven bit-rate, expressed in bits/s. "
|
||||
"Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n"
|
||||
"Default is " STR(DEFAULT_BIT_RATE) ".",
|
||||
},
|
||||
{
|
||||
.longopt = "codec-options",
|
||||
.argdesc = "key[:type]=value[,...]",
|
||||
.text = "Set a list of comma-separated key:type=value options for the "
|
||||
"device encoder.\n"
|
||||
"The possible values for 'type' are 'int' (default), 'long', "
|
||||
"'float' and 'string'.\n"
|
||||
"The list of possible codec options is available in the "
|
||||
"Android documentation: "
|
||||
"<https://d.android.com/reference/android/media/MediaFormat>",
|
||||
},
|
||||
{
|
||||
.longopt = "crop",
|
||||
.argdesc = "width:height:x:y",
|
||||
.text = "Crop the device screen on the server.\n"
|
||||
"The values are expressed in the device natural orientation "
|
||||
"(typically, portrait for a phone, landscape for a tablet). "
|
||||
"Any --max-size value is cmoputed on the cropped size.",
|
||||
},
|
||||
{
|
||||
.longopt = "disable-screensaver",
|
||||
.text = "Disable screensaver while scrcpy is running.",
|
||||
},
|
||||
{
|
||||
.longopt = "display",
|
||||
.argdesc = "id",
|
||||
.text = "Specify the display id to mirror.\n"
|
||||
"\n"
|
||||
"The list of possible display ids can be listed by:\n"
|
||||
" adb shell dumpsys display\n"
|
||||
"(search \"mDisplayId=\" in the output)\n"
|
||||
"\n"
|
||||
"Default is 0.",
|
||||
},
|
||||
{
|
||||
.longopt = "display-buffer",
|
||||
.argdesc = "ms",
|
||||
.text = "Add a buffering delay (in milliseconds) before displaying. "
|
||||
"This increases latency to compensate for jitter.\n"
|
||||
"Default is 0 (no buffering).",
|
||||
},
|
||||
{
|
||||
.longopt = "encoder",
|
||||
.argdesc = "name",
|
||||
.text = "Use a specific MediaCodec encoder (must be a H.264 encoder).",
|
||||
},
|
||||
{
|
||||
.longopt = "force-adb-forward",
|
||||
.text = "Do not attempt to use \"adb reverse\" to connect to the "
|
||||
"device.",
|
||||
},
|
||||
{
|
||||
.longopt = "forward-all-clicks",
|
||||
.text = "By default, right-click triggers BACK (or POWER on) and "
|
||||
"middle-click triggers HOME. This option disables these "
|
||||
"shortcuts and forwards the clicks to the device instead.",
|
||||
},
|
||||
{
|
||||
.shortopt = 'f',
|
||||
.longopt = "fullscreen",
|
||||
.text = "Start in fullscreen.",
|
||||
},
|
||||
{
|
||||
.shortopt = 'K',
|
||||
.longopt = "hid-keyboard",
|
||||
.text = "Simulate a physical keyboard by using HID over AOAv2.\n"
|
||||
"It provides a better experience for IME users, and allows to "
|
||||
"generate non-ASCII characters, contrary to the default "
|
||||
"injection method.\n"
|
||||
"It may only work over USB, and is currently only supported "
|
||||
"on Linux.",
|
||||
},
|
||||
{
|
||||
.shortopt = 'h',
|
||||
.longopt = "help",
|
||||
.text = "Print this help.",
|
||||
},
|
||||
{
|
||||
.longopt = "legacy-paste",
|
||||
.text = "Inject computer clipboard text as a sequence of key events "
|
||||
"on Ctrl+v (like MOD+Shift+v).\n"
|
||||
"This is a workaround for some devices not behaving as "
|
||||
"expected when setting the device clipboard programmatically.",
|
||||
},
|
||||
{
|
||||
.longopt = "lock-video-orientation",
|
||||
.argdesc = "value",
|
||||
.optional_arg = true,
|
||||
.text = "Lock video orientation to value.\n"
|
||||
"Possible values are \"unlocked\", \"initial\" (locked to the "
|
||||
"initial orientation), 0, 1, 2 and 3. Natural device "
|
||||
"orientation is 0, and each increment adds a 90 degrees "
|
||||
"rotation counterclockwise.\n"
|
||||
"Default is \"unlocked\".\n"
|
||||
"Passing the option without argument is equivalent to passing "
|
||||
"\"initial\".",
|
||||
},
|
||||
{
|
||||
.longopt = "max-fps",
|
||||
.argdesc = "value",
|
||||
.text = "Limit the frame rate of screen capture (officially supported "
|
||||
"since Android 10, but may work on earlier versions).",
|
||||
},
|
||||
{
|
||||
.shortopt = 'm',
|
||||
.longopt = "max-size",
|
||||
.argdesc = "value",
|
||||
.text = "Limit both the width and height of the video to value. The "
|
||||
"other dimension is computed so that the device aspect-ratio "
|
||||
"is preserved.\n"
|
||||
"Default is 0 (unlimited).",
|
||||
},
|
||||
{
|
||||
.shortopt = 'n',
|
||||
.longopt = "no-control",
|
||||
.text = "Disable device control (mirror the device in read-only).",
|
||||
},
|
||||
{
|
||||
.shortopt = 'N',
|
||||
.longopt = "no-display",
|
||||
.text = "Do not display device (only when screen recording).",
|
||||
},
|
||||
};
|
||||
|
||||
static void
|
||||
print_option_usage_header(const struct sc_option *opt) {
|
||||
struct sc_strbuf buf;
|
||||
if (!sc_strbuf_init(&buf, 128)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
(void) ok; // only used for assertions
|
||||
|
||||
if (opt->shortopt) {
|
||||
ok = sc_strbuf_append_char(&buf, '-');
|
||||
assert(ok);
|
||||
|
||||
|
||||
ok = sc_strbuf_append_char(&buf, opt->shortopt);
|
||||
assert(ok);
|
||||
|
||||
if (opt->longopt) {
|
||||
ok = sc_strbuf_append_staticstr(&buf, ", ");
|
||||
assert(ok);
|
||||
}
|
||||
}
|
||||
|
||||
if (opt->longopt) {
|
||||
ok = sc_strbuf_append_staticstr(&buf, "--");
|
||||
assert(ok);
|
||||
|
||||
if (!sc_strbuf_append_str(&buf, opt->longopt)) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
if (opt->argdesc) {
|
||||
if (opt->optional_arg && !sc_strbuf_append_char(&buf, '[')) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!sc_strbuf_append_char(&buf, '=')) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!sc_strbuf_append_str(&buf, opt->argdesc)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (opt->optional_arg && !sc_strbuf_append_char(&buf, ']')) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, " %s\n", buf.s);
|
||||
free(buf.s);
|
||||
return;
|
||||
|
||||
error:
|
||||
fprintf(stderr, "<ERROR>\n");
|
||||
}
|
||||
|
||||
static void
|
||||
print_option_usage(const struct sc_option *opt, unsigned cols) {
|
||||
assert(cols > 20); // We need some space
|
||||
assert(opt->text);
|
||||
|
||||
print_option_usage_header(opt);
|
||||
|
||||
char *text = wrap_lines(opt->text, cols, 8);
|
||||
if (!text) {
|
||||
fprintf(stderr, "<ERROR>\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s\n", text);
|
||||
free(text);
|
||||
}
|
||||
|
||||
void
|
||||
scrcpy_print_usage(const char *arg0) {
|
||||
fprintf(stderr, "Usage: %s [options]\n"
|
||||
"\n"
|
||||
"Options:\n", arg0);
|
||||
for (size_t i = 0; i < ARRAY_LEN(options); ++i) {
|
||||
fprintf(stderr, "\n");
|
||||
print_option_usage(&options[i], 80);
|
||||
}
|
||||
if (false)
|
||||
fprintf(stderr,
|
||||
"Usage: %s [options]\n"
|
||||
"\n"
|
||||
@ -66,12 +296,12 @@ scrcpy_print_usage(const char *arg0) {
|
||||
"\n"
|
||||
" --force-adb-forward\n"
|
||||
" Do not attempt to use \"adb reverse\" to connect to the\n"
|
||||
" the device.\n"
|
||||
" device.\n"
|
||||
"\n"
|
||||
" --forward-all-clicks\n"
|
||||
" By default, right-click triggers BACK (or POWER on) and\n"
|
||||
" middle-click triggers HOME. This option disables these\n"
|
||||
" shortcuts and forward the clicks to the device instead.\n"
|
||||
" shortcuts and forwards the clicks to the device instead.\n"
|
||||
"\n"
|
||||
" -f, --fullscreen\n"
|
||||
" Start in fullscreen.\n"
|
||||
@ -118,7 +348,7 @@ scrcpy_print_usage(const char *arg0) {
|
||||
"\n"
|
||||
" -N, --no-display\n"
|
||||
" Do not display device (only when screen recording is\n"
|
||||
" enabled).\n"
|
||||
" enabled).\n" // or V4L2 sink
|
||||
"\n"
|
||||
" --no-key-repeat\n"
|
||||
" Do not forward repeated key events when a key is held down.\n"
|
||||
|
@ -26,7 +26,7 @@ struct sc_clock_point {
|
||||
* array.
|
||||
*
|
||||
* To estimate the slope, it splits the last SC_CLOCK_RANGE points into two
|
||||
* sets of SC_CLOCK_RANGE/2 points, and compute their centroid ("average
|
||||
* sets of SC_CLOCK_RANGE/2 points, and computes their centroid ("average
|
||||
* point"). The slope of the estimated affine function is that of the line
|
||||
* passing through these two points.
|
||||
*
|
||||
|
@ -43,6 +43,11 @@
|
||||
# define SCRCPY_SDL_HAS_WINDOW_ALWAYS_ON_TOP
|
||||
#endif
|
||||
|
||||
#if SDL_VERSION_ATLEAST(2, 0, 6)
|
||||
// <https://github.com/libsdl-org/SDL/commit/d7a318de563125e5bb465b1000d6bc9576fbc6fc>
|
||||
# define SCRCPY_SDL_HAS_HINT_TOUCH_MOUSE_EVENTS
|
||||
#endif
|
||||
|
||||
#if SDL_VERSION_ATLEAST(2, 0, 8)
|
||||
// <https://hg.libsdl.org/SDL/rev/dfde5d3f9781>
|
||||
# define SCRCPY_SDL_HAS_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
|
||||
|
@ -56,7 +56,7 @@ static const char *const screen_power_mode_labels[] = {
|
||||
};
|
||||
|
||||
static void
|
||||
write_position(uint8_t *buf, const struct position *position) {
|
||||
write_position(uint8_t *buf, const struct sc_position *position) {
|
||||
buffer_write32be(&buf[0], position->point.x);
|
||||
buffer_write32be(&buf[4], position->point.y);
|
||||
buffer_write16be(&buf[8], position->screen_size.width);
|
||||
|
@ -57,11 +57,11 @@ struct control_msg {
|
||||
enum android_motionevent_action action;
|
||||
enum android_motionevent_buttons buttons;
|
||||
uint64_t pointer_id;
|
||||
struct position position;
|
||||
struct sc_position position;
|
||||
float pressure;
|
||||
} inject_touch_event;
|
||||
struct {
|
||||
struct position position;
|
||||
struct sc_position position;
|
||||
int32_t hscroll;
|
||||
int32_t vscroll;
|
||||
} inject_scroll_event;
|
||||
|
@ -3,22 +3,22 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct size {
|
||||
struct sc_size {
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
};
|
||||
|
||||
struct point {
|
||||
struct sc_point {
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
};
|
||||
|
||||
struct position {
|
||||
struct sc_position {
|
||||
// The video screen size may be different from the real device screen size,
|
||||
// so store to which size the absolute position apply, to scale it
|
||||
// accordingly.
|
||||
struct size screen_size;
|
||||
struct point point;
|
||||
struct sc_size screen_size;
|
||||
struct sc_point point;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -332,7 +332,7 @@ input_manager_process_text_input(struct input_manager *im,
|
||||
static bool
|
||||
simulate_virtual_finger(struct input_manager *im,
|
||||
enum android_motionevent_action action,
|
||||
struct point point) {
|
||||
struct sc_point point) {
|
||||
bool up = action == AMOTION_EVENT_ACTION_UP;
|
||||
|
||||
struct control_msg msg;
|
||||
@ -352,8 +352,8 @@ simulate_virtual_finger(struct input_manager *im,
|
||||
return true;
|
||||
}
|
||||
|
||||
static struct point
|
||||
inverse_point(struct point point, struct size size) {
|
||||
static struct sc_point
|
||||
inverse_point(struct sc_point point, struct sc_size size) {
|
||||
point.x = size.width - point.x;
|
||||
point.y = size.height - point.y;
|
||||
return point;
|
||||
@ -545,10 +545,10 @@ input_manager_process_mouse_motion(struct input_manager *im,
|
||||
im->mp->ops->process_mouse_motion(im->mp, event);
|
||||
|
||||
if (im->vfinger_down) {
|
||||
struct point mouse =
|
||||
struct sc_point mouse =
|
||||
screen_convert_window_to_frame_coords(im->screen, event->x,
|
||||
event->y);
|
||||
struct point vfinger = inverse_point(mouse, im->screen->frame_size);
|
||||
struct sc_point vfinger = inverse_point(mouse, im->screen->frame_size);
|
||||
simulate_virtual_finger(im, AMOTION_EVENT_ACTION_MOVE, vfinger);
|
||||
}
|
||||
}
|
||||
@ -630,10 +630,10 @@ input_manager_process_mouse_button(struct input_manager *im,
|
||||
#define CTRL_PRESSED (SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL))
|
||||
if ((down && !im->vfinger_down && CTRL_PRESSED)
|
||||
|| (!down && im->vfinger_down)) {
|
||||
struct point mouse =
|
||||
struct sc_point mouse =
|
||||
screen_convert_window_to_frame_coords(im->screen, event->x,
|
||||
event->y);
|
||||
struct point vfinger = inverse_point(mouse, im->screen->frame_size);
|
||||
struct sc_point vfinger = inverse_point(mouse, im->screen->frame_size);
|
||||
enum android_motionevent_action action = down
|
||||
? AMOTION_EVENT_ACTION_DOWN
|
||||
: AMOTION_EVENT_ACTION_UP;
|
||||
|
@ -125,7 +125,7 @@ convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct screen *screen,
|
||||
int mouse_y;
|
||||
SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||
|
||||
struct position position = {
|
||||
struct sc_position position = {
|
||||
.screen_size = screen->frame_size,
|
||||
.point = screen_convert_window_to_frame_coords(screen,
|
||||
mouse_x, mouse_y),
|
||||
|
@ -372,7 +372,7 @@ bool
|
||||
recorder_init(struct recorder *recorder,
|
||||
const char *filename,
|
||||
enum sc_record_format format,
|
||||
struct size declared_frame_size) {
|
||||
struct sc_size declared_frame_size) {
|
||||
recorder->filename = strdup(filename);
|
||||
if (!recorder->filename) {
|
||||
LOGE("Could not strdup filename");
|
||||
|
@ -25,7 +25,7 @@ struct recorder {
|
||||
char *filename;
|
||||
enum sc_record_format format;
|
||||
AVFormatContext *ctx;
|
||||
struct size declared_frame_size;
|
||||
struct sc_size declared_frame_size;
|
||||
bool header_written;
|
||||
|
||||
sc_thread thread;
|
||||
@ -44,7 +44,7 @@ struct recorder {
|
||||
|
||||
bool
|
||||
recorder_init(struct recorder *recorder, const char *filename,
|
||||
enum sc_record_format format, struct size declared_frame_size);
|
||||
enum sc_record_format format, struct sc_size declared_frame_size);
|
||||
|
||||
void
|
||||
recorder_destroy(struct recorder *recorder);
|
||||
|
108
app/src/scrcpy.c
108
app/src/scrcpy.c
@ -57,41 +57,30 @@ struct scrcpy {
|
||||
struct input_manager input_manager;
|
||||
};
|
||||
|
||||
static inline void
|
||||
push_event(uint32_t type, const char *name) {
|
||||
SDL_Event event;
|
||||
event.type = type;
|
||||
int ret = SDL_PushEvent(&event);
|
||||
if (ret < 0) {
|
||||
LOGE("Could not post %s event: %s", name, SDL_GetError());
|
||||
// What could we do?
|
||||
}
|
||||
}
|
||||
#define PUSH_EVENT(TYPE) push_event(TYPE, # TYPE)
|
||||
|
||||
#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);
|
||||
PUSH_EVENT(SDL_QUIT);
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
||||
// init SDL and set appropriate hints
|
||||
static bool
|
||||
sdl_init_and_configure(bool display, const char *render_driver,
|
||||
bool disable_screensaver) {
|
||||
uint32_t flags = display ? SDL_INIT_VIDEO : SDL_INIT_EVENTS;
|
||||
if (SDL_Init(flags)) {
|
||||
LOGC("Could not initialize SDL: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
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) {
|
||||
return true;
|
||||
}
|
||||
static void
|
||||
sdl_set_hints(const char *render_driver) {
|
||||
|
||||
if (render_driver && !SDL_SetHint(SDL_HINT_RENDER_DRIVER, render_driver)) {
|
||||
LOGW("Could not set render driver");
|
||||
@ -109,6 +98,15 @@ sdl_init_and_configure(bool display, const char *render_driver,
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SCRCPY_SDL_HAS_HINT_TOUCH_MOUSE_EVENTS
|
||||
// Disable synthetic mouse events from touch events
|
||||
// Touch events with id SDL_TOUCH_MOUSEID are ignored anyway, but it is
|
||||
// better not to generate them in the first place.
|
||||
if (!SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0")) {
|
||||
LOGW("Could not disable synthetic mouse events");
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SCRCPY_SDL_HAS_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
|
||||
// Disable compositor bypassing on X11
|
||||
if (!SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0")) {
|
||||
@ -120,6 +118,21 @@ sdl_init_and_configure(bool display, const char *render_driver,
|
||||
if (!SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0")) {
|
||||
LOGW("Could not disable minimize on focus loss");
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sdl_configure(bool display, bool disable_screensaver) {
|
||||
#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) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (disable_screensaver) {
|
||||
LOGD("Screensaver disabled");
|
||||
@ -128,8 +141,6 @@ sdl_init_and_configure(bool display, const char *render_driver,
|
||||
LOGD("Screensaver enabled");
|
||||
SDL_EnableScreenSaver();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
@ -248,9 +259,7 @@ stream_on_eos(struct stream *stream, void *userdata) {
|
||||
(void) stream;
|
||||
(void) userdata;
|
||||
|
||||
SDL_Event stop_event;
|
||||
stop_event.type = EVENT_STREAM_STOPPED;
|
||||
SDL_PushEvent(&stop_event);
|
||||
PUSH_EVENT(EVENT_STREAM_STOPPED);
|
||||
}
|
||||
|
||||
bool
|
||||
@ -258,6 +267,14 @@ scrcpy(struct scrcpy_options *options) {
|
||||
static struct scrcpy scrcpy;
|
||||
struct scrcpy *s = &scrcpy;
|
||||
|
||||
// Minimal SDL initialization
|
||||
if (SDL_Init(SDL_INIT_EVENTS)) {
|
||||
LOGC("Could not initialize SDL: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
atexit(SDL_Quit);
|
||||
|
||||
if (!server_init(&s->server)) {
|
||||
return false;
|
||||
}
|
||||
@ -278,7 +295,6 @@ scrcpy(struct scrcpy_options *options) {
|
||||
bool controller_started = false;
|
||||
bool screen_initialized = false;
|
||||
|
||||
bool record = !!options->record_filename;
|
||||
struct server_params params = {
|
||||
.serial = options->serial,
|
||||
.log_level = options->log_level,
|
||||
@ -303,15 +319,21 @@ scrcpy(struct scrcpy_options *options) {
|
||||
|
||||
server_started = true;
|
||||
|
||||
if (!sdl_init_and_configure(options->display, options->render_driver,
|
||||
options->disable_screensaver)) {
|
||||
if (options->display) {
|
||||
sdl_set_hints(options->render_driver);
|
||||
}
|
||||
|
||||
// Initialize SDL video in addition if display is enabled
|
||||
if (options->display && SDL_Init(SDL_INIT_VIDEO)) {
|
||||
LOGC("Could not initialize SDL: %s", SDL_GetError());
|
||||
goto end;
|
||||
}
|
||||
|
||||
char device_name[DEVICE_NAME_FIELD_LENGTH];
|
||||
struct size frame_size;
|
||||
sdl_configure(options->display, options->disable_screensaver);
|
||||
|
||||
if (!server_connect_to(&s->server, device_name, &frame_size)) {
|
||||
struct server_info info;
|
||||
|
||||
if (!server_connect_to(&s->server, &info)) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
@ -334,11 +356,11 @@ scrcpy(struct scrcpy_options *options) {
|
||||
}
|
||||
|
||||
struct recorder *rec = NULL;
|
||||
if (record) {
|
||||
if (options->record_filename) {
|
||||
if (!recorder_init(&s->recorder,
|
||||
options->record_filename,
|
||||
options->record_format,
|
||||
frame_size)) {
|
||||
info.frame_size)) {
|
||||
goto end;
|
||||
}
|
||||
rec = &s->recorder;
|
||||
@ -384,11 +406,11 @@ scrcpy(struct scrcpy_options *options) {
|
||||
|
||||
if (options->display) {
|
||||
const char *window_title =
|
||||
options->window_title ? options->window_title : device_name;
|
||||
options->window_title ? options->window_title : info.device_name;
|
||||
|
||||
struct screen_params screen_params = {
|
||||
.window_title = window_title,
|
||||
.frame_size = frame_size,
|
||||
.frame_size = info.frame_size,
|
||||
.always_on_top = options->always_on_top,
|
||||
.window_x = options->window_x,
|
||||
.window_y = options->window_y,
|
||||
@ -411,8 +433,8 @@ scrcpy(struct scrcpy_options *options) {
|
||||
|
||||
#ifdef HAVE_V4L2
|
||||
if (options->v4l2_device) {
|
||||
if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device, frame_size,
|
||||
options->v4l2_buffer)) {
|
||||
if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device,
|
||||
info.frame_size, options->v4l2_buffer)) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
103
app/src/screen.c
103
app/src/screen.c
@ -14,9 +14,9 @@
|
||||
|
||||
#define DOWNCAST(SINK) container_of(SINK, struct screen, frame_sink)
|
||||
|
||||
static inline struct size
|
||||
get_rotated_size(struct size size, int rotation) {
|
||||
struct size rotated_size;
|
||||
static inline struct sc_size
|
||||
get_rotated_size(struct sc_size size, int rotation) {
|
||||
struct sc_size rotated_size;
|
||||
if (rotation & 1) {
|
||||
rotated_size.width = size.height;
|
||||
rotated_size.height = size.width;
|
||||
@ -27,26 +27,26 @@ get_rotated_size(struct size size, int rotation) {
|
||||
return rotated_size;
|
||||
}
|
||||
|
||||
// get the window size in a struct size
|
||||
static struct size
|
||||
// get the window size in a struct sc_size
|
||||
static struct sc_size
|
||||
get_window_size(const struct screen *screen) {
|
||||
int width;
|
||||
int height;
|
||||
SDL_GetWindowSize(screen->window, &width, &height);
|
||||
|
||||
struct size size;
|
||||
struct sc_size size;
|
||||
size.width = width;
|
||||
size.height = height;
|
||||
return size;
|
||||
}
|
||||
|
||||
static struct point
|
||||
static struct sc_point
|
||||
get_window_position(const struct screen *screen) {
|
||||
int x;
|
||||
int y;
|
||||
SDL_GetWindowPosition(screen->window, &x, &y);
|
||||
|
||||
struct point point;
|
||||
struct sc_point point;
|
||||
point.x = x;
|
||||
point.y = y;
|
||||
return point;
|
||||
@ -54,7 +54,7 @@ get_window_position(const struct screen *screen) {
|
||||
|
||||
// set the window size to be applied when fullscreen is disabled
|
||||
static void
|
||||
set_window_size(struct screen *screen, struct size new_size) {
|
||||
set_window_size(struct screen *screen, struct sc_size new_size) {
|
||||
assert(!screen->fullscreen);
|
||||
assert(!screen->maximized);
|
||||
SDL_SetWindowSize(screen->window, new_size.width, new_size.height);
|
||||
@ -62,7 +62,7 @@ set_window_size(struct screen *screen, struct size new_size) {
|
||||
|
||||
// get the preferred display bounds (i.e. the screen bounds with some margins)
|
||||
static bool
|
||||
get_preferred_display_bounds(struct size *bounds) {
|
||||
get_preferred_display_bounds(struct sc_size *bounds) {
|
||||
SDL_Rect rect;
|
||||
#ifdef SCRCPY_SDL_HAS_GET_DISPLAY_USABLE_BOUNDS
|
||||
# define GET_DISPLAY_BOUNDS(i, r) SDL_GetDisplayUsableBounds((i), (r))
|
||||
@ -80,7 +80,7 @@ get_preferred_display_bounds(struct size *bounds) {
|
||||
}
|
||||
|
||||
static bool
|
||||
is_optimal_size(struct size current_size, struct size content_size) {
|
||||
is_optimal_size(struct sc_size current_size, struct sc_size content_size) {
|
||||
// The size is optimal if we can recompute one dimension of the current
|
||||
// size from the other
|
||||
return current_size.height == current_size.width * content_size.height
|
||||
@ -94,16 +94,16 @@ is_optimal_size(struct size current_size, struct size content_size) {
|
||||
// crops the black borders)
|
||||
// - it keeps the aspect ratio
|
||||
// - it scales down to make it fit in the display_size
|
||||
static struct size
|
||||
get_optimal_size(struct size current_size, struct size content_size) {
|
||||
static struct sc_size
|
||||
get_optimal_size(struct sc_size current_size, struct sc_size content_size) {
|
||||
if (content_size.width == 0 || content_size.height == 0) {
|
||||
// avoid division by 0
|
||||
return current_size;
|
||||
}
|
||||
|
||||
struct size window_size;
|
||||
struct sc_size window_size;
|
||||
|
||||
struct size display_size;
|
||||
struct sc_size display_size;
|
||||
if (!get_preferred_display_bounds(&display_size)) {
|
||||
// could not get display bounds, do not constraint the size
|
||||
window_size.width = current_size.width;
|
||||
@ -135,10 +135,10 @@ get_optimal_size(struct size current_size, struct size content_size) {
|
||||
|
||||
// initially, there is no current size, so use the frame size as current size
|
||||
// req_width and req_height, if not 0, are the sizes requested by the user
|
||||
static inline struct size
|
||||
get_initial_optimal_size(struct size content_size, uint16_t req_width,
|
||||
static inline struct sc_size
|
||||
get_initial_optimal_size(struct sc_size content_size, uint16_t req_width,
|
||||
uint16_t req_height) {
|
||||
struct size window_size;
|
||||
struct sc_size window_size;
|
||||
if (!req_width && !req_height) {
|
||||
window_size = get_optimal_size(content_size, content_size);
|
||||
} else {
|
||||
@ -166,9 +166,9 @@ screen_update_content_rect(struct screen *screen) {
|
||||
int dh;
|
||||
SDL_GL_GetDrawableSize(screen->window, &dw, &dh);
|
||||
|
||||
struct size content_size = screen->content_size;
|
||||
struct sc_size content_size = screen->content_size;
|
||||
// The drawable size is the window size * the HiDPI scale
|
||||
struct size drawable_size = {dw, dh};
|
||||
struct sc_size drawable_size = {dw, dh};
|
||||
|
||||
SDL_Rect *rect = &screen->rect;
|
||||
|
||||
@ -200,7 +200,7 @@ screen_update_content_rect(struct screen *screen) {
|
||||
static inline SDL_Texture *
|
||||
create_texture(struct screen *screen) {
|
||||
SDL_Renderer *renderer = screen->renderer;
|
||||
struct size size = screen->frame_size;
|
||||
struct sc_size size = screen->frame_size;
|
||||
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12,
|
||||
SDL_TEXTUREACCESS_STREAMING,
|
||||
size.width, size.height);
|
||||
@ -282,17 +282,33 @@ sc_video_buffer_on_new_frame(struct sc_video_buffer *vb, bool previous_skipped,
|
||||
(void) vb;
|
||||
struct screen *screen = userdata;
|
||||
|
||||
// event_failed implies previous_skipped (the previous frame may not have
|
||||
// been consumed if the event was not sent)
|
||||
assert(!screen->event_failed || previous_skipped);
|
||||
|
||||
bool need_new_event;
|
||||
if (previous_skipped) {
|
||||
fps_counter_add_skipped_frame(&screen->fps_counter);
|
||||
// The EVENT_NEW_FRAME triggered for the previous frame will consume
|
||||
// this new frame instead
|
||||
// this new frame instead, unless the previous event failed
|
||||
need_new_event = screen->event_failed;
|
||||
} else {
|
||||
need_new_event = true;
|
||||
}
|
||||
|
||||
if (need_new_event) {
|
||||
static SDL_Event new_frame_event = {
|
||||
.type = EVENT_NEW_FRAME,
|
||||
};
|
||||
|
||||
// Post the event on the UI thread
|
||||
SDL_PushEvent(&new_frame_event);
|
||||
int ret = SDL_PushEvent(&new_frame_event);
|
||||
if (ret < 0) {
|
||||
LOGW("Could not post new frame event: %s", SDL_GetError());
|
||||
screen->event_failed = true;
|
||||
} else {
|
||||
screen->event_failed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -302,6 +318,7 @@ screen_init(struct screen *screen, const struct screen_params *params) {
|
||||
screen->has_frame = false;
|
||||
screen->fullscreen = false;
|
||||
screen->maximized = false;
|
||||
screen->event_failed = false;
|
||||
|
||||
static const struct sc_video_buffer_callbacks cbs = {
|
||||
.on_new_frame = sc_video_buffer_on_new_frame,
|
||||
@ -330,13 +347,13 @@ screen_init(struct screen *screen, const struct screen_params *params) {
|
||||
if (screen->rotation) {
|
||||
LOGI("Initial display rotation set to %u", screen->rotation);
|
||||
}
|
||||
struct size content_size =
|
||||
struct sc_size content_size =
|
||||
get_rotated_size(screen->frame_size, screen->rotation);
|
||||
screen->content_size = content_size;
|
||||
|
||||
struct size window_size = get_initial_optimal_size(content_size,
|
||||
params->window_width,
|
||||
params->window_height);
|
||||
struct sc_size window_size =
|
||||
get_initial_optimal_size(content_size,params->window_width,
|
||||
params->window_height);
|
||||
uint32_t window_flags = SDL_WINDOW_HIDDEN
|
||||
| SDL_WINDOW_RESIZABLE
|
||||
| SDL_WINDOW_ALLOW_HIGHDPI;
|
||||
@ -508,10 +525,10 @@ 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 = {
|
||||
resize_for_content(struct screen *screen, struct sc_size old_content_size,
|
||||
struct sc_size new_content_size) {
|
||||
struct sc_size window_size = get_window_size(screen);
|
||||
struct sc_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
|
||||
@ -522,7 +539,7 @@ resize_for_content(struct screen *screen, struct size old_content_size,
|
||||
}
|
||||
|
||||
static void
|
||||
set_content_size(struct screen *screen, struct size new_content_size) {
|
||||
set_content_size(struct screen *screen, struct sc_size new_content_size) {
|
||||
if (!screen->fullscreen && !screen->maximized) {
|
||||
resize_for_content(screen, screen->content_size, new_content_size);
|
||||
} else if (!screen->resize_pending) {
|
||||
@ -553,7 +570,7 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct size new_content_size =
|
||||
struct sc_size new_content_size =
|
||||
get_rotated_size(screen->frame_size, rotation);
|
||||
|
||||
set_content_size(screen, new_content_size);
|
||||
@ -566,7 +583,7 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
|
||||
|
||||
// recreate the texture and resize the window if the frame size has changed
|
||||
static bool
|
||||
prepare_for_frame(struct screen *screen, struct size new_frame_size) {
|
||||
prepare_for_frame(struct screen *screen, struct sc_size new_frame_size) {
|
||||
if (screen->frame_size.width != new_frame_size.width
|
||||
|| screen->frame_size.height != new_frame_size.height) {
|
||||
// frame dimension changed, destroy texture
|
||||
@ -574,7 +591,7 @@ prepare_for_frame(struct screen *screen, struct size new_frame_size) {
|
||||
|
||||
screen->frame_size = new_frame_size;
|
||||
|
||||
struct size new_content_size =
|
||||
struct sc_size new_content_size =
|
||||
get_rotated_size(new_frame_size, screen->rotation);
|
||||
set_content_size(screen, new_content_size);
|
||||
|
||||
@ -615,7 +632,7 @@ screen_update_frame(struct screen *screen) {
|
||||
|
||||
fps_counter_add_rendered_frame(&screen->fps_counter);
|
||||
|
||||
struct size new_frame_size = {frame->width, frame->height};
|
||||
struct sc_size new_frame_size = {frame->width, frame->height};
|
||||
if (!prepare_for_frame(screen, new_frame_size)) {
|
||||
return false;
|
||||
}
|
||||
@ -682,10 +699,10 @@ screen_resize_to_fit(struct screen *screen) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct point point = get_window_position(screen);
|
||||
struct size window_size = get_window_size(screen);
|
||||
struct sc_point point = get_window_position(screen);
|
||||
struct sc_size window_size = get_window_size(screen);
|
||||
|
||||
struct size optimal_size =
|
||||
struct sc_size optimal_size =
|
||||
get_optimal_size(window_size, screen->content_size);
|
||||
|
||||
// Center the window related to the device screen
|
||||
@ -711,7 +728,7 @@ screen_resize_to_pixel_perfect(struct screen *screen) {
|
||||
screen->maximized = false;
|
||||
}
|
||||
|
||||
struct size content_size = screen->content_size;
|
||||
struct sc_size content_size = screen->content_size;
|
||||
SDL_SetWindowSize(screen->window, content_size.width, content_size.height);
|
||||
LOGD("Resized to pixel-perfect: %ux%u", content_size.width,
|
||||
content_size.height);
|
||||
@ -766,7 +783,7 @@ screen_handle_event(struct screen *screen, SDL_Event *event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct point
|
||||
struct sc_point
|
||||
screen_convert_drawable_to_frame_coords(struct screen *screen,
|
||||
int32_t x, int32_t y) {
|
||||
unsigned rotation = screen->rotation;
|
||||
@ -780,7 +797,7 @@ screen_convert_drawable_to_frame_coords(struct screen *screen,
|
||||
y = (int64_t) (y - screen->rect.y) * h / screen->rect.h;
|
||||
|
||||
// rotate
|
||||
struct point result;
|
||||
struct sc_point result;
|
||||
switch (rotation) {
|
||||
case 0:
|
||||
result.x = x;
|
||||
@ -803,7 +820,7 @@ screen_convert_drawable_to_frame_coords(struct screen *screen,
|
||||
return result;
|
||||
}
|
||||
|
||||
struct point
|
||||
struct sc_point
|
||||
screen_convert_window_to_frame_coords(struct screen *screen,
|
||||
int32_t x, int32_t y) {
|
||||
screen_hidpi_scale_coords(screen, &x, &y);
|
||||
|
@ -27,13 +27,13 @@ struct screen {
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Texture *texture;
|
||||
struct sc_opengl gl;
|
||||
struct size frame_size;
|
||||
struct size content_size; // rotated frame_size
|
||||
struct sc_size frame_size;
|
||||
struct sc_size content_size; // rotated frame_size
|
||||
|
||||
bool resize_pending; // resize requested while fullscreen or maximized
|
||||
// The content size the last time the window was not maximized or
|
||||
// fullscreen (meaningful only when resize_pending is true)
|
||||
struct size windowed_content_size;
|
||||
struct sc_size windowed_content_size;
|
||||
|
||||
// client rotation: 0, 1, 2 or 3 (x90 degrees counterclockwise)
|
||||
unsigned rotation;
|
||||
@ -44,12 +44,14 @@ struct screen {
|
||||
bool maximized;
|
||||
bool mipmaps;
|
||||
|
||||
bool event_failed; // in case SDL_PushEvent() returned an error
|
||||
|
||||
AVFrame *frame;
|
||||
};
|
||||
|
||||
struct screen_params {
|
||||
const char *window_title;
|
||||
struct size frame_size;
|
||||
struct sc_size frame_size;
|
||||
bool always_on_top;
|
||||
|
||||
int16_t window_x;
|
||||
@ -120,13 +122,13 @@ screen_handle_event(struct screen *screen, SDL_Event *event);
|
||||
|
||||
// convert point from window coordinates to frame coordinates
|
||||
// x and y are expressed in pixels
|
||||
struct point
|
||||
struct sc_point
|
||||
screen_convert_window_to_frame_coords(struct screen *screen,
|
||||
int32_t x, int32_t y);
|
||||
|
||||
// convert point from drawable coordinates to frame coordinates
|
||||
// x and y are expressed in pixels
|
||||
struct point
|
||||
struct sc_point
|
||||
screen_convert_drawable_to_frame_coords(struct screen *screen,
|
||||
int32_t x, int32_t y);
|
||||
|
||||
|
@ -426,8 +426,7 @@ error:
|
||||
}
|
||||
|
||||
static bool
|
||||
device_read_info(sc_socket device_socket, char *device_name,
|
||||
struct size *size) {
|
||||
device_read_info(sc_socket device_socket, struct server_info *info) {
|
||||
unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4];
|
||||
ssize_t r = net_recv_all(device_socket, buf, sizeof(buf));
|
||||
if (r < DEVICE_NAME_FIELD_LENGTH + 4) {
|
||||
@ -436,18 +435,17 @@ device_read_info(sc_socket device_socket, char *device_name,
|
||||
}
|
||||
// in case the client sends garbage
|
||||
buf[DEVICE_NAME_FIELD_LENGTH - 1] = '\0';
|
||||
// strcpy is safe here, since name contains at least
|
||||
// DEVICE_NAME_FIELD_LENGTH bytes and strlen(buf) < DEVICE_NAME_FIELD_LENGTH
|
||||
strcpy(device_name, (char *) buf);
|
||||
size->width = (buf[DEVICE_NAME_FIELD_LENGTH] << 8)
|
||||
| buf[DEVICE_NAME_FIELD_LENGTH + 1];
|
||||
size->height = (buf[DEVICE_NAME_FIELD_LENGTH + 2] << 8)
|
||||
| buf[DEVICE_NAME_FIELD_LENGTH + 3];
|
||||
memcpy(info->device_name, (char *) buf, sizeof(info->device_name));
|
||||
|
||||
info->frame_size.width = (buf[DEVICE_NAME_FIELD_LENGTH] << 8)
|
||||
| buf[DEVICE_NAME_FIELD_LENGTH + 1];
|
||||
info->frame_size.height = (buf[DEVICE_NAME_FIELD_LENGTH + 2] << 8)
|
||||
| buf[DEVICE_NAME_FIELD_LENGTH + 3];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
server_connect_to(struct server *server, char *device_name, struct size *size) {
|
||||
server_connect_to(struct server *server, struct server_info *info) {
|
||||
if (!server->tunnel_forward) {
|
||||
server->video_socket = net_accept(server->server_socket);
|
||||
if (server->video_socket == SC_INVALID_SOCKET) {
|
||||
@ -488,7 +486,7 @@ server_connect_to(struct server *server, char *device_name, struct size *size) {
|
||||
server->tunnel_enabled = false;
|
||||
|
||||
// The sockets will be closed on stop if device_read_info() fails
|
||||
return device_read_info(server->video_socket, device_name, size);
|
||||
return device_read_info(server->video_socket, info);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -14,6 +14,12 @@
|
||||
#include "util/net.h"
|
||||
#include "util/thread.h"
|
||||
|
||||
#define DEVICE_NAME_FIELD_LENGTH 64
|
||||
struct server_info {
|
||||
char device_name[DEVICE_NAME_FIELD_LENGTH];
|
||||
struct sc_size frame_size;
|
||||
};
|
||||
|
||||
struct server {
|
||||
char *serial;
|
||||
process_t process;
|
||||
@ -58,11 +64,9 @@ server_init(struct server *server);
|
||||
bool
|
||||
server_start(struct server *server, const struct server_params *params);
|
||||
|
||||
#define DEVICE_NAME_FIELD_LENGTH 64
|
||||
// block until the communication with the server is established
|
||||
// device_name must point to a buffer of at least DEVICE_NAME_FIELD_LENGTH bytes
|
||||
bool
|
||||
server_connect_to(struct server *server, char *device_name, struct size *size);
|
||||
server_connect_to(struct server *server, struct server_info *info);
|
||||
|
||||
// disconnect and kill the server process
|
||||
void
|
||||
|
@ -1,9 +1,11 @@
|
||||
#include "str_util.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "util/strbuf.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
@ -209,3 +211,76 @@ utf8_from_wide_char(const wchar_t *ws) {
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
char *wrap_lines(const char *input, unsigned columns, unsigned indent) {
|
||||
struct sc_strbuf buf;
|
||||
|
||||
// The output string should not be a lot longer than the input string (just
|
||||
// a few '\n' added), so this initial capacity should almost always avoid
|
||||
// internal realloc() in string buffer
|
||||
size_t cap = strlen(input) * 3 / 2;
|
||||
|
||||
if (!sc_strbuf_init(&buf, cap)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
assert(indent < columns);
|
||||
|
||||
#define APPEND(S,N) if (!sc_strbuf_append(&buf, S, N)) goto error
|
||||
#define APPEND_CHAR(C) if (!sc_strbuf_append_char(&buf, C)) goto error
|
||||
#define APPEND_N(C,N) if (!sc_strbuf_append_n(&buf, C, N)) goto error
|
||||
#define APPEND_INDENT() if (indent) APPEND_N(' ', indent)
|
||||
|
||||
APPEND_INDENT();
|
||||
|
||||
// The last separator encountered, it must be inserted only conditionnaly,
|
||||
// depending on the next token
|
||||
char pending = 0;
|
||||
|
||||
// col tracks the current column in the current line
|
||||
size_t col = indent;
|
||||
while (*input) {
|
||||
size_t sep_idx = strcspn(input, "\n ");
|
||||
bool wrap = col + sep_idx > columns;
|
||||
|
||||
char sep = input[sep_idx];
|
||||
if (sep == ' ')
|
||||
sep = ' ';
|
||||
|
||||
if (wrap) {
|
||||
APPEND_CHAR('\n');
|
||||
APPEND_INDENT();
|
||||
col = indent;
|
||||
} else if (pending) {
|
||||
APPEND_CHAR(pending);
|
||||
++col;
|
||||
if (pending == '\n')
|
||||
{
|
||||
APPEND_INDENT();
|
||||
col = indent;
|
||||
}
|
||||
}
|
||||
|
||||
if (sep_idx) {
|
||||
APPEND(input, sep_idx);
|
||||
col += sep_idx;
|
||||
}
|
||||
|
||||
pending = sep;
|
||||
|
||||
input += sep_idx;
|
||||
if (*input != '\0') {
|
||||
// Skip the separator
|
||||
++input;
|
||||
}
|
||||
}
|
||||
|
||||
if (pending)
|
||||
APPEND_CHAR(pending);
|
||||
|
||||
return buf.s;
|
||||
|
||||
error:
|
||||
free(buf.s);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -62,4 +62,8 @@ char *
|
||||
utf8_from_wide_char(const wchar_t *s);
|
||||
#endif
|
||||
|
||||
// Wrap input lines at words boundaries (spaces) so that they fit in 'columns'
|
||||
// columns, left-indented by 'indent' spaces
|
||||
char *wrap_lines(const char *input, unsigned columns, unsigned indent);
|
||||
|
||||
#endif
|
||||
|
77
app/src/util/strbuf.c
Normal file
77
app/src/util/strbuf.c
Normal file
@ -0,0 +1,77 @@
|
||||
#include "strbuf.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
bool
|
||||
sc_strbuf_init(struct sc_strbuf *buf, size_t init_cap) {
|
||||
buf->s = malloc(init_cap + 1); // +1 for '\0'
|
||||
if (!buf->s) {
|
||||
return false;
|
||||
}
|
||||
|
||||
buf->len = 0;
|
||||
buf->cap = init_cap;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
sc_strbuf_reserve(struct sc_strbuf *buf, size_t len) {
|
||||
if (buf->len + len > buf->cap) {
|
||||
fprintf(stderr, "realloc\n");
|
||||
size_t new_cap = buf->cap * 3 / 2 + len;
|
||||
char *s = realloc(buf->s, new_cap + 1); // +1 for '\0'
|
||||
if (!s) {
|
||||
// Leave the old buf->s
|
||||
return false;
|
||||
}
|
||||
buf->s = s;
|
||||
buf->cap = new_cap;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_strbuf_append(struct sc_strbuf *buf, const char *s, size_t len) {
|
||||
assert(s);
|
||||
assert(*s);
|
||||
assert(strlen(s) >= len);
|
||||
if (!sc_strbuf_reserve(buf, len)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(&buf->s[buf->len], s, len);
|
||||
buf->len += len;
|
||||
buf->s[buf->len] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_strbuf_append_char(struct sc_strbuf *buf, const char c) {
|
||||
if (!sc_strbuf_reserve(buf, 1)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
buf->s[buf->len] = c;
|
||||
buf->len ++;
|
||||
buf->s[buf->len] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_strbuf_append_n(struct sc_strbuf *buf, const char c, size_t n) {
|
||||
if (!sc_strbuf_reserve(buf, n)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memset(&buf->s[buf->len], c, n);
|
||||
buf->len += n;
|
||||
buf->s[buf->len] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
37
app/src/util/strbuf.h
Normal file
37
app/src/util/strbuf.h
Normal file
@ -0,0 +1,37 @@
|
||||
#ifndef SC_STRBUF_H
|
||||
#define SC_STRBUF_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
struct sc_strbuf {
|
||||
char *s;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
};
|
||||
|
||||
// buf->s must be manually freed by the caller
|
||||
bool
|
||||
sc_strbuf_init(struct sc_strbuf *buf, size_t init_cap);
|
||||
|
||||
bool
|
||||
sc_strbuf_append(struct sc_strbuf *buf, const char *s, size_t len);
|
||||
|
||||
bool
|
||||
sc_strbuf_append_char(struct sc_strbuf *buf, const char c);
|
||||
|
||||
bool
|
||||
sc_strbuf_append_n(struct sc_strbuf *buf, const char c, size_t n);
|
||||
|
||||
static inline bool
|
||||
sc_strbuf_append_str(struct sc_strbuf *buf, const char *s) {
|
||||
return sc_strbuf_append(buf, s, strlen(s));
|
||||
}
|
||||
|
||||
// Append static string (i.e. the string size is known at compile time, for
|
||||
// example a string literal)
|
||||
#define sc_strbuf_append_staticstr(BUF, S) \
|
||||
sc_strbuf_append(BUF, S, sizeof(S) - 1)
|
||||
|
||||
#endif
|
@ -1,6 +1,8 @@
|
||||
#ifndef SC_TICK_H
|
||||
#define SC_TICK_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef int64_t sc_tick;
|
||||
|
@ -359,7 +359,7 @@ sc_v4l2_frame_sink_push(struct sc_frame_sink *sink, const AVFrame *frame) {
|
||||
|
||||
bool
|
||||
sc_v4l2_sink_init(struct sc_v4l2_sink *vs, const char *device_name,
|
||||
struct size frame_size, sc_tick buffering_time) {
|
||||
struct sc_size frame_size, sc_tick buffering_time) {
|
||||
vs->device_name = strdup(device_name);
|
||||
if (!vs->device_name) {
|
||||
LOGE("Could not strdup v4l2 device name");
|
||||
|
@ -18,7 +18,7 @@ struct sc_v4l2_sink {
|
||||
AVCodecContext *encoder_ctx;
|
||||
|
||||
char *device_name;
|
||||
struct size frame_size;
|
||||
struct sc_size frame_size;
|
||||
sc_tick buffering_time;
|
||||
|
||||
sc_thread thread;
|
||||
@ -34,7 +34,7 @@ struct sc_v4l2_sink {
|
||||
|
||||
bool
|
||||
sc_v4l2_sink_init(struct sc_v4l2_sink *vs, const char *device_name,
|
||||
struct size frame_size, sc_tick buffering_time);
|
||||
struct sc_size frame_size, sc_tick buffering_time);
|
||||
|
||||
void
|
||||
sc_v4l2_sink_destroy(struct sc_v4l2_sink *vs);
|
||||
|
42
app/tests/test_strbuf.c
Normal file
42
app/tests/test_strbuf.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util/strbuf.h"
|
||||
|
||||
static void test_strbuf_simple(void) {
|
||||
struct sc_strbuf buf;
|
||||
bool ok = sc_strbuf_init(&buf, 10);
|
||||
assert(ok);
|
||||
|
||||
ok = sc_strbuf_append_staticstr(&buf, "Hello");
|
||||
assert(ok);
|
||||
|
||||
ok = sc_strbuf_append_char(&buf, ' ');
|
||||
assert(ok);
|
||||
|
||||
ok = sc_strbuf_append_staticstr(&buf, "world");
|
||||
assert(ok);
|
||||
|
||||
ok = sc_strbuf_append_staticstr(&buf, "!\n");
|
||||
assert(ok);
|
||||
|
||||
ok = sc_strbuf_append_staticstr(&buf, "This is a test");
|
||||
assert(ok);
|
||||
|
||||
ok = sc_strbuf_append_n(&buf, '.', 3);
|
||||
assert(ok);
|
||||
|
||||
assert(!strcmp(buf.s, "Hello world!\nThis is a test..."));
|
||||
free(buf.s);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
test_strbuf_simple();
|
||||
return 0;
|
||||
}
|
@ -299,6 +299,45 @@ static void test_strlist_contains(void) {
|
||||
assert(strlist_contains("xyz", '\0', "xyz"));
|
||||
}
|
||||
|
||||
static void test_wrap_lines(void) {
|
||||
const char *s = "This is a text to test line wrapping. The lines must be "
|
||||
"wrapped either at a space or a line break.\n"
|
||||
"\n"
|
||||
"This rectangle must remains a rectangle because it is "
|
||||
"drawn in lines having lengths lower than the specified "
|
||||
"number of columns:\n"
|
||||
" +----+\n"
|
||||
" | |\n"
|
||||
" +----+\n";
|
||||
|
||||
// |---- 1 1 2 2|
|
||||
// |0 5 0 5 0 3| <-- 24 columns
|
||||
const char *expected = " This is a text to\n"
|
||||
" test line wrapping.\n"
|
||||
" The lines must be\n"
|
||||
" wrapped either at a\n"
|
||||
" space or a line\n"
|
||||
" break.\n"
|
||||
" \n"
|
||||
" This rectangle must\n"
|
||||
" remains a rectangle\n"
|
||||
" because it is drawn\n"
|
||||
" in lines having\n"
|
||||
" lengths lower than\n"
|
||||
" the specified number\n"
|
||||
" of columns:\n"
|
||||
" +----+\n"
|
||||
" | |\n"
|
||||
" +----+\n";
|
||||
|
||||
char *formatted = wrap_lines(s, 24, 4);
|
||||
assert(formatted);
|
||||
|
||||
assert(!strcmp(formatted, expected));
|
||||
|
||||
free(formatted);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
@ -317,5 +356,6 @@ int main(int argc, char *argv[]) {
|
||||
test_parse_integers();
|
||||
test_parse_integer_with_suffix();
|
||||
test_strlist_contains();
|
||||
test_wrap_lines();
|
||||
return 0;
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ buildscript {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:4.0.1'
|
||||
classpath 'com.android.tools.build:gradle:7.0.2'
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
|
@ -2,7 +2,7 @@ apply plugin: 'checkstyle'
|
||||
check.dependsOn 'checkstyle'
|
||||
|
||||
checkstyle {
|
||||
toolVersion = '6.19'
|
||||
toolVersion = '9.0.1'
|
||||
}
|
||||
|
||||
task checkstyle(type: Checkstyle) {
|
||||
|
@ -37,6 +37,14 @@ page at http://checkstyle.sourceforge.net/config.html -->
|
||||
|
||||
<module name="SuppressWarningsFilter"/>
|
||||
|
||||
<module name="LineLength">
|
||||
<!-- what is a good max value? -->
|
||||
<property name="max" value="150" />
|
||||
<!-- ignore lines like "$File: //depot/... $" -->
|
||||
<property name="ignorePattern" value="\$File.*\$" />
|
||||
<property name="severity" value="info" />
|
||||
</module>
|
||||
|
||||
<module name="TreeWalker">
|
||||
|
||||
<!-- Checks for Naming Conventions. -->
|
||||
@ -72,13 +80,6 @@ page at http://checkstyle.sourceforge.net/config.html -->
|
||||
|
||||
<!-- Checks for Size Violations. -->
|
||||
<!-- See http://checkstyle.sf.net/config_sizes.html -->
|
||||
<module name="LineLength">
|
||||
<!-- what is a good max value? -->
|
||||
<property name="max" value="150" />
|
||||
<!-- ignore lines like "$File: //depot/... $" -->
|
||||
<property name="ignorePattern" value="\$File.*\$" />
|
||||
<property name="severity" value="info" />
|
||||
</module>
|
||||
<module name="MethodLength" />
|
||||
<module name="ParameterNumber">
|
||||
<property name="ignoreOverriddenMethods" value="true"/>
|
||||
@ -152,26 +153,6 @@ page at http://checkstyle.sourceforge.net/config.html -->
|
||||
</module>
|
||||
<module name="UpperEll" />
|
||||
|
||||
<module name="FileContentsHolder" />
|
||||
<!-- Required by comment suppression filters -->
|
||||
|
||||
</module>
|
||||
|
||||
<module name="SuppressionFilter">
|
||||
<!--<property name="file" value="team-props/checkstyle/checkstyle-suppressions.xml" />-->
|
||||
</module>
|
||||
|
||||
<!-- Enable suppression comments -->
|
||||
<module name="SuppressionCommentFilter">
|
||||
<property name="offCommentFormat" value="CHECKSTYLE IGNORE\s+(\S+)" />
|
||||
<property name="onCommentFormat" value="CHECKSTYLE END IGNORE\s+(\S+)" />
|
||||
<property name="checkFormat" value="$1" />
|
||||
</module>
|
||||
<module name="SuppressWithNearbyCommentFilter">
|
||||
<!-- Syntax is "SUPPRESS CHECKSTYLE name" -->
|
||||
<property name="commentFormat" value="SUPPRESS CHECKSTYLE (\w+)" />
|
||||
<property name="checkFormat" value="$1" />
|
||||
<property name="influenceFormat" value="1" />
|
||||
</module>
|
||||
|
||||
</module>
|
||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,5 +1,5 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
@ -1,11 +1,11 @@
|
||||
apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
compileSdkVersion 30
|
||||
compileSdkVersion 31
|
||||
defaultConfig {
|
||||
applicationId "com.genymobile.scrcpy"
|
||||
minSdkVersion 21
|
||||
targetSdkVersion 30
|
||||
targetSdkVersion 31
|
||||
versionCode 11900
|
||||
versionName "1.19"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
|
@ -2,7 +2,6 @@ package com.genymobile.scrcpy;
|
||||
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
Reference in New Issue
Block a user