Add new HTTP(anda) commands

This commit is contained in:
wheremyfoodat 2023-07-26 20:12:36 +03:00
parent 8fb3096331
commit fa6ce2a7f2
4 changed files with 31 additions and 2 deletions

View file

@ -92,6 +92,7 @@ class Emulator {
void resume(); // Resume the emulator
void pause(); // Pause the emulator
void togglePause();
bool loadROM(const std::filesystem::path& path);
bool loadNCSD(const std::filesystem::path& path, ROMType type);

View file

@ -12,7 +12,7 @@
#include "helpers.hpp"
enum class HttpActionType { None, Screenshot, Key };
enum class HttpActionType { None, Screenshot, Key, TogglePause, Reset };
class Emulator;
namespace httplib {
@ -42,6 +42,8 @@ class HttpAction {
static std::unique_ptr<HttpAction> createScreenshotAction(DeferredResponseWrapper& response);
static std::unique_ptr<HttpAction> createKeyAction(uint32_t key, bool state);
static std::unique_ptr<HttpAction> createTogglePauseAction();
static std::unique_ptr<HttpAction> createResetAction();
};
struct HttpServer {

View file

@ -175,7 +175,7 @@ void Emulator::run() {
// Use the F4 button as a hot-key to pause or resume the emulator
// We can't use the audio play/pause buttons because it's annoying
case SDLK_F4: {
running ? pause() : resume();
togglePause();
break;
}
@ -364,6 +364,7 @@ void Emulator::run() {
// Only resume if a ROM is properly loaded
void Emulator::resume() { running = (romType != ROMType::None); }
void Emulator::pause() { running = false; }
void Emulator::togglePause() { running ? pause() : resume(); }
void Emulator::runFrame() {
if (running) {

View file

@ -19,6 +19,16 @@ class HttpActionScreenshot : public HttpAction {
DeferredResponseWrapper& getResponse() { return response; }
};
class HttpActionTogglePause : public HttpAction {
public:
HttpActionTogglePause() : HttpAction(HttpActionType::TogglePause) {}
};
class HttpActionReset : public HttpAction {
public:
HttpActionReset() : HttpAction(HttpActionType::Reset) {}
};
class HttpActionKey : public HttpAction {
u32 key;
bool state;
@ -35,6 +45,8 @@ std::unique_ptr<HttpAction> HttpAction::createScreenshotAction(DeferredResponseW
}
std::unique_ptr<HttpAction> HttpAction::createKeyAction(u32 key, bool state) { return std::make_unique<HttpActionKey>(key, state); }
std::unique_ptr<HttpAction> HttpAction::createTogglePauseAction() { return std::make_unique<HttpActionTogglePause>(); }
std::unique_ptr<HttpAction> HttpAction::createResetAction() { return std::make_unique<HttpActionReset>(); }
HttpServer::HttpServer(Emulator* emulator)
: emulator(emulator), server(std::make_unique<httplib::Server>()), keyMap({
@ -110,6 +122,16 @@ void HttpServer::startHttpServer() {
server->Get("/status", [this](const httplib::Request&, httplib::Response& response) { response.set_content(status(), "text/plain"); });
server->Get("/togglepause", [this](const httplib::Request&, httplib::Response& response) {
pushAction(HttpAction::createTogglePauseAction());
response.set_content("ok", "text/plain");
});
server->Get("/reset", [this](const httplib::Request&, httplib::Response& response) {
pushAction(HttpAction::createResetAction());
response.set_content("ok", "text/plain");
});
// TODO: ability to specify host and port
printf("Starting HTTP server on port 1234\n");
server->listen("localhost", 1234);
@ -165,6 +187,9 @@ void HttpServer::processActions() {
break;
}
case HttpActionType::TogglePause: emulator->togglePause(); break;
case HttpActionType::Reset: emulator->reset(Emulator::ReloadOption::Reload); break;
default: break;
}
}