-
Notifications
You must be signed in to change notification settings - Fork 18
/
viper_window.cpp
executable file
·1571 lines (1412 loc) · 64.4 KB
/
viper_window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "viper_window.h"
#include "ui_viper_window.h"
#include "misc/overlaymsgproxy.h"
#include "misc/versioncontainer.h"
#include "dialog/liquidequalizerwidget.h"
#include "misc/eventfilter.h"
#include "dialog/logdlg.h"
#include "misc/stylehelper.h"
#include "config/appconfigwrapper.h"
#include "misc/mathfunctions.h"
#include "misc/loghelper.h"
#include "misc/presetprovider.h"
#include "config/io.h"
#include "config/container.h"
#include "dialog/settingsdlg.h"
#include "dialog/presetdlg.h"
#include "dialog/convolverdlg.h"
#include "misc/converter.h"
#include "misc/common.h"
#include "config/container.h"
#include "config/io.h"
#ifndef VIPER_PLUGINMODE
#include "dbus/serveradaptor.h"
#include "dbus/clientproxy.h"
#include "dialog/firstlaunchwizard.h"
#include "dialog/statusfragment.h"
#include <phantomstyle.h>
#endif
#include "misc/GstRegistryHelper.h"
#include "3rdparty/WAF/Animation/Animation.h"
#include <QTimer>
#include <QMenu>
#include <QMessageBox>
#include <QFileDialog>
#include <QWhatsThis>
#include <QSystemTrayIcon>
#include <QCloseEvent>
#include <QFrame>
#include <QGraphicsOpacityEffect>
#include <QDebug>
#include <cmath>
#include <string>
#include <sstream>
#include <fstream>
#include <dialog/qmessageoverlay.h>
using namespace std;
#ifdef VIPER_PLUGINMODE
ViperWindow::ViperWindow(QString working_dir, QWidget *parent) :
QWidget(parent),
#else
ViperWindow::ViperWindow(QString exepath, bool statupInTray, bool allowMultipleInst, QWidget *parent) :
QWidget(parent),
#endif
ui(new Ui::ViperWindow)
{
Q_INIT_RESOURCE(v4l_frontend_resources);
Q_INIT_RESOURCE(v4l_frontend_styles);
ui->setupUi(this);
bool aboutToQuit = false;
LogHelper::clearLog();
LogHelper::writeLog("UI launched...");
connect(ui->popupButton, &QAbstractButton::clicked, this, &ViperWindow::popupButtonPressed);
#ifdef VIPER_PLUGINMODE
/* Attach animation parent to the topmost widget */
this->setProperty("animation_filter", true);
this->layout()->setMargin(0);
ui->popupButton->setIcon(QPixmap(":/resources/icon/checkout.svg"));
#else
ui->popupButton->hide();
m_exepath = exepath;
m_startupInTraySwitch = statupInTray;
tray_disableAction = new QAction();
msg_launchfail = new OverlayMsgProxy(this);
msg_versionmismatch = new OverlayMsgProxy(this);
#endif
msg_notrunning = new OverlayMsgProxy(this);
conf = new ConfigContainer();
m_stylehelper = new StyleHelper(this);
#ifndef VIPER_PLUGINMODE
m_dbus = new DBusProxy();
m_appwrapper = new AppConfigWrapper(m_stylehelper);
#else
m_appwrapper = new AppConfigWrapper(m_stylehelper, working_dir);
#endif
m_appwrapper->loadAppConfig();
#ifndef VIPER_PLUGINMODE
InitializeSpectrum();
/* Load audio.conf */
conf->setConfigMap(readConfig());
LoadConfig();
#else
/* Load defaults and wait for update from proxy */
conf->setConfigMap(ConfigIO::readString(QString::fromStdString(default_config)));
LoadConfig();
#endif
conv_dlg = new ConvolverDlg(this,this);
preset_dlg = new PresetDlg(this);
#ifndef VIPER_PLUGINMODE
createTrayIcon();
initGlobalTrayActions();
#endif
settings_dlg = new SettingsDlg(this,this);
log_dlg = new LogDlg(this);
#ifndef VIPER_PLUGINMODE
//This section checks if another instance is already running and switches to it.
new GuiAdaptor(this);
QDBusConnection connection = QDBusConnection::sessionBus();
bool serviceRegistrationSuccessful = connection.registerObject("/Gui", this);
bool objectRegistrationSuccessful = connection.registerService("cf.thebone.viper4linux.Gui");
if(serviceRegistrationSuccessful && objectRegistrationSuccessful)
LogHelper::writeLog("DBus service registration successful");
else{
LogHelper::writeLog("DBus service registration failed. Name already aquired by other instance");
if(!allowMultipleInst){
LogHelper::writeLog("Attempting to switch to this instance...");
auto m_dbInterface = new cf::thebone::viper4linux::Gui("cf.thebone.viper4linux.Gui", "/Gui",
QDBusConnection::sessionBus(), this);
if(!m_dbInterface->isValid())
LogHelper::writeLog("Critical: Unable to connect to other DBus instance. Continuing anyway...");
else{
QDBusPendingReply<> msg = m_dbInterface->raiseWindow();
if(msg.isError() || !msg.isValid()){
LogHelper::writeLog("Critical: Other DBus instance returned invalid or error message. Continuing anyway...");
}
else{
aboutToQuit = true;
LogHelper::writeLog("Success! Waiting for event loop to exit...");
QTimer::singleShot(0, qApp, &QCoreApplication::quit);
}
}
}
}
#endif
//Cancel constructor if quitting soon
if(aboutToQuit) return;
QMenu *menu = new QMenu();
#ifndef VIPER_PLUGINMODE
spectrum = new QAction(tr("Reload spectrum"),this);
connect(spectrum,&QAction::triggered,this,&ViperWindow::RestartSpectrum);
menu->addAction(tr("Reload viper"), this,SLOT(Restart()));
menu->addAction(spectrum);
menu->addAction(tr("Driver status"), this,[this](){
if(!GstRegistryHelper::HasDBusSupport()){
OverlayMsgProxy* msg = new OverlayMsgProxy(this);
msg->openError(tr("Unavailable"),
tr("Viper4Linux Legacy does not support this feature.\n"
"Please upgrade to a later version of Viper4Linux\n"
"to access this dialog (Audio4Linux version)."),
tr("Go back"));
return;
}
if(!m_dbus->isValid()){
ShowDBusError();
return;
}
StatusDialog* sd = new StatusDialog(m_dbus);
QWidget* host = new QWidget(this);
host->setProperty("menu", false);
QVBoxLayout* hostLayout = new QVBoxLayout(host);
hostLayout->addWidget(sd);
host->hide();
host->setAutoFillBackground(true);
connect(sd,&StatusDialog::closePressed,this,[host](){
WAF::Animation::sideSlideOut(host, WAF::BottomSide);
});
WAF::Animation::sideSlideIn(host, WAF::BottomSide);
});
#endif
menu->addAction(tr("Load from file"), this,SLOT(LoadExternalFile()));
menu->addAction(tr("Save to file"), this,SLOT(SaveExternalFile()));
#ifndef VIPER_PLUGINMODE
menu->addAction(tr("View logs"), this,SLOT(OpenLog()));
#endif
menu->addAction(tr("Context help"), this,[](){QWhatsThis::enterWhatsThisMode();});
ui->toolButton->setMenu(menu);
m_stylehelper->SetStyle();
ui->eq_widget->setAccentColor(palette().highlight().color());
ConnectActions();
#ifndef VIPER_PLUGINMODE
if(m_appwrapper->getTrayMode() || m_startupInTraySwitch) trayIcon->show();
else trayIcon->hide();
connect(m_dbus, &DBusProxy::propertiesCommitted, this, [this](){
if(m_appwrapper->getSyncDisabled())
return;
conf->setConfigMap(m_dbus->FetchPropertyMap());
LoadConfig(Context::DBus);
});
#endif
connect(m_appwrapper,&AppConfigWrapper::styleChanged,this,[this](){
/* Make sure the QToolButton looks the same as the QPushButton */
ui->toolButton->setMaximumSize(ui->set->width(),
ui->set->height());
ui->frame->setStyleSheet(QString("QFrame#frame{background-color: %1;}").arg(this->palette().window().color().lighter().name()));
ui->tabhost->setStyleSheet(QString("QWidget#tabhostPage1,QWidget#tabhostPage2,QWidget#tabhostPage3,QWidget#tabhostPage4,QWidget#tabhostPage5,QWidget#tabhostPage6,QWidget#tabhostPage7{background-color: %1;}").arg(this->palette().window().color().lighter().name()));
ui->tabbar->redrawTabBar();
#ifndef VIPER_PLUGINMODE
RestartSpectrum();
#endif
ui->eq_widget->setAccentColor(palette().highlight().color());
});
ui->eq_widget->setAlwaysDrawHandles(m_appwrapper->getEqualizerPermanentHandles());
connect(m_appwrapper,&AppConfigWrapper::eqChanged,this,[this](){
ui->eq_widget->setAlwaysDrawHandles(m_appwrapper->getEqualizerPermanentHandles());
});
connect(m_appwrapper, &AppConfigWrapper::languageChanged, this, [this](){
auto result = QMessageBox::information(this, "Warning", "Please restart this application to apply the updated language settings. Press 'Yes' to quit now.",
QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::Cancel);
if(result == QMessageBox::StandardButton::Yes)
{
this->close();
exit(0);
}
});
#ifndef VIPER_PLUGINMODE
ToggleSpectrum(m_appwrapper->getSpetrumEnable(),true);
if(!m_appwrapper->getIntroShown())
LaunchFirstRunSetup();
else
QTimer::singleShot(300,this,[this]{
RunDiagnosticChecks();
});
#endif
ui->tabbar->setAnimatePageChange(true);
ui->tabbar->setCustomStackWidget(ui->tabhost);
ui->tabbar->setDetachCustomStackedWidget(true);
ui->tabbar->addPage(tr("Bass/Clarity"));
ui->tabbar->addPage(tr("Dynamic"));
ui->tabbar->addPage(tr("Surround"));
ui->tabbar->addPage(tr("Equalizer"));
ui->tabbar->addPage(tr("Compressor"));
ui->tabbar->addPage(tr("Volume"));
ui->tabbar->addPage(tr("Miscellaneous"));
ui->frame->setStyleSheet(QString("QFrame#frame{background-color: %1;}").arg(this->palette().window().color().lighter().name()));
ui->tabhost->setStyleSheet(QString("QWidget#tabhostPage1,QWidget#tabhostPage2,QWidget#tabhostPage3,QWidget#tabhostPage4,QWidget#tabhostPage5,QWidget#tabhostPage6,QWidget#tabhostPage7{background-color: %1;}").arg(this->palette().window().color().lighter().name()));
ui->tabbar->redrawTabBar();
/* Make sure the QToolButton looks the same as the QPushButton */
ui->toolButton->setMaximumSize(ui->set->width(),
ui->set->height());
}
ViperWindow::~ViperWindow()
{
delete ui;
}
void ViperWindow::loadConfigFromContainer(ConfigContainer c){
conf->setConfigMap(c.getConfigMap());
LoadConfig();
}
#ifdef VIPER_PLUGINMODE
QString ViperWindow::getLegacyPath(){
return QString("%1/.config/viper4linux/").arg(QDir::homePath());
}
#endif
#ifndef VIPER_PLUGINMODE
void ViperWindow::LaunchFirstRunSetup(){
FirstLaunchWizard* wiz = new FirstLaunchWizard(m_appwrapper,this);
QHBoxLayout* lbLayout = new QHBoxLayout;
QMessageOverlay* lightBox = new QMessageOverlay(this);
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect();
lightBox->setGraphicsEffect(eff);
lightBox->setLayout(lbLayout);
lightBox->layout()->addWidget(wiz);
lightBox->show();
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(500);
a->setStartValue(0);
a->setEndValue(1);
a->setEasingCurve(QEasingCurve::InBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
connect(wiz,&FirstLaunchWizard::wizardFinished,[=]{
QPropertyAnimation *b = new QPropertyAnimation(eff,"opacity");
b->setDuration(500);
b->setStartValue(1);
b->setEndValue(0);
b->setEasingCurve(QEasingCurve::OutCirc);
b->start(QPropertyAnimation::DeleteWhenStopped);
connect(b,&QAbstractAnimation::finished, [=](){
m_appwrapper->setIntroShown(true);
lightBox->hide();
settings_dlg->refreshAll();
QTimer::singleShot(300,this,[this]{
RunDiagnosticChecks();
});
});
});
}
void ViperWindow::RunDiagnosticChecks(){
if(system("which viper > /dev/null 2>&1") == 1){
OverlayMsgProxy *msg = new OverlayMsgProxy(this);
msg->openError(tr("Viper not installed"),
tr("Unable to find the viper executable.\n"
"Please make sure viper is installed and you\n"
"are using the lastest version of gst-plugin-viperfx"),
tr("Continue anyway"));
}
else if(!GstRegistryHelper::IsPluginInstalled()){
OverlayMsgProxy *msg = new OverlayMsgProxy(this);
msg->openError(tr("Missing component"),
tr("Unable to find the GStreamer viperfx plugin.\n"
"Make sure you installed Viper4Linux correctly\n"
"and double-check the README instructions.\n"
"You can also run 'gst-inspect-1.0 viperfx'\n"
"to check if the plugin was correctly installed."),
tr("Try again"));
connect(msg,&OverlayMsgProxy::buttonPressed,[this](){
QTimer::singleShot(300,this,[this](){
gst_update_registry();
RunDiagnosticChecks();
});
});
}
else if(GstRegistryHelper::HasDBusSupport() && !m_dbus->isValid()){
qWarning() << "[W] Viper DBus IPC is inactive. Attempting to launch service using viper.sh";
Restart();
}
}
#endif
//Spectrum
#ifndef VIPER_PLUGINMODE
void ViperWindow::SetSpectrumVisibility(bool v){
m_spectrograph->setVisible(v);
if(v)
this->findChild<QFrame*>("analysisLayout_spectrum")->setFrameShape(QFrame::StyledPanel);
else
this->findChild<QFrame*>("analysisLayout_spectrum")->setFrameShape(QFrame::NoFrame);
}
void ViperWindow::InitializeSpectrum(){
m_spectrograph = new Spectrograph(this);
m_audioengine = new AudioStreamEngine(this);
int refresh = m_appwrapper->getSpectrumRefresh();
if(refresh == 0) refresh = 20;
if(refresh < 10) refresh = 10;
else if(refresh > 500) refresh = 500;
m_audioengine->setNotifyIntervalMs(refresh);
analysisLayout.reset(new QFrame());
analysisLayout->setObjectName("analysisLayout_spectrum");
analysisLayout->setFrameShape(QFrame::Shape::StyledPanel);
analysisLayout->setLayout(new QHBoxLayout);
analysisLayout->layout()->setMargin(0);
analysisLayout->layout()->addWidget(m_spectrograph);
auto buttonbox = ui->host_layout->takeAt(ui->host_layout->count() - 1);
layout()->addWidget(analysisLayout.data());
layout()->addItem(buttonbox);
analysisLayout.take();
SetSpectrumVisibility(false);
connect(m_appwrapper,&AppConfigWrapper::spectrumChanged,this,[this]{
ToggleSpectrum(m_appwrapper->getSpetrumEnable(),true);
});
connect(m_appwrapper,&AppConfigWrapper::spectrumReloadRequired,this,&ViperWindow::RestartSpectrum);
}
void ViperWindow::RestartSpectrum(){
ToggleSpectrum(false,false);
ToggleSpectrum(m_appwrapper->getSpetrumEnable(),false);
}
void ViperWindow::RefreshSpectrumParameters(){
int bands = m_appwrapper->getSpectrumBands();
int minfreq = m_appwrapper->getSpectrumMinFreq();
int maxfreq = m_appwrapper->getSpectrumMaxFreq();
int refresh = m_appwrapper->getSpectrumRefresh();
float multiplier = m_appwrapper->getSpectrumMultiplier();
//Set default values if undefined
if(bands == 0) bands = 100;
if(maxfreq == 0) maxfreq = 1000;
if(refresh == 0) refresh = 10;
if(multiplier == 0) multiplier = 0.15;
//Check boundaries
if(bands < 5 ) bands = 5;
else if(bands > 300) bands = 300;
if(minfreq < 0) minfreq = 0;
else if(minfreq > 10000) minfreq = 10000;
if(maxfreq < 100) maxfreq = 100;
else if(maxfreq > 24000) maxfreq = 24000;
if(refresh < 10) refresh = 10;
else if(refresh > 500) refresh = 500;
if(multiplier < 0.01) multiplier = 0.01;
else if(multiplier > 1) multiplier = 1;
if(maxfreq < minfreq) maxfreq = minfreq + 100;
QColor outline;
if (palette().window().style() == Qt::TexturePattern)
outline = QColor(0, 0, 0, 160);
else
outline = palette().window().color().lighter(140);
if(m_appwrapper->getSpectrumTheme() == 0)
m_spectrograph->setTheme(Qt::black,
QColor(51,204,201),
QColor(51,204,201).darker(),
QColor(255,255,0),
m_appwrapper->getSpetrumGrid(),
(Spectrograph::Mode)m_appwrapper->getSpectrumShape());
else
m_spectrograph->setTheme(palette().window().color().lighter(),
palette().highlight().color(),
palette().text().color(),
outline.lighter(108),
m_appwrapper->getSpetrumGrid(),
(Spectrograph::Mode)m_appwrapper->getSpectrumShape());
m_spectrograph->setParams(bands, minfreq, maxfreq);
m_audioengine->setNotifyIntervalMs(refresh);
m_audioengine->setMultiplier(multiplier);
}
void ViperWindow::ToggleSpectrum(bool on,bool ctrl_visibility){
RefreshSpectrumParameters();
if(ctrl_visibility)spectrum->setVisible(on);
if(on && (!m_spectrograph->isVisible() || !ctrl_visibility)){
if(ctrl_visibility){
SetSpectrumVisibility(true);
this->setGeometry(this->x(), this->y(), this->width(),this->height()+m_spectrograph->size().height());
//this->setFixedSize(this->width(),this->height()+m_spectrograph->size().height());
}
QAudioDeviceInfo in;
for(auto item : QAudioDeviceInfo::availableDevices(QAudio::AudioInput))
if(item.deviceName()==m_appwrapper->getSpectrumInput())
in = item;
LogHelper::writeLog("Spectrum Expected Input Device: "+m_appwrapper->getSpectrumInput());
LogHelper::writeLog("Spectrum Found Input Device: "+in.deviceName());
LogHelper::writeLog("Spectrum Default Input Device: "+QAudioDeviceInfo::defaultInputDevice().deviceName());
m_audioengine->setAudioInputDevice(in);
m_audioengine->initializeRecord();
m_audioengine->startRecording();
connect(m_audioengine, static_cast<void (AudioStreamEngine::*)(QAudio::Mode,QAudio::State)>(&AudioStreamEngine::stateChanged),
this, [this](QAudio::Mode mode,QAudio::State state){
Q_UNUSED(mode);
if (QAudio::ActiveState != state && QAudio::SuspendedState != state) {
m_spectrograph->reset();
}
});
connect(m_audioengine, static_cast<void (AudioStreamEngine::*)(qint64, qint64, const FrequencySpectrum &)>(&AudioStreamEngine::spectrumChanged),
this, [this](qint64, qint64,const FrequencySpectrum &spectrum){
m_spectrograph->spectrumChanged(spectrum);
});
}
else if(!on && (m_spectrograph->isVisible() || !ctrl_visibility)){
if(ctrl_visibility){
SetSpectrumVisibility(false);
this->setGeometry(this->x(), this->y(), this->width(),this->height()-m_spectrograph->size().height());
//this->setFixedSize(this->width(),this->height()-m_spectrograph->size().height());
}
m_spectrograph->reset();
m_audioengine->reset();
}
}
#endif
//Overrides
void ViperWindow::setVisible(bool visible)
{
#ifndef VIPER_PLUGINMODE
//Reconnect to dbus to make sure the connection isn't stale
m_dbus = new DBusProxy();
updateTrayPresetList();
updateTrayConvolverList();
#endif
//Hide all other windows if set to invisible
if(!visible){
log_dlg->hide();
preset_dlg->hide();
}
#ifndef VIPER_PLUGINMODE
if(GstRegistryHelper::HasDBusSupport() && m_dbus->isValid() &&
msg_notrunning != nullptr)
msg_notrunning->hide();
#endif
QWidget::setVisible(visible);
}
void ViperWindow::setPopupButtonEnabled(bool b)
{
ui->popupButton->setVisible(b);
}
void ViperWindow::closeEvent(QCloseEvent *event)
{
#ifdef Q_OS_OSX
if (!event->spontaneous() || !isVisible()) {
return;
}
#endif
#ifndef VIPER_PLUGINMODE
if (trayIcon->isVisible()) {
hide();
event->ignore();
}
#endif
}
//DBus
void ViperWindow::ShowDBusError(){
#ifndef VIPER_PLUGINMODE
if(msg_notrunning != nullptr)
msg_notrunning->hide();
msg_notrunning = new OverlayMsgProxy(this);
msg_notrunning->openError(tr("Viper inactive"),
tr("Unable to connect to the DBus interface.\n"
"Please make sure viper is active and you are\n"
"using the lastest version of gst-plugin-viperfx"),
tr("Launch viper"));
connect(msg_notrunning,&OverlayMsgProxy::buttonPressed,[this](){
int returncode = system("viper start");
if(returncode != 0){
if(msg_launchfail != nullptr)
msg_launchfail->hide();
msg_launchfail = new OverlayMsgProxy(this);
msg_launchfail->openError(tr("Failed to launch viper"),
tr("viper.sh has returned a non-null exit code.\n"
"Please make sure viper is correctly installed\n"
"and try to restart it manually"),
tr("Continue anyway"));
} else {
//Reconnect DBus
QTimer::singleShot(500,this,[this](){
m_dbus = new DBusProxy();
RestartSpectrum();
});
}
});
#else
if(msg_notrunning != nullptr)
msg_notrunning->hide();
msg_notrunning = new OverlayMsgProxy(this);
msg_notrunning->openError(tr("Service not found"),
tr("Unable to connect to the DBus interface.\n"
"Please make sure viper is active and you are\n"
"using the lastest version of gst-plugin-viperfx"),
tr("Continue"));
#endif
}
//Systray
#ifndef VIPER_PLUGINMODE
void ViperWindow::raiseWindow(){
/*
* NOTE: Raising the window does not always work!
*
* KDE users can disable 'Focus Stealing Prevention'
* in the Window Behavior section (system settings)
* as a workaround.
*/
show();
setWindowState( (windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
raise();
activateWindow();
}
void ViperWindow::setTrayVisible(bool visible){
if(visible) trayIcon->show();
else trayIcon->hide();
}
void ViperWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
setVisible(!this->isVisible());
if(isVisible()){
this->showNormal();
this->setWindowState( (windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
this->raise();
this->activateWindow();
}
//Hide tray icon if disabled and MainWin is visible (for cmdline force switch)
if(!m_appwrapper->getTrayMode() && this->isVisible()) trayIcon->hide();
break;
default:
;
}
}
void ViperWindow::updateTrayPresetList(){
if(tray_presetMenu != nullptr){
tray_presetMenu->clear();
QString absolute = QFileInfo(m_appwrapper->getPath()).absoluteDir().absolutePath();
QString path = pathAppend(absolute,"presets");
QDir dir(path);
if (!dir.exists())
dir.mkpath(".");
QStringList nameFilter("*.conf");
QStringList files = dir.entryList(nameFilter);
if(files.count()<1){
QAction *noPresets = new QAction("No presets found");
noPresets->setEnabled(false);
tray_presetMenu->addAction(noPresets);
}
else{
for(int i = 0; i < files.count(); i++){
//Strip extensions
QFileInfo fi(files[i]);
files[i] = fi.completeBaseName();
//Add entry
QAction *newEntry = new QAction(files[i]);
connect(newEntry,&QAction::triggered,this,[=](){
LoadPresetFile(pathAppend(path,QString("%1.conf").arg(files[i])));
});
tray_presetMenu->addAction(newEntry);
}
}
}
}
void ViperWindow::updateTrayConvolverList(){
if(tray_convMenu != nullptr){
tray_convMenu->clear();
QString absolute = QFileInfo(m_appwrapper->getPath()).absoluteDir().absolutePath();
QString path = pathAppend(absolute,"irs_favorites");
QDir dir(path);
if (!dir.exists())
dir.mkpath(".");
QStringList nameFilter({"*.wav","*.irs"});
QStringList files = dir.entryList(nameFilter);
if(files.count()<1){
QAction *noPresets = new QAction("No impulse responses found");
noPresets->setEnabled(false);
tray_convMenu->addAction(noPresets);
}
else{
for(int i = 0; i < files.count(); i++){
//Add entry
QAction *newEntry = new QAction(files[i]);
connect(newEntry,&QAction::triggered,this,[=](){
SetIRS(files[i],true);
});
tray_convMenu->addAction(newEntry);
}
}
}
}
void ViperWindow::createTrayIcon()
{
trayIcon = new QSystemTrayIcon(this);
trayIcon->setToolTip("Viper4Linux");
connect(trayIcon, &QSystemTrayIcon::activated, this, &ViperWindow::iconActivated);
trayIcon->setIcon(QIcon(":/viper.png"));
}
void ViperWindow::updateTrayMenu(QMenu* menu){
trayIcon->hide();
createTrayIcon();
trayIcon->show();
trayIconMenu = menu;
trayIcon->setContextMenu(trayIconMenu);
connect(trayIcon->contextMenu(),&QMenu::aboutToShow,[this]{
updateTrayPresetList();
updateTrayConvolverList();
});
m_appwrapper->setTrayContextMenu(MenuIO::buildString(menu));
}
QMenu* ViperWindow::getTrayContextMenu(){
return trayIconMenu;
}
void ViperWindow::initGlobalTrayActions(){
tray_disableAction = new QAction(tr("&Disable FX"), this);
tray_disableAction->setProperty("tag","disablefx");
tray_disableAction->setCheckable(true);
tray_disableAction->setChecked(!conf->getBool("fx_enable"));
connect(tray_disableAction, &QAction::triggered, this, [this](){
conf->setValue("fx_enable",!tray_disableAction->isChecked());
ui->disableFX->setChecked(tray_disableAction->isChecked());
ApplyConfig();
});
tray_presetMenu = new QMenu(tr("&Presets"));
tray_presetMenu->setProperty("tag","menu_preset");
tray_convMenu = new QMenu(tr("&Convolver Bookmarks"));
tray_convMenu->setProperty("tag","menu_convolver");
auto init = MenuIO::buildMenu(buildAvailableActions(),m_appwrapper->getTrayContextMenu());
if(init->actions().count() < 1)
init = buildDefaultActions();
updateTrayMenu(init);
}
QMenu* ViperWindow::buildAvailableActions()
{
QAction *reloadAction = new QAction(tr("&Reload Viper"), this);
reloadAction->setProperty("tag","reload");
connect(reloadAction, &QAction::triggered, this, &ViperWindow::Restart);
QAction* quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
quitAction->setProperty("tag","quit");
QMenu* dynsysMenu = new QMenu(tr("Dynamic &System Presets"), this);
for(auto preset : PresetProvider::Dynsys::DYNSYS_LOOKUP_TABLE().keys()){
QAction *newEntry = new QAction(preset);
connect(newEntry,&QAction::triggered,this,[=](){
ui->dynsys_preset->setCurrentText(preset);
DynsysPresetSelectionUpdated();
});
dynsysMenu->addAction(newEntry);
}
dynsysMenu->setProperty("tag","menu_dynsys_preset");
QMenu* eqMenu = new QMenu(tr("&EQ Presets"), this);
for(auto preset : PresetProvider::EQ::EQ_LOOKUP_TABLE().keys()){
QAction *newEntry = new QAction(preset);
connect(newEntry,&QAction::triggered,this,[=](){
if(preset == tr("Default"))
ResetEQ();
else{
ui->eqpreset->setCurrentText(preset);
EqPresetSelectionUpdated();
}
});
eqMenu->addAction(newEntry);
}
eqMenu->setProperty("tag","menu_eq_preset");
QMenu* colmMenu = new QMenu(tr("C&olorful Music Presets"), this);
for(auto preset : PresetProvider::Colm::COLM_LOOKUP_TABLE().keys()){
if(preset == tr("Unknown")) continue;
QAction *newEntry = new QAction(preset);
connect(newEntry,&QAction::triggered,this,[=](){
ui->colmpreset->setCurrentText(preset);
ColmPresetSelectionUpdated();
});
colmMenu->addAction(newEntry);
}
colmMenu->setProperty("tag","menu_colm_preset");
QMenu* bassModeMenu = new QMenu(tr("ViPER &Bass Mode"), this);
QStringList bassModes({tr("Natural Bass"),tr("Pure Bass+"),tr("Subwoofer")});
for(auto mode : bassModes){
QAction *newEntry = new QAction(mode);
connect(newEntry,&QAction::triggered,this,[=](){
ui->vbmode->setCurrentIndex(bassModes.indexOf(mode));
OnUpdate(true);
});
bassModeMenu->addAction(newEntry);
}
bassModeMenu->setProperty("tag","menu_bass_mode");
QMenu* clarityModeMenu = new QMenu(tr("ViPER C&larity Mode"), this);
QStringList clarityModes({tr("Natural"),tr("OZone+"),tr("XHiFi")});
for(auto mode : clarityModes){
QAction *newEntry = new QAction(mode);
connect(newEntry,&QAction::triggered,this,[=](){
ui->vcmode->setCurrentIndex(clarityModes.indexOf(mode));
OnUpdate(true);
});
clarityModeMenu->addAction(newEntry);
}
clarityModeMenu->setProperty("tag","menu_clarity_mode");
QMenu* menu = new QMenu(this);
menu->addAction(tray_disableAction);
menu->addAction(reloadAction);
menu->addMenu(tray_presetMenu);
menu->addSeparator();
menu->addMenu(dynsysMenu);
menu->addMenu(eqMenu);
menu->addMenu(colmMenu);
menu->addMenu(tray_convMenu);
menu->addSeparator();
menu->addMenu(bassModeMenu);
menu->addMenu(clarityModeMenu);
menu->addSeparator();
menu->addAction(quitAction);
return menu;
}
QMenu* ViperWindow::buildDefaultActions()
{
QAction *reloadAction = new QAction(tr("&Reload Viper"), this);
reloadAction->setProperty("tag","reload");
connect(reloadAction, &QAction::triggered, this, &ViperWindow::Restart);
QAction* quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
quitAction->setProperty("tag","quit");
QMenu* menu = new QMenu(this);
menu->addAction(tray_disableAction);
menu->addAction(reloadAction);
menu->addMenu(tray_presetMenu);
menu->addSeparator();
menu->addAction(quitAction);
return menu;
}
#endif
//---Dialogs/Buttons
void ViperWindow::DialogHandler(){
if(sender() == ui->conv_select){
QWidget* host = new QWidget(this);
host->setProperty("menu", false);
QVBoxLayout* hostLayout = new QVBoxLayout(host);
hostLayout->addWidget(conv_dlg);
host->hide();
host->setAutoFillBackground(true);
connect(conv_dlg,&ConvolverDlg::closePressed,this,[host](){
WAF::Animation::sideSlideOut(host, WAF::BottomSide);
});
WAF::Animation::sideSlideIn(host, WAF::BottomSide); }
else if(sender() == ui->set){
QFrame* host = new QFrame(this);
host->setProperty("menu", false);
QVBoxLayout* hostLayout = new QVBoxLayout(host);
hostLayout->addWidget(settings_dlg);
host->hide();
host->setAutoFillBackground(true);
settings_dlg->updateInputSinks();
connect(settings_dlg,&SettingsDlg::closeClicked,this,[host](){
host->update();
host->repaint();
WAF::Animation::sideSlideOut(host, WAF::BottomSide);
});
WAF::Animation::sideSlideIn(host, WAF::BottomSide);
}
else if(sender() == ui->cpreset){
if(preset_dlg->isVisible()){
//Hacky workaround to reliably raise the window on all distros
Qt::WindowFlags eFlags = preset_dlg->windowFlags();
eFlags |= Qt::WindowStaysOnTopHint;
preset_dlg->setWindowFlags(eFlags);
preset_dlg->show();
eFlags &= ~Qt::WindowStaysOnTopHint;
preset_dlg->setWindowFlags(eFlags);
preset_dlg->showNormal();
preset_dlg->setWindowState( (windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
preset_dlg->raise();
preset_dlg->activateWindow();
return;
}
preset_dlg->move(x() + (width() - preset_dlg->width()) / 2,
y() + (height() - preset_dlg->height()) / 2);
preset_dlg->setModal(true);
preset_dlg->show();
}
}
void ViperWindow::OpenLog(){
log_dlg->show();
log_dlg->updateLog();
}
void ViperWindow::Reset(){
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this,tr("Reset Configuration"),tr("Are you sure?"),
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
conf->setConfigMap(ConfigIO::readString(QString::fromStdString(default_config)));
LoadConfig();
ApplyConfig();
}
}
void ViperWindow::DisableFX(){
#ifndef VIPER_PLUGINMODE
tray_disableAction->setChecked(ui->disableFX->isChecked());
#endif
//Apply instantly
if(!lockapply)ApplyConfig();
}
//---Reloader
void ViperWindow::OnUpdate(bool ignoremode){
//Will be called when slider has been moved, dynsys/eq preset set or spinbox changed
if((m_appwrapper->getAutoFx() &&
(ignoremode||m_appwrapper->getAutoFxMode()==0)) && !lockapply)
ApplyConfig();
}
void ViperWindow::OnRelease(){
if((m_appwrapper->getAutoFx() &&
m_appwrapper->getAutoFxMode()==1) && !lockapply)
ApplyConfig();
}
#ifndef VIPER_PLUGINMODE
void ViperWindow::Restart(){
if(m_appwrapper->getGFix())system("killall -r glava");
system("viper restart");
if(m_appwrapper->getGFix())system("setsid glava -d >/dev/null 2>&1 &");
RestartSpectrum();
}
#endif
//---User preset management
void ViperWindow::LoadPresetFile(const QString& filename){
#ifndef VIPER_PLUGINMODE
const QString& src = filename;
const QString dest = m_appwrapper->getPath();
if (QFile::exists(dest))QFile::remove(dest);
QFile::copy(src,dest);
conf->setConfigMap(readConfig());
#else
conf->setConfigMap(readConfig(filename));
#endif
LogHelper::writeLog("Loading from " + filename+ " (main/loadpreset)");
LoadConfig();
ApplyConfig();
}
void ViperWindow::SavePresetFile(const QString& filename){
#ifndef VIPER_PLUGINMODE
const QString src = m_appwrapper->getPath();
const QString& dest = filename;
if (QFile::exists(dest))QFile::remove(dest);
QFile::copy(src,dest);
#else
ConfigIO::writeFile(filename, conf->getConfigMap());
#endif
LogHelper::writeLog("Saving to " + filename+ " (main/savepreset)");
}
void ViperWindow::LoadExternalFile(){
QString filename = QFileDialog::getOpenFileName(this,tr("Load custom audio.conf"),"","*.conf");
if(filename=="")return;
#ifndef VIPER_PLUGINMODE
const QString& src = filename;
const QString dest = m_appwrapper->getPath();
if (QFile::exists(dest))QFile::remove(dest);
QFile::copy(src,dest);
conf->setConfigMap(readConfig());
#else
conf->setConfigMap(readConfig(filename));
#endif
LogHelper::writeLog("Loading from " + filename+ " (main/loadexternal)");
LoadConfig();