2011年11月21日 星期一

Enable/Disable touchpad

寫一個 shell script 來判斷 touchpad 狀態並切換 enable/disable
寫法可能不是很好,因為 shell script 不熟... :P
之後再研究怎麼判斷有沒有接滑鼠,實現有接滑鼠就自動 disable touchpad 的功能
#!/bin/bash

KEYWORD="PS/2 Logitech Wheel Mouse"
ID=""

FOUND=`xinput --list | grep "${KEYWORD}"`
for i in ${FOUND}; do
    MATCH=`echo ${i} | cut -d "=" -f 2 -`
    if [ ${i} != ${MATCH} ]; then
        ID=${MATCH}
    fi
done

if [ ${ID} == "" ]; then
    echo "${KEYWORD} not found!"
    exit
fi

FOUND=`xinput --list-props ${ID} | grep "Device Enabled"`
DEVICE_ENABLE=""
for i in ${FOUND}; do
    case ${i} in
    "("*)
        DEVICE_ENABLE=`echo ${i} | cut -d "(" -f 2 -`
        DEVICE_ENABLE=`echo ${DEVICE_ENABLE} | cut -d ")" -f 1 -`
        ;;
    esac
done

if [ "${DEVICE_ENABLE}" == "" ]; then
    echo "Device not found!"
    exit
fi

ENABLED=`echo ${FOUND} | cut -d ":" -f 2 -`
if [ "${ENABLED}" == "${FOUND}" ]; then
    echo "Error!"
    exit
fi

ENABLED=`expr ${ENABLED} + 0`

echo -n "Found \"${KEYWORD}\" (ID= ${ID}, DEVICE= ${DEVICE_ENABLE}) "
if [ "${ENABLED}" == "0" ]; then
    echo "disabled."
else
    echo "enabled."
fi

case $1 in
    0)
        xinput set-prop ${ID} ${DEVICE_ENABLE} 0
        echo "Touch pad disabled!"
        ;;
    1)
        xinput set-prop ${ID} ${DEVICE_ENABLE} 1
        echo "Enable touch pad..."
        ;;
    *)
        if [ "${ENABLED}" == "0" ]; then
            echo "Switch to enable..."
            xinput set-prop ${ID} ${DEVICE_ENABLE} 1
        else
            echo "Switch to disable..."
            xinput set-prop ${ID} ${DEVICE_ENABLE} 0
        fi
        ;;
esac

沒有留言:

張貼留言