filebrowser now ends up in previous directory again after playing a dvd stucture...
[enigma2.git] / lib / python / Plugins / Extensions / DVDPlayer / src / servicedvd.cpp
index 12d1405..da7bc5f 100644 (file)
@@ -1,8 +1,8 @@
 /* yes, it's dvd  */
-#include "servicedvd.h"
 #include <lib/base/eerror.h>
 #include <lib/base/object.h>
 #include <lib/base/ebase.h>
+#include <lib/base/nconfig.h>
 #include <string>
 #include <lib/service/service.h>
 #include <lib/base/init_num.h>
@@ -19,6 +19,7 @@
 extern "C" {
 #include <dreamdvd/ddvdlib.h>
 }
+#include "servicedvd.h"
 
 // eServiceFactoryDVD
 
@@ -54,7 +55,7 @@ RESULT eServiceFactoryDVD::play(const eServiceReference &ref, ePtr<iPlayableServ
        return 0;
 }
 
-RESULT eServiceFactoryDVD::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
+RESULT eServiceFactoryDVD::record(const eServiceReference &/*ref*/, ePtr<iRecordableService> &ptr)
 {
        ptr=0;
        return -1;
@@ -67,7 +68,7 @@ RESULT eServiceFactoryDVD::list(const eServiceReference &, ePtr<iListableService
 }
 
 
-RESULT eServiceFactoryDVD::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
+RESULT eServiceFactoryDVD::info(const eServiceReference &/*ref*/, ePtr<iStaticServiceInformation> &ptr)
 {
        ptr=0;
        return -1;
@@ -93,17 +94,28 @@ eServiceDVD::eServiceDVD(const char *filename):
        m_sn(eApp, ddvd_get_messagepipe_fd(m_ddvdconfig), eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Error|eSocketNotifier::Hungup),
        m_pump(eApp, 1)
 {
+       std::string aspect;
        eDebug("SERVICEDVD construct!");
        // create handle
        ddvd_set_dvd_path(m_ddvdconfig, filename);
        ddvd_set_ac3thru(m_ddvdconfig, 0);
        ddvd_set_language(m_ddvdconfig, "de");
-       ddvd_set_video(m_ddvdconfig, DDVD_16_9, DDVD_PAL);
+
+       if (ePythonConfigQuery::getConfigValue("config.av.aspect", aspect) != 0)
+               aspect = "16_9";
+       if (aspect == "4_3_letterbox")
+               ddvd_set_video(m_ddvdconfig, DDVD_4_3_LETTERBOX, DDVD_PAL);
+       else if (aspect == "4_3_panscan")
+               ddvd_set_video(m_ddvdconfig, DDVD_4_3_PAN_SCAN, DDVD_PAL);
+       else
+               ddvd_set_video(m_ddvdconfig, DDVD_16_9, DDVD_PAL);
+
        ddvd_set_lfb(m_ddvdconfig, (unsigned char *)m_pixmap->surface->data, 720, 576, 4, 720*4);
        CONNECT(m_sn.activated, eServiceDVD::gotMessage);
        CONNECT(m_pump.recv_msg, eServiceDVD::gotThreadMessage);
        strcpy(m_ddvd_titlestring,"");
        m_cue_pts = 0;
+       pause();
 }
 
 void eServiceDVD::gotThreadMessage(const int &msg)
@@ -117,7 +129,7 @@ void eServiceDVD::gotThreadMessage(const int &msg)
        }
 }
 
-void eServiceDVD::gotMessage(int what)
+void eServiceDVD::gotMessage(int /*what*/)
 {
        switch(ddvd_get_next_message(m_ddvdconfig,1))
        {
@@ -205,20 +217,13 @@ void eServiceDVD::gotMessage(int what)
                {
                        static struct ddvd_time last_info;
                        struct ddvd_time info;
-                       eDebug("DVD_SHOWOSD_TIME!");
+//                     eDebug("DVD_SHOWOSD_TIME!");
                        ddvd_get_last_time(m_ddvdconfig, &info);
-                       int spu_id;
-                       uint16_t spu_lang;
-                       ddvd_get_last_spu(m_ddvdconfig, &spu_id, &spu_lang);
                        if ( info.pos_chapter != last_info.pos_chapter )
-                       {
                                m_event(this, evUser+8); // chapterUpdated
-                       }
                        if ( info.pos_title != last_info.pos_title )
-                       {
                                m_event(this, evUser+9); // titleUpdated
-                       }
-                       ddvd_get_last_time(m_ddvdconfig, &last_info);
+                       memcpy(&last_info, &info, sizeof(struct ddvd_time));
                        break;
                }
                case DDVD_SHOWOSD_TITLESTRING:
@@ -226,6 +231,8 @@ void eServiceDVD::gotMessage(int what)
                        ddvd_get_title_string(m_ddvdconfig, m_ddvd_titlestring);
                        eDebug("DDVD_SHOWOSD_TITLESTRING: %s",m_ddvd_titlestring);
                        loadCuesheet();
+                       if (!m_cue_pts)
+                               unpause();
                        m_event(this, evStart);
                        break;
                }
@@ -250,6 +257,7 @@ eServiceDVD::~eServiceDVD()
 {
        eDebug("SERVICEDVD destruct!");
        kill();
+       saveCuesheet();
        ddvd_close(m_ddvdconfig);
 }
 
@@ -277,22 +285,11 @@ RESULT eServiceDVD::stop()
        eDebug("DVD: stop %s", m_filename.c_str());
        m_state = stStopped;
        ddvd_send_key(m_ddvdconfig, DDVD_KEY_EXIT);
-       struct ddvd_time info;
-       ddvd_get_last_time(m_ddvdconfig, &info);
-       if ( info.pos_chapter < info.end_chapter )
-       {
-               pts_t pos;
-               pos = info.pos_hours * 3600;
-               pos += info.pos_minutes * 60;
-               pos += info.pos_seconds;
-               pos *= 90000;
-               m_cue_pts = pos;
-       }
-       saveCuesheet();
+
        return 0;
 }
 
