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 if (getenv ("ARDOUR_SAE")) {
60 find_files_matching_pattern (found, spath, string_compose ("*SAE-*.%1", Keyboard::binding_filename_suffix));
62 find_files_matching_pattern (found, spath, string_compose ("*.%1", Keyboard::binding_filename_suffix));
69 for (vector<std::string>::iterator x = found.begin(); x != found.end(); ++x) {
71 pair<string,string> namepath;
72 namepath.second = path;
73 namepath.first = PBD::basename_nosuffix (path);
74 files.insert (namepath);
79 ArdourKeyboard::setup_keybindings ()
81 using namespace ARDOUR_COMMAND_LINE;
82 string default_bindings = string_compose ("%1%2", UIConfiguration::instance().get_default_bindings(), Keyboard::binding_filename_suffix);
85 binding_files.clear ();
87 find_bindings_files (binding_files);
89 /* set up the per-user bindings path */
91 string lowercase_program_name = downcase (string(PROGRAM_NAME));
93 user_keybindings_path = Glib::build_filename (user_config_directory(), lowercase_program_name + binding_filename_suffix);
95 if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
96 std::pair<string,string> newpair;
97 newpair.first = _("your own");
98 newpair.second = user_keybindings_path;
99 binding_files.insert (newpair);
102 /* check to see if they gave a style name ("SAE", "ergonomic") or
103 an actual filename (*.bindings)
106 if (!keybindings_path.empty() && keybindings_path.find (binding_filename_suffix) == string::npos) {
108 // just a style name - allow user to
109 // specify the layout type.
113 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
115 /* user-specified keyboard layout */
117 keybindings_path += '-';
118 keybindings_path += layout;
122 /* default to US/ANSI - we have to pick something */
124 keybindings_path += "-us";
127 keybindings_path += binding_filename_suffix;
130 if (keybindings_path.empty()) {
132 /* no path or binding name given: check the user one first */
134 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
136 keybindings_path = "";
140 keybindings_path = user_keybindings_path;
144 /* if we still don't have a path at this point, use the default */
146 if (keybindings_path.empty()) {
147 keybindings_path = default_bindings;
150 cerr << "KP is " << keybindings_path << endl;
154 if (!Glib::path_is_absolute (keybindings_path)) {
156 /* not absolute - look in the usual places */
157 std::string keybindings_file;
159 if (!find_file (ardour_config_search_path(), keybindings_path, keybindings_file)) {
161 if (keybindings_path == default_bindings) {
162 error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
165 warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
168 keybindings_path = default_bindings;
175 keybindings_path = keybindings_file;
182 /* path is absolute already */
184 if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
185 if (keybindings_path == default_bindings) {
186 error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
189 keybindings_path = default_bindings;
198 info << string_compose (_("Loading keybindings from %1"), keybindings_path) << endmsg;
200 load_keybindings (keybindings_path);
202 /* catch changes made via some GTK mechanism */
204 // GtkAccelMap* accelmap = gtk_accel_map_get();
205 // g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, this);
209 ArdourKeyboard::get_state (void)
211 XMLNode* node = &Keyboard::get_state ();
214 snprintf (buf, sizeof (buf), "%d", constraint_mod);
215 node->add_property ("constraint-modifier", buf);
216 snprintf (buf, sizeof (buf), "%d", trim_contents_mod);
217 node->add_property ("trim-contents-modifier", buf);
218 snprintf (buf, sizeof (buf), "%d", trim_overlap_mod);
219 node->add_property ("trim-overlap-modifier", buf);
220 snprintf (buf, sizeof (buf), "%d", trim_anchored_mod);
221 node->add_property ("trim-anchored-modifier", buf);
222 snprintf (buf, sizeof (buf), "%d", fine_adjust_mod);
223 node->add_property ("fine-adjust-modifier", buf);
224 snprintf (buf, sizeof (buf), "%d", push_points_mod);
225 node->add_property ("push-points-modifier", buf);
226 snprintf (buf, sizeof (buf), "%d", note_size_relative_mod);
227 node->add_property ("note-size-relative-modifier", buf);
233 ArdourKeyboard::set_state (const XMLNode& node, int version)
235 const XMLProperty* prop;
237 if ((prop = node.property ("constraint-modifier")) != 0) {
238 sscanf (prop->value().c_str(), "%d", &constraint_mod);
241 if ((prop = node.property ("trim-contents-modifier")) != 0) {
242 sscanf (prop->value().c_str(), "%d", &trim_contents_mod);
245 if ((prop = node.property ("trim-overlap-modifier")) != 0) {
246 sscanf (prop->value().c_str(), "%d", &trim_overlap_mod);
249 if ((prop = node.property ("trim-anchored-modifier")) != 0) {
250 sscanf (prop->value().c_str(), "%d", &trim_anchored_mod);
253 if ((prop = node.property ("fine-adjust-modifier")) != 0) {
254 sscanf (prop->value().c_str(), "%d", &fine_adjust_mod);
257 if ((prop = node.property ("push-points-modifier")) != 0) {
258 sscanf (prop->value().c_str(), "%d", &push_points_mod);
261 if ((prop = node.property ("note-size-relative-modifier")) != 0) {
262 sscanf (prop->value().c_str(), "%d", ¬e_size_relative_mod);
265 return Keyboard::set_state (node, version);
268 /* Snap and snap delta modifiers may contain each other, so we use the
269 * following two methods to sort that out:
272 ArdourKeyboard::indicates_snap (guint state)
274 const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
275 const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
276 const bool s_contains_d = Keyboard::modifier_state_contains (Keyboard::snap_modifier (), Keyboard::snap_delta_modifier ());
278 return (contains_s && ((contains_d && s_contains_d) || !contains_d));
282 ArdourKeyboard::indicates_snap_delta (guint state)
284 const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
285 const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
286 const bool d_contains_s = Keyboard::modifier_state_contains (Keyboard::snap_delta_modifier (), Keyboard::snap_modifier ());
288 return (contains_d && ((contains_s && d_contains_s) || !contains_s));
292 ArdourKeyboard::set_constraint_modifier (guint mod)
294 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~constraint_mod);
295 constraint_mod = mod;
296 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | constraint_mod);
300 ArdourKeyboard::set_trim_contents_modifier (guint mod)
302 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_contents_mod);
303 trim_contents_mod = mod;
304 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_contents_mod);
308 ArdourKeyboard::set_trim_overlap_modifier (guint mod)
310 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_overlap_mod);
311 trim_overlap_mod = mod;
312 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_overlap_mod);
316 ArdourKeyboard::set_trim_anchored_modifier (guint mod)
318 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_anchored_mod);
319 trim_anchored_mod = mod;
320 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_anchored_mod);
324 ArdourKeyboard::set_fine_adjust_modifier (guint mod)
326 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~fine_adjust_mod);
327 fine_adjust_mod = mod;
328 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | fine_adjust_mod);
332 ArdourKeyboard::set_push_points_modifier (guint mod)
334 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~push_points_mod);
335 push_points_mod = mod;
336 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | push_points_mod);
340 ArdourKeyboard::set_note_size_relative_modifier (guint mod)
342 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~note_size_relative_mod);
343 note_size_relative_mod = mod;
344 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | note_size_relative_mod);
348 ArdourKeyboard::selection_type (guint state)
350 /* note that there is no modifier for "Add" */
352 if (modifier_state_equals (state, RangeSelectModifier)) {
353 return Selection::Extend;
354 } else if (modifier_state_equals (state, PrimaryModifier)) {
355 return Selection::Toggle;
357 return Selection::Set;