2 Copyright (C) 2001 Paul Davis
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.
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.
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.
26 #include "pbd/gstdio_compat.h"
28 #include <gtkmm/widget.h>
29 #include <gtkmm/window.h>
30 #include <gtkmm/accelmap.h>
31 #include <gdk/gdkkeysyms.h>
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"
41 #include "gtkmm2ext/actions.h"
42 #include "gtkmm2ext/bindings.h"
43 #include "gtkmm2ext/keyboard.h"
44 #include "gtkmm2ext/debug.h"
45 #include "gtkmm2ext/utils.h"
51 using namespace Gtkmm2ext;
54 guint Keyboard::edit_but = 3;
55 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
56 guint Keyboard::delete_but = 3;
57 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
58 guint Keyboard::insert_note_but = 1;
59 guint Keyboard::insert_note_mod = GDK_CONTROL_MASK;
63 uint Keyboard::PrimaryModifier = GDK_MOD2_MASK; // Command
64 guint Keyboard::SecondaryModifier = GDK_CONTROL_MASK; // Control
65 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
66 guint Keyboard::Level4Modifier = GDK_MOD1_MASK; // Alt/Option
67 guint Keyboard::CopyModifier = GDK_CONTROL_MASK; // Control
68 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
69 guint Keyboard::button2_modifiers = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
71 const char* Keyboard::primary_modifier_name() { return _("Command"); }
72 const char* Keyboard::secondary_modifier_name() { return _("Control"); }
73 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
74 const char* Keyboard::level4_modifier_name() { return _("Option"); }
76 const char* Keyboard::primary_modifier_short_name() { return _("Cmd"); }
77 const char* Keyboard::secondary_modifier_short_name() { return _("Ctrl"); }
78 const char* Keyboard::tertiary_modifier_short_name() { return S_("Key|Shift"); }
79 const char* Keyboard::level4_modifier_short_name() { return _("Opt"); }
81 guint Keyboard::snap_mod = Keyboard::Level4Modifier|Keyboard::TertiaryModifier; // XXX this is probably completely wrong
82 guint Keyboard::snap_delta_mod = Keyboard::Level4Modifier;
86 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
87 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK; // Alt/Option
88 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
89 guint Keyboard::Level4Modifier = GDK_MOD4_MASK; // Mod4/Windows
90 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;
91 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
92 guint Keyboard::button2_modifiers = 0; /* not used */
94 const char* Keyboard::primary_modifier_name() { return _("Control"); }
95 const char* Keyboard::secondary_modifier_name() { return _("Alt"); }
96 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
97 const char* Keyboard::level4_modifier_name() { return _("Windows"); }
99 const char* Keyboard::primary_modifier_short_name() { return _("Ctrl"); }
100 const char* Keyboard::secondary_modifier_short_name() { return _("Alt"); }
101 const char* Keyboard::tertiary_modifier_short_name() { return S_("Key|Shift"); }
102 const char* Keyboard::level4_modifier_short_name() { return _("Win"); }
104 guint Keyboard::snap_mod = Keyboard::SecondaryModifier;
105 guint Keyboard::snap_delta_mod = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
109 guint Keyboard::GainFineScaleModifier = Keyboard::PrimaryModifier;
110 guint Keyboard::GainExtraFineScaleModifier = Keyboard::SecondaryModifier;
112 guint Keyboard::ScrollZoomVerticalModifier = Keyboard::SecondaryModifier;
113 guint Keyboard::ScrollZoomHorizontalModifier = Keyboard::PrimaryModifier;
114 guint Keyboard::ScrollHorizontalModifier = Keyboard::TertiaryModifier;
116 Keyboard* Keyboard::_the_keyboard = 0;
117 Gtk::Window* Keyboard::current_window = 0;
118 bool Keyboard::_some_magic_widget_has_focus = false;
120 std::string Keyboard::user_keybindings_path;
121 bool Keyboard::can_save_keybindings = false;
122 bool Keyboard::bindings_changed_after_save_became_legal = false;
123 map<string,string> Keyboard::binding_files;
124 string Keyboard::_current_binding_name;
125 string Keyboard::binding_filename_suffix = X_(".keys");
126 Gtk::Window* Keyboard::pre_dialog_active_window = 0;
128 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
129 GdkModifierType Keyboard::RelevantModifierKeyMask;
130 sigc::signal0<void> Keyboard::RelevantModifierKeysChanged;
133 Keyboard::magic_widget_grab_focus ()
135 _some_magic_widget_has_focus = true;
139 Keyboard::magic_widget_drop_focus ()
141 _some_magic_widget_has_focus = false;
145 Keyboard::some_magic_widget_has_focus ()
147 return _some_magic_widget_has_focus;
150 Keyboard::Keyboard ()
152 if (_the_keyboard == 0) {
153 _the_keyboard = this;
154 _current_binding_name = _("Unknown");
157 reset_relevant_modifier_key_mask();
159 snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
162 Keyboard::~Keyboard ()
164 gtk_key_snooper_remove (snooper_id);
168 Keyboard::get_state (void)
170 XMLNode* node = new XMLNode ("Keyboard");
173 snprintf (buf, sizeof (buf), "%d", CopyModifier);
174 node->add_property ("copy-modifier", buf);
175 snprintf (buf, sizeof (buf), "%d", edit_but);
176 node->add_property ("edit-button", buf);
177 snprintf (buf, sizeof (buf), "%d", edit_mod);
178 node->add_property ("edit-modifier", buf);
179 snprintf (buf, sizeof (buf), "%d", delete_but);
180 node->add_property ("delete-button", buf);
181 snprintf (buf, sizeof (buf), "%d", delete_mod);
182 node->add_property ("delete-modifier", buf);
183 snprintf (buf, sizeof (buf), "%d", snap_mod);
184 node->add_property ("snap-modifier", buf);
185 snprintf (buf, sizeof (buf), "%d", snap_delta_mod);
186 node->add_property ("snap-delta-modifier", buf);
187 snprintf (buf, sizeof (buf), "%d", insert_note_but);
188 node->add_property ("insert-note-button", buf);
189 snprintf (buf, sizeof (buf), "%d", insert_note_mod);
190 node->add_property ("insert-note-modifier", buf);
196 Keyboard::set_state (const XMLNode& node, int /*version*/)
198 XMLProperty const * prop;
200 if ((prop = node.property ("copy-modifier")) != 0) {
201 sscanf (prop->value().c_str(), "%d", &CopyModifier);
204 if ((prop = node.property ("edit-button")) != 0) {
205 sscanf (prop->value().c_str(), "%d", &edit_but);
208 if ((prop = node.property ("edit-modifier")) != 0) {
209 sscanf (prop->value().c_str(), "%d", &edit_mod);
212 if ((prop = node.property ("delete-button")) != 0) {
213 sscanf (prop->value().c_str(), "%d", &delete_but);
216 if ((prop = node.property ("delete-modifier")) != 0) {
217 sscanf (prop->value().c_str(), "%d", &delete_mod);
220 if ((prop = node.property ("snap-modifier")) != 0) {
221 sscanf (prop->value().c_str(), "%d", &snap_mod);
224 if ((prop = node.property ("snap-delta-modifier")) != 0) {
225 sscanf (prop->value().c_str(), "%d", &snap_delta_mod);
228 if ((prop = node.property ("insert-note-button")) != 0) {
229 sscanf (prop->value().c_str(), "%d", &insert_note_but);
232 if ((prop = node.property ("insert-note-modifier")) != 0) {
233 sscanf (prop->value().c_str(), "%d", &insert_note_mod);
240 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
242 return ((Keyboard *) data)->snooper (widget, event);
247 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
255 "Snoop widget %1 name: [%6] key %2 [%8] type %3 state %4 [%7] magic %5\n",
256 widget, event->keyval, event->type, event->state, _some_magic_widget_has_focus,
257 gtk_widget_get_name (widget), show_gdk_event_state (event->state), gdk_keyval_name (event->keyval)
261 if (event->keyval == GDK_Shift_R) {
262 keyval = GDK_Shift_L;
264 } else if (event->keyval == GDK_Control_R) {
265 keyval = GDK_Control_L;
268 keyval = event->keyval;
271 if (event->state & ScrollZoomVerticalModifier) {
272 /* There is a special and rather hacky situation in Editor which makes
273 it useful to know when the modifier key for vertical zoom has been
274 released, so emit a signal here (see Editor::_stepping_axis_view).
275 Note that the state bit for the modifier key is set for the key-up
276 event when the modifier is released, but not the key-down when it
277 is pressed, so we get here on key-up, which is what we want.
279 ZoomVerticalModifierReleased (); /* EMIT SIGNAL */
282 if (event->type == GDK_KEY_PRESS) {
284 if (find (state.begin(), state.end(), keyval) == state.end()) {
285 state.push_back (keyval);
286 sort (state.begin(), state.end());
290 /* key is already down. if its also used for release,
291 prevent auto-repeat events.
295 /* August 2015: we don't have any release bindings
298 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
300 const AccelKey& ak (k->first);
302 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
303 DEBUG_TRACE (DEBUG::Keyboard, "Suppress auto repeat\n");
312 /* Special keys that we want to handle in
313 any dialog, no matter whether it uses
314 the regular set of accelerators or not
317 if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
318 switch (event->keyval) {
320 close_current_dialog ();
326 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("snooper returns %1\n", ret));
332 Keyboard::reset_relevant_modifier_key_mask ()
334 RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
336 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
337 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
338 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
339 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | Level4Modifier);
340 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
341 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
343 gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
346 /* Remove SUPER,HYPER,META.
348 * GTK on OS X adds META when Command is pressed for various indefensible reasons, since
349 * it also uses MOD2 to indicate Command. Our code assumes that each
350 * modifier (Primary, Secondary etc.) is represented by a single bit in
351 * the modifier mask, but GTK's (STUPID) design uses two (MOD2 + META)
352 * to represent the Command key. Some discussion about this is here:
353 * https://bugzilla.gnome.org/show_bug.cgi?id=692597
355 * We cannot do this until AFTER we told GTK what the default modifier
356 * was, because otherwise it will fail to recognize MOD2-META-<key> as
359 * Note that in the tabbed branch, we no longer use GTK accelerators
360 * for functional purposes, so this is as critical for that branch.
363 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~GDK_SUPER_MASK);
364 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~GDK_HYPER_MASK);
365 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~GDK_META_MASK);
367 RelevantModifierKeysChanged(); /* EMIT SIGNAL */
371 Keyboard::close_current_dialog ()
373 if (current_window) {
374 current_window->hide ();
377 if (pre_dialog_active_window) {
378 pre_dialog_active_window->present ();
379 pre_dialog_active_window = 0;
385 Keyboard::catch_user_event_for_pre_dialog_focus (GdkEvent* ev, Gtk::Window* w)
388 case GDK_BUTTON_PRESS:
389 case GDK_BUTTON_RELEASE:
391 case GDK_KEY_RELEASE:
392 pre_dialog_active_window = w;
395 case GDK_FOCUS_CHANGE:
396 if (ev->focus_change.in) {
397 pre_dialog_active_window = w;
408 Keyboard::key_is_down (uint32_t keyval)
410 return find (state.begin(), state.end(), keyval) != state.end();
414 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
416 current_window = win;
417 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Entering window, title = %1\n", win->get_title()));
422 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
425 switch (ev->detail) {
426 case GDK_NOTIFY_INFERIOR:
427 DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
430 case GDK_NOTIFY_VIRTUAL:
431 DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
435 DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
436 DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
441 DEBUG_TRACE (DEBUG::Keyboard, "LEAVE window without event\n");
449 Keyboard::focus_in_window (GdkEventFocus *, Gtk::Window* win)
451 current_window = win;
452 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Focusing in window, title = %1\n", win->get_title()));
457 Keyboard::focus_out_window (GdkEventFocus * ev, Gtk::Window* win)
463 if (win == current_window) {
468 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Foucusing out window, title = %1\n", win->get_title()));
474 Keyboard::set_edit_button (guint but)
480 Keyboard::set_edit_modifier (guint mod)
483 reset_relevant_modifier_key_mask();
487 Keyboard::set_delete_button (guint but)
493 Keyboard::set_delete_modifier (guint mod)
496 reset_relevant_modifier_key_mask();
500 Keyboard::set_insert_note_button (guint but)
502 insert_note_but = but;
506 Keyboard::set_insert_note_modifier (guint mod)
508 insert_note_mod = mod;
509 reset_relevant_modifier_key_mask();
514 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
517 reset_relevant_modifier_key_mask();
521 Keyboard::set_snap_modifier (guint mod)
524 reset_relevant_modifier_key_mask();
528 Keyboard::set_snap_delta_modifier (guint mod)
530 snap_delta_mod = mod;
531 reset_relevant_modifier_key_mask();
535 Keyboard::is_edit_event (GdkEventButton *ev)
537 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
538 (ev->button == Keyboard::edit_button()) &&
539 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
543 Keyboard::is_insert_note_event (GdkEventButton *ev)
545 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
546 (ev->button == Keyboard::insert_note_button()) &&
547 ((ev->state & RelevantModifierKeyMask) == Keyboard::insert_note_modifier());
551 Keyboard::is_button2_event (GdkEventButton* ev)
554 return (ev->button == 2) ||
555 ((ev->button == 1) &&
556 ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
558 return ev->button == 2;
563 Keyboard::is_delete_event (GdkEventButton *ev)
565 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
566 (ev->button == Keyboard::delete_button()) &&
567 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
571 Keyboard::is_context_menu_event (GdkEventButton *ev)
573 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
575 ((ev->state & RelevantModifierKeyMask) == 0);
579 Keyboard::no_modifiers_active (guint state)
581 return (state & RelevantModifierKeyMask) == 0;
585 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
587 return (state & mask) == (guint) mask;
591 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
593 return (state & RelevantModifierKeyMask) == (guint) mask;
597 Keyboard::keybindings_changed ()
599 if (Keyboard::can_save_keybindings) {
600 Keyboard::bindings_changed_after_save_became_legal = true;
603 Keyboard::save_keybindings ();
607 Keyboard::set_can_save_keybindings (bool yn)
609 can_save_keybindings = yn;
613 Keyboard::save_keybindings ()
615 if (can_save_keybindings && bindings_changed_after_save_became_legal) {
616 /* Call to specific implementation to save bindings to path */
617 store_keybindings (user_keybindings_path);
622 Keyboard::load_keybindings (string const & path)
625 info << "Loading bindings from " << path << endl;
627 /* Call to specific implementation to load bindings from path */
628 read_keybindings (path);
630 _current_binding_name = _("Unknown");
632 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
633 if (path == x->second) {
634 _current_binding_name = x->first;
641 error << string_compose (_("key bindings file not found at \"%2\" or contains errors."), path)
650 Keyboard::read_keybindings (string const & path)
654 if (!tree.read (path.c_str())) {
658 /* toplevel node is "BindingSet; children are "Bindings" */
660 XMLNodeList const& children = tree.root()->children();
662 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
663 XMLNode const * child = *i;
664 if (child->name() == X_("Bindings")) {
665 XMLProperty const* name = child->property (X_("name"));
667 warning << _("Keyboard binding found without a name") << endmsg;
671 Bindings* b = new Bindings (name->value());
680 Keyboard::store_keybindings (string const & path)
682 XMLNode* node = new XMLNode (X_("BindingSet"));
686 for (list<Bindings*>::const_iterator b = Bindings::bindings.begin(); b != Bindings::bindings.end(); ++b) {
687 bnode = new XMLNode (X_("Bindings"));
688 bnode->add_property (X_("name"), (*b)->name());
690 node->add_child_nocopy (*bnode);
694 tree.set_root (node); /* tree now owns root and will delete it */
696 if (!tree.write (path)) {
697 error << string_compose (_("Cannot save key bindings to %1"), path) << endmsg;
705 Keyboard::reset_bindings ()
707 if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
709 string new_path = user_keybindings_path;
712 if (::g_rename (user_keybindings_path.c_str(), new_path.c_str())) {
713 error << string_compose (_("Cannot rename your own keybinding file (%1)"), strerror (errno)) << endmsg;
719 PBD::Unwinder<bool> uw (can_save_keybindings, false);
720 setup_keybindings ();
721 Bindings::associate_all ();