mp3.c
author nathan
Wed, 04 Feb 2009 12:30:14 +0800
branchtrunk
changeset 23 3b14b8aacaa0
parent 22 93aaf15c145a
child 24 0598c933ccae
permissions -rw-r--r--
remove unecessary compatibility code
     1 /*
     2  * MP3/MPlayer plugin to VDR (C++)
     3  *
     4  * (C) 2001-2009 Stefan Huelswitt <s.huelswitt@gmx.de>
     5  *
     6  * This code is free software; you can redistribute it and/or
     7  * modify it under the terms of the GNU General Public License
     8  * as published by the Free Software Foundation; either version 2
     9  * of the License, or (at your option) any later version.
    10  *
    11  * This code is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License
    17  * along with this program; if not, write to the Free Software
    18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    19  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
    20  */
    21 
    22 #include <stdlib.h>
    23 #include <getopt.h>
    24 #include <strings.h>
    25 #include <typeinfo>
    26 
    27 #include "common.h"
    28 
    29 #include <vdr/menuitems.h>
    30 #include <vdr/status.h>
    31 #include <vdr/plugin.h>
    32 #include <vdr/interface.h>
    33 #include <vdr/skins.h>
    34 
    35 #include "setup.h"
    36 #include "setup-mp3.h"
    37 #include "data-mp3.h"
    38 #include "data-src.h"
    39 #include "player-mp3.h"
    40 #include "menu.h"
    41 #include "menu-async.h"
    42 #include "decoder.h"
    43 #include "i18n.h"
    44 #include "version.h"
    45 #include "service.h"
    46 
    47 #ifdef DEBUG
    48 #include <mad.h>
    49 #endif
    50 
    51 const char *sourcesSub=0;
    52 cFileSources MP3Sources;
    53 
    54 static const char *plugin_name=0;
    55 const char *i18n_name=0;
    56 
    57 // --- cMenuSetupMP3 --------------------------------------------------------
    58 
    59 class cMenuSetupMP3 : public cMenuSetupPage {
    60 private:
    61   cMP3Setup data;
    62   //
    63   const char *cddb[3], *disp[2], *scan[3], *bgr[3];
    64   const char *aout[AUDIOOUTMODES];
    65   int amode, amodes[AUDIOOUTMODES];
    66 protected:
    67   virtual void Store(void);
    68 public:
    69   cMenuSetupMP3(void);
    70   };
    71 
    72 cMenuSetupMP3::cMenuSetupMP3(void)
    73 {
    74   static const char allowed[] = { "abcdefghijklmnopqrstuvwxyz0123456789-_" };
    75   int numModes=0;
    76   aout[numModes]=trVDR("DVB"); amodes[numModes]=AUDIOOUTMODE_DVB; numModes++;
    77 #ifdef WITH_OSS
    78   aout[numModes]=tr("OSS"); amodes[numModes]=AUDIOOUTMODE_OSS; numModes++;
    79 #endif
    80   data=MP3Setup;
    81   amode=0;
    82   for(int i=0; i<numModes; i++)
    83     if(amodes[i]==data.AudioOutMode) { amode=i; break; }
    84 
    85   SetSection(tr("MP3"));
    86   Add(new cMenuEditStraItem(tr("Setup.MP3$Audio output mode"),     &amode,numModes,aout));
    87   Add(new cMenuEditBoolItem(tr("Setup.MP3$Audio mode"),            &data.AudioMode, tr("Round"), tr("Dither")));
    88   Add(new cMenuEditBoolItem(tr("Setup.MP3$Use 48kHz mode only"),   &data.Only48kHz));
    89   disp[0]=tr("classic");
    90   disp[1]=tr("via skin");
    91   Add(new cMenuEditStraItem(tr("Setup.MP3$Replay display"),        &data.ReplayDisplay, 2, disp));
    92   Add(new cMenuEditIntItem( tr("Setup.MP3$Display mode"),          &data.DisplayMode, 1, 3));
    93   bgr[0]=tr("Black");
    94   bgr[1]=tr("Live");
    95   bgr[2]=tr("Images");
    96   Add(new cMenuEditStraItem(tr("Setup.MP3$Background mode"),       &data.BackgrMode, 3, bgr));
    97   Add(new cMenuEditBoolItem(tr("Setup.MP3$Initial loop mode"),     &data.InitLoopMode));
    98   Add(new cMenuEditBoolItem(tr("Setup.MP3$Initial shuffle mode"),  &data.InitShuffleMode));
    99   Add(new cMenuEditBoolItem(tr("Setup.MP3$Abort player at end of list"),&data.AbortAtEOL));
   100   scan[0]=tr("disabled");
   101   scan[1]=tr("ID3 only");
   102   scan[2]=tr("ID3 & Level");
   103   Add(new cMenuEditStraItem(tr("Setup.MP3$Background scan"),       &data.BgrScan, 3, scan));
   104   Add(new cMenuEditBoolItem(tr("Setup.MP3$Editor display mode"),   &data.EditorMode, tr("Filenames"), tr("ID3 names")));
   105   Add(new cMenuEditBoolItem(tr("Setup.MP3$Mainmenu mode"),         &data.MenuMode, tr("Playlists"), tr("Browser")));
   106   Add(new cMenuEditBoolItem(tr("Setup.MP3$Keep selection menu"),   &data.KeepSelect));
   107   Add(new cMenuEditBoolItem(tr("Setup.MP3$Title/Artist order"),    &data.TitleArtistOrder, tr("Normal"), tr("Reversed")));
   108   Add(new cMenuEditBoolItem(tr("Hide mainmenu entry"),             &data.HideMainMenu));
   109   Add(new cMenuEditIntItem( tr("Setup.MP3$Normalizer level"),      &data.TargetLevel, 0, MAX_TARGET_LEVEL));
   110   Add(new cMenuEditIntItem( tr("Setup.MP3$Limiter level"),         &data.LimiterLevel, MIN_LIMITER_LEVEL, 100));
   111   Add(new cMenuEditBoolItem(tr("Setup.MP3$Use HTTP proxy"),        &data.UseProxy));
   112   Add(new cMenuEditStrItem( tr("Setup.MP3$HTTP proxy host"),       data.ProxyHost,MAX_HOSTNAME,allowed));
   113   Add(new cMenuEditIntItem( tr("Setup.MP3$HTTP proxy port"),       &data.ProxyPort,1,65535));
   114   cddb[0]=tr("disabled");
   115   cddb[1]=tr("local only");
   116   cddb[2]=tr("local&remote");
   117   Add(new cMenuEditStraItem(tr("Setup.MP3$CDDB for CD-Audio"),     &data.UseCddb,3,cddb));
   118   Add(new cMenuEditStrItem( tr("Setup.MP3$CDDB server"),           data.CddbHost,MAX_HOSTNAME,allowed));
   119   Add(new cMenuEditIntItem( tr("Setup.MP3$CDDB port"),             &data.CddbPort,1,65535));
   120 }
   121 
   122 void cMenuSetupMP3::Store(void)
   123 {
   124   data.AudioOutMode=amodes[amode];
   125 
   126   MP3Setup=data;
   127   SetupStore("InitLoopMode",     MP3Setup.InitLoopMode   );
   128   SetupStore("InitShuffleMode",  MP3Setup.InitShuffleMode);
   129   SetupStore("AudioMode",        MP3Setup.AudioMode      );
   130   SetupStore("AudioOutMode",     MP3Setup.AudioOutMode   );
   131   SetupStore("BgrScan",          MP3Setup.BgrScan        );
   132   SetupStore("EditorMode",       MP3Setup.EditorMode     );
   133   SetupStore("DisplayMode",      MP3Setup.DisplayMode    );
   134   SetupStore("BackgrMode",       MP3Setup.BackgrMode     );
   135   SetupStore("MenuMode",         MP3Setup.MenuMode       );
   136   SetupStore("TargetLevel",      MP3Setup.TargetLevel    );
   137   SetupStore("LimiterLevel",     MP3Setup.LimiterLevel   );
   138   SetupStore("Only48kHz",        MP3Setup.Only48kHz      );
   139   SetupStore("UseProxy",         MP3Setup.UseProxy       );
   140   SetupStore("ProxyHost",        MP3Setup.ProxyHost      );
   141   SetupStore("ProxyPort",        MP3Setup.ProxyPort      );
   142   SetupStore("UseCddb",          MP3Setup.UseCddb        );
   143   SetupStore("CddbHost",         MP3Setup.CddbHost       );
   144   SetupStore("CddbPort",         MP3Setup.CddbPort       );
   145   SetupStore("AbortAtEOL",       MP3Setup.AbortAtEOL     );
   146   SetupStore("ReplayDisplay",    MP3Setup.ReplayDisplay  );
   147   SetupStore("HideMainMenu",     MP3Setup.HideMainMenu   );
   148   SetupStore("KeepSelect",       MP3Setup.KeepSelect     );
   149   SetupStore("TitleArtistOrder", MP3Setup.TitleArtistOrder);
   150 }
   151 
   152 // --- cAsyncStatus ------------------------------------------------------------
   153 
   154 cAsyncStatus asyncStatus;
   155 
   156 cAsyncStatus::cAsyncStatus(void)
   157 {
   158   text=0;
   159   changed=false;
   160 }
   161 
   162 cAsyncStatus::~cAsyncStatus()
   163 {
   164   free((void *)text);
   165 }
   166 
   167 void cAsyncStatus::Set(const char *Text)
   168 {
   169   Lock();
   170   free((void *)text);
   171   text=Text ? strdup(Text) : 0;
   172   changed=true;
   173   Unlock();
   174 }
   175 
   176 const char *cAsyncStatus::Begin(void)
   177 {
   178   Lock();
   179   return text;
   180 }
   181 
   182 void cAsyncStatus::Finish(void)
   183 {
   184   changed=false;
   185   Unlock();
   186 }
   187 
   188 // --- --------------------------------------------------------------------
   189 
   190 static cString TitleArtist(const char *title, const char *artist)
   191 {
   192   const char *fmt;
   193   if(artist && artist[0]) {
   194     if(MP3Setup.TitleArtistOrder) fmt="%2$s - %1$s";
   195     else  fmt="%s - %s";
   196     }
   197   else fmt="%s";
   198   return cString::sprintf(fmt,title,artist);
   199 }
   200 
   201 // --- cMP3Control --------------------------------------------------------
   202 
   203 #define MAXROWS 120
   204 
   205 class cMP3Control : public cControl {
   206 private:
   207   cOsd *osd;
   208   const cFont *font;
   209   cSkinDisplayReplay *disp;
   210   int bw, bh, bwc, fw, fh;
   211   //
   212   cMP3Player *player;
   213   bool visible, shown, bigwin, statusActive;
   214   time_t timeoutShow, greentime, oktime;
   215   cTimeMs lastkeytime;
   216   int number;
   217   bool selecting;
   218   //
   219   cMP3PlayInfo *lastMode;
   220   time_t fliptime, listtime;
   221   int hashlist[MAXROWS];
   222   int flip, flipint, top, rows;
   223   int lastIndex, lastTotal, lastTop;
   224   int framesPerSecond;
   225   //
   226   bool jumpactive, jumphide, jumpsecs;
   227   int jumpmm;
   228   //
   229   void ShowTimed(int Seconds=0);
   230   void ShowProgress(bool open=false, bool bigWin=false);
   231   void ShowStatus(bool force);
   232   void HideStatus(void);
   233   void DisplayInfo(const char *s=0);
   234   void JumpDisplay(void);
   235   void JumpProcess(eKeys Key);
   236   void Jump(void);
   237   void Stop(void);
   238   void Write(int x, int y, int w, const char *text, int fg=clrWhite, int bg=clrGray50);
   239   void Fill(int x, int y, int w, int h, int fg);
   240   inline void Flush(void);
   241 public:
   242   cMP3Control(void);
   243   virtual ~cMP3Control();
   244   virtual eOSState ProcessKey(eKeys Key);
   245   virtual void Show(void) { ShowTimed(); }
   246   virtual void Hide(void);
   247   bool Visible(void) { return visible; }
   248   static bool SetPlayList(cPlayList *plist);
   249   };
   250 
   251 cMP3Control::cMP3Control(void)
   252 :cControl(player=new cMP3Player)
   253 {
   254   visible=shown=bigwin=selecting=jumpactive=jumphide=statusActive=false;
   255   timeoutShow=greentime=oktime=0;
   256   number=0;
   257   lastMode=0;
   258   framesPerSecond=SecondsToFrames(1);
   259   osd=0; disp=0;
   260   font=cFont::GetFont(fontOsd);
   261   cStatus::MsgReplaying(this,"MP3",0,true);
   262 }
   263 
   264 cMP3Control::~cMP3Control()
   265 {
   266   delete lastMode;
   267   Hide();
   268   Stop();
   269 }
   270 
   271 void cMP3Control::Stop(void)
   272 {
   273   cStatus::MsgReplaying(this,0,0,false);
   274   delete player; player=0;
   275   mgr->Halt();
   276   mgr->Flush(); //XXX remove later
   277 }
   278 
   279 bool cMP3Control::SetPlayList(cPlayList *plist)
   280 {
   281   bool res;
   282   cControl *control=cControl::Control();
   283   // is there a running MP3 player?
   284   if(control && typeid(*control)==typeid(cMP3Control)) {
   285     // add songs to running playlist
   286     mgr->Add(plist);
   287     res=true;
   288     }
   289   else {
   290     mgr->Flush();
   291     mgr->Add(plist);
   292     cControl::Launch(new cMP3Control);
   293     res=false;
   294     }
   295   delete plist;
   296   return res;
   297 }
   298 
   299 void cMP3Control::ShowTimed(int Seconds)
   300 {
   301   if(!visible) {
   302     ShowProgress(true);
   303     timeoutShow=(Seconds>0) ? time(0)+Seconds : 0;
   304     }
   305 }
   306 
   307 void cMP3Control::Hide(void)
   308 {
   309   HideStatus();
   310   timeoutShow=0;
   311   if(visible) {
   312     delete osd; osd=0;
   313     delete disp; disp=0;
   314     visible=bigwin=false;
   315 #if APIVERSNUM >= 10500
   316     SetNeedsFastResponse(false);
   317 #else
   318     needsFastResponse=false;
   319 #endif
   320     }
   321 }
   322 
   323 void cMP3Control::ShowStatus(bool force)
   324 {
   325   if((asyncStatus.Changed() || (force && !statusActive)) && !jumpactive) {
   326     const char *text=asyncStatus.Begin();
   327     if(text) {
   328       if(MP3Setup.ReplayDisplay || !osd) {
   329         if(statusActive) Skins.Message(mtStatus,0);
   330         Skins.Message(mtStatus,text);
   331         }
   332       else {
   333         if(!statusActive) osd->SaveRegion(0,bh-2*fh,bw-1,bh-fh-1);
   334         osd->DrawText(0,bh-2*fh,text,clrBlack,clrCyan,font,bw,fh,taCenter);
   335         osd->Flush();
   336         }
   337       statusActive=true;
   338       }
   339     else
   340       HideStatus();
   341     asyncStatus.Finish();
   342     }
   343 }
   344 
   345 void cMP3Control::HideStatus(void)
   346 {
   347   if(statusActive) {
   348     if(MP3Setup.ReplayDisplay || !osd)
   349       Skins.Message(mtStatus,0);
   350     else {
   351       osd->RestoreRegion();
   352       osd->Flush();
   353       }
   354     }
   355   statusActive=false;
   356 }
   357 
   358 #define CTAB    11 // some tabbing values for the progress display
   359 #define CTAB2   5
   360 
   361 void cMP3Control::Write(int x, int y, int w, const char *text, int fg, int bg)
   362 {
   363   if(osd) {
   364     x*=fw; if(x<0) x+=bw;
   365     y*=fh; if(y<0) y+=bh;
   366     osd->DrawText(x,y,text,fg,bg,font,w*fw);
   367     }
   368 }
   369 
   370 void cMP3Control::Fill(int x, int y, int w, int h, int fg)
   371 {
   372   if(osd) {
   373     x*=fw; if(x<0) x+=bw;
   374     y*=fh; if(y<0) y+=bh;
   375     osd->DrawRectangle(x,y,x+w*fw-1,y+h*fh-1,fg);
   376     }
   377 }
   378 
   379 void cMP3Control::Flush(void)
   380 {
   381   if(MP3Setup.ReplayDisplay) Skins.Flush();
   382   else if(osd) osd->Flush();
   383 }
   384 
   385 void cMP3Control::ShowProgress(bool open, bool bigWin)
   386 {
   387   int index, total;
   388 
   389   if(player->GetIndex(index,total) && total>=0) {
   390     if(!visible && open) {
   391       HideStatus();
   392       if(MP3Setup.ReplayDisplay) {
   393         disp=Skins.Current()->DisplayReplay(false);
   394         if(!disp) return;
   395         bigWin=false;
   396         }
   397       else {
   398         int bt, bp;
   399         fw=font->Width(' ')*2;
   400         fh=font->Height();
   401         if(bigWin) {
   402           bw=Setup.OSDWidth;
   403           bh=Setup.OSDHeight;
   404           bt=0;
   405           bp=2*fh;
   406           rows=(bh-bp-fh/3)/fh;
   407           }
   408         else {
   409           bw=Setup.OSDWidth;
   410           bh=bp=2*fh;
   411           bt=Setup.OSDHeight-bh;
   412           rows=0;
   413           }
   414         bwc=bw/fw+1;
   415         //d(printf("mp3: bw=%d bh=%d bt=%d bp=%d bwc=%d rows=%d fw=%d fh=%d\n",
   416         //  bw,bh,bt,bp,bwc,rows,fw,fh))
   417         osd=cOsdProvider::NewOsd(Setup.OSDLeft,Setup.OSDTop+bt);
   418         if(!osd) return;
   419         if(bigWin) {
   420           tArea Areas[] = { { 0,0,bw-1,bh-bp-1,2 }, { 0,bh-bp,bw-1,bh-1,4 } };
   421           osd->SetAreas(Areas,sizeof(Areas)/sizeof(tArea));
   422           }
   423         else {
   424           tArea Areas[] = { { 0,0,bw-1,bh-1,4 } };
   425           osd->SetAreas(Areas,sizeof(Areas)/sizeof(tArea));
   426           }
   427         osd->DrawRectangle(0,0,bw-1,bh-1,clrGray50);
   428         osd->Flush();
   429         }
   430       ShowStatus(true);
   431       bigwin=bigWin;
   432       visible=true;
   433 #if APIVERSNUM >= 10500
   434       SetNeedsFastResponse(true);
   435 #else
   436       needsFastResponse=true;
   437 #endif
   438       fliptime=listtime=0; flipint=0; flip=-1; top=lastTop=-1; lastIndex=lastTotal=-1;
   439       delete lastMode; lastMode=0;
   440       }
   441 
   442     cMP3PlayInfo *mode=new cMP3PlayInfo;
   443     bool valid=mgr->Info(-1,mode);
   444     bool changed=(!lastMode || mode->Hash!=lastMode->Hash);
   445     char buf[256];
   446     if(changed) { d(printf("mp3-ctrl: mode change detected\n")) }
   447 
   448     if(valid) { // send progress to status monitor
   449       if(changed || mode->Loop!=lastMode->Loop || mode->Shuffle!=lastMode->Shuffle) {
   450         snprintf(buf,sizeof(buf),"[%c%c] (%d/%d) %s",
   451                   mode->Loop?'L':'.',mode->Shuffle?'S':'.',mode->Num,mode->MaxNum,*TitleArtist(mode->Title,mode->Artist));
   452         cStatus::MsgReplaying(this,buf,mode->Filename[0]?mode->Filename:0,true);
   453         }
   454       }
   455 
   456     if(visible) { // refresh the OSD progress display
   457       bool flush=false;
   458 
   459       if(MP3Setup.ReplayDisplay) {
   460         if(!statusActive) {
   461           if(total>0) disp->SetProgress(index,total);
   462           disp->SetCurrent(IndexToHMSF(index));
   463           disp->SetTotal(IndexToHMSF(total));
   464           bool Play, Forward;
   465           int Speed;
   466           if(GetReplayMode(Play,Forward,Speed)) 
   467             disp->SetMode(Play, Forward, Speed);
   468           flush=true;
   469           }
   470         }
   471       else {
   472         if(!selecting && changed && !statusActive) {
   473           snprintf(buf,sizeof(buf),"(%d/%d)",mode->Num,mode->MaxNum);
   474           Write(0,-2,CTAB,buf);
   475           flush=true;
   476           }
   477 
   478         if(!lastMode || mode->Loop!=lastMode->Loop) {
   479           if(mode->Loop) Write(-4,-1,0,"L",clrBlack,clrYellow);
   480           else Fill(-4,-1,2,1,clrGray50);
   481           flush=true;
   482           }
   483         if(!lastMode || mode->Shuffle!=lastMode->Shuffle) {
   484           if(mode->Shuffle) Write(-2,-1,0,"S",clrWhite,clrRed);
   485           else Fill(-2,-1,2,1,clrGray50);
   486           flush=true;
   487           }
   488 
   489         index/=framesPerSecond; total/=framesPerSecond;
   490         if(index!=lastIndex || total!=lastTotal) {
   491           if(total>0) {
   492             cProgressBar ProgressBar(bw-(CTAB+CTAB2)*fw,fh,index,total);
   493             osd->DrawBitmap(CTAB*fw,bh-fh,ProgressBar);
   494             }
   495           snprintf(buf,sizeof(buf),total?"%02d:%02d/%02d:%02d":"%02d:%02d",index/60,index%60,total/60,total%60);
   496           Write(0,-1,11,buf);
   497           flush=true;
   498           }
   499         }
   500 
   501       if(!jumpactive) {
   502         bool doflip=false;
   503         if(MP3Setup.ReplayDisplay && (!lastMode || mode->Loop!=lastMode->Loop || mode->Shuffle!=lastMode->Shuffle))
   504           doflip=true;
   505         if(!valid || changed) {
   506           fliptime=time(0); flip=0;
   507 	  doflip=true;
   508 	  }
   509         else if(time(0)>fliptime+flipint) {
   510 	  fliptime=time(0);
   511 	  flip++; if(flip>=MP3Setup.DisplayMode) flip=0;
   512           doflip=true;
   513 	  }
   514         if(doflip) {
   515           buf[0]=0;
   516           switch(flip) {
   517 	    default:
   518 	      flip=0;
   519 	      // fall through
   520 	    case 0:
   521 	      snprintf(buf,sizeof(buf),"%s",*TitleArtist(mode->Title,mode->Artist));
   522 	      flipint=6;
   523 	      break;
   524 	    case 1:
   525               if(mode->Album[0]) {
   526       	        snprintf(buf,sizeof(buf),mode->Year>0?"from: %s (%d)":"from: %s",mode->Album,mode->Year);
   527 	        flipint=4;
   528 	        }
   529               else fliptime=0;
   530               break;
   531 	    case 2:
   532               if(mode->MaxBitrate>0)
   533                 snprintf(buf,sizeof(buf),"%.1f kHz, %d-%d kbps, %s",mode->SampleFreq/1000.0,mode->Bitrate/1000,mode->MaxBitrate/1000,mode->SMode);
   534               else
   535                 snprintf(buf,sizeof(buf),"%.1f kHz, %d kbps, %s",mode->SampleFreq/1000.0,mode->Bitrate/1000,mode->SMode);
   536 	      flipint=3;
   537 	      break;
   538 	    }
   539           if(buf[0]) {
   540             if(MP3Setup.ReplayDisplay) {
   541               char buf2[256];
   542               snprintf(buf2,sizeof(buf2),"[%c%c] (%d/%d) %s",
   543                        mode->Loop?'L':'.',mode->Shuffle?'S':'.',mode->Num,mode->MaxNum,buf);
   544               disp->SetTitle(buf2);
   545               flush=true;
   546               }
   547             else {
   548               if(!statusActive) {
   549                 DisplayInfo(buf);
   550                 flush=true;
   551                 }
   552               else { d(printf("mp3-ctrl: display info skip due to status active\n")) }
   553               }
   554             }
   555           }
   556         }
   557 
   558       if(bigwin) {
   559         bool all=(top!=lastTop || changed);
   560         if(all || time(0)>listtime+2) {
   561           int num=(top>0 && mode->Num==lastMode->Num) ? top : mode->Num - rows/2;
   562           if(num+rows>mode->MaxNum) num=mode->MaxNum-rows+1;
   563           if(num<1) num=1;
   564           top=num;
   565           for(int i=0 ; i<rows && i<MAXROWS && num<=mode->MaxNum ; i++,num++) {
   566             cMP3PlayInfo pi;
   567             mgr->Info(num,&pi); if(!pi.Title[0]) break;
   568             snprintf(buf,sizeof(buf),"%d.\t%s",num,*TitleArtist(pi.Title,pi.Artist));
   569             int fg=clrWhite, bg=clrGray50;
   570             int hash=MakeHash(buf);
   571             if(num==mode->Num) { fg=clrBlack; bg=clrCyan; hash=(hash^77) + 23; }
   572             if(all || hash!=hashlist[i]) {
   573               char *s=rindex(buf,'\t');
   574               if(s) {
   575                 *s++=0;
   576                 Write(0,i,5,buf,fg,bg);
   577                 Write(5,i,bwc-5,s,fg,bg);
   578                 }
   579               else
   580                 Write(0,i,bwc,buf,fg,bg);
   581               flush=true;
   582               hashlist[i]=hash;
   583               }
   584             }
   585           listtime=time(0); lastTop=top;
   586           }
   587         }
   588 
   589       if(flush) Flush();
   590       }
   591 
   592     lastIndex=index; lastTotal=total;
   593     delete lastMode; lastMode=mode;
   594     }
   595 }
   596 
   597 void cMP3Control::DisplayInfo(const char *s)
   598 {
   599   if(s) Write(CTAB,-2,bwc-CTAB,s);
   600   else Fill(CTAB,-2,bwc-CTAB,1,clrGray50);
   601 }
   602 
   603 void cMP3Control::JumpDisplay(void)
   604 {
   605   char buf[64];
   606   const char *j=trVDR("Jump: "), u=jumpsecs?'s':'m';
   607   if(!jumpmm) sprintf(buf,"%s- %c",  j,u);
   608   else        sprintf(buf,"%s%d- %c",j,jumpmm,u);
   609   if(MP3Setup.ReplayDisplay) {
   610     disp->SetJump(buf);
   611     }
   612   else {
   613     DisplayInfo(buf);
   614     }
   615 }
   616 
   617 void cMP3Control::JumpProcess(eKeys Key)
   618 {
   619  int n=Key-k0, d=jumpsecs?1:60;
   620   switch (Key) {
   621     case k0 ... k9:
   622       if(jumpmm*10+n <= lastTotal/d) jumpmm=jumpmm*10+n;
   623       JumpDisplay();
   624       break;
   625     case kBlue:
   626       jumpsecs=!jumpsecs;
   627       JumpDisplay();
   628       break;
   629     case kPlay:
   630     case kUp:
   631       jumpmm-=lastIndex/d;
   632       // fall through
   633     case kFastRew:
   634     case kFastFwd:
   635     case kLeft:
   636     case kRight:
   637       player->SkipSeconds(jumpmm*d * ((Key==kLeft || Key==kFastRew) ? -1:1));
   638       // fall through
   639     default:
   640       jumpactive=false;
   641       break;
   642     }
   643 
   644   if(!jumpactive) {
   645     if(jumphide) Hide();
   646     else if(MP3Setup.ReplayDisplay) disp->SetJump(0);
   647     }
   648 }
   649 
   650 void cMP3Control::Jump(void)
   651 {
   652   jumpmm=0; jumphide=jumpsecs=false;
   653   if(!visible) {
   654     ShowTimed(); if(!visible) return;
   655     jumphide=true;
   656     }
   657   JumpDisplay();
   658   jumpactive=true; fliptime=0; flip=-1;
   659 }
   660 
   661 eOSState cMP3Control::ProcessKey(eKeys Key)
   662 {
   663   if(!player->Active()) return osEnd;
   664 
   665   if(timeoutShow && time(0)>timeoutShow) Hide();
   666   ShowProgress();
   667   ShowStatus(Key==kNone && !Skins.IsOpen());
   668 
   669   if(jumpactive && Key!=kNone) { JumpProcess(Key); return osContinue; }
   670 
   671   switch(Key) {
   672     case kUp:
   673     case kUp|k_Repeat:
   674     case kNext:
   675     case kNext|k_Repeat:    
   676       mgr->Next(); player->Play();
   677       break;
   678     case kDown:
   679     case kDown|k_Repeat:
   680     case kPrev:
   681     case kPrev|k_Repeat:
   682       if(!player->PrevCheck()) mgr->Prev();
   683       player->Play();
   684       break;
   685     case kLeft:
   686     case kLeft|k_Repeat:
   687       if(bigwin) {
   688         if(top>0) { top-=rows; if(top<1) top=1; }
   689         break;
   690         }
   691       // fall through
   692     case kFastRew:
   693     case kFastRew|k_Repeat:
   694       if(!player->IsStream()) player->SkipSeconds(-JUMPSIZE);
   695       break;
   696     case kRight:
   697     case kRight|k_Repeat:
   698       if(bigwin) {
   699         if(top>0) top+=rows;
   700         break;
   701         }
   702       // fall through
   703     case kFastFwd:
   704     case kFastFwd|k_Repeat:
   705       if(!player->IsStream()) player->SkipSeconds(JUMPSIZE);
   706       break;
   707     case kRed:
   708       if(!player->IsStream()) Jump();
   709       break;
   710     case kGreen:
   711       if(lastMode) {
   712         if(time(0)>greentime) {
   713           if(lastMode->Loop || (!lastMode->Loop && !lastMode->Shuffle)) mgr->ToggleLoop();
   714           if(lastMode->Shuffle) mgr->ToggleShuffle();
   715           }
   716         else {
   717           if(!lastMode->Loop) mgr->ToggleLoop();
   718           else if(!lastMode->Shuffle) mgr->ToggleShuffle();
   719           else mgr->ToggleLoop();
   720           }
   721         greentime=time(0)+MULTI_TIMEOUT;
   722         }
   723       break;
   724     case kPlay:
   725       player->Play();
   726       break;
   727     case kPause:
   728     case kYellow:
   729       if(!player->IsStream()) player->Pause();
   730       break;
   731     case kStop:
   732     case kBlue:
   733       Hide();
   734       Stop();
   735       return osEnd;
   736     case kBack:
   737       Hide();
   738       cRemote::CallPlugin(plugin_name);
   739       return osBack;
   740 
   741     case k0 ... k9:
   742       number=number*10+Key-k0;
   743       if(lastMode && number>0 && number<=lastMode->MaxNum) {
   744         if(!visible) ShowTimed(SELECTHIDE_TIMEOUT);
   745         else if(timeoutShow>0) timeoutShow=time(0)+SELECTHIDE_TIMEOUT;
   746         selecting=true; lastkeytime.Set(SELECT_TIMEOUT);
   747         char buf[32];
   748         if(MP3Setup.ReplayDisplay) {
   749           snprintf(buf,sizeof(buf),"%s%d-/%d",trVDR("Jump: "),number,lastMode->MaxNum);
   750           disp->SetJump(buf);
   751           }
   752         else {
   753           snprintf(buf,sizeof(buf),"(%d-/%d)",number,lastMode->MaxNum);
   754           Write(0,-2,CTAB,buf);
   755           }
   756         Flush();
   757         break;
   758         }
   759       number=0; lastkeytime.Set();
   760       // fall through
   761     case kNone:
   762       if(selecting && lastkeytime.TimedOut()) {
   763         if(number>0) { mgr->Goto(number); player->Play();  }
   764         if(lastMode) lastMode->Hash=-1;
   765         number=0; selecting=false;
   766         if(MP3Setup.ReplayDisplay && disp) disp->SetJump(0);
   767         }
   768       break;
   769     case kOk:
   770       if(time(0)>oktime || MP3Setup.ReplayDisplay) {
   771         visible ? Hide() : ShowTimed();
   772         }
   773       else {
   774         if(visible && !bigwin) { Hide(); ShowProgress(true,true); }
   775         else { Hide(); ShowTimed(); }
   776         }
   777       oktime=time(0)+MULTI_TIMEOUT;
   778       ShowStatus(true);
   779       break;
   780     default:
   781       return osUnknown;
   782     }
   783   return osContinue;
   784 }
   785 
   786 // --- cMenuID3Info ------------------------------------------------------------
   787 
   788 class cMenuID3Info : public cOsdMenu {
   789 private:
   790   cOsdItem *Item(const char *name, const char *text);
   791   cOsdItem *Item(const char *name, const char *format, const float num);
   792   void Build(cSongInfo *info, const char *name);
   793 public:
   794   cMenuID3Info(cSong *song);
   795   cMenuID3Info(cSongInfo *si, const char *name);
   796   virtual eOSState ProcessKey(eKeys Key);
   797   };
   798 
   799 cMenuID3Info::cMenuID3Info(cSong *song)
   800 :cOsdMenu(tr("ID3 information"),12)
   801 {
   802   Build(song->Info(),song->Name());
   803 }
   804 
   805 cMenuID3Info::cMenuID3Info(cSongInfo *si, const char *name)
   806 :cOsdMenu(tr("ID3 information"),12)
   807 {
   808   Build(si,name);
   809 }
   810 
   811 void cMenuID3Info::Build(cSongInfo *si, const char *name)
   812 {
   813   if(si) {
   814     Item(tr("Filename"),name);
   815     if(si->HasInfo() && si->Total>0) {
   816       char *buf=0;
   817       asprintf(&buf,"%02d:%02d",si->Total/60,si->Total%60);
   818       Item(tr("Length"),buf);
   819       free(buf);
   820       Item(tr("Title"),si->Title);
   821       Item(tr("Artist"),si->Artist);
   822       Item(tr("Album"),si->Album);
   823       Item(tr("Year"),0,(float)si->Year);
   824       Item(tr("Samplerate"),"%.1f kHz",si->SampleFreq/1000.0);
   825       Item(tr("Bitrate"),"%.f kbit/s",si->Bitrate/1000.0);
   826       Item(trVDR("Channels"),0,(float)si->Channels);
   827       }
   828     Display();
   829     }
   830 }
   831 
   832 cOsdItem *cMenuID3Info::Item(const char *name, const char *format, const float num)
   833 {
   834   cOsdItem *item;
   835   if(num>=0.0) {
   836     char *buf=0;
   837     asprintf(&buf,format?format:"%.f",num);
   838     item=Item(name,buf);
   839     free(buf);
   840     }
   841   else item=Item(name,"");
   842   return item;
   843 }
   844 
   845 cOsdItem *cMenuID3Info::Item(const char *name, const char *text)
   846 {
   847   char *buf=0;
   848   asprintf(&buf,"%s:\t%s",name,text?text:"");
   849   cOsdItem *item = new cOsdItem(buf,osBack);
   850   item->SetSelectable(false);
   851   free(buf);
   852   Add(item); return item;
   853 }
   854 
   855 eOSState cMenuID3Info::ProcessKey(eKeys Key)
   856 {
   857   eOSState state = cOsdMenu::ProcessKey(Key);
   858 
   859   if(state==osUnknown) {
   860      switch(Key) {
   861        case kRed:
   862        case kGreen:
   863        case kYellow:
   864        case kBlue:   return osContinue;
   865        case kMenu:   return osEnd;
   866        default: break;
   867        }
   868      }
   869   return state;
   870 }
   871 
   872 // --- cMenuInstantBrowse -------------------------------------------------------
   873 
   874 class cMenuInstantBrowse : public cMenuBrowse {
   875 private:
   876   const char *selecttext, *alltext;
   877   virtual void SetButtons(void);
   878   virtual eOSState ID3Info(void);
   879 public:
   880   cMenuInstantBrowse(cFileSource *Source, const char *Selecttext, const char *Alltext);
   881   virtual eOSState ProcessKey(eKeys Key);
   882   };
   883 
   884 cMenuInstantBrowse::cMenuInstantBrowse(cFileSource *Source, const char *Selecttext, const char *Alltext)
   885 :cMenuBrowse(Source,true,true,tr("Directory browser"),excl_br)
   886 {
   887   selecttext=Selecttext; alltext=Alltext;
   888   SetButtons();
   889 }
   890 
   891 void cMenuInstantBrowse::SetButtons(void)
   892 {
   893   SetHelp(selecttext, currentdir?tr("Parent"):0, currentdir?0:alltext, tr("ID3 info"));
   894   Display();
   895 }
   896 
   897 eOSState cMenuInstantBrowse::ID3Info(void)
   898 {
   899   cFileObj *item=CurrentItem();
   900   if(item && item->Type()==otFile) {
   901     cSong *song=new cSong(item);
   902     cSongInfo *si;
   903     if(song && (si=song->Info())) {
   904       AddSubMenu(new cMenuID3Info(si,item->Path()));
   905       }
   906     delete song;
   907     }
   908   return osContinue;
   909 }
   910 
   911 eOSState cMenuInstantBrowse::ProcessKey(eKeys Key)
   912 {
   913   eOSState state=cOsdMenu::ProcessKey(Key);
   914   if(state==osUnknown) {
   915      switch (Key) {
   916        case kYellow: lastselect=new cFileObj(source,0,0,otBase);
   917                      return osBack;
   918        default: break;
   919        }
   920      }
   921   if(state==osUnknown) state=cMenuBrowse::ProcessStdKey(Key,state);
   922   return state;
   923 }
   924 
   925 // --- cMenuPlayListItem -------------------------------------------------------
   926 
   927 class cMenuPlayListItem : public cOsdItem {
   928   private:
   929   bool showID3;
   930   cSong *song;
   931 public:
   932   cMenuPlayListItem(cSong *Song, bool showid3);
   933   cSong *Song(void) { return song; }
   934   virtual void Set(void);
   935   void Set(bool showid3);
   936   };
   937 
   938 cMenuPlayListItem::cMenuPlayListItem(cSong *Song, bool showid3)
   939 {
   940   song=Song;
   941   Set(showid3);
   942 }
   943 
   944 void cMenuPlayListItem::Set(bool showid3)
   945 {
   946   showID3=showid3;
   947   Set();
   948 }
   949 
   950 void cMenuPlayListItem::Set(void)
   951 {
   952   char *buffer=0;
   953   cSongInfo *si=song->Info(false);
   954   if(showID3 && !si) si=song->Info();
   955   if(showID3 && si && si->Title)
   956     asprintf(&buffer, "%d.\t%s",song->Index()+1,*TitleArtist(si->Title,si->Artist));
   957   else
   958     asprintf(&buffer, "%d.\t<%s>",song->Index()+1,song->Name());
   959   SetText(buffer,false);
   960 }
   961 
   962 // --- cMenuPlayList ------------------------------------------------------
   963 
   964 class cMenuPlayList : public cOsdMenu {
   965 private:
   966   cPlayList *playlist;
   967   bool browsing, showid3;
   968   void Buttons(void);
   969   void Refresh(bool all = false);
   970   void Add(void);
   971   virtual void Move(int From, int To);
   972   eOSState Remove(void);
   973   eOSState ShowID3(void);
   974   eOSState ID3Info(void);
   975 public:
   976   cMenuPlayList(cPlayList *Playlist);
   977   virtual eOSState ProcessKey(eKeys Key);
   978   };
   979 
   980 cMenuPlayList::cMenuPlayList(cPlayList *Playlist)
   981 :cOsdMenu(tr("Playlist editor"),4)
   982 {
   983   browsing=showid3=false;
   984   playlist=Playlist;
   985   if(MP3Setup.EditorMode) showid3=true;
   986 
   987   cSong *mp3 = playlist->First();
   988   while(mp3) {
   989     cOsdMenu::Add(new cMenuPlayListItem(mp3,showid3));
   990     mp3 = playlist->cList<cSong>::Next(mp3);
   991     }
   992   Buttons(); Display();
   993 }
   994 
   995 void cMenuPlayList::Buttons(void)
   996 {
   997   SetHelp(tr("Add"), showid3?tr("Filenames"):tr("ID3 names"), tr("Remove"), trVDR("Button$Mark"));
   998 }
   999 
  1000 void cMenuPlayList::Refresh(bool all)
  1001 {
  1002   cMenuPlayListItem *cur=(cMenuPlayListItem *)((all || Count()<2) ? First() : Get(Current()));
  1003   while(cur) {
  1004     cur->Set(showid3);
  1005     cur=(cMenuPlayListItem *)Next(cur);
  1006     }
  1007 }
  1008 
  1009 void cMenuPlayList::Add(void)
  1010 {
  1011   cFileObj *item=cMenuInstantBrowse::GetSelected();
  1012   if(item) {
  1013     Status(tr("Scanning directory..."));
  1014     cInstantPlayList *newpl=new cInstantPlayList(item);
  1015     if(newpl->Load()) {
  1016       if(newpl->Count()) {
  1017         if(newpl->Count()==1 || Interface->Confirm(tr("Add recursivly?"))) {
  1018           cSong *mp3=newpl->First();
  1019           while(mp3) {
  1020             cSong *n=new cSong(mp3);
  1021             if(Count()>0) {
  1022               cMenuPlayListItem *current=(cMenuPlayListItem *)Get(Current());
  1023               playlist->Add(n,current->Song());
  1024               cOsdMenu::Add(new cMenuPlayListItem(n,showid3),true,current);
  1025               }
  1026             else {
  1027               playlist->Add(n);
  1028               cOsdMenu::Add(new cMenuPlayListItem(n,showid3),true);
  1029               }
  1030             mp3=newpl->cList<cSong>::Next(mp3);
  1031             }
  1032           playlist->Save();
  1033           Refresh(); Display();
  1034           }
  1035         }
  1036       else Error(tr("Empty directory!"));
  1037       }
  1038     else Error(tr("Error scanning directory!"));
  1039     delete newpl;
  1040     Status(0);
  1041     }
  1042 }
  1043 
  1044 void cMenuPlayList::Move(int From, int To)
  1045 {
  1046   playlist->Move(From,To); playlist->Save();
  1047   cOsdMenu::Move(From,To);
  1048   Refresh(true); Display();
  1049 }
  1050 
  1051 eOSState cMenuPlayList::ShowID3(void)
  1052 {
  1053   showid3=!showid3;
  1054   Buttons(); Refresh(true); Display();
  1055   return osContinue;
  1056 }
  1057 
  1058 eOSState cMenuPlayList::ID3Info(void)
  1059 {
  1060   if(Count()>0) {
  1061     cMenuPlayListItem *current = (cMenuPlayListItem *)Get(Current());
  1062     AddSubMenu(new cMenuID3Info(current->Song()));
  1063     }
  1064   return osContinue;
  1065 }
  1066 
  1067 eOSState cMenuPlayList::Remove(void)
  1068 {
  1069   if(Count()>0) {
  1070     cMenuPlayListItem *current = (cMenuPlayListItem *)Get(Current());
  1071     if(Interface->Confirm(tr("Remove entry?"))) {
  1072       playlist->Del(current->Song()); playlist->Save();
  1073       cOsdMenu::Del(Current());
  1074       Refresh(); Display();
  1075       }
  1076     }
  1077   return osContinue;
  1078 }
  1079 
  1080 eOSState cMenuPlayList::ProcessKey(eKeys Key)
  1081 {
  1082   eOSState state = cOsdMenu::ProcessKey(Key);
  1083 
  1084   if(browsing && !HasSubMenu() && state==osContinue) { Add(); browsing=false; }
  1085 
  1086   if(state==osUnknown) {
  1087      switch(Key) {
  1088        case kOk:     return ID3Info();
  1089        case kRed:    browsing=true;
  1090                      return AddSubMenu(new cMenuInstantBrowse(MP3Sources.GetSource(),tr("Add"),tr("Add all")));
  1091        case kGreen:  return ShowID3();
  1092        case kYellow: return Remove();
  1093        case kBlue:   Mark(); return osContinue;
  1094        case kMenu:   return osEnd;
  1095        default: break;
  1096        }
  1097      }
  1098   return state;
  1099 }
  1100 
  1101 // --- cPlaylistRename --------------------------------------------------------
  1102 
  1103 class cPlaylistRename : public cOsdMenu {
  1104 private:
  1105   static char *newname;
  1106   const char *oldname;
  1107   char data[64];
  1108 public:
  1109   cPlaylistRename(const char *Oldname);
  1110   virtual eOSState ProcessKey(eKeys Key);
  1111   static const char *GetNewname(void) { return newname; }
  1112   };
  1113 
  1114 char *cPlaylistRename::newname = NULL;
  1115 
  1116 cPlaylistRename::cPlaylistRename(const char *Oldname)
  1117 :cOsdMenu(tr("Rename playlist"), 15)
  1118 {
  1119   free(newname); newname=0;
  1120 
  1121   oldname=Oldname;
  1122   char *buf=NULL;
  1123   asprintf(&buf,"%s\t%s",tr("Old name:"),oldname);
  1124   cOsdItem *old = new cOsdItem(buf,osContinue);
  1125   old->SetSelectable(false);
  1126   Add(old);
  1127   free(buf);
  1128 
  1129   data[0]=0;
  1130   Add(new cMenuEditStrItem( tr("New name"), data, sizeof(data)-1, tr(FileNameChars)),true);
  1131 }
  1132 
  1133 eOSState cPlaylistRename::ProcessKey(eKeys Key)
  1134 {
  1135   eOSState state = cOsdMenu::ProcessKey(Key);
  1136 
  1137   if (state == osUnknown) {
  1138      switch (Key) {
  1139        case kOk:     if(data[0] && strcmp(data,oldname)) newname=strdup(data);
  1140                      return osBack;
  1141        case kRed:
  1142        case kGreen:
  1143        case kYellow:
  1144        case kBlue:   return osContinue;
  1145        default: break;
  1146        }
  1147      }
  1148   return state;
  1149 }
  1150 
  1151 // --- cMenuMP3Item -----------------------------------------------------
  1152 
  1153 class cMenuMP3Item : public cOsdItem {
  1154   private:
  1155   cPlayList *playlist;
  1156   virtual void Set(void);
  1157 public:
  1158   cMenuMP3Item(cPlayList *PlayList);
  1159   cPlayList *List(void) { return playlist; }
  1160   };
  1161 
  1162 cMenuMP3Item::cMenuMP3Item(cPlayList *PlayList)
  1163 {
  1164   playlist=PlayList;
  1165   Set();
  1166 }
  1167 
  1168 void cMenuMP3Item::Set(void)
  1169 {
  1170   char *buffer=0;
  1171   asprintf(&buffer," %s",playlist->BaseName());
  1172   SetText(buffer,false);
  1173 }
  1174 
  1175 // --- cMenuMP3 --------------------------------------------------------
  1176 
  1177 class cMenuMP3 : public cOsdMenu {
  1178 private:
  1179   cPlayLists *lists;
  1180   bool renaming, sourcing, instanting;
  1181   int buttonnum;
  1182   eOSState Play(void);
  1183   eOSState Edit(void);
  1184   eOSState New(void);
  1185   eOSState Delete(void);
  1186   eOSState Rename(bool second);
  1187   eOSState Source(bool second);
  1188   eOSState Instant(bool second);
  1189   void ScanLists(void);
  1190   eOSState SetButtons(int num);
  1191 public:
  1192   cMenuMP3(void);
  1193   ~cMenuMP3(void);
  1194   virtual eOSState ProcessKey(eKeys Key);
  1195   };
  1196 
  1197 cMenuMP3::cMenuMP3(void)
  1198 :cOsdMenu(tr("MP3"))
  1199 {
  1200   renaming=sourcing=instanting=false;
  1201   lists=new cPlayLists;
  1202   ScanLists(); SetButtons(1);
  1203   if(MP3Setup.MenuMode) Instant(false);
  1204 }
  1205 
  1206 cMenuMP3::~cMenuMP3(void)
  1207 {
  1208   delete lists;
  1209 }
  1210 
  1211 eOSState cMenuMP3::SetButtons(int num)
  1212 {
  1213   switch(num) {
  1214     case 1:
  1215       SetHelp(trVDR("Button$Edit"), tr("Source"), tr("Browse"), ">>");
  1216       break;
  1217     case 2:
  1218       SetHelp("<<", trVDR("Button$New"), trVDR("Button$Delete"), tr("Rename"));
  1219       break;
  1220     }
  1221   buttonnum=num; Display();
  1222   return osContinue;
  1223 }
  1224 
  1225 void cMenuMP3::ScanLists(void)
  1226 {
  1227   Clear();
  1228   Status(tr("Scanning playlists..."));
  1229   bool res=lists->Load(MP3Sources.GetSource());
  1230   Status(0);
  1231   if(res) {
  1232     cPlayList *plist=lists->First();
  1233     while(plist) {
  1234       Add(new cMenuMP3Item(plist));
  1235       plist=lists->Next(plist);
  1236       }
  1237     }
  1238   else Error(tr("Error scanning playlists!"));
  1239 }
  1240 
  1241 eOSState cMenuMP3::Delete(void)
  1242 {
  1243   if(Count()>0) {
  1244     if(Interface->Confirm(tr("Delete playlist?")) &&
  1245        Interface->Confirm(tr("Are you sure?")) ) {
  1246       cPlayList *plist = ((cMenuMP3Item *)Get(Current()))->List();
  1247       if(plist->Delete()) {
  1248         lists->Del(plist);
  1249         cOsdMenu::Del(Current());
  1250         Display();
  1251         }
  1252       else Error(tr("Error deleting playlist!"));
  1253       }
  1254     }
  1255   return osContinue;
  1256 }
  1257 
  1258 eOSState cMenuMP3::New(void)
  1259 {
  1260   cPlayList *plist=new cPlayList(MP3Sources.GetSource(),0,0);
  1261   char name[32];
  1262   int i=0;
  1263   do {
  1264     if(i) sprintf(name,"%s%d",tr("unnamed"),i++);
  1265     else { strcpy(name,tr("unnamed")); i++; }
  1266     } while(plist->TestName(name));
  1267 
  1268   if(plist->Create(name)) {
  1269     lists->Add(plist);
  1270     Add(new cMenuMP3Item(plist), true);
  1271 
  1272     isyslog("MP3: playlist %s added", plist->Name());
  1273     return AddSubMenu(new cMenuPlayList(plist));
  1274     }
  1275   Error(tr("Error creating playlist!"));
  1276   delete plist;
  1277   return osContinue;
  1278 }
  1279 
  1280 eOSState cMenuMP3::Rename(bool second)
  1281 {
  1282   if(HasSubMenu() || Count() == 0) return osContinue;
  1283 
  1284   cPlayList *plist = ((cMenuMP3Item *)Get(Current()))->List();
  1285   if(!second) {
  1286     renaming=true;
  1287     return AddSubMenu(new cPlaylistRename(plist->BaseName()));
  1288     }
  1289   renaming=false;
  1290   const char *newname=cPlaylistRename::GetNewname();
  1291   if(newname) {
  1292     if(plist->Rename(newname)) {
  1293       RefreshCurrent();
  1294       DisplayCurrent(true);
  1295       }
  1296     else Error(tr("Error renaming playlist!"));
  1297     }
  1298   return osContinue;
  1299 }
  1300 
  1301 eOSState cMenuMP3::Edit(void)
  1302 {
  1303   if(HasSubMenu() || Count() == 0) return osContinue;
  1304 
  1305   cPlayList *plist = ((cMenuMP3Item *)Get(Current()))->List();
  1306   if(!plist->Load()) Error(tr("Error loading playlist!"));
  1307   else if(!plist->IsWinAmp()) {
  1308     isyslog("MP3: editing playlist %s", plist->Name());
  1309     return AddSubMenu(new cMenuPlayList(plist));
  1310     }
  1311   else Error(tr("Can't edit a WinAmp playlist!"));
  1312   return osContinue;
  1313 }
  1314 
  1315 eOSState cMenuMP3::Play(void)
  1316 {
  1317   if(HasSubMenu() || Count() == 0) return osContinue;
  1318 
  1319   Status(tr("Loading playlist..."));
  1320   cPlayList *newpl=new cPlayList(((cMenuMP3Item *)Get(Current()))->List());
  1321   if(newpl->Load() && newpl->Count()) {
  1322     isyslog("mp3: playback started with playlist %s", newpl->Name());
  1323     cMP3Control::SetPlayList(newpl);
  1324     if(MP3Setup.KeepSelect) { Status(0); return osContinue; }
  1325     return osEnd;
  1326     }
  1327   Status(0);
  1328   delete newpl;
  1329   Error(tr("Error loading playlist!"));
  1330   return osContinue;
  1331 }
  1332 
  1333 eOSState cMenuMP3::Source(bool second)
  1334 {
  1335   if(HasSubMenu()) return osContinue;
  1336 
  1337   if(!second) {
  1338     sourcing=true;
  1339     return AddSubMenu(new cMenuSource(&MP3Sources,tr("MP3 source")));
  1340     }
  1341   sourcing=false;
  1342   cFileSource *src=cMenuSource::GetSelected();
  1343   if(src) {
  1344     MP3Sources.SetSource(src);
  1345     ScanLists();
  1346     Display();
  1347     }
  1348   return osContinue;
  1349 }
  1350 
  1351 eOSState cMenuMP3::Instant(bool second)
  1352 {
  1353   if(HasSubMenu()) return osContinue;
  1354 
  1355   if(!second) {
  1356     instanting=true;
  1357     return AddSubMenu(new cMenuInstantBrowse(MP3Sources.GetSource(),trVDR("Button$Play"),tr("Play all")));
  1358     }
  1359   instanting=false;
  1360   cFileObj *item=cMenuInstantBrowse::GetSelected();
  1361   if(item) {
  1362     Status(tr("Building playlist..."));
  1363     cInstantPlayList *newpl = new cInstantPlayList(item);
  1364     if(newpl->Load() && newpl->Count()) {
  1365       isyslog("mp3: playback started with instant playlist %s", newpl->Name());
  1366       cMP3Control::SetPlayList(newpl);
  1367       if(MP3Setup.KeepSelect) { Status(0); return Instant(false); }
  1368       return osEnd;
  1369       }
  1370     Status(0);
  1371     delete newpl;
  1372     Error(tr("Error building playlist!"));
  1373     }
  1374   return osContinue;
  1375 }
  1376 
  1377 eOSState cMenuMP3::ProcessKey(eKeys Key)
  1378 {
  1379   eOSState state = cOsdMenu::ProcessKey(Key);
  1380 
  1381   if(!HasSubMenu() && state==osContinue) { // eval the return value from submenus
  1382     if(renaming) return Rename(true);
  1383     if(sourcing) return Source(true);
  1384     if(instanting) return Instant(true);
  1385     }
  1386 
  1387   if(state == osUnknown) {
  1388     switch(Key) {
  1389       case kOk:     return Play();
  1390       case kRed:    return (buttonnum==1 ? Edit() : SetButtons(1)); 
  1391       case kGreen:  return (buttonnum==1 ? Source(false) : New());
  1392       case kYellow: return (buttonnum==1 ? Instant(false) : Delete());
  1393       case kBlue:   return (buttonnum==1 ? SetButtons(2) : Rename(false));
  1394       case kMenu:   return osEnd;
  1395       default:      break;
  1396       }
  1397     }
  1398   return state;
  1399 }
  1400 
  1401 // --- PropagateImage ----------------------------------------------------------
  1402 
  1403 void PropagateImage(const char *image)
  1404 {
  1405   cPlugin *graphtft=cPluginManager::GetPlugin("graphtft");
  1406   if(graphtft) graphtft->SetupParse("CoverImage",image ? image:"");
  1407 }
  1408 
  1409 // --- cPluginMP3 --------------------------------------------------------------
  1410 
  1411 static const char *DESCRIPTION    = trNOOP("A versatile audio player");
  1412 static const char *MAINMENUENTRY  = "MP3";
  1413 
  1414 class cPluginMp3 : public cPlugin {
  1415 private:
  1416   bool ExternalPlay(const char *path, bool test);
  1417 public:
  1418   cPluginMp3(void);
  1419   virtual ~cPluginMp3();
  1420   virtual const char *Version(void) { return PluginVersion; }
  1421   virtual const char *Description(void) { return tr(DESCRIPTION); }
  1422   virtual const char *CommandLineHelp(void);
  1423   virtual bool ProcessArgs(int argc, char *argv[]);
  1424   virtual bool Initialize(void);
  1425   virtual void Housekeeping(void);
  1426   virtual const char *MainMenuEntry(void);
  1427   virtual cOsdObject *MainMenuAction(void);
  1428   virtual cMenuSetupPage *SetupMenu(void);
  1429   virtual bool SetupParse(const char *Name, const char *Value);
  1430   virtual bool Service(const char *Id, void *Data);
  1431   virtual const char **SVDRPHelpPages(void);
  1432   virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
  1433   };
  1434 
  1435 cPluginMp3::cPluginMp3(void)
  1436 {
  1437   // Initialize any member varaiables here.
  1438   // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
  1439   // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
  1440 }
  1441 
  1442 cPluginMp3::~cPluginMp3()
  1443 {
  1444   InfoCache.Shutdown();
  1445   delete mgr;
  1446 }
  1447 
  1448 const char *cPluginMp3::CommandLineHelp(void)
  1449 {
  1450   static char *help_str=0;
  1451   
  1452   free(help_str);    //                                     for easier orientation, this is column 80|
  1453   asprintf(&help_str,"  -m CMD,   --mount=CMD    use CMD to mount/unmount/eject mp3 sources\n"
  1454                      "                           (default: %s)\n"
  1455                      "  -n CMD,   --network=CMD  execute CMD before & after network access\n"
  1456                      "                           (default: %s)\n"
  1457                      "  -C DIR,   --cache=DIR    store ID3 cache file in DIR\n"
  1458                      "                           (default: %s)\n"
  1459                      "  -B DIR,   --cddb=DIR     search CDDB files in DIR\n"
  1460                      "                           (default: %s)\n"
  1461                      "  -D DEV,   --dsp=DEV      device for OSS output\n"
  1462                      "                           (default: %s)\n"
  1463                      "  -i CMD,   --iconv=CMD    use CMD to convert background images\n"
  1464                      "                           (default: %s)\n"
  1465                      "  -I IMG,   --defimage=IMG use IMG as default background image\n"
  1466                      "                           (default: none)\n"
  1467                      "  -c DIR,   --icache=DIR   cache converted images in DIR\n"
  1468                      "                           (default: %s)\n"
  1469                      "  -S SUB,   --sources=SUB  search sources config in SUB subdirectory\n"
  1470                      "                           (default: %s)\n",
  1471                      
  1472                      mountscript,
  1473                      netscript ? netscript:"none",
  1474                      cachedir ? cachedir:"video dir",
  1475 #ifdef HAVE_SNDFILE
  1476                      cddbpath,
  1477 #else
  1478                      "none",
  1479 #endif
  1480 #ifdef WITH_OSS
  1481                      dspdevice,
  1482 #else
  1483                      "none",
  1484 #endif
  1485                      imageconv,
  1486                      imagecache,
  1487                      sourcesSub ? sourcesSub:"empty"
  1488                      );
  1489   return help_str;
  1490 }
  1491 
  1492 bool cPluginMp3::ProcessArgs(int argc, char *argv[])
  1493 {
  1494   static struct option long_options[] = {
  1495       { "mount",    required_argument, NULL, 'm' },
  1496       { "network",  required_argument, NULL, 'n' },
  1497       { "cddb",     required_argument, NULL, 'B' },
  1498       { "dsp",      required_argument, NULL, 'D' },
  1499       { "cache",    required_argument, NULL, 'C' },
  1500       { "icache",   required_argument, NULL, 'c' },
  1501       { "iconv",    required_argument, NULL, 'i' },
  1502       { "defimage", required_argument, NULL, 'I' },
  1503       { "sources",  required_argument, NULL, 'S' },
  1504       { NULL }
  1505     };
  1506 
  1507   int c, option_index = 0;
  1508   while((c=getopt_long(argc,argv,"c:i:m:n:B:C:D:S:",long_options,&option_index))!=-1) {
  1509     switch (c) {
  1510       case 'i': imageconv=optarg; break;
  1511       case 'c': imagecache=optarg; break;
  1512       case 'm': mountscript=optarg; break;
  1513       case 'n': netscript=optarg; break;
  1514       case 'C': cachedir=optarg; break;
  1515       case 'I': def_usr_img=optarg; break;
  1516       case 'S': sourcesSub=optarg; break;
  1517       case 'B':
  1518 #ifdef HAVE_SNDFILE
  1519                 cddbpath=optarg; break;
  1520 #else
  1521                 fprintf(stderr, "mp3: libsndfile support has not been compiled in!\n"); return false;
  1522 #endif
  1523       case 'D':
  1524 #ifdef WITH_OSS
  1525                 dspdevice=optarg; break;
  1526 #else
  1527                 fprintf(stderr, "mp3: OSS output has not been compiled in!\n"); return false;
  1528 #endif
  1529       default:  return false;
  1530       }
  1531     }
  1532   return true;
  1533 }
  1534 
  1535 bool cPluginMp3::Initialize(void)
  1536 {
  1537   if(!CheckVDRVersion(1,4,5,"mp3")) return false;
  1538   plugin_name="mp3";
  1539 #if APIVERSNUM < 10507
  1540   i18n_name="mp3";
  1541 #else
  1542   i18n_name="vdr-mp3";
  1543 #endif
  1544   MP3Sources.Load(AddDirectory(ConfigDirectory(sourcesSub),"mp3sources.conf"));
  1545   if(MP3Sources.Count()<1) {
  1546      esyslog("ERROR: you should have defined at least one source in mp3sources.conf");
  1547      fprintf(stderr,"No source(s) defined in mp3sources.conf\n");
  1548      return false;
  1549      }
  1550   InfoCache.Load();
  1551 #if APIVERSNUM < 10507
  1552   RegisterI18n(Phrases);
  1553 #endif
  1554   mgr=new cPlayManager;
  1555   if(!mgr) {
  1556     esyslog("ERROR: creating playmanager failed");
  1557     fprintf(stderr,"Creating playmanager failed\n");
  1558     return false;
  1559     }
  1560   d(printf("mp3: using %s\n",mad_version))
  1561   d(printf("mp3: compiled with %s\n",MAD_VERSION))
  1562   return true;
  1563 }
  1564 
  1565 void cPluginMp3::Housekeeping(void)
  1566 {
  1567   InfoCache.Save();
  1568 }
  1569 
  1570 const char *cPluginMp3::MainMenuEntry(void)
  1571 {
  1572   return MP3Setup.HideMainMenu ? 0 : tr(MAINMENUENTRY);
  1573 }
  1574 
  1575 cOsdObject *cPluginMp3::MainMenuAction(void)
  1576 {
  1577   return new cMenuMP3;
  1578 }
  1579 
  1580 cMenuSetupPage *cPluginMp3::SetupMenu(void)
  1581 {
  1582   return new cMenuSetupMP3;
  1583 }
  1584 
  1585 bool cPluginMp3::SetupParse(const char *Name, const char *Value)
  1586 {
  1587   if      (!strcasecmp(Name, "InitLoopMode"))     MP3Setup.InitLoopMode    = atoi(Value);
  1588   else if (!strcasecmp(Name, "InitShuffleMode"))  MP3Setup.InitShuffleMode = atoi(Value);
  1589   else if (!strcasecmp(Name, "AudioMode"))        MP3Setup.AudioMode       = atoi(Value);
  1590   else if (!strcasecmp(Name, "BgrScan"))          MP3Setup.BgrScan         = atoi(Value);
  1591   else if (!strcasecmp(Name, "EditorMode"))       MP3Setup.EditorMode      = atoi(Value);
  1592   else if (!strcasecmp(Name, "DisplayMode"))      MP3Setup.DisplayMode     = atoi(Value);
  1593   else if (!strcasecmp(Name, "BackgrMode"))       MP3Setup.BackgrMode      = atoi(Value);
  1594   else if (!strcasecmp(Name, "MenuMode"))         MP3Setup.MenuMode        = atoi(Value);
  1595   else if (!strcasecmp(Name, "TargetLevel"))      MP3Setup.TargetLevel     = atoi(Value);
  1596   else if (!strcasecmp(Name, "LimiterLevel"))     MP3Setup.LimiterLevel    = atoi(Value);
  1597   else if (!strcasecmp(Name, "Only48kHz"))        MP3Setup.Only48kHz       = atoi(Value);
  1598   else if (!strcasecmp(Name, "UseProxy"))         MP3Setup.UseProxy        = atoi(Value);
  1599   else if (!strcasecmp(Name, "ProxyHost"))        strn0cpy(MP3Setup.ProxyHost,Value,MAX_HOSTNAME);
  1600   else if (!strcasecmp(Name, "ProxyPort"))        MP3Setup.ProxyPort       = atoi(Value);
  1601   else if (!strcasecmp(Name, "UseCddb"))          MP3Setup.UseCddb         = atoi(Value);
  1602   else if (!strcasecmp(Name, "CddbHost"))         strn0cpy(MP3Setup.CddbHost,Value,MAX_HOSTNAME);
  1603   else if (!strcasecmp(Name, "CddbPort"))         MP3Setup.CddbPort        = atoi(Value);
  1604   else if (!strcasecmp(Name, "AbortAtEOL"))       MP3Setup.AbortAtEOL      = atoi(Value);
  1605   else if (!strcasecmp(Name, "AudioOutMode")) {
  1606     MP3Setup.AudioOutMode = atoi(Value);
  1607 #ifndef WITH_OSS
  1608     if(MP3Setup.AudioOutMode==AUDIOOUTMODE_OSS) {
  1609       esyslog("WARNING: AudioOutMode OSS not supported, falling back to DVB");
  1610       MP3Setup.AudioOutMode=AUDIOOUTMODE_DVB;
  1611       }
  1612 #endif
  1613     }
  1614   else if (!strcasecmp(Name, "ReplayDisplay"))      MP3Setup.ReplayDisplay = atoi(Value);
  1615   else if (!strcasecmp(Name, "HideMainMenu"))       MP3Setup.HideMainMenu  = atoi(Value);
  1616   else if (!strcasecmp(Name, "KeepSelect"))         MP3Setup.KeepSelect    = atoi(Value);
  1617   else if (!strcasecmp(Name, "TitleArtistOrder"))   MP3Setup.TitleArtistOrder = atoi(Value);
  1618   else return false;
  1619   return true;
  1620 }
  1621 
  1622 bool cPluginMp3::ExternalPlay(const char *path, bool test)
  1623 {
  1624   char real[PATH_MAX+1];
  1625   if(realpath(path,real)) {
  1626     cFileSource *src=MP3Sources.FindSource(real);
  1627     if(src) {
  1628       cFileObj *item=new cFileObj(src,0,0,otFile);
  1629       if(item) {
  1630         item->SplitAndSet(real);
  1631         if(item->GuessType()) {
  1632           if(item->Exists()) {
  1633             cInstantPlayList *pl=new cInstantPlayList(item);
  1634             if(pl && pl->Load() && pl->Count()) {
  1635               if(!test) cMP3Control::SetPlayList(pl);
  1636               else delete pl;
  1637               delete item;
  1638               return true;
  1639               }
  1640             else dsyslog("MP3 service: error building playlist");
  1641             delete pl;
  1642             }
  1643           else dsyslog("MP3 service: cannot play '%s'",path);
  1644           }
  1645         else dsyslog("MP3 service: GuessType() failed for '%s'",path);
  1646         delete item;
  1647         }
  1648       }
  1649     else dsyslog("MP3 service: cannot find source for '%s', real '%s'",path,real);
  1650     }
  1651   else if(errno!=ENOENT && errno!=ENOTDIR)
  1652     esyslog("ERROR: realpath: %s: %s",path,strerror(errno));
  1653   return false;
  1654 }
  1655 
  1656 bool cPluginMp3::Service(const char *Id, void *Data)
  1657 {
  1658   if(!strcasecmp(Id,"MP3-Play-v1")) {
  1659     if(Data) {
  1660       struct MPlayerServiceData *msd=(struct MPlayerServiceData *)Data;
  1661       msd->result=ExternalPlay(msd->data.filename,false);
  1662       }
  1663     return true;
  1664     }
  1665   else if(!strcasecmp(Id,"MP3-Test-v1")) {
  1666     if(Data) {
  1667       struct MPlayerServiceData *msd=(struct MPlayerServiceData *)Data;
  1668       msd->result=ExternalPlay(msd->data.filename,true);
  1669       }
  1670     return true;
  1671     }
  1672   return false;
  1673 }
  1674 
  1675 const char **cPluginMp3::SVDRPHelpPages(void)
  1676 {
  1677   static const char *HelpPages[] = {
  1678     "PLAY <filename>\n"
  1679     "    Triggers playback of file 'filename'.",
  1680     "TEST <filename>\n"
  1681     "    Tests is playback of file 'filename' is possible.",
  1682     "CURR\n"
  1683     "    Returns filename of song currently being replayed.",
  1684     NULL
  1685     };
  1686   return HelpPages;
  1687 }
  1688 
  1689 cString cPluginMp3::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
  1690 {
  1691   if(!strcasecmp(Command,"PLAY")) {
  1692     if(*Option) {
  1693       if(ExternalPlay(Option,false)) return "Playback triggered";
  1694       else { ReplyCode=550; return "Playback failed"; }
  1695       }
  1696     else { ReplyCode=501; return "Missing filename"; }
  1697     }
  1698   else if(!strcasecmp(Command,"TEST")) {
  1699     if(*Option) {
  1700       if(ExternalPlay(Option,true)) return "Playback possible";
  1701       else { ReplyCode=550; return "Playback not possible"; }
  1702       }
  1703     else { ReplyCode=501; return "Missing filename"; }
  1704     }
  1705   else if(!strcasecmp(Command,"CURR")) {
  1706     cControl *control=cControl::Control();
  1707     if(control && typeid(*control)==typeid(cMP3Control)) {
  1708       cMP3PlayInfo mode;
  1709       if(mgr->Info(-1,&mode)) return mode.Filename;
  1710       else return "<unknown>";
  1711       }
  1712     else { ReplyCode=550; return "No running playback"; }
  1713     }
  1714   return NULL;
  1715 }
  1716 
  1717 VDRPLUGINCREATOR(cPluginMp3); // Don't touch this!