From 5d2beeb6432deadc8733c8d4e889191a819d2130 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Sat, 18 Dec 2021 15:19:14 +0100 Subject: [PATCH] renamed project, search for config file next to binary and not in CWD --- include/config.h | 5 +++-- include/mainwindow.h | 2 +- testqt.pro => soundboard.pro | 0 src/config.cpp | 24 +++++++++++++++++++++--- src/main.cpp | 6 +++++- src/mainwindow.cpp | 10 +++++----- src/sound.cpp | 6 ++++++ src/sounddevice.cpp | 6 +++++- 8 files changed, 46 insertions(+), 13 deletions(-) rename testqt.pro => soundboard.pro (100%) diff --git a/include/config.h b/include/config.h index 519fa2d..4fa818e 100644 --- a/include/config.h +++ b/include/config.h @@ -6,7 +6,7 @@ class Config { public: - Config(); + Config(const std::string binaryArgument); ~Config(); bool hasChanged(); @@ -36,5 +36,6 @@ public: } rootConfig; private: - std::string file = "soundboard.json"; + std::string binaryPath; + const std::string file = "soundboard.json"; }; \ No newline at end of file diff --git a/include/mainwindow.h b/include/mainwindow.h index e483b98..5f5dafc 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -19,7 +19,7 @@ class MainWindow : public QMainWindow Q_OBJECT public: - MainWindow(QWidget *parent = nullptr); + MainWindow(const std::string& binary, QWidget *parent = nullptr); ~MainWindow(); public slots: diff --git a/testqt.pro b/soundboard.pro similarity index 100% rename from testqt.pro rename to soundboard.pro diff --git a/src/config.cpp b/src/config.cpp index 2a7d9cd..79f0b2f 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -4,9 +4,14 @@ #include #include +#include // realpath +#include // dirname + #include using json = nlohmann::json; +#include + template static void readVector(std::vector& v, const json& j) { v.clear(); @@ -14,7 +19,18 @@ static void readVector(std::vector& v, const json& j) { std::copy(j.begin(), j.end(), std::insert_iterator>(v, v.begin())); } -Config::Config() { +Config::Config(const std::string binaryArgument) { + char* buff = realpath(binaryArgument.c_str(), NULL); // buff needs free ! + //ssize_t read = readlink(binaryArgument.c_str(), buf, MAXBUF); + if(buff) { + // success + char* binPath = dirname(buff); + binaryPath = std::string(binPath) + '/'; + free(buff); + } else { + // error + Log::error << "unable to read path of the binaryFile"; + } } Config::~Config() { @@ -53,15 +69,17 @@ bool Config::ButtonConfig::isValid() const { } void Config::load() { - std::ifstream stream(file); + std::ifstream stream(binaryPath + file); json json; if(stream) { try { stream >> json; + rootConfig = json; } catch(nlohmann::detail::parse_error& pe) { std::cout << "json error: " << pe.what() << std::endl; return; } + } else { + Log::error << "config File not found"; } - rootConfig = json; } diff --git a/src/main.cpp b/src/main.cpp index 9fdbd06..9a0e32e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,12 +6,16 @@ int main(int argc, char *argv[]) { + if(argc < 1) { + return -1; + } + Log::init(); Log::setColoredOutput(true); Log::setConsoleLogLevel(Log::Level::TRACE); QApplication a(argc, argv); - MainWindow w; + MainWindow w(argv[0]); w.show(); int result = a.exec(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 76eaa27..09c18a5 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -6,7 +6,7 @@ #include -MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::Soundboard) { +MainWindow::MainWindow(const std::string& binary, QWidget* parent) : QMainWindow(parent), ui(new Ui::Soundboard), config(binary) { ui->setupUi(this); up = new QxtGlobalShortcut(QKeySequence("Shift+Up")); @@ -85,25 +85,25 @@ static uint8_t wrap0(uint8_t a, uint8_t max) { void MainWindow::moveUp() { Log::debug << "up"; - reselectNext([max = ui->gridLayout->rowCount()](uint8_t& x, uint8_t& y){ y = wrap(--y, max); }); + reselectNext([max = ui->gridLayout->rowCount()](uint8_t&, uint8_t& y){ y = wrap(--y, max); }); Log::debug << "x " << (int) xCoord << " y " << (int) yCoord; } void MainWindow::moveDown() { Log::debug << "down"; - reselectNext([max = ui->gridLayout->rowCount()](uint8_t& x, uint8_t& y){ y = wrap0(++y, max); }); + reselectNext([max = ui->gridLayout->rowCount()](uint8_t&, uint8_t& y){ y = wrap0(++y, max); }); Log::debug << "x " << (int) xCoord << " y " << (int) yCoord; } void MainWindow::moveLeft() { Log::debug << "left"; - reselectNext([max = ui->gridLayout->columnCount()](uint8_t& x, uint8_t& y){ x = wrap(--x, max); }); + reselectNext([max = ui->gridLayout->columnCount()](uint8_t& x, uint8_t&){ x = wrap(--x, max); }); Log::debug << "x " << (int) xCoord << " y " << (int) yCoord; } void MainWindow::moveRight() { Log::debug << "right"; - reselectNext([max = ui->gridLayout->columnCount()](uint8_t& x, uint8_t& y){ x = wrap0(++x, max); }); + reselectNext([max = ui->gridLayout->columnCount()](uint8_t& x, uint8_t&){ x = wrap0(++x, max); }); Log::debug << "x " << (int) xCoord << " y " << (int) yCoord; } diff --git a/src/sound.cpp b/src/sound.cpp index 3bdbb0d..1b30794 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -130,3 +130,9 @@ Sound::~Sound() { ma_context_uninit(&context); } +void Sound::backgroundThreadLoop() { + while(threadShouldrun) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + + } +} diff --git a/src/sounddevice.cpp b/src/sounddevice.cpp index c3bf2e9..e662fca 100644 --- a/src/sounddevice.cpp +++ b/src/sounddevice.cpp @@ -47,7 +47,7 @@ void SoundDevice::sound_callback(void* outbuffer, ma_uint32 frameCount) { unsigned int decoderCount = data.decoderCount; unsigned int streamDivider = std::max(decoderCount, MINSTREAMCOUNT); //nicht listplaybacks.size() verwenden, da manche playbacks möglicherweise noch fertig sind, aber noch nicht aus der list entfernt wurden. (wird von anderem thread gemacht) - unsigned int count = 0; //nummer des aktuellen decoders (nur für debugging) + // unsigned int count = 0; //nummer des aktuellen decoders (nur für debugging) for (SoundDevice::Playback* pb : data.playbacks) { if (pb->isDone) continue; //fertige encoder ignorieren @@ -96,6 +96,10 @@ SoundDevice* SoundDevice::createDevice(ma_context* ctx, const std::string& name) ma_device_info* pPlaybackDeviceInfos; ma_uint32 playbackDeviceCount; ma_result result = ma_context_get_devices(ctx, &pPlaybackDeviceInfos, &playbackDeviceCount, NULL, 0); + if(result != MA_SUCCESS) { + Log::error << "could not get sound device list"; + return nullptr; + } int8_t choosenDevice = -1; Log::info << " " << playbackDeviceCount << " playback devices found:";