Aug 032009
 

As the topic says; I am a Windows user. I been using the XBMC on Xbox1 for many years. Since the hardware on Xbox does not handle the new â??HD-thingâ?, I started to find a replacement. This is my recipe that forced me to learn some basic Linux stuff.

Read my part 1 before you continue with this one.

1. Requirements
You need these following items:
* Ubuntu preinstalled with XBMC and LIRC
* A MCE IR receiver/remote that is supported by LIRC
* A confirmed working LIRC, tested with the irw command

2. What do you want it to do?
My HTPC requirements is XBMC, switch between another program (like MythTV) and do simple movements on the desktop. Another thing that is important is the on/off function and my prefer method is suspend-to-ram. It is quick, quiet and fully manageable on Linux.
Much of the information (and some scripts) has been processed and modified from vikjon’s Blog

3. Setting up IRExec+IRXevent and application specific settings
LIRC has an extra tool called IRExec+IRXevent that basically execute a command to terminal when an event happens. Also the MythTV-crew have made a generator for default LIRC-config. It creates application specific settings, we’ll now install’em both:
1. Open a terminal (Applications -> Accessories -> Terminal)
2. Install IRExec+IRXevent by entering: sudo apt-get install lirc-x
3. Install Mythbuntu-lirc-generator by entering: sudo apt-get install mythbuntu-lirc-generator
4. Create default settings running: mythbuntu-lirc-generator
5. Start IRExec to check that it works, enter: irexec (nothing should be printed and a waiting cursor, break it with Ctrl-C)

LIRC is installed as a daemon (service) but the IRExec+IRXevent need to be in your “user space”. This script shall be started when you login. Here is what todo (in terminal):
1. Create a scripts directory, enter: mkdir ~/scripts
2. Create a script-file, enter: nano ~/scripts/startirexec.sh
3. Paste inn these lines, replace the “username” in the text with your own username

#!/bin/bash

# Test to see if IRXevent is running first, if so kill it, then restart
if ps -ef|grep -v grep|grep -i irxevent
then
ps aux|grep -i username|grep -i irxevent |awk ‘{print $2}’|xargs kill
else
# Do nothing
echo “irxevent already dead!”
fi

# Test to see if IRexec is running first, if so kill it, then restart
if ps -ef|grep -v grep|grep -vi start|grep -i irexec
then
ps aux|grep -i username|grep -i irexec |grep -vi start|awk ‘{print $2}’|xargs kill
else
# Do nothing
echo “irexec already dead!”
fi

#test to see if an instance of irxevent is already running
if ps -ef|grep -v grep|grep irxevent
then
# do nothing
echo “irxevent already running”
else
# start irxevent
irxevent /home/username/.lircrc &
fi

#test to see if an instance of irexec is already running
if ps -ef|grep -v grep|grep irexec
then
# do nothing
echo “irexec already running”
else
# start irxevent
irexec -d /home/username/.lircrc &
fi

exit

4. Save the file by pressing Ctrl-X, say Yes to save.
5. Go to System -> Preferences -> Startup applications
6. Press add and goto the directory “scripts”. Add the startirexec.sh file

When you restart and same user logs in, the IRXevent+IRExec will be running in the background.

3.1 Start/kill scripts for your applications
Here are two examples of start/kill application scripts. You should create one on each program you want to control using the remote.

Start-script (to customize to others change the xbmc.bin and the executable xbmc later on)

#!/bin/bash

# Test to see if XBMC is running first
if ps -ef|grep -v grep|grep -i xbmc.bin
then
# Do nothing
echo “XBMC already Running!”
else
# Startup XBMC
xbmc
fi
exit

Kill script (it does a soft kill first and hard after some time. Replace the “username” with your username.)
To manage other applications replace the xbmc and xbmc.bin with your executable.

#!/bin/bash

# Test to see if XBMC is running first
if ps -ef|grep -v grep|grep -i xbmc.bin
then
# Try a clean kill
ps aux|grep -i username|grep -v grep|grep -i xbmc.bin|awk ‘{print $2}’|xargs kill
echo `date` “Killed XBMC! (soft)” >> /tmp/killXBMC.log
else
echo “XBMC already dead! (soft)”
exit
fi

