20380d5b0176fc971a3a40fe7e01a0dd50d915f7
[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 guint Keyboard::PrimaryModifier = GDK_META_MASK;   // Command
61 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK; // Alt/Option
62 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
63 guint Keyboard::Level4Modifier = GDK_CONTROL_MASK; // Control
64 guint Keyboard::CopyModifier = GDK_MOD1_MASK;      // Alt/Option
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 _("Option"); }
70 const char* Keyboard::tertiary_modifier_name() { return _("Shift"); }
71 const char* Keyboard::level4_modifier_name() { return _("Control"); }
72 const char* Keyboard::copy_modifier_name() { return _("Mod1";    ); }
73 const char* Keyboard::rangeselect_modifier_name() { return _("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 _("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 _("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                         if (current_window) {
303                                 current_window->hide ();
304                                 current_window = 0;
305                                 ret = true;
306                         }
307                         break;
308                 }
309         }
310
311         return ret;
312 }
313
314 bool
315 Keyboard::key_is_down (uint32_t keyval)
316 {
317         return find (state.begin(), state.end(), keyval) != state.end();
318 }
319
320 bool
321 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
322 {
323         current_window = win;
324         return false;
325 }
326
327 bool
328 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
329 {
330         if (ev) {
331                 switch (ev->detail) {
332                 case GDK_NOTIFY_INFERIOR:
333                         DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
334                         break;
335
336                 case GDK_NOTIFY_VIRTUAL:
337                         DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
338                         /* fallthru */
339
340                 default:
341                         DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
342                         DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
343                         state.clear ();
344                         current_window = 0;
345                 }
346         } else {
347                 current_window = 0;
348         }
349
350         return false;
351 }
352
353 void
354 Keyboard::set_edit_button (guint but)
355 {
356         edit_but = but;
357 }
358
359 void
360 Keyboard::set_edit_modifier (guint mod)
361 {
362         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
363         edit_mod = mod;
364         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
365 }
366
367 void
368 Keyboard::set_delete_button (guint but)
369 {
370         delete_but = but;
371 }
372
373 void
374 Keyboard::set_delete_modifier (guint mod)
375 {
376         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
377         delete_mod = mod;
378         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
379 }
380
381 void
382 Keyboard::set_insert_note_button (guint but)
383 {
384         insert_note_but = but;
385 }
386
387 void
388 Keyboard::set_insert_note_modifier (guint mod)
389 {
390         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~insert_note_mod);
391         insert_note_mod = mod;
392         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | insert_note_mod);
393 }
394
395
396 void
397 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
398 {
399         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
400         var = newval;
401         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
402 }
403
404 void
405 Keyboard::set_snap_modifier (guint mod)
406 {
407         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
408         snap_mod = mod;
409         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
410 }
411
412 bool
413 Keyboard::is_edit_event (GdkEventButton *ev)
414 {
415         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
416                 (ev->button == Keyboard::edit_button()) &&
417                 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
418 }
419
420 bool
421 Keyboard::is_insert_note_event (GdkEventButton *ev)
422 {
423         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
424                 (ev->button == Keyboard::insert_note_button()) &&
425                 ((ev->state & RelevantModifierKeyMask) == Keyboard::insert_note_modifier());
426 }
427
428 bool
429 Keyboard::is_button2_event (GdkEventButton* ev)
430 {
431 #ifdef GTKOSX
432         return (ev->button == 2) ||
433                 ((ev->button == 1) &&
434                  ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
435 #else
436         return ev->button == 2;
437 #endif
438 }
439
440 bool
441 Keyboard::is_delete_event (GdkEventButton *ev)
442 {
443         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
444                 (ev->button == Keyboard::delete_button()) &&
445                 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
446 }
447
448 bool
449 Keyboard::is_context_menu_event (GdkEventButton *ev)
450 {
451         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
452                 (ev->button == 3) &&
453                 ((ev->state & RelevantModifierKeyMask) == 0);
454 }
455
456 bool
457 Keyboard::no_modifiers_active (guint state)
458 {
459         return (state & RelevantModifierKeyMask) == 0;
460 }
461
462 bool
463 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
464 {
465         return (state & mask) == (guint) mask;
466 }
467
468 bool
469 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
470 {
471         return (state & RelevantModifierKeyMask) == (guint) mask;
472 }
473
474 void
475 Keyboard::keybindings_changed ()
476 {
477         if (Keyboard::can_save_keybindings) {
478                 Keyboard::bindings_changed_after_save_became_legal = true;
479         }
480
481         Keyboard::save_keybindings ();
482 }
483
484 void
485 Keyboard::set_can_save_keybindings (bool yn)
486 {
487         can_save_keybindings = yn;
488 }
489
490 void
491 Keyboard::save_keybindings ()
492 {
493         if (can_save_keybindings && bindings_changed_after_save_became_legal) {
494                 Gtk::AccelMap::save (user_keybindings_path);
495         }
496 }
497
498 bool
499 Keyboard::load_keybindings (string path)
500 {
501         try {
502                 info << "Loading bindings from " << path << endl;
503
504                 Gtk::AccelMap::load (path);
505
506                 _current_binding_name = _("Unknown");
507
508                 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
509                         if (path == x->second) {
510                                 _current_binding_name = x->first;
511                                 break;
512                         }
513                 }
514
515
516         } catch (...) {
517                 error << string_compose (_("Ardour key bindings file not found at \"%1\" or contains errors."), path)
518                       << endmsg;
519                 return false;
520         }
521
522         /* now find all release-driven bindings */
523
524         vector<string> groups;
525         vector<string> names;
526         vector<string> tooltips;
527         vector<AccelKey> bindings;
528
529         ActionManager::get_all_actions (groups, names, tooltips, bindings);
530
531         vector<string>::iterator g;
532         vector<AccelKey>::iterator b;
533         vector<string>::iterator n;
534
535         release_keys.clear ();
536
537         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
538                 stringstream s;
539                 s << "Action: " << *n << " Group: " << *g << " Binding: ";
540
541                 if ((*b).get_key() != GDK_VoidSymbol) {
542                         s << b->get_key() << " w/mod " << hex << b->get_mod() << dec << " = " << b->get_abbrev () << "\n";
543                 } else {
544                         s << "unbound\n";
545                 }
546
547                 DEBUG_TRACE (DEBUG::Bindings, s.str ());
548         }
549
550         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
551                 if ((*b).get_mod() & Gdk::RELEASE_MASK) {
552                         release_keys.insert (pair<AccelKey,two_strings> (*b, two_strings (*g, *n)));
553                 }
554         }
555
556         return true;
557 }
558
559