1 |
/* |
2 |
* sys_unix.cpp - System dependent routines, Unix implementation |
3 |
* |
4 |
* Basilisk II (C) 1997-2001 Christian Bauer |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
|
21 |
#include "sysdeps.h" |
22 |
|
23 |
#include <sys/ioctl.h> |
24 |
#include <sys/stat.h> |
25 |
#include <errno.h> |
26 |
|
27 |
#ifdef __linux__ |
28 |
#include <sys/mount.h> |
29 |
#include <linux/cdrom.h> |
30 |
#include <linux/fd.h> |
31 |
#include <linux/major.h> |
32 |
#include <linux/kdev_t.h> |
33 |
#include <linux/unistd.h> |
34 |
|
35 |
#ifdef __NR__llseek |
36 |
_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo, loff_t *, res, uint, wh); |
37 |
#else |
38 |
static int _llseek(uint fd, ulong hi, ulong lo, loff_t *res, uint wh) |
39 |
{ |
40 |
if (hi) |
41 |
return -1; |
42 |
*res = lseek(fd, lo, wh); |
43 |
if (*res == -1) |
44 |
return -1; |
45 |
return 0; |
46 |
} |
47 |
#endif |
48 |
#endif |
49 |
|
50 |
#if defined(__FreeBSD__) || defined(__NetBSD__) |
51 |
#include <sys/cdio.h> |
52 |
#endif |
53 |
|
54 |
#include "main.h" |
55 |
#include "macos_util.h" |
56 |
#include "prefs.h" |
57 |
#include "user_strings.h" |
58 |
#include "sys.h" |
59 |
|
60 |
#define DEBUG 0 |
61 |
#include "debug.h" |
62 |
|
63 |
|
64 |
// File handles are pointers to these structures |
65 |
struct file_handle { |
66 |
char *name; // Copy of device/file name |
67 |
int fd; |
68 |
bool is_file; // Flag: plain file or /dev/something? |
69 |
bool is_floppy; // Flag: floppy device |
70 |
bool is_cdrom; // Flag: CD-ROM device |
71 |
bool read_only; // Copy of Sys_open() flag |
72 |
loff_t start_byte; // Size of file header (if any) |
73 |
loff_t file_size; // Size of file data (only valid if is_file is true) |
74 |
|
75 |
#if defined(__linux__) |
76 |
int cdrom_cap; // CD-ROM capability flags (only valid if is_cdrom is true) |
77 |
#elif defined(__FreeBSD__) |
78 |
struct ioc_capability cdrom_cap; |
79 |
#endif |
80 |
}; |
81 |
|
82 |
// File handle of first floppy drive (for SysMountFirstFloppy()) |
83 |
static file_handle *first_floppy = NULL; |
84 |
|
85 |
|
86 |
/* |
87 |
* Initialization |
88 |
*/ |
89 |
|
90 |
void SysInit(void) |
91 |
{ |
92 |
} |
93 |
|
94 |
|
95 |
/* |
96 |
* Deinitialization |
97 |
*/ |
98 |
|
99 |
void SysExit(void) |
100 |
{ |
101 |
} |
102 |
|
103 |
|
104 |
/* |
105 |
* Mount first floppy disk |
106 |
*/ |
107 |
|
108 |
void SysMountFirstFloppy(void) |
109 |
{ |
110 |
if (first_floppy) |
111 |
MountVolume(first_floppy); |
112 |
} |
113 |
|
114 |
|
115 |
/* |
116 |
* This gets called when no "floppy" prefs items are found |
117 |
* It scans for available floppy drives and adds appropriate prefs items |
118 |
*/ |
119 |
|
120 |
void SysAddFloppyPrefs(void) |
121 |
{ |
122 |
#if defined(__linux__) |
123 |
PrefsAddString("floppy", "/dev/fd0H1440"); |
124 |
PrefsAddString("floppy", "/dev/fd1H1440"); |
125 |
#elif defined(__NetBSD__) |
126 |
PrefsAddString("floppy", "/dev/fd0a"); |
127 |
PrefsAddString("floppy", "/dev/fd1a"); |
128 |
#else |
129 |
PrefsAddString("floppy", "/dev/fd0"); |
130 |
PrefsAddString("floppy", "/dev/fd1"); |
131 |
#endif |
132 |
} |
133 |
|
134 |
|
135 |
/* |
136 |
* This gets called when no "disk" prefs items are found |
137 |
* It scans for available HFS volumes and adds appropriate prefs items |
138 |
*/ |
139 |
|
140 |
void SysAddDiskPrefs(void) |
141 |
{ |
142 |
#ifdef __linux__ |
143 |
FILE *f = fopen("/etc/fstab", "r"); |
144 |
if (f) { |
145 |
char line[256]; |
146 |
while(fgets(line, 255, f)) { |
147 |
// Read line |
148 |
int len = strlen(line); |
149 |
if (len == 0 || line[0] == '#') |
150 |
continue; |
151 |
line[len-1] = 0; |
152 |
|
153 |
// Parse line |
154 |
char *dev, *mnt_point, *fstype; |
155 |
if (sscanf(line, "%as %as %as", &dev, &mnt_point, &fstype) == 3) { |
156 |
if (strcmp(fstype, "hfs") == 0) |
157 |
PrefsAddString("disk", dev); |
158 |
} |
159 |
free(dev); free(mnt_point); free(fstype); |
160 |
} |
161 |
fclose(f); |
162 |
} |
163 |
#endif |
164 |
} |
165 |
|
166 |
|
167 |
/* |
168 |
* This gets called when no "cdrom" prefs items are found |
169 |
* It scans for available CD-ROM drives and adds appropriate prefs items |
170 |
*/ |
171 |
|
172 |
void SysAddCDROMPrefs(void) |
173 |
{ |
174 |
// Don't scan for drives if nocdrom option given |
175 |
if (PrefsFindBool("nocdrom")) |
176 |
return; |
177 |
|
178 |
#if defined(__linux__) |
179 |
PrefsAddString("cdrom", "/dev/cdrom"); |
180 |
#elif defined(__FreeBSD__) |
181 |
PrefsAddString("cdrom", "/dev/cd0c"); |
182 |
#elif defined(__NetBSD__) |
183 |
PrefsAddString("cdrom", "/dev/cd0d"); |
184 |
#endif |
185 |
} |
186 |
|
187 |
|
188 |
/* |
189 |
* Add default serial prefs (must be added, even if no ports present) |
190 |
*/ |
191 |
|
192 |
void SysAddSerialPrefs(void) |
193 |
{ |
194 |
#if defined(__linux__) |
195 |
PrefsAddString("seriala", "/dev/ttyS0"); |
196 |
PrefsAddString("serialb", "/dev/ttyS1"); |
197 |
#elif defined(__FreeBSD__) |
198 |
PrefsAddString("seriala", "/dev/cuaa0"); |
199 |
PrefsAddString("serialb", "/dev/cuaa1"); |
200 |
#elif defined(__NetBSD__) |
201 |
PrefsAddString("seriala", "/dev/tty00"); |
202 |
PrefsAddString("serialb", "/dev/tty01"); |
203 |
#endif |
204 |
} |
205 |
|
206 |
|
207 |
/* |
208 |
* Check if device is a mounted HFS volume, get mount name |
209 |
*/ |
210 |
|
211 |
static bool is_drive_mounted(const char *dev_name, char *mount_name) |
212 |
{ |
213 |
#ifdef __linux__ |
214 |
FILE *f = fopen("/proc/mounts", "r"); |
215 |
if (f) { |
216 |
char line[256]; |
217 |
while(fgets(line, 255, f)) { |
218 |
// Read line |
219 |
int len = strlen(line); |
220 |
if (len == 0) |
221 |
continue; |
222 |
line[len-1] = 0; |
223 |
|
224 |
// Parse line |
225 |
if (strncmp(line, dev_name, strlen(dev_name)) == 0) { |
226 |
mount_name[0] = 0; |
227 |
char *dummy; |
228 |
sscanf(line, "%as %s", &dummy, mount_name); |
229 |
free(dummy); |
230 |
fclose(f); |
231 |
return true; |
232 |
} |
233 |
} |
234 |
fclose(f); |
235 |
} |
236 |
#endif |
237 |
return false; |
238 |
} |
239 |
|
240 |
|
241 |
/* |
242 |
* Open file/device, create new file handle (returns NULL on error) |
243 |
*/ |
244 |
|
245 |
void *Sys_open(const char *name, bool read_only) |
246 |
{ |
247 |
bool is_file = strncmp(name, "/dev/", 5) != 0; |
248 |
#if defined(__FreeBSD__) |
249 |
// SCSI IDE |
250 |
bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0 || strncmp(name, "/dev/acd", 8) == 0; |
251 |
#else |
252 |
bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0; |
253 |
#endif |
254 |
|
255 |
D(bug("Sys_open(%s, %s)\n", name, read_only ? "read-only" : "read/write")); |
256 |
|
257 |
// Check if write access is allowed, set read-only flag if not |
258 |
if (!read_only && access(name, W_OK)) |
259 |
read_only = true; |
260 |
|
261 |
// Print warning message and eventually unmount drive when this is an HFS volume mounted under Linux (double mounting will corrupt the volume) |
262 |
char mount_name[256]; |
263 |
if (!is_file && !read_only && is_drive_mounted(name, mount_name)) { |
264 |
char str[512]; |
265 |
sprintf(str, GetString(STR_VOLUME_IS_MOUNTED_WARN), mount_name); |
266 |
WarningAlert(str); |
267 |
sprintf(str, "umount %s", mount_name); |
268 |
if (system(str)) { |
269 |
sprintf(str, GetString(STR_CANNOT_UNMOUNT_WARN), mount_name, strerror(errno)); |
270 |
WarningAlert(str); |
271 |
return NULL; |
272 |
} |
273 |
} |
274 |
|
275 |
// Open file/device |
276 |
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) |
277 |
int fd = open(name, (read_only ? O_RDONLY : O_RDWR) | (is_cdrom ? O_NONBLOCK : 0)); |
278 |
#else |
279 |
int fd = open(name, read_only ? O_RDONLY : O_RDWR); |
280 |
#endif |
281 |
if (fd < 0 && !read_only) { |
282 |
// Read-write failed, try read-only |
283 |
read_only = true; |
284 |
fd = open(name, O_RDONLY); |
285 |
} |
286 |
if (fd >= 0) { |
287 |
file_handle *fh = new file_handle; |
288 |
fh->name = strdup(name); |
289 |
fh->fd = fd; |
290 |
fh->is_file = is_file; |
291 |
fh->read_only = read_only; |
292 |
fh->start_byte = 0; |
293 |
fh->is_floppy = false; |
294 |
fh->is_cdrom = false; |
295 |
if (fh->is_file) { |
296 |
// Detect disk image file layout |
297 |
loff_t size = 0; |
298 |
#if defined(__linux__) |
299 |
_llseek(fh->fd, 0, 0, &size, SEEK_END); |
300 |
#else |
301 |
size = lseek(fd, 0, SEEK_END); |
302 |
#endif |
303 |
uint8 data[256]; |
304 |
lseek(fd, 0, SEEK_SET); |
305 |
read(fd, data, 256); |
306 |
FileDiskLayout(size, data, fh->start_byte, fh->file_size); |
307 |
} else { |
308 |
struct stat st; |
309 |
if (fstat(fd, &st) == 0) { |
310 |
if (S_ISBLK(st.st_mode)) { |
311 |
fh->is_cdrom = is_cdrom; |
312 |
#if defined(__linux__) |
313 |
fh->is_floppy = (MAJOR(st.st_rdev) == FLOPPY_MAJOR); |
314 |
#ifdef CDROM_GET_CAPABILITY |
315 |
if (is_cdrom) { |
316 |
fh->cdrom_cap = ioctl(fh->fd, CDROM_GET_CAPABILITY); |
317 |
if (fh->cdrom_cap < 0) |
318 |
fh->cdrom_cap = 0; |
319 |
} |
320 |
#else |
321 |
fh->cdrom_cap = 0; |
322 |
#endif |
323 |
#elif defined(__FreeBSD__) |
324 |
fh->is_floppy = ((st.st_rdev >> 16) == 2); |
325 |
#ifdef CDIOCCAPABILITY |
326 |
if (is_cdrom) { |
327 |
if (ioctl(fh->fd, CDIOCCAPABILITY, &fh->cdrom_cap) < 0) |
328 |
memset(&fh->cdrom_cap, 0, sizeof(fh->cdrom_cap)); |
329 |
} |
330 |
#else |
331 |
fh->cdrom_cap = 0; |
332 |
#endif |
333 |
#elif defined(__NetBSD__) |
334 |
fh->is_floppy = ((st.st_rdev >> 16) == 2); |
335 |
#endif |
336 |
} |
337 |
} |
338 |
} |
339 |
if (fh->is_floppy && first_floppy == NULL) |
340 |
first_floppy = fh; |
341 |
return fh; |
342 |
} else { |
343 |
printf("WARNING: Cannot open %s (%s)\n", name, strerror(errno)); |
344 |
return NULL; |
345 |
} |
346 |
} |
347 |
|
348 |
|
349 |
/* |
350 |
* Close file/device, delete file handle |
351 |
*/ |
352 |
|
353 |
void Sys_close(void *arg) |
354 |
{ |
355 |
file_handle *fh = (file_handle *)arg; |
356 |
if (!fh) |
357 |
return; |
358 |
|
359 |
close(fh->fd); |
360 |
if (fh->name) |
361 |
free(fh->name); |
362 |
delete fh; |
363 |
} |
364 |
|
365 |
|
366 |
/* |
367 |
* Read "length" bytes from file/device, starting at "offset", to "buffer", |
368 |
* returns number of bytes read (or 0) |
369 |
*/ |
370 |
|
371 |
size_t Sys_read(void *arg, void *buffer, loff_t offset, size_t length) |
372 |
{ |
373 |
file_handle *fh = (file_handle *)arg; |
374 |
if (!fh) |
375 |
return 0; |
376 |
|
377 |
// Seek to position |
378 |
#if defined(__linux__) |
379 |
loff_t pos = offset + fh->start_byte, res; |
380 |
if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0) |
381 |
return 0; |
382 |
#else |
383 |
if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0) |
384 |
return 0; |
385 |
#endif |
386 |
|
387 |
// Read data |
388 |
return read(fh->fd, buffer, length); |
389 |
} |
390 |
|
391 |
|
392 |
/* |
393 |
* Write "length" bytes from "buffer" to file/device, starting at "offset", |
394 |
* returns number of bytes written (or 0) |
395 |
*/ |
396 |
|
397 |
size_t Sys_write(void *arg, void *buffer, loff_t offset, size_t length) |
398 |
{ |
399 |
file_handle *fh = (file_handle *)arg; |
400 |
if (!fh) |
401 |
return 0; |
402 |
|
403 |
// Seek to position |
404 |
#if defined(__linux__) |
405 |
loff_t pos = offset + fh->start_byte, res; |
406 |
if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0) |
407 |
return 0; |
408 |
#else |
409 |
if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0) |
410 |
return 0; |
411 |
#endif |
412 |
|
413 |
// Write data |
414 |
return write(fh->fd, buffer, length); |
415 |
} |
416 |
|
417 |
|
418 |
/* |
419 |
* Return size of file/device (minus header) |
420 |
*/ |
421 |
|
422 |
loff_t SysGetFileSize(void *arg) |
423 |
{ |
424 |
file_handle *fh = (file_handle *)arg; |
425 |
if (!fh) |
426 |
return true; |
427 |
|
428 |
if (fh->is_file) |
429 |
return fh->file_size; |
430 |
else { |
431 |
#if defined(__linux__) |
432 |
long blocks; |
433 |
if (ioctl(fh->fd, BLKGETSIZE, &blocks) < 0) |
434 |
return 0; |
435 |
D(bug(" BLKGETSIZE returns %d blocks\n", blocks)); |
436 |
return (loff_t)blocks * 512; |
437 |
#else |
438 |
return lseek(fh->fd, 0, SEEK_END) - fh->start_byte; |
439 |
#endif |
440 |
} |
441 |
} |
442 |
|
443 |
|
444 |
/* |
445 |
* Eject volume (if applicable) |
446 |
*/ |
447 |
|
448 |
void SysEject(void *arg) |
449 |
{ |
450 |
file_handle *fh = (file_handle *)arg; |
451 |
if (!fh) |
452 |
return; |
453 |
|
454 |
#if defined(__linux__) |
455 |
if (fh->is_floppy) { |
456 |
fsync(fh->fd); |
457 |
ioctl(fh->fd, FDFLUSH); |
458 |
ioctl(fh->fd, FDEJECT); |
459 |
} else if (fh->is_cdrom) { |
460 |
ioctl(fh->fd, CDROMEJECT); |
461 |
close(fh->fd); // Close and reopen so the driver will see the media change |
462 |
fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK); |
463 |
} |
464 |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
465 |
if (fh->is_floppy) { |
466 |
fsync(fh->fd); |
467 |
//ioctl(fh->fd, FDFLUSH); |
468 |
//ioctl(fh->fd, FDEJECT); |
469 |
} else if (fh->is_cdrom) { |
470 |
ioctl(fh->fd, CDIOCEJECT); |
471 |
close(fh->fd); // Close and reopen so the driver will see the media change |
472 |
fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK); |
473 |
} |
474 |
#endif |
475 |
} |
476 |
|
477 |
|
478 |
/* |
479 |
* Format volume (if applicable) |
480 |
*/ |
481 |
|
482 |
bool SysFormat(void *arg) |
483 |
{ |
484 |
file_handle *fh = (file_handle *)arg; |
485 |
if (!fh) |
486 |
return false; |
487 |
|
488 |
//!! |
489 |
return true; |
490 |
} |
491 |
|
492 |
|
493 |
/* |
494 |
* Check if file/device is read-only (this includes the read-only flag on Sys_open()) |
495 |
*/ |
496 |
|
497 |
bool SysIsReadOnly(void *arg) |
498 |
{ |
499 |
file_handle *fh = (file_handle *)arg; |
500 |
if (!fh) |
501 |
return true; |
502 |
|
503 |
#if defined(__linux__) |
504 |
if (fh->is_floppy) { |
505 |
struct floppy_drive_struct stat; |
506 |
ioctl(fh->fd, FDGETDRVSTAT, &stat); |
507 |
return !(stat.flags & FD_DISK_WRITABLE); |
508 |
} else |
509 |
#endif |
510 |
return fh->read_only; |
511 |
} |
512 |
|
513 |
|
514 |
/* |
515 |
* Check if the given file handle refers to a fixed or a removable disk |
516 |
*/ |
517 |
|
518 |
bool SysIsFixedDisk(void *arg) |
519 |
{ |
520 |
file_handle *fh = (file_handle *)arg; |
521 |
if (!fh) |
522 |
return true; |
523 |
|
524 |
if (fh->is_file) |
525 |
return true; |
526 |
else if (fh->is_floppy || fh->is_cdrom) |
527 |
return false; |
528 |
else |
529 |
return true; |
530 |
} |
531 |
|
532 |
|
533 |
/* |
534 |
* Check if a disk is inserted in the drive (always true for files) |
535 |
*/ |
536 |
|
537 |
bool SysIsDiskInserted(void *arg) |
538 |
{ |
539 |
file_handle *fh = (file_handle *)arg; |
540 |
if (!fh) |
541 |
return false; |
542 |
|
543 |
if (fh->is_file) { |
544 |
return true; |
545 |
|
546 |
#if defined(__linux__) |
547 |
} else if (fh->is_floppy) { |
548 |
char block[512]; |
549 |
lseek(fh->fd, 0, SEEK_SET); |
550 |
return read(fh->fd, block, 512) == 512; |
551 |
} else if (fh->is_cdrom) { |
552 |
#ifdef CDROM_MEDIA_CHANGED |
553 |
if (fh->cdrom_cap & CDC_MEDIA_CHANGED) { |
554 |
// If we don't do this, all attempts to read from a disc fail |
555 |
// once the tray has been open (altough the TOC reads fine). |
556 |
// Can somebody explain this to me? |
557 |
if (ioctl(fh->fd, CDROM_MEDIA_CHANGED) == 1) { |
558 |
close(fh->fd); |
559 |
fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK); |
560 |
} |
561 |
} |
562 |
#endif |
563 |
#ifdef CDROM_DRIVE_STATUS |
564 |
if (fh->cdrom_cap & CDC_DRIVE_STATUS) { |
565 |
return ioctl(fh->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK; |
566 |
} |
567 |
#endif |
568 |
cdrom_tochdr header; |
569 |
return ioctl(fh->fd, CDROMREADTOCHDR, &header) == 0; |
570 |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
571 |
} else if (fh->is_floppy) { |
572 |
return false; //!! |
573 |
} else if (fh->is_cdrom) { |
574 |
struct ioc_toc_header header; |
575 |
return ioctl(fh->fd, CDIOREADTOCHEADER, &header) == 0; |
576 |
#endif |
577 |
|
578 |
} else |
579 |
return true; |
580 |
} |
581 |
|
582 |
|
583 |
/* |
584 |
* Prevent medium removal (if applicable) |
585 |
*/ |
586 |
|
587 |
void SysPreventRemoval(void *arg) |
588 |
{ |
589 |
file_handle *fh = (file_handle *)arg; |
590 |
if (!fh) |
591 |
return; |
592 |
|
593 |
#if defined(__linux__) && defined(CDROM_LOCKDOOR) |
594 |
if (fh->is_cdrom) |
595 |
ioctl(fh->fd, CDROM_LOCKDOOR, 1); |
596 |
#endif |
597 |
} |
598 |
|
599 |
|
600 |
/* |
601 |
* Allow medium removal (if applicable) |
602 |
*/ |
603 |
|
604 |
void SysAllowRemoval(void *arg) |
605 |
{ |
606 |
file_handle *fh = (file_handle *)arg; |
607 |
if (!fh) |
608 |
return; |
609 |
|
610 |
#if defined(__linux__) && defined(CDROM_LOCKDOOR) |
611 |
if (fh->is_cdrom) |
612 |
ioctl(fh->fd, CDROM_LOCKDOOR, 0); |
613 |
#endif |
614 |
} |
615 |
|
616 |
|
617 |
/* |
618 |
* Read CD-ROM TOC (binary MSF format, 804 bytes max.) |
619 |
*/ |
620 |
|
621 |
bool SysCDReadTOC(void *arg, uint8 *toc) |
622 |
{ |
623 |
file_handle *fh = (file_handle *)arg; |
624 |
if (!fh) |
625 |
return false; |
626 |
|
627 |
if (fh->is_cdrom) { |
628 |
#if defined(__linux__) |
629 |
uint8 *p = toc + 2; |
630 |
|
631 |
// Header |
632 |
cdrom_tochdr header; |
633 |
if (ioctl(fh->fd, CDROMREADTOCHDR, &header) < 0) |
634 |
return false; |
635 |
*p++ = header.cdth_trk0; |
636 |
*p++ = header.cdth_trk1; |
637 |
|
638 |
// Tracks |
639 |
cdrom_tocentry entry; |
640 |
for (int i=header.cdth_trk0; i<=header.cdth_trk1; i++) { |
641 |
entry.cdte_track = i; |
642 |
entry.cdte_format = CDROM_MSF; |
643 |
if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0) |
644 |
return false; |
645 |
*p++ = 0; |
646 |
*p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl; |
647 |
*p++ = entry.cdte_track; |
648 |
*p++ = 0; |
649 |
*p++ = 0; |
650 |
*p++ = entry.cdte_addr.msf.minute; |
651 |
*p++ = entry.cdte_addr.msf.second; |
652 |
*p++ = entry.cdte_addr.msf.frame; |
653 |
} |
654 |
|
655 |
// Leadout track |
656 |
entry.cdte_track = CDROM_LEADOUT; |
657 |
entry.cdte_format = CDROM_MSF; |
658 |
if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0) |
659 |
return false; |
660 |
*p++ = 0; |
661 |
*p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl; |
662 |
*p++ = entry.cdte_track; |
663 |
*p++ = 0; |
664 |
*p++ = 0; |
665 |
*p++ = entry.cdte_addr.msf.minute; |
666 |
*p++ = entry.cdte_addr.msf.second; |
667 |
*p++ = entry.cdte_addr.msf.frame; |
668 |
|
669 |
// TOC size |
670 |
int toc_size = p - toc; |
671 |
*toc++ = toc_size >> 8; |
672 |
*toc++ = toc_size & 0xff; |
673 |
return true; |
674 |
#elif defined(__FreeBSD__) |
675 |
uint8 *p = toc + 2; |
676 |
|
677 |
// Header |
678 |
struct ioc_toc_header header; |
679 |
if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0) |
680 |
return false; |
681 |
*p++ = header.starting_track; |
682 |
*p++ = header.ending_track; |
683 |
|
684 |
// Tracks |
685 |
struct ioc_read_toc_single_entry entry; |
686 |
for (int i=header.starting_track; i<=header.ending_track; i++) { |
687 |
entry.track = i; |
688 |
entry.address_format = CD_MSF_FORMAT; |
689 |
if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0) |
690 |
return false; |
691 |
*p++ = 0; |
692 |
*p++ = (entry.entry.addr_type << 4) | entry.entry.control; |
693 |
*p++ = entry.entry.track; |
694 |
*p++ = 0; |
695 |
*p++ = 0; |
696 |
*p++ = entry.entry.addr.msf.minute; |
697 |
*p++ = entry.entry.addr.msf.second; |
698 |
*p++ = entry.entry.addr.msf.frame; |
699 |
} |
700 |
|
701 |
// Leadout track |
702 |
entry.track = CD_TRACK_INFO; |
703 |
entry.address_format = CD_MSF_FORMAT; |
704 |
if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0) |
705 |
return false; |
706 |
*p++ = 0; |
707 |
*p++ = (entry.entry.addr_type << 4) | entry.entry.control; |
708 |
*p++ = entry.entry.track; |
709 |
*p++ = 0; |
710 |
*p++ = 0; |
711 |
*p++ = entry.entry.addr.msf.minute; |
712 |
*p++ = entry.entry.addr.msf.second; |
713 |
*p++ = entry.entry.addr.msf.frame; |
714 |
|
715 |
// TOC size |
716 |
int toc_size = p - toc; |
717 |
*toc++ = toc_size >> 8; |
718 |
*toc++ = toc_size & 0xff; |
719 |
return true; |
720 |
#elif defined(__NetBSD__) |
721 |
uint8 *p = toc + 2; |
722 |
|
723 |
// Header |
724 |
struct ioc_toc_header header; |
725 |
if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0) |
726 |
return false; |
727 |
*p++ = header.starting_track; |
728 |
*p++ = header.ending_track; |
729 |
|
730 |
// Tracks (this is nice... :-) |
731 |
struct ioc_read_toc_entry entries; |
732 |
entries.address_format = CD_MSF_FORMAT; |
733 |
entries.starting_track = 1; |
734 |
entries.data_len = 800; |
735 |
entries.data = (cd_toc_entry *)p; |
736 |
if (ioctl(fh->fd, CDIOREADTOCENTRIES, &entries) < 0) |
737 |
return false; |
738 |
|
739 |
// TOC size |
740 |
int toc_size = p - toc; |
741 |
*toc++ = toc_size >> 8; |
742 |
*toc++ = toc_size & 0xff; |
743 |
return true; |
744 |
#endif |
745 |
} else |
746 |
return false; |
747 |
} |
748 |
|
749 |
|
750 |
/* |
751 |
* Read CD-ROM position data (Sub-Q Channel, 16 bytes, see SCSI standard) |
752 |
*/ |
753 |
|
754 |
bool SysCDGetPosition(void *arg, uint8 *pos) |
755 |
{ |
756 |
file_handle *fh = (file_handle *)arg; |
757 |
if (!fh) |
758 |
return false; |
759 |
|
760 |
if (fh->is_cdrom) { |
761 |
#if defined(__linux__) |
762 |
cdrom_subchnl chan; |
763 |
chan.cdsc_format = CDROM_MSF; |
764 |
if (ioctl(fh->fd, CDROMSUBCHNL, &chan) < 0) |
765 |
return false; |
766 |
*pos++ = 0; |
767 |
*pos++ = chan.cdsc_audiostatus; |
768 |
*pos++ = 0; |
769 |
*pos++ = 12; // Sub-Q data length |
770 |
*pos++ = 0; |
771 |
*pos++ = (chan.cdsc_adr << 4) | chan.cdsc_ctrl; |
772 |
*pos++ = chan.cdsc_trk; |
773 |
*pos++ = chan.cdsc_ind; |
774 |
*pos++ = 0; |
775 |
*pos++ = chan.cdsc_absaddr.msf.minute; |
776 |
*pos++ = chan.cdsc_absaddr.msf.second; |
777 |
*pos++ = chan.cdsc_absaddr.msf.frame; |
778 |
*pos++ = 0; |
779 |
*pos++ = chan.cdsc_reladdr.msf.minute; |
780 |
*pos++ = chan.cdsc_reladdr.msf.second; |
781 |
*pos++ = chan.cdsc_reladdr.msf.frame; |
782 |
return true; |
783 |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
784 |
struct ioc_read_subchannel chan; |
785 |
chan.data_format = CD_MSF_FORMAT; |
786 |
chan.address_format = CD_MSF_FORMAT; |
787 |
chan.track = CD_CURRENT_POSITION; |
788 |
if (ioctl(fh->fd, CDIOCREADSUBCHANNEL, &chan) < 0) |
789 |
return false; |
790 |
*pos++ = 0; |
791 |
*pos++ = chan.data->header.audio_status; |
792 |
*pos++ = 0; |
793 |
*pos++ = 12; // Sub-Q data length |
794 |
*pos++ = 0; |
795 |
*pos++ = (chan.data->what.position.addr_type << 4) | chan.data->what.position.control; |
796 |
*pos++ = chan.data->what.position.track_number; |
797 |
*pos++ = chan.data->what.position.index_number; |
798 |
*pos++ = 0; |
799 |
*pos++ = chan.data->what.position.absaddr.msf.minute; |
800 |
*pos++ = chan.data->what.position.absaddr.msf.second; |
801 |
*pos++ = chan.data->what.position.absaddr.msf.frame; |
802 |
*pos++ = 0; |
803 |
*pos++ = chan.data->what.position.reladdr.msf.minute; |
804 |
*pos++ = chan.data->what.position.reladdr.msf.second; |
805 |
*pos++ = chan.data->what.position.reladdr.msf.frame; |
806 |
return true; |
807 |
#endif |
808 |
} else |
809 |
return false; |
810 |
} |
811 |
|
812 |
|
813 |
/* |
814 |
* Play CD audio |
815 |
*/ |
816 |
|
817 |
bool SysCDPlay(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, uint8 end_m, uint8 end_s, uint8 end_f) |
818 |
{ |
819 |
file_handle *fh = (file_handle *)arg; |
820 |
if (!fh) |
821 |
return false; |
822 |
|
823 |
if (fh->is_cdrom) { |
824 |
#if defined(__linux__) |
825 |
cdrom_msf play; |
826 |
play.cdmsf_min0 = start_m; |
827 |
play.cdmsf_sec0 = start_s; |
828 |
play.cdmsf_frame0 = start_f; |
829 |
play.cdmsf_min1 = end_m; |
830 |
play.cdmsf_sec1 = end_s; |
831 |
play.cdmsf_frame1 = end_f; |
832 |
return ioctl(fh->fd, CDROMPLAYMSF, &play) == 0; |
833 |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
834 |
struct ioc_play_msf play; |
835 |
play.start_m = start_m; |
836 |
play.start_s = start_s; |
837 |
play.start_f = start_f; |
838 |
play.end_m = end_m; |
839 |
play.end_s = end_s; |
840 |
play.end_f = end_f; |
841 |
return ioctl(fh->fd, CDIOCPLAYMSF, &play) == 0; |
842 |
#endif |
843 |
} else |
844 |
return false; |
845 |
} |
846 |
|
847 |
|
848 |
/* |
849 |
* Pause CD audio |
850 |
*/ |
851 |
|
852 |
bool SysCDPause(void *arg) |
853 |
{ |
854 |
file_handle *fh = (file_handle *)arg; |
855 |
if (!fh) |
856 |
return false; |
857 |
|
858 |
if (fh->is_cdrom) { |
859 |
#if defined(__linux__) |
860 |
return ioctl(fh->fd, CDROMPAUSE) == 0; |
861 |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
862 |
return ioctl(fh->fd, CDIOCPAUSE) == 0; |
863 |
#endif |
864 |
} else |
865 |
return false; |
866 |
} |
867 |
|
868 |
|
869 |
/* |
870 |
* Resume paused CD audio |
871 |
*/ |
872 |
|
873 |
bool SysCDResume(void *arg) |
874 |
{ |
875 |
file_handle *fh = (file_handle *)arg; |
876 |
if (!fh) |
877 |
return false; |
878 |
|
879 |
if (fh->is_cdrom) { |
880 |
#if defined(__linux__) |
881 |
return ioctl(fh->fd, CDROMRESUME) == 0; |
882 |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
883 |
return ioctl(fh->fd, CDIOCRESUME) == 0; |
884 |
#endif |
885 |
} else |
886 |
return false; |
887 |
} |
888 |
|
889 |
|
890 |
/* |
891 |
* Stop CD audio |
892 |
*/ |
893 |
|
894 |
bool SysCDStop(void *arg, uint8 lead_out_m, uint8 lead_out_s, uint8 lead_out_f) |
895 |
{ |
896 |
file_handle *fh = (file_handle *)arg; |
897 |
if (!fh) |
898 |
return false; |
899 |
|
900 |
if (fh->is_cdrom) { |
901 |
#if defined(__linux__) |
902 |
return ioctl(fh->fd, CDROMSTOP) == 0; |
903 |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
904 |
return ioctl(fh->fd, CDIOCSTOP) == 0; |
905 |
#endif |
906 |
} else |
907 |
return false; |
908 |
} |
909 |
|
910 |
|
911 |
/* |
912 |
* Perform CD audio fast-forward/fast-reverse operation starting from specified address |
913 |
*/ |
914 |
|
915 |
bool SysCDScan(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, bool reverse) |
916 |
{ |
917 |
file_handle *fh = (file_handle *)arg; |
918 |
if (!fh) |
919 |
return false; |
920 |
|
921 |
// Not supported under Linux |
922 |
return false; |
923 |
} |
924 |
|
925 |
|
926 |
/* |
927 |
* Set CD audio volume (0..255 each channel) |
928 |
*/ |
929 |
|
930 |
void SysCDSetVolume(void *arg, uint8 left, uint8 right) |
931 |
{ |
932 |
file_handle *fh = (file_handle *)arg; |
933 |
if (!fh) |
934 |
return; |
935 |
|
936 |
if (fh->is_cdrom) { |
937 |
#if defined(__linux__) |
938 |
cdrom_volctrl vol; |
939 |
vol.channel0 = vol.channel2 = left; |
940 |
vol.channel1 = vol.channel3 = right; |
941 |
ioctl(fh->fd, CDROMVOLCTRL, &vol); |
942 |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
943 |
struct ioc_vol vol; |
944 |
vol.vol[0] = vol.vol[2] = left; |
945 |
vol.vol[1] = vol.vol[3] = right; |
946 |
ioctl(fh->fd, CDIOCSETVOL, &vol); |
947 |
#endif |
948 |
} |
949 |
} |
950 |
|
951 |
|
952 |
/* |
953 |
* Get CD audio volume (0..255 each channel) |
954 |
*/ |
955 |
|
956 |
void SysCDGetVolume(void *arg, uint8 &left, uint8 &right) |
957 |
{ |
958 |
file_handle *fh = (file_handle *)arg; |
959 |
if (!fh) |
960 |
return; |
961 |
|
962 |
left = right = 0; |
963 |
if (fh->is_cdrom) { |
964 |
#if defined(__linux__) |
965 |
cdrom_volctrl vol; |
966 |
ioctl(fh->fd, CDROMVOLREAD, &vol); |
967 |
left = vol.channel0; |
968 |
right = vol.channel1; |
969 |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
970 |
struct ioc_vol vol; |
971 |
ioctl(fh->fd, CDIOCGETVOL, &vol); |
972 |
left = vol.vol[0]; |
973 |
right = vol.vol[1]; |
974 |
#endif |
975 |
} |
976 |
} |