Skip to content

Commit 0ba74fb

Browse files
committed
Make scrcpy.h independant of other headers
The header scrcpy.h is intended to be the "public" API. It should not depend on other internal headers. Therefore, declare all required structs in this header and adapt internal code.
1 parent 29e5af7 commit 0ba74fb

File tree

12 files changed

+62
-52
lines changed

12 files changed

+62
-52
lines changed

app/src/cli.c

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
#include <assert.h>
44
#include <getopt.h>
55
#include <stdint.h>
6+
#include <stdio.h>
67
#include <unistd.h>
78

89
#include "config.h"
9-
#include "recorder.h"
10+
#include "scrcpy.h"
1011
#include "util/log.h"
1112
#include "util/str_util.h"
1213

@@ -382,10 +383,10 @@ parse_rotation(const char *s, uint8_t *rotation) {
382383
static bool
383384
parse_window_position(const char *s, int16_t *position) {
384385
// special value for "auto"
385-
static_assert(WINDOW_POSITION_UNDEFINED == -0x8000, "unexpected value");
386+
static_assert(SC_WINDOW_POSITION_UNDEFINED == -0x8000, "unexpected value");
386387

387388
if (!strcmp(s, "auto")) {
388-
*position = WINDOW_POSITION_UNDEFINED;
389+
*position = SC_WINDOW_POSITION_UNDEFINED;
389390
return true;
390391
}
391392

@@ -414,7 +415,7 @@ parse_window_dimension(const char *s, uint16_t *dimension) {
414415
}
415416

416417
static bool
417-
parse_port_range(const char *s, struct port_range *port_range) {
418+
parse_port_range(const char *s, struct sc_port_range *port_range) {
418419
long values[2];
419420
size_t count = parse_integers_arg(s, 2, values, 0, 0xFFFF, "port");
420421
if (!count) {
@@ -480,31 +481,31 @@ parse_log_level(const char *s, enum sc_log_level *log_level) {
480481
}
481482

482483
static bool
483-
parse_record_format(const char *optarg, enum recorder_format *format) {
484+
parse_record_format(const char *optarg, enum sc_record_format *format) {
484485
if (!strcmp(optarg, "mp4")) {
485-
*format = RECORDER_FORMAT_MP4;
486+
*format = SC_RECORD_FORMAT_MP4;
486487
return true;
487488
}
488489
if (!strcmp(optarg, "mkv")) {
489-
*format = RECORDER_FORMAT_MKV;
490+
*format = SC_RECORD_FORMAT_MKV;
490491
return true;
491492
}
492493
LOGE("Unsupported format: %s (expected mp4 or mkv)", optarg);
493494
return false;
494495
}
495496

496-
static enum recorder_format
497+
static enum sc_record_format
497498
guess_record_format(const char *filename) {
498499
size_t len = strlen(filename);
499500
if (len < 4) {
500501
return 0;
501502
}
502503
const char *ext = &filename[len - 4];
503504
if (!strcmp(ext, ".mp4")) {
504-
return RECORDER_FORMAT_MP4;
505+
return SC_RECORD_FORMAT_MP4;
505506
}
506507
if (!strcmp(ext, ".mkv")) {
507-
return RECORDER_FORMAT_MKV;
508+
return SC_RECORD_FORMAT_MKV;
508509
}
509510
return 0;
510511
}

app/src/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "scrcpy.h"
22

3+
#include <assert.h>
34
#include <stdbool.h>
45
#include <unistd.h>
56
#include <libavformat/avformat.h>

app/src/recorder.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ recorder_queue_clear(struct recorder_queue *queue) {
6363
bool
6464
recorder_init(struct recorder *recorder,
6565
const char *filename,
66-
enum recorder_format format,
66+
enum sc_record_format format,
6767
struct size declared_frame_size) {
6868
recorder->filename = SDL_strdup(filename);
6969
if (!recorder->filename) {
@@ -105,10 +105,10 @@ recorder_destroy(struct recorder *recorder) {
105105
}
106106

107107
static const char *
108-
recorder_get_format_name(enum recorder_format format) {
108+
recorder_get_format_name(enum sc_record_format format) {
109109
switch (format) {
110-
case RECORDER_FORMAT_MP4: return "mp4";
111-
case RECORDER_FORMAT_MKV: return "matroska";
110+
case SC_RECORD_FORMAT_MP4: return "mp4";
111+
case SC_RECORD_FORMAT_MKV: return "matroska";
112112
default: return NULL;
113113
}
114114
}

app/src/recorder.h

+3-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@
88

99
#include "config.h"
1010
#include "common.h"
11+
#include "scrcpy.h"
1112
#include "util/queue.h"
1213

13-
enum recorder_format {
14-
RECORDER_FORMAT_AUTO,
15-
RECORDER_FORMAT_MP4,
16-
RECORDER_FORMAT_MKV,
17-
};
18-
1914
struct record_packet {
2015
AVPacket packet;
2116
struct record_packet *next;
@@ -25,7 +20,7 @@ struct recorder_queue QUEUE(struct record_packet);
2520

2621
struct recorder {
2722
char *filename;
28-
enum recorder_format format;
23+
enum sc_record_format format;
2924
AVFormatContext *ctx;
3025
struct size declared_frame_size;
3126
bool header_written;
@@ -46,7 +41,7 @@ struct recorder {
4641

4742
bool
4843
recorder_init(struct recorder *recorder, const char *filename,
49-
enum recorder_format format, struct size declared_frame_size);
44+
enum sc_record_format format, struct size declared_frame_size);
5045

5146
void
5247
recorder_destroy(struct recorder *recorder);

app/src/scrcpy.c

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <SDL2/SDL.h>
99

1010
#ifdef _WIN32
11+
// not needed here, but winsock2.h must never be included AFTER windows.h
12+
# include <winsock2.h>
1113
# include <windows.h>
1214
#endif
1315

app/src/scrcpy.h

+28-11
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,30 @@
22
#define SCRCPY_H
33

44
#include <stdbool.h>
5+
#include <stddef.h>
56
#include <stdint.h>
67

78
#include "config.h"
8-
#include "common.h"
9-
#include "input_manager.h"
10-
#include "recorder.h"
11-
#include "util/log.h"
9+
10+
enum sc_log_level {
11+
SC_LOG_LEVEL_DEBUG,
12+
SC_LOG_LEVEL_INFO,
13+
SC_LOG_LEVEL_WARN,
14+
SC_LOG_LEVEL_ERROR,
15+
};
16+
17+
enum sc_record_format {
18+
SC_RECORD_FORMAT_AUTO,
19+
SC_RECORD_FORMAT_MP4,
20+
SC_RECORD_FORMAT_MKV,
21+
};
22+
23+
struct sc_port_range {
24+
uint16_t first;
25+
uint16_t last;
26+
};
27+
28+
#define SC_WINDOW_POSITION_UNDEFINED (-0x8000)
1229

1330
struct scrcpy_options {
1431
const char *serial;
@@ -19,15 +36,15 @@ struct scrcpy_options {
1936
const char *render_driver;
2037
const char *codec_options;
2138
enum sc_log_level log_level;
22-
enum recorder_format record_format;
23-
struct port_range port_range;
39+
enum sc_record_format record_format;
40+
struct sc_port_range port_range;
2441
uint16_t max_size;
2542
uint32_t bit_rate;
2643
uint16_t max_fps;
2744
int8_t lock_video_orientation;
2845
uint8_t rotation;
29-
int16_t window_x; // WINDOW_POSITION_UNDEFINED for "auto"
30-
int16_t window_y; // WINDOW_POSITION_UNDEFINED for "auto"
46+
int16_t window_x; // SC_WINDOW_POSITION_UNDEFINED for "auto"
47+
int16_t window_y; // SC_WINDOW_POSITION_UNDEFINED for "auto"
3148
uint16_t window_width;
3249
uint16_t window_height;
3350
uint16_t display_id;
@@ -55,7 +72,7 @@ struct scrcpy_options {
5572
.render_driver = NULL, \
5673
.codec_options = NULL, \
5774
.log_level = SC_LOG_LEVEL_INFO, \
58-
.record_format = RECORDER_FORMAT_AUTO, \
75+
.record_format = SC_RECORD_FORMAT_AUTO, \
5976
.port_range = { \
6077
.first = DEFAULT_LOCAL_PORT_RANGE_FIRST, \
6178
.last = DEFAULT_LOCAL_PORT_RANGE_LAST, \
@@ -65,8 +82,8 @@ struct scrcpy_options {
6582
.max_fps = 0, \
6683
.lock_video_orientation = DEFAULT_LOCK_VIDEO_ORIENTATION, \
6784
.rotation = 0, \
68-
.window_x = WINDOW_POSITION_UNDEFINED, \
69-
.window_y = WINDOW_POSITION_UNDEFINED, \
85+
.window_x = SC_WINDOW_POSITION_UNDEFINED, \
86+
.window_y = SC_WINDOW_POSITION_UNDEFINED, \
7087
.window_width = 0, \
7188
.window_height = 0, \
7289
.display_id = 0, \

app/src/screen.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "common.h"
99
#include "compat.h"
1010
#include "icon.xpm"
11+
#include "scrcpy.h"
1112
#include "tiny_xpm.h"
1213
#include "video_buffer.h"
1314
#include "util/lock.h"
@@ -257,9 +258,9 @@ screen_init_rendering(struct screen *screen, const char *window_title,
257258
window_flags |= SDL_WINDOW_BORDERLESS;
258259
}
259260

260-
int x = window_x != WINDOW_POSITION_UNDEFINED
261+
int x = window_x != SC_WINDOW_POSITION_UNDEFINED
261262
? window_x : (int) SDL_WINDOWPOS_UNDEFINED;
262-
int y = window_y != WINDOW_POSITION_UNDEFINED
263+
int y = window_y != SC_WINDOW_POSITION_UNDEFINED
263264
? window_y : (int) SDL_WINDOWPOS_UNDEFINED;
264265
screen->window = SDL_CreateWindow(window_title, x, y,
265266
window_size.width, window_size.height,

app/src/screen.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include "common.h"
1010
#include "opengl.h"
1111

12-
#define WINDOW_POSITION_UNDEFINED (-0x8000)
13-
1412
struct video_buffer;
1513

1614
struct screen {
@@ -76,7 +74,7 @@ void
7674
screen_init(struct screen *screen);
7775

7876
// initialize screen, create window, renderer and texture (window is hidden)
79-
// window_x and window_y accept WINDOW_POSITION_UNDEFINED
77+
// window_x and window_y accept SC_WINDOW_POSITION_UNDEFINED
8078
bool
8179
screen_init_rendering(struct screen *screen, const char *window_title,
8280
struct size frame_size, bool always_on_top,

app/src/server.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ listen_on_port(uint16_t port) {
143143

144144
static bool
145145
enable_tunnel_reverse_any_port(struct server *server,
146-
struct port_range port_range) {
146+
struct sc_port_range port_range) {
147147
uint16_t port = port_range.first;
148148
for (;;) {
149149
if (!enable_tunnel_reverse(server->serial, port)) {
@@ -189,7 +189,7 @@ enable_tunnel_reverse_any_port(struct server *server,
189189

190190
static bool
191191
enable_tunnel_forward_any_port(struct server *server,
192-
struct port_range port_range) {
192+
struct sc_port_range port_range) {
193193
server->tunnel_forward = true;
194194
uint16_t port = port_range.first;
195195
for (;;) {
@@ -217,7 +217,7 @@ enable_tunnel_forward_any_port(struct server *server,
217217
}
218218

219219
static bool
220-
enable_tunnel_any_port(struct server *server, struct port_range port_range,
220+
enable_tunnel_any_port(struct server *server, struct sc_port_range port_range,
221221
bool force_adb_forward) {
222222
if (!force_adb_forward) {
223223
// Attempt to use "adb reverse"

app/src/server.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "config.h"
1010
#include "command.h"
1111
#include "common.h"
12+
#include "scrcpy.h"
1213
#include "util/log.h"
1314
#include "util/net.h"
1415

@@ -20,7 +21,7 @@ struct server {
2021
socket_t server_socket; // only used if !tunnel_forward
2122
socket_t video_socket;
2223
socket_t control_socket;
23-
struct port_range port_range;
24+
struct sc_port_range port_range;
2425
uint16_t local_port; // selected from port_range
2526
bool tunnel_enabled;
2627
bool tunnel_forward; // use "adb forward" instead of "adb reverse"
@@ -47,7 +48,7 @@ struct server_params {
4748
enum sc_log_level log_level;
4849
const char *crop;
4950
const char *codec_options;
50-
struct port_range port_range;
51+
struct sc_port_range port_range;
5152
uint16_t max_size;
5253
uint32_t bit_rate;
5354
uint16_t max_fps;

app/src/util/log.h

-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33

44
#include <SDL2/SDL_log.h>
55

6-
enum sc_log_level {
7-
SC_LOG_LEVEL_DEBUG,
8-
SC_LOG_LEVEL_INFO,
9-
SC_LOG_LEVEL_WARN,
10-
SC_LOG_LEVEL_ERROR,
11-
};
12-
136
#define LOGV(...) SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
147
#define LOGD(...) SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
158
#define LOGI(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)

app/tests/test_cli.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <assert.h>
2+
#include <string.h>
23

34
#include "cli.h"
45
#include "common.h"
@@ -83,7 +84,7 @@ static void test_options(void) {
8384
assert(opts->port_range.last == 1236);
8485
assert(!strcmp(opts->push_target, "/sdcard/Movies"));
8586
assert(!strcmp(opts->record_filename, "file"));
86-
assert(opts->record_format == RECORDER_FORMAT_MKV);
87+
assert(opts->record_format == SC_RECORD_FORMAT_MKV);
8788
assert(opts->render_expired_frames);
8889
assert(!strcmp(opts->serial, "0123456789abcdef"));
8990
assert(opts->show_touches);
@@ -118,7 +119,7 @@ static void test_options2(void) {
118119
assert(!opts->control);
119120
assert(!opts->display);
120121
assert(!strcmp(opts->record_filename, "file.mp4"));
121-
assert(opts->record_format == RECORDER_FORMAT_MP4);
122+
assert(opts->record_format == SC_RECORD_FORMAT_MP4);
122123
}
123124

124125
int main(void) {

0 commit comments

Comments
 (0)