#!/bin/bash


# prima del start X viene chiamato init_input_filter, dissabilita touch da filtrare
# durante start3g e hotplug viene chiamato set_hid_filter per impostare filtri e touchmon
# touchquery risponde 0 quando non si deve fare nulla
#                     1 quando rileva un touch tradizionale, ritorna parametri per inputattach o generic
#                     2 quando rileva input event, ritorna elenco di device attivi


CALIBRATION_CONF=/usr/share/X11/xorg.conf.d/99-calibration.conf

INPUTATTACH='systools/inputattach'
INPUTFILTER='systools/input_filter'


# nel caso di upgrade patch calibration file to match new driver name
function patch_old_calibration
{
  if ! [ -f $CALIBRATION_CONF ] || ! cat $CALIBRATION_CONF | grep -q "filtered_touchscreen_input"
  then
    return
  fi
  cp $CALIBRATION_CONF /tmp/calib
  cat /tmp/calib | sed 's/filtered_touchscreen_input/filtered/g' > $CALIBRATION_CONF
  rm /tmp/calib
}

function search_touch_devices
{

  TOUCH_OVERRIDE=`cat touch_device.override | grep '/dev/input' | grep -v '#'`
  if [ -n "$TOUCH_OVERRIDE" ]
  then
    echo $TOUCH_OVERRIDE
  else
    IDS=`xinput_calibrator --list | sed 's/.*id=//g'`
    for id in $IDS
    do
      xinput list-props $id | tr '\t"' '  ' | grep "Device Node" | cut -d: -f2 | tr -d ' '
    done
  fi
}

function trash_search_touch_devices
{
  IDS=`xinput list | grep -i touch | grep pointer | tail -1 | tr '\t' ' ' | sed 's/.* id=\([^ ]*\).*/\1/'`
  for id in $IDS
  do
    xinput list-props $id | tr '\t"' '  ' | grep "Device Node" | cut -d: -f2 | tr -d ' '
  done
}

# converte /dev/input/event* al ID del xinput
function find_device_id 
{
  IDS=`xinput list | sed 's/.*id=\([0-9]*\).*/\1/'`
  for id in $IDS
  do
    if xinput list-props $id | grep "Device Node" | grep -q "\"$1\""
    then
      echo $id
      return  
    fi  
  done
}

function install_touchmon
{
  killall touchmon
  TOUCH_BEEP_ENABLED=`systools/get_eurodb_config_val TouchScreenBeep bool`
  if [ "$TOUCH_BEEP_ENABLED" == "1" ]
  then
#  TOUCH_EVDEV_LIST="  "`grep -i touch /var/log/Xorg.0.log | grep /dev/input | grep evdev | cut -d'"' -f2 | sort | uniq`
    TOUCH_EVDEV_LIST=`search_touch_devices`
echo "touch evdev list "$TOUCH_EVDEV_LIST
    for TOUCH_EVDEV in $TOUCH_EVDEV_LIST
    do 
      if [ -n "$TOUCH_EVDEV" ]
      then
         ./touchmon $TOUCH_EVDEV &
      fi
    done
  fi
}

function trash_install_inputattach
{
  if echo -- "$1" | grep generic 
  then
    $INPUTFILTER $1 &
  else
    $INPUTATTACH $1 --daemon
  fi
}

# installa eventuali driver tradizionali - seriale, elo o hidraw
function install_inputattach
{
  TOUCH_DETECT=`cat e3g_inputattach 2>/dev/null`
  [ "$TOUCH_DETECT" ] || return
  if echo -- "$TOUCH_DETECT" | grep generic 
  then
    $INPUTFILTER $TOUCH_DETECT &
  else
    INPUTATTACH_DEV=`echo $1 | tr ' ' '\n' | tail -1`
    if [ -n "$INPUTATTACH_DEV" ]
    then 
      stty -F $INPUTATTACH_DEV min 1 time 0 -icanon
    fi
    {
      while true; do
         $INPUTATTACH $TOUCH_DETECT
      done
    } &
  fi
}

function remove_generic_inputattach
{
#  killall input_filter --generic
  ps auxw | egrep 'input_filter.*--generic' | grep -v grep | awk '{print $2}' | xargs kill 2>/dev/null
}


# installa eventuali input_filter per hid device dal elenco input_filter.conf
function install_input_filter
{
  systools/init_input_filter start_filter
}


function trash_find_active_touch_device
{
  for i in `search_touch_devices`
  do
    echo -- "$1" | grep "$i"
  done   
}

function try_to_calibrate 
{
  if [ ! -f /tmp/touchscreen_calibrated ] 
  then
    ./calibrate_touchscreen && exit
  fi
  rm /tmp/touchscreen_calibrated
}

function handle_touchscreen
{
  remove_generic_inputattach
  install_input_filter
  try_to_calibrate
  install_inputattach
  install_touchmon
}
