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