ftpiiu_plugin/source/nx/platform.cpp

168 lines
3.3 KiB
C++
Raw Normal View History

2020-04-05 21:16:16 +02:00
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program 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.
//
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
#include "platform.h"
#include "imgui_deko3d.h"
#include "imgui_nx.h"
#include "imgui.h"
#include <switch.h>
#include <mutex>
#include <thread>
bool platform::init ()
{
IMGUI_CHECKVERSION ();
ImGui::CreateContext ();
if (!imgui::nx::init ())
{
ImGui::DestroyContext ();
return false;
}
imgui::deko3d::init ();
return true;
}
bool platform::loop ()
{
if (!appletMainLoop ())
return false;
hidScanInput ();
auto const keysDown = hidKeysDown (CONTROLLER_P1_AUTO);
if (keysDown & KEY_PLUS)
return false;
imgui::nx::newFrame ();
imgui::deko3d::newFrame ();
ImGui::NewFrame ();
imgui::deko3d::test ();
return true;
}
void platform::render ()
{
ImGui::Render ();
imgui::deko3d::render ();
}
void platform::exit ()
{
imgui::nx::exit ();
imgui::deko3d::exit ();
ImGui::DestroyContext ();
}
///////////////////////////////////////////////////////////////////////////
class platform::Thread::privateData_t
{
public:
privateData_t () = default;
privateData_t (std::function<void ()> func_) : thread (func_)
{
}
std::thread thread;
};
///////////////////////////////////////////////////////////////////////////
platform::Thread::~Thread () = default;
platform::Thread::Thread () : m_d (new privateData_t ())
{
}
platform::Thread::Thread (std::function<void ()> func_) : m_d (new privateData_t (func_))
{
}
platform::Thread::Thread (Thread &&that_) : m_d (new privateData_t ())
{
std::swap (m_d, that_.m_d);
}
platform::Thread &platform::Thread::operator= (Thread &&that_)
{
std::swap (m_d, that_.m_d);
return *this;
}
void platform::Thread::join ()
{
m_d->thread.join ();
}
void platform::Thread::sleep (std::chrono::milliseconds const timeout_)
{
std::this_thread::sleep_for (timeout_);
}
///////////////////////////////////////////////////////////////////////////
#define USE_STD_MUTEX 1
class platform::Mutex::privateData_t
{
public:
#if USE_STD_MUTEX
std::mutex mutex;
#else
::Mutex mutex;
#endif
};
///////////////////////////////////////////////////////////////////////////
platform::Mutex::~Mutex () = default;
platform::Mutex::Mutex () : m_d (new privateData_t ())
{
#if !USE_STD_MUTEX
mutexInit (&m_d->mutex);
#endif
}
void platform::Mutex::lock ()
{
#if USE_STD_MUTEX
m_d->mutex.lock ();
#else
mutexLock (&m_d->mutex);
#endif
}
void platform::Mutex::unlock ()
{
#if USE_STD_MUTEX
m_d->mutex.unlock ();
#else
mutexUnlock (&m_d->mutex);
#endif
}