Skip to content

Display and control your Android device

Move device-related code to device.c

Romain Vimont3b06e7d50063
diff --git a/app/meson.build b/app/meson.build
index 216841ed..0ad89382 100644
--- a/app/meson.build
+++ b/app/meson.build
@@ -7,6 +7,7 @@ src = [
'src/controller.c',
'src/convert.c',
'src/decoder.c',
+ 'src/device.c',
'src/frames.c',
'src/lockutil.c',
'src/netutil.c',
diff --git a/app/src/device.c b/app/src/device.c
new file mode 100644
index 00000000..8324faeb
--- /dev/null
+++ b/app/src/device.c
@@ -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;
+}
diff --git a/app/src/device.h b/app/src/device.h
new file mode 100644
index 00000000..ffaeaa28
--- /dev/null
+++ b/app/src/device.h
@@ -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
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c
index a791903c..cda3bc2a 100644
--- a/app/src/scrcpy.c
+++ b/app/src/scrcpy.c
@@ -13,6 +13,7 @@
#include "controller.h"
#include "convert.h"
#include "decoder.h"
+#include "device.h"
#include "events.h"
#include "frames.h"
#include "lockutil.h"
@@ -21,8 +22,6 @@
#include "server.h"
#include "tinyxpm.h"
-#define DEVICE_NAME_FIELD_LENGTH 64
-
static struct server server = SERVER_INITIALIZER;
static struct screen screen = SCREEN_INITIALIZER;
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) {
int x;
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
// therefore, we transmit the screen size before the video stream, to be able
// to init the window immediately
- if (!read_initial_device_info(device_socket, device_name, &frame_size)) {
- SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not retrieve initial screen size");
+ if (!device_read_info(device_socket, device_name, &frame_size)) {
server_stop(&server, serial);
return SDL_FALSE;
}