Skip to content

Commit b7da56a

Browse files
committed
Add --watch cmdline option to watch for changes to audio.conf and auto-apply them
1 parent 0505346 commit b7da56a

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

src/MainWindow.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,11 @@ MainWindow::MainWindow(QString exepath,
143143
Log::information("If you want to use this application with PulseAudio, you need to recompile this app with proper support enabled.");
144144
Log::information("Refer to the README for more detailed information.");
145145
Log::information("");
146-
Log::debug("Blacklisted apps: " + AppConfig::instance().get<QString>(AppConfig::AudioAppBlocklist) /* explicitly use as QString here */);
146+
Log::debug("MainWindow::ctor: Blacklisted apps: " + AppConfig::instance().get<QString>(AppConfig::AudioAppBlocklist) /* explicitly use as QString here */);
147147
audioService = new PipewireAudioService();
148148
#endif
149149
connect(&DspConfig::instance(), &DspConfig::updated, audioService, &IAudioService::update);
150+
connect(&DspConfig::instance(), &DspConfig::updatedExternally, audioService, &IAudioService::update);
150151
}
151152

152153
// Prepare base UI

src/MainWindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class MainWindow :
5555
public:
5656
explicit MainWindow(QString exepath,
5757
bool statupInTray,
58-
bool allowMultipleInst,
58+
bool allowMultipleInst,
5959
QWidget *parent = nullptr);
6060
~MainWindow();
6161

src/config/DspConfig.h

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <QObject>
2525
#include <QMetaEnum>
26+
#include <QFileSystemWatcher>
2627

2728
using namespace std;
2829

@@ -94,18 +95,30 @@ class DspConfig :
9495
};
9596
Q_ENUM(Type)
9697

97-
static DspConfig &instance()
98+
static DspConfig &instance(bool watcherEnabled = false)
9899
{
99-
static DspConfig _instance;
100+
static DspConfig _instance(watcherEnabled);
100101
return _instance;
101102
}
102103

103104
DspConfig(DspConfig const &) = delete;
104105
void operator= (DspConfig const &) = delete;
105106

106-
DspConfig()
107+
DspConfig(bool watcherEnabled = false)
107108
{
108109
_conf = new ConfigContainer();
110+
111+
if(watcherEnabled)
112+
{
113+
Log::debug("DspConfig::ctor: File system watcher enabled");
114+
115+
_watcher = new QFileSystemWatcher(this);
116+
if(!_watcher->addPath(AppConfig::instance().getDspConfPath()))
117+
{
118+
Log::warning(QString("DspConfig::ctor: Failed to register path %0 with QFileSystemWatcher").arg(AppConfig::instance().getDspConfPath()));
119+
}
120+
connect(_watcher, &QFileSystemWatcher::fileChanged, this, &DspConfig::fileChanged);
121+
}
109122
}
110123

111124
void set(const Key &key,
@@ -222,6 +235,8 @@ class DspConfig :
222235
_conf->setConfigMap(map);
223236
emit configBuffered();
224237
emit updated(this);
238+
239+
save();
225240
}
226241

227242
void loadDefault()
@@ -237,15 +252,30 @@ class DspConfig :
237252
_conf->setConfigMap(map);
238253
emit configBuffered();
239254
emit updated(this);
255+
256+
save();
240257
}
241258
}
242259

243260
signals:
244261
void configBuffered();
245262
void updated(DspConfig* self);
263+
void updatedExternally(DspConfig* self);
264+
265+
private slots:
266+
void fileChanged(const QString &path)
267+
{
268+
if(_watcher->files().contains(path))
269+
{
270+
Log::debug("DspConfig::fileChanged: Config changed");
271+
load();
272+
emit updatedExternally(this);
273+
}
274+
}
246275

247276
private:
248277
ConfigContainer *_conf;
278+
QFileSystemWatcher *_watcher;
249279
};
250280

251281
#endif // DSPCONFIGWRAPPER_H

src/main.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "MainWindow.h"
2+
#include "config/DspConfig.h"
23

34
#include "utils/FindBinary.h"
45

@@ -65,8 +66,10 @@ int main(int argc,
6566
parser.setApplicationDescription("JamesDSP for Linux");
6667
parser.addHelpOption();
6768

68-
QCommandLineOption tray(QStringList() << "t" << "tray", "Start minimized in systray (forced)");
69-
parser.addOption(tray);
69+
QCommandLineOption tray(QStringList() << "t" << "tray", "Start minimized in systray (forced)");
70+
parser.addOption(tray);
71+
QCommandLineOption watch(QStringList() << "w" << "watch", "Watch audio.conf and apply changes made by external apps automatically");
72+
parser.addOption(watch);
7073
QCommandLineOption minst(QStringList() << "m" << "allow-multiple-instances", "Allow multiple instances of this app");
7174
parser.addOption(minst);
7275
QCommandLineOption spinlck(QStringList() << "d" << "spinlock-on-crash", "Wait for debugger in case of crash");
@@ -81,8 +84,11 @@ int main(int argc,
8184
setlocale(LC_NUMERIC, "C");
8285
}
8386

87+
// Prepare DspConfig based on cmdline argument
88+
DspConfig::instance(parser.isSet(watch));
89+
8490
QApplication::setQuitOnLastWindowClosed(false);
85-
MainWindow w(QString::fromLocal8Bit(exepath), parser.isSet(tray), parser.isSet(minst));
91+
MainWindow w(QString::fromLocal8Bit(exepath), parser.isSet(tray), parser.isSet(minst));
8692
w.setFixedSize(w.geometry().width(), w.geometry().height());
8793
w.setGeometry(
8894
QStyle::alignedRect(

0 commit comments

Comments
 (0)