Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[HOWTO] Customizing Thinkpad Fn+F3 to lock with xscreensaver
View unanswered posts
View posts from last 24 hours

Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message

Joined: 27 Oct 2005
Posts: 11
Location: UK

PostPosted: Tue Jan 02, 2007 1:06 am    Post subject: [HOWTO] Customizing Thinkpad Fn+F3 to lock with xscreensaver Reply with quote

After installing Gentoo on a partition of a shiny new Thinkpad T42 I figured some customization was in order. Ordinarily, the Fn+F3 key combination blanks the screen on a Thinkpad. I find this a bit pointless (if I want to save power, I'll suspend or hibernate it, not just turn off the screen - besides most of the time I'm working from AC). However, it would be nice to have a keyboard combination to lock the screen in X (usually Gnome or KDE for me) with xscreensaver.

Unfortunately, this turned out to be a nightmarishly complicated task: part of the reason for posting this is to save frustration on the part of anyone else that wants to do this, and partly to find out if there's a better (i.e. easier) way. I suspect most of the following could be easily customized to non-Thinkpad laptops or possibly certain desktop machines (I've marked those sections which are Thinkpad specific below). So, without further ado:

1. Enable ibm_acpi in the kernel (Thinkpad specific)

If you've got a Thinkpad, you've probably done this already, but just in case...

Make sure Power management options -> ACPI Support -> IBM Thinkpad Laptop Extras is enabled or set to module in your kernel configuration (you'll need this to capture the Fn+F3 combination via acpid). If you've set it to build as a module, you'll need to add ibm_acpi to /etc/modules.autoload.d/kernel-2.6 and remember to run modules-update:

# echo "ibm_acpi" >> /etc/modules.autoload.d/kernel-2.6
# modules-update

If you've rebuilt your kernel to enable this, you'll need to reboot before this will take effect.

2. Install acpid

You've probably already got acpid installed (it's useful for lots of things, especially on a laptop), but again just in case... Install acpid, start it, and add it to the default runlevel:

# emerge acpid
# /etc/init.d/acpid start
# rc-update add acpid default

3. Enable hotkeys in ibm_acpi (Thinkpad specific)

Normally, hotkey combinations like Fn+F3 don't generate ACPI events on a Thinkpad. To enable such combinations to generate ACPI events (which we can watch for and react to with acpid) we need to set some values in /proc/acpi/ibm/hotkey, like so:

# echo "enable" > /proc/acpi/ibm/hotkey
# echo "0xffff" > /proc/acpi/ibm/hotkey
# cat /proc/acpi/ibm/hotkey
status:         enabled
mask:           0xffff
commands:       enable, disable, reset, <mask>

Ideally, we want the above to be set whenever we boot the machine. The local.start configuration is as good a place as any to do this:

# cat >> /etc/conf.d/local.start
# Allow ibm_acpi to capture hotkeys
echo "enable" > /proc/acpi/ibm/hotkey
echo "0xffff" > /proc/acpi/ibm/hotkey
^D (hit Ctrl+D to quit cat)

