summaryrefslogtreecommitdiff
path: root/macosx/plugins/DFCdrom/macsrc/cdr-macosx.c
blob: 7c423757ecaf5ca44cee5894b32e7456ce32c446 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Copyright (c) 2010, Wei Mingzhi <whistler@openoffice.org>.
 * All Rights Reserved.
 *
 * Based on: Cdrom for Psemu Pro like Emulators
 * By: linuzappz <linuzappz@hotmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 */

#include "cdr.h"

#ifdef _MACOSX

#include <IOKit/IOKitLib.h>
#include <IOKit/IOBSD.h>
#include <IOKit/storage/IOCDMedia.h>
#include <IOKit/storage/IODVDMedia.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IOCDMediaBSDClient.h>
#include <CoreFoundation/CoreFoundation.h>

int cdHandle = -1;
char cdDevice[4096] = "";

static int IsPsxDisc(const char *dev) {
	int fd;
	char buf[CD_FRAMESIZE_RAW];
	dk_cd_read_t r;
	
	fd = open(dev, O_RDONLY, 0);
	if (fd < 0) return 0;
	
	memset(&r, 0, sizeof(r));
	
	r.offset = msf_to_lba(0, 2, 4) * CD_FRAMESIZE_RAW;
	r.sectorArea = 0xF8;
	r.sectorType = kCDSectorTypeUnknown;
	r.bufferLength = CD_FRAMESIZE_RAW;
	r.buffer = buf;
	
	if (ioctl(fd, DKIOCCDREAD, &r) != kIOReturnSuccess) {
		close(fd);
		return 0;
	}
	
	close(fd);
	
	if (strncmp(buf + 56, "Sony Computer Entertainment", 27) == 0) {
		return 1;
	}
	
	return 0;
}

static void FindCdDevice(char *dev) {
	io_object_t   next_media;
	kern_return_t kern_result;
	io_iterator_t media_iterator;
	CFMutableDictionaryRef classes_to_match;
	const char *name, *cd = kIOCDMediaClass, *dvd = kIODVDMediaClass;
	
	dev[0] = '\0';
	name = cd;
	
start:
	classes_to_match = IOServiceMatching(name);
	if (classes_to_match == NULL) goto end;
	
	CFDictionarySetValue(classes_to_match, CFSTR(kIOMediaEjectableKey),
						 kCFBooleanTrue);
	
	kern_result = IOServiceGetMatchingServices(kIOMasterPortDefault,
											   classes_to_match, &media_iterator);
	
	if (kern_result != KERN_SUCCESS) goto end;
	
	next_media = IOIteratorNext(media_iterator);
	if (next_media != 0) {
		char psz_buf[0x32];
		size_t dev_path_length;
		CFTypeRef str_bsd_path;
		
		do {
			str_bsd_path = IORegistryEntryCreateCFProperty(next_media,
														   CFSTR(kIOBSDNameKey), kCFAllocatorDefault, 0);
			
			if (str_bsd_path == NULL) {
				IOObjectRelease(next_media);
				continue;
			}
			
			strcpy(psz_buf, "/dev/r");
			dev_path_length = strlen(psz_buf);
			
			if (CFStringGetCString(str_bsd_path, (char *)&psz_buf + dev_path_length,
								   sizeof(psz_buf) - dev_path_length, kCFStringEncodingASCII))
			{
				strcpy(dev, psz_buf);
				
				if (IsPsxDisc(dev)) {
					CFRelease(str_bsd_path);
					IOObjectRelease(next_media);
					IOObjectRelease(media_iterator);
					return;
				}
			}
			
			CFRelease(str_bsd_path);
			IOObjectRelease(next_media);
		} while ((next_media = IOIteratorNext(media_iterator)) != 0);
	}
	
	IOObjectRelease(media_iterator);
	
end:
	if (dev[0] == '\0') {
		if (name == cd) {
			name = dvd; // Is this really necessary or correct? Dunno...
			goto start;
		}
	}
}

int OpenCdHandle(const char *dev) {
	if (dev != NULL && dev[0] != '\0') strcpy(cdDevice, dev);
	else if (cdDevice[0] == '\0') FindCdDevice(cdDevice);
	
	cdHandle = open(cdDevice, O_RDONLY, 0);
	if (cdHandle < 0) return -1;
	
	if (CdrSpeed > 0) {
		u_int16_t speed = kCDSpeedMin * CdrSpeed;
		ioctl(cdHandle, DKIOCCDSETSPEED, &speed);
	}
	
	return 0;
}

void CloseCdHandle() {
	if (cdHandle != -1) close(cdHandle);
	cdHandle = -1;
}

int IsCdHandleOpen() {
	return (cdHandle != -1);
}

long GetTN(unsigned char *buffer) {
	if (cdHandle < 0) return -1;

	// TODO
	buffer[0] = 1;
	buffer[1] = 1;

	return 0;
}

long GetTD(unsigned char track, unsigned char *buffer) {
	if (cdHandle < 0) return -1;

	// TODO
	memset(buffer + 1, 0, 3);
	return 0;
}

long GetTE(unsigned char track, unsigned char *m, unsigned char *s, unsigned char *f) {
	return -1; // TODO
}

long ReadSector(crdata *cr) {
	int lba;
	dk_cd_read_t r;
	char buf[CD_FRAMESIZE_RAW];
	
	if (cdHandle < 0) return -1;
	
	lba = msf_to_lba(cr->msf.cdmsf_min0, cr->msf.cdmsf_sec0, cr->msf.cdmsf_frame0);
	
	memset(&r, 0, sizeof(r));
	
	r.offset = lba * CD_FRAMESIZE_RAW;
	r.sectorArea = 0xF8;
	r.sectorType = kCDSectorTypeUnknown;
	r.bufferLength = CD_FRAMESIZE_RAW;
	r.buffer = buf; // ??? Why using cr->buf directly does not work in threaded mode?
	
	if (ioctl(cdHandle, DKIOCCDREAD, &r) != kIOReturnSuccess) {
		return -1;
	}
	
	memcpy(cr->buf, buf, CD_FRAMESIZE_RAW);
	return 0;
}

long PlayCDDA(unsigned char *sector) {
	return 0; // TODO
}

long StopCDDA() {
	return 0; // TODO
}

long GetStatus(int playing, struct CdrStat *stat) {
	memset(stat, 0, sizeof(struct CdrStat));
	stat->Type = 0x01;
	
	// Close and reopen the CD handle. If opening failed,
	// then there is no CD in drive.
	// Note that this WILL be screwed if user inserted another
	// removable device such as USB stick when tray is open.
	// There may be a better way, but this should do the job.
	if (cdHandle >= 0) {
		close(cdHandle);
		cdHandle = -1;
	}
	
	cdHandle = open(cdDevice, O_RDONLY, 0);
	if (cdHandle < 0) {
		// No CD in drive
		stat->Type = 0xff;
		stat->Status |= 0x10;
	} else {
		if (CdrSpeed > 0) {
			u_int16_t speed = kCDSpeedMin * CdrSpeed;
			ioctl(cdHandle, DKIOCCDSETSPEED, &speed);
		}
	}
	
	return 0;
}

unsigned char *ReadSub(const unsigned char *time) {
	return NULL; // TODO
}

char *CDRgetDriveLetter(void) {
	return cdDevice;
}

#endif