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