Disable compilation of currently unused code
[ladish.git] / gui / Patchage.cpp
blob5fd8e30bd1c1d2527bf84f7327a57da2f2195ecb
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
8 **************************************************************************
9 * This file contains implementation of the application class
10 **************************************************************************
12 * LADI Session Handler is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * LADI Session Handler is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
24 * or write to the Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "common.h"
30 #include <string.h>
31 #include <cmath>
32 #include <sstream>
33 #include <fstream>
34 #include <libgnomecanvasmm.h>
35 #include <libglademm/xml.h>
36 #include <gtk/gtkwindow.h>
37 #include <boost/format.hpp>
39 #include "Patchage.hpp"
40 #include "lash_proxy.hpp"
41 #include "project_list.hpp"
42 #include "session.hpp"
43 #include "globals.hpp"
44 #include "a2j_proxy.hpp"
45 #include "dbus_helpers.h"
46 #include "load_projects_dialog.hpp"
47 #include "graph_canvas.h"
48 #include "../jack_proxy.h"
50 Patchage * g_app;
52 #define LOG_TO_STD
53 //#define LOG_TO_STATUS
55 graph_canvas_handle g_jack_graph_canvas;
56 graph_handle g_jack_graph;
58 /* Gtk helpers (resize combo boxes) */
60 static void
61 gtkmm_get_ink_pixel_size (Glib::RefPtr<Pango::Layout> layout,
62 int& width,
63 int& height)
65 Pango::Rectangle ink_rect = layout->get_ink_extents ();
67 width = (ink_rect.get_width() + PANGO_SCALE / 2) / PANGO_SCALE;
68 height = (ink_rect.get_height() + PANGO_SCALE / 2) / PANGO_SCALE;
71 static void
72 gtkmm_set_width_for_given_text (Gtk::Widget &w, const gchar *text,
73 gint hpadding/*, gint vpadding*/)
76 int old_width, old_height;
77 w.get_size_request(old_width, old_height);
79 int width, height;
80 w.ensure_style ();
82 gtkmm_get_ink_pixel_size (w.create_pango_layout (text), width, height);
83 w.set_size_request(width + hpadding, old_height);//height + vpadding);
86 /* end Gtk helpers */
89 #define INIT_WIDGET(x) x(g_xml, ((const char*)#x) + 1)
91 Patchage::Patchage(int argc, char** argv)
92 : _max_dsp_load(0.0)
93 , INIT_WIDGET(_about_win)
94 , INIT_WIDGET(_buffer_size_combo)
95 , INIT_WIDGET(_clear_load_but)
96 , INIT_WIDGET(_main_scrolledwin)
97 , INIT_WIDGET(_main_win)
98 , INIT_WIDGET(_main_xrun_progress)
99 , INIT_WIDGET(_main_a2j_status_label)
100 , INIT_WIDGET(_menu_file_quit)
101 , INIT_WIDGET(_menu_help_about)
102 , INIT_WIDGET(_menu_jack_start)
103 , INIT_WIDGET(_menu_jack_stop)
104 , INIT_WIDGET(_menu_a2j_start)
105 , INIT_WIDGET(_menu_a2j_stop)
106 , INIT_WIDGET(_menu_load_project)
107 , INIT_WIDGET(_menu_save_all_projects)
108 , INIT_WIDGET(_menu_close_all_projects)
109 , INIT_WIDGET(_menu_store_positions)
110 , INIT_WIDGET(_menu_view_arrange)
111 , INIT_WIDGET(_menu_view_projects)
112 , INIT_WIDGET(_menu_view_refresh)
113 , INIT_WIDGET(_menu_view_toolbar)
114 , INIT_WIDGET(_project_list_viewport)
115 , INIT_WIDGET(_sample_rate_label)
116 , INIT_WIDGET(_toolbar)
117 , INIT_WIDGET(_zoom_full_but)
118 , INIT_WIDGET(_zoom_normal_but)
120 g_app = this;
122 while (argc > 0) {
123 if (!strcmp(*argv, "--help")) {
124 std::cout << "Usage: patchage [OPTIONS]\nOptions: --no-alsa" << std::endl;
125 exit(0);
128 argv++;
129 argc--;
132 patchage_dbus_init();
134 graph_create(JACKDBUS_SERVICE, JACKDBUS_OBJECT, &g_jack_graph);
135 graph_canvas_create(1600 * 2, 1200 * 2, &g_jack_graph_canvas);
136 graph_canvas_attach(g_jack_graph_canvas, g_jack_graph);
137 graph_activate(g_jack_graph);
139 Glib::set_application_name("Patchage");
140 _about_win->property_program_name() = "Patchage";
141 _about_win->property_logo_icon_name() = "gladish";
142 gtk_window_set_default_icon_name("gladish");
144 gtkmm_set_width_for_given_text(*_buffer_size_combo, "4096 frames", 40);
146 _main_scrolledwin->add(*Glib::wrap(canvas_get_widget(graph_canvas_get_canvas(g_jack_graph_canvas))));
148 // _canvas->scroll_to(static_cast<int>(_canvas->width()/2 - 320),
149 // static_cast<int>(_canvas->height()/2 - 240)); // FIXME: hardcoded
151 _main_scrolledwin->property_hadjustment().get_value()->set_step_increment(10);
152 _main_scrolledwin->property_vadjustment().get_value()->set_step_increment(10);
154 _main_scrolledwin->signal_scroll_event().connect(
155 sigc::mem_fun(this, &Patchage::on_scroll));
157 _buffer_size_combo->signal_changed().connect(
158 sigc::mem_fun(this, &Patchage::buffer_size_changed));
159 _clear_load_but->signal_clicked().connect(
160 sigc::mem_fun(this, &Patchage::clear_load));
161 _zoom_normal_but->signal_clicked().connect(sigc::bind(
162 sigc::mem_fun(this, &Patchage::zoom), 1.0));
163 // _zoom_full_but->signal_clicked().connect(sigc::mem_fun(_canvas.get(), &PatchageCanvas::zoom_full));
165 // _menu_load_project->signal_activate().connect(sigc::mem_fun(this, &Patchage::load_project_ask));
166 // _menu_save_all_projects->signal_activate().connect(sigc::mem_fun(this, &Patchage::save_all_projects));
167 // _menu_close_all_projects->signal_activate().connect(sigc::mem_fun(this, &Patchage::close_all_projects));
169 _menu_file_quit->signal_activate().connect(
170 sigc::mem_fun(this, &Patchage::on_quit));
171 _menu_view_refresh->signal_activate().connect(
172 sigc::mem_fun(this, &Patchage::refresh));
173 _menu_view_arrange->signal_activate().connect(
174 sigc::mem_fun(this, &Patchage::on_arrange));
175 _menu_view_toolbar->signal_activate().connect(
176 sigc::mem_fun(this, &Patchage::on_view_toolbar));
177 _menu_view_projects->signal_toggled().connect(
178 sigc::mem_fun(this, &Patchage::on_show_projects));
179 _menu_help_about->signal_activate().connect(
180 sigc::mem_fun(this, &Patchage::on_help_about));
182 Glib::wrap(canvas_get_widget(graph_canvas_get_canvas(g_jack_graph_canvas)))->show();
183 _main_win->present();
185 _about_win->set_transient_for(*_main_win);
187 // _a2j = new a2j_proxy;
189 //info_msg(str(boost::format("a2j jack client name is '%s'") % _a2j->get_jack_client_name()));
191 // _session = new session();
193 // _project_list = new project_list(this, _session);
194 // _project_list_viewport->hide();
196 // _lash = new lash_proxy(_session);
198 //_menu_jack_start->signal_activate().connect(sigc::mem_fun(_jack, &jack_proxy::start_server));
199 //_menu_jack_stop->signal_activate().connect(sigc::mem_fun(_jack, &jack_proxy::stop_server));
201 // _menu_a2j_start->signal_activate().connect(sigc::mem_fun(_a2j, &a2j_proxy::start_bridge));
202 // _menu_a2j_stop->signal_activate().connect(sigc::mem_fun(_a2j, &a2j_proxy::stop_bridge));
204 //jack_status_changed(_jack->is_started());
206 connect_widgets();
207 update_state();
209 // _canvas->grab_focus();
211 // Idle callback, check if we need to refresh
212 Glib::signal_timeout().connect(
213 sigc::mem_fun(this, &Patchage::idle_callback), 100);
216 Patchage::~Patchage()
218 //delete _lash;
219 //delete _project_list;
220 //delete _session;
221 //delete _a2j;
223 _about_win.destroy();
224 //_main_win.destroy();
226 patchage_dbus_uninit();
229 bool
230 Patchage::idle_callback()
232 update_load();
234 return true;
237 void
238 Patchage::update_toolbar()
240 #if 0
241 bool started;
243 started = _jack->is_started();
245 _buffer_size_combo->set_sensitive(started);
247 if (started)
249 _buffer_size_combo->set_active((int)log2f(_jack->buffer_size()) - 5);
251 #endif
254 void
255 Patchage::update_load()
257 #if 0
258 if (!_jack->is_started())
260 _main_xrun_progress->set_text("JACK stopped");
261 return;
264 char tmp_buf[8];
265 snprintf(tmp_buf, 8, "%zd", _jack->xruns());
267 _main_xrun_progress->set_text(std::string(tmp_buf) + " Dropouts");
269 float load = _jack->get_dsp_load();
271 load /= 100.0; // dbus returns it in percents, we use 0..1
273 if (load > _max_dsp_load)
275 _max_dsp_load = load;
276 _main_xrun_progress->set_fraction(load);
278 #endif
282 void
283 Patchage::zoom(double z)
285 // _canvas->set_zoom(z);
289 void
290 Patchage::refresh()
292 // assert(_canvas);
294 // _canvas->destroy();
296 // if (_jack)
297 // _jack->refresh();
299 // for (ItemList::iterator i = _canvas->items().begin(); i != _canvas->items().end(); ++i) {
300 // (*i)->resize();
301 // }
304 void
305 Patchage::clear_load()
307 #if 0
308 _main_xrun_progress->set_fraction(0.0);
309 _jack->reset_xruns();
310 _max_dsp_load = 0.0;
311 #endif
315 void
316 Patchage::error_msg(const std::string& msg)
318 #if defined(LOG_TO_STATUS)
319 status_msg(msg);
320 #endif
321 #if defined(LOG_TO_STD)
322 std::cerr << msg << std::endl;
323 #endif
327 void
328 Patchage::info_msg(const std::string& msg)
330 #if defined(LOG_TO_STATUS)
331 status_msg(msg);
332 #endif
333 #if defined(LOG_TO_STD)
334 std::cerr << msg << std::endl;
335 #endif
339 void
340 Patchage::status_msg(const std::string& msg)
345 void
346 Patchage::update_state()
348 // for (ItemList::iterator i = _canvas->items().begin(); i != _canvas->items().end(); ++i) {
349 // shared_ptr<Module> module = dynamic_pointer_cast<Module>(*i);
350 // if (module)
351 // module->load_location();
352 // }
356 /** Update the sensitivity status of menus to reflect the present.
358 * (eg. disable "Connect to Jack" when Patchage is already connected to Jack)
360 void
361 Patchage::connect_widgets()
363 //_jack->signal_started.connect(sigc::bind(sigc::mem_fun(this, &Patchage::jack_status_changed), true));
364 //_jack->signal_stopped.connect(sigc::bind(sigc::mem_fun(this, &Patchage::jack_status_changed), false));
367 void
368 Patchage::jack_status_changed(
369 bool started)
371 update_toolbar();
373 _menu_jack_start->set_sensitive(!started);
374 _menu_jack_stop->set_sensitive(started);
375 _clear_load_but->set_sensitive(started);
376 if (!started)
378 _main_xrun_progress->set_fraction(0.0);
382 void
383 Patchage::on_arrange()
385 // assert(_canvas);
387 // _canvas->arrange();
391 void
392 Patchage::on_help_about()
394 _about_win->run();
395 _about_win->hide();
399 void
400 Patchage::on_quit()
402 _main_win->hide();
405 void
406 Patchage::on_show_projects()
408 if (_menu_view_projects->get_active())
409 _project_list_viewport->show();
410 else
411 _project_list_viewport->hide();
414 void
415 Patchage::on_view_toolbar()
417 if (_menu_view_toolbar->get_active())
418 _toolbar->show();
419 else
420 _toolbar->hide();
424 bool
425 Patchage::on_scroll(GdkEventScroll* ev)
427 std::cout << "ON SCROLL" << std::endl;
428 return false;
432 void
433 Patchage::buffer_size_changed()
435 #if 0
436 const int selected = _buffer_size_combo->get_active_row_number();
438 if (selected == -1)
440 update_toolbar();
442 else
444 uint32_t buffer_size = 1 << (selected+5);
446 // this check is temporal workaround for jack bug
447 // we skip setting buffer size if it same as acutal one
448 // proper place for such check is in jack
449 if (_jack->buffer_size() != buffer_size)
451 if (!_jack->set_buffer_size(buffer_size))
453 update_toolbar(); // reset combo box to actual value
457 #endif
460 void
461 Patchage::set_studio_availability(
462 bool available)
464 if (available)
466 _main_win->set_title("Active Studio - LADI Session Handler");
468 else
470 _main_win->set_title("LADI Session Handler");
472 //_project_list->set_lash_availability(available);
473 _menu_view_projects->set_active(available);
476 #if 0
477 void
478 Patchage::set_a2j_status(
479 unsigned int status)
481 const char * status_text;
483 switch (status)
485 case A2J_STATUS_NO_RESPONSE:
486 status_text = "A2J N/A";
487 _menu_a2j_start->set_sensitive(false);
488 _menu_a2j_stop->set_sensitive(false);
489 break;
490 case A2J_STATUS_BRIDGE_STOPPED:
491 status_text = "A2J bridge stopped";
492 _menu_a2j_start->set_sensitive(true);
493 _menu_a2j_stop->set_sensitive(false);
494 break;
495 case A2J_STATUS_BRIDGE_STARTED:
496 status_text = "A2J bridge started";
497 _menu_a2j_start->set_sensitive(false);
498 _menu_a2j_stop->set_sensitive(true);
499 break;
500 default:
501 error_msg(str(boost::format("Unknown A2J status %u") % status));
502 status_text = "Unknown A2J status";
503 _menu_a2j_start->set_sensitive(true);
504 _menu_a2j_stop->set_sensitive(true);
505 break;
508 _main_a2j_status_label->set_text(status_text);
511 void
512 Patchage::load_project_ask()
514 std::list<lash_project_info> projects;
516 _lash->get_available_projects(projects);
517 run_load_project_dialog(projects);
520 void
521 Patchage::load_project(
522 const std::string& project_name)
524 _lash->load_project(project_name);
527 void
528 Patchage::save_all_projects()
530 _lash->save_all_projects();
533 void
534 Patchage::save_project(
535 const std::string& project_name)
537 _lash->save_project(project_name);
540 void
541 Patchage::close_project(
542 const std::string& project_name)
544 _lash->close_project(project_name);
547 void
548 Patchage::close_all_projects()
550 _lash->close_all_projects();
553 void
554 Patchage::on_port_added(
555 const char * jack_client_name,
556 const char * jack_port_name,
557 PortType port_type,
558 bool is_input,
559 bool is_terminal)
561 bool is_a2j_mapped;
562 std::string canvas_client_name;
563 std::string canvas_port_name;
564 uint32_t alsa_client_id;
565 boost::shared_ptr<PatchageModule> module;
567 is_a2j_mapped = strcmp(_a2j->get_jack_client_name(), jack_client_name) == 0;
568 if (is_a2j_mapped)
570 if (!_a2j->map_jack_port(jack_port_name, canvas_client_name, canvas_port_name, alsa_client_id))
572 return;
575 canvas_port_name = str(boost::format(canvas_port_name + " [a2j:%u]") % alsa_client_id);
577 else
579 canvas_client_name = jack_client_name;
580 canvas_port_name = jack_port_name;
583 ModuleType module_type = InputOutput;
584 if (_state_manager->get_module_split(canvas_client_name, is_terminal && !is_a2j_mapped)) {
585 if (is_input) {
586 module_type = Input;
587 } else {
588 module_type = Output;
592 module = _canvas->find_module(canvas_client_name, module_type);
593 if (!module) {
594 module = boost::shared_ptr<PatchageModule>(new PatchageModule(this, canvas_client_name, module_type));
595 module->load_location();
596 _canvas->add_item(module);
599 if (module->get_port(canvas_port_name)) {
600 return;
603 boost::shared_ptr<PatchagePort> port = boost::shared_ptr<PatchagePort>(
604 new PatchagePort(
605 module,
606 canvas_port_name,
607 is_input,
608 _state_manager->get_port_color(port_type)));
610 port->type = port_type;
611 port->is_a2j_mapped = is_a2j_mapped;
612 if (is_a2j_mapped)
614 port->a2j_jack_port_name = jack_port_name;
617 module->add_port(port);
619 module->resize();
622 boost::shared_ptr<PatchagePort>
623 Patchage::lookup_port(
624 const char * jack_client_name,
625 const char * jack_port_name)
627 if (strcmp(_a2j->get_jack_client_name(), jack_client_name) == 0)
629 return _canvas->lookup_port_by_a2j_jack_port_name(jack_port_name);
632 return _canvas->get_port(jack_client_name, jack_port_name);
635 void
636 Patchage::on_port_removed(
637 const char * jack_client_name,
638 const char * jack_port_name)
640 boost::shared_ptr<PatchagePort> port = lookup_port(jack_client_name, jack_port_name);
641 if (!port) {
642 error_msg(str(boost::format("Unable to remove unknown port '%s':'%s'") % jack_client_name % jack_port_name));
643 return;
646 boost::shared_ptr<PatchageModule> module = dynamic_pointer_cast<PatchageModule>(port->module().lock());
648 module->remove_port(port);
649 port.reset();
651 // No empty modules (for now)
652 if (module->num_ports() == 0) {
653 _canvas->remove_item(module);
654 module.reset();
655 } else {
656 module->resize();
660 void
661 Patchage::on_ports_connected(
662 const char * jack_client1_name,
663 const char * jack_port1_name,
664 const char * jack_client2_name,
665 const char * jack_port2_name)
667 boost::shared_ptr<PatchagePort> port1 = lookup_port(jack_client1_name, jack_port1_name);
668 if (!port1) {
669 error_msg((std::string)"Unable to connect unknown port '" + jack_port1_name + "' of client '" + jack_client1_name + "'");
670 return;
673 boost::shared_ptr<PatchagePort> port2 = lookup_port(jack_client2_name, jack_port2_name);
674 if (!port2) {
675 error_msg((std::string)"Unable to connect unknown port '" + jack_port2_name + "' of client '" + jack_client2_name + "'");
676 return;
679 _canvas->add_connection(port1, port2, port1->color() + 0x22222200);
682 void
683 Patchage::on_ports_disconnected(
684 const char * jack_client1_name,
685 const char * jack_port1_name,
686 const char * jack_client2_name,
687 const char * jack_port2_name)
689 boost::shared_ptr<PatchagePort> port1 = lookup_port(jack_client1_name, jack_port1_name);
690 if (!port1) {
691 error_msg((std::string)"Unable to disconnect unknown port '" + jack_port1_name + "' of client '" + jack_client1_name + "'");
692 return;
695 boost::shared_ptr<PatchagePort> port2 = lookup_port(jack_client2_name, jack_port2_name);
696 if (!port2) {
697 error_msg((std::string)"Unable to disconnect unknown port '" + jack_port2_name + "' of client '" + jack_client2_name + "'");
698 return;
701 _canvas->remove_connection(port1, port2);
704 static
705 bool
706 port_type_match(
707 boost::shared_ptr<PatchagePort> port1,
708 boost::shared_ptr<PatchagePort> port2)
710 return port1->type == port2->type;
713 void
714 Patchage::get_port_jack_names(
715 boost::shared_ptr<PatchagePort> port,
716 std::string& jack_client_name,
717 std::string& jack_port_name)
719 if (port->is_a2j_mapped)
721 jack_client_name = _a2j->get_jack_client_name();
722 jack_port_name = port->a2j_jack_port_name;
724 else
726 jack_client_name = port->module().lock()->name();
727 jack_port_name = port->name();
731 void
732 Patchage::connect(
733 boost::shared_ptr<PatchagePort> port1,
734 boost::shared_ptr<PatchagePort> port2)
736 std::string jack_client1_name;
737 std::string jack_port1_name;
738 std::string jack_client2_name;
739 std::string jack_port2_name;
741 if (port_type_match(port1, port2))
743 get_port_jack_names(port1, jack_client1_name, jack_port1_name);
744 get_port_jack_names(port2, jack_client2_name, jack_port2_name);
746 _jack->connect(
747 jack_client1_name.c_str(),
748 jack_port1_name.c_str(),
749 jack_client2_name.c_str(),
750 jack_port2_name.c_str());
752 else
754 status_msg("ERROR: Attempt to connect ports with mismatched types");
758 void
759 Patchage::disconnect(
760 boost::shared_ptr<PatchagePort> port1,
761 boost::shared_ptr<PatchagePort> port2)
763 std::string jack_client1_name;
764 std::string jack_port1_name;
765 std::string jack_client2_name;
766 std::string jack_port2_name;
768 if (port_type_match(port1, port2))
770 get_port_jack_names(port1, jack_client1_name, jack_port1_name);
771 get_port_jack_names(port2, jack_client2_name, jack_port2_name);
773 _jack->disconnect(
774 jack_client1_name.c_str(),
775 jack_port1_name.c_str(),
776 jack_client2_name.c_str(),
777 jack_port2_name.c_str());
779 else
781 status_msg("ERROR: Attempt to disconnect ports with mismatched types");
785 bool
786 Patchage::is_canvas_empty()
788 return _canvas->items().empty();
790 #endif