Skip to content

Commit

Permalink
Create GridConsumerBase for shared implementation of IGridConsumer; p…
Browse files Browse the repository at this point in the history
…ersist virtual grid mirror settings in patch
  • Loading branch information
Dewb committed Sep 17, 2023
1 parent 1c080e5 commit 2010b77
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 180 deletions.
18 changes: 7 additions & 11 deletions src/common/core/GridConnection/GridConnection.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#include "GridConnection.hpp"

Grid::~Grid()
{
}

void GridConnectionManager::registerGrid(Grid* grid)
{
grids.insert(grid);

// Check if there's a consumer with a saved connection looking for this grid
GridConsumer* consumerToConnect = nullptr;
IGridConsumer* consumerToConnect = nullptr;
for (auto consumer : consumers)
{
if (!isConnected(consumer) && grid->getDevice().id == consumer->gridGetLastDeviceId(true))
Expand All @@ -25,7 +21,7 @@ void GridConnectionManager::registerGrid(Grid* grid)
}
}

void GridConnectionManager::registerGridConsumer(GridConsumer* consumer)
void GridConnectionManager::registerGridConsumer(IGridConsumer* consumer)
{
consumers.insert(consumer);

Expand Down Expand Up @@ -66,13 +62,13 @@ void GridConnectionManager::deregisterGrid(std::string id, bool deleteGrid)
}
}

void GridConnectionManager::deregisterGridConsumer(GridConsumer* consumer)
void GridConnectionManager::deregisterGridConsumer(IGridConsumer* consumer)
{
disconnect(consumer);
consumers.erase(consumer);
}

void GridConnectionManager::connect(Grid* grid, GridConsumer* consumer)
void GridConnectionManager::connect(Grid* grid, IGridConsumer* consumer)
{
disconnect(consumer, true);
disconnect(grid, true);
Expand All @@ -82,7 +78,7 @@ void GridConnectionManager::connect(Grid* grid, GridConsumer* consumer)
consumer->gridConnected(grid);
}

bool GridConnectionManager::isConnected(GridConsumer* consumer)
bool GridConnectionManager::isConnected(IGridConsumer* consumer)
{
return consumerToGridMap.find(consumer) != consumerToGridMap.end();
}
Expand All @@ -100,15 +96,15 @@ void GridConnectionManager::disconnect(Grid* grid, bool ownerChanged)
auto iter = idToConsumerMap.find(grid->getDevice().id);
if (iter != idToConsumerMap.end())
{
GridConsumer* consumer = iter->second;
auto consumer = iter->second;
consumer->gridDisconnected(ownerChanged);
idToConsumerMap.erase(grid->getDevice().id);
consumerToGridMap.erase(consumer);
}
}
}

