Last active
August 29, 2015 14:03
-
-
Save lanrat/62300cf327f29d72bfb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
USB_SERIAL="TODO" | |
START_CMD="xscreensaver -no-splash" | |
LOCK_CMD="xscreensaver-command --lock" | |
UNLOCK_CMD="killall xscreensaver" | |
LAST_SEEN="" | |
#return 1 if device is found, 0 otherwise | |
function checkUSB() | |
{ | |
USB_SERIAL_ID_LIST=$(lsusb -v 2>/dev/null | grep 'iSerial' | awk '{print $3}' | grep '\S') | |
for id in ${USB_SERIAL_ID_LIST}; | |
do | |
if [ "$id" = "$USB_SERIAL" ]; | |
then | |
#device detected | |
echo "1" | |
return | |
fi | |
done | |
#not detected | |
echo "0" | |
return | |
} | |
# Notify us if the state changes | |
function stateChange() | |
{ | |
state=$(checkUSB) | |
#echo "S: $state" | |
if [[ "$LAST_SEEN" = "1" && "$state" = "0" ]]; | |
then | |
LAST_SEEN=$state | |
#device unpluged, lock computer | |
echo "Lock" | |
$LOCK_CMD > /dev/null | |
elif [[ "$LAST_SEEN" = "0" && "$state" = "1" ]]; | |
then | |
LAST_SEEN=$state | |
#device plugged in, unlock computer | |
echo "Unlock" | |
$UNLOCK_CMD 2>/dev/null | |
$START_CMD & | |
fi | |
} | |
# Main loop | |
while true | |
do | |
stateChange | |
sleep 1 | |
done | |
# Cleanup | |
kill $! | |
wait $! 2>/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment