4a85a0208082e8e42259272e6c072bdde8b6cac4
[enigma2.git] / lib / python / Plugins / Extensions / DVDPlayer / src / servicedvd.cpp
1 /* yes, it's dvd  */
2 #include <lib/base/eerror.h>
3 #include <lib/base/object.h>
4 #include <lib/base/ebase.h>
5 #include <lib/base/nconfig.h>
6 #include <string>
7 #include <lib/service/service.h>
8 #include <lib/base/init_num.h>
9 #include <lib/base/init.h>
10 #include <lib/gui/esubtitle.h>
11 #include <lib/gdi/gpixmap.h>
12
13 #include <byteswap.h>
14 #include <netinet/in.h>
15 #ifndef BYTE_ORDER
16 #error no byte order defined!
17 #endif
18
19 extern "C" {
20 #include <dreamdvd/ddvdlib.h>
21 }
22 #include "servicedvd.h"
23
24 // eServiceFactoryDVD
25
26 eServiceFactoryDVD::eServiceFactoryDVD()
27 {
28         ePtr<eServiceCenter> sc;
29         
30         eServiceCenter::getPrivInstance(sc);
31         if (sc)
32         {
33                 std::list<std::string> extensions;
34                 extensions.push_back("iso");
35                 sc->addServiceFactory(eServiceFactoryDVD::id, this, extensions);
36         }
37 }
38
39 eServiceFactoryDVD::~eServiceFactoryDVD()
40 {
41         ePtr<eServiceCenter> sc;
42         
43         eServiceCenter::getPrivInstance(sc);
44         if (sc)
45                 sc->removeServiceFactory(eServiceFactoryDVD::id);
46 }
47
48 DEFINE_REF(eServiceFactoryDVD)
49
50         // iServiceHandler
51 RESULT eServiceFactoryDVD::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
52 {
53                 // check resources...
54         ptr = new eServiceDVD(ref.path.c_str());
55         return 0;
56 }
57
58 RESULT eServiceFactoryDVD::record(const eServiceReference &/*ref*/, ePtr<iRecordableService> &ptr)
59 {
60         ptr=0;
61         return -1;
62 }
63
64 RESULT eServiceFactoryDVD::list(const eServiceReference &, ePtr<iListableService> &ptr)
65 {
66         ptr=0;
67         return -1;
68 }
69
70
71 RESULT eServiceFactoryDVD::info(const eServiceReference &/*ref*/, ePtr<iStaticServiceInformation> &ptr)
72 {
73         ptr=0;
74         return -1;
75 }
76
77 RESULT eServiceFactoryDVD::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
78 {
79         ptr = 0;
80         return -1;
81 }
82
83 // eServiceDVD
84
85 DEFINE_REF(eServiceDVD);
86
87 eServiceDVD::eServiceDVD(const char *filename):
88         m_filename(filename),
89         m_ddvdconfig(ddvd_create()),
90         m_subtitle_widget(0),
91         m_state(stIdle),
92         m_current_trick(0),
93         m_pump(eApp, 1)
94 {
95         int aspect = DDVD_16_9; 
96         int policy = DDVD_PAN_SCAN;
97
98         char tmp[255];
99         ssize_t rd;
100
101         m_sn = eSocketNotifier::create(eApp, ddvd_get_messagepipe_fd(m_ddvdconfig), eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Error|eSocketNotifier::Hungup);
102         eDebug("SERVICEDVD construct!");
103         // create handle
104         ddvd_set_dvd_path(m_ddvdconfig, filename);
105         ddvd_set_ac3thru(m_ddvdconfig, 0);
106         ddvd_set_language(m_ddvdconfig, "de");
107
108         int fd = open("/proc/stb/video/aspect", O_RDONLY);
109         if (fd > -1)
110         {
111                 rd = read(fd, tmp, 255);
112                 if (rd > 2 && !strncmp(tmp, "4:3", 3))
113                         aspect = DDVD_4_3;
114                 else if (rd > 4 && !strncmp(tmp, "16:10", 5))
115                         aspect = DDVD_16_10;
116                 close(fd);
117         }
118
119         fd = open("/proc/stb/video/policy", O_RDONLY);
120         if (fd > -1)
121         {
122                 rd = read(fd, tmp, 255);
123                 if (rd > 6 && !strncmp(tmp, "bestfit", 7))
124                         aspect = DDVD_JUSTSCALE;
125                 else if (rd > 8 && !strncmp(tmp, "letterbox", 9))
126                         aspect = DDVD_LETTERBOX;
127                 close(fd);
128         }
129
130         ddvd_set_video(m_ddvdconfig, aspect, policy, DDVD_PAL /*unused*/);
131
132         CONNECT(m_sn->activated, eServiceDVD::gotMessage);
133         CONNECT(m_pump.recv_msg, eServiceDVD::gotThreadMessage);
134         strcpy(m_ddvd_titlestring,"");
135         m_cue_pts = 0;
136         pause();
137 }
138
139 void eServiceDVD::gotThreadMessage(const int &msg)
140 {
141         switch(msg)
142         {
143         case 1: // thread stopped
144                 m_state = stStopped;
145                 m_event(this, evStopped);
146                 break;
147         }
148 }
149
150 void eServiceDVD::gotMessage(int /*what*/)
151 {
152         switch(ddvd_get_next_message(m_ddvdconfig,1))
153         {
154                 case DDVD_COLORTABLE_UPDATE:
155                 {
156 /*
157                         struct ddvd_color ctmp[4];
158                         ddvd_get_last_colortable(ddvdconfig, ctmp);
159                         int i=0;
160                         while (i < 4)
161                         {
162                                 rd1[252+i]=ctmp[i].red;
163                                 bl1[252+i]=ctmp[i].blue;
164                                 gn1[252+i]=ctmp[i].green;
165                                 tr1[252+i]=ctmp[i].trans;
166                                 i++;
167                         }
168                         if(ioctl(fb, FBIOPUTCMAP, &colormap) == -1)
169                         {
170                                 printf("Framebuffer: <FBIOPUTCMAP failed>\n");
171                                 return 1;
172                         }
173 */
174                         eDebug("no support for 8bpp framebuffer in dvdplayer yet!");
175                         break;
176                 }
177                 case DDVD_SCREEN_UPDATE:
178                         eDebug("DVD_SCREEN_UPDATE!");
179                         if (m_subtitle_widget) {
180                                 int x1,x2,y1,y2;
181                                 ddvd_get_last_blit_area(m_ddvdconfig, &x1, &x2, &y1, &y2);
182                                 m_subtitle_widget->setPixmap(m_pixmap, eRect(x1, y1, x2-x1, y2-y1));
183                         }
184                         break;
185                 case DDVD_SHOWOSD_STATE_PLAY:
186                 {
187                         eDebug("DVD_SHOWOSD_STATE_PLAY!");
188                         m_current_trick = 0;
189                         m_event(this, evUser+1);
190                         break;
191                 }
192                 case DDVD_SHOWOSD_STATE_PAUSE:
193                 {
194                         eDebug("DVD_SHOWOSD_STATE_PAUSE!");
195                         m_event(this, evUser+2);
196                         break;
197                 }
198                 case DDVD_SHOWOSD_STATE_FFWD:
199                 {
200                         eDebug("DVD_SHOWOSD_STATE_FFWD!");
201                         m_event(this, evUser+3);
202                         break;
203                 }
204                 case DDVD_SHOWOSD_STATE_FBWD:
205                 {
206                         eDebug("DVD_SHOWOSD_STATE_FBWD!");
207                         m_event(this, evUser+4);
208                         break;
209                 }
210                 case DDVD_SHOWOSD_STRING:
211                 {
212                         eDebug("DVD_SHOWOSD_STRING!");
213                         m_event(this, evUser+5);
214                         break;
215                 }
216                 case DDVD_SHOWOSD_AUDIO:
217                 {
218                         eDebug("DVD_SHOWOSD_STRING!");
219                         m_event(this, evUser+6);
220                         break;
221                 }
222                 case DDVD_SHOWOSD_SUBTITLE:
223                 {
224                         eDebug("DVD_SHOWOSD_SUBTITLE!");
225                         m_event((iPlayableService*)this, evUpdatedInfo);
226                         m_event(this, evUser+7);
227                         break;
228                 }
229                 case DDVD_EOF_REACHED:
230                         eDebug("DVD_EOF_REACHED!");
231                         m_event(this, evEOF);
232                         break;
233                 case DDVD_SOF_REACHED:
234                         eDebug("DVD_SOF_REACHED!");
235                         m_event(this, evSOF);
236                         break;
237                 case DDVD_SHOWOSD_TIME:
238                 {
239                         static struct ddvd_time last_info;
240                         struct ddvd_time info;
241 //                      eDebug("DVD_SHOWOSD_TIME!");
242                         ddvd_get_last_time(m_ddvdconfig, &info);
243                         if ( info.pos_chapter != last_info.pos_chapter )
244                                 m_event(this, evUser+8); // chapterUpdated
245                         if ( info.pos_title != last_info.pos_title )
246                                 m_event(this, evUser+9); // titleUpdated
247                         memcpy(&last_info, &info, sizeof(struct ddvd_time));
248                         break;
249                 }
250                 case DDVD_SHOWOSD_TITLESTRING:
251                 {
252                         ddvd_get_title_string(m_ddvdconfig, m_ddvd_titlestring);
253                         eDebug("DDVD_SHOWOSD_TITLESTRING: %s",m_ddvd_titlestring);
254                         loadCuesheet();
255                         if (!m_cue_pts)
256                                 unpause();
257                         m_event(this, evStart);
258                         break;
259                 }
260                 case DDVD_MENU_OPENED:
261                         eDebug("DVD_MENU_OPENED!");
262                         m_state = stMenu;
263                         m_event(this, evSeekableStatusChanged);
264                         m_event(this, evUser+11);
265                         break;
266                 case DDVD_MENU_CLOSED:
267                         eDebug("DVD_MENU_CLOSED!");
268                         m_state = stRunning;
269                         m_event(this, evSeekableStatusChanged);
270                         m_event(this, evUser+12);
271                         break;
272                 default:
273                         break;
274         }
275 }
276
277 eServiceDVD::~eServiceDVD()
278 {
279         eDebug("SERVICEDVD destruct!");
280         kill();
281         saveCuesheet();
282         ddvd_close(m_ddvdconfig);
283 }
284
285 RESULT eServiceDVD::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
286 {
287         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
288         return 0;
289 }
290
291 RESULT eServiceDVD::start()
292 {
293         assert(m_state == stIdle);
294         m_state = stRunning;
295         eDebug("eServiceDVD starting");
296 //      m_event(this, evStart);
297         return 0;
298 }
299
300 RESULT eServiceDVD::stop()
301 {
302         assert(m_state != stIdle);
303         if (m_state == stStopped)
304                 return -1;
305         eDebug("DVD: stop %s", m_filename.c_str());
306         m_state = stStopped;
307         ddvd_send_key(m_ddvdconfig, DDVD_KEY_EXIT);
308
309         return 0;
310 }
311
312 RESULT eServiceDVD::setTarget(int /*target*/)
313 {
314         return -1;
315 }
316
317 RESULT eServiceDVD::pause(ePtr<iPauseableService> &ptr)
318 {
319         ptr=this;
320         return 0;
321 }
322
323 RESULT eServiceDVD::seek(ePtr<iSeekableService> &ptr)
324 {
325         ptr=this;
326         return 0;
327 }
328
329 RESULT eServiceDVD::subtitle(ePtr<iSubtitleOutput> &ptr)
330 {
331         ptr=this;
332         return 0;
333 }
334
335 RESULT eServiceDVD::keys(ePtr<iServiceKeys> &ptr)
336 {
337         ptr=this;
338         return 0;
339 }
340
341         // iPausableService
342 RESULT eServiceDVD::setSlowMotion(int /*ratio*/)
343 {
344         return -1;
345 }
346
347 RESULT eServiceDVD::setFastForward(int trick)
348 {
349         eDebug("setTrickmode(%d)", trick);
350         while (m_current_trick > trick && m_current_trick != -64)
351         {
352                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_FBWD);
353                 if (m_current_trick == 0)
354                         m_current_trick = -2;
355                 else if (m_current_trick > 0)
356                 {
357                         m_current_trick /= 2;
358                         if (abs(m_current_trick) == 1)
359                                 m_current_trick=0;
360                 }
361                 else
362                         m_current_trick *= 2;
363         }
364         while (m_current_trick < trick && m_current_trick != 64)
365         {
366                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_FFWD);
367                 if (m_current_trick == 0)
368                         m_current_trick = 2;
369                 else if (m_current_trick < 0)
370                 {
371                         m_current_trick /= 2;
372                         if (abs(m_current_trick) == 1)
373                                 m_current_trick=0;
374                 }
375                 else
376                         m_current_trick *= 2;
377         }
378         return 0;
379 }
380
381 RESULT eServiceDVD::pause()
382 {
383         eDebug("set pause!\n");
384         ddvd_send_key(m_ddvdconfig, DDVD_KEY_PAUSE);
385         return 0;
386 }
387
388 RESULT eServiceDVD::unpause()
389 {
390         eDebug("set unpause!\n");
391         ddvd_send_key(m_ddvdconfig, DDVD_KEY_PLAY);
392         return 0;
393 }
394
395 void eServiceDVD::thread()
396 {
397         eDebug("eServiceDVD dvd thread started");
398         hasStarted();
399         ddvd_run(m_ddvdconfig);
400 }
401
402 void eServiceDVD::thread_finished()
403 {
404         eDebug("eServiceDVD dvd thread finished");
405         m_pump.send(1); // inform main thread
406 }
407
408 RESULT eServiceDVD::info(ePtr<iServiceInformation>&i)
409 {
410         i = this;
411         return 0;
412 }
413
414 RESULT eServiceDVD::getName(std::string &name)
415 {
416         if ( m_ddvd_titlestring[0] != '\0' )
417                 name = m_ddvd_titlestring;
418         else
419                 name = m_filename;
420         return 0;
421 }
422
423 int eServiceDVD::getInfo(int w)
424 {
425         switch (w)
426         {
427                 case sCurrentChapter:
428                 {
429                         struct ddvd_time info;
430                         ddvd_get_last_time(m_ddvdconfig, &info);
431                         return info.pos_chapter;
432                 }
433                 case sTotalChapters:
434                 {
435                         struct ddvd_time info;
436                         ddvd_get_last_time(m_ddvdconfig, &info);
437                         return info.end_chapter;
438                 }
439                 case sCurrentTitle:
440                 {
441                         struct ddvd_time info;
442                         ddvd_get_last_time(m_ddvdconfig, &info);
443                         return info.pos_title;
444                 }
445                 case sTotalTitles:
446                 {
447                         struct ddvd_time info;
448                         ddvd_get_last_time(m_ddvdconfig, &info);
449                         return info.end_title;
450                 }
451                 case sTXTPID:   // we abuse HAS_TELEXT icon in InfoBar to signalize subtitles status
452                 {
453                         int spu_id;
454                         uint16_t spu_lang;
455                         ddvd_get_last_spu(m_ddvdconfig, &spu_id, &spu_lang);
456                         return spu_id;
457                 }
458                 case sUser+6:
459                 case sUser+7:
460                         return resIsPyObject;
461                 default:
462                         return resNA;
463         }
464 }
465
466 std::string eServiceDVD::getInfoString(int w)
467 {
468         switch(w)
469         {
470                 case sServiceref:
471                         break;
472                 default:
473                         eDebug("unhandled getInfoString(%d)", w);
474         }
475         return "";
476 }
477
478 PyObject *eServiceDVD::getInfoObject(int w)
479 {
480         switch(w)
481         {
482                 case sUser+6:
483                 {
484                         ePyObject tuple = PyTuple_New(3);
485                         int audio_id,audio_type;
486                         uint16_t audio_lang;
487                         ddvd_get_last_audio(m_ddvdconfig, &audio_id, &audio_lang, &audio_type);
488                         char audio_string[3]={audio_lang >> 8, audio_lang, 0};
489                         PyTuple_SetItem(tuple, 0, PyInt_FromLong(audio_id+1));
490                         PyTuple_SetItem(tuple, 1, PyString_FromString(audio_string));
491                         switch(audio_type)
492                         {
493                                 case DDVD_MPEG:
494                                         PyTuple_SetItem(tuple, 2, PyString_FromString("MPEG"));
495                                         break;
496                                 case DDVD_AC3:
497                                         PyTuple_SetItem(tuple, 2, PyString_FromString("AC3"));
498                                         break;
499                                 case DDVD_DTS:
500                                         PyTuple_SetItem(tuple, 2, PyString_FromString("DTS"));
501                                         break;
502                                 case DDVD_LPCM:
503                                         PyTuple_SetItem(tuple, 2, PyString_FromString("LPCM"));
504                                         break;
505                                 default:
506                                         PyTuple_SetItem(tuple, 2, PyString_FromString(""));
507                         }
508                         return tuple;
509                 }
510                 case sUser+7:
511                 {
512                         ePyObject tuple = PyTuple_New(2);
513                         int spu_id;
514                         uint16_t spu_lang;
515                         ddvd_get_last_spu(m_ddvdconfig, &spu_id, &spu_lang);
516                         char spu_string[3]={spu_lang >> 8, spu_lang, 0};
517                         if (spu_id == -1)
518                         {
519                                 PyTuple_SetItem(tuple, 0, PyInt_FromLong(0));
520                                 PyTuple_SetItem(tuple, 1, PyString_FromString(""));
521                         }
522                         else
523                         {
524                                 PyTuple_SetItem(tuple, 0, PyInt_FromLong(spu_id+1));
525                                 PyTuple_SetItem(tuple, 1, PyString_FromString(spu_string));
526                         }                               
527                         return tuple;
528                 }
529                 default:
530                         eDebug("unhandled getInfoObject(%d)", w);
531         }
532         Py_RETURN_NONE;
533 }
534
535 RESULT eServiceDVD::enableSubtitles(eWidget *parent, SWIG_PYOBJECT(ePyObject) /*entry*/)
536 {
537         if (m_subtitle_widget)
538                 delete m_subtitle_widget;
539
540         m_subtitle_widget = new eSubtitleWidget(parent);
541         m_subtitle_widget->resize(parent->size());
542
543         eSize size = parent->size();
544
545         if (!m_pixmap)
546         {
547                 m_pixmap = new gPixmap(size, 32);
548                 ddvd_set_lfb(m_ddvdconfig, (unsigned char *)m_pixmap->surface->data, size.width(), size.height(), 4, size.width()*4);
549                 run(); // start the thread
550         }
551
552         m_subtitle_widget->setZPosition(-1);
553         m_subtitle_widget->show();
554
555         return 0;
556 }
557
558 RESULT eServiceDVD::disableSubtitles(eWidget */*parent*/)
559 {
560         delete m_subtitle_widget;
561         m_subtitle_widget = 0;
562         return 0;
563 }
564
565 PyObject *eServiceDVD::getSubtitleList()
566 {
567         eDebug("eServiceDVD::getSubtitleList nyi");
568         Py_RETURN_NONE;
569 }
570
571 PyObject *eServiceDVD::getCachedSubtitle()
572 {
573         eDebug("eServiceDVD::getCachedSubtitle nyi");
574         Py_RETURN_NONE;
575 }
576
577 RESULT eServiceDVD::getLength(pts_t &len)
578 {
579 //      eDebug("eServiceDVD::getLength");
580         struct ddvd_time info;
581         ddvd_get_last_time(m_ddvdconfig, &info);
582         len = info.end_hours * 3600;
583         len += info.end_minutes * 60;
584         len += info.end_seconds;
585         len *= 90000;
586         return 0;
587 }
588
589 RESULT eServiceDVD::seekTo(pts_t to)
590 {
591         eDebug("eServiceDVD::seekTo(%lld)",to);
592         if ( to > 0 )
593         {
594                 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);
595                 ddvd_set_resume_pos(m_ddvdconfig, m_resume_info);
596         }
597         return 0;
598 }
599
600 RESULT eServiceDVD::seekRelative(int direction, pts_t to)
601 {
602         int seconds = to / 90000;
603         seconds *= direction;
604         eDebug("seekRelative %d %d", direction, seconds);
605         ddvd_skip_seconds(m_ddvdconfig, seconds);
606         return 0;
607 }
608
609 RESULT eServiceDVD::getPlayPosition(pts_t &pos)
610 {
611         struct ddvd_time info;
612         ddvd_get_last_time(m_ddvdconfig, &info);
613         pos = info.pos_hours * 3600;
614         pos += info.pos_minutes * 60;
615         pos += info.pos_seconds;
616 //      eDebug("getPlayPosition %lld", pos);
617         pos *= 90000;
618         return 0;
619 }
620
621 RESULT eServiceDVD::seekTitle(int title)
622 {
623         eDebug("setTitle %d", title);
624         ddvd_set_title(m_ddvdconfig, title);
625         return 0;
626 }
627
628 RESULT eServiceDVD::seekChapter(int chapter)
629 {
630         eDebug("setChapter %d", chapter);
631         if ( chapter > 0 )
632                 ddvd_set_chapter(m_ddvdconfig, chapter);
633         return 0;
634 }
635
636 RESULT eServiceDVD::setTrickmode(int /*trick*/)
637 {
638         return -1;
639 }
640
641 RESULT eServiceDVD::isCurrentlySeekable()
642 {
643         return m_state == stRunning;
644 }
645
646 RESULT eServiceDVD::keyPressed(int key)
647 {
648         switch(key)
649         {
650         case iServiceKeys::keyLeft:
651                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_LEFT);
652                 break;
653         case iServiceKeys::keyRight:
654                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_RIGHT);
655                 break;
656         case iServiceKeys::keyUp:
657                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_UP);
658                 break;
659         case iServiceKeys::keyDown:
660                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_DOWN);
661                 break;
662         case iServiceKeys::keyOk:
663                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_OK);
664                 break;
665         case iServiceKeys::keyUser:
666                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_AUDIO);
667                 break;
668         case iServiceKeys::keyUser+1:
669                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_SUBTITLE);
670                 break;
671         case iServiceKeys::keyUser+2:
672                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_AUDIOMENU);
673                 break;
674         case iServiceKeys::keyUser+3:
675                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_NEXT_CHAPTER);
676                 break;
677         case iServiceKeys::keyUser+4:
678                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_PREV_CHAPTER);
679                 break;
680         case iServiceKeys::keyUser+5:
681                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_NEXT_TITLE);
682                 break;
683         case iServiceKeys::keyUser+6:
684                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_PREV_TITLE);
685                 break;
686         case iServiceKeys::keyUser+7:
687                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_MENU);
688                 break;
689         default:
690                 return -1;
691         }
692         return 0;
693 }
694
695 RESULT eServiceDVD::cueSheet(ePtr<iCueSheet> &ptr)
696 {
697         if (m_cue_pts)
698         {
699                 ptr = this;
700                 return 0;
701         }
702         ptr = 0;
703         return -1;
704 }
705
706 PyObject *eServiceDVD::getCutList()
707 {
708         ePyObject list = PyList_New(1);
709         ePyObject tuple = PyTuple_New(2);
710         PyTuple_SetItem(tuple, 0, PyLong_FromLongLong(m_cue_pts));
711         PyTuple_SetItem(tuple, 1, PyInt_FromLong(3));
712         PyList_SetItem(list, 0, tuple);
713         return list;
714 }
715
716 void eServiceDVD::setCutList(ePyObject /*list*/)
717 {
718 }
719
720 void eServiceDVD::setCutListEnable(int /*enable*/)
721 {
722 }
723
724 void eServiceDVD::loadCuesheet()
725 {
726         char filename[128];
727         if ( m_ddvd_titlestring[0] != '\0' )
728                 snprintf(filename, 128, "/home/root/dvd-%s.cuts", m_ddvd_titlestring);
729         else
730                 snprintf(filename, 128, "%s/dvd.cuts", m_filename.c_str());
731
732         eDebug("eServiceDVD::loadCuesheet() filename=%s",filename);
733
734         FILE *f = fopen(filename, "rb");
735
736         if (f)
737         {
738                 unsigned long long where;
739                 unsigned int what;
740
741                 if (!fread(&where, sizeof(where), 1, f))
742                         return;
743                 if (!fread(&what, sizeof(what), 1, f))
744                         return;
745 #if BYTE_ORDER == LITTLE_ENDIAN
746                 where = bswap_64(where);
747 #endif
748                 what = ntohl(what);
749
750                 if (!fread(&m_resume_info, sizeof(struct ddvd_resume), 1, f))
751                         return;
752                 if (!fread(&what, sizeof(what), 1, f))
753                         return;
754
755                 what = ntohl(what);
756                 if (what != 4 )
757                         return;
758
759                 m_cue_pts = where;
760
761                 fclose(f);
762         } else
763                 eDebug("cutfile not found!");
764
765         if (m_cue_pts)
766         {
767                 m_event((iPlayableService*)this, evCuesheetChanged);
768                 eDebug("eServiceDVD::loadCuesheet() pts=%lld",m_cue_pts);
769         }
770 }
771
772 void eServiceDVD::saveCuesheet()
773 {
774         eDebug("eServiceDVD::saveCuesheet()");
775
776         struct ddvd_resume resume_info;
777         ddvd_get_resume_pos(m_ddvdconfig, &resume_info);
778
779         if (resume_info.title)
780         {
781                 struct ddvd_time info;
782                 ddvd_get_last_time(m_ddvdconfig, &info);
783                 pts_t pos;
784                 pos = info.pos_hours * 3600;
785                 pos += info.pos_minutes * 60;
786                 pos += info.pos_seconds;
787                 pos *= 90000;
788                 m_cue_pts = pos;
789                 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);
790         }
791         else
792         {
793                 eDebug("we're in a menu or somewhere else funny. so save cuesheet with pts=0");
794                 m_cue_pts = 0;
795         }
796
797         char filename[128];
798         if ( m_ddvd_titlestring[0] != '\0' )
799                 snprintf(filename, 128, "/home/root/dvd-%s.cuts", m_ddvd_titlestring);
800         else
801                 snprintf(filename, 128, "%s/dvd.cuts", m_filename.c_str());
802         
803         FILE *f = fopen(filename, "wb");
804
805         if (f)
806         {
807                 unsigned long long where;
808                 int what;
809
810 #if BYTE_ORDER == BIG_ENDIAN
811                 where = m_cue_pts;
812 #else
813                 where = bswap_64(m_cue_pts);
814 #endif
815                 what = htonl(3);
816                 fwrite(&where, sizeof(where), 1, f);
817                 fwrite(&what, sizeof(what), 1, f);
818                 
819                 what = htonl(4);
820                 fwrite(&resume_info, sizeof(struct ddvd_resume), 1, f);
821                 fwrite(&what, sizeof(what), 1, f);
822
823                 fclose(f);
824         }
825 }
826
827 eAutoInitPtr<eServiceFactoryDVD> init_eServiceFactoryDVD(eAutoInitNumbers::service+1, "eServiceFactoryDVD");
828
829 PyMODINIT_FUNC
830 initservicedvd(void)
831 {
832         Py_InitModule("servicedvd", NULL);
833 }