-
Notifications
You must be signed in to change notification settings - Fork 1
/
conv-keymap.pl
executable file
·150 lines (128 loc) · 3.43 KB
/
conv-keymap.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/perl
# Convert a console-tools keymap to a kexec-loader keymap
# Copyright (C) 2007-2009 Daniel Collins <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use strict;
use warnings;
my $infile = undef;
my $outfile = undef;
my $path = "/usr/share/keymaps/include:/usr/share/keymaps/i386/include";
my $pw = 0;
foreach my $arg(@ARGV) {
if($pw) {
$path = $arg;
$pw = 0;
next;
}
if(substr($arg, 0, 1) eq "-") {
if($arg eq "-h") {
print "Usage: ./conv-keymap.pl [-p include-path] <input keymap> <output keymap>\n";
exit(0);
}elsif($arg eq "-p") {
$pw = 1;
}else{
print STDERR "Usage: ./conv-keymap.pl [-p include-path] <input keymap> <output keymap>\n";
exit(1);
}
}elsif(!defined($infile)) {
$infile = $arg;
}elsif(!defined($outfile)) {
$outfile = $arg;
}else{
print STDERR "Usage: ./conv-keymap.pl [-p include-path] <input keymap> <output keymap>\n";
exit(1);
}
}
if(!defined($outfile)) {
print STDERR "Usage: ./conv-keymap.pl [-p include-path] <input keymap> <output keymap>\n";
exit(1);
}
my $t = $infile;
$t =~ s/\/[^\/]+$//;
$path .= ":$t";
open(TMP, ">$outfile") or die("Can't open keymap.txt: $!");
sub parse_keymap {
my $file = $_[0];
my @data = ();
if(-e $file) {
goto READFILE;
}
if(-e "$file.gz") {
$file = "$file.gz";
goto READFILE;
}
foreach my $dir(split(/:/, $path)) {
if(-e "$dir/$file") {
$file = "$dir/$file";
goto READFILE;
}
if(-e "$dir/$file.gz") {
$file = "$dir/$file.gz";
goto READFILE;
}
if(-e "$dir/$file.inc") {
$file = "$dir/$file.inc";
goto READFILE;
}
if(-e "$dir/$file.inc.gz") {
$file = "$dir/$file.inc.gz";
goto READFILE;
}
}
READFILE:
if($file =~ /\.gz$/) {
@data = qx(zcat $file);
}else{
@data = qx(cat $file);
}
foreach my $line(@data) {
chomp($line);
$line =~ s/^\s+//g;
$line =~ s/\s+$//g;
$line =~ s/\s*#.*//g;
if($line eq "") {
next;
}
if($line =~ /keycode\s+\d+\s*=/) {
my ($a, $b) = split(/\s*=\s*/, $line, 2);
my @as = split(/\s+/, $a);
my $key = pop(@as);
my $cmd = join(" ", @as);
my @actions = split(/\s+/, $b);
my $m = 0;
foreach my $c(@as) {
$m |= 1 if($c eq "shift");
$m |= 2 if($c eq "altgr");
$m |= 4 if($c eq "control");
$m |= 8 if($c eq "alt");
$m |= 16 if($c eq "shiftl");
$m |= 32 if($c eq "shiftr");
$m |= 64 if($c eq "ctrll");
$m |= 128 if($c eq "ctrlr");
}
print TMP "$m\t$key\t$actions[0]\n" if(@actions > 0);
print TMP "1\t$key\t$actions[1]\n" if(@actions > 1);
print TMP "2\t$key\t$actions[2]\n" if(@actions > 2);
print TMP "4\t$key\t$actions[3]\n" if(@actions > 3);
}
if($line =~ /include ".+"/) {
my (undef, $ifile) = split(/"/, $line);
parse_keymap($ifile);
}
}
}
parse_keymap($infile);
close(TMP);