1 if [ ! "$_KEYMAP_SUBR" ]; then _KEYMAP_SUBR=1
3 # Copyright (c) 2013-2015 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." keymap.subr
34 f_include $BSDCFG_SHARE/struct.subr
36 ############################################################ CONFIGURATION
39 # Defaults taken from usr.sbin/kbdmap/kbdmap.h
42 : ${DEFAULT_KEYMAP_DIR:=/usr/share/syscons/keymaps}
44 ############################################################ GLOBALS
49 # A "keymap" from kbdmap's point of view
50 f_struct_define KEYMAP \
56 # Default behavior is to call f_keymap_get_all() automatically when loaded.
58 : ${KEYMAP_SELF_SCAN_ALL=1}
60 ############################################################ FUNCTIONS
62 # f_keymap_register $name $desc $keym $mark
64 # Register a keymap. A `structure' (see struct.subr) is created with the name
65 # keymap_$name (so make sure $name contains only alpha-numeric characters or
66 # the underscore, `_'). The remaining arguments after $name correspond to the
67 # propertise of the `KEYMAP' structure-type (defined above).
69 # If not already registered, the keymap is then appended to the KEYMAPS
70 # environment variable, a space-separated list of all registered keymaps.
74 local name="$1" desc="$2" keym="$3" mark="$4"
76 f_struct_new KEYMAP "keymap_$name" || return $FAILURE
77 keymap_$name set desc "$desc"
78 keymap_$name set keym "$keym"
79 keymap_$name set mark "$mark"
81 # Scan our global register to see if needs ammending
84 [ "$k" = "$name" ] || continue
87 [ "$found" ] || KEYMAPS="$KEYMAPS $name"
92 # f_keymap_checkfile $keymap
94 # Check that $keymap is a readable kbdmap(5) file. Returns success if $keymap
95 # is a file, is readable, and exists in $DEFAULT_KEYMAP_DIR; otherwise failure.
96 # If debugging is enabled, an appropriate debug error message is printed if
97 # $keymap is not available.
103 # Fixup keymap if it doesn't already contain at least one `/'
104 [ "${keym#*/}" = "$keym" ] && keym="$DEFAULT_KEYMAP_DIR/$keym"
107 [ -f "$keym" -a -r "$keym" ] && return $SUCCESS
108 f_debugging || return $FAILURE
110 # Print an appropriate debug error message
111 if [ ! -e "$keym" ]; then
112 f_dprintf "%s: No such file or directory" "$keym"
113 elif [ ! -f "$keym" ]; then
114 f_dprintf "%s: Not a file!" "$keym"
115 elif [ ! -r "$keym" ]; then
116 f_dprintf "%s: Permission denied" "$keym"
124 # Get all keymap information for kbdmap(5) entries both in the database and
125 # loosely existing in $DEFAULT_KEYMAP_DIR.
129 local fname=f_keymap_get_all
130 local lang="${LC_ALL:-${LC_CTYPE:-${LANG:-$DEFAULT_LANG}}}"
131 [ "$lang" = "C" ] && lang="$DEFAULT_LANG"
133 f_dprintf "%s: Looking for keymap files..." $fname
134 f_dialog_info "$msg_looking_for_keymap_files"
135 f_dprintf "DEFAULT_LANG=[%s]" "$DEFAULT_LANG"
137 eval "$( awk -F: -v lang="$lang" -v lang_default="$DEFAULT_LANG" '
139 # en_US.ISO8859-1 -> en_..\.ISO8859-1
141 if (length(dialect) >= 6 &&
142 substr(dialect, 3, 1) == "_")
143 dialect = substr(dialect, 1, 3) ".." \
145 printf "f_dprintf \"dialect=[%%s]\" \"%s\";\n", dialect
147 # en_US.ISO8859-1 -> en
149 if (length(lang_abk) >= 3 &&
150 substr(lang_abk, 3, 1) == "_")
151 lang_abk = substr(lang_abk, 1, 2)
152 printf "f_dprintf \"lang_abk=[%%s]\" \"%s\";\n",
155 function find_token(buffer, token)
157 if (split(buffer, tokens, /,/) == 0) return 0
160 if (token == tokens[t]) { found = 1; break }
163 function add_keymap(desc,mark,keym)
167 gsub(/[^[:alnum:]_]/, "_", name)
168 gsub(/'\''/, "'\''\\'\'\''", desc);
169 printf "f_keymap_checkfile %s && " \
170 "f_keymap_register %s '\'%s\'' %s %u\n",
171 keym, name, desc, keym, mark
173 !/^[[:space:]]*(#|$)/ {
174 sub(/^[[:space:]]*/, "", $0)
176 if (keym ~ /^(MENU|FONT)$/) next
177 lg = ($2 == "" ? lang_default : $2)
179 # Match the entry and store the type of match we made
180 # as the mark value (so that if we make a better match
181 # later on with a higher mark, it overwrites previous)
184 if (find_token(lg, lang))
185 add_keymap($3, 4, keym) # Best match
186 else if (mark <= 3 && find_token(lg, dialect))
187 add_keymap($3, 3, keym)
188 else if (mark <= 2 && find_token(lg, lang_abk))
189 add_keymap($3, 2, keym)
190 else if (mark <= 1 && find_token(lg, lang_default))
191 add_keymap($3, 1, keym)
193 add_keymap($3, 0, keym)
195 ' "$DEFAULT_KEYMAP_DIR/INDEX.${DEFAULT_KEYMAP_DIR##*/}" )"
199 # Look for keymaps not in database
201 local direntry keym name
203 for direntry in "$DEFAULT_KEYMAP_DIR"/*; do
204 [ "${direntry##*.}" = ".kbd" ] || continue
205 keym="${direntry##*/}"
206 f_str2varname "$keym" name
207 f_struct keymap_$name && continue
208 f_keymap_checkfile "$keym" &&
209 f_keymap_register $name "${keym%.*}" "$keym" 0
210 f_dprintf "%s: not in kbdmap(5) database" "$keym"
214 # Sort the items by their descriptions
216 f_dprintf "%s: Sorting keymap entries by description..." $fname
218 for k in $KEYMAPS; do
220 # NOTE: Translate '8x8' to '8x08' before sending to
221 # sort(1) so that things work out as we might expect.
222 debug= keymap_$k get desc | awk 'gsub(/8x8/,"8x08")||1'
223 done | sort -k2 | awk '{
224 printf "%s%s", (started ? " " : ""), $1; started = 1
231 # f_keymap_kbdcontrol $keymap
233 # Install keyboard map file from $keymap.
235 f_keymap_kbdcontrol()
239 [ "$keymap" ] || return $SUCCESS
241 # Fixup keymap if it doesn't already contain at least one `/'
242 [ "${keymap#*/}" = "$keymap" ] && keymap="$DEFAULT_KEYMAP_DIR/$keymap"
244 [ "$USE_XDIALOG" ] || kbdcontrol -l "$keymap"
247 ############################################################ MAIN
250 # Scan for keymaps unless requeted otherwise
252 f_dprintf "%s: KEYMAP_SELF_SCAN_ALL=[%s]" keymap.subr "$KEYMAP_SELF_SCAN_ALL"
253 case "$KEYMAP_SELF_SCAN_ALL" in
254 ""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
258 f_count NKEYMAPS $KEYMAPS
259 f_dprintf "%s: Found %u keymap file(s)." keymap.subr $NKEYMAPS
261 f_dprintf "%s: Successfully loaded." keymap.subr