You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

genkeymap.pl 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as
  7. # published by the Free Software Foundation; either version 2 of the
  8. # License, or any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. # 02110-1301, USA.
  19. =head1 NAME
  20. genkeymap.pl
  21. =head1 SYNOPSIS
  22. genkeymap.pl [options] <keymap name>
  23. Options:
  24. -f,--from=<name> Set BIOS keymap name (default "us")
  25. -h,--help Display brief help message
  26. -v,--verbose Increase verbosity
  27. -q,--quiet Decrease verbosity
  28. =cut
  29. # With reference to:
  30. #
  31. # http://gunnarwrobel.de/wiki/Linux-and-the-keyboard.html
  32. use Getopt::Long;
  33. use Pod::Usage;
  34. use strict;
  35. use warnings;
  36. use constant BIOS_KEYMAP => "us";
  37. use constant BKEYMAP_MAGIC => "bkeymap";
  38. use constant MAX_NR_KEYMAPS => 256;
  39. use constant NR_KEYS => 128;
  40. use constant KG_SHIFT => 0;
  41. use constant KG_ALTGR => 1;
  42. use constant KG_CTRL => 2;
  43. use constant KG_ALT => 3;
  44. use constant KG_SHIFTL => 4;
  45. use constant KG_KANASHIFT => 4;
  46. use constant KG_SHIFTR => 5;
  47. use constant KG_CTRLL => 6;
  48. use constant KG_CTRLR => 7;
  49. use constant KG_CAPSSHIFT => 8;
  50. use constant KT_LATIN => 0;
  51. use constant KT_FN => 1;
  52. use constant KT_SPEC => 2;
  53. use constant KT_PAD => 3;
  54. use constant KT_DEAD => 4;
  55. use constant KT_CONS => 5;
  56. use constant KT_CUR => 6;
  57. use constant KT_SHIFT => 7;
  58. use constant KT_META => 8;
  59. use constant KT_ASCII => 9;
  60. use constant KT_LOCK => 10;
  61. use constant KT_LETTER => 11;
  62. use constant KT_SLOCK => 12;
  63. use constant KT_SPKUP => 14;
  64. my $verbosity = 1;
  65. my $from_name = BIOS_KEYMAP;
  66. # Read named keymaps using "loadkeys -b"
  67. #
  68. sub read_keymaps {
  69. my $name = shift;
  70. my $keymaps = [];
  71. # Generate binary keymap
  72. open my $pipe, "-|", "loadkeys", "-b", $name
  73. or die "Could not load keymap \"".$name."\": $!\n";
  74. # Check magic
  75. read $pipe, my $magic, length BKEYMAP_MAGIC
  76. or die "Could not read from \"".$name."\": $!\n";
  77. die "Bad magic value from \"".$name."\"\n"
  78. unless $magic eq BKEYMAP_MAGIC;
  79. # Read list of included keymaps
  80. read $pipe, my $included, MAX_NR_KEYMAPS
  81. or die "Could not read from \"".$name."\": $!\n";
  82. my @included = unpack ( "C*", $included );
  83. die "Missing or truncated keymap list from \"".$name."\"\n"
  84. unless @included == MAX_NR_KEYMAPS;
  85. # Read each keymap in turn
  86. for ( my $keymap = 0 ; $keymap < MAX_NR_KEYMAPS ; $keymap++ ) {
  87. if ( $included[$keymap] ) {
  88. read $pipe, my $keysyms, ( NR_KEYS * 2 )
  89. or die "Could not read from \"".$name."\": $!\n";
  90. my @keysyms = unpack ( "S*", $keysyms );
  91. die "Missing or truncated keymap ".$keymap." from \"".$name."\"\n"
  92. unless @keysyms == NR_KEYS;
  93. push @$keymaps, \@keysyms;
  94. } else {
  95. push @$keymaps, undef;
  96. }
  97. }
  98. close $pipe;
  99. return $keymaps;
  100. }
  101. # Translate keysym value to ASCII
  102. #
  103. sub keysym_to_ascii {
  104. my $keysym = shift;
  105. # Non-existent keysyms have no ASCII equivalent
  106. return unless $keysym;
  107. # Sanity check
  108. if ( $keysym & 0xf000 ) {
  109. warn "Unexpected keysym ".sprintf ( "0x%04x", $keysym )."\n";
  110. return;
  111. }
  112. # Extract type and value
  113. my $type = ( $keysym >> 8 );
  114. my $value = ( $keysym & 0xff );
  115. # Non-simple types have no ASCII equivalent
  116. return unless ( ( $type == KT_LATIN ) || ( $type == KT_ASCII ) ||
  117. ( $type == KT_LETTER ) );
  118. # High-bit-set characters cannot be generated on a US keyboard
  119. return if $value & 0x80;
  120. return $value;
  121. }
  122. # Translate ASCII to descriptive name
  123. #
  124. sub ascii_to_name {
  125. my $ascii = shift;
  126. if ( $ascii == 0x5c ) {
  127. return "'\\\\'";
  128. } elsif ( $ascii == 0x27 ) {
  129. return "'\\\''";
  130. } elsif ( ( $ascii >= 0x20 ) && ( $ascii <= 0x7e ) ) {
  131. return sprintf ( "'%c'", $ascii );
  132. } elsif ( $ascii <= 0x1a ) {
  133. return sprintf ( "Ctrl-%c", ( 0x40 + $ascii ) );
  134. } else {
  135. return sprintf ( "0x%02x", $ascii );
  136. }
  137. }
  138. # Produce translation table between two keymaps
  139. #
  140. sub translate_keymaps {
  141. my $from = shift;
  142. my $to = shift;
  143. my $map = {};
  144. foreach my $keymap ( 0, 1 << KG_SHIFT, 1 << KG_CTRL ) {
  145. for ( my $keycode = 0 ; $keycode < NR_KEYS ; $keycode++ ) {
  146. my $from_ascii = keysym_to_ascii ( $from->[$keymap]->[$keycode] )
  147. or next;
  148. my $to_ascii = keysym_to_ascii ( $to->[$keymap]->[$keycode] )
  149. or next;
  150. my $new_map = ( ! exists $map->{$from_ascii} );
  151. my $update_map =
  152. ( $new_map || ( $keycode < $map->{$from_ascii}->{keycode} ) );
  153. if ( ( $verbosity > 1 ) &&
  154. ( ( $from_ascii != $to_ascii ) ||
  155. ( $update_map && ! $new_map ) ) ) {
  156. printf STDERR "In keymap %d: %s => %s%s\n", $keymap,
  157. ascii_to_name ( $from_ascii ), ascii_to_name ( $to_ascii ),
  158. ( $update_map ? ( $new_map ? "" : " (override)" )
  159. : " (ignored)" );
  160. }
  161. if ( $update_map ) {
  162. $map->{$from_ascii} = {
  163. to_ascii => $to_ascii,
  164. keycode => $keycode,
  165. };
  166. }
  167. }
  168. }
  169. return { map { $_ => $map->{$_}->{to_ascii} } keys %$map };
  170. }
  171. # Parse command-line options
  172. Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
  173. GetOptions (
  174. 'verbose|v+' => sub { $verbosity++; },
  175. 'quiet|q+' => sub { $verbosity--; },
  176. 'from|f=s' => sub { shift; $from_name = shift; },
  177. 'help|h' => sub { pod2usage ( 1 ); },
  178. ) or die "Could not parse command-line options\n";
  179. pod2usage ( 1 ) unless @ARGV == 1;
  180. my $to_name = shift;
  181. # Read and translate keymaps
  182. my $from = read_keymaps ( $from_name );
  183. my $to = read_keymaps ( $to_name );
  184. my $map = translate_keymaps ( $from, $to );
  185. # Generate output
  186. ( my $to_name_c = $to_name ) =~ s/\W/_/g;
  187. printf "/** \@file\n";
  188. printf " *\n";
  189. printf " * \"".$to_name."\" keyboard mapping\n";
  190. printf " *\n";
  191. printf " * This file is automatically generated; do not edit\n";
  192. printf " *\n";
  193. printf " */\n";
  194. printf "\n";
  195. printf "FILE_LICENCE ( PUBLIC_DOMAIN );\n";
  196. printf "\n";
  197. printf "#include <ipxe/keymap.h>\n";
  198. printf "\n";
  199. printf "/** \"".$to_name."\" keyboard mapping */\n";
  200. printf "struct key_mapping ".$to_name_c."_mapping[] __keymap = {\n";
  201. foreach my $from_sym ( sort { $a <=> $b } keys %$map ) {
  202. my $to_sym = $map->{$from_sym};
  203. next if $from_sym == $to_sym;
  204. printf "\t{ 0x%02x, 0x%02x },\t/* %s => %s */\n", $from_sym, $to_sym,
  205. ascii_to_name ( $from_sym ), ascii_to_name ( $to_sym );
  206. }
  207. printf "};\n";