Sunday, June 30, 2013

AIM Media Center Remote Control RC118 with Linux and XBMC

Bought myself a new remote control from Maplin yesterday for my media centre running XBMC on Linux (Debian Testing). It's an AIM Media Center Remote Control, model RC118 and only costs £20.


It's nicely made for the price, the only cons are that it's got a Windows logo on one button (uck!) and unlike my old remote (also from Maplin) not all the buttons work 'out of the box' on Linux & XBMC.

The 'pro' of this remote however is that it's a 'standard' MCE RC6 remote so the buttons can be translated and mapped with lirc. I couldn't do this with my old remote as it emulates a keyboard device.

For a relatively popular remote, surprisingly little information can be found on the web on how to set it up properly with lirc and XBMC, just the usual array of forum posts and incomplete or non-working advice. So some reading was required of the lirc & XBMC's Lircmap.xml documentation.

So here's the solution, if you discount creating the map files (as I've supplied them here) it's actually very simple. I've tested this on Debian (Testing) & Fedora 18 so I assume this would work just as well on derivatives such as Ubuntu/Mint and CentOS/RHEL respectively.
 

So first, install lirc...

Either use your preferred software installer GUI or save time by opening a terminal & using the command line;

On Debian & Ubuntu

 sudo apt-get install lirc  

On Fedora

 sudo yum install lirc  


Configure lirc

The lirc daemon is configured with the file /etc/lirc/lircd.conf.
 In the terminal, you can open this file for editing in gedit (or your preferred text editor);
 sudo gedit /etc/lirc/lircd.conf  
Append the following text your lircd.conf.
 begin remote  
  name rc118  
  bits      13  
  flags RC6|CONST_LENGTH  
  eps      30  
  aeps     100  
  header    2700  855  
  one      486  419  
  zero     486  419  
  pre_data_bits  24  
  pre_data    0x1BFF83  
  gap     107341  
  min_repeat   1  
  toggle_bit_mask 0x8000  
  rc6_mask  0x100000000  
    begin codes  
      Power          0x1BF3  
      Record          0x1BE8  
      Stop           0x1BE6  
      Pause          0x1BE7  
      Rewind          0x1BEA  
      Forward         0x1BEB  
      Previous         0x1BE4  
      Play           0x1BE9  
      Next           0x1BE5  
      Zoomout         0x1BD9  
      Zoomin          0x1BDA  
      Eject          0x1BB7  
      Select          0x1BDB  
      Menu           0x1BF2  
      Back           0x1BDC  
      Info           0x1BF0  
      Up            0x1BE1  
      Down           0x1BE0  
      Left           0x1BDF  
      Right          0x1BDE  
      OK            0x1BDD  
      Vol+           0x1BEF  
      Vol-           0x1BEE  
      Chan+          0x1BED  
      Chan-          0x1BEC  
      Mute           0x1BF1  
      PICTURES         0x1BB6  
      VIDEOS          0x1BB5  
      MUSIC          0x1BB8  
      1            0x1BFE  
      2            0x1BFD  
      3            0x1BFC  
      4            0x1BFB  
      5            0x1BFA  
      6            0x1BF9  
      7            0x1BF8  
      8            0x1BF7  
      9            0x1BF6  
      0            0x1BFF  
      Star           0x1BE2  
      Hash           0x1BE3  
      Clear          0x1BF5  
      Keyboard         0x1BA5  
      Edit           0x1BF4  
      Red           0x1BA4  
      Green          0x1BA3  
      Yellow          0x1BA2  
      Blue           0x1BA1  
    end codes  
 end remote  

On Debian...

The Debian package of lirc comes with a /etc/lirc/hardware.conf file, the Fedora install does not and just works without one. On Debian I had to also amend the hardware.conf file by amending the DRIVER & DEVICE variables to...
 DRIVER="default"  
 DEVICE="/dev/lirc0"  
This is likely to be the same for most installations but if in doubt of what your device is, look in /dev;
 ls -l /dev/lirc*  

Restart lirc

 sudo service lirc restart  

Configure XBMC

XBMC already comes with a set of key maps for remote controls via lirc in the file /usr/share/xbmc/system/Lircmap.xml. You can also have a user specific Lircmap.xml in your ~/.xbmc/userdata directory.
Here we'll put our mappings for our 'rc118' lirc remote (this name being defined in our /etc/lirc/lircd.conf file) to our user Lircmap.xml file.

In a terminal, open a new file in gedit (or your preferred text editor);
 gedit ~/.xbmc/userdata/Lircmap.xml  
