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