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