Ensure that the newly selected track is visible on the canvas when scrolling through...
[ardour.git] / gtk2_ardour / keyboard.cc
1 /*
2     Copyright (C) 2001 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 <vector>
21 #include <ardour/ardour.h>
22
23 #include "ardour_ui.h"
24
25 #include <algorithm>
26 #include <fstream>
27 #include <iostream>
28
29 #include <ctype.h>
30
31 #include <gtkmm/accelmap.h>
32
33 #include <gdk/gdkkeysyms.h>
34 #include <pbd/error.h>
35
36 #include "keyboard.h"
37 #include "gui_thread.h"
38 #include "opts.h"
39 #include "actions.h"
40
41 #include "i18n.h"
42
43 using namespace PBD;
44 using namespace ARDOUR;
45 using namespace Gtk;
46 using namespace std;
47
48 #define KBD_DEBUG 0
49 bool debug_keyboard = false;
50
51 guint Keyboard::edit_but = 3;
52 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
53 guint Keyboard::delete_but = 3;
54 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
55 guint Keyboard::snap_mod = GDK_MOD3_MASK;
56
57 #ifdef GTKOSX
58 guint Keyboard::PrimaryModifier = GDK_META_MASK;   // Command
59 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK; // Alt/Option
60 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
61 guint Keyboard::Level4Modifier = GDK_CONTROL_MASK; // Control
62 guint Keyboard::CopyModifier = GDK_MOD1_MASK;      // Alt/Option
63 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;   
64 #else
65 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
66 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK;  // Alt/Option
67 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK;  // Shift
68 guint Keyboard::Level4Modifier = GDK_MOD4_MASK;     // Mod4/Windows
69 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;    
70 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;   
71 #endif
72
73 Keyboard*    Keyboard::_the_keyboard = 0;
74 Gtk::Window* Keyboard::current_window = 0;
75 bool         Keyboard::_some_magic_widget_has_focus = false;
76
77 std::string Keyboard::user_keybindings_path;
78 bool Keyboard::can_save_keybindings = false;
79 bool Keyboard::bindings_changed_after_save_became_legal = false;
80 map<string,string> Keyboard::binding_files;
81 string Keyboard::_current_binding_name = _("Unknown");
82 map<AccelKey,pair<string,string>,Keyboard::AccelKeyLess> Keyboard::release_keys;
83
84 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
85
86 GdkModifierType Keyboard::RelevantModifierKeyMask;
87
88 void
89 Keyboard::magic_widget_grab_focus () 
90 {
91         _some_magic_widget_has_focus = true;
92 }
93
94 void
95 Keyboard::magic_widget_drop_focus ()
96 {
97         _some_magic_widget_has_focus = false;
98 }
99
100 bool
101 Keyboard::some_magic_widget_has_focus ()
102 {
103         return _some_magic_widget_has_focus;
104 }
105
106 Keyboard::Keyboard ()
107 {
108         if (_the_keyboard == 0) {
109                 _the_keyboard = this;
110         }
111
112         RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
113
114         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
115         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
116         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
117         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | Level4Modifier);
118         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
119         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
120
121         gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
122
123         snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
124
125         XMLNode* node = ARDOUR_UI::instance()->keyboard_settings();
126         set_state (*node);
127 }
128
129 Keyboard::~Keyboard ()
130 {
131         gtk_key_snooper_remove (snooper_id);
132 }
133
134 XMLNode& 
135 Keyboard::get_state (void)
136 {
137         XMLNode* node = new XMLNode ("Keyboard");
138         char buf[32];
139
140         snprintf (buf, sizeof (buf), "%d", edit_but);
141         node->add_property ("edit-button", buf);
142         snprintf (buf, sizeof (buf), "%d", edit_mod);
143         node->add_property ("edit-modifier", buf);
144         snprintf (buf, sizeof (buf), "%d", delete_but);
145         node->add_property ("delete-button", buf);
146         snprintf (buf, sizeof (buf), "%d", delete_mod);
147         node->add_property ("delete-modifier", buf);
148         snprintf (buf, sizeof (buf), "%d", snap_mod);
149         node->add_property ("snap-modifier", buf);
150
151         return *node;
152 }
153
154 int 
155 Keyboard::set_state (const XMLNode& node)
156 {
157         const XMLProperty* prop;
158
159         if ((prop = node.property ("edit-button")) != 0) {
160                 sscanf (prop->value().c_str(), "%d", &edit_but);
161         } 
162
163         if ((prop = node.property ("edit-modifier")) != 0) {
164                 sscanf (prop->value().c_str(), "%d", &edit_mod);
165         } 
166
167         if ((prop = node.property ("delete-button")) != 0) {
168                 sscanf (prop->value().c_str(), "%d", &delete_but);
169         } 
170
171         if ((prop = node.property ("delete-modifier")) != 0) {
172                 sscanf (prop->value().c_str(), "%d", &delete_mod);
173         } 
174
175         if ((prop = node.property ("snap-modifier")) != 0) {
176                 sscanf (prop->value().c_str(), "%d", &snap_mod);
177         } 
178
179         return 0;
180 }
181
182 gint
183 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
184 {
185         return ((Keyboard *) data)->snooper (widget, event);
186 }
187
188 gint
189 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
190 {
191         uint32_t keyval;
192         bool ret = false;
193
194 #if 0
195         cerr << "snoop widget " << widget << " key " << event->keyval << " type: " << event->type 
196              << " state " << std::hex << event->state << std::dec
197              << endl;
198 #endif
199
200 #if KBD_DEBUG
201         if (debug_keyboard) {
202                 cerr << "snoop widget " << widget << " key " << event->keyval << " type: " << event->type 
203                      << endl;
204         }
205 #endif
206
207         if (event->keyval == GDK_Shift_R) {
208                 keyval = GDK_Shift_L;
209
210         } else  if (event->keyval == GDK_Control_R) {
211                 keyval = GDK_Control_L;
212
213         } else {
214                 keyval = event->keyval;
215         }
216                 
217         if (event->type == GDK_KEY_PRESS) {
218
219                 if (find (state.begin(), state.end(), keyval) == state.end()) {
220                         state.push_back (keyval);
221                         sort (state.begin(), state.end());
222
223                 } else {
224
225                         /* key is already down. if its also used for release,
226                            prevent auto-repeat events.
227                         */
228
229                         for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
230
231                                 const AccelKey& ak (k->first);
232                                 
233                                 if (keyval == ak.get_key() && (Gdk::ModifierType)(event->state | Gdk::RELEASE_MASK) == ak.get_mod()) {
234                                         ret = true;
235                                         break;
236                                 }
237                         }
238                 }
239
240         } else if (event->type == GDK_KEY_RELEASE) {
241
242                 State::iterator i;
243                 
244                 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
245                         state.erase (i);
246                         sort (state.begin(), state.end());
247                 } 
248
249                 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
250
251                         const AccelKey& ak (k->first);
252                         two_strings ts (k->second);
253
254                         if (keyval == ak.get_key() && (Gdk::ModifierType)(event->state | Gdk::RELEASE_MASK) == ak.get_mod()) {
255                                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (ts.first.c_str(), ts.second.c_str());
256                                 if (act) {
257                                         act->activate();
258                                         ret = true;
259                                 }
260                                 break;
261                         }
262                 }
263         }
264
265         /* Special keys that we want to handle in
266            any dialog, no matter whether it uses
267            the regular set of accelerators or not
268         */
269
270         if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
271                 switch (event->keyval) {
272                 case GDK_w:
273                         if (current_window) {
274                                 current_window->hide ();
275                                 current_window = 0;
276                                 ret = true;
277                         }
278                         break;
279                 }
280         }
281
282         return ret;
283 }
284
285 bool
286 Keyboard::key_is_down (uint32_t keyval)
287 {
288         return find (state.begin(), state.end(), keyval) != state.end();
289 }
290
291 bool
292 Keyboard::enter_window (GdkEventCrossing *ev, Gtk::Window* win)
293 {
294         current_window = win;
295         return false;
296 }
297
298 bool
299 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* win)
300 {
301         switch (ev->detail) {
302         case GDK_NOTIFY_INFERIOR:
303                 if (debug_keyboard) {
304                         cerr << "INFERIOR crossing ... out\n";
305                 }
306                 break;
307
308         case GDK_NOTIFY_VIRTUAL:
309                 if (debug_keyboard) {
310                         cerr << "VIRTUAL crossing ... out\n";
311                 }
312                 /* fallthru */
313
314         default:
315                 if (debug_keyboard) {
316                         cerr << "REAL CROSSING ... out\n";
317                         cerr << "clearing current target\n";
318                 }
319                 state.clear ();
320                 current_window = 0;
321         }
322
323         return false;
324 }
325
326 void
327 Keyboard::set_edit_button (guint but)
328 {
329         edit_but = but;
330 }
331
332 void
333 Keyboard::set_edit_modifier (guint mod)
334 {
335         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
336         edit_mod = mod;
337         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
338 }
339
340 void
341 Keyboard::set_delete_button (guint but)
342 {
343         delete_but = but;
344 }
345
346 void
347 Keyboard::set_delete_modifier (guint mod)
348 {
349         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
350         delete_mod = mod;
351         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
352 }
353
354 void
355 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
356 {
357         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
358         var = newval;
359         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
360 }
361
362 void
363 Keyboard::set_snap_modifier (guint mod)
364 {
365         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
366         snap_mod = mod;
367         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
368 }
369
370 bool
371 Keyboard::is_edit_event (GdkEventButton *ev)
372 {
373         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) && 
374                 (ev->button == Keyboard::edit_button()) && 
375                 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
376 }
377
378 bool
379 Keyboard::is_delete_event (GdkEventButton *ev)
380 {
381         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) && 
382                 (ev->button == Keyboard::delete_button()) && 
383                 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
384 }
385
386 bool
387 Keyboard::is_context_menu_event (GdkEventButton *ev)
388 {
389         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) && 
390                 (ev->button == 3) && 
391                 ((ev->state & RelevantModifierKeyMask) == 0);
392 }
393
394 bool 
395 Keyboard::no_modifiers_active (guint state)
396 {
397         return (state & RelevantModifierKeyMask) == 0;
398 }
399
400 bool
401 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
402 {
403         return (state & mask) == (guint) mask;
404 }
405
406 bool
407 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
408 {
409         return (state & RelevantModifierKeyMask) == (guint) mask;
410 }
411
412 Selection::Operation
413 Keyboard::selection_type (guint state)
414 {
415         /* note that there is no modifier for "Add" */
416
417         if (modifier_state_equals (state, RangeSelectModifier)) {
418                 return Selection::Extend;
419         } else if (modifier_state_equals (state, PrimaryModifier)) {
420                 return Selection::Toggle;
421         } else {
422                 return Selection::Set;
423         }
424 }
425
426
427 static void 
428 accel_map_changed (GtkAccelMap* map,
429                    gchar* path,
430                    guint  key,
431                    GdkModifierType mod,
432                    gpointer arg)
433 {
434         Keyboard::keybindings_changed ();
435 }
436
437 void
438 Keyboard::keybindings_changed ()
439 {
440         if (Keyboard::can_save_keybindings) {
441                 Keyboard::bindings_changed_after_save_became_legal = true;
442         }
443
444         Keyboard::save_keybindings ();
445 }
446
447 void
448 Keyboard::set_can_save_keybindings (bool yn)
449 {
450         can_save_keybindings = yn;
451 }
452
453 void
454 Keyboard::save_keybindings ()
455 {
456         if (can_save_keybindings && bindings_changed_after_save_became_legal) {
457                 Gtk::AccelMap::save (user_keybindings_path);
458         } 
459 }
460
461 void
462 Keyboard::setup_keybindings ()
463 {
464         using namespace ARDOUR_COMMAND_LINE;
465         std::string default_bindings = "mnemonic-us.bindings";
466         std::string path;
467         vector<string> strs;
468
469         binding_files.clear ();
470
471         ARDOUR::find_bindings_files (binding_files);
472
473         /* set up the per-user bindings path */
474         
475         strs.push_back (Glib::get_home_dir());
476         strs.push_back (".ardour2");
477         strs.push_back ("ardour.bindings");
478
479         user_keybindings_path = Glib::build_filename (strs);
480
481         if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
482                 std::pair<string,string> newpair;
483                 newpair.first = _("your own");
484                 newpair.second = user_keybindings_path;
485                 binding_files.insert (newpair);
486         }
487
488         /* check to see if they gave a style name ("SAE", "ergonomic") or
489            an actual filename (*.bindings)
490         */
491
492         if (!keybindings_path.empty() && keybindings_path.find (".bindings") == string::npos) {
493                 
494                 // just a style name - allow user to
495                 // specify the layout type. 
496                 
497                 char* layout;
498                 
499                 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
500                         
501                         /* user-specified keyboard layout */
502                         
503                         keybindings_path += '-';
504                         keybindings_path += layout;
505
506                 } else {
507
508                         /* default to US/ANSI - we have to pick something */
509
510                         keybindings_path += "-us";
511                 }
512                 
513                 keybindings_path += ".bindings";
514         } 
515
516         if (keybindings_path.empty()) {
517
518                 /* no path or binding name given: check the user one first */
519
520                 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
521                         
522                         keybindings_path = "";
523
524                 } else {
525                         
526                         keybindings_path = user_keybindings_path;
527                 }
528         } 
529
530         /* if we still don't have a path at this point, use the default */
531
532         if (keybindings_path.empty()) {
533                 keybindings_path = default_bindings;
534         }
535
536         while (true) {
537
538                 if (!Glib::path_is_absolute (keybindings_path)) {
539                         
540                         /* not absolute - look in the usual places */
541                         
542                         path = find_config_file (keybindings_path);
543                         
544                         if (path.empty()) {
545                                 
546                                 if (keybindings_path == default_bindings) {
547                                         error << _("Default keybindings not found - Ardour will be hard to use!") << endmsg;
548                                         return;
549                                 } else {
550                                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"), 
551                                                                    keybindings_path)
552                                                 << endmsg;
553                                         keybindings_path = default_bindings;
554                                 }
555
556                         } else {
557
558                                 /* use it */
559
560                                 keybindings_path = path;
561                                 break;
562                                 
563                         }
564
565                 } else {
566                         
567                         /* path is absolute already */
568
569                         if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
570                                 if (keybindings_path == default_bindings) {
571                                         error << _("Default keybindings not found - Ardour will be hard to use!") << endmsg;
572                                         return;
573                                 } else {
574                                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"), 
575                                                                    keybindings_path)
576                                                 << endmsg;
577                                         keybindings_path = default_bindings;
578                                 }
579
580                         } else {
581                                 break;
582                         }
583                 }
584         }
585
586         load_keybindings (keybindings_path);
587
588         /* catch changes */
589
590         GtkAccelMap* accelmap = gtk_accel_map_get();
591         g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, 0);
592 }
593
594 bool
595 Keyboard::load_keybindings (string path)
596 {
597         try {
598                 cerr << "loading bindings from " << path << endl;
599
600                 Gtk::AccelMap::load (path);
601
602                 _current_binding_name = _("Unknown");
603
604                 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
605                         if (path == x->second) {
606                                 _current_binding_name = x->first;
607                                 break;
608                         }
609                 }
610
611
612         } catch (...) {
613                 error << string_compose (_("Ardour key bindings file not found at \"%1\" or contains errors."), path)
614                       << endmsg;
615                 return false;
616         }
617
618         /* now find all release-driven bindings */
619
620         vector<string> groups;
621         vector<string> names;
622         vector<AccelKey> bindings;
623         
624         ActionManager::get_all_actions (groups, names, bindings);
625         
626         vector<string>::iterator g;
627         vector<AccelKey>::iterator b;
628         vector<string>::iterator n;
629
630         release_keys.clear ();
631
632         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
633                 if ((*b).get_mod() & Gdk::RELEASE_MASK) {
634                         release_keys.insert (pair<AccelKey,two_strings> (*b, two_strings (*g, *n)));
635                 }
636         }
637
638         return true;
639 }
640
641