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