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 <giomm/init.h> // NOLINT
28 #include <giomm/socketservice.h>
29 //#include <ext/stdio_filebuf.h>
30 #include "jsonrpc_methods.h"
31 
32 class GxService;
33 
34 class JsonValue {
35 protected:
36  JsonValue() {}
37  virtual ~JsonValue() {}
38  friend class JsonArray;
39 public:
40  virtual double getFloat() const;
41  virtual int getInt() const;
42  virtual const Glib::ustring& getString() const;
43  virtual gx_system::JsonSubParser getSubParser() const;
44 };
45 
46 class JsonArray: public std::vector<JsonValue*> {
47 public:
48  JsonArray():std::vector<JsonValue*>() {}
49  ~JsonArray();
50  JsonValue *operator[](unsigned int i);
51  void append(gx_system::JsonParser& jp);
52 };
53 
54 class CmdConnection: public sigc::trackable {
55 public:
56  struct methodnames {
57  const char *name;
59  };
60  enum msg_type {
79  };
80 private:
81  GxService& serv;
82  Glib::RefPtr<Gio::SocketConnection> connection;
83  std::list<std::string> outgoing;
84  unsigned int current_offset;
86  bool midi_config_mode;
87  std::bitset<END_OF_FLAGS> flags;
88  float maxlevel[gx_engine::MaxLevel::channelcount];
89 private:
90  bool find_token(const Glib::ustring& token, msg_type *start, msg_type *end);
91  void activate(int n, bool v) { flags.set(n, v); }
92  void exec(Glib::ustring cmd);
93  void call(gx_system::JsonWriter& jw, const methodnames *mn, JsonArray& params);
94  void notify(gx_system::JsonStringWriter& jw, const methodnames *mn, JsonArray& params);
95  bool request(gx_system::JsonStringParser& jp, gx_system::JsonStringWriter& jw, bool batch_start);
96  void write_error(gx_system::JsonWriter& jw, int code, const char *message);
97  void write_error(gx_system::JsonWriter& jw, int code, Glib::ustring& message) { write_error(jw, code, message.c_str()); }
98  void error_response(gx_system::JsonWriter& jw, int code, const char *message);
99  void error_response(gx_system::JsonWriter& jw, int code, const Glib::ustring& message) { error_response(jw, code, message.c_str()); }
100  void send_notify_begin(gx_system::JsonStringWriter& jw, const char *method) { jw.send_notify_begin(method); }
101  void send_notify_end(gx_system::JsonStringWriter& jw, bool send_out=true);
102  void listen(const Glib::ustring& tp);
103  void unlisten(const Glib::ustring& tp);
104  void process(gx_system::JsonStringParser& jp);
105 
106 public:
107  CmdConnection(GxService& serv, const Glib::RefPtr<Gio::SocketConnection>& connection_);
108  ~CmdConnection();
109  bool on_data_in(Glib::IOCondition cond);
110  bool on_data_out(Glib::IOCondition cond);
112  bool is_activated(msg_type n) { return flags[n]; }
113  void update_maxlevel(unsigned int channel, float v) { maxlevel[channel] = max(maxlevel[channel], v); }
114  friend class UiBuilderVirt;
115 };
116 
117 class GxService: public Gio::SocketService {
118 private:
119  struct ChangedPlugin {
120  std::string id;
122  ChangedPlugin(const std::string& id_, gx_engine::PluginChange::pc status_): id(id_), status(status_) {}
123  };
124 private:
125  gx_preset::GxSettings& settings;
126  gx_jack::GxJack& jack;
127  TunerSwitcher& tuner_switcher;
128  sigc::slot<void> quit_mainloop;
129  time_t oldest_unsaved;
130  time_t last_change;
131  sigc::connection save_conn;
132  std::list<CmdConnection*> connection_list;
134  std::map<std::string,bool> *preg_map;
135  float maxlevel[gx_engine::MaxLevel::channelcount];
136 private:
137  virtual bool on_incoming(const Glib::RefPtr<Gio::SocketConnection>& connection,
138  const Glib::RefPtr<Glib::Object>& source_object);
139  void save_state();
140  void remove_connection(CmdConnection* p);
141  bool broadcast_listeners(CmdConnection::msg_type n, CmdConnection *sender = 0);
142  void broadcast(gx_system::JsonStringWriter& jw, CmdConnection::msg_type n, CmdConnection *sender = 0);
143  void connect_value_changed_signal(gx_engine::Parameter *p);
144 
145  // message formatting functions
146  void serialize_parameter_change(gx_system::JsonWriter& jw);
147  void ladspaloader_write_changes(gx_system::JsonWriter& jw, std::vector<ChangedPlugin>& changed_plugins);
148 
149  // signal handler
150  void on_param_insert_remove(gx_engine::Parameter *p, bool insert);
151  void on_param_value_changed(gx_engine::Parameter *p);
152  void preset_changed();
153  void on_engine_state_change(gx_engine::GxEngineState state);
154  void on_tuner_freq_changed();
155  void display(const Glib::ustring& bank, const Glib::ustring& preset);
156  void set_display_state(TunerSwitcher::SwitcherState newstate);
157  void on_selection_done(bool v);
158  void on_presetlist_changed();
159  void on_log_message(const string& msg, GxLogger::MsgType tp, bool plugged);
160  void on_midi_changed();
161  void on_midi_value_changed(int ctl, int value);
162  void on_osc_size_changed(unsigned int sz);
163  int on_osc_activation(bool v);
164  void on_jack_load_changed();
165  void on_rack_unit_changed(bool stereo);
166  static void add_changed_plugin(gx_engine::Plugin* pl, gx_engine::PluginChange::pc v,
167  std::vector<ChangedPlugin>& vec);
168  void create_bluetooth_sockets(const Glib::ustring& host);
169 
170  friend class CmdConnection;
171 public:
173  TunerSwitcher& tunerswitcher, sigc::slot<void> quit_mainloop_,
174  const Glib::ustring& host, int *port);
175  ~GxService();
176  void send_rack_changed(bool stereo, CmdConnection *cmd);
178  void update_maxlevel(CmdConnection *cmd = 0);
179  float get_maxlevel(unsigned int channel) {
180  assert(channel < gx_engine::MaxLevel::channelcount);
181  float v = maxlevel[channel];
182  maxlevel[channel] = 0;
183  return v;
184  }
185 };
186 
189 
190 #endif // SRC_HEADERS_JSONRPC_H_
CmdConnection::f_state_changed
@ f_state_changed
Definition: jsonrpc.h:62
CmdConnection::f_midi_value_changed
@ f_midi_value_changed
Definition: jsonrpc.h:70
gx_system::JsonStringParser
Definition: gx_json.h:202
CmdConnection::CmdConnection
CmdConnection(GxService &serv, const Glib::RefPtr< Gio::SocketConnection > &connection_)
Definition: jsonrpc.cpp:237
CmdConnection::msg_type
msg_type
Definition: jsonrpc.h:60
string_to_engine_state
gx_engine::GxEngineState string_to_engine_state(const std::string &s)
Definition: jsonrpc.cpp:41
GxService::get_maxlevel
float get_maxlevel(unsigned int channel)
Definition: jsonrpc.h:179
gx_engine::PluginChange::pc
pc
Definition: gx_engine.h:67
CmdConnection::f_preset_changed
@ f_preset_changed
Definition: jsonrpc.h:61
CmdConnection
Definition: jsonrpc.h:54
gx_engine::MaxLevel::channelcount
static const unsigned int channelcount
Definition: gx_internal_plugins.h:51
gx_system::JsonStringWriter
Definition: gx_json.h:99
CmdConnection::methodnames::m_id
jsonrpc_method m_id
Definition: jsonrpc.h:58
gx_system::JsonSubParser
Definition: gx_json.h:194
CmdConnection::f_display
@ f_display
Definition: jsonrpc.h:64
CmdConnection::f_display_state
@ f_display_state
Definition: jsonrpc.h:65
JsonValue::getFloat
virtual double getFloat() const
Definition: jsonrpc.cpp:142
gx_engine::GxEngineState
GxEngineState
Definition: gx_modulesequencer.h:289
CmdConnection::f_presetlist_changed
@ f_presetlist_changed
Definition: jsonrpc.h:67
JsonArray
Definition: jsonrpc.h:46
JsonValue::getSubParser
virtual gx_system::JsonSubParser getSubParser() const
Definition: jsonrpc.cpp:154
CmdConnection::f_osc_size_changed
@ f_osc_size_changed
Definition: jsonrpc.h:72
JsonValue
Definition: jsonrpc.h:34
max
#define max(x, y)
Definition: gx_faust_support.h:5
GxService::update_maxlevel
void update_maxlevel(CmdConnection *cmd=0)
Definition: jsonrpc.cpp:2221
GxService::ladspaloader_update_plugins
void ladspaloader_update_plugins(gx_system::JsonWriter *jw, CmdConnection *cmd)
Definition: jsonrpc.cpp:1839
CmdConnection::methodnames::name
const char * name
Definition: jsonrpc.h:57
UiBuilderVirt
Definition: jsonrpc.cpp:185
GxService::GxService
GxService(gx_preset::GxSettings &settings_, gx_jack::GxJack &jack_, TunerSwitcher &tunerswitcher, sigc::slot< void > quit_mainloop_, const Glib::ustring &host, int *port)
Definition: jsonrpc.cpp:1704
gx_jack::GxJack
Definition: gx_jack.h:111
GxService::send_rack_changed
void send_rack_changed(bool stereo, CmdConnection *cmd)
Definition: jsonrpc.cpp:1865
CmdConnection::f_freq_changed
@ f_freq_changed
Definition: jsonrpc.h:63
CmdConnection::f_parameter_change_notify
@ f_parameter_change_notify
Definition: jsonrpc.h:74
gx_system::JsonWriter
Definition: gx_json.h:55
JsonValue::getInt
virtual int getInt() const
Definition: jsonrpc.cpp:146
CmdConnection::~CmdConnection
~CmdConnection()
Definition: jsonrpc.cpp:248
gx_system::JsonParser
Definition: gx_json.h:112
CmdConnection::on_data_in
bool on_data_in(Glib::IOCondition cond)
Definition: jsonrpc.cpp:1295
CmdConnection::is_activated
bool is_activated(msg_type n)
Definition: jsonrpc.h:112
JsonArray::append
void append(gx_system::JsonParser &jp)
Definition: jsonrpc.cpp:117
CmdConnection::f_jack_load_changed
@ f_jack_load_changed
Definition: jsonrpc.h:73
CmdConnection::f_osc_activation
@ f_osc_activation
Definition: jsonrpc.h:71
JsonValue::~JsonValue
virtual ~JsonValue()
Definition: jsonrpc.h:37
jsonrpc_method
jsonrpc_method
Definition: jsonrpc_methods-generated.h:8
CmdConnection::methodnames
Definition: jsonrpc.h:56
JsonArray::operator[]
JsonValue * operator[](unsigned int i)
Definition: jsonrpc.cpp:110
gx_preset::GxSettings
Definition: gx_preset.h:137
CmdConnection::update_maxlevel
void update_maxlevel(unsigned int channel, float v)
Definition: jsonrpc.h:113
JsonValue::JsonValue
JsonValue()
Definition: jsonrpc.h:36
CmdConnection::f_midi_changed
@ f_midi_changed
Definition: jsonrpc.h:69
GxService
Definition: jsonrpc.h:117
CmdConnection::f_units_changed
@ f_units_changed
Definition: jsonrpc.h:77
JsonArray::JsonArray
JsonArray()
Definition: jsonrpc.h:48
CmdConnection::f_misc_msg
@ f_misc_msg
Definition: jsonrpc.h:76
CmdConnection::f_selection_done
@ f_selection_done
Definition: jsonrpc.h:66
start
CmdConnection::msg_type start
Definition: jsonrpc.cpp:257
engine_state_to_string
const char * engine_state_to_string(gx_engine::GxEngineState s)
Definition: jsonrpc.cpp:31
engine.h
TunerSwitcher::SwitcherState
SwitcherState
Definition: tunerswitcher.h:33
end
CmdConnection::msg_type end
Definition: jsonrpc.cpp:258
gx_system::JsonStringWriter::send_notify_begin
void send_notify_begin(const char *method)
Definition: gx_json.cpp:226
TunerSwitcher
Definition: tunerswitcher.h:29
gx_engine::Plugin
Definition: gx_pluginloader.h:45
CmdConnection::END_OF_FLAGS
@ END_OF_FLAGS
Definition: jsonrpc.h:78
JsonArray::~JsonArray
~JsonArray()
Definition: jsonrpc.cpp:104
CmdConnection::f_plugins_changed
@ f_plugins_changed
Definition: jsonrpc.h:75
JsonValue::getString
virtual const Glib::ustring & getString() const
Definition: jsonrpc.cpp:150
CmdConnection::f_log_message
@ f_log_message
Definition: jsonrpc.h:68
gx_engine::Parameter
Definition: gx_parameter.h:106
CmdConnection::send
void send(gx_system::JsonStringWriter &jw)
Definition: jsonrpc.cpp:1325
GxLogger::MsgType
MsgType
Definition: gx_logging.h:39
GxService::~GxService
~GxService()
Definition: jsonrpc.cpp:1764
token
const char * token
Definition: jsonrpc.cpp:256
CmdConnection::on_data_out
bool on_data_out(Glib::IOCondition cond)
Definition: jsonrpc.cpp:1282