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