summaryrefslogtreecommitdiff
path: root/dvd.c
blob: 8daa926cb2bf834e687652fc7bb4aeb7ae144103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * dvd.c: Functions for handling DVDs
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * Initially written by Andreas Schultz <aschultz@warp10.net>
 *
 * $Id: dvd.c 1.1 2001/08/03 12:35:38 kls Exp $
 */

//XXX //#define DVDDEBUG        1
//XXX //#define DEBUG_BUFFER    1

#include <fcntl.h>
#include <linux/cdrom.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include "dvd.h"

// --- cDVD ----------------------------------------------------------------------------

const char *cDVD::deviceName = "/dev/dvd";
cDVD *cDVD::dvdInstance = NULL;

cDVD *cDVD::getDVD(void)
{
  if (!dvdInstance)
     new cDVD;
  return dvdInstance;
}

cDVD::cDVD(void)
{
  dvd = NULL;
  title = NULL;
  vmg_file = NULL;
  vts_file = NULL;
  dvdInstance = this;
}

cDVD::~cDVD()
{
  Close();
}

void cDVD::SetDeviceName(const char *DeviceName)
{
  deviceName = strdup(DeviceName);
}

const char *cDVD::DeviceName(void)
{
  return deviceName;
}

void cDVD::Open(void)
{
  if (!dvd)
     dvd = DVDOpen(deviceName);
}

void cDVD::Close(void)
{
#ifdef DVDDEBUG
  dsyslog(LOG_INFO, "DVD: cDVD::Close(%p): vts: %p, vmg: %p, title: %p, dvd: %p", this, vts_file, vmg_file, title, dvd);
#endif
  if (vts_file)
     ifoClose(vts_file);
  if (vmg_file)
     ifoClose(vmg_file);
  if (title)
     DVDCloseFile(title);
  if (dvd)
     DVDClose(dvd);
  vts_file = NULL;
  vmg_file = NULL;
  title = NULL;
  dvd = NULL;
}

void cDVD::Eject(void)
{
  int fd;
  Close();
  // ignore all errors try our best :-)
  if ((fd = open(deviceName, O_RDONLY)) > 0) {
     ioctl(fd, CDROMEJECT, 0);
     close(fd);
     }
}

ifo_handle_t *cDVD::openVMG(void)
{
  if (!isValid())
     return NULL;
  if (!vmg_file)
     vmg_file = ifoOpen(dvd, 0);
  return vmg_file;
}

ifo_handle_t *cDVD::openVTS(int TitleSet)
{
  if (!isValid())
     return NULL;
  if (vts_file && (titleset != TitleSet)) {
     ifoClose(vts_file);
     vts_file = NULL;
     }
  if (!vts_file) {
     titleset = TitleSet;
     vts_file = ifoOpen(dvd, TitleSet);
     }
  return vts_file;
}

dvd_file_t *cDVD::openTitle(int Title, dvd_read_domain_t domain)
{
  if (!isValid())
     return NULL;
  if (title)
     DVDCloseFile(title);
  title = DVDOpenFile(dvd, Title, domain);
  return title;
}