]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_bsd.c
audit of checkstuck/unstickentity code and relates stuff, this seems to fix the falli...
[divverent/darkplaces.git] / cd_bsd.c
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 <sys/types.h>
22 #include <sys/cdio.h>
23 #include <sys/ioctl.h>
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <paths.h>
28 #include <unistd.h>
29 #include <time.h>
30 #ifndef __FreeBSD__
31 # include <util.h>
32 #endif
33
34 #include "quakedef.h"
35 #include "cdaudio.h"
36
37
38 #ifndef __FreeBSD__
39 # define DEFAULT_CD_DEVICE _PATH_DEV "cd0"
40 #else
41 # define DEFAULT_CD_DEVICE "/dev/acd0c"
42 #endif
43
44 static int cdfile = -1;
45 static char cd_dev[64] = DEFAULT_CD_DEVICE;
46
47
48 void CDAudio_SysEject (void)
49 {
50         if (cdfile == -1)
51                 return;
52
53         ioctl(cdfile, CDIOCALLOW);
54         if (ioctl(cdfile, CDIOCEJECT) == -1)
55                 Con_Print("ioctl CDIOCEJECT failed\n");
56 }
57
58
59 void CDAudio_SysCloseDoor (void)
60 {
61         if (cdfile == -1)
62                 return;
63
64         ioctl(cdfile, CDIOCALLOW);
65         if (ioctl(cdfile, CDIOCCLOSE) == -1)
66                 Con_Print("ioctl CDIOCCLOSE failed\n");
67 }
68
69 int CDAudio_SysGetAudioDiskInfo (void)
70 {
71         struct ioc_toc_header tochdr;
72
73         if (cdfile == -1)
74                 return -1;
75
76         if (ioctl(cdfile, CDIOREADTOCHEADER, &tochdr) == -1)
77         {
78                 Con_Print("ioctl CDIOREADTOCHEADER failed\n");
79                 return -1;
80         }
81
82         if (tochdr.starting_track < 1)
83         {
84                 Con_Print("CDAudio: no music tracks\n");
85                 return -1;
86         }
87
88         return tochdr.ending_track;
89 }
90
91
92 float CDAudio_SysGetVolume (void)
93 {
94         struct ioc_vol vol;
95
96         if (cdfile == -1)
97                 return -1.0f;
98
99         if (ioctl (cdfile, CDIOCGETVOL, &vol) == -1)
100         {
101                 Con_Print("ioctl CDIOCGETVOL failed\n");
102                 return -1.0f;
103         }
104
105         return (vol.vol[0] + vol.vol[1]) / 2.0f / 255.0f;
106 }
107
108
109 void CDAudio_SysSetVolume (float volume)
110 {
111         struct ioc_vol vol;
112
113         if (cdfile == -1)
114                 return;
115
116         vol.vol[0] = vol.vol[1] = volume * 255;
117         vol.vol[2] = vol.vol[3] = 0;
118
119         if (ioctl (cdfile, CDIOCSETVOL, &vol) == -1)
120                 Con_Printf ("ioctl CDIOCSETVOL failed\n");
121 }
122
123
124 int CDAudio_SysPlay (unsigned char track)
125 {
126         struct ioc_read_toc_entry rte;
127         struct cd_toc_entry entry;
128         struct ioc_play_track ti;
129
130         if (cdfile == -1)
131                 return -1;
132
133         // don't try to play a non-audio track
134         rte.address_format = CD_MSF_FORMAT;
135         rte.starting_track = track;
136         rte.data_len = sizeof(entry);
137         rte.data = &entry;
138         if (ioctl(cdfile, CDIOREADTOCENTRYS, &rte) == -1)
139         {
140                 Con_Print("ioctl CDIOREADTOCENTRYS failed\n");
141                 return -1;
142         }
143         if (entry.control & 4)  // if it's a data track
144         {
145                 Con_Printf("CDAudio: track %i is not audio\n", track);
146                 return -1;
147         }
148
149         if (cdPlaying)
150                 CDAudio_Stop();
151
152         ti.start_track = track;
153         ti.end_track = track;
154         ti.start_index = 1;
155         ti.end_index = 99;
156
157         if (ioctl(cdfile, CDIOCPLAYTRACKS, &ti) == -1)
158         {
159                 Con_Print("ioctl CDIOCPLAYTRACKS failed\n");
160                 return -1;
161         }
162
163         if (ioctl(cdfile, CDIOCRESUME) == -1)
164         {
165                 Con_Print("ioctl CDIOCRESUME failed\n");
166                 return -1;
167         }
168
169         return 0;
170 }
171
172
173 int CDAudio_SysStop (void)
174 {
175         if (cdfile == -1)
176                 return -1;
177
178         if (ioctl(cdfile, CDIOCSTOP) == -1)
179         {
180                 Con_Printf("ioctl CDIOCSTOP failed (%d)\n", errno);
181                 return -1;
182         }
183         ioctl(cdfile, CDIOCALLOW);
184
185         return 0;
186 }
187
188 int CDAudio_SysPause (void)
189 {
190         if (cdfile == -1)
191                 return -1;
192
193         if (ioctl(cdfile, CDIOCPAUSE) == -1)
194         {
195                 Con_Print("ioctl CDIOCPAUSE failed\n");
196                 return -1;
197         }
198
199         return 0;
200 }
201
202
203 int CDAudio_SysResume (void)
204 {
205         if (cdfile == -1)
206                 return -1;
207
208         if (ioctl(cdfile, CDIOCRESUME) == -1)
209                 Con_Print("ioctl CDIOCRESUME failed\n");
210
211         return 0;
212 }
213
214 int CDAudio_SysUpdate (void)
215 {
216         static time_t lastchk = 0;
217         struct ioc_read_subchannel subchnl;
218         struct cd_sub_channel_info data;
219
220         if (cdPlaying && lastchk < time(NULL))
221         {
222                 lastchk = time(NULL) + 2; //two seconds between chks
223
224                 bzero(&subchnl, sizeof(subchnl));
225                 subchnl.data = &data;
226                 subchnl.data_len = sizeof(data);
227                 subchnl.address_format = CD_MSF_FORMAT;
228                 subchnl.data_format = CD_CURRENT_POSITION;
229
230                 if (ioctl(cdfile, CDIOCREADSUBCHANNEL, &subchnl) == -1)
231                 {
232                         Con_Print("ioctl CDIOCREADSUBCHANNEL failed\n");
233                         cdPlaying = false;
234                         return -1;
235                 }
236                 if (data.header.audio_status != CD_AS_PLAY_IN_PROGRESS &&
237                         data.header.audio_status != CD_AS_PLAY_PAUSED)
238                 {
239                         cdPlaying = false;
240                         if (cdPlayLooping)
241                                 CDAudio_Play(cdPlayTrack, true);
242                 }
243                 else
244                         cdPlayTrack = data.what.position.track_number;
245         }
246
247         return 0;
248 }
249
250 void CDAudio_SysInit (void)
251 {
252         int i;
253
254 // COMMANDLINEOPTION: BSD Sound: -cddev <devicepath> chooses which CD drive to use
255         if ((i = COM_CheckParm("-cddev")) != 0 && i < com_argc - 1)
256                 strlcpy(cd_dev, com_argv[i + 1], sizeof(cd_dev));
257 }
258
259 int CDAudio_SysStartup (void)
260 {
261 #ifndef __FreeBSD__
262         char buff [80];
263
264         if ((cdfile = opendisk(cd_dev, O_RDONLY, buff, sizeof(buff), 0)) == -1)
265 #else
266         if ((cdfile = open(cd_dev, O_RDONLY)) < 0)
267 #endif
268         {
269                 Con_Printf("CDAudio_SysStartup: open of \"%s\" failed (%i)\n",
270                                         cd_dev, errno);
271                 cdfile = -1;
272                 return -1;
273         }
274
275         return 0;
276 }
277
278 void CDAudio_SysShutdown (void)
279 {
280         close(cdfile);
281         cdfile = -1;
282 }