change charset for it_IT
[ardour2.git] / gtk2_ardour / keyboard.cc
blobdd153873500ba807678ea2ea2ff37dcce784729f
1 /*
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 <vector>
21 #include <ardour/ardour.h>
23 #include "ardour_ui.h"
25 #include <algorithm>
26 #include <fstream>
27 #include <iostream>
29 #include <ctype.h>
31 #include <gtkmm/accelmap.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <pbd/error.h>
36 #include "keyboard.h"
37 #include "gui_thread.h"
38 #include "opts.h"
39 #include "actions.h"
41 #include "i18n.h"
43 using namespace PBD;
44 using namespace ARDOUR;
45 using namespace Gtk;
46 using namespace std;
48 #define KBD_DEBUG 0
49 bool debug_keyboard = false;
51 guint Keyboard::edit_but = 3;
52 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
53 guint Keyboard::delete_but = 3;
54 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
55 guint Keyboard::snap_mod = GDK_MOD3_MASK;
57 #ifdef GTKOSX
58 guint Keyboard::PrimaryModifier = GDK_META_MASK; // Command
59 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK; // Alt/Option
60 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
61 guint Keyboard::Level4Modifier = GDK_CONTROL_MASK; // Control
62 guint Keyboard::CopyModifier = GDK_MOD1_MASK; // Alt/Option
63 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
64 guint Keyboard::button2_modifiers = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
65 #else
66 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
67 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK; // Alt/Option
68 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
69 guint Keyboard::Level4Modifier = GDK_MOD4_MASK; // Mod4/Windows
70 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;
71 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
72 guint Keyboard::button2_modifiers = 0; /* not used */
73 #endif
76 Keyboard* Keyboard::_the_keyboard = 0;
77 Gtk::Window* Keyboard::current_window = 0;
78 bool Keyboard::_some_magic_widget_has_focus = false;
80 std::string Keyboard::user_keybindings_path;
81 bool Keyboard::can_save_keybindings = false;
82 bool Keyboard::bindings_changed_after_save_became_legal = false;
83 map<string,string> Keyboard::binding_files;
84 string Keyboard::_current_binding_name = _("Unknown");
85 map<AccelKey,pair<string,string>,Keyboard::AccelKeyLess> Keyboard::release_keys;
87 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
89 GdkModifierType Keyboard::RelevantModifierKeyMask;
91 void
92 Keyboard::magic_widget_grab_focus ()
94 _some_magic_widget_has_focus = true;
97 void
98 Keyboard::magic_widget_drop_focus ()
100 _some_magic_widget_has_focus = false;
103 bool
104 Keyboard::some_magic_widget_has_focus ()
106 return _some_magic_widget_has_focus;
109 Keyboard::Keyboard ()
111 if (_the_keyboard == 0) {
112 _the_keyboard = this;
115 RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
117 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
118 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
119 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
120 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | Level4Modifier);
121 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
122 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
124 gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
126 snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
128 XMLNode* node = ARDOUR_UI::instance()->keyboard_settings();
129 set_state (*node);
132 Keyboard::~Keyboard ()
134 gtk_key_snooper_remove (snooper_id);
137 XMLNode&
138 Keyboard::get_state (void)
140 XMLNode* node = new XMLNode ("Keyboard");
141 char buf[32];
143 snprintf (buf, sizeof (buf), "%d", edit_but);
144 node->add_property ("edit-button", buf);
145 snprintf (buf, sizeof (buf), "%d", edit_mod);
146 node->add_property ("edit-modifier", buf);
147 snprintf (buf, sizeof (buf), "%d", delete_but);
148 node->add_property ("delete-button", buf);
149 snprintf (buf, sizeof (buf), "%d", delete_mod);
150 node->add_property ("delete-modifier", buf);
151 snprintf (buf, sizeof (buf), "%d", snap_mod);
152 node->add_property ("snap-modifier", buf);
154 return *node;
157 int
158 Keyboard::set_state (const XMLNode& node)
160 const XMLProperty* prop;
162 if ((prop = node.property ("edit-button")) != 0) {
163 sscanf (prop->value().c_str(), "%d", &edit_but);
166 if ((prop = node.property ("edit-modifier")) != 0) {
167 sscanf (prop->value().c_str(), "%d", &edit_mod);
170 if ((prop = node.property ("delete-button")) != 0) {
171 sscanf (prop->value().c_str(), "%d", &delete_but);
174 if ((prop = node.property ("delete-modifier")) != 0) {
175 sscanf (prop->value().c_str(), "%d", &delete_mod);
178 if ((prop = node.property ("snap-modifier")) != 0) {
179 sscanf (prop->value().c_str(), "%d", &snap_mod);
182 return 0;
185 gint
186 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
188 return ((Keyboard *) data)->snooper (widget, event);
191 gint
192 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
194 uint32_t keyval;
195 bool ret = false;
197 #if 0
198 cerr << "snoop widget " << widget << " key " << event->keyval << " type: " << event->type
199 << " state " << std::hex << event->state << std::dec
200 << endl;
201 #endif
203 #if KBD_DEBUG
204 if (debug_keyboard) {
205 cerr << "snoop widget " << widget << " key " << event->keyval << " type: " << event->type
206 << endl;
208 #endif
210 if (event->keyval == GDK_Shift_R) {
211 keyval = GDK_Shift_L;
213 } else if (event->keyval == GDK_Control_R) {
214 keyval = GDK_Control_L;
216 } else {
217 keyval = event->keyval;
220 if (event->type == GDK_KEY_PRESS) {
222 if (find (state.begin(), state.end(), keyval) == state.end()) {
223 state.push_back (keyval);
224 sort (state.begin(), state.end());
226 } else {
228 /* key is already down. if its also used for release,
229 prevent auto-repeat events.
232 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
234 const AccelKey& ak (k->first);
236 if (keyval == ak.get_key() && (Gdk::ModifierType)(event->state | Gdk::RELEASE_MASK) == ak.get_mod()) {
237 ret = true;
238 break;
243 } else if (event->type == GDK_KEY_RELEASE) {
245 State::iterator i;
247 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
248 state.erase (i);
249 sort (state.begin(), state.end());
252 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
254 const AccelKey& ak (k->first);
255 two_strings ts (k->second);
257 if (keyval == ak.get_key() && (Gdk::ModifierType)(event->state | Gdk::RELEASE_MASK) == ak.get_mod()) {
258 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (ts.first.c_str(), ts.second.c_str());
259 if (act) {
260 act->activate();
261 ret = true;
263 break;
268 /* Special keys that we want to handle in
269 any dialog, no matter whether it uses
270 the regular set of accelerators or not
273 if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
274 switch (event->keyval) {
275 case GDK_w:
276 if (current_window) {
277 current_window->hide ();
278 current_window = 0;
279 ret = true;
281 break;
285 return ret;
288 bool
289 Keyboard::key_is_down (uint32_t keyval)
291 return find (state.begin(), state.end(), keyval) != state.end();
294 bool
295 Keyboard::enter_window (GdkEventCrossing *ev, Gtk::Window* win)
297 current_window = win;
298 return false;
301 bool
302 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* win)
304 if (ev) {
305 switch (ev->detail) {
306 case GDK_NOTIFY_INFERIOR:
307 if (debug_keyboard) {
308 cerr << "INFERIOR crossing ... out\n";
310 break;
312 case GDK_NOTIFY_VIRTUAL:
313 if (debug_keyboard) {
314 cerr << "VIRTUAL crossing ... out\n";
316 /* fallthru */
318 default:
319 if (debug_keyboard) {
320 cerr << "REAL CROSSING ... out\n";
321 cerr << "clearing current target\n";
323 state.clear ();
324 current_window = 0;
326 } else {
327 current_window = 0;
330 return false;
333 void
334 Keyboard::set_edit_button (guint but)
336 edit_but = but;
339 void
340 Keyboard::set_edit_modifier (guint mod)
342 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
343 edit_mod = mod;
344 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
347 void
348 Keyboard::set_delete_button (guint but)
350 delete_but = but;
353 void
354 Keyboard::set_delete_modifier (guint mod)
356 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
357 delete_mod = mod;
358 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
361 void
362 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
364 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
365 var = newval;
366 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
369 void
370 Keyboard::set_snap_modifier (guint mod)
372 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
373 snap_mod = mod;
374 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
377 bool
378 Keyboard::is_edit_event (GdkEventButton *ev)
380 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
381 (ev->button == Keyboard::edit_button()) &&
382 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
385 bool
386 Keyboard::is_button2_event (GdkEventButton* ev)
388 #ifdef GTKOSX
389 return (ev->button == 2) ||
390 ((ev->button == 1) &&
391 ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
392 #else
393 return ev->button == 2;
394 #endif
397 bool
398 Keyboard::is_delete_event (GdkEventButton *ev)
400 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
401 (ev->button == Keyboard::delete_button()) &&
402 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
405 bool
406 Keyboard::is_context_menu_event (GdkEventButton *ev)
408 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
409 (ev->button == 3) &&
410 ((ev->state & RelevantModifierKeyMask) == 0);
413 bool
414 Keyboard::no_modifiers_active (guint state)
416 return (state & RelevantModifierKeyMask) == 0;
419 bool
420 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
422 return (state & mask) == (guint) mask;
425 bool
426 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
428 return (state & RelevantModifierKeyMask) == (guint) mask;
431 Selection::Operation
432 Keyboard::selection_type (guint state)
434 /* note that there is no modifier for "Add" */
436 if (modifier_state_equals (state, RangeSelectModifier)) {
437 return Selection::Extend;
438 } else if (modifier_state_equals (state, PrimaryModifier)) {
439 return Selection::Toggle;
440 } else {
441 return Selection::Set;
446 static void
447 accel_map_changed (GtkAccelMap* map,
448 gchar* path,
449 guint key,
450 GdkModifierType mod,
451 gpointer arg)
453 Keyboard::keybindings_changed ();
456 void
457 Keyboard::keybindings_changed ()
459 if (Keyboard::can_save_keybindings) {
460 Keyboard::bindings_changed_after_save_became_legal = true;
463 Keyboard::save_keybindings ();
466 void
467 Keyboard::set_can_save_keybindings (bool yn)
469 can_save_keybindings = yn;
472 void
473 Keyboard::save_keybindings ()
475 if (can_save_keybindings && bindings_changed_after_save_became_legal) {
476 Gtk::AccelMap::save (user_keybindings_path);
480 void
481 Keyboard::setup_keybindings ()
483 using namespace ARDOUR_COMMAND_LINE;
484 std::string default_bindings = "mnemonic-us.bindings";
485 std::string path;
486 vector<string> strs;
488 binding_files.clear ();
490 ARDOUR::find_bindings_files (binding_files);
492 /* set up the per-user bindings path */
494 strs.push_back (Glib::get_home_dir());
495 strs.push_back (".ardour2");
496 strs.push_back ("ardour.bindings");
498 user_keybindings_path = Glib::build_filename (strs);
500 if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
501 std::pair<string,string> newpair;
502 newpair.first = _("your own");
503 newpair.second = user_keybindings_path;
504 binding_files.insert (newpair);
507 /* check to see if they gave a style name ("SAE", "ergonomic") or
508 an actual filename (*.bindings)
511 if (!keybindings_path.empty() && keybindings_path.find (".bindings") == string::npos) {
513 // just a style name - allow user to
514 // specify the layout type.
516 char* layout;
518 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
520 /* user-specified keyboard layout */
522 keybindings_path += '-';
523 keybindings_path += layout;
525 } else {
527 /* default to US/ANSI - we have to pick something */
529 keybindings_path += "-us";
532 keybindings_path += ".bindings";
535 if (keybindings_path.empty()) {
537 /* no path or binding name given: check the user one first */
539 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
541 keybindings_path = "";
543 } else {
545 keybindings_path = user_keybindings_path;
549 /* if we still don't have a path at this point, use the default */
551 if (keybindings_path.empty()) {
552 keybindings_path = default_bindings;
555 while (true) {
557 if (!Glib::path_is_absolute (keybindings_path)) {
559 /* not absolute - look in the usual places */
561 path = find_config_file (keybindings_path);
563 if (path.empty()) {
565 if (keybindings_path == default_bindings) {
566 error << _("Default keybindings not found - Ardour will be hard to use!") << endmsg;
567 return;
568 } else {
569 warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
570 keybindings_path)
571 << endmsg;
572 keybindings_path = default_bindings;
575 } else {
577 /* use it */
579 keybindings_path = path;
580 break;
584 } else {
586 /* path is absolute already */
588 if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
589 if (keybindings_path == default_bindings) {
590 error << _("Default keybindings not found - Ardour will be hard to use!") << endmsg;
591 return;
592 } else {
593 warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
594 keybindings_path)
595 << endmsg;
596 keybindings_path = default_bindings;
599 } else {
600 break;
605 load_keybindings (keybindings_path);
607 /* catch changes */
609 GtkAccelMap* accelmap = gtk_accel_map_get();
610 g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, 0);
613 bool
614 Keyboard::load_keybindings (string path)
616 try {
617 cerr << "loading bindings from " << path << endl;
619 Gtk::AccelMap::load (path);
621 _current_binding_name = _("Unknown");
623 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
624 if (path == x->second) {
625 _current_binding_name = x->first;
626 break;
631 } catch (...) {
632 error << string_compose (_("Ardour key bindings file not found at \"%1\" or contains errors."), path)
633 << endmsg;
634 return false;
637 /* now find all release-driven bindings */
639 vector<string> groups;
640 vector<string> names;
641 vector<AccelKey> bindings;
643 ActionManager::get_all_actions (groups, names, bindings);
645 vector<string>::iterator g;
646 vector<AccelKey>::iterator b;
647 vector<string>::iterator n;
649 release_keys.clear ();
651 bool show_bindings = (getenv ("ARDOUR_SHOW_BINDINGS") != 0);
653 for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
655 if (show_bindings) {
657 cerr << "Action: " << (*n) << " Group: " << (*g) << " binding = ";
659 if ((*b).get_key() != GDK_VoidSymbol) {
660 cerr << (*b).get_key() << " w/mod = " << hex << (*b).get_mod() << dec << " = " << (*b).get_abbrev();
661 } else {
662 cerr << "unbound";
665 cerr << endl;
668 if ((*b).get_mod() & Gdk::RELEASE_MASK) {
669 release_keys.insert (pair<AccelKey,two_strings> (*b, two_strings (*g, *n)));
673 return true;