Merge libs/ardour and gtk2_ardour with 2.0-ongoing R2837.
[ardour.git] / gtk2_ardour / connection_editor.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <map>
21 #include <vector>
22 #include <stdint.h>
23
24 #include <gtkmm2ext/gtk_ui.h>
25 #include <gtkmm2ext/utils.h>
26 #include <sigc++/bind.h>
27
28 #include "connection_editor.h"
29
30 #include <ardour/session.h>
31 #include <ardour/session_connection.h>
32 #include <ardour/audioengine.h>
33 #include <ardour/connection.h>
34
35 #include "utils.h"
36 #include "keyboard.h"
37 #include "prompter.h"
38
39 #include "i18n.h"
40
41 #include <inttypes.h>
42
43 using namespace std;
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace Gtk;
47 using namespace sigc;
48
49 ConnectionEditor::ConnectionEditor ()
50         : ArdourDialog (_("ardour: connections")),
51           input_frame (_("Input Connections")),
52           output_frame (_("Output Connections")),
53           new_input_connection_button (_("New Input")),
54           new_output_connection_button (_("New Output")),
55           delete_connection_button (_("Delete")),
56           clear_button (_("Clear")),
57           add_port_button (_("Add Port")),
58           ok_button (_("Close")),
59           cancel_button (_("Cancel")),
60           rescan_button (_("Rescan"))
61           
62 {
63         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
64
65         session = 0;
66         selected_port = -1;
67         current_connection = 0;
68         push_at_front = false;
69
70         set_name ("ConnectionEditorWindow");
71
72         ok_button.set_name ("ConnectionEditorButton");
73         cancel_button.set_name ("ConnectionEditorButton");
74         rescan_button.set_name ("ConnectionEditorButton");
75         new_input_connection_button.set_name ("ConnectionEditorButton");
76         new_output_connection_button.set_name ("ConnectionEditorButton");
77         clear_button.set_name ("ConnectionEditorButton");
78         
79         button_frame.set_name ("ConnectionEditorFrame");
80         input_frame.set_name ("ConnectionEditorFrame");
81         output_frame.set_name ("ConnectionEditorFrame");
82
83         button_box.set_spacing (15);
84         button_box.set_border_width (5);
85         Gtkmm2ext::set_size_request_to_display_given_text (ok_button, _("OK"), 40, 15);
86         button_box.pack_end (ok_button, false, false);
87         // button_box.pack_end (cancel_button, false, false);
88         cancel_button.hide();
89         button_frame.add (button_box);
90
91         ok_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::accept));
92         cancel_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::cancel));
93         cancel_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::rescan));
94
95         notebook.set_name ("ConnectionEditorNotebook");
96         notebook.set_size_request (-1, 125);
97
98         clear_button.set_name ("ConnectionEditorButton");
99         add_port_button.set_name ("ConnectionEditorButton");
100         Gtkmm2ext::set_size_request_to_display_given_text (add_port_button, _("Add Port"), 35, 15);
101
102         selector_frame.set_name ("ConnectionEditorFrame");
103         port_frame.set_name ("ConnectionEditorFrame");
104
105         selector_frame.set_label (_("Available Ports"));
106         
107         selector_button_box.set_spacing (5);
108         selector_button_box.set_border_width (5);
109         Gtkmm2ext::set_size_request_to_display_given_text (rescan_button, _("Rescan"), 35, 15);
110         selector_button_box.pack_start (rescan_button, false, false);
111
112         selector_box.set_spacing (5);
113         selector_box.set_border_width (5);
114         selector_box.pack_start (notebook);
115         selector_box.pack_start (selector_button_box);
116
117         selector_frame.add (selector_box);
118
119         port_box.set_spacing (5);
120         port_box.set_border_width (3);
121
122         port_button_box.set_spacing (5);
123         port_button_box.set_border_width (2);
124
125         port_button_box.pack_start (add_port_button, false, false);
126         port_and_button_box.set_border_width (5);
127         port_and_button_box.pack_start (port_button_box, false, false);
128         port_and_button_box.pack_start (port_box);
129
130         port_frame.add (port_and_button_box);
131
132         port_and_selector_box.set_spacing (5);
133         port_and_selector_box.pack_start (port_frame);
134         port_and_selector_box.pack_start (selector_frame);
135
136         right_vbox.set_spacing (5);
137         right_vbox.set_border_width (5);
138         right_vbox.pack_start (port_and_selector_box);
139
140         input_connection_model = ListStore::create (connection_columns);
141         output_connection_model = ListStore::create (connection_columns);
142         
143         input_connection_display.set_model (input_connection_model);
144         output_connection_display.set_model (output_connection_model);
145
146         input_connection_display.append_column (_("Connections"), connection_columns.name);
147         output_connection_display.append_column (_("Connections"), connection_columns.name);
148
149         input_connection_display.get_selection()->set_mode(Gtk::SELECTION_SINGLE);
150         input_connection_display.set_size_request (80, -1);
151         input_connection_display.set_name ("ConnectionEditorConnectionList");
152
153         output_connection_display.get_selection()->set_mode(Gtk::SELECTION_SINGLE);
154         output_connection_display.set_size_request (80, -1);
155         output_connection_display.set_name ("ConnectionEditorConnectionList");
156
157         input_connection_display.get_selection()->signal_changed().connect (bind (mem_fun(*this, &ConnectionEditor::selection_changed), &input_connection_display));
158         output_connection_display.get_selection()->signal_changed().connect (bind (mem_fun(*this, &ConnectionEditor::selection_changed), &output_connection_display));
159
160
161         input_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
162         output_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
163
164         input_scroller.add (input_connection_display);
165         output_scroller.add (output_connection_display);
166
167         input_box.set_border_width (5);
168         input_box.set_spacing (5);
169         input_box.pack_start (input_scroller);
170         input_box.pack_start (new_input_connection_button, false, false);
171         input_frame.add (input_box);
172
173         output_box.set_border_width (5);
174         output_box.set_spacing (5);
175         output_box.pack_start (output_scroller);
176         output_box.pack_start (new_output_connection_button, false, false);
177         output_frame.add (output_box);
178
179         connection_box.set_spacing (5);
180         connection_box.pack_start (input_frame);
181         connection_box.pack_start (output_frame);
182
183         left_vbox.set_spacing (5);
184         left_vbox.pack_start (connection_box);
185
186         main_hbox.set_border_width (10);
187         main_hbox.set_spacing (5);
188         main_hbox.pack_start (left_vbox);
189         main_hbox.pack_start (right_vbox);
190
191         main_vbox.set_border_width (10);
192         main_vbox.set_spacing (5);
193         main_vbox.pack_start (main_hbox);
194         main_vbox.pack_start (button_frame, false, false);
195
196         get_vbox()->pack_start (main_vbox);
197         
198         clear_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::clear));
199         add_port_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::add_port));
200         new_input_connection_button.signal_clicked().connect (bind (mem_fun(*this, &ConnectionEditor::new_connection), true));
201         new_output_connection_button.signal_clicked().connect (bind (mem_fun(*this, &ConnectionEditor::new_connection), false));
202         delete_connection_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::delete_connection));
203 }
204
205 ConnectionEditor::~ConnectionEditor()
206 {
207 }
208
209 void
210 ConnectionEditor::set_session (Session *s)
211 {
212         if (s != session) {
213
214                 ArdourDialog::set_session (s);
215                 
216                 if (session) {
217                         session->ConnectionAdded.connect (mem_fun(*this, &ConnectionEditor::proxy_add_connection_and_select));
218                         session->ConnectionRemoved.connect (mem_fun(*this, &ConnectionEditor::proxy_remove_connection));
219                 } else {
220                         hide ();
221                 }
222         }
223 }
224
225 void
226 ConnectionEditor::rescan ()
227 {
228         refill_connection_display ();
229         display_ports ();
230 }
231
232 void
233 ConnectionEditor::cancel ()
234 {
235         hide ();
236 }
237
238 void
239 ConnectionEditor::accept ()
240 {
241         hide ();
242 }
243
244 void
245 ConnectionEditor::clear ()
246 {
247         if (current_connection) {
248                 current_connection->clear ();
249         }
250 }
251
252 void
253 ConnectionEditor::on_map ()
254 {
255         refill_connection_display ();
256         Window::on_map ();
257 }
258
259 void
260 ConnectionEditor::add_connection (ARDOUR::Connection *connection)
261 {
262         TreeModel::Row row;
263
264         if (dynamic_cast<InputConnection *> (connection)) {
265
266                 if (push_at_front) {
267                         row = *(input_connection_model->prepend());
268                 } else {
269                         row = *(input_connection_model->append());
270                 }
271
272         } else {
273
274                 if (push_at_front) {
275                         row = *(output_connection_model->prepend());
276                 } else {
277                         row = *(output_connection_model->append());
278                 }
279         }
280
281         row[connection_columns.connection] = connection;
282         row[connection_columns.name] = connection->name();
283 }
284
285 void
286 ConnectionEditor::remove_connection (ARDOUR::Connection *connection)
287 {
288         TreeModel::iterator i;
289         Glib::RefPtr<TreeModel> model = input_connection_model;
290
291         if (dynamic_cast<InputConnection *> (connection) == 0) {
292                 model = output_connection_model;
293         }
294
295         TreeModel::Children rows = model->children();
296
297         for (i = rows.begin(); i != rows.end(); ++i) {
298                 if ((*i)[connection_columns.connection] == connection) {
299                         // model->erase (i);
300                         break;
301                 }
302         }
303 }
304
305 void
306 ConnectionEditor::proxy_add_connection_and_select (ARDOUR::Connection *connection)
307 {
308         Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &ConnectionEditor::add_connection_and_select), connection));
309 }
310
311 void
312 ConnectionEditor::proxy_remove_connection (ARDOUR::Connection *connection)
313 {
314         Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &ConnectionEditor::remove_connection), connection));
315 }
316
317 void
318 ConnectionEditor::add_connection_and_select (ARDOUR::Connection *connection)
319 {
320         add_connection (connection);
321
322         // GTK2FIX
323         // if (dynamic_cast<InputConnection *> (connection)) {
324         // input_connection_display.rows().front().select ();
325         // } else {
326         //      output_connection_display.rows().front().select ();
327         //}
328 }
329
330 void
331 ConnectionEditor::refill_connection_display ()
332 {
333         input_connection_display.set_model (Glib::RefPtr<TreeModel>(0));
334         output_connection_display.set_model (Glib::RefPtr<TreeModel>(0));
335
336         input_connection_model->clear();
337         output_connection_model->clear();
338
339         current_connection = 0;
340         
341         if (session) {
342                 session->foreach_connection (this, &ConnectionEditor::add_connection);
343         }
344
345         input_connection_display.set_model (input_connection_model);
346         output_connection_display.set_model (output_connection_model);
347
348 }
349         
350 void
351 ConnectionEditor::selection_changed (TreeView* view)
352 {
353         ARDOUR::Connection *old_current = current_connection;
354
355         TreeIter iter;
356         TreeModel::Path path;
357         Glib::RefPtr<TreeView::Selection> selection = view->get_selection();
358         Glib::RefPtr<TreeModel> model = view->get_model();
359         bool input = (view == &input_connection_display);
360
361         iter = model->get_iter (path);
362         
363         current_connection = (*iter)[connection_columns.connection];
364
365         if (old_current != current_connection) {
366                 config_connection.disconnect ();
367                 connect_connection.disconnect ();
368         }
369
370         if (current_connection) {
371                 config_connection = current_connection->ConfigurationChanged.connect 
372                         (bind (mem_fun(*this, &ConnectionEditor::configuration_changed), input));
373                 connect_connection = current_connection->ConnectionsChanged.connect 
374                         (bind (mem_fun(*this, &ConnectionEditor::connections_changed), input));
375         }
376         
377         display_connection_state (input);
378         display_ports ();
379 }
380
381 void
382 ConnectionEditor::configuration_changed (bool for_input)
383 {
384         display_connection_state (for_input);
385 }
386
387 void
388 ConnectionEditor::connections_changed (int which_port, bool for_input)
389 {
390         display_connection_state (for_input);
391 }
392
393 void
394 ConnectionEditor::display_ports ()
395 {
396         if (session == 0 || current_connection == 0) {
397                 return;
398         }
399         
400         using namespace Notebook_Helpers;
401
402         typedef std::map<std::string,std::vector<std::pair<std::string,std::string> > > PortMap;
403         PortMap portmap;
404         const char **ports;
405         PageList& pages = notebook.pages();
406         gint current_page;
407         vector<string> rowdata;
408         bool for_input;
409
410         current_page = notebook.get_current_page ();
411         pages.clear ();
412
413         /* get relevant current JACK ports */
414
415         for_input = (dynamic_cast<InputConnection *> (current_connection) != 0);
416
417         ports = session->engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input?JackPortIsOutput:JackPortIsInput);
418
419         if (ports == 0) {
420                 return;
421         }
422
423         /* find all the client names and group their ports into a list-by-client */
424         
425         for (int n = 0; ports[n]; ++n) {
426
427                 pair<string,vector<pair<string,string> > > newpair;
428                 pair<string,string> strpair;
429                 pair<PortMap::iterator,bool> result;
430
431                 string str = ports[n];
432                 string::size_type pos;
433                 string portname;
434
435                 pos = str.find (':');
436
437                 newpair.first = str.substr (0, pos); 
438                 portname = str.substr (pos+1);
439
440                 result = portmap.insert (newpair);
441
442                 strpair.first = portname;
443                 strpair.second = str;
444
445                 result.first->second.push_back (strpair);
446         }
447
448         PortMap::iterator i;
449
450         for (i = portmap.begin(); i != portmap.end(); ++i) {
451                 
452                 Box *client_box = manage (new VBox);
453                 Gtk::CTreeView *display = manage (new Gtk::TreeView);
454                 RefPtr<TreeModel> model = TreeModel::create (columns);
455                 ScrolledWindow *scroller = manage (new ScrolledWindow);
456
457                 display->set_selection_mode (GTK_SELECTION_SINGLE);
458                 display->set_name ("ConnectionEditorList");
459
460                 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
461                         
462                         Row row = model->append ();
463
464                         row[displayed_name] = s->first;
465                         row[full_name] = s->second;
466                 }
467
468                 display->get_selection()->signal_changed().connect (bind (mem_fun(*this, &ConnectionEditor::port_selection_handler), display));
469                 
470                 Label *tab_label = manage (new Label);
471
472                 tab_label->set_name ("ConnectionEditorNotebookTab");
473                 tab_label->set_text ((*i).first);
474
475                 display->set_model (model);
476
477                 scroller->add (*client_port_display);
478                 scroller->set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
479
480                 client_box->pack_start (*scroller);
481
482                 pages.push_back (TabElem (*client_box, *tab_label));
483         }
484
485         notebook.set_page (current_page);
486         notebook.show.connect (bind (mem_fun (notebook, &Notebook::set_page), current_page));
487         selector_box.show_all ();
488 }       
489
490 void
491 ConnectionEditor::display_connection_state (bool for_input)
492 {
493         Glib::Mutex::Lock lm  (port_display_lock);
494         uint32_t limit;
495
496         if (session == 0 || current_connection == 0) {
497                 return;
498         }
499
500         string frame_label = _("Connection \"");
501         frame_label += current_connection->name();
502         frame_label += _("\"");
503         port_frame.set_label (frame_label);
504
505         for (slist<ScrolledWindow *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
506                 
507                 slist<ScrolledWindow *>::iterator tmp;
508
509                 tmp = i;
510                 tmp++;
511
512                 port_box.remove (**i);
513                 delete *i;
514                 port_displays.erase (i);
515
516                 i = tmp;
517         } 
518         
519         limit = current_connection->nports();
520
521         for (uint32_t n = 0; n < limit; ++n) {
522
523                 CList *clist;
524                 ScrolledWindow *scroller;
525
526                 const gchar *title[1];
527                 char buf[32];
528                 string really_short_name;
529
530                 if (for_input) {
531                         snprintf(buf, sizeof(buf)-1, _("in %d"), n+1);
532                 } else {
533                         snprintf(buf, sizeof(buf)-1, _("out %d"), n+1);
534                 }
535                         
536                 tview = manage (new TreeView());
537                 Glib::RefPtr<ListStore> port_model = ListStore::create (*port_display_columns);
538                 
539                 tview->set_model (port_model);
540                 tview->append_column (_(buf), port_display_columns->name);
541                 tview->set_selection()->set_mode (Gtk::SELECTION_SINGLE);
542                 tview->set_data ("port", (gpointer) ((intptr_t) n));
543                 tview->set_headers_visible (true);
544                 tview->set_name ("ConnectionEditorPortList");
545                 tview->signal_button_press_event().connect (bind (mem_fun(*this, &ConnectionEditor::port_column_click), clist));
546
547                 scroller = manage (new ScrolledWindow);
548                 
549                 scroller->add (*tview);
550                 scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
551
552                 port_displays.insert (port_displays.end(), scroller);
553                 port_box.pack_start (*scroller);
554
555                 scroller->set_size_request (-1, 75);
556
557                 /* now fill the clist with the current connections */
558
559                 const ARDOUR::Connection::PortList& connections = current_connection->port_connections (n);
560         
561                 for (ARDOUR::Connection::PortList::const_iterator i = connections.begin(); i != connections.end(); ++i) {
562
563                         TreeModel::Row row = *(model->append());
564
565                         row[port_connection_columns.name] = (*i)->name();
566                 }
567         }
568
569         port_box.show_all ();
570 }
571
572 void
573 ConnectionEditor::port_selection_changed (TreeView* tview)
574 {
575         Glib::RefPtr<TreeView::Selection> sel = tview->get_selection();
576         TreeModel::iterator iter = sel->get_selected();
577
578         if (!current_connection) {
579                 return;
580         }
581
582         if (iter) {
583                 TreeModel::Row row = *iter;
584                 string other_port_name = row[port_display_columns.full_name];
585
586         
587         if (current_connection && selected_port >= 0) {
588                 current_connection->add_connection (selected_port, other_port_name);
589         }
590
591 }
592
593 void
594 ConnectionEditor::add_port ()
595 {
596         if (current_connection) {
597                 current_connection->add_port ();
598         }
599 }
600
601 void
602 ConnectionEditor::connection_port_button_press_event (GdkEventButton* ev, TreeView* tview)
603 {
604         Glib::Mutex::Lock lm  (port_display_lock);
605
606         int which_port = reinterpret_cast<intptr_t> (treeview->get_data ("port"));
607
608         if (which_port != selected_port) {
609                 
610                 selected_port = which_port;
611                 display_ports ();
612
613                 tview->set_name ("ConnectionEditorPortListSelected");
614
615                 for (slist<ScrolledWindow *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
616
617                         Widget *child = (*i)->get_child();
618
619                         if (static_cast<TreeView *> (child) != tview) {
620                                 child->set_name ("ConnectionEditorPortList");
621                                 child->queue_draw ();
622                         }
623                 }
624                 
625         } else {
626                 
627                 selected_port = -1;
628                 clist->set_name ("ConnectionEditorPortList");
629                 clist->queue_draw();
630         }
631 }
632
633 void
634 ConnectionEditor::connection_selection_changed (TreeView* tview);
635 {
636         Glib::RefPtr<TreeView::Selection> sel = tview->get_selection();
637         TreeModel::iterator iter = sel->get_selected();
638
639         if (iter) {
640                 TreeModel::Row row = *iter;
641                 current_connection = row[XXXX_display_columns.connection];
642         } else {
643                 current_connection = 0;
644         }
645 }
646
647 void
648 ConnectionEditor::new_connection (bool for_input)
649 {
650         string name;
651
652         if (session == 0) {
653                 return;
654         }
655
656         ArdourPrompter prompter (true);
657         prompter.set_prompt (_("Name for new connection:"));
658         prompter.done.connect (Gtk::Main::quit.slot());
659
660         switch (prompter.run()) {
661         case Gtk::RESPONSE_ACCEPT:
662                 prompter.get_result (name);
663                 push_at_front = true;
664                 if (name.length()) {
665                         if (for_input) {
666                                 session->add_connection (new ARDOUR::InputConnection (name));
667                         } else {
668                                 session->add_connection (new ARDOUR::OutputConnection (name));
669                         }
670                 }
671                 push_at_front = false;
672                 break;
673
674         default:
675                 break;
676         }
677 }
678
679 void
680 ConnectionEditor::delete_connection ()
681 {
682         if (session && current_connection) {
683                 session->remove_connection (current_connection);
684                 current_connection = 0;
685         }
686 }
687
688 gint
689 ConnectionEditor::port_button_event (GdkEventButton *ev, Treeview* treeview)
690 {
691         int row, col;
692         TreeIter iter;
693         TreeModel::Path path;
694         TreeViewColumn* column;
695         int cellx;
696         int celly;
697
698         if (current_connection == 0) {
699                 return false;
700         }
701
702         if (!(Keyboard::is_delete_event (ev))) {
703                 return false;
704         }
705
706         if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
707                 return false;
708         }
709
710         if ((iter = treeview->get_model()->get_iter (path))) {
711                 /* path is valid */
712                 
713                 string port_name = (*iter)[columns.full_name];
714                 int which_port = (intptr_t) treeview->get_data ("port");        
715
716                 current_connection->remove_connection (which_port, port_name);
717         }
718
719         return true;
720 }