#!/bin/bash
#Tablet mode for Acer R11 Chromebook running Linux
#Copyright Kris Occhipinti
#http://filmsbykris.com
#August 4th 2019
# License GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt

#xinput list 
state_file="$HOME/.tablet_state"
device="Elan Touchscreen"

if [ ! -f "$state_file" ];then
  echo "0" > "$state_file"
fi

state="$(cat $state_file)"
#Portrait (left)

if [ "$state" = "0" ];then
  echo "1" > "$state_file"

  #rotate screen
  xrandr -o left

  #rotate mouse  
  xinput set-prop "$device" --type=float "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1                                     
  xinput set-prop "$device" --type=float "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1                         

  #disable keyboard and touchpad
  xinput --disable "$(xinput|grep "AT Translated Set 2 keyboard"|cut -d\= -f2|cut -f1)"
  xinput --disable "$(xinput|grep "Elan Touchpad"|cut -d\= -f2|cut -f1)"
else                                                                                                                                               
  #landscape (normal)                                                                                                                              
  echo "0" > "$state_file"
  xrandr -o normal                                                                                                                                 
  xinput set-prop "$device" --type=float "Coordinate Transformation Matrix" 0 0 0 0 0 0 0 0 0                                      
  xinput set-prop "$device" --type=float "Coordinate Transformation Matrix" 0 0 0 0 0 0 0 0 0    

  #enable keyboard and touchpad
  xinput --enable "$(xinput|grep "AT Translated Set 2 keyboard"|cut -d\= -f2|cut -f1)"
  xinput --enable "$(xinput|grep "Elan Touchpad"|cut -d\= -f2|cut -f1)"
fi