Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

My iPhone is connected via USB to my Ubuntu desktop via a dock. How do I configure it so that when I dock my phone the screen is unlocked and when removing it it locks it?

share|improve this question
    
Great trick!. But you can use comments to put you comments ;) –  Javier Rivera Dec 17 '10 at 11:32
    
Indeed, it would probably make it more readable. So as a comment: I got tired of having to type my password to unlock my screen after coming back to my desk and remembering to lock it before I leave. I found a solution and thought I'd post it here in a question/answer form in case this is something useful to other people. –  Roman Dec 17 '10 at 11:52

2 Answers 2

up vote 4 down vote accepted

Found an excellent script here courtesy of Evan Boldt on how to do this. Thanks Evan!

First find out the id of your device by using lsusb

The create a script under your home dir (let's use /home/me/iPhoneLock.sh for this example) that looks sort of like this:

#!/bin/bash

#Replace with the ID of your USB device
id="ID ffff:1234 Apple, Inc. iPhone 3G"
serial="12345"

#runs every 2 seconds
for ((i=0; i<=30; i++))
do
if [ -z "`lsusb -v 2> /dev/null | grep "$serial"`" ]
then

    echo "Device is NOT plugged in"

    if [ -n "`DISPLAY=:0 gnome-screensaver-command --query | grep "is active"`" ]
    then
    if [ -e /tmp/autoUnlock.lock ]
    then
    #stop locking the screen
    rm /tmp/autoUnlock.lock

fi

elif [ -e /tmp/autoUnlock.lock ]
then

    DISPLAY=:0 notify-send -t 5000 --icon=dialog-info "iPhone Disconnected" "Locking     screen"
    #lock the desktop
    DISPLAY=:0 gnome-screensaver-command --lock

    rm /tmp/autoUnlock.lock

fi
else

    echo "iPhone IS plugged in"
    if [ ! -e /tmp/autoUnlock.lock ]
    then
    DISPLAY=:0 gnome-screensaver-command --deactivate
    DISPLAY=:0 notify-send -t 5000 --icon=dialog-info "iPhone Connected" "Welcome     Back!"
    touch /tmp/autoUnlock.lock

    fi

fi
sleep 2
done

Next, edit your crontab:

crontab -e

Finally configure it so that it runs every minute:

* * * * * bash /home/username/bin/autoUnlock & >/dev/null 2>&1

Note of warning though: Of course this means that anyone with your phone will be able to unlock your screen. A nice improvement would be to only unlock your screen if your phone was unlocked.

This of course is applicable to any usb device.

The script is licensed under the CC-GNU GPL version 2.0 or later.

share|improve this answer

even simpler script

use lsusb to get the device id of the usb device you want to use as your "key" and replace it in this script (called checkKey.sh)

#!/bin/sh
key="0a12:0001" #ID of the USB device to use as a "key"
if [ `fuser $0|wc -w` -gt "1" ];then  exit; fi  # exit if already running

while [ 1 -gt 0 ];  do
    device=$(lsusb | grep $key) # is "key" connected?
    ss_state=$(gnome-screensaver-command -q | grep inactive) #is screen locked?
    if [ -z "$device" ]; then
        gnome-screensaver-command -l; #no key, lock the screen
    else
        if [ -z "$ss_state" ]; then
                        #key present & screen locked so unlock
            gnome-screensaver-command -d; 
        else
                        #key present, not locked, just poke it
            gnome-screensaver-command -p;
        fi      
    fi
    sleep 10; #sleep for a few seconds before looking again
done

then just cron this to run every few minutes... this way it will start up again if it stops for some reason.... the script should run continuously, but will exit if cron tries to run a second copy. To have cron affect a GUI app, like the screen saver, you have to tell it which display to use, so put this in your crontab (obviously set the path properly)

* * * * * export DISPLAY=:0 && /home/someuser/checkKey.sh
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.