renamed project, search for config file next to binary and not in CWD

This commit is contained in:
mrbesen 2021-12-18 15:19:14 +01:00
parent d614316af6
commit 5d2beeb643
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
8 changed files with 46 additions and 13 deletions

View File

@ -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";
};

View File

@ -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:

View File

@ -4,9 +4,14 @@
#include <iostream>
#include <algorithm>
#include <stdlib.h> // realpath
#include <libgen.h> // dirname
#include <nlohmann/json.hpp>
using json = nlohmann::json;
#include <Log.h>
template<typename T>
static void readVector(std::vector<T>& v, const json& j) {
v.clear();
@ -14,7 +19,18 @@ static void readVector(std::vector<T>& v, const json& j) {
std::copy(j.begin(), j.end(), std::insert_iterator<std::vector<T>>(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;
}

View File

@ -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();

View File

@ -6,7 +6,7 @@
#include <Log.h>
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;
}

View File

@ -130,3 +130,9 @@ Sound::~Sound() {
ma_context_uninit(&context);
}
void Sound::backgroundThreadLoop() {
while(threadShouldrun) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}

View File

@ -47,7 +47,7 @@ void SoundDevice::sound_callback(void* outbuffer, ma_uint32 frameCount) {
unsigned int decoderCount = data.decoderCount;
unsigned int streamDivider = std::max<unsigned int>(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:";