slitaz-tools view tinyutils/tazx @ rev 948

tazx: fix Xorg keyboard layouts (thanks kubepc)
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Jul 31 17:21:47 2015 +0300 (2015-07-31)
parents 3b8de2d6aeb3
children 33bac140fd6d
line source
1 #!/bin/sh
2 #
3 # Tazx - Ncurses X configuration for SliTaz GNU/Linux using Dialog boxes.
4 # This tinyutil is part of slitaz-tools. Tazx can configure Xorg with
5 # several Window Managers. The GTK interface to configure X is in tazbox.
6 #
7 # (c) 2011-2015 SliTaz GNU/Linux - GNU GPL v3.
8 # Authors: Christophe Lincoln <pankso@slitaz.org>
9 # Pascal Bellard <pascal.bellard@slitaz.org>
10 #
12 # 20140317: most xsession, xinitrc are obsolete, provided system files
13 # are in /etc/skel and WM config files should no longer be copied by
14 # tazx to user home. If some configs are needed by WMs they should
15 # be set by a /usr/bin/wmname-session script I guess. - Pankso
17 . /lib/libtaz.sh
18 . /etc/slitaz/slitaz.conf
20 export TEXTDOMAIN='slitaz-tools' #i18n
21 : ${DIALOG=dialog --timeout 60}
24 # Default user for config files in Live mode, id is 1000 since it is
25 # created by /etc/init.d/bootopts.sh.
27 USER=$(awk -F: '/1000/{print $1}' /etc/passwd)
30 #
31 # Functions
32 #
35 # Save chunk of xorg.conf into specified file
37 save_chunk() {
38 sed -e '/Section "'${1#*-}'"/,/EndSection/!d' \
39 -e "s/EndSection/&\n/" $xorg_template > \
40 $xorg_config/$1.conf
41 }
44 # Create a xorg.conf if needed
46 gen_xorg_conf_file() {
47 if [ ! -f /etc/X11/xorg.conf ]; then
48 cat > /etc/X11/xorg.conf << EOT
49 # You can put here your own Xorg configurations. This config file is read
50 # before all files in /etc/X11/xorg.conf.d and will NOT be erased by any
51 # updates.
52 EOT
53 fi
54 }
57 # Add or remove xkb applet from lxpanel
59 xkb() {
60 case $(id -u) in
61 0)
62 # Empty if lxpanel not running, else something like:
63 # tux lxpanel -p slitaz
64 LXPS=$(ps -ouser,args | sed -n '/[l]xpanel/ s| *| |p' | \
65 fgrep -v '[' | fgrep -v 'xkb' | fgrep -v 'sh')
67 # Stop lxpanel
68 if [ -n "$LXPS" ]; then
69 lxpanelctl exit
70 killall lxpanel
71 fi
73 # For all human users
74 for USER in $(awk -F: '/\/home\//{print $1}' /etc/passwd); do
75 su $USER -c '. ~/.profile; tazx xkb'
76 done
78 # Re-run lxpanel
79 [ -n "$LXPS" ] && su ${LXPS%% *} -c "exec ${LXPS#* }" &
80 ;;
82 *)
83 DOTCONFIG=${XDG_CONFIG_HOME:-$HOME/.config}
84 CFG_LXPANEL=$DOTCONFIG/lxpanel
86 if [ ! -d $CFG_LXPANEL ]; then
87 mkdir -p $DOTCONFIG
88 cp -r /etc/lxpanel $DOTCONFIG
89 fi
91 case $(fgrep 'XkbLayout' /etc/X11/xorg.conf.d/40-Keyboard.conf | \
92 fgrep -o ',') in
93 '')
94 for CFG in $(find $CFG_LXPANEL -type f -name panel); do
95 if [ -n "$(grep 'type = xkb' $CFG)" ]; then
96 echo "Remove xkb applet from $CFG"
97 sed -i "$(grep -n -e "^Plugin {" -e "^}" -e "xkb" \
98 $CFG | grep -C 1 xkb | grep -v xkb | cut -d: \
99 -f1 | tr '\n' ' ' | sed 's| |,|')d" $CFG
100 fi
101 done ;;
102 ',')
103 for CFG in $(find $CFG_LXPANEL -type f -name panel); do
104 if [ -z "$(grep 'type = xkb' $CFG)" ]; then
105 echo "Add xkb applet to $CFG"
106 XKB='Plugin {\
107 type = xkb\
108 Config {\
109 DisplayType=0\
110 PerWinLayout=0\
111 NoResetOpt=0\
112 KeepSysLayouts=1\
113 Model=pc105\
114 LayoutsList=us\
115 VariantsList=,\
116 ToggleOpt=shift_caps_toggle\
117 FlagSize=4\
118 }\
119 }\
120 '
121 sed -i "$(grep -n -e "^Plugin {" -e "tray" $CFG | \
122 grep -B1 tray | head -n1 | cut -d: -f1) i\\
123 $XKB" $CFG
124 fi
125 done ;;
126 esac ;;
127 esac
128 }
131 # Generate or regenerate 40-Keyboard.conf
133 keyboard_conf() {
134 # Configure the keyboard with the right keymap
135 keymap=$(cat /etc/keymap.conf)
136 keyboard_config="/etc/X11/xorg.conf.d/40-Keyboard.conf"
137 variant=""
139 # this XkbOption moved from 40-evdev.conf (xorg-xf86-input-evdev)
140 options="terminate:ctrl_alt_bksp"
141 _n 'Creating:'; echo -n " $keyboard_config"
142 mkdir -p $(dirname $keyboard_config)
144 # Reference: syslinux receipt; LXPanel keyboard layout switcher
145 case "$keymap" in
146 # Swiss FrenCH
147 fr_CH-latin1) layout='ch'; variant='fr' ;;
148 # English UK
149 uk) layout='gb' ;;
150 # Greek
151 gr) layout='us,gr'
152 options="grp:ctrl_shift_toggle, grp_led:scroll, compose:ralt, $options" ;;
153 # Russian
154 ru) layout='us,ru'
155 options="grp:ctrl_shift_toggle, grp_led:scroll, compose:ralt, $options" ;;
156 # Ukrainian
157 ua) layout='us,ua,ru'
158 options="grp:ctrl_shift_toggle, compose:ralt, $options" ;;
159 # Slovenian
160 slovene) layout='si' ;;
161 # Brasil (us-acentos); English US (acentos)
162 us-acentos) layout='us'; variant='intl' ;;
163 # Deutsch Schweiz
164 de_CH-latin1) layout='ch' ;;
165 # Deutsch Schweiz (sg) in syslinux menu
166 sg-latin1)
167 # I can't guess appropriate layout for this one
168 ;;
169 # Dvorak
170 dvorak) layout='us'; variant='dvorak' ;;
171 dvorak-l) layout='us'; variant='dvorak-l' ;;
172 dvorak-r) layout='us'; variant='dvorak-r' ;;
173 # French (Canada)
174 cf) layout='ca' ;;
175 # Croatian
176 hr,croat|croat) layout='hr' ;;
177 # Japanese
178 jp106) layout='jp' ;; # is ok?
179 # Dutch
180 nl2) layout='nl' ;;
181 # Polish
182 pl2) layout='pl' ;;
183 # Turkish
184 trq) layout='tr' ;;
185 tr_q-latin5) layout='tr'; variant='ku' ;; # is ok?
186 *)
187 # Use clean /etc/keymap.conf value.
188 keymap=${keymap%-latin1}
189 keymap=${keymap%-lat2}
190 keymap=${keymap%-lat6}
191 layout=${keymap%-abnt2}
192 esac
194 cat > $keyboard_config << EOC
195 Section "InputClass"
196 Identifier "Keyboard Defaults"
197 MatchIsKeyboard "yes"
198 Option "XkbLayout" "$layout"
199 Option "XkbVariant" "$variant"
200 Option "XkbOptions" "$options"
201 EndSection
202 EOC
204 status
205 [ -z "$variant" ] && sed -i '/XkbVariant/d' $keyboard_config
206 [ -z $(busybox which lxpanel) ] || xkb
207 }
210 # Populate xorg.conf.d
212 xorg_conf_d() {
213 # Define the xorg.conf.d (can be /etc/X11/xorg.conf.d or /usr/share/X11/xorg.conf.d)
214 xorg_config=/etc/X11/xorg.conf.d
216 # Define the xorg.conf.new place.
217 xorg_template=/root/xorg.conf.new
219 # Obtain a default configuration file from Xorg.
220 Xorg -configure :2
222 # Backup existing config.
223 tar -cf $xorg_config/../Previous_xorg.conf.d.tar $xorg_config/ &> /dev/null
225 # Put the different sections in separate files in the config directory.
226 save_chunk 10-ServerLayout
227 sed -i '/Core/d' $xorg_config/10-ServerLayout.conf
228 save_chunk 20-Files
229 save_chunk 30-Module
230 save_chunk 50-Monitor
231 save_chunk 60-Device
232 save_chunk 70-Screen
234 # Remove the template.
235 rm $xorg_template
237 # Keyboard & xorg.conf
238 keyboard_conf
239 gen_xorg_conf_file
240 }
243 # Process dialog's return code.
245 dialog_end() {
246 retval=$?
247 exec 3>&-
248 # Continue or exit.
249 case $retval in
250 0|3) continue ;;
251 1|255) exit 0 ;;
252 esac
253 }
256 # Install xorg server.
258 install_xorg() {
259 [ -f "$PKGS_DB/packages.list" ] || tazpkg recharge
261 exec 3>&1
263 driver=$($DIALOG \
264 --clear --colors --scrollbar --title " $(_ 'Install Xorg') " \
265 --menu "$(_ 'Tazx helps you to select your X driver.')" 16 70 9 \
266 'auto' "$(_ 'Auto configuring')" \
267 $(fgrep xorg-xf86-video- $PKGS_DB/packages.list | cut -d- -f4 | \
268 sed "s|.*|& $(_ 'driver')|") \
269 'quit' "$(_ 'Quit')" \
270 2>&1 1>&3)
272 dialog_end
274 case $driver in
275 quit) exit 0 ;;
276 auto) autoselect_driver ;;
277 *)
278 inst_pkg xorg-server
279 inst_pkg xorg-xf86-video-$driver
280 xorg_conf_d ;;
281 esac
282 }
285 # Auto-select and install Xorg driver.
287 autoselect_driver() {
288 inst_pkg xorg-server
290 for VID in $(busybox lspci | grep 'Class 03' | cut -c21-24 | sort -u); do
291 case $VID in
292 1022|1002) d=ati ;;
293 8086) d=intel ;;
294 10de) d=nouveau ;;
295 1039) d=sis ;;
296 15ad) d=vmware ;;
297 *) d=''; $DIALOG --colors --msgbox \
298 "\n\Z1$(_n 'Failed to setup driver')\Zn $(_ '(unknown hardware)')" 7 68 ;;
299 esac
300 [ -n "$d" ] && inst_pkg xorg-xf86-video-$d
301 done
302 }
305 # Install specified package.
307 inst_pkg() {
308 pkg="$@"
309 colorize 3 $(_ 'Installing pkg: %s' "$pkg")
311 case $(id -u) in
312 0) CMD="tazpkg -gi $@" ;;
313 *) CMD="sudo tazpkg -gi $@" ;;
314 esac
315 [ -d "$INSTALLED/$@" ] || $CMD
316 }
319 # Select/install Window Manager and favorite applications.
321 apps_dialog() {
322 slitaz_apps_conf
323 . $user_conf
325 exec 3>&1
327 app=$($DIALOG --clear --colors --backtitle "$msg" \
328 --title "{\Z3 $(_n 'Install Window Manager and applications') \Zn}" \
329 --inputmenu "" 21 52 18 \
330 $(for a in WINDOW_MANAGER EDITOR BROWSER TERMINAL FILE_MANAGER; do
331 eval n='$'$a; echo $a $n;
332 done) 2>&1 1>&3)
333 dialog_end
335 case $app in
336 RENAMED*) # like: RENAMED EDITOR myeditor
337 appname=$(echo $app | cut -d" " -f3)
338 app=$(echo $app | cut -d" " -f2) ;;
339 *)
340 app_name_dialog only-installed ;;
341 esac
343 if [ "$appname" != " " ]; then
344 if [ "$appname" == 'show_ALL' ]; then
345 app_name_dialog full-list
346 inst_pkg $appname
347 fi
348 if [ -z $(busybox which $appname) ]; then
349 inst_pkg $appname
350 inst_pkg $(lzcat $PKGS_DB/files.list.lzma | fgrep /bin/$appname | head -n1 | cut -d: -f1)
351 fi
352 sed -i "/$app/ s|=.*$|=\"$appname\"|" $user_conf
353 fi
355 msg=''; apps_dialog # loop
356 }
359 # Show list of applications to choose.
361 app_name_dialog() {
362 case $1 in
363 full-list) height='24' ;;
364 *) height='18' ;;
365 esac
367 exec 3>&1
369 appname=$($DIALOG --clear --colors --title "$app" \
370 --menu '' $height 50 24 $(gen_applist $app $@) 2>&1 1>&3)
371 dialog_end
372 }
375 # Generate application list for Dialog or Yad.
377 gen_applist() {
378 # $1 = [ FILE_MANAGER | BROWSER | EDITOR | TERMINAL | WINDOW_MANAGER ]
379 # $2 = [ only-installed | full-list ]
380 # $3 = [ yad | * ]
382 . $user_conf
384 FILE_MANAGERS="clex caja emelfm2 mc pathfinder pcmanfm pcmanfm-qt ranger \
385 spacefm thunar xfe ytree"
387 BROWSERS="arora chrome cream dillo elinks firefox firefox-official iron \
388 links lynx midori netsurf opera QtWeb qupzilla retawq seamonkey surf tazweb \
389 w3m xombrero"
391 EDITORS="adie beaver bluefish emacs geany jed joe juffed ht le leafpad \
392 mcedit mousepad mp-5 nano qedit SciTE tea vi vim xedit xfw zile"
394 TERMINALS="aterm evilvte lxterminal mrxvt qterminal sakura stjerm tilda \
395 urxvt vte xfce4-terminal xterm yeahconsole"
397 WINDOW_MANAGERS="awesome blackbox compiz dwm echinus enlightenment fluxbox \
398 icewm jwm lxde-session lxqt karmen matchbox openbox-session pekwm ratpoison \
399 xfwm4"
401 eval list='$'"$1"S
402 eval current='$'"$1"
404 for i in $current $(echo " $list " | sed "s| $current | |g"); do
406 case $(busybox which $i) in
407 '') # not installed
408 t='---' ;;
409 *) # installed
410 t='<--' ;;
411 esac
413 if [ "$2" == 'full-list' ] || [ "$t" == '<--' ]; then
414 echo "$i"
416 if [ "$3" != 'yad' ]; then
417 echo "$(echo $t | sed "s|---|$(_n 'Install')|g")"
418 fi
419 fi
420 done
422 if [ "$2" == 'only-installed' ] && [ "$3" != 'yad' ]; then
423 echo 'show_ALL' "+$(_n 'Install')"
424 fi
425 }
428 # Screen configuration dialog.
430 config_dialog() {
431 . /etc/rcS.conf
432 case $LOGIN_MANAGER in
433 slim) LM="$(_ 'ON')" ;;
434 *) LM="$(_ 'OFF')" ;;
435 esac
437 exec 3>&1
439 value=$($DIALOG \
440 --clear --colors \
441 --title "{ $(_n 'SliTaz Xorg config') }" \
442 --menu "" 16 72 10 \
443 "xorg" "$(_n 'Install or reconfigure Xorg')" \
444 "xorg-light" "$(_n 'Install Xorg server (light version)')" \
445 "onboot" "$(_n 'AutoStart Xorg by SLiM on boot'): \Z5$LM\Zn" \
446 "slim" "$(_n '(Re)Start Simple Login Manager now')" \
447 "wm_apps" "$(_n 'Install Window Manager and applications')" \
448 "quit" "$(_n 'Quit Tazx utility')" \
449 2>&1 1>&3)
450 dialog_end
452 case "$value" in
453 xorg)
454 install_xorg ;;
456 xorg-light)
457 inst_pkg xorg-server-light
458 inst_pkg xorg-xf86-video-fbdev
459 xorg_conf_d ;;
461 onboot)
462 inst_pkg slim
463 inst_pkg dbus
465 if [ $LM == "$(_ 'OFF')" ]; then
466 LM='slim'
467 else
468 LM=''
469 fi
470 sed -i "/LOGIN_MANAGER/ s|=.*$|=$LM|" /etc/rcS.conf
472 [ -z "$LM" ] && LM='disabled'
473 _n "Start X on boot:"; boldify " $LM" ;;
475 slim)
476 inst_pkg slim
477 inst_pkg dbus
478 if [ -z $XAUTHORITY ]; then
479 stopd slim; sleep 2; startd slim
480 fi ;;
482 wm_apps)
483 apps_dialog ;;
485 *)
486 exit 0 ;;
487 esac
488 }
491 # ~/.config/slitaz/applications.conf: Missing = failed to login
492 # Make users applications.conf in /etc/skel so new added user will get
493 # a working X session. Note --> flavors can have a custom config in
494 # /etc/slitaz/applications that we must use.
496 slitaz_apps_conf() {
497 apps_conf=/etc/slitaz/applications.conf
499 [ -f $apps_conf ] || inst_pkg slitaz-configs --forced
501 if [ "$(id -u)" -eq 0 ]; then
502 user_conf="$apps_conf"
503 new_user_conf='/etc/skel/.config/slitaz/applications.conf'
504 mkdir -p $(dirname $new_user_conf)
505 cp -f $apps_conf $new_user_conf
506 else
507 # If started by: startx and with boot opts screen=text
508 user_conf="${XDG_CONFIG_HOME:-$HOME/.config}/slitaz/applications.conf"
509 if [ ! -f "$user_conf" ]; then
510 mkdir -p $(dirname $user_conf)
511 cp $apps_conf $user_conf
512 fi
513 fi
514 [ -f "$HOME/.xinitrc" ] || inst_pkg slitaz-configs-base --forced
515 }
518 # Handle live options: screen= video=
520 proc_cmdline()
521 {
522 if grep -qs 'screen=' /proc/cmdline ; then
523 MODE="$(sed 's/.*screen=\([0-9]*x[0-9]*\).*/\1/' < /proc/cmdline)"
524 sed "s/.*EndSubSection.*/\\t\\tModes\\t\"$MODE\"\\n&/" \
525 -i ${xorg_config}/70-Screen.conf
526 elif grep -qs 'video=' /proc/cmdline ; then
527 MODE="$(sed 's/.*video=\([0-9]*x[0-9]*\).*/\1/' < /proc/cmdline)"
528 DEPTH=$(sed 's/.*video=.*-\([0-9]*\).*/\1/' < /proc/cmdline)
529 sed "s/.*EndSubSection.*/\\t\\tModes\\t\"$MODE\"\\n&/" \
530 -i ${xorg_config}/70-Screen.conf
531 set_depth $DEPTH
532 fi
533 }
536 set_depth()
537 {
538 case "$1" in
539 8|15|16|24|32) colors=$1 ;;
540 *)
541 exec 3>&1
542 colors=$($DIALOG --menu " " 12 12 12 \
543 32 truecolor 24 truecolor 16 hicolor 15 hicolor 8 256colors 2>&1 1>&3)
544 dialog_end ;;
545 esac
546 sed 's|Monitor.*"$|&\n\tDefaultDepth '$colors'|' -i ${xorg_config}/70-Screen.conf
547 }
551 # Commands
553 case "$1" in
554 install-xorg)
555 check_root
556 install_xorg
557 slitaz_apps_conf ;;
559 config-xorg)
560 check_root
561 slitaz_apps_conf
562 xorg_conf_d
563 proc_cmdline ;;
565 init)
566 # We don't need much config files with last Xorg version, So just
567 # get files for starting an X session and config the keyboard.
568 check_root
569 slitaz_apps_conf
570 keyboard_conf
571 gen_xorg_conf_file ;;
573 keyboard)
574 keyboard_conf ;;
576 get-applist)
577 slitaz_apps_conf
578 gen_applist $2 $3 $4 ;;
580 auto)
581 autoselect_driver ;;
583 bpp)
584 check_root
585 xorg_conf_d
586 proc_cmdline
587 set_depth $2 ;;
589 xkb)
590 xkb ;;
593 *help|-h|-?)
594 emsg "
595 <b>tazx</b> - $(_ 'Configuration tool for SliTaz GNU/Linux')
597 <c 33>$(_ 'Usage:')</c> <b>tazx</b> <c 34>$(_ 'command')</c> $(_ 'parameter')
599 <c 34>$(_ 'Commands:')</c>
600 install-xorg $(_ 'Install Xorg and selected Xorg driver')
601 config-xorg $(_ 'Configure Xorg, generate configuration files')
602 bpp $(_ 'Set monitor default depth (valid parameters: 8, 15, 16 or 24)')
603 auto $(_ 'Auto-select and install Xorg driver')
604 init $(_ 'Get files for starting an X session and config the keyboard')
605 keyboard $(_ 'Generate or regenerate Xorg keyboard config')
606 "
607 ;;
609 *)
610 # User can get a new .xinitrc with tazx from cmdline.
611 case $(id -u) in
612 0)
613 config_dialog ;;
614 *)
615 msg="\Z1 $(_n 'Run tazx as root if you need to configure xorg') \Zn"
616 apps_dialog ;;
617 esac
618 # gen_xsession
619 ;;
620 esac
622 exit 0