Paste the following text into your new file;
 <lircmap>  
   <remote device="rc118">  
     <mypictures>PICTURES</mypictures>  
     <liveradio>RADIO</liveradio>  
     <myvideo>VIDEOS</myvideo>  
     <mymusic>MUSIC</mymusic>  
     <record>Record</record>  
     <pause>Pause</pause>  
     <stop>Stop</stop>  
     <skipminus>Previous</skipminus>  
     <play>Play</play>  
     <skipplus>Next</skipplus>  
     <reverse>Rewind</reverse>  
     <forward>Forward</forward>  
     <start>Menu</start>  
     <back>Back</back>  
     <info>Info</info>  
     <volumeplus>Vol+</volumeplus>  
     <volumeminus>Vol-</volumeminus>  
     <left>Left</left>  
     <right>Right</right>  
     <up>Up</up>  
     <down>Down</down>  
     <select>OK</select>  
     <channelplus>Chan+</channelplus>  
     <channelminus>Chan-</channelminus>  
     <mute>Mute</mute>  
     <recordedtv>Eject</recordedtv>  
     <guide>Zoomout</guide>  
     <livetv>Zoomin</livetv>  
     <menu>Select</menu>  
     <one>1</one>  
     <two>2</two>  
     <three>3</three>  
     <four>4</four>  
     <five>5</five>  
     <six>6</six>  
     <seven>7</seven>  
     <eight>8</eight>  
     <nine>9</nine>  
     <star>Star</star>  
     <zero>0</zero>  
     <hash>Hash</hash>  
     <clear>Clear</clear>  
     <teletext>Keyboard</teletext>  
     <enter>Edit</enter>  
     <red>Red</red>  
     <green>Green</green>  
     <yellow>Yellow</yellow>  
     <blue>Blue</blue>  
   </remote>  
 </lircmap>  
Save your new file.

Disable the remote's kernel driver

Now the RC118 remote is handled by lirc you have no need for the Linux kernel to handle it. To prevent XBMC from receiving input from both lirc and xinput (resulting in double key presses for those buttons that did work out of the box) you need to disable the relevant kernel module.

Unload the RC6 decoder driver with the following command;
 sudo modprobe -r ir_rc6_decoder  
To disable this driver so it's not loaded when the system is booted, blacklist it as follows;
 echo "blacklist ir_rc6_decoder" | sudo tee -a /etc/modprobe.d/ir_rc6_decoder.conf  


Now start or restart XBMC and your AIM RC118 remote works completely.

Saturday, April 20, 2013

Tidying up df's line wraps on HP-UX

When running the df command on a HP-UX system, whenever a device in the first column has a long name the line will wrap. This initially makes the command unreliable if you're writing a script that reports on filesystem free space for example.

 $ df -lkP  
 Filesystem     1024-blocks Used Available Capacity Mounted on  
 /dev/vgrs710rsrid2/lvoldata2  
            858283043 676273843 182009200  79%  /data2  
 /dev/vgrs710rsrid2/lvoldata  
            199398672 118367215 81031457  60%  /data  
 /dev/vgrs710jnl/lvoljnl  
            152256633 77300832 74955801  51%  /journals  
 /dev/vgmsa2312t3/lvolmsadata3  
            1857692665 428977413 1428715252  24%  /data3  
 /dev/vgmsa2312t4/lvolmsadata4  
            1717106748 439950342 1277156406  26%  /data6  
 /dev/vgmsa2312t5/lvolmsadata5  
            1947765811 1871078336 76687475  97%  /data5  
 /dev/vg00/lvol4    1041520  93368  948152   9%  /home  
 /dev/vg00/lvol5    10482360 8147296 2335064  78%  /opt  
 /dev/vg00/lvol6    2088120  936752 1151368  45%  /tmp  
 /dev/vg00/lvol7    20934792 16264528 4670264  78%  /usr  
 /dev/vg00/lvol9    9831597  19038 9812559   1%  /var/opt/ignite/depots  
 /dev/vg00/lvol8    10422528 1068448 9354080  11%  /var  
 /dev/vg00/lvol1    522448  279624  242824  54%  /stand  
 /dev/vg00/lvol3    1044448  515952  528496  50%  /  

This can be fixed by piping the output through our old friend awk as follows;
 $ df -lkP | awk '{  
          if ( NR == 1 ) { next }  
          if ( NF == 6 ) { print }  
          if ( NF == 5 ) { next }  
          if ( NF == 1 ) {  
                getline record;  
                $0 = $0 record  
                print $0  
                  }  
          }'  

This bit of awk firstly discards the first header line from df and then in the case where a line has just 1 text field the next line is appended to it and then printed, any lines with 5 fields are also discarded.
This then produces a consistent df output;
 $ df -lkP | awk '{  
          if ( NR == 1 ) { next }  
          if ( NF == 6 ) { print }  
          if ( NF == 5 ) { next }  
          if ( NF == 1 ) {  
                getline record;  
                $0 = $0 record  
                print $0  
                  }  
          }'  
 /dev/vgrs710rsrid2/lvoldata2           858283043 676273843 182009200  79%  /data2  
 /dev/vgrs710rsrid2/lvoldata           199398672 118367215 81031457  60%  /data  
 /dev/vgrs710jnl/lvoljnl           152256633 77300832 74955801  51%  /journals  
 /dev/vgmsa2312t3/lvolmsadata3           1857692665 428977413 1428715252  24%  /data3  
 /dev/vgmsa2312t4/lvolmsadata4           1717106748 439950342 1277156406  26%  /data6  
 /dev/vgmsa2312t5/lvolmsadata5           1947765811 1871078336 76687475  97%  /data5  
 /dev/vg00/lvol4    1041520  93368  948152   9%  /home  
 /dev/vg00/lvol5    10482360 8147296 2335064  78%  /opt  
 /dev/vg00/lvol6    2088120  936752 1151368  45%  /tmp  
 /dev/vg00/lvol7    20934792 16264528 4670264  78%  /usr  
 /dev/vg00/lvol9    9831597  19038 9812559   1%  /var/opt/ignite/depots  
 /dev/vg00/lvol8    10422528 1068504 9354024  11%  /var  
 /dev/vg00/lvol1    522448  279624  242824  54%  /stand  
 /dev/vg00/lvol3    1044448  515952  528496  50%  /