void GridConnectionManager::disconnect(GridConsumer* consumer, bool ownerChanged)
void GridConnectionManager::disconnect(IGridConsumer* consumer, bool ownerChanged)
{
if (consumer)
{
Expand Down
22 changes: 11 additions & 11 deletions src/common/core/GridConnection/GridConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct SerialOscInterface;

struct Grid
{
virtual ~Grid();
virtual ~Grid() {}
virtual const MonomeDevice& getDevice() = 0;
virtual void updateRow(int x_offset, int y, uint8_t bitfield) = 0;
virtual void updateQuadrant(int x, int y, uint8_t* leds) = 0;
Expand All @@ -18,31 +18,31 @@ struct Grid
virtual bool isHardware() = 0;
};

struct GridConsumer
struct IGridConsumer
{
virtual ~IGridConsumer() {}
virtual void gridConnected(Grid* grid) = 0;
virtual void gridDisconnected(bool ownerChanged) = 0;
virtual std::string gridGetCurrentDeviceId() = 0;
virtual std::string gridGetLastDeviceId(bool owned) = 0;
virtual void gridButtonEvent(int x, int y, bool state) = 0;
virtual void encDeltaEvent(int n, int d) = 0;
virtual Grid* gridGetDevice() = 0;
virtual ~GridConsumer() {};
};

struct GridConnectionManager final
{
// Only call these from the UI thread
void registerGrid(Grid* grid);
void registerGridConsumer(GridConsumer* consumer);
void registerGridConsumer(IGridConsumer* consumer);
void deregisterGrid(std::string id, bool deleteGrid = false);
void deregisterGridConsumer(GridConsumer* consumer);
void deregisterGridConsumer(IGridConsumer* consumer);

void connect(Grid* grid, GridConsumer* consumer);
bool isConnected(GridConsumer* consumer);
void connect(Grid* grid, IGridConsumer* consumer);
bool isConnected(IGridConsumer* consumer);
bool isConnected(std::string id);
void disconnect(Grid* grid, bool ownerChanged = false);
void disconnect(GridConsumer* consumer, bool ownerChanged = false);
void disconnect(IGridConsumer* consumer, bool ownerChanged = false);

void dispatchButtonMessage(MonomeDevice* device, int x, int y, bool state);
void dispatchEncDeltaMessage(MonomeDevice* device, int n, int d);
Expand All @@ -58,8 +58,8 @@ struct GridConnectionManager final
GridConnectionManager(GridConnectionManager&&) = delete;
GridConnectionManager&& operator=(const GridConnectionManager&&) = delete;

std::set<GridConsumer*> consumers;
std::set<IGridConsumer*> consumers;
std::set<Grid*> grids;
std::map<std::string, GridConsumer*> idToConsumerMap;
std::map<GridConsumer*, Grid*> consumerToGridMap;
std::map<std::string, IGridConsumer*> idToConsumerMap;
std::map<IGridConsumer*, Grid*> consumerToGridMap;
};
6 changes: 3 additions & 3 deletions src/common/core/GridConnection/GridConnectionMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace rack;
struct NewConnectGridItem : rack::ui::MenuItem
{
Grid* grid;
GridConsumer* consumer;
IGridConsumer* consumer;
ActionQueue* actionQueue;

void onAction(const rack::event::Action& e) override
Expand All @@ -22,7 +22,7 @@ struct NewConnectGridItem : rack::ui::MenuItem
}
};

void menuUserReacquireGrid(GridConsumer* consumer, std::string lastDeviceId, ActionQueue* actionQueue)
void menuUserReacquireGrid(IGridConsumer* consumer, std::string lastDeviceId, ActionQueue* actionQueue)
{
for (Grid* grid : GridConnectionManager::get().getGrids())
{
Expand All @@ -41,7 +41,7 @@ void menuUserReacquireGrid(GridConsumer* consumer, std::string lastDeviceId, Act
}
}

void appendDeviceConnectionMenu(rack::Menu* menu, GridConsumer* consumer, ActionQueue* actionQueue, bool hardwareOnly)
void appendDeviceConnectionMenu(rack::Menu* menu, IGridConsumer* consumer, ActionQueue* actionQueue, bool hardwareOnly)
{
std::string currentConnectedDeviceId = consumer->gridGetCurrentDeviceId();
std::string lastConnectedDeviceId = consumer->gridGetLastDeviceId(false);
Expand Down
2 changes: 1 addition & 1 deletion src/common/core/GridConnection/GridConnectionMenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#include "GridConnection.hpp"
#include "ActionQueue.hpp"

void appendDeviceConnectionMenu(rack::Menu* menu, GridConsumer* consumer, ActionQueue* queue, bool hardwareOnly = false);
void appendDeviceConnectionMenu(rack::Menu* menu, IGridConsumer* consumer, ActionQueue* queue, bool hardwareOnly = false);
106 changes: 106 additions & 0 deletions src/common/core/GridConnection/GridConsumerBase.cpp
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);
}
}
30 changes: 30 additions & 0 deletions src/common/core/GridConnection/GridConsumerBase.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "GridConnection.hpp"
#include "rack.hpp"

struct GridConsumerBase : public IGridConsumer
{
// IGridConsumer partial implementation; child classes implement gridButtonEvent/encDeltaEvent
void gridConnected(Grid* grid) override;
void gridDisconnected(bool ownerChanged) override;
std::string gridGetCurrentDeviceId() override;
std::string gridGetLastDeviceId(bool owned) override;
Grid* gridGetDevice() override;

GridConsumerBase();
virtual ~GridConsumerBase() {};

void userReacquireGrid();

void saveGridConnectionToJson(json_t* rootJ);
void loadGridConnectionFromJson(json_t* rootJ);

std::string lastConnectedDeviceId;
std::string currentConnectedDeviceId;
bool connectionOwned;

protected:
void toggleGridConnection(Grid* grid);
Grid* gridConnection;
};
Loading

0 comments on commit 2010b77

Please sign in to comment.