add MidiByteArray::compare_n()
[ardour.git] / gtk2_ardour / keyboard.cc
1 /*
2     Copyright (C) 2001 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include "pbd/convert.h"
21 #include "pbd/error.h"
22 #include "pbd/file_utils.h"
23 #include "pbd/basename.h"
24
25 #include "ardour/filesystem_paths.h"
26
27 #include "ardour_ui.h"
28 #include "public_editor.h"
29 #include "keyboard.h"
30 #include "opts.h"
31 #include "ui_config.h"
32
33 #include "pbd/i18n.h"
34
35 using namespace std;
36 using namespace Gtk;
37 using namespace PBD;
38 using namespace ARDOUR;
39 using Gtkmm2ext::Keyboard;
40
41 #ifdef __APPLE__
42 guint ArdourKeyboard::constraint_mod = Keyboard::PrimaryModifier;
43 #else
44 guint ArdourKeyboard::constraint_mod = Keyboard::TertiaryModifier;
45 #endif
46
47 /* TrimDrag::start_grab() */
48 guint ArdourKeyboard::trim_contents_mod = Keyboard::PrimaryModifier;
49
50 /* TrimDrag::motion() */
51 guint ArdourKeyboard::trim_overlap_mod = Keyboard::TertiaryModifier;
52
53 /* TrimDrag::start_grab() */
54 guint ArdourKeyboard::trim_anchored_mod = Keyboard::PrimaryModifier|Keyboard::TertiaryModifier;
55
56 /* ControlPointDrag::motion() && LineDrag::motion()*/
57 guint ArdourKeyboard::fine_adjust_mod = Keyboard::PrimaryModifier|Keyboard::SecondaryModifier;
58
59 /* ControlPointDrag::start_grab() && MarkerDrag::motion() */
60 guint ArdourKeyboard::push_points_mod = Keyboard::PrimaryModifier|Keyboard::Level4Modifier;
61
62 /* NoteResizeDrag::start_grab() */
63 guint ArdourKeyboard::note_size_relative_mod = Keyboard::TertiaryModifier;
64
65 ArdourKeyboard::ArdourKeyboard (ARDOUR_UI& ardour_ui) : ui (ardour_ui)
66 {
67         Keyboard::RelevantModifierKeysChanged.connect (sigc::mem_fun (*this, &ArdourKeyboard::reset_relevant_modifier_key_mask));
68 }
69
70 void
71 ArdourKeyboard::find_bindings_files (map<string,string>& files)
72 {
73         vector<std::string> found;
74         Searchpath spath = ardour_config_search_path();
75
76         find_files_matching_pattern (found, spath, string_compose ("*%1", Keyboard::binding_filename_suffix));
77
78         if (found.empty()) {
79                 return;
80         }
81
82         for (vector<std::string>::iterator x = found.begin(); x != found.end(); ++x) {
83                 std::string path(*x);
84                 pair<string,string> namepath;
85                 namepath.second = path;
86                 namepath.first = PBD::basename_nosuffix (path);
87                 files.insert (namepath);
88         }
89 }
90
91 void
92 ArdourKeyboard::setup_keybindings ()
93 {
94         using namespace ARDOUR_COMMAND_LINE;
95         string default_bindings = string_compose ("%1%2", UIConfiguration::instance().get_default_bindings(), Keyboard::binding_filename_suffix);
96         vector<string> strs;
97
98         binding_files.clear ();
99
100         find_bindings_files (binding_files);
101
102         /* set up the per-user bindings path */
103
104         string lowercase_program_name = downcase (string(PROGRAM_NAME));
105
106         user_keybindings_path = Glib::build_filename (user_config_directory(), lowercase_program_name + binding_filename_suffix);
107
108         if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
109                 std::pair<string,string> newpair;
110                 newpair.first = _("your own");
111                 newpair.second = user_keybindings_path;
112                 binding_files.insert (newpair);
113         }
114
115         /* check to see if they gave a style name ("ergonomic") or an actual filename (*.bindings)
116         */
117
118         if (!keybindings_path.empty() && keybindings_path.find (binding_filename_suffix) == string::npos) {
119
120                 // just a style name - allow user to
121                 // specify the layout type.
122
123                 char* layout;
124
125                 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
126
127                         /* user-specified keyboard layout */
128
129                         keybindings_path += '-';
130                         keybindings_path += layout;
131
132                 } else {
133
134                         /* default to US/ANSI - we have to pick something */
135
136                         keybindings_path += "-us";
137                 }
138
139                 keybindings_path += binding_filename_suffix;
140         }
141
142         if (keybindings_path.empty()) {
143
144                 /* no path or binding name given: check the user one first */
145
146                 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
147
148                         keybindings_path = "";
149
150                 } else {
151
152                         keybindings_path = user_keybindings_path;
153                 }
154         }
155
156         /* if we still don't have a path at this point, use the default */
157
158         if (keybindings_path.empty()) {
159                 keybindings_path = default_bindings;
160         }
161
162         cerr << "KP is " << keybindings_path << endl;
163
164         while (true) {
165
166                 if (!Glib::path_is_absolute (keybindings_path)) {
167
168                         /* not absolute - look in the usual places */
169                         std::string keybindings_file;
170
171                         if (!find_file (ardour_config_search_path(), keybindings_path, keybindings_file)) {
172
173                                 if (keybindings_path == default_bindings) {
174                                         error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
175                                         return;
176                                 } else {
177                                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
178                                                                    keybindings_path)
179                                                 << endmsg;
180                                         keybindings_path = default_bindings;
181                                 }
182
183                         } else {
184
185                                 /* use it */
186
187                                 keybindings_path = keybindings_file;
188                                 break;
189
190                         }
191
192                 } else {
193
194                         /* path is absolute already */
195
196                         if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
197                                 if (keybindings_path == default_bindings) {
198                                         error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
199                                         return;
200                                 } else {
201                                         keybindings_path = default_bindings;
202                                 }
203
204                         } else {
205                                 break;
206                         }
207                 }
208         }
209
210         info << string_compose (_("Loading keybindings from %1"), keybindings_path) << endmsg;
211
212         load_keybindings (keybindings_path);
213
214         /* catch changes made via some GTK mechanism */
215
216         // GtkAccelMap* accelmap = gtk_accel_map_get();
217         // g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, this);
218 }
219
220 XMLNode&
221 ArdourKeyboard::get_state (void)
222 {
223         XMLNode* node = &Keyboard::get_state ();
224         char buf[32];
225
226         snprintf (buf, sizeof (buf), "%d", constraint_mod);
227         node->add_property ("constraint-modifier", buf);
228         snprintf (buf, sizeof (buf), "%d", trim_contents_mod);
229         node->add_property ("trim-contents-modifier", buf);
230         snprintf (buf, sizeof (buf), "%d", trim_overlap_mod);
231         node->add_property ("trim-overlap-modifier", buf);
232         snprintf (buf, sizeof (buf), "%d", trim_anchored_mod);
233         node->add_property ("trim-anchored-modifier", buf);
234         snprintf (buf, sizeof (buf), "%d", fine_adjust_mod);
235         node->add_property ("fine-adjust-modifier", buf);
236         snprintf (buf, sizeof (buf), "%d", push_points_mod);
237         node->add_property ("push-points-modifier", buf);
238         snprintf (buf, sizeof (buf), "%d", note_size_relative_mod);
239         node->add_property ("note-size-relative-modifier", buf);
240
241         return *node;
242 }
243
244 int
245 ArdourKeyboard::set_state (const XMLNode& node, int version)
246 {
247         XMLProperty const * prop;
248
249         if ((prop = node.property ("constraint-modifier")) != 0) {
250                 sscanf (prop->value().c_str(), "%d", &constraint_mod);
251         }
252
253         if ((prop = node.property ("trim-contents-modifier")) != 0) {
254                 sscanf (prop->value().c_str(), "%d", &trim_contents_mod);
255         }
256
257         if ((prop = node.property ("trim-overlap-modifier")) != 0) {
258                 sscanf (prop->value().c_str(), "%d", &trim_overlap_mod);
259         }
260
261         if ((prop = node.property ("trim-anchored-modifier")) != 0) {
262                 sscanf (prop->value().c_str(), "%d", &trim_anchored_mod);
263         }
264
265         if ((prop = node.property ("fine-adjust-modifier")) != 0) {
266                 sscanf (prop->value().c_str(), "%d", &fine_adjust_mod);
267         }
268
269         if ((prop = node.property ("push-points-modifier")) != 0) {
270                 sscanf (prop->value().c_str(), "%d", &push_points_mod);
271         }
272
273         if ((prop = node.property ("note-size-relative-modifier")) != 0) {
274                 sscanf (prop->value().c_str(), "%d", &note_size_relative_mod);
275         }
276
277         return Keyboard::set_state (node, version);
278 }
279
280 void
281 ArdourKeyboard::reset_relevant_modifier_key_mask ()
282 {
283         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | constraint_mod);
284         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_contents_mod);
285         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_overlap_mod);
286         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_anchored_mod);
287         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | fine_adjust_mod);
288         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | push_points_mod);
289         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | note_size_relative_mod);
290 }
291
292 /* Snap and snap delta modifiers may contain each other, so we use the
293  * following two methods to sort that out:
294  */
295 bool
296 ArdourKeyboard::indicates_snap (guint state)
297 {
298         const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
299         const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
300         const bool s_contains_d = Keyboard::modifier_state_contains (Keyboard::snap_modifier (), Keyboard::snap_delta_modifier ());
301
302         return  (contains_s && ((contains_d && s_contains_d) || !contains_d));
303 }
304
305 bool
306 ArdourKeyboard::indicates_snap_delta (guint state)
307 {
308         const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
309         const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
310         const bool d_contains_s = Keyboard::modifier_state_contains (Keyboard::snap_delta_modifier (), Keyboard::snap_modifier ());
311
312         return (contains_d && ((contains_s && d_contains_s) || !contains_s));
313 }
314
315 /* Constraint and copy modifiers are both in effect at the beginning of some drags, and may be set ambiguously */
316 bool
317 ArdourKeyboard::indicates_copy (guint state)
318 {
319         const bool contains_c = Keyboard::modifier_state_contains (state, Keyboard::CopyModifier);
320         const bool equals_cs = Keyboard::modifier_state_equals (state, constraint_modifier ());
321
322         return  contains_c && !equals_cs;
323 }
324
325 bool
326 ArdourKeyboard::indicates_constraint (guint state)
327 {
328         const bool contains_cs = Keyboard::modifier_state_contains (state, constraint_modifier ());
329         const bool equals_c = Keyboard::modifier_state_equals (state, Keyboard::CopyModifier);
330
331         return contains_cs && !equals_c;
332 }
333
334 void
335 ArdourKeyboard::set_constraint_modifier (guint mod)
336 {
337         constraint_mod = mod;
338         the_keyboard().reset_relevant_modifier_key_mask();
339 }
340
341 void
342 ArdourKeyboard::set_trim_contents_modifier (guint mod)
343 {
344         trim_contents_mod = mod;
345         the_keyboard().reset_relevant_modifier_key_mask();
346 }
347
348 void
349 ArdourKeyboard::set_trim_overlap_modifier (guint mod)
350 {
351         trim_overlap_mod = mod;
352         the_keyboard().reset_relevant_modifier_key_mask();
353 }
354
355 void
356 ArdourKeyboard::set_trim_anchored_modifier (guint mod)
357 {
358         trim_anchored_mod = mod;
359         the_keyboard().reset_relevant_modifier_key_mask();
360 }
361
362 void
363 ArdourKeyboard::set_fine_adjust_modifier (guint mod)
364 {
365         fine_adjust_mod = mod;
366         the_keyboard().reset_relevant_modifier_key_mask();
367 }
368
369 void
370 ArdourKeyboard::set_push_points_modifier (guint mod)
371 {
372         push_points_mod = mod;
373         the_keyboard().reset_relevant_modifier_key_mask();
374 }
375
376 void
377 ArdourKeyboard::set_note_size_relative_modifier (guint mod)
378 {
379         note_size_relative_mod = mod;
380         the_keyboard().reset_relevant_modifier_key_mask();
381 }
382
383 Selection::Operation
384 ArdourKeyboard::selection_type (guint state)
385 {
386         /* note that there is no modifier for "Add" */
387
388         if (modifier_state_equals (state, RangeSelectModifier)) {
389                 return Selection::Extend;
390         } else if (modifier_state_equals (state, PrimaryModifier)) {
391                 return Selection::Toggle;
392         } else {
393                 return Selection::Set;
394         }
395 }