+ DSSI: marshal cairo_iface as well
[calf.git] / src / dssigui.cpp
blob510672160e097ed070d5cadc2ab2a56c4386ab4f
1 /* Calf DSP Library utility application.
2 * DSSI GUI application.
3 * Copyright (C) 2007 Krzysztof Foltman
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 #include <assert.h>
21 #include <getopt.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <config.h>
25 #include <calf/giface.h>
26 #include <calf/gui.h>
27 #include <calf/main_win.h>
28 #include <calf/osctl.h>
29 #include <calf/osctlnet.h>
30 #include <calf/osctlserv.h>
32 using namespace std;
33 using namespace dsp;
34 using namespace calf_plugins;
35 using namespace osctl;
37 #define debug_printf(...)
39 #if 0
40 void osctl_test()
42 string sdata = string("\000\000\000\003123\000test\000\000\000\000\000\000\000\001\000\000\000\002", 24);
43 osc_stream is(sdata);
44 vector<osc_data> data;
45 is.read("bsii", data);
46 assert(is.pos == sdata.length());
47 assert(data.size() == 4);
48 assert(data[0].type == osc_blob);
49 assert(data[1].type == osc_string);
50 assert(data[2].type == osc_i32);
51 assert(data[3].type == osc_i32);
52 assert(data[0].strval == "123");
53 assert(data[1].strval == "test");
54 assert(data[2].i32val == 1);
55 assert(data[3].i32val == 2);
56 osc_stream os("");
57 os.write(data);
58 assert(os.buffer == sdata);
59 osc_server srv;
60 srv.bind("0.0.0.0", 4541);
62 osc_client cli;
63 cli.bind("0.0.0.0", 0);
64 cli.set_addr("0.0.0.0", 4541);
65 if (!cli.send("/blah", data))
66 g_error("Could not send the OSC message");
68 g_main_loop_run(g_main_loop_new(NULL, FALSE));
70 #endif
72 struct cairo_params
74 enum { HAS_COLOR = 1, HAS_WIDTH = 2 };
75 uint32_t flags;
76 float r, g, b, a;
77 float line_width;
79 cairo_params()
80 : flags(0)
81 , r(0.f)
82 , g(0.f)
83 , b(0.f)
84 , a(1.f)
85 , line_width(1)
90 struct graph_item: public cairo_params
92 float data[128];
95 struct dot_item: public cairo_params
97 float x, y;
98 int32_t size;
101 struct gridline_item: public cairo_params
103 float pos;
104 int32_t vertical;
105 std::string text;
108 struct param_line_graphs
110 vector<graph_item *> graphs;
111 vector<dot_item *> dots;
112 vector<gridline_item *> gridlines;
114 void clear();
117 void param_line_graphs::clear()
119 for (size_t i = 0; i < graphs.size(); i++)
120 delete graphs[i];
121 graphs.clear();
123 for (size_t i = 0; i < dots.size(); i++)
124 delete dots[i];
125 dots.clear();
127 for (size_t i = 0; i < gridlines.size(); i++)
128 delete gridlines[i];
129 gridlines.clear();
133 struct plugin_proxy: public plugin_ctl_iface, public plugin_metadata_proxy, public line_graph_iface
135 osc_client *client;
136 bool send_osc;
137 plugin_gui *gui;
138 map<string, string> cfg_vars;
139 int param_count;
140 float *params;
141 map<int, param_line_graphs> graphs;
143 plugin_proxy(plugin_metadata_iface *md)
144 : plugin_metadata_proxy(md)
146 client = NULL;
147 send_osc = false;
148 gui = NULL;
149 param_count = md->get_param_count();
150 params = new float[param_count];
151 for (int i = 0; i < param_count; i++)
152 params[i] = get_param_props(i)->def_value;
154 virtual float get_param_value(int param_no) {
155 if (param_no < 0 || param_no >= param_count)
156 return 0;
157 return params[param_no];
159 virtual void set_param_value(int param_no, float value) {
160 if (param_no < 0 || param_no >= param_count)
161 return;
162 params[param_no] = value;
163 if (send_osc)
165 osc_inline_typed_strstream str;
166 str << (uint32_t)(param_no + get_param_port_offset()) << value;
167 client->send("/control", str);
170 virtual bool activate_preset(int bank, int program) {
171 if (send_osc) {
172 osc_inline_typed_strstream str;
173 str << (uint32_t)(bank) << (uint32_t)(program);
174 client->send("/program", str);
175 return false;
177 return false;
179 virtual float get_level(unsigned int port) { return 0.f; }
180 virtual void execute(int command_no) {
181 if (send_osc) {
182 stringstream ss;
183 ss << command_no;
185 osc_inline_typed_strstream str;
186 str << "ExecCommand" << ss.str();
187 client->send("/configure", str);
189 str.clear();
190 str << "ExecCommand" << "";
191 client->send("/configure", str);
194 char *configure(const char *key, const char *value) {
195 cfg_vars[key] = value;
196 osc_inline_typed_strstream str;
197 str << key << value;
198 client->send("/configure", str);
199 return NULL;
201 void send_configures(send_configure_iface *sci) {
202 for (map<string, string>::iterator i = cfg_vars.begin(); i != cfg_vars.end(); i++)
203 sci->send_configure(i->first.c_str(), i->second.c_str());
205 virtual line_graph_iface *get_line_graph_iface() { return this; }
206 virtual bool get_graph(int index, int subindex, float *data, int points, cairo_iface *context);
207 virtual bool get_dot(int index, int subindex, float &x, float &y, int &size, cairo_iface *context);
208 virtual bool get_gridline(int index, int subindex, float &pos, bool &vertical, std::string &legend, cairo_iface *context);
209 void update_cairo_context(cairo_iface *context, cairo_params &item);
212 bool plugin_proxy::get_graph(int index, int subindex, float *data, int points, cairo_iface *context)
214 if (!graphs.count(index))
215 return false;
216 param_line_graphs &g = graphs[index];
217 if (subindex < (int)g.graphs.size())
219 float *sdata = g.graphs[subindex]->data;
220 for (int i = 0; i < points; i++) {
221 float pos = i * 127.0 / points;
222 int ipos = i * 127 / points;
223 data[i] = sdata[ipos] + (sdata[ipos + 1] - sdata[ipos]) * (pos-ipos);
225 update_cairo_context(context, *g.graphs[subindex]);
226 return true;
228 return false;
231 bool plugin_proxy::get_dot(int index, int subindex, float &x, float &y, int &size, cairo_iface *context)
233 if (!graphs.count(index))
234 return false;
235 param_line_graphs &g = graphs[index];
236 if (subindex < (int)g.dots.size())
238 dot_item &item = *g.dots[subindex];
239 x = item.x;
240 y = item.y;
241 size = item.size;
242 update_cairo_context(context, item);
243 return true;
245 return false;
248 bool plugin_proxy::get_gridline(int index, int subindex, float &pos, bool &vertical, std::string &legend, cairo_iface *context)
250 if (!graphs.count(index))
251 return false;
252 param_line_graphs &g = graphs[index];
253 if (subindex < (int)g.gridlines.size())
255 gridline_item &item = *g.gridlines[subindex];
256 pos = item.pos;
257 vertical = item.vertical != 0;
258 legend = item.text;
259 update_cairo_context(context, item);
260 return true;
262 return false;
265 void plugin_proxy::update_cairo_context(cairo_iface *context, cairo_params &item)
267 if (item.flags & cairo_params::HAS_COLOR)
268 context->set_source_rgba(item.r, item.g, item.b, item.a);
269 if (item.flags & cairo_params::HAS_WIDTH)
270 context->set_line_width(item.line_width);
273 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
275 GMainLoop *mainloop;
277 static bool osc_debug = false;
279 struct dssi_osc_server: public osc_server, public osc_message_sink<osc_strstream>
281 plugin_proxy *plugin;
282 main_window *main_win;
283 plugin_gui_window *window;
284 string effect_name, title;
285 osc_client cli;
286 bool in_program, enable_dump;
287 vector<plugin_preset> presets;
288 /// Timeout callback source ID
289 int source_id;
291 dssi_osc_server()
292 : plugin(NULL)
293 , main_win(new main_window)
294 , window(new plugin_gui_window(main_win))
296 sink = this;
297 source_id = 0;
300 static void on_destroy(GtkWindow *window, dssi_osc_server *self)
302 debug_printf("on_destroy, have to send \"exiting\"\n");
303 bool result = self->cli.send("/exiting");
304 debug_printf("result = %d\n", result ? 1 : 0);
305 g_main_loop_quit(mainloop);
306 // eliminate a warning with empty debug_printf
307 result = false;
310 void create_window()
312 plugin = NULL;
313 vector<plugin_metadata_iface *> plugins;
314 get_all_plugins(plugins);
315 for (unsigned int i = 0; i < plugins.size(); i++)
317 if (!strcmp(plugins[i]->get_id(), effect_name.c_str()))
319 plugin = new plugin_proxy(plugins[i]);
320 break;
323 if (!plugin)
325 fprintf(stderr, "Unknown plugin: %s\n", effect_name.c_str());
326 exit(1);
328 plugin->client = &cli;
329 plugin->send_osc = true;
330 ((main_window *)window->main)->conditions.insert("dssi");
331 ((main_window *)window->main)->conditions.insert("directlink");
332 window->create(plugin, title.c_str(), effect_name.c_str());
333 plugin->gui = window->gui;
334 gtk_signal_connect(GTK_OBJECT(window->toplevel), "destroy", G_CALLBACK(on_destroy), this);
335 vector<plugin_preset> tmp_presets;
336 get_builtin_presets().get_for_plugin(presets, effect_name.c_str());
337 get_user_presets().get_for_plugin(tmp_presets, effect_name.c_str());
338 presets.insert(presets.end(), tmp_presets.begin(), tmp_presets.end());
339 source_id = g_timeout_add_full(G_PRIORITY_LOW, 1000/30, on_idle, this, NULL);
342 static gboolean on_idle(void *self)
344 ((dssi_osc_server *)(self))->send_osc_update();
345 return TRUE;
348 void set_osc_update(bool enabled)
350 osc_inline_typed_strstream data;
351 data << "OSC:FEEDBACK_URI";
352 data << (enabled ? get_uri() : "");
353 cli.send("/configure", data);
355 void send_osc_update()
357 static int serial_no = 0;
358 osc_inline_typed_strstream data;
359 data << "OSC:UPDATE";
360 data << calf_utils::i2s(serial_no++);
361 cli.send("/configure", data);
364 virtual void receive_osc_message(std::string address, std::string args, osc_strstream &buffer);
365 void unmarshal_line_graph(osc_strstream &buffer);
368 void dssi_osc_server::unmarshal_line_graph(osc_strstream &buffer)
370 uint32_t cmd;
372 do {
373 buffer >> cmd;
374 if (cmd == LGI_GRAPH)
376 uint32_t param;
377 buffer >> param;
378 param_line_graphs &graphs = plugin->graphs[param];
380 graphs.clear();
381 cairo_params params;
383 do {
384 buffer >> cmd;
385 if (cmd == LGI_SET_RGBA)
387 params.flags |= cairo_params::HAS_COLOR;
388 buffer >> params.r >> params.g >> params.b >> params.a;
390 else
391 if (cmd == LGI_SET_WIDTH)
393 params.flags |= cairo_params::HAS_WIDTH;
394 buffer >> params.line_width;
396 else
397 if (cmd == LGI_SUBGRAPH)
399 buffer >> param; // ignore number of points
400 graph_item *gi = new graph_item;
401 (cairo_params &)*gi = params;
402 for (int i = 0; i < 128; i++)
403 buffer >> gi->data[i];
404 graphs.graphs.push_back(gi);
405 params.flags = 0;
407 else
408 if (cmd == LGI_DOT)
410 dot_item *di = new dot_item;
411 (cairo_params &)*di = params;
412 buffer >> di->x >> di->y >> di->size;
413 graphs.dots.push_back(di);
414 params.flags = 0;
416 else
417 if (cmd == LGI_LEGEND)
419 gridline_item *li = new gridline_item;
420 (cairo_params &)*li = params;
421 buffer >> li->pos >> li->vertical >> li->text;
422 graphs.gridlines.push_back(li);
423 params.flags = 0;
425 else
426 break;
427 } while(1);
430 else
431 break;
432 } while(1);
435 void dssi_osc_server::receive_osc_message(std::string address, std::string args, osc_strstream &buffer)
437 if (osc_debug)
438 dump.receive_osc_message(address, args, buffer);
439 if (address == prefix + "/update" && args == "s")
441 string str;
442 buffer >> str;
443 debug_printf("UPDATE: %s\n", str.c_str());
444 return;
446 else if (address == prefix + "/quit")
448 set_osc_update(false);
449 debug_printf("QUIT\n");
450 g_main_loop_quit(mainloop);
451 return;
453 else if (address == prefix + "/configure"&& args == "ss")
455 string key, value;
456 buffer >> key >> value;
457 plugin->cfg_vars[key] = value;
458 // XXXKF perhaps this should be queued !
459 window->gui->refresh();
460 return;
462 else if (address == prefix + "/program"&& args == "ii")
464 uint32_t bank, program;
466 buffer >> bank >> program;
468 unsigned int nr = bank * 128 + program;
469 debug_printf("PROGRAM %d\n", nr);
470 if (nr == 0)
472 bool sosc = plugin->send_osc;
473 plugin->send_osc = false;
474 int count = plugin->get_param_count();
475 for (int i =0 ; i < count; i++)
476 plugin->set_param_value(i, plugin->get_param_props(i)->def_value);
477 plugin->send_osc = sosc;
478 window->gui->refresh();
479 // special handling for default preset
480 return;
482 nr--;
483 if (nr >= presets.size())
484 return;
485 bool sosc = plugin->send_osc;
486 plugin->send_osc = false;
487 presets[nr].activate(plugin);
488 plugin->send_osc = sosc;
489 window->gui->refresh();
491 // cli.send("/update", data);
492 return;
494 else if (address == prefix + "/control" && args == "if")
496 uint32_t port;
497 float val;
499 buffer >> port >> val;
501 int idx = port - plugin->get_param_port_offset();
502 debug_printf("CONTROL %d %f\n", idx, val);
503 bool sosc = plugin->send_osc;
504 plugin->send_osc = false;
505 window->gui->set_param_value(idx, val);
506 plugin->send_osc = sosc;
507 set_osc_update(false);
508 set_osc_update(true);
509 return;
511 else if (address == prefix + "/show")
513 set_osc_update(true);
515 gtk_widget_show_all(GTK_WIDGET(window->toplevel));
516 return;
518 else if (address == prefix + "/hide")
520 set_osc_update(false);
522 gtk_widget_hide(GTK_WIDGET(window->toplevel));
523 return;
525 else if (address == prefix + "/lineGraph")
527 unmarshal_line_graph(buffer);
528 return;
530 else
531 printf("Unknown OSC address: %s\n", address.c_str());
534 //////////////////////////////////////////////////////////////////////////////////
536 void help(char *argv[])
538 printf("GTK+ user interface for Calf DSSI plugins\nSyntax: %s [--help] [--version] <osc-url> <so-file> <plugin-label> <instance-name>\n", argv[0]);
541 static struct option long_options[] = {
542 {"help", 0, 0, 'h'},
543 {"version", 0, 0, 'v'},
544 {"debug", 0, 0, 'd'},
545 {0,0,0,0},
548 int main(int argc, char *argv[])
550 char *debug_var = getenv("OSC_DEBUG");
551 if (debug_var && atoi(debug_var))
552 osc_debug = true;
554 gtk_init(&argc, &argv);
555 while(1) {
556 int option_index;
557 int c = getopt_long(argc, argv, "dhv", long_options, &option_index);
558 if (c == -1)
559 break;
560 switch(c) {
561 case 'd':
562 osc_debug = true;
563 break;
564 case 'h':
565 help(argv);
566 return 0;
567 case 'v':
568 printf("%s\n", PACKAGE_STRING);
569 return 0;
572 if (argc - optind != 4)
574 help(argv);
575 exit(0);
578 try {
579 get_builtin_presets().load_defaults(true);
580 get_user_presets().load_defaults(false);
582 catch(calf_plugins::preset_exception &e)
584 fprintf(stderr, "Error while loading presets: %s\n", e.what());
585 exit(1);
588 dssi_osc_server srv;
589 srv.prefix = "/dssi/"+string(argv[optind + 1]) + "/" + string(argv[optind + 2]);
590 for (char *p = argv[optind + 2]; *p; p++)
591 *p = tolower(*p);
592 srv.effect_name = argv[optind + 2];
593 srv.title = argv[optind + 3];
595 srv.bind();
596 srv.create_window();
598 mainloop = g_main_loop_new(NULL, FALSE);
600 srv.cli.bind();
601 srv.cli.set_url(argv[optind]);
603 debug_printf("URI = %s\n", srv.get_uri().c_str());
605 string data_buf, type_buf;
606 osc_inline_typed_strstream data;
607 data << srv.get_uri();
608 if (!srv.cli.send("/update", data))
610 g_error("Could not send the initial update message via OSC to %s", argv[optind]);
611 return 1;
614 g_main_loop_run(mainloop);
615 if (srv.source_id)
616 g_source_remove(srv.source_id);
618 srv.set_osc_update(false);
619 debug_printf("exited\n");
621 return 0;