doompanning/src/doompanning.cc

383 lines
10 KiB
C++
Raw Normal View History

#include <nng/nng.h>
#include <imgui.h>
#include <backends/imgui_impl_sdl.h>
#include <backends/imgui_impl_sdlrenderer.h>
2023-02-12 17:39:26 +01:00
#include <SDL2/SDL.h>
#include <algorithm>
#include <cerrno>
#include <cstdlib>
#include <thread>
#include <type_traits>
#include <vector>
#include <signal.h>
#include <spawn.h>
#include <sys/wait.h>
#include "dp_common.h"
#include "dp_util.hpp"
void dp_sdl_fatal(const char *const msg)
{
log_fatal("%s: %s", msg, SDL_GetError());
abort();
}
struct DoomState
{
doomid_t id;
DP_DoomState state = DP_DS_Unknown;
};
static_assert(std::is_trivially_copyable<DoomState>::value,
"DoomState must be a trivially copyable type");
2023-02-12 13:40:41 +01:00
struct ControllerContext
{
nng_socket pub;
nng_socket sub;
SDL_Window *window;
SDL_Renderer *renderer;
std::vector<DoomState> dooms;
bool quit = false;
ExampleAppLog appLog;
int columns = 4;
2023-02-12 13:40:41 +01:00
};
struct ControllerActions
{
int doomsToSpawn = 0;
bool endAllDooms = false;
};
void spawn_doom(ControllerContext &ctx)
{
DoomState ds = {};
const char *const argv[] = { "doomsim", nullptr };
// TODO: Close stdin and stdout? Leave them open for now to see the logging
// output.
if (auto err = posix_spawn(&ds.id, "doomsim", nullptr, nullptr,
const_cast<char *const *>(argv), nullptr))
{
log_error("Could not spawn doom: %s", strerror(err));
return;
}
ctx.dooms.emplace_back(ds);
log_info("Spawned new doom, pid=%d", ds.id);
}
void end_all_dooms(ControllerContext &ctx)
{
nng_msg *msg = nullptr;
int res = 0;
if ((res = nng_msg_alloc(&msg, sizeof(MsgMcstCommand))))
dp_nng_fatal("ctrl/nng_msg_alloc", res);
auto dpmsg = DP_NNG_BODY_AS(msg, MsgMcstCommand);
dpmsg->head.msgType = DP_MT_McstCommand;
dpmsg->cmd = DP_DC_QuitDoom;
if ((res = nng_sendmsg(ctx.pub, msg, 0)))
dp_nng_fatal("ctrl/sendmsg", res);
}
void signal_all_dooms(ControllerContext &ctx, int signum)
{
std::for_each(std::begin(ctx.dooms), std::end(ctx.dooms),
[signum] (const auto &ds)
{
kill(ds.id, signum);
});
}
void perform_actions(ControllerContext &ctx, const ControllerActions &actions)
{
if (actions.doomsToSpawn)
{
log_info("Spawning %d new dooms", actions.doomsToSpawn);
for (int i=0; i<actions.doomsToSpawn; ++i)
spawn_doom(ctx);
}
if (actions.endAllDooms)
{
log_info("Telling all dooms to quit");
end_all_dooms(ctx);
}
}
void check_on_dooms(ControllerContext &ctx)
{
pid_t pid = 0;
do
{
int wstatus = 0;
if (pid = waitpid(0, &wstatus, WNOHANG); pid > 0)
{
auto dit = find_in_container(ctx.dooms, [pid] (const auto &ds) { return ds.id == pid; });
assert(dit != std::end(ctx.dooms));
if (dit != std::end(ctx.dooms))
{
if (WIFEXITED(wstatus))
log_info("doom(%d) exited with status %d", pid, WEXITSTATUS(wstatus));
else if (WIFSIGNALED(wstatus))
log_warn("doom#(%d) got killed by signal %d", pid, WTERMSIG(wstatus));
ctx.dooms.erase(dit);
}
}
} while (pid > 0);
}
void do_networking(ControllerContext &ctx)
{
// FIXME: test code. spam RunDoom
{
nng_msg *msg = nullptr;
int res = 0;
if ((res = nng_msg_alloc(&msg, sizeof(MsgMcstCommand))))
dp_nng_fatal("ctrl/nng_msg_alloc", res);
auto dpmsg = DP_NNG_BODY_AS(msg, MsgMcstCommand);
dpmsg->head.msgType = DP_MT_McstCommand;
dpmsg->cmd = DP_DC_RunDoom;
if ((res = nng_sendmsg(ctx.pub, msg, 0)))
dp_nng_fatal("ctrl/sendmsg", res);
}
}
void final_cleanup(ControllerContext &ctx)
{
log_debug("final cleanup: ending all dooms");
end_all_dooms(ctx);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
check_on_dooms(ctx);
if (!ctx.dooms.empty())
{
log_warn("final cleanup: terminating all %zu remaining dooms", ctx.dooms.size());
signal_all_dooms(ctx, SIGTERM);
}
}
ControllerActions run_ui(ControllerContext &ctx)
{
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
const bool has_debug_tools = true;
#else
const bool has_debug_tools = false;
#endif
static bool show_app_metrics = false;
static bool show_app_debug_log = false;
static bool show_log_window = true;
if (show_app_metrics)
ImGui::ShowMetricsWindow(&show_app_metrics);
if (show_app_debug_log)
ImGui::ShowDebugLogWindow(&show_app_debug_log);
2023-02-12 16:12:01 +01:00
const ImGuiViewport* main_viewport = ImGui::GetMainViewport();
if (show_log_window)
{
ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 666, main_viewport->WorkPos.y + 20), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver);
ctx.appLog.Draw("log");
}
2023-02-12 16:12:01 +01:00
ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 20, main_viewport->WorkPos.y + 20), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(420, 340), ImGuiCond_FirstUseEver);
2023-02-12 16:12:01 +01:00
ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar;
static std::array<char, 1024> strbuf;
2023-02-12 16:12:01 +01:00
auto &io = ImGui::GetIO();
aprintf(strbuf, "doompanning - #dooms=%zu, %.2f ms/frame (%.1f fps)###doompanning",
ctx.dooms.size(), 1000.0f / io.Framerate, io.Framerate);
2023-02-12 16:12:01 +01:00
// Main body of the doompanning window starts here.
if (!ImGui::Begin(strbuf.data(), nullptr, window_flags))
{
// Early out if the window is collapsed, as an optimization.
ImGui::End();
return {};
}
// Menu Bar
if (ImGui::BeginMenuBar())
{
if (ImGui::BeginMenu("Menu"))
{
ImGui::MenuItem("Log Window", nullptr, &show_log_window);
ImGui::MenuItem("Quit", "Ctrl+Q", &ctx.quit, true);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Tools"))
{
ImGui::MenuItem("Dear ImGui Metrics/Debugger", NULL, &show_app_metrics, has_debug_tools);
ImGui::MenuItem("Dear ImGui Debug Log", NULL, &show_app_debug_log, has_debug_tools);
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
// Window contents
ControllerActions result = {};
static int doomsToSpawn = 1;
ImGui::PushItemWidth(ImGui::GetFontSize() * -16); // affects stuff like slider widths
ImGui::SliderInt("Layout columns##columns", &ctx.columns, 1, 32, "%d", ImGuiSliderFlags_AlwaysClamp);
ImGui::SliderInt("##dooms", &doomsToSpawn, 1, 256, "%d", ImGuiSliderFlags_AlwaysClamp | ImGuiSliderFlags_Logarithmic);
aprintf(strbuf, "Spawn %d more doom%s###spawnmore", doomsToSpawn, doomsToSpawn > 1 ? "s" : "");
if (ImGui::SameLine(); ImGui::Button(strbuf.data()))
result.doomsToSpawn = doomsToSpawn;
if (ImGui::Button("End all Dooms"))
result.endAllDooms = true;
ImGui::PopItemWidth();
ImGui::End();
return result;
}
int doom_controller_loop(ControllerContext &ctx)
{
2023-02-12 13:40:41 +01:00
static constexpr ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
2023-02-18 22:41:05 +01:00
ControllerActions actions = {};
while (!ctx.quit)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
ctx.quit = true;
2023-02-12 13:40:41 +01:00
if (event.type == SDL_WINDOWEVENT
&& event.window.event == SDL_WINDOWEVENT_CLOSE
&& event.window.windowID == SDL_GetWindowID(ctx.window))
2023-02-12 13:40:41 +01:00
{
ctx.quit = true;
2023-02-12 13:40:41 +01:00
}
}
// Process input events not consumed by ImGui
if (auto &io = ImGui::GetIO();
!io.WantCaptureKeyboard)
{
if (io.KeyCtrl && ImGui::IsKeyDown(ImGuiKey_Q))
{
ctx.quit = true;
}
}
perform_actions(ctx, actions);
check_on_dooms(ctx);
do_networking(ctx);
// Start the Dear ImGui frame
ImGui_ImplSDLRenderer_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
actions = run_ui(ctx);
// Rendering
2023-02-18 22:41:05 +01:00
const auto [r, g, b, a] = imvec4_to_rgba(clear_color);
SDL_SetRenderDrawColor(ctx.renderer, r, g, b, a);
SDL_RenderClear(ctx.renderer);
2023-02-18 22:41:05 +01:00
// TODO render dooms
ImGui::Render();
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(ctx.renderer);
}
final_cleanup(ctx);
return 0;
}
void log_to_imgui(log_Event *ev)
{
auto ctx = reinterpret_cast<ControllerContext *>(ev->udata);
ctx->appLog.AddLog(ev->fmt, ev->ap);
}
int main(int argc, char *argv[])
{
(void) argc;
(void) argv;
#ifndef NDEBUG
log_set_level(LOG_TRACE);
#else
log_set_level(LOG_DEBUG);
#endif
log_info("doompanning ctrl starting");
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER))
dp_sdl_fatal("SDL_Init");
#ifdef SDL_HINT_IME_SHOW_UI
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif
const auto windowFlags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL;
auto window = SDL_CreateWindow("doompanning", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, windowFlags);
if (!window)
dp_sdl_fatal("SDL_CreateWindow");
auto renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (!renderer)
dp_sdl_fatal("SDL_CreateRenderer");
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::GetIO().IniFilename = "doompanning_ui.ini";
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer_Init(renderer);
dp_nng_init_limits(1, 1, 1); // int ncpu_max, int pool_thread_limit_max, int resolv_thread_limit
2023-02-04 01:56:30 +01:00
ControllerContext ctx;
ctx.pub = make_ctrl_pub(CtrlUrl);
ctx.sub = make_ctrl_sub(DoomUrl);
ctx.window = window;
ctx.renderer = renderer;
2023-02-04 01:56:30 +01:00
log_add_callback(log_to_imgui, &ctx, LOG_TRACE);
int ret = doom_controller_loop(ctx);
2023-02-04 01:56:30 +01:00
nng_close(ctx.pub);
nng_close(ctx.sub);
2023-02-04 01:56:30 +01:00
2023-02-12 13:40:41 +01:00
return ret;
}