ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sys_unix.cpp
Revision: 1.26
Committed: 2005-08-01T05:23:02Z (19 years, 1 month ago) by gbeauche
Branch: MAIN
Changes since 1.25: +0 -31 lines
Log Message:
Drop the old _llseek() hack. That was causing problems and we "now" use the
right approach with LFS for a few years now.

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