Productivity

Control Your Desktop PC With A Remote Using LIRC

Walking to your computer to change a song or to skip to the next PowerPoint slide isn’t ideal. Set up LIRC to control your Linux or Windows PC with any infrared remote, and in any situation.

We’ve covered controlling your XBMC with a variety of apps and remote controls, but if you already have a universal remote and you want to control more than just XBMC centre you’ll need to do some customisation — this is where LIRC comes in. LIRC is a set of tools that will enable nearly any infrared remote control to communicate with your PC. Not only does it have built-in support for media programs like XBMC, but you can configure it to do almost anything in a given program — switch tracks in Amarok, give a presentation with OpenOffice, or watch a video in MPlayer without having to get near your mouse and keyboard.

LIRC was orignally for Linux, though a Windows port is available (known as WinLIRC). This particular how-to will be for Linux, but where appropriate I will note the differences between the Windows version and the Linux version so Windows users can follow along — all my field testing is with the Linux version, so Windows users trying out WinLIRC should post their results and tips in the comments below.

If you’re setting up LIRC on Linux, be forewarned: there is a lot of command line work involved. Most Linux users will be pretty familiar with this, but I will say it’s the most command line and configuration file editing work I’ve done for one particular project, so prepare yourself.

Get Your Hardware Together

LIRC is extremely popular with homebrew IR receivers, but if you’re not quite the electronic DIY whiz, you have a few options. If you’re running this on Linux, the IRA-3 from Home Electronics has worked very well for me, and does not require recompiling the kernel which most receivers do. If you’re going for the Windows approach, you’re more or less stuck with homebrew receivers, since it’s usually hard to find receivers on their own for sale. Your best bet is to search eBay for serial IR receivers — in the past, I’ve actually found a few good-looking options there that will even tell you if they work with WinLIRC or not.

Note also that LIRC requires the IR receiver to be a serial device — USB won’t work, whether direct or with an adapter; you’ll either need a free serial port on your computer or, if you’re doing this on Windows, you can also use a built-in compatible IR receiver such as the one on Hauppauge TV tuner cards (you could do this on Linux, too, but you’d have to recompile the kernel, which is unnecessary if you go with the aforementioned IRA-3).

As far as remotes go, pretty much any infrared remote should work fine.

Installing LIRC and Checking Your Connections

LIRC (version 0.8.6 at the time of this writing) should be in the Ubuntu repository, so assuming you’re using Ubuntu you can just install lircd from Synaptic (otherwise, you’ll have to download and install lircd manually from their web site). In addition, if you’re using an Irman device (such as the IRA-3 suggested above), you need to install libirman-dev as well. On Windows, just download the zip file and extract it somewhere.

First, you’ll need to figure out in which port your device is plugged — the first serial port on a Linux PC being /dev/ttyS0, the second being /dev/ttys1, and so on. To check if it’s plugged into the right port and if it’s communicating with your remote, open up Terminal and type: cat /dev/ttyS0, replacing ttyS0 with the serial port you’re hooked into. You should see the red light on the device light up, and if you’re lucky, pressing some buttons on your remote should cause the terminal to barf up some nonsense, as seen below.

On my remote, only one or two buttons actually caused this, so if it doesn’t work by just mashing buttons, don’t fret — carefully go through and press every button on your remote. It’s likely one of them will cause a whole string of gibberish to fill up your terminal window, and that’s what we’re looking for — it means you’ve defined the correct port and that everything is communicating properly.

Now you need to get your hardware.conf and lircd.conf set up. You’ll need root privileges to do this, so type sudo gedit /etc/lirc/hardware.conf in Terminal to edit the it). A hardware.conf for an Irman device hooked up to /dev/ttyS0 should look something like this:

REMOTE=”Irman / UIR”
REMOTE_MODULES-”"
REMOTE_DRIVER=”irman”
REMOTE_DEVICE=”/dev/ttyS0″
REMOTE_LIRCD_CONF=”/etc/lirc/lircd.conf”
REMOTE_LIRCD_ARGS=”"
TRANSMITTER=”None”
TRANSMITTER_MODULES=”"
TRANSMITTER_DRIVER=”"
TRANSMITTER_DEVICE=”"
TRANSMITTER_LIRCD_CONF=”"
TRANSMITTER_LIRCD_ARGS=”"
START_LIRCD=”true”
START_LIRCMD=”"
LOAD_MODULES=”"
LIRCMD_CONF=”"
FORCE_NONINTERACTIVE_RECONFIGURATION=”false”

Of course, you’ll need to replace the location of your device, the type of device and driver above with the attributes of your own hardware should you be using something different than I have suggested.

For Windows users, the configuration will all be done within the GUI for WinLIRC. Launch WinLIRC and it will give you a popup telling you it failed — click OK and it’ll bring you to the main window. Choose the COM port in which it’s installed, and leave the rest of the settings as their default, as that should be OK. If you click the browse button next to the config line and choose sample.cf, which came with WinLIRC, you should be able to test if it’s working. Hit the Raw Codes button and point your remote at your receiver. If you see codes, you’re all set and you can move on.

Configure LIRC with Your Remote

Next, you’ll need to create an lircd.conf file for your remote — the most important part of the process. This is what translates the button codes on your remote into commands for your other software to interpret. There is a list of lircd.conf files on lirc’s website that correspond to different remotes, but I haven’t had good luck with these — I prefer to create my own using irrecord (a process that is built-in to WinLIRC if you’re on Windows). I’ve just gotten much better results doing it myself; the pre-made config files always gave me errors.

