From df9cf1c14a4fa1a6854131160cc4806b2b4ea3e0 Mon Sep 17 00:00:00 2001 From: oxmox Date: Sun, 19 Feb 2023 16:23:29 +0100 Subject: [PATCH] intermittent ctrl commit: fork() based spawn code which makes nng panic --- src/doompanning.cc | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/doompanning.cc b/src/doompanning.cc index eb5891a..d4677d9 100644 --- a/src/doompanning.cc +++ b/src/doompanning.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include "dp_common.h" #include "dp_util.hpp" @@ -56,7 +57,7 @@ struct ControllerActions #define DOOM_EXECUTABLE "dp_doom" -void spawn_doom(ControllerContext &ctx) +void spawn_doom_posix_spawn(ControllerContext &ctx) { DoomState ds; @@ -64,7 +65,8 @@ void spawn_doom(ControllerContext &ctx) // TODO: Close stdin and stdout? Leave them open for now to see the logging // output. - // FIXME: this doesn't work with SDL :( + // FIXME: SDL is not able to init its sound system in the doomchild. Try to ask + // it if it's already initialized and skip if true. if (auto err = posix_spawn(&ds.id, DOOM_EXECUTABLE, nullptr, nullptr, const_cast(argv), nullptr)) { @@ -82,6 +84,45 @@ void spawn_doom(ControllerContext &ctx) ctx.dooms.emplace_back(ds); } +// nng does not like this at all and panics (for good reasons) +void spawn_doom_fork(ControllerContext &ctx) +{ + auto pid = fork(); + + if (pid < 0) + { + log_error("Could not spawn doom: %s", strerror(errno)); + return; + } + + if (pid == 0) // doomchild + { + log_info("doomchild %d cleaning the room", getpid()); + ImGui::DestroyContext(); + log_info("doomchild %d cleanup done", getpid()); + + const char *const argv[] = { DOOM_EXECUTABLE, nullptr }; + execvp(DOOM_EXECUTABLE, const_cast(argv)); + } + else if (pid > 0) // doomparent + { + DoomState ds; + ds.id = pid; + ds.texture = SDL_CreateTexture(ctx.renderer, SDL_PIXELFORMAT_RGB888, + SDL_TEXTUREACCESS_STREAMING, DoomScreenWidth, DoomScreenHeight); + if (!ds.texture) + dp_sdl_fatal("SDL_CreateTexture"); + + log_info("Spawned new doom, pid=%d", ds.id); + ctx.dooms.emplace_back(ds); + } +} + +inline void spawn_doom(ControllerContext &ctx) +{ + spawn_doom_posix_spawn(ctx); +} + void end_all_dooms(ControllerContext &ctx) { nng_msg *msg = nullptr;