ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sys_unix.cpp
Revision: 1.20
Committed: 2003-10-12T21:55:44Z (20 years, 9 months ago) by gbeauche
Branch: MAIN
Changes since 1.19: +11 -1 lines
Log Message:
Add hacks to disable "floppy" & "serial[ab]" entries that would cause
Basilisk II to hang on MacOS X as an X11 build. I am not sure this is
supposed to work in an Aqua build so I leave that enable there, assuming
<AvailabilityMacros.h> is included somehow, thus defining MAC_OS_X_VERSION_*
and friends.

File Contents

# Content
1 /*
2 * sys_unix.cpp - System dependent routines, Unix implementation
3 *
4 * Basilisk II (C) 1997-2003 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 #include <dirent.h>
35
36 #ifdef __NR__llseek
37 _syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo, loff_t *, res, unsigned int, wh);
38 #else
39 static int _llseek(unsigned int fd, unsigned long hi, unsigned long lo, loff_t *res, unsigned int wh)
40 {
41 if (hi)
42 return -1;
43 *res = lseek(fd, lo, wh);
44 if (*res == -1)
45 return -1;
46 return 0;
47 }
48 #endif
49 #endif
50
51 #if defined(__FreeBSD__) || defined(__NetBSD__)
52 #include <sys/cdio.h>
53 #endif
54
55 #include "main.h"
56 #include "macos_util.h"
57 #include "prefs.h"
58 #include "user_strings.h"
59 #include "sys.h"
60
61 #define DEBUG 0
62 #include "debug.h"
63
64
65 // File handles are pointers to these structures
66 struct file_handle {
67 char *name; // Copy of device/file name
68 int fd;
69 bool is_file; // Flag: plain file or /dev/something?
70 bool is_floppy; // Flag: floppy device
71 bool is_cdrom; // Flag: CD-ROM device
72 bool read_only; // Copy of Sys_open() flag
73 loff_t start_byte; // Size of file header (if any)
74 loff_t file_size; // Size of file data (only valid if is_file is true)
75
76 #if defined(__linux__)
77 int cdrom_cap; // CD-ROM capability flags (only valid if is_cdrom is true)
78 #elif defined(__FreeBSD__)
79 struct ioc_capability cdrom_cap;
80 #elif defined(__APPLE__) && defined(__MACH__)
81 char *ioctl_name; // For CDs on OS X - a device for special ioctls
82 int ioctl_fd;
83 #endif
84 };
85
86 // File handle of first floppy drive (for SysMountFirstFloppy())
87 static file_handle *first_floppy = NULL;
88
89
90 /*
91 * Initialization
92 */
93
94 void SysInit(void)
95 {
96 }
97
98
99 /*
100 * Deinitialization
101 */
102
103 void SysExit(void)
104 {
105 }
106
107
108 /*
109 * Mount first floppy disk
110 */
111
112 void SysMountFirstFloppy(void)
113 {
114 if (first_floppy)
115 MountVolume(first_floppy);
116 }
117
118
119 /*
120 * This gets called when no "floppy" prefs items are found
121 * It scans for available floppy drives and adds appropriate prefs items
122 */
123
124 void SysAddFloppyPrefs(void)
125 {
126 #if defined(__linux__)
127 if (access("/dev/.devfsd", F_OK) < 0) {
128 PrefsAddString("floppy", "/dev/fd0u1440");
129 PrefsAddString("floppy", "/dev/fd1u1440");
130 } else {
131 DIR *fd_dir = opendir("/dev/floppy");
132 if (fd_dir) {
133 struct dirent *floppy_dev;
134 while ((floppy_dev = readdir(fd_dir)) != NULL) {
135 if (strstr(floppy_dev->d_name, "u1440") != NULL) {
136 char fd_dev[20];
137 sprintf(fd_dev, "/dev/floppy/%s", floppy_dev->d_name);
138 PrefsAddString("floppy", fd_dev);
139 }
140 }
141 closedir(fd_dir);
142 }
143 }
144 #elif defined(__NetBSD__)
145 PrefsAddString("floppy", "/dev/fd0a");
146 PrefsAddString("floppy", "/dev/fd1a");
147 #elif defined(__APPLE__) && defined(__MACH__)
148 // FIXME: We assume an Aqua build causes <AvailabilityMacros.h> to
149 // be included, thusly enabling this part of code that would cause
150 // Basilisk II to hang otherwise.
151 #ifdef MAC_OS_X_VERSION_10_0
152 PrefsAddString("floppy", "/dev/fd/0");
153 PrefsAddString("floppy", "/dev/fd/1");
154 #endif
155 #else
156 PrefsAddString("floppy", "/dev/fd0");
157 PrefsAddString("floppy", "/dev/fd1");
158 #endif
159 }
160
161
162 /*
163 * This gets called when no "disk" prefs items are found
164 * It scans for available HFS volumes and adds appropriate prefs items
165 * On OS X, we could do the same, but on an OS X machine I think it is
166 * very unlikely that any mounted volumes would contain a system which
167 * is old enough to boot a 68k Mac, so we just do nothing here for now.
168 */
169
170 void SysAddDiskPrefs(void)
171 {
172 #ifdef __linux__
173 FILE *f = fopen("/etc/fstab", "r");
174 if (f) {
175 char line[256];
176 while(fgets(line, 255, f)) {
177 // Read line
178 int len = strlen(line);
179 if (len == 0 || line[0] == '#')
180 continue;
181 line[len-1] = 0;
182
183 // Parse line
184 char *dev, *mnt_point, *fstype;
185 if (sscanf(line, "%as %as %as", &dev, &mnt_point, &fstype) == 3) {
186 if (strcmp(fstype, "hfs") == 0)
187 PrefsAddString("disk", dev);
188 }
189 free(dev); free(mnt_point); free(fstype);
190 }
191 fclose(f);
192 }
193 #endif
194 }
195
196
197 /*
198 * This gets called when no "cdrom" prefs items are found
199 * It scans for available CD-ROM drives and adds appropriate prefs items
200 */
201
202 void SysAddCDROMPrefs(void)
203 {
204 // Don't scan for drives if nocdrom option given
205 if (PrefsFindBool("nocdrom"))
206 return;
207
208 #if defined(__linux__)
209 if (access("/dev/.devfsd", F_OK) < 0)
210 PrefsAddString("cdrom", "/dev/cdrom");
211 else {
212 DIR *cd_dir = opendir("/dev/cdroms");
213 if (cd_dir) {
214 struct dirent *cdrom_dev;
215 while ((cdrom_dev = readdir(cd_dir)) != NULL) {
216 if (strcmp(cdrom_dev->d_name, ".") != 0 && strcmp(cdrom_dev->d_name, "..") != 0) {
217 char cd_dev[20];
218 sprintf(cd_dev, "/dev/cdroms/%s", cdrom_dev->d_name);
219 PrefsAddString("cdrom", cd_dev);
220 }
221 }
222 closedir(cd_dir);
223 }
224 }
225 #elif defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_0)
226 extern void DarwinAddCDROMPrefs(void);
227
228 DarwinAddCDROMPrefs();
229 #elif defined(__FreeBSD__) || defined(__NetBSD__)
230 PrefsAddString("cdrom", "/dev/cd0c");
231 #endif
232 }
233
234
235 /*
236 * Add default serial prefs (must be added, even if no ports present)
237 */
238
239 void SysAddSerialPrefs(void)
240 {
241 #if defined(__linux__)
242 if (access("/dev/.devfsd", F_OK) < 0) {
243 PrefsAddString("seriala", "/dev/ttyS0");
244 PrefsAddString("serialb", "/dev/ttyS1");
245 } else {
246 PrefsAddString("seriala", "/dev/tts/0");
247 PrefsAddString("serialb", "/dev/tts/1");
248 }
249 #elif defined(__FreeBSD__)
250 PrefsAddString("seriala", "/dev/cuaa0");
251 PrefsAddString("serialb", "/dev/cuaa1");
252 #elif defined(__NetBSD__)
253 PrefsAddString("seriala", "/dev/tty00");
254 PrefsAddString("serialb", "/dev/tty01");
255 #elif defined(__APPLE__) && defined(__MACH__)
256 // FIXME: We assume an Aqua build causes <AvailabilityMacros.h> to
257 // be included, thusly enabling this part of code that would cause
258 // Basilisk II to hang otherwise.
259 #ifdef MAC_OS_X_VERSION_10_0
260 PrefsAddString("seriala", "/dev/ttys0");
261 PrefsAddString("serialb", "/dev/ttys1");
262 #endif
263 // PrefsAddString("seriala", "/dev/cu.modem");
264 // PrefsAddString("serialb", "/dev/cu.IrDA-IrCOMMch-b");
265 #endif
266 }
267
268
269 /*
270 * Check if device is a mounted HFS volume, get mount name
271 */
272
273 static bool is_drive_mounted(const char *dev_name, char *mount_name)
274 {
275 #ifdef __linux__
276 FILE *f = fopen("/proc/mounts", "r");
277 if (f) {
278 char line[256];
279 while(fgets(line, 255, f)) {
280 // Read line
281 int len = strlen(line);
282 if (len == 0)
283 continue;
284 line[len-1] = 0;
285
286 // Parse line
287 if (strncmp(line, dev_name, strlen(dev_name)) == 0) {
288 mount_name[0] = 0;
289 char *dummy;
290 sscanf(line, "%as %s", &dummy, mount_name);
291 free(dummy);
292 fclose(f);
293 return true;
294 }
295 }
296 fclose(f);
297 }
298 #endif
299 return false;
300 }
301
302
303 /*
304 * Open file/device, create new file handle (returns NULL on error)
305 */
306
307 void *Sys_open(const char *name, bool read_only)
308 {
309 bool is_file = strncmp(name, "/dev/", 5) != 0;
310 #if defined(__FreeBSD__)
311 // SCSI IDE
312 bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0 || strncmp(name, "/dev/acd", 8) == 0;
313 #else
314 bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0;
315 #endif
316
317 #if defined(__APPLE__) && defined(__MACH__)
318 //
319 // There is no set filename in /dev which is the cdrom,
320 // so we have to see if it is any of the devices that we found earlier
321 //
322 const char *cdrom;
323 int tmp = 0;
324
325 while ( (cdrom = PrefsFindString("cdrom", tmp) ) != NULL )
326 {
327 if ( strcmp(name, cdrom) == 0 )
328 {
329 is_cdrom = 1;
330 read_only = 1;
331 break;
332 }
333 ++tmp;
334 }
335 #endif
336
337 D(bug("Sys_open(%s, %s)\n", name, read_only ? "read-only" : "read/write"));
338
339 // Check if write access is allowed, set read-only flag if not
340 if (!read_only && access(name, W_OK))
341 read_only = true;
342
343 // Print warning message and eventually unmount drive when this is an HFS volume mounted under Linux (double mounting will corrupt the volume)
344 char mount_name[256];
345 if (!is_file && !read_only && is_drive_mounted(name, mount_name)) {
346 char str[512];
347 sprintf(str, GetString(STR_VOLUME_IS_MOUNTED_WARN), mount_name);
348 WarningAlert(str);
349 sprintf(str, "umount %s", mount_name);
350 if (system(str)) {
351 sprintf(str, GetString(STR_CANNOT_UNMOUNT_WARN), mount_name, strerror(errno));
352 WarningAlert(str);
353 return NULL;
354 }
355 }
356
357 // Open file/device
358 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
359 int fd = open(name, (read_only ? O_RDONLY : O_RDWR) | (is_cdrom ? O_NONBLOCK : 0));
360 #else
361 int fd = open(name, read_only ? O_RDONLY : O_RDWR);
362 #endif
363 if (fd < 0 && !read_only) {
364 // Read-write failed, try read-only
365 read_only = true;
366 fd = open(name, O_RDONLY);
367 }
368 if (fd >= 0) {
369 file_handle *fh = new file_handle;
370 fh->name = strdup(name);
371 fh->fd = fd;
372 fh->is_file = is_file;
373 fh->read_only = read_only;
374 fh->start_byte = 0;
375 fh->is_floppy = false;
376 fh->is_cdrom = false;
377 if (fh->is_file) {
378 // Detect disk image file layout
379 loff_t size = 0;
380 #if defined(__linux__)
381 _llseek(fh->fd, 0, 0, &size, SEEK_END);
382 #else
383 size = lseek(fd, 0, SEEK_END);
384 #endif
385 uint8 data[256];
386 lseek(fd, 0, SEEK_SET);
387 read(fd, data, 256);
388 FileDiskLayout(size, data, fh->start_byte, fh->file_size);
389 } else {
390 struct stat st;
391 if (fstat(fd, &st) == 0) {
392 if (S_ISBLK(st.st_mode)) {
393 fh->is_cdrom = is_cdrom;
394 #if defined(__linux__)
395 fh->is_floppy = (MAJOR(st.st_rdev) == FLOPPY_MAJOR);
396 #ifdef CDROM_GET_CAPABILITY
397 if (is_cdrom) {
398 fh->cdrom_cap = ioctl(fh->fd, CDROM_GET_CAPABILITY);
399 if (fh->cdrom_cap < 0)
400 fh->cdrom_cap = 0;
401 }
402 #else
403 fh->cdrom_cap = 0;
404 #endif
405 #elif defined(__FreeBSD__)
406 fh->is_floppy = ((st.st_rdev >> 16) == 2);
407 #ifdef CDIOCCAPABILITY
408 if (is_cdrom) {
409 if (ioctl(fh->fd, CDIOCCAPABILITY, &fh->cdrom_cap) < 0)
410 memset(&fh->cdrom_cap, 0, sizeof(fh->cdrom_cap));
411 }
412 #else
413 fh->cdrom_cap = 0;
414 #endif
415 #elif defined(__NetBSD__)
416 fh->is_floppy = ((st.st_rdev >> 16) == 2);
417 #endif
418 }
419 #if defined(__APPLE__) && defined(__MACH__)
420
421 // In OS X, the device name is OK for sending ioctls to,
422 // but not for reading raw CDROM data from.
423 // (it seems to have extra data padded in)
424 //
425 // So, we keep the already opened fiole handle,
426 // and open a slightly different file for CDROM data
427 //
428 if ( is_cdrom )
429 {
430 fh->ioctl_name = fh->name;
431 fh->ioctl_fd = fh->fd;
432
433 fh->name = (char *) malloc(strlen(name) + 2);
434 if ( fh->name )
435 {
436 *fh->name = '\0';
437 strcat(fh->name, name);
438 strcat(fh->name, "s1");
439 fh->fd = open(fh->name, (read_only ? O_RDONLY : O_RDWR));
440 if ( fh->fd < 0 ) {
441 printf("WARNING: Cannot open %s (%s)\n",
442 fh->name, strerror(errno));
443 return NULL;
444 }
445 }
446 }
447 #endif
448 }
449 }
450 if (fh->is_floppy && first_floppy == NULL)
451 first_floppy = fh;
452 return fh;
453 } else {
454 printf("WARNING: Cannot open %s (%s)\n", name, strerror(errno));
455 return NULL;
456 }
457 }
458
459
460 /*
461 * Close file/device, delete file handle
462 */
463
464 void Sys_close(void *arg)
465 {
466 file_handle *fh = (file_handle *)arg;
467 if (!fh)
468 return;
469
470 close(fh->fd);
471 if (fh->name)
472 free(fh->name);
473 delete fh;
474 }
475
476
477 /*
478 * Read "length" bytes from file/device, starting at "offset", to "buffer",
479 * returns number of bytes read (or 0)
480 */
481
482 size_t Sys_read(void *arg, void *buffer, loff_t offset, size_t length)
483 {
484 file_handle *fh = (file_handle *)arg;
485 if (!fh)
486 return 0;
487
488 // Seek to position
489 #if defined(__linux__)
490 loff_t pos = offset + fh->start_byte, res;
491 if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0)
492 return 0;
493 #else
494 if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0)
495 return 0;
496 #endif
497
498 // Read data
499 return read(fh->fd, buffer, length);
500 }
501
502
503 /*
504 * Write "length" bytes from "buffer" to file/device, starting at "offset",
505 * returns number of bytes written (or 0)
506 */
507
508 size_t Sys_write(void *arg, void *buffer, loff_t offset, size_t length)
509 {
510 file_handle *fh = (file_handle *)arg;
511 if (!fh)
512 return 0;
513
514 // Seek to position
515 #if defined(__linux__)
516 loff_t pos = offset + fh->start_byte, res;
517 if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0)
518 return 0;
519 #else
520 if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0)
521 return 0;
522 #endif
523
524 // Write data
525 return write(fh->fd, buffer, length);
526 }
527
528
529 /*
530 * Return size of file/device (minus header)
531 */
532
533 loff_t SysGetFileSize(void *arg)
534 {
535 file_handle *fh = (file_handle *)arg;
536 if (!fh)
537 return true;
538
539 if (fh->is_file)
540 return fh->file_size;
541 else {
542 #if defined(__linux__)
543 long blocks;
544 if (ioctl(fh->fd, BLKGETSIZE, &blocks) < 0)
545 return 0;
546 D(bug(" BLKGETSIZE returns %d blocks\n", blocks));
547 return (loff_t)blocks * 512;
548 #else
549 return lseek(fh->fd, 0, SEEK_END) - fh->start_byte;
550 #endif
551 }
552 }
553
554
555 /*
556 * Eject volume (if applicable)
557 */
558
559 void SysEject(void *arg)
560 {
561 file_handle *fh = (file_handle *)arg;
562 if (!fh)
563 return;
564
565 #if defined(__linux__)
566 if (fh->is_floppy) {
567 fsync(fh->fd);
568 ioctl(fh->fd, FDFLUSH);
569 ioctl(fh->fd, FDEJECT);
570 close(fh->fd); // Close and reopen so the driver will see the media change
571 fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
572 } else if (fh->is_cdrom) {
573 ioctl(fh->fd, CDROMEJECT);
574 close(fh->fd); // Close and reopen so the driver will see the media change
575 fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
576 }
577 #elif defined(__FreeBSD__) || defined(__NetBSD__)
578 if (fh->is_floppy) {
579 fsync(fh->fd);
580 } else if (fh->is_cdrom) {
581 ioctl(fh->fd, CDIOCEJECT);
582 close(fh->fd); // Close and reopen so the driver will see the media change
583 fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
584 }
585 #elif defined(__APPLE__) && defined(__MACH__)
586 if ( fh->is_cdrom ) {
587
588 // Stolen from IOKit/storage/IOMediaBSDClient.h
589 #define DKIOCEJECT _IO('d', 21)
590
591 close(fh->fd);
592 if ( ioctl(fh->ioctl_fd, DKIOCEJECT) < 0 )
593 {
594 printf("ioctl(DKIOCEJECT) failed on file %s: %s\n",
595 fh->ioctl_name, strerror(errno));
596
597 // If we are running OSX, the device may be is busy
598 // due to the Finder having the disk mounted and open,
599 // so we have to use another method.
600
601 // The only problem is that this takes about 5 seconds:
602
603 char *cmd = (char *) malloc(30+sizeof(fh->name));
604
605 if ( ! cmd )
606 return;
607 close(fh->ioctl_fd);
608 sprintf(cmd, "diskutil eject %s 2>&1 >/dev/null", fh->name);
609 system(cmd);
610 }
611 }
612 #endif
613 }
614
615
616 /*
617 * Format volume (if applicable)
618 */
619
620 bool SysFormat(void *arg)
621 {
622 file_handle *fh = (file_handle *)arg;
623 if (!fh)
624 return false;
625
626 //!!
627 return true;
628 }
629
630
631 /*
632 * Check if file/device is read-only (this includes the read-only flag on Sys_open())
633 */
634
635 bool SysIsReadOnly(void *arg)
636 {
637 file_handle *fh = (file_handle *)arg;
638 if (!fh)
639 return true;
640
641 #if defined(__linux__)
642 if (fh->is_floppy) {
643 struct floppy_drive_struct stat;
644 ioctl(fh->fd, FDGETDRVSTAT, &stat);
645 return !(stat.flags & FD_DISK_WRITABLE);
646 } else
647 #endif
648 return fh->read_only;
649 }
650
651
652 /*
653 * Check if the given file handle refers to a fixed or a removable disk
654 */
655
656 bool SysIsFixedDisk(void *arg)
657 {
658 file_handle *fh = (file_handle *)arg;
659 if (!fh)
660 return true;
661
662 if (fh->is_file)
663 return true;
664 else if (fh->is_floppy || fh->is_cdrom)
665 return false;
666 else
667 return true;
668 }
669
670
671 /*
672 * Check if a disk is inserted in the drive (always true for files)
673 */
674
675 bool SysIsDiskInserted(void *arg)
676 {
677 file_handle *fh = (file_handle *)arg;
678 if (!fh)
679 return false;
680
681 if (fh->is_file) {
682 return true;
683
684 #if defined(__linux__)
685 } else if (fh->is_floppy) {
686 char block[512];
687 lseek(fh->fd, 0, SEEK_SET);
688 ssize_t actual = read(fh->fd, block, 512);
689 if (actual < 0) {
690 close(fh->fd); // Close and reopen so the driver will see the media change
691 fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
692 actual = read(fh->fd, block, 512);
693 }
694 return actual == 512;
695 } else if (fh->is_cdrom) {
696 #ifdef CDROM_MEDIA_CHANGED
697 if (fh->cdrom_cap & CDC_MEDIA_CHANGED) {
698 // If we don't do this, all attempts to read from a disc fail
699 // once the tray has been opened (altough the TOC reads fine).
700 // Can somebody explain this to me?
701 if (ioctl(fh->fd, CDROM_MEDIA_CHANGED) == 1) {
702 close(fh->fd);
703 fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
704 }
705 }
706 #endif
707 #ifdef CDROM_DRIVE_STATUS
708 if (fh->cdrom_cap & CDC_DRIVE_STATUS) {
709 return ioctl(fh->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK;
710 }
711 #endif
712 cdrom_tochdr header;
713 return ioctl(fh->fd, CDROMREADTOCHDR, &header) == 0;
714 #elif defined(__FreeBSD__) || defined(__NetBSD__)
715 } else if (fh->is_floppy) {
716 return false; //!!
717 } else if (fh->is_cdrom) {
718 struct ioc_toc_header header;
719 return ioctl(fh->fd, CDIOREADTOCHEADER, &header) == 0;
720 #endif
721
722 } else
723 return true;
724 }
725
726
727 /*
728 * Prevent medium removal (if applicable)
729 */
730
731 void SysPreventRemoval(void *arg)
732 {
733 file_handle *fh = (file_handle *)arg;
734 if (!fh)
735 return;
736
737 #if defined(__linux__) && defined(CDROM_LOCKDOOR)
738 if (fh->is_cdrom)
739 ioctl(fh->fd, CDROM_LOCKDOOR, 1);
740 #endif
741 }
742
743
744 /*
745 * Allow medium removal (if applicable)
746 */
747
748 void SysAllowRemoval(void *arg)
749 {
750 file_handle *fh = (file_handle *)arg;
751 if (!fh)
752 return;
753
754 #if defined(__linux__) && defined(CDROM_LOCKDOOR)
755 if (fh->is_cdrom)
756 ioctl(fh->fd, CDROM_LOCKDOOR, 0);
757 #endif
758 }
759
760
761 /*
762 * Read CD-ROM TOC (binary MSF format, 804 bytes max.)
763 */
764
765 bool SysCDReadTOC(void *arg, uint8 *toc)
766 {
767 file_handle *fh = (file_handle *)arg;
768 if (!fh)
769 return false;
770
771 if (fh->is_cdrom) {
772 #if defined(__linux__)
773 uint8 *p = toc + 2;
774
775 // Header
776 cdrom_tochdr header;
777 if (ioctl(fh->fd, CDROMREADTOCHDR, &header) < 0)
778 return false;
779 *p++ = header.cdth_trk0;
780 *p++ = header.cdth_trk1;
781
782 // Tracks
783 cdrom_tocentry entry;
784 for (int i=header.cdth_trk0; i<=header.cdth_trk1; i++) {
785 entry.cdte_track = i;
786 entry.cdte_format = CDROM_MSF;
787 if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0)
788 return false;
789 *p++ = 0;
790 *p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl;
791 *p++ = entry.cdte_track;
792 *p++ = 0;
793 *p++ = 0;
794 *p++ = entry.cdte_addr.msf.minute;
795 *p++ = entry.cdte_addr.msf.second;
796 *p++ = entry.cdte_addr.msf.frame;
797 }
798
799 // Leadout track
800 entry.cdte_track = CDROM_LEADOUT;
801 entry.cdte_format = CDROM_MSF;
802 if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0)
803 return false;
804 *p++ = 0;
805 *p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl;
806 *p++ = entry.cdte_track;
807 *p++ = 0;
808 *p++ = 0;
809 *p++ = entry.cdte_addr.msf.minute;
810 *p++ = entry.cdte_addr.msf.second;
811 *p++ = entry.cdte_addr.msf.frame;
812
813 // TOC size
814 int toc_size = p - toc;
815 *toc++ = toc_size >> 8;
816 *toc++ = toc_size & 0xff;
817 return true;
818 #elif defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_2)
819 extern bool DarwinCDReadTOC(char *name, uint8 *toc);
820
821 return DarwinCDReadTOC(fh->name, toc);
822 #elif defined(__FreeBSD__)
823 uint8 *p = toc + 2;
824
825 // Header
826 struct ioc_toc_header header;
827 if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
828 return false;
829 *p++ = header.starting_track;
830 *p++ = header.ending_track;
831
832 // Tracks
833 struct ioc_read_toc_single_entry entry;
834 for (int i=header.starting_track; i<=header.ending_track; i++) {
835 entry.track = i;
836 entry.address_format = CD_MSF_FORMAT;
837 if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0)
838 return false;
839 *p++ = 0;
840 *p++ = (entry.entry.addr_type << 4) | entry.entry.control;
841 *p++ = entry.entry.track;
842 *p++ = 0;
843 *p++ = 0;
844 *p++ = entry.entry.addr.msf.minute;
845 *p++ = entry.entry.addr.msf.second;
846 *p++ = entry.entry.addr.msf.frame;
847 }
848
849 // Leadout track
850 entry.track = CD_TRACK_INFO;
851 entry.address_format = CD_MSF_FORMAT;
852 if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0)
853 return false;
854 *p++ = 0;
855 *p++ = (entry.entry.addr_type << 4) | entry.entry.control;
856 *p++ = entry.entry.track;
857 *p++ = 0;
858 *p++ = 0;
859 *p++ = entry.entry.addr.msf.minute;
860 *p++ = entry.entry.addr.msf.second;
861 *p++ = entry.entry.addr.msf.frame;
862
863 // TOC size
864 int toc_size = p - toc;
865 *toc++ = toc_size >> 8;
866 *toc++ = toc_size & 0xff;
867 return true;
868 #elif defined(__NetBSD__)
869 uint8 *p = toc + 2;
870
871 // Header
872 struct ioc_toc_header header;
873 if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
874 return false;
875 *p++ = header.starting_track;
876 *p++ = header.ending_track;
877
878 // Tracks (this is nice... :-)
879 struct ioc_read_toc_entry entries;
880 entries.address_format = CD_MSF_FORMAT;
881 entries.starting_track = 1;
882 entries.data_len = 800;
883 entries.data = (cd_toc_entry *)p;
884 if (ioctl(fh->fd, CDIOREADTOCENTRIES, &entries) < 0)
885 return false;
886
887 // TOC size
888 int toc_size = p - toc;
889 *toc++ = toc_size >> 8;
890 *toc++ = toc_size & 0xff;
891 return true;
892 #else
893 return false;
894 #endif
895 } else
896 return false;
897 }
898
899
900 /*
901 * Read CD-ROM position data (Sub-Q Channel, 16 bytes, see SCSI standard)
902 */
903
904 bool SysCDGetPosition(void *arg, uint8 *pos)
905 {
906 file_handle *fh = (file_handle *)arg;
907 if (!fh)
908 return false;
909
910 if (fh->is_cdrom) {
911 #if defined(__linux__)
912 cdrom_subchnl chan;
913 chan.cdsc_format = CDROM_MSF;
914 if (ioctl(fh->fd, CDROMSUBCHNL, &chan) < 0)
915 return false;
916 *pos++ = 0;
917 *pos++ = chan.cdsc_audiostatus;
918 *pos++ = 0;
919 *pos++ = 12; // Sub-Q data length
920 *pos++ = 0;
921 *pos++ = (chan.cdsc_adr << 4) | chan.cdsc_ctrl;
922 *pos++ = chan.cdsc_trk;
923 *pos++ = chan.cdsc_ind;
924 *pos++ = 0;
925 *pos++ = chan.cdsc_absaddr.msf.minute;
926 *pos++ = chan.cdsc_absaddr.msf.second;
927 *pos++ = chan.cdsc_absaddr.msf.frame;
928 *pos++ = 0;
929 *pos++ = chan.cdsc_reladdr.msf.minute;
930 *pos++ = chan.cdsc_reladdr.msf.second;
931 *pos++ = chan.cdsc_reladdr.msf.frame;
932 return true;
933 #elif defined(__FreeBSD__) || defined(__NetBSD__)
934 struct ioc_read_subchannel chan;
935 chan.data_format = CD_MSF_FORMAT;
936 chan.address_format = CD_MSF_FORMAT;
937 chan.track = CD_CURRENT_POSITION;
938 if (ioctl(fh->fd, CDIOCREADSUBCHANNEL, &chan) < 0)
939 return false;
940 *pos++ = 0;
941 *pos++ = chan.data->header.audio_status;
942 *pos++ = 0;
943 *pos++ = 12; // Sub-Q data length
944 *pos++ = 0;
945 *pos++ = (chan.data->what.position.addr_type << 4) | chan.data->what.position.control;
946 *pos++ = chan.data->what.position.track_number;
947 *pos++ = chan.data->what.position.index_number;
948 *pos++ = 0;
949 *pos++ = chan.data->what.position.absaddr.msf.minute;
950 *pos++ = chan.data->what.position.absaddr.msf.second;
951 *pos++ = chan.data->what.position.absaddr.msf.frame;
952 *pos++ = 0;
953 *pos++ = chan.data->what.position.reladdr.msf.minute;
954 *pos++ = chan.data->what.position.reladdr.msf.second;
955 *pos++ = chan.data->what.position.reladdr.msf.frame;
956 return true;
957 #else
958 return false;
959 #endif
960 } else
961 return false;
962 }
963
964
965 /*
966 * Play CD audio
967 */
968
969 bool SysCDPlay(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, uint8 end_m, uint8 end_s, uint8 end_f)
970 {
971 file_handle *fh = (file_handle *)arg;
972 if (!fh)
973 return false;
974
975 if (fh->is_cdrom) {
976 #if defined(__linux__)
977 cdrom_msf play;
978 play.cdmsf_min0 = start_m;
979 play.cdmsf_sec0 = start_s;
980 play.cdmsf_frame0 = start_f;
981 play.cdmsf_min1 = end_m;
982 play.cdmsf_sec1 = end_s;
983 play.cdmsf_frame1 = end_f;
984 return ioctl(fh->fd, CDROMPLAYMSF, &play) == 0;
985 #elif defined(__FreeBSD__) || defined(__NetBSD__)
986 struct ioc_play_msf play;
987 play.start_m = start_m;
988 play.start_s = start_s;
989 play.start_f = start_f;
990 play.end_m = end_m;
991 play.end_s = end_s;
992 play.end_f = end_f;
993 return ioctl(fh->fd, CDIOCPLAYMSF, &play) == 0;
994 #else
995 return false;
996 #endif
997 } else
998 return false;
999 }
1000
1001
1002 /*
1003 * Pause CD audio
1004 */
1005
1006 bool SysCDPause(void *arg)
1007 {
1008 file_handle *fh = (file_handle *)arg;
1009 if (!fh)
1010 return false;
1011
1012 if (fh->is_cdrom) {
1013 #if defined(__linux__)
1014 return ioctl(fh->fd, CDROMPAUSE) == 0;
1015 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1016 return ioctl(fh->fd, CDIOCPAUSE) == 0;
1017 #else
1018 return false;
1019 #endif
1020 } else
1021 return false;
1022 }
1023
1024
1025 /*
1026 * Resume paused CD audio
1027 */
1028
1029 bool SysCDResume(void *arg)
1030 {
1031 file_handle *fh = (file_handle *)arg;
1032 if (!fh)
1033 return false;
1034
1035 if (fh->is_cdrom) {
1036 #if defined(__linux__)
1037 return ioctl(fh->fd, CDROMRESUME) == 0;
1038 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1039 return ioctl(fh->fd, CDIOCRESUME) == 0;
1040 #else
1041 return false;
1042 #endif
1043 } else
1044 return false;
1045 }
1046
1047
1048 /*
1049 * Stop CD audio
1050 */
1051
1052 bool SysCDStop(void *arg, uint8 lead_out_m, uint8 lead_out_s, uint8 lead_out_f)
1053 {
1054 file_handle *fh = (file_handle *)arg;
1055 if (!fh)
1056 return false;
1057
1058 if (fh->is_cdrom) {
1059 #if defined(__linux__)
1060 return ioctl(fh->fd, CDROMSTOP) == 0;
1061 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1062 return ioctl(fh->fd, CDIOCSTOP) == 0;
1063 #else
1064 return false;
1065 #endif
1066 } else
1067 return false;
1068 }
1069
1070
1071 /*
1072 * Perform CD audio fast-forward/fast-reverse operation starting from specified address
1073 */
1074
1075 bool SysCDScan(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, bool reverse)
1076 {
1077 file_handle *fh = (file_handle *)arg;
1078 if (!fh)
1079 return false;
1080
1081 // Not supported under Linux
1082 return false;
1083 }
1084
1085
1086 /*
1087 * Set CD audio volume (0..255 each channel)
1088 */
1089
1090 void SysCDSetVolume(void *arg, uint8 left, uint8 right)
1091 {
1092 file_handle *fh = (file_handle *)arg;
1093 if (!fh)
1094 return;
1095
1096 if (fh->is_cdrom) {
1097 #if defined(__linux__)
1098 cdrom_volctrl vol;
1099 vol.channel0 = vol.channel2 = left;
1100 vol.channel1 = vol.channel3 = right;
1101 ioctl(fh->fd, CDROMVOLCTRL, &vol);
1102 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1103 struct ioc_vol vol;
1104 vol.vol[0] = vol.vol[2] = left;
1105 vol.vol[1] = vol.vol[3] = right;
1106 ioctl(fh->fd, CDIOCSETVOL, &vol);
1107 #endif
1108 }
1109 }
1110
1111
1112 /*
1113 * Get CD audio volume (0..255 each channel)
1114 */
1115
1116 void SysCDGetVolume(void *arg, uint8 &left, uint8 &right)
1117 {
1118 file_handle *fh = (file_handle *)arg;
1119 if (!fh)
1120 return;
1121
1122 left = right = 0;
1123 if (fh->is_cdrom) {
1124 #if defined(__linux__)
1125 cdrom_volctrl vol;
1126 ioctl(fh->fd, CDROMVOLREAD, &vol);
1127 left = vol.channel0;
1128 right = vol.channel1;
1129 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1130 struct ioc_vol vol;
1131 ioctl(fh->fd, CDIOCGETVOL, &vol);
1132 left = vol.vol[0];
1133 right = vol.vol[1];
1134 #endif
1135 }
1136 }