# takes a second or two to die with the soft kill
sleep 2

# Test to see if it’s still running
if ps -ef|grep -v grep|grep -i xbmc.bin
then
# If it’s still around, kill it -9
ps aux|grep -i username|grep -v grep|grep -i xbmc.bin|awk ‘{print $2}’|xargs kill -9
echo `date` “Killed XBMC! (hard)” >> /tmp/killXBMC.log
else
echo “XBMC already dead! (hard)”
exit
fi

Both of these scripts has been stored as ~/scripts/startxbmc.sh and ~/scripts/killxbmc.sh.

You could also create a ~/scripts/reboot.sh, it could look something like this

#!/bin/bash
sudo reboot
exit

Simple and hopefully you have turn off the password-nagging (as described in part 1) so it actually works 😉

3.2 Executing these scripts using the IRExec
The mythbuntu-lirc-generator created a hidden directory that contains custom settings for several applications. We are now going to create a simple “common” script that will work independent on which program you have executed.

1. Open a terminal (Applications -> Accessories -> Terminal)
2. Create a common file for our commands, enter: nano ~/.lirc/common
3. Paste inn these commands

#Kill then start XBMC when pressing the Videos button
begin
remote = mceusb
prog = irexec
button = Videos
config = sh /home/username/scripts/killxbmc.sh
config = sh /home/username/scripts/startxbmc.sh &
repeat = 0
delay = 0
end

#Reboot the computer when you press the “Clear” button
begin
remote = mceusb
prog = irexec
button = Clear
config = sh /home/username/scripts/reboot.sh &
repeat = 0
delay = 0
end

4. Press Ctrl-X and save file.
5. Now we have to include the file as a LIRC resource, enter: nano ~/.lircrc
6. Add at the end of the file: include ~/.lirc/common
7. Press Ctrl-X and save file
8. Restart the IRExec by entering: sh ~/scripts/startirexec.sh

Try the remote by pressing “My Videos” and see that XBMC starts.

4. Wake-up on USB
MythTv have a good guide on how to get suspend-to-ram work, this info is partly taken from that guide.

1. Open a terminal (Applications -> Accessories -> Terminal)
2. List the devices available, enter: cat /proc/acpi/wakeup (it should look something like the list below)

Device S-state Status Sysfs node
PCI0 S4 disabled no-bus:pci0000:00
PEG1 S4 disabled pci:0000:00:01.0
IGBE S4 disabled pci:0000:00:19.0
PCX1 S4 disabled pci:0000:00:1c.0
HUB S4 disabled pci:0000:00:1e.0
USB0 S3 disabled pci:0000:00:1d.0
USB1 S3 disabled pci:0000:00:1d.1

3. Usally the USB0 is the one that your ir-receiver is connected to. To activate it enter: sudo sh -c ‘echo “USB0” >> /proc/acpi/wakeup’ (it should state S3 behind it, or else the item will be shutoff during suspend-to-ram)
4. Check in the list that the device is enabled, enter: cat /proc/acpi/wakeup

Device S-state Status Sysfs node
(snip)
HUB S4 disabled pci:0000:00:1e.0
USB0 S3 enabled pci:0000:00:1d.0
USB1 S3 disabled pci:0000:00:1d.1

5. Do a actual test (Warning; this might crash your system. Save any unsaved stuff before you test). Enter: sudo sh /etc/acpi/sleepbtn.sh
6. When it has suspended, hold the power-button on your remote down and see if it wakes up. (If it don’t you either got the wrong USB-device, in the wakeup-list, or the receiver doesn’t send a valid power-on signal to the computer.)

Notice that when the computer wakes up either it hangs or the remote does not work. This will be handled in the next chapter. But first, lets make the change wakeup device permanent.
1. Open a terminal (Applications -> Accessories -> Terminal)
2. Edit the rc.local file (equal to autoexec.bat for us Windows users): sudo nano /etc/rc.local
3. Add at the end of the file (but before exit 0): sudo sh -c ‘echo “USB0” >> /proc/acpi/wakeup’ (change the USB0 if another device worked for you)

Reboot your computer to check if the /proc/acpi/wakeup list has the USB0 (or what ever device working for you) enabled. Do a second test, just to make sure the suspend/resume works.

