faderport: turn out all lights from destructor, and use Port::drain() to make sure...
[ardour.git] / libs / gtkmm2ext / 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
22 #include <algorithm>
23 #include <cerrno>
24 #include <ctype.h>
25
26 #include "pbd/gstdio_compat.h"
27
28 #include <gtkmm/widget.h>
29 #include <gtkmm/window.h>
30 #include <gtkmm/accelmap.h>
31 #include <gdk/gdkkeysyms.h>
32
33 #include "pbd/error.h"
34 #include "pbd/convert.h"
35 #include "pbd/file_utils.h"
36 #include "pbd/search_path.h"
37 #include "pbd/xml++.h"
38 #include "pbd/debug.h"
39 #include "pbd/unwind.h"
40
41 #include "gtkmm2ext/keyboard.h"
42 #include "gtkmm2ext/actions.h"
43 #include "gtkmm2ext/debug.h"
44
45 #include "i18n.h"
46
47 using namespace PBD;
48 using namespace Gtk;
49 using namespace Gtkmm2ext;
50 using namespace std;
51
52 guint Keyboard::edit_but = 3;
53 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
54 guint Keyboard::delete_but = 3;
55 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
56 guint Keyboard::insert_note_but = 1;
57 guint Keyboard::insert_note_mod = GDK_CONTROL_MASK;
58
59 #ifdef GTKOSX
60
61 uint Keyboard::PrimaryModifier = GDK_MOD2_MASK;   // Command
62 guint Keyboard::SecondaryModifier = GDK_CONTROL_MASK; // Control
63 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
64 guint Keyboard::Level4Modifier = GDK_MOD1_MASK; // Alt/Option
65 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;      // Control
66 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
67 guint Keyboard::button2_modifiers = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
68
69 const char* Keyboard::primary_modifier_name() { return _("Command"); }
70 const char* Keyboard::secondary_modifier_name() { return _("Control"); }
71 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
72 const char* Keyboard::level4_modifier_name() { return _("Option"); }
73
74 guint Keyboard::snap_mod = Keyboard::Level4Modifier|Keyboard::TertiaryModifier; // XXX this is probably completely wrong
75 guint Keyboard::snap_delta_mod = Keyboard::Level4Modifier;
76
77 #else
78
79 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
80 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK;  // Alt/Option
81 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK;  // Shift
82 guint Keyboard::Level4Modifier = GDK_MOD4_MASK;     // Mod4/Windows
83 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;
84 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
85 guint Keyboard::button2_modifiers = 0; /* not used */
86
87 const char* Keyboard::primary_modifier_name() { return _("Control"); }
88 const char* Keyboard::secondary_modifier_name() { return _("Alt"); }
89 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
90 const char* Keyboard::level4_modifier_name() { return _("Windows"); }
91
92 guint Keyboard::snap_mod = Keyboard::SecondaryModifier;
93 guint Keyboard::snap_delta_mod = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
94
95 #endif
96
97 guint Keyboard::GainFineScaleModifier = Keyboard::PrimaryModifier;
98 guint Keyboard::GainExtraFineScaleModifier = Keyboard::SecondaryModifier;
99
100 guint Keyboard::ScrollZoomVerticalModifier = Keyboard::SecondaryModifier;
101 guint Keyboard::ScrollZoomHorizontalModifier = Keyboard::PrimaryModifier;
102 guint Keyboard::ScrollHorizontalModifier = Keyboard::TertiaryModifier;
103
104 Keyboard*    Keyboard::_the_keyboard = 0;
105 Gtk::Window* Keyboard::current_window = 0;
106 bool         Keyboard::_some_magic_widget_has_focus = false;
107
108 std::string Keyboard::user_keybindings_path;
109 bool Keyboard::can_save_keybindings = false;
110 bool Keyboard::bindings_changed_after_save_became_legal = false;
111 map<string,string> Keyboard::binding_files;
112 string Keyboard::_current_binding_name;
113 map<AccelKey,pair<string,string>,Keyboard::AccelKeyLess> Keyboard::release_keys;
114
115 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
116
117 GdkModifierType Keyboard::RelevantModifierKeyMask;
118
119 void
120 Keyboard::magic_widget_grab_focus ()
121 {
122         _some_magic_widget_has_focus = true;
123 }
124
125 void
126 Keyboard::magic_widget_drop_focus ()
127 {
128         _some_magic_widget_has_focus = false;
129 }
130
131 bool
132 Keyboard::some_magic_widget_has_focus ()
133 {
134         return _some_magic_widget_has_focus;
135 }
136
137 Keyboard::Keyboard ()
138 {
139         if (_the_keyboard == 0) {
140                 _the_keyboard = this;
141                 _current_binding_name = _("Unknown");
142         }
143
144         RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
145
146         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
147         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
148         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
149         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | Level4Modifier);
150         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
151         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
152
153         gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
154
155         snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
156 }
157
158 Keyboard::~Keyboard ()
159 {
160         gtk_key_snooper_remove (snooper_id);
161 }
162
163 XMLNode&
164 Keyboard::get_state (void)
165 {
166         XMLNode* node = new XMLNode ("Keyboard");
167         char buf[32];
168
169         snprintf (buf, sizeof (buf), "%d", CopyModifier);
170         node->add_property ("copy-modifier", buf);
171         snprintf (buf, sizeof (buf), "%d", edit_but);
172         node->add_property ("edit-button", buf);
173         snprintf (buf, sizeof (buf), "%d", edit_mod);
174         node->add_property ("edit-modifier", buf);
175         snprintf (buf, sizeof (buf), "%d", delete_but);
176         node->add_property ("delete-button", buf);
177         snprintf (buf, sizeof (buf), "%d", delete_mod);
178         node->add_property ("delete-modifier", buf);
179         snprintf (buf, sizeof (buf), "%d", snap_mod);
180         node->add_property ("snap-modifier", buf);
181         snprintf (buf, sizeof (buf), "%d", snap_delta_mod);
182         node->add_property ("snap-delta-modifier", buf);
183         snprintf (buf, sizeof (buf), "%d", insert_note_but);
184         node->add_property ("insert-note-button", buf);
185         snprintf (buf, sizeof (buf), "%d", insert_note_mod);
186         node->add_property ("insert-note-modifier", buf);
187
188         return *node;
189 }
190
191 int
192 Keyboard::set_state (const XMLNode& node, int /*version*/)
193 {
194         const XMLProperty* prop;
195
196         if ((prop = node.property ("copy-modifier")) != 0) {
197                 sscanf (prop->value().c_str(), "%d", &CopyModifier);
198         }
199
200         if ((prop = node.property ("edit-button")) != 0) {
201                 sscanf (prop->value().c_str(), "%d", &edit_but);
202         }
203
204         if ((prop = node.property ("edit-modifier")) != 0) {
205                 sscanf (prop->value().c_str(), "%d", &edit_mod);
206         }
207
208         if ((prop = node.property ("delete-button")) != 0) {
209                 sscanf (prop->value().c_str(), "%d", &delete_but);
210         }
211
212         if ((prop = node.property ("delete-modifier")) != 0) {
213                 sscanf (prop->value().c_str(), "%d", &delete_mod);
214         }
215
216         if ((prop = node.property ("snap-modifier")) != 0) {
217                 sscanf (prop->value().c_str(), "%d", &snap_mod);
218         }
219
220         if ((prop = node.property ("snap-delta-modifier")) != 0) {
221                 sscanf (prop->value().c_str(), "%d", &snap_delta_mod);
222         }
223
224         if ((prop = node.property ("insert-note-button")) != 0) {
225                 sscanf (prop->value().c_str(), "%d", &insert_note_but);
226         }
227
228         if ((prop = node.property ("insert-note-modifier")) != 0) {
229                 sscanf (prop->value().c_str(), "%d", &insert_note_mod);
230         }
231
232         return 0;
233 }
234
235 gint
236 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
237 {
238         return ((Keyboard *) data)->snooper (widget, event);
239 }
240
241 gint
242 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
243 {
244         uint32_t keyval;
245         bool ret = false;
246
247         DEBUG_TRACE (
248                 DEBUG::Keyboard,
249                 string_compose (
250                         "Snoop widget %1 name: [%6] key %2 type %3 state %4 magic %5\n",
251                         widget, event->keyval, event->type, event->state, _some_magic_widget_has_focus,
252                         gtk_widget_get_name (widget)
253                         )
254                 );
255
256         if (event->keyval == GDK_Shift_R) {
257                 keyval = GDK_Shift_L;
258
259         } else if (event->keyval == GDK_Control_R) {
260                 keyval = GDK_Control_L;
261
262         } else {
263                 keyval = event->keyval;
264         }
265
266         if (event->state & ScrollZoomVerticalModifier) {
267                 /* There is a special and rather hacky situation in Editor which makes
268                    it useful to know when the modifier key for vertical zoom has been
269                    released, so emit a signal here (see Editor::_stepping_axis_view).
270                    Note that the state bit for the modifier key is set for the key-up
271                    event when the modifier is released, but not the key-down when it
272                    is pressed, so we get here on key-up, which is what we want.
273                 */
274                 ZoomVerticalModifierReleased (); /* EMIT SIGNAL */
275         }
276
277         if (event->type == GDK_KEY_PRESS) {
278
279                 if (find (state.begin(), state.end(), keyval) == state.end()) {
280                         state.push_back (keyval);
281                         sort (state.begin(), state.end());
282
283                 } else {
284
285                         /* key is already down. if its also used for release,
286                            prevent auto-repeat events.
287                         */
288
289                         for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
290
291                                 const AccelKey& ak (k->first);
292
293                                 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
294                                         DEBUG_TRACE (DEBUG::Keyboard, "Suppress auto repeat\n");
295                                         ret = true;
296                                         break;
297                                 }
298                         }
299                 }
300
301         } else if (event->type == GDK_KEY_RELEASE) {
302
303                 State::iterator i;
304
305                 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
306                         state.erase (i);
307                         sort (state.begin(), state.end());
308                 }
309
310                 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
311
312                         const AccelKey& ak (k->first);
313                         two_strings ts (k->second);
314
315                         if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
316                                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (ts.first.c_str(), ts.second.c_str());
317                                 if (act) {
318                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Activate %1 %2\n", ts.first, ts.second));
319                                         act->activate();
320                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Use repeat, suppress other\n", ts.first, ts.second));
321                                         ret = true;
322                                 }
323                                 break;
324                         }
325                 }
326         }
327
328         /* Special keys that we want to handle in
329            any dialog, no matter whether it uses
330            the regular set of accelerators or not
331         */
332
333         if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
334                 switch (event->keyval) {
335                 case GDK_w:
336                         close_current_dialog ();
337                         ret = true;
338                         break;
339                 }
340         }
341
342         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("snooper returns %1\n", ret));
343
344         return ret;
345 }
346
347 void
348 Keyboard::close_current_dialog ()
349 {
350         if (current_window) {
351                 current_window->hide ();
352                 current_window = 0;
353         }
354 }
355
356 bool
357 Keyboard::key_is_down (uint32_t keyval)
358 {
359         return find (state.begin(), state.end(), keyval) != state.end();
360 }
361
362 bool
363 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
364 {
365         current_window = win;
366         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Entering window, title = %1\n", win->get_title()));
367         return false;
368 }
369
370 bool
371 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
372 {
373         if (ev) {
374                 switch (ev->detail) {
375                 case GDK_NOTIFY_INFERIOR:
376                         DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
377                         break;
378
379                 case GDK_NOTIFY_VIRTUAL:
380                         DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
381                         /* fallthru */
382
383                 default:
384                         DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
385                         DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
386                         state.clear ();
387                         current_window = 0;
388                 }
389         } else {
390                 current_window = 0;
391         }
392
393         return false;
394 }
395
396 bool
397 Keyboard::focus_in_window (GdkEventFocus *, Gtk::Window* win)
398 {
399         current_window = win;
400         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Focusing in window, title = %1\n", win->get_title()));
401         return false;
402 }
403
404 bool
405 Keyboard::focus_out_window (GdkEventFocus * ev, Gtk::Window* win)
406 {
407         if (ev) {
408                 state.clear ();
409                 current_window = 0;
410         }  else {
411                 current_window = 0;
412         }
413
414         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Foucusing out window, title = %1\n", win->get_title()));
415
416         return false;
417 }
418
419 void
420 Keyboard::set_edit_button (guint but)
421 {
422         edit_but = but;
423 }
424
425 void
426 Keyboard::set_edit_modifier (guint mod)
427 {
428         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
429         edit_mod = mod;
430         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
431 }
432
433 void
434 Keyboard::set_delete_button (guint but)
435 {
436         delete_but = but;
437 }
438
439 void
440 Keyboard::set_delete_modifier (guint mod)
441 {
442         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
443         delete_mod = mod;
444         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
445 }
446
447 void
448 Keyboard::set_insert_note_button (guint but)
449 {
450         insert_note_but = but;
451 }
452
453 void
454 Keyboard::set_insert_note_modifier (guint mod)
455 {
456         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~insert_note_mod);
457         insert_note_mod = mod;
458         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | insert_note_mod);
459 }
460
461
462 void
463 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
464 {
465         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
466         var = newval;
467         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
468 }
469
470 void
471 Keyboard::set_snap_modifier (guint mod)
472 {
473         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
474         snap_mod = mod;
475         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
476 }
477
478 void
479 Keyboard::set_snap_delta_modifier (guint mod)
480 {
481         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_delta_mod);
482         snap_delta_mod = mod;
483         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_delta_mod);
484 }
485
486 bool
487 Keyboard::is_edit_event (GdkEventButton *ev)
488 {
489         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
490                 (ev->button == Keyboard::edit_button()) &&
491                 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
492 }
493
494 bool
495 Keyboard::is_insert_note_event (GdkEventButton *ev)
496 {
497         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
498                 (ev->button == Keyboard::insert_note_button()) &&
499                 ((ev->state & RelevantModifierKeyMask) == Keyboard::insert_note_modifier());
500 }
501
502 bool
503 Keyboard::is_button2_event (GdkEventButton* ev)
504 {
505 #ifdef GTKOSX
506         return (ev->button == 2) ||
507                 ((ev->button == 1) &&
508                  ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
509 #else
510         return ev->button == 2;
511 #endif
512 }
513
514 bool
515 Keyboard::is_delete_event (GdkEventButton *ev)
516 {
517         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
518                 (ev->button == Keyboard::delete_button()) &&
519                 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
520 }
521
522 bool
523 Keyboard::is_context_menu_event (GdkEventButton *ev)
524 {
525         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
526                 (ev->button == 3) &&
527                 ((ev->state & RelevantModifierKeyMask) == 0);
528 }
529
530 bool
531 Keyboard::no_modifiers_active (guint state)
532 {
533         return (state & RelevantModifierKeyMask) == 0;
534 }
535
536 bool
537 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
538 {
539         return (state & mask) == (guint) mask;
540 }
541
542 bool
543 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
544 {
545         return (state & RelevantModifierKeyMask) == (guint) mask;
546 }
547
548 void
549 Keyboard::keybindings_changed ()
550 {
551         if (Keyboard::can_save_keybindings) {
552                 Keyboard::bindings_changed_after_save_became_legal = true;
553         }
554
555         Keyboard::save_keybindings ();
556 }
557
558 void
559 Keyboard::set_can_save_keybindings (bool yn)
560 {
561         can_save_keybindings = yn;
562 }
563
564 void
565 Keyboard::save_keybindings ()
566 {
567         if (can_save_keybindings && bindings_changed_after_save_became_legal) {
568                 Gtk::AccelMap::save (user_keybindings_path);
569         }
570 }
571
572 bool
573 Keyboard::load_keybindings (string path)
574 {
575         try {
576                 info << "Loading bindings from " << path << endl;
577
578                 Gtk::AccelMap::load (path);
579
580                 _current_binding_name = _("Unknown");
581
582                 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
583                         if (path == x->second) {
584                                 _current_binding_name = x->first;
585                                 break;
586                         }
587                 }
588
589
590         } catch (...) {
591                 error << string_compose (_("key bindings file not found at \"%2\" or contains errors."), path)
592                       << endmsg;
593                 return false;
594         }
595
596         /* now find all release-driven bindings */
597
598         vector<string> groups;
599         vector<string> names;
600         vector<string> tooltips;
601         vector<AccelKey> bindings;
602
603         ActionManager::get_all_actions (groups, names, tooltips, bindings);
604
605         vector<string>::iterator g;
606         vector<AccelKey>::iterator b;
607         vector<string>::iterator n;
608
609         release_keys.clear ();
610
611         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
612                 stringstream s;
613                 s << "Action: " << *n << " Group: " << *g << " Binding: ";
614
615                 if ((*b).get_key() != GDK_VoidSymbol) {
616                         s << b->get_key() << " w/mod " << hex << b->get_mod() << dec << " = " << b->get_abbrev () << "\n";
617                 } else {
618                         s << "unbound\n";
619                 }
620
621                 DEBUG_TRACE (DEBUG::Bindings, s.str ());
622         }
623
624         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
625                 if ((*b).get_mod() & Gdk::RELEASE_MASK) {
626                         release_keys.insert (pair<AccelKey,two_strings> (*b, two_strings (*g, *n)));
627                 }
628         }
629
630         return true;
631 }
632
633 int
634 Keyboard::reset_bindings ()
635 {
636         if (Glib::file_test (user_keybindings_path,  Glib::FILE_TEST_EXISTS)) {
637
638                 string new_path = user_keybindings_path;
639                 new_path += ".old";
640
641                 if (::g_rename (user_keybindings_path.c_str(), new_path.c_str())) {
642                         error << string_compose (_("Cannot rename your own keybinding file (%1)"), strerror (errno)) << endmsg;
643                         return -1;
644                 }
645         }
646
647         {
648                 PBD::Unwinder<bool> uw (can_save_keybindings, false);
649                 setup_keybindings ();
650         }
651
652         return 0;
653 }