Skip to content

Instantly share code, notes, and snippets.

@lanrat
Last active August 29, 2015 14:03
Show Gist options
  • Save lanrat/62300cf327f29d72bfb1 to your computer and use it in GitHub Desktop.
Save lanrat/62300cf327f29d72bfb1 to your computer and use it in GitHub Desktop.
#!/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