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