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