You’ll likely need to run irrecord with some options that tell it what you’re using. For an Irman device hooked up to /dev/ttyS0, your terminal command to launch it would look like this:

irrecord -H irman -d /dev/ttyS0 lircd.conf

Once again, replacing irman and /dev/ttyS0 with your respective driver and port if you’re using your own hardware. After that, it’s just a matter of following the instructions. The new version of LIRC requires that you use button names from their pre-made list, which can be found here. It doesn’t particularly matter which ones you use for which buttons; it’s just a way for LIRC to turn code into English so you can help it communicate with your desired software.

Most buttons on your remote will have an obvious counterpart on that list, but if they don’t, you can just use one of the other names that correspond to a button you don’t have (for example, my remote has a “Guide” button for which I saw no similar name on that list, so I just used KEY_YELLOW). After you’re finished with irrecord, your lircd.conf file will be in your home folder. Copy it over to /etc/lirc with root permissions (by typing sudo cp ~/lircd.conf /etc/lirc/ in Terminal), replacing the old lircd.conf that was there. You’ll also want to open it up and, next to “name”, replace “lirc” with the name of your remote (for my examples, I will use “urc6131n” as that is the name of my remote). Below is a picture of a typical lircd.conf file.

To test if your configuration works, run irw in Terminal. It will give you the illusion of a terminal hang, but if you press buttons on your remote, it should spit the names of the buttons out at you. If it does, congratulations, you rule at UNIX.

Windows users will follow pretty much the same process. From the main WinLIRC window, hit the Learn button to start up WinLIRC’s built-in version of irrecord. Just follow the directions; it should be the same as above with the exception of the first two steps (give it an error margin of 25 and just leave the custom gap length blank) and the fact that you can use whatever names you want for the buttons, you don’t have to use LIRC’s new naming system (so you can just type “Fastforward” for the power button if you prefer). When you’ve gone through all the buttons, just leave the input box blank and hit enter. When you’re done with everything, hit OK in the main window and it will minimise to the taskbar, awaiting input.

Getting LIRC to Communicate with the Software of Your Choice

Setting up your remote is only half of the equation — the other half is getting LIRC to communicate with the software you want to control. Some software (such as our favourite media centre software, XBMC) has built-in support for LIRC with just a bit more configuration (the process for XBMC is very well described here, so I won’t go through it myself). For most programs, though, you’ll want to create a global .lircrc file, and map it to keyboard shortcuts or application commands to get it to do what you want. As an example, we’ll use Amarok, although the possibilities are pretty much endless if you put your mind to it.

Create a text file in your home directory called .lircrc and open it up in gedit. The basic format looks like this:

begin
prog = irexec
remote = urc6131n
button = KEY_PLAY
config = dbus-send –type=method_call –dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.Play
end

begin
prog = irexec
remote = urc6131n
button = KEY_STOP
config = dbus-send –type=method_call –dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.Stop
end

begin
prog = irexec
remote = urc6131n
button = KEY_PAUSE
config = dbus-send –type=method_call –dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.Pause
end

Repeat that block for each button you want to have a function. Basically, you’re mapping your buttons to specific terminal commands that control your program — in this case, dbus-send –type=method_call –dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.Pause is the terminal command for Amarok to pause music (if it’s not working for you, you may need kdelibs4c2a installed). You can make slightly more complicated commands as well (incorporating multiple buttons, delays, etc) — more information this being available here. If you choose to configure it this way, you must have irexec running in the background for your remote to work (you can either launch it in Terminal beforehand or set it up to run when your computer launches in System > Preferences > Startup Applications if you’re running Ubuntu).

Another way to go about this would be to use keyboard commands using irxevent instead of irexec (for controlling X applications in Linux). You’ll need to install lirc-x (available in the Ubuntu repositories as well) first, and then form your .lircrc file like this:

begin
remote = urc6131
prog = irxevent
button = KEY_PLAY
config = Key space CurrentWindow
end

In order to do keyboard combinations, you’d need more knowledge of batch scripting, but since lots of programs have local, one-key shortcuts that you can configure (as shown below in Amarok), you’re better off just assigning specific functions within the application to keys. The above example presses the spacebar in the current window when I press play on my remote — effectively telling Amarok to play if it’s my frontmost window. It’s not perfect if you want to have multiple things running at once, but it works pretty well if you’re not actively using the PC at the time (though keep in mind that you’ll have to have irxevent running for your remote to communicate with the program in question, just as you would irexec in the example above).

For Windows, you can use Lifehacker’s favourite keymapping utility AutoHotKey to do essentially the same thing. You can get a template for the AutoHotKey script here, editing the middle section to your liking. The script has instructions inside it, so if you’re at all familiar with AutoHotkey, programming it shouldn’t be a problem — it’s the same as mapping keyboard keys, except using the button names that you defined during the setup process. For more information on mapping keys with AutoHotKey, check out our guide to turning any action into a keyboard shortcut.

These are just a few of the ways you can use LIRC to control your favourite programs. It’s extremely open-ended, so I’ve only presented you with a few basic examples, but with a little bit of research you figure out how to do just about anything. For more information on LIRC and doing some more advanced things with the configuration files, as well as other software that works with LIRC, you’ll definitely want to check out LIRC’s website and documentation. Windows users should definitely peruse WinLIRC’s website as well.

For mapping a specific program, your best bet is to use the irexec method if possible, so you’ll need to either look through that program’s documentation or Google around to find terminal commands that correspond to functions within that program.

If you’ve got a favourite use for LIRC or WinLIRC, let us know what it is and how you got it working in the comments!