Fedora, Linux, IR Remotes, LiRC and MythTV

Remote Control

This post is mostly focused around getting a Microsoft MCE, Media Centre Edition, infra-red remote working with a MythTV frontend. However I will be covering as much as I can about about generic issues.

My setup has Fedora 16 running with a hard drive install of Fedora LiveCD and I did this work in May 2012. This also means a Gnome Windows Manager.

Our system uses an MCE remote and the default MCE remote control. Because it is a USB remote it is referred to as “mceusb”.

Please note that like many similar articles this one is an evolving one as I learn more about the subject. The config files and shell scripts in particular may not be perfect and you may need to expand on the information here.

I have tried to flesh out as much of the detail as I can. I appreciate that my circumstances won’t exactly fit yours and as time passes the situation will change, however I hope there is enough that it can help.

Note: This article assumes that you are logging in as “root”. Yes I know not secure but it’s just for an internal MythTV. I have tried to do this as user other than root and it seems to get what I want is very difficult unless you are root, so I am sticking with root !

Resources

Linux Infrared Remote Control (LIRC) website
http://www.lirc.org/

RPM Fusion
http://rpmfusion.org

LIRC

LIRC has been the traditional way for infra-red remote to interact with Linux and is indeed the way that MythTV works best with an IR. In the latest versions of the Linux kernel and the latest versions of Fedora, specifically F15 & F16, this functionality has been moved into the kernel.

This has caused problems with trying to use an IR with MythTV because firstly the implementation in the kernel doesn’t seem near perfect at this time but as much as that is the problem that as soon as the kernel grabs the IR it means LIRC can’t get to it.

Lirc config files

There can be a number of config files for lirc but since we are dealing with MythTV there are two we are interested in:

Lirc IR config used by MythTV:    /root/.mythtv/lircrc
Global IR config used by lirc:    /root/.lircrc
Lirc config:                      /etc/lirc/lircd.conf

The first two IR configs are used to customize what key presses do for MythTV and a global config for key presses. This is different to the “lircd.conf” which is concerned with the configuration of the actual remote, in our case MCE.

It would seem that if MythTV is not running then “/root/.mythtv/lircrc” is not used. However if MythTV is running then both “/root/.mythtv/lircrc” and “/root/.lircrc”. When both config files are in use I am not sure what happens if a key has a defined action in both sets – my sense of it is that both are used and both actions are taken. For this reason it seems bets to define each key in only one of the configs.

Key definitions in config files

A typical key definition looks like this:

 

As this is inside the MythTV lirc config, “/root/.mythtv/lircrc”, it is interpreted by MythTV and sends mythfrontend a “T” key. It also has “repeat =3” this means that if the button is held down and sends multiple key strokes it won’t autorepeat until at least 3 keys strokes have been pressed. It stops the problem with 2 key strokes being passed when you push buttons.

On the other hand look at the following key definition from the global config file, “/root/.lircrc”:

It says that if a “KEY_BACK” is pressed it should run “irexec” to run “xte” to simulate pressing an “<esc>” key.

Tools

irw

Irw is a command line tool for monitoring IR key presses. Start up the LIRC daemon, “service lirc start” and then run “irw” from the command line. This should give output similar to:

[root@mytha ~]# irw
000000037ff07bdd 00 KEY_OK mceusb
000000037ff07bdc 00 KEY_BACK mceusb
000000037ff07be9 00 KEY_PLAY mceusb
000000037ff07ba1 00 KEY_BLUE mceusb
000000037ff07bf1 00 KEY_MUTE mceusb

xte

This is found in the package “xautomation”. The xte man page says:

xte is a program that generates fake input using the XTest
extension, more reliable than xse.

For us it allows us to press a key on the remote and have a command line tool send a given key or keys to an X display.

irexec

This is a utility tool which I think comes with lirc. Its a little daemon process, well its a daemon process if you run it “irexec -d”, that will run any arbitrary script or application

For irexec to run it has to be started after the lirc daemon starts. This is the reason for the script “/usr/bin/restart_lirc.sh”

Steps

1. Install YumEx, though I don’t think this is absolutely required but I
prefer it over the default of Package Manager.
2. Add RPMFusion repositories. Do this by going to http://rpmfusion.org and
clicking on “Configuration”. Again probably not absolutely necessary but
what I prefer to do.
3. Using YumEx add the following LIRC packages:

gnome-lirc-properties
Gnome GUI for remote controls   (Probably not necessary)

kremotecontrol
KDE front end for remote controls   (Probably not necessary)

lirc

lirc-remotes

also

xautomation
Provides “xte” for generating fake keystrokes

If you are having problems I would remove all Lirc modules except the ones listed above.

4. We need to stop the kernel from grabbing the IR so we need to add it to the
black list of kernel devices.Edit “/etc/modprobe.d/blacklist.conf” and add:

blacklist mceusb
blacklist lirc_dev
blacklist rc_core

It is probably only necessary to have “mceusb” on the list but my current system
has all 3 and doesn’t seem to do any harm.

5. The Lirc GUI does not seem to work in Fedora 16 so we need to copy the config file
for the default MCE remote to the lirc directory.Run the following from a terminal

cd /etc/lirc
mv lircd.conf lircd.conf.ootb
cp /usr/share/lirc-remotes/mceusb/lircd.conf.mceusb /etc/lirc/lircd.conf

