Guitarix
jsonrpc.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009, 2010 Hermann Meyer, James Warden, Andreas Degert
3 * Copyright (C) 2011 Pete Shorthose
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#ifndef SRC_HEADERS_JSONRPC_H_
23#define SRC_HEADERS_JSONRPC_H_
24
25#include "engine.h"
26#include <bitset>
27#include <queue>
28#include <giomm/init.h> // NOLINT
29#include <giomm/socketservice.h>
30#include "jsonrpc_methods.h"
31
32class GxService;
33
34class JsonValue {
35protected:
37 virtual ~JsonValue() {}
38 friend class JsonArray;
39public:
40 virtual double getFloat() const;
41 virtual int getInt() const;
42 virtual const Glib::ustring& getString() const;
44};
45
46class JsonArray: public std::vector<JsonValue*> {
47public:
48 JsonArray():std::vector<JsonValue*>() {}
50 JsonValue *operator[](unsigned int i);
52};
53
54class CmdConnection: public sigc::trackable {
55public:
56 struct methodnames {
57 const char *name;
58 jsonrpc_method m_id;
59 };
60 enum msg_type {
76 };
77private:
79 Glib::RefPtr<Gio::SocketConnection> connection;
80 std::list<std::string> outgoing;
81 unsigned int current_offset;
84 std::bitset<END_OF_FLAGS> flags;
85 std::map<string,float> maxlevel;
86private:
87 bool find_token(const Glib::ustring& token, msg_type *start, msg_type *end);
88 void activate(int n, bool v) { flags.set(n, v); }
89 void exec(Glib::ustring cmd);
90 void call(gx_system::JsonWriter& jw, const methodnames *mn, JsonArray& params);
93 void write_error(gx_system::JsonWriter& jw, int code, const char *message);
94 void write_error(gx_system::JsonWriter& jw, int code, Glib::ustring& message) { write_error(jw, code, message.c_str()); }
95 void error_response(gx_system::JsonWriter& jw, int code, const char *message);
96 void error_response(gx_system::JsonWriter& jw, int code, const Glib::ustring& message) { error_response(jw, code, message.c_str()); }
97 void send_notify_begin(gx_system::JsonStringWriter& jw, const char *method) { jw.send_notify_begin(method); }
98 void send_notify_end(gx_system::JsonStringWriter& jw, bool send_out=true);
99 void listen(const Glib::ustring& tp);
100 void unlisten(const Glib::ustring& tp);
102
103public:
104 CmdConnection(GxService& serv, const Glib::RefPtr<Gio::SocketConnection>& connection_);
106 bool on_data_in(Glib::IOCondition cond);
107 bool on_data_out(Glib::IOCondition cond);
109 bool is_activated(msg_type n) { return flags[n]; }
110 void update_maxlevel(const std::string& id, float v) { float& m = maxlevel[id]; m = max(m, v); }
111 friend class UiBuilderVirt;
112};
113
118};
119
120class GxService: public Gio::SocketService {
121private:
123 std::string id;
125 ChangedPlugin(const std::string& id_, gx_engine::PluginChange::pc status_): id(id_), status(status_) {}
126 };
127private:
131 sigc::slot<void> quit_mainloop;
134 sigc::connection save_conn;
135 std::list<CmdConnection*> connection_list;
136 std::queue<broadcast_data> broadcast_list;
138 std::map<std::string,bool> *preg_map;
139 std::map<std::string,float> maxlevel;
140private:
141 virtual bool on_incoming(const Glib::RefPtr<Gio::SocketConnection>& connection,
142 const Glib::RefPtr<Glib::Object>& source_object);
149
150 // message formatting functions
152 void ladspaloader_write_changes(gx_system::JsonWriter& jw, std::vector<ChangedPlugin>& changed_plugins);
153
154 // signal handler
160 void display(const Glib::ustring& bank, const Glib::ustring& preset);
162 void on_selection_done(bool v);
164 void on_log_message(const string& msg, GxLogger::MsgType tp, bool plugged);
166 void on_midi_value_changed(int ctl, int value);
167 void on_rack_unit_changed(bool stereo);
169 std::vector<ChangedPlugin>& vec);
170 void create_bluetooth_sockets(const Glib::ustring& host);
171
172 friend class CmdConnection;
173public:
175 TunerSwitcher& tunerswitcher, sigc::slot<void> quit_mainloop_,
176 const Glib::ustring& host, int *port);
178 void send_rack_changed(bool stereo, CmdConnection *cmd);
180 float update_maxlevel(const std::string& id, bool reset=false);
181};
182
185
186#endif // SRC_HEADERS_JSONRPC_H_
unsigned int current_offset
Definition: jsonrpc.h:81
void call(gx_system::JsonWriter &jw, const methodnames *mn, JsonArray &params)
gx_system::JsonStringParser jp
Definition: jsonrpc.h:82
bool on_data_in(Glib::IOCondition cond)
bool is_activated(msg_type n)
Definition: jsonrpc.h:109
Glib::RefPtr< Gio::SocketConnection > connection
Definition: jsonrpc.h:79
void send_notify_begin(gx_system::JsonStringWriter &jw, const char *method)
Definition: jsonrpc.h:97
void activate(int n, bool v)
Definition: jsonrpc.h:88
friend class UiBuilderVirt
Definition: jsonrpc.h:111
GxService & serv
Definition: jsonrpc.h:78
void write_error(gx_system::JsonWriter &jw, int code, const char *message)
void update_maxlevel(const std::string &id, float v)
Definition: jsonrpc.h:110
void error_response(gx_system::JsonWriter &jw, int code, const Glib::ustring &message)
Definition: jsonrpc.h:96
void exec(Glib::ustring cmd)
void notify(gx_system::JsonStringWriter &jw, const methodnames *mn, JsonArray &params)
std::list< std::string > outgoing
Definition: jsonrpc.h:80
void write_error(gx_system::JsonWriter &jw, int code, Glib::ustring &message)
Definition: jsonrpc.h:94
void listen(const Glib::ustring &tp)
void error_response(gx_system::JsonWriter &jw, int code, const char *message)
void send_notify_end(gx_system::JsonStringWriter &jw, bool send_out=true)
bool on_data_out(Glib::IOCondition cond)
CmdConnection(GxService &serv, const Glib::RefPtr< Gio::SocketConnection > &connection_)
bool find_token(const Glib::ustring &token, msg_type *start, msg_type *end)
bool request(gx_system::JsonStringParser &jp, gx_system::JsonStringWriter &jw, bool batch_start)
void unlisten(const Glib::ustring &tp)
std::bitset< END_OF_FLAGS > flags
Definition: jsonrpc.h:84
bool midi_config_mode
Definition: jsonrpc.h:83
void send(gx_system::JsonStringWriter &jw)
@ f_freq_changed
Definition: jsonrpc.h:63
@ f_display_state
Definition: jsonrpc.h:65
@ f_midi_value_changed
Definition: jsonrpc.h:70
@ f_plugins_changed
Definition: jsonrpc.h:72
@ f_units_changed
Definition: jsonrpc.h:74
@ f_presetlist_changed
Definition: jsonrpc.h:67
@ f_parameter_change_notify
Definition: jsonrpc.h:71
@ f_midi_changed
Definition: jsonrpc.h:69
@ f_log_message
Definition: jsonrpc.h:68
@ f_state_changed
Definition: jsonrpc.h:62
@ f_selection_done
Definition: jsonrpc.h:66
@ f_preset_changed
Definition: jsonrpc.h:61
@ END_OF_FLAGS
Definition: jsonrpc.h:75
std::map< string, float > maxlevel
Definition: jsonrpc.h:85
void process(gx_system::JsonStringParser &jp)
sigc::connection save_conn
Definition: jsonrpc.h:134
void send_rack_changed(bool stereo, CmdConnection *cmd)
void ladspaloader_write_changes(gx_system::JsonWriter &jw, std::vector< ChangedPlugin > &changed_plugins)
void broadcast(gx_system::JsonStringWriter &jw, CmdConnection::msg_type n, CmdConnection *sender=0)
GxService(gx_preset::GxSettings &settings_, gx_jack::GxJack &jack_, TunerSwitcher &tunerswitcher, sigc::slot< void > quit_mainloop_, const Glib::ustring &host, int *port)
void serialize_parameter_change(gx_system::JsonWriter &jw)
void on_rack_unit_changed(bool stereo)
void create_bluetooth_sockets(const Glib::ustring &host)
void on_selection_done(bool v)
void ladspaloader_update_plugins(gx_system::JsonWriter *jw, CmdConnection *cmd)
void remove_connection(CmdConnection *p)
bool broadcast_listeners(CmdConnection::msg_type n, CmdConnection *sender=0)
void save_state()
void preset_changed()
void on_presetlist_changed()
void on_param_value_changed(gx_engine::Parameter *p)
void on_engine_state_change(gx_engine::GxEngineState state)
std::map< std::string, float > maxlevel
Definition: jsonrpc.h:139
std::map< std::string, bool > * preg_map
Definition: jsonrpc.h:138
static void add_changed_plugin(gx_engine::Plugin *pl, gx_engine::PluginChange::pc v, std::vector< ChangedPlugin > &vec)
gx_system::JsonStringWriter * jwc
Definition: jsonrpc.h:137
std::queue< broadcast_data > broadcast_list
Definition: jsonrpc.h:136
void on_tuner_freq_changed()
time_t last_change
Definition: jsonrpc.h:133
gx_preset::GxSettings & settings
Definition: jsonrpc.h:128
void on_log_message(const string &msg, GxLogger::MsgType tp, bool plugged)
void display(const Glib::ustring &bank, const Glib::ustring &preset)
time_t oldest_unsaved
Definition: jsonrpc.h:132
TunerSwitcher & tuner_switcher
Definition: jsonrpc.h:130
void on_midi_changed()
std::list< CmdConnection * > connection_list
Definition: jsonrpc.h:135
virtual bool on_incoming(const Glib::RefPtr< Gio::SocketConnection > &connection, const Glib::RefPtr< Glib::Object > &source_object)
void on_param_insert_remove(gx_engine::Parameter *p, bool insert)
void connect_value_changed_signal(gx_engine::Parameter *p)
float update_maxlevel(const std::string &id, bool reset=false)
bool idle_broadcast_handler()
void set_display_state(TunerSwitcher::SwitcherState newstate)
void on_midi_value_changed(int ctl, int value)
gx_jack::GxJack & jack
Definition: jsonrpc.h:129
sigc::slot< void > quit_mainloop
Definition: jsonrpc.h:131
JsonValue * operator[](unsigned int i)
JsonArray()
Definition: jsonrpc.h:48
void append(gx_system::JsonParser &jp)
virtual int getInt() const
virtual ~JsonValue()
Definition: jsonrpc.h:37
virtual gx_system::JsonSubParser getSubParser() const
JsonValue()
Definition: jsonrpc.h:36
virtual const Glib::ustring & getString() const
virtual double getFloat() const
void send_notify_begin(const char *method)
#define max(x, y)
const char * engine_state_to_string(gx_engine::GxEngineState s)
gx_engine::GxEngineState string_to_engine_state(const std::string &s)
jsonrpc_method m_id
Definition: jsonrpc.h:58
gx_engine::PluginChange::pc status
Definition: jsonrpc.h:124
ChangedPlugin(const std::string &id_, gx_engine::PluginChange::pc status_)
Definition: jsonrpc.h:125
CmdConnection * sender
Definition: jsonrpc.h:117
gx_system::JsonStringWriter * jw
Definition: jsonrpc.h:115
CmdConnection::msg_type n
Definition: jsonrpc.h:116