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