Skip to content

Commit

Permalink
Initialize server struct dynamically
Browse files Browse the repository at this point in the history
This will allow to add mutex/cond fields.
  • Loading branch information
rom1v committed Jan 1, 2021
1 parent ab498cc commit 5e303e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
28 changes: 15 additions & 13 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "util/log.h"
#include "util/net.h"

static struct server server = SERVER_INITIALIZER;
static struct server server;
static struct screen screen = SCREEN_INITIALIZER;
static struct fps_counter fps_counter;
static struct video_buffer video_buffer;
Expand Down Expand Up @@ -304,6 +304,18 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {

bool
scrcpy(const struct scrcpy_options *options) {
if (!server_init(&server)) {
return false;
}

bool fps_counter_initialized = false;
bool video_buffer_initialized = false;
bool file_handler_initialized = false;
bool recorder_initialized = false;
bool stream_started = false;
bool controller_initialized = false;
bool controller_started = false;

bool record = !!options->record_filename;
struct server_params params = {
.log_level = options->log_level,
Expand All @@ -322,19 +334,9 @@ scrcpy(const struct scrcpy_options *options) {
.force_adb_forward = options->force_adb_forward,
};
if (!server_start(&server, options->serial, &params)) {
return false;
goto end;
}

bool ret = false;

bool fps_counter_initialized = false;
bool video_buffer_initialized = false;
bool file_handler_initialized = false;
bool recorder_initialized = false;
bool stream_started = false;
bool controller_initialized = false;
bool controller_started = false;

if (!sdl_init_and_configure(options->display, options->render_driver,
options->disable_screensaver)) {
goto end;
Expand Down Expand Up @@ -444,7 +446,7 @@ scrcpy(const struct scrcpy_options *options) {

input_manager_init(&input_manager, options);

ret = event_loop(options);
bool ret = event_loop(options);
LOGD("quit...");

screen_destroy(&screen);
Expand Down
21 changes: 19 additions & 2 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,26 @@ close_socket(socket_t socket) {
}
}

void
bool
server_init(struct server *server) {
*server = (struct server) SERVER_INITIALIZER;
server->serial = NULL;
server->process = PROCESS_NONE;
server->wait_server_thread = NULL;
atomic_flag_clear_explicit(&server->server_socket_closed,
memory_order_relaxed);

server->server_socket = INVALID_SOCKET;
server->video_socket = INVALID_SOCKET;
server->control_socket = INVALID_SOCKET;

server->port_range.first = 0;
server->port_range.last = 0;
server->local_port = 0;

server->tunnel_enabled = false;
server->tunnel_forward = false;

return true;
}

static int
Expand Down
19 changes: 1 addition & 18 deletions app/src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,6 @@ struct server {
bool tunnel_forward; // use "adb forward" instead of "adb reverse"
};

#define SERVER_INITIALIZER { \
.serial = NULL, \
.process = PROCESS_NONE, \
.wait_server_thread = NULL, \
.server_socket_closed = ATOMIC_FLAG_INIT, \
.server_socket = INVALID_SOCKET, \
.video_socket = INVALID_SOCKET, \
.control_socket = INVALID_SOCKET, \
.port_range = { \
.first = 0, \
.last = 0, \
}, \
.local_port = 0, \
.tunnel_enabled = false, \
.tunnel_forward = false, \
}

struct server_params {
enum sc_log_level log_level;
const char *crop;
Expand All @@ -62,7 +45,7 @@ struct server_params {
};

// init default values
void
bool
server_init(struct server *server);

// push, enable tunnel et start the server
Expand Down

0 comments on commit 5e303e1

Please sign in to comment.