1 |
|
/* |
2 |
|
* sys_unix.cpp - System dependent routines, Unix implementation |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-1999 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2005 Christian Bauer |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
24 |
|
#include <sys/stat.h> |
25 |
|
#include <errno.h> |
26 |
|
|
27 |
+ |
#ifdef HAVE_AVAILABILITYMACROS_H |
28 |
+ |
#include <AvailabilityMacros.h> |
29 |
+ |
#endif |
30 |
+ |
|
31 |
|
#ifdef __linux__ |
32 |
|
#include <sys/mount.h> |
33 |
|
#include <linux/cdrom.h> |
35 |
|
#include <linux/major.h> |
36 |
|
#include <linux/kdev_t.h> |
37 |
|
#include <linux/unistd.h> |
38 |
+ |
#include <dirent.h> |
39 |
|
|
40 |
|
#ifdef __NR__llseek |
41 |
< |
_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo, loff_t *, res, uint, wh); |
41 |
> |
_syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo, loff_t *, res, unsigned int, wh); |
42 |
|
#else |
43 |
< |
static int _llseek(uint fd, ulong hi, ulong lo, loff_t *res, uint wh) |
43 |
> |
static int _llseek(unsigned int fd, unsigned long hi, unsigned long lo, loff_t *res, unsigned int wh) |
44 |
|
{ |
45 |
|
if (hi) |
46 |
|
return -1; |
81 |
|
int cdrom_cap; // CD-ROM capability flags (only valid if is_cdrom is true) |
82 |
|
#elif defined(__FreeBSD__) |
83 |
|
struct ioc_capability cdrom_cap; |
84 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) |
85 |
+ |
char *ioctl_name; // For CDs on OS X - a device for special ioctls |
86 |
+ |
int ioctl_fd; |
87 |
|
#endif |
88 |
|
}; |
89 |
|
|
128 |
|
void SysAddFloppyPrefs(void) |
129 |
|
{ |
130 |
|
#if defined(__linux__) |
131 |
< |
PrefsAddString("floppy", "/dev/fd0H1440"); |
132 |
< |
PrefsAddString("floppy", "/dev/fd1H1440"); |
131 |
> |
if (access("/dev/.devfsd", F_OK) < 0) { |
132 |
> |
PrefsAddString("floppy", "/dev/fd0u1440"); |
133 |
> |
PrefsAddString("floppy", "/dev/fd1u1440"); |
134 |
> |
} else { |
135 |
> |
DIR *fd_dir = opendir("/dev/floppy"); |
136 |
> |
if (fd_dir) { |
137 |
> |
struct dirent *floppy_dev; |
138 |
> |
while ((floppy_dev = readdir(fd_dir)) != NULL) { |
139 |
> |
if (strstr(floppy_dev->d_name, "u1440") != NULL) { |
140 |
> |
char fd_dev[20]; |
141 |
> |
sprintf(fd_dev, "/dev/floppy/%s", floppy_dev->d_name); |
142 |
> |
PrefsAddString("floppy", fd_dev); |
143 |
> |
} |
144 |
> |
} |
145 |
> |
closedir(fd_dir); |
146 |
> |
} |
147 |
> |
} |
148 |
|
#elif defined(__NetBSD__) |
149 |
|
PrefsAddString("floppy", "/dev/fd0a"); |
150 |
|
PrefsAddString("floppy", "/dev/fd1a"); |
151 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) |
152 |
+ |
#if defined(AQUA) || defined(HAVE_FRAMEWORK_IOKIT) |
153 |
+ |
extern void DarwinAddFloppyPrefs(void); |
154 |
+ |
|
155 |
+ |
DarwinAddFloppyPrefs(); |
156 |
+ |
#else |
157 |
+ |
// Until I can convince the other guys that my Darwin code is useful, |
158 |
+ |
// we just add something safe (a non-existant device): |
159 |
+ |
PrefsAddString("floppy", "/dev/null"); |
160 |
+ |
#endif |
161 |
|
#else |
162 |
|
PrefsAddString("floppy", "/dev/fd0"); |
163 |
|
PrefsAddString("floppy", "/dev/fd1"); |
168 |
|
/* |
169 |
|
* This gets called when no "disk" prefs items are found |
170 |
|
* It scans for available HFS volumes and adds appropriate prefs items |
171 |
+ |
* On OS X, we could do the same, but on an OS X machine I think it is |
172 |
+ |
* very unlikely that any mounted volumes would contain a system which |
173 |
+ |
* is old enough to boot a 68k Mac, so we just do nothing here for now. |
174 |
|
*/ |
175 |
|
|
176 |
|
void SysAddDiskPrefs(void) |
182 |
|
while(fgets(line, 255, f)) { |
183 |
|
// Read line |
184 |
|
int len = strlen(line); |
185 |
< |
if (len == 0) |
185 |
> |
if (len == 0 || line[0] == '#') |
186 |
|
continue; |
187 |
|
line[len-1] = 0; |
188 |
|
|
212 |
|
return; |
213 |
|
|
214 |
|
#if defined(__linux__) |
215 |
< |
PrefsAddString("cdrom", "/dev/cdrom"); |
216 |
< |
#elif defined(__FreeBSD__) |
215 |
> |
if (access("/dev/.devfsd", F_OK) < 0) |
216 |
> |
PrefsAddString("cdrom", "/dev/cdrom"); |
217 |
> |
else { |
218 |
> |
DIR *cd_dir = opendir("/dev/cdroms"); |
219 |
> |
if (cd_dir) { |
220 |
> |
struct dirent *cdrom_dev; |
221 |
> |
while ((cdrom_dev = readdir(cd_dir)) != NULL) { |
222 |
> |
if (strcmp(cdrom_dev->d_name, ".") != 0 && strcmp(cdrom_dev->d_name, "..") != 0) { |
223 |
> |
char cd_dev[20]; |
224 |
> |
sprintf(cd_dev, "/dev/cdroms/%s", cdrom_dev->d_name); |
225 |
> |
PrefsAddString("cdrom", cd_dev); |
226 |
> |
} |
227 |
> |
} |
228 |
> |
closedir(cd_dir); |
229 |
> |
} |
230 |
> |
} |
231 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
232 |
> |
#if defined(AQUA) || defined(HAVE_FRAMEWORK_IOKIT) |
233 |
> |
extern void DarwinAddCDROMPrefs(void); |
234 |
> |
|
235 |
> |
DarwinAddCDROMPrefs(); |
236 |
> |
#else |
237 |
> |
// Until I can convince the other guys that my Darwin code is useful, |
238 |
> |
// we just do nothing (it is safe to have no cdrom device) |
239 |
> |
#endif |
240 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
241 |
|
PrefsAddString("cdrom", "/dev/cd0c"); |
182 |
– |
#elif defined(__NetBSD__) |
183 |
– |
PrefsAddString("cdrom", "/dev/cd0d"); |
242 |
|
#endif |
243 |
|
} |
244 |
|
|
250 |
|
void SysAddSerialPrefs(void) |
251 |
|
{ |
252 |
|
#if defined(__linux__) |
253 |
< |
PrefsAddString("seriala", "/dev/ttyS0"); |
254 |
< |
PrefsAddString("serialb", "/dev/ttyS1"); |
253 |
> |
if (access("/dev/.devfsd", F_OK) < 0) { |
254 |
> |
PrefsAddString("seriala", "/dev/ttyS0"); |
255 |
> |
PrefsAddString("serialb", "/dev/ttyS1"); |
256 |
> |
} else { |
257 |
> |
PrefsAddString("seriala", "/dev/tts/0"); |
258 |
> |
PrefsAddString("serialb", "/dev/tts/1"); |
259 |
> |
} |
260 |
|
#elif defined(__FreeBSD__) |
261 |
|
PrefsAddString("seriala", "/dev/cuaa0"); |
262 |
|
PrefsAddString("serialb", "/dev/cuaa1"); |
263 |
|
#elif defined(__NetBSD__) |
264 |
|
PrefsAddString("seriala", "/dev/tty00"); |
265 |
|
PrefsAddString("serialb", "/dev/tty01"); |
266 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) |
267 |
+ |
#if defined(AQUA) || defined(HAVE_FRAMEWORK_IOKIT) |
268 |
+ |
extern void DarwinAddSerialPrefs(void); |
269 |
+ |
|
270 |
+ |
DarwinAddSerialPrefs(); |
271 |
+ |
#else |
272 |
+ |
// Until I can convince the other guys that my Darwin code is useful, |
273 |
+ |
// we just add something safe (non-existant devices): |
274 |
+ |
PrefsAddString("seriala", "/dev/null"); |
275 |
+ |
PrefsAddString("serialb", "/dev/null"); |
276 |
+ |
#endif |
277 |
|
#endif |
278 |
|
} |
279 |
|
|
326 |
|
bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0; |
327 |
|
#endif |
328 |
|
|
329 |
+ |
#if defined(__APPLE__) && defined(__MACH__) |
330 |
+ |
// |
331 |
+ |
// There is no set filename in /dev which is the cdrom, |
332 |
+ |
// so we have to see if it is any of the devices that we found earlier |
333 |
+ |
// |
334 |
+ |
const char *cdrom; |
335 |
+ |
int tmp = 0; |
336 |
+ |
|
337 |
+ |
while ( (cdrom = PrefsFindString("cdrom", tmp) ) != NULL ) |
338 |
+ |
{ |
339 |
+ |
if ( strcmp(name, cdrom) == 0 ) |
340 |
+ |
{ |
341 |
+ |
is_cdrom = 1; |
342 |
+ |
read_only = 1; |
343 |
+ |
break; |
344 |
+ |
} |
345 |
+ |
++tmp; |
346 |
+ |
} |
347 |
+ |
#endif |
348 |
+ |
|
349 |
|
D(bug("Sys_open(%s, %s)\n", name, read_only ? "read-only" : "read/write")); |
350 |
|
|
351 |
|
// Check if write access is allowed, set read-only flag if not |
367 |
|
} |
368 |
|
|
369 |
|
// Open file/device |
370 |
< |
#if defined(__linux__) |
370 |
> |
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) |
371 |
|
int fd = open(name, (read_only ? O_RDONLY : O_RDWR) | (is_cdrom ? O_NONBLOCK : 0)); |
372 |
|
#else |
373 |
|
int fd = open(name, read_only ? O_RDONLY : O_RDWR); |
428 |
|
fh->is_floppy = ((st.st_rdev >> 16) == 2); |
429 |
|
#endif |
430 |
|
} |
431 |
+ |
#if defined(__APPLE__) && defined(__MACH__) |
432 |
+ |
|
433 |
+ |
// In OS X, the device name is OK for sending ioctls to, |
434 |
+ |
// but not for reading raw CDROM data from. |
435 |
+ |
// (it seems to have extra data padded in) |
436 |
+ |
// |
437 |
+ |
// So, we keep the already opened fiole handle, |
438 |
+ |
// and open a slightly different file for CDROM data |
439 |
+ |
// |
440 |
+ |
if ( is_cdrom ) |
441 |
+ |
{ |
442 |
+ |
fh->ioctl_name = fh->name; |
443 |
+ |
fh->ioctl_fd = fh->fd; |
444 |
+ |
|
445 |
+ |
fh->name = (char *) malloc(strlen(name) + 2); |
446 |
+ |
if ( fh->name ) |
447 |
+ |
{ |
448 |
+ |
*fh->name = '\0'; |
449 |
+ |
strcat(fh->name, name); |
450 |
+ |
strcat(fh->name, "s1"); |
451 |
+ |
fh->fd = open(fh->name, (read_only ? O_RDONLY : O_RDWR)); |
452 |
+ |
if ( fh->fd < 0 ) { |
453 |
+ |
printf("WARNING: Cannot open %s (%s)\n", |
454 |
+ |
fh->name, strerror(errno)); |
455 |
+ |
return NULL; |
456 |
+ |
} |
457 |
+ |
} |
458 |
+ |
} |
459 |
+ |
#endif |
460 |
|
} |
461 |
|
} |
462 |
|
if (fh->is_floppy && first_floppy == NULL) |
579 |
|
fsync(fh->fd); |
580 |
|
ioctl(fh->fd, FDFLUSH); |
581 |
|
ioctl(fh->fd, FDEJECT); |
582 |
+ |
close(fh->fd); // Close and reopen so the driver will see the media change |
583 |
+ |
fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR); |
584 |
|
} else if (fh->is_cdrom) { |
585 |
|
ioctl(fh->fd, CDROMEJECT); |
586 |
|
close(fh->fd); // Close and reopen so the driver will see the media change |
589 |
|
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
590 |
|
if (fh->is_floppy) { |
591 |
|
fsync(fh->fd); |
467 |
– |
//ioctl(fh->fd, FDFLUSH); |
468 |
– |
//ioctl(fh->fd, FDEJECT); |
592 |
|
} else if (fh->is_cdrom) { |
593 |
|
ioctl(fh->fd, CDIOCEJECT); |
594 |
|
close(fh->fd); // Close and reopen so the driver will see the media change |
595 |
|
fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK); |
596 |
|
} |
597 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) |
598 |
+ |
if ( fh->is_cdrom ) { |
599 |
+ |
|
600 |
+ |
// Stolen from IOKit/storage/IOMediaBSDClient.h |
601 |
+ |
#define DKIOCEJECT _IO('d', 21) |
602 |
+ |
|
603 |
+ |
close(fh->fd); |
604 |
+ |
if ( ioctl(fh->ioctl_fd, DKIOCEJECT) < 0 ) |
605 |
+ |
{ |
606 |
+ |
printf("ioctl(DKIOCEJECT) failed on file %s: %s\n", |
607 |
+ |
fh->ioctl_name, strerror(errno)); |
608 |
+ |
|
609 |
+ |
// If we are running OSX, the device may be is busy |
610 |
+ |
// due to the Finder having the disk mounted and open, |
611 |
+ |
// so we have to use another method. |
612 |
+ |
|
613 |
+ |
// The only problem is that this takes about 5 seconds: |
614 |
+ |
|
615 |
+ |
char *cmd = (char *) malloc(30+sizeof(fh->name)); |
616 |
+ |
|
617 |
+ |
if ( ! cmd ) |
618 |
+ |
return; |
619 |
+ |
close(fh->ioctl_fd); |
620 |
+ |
sprintf(cmd, "diskutil eject %s 2>&1 >/dev/null", fh->name); |
621 |
+ |
system(cmd); |
622 |
+ |
} |
623 |
+ |
} |
624 |
|
#endif |
625 |
|
} |
626 |
|
|
697 |
|
} else if (fh->is_floppy) { |
698 |
|
char block[512]; |
699 |
|
lseek(fh->fd, 0, SEEK_SET); |
700 |
< |
return read(fh->fd, block, 512) == 512; |
700 |
> |
ssize_t actual = read(fh->fd, block, 512); |
701 |
> |
if (actual < 0) { |
702 |
> |
close(fh->fd); // Close and reopen so the driver will see the media change |
703 |
> |
fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR); |
704 |
> |
actual = read(fh->fd, block, 512); |
705 |
> |
} |
706 |
> |
return actual == 512; |
707 |
|
} else if (fh->is_cdrom) { |
708 |
+ |
#ifdef CDROM_MEDIA_CHANGED |
709 |
+ |
if (fh->cdrom_cap & CDC_MEDIA_CHANGED) { |
710 |
+ |
// If we don't do this, all attempts to read from a disc fail |
711 |
+ |
// once the tray has been opened (altough the TOC reads fine). |
712 |
+ |
// Can somebody explain this to me? |
713 |
+ |
if (ioctl(fh->fd, CDROM_MEDIA_CHANGED) == 1) { |
714 |
+ |
close(fh->fd); |
715 |
+ |
fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK); |
716 |
+ |
} |
717 |
+ |
} |
718 |
+ |
#endif |
719 |
|
#ifdef CDROM_DRIVE_STATUS |
720 |
|
if (fh->cdrom_cap & CDC_DRIVE_STATUS) { |
721 |
|
return ioctl(fh->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK; |
827 |
|
*toc++ = toc_size >> 8; |
828 |
|
*toc++ = toc_size & 0xff; |
829 |
|
return true; |
830 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_2) |
831 |
+ |
extern bool DarwinCDReadTOC(char *name, uint8 *toc); |
832 |
+ |
|
833 |
+ |
return DarwinCDReadTOC(fh->name, toc); |
834 |
|
#elif defined(__FreeBSD__) |
835 |
|
uint8 *p = toc + 2; |
836 |
|
|
901 |
|
*toc++ = toc_size >> 8; |
902 |
|
*toc++ = toc_size & 0xff; |
903 |
|
return true; |
904 |
+ |
#else |
905 |
+ |
return false; |
906 |
|
#endif |
907 |
|
} else |
908 |
|
return false; |
966 |
|
*pos++ = chan.data->what.position.reladdr.msf.second; |
967 |
|
*pos++ = chan.data->what.position.reladdr.msf.frame; |
968 |
|
return true; |
969 |
+ |
#else |
970 |
+ |
return false; |
971 |
|
#endif |
972 |
|
} else |
973 |
|
return false; |
1003 |
|
play.end_s = end_s; |
1004 |
|
play.end_f = end_f; |
1005 |
|
return ioctl(fh->fd, CDIOCPLAYMSF, &play) == 0; |
1006 |
+ |
#else |
1007 |
+ |
return false; |
1008 |
|
#endif |
1009 |
|
} else |
1010 |
|
return false; |
1026 |
|
return ioctl(fh->fd, CDROMPAUSE) == 0; |
1027 |
|
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
1028 |
|
return ioctl(fh->fd, CDIOCPAUSE) == 0; |
1029 |
+ |
#else |
1030 |
+ |
return false; |
1031 |
|
#endif |
1032 |
|
} else |
1033 |
|
return false; |
1049 |
|
return ioctl(fh->fd, CDROMRESUME) == 0; |
1050 |
|
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
1051 |
|
return ioctl(fh->fd, CDIOCRESUME) == 0; |
1052 |
+ |
#else |
1053 |
+ |
return false; |
1054 |
|
#endif |
1055 |
|
} else |
1056 |
|
return false; |
1072 |
|
return ioctl(fh->fd, CDROMSTOP) == 0; |
1073 |
|
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
1074 |
|
return ioctl(fh->fd, CDIOCSTOP) == 0; |
1075 |
+ |
#else |
1076 |
+ |
return false; |
1077 |
|
#endif |
1078 |
|
} else |
1079 |
|
return false; |