da9c0bb7ff47407feeec230e802491f04e40d1b3
[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                 extensions.push_back("img");
36                 sc->addServiceFactory(eServiceFactoryDVD::id, this, extensions);
37         }
38 }
39
40 eServiceFactoryDVD::~eServiceFactoryDVD()
41 {
42         ePtr<eServiceCenter> sc;
43         
44         eServiceCenter::getPrivInstance(sc);
45         if (sc)
46                 sc->removeServiceFactory(eServiceFactoryDVD::id);
47 }
48
49 DEFINE_REF(eServiceFactoryDVD)
50
51         // iServiceHandler
52 RESULT eServiceFactoryDVD::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
53 {
54                 // check resources...
55         ptr = new eServiceDVD(ref);
56         return 0;
57 }
58
59 RESULT eServiceFactoryDVD::record(const eServiceReference &/*ref*/, ePtr<iRecordableService> &ptr)
60 {
61         ptr=0;
62         return -1;
63 }
64
65 RESULT eServiceFactoryDVD::list(const eServiceReference &, ePtr<iListableService> &ptr)
66 {
67         ptr=0;
68         return -1;
69 }
70
71
72 RESULT eServiceFactoryDVD::info(const eServiceReference &/*ref*/, ePtr<iStaticServiceInformation> &ptr)
73 {
74         ptr=0;
75         return -1;
76 }
77
78 RESULT eServiceFactoryDVD::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
79 {
80         ptr = 0;
81         return -1;
82 }
83
84 // eServiceDVD
85
86 DEFINE_REF(eServiceDVD);
87
88 eServiceDVD::eServiceDVD(eServiceReference ref):
89         m_ref(ref), m_ddvdconfig(ddvd_create()), m_subtitle_widget(0), m_state(stIdle),
90         m_current_trick(0), m_pump(eApp, 1), m_width(-1), m_height(-1),
91         m_aspect(-1), m_framerate(-1), m_progressive(-1)
92 {
93         int aspect = DDVD_16_9;
94         int policy = DDVD_PAN_SCAN;
95         int policy2 = DDVD_PAN_SCAN;
96
97         char tmp[255];
98         ssize_t rd;
99
100         m_sn = eSocketNotifier::create(eApp, ddvd_get_messagepipe_fd(m_ddvdconfig), eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Error|eSocketNotifier::Hungup);
101         eDebug("SERVICEDVD construct!");
102         // create handle
103         ddvd_set_dvd_path(m_ddvdconfig, ref.path.c_str());
104         ddvd_set_ac3thru(m_ddvdconfig, 0);
105
106         std::string ddvd_language;
107         if (!ePythonConfigQuery::getConfigValue("config.osd.language", ddvd_language))
108                 ddvd_set_language(m_ddvdconfig, (ddvd_language.substr(0,2)).c_str());
109
110         int fd = open("/proc/stb/video/aspect", O_RDONLY);
111         if (fd > -1)
112         {
113                 rd = read(fd, tmp, 255);
114                 if (rd > 2 && !strncmp(tmp, "4:3", 3))
115                         aspect = DDVD_4_3;
116                 else if (rd > 4 && !strncmp(tmp, "16:10", 5))
117                         aspect = DDVD_16_10;
118                 close(fd);
119         }
120
121         fd = open("/proc/stb/video/policy", O_RDONLY);
122         if (fd > -1)
123         {
124                 rd = read(fd, tmp, 255);
125                 if (rd > 6 && !strncmp(tmp, "bestfit", 7))
126                         policy = DDVD_JUSTSCALE;
127                 else if (rd > 8 && !strncmp(tmp, "letterbox", 9))
128                         policy = DDVD_LETTERBOX;
129                 close(fd);
130         }
131
132 #ifdef DDVD_SUPPORTS_16_10_SCALING
133         fd = open("/proc/stb/video/policy2", O_RDONLY);
134         if (fd > -1)
135         {
136                 rd = read(fd, tmp, 255);
137                 if (rd > 6 && !strncmp(tmp, "bestfit", 7))
138                         policy2 = DDVD_JUSTSCALE;
139                 else if (rd > 8 && !strncmp(tmp, "letterbox", 9))
140                         policy2 = DDVD_LETTERBOX;
141                 close(fd);
142         }
143         ddvd_set_video_ex(m_ddvdconfig, aspect, policy, policy2, DDVD_PAL /*unused*/);
144 #else
145         ddvd_set_video(m_ddvdconfig, aspect, policy, DDVD_PAL /*unused*/);
146 #warning please update libdreamdvd for 16:10 scaling support!
147 #endif
148
149         CONNECT(m_sn->activated, eServiceDVD::gotMessage);
150         CONNECT(m_pump.recv_msg, eServiceDVD::gotThreadMessage);
151         strcpy(m_ddvd_titlestring,"");
152         m_cue_pts = 0;
153         pause();
154 }
155
156 void eServiceDVD::gotThreadMessage(const int &msg)
157 {
158         switch(msg)
159         {
160         case 1: // thread stopped
161                 m_state = stStopped;
162                 m_event(this, evStopped);
163                 break;
164         }
165 }
166
167 void eServiceDVD::gotMessage(int /*what*/)
168 {
169         switch(ddvd_get_next_message(m_ddvdconfig,1))
170         {
171                 case DDVD_COLORTABLE_UPDATE:
172                 {
173 /*
174                         struct ddvd_color ctmp[4];
175                         ddvd_get_last_colortable(ddvdconfig, ctmp);
176                         int i=0;
177                         while (i < 4)
178                         {
179                                 rd1[252+i]=ctmp[i].red;
180                                 bl1[252+i]=ctmp[i].blue;
181                                 gn1[252+i]=ctmp[i].green;
182                                 tr1[252+i]=ctmp[i].trans;
183                                 i++;
184                         }
185                         if(ioctl(fb, FBIOPUTCMAP, &colormap) == -1)
186                         {
187                                 printf("Framebuffer: <FBIOPUTCMAP failed>\n");
188                                 return 1;
189                         }
190 */
191                         eDebug("no support for 8bpp framebuffer in dvdplayer yet!");
192                         break;
193                 }
194                 case DDVD_SCREEN_UPDATE:
195                         eDebug("DVD_SCREEN_UPDATE!");
196                         if (m_subtitle_widget) {
197                                 int x1,x2,y1,y2;
198                                 ddvd_get_last_blit_area(m_ddvdconfig, &x1, &x2, &y1, &y2);
199                                 
200                                 int x_offset = 0, y_offset = 0, width = 720, height = 576;
201
202 #ifdef DDVD_SUPPORTS_GET_BLIT_DESTINATION
203                                 ddvd_get_blit_destination(m_ddvdconfig, &x_offset, &y_offset, &width, &height);
204                                 eDebug("values got from ddvd: %d %d %d %d", x_offset, y_offset, width, height);
205                                 y_offset = -y_offset;
206                                 width -= x_offset * 2;
207                                 height -= y_offset * 2;
208 #endif
209                                 eRect dest(x_offset, y_offset, width, height);
210
211                                 if (dest.width() && dest.height())
212                                         m_subtitle_widget->setPixmap(m_pixmap, eRect(x1, y1, (x2-x1)+1, (y2-y1)+1), dest);
213                         }
214                         break;
215                 case DDVD_SHOWOSD_STATE_PLAY:
216                 {
217                         eDebug("DVD_SHOWOSD_STATE_PLAY!");
218                         m_current_trick = 0;
219                         m_event(this, evUser+1);
220                         break;
221                 }
222                 case DDVD_SHOWOSD_STATE_PAUSE:
223                 {
224                         eDebug("DVD_SHOWOSD_STATE_PAUSE!");
225                         m_event(this, evUser+2);
226                         break;
227                 }
228                 case DDVD_SHOWOSD_STATE_FFWD:
229                 {
230                         eDebug("DVD_SHOWOSD_STATE_FFWD!");
231                         m_event(this, evUser+3);
232                         break;
233                 }
234                 case DDVD_SHOWOSD_STATE_FBWD:
235                 {
236                         eDebug("DVD_SHOWOSD_STATE_FBWD!");
237                         m_event(this, evUser+4);
238                         break;
239                 }
240                 case DDVD_SHOWOSD_STRING:
241                 {
242                         eDebug("DVD_SHOWOSD_STRING!");
243                         m_event(this, evUser+5);
244                         break;
245                 }
246                 case DDVD_SHOWOSD_AUDIO:
247                 {
248                         eDebug("DVD_SHOWOSD_STRING!");
249                         m_event(this, evUser+6);
250                         break;
251                 }
252                 case DDVD_SHOWOSD_SUBTITLE:
253                 {
254                         eDebug("DVD_SHOWOSD_SUBTITLE!");
255                         m_event((iPlayableService*)this, evUpdatedInfo);
256                         m_event(this, evUser+7);
257                         break;
258                 }
259                 case DDVD_EOF_REACHED:
260                         eDebug("DVD_EOF_REACHED!");
261                         m_event(this, evEOF);
262                         break;
263                 case DDVD_SOF_REACHED:
264                         eDebug("DVD_SOF_REACHED!");
265                         m_event(this, evSOF);
266                         break;
267                 case DDVD_SHOWOSD_ANGLE:
268                 {
269                         int current, num;
270                         ddvd_get_angle_info(m_ddvdconfig, &current, &num);
271                         eDebug("DVD_ANGLE_INFO: %d / %d", current, num);
272                         m_event(this, evUser+13);
273                         break;
274                 }
275                 case DDVD_SHOWOSD_TIME:
276                 {
277                         static struct ddvd_time last_info;
278                         struct ddvd_time info;
279 //                      eDebug("DVD_SHOWOSD_TIME!");
280                         ddvd_get_last_time(m_ddvdconfig, &info);
281                         if ( info.pos_chapter != last_info.pos_chapter )
282                                 m_event(this, evUser+8); // chapterUpdated
283                         if ( info.pos_title != last_info.pos_title )
284                                 m_event(this, evUser+9); // titleUpdated
285                         memcpy(&last_info, &info, sizeof(struct ddvd_time));
286                         break;
287                 }
288                 case DDVD_SHOWOSD_TITLESTRING:
289                 {
290                         ddvd_get_title_string(m_ddvdconfig, m_ddvd_titlestring);
291                         eDebug("DDVD_SHOWOSD_TITLESTRING: %s",m_ddvd_titlestring);
292                         loadCuesheet();
293                         if (!m_cue_pts)
294                                 unpause();
295                         m_event(this, evStart);
296                         break;
297                 }
298                 case DDVD_MENU_OPENED:
299                         eDebug("DVD_MENU_OPENED!");
300                         m_state = stMenu;
301                         m_event(this, evSeekableStatusChanged);
302                         m_event(this, evUser+11);
303                         break;
304                 case DDVD_MENU_CLOSED:
305                         eDebug("DVD_MENU_CLOSED!");
306                         m_state = stRunning;
307                         m_event(this, evSeekableStatusChanged);
308                         m_event(this, evUser+12);
309                         break;
310 #ifdef DDVD_SUPPORTS_PICTURE_INFO
311                 case DDVD_SIZE_CHANGED:
312                 {
313                         int changed = m_width != -1 && m_height != -1 && m_aspect != -1;
314                         ddvd_get_last_size(m_ddvdconfig, &m_width, &m_height, &m_aspect);
315                         if (changed)
316                                 m_event((iPlayableService*)this, evVideoSizeChanged);
317                         break;
318                 }
319                 case DDVD_PROGRESSIVE_CHANGED:
320                 {
321                         int changed = m_progressive != -1;
322                         ddvd_get_last_progressive(m_ddvdconfig, &m_progressive);
323                         if (changed)
324                                 m_event((iPlayableService*)this, evVideoProgressiveChanged);
325                         break;
326                 }
327                 case DDVD_FRAMERATE_CHANGED:
328                 {
329                         int changed = m_framerate != -1;
330                         ddvd_get_last_framerate(m_ddvdconfig, &m_framerate);
331                         if (changed)
332                                 m_event((iPlayableService*)this, evVideoFramerateChanged);
333                         break;
334                 }
335 #endif
336                 default:
337                         break;
338         }
339 }
340
341 eServiceDVD::~eServiceDVD()
342 {
343         eDebug("SERVICEDVD destruct!");
344         kill();
345         saveCuesheet();
346         ddvd_close(m_ddvdconfig);
347         disableSubtitles(0);
348 }
349
350 RESULT eServiceDVD::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
351 {
352         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
353         return 0;
354 }
355
356 RESULT eServiceDVD::start()
357 {
358         ASSERT(m_state == stIdle);
359         m_state = stRunning;
360         eDebug("eServiceDVD starting");
361 //      m_event(this, evStart);
362         return 0;
363 }
364
365 RESULT eServiceDVD::stop()
366 {
367         ASSERT(m_state != stIdle);
368         if (m_state == stStopped)
369                 return -1;
370         eDebug("DVD: stop %s", m_ref.path.c_str());
371         m_state = stStopped;
372         ddvd_send_key(m_ddvdconfig, DDVD_KEY_EXIT);
373
374         return 0;
375 }
376
377 RESULT eServiceDVD::setTarget(int /*target*/)
378 {
379         return -1;
380 }
381
382 RESULT eServiceDVD::pause(ePtr<iPauseableService> &ptr)
383 {
384         ptr=this;
385         return 0;
386 }
387
388 RESULT eServiceDVD::seek(ePtr<iSeekableService> &ptr)
389 {
390         ptr=this;
391         return 0;
392 }
393
394 RESULT eServiceDVD::subtitle(ePtr<iSubtitleOutput> &ptr)
395 {
396         ptr=this;
397         return 0;
398 }
399
400 RESULT eServiceDVD::audioTracks(ePtr<iAudioTrackSelection> &ptr)
401 {
402         ptr = this;
403         return 0;
404 }
405
406 int eServiceDVD::getNumberOfTracks()
407 {
408         int i = 0;
409         ddvd_get_audio_count(m_ddvdconfig, &i);
410         eDebug("getNumberOfTracks returns %i", i);
411         return i;
412 }
413
414 int eServiceDVD::getCurrentTrack()
415 {
416         int audio_id,audio_type;
417         uint16_t audio_lang;
418         ddvd_get_last_audio(m_ddvdconfig, &audio_id, &audio_lang, &audio_type);
419         eDebug("getCurrentTrack returns %i", audio_id);
420         return audio_id;
421 }
422
423 RESULT eServiceDVD::selectTrack(unsigned int i)
424 {
425         ddvd_set_audio(m_ddvdconfig, i);
426         eDebug("selectTrack %i", i);
427         return 0;
428 }
429
430 RESULT eServiceDVD::getTrackInfo(struct iAudioTrackInfo &info, unsigned int audio_id)
431 {
432         int audio_type;
433         uint16_t audio_lang;
434         ddvd_get_audio_byid(m_ddvdconfig, audio_id, &audio_lang, &audio_type);
435         char audio_string[3]={audio_lang >> 8, audio_lang, 0};
436         info.m_pid = audio_id+1;
437         info.m_language = audio_string;
438         switch(audio_type)
439         {
440                 case DDVD_MPEG:
441                         info.m_description = "MPEG";
442                         break;
443                 case DDVD_AC3:
444                         info.m_description = "AC3";
445                         break;
446                 case DDVD_DTS:
447                         info.m_description = "DTS";
448                         break;
449                 case DDVD_LPCM:
450                         info.m_description = "LPCM";
451                         break;
452                 default:
453                         info.m_description = "und";
454         }
455         return 0;
456 }
457
458 RESULT eServiceDVD::keys(ePtr<iServiceKeys> &ptr)
459 {
460         ptr=this;
461         return 0;
462 }
463
464         // iPausableService
465 RESULT eServiceDVD::setSlowMotion(int /*ratio*/)
466 {
467         return -1;
468 }
469
470 RESULT eServiceDVD::setFastForward(int trick)
471 {
472         eDebug("setTrickmode(%d)", trick);
473         while (m_current_trick > trick && m_current_trick != -64)
474         {
475                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_FBWD);
476                 if (m_current_trick == 0)
477                         m_current_trick = -2;
478                 else if (m_current_trick > 0)
479                 {
480                         m_current_trick /= 2;
481                         if (abs(m_current_trick) == 1)
482                                 m_current_trick=0;
483                 }
484                 else
485                         m_current_trick *= 2;
486         }
487         while (m_current_trick < trick && m_current_trick != 64)
488         {
489                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_FFWD);
490                 if (m_current_trick == 0)
491                         m_current_trick = 2;
492                 else if (m_current_trick < 0)
493                 {
494                         m_current_trick /= 2;
495                         if (abs(m_current_trick) == 1)
496                                 m_current_trick=0;
497                 }
498                 else
499                         m_current_trick *= 2;
500         }
501         return 0;
502 }
503
504 RESULT eServiceDVD::pause()
505 {
506         eDebug("set pause!\n");
507         ddvd_send_key(m_ddvdconfig, DDVD_KEY_PAUSE);
508         return 0;
509 }
510
511 RESULT eServiceDVD::unpause()
512 {
513         eDebug("set unpause!\n");
514         ddvd_send_key(m_ddvdconfig, DDVD_KEY_PLAY);
515         return 0;
516 }
517
518 void eServiceDVD::thread()
519 {
520         eDebug("eServiceDVD dvd thread started");
521         hasStarted();
522         ddvd_run(m_ddvdconfig);
523 }
524
525 void eServiceDVD::thread_finished()
526 {
527         eDebug("eServiceDVD dvd thread finished");
528         m_pump.send(1); // inform main thread
529 }
530
531 RESULT eServiceDVD::info(ePtr<iServiceInformation>&i)
532 {
533         i = this;
534         return 0;
535 }
536
537 RESULT eServiceDVD::getName(std::string &name)
538 {
539         if ( m_ddvd_titlestring[0] != '\0' )
540                 name = m_ddvd_titlestring;
541         else
542                 if ( !m_ref.name.empty() )
543                         name = m_ref.name;
544                 else
545                         name = m_ref.path;
546         return 0;
547 }
548
549 int eServiceDVD::getInfo(int w)
550 {
551         switch (w)
552         {
553                 case sCurrentChapter:
554                 {
555                         struct ddvd_time info;
556                         ddvd_get_last_time(m_ddvdconfig, &info);
557                         return info.pos_chapter;
558                 }
559                 case sTotalChapters:
560                 {
561                         struct ddvd_time info;
562                         ddvd_get_last_time(m_ddvdconfig, &info);
563                         return info.end_chapter;
564                 }
565                 case sCurrentTitle:
566                 {
567                         struct ddvd_time info;
568                         ddvd_get_last_time(m_ddvdconfig, &info);
569                         return info.pos_title;
570                 }
571                 case sTotalTitles:
572                 {
573                         struct ddvd_time info;
574                         ddvd_get_last_time(m_ddvdconfig, &info);
575                         return info.end_title;
576                 }
577                 case sTXTPID:   // we abuse HAS_TELEXT icon in InfoBar to signalize subtitles status
578                 {
579                         int spu_id;
580                         uint16_t spu_lang;
581                         ddvd_get_last_spu(m_ddvdconfig, &spu_id, &spu_lang);
582                         return spu_id;
583                 }
584                 case sUser+6:
585                 case sUser+7:
586                 case sUser+8:
587                         return resIsPyObject;
588 #ifdef DDVD_SUPPORTS_PICTURE_INFO
589                 case sVideoWidth:
590                         return m_width;
591                 case sVideoHeight:
592                         return m_height;
593                 case sAspect:
594                         return m_aspect;
595                 case sProgressive:
596                         return m_progressive;
597                 case sFrameRate:
598                         return m_framerate;
599 #endif
600                 default:
601                         return resNA;
602         }
603 }
604
605 std::string eServiceDVD::getInfoString(int w)
606 {
607         switch(w)
608         {
609                 case sServiceref:
610                         return m_ref.toString();
611                 default:
612                         eDebug("unhandled getInfoString(%d)", w);
613         }
614         return "";
615 }
616
617 PyObject *eServiceDVD::getInfoObject(int w)
618 {
619         switch(w)
620         {
621                 case sUser+6:
622                 {
623                         ePyObject tuple = PyTuple_New(3);
624                         int audio_id,audio_type;
625                         uint16_t audio_lang;
626                         ddvd_get_last_audio(m_ddvdconfig, &audio_id, &audio_lang, &audio_type);
627                         char audio_string[3]={audio_lang >> 8, audio_lang, 0};
628                         PyTuple_SetItem(tuple, 0, PyInt_FromLong(audio_id+1));
629                         PyTuple_SetItem(tuple, 1, PyString_FromString(audio_string));
630                         switch(audio_type)
631                         {
632                                 case DDVD_MPEG:
633                                         PyTuple_SetItem(tuple, 2, PyString_FromString("MPEG"));
634                                         break;
635                                 case DDVD_AC3:
636                                         PyTuple_SetItem(tuple, 2, PyString_FromString("AC3"));
637                                         break;
638                                 case DDVD_DTS:
639                                         PyTuple_SetItem(tuple, 2, PyString_FromString("DTS"));
640                                         break;
641                                 case DDVD_LPCM:
642                                         PyTuple_SetItem(tuple, 2, PyString_FromString("LPCM"));
643                                         break;
644                                 default:
645                                         PyTuple_SetItem(tuple, 2, PyString_FromString(""));
646                         }
647                         return tuple;
648                 }
649                 case sUser+7:
650                 {
651                         ePyObject tuple = PyTuple_New(2);
652                         int spu_id;
653                         uint16_t spu_lang;
654                         ddvd_get_last_spu(m_ddvdconfig, &spu_id, &spu_lang);
655                         char spu_string[3]={spu_lang >> 8, spu_lang, 0};
656                         if (spu_id == -1)
657                         {
658                                 PyTuple_SetItem(tuple, 0, PyInt_FromLong(0));
659                                 PyTuple_SetItem(tuple, 1, PyString_FromString(""));
660                         }
661                         else
662                         {
663                                 PyTuple_SetItem(tuple, 0, PyInt_FromLong(spu_id+1));
664                                 PyTuple_SetItem(tuple, 1, PyString_FromString(spu_string));
665                         }                               
666                         return tuple;
667                 }
668                 case sUser+8:
669                 {
670                         ePyObject tuple = PyTuple_New(2);
671                         int current, num;
672                         ddvd_get_angle_info(m_ddvdconfig, &current, &num);
673                         PyTuple_SetItem(tuple, 0, PyInt_FromLong(current));
674                         PyTuple_SetItem(tuple, 1, PyInt_FromLong(num));
675
676                         return tuple;
677                 }
678                 default:
679                         eDebug("unhandled getInfoObject(%d)", w);
680         }
681         Py_RETURN_NONE;
682 }
683
684 RESULT eServiceDVD::enableSubtitles(eWidget *parent, SWIG_PYOBJECT(ePyObject) /*entry*/)
685 {
686         delete m_subtitle_widget;
687
688         m_subtitle_widget = new eSubtitleWidget(parent);
689         m_subtitle_widget->resize(parent->size());
690
691         eSize size = eSize(720, 576);
692
693         if (!m_pixmap)
694         {
695                 m_pixmap = new gPixmap(size, 32, 1); /* allocate accel surface (if possible) */
696 #ifdef DDVD_SUPPORTS_GET_BLIT_DESTINATION
697                 ddvd_set_lfb_ex(m_ddvdconfig, (unsigned char *)m_pixmap->surface->data, size.width(), size.height(), 4, size.width()*4, 1);
698 #else
699                 ddvd_set_lfb(m_ddvdconfig, (unsigned char *)m_pixmap->surface->data, size.width(), size.height(), 4, size.width()*4);
700 #warning please update libdreamdvd for fast scaling
701 #endif
702                 run(); // start the thread
703         }
704
705         m_subtitle_widget->setZPosition(-1);
706         m_subtitle_widget->show();
707
708         return 0;
709 }
710
711 RESULT eServiceDVD::disableSubtitles(eWidget */*parent*/)
712 {
713         delete m_subtitle_widget;
714         m_subtitle_widget = 0;
715         return 0;
716 }
717
718 PyObject *eServiceDVD::getSubtitleList()
719 {
720         eDebug("eServiceMP3::getSubtitleList");
721
722         ePyObject l = PyList_New(0);
723         unsigned int spu_count = 0;
724         ddvd_get_spu_count(m_ddvdconfig, &spu_count);
725
726         for ( unsigned int spu_id = 0; spu_id < spu_count; spu_id++ )
727         {
728                 uint16_t spu_lang;
729                 ddvd_get_spu_byid(m_ddvdconfig, spu_id, &spu_lang);
730                 char spu_string[3]={spu_lang >> 8, spu_lang, 0};
731
732                 ePyObject tuple = PyTuple_New(5);
733                 PyTuple_SetItem(tuple, 0, PyInt_FromLong(2));
734                 PyTuple_SetItem(tuple, 1, PyInt_FromLong(0));
735                 PyTuple_SetItem(tuple, 2, PyInt_FromLong(3));
736                 PyTuple_SetItem(tuple, 3, PyInt_FromLong(0));
737                 PyTuple_SetItem(tuple, 4, PyString_FromString(spu_string));
738                 PyList_Append(l, tuple);
739                 Py_DECREF(tuple);
740         }
741         return l;
742 }
743
744 PyObject *eServiceDVD::getCachedSubtitle()
745 {
746         eDebug("eServiceDVD::getCachedSubtitle nyi");
747         Py_RETURN_NONE;
748 }
749
750 RESULT eServiceDVD::getLength(pts_t &len)
751 {
752 //      eDebug("eServiceDVD::getLength");
753         struct ddvd_time info;
754         ddvd_get_last_time(m_ddvdconfig, &info);
755         len = info.end_hours * 3600;
756         len += info.end_minutes * 60;
757         len += info.end_seconds;
758         len *= 90000;
759         return 0;
760 }
761
762 RESULT eServiceDVD::seekTo(pts_t to)
763 {
764         eDebug("eServiceDVD::seekTo(%lld)",to);
765         if ( to > 0 )
766         {
767                 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);
768                 ddvd_set_resume_pos(m_ddvdconfig, m_resume_info);
769         }
770         return 0;
771 }
772
773 RESULT eServiceDVD::seekRelative(int direction, pts_t to)
774 {
775         int seconds = to / 90000;
776         seconds *= direction;
777         eDebug("seekRelative %d %d", direction, seconds);
778         ddvd_skip_seconds(m_ddvdconfig, seconds);
779         return 0;
780 }
781
782 RESULT eServiceDVD::getPlayPosition(pts_t &pos)
783 {
784         struct ddvd_time info;
785         ddvd_get_last_time(m_ddvdconfig, &info);
786         pos = info.pos_hours * 3600;
787         pos += info.pos_minutes * 60;
788         pos += info.pos_seconds;
789 //      eDebug("getPlayPosition %lld", pos);
790         pos *= 90000;
791         return 0;
792 }
793
794 RESULT eServiceDVD::seekTitle(int title)
795 {
796         eDebug("setTitle %d", title);
797         ddvd_set_title(m_ddvdconfig, title);
798         return 0;
799 }
800
801 RESULT eServiceDVD::seekChapter(int chapter)
802 {
803         eDebug("setChapter %d", chapter);
804         if ( chapter > 0 )
805                 ddvd_set_chapter(m_ddvdconfig, chapter);
806         return 0;
807 }
808
809 RESULT eServiceDVD::setTrickmode(int /*trick*/)
810 {
811         return -1;
812 }
813
814 RESULT eServiceDVD::isCurrentlySeekable()
815 {
816         return m_state == stRunning ? 3 : 0;
817 }
818
819 RESULT eServiceDVD::keyPressed(int key)
820 {
821         switch(key)
822         {
823         case iServiceKeys::keyLeft:
824                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_LEFT);
825                 break;
826         case iServiceKeys::keyRight:
827                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_RIGHT);
828                 break;
829         case iServiceKeys::keyUp:
830                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_UP);
831                 break;
832         case iServiceKeys::keyDown:
833                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_DOWN);
834                 break;
835         case iServiceKeys::keyOk:
836                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_OK);
837                 break;
838         case iServiceKeys::keyUser:
839                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_AUDIO);
840                 break;
841         case iServiceKeys::keyUser+1:
842                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_SUBTITLE);
843                 break;
844         case iServiceKeys::keyUser+2:
845                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_AUDIOMENU);
846                 break;
847         case iServiceKeys::keyUser+3:
848                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_NEXT_CHAPTER);
849                 break;
850         case iServiceKeys::keyUser+4:
851                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_PREV_CHAPTER);
852                 break;
853         case iServiceKeys::keyUser+5:
854                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_NEXT_TITLE);
855                 break;
856         case iServiceKeys::keyUser+6:
857                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_PREV_TITLE);
858                 break;
859         case iServiceKeys::keyUser+7:
860                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_MENU);
861                 break;
862         case iServiceKeys::keyUser+8:
863                 ddvd_send_key(m_ddvdconfig, DDVD_KEY_ANGLE);
864                 break;
865         default:
866                 return -1;
867         }
868         return 0;
869 }
870
871 RESULT eServiceDVD::cueSheet(ePtr<iCueSheet> &ptr)
872 {
873         if (m_cue_pts)
874         {
875                 ptr = this;
876                 return 0;
877         }
878         ptr = 0;
879         return -1;
880 }
881
882 PyObject *eServiceDVD::getCutList()
883 {
884         ePyObject list = PyList_New(1);
885         ePyObject tuple = PyTuple_New(2);
886         PyTuple_SetItem(tuple, 0, PyLong_FromLongLong(m_cue_pts));
887         PyTuple_SetItem(tuple, 1, PyInt_FromLong(3));
888         PyList_SetItem(list, 0, tuple);
889         return list;
890 }
891
892 void eServiceDVD::setCutList(ePyObject /*list*/)
893 {
894 }
895
896 void eServiceDVD::setCutListEnable(int /*enable*/)
897 {
898 }
899
900 void eServiceDVD::loadCuesheet()
901 {
902         char filename[128];
903         if ( m_ddvd_titlestring[0] != '\0' )
904                 snprintf(filename, 128, "/home/root/dvd-%s.cuts", m_ddvd_titlestring);
905         else
906                 snprintf(filename, 128, "%s/dvd.cuts", m_ref.path.c_str());
907
908         eDebug("eServiceDVD::loadCuesheet() filename=%s",filename);
909
910         FILE *f = fopen(filename, "rb");
911
912         if (f)
913         {
914                 unsigned long long where;
915                 unsigned int what;
916
917                 if (!fread(&where, sizeof(where), 1, f))
918                         return;
919                 if (!fread(&what, sizeof(what), 1, f))
920                         return;
921 #if BYTE_ORDER == LITTLE_ENDIAN
922                 where = bswap_64(where);
923 #endif
924                 what = ntohl(what);
925
926                 if (!fread(&m_resume_info, sizeof(struct ddvd_resume), 1, f))
927                         return;
928                 if (!fread(&what, sizeof(what), 1, f))
929                         return;
930
931                 what = ntohl(what);
932                 if (what != 4 )
933                         return;
934
935                 m_cue_pts = where;
936
937                 fclose(f);
938         } else
939                 eDebug("cutfile not found!");
940
941         if (m_cue_pts)
942         {
943                 m_event((iPlayableService*)this, evCuesheetChanged);
944                 eDebug("eServiceDVD::loadCuesheet() pts=%lld",m_cue_pts);
945         }
946 }
947
948 void eServiceDVD::saveCuesheet()
949 {
950         eDebug("eServiceDVD::saveCuesheet()");
951
952         struct ddvd_resume resume_info;
953         ddvd_get_resume_pos(m_ddvdconfig, &resume_info);
954
955         if (resume_info.title)
956         {
957                 struct ddvd_time info;
958                 ddvd_get_last_time(m_ddvdconfig, &info);
959                 pts_t pos;
960                 pos = info.pos_hours * 3600;
961                 pos += info.pos_minutes * 60;
962                 pos += info.pos_seconds;
963                 pos *= 90000;
964                 m_cue_pts = pos;
965                 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);
966         }
967         else
968         {
969                 eDebug("we're in a menu or somewhere else funny. so save cuesheet with pts=0");
970                 m_cue_pts = 0;
971         }
972
973         char filename[128];
974         if ( m_ddvd_titlestring[0] != '\0' )
975                 snprintf(filename, 128, "/home/root/dvd-%s.cuts", m_ddvd_titlestring);
976         else
977                 snprintf(filename, 128, "%s/dvd.cuts", m_ref.path.c_str());
978         
979         FILE *f = fopen(filename, "wb");
980
981         if (f)
982         {
983                 unsigned long long where;
984                 int what;
985
986 #if BYTE_ORDER == BIG_ENDIAN
987                 where = m_cue_pts;
988 #else
989                 where = bswap_64(m_cue_pts);
990 #endif
991                 what = htonl(3);
992                 fwrite(&where, sizeof(where), 1, f);
993                 fwrite(&what, sizeof(what), 1, f);
994                 
995                 what = htonl(4);
996                 fwrite(&resume_info, sizeof(struct ddvd_resume), 1, f);
997                 fwrite(&what, sizeof(what), 1, f);
998
999                 fclose(f);
1000         }
1001 }
1002
1003 eAutoInitPtr<eServiceFactoryDVD> init_eServiceFactoryDVD(eAutoInitNumbers::service+1, "eServiceFactoryDVD");
1004
1005 PyMODINIT_FUNC
1006 initservicedvd(void)
1007 {
1008         Py_InitModule("servicedvd", NULL);
1009 }