1 /* -*- Mode: C ; c-basic-offset: 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.
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"
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) */
61 gtkmm_get_ink_pixel_size (Glib::RefPtr
<Pango::Layout
> layout
,
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
;
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
);
82 gtkmm_get_ink_pixel_size (w
.create_pango_layout (text
), width
, height
);
83 w
.set_size_request(width
+ hpadding
, old_height
);//height + vpadding);
89 #define INIT_WIDGET(x) x(g_xml, ((const char*)#x) + 1)
91 Patchage::Patchage(int argc
, char** argv
)
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
)
123 if (!strcmp(*argv
, "--help")) {
124 std::cout
<< "Usage: patchage [OPTIONS]\nOptions: --no-alsa" << std::endl
;
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());
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()
219 //delete _project_list;
223 _about_win
.destroy();
224 //_main_win.destroy();
226 patchage_dbus_uninit();
230 Patchage::idle_callback()
238 Patchage::update_toolbar()
243 started
= _jack
->is_started();
245 _buffer_size_combo
->set_sensitive(started
);
249 _buffer_size_combo
->set_active((int)log2f(_jack
->buffer_size()) - 5);
255 Patchage::update_load()
258 if (!_jack
->is_started())
260 _main_xrun_progress
->set_text("JACK stopped");
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
);
283 Patchage::zoom(double z
)
285 // _canvas->set_zoom(z);
294 // _canvas->destroy();
299 // for (ItemList::iterator i = _canvas->items().begin(); i != _canvas->items().end(); ++i) {
305 Patchage::clear_load()
308 _main_xrun_progress
->set_fraction(0.0);
309 _jack
->reset_xruns();
316 Patchage::error_msg(const std::string
& msg
)
318 #if defined(LOG_TO_STATUS)
321 #if defined(LOG_TO_STD)
322 std::cerr
<< msg
<< std::endl
;
328 Patchage::info_msg(const std::string
& msg
)
330 #if defined(LOG_TO_STATUS)
333 #if defined(LOG_TO_STD)
334 std::cerr
<< msg
<< std::endl
;
340 Patchage::status_msg(const std::string
& msg
)
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);
351 // module->load_location();
356 /** Update the sensitivity status of menus to reflect the present.
358 * (eg. disable "Connect to Jack" when Patchage is already connected to Jack)
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));
368 Patchage::jack_status_changed(
373 _menu_jack_start
->set_sensitive(!started
);
374 _menu_jack_stop
->set_sensitive(started
);
375 _clear_load_but
->set_sensitive(started
);
378 _main_xrun_progress
->set_fraction(0.0);
383 Patchage::on_arrange()
387 // _canvas->arrange();
392 Patchage::on_help_about()
406 Patchage::on_show_projects()
408 if (_menu_view_projects
->get_active())
409 _project_list_viewport
->show();
411 _project_list_viewport
->hide();
415 Patchage::on_view_toolbar()
417 if (_menu_view_toolbar
->get_active())
425 Patchage::on_scroll(GdkEventScroll
* ev
)
427 std::cout
<< "ON SCROLL" << std::endl
;
433 Patchage::buffer_size_changed()
436 const int selected
= _buffer_size_combo
->get_active_row_number();
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
461 Patchage::set_studio_availability(
466 _main_win
->set_title("Active Studio - LADI Session Handler");
470 _main_win
->set_title("LADI Session Handler");
472 //_project_list->set_lash_availability(available);
473 _menu_view_projects
->set_active(available
);
478 Patchage::set_a2j_status(
481 const char * status_text
;
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);
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);
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);
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);
508 _main_a2j_status_label
->set_text(status_text
);
512 Patchage::load_project_ask()
514 std::list
<lash_project_info
> projects
;
516 _lash
->get_available_projects(projects
);
517 run_load_project_dialog(projects
);
521 Patchage::load_project(
522 const std::string
& project_name
)
524 _lash
->load_project(project_name
);
528 Patchage::save_all_projects()
530 _lash
->save_all_projects();
534 Patchage::save_project(
535 const std::string
& project_name
)
537 _lash
->save_project(project_name
);
541 Patchage::close_project(
542 const std::string
& project_name
)
544 _lash
->close_project(project_name
);
548 Patchage::close_all_projects()
550 _lash
->close_all_projects();
554 Patchage::on_port_added(
555 const char * jack_client_name
,
556 const char * jack_port_name
,
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;
570 if (!_a2j
->map_jack_port(jack_port_name
, canvas_client_name
, canvas_port_name
, alsa_client_id
))
575 canvas_port_name
= str(boost::format(canvas_port_name
+ " [a2j:%u]") % alsa_client_id
);
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
)) {
588 module_type
= Output
;
592 module
= _canvas
->find_module(canvas_client_name
, module_type
);
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
)) {
603 boost::shared_ptr
<PatchagePort
> port
= boost::shared_ptr
<PatchagePort
>(
608 _state_manager
->get_port_color(port_type
)));
610 port
->type
= port_type
;
611 port
->is_a2j_mapped
= is_a2j_mapped
;
614 port
->a2j_jack_port_name
= jack_port_name
;
617 module
->add_port(port
);
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
);
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
);
642 error_msg(str(boost::format("Unable to remove unknown port '%s':'%s'") % jack_client_name
% jack_port_name
));
646 boost::shared_ptr
<PatchageModule
> module
= dynamic_pointer_cast
<PatchageModule
>(port
->module().lock());
648 module
->remove_port(port
);
651 // No empty modules (for now)
652 if (module
->num_ports() == 0) {
653 _canvas
->remove_item(module
);
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
);
669 error_msg((std::string
)"Unable to connect unknown port '" + jack_port1_name
+ "' of client '" + jack_client1_name
+ "'");
673 boost::shared_ptr
<PatchagePort
> port2
= lookup_port(jack_client2_name
, jack_port2_name
);
675 error_msg((std::string
)"Unable to connect unknown port '" + jack_port2_name
+ "' of client '" + jack_client2_name
+ "'");
679 _canvas
->add_connection(port1
, port2
, port1
->color() + 0x22222200);
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
);
691 error_msg((std::string
)"Unable to disconnect unknown port '" + jack_port1_name
+ "' of client '" + jack_client1_name
+ "'");
695 boost::shared_ptr
<PatchagePort
> port2
= lookup_port(jack_client2_name
, jack_port2_name
);
697 error_msg((std::string
)"Unable to disconnect unknown port '" + jack_port2_name
+ "' of client '" + jack_client2_name
+ "'");
701 _canvas
->remove_connection(port1
, port2
);
707 boost::shared_ptr
<PatchagePort
> port1
,
708 boost::shared_ptr
<PatchagePort
> port2
)
710 return port1
->type
== port2
->type
;
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
;
726 jack_client_name
= port
->module().lock()->name();
727 jack_port_name
= port
->name();
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
);
747 jack_client1_name
.c_str(),
748 jack_port1_name
.c_str(),
749 jack_client2_name
.c_str(),
750 jack_port2_name
.c_str());
754 status_msg("ERROR: Attempt to connect ports with mismatched types");
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
);
774 jack_client1_name
.c_str(),
775 jack_port1_name
.c_str(),
776 jack_client2_name
.c_str(),
777 jack_port2_name
.c_str());
781 status_msg("ERROR: Attempt to disconnect ports with mismatched types");
786 Patchage::is_canvas_empty()
788 return _canvas
->items().empty();