radically change Keyboard/Binding API design to disconnect Gtk::Action lookup from...
[ardour.git] / libs / gtkmm2ext / 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
22 #include <algorithm>
23 #include <cerrno>
24 #include <ctype.h>
25
26 #include "pbd/gstdio_compat.h"
27
28 #include <gtkmm/widget.h>
29 #include <gtkmm/window.h>
30 #include <gtkmm/accelmap.h>
31 #include <gdk/gdkkeysyms.h>
32
33 #include "pbd/error.h"
34 #include "pbd/convert.h"
35 #include "pbd/file_utils.h"
36 #include "pbd/search_path.h"
37 #include "pbd/xml++.h"
38 #include "pbd/debug.h"
39 #include "pbd/unwind.h"
40
41 #include "gtkmm2ext/actions.h"
42 #include "gtkmm2ext/bindings.h"
43 #include "gtkmm2ext/keyboard.h"
44 #include "gtkmm2ext/debug.h"
45
46 #include "i18n.h"
47
48 using namespace PBD;
49 using namespace Gtk;
50 using namespace Gtkmm2ext;
51 using namespace std;
52
53 guint Keyboard::edit_but = 3;
54 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
55 guint Keyboard::delete_but = 3;
56 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
57 guint Keyboard::insert_note_but = 1;
58 guint Keyboard::insert_note_mod = GDK_CONTROL_MASK;
59
60 #ifdef GTKOSX
61
62 uint Keyboard::PrimaryModifier = GDK_MOD2_MASK;   // Command
63 guint Keyboard::SecondaryModifier = GDK_CONTROL_MASK; // Control
64 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
65 guint Keyboard::Level4Modifier = GDK_MOD1_MASK; // Alt/Option
66 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;      // Control
67 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
68 guint Keyboard::button2_modifiers = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
69
70 const char* Keyboard::primary_modifier_name() { return _("Command"); }
71 const char* Keyboard::secondary_modifier_name() { return _("Control"); }
72 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
73 const char* Keyboard::level4_modifier_name() { return _("Option"); }
74
75 guint Keyboard::snap_mod = Keyboard::Level4Modifier|Keyboard::TertiaryModifier; // XXX this is probably completely wrong
76 guint Keyboard::snap_delta_mod = Keyboard::Level4Modifier;
77
78 #else
79
80 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
81 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK;  // Alt/Option
82 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK;  // Shift
83 guint Keyboard::Level4Modifier = GDK_MOD4_MASK;     // Mod4/Windows
84 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;
85 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
86 guint Keyboard::button2_modifiers = 0; /* not used */
87
88 const char* Keyboard::primary_modifier_name() { return _("Control"); }
89 const char* Keyboard::secondary_modifier_name() { return _("Alt"); }
90 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
91 const char* Keyboard::level4_modifier_name() { return _("Windows"); }
92
93 guint Keyboard::snap_mod = Keyboard::SecondaryModifier;
94 guint Keyboard::snap_delta_mod = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
95
96 #endif
97
98 guint Keyboard::GainFineScaleModifier = Keyboard::PrimaryModifier;
99 guint Keyboard::GainExtraFineScaleModifier = Keyboard::SecondaryModifier;
100
101 guint Keyboard::ScrollZoomVerticalModifier = Keyboard::SecondaryModifier;
102 guint Keyboard::ScrollZoomHorizontalModifier = Keyboard::PrimaryModifier;
103 guint Keyboard::ScrollHorizontalModifier = Keyboard::TertiaryModifier;
104
105 Keyboard*    Keyboard::_the_keyboard = 0;
106 Gtk::Window* Keyboard::current_window = 0;
107 bool         Keyboard::_some_magic_widget_has_focus = false;
108
109 std::string Keyboard::user_keybindings_path;
110 bool Keyboard::can_save_keybindings = false;
111 bool Keyboard::bindings_changed_after_save_became_legal = false;
112 map<string,string> Keyboard::binding_files;
113 string Keyboard::_current_binding_name;
114 Gtk::Window* Keyboard::pre_dialog_active_window = 0;
115 PBD::Signal0<void> Keyboard::BindingsChanged;
116
117 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
118 GdkModifierType Keyboard::RelevantModifierKeyMask;
119
120 void
121 Keyboard::magic_widget_grab_focus ()
122 {
123         _some_magic_widget_has_focus = true;
124 }
125
126 void
127 Keyboard::magic_widget_drop_focus ()
128 {
129         _some_magic_widget_has_focus = false;
130 }
131
132 bool
133 Keyboard::some_magic_widget_has_focus ()
134 {
135         return _some_magic_widget_has_focus;
136 }
137
138 Keyboard::Keyboard ()
139 {
140         if (_the_keyboard == 0) {
141                 _the_keyboard = this;
142                 _current_binding_name = _("Unknown");
143         }
144
145         RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
146
147         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
148         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
149         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
150         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | Level4Modifier);
151         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
152         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
153
154         gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
155
156 #ifdef __APPLE__
157         /* Remove SUPER,HYPER,META.
158          *
159          * GTK on OS X adds META when Command is pressed for various indefensible reasons, since
160          * it also uses MOD2 to indicate Command. Our code assumes that each
161          * modifier (Primary, Secondary etc.) is represented by a single bit in
162          * the modifier mask, but GTK's (STUPID) design uses two (MOD2 + META)
163          * to represent the Command key. Some discussion about this is here:
164          * https://bugzilla.gnome.org/show_bug.cgi?id=692597 
165          *
166          * We cannot do this until AFTER we told GTK what the default modifier
167          * was, because otherwise it will fail to recognize MOD2-META-<key> as
168          * an accelerator.
169          *
170          * Note that in the tabbed branch, we no longer use GTK accelerators
171          * for functional purposes, so this is as critical for that branch.
172          */
173
174         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~GDK_SUPER_MASK);
175         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~GDK_HYPER_MASK);
176         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~GDK_META_MASK);
177 #endif
178
179         snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
180 }
181
182 Keyboard::~Keyboard ()
183 {
184         gtk_key_snooper_remove (snooper_id);
185 }
186
187 XMLNode&
188 Keyboard::get_state (void)
189 {
190         XMLNode* node = new XMLNode ("Keyboard");
191         char buf[32];
192
193         snprintf (buf, sizeof (buf), "%d", CopyModifier);
194         node->add_property ("copy-modifier", buf);
195         snprintf (buf, sizeof (buf), "%d", edit_but);
196         node->add_property ("edit-button", buf);
197         snprintf (buf, sizeof (buf), "%d", edit_mod);
198         node->add_property ("edit-modifier", buf);
199         snprintf (buf, sizeof (buf), "%d", delete_but);
200         node->add_property ("delete-button", buf);
201         snprintf (buf, sizeof (buf), "%d", delete_mod);
202         node->add_property ("delete-modifier", buf);
203         snprintf (buf, sizeof (buf), "%d", snap_mod);
204         node->add_property ("snap-modifier", buf);
205         snprintf (buf, sizeof (buf), "%d", snap_delta_mod);
206         node->add_property ("snap-delta-modifier", buf);
207         snprintf (buf, sizeof (buf), "%d", insert_note_but);
208         node->add_property ("insert-note-button", buf);
209         snprintf (buf, sizeof (buf), "%d", insert_note_mod);
210         node->add_property ("insert-note-modifier", buf);
211
212         return *node;
213 }
214
215 int
216 Keyboard::set_state (const XMLNode& node, int /*version*/)
217 {
218         const XMLProperty* prop;
219
220         if ((prop = node.property ("copy-modifier")) != 0) {
221                 sscanf (prop->value().c_str(), "%d", &CopyModifier);
222         }
223
224         if ((prop = node.property ("edit-button")) != 0) {
225                 sscanf (prop->value().c_str(), "%d", &edit_but);
226         }
227
228         if ((prop = node.property ("edit-modifier")) != 0) {
229                 sscanf (prop->value().c_str(), "%d", &edit_mod);
230         }
231
232         if ((prop = node.property ("delete-button")) != 0) {
233                 sscanf (prop->value().c_str(), "%d", &delete_but);
234         }
235
236         if ((prop = node.property ("delete-modifier")) != 0) {
237                 sscanf (prop->value().c_str(), "%d", &delete_mod);
238         }
239
240         if ((prop = node.property ("snap-modifier")) != 0) {
241                 sscanf (prop->value().c_str(), "%d", &snap_mod);
242         }
243
244         if ((prop = node.property ("snap-delta-modifier")) != 0) {
245                 sscanf (prop->value().c_str(), "%d", &snap_delta_mod);
246         }
247
248         if ((prop = node.property ("insert-note-button")) != 0) {
249                 sscanf (prop->value().c_str(), "%d", &insert_note_but);
250         }
251
252         if ((prop = node.property ("insert-note-modifier")) != 0) {
253                 sscanf (prop->value().c_str(), "%d", &insert_note_mod);
254         }
255
256         return 0;
257 }
258
259 gint
260 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
261 {
262         return ((Keyboard *) data)->snooper (widget, event);
263 }
264
265 static string
266 show_gdk_event_state (int state)
267 {
268         string s;
269         if (state & GDK_SHIFT_MASK) {
270                 s += "+SHIFT";
271         }
272         if (state & GDK_LOCK_MASK) {
273                 s += "+LOCK";
274         }
275         if (state & GDK_CONTROL_MASK) {
276                 s += "+CONTROL";
277         }
278         if (state & GDK_MOD1_MASK) {
279                 s += "+MOD1";
280         }
281         if (state & GDK_MOD2_MASK) {
282                 s += "+MOD2";
283         }
284         if (state & GDK_MOD3_MASK) {
285                 s += "+MOD3";
286         }
287         if (state & GDK_MOD4_MASK) {
288                 s += "+MOD4";
289         }
290         if (state & GDK_MOD5_MASK) {
291                 s += "+MOD5";
292         }
293         if (state & GDK_BUTTON1_MASK) {
294                 s += "+BUTTON1";
295         }
296         if (state & GDK_BUTTON2_MASK) {
297                 s += "+BUTTON2";
298         }
299         if (state & GDK_BUTTON3_MASK) {
300                 s += "+BUTTON3";
301         }
302         if (state & GDK_BUTTON4_MASK) {
303                 s += "+BUTTON4";
304         }
305         if (state & GDK_BUTTON5_MASK) {
306                 s += "+BUTTON5";
307         }
308         if (state & GDK_SUPER_MASK) {
309                 s += "+SUPER";
310         }
311         if (state & GDK_HYPER_MASK) {
312                 s += "+HYPER";
313         }
314         if (state & GDK_META_MASK) {
315                 s += "+META";
316         }
317         if (state & GDK_RELEASE_MASK) {
318                 s += "+RELEASE";
319         }
320
321         return s;
322 }
323
324 gint
325 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
326 {
327         uint32_t keyval;
328         bool ret = false;
329
330         DEBUG_TRACE (
331                 DEBUG::Keyboard,
332                 string_compose (
333                         "Snoop widget %1 name: [%6] key %2 [%8] type %3 state %4 [%7] magic %5\n",
334                         widget, event->keyval, event->type, event->state, _some_magic_widget_has_focus,
335                         gtk_widget_get_name (widget), show_gdk_event_state (event->state), gdk_keyval_name (event->keyval)
336                         )
337                 );
338
339         if (event->keyval == GDK_Shift_R) {
340                 keyval = GDK_Shift_L;
341
342         } else if (event->keyval == GDK_Control_R) {
343                 keyval = GDK_Control_L;
344
345         } else {
346                 keyval = event->keyval;
347         }
348
349         if (event->state & ScrollZoomVerticalModifier) {
350                 /* There is a special and rather hacky situation in Editor which makes
351                    it useful to know when the modifier key for vertical zoom has been
352                    released, so emit a signal here (see Editor::_stepping_axis_view).
353                    Note that the state bit for the modifier key is set for the key-up
354                    event when the modifier is released, but not the key-down when it
355                    is pressed, so we get here on key-up, which is what we want.
356                 */
357                 ZoomVerticalModifierReleased (); /* EMIT SIGNAL */
358         }
359
360         if (event->type == GDK_KEY_PRESS) {
361
362                 if (find (state.begin(), state.end(), keyval) == state.end()) {
363                         state.push_back (keyval);
364                         sort (state.begin(), state.end());
365
366                 } else {
367
368                         /* key is already down. if its also used for release,
369                            prevent auto-repeat events.
370                         */
371
372 #if 0
373                         /* August 2015: we don't have any release bindings
374                          */
375                         
376                         for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
377
378                                 const AccelKey& ak (k->first);
379
380                                 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
381                                         DEBUG_TRACE (DEBUG::Keyboard, "Suppress auto repeat\n");
382                                         ret = true;
383                                         break;
384                                 }
385                         }
386 #endif
387                 }
388         }
389
390         /* Special keys that we want to handle in
391            any dialog, no matter whether it uses
392            the regular set of accelerators or not
393         */
394
395         if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
396                 switch (event->keyval) {
397                 case GDK_w:
398                         close_current_dialog ();
399                         ret = true;
400                         break;
401                 }
402         }
403
404         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("snooper returns %1\n", ret));
405
406         return ret;
407 }
408
409 void
410 Keyboard::close_current_dialog ()
411 {
412         if (current_window) {
413                 current_window->hide ();
414                 current_window = 0;
415
416                 if (pre_dialog_active_window) {
417                         pre_dialog_active_window->present ();
418                         pre_dialog_active_window = 0;
419                 }
420         }
421 }
422
423 bool
424 Keyboard::catch_user_event_for_pre_dialog_focus (GdkEvent* ev, Gtk::Window* w)
425 {
426         switch (ev->type) {
427         case GDK_BUTTON_PRESS:
428         case GDK_BUTTON_RELEASE:
429         case GDK_KEY_PRESS:
430         case GDK_KEY_RELEASE:
431                 pre_dialog_active_window = w;
432                 break;
433
434         case GDK_FOCUS_CHANGE:
435                 if (ev->focus_change.in) {
436                         pre_dialog_active_window = w;
437                 }
438                 break;
439
440         default:
441                 break;
442         }
443         return false;
444 }
445
446 bool
447 Keyboard::key_is_down (uint32_t keyval)
448 {
449         return find (state.begin(), state.end(), keyval) != state.end();
450 }
451
452 bool
453 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
454 {
455         current_window = win;
456         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Entering window, title = %1\n", win->get_title()));
457         return false;
458 }
459
460 bool
461 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
462 {
463         if (ev) {
464                 switch (ev->detail) {
465                 case GDK_NOTIFY_INFERIOR:
466                         DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
467                         break;
468
469                 case GDK_NOTIFY_VIRTUAL:
470                         DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
471                         /* fallthru */
472
473                 default:
474                         DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
475                         DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
476                         state.clear ();
477                         current_window = 0;
478                 }
479         } else {
480                 current_window = 0;
481         }
482
483         return false;
484 }
485
486 bool
487 Keyboard::focus_in_window (GdkEventFocus *, Gtk::Window* win)
488 {
489         current_window = win;
490         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Focusing in window, title = %1\n", win->get_title()));
491         return false;
492 }
493
494 bool
495 Keyboard::focus_out_window (GdkEventFocus * ev, Gtk::Window* win)
496 {
497         if (ev) {
498                 state.clear ();
499                 current_window = 0;
500         }  else {
501                 current_window = 0;
502         }
503
504         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Foucusing out window, title = %1\n", win->get_title()));
505
506         return false;
507 }
508
509 void
510 Keyboard::set_edit_button (guint but)
511 {
512         edit_but = but;
513 }
514
515 void
516 Keyboard::set_edit_modifier (guint mod)
517 {
518         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
519         edit_mod = mod;
520         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
521 }
522
523 void
524 Keyboard::set_delete_button (guint but)
525 {
526         delete_but = but;
527 }
528
529 void
530 Keyboard::set_delete_modifier (guint mod)
531 {
532         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
533         delete_mod = mod;
534         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
535 }
536
537 void
538 Keyboard::set_insert_note_button (guint but)
539 {
540         insert_note_but = but;
541 }
542
543 void
544 Keyboard::set_insert_note_modifier (guint mod)
545 {
546         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~insert_note_mod);
547         insert_note_mod = mod;
548         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | insert_note_mod);
549 }
550
551
552 void
553 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
554 {
555         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
556         var = newval;
557         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
558 }
559
560 void
561 Keyboard::set_snap_modifier (guint mod)
562 {
563         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
564         snap_mod = mod;
565         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
566 }
567
568 void
569 Keyboard::set_snap_delta_modifier (guint mod)
570 {
571         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_delta_mod);
572         snap_delta_mod = mod;
573         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_delta_mod);
574 }
575
576 bool
577 Keyboard::is_edit_event (GdkEventButton *ev)
578 {
579         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
580                 (ev->button == Keyboard::edit_button()) &&
581                 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
582 }
583
584 bool
585 Keyboard::is_insert_note_event (GdkEventButton *ev)
586 {
587         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
588                 (ev->button == Keyboard::insert_note_button()) &&
589                 ((ev->state & RelevantModifierKeyMask) == Keyboard::insert_note_modifier());
590 }
591
592 bool
593 Keyboard::is_button2_event (GdkEventButton* ev)
594 {
595 #ifdef GTKOSX
596         return (ev->button == 2) ||
597                 ((ev->button == 1) &&
598                  ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
599 #else
600         return ev->button == 2;
601 #endif
602 }
603
604 bool
605 Keyboard::is_delete_event (GdkEventButton *ev)
606 {
607         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
608                 (ev->button == Keyboard::delete_button()) &&
609                 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
610 }
611
612 bool
613 Keyboard::is_context_menu_event (GdkEventButton *ev)
614 {
615         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
616                 (ev->button == 3) &&
617                 ((ev->state & RelevantModifierKeyMask) == 0);
618 }
619
620 bool
621 Keyboard::no_modifiers_active (guint state)
622 {
623         return (state & RelevantModifierKeyMask) == 0;
624 }
625
626 bool
627 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
628 {
629         return (state & mask) == (guint) mask;
630 }
631
632 bool
633 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
634 {
635         return (state & RelevantModifierKeyMask) == (guint) mask;
636 }
637
638 void
639 Keyboard::keybindings_changed ()
640 {
641         if (Keyboard::can_save_keybindings) {
642                 Keyboard::bindings_changed_after_save_became_legal = true;
643         }
644
645         Keyboard::save_keybindings ();
646         BindingsChanged (); /* EMIT SIGNAL */
647 }
648
649 void
650 Keyboard::set_can_save_keybindings (bool yn)
651 {
652         can_save_keybindings = yn;
653 }
654
655 void
656 Keyboard::save_keybindings ()
657 {
658         if (can_save_keybindings && bindings_changed_after_save_became_legal) {
659                 /* Call to specific implementation to save bindings to path */
660                 store_keybindings (user_keybindings_path);
661         }
662 }
663
664 bool
665 Keyboard::load_keybindings (string const & path)
666 {
667         try {
668                 info << "Loading bindings from " << path << endl;
669
670                 /* Call to specific implementation to load bindings from path */
671                 read_keybindings (path);
672                 
673                 _current_binding_name = _("Unknown");
674
675                 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
676                         if (path == x->second) {
677                                 _current_binding_name = x->first;
678                                 break;
679                         }
680                 }
681
682
683         } catch (...) {
684                 error << string_compose (_("key bindings file not found at \"%2\" or contains errors."), path)
685                       << endmsg;
686                 return false;
687         }
688
689         return true;
690 }
691
692 int
693 Keyboard::read_keybindings (string const & path)
694 {
695         XMLTree tree;
696
697         if (!tree.read (path.c_str())) {
698                 return -1;
699         }
700
701         /* toplevel node is "BindingSet; children are "Bindings" */
702         
703         XMLNodeList const& children = tree.root()->children();
704
705         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
706                 if ((*i)->name() == X_("Bindings")) {
707                         XMLProperty const* name = (*i)->property (X_("name"));
708                         if (!name) {
709                                 warning << _("Keyboard binding found without a name") << endmsg;
710                                 continue;
711                         }
712                         
713                         Bindings* b = new Bindings (name->value());
714                         b->load (**i);
715                 }
716         }
717         
718         return 0;
719 }
720
721 int
722 Keyboard::store_keybindings (string const & path)
723 {
724         XMLNode* node = new XMLNode (X_("BindingSet"));
725         XMLNode* bnode;
726         int ret = 0;
727
728         for (list<Bindings*>::const_iterator b = Bindings::bindings.begin(); b != Bindings::bindings.end(); ++b) {
729                 bnode = new XMLNode (X_("Bindings"));
730                 bnode->add_property (X_("name"), (*b)->name());
731                 (*b)->save (*bnode);
732                 node->add_child_nocopy (*bnode);
733         }
734
735         XMLTree tree;
736         tree.set_root (node); /* tree now owns root and will delete it */
737
738         if (!tree.write (path)) {
739                 error << string_compose (_("Cannot save key bindings to %1"), path) << endmsg;
740                 ret = -1;
741         }
742         
743         return ret;
744 }
745
746 int
747 Keyboard::reset_bindings ()
748 {
749         if (Glib::file_test (user_keybindings_path,  Glib::FILE_TEST_EXISTS)) {
750
751                 string new_path = user_keybindings_path;
752                 new_path += ".old";
753
754                 if (::g_rename (user_keybindings_path.c_str(), new_path.c_str())) {
755                         error << string_compose (_("Cannot rename your own keybinding file (%1)"), strerror (errno)) << endmsg;
756                         return -1;
757                 }
758         }
759
760         {
761                 PBD::Unwinder<bool> uw (can_save_keybindings, false);
762                 setup_keybindings ();
763                 Bindings::associate_all ();
764         }
765
766         return 0;
767 }