Add Nintendo Switch Borealis GUI (#349)

This commit is contained in:
H0neyBadger 2020-10-25 10:51:49 +01:00 committed by GitHub
commit f7c83e8416
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 3470 additions and 124 deletions

View file

@ -0,0 +1,61 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_DISCOVERYMANAGER_H
#define CHIAKI_DISCOVERYMANAGER_H
#include <map>
#include <string>
#include <chiaki/log.h>
#include <chiaki/discoveryservice.h>
#include "host.h"
#include "settings.h"
static void Discovery(ChiakiDiscoveryHost*, void*);
class DiscoveryManager
{
private:
Settings * settings;
ChiakiLog * log;
ChiakiDiscoveryService service;
ChiakiDiscovery discovery;
struct sockaddr * host_addr;
size_t host_addr_len;
uint32_t GetIPv4BroadcastAddr();
bool service_enable;
public:
typedef enum hoststate
{
UNKNOWN,
READY,
STANDBY,
SHUTDOWN,
} HostState;
DiscoveryManager(Settings *settings);
~DiscoveryManager();
void SetService(bool);
int Send();
int Send(struct sockaddr *host_addr, size_t host_addr_len);
int Send(const char *);
void DiscoveryCB(ChiakiDiscoveryHost*);
};
#endif //CHIAKI_DISCOVERYMANAGER_H

View file

@ -0,0 +1,35 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_EXCEPTION_H
#define CHIAKI_EXCEPTION_H
#include <exception>
#include <string>
class Exception : public std::exception
{
private:
const char *msg;
public:
explicit Exception(const char *msg) : msg(msg) {}
const char *what() const noexcept override { return msg; }
};
#endif // CHIAKI_EXCEPTION_H

102
switch/include/gui.h Normal file
View file

@ -0,0 +1,102 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_GUI_H
#define CHIAKI_GUI_H
#include <glad.h>
#include <GLFW/glfw3.h>
#include "nanovg.h"
#include "nanovg_gl.h"
#include "nanovg_gl_utils.h"
#include <borealis.hpp>
#include <thread>
#include <map>
#include "host.h"
#include "settings.h"
#include "discoverymanager.h"
#include "io.h"
class HostInterface
{
private:
brls::TabFrame * root;
IO * io;
Host * host;
Settings * settings;
brls::List * hostList;
bool connected = false;
public:
HostInterface(brls::List * hostList, IO * io, Host * host, Settings * settings);
~HostInterface();
void Register(bool pin_incorrect);
void Wakeup(brls::View * view);
void Connect(brls::View * view);
void ConnectSession();
void Disconnect();
bool Stream();
bool CloseStream(ChiakiQuitEvent * quit);
};
class MainApplication
{
private:
ChiakiLog * log;
std::map<std::string, Host> * hosts;
Settings * settings;
DiscoveryManager * discoverymanager;
IO * io;
brls::TabFrame * rootFrame;
std::map<Host *, brls::List *> host_menuitems;
bool BuildConfigurationMenu(brls::List *, Host * host = nullptr);
public:
MainApplication(std::map<std::string, Host> * hosts,
Settings * settings, DiscoveryManager * discoverymanager,
IO * io, ChiakiLog * log);
~MainApplication();
bool Load();
};
class PS4RemotePlay : public brls::View
{
private:
brls::AppletFrame * frame;
// to display stream on screen
IO * io;
// to send gamepad inputs
Host * host;
brls::Label * label;
ChiakiControllerState state = { 0 };
// FPS calculation
// double base_time;
// int frame_counter = 0;
// int fps = 0;
public:
PS4RemotePlay(IO * io, Host * host);
~PS4RemotePlay();
void draw(NVGcontext * vg, int x, int y, unsigned width, unsigned height, brls::Style * style, brls::FrameContext * ctx) override;
};
#endif // CHIAKI_GUI_H

129
switch/include/host.h Normal file
View file

@ -0,0 +1,129 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_HOST_H
#define CHIAKI_HOST_H
#include <string>
#include <map>
#include <netinet/in.h>
#include <chiaki/log.h>
#include <chiaki/regist.h>
#include <chiaki/discovery.h>
#include <chiaki/opusdecoder.h>
#include <chiaki/controller.h>
#include "exception.h"
#include "io.h"
#include "settings.h"
class DiscoveryManager;
static void Discovery(ChiakiDiscoveryHost*, void *);
static void InitAudioCB(unsigned int channels, unsigned int rate, void * user);
static bool VideoCB(uint8_t * buf, size_t buf_size, void * user);
static void AudioCB(int16_t * buf, size_t samples_count, void * user);
static void RegistEventCB(ChiakiRegistEvent * event, void * user);
static void EventCB(ChiakiEvent * event, void * user);
enum RegistError
{
HOST_REGISTER_OK,
HOST_REGISTER_ERROR_SETTING_PSNACCOUNTID,
HOST_REGISTER_ERROR_SETTING_PSNONLINEID
};
class Settings;
class Host
{
private:
ChiakiLog * log = nullptr;
Settings * settings = nullptr;
//video config
ChiakiVideoResolutionPreset video_resolution = CHIAKI_VIDEO_RESOLUTION_PRESET_720p;
ChiakiVideoFPSPreset video_fps = CHIAKI_VIDEO_FPS_PRESET_60;
// info from discovery manager
int device_discovery_protocol_version = 0;
std::string host_type;
// user info
std::string psn_online_id = "";
std::string psn_account_id = "";
// info from regist/settings
ChiakiRegist regist = {};
ChiakiRegistInfo regist_info = {};
std::function<void()> chiaki_regist_event_type_finished_canceled = nullptr;
std::function<void()> chiaki_regist_event_type_finished_failed = nullptr;
std::function<void()> chiaki_regist_event_type_finished_success = nullptr;
std::string ap_ssid;
std::string ap_bssid;
std::string ap_key;
std::string ap_name;
std::string ps4_nickname;
// mac address = 48 bits
uint8_t ps4_mac[6] = {0};
char rp_regist_key[CHIAKI_SESSION_AUTH_SIZE] = {0};
uint32_t rp_key_type = 0;
uint8_t rp_key[0x10] = {0};
// manage stream session
bool session_init = false;
ChiakiSession session;
ChiakiOpusDecoder opus_decoder;
ChiakiConnectVideoProfile video_profile;
ChiakiControllerState keyboard_state;
friend class DiscoveryManager;
friend class Settings;
public:
// internal state
ChiakiDiscoveryHostState state = CHIAKI_DISCOVERY_HOST_STATE_UNKNOWN;
bool discovered = false;
bool registered = false;
// rp_key_data is true when rp_key, rp_regist_key, rp_key_type
bool rp_key_data = false;
std::string host_name;
int system_version = 0;
// sony's host_id == mac addr without colon
std::string host_id;
std::string host_addr;
Host(ChiakiLog * log, Settings * settings, std::string host_name);
~Host();
bool GetVideoResolution(int * ret_width, int * ret_height);
int Register(std::string pin);
int Wakeup();
int InitSession(IO *);
int FiniSession();
void StopSession();
void StartSession();
void SendFeedbackState(ChiakiControllerState*);
void RegistCB(ChiakiRegistEvent *);
void SetRegistEventTypeFinishedCanceled(std::function<void()> chiaki_regist_event_type_finished_canceled)
{
this->chiaki_regist_event_type_finished_canceled = chiaki_regist_event_type_finished_canceled;
};
void SetRegistEventTypeFinishedFailed(std::function<void()> chiaki_regist_event_type_finished_failed)
{
this->chiaki_regist_event_type_finished_failed = chiaki_regist_event_type_finished_failed;
};
void SetRegistEventTypeFinishedSuccess(std::function<void()> chiaki_regist_event_type_finished_success)
{
this->chiaki_regist_event_type_finished_success = chiaki_regist_event_type_finished_success;
};
};
#endif

136
switch/include/io.h Normal file
View file

@ -0,0 +1,136 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_IO_H
#define CHIAKI_IO_H
#include <functional>
#include <cstdint>
#include <SDL2/SDL.h>
#include <glad.h> // glad library (OpenGL loader)
#include <chiaki/session.h>
/*
https://github.com/devkitPro/switch-glad/blob/master/include/glad/glad.h
https://glad.dav1d.de/#profile=core&language=c&specification=gl&api=gl%3D4.3&extensions=GL_EXT_texture_compression_s3tc&extensions=GL_EXT_texture_filter_anisotropic
Language/Generator: C/C++
Specification: gl
APIs: gl=4.3
Profile: core
Extensions:
GL_EXT_texture_compression_s3tc,
GL_EXT_texture_filter_anisotropic
Loader: False
Local files: False
Omit khrplatform: False
Reproducible: False
*/
#include <mutex>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
#include <chiaki/log.h>
#include <chiaki/controller.h>
#include "exception.h"
#define PLANES_COUNT 3
#define SDL_JOYSTICK_COUNT 2
class IO
{
private:
ChiakiLog * log;
int video_width;
int video_height;
bool quit = false;
// opengl reader writer
std::mutex mtx;
// default nintendo switch res
int screen_width = 1280;
int screen_height = 720;
std::function<bool()> chiaki_event_connected_cb = nullptr;
std::function<bool(bool)> chiaki_even_login_pin_request_cb = nullptr;
std::function<bool(ChiakiQuitEvent *)> chiaki_event_quit_cb = nullptr;
AVCodec * codec;
AVCodecContext * codec_context;
AVFrame * frame;
SDL_AudioDeviceID sdl_audio_device_id = 0;
SDL_Event sdl_event;
SDL_Joystick * sdl_joystick_ptr[SDL_JOYSTICK_COUNT] = {0};
GLuint vao;
GLuint vbo;
GLuint tex[PLANES_COUNT];
GLuint pbo[PLANES_COUNT];
GLuint vert;
GLuint frag;
GLuint prog;
private:
bool InitAVCodec();
bool InitOpenGl();
bool InitOpenGlTextures();
bool InitOpenGlShader();
void OpenGlDraw();
#ifdef DEBUG_OPENGL
void CheckGLError(const char * func, const char * file, int line);
void DumpShaderError(GLuint prog, const char * func, const char * file, int line);
void DumpProgramError(GLuint prog, const char * func, const char * file, int line);
#endif
GLuint CreateAndCompileShader(GLenum type, const char * source);
void SetOpenGlYUVPixels(AVFrame * frame);
bool ReadGameKeys(SDL_Event * event, ChiakiControllerState * state);
bool ReadGameTouchScreen(ChiakiControllerState * state);
public:
IO(ChiakiLog * log);
~IO();
void SetMesaConfig();
bool VideoCB(uint8_t * buf, size_t buf_size);
void SetEventConnectedCallback(std::function<bool()> chiaki_event_connected_cb)
{
this->chiaki_event_connected_cb = chiaki_event_connected_cb;
};
void SetEventLoginPinRequestCallback(std::function<bool(bool)> chiaki_even_login_pin_request_cb)
{
this->chiaki_even_login_pin_request_cb = chiaki_even_login_pin_request_cb;
};
void SetEventQuitCallback(std::function<bool(ChiakiQuitEvent *)> chiaki_event_quit_cb)
{
this->chiaki_event_quit_cb = chiaki_event_quit_cb;
};
void InitAudioCB(unsigned int channels, unsigned int rate);
void AudioCB(int16_t * buf, size_t samples_count);
void EventCB(ChiakiEvent *event);
bool InitVideo(int video_width, int video_height, int screen_width, int screen_height);
bool FreeVideo();
bool InitJoystick();
bool FreeJoystick();
bool ReadUserKeyboard(char * buffer, size_t buffer_size);
bool MainLoop(ChiakiControllerState * state);
};
#endif //CHIAKI_IO_H

105
switch/include/settings.h Normal file
View file

@ -0,0 +1,105 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_SETTINGS_H
#define CHIAKI_SETTINGS_H
#include <regex>
#include "host.h"
#include <chiaki/log.h>
// mutual host and settings
class Host;
class Settings
{
private:
ChiakiLog * log;
const char * filename = "chiaki.conf";
std::map<std::string, Host> *hosts;
// global_settings from psedo INI file
ChiakiVideoResolutionPreset global_video_resolution = CHIAKI_VIDEO_RESOLUTION_PRESET_720p;
ChiakiVideoFPSPreset global_video_fps = CHIAKI_VIDEO_FPS_PRESET_60;
std::string global_psn_online_id = "";
std::string global_psn_account_id = "";
typedef enum configurationitem
{
UNKNOWN,
HOST_NAME,
HOST_IP,
PSN_ONLINE_ID,
PSN_ACCOUNT_ID,
RP_KEY,
RP_KEY_TYPE,
RP_REGIST_KEY,
VIDEO_RESOLUTION,
VIDEO_FPS,
} ConfigurationItem;
// dummy parser implementation
// the aim is not to have bulletproof parser
// the goal is to read/write inernal flat configuration file
const std::map<Settings::ConfigurationItem, std::regex> re_map = {
{ HOST_NAME, std::regex("^\\[\\s*(.+)\\s*\\]") },
{ HOST_IP, std::regex("^\\s*host_ip\\s*=\\s*\"?(\\d+\\.\\d+\\.\\d+\\.\\d+)\"?") },
{ PSN_ONLINE_ID, std::regex("^\\s*psn_online_id\\s*=\\s*\"?(\\w+)\"?") },
{ PSN_ACCOUNT_ID, std::regex("^\\s*psn_account_id\\s*=\\s*\"?([\\w/=+]+)\"?") },
{ RP_KEY, std::regex("^\\s*rp_key\\s*=\\s*\"?([\\w/=+]+)\"?") },
{ RP_KEY_TYPE, std::regex("^\\s*rp_key_type\\s*=\\s*\"?(\\d)\"?") },
{ RP_REGIST_KEY, std::regex("^\\s*rp_regist_key\\s*=\\s*\"?([\\w/=+]+)\"?") },
{ VIDEO_RESOLUTION, std::regex("^\\s*video_resolution\\s*=\\s*\"?(1080p|720p|540p|360p)\"?") },
{ VIDEO_FPS, std::regex("^\\s*video_fps\\s*=\\s*\"?(60|30)\"?") },
};
ConfigurationItem ParseLine(std::string * line, std::string * value);
size_t GetB64encodeSize(size_t);
public:
Settings(ChiakiLog * log, std::map<std::string, Host> * hosts): log(log), hosts(hosts){};
Host * GetOrCreateHost(std::string * host_name);
ChiakiLog* GetLogger();
std::string GetPSNOnlineID(Host * host);
std::string GetPSNAccountID(Host * host);
void SetPSNOnlineID(Host * host, std::string psn_online_id);
void SetPSNAccountID(Host * host, std::string psn_account_id);
ChiakiVideoResolutionPreset GetVideoResolution(Host * host);
ChiakiVideoFPSPreset GetVideoFPS(Host * host);
std::string ResolutionPresetToString(ChiakiVideoResolutionPreset resolution);
std::string FPSPresetToString(ChiakiVideoFPSPreset fps);
ChiakiVideoResolutionPreset StringToResolutionPreset(std::string value);
ChiakiVideoFPSPreset StringToFPSPreset(std::string value);
int ResolutionPresetToInt(ChiakiVideoResolutionPreset resolution);
int FPSPresetToInt(ChiakiVideoFPSPreset fps);
void SetVideoResolution(Host * host, ChiakiVideoResolutionPreset value);
void SetVideoFPS(Host * host, ChiakiVideoFPSPreset value);
void SetVideoResolution(Host * host, std::string value);
void SetVideoFPS(Host * host, std::string value);
std::string GetHostIPAddr(Host * host);
std::string GetHostName(Host * host);
bool SetHostRPKeyType(Host * host, std::string value);
int GetHostRPKeyType(Host * host);
std::string GetHostRPKey(Host * host);
std::string GetHostRPRegistKey(Host * host);
bool SetHostRPKey(Host * host, std::string rp_key_b64);
bool SetHostRPRegistKey(Host * host, std::string rp_regist_key_b64);
void ParseFile();
int WriteFile();
};
#endif // CHIAKI_SETTINGS_H