Sunday, February 10, 2008

Remote control on Linux

I'm giddy. I just got a piece of hardware working with Linux in under a day, and since this is such a rare occurrence (possibly a first for me), I thought I'd write about it. So if you're not into technical mumbo-jumbo, now's your chance to flee.

The motivation:
We listen to music and watch videos and DVDs on our computer, on Linux, using a couple little PyQt media apps I wrote, basically to wrap around mplayer. Trouble is, especially with a baby in the room, yelling over top of key dialogue, you have to have someone sitting at the computer to be able to pause and go back to find out what Bob said to Doug. (If you're wondering, it was "Take off, eh?".)

The proposed solution:
Remote control! Sit on the couch in comfort, pause, rewind, crank the volume up to 11, just like regular people with their fancy television machines. What could be better? So I went to the local computer store, and (high on my inflated sense of Linux's superiority) I cockily picked out a remote control that claimed to only work with Windows (boo) Media Center, and only on machines that came with this remote. ("For distribution only with a new computer" - hogwash!)

The problem:
Although Linux is, in fact, superior, it nevertheless takes an average of two weekends to get any new piece of hardware working with it. Whereas Windows lets you plug the thing in and start using it right away (most likely to cause Windows to crash), Linux often requires you to (a) know what you're doing, and (b) have the extreme patience required to do it.

The details:
- Microsoft/Philips Media Center Remote (Alt), Version 2, Model 1039 (the one in the middle) with a USB infrared receiver
- Linux (Fedora Core 7, kernel 2.6.23.14-64.fc7)
- mplayer 1.0rc2

The procedure:
Bear in mind both that I've had some small amount of previous experience with Linux infrared remote control (lirc) before (thanks, Claude!), and also that I went down a few wrong roads (this is just the condensed list)...

1. Place the receiver in an accessible spot, and plug it into a spare USB slot. Check dmesg to see that it was recognized:
usb 2-1: new full speed USB device using ohci_hcd and address 4
usb 2-1: configuration #1 chosen from 1 choice


2. Use lsusb to find out (doesn't say on it anywhere) that it actually is a Philips remote:
Bus 002 Device 004: ID 0471:0815 Philips

3. Search Google for "mplayer 0471:0815 Philips" to get this useful page.

4. Download the latest version of lirc, using the instructions on that page:
cd /usr/src
wget http://lirc.sourceforge.net/software/snapshots/lirc-0.8.2pre2.tar.bz2
bunzip2 lirc-0.8.2pre2.tar.bz2
tar -xvf lirc-0.8.2pre2.tar
cd lirc-0.8.2pre2


5. Run ./setup.sh, choose Driver Configuration "8 USB devices" / "o Windows Media Center Remotes (new version, Philips et al.)", leave Software Configuration on default "1 Compile tools for X-Windows", then "3 Save configuration & run configure".

6. When it's done being configured, run make.

7. Panic when it refuses to compile, but then calm down and remember that I compile things for a living. Search Google, discover that a function return value changed (sacrilege!), and edit /usr/src/lirc-0.8.2pre2/drivers/lirc_dev/lirc_dev.c to not bother checking the (now-void) return value of unregister_chrdev(). Re-run make, then make install (as root) when it's done.

8. Copy the lircd.conf into the place my distro wants it, rather than the place specified in the webpage:
cp remotes/mceusb/lircd.conf.mceusb /etc/lircd.conf

9. Load the new lirc_mceusb2 module, and when that works, add it to /etc/rc.local so it'll run on startup:
modprobe lirc_mceusb2

10. Start lircd, and when that works, add it to /etc/rc.local so it'll run on startup:
/etc/init.d/lirc restart

11. Test that the remote control buttons work - run irw and then start hitting buttons on the remote. It should come back with output something like this:
000000037ff07bee 00 VolDown mceusb
000000037ff07bee 01 VolDown mceusb
000000037ff07bef 00 VolUp mceusb
000000037ff07bef 01 VolUp mceusb
000000037ff07be9 00 Play mceusb
000000037ff07be9 01 Play mceusb


12. Now, to get mplayer working. I didn't have the source kicking around, so - download the latest version of mplayer.

13. Unzip and untar the source, and check out the configure options with:
./configure --help

14. Configure mplayer with the GUI and lirc support enabled:
./configure --enable-gui --enable-lirc

15. Once that finishes, run make, and then when that finishes, make install (as root).

16. Read mplayer's manpage to determine that the default lirc configuration file is assumed to be ~/.lircrc, or that you can specify another path with -lircconf <filename>.

17. Google search for mplayer lirc configuration, and find this other helpful page.

18. As mentioned on that page, run mplayer -input cmdlist to get a list of commands that mplayer accepts. For example:
quit                 [Integer]
pause
volume               Float [Integer]
mute                 [Integer]


19. Combine these commands with the output from your remote (from step 11) in your ~/.lircrc file in this format:
begin
   prog = mplayer
   button = VolDown
   repeat = 1
   config = volume -1
end
begin
   prog = mplayer
   button = VolUp
   repeat = 1
   config = volume 1
end
begin
   prog = mplayer
   button = Pause
   config = pause
end
begin
   prog = mplayer
   button = Mute
   config = mute
end
begin
   prog = mplayer
   button = Stop
   config = quit
end


20. Play something in mplayer, and feel like a Linux god when you can actually turn the volume up and down. Strut around the place, take elaborate bows, say stuff like "yes, I am great" until Holly gets tired of it.

And there you have it. A Linux triumph in less than a day. w00t!

2 comments:

Raven said...

You make it sound so easy :-)

Also, kudos for successfully watching shows with a baby in the room. We haven't been able to do that at all.

Claude said...

Getting Linux apps to work with IR remotes is actually fairly straightforward....

It's the IR transmitter that's a bitch. I never managed to get that to work.