0b67ac8477f028d63783d9b20b389c2f46d455c8
[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     $Id$
19 */
20
21 #include <map>
22 #include <vector>
23 #include <stdint.h>
24
25 #include <gtkmm2ext/gtk_ui.h>
26 #include <gtkmm2ext/utils.h>
27 #include <sigc++/bind.h>
28
29 #include "connection_editor.h"
30
31 #include <ardour/session.h>
32 #include <ardour/session_connection.h>
33 #include <ardour/audioengine.h>
34 #include <ardour/connection.h>
35
36 #include "utils.h"
37 #include "keyboard.h"
38 #include "prompter.h"
39
40 #include "i18n.h"
41
42 #include <inttypes.h>
43
44 using namespace std;
45 using namespace ARDOUR;
46 using namespace Gtk;
47 using namespace sigc;
48
49 ConnectionEditor::ConnectionEditor ()
50         : ArdourDialog ("connection editor"),
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         set_title (_("ardour: connections"));
197         add (main_vbox);
198
199         // GTK2FIX
200         // signal_delete_event.connect (bind (ptr_fun (just_hide_it), reinterpret_cast<Window *> (this)));
201         
202         clear_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::clear));
203         add_port_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::add_port));
204         new_input_connection_button.signal_clicked().connect (bind (mem_fun(*this, &ConnectionEditor::new_connection), true));
205         new_output_connection_button.signal_clicked().connect (bind (mem_fun(*this, &ConnectionEditor::new_connection), false));
206         delete_connection_button.signal_clicked().connect (mem_fun(*this, &ConnectionEditor::delete_connection));
207 }
208
209 ConnectionEditor::~ConnectionEditor()
210 {
211 }
212
213 void
214 ConnectionEditor::set_session (Session *s)
215 {
216         if (s != session) {
217
218                 ArdourDialog::set_session (s);
219                 
220                 if (session) {
221                         session->ConnectionAdded.connect (mem_fun(*this, &ConnectionEditor::proxy_add_connection_and_select));
222                         session->ConnectionRemoved.connect (mem_fun(*this, &ConnectionEditor::proxy_remove_connection));
223                 } else {
224                         hide ();
225                 }
226         }
227 }
228
229 void
230 ConnectionEditor::rescan ()
231 {
232         refill_connection_display ();
233         display_ports ();
234 }
235
236 void
237 ConnectionEditor::cancel ()
238 {
239         hide ();
240 }
241
242 void
243 ConnectionEditor::accept ()
244 {
245         hide ();
246 }
247
248 void
249 ConnectionEditor::clear ()
250 {
251         if (current_connection) {
252                 current_connection->clear ();
253         }
254 }
255
256 void
257 ConnectionEditor::on_map ()
258 {
259         refill_connection_display ();
260         Window::on_map ();
261 }
262
263 void
264 ConnectionEditor::add_connection (ARDOUR::Connection *connection)
265 {
266         TreeModel::Row row;
267
268         if (dynamic_cast<InputConnection *> (connection)) {
269
270                 if (push_at_front) {
271                         row = *(input_connection_model->prepend());
272                 } else {
273                         row = *(input_connection_model->append());
274                 }
275
276         } else {
277
278                 if (push_at_front) {
279                         row = *(output_connection_model->prepend());
280                 } else {
281                         row = *(output_connection_model->append());
282                 }
283         }
284
285         row[connection_columns.connection] = connection;
286         row[connection_columns.name] = connection->name();
287 }
288
289 void
290 ConnectionEditor::remove_connection (ARDOUR::Connection *connection)
291 {
292         TreeModel::iterator i;
293         Glib::RefPtr<TreeModel> model = input_connection_model;
294
295         if (dynamic_cast<InputConnection *> (connection) == 0) {
296                 model = output_connection_model;
297         }
298
299         TreeModel::Children rows = model->children();
300
301         for (i = rows.begin(); i != rows.end(); ++i) {
302                 if ((*i)[connection_columns.connection] == connection) {
303                         // model->erase (i);
304                         break;
305                 }
306         }
307 }
308
309 void
310 ConnectionEditor::proxy_add_connection_and_select (ARDOUR::Connection *connection)
311 {
312         Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &ConnectionEditor::add_connection_and_select), connection));
313 }
314
315 void
316 ConnectionEditor::proxy_remove_connection (ARDOUR::Connection *connection)
317 {
318         Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &ConnectionEditor::remove_connection), connection));
319 }
320
321 void
322 ConnectionEditor::add_connection_and_select (ARDOUR::Connection *connection)
323 {
324         add_connection (connection);
325
326         // GTK2FIX
327         // if (dynamic_cast<InputConnection *> (connection)) {
328         // input_connection_display.rows().front().select ();
329         // } else {
330         //      output_connection_display.rows().front().select ();
331         //}
332 }
333
334 void
335 ConnectionEditor::refill_connection_display ()
336 {
337         input_connection_display.set_model (Glib::RefPtr<TreeModel>(0));
338         output_connection_display.set_model (Glib::RefPtr<TreeModel>(0));
339
340         input_connection_model.clear();
341         output_connection_model.clear();
342
343         current_connection = 0;
344         
345         if (session) {
346                 session->foreach_connection (this, &ConnectionEditor::add_connection);
347         }
348
349         input_connection_display.set_model (input_connection_model);
350         output_connection_display.set_model (output_connection_model);
351
352 }
353         
354 void
355 ConnectionEditor::selection_changed (TreeView* view)
356 {
357         ARDOUR::Connection *old_current = current_connection;
358
359         TreeIter iter;
360         TreeModel::Path path;
361         Glib::RefPtr<TreeView::Selection> selection = view->get_selection();
362         Glib::RefPtr<TreeModel> model = view->get_model();
363         bool input = (view == &input_connection_display);
364
365         iter = model->get_iter (path);
366         
367         current_connection = (*iter)[connection_columns.connection];
368
369         if (old_current != current_connection) {
370                 config_connection.disconnect ();
371                 connect_connection.disconnect ();
372         }
373
374         if (current_connection) {
375                 config_connection = current_connection->ConfigurationChanged.connect 
376                         (bind (mem_fun(*this, &ConnectionEditor::configuration_changed), input));
377                 connect_connection = current_connection->ConnectionsChanged.connect 
378                         (bind (mem_fun(*this, &ConnectionEditor::connections_changed), input));
379         }
380         
381         display_connection_state (input);
382         display_ports ();
383 }
384
385 void
386 ConnectionEditor::configuration_changed (bool for_input)
387 {
388         display_connection_state (for_input);
389 }
390
391 void
392 ConnectionEditor::connections_changed (int which_port, bool for_input)
393 {
394         display_connection_state (for_input);
395 }
396
397 void
398 ConnectionEditor::display_ports ()
399 {
400         if (session == 0 || current_connection == 0) {
401                 return;
402         }
403         
404         using namespace Notebook_Helpers;
405
406         typedef std::map<std::string,std::vector<std::pair<std::string,std::string> > > PortMap;
407         PortMap portmap;
408         const char **ports;
409         PageList& pages = notebook.pages();
410         gint current_page;
411         vector<string> rowdata;
412         bool for_input;
413
414         current_page = notebook.get_current_page ();
415         pages.clear ();
416
417         /* get relevant current JACK ports */
418
419         for_input = (dynamic_cast<InputConnection *> (current_connection) != 0);
420
421         ports = session->engine().get_ports ("", JACK_DEFAULT_AUDIO_TYPE, for_input?JackPortIsOutput:JackPortIsInput);
422
423         if (ports == 0) {
424                 return;
425         }
426
427         /* find all the client names and group their ports into a list-by-client */
428         
429         for (int n = 0; ports[n]; ++n) {
430
431                 pair<string,vector<pair<string,string> > > newpair;
432                 pair<string,string> strpair;
433                 pair<PortMap::iterator,bool> result;
434
435                 string str = ports[n];
436                 string::size_type pos;
437                 string portname;
438
439                 pos = str.find (':');
440
441                 newpair.first = str.substr (0, pos); 
442                 portname = str.substr (pos+1);
443
444                 result = portmap.insert (newpair);
445
446                 strpair.first = portname;
447                 strpair.second = str;
448
449                 result.first->second.push_back (strpair);
450         }
451
452         PortMap::iterator i;
453
454         for (i = portmap.begin(); i != portmap.end(); ++i) {
455                 
456                 Box *client_box = manage (new VBox);
457                 Gtk::CTreeView *display = manage (new Gtk::TreeView);
458                 RefPtr<TreeModel> model = TreeModel::create (columns);
459                 ScrolledWindow *scroller = manage (new ScrolledWindow);
460
461                 display->set_selection_mode (GTK_SELECTION_SINGLE);
462                 display->set_name ("ConnectionEditorList");
463
464                 for (vector<pair<string,string> >::iterator s = i->second.begin(); s != i->second.end(); ++s) {
465                         
466                         Row row = model->append ();
467
468                         row[displayed_name] = s->first;
469                         row[full_name] = s->second;
470                 }
471
472                 display->get_selection()->signal_changed().connect (bind (mem_fun(*this, &ConnectionEditor::port_selection_handler), display));
473                 
474                 Label *tab_label = manage (new Label);
475
476                 tab_label->set_name ("ConnectionEditorNotebookTab");
477                 tab_label->set_text ((*i).first);
478
479                 display->set_model (model);
480
481                 scroller->add (*client_port_display);
482                 scroller->set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
483
484                 client_box->pack_start (*scroller);
485
486                 pages.push_back (TabElem (*client_box, *tab_label));
487         }
488
489         notebook.set_page (current_page);
490         notebook.show.connect (bind (mem_fun (notebook, &Notebook::set_page), current_page));
491         selector_box.show_all ();
492 }       
493
494 void
495 ConnectionEditor::display_connection_state (bool for_input)
496 {
497         LockMonitor lm (port_display_lock, __LINE__, __FILE__);
498         uint32_t limit;
499
500         if (session == 0 || current_connection == 0) {
501                 return;
502         }
503
504         string frame_label = _("Connection \"");
505         frame_label += current_connection->name();
506         frame_label += _("\"");
507         port_frame.set_label (frame_label);
508
509         for (slist<ScrolledWindow *>::iterator i = port_displays.begin(); i != port_displays.end(); ) {
510                 
511                 slist<ScrolledWindow *>::iterator tmp;
512
513                 tmp = i;
514                 tmp++;
515
516                 port_box.remove (**i);
517                 delete *i;
518                 port_displays.erase (i);
519
520                 i = tmp;
521         } 
522         
523         limit = current_connection->nports();
524
525         for (uint32_t n = 0; n < limit; ++n) {
526
527                 CList *clist;
528                 ScrolledWindow *scroller;
529
530                 const gchar *title[1];
531                 char buf[32];
532                 string really_short_name;
533
534                 if (for_input) {
535                         snprintf(buf, sizeof(buf)-1, _("in %d"), n+1);
536                 } else {
537                         snprintf(buf, sizeof(buf)-1, _("out %d"), n+1);
538                 }
539                         
540                 tview = manage (new TreeView());
541                 Glib::RefPtr<ListStore> port_model = ListStore::create (*port_display_columns);
542                 
543                 tview->set_model (port_model);
544                 tview->append_column (_(buf), port_display_columns->name);
545                 tview->set_selection()->set_mode (Gtk::SELECTION_SINGLE);
546                 tview->set_data ("port", (gpointer) ((intptr_t) n));
547                 tview->set_headers_visible (true);
548                 tview->set_name ("ConnectionEditorPortList");
549                 tview->signal_button_press_event().connect (bind (mem_fun(*this, &ConnectionEditor::port_column_click), clist));
550
551                 scroller = manage (new ScrolledWindow);
552                 
553                 scroller->add (*tview);
554                 scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
555
556                 port_displays.insert (port_displays.end(), scroller);
557                 port_box.pack_start (*scroller);
558
559                 scroller->set_size_request (-1, 75);
560
561                 /* now fill the clist with the current connections */
562
563                 const ARDOUR::Connection::PortList& connections = current_connection->port_connections (n);
564         
565                 for (ARDOUR::Connection::PortList::const_iterator i = connections.begin(); i != connections.end(); ++i) {
566
567                         TreeModel::Row row = *(model->append());
568
569                         row[port_connection_columns.name] = (*i)->name();
570                 }
571         }
572
573         port_box.show_all ();
574 }
575
576 void
577 ConnectionEditor::port_selection_changed (TreeView* tview)
578 {
579         Glib::RefPtr<TreeView::Selection> sel = tview->get_selection();
580         TreeModel::iterator iter = sel->get_selected();
581
582         if (!current_connection) {
583                 return;
584         }
585
586         if (iter) {
587                 TreeModel::Row row = *iter;
588                 string other_port_name = row[port_display_columns.full_name];
589
590         
591         if (current_connection && selected_port >= 0) {
592                 current_connection->add_connection (selected_port, other_port_name);
593         }
594
595 }
596
597 void
598 ConnectionEditor::add_port ()
599 {
600         if (current_connection) {
601                 current_connection->add_port ();
602         }
603 }
604
605 void
606 ConnectionEditor::connection_port_button_press_event (GdkEventButton* ev, TreeView* tview)
607 {
608         LockMonitor lm (port_display_lock, __LINE__, __FILE__);
609
610         int which_port = reinterpret_cast<intptr_t> (treeview->get_data ("port"));
611
612         if (which_port != selected_port) {
613                 
614                 selected_port = which_port;
615                 display_ports ();
616
617                 tview->set_name ("ConnectionEditorPortListSelected");
618
619                 for (slist<ScrolledWindow *>::iterator i = port_displays.begin(); i != port_displays.end(); ++i) {
620
621                         Widget *child = (*i)->get_child();
622
623                         if (static_cast<TreeView *> (child) != tview) {
624                                 child->set_name ("ConnectionEditorPortList");
625                                 child->queue_draw ();
626                         }
627                 }
628                 
629         } else {
630                 
631                 selected_port = -1;
632                 clist->set_name ("ConnectionEditorPortList");
633                 clist->queue_draw();
634         }
635 }
636
637 void
638 ConnectionEditor::connection_selection_changed (TreeView* tview);
639 {
640         Glib::RefPtr<TreeView::Selection> sel = tview->get_selection();
641         TreeModel::iterator iter = sel->get_selected();
642
643         if (iter) {
644                 TreeModel::Row row = *iter;
645                 current_connection = row[XXXX_display_columns.connection];
646         } else {
647                 current_connection = 0;
648         }
649 }
650
651 void
652 ConnectionEditor::new_connection (bool for_input)
653 {
654         string name;
655
656         if (session == 0) {
657                 return;
658         }
659
660         ArdourPrompter prompter (true);
661         prompter.set_prompt (_("Name for new connection:"));
662         prompter.done.connect (Gtk::Main::quit.slot());
663
664         switch (prompter.run()) {
665         case GTK_RESPONSE_ACCEPT:
666                 prompter.get_result (name);
667                 push_at_front = true;
668                 if (name.length()) {
669                         if (for_input) {
670                                 session->add_connection (new ARDOUR::InputConnection (name));
671                         } else {
672                                 session->add_connection (new ARDOUR::OutputConnection (name));
673                         }
674                 }
675                 push_at_front = false;
676                 break;
677
678         default:
679                 break;
680         }
681 }
682
683 void
684 ConnectionEditor::delete_connection ()
685 {
686         if (session && current_connection) {
687                 session->remove_connection (current_connection);
688                 current_connection = 0;
689         }
690 }
691
692 gint
693 ConnectionEditor::port_button_event (GdkEventButton *ev, Treeview* treeview)
694 {
695         int row, col;
696         TreeIter iter;
697         TreeModel::Path path;
698         TreeViewColumn* column;
699         int cellx;
700         int celly;
701
702         if (current_connection == 0) {
703                 return false;
704         }
705
706         if (!(Keyboard::is_delete_event (ev))) {
707                 return false;
708         }
709
710         if (!treeview->get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
711                 return false;
712         }
713
714         if ((iter = treeview->get_model()->get_iter (path))) {
715                 /* path is valid */
716                 
717                 string port_name = (*iter)[columns.full_name];
718                 int which_port = (intptr_t) treeview->get_data ("port");        
719
720                 current_connection->remove_connection (which_port, port_name);
721         }
722
723         return true;
724 }