From e59a386af8f73e0e0ae202893e1dcd9cb7543e74 Mon Sep 17 00:00:00 2001 From: Michael Dewberry Date: Sun, 17 Sep 2023 20:55:31 -0400 Subject: [PATCH] Remove redundant connection menu code, add ability to stop mirroring --- .../core/GridConnection/GridConnection.cpp | 13 +++ .../core/GridConnection/GridConnection.hpp | 2 + .../GridConnection/GridConnectionMenu.cpp | 19 ++--- .../core/GridConnection/GridConsumerBase.cpp | 18 ++-- .../core/GridConnection/GridConsumerBase.hpp | 2 +- src/common/core/LibAVR32Module.cpp | 5 -- src/common/core/LibAVR32Module.hpp | 3 +- src/common/core/LibAVR32ModuleWidget.cpp | 83 +------------------ src/common/core/LibAVR32ModuleWidget.hpp | 1 - src/common/widgets/USBAJack.hpp | 9 +- src/virtualgrid/VirtualGridWidget.cpp | 9 ++ 11 files changed, 50 insertions(+), 114 deletions(-) diff --git a/src/common/core/GridConnection/GridConnection.cpp b/src/common/core/GridConnection/GridConnection.cpp index c3ad4ad..7de5d67 100644 --- a/src/common/core/GridConnection/GridConnection.cpp +++ b/src/common/core/GridConnection/GridConnection.cpp @@ -123,6 +123,19 @@ void GridConnectionManager::disconnect(IGridConsumer* consumer, bool ownerChange } } +void GridConnectionManager::toggleConnection(Grid* grid, IGridConsumer* consumer) +{ + if (consumer && consumer->gridGetDevice() == grid) + { + disconnect(consumer, true); + consumer->setLastDeviceId(""); + } + else + { + connect(grid, consumer); + } +} + void GridConnectionManager::dispatchButtonMessage(MonomeDevice* device, int x, int y, bool state) { auto iter = idToConsumerMap.find(device->id); diff --git a/src/common/core/GridConnection/GridConnection.hpp b/src/common/core/GridConnection/GridConnection.hpp index d12b335..e67f4e6 100644 --- a/src/common/core/GridConnection/GridConnection.hpp +++ b/src/common/core/GridConnection/GridConnection.hpp @@ -25,6 +25,7 @@ struct IGridConsumer virtual void gridDisconnected(bool ownerChanged) = 0; virtual std::string gridGetCurrentDeviceId() = 0; virtual std::string gridGetLastDeviceId(bool owned) = 0; + virtual void setLastDeviceId(std::string id) = 0; virtual void gridButtonEvent(int x, int y, bool state) = 0; virtual void encDeltaEvent(int n, int d) = 0; virtual Grid* gridGetDevice() = 0; @@ -43,6 +44,7 @@ struct GridConnectionManager final bool isConnected(std::string id); void disconnect(Grid* grid, bool ownerChanged = false); void disconnect(IGridConsumer* consumer, bool ownerChanged = false); + void toggleConnection(Grid* grid, IGridConsumer* consumer); void dispatchButtonMessage(MonomeDevice* device, int x, int y, bool state); void dispatchEncDeltaMessage(MonomeDevice* device, int n, int d); diff --git a/src/common/core/GridConnection/GridConnectionMenu.cpp b/src/common/core/GridConnection/GridConnectionMenu.cpp index 7463f2b..3661377 100644 --- a/src/common/core/GridConnection/GridConnectionMenu.cpp +++ b/src/common/core/GridConnection/GridConnectionMenu.cpp @@ -3,7 +3,7 @@ using namespace rack; -struct NewConnectGridItem : rack::ui::MenuItem +struct ConnectGridItem : rack::ui::MenuItem { Grid* grid; IGridConsumer* consumer; @@ -15,9 +15,10 @@ struct NewConnectGridItem : rack::ui::MenuItem { auto thisGrid = grid; auto thisConsumer = consumer; - - actionQueue->push([thisGrid, thisConsumer]() - { GridConnectionManager::get().connect(thisGrid, thisConsumer); }); + + actionQueue->push([thisGrid, thisConsumer]() { + GridConnectionManager::get().toggleConnection(thisGrid, thisConsumer); + }); } } }; @@ -30,11 +31,9 @@ void menuUserReacquireGrid(IGridConsumer* consumer, std::string lastDeviceId, Ac { if (actionQueue) { - actionQueue->push([grid, consumer]() - { - GridConnectionManager::get().connect(grid, consumer); - } - ); + actionQueue->push([grid, consumer]() { + GridConnectionManager::get().connect(grid, consumer); + }); } return; } @@ -75,7 +74,7 @@ void appendDeviceConnectionMenu(rack::Menu* menu, IGridConsumer* consumer, Actio continue; } - auto connectItem = new NewConnectGridItem(); + auto connectItem = new ConnectGridItem(); connectItem->text = "└ " + grid->getDevice().type + " (" + grid->getDevice().id + ") "; auto rightText = ""; diff --git a/src/common/core/GridConnection/GridConsumerBase.cpp b/src/common/core/GridConnection/GridConsumerBase.cpp index e95e01c..15dde9a 100644 --- a/src/common/core/GridConnection/GridConsumerBase.cpp +++ b/src/common/core/GridConnection/GridConsumerBase.cpp @@ -46,6 +46,11 @@ std::string GridConsumerBase::gridGetLastDeviceId(bool owned) return lastConnectedDeviceId; } +void GridConsumerBase::setLastDeviceId(std::string id) +{ + lastConnectedDeviceId = id; +} + Grid* GridConsumerBase::gridGetDevice() { return gridConnection; @@ -66,19 +71,6 @@ void GridConsumerBase::userReacquireGrid() } } -void GridConsumerBase::toggleGridConnection(Grid* grid) -{ - if (gridConnection == grid) - { - GridConnectionManager::get().disconnect(this, true); - lastConnectedDeviceId = ""; - } - else - { - GridConnectionManager::get().connect(grid, this); - } -} - void GridConsumerBase::saveGridConnectionToJson(json_t* rootJ) { std::string deviceId = lastConnectedDeviceId; diff --git a/src/common/core/GridConnection/GridConsumerBase.hpp b/src/common/core/GridConnection/GridConsumerBase.hpp index 828adbe..d96fbff 100644 --- a/src/common/core/GridConnection/GridConsumerBase.hpp +++ b/src/common/core/GridConnection/GridConsumerBase.hpp @@ -10,6 +10,7 @@ struct GridConsumerBase : public IGridConsumer void gridDisconnected(bool ownerChanged) override; std::string gridGetCurrentDeviceId() override; std::string gridGetLastDeviceId(bool owned) override; + virtual void setLastDeviceId(std::string id) override; Grid* gridGetDevice() override; GridConsumerBase(); @@ -25,6 +26,5 @@ struct GridConsumerBase : public IGridConsumer bool connectionOwned; protected: - void toggleGridConnection(Grid* grid); Grid* gridConnection; }; \ No newline at end of file diff --git a/src/common/core/LibAVR32Module.cpp b/src/common/core/LibAVR32Module.cpp index 1de510a..35ea14b 100644 --- a/src/common/core/LibAVR32Module.cpp +++ b/src/common/core/LibAVR32Module.cpp @@ -139,11 +139,6 @@ void LibAVR32Module::encDeltaEvent(int n, int d) } } -void LibAVR32Module::userToggleGridConnection(Grid* grid) -{ - audioThreadActions.push([this, grid]() { this->toggleGridConnection(grid); }); -} - void LibAVR32Module::readSerialMessages() { uint8_t* msg; diff --git a/src/common/core/LibAVR32Module.hpp b/src/common/core/LibAVR32Module.hpp index 25813be..9e68384 100644 --- a/src/common/core/LibAVR32Module.hpp +++ b/src/common/core/LibAVR32Module.hpp @@ -56,7 +56,6 @@ struct LibAVR32Module : rack::engine::Module, GridConsumerBase virtual void gridButtonEvent(int x, int y, bool state) override; virtual void encDeltaEvent(int n, int d) override; - void userToggleGridConnection(Grid* grid); virtual void readSerialMessages(); void requestReloadFirmware(bool preserveMemory, const std::string& firmwareName = ""); @@ -87,6 +86,8 @@ struct LibAVR32Module : rack::engine::Module, GridConsumerBase // Thread-safe for single-producer, single-consumer friend struct TeletypeSceneIO; + friend struct LibAVR32ModuleWidget; + friend struct USBAJack; ActionQueue audioThreadActions; }; diff --git a/src/common/core/LibAVR32ModuleWidget.cpp b/src/common/core/LibAVR32ModuleWidget.cpp index a40fc87..6c4b0c2 100644 --- a/src/common/core/LibAVR32ModuleWidget.cpp +++ b/src/common/core/LibAVR32ModuleWidget.cpp @@ -1,5 +1,6 @@ #include "LibAVR32ModuleWidget.hpp" #include "LibAVR32Module.hpp" +#include "GridConnectionMenu.hpp" #include "VirtualGridModule.hpp" #include "VirtualGridWidget.hpp" #include "SerialOscInterface.hpp" @@ -11,16 +12,6 @@ namespace fs = ghc::filesystem; using namespace rack; -struct ConnectGridItem : rack::ui::MenuItem -{ - LibAVR32Module* module; - Grid* grid; - - void onAction(const rack::event::Action& e) override - { - module->userToggleGridConnection(grid); - } -}; struct ReloadFirmwareItem : rack::ui::MenuItem { @@ -183,75 +174,5 @@ void LibAVR32ModuleWidget::appendContextMenu(rack::Menu* menu) menu->addChild(firmwareMenu); menu->addChild(new MenuSeparator()); - appendConnectionMenu(menu); -} - -void LibAVR32ModuleWidget::appendConnectionMenu(rack::Menu* menu) -{ - LibAVR32Module* m = dynamic_cast(module); - assert(m); - - menu->addChild(construct(&MenuLabel::text, "Device Connection")); - - if (SerialOscInterface::get()->isServiceDetected()) - { - menu->addChild( - construct( - &MenuLabel::text, - "serialosc version " + SerialOscInterface::get()->getServiceVersion())); - } - else - { - menu->addChild( - createMenuItem("└ serialosc service not detected, click here to install", "", - [=]() - { - system::openBrowser("https://monome.org/docs/serialosc/setup/"); - })); - } - - // enumerate registered grid devices - int deviceCount = 0; - bool preferredDeviceFound = false; - for (Grid* grid : GridConnectionManager::get().getGrids()) - { - auto connectItem = new ConnectGridItem(); - connectItem->text = "└ " + grid->getDevice().type + " (" + grid->getDevice().id + ") "; - - auto rightText = ""; - if (m->currentConnectedDeviceId == grid->getDevice().id) - { - rightText = "✔"; - preferredDeviceFound = true; - } - else if (m->currentConnectedDeviceId == "" && m->gridGetLastDeviceId(false) == grid->getDevice().id) - { - rightText = "⋯"; - preferredDeviceFound = true; - } - - connectItem->rightText = rightText; - connectItem->module = m; - connectItem->grid = grid; - menu->addChild(connectItem); - deviceCount++; - } - - if (deviceCount == 0) - { - menu->addChild(construct(&MenuLabel::text, " (no physical or virtual devices found)")); - } - - if (m->currentConnectedDeviceId == "" && m->gridGetLastDeviceId(false) != "") - { - if (preferredDeviceFound) - { - menu->addChild(createMenuItem("Reacquire grid", "", [=]() { m->userReacquireGrid(); })); - } - else - { - menu->addChild(construct(&MenuLabel::text, "Can't reacquire grid (" + m->gridGetLastDeviceId(false) + " not found)")); - } - } - + appendDeviceConnectionMenu(menu, m, &m->audioThreadActions); } diff --git a/src/common/core/LibAVR32ModuleWidget.hpp b/src/common/core/LibAVR32ModuleWidget.hpp index 82d7393..c82533f 100644 --- a/src/common/core/LibAVR32ModuleWidget.hpp +++ b/src/common/core/LibAVR32ModuleWidget.hpp @@ -10,5 +10,4 @@ struct LibAVR32ModuleWidget : rack::app::ModuleWidget LibAVR32ModuleWidget(); virtual void appendContextMenu(rack::ui::Menu* menu) override; - void appendConnectionMenu(rack::Menu* menu); }; \ No newline at end of file diff --git a/src/common/widgets/USBAJack.hpp b/src/common/widgets/USBAJack.hpp index b268d48..3e1dd7b 100644 --- a/src/common/widgets/USBAJack.hpp +++ b/src/common/widgets/USBAJack.hpp @@ -2,6 +2,7 @@ #include "rack.hpp" #include "LibAVR32Module.hpp" +#include "GridConnectionMenu.hpp" struct USBAJack : rack::Switch @@ -87,8 +88,12 @@ struct USBAJack : rack::Switch auto mw = dynamic_cast(parent); if (mw) { - menu->addChild(new rack::MenuSeparator()); - mw->appendConnectionMenu(menu); + auto m = dynamic_cast(mw->module); + if (mw) + { + menu->addChild(new rack::MenuSeparator()); + appendDeviceConnectionMenu(menu, m, &m->audioThreadActions); + } } } }; diff --git a/src/virtualgrid/VirtualGridWidget.cpp b/src/virtualgrid/VirtualGridWidget.cpp index 3a20599..0a7587a 100644 --- a/src/virtualgrid/VirtualGridWidget.cpp +++ b/src/virtualgrid/VirtualGridWidget.cpp @@ -216,6 +216,15 @@ void VirtualGridWidget::appendContextMenu(Menu * menu) menu->addChild(createSubmenuItem("Mirror hardware grid", "", [=](Menu *childMenu) { appendDeviceConnectionMenu(childMenu, grid->mirrorModeConsumer, &grid->audioThreadActions, true); + childMenu->addChild(createMenuItem("Stop mirroring", "", + [=]() { + if (grid->mirrorModeConsumer) { + GridConnectionManager::get().disconnect(grid->mirrorModeConsumer); + grid->mirrorModeConsumer->setLastDeviceId(""); + } + }, + !grid->mirrorModeConsumer || grid->mirrorModeConsumer->gridGetDevice() == nullptr + )); })); menu->addChild(new MenuSeparator());