Skip to content

Commit

Permalink
Remove redundant connection menu code, add ability to stop mirroring
Browse files Browse the repository at this point in the history
  • Loading branch information
Dewb committed Sep 18, 2023
1 parent 7acc876 commit e59a386
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 114 deletions.
13 changes: 13 additions & 0 deletions src/common/core/GridConnection/GridConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/common/core/GridConnection/GridConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
19 changes: 9 additions & 10 deletions src/common/core/GridConnection/GridConnectionMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using namespace rack;

struct NewConnectGridItem : rack::ui::MenuItem
struct ConnectGridItem : rack::ui::MenuItem
{
Grid* grid;
IGridConsumer* consumer;
Expand All @@ -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);
});
}
}
};
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 = "";
Expand Down
18 changes: 5 additions & 13 deletions src/common/core/GridConnection/GridConsumerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/common/core/GridConnection/GridConsumerBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -25,6 +26,5 @@ struct GridConsumerBase : public IGridConsumer
bool connectionOwned;

protected:
void toggleGridConnection(Grid* grid);
Grid* gridConnection;
};
5 changes: 0 additions & 5 deletions src/common/core/LibAVR32Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/common/core/LibAVR32Module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "");

Expand Down Expand Up @@ -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;
};

Expand Down
83 changes: 2 additions & 81 deletions src/common/core/LibAVR32ModuleWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "LibAVR32ModuleWidget.hpp"
#include "LibAVR32Module.hpp"
#include "GridConnectionMenu.hpp"
#include "VirtualGridModule.hpp"
#include "VirtualGridWidget.hpp"
#include "SerialOscInterface.hpp"
Expand All @@ -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
{
Expand Down Expand Up @@ -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<LibAVR32Module*>(module);
assert(m);

menu->addChild(construct<MenuLabel>(&MenuLabel::text, "Device Connection"));

if (SerialOscInterface::get()->isServiceDetected())
{
menu->addChild(
construct<MenuLabel>(
&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>(&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>(&MenuLabel::text, "Can't reacquire grid (" + m->gridGetLastDeviceId(false) + " not found)"));
}
}

appendDeviceConnectionMenu(menu, m, &m->audioThreadActions);
}
1 change: 0 additions & 1 deletion src/common/core/LibAVR32ModuleWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ struct LibAVR32ModuleWidget : rack::app::ModuleWidget
LibAVR32ModuleWidget();

virtual void appendContextMenu(rack::ui::Menu* menu) override;
void appendConnectionMenu(rack::Menu* menu);
};
9 changes: 7 additions & 2 deletions src/common/widgets/USBAJack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "rack.hpp"
#include "LibAVR32Module.hpp"
#include "GridConnectionMenu.hpp"


struct USBAJack : rack::Switch
Expand Down Expand Up @@ -87,8 +88,12 @@ struct USBAJack : rack::Switch
auto mw = dynamic_cast<LibAVR32ModuleWidget*>(parent);
if (mw)
{
menu->addChild(new rack::MenuSeparator());
mw->appendConnectionMenu(menu);
auto m = dynamic_cast<LibAVR32Module*>(mw->module);
if (mw)
{
menu->addChild(new rack::MenuSeparator());
appendDeviceConnectionMenu(menu, m, &m->audioThreadActions);
}
}
}
};
9 changes: 9 additions & 0 deletions src/virtualgrid/VirtualGridWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit e59a386

Please sign in to comment.