Monday, March 23, 2009

How to use LIRC with Amarok 2, Exaile, and other media players in Ubuntu

It has been a while since I updated. School and life in general have kept me busy, and I haven't had many adventures in Linux, but today I had to grapple with a new problem.

Amarok 2 was released last December and after I first tried the beta, I really didn't like it. Amarok 1.4 was my music player of choice, you see, but the times, they are a changin'. In the upcoming release of Ubuntu (Jaunty Jackelope, 9.04) Amarok is due to be replaced by version 2. I decided to try out Amarok 2 again, figuring I will either have to get used to it, or find myself a new player.

Truth is, I still hate Amarok 2. And while I could go into details on that, it is not the reason I am here. When I was first trying to learn to love Amarok 2, I spent a while trying to fix one of my main annoyances with it: lack of support with LIRC (Linux Infrared Remote Control.) In 1.4, the DCOP server made things extremely easy, but it no longer exists in Amarok 2, and so you have to get creative. Edit: *As pointed out in the comments for this post, DCOP was actually replaced with DBus, which I managed to miss somehow. I admittedly don't know much about DBus myself, but if you are interested in getting LIRC working with Amarok 2, you will save yourself some effort looking into DBus. However, this guide still applies to Exaile and any other media players that don't specifically offer remote control support. Thanks to nhnFreespirit for pointing this out!*

As I found out, this guide will also work for other music players. In addition to an already set up LIRC daemon and remote control, you will need an audio player with either global hotkey support (like Amarok 2) or a media player that can take advantage of the keyboard shortcuts (the default way to use media buttons on a keyboard) in Gnome. You might also need a keyboard with media buttons, but I haven't tried it without them, so it may work (if it does, you should let me know!) These instructions are taken from three or four other resources that, in my exhaustive search, I lost and so can't give proper credit, but thanks to those who paved the way.

Step 1: Global Hotkeys
The first thing you need to do is set your global hotkeys to your media buttons. If you are using Amarok 2, this needs to be done within Amarok (go to the configure shortcuts dialogue, and set your global hotkeys for play/pause, stop, next track, and previous track to the corresponding media buttons on your keyboard. If you are using a Gnome/GTK+ music player, you should be able to set these under Preferences -> Keyboard Shortcuts. In either case, you should now be able to control the music with these media buttons.

Step 2: Set LIRC to use acpi scripts
There are scripts in /etc/acpi/ that will actually control these multimedia button functions, and if you tell LIRC to use them, you can get your remote to tell the computer that you actually hit that button on the keyboard. The associated scripts are are called playbtn.sh, stopbtn.sh, nextbtn.sh, and prevbtn.sh. You can use irexec to run these scripts by setting them up in the ~/.lircrc file. For example, I have included the play button and stop button from my .lircrc file:
begin
prog = irexec
remote = AtiRW
button = play
config = /etc/acpi/playbtn.sh
end

begin
prog = irexec
remote = AtiRW
button = stop
config = /etc/acpi/stopbtn.sh
end
*Note, your "remote =" and "button=" will vary depending on how you set up your LIRC daemon. In my example, I named my remote "AtiRW", my play button "play" and my stop button "stop". These values come from your lircd.conf file when you set it up.

Set all of the buttons you want. You can even set up the vol+/vol-/mute buttons on your keyboard if you have them to change the master volume, though I prefer to use aumix or amixer to do that because there is no OSD.

When you are done setting up your keys in ~/.lircrc, save the file and close it. Then run irexec as root in the terminal with "sudo irexec" (It should have been installed with LIRC, but if it hasn't, you can install it with apt-get first.) If you have set up everything correctly, at this point you can press the buttons on your remote and it should cause the music player to function accordingly. If it doesn't work, make sure A) You set up LIRC correctly (I didn't cover that here), B) LIRC is currently running, C)You ran irexec AS ROOT (you will get an error otherwise), and D) Your media buttons on your keyboard work.

Step 3: Get irexec to run at boot
For me, irexec (the program that tells other programs/scripts to run based on LIRC input) was automatically told to run at start up with LIRC. However, even if that is the case for you too, to run these scripts irexec has to be running as root so you will have to do this next step either way. For whatever reason, and the sources I found on the internet seem to confirm it, the usual methods for starting irexec always seem to result in them being run as a normal user. We are going to have to get around that.

It would be a good time to note that, for good reason, running programs as root should be done with the utmost caution, and the only reason we are running irexec as root is because running the acpi scripts have to be run as root. (I even tried copying them to my home directory and changing the permissions/ownership with no luck.) Anybody with an alternative method that does not require irexec to be run as root should leave a comment on how to do it and I will update accordingly.

The only way I managed to get irexec to run as root was found here thanks to Ubuntu forums user Kipee. They suggest adding it to crontab. Edit crontab with:
sudo crontab -e
You may be asked which editor to use, if so just select whichever one you are most comfortable with. Add a new line and insert the following, replacing USER with your username:
@reboot sleep 30 && export DISPLAY=:0 &&/usr/bin/irexec -d /home/USER/.lircrc

"@reboot" tells it to run when the machine is booted. "sleep 30" causes irexec to wait 30 seconds before starting, and is only to make sure that the LIRC daemon is running before trying to run irexec. (Note: in Kipee's post, they use 120, but two whole minutes seems longer than necessary in my experience). Close out of crontab (ctrl+x if you are editing with nano) and make sure to save it. Now reboot. When the machine reboots, irexec should now start running (remember, after 30 seconds), and when you open up your favorite media player, it should respond to remote control input.

Other Considerations
These directions assume you have multimedia buttons on your keyboard, and I am not entirely sure how one could achieve the same effect without those multimedia buttons. (If you try running the acpi scripts without assigning them to a key, they don't work!) If you are using a player with configurable shortcut keys (like Amarok 2), I would imagine it would be possible, though I haven't tried it, to use irxevent to fake keypresses in that program (for example, set ctrl+p to play in Amarok, set the play button on your remote to sent "ctrl+p" to Amarok.) Edit: *as mentioned previously, for Amarok 2 use DBus instead.* This would also bypass the need to run irexec as root (or in fact, at all.) You Alternatively, the scripts that run the multimedia keys are tied to key numbers given in /usr/share/acpi-support/key-constraints, and it *might* be possible to use those even without multimedia keys, though how you would do that is beyond me, and if you attempt it you do so at your own risk. Any other solutions to this problem? Feel free to leave some comments.

And in case you were wondering, I switched to Exaile, and now that I've bypassed the hurdle of being able to use my remote, I am loving it.

7 comments:

nhnFreespirit said...

"In 1.4, the DCOP server made things extremely easy, but it no longer exists in Amarok 2, and so you have to get creative."

Amarok 2 uses DBus instead of DCOP. The advantage of DBus is that it is not KDE specific but used by other desktops as well. Furthermore, the Amarok 2 DBus interface follows the MPRIS specification, meaning that any script that uses DBus to control Amarok 2 will also work for other MPRIS enabled players, such as XMMS.

Other than that, using DBus is almost the same as using DCOP. Try running "qdbus org.mpris.amarok /Player" for a list of playback control functions and "qdbus org.mpris.amarok /Player Play" to start playing, for instance.

All in all the move from DCOP to DBus is a big plus, and asking on our forum, mailing list or irc channel would have pointed you to DBus and saved you ton of trouble.

Swinky said...

Wow, thanks for the info, nhnFreespirit! In my searches I did come across mentions of DBus, but I somehow seemed to miss anything more specific about it. I will update my post accordingly.

Though as I stated in my post, I can't say that I am a fan of Amarok 2 (for reasons other than my perceived lack of remote control support). I considered switching to Exaile for a long time, but the lack of remote support for it turned me away. Ironically, figuring out the extremely roundabout way to get remote support in Amarok 2 made me figure out how to get remote support in Exaile.

The MPRIS specification definitely looks pretty neat, and I am glad to see an effort to make a more universal remote control option for media players on Linux.

nhnFreespirit said...

"Though as I stated in my post, I can't say that I am a fan of Amarok 2 (for reasons other than my perceived lack of remote control support)"

That is fair. And I agree that 2.0.0 and still 2.0.2 is a bit rough.

We will be releasing the first beta of 2.1.0 in a few days, and this release is really miles ahead of 2.0.x in my opinion, so perhaps you will have more luck with that. You can check out some of the major work that has gone into the playlist for the new release on my blog here: http://amarok.kde.org/blog/archives/876-Playlist-Layout-Editor.html

Swinky said...

I have to say that the lack of customization in the playlist layout was my absolute biggest complaint in 2.0, so I am really glad to see it getting fixed! I'll definitely have to give 2.1 a try. Thanks again for stopping by my blog, and setting me straight with the DBus thing.

nhnFreespirit said...

*tips hat*

Wicher Minnaard said...

There is a LIRC plugin for Exaile available now at my blog. Using the plugin instead of going the irexec route saves a bunch of forks and syscalls and makes for more predictable behaviour wrt latencies.
Since this is the first hit on Google for 'lirc exaile' I thought it would be a good idea to plug the plugin here.

Money Talks said...

Nice post. Great blog. Thanks for the share. It was very interesting and informative.
Super Bowl 2012| Super Bowl commercials 2012| Money Talks News|