fix dragging that involves locked regions; auto-rebinding patch for people to experim...
[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 <ardour/ardour.h>
21
22 #include "ardour_ui.h"
23
24 #include <algorithm>
25 #include <fstream>
26 #include <iostream>
27
28 #include <ctype.h>
29
30 #include <gtkmm/accelmap.h>
31
32 #include <gdk/gdkkeysyms.h>
33 #include <pbd/error.h>
34
35 #include "keyboard.h"
36 #include "gui_thread.h"
37 #include "opts.h"
38
39 #include "i18n.h"
40
41 using namespace PBD;
42 using namespace ARDOUR;
43
44 #define KBD_DEBUG 0
45 bool debug_keyboard = false;
46
47 guint Keyboard::edit_but = 3;
48 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
49 guint Keyboard::delete_but = 3;
50 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
51 guint Keyboard::snap_mod = GDK_MOD3_MASK;
52
53 #ifdef GTKOSX
54 guint Keyboard::PrimaryModifier = GDK_META_MASK;   // Command
55 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK; // Alt/Option
56 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
57 guint Keyboard::Level4Modifier = GDK_CONTROL_MASK; // Control
58 guint Keyboard::CopyModifier = GDK_MOD1_MASK;      // Alt/Option
59 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;   
60 #else
61 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
62 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK;  // Alt/Option
63 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK;  // Shift
64 guint Keyboard::Level4Modifier = GDK_MOD4_MASK;     // Mod4/Windows
65 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;    
66 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;   
67 #endif
68
69 Keyboard*    Keyboard::_the_keyboard = 0;
70 Gtk::Window* Keyboard::current_window = 0;
71 bool         Keyboard::_some_magic_widget_has_focus = false;
72
73 std::string Keyboard::user_keybindings_path;
74 bool Keyboard::can_save_keybindings = false;
75 map<string,string> Keyboard::binding_files;
76 std::string Keyboard::_current_binding_name = _("Unknown");
77
78 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
79
80 GdkModifierType Keyboard::RelevantModifierKeyMask;
81
82 void
83 Keyboard::magic_widget_grab_focus () 
84 {
85         _some_magic_widget_has_focus = true;
86 }
87
88 void
89 Keyboard::magic_widget_drop_focus ()
90 {
91         _some_magic_widget_has_focus = false;
92 }
93
94 bool
95 Keyboard::some_magic_widget_has_focus ()
96 {
97         return _some_magic_widget_has_focus;
98 }
99
100 Keyboard::Keyboard ()
101 {
102         if (_the_keyboard == 0) {
103                 _the_keyboard = this;
104         }
105
106         RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
107
108         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
109         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
110         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
111         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | Level4Modifier);
112         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
113         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
114
115         gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
116
117         snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
118
119         XMLNode* node = ARDOUR_UI::instance()->keyboard_settings();
120         set_state (*node);
121 }
122
123 Keyboard::~Keyboard ()
124 {
125         gtk_key_snooper_remove (snooper_id);
126 }
127
128 XMLNode& 
129 Keyboard::get_state (void)
130 {
131         XMLNode* node = new XMLNode ("Keyboard");
132         char buf[32];
133
134         snprintf (buf, sizeof (buf), "%d", edit_but);
135         node->add_property ("edit-button", buf);
136         snprintf (buf, sizeof (buf), "%d", edit_mod);
137         node->add_property ("edit-modifier", buf);
138         snprintf (buf, sizeof (buf), "%d", delete_but);
139         node->add_property ("delete-button", buf);
140         snprintf (buf, sizeof (buf), "%d", delete_mod);
141         node->add_property ("delete-modifier", buf);
142         snprintf (buf, sizeof (buf), "%d", snap_mod);
143         node->add_property ("snap-modifier", buf);
144
145         return *node;
146 }
147
148 int 
149 Keyboard::set_state (const XMLNode& node)
150 {
151         const XMLProperty* prop;
152
153         if ((prop = node.property ("edit-button")) != 0) {
154                 sscanf (prop->value().c_str(), "%d", &edit_but);
155         } 
156
157         if ((prop = node.property ("edit-modifier")) != 0) {
158                 sscanf (prop->value().c_str(), "%d", &edit_mod);
159         } 
160
161         if ((prop = node.property ("delete-button")) != 0) {
162                 sscanf (prop->value().c_str(), "%d", &delete_but);
163         } 
164
165         if ((prop = node.property ("delete-modifier")) != 0) {
166                 sscanf (prop->value().c_str(), "%d", &delete_mod);
167         } 
168
169         if ((prop = node.property ("snap-modifier")) != 0) {
170                 sscanf (prop->value().c_str(), "%d", &snap_mod);
171         } 
172
173         return 0;
174 }
175
176 gint
177 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
178 {
179         return ((Keyboard *) data)->snooper (widget, event);
180 }
181
182 gint
183 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
184 {
185         uint32_t keyval;
186
187 #if 0
188         cerr << "snoop widget " << widget << " key " << event->keyval << " type: " << event->type 
189              << " state " << std::hex << event->state << std::dec
190              << endl;
191 #endif
192
193 #if KBD_DEBUG
194         if (debug_keyboard) {
195                 cerr << "snoop widget " << widget << " key " << event->keyval << " type: " << event->type 
196                      << endl;
197         }
198 #endif
199
200         if (event->keyval == GDK_Shift_R) {
201                 keyval = GDK_Shift_L;
202
203         } else  if (event->keyval == GDK_Control_R) {
204                 keyval = GDK_Control_L;
205
206         } else {
207                 keyval = event->keyval;
208         }
209                 
210         if (event->type == GDK_KEY_PRESS) {
211
212                 if (find (state.begin(), state.end(), keyval) == state.end()) {
213                         state.push_back (keyval);
214                         sort (state.begin(), state.end());
215                 } 
216
217         } else if (event->type == GDK_KEY_RELEASE) {
218
219                 State::iterator i;
220                 
221                 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
222                         state.erase (i);
223                         sort (state.begin(), state.end());
224                 } 
225
226         }
227
228         if (event->type == GDK_KEY_RELEASE && event->keyval == GDK_w && modifier_state_equals (event->state, PrimaryModifier)) {
229                 if (current_window) {
230                         current_window->hide ();
231                         current_window = 0;
232                 }
233         }
234
235         return false;
236 }
237
238 bool
239 Keyboard::key_is_down (uint32_t keyval)
240 {
241         return find (state.begin(), state.end(), keyval) != state.end();
242 }
243
244 bool
245 Keyboard::enter_window (GdkEventCrossing *ev, Gtk::Window* win)
246 {
247         current_window = win;
248         return false;
249 }
250
251 bool
252 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* win)
253 {
254         switch (ev->detail) {
255         case GDK_NOTIFY_INFERIOR:
256                 if (debug_keyboard) {
257                         cerr << "INFERIOR crossing ... out\n";
258                 }
259                 break;
260
261         case GDK_NOTIFY_VIRTUAL:
262                 if (debug_keyboard) {
263                         cerr << "VIRTUAL crossing ... out\n";
264                 }
265                 /* fallthru */
266
267         default:
268                 if (debug_keyboard) {
269                         cerr << "REAL CROSSING ... out\n";
270                         cerr << "clearing current target\n";
271                 }
272                 state.clear ();
273                 current_window = 0;
274         }
275
276         return false;
277 }
278
279 void
280 Keyboard::set_edit_button (guint but)
281 {
282         edit_but = but;
283 }
284
285 void
286 Keyboard::set_edit_modifier (guint mod)
287 {
288         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
289         edit_mod = mod;
290         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
291 }
292
293 void
294 Keyboard::set_delete_button (guint but)
295 {
296         delete_but = but;
297 }
298
299 void
300 Keyboard::set_delete_modifier (guint mod)
301 {
302         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
303         delete_mod = mod;
304         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
305 }
306
307 void
308 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
309 {
310         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
311         var = newval;
312         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
313 }
314
315 void
316 Keyboard::set_snap_modifier (guint mod)
317 {
318         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
319         snap_mod = mod;
320         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
321 }
322
323 bool
324 Keyboard::is_edit_event (GdkEventButton *ev)
325 {
326
327         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) && 
328                 (ev->button == Keyboard::edit_button()) && 
329                 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
330 }
331
332 bool
333 Keyboard::is_delete_event (GdkEventButton *ev)
334 {
335         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) && 
336                 (ev->button == Keyboard::delete_button()) && 
337                 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
338 }
339
340 bool
341 Keyboard::is_context_menu_event (GdkEventButton *ev)
342 {
343         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) && 
344                 (ev->button == 3) && 
345                 ((ev->state & RelevantModifierKeyMask) == 0);
346 }
347
348 bool 
349 Keyboard::no_modifiers_active (guint state)
350 {
351         return (state & RelevantModifierKeyMask) == 0;
352 }
353
354 bool
355 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
356 {
357         return (state & mask) == (guint) mask;
358 }
359
360 bool
361 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
362 {
363         return (state & RelevantModifierKeyMask) == (guint) mask;
364 }
365
366 Selection::Operation
367 Keyboard::selection_type (guint state)
368 {
369         /* note that there is no modifier for "Add" */
370
371         if (modifier_state_equals (state, RangeSelectModifier)) {
372                 return Selection::Extend;
373         } else if (modifier_state_equals (state, PrimaryModifier)) {
374                 return Selection::Toggle;
375         } else {
376                 return Selection::Set;
377         }
378 }
379
380
381 static void 
382 accel_map_changed (GtkAccelMap* map,
383                    gchar* path,
384                    guint  key,
385                    GdkModifierType mod,
386                    gpointer arg)
387 {
388         Keyboard::save_keybindings ();
389 }
390
391 void
392 Keyboard::set_can_save_keybindings (bool yn)
393 {
394         can_save_keybindings = yn;
395 }
396
397 void
398 Keyboard::save_keybindings ()
399 {
400         if (can_save_keybindings) {
401                 Gtk::AccelMap::save (user_keybindings_path);
402         } 
403 }
404
405 void
406 Keyboard::setup_keybindings ()
407 {
408         using namespace ARDOUR_COMMAND_LINE;
409         std::string default_bindings = "mnemonic-us.bindings";
410         std::string path;
411         vector<string> strs;
412
413         binding_files.clear ();
414
415         ARDOUR::find_bindings_files (binding_files);
416
417         /* set up the per-user bindings path */
418         
419         strs.push_back (Glib::get_home_dir());
420         strs.push_back (".ardour2");
421         strs.push_back ("ardour.bindings");
422
423         user_keybindings_path = Glib::build_filename (strs);
424
425         if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
426                 std::pair<string,string> newpair;
427                 newpair.first = _("your own");
428                 newpair.second = user_keybindings_path;
429                 binding_files.insert (newpair);
430         }
431
432         /* check to see if they gave a style name ("SAE", "ergonomic") or
433            an actual filename (*.bindings)
434         */
435
436         if (!keybindings_path.empty() && keybindings_path.find (".bindings") == string::npos) {
437                 
438                 // just a style name - allow user to
439                 // specify the layout type. 
440                 
441                 char* layout;
442                 
443                 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
444                         
445                         /* user-specified keyboard layout */
446                         
447                         keybindings_path += '-';
448                         keybindings_path += layout;
449
450                 } else {
451
452                         /* default to US/ANSI - we have to pick something */
453
454                         keybindings_path += "-us";
455                 }
456                 
457                 keybindings_path += ".bindings";
458         } 
459
460         if (keybindings_path.empty()) {
461
462                 /* no path or binding name given: check the user one first */
463
464                 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
465                         
466                         keybindings_path = "";
467
468                 } else {
469                         
470                         keybindings_path = user_keybindings_path;
471                 }
472         } 
473
474         /* if we still don't have a path at this point, use the default */
475
476         if (keybindings_path.empty()) {
477                 keybindings_path = default_bindings;
478         }
479
480         while (true) {
481
482                 if (!Glib::path_is_absolute (keybindings_path)) {
483                         
484                         /* not absolute - look in the usual places */
485                         
486                         path = find_config_file (keybindings_path);
487                         
488                         if (path.empty()) {
489                                 
490                                 if (keybindings_path == default_bindings) {
491                                         error << _("Default keybindings not found - Ardour will be hard to use!") << endmsg;
492                                         return;
493                                 } else {
494                                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"), 
495                                                                    keybindings_path)
496                                                 << endmsg;
497                                         keybindings_path = default_bindings;
498                                 }
499
500                         } else {
501
502                                 /* use it */
503
504                                 keybindings_path = path;
505                                 break;
506                                 
507                         }
508
509                 } else {
510                         
511                         /* path is absolute already */
512
513                         if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
514                                 if (keybindings_path == default_bindings) {
515                                         error << _("Default keybindings not found - Ardour will be hard to use!") << endmsg;
516                                         return;
517                                 } else {
518                                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"), 
519                                                                    keybindings_path)
520                                                 << endmsg;
521                                         keybindings_path = default_bindings;
522                                 }
523
524                         } else {
525                                 break;
526                         }
527                 }
528         }
529
530         load_keybindings (keybindings_path);
531
532         /* catch changes */
533
534         GtkAccelMap* accelmap = gtk_accel_map_get();
535         g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, 0);
536 }
537
538 bool
539 Keyboard::load_keybindings (string path)
540 {
541         try {
542                 cerr << "loading bindings from " << path << endl;
543
544                 Gtk::AccelMap::load (path);
545
546                 _current_binding_name = _("Unknown");
547
548                 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
549                         if (path == x->second) {
550                                 _current_binding_name = x->first;
551                                 break;
552                         }
553                 }
554
555                 return true;
556
557         } catch (...) {
558                 error << string_compose (_("Ardour key bindings file not found at \"%1\" or contains errors."), path)
559                       << endmsg;
560                 return false;
561         }
562 }
563
564