-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor grid state management and add grid mirror mode (#176)
* Remove firmware products in res/ on make clean * Remove serialosc pre-init on module widget load * Prototype of virtual grid mirror mode * null-init mirror consumer gridConnection properly * Move mirror options to a submenu & only list hardware * Create GridConsumerBase for shared implementation of IGridConsumer; persist virtual grid mirror settings in patch
- Loading branch information
Showing
13 changed files
with
423 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
#include "rack.hpp" | ||
#include <functional> | ||
|
||
typedef std::function<void(void)> Action; | ||
typedef rack::dsp::RingBuffer<Action, 4> ActionQueue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#include "GridConnectionMenu.hpp" | ||
#include "SerialOscInterface.hpp" | ||
|
||
using namespace rack; | ||
|
||
struct NewConnectGridItem : rack::ui::MenuItem | ||
{ | ||
Grid* grid; | ||
IGridConsumer* consumer; | ||
ActionQueue* actionQueue; | ||
|
||
void onAction(const rack::event::Action& e) override | ||
{ | ||
if (actionQueue) | ||
{ | ||
auto thisGrid = grid; | ||
auto thisConsumer = consumer; | ||
|
||
actionQueue->push([thisGrid, thisConsumer]() | ||
{ GridConnectionManager::get().connect(thisGrid, thisConsumer); }); | ||
} | ||
} | ||
}; | ||
|
||
void menuUserReacquireGrid(IGridConsumer* consumer, std::string lastDeviceId, ActionQueue* actionQueue) | ||
{ | ||
for (Grid* grid : GridConnectionManager::get().getGrids()) | ||
{ | ||
if (lastDeviceId == grid->getDevice().id) | ||
{ | ||
if (actionQueue) | ||
{ | ||
actionQueue->push([grid, consumer]() | ||
{ | ||
GridConnectionManager::get().connect(grid, consumer); | ||
} | ||
); | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
|
||
void appendDeviceConnectionMenu(rack::Menu* menu, IGridConsumer* consumer, ActionQueue* actionQueue, bool hardwareOnly) | ||
{ | ||
std::string currentConnectedDeviceId = consumer->gridGetCurrentDeviceId(); | ||
std::string lastConnectedDeviceId = consumer->gridGetLastDeviceId(false); | ||
|
||
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()) | ||
{ | ||
if (hardwareOnly && !grid->isHardware()) | ||
{ | ||
continue; | ||
} | ||
|
||
auto connectItem = new NewConnectGridItem(); | ||
connectItem->text = "└ " + grid->getDevice().type + " (" + grid->getDevice().id + ") "; | ||
|
||
auto rightText = ""; | ||
if (currentConnectedDeviceId == grid->getDevice().id) | ||
{ | ||
rightText = "✔"; | ||
preferredDeviceFound = true; | ||
} | ||
else if (currentConnectedDeviceId == "" && lastConnectedDeviceId == grid->getDevice().id) | ||
{ | ||
rightText = "⋯"; | ||
preferredDeviceFound = true; | ||
} | ||
|
||
connectItem->rightText = rightText; | ||
connectItem->grid = grid; | ||
connectItem->actionQueue = actionQueue; | ||
connectItem->consumer = consumer; | ||
|
||
menu->addChild(connectItem); | ||
deviceCount++; | ||
} | ||
|
||
if (deviceCount == 0) | ||
{ | ||
std::string message = hardwareOnly ? "no hardware devices found" : "no hardware or virtual devices found"; | ||
menu->addChild(construct<MenuLabel>(&MenuLabel::text, " (" + message + ")")); | ||
} | ||
|
||
if (currentConnectedDeviceId == "" && lastConnectedDeviceId != "") | ||
{ | ||
if (preferredDeviceFound) | ||
{ | ||
menu->addChild(createMenuItem("Reacquire grid", "", [=]() | ||
{ | ||
if (lastConnectedDeviceId != "") | ||
{ | ||
menuUserReacquireGrid(consumer, lastConnectedDeviceId, actionQueue); | ||
} | ||
} | ||
)); | ||
} | ||
else | ||
{ | ||
menu->addChild(construct<MenuLabel>(&MenuLabel::text, "Can't reacquire grid (" + lastConnectedDeviceId + " not found)")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
#include "rack.hpp" | ||
#include "GridConnection.hpp" | ||
#include "ActionQueue.hpp" | ||
|
||
void appendDeviceConnectionMenu(rack::Menu* menu, IGridConsumer* consumer, ActionQueue* queue, bool hardwareOnly = false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include "GridConsumerBase.hpp" | ||
|
||
GridConsumerBase::GridConsumerBase() | ||
: lastConnectedDeviceId("") | ||
, currentConnectedDeviceId("") | ||
, gridConnection(nullptr) | ||
{ | ||
|
||
} | ||
|
||
void GridConsumerBase::gridConnected(Grid* newConnection) | ||
{ | ||
gridConnection = newConnection; | ||
if (gridConnection) | ||
{ | ||
std::string id = gridConnection->getDevice().id; | ||
lastConnectedDeviceId = id; | ||
currentConnectedDeviceId = id; | ||
connectionOwned = true; | ||
} | ||
} | ||
|
||
void GridConsumerBase::gridDisconnected(bool ownerChanged) | ||
{ | ||
gridConnection = nullptr; | ||
currentConnectedDeviceId = ""; | ||
|
||
if (ownerChanged) | ||
{ | ||
connectionOwned = false; | ||
} | ||
} | ||
|
||
std::string GridConsumerBase::gridGetCurrentDeviceId() | ||
{ | ||
return currentConnectedDeviceId; | ||
} | ||
|
||
std::string GridConsumerBase::gridGetLastDeviceId(bool owned) | ||
{ | ||
if (owned && !connectionOwned) | ||
{ | ||
return ""; | ||
} | ||
|
||
return lastConnectedDeviceId; | ||
} | ||
|
||
Grid* GridConsumerBase::gridGetDevice() | ||
{ | ||
return gridConnection; | ||
} | ||
|
||
void GridConsumerBase::userReacquireGrid() | ||
{ | ||
if (lastConnectedDeviceId != "" && gridConnection == nullptr) | ||
{ | ||
for (Grid* grid : GridConnectionManager::get().getGrids()) | ||
{ | ||
if (gridGetLastDeviceId(false) == grid->getDevice().id) | ||
{ | ||
GridConnectionManager::get().connect(grid, this); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
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; | ||
if (gridConnection) | ||
{ | ||
deviceId = gridConnection->getDevice().id; | ||
} | ||
json_object_set_new(rootJ, "connectedDeviceId", json_string(deviceId.c_str())); | ||
json_object_set_new(rootJ, "connectionOwned", json_boolean(connectionOwned)); | ||
} | ||
|
||
void GridConsumerBase::loadGridConnectionFromJson(json_t* rootJ) | ||
{ | ||
json_t* id = json_object_get(rootJ, "connectedDeviceId"); | ||
if (id) | ||
{ | ||
lastConnectedDeviceId = json_string_value(id); | ||
} | ||
|
||
json_t* owned = json_object_get(rootJ, "connectionOwned"); | ||
if (owned) | ||
{ | ||
connectionOwned = json_boolean_value(owned); | ||
} | ||
} |
Oops, something went wrong.