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