-RESULT eServiceDVD::setTarget(int target)
+RESULT eServiceDVD::setTarget(int /*target*/)
 {
        return -1;
 }
@@ -322,7 +319,7 @@ RESULT eServiceDVD::keys(ePtr<iServiceKeys> &ptr)
 }
 
        // iPausableService
-RESULT eServiceDVD::setSlowMotion(int ratio)
+RESULT eServiceDVD::setSlowMotion(int /*ratio*/)
 {
        return -1;
 }
@@ -406,41 +403,31 @@ RESULT eServiceDVD::getName(std::string &name)
 int eServiceDVD::getInfo(int w)
 {
        switch (w)
-               {
-               case sUser:
-               case sArtist:
-               case sAlbum:
-                       return resIsPyObject;  // then getInfoObject should be called
-               case sComment:
-               case sTracknumber:
-               case sGenre:
-                       return resIsString;  // then getInfoString should be called
-               case evUser+8:
+       {
+               case sCurrentChapter:
                {
                        struct ddvd_time info;
                        ddvd_get_last_time(m_ddvdconfig, &info);
                        return info.pos_chapter;
                }
-               case evUser+80:
+               case sTotalChapters:
                {
                        struct ddvd_time info;
                        ddvd_get_last_time(m_ddvdconfig, &info);
                        return info.end_chapter;
                }
-
-               case evUser+9:
+               case sCurrentTitle:
                {
                        struct ddvd_time info;
                        ddvd_get_last_time(m_ddvdconfig, &info);
                        return info.pos_title;
                }
-               case evUser+90:
+               case sTotalTitles:
                {
                        struct ddvd_time info;
                        ddvd_get_last_time(m_ddvdconfig, &info);
                        return info.end_title;
                }
-
                case sTXTPID:   // we abuse HAS_TELEXT icon in InfoBar to signalize subtitles status
                {
                        int spu_id;
@@ -448,6 +435,9 @@ int eServiceDVD::getInfo(int w)
                        ddvd_get_last_spu(m_ddvdconfig, &spu_id, &spu_lang);
                        return spu_id;
                }
+               case sUser+6:
+               case sUser+7:
+                       return resIsPyObject;
                default:
                        return resNA;
        }
@@ -457,64 +447,72 @@ std::string eServiceDVD::getInfoString(int w)
 {
        switch(w)
        {
-               case evUser+7: {
-                       int spu_id;
-                       uint16_t spu_lang;
-                       ddvd_get_last_spu(m_ddvdconfig, &spu_id, &spu_lang);
-                       unsigned char spu_string[3]={spu_lang >> 8, spu_lang, 0};
-                       char osd[100];
-                       if (spu_id == -1)
-                               sprintf(osd,"");
-                       else
-                               sprintf(osd,"%d - %s",spu_id+1,spu_string);
-//                     lbo_changed=1;
-                       return osd;
-                       }
-               case evUser+6:
-                       {
+               case sServiceref:
+                       break;
+               default:
+                       eDebug("unhandled getInfoString(%d)", w);
+       }
+       return "";
+}
+
+PyObject *eServiceDVD::getInfoObject(int w)
+{
+       switch(w)
+       {
+               case sUser+6:
+               {
+                       ePyObject tuple = PyTuple_New(3);
                        int audio_id,audio_type;
                        uint16_t audio_lang;
                        ddvd_get_last_audio(m_ddvdconfig, &audio_id, &audio_lang, &audio_type);
                        char audio_string[3]={audio_lang >> 8, audio_lang, 0};
-                       char audio_form[5];
+                       PyTuple_SetItem(tuple, 0, PyInt_FromLong(audio_id+1));
+                       PyTuple_SetItem(tuple, 1, PyString_FromString(audio_string));
                        switch(audio_type)
                        {
                                case DDVD_MPEG:
-                                       sprintf(audio_form,"MPEG");
+                                       PyTuple_SetItem(tuple, 2, PyString_FromString("MPEG"));
                                        break;
                                case DDVD_AC3:
-                                       sprintf(audio_form,"AC3");
+                                       PyTuple_SetItem(tuple, 2, PyString_FromString("AC3"));
                                        break;
                                case DDVD_DTS:
-                                       sprintf(audio_form,"DTS");
+                                       PyTuple_SetItem(tuple, 2, PyString_FromString("DTS"));
                                        break;
                                case DDVD_LPCM:
-                                       sprintf(audio_form,"LPCM");
+                                       PyTuple_SetItem(tuple, 2, PyString_FromString("LPCM"));
                                        break;
                                default:
-                                       sprintf(audio_form,"-");
+                                       PyTuple_SetItem(tuple, 2, PyString_FromString(""));
                        }
-                       char osd[100];
-                       sprintf(osd,"%d - %s (%s)",audio_id+1,audio_string,audio_form);
-                       return osd;
+                       return tuple;
+               }
+               case sUser+7:
+               {
+                       ePyObject tuple = PyTuple_New(2);
+                       int spu_id;
+                       uint16_t spu_lang;
+                       ddvd_get_last_spu(m_ddvdconfig, &spu_id, &spu_lang);
+                       char spu_string[3]={spu_lang >> 8, spu_lang, 0};
+                       if (spu_id == -1)
+                       {
+                               PyTuple_SetItem(tuple, 0, PyInt_FromLong(0));
+                               PyTuple_SetItem(tuple, 1, PyString_FromString(""));
                        }
-               default:
-                       eDebug("unhandled getInfoString(%d)", w);
-       }
-       return "";
-}
-
-PyObject *eServiceDVD::getInfoObject(int w)
-{
-       switch(w)
-       {
+                       else
+                       {
+                               PyTuple_SetItem(tuple, 0, PyInt_FromLong(spu_id+1));
+                               PyTuple_SetItem(tuple, 1, PyString_FromString(spu_string));
+                       }                               
+                       return tuple;
+               }
                default:
                        eDebug("unhandled getInfoObject(%d)", w);
        }
        Py_RETURN_NONE;
 }
 
-RESULT eServiceDVD::enableSubtitles(eWidget *parent, SWIG_PYOBJECT(ePyObject) entry)
+RESULT eServiceDVD::enableSubtitles(eWidget *parent, SWIG_PYOBJECT(ePyObject) /*entry*/)
 {
        if (m_subtitle_widget)
                delete m_subtitle_widget;
@@ -526,7 +524,7 @@ RESULT eServiceDVD::enableSubtitles(eWidget *parent, SWIG_PYOBJECT(ePyObject) en
        return 0;
 }
 
-RESULT eServiceDVD::disableSubtitles(eWidget *parent)
+RESULT eServiceDVD::disableSubtitles(eWidget */*parent*/)
 {
        delete m_subtitle_widget;
        m_subtitle_widget = 0;
@@ -559,15 +557,12 @@ RESULT eServiceDVD::getLength(pts_t &len)
 
 RESULT eServiceDVD::seekTo(pts_t to)
 {
-       struct ddvd_time info;
-       to /= 90000;
-       int cur;
-       ddvd_get_last_time(m_ddvdconfig, &info);
-       cur = info.pos_hours * 3600;
-       cur += info.pos_minutes * 60;
-       cur += info.pos_seconds;
-       eDebug("seekTo %lld, cur %d, diff %lld", to, cur, cur - to);
-       ddvd_skip_seconds(m_ddvdconfig, cur - to);
+       eDebug("eServiceDVD::seekTo(%lld)",to);
+       if ( to > 0 )
+       {
+               eDebug("set_resume_pos: resume_info.title=%d, chapter=%d, block=%lu, audio_id=%d, audio_lock=%d, spu_id=%d, spu_lock=%d",m_resume_info.title,m_resume_info.chapter,m_resume_info.block,m_resume_info.audio_id, m_resume_info.audio_lock, m_resume_info.spu_id, m_resume_info.spu_lock);
+               ddvd_set_resume_pos(m_ddvdconfig, m_resume_info);
+       }
        return 0;
 }
 
@@ -607,7 +602,7 @@ RESULT eServiceDVD::seekChapter(int chapter)
        return 0;
 }
 
-RESULT eServiceDVD::setTrickmode(int trick)
+RESULT eServiceDVD::setTrickmode(int /*trick*/)
 {
        return -1;
 }
@@ -687,20 +682,21 @@ PyObject *eServiceDVD::getCutList()
        return list;
 }
 
-void eServiceDVD::setCutList(ePyObject list)
+void eServiceDVD::setCutList(ePyObject /*list*/)
 {
 }
 
-void eServiceDVD::setCutListEnable(int enable)
+void eServiceDVD::setCutListEnable(int /*enable*/)
 {
 }
 
 void eServiceDVD::loadCuesheet()
 {
-       eDebug("eServiceDVD::loadCuesheet()");
        char filename[128];
        if ( m_ddvd_titlestring[0] != '\0' )
                snprintf(filename, 128, "/home/root/dvd-%s.cuts", m_ddvd_titlestring);
+       else
+               snprintf(filename, 128, "%s/dvd.cuts", m_filename.c_str());
 
        eDebug("eServiceDVD::loadCuesheet() filename=%s",filename);
 
@@ -708,38 +704,70 @@ void eServiceDVD::loadCuesheet()
 
        if (f)
        {
-               eDebug("loading cuts..");
                unsigned long long where;
                unsigned int what;
 
                if (!fread(&where, sizeof(where), 1, f))
                        return;
-
                if (!fread(&what, sizeof(what), 1, f))
                        return;
-                       
 #if BYTE_ORDER == LITTLE_ENDIAN
                where = bswap_64(where);
 #endif
                what = ntohl(what);
 
-               m_cue_pts = where;
+               if (!fread(&m_resume_info, sizeof(struct ddvd_resume), 1, f))
+                       return;
+               if (!fread(&what, sizeof(what), 1, f))
+                       return;
+
+               what = ntohl(what);
+               if (what != 4 )
+                       return;
+
+               m_cue_pts = where;
+
                fclose(f);
        } else
                eDebug("cutfile not found!");
 
-       eDebug("eServiceDVD::loadCuesheet() pts=%lld",m_cue_pts);
-
        if (m_cue_pts)
+       {
                m_event((iPlayableService*)this, evCuesheetChanged);
+               eDebug("eServiceDVD::loadCuesheet() pts=%lld",m_cue_pts);
+       }
 }
 
 void eServiceDVD::saveCuesheet()
 {
-       eDebug("eServiceDVD::saveCuesheet() pts=%lld",m_cue_pts);
+       eDebug("eServiceDVD::saveCuesheet()");
+
+       struct ddvd_resume resume_info;
+       ddvd_get_resume_pos(m_ddvdconfig, &resume_info);
+
+       if (resume_info.title)
+       {
+               struct ddvd_time info;
+               ddvd_get_last_time(m_ddvdconfig, &info);
+               pts_t pos;
+               pos = info.pos_hours * 3600;
+               pos += info.pos_minutes * 60;
+               pos += info.pos_seconds;
+               pos *= 90000;
+               m_cue_pts = pos;
+               eDebug("ddvd_get_resume_pos resume_info.title=%d, chapter=%d, block=%lu, audio_id=%d, audio_lock=%d, spu_id=%d, spu_lock=%d  (pts=%llu)",resume_info.title,resume_info.chapter,resume_info.block,resume_info.audio_id, resume_info.audio_lock, resume_info.spu_id, resume_info.spu_lock,m_cue_pts);
+       }
+       else
+       {
+               eDebug("we're in a menu or somewhere else funny. so save cuesheet with pts=0");
+               m_cue_pts = 0;
+       }
+
        char filename[128];
        if ( m_ddvd_titlestring[0] != '\0' )
                snprintf(filename, 128, "/home/root/dvd-%s.cuts", m_ddvd_titlestring);
+       else
+               snprintf(filename, 128, "%s/dvd.cuts", m_filename.c_str());
        
        FILE *f = fopen(filename, "wb");
 
@@ -748,16 +776,19 @@ void eServiceDVD::saveCuesheet()
                unsigned long long where;
                int what;
 
-               {
 #if BYTE_ORDER == BIG_ENDIAN
-                       where = m_cue_pts;
+               where = m_cue_pts;
 #else
-                       where = bswap_64(m_cue_pts);
+               where = bswap_64(m_cue_pts);
 #endif
-                       what = 3;
-                       fwrite(&where, sizeof(where), 1, f);
-                       fwrite(&what, sizeof(what), 1, f);
-               }
+               what = htonl(3);
+               fwrite(&where, sizeof(where), 1, f);
+               fwrite(&what, sizeof(what), 1, f);
+               
+               what = htonl(4);
+               fwrite(&resume_info, sizeof(struct ddvd_resume), 1, f);
+               fwrite(&what, sizeof(what), 1, f);
+
                fclose(f);
        }
 }