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.
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/file_utils.h"
35 #include "pbd/search_path.h"
36 #include "pbd/xml++.h"
37 #include "pbd/debug.h"
39 #include "gtkmm2ext/keyboard.h"
40 #include "gtkmm2ext/actions.h"
41 #include "gtkmm2ext/debug.h"
47 using namespace Gtkmm2ext
;
50 guint
Keyboard::edit_but
= 3;
51 guint
Keyboard::edit_mod
= GDK_CONTROL_MASK
;
52 guint
Keyboard::delete_but
= 3;
53 guint
Keyboard::delete_mod
= GDK_SHIFT_MASK
;
54 guint
Keyboard::insert_note_but
= 3;
55 guint
Keyboard::insert_note_mod
= GDK_CONTROL_MASK
;
56 guint
Keyboard::snap_mod
= GDK_MOD3_MASK
;
60 guint
Keyboard::PrimaryModifier
= GDK_META_MASK
; // Command
61 guint
Keyboard::SecondaryModifier
= GDK_MOD1_MASK
; // Alt/Option
62 guint
Keyboard::TertiaryModifier
= GDK_SHIFT_MASK
; // Shift
63 guint
Keyboard::Level4Modifier
= GDK_CONTROL_MASK
; // Control
64 guint
Keyboard::CopyModifier
= GDK_MOD1_MASK
; // Alt/Option
65 guint
Keyboard::RangeSelectModifier
= GDK_SHIFT_MASK
;
66 guint
Keyboard::button2_modifiers
= Keyboard::SecondaryModifier
|Keyboard::Level4Modifier
;
68 const char* Keyboard::primary_modifier_name() { return _("Command"); }
69 const char* Keyboard::secondary_modifier_name() { return _("Option"); }
70 const char* Keyboard::tertiary_modifier_name() { return _("Shift"); }
71 const char* Keyboard::level4_modifier_name() { return _("Control"); }
72 const char* Keyboard::copy_modifier_name() { return _("Mod1"; ); }
73 const char* Keyboard::rangeselect_modifier_name() { return _("Shift"); }
77 guint
Keyboard::PrimaryModifier
= GDK_CONTROL_MASK
; // Control
78 guint
Keyboard::SecondaryModifier
= GDK_MOD1_MASK
; // Alt/Option
79 guint
Keyboard::TertiaryModifier
= GDK_SHIFT_MASK
; // Shift
80 guint
Keyboard::Level4Modifier
= GDK_MOD4_MASK
; // Mod4/Windows
81 guint
Keyboard::CopyModifier
= GDK_CONTROL_MASK
;
82 guint
Keyboard::RangeSelectModifier
= GDK_SHIFT_MASK
;
83 guint
Keyboard::button2_modifiers
= 0; /* not used */
85 const char* Keyboard::primary_modifier_name() { return _("Control"); }
86 const char* Keyboard::secondary_modifier_name() { return _("Alt"); }
87 const char* Keyboard::tertiary_modifier_name() { return _("Shift"); }
88 const char* Keyboard::level4_modifier_name() { return _("Meta"); }
89 const char* Keyboard::copy_modifier_name() { return _("Control"); }
90 const char* Keyboard::rangeselect_modifier_name() { return _("Shift"); }
94 Keyboard
* Keyboard::_the_keyboard
= 0;
95 Gtk::Window
* Keyboard::current_window
= 0;
96 bool Keyboard::_some_magic_widget_has_focus
= false;
98 std::string
Keyboard::user_keybindings_path
;
99 bool Keyboard::can_save_keybindings
= false;
100 bool Keyboard::bindings_changed_after_save_became_legal
= false;
101 map
<string
,string
> Keyboard::binding_files
;
102 string
Keyboard::_current_binding_name
;
103 map
<AccelKey
,pair
<string
,string
>,Keyboard::AccelKeyLess
> Keyboard::release_keys
;
105 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
107 GdkModifierType
Keyboard::RelevantModifierKeyMask
;
110 Keyboard::magic_widget_grab_focus ()
112 _some_magic_widget_has_focus
= true;
116 Keyboard::magic_widget_drop_focus ()
118 _some_magic_widget_has_focus
= false;
122 Keyboard::some_magic_widget_has_focus ()
124 return _some_magic_widget_has_focus
;
127 Keyboard::Keyboard ()
129 if (_the_keyboard
== 0) {
130 _the_keyboard
= this;
131 _current_binding_name
= _("Unknown");
134 RelevantModifierKeyMask
= (GdkModifierType
) gtk_accelerator_get_default_mod_mask ();
136 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
| PrimaryModifier
);
137 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
| SecondaryModifier
);
138 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
| TertiaryModifier
);
139 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
| Level4Modifier
);
140 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
| CopyModifier
);
141 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
| RangeSelectModifier
);
143 gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask
);
145 snooper_id
= gtk_key_snooper_install (_snooper
, (gpointer
) this);
148 Keyboard::~Keyboard ()
150 gtk_key_snooper_remove (snooper_id
);
154 Keyboard::get_state (void)
156 XMLNode
* node
= new XMLNode ("Keyboard");
159 snprintf (buf
, sizeof (buf
), "%d", edit_but
);
160 node
->add_property ("edit-button", buf
);
161 snprintf (buf
, sizeof (buf
), "%d", edit_mod
);
162 node
->add_property ("edit-modifier", buf
);
163 snprintf (buf
, sizeof (buf
), "%d", delete_but
);
164 node
->add_property ("delete-button", buf
);
165 snprintf (buf
, sizeof (buf
), "%d", delete_mod
);
166 node
->add_property ("delete-modifier", buf
);
167 snprintf (buf
, sizeof (buf
), "%d", snap_mod
);
168 node
->add_property ("snap-modifier", buf
);
169 snprintf (buf
, sizeof (buf
), "%d", insert_note_but
);
170 node
->add_property ("insert-note-button", buf
);
171 snprintf (buf
, sizeof (buf
), "%d", insert_note_mod
);
172 node
->add_property ("insert-note-modifier", buf
);
178 Keyboard::set_state (const XMLNode
& node
, int /*version*/)
180 const XMLProperty
* prop
;
182 if ((prop
= node
.property ("edit-button")) != 0) {
183 sscanf (prop
->value().c_str(), "%d", &edit_but
);
186 if ((prop
= node
.property ("edit-modifier")) != 0) {
187 sscanf (prop
->value().c_str(), "%d", &edit_mod
);
190 if ((prop
= node
.property ("delete-button")) != 0) {
191 sscanf (prop
->value().c_str(), "%d", &delete_but
);
194 if ((prop
= node
.property ("delete-modifier")) != 0) {
195 sscanf (prop
->value().c_str(), "%d", &delete_mod
);
198 if ((prop
= node
.property ("snap-modifier")) != 0) {
199 sscanf (prop
->value().c_str(), "%d", &snap_mod
);
202 if ((prop
= node
.property ("insert-note-button")) != 0) {
203 sscanf (prop
->value().c_str(), "%d", &insert_note_but
);
206 if ((prop
= node
.property ("insert-note-modifier")) != 0) {
207 sscanf (prop
->value().c_str(), "%d", &insert_note_mod
);
214 Keyboard::_snooper (GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
216 return ((Keyboard
*) data
)->snooper (widget
, event
);
220 Keyboard::snooper (GtkWidget
*widget
, GdkEventKey
*event
)
228 "Snoop widget %1 key %2 type %3 state %4 magic %5\n",
229 widget
, event
->keyval
, event
->type
, event
->state
, _some_magic_widget_has_focus
233 if (event
->keyval
== GDK_Shift_R
) {
234 keyval
= GDK_Shift_L
;
236 } else if (event
->keyval
== GDK_Control_R
) {
237 keyval
= GDK_Control_L
;
240 keyval
= event
->keyval
;
243 if (event
->type
== GDK_KEY_PRESS
) {
245 if (find (state
.begin(), state
.end(), keyval
) == state
.end()) {
246 state
.push_back (keyval
);
247 sort (state
.begin(), state
.end());
251 /* key is already down. if its also used for release,
252 prevent auto-repeat events.
255 for (map
<AccelKey
,two_strings
,AccelKeyLess
>::iterator k
= release_keys
.begin(); k
!= release_keys
.end(); ++k
) {
257 const AccelKey
& ak (k
->first
);
259 if (keyval
== ak
.get_key() && (Gdk::ModifierType
)((event
->state
& Keyboard::RelevantModifierKeyMask
) | Gdk::RELEASE_MASK
) == ak
.get_mod()) {
260 DEBUG_TRACE (DEBUG::Keyboard
, "Suppress auto repeat\n");
267 } else if (event
->type
== GDK_KEY_RELEASE
) {
271 if ((i
= find (state
.begin(), state
.end(), keyval
)) != state
.end()) {
273 sort (state
.begin(), state
.end());
276 for (map
<AccelKey
,two_strings
,AccelKeyLess
>::iterator k
= release_keys
.begin(); k
!= release_keys
.end(); ++k
) {
278 const AccelKey
& ak (k
->first
);
279 two_strings
ts (k
->second
);
281 if (keyval
== ak
.get_key() && (Gdk::ModifierType
)((event
->state
& Keyboard::RelevantModifierKeyMask
) | Gdk::RELEASE_MASK
) == ak
.get_mod()) {
282 Glib::RefPtr
<Gtk::Action
> act
= ActionManager::get_action (ts
.first
.c_str(), ts
.second
.c_str());
284 DEBUG_TRACE (DEBUG::Keyboard
, string_compose ("Activate %1 %2\n", ts
.first
, ts
.second
));
286 DEBUG_TRACE (DEBUG::Keyboard
, string_compose ("Use repeat, suppress other\n", ts
.first
, ts
.second
));
294 /* Special keys that we want to handle in
295 any dialog, no matter whether it uses
296 the regular set of accelerators or not
299 if (event
->type
== GDK_KEY_RELEASE
&& modifier_state_equals (event
->state
, PrimaryModifier
)) {
300 switch (event
->keyval
) {
302 if (current_window
) {
303 current_window
->hide ();
315 Keyboard::key_is_down (uint32_t keyval
)
317 return find (state
.begin(), state
.end(), keyval
) != state
.end();
321 Keyboard::enter_window (GdkEventCrossing
*, Gtk::Window
* win
)
323 current_window
= win
;
328 Keyboard::leave_window (GdkEventCrossing
*ev
, Gtk::Window
* /*win*/)
331 switch (ev
->detail
) {
332 case GDK_NOTIFY_INFERIOR
:
333 DEBUG_TRACE (DEBUG::Keyboard
, "INFERIOR crossing ... out\n");
336 case GDK_NOTIFY_VIRTUAL
:
337 DEBUG_TRACE (DEBUG::Keyboard
, "VIRTUAL crossing ... out\n");
341 DEBUG_TRACE (DEBUG::Keyboard
, "REAL crossing ... out\n");
342 DEBUG_TRACE (DEBUG::Keyboard
, "Clearing current target\n");
354 Keyboard::set_edit_button (guint but
)
360 Keyboard::set_edit_modifier (guint mod
)
362 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
& ~edit_mod
);
364 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
| edit_mod
);
368 Keyboard::set_delete_button (guint but
)
374 Keyboard::set_delete_modifier (guint mod
)
376 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
& ~delete_mod
);
378 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
| delete_mod
);
382 Keyboard::set_insert_note_button (guint but
)
384 insert_note_but
= but
;
388 Keyboard::set_insert_note_modifier (guint mod
)
390 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
& ~insert_note_mod
);
391 insert_note_mod
= mod
;
392 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
| insert_note_mod
);
397 Keyboard::set_modifier (uint32_t newval
, uint32_t& var
)
399 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
& ~var
);
401 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
| var
);
405 Keyboard::set_snap_modifier (guint mod
)
407 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
& ~snap_mod
);
409 RelevantModifierKeyMask
= GdkModifierType (RelevantModifierKeyMask
| snap_mod
);
413 Keyboard::is_edit_event (GdkEventButton
*ev
)
415 return (ev
->type
== GDK_BUTTON_PRESS
|| ev
->type
== GDK_BUTTON_RELEASE
) &&
416 (ev
->button
== Keyboard::edit_button()) &&
417 ((ev
->state
& RelevantModifierKeyMask
) == Keyboard::edit_modifier());
421 Keyboard::is_insert_note_event (GdkEventButton
*ev
)
423 return (ev
->type
== GDK_BUTTON_PRESS
|| ev
->type
== GDK_BUTTON_RELEASE
) &&
424 (ev
->button
== Keyboard::insert_note_button()) &&
425 ((ev
->state
& RelevantModifierKeyMask
) == Keyboard::insert_note_modifier());
429 Keyboard::is_button2_event (GdkEventButton
* ev
)
432 return (ev
->button
== 2) ||
433 ((ev
->button
== 1) &&
434 ((ev
->state
& Keyboard::button2_modifiers
) == Keyboard::button2_modifiers
));
436 return ev
->button
== 2;
441 Keyboard::is_delete_event (GdkEventButton
*ev
)
443 return (ev
->type
== GDK_BUTTON_PRESS
|| ev
->type
== GDK_BUTTON_RELEASE
) &&
444 (ev
->button
== Keyboard::delete_button()) &&
445 ((ev
->state
& RelevantModifierKeyMask
) == Keyboard::delete_modifier());
449 Keyboard::is_context_menu_event (GdkEventButton
*ev
)
451 return (ev
->type
== GDK_BUTTON_PRESS
|| ev
->type
== GDK_BUTTON_RELEASE
) &&
453 ((ev
->state
& RelevantModifierKeyMask
) == 0);
457 Keyboard::no_modifiers_active (guint state
)
459 return (state
& RelevantModifierKeyMask
) == 0;
463 Keyboard::modifier_state_contains (guint state
, ModifierMask mask
)
465 return (state
& mask
) == (guint
) mask
;
469 Keyboard::modifier_state_equals (guint state
, ModifierMask mask
)
471 return (state
& RelevantModifierKeyMask
) == (guint
) mask
;
475 Keyboard::keybindings_changed ()
477 if (Keyboard::can_save_keybindings
) {
478 Keyboard::bindings_changed_after_save_became_legal
= true;
481 Keyboard::save_keybindings ();
485 Keyboard::set_can_save_keybindings (bool yn
)
487 can_save_keybindings
= yn
;
491 Keyboard::save_keybindings ()
493 if (can_save_keybindings
&& bindings_changed_after_save_became_legal
) {
494 Gtk::AccelMap::save (user_keybindings_path
);
499 Keyboard::load_keybindings (string path
)
502 info
<< "Loading bindings from " << path
<< endl
;
504 Gtk::AccelMap::load (path
);
506 _current_binding_name
= _("Unknown");
508 for (map
<string
,string
>::iterator x
= binding_files
.begin(); x
!= binding_files
.end(); ++x
) {
509 if (path
== x
->second
) {
510 _current_binding_name
= x
->first
;
517 error
<< string_compose (_("Ardour key bindings file not found at \"%1\" or contains errors."), path
)
522 /* now find all release-driven bindings */
524 vector
<string
> groups
;
525 vector
<string
> names
;
526 vector
<string
> tooltips
;
527 vector
<AccelKey
> bindings
;
529 ActionManager::get_all_actions (groups
, names
, tooltips
, bindings
);
531 vector
<string
>::iterator g
;
532 vector
<AccelKey
>::iterator b
;
533 vector
<string
>::iterator n
;
535 release_keys
.clear ();
537 for (n
= names
.begin(), b
= bindings
.begin(), g
= groups
.begin(); n
!= names
.end(); ++n
, ++b
, ++g
) {
539 s
<< "Action: " << *n
<< " Group: " << *g
<< " Binding: ";
541 if ((*b
).get_key() != GDK_VoidSymbol
) {
542 s
<< b
->get_key() << " w/mod " << hex
<< b
->get_mod() << dec
<< " = " << b
->get_abbrev () << "\n";
547 DEBUG_TRACE (DEBUG::Bindings
, s
.str ());
550 for (n
= names
.begin(), b
= bindings
.begin(), g
= groups
.begin(); n
!= names
.end(); ++n
, ++b
, ++g
) {
551 if ((*b
).get_mod() & Gdk::RELEASE_MASK
) {
552 release_keys
.insert (pair
<AccelKey
,two_strings
> (*b
, two_strings (*g
, *n
)));