]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cd_linux.c
Fine-grained CD volume support for Linux. Note that it uses the CD player internal...
[xonotic/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         if (ioctl (cdfile, CDROMVOLCTRL, &vol) == -1)
106                 Con_DPrint("ioctl CDROMVOLCTRL failed\n");
107 }
108
109
110 int CDAudio_SysPlay (qbyte track)
111 {
112         struct cdrom_tocentry entry;
113         struct cdrom_ti ti;
114
115         if (cdfile == -1)
116                 return -1;
117
118         // don't try to play a non-audio track
119         entry.cdte_track = track;
120         entry.cdte_format = CDROM_MSF;
121         if (ioctl(cdfile, CDROMREADTOCENTRY, &entry) == -1)
122         {
123                 Con_DPrint("ioctl CDROMREADTOCENTRY failed\n");
124                 return -1;
125         }
126         if (entry.cdte_ctrl == CDROM_DATA_TRACK)
127         {
128                 Con_Printf("CDAudio: track %i is not audio\n", track);
129                 return -1;
130         }
131
132         if (cdPlaying)
133                 CDAudio_Stop();
134
135         ti.cdti_trk0 = track;
136         ti.cdti_trk1 = track;
137         ti.cdti_ind0 = 1;
138         ti.cdti_ind1 = 99;
139
140         if (ioctl(cdfile, CDROMPLAYTRKIND, &ti) == -1)
141         {
142                 Con_DPrint("ioctl CDROMPLAYTRKIND failed\n");
143                 return -1;
144         }
145
146         if (ioctl(cdfile, CDROMRESUME) == -1)
147         {
148                 Con_DPrint("ioctl CDROMRESUME failed\n");
149                 return -1;
150         }
151
152         return 0;
153 }
154
155
156 int CDAudio_SysStop (void)
157 {
158         if (cdfile == -1)
159                 return -1;
160
161         if (ioctl(cdfile, CDROMSTOP) == -1)
162         {
163                 Con_DPrintf("ioctl CDROMSTOP failed (%d)\n", errno);
164                 return -1;
165         }
166
167         return 0;
168 }
169
170 int CDAudio_SysPause (void)
171 {
172         if (cdfile == -1)
173                 return -1;
174
175         if (ioctl(cdfile, CDROMPAUSE) == -1)
176         {
177                 Con_DPrint("ioctl CDROMPAUSE failed\n");
178                 return -1;
179         }
180
181         return 0;
182 }
183
184
185 int CDAudio_SysResume (void)
186 {
187         if (cdfile == -1)
188                 return -1;
189
190         if (ioctl(cdfile, CDROMRESUME) == -1)
191                 Con_DPrint("ioctl CDROMRESUME failed\n");
192
193         return 0;
194 }
195
196 int CDAudio_SysUpdate (void)
197 {
198         struct cdrom_subchnl subchnl;
199         static time_t lastchk = 0;
200
201         if (cdPlaying && lastchk < time(NULL))
202         {
203                 lastchk = time(NULL) + 2; //two seconds between chks
204                 subchnl.cdsc_format = CDROM_MSF;
205                 if (ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1)
206                 {
207                         Con_DPrint("ioctl CDROMSUBCHNL failed\n");
208                         cdPlaying = false;
209                         return -1;
210                 }
211                 if (subchnl.cdsc_audiostatus != CDROM_AUDIO_PLAY &&
212                         subchnl.cdsc_audiostatus != CDROM_AUDIO_PAUSED)
213                 {
214                         cdPlaying = false;
215                         if (cdPlayLooping)
216                                 CDAudio_Play(cdPlayTrack, true);
217                 }
218                 else
219                         cdPlayTrack = subchnl.cdsc_trk;
220         }
221
222         return 0;
223 }
224
225 void CDAudio_SysInit (void)
226 {
227         int i;
228
229         if ((i = COM_CheckParm("-cddev")) != 0 && i < com_argc - 1)
230                 strlcpy(cd_dev, com_argv[i + 1], sizeof(cd_dev));
231 }
232
233 int CDAudio_SysStartup (void)
234 {
235         if ((cdfile = open(cd_dev, O_RDONLY | O_NONBLOCK)) == -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 }