#!/bin/bash

# normal
#xinput set-prop `xinput list | grep -i touch | grep -v -e 'Mouse' | grep -Po 'id=\K[0-9]+'` "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
# rotate left
xinput set-prop `xinput list | grep -i touch | grep -v -e 'Mouse' | grep -Po 'id=\K[0-9]+'` "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1
# rotate right
#xinput set-prop `xinput list | grep -i touch | grep -v -e 'Mouse' | grep -Po 'id=\K[0-9]+'` "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1
# reflection (invert)
#xinput set-prop `xinput list | grep -i touch | grep -v -e 'Mouse' | grep -Po 'id=\K[0-9]+'` "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1

#exit

#sleep 3

getPortName() {
  len=$(expr index "$1" ' ')
  expr substr "$1" '1' "$(($len - 1))"
}

getID() {
  pos=$(expr index "$1" 'id')    
  expr substr "$1" "$(($pos + 3))"  "2"
}


displayVerticalName1='TouchPlanet XTM'
#displayVerticalName2='ILITEK Multi-Touch-V5000'
displayVerticalPortName=$(getPortName "$(xrandr | grep '1080x1920+')")

displayHorizontalName='wch.cn USB2IIC_CTP_CONTROL'
displayHorizontalPortName=$(getPortName "$(xrandr | grep '1080x1080+')")
displayHorizontalID=$(getID "$(xinput --list | grep "$displayHorizontalName")" )

xinput map-to-output "$displayVerticalName1" "$displayVerticalPortName"
#xinput map-to-output "$displayVerticalName2" "$displayVerticalPortName"
xinput map-to-output "$displayHorizontalID" "$displayHorizontalPortName"
#xinput map-to-output "$displayHorizontalName" "$displayHorizontalPortName"

xinput map-to-output  "$displayVerticalPortName"