1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 
21 #include "quakedef.h"
22 
23 #include <sys/types.h>
24 #include <sys/cdio.h>
25 #include <sys/ioctl.h>
26 
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <paths.h>
30 #include <unistd.h>
31 #include <time.h>
32 #ifndef __FreeBSD__
33 # include <util.h>
34 #endif
35 
36 #include "cdaudio.h"
37 
38 
39 #ifndef __FreeBSD__
40 # define DEFAULT_CD_DEVICE _PATH_DEV "cd0"
41 #else
42 # define DEFAULT_CD_DEVICE "/dev/acd0c"
43 #endif
44 
45 static int cdfile = -1;
46 static char cd_dev[64] = DEFAULT_CD_DEVICE;
47 
48 
CDAudio_SysEject(void)49 void CDAudio_SysEject (void)
50 {
51 	if (cdfile == -1)
52 		return;
53 
54 	ioctl(cdfile, CDIOCALLOW);
55 	if (ioctl(cdfile, CDIOCEJECT) == -1)
56 		Con_Print("ioctl CDIOCEJECT failed\n");
57 }
58 
59 
CDAudio_SysCloseDoor(void)60 void CDAudio_SysCloseDoor (void)
61 {
62 	if (cdfile == -1)
63 		return;
64 
65 	ioctl(cdfile, CDIOCALLOW);
66 	if (ioctl(cdfile, CDIOCCLOSE) == -1)
67 		Con_Print("ioctl CDIOCCLOSE failed\n");
68 }
69 
CDAudio_SysGetAudioDiskInfo(void)70 int CDAudio_SysGetAudioDiskInfo (void)
71 {
72 	struct ioc_toc_header tochdr;
73 
74 	if (cdfile == -1)
75 		return -1;
76 
77 	if (ioctl(cdfile, CDIOREADTOCHEADER, &tochdr) == -1)
78 	{
79 		Con_Print("ioctl CDIOREADTOCHEADER failed\n");
80 		return -1;
81 	}
82 
83 	if (tochdr.starting_track < 1)
84 	{
85 		Con_Print("CDAudio: no music tracks\n");
86 		return -1;
87 	}
88 
89 	return tochdr.ending_track;
90 }
91 
92 
CDAudio_SysGetVolume(void)93 float CDAudio_SysGetVolume (void)
94 {
95 	struct ioc_vol vol;
96 
97 	if (cdfile == -1)
98 		return -1.0f;
99 
100 	if (ioctl (cdfile, CDIOCGETVOL, &vol) == -1)
101 	{
102 		Con_Print("ioctl CDIOCGETVOL failed\n");
103 		return -1.0f;
104 	}
105 
106 	return (vol.vol[0] + vol.vol[1]) / 2.0f / 255.0f;
107 }
108 
109 
CDAudio_SysSetVolume(float volume)110 void CDAudio_SysSetVolume (float volume)
111 {
112 	struct ioc_vol vol;
113 
114 	if (cdfile == -1)
115 		return;
116 
117 	vol.vol[0] = vol.vol[1] = volume * 255;
118 	vol.vol[2] = vol.vol[3] = 0;
119 
120 	if (ioctl (cdfile, CDIOCSETVOL, &vol) == -1)
121 		Con_Printf ("ioctl CDIOCSETVOL failed\n");
122 }
123 
124 
CDAudio_SysPlay(int track)125 int CDAudio_SysPlay (int track)
126 {
127 	struct ioc_read_toc_entry rte;
128 	struct cd_toc_entry entry;
129 	struct ioc_play_track ti;
130 
131 	if (cdfile == -1)
132 		return -1;
133 
134 	// don't try to play a non-audio track
135 	rte.address_format = CD_MSF_FORMAT;
136 	rte.starting_track = track;
137 	rte.data_len = sizeof(entry);
138 	rte.data = &entry;
139 	if (ioctl(cdfile, CDIOREADTOCENTRYS, &rte) == -1)
140 	{
141 		Con_Print("ioctl CDIOREADTOCENTRYS failed\n");
142 		return -1;
143 	}
144 	if (entry.control & 4)  // if it's a data track
145 	{
146 		Con_Printf("CDAudio: track %i is not audio\n", track);
147 		return -1;
148 	}
149 
150 	if (cdPlaying)
151 		CDAudio_Stop();
152 
153 	ti.start_track = track;
154 	ti.end_track = track;
155 	ti.start_index = 1;
156 	ti.end_index = 99;
157 
158 	if (ioctl(cdfile, CDIOCPLAYTRACKS, &ti) == -1)
159 	{
160 		Con_Print("ioctl CDIOCPLAYTRACKS failed\n");
161 		return -1;
162 	}
163 
164 	if (ioctl(cdfile, CDIOCRESUME) == -1)
165 	{
166 		Con_Print("ioctl CDIOCRESUME failed\n");
167 		return -1;
168 	}
169 
170 	return 0;
171 }
172 
173 
CDAudio_SysStop(void)174 int CDAudio_SysStop (void)
175 {
176 	if (cdfile == -1)
177 		return -1;
178 
179 	if (ioctl(cdfile, CDIOCSTOP) == -1)
180 	{
181 		Con_Printf("ioctl CDIOCSTOP failed (%d)\n", errno);
182 		return -1;
183 	}
184 	ioctl(cdfile, CDIOCALLOW);
185 
186 	return 0;
187 }
188 
CDAudio_SysPause(void)189 int CDAudio_SysPause (void)
190 {
191 	if (cdfile == -1)
192 		return -1;
193 
194 	if (ioctl(cdfile, CDIOCPAUSE) == -1)
195 	{
196 		Con_Print("ioctl CDIOCPAUSE failed\n");
197 		return -1;
198 	}
199 
200 	return 0;
201 }
202 
203 
CDAudio_SysResume(void)204 int CDAudio_SysResume (void)
205 {
206 	if (cdfile == -1)
207 		return -1;
208 
209 	if (ioctl(cdfile, CDIOCRESUME) == -1)
210 		Con_Print("ioctl CDIOCRESUME failed\n");
211 
212 	return 0;
213 }
214 
CDAudio_SysUpdate(void)215 int CDAudio_SysUpdate (void)
216 {
217 	static time_t lastchk = 0;
218 	struct ioc_read_subchannel subchnl;
219 	struct cd_sub_channel_info data;
220 
221 	if (cdPlaying && lastchk < time(NULL))
222 	{
223 		lastchk = time(NULL) + 2; //two seconds between chks
224 
225 		bzero(&subchnl, sizeof(subchnl));
226 		subchnl.data = &data;
227 		subchnl.data_len = sizeof(data);
228 		subchnl.address_format = CD_MSF_FORMAT;
229 		subchnl.data_format = CD_CURRENT_POSITION;
230 
231 		if (ioctl(cdfile, CDIOCREADSUBCHANNEL, &subchnl) == -1)
232 		{
233 			Con_Print("ioctl CDIOCREADSUBCHANNEL failed\n");
234 			cdPlaying = false;
235 			return -1;
236 		}
237 		if (data.header.audio_status != CD_AS_PLAY_IN_PROGRESS &&
238 			data.header.audio_status != CD_AS_PLAY_PAUSED)
239 		{
240 			cdPlaying = false;
241 			if (cdPlayLooping)
242 				CDAudio_Play(cdPlayTrack, true);
243 		}
244 		else
245 			cdPlayTrack = data.what.position.track_number;
246 	}
247 
248 	return 0;
249 }
250 
CDAudio_SysInit(void)251 void CDAudio_SysInit (void)
252 {
253 	int i;
254 
255 // COMMANDLINEOPTION: BSD Sound: -cddev <devicepath> chooses which CD drive to use
256 	if ((i = COM_CheckParm("-cddev")) != 0 && i < com_argc - 1)
257 		strlcpy(cd_dev, com_argv[i + 1], sizeof(cd_dev));
258 }
259 
CDAudio_SysStartup(void)260 int CDAudio_SysStartup (void)
261 {
262 #ifndef __FreeBSD__
263 	char buff [80];
264 
265 	if ((cdfile = opendisk(cd_dev, O_RDONLY, buff, sizeof(buff), 0)) == -1)
266 #else
267 	if ((cdfile = open(cd_dev, O_RDONLY)) < 0)
268 #endif
269 	{
270 		Con_Printf("CDAudio_SysStartup: open of \"%s\" failed (%i)\n",
271 					cd_dev, errno);
272 		cdfile = -1;
273 		return -1;
274 	}
275 
276 	return 0;
277 }
278 
CDAudio_SysShutdown(void)279 void CDAudio_SysShutdown (void)
280 {
281 	close(cdfile);
282 	cdfile = -1;
283 }
284