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