xref: /freebsd/usr.sbin/bsdinstall/scripts/keymap (revision 7661de35d15f582ab33e3bd6b8d909601557e436)
1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# Copyright (c) 2013 Devin Teske
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28# $FreeBSD$
29#
30############################################################ INCLUDES
31
32BSDCFG_SHARE="/usr/share/bsdconfig"
33. $BSDCFG_SHARE/common.subr || exit 1
34f_dprintf "%s: loading includes..." "$0"
35f_include $BSDCFG_SHARE/dialog.subr
36f_include $BSDCFG_SHARE/keymap.subr
37f_include $BSDCFG_SHARE/sysrc.subr
38
39############################################################ CONFIGURATION
40
41#
42# Default file to store keymap selection in
43#
44: ${KEYMAPFILE:=$BSDINSTALL_TMPETC/rc.conf.keymap}
45
46#
47# Default path to keymap INDEX containing descriptions
48#
49: ${MAPDESCFILE:=/usr/share/syscons/keymaps/INDEX.keymaps}
50
51############################################################ GLOBALS
52
53#
54# Strings that should be moved to an i18n file and loaded with f_include_lang()
55#
56hline_arrows_tab_enter="Press arrows, TAB or ENTER"
57msg_continue_with_keymap="Continue with %s keymap"
58msg_default="default"
59msg_error="Error"
60msg_freebsd_installer="FreeBSD Installer"
61msg_keymap_menu_text="The system console driver for FreeBSD defaults to standard \"US\"\nkeyboard map. Other keymaps can be chosen below."
62msg_keymap_selection="Keymap Selection"
63msg_ok="OK"
64msg_select="Select"
65msg_test_keymap="Test %s keymap"
66msg_test_the_currently_selected_keymap="Test the currently selected keymap"
67msg_test_the_keymap_by_typing="Test the keymap by typing letters, numbers, and symbols. Characters\nshould match labels on the keyboard keys. Press Enter to stop testing."
68
69############################################################ FUNCTIONS
70
71# dialog_keymap_test $keymap
72#
73# Activate $keymap and display an input box (without cancel button) for the
74# user to test keyboard input and return. Always returns success.
75#
76dialog_keymap_test()
77{
78	local keym="$1"
79	local title= # Calculated below
80	local btitle= # Calculated below
81	local prompt="$msg_test_the_keymap_by_typing"
82	local hline=
83
84	# Attempt to activate the keymap
85	if [ "$keym" ]; then
86		local err
87		err=$( f_keymap_kbdcontrol "$keym" 2>&1 > /dev/null )
88		if [ "$err" ]; then
89			f_dialog_title "$msg_error"
90			f_dialog_msgbox "$err"
91			f_dialog_title_restore
92			return $FAILURE
93		fi
94	fi
95
96	f_dialog_title "$( printf "$msg_test_keymap" "${keym:-$msg_default}" )"
97	title="$DIALOG_TITLE"
98	btitle="$DIALOG_BACKTITLE"
99	f_dialog_title_restore
100
101	local height width
102	f_dialog_inputbox_size height width \
103		"$title" "$btitle" "$prompt" "" "$hline"
104
105	$DIALOG \
106		--title "$title"      \
107		--backtitle "$btitle" \
108		--hline "$hline"      \
109		--ok-label "$msg_ok"  \
110		--no-cancel           \
111		--inputbox "$prompt"  \
112		$height $width        \
113		2>/dev/null >&$DIALOG_TERMINAL_PASSTHRU_FD
114
115	return $DIALOG_OK
116}
117
118############################################################ MAIN
119
120#
121# Initialize
122#
123f_dialog_title "$msg_keymap_selection"
124f_dialog_backtitle "$msg_freebsd_installer"
125
126#
127# Die immediately if we can't dump the current keyboard map
128#
129#error=$( kbdcontrol -d 2>&1 > /dev/null ) || f_die $FAILURE "%s" "$error"
130
131# Capture Ctrl-C for clean-up
132trap 'rm -f $KEYMAPFILE; exit $FAILURE' SIGINT
133
134# Get a value from rc.conf(5) as initial value (if not being scripted)
135f_getvar $VAR_KEYMAP keymap
136if [ ! "$keymap" ]; then
137	keymap=$( f_sysrc_get keymap )
138	case "$keymap" in [Nn][Oo]) keymap="";; esac
139fi
140
141#
142# Loop until the user has finalized their selection (by clicking the
143# [relabeled] Cancel button).
144#
145width=67 first_pass=1 back_from_testing=
146[ "$USE_XDIALOG" ] && width=70
147prompt="$msg_keymap_menu_text"
148hline="$hline_arrows_tab_enter"
149while :; do
150	#
151	# Re/Build list of keymaps
152	#
153	cont_msg=$( printf "$msg_continue_with_keymap" \
154	                   "${keymap:-$msg_default}" )
155	test_msg=$( printf "$msg_test_keymap" "${keymap:-$msg_default}" )
156	menu_list="
157		'>>> $cont_msg' '' '$msg_continue_with_current_keymap'
158		'->- $test_msg' '' '$msg_test_the_currently_selected_keymap'
159	" # END-QUOTE
160	if [ "$first_pass" ]; then
161		defaultitem=
162		first_pass=
163	else
164		defaultitem="->- $test_msg"
165	fi
166	for k in $KEYMAPS; do
167		keymap_$k get keym keym
168		keymap_$k get desc desc
169		radio=" "
170		if [ "$keym" = "$keymap" ]; then
171			radio="*"
172			if [ "$back_from_testing" ]; then
173				defaultitem="(*) $desc"
174				back_from_testing=
175			fi
176		fi
177		f_shell_escape "$desc" desc
178		menu_list="$menu_list
179			'($radio) $desc' '' '$keym: $desc'
180		" # END-QUOTE
181	done
182	back_from_testing=
183
184	#
185	# Display keymap configuration menu
186	#
187	eval f_dialog_menu_with_help_size height \"\" rows \
188		\"\$DIALOG_TITLE\"     \
189		\"\$DIALOG_BACKTITLE\" \
190		\"\$prompt\"           \
191		\"\$hline\"            \
192		$menu_list
193	menu_choice=$( eval $DIALOG \
194		--title \"\$DIALOG_TITLE\"         \
195		--backtitle \"\$DIALOG_BACKTITLE\" \
196		--hline \"\$hline\"                \
197		--keep-tite                        \
198		--item-help                        \
199		--ok-label \"\$msg_select\"        \
200		--cancel-label \"\$msg_cancel\"    \
201		--default-item \"\$defaultitem\"   \
202		--menu \"\$prompt\"                \
203		$height $width $rows               \
204		$menu_list                         \
205		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
206	) || {
207		f_quietly rm -f "$KEYMAPFILE"
208		exit $FAILURE # Exit with an error so bsdinstall restarts
209	}
210	f_dialog_data_sanitize menu_choice
211
212	case "$menu_choice" in
213	">>> "*) # Continue with keymap
214		break ;;
215	"->-"*) # Test keymap
216		dialog_keymap_test "$keymap"
217		back_from_testing=1
218		continue ;;
219	esac
220
221	# Turn the user's choice into a number
222	n=$( eval f_dialog_menutag2index_with_help \
223		\"\$menu_choice\" $menu_list )
224
225	# Turn that number ithe name of the keymap struct
226	k=$( set -- $KEYMAPS; eval echo \"\${$(( $n - 2))}\" )
227
228	# Get actual keymap setting while we update $keymap and $KEYMAPFILE
229	keymap_$k get keym keymap
230	echo "keymap=\"$keymap\"" > "$KEYMAPFILE"
231done
232
233f_quietly f_keymap_kbdcontrol "$keymap"
234
235################################################################################
236# END
237################################################################################
238