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 #ifndef GUITARIX_AS_PLUGIN
31 #include "jsonrpc_methods.h"
32 #else
33 #include "jsonrpc_methods-generated.h"
34 #endif
35 
36 class GxService;
37 
38 class JsonValue {
39 protected:
40  JsonValue() {}
41  virtual ~JsonValue() {}
42  friend class JsonArray;
43 public:
44  virtual double getFloat() const;
45  virtual int getInt() const;
46  virtual const Glib::ustring& getString() const;
48 };
49 
50 class JsonArray: public std::vector<JsonValue*> {
51 public:
52  JsonArray():std::vector<JsonValue*>() {}
54  JsonValue *operator[](unsigned int i);
56 };
57 
58 class CmdConnection: public sigc::trackable {
59 public:
60  struct methodnames {
61  const char *name;
62  jsonrpc_method m_id;
63  };
64  enum msg_type {
80  };
81 private:
83  Glib::RefPtr<Gio::SocketConnection> connection;
84  std::list<std::string> outgoing;
85  unsigned int current_offset;
88  std::bitset<END_OF_FLAGS> flags;
89  std::map<string,float> maxlevel;
90 private:
91  bool find_token(const Glib::ustring& token, msg_type *start, msg_type *end);
92  void activate(int n, bool v) { flags.set(n, v); }
93  void exec(Glib::ustring cmd);
94  void call(gx_system::JsonWriter& jw, const methodnames *mn, JsonArray& params);
97  void write_error(gx_system::JsonWriter& jw, int code, const char *message);
98  void write_error(gx_system::JsonWriter& jw, int code, Glib::ustring& message) { write_error(jw, code, message.c_str()); }
99  void error_response(gx_system::JsonWriter& jw, int code, const char *message);
100  void error_response(gx_system::JsonWriter& jw, int code, const Glib::ustring& message) { error_response(jw, code, message.c_str()); }
101  void send_notify_begin(gx_system::JsonStringWriter& jw, const char *method) { jw.send_notify_begin(method); }
102  void send_notify_end(gx_system::JsonStringWriter& jw, bool send_out=true);
103  void listen(const Glib::ustring& tp);
104  void unlisten(const Glib::ustring& tp);
106 
107 public:
108  CmdConnection(GxService& serv, const Glib::RefPtr<Gio::SocketConnection>& connection_);
110  bool on_data_in(Glib::IOCondition cond);
111  bool on_data_out(Glib::IOCondition cond);
113  bool is_activated(msg_type n) { return flags[n]; }
114  void update_maxlevel(const std::string& id, float v) { float& m = maxlevel[id]; m = max(m, v); }
115  friend class UiBuilderVirt;
116 };
117 
122 };
123 
124 class GxService: public Gio::SocketService {
125 private:
126  struct ChangedPlugin {
127  std::string id;
129  ChangedPlugin(const std::string& id_, gx_engine::PluginChange::pc status_): id(id_), status(status_) {}
130  };
131 private:
135  sigc::slot<void> quit_mainloop;
137  time_t last_change;
138  sigc::connection save_conn;
139  std::list<CmdConnection*> connection_list;
140  std::queue<broadcast_data> broadcast_list;
142  std::map<std::string,bool> *preg_map;
143  std::map<std::string,float> maxlevel;
144 private:
145  virtual bool on_incoming(const Glib::RefPtr<Gio::SocketConnection>& connection,
146  const Glib::RefPtr<Glib::Object>& source_object);
147  void save_state();
153 
154  // message formatting functions
156  void ladspaloader_write_changes(gx_system::JsonWriter& jw, std::vector<ChangedPlugin>& changed_plugins);
157 
158  // signal handler
164  void display(const Glib::ustring& bank, const Glib::ustring& preset);
166  void on_selection_done(bool v);
168  void on_log_message(const string& msg, GxLogger::MsgType tp, bool plugged);
170  void on_midi_value_changed(int ctl, int value);
171  void on_rack_unit_changed(bool stereo);
173  std::vector<ChangedPlugin>& vec);
174  void create_bluetooth_sockets(const Glib::ustring& host);
175 
176  friend class CmdConnection;
177 public:
179  TunerSwitcher& tunerswitcher, sigc::slot<void> quit_mainloop_,
180  const Glib::ustring& host, int *port);
182  void send_rack_changed(bool stereo, CmdConnection *cmd);
184  float update_maxlevel(const std::string& id, bool reset=false);
185 };
186 
189 
190 #endif // SRC_HEADERS_JSONRPC_H_
unsigned int current_offset
Definition: jsonrpc.h:85
void call(gx_system::JsonWriter &jw, const methodnames *mn, JsonArray &params)
gx_system::JsonStringParser jp
Definition: jsonrpc.h:86
bool on_data_in(Glib::IOCondition cond)
bool is_activated(msg_type n)
Definition: jsonrpc.h:113
Glib::RefPtr< Gio::SocketConnection > connection
Definition: jsonrpc.h:83
void send_notify_begin(gx_system::JsonStringWriter &jw, const char *method)
Definition: jsonrpc.h:101
void activate(int n, bool v)
Definition: jsonrpc.h:92
friend class UiBuilderVirt
Definition: jsonrpc.h:115
GxService & serv
Definition: jsonrpc.h:82
void write_error(gx_system::JsonWriter &jw, int code, const char *message)
void update_maxlevel(const std::string &id, float v)
Definition: jsonrpc.h:114
void error_response(gx_system::JsonWriter &jw, int code, const Glib::ustring &message)
Definition: jsonrpc.h:100
void exec(Glib::ustring cmd)
void notify(gx_system::JsonStringWriter &jw, const methodnames *mn, JsonArray &params)
std::list< std::string > outgoing
Definition: jsonrpc.h:84
void write_error(gx_system::JsonWriter &jw, int code, Glib::ustring &message)
Definition: jsonrpc.h:98
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:88
bool midi_config_mode
Definition: jsonrpc.h:87
void send(gx_system::JsonStringWriter &jw)
@ f_freq_changed
Definition: jsonrpc.h:67
@ f_display_state
Definition: jsonrpc.h:69
@ f_midi_value_changed
Definition: jsonrpc.h:74
@ f_plugins_changed
Definition: jsonrpc.h:76
@ f_units_changed
Definition: jsonrpc.h:78
@ f_presetlist_changed
Definition: jsonrpc.h:71
@ f_parameter_change_notify
Definition: jsonrpc.h:75
@ f_midi_changed
Definition: jsonrpc.h:73
@ f_log_message
Definition: jsonrpc.h:72
@ f_state_changed
Definition: jsonrpc.h:66
@ f_selection_done
Definition: jsonrpc.h:70
@ f_preset_changed
Definition: jsonrpc.h:65
@ END_OF_FLAGS
Definition: jsonrpc.h:79
std::map< string, float > maxlevel
Definition: jsonrpc.h:89
void process(gx_system::JsonStringParser &jp)
sigc::connection save_conn
Definition: jsonrpc.h:138
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:143
std::map< std::string, bool > * preg_map
Definition: jsonrpc.h:142
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:141
std::queue< broadcast_data > broadcast_list
Definition: jsonrpc.h:140
void on_tuner_freq_changed()
time_t last_change
Definition: jsonrpc.h:137
gx_preset::GxSettings & settings
Definition: jsonrpc.h:132
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:136
TunerSwitcher & tuner_switcher
Definition: jsonrpc.h:134
void on_midi_changed()
std::list< CmdConnection * > connection_list
Definition: jsonrpc.h:139
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:133
sigc::slot< void > quit_mainloop
Definition: jsonrpc.h:135
JsonArray()
Definition: jsonrpc.h:52
JsonValue * operator[](unsigned int i)
void append(gx_system::JsonParser &jp)
virtual const Glib::ustring & getString() const
virtual int getInt() const
virtual ~JsonValue()
Definition: jsonrpc.h:41
virtual gx_system::JsonSubParser getSubParser() const
JsonValue()
Definition: jsonrpc.h:40
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:62
gx_engine::PluginChange::pc status
Definition: jsonrpc.h:128
ChangedPlugin(const std::string &id_, gx_engine::PluginChange::pc status_)
Definition: jsonrpc.h:129
CmdConnection * sender
Definition: jsonrpc.h:121
gx_system::JsonStringWriter * jw
Definition: jsonrpc.h:119
CmdConnection::msg_type n
Definition: jsonrpc.h:120