#!/bin/sh
#(c) Barry Kauler 2005/2006 www.puppylinux.com
#(c) rarsa, zigbert, rerwin, rodin.s, ecube, 01micko, etc
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#keyboard/mouse wizard for Puppy Linux
#130216 add button for "mouse sensitivity", run pupx.

export TEXTDOMAIN=input-wizard
export OUTPUT_CHARSET=UTF-8

#==============================================================
#    MOUSE WIZARD (type of mouse, xmodmap, xorg.conf opts)
#==============================================================

if [ "$1" = "mouse" ] ; then
	MOUSEDEV="`cat /etc/mousedevice`"
	DEF_SERIAL="false"
	DEF_USB="false"
	case $MOUSEDEV in 
		tty*) DEF_SERIAL="true" ;;
		*) DEF_USB="true" ;;
	esac

	CHK_SCROLL_WHEEL="true" #Mouse scroll wheel
	grep -q '#Option\W\+"ZAxisMapping' /etc/X11/xorg.conf && CHK_SCROLL_WHEEL="false"

	CHK_MIDDLE_BUTTON="true"
	grep -q '#Option\W\+"Emulate3Buttons' /etc/X11/xorg.conf && CHK_MIDDLE_BUTTON="false"

	CHK_AUTOHIDE_MOUSE="false"
	IDLETIME="0"
	if [ -f /etc/mousehide ];then
		IDLETIME="`cat /etc/mousehide | cut -f 1 -d ','`"
		[ ! "$IDLETIME" = "0" ] && CHK_AUTOHIDE_MOUSE="true"
	fi

	# if the button 1 is mapped to something different than 1 then I assume
	# it is a lefhanded mouse
	CHK_LEFTHANDED_MOUSE="false"
	LEFT_H_MOUSE=`xmodmap -pp | grep -om1 "[0-9]$"`
	if [ ${LEFT_H_MOUSE} -ne 1 ] ; then
		CHK_LEFTHANDED_MOUSE="true"
	fi

	#v4.11 Control "tapping" function on ALPS/Synaptics touchpads - manipulates MaxTapTime option in xorg.conf.
	TAPCHKBOX=""
	if [ -f /etc/X11/xorg.conf -a "`grep '"MaxTapTime"' /etc/X11/xorg.conf`" != "" ];then #touchpad present
		[ "`grep '"MaxTapTime"' /etc/X11/xorg.conf | grep '"0"'`" = "" ] && CHECKT="true" || CHECKT="false" #taps enabled/disabled
		TAPCHKBOX="    <checkbox>
     <label>$(gettext 'Enable touchpad tapping for mouse button clicks')</label>
     <variable>CHECKBOX013</variable>
     <default>$CHECKT</default>
    </checkbox>
"
	fi
  
	export MouseWizard='
