better system for managing kbd focus after cmd-w closing a dialog.
[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/keyboard.h"
42 #include "gtkmm2ext/actions.h"
43 #include "gtkmm2ext/debug.h"
44
45 #include "i18n.h"
46
47 using namespace PBD;
48 using namespace Gtk;
49 using namespace Gtkmm2ext;
50 using namespace std;
51
52 guint Keyboard::edit_but = 3;
53 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
54 guint Keyboard::delete_but = 3;
55 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
56 guint Keyboard::insert_note_but = 1;
57 guint Keyboard::insert_note_mod = GDK_CONTROL_MASK;
58
59 #ifdef GTKOSX
60
61 uint Keyboard::PrimaryModifier = GDK_MOD2_MASK;   // Command
62 guint Keyboard::SecondaryModifier = GDK_CONTROL_MASK; // Control
63 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
64 guint Keyboard::Level4Modifier = GDK_MOD1_MASK; // Alt/Option
65 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;      // Control
66 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
67 guint Keyboard::button2_modifiers = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
68
69 const char* Keyboard::primary_modifier_name() { return _("Command"); }
70 const char* Keyboard::secondary_modifier_name() { return _("Control"); }
71 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
72 const char* Keyboard::level4_modifier_name() { return _("Option"); }
73
74 guint Keyboard::snap_mod = Keyboard::Level4Modifier|Keyboard::TertiaryModifier; // XXX this is probably completely wrong
75 guint Keyboard::snap_delta_mod = Keyboard::Level4Modifier;
76
77 #else
78
79 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
80 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK;  // Alt/Option
81 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK;  // Shift
82 guint Keyboard::Level4Modifier = GDK_MOD4_MASK;     // Mod4/Windows
83 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;
84 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
85 guint Keyboard::button2_modifiers = 0; /* not used */
86
87 const char* Keyboard::primary_modifier_name() { return _("Control"); }
88 const char* Keyboard::secondary_modifier_name() { return _("Alt"); }
89 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
90 const char* Keyboard::level4_modifier_name() { return _("Windows"); }
91
92 guint Keyboard::snap_mod = Keyboard::SecondaryModifier;
93 guint Keyboard::snap_delta_mod = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
94
95 #endif
96
97 guint Keyboard::GainFineScaleModifier = Keyboard::PrimaryModifier;
98 guint Keyboard::GainExtraFineScaleModifier = Keyboard::SecondaryModifier;
99
100 guint Keyboard::ScrollZoomVerticalModifier = Keyboard::SecondaryModifier;
101 guint Keyboard::ScrollZoomHorizontalModifier = Keyboard::PrimaryModifier;
102 guint Keyboard::ScrollHorizontalModifier = Keyboard::TertiaryModifier;
103
104 Keyboard*    Keyboard::_the_keyboard = 0;
105 Gtk::Window* Keyboard::current_window = 0;
106 bool         Keyboard::_some_magic_widget_has_focus = false;
107
108 std::string Keyboard::user_keybindings_path;
109 bool Keyboard::can_save_keybindings = false;
110 bool Keyboard::bindings_changed_after_save_became_legal = false;
111 map<string,string> Keyboard::binding_files;
112 string Keyboard::_current_binding_name;
113 map<AccelKey,pair<string,string>,Keyboard::AccelKeyLess> Keyboard::release_keys;
114 Gtk::Window* Keyboard::pre_dialog_active_window = 0;
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                         for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
372
373                                 const AccelKey& ak (k->first);
374
375                                 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
376                                         DEBUG_TRACE (DEBUG::Keyboard, "Suppress auto repeat\n");
377                                         ret = true;
378                                         break;
379                                 }
380                         }
381                 }
382
383         } else if (event->type == GDK_KEY_RELEASE) {
384
385                 State::iterator i;
386
387                 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
388                         state.erase (i);
389                         sort (state.begin(), state.end());
390                 }
391
392                 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
393
394                         const AccelKey& ak (k->first);
395                         two_strings ts (k->second);
396
397                         if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
398                                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (ts.first.c_str(), ts.second.c_str());
399                                 if (act) {
400                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Activate %1 %2\n", ts.first, ts.second));
401                                         act->activate();
402                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Use repeat, suppress other\n", ts.first, ts.second));
403                                         ret = true;
404                                 }
405                                 break;
406                         }
407                 }
408         }
409
410         /* Special keys that we want to handle in
411            any dialog, no matter whether it uses
412            the regular set of accelerators or not
413         */
414
415         if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
416                 switch (event->keyval) {
417                 case GDK_w:
418                         close_current_dialog ();
419                         ret = true;
420                         break;
421                 }
422         }
423
424         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("snooper returns %1\n", ret));
425
426         return ret;
427 }
428
429 void
430 Keyboard::close_current_dialog ()
431 {
432         if (current_window) {
433                 current_window->hide ();
434                 current_window = 0;
435
436                 if (pre_dialog_active_window) {
437                         pre_dialog_active_window->present ();
438                         pre_dialog_active_window = 0;
439                 }
440         }
441 }
442
443 bool
444 Keyboard::catch_user_event_for_pre_dialog_focus (GdkEvent* ev, Gtk::Window* w)
445 {
446         switch (ev->type) {
447         case GDK_BUTTON_PRESS:
448         case GDK_BUTTON_RELEASE:
449         case GDK_KEY_PRESS:
450         case GDK_KEY_RELEASE:
451                 pre_dialog_active_window = w;
452                 break;
453
454         case GDK_FOCUS_CHANGE:
455                 if (ev->focus_change.in) {
456                         pre_dialog_active_window = w;
457                 }
458                 break;
459
460         default:
461                 break;
462         }
463         return false;
464 }
465
466 bool
467 Keyboard::key_is_down (uint32_t keyval)
468 {
469         return find (state.begin(), state.end(), keyval) != state.end();
470 }
471
472 bool
473 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
474 {
475         current_window = win;
476         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Entering window, title = %1\n", win->get_title()));
477         return false;
478 }
479
480 bool
481 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
482 {
483         if (ev) {
484                 switch (ev->detail) {
485                 case GDK_NOTIFY_INFERIOR:
486                         DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
487                         break;
488
489                 case GDK_NOTIFY_VIRTUAL:
490                         DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
491                         /* fallthru */
492
493                 default:
494                         DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
495                         DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
496                         state.clear ();
497                         current_window = 0;
498                 }
499         } else {
500                 current_window = 0;
501         }
502
503         return false;
504 }
505
506 bool
507 Keyboard::focus_in_window (GdkEventFocus *, Gtk::Window* win)
508 {
509         current_window = win;
510         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Focusing in window, title = %1\n", win->get_title()));
511         return false;
512 }
513
514 bool
515 Keyboard::focus_out_window (GdkEventFocus * ev, Gtk::Window* win)
516 {
517         if (ev) {
518                 state.clear ();
519                 current_window = 0;
520         }  else {
521                 current_window = 0;
522         }
523
524         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Foucusing out window, title = %1\n", win->get_title()));
525
526         return false;
527 }
528
529 void
530 Keyboard::set_edit_button (guint but)
531 {
532         edit_but = but;
533 }
534
535 void
536 Keyboard::set_edit_modifier (guint mod)
537 {
538         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
539         edit_mod = mod;
540         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
541 }
542
543 void
544 Keyboard::set_delete_button (guint but)
545 {
546         delete_but = but;
547 }
548
549 void
550 Keyboard::set_delete_modifier (guint mod)
551 {
552         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
553         delete_mod = mod;
554         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
555 }
556
557 void
558 Keyboard::set_insert_note_button (guint but)
559 {
560         insert_note_but = but;
561 }
562
563 void
564 Keyboard::set_insert_note_modifier (guint mod)
565 {
566         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~insert_note_mod);
567         insert_note_mod = mod;
568         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | insert_note_mod);
569 }
570
571
572 void
573 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
574 {
575         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
576         var = newval;
577         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
578 }
579
580 void
581 Keyboard::set_snap_modifier (guint mod)
582 {
583         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
584         snap_mod = mod;
585         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
586 }
587
588 void
589 Keyboard::set_snap_delta_modifier (guint mod)
590 {
591         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_delta_mod);
592         snap_delta_mod = mod;
593         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_delta_mod);
594 }
595
596 bool
597 Keyboard::is_edit_event (GdkEventButton *ev)
598 {
599         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
600                 (ev->button == Keyboard::edit_button()) &&
601                 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
602 }
603
604 bool
605 Keyboard::is_insert_note_event (GdkEventButton *ev)
606 {
607         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
608                 (ev->button == Keyboard::insert_note_button()) &&
609                 ((ev->state & RelevantModifierKeyMask) == Keyboard::insert_note_modifier());
610 }
611
612 bool
613 Keyboard::is_button2_event (GdkEventButton* ev)
614 {
615 #ifdef GTKOSX
616         return (ev->button == 2) ||
617                 ((ev->button == 1) &&
618                  ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
619 #else
620         return ev->button == 2;
621 #endif
622 }
623
624 bool
625 Keyboard::is_delete_event (GdkEventButton *ev)
626 {
627         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
628                 (ev->button == Keyboard::delete_button()) &&
629                 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
630 }
631
632 bool
633 Keyboard::is_context_menu_event (GdkEventButton *ev)
634 {
635         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
636                 (ev->button == 3) &&
637                 ((ev->state & RelevantModifierKeyMask) == 0);
638 }
639
640 bool
641 Keyboard::no_modifiers_active (guint state)
642 {
643         return (state & RelevantModifierKeyMask) == 0;
644 }
645
646 bool
647 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
648 {
649         return (state & mask) == (guint) mask;
650 }
651
652 bool
653 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
654 {
655         return (state & RelevantModifierKeyMask) == (guint) mask;
656 }
657
658 void
659 Keyboard::keybindings_changed ()
660 {
661         if (Keyboard::can_save_keybindings) {
662                 Keyboard::bindings_changed_after_save_became_legal = true;
663         }
664
665         Keyboard::save_keybindings ();
666 }
667
668 void
669 Keyboard::set_can_save_keybindings (bool yn)
670 {
671         can_save_keybindings = yn;
672 }
673
674 void
675 Keyboard::save_keybindings ()
676 {
677         if (can_save_keybindings && bindings_changed_after_save_became_legal) {
678                 Gtk::AccelMap::save (user_keybindings_path);
679         }
680 }
681
682 bool
683 Keyboard::load_keybindings (string path)
684 {
685         try {
686                 info << "Loading bindings from " << path << endl;
687
688                 Gtk::AccelMap::load (path);
689
690                 _current_binding_name = _("Unknown");
691
692                 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
693                         if (path == x->second) {
694                                 _current_binding_name = x->first;
695                                 break;
696                         }
697                 }
698
699
700         } catch (...) {
701                 error << string_compose (_("key bindings file not found at \"%2\" or contains errors."), path)
702                       << endmsg;
703                 return false;
704         }
705
706         /* now find all release-driven bindings */
707
708         vector<string> groups;
709         vector<string> names;
710         vector<string> tooltips;
711         vector<AccelKey> bindings;
712
713         ActionManager::get_all_actions (groups, names, tooltips, bindings);
714
715         vector<string>::iterator g;
716         vector<AccelKey>::iterator b;
717         vector<string>::iterator n;
718
719         release_keys.clear ();
720
721         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
722                 stringstream s;
723                 s << "Action: " << *n << " Group: " << *g << " Binding: ";
724
725                 if ((*b).get_key() != GDK_VoidSymbol) {
726                         s << b->get_key() << " w/mod " << hex << b->get_mod() << dec << " = " << b->get_abbrev () << "\n";
727                 } else {
728                         s << "unbound\n";
729                 }
730
731                 DEBUG_TRACE (DEBUG::Bindings, s.str ());
732         }
733
734         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
735                 if ((*b).get_mod() & Gdk::RELEASE_MASK) {
736                         release_keys.insert (pair<AccelKey,two_strings> (*b, two_strings (*g, *n)));
737                 }
738         }
739
740         return true;
741 }
742
743 int
744 Keyboard::reset_bindings ()
745 {
746         if (Glib::file_test (user_keybindings_path,  Glib::FILE_TEST_EXISTS)) {
747
748                 string new_path = user_keybindings_path;
749                 new_path += ".old";
750
751                 if (::g_rename (user_keybindings_path.c_str(), new_path.c_str())) {
752                         error << string_compose (_("Cannot rename your own keybinding file (%1)"), strerror (errno)) << endmsg;
753                         return -1;
754                 }
755         }
756
757         {
758                 PBD::Unwinder<bool> uw (can_save_keybindings, false);
759                 setup_keybindings ();
760         }
761
762         return 0;
763 }