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); sdl_configure(options->display, options->disable_screensaver);
char device_name[DEVICE_NAME_FIELD_LENGTH]; struct server_info info;
struct sc_size frame_size;
if (!server_connect_to(&s->server, device_name, &frame_size)) { if (!server_connect_to(&s->server, &info)) {
goto end; goto end;
} }
@ -361,7 +360,7 @@ scrcpy(struct scrcpy_options *options) {
if (!recorder_init(&s->recorder, if (!recorder_init(&s->recorder,
options->record_filename, options->record_filename,
options->record_format, options->record_format,
frame_size)) { info.frame_size)) {
goto end; goto end;
} }
rec = &s->recorder; rec = &s->recorder;
@ -407,11 +406,11 @@ scrcpy(struct scrcpy_options *options) {
if (options->display) { if (options->display) {
const char *window_title = 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 = { struct screen_params screen_params = {
.window_title = window_title, .window_title = window_title,
.frame_size = frame_size, .frame_size = info.frame_size,
.always_on_top = options->always_on_top, .always_on_top = options->always_on_top,
.window_x = options->window_x, .window_x = options->window_x,
.window_y = options->window_y, .window_y = options->window_y,
@ -434,8 +433,8 @@ scrcpy(struct scrcpy_options *options) {
#ifdef HAVE_V4L2 #ifdef HAVE_V4L2
if (options->v4l2_device) { if (options->v4l2_device) {
if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device, frame_size, if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device,
options->v4l2_buffer)) { info.frame_size, options->v4l2_buffer)) {
goto end; goto end;
} }

View file

@ -426,8 +426,7 @@ error:
} }
static bool static bool
device_read_info(sc_socket device_socket, char *device_name, device_read_info(sc_socket device_socket, struct server_info *info) {
struct sc_size *size) {
unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4]; unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4];
ssize_t r = net_recv_all(device_socket, buf, sizeof(buf)); ssize_t r = net_recv_all(device_socket, buf, sizeof(buf));
if (r < DEVICE_NAME_FIELD_LENGTH + 4) { 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 // in case the client sends garbage
buf[DEVICE_NAME_FIELD_LENGTH - 1] = '\0'; buf[DEVICE_NAME_FIELD_LENGTH - 1] = '\0';
// strcpy is safe here, since name contains at least memcpy(info->device_name, (char *) buf, sizeof(info->device_name));
// DEVICE_NAME_FIELD_LENGTH bytes and strlen(buf) < DEVICE_NAME_FIELD_LENGTH
strcpy(device_name, (char *) buf); info->frame_size.width = (buf[DEVICE_NAME_FIELD_LENGTH] << 8)
size->width = (buf[DEVICE_NAME_FIELD_LENGTH] << 8)
| buf[DEVICE_NAME_FIELD_LENGTH + 1]; | 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]; | buf[DEVICE_NAME_FIELD_LENGTH + 3];
return true; return true;
} }
bool bool
server_connect_to(struct server *server, char *device_name, server_connect_to(struct server *server, struct server_info *info) {
struct sc_size *size) {
if (!server->tunnel_forward) { if (!server->tunnel_forward) {
server->video_socket = net_accept(server->server_socket); server->video_socket = net_accept(server->server_socket);
if (server->video_socket == SC_INVALID_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; server->tunnel_enabled = false;
// The sockets will be closed on stop if device_read_info() fails // 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 void

View file

@ -14,6 +14,12 @@
#include "util/net.h" #include "util/net.h"
#include "util/thread.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 { struct server {
char *serial; char *serial;
process_t process; process_t process;
@ -58,12 +64,9 @@ server_init(struct server *server);
bool bool
server_start(struct server *server, const struct server_params *params); 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 // 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 bool
server_connect_to(struct server *server, char *device_name, server_connect_to(struct server *server, struct server_info *info);
struct sc_size *size);
// disconnect and kill the server process // disconnect and kill the server process
void void