s243a

tazx -- /usr/bin -- TazPup Filesystem

Mar 5th, 2019
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.61 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # Tazx - Ncurses X configuration for SliTaz GNU/Linux using Dialog boxes.
  4. # This tiny util 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-2017 SliTaz GNU/Linux - GNU GPL v3.
  8. # Authors: Christophe Lincoln <[email protected]>
  9. #          Pascal Bellard <[email protected]>
  10. #
  11.  
  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
  16.  
  17. . /lib/libtaz.sh
  18. . /etc/slitaz/slitaz.conf
  19.  
  20. export TEXTDOMAIN='slitaz-tools' #i18n
  21. : ${DIALOG=dialog --timeout 60}
  22.  
  23.  
  24. # Default user for config files in Live mode, id is 1000 since it is
  25. # created by /etc/init.d/bootopts.sh.
  26.  
  27. USER=$(awk -F: '/1000/{print $1}' /etc/passwd)
  28.  
  29.  
  30. #
  31. # Functions
  32. #
  33.  
  34.  
  35. # Save chunk of xorg.conf into specified file
  36.  
  37. save_chunk() {
  38.     sed -e '/Section "'${1#*-}'"/,/EndSection/!d' \
  39.         -e "s/EndSection/&\n/" $xorg_template > \
  40.         $xorg_config/$1.conf
  41. }
  42.  
  43.  
  44. # Create a xorg.conf if needed
  45.  
  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. }
  55.  
  56.  
  57. # Generate or regenerate 40-Keyboard.conf
  58.  
  59. keyboard_conf() {
  60.     # Configure the keyboard with the correct keymap
  61.     keymap=$(cat /etc/keymap.conf)
  62.     keyboard_config="/etc/X11/xorg.conf.d/40-Keyboard.conf"
  63.     variant=""
  64.  
  65.     # this XkbOption moved from 40-evdev.conf (xorg-xf86-input-evdev)
  66.     options="terminate:ctrl_alt_bksp"
  67.     _n 'Creating:'; echo -n " $keyboard_config"
  68.     mkdir -p $(dirname $keyboard_config)
  69.  
  70.     # Reference: syslinux receipt; LXPanel keyboard layout switcher
  71.     case "$keymap" in
  72.         # Swiss FrenCH
  73.         fr_CH-latin1)   layout='ch'; variant='fr' ;;
  74.         # English UK
  75.         uk)             layout='gb' ;;
  76.         # Greek
  77.         gr)             layout='us,gr'
  78.             options="grp:ctrl_shift_toggle, grp_led:scroll, compose:ralt, $options" ;;
  79.         # Russian
  80.         ru)             layout='us,ru'
  81.             options="grp:ctrl_shift_toggle, grp_led:scroll, compose:ralt, $options" ;;
  82.         # Ukrainian
  83.         ua)             layout='us,ua,ru'
  84.             options="grp:ctrl_shift_toggle, compose:ralt, $options" ;;
  85.         # Slovenian
  86.         slovene)        layout='si' ;;
  87.         # Brasil (us-acentos); English US (acentos)
  88.         us-acentos)     layout='us'; variant='intl' ;;
  89.         # Deutsch Schweiz
  90.         de_CH-latin1)   layout='ch' ;;
  91.         # Deutsch Schweiz (sg) in syslinux menu
  92.         sg-latin1)
  93.             # I can't guess appropriate layout for this one
  94.             ;;
  95.         # Dvorak
  96.         dvorak)         layout='us'; variant='dvorak'   ;;
  97.         dvorak-l)       layout='us'; variant='dvorak-l' ;;
  98.         dvorak-r)       layout='us'; variant='dvorak-r' ;;
  99.         # French (Canada)
  100.         cf)             layout='ca' ;;
  101.         # Croatian
  102.         hr,croat|croat) layout='hr' ;;
  103.         # Japanese
  104.         jp106)          layout='jp' ;; # is ok?
  105.         # Dutch
  106.         nl2)            layout='nl' ;;
  107.         # Polish
  108.         pl2)            layout='pl' ;;
  109.         # Turkish
  110.         trq)            layout='tr' ;;
  111.         tr_q-latin5)    layout='tr'; variant='ku' ;; # is ok?
  112.         # Persian(Farsi) (Iran)
  113.         ir)             layout='us,ir'
  114.             options="grp:alt_shift_toggle, grp_led:scroll, $options" ;;
  115.         *)
  116.             # Use clean /etc/keymap.conf value.
  117.             keymap=${keymap%-latin1}
  118.             keymap=${keymap%-lat2}
  119.             keymap=${keymap%-lat6}
  120.             layout=${keymap%-abnt2}
  121.     esac
  122.  
  123.     update_conf=''
  124.     if [ -e "$keyboard_config" ]; then
  125.         actual_layout="$(awk 'BEGIN{FS="\""}{if($2=="XkbLayout")print $4}' $keyboard_config)"
  126.         # config not matched '/etc/keymap.conf', update
  127.         [ "$layout" != "$actual_layout" ] && update_conf='1'
  128.     else
  129.         # config absent, create
  130.         update_conf='1'
  131.     fi
  132.  
  133.     [ -n "$update_conf" ] && cat > $keyboard_config <<EOC
  134. Section "InputClass"
  135.     Identifier "Keyboard Defaults"
  136.     MatchIsKeyboard "yes"
  137.     Option "XkbLayout" "$layout"
  138.     Option "XkbVariant" "$variant"
  139.     Option "XkbOptions" "$options"
  140. EndSection
  141. EOC
  142.  
  143.     status
  144.     [ -z "$variant" ] && sed -i '/XkbVariant/d' $keyboard_config
  145. }
  146.  
  147.  
  148. # Populate xorg.conf.d
  149.  
  150. xorg_conf_d() {
  151.     # Define the xorg.conf.d (can be /etc/X11/xorg.conf.d or /usr/share/X11/xorg.conf.d)
  152.     xorg_config=/etc/X11/xorg.conf.d
  153.  
  154.     # Define the xorg.conf.new place.
  155.     xorg_template=/root/xorg.conf.new
  156.  
  157.     # Obtain a default configuration file from Xorg.
  158.     Xorg -configure :2
  159.  
  160.     # Backup existing config.
  161.     tar -cf $xorg_config/../Previous_xorg.conf.d.tar $xorg_config/ &>/dev/null
  162.  
  163.     # Put the different sections in separate files in the config directory.
  164.     save_chunk 10-ServerLayout
  165.     sed -i '/Core/d' $xorg_config/10-ServerLayout.conf
  166.     save_chunk 20-Files
  167.     save_chunk 30-Module
  168.     save_chunk 50-Monitor
  169.     save_chunk 60-Device
  170.     save_chunk 70-Screen
  171.  
  172.     # Remove the template.
  173.     rm $xorg_template
  174.  
  175.     # Keyboard & xorg.conf
  176.     keyboard_conf
  177.     gen_xorg_conf_file
  178. }
  179.  
  180.  
  181. # Process dialog's return code.
  182.  
  183. dialog_end() {
  184.     retval=$?
  185.     exec 3>&-
  186.     # Continue or exit.
  187.     case $retval in
  188.         0|3)   continue ;;
  189.         1|255) exit 0   ;;
  190.     esac
  191. }
  192.  
  193.  
  194. # Install xorg server.
  195.  
  196. install_xorg() {
  197.     [ -f "$PKGS_DB/packages.list" ] || tazpkg recharge
  198.  
  199.     exec 3>&1
  200.  
  201.     driver=$($DIALOG \
  202.         --clear --colors --scrollbar --title " $(_ 'Install Xorg') " \
  203.         --menu "$(_ 'Tazx helps you to select your X driver.')" 16 70 9 \
  204.         'auto' "$(_ 'Auto configuring')" \
  205.         $(fgrep xorg-xf86-video- $PKGS_DB/packages.list | cut -d- -f4 | \
  206.             sed "s|.*|& $(_ 'driver')|") \
  207.         'quit' "$(_ 'Quit')" \
  208.         2>&1 1>&3)
  209.  
  210.     dialog_end
  211.  
  212.     case $driver in
  213.         quit) exit 0 ;;
  214.         auto) autoselect_driver ;;
  215.         *)
  216.             inst_pkg xorg-server
  217.             inst_pkg xorg-xf86-video-$driver
  218.             xorg_conf_d ;;
  219.     esac
  220. }
  221.  
  222.  
  223. # Auto-select and install Xorg driver.
  224.  
  225. autoselect_driver() {
  226.     inst_pkg xorg-server
  227.  
  228.     for VID in $(busybox lspci | grep 'Class 03' | cut -c21-24 | sort -u); do
  229.         case $VID in
  230.             1022|1002) d=ati ;;
  231.             8086) d=intel ;;
  232.             10de) d=nouveau ;;
  233.             1039) d=sis ;;
  234.             15ad) d=vmware ;;
  235.             *) d=''; $DIALOG --colors --msgbox \
  236.                 "\n\Z1$(_n 'Failed to setup driver')\Zn $(_ '(unknown hardware)')" 7 68 ;;
  237.         esac
  238.     [ -n "$d" ] && inst_pkg xorg-xf86-video-$d
  239.     [ -n "$d" ] && inst_pkg mesa-dri-$d
  240.     done
  241. }
  242.  
  243.  
  244. # Install specified package.
  245.  
  246. inst_pkg() {
  247.     pkg="$@"
  248.     colorize 3 $(_ 'Installing pkg: %s' "$pkg")
  249.  
  250.     case $(id -u) in
  251.         0) CMD="tazpkg -gi $@" ;;
  252.         *) CMD="sudo tazpkg -gi $@" ;;
  253.     esac
  254.     [ -d "$INSTALLED/$@" ] || $CMD
  255. }
  256.  
  257.  
  258. # Select/install Window Manager and favorite applications.
  259.  
  260. apps_dialog() {
  261.     slitaz_apps_conf
  262.     . $user_conf
  263.  
  264.     exec 3>&1
  265.  
  266.     app=$($DIALOG --clear --colors --backtitle "$msg" --ok-label " $(_n 'Install')..." \
  267.         --title "{\Z3 $(_n 'Install Window Manager and applications') \Zn}" \
  268.         --inputmenu "" 21 52 18 \
  269.         $(for a in WINDOW_MANAGER EDITOR BROWSER TERMINAL FILE_MANAGER; do
  270.             eval n='$'$a; echo $a $n;
  271.         done) 2>&1 1>&3)
  272.     dialog_end
  273.  
  274.     case $app in
  275.         RENAMED*) # like: RENAMED EDITOR myeditor
  276.             appname=$(echo $app | cut -d" " -f3)
  277.                 app=$(echo $app | cut -d" " -f2) ;;
  278.         *)
  279.             app_name_dialog only-installed ;;
  280.     esac
  281.  
  282.     if [ "$appname" != " " ]; then
  283.         if [ "$appname" == 'show_ALL' ]; then
  284.             app_name_dialog full-list
  285.             inst_pkg $appname
  286.         fi
  287.         if [ -z $(busybox which $appname) ]; then
  288.             inst_pkg $appname
  289.             inst_pkg $(lzcat $PKGS_DB/files.list.lzma | fgrep /bin/$appname | head -n1 | cut -d: -f1)
  290.         fi
  291.         sed -i "/$app/ s|=.*$|=\"$appname\"|" $user_conf
  292.     fi
  293.  
  294.     msg=''; apps_dialog # loop
  295. }
  296.  
  297.  
  298. # Show list of applications to choose.
  299.  
  300. app_name_dialog() {
  301.     case $1 in
  302.         full-list) height='24' ;;
  303.         *)         height='18' ;;
  304.     esac
  305.  
  306.     exec 3>&1
  307.  
  308.     appname=$($DIALOG --clear --colors --title "$app" \
  309.         --menu '' $height 50 24 $(gen_applist $app $@) 2>&1 1>&3)
  310.     dialog_end
  311. }
  312.  
  313.  
  314. # Generate application list for Dialog or Yad.
  315.  
  316. gen_applist() {
  317.     # $1 = [ FILE_MANAGER | BROWSER | EDITOR | TERMINAL | WINDOW_MANAGER ]
  318.     # $2 = [ only-installed | full-list ]
  319.     # $3 = [ yad | * ]
  320.  
  321.     . $user_conf
  322.  
  323.     FILE_MANAGERS="clex caja emelfm2 mc pathfinder pcmanfm pcmanfm-qt ranger \
  324. spacefm thunar xfe ytree"
  325.  
  326.     BROWSERS="arora chrome cream dillo elinks firefox firefox-official iron \
  327. links lynx midori netsurf opera palemoon QtWeb qupzilla retawq seamonkey surf \
  328. tazweb w3m xombrero"
  329.  
  330.     EDITORS="adie beaver bluefish emacs geany jed joe juffed ht le leafpad \
  331. mcedit mousepad mp-5 nano qedit SciTE tea vi vim xedit xfw zile"
  332.  
  333.     TERMINALS="aterm evilvte lxterminal mrxvt qterminal sakura stjerm tilda \
  334. urxvt vte xfce4-terminal xterm yeahconsole"
  335.  
  336.     WINDOW_MANAGERS="awesome blackbox compiz dwm echinus enlightenment fluxbox \
  337. icewm jwm lxde-session lxqt karmen matchbox openbox-session pekwm ratpoison \
  338. xfwm4"
  339.  
  340.     eval list='$'"$1"S
  341.     eval current='$'"$1"
  342.  
  343.     for i in $current $(echo " $list " | sed "s| $current | |g"); do
  344.  
  345.         case $(busybox which $i) in
  346.             '') # not installed
  347.                 t='---' ;;
  348.             *)  # installed
  349.                 t='<--' ;;
  350.         esac
  351.  
  352.         if [ "$2" == 'full-list' ] || [ "$t" == '<--' ]; then
  353.             echo "$i"
  354.  
  355.             if [ "$3" != 'yad' ]; then
  356.                 echo "$(echo $t | sed "s|---|$(_n 'Install')|g")"
  357.             fi
  358.         fi
  359.     done
  360.  
  361.     if [ "$2" == 'only-installed' ] && [ "$3" != 'yad' ]; then
  362.         echo 'show_ALL' "+$(_n 'Install')"
  363.     fi
  364. }
  365.  
  366.  
  367. # Screen configuration dialog.
  368.  
  369. config_dialog() {
  370.     . /etc/rcS.conf
  371.     case $LOGIN_MANAGER in
  372.         slim) LM="$(_ 'ON')"  ;;
  373.         *)    LM="$(_ 'OFF')" ;;
  374.     esac
  375.  
  376.     exec 3>&1
  377.  
  378.     value=$($DIALOG \
  379.         --clear --colors \
  380.         --title "{ $(_n 'SliTaz Xorg config') }" \
  381.         --menu "" 16 72 10 \
  382.     "xorg"        "$(_n 'Install or reconfigure Xorg')" \
  383.     "xorg-light"  "$(_n 'Install Xorg server (light version)')" \
  384.     "onboot"      "$(_n 'AutoStart Xorg by SLiM on boot'): \Z5$LM\Zn" \
  385.     "slim"        "$(_n '(Re)Start Simple Login Manager now')" \
  386.     "wm_apps"     "$(_n 'Install Window Manager and applications')" \
  387.     "quit"        "$(_n 'Quit Tazx utility')" \
  388.     2>&1 1>&3)
  389.     dialog_end
  390.  
  391.     case "$value" in
  392.         xorg)
  393.             install_xorg ;;
  394.  
  395.         xorg-light)
  396.             inst_pkg xorg-server-light
  397.             inst_pkg xorg-xf86-video-fbdev
  398.             xorg_conf_d ;;
  399.  
  400.         onboot)
  401.             inst_pkg slim
  402.             inst_pkg dbus
  403.  
  404.             if [ $LM == "$(_ 'OFF')" ]; then
  405.                 LM='slim'
  406.             else
  407.                 LM=''
  408.             fi
  409.             sed -i "/LOGIN_MANAGER/ s|=.*$|=$LM|" /etc/rcS.conf
  410.  
  411.             [ -z "$LM" ] && LM='disabled'
  412.             _n "Start X on boot:"; boldify " $LM" ;;
  413.  
  414.         slim)
  415.             inst_pkg slim
  416.             inst_pkg dbus
  417.             if [ -z $XAUTHORITY ]; then
  418.                 stopd slim; sleep 2; startd slim
  419.             fi ;;
  420.  
  421.         wm_apps)
  422.             apps_dialog ;;
  423.  
  424.         *)
  425.             exit 0 ;;
  426.     esac
  427. }
  428.  
  429.  
  430. # ~/.config/slitaz/applications.conf: Missing = failed to login
  431. # Make users applications.conf in /etc/skel so new added user will get
  432. # a working X session. Note --> flavors can have a custom config in
  433. # /etc/slitaz/applications that we must use.
  434.  
  435. slitaz_apps_conf() {
  436.     apps_conf=/etc/slitaz/applications.conf
  437.  
  438.     [ -f $apps_conf ] || inst_pkg slitaz-configs --forced
  439.  
  440.     if [ "$(id -u)" -eq 0 ]; then
  441.         user_conf="$apps_conf"
  442.         new_user_conf='/etc/skel/.config/slitaz/applications.conf'
  443.         mkdir -p $(dirname $new_user_conf)
  444.         cp -f $apps_conf $new_user_conf
  445.     else
  446.         # If started by: startx and with boot opts screen=text
  447.         user_conf="${XDG_CONFIG_HOME:-$HOME/.config}/slitaz/applications.conf"
  448.         if [ ! -f "$user_conf" ]; then
  449.             mkdir -p $(dirname $user_conf)
  450.             cp $apps_conf $user_conf
  451.         fi
  452.     fi
  453.     [ -f "$HOME/.xinitrc" ] || inst_pkg slitaz-configs-base --forced
  454. }
  455.  
  456.  
  457. # Handle live options: screen= video=
  458.  
  459. proc_cmdline()
  460. {
  461.     if grep -qs 'screen=' /proc/cmdline ; then
  462.         MODE="$(sed 's/.*screen=\([0-9]*x[0-9]*\).*/\1/' < /proc/cmdline)"
  463.         sed "s/.*EndSubSection.*/\\t\\tModes\\t\"$MODE\"\\n&/" \
  464.             -i ${xorg_config}/70-Screen.conf
  465.     elif grep -qs 'video=' /proc/cmdline ; then
  466.         MODE="$(sed 's/.*video=\([0-9]*x[0-9]*\).*/\1/' < /proc/cmdline)"
  467.         DEPTH=$(sed 's/.*video=.*-\([0-9]*\).*/\1/' < /proc/cmdline)
  468.         sed "s/.*EndSubSection.*/\\t\\tModes\\t\"$MODE\"\\n&/" \
  469.             -i ${xorg_config}/70-Screen.conf
  470.         set_depth $DEPTH
  471.     fi
  472. }
  473.  
  474.  
  475. set_depth()
  476. {
  477.     case "$1" in
  478.         8|15|16|24|32)  colors=$1 ;;
  479.         *)
  480.             exec 3>&1
  481.             colors=$($DIALOG --menu " " 12 12 12 \
  482.             32 truecolor 24 truecolor 16 hicolor 15 hicolor 8 256colors 2>&1 1>&3)
  483.             dialog_end ;;
  484.     esac
  485.     (grep -q 'DefaultDepth' ${xorg_config}/70-Screen.conf) || \
  486.     sed 's|Monitor.*"$|&\n\tDefaultDepth '$colors'|' -i ${xorg_config}/70-Screen.conf
  487. }
  488.  
  489.  
  490.  
  491. # Commands
  492.  
  493. case "$1" in
  494.     install-xorg)
  495.         check_root
  496.         install_xorg
  497.         slitaz_apps_conf ;;
  498.  
  499.     config-xorg)
  500.         check_root
  501.         slitaz_apps_conf
  502.         xorg_conf_d
  503.         proc_cmdline ;;
  504.  
  505.     init)
  506.         # We don't need much config files with last Xorg version, So just
  507.         # get files for starting an X session and config the keyboard.
  508.         check_root
  509.         slitaz_apps_conf
  510.         keyboard_conf
  511.         gen_xorg_conf_file ;;
  512.  
  513.     keyboard)
  514.         keyboard_conf ;;
  515.  
  516.     get-applist)
  517.         slitaz_apps_conf
  518.         gen_applist $2 $3 $4 ;;
  519.  
  520.     auto)
  521.         autoselect_driver ;;
  522.  
  523.     bpp)
  524.         check_root
  525.         xorg_conf_d
  526.         proc_cmdline
  527.         set_depth $2 ;;
  528.  
  529.     *help|-h|-?)
  530.         emsg "
  531. <b>tazx</b> - $(_ 'Configuration tool for SliTaz GNU/Linux')
  532.  
  533. <c 33>$(_ 'Usage:')</c> <b>tazx</b> <c 34>$(_ 'command')</c> $(_ 'parameter')
  534.  
  535. <c 34>$(_ 'Commands:')</c>
  536. install-xorg  $(_ 'Install Xorg and selected Xorg driver')
  537. config-xorg   $(_ 'Configure Xorg, generate configuration files')
  538. bpp           $(_ 'Set monitor default depth (valid parameters: 8, 15, 16 or 24)')
  539. auto          $(_ 'Auto-select and install Xorg driver')
  540. init          $(_ 'Get files for starting an X session and config the keyboard')
  541. keyboard      $(_ 'Generate or regenerate Xorg keyboard config')
  542. "
  543.         ;;
  544.  
  545.     *)
  546.         # User can get a new .xinitrc with tazx from cmdline.
  547.         case $(id -u) in
  548.             0)
  549.                 config_dialog ;;
  550.             *)
  551.                 msg="\Z1 $(_n 'Run tazx as root if you need to configure xorg') \Zn"
  552.                 apps_dialog ;;
  553.         esac
  554.         # gen_xsession
  555.         ;;
  556. esac
  557.  
  558. exit 0
Advertisement
Add Comment
Please, Sign In to add comment