734c4ecc0e4193b731745cfbde4f7d400a1c3340
[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 "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 GTKOSX
42 guint ArdourKeyboard::constraint_mod = Keyboard::PrimaryModifier;
43 #else
44 guint ArdourKeyboard::constraint_mod = Keyboard::SecondaryModifier;
45 #endif
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;
52
53 void
54 ArdourKeyboard::find_bindings_files (map<string,string>& files)
55 {
56         vector<std::string> found;
57         Searchpath spath = ardour_config_search_path();
58
59         if (getenv ("ARDOUR_SAE")) {
60                 find_files_matching_pattern (found, spath, string_compose ("*SAE-*.%1", Keyboard::binding_filename_suffix));
61         } else {
62                 find_files_matching_pattern (found, spath, string_compose ("*.%1", Keyboard::binding_filename_suffix));
63         }
64
65         if (found.empty()) {
66                 return;
67         }
68
69         for (vector<std::string>::iterator x = found.begin(); x != found.end(); ++x) {
70                 std::string path(*x);
71                 pair<string,string> namepath;
72                 namepath.second = path;
73                 namepath.first = PBD::basename_nosuffix (path);
74                 files.insert (namepath);
75         }
76 }
77
78 void
79 ArdourKeyboard::setup_keybindings ()
80 {
81         using namespace ARDOUR_COMMAND_LINE;
82         string default_bindings = string_compose ("%1%2", UIConfiguration::instance().get_default_bindings(), Keyboard::binding_filename_suffix);
83         vector<string> strs;
84
85         binding_files.clear ();
86
87         find_bindings_files (binding_files);
88
89         /* set up the per-user bindings path */
90
91         string lowercase_program_name = downcase (string(PROGRAM_NAME));
92
93         user_keybindings_path = Glib::build_filename (user_config_directory(), lowercase_program_name + binding_filename_suffix);
94
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);
100         }
101
102         /* check to see if they gave a style name ("SAE", "ergonomic") or
103            an actual filename (*.bindings)
104         */
105
106         if (!keybindings_path.empty() && keybindings_path.find (binding_filename_suffix) == string::npos) {
107
108                 // just a style name - allow user to
109                 // specify the layout type.
110
111                 char* layout;
112
113                 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
114
115                         /* user-specified keyboard layout */
116
117                         keybindings_path += '-';
118                         keybindings_path += layout;
119
120                 } else {
121
122                         /* default to US/ANSI - we have to pick something */
123
124                         keybindings_path += "-us";
125                 }
126
127                 keybindings_path += binding_filename_suffix;
128         }
129
130         if (keybindings_path.empty()) {
131
132                 /* no path or binding name given: check the user one first */
133
134                 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
135
136                         keybindings_path = "";
137
138                 } else {
139
140                         keybindings_path = user_keybindings_path;
141                 }
142         }
143
144         /* if we still don't have a path at this point, use the default */
145
146         if (keybindings_path.empty()) {
147                 keybindings_path = default_bindings;
148         }
149
150         cerr << "KP is " << keybindings_path << endl;
151
152         while (true) {
153
154                 if (!Glib::path_is_absolute (keybindings_path)) {
155
156                         /* not absolute - look in the usual places */
157                         std::string keybindings_file;
158
159                         if (!find_file (ardour_config_search_path(), keybindings_path, keybindings_file)) {
160
161                                 if (keybindings_path == default_bindings) {
162                                         error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
163                                         return;
164                                 } else {
165                                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
166                                                                    keybindings_path)
167                                                 << endmsg;
168                                         keybindings_path = default_bindings;
169                                 }
170
171                         } else {
172
173                                 /* use it */
174
175                                 keybindings_path = keybindings_file;
176                                 break;
177
178                         }
179
180                 } else {
181
182                         /* path is absolute already */
183
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;
187                                         return;
188                                 } else {
189                                         keybindings_path = default_bindings;
190                                 }
191
192                         } else {
193                                 break;
194                         }
195                 }
196         }
197
198         info << string_compose (_("Loading keybindings from %1"), keybindings_path) << endmsg;
199
200         load_keybindings (keybindings_path);
201
202         /* catch changes made via some GTK mechanism */
203
204         // GtkAccelMap* accelmap = gtk_accel_map_get();
205         // g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, this);
206 }
207
208 XMLNode&
209 ArdourKeyboard::get_state (void)
210 {
211         XMLNode* node = &Keyboard::get_state ();
212         char buf[32];
213
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);
228
229         return *node;
230 }
231
232 int
233 ArdourKeyboard::set_state (const XMLNode& node, int version)
234 {
235         const XMLProperty* prop;
236
237         if ((prop = node.property ("constraint-modifier")) != 0) {
238                 sscanf (prop->value().c_str(), "%d", &constraint_mod);
239         }
240
241         if ((prop = node.property ("trim-contents-modifier")) != 0) {
242                 sscanf (prop->value().c_str(), "%d", &trim_contents_mod);
243         }
244
245         if ((prop = node.property ("trim-overlap-modifier")) != 0) {
246                 sscanf (prop->value().c_str(), "%d", &trim_overlap_mod);
247         }
248
249         if ((prop = node.property ("trim-anchored-modifier")) != 0) {
250                 sscanf (prop->value().c_str(), "%d", &trim_anchored_mod);
251         }
252
253         if ((prop = node.property ("fine-adjust-modifier")) != 0) {
254                 sscanf (prop->value().c_str(), "%d", &fine_adjust_mod);
255         }
256
257         if ((prop = node.property ("push-points-modifier")) != 0) {
258                 sscanf (prop->value().c_str(), "%d", &push_points_mod);
259         }
260
261         if ((prop = node.property ("note-size-relative-modifier")) != 0) {
262                 sscanf (prop->value().c_str(), "%d", &note_size_relative_mod);
263         }
264
265         return Keyboard::set_state (node, version);
266 }
267
268 /* Snap and snap delta modifiers may contain each other, so we use the
269  * following two methods to sort that out:
270  */
271 bool
272 ArdourKeyboard::indicates_snap (guint state)
273 {
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 ());
277
278         return  (contains_s && ((contains_d && s_contains_d) || !contains_d));
279 }
280
281 bool
282 ArdourKeyboard::indicates_snap_delta (guint state)
283 {
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 ());
287
288         return (contains_d && ((contains_s && d_contains_s) || !contains_s));
289 }
290
291 void
292 ArdourKeyboard::set_constraint_modifier (guint mod)
293 {
294         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~constraint_mod);
295         constraint_mod = mod;
296         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | constraint_mod);
297 }
298
299 void
300 ArdourKeyboard::set_trim_contents_modifier (guint mod)
301 {
302         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_contents_mod);
303         trim_contents_mod = mod;
304         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_contents_mod);
305 }
306
307 void
308 ArdourKeyboard::set_trim_overlap_modifier (guint mod)
309 {
310         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_overlap_mod);
311         trim_overlap_mod = mod;
312         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_overlap_mod);
313 }
314
315 void
316 ArdourKeyboard::set_trim_anchored_modifier (guint mod)
317 {
318         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_anchored_mod);
319         trim_anchored_mod = mod;
320         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_anchored_mod);
321 }
322
323 void
324 ArdourKeyboard::set_fine_adjust_modifier (guint mod)
325 {
326         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~fine_adjust_mod);
327         fine_adjust_mod = mod;
328         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | fine_adjust_mod);
329 }
330
331 void
332 ArdourKeyboard::set_push_points_modifier (guint mod)
333 {
334         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~push_points_mod);
335         push_points_mod = mod;
336         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | push_points_mod);
337 }
338
339 void
340 ArdourKeyboard::set_note_size_relative_modifier (guint mod)
341 {
342         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~note_size_relative_mod);
343         note_size_relative_mod = mod;
344         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | note_size_relative_mod);
345 }
346
347 Selection::Operation
348 ArdourKeyboard::selection_type (guint state)
349 {
350         /* note that there is no modifier for "Add" */
351
352         if (modifier_state_equals (state, RangeSelectModifier)) {
353                 return Selection::Extend;
354         } else if (modifier_state_equals (state, PrimaryModifier)) {
355                 return Selection::Toggle;
356         } else {
357                 return Selection::Set;
358         }
359 }