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