Mercurial > libdvdnav.hg
annotate dvd_input.c @ 197:a20a5976a207 src
last_cmd_nav_lbn has to be initialized with an invalid value, so that the first
test to filter double commands will always fail, but 0 is not invalid here!
* use SRI_END_OF_CELL and reenable double command filter
* fix return values of get_current_button()
* use return value of button_auto_action()
author | mroi |
---|---|
date | Mon, 12 May 2003 19:43:11 +0000 |
parents | 5ee9de9e2347 |
children |
rev | line source |
---|---|
168 | 1 /* |
2 * Copyright (C) 2002 Samuel Hocevar <sam@zoy.org>, | |
3 * Håkan Hjort <d95hjort@dtek.chalmers.se> | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. | |
18 */ | |
19 | |
20 #include "config.h" | |
21 | |
22 #include <stdio.h> | |
23 #include <stdlib.h> | |
24 #include <fcntl.h> | |
25 #include <unistd.h> | |
26 | |
185 | 27 #include <dlfcn.h> |
28 | |
168 | 29 #include "dvd_reader.h" |
30 #include "dvd_input.h" | |
31 | |
185 | 32 |
33 #ifndef _MSC_VER | |
34 #define LIBDVDCSS_NAME = "libdvdcss.so.2" | |
35 #else | |
36 #define LIBDVDCSS_NAME = "libdvdcss.dll" | |
37 #endif | |
38 | |
168 | 39 /* The function pointers that is the exported interface of this file. */ |
40 dvd_input_t (*dvdinput_open) (const char *); | |
41 int (*dvdinput_close) (dvd_input_t); | |
42 int (*dvdinput_seek) (dvd_input_t, int); | |
43 int (*dvdinput_title) (dvd_input_t, int); | |
44 int (*dvdinput_read) (dvd_input_t, void *, int, int); | |
45 char * (*dvdinput_error) (dvd_input_t); | |
46 | |
47 #ifdef HAVE_DVDCSS_DVDCSS_H | |
48 /* linking to libdvdcss */ | |
49 #include <dvdcss/dvdcss.h> | |
50 #define DVDcss_open(a) dvdcss_open((char*)(a)) | |
51 #define DVDcss_close dvdcss_close | |
52 #define DVDcss_seek dvdcss_seek | |
53 #define DVDcss_title dvdcss_title | |
54 #define DVDcss_read dvdcss_read | |
55 #define DVDcss_error dvdcss_error | |
56 #else | |
185 | 57 |
168 | 58 /* dlopening libdvdcss */ |
59 #include <dlfcn.h> | |
185 | 60 |
168 | 61 typedef struct dvdcss_s *dvdcss_handle; |
62 static dvdcss_handle (*DVDcss_open) (const char *); | |
63 static int (*DVDcss_close) (dvdcss_handle); | |
64 static int (*DVDcss_seek) (dvdcss_handle, int, int); | |
65 static int (*DVDcss_title) (dvdcss_handle, int); | |
66 static int (*DVDcss_read) (dvdcss_handle, void *, int, int); | |
67 static char * (*DVDcss_error) (dvdcss_handle); | |
68 #endif | |
69 | |
70 /* The DVDinput handle, add stuff here for new input methods. */ | |
71 struct dvd_input_s { | |
72 /* libdvdcss handle */ | |
73 dvdcss_handle dvdcss; | |
74 | |
75 /* dummy file input */ | |
76 int fd; | |
77 }; | |
78 | |
79 | |
80 /** | |
81 * initialize and open a DVD device or file. | |
82 */ | |
83 static dvd_input_t css_open(const char *target) | |
84 { | |
85 dvd_input_t dev; | |
86 | |
87 /* Allocate the handle structure */ | |
183 | 88 dev = (dvd_input_t) malloc(sizeof(*dev)); |
168 | 89 if(dev == NULL) { |
90 fprintf(stderr, "libdvdread: Could not allocate memory.\n"); | |
91 return NULL; | |
92 } | |
93 | |
94 /* Really open it with libdvdcss */ | |
95 dev->dvdcss = DVDcss_open(target); | |
96 if(dev->dvdcss == 0) { | |
97 fprintf(stderr, "libdvdread: Could not open %s with libdvdcss.\n", target); | |
98 free(dev); | |
99 return NULL; | |
100 } | |
101 | |
102 return dev; | |
103 } | |
104 | |
105 /** | |
106 * return the last error message | |
107 */ | |
108 static char *css_error(dvd_input_t dev) | |
109 { | |
110 return DVDcss_error(dev->dvdcss); | |
111 } | |
112 | |
113 /** | |
114 * seek into the device. | |
115 */ | |
116 static int css_seek(dvd_input_t dev, int blocks) | |
117 { | |
118 /* DVDINPUT_NOFLAGS should match the DVDCSS_NOFLAGS value. */ | |
119 return DVDcss_seek(dev->dvdcss, blocks, DVDINPUT_NOFLAGS); | |
120 } | |
121 | |
122 /** | |
123 * set the block for the begining of a new title (key). | |
124 */ | |
125 static int css_title(dvd_input_t dev, int block) | |
126 { | |
127 return DVDcss_title(dev->dvdcss, block); | |
128 } | |
129 | |
130 /** | |
131 * read data from the device. | |
132 */ | |
133 static int css_read(dvd_input_t dev, void *buffer, int blocks, int flags) | |
134 { | |
135 return DVDcss_read(dev->dvdcss, buffer, blocks, flags); | |
136 } | |
137 | |
138 /** | |
139 * close the DVD device and clean up the library. | |
140 */ | |
141 static int css_close(dvd_input_t dev) | |
142 { | |
143 int ret; | |
144 | |
145 ret = DVDcss_close(dev->dvdcss); | |
146 | |
147 if(ret < 0) | |
148 return ret; | |
149 | |
150 free(dev); | |
151 | |
152 return 0; | |
153 } | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | |
160 /** | |
161 * initialize and open a DVD device or file. | |
162 */ | |
163 static dvd_input_t file_open(const char *target) | |
164 { | |
165 dvd_input_t dev; | |
166 | |
167 /* Allocate the library structure */ | |
183 | 168 dev = (dvd_input_t) malloc(sizeof(*dev)); |
168 | 169 if(dev == NULL) { |
170 fprintf(stderr, "libdvdread: Could not allocate memory.\n"); | |
171 return NULL; | |
172 } | |
173 | |
174 /* Open the device */ | |
188
5ee9de9e2347
Fix some win32 stuff to make reading files work as opposed to dirs or disks
tchamp
parents:
185
diff
changeset
|
175 #ifndef _MSC_VER |
168 | 176 dev->fd = open(target, O_RDONLY); |
188
5ee9de9e2347
Fix some win32 stuff to make reading files work as opposed to dirs or disks
tchamp
parents:
185
diff
changeset
|
177 #else |
5ee9de9e2347
Fix some win32 stuff to make reading files work as opposed to dirs or disks
tchamp
parents:
185
diff
changeset
|
178 dev->fd = open(target, O_RDONLY | O_BINARY); |
5ee9de9e2347
Fix some win32 stuff to make reading files work as opposed to dirs or disks
tchamp
parents:
185
diff
changeset
|
179 #endif |
168 | 180 if(dev->fd < 0) { |
181 perror("libdvdread: Could not open input"); | |
182 free(dev); | |
183 return NULL; | |
184 } | |
185 | |
186 return dev; | |
187 } | |
188 | |
189 /** | |
190 * return the last error message | |
191 */ | |
192 static char *file_error(dvd_input_t dev) | |
193 { | |
194 /* use strerror(errno)? */ | |
195 return (char *)"unknown error"; | |
196 } | |
197 | |
198 /** | |
199 * seek into the device. | |
200 */ | |
201 static int file_seek(dvd_input_t dev, int blocks) | |
202 { | |
203 off_t pos; | |
204 | |
205 pos = lseek(dev->fd, (off_t)blocks * (off_t)DVD_VIDEO_LB_LEN, SEEK_SET); | |
206 if(pos < 0) { | |
207 return pos; | |
208 } | |
209 /* assert pos % DVD_VIDEO_LB_LEN == 0 */ | |
210 return (int) (pos / DVD_VIDEO_LB_LEN); | |
211 } | |
212 | |
213 /** | |
214 * set the block for the begining of a new title (key). | |
215 */ | |
216 static int file_title(dvd_input_t dev, int block) | |
217 { | |
218 return -1; | |
219 } | |
220 | |
221 /** | |
222 * read data from the device. | |
223 */ | |
224 static int file_read(dvd_input_t dev, void *buffer, int blocks, int flags) | |
225 { | |
226 size_t len; | |
227 ssize_t ret; | |
228 | |
229 len = (size_t)blocks * DVD_VIDEO_LB_LEN; | |
230 | |
231 while(len > 0) { | |
232 | |
233 ret = read(dev->fd, buffer, len); | |
234 | |
235 if(ret < 0) { | |
236 /* One of the reads failed, too bad. We won't even bother | |
237 * returning the reads that went ok, and as in the posix spec | |
238 * the file postition is left unspecified after a failure. */ | |
239 return ret; | |
240 } | |
241 | |
242 if(ret == 0) { | |
243 /* Nothing more to read. Return the whole blocks, if any, that we got. | |
244 and adjust the file possition back to the previous block boundary. */ | |
245 size_t bytes = (size_t)blocks * DVD_VIDEO_LB_LEN - len; | |
246 off_t over_read = -(bytes % DVD_VIDEO_LB_LEN); | |
247 /*off_t pos =*/ lseek(dev->fd, over_read, SEEK_CUR); | |
248 /* should have pos % 2048 == 0 */ | |
249 return (int) (bytes / DVD_VIDEO_LB_LEN); | |
250 } | |
251 | |
252 len -= ret; | |
253 } | |
254 | |
255 return blocks; | |
256 } | |
257 | |
258 /** | |
259 * close the DVD device and clean up. | |
260 */ | |
261 static int file_close(dvd_input_t dev) | |
262 { | |
263 int ret; | |
264 | |
265 ret = close(dev->fd); | |
266 | |
267 if(ret < 0) | |
268 return ret; | |
269 | |
270 free(dev); | |
271 | |
272 return 0; | |
273 } | |
274 | |
275 | |
276 /** | |
277 * Setup read functions with either libdvdcss or minimal DVD access. | |
278 */ | |
279 int dvdinput_setup(void) | |
280 { | |
281 void *dvdcss_library = NULL; | |
282 char **dvdcss_version = NULL; | |
283 | |
284 #ifdef HAVE_DVDCSS_DVDCSS_H | |
285 /* linking to libdvdcss */ | |
286 dvdcss_library = &dvdcss_library; /* Give it some value != NULL */ | |
287 /* the DVDcss_* functions have been #defined at the top */ | |
288 dvdcss_version = &dvdcss_interface_2; | |
289 | |
290 #else | |
291 /* dlopening libdvdcss */ | |
185 | 292 |
293 #ifndef _MSC_VER | |
168 | 294 dvdcss_library = dlopen("libdvdcss.so.2", RTLD_LAZY); |
185 | 295 #else |
296 dvdcss_library = dlopen("libdvdcss.dll", RTLD_LAZY); | |
297 #endif | |
298 | |
168 | 299 if(dvdcss_library != NULL) { |
300 #if defined(__OpenBSD__) && !defined(__ELF__) | |
301 #define U_S "_" | |
302 #else | |
303 #define U_S | |
304 #endif | |
305 DVDcss_open = (dvdcss_handle (*)(const char*)) | |
306 dlsym(dvdcss_library, U_S "dvdcss_open"); | |
307 DVDcss_close = (int (*)(dvdcss_handle)) | |
308 dlsym(dvdcss_library, U_S "dvdcss_close"); | |
309 DVDcss_title = (int (*)(dvdcss_handle, int)) | |
310 dlsym(dvdcss_library, U_S "dvdcss_title"); | |
311 DVDcss_seek = (int (*)(dvdcss_handle, int, int)) | |
312 dlsym(dvdcss_library, U_S "dvdcss_seek"); | |
313 DVDcss_read = (int (*)(dvdcss_handle, void*, int, int)) | |
314 dlsym(dvdcss_library, U_S "dvdcss_read"); | |
315 DVDcss_error = (char* (*)(dvdcss_handle)) | |
316 dlsym(dvdcss_library, U_S "dvdcss_error"); | |
317 | |
318 dvdcss_version = (char **)dlsym(dvdcss_library, U_S "dvdcss_interface_2"); | |
319 | |
320 if(dlsym(dvdcss_library, U_S "dvdcss_crack")) { | |
321 fprintf(stderr, | |
322 "libdvdread: Old (pre-0.0.2) version of libdvdcss found.\n" | |
323 "libdvdread: You should get the latest version from " | |
324 "http://www.videolan.org/\n" ); | |
325 dlclose(dvdcss_library); | |
326 dvdcss_library = NULL; | |
327 } else if(!DVDcss_open || !DVDcss_close || !DVDcss_title || !DVDcss_seek | |
328 || !DVDcss_read || !DVDcss_error || !dvdcss_version) { | |
329 fprintf(stderr, "libdvdread: Missing symbols in libdvdcss.so.2, " | |
330 "this shouldn't happen !\n"); | |
331 dlclose(dvdcss_library); | |
332 } | |
333 } | |
334 #endif /* HAVE_DVDCSS_DVDCSS_H */ | |
335 | |
336 if(dvdcss_library != NULL) { | |
337 /* | |
338 char *psz_method = getenv( "DVDCSS_METHOD" ); | |
339 char *psz_verbose = getenv( "DVDCSS_VERBOSE" ); | |
340 fprintf(stderr, "DVDCSS_METHOD %s\n", psz_method); | |
341 fprintf(stderr, "DVDCSS_VERBOSE %s\n", psz_verbose); | |
342 */ | |
343 fprintf(stderr, "libdvdread: Using libdvdcss version %s for DVD access\n", | |
344 *dvdcss_version); | |
345 | |
346 /* libdvdcss wrapper functions */ | |
347 dvdinput_open = css_open; | |
348 dvdinput_close = css_close; | |
349 dvdinput_seek = css_seek; | |
350 dvdinput_title = css_title; | |
351 dvdinput_read = css_read; | |
352 dvdinput_error = css_error; | |
353 return 1; | |
354 | |
355 } else { | |
356 fprintf(stderr, "libdvdread: Encrypted DVD support unavailable.\n"); | |
357 | |
358 /* libdvdcss replacement functions */ | |
359 dvdinput_open = file_open; | |
360 dvdinput_close = file_close; | |
361 dvdinput_seek = file_seek; | |
362 dvdinput_title = file_title; | |
363 dvdinput_read = file_read; | |
364 dvdinput_error = file_error; | |
365 return 0; | |
366 } | |
367 } |