Skip to content

Commit

Permalink
Merge pull request #232 from mariotaku/fix/libgs-segfault
Browse files Browse the repository at this point in the history
Fix/libgs segfault
  • Loading branch information
mariotaku authored Jun 17, 2023
2 parents 6842131 + 6935686 commit 4a9cc43
Show file tree
Hide file tree
Showing 35 changed files with 442 additions and 306 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: recursive
fetch-depth: 0

- name: Update packages
run: sudo apt-get -yq update
Expand Down Expand Up @@ -52,6 +53,7 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: recursive
fetch-depth: 0

- name: Download ares-cli-rs
uses: robinraju/release-downloader@v1.8
Expand Down Expand Up @@ -97,6 +99,7 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: recursive
fetch-depth: 0

- name: Update Packages
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release
BUILD_TYPE: RelWithDebInfo

jobs:
release-webos:
Expand Down
4 changes: 2 additions & 2 deletions app/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern pcmanager_t *pcmanager;

typedef struct app_t {
SDL_Window *window;
SDL_threadID main_thread_id;
os_info_t os_info;
app_input_t input;
app_backend_t backend;
Expand Down Expand Up @@ -69,11 +70,10 @@ bool app_get_mouse_relative();

void app_set_keep_awake(bool);


void app_set_fullscreen(app_t*app, bool);

void app_open_url(const char *url);

void app_init_locale();

const char *app_get_locale_lang();
const char *app_get_locale_lang();
2 changes: 1 addition & 1 deletion app/backend/apploader/apploader.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static int task_run(apploader_task_t *task) {
PAPP_LIST ll = NULL;
client = app_gs_client_new();
if ((ret = gs_applist(client, node->server, &ll)) != GS_OK) {
error = gs_error;
gs_get_error(&error);
goto finish;
}
if (ll == NULL) {
Expand Down
9 changes: 5 additions & 4 deletions app/backend/pcmanager/listeners.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef struct pcmanager_listener_list {
static int pcmanager_callbacks_comparator(pcmanager_listener_list *p1, const void *p2);

void pcmanager_listeners_notify(pcmanager_t *manager, const uuidstr_t *uuid, pcmanager_notify_type_t type) {
assert(manager != NULL);
assert(SDL_ThreadID() == manager->thread_id);
for (pcmanager_listener_list *cur = manager->listeners; cur != NULL;) {
pcmanager_listener_list *next = cur->next;
Expand All @@ -52,8 +53,8 @@ void pcmanager_listeners_notify(pcmanager_t *manager, const uuidstr_t *uuid, pcm
}

void pcmanager_register_listener(pcmanager_t *manager, const pcmanager_listener_t *listener, void *userdata) {
assert(manager);
assert(listener);
assert(manager != NULL);
assert(listener != NULL);
assert(SDL_ThreadID() == manager->thread_id);
pcmanager_listener_list *node = listeners_new();
node->listener = listener;
Expand All @@ -62,8 +63,8 @@ void pcmanager_register_listener(pcmanager_t *manager, const pcmanager_listener_
}

void pcmanager_unregister_listener(pcmanager_t *manager, const pcmanager_listener_t *listener) {
assert(manager);
assert(listener);
assert(manager != NULL);
assert(listener != NULL);
assert(SDL_ThreadID() == manager->thread_id);
pcmanager_listener_list *node = listeners_find_by(manager->listeners, listener, pcmanager_callbacks_comparator);
if (!node)
Expand Down
6 changes: 3 additions & 3 deletions app/backend/pcmanager/pairing.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ static int pin_random(int min, int max);

bool pcmanager_pair(pcmanager_t *manager, const uuidstr_t *uuid, char *pin, pcmanager_callback_t callback,
void *userdata) {
SDL_assert(manager != NULL);
SDL_assert(uuid != NULL);
SDL_assert(pin != NULL);
SDL_assert_release(manager != NULL);
SDL_assert_release(uuid != NULL);
SDL_assert_release(pin != NULL);
const pclist_t *node = pcmanager_node(manager, uuid);
if (node == NULL) {
return false;
Expand Down
18 changes: 9 additions & 9 deletions app/backend/pcmanager/pclist.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,33 +146,33 @@ void pclist_ll_nodefree(pclist_t *node) {


pclist_t *pclist_find_by_ip(pcmanager_t *manager, const char *ip) {
SDL_assert(ip != NULL);
SDL_assert_release(ip != NULL);
pclist_lock(manager);
pclist_t *result = pclist_ll_find_by(manager->servers, ip, pclist_ll_compare_address);
pclist_unlock(manager);
return result;
}

pclist_t *pclist_find_by_uuid(pcmanager_t *manager, const uuidstr_t *uuid) {
SDL_assert(uuid);
SDL_assert_release(uuid != NULL);
pclist_lock(manager);
pclist_t *result = pclist_ll_find_by(manager->servers, uuid, pclist_ll_compare_uuid);
pclist_unlock(manager);
return result;
}

static int pclist_ll_compare_address(pclist_t *other, const void *v) {
SDL_assert(v);
SDL_assert(other);
SDL_assert(other->server);
SDL_assert(other->server->serverInfo.address);
SDL_assert_release(v);
SDL_assert_release(other);
SDL_assert_release(other->server);
SDL_assert_release(other->server->serverInfo.address);
return SDL_strcmp(other->server->serverInfo.address, (const char *) v);
}

static int pclist_ll_compare_uuid(pclist_t *other, const void *v) {
SDL_assert(v);
SDL_assert(other);
SDL_assert(other->server);
SDL_assert_release(v);
SDL_assert_release(other);
SDL_assert_release(other->server);
return !uuidstr_t_equals_t(&other->id, (const uuidstr_t *) v);
}

Expand Down
2 changes: 1 addition & 1 deletion app/backend/pcmanager/pcmanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int pcmanager_server_current_app(pcmanager_t *manager, const uuidstr_t *uuid) {
}

int pcmanager_node_current_app(const pclist_t *node) {
SDL_assert(node != NULL);
SDL_assert_release(node != NULL);
return node->server->currentGame;
}

Expand Down
2 changes: 2 additions & 0 deletions app/backend/pcmanager/worker/pairing.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ int worker_pairing(worker_context_t *context) {
int ret = gs_pair(client, server, context->arg1);
gs_destroy(client);
if (ret != GS_OK) {
const char *gs_error = NULL;
gs_get_error(&gs_error);
context->error = gs_error != NULL ? strdup(gs_error) : NULL;
serverdata_free(server);
return ret;
Expand Down
2 changes: 2 additions & 0 deletions app/backend/pcmanager/worker/quit_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ int worker_quit_app(worker_context_t *context) {
SERVER_STATE state = {.code = SERVER_STATE_AVAILABLE};
pclist_upsert(context->manager, &context->uuid, &state, node->server);
} else {
const char *gs_error = NULL;
gs_get_error(&gs_error);
context->error = gs_error != NULL ? strdup(gs_error) : NULL;
}
return ret;
Expand Down
14 changes: 8 additions & 6 deletions app/backend/pcmanager/worker/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "util/bus.h"
#include "app.h"
#include "logging.h"
#include "ui/fatal_error.h"

static void notify_querying(pclist_update_context_t *args);

Expand All @@ -16,13 +17,12 @@ int worker_host_update(worker_context_t *context) {
}

int pcmanager_update_by_ip(worker_context_t *context, const char *ip, bool force) {
SDL_assert(context != NULL);
SDL_assert(context->manager != NULL);
SDL_assert(ip != NULL);
SDL_assert_release(context != NULL);
SDL_assert_release(context->manager != NULL);
SDL_assert_release(ip != NULL);
pcmanager_t *manager = context->manager;
char *ip_dup = strdup(ip);
pclist_t *existing = pclist_find_by_ip(manager, ip_dup);
int ret = 0;
if (existing) {
SERVER_STATE_ENUM state = existing->state.code;
if (state == SERVER_STATE_QUERYING) {
Expand All @@ -42,9 +42,9 @@ int pcmanager_update_by_ip(worker_context_t *context, const char *ip, bool force
pclist_unlock(manager);
bus_pushaction_sync((bus_actionfunc) notify_querying, &args);
}
PSERVER_DATA server = serverdata_new();
GS_CLIENT client = app_gs_client_new();
ret = gs_init(client, server, ip_dup, app_configuration->unsupported);
PSERVER_DATA server = serverdata_new();
int ret = gs_init(client, server, ip_dup, app_configuration->unsupported);
ip_dup = NULL;
gs_destroy(client);
if (existing) {
Expand All @@ -66,6 +66,8 @@ int pcmanager_update_by_ip(worker_context_t *context, const char *ip, bool force
SERVER_STATE state = {.code = server->paired ? SERVER_STATE_AVAILABLE : SERVER_STATE_NOT_PAIRED};
pclist_upsert(manager, &uuid, &state, server);
} else {
const char *gs_error = NULL;
ret = gs_get_error(&gs_error);
context->error = gs_error != NULL ? strdup(gs_error) : NULL;
if (existing && ret == GS_IO_ERROR) {
commons_log_warn("PCManager", "IO error while updating status from %s: %s", server->serverInfo.address,
Expand Down
50 changes: 44 additions & 6 deletions app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "ss4s.h"
#include "input/app_input.h"
#include "errors.h"
#include "ui/fatal_error.h"

#include <SDL_image.h>

Expand All @@ -43,14 +45,23 @@ SDL_Window *app_create_window();

static void log_libs_version();

static SDL_AssertState app_assertion_handler_abort(const SDL_AssertData *data, void *userdata);

static SDL_AssertState app_assertion_handler_ui(const SDL_AssertData *data, void *userdata);

static app_t *app = NULL;

int main(int argc, char *argv[]) {
commons_logging_init("moonlight");
SDL_LogSetOutputFunction(commons_sdl_log, NULL);
SDL_SetAssertionHandler(app_assertion_handler_abort, NULL);
commons_log_info("APP", "Start Moonlight. Version %s", APP_VERSION);
log_libs_version();
app_gs_client_mutex = SDL_CreateMutex();

app_t app_;
app_t app_ = {
.main_thread_id = SDL_ThreadID()
};
int ret = app_init(&app_, argc, argv);
if (ret != 0) {
return ret;
Expand All @@ -64,6 +75,8 @@ int main(int argc, char *argv[]) {

app_.window = app_create_window();

app = &app_;

SS4S_PostInit(argc, argv);

app_handle_launch(argc, argv);
Expand Down Expand Up @@ -94,19 +107,24 @@ int main(int argc, char *argv[]) {
lv_group_set_default(group);
app_input_init(&app_.input, &app_);

SDL_SetAssertionHandler(app_assertion_handler_ui, &app_);

lv_obj_t *scr = lv_scr_act();
lv_obj_clear_flag(scr, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_style_bg_opa(scr, 0, 0);
app_uimanager = lv_fragment_manager_create(NULL);
lv_fragment_t *fragment = lv_fragment_create(&launcher_controller_class, &app_);
lv_fragment_manager_push(app_uimanager, fragment, &scr);


while (app_is_running()) {
app_process_events();
lv_task_handler();
SDL_Delay(1);
}

SDL_SetAssertionHandler(NULL, NULL);

lv_fragment_manager_del(app_uimanager);

app_input_deinit(&app_.input);
Expand Down Expand Up @@ -160,7 +178,7 @@ SDL_Window *app_create_window() {
win_height = app_configuration->window_state.h;
}
SDL_Window *win = SDL_CreateWindow("Moonlight", win_x, win_y, win_width, win_height, win_flags);
SDL_assert(win != NULL);
SDL_assert_release(win != NULL);

SDL_Surface *winicon = IMG_Load_RW(SDL_RWFromConstMem(lv_sdl_img_data_logo_96.data.constptr,
(int) lv_sdl_img_data_logo_96.data_len), SDL_TRUE);
Expand All @@ -169,7 +187,7 @@ SDL_Window *app_create_window() {
SDL_SetWindowMinimumSize(win, 640, 480);
int w = 0, h = 0;
SDL_GetWindowSize(win, &w, &h);
SDL_assert(w > 0 && h > 0);
SDL_assert_release(w > 0 && h > 0);
ui_display_size(w, h);
return win;
}
Expand All @@ -184,12 +202,18 @@ bool app_is_running() {

GS_CLIENT app_gs_client_new() {
SDL_LockMutex(app_gs_client_mutex);
SDL_assert(app_configuration);
SDL_assert_release(app_configuration);
GS_CLIENT client = gs_new(app_configuration->key_dir, app_configuration->debug_level);
if (client == NULL) {
commons_log_fatal("APP", "Failed to create GameStream client: %s", gs_error);
const char *message = NULL;
gs_get_error(&message);
app_fatal_error("Fatal error", "Failed to create GS_CLIENT: %s", message);
if (SDL_ThreadID() == app->main_thread_id) {
abort();
} else {
app_halt();
}
}
SDL_assert(client);
SDL_UnlockMutex(app_gs_client_mutex);
return client;
}
Expand All @@ -206,3 +230,17 @@ static void log_libs_version() {
const SDL_version *img_version = IMG_Linked_Version();
commons_log_debug("APP", "SDL_image version: %d.%d.%d", img_version->major, img_version->minor, img_version->patch);
}

static SDL_AssertState app_assertion_handler_abort(const SDL_AssertData *data, void *userdata) {
(void) userdata;
commons_log_fatal("Assertion", "at %s (%s:%d): '%s'", data->function, data->filename, data->linenum,
data->condition);
return SDL_ASSERTION_ABORT;
}

static SDL_AssertState app_assertion_handler_ui(const SDL_AssertData *data, void *userdata) {
(void) userdata;
app_fatal_error("Assertion failure", "at %s\n(%s:%d): '%s'", data->function, data->filename, data->linenum,
data->condition);
return SDL_ASSERTION_ALWAYS_IGNORE;
}
3 changes: 2 additions & 1 deletion app/platform/sdl/bus.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "util/bus.h"

#include <SDL.h>
#include <assert.h>

typedef struct bus_blocking_action_t {
bus_actionfunc action;
Expand All @@ -23,7 +24,7 @@ bool bus_pushevent(int which, void *data1, void *data2) {
}

bool bus_pushaction(bus_actionfunc action, void *data) {
SDL_assert(action);
assert(action != NULL);
return bus_pushevent(BUS_INT_EVENT_ACTION, action, data);
}

Expand Down
2 changes: 1 addition & 1 deletion app/platform/sdl/path_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ char *path_cache() {
static char *path_cache_parent() {
#ifdef __WIN32
char *appdata = SDL_getenv("LOCALAPPDATA");
SDL_assert(appdata);
SDL_assert_release(appdata);
char *appdir = path_join(appdata, "moonlight-tv");
path_dir_ensure(appdir);
char *cachedir = path_join(appdir, "cache");
Expand Down
4 changes: 2 additions & 2 deletions app/stream/input/sdl/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static bool isSystemKeyCaptureActive() {

void performPendingSpecialKeyCombo() {
// The caller must ensure all keys are up
SDL_assert(keys_len(_pressed_keys) == 0);
SDL_assert_release(keys_len(_pressed_keys) == 0);

switch (_pending_key_combo) {
case KeyComboQuit:
Expand Down Expand Up @@ -177,7 +177,7 @@ void sdlinput_handle_key_event(SDL_KeyboardEvent *event) {

if (event->repeat) {
// Ignore repeat key down events
SDL_assert(event->state == SDL_PRESSED);
SDL_assert_release(event->state == SDL_PRESSED);
return;
}

Expand Down
Loading

0 comments on commit 4a9cc43

Please sign in to comment.