-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviewManager.h
256 lines (219 loc) · 7.25 KB
/
viewManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#pragma once
#include "libs/nlohmann/json.hpp"
#include <dlfcn.h>
#include <mutex>
#include <vector>
#include "controllerList.h"
#include "helpers/getTicks.h"
#include "host.h"
#include "log.h"
#include "plugins/components/componentInterface.h"
#include "plugins/components/view.h"
#include "styles.h"
#ifdef DRAW_ST7789
#ifdef USE_DRAW_WITH_SDL
#include "draw/ST7789/drawWithSDL.h"
#else
#include "draw/ST7789/draw.h"
#endif
#elifdef DRAW_SSD1306
#ifdef USE_DRAW_WITH_SDL
#include "draw/SSD1306/drawWithSDL.h"
#else
#include "draw/SSD1306/draw.h"
#endif
#else
#include "draw/SDL/draw.h"
#endif
class ViewManager {
public:
struct Plugin {
std::string name;
std::function<ComponentInterface*(ComponentInterface::Props props)> allocator;
};
std::vector<Plugin> plugins;
protected:
struct SharedComponent {
std::string name;
ComponentInterface* component;
};
View* lastView = NULL;
std::vector<View*> views;
public:
View* view = NULL;
void setView(std::string value, bool force = false)
{
printf("set view string to %s\n", value.c_str());
if (value == "&previous") {
value = lastView->name;
}
for (int i = 0; i < views.size(); i++) {
if (views[i]->name == value) {
if (view != views[i] || force) {
if (lastView != view) {
lastView = view;
}
view = views[i];
view->setGroup(view->resetGroupOnSetView ? 0 : view->activeGroup);
for (int i = 0; i < 256; i++) {
view->onContext(i, contextVar[i]);
}
render();
}
return;
}
}
logWarn("Unknown view: %s\n", value.c_str());
}
protected:
std::mutex m;
// there should be about 4 to 12 encoders, however with 256 we are sure to not be out of bounds
unsigned long lastEncoderTick[256] = { 0 };
static ViewManager* instance;
ViewManager()
: draw(styles)
{
}
void setContext(uint8_t index, float value)
{
// printf("set context %d to %f\n", index, value);
contextVar[index] = value;
view->onContext(index, value);
}
Plugin& loadPlugin(std::string name, nlohmann::json& config)
{
for (auto& plugin : plugins) {
if (plugin.name == name) {
return plugin;
}
}
Plugin plugin;
plugin.name = name;
#ifdef IS_RPI
std::string path = "./plugins/components/Pixel/build/arm/libzic_" + name + "Component.so";
#else
std::string path = "./plugins/components/Pixel/build/x86/libzic_" + name + "Component.so";
#endif
if (config.contains("pluginPath")) {
path = config["pluginPath"].get<std::string>();
}
void* handle = dlopen(path.c_str(), RTLD_LAZY);
if (!handle) {
throw std::runtime_error("Cannot open component library " + path + ": " + dlerror());
}
dlerror();
plugin.allocator = [handle](ComponentInterface::Props props) {
void* allocator = dlsym(handle, "allocator");
return ((ComponentInterface * (*)(ComponentInterface::Props props)) allocator)(props);
};
const char* dlsym_error = dlerror();
if (dlsym_error) {
dlclose(handle);
throw std::runtime_error("Cannot load symbol: " + std::string(dlsym_error));
}
plugins.push_back(plugin);
return plugins.back();
}
void addComponent(nlohmann::json& config, View* targetView)
{
try {
std::string name = config["componentName"].get<std::string>();
// logDebug("Adding component %s", name.c_str());
Point position = { config["bounds"][0].get<int>(), config["bounds"][1].get<int>() };
// Check if width and height exist, otherwise use default values
int w = config.contains("bounds") && config["bounds"].size() > 2 ? config["bounds"][2].get<int>() : styles.screen.w;
int h = config.contains("bounds") && config["bounds"].size() > 3 ? config["bounds"][3].get<int>() : styles.screen.h;
Size size = { w, h };
ComponentInterface::Props props = {
targetView->name + "_" + name + "_x" + std::to_string(position.x) + "_y" + std::to_string(position.y),
config,
position,
size,
getPlugin,
sendAudioEvent,
getController,
targetView,
[this](uint8_t index, float value) { setContext(index, value); }
};
Plugin& plugin = loadPlugin(name, config);
ComponentInterface* component = plugin.allocator(props);
targetView->components.push_back(component);
if (component->jobRendering) {
targetView->componentsJob.push_back(component);
}
} catch (const std::exception& e) {
logError("Error adding component: %s in %s", e.what(), config.dump().c_str());
}
}
public:
#ifdef USE_DRAW_WITH_SDL
DrawWithSDL draw;
#else
Draw draw;
#endif
float contextVar[256] = { 0 };
static ViewManager& get()
{
if (!instance) {
instance = new ViewManager();
}
return *instance;
}
void init()
{
draw.init();
view = views[0];
setView(view->name, true);
for (auto& v : views) {
v->init();
}
}
bool render()
{
m.lock();
if (!views.size()) {
return false;
}
draw.clear();
if (lastView != NULL) {
for (auto& component : lastView->components) {
for (auto* value : component->values) {
value->setOnUpdateCallback([](float, void* data) { }, NULL);
}
}
}
view->initActiveComponents();
m.unlock();
renderComponents();
return true;
}
void renderComponents(unsigned long now = getTicks())
{
m.lock();
view->renderComponents(now);
m.unlock();
}
void config(nlohmann::json& config)
{
if (config.is_array()) {
for (auto& v : config) {
// TODO Might want to move all this in view!!!
if (v.contains("name") && v.contains("components") && v["components"].is_array()) {
logInfo("Loading view %s", v["name"].get<std::string>().c_str());
View* newView = new View(draw, [&](std::string name) { setView(name); }, contextVar);
newView->name = v["name"];
try {
// TODO how to handle extra config?
views.push_back(newView);
for (auto& component : v["components"]) {
addComponent(component, newView);
}
} catch (const std::exception& e) {
logError("view %s config: %s", newView->name.c_str(), e.what());
}
}
}
}
}
};
ViewManager* ViewManager::instance = NULL;