<window title="'$(gettext 'Mouse Wizard')'" icon-name="gtk-preferences">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed mouse.svg 60 "<b>$(gettext "Configure your mouse.")</b>" "$(gettext "If you got a touchpad, choose USB interface.")"`"'
  <hbox space-expand="true" space-fill="true">
  <hbox space-expand="false" space-fill="false">
  <vbox  space-expand="true" space-fill="true">
    <frame '$(gettext 'Interface')'>
      <hbox space-expand="false" space-fill="false">
        <vbox space-expand="false" space-fill="false">
           <text height-request="1"><label>""</label></text>
           <radiobutton height-request="45">
            <label>'$(gettext 'USB or PS/2')'</label>
            <variable>RADIO_USB</variable>
            <default>'$DEF_USB'</default>
            <action>if true disable:COMPORT</action>
          </radiobutton>
          <radiobutton height-request="45">
            <label>'$(gettext 'Serial')'</label>
            <variable>RADIO_SERIAL</variable>
            <default>'$DEF_SERIAL'</default>
            <action>if true enable:COMPORT</action>
          </radiobutton>
        </vbox>
        <text space-expand="true" space-fill="true"><label>""</label></text>
        <text width-request="5"><label>""</label></text>
      </hbox>
        <text height-request="5"><label>""</label></text>
          <hbox space-expand="false" space-fill="false">
            <text><label>'$(gettext 'Port')'</label></text>
            <combobox width-request="90">
              <item>ttyS0</item>
              <item>ttyS1</item>
              <item>ttyS2</item>
              <item>ttyS3</item>
              <variable>COMPORT</variable>
              <visible>disabled</visible>
            </combobox>
          </hbox>
    </frame>
  </vbox>
  </hbox>
  <vbox space-expand="true" space-fill="true">
    <frame '$(gettext 'Features')'>
      <text height-request="5"><label>""</label></text>
      <checkbox>
        <label>'$(gettext 'Mouse has a scroll wheel')'</label>
        <variable>CHK_SCROLL_WHEEL</variable>
        <default>'$CHK_SCROLL_WHEEL'</default>
      </checkbox>
      <checkbox>
        <label>'$(gettext 'Middle button emulated on a 2-button mouse')'</label>
        <variable>CHK_MIDDLE_BUTTON</variable>
        <default>'$CHK_MIDDLE_BUTTON'</default>
      </checkbox>
      <checkbox>
        <label>'$(gettext 'Auto-hide mouse cursor when not moving')'</label>
        <variable>CHK_AUTOHIDE_MOUSE</variable>
        <default>'$CHK_AUTOHIDE_MOUSE'</default>
      </checkbox>
      <checkbox>
        <label>'$(gettext 'Left-handed mouse')'</label>
        <variable>CHK_LEFTHANDED_MOUSE</variable>
        <default>'$CHK_LEFTHANDED_MOUSE'</default>
      </checkbox>
      '$TAPCHKBOX'
      <text height-request="5"><label>""</label></text>
    </frame>
  </vbox>
  </hbox>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Cancel")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <action>exit:CANCEL</action>
    </button>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action>exit:OK</action>
    </button>
  </hbox>
</vbox>
</window>'

	. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
	RETPARAMS=`gtkdialog -p MouseWizard`
	eval "$RETPARAMS"

	if [ "$EXIT" != "OK" ] ; then
		exit
	fi

	if [ "$RADIO_SERIAL" = "true" ];then
		MOUSEDEV="$COMPORT" #ttyS0,1,2,3
		ln -snf $COMPORT /dev/mouse #v411
		modprobe sermouse 
	fi
	if [ "$RADIO_USB" = "true" ];then #use this for both usb and ps2 mouse
		MOUSEDEV="input/mice" #usb mouse
		modprobe usbhid 2>/dev/null
		modprobe hid
		modprobe mousedev
		ln -snf input/mice /dev/mouse
	fi
	echo -n "$MOUSEDEV" > /etc/mousedevice

	#v4.11 Touchpad tapping control - sets xorg.conf MaxTapTime to 0 to disable tapping and to default or user-specified xorg.conf value to enable.
	if [ "`echo "$RETPARAMS" | grep "CHECKBOX013"`" != "" ];then
		ZEROTAPTIME="`grep '"MaxTapTime"' /etc/X11/xorg.conf | grep '"0"'`"
		if [ "`echo "$RETPARAMS" | grep "CHECKBOX013" | grep "true"`" != "" ];then #enable tapping
			if [ "$ZEROTAPTIME" != "" ];then #currently disabled, else leave as is
				[ -f /root/.maxtaptime ] && MAXTAPTIME="`grep -m 1 '"MaxTapTime"' /root/.maxtaptime`" || MAXTAPTIME='	Option "MaxTapTime" "220"'
				sed -i -e "s/$ZEROTAPTIME/$MAXTAPTIME/" /etc/X11/xorg.conf
			fi
		else #disable tapping
			if [ "$ZEROTAPTIME" = "" ];then #currently enabled, else leave as is
				MAXTAPTIME="`grep '"MaxTapTime"' /etc/X11/xorg.conf`"
				if [ "$MAXTAPTIME" != "" ];then
					echo "$MAXTAPTIME" > /root/.maxtaptime
					ZEROTAPTIME='	Option "MaxTapTime" "0"'
					SEDSCRIPT="s/$MAXTAPTIME/$ZEROTAPTIME/"
					sed -i -e  "$SEDSCRIPT" /etc/X11/xorg.conf
				fi
			fi
		fi
	fi     

	if [ "$CHK_LEFTHANDED_MOUSE" = "true" ];then #v2.01
		XMODMAP_CMD="pointer = 3 2 1 4 5"
	else
		XMODMAP_CMD="pointer = 1 2 3 4 5"
	fi
	xmodmap -e "${XMODMAP_CMD}"
	grep -v "^pointer" /root/.Xmodmap > /tmp/Xmodmap.tmp
	cp /tmp/Xmodmap.tmp /root/.Xmodmap
	echo "${XMODMAP_CMD}" >> /root/.Xmodmap
	#

	if [ "$CHK_AUTOHIDE_MOUSE" = "true" ];then #v2.01
		[ "$IDLETIME" = "0" ] && IDLETIME="3"
		echo -n "${IDLETIME}," > /etc/mousehide
		[ "`pidof unclutter`" = "" ] && unclutter -idle $IDLETIME &
	else
		echo -n '0,' > /etc/mousehide
		[ ! "`pidof unclutter`" = "" ] && killall unclutter
	fi

	# emulate middle button
	if [ "$CHK_MIDDLE_BUTTON" = "true" ];then
		#\W\+ means one or more whitespace chars...
		sed_ptn1='s/.*#Option\W\+"Emulate3Buttons"/	Option      "Emulate3Buttons"/g'
		sed_ptn2='s/.*#Option\W\+"Emulate3Timeout"/	Option      "Emulate3Timeout"/g'
	else
		sed_ptn1='s/.*Option\W\+"Emulate3Buttons"/	#Option     "Emulate3Buttons"/g'
		sed_ptn2='s/.*Option\W\+"Emulate3Timeout"/	#Option     "Emulate3Timeout"/g'
	fi
	if [ -f /etc/X11/xorg.conf ];then
		sed -i -e "$sed_ptn1" -e "$sed_ptn2" /etc/X11/xorg.conf
	fi

	if [ "$CHK_SCROLL_WHEEL" = "true" ];then
		sed_ptn='s/.*#Option\W\+"ZAxisMapping"\W\+"4 5".*/	Option      "ZAxisMapping" "4 5" #scrollwheel/g'
	else
		sed_ptn='s/.*Option\W\+"ZAxisMapping"\W\+"4 5".*/	#Option     "ZAxisMapping" "4 5" #scrollwheel/g'
	fi
	[ -f /etc/X11/xorg.conf ] && sed -i "$sed_ptn" /etc/X11/xorg.conf
	exit
fi

#==============================================================
#                      MAIN  INPUT WIZARD
#==============================================================

PUPX_MOUSE='pupx mouse'
PUPX_KEYBOARD='pupx keyboard'
MOUSE_CONFIG="$0 mouse"

export MouseKeyboardWizard='
<window title="'$(gettext 'Input Wizard')'" icon-name="gtk-preferences">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed mouse_keyboard.svg 60 " " "$(gettext "Configure your input devices")"`"'
  <hbox space-expand="true" space-fill="true">
    <frame '$(gettext 'Keyboard')'>
      <hbox>
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon country_keyboard.svg big`"'
          <action>'chooselayout'</action>
        </button>
        <text space-expand="false" space-fill="false"><label>'$(gettext 'Layout for your country')'</label></text>
        <text space-expand="true" space-fill="true"><label>""</label></text>
      </hbox>
      <hbox>
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon keyboard_shortcut.svg big`"'
          <action>'xkbconfigmanager'</action>
        </button>
        <text space-expand="false" space-fill="false"><label>'$(gettext 'Advanced configuration')'</label></text>
        <text space-expand="true" space-fill="true"><label>""</label></text>
      </hbox>
      <hbox>
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon keyboard.svg big`"'
          <action>'${PUPX_KEYBOARD}'</action>
        </button>
        <text space-expand="false" space-fill="false"><label>'$(gettext 'X Keyboard properties')'</label></text>
        <text space-expand="true" space-fill="true"><label>""</label></text>
      </hbox>
    </frame>

    <frame '$(gettext 'Pointer')'>
      <hbox>
        <text><label>'$(gettext 'Type of mouse')'</label></text>
        <button>
          '"`/usr/lib/gtkdialog/xml_button-icon mouse.svg big`"'
          <action>'${MOUSE_CONFIG}'</action>
        </button>
      </hbox>
      <hbox>
        <text><label>'$(gettext 'Adjust mouse sensitivity')'</label></text>
        <button>
          '"`/usr/lib/gtkdialog/xml_button-icon mouse_sensitivity.svg big`"'
          <action>'${PUPX_MOUSE}'</action>
        </button>
      </hbox>
      <hbox>
        <text><label>'$(gettext 'Touchpad')'</label></text>
        <button>
          '"`/usr/lib/gtkdialog/xml_button-icon touchpad.svg big`"'
          <action>'flsynclient'</action>
        </button>
      </hbox>
    </frame>
  </hbox>

  <hbox space-expand="false" space-fill="false">
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
      <label>'$(gettext 'Quit')'</label>
    </button>
  </hbox>
 </vbox>
</window>'

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
gtkdialog -p MouseKeyboardWizard

### END ###
