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.
20 #include <ardour/ardour.h>
22 #include "ardour_ui.h"
30 #include <gtkmm/accelmap.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <pbd/error.h>
36 #include "gui_thread.h"
42 using namespace ARDOUR;
45 bool debug_keyboard = false;
47 guint Keyboard::edit_but = 3;
48 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
49 guint Keyboard::delete_but = 3;
50 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
51 guint Keyboard::snap_mod = GDK_MOD3_MASK;
54 guint Keyboard::PrimaryModifier = GDK_META_MASK; // Command
55 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK; // Alt/Option
56 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
57 guint Keyboard::CopyModifier = GDK_MOD1_MASK; // Alt/Option
58 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
60 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
61 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK; // Alt/Option
62 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
63 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;
64 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
67 Keyboard* Keyboard::_the_keyboard = 0;
68 Gtk::Window* Keyboard::current_window = 0;
69 bool Keyboard::_some_magic_widget_has_focus = false;
71 std::string Keyboard::user_keybindings_path;
72 bool Keyboard::can_save_keybindings = false;
73 map<string,string> Keyboard::binding_files;
74 std::string Keyboard::_current_binding_name = _("Unknown");
76 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
78 GdkModifierType Keyboard::RelevantModifierKeyMask;
81 Keyboard::magic_widget_grab_focus ()
83 _some_magic_widget_has_focus = true;
87 Keyboard::magic_widget_drop_focus ()
89 _some_magic_widget_has_focus = false;
93 Keyboard::some_magic_widget_has_focus ()
95 return _some_magic_widget_has_focus;
100 if (_the_keyboard == 0) {
101 _the_keyboard = this;
104 RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
106 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
107 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
108 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
109 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
110 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
112 snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
114 XMLNode* node = ARDOUR_UI::instance()->keyboard_settings();
118 Keyboard::~Keyboard ()
120 gtk_key_snooper_remove (snooper_id);
124 Keyboard::get_state (void)
126 XMLNode* node = new XMLNode ("Keyboard");
129 snprintf (buf, sizeof (buf), "%d", edit_but);
130 node->add_property ("edit-button", buf);
131 snprintf (buf, sizeof (buf), "%d", edit_mod);
132 node->add_property ("edit-modifier", buf);
133 snprintf (buf, sizeof (buf), "%d", delete_but);
134 node->add_property ("delete-button", buf);
135 snprintf (buf, sizeof (buf), "%d", delete_mod);
136 node->add_property ("delete-modifier", buf);
137 snprintf (buf, sizeof (buf), "%d", snap_mod);
138 node->add_property ("snap-modifier", buf);
144 Keyboard::set_state (const XMLNode& node)
146 const XMLProperty* prop;
148 if ((prop = node.property ("edit-button")) != 0) {
149 sscanf (prop->value().c_str(), "%d", &edit_but);
152 if ((prop = node.property ("edit-modifier")) != 0) {
153 sscanf (prop->value().c_str(), "%d", &edit_mod);
156 if ((prop = node.property ("delete-button")) != 0) {
157 sscanf (prop->value().c_str(), "%d", &delete_but);
160 if ((prop = node.property ("delete-modifier")) != 0) {
161 sscanf (prop->value().c_str(), "%d", &delete_mod);
164 if ((prop = node.property ("snap-modifier")) != 0) {
165 sscanf (prop->value().c_str(), "%d", &snap_mod);
172 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
174 return ((Keyboard *) data)->snooper (widget, event);
178 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
183 cerr << "snoop widget " << widget << " key " << event->keyval << " type: " << event->type
184 << " state " << std::hex << event->state << std::dec
189 if (debug_keyboard) {
190 cerr << "snoop widget " << widget << " key " << event->keyval << " type: " << event->type
195 if (event->keyval == GDK_Shift_R) {
196 keyval = GDK_Shift_L;
198 } else if (event->keyval == GDK_Control_R) {
199 keyval = GDK_Control_L;
202 keyval = event->keyval;
205 if (event->type == GDK_KEY_PRESS) {
207 if (find (state.begin(), state.end(), keyval) == state.end()) {
208 state.push_back (keyval);
209 sort (state.begin(), state.end());
212 } else if (event->type == GDK_KEY_RELEASE) {
216 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
218 sort (state.begin(), state.end());
223 if (event->type == GDK_KEY_RELEASE && event->keyval == GDK_w && modifier_state_equals (event->state, PrimaryModifier)) {
224 if (current_window) {
225 current_window->hide ();
234 Keyboard::key_is_down (uint32_t keyval)
236 return find (state.begin(), state.end(), keyval) != state.end();
240 Keyboard::enter_window (GdkEventCrossing *ev, Gtk::Window* win)
242 current_window = win;
247 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* win)
249 switch (ev->detail) {
250 case GDK_NOTIFY_INFERIOR:
251 if (debug_keyboard) {
252 cerr << "INFERIOR crossing ... out\n";
256 case GDK_NOTIFY_VIRTUAL:
257 if (debug_keyboard) {
258 cerr << "VIRTUAL crossing ... out\n";
263 if (debug_keyboard) {
264 cerr << "REAL CROSSING ... out\n";
265 cerr << "clearing current target\n";
275 Keyboard::set_edit_button (guint but)
281 Keyboard::set_edit_modifier (guint mod)
283 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
285 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
289 Keyboard::set_delete_button (guint but)
295 Keyboard::set_delete_modifier (guint mod)
297 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
299 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
303 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
305 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
307 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
311 Keyboard::set_snap_modifier (guint mod)
313 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
315 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
319 Keyboard::is_edit_event (GdkEventButton *ev)
322 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
323 (ev->button == Keyboard::edit_button()) &&
324 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
328 Keyboard::is_delete_event (GdkEventButton *ev)
330 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
331 (ev->button == Keyboard::delete_button()) &&
332 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
336 Keyboard::is_context_menu_event (GdkEventButton *ev)
338 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
340 ((ev->state & RelevantModifierKeyMask) == 0);
344 Keyboard::no_modifiers_active (guint state)
346 return (state & RelevantModifierKeyMask) == 0;
350 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
352 return (state & mask) == (guint) mask;
356 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
358 return (state & RelevantModifierKeyMask) == (guint) mask;
362 Keyboard::selection_type (guint state)
364 /* note that there is no modifier for "Add" */
366 if (modifier_state_equals (state, RangeSelectModifier)) {
367 return Selection::Extend;
368 } else if (modifier_state_equals (state, PrimaryModifier)) {
369 return Selection::Toggle;
371 return Selection::Set;
377 accel_map_changed (GtkAccelMap* map,
383 Keyboard::save_keybindings ();
387 Keyboard::set_can_save_keybindings (bool yn)
389 can_save_keybindings = yn;
393 Keyboard::save_keybindings ()
395 if (can_save_keybindings) {
396 Gtk::AccelMap::save (user_keybindings_path);
401 Keyboard::setup_keybindings ()
403 using namespace ARDOUR_COMMAND_LINE;
404 std::string default_bindings = "mnemonic-us.bindings";
408 ARDOUR::find_bindings_files (binding_files);
410 /* set up the per-user bindings path */
412 strs.push_back (Glib::get_home_dir());
413 strs.push_back (".ardour2");
414 strs.push_back ("ardour.bindings");
416 user_keybindings_path = Glib::build_filename (strs);
418 /* check to see if they gave a style name ("SAE", "ergonomic") or
419 an actual filename (*.bindings)
422 if (!keybindings_path.empty() && keybindings_path.find (".bindings") == string::npos) {
424 // just a style name - allow user to
425 // specify the layout type.
429 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
431 /* user-specified keyboard layout */
433 keybindings_path += '-';
434 keybindings_path += layout;
438 /* default to US/ANSI - we have to pick something */
440 keybindings_path += "-us";
443 keybindings_path += ".bindings";
446 if (keybindings_path.empty()) {
448 /* no path or binding name given: check the user one first */
450 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
452 keybindings_path = "";
456 keybindings_path = user_keybindings_path;
460 /* if we still don't have a path at this point, use the default */
462 if (keybindings_path.empty()) {
463 keybindings_path = default_bindings;
468 if (!Glib::path_is_absolute (keybindings_path)) {
470 /* not absolute - look in the usual places */
472 path = find_config_file (keybindings_path);
476 if (keybindings_path == default_bindings) {
477 error << _("Default keybindings not found - Ardour will be hard to use!") << endmsg;
480 warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
483 keybindings_path = default_bindings;
490 keybindings_path = path;
497 /* path is absolute already */
499 if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
500 if (keybindings_path == default_bindings) {
501 error << _("Default keybindings not found - Ardour will be hard to use!") << endmsg;
504 warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
507 keybindings_path = default_bindings;
516 load_keybindings (keybindings_path);
520 GtkAccelMap* accelmap = gtk_accel_map_get();
521 g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, 0);
525 Keyboard::load_keybindings (string path)
528 cerr << "loading bindings from " << path << endl;
530 Gtk::AccelMap::load (path);
532 _current_binding_name = _("Unknown");
534 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
535 if (path == x->second) {
536 _current_binding_name = x->first;
544 error << string_compose (_("Ardour key bindings file not found at \"%1\" or contains errors."), path)