Wrap device name and size in a struct

As a benefit, this avoids to take care of the device name length on the
caller side.
This commit is contained in:
Romain Vimont 2021-10-30 19:07:35 +02:00
parent 58ea238fb2
commit 676fa73d2c
3 changed files with 23 additions and 24 deletions

View file

@ -331,10 +331,9 @@ scrcpy(struct scrcpy_options *options) {
sdl_configure(options->display, options->disable_screensaver);
char device_name[DEVICE_NAME_FIELD_LENGTH];
struct sc_size frame_size;
struct server_info info;
if (!server_connect_to(&s->server, device_name, &frame_size)) {
if (!server_connect_to(&s->server, &info)) {
goto end;
}
@ -361,7 +360,7 @@ scrcpy(struct scrcpy_options *options) {
if (!recorder_init(&s->recorder,
options->record_filename,
options->record_format,
frame_size)) {
info.frame_size)) {
goto end;
}
rec = &s->recorder;
@ -407,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,
@ -434,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;
}

View file

@ -426,8 +426,7 @@ error:
}
static bool
device_read_info(sc_socket device_socket, char *device_name,
struct sc_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,19 +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)
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];
size->height = (buf[DEVICE_NAME_FIELD_LENGTH + 2] << 8)
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 sc_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) {
@ -489,7 +486,7 @@ server_connect_to(struct server *server, char *device_name,
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

View file

@ -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,12 +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 sc_size *size);
server_connect_to(struct server *server, struct server_info *info);
// disconnect and kill the server process
void