4.1 Suspend-to-ram and wakeup script
When a computer is suspended a list of devices, drivers etc are stopped. IRExec+IRXEvent needs LIRC to work properly. Both of them are not installed as daemons so they will not be included in a suspend (for some reason).
I have also encountered several lock-ups because of the mce_usb driver, so it is clever to remove driver before a suspend. If you have other drivers for LIRC, just replace the mce_usb with your own name.

The suspendscript will be located at the same spots as our other scripts, here is what you do:
1. Open a terminal (Applications -> Accessories -> Terminal)
2. Create the suspendfile, enter: nano ~/scripts/suspendcomputer.sh
3. Paste the script below into the file

#!/bin/sh
sh ~/scripts/killxbmc.sh
sudo /etc/init.d/lirc stop
sudo modprobe -r lirc_mceusb
sudo /etc/acpi/sleep.sh force
exit

4. Edit the common-file and make powerbutton, on the remote, run suspendcomputer.sh using the chapter 3.2 technique: nano ~/.lirc/common (if you are uncertain on the name of the powerbutton use irw to get them)

You could test the remote powerbutton and see that it suspend. Turn the power on with the remote again. Nothing IR-based is going to work since we have stopped LIRC and removed the drivers. Here is the wakeup-script.
1. Open a terminal (Applications -> Accessories -> Terminal)
2. Create a acpi-resume file by entering: sudo nano /etc/acpi/resume.d/99-resume-lirc.sh
3. Paste the script below into the file. (Change the “Username” with your username, since the resume-daemon execute on another user/level we need to specify.)

#!/bin/bash
sudo modprobe lirc_mceusb
sudo /etc/init.d/lirc start
sudo -u username sh /home/username/scripts/startIRExec.sh
sudo -u username sh /home/username/scripts/startxbmc.sh

4. Make sure the script is executeable by entering: sudo chmod +x /etc/acpi/resume.d/99-resume-lirc.sh
5. Test the script by entering: sudo sh /etc/acpi/resume.d/99-resume-lirc.sh

Now the LIRC and the driver should work again. So try a new suspend/resume using the remote and the 99-resume-lirc.sh will be the last script executed by the resume-daemon.

5. Navigate on desktop using IR
After reading Bryan Ludvigsens blog on how to control Snes using LIRC and Xmacro, I got the idea of use something similar for controlling the desktop.

First off you need to install xmacro, this is how you do it:
1. Open a terminal (Applications -> Accessories -> Terminal)
2. Install xmacro by entering: sudo apt-get install xmacro

Bryans approach is to complicated for me (linux newbie, you know). I took an easier route by reading this post. Here are some combinations you probably want to use, paste the line after the : – sign to test. If it works in terminal it works using the IRexec.

“Start”-button: echo -e ‘KeyStrPress Control_L\n KeyStrPress Escape\n KeyStrRelease Escape\n KeyStrRelease Control_L\n’ | xmacroplay -d 50 :0
Next window (alt-tab): echo -e ‘KeyStrPress Alt_L\n KeyStr Tab\n KeyStrRelease Alt_L’ | xmacroplay -d 50 :0
Close: echo -e ‘KeyStrPress Alt_L\n KeyStr F4\n KeyStrRelease Alt_L’ | xmacroplay :0

You probably got the picture. You can also record your own macros using this command: xmacrorec or xmacrorec2 (read more about that here). To find keyboard shortcuts read this wiki.

5.1 Grouping to re-use buttons in LIRC setup
LIRC support modes (or groups) that activate buttons based on where you are navigating. It is a way to seperate for example the Start-button on the remote. In XBMC you want to get the menu but in “desktop mode” you want it to open the start menu.
The ~/.lirc/common file would be a good place to seperate these modes, here is an example:

#Our wakeup-script execute xbmc and when LIRC is reloaded we are in “Xbmc-mode”
begin
flags = startup_mode
mode = mode_xbmc
end

#Global keys goes here (switch to/from applications). The XBMC button shall have the mode_xbmc (we don’t have any group since XBMC manage itself :))
begin
remote = mceusb
prog = irexec
button = Videos
mode = mode_xbmc
config = sh /home/username/scripts/killxbmc.sh
config = sh /home/username/scripts/startxbmc.sh &
repeat = 0
delay = 0
end

