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 {
78  END_OF_FLAGS
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);
111  void send(gx_system::JsonStringWriter& jw);
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);
177  void ladspaloader_update_plugins(gx_system::JsonWriter *jw, 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::msg_type end
Definition: jsonrpc.cpp:258
jsonrpc_method m_id
Definition: jsonrpc.h:58
CmdConnection::msg_type start
Definition: jsonrpc.cpp:257
bool is_activated(msg_type n)
Definition: jsonrpc.h:112
gx_engine::GxEngineState string_to_engine_state(const std::string &s)
Definition: jsonrpc.cpp:41
static const unsigned int channelcount
float get_maxlevel(unsigned int channel)
Definition: jsonrpc.h:179
STL namespace.
virtual double getFloat() const
Definition: jsonrpc.cpp:142
virtual gx_system::JsonSubParser getSubParser() const
Definition: jsonrpc.cpp:154
#define max(x, y)
virtual int getInt() const
Definition: jsonrpc.cpp:146
void send_notify_begin(const char *method)
Definition: gx_json.cpp:226
virtual ~JsonValue()
Definition: jsonrpc.h:37
JsonValue()
Definition: jsonrpc.h:36
const char * engine_state_to_string(gx_engine::GxEngineState s)
Definition: jsonrpc.cpp:31
const char * token
Definition: jsonrpc.cpp:256
JsonArray()
Definition: jsonrpc.h:48
void update_maxlevel(unsigned int channel, float v)
Definition: jsonrpc.h:113
virtual const Glib::ustring & getString() const
Definition: jsonrpc.cpp:150