Note “lircd.conf” may not exist so the “mv” command may fail

6. Edit “/etc/lirc/lircd.conf”

7. Add the following directly after this section, under “rc6_mask…”

8. Add the following to the end of “/etc/profile”

This will run “modprobe mceusb” whenever a user logs in. This is necessary
since we have blacklisted mce in the kernel.

9. Reboot
10. Login to a terminal and run irw and press some controls on the remote, you should
see something similar to

000000037ff07bdd 00 KEY_OK mceusb
000000037ff07bdc 00 KEY_BACK mceusb
000000037ff07be9 00 KEY_PLAY mceusb
11. Assuming that goes well kill “irw” and continue.
12. You now need need to create
13. Create “/usr/bin/restart_mythfrontend.sh”, a copy can be found at the bottom of this
article.This is a shell script for stopping and starting mythfrontend. Ensure this has execute
permissions for “root” or whatever user you run mythfrontend.
14. Create “/root/.lircrc” a sample of this is at the bottom of this article.Note: Even if you are logging in as a different user you still need to create this
file as “/root/.mythtv/lircrc” because the service is running as root
15. Create a “/root/.mythtv/lircrc” file a sample is at the bottom of this article.If you don’t run mythfrontend as “root” then put this file in the home directory of the user you use to run mythfrontend.- The users home directory may not be what you expect. In the case of the “mythtv” user the file will be:/var/lib/mythtv/.mythtv/lircrc
16. Create “/usr/bin/shutdown_dlg.sh”, a copy can be found at the bottom of this
article.This is a shell script for displaying the shutdown prompt. Ensure this has execute permissions for “root” or whatever user you run mythfrontend.This script will be run when you press the blue key on the remote. You can see this
in the “/root/.lircrc” config file in the section:
17. Create “/usr/bin/restart_lirc.sh”, a copy can be found at the bottom of this
article.This is a shell script for stopping the lirc daemon and then restarting it followed
by starting irexec.This script is necessary to ensure that irexec starts AFTER lirc. Its a pragmatic and
simple solution.
18. Create a desktop autostart shortcut “/etc/xdg/autostart/irexec.desktop”, Its a text file and you can find the copy of it at the bottom of this article.This file will be used to restart irexec. Have a look at “/etc/xdg/autostart/irexec.desktop”, you should see it’s pretty intuitive.

 

How does it go

This whole process is a bit confusing, well it certainly was for me to begin with. So if I outline how I think it is working that might help.

  1. PC starts but kernel does not load MCE as it is blacklisted
  2. Because you have included “modprobe mceusb” at the bottom of “/etc/profile” MCE is now available to lirc
  3. Lirc is autostarted because it’s a service.
  4. The user logs in, automatically in my case.
  5. As part of the autostart process in Gnome 3 the file “irexec.desktop” is used to run “/usr/bin/restart_lirc.sh”
  6. The script “/usr/bin/restart_lirc.sh” stops the lirc daemon and restarts it and also starts irexec
  7. The user chooses to start MythTV by clicking on the power button on the remote
  8. Lirc receives the key “KEY_POWER” and only knows about the config file “/root/.lircrc”, this details to run “restart_mythfrontend.sh” when the power button is pressed.
  9. “restart_mythfrontend.sh” starts MythTV
  10. The user presses various keys on the remote to do things in MythTV. What key does what is determined by the file “/root/.mythtv/lircrc”
  11. Eventually the user wants to close down MythTV frontend so he presses the power button again and again it is picked up by “/root/.mythtv/lircrc” and “restart_mythfrontend.sh” is run.
  12. However it detects mythfrontend is running and so instead of starting mythfrontend it
    stops it.
  13. The user wants to shutdown Fedora and presses the blue button on the remote.
  14. Lirc uses “/root/.mythtv/lircrc” to translate a key “KEY_BLUE” to run the script “/usr/bin/shutdown_dlg.sh” which brings up the shutdown dialog.
  15. If the user want to shutdown he presses “KEY_OK” and lirc translates this to use “xte” to simulate pressing “<Enter>” using “/root/.mythtv/lircrc”.
  16. If the user decided he doesn’t want to shutdown and presses “KEY_BACK” then lirc once again uses “/root/.mythtv/lircrc” to use “xte” to simulate presses an “<esc>” key and exit the shutdown dialog.

Trouble shooting

Double key presses

One common problem with using Lirc in Fedora 16 is that each press of a IR key results in two keystrokes which can give frustrating and confusing results.

To see if this is happening run “irw” and press keys. If you see double keys coming through like below then you know this is your problem:

000000037ff07bdd 00 KEY_OK mceusb
000000037ff07bdd 00 KEY_OK mceusb
000000037ff07bdc 00 KEY_BACK mceusb
000000037ff07bdc 00 KEY_BACK mceusb
000000037ff07be9 00 KEY_PLAY mceusb
000000037ff07be9 00 KEY_PLAY mceusb

There are a number of resolutions to this as I found out, however the problem was resolved by editing “/etc/lirc/lircd.conf”:

Add the following directly after this section, under “rc6_mask…”

 

Files

/root/.lircrc

 /usr/bin/restart_mythfrontend.sh

/root/.mythtv/lircrc

 /usr/bin/shutdown_dlg.sh

 /etc/xdg/autostart/irexec.desktop

 /usr/bin/restart_lirc.sh