From e9bcdb25ffdd60e1c498565dd5d619e9b2951c76 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Sun, 23 May 2021 20:02:45 +0100 Subject: [PATCH 1/5] Use ISO format when writing date to log. Signed-off-by: Andrea Odetti --- source/linux/windows/time.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/source/linux/windows/time.cpp b/source/linux/windows/time.cpp index 397a1f59..acadbf59 100644 --- a/source/linux/windows/time.cpp +++ b/source/linux/windows/time.cpp @@ -22,10 +22,10 @@ DWORD timeGetTime() /// Returns the number of ticks since an undefined time (usually system startup). DWORD GetTickCount() { - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - const uint64_t ticks = (uint64_t)(ts.tv_nsec / 1000000) + ((uint64_t)ts.tv_sec * 1000ull); - return ticks; + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + const uint64_t ticks = (uint64_t)(ts.tv_nsec / 1000000) + ((uint64_t)ts.tv_sec * 1000ull); + return ticks; } void GetLocalTime(SYSTEMTIME *t) @@ -48,18 +48,18 @@ void GetLocalTime(SYSTEMTIME *t) t->wYear = local->tm_year; } -int GetDateFormat(LCID Locale, DWORD dwFlags, CONST SYSTEMTIME *lpDate, LPCSTR lpFormat, LPSTR lpDateStr, int cchDate) +int GetDateFormat(LCID /* Locale */, DWORD /* dwFlags */, CONST SYSTEMTIME * /* lpDate */, LPCSTR /* lpFormat */, LPSTR lpDateStr, int cchDate) { std::time_t t = std::time(nullptr); std::tm tm = *std::localtime(&t); std::ostringstream ss; - ss << std::put_time(&tm, "%D"); + ss << std::put_time(&tm, "%F"); const std::string str = ss.str(); strncpy(lpDateStr, str.c_str(), cchDate); return cchDate; // not 100% sure, but it is never used } -int GetTimeFormat(LCID Locale, DWORD dwFlags, CONST SYSTEMTIME *lpTime, LPCSTR lpFormat, LPSTR lpTimeStr, int cchTime) +int GetTimeFormat(LCID /* Locale */, DWORD /* dwFlags */, CONST SYSTEMTIME * /* lpTime */, LPCSTR /* lpFormat */, LPSTR lpTimeStr, int cchTime) { std::time_t t = std::time(nullptr); std::tm tm = *std::localtime(&t); From 535ceaadc6027370216141a47f387a7ec30384a9 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Sun, 23 May 2021 20:03:09 +0100 Subject: [PATCH 2/5] Ensure LogFileTimeUntilFirstKeyReadReset() gets called after every reboot. https://github.com/audetto/AppleWin/issues/30 Signed-off-by: Andrea Odetti --- source/linux/linuxframe.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/linux/linuxframe.cpp b/source/linux/linuxframe.cpp index 980d8d26..cdbc9cf7 100644 --- a/source/linux/linuxframe.cpp +++ b/source/linux/linuxframe.cpp @@ -2,6 +2,7 @@ #include "linux/linuxframe.h" #include "Interface.h" #include "Log.h" +#include "Core.h" void LinuxFrame::FrameDrawDiskLEDS() { @@ -54,6 +55,7 @@ void LinuxFrame::Initialize() const size_t numberOfBytes = sizeof(bgra_t) * numberOfPixels; myFramebuffer.resize(numberOfBytes); video.Initialize(myFramebuffer.data()); + LogFileTimeUntilFirstKeyReadReset(); } void LinuxFrame::Destroy() From 09da101cbb1da90b4b6034a7cfb7df6c9fe0f500 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Sun, 23 May 2021 20:04:59 +0100 Subject: [PATCH 3/5] FileRegistry: fix problem with conf file not created. https://github.com/audetto/AppleWin/issues/30 Signed-off-by: Andrea Odetti --- source/frontends/common2/fileregistry.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/frontends/common2/fileregistry.cpp b/source/frontends/common2/fileregistry.cpp index f040cb78..1176459e 100644 --- a/source/frontends/common2/fileregistry.cpp +++ b/source/frontends/common2/fileregistry.cpp @@ -60,7 +60,6 @@ namespace } else { - mySaveOnExit = false; LogFileOutput("Registry: configuration file '%s' not found\n", filename.c_str()); } } @@ -69,7 +68,14 @@ namespace { if (mySaveOnExit) { - boost::property_tree::ini_parser::write_ini(myFilename, myINI); + try + { + boost::property_tree::ini_parser::write_ini(myFilename, myINI); + } + catch(const std::exception& e) + { + LogFileOutput("Registry: cannot save settings to '%s': %s\n", myFilename.c_str(), e.what()); + } } } From 7b09bacb0117eaf8f208d186bca98333404e8cc0 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Sun, 23 May 2021 20:06:36 +0100 Subject: [PATCH 4/5] Initialisation: some fixes. Ensure log file is created before it is used the first time. Only close the log file once at the end. https://github.com/audetto/AppleWin/issues/30 Signed-off-by: Andrea Odetti --- source/frontends/common2/utils.cpp | 2 +- source/frontends/libretro/game.cpp | 8 +++++--- source/frontends/libretro/game.h | 5 ++++- source/frontends/libretro/libretro.cpp | 4 +--- source/frontends/ncurses/main.cpp | 11 +++-------- source/frontends/sdl/main.cpp | 18 +++++------------- source/linux/context.cpp | 23 +++++++++++++++++++++-- source/linux/context.h | 9 +++++++++ 8 files changed, 49 insertions(+), 31 deletions(-) diff --git a/source/frontends/common2/utils.cpp b/source/frontends/common2/utils.cpp index cb35072f..b64e6e4f 100644 --- a/source/frontends/common2/utils.cpp +++ b/source/frontends/common2/utils.cpp @@ -123,9 +123,9 @@ namespace common2 HD_Destroy(); PrintDestroy(); CpuDestroy(); + DebugDestroy(); GetCardMgr().GetDisk2CardMgr().Destroy(); - LogDone(); RiffFinishWriteFile(); } diff --git a/source/frontends/libretro/game.cpp b/source/frontends/libretro/game.cpp index 3f880525..14706652 100644 --- a/source/frontends/libretro/game.cpp +++ b/source/frontends/libretro/game.cpp @@ -54,10 +54,12 @@ namespace ra2 unsigned Game::ourInputDevices[MAX_PADS] = {RETRO_DEVICE_NONE}; - Game::Game(const std::shared_ptr & frame) - : myFrame(frame), mySpeed(true), myButtonStates(RETRO_DEVICE_ID_JOYPAD_R3 + 1) + Game::Game() + : myLogger(true) + , myFrame(new ra2::RetroFrame()) + , mySpeed(true) + , myButtonStates(RETRO_DEVICE_ID_JOYPAD_R3 + 1) { - LogInit(); InitialiseRetroRegistry(); SetFrame(myFrame); myFrame->Initialize(); diff --git a/source/frontends/libretro/game.h b/source/frontends/libretro/game.h index 386f7915..7f831cee 100644 --- a/source/frontends/libretro/game.h +++ b/source/frontends/libretro/game.h @@ -3,6 +3,8 @@ #include "frontends/common2/speed.h" #include "frontends/libretro/environment.h" +#include "linux/context.h" + #include #include @@ -14,7 +16,7 @@ namespace ra2 class Game { public: - Game(const std::shared_ptr & frame); + Game(); ~Game(); bool loadGame(const std::string & path); @@ -33,6 +35,7 @@ namespace ra2 static retro_usec_t ourFrameTime; private: + const Logger myLogger; const std::shared_ptr myFrame; common2::Speed mySpeed; // fixed speed diff --git a/source/frontends/libretro/libretro.cpp b/source/frontends/libretro/libretro.cpp index 1d6e3d59..5a2fb8f1 100644 --- a/source/frontends/libretro/libretro.cpp +++ b/source/frontends/libretro/libretro.cpp @@ -14,7 +14,6 @@ #include "frontends/libretro/environment.h" #include "frontends/libretro/rdirectsound.h" #include "frontends/libretro/retroregistry.h" -#include "frontends/libretro/retroframe.h" namespace { @@ -180,8 +179,7 @@ bool retro_load_game(const retro_game_info *info) try { - std::shared_ptr frame(new ra2::RetroFrame()); - std::unique_ptr game(new ra2::Game(frame)); + std::unique_ptr game(new ra2::Game()); const std::string snapshotEnding = ".aws.yaml"; const std::string gamePath = info->path; diff --git a/source/frontends/ncurses/main.cpp b/source/frontends/ncurses/main.cpp index 8fdbf253..584b5822 100644 --- a/source/frontends/ncurses/main.cpp +++ b/source/frontends/ncurses/main.cpp @@ -132,7 +132,6 @@ namespace void EnterMessageLoop(const common2::EmulatorOptions & options, const std::shared_ptr & frame) { - LogFileTimeUntilFirstKeyReadReset(); while (ContinueExecution(options, frame)) { } @@ -146,16 +145,12 @@ namespace if (!run) return 1; - if (options.log) - { - LogInit(); - } - + const Logger logger(options.log); InitializeFileRegistry(options); g_nMemoryClearType = options.memclear; - std::shared_ptr frame(new na2::NFrame(options.paddleDeviceName)); - Initialisation init(frame); + const std::shared_ptr frame(new na2::NFrame(options.paddleDeviceName)); + const Initialisation init(frame); na2::SetCtrlCHandler(options.headless); applyOptions(options); diff --git a/source/frontends/sdl/main.cpp b/source/frontends/sdl/main.cpp index 86e9847d..dbf89c78 100644 --- a/source/frontends/sdl/main.cpp +++ b/source/frontends/sdl/main.cpp @@ -90,18 +90,11 @@ void run_sdl(int argc, const char * argv []) if (!run) return; + const Logger logger(options.log); + g_nMemoryClearType = options.memclear; InitializeFileRegistry(options); - if (options.log) - { - LogInit(); - } - - g_nMemoryClearType = options.memclear; - Paddle::instance.reset(new sa2::Gamepad(0)); - std::shared_ptr frame; - if (options.imgui) { frame.reset(new sa2::SDLImGuiFrame(options)); @@ -111,13 +104,14 @@ void run_sdl(int argc, const char * argv []) frame.reset(new sa2::SDLRendererFrame(options)); } + Paddle::instance.reset(new sa2::Gamepad(0)); + const Initialisation init(frame); + if (SDL_GL_SetSwapInterval(options.glSwapInterval)) { throw std::runtime_error(SDL_GetError()); } - Initialisation init(frame); - applyOptions(options); const int fps = getRefreshRate(); @@ -230,8 +224,6 @@ int main(int argc, const char * argv []) std::cerr << e.what() << std::endl; } - // this must happen BEFORE the SDL_Quit() as otherwise we have a double free (of the game controller). - Paddle::instance.reset(); SDL_Quit(); return exit; diff --git a/source/linux/context.cpp b/source/linux/context.cpp index a9c5ab17..1fe950d8 100644 --- a/source/linux/context.cpp +++ b/source/linux/context.cpp @@ -1,10 +1,13 @@ #include "StdAfx.h" #include "linux/context.h" +#include "linux/linuxframe.h" +#include "linux/registry.h" +#include "linux/paddle.h" +#include "linux/duplicates/PropertySheet.h" #include "Interface.h" -#include "linux/duplicates/PropertySheet.h" -#include "linux/linuxframe.h" +#include "Log.h" namespace { @@ -43,4 +46,20 @@ Initialisation::~Initialisation() { GetFrame().Destroy(); SetFrame(std::shared_ptr()); + + Paddle::instance.reset(); + Registry::instance.reset(); +} + +Logger::Logger(const bool log) +{ + if (log) + { + LogInit(); + } +} + +Logger::~Logger() +{ + LogDone(); } diff --git a/source/linux/context.h b/source/linux/context.h index 61ad6cd2..a9bce884 100644 --- a/source/linux/context.h +++ b/source/linux/context.h @@ -6,9 +6,18 @@ class FrameBase; void SetFrame(const std::shared_ptr & frame); +// RAII around Frame Registry and Paddle class Initialisation { public: Initialisation(const std::shared_ptr & frame); ~Initialisation(); }; + +// RAII around LogInit / LogDone. +class Logger +{ +public: + Logger(const bool log); + ~Logger(); +}; From bde3eb92b0a31241125865d07d3a92ef340bd7e4 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Mon, 24 May 2021 09:22:01 +0100 Subject: [PATCH 5/5] Improve symmetry of Constructor/Destructor functions. Signed-off-by: Andrea Odetti --- source/frontends/common2/fileregistry.cpp | 4 ++-- source/frontends/common2/fileregistry.h | 5 ++++- source/frontends/libretro/game.cpp | 2 +- source/frontends/libretro/retroregistry.cpp | 4 ++-- source/frontends/libretro/retroregistry.h | 6 +++++- source/frontends/ncurses/main.cpp | 9 ++++++--- source/frontends/ncurses/nframe.cpp | 10 ++-------- source/frontends/ncurses/nframe.h | 6 +++--- source/frontends/sdl/main.cpp | 6 +++--- source/linux/context.cpp | 9 ++++++++- source/linux/context.h | 8 +++++++- 11 files changed, 43 insertions(+), 26 deletions(-) diff --git a/source/frontends/common2/fileregistry.cpp b/source/frontends/common2/fileregistry.cpp index 1176459e..6a70fbb0 100644 --- a/source/frontends/common2/fileregistry.cpp +++ b/source/frontends/common2/fileregistry.cpp @@ -110,7 +110,7 @@ namespace common2 } } - void InitializeFileRegistry(const EmulatorOptions & options) + std::shared_ptr CreateFileRegistry(const EmulatorOptions & options) { const std::string homeDir = getHomeDir(); @@ -131,7 +131,7 @@ namespace common2 std::shared_ptr config(new Configuration(filename, saveOnExit)); config->addExtraOptions(options.registryOptions); - Registry::instance = config; + return config; } } diff --git a/source/frontends/common2/fileregistry.h b/source/frontends/common2/fileregistry.h index 4d51a665..4fc73bde 100644 --- a/source/frontends/common2/fileregistry.h +++ b/source/frontends/common2/fileregistry.h @@ -1,6 +1,9 @@ #pragma once #include +#include + +class Registry; namespace common2 { @@ -8,6 +11,6 @@ namespace common2 struct EmulatorOptions; std::string GetConfigFile(const std::string & filename); - void InitializeFileRegistry(const EmulatorOptions & options); + std::shared_ptr CreateFileRegistry(const EmulatorOptions & options); } diff --git a/source/frontends/libretro/game.cpp b/source/frontends/libretro/game.cpp index 14706652..b512d0dd 100644 --- a/source/frontends/libretro/game.cpp +++ b/source/frontends/libretro/game.cpp @@ -60,7 +60,7 @@ namespace ra2 , mySpeed(true) , myButtonStates(RETRO_DEVICE_ID_JOYPAD_R3 + 1) { - InitialiseRetroRegistry(); + Registry::instance = CreateRetroRegistry(); SetFrame(myFrame); myFrame->Initialize(); diff --git a/source/frontends/libretro/retroregistry.cpp b/source/frontends/libretro/retroregistry.cpp index da3f989d..30b65fd9 100644 --- a/source/frontends/libretro/retroregistry.cpp +++ b/source/frontends/libretro/retroregistry.cpp @@ -133,7 +133,7 @@ namespace ra2 environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, retroVariables.data()); } - void InitialiseRetroRegistry() + std::shared_ptr CreateRetroRegistry() { const auto registry = std::make_shared(); @@ -158,7 +158,7 @@ namespace ra2 } } - Registry::instance = registry; + return registry; } } diff --git a/source/frontends/libretro/retroregistry.h b/source/frontends/libretro/retroregistry.h index 1e21d09d..64580295 100644 --- a/source/frontends/libretro/retroregistry.h +++ b/source/frontends/libretro/retroregistry.h @@ -1,9 +1,13 @@ #pragma once +#include + +class Registry; + namespace ra2 { void SetupRetroVariables(); - void InitialiseRetroRegistry(); + std::shared_ptr CreateRetroRegistry(); } diff --git a/source/frontends/ncurses/main.cpp b/source/frontends/ncurses/main.cpp index 584b5822..53a1c59b 100644 --- a/source/frontends/ncurses/main.cpp +++ b/source/frontends/ncurses/main.cpp @@ -24,6 +24,7 @@ #include "frontends/common2/utils.h" #include "frontends/ncurses/world.h" #include "frontends/ncurses/nframe.h" +#include "frontends/ncurses/evdevpaddle.h" namespace { @@ -146,11 +147,13 @@ namespace return 1; const Logger logger(options.log); - InitializeFileRegistry(options); + const std::shared_ptr registry = CreateFileRegistry(options); g_nMemoryClearType = options.memclear; - const std::shared_ptr frame(new na2::NFrame(options.paddleDeviceName)); - const Initialisation init(frame); + const std::shared_ptr paddle(new na2::EvDevPaddle(options.paddleDeviceName)); + + const std::shared_ptr frame(new na2::NFrame(paddle)); + const Initialisation init(registry, frame, paddle); na2::SetCtrlCHandler(options.headless); applyOptions(options); diff --git a/source/frontends/ncurses/nframe.cpp b/source/frontends/ncurses/nframe.cpp index e3f0f24f..c2231a9f 100644 --- a/source/frontends/ncurses/nframe.cpp +++ b/source/frontends/ncurses/nframe.cpp @@ -3,7 +3,6 @@ #include "frontends/ncurses/colors.h" #include "frontends/ncurses/asciiart.h" #include "frontends/ncurses/evdevpaddle.h" - #include "Interface.h" #include "Memory.h" #include "Log.h" @@ -39,8 +38,8 @@ namespace na2 std::shared_ptr colors; }; - NFrame::NFrame(const std::string & paddleDevice) - : myPaddleDevice(paddleDevice) + NFrame::NFrame(const std::shared_ptr & paddle) + : myPaddle(paddle) , myRows(-1) , myColumns(-1) { @@ -54,8 +53,6 @@ namespace na2 myTextFlashCounter = 0; myTextFlashState = 0; myAsciiArt.reset(new ASCIIArt()); - myPaddle.reset(new EvDevPaddle(myPaddleDevice)); - Paddle::instance = myPaddle; } void NFrame::Destroy() @@ -67,9 +64,6 @@ namespace na2 myStatus.reset(); myAsciiArt.reset(); - myPaddle.reset(); - Paddle::instance.reset(); - myNCurses.reset(); } diff --git a/source/frontends/ncurses/nframe.h b/source/frontends/ncurses/nframe.h index eab965b8..fdfb912c 100644 --- a/source/frontends/ncurses/nframe.h +++ b/source/frontends/ncurses/nframe.h @@ -16,7 +16,7 @@ namespace na2 class NFrame : public common2::CommonFrame { public: - NFrame(const std::string & paddleDevice); + NFrame(const std::shared_ptr & paddle); WINDOW * GetWindow(); WINDOW * GetStatus(); @@ -35,7 +35,8 @@ namespace na2 private: - const std::string myPaddleDevice; + const std::shared_ptr myPaddle; + int myRows; int myColumns; int myTextFlashCounter; @@ -44,7 +45,6 @@ namespace na2 std::shared_ptr myFrame; std::shared_ptr myStatus; std::shared_ptr myAsciiArt; - std::shared_ptr myPaddle; std::shared_ptr myNCurses; LPBYTE myTextBank1; // Aux diff --git a/source/frontends/sdl/main.cpp b/source/frontends/sdl/main.cpp index dbf89c78..cb5d5607 100644 --- a/source/frontends/sdl/main.cpp +++ b/source/frontends/sdl/main.cpp @@ -91,8 +91,8 @@ void run_sdl(int argc, const char * argv []) return; const Logger logger(options.log); + const std::shared_ptr registry = CreateFileRegistry(options); g_nMemoryClearType = options.memclear; - InitializeFileRegistry(options); std::shared_ptr frame; if (options.imgui) @@ -104,8 +104,8 @@ void run_sdl(int argc, const char * argv []) frame.reset(new sa2::SDLRendererFrame(options)); } - Paddle::instance.reset(new sa2::Gamepad(0)); - const Initialisation init(frame); + std::shared_ptr paddle(new sa2::Gamepad(0)); + const Initialisation init(registry, frame, paddle); if (SDL_GL_SetSwapInterval(options.glSwapInterval)) { diff --git a/source/linux/context.cpp b/source/linux/context.cpp index 1fe950d8..00ad45aa 100644 --- a/source/linux/context.cpp +++ b/source/linux/context.cpp @@ -36,9 +36,16 @@ Video& GetVideo() return sg_Video; } -Initialisation::Initialisation(const std::shared_ptr & frame) +Initialisation::Initialisation( + const std::shared_ptr & registry, + const std::shared_ptr & frame, + const std::shared_ptr & paddle + ) { + Registry::instance = registry; SetFrame(frame); + Paddle::instance = paddle; + frame->Initialize(); } diff --git a/source/linux/context.h b/source/linux/context.h index a9bce884..7bcac22f 100644 --- a/source/linux/context.h +++ b/source/linux/context.h @@ -3,6 +3,8 @@ #include class FrameBase; +class Paddle; +class Registry; void SetFrame(const std::shared_ptr & frame); @@ -10,7 +12,11 @@ void SetFrame(const std::shared_ptr & frame); class Initialisation { public: - Initialisation(const std::shared_ptr & frame); + Initialisation( + const std::shared_ptr & registry, + const std::shared_ptr & frame, + const std::shared_ptr & paddle + ); ~Initialisation(); };