<?xml version="1.0"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [ 
<!ENTITY version "0.01">
]> 
<article>
  <articleinfo>
    <title>
      The mpd and lirc MINI HOWTO
    </title>
    <author>
      <firstname>
         Niklas
      </firstname>
      <surname>
         Hofer
      </surname>
      <affiliation>
        <address format="linespecific">
          <email>
hofer[AT]informatik.hu-berlin.de
          </email>
        </address>
      </affiliation>
    </author>
    <pubdate>
      v0.09, 25 Jun 2003 
    </pubdate>
    <abstract>
      <para>
         This is a more or less detailed guide to configuration
         of the Music Player Daemon (MPD) written by Warren Dukes 
         and LIRC (Linux Infrared Remote Control) by Christoph Bartelmus
        to work together in a home environment, for example as
        a music jukebox.
     </para>
      <para>
         The Author is not a native English speaker so please be
         indulgent. If you find errors or you want to add something,
         please send him a diff to
       <ulink url="mailto:hofer[AT]informatik.hu-berlin.de">
hofer[AT]informatik.hu-berlin.de
       </ulink>
       .
     </para>
    </abstract>
  </articleinfo>

  <sect1 id="introduction">
    <title>
      Introduction 
   </title>
   <sect2>
      <title>
         Reasons
      </title>
       <para>
          You want to use MPD and LIRC together for (at least) one of 
          following reasons:
         <itemizedlist>
           <listitem>
             <para>
                You have got a hole bunch of music you want to listen to in
                a way that doesn't lack neiter comfort nor functionality.
             </para>
           </listitem>
           <listitem>
             <para>
                You have got an old (or even new) PC you don't need anymore
                but your superduper hifi rack is unusable because you spent
                all your CDs to the local orphanage while just keeping the mp3 
                backups
             </para>
           </listitem>
           <listitem>
              <para>
                 ...
             </para>
           </listitem>
         </itemizedlist>
      </para>
   </sect2>
   <sect2>
      <title>
         Updates
      </title>
      <para>
         You may check out
         <ulink url="http://www.informatik.hu-berlin.de/~hofer">
            http://www.informatik.hu-berlin.de/~hofer
         </ulink>
         for the most recent version of this HOWTO. If you find errors or you
         miss a theme, please add it and send the diff to the author.
      </para>
   </sect2>
  </sect1>

  <sect1 id="quick_steps" >
    <title>
      Quick Steps
    </title>
    <para>
      This section is written by 
      <ulink url="mailto:hofer[at]informatik.hu-berlin.de">
         Niklas Hofer (hofer[at]informatik.hu-berlin.de) 
      </ulink>
      (The 
      <emphasis remap="bf">
        latest version 
      </emphasis>
      of this document is at 
      <ulink url="http://musicpd.sourceforge.net">
         "http://musicpd.sourceforge.net" 
      </ulink>
      . You may want to check there for changes). Mirror sites are at - 
      <ulink url="http://www.informatik.hu-berlin.de/~hofer">
         "http://www.informatik.hu-berlin.de/~hofer" 
      </ulink>
      .
    </para>
    <sect2 id="precautions">
      <title>
        Precautionary Preparations 
      </title>
      <para>
        There are only some small Precautions:
        <itemizedlist>
           <listitem>
             <para>
              save your old config files
           </para>
           </listitem>
           <listitem>
             <para>
              prepare your neighbors for some future sleepless nights
           </para>
           </listitem>
           <listitem>
             <para>
              ask your mum/girlfriend/wife for sanction and wishes!
           </para>
           </listitem>
        </itemizedlist>
      </para>
    </sect2>
    <sect2 id="impatient">
      <title>
        For the Impatient 
      </title>
      <para>
        <itemizedlist>
          <listitem>
             <para>
                Create yourself an infrared receiver (hardware)
            </para>
          </listitem>
          <listitem>
            <para>
              and don't forget to 
              <emphasis remap="bf">
                 connect
              </emphasis>
              it (happened to me)
            </para>
          </listitem>
          <listitem>
             <para>
                Find a unused remote control.
            </para>
          </listitem>
          <listitem>
            <para>
               Download, configure and install LIRC
            </para>
          </listitem>
          <listitem>
             <para>
                Download, install and configure MPD
            </para>
          </listitem>
          <listitem>
            <para>
               Edit the LIRC config files as you need 
               (this is the thing this Howto is all about)
            </para>
          </listitem>
          <listitem>
             <para>
                Startup LIRC and MPD and have fun using the 
                remote control
            </para>
          </listitem>
        </itemizedlist>
      </para>
      <para>
         If you can do all that by yourself, you don't need this Howto.
      </para>
      <para>
         See details of above steps in the following sections.... 
      </para>
   </sect2>
</sect1>

<sect1 id="irhardware">
   <title>
      Infrared Hardware
   </title>
     <sect2 id="receiver">
        <title>
           Creating the infrared receiver
        </title>
        <para>
           Please visit 
           <ulink url="http://www.lirc.org/receivers.html">
              http://www.lirc.org/receivers.html
           </ulink>
           and
           <ulink url="http://www.manoweb.com/alesan/lirc/cocoon/">
              http://www.manoweb.com/alesan/lirc/cocoon/
           </ulink>

           for a detailed descripton for building an infrared receiver device.
           In Germany, you can get all the parts needed by CONRAD, a market 
           dedicated to elektronic wares. The part IDs will be contained in the
           next revision of this HOWTO, if I find them.
        </para>
     </sect2>
     <sect2 id="getting_remotecontrol">
        <title>
           Remote control device
        </title>
        <para>
            Find an old VRC remote control (grab the one from your parents, 
            they don't need it anyway). A TV remote works, too, but we want to
            use the Play, Stop, Pause keys for playback control. 
            Buying a universal remote control that combines multiple commom 
            remotes is possibility, too. Have a look at eBay.
        </para>
     </sect2>
  </sect1>

  <sect1 id="lirc">
    <title>
       LIRC setup
    </title>
    <para>
       LIRC consists of two parts:
       <itemizedlist>
          <listitem>
             <para>
                a kernel module that has to match your running kernel 
             </para>
          </listitem>
          <listitem>
             <para>
                a userspace deamon called lircd that decodes infrared signals
                and provides them on a Unix domain socket
             </para>
          </listitem>
       </itemizedlist>
       Please look at the LIRC homepage or in the documentation delivered
       by the source package for a more detailed description.
    </para>
       
       <sect2 id="lirc_installation">
          <title>
             Installation
          </title>
             <para>
               <orderedlist inheritnum="ignore" continuation="restarts">
                  <listitem>
                     <para>
                         First, go to 
                         <ulink url="http://www.lirc.org">
                            http://www.lirc.org
                         </ulink>
                         and download the recent version of lirc. 
                         Alternatively you can install the lirc package provided
                         by your distribution. I suggest to do it manually, 
                         because on my system the compilation of the kernel 
                         module with the destribution given packages didn't work
                         (maybe my fault).
                      </para>
                   </listitem>
                  <listitem>
                     <para>
                        Unpack the archive and follow its INSTALL instructions.
                        <programlisting format="linespecific">
    $ tar xvfz lirc-version.tar.gz
    $ cd lirc-version
    $ less INSTALL
                        </programlisting>
                        If you get problems, please read all the output the
                        install script produces, it may contain information
                        to help  solving them.  Notice to set the correct
                        options for the hardware you built (serial ports, 
                        adresses etc). You will need the sources of your 
                        currently running kernel (or the kernel you want to use 
                        with lirc) in /usr/src/linux or it must be a symbolic 
                        link to the regarding directory. I suggest you download
                        the current kernel source from
                         <ulink url="http://www.kernel.org">
                            http://www.kernel.org
                         </ulink>
                         and read the Kernel-HOWTO by Al Dev if you never built
                         a kernel by yourself.
                      </para>
                   </listitem>
                </orderedlist>
                Finished with that you should have installed a kernel module
                (ie. lirc-serial) , the LIRC deamon (often in /usr/sbin/lircd)
                and the lirc configuration (/etc/lirc). The directories may 
                vary, but the install script is very verbose and should have
                told you all the relevant information.
             </para>
          </sect2>
       <sect2 id="lirc_configuration">
          <title>
             LIRC Configuration
          </title>
          <para>
             Use your favourite editor the edit the files in /etc/lirc
               <orderedlist inheritnum="ignore" continuation="restarts">
                  <listitem>
                     <para>
                        <emphasis remap="bf">
                           /etc/lirc/hardware.conf 
                        </emphasis>
                        should look this way (depending 
                        on your hardware setup. This is for a receiver connected
                        to the serial port.
                        <programlisting format="linespecific">
                           LIRCD_ARGS=""

                           START_LIRCMD=false

                           #Try to load appropriate kernel modules
                           LOAD_MODULES=true

                           DRIVER="default"
                           DEVICE="/dev/lirc"
                           MODULES="lirc_serial"
                        </programlisting>
                      </para>
                   </listitem>
                  <listitem>
                     <para>
                        The file 
                        <emphasis remap="bf">
                           /etc/lirc/lirc-modules-source.conf
                        </emphasis>
                        was created by
                        the lirc configuration script and should match the 
                        values you entered. Again, here is an example for a 
                        serial connected device. So only the SERIAL section 
                        contains useful data.
                        <programlisting format="linespecific">
                           # Coma separated list of lirc kernel drivers to build
                           LIRC_MODULES="serial"

                           # Serial module configuration (these may vary!)
                           LIRC_SERIAL_PORT="0x3f8"
                           LIRC_SERIAL_IRQ="4"
                           LIRC_SERIAL_CFLAGS=" -DLIRC_SERIAL_SOFTCARRIER"

                           # Sir module configuration
                           LIRC_SIR_PORT="UNCONFIGURED"
                           LIRC_SIR_IRQ="UNCONFIGURED"
                           LIRC_SIR_CFLAGS="UNCONFIGURED"

                           # Parallel module configuration
                           LIRC_PARALLEL_PORT="UNCONFIGURED"
                           LIRC_PARALLEL_IRQ="UNCONFIGURED"
                           LIRC_PARALLEL_TIMER="UNCONFIGURED"
                        </programlisting>
                      </para>
                   </listitem>
                </orderedlist>
          </para>
       </sect2>
       <sect2 id="lirc_test">
          <title>
             Testing the hardware configuration
          </title>
          <para>
             Plug in your receiver, run lircd by typing (as root)
             <programlisting format="linespecific">
   # /etc/init.d/lirc start
             </programlisting>
             ,run 
             <emphasis remap="bf">
                mode2 
             </emphasis>
             delivered by the lirc package 
             (don't confuse with mode3), grab your remote control
             and start typing satanic patterns on your remote 
             while pointing it to the receiver. LIRC must not run!
             You should see output like this:
             <programlisting format="linespecific">
                pulse 93
                space 4965
                pulse 108
                space 4969
                pulse 93
                space 7496
                pulse 93
                space 7489
                pulse 93
                space 47915
                pulse 138
                space 7475
                pulse 93
                space 7494
                pulse 93
             </programlisting>
             If not, please read
             <ulink url="http://www.lirc.org/html/install.html#testing">
                http://www.lirc.org/html/install.html
             </ulink>
             . If yes, then be happy and continue with the next paragraph.
          </para>
       </sect2>
       <sect2 id="remote_control_configuration">
          <title>
             Remote control configuration
          </title>
          <para>
             Here comes the first tricky part.
             If you got a common remote control (not universal) the go to
             <ulink url="http://lirc.sourceforge.net/remotes/">
                http://lirc.sourceforge.net/remotes/
             </ulink> 
             and get the config file for your remote. If your device is
             not listed there, you have to create a file for yourself.
             Don't worry, its very easy. Maybe more easy than seeking
             for the correct file in the archive. Please read the irrecord
             manpage.
          </para>
          <para>
             If you have got a well documented universal remote control
             you can switch it exactly to a remote control you can find
             in the above archive. In the case you ain't got a good 
             documentation ("Press 123 for getting THE Phillips remote")
             you have to try a bit. Or simply select your favourite 
             manufacturer at the remote and create a config file yourself.
             Note: All the keys to get use of the best way this is, young jedi!
          </para>
          <para>
             After all, you should have a config file for your remote. It
             is yours. Your own. Your precioussss. Hide it in the mountains
             and keep it at a safe place. Copy it to 
             /etc/lirc/lircd.conf. A broad outline:
             <programlisting format="linespecific">
         # this config file was automatically generated
         # using lirc-0.6.4-CVS(hauppauge) on Sat May 26 21:15:45 2001
         #
         # contributed by G.P.M. van Dijnsen
         #
         # brand:             Philips
         # model:             RC 5260
         # supported devices: Unknown TV set and unknown VCR ( suspected pre 1987 )
         #

         begin remote

           name  Philips_5260
           bits            6
           flags RC5|CONST_LENGTH
           eps            30
           aeps          100

           one           889  889
           zero          889  889
           plead         889
           pre_data_bits   7
           pre_data       0x40
           gap          113792
           toggle_bit      2


               begin codes
                   1                        0x0000000000000001
                   2                        0x0000000000000002
                   3                        0x0000000000000003
                   4                        0x0000000000000004
                   5                        0x0000000000000005
                   6                        0x0000000000000006
                   7                        0x0000000000000007
                   8                        0x0000000000000008
                   9                        0x0000000000000009
                   0                        0x0000000000000000
                   1-                       0x000000000000000A
                   2-                       0x000000000000000B
                   brightness_down          0x0000000000000013
                   brightness_up            0x0000000000000012
                   contrast_down            0x0000000000000015
                   contrast_up              0x0000000000000014
                   volume_down              0x0000000000000011
                   volume_up                0x0000000000000010
                   trebble_down             0x0000000000000019
                   trebble_up               0x0000000000000018
                   bass_down                0x0000000000000017
                   bass_up                  0x0000000000000016
                   show_settings            0x000000000000000F
                   green                    0x000000000000000E
                   mute                     0x000000000000000D
                   skip_forward             0x0000000000000022
                   power                    0x000000000000000C
                   txt_off                  0x000000000000003F
                   txt_on                   0x000000000000003C
                   txt_clock                0x000000000000002A
                   txt_half                 0x000000000000002E
                   txt_question             0x000000000000002C
                   txt_cross                0x000000000000002D
                   txt_hold                 0x0000000000000029
                   txt_enlarg               0x000000000000002B
                   bell_on                  0x000000000000003B
                   bell_off                 0x000000000000002F
                   record                   0x0000000000000037
                   back                     0x0000000000000032
                   pause                    0x0000000000000036
                   play                     0x0000000000000035
                   forward                  0x0000000000000034
                   arrows                   0x0000000000000033
                   xmark                    0x0000000000000031
               end codes

         end remote
             </programlisting>
             For testing you now should restart LIRC, run 
             <emphasis remap="bf">irw</emphasis> and type satanic pattrerns, 
             again. The output should look like this:
             <programlisting format="linespecific">
                0000000000001010 00 volume_up Philips_5260
                0000000000001011 00 volume_down Philips_5260
                000000000000100d 00 mute Philips_5260
                000000000000103f 01 txt_off Philips_5260
                000000000000100d 00 mute Philips_5260
             </programlisting>
             You see a code (like morse, but ir) you may ignor, the key name
             and the remote's name. This is a good way to procure the key
             mapping of your configuration.
          </para>
       </sect2>
  </sect1>

  <sect1 id="mpd">
    <title>
       MPD setup
    </title>
    <para>
       Please download the package from
       <ulink url="http://musicpd.sourceforge.net">
          http://musicpd.sourceforge.net 
       </ulink>
       or checkout the cvs like described there.
       Consult the INSTALL file to find alle the neccassary information
       to build and install your very own MPD.
    </para>
  </sect1>

  <sect1 id="lirc-conf">
    <title>
       LIRC - MPD interaction
    </title>
     <sect2 id="mpc">
        <title>
           mpc
        </title>
       <para>
          You know should have a running lirc that listens to your remote
          control and an MPD accessible by telnet or by the webinterface
          phpMp. 
       </para>
       <para>
          Now we have to let both interact with each other. Since the 
          remote control has an one-way interface we only have to associate
          the keypresses on the remote to MPD functions. It's as easy as
          clever: in the lirc package there is an executable named irexec,
          which executes prearranged executables when hitting keys. And this is
          exactly the feature we want. But which executable do we want to 
          launch? Simply passing the command to telnet would be a way.
          I wrote a small perl script (mpc for MP client), which does exactly this
          and can be modified in a very simple way (for everyone having basic perl
          knowledge). Here it is:
          <programlisting format="linespecific">
             #!/usr/bin/perl

             # mpc
             # by Niklas Hofer , alzhaimer at gmx.net
             # connects to mpd and sends given command
             # can't get feedback yet - please contact me if you know a solution
             
             # Settings | please modify
             $host='127.0.0.1';
             $port='2323'; 

             # you don't have to set this if telnet is in PATH
             $mytelnet='/usr/bin/telnet';

             ## the fun begins here


             # using telnet is faster than using Net::Socket
             # not using strict for faster execution

             chomp ($telnet=`which telnet`);
             $telnet=$telnet ? $telnet : $mytelnet;

             $msg=join(' ', @ARGV );

             if ($msg) {
                open TN,"|$telnet $host $port" || die qq~can't pipe to $telnet: $!~;
                print TN $msg."\n";
                close TN;
             }
          </programlisting>
       </para>
     </sect2>
     <sect2 id="mpc_playlist">
        <title>
           mpc_playlist
        </title>
       <para>
          And I wrote another scipt - named mpc_playlist - that loads all
          playlists beginning with a specified number, letter or string.
          So you can press ie. the '1' key on your remote to add rock songs
          to the playlist, if you named the rock lists 
          <emphasis remap="bf">1_metallica</emphasis> or
          <emphasis remap="bf">1_sepultura</emphasis> and so on, accoordingly
          to other numbers.
          <programlisting format="linespecific">
             #!/usr/bin/perl


             # mpc_playlist
             # by Niklas Hofer , alzhaimer at gmx.net
             # uses mpc to add playlists beginning with special char

             # Settings | please modify
             $playlistdir ='/data/playlists/' # has to end with slash (lazyness, speed)


             ## the fun begins here
             $first=shift || die "you must specify some of the first chars of a playlist\n";
             @files=sort <$playlistsdir/*.m3u>;
             foreach (@files) {
                  chomp;
                  s/^.+\///;
                  if (/^$first/) {
                         s/\.m3u$//;
                         system("mpc load $_");
                   }
             }
          </programlisting>
          Don't forget to place mpc and mpc_playlist in your 
          path - copy them to /usr/bin/ or /usr/local/bin. 
       </para>
     </sect2>

     <sect2 id="irexec">
        <title>
           irexec
        </title>
        <para>
          The config file of irexec is
          <emphasis remap="bf">
            /etc/lirc/lirc.lircrc
          </emphasis>
          . Here is an example:
          
          <programlisting format="linespecific">
            # using aumix to control volume
            begin
                    remote = Philips_5260
                    # replace this by the name of your remote control
                    
                    button = volume_up
                    # this is the name of the key (get it by irw)
                    
                    prog   = irexec
                    # irexec execs any desired executable, 
                    # specified by config (see below)
                    
                    repeat = 1
                    # is this key repeatable (bad for mpc commands)
                    
                    config = aumix -w +3 -v +3;aumix -S
                    # and here is the exec to run
                    # if you want multiple commands run in a row, join them
                    # with semicola

                    # if you want several alternating commands 
                    # simply add a new config line (and set repeat=0)
            end

            begin
                    remote = Philips_5260
                    button = volume_down
                    prog   = irexec
                    repeat = 1
                    config = aumix -w -4 -v -4;aumix -S
            end

            # mute (sound on/off)
            begin
                    remote = Philips_5260
                    button = mute
                    prog   = irexec
                    repeat = 0
                    config = aumix -S;aumix -w 0 -v 0
                    config = aumix -L
            end

            # open/close CDROM
            begin
                    remote = Philips_5260
                    button = txt_cross
                    prog   = irexec
                    repeat = 0
                    config = eject /cdrom
                    config = eject -t /cdrom
            end


            ########################################
            # mpd control
            ########################################
            begin
                    remote = Philips_5260
                    button = txt_hold
                    prog   = irexec
                    repeat = 0
                    config = mpc stop
            end
            begin
                    remote = Philips_5260
                    button = txt_clock
                    prog   = irexec
                    repeat = 0
                    config = mpc next
            end
            begin
                    remote = Philips_5260
                    button = txt_half
                    prog   = irexec
                    repeat = 0
                    config = mpc previous
            end
            begin
                    remote = Philips_5260
                    button = txt_enlarg
                    prog   = irexec
                    repeat = 0
                    config = mpc play 0
            end
            begin
                    remote = Philips_5260
                    button = green
                    prog   = irexec
                    repeat = 0
                    config = mpc pause
            end
            begin
                    remote = Philips_5260
                    button = txt_question
                    prog   = irexec
                    repeat = 0
                    config = mpc shuffle
            end

            ########################################
            # playlist loading
            ########################################
            begin
                    remote = Philips_5260
                    button = 1
                    prog   = irexec
                    repeat = 0
                    config = mpc_playlist 1
            end
            begin
                    remote = Philips_5260
                    button = 2
                    prog   = irexec
                    repeat = 0
                    config = mpc_playlist 2
            end
            begin
                    remote = Philips_5260
                    button = 3
                    prog   = irexec
                    repeat = 0
                    config = mpc_playlist 3
            end
            # ....
            begin
                    remote = Philips_5260
                    button = show_settings
                    prog   = irexec
                    repeat = 0
                    config = mpc clear
            end
        </programlisting>
        </para>
     </sect2>
  </sect1>

  <sect1 id="startup">
    <title>
       Startup
    </title>
    <para>
       <programlisting format="linespecific">
          /etc/init.d/lircd start
          mpd /your/config/file
       </programlisting>
       From now on you can use the web interface and your remote to 
       control your music.
    </para>
  </sect1>

</article>
