Loading...
Searching...
No Matches
BitfocusContext.hpp
1#pragma once
2#include <boost/asio/io_context.hpp>
3#include <boost/asio/ip/udp.hpp>
4
5#include <QColor>
6#include <QCoreApplication>
7#include <QJsonArray>
8#include <QJsonDocument>
9#include <QJsonObject>
10#include <QProcess>
11#include <QSocketNotifier>
12
13#include <verdigris>
14#if !defined(_WIN32)
15#include <sys/socket.h>
16#include <sys/un.h>
17
18#include <fcntl.h>
19#include <stdlib.h>
20#include <unistd.h>
21#endif
22namespace bitfocus
23{
24
25using module_configuration = std::map<QString, QVariant>;
27{
29 {
30 struct choice
31 {
32 QString id;
33 QString label;
34 };
35
36 QString id;
37 QString label;
38 // "static-text", "textinput", "number", "checkbox", "choices", "bonjour-device", "dropdown"
39 QString type;
40 QVariant value;
41 QString tooltip;
42 QString regex;
43 // ex. : "e=>!![\"TF\",\"DM3\",\"DM7\"].includes(e.model)&&(e.kaInterval=e.kaIntervalH,!0)",
44 QString isVisibleFn;
45 QVariant min;
46 QVariant max;
47 std::vector<choice> choices;
48 QVariant default_value{}; // true, a number, a string etc
49 double width{};
50 };
51
53 {
54 bool hasLearn{};
55 QString name;
56 std::vector<config_field> options;
57 };
59 {
60 QString name;
61 QVariant value;
62 };
64 {
65 bool hasLearn{};
66 bool isInverted{false};
67 bool disabled{false};
68 int upgradeIndex{0};
69 QString name;
70 std::vector<config_field> options;
71 QString type;
72 struct
73 {
74 QColor bgcolor;
75 QColor color;
76 } defaultStyle;
77 };
79 {
80 QString name;
81 QString category;
82 QString text;
83 QString type;
84 std::vector<QVariantMap> feedbacks;
85 std::vector<QVariantMap> steps;
86 };
87
88 std::map<QString, action_definition> actions;
89 std::map<QString, variable_definition> variables;
90 std::map<QString, feedback_definition> feedbacks;
91 std::map<QString, preset_definition> presets;
92 std::vector<config_field> config_fields;
93 module_configuration config;
94};
95
96// note: callback id shared between both ends so every message has to be processed in order
97#if defined(_WIN32)
98struct win32_handles;
99struct module_handler_base : public QObject
100{
101 std::unique_ptr<win32_handles> handles{};
102 explicit module_handler_base(
103 QString node_path, QString module_path, QString entrypoint);
104 virtual ~module_handler_base();
105 void do_write(std::string_view res);
106 virtual void processMessage(std::string_view) = 0;
107};
108#else
109struct module_handler_base : public QObject
110{
111 char buf[16 * 4096]{};
112 std::vector<char> queue;
113 QProcess process{};
114 QSocketNotifier* socket{};
115 int pfd[2]{};
116
117 explicit module_handler_base(
118 QString node_path, QString module_path, QString entrypoint);
119 virtual ~module_handler_base();
120
121 void on_read(QSocketDescriptor, QSocketNotifier::Type);
122 void do_write(std::string_view res);
123
124 virtual void processMessage(std::string_view) = 0;
125};
126#endif
127
129{
130 W_OBJECT(module_handler)
131public:
132 explicit module_handler(
133 QString path, QString entrypoint, QString nodeVersion, QString apiversion,
134 module_configuration config);
135 virtual ~module_handler();
136
137 using module_handler_base::do_write;
138 void do_write(QString res);
139 QString jsonToString(QJsonObject obj);
140
141 void afterRegistration(std::function<void()>);
142
143 void processMessage(std::string_view v) override;
144
145 int writeRequest(QString name, QString p);
146 void writeReply(QJsonValue id, QString p);
147 void writeReply(QJsonValue id, QJsonObject p);
148 void writeReply(QJsonValue id, QString p, bool success);
149 void writeReply(QJsonValue id, QJsonObject p, bool success);
150
151 // Module -> app (requests handling)
152 void on_register(QJsonValue id);
153 void on_setActionDefinitions(QJsonArray obj);
154 void on_setVariableDefinitions(QJsonArray obj);
155 void on_setFeedbackDefinitions(QJsonArray obj);
156 void on_setPresetDefinitions(QJsonArray obj);
157 void on_setVariableValues(QJsonArray obj);
158 void on_set_status(QJsonObject obj);
159 void on_saveConfig(QJsonObject obj);
160 void on_parseVariablesInString(QJsonValue id, QJsonObject obj);
161 void on_updateFeedbackValues(QJsonObject obj);
162 void on_recordAction(QJsonObject obj);
163 void on_setCustomVariable(QJsonObject obj);
164 void on_sharedUdpSocketJoin(QJsonObject obj);
165 void on_sharedUdpSocketLeave(QJsonObject obj);
166 void on_sharedUdpSocketSend(QJsonObject obj);
167 void on_send_osc(QJsonObject obj);
168
169 module_data::config_field parseConfigField(QJsonObject f);
170
171 // Module -> app (replies handling)
172 void on_response_configFields(QJsonArray fields);
173
174 // App -> module
175
176 int init(QString label);
177 void send_success(QJsonValue id);
178 void updateConfigAndLabel(QString label, module_configuration conf);
179 int requestConfigFields();
180 void updateFeedbacks();
181 void feedbackLearnValues();
182 void feedbackDelete();
183 void variablesChanged();
184 void actionUpdate();
185 void actionDelete();
186 void actionLearnValues();
187 void actionRun(std::string_view act, QVariantMap options);
188 void destroy();
189 void executeHttpRequest();
190 void startStopRecordingActions();
191 void sharedUdpSocketMessage();
192 void sharedUdpSocketError();
193 const bitfocus::module_data& model();
194
195 void configurationParsed() W_SIGNAL(configurationParsed);
196 void variableChanged(QString var, QVariant val) W_SIGNAL(variableChanged, var, val);
197
198private:
199 bitfocus::module_data m_model;
200
201 boost::asio::io_context m_send_service;
202 boost::asio::ip::udp::socket m_socket{m_send_service};
203
204 std::vector<std::function<void()>> m_afterRegistrationQueue;
205 int m_cbid{1};
206
207 int m_init_msg_id{-1};
208 int m_req_cfg_id{-1};
209
210 bool m_expects_label_updates{true};
211 bool m_registered{false};
212};
213
214} // namespace bitfocus
Definition BitfocusContext.hpp:53
Definition BitfocusContext.hpp:31
Definition BitfocusContext.hpp:29
Definition BitfocusContext.hpp:64
Definition BitfocusContext.hpp:79
Definition BitfocusContext.hpp:59
Definition BitfocusContext.hpp:27
Definition BitfocusContext.hpp:110
Definition BitfocusContext.hpp:129
Definition BitfocusContext.win32.cpp:17