Advertisement
Guest User

keybindings.pl

a guest
Mar 5th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. my $action = '';
  6. my $filename = '-';
  7.  
  8. for my $arg (@ARGV){
  9. if ($arg eq "-e" or $arg eq "--export"){
  10. $action = 'export';
  11. } elsif ($arg eq "-i" or $arg eq "--import"){
  12. $action = 'import';
  13. } elsif ($arg eq "-h" or $arg eq "--help"){
  14. print "Import and export keybindings\n";
  15. print " -e, --export <filename>\n";
  16. print " -i, --import <filename>\n";
  17. print " -h, --help\n";
  18. exit;
  19. } elsif ($arg =~ /^\-/){
  20. die "Unknown argument $arg";
  21. } else {
  22. $filename = $arg;
  23. if (!$action){
  24. if ( -e $filename){
  25. $action='import';
  26. } else {
  27. $action='export';
  28. }
  29. }
  30. }
  31. }
  32.  
  33. $action='export' if (!$action);
  34. if ($action eq 'export'){
  35. &export();
  36. } else {
  37. &import();
  38. }
  39.  
  40. sub export(){
  41. my $gsettingsFolders = [
  42. ['org.gnome.desktop.wm.keybindings','.'],
  43. ['org.gnome.settings-daemon.plugins.power','button'],
  44. ['org.gnome.settings-daemon.plugins.media-keys','.'],
  45. ];
  46.  
  47. my $customBindings = [
  48. ];
  49.  
  50. $filename = ">$filename";
  51. open (my $fh, $filename) || die "Can't open file $filename: $!";
  52.  
  53. for my $folder (@$gsettingsFolders){
  54. my @keylist = split(/\n/, `gsettings list-recursively $folder->[0]`);
  55. foreach my $line (@keylist){
  56. if ($line =~ /^([^ ]+) ([^ ]+)(?: \@[a-z]+)? (.*)/){
  57. my ($path, $name, $value) = ($1,$2,$3);
  58. if ($name eq "custom-keybindings"){
  59. $value =~ s/[\[\]\' ]//g;
  60. my @c = split(/,/, $value);
  61. $customBindings = \@c;
  62. } elsif ($name =~ /$folder->[1]/){
  63. if ($value =~ /^\[|\'/){
  64. if ($value =~ /^\[\'(?:disabled)?\'\]$/){
  65. $value = '[]';
  66. }
  67. print $fh "$path\t$name\t$value\n";
  68. }
  69. }
  70. } else {
  71. die "Could note parse $line";
  72. }
  73. }
  74. }
  75.  
  76. for my $folder (@$customBindings){
  77. my $gs = `gsettings list-recursively org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:$folder`;
  78. my ($binding) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding binding (\'[^\n]+\')/g;
  79. my ($command) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding command (\'[^\n]+\')/g;
  80. my ($name) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding name (\'[^\n]+\')/g;
  81. print $fh "custom\t$name\t$command\t$binding\n"
  82. }
  83.  
  84. close($fh);
  85. }
  86.  
  87. sub import(){
  88.  
  89. $filename = "<$filename";
  90. open (my $fh, $filename) || die "Can't open file $filename: $!";
  91.  
  92. my $customcount=0;
  93.  
  94. while (my $line = <$fh>){
  95. chomp $line;
  96. if ($line){
  97. my @v = split(/\t/, $line);
  98. if (@v[0] eq 'custom'){
  99. my ($custom, $name, $command, $binding) = @v;
  100. print "Installing custom keybinding: $name\n";
  101. print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ name \"$name\"`;
  102. print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ command \"$command\"`;
  103. print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ binding \"$binding\"`;
  104. $customcount++;
  105. } else {
  106. my ($path, $name, $value) = @v;
  107. print "Importing $path $name\n";
  108. print `gsettings set \"$path\" \"$name\" \"$value\"`;
  109. }
  110. }
  111. }
  112. if ($customcount > 0){
  113. my $customlist = "";
  114. for (my $i=0; $i<$customcount; $i++){
  115. $customlist .= "," if ($customlist);
  116. $customlist .= "'/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$i/'";
  117. }
  118. $customlist = "[$customlist]";
  119. print "Importing list of custom keybindings.\n";
  120. print `gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings \"$customlist\"`;
  121. }
  122.  
  123. close($fh);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement