Adding LIRC to Mythbuntu Game Emulators

Most of the stuff in this post I found at an awesome wiki that explains how to completely configure mythgame for mythbuntu. This post just covers setting up zsnes and sdlmame to work with lirc so that you can use your remote to navigate menus and control volume and all that great stuff.

Here is a link to the article with information on almost every emulator out there and how to configure them: http://www.mythtv.org/wiki/Configuring_MythGame_Emulation#Remote_Control_Integration.

Step 1 Install Packages Using Synaptec Package Manager

  • xmacro
  • sdlmame
  • zsnes

Step 2 Edit LIRC Config Files

Run the following command from console:

nano ~/.lirc/irexec

File Contents:

begin
remote = mceusb
prog = irexec
button = Stop
config = xmacro-wrap extra1
repeat = 0
end
begin
remote = mceusb
prog = irexec
button = Power
config = xmacro-wrap Exit
repeat = 0
end
begin
remote = mceusb
prog = irexec
button = Pause
config = xmacro-wrap Pause
repeat = 0
end
begin
remote = mceusb
prog = irexec
button = Play
config = xmacro-wrap LoadState
repeat = 0
end
begin
remote = mceusb
prog = irexec
button = Record
config = xmacro-wrap SaveState
repeat = 0
end
begin
remote = mceusb
prog = irexec
button = More
config = xmacro-wrap SelectState
repeat = 1
end
begin
remote = mceusb
prog = irexec
button = VolDown
config = xmacro-wrap VolDown
repeat = 1
end
begin
remote = mceusb
prog = irexec
button = VolUp
config = xmacro-wrap VolUp
repeat = 1
end
begin
remote = mceusb
prog = irexec
button = OK
config = xmacro-wrap Return
repeat = 0
end
begin
remote = mceusb
prog = irexec
button = Home
config = xmacro-wrap Menu
repeat = 0
end
begin
remote = mceusb
prog = irexec
button = Up
config = xmacro-wrap Up
repeat = 1
end
begin
remote = mceusb
prog = irexec
button = Down
config = xmacro-wrap Down
repeat = 1
end
begin
remote = mceusb
prog = irexec
button = Left
config = xmacro-wrap Left
repeat = 1
end
begin
remote = mceusb
prog = irexec
button = Right
config = xmacro-wrap Right
repeat = 1
end

Run the following command from console:

nano ~/.lircrc

Add the following line to that file:

include ~/.lirc/irexec

You may need to restart your lirc daemon for changes to take effect.

/etc/init.d/lirc restart

Step 3 Create a macro-wrap Script

OH Wait! Here is one for you already that contains the commands for both zsnes:
run this command from console and paste the text in the box below in

sudo nano /usr/bin/xmacro-wrap

File Contents:

#!/bin/bash
# xmacroplay wrapper for emulators under mythTV

