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