ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sys_unix.cpp
Revision: 1.17
Committed: 2002-03-29T16:24:18Z (22 years, 6 months ago) by gbeauche
Branch: MAIN
Changes since 1.16: +6 -8 lines
Log Message:
- Clarified test of access() return value
- s/strcpy/sprintf/ for cd_dev concatenation
- Statically allocate fd_dev[] and cd_dev[]
Better testing is yet to be done

File Contents

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