Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Commit

Permalink
More overhauls to get things working
Browse files Browse the repository at this point in the history
  • Loading branch information
kbhomes committed Jan 20, 2019
1 parent 6e8a26e commit dd53ba6
Show file tree
Hide file tree
Showing 34 changed files with 392 additions and 110 deletions.
2 changes: 1 addition & 1 deletion libgamestream/http.c
Expand Up @@ -40,7 +40,7 @@ static const char *pKeyFile = "./key.pem";

SSL_CTX *ssl_ctx;

static bool debug = true;
static bool debug = false;

/**
* Workaround for issues stemming from SSL.
Expand Down
20 changes: 12 additions & 8 deletions src/application.cpp
Expand Up @@ -40,7 +40,7 @@ Application::Application() :
config_parse(MOONLIGHT_DATA_DIR "moonlight.ini", &config_);
config_.debug_level = 2;

push_state(new UiStateInitial(this));
pushState(new UiStateInitial(this));
}

Application::~Application() {
Expand Down Expand Up @@ -69,15 +69,20 @@ void Application::start() {
UiState *state = ui_states_.back();
UiStateResult state_result = state->update(input);

if (state_result == UiStateResultNormal) {
if (state_result.type == UiStateResultType::PopState || state_result.type == UiStateResultType::ReplaceState) {
popState();
}

if (state_result.type == UiStateResultType::PushState || state_result.type == UiStateResultType::ReplaceState) {
pushState(state_result.state);
}

if (state_result.type == UiStateResultType::Normal) {
SDL_SetRenderDrawColor(ui()->renderer, 0xeb, 0xeb, 0xeb, 0xff);
SDL_RenderClear(ui()->renderer);
state->render();
SDL_RenderPresent(ui()->renderer);
}
else if (state_result == UiStateResultExit) {
pop_state();
}
}
}

Expand All @@ -93,17 +98,16 @@ SUI *Application::ui() {
return ui_;
}

void Application::push_state(UiState *state) {
void Application::pushState(UiState *state) {
// Notify the new state that it is being entered
UiState *parent = ui_states_.size() ? ui_states_.back() : nullptr;
state->enter(parent);
ui_states_.push_back(state);
}

void Application::pop_state() {
void Application::popState() {
// Notify the current state that it is being exited
UiState *state = ui_states_.back();

state->exit();
ui_states_.pop_back();
delete state;
Expand Down
9 changes: 7 additions & 2 deletions src/application.h
@@ -1,5 +1,6 @@
#pragma once

#include "util.h"
#include "server.h"
#include "promise.h"
#include "ui/ui-state.h"
Expand All @@ -24,8 +25,10 @@ class Application

Server *server();
SUI *ui();
void push_state(UiState *state);
void pop_state();
void pushState(UiState *state);
void popState();

void log(const char *format, ...);

private:
CONFIGURATION config_;
Expand All @@ -35,4 +38,6 @@ class Application
SUI *ui_;
std::vector<UiState *> ui_states_;
bool ui_should_exit_;

Mutex log_mutex_;
};
2 changes: 2 additions & 0 deletions src/main.cpp
Expand Up @@ -19,6 +19,7 @@

#include <switch.h>

#include "util.h"
#include "application.h"
//#include "configuration.h"
//#include "config.h"
Expand Down Expand Up @@ -54,6 +55,7 @@


int main(int argc, char* argv[]) {
logInit(true);

auto application = std::make_unique<Application>();
application->start();
Expand Down
7 changes: 7 additions & 0 deletions src/promise.h
@@ -1,5 +1,6 @@
#pragma once

#include "util.h"
#include <switch.h>
#include <stdio.h>
#include <optional>
Expand All @@ -18,12 +19,16 @@ class promise {
}

inline bool get() {
logPrint("promise.get\n");

int index;
Result rc = waitMulti(&index, -1,
waiterForUEvent(&resolve_event_),
waiterForUEvent(&reject_event_)
);

logPrint("promise.get: wait completed\n");

if (R_SUCCEEDED(rc)) {
if (index == 0) {
return true;
Expand Down Expand Up @@ -54,12 +59,14 @@ class promise {

inline void resolve(TResolve value) {
// Signal that this ptromise was resolved
logPrint("promise.resolve\n");
resolve_value_ = value;
ueventSignal(&resolve_event_);
};

inline void reject(TReject value) {
// Signal that this promise was rejected
logPrint("promise.reject\n");
reject_value_ = value;
ueventSignal(&reject_event_);
};
Expand Down
16 changes: 11 additions & 5 deletions src/server.cpp
Expand Up @@ -89,12 +89,16 @@ bool Server::paired() {
promise<bool, ServerError> *Server::connect() {
// Tell the worker to initiate the connection to the server
ueventSignal(&event_connect_);

logPrint("Did signal connect\n");
return connect_promise_;
}

promise<bool, ServerError> *Server::pair() {
// Tell the worker to initiate server pairing
ueventSignal(&event_pair_);

logPrint("Did signal pair\n");
return pair_promise_;
}

Expand All @@ -117,7 +121,7 @@ CONNECTION_LISTENER_CALLBACKS Server::getCallbacks() {
}

void Server::thread_() {
printf("Server thread started\n");
logPrint("Server thread started\n");

Result rc;
int index = -1;
Expand All @@ -132,7 +136,7 @@ void Server::thread_() {
waiterForUEvent(&event_close_));

if (R_SUCCEEDED(rc)) {
printf("Got index of thread event: %d\n", index);
logPrint("Got index of thread event: %d\n", index);

switch (index) {
case 0: threadConnect_(); break;
Expand All @@ -151,26 +155,28 @@ void Server::thread_() {
}

void Server::threadConnect_() {
printf("[server] connect\n");
int ret = gs_init(&server_, config_->address, MOONLIGHT_DATA_DIR "key", config_->debug_level, config_->unsupported);

if (ret == GS_OK) {
printf("[server] connect.resolve\n");
connect_promise_->resolve(true);
}
else {
printf("[server] connect.reject\n");
connect_promise_->reject(ServerError(ret, gs_error));
}
}

void Server::threadPair_() {
logPrint("Before gs_pair\n");

int ret = gs_pair(&server_, &pin_[0]);
logPrint("Got gs_pair result: %d\n", ret);

if (ret == GS_OK) {
logPrint("Resolving gs_pair\n");
pair_promise_->resolve(true);
}
else {
logPrint("[thread:%ld] Rejecting gs_pair\n");
pair_promise_->reject(ServerError(ret, gs_error));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Expand Up @@ -21,6 +21,7 @@

#include <switch.h>

#include "util.h"
#include "promise.h"

extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/state-connecting.c
Expand Up @@ -72,7 +72,7 @@
// sui_draw_top_header("Moonlight › Connection");

// // Draw the OK and Back actions on the bottom toolbar
// sui_draw_bottom_toolbar(2, "OK", SUIToolbarActionA, "Back", SUIToolbarActionB);
// sui_draw_bottom_toolbar(2, "OK", SUIToolbarAction::A, "Back", SUIToolbarAction::B);

// ui_draw_fps(&props.counter);

Expand Down
2 changes: 1 addition & 1 deletion src/ui/state-connection-failed.c
Expand Up @@ -60,7 +60,7 @@
// sui_draw_top_header("Moonlight › Connection");

// // Draw the OK and Back actions on the bottom toolbar
// sui_draw_bottom_toolbar(2, "OK", SUIToolbarActionA, "Back", SUIToolbarActionB);
// sui_draw_bottom_toolbar(2, "OK", SUIToolbarAction::A, "Back", SUIToolbarAction::B);

// ui_draw_fps(&props.counter);

Expand Down
2 changes: 1 addition & 1 deletion src/ui/state-games-list.c
Expand Up @@ -205,7 +205,7 @@
// SDL_RenderClear(ui.renderer);

// sui_draw_top_header("Moonlight › Games");
// sui_draw_bottom_toolbar(2, "OK", SUIToolbarActionA, "Back", SUIToolbarActionB);
// sui_draw_bottom_toolbar(2, "OK", SUIToolbarAction::A, "Back", SUIToolbarAction::B);

// sui_scene_render(&props.gamesListScene);

Expand Down
2 changes: 1 addition & 1 deletion src/ui/state-initial.c
Expand Up @@ -65,7 +65,7 @@
// sui_draw_texture(props.logoTexture, (ui.width - props.logoWidth) / 2, 150, props.logoWidth, props.logoHeight);

// // Draw the OK action on the bottom toolbar
// sui_draw_bottom_toolbar(1, "OK", SUIToolbarActionA);
// sui_draw_bottom_toolbar(1, "OK", SUIToolbarAction::A);

// // Draw the main buttons
// sui_button_set_render(&props.buttons);
Expand Down
2 changes: 1 addition & 1 deletion src/ui/state-settings.c
Expand Up @@ -37,7 +37,7 @@
// sui_draw_top_header("Moonlight › Settings");

// // Draw the OK and Back actions on the bottom toolbar
// sui_draw_bottom_toolbar(2, "OK", SUIToolbarActionA, "Back", SUIToolbarActionB);
// sui_draw_bottom_toolbar(2, "OK", SUIToolbarAction::A, "Back", SUIToolbarAction::B);

// ui_draw_fps(&props.counter);

Expand Down
8 changes: 3 additions & 5 deletions src/ui/switch/sui-button.cpp
Expand Up @@ -22,7 +22,7 @@ SUIButton::~SUIButton() {

void SUIButton::update(SUIInput *input) {
if (isFocused() && input->buttons.down & KEY_A) {
triggerListener(SUIEventClick);
triggerListener(SUIEvent::Click);
}
}

Expand Down Expand Up @@ -58,11 +58,9 @@ void SUIButton::renderContent() {
uint32_t text_color = isFocused() ? SUI_BUTTON_FOCUSED_TEXT_COLOR : SUI_BUTTON_TEXT_COLOR;
graphics()->drawText(ui()->font_normal,
text_,
bounds_.w / 2,
bounds_.h / 2,
{0, 0, bounds().w, bounds().h},
text_color,
true,
-1);
true);
}

bool SUIButton::isFocusable() {
Expand Down
12 changes: 6 additions & 6 deletions src/ui/switch/sui-common.h
Expand Up @@ -20,14 +20,14 @@

typedef SDL_Rect SUIRect;

enum SUIFocusResult {
SUIFocusRetain,
SUIFocusRelease
enum class SUIFocusResult {
Retain,
Release
};

enum SUIEvent {
SUIEventClick,
SUIEventFocus
enum class SUIEvent {
Click,
Focus
};

class SUIElement;
Expand Down
8 changes: 5 additions & 3 deletions src/ui/switch/sui-container.cpp
Expand Up @@ -29,7 +29,9 @@ void SUIContainer::update(SUIInput *input) {

void SUIContainer::render() {
for (auto child : children_) {
child->render();
if (child->isVisible()) {
child->render();
}
}
}

Expand Down Expand Up @@ -70,10 +72,10 @@ SUIFocusResult SUIContainer::updateFocus(SUIInput *input, SUIElement *previous)

if (next_candidate) {
stage()->setFocusedElement(next_candidate);
return SUIFocusRetain;
return SUIFocusResult::Retain;
}
else {
return SUIFocusRelease;
return SUIFocusResult::Release;
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/ui/switch/sui-element.cpp
Expand Up @@ -6,7 +6,8 @@
SUIElement::SUIElement(std::string name)
: name_(name),
parent_(nullptr),
bounds_()
bounds_(),
visible_(true)
{
graphics_ = new SUIGraphics(this);
}
Expand Down Expand Up @@ -39,6 +40,14 @@ SUIElement *SUIElement::acceptFocus() {
return isFocusable() ? this : nullptr;
}

bool SUIElement::isVisible() {
return visible_;
}

void SUIElement::setVisible(bool value) {
visible_ = value;
}

void SUIElement::triggerListener(SUIEvent event) {
auto event_range = listeners_.equal_range(event);
for (auto it = event_range.first; it != event_range.second; it++) {
Expand Down
5 changes: 4 additions & 1 deletion src/ui/switch/sui-element.h
Expand Up @@ -30,6 +30,9 @@ class SUIElement {
bool isFocused();
virtual SUIElement *acceptFocus();

bool isVisible();
void setVisible(bool value);

void triggerListener(SUIEvent event);
void addListener(SUIEvent event, SUIEventListener listener);

Expand All @@ -54,6 +57,6 @@ class SUIElement {

std::multimap<SUIEvent, SUIEventListener> listeners_;

// Focus set capabilities
bool visible_;
bool focusable_;
};
2 changes: 1 addition & 1 deletion src/ui/switch/sui-fps-counter.cpp
Expand Up @@ -50,7 +50,7 @@ void SUIFpsCounter::render() {
graphics()->measureText(ui_->font_small, fps_text, &fps_text_width, &fps_text_height);

graphics()->drawBox(8, 8, 10 + fps_text_width, fps_text_height + 2, RGBA8(255, 255, 255, 200));
graphics()->drawText(ui_->font_small, fps_text, 18, 10, fps_color, false, -1);
graphics()->drawText(ui()->font_small, fps_text, 18, 10, fps_color, false, -1);

short progressDigit = frame_ % fps_text_height;
graphics()->drawBox(10, 10, 2, progressDigit, fps_color);
Expand Down

0 comments on commit dd53ba6

Please sign in to comment.