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 "pbd/convert.h"
21 #include "pbd/error.h"
22 #include "pbd/file_utils.h"
23 #include "pbd/basename.h"
25 #include "ardour/filesystem_paths.h"
27 #include "ardour_ui.h"
28 #include "public_editor.h"
31 #include "ui_config.h"
38 using namespace ARDOUR;
39 using Gtkmm2ext::Keyboard;
42 guint ArdourKeyboard::constraint_mod = Keyboard::PrimaryModifier;
44 guint ArdourKeyboard::constraint_mod = Keyboard::SecondaryModifier;
46 guint ArdourKeyboard::trim_contents_mod = Keyboard::PrimaryModifier;
47 guint ArdourKeyboard::trim_overlap_mod = Keyboard::TertiaryModifier;
48 guint ArdourKeyboard::trim_anchored_mod = Keyboard::TertiaryModifier;
49 guint ArdourKeyboard::fine_adjust_mod = Keyboard::SecondaryModifier;
50 guint ArdourKeyboard::push_points_mod = Keyboard::PrimaryModifier;
51 guint ArdourKeyboard::note_size_relative_mod = Keyboard::PrimaryModifier;
54 ArdourKeyboard::find_bindings_files (map<string,string>& files)
56 vector<std::string> found;
57 Searchpath spath = ardour_config_search_path();
59 find_files_matching_pattern (found, spath, string_compose ("*%1", Keyboard::binding_filename_suffix));
65 for (vector<std::string>::iterator x = found.begin(); x != found.end(); ++x) {
67 pair<string,string> namepath;
68 namepath.second = path;
69 namepath.first = PBD::basename_nosuffix (path);
70 files.insert (namepath);
75 ArdourKeyboard::setup_keybindings ()
77 using namespace ARDOUR_COMMAND_LINE;
78 string default_bindings = string_compose ("%1%2", UIConfiguration::instance().get_default_bindings(), Keyboard::binding_filename_suffix);
81 binding_files.clear ();
83 find_bindings_files (binding_files);
85 /* set up the per-user bindings path */
87 string lowercase_program_name = downcase (string(PROGRAM_NAME));
89 user_keybindings_path = Glib::build_filename (user_config_directory(), lowercase_program_name + binding_filename_suffix);
91 if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
92 std::pair<string,string> newpair;
93 newpair.first = _("your own");
94 newpair.second = user_keybindings_path;
95 binding_files.insert (newpair);
98 /* check to see if they gave a style name ("ergonomic") or an actual filename (*.bindings)
101 if (!keybindings_path.empty() && keybindings_path.find (binding_filename_suffix) == string::npos) {
103 // just a style name - allow user to
104 // specify the layout type.
108 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
110 /* user-specified keyboard layout */
112 keybindings_path += '-';
113 keybindings_path += layout;
117 /* default to US/ANSI - we have to pick something */
119 keybindings_path += "-us";
122 keybindings_path += binding_filename_suffix;
125 if (keybindings_path.empty()) {
127 /* no path or binding name given: check the user one first */
129 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
131 keybindings_path = "";
135 keybindings_path = user_keybindings_path;
139 /* if we still don't have a path at this point, use the default */
141 if (keybindings_path.empty()) {
142 keybindings_path = default_bindings;
145 cerr << "KP is " << keybindings_path << endl;
149 if (!Glib::path_is_absolute (keybindings_path)) {
151 /* not absolute - look in the usual places */
152 std::string keybindings_file;
154 if (!find_file (ardour_config_search_path(), keybindings_path, keybindings_file)) {
156 if (keybindings_path == default_bindings) {
157 error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
160 warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
163 keybindings_path = default_bindings;
170 keybindings_path = keybindings_file;
177 /* path is absolute already */
179 if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
180 if (keybindings_path == default_bindings) {
181 error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
184 keybindings_path = default_bindings;
193 info << string_compose (_("Loading keybindings from %1"), keybindings_path) << endmsg;
195 load_keybindings (keybindings_path);
197 /* catch changes made via some GTK mechanism */
199 // GtkAccelMap* accelmap = gtk_accel_map_get();
200 // g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, this);
204 ArdourKeyboard::get_state (void)
206 XMLNode* node = &Keyboard::get_state ();
209 snprintf (buf, sizeof (buf), "%d", constraint_mod);
210 node->add_property ("constraint-modifier", buf);
211 snprintf (buf, sizeof (buf), "%d", trim_contents_mod);
212 node->add_property ("trim-contents-modifier", buf);
213 snprintf (buf, sizeof (buf), "%d", trim_overlap_mod);
214 node->add_property ("trim-overlap-modifier", buf);
215 snprintf (buf, sizeof (buf), "%d", trim_anchored_mod);
216 node->add_property ("trim-anchored-modifier", buf);
217 snprintf (buf, sizeof (buf), "%d", fine_adjust_mod);
218 node->add_property ("fine-adjust-modifier", buf);
219 snprintf (buf, sizeof (buf), "%d", push_points_mod);
220 node->add_property ("push-points-modifier", buf);
221 snprintf (buf, sizeof (buf), "%d", note_size_relative_mod);
222 node->add_property ("note-size-relative-modifier", buf);
228 ArdourKeyboard::set_state (const XMLNode& node, int version)
230 XMLProperty const * prop;
232 if ((prop = node.property ("constraint-modifier")) != 0) {
233 sscanf (prop->value().c_str(), "%d", &constraint_mod);
236 if ((prop = node.property ("trim-contents-modifier")) != 0) {
237 sscanf (prop->value().c_str(), "%d", &trim_contents_mod);
240 if ((prop = node.property ("trim-overlap-modifier")) != 0) {
241 sscanf (prop->value().c_str(), "%d", &trim_overlap_mod);
244 if ((prop = node.property ("trim-anchored-modifier")) != 0) {
245 sscanf (prop->value().c_str(), "%d", &trim_anchored_mod);
248 if ((prop = node.property ("fine-adjust-modifier")) != 0) {
249 sscanf (prop->value().c_str(), "%d", &fine_adjust_mod);
252 if ((prop = node.property ("push-points-modifier")) != 0) {
253 sscanf (prop->value().c_str(), "%d", &push_points_mod);
256 if ((prop = node.property ("note-size-relative-modifier")) != 0) {
257 sscanf (prop->value().c_str(), "%d", ¬e_size_relative_mod);
260 return Keyboard::set_state (node, version);
263 /* Snap and snap delta modifiers may contain each other, so we use the
264 * following two methods to sort that out:
267 ArdourKeyboard::indicates_snap (guint state)
269 const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
270 const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
271 const bool s_contains_d = Keyboard::modifier_state_contains (Keyboard::snap_modifier (), Keyboard::snap_delta_modifier ());
273 return (contains_s && ((contains_d && s_contains_d) || !contains_d));
277 ArdourKeyboard::indicates_snap_delta (guint state)
279 const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
280 const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
281 const bool d_contains_s = Keyboard::modifier_state_contains (Keyboard::snap_delta_modifier (), Keyboard::snap_modifier ());
283 return (contains_d && ((contains_s && d_contains_s) || !contains_s));
287 ArdourKeyboard::set_constraint_modifier (guint mod)
289 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~constraint_mod);
290 constraint_mod = mod;
291 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | constraint_mod);
295 ArdourKeyboard::set_trim_contents_modifier (guint mod)
297 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_contents_mod);
298 trim_contents_mod = mod;
299 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_contents_mod);
303 ArdourKeyboard::set_trim_overlap_modifier (guint mod)
305 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_overlap_mod);
306 trim_overlap_mod = mod;
307 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_overlap_mod);
311 ArdourKeyboard::set_trim_anchored_modifier (guint mod)
313 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_anchored_mod);
314 trim_anchored_mod = mod;
315 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_anchored_mod);
319 ArdourKeyboard::set_fine_adjust_modifier (guint mod)
321 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~fine_adjust_mod);
322 fine_adjust_mod = mod;
323 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | fine_adjust_mod);
327 ArdourKeyboard::set_push_points_modifier (guint mod)
329 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~push_points_mod);
330 push_points_mod = mod;
331 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | push_points_mod);
335 ArdourKeyboard::set_note_size_relative_modifier (guint mod)
337 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~note_size_relative_mod);
338 note_size_relative_mod = mod;
339 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | note_size_relative_mod);
343 ArdourKeyboard::selection_type (guint state)
345 /* note that there is no modifier for "Add" */
347 if (modifier_state_equals (state, RangeSelectModifier)) {
348 return Selection::Extend;
349 } else if (modifier_state_equals (state, PrimaryModifier)) {
350 return Selection::Toggle;
352 return Selection::Set;