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 // Quake is a trademark of Id Software, Inc., (c) 1996 Id Software, Inc. All
21 // rights reserved.
22 
23 // suggested by Zero_Dogg to fix a compile problem on Mandriva Linux
24 #include "quakedef.h"
25 
26 #include <linux/cdrom.h>
27 #include <sys/ioctl.h>
28 
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <unistd.h>
33 
34 #include "cdaudio.h"
35 
36 
37 static int cdfile = -1;
38 static char cd_dev[64] = "/dev/cdrom";
39 
40 
CDAudio_SysEject(void)41 void CDAudio_SysEject (void)
42 {
43 	if (cdfile == -1)
44 		return;
45 
46 	if (ioctl(cdfile, CDROMEJECT) == -1)
47 		Con_Print("ioctl CDROMEJECT failed\n");
48 }
49 
50 
CDAudio_SysCloseDoor(void)51 void CDAudio_SysCloseDoor (void)
52 {
53 	if (cdfile == -1)
54 		return;
55 
56 	if (ioctl(cdfile, CDROMCLOSETRAY) == -1)
57 		Con_Print("ioctl CDROMCLOSETRAY failed\n");
58 }
59 
CDAudio_SysGetAudioDiskInfo(void)60 int CDAudio_SysGetAudioDiskInfo (void)
61 {
62 	struct cdrom_tochdr tochdr;
63 
64 	if (cdfile == -1)
65 		return -1;
66 
67 	if (ioctl(cdfile, CDROMREADTOCHDR, &tochdr) == -1)
68 	{
69 		Con_Print("ioctl CDROMREADTOCHDR failed\n");
70 		return -1;
71 	}
72 
73 	if (tochdr.cdth_trk0 < 1)
74 	{
75 		Con_Print("CDAudio: no music tracks\n");
76 		return -1;
77 	}
78 
79 	return tochdr.cdth_trk1;
80 }
81 
82 
CDAudio_SysGetVolume(void)83 float CDAudio_SysGetVolume (void)
84 {
85 	struct cdrom_volctrl vol;
86 
87 	if (cdfile == -1)
88 		return -1.0f;
89 
90 	if (ioctl (cdfile, CDROMVOLREAD, &vol) == -1)
91 	{
92 		Con_Print("ioctl CDROMVOLREAD failed\n");
93 		return -1.0f;
94 	}
95 
96 	return (vol.channel0 + vol.channel1) / 2.0f / 255.0f;
97 }
98 
99 
CDAudio_SysSetVolume(float volume)100 void CDAudio_SysSetVolume (float volume)
101 {
102 	struct cdrom_volctrl vol;
103 
104 	if (cdfile == -1)
105 		return;
106 
107 	vol.channel0 = vol.channel1 = (__u8)(volume * 255);
108 	vol.channel2 = vol.channel3 = 0;
109 
110 	if (ioctl (cdfile, CDROMVOLCTRL, &vol) == -1)
111 		Con_Print("ioctl CDROMVOLCTRL failed\n");
112 }
113 
114 
CDAudio_SysPlay(int track)115 int CDAudio_SysPlay (int track)
116 {
117 	struct cdrom_tocentry entry;
118 	struct cdrom_ti ti;
119 
120 	if (cdfile == -1)
121 		return -1;
122 
123 	// don't try to play a non-audio track
124 	entry.cdte_track = track;
125 	entry.cdte_format = CDROM_MSF;
126 	if (ioctl(cdfile, CDROMREADTOCENTRY, &entry) == -1)
127 	{
128 		Con_Print("ioctl CDROMREADTOCENTRY failed\n");
129 		return -1;
130 	}
131 	if (entry.cdte_ctrl == CDROM_DATA_TRACK)
132 	{
133 		Con_Printf("CDAudio: track %i is not audio\n", track);
134 		return -1;
135 	}
136 
137 	if (cdPlaying)
138 		CDAudio_Stop();
139 
140 	ti.cdti_trk0 = track;
141 	ti.cdti_trk1 = track;
142 	ti.cdti_ind0 = 1;
143 	ti.cdti_ind1 = 99;
144 
145 	if (ioctl(cdfile, CDROMPLAYTRKIND, &ti) == -1)
146 	{
147 		Con_Print("ioctl CDROMPLAYTRKIND failed\n");
148 		return -1;
149 	}
150 
151 	if (ioctl(cdfile, CDROMRESUME) == -1)
152 	{
153 		Con_Print("ioctl CDROMRESUME failed\n");
154 		return -1;
155 	}
156 
157 	return 0;
158 }
159 
160 
CDAudio_SysStop(void)161 int CDAudio_SysStop (void)
162 {
163 	if (cdfile == -1)
164 		return -1;
165 
166 	if (ioctl(cdfile, CDROMSTOP) == -1)
167 	{
168 		Con_Printf("ioctl CDROMSTOP failed (%d)\n", errno);
169 		return -1;
170 	}
171 
172 	return 0;
173 }
174 
CDAudio_SysPause(void)175 int CDAudio_SysPause (void)
176 {
177 	if (cdfile == -1)
178 		return -1;
179 
180 	if (ioctl(cdfile, CDROMPAUSE) == -1)
181 	{
182 		Con_Print("ioctl CDROMPAUSE failed\n");
183 		return -1;
184 	}
185 
186 	return 0;
187 }
188 
189 
CDAudio_SysResume(void)190 int CDAudio_SysResume (void)
191 {
192 	if (cdfile == -1)
193 		return -1;
194 
195 	if (ioctl(cdfile, CDROMRESUME) == -1)
196 		Con_Print("ioctl CDROMRESUME failed\n");
197 
198 	return 0;
199 }
200 
CDAudio_SysUpdate(void)201 int CDAudio_SysUpdate (void)
202 {
203 	struct cdrom_subchnl subchnl;
204 	static time_t lastchk = 0;
205 
206 	if (cdPlaying && lastchk < time(NULL) && cdfile != -1)
207 	{
208 		lastchk = time(NULL) + 2; //two seconds between chks
209 		subchnl.cdsc_format = CDROM_MSF;
210 		if (ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1)
211 		{
212 			Con_Print("ioctl CDROMSUBCHNL failed\n");
213 			cdPlaying = false;
214 			return -1;
215 		}
216 		if (subchnl.cdsc_audiostatus != CDROM_AUDIO_PLAY &&
217 			subchnl.cdsc_audiostatus != CDROM_AUDIO_PAUSED)
218 		{
219 			cdPlaying = false;
220 			if (cdPlayLooping)
221 				CDAudio_Play(cdPlayTrack, true);
222 		}
223 		else
224 			cdPlayTrack = subchnl.cdsc_trk;
225 	}
226 
227 	return 0;
228 }
229 
CDAudio_SysInit(void)230 void CDAudio_SysInit (void)
231 {
232 	int i;
233 
234 // COMMANDLINEOPTION: Linux Sound: -cddev <devicepath> chooses which CD drive to use
235 	if ((i = COM_CheckParm("-cddev")) != 0 && i < com_argc - 1)
236 		strlcpy(cd_dev, com_argv[i + 1], sizeof(cd_dev));
237 }
238 
CDAudio_SysStartup(void)239 int CDAudio_SysStartup (void)
240 {
241 	if ((cdfile = open(cd_dev, O_RDONLY | O_NONBLOCK)) == -1)
242 	{
243 		Con_Printf("CDAudio_SysStartup: open of \"%s\" failed (%i)\n",
244 					cd_dev, errno);
245 		cdfile = -1;
246 		return -1;
247 	}
248 
249 	return 0;
250 }
251 
CDAudio_SysShutdown(void)252 void CDAudio_SysShutdown (void)
253 {
254 	close(cdfile);
255 	cdfile = -1;
256 }
257