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.
29 #include <glib/gstdio.h>
31 #include <gtkmm/widget.h>
32 #include <gtkmm/window.h>
33 #include <gtkmm/accelmap.h>
34 #include <gdk/gdkkeysyms.h>
36 #include "pbd/error.h"
37 #include "pbd/convert.h"
38 #include "pbd/file_utils.h"
39 #include "pbd/search_path.h"
40 #include "pbd/xml++.h"
41 #include "pbd/debug.h"
42 #include "pbd/unwind.h"
44 #include "gtkmm2ext/keyboard.h"
45 #include "gtkmm2ext/actions.h"
46 #include "gtkmm2ext/debug.h"
52 using namespace Gtkmm2ext;
55 guint Keyboard::edit_but = 3;
56 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
57 guint Keyboard::delete_but = 3;
58 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
59 guint Keyboard::insert_note_but = 1;
60 guint Keyboard::insert_note_mod = GDK_CONTROL_MASK;
61 guint Keyboard::snap_mod = GDK_MOD3_MASK;
62 guint Keyboard::snap_delta_mod = 0;
66 uint Keyboard::PrimaryModifier = GDK_MOD2_MASK; // Command
67 guint Keyboard::SecondaryModifier = GDK_CONTROL_MASK; // Control
68 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
69 guint Keyboard::Level4Modifier = GDK_MOD1_MASK; // Alt/Option
70 guint Keyboard::CopyModifier = GDK_CONTROL_MASK; // Control
71 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
72 guint Keyboard::button2_modifiers = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
74 const char* Keyboard::primary_modifier_name() { return _("Command"); }
75 const char* Keyboard::secondary_modifier_name() { return _("Control"); }
76 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
77 const char* Keyboard::level4_modifier_name() { return _("Option"); }
78 const char* Keyboard::copy_modifier_name() { return _("Control"); }
79 const char* Keyboard::rangeselect_modifier_name() { return S_("Key|Shift"); }
83 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
84 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK; // Alt/Option
85 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
86 guint Keyboard::Level4Modifier = GDK_MOD4_MASK; // Mod4/Windows
87 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;
88 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
89 guint Keyboard::button2_modifiers = 0; /* not used */
91 const char* Keyboard::primary_modifier_name() { return _("Control"); }
92 const char* Keyboard::secondary_modifier_name() { return _("Alt"); }
93 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
94 const char* Keyboard::level4_modifier_name() { return _("Meta"); }
95 const char* Keyboard::copy_modifier_name() { return _("Control"); }
96 const char* Keyboard::rangeselect_modifier_name() { return S_("Key|Shift"); }
100 guint Keyboard::GainFineScaleModifier = Keyboard::PrimaryModifier;
101 guint Keyboard::GainExtraFineScaleModifier = Keyboard::SecondaryModifier;
103 guint Keyboard::ScrollZoomVerticalModifier = Keyboard::SecondaryModifier;
104 guint Keyboard::ScrollZoomHorizontalModifier = Keyboard::PrimaryModifier;
105 guint Keyboard::ScrollHorizontalModifier = Keyboard::TertiaryModifier;
108 Keyboard* Keyboard::_the_keyboard = 0;
109 Gtk::Window* Keyboard::current_window = 0;
110 bool Keyboard::_some_magic_widget_has_focus = false;
112 std::string Keyboard::user_keybindings_path;
113 bool Keyboard::can_save_keybindings = false;
114 bool Keyboard::bindings_changed_after_save_became_legal = false;
115 map<string,string> Keyboard::binding_files;
116 string Keyboard::_current_binding_name;
117 map<AccelKey,pair<string,string>,Keyboard::AccelKeyLess> Keyboard::release_keys;
119 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
121 GdkModifierType Keyboard::RelevantModifierKeyMask;
124 Keyboard::magic_widget_grab_focus ()
126 _some_magic_widget_has_focus = true;
130 Keyboard::magic_widget_drop_focus ()
132 _some_magic_widget_has_focus = false;
136 Keyboard::some_magic_widget_has_focus ()
138 return _some_magic_widget_has_focus;
141 Keyboard::Keyboard ()
143 if (_the_keyboard == 0) {
144 _the_keyboard = this;
145 _current_binding_name = _("Unknown");
148 RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
150 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
151 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
152 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
153 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | Level4Modifier);
154 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
155 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
157 gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
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", edit_but);
174 node->add_property ("edit-button", buf);
175 snprintf (buf, sizeof (buf), "%d", edit_mod);
176 node->add_property ("edit-modifier", buf);
177 snprintf (buf, sizeof (buf), "%d", delete_but);
178 node->add_property ("delete-button", buf);
179 snprintf (buf, sizeof (buf), "%d", delete_mod);
180 node->add_property ("delete-modifier", buf);
181 snprintf (buf, sizeof (buf), "%d", snap_mod);
182 node->add_property ("snap-modifier", buf);
183 snprintf (buf, sizeof (buf), "%d", snap_delta_mod);
184 node->add_property ("snap-delta-modifier", buf);
185 snprintf (buf, sizeof (buf), "%d", insert_note_but);
186 node->add_property ("insert-note-button", buf);
187 snprintf (buf, sizeof (buf), "%d", insert_note_mod);
188 node->add_property ("insert-note-modifier", buf);
194 Keyboard::set_state (const XMLNode& node, int /*version*/)
196 const XMLProperty* prop;
198 if ((prop = node.property ("edit-button")) != 0) {
199 sscanf (prop->value().c_str(), "%d", &edit_but);
202 if ((prop = node.property ("edit-modifier")) != 0) {
203 sscanf (prop->value().c_str(), "%d", &edit_mod);
206 if ((prop = node.property ("delete-button")) != 0) {
207 sscanf (prop->value().c_str(), "%d", &delete_but);
210 if ((prop = node.property ("delete-modifier")) != 0) {
211 sscanf (prop->value().c_str(), "%d", &delete_mod);
214 if ((prop = node.property ("snap-modifier")) != 0) {
215 sscanf (prop->value().c_str(), "%d", &snap_mod);
218 if ((prop = node.property ("snap-delta-modifier")) != 0) {
219 sscanf (prop->value().c_str(), "%d", &snap_delta_mod);
222 if ((prop = node.property ("insert-note-button")) != 0) {
223 sscanf (prop->value().c_str(), "%d", &insert_note_but);
226 if ((prop = node.property ("insert-note-modifier")) != 0) {
227 sscanf (prop->value().c_str(), "%d", &insert_note_mod);
234 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
236 return ((Keyboard *) data)->snooper (widget, event);
240 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
248 "Snoop widget %1 name: [%6] key %2 type %3 state %4 magic %5\n",
249 widget, event->keyval, event->type, event->state, _some_magic_widget_has_focus,
250 gtk_widget_get_name (widget)
254 if (event->keyval == GDK_Shift_R) {
255 keyval = GDK_Shift_L;
257 } else if (event->keyval == GDK_Control_R) {
258 keyval = GDK_Control_L;
261 keyval = event->keyval;
264 if (event->state & ScrollZoomVerticalModifier) {
265 /* There is a special and rather hacky situation in Editor which makes
266 it useful to know when the modifier key for vertical zoom has been
267 released, so emit a signal here (see Editor::_stepping_axis_view).
268 Note that the state bit for the modifier key is set for the key-up
269 event when the modifier is released, but not the key-down when it
270 is pressed, so we get here on key-up, which is what we want.
272 ZoomVerticalModifierReleased (); /* EMIT SIGNAL */
275 if (event->type == GDK_KEY_PRESS) {
277 if (find (state.begin(), state.end(), keyval) == state.end()) {
278 state.push_back (keyval);
279 sort (state.begin(), state.end());
283 /* key is already down. if its also used for release,
284 prevent auto-repeat events.
287 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
289 const AccelKey& ak (k->first);
291 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
292 DEBUG_TRACE (DEBUG::Keyboard, "Suppress auto repeat\n");
299 } else if (event->type == GDK_KEY_RELEASE) {
303 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
305 sort (state.begin(), state.end());
308 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
310 const AccelKey& ak (k->first);
311 two_strings ts (k->second);
313 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
314 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (ts.first.c_str(), ts.second.c_str());
316 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Activate %1 %2\n", ts.first, ts.second));
318 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Use repeat, suppress other\n", ts.first, ts.second));
326 /* Special keys that we want to handle in
327 any dialog, no matter whether it uses
328 the regular set of accelerators or not
331 if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
332 switch (event->keyval) {
334 close_current_dialog ();
340 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("snooper returns %1\n", ret));
346 Keyboard::close_current_dialog ()
348 if (current_window) {
349 current_window->hide ();
355 Keyboard::key_is_down (uint32_t keyval)
357 return find (state.begin(), state.end(), keyval) != state.end();
361 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
363 current_window = win;
364 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Entering window, title = %1\n", win->get_title()));
369 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
372 switch (ev->detail) {
373 case GDK_NOTIFY_INFERIOR:
374 DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
377 case GDK_NOTIFY_VIRTUAL:
378 DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
382 DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
383 DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
395 Keyboard::focus_in_window (GdkEventFocus *, Gtk::Window* win)
397 current_window = win;
398 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Focusing in window, title = %1\n", win->get_title()));
403 Keyboard::focus_out_window (GdkEventFocus * ev, Gtk::Window* win)
412 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Foucusing out window, title = %1\n", win->get_title()));
418 Keyboard::set_edit_button (guint but)
424 Keyboard::set_edit_modifier (guint mod)
426 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
428 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
432 Keyboard::set_delete_button (guint but)
438 Keyboard::set_delete_modifier (guint mod)
440 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
442 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
446 Keyboard::set_insert_note_button (guint but)
448 insert_note_but = but;
452 Keyboard::set_insert_note_modifier (guint mod)
454 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~insert_note_mod);
455 insert_note_mod = mod;
456 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | insert_note_mod);
461 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
463 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
465 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
469 Keyboard::set_snap_modifier (guint mod)
471 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
473 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
477 Keyboard::set_snap_delta_modifier (guint mod)
478 {cerr << "setting snap delta mod" << endl;
479 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_delta_mod);
480 snap_delta_mod = mod;
481 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_delta_mod);
485 Keyboard::is_edit_event (GdkEventButton *ev)
487 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
488 (ev->button == Keyboard::edit_button()) &&
489 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
493 Keyboard::is_insert_note_event (GdkEventButton *ev)
495 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
496 (ev->button == Keyboard::insert_note_button()) &&
497 ((ev->state & RelevantModifierKeyMask) == Keyboard::insert_note_modifier());
501 Keyboard::is_button2_event (GdkEventButton* ev)
504 return (ev->button == 2) ||
505 ((ev->button == 1) &&
506 ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
508 return ev->button == 2;
513 Keyboard::is_delete_event (GdkEventButton *ev)
515 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
516 (ev->button == Keyboard::delete_button()) &&
517 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
521 Keyboard::is_context_menu_event (GdkEventButton *ev)
523 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
525 ((ev->state & RelevantModifierKeyMask) == 0);
529 Keyboard::no_modifiers_active (guint state)
531 return (state & RelevantModifierKeyMask) == 0;
535 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
537 return (state & mask) == (guint) mask;
541 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
543 return (state & RelevantModifierKeyMask) == (guint) mask;
547 Keyboard::keybindings_changed ()
549 if (Keyboard::can_save_keybindings) {
550 Keyboard::bindings_changed_after_save_became_legal = true;
553 Keyboard::save_keybindings ();
557 Keyboard::set_can_save_keybindings (bool yn)
559 can_save_keybindings = yn;
563 Keyboard::save_keybindings ()
565 if (can_save_keybindings && bindings_changed_after_save_became_legal) {
566 Gtk::AccelMap::save (user_keybindings_path);
571 Keyboard::load_keybindings (string path)
574 info << "Loading bindings from " << path << endl;
576 Gtk::AccelMap::load (path);
578 _current_binding_name = _("Unknown");
580 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
581 if (path == x->second) {
582 _current_binding_name = x->first;
589 error << string_compose (_("key bindings file not found at \"%2\" or contains errors."), path)
594 /* now find all release-driven bindings */
596 vector<string> groups;
597 vector<string> names;
598 vector<string> tooltips;
599 vector<AccelKey> bindings;
601 ActionManager::get_all_actions (groups, names, tooltips, bindings);
603 vector<string>::iterator g;
604 vector<AccelKey>::iterator b;
605 vector<string>::iterator n;
607 release_keys.clear ();
609 for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
611 s << "Action: " << *n << " Group: " << *g << " Binding: ";
613 if ((*b).get_key() != GDK_VoidSymbol) {
614 s << b->get_key() << " w/mod " << hex << b->get_mod() << dec << " = " << b->get_abbrev () << "\n";
619 DEBUG_TRACE (DEBUG::Bindings, s.str ());
622 for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
623 if ((*b).get_mod() & Gdk::RELEASE_MASK) {
624 release_keys.insert (pair<AccelKey,two_strings> (*b, two_strings (*g, *n)));
632 Keyboard::reset_bindings ()
634 if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
636 string new_path = user_keybindings_path;
639 if (::g_rename (user_keybindings_path.c_str(), new_path.c_str())) {
640 error << string_compose (_("Cannot rename your own keybinding file (%1)"), strerror (errno)) << endmsg;
646 PBD::Unwinder<bool> uw (can_save_keybindings, false);
647 setup_keybindings ();