Skip to content

Commit 2f7d74c

Browse files
committed
Add listener support to Controller for device change notifications
1 parent 1c5cccb commit 2f7d74c

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/controller.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct Controller::Impl : public MidiKeyboardStateListener {
2222
juce::String virtualDeviceName { "VMC-MIDI-Out" };
2323
Device device;
2424
juce::File deviceFile;
25+
ListenerList<Controller::Listener> listeners;
2526

2627
void saveSettings()
2728
{
@@ -53,6 +54,7 @@ struct Controller::Impl : public MidiKeyboardStateListener {
5354
{
5455
if (device.load (file)) {
5556
deviceFile = file;
57+
listeners.call (&Controller::Listener::deviceChanged);
5658
return true;
5759
}
5860
return false;
@@ -180,4 +182,7 @@ void Controller::audioDeviceAboutToStart (AudioIODevice*) {}
180182
void Controller::audioDeviceStopped() {}
181183
void Controller::audioDeviceError (const String& errorMessage) { juce::ignoreUnused (errorMessage); }
182184

185+
void Controller::addListener (Listener* listener) { impl->listeners.add (listener); }
186+
void Controller::removeListener (Listener* listener) { impl->listeners.remove (listener); }
187+
183188
} // namespace vmc

src/controller.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class Controller final : public AudioIODeviceCallback,
1717
public:
1818
Controller();
1919
~Controller();
20+
21+
struct Listener {
22+
virtual ~Listener() = default;
23+
virtual void deviceChanged() = 0;
24+
};
2025

2126
Device device() const;
2227
bool loadDeviceFile (const juce::File&);
@@ -53,7 +58,10 @@ class Controller final : public AudioIODeviceCallback,
5358
virtual void handleIncomingMidiMessage (MidiInput*, const MidiMessage&) override {}
5459
virtual void handlePartialSysexMessage (MidiInput*, const uint8*,
5560
int, double) override {}
56-
61+
62+
void addListener (Listener*);
63+
void removeListener (Listener*);
64+
5765
private:
5866
struct Impl;
5967
std::unique_ptr<Impl> impl;

src/main.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ class Application : public JUCEApplication {
5757
ignoreUnused (commandLine);
5858
}
5959

60-
class MainWindow : public DocumentWindow {
60+
class MainWindow : public DocumentWindow,
61+
public Controller::Listener {
6162
public:
6263
MainWindow (String name, Controller& vc)
6364
: DocumentWindow (name, Desktop::getInstance().getDefaultLookAndFeel().findColour (ResizableWindow::backgroundColourId),
6465
DocumentWindow::closeButton | DocumentWindow::minimiseButton),
6566
controller (vc)
6667
{
68+
controller.addListener (this);
6769
#if JUCE_LINUX
6870
setUsingNativeTitleBar (false);
6971
#else
@@ -81,11 +83,14 @@ class Application : public JUCEApplication {
8183
setContentComponentSize (VMC_WIDTH, VMC_HEIGHT);
8284
setResizable (false, false);
8385

86+
deviceChanged();
87+
8488
setVisible (true);
8589
}
8690

8791
~MainWindow() override
8892
{
93+
controller.removeListener (this);
8994
clearContentComponent();
9095
setConstrainer (nullptr);
9196
}
@@ -110,6 +115,12 @@ class Application : public JUCEApplication {
110115
return;
111116
}
112117

118+
void deviceChanged() override
119+
{
120+
auto name = controller.deviceFile().getFileNameWithoutExtension();
121+
setName ("Virtual MIDI Controller - " + name);
122+
}
123+
113124
private:
114125
Controller& controller;
115126
ComponentBoundsConstrainer constrain;

0 commit comments

Comments
 (0)