#!/usr/bin/env bash

# changing setting in anything other than libinput might be an issue/ineffective/break libinput config 

sudo apt install xinput

TRACKPAD=51-synapics-trackpad.conf
TRACKPOINT=52-alps-trackpoint.conf
XORG_CONFDIR=/etc/X11/xorg.conf.d

mkdir -p ${XORG_CONFDIR}

# xinput list  | grep DualPoint

# ⎜   ↳ AlpsPS/2 ALPS DualPoint TouchPad        	id=11	[slave  pointer  (2)]
# ⎜   ↳ AlpsPS/2 ALPS DualPoint Stick           	id=12	[slave  pointer  (2)]

# xinput list-props 12

# Device 'AlpsPS/2 ALPS DualPoint Stick':
#	Device Enabled (148):	1
#	Coordinate Transformation Matrix (150):0.900000, 0.000000, 0.000000, 0.000000, 0.900000, 0.000000, 0.000000, 0.000000, 1.000000
#	libinput Natural Scrolling Enabled (316):	0
#	libinput Natural Scrolling Enabled Default (317):	0
#	libinput Scroll Methods Available (318):	0, 0, 1
#	libinput Scroll Method Enabled (319):	0, 0, 1
#	libinput Scroll Method Enabled Default (320):	0, 0, 1
#	libinput Button Scrolling Button (321):2
#	libinput Button Scrolling Button Default (322):2
#	libinput Button Scrolling Button Lock Enabled (323):	0
#	libinput Button Scrolling Button Lock Enabled Default (324):	0
#	libinput Middle Emulation Enabled (325):	0
#	libinput Middle Emulation Enabled Default (326):	0
#	libinput Accel Speed (327):	0.000000
#	libinput Accel Speed Default (328):	0.000000
#	libinput Accel Profiles Available (329):	1, 1
#	libinput Accel Profile Enabled (330):	1, 0
#	libinput Accel Profile Enabled Default (331):	1, 0
#	libinput Left Handed Enabled (332):	0
#	libinput Left Handed Enabled Default (333):	0
#	libinput Send Events Modes Available (268):	1, 0
#	libinput Send Events Mode Enabled (269):	0, 0
#	libinput Send Events Mode Enabled Default (270):	0, 0
#	Device Node (271):	"/dev/input/event2"
#	Device Product ID (272):	2, 8
#	libinput Drag Lock Buttons (334):	<no items>
#	libinput Horizontal Scroll Enabled (335):	1

# xinput list-props 11

# Device 'AlpsPS/2 ALPS DualPoint TouchPad':
# 	Device Enabled (148):	1
#	Coordinate Transformation Matrix (150):1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000
# 	Device Accel Profile (275):	1
# 	Device Accel Constant Deceleration (276):	2.500000
# 	Device Accel Adaptive Deceleration (277):	1.000000
# 	Device Accel Velocity Scaling (278):	12.500000
# 	Synaptics Edges (279):	153, 870, 115, 652
# 	Synaptics Finger (280):12, 15, 0
# 	Synaptics Tap Time (281):	180
# 	Synaptics Tap Move (282):	56
# 	Synaptics Tap Durations (283):	180, 180, 100
# 	Synaptics ClickPad (284):	0
# 	Synaptics Middle Button Timeout (285):	75
# 	Synaptics Two-Finger Pressure (286):	141
# 	Synaptics Two-Finger Width (287):	7
# 	Synaptics Scrolling Distance (288):	25, 25
# 	Synaptics Edge Scrolling (289):1, 0, 0
# 	Synaptics Two-Finger Scrolling (290):	0, 0
# 	Synaptics Move Speed (291):	1.000000, 1.750000, 0.156495, 0.000000
# 	Synaptics Off (292):	0
# 	Synaptics Locked Drags (293):	0
# 	Synaptics Locked Drags Timeout (294):	5000
# 	Synaptics Tap Action (295):	0, 0, 0, 0, 0, 0, 0
# 	Synaptics Click Action (296):	1, 1, 1
# 	Synaptics Circular Scrolling (297):	0
# 	Synaptics Circular Scrolling Distance (298):	0.100000
# 	Synaptics Circular Scrolling Trigger (299):	0
# 	Synaptics Circular Pad (300):	0
# 	Synaptics Palm Detection (301):0
# 	Synaptics Palm Dimensions (302):	10, 100
# 	Synaptics Coasting Speed (303):20.000000, 50.000000
# 	Synaptics Pressure Motion (304):	15, 80
# 	Synaptics Pressure Motion Factor (305):1.000000, 1.000000
# 	Synaptics Grab Event Device (306):	0
# 	Synaptics Gestures (307):	1
# 	Synaptics Capabilities (308):	1, 1, 1, 0, 0, 1, 0
# 	Synaptics Pad Resolution (309):1, 1
# 	Synaptics Area (310):	0, 0, 0, 0
# 	Synaptics Noise Cancellation (311):	6, 6
# 	Device Product ID (272):	2, 8
# 	Device Node (271):	"/dev/input/event3"

