change name of default bindings to us.bindings (and thus binding definitions to us...
[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
24 #include "ardour/filesystem_paths.h"
25
26 #include "ardour_ui.h"
27 #include "public_editor.h"
28 #include "keyboard.h"
29 #include "opts.h"
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace Gtk;
35 using namespace PBD;
36 using namespace ARDOUR;
37 using Gtkmm2ext::Keyboard;
38  
39 #ifdef GTKOSX
40 guint ArdourKeyboard::constraint_mod = Keyboard::PrimaryModifier;
41 #else
42 guint ArdourKeyboard::constraint_mod = Keyboard::SecondaryModifier;
43 #endif
44 guint ArdourKeyboard::trim_contents_mod = Keyboard::PrimaryModifier;
45 guint ArdourKeyboard::trim_overlap_mod = Keyboard::TertiaryModifier;
46 guint ArdourKeyboard::trim_anchored_mod = Keyboard::TertiaryModifier;
47 guint ArdourKeyboard::fine_adjust_mod = Keyboard::SecondaryModifier;
48 guint ArdourKeyboard::push_points_mod = Keyboard::PrimaryModifier;
49 guint ArdourKeyboard::note_size_relative_mod = Keyboard::PrimaryModifier;
50
51 void
52 ArdourKeyboard::setup_keybindings ()
53 {
54         using namespace ARDOUR_COMMAND_LINE;
55         string default_bindings = "us.bindings";
56         vector<string> strs;
57
58         binding_files.clear ();
59
60         ARDOUR::find_bindings_files (binding_files);
61
62         /* set up the per-user bindings path */
63
64         string lowercase_program_name = downcase (string(PROGRAM_NAME));
65
66         user_keybindings_path = Glib::build_filename (user_config_directory(), lowercase_program_name + ".bindings");
67
68         if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
69                 std::pair<string,string> newpair;
70                 newpair.first = _("your own");
71                 newpair.second = user_keybindings_path;
72                 binding_files.insert (newpair);
73         }
74
75         /* check to see if they gave a style name ("SAE", "ergonomic") or
76            an actual filename (*.bindings)
77         */
78
79         if (!keybindings_path.empty() && keybindings_path.find (".bindings") == string::npos) {
80
81                 // just a style name - allow user to
82                 // specify the layout type.
83
84                 char* layout;
85
86                 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
87
88                         /* user-specified keyboard layout */
89
90                         keybindings_path += '-';
91                         keybindings_path += layout;
92
93                 } else {
94
95                         /* default to US/ANSI - we have to pick something */
96
97                         keybindings_path += "-us";
98                 }
99
100                 keybindings_path += ".bindings";
101         }
102
103         if (keybindings_path.empty()) {
104
105                 /* no path or binding name given: check the user one first */
106
107                 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
108
109                         keybindings_path = "";
110
111                 } else {
112
113                         keybindings_path = user_keybindings_path;
114                 }
115         }
116
117         /* if we still don't have a path at this point, use the default */
118
119         if (keybindings_path.empty()) {
120                 keybindings_path = default_bindings;
121         }
122
123         while (true) {
124
125                 if (!Glib::path_is_absolute (keybindings_path)) {
126
127                         /* not absolute - look in the usual places */
128                         std::string keybindings_file;
129
130                         if ( ! find_file (ardour_config_search_path(), keybindings_path, keybindings_file)) {
131
132                                 if (keybindings_path == default_bindings) {
133                                         error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
134                                         return;
135                                 } else {
136                                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
137                                                                    keybindings_path)
138                                                 << endmsg;
139                                         keybindings_path = default_bindings;
140                                 }
141
142                         } else {
143
144                                 /* use it */
145
146                                 keybindings_path = keybindings_file;
147                                 break;
148
149                         }
150
151                 } else {
152
153                         /* path is absolute already */
154
155                         if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
156                                 if (keybindings_path == default_bindings) {
157                                         error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
158                                         return;
159                                 } else {
160                                         keybindings_path = default_bindings;
161                                 }
162
163                         } else {
164                                 break;
165                         }
166                 }
167         }
168
169         load_keybindings (keybindings_path);
170         
171         /* catch changes made via some GTK mechanism */
172
173         // GtkAccelMap* accelmap = gtk_accel_map_get();
174         // g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, this);
175 }
176
177 XMLNode&
178 ArdourKeyboard::get_state (void)
179 {
180         XMLNode* node = &Keyboard::get_state ();
181         char buf[32];
182
183         snprintf (buf, sizeof (buf), "%d", constraint_mod);
184         node->add_property ("constraint-modifier", buf);
185         snprintf (buf, sizeof (buf), "%d", trim_contents_mod);
186         node->add_property ("trim-contents-modifier", buf);
187         snprintf (buf, sizeof (buf), "%d", trim_overlap_mod);
188         node->add_property ("trim-overlap-modifier", buf);
189         snprintf (buf, sizeof (buf), "%d", trim_anchored_mod);
190         node->add_property ("trim-anchored-modifier", buf);
191         snprintf (buf, sizeof (buf), "%d", fine_adjust_mod);
192         node->add_property ("fine-adjust-modifier", buf);
193         snprintf (buf, sizeof (buf), "%d", push_points_mod);
194         node->add_property ("push-points-modifier", buf);
195         snprintf (buf, sizeof (buf), "%d", note_size_relative_mod);
196         node->add_property ("note-size-relative-modifier", buf);
197
198         return *node;
199 }
200
201 int
202 ArdourKeyboard::set_state (const XMLNode& node, int version)
203 {
204         const XMLProperty* prop;
205
206         if ((prop = node.property ("constraint-modifier")) != 0) {
207                 sscanf (prop->value().c_str(), "%d", &constraint_mod);
208         }
209
210         if ((prop = node.property ("trim-contents-modifier")) != 0) {
211                 sscanf (prop->value().c_str(), "%d", &trim_contents_mod);
212         }
213
214         if ((prop = node.property ("trim-overlap-modifier")) != 0) {
215                 sscanf (prop->value().c_str(), "%d", &trim_overlap_mod);
216         }
217
218         if ((prop = node.property ("trim-anchored-modifier")) != 0) {
219                 sscanf (prop->value().c_str(), "%d", &trim_anchored_mod);
220         }
221
222         if ((prop = node.property ("fine-adjust-modifier")) != 0) {
223                 sscanf (prop->value().c_str(), "%d", &fine_adjust_mod);
224         }
225
226         if ((prop = node.property ("push-points-modifier")) != 0) {
227                 sscanf (prop->value().c_str(), "%d", &push_points_mod);
228         }
229
230         if ((prop = node.property ("note-size-relative-modifier")) != 0) {
231                 sscanf (prop->value().c_str(), "%d", &note_size_relative_mod);
232         }
233
234         return Keyboard::set_state (node, version);
235 }
236
237 /* Snap and snap delta modifiers may contain each other, so we use the
238  * following two methods to sort that out:
239  */
240 bool
241 ArdourKeyboard::indicates_snap (guint state)
242 {
243         const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
244         const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
245         const bool s_contains_d = Keyboard::modifier_state_contains (Keyboard::snap_modifier (), Keyboard::snap_delta_modifier ());
246
247         return  (contains_s && ((contains_d && s_contains_d) || !contains_d));
248 }
249
250 bool
251 ArdourKeyboard::indicates_snap_delta (guint state)
252 {
253         const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
254         const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
255         const bool d_contains_s = Keyboard::modifier_state_contains (Keyboard::snap_delta_modifier (), Keyboard::snap_modifier ());
256
257         return (contains_d && ((contains_s && d_contains_s) || !contains_s));
258 }
259
260 void
261 ArdourKeyboard::set_constraint_modifier (guint mod)
262 {
263         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~constraint_mod);
264         constraint_mod = mod;
265         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | constraint_mod);
266 }
267
268 void
269 ArdourKeyboard::set_trim_contents_modifier (guint mod)
270 {
271         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_contents_mod);
272         trim_contents_mod = mod;
273         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_contents_mod);
274 }
275
276 void
277 ArdourKeyboard::set_trim_overlap_modifier (guint mod)
278 {
279         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_overlap_mod);
280         trim_overlap_mod = mod;
281         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_overlap_mod);
282 }
283
284 void
285 ArdourKeyboard::set_trim_anchored_modifier (guint mod)
286 {
287         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_anchored_mod);
288         trim_anchored_mod = mod;
289         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_anchored_mod);
290 }
291
292 void
293 ArdourKeyboard::set_fine_adjust_modifier (guint mod)
294 {
295         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~fine_adjust_mod);
296         fine_adjust_mod = mod;
297         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | fine_adjust_mod);
298 }
299
300 void
301 ArdourKeyboard::set_push_points_modifier (guint mod)
302 {
303         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~push_points_mod);
304         push_points_mod = mod;
305         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | push_points_mod);
306 }
307
308 void
309 ArdourKeyboard::set_note_size_relative_modifier (guint mod)
310 {
311         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~note_size_relative_mod);
312         note_size_relative_mod = mod;
313         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | note_size_relative_mod);
314 }
315
316 Selection::Operation
317 ArdourKeyboard::selection_type (guint state)
318 {
319         /* note that there is no modifier for "Add" */
320
321         if (modifier_state_equals (state, RangeSelectModifier)) {
322                 return Selection::Extend;
323         } else if (modifier_state_equals (state, PrimaryModifier)) {
324                 return Selection::Toggle;
325         } else {
326                 return Selection::Set;
327         }
328 }