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:
begin prog = mythtv button = Teletext config = T repeat = 3 end
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”:
begin remote = mceusb button = KEY_BACK prog = irexec repeat = 3 config = xte -x :0 'key Escape' end
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 kremotecontrol lirc lirc-remotes also xautomation 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 |
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”
begin remote name mceusb bits 16 flags RC6|CONST_LENGTH eps 30 aeps 100 header 2667 889 one 444 444 zero 444 444 pre_data_bits 21 pre_data 0x37FF0 gap 105000 toggle_bit 22 rc6_mask 0x100000000 |
7. | Add the following directly after this section, under “rc6_mask…”
suppress_repeat 3 |
8. | Add the following to the end of “/etc/profile”
modprobe mceusb This will run “modprobe mceusb” whenever a user logs in. This is necessary |
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: begin remote = mceusb button = KEY_OK prog = irexec repeat = 3 config = xte -x :0 'key Enter' end |
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.
- PC starts but kernel does not load MCE as it is blacklisted
- Because you have included “modprobe mceusb” at the bottom of “/etc/profile” MCE is now available to lirc
- Lirc is autostarted because it’s a service.
- The user logs in, automatically in my case.
- As part of the autostart process in Gnome 3 the file “irexec.desktop” is used to run “/usr/bin/restart_lirc.sh”
- The script “/usr/bin/restart_lirc.sh” stops the lirc daemon and restarts it and also starts irexec
- The user chooses to start MythTV by clicking on the power button on the remote
- 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.
- “restart_mythfrontend.sh” starts MythTV
- 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”
- 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.
- However it detects mythfrontend is running and so instead of starting mythfrontend it
stops it. - The user wants to shutdown Fedora and presses the blue button on the remote.
- 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.
- 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”.
- 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”:
begin remote name mceusb bits 16 flags RC6|CONST_LENGTH eps 30 aeps 100 header 2667 889 one 444 444 zero 444 444 pre_data_bits 21 pre_data 0x37FF0 gap 105000 toggle_bit 22 rc6_mask 0x100000000
Add the following directly after this section, under “rc6_mask…”
suppress_repeat 3
Files
/root/.lircrc
# # If the power button is pressed then start # MythTV # begin remote = mceusb button = KEY_POWER prog = irexec repeat = 3 config = restart_mythfrontend.sh end # # If <esc> is pressed then use "xte" to send # an excape character # begin remote = mceusb button = KEY_BACK prog = irexec repeat = 3 config = xte -x :0 'key Escape' end # # if "Ok" is pressed then use "xte" send an <Enter> # character. # begin remote = mceusb button = KEY_OK prog = irexec repeat = 3 config = xte -x :0 'key Enter' end # # If the blue key, at the bottom of the remote is pressed then begin remote = mceusb button = KEY_BLUE prog = irexec repeat = 3 # Fedora 16 #config = /usr/bin/shutdown_dlg.sh # Fedora 14 config = xte -x :0 'keydown Alt_L'; xte -x :0 'keydown Control_L';xte -x :0 'key Delete';xte -x :0 'keyup Control_L';xte -x :0 'keyup Alt_L' end
/usr/bin/restart_mythfrontend.sh
#!/bin/sh # # Script to be driven by the power button on the # Infra-red remote control. # # If Myth Frontend is running then it is killed and # if it is not running it Myth Frontend is started. # # # This script is based on work by described on the MythTV wiki: # # http://www.mythtv.org/wiki/Setting_A_Button_On_Your_Remote_To_Restart_mythfrontend # # Set the X display so that any following commands will # be sent to the correct display export DISPLAY=:0 # Initialise RUNNING=0; # # Determine if MythTV frontend is running # for x in `ps -C mythfrontend | grep -v PID` end; do test $x != 'mythfrontend' && continue RUNNING=1; done if [ $RUNNING = 1 ]; then # # If Myth Frontend is running kill all instances # echo "Front end running" killall mythfrontend else # # If Myth Frontend not running then start it # echo "Starting Frontend" mythfrontend & fi
/root/.mythtv/lircrc
begin prog = mythtv button = KEY_1 config = 1 repeat = 3 end begin prog = mythtv button = KEY_2 config = 2 repeat = 3 end begin prog = mythtv button = KEY_3 config = 3 repeat = 3 end begin prog = mythtv button = KEY_4 config = 4 repeat = 3 end begin prog = mythtv button = KEY_5 config = 5 repeat = 3 end begin prog = mythtv button = KEY_6 config = 6 repeat = 3 end begin prog = mythtv button = KEY_7 config = 7 repeat = 3 end begin prog = mythtv button = KEY_8 config = 8 repeat = 3 end begin prog = mythtv button = KEY_9 config = 9 repeat = 3 end begin prog = mythtv button = KEY_0 config = 0 repeat = 3 end begin prog = mythtv button = KEY_MEDIA config = Esc repeat = 3 end begin prog = mythtv button = KEY_EPG config = M repeat = 3 end begin prog = mythtv button = More config = I repeat = 3 end begin prog = mythtv button = KEY_VOLUMEDOWN repeat = 1 #config = F10 config = [ repeat = 3 end begin prog = mythtv button = KEY_VOLUMEUP repeat = 1 #config = F11 config = ] repeat = 3 end begin prog = mythtv button = KEY_CHANNELUP repeat = 3 config = Up repeat = 3 end begin prog = mythtv button = KEY_CHANNELDOWN repeat = 3 config = Down repeat = 3 end begin prog = mythtv button = KEY_UP repeat = 3 config = Up repeat = 3 end begin prog = mythtv button = KEY_DOWN repeat = 3 config = Down repeat = 3 end begin prog = mythtv button = KEY_LEFT repeat = 3 config = Left repeat = 3 end begin prog = mythtv button = KEY_RIGHT repeat = 3 config = Right end begin prog = mythtv button = KEY_PLAY config = Return repeat = 3 end begin prog = mythtv button = KEY_ENTER config = Return repeat = 3 end begin prog = mythtv button = KEY_MUTE config = | repeat = 3 end begin prog = mythtv button = KEY_REWIND config = < repeat = 3 end begin prog = mythtv button = KEY_FASTFORWARD config = > repeat = 3 end begin prog = mythtv button = KEY_RECORD config = R repeat = 3 end begin prog = mythtv button = KEY_STOP config = O repeat = 3 end begin prog = mythtv button = KEY_PAUSE config = P repeat = 3 end # Use for backwards commercial skip begin prog = mythtv button = KEY_PREVIOUS config = Q repeat = 3 end # Use for forward commercial skip begin prog = mythtv button = KEY_NEXT config = Z repeat = 3 end begin prog = mythtv button = KEY_TUNER repeat = 3 config = ALT+L end # Toggle subtitles (closed captions) begin prog = mythtv button = Teletext config = T repeat = 3 end # # Pressing the MCE "home" buttom brings up the menu # begin prog = mythtv button = KEY_HOME repeat = 3 config = M end
/usr/bin/shutdown_dlg.sh
#!/bin/sh # # Script to display the logout UI # export DISPLAY=:0 /usr/bin/gnome-session-quit --power-off
/etc/xdg/autostart/irexec.desktop
[Desktop Entry] Type=Application Name=irexec # Run irexec as a forked process by specifying "-d" or "--daemon" Exec=/usr/bin/restart_lirc.sh Icon=system-run Comment= Name[en_US]=irexec Comment[en_US]= X-GNOME-Autostart-enabled=true
/usr/bin/restart_lirc.sh
#!/bin/sh # # Script to restart lirc and then start up irexec # # It is vital that irexec starts AFTER lirc and this # script will ensure that. # # This will start from an autostart shortcut at # # /etc/xdg/autostart/irexec.desktop # # Kill any running irexec processes killall -9 irexec # Restart the lirc daemon service lirc restart # Start irexec irexec --daemon