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