Note that the above tells ibm_acpi to generate ACPI events for ALL hotkeys (e.g. Fn+F1 thru Fn+F12 plus some others). In certain cases this prevents the default action of that combination from operating. For example, after doing the above on my T42, Fn+F5 no longer toggles the integrated Bluetooth on and off (althfor xscreensaver first ough it wasn't difficult to restore that functionality and customize it a bit with another acpid script).

If you want to limit the effect of the above script, read /usr/src/linux/Documentation/ibm-acpi.txt and adjust the 0xffff mask on the final line of the script accordingly.

4. Install xscreensaver

Again, you've probably already got this, but for the sake of completeness... If you've installed gnome-screensaver, get rid of it first:

# emerge --unmerge gnome-screensaver

If you're using gdm, before installing xscreensaver you may want to add new-login to the USE flags (to allow someone to start a new X session when the machine is locked):

# echo "x11-misc/xscreensaver new-login" >> /etc/portage/package.use

Install xscreensaver:

# emerge xscreensaver

for xscreensaver first
As your regular user (NOT root), start the xscreensaver daemon:

$ xscreensaver &

You should find that the xscreensaver daemon automatically starts whenever you login to X (at least, it does for me in Gnome - not sure about other desktops, you may need to add xscreensaver & to your ~/.xinitrc script to start it automatically if not).

You'll also want to configure xscreensaver to select what screensavers you want to run, and various other options (like whether some screensavers can get screen-grabs etc). Under Gnome you should find the configuration under System -> Preferences -> Screensaver (after installing xscreensaver, you may need to re-login to Gnome before this entry appears), or you can just run the following from the command line (again, as your regular user, not root):

$ xscreensaver-demo

4. Configure acpid to respond to Fn+F3 (partially Thinkpad specific)

Finally, we need to add a couple of scripts to acpid's configuration to enable it to respond to Fn+F3 keypresses. Firstly, create a file called /etc/acpi/events/hotkey_screenlock and stick the following in it:

event=ibm/hotkey HKEY 00000080 00001003
action=/etc/acpi/actions/ %e

The first line specifies the ACPI event to respond to, the second which script to run in response. In this case "ibm/hotkey HKEY 00000080 00001003" means Fn+F3 (and is obviously Thinkpad specific). If you want to replace this with another ACPI event (e.g. another hotkey combination) you'll need to experiment with acpid a bit: use tail -f /var/log/messages to watch your log file (or whatever log file acpid dumps stuff into given your particular syslog configuration) and try hitting hotkey combinations to see what gets logged. You should be able to figure out what to replace the event entry with in the script above from the logged entries. For Thinkpads there's a handy reference to the available ACPI events that the hotkey combinations generate at

Anyway, now we need to create the script that'll be run when acpid notices an Fn+F3 event. This was the seriously tricky bit that took me hours to figure out and debug. I'm almost disappointed the resulting script is so short... :roll:

Create a file called /etc/acpi/actions/ and stick the following in it:


# Simple die() function - outputs the argument to syslog and quits with a
# non-zero exit code
die() {
        logger -- "$0: $*"
        exit 1

# Returns (on stdout) the home directory of the specified user. I'm sure
# there's a better way of doing this...
homedir() {
        awk -F: "\$1==\"$1\" {print \$6;}" /etc/passwd

# Because we're running from acpid (which is running as root) we don't have a
# "complete" environment (we're missing DISPLAY, and more importantly we're
# missing HOME ... awwww).
HOME=$(homedir root)

# Check if the xscreensaver daemon is running
XSS_PID=$(pgrep "^xscreensaver$")
[[ "x$XSS_PID" != "x" ]] || die "xscreensaver daemon is not running."

# Figure out the user who's running xscreensaver
XSS_USER=$(stat -c '%U' /proc/$XSS_PID)
[[ "x$XSS_USER" != "x" ]] || die "Unable to determine user of the xscreensaver daemon."

# In order to get access to the DISPLAY (which XSS_CMD needs) we need to
# duplicate the Xauthority of the user running xscreensaver
xauth -f $(homedir $XSS_USER)/.Xauthority nextract - $DISPLAY | xauth nmerge - || die "Unable to grab Xauthority from $XSS_USER."

# Check whether the screensaver is already locked
xscreensaver-command -time | grep -q "locked" && die "xscreensaver is already locked!"

xscreensaver-command -lock || die "Failed to lock xscreensaver."

Note that the above code relies upon the following external utilities which I assume most people have installed:

  • awk (from sys-apps/gawk)
  • logger (from sys-apps/linux)
  • stat (from sys-apps/coreutils)
  • grep (from sys-apps/grep)
  • pgrep (from sys-process/procps)
  • xauth (from x11-apps/xauth)

Remember to make the file executable, and reload the acpid configuration:

# chmod +x /etc/acpi/actions/
# /etc/init.d/acpid reload

That's it! You should now find that hitting Fn+F3 will lock your X session... If not, hopefully the script has enough debugging logic to leave you some clue as to what went wrong in your syslog.

2006-01-02 02:11 Edited: some grammatical corrections, added ThinkWiki link to last section.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum