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