2 * Copyright (C) 2001-2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2011-2012 Carl Hetherington <carl@carlh.net>
4 * Copyright (C) 2013 Colin Fletcher <colin.m.fletcher@googlemail.com>
5 * Copyright (C) 2015-2016 Nick Mainsbridge <mainsbridge@gmail.com>
6 * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
7 * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "pbd/gstdio_compat.h"
32 #include <gtkmm/widget.h>
33 #include <gtkmm/window.h>
34 #include <gtkmm/accelmap.h>
35 #include <gdk/gdkkeysyms.h>
37 #include "pbd/error.h"
38 #include "pbd/convert.h"
39 #include "pbd/file_utils.h"
40 #include "pbd/search_path.h"
41 #include "pbd/xml++.h"
42 #include "pbd/debug.h"
43 #include "pbd/unwind.h"
45 #include "gtkmm2ext/actions.h"
46 #include "gtkmm2ext/bindings.h"
47 #include "gtkmm2ext/keyboard.h"
48 #include "gtkmm2ext/debug.h"
49 #include "gtkmm2ext/utils.h"
55 using namespace Gtkmm2ext;
58 guint Keyboard::edit_but = 3;
59 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
60 guint Keyboard::delete_but = 3;
61 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
62 guint Keyboard::insert_note_but = 1;
63 guint Keyboard::insert_note_mod = GDK_CONTROL_MASK;
67 guint Keyboard::PrimaryModifier = GDK_MOD2_MASK; // Command
68 guint Keyboard::SecondaryModifier = GDK_CONTROL_MASK; // Control
69 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
70 guint Keyboard::Level4Modifier = GDK_MOD1_MASK; // Alt/Option
71 guint Keyboard::CopyModifier = GDK_CONTROL_MASK; // Control
72 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
73 guint Keyboard::button2_modifiers = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
75 const char* Keyboard::primary_modifier_name() { return _("Command"); }
76 const char* Keyboard::secondary_modifier_name() { return _("Control"); }
77 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
78 const char* Keyboard::level4_modifier_name() { return _("Option"); }
80 const char* Keyboard::primary_modifier_short_name() { return _("Cmd"); }
81 const char* Keyboard::secondary_modifier_short_name() { return _("Ctrl"); }
82 const char* Keyboard::tertiary_modifier_short_name() { return S_("Key|Shift"); }
83 const char* Keyboard::level4_modifier_short_name() { return _("Opt"); }
85 guint Keyboard::snap_mod = Keyboard::Level4Modifier|Keyboard::TertiaryModifier; // XXX this is probably completely wrong
86 guint Keyboard::snap_delta_mod = Keyboard::Level4Modifier;
90 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
91 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK; // Alt/Option
92 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
93 guint Keyboard::Level4Modifier = GDK_MOD4_MASK|GDK_SUPER_MASK; // Mod4/Windows
94 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;
95 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
96 guint Keyboard::button2_modifiers = 0; /* not used */
98 const char* Keyboard::primary_modifier_name() { return _("Control"); }
99 const char* Keyboard::secondary_modifier_name() { return _("Alt"); }
100 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
101 const char* Keyboard::level4_modifier_name() { return _("Windows"); }
103 const char* Keyboard::primary_modifier_short_name() { return _("Ctrl"); }
104 const char* Keyboard::secondary_modifier_short_name() { return _("Alt"); }
105 const char* Keyboard::tertiary_modifier_short_name() { return S_("Key|Shift"); }
106 const char* Keyboard::level4_modifier_short_name() { return _("Win"); }
108 guint Keyboard::snap_mod = Keyboard::SecondaryModifier;
109 guint Keyboard::snap_delta_mod = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
113 guint Keyboard::GainFineScaleModifier = Keyboard::PrimaryModifier;
114 guint Keyboard::GainExtraFineScaleModifier = Keyboard::SecondaryModifier;
116 guint Keyboard::ScrollZoomVerticalModifier = Keyboard::SecondaryModifier;
117 guint Keyboard::ScrollZoomHorizontalModifier = Keyboard::PrimaryModifier;
118 guint Keyboard::ScrollHorizontalModifier = Keyboard::TertiaryModifier;
120 Keyboard* Keyboard::_the_keyboard = 0;
121 Gtk::Window* Keyboard::current_window = 0;
122 bool Keyboard::_some_magic_widget_has_focus = false;
124 std::string Keyboard::user_keybindings_path;
125 bool Keyboard::can_save_keybindings = false;
126 bool Keyboard::bindings_changed_after_save_became_legal = false;
127 map<string,string> Keyboard::binding_files;
128 string Keyboard::_current_binding_name;
129 string Keyboard::binding_filename_suffix = X_(".keys");
130 Gtk::Window* Keyboard::pre_dialog_active_window = 0;
132 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
133 GdkModifierType Keyboard::RelevantModifierKeyMask;
134 sigc::signal0<void> Keyboard::RelevantModifierKeysChanged;
137 Keyboard::magic_widget_grab_focus ()
139 _some_magic_widget_has_focus = true;
143 Keyboard::magic_widget_drop_focus ()
145 _some_magic_widget_has_focus = false;
149 Keyboard::some_magic_widget_has_focus ()
151 return _some_magic_widget_has_focus;
154 Keyboard::Keyboard ()
156 if (_the_keyboard == 0) {
157 _the_keyboard = this;
158 _current_binding_name = _("Unknown");
161 reset_relevant_modifier_key_mask();
163 snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
166 Keyboard::~Keyboard ()
168 gtk_key_snooper_remove (snooper_id);
172 Keyboard::get_state (void)
174 XMLNode* node = new XMLNode ("Keyboard");
176 node->set_property ("copy-modifier", CopyModifier);
177 node->set_property ("edit-button", edit_but);
178 node->set_property ("edit-modifier", edit_mod);
179 node->set_property ("delete-button", delete_but);
180 node->set_property ("delete-modifier", delete_mod);
181 node->set_property ("snap-modifier", snap_mod);
182 node->set_property ("snap-delta-modifier", snap_delta_mod);
183 node->set_property ("insert-note-button", insert_note_but);
184 node->set_property ("insert-note-modifier", insert_note_mod);
190 Keyboard::set_state (const XMLNode& node, int /*version*/)
192 node.get_property ("copy-modifier", CopyModifier);
193 node.get_property ("edit-button", edit_but);
194 node.get_property ("edit-modifier", edit_mod);
195 node.get_property ("delete-button", delete_but);
196 node.get_property ("delete-modifier", delete_mod);
197 node.get_property ("snap-modifier", snap_mod);
198 node.get_property ("snap-delta-modifier", snap_delta_mod);
199 node.get_property ("insert-note-button", insert_note_but);
200 node.get_property ("insert-note-modifier", insert_note_mod);
206 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
208 return ((Keyboard *) data)->snooper (widget, event);
213 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
221 "Snoop widget %1 name: [%6] key %2 [%8] type %3 state %4 [%7] magic %5\n",
222 widget, event->keyval, event->type, event->state, _some_magic_widget_has_focus,
223 gtk_widget_get_name (widget), show_gdk_event_state (event->state), gdk_keyval_name (event->keyval)
227 if (event->keyval == GDK_Shift_R) {
228 keyval = GDK_Shift_L;
230 } else if (event->keyval == GDK_Control_R) {
231 keyval = GDK_Control_L;
234 keyval = event->keyval;
237 if (event->state & ScrollZoomVerticalModifier) {
238 /* There is a special and rather hacky situation in Editor which makes
239 it useful to know when the modifier key for vertical zoom has been
240 released, so emit a signal here (see Editor::_stepping_axis_view).
241 Note that the state bit for the modifier key is set for the key-up
242 event when the modifier is released, but not the key-down when it
243 is pressed, so we get here on key-up, which is what we want.
245 ZoomVerticalModifierReleased (); /* EMIT SIGNAL */
248 if (event->type == GDK_KEY_PRESS) {
250 if (find (state.begin(), state.end(), keyval) == state.end()) {
251 state.push_back (keyval);
252 sort (state.begin(), state.end());
256 /* key is already down. if its also used for release,
257 prevent auto-repeat events.
261 /* August 2015: we don't have any release bindings
264 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
266 const AccelKey& ak (k->first);
268 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
269 DEBUG_TRACE (DEBUG::Keyboard, "Suppress auto repeat\n");
278 if (event->type == GDK_KEY_RELEASE) {
280 State::iterator k = find (state.begin(), state.end(), keyval);
282 if (k != state.end()) {
283 /* this cannot change the ordering, so need to sort */
286 DEBUG_TRACE (DEBUG::Keyboard, "no keys down\n");
289 if (DEBUG_ENABLED(DEBUG::Keyboard)) {
291 DEBUG_STR_APPEND(a, "keyboard, keys still down: ");
292 for (State::iterator i = state.begin(); i != state.end(); ++i) {
293 DEBUG_STR_APPEND(a, gdk_keyval_name (*i));
294 DEBUG_STR_APPEND(a, ',');
296 DEBUG_STR_APPEND(a, '\n');
297 DEBUG_TRACE (DEBUG::Keyboard, DEBUG_STR(a).str());
303 if (modifier_state_equals (event->state, PrimaryModifier)) {
305 /* Special keys that we want to handle in
306 any dialog, no matter whether it uses
307 the regular set of accelerators or not
310 switch (event->keyval) {
312 close_current_dialog ();
319 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("snooper returns %1\n", ret));
325 Keyboard::reset_relevant_modifier_key_mask ()
327 RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
329 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
330 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
331 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
332 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | Level4Modifier);
333 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
334 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
336 gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
338 RelevantModifierKeysChanged(); /* EMIT SIGNAL */
342 Keyboard::close_current_dialog ()
344 if (current_window) {
345 current_window->hide ();
348 if (pre_dialog_active_window) {
349 pre_dialog_active_window->present ();
350 pre_dialog_active_window = 0;
356 Keyboard::catch_user_event_for_pre_dialog_focus (GdkEvent* ev, Gtk::Window* w)
359 case GDK_BUTTON_PRESS:
360 case GDK_BUTTON_RELEASE:
362 case GDK_KEY_RELEASE:
363 pre_dialog_active_window = w;
366 case GDK_FOCUS_CHANGE:
367 if (ev->focus_change.in) {
368 pre_dialog_active_window = w;
379 Keyboard::key_is_down (uint32_t keyval)
381 return find (state.begin(), state.end(), keyval) != state.end();
385 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
387 current_window = win;
388 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Entering window, title = %1\n", win->get_title()));
393 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
396 switch (ev->detail) {
397 case GDK_NOTIFY_INFERIOR:
398 DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
401 case GDK_NOTIFY_VIRTUAL:
402 DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
406 DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
407 DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
412 DEBUG_TRACE (DEBUG::Keyboard, "LEAVE window without event\n");
420 Keyboard::focus_in_window (GdkEventFocus *, Gtk::Window* win)
422 current_window = win;
423 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Focusing in window, title = %1\n", win->get_title()));
428 Keyboard::focus_out_window (GdkEventFocus * ev, Gtk::Window* win)
434 if (win == current_window) {
439 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Foucusing out window, title = %1\n", win->get_title()));
445 Keyboard::set_edit_button (guint but)
451 Keyboard::set_edit_modifier (guint mod)
454 reset_relevant_modifier_key_mask();
458 Keyboard::set_delete_button (guint but)
464 Keyboard::set_delete_modifier (guint mod)
467 reset_relevant_modifier_key_mask();
471 Keyboard::set_insert_note_button (guint but)
473 insert_note_but = but;
477 Keyboard::set_insert_note_modifier (guint mod)
479 insert_note_mod = mod;
480 reset_relevant_modifier_key_mask();
485 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
488 reset_relevant_modifier_key_mask();
492 Keyboard::set_snap_modifier (guint mod)
495 reset_relevant_modifier_key_mask();
499 Keyboard::set_snap_delta_modifier (guint mod)
501 snap_delta_mod = mod;
502 reset_relevant_modifier_key_mask();
506 Keyboard::is_edit_event (GdkEventButton *ev)
508 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
509 (ev->button == Keyboard::edit_button()) &&
510 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
514 Keyboard::is_insert_note_event (GdkEventButton *ev)
516 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
517 (ev->button == Keyboard::insert_note_button()) &&
518 ((ev->state & RelevantModifierKeyMask) == Keyboard::insert_note_modifier());
522 Keyboard::is_button2_event (GdkEventButton* ev)
525 return (ev->button == 2) ||
526 ((ev->button == 1) &&
527 ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
529 return ev->button == 2;
534 Keyboard::is_delete_event (GdkEventButton *ev)
536 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
537 (ev->button == Keyboard::delete_button()) &&
538 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
542 Keyboard::is_context_menu_event (GdkEventButton *ev)
544 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
546 ((ev->state & RelevantModifierKeyMask) == 0);
550 Keyboard::no_modifiers_active (guint state)
552 return (state & RelevantModifierKeyMask) == 0;
556 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
558 return (state & mask) == (guint) mask;
562 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
564 return (state & RelevantModifierKeyMask) == (guint) mask;
568 Keyboard::keybindings_changed ()
570 if (Keyboard::can_save_keybindings) {
571 Keyboard::bindings_changed_after_save_became_legal = true;
574 Keyboard::save_keybindings ();
578 Keyboard::set_can_save_keybindings (bool yn)
580 can_save_keybindings = yn;
584 Keyboard::save_keybindings ()
586 if (can_save_keybindings && bindings_changed_after_save_became_legal) {
587 /* Call to specific implementation to save bindings to path */
588 store_keybindings (user_keybindings_path);
593 Keyboard::load_keybindings (string const & path)
596 info << "Loading bindings from " << path << endl;
598 /* Call to specific implementation to load bindings from path */
599 read_keybindings (path);
601 _current_binding_name = _("Unknown");
603 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
604 if (path == x->second) {
605 _current_binding_name = x->first;
612 error << string_compose (_("key bindings file not found at \"%2\" or contains errors."), path)
621 Keyboard::read_keybindings (string const & path)
625 if (!tree.read (path.c_str())) {
629 /* toplevel node is "BindingSet; children are "Bindings" */
631 XMLNodeList const& children = tree.root()->children();
633 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
634 XMLNode const * child = *i;
635 if (child->name() == X_("Bindings")) {
636 XMLProperty const* name = child->property (X_("name"));
638 warning << _("Keyboard binding found without a name") << endmsg;
642 Bindings* b = new Bindings (name->value());
651 Keyboard::store_keybindings (string const & path)
653 XMLNode* node = new XMLNode (X_("BindingSet"));
657 for (list<Bindings*>::const_iterator b = Bindings::bindings.begin(); b != Bindings::bindings.end(); ++b) {
658 bnode = new XMLNode (X_("Bindings"));
659 bnode->set_property (X_("name"), (*b)->name());
661 node->add_child_nocopy (*bnode);
665 tree.set_root (node); /* tree now owns root and will delete it */
667 if (!tree.write (path)) {
668 error << string_compose (_("Cannot save key bindings to %1"), path) << endmsg;
676 Keyboard::reset_bindings ()
678 if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
680 string new_path = user_keybindings_path;
683 if (::g_rename (user_keybindings_path.c_str(), new_path.c_str())) {
684 error << string_compose (_("Cannot rename your own keybinding file (%1)"), strerror (errno)) << endmsg;
690 PBD::Unwinder<bool> uw (can_save_keybindings, false);
691 Bindings::reset_bindings ();
692 setup_keybindings ();
693 Bindings::associate_all ();