Vkeybd: default to single-row QWERTY binding
[ardour.git] / gtk2_ardour / virtual_keyboard_window.cc
1 /*
2  * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <gtkmm/box.h>
20
21
22 #include "pbd/convert.h"
23
24 #include "ardour/async_midi_port.h"
25 #include "ardour/session.h"
26
27 #include "gtkmm2ext/utils.h"
28 #include "widgets/tooltips.h"
29
30 #include "ardour_ui.h"
31 #include "ui_config.h"
32 #include "utils.h"
33 #include "virtual_keyboard_window.h"
34
35 #include "pbd/i18n.h"
36
37 using namespace Glib;
38 using namespace Gtk;
39 using namespace ArdourWidgets;
40
41 #define PX_SCALE(px) std::max ((float)px, rintf ((float)px* UIConfiguration::instance ().get_ui_scale ()))
42
43 VirtualKeyboardWindow::VirtualKeyboardWindow ()
44         : ArdourWindow (_("Virtual MIDI Keyboard"))
45         , _bank_msb (*manage (new Adjustment (0, 0, 127, 1, 16)))
46         , _bank_lsb (*manage (new Adjustment (0, 0, 127, 1, 16)))
47         , _patchpgm (*manage (new Adjustment (1, 1, 128, 1, 16)))
48         , _cfg_display (S_("Virtual keyboard|Config"), ArdourButton::led_default_elements)
49         , _pgm_display (_("Bank/Patch"), ArdourButton::led_default_elements)
50         , _yaxis_velocity (_("Y-Axis"), ArdourButton::led_default_elements)
51         , _send_panic (_("Panic"), ArdourButton::default_elements)
52         , _piano_key_velocity (*manage (new Adjustment (100, 1, 127, 1, 16)))
53         , _piano_min_velocity (*manage (new Adjustment (  1, 1, 127, 1, 16)))
54         , _piano_max_velocity (*manage (new Adjustment (127, 1, 127, 1, 16)))
55         , _piano_octave_key (*manage (new Adjustment (4, -1, 7, 1, 1)))
56         , _piano_octave_range (*manage (new Adjustment (7, 2, 11, 1, 1)))
57         , _pitch_adjustment (8192, 0, 16383, 1, 256)
58 {
59         _piano.set_flags (Gtk::CAN_FOCUS);
60
61         _piano.set_keyboard_layout (APianoKeyboard::S_QWERTY);
62         _piano.set_annotate_octave (true);
63         _piano.set_grand_piano_highlight (false);
64         _piano.set_annotate_layout (true);
65         _piano.set_annotate_octave (true);
66
67         for (int c = 0; c < 16; ++c) {
68                 char buf[16];
69                 sprintf (buf, "%d", c + 1);
70                 _midi_channel.append_text_item (buf);
71         }
72         for (int t = -12; t < 13; ++t) {
73                 char buf[16];
74                 sprintf (buf, "%d", t);
75                 _transpose_output.append_text_item (buf);
76         }
77
78         using namespace Menu_Helpers;
79         _keyboard_layout.AddMenuElem (MenuElem (_("QWERTY"), sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), "QWERTY")));
80         _keyboard_layout.AddMenuElem (MenuElem (_("QWERTZ"), sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), "QWERTZ")));
81         _keyboard_layout.AddMenuElem (MenuElem (_("AZERTY"), sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), "AZERTY")));
82         _keyboard_layout.AddMenuElem (MenuElem (_("DVORAK"), sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), "DVORAK")));
83         _keyboard_layout.AddMenuElem (MenuElem (_("QWERTY Single"), sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), "QWERTY Single")));
84         _keyboard_layout.AddMenuElem (MenuElem (_("QWERTZ Single"), sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), "QWERTZ Single")));
85
86         Gtkmm2ext::set_size_request_to_display_given_text_width (_keyboard_layout, _("QWERTZ Single"), 2, 0); // Longest Text
87         _keyboard_layout.set_active (_("QWERTY Single"));
88         _midi_channel.set_active ("1");
89         _transpose_output.set_active ("0");
90
91         _cfg_display.set_active (false);
92         _pgm_display.set_active (false);
93         _yaxis_velocity.set_active (false);
94
95         _send_panic.set_can_focus (false);
96
97         _pitchbend            = boost::shared_ptr<VKBDControl> (new VKBDControl ("PB", 8192, 16383));
98         _pitch_slider         = manage (new VSliderController (&_pitch_adjustment, _pitchbend, 0, PX_SCALE (15)));
99         _pitch_slider_tooltip = new Gtkmm2ext::PersistentTooltip (_pitch_slider);
100
101         _pitch_adjustment.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::pitch_slider_adjusted));
102         _pitchbend->ValueChanged.connect_same_thread (_cc_connections, boost::bind (&VirtualKeyboardWindow::pitch_bend_event_handler, this, _1));
103
104         set_tooltip (_yaxis_velocity, _("When enabled, mouse-click y-axis position defines the velocity."));
105
106         set_tooltip (_piano_octave_key, _("The center octave, and lowest octave for keyboard control. Change with Arrow left/right."));
107         set_tooltip (_piano_octave_range, _("Available octave range, centered around the key-octave."));
108         set_tooltip (_keyboard_layout, _("Keyboard layout to use for keyboard control."));
109
110         set_tooltip (_piano_key_velocity, _("The default velocity to use with keyboard control, and when y-axis click-position is disabled."));
111         set_tooltip (_piano_min_velocity, _("Velocity to use when clicking at the top-end of a key."));
112         set_tooltip (_piano_max_velocity, _("Velocity to use when clicking at the bottom-end of a key."));
113
114         set_tooltip (_send_panic, _("Send MIDI Panic message for current channel"));
115
116         _pitch_slider_tooltip->set_tip (_("Pitchbend: ") + std::string ("8192"));
117         _pitch_slider->set_can_focus (false);
118
119         /* config */
120         Table* cfg_tbl = manage (new Table);
121         cfg_tbl->attach (_yaxis_velocity,                      0, 1, 0, 1, SHRINK, SHRINK, 4, 0);
122         cfg_tbl->attach (*manage (new Label (_("Velocity:"))), 0, 1, 1, 2, SHRINK, SHRINK, 4, 0);
123
124         cfg_tbl->attach (_piano_min_velocity,                  1, 2, 0, 1, SHRINK, SHRINK, 4, 0);
125         cfg_tbl->attach (*manage (new Label (_("Min"))),       1, 2, 1, 2, SHRINK, SHRINK, 4, 0);
126         cfg_tbl->attach (_piano_max_velocity,                  2, 3, 0, 1, SHRINK, SHRINK, 4, 0);
127         cfg_tbl->attach (*manage (new Label (_("Max"))),       2, 3, 1, 2, SHRINK, SHRINK, 4, 0);
128         cfg_tbl->attach (_piano_key_velocity,                  3, 4, 0, 1, SHRINK, SHRINK, 4, 0);
129         cfg_tbl->attach (*manage (new Label (_("Key"))),       3, 4, 1, 2, SHRINK, SHRINK, 4, 0);
130
131         cfg_tbl->attach (*manage (new ArdourVSpacer),          4, 5, 0, 2, SHRINK, FILL,   4, 0);
132
133         cfg_tbl->attach (_piano_octave_key,                    5, 6, 0, 1, SHRINK, SHRINK, 4, 0);
134         cfg_tbl->attach (*manage (new Label (_("Octave"))),    5, 6, 1, 2, SHRINK, SHRINK, 4, 0);
135         cfg_tbl->attach (_piano_octave_range,                  6, 7, 0, 1, SHRINK, SHRINK, 4, 0);
136         cfg_tbl->attach (*manage (new Label (_("Range"))),     6, 7, 1, 2, SHRINK, SHRINK, 4, 0);
137
138         cfg_tbl->attach (*manage (new ArdourVSpacer),          7, 8, 0, 2, SHRINK, FILL,   4, 0);
139
140         cfg_tbl->attach (_keyboard_layout,                     8, 9, 0, 2, FILL,   SHRINK, 4, 1);
141         cfg_tbl->show_all ();
142
143         /* bank/patch */
144         Table* pgm_tbl = manage (new Table);
145
146         Label* lbl = manage (new Label (_("Note: Prefer\nTrack-controls")));
147         lbl->set_justify (JUSTIFY_CENTER);
148
149         pgm_tbl->attach (*lbl,                             0, 1, 0, 2, SHRINK, SHRINK, 4, 0);
150         pgm_tbl->attach (*manage (new ArdourVSpacer),      1, 2, 0, 2, SHRINK, FILL,   4, 0);
151         pgm_tbl->attach (_bank_msb,                        2, 3, 0, 1, SHRINK, SHRINK, 4, 0);
152         pgm_tbl->attach (_bank_lsb,                        3, 4, 0, 1, SHRINK, SHRINK, 4, 0);
153         pgm_tbl->attach (_patchpgm,                        4, 5, 0, 1, SHRINK, SHRINK, 4, 0);
154         pgm_tbl->attach (*manage (new Label (_("MSB"))),   2, 3, 1, 2, SHRINK, SHRINK, 4, 0);
155         pgm_tbl->attach (*manage (new Label (_("LSB"))),   3, 4, 1, 2, SHRINK, SHRINK, 4, 0);
156         pgm_tbl->attach (*manage (new Label (_("PGM"))),   4, 5, 1, 2, SHRINK, SHRINK, 4, 0);
157         pgm_tbl->attach (*manage (new ArdourVSpacer),      5, 6, 0, 2, SHRINK, FILL,   4, 0);
158         pgm_tbl->attach (_send_panic,                      6, 7, 0, 2, SHRINK, SHRINK, 4, 0);
159         pgm_tbl->show_all ();
160
161         /* settings */
162         Table* tbl = manage (new Table);
163         tbl->attach (_midi_channel, 0, 1, 0, 1, SHRINK, SHRINK, 4, 0);
164         tbl->attach (*manage (new Label (_("Channel"))), 0, 1, 1, 2, SHRINK, SHRINK, 4, 0);
165         tbl->attach (*manage (new ArdourVSpacer), 1, 2, 0, 2, SHRINK, FILL, 4, 0);
166         tbl->attach (*_pitch_slider, 2, 3, 0, 2, SHRINK, FILL, 4, 0);
167
168         const char* default_cc[VKBD_NCTRLS] = { "7", "8", "1", "11", "91", "92", "93", "94" };
169
170         int col = 3;
171         for (int i = 0; i < VKBD_NCTRLS; ++i, ++col) {
172                 _cc[i]      = boost::shared_ptr<VKBDControl> (new VKBDControl ("CC"));
173                 _cc_knob[i] = manage (new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Flags (0)));
174                 _cc_knob[i]->set_controllable (_cc[i]);
175                 _cc_knob[i]->set_size_request (PX_SCALE (21), PX_SCALE (21));
176                 _cc_knob[i]->set_tooltip_prefix (_("CC: "));
177                 _cc_knob[i]->set_name ("monitor section knob");
178
179                 for (int c = 1; c < 120; ++c) {
180                         if (c == 32) {
181                                 continue;
182                         }
183                         char key[32];
184                         sprintf (key, "%d", c);
185                         _cc_key[i].append_text_item (key);
186                 }
187                 _cc_key[i].set_active (default_cc[i]);
188
189                 tbl->attach (*_cc_knob[i], col, col + 1, 0, 1, SHRINK, SHRINK, 4, 2);
190                 tbl->attach (_cc_key[i],   col, col + 1, 1, 2, SHRINK, SHRINK, 4, 2);
191
192                 _cc[i]->ValueChanged.connect_same_thread (_cc_connections,
193                                                           boost::bind (&VirtualKeyboardWindow::control_change_event_handler, this, i, _1));
194         }
195
196         tbl->attach (*manage (new ArdourVSpacer), col, col + 1, 0, 2, SHRINK, FILL, 4, 0);
197         ++col;
198         tbl->attach (_transpose_output,                      col, col + 1, 0, 1, SHRINK, SHRINK, 4, 0);
199         tbl->attach (*manage (new Label (_("Transpose"))),   col, col + 1, 1, 2, SHRINK, SHRINK, 4, 0);
200
201         /* main layout */
202         Box* box1 = manage (new HBox ());
203         box1->pack_start (*tbl, true, false);
204
205         Box* box2 = manage (new VBox ());
206         box2->pack_start (_pgm_display, false, false, 1);
207         box2->pack_start (_cfg_display, false, false, 1);
208         box1->pack_start (*box2, false, false);
209
210         _cfg_box = manage (new HBox ());
211         _cfg_box->pack_start (*cfg_tbl, true, false);
212         _cfg_box->set_no_show_all (true);
213
214         _pgm_box = manage (new HBox ());
215         _pgm_box->pack_start (*pgm_tbl, true, false);
216         _pgm_box->set_no_show_all (true);
217
218         VBox* vbox = manage (new VBox);
219         vbox->pack_start (*box1, false, false, 4);
220         vbox->pack_start (*_pgm_box, false, false, 4);
221         vbox->pack_start (*_cfg_box, false, false, 4);
222         vbox->pack_start (_piano, true, true);
223         add (*vbox);
224
225         _bank_msb.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
226         _bank_lsb.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
227         _patchpgm.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
228
229         _piano_key_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 0));
230         _piano_min_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 1));
231         _piano_max_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 2));
232
233         _piano_octave_key.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_octave_key));
234         _piano_octave_range.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_octave_range));
235
236         _cfg_display.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::toggle_config), false);
237         _pgm_display.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::toggle_bankpatch), false);
238         _yaxis_velocity.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::toggle_yaxis_velocity), false);
239         _send_panic.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::send_panic_message), false);
240
241
242         _piano.NoteOn.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::note_on_event_handler));
243         _piano.NoteOff.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::note_off_event_handler));
244
245         update_velocity_settings (0);
246         update_octave_range ();
247
248         set_keep_above (true);
249         vbox->show_all ();
250 }
251
252 VirtualKeyboardWindow::~VirtualKeyboardWindow ()
253 {
254         delete _pitch_slider_tooltip;
255 }
256
257 void
258 VirtualKeyboardWindow::set_session (ARDOUR::Session* s)
259 {
260         ArdourWindow::set_session (s);
261
262         if (!_session) {
263                 return;
264         }
265
266         XMLNode* node = _session->instant_xml (X_("VirtualKeyboard"));
267         if (node) {
268                 set_state (*node);
269         }
270 }
271
272 XMLNode&
273 VirtualKeyboardWindow::get_state ()
274 {
275         XMLNode* node = new XMLNode (X_("VirtualKeyboard"));
276         node->set_property (X_("YAxisVelocity"), _yaxis_velocity.get_active ());
277         node->set_property (X_("Layout"), _keyboard_layout.get_text ());
278         node->set_property (X_("Channel"), _midi_channel.get_text ());
279         node->set_property (X_("Transpose"), _transpose_output.get_text ());
280         node->set_property (X_("MinVelocity"), _piano_min_velocity.get_value_as_int ());
281         node->set_property (X_("MaxVelocity"), _piano_max_velocity.get_value_as_int ());
282         node->set_property (X_("KeyVelocity"), _piano_key_velocity.get_value_as_int ());
283         node->set_property (X_("Octave"), _piano_octave_key.get_value_as_int ());
284         node->set_property (X_("Range"), _piano_octave_range.get_value_as_int ());
285         for (int i = 0; i < VKBD_NCTRLS; ++i) {
286                 char buf[16];
287                 sprintf (buf, "CC-%d", i);
288                 node->set_property (buf, _cc_key[i].get_text ());
289         }
290         return *node;
291 }
292
293 void
294 VirtualKeyboardWindow::set_state (const XMLNode& root)
295 {
296         if (root.name () != "VirtualKeyboard") {
297                 return;
298         }
299
300         XMLNode const* node = &root;
301
302         std::string layout;
303         if (node->get_property (X_("Layout"), layout)) {
304                 select_keyboard_layout (layout);
305         }
306
307         for (int i = 0; i < VKBD_NCTRLS; ++i) {
308                 char buf[16];
309                 sprintf (buf, "CC-%d", i);
310                 std::string cckey;
311                 if (node->get_property (buf, cckey)) {
312                         _cc_key[i].set_active (cckey);
313                 }
314         }
315
316         std::string s;
317         if (node->get_property (X_("Channel"), s)) {
318                 uint8_t channel = PBD::atoi (_midi_channel.get_text ());
319                 if (channel > 0 && channel < 17) {
320                         _midi_channel.set_active (s);
321                 }
322         }
323         if (node->get_property (X_("Transpose"), s)) {
324                 _transpose_output.set_active (s);
325         }
326
327         bool a;
328         if (node->get_property (X_("YAxisVelocity"), a)) {
329                 _yaxis_velocity.set_active (a);
330         }
331
332         int v;
333         if (node->get_property (X_("MinVelocity"), v)) {
334                 _piano_min_velocity.set_value (v);
335         }
336         if (node->get_property (X_("MaxVelocity"), v)) {
337                 _piano_max_velocity.set_value (v);
338         }
339         if (node->get_property (X_("KeyVelocity"), v)) {
340                 _piano_key_velocity.set_value (v);
341         }
342         if (node->get_property (X_("Octave"), v)) {
343                 _piano_octave_key.set_value (v);
344         }
345         if (node->get_property (X_("Range"), v)) {
346                 _piano_octave_range.set_value (v);
347         }
348
349         update_velocity_settings (0);
350         update_octave_range ();
351         update_octave_key ();
352 }
353
354 bool
355 VirtualKeyboardWindow::on_focus_in_event (GdkEventFocus* ev)
356 {
357         _piano.grab_focus ();
358         return ArdourWindow::on_focus_in_event (ev);
359 }
360
361 void
362 VirtualKeyboardWindow::on_unmap ()
363 {
364         ArdourWindow::on_unmap ();
365         ARDOUR_UI::instance ()->reset_focus (this);
366 }
367
368 bool
369 VirtualKeyboardWindow::on_key_press_event (GdkEventKey* ev)
370 {
371         /* try propagate unmodified events first */
372         if ((ev->state & 0xf) == 0) {
373                 if (gtk_window_propagate_key_event (gobj(), ev)) {
374                         return true;
375                 }
376         }
377
378         _piano.grab_focus ();
379
380         /* handle up/down */
381         // XXX consider to handle these in APianoKeyboard::on_key_press_event
382         // and use signals. -- also subscribe SustainChanged, indicate sustain.
383         // TODO: pitch-bend shortcuts
384         if (ev->type == GDK_KEY_PRESS) {
385                 if (ev->keyval == GDK_KEY_Left) {
386                         _piano_octave_key.set_value (_piano_octave_key.get_value_as_int () - 1);
387                         return true;
388                 }
389                 if (ev->keyval == GDK_KEY_Right) {
390                         _piano_octave_key.set_value (_piano_octave_key.get_value_as_int () + 1);
391                         return true;
392                 }
393         }
394
395         return ARDOUR_UI_UTILS::relay_key_press (ev, this);
396 }
397
398 void
399 VirtualKeyboardWindow::select_keyboard_layout (std::string const& l)
400 {
401         _keyboard_layout.set_active (l);
402         if (l == "QWERTY") {
403                 _piano.set_keyboard_layout (APianoKeyboard::QWERTY);
404         } else if (l == "QWERTZ") {
405                 _piano.set_keyboard_layout (APianoKeyboard::QWERTZ);
406         } else if (l == "AZERTY") {
407                 _piano.set_keyboard_layout (APianoKeyboard::AZERTY);
408         } else if (l == "DVORAK") {
409                 _piano.set_keyboard_layout (APianoKeyboard::DVORAK);
410         } else if (l == "QWERTY Single") {
411                 _piano.set_keyboard_layout (APianoKeyboard::S_QWERTY);
412         } else if (l == "QWERTZ Single") {
413                 _piano.set_keyboard_layout (APianoKeyboard::S_QWERTZ);
414         } else {
415         _keyboard_layout.set_active ("QWERTY");
416         }
417         _piano.grab_focus ();
418 }
419
420 bool
421 VirtualKeyboardWindow::toggle_config (GdkEventButton* ev)
422 {
423         bool a = !_cfg_display.get_active ();
424         _cfg_display.set_active (a);
425         if (a) {
426                 _cfg_box->show ();
427         } else {
428                 _cfg_box->hide ();
429         }
430         return false;
431 }
432
433 bool
434 VirtualKeyboardWindow::toggle_bankpatch (GdkEventButton*)
435 {
436         bool a = !_pgm_display.get_active ();
437         _pgm_display.set_active (a);
438         if (a) {
439                 _pgm_box->show ();
440         } else {
441                 _pgm_box->hide ();
442         }
443         return false;
444 }
445
446 void
447 VirtualKeyboardWindow::update_octave_key ()
448 {
449         _piano.set_octave (_piano_octave_key.get_value_as_int ());
450         _piano.grab_focus ();
451 }
452
453 void
454 VirtualKeyboardWindow::update_octave_range ()
455 {
456         _piano.set_octave_range (_piano_octave_range.get_value_as_int ());
457         _piano.set_grand_piano_highlight (_piano_octave_range.get_value_as_int () > 3);
458         _piano.grab_focus ();
459 }
460
461 bool
462 VirtualKeyboardWindow::toggle_yaxis_velocity (GdkEventButton*)
463 {
464         _yaxis_velocity.set_active (!_yaxis_velocity.get_active ());
465         update_velocity_settings (0);
466         return false;
467 }
468
469 bool
470 VirtualKeyboardWindow::send_panic_message (GdkEventButton*)
471 {
472         uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
473         uint8_t ev[3];
474         ev[0] = MIDI_CMD_CONTROL | channel;
475         ev[1] = MIDI_CTL_SUSTAIN;
476         ev[2] = 0;
477         _session->vkbd_output_port ()->write (ev, 3, 0);
478         ev[1] = MIDI_CTL_ALL_NOTES_OFF;
479         _session->vkbd_output_port ()->write (ev, 3, 0);
480         ev[1] = MIDI_CTL_RESET_CONTROLLERS;
481         _session->vkbd_output_port ()->write (ev, 3, 0);
482         return false;
483 }
484
485 void
486 VirtualKeyboardWindow::bank_patch ()
487 {
488         int msb = _bank_msb.get_value_as_int ();
489         int lsb = _bank_lsb.get_value_as_int ();
490         int pgm = _patchpgm.get_value_as_int () - 1;
491
492         uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
493         uint8_t ev[3];
494         ev[0] = MIDI_CMD_CONTROL | channel;
495         ev[1] = MIDI_CTL_MSB_BANK;
496         ev[2] = (msb >> 7) & 0x7f;
497         _session->vkbd_output_port ()->write (ev, 3, 0);
498         ev[1] = MIDI_CTL_LSB_BANK | channel;
499         ev[2] = lsb & 0x7f;
500         _session->vkbd_output_port ()->write (ev, 3, 0);
501         ev[0] = MIDI_CMD_PGM_CHANGE | channel;
502         ev[1] = pgm & 0x7f;
503         _session->vkbd_output_port ()->write (ev, 2, 0);
504 }
505
506 void
507 VirtualKeyboardWindow::update_velocity_settings (int ctrl)
508 {
509         if (_piano_min_velocity.get_value_as_int () > _piano_max_velocity.get_value_as_int ()) {
510                 if (ctrl == 2) {
511                         _piano_min_velocity.set_value (_piano_max_velocity.get_value_as_int ());
512                         return;
513                 } else {
514                         _piano_max_velocity.set_value (_piano_min_velocity.get_value_as_int ());
515                         return;
516                 }
517         }
518
519         if (_yaxis_velocity.get_active ()) {
520                 _piano.set_velocities (_piano_min_velocity.get_value_as_int (),
521                                        _piano_max_velocity.get_value_as_int (),
522                                        _piano_key_velocity.get_value_as_int ());
523         } else {
524                 _piano.set_velocities (_piano_key_velocity.get_value_as_int (),
525                                        _piano_key_velocity.get_value_as_int (),
526                                        _piano_key_velocity.get_value_as_int ());
527         }
528         update_sensitivity ();
529 }
530
531 void
532 VirtualKeyboardWindow::update_sensitivity ()
533 {
534         bool c = _yaxis_velocity.get_active ();
535         _piano_min_velocity.set_sensitive (c);
536         _piano_max_velocity.set_sensitive (c);
537         _piano.grab_focus ();
538 }
539
540 void
541 VirtualKeyboardWindow::pitch_slider_adjusted ()
542 {
543         _pitchbend->set_value (_pitch_adjustment.get_value (), PBD::Controllable::NoGroup);
544         char buf[64];
545         snprintf (buf, sizeof (buf), "%.0f", _pitch_adjustment.get_value ());
546         _pitch_slider_tooltip->set_tip (_("Pitchbend: ") + std::string(buf));
547 }
548
549 void
550 VirtualKeyboardWindow::note_on_event_handler (int note, int velocity)
551 {
552         _piano.grab_focus ();
553         if (!_session) {
554                 return;
555         }
556         note += PBD::atoi (_transpose_output.get_text ());
557         if (note < 0 || note > 127) {
558                 return;
559         }
560         uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
561         uint8_t ev[3];
562         ev[0] = MIDI_CMD_NOTE_ON | channel;
563         ev[1] = note;
564         ev[2] = velocity;
565         _session->vkbd_output_port ()->write (ev, 3, 0);
566 }
567
568 void
569 VirtualKeyboardWindow::note_off_event_handler (int note)
570 {
571         if (!_session) {
572                 return;
573         }
574         note += PBD::atoi (_transpose_output.get_text ());
575         if (note < 0 || note > 127) {
576                 return;
577         }
578         uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
579         uint8_t ev[3];
580         ev[0] = MIDI_CMD_NOTE_OFF | channel;
581         ev[1] = note;
582         ev[2] = 0;
583         _session->vkbd_output_port ()->write (ev, 3, 0);
584 }
585
586 void
587 VirtualKeyboardWindow::control_change_event_handler (int key, int val)
588 {
589         if (!_session) {
590                 return;
591         }
592         assert (key >= 0 && key < VKBD_NCTRLS);
593         int ctrl = PBD::atoi (_cc_key[key].get_text ());
594         assert (ctrl > 0 && ctrl < 127);
595         uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
596         uint8_t ev[3];
597         ev[0] = MIDI_CMD_CONTROL | channel;
598         ev[1] = ctrl;
599         ev[2] = val;
600         _session->vkbd_output_port ()->write (ev, 3, 0);
601 }
602
603 void
604 VirtualKeyboardWindow::pitch_bend_event_handler (int val)
605 {
606         if (!_session) {
607                 return;
608         }
609         uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
610         uint8_t ev[3];
611         ev[0] = MIDI_CMD_BENDER | channel;
612         ev[1] = val & 0x7f;
613         ev[2] = (val >> 7) & 0x7f;
614         _session->vkbd_output_port ()->write (ev, 3, 0);
615 }