# finds an emulator running and loads its keytable.
# add your emulator below! Use the ‘unused’ key for unused commands
function keyAssign {
# master list of commands
COMMAND=( ‘Exit’ ‘Reset’ ‘SaveState’ ‘LoadState’ ‘PickState’ \
‘IncState’ ‘DecState’ ‘Fastforward’ ‘SlowDown’ ‘Rewind’ \
‘Pause’ ‘Return’ ‘Menu’ ‘VolUp’ ‘VolDown’ \
‘Mute’ ‘Up’ ‘Down’ ‘Left’ ‘Right’ \
‘Extra1′ ‘Extra2′ ‘Extra3′ ‘Extra4′ ‘Extra5′)
let NUMCOMMANDS=${#COMMAND[@]}-1
KEYS=”

####### ########################################################
# Add other emulators below! ##################################################
####### ########################################################

#zsnes configuration
if ps -A | egrep -q ‘ zsnes’; then
# quit reset save_st load_st pick_st
KEYS=( ‘F11′ ‘F10′ ‘F2′ ‘F4′ ‘F3′ \
# st+ st- ffwd slow rwnd
‘unused’ ‘unused’ ‘unused’ ‘unused’ ‘unused’ \
# pause ok menu vol+ vol-
‘p’ ‘Return’ ‘Escape’ ‘F8′ ‘F9′ \
# mute up down left right
‘unused’ ‘Up’ ‘Down’ ‘Left’ ‘Right’ \
# extra1 extra2 extra3 extra4 extra5
‘Escape’ ‘unused’ ‘unused’ ‘unused’ ‘unused’ )
PASSNUMERAL=’true’ #use numbers to change state for fceu
EMUNAME=”ZSNES”
#sdlmame configuration
elif ps -A | egrep -q ‘ sdlmame’; then
# quit reset save_st load_st pick_st
KEYS=( ‘Escape’ ‘F3′ ‘F7′ ‘Shift_L+F7′ ‘unused’ \
# st+ st- ffwd slow rwnd
‘unused’ ‘unused’ ‘unused’ ‘unused’ ‘comma’ \
# pause ok menu vol+ vol-
‘unused’ ‘Return’ ‘Tab’ ‘Right’ ‘Left’ \
# mute up down left right
‘asciitilde’ ‘Up’ ‘Down’ ‘Left’ ‘Right’ \
# extra1 extra2 extra3 extra4 extra5
‘Escape’ ‘unused’ ‘unused’ ‘unused’ ‘unused’ )
PASSNUMERAL=’true’ #use numbers to change state for nestopia
EMUNAME=”SDLMAME”
fi
}

#use KeyStr and hold down for a little bit. $1 is keystr, [$2] is delay
function pressKey {
echo “KeyStrPress $1″ | xmacroplay “:0.0″
local SLEEPTIME=.05 #.05 is a good default hold time for fceu and zsnes
if [ $# == 2 ]; then
SLEEPTIME=$2
fi
sleep $SLEEPTIME
echo “KeyStrRelease $1″ | xmacroplay “:0.0″
debugOutput “$1 ($SLEEPTIME seconds)” 1
}

#output to console/logfile
function debugOutput {
ECHOCOMMAND=”echo”

# use “debugOutput text 1″ or similar to prevent newline
if [ $# == 2 ]; then
ECHOCOMMAND=”echo -n”
fi
if [ $DEBUGMODE != 'none' ]; then
$ECHOCOMMAND “$1″
fi
if [ $DEBUGMODE = 'true' ]; then
$ECHOCOMMAND “$1″ >> ~/.xmacro-wrap.log

#trim log
if [ ! -z "$LOGFILESIZE" ]; then
cat ~/.xmacro-wrap.log | tail -n $LOGFILESIZE > ~/.xmacro-wrap.log
fi
fi
}

#send the key!
function sendData {
KEYSYM=’undefined’

# see if a numeral was entered
# if so, set the key symbol to that number if allowed by settings
# if the numeral is also a command, the numeral will be overwritten later
for i in 0 1 2 3 4 5 6 7 8 9; do
if [ "$1" == $i ] && [ "$PASSNUMERAL" = 'true' ]; then
KEYSYM=$1
fi
done

#find the key we want to send
# look for a match in our definitions
for i in `seq 0 $NUMCOMMANDS`; do
if [ "${COMMAND[i]}” = “$1″ ]; then
KEYSYM=${KEYS[i]}
COMMANDNAME=${COMMAND[i]}
fi
done

#if not legal key
if [ "$KEYSYM" = 'undefined' ] || [ "$KEYSYM" = 'unused' ]; then
debugOutput ” $KEYSYM for $EMUNAME”
exit 1
fi

#otherwise do it!
pressKey $KEYSYM $2
debugOutput ” sent to $EMUNAME”
exit 0
}

# true logs to file and to stdout
# false logs to stdout only
# none is silent
DEBUGMODE=’true’
LOGFILESIZE=’50′ #number of lines, use blank for unlimited

if [ "$#" == "0" ] || [ "$#" -gt "3" ]; then
echo ‘Wrapper for xmacroplay for use with emulators’
echo ”
echo ‘Usage: xmacro-wrap COMMAND [DURATION]‘
echo ‘ or: xmacro-wrap KEYSTR direct [DURATION]‘
echo ”
echo ‘COMMAND is any internally recognized command, for example’
echo ‘ Exit, Reset, SaveState, Mute.’
echo ”
echo ‘DURATION is in seconds.’
echo ”
echo ‘Direct mode is invoked like this: xmacro-wrap backslash direct 1′
echo ‘ (Sends backslash key, held for 1 second)’
echo ‘ see X11/keysymdef.h’
echo ”
exit 1
fi

#direct mode option
if [ "$2" = "direct" ]; then
debugOutput “Direct output: ” 1
pressKey $1 $3
exit 0
fi

DATESTR=”`date +%m/%d/%y` `date +%T`”
keyAssign
# if an emulator isn’t running, KEYS is blank
if [ -z "$KEYS" ]; then
debugOutput “$DATESTR, no emulator to pass command \”$1\”!”
exit 1
else
debugOutput “$DATESTR, command \”$1\”: ” 1
fi
sendData $@

Run the following command in console:

sudo chmod 755 /usr/bin/xmacro-wrap

Step 4 Create Game Launcher Scripts

Run the following command from console:

nano ~/snes.sh

File Contents:

#!/bin/bash
# zsnes path + executable
ZSNES=/usr/bin/zsnes
# irexec path + executable
IREXEC=/usr/bin/irexec
# irexec process name to kill
IREXEC_PS=irexec

$IREXEC &
$ZSNES “$1″
killall $IREXEC_PS
exit 0

Run the following command from console:

nano ~/mame.sh

File Contents:

#!/bin/bash
# zsnes path + executable
MAME=/usr/games/sdlmame
# irexec path + executable
IREXEC=/usr/bin/irexec
# irexec process name to kill
IREXEC_PS=irexec

$IREXEC &
$MAME -skip_gameinfo $1
killall $IREXEC_PS
exit 0

Run the following commands from console:

chmod 755 ~/snes.sh
chmod 755 ~/mame.sh

Step 5 Configuring Mythbuntu Game Players

Configure zsnes keys MISC->Misc Keys:

Start up the Myth Frontend Interface.
Select Utilities / Setup -> Setup -> Media Settings -> Game Settings -> Game Players.
New Game Player
Type: SNES
Command: ~/snes.sh

New Game Player
Type: MAME
Command: ~/mame.sh

Step 6 Problems

This guide requires that you have setup the proper keys in zsnes to match the keys in the xmacro-wrap script.

Make sure that in /etc/mame.ini file the rom-path is set to the path where all of the roms are located or sdlmame may not work.

Enjoy!

**Edit here is an updated set of lirc and shell scripts for running macrowrap!

mythbuntu-macrowrap-lirc

4 Comments

  • By Potter29Dianne, June 6, 2010 @ 5:30 am

    Make your own life time more simple take the credit loans and all you require.

Other Links to this Post

  1. Lars Werner » A Windows user confession: Make LIRC on Ubuntu manage your HTPC + making suspend-to-ram work properly — August 3, 2009 @ 3:45 am

  2. Kylie Batt — April 21, 2010 @ 12:52 am

  3. Kylie Batt — April 21, 2010 @ 12:04 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.