Wimplicit-fallthrough fixes for tools/utils
[ardour.git] / tools / fmt-bindings
index 7426d909e6e3a6a5264ef89ae5593255f1ff1882..d2dd1adfca5e19b92ee56723d40123df05922d9a 100755 (executable)
@@ -2,7 +2,9 @@
 
 # import module
 use Getopt::Long; 
-
+use File::Basename;
+use File::Spec;
+    
 $semicolon = ";"; # help out stupid emacs
 $title = "Ardour Shortcuts";
 $in_group_def = 0;
@@ -12,8 +14,8 @@ $group_key;
 $group_number = 0;
 %group_names;
 %group_text;
-%group_files;
-%group_handles;
+%owner_bindings;
+%group_owners;
 %group_bindings;
 %modifier_map;
 %group_numbering;
@@ -21,9 +23,8 @@ $group_number = 0;
 
 $platform = linux;
 $winkey = 'Win';
-$make_cheatsheet = 1;
-$make_accelmap = 0;
-$ardour_bindings = 0;
+$make_cheatsheet = 0;
+$make_accelmap = 1;
 $merge_from = "";
 $html = 0;
 
@@ -31,24 +32,15 @@ GetOptions ("platform=s" => \$platform,
            "winkey=s" => \$winkey,
            "cheatsheet" => \$make_cheatsheet,
            "accelmap" => \$make_accelmap,
-           "ardourbindings" => \$ardour_bindings,
            "merge=s" => \$merge_from,
             "html" => \$html);
 
-#
-# The use of a separate @WINDOW@ meta-modifier was a mistake and needs to be removed from the bindings.in file.
-# There is no separate key/modifier we can use for this, so inevitably it will be the same modifier as one of
-# the other meta-modifiers. This means, for example, that @WINDOW@-b could be same as @SECONDARY@-b with no
-# warnings given. One of the bindings will work, one won't.
-#
-
 if ($platform eq "darwin") {
 
     $gtk_modifier_map{'PRIMARY'} = 'Primary'; # GTK supports Primary to allow platform-independent binding to the "primary" modifier, which on OS X is Command
     $gtk_modifier_map{'SECONDARY'} = 'Control';
     $gtk_modifier_map{'TERTIARY'} = 'Shift';
     $gtk_modifier_map{'LEVEL4'} = 'Mod1'; 
-    $gtk_modifier_map{'WINDOW'} = 'Control';
 
     # cs_modifier_map == "Cheat Sheet Modifier Map"
     # Used to control what gets shown in the
@@ -58,7 +50,6 @@ if ($platform eq "darwin") {
     $cs_modifier_map{'SECONDARY'} = 'Control';
     $cs_modifier_map{'TERTIARY'} = 'Shift';
     $cs_modifier_map{'LEVEL4'} = 'Opt';
-    $cs_modifier_map{'WINDOW'} = 'Control';
 
     # used to display what gets shown in the
     # cheat sheet for mouse bindings. Differs
@@ -69,7 +60,6 @@ if ($platform eq "darwin") {
     $mouse_modifier_map{'SECONDARY'} = 'Ctrl';
     $mouse_modifier_map{'TERTIARY'} = 'Shift';
     $mouse_modifier_map{'LEVEL4'} = 'Opt';
-    $mouse_modifier_map{'WINDOW'} = 'Ctrl';
 
 } else {
 
@@ -77,7 +67,6 @@ if ($platform eq "darwin") {
     $gtk_modifier_map{'SECONDARY'} = 'Alt';
     $gtk_modifier_map{'TERTIARY'} = 'Shift';
     $gtk_modifier_map{'LEVEL4'} = $winkey;  # something like "Mod4><Super" 
-    $gtk_modifier_map{'WINDOW'} = 'Alt';
 
     # cs_modifier_map == "Cheat Sheet Modifier Map"
     # Used to control what gets shown in the
@@ -87,7 +76,6 @@ if ($platform eq "darwin") {
     $cs_modifier_map{'SECONDARY'} = 'Alt';
     $cs_modifier_map{'TERTIARY'} = 'Shift';
     $cs_modifier_map{'LEVEL4'} = 'Win';
-    $cs_modifier_map{'WINDOW'} = 'Alt';
 
     # used to display what gets shown in the
     # cheat sheet for mouse bindings. Differs
@@ -98,7 +86,6 @@ if ($platform eq "darwin") {
     $mouse_modifier_map{'SECONDARY'} = 'Alt';
     $mouse_modifier_map{'TERTIARY'} = 'Shift';
     $mouse_modifier_map{'LEVEL4'} = 'Win';
-    $mouse_modifier_map{'WINDOW'} = 'Alt';
 }
 
 %keycodes = ();
@@ -189,11 +176,16 @@ if ($merge_from) {
     close (BINDINGS);
 }
 
-if ($make_accelmap && !$merge_from && !$ardour_bindings) {
-    print ";; this accelmap was produced by tools/fmt-bindings\n";
+if ($make_accelmap && !$merge_from) {
+    print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 }
 
-while (<>) {
+$bindings_name = basename ($ARGV[0]);
+$bindings_name =~ s/.bindings\.in$//;
+
+open SOURCE, "<", $ARGV[0] or die $!;
+
+while (<SOURCE>) {
     next if /^$semicolon/;
 
     if (/^\$/) {
@@ -218,13 +210,12 @@ while (<>) {
 
        s/^%//;
        chop;
-       ($group_key,$group_file,$group_name) = split (/\s+/, $_, 3);
-       if ($make_accelmap && $ardour_bindings) {
-           if (!exists ($group_handles{$group_file})) {
-               print "Try to open ", $group_file . ".bindings\n";
-               open $group_handles{$group_file}, ">", $group_file . ".bindings" or die "Cannot open bindings file " . $group_file . ".bindings: $!"
+       ($group_key,$owner,$group_name) = split (/\s+/, $_, 3);
+       if ($make_accelmap) {
+           if (!exists ($owner_bindings{$owner})) {
+               $owner_bindings{$owner} = [ [] ];
            }
-           $group_files{$group_key} = $group_handles{$group_file}
+           $group_owners{$group_key} = $owner;
        }
        $group_number++;
        $group_text = "";
@@ -253,7 +244,8 @@ while (<>) {
 
        $gkey = $key;
        $gkey =~ s/^-//;
-           
+       $owner = $group_owners{$gkey};
+
        # substitute bindings
 
        $gtk_binding = $binding;
@@ -272,32 +264,28 @@ while (<>) {
            }
        } 
 
-       # print the accelmap output
+       # store the accelmap output
 
        if ($key =~ /^\+/) {
            # remove + and don't print it in the accelmap
            $key =~ s/^\+//;
        } else {
-           # include this in the accelmap
-           if (!$merge_from && $make_accelmap) {
-               if (!$ardour_bindings) {
-                   foreach $k (keys %gtk_modifier_map) {
-                       $gtk_binding =~ s/\@$k\@/$gtk_modifier_map{$k}/;
-                   }
-                   print "(gtk_accel_path \"<Actions>/$action\" \"$gtk_binding\")\n";
-               } else {
-                   $b = $binding;
-                   $b =~ s/<@//g;
-                   $b =~ s/@>//g;
-                   $b =~ s/PRIMARY/Primary-/;
-                   $b =~ s/SECONDARY/Secondary-/;
-                   $b =~ s/TERTIARY/Tertiary-/;
-                   $b =~ s/LEVEL4/Level4-/;
-                   
-                   if (exists ($group_files{$gkey})) {
-                       print { $group_files{$gkey}  } "<Binding key=\"" . $b . "\" action=\"" . $action . "\"/>\n";
-                   }
-               }
+           # include this in the accelmap if it is part of a group that has an "owner"
+           if (!$merge_from && $make_accelmap && exists ($owner_bindings{$owner})) {
+
+               $b = $binding;
+               $b =~ s/<@//g;
+               $b =~ s/@>//g;
+               $b =~ s/PRIMARY/Primary-/;
+               $b =~ s/SECONDARY/Secondary-/;
+               $b =~ s/TERTIARY/Tertiary-/;
+               $b =~ s/LEVEL4/Level4-/;
+
+               $g = $group_names{$gkey};
+               $g =~ s/\\&/&amp;/g;
+
+               $bref = $owner_bindings{$owner};
+               push (@$bref, [ $action, $b, $g]);
            }
        }
 
@@ -309,14 +297,41 @@ while (<>) {
        $bref = $group_bindings{$key};
        push (@$bref, [$binding, $text]);
 
+       $sref = $section_text{$key};
+       push (@$sref, [$owner]);
+       
        next;
     }
 
     next;
 }
 
-foreach my $key (keys %group_handles) {
-    close $group_handles{$key} or die "Group file $group_files{$key} not closed!"
+if ($make_accelmap) {
+    print "<BindingSet name=\"" . $bindings_name . "\">\n";
+    
+    foreach $owner (keys %owner_bindings) {
+       print " <Bindings name=\"$owner\">\n  <Press>\n";
+       $bindings = $owner_bindings{$owner};
+       shift (@$bindings); # remove initial empty element
+       for my $binding (@$bindings) {
+           print '   <Binding key="' . @$binding[1] . '" action="' . @$binding[0] . '" group="' . @$binding[2] . "\"/>\n";
+       }
+        print "  </Press>\n </Bindings>\n";
+    }
+
+    # merge in the "fixed" bindings that are not defined by the argument given to this program
+    # this covers things like the step editor, monitor and processor box bindings
+
+    foreach $hardcoded_bindings ("mixer.bindings", "step_editing.bindings", "monitor.bindings", "processor_box.bindings") {
+       $path = File::Spec->catfile (dirname ($ARGV[0]), $hardcoded_bindings);
+       open HARDCODED, "<", $path or die $!;
+       while (<HARDCODED>) {
+           print $_;
+       }
+       close HARDCODED;
+    }
+    
+    print "</BindingSet>\n";
 }
 
 if ($make_accelmap || !$make_cheatsheet) {