#Still global, but here we change to “desktop” mode
begin
remote = mceusb
prog = irexec
button = Music
mode = mode_desktop
config = sh /home/username/scripts/killxbmc.sh &
repeat = 0
delay = 0
end

#Here is the functions for the desktop, note the mode_desktop
begin mode_desktop

#Startbutton
begin
remote = mceusb
prog = irexec
button = Home
config = echo -e ‘KeyStrPress Control_L\n KeyStrPress Escape\n KeyStrRelease Escape\n KeyStrRelease Control_L\n’ | xmacroplay -d 50 :0
repeat = 0
delay = 0
end

# (…) More desktop-buttons goes here…

#End the mode_desktop here
end mode_desktop

When you press the “My Music” button XBMC will quit and the buttons below would be activated. When you are finished a press on “My Videos” will launch XBMC and deactivate the desktop-mode.

6. All scripts wrapped up
Here are my scripts that I’m currently using. Please note that you need to change the username in some of these scripts.
Common-script to be placed in: ~/.lirc/
Launchscripts to be used with IRExec. Common placement it ~/scripts/
Suspend/resume script to be placed in /etc/acpi/resume.d and common placement ~/scripts/

Note that these scripts contains comments on my native language Norwegian. But you should understand based on the guide. If there are any questions, please let me know in the comments.

    What is next…
    My HTPC works as expected so I don’t spend much time investigating. Nothing new will be added unless there are new stuff that could be interesting.
    The latest modification I’ve done is to install Spotify on Ubuntu, it is done easily by following this guide.
 Posted by at 12:45:49

  6 Responses to “A Windows user confession: Make LIRC on Ubuntu manage your HTPC + making suspend-to-ram work properly”

  1. I use ubuntu 10.04 LTS
    I have a mce usb remote that works fine in XBMC. Power button puts the puter to sleep.
    The system does suspend after the time set, It wakes up from mouse and keyboard but NOT from the remote.

    I have followed the instructions provided here to the tee and it doesn’t wake up.
    I did not get any errors during installation and the entire setup. Rebooted, started the XBMC, forced it to sleep but does not wake up by remote. I have tried in different ways with multiple reboots but it doesn’t work. Please help. This is one of the best guides in this matter that could be found on the net and it should be maintained as such. Thanks you very much.

  2. Jim Cox:
    The errormessage “irxevent: bad file format, /home/james/.lirc/common:23” shows that something is wrong with the file /home/james/.lirc/common on line 23.

    The file “common” I present i 3.2, is only 20 lines long. Something xtra has been added to the common-file is my best guess. Please check it and try again 🙂

  3. Hi, thankyou for the guide, I i’ve followed it to the end of section 3.2 but when I try to restart IRExec with the command sh ~/scripts/startirexec.sh

    I get:
    james@revo:/$ sh ~/scripts/startirexec.sh
    irxevent already dead!
    irexec already dead!
    irxevent: bad file format, /home/james/.lirc/common:23
    james 1811 1622 0 22:48 pts/0 00:00:00 sh /home/james/scripts/startirexec.sh
    irexec already running

    irxevent: bad file format, /home/james…etc… Any suggestions?

  4. when I run the ~/scripts/startirexec.sh, the error below:
    ——————————————————–
    xbmc@htrock:~/scripts$ ./startirexec.sh
    xbmc 7190 1 0 22:19 pts/2 00:00:00 irxevent /home/xbmc/.lircrc
    awk: 1: unexpected character 0xe2
    awk: line 2: missing } near end of file
    ——————————————————–

    I found the error should be the below line:
    ps aux|grep -i xbmc|grep -i irxevent |awk â??{print $2}â??|xargs kill

    I don’t know how to fix the error, Can you re-check the script?

    My system is Ubuntu 9.10 Karmix 64-bit.

  5. Thanks! Thanks! Thanks! I’m a win/mac person that’s getting to grips with ubuntu and you’re guide was pitched at just the right level.
    The suspend to ram stuff worked well for me.

  6. […]       « Adito v0.9.1 windows installer A Windows user confession: Make LIRC on Ubuntu manage your HTPC + making suspend-to-ram work properl… […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: