From bb56941f6b61529dd770475296d093727c68fdc6 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 17 Jan 2025 21:02:51 +0100 Subject: Automatically use correct files path (executable path or /media/usb) --- MIDIPlayer.cpp | 9 ++++++--- MIDIPlayer.h | 5 ++++- Makefile | 3 ++- config.cpp | 26 ++++++++++++++++++++++++++ config.h | 13 +++++++++++++ midiplay.cpp | 5 ++++- 6 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 config.cpp create mode 100644 config.h diff --git a/MIDIPlayer.cpp b/MIDIPlayer.cpp index 2c0bc6a..8202850 100644 --- a/MIDIPlayer.cpp +++ b/MIDIPlayer.cpp @@ -1,5 +1,7 @@ #include "MIDIPlayer.h" +#include "config.h" + #include #include @@ -16,12 +18,13 @@ namespace fs = std::filesystem; using namespace std::chrono_literals; namespace { - std::unordered_set supported_devices{"AudioBox 22 VSL", "CH345"}; + std::unordered_set supported_devices{"AudioBox 22 VSL", "CH345", "M2"}; } -MIDIPlayer::MIDIPlayer(const std::filesystem::path& path): +MIDIPlayer::MIDIPlayer(Config& config): + m_config(config), m_child{}, - m_dir{path}, + m_dir{m_config.get_file_path()}, m_file{} { std::vector list = get_filelist(); diff --git a/MIDIPlayer.h b/MIDIPlayer.h index 811bea4..8009029 100644 --- a/MIDIPlayer.h +++ b/MIDIPlayer.h @@ -6,12 +6,14 @@ #include #include +#include "config.h" + #include class MIDIPlayer { public: - MIDIPlayer(const std::filesystem::path& path = "."); + MIDIPlayer(Config& config); void start(); @@ -32,6 +34,7 @@ private: void close_seq(void); void iterate_ports(void); + Config& m_config; boost::process::child m_child; std::filesystem::path m_dir; std::string m_file; diff --git a/Makefile b/Makefile index 70a1928..3b36a8b 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,8 @@ TARGET=midiplay SRCS=\ midiplay.cpp \ - MIDIPlayer.cpp + MIDIPlayer.cpp \ + config.cpp \ OBJS=$(SRCS:.cpp=.o) diff --git a/config.cpp b/config.cpp new file mode 100644 index 0000000..beb7f97 --- /dev/null +++ b/config.cpp @@ -0,0 +1,26 @@ +#include "config.h" + +#include +#include + +namespace fs = std::filesystem; + +Config::Config(int argc, char* argv[]) +{ + m_executable_path = fs::path{argv[0]}.parent_path().string(); +} + +std::string Config::get_executable_path() +{ + return m_executable_path; +} + +std::string Config::get_file_path() +{ + // Just a heuristic: If this exists in current dir, then use this path + if (fs::exists(fs::path{m_executable_path} / "magic1.midi")) { + return m_executable_path; + } + return "/media/usb"; +} + diff --git a/config.h b/config.h new file mode 100644 index 0000000..d0520dd --- /dev/null +++ b/config.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +class Config { + public: + Config(int argc, char* argv[]); + std::string get_executable_path(); + std::string get_file_path(); + + private: + std::string m_executable_path; +}; diff --git a/midiplay.cpp b/midiplay.cpp index bc1ad08..36b732e 100644 --- a/midiplay.cpp +++ b/midiplay.cpp @@ -1,5 +1,7 @@ #include "MIDIPlayer.h" +#include "config.h" + #include #include #include @@ -81,7 +83,8 @@ std::string to_xml(const std::vector& filelist, const std::string& int main(int argc, char* argv[]) { try { - MIDIPlayer player; + Config config{argc, argv}; + MIDIPlayer player{config}; std::string ok_data{"okOK"}; std::string error_data{"errorGeneral Error"}; -- cgit v1.2.3