IPC cleanup, common code cleanup too

This commit is contained in:
XorTroll 2020-02-12 22:04:12 +01:00
parent 0b21d2f90c
commit d905b1e008
11 changed files with 75 additions and 176 deletions

View file

@ -1,28 +0,0 @@
#pragma once
#include <stratosphere.hpp>
#include <ul_Include.hpp>
namespace ipc
{
class IDaemonService : public ams::sf::IServiceObject
{
private:
enum class CommandId
{
GetLatestMessage = 0
};
public:
void GetLatestMessage(ams::sf::Out<u32> msg);
public:
DEFINE_SERVICE_DISPATCH_TABLE
{
MAKE_SERVICE_COMMAND_META(GetLatestMessage)
};
};
}

View file

@ -1,5 +1,6 @@
#include <ecs/ecs_ExternalContent.hpp>
#include <ipc/ipc_IDaemonService.hpp>
#include <ipc/ipc_IPrivateService.hpp>
#include <ipc/ipc_IPublicService.hpp>
#include <db/db_Save.hpp>
#include <os/os_Titles.hpp>
#include <os/os_HomeMenu.hpp>
@ -91,7 +92,7 @@ void HandleHomeButton()
{
am::LibraryAppletTerminate();
auto status = CreateStatus();
UL_R_TRY(LaunchMenu(am::MenuStartMode::Menu, status));
UL_ASSERT(LaunchMenu(am::MenuStartMode::Menu, status));
return;
}
if(am::ApplicationIsActive())
@ -100,7 +101,7 @@ void HandleHomeButton()
{
am::HomeMenuSetForeground();
auto status = CreateStatus();
UL_R_TRY(LaunchMenu(am::MenuStartMode::MenuApplicationSuspended, status));
UL_ASSERT(LaunchMenu(am::MenuStartMode::MenuApplicationSuspended, status));
used_to_reopen_menu = true;
}
}
@ -161,7 +162,7 @@ void UpdateOperationMode()
// We're qlaunch, not using appletMainLoop, thus have to take care of this manually
u8 tmp_mode = 0;
UL_R_TRY(serviceDispatchOut(appletGetServiceSession_CommonStateGetter(), 5, tmp_mode));
UL_ASSERT(serviceDispatchOut(appletGetServiceSession_CommonStateGetter(), 5, tmp_mode));
console_mode = (AppletOperationMode)tmp_mode;
}
@ -357,19 +358,26 @@ namespace
static const size_t MaxDomainObjects = 0x40;
};
constexpr size_t MaxServers = 2;
constexpr size_t MaxSessions = 2;
constexpr size_t MaxPrivateSessions = 1;
constexpr ams::sm::ServiceName PrivateServiceName = ams::sm::ServiceName::Encode(AM_DAEMON_PRIVATE_SERVICE_NAME);
ams::sf::hipc::ServerManager<MaxServers, ServerOptions, MaxSessions> private_service_manager;
constexpr size_t MaxPublicSessions = 0x20;
constexpr ams::sm::ServiceName PublicServiceName = ams::sm::ServiceName::Encode(AM_DAEMON_PUBLIC_SERVICE_NAME);
constexpr size_t NumServers = 2;
constexpr size_t MaxSessions = MaxPrivateSessions + MaxPublicSessions + 1;
ams::sf::hipc::ServerManager<NumServers, ServerOptions, MaxSessions> daemon_ipc_manager;
}
namespace daemn
{
void IPCManagerThread(void *arg)
{
UL_R_TRY(private_service_manager.RegisterServer<ipc::IDaemonService>(PrivateServiceName, MaxSessions).GetValue());
private_service_manager.LoopProcess();
UL_ASSERT(daemon_ipc_manager.RegisterServer<ipc::IPrivateService>(PrivateServiceName, MaxPrivateSessions).GetValue());
UL_ASSERT(daemon_ipc_manager.RegisterServer<ipc::IPublicService>(PublicServiceName, MaxPublicSessions).GetValue());
daemon_ipc_manager.LoopProcess();
}
void USBViewerThread(void *arg)
@ -395,7 +403,7 @@ namespace daemn
{
if(!am::LibraryAppletIsActive())
{
UL_R_TRY(am::WebAppletStart(&webapplet_flag));
UL_ASSERT(am::WebAppletStart(&webapplet_flag));
sth_done = true;
memset(&webapplet_flag, 0, sizeof(webapplet_flag));
@ -406,7 +414,7 @@ namespace daemn
if(!am::LibraryAppletIsActive())
{
auto status = CreateStatus();
UL_R_TRY(LaunchMenu(am::MenuStartMode::StartupScreen, status))
UL_ASSERT(LaunchMenu(am::MenuStartMode::StartupScreen, status))
sth_done = true;
menu_restart_flag = false;
@ -417,7 +425,7 @@ namespace daemn
if(!am::LibraryAppletIsActive())
{
u8 albumflag = 2;
UL_R_TRY(am::LibraryAppletStart(AppletId_photoViewer, 0x10000, &albumflag, sizeof(albumflag)));
UL_ASSERT(am::LibraryAppletStart(AppletId_photoViewer, 0x10000, &albumflag, sizeof(albumflag)));
sth_done = true;
album_flag = false;
@ -430,7 +438,7 @@ namespace daemn
if(strlen(hbapplaunch_flag.nro_path))
{
auto params = hb::HbTargetParams::Create(hbapplaunch_flag.nro_path, hbapplaunch_flag.nro_argv, false);
UL_R_TRY(ecs::RegisterLaunchAsApplication(titlelaunch_flag, "/ulaunch/bin/uHbTarget/app/", &params, sizeof(params), selected_uid));
UL_ASSERT(ecs::RegisterLaunchAsApplication(titlelaunch_flag, "/ulaunch/bin/uHbTarget/app/", &params, sizeof(params), selected_uid));
sth_done = true;
app_opened_hb = true;
titlelaunch_flag = 0;
@ -438,7 +446,7 @@ namespace daemn
}
else
{
UL_R_TRY(am::ApplicationStart(titlelaunch_flag, false, selected_uid));
UL_ASSERT(am::ApplicationStart(titlelaunch_flag, false, selected_uid));
sth_done = true;
titlelaunch_flag = 0;
}
@ -449,7 +457,7 @@ namespace daemn
if(!am::LibraryAppletIsActive())
{
auto params = hb::HbTargetParams::Create(hblaunch_flag.nro_path, hblaunch_flag.nro_argv, false);
UL_R_TRY(ecs::RegisterLaunchAsApplet(config.homebrew_applet_program_id, 0, "/ulaunch/bin/uHbTarget/applet/", &params, sizeof(params)));
UL_ASSERT(ecs::RegisterLaunchAsApplet(config.homebrew_applet_program_id, 0, "/ulaunch/bin/uHbTarget/applet/", &params, sizeof(params)));
sth_done = true;
hblaunch_flag.nro_path[0] = '\0';
}
@ -460,7 +468,7 @@ namespace daemn
if((cur_id == AppletId_web) || (cur_id == AppletId_photoViewer) || (cur_id == config.homebrew_applet_program_id))
{
auto status = CreateStatus();
UL_R_TRY(LaunchMenu(am::MenuStartMode::Menu, status));
UL_ASSERT(LaunchMenu(am::MenuStartMode::Menu, status));
sth_done = true;
}
}
@ -471,7 +479,7 @@ namespace daemn
if(!am::ApplicationIsActive() && !am::LibraryAppletIsActive())
{
auto status = CreateStatus();
UL_R_TRY(LaunchMenu(am::MenuStartMode::MenuLaunchFailure, status));
UL_ASSERT(LaunchMenu(am::MenuStartMode::MenuLaunchFailure, status));
}
}
}
@ -494,12 +502,13 @@ namespace daemn
{
ams::hos::SetVersionForLibnx();
UL_R_TRY(nsInitialize())
UL_R_TRY(appletLoadAndApplyIdlePolicySettings())
UL_ASSERT(nsInitialize())
UL_ASSERT(pminfoInitialize())
UL_ASSERT(appletLoadAndApplyIdlePolicySettings())
UpdateOperationMode();
UL_R_TRY(ecs::Initialize())
UL_ASSERT(ecs::Initialize())
UL_R_TRY(db::Mount())
UL_ASSERT(db::Mount())
// Remove old password files
fs::DeleteDirectory(UL_BASE_DB_DIR "/user");
@ -521,12 +530,12 @@ namespace daemn
if(config.viewer_usb_enabled)
{
UL_R_TRY(usbCommsInitialize());
UL_ASSERT(usbCommsInitialize());
usbbuf = new (std::align_val_t(0x1000)) u8[RawRGBAScreenBufferSize]();
UL_R_TRY(LaunchUSBViewerThread());
UL_ASSERT(LaunchUSBViewerThread());
}
UL_R_TRY(LaunchIPCManagerThread())
UL_ASSERT(LaunchIPCManagerThread())
#if UL_DEV
// Debug testing mode
@ -590,6 +599,7 @@ namespace daemn
}
nsExit();
pminfoExit();
ecs::Exit();
db::Unmount();
}
@ -604,7 +614,7 @@ int main()
cfg::CacheEverything();
auto status = CreateStatus();
UL_R_TRY(LaunchMenu(am::MenuStartMode::StartupScreen, status))
UL_ASSERT(LaunchMenu(am::MenuStartMode::StartupScreen, status))
while(true)
{

View file

@ -1,15 +0,0 @@
#include <ipc/ipc_IDaemonService.hpp>
#include <am/am_DaemonMenuInteraction.hpp>
extern ams::os::Mutex g_last_menu_msg_lock;
extern am::MenuMessage g_last_menu_msg;
namespace ipc
{
void IDaemonService::GetLatestMessage(ams::sf::Out<u32> msg)
{
std::scoped_lock _lock(g_last_menu_msg_lock);
msg.SetValue((u32)g_last_menu_msg);
g_last_menu_msg = am::MenuMessage::Invalid;
}
}

View file

@ -7,6 +7,7 @@ namespace am
{
bool LibraryAppletIsActive();
void LibraryAppletSetMenuAppletId(AppletId id);
AppletId LibraryAppletGetMenuAppletId();
bool LibraryAppletIsMenu();
void LibraryAppletTerminate();
Result LibraryAppletStart(AppletId id, u32 la_version, void *in_data, size_t in_size);

View file

@ -22,7 +22,7 @@ using JSON = nlohmann::json;
#define UL_BASE_DIR "ulaunch"
#define UL_BASE_SD_DIR "sdmc:/" UL_BASE_DIR
#define UL_DB_MOUNT_NAME "qsave"
#define UL_DB_MOUNT_NAME "ul_save"
#define UL_DB_MOUNT_PATH UL_DB_MOUNT_NAME ":/"
#define UL_BASE_DB_DIR UL_DB_MOUNT_PATH UL_BASE_DIR
#define UL_ENTRIES_PATH UL_BASE_SD_DIR "/entries"
@ -79,16 +79,15 @@ static constexpr Mutex EmptyMutex = (Mutex)0;
#include <ul_Results.hpp>
#define UL_ASSERT(expr) { auto _tmp_rc = (expr); if(R_FAILED(_tmp_rc)) { fatalThrow(_tmp_rc); } }
// Console (debug)
#if UL_DEV
#include <iostream>
#define CONSOLE_OUT(...) { std::cout << __VA_ARGS__ << std::endl; consoleUpdate(NULL); }
#define CONSOLE_FMT(fmt, ...) { printf(fmt "\n", ##__VA_ARGS__); consoleUpdate(NULL); }
inline void Panic(std::string msg)
{
// TODO: non-console panic...?
}
#define UL_R_TRY(expr) { auto _tmp_rc = (expr); if(R_FAILED(_tmp_rc)) { fatalThrow(_tmp_rc); } }
#endif

View file

@ -1,84 +1,17 @@
#include <switch.h>
#include <vector>
#include <string>
// Result-making macros
#define RES_BLOCK_BEGIN static std::vector<ResultModuleImpl> BlockImpl = {
#define RES_MODULE_BEGIN(name, val) { val * 100, #name, {
#define RES_DEFINE(name, desc) { desc, #name },
#define RES_MODULE_END } },
#define RES_BLOCK_END };
#define RES_VALUE(module, name) res::GetResultByModuleAndName(#module, #name)
#define RES_DESCRIPTION(rc) res::GetDescriptionByResult(rc)
namespace res
{
struct ResultImpl
{
u32 res_desc;
std::string res_name;
};
struct ResultModuleImpl
{
u32 mod_val;
std::string mod_name;
std::vector<ResultImpl> results;
};
// All 2380-**** results are from uLaunch!
static constexpr u32 Module = 380;
RES_BLOCK_BEGIN
RES_MODULE_BEGIN(Misc, 1)
RES_DEFINE(InvalidJSONFile, 1)
RES_MODULE_END
RES_MODULE_BEGIN(Daemon, 2)
RES_DEFINE(ApplicationActive, 1)
RES_DEFINE(InvalidSelectedUser, 2)
RES_DEFINE(AlreadyQueued, 3)
RES_DEFINE(ApplicationNotActive, 4)
RES_MODULE_END
RES_BLOCK_END
inline Result GetResultByModuleAndName(std::string mod, std::string name)
{
for(auto &module: BlockImpl)
{
if(module.mod_name == mod)
{
for(auto &res: module.results)
{
if(res.res_name == name)
{
return MAKERESULT(Module, module.mod_val + res.res_desc);
}
}
}
}
return 0;
}
inline std::string GetDescriptionByResult(Result rc)
{
for(auto &module: BlockImpl)
{
for(auto &res: module.results)
{
auto resval = MAKERESULT(Module, module.mod_val + res.res_desc);
if(resval == rc) return module.mod_name + " - " + res.res_name;
}
}
return "";
}
Result GetResultByModuleAndName(std::string mod, std::string name);
std::string GetDescriptionByResult(Result rc);
}

View file

@ -39,6 +39,11 @@ namespace am
g_menu_applet_id = id;
}
AppletId LibraryAppletGetMenuAppletId()
{
return g_menu_applet_id;
}
bool LibraryAppletIsMenu()
{
return (LibraryAppletIsActive() && (g_menu_applet_id != InvalidAppletId) && (LibraryAppletGetId() == g_menu_applet_id));

View file

@ -37,12 +37,12 @@ namespace qmenu
{
void Initialize()
{
UL_R_TRY(accountInitialize(AccountServiceType_System))
UL_R_TRY(nsInitialize())
UL_R_TRY(net::Initialize())
UL_R_TRY(psmInitialize())
UL_R_TRY(setsysInitialize())
UL_R_TRY(setInitialize())
UL_ASSERT(accountInitialize(AccountServiceType_System))
UL_ASSERT(nsInitialize())
UL_ASSERT(net::Initialize())
UL_ASSERT(psmInitialize())
UL_ASSERT(setsysInitialize())
UL_ASSERT(setInitialize())
// Register handlers for HOME button press detection
am::RegisterLibAppletHomeButtonDetection();
@ -50,7 +50,7 @@ namespace qmenu
ui::QuickMenu::RegisterHomeButtonDetection();
// Initialize Daemon message handling
UL_R_TRY(am::InitializeDaemonMessageHandler())
UL_ASSERT(am::InitializeDaemonMessageHandler())
// Load menu g_ul_config and theme
@ -86,10 +86,10 @@ int main()
if(smode != am::MenuStartMode::Invalid)
{
// Check if our RomFs file exists...
if(!fs::ExistsFile(MENU_ROMFS_BIN)) Panic("Unable to find RomFs binary: '" MENU_ROMFS_BIN "'");
if(!fs::ExistsFile(MENU_ROMFS_BIN)) UL_ASSERT(RES_VALUE(Menu, RomfsBinNotFound))
// Try to mount RomFs from our binary
UL_R_TRY(romfsMountFromFsdev(MENU_ROMFS_BIN, 0, "romfs"))
UL_ASSERT(romfsMountFromFsdev(MENU_ROMFS_BIN, 0, "romfs"))
// After initializing RomFs, start initializing the rest of stuff here
app_buf = new u8[RawRGBAScreenBufferSize]();

View file

@ -20,19 +20,19 @@ namespace am
mutexUnlock(&g_receiver_lock);
if(should_stop) break;
MenuMessage tmp_msg = MenuMessage::Invalid;
auto rc = serviceDispatchOut(&g_daemon_srv, 0, tmp_msg);
if(R_SUCCEEDED(rc))
u64 pid_placeholder = 0;
UL_ASSERT(serviceDispatchInOut(&g_daemon_srv, 0, pid_placeholder, tmp_msg,
.in_send_pid = true,
));
mutexLock(&g_receiver_lock);
for(auto &[cb, msg] : g_callback_table)
{
mutexLock(&g_receiver_lock);
for(auto &[cb, msg] : g_callback_table)
if(msg == tmp_msg)
{
if(msg == tmp_msg)
{
cb();
}
cb();
}
mutexUnlock(&g_receiver_lock);
}
mutexUnlock(&g_receiver_lock);
svcSleepThread(10'000'000ul);
}
}
@ -61,6 +61,7 @@ namespace am
g_thr_should_stop = true;
mutexUnlock(&g_receiver_lock);
threadWaitForExit(&g_receiver_thr);
threadClose(&g_receiver_thr);
serviceClose(&g_daemon_srv);
g_init = false;
}

View file

@ -23,7 +23,6 @@ namespace am
extern "C"
{
// Wrap libappletStart and libappletLaunch to use our custom waiting system
Result __wrap_libappletStart(AppletHolder *h)
{
@ -37,7 +36,6 @@ extern "C"
{
while(true)
{
if(appletHolderCheckFinished(h)) break;
if(!serviceIsActive(&h->s)) break;
@ -58,10 +56,7 @@ extern "C"
LibAppletExitReason reason = appletHolderGetExitReason(h);
if(reason == LibAppletExitReason_Canceled || reason == LibAppletExitReason_Abnormal || reason == LibAppletExitReason_Unexpected)
{
rc = MAKERESULT(Module_Libnx, LibnxError_LibAppletBadExit);
}
if(reason == LibAppletExitReason_Canceled || reason == LibAppletExitReason_Abnormal || reason == LibAppletExitReason_Unexpected) rc = MAKERESULT(Module_Libnx, LibnxError_LibAppletBadExit);
}
mutexLock(&g_amwrap_detection_lock);
@ -73,19 +68,18 @@ extern "C"
Result __wrap_libappletLaunch(AppletId id, LibAppletArgs *commonargs, const void* arg, size_t arg_size, void* reply, size_t reply_size, size_t *out_reply_size)
{
Result rc=0;
AppletHolder holder;
rc = appletCreateLibraryApplet(&holder, id, LibAppletMode_AllForeground);
if (R_FAILED(rc)) return rc;
Result rc = appletCreateLibraryApplet(&holder, id, LibAppletMode_AllForeground);
if(R_FAILED(rc)) return rc;
rc = libappletArgsPush(commonargs, &holder);
if (R_SUCCEEDED(rc) && arg && arg_size) rc = libappletPushInData(&holder, arg, arg_size);
if(R_SUCCEEDED(rc) && arg && arg_size) rc = libappletPushInData(&holder, arg, arg_size);
if (R_SUCCEEDED(rc)) rc = __wrap_libappletStart(&holder);
if(R_SUCCEEDED(rc)) rc = __wrap_libappletStart(&holder);
if (R_SUCCEEDED(rc) && reply && reply_size) rc = libappletPopOutData(&holder, reply, reply_size, out_reply_size);
if(R_SUCCEEDED(rc) && reply && reply_size) rc = libappletPopOutData(&holder, reply, reply_size, out_reply_size);
appletHolderClose(&holder);

View file

@ -17,7 +17,6 @@ extern cfg::Config g_ul_config;
namespace ui::actions
{
void ShowAboutDialog()
{
g_menu_app_instance->CreateShowDialog(cfg::GetLanguageString(g_ul_config.main_lang, g_ul_config.default_lang, "ulaunch_about"), "uLaunch v" + std::string(UL_VERSION) + "\n\n" + cfg::GetLanguageString(g_ul_config.main_lang, g_ul_config.default_lang, "ulaunch_desc") + ":\nhttps://github.com/XorTroll/uLaunch", { cfg::GetLanguageString(g_ul_config.main_lang, g_ul_config.default_lang, "ok") }, true, "romfs:/LogoLarge.png");