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