diff --git a/include/httpserver.hpp b/include/httpserver.hpp index f6329589..9d3cb818 100644 --- a/include/httpserver.hpp +++ b/include/httpserver.hpp @@ -18,7 +18,7 @@ class Emulator; namespace httplib { class Server; class Response; -} // namespace httplib +} // Wrapper for httplib::Response that allows the HTTP server to wait for the response to be ready struct DeferredResponseWrapper { @@ -32,6 +32,8 @@ struct DeferredResponseWrapper { // Actions derive from this class and are used to communicate with the HTTP server class HttpAction { + HttpActionType type; + public: HttpAction(HttpActionType type) : type(type) {} virtual ~HttpAction() = default; @@ -40,22 +42,17 @@ class HttpAction { static std::unique_ptr createScreenshotAction(DeferredResponseWrapper& response); static std::unique_ptr createKeyAction(uint32_t key, bool state); - - private: - HttpActionType type; }; struct HttpServer { HttpServer(Emulator* emulator); ~HttpServer(); - void processActions(); private: static constexpr const char* httpServerScreenshotPath = "screenshot.png"; Emulator* emulator; - std::unique_ptr server; std::thread httpServerThread; diff --git a/include/services/hid.hpp b/include/services/hid.hpp index a6cf2ec2..e10f78f0 100644 --- a/include/services/hid.hpp +++ b/include/services/hid.hpp @@ -90,11 +90,10 @@ class HIDService { void pressKey(u32 mask) { newButtons |= mask; } void releaseKey(u32 mask) { newButtons &= ~mask; } - bool isPressed(u32 mask) { return (oldButtons & mask) != 0; } - u32 getOldButtons() { return oldButtons; } - s16 getCirclepadX() { return circlePadX; } - s16 getCirclepadY() { return circlePadY; } + u32 getOldButtons() const { return oldButtons; } + s16 getCirclepadX() const { return circlePadX; } + s16 getCirclepadY() const { return circlePadY; } void setCirclepadX(s16 x) { circlePadX = x; diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 85e67d01..d8841807 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -8,35 +8,33 @@ #include #include "emulator.hpp" +#include "helpers.hpp" #include "httplib.h" class HttpActionScreenshot : public HttpAction { + DeferredResponseWrapper& response; + public: HttpActionScreenshot(DeferredResponseWrapper& response) : HttpAction(HttpActionType::Screenshot), response(response) {} - DeferredResponseWrapper& getResponse() { return response; } - - private: - DeferredResponseWrapper& response; }; class HttpActionKey : public HttpAction { - public: - HttpActionKey(uint32_t key, bool state) : HttpAction(HttpActionType::Key), key(key), state(state) {} - - uint32_t getKey() const { return key; } - bool getState() const { return state; } - - private: - uint32_t key; + u32 key; bool state; + + public: + HttpActionKey(u32 key, bool state) : HttpAction(HttpActionType::Key), key(key), state(state) {} + + u32 getKey() const { return key; } + bool getState() const { return state; } }; std::unique_ptr HttpAction::createScreenshotAction(DeferredResponseWrapper& response) { return std::make_unique(response); } -std::unique_ptr HttpAction::createKeyAction(uint32_t key, bool state) { return std::make_unique(key, state); } +std::unique_ptr HttpAction::createKeyAction(u32 key, bool state) { return std::make_unique(key, state); } HttpServer::HttpServer(Emulator* emulator) : emulator(emulator), server(std::make_unique()), keyMap({ @@ -86,8 +84,7 @@ void HttpServer::startHttpServer() { u32 key = stringToKey(keyStr); if (key != 0) { - bool state = value == "1"; - + bool state = (value == "1"); if (!state && value != "0") { // Invalid state ok = false; @@ -103,11 +100,7 @@ void HttpServer::startHttpServer() { } } - if (ok) { - response.set_content("ok", "text/plain"); - } else { - response.set_content("error", "text/plain"); - } + response.set_content(ok ? "ok" : "error", "text/plain"); }); server->Get("/step", [this](const httplib::Request&, httplib::Response& response) { @@ -124,14 +117,15 @@ void HttpServer::startHttpServer() { std::string HttpServer::status() { HIDService& hid = emulator->kernel.getServiceManager().getHID(); - std::stringstream stringStream; stringStream << "Panda3DS\n"; stringStream << "Status: " << (paused ? "Paused" : "Running") << "\n"; + // TODO: This currently doesn't work for N3DS buttons + auto keyPressed = [](const HIDService& hid, u32 mask) { return (hid.getOldButtons() & mask) != 0; }; for (auto& [keyStr, value] : keyMap) { - stringStream << keyStr << ": " << hid.isPressed(value) << "\n"; + stringStream << keyStr << ": " << keyPressed(hid, value) << "\n"; } return stringStream.str(); @@ -160,6 +154,7 @@ void HttpServer::processActions() { response.cv.notify_one(); break; } + case HttpActionType::Key: { HttpActionKey* keyAction = static_cast(action.get()); if (keyAction->getState()) { @@ -169,9 +164,8 @@ void HttpServer::processActions() { } break; } - default: { - break; - } + + default: break; } } }