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