# https://amionrails.wordpress.com/2018/08/29/adjusting-speed-of-pointer-stick-track-ball-on-ubuntu/
# https://wiki.gentoo.org/wiki/Libinput#Pointer_acceleration

#cat <<EOF | sudo tee ${XORG_CONFDIR}/40-libinput.conf >/dev/null
#Section "InputClass"
#        Identifier "libinput pointer catchall"
#        MatchIsPointer "on"
#        Driver "libinput"
#        Option "TransformationMatrix" "0.90 0 0 0 0.90 0 0 0 1"
#EndSection
#EOF

cat <<EOF | sudo tee ${XORG_CONFDIR}/${TRACKPOINT} >/dev/null
Section "InputClass"
        Identifier "TrackPoint Clone"
        MatchProduct "AlpsPS/2 ALPS DualPoint Stick"
        Driver "libinput"
        Option "TransformationMatrix" "0.20 0 0 0 0.20 0 0 0 1"
EndSection
EOF

# set the trackpoint matrix above in running session
xinput set-prop 12 150 0.20 0 0 0 0.20 0 0 0 1

## trackpoint
#cat > ${XORG_CONFDIR}/${TRACKPOINT} <<EOF
#Section "InputClass"
#        Identifier      "TrackPoint Clone Acceleration and Middle Click Emulation"
#        MatchProduct    "AlpsPS/2 ALPS DualPoint Stick"
#        Driver          "libinput"
#        MatchDevicePath "/dev/input/event*"
##       Option          "EmulateWheel"          "true"
##       Option          "EmulateWheelButton"    "2"
#        Option          "Emulate3Buttons"       "true"
#        Option          "AccelerationProfile"   "7"
#        Option          "AdaptiveDeceleration"  "1.3"
#        Option          "ConstantDeceleration"  "1"
#        Option          "ExpectedRate"          "10"
##       Option          "XAxisMapping"          "6 7"
##       Option          "YAxisMapping"          "4 5"
#EndSection
#EOF

## trackpad
#cat <<EOF | sudo tee ${XORG_CONFDIR}/${TRACKPAD} >/dev/null
#Section "InputDevice"
#    Identifier "SynapticsMouse1"
#    MatchProduct "AlpsPS/2 ALPS DualPoint TouchPad"
#    Driver "libinput"
#    Option "EdgeMotionMinSpeed" "200"
#    Option "MaxTapTime" "0"  # do not use taping, i.e. touching/tapping will not be taken as a mouse click
#    Option "MaxSpeed" "1.50"
#    Option "MinSpeed" "0.7"
#    Option "BottomEdge" "650"
#    Option "EdgeMotionMaxSpeed" "200"
#    Option "CircScrollTrigger" "2"
#    Option "UpDownScrolling" "0"
#    Option "SHMConfig" "on"
#    Option "LeftEdge" "120"
#    Option "FingerLow" "14"
#    Option "HorizScrollDelta" "20"
#    Option "MaxTapMove" "110"
#    Option "FingerHigh" "15"
#    Option "VertScrollDelta" "20"
#    Option "CircularScrolling" "1"
#    Option "RightEdge" "830"
#    Option "AccelFactor" "0.3"
#    Option "TopEdge" "120"
#EndSection
#EOF

# cat /sys/devices/platform/i8042/serio1/input/input6/name
## AlpsPS/2 ALPS DualPoint TouchPad

#Device Accel Profile (264): 2
#Device Accel Constant Deceleration (265):   6.000000
#Device Accel Adaptive Deceleration (266):   1.000000
#Device Accel Velocity Scaling (267):    10.000000

# cat <<EOF | sudo tee ${XORG_CONFDIR}/${TRACKPOINT} >/dev/null
# Section "InputClass"
#    Identifier   	"trackpoint catchall"
#    MatchIsPointer   	"true"
#    MatchProduct   	"AlpsPS/2 ALPS DualPoint Stick"
#    MatchDevicePath   	"/dev/input/event*"
#    Option   		"Emulate3Buttons"   "true"
#    Option   		"EmulateWheel"   "true"
#    Option   "EmulateWheelButton"   "2"
#    Option   "XAxisMapping"   "6 7"
#    Option   "YAxisMapping"   "4 5"
#    Option   "VertResolution" "100"
#    Option   "HorizResolution" "100"
#    Option   "AccelerationScheme" "none"
#    Option   "AccelerationProfile" "1"
#    Option   "AccelerationScheme" "predictable"
#    Option   "AccelerationThreshold" "5"
#    Option   "AccelerationNumerator" "2"
#    Option   "AccelerationDenominator" "1"
#    Option   "ConstantDeceleration" "2.4"
#    Option   "AdaptiveDeceleration" "2.1"
# EndSection
# EOF
