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