Virtual-Keyboard: Fix use of scrollwheel on dropdowns
[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 #include "pbd/convert.h"
22 #include "ardour/async_midi_port.h"
23 #include "ardour/session.h"
24 #include "widgets/tooltips.h"
25
26 #include "ardour_ui.h"
27 #include "virtual_keyboard_window.h"
28 #include "ui_config.h"
29 #include "utils.h"
30
31 #include "pbd/i18n.h"
32
33 using namespace Glib;
34 using namespace Gtk;
35 using namespace ArdourWidgets;
36
37 #define PX_SCALE(px) std::max((float)px, rintf((float)px * UIConfiguration::instance().get_ui_scale()))
38
39 VirtualKeyboardWindow::VirtualKeyboardWindow ()
40         : ArdourWindow (_("Virtual Keyboard"))
41         , _piano_channel (*manage (new Adjustment (1, 1, 16, 1, 1)))
42         , _bank_msb (*manage (new Adjustment (0, 0, 127, 1, 16)))
43         , _bank_lsb (*manage (new Adjustment (0, 0, 127, 1, 16)))
44         , _patchpgm (*manage (new Adjustment (1, 1, 128, 1, 16)))
45         , _cfg_display ("Config", ArdourButton::led_default_elements)
46         , _pgm_display ("Bank/Patch", ArdourButton::led_default_elements)
47         , _yaxis_velocity ("Y-Axis Click Velocity", ArdourButton::led_default_elements)
48         , _send_panic ("Panic", ArdourButton::default_elements)
49         , _piano_key_velocity (*manage (new Adjustment (100, 1, 127, 1, 16)))
50         , _piano_min_velocity (*manage (new Adjustment (1  , 1, 127, 1, 16)))
51         , _piano_max_velocity (*manage (new Adjustment (127, 1, 127, 1, 16)))
52         , _pitch_adjustment (8192, 0, 16383, 1, 256)
53 {
54         _piano = (PianoKeyboard*)piano_keyboard_new();
55         _pianomm = Glib::wrap((GtkWidget*)_piano);
56         _pianomm->set_flags(Gtk::CAN_FOCUS);
57         _pianomm->add_events(Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
58         piano_keyboard_set_keyboard_layout (_piano, "QWERTY");
59
60         using namespace Menu_Helpers;
61         _keyboard_layout.AddMenuElem (MenuElem ("QWERTY",
62                                 sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), 0)));
63         _keyboard_layout.AddMenuElem (MenuElem ("QWERTZ",
64                                 sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), 1)));
65         _keyboard_layout.AddMenuElem (MenuElem ("AZERTY",
66                                 sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), 2)));
67         _keyboard_layout.set_active (_("QWERTY"));
68
69         _cfg_display.set_active (false);
70         _pgm_display.set_active (false);
71         _yaxis_velocity.set_active (false);
72
73         _pitchbend = boost::shared_ptr<VKBDControl> (new VKBDControl ("PB", 8192, 16383));
74         _pitch_slider = manage (new VSliderController(&_pitch_adjustment, _pitchbend, 0, PX_SCALE (15)));
75         _pitch_slider_tooltip = new Gtkmm2ext::PersistentTooltip (_pitch_slider);
76         _pitch_adjustment.signal_value_changed().connect (
77                         sigc::mem_fun (*this, &VirtualKeyboardWindow::pitch_slider_adjusted));
78         _pitchbend->ValueChanged.connect_same_thread (_cc_connections,
79                         boost::bind (&VirtualKeyboardWindow::pitch_bend_event_handler, this, _1));
80
81         set_tooltip (_send_panic, "Send MIDI Panic message for current channel");
82         _pitch_slider_tooltip->set_tip ("Pitchbend: 8192");
83
84         /* config */
85         HBox* cfg_box = manage (new HBox);
86         cfg_box->set_spacing (4);
87         cfg_box->pack_start (*manage (new Label (_("Key Velocity:"))), false, false);
88         cfg_box->pack_start (_piano_key_velocity, false, false);
89         cfg_box->pack_start (_yaxis_velocity, false, false, 8);
90         cfg_box->pack_start (*manage (new Label (_("Min Velocity:"))), false, false);
91         cfg_box->pack_start (_piano_min_velocity, false, false);
92         cfg_box->pack_start (*manage (new Label (_("Max Velocity:"))), false, false);
93         cfg_box->pack_start (_piano_max_velocity, false, false);
94         cfg_box->pack_start (_keyboard_layout, false, false, 8);
95         cfg_box->show_all ();
96
97         /* bank/patch */
98         Table* pgm_tbl = manage (new Table);
99
100         Label* lbl = manage (new Label (_("Note: Prefer\nTrack-controls")));
101         lbl->set_justify (JUSTIFY_CENTER);
102
103         pgm_tbl->attach (*lbl, 0, 1, 0, 2, SHRINK, SHRINK, 4, 0);
104         pgm_tbl->attach (*manage (new ArdourVSpacer), 1, 2, 0, 2, SHRINK, FILL, 4, 0);
105         pgm_tbl->attach (_bank_msb, 2, 3, 0, 1, SHRINK, SHRINK, 4, 0);
106         pgm_tbl->attach (_bank_lsb, 3, 4, 0, 1, SHRINK, SHRINK, 4, 0);
107         pgm_tbl->attach (_patchpgm, 4, 5, 0, 1, SHRINK, SHRINK, 4, 0);
108         pgm_tbl->attach (*manage (new Label (_("MSB"))), 2, 3, 1, 2, SHRINK, SHRINK, 4, 0);
109         pgm_tbl->attach (*manage (new Label (_("LSB"))), 3, 4, 1, 2, SHRINK, SHRINK, 4, 0);
110         pgm_tbl->attach (*manage (new Label (_("PGM"))), 4, 5, 1, 2, SHRINK, SHRINK, 4, 0);
111         pgm_tbl->attach (*manage (new ArdourVSpacer), 5, 6, 0, 2, SHRINK, FILL, 4, 0);
112         pgm_tbl->attach (_send_panic, 6, 7, 0, 2, SHRINK, SHRINK, 4, 0);
113         pgm_tbl->show_all ();
114
115         /* settings */
116         Table* tbl = manage (new Table);
117         tbl->attach (_piano_channel, 0, 1, 0, 1, SHRINK, SHRINK, 4, 0);
118         tbl->attach (*manage (new Label (_("Channel"))), 0, 1, 1, 2, SHRINK, SHRINK, 4, 0);
119         tbl->attach (*manage (new ArdourVSpacer), 1, 2, 0, 2, SHRINK, FILL, 4, 0);
120         tbl->attach (*_pitch_slider, 2, 3, 0, 2, SHRINK, FILL, 4, 0);
121
122         const char* default_cc[VKBD_NCTRLS] = { "7", "8", "1", "11", "91", "92", "93", "94" };
123
124         for (int i = 0; i < VKBD_NCTRLS; ++i) {
125                 _cc[i] = boost::shared_ptr<VKBDControl> (new VKBDControl ("CC"));
126                 _cc_knob[i] = manage(new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Flags (0)));
127                 _cc_knob[i]->set_controllable (_cc[i]);
128                 _cc_knob[i]->set_size_request (PX_SCALE(21), PX_SCALE(21));
129                 _cc_knob[i]->set_tooltip_prefix (_("CC: "));
130                 _cc_knob[i]->set_name ("monitor section knob");
131
132                 for (int c = 1; c < 120; ++c) {
133                         if (c == 32) {
134                                 continue;
135                         }
136                         char key[32];
137                         sprintf (key, "%d", c);
138                         _cc_key[i].append_text_item (key);
139                 }
140                 _cc_key[i].set_active (default_cc[i]);
141
142                 tbl->attach (*_cc_knob[i], i+3, i+4, 0, 1, SHRINK, SHRINK, 4, 2);
143                 tbl->attach (_cc_key[i]  , i+3, i+4, 1, 2, SHRINK, SHRINK, 4, 2);
144
145                 _cc[i]->ValueChanged.connect_same_thread (_cc_connections,
146                                 boost::bind (&VirtualKeyboardWindow::control_change_event_handler, this, i, _1));
147         }
148
149         /* main layout */
150         Box* box1 = manage (new HBox ());
151         box1->pack_start (*tbl, true, false);
152
153         Box* box2 = manage (new VBox ());
154         box2->pack_start (_pgm_display, false, false);
155         box2->pack_start (_cfg_display, false, false);
156         box1->pack_start (*box2, false, false);
157
158         _cfg_box = manage (new HBox ());
159         _cfg_box->pack_start (*cfg_box, true, false);
160         _cfg_box->set_no_show_all (true);
161
162         _pgm_box = manage (new HBox ());
163         _pgm_box->pack_start (*pgm_tbl, true, false);
164         _pgm_box->set_no_show_all (true);
165
166         VBox* vbox = manage (new VBox);
167         vbox->pack_start (*box1, false, false, 4);
168         vbox->pack_start (*_pgm_box, false, false, 4);
169         vbox->pack_start (*_cfg_box, false, false, 4);
170         vbox->pack_start (*_pianomm, true, true);
171         add (*vbox);
172
173         _bank_msb.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
174         _bank_lsb.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
175         _patchpgm.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
176
177         _piano_key_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 0));
178         _piano_min_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 1));
179         _piano_max_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 2));
180
181         _cfg_display.signal_button_release_event().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::toggle_config), false);
182         _pgm_display.signal_button_release_event().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::toggle_bankpatch), false);
183         _yaxis_velocity.signal_button_release_event().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::toggle_yaxis_velocity), false);
184         _send_panic.signal_button_release_event().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::send_panic_message), false);
185
186         g_signal_connect (G_OBJECT (_piano), "note-on", G_CALLBACK (VirtualKeyboardWindow::_note_on_event_handler), this);
187         g_signal_connect (G_OBJECT (_piano), "note-off", G_CALLBACK (VirtualKeyboardWindow::_note_off_event_handler), this);
188
189         update_velocity_settings (0);
190
191         set_keep_above (true);
192         vbox->show_all();
193 }
194
195 VirtualKeyboardWindow::~VirtualKeyboardWindow ()
196 {
197         delete _pianomm;
198         delete _pitch_slider_tooltip;
199 }
200
201 void
202 VirtualKeyboardWindow::set_session (ARDOUR::Session* s)
203 {
204         ArdourWindow::set_session (s);
205
206         if (!_session) {
207                 return;
208         }
209
210         XMLNode* node = _session->instant_xml(X_("VirtualKeyboard"));
211         if (node) {
212                 set_state (*node);
213         }
214 }
215
216 XMLNode&
217 VirtualKeyboardWindow::get_state ()
218 {
219         XMLNode* node = new XMLNode (X_("VirtualKeyboard"));
220         node->set_property (X_("YAxisVelocity"), _yaxis_velocity.get_active());
221         node->set_property (X_("Layout"), _keyboard_layout.get_text ());
222         node->set_property (X_("Channel"), _piano_channel.get_value_as_int ());
223         node->set_property (X_("MinVelocity"), _piano_min_velocity.get_value_as_int ());
224         node->set_property (X_("MaxVelocity"), _piano_max_velocity.get_value_as_int ());
225         node->set_property (X_("KeyVelocity"), _piano_key_velocity.get_value_as_int ());
226         for (int i = 0; i < VKBD_NCTRLS; ++i) {
227                 char buf[16];
228                 sprintf (buf, "CC-%d", i);
229                 node->set_property (buf, _cc_key[i].get_text());
230         }
231         return *node;
232 }
233
234 void
235 VirtualKeyboardWindow::set_state (const XMLNode &root)
236 {
237         if (root.name() != "VirtualKeyboard") {
238                 return;
239         }
240
241         XMLNode const* node = &root;
242
243         std::string layout;
244         if (node->get_property(X_("Layout"), layout)) {
245                 piano_keyboard_set_keyboard_layout (_piano, layout.c_str());
246                 _keyboard_layout.set_active (layout);
247         }
248
249         for (int i = 0; i < VKBD_NCTRLS; ++i) {
250                 char buf[16];
251                 sprintf (buf, "CC-%d", i);
252                 std::string cckey;
253                 if (node->get_property(buf, cckey)) {
254                         _cc_key[i].set_active (cckey);
255                 }
256         }
257
258         bool a;
259         if (node->get_property(X_("YAxisVelocity"), a)) {
260         _yaxis_velocity.set_active (a);
261         }
262
263         int v;
264         if (node->get_property(X_("Channel"), v)) {
265                 _piano_channel.set_value (v);
266         }
267         if (node->get_property(X_("MinVelocity"), v)) {
268                 _piano_min_velocity.set_value (v);
269         }
270         if (node->get_property(X_("MaxVelocity"), v)) {
271                 _piano_max_velocity.set_value (v);
272         }
273         if (node->get_property(X_("KeyVelocity"), v)) {
274                 _piano_key_velocity.set_value (v);
275         }
276
277         update_velocity_settings (0);
278 }
279
280 void
281 VirtualKeyboardWindow::on_unmap ()
282 {
283         ArdourWindow::on_unmap ();
284         ARDOUR_UI::instance()->reset_focus (this);
285 }
286
287 bool
288 VirtualKeyboardWindow::on_key_press_event (GdkEventKey* ev)
289 {
290         return ARDOUR_UI_UTILS::relay_key_press (ev, this);
291 }
292
293 void
294 VirtualKeyboardWindow::select_keyboard_layout (int l)
295 {
296         switch (l) {
297                 default:
298                 case 0:
299                         piano_keyboard_set_keyboard_layout (_piano, "QWERTY");
300                         break;
301                 case 1:
302                         piano_keyboard_set_keyboard_layout (_piano, "QWERTZ");
303                         break;
304                 case 2:
305                         piano_keyboard_set_keyboard_layout (_piano, "AZERTY");
306                         break;
307         }
308 }
309
310 bool
311 VirtualKeyboardWindow::toggle_config (GdkEventButton* ev)
312 {
313         bool a = ! _cfg_display.get_active ();
314         _cfg_display.set_active (a);
315         if (a) {
316                 _cfg_box->show ();
317         } else {
318                 _cfg_box->hide ();
319         }
320         return false;
321 }
322
323 bool
324 VirtualKeyboardWindow::toggle_bankpatch (GdkEventButton*)
325 {
326         bool a = ! _pgm_display.get_active ();
327         _pgm_display.set_active (a);
328         if (a) {
329                 _pgm_box->show ();
330         } else {
331                 _pgm_box->hide ();
332         }
333         return false;
334 }
335
336 bool
337 VirtualKeyboardWindow::toggle_yaxis_velocity (GdkEventButton*)
338 {
339         _yaxis_velocity.set_active (!_yaxis_velocity.get_active ());
340         update_velocity_settings (0);
341         return false;
342 }
343
344 bool
345 VirtualKeyboardWindow::send_panic_message (GdkEventButton*)
346 {
347         uint8_t channel = _piano_channel.get_value_as_int () - 1;
348         uint8_t ev[3];
349         ev[0] = MIDI_CMD_CONTROL | channel;
350         ev[1] = MIDI_CTL_SUSTAIN;
351         ev[2] = 0;
352         _session->vkbd_output_port()->write (ev, 3, 0);
353         ev[1] = MIDI_CTL_ALL_NOTES_OFF;
354         _session->vkbd_output_port()->write (ev, 3, 0);
355         ev[1] = MIDI_CTL_RESET_CONTROLLERS;
356         _session->vkbd_output_port()->write (ev, 3, 0);
357         return false;
358 }
359
360 void
361 VirtualKeyboardWindow::bank_patch ()
362 {
363         int msb = _bank_msb.get_value_as_int ();
364         int lsb = _bank_lsb.get_value_as_int ();
365         int pgm = _patchpgm.get_value_as_int () - 1;
366
367         uint8_t channel = _piano_channel.get_value_as_int () - 1;
368         uint8_t ev[3];
369         ev[0] = MIDI_CMD_CONTROL | channel;
370         ev[1] = MIDI_CTL_MSB_BANK;
371         ev[2] = (msb >> 7) & 0x7f;
372         _session->vkbd_output_port()->write (ev, 3, 0);
373         ev[1] = MIDI_CTL_LSB_BANK | channel;
374         ev[2] = lsb & 0x7f;
375         _session->vkbd_output_port()->write (ev, 3, 0);
376         ev[0] = MIDI_CMD_PGM_CHANGE | channel;
377         ev[1] = pgm & 0x7f;
378         _session->vkbd_output_port()->write (ev, 2, 0);
379 }
380
381 void
382 VirtualKeyboardWindow::update_velocity_settings (int ctrl)
383 {
384         if (_piano_min_velocity.get_value_as_int () > _piano_max_velocity.get_value_as_int ()) {
385                 if (ctrl == 2) {
386                         _piano_min_velocity.set_value (_piano_max_velocity.get_value_as_int ());
387                         return;
388                 } else {
389                         _piano_max_velocity.set_value (_piano_min_velocity.get_value_as_int ());
390                         return;
391                 }
392         }
393
394         if (_yaxis_velocity.get_active ()) {
395                 piano_keyboard_set_velocities (_piano,
396                                 _piano_min_velocity.get_value_as_int (),
397                                 _piano_max_velocity.get_value_as_int (),
398                                 _piano_key_velocity.get_value_as_int ()
399                                 );
400         } else {
401                 piano_keyboard_set_velocities (_piano,
402                                 _piano_key_velocity.get_value_as_int (),
403                                 _piano_key_velocity.get_value_as_int (),
404                                 _piano_key_velocity.get_value_as_int ()
405                                 );
406         }
407         update_sensitivity ();
408 }
409
410 void
411 VirtualKeyboardWindow::update_sensitivity ()
412 {
413         bool c = _yaxis_velocity.get_active ();
414         _piano_min_velocity.set_sensitive (c);
415         _piano_max_velocity.set_sensitive (c);
416 }
417
418 void
419 VirtualKeyboardWindow::pitch_slider_adjusted ()
420 {
421         _pitchbend->set_value (_pitch_adjustment.get_value (), PBD::Controllable::NoGroup);
422         char buf[64];
423         snprintf(buf, sizeof(buf), "Pitchbend: %.0f", _pitch_adjustment.get_value());
424         _pitch_slider_tooltip->set_tip (buf);
425 }
426
427
428 void
429 VirtualKeyboardWindow::note_on_event_handler (int note, int velocity)
430 {
431         _pianomm->grab_focus ();
432         if (!_session) {
433                 return;
434         }
435         uint8_t channel = _piano_channel.get_value_as_int () - 1;
436         uint8_t ev[3];
437         ev[0] = MIDI_CMD_NOTE_ON | channel;
438         ev[1] = note;
439         ev[2] = velocity;
440         _session->vkbd_output_port()->write (ev, 3, 0);
441 }
442
443 void
444 VirtualKeyboardWindow::note_off_event_handler (int note)
445 {
446         if (!_session) {
447                 return;
448         }
449
450         uint8_t channel = _piano_channel.get_value_as_int () - 1;
451         uint8_t ev[3];
452         ev[0] = MIDI_CMD_NOTE_OFF | channel;
453         ev[1] = note;
454         ev[2] = 0;
455         _session->vkbd_output_port()->write (ev, 3, 0);
456 }
457
458 void
459 VirtualKeyboardWindow::control_change_event_handler (int key, int val)
460 {
461         if (!_session) {
462                 return;
463         }
464         assert (key >= 0 && key < VKBD_NCTRLS);
465         int ctrl = PBD::atoi (_cc_key[key].get_text());
466         assert (ctrl > 0 && ctrl < 127);
467         uint8_t channel = _piano_channel.get_value_as_int () - 1;
468         uint8_t ev[3];
469         ev[0] = MIDI_CMD_CONTROL | channel;
470         ev[1] = ctrl;
471         ev[2] = val;
472         _session->vkbd_output_port()->write (ev, 3, 0);
473 }
474
475 void
476 VirtualKeyboardWindow::pitch_bend_event_handler (int val)
477 {
478         if (!_session) {
479                 return;
480         }
481         uint8_t channel = _piano_channel.get_value_as_int () - 1;
482         uint8_t ev[3];
483         ev[0] = MIDI_CMD_BENDER | channel;
484         ev[1] = val & 0x7f;
485         ev[2] = (val >> 7) & 0x7f;
486         _session->vkbd_output_port()->write (ev, 3, 0);
487 }