1 /*
2 * Copyright (C) 2005-2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2008-2010 David Robillard <d@drobilla.net>
5 * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2012-2016 Tim Mayberry <mojofunk@gmail.com>
7 * Copyright (C) 2014-2018 Ben Loftis <ben@harrisonconsoles.com>
8 * Copyright (C) 2015-2016 Nick Mainsbridge <mainsbridge@gmail.com>
9 * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26 #include "pbd/convert.h"
27 #include "pbd/error.h"
28 #include "pbd/file_utils.h"
29 #include "pbd/basename.h"
30
31 #include "ardour/filesystem_paths.h"
32 #include "ardour/revision.h"
33
34 #include "ardour_ui.h"
35 #include "public_editor.h"
36 #include "keyboard.h"
37 #include "opts.h"
38 #include "ui_config.h"
39
40 #include "pbd/i18n.h"
41
42 using namespace std;
43 using namespace Gtk;
44 using namespace PBD;
45 using namespace ARDOUR;
46 using Gtkmm2ext::Keyboard;
47
48 #ifdef __APPLE__
49 guint ArdourKeyboard::constraint_mod = Keyboard::PrimaryModifier;
50 #else
51 guint ArdourKeyboard::constraint_mod = Keyboard::TertiaryModifier;
52 #endif
53
54 /* RegionSlipContentsDrag */
55 guint ArdourKeyboard::slip_contents_mod = Keyboard::PrimaryModifier|Keyboard::TertiaryModifier;
56
57 /* TrimDrag::motion() */
58 guint ArdourKeyboard::trim_overlap_mod = Keyboard::TertiaryModifier;
59
60 /* TrimDrag::start_grab() */
61 guint ArdourKeyboard::trim_anchored_mod = Keyboard::PrimaryModifier|Keyboard::TertiaryModifier;
62
63 /* ControlPointDrag::motion() && LineDrag::motion()*/
64 guint ArdourKeyboard::fine_adjust_mod = Keyboard::PrimaryModifier|Keyboard::SecondaryModifier; // XXX better just 2ndary
65
66 /* ControlPointDrag::start_grab() && MarkerDrag::motion() */
67 guint ArdourKeyboard::push_points_mod = Keyboard::PrimaryModifier|Keyboard::Level4Modifier;
68
69 /* NoteResizeDrag::start_grab() */
70 guint ArdourKeyboard::note_size_relative_mod = Keyboard::TertiaryModifier; // XXX better: 2ndary
71
ArdourKeyboard(ARDOUR_UI & ardour_ui)72 ArdourKeyboard::ArdourKeyboard (ARDOUR_UI& ardour_ui) : ui (ardour_ui)
73 {
74 Keyboard::RelevantModifierKeysChanged.connect (sigc::mem_fun (*this, &ArdourKeyboard::reset_relevant_modifier_key_mask));
75 }
76
77 void
find_bindings_files(map<string,string> & files)78 ArdourKeyboard::find_bindings_files (map<string,string>& files)
79 {
80 vector<std::string> found;
81 Searchpath spath = ardour_config_search_path();
82
83 find_files_matching_pattern (found, spath, string_compose ("*%1", Keyboard::binding_filename_suffix));
84
85 if (found.empty()) {
86 return;
87 }
88
89 for (vector<std::string>::iterator x = found.begin(); x != found.end(); ++x) {
90 std::string path(*x);
91 pair<string,string> namepath;
92 namepath.second = path;
93 namepath.first = PBD::basename_nosuffix (path);
94 files.insert (namepath);
95 }
96 }
97
98 void
setup_keybindings()99 ArdourKeyboard::setup_keybindings ()
100 {
101 string keybindings_path = ARDOUR_COMMAND_LINE::keybindings_path;
102 string default_bindings = string_compose ("%1%2", UIConfiguration::instance().get_default_bindings(), Keyboard::binding_filename_suffix);
103 vector<string> strs;
104
105 binding_files.clear ();
106
107 find_bindings_files (binding_files);
108
109 /* set up the per-user bindings path */
110
111 string lowercase_program_name = downcase (string(PROGRAM_NAME));
112
113 /* extract and append minor vesion */
114 std::string rev (revision);
115 std::size_t pos = rev.find_first_of("-");
116 if (pos != string::npos && pos > 0) {
117 lowercase_program_name += "-";
118 lowercase_program_name += rev.substr (0, pos);
119 }
120
121 user_keybindings_path = Glib::build_filename (user_config_directory(), lowercase_program_name + binding_filename_suffix);
122
123 if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
124 std::pair<string,string> newpair;
125 newpair.first = _("your own");
126 newpair.second = user_keybindings_path;
127 binding_files.insert (newpair);
128 }
129
130 /* check to see if they gave a style name ("ergonomic") or an actual filename (*.bindings)
131 */
132
133 if (!keybindings_path.empty() && keybindings_path.find (binding_filename_suffix) == string::npos) {
134
135 // just a style name - allow user to
136 // specify the layout type.
137
138 char* layout;
139
140 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
141
142 /* user-specified keyboard layout */
143
144 keybindings_path += '-';
145 keybindings_path += layout;
146
147 } else {
148
149 /* default to US/ANSI - we have to pick something */
150
151 keybindings_path += "-us";
152 }
153
154 keybindings_path += binding_filename_suffix;
155 }
156
157 if (keybindings_path.empty()) {
158
159 /* no path or binding name given: check the user one first */
160
161 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
162
163 keybindings_path = "";
164
165 } else {
166
167 keybindings_path = user_keybindings_path;
168 }
169 }
170
171 /* if we still don't have a path at this point, use the default */
172
173 if (keybindings_path.empty()) {
174 keybindings_path = default_bindings;
175 }
176
177 while (true) {
178
179 if (!Glib::path_is_absolute (keybindings_path)) {
180
181 /* not absolute - look in the usual places */
182 std::string keybindings_file;
183
184 if (!find_file (ardour_config_search_path(), keybindings_path, keybindings_file)) {
185
186 if (keybindings_path == default_bindings) {
187 error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
188 return;
189 } else {
190 warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
191 keybindings_path)
192 << endmsg;
193 keybindings_path = default_bindings;
194 }
195
196 } else {
197
198 /* use it */
199
200 keybindings_path = keybindings_file;
201 break;
202
203 }
204
205 } else {
206
207 /* path is absolute already */
208
209 if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
210 if (keybindings_path == default_bindings) {
211 error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
212 return;
213 } else {
214 keybindings_path = default_bindings;
215 }
216
217 } else {
218 break;
219 }
220 }
221 }
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&
get_state(void)232 ArdourKeyboard::get_state (void)
233 {
234 XMLNode* node = &Keyboard::get_state ();
235
236 node->set_property ("constraint-modifier", constraint_mod);
237 node->set_property ("slip-contents-modifier", slip_contents_mod);
238 node->set_property ("trim-overlap-modifier", trim_overlap_mod);
239 node->set_property ("trim-anchored-modifier", trim_anchored_mod);
240 node->set_property ("fine-adjust-modifier", fine_adjust_mod);
241 node->set_property ("push-points-modifier", push_points_mod);
242 node->set_property ("note-size-relative-modifier", note_size_relative_mod);
243
244 return *node;
245 }
246
247 int
set_state(const XMLNode & node,int version)248 ArdourKeyboard::set_state (const XMLNode& node, int version)
249 {
250 node.get_property ("constraint-modifier", constraint_mod);
251 node.get_property ("slip-contents-modifier", slip_contents_mod);
252 node.get_property ("trim-overlap-modifier", trim_overlap_mod);
253 node.get_property ("trim-anchored-modifier", trim_anchored_mod);
254 node.get_property ("fine-adjust-modifier", fine_adjust_mod);
255 node.get_property ("push-points-modifier", push_points_mod);
256 node.get_property ("note-size-relative-modifier", note_size_relative_mod);
257
258 return Keyboard::set_state (node, version);
259 }
260
261 void
reset_relevant_modifier_key_mask()262 ArdourKeyboard::reset_relevant_modifier_key_mask ()
263 {
264 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | constraint_mod);
265 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | slip_contents_mod);
266 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_overlap_mod);
267 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_anchored_mod);
268 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | fine_adjust_mod);
269 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | push_points_mod);
270 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | note_size_relative_mod);
271 }
272
273 /* Snap and snap delta modifiers may contain each other, so we use the
274 * following two methods to sort that out:
275 */
276 bool
indicates_snap(guint state)277 ArdourKeyboard::indicates_snap (guint state)
278 {
279 const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
280 const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
281 const bool s_contains_d = Keyboard::modifier_state_contains (Keyboard::snap_modifier (), Keyboard::snap_delta_modifier ());
282
283 return (contains_s && ((contains_d && s_contains_d) || !contains_d));
284 }
285
286 bool
indicates_snap_delta(guint state)287 ArdourKeyboard::indicates_snap_delta (guint state)
288 {
289 const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
290 const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
291 const bool d_contains_s = Keyboard::modifier_state_contains (Keyboard::snap_delta_modifier (), Keyboard::snap_modifier ());
292
293 return (contains_d && ((contains_s && d_contains_s) || !contains_s));
294 }
295
296 /* Constraint and copy modifiers are both in effect at the beginning of some drags, and may be set ambiguously */
297 bool
indicates_copy(guint state)298 ArdourKeyboard::indicates_copy (guint state)
299 {
300 const bool contains_c = Keyboard::modifier_state_contains (state, Keyboard::CopyModifier);
301 const bool equals_cs = Keyboard::modifier_state_equals (state, constraint_modifier ());
302
303 return contains_c && !equals_cs;
304 }
305
306 bool
indicates_constraint(guint state)307 ArdourKeyboard::indicates_constraint (guint state)
308 {
309 const bool contains_cs = Keyboard::modifier_state_contains (state, constraint_modifier ());
310 const bool equals_c = Keyboard::modifier_state_equals (state, Keyboard::CopyModifier);
311
312 return contains_cs && !equals_c;
313 }
314
315 void
set_constraint_modifier(guint mod)316 ArdourKeyboard::set_constraint_modifier (guint mod)
317 {
318 constraint_mod = mod;
319 the_keyboard().reset_relevant_modifier_key_mask();
320 }
321
322 void
set_slip_contents_modifier(guint mod)323 ArdourKeyboard::set_slip_contents_modifier (guint mod)
324 {
325 slip_contents_mod = mod;
326 the_keyboard().reset_relevant_modifier_key_mask();
327 }
328
329 void
set_trim_overlap_modifier(guint mod)330 ArdourKeyboard::set_trim_overlap_modifier (guint mod)
331 {
332 trim_overlap_mod = mod;
333 the_keyboard().reset_relevant_modifier_key_mask();
334 }
335
336 void
set_trim_anchored_modifier(guint mod)337 ArdourKeyboard::set_trim_anchored_modifier (guint mod)
338 {
339 trim_anchored_mod = mod;
340 the_keyboard().reset_relevant_modifier_key_mask();
341 }
342
343 void
set_fine_adjust_modifier(guint mod)344 ArdourKeyboard::set_fine_adjust_modifier (guint mod)
345 {
346 fine_adjust_mod = mod;
347 the_keyboard().reset_relevant_modifier_key_mask();
348 }
349
350 void
set_push_points_modifier(guint mod)351 ArdourKeyboard::set_push_points_modifier (guint mod)
352 {
353 push_points_mod = mod;
354 the_keyboard().reset_relevant_modifier_key_mask();
355 }
356
357 void
set_note_size_relative_modifier(guint mod)358 ArdourKeyboard::set_note_size_relative_modifier (guint mod)
359 {
360 note_size_relative_mod = mod;
361 the_keyboard().reset_relevant_modifier_key_mask();
362 }
363
364 Selection::Operation
selection_type(guint state)365 ArdourKeyboard::selection_type (guint state)
366 {
367 /* note that there is no modifier for "Add" */
368
369 if (modifier_state_equals (state, RangeSelectModifier)) {
370 return Selection::Extend;
371 } else if (modifier_state_equals (state, PrimaryModifier)) {
372 return Selection::Toggle;
373 } else {
374 return Selection::Set;
375 }
376 }
377