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