diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2008-02-14 01:59:28 +0000 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2008-02-14 01:59:28 +0000 |
commit | 26bffbce44cfb10286aa5bc353c4e02e95f6f839 (patch) | |
tree | c1945974bea592cc03a1dab2efbc52dfebd826d4 /gtk2_ardour/keyboard.cc | |
parent | 3e50efd13323a000af70bb2639927685fc4f1ba9 (diff) |
dynamic discovery & loading of keybindings
git-svn-id: svn://localhost/ardour2/branches/2.0-ongoing@3055 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'gtk2_ardour/keyboard.cc')
-rw-r--r-- | gtk2_ardour/keyboard.cc | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/gtk2_ardour/keyboard.cc b/gtk2_ardour/keyboard.cc index 549b601ec2..75c6dc1e1d 100644 --- a/gtk2_ardour/keyboard.cc +++ b/gtk2_ardour/keyboard.cc @@ -17,6 +17,8 @@ */ +#include <ardour/ardour.h> + #include "ardour_ui.h" #include <algorithm> @@ -25,15 +27,19 @@ #include <ctype.h> +#include <gtkmm/accelmap.h> + #include <gdk/gdkkeysyms.h> #include <pbd/error.h> #include "keyboard.h" #include "gui_thread.h" +#include "opts.h" #include "i18n.h" using namespace PBD; +using namespace ARDOUR; #define KBD_DEBUG 0 bool debug_keyboard = false; @@ -62,6 +68,11 @@ Keyboard* Keyboard::_the_keyboard = 0; Gtk::Window* Keyboard::current_window = 0; bool Keyboard::_some_magic_widget_has_focus = false; +std::string Keyboard::user_keybindings_path; +bool Keyboard::can_save_keybindings = false; +map<string,string> Keyboard::binding_files; +std::string Keyboard::_current_binding_name = _("Unknown"); + /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */ GdkModifierType Keyboard::RelevantModifierKeyMask; @@ -360,3 +371,180 @@ Keyboard::selection_type (guint state) return Selection::Set; } } + + +static void +accel_map_changed (GtkAccelMap* map, + gchar* path, + guint key, + GdkModifierType mod, + gpointer arg) +{ + Keyboard::save_keybindings (); +} + +void +Keyboard::set_can_save_keybindings (bool yn) +{ + can_save_keybindings = yn; +} + +void +Keyboard::save_keybindings () +{ + if (can_save_keybindings) { + Gtk::AccelMap::save (user_keybindings_path); + } +} + +void +Keyboard::setup_keybindings () +{ + using namespace ARDOUR_COMMAND_LINE; + std::string default_bindings = "mnemonic-us.bindings"; + std::string path; + vector<string> strs; + + ARDOUR::find_bindings_files (binding_files); + + /* set up the per-user bindings path */ + + strs.push_back (Glib::get_home_dir()); + strs.push_back (".ardour2"); + strs.push_back ("ardour.bindings"); + + user_keybindings_path = Glib::build_filename (strs); + + /* check to see if they gave a style name ("SAE", "ergonomic") or + an actual filename (*.bindings) + */ + + if (!keybindings_path.empty() && keybindings_path.find (".bindings") == string::npos) { + + // just a style name - allow user to + // specify the layout type. + + char* layout; + + if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') { + + /* user-specified keyboard layout */ + + keybindings_path += '-'; + keybindings_path += layout; + + } else { + + /* default to US/ANSI - we have to pick something */ + + keybindings_path += "-us"; + } + + keybindings_path += ".bindings"; + } + + if (keybindings_path.empty()) { + + /* no path or binding name given: check the user one first */ + + if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) { + + keybindings_path = ""; + + } else { + + keybindings_path = user_keybindings_path; + } + } + + /* if we still don't have a path at this point, use the default */ + + if (keybindings_path.empty()) { + keybindings_path = default_bindings; + } + + while (true) { + + if (!Glib::path_is_absolute (keybindings_path)) { + + /* not absolute - look in the usual places */ + + path = find_config_file (keybindings_path); + + if (path.empty()) { + + if (keybindings_path == default_bindings) { + error << _("Default keybindings not found - Ardour will be hard to use!") << endmsg; + return; + } else { + warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"), + keybindings_path) + << endmsg; + keybindings_path = default_bindings; + } + + } else { + + /* use it */ + + keybindings_path = path; + break; + + } + + } else { + + /* path is absolute already */ + + if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) { + if (keybindings_path == default_bindings) { + error << _("Default keybindings not found - Ardour will be hard to use!") << endmsg; + return; + } else { + warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"), + keybindings_path) + << endmsg; + keybindings_path = default_bindings; + } + + } else { + break; + } + } + } + + load_keybindings (keybindings_path); + + /* catch changes */ + + GtkAccelMap* accelmap = gtk_accel_map_get(); + g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, 0); +} + +bool +Keyboard::load_keybindings (string path) +{ + try { + cerr << "loading bindings from " << path << endl; + + Gtk::AccelMap::load (path); + + _current_binding_name = _("Unknown"); + + for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) { + if (path == x->second) { + _current_binding_name = x->first; + break; + } + } + + return true; + + } catch (...) { + error << string_compose (_("Ardour key bindings file not found at \"%1\" or contains errors."), path) + << endmsg; + return false; + } +} + + |