Move device-related code to device.c

Move the code to read the initial device info from scrcpy.c to a
separate file, device.c.
This commit is contained in:
Romain Vimont 2018-02-08 15:42:49 +01:00
parent 28c5cc030b
commit 3b06e7d500
4 changed files with 33 additions and 19 deletions

View file

@ -7,6 +7,7 @@ src = [
'src/controller.c', 'src/controller.c',
'src/convert.c', 'src/convert.c',
'src/decoder.c', 'src/decoder.c',
'src/device.c',
'src/frames.c', 'src/frames.c',
'src/lockutil.c', 'src/lockutil.c',
'src/netutil.c', 'src/netutil.c',

16
app/src/device.c Normal file
View file

@ -0,0 +1,16 @@
#include "device.h"
SDL_bool device_read_info(TCPsocket device_socket, char *device_name, struct size *size) {
unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4];
if (SDLNet_TCP_Recv(device_socket, buf, sizeof(buf)) <= 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not retrieve device information");
return SDL_FALSE;
}
buf[DEVICE_NAME_FIELD_LENGTH - 1] = '\0'; // in case the client sends garbage
// 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];
return SDL_TRUE;
}

14
app/src/device.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef DEVICE_H
#define DEVICE_H
#include <SDL2/SDL_net.h>
#include <SDL2/SDL_stdinc.h>
#include "common.h"
#define DEVICE_NAME_FIELD_LENGTH 64
// name must be at least DEVICE_NAME_FIELD_LENGTH bytes
SDL_bool device_read_info(TCPsocket device_socket, char *name, struct size *frame_size);
#endif

View file

@ -13,6 +13,7 @@
#include "controller.h" #include "controller.h"
#include "convert.h" #include "convert.h"
#include "decoder.h" #include "decoder.h"
#include "device.h"
#include "events.h" #include "events.h"
#include "frames.h" #include "frames.h"
#include "lockutil.h" #include "lockutil.h"
@ -21,8 +22,6 @@
#include "server.h" #include "server.h"
#include "tinyxpm.h" #include "tinyxpm.h"
#define DEVICE_NAME_FIELD_LENGTH 64
static struct server server = SERVER_INITIALIZER; static struct server server = SERVER_INITIALIZER;
static struct screen screen = SCREEN_INITIALIZER; static struct screen screen = SCREEN_INITIALIZER;
static struct frames frames; static struct frames frames;
@ -47,21 +46,6 @@ static void count_frame(void) {
} }
} }
// name must be at least DEVICE_NAME_FIELD_LENGTH bytes
static SDL_bool read_initial_device_info(TCPsocket socket, char *device_name, struct size *size) {
unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4];
if (SDLNet_TCP_Recv(socket, buf, sizeof(buf)) <= 0) {
return SDL_FALSE;
}
buf[DEVICE_NAME_FIELD_LENGTH - 1] = '\0'; // in case the client sends garbage
// scrcpy 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];
return SDL_TRUE;
}
static struct point get_mouse_point(void) { static struct point get_mouse_point(void) {
int x; int x;
int y; int y;
@ -339,8 +323,7 @@ SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 b
// screenrecord does not send frames when the screen content does not change // screenrecord does not send frames when the screen content does not change
// therefore, we transmit the screen size before the video stream, to be able // therefore, we transmit the screen size before the video stream, to be able
// to init the window immediately // to init the window immediately
if (!read_initial_device_info(device_socket, device_name, &frame_size)) { if (!device_read_info(device_socket, device_name, &frame_size)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not retrieve initial screen size");
server_stop(&server, serial); server_stop(&server, serial);
return SDL_FALSE; return SDL_FALSE;
} }