ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sys_unix.cpp
Revision: 1.28
Committed: 2005-11-24T17:23:43Z (18 years, 11 months ago) by cebix
Branch: MAIN
CVS Tags: nigel-build-17
Changes since 1.27: +19 -12 lines
Log Message:
ignore floppy device open fail in Sys_open() so the floppy drive will be
operative even if no disk was inserted at startup

File Contents

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