xref: /freebsd/usr.sbin/bsdinstall/scripts/keymap (revision abd87254)
1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# Copyright (c) 2013-2015 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#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." "$0"
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/keymap.subr
36f_include $BSDCFG_SHARE/sysrc.subr
37
38############################################################ CONFIGURATION
39
40#
41# Default file to store keymap selection in
42#
43: ${KEYMAPFILE:=$BSDINSTALL_TMPETC/rc.conf.keymap}
44
45############################################################ GLOBALS
46
47#
48# Strings that should be moved to an i18n file and loaded with f_include_lang()
49#
50hline_arrows_tab_enter="Press arrows, TAB or ENTER"
51msg_continue_with_keymap="Continue with %s keymap"
52msg_default="default"
53msg_error="Error"
54msg_freebsd_installer="$OSNAME Installer"
55msg_keymap_menu_text="The system console driver for $OSNAME defaults to standard \"US\"\nkeyboard map. Other keymaps can be chosen below."
56msg_keymap_selection="Keymap Selection"
57msg_ok="OK"
58msg_select="Select"
59msg_test_keymap="Test %s keymap"
60msg_test_the_currently_selected_keymap="Test the currently selected keymap"
61msg_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."
62
63############################################################ FUNCTIONS
64
65# dialog_keymap_test $keymap
66#
67# Activate $keymap and display an input box (without cancel button) for the
68# user to test keyboard input and return. Always returns success.
69#
70dialog_keymap_test()
71{
72	local keym="$1"
73	local title= # Calculated below
74	local btitle= # Calculated below
75	local prompt="$msg_test_the_keymap_by_typing"
76	local hline=
77
78	# Attempt to activate the keymap
79	if [ "$keym" ]; then
80		local err
81		err=$( f_keymap_kbdcontrol "$keym" 2>&1 > /dev/null )
82		if [ "$err" ]; then
83			f_dialog_title "$msg_error"
84			f_dialog_msgbox "$err"
85			f_dialog_title_restore
86			return $FAILURE
87		fi
88	fi
89
90	f_dialog_title "$( printf "$msg_test_keymap" "${keym:-$msg_default}" )"
91	title="$DIALOG_TITLE"
92	btitle="$DIALOG_BACKTITLE"
93	f_dialog_title_restore
94
95	local height width
96	f_dialog_inputbox_size height width \
97		"$title" "$btitle" "$prompt" "" "$hline"
98
99	$DIALOG \
100		--title "$title"      \
101		--backtitle "$btitle" \
102		--hline "$hline"      \
103		--ok-label "$msg_ok"  \
104		--no-cancel           \
105		--inputbox "$prompt"  \
106		$height $width        \
107		2>/dev/null >&$DIALOG_TERMINAL_PASSTHRU_FD
108
109	return $DIALOG_OK
110}
111
112############################################################ MAIN
113
114#
115# Initialize
116#
117f_dialog_title "$msg_keymap_selection"
118f_dialog_backtitle "$msg_freebsd_installer"
119
120#
121# Die immediately if we can't dump the current keyboard map
122#
123#error=$( kbdcontrol -d 2>&1 > /dev/null ) || f_die $FAILURE "%s" "$error"
124
125# Capture Ctrl-C for clean-up
126trap 'rm -f $KEYMAPFILE; exit $FAILURE' SIGINT
127
128# Get a value from rc.conf(5) as initial value (if not being scripted)
129f_getvar $VAR_KEYMAP keymap
130if [ ! "$keymap" ]; then
131	keymap=$( f_sysrc_get keymap )
132	case "$keymap" in [Nn][Oo]) keymap="";; esac
133fi
134
135#
136# Loop until the user has finalized their selection (by clicking the
137# [relabeled] Cancel button).
138#
139width=67 first_pass=1 back_from_testing=
140[ "$USE_XDIALOG" ] && width=70
141prompt="$msg_keymap_menu_text"
142hline="$hline_arrows_tab_enter"
143while :; do
144	#
145	# Re/Build list of keymaps
146	#
147	cont_msg=$( printf "$msg_continue_with_keymap" \
148	                   "${keymap:-$msg_default}" )
149	test_msg=$( printf "$msg_test_keymap" "${keymap:-$msg_default}" )
150	menu_list="
151		'>>> $cont_msg' '' '$msg_continue_with_current_keymap'
152		'->- $test_msg' '' '$msg_test_the_currently_selected_keymap'
153	" # END-QUOTE
154	if [ "$first_pass" ]; then
155		defaultitem=
156		first_pass=
157	else
158		defaultitem="->- $test_msg"
159	fi
160	for k in $KEYMAPS; do
161		keymap_$k get keym keym
162		keymap_$k get desc desc
163		radio=" "
164		if [ "$keym" = "$keymap" ]; then
165			radio="*"
166			if [ "$back_from_testing" ]; then
167				defaultitem="(*) $desc"
168				back_from_testing=
169			fi
170		fi
171		f_shell_escape "$desc" desc
172		menu_list="$menu_list
173			'($radio) $desc' '' '$keym: $desc'
174		" # END-QUOTE
175	done
176	back_from_testing=
177
178	#
179	# Display keymap configuration menu
180	#
181	eval f_dialog_menu_with_help_size height \"\" rows \
182		\"\$DIALOG_TITLE\"     \
183		\"\$DIALOG_BACKTITLE\" \
184		\"\$prompt\"           \
185		\"\$hline\"            \
186		$menu_list
187	menu_choice=$( eval $DIALOG \
188		--title \"\$DIALOG_TITLE\"         \
189		--backtitle \"\$DIALOG_BACKTITLE\" \
190		--hline \"\$hline\"                \
191		--keep-tite                        \
192		--item-help                        \
193		--ok-label \"\$msg_select\"        \
194		--cancel-label \"\$msg_cancel\"    \
195		--default-item \"\$defaultitem\"   \
196		--menu \"\$prompt\"                \
197		$height $width $rows               \
198		${USE_DIALOG:+--} $menu_list       \
199		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
200	) || {
201		f_quietly rm -f "$KEYMAPFILE"
202		exit $FAILURE # Exit with an error so bsdinstall restarts
203	}
204	f_dialog_data_sanitize menu_choice
205
206	case "$menu_choice" in
207	">>> "*) # Continue with keymap
208		break ;;
209	"->-"*) # Test keymap
210		dialog_keymap_test "$keymap"
211		back_from_testing=1
212		continue ;;
213	esac
214
215	# Turn the user's choice into a number
216	n=$( eval f_dialog_menutag2index_with_help \
217		\"\$menu_choice\" $menu_list )
218
219	# Turn that number ithe name of the keymap struct
220	k=$( set -- $KEYMAPS; eval echo \"\${$(( $n - 2))}\" )
221
222	# Get actual keymap setting while we update $keymap and $KEYMAPFILE
223	keymap_$k get keym keymap
224	echo "keymap=\"$keymap\"" > "$KEYMAPFILE"
225done
226
227f_quietly f_keymap_kbdcontrol "$keymap"
228exit $SUCCESS
229
230################################################################################
231# END
232################################################################################
233