Loading...
Searching...
No Matches
jsonrpc.h
Go to the documentation of this file.
/*
* Copyright (C) 2009, 2010 Hermann Meyer, James Warden, Andreas Degert
* Copyright (C) 2011 Pete Shorthose
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#ifndef SRC_HEADERS_JSONRPC_H_
#define SRC_HEADERS_JSONRPC_H_
#include "engine.h"
#include <bitset>
#include <queue>
#include <giomm/init.h> // NOLINT
#include <giomm/socketservice.h>
#ifndef GUITARIX_AS_PLUGIN
#include "jsonrpc_methods.h"
#else
#include "jsonrpc_methods-generated.h"
#endif
class GxService;
class JsonValue {
protected:
JsonValue() {}
virtual ~JsonValue() {}
friend class JsonArray;
public:
virtual double getFloat() const;
virtual int getInt() const;
virtual const Glib::ustring& getString() const;
virtual gx_system::JsonSubParser getSubParser() const;
};
class JsonArray: public std::vector<JsonValue*> {
public:
JsonArray():std::vector<JsonValue*>() {}
~JsonArray();
JsonValue *operator[](unsigned int i);
void append(gx_system::JsonParser& jp);
};
class CmdConnection: public sigc::trackable {
public:
struct methodnames {
const char *name;
jsonrpc_method m_id;
};
enum msg_type {
f_preset_changed,
f_state_changed,
f_freq_changed,
f_display,
f_display_state,
f_selection_done,
f_presetlist_changed,
f_log_message,
f_midi_changed,
f_midi_value_changed,
f_parameter_change_notify,
f_plugins_changed,
f_misc_msg,
f_units_changed,
END_OF_FLAGS
};
private:
GxService& serv;
Glib::RefPtr<Gio::SocketConnection> connection;
std::list<std::string> outgoing;
unsigned int current_offset;
gx_system::JsonStringParser jp;
bool midi_config_mode;
std::bitset<END_OF_FLAGS> flags;
std::map<string,float> maxlevel;
private:
bool find_token(const Glib::ustring& token, msg_type *start, msg_type *end);
void activate(int n, bool v) { flags.set(n, v); }
void exec(Glib::ustring cmd);
void call(gx_system::JsonWriter& jw, const methodnames *mn, JsonArray& params);
void notify(gx_system::JsonStringWriter& jw, const methodnames *mn, JsonArray& params);
bool request(gx_system::JsonStringParser& jp, gx_system::JsonStringWriter& jw, bool batch_start);
void write_error(gx_system::JsonWriter& jw, int code, const char *message);
void write_error(gx_system::JsonWriter& jw, int code, Glib::ustring& message) { write_error(jw, code, message.c_str()); }
void error_response(gx_system::JsonWriter& jw, int code, const char *message);
void error_response(gx_system::JsonWriter& jw, int code, const Glib::ustring& message) { error_response(jw, code, message.c_str()); }
void send_notify_begin(gx_system::JsonStringWriter& jw, const char *method) { jw.send_notify_begin(method); }
void send_notify_end(gx_system::JsonStringWriter& jw, bool send_out=true);
void listen(const Glib::ustring& tp);
void unlisten(const Glib::ustring& tp);
void process(gx_system::JsonStringParser& jp);
public:
CmdConnection(GxService& serv, const Glib::RefPtr<Gio::SocketConnection>& connection_);
~CmdConnection();
bool on_data_in(Glib::IOCondition cond);
bool on_data_out(Glib::IOCondition cond);
void send(gx_system::JsonStringWriter& jw);
bool is_activated(msg_type n) { return flags[n]; }
void update_maxlevel(const std::string& id, float v) { float& m = maxlevel[id]; m = max(m, v); }
friend class UiBuilderVirt;
};
struct broadcast_data {
gx_system::JsonStringWriter *jw;
CmdConnection::msg_type n;
CmdConnection *sender;
};
class GxService: public Gio::SocketService {
private:
struct ChangedPlugin {
std::string id;
gx_engine::PluginChange::pc status;
ChangedPlugin(const std::string& id_, gx_engine::PluginChange::pc status_): id(id_), status(status_) {}
};
private:
gx_preset::GxSettings& settings;
gx_jack::GxJack& jack;
TunerSwitcher& tuner_switcher;
sigc::slot<void> quit_mainloop;
time_t oldest_unsaved;
time_t last_change;
sigc::connection save_conn;
std::list<CmdConnection*> connection_list;
std::queue<broadcast_data> broadcast_list;
gx_system::JsonStringWriter *jwc;
std::map<std::string,bool> *preg_map;
std::map<std::string,float> maxlevel;
private:
virtual bool on_incoming(const Glib::RefPtr<Gio::SocketConnection>& connection,
const Glib::RefPtr<Glib::Object>& source_object);
void save_state();
void remove_connection(CmdConnection* p);
bool broadcast_listeners(CmdConnection::msg_type n, CmdConnection *sender = 0);
void broadcast(gx_system::JsonStringWriter& jw, CmdConnection::msg_type n, CmdConnection *sender = 0);
bool idle_broadcast_handler();
void connect_value_changed_signal(gx_engine::Parameter *p);
// message formatting functions
void serialize_parameter_change(gx_system::JsonWriter& jw);
void ladspaloader_write_changes(gx_system::JsonWriter& jw, std::vector<ChangedPlugin>& changed_plugins);
// signal handler
void on_param_insert_remove(gx_engine::Parameter *p, bool insert);
void on_param_value_changed(gx_engine::Parameter *p);
void preset_changed();
void on_engine_state_change(gx_engine::GxEngineState state);
void on_tuner_freq_changed();
void display(const Glib::ustring& bank, const Glib::ustring& preset);
void set_display_state(TunerSwitcher::SwitcherState newstate);
void on_selection_done(bool v);
void on_presetlist_changed();
void on_log_message(const string& msg, GxLogger::MsgType tp, bool plugged);
void on_midi_changed();
void on_midi_value_changed(int ctl, int value);
void on_rack_unit_changed(bool stereo);
static void add_changed_plugin(gx_engine::Plugin* pl, gx_engine::PluginChange::pc v,
std::vector<ChangedPlugin>& vec);
void create_bluetooth_sockets(const Glib::ustring& host);
friend class CmdConnection;
public:
GxService(gx_preset::GxSettings& settings_, gx_jack::GxJack& jack_,
TunerSwitcher& tunerswitcher, sigc::slot<void> quit_mainloop_,
const Glib::ustring& host, int *port);
~GxService();
void send_rack_changed(bool stereo, CmdConnection *cmd);
void ladspaloader_update_plugins(gx_system::JsonWriter *jw, CmdConnection *cmd);
float update_maxlevel(const std::string& id, bool reset=false);
};
const char *engine_state_to_string(gx_engine::GxEngineState s);
gx_engine::GxEngineState string_to_engine_state(const std::string& s);
#endif // SRC_HEADERS_JSONRPC_H_
CmdConnection
Definition jsonrpc.h:58
CmdConnection::current_offset
unsigned int current_offset
Definition jsonrpc.h:85
CmdConnection::call
void call(gx_system::JsonWriter &jw, const methodnames *mn, JsonArray &params)
CmdConnection::jp
gx_system::JsonStringParser jp
Definition jsonrpc.h:86
CmdConnection::on_data_in
bool on_data_in(Glib::IOCondition cond)
CmdConnection::is_activated
bool is_activated(msg_type n)
Definition jsonrpc.h:113
CmdConnection::connection
Glib::RefPtr< Gio::SocketConnection > connection
Definition jsonrpc.h:83
CmdConnection::send_notify_begin
void send_notify_begin(gx_system::JsonStringWriter &jw, const char *method)
Definition jsonrpc.h:101
CmdConnection::activate
void activate(int n, bool v)
Definition jsonrpc.h:92
CmdConnection::UiBuilderVirt
friend class UiBuilderVirt
Definition jsonrpc.h:115
CmdConnection::serv
GxService & serv
Definition jsonrpc.h:82
CmdConnection::write_error
void write_error(gx_system::JsonWriter &jw, int code, const char *message)
CmdConnection::update_maxlevel
void update_maxlevel(const std::string &id, float v)
Definition jsonrpc.h:114
CmdConnection::error_response
void error_response(gx_system::JsonWriter &jw, int code, const Glib::ustring &message)
Definition jsonrpc.h:100
CmdConnection::exec
void exec(Glib::ustring cmd)
CmdConnection::notify
void notify(gx_system::JsonStringWriter &jw, const methodnames *mn, JsonArray &params)
CmdConnection::outgoing
std::list< std::string > outgoing
Definition jsonrpc.h:84
CmdConnection::write_error
void write_error(gx_system::JsonWriter &jw, int code, Glib::ustring &message)
Definition jsonrpc.h:98
CmdConnection::listen
void listen(const Glib::ustring &tp)
CmdConnection::error_response
void error_response(gx_system::JsonWriter &jw, int code, const char *message)
CmdConnection::send_notify_end
void send_notify_end(gx_system::JsonStringWriter &jw, bool send_out=true)
CmdConnection::on_data_out
bool on_data_out(Glib::IOCondition cond)
CmdConnection::CmdConnection
CmdConnection(GxService &serv, const Glib::RefPtr< Gio::SocketConnection > &connection_)
CmdConnection::find_token
bool find_token(const Glib::ustring &token, msg_type *start, msg_type *end)
CmdConnection::request
bool request(gx_system::JsonStringParser &jp, gx_system::JsonStringWriter &jw, bool batch_start)
CmdConnection::unlisten
void unlisten(const Glib::ustring &tp)
CmdConnection::flags
std::bitset< END_OF_FLAGS > flags
Definition jsonrpc.h:88
CmdConnection::midi_config_mode
bool midi_config_mode
Definition jsonrpc.h:87
CmdConnection::send
void send(gx_system::JsonStringWriter &jw)
CmdConnection::msg_type
msg_type
Definition jsonrpc.h:64
CmdConnection::f_freq_changed
@ f_freq_changed
Definition jsonrpc.h:67
CmdConnection::f_display_state
@ f_display_state
Definition jsonrpc.h:69
CmdConnection::f_midi_value_changed
@ f_midi_value_changed
Definition jsonrpc.h:74
CmdConnection::f_plugins_changed
@ f_plugins_changed
Definition jsonrpc.h:76
CmdConnection::f_misc_msg
@ f_misc_msg
Definition jsonrpc.h:77
CmdConnection::f_units_changed
@ f_units_changed
Definition jsonrpc.h:78
CmdConnection::f_presetlist_changed
@ f_presetlist_changed
Definition jsonrpc.h:71
CmdConnection::f_display
@ f_display
Definition jsonrpc.h:68
CmdConnection::f_parameter_change_notify
@ f_parameter_change_notify
Definition jsonrpc.h:75
CmdConnection::f_midi_changed
@ f_midi_changed
Definition jsonrpc.h:73
CmdConnection::f_log_message
@ f_log_message
Definition jsonrpc.h:72
CmdConnection::f_state_changed
@ f_state_changed
Definition jsonrpc.h:66
CmdConnection::f_selection_done
@ f_selection_done
Definition jsonrpc.h:70
CmdConnection::f_preset_changed
@ f_preset_changed
Definition jsonrpc.h:65
CmdConnection::END_OF_FLAGS
@ END_OF_FLAGS
Definition jsonrpc.h:79
CmdConnection::maxlevel
std::map< string, float > maxlevel
Definition jsonrpc.h:89
CmdConnection::process
void process(gx_system::JsonStringParser &jp)
CmdConnection::~CmdConnection
~CmdConnection()
GxLogger::MsgType
MsgType
Definition gx_logging.h:37
GxService
Definition jsonrpc.h:124
GxService::~GxService
~GxService()
GxService::save_conn
sigc::connection save_conn
Definition jsonrpc.h:138
GxService::send_rack_changed
void send_rack_changed(bool stereo, CmdConnection *cmd)
GxService::ladspaloader_write_changes
void ladspaloader_write_changes(gx_system::JsonWriter &jw, std::vector< ChangedPlugin > &changed_plugins)
GxService::CmdConnection
friend class CmdConnection
Definition jsonrpc.h:176
GxService::broadcast
void broadcast(gx_system::JsonStringWriter &jw, CmdConnection::msg_type n, CmdConnection *sender=0)
GxService::GxService
GxService(gx_preset::GxSettings &settings_, gx_jack::GxJack &jack_, TunerSwitcher &tunerswitcher, sigc::slot< void > quit_mainloop_, const Glib::ustring &host, int *port)
GxService::serialize_parameter_change
void serialize_parameter_change(gx_system::JsonWriter &jw)
GxService::on_rack_unit_changed
void on_rack_unit_changed(bool stereo)
GxService::create_bluetooth_sockets
void create_bluetooth_sockets(const Glib::ustring &host)
GxService::on_selection_done
void on_selection_done(bool v)
GxService::ladspaloader_update_plugins
void ladspaloader_update_plugins(gx_system::JsonWriter *jw, CmdConnection *cmd)
GxService::remove_connection
void remove_connection(CmdConnection *p)
GxService::broadcast_listeners
bool broadcast_listeners(CmdConnection::msg_type n, CmdConnection *sender=0)
GxService::save_state
void save_state()
GxService::preset_changed
void preset_changed()
GxService::on_presetlist_changed
void on_presetlist_changed()
GxService::on_param_value_changed
void on_param_value_changed(gx_engine::Parameter *p)
GxService::on_engine_state_change
void on_engine_state_change(gx_engine::GxEngineState state)
GxService::maxlevel
std::map< std::string, float > maxlevel
Definition jsonrpc.h:143
GxService::preg_map
std::map< std::string, bool > * preg_map
Definition jsonrpc.h:142
GxService::add_changed_plugin
static void add_changed_plugin(gx_engine::Plugin *pl, gx_engine::PluginChange::pc v, std::vector< ChangedPlugin > &vec)
GxService::jwc
gx_system::JsonStringWriter * jwc
Definition jsonrpc.h:141
GxService::broadcast_list
std::queue< broadcast_data > broadcast_list
Definition jsonrpc.h:140
GxService::on_tuner_freq_changed
void on_tuner_freq_changed()
GxService::last_change
time_t last_change
Definition jsonrpc.h:137
GxService::settings
gx_preset::GxSettings & settings
Definition jsonrpc.h:132
GxService::on_log_message
void on_log_message(const string &msg, GxLogger::MsgType tp, bool plugged)
GxService::display
void display(const Glib::ustring &bank, const Glib::ustring &preset)
GxService::oldest_unsaved
time_t oldest_unsaved
Definition jsonrpc.h:136
GxService::tuner_switcher
TunerSwitcher & tuner_switcher
Definition jsonrpc.h:134
GxService::on_midi_changed
void on_midi_changed()
GxService::connection_list
std::list< CmdConnection * > connection_list
Definition jsonrpc.h:139
GxService::on_incoming
virtual bool on_incoming(const Glib::RefPtr< Gio::SocketConnection > &connection, const Glib::RefPtr< Glib::Object > &source_object)
GxService::on_param_insert_remove
void on_param_insert_remove(gx_engine::Parameter *p, bool insert)
GxService::connect_value_changed_signal
void connect_value_changed_signal(gx_engine::Parameter *p)
GxService::update_maxlevel
float update_maxlevel(const std::string &id, bool reset=false)
GxService::idle_broadcast_handler
bool idle_broadcast_handler()
GxService::set_display_state
void set_display_state(TunerSwitcher::SwitcherState newstate)
GxService::on_midi_value_changed
void on_midi_value_changed(int ctl, int value)
GxService::jack
gx_jack::GxJack & jack
Definition jsonrpc.h:133
GxService::quit_mainloop
sigc::slot< void > quit_mainloop
Definition jsonrpc.h:135
JsonArray
Definition jsonrpc.h:50
JsonArray::operator[]
JsonValue * operator[](unsigned int i)
JsonArray::JsonArray
JsonArray()
Definition jsonrpc.h:52
JsonArray::append
void append(gx_system::JsonParser &jp)
JsonArray::~JsonArray
~JsonArray()
JsonValue
Definition jsonrpc.h:38
JsonValue::getInt
virtual int getInt() const
JsonValue::~JsonValue
virtual ~JsonValue()
Definition jsonrpc.h:41
JsonValue::getSubParser
virtual gx_system::JsonSubParser getSubParser() const
JsonValue::JsonValue
JsonValue()
Definition jsonrpc.h:40
JsonValue::getString
virtual const Glib::ustring & getString() const
JsonValue::getFloat
virtual double getFloat() const
JsonValue::JsonArray
friend class JsonArray
Definition jsonrpc.h:42
TunerSwitcher
Definition tunerswitcher.h:29
TunerSwitcher::SwitcherState
SwitcherState
Definition tunerswitcher.h:31
gx_engine::Parameter
Definition gx_parameter.h:107
gx_engine::Plugin
Definition gx_pluginloader.h:45
gx_jack::GxJack
Definition gx_jack.h:168
gx_preset::GxSettings
Definition gx_preset.h:140
gx_system::JsonParser
Definition gx_json.h:112
gx_system::JsonStringParser
Definition gx_json.h:202
gx_system::JsonStringWriter
Definition gx_json.h:99
gx_system::JsonStringWriter::send_notify_begin
void send_notify_begin(const char *method)
gx_system::JsonSubParser
Definition gx_json.h:194
gx_system::JsonWriter
Definition gx_json.h:55
engine.h
max
#define max(x, y)
Definition gx_faust_support.h:7
engine_state_to_string
const char * engine_state_to_string(gx_engine::GxEngineState s)
string_to_engine_state
gx_engine::GxEngineState string_to_engine_state(const std::string &s)
gx_engine::GxEngineState
GxEngineState
Definition gx_modulesequencer.h:301
CmdConnection::methodnames
Definition jsonrpc.h:60
CmdConnection::methodnames::m_id
jsonrpc_method m_id
Definition jsonrpc.h:62
CmdConnection::methodnames::name
const char * name
Definition jsonrpc.h:61
GxService::ChangedPlugin::status
gx_engine::PluginChange::pc status
Definition jsonrpc.h:128
GxService::ChangedPlugin::ChangedPlugin
ChangedPlugin(const std::string &id_, gx_engine::PluginChange::pc status_)
Definition jsonrpc.h:129
GxService::ChangedPlugin::id
std::string id
Definition jsonrpc.h:127
broadcast_data
Definition jsonrpc.h:118
broadcast_data::sender
CmdConnection * sender
Definition jsonrpc.h:121
broadcast_data::jw
gx_system::JsonStringWriter * jw
Definition jsonrpc.h:119
broadcast_data::n
CmdConnection::msg_type n
Definition jsonrpc.h:120
gx_engine::PluginChange::pc
pc
Definition gx_engine.h:66