ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sys_unix.cpp
Revision: 1.27
Committed: 2005-11-24T17:02:59Z (18 years, 11 months ago) by cebix
Branch: MAIN
Changes since 1.26: +12 -14 lines
Log Message:
modernized Linux floppy detection

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     char *name; // Copy of device/file name
57     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    
312 nigel 1.19 #if defined(__APPLE__) && defined(__MACH__)
313     //
314     // There is no set filename in /dev which is the cdrom,
315     // so we have to see if it is any of the devices that we found earlier
316     //
317     const char *cdrom;
318     int tmp = 0;
319    
320     while ( (cdrom = PrefsFindString("cdrom", tmp) ) != NULL )
321     {
322     if ( strcmp(name, cdrom) == 0 )
323     {
324     is_cdrom = 1;
325     read_only = 1;
326     break;
327     }
328     ++tmp;
329     }
330     #endif
331    
332 cebix 1.1 D(bug("Sys_open(%s, %s)\n", name, read_only ? "read-only" : "read/write"));
333    
334     // Check if write access is allowed, set read-only flag if not
335     if (!read_only && access(name, W_OK))
336     read_only = true;
337    
338     // Print warning message and eventually unmount drive when this is an HFS volume mounted under Linux (double mounting will corrupt the volume)
339     char mount_name[256];
340     if (!is_file && !read_only && is_drive_mounted(name, mount_name)) {
341     char str[512];
342     sprintf(str, GetString(STR_VOLUME_IS_MOUNTED_WARN), mount_name);
343     WarningAlert(str);
344     sprintf(str, "umount %s", mount_name);
345     if (system(str)) {
346     sprintf(str, GetString(STR_CANNOT_UNMOUNT_WARN), mount_name, strerror(errno));
347     WarningAlert(str);
348     return NULL;
349     }
350     }
351    
352     // Open file/device
353 cebix 1.8 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
354 cebix 1.1 int fd = open(name, (read_only ? O_RDONLY : O_RDWR) | (is_cdrom ? O_NONBLOCK : 0));
355     #else
356     int fd = open(name, read_only ? O_RDONLY : O_RDWR);
357     #endif
358     if (fd < 0 && !read_only) {
359     // Read-write failed, try read-only
360     read_only = true;
361     fd = open(name, O_RDONLY);
362     }
363     if (fd >= 0) {
364     file_handle *fh = new file_handle;
365     fh->name = strdup(name);
366     fh->fd = fd;
367     fh->is_file = is_file;
368     fh->read_only = read_only;
369     fh->start_byte = 0;
370     fh->is_floppy = false;
371     fh->is_cdrom = false;
372     if (fh->is_file) {
373     // Detect disk image file layout
374     loff_t size = 0;
375     size = lseek(fd, 0, SEEK_END);
376     uint8 data[256];
377     lseek(fd, 0, SEEK_SET);
378     read(fd, data, 256);
379     FileDiskLayout(size, data, fh->start_byte, fh->file_size);
380     } else {
381     struct stat st;
382     if (fstat(fd, &st) == 0) {
383     if (S_ISBLK(st.st_mode)) {
384     fh->is_cdrom = is_cdrom;
385     #if defined(__linux__)
386     fh->is_floppy = (MAJOR(st.st_rdev) == FLOPPY_MAJOR);
387     #ifdef CDROM_GET_CAPABILITY
388     if (is_cdrom) {
389     fh->cdrom_cap = ioctl(fh->fd, CDROM_GET_CAPABILITY);
390     if (fh->cdrom_cap < 0)
391     fh->cdrom_cap = 0;
392     }
393     #else
394     fh->cdrom_cap = 0;
395     #endif
396 cebix 1.4 #elif defined(__FreeBSD__)
397 cebix 1.1 fh->is_floppy = ((st.st_rdev >> 16) == 2);
398     #ifdef CDIOCCAPABILITY
399     if (is_cdrom) {
400     if (ioctl(fh->fd, CDIOCCAPABILITY, &fh->cdrom_cap) < 0)
401     memset(&fh->cdrom_cap, 0, sizeof(fh->cdrom_cap));
402     }
403     #else
404     fh->cdrom_cap = 0;
405     #endif
406 cebix 1.4 #elif defined(__NetBSD__)
407     fh->is_floppy = ((st.st_rdev >> 16) == 2);
408 cebix 1.1 #endif
409     }
410 nigel 1.19 #if defined(__APPLE__) && defined(__MACH__)
411    
412     // In OS X, the device name is OK for sending ioctls to,
413     // but not for reading raw CDROM data from.
414     // (it seems to have extra data padded in)
415     //
416     // So, we keep the already opened fiole handle,
417     // and open a slightly different file for CDROM data
418     //
419     if ( is_cdrom )
420     {
421     fh->ioctl_name = fh->name;
422     fh->ioctl_fd = fh->fd;
423    
424     fh->name = (char *) malloc(strlen(name) + 2);
425     if ( fh->name )
426     {
427     *fh->name = '\0';
428     strcat(fh->name, name);
429     strcat(fh->name, "s1");
430     fh->fd = open(fh->name, (read_only ? O_RDONLY : O_RDWR));
431     if ( fh->fd < 0 ) {
432     printf("WARNING: Cannot open %s (%s)\n",
433     fh->name, strerror(errno));
434     return NULL;
435     }
436     }
437     }
438     #endif
439 cebix 1.1 }
440     }
441     if (fh->is_floppy && first_floppy == NULL)
442     first_floppy = fh;
443     return fh;
444     } else {
445     printf("WARNING: Cannot open %s (%s)\n", name, strerror(errno));
446     return NULL;
447     }
448     }
449    
450    
451     /*
452     * Close file/device, delete file handle
453     */
454    
455     void Sys_close(void *arg)
456     {
457     file_handle *fh = (file_handle *)arg;
458     if (!fh)
459     return;
460    
461     close(fh->fd);
462     if (fh->name)
463     free(fh->name);
464     delete fh;
465     }
466    
467    
468     /*
469     * Read "length" bytes from file/device, starting at "offset", to "buffer",
470     * returns number of bytes read (or 0)
471     */
472    
473     size_t Sys_read(void *arg, void *buffer, loff_t offset, size_t length)
474     {
475     file_handle *fh = (file_handle *)arg;
476     if (!fh)
477     return 0;
478    
479     // Seek to position
480     if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0)
481     return 0;
482    
483     // Read data
484     return read(fh->fd, buffer, length);
485     }
486    
487    
488     /*
489     * Write "length" bytes from "buffer" to file/device, starting at "offset",
490     * returns number of bytes written (or 0)
491     */
492    
493     size_t Sys_write(void *arg, void *buffer, loff_t offset, size_t length)
494     {
495     file_handle *fh = (file_handle *)arg;
496     if (!fh)
497     return 0;
498    
499     // Seek to position
500     if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0)
501     return 0;
502    
503     // Write data
504     return write(fh->fd, buffer, length);
505     }
506    
507    
508     /*
509     * Return size of file/device (minus header)
510     */
511    
512     loff_t SysGetFileSize(void *arg)
513     {
514     file_handle *fh = (file_handle *)arg;
515     if (!fh)
516     return true;
517    
518     if (fh->is_file)
519     return fh->file_size;
520     else {
521 cebix 1.3 #if defined(__linux__)
522 cebix 1.5 long blocks;
523     if (ioctl(fh->fd, BLKGETSIZE, &blocks) < 0)
524     return 0;
525     D(bug(" BLKGETSIZE returns %d blocks\n", blocks));
526     return (loff_t)blocks * 512;
527 cebix 1.1 #else
528     return lseek(fh->fd, 0, SEEK_END) - fh->start_byte;
529     #endif
530     }
531     }
532    
533    
534     /*
535     * Eject volume (if applicable)
536     */
537    
538     void SysEject(void *arg)
539     {
540     file_handle *fh = (file_handle *)arg;
541     if (!fh)
542     return;
543    
544     #if defined(__linux__)
545     if (fh->is_floppy) {
546     fsync(fh->fd);
547     ioctl(fh->fd, FDFLUSH);
548     ioctl(fh->fd, FDEJECT);
549 cebix 1.14 close(fh->fd); // Close and reopen so the driver will see the media change
550     fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
551 cebix 1.1 } else if (fh->is_cdrom) {
552     ioctl(fh->fd, CDROMEJECT);
553     close(fh->fd); // Close and reopen so the driver will see the media change
554     fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
555     }
556 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
557 cebix 1.1 if (fh->is_floppy) {
558     fsync(fh->fd);
559     } else if (fh->is_cdrom) {
560     ioctl(fh->fd, CDIOCEJECT);
561     close(fh->fd); // Close and reopen so the driver will see the media change
562     fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
563     }
564 nigel 1.19 #elif defined(__APPLE__) && defined(__MACH__)
565     if ( fh->is_cdrom ) {
566    
567     // Stolen from IOKit/storage/IOMediaBSDClient.h
568     #define DKIOCEJECT _IO('d', 21)
569    
570     close(fh->fd);
571     if ( ioctl(fh->ioctl_fd, DKIOCEJECT) < 0 )
572     {
573     printf("ioctl(DKIOCEJECT) failed on file %s: %s\n",
574     fh->ioctl_name, strerror(errno));
575    
576     // If we are running OSX, the device may be is busy
577     // due to the Finder having the disk mounted and open,
578     // so we have to use another method.
579    
580     // The only problem is that this takes about 5 seconds:
581    
582     char *cmd = (char *) malloc(30+sizeof(fh->name));
583    
584     if ( ! cmd )
585     return;
586     close(fh->ioctl_fd);
587     sprintf(cmd, "diskutil eject %s 2>&1 >/dev/null", fh->name);
588     system(cmd);
589     }
590     }
591 cebix 1.1 #endif
592     }
593    
594    
595     /*
596     * Format volume (if applicable)
597     */
598    
599     bool SysFormat(void *arg)
600     {
601     file_handle *fh = (file_handle *)arg;
602     if (!fh)
603     return false;
604    
605     //!!
606     return true;
607     }
608    
609    
610     /*
611     * Check if file/device is read-only (this includes the read-only flag on Sys_open())
612     */
613    
614     bool SysIsReadOnly(void *arg)
615     {
616     file_handle *fh = (file_handle *)arg;
617     if (!fh)
618     return true;
619    
620 cebix 1.3 #if defined(__linux__)
621 cebix 1.1 if (fh->is_floppy) {
622     struct floppy_drive_struct stat;
623     ioctl(fh->fd, FDGETDRVSTAT, &stat);
624     return !(stat.flags & FD_DISK_WRITABLE);
625     } else
626     #endif
627     return fh->read_only;
628     }
629    
630    
631     /*
632     * Check if the given file handle refers to a fixed or a removable disk
633     */
634    
635     bool SysIsFixedDisk(void *arg)
636     {
637     file_handle *fh = (file_handle *)arg;
638     if (!fh)
639     return true;
640    
641     if (fh->is_file)
642     return true;
643     else if (fh->is_floppy || fh->is_cdrom)
644     return false;
645     else
646     return true;
647     }
648    
649    
650     /*
651     * Check if a disk is inserted in the drive (always true for files)
652     */
653    
654     bool SysIsDiskInserted(void *arg)
655     {
656     file_handle *fh = (file_handle *)arg;
657     if (!fh)
658     return false;
659    
660     if (fh->is_file) {
661     return true;
662    
663     #if defined(__linux__)
664     } else if (fh->is_floppy) {
665     char block[512];
666     lseek(fh->fd, 0, SEEK_SET);
667 cebix 1.14 ssize_t actual = read(fh->fd, block, 512);
668     if (actual < 0) {
669     close(fh->fd); // Close and reopen so the driver will see the media change
670     fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
671     actual = read(fh->fd, block, 512);
672     }
673     return actual == 512;
674 cebix 1.1 } else if (fh->is_cdrom) {
675 cebix 1.8 #ifdef CDROM_MEDIA_CHANGED
676     if (fh->cdrom_cap & CDC_MEDIA_CHANGED) {
677     // If we don't do this, all attempts to read from a disc fail
678 cebix 1.14 // once the tray has been opened (altough the TOC reads fine).
679 cebix 1.8 // Can somebody explain this to me?
680     if (ioctl(fh->fd, CDROM_MEDIA_CHANGED) == 1) {
681     close(fh->fd);
682     fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
683     }
684     }
685     #endif
686 cebix 1.1 #ifdef CDROM_DRIVE_STATUS
687     if (fh->cdrom_cap & CDC_DRIVE_STATUS) {
688     return ioctl(fh->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK;
689     }
690     #endif
691     cdrom_tochdr header;
692     return ioctl(fh->fd, CDROMREADTOCHDR, &header) == 0;
693 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
694 cebix 1.1 } else if (fh->is_floppy) {
695     return false; //!!
696     } else if (fh->is_cdrom) {
697     struct ioc_toc_header header;
698     return ioctl(fh->fd, CDIOREADTOCHEADER, &header) == 0;
699     #endif
700    
701     } else
702     return true;
703     }
704    
705    
706     /*
707     * Prevent medium removal (if applicable)
708     */
709    
710     void SysPreventRemoval(void *arg)
711     {
712     file_handle *fh = (file_handle *)arg;
713     if (!fh)
714     return;
715    
716     #if defined(__linux__) && defined(CDROM_LOCKDOOR)
717     if (fh->is_cdrom)
718     ioctl(fh->fd, CDROM_LOCKDOOR, 1);
719     #endif
720     }
721    
722    
723     /*
724     * Allow medium removal (if applicable)
725     */
726    
727     void SysAllowRemoval(void *arg)
728     {
729     file_handle *fh = (file_handle *)arg;
730     if (!fh)
731     return;
732    
733 cebix 1.2 #if defined(__linux__) && defined(CDROM_LOCKDOOR)
734 cebix 1.1 if (fh->is_cdrom)
735     ioctl(fh->fd, CDROM_LOCKDOOR, 0);
736     #endif
737     }
738    
739    
740     /*
741     * Read CD-ROM TOC (binary MSF format, 804 bytes max.)
742     */
743    
744     bool SysCDReadTOC(void *arg, uint8 *toc)
745     {
746     file_handle *fh = (file_handle *)arg;
747     if (!fh)
748     return false;
749    
750     if (fh->is_cdrom) {
751     #if defined(__linux__)
752     uint8 *p = toc + 2;
753    
754     // Header
755     cdrom_tochdr header;
756     if (ioctl(fh->fd, CDROMREADTOCHDR, &header) < 0)
757     return false;
758     *p++ = header.cdth_trk0;
759     *p++ = header.cdth_trk1;
760    
761     // Tracks
762     cdrom_tocentry entry;
763     for (int i=header.cdth_trk0; i<=header.cdth_trk1; i++) {
764     entry.cdte_track = i;
765     entry.cdte_format = CDROM_MSF;
766     if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0)
767     return false;
768     *p++ = 0;
769     *p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl;
770     *p++ = entry.cdte_track;
771     *p++ = 0;
772     *p++ = 0;
773     *p++ = entry.cdte_addr.msf.minute;
774     *p++ = entry.cdte_addr.msf.second;
775     *p++ = entry.cdte_addr.msf.frame;
776     }
777    
778     // Leadout track
779     entry.cdte_track = CDROM_LEADOUT;
780     entry.cdte_format = CDROM_MSF;
781     if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0)
782     return false;
783     *p++ = 0;
784     *p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl;
785     *p++ = entry.cdte_track;
786     *p++ = 0;
787     *p++ = 0;
788     *p++ = entry.cdte_addr.msf.minute;
789     *p++ = entry.cdte_addr.msf.second;
790     *p++ = entry.cdte_addr.msf.frame;
791    
792     // TOC size
793     int toc_size = p - toc;
794     *toc++ = toc_size >> 8;
795     *toc++ = toc_size & 0xff;
796     return true;
797 gbeauche 1.25 #elif defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_2) && defined(HAVE_FRAMEWORK_COREFOUNDATION)
798 nigel 1.19 extern bool DarwinCDReadTOC(char *name, uint8 *toc);
799    
800     return DarwinCDReadTOC(fh->name, toc);
801 cebix 1.4 #elif defined(__FreeBSD__)
802 cebix 1.1 uint8 *p = toc + 2;
803    
804     // Header
805     struct ioc_toc_header header;
806     if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
807     return false;
808     *p++ = header.starting_track;
809     *p++ = header.ending_track;
810    
811     // Tracks
812     struct ioc_read_toc_single_entry entry;
813     for (int i=header.starting_track; i<=header.ending_track; i++) {
814     entry.track = i;
815     entry.address_format = CD_MSF_FORMAT;
816     if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0)
817     return false;
818     *p++ = 0;
819     *p++ = (entry.entry.addr_type << 4) | entry.entry.control;
820     *p++ = entry.entry.track;
821     *p++ = 0;
822     *p++ = 0;
823     *p++ = entry.entry.addr.msf.minute;
824     *p++ = entry.entry.addr.msf.second;
825     *p++ = entry.entry.addr.msf.frame;
826     }
827    
828     // Leadout track
829     entry.track = CD_TRACK_INFO;
830     entry.address_format = CD_MSF_FORMAT;
831     if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0)
832     return false;
833     *p++ = 0;
834     *p++ = (entry.entry.addr_type << 4) | entry.entry.control;
835     *p++ = entry.entry.track;
836     *p++ = 0;
837     *p++ = 0;
838     *p++ = entry.entry.addr.msf.minute;
839     *p++ = entry.entry.addr.msf.second;
840     *p++ = entry.entry.addr.msf.frame;
841 cebix 1.4
842     // TOC size
843     int toc_size = p - toc;
844     *toc++ = toc_size >> 8;
845     *toc++ = toc_size & 0xff;
846     return true;
847     #elif defined(__NetBSD__)
848     uint8 *p = toc + 2;
849    
850     // Header
851     struct ioc_toc_header header;
852     if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
853     return false;
854     *p++ = header.starting_track;
855     *p++ = header.ending_track;
856    
857     // Tracks (this is nice... :-)
858     struct ioc_read_toc_entry entries;
859     entries.address_format = CD_MSF_FORMAT;
860     entries.starting_track = 1;
861     entries.data_len = 800;
862     entries.data = (cd_toc_entry *)p;
863     if (ioctl(fh->fd, CDIOREADTOCENTRIES, &entries) < 0)
864     return false;
865 cebix 1.1
866     // TOC size
867     int toc_size = p - toc;
868     *toc++ = toc_size >> 8;
869     *toc++ = toc_size & 0xff;
870     return true;
871 cebix 1.15 #else
872     return false;
873 cebix 1.1 #endif
874     } else
875     return false;
876     }
877    
878    
879     /*
880     * Read CD-ROM position data (Sub-Q Channel, 16 bytes, see SCSI standard)
881     */
882    
883     bool SysCDGetPosition(void *arg, uint8 *pos)
884     {
885     file_handle *fh = (file_handle *)arg;
886     if (!fh)
887     return false;
888    
889     if (fh->is_cdrom) {
890     #if defined(__linux__)
891     cdrom_subchnl chan;
892     chan.cdsc_format = CDROM_MSF;
893     if (ioctl(fh->fd, CDROMSUBCHNL, &chan) < 0)
894     return false;
895     *pos++ = 0;
896     *pos++ = chan.cdsc_audiostatus;
897     *pos++ = 0;
898     *pos++ = 12; // Sub-Q data length
899     *pos++ = 0;
900     *pos++ = (chan.cdsc_adr << 4) | chan.cdsc_ctrl;
901     *pos++ = chan.cdsc_trk;
902     *pos++ = chan.cdsc_ind;
903     *pos++ = 0;
904     *pos++ = chan.cdsc_absaddr.msf.minute;
905     *pos++ = chan.cdsc_absaddr.msf.second;
906     *pos++ = chan.cdsc_absaddr.msf.frame;
907     *pos++ = 0;
908     *pos++ = chan.cdsc_reladdr.msf.minute;
909     *pos++ = chan.cdsc_reladdr.msf.second;
910     *pos++ = chan.cdsc_reladdr.msf.frame;
911     return true;
912 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
913 cebix 1.1 struct ioc_read_subchannel chan;
914     chan.data_format = CD_MSF_FORMAT;
915     chan.address_format = CD_MSF_FORMAT;
916     chan.track = CD_CURRENT_POSITION;
917     if (ioctl(fh->fd, CDIOCREADSUBCHANNEL, &chan) < 0)
918     return false;
919     *pos++ = 0;
920     *pos++ = chan.data->header.audio_status;
921     *pos++ = 0;
922     *pos++ = 12; // Sub-Q data length
923     *pos++ = 0;
924     *pos++ = (chan.data->what.position.addr_type << 4) | chan.data->what.position.control;
925     *pos++ = chan.data->what.position.track_number;
926     *pos++ = chan.data->what.position.index_number;
927     *pos++ = 0;
928     *pos++ = chan.data->what.position.absaddr.msf.minute;
929     *pos++ = chan.data->what.position.absaddr.msf.second;
930     *pos++ = chan.data->what.position.absaddr.msf.frame;
931     *pos++ = 0;
932     *pos++ = chan.data->what.position.reladdr.msf.minute;
933     *pos++ = chan.data->what.position.reladdr.msf.second;
934     *pos++ = chan.data->what.position.reladdr.msf.frame;
935     return true;
936 cebix 1.15 #else
937     return false;
938 cebix 1.1 #endif
939     } else
940     return false;
941     }
942    
943    
944     /*
945     * Play CD audio
946     */
947    
948     bool SysCDPlay(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, uint8 end_m, uint8 end_s, uint8 end_f)
949     {
950     file_handle *fh = (file_handle *)arg;
951     if (!fh)
952     return false;
953    
954     if (fh->is_cdrom) {
955     #if defined(__linux__)
956     cdrom_msf play;
957     play.cdmsf_min0 = start_m;
958     play.cdmsf_sec0 = start_s;
959     play.cdmsf_frame0 = start_f;
960     play.cdmsf_min1 = end_m;
961     play.cdmsf_sec1 = end_s;
962     play.cdmsf_frame1 = end_f;
963     return ioctl(fh->fd, CDROMPLAYMSF, &play) == 0;
964 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
965 cebix 1.1 struct ioc_play_msf play;
966     play.start_m = start_m;
967     play.start_s = start_s;
968     play.start_f = start_f;
969     play.end_m = end_m;
970     play.end_s = end_s;
971     play.end_f = end_f;
972     return ioctl(fh->fd, CDIOCPLAYMSF, &play) == 0;
973 cebix 1.15 #else
974     return false;
975 cebix 1.1 #endif
976     } else
977     return false;
978     }
979    
980    
981     /*
982     * Pause CD audio
983     */
984    
985     bool SysCDPause(void *arg)
986     {
987     file_handle *fh = (file_handle *)arg;
988     if (!fh)
989     return false;
990    
991     if (fh->is_cdrom) {
992     #if defined(__linux__)
993     return ioctl(fh->fd, CDROMPAUSE) == 0;
994 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
995 cebix 1.1 return ioctl(fh->fd, CDIOCPAUSE) == 0;
996 cebix 1.15 #else
997     return false;
998 cebix 1.1 #endif
999     } else
1000     return false;
1001     }
1002    
1003    
1004     /*
1005     * Resume paused CD audio
1006     */
1007    
1008     bool SysCDResume(void *arg)
1009     {
1010     file_handle *fh = (file_handle *)arg;
1011     if (!fh)
1012     return false;
1013    
1014     if (fh->is_cdrom) {
1015     #if defined(__linux__)
1016     return ioctl(fh->fd, CDROMRESUME) == 0;
1017 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1018 cebix 1.1 return ioctl(fh->fd, CDIOCRESUME) == 0;
1019 cebix 1.15 #else
1020     return false;
1021 cebix 1.1 #endif
1022     } else
1023     return false;
1024     }
1025    
1026    
1027     /*
1028     * Stop CD audio
1029     */
1030    
1031     bool SysCDStop(void *arg, uint8 lead_out_m, uint8 lead_out_s, uint8 lead_out_f)
1032     {
1033     file_handle *fh = (file_handle *)arg;
1034     if (!fh)
1035     return false;
1036    
1037     if (fh->is_cdrom) {
1038     #if defined(__linux__)
1039     return ioctl(fh->fd, CDROMSTOP) == 0;
1040 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1041 cebix 1.1 return ioctl(fh->fd, CDIOCSTOP) == 0;
1042 cebix 1.15 #else
1043     return false;
1044 cebix 1.1 #endif
1045     } else
1046     return false;
1047     }
1048    
1049    
1050     /*
1051     * Perform CD audio fast-forward/fast-reverse operation starting from specified address
1052     */
1053    
1054     bool SysCDScan(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, bool reverse)
1055     {
1056     file_handle *fh = (file_handle *)arg;
1057     if (!fh)
1058     return false;
1059    
1060     // Not supported under Linux
1061     return false;
1062     }
1063    
1064    
1065     /*
1066     * Set CD audio volume (0..255 each channel)
1067     */
1068    
1069     void SysCDSetVolume(void *arg, uint8 left, uint8 right)
1070     {
1071     file_handle *fh = (file_handle *)arg;
1072     if (!fh)
1073     return;
1074    
1075     if (fh->is_cdrom) {
1076     #if defined(__linux__)
1077     cdrom_volctrl vol;
1078     vol.channel0 = vol.channel2 = left;
1079     vol.channel1 = vol.channel3 = right;
1080     ioctl(fh->fd, CDROMVOLCTRL, &vol);
1081 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1082 cebix 1.1 struct ioc_vol vol;
1083     vol.vol[0] = vol.vol[2] = left;
1084     vol.vol[1] = vol.vol[3] = right;
1085     ioctl(fh->fd, CDIOCSETVOL, &vol);
1086     #endif
1087     }
1088     }
1089    
1090    
1091     /*
1092     * Get CD audio volume (0..255 each channel)
1093     */
1094    
1095     void SysCDGetVolume(void *arg, uint8 &left, uint8 &right)
1096     {
1097     file_handle *fh = (file_handle *)arg;
1098     if (!fh)
1099     return;
1100    
1101     left = right = 0;
1102     if (fh->is_cdrom) {
1103     #if defined(__linux__)
1104     cdrom_volctrl vol;
1105     ioctl(fh->fd, CDROMVOLREAD, &vol);
1106     left = vol.channel0;
1107     right = vol.channel1;
1108 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1109 cebix 1.1 struct ioc_vol vol;
1110     ioctl(fh->fd, CDIOCGETVOL, &vol);
1111     left = vol.vol[0];
1112     right = vol.vol[1];
1113     #endif
1114     }
1115     }