]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cd_linux.c
Moved an extern to the proper header.
[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_DPrintf("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_DPrintf("ioctl CDROMCLOSETRAY failed\n");
55 }
56
57 int CDAudio_SysGetAudioDiskInfo (void)
58 {
59         struct cdrom_tochdr tochdr;
60
61         if (ioctl(cdfile, CDROMREADTOCHDR, &tochdr) == -1)
62         {
63                 Con_DPrintf("ioctl CDROMREADTOCHDR failed\n");
64                 return -1;
65         }
66
67         if (tochdr.cdth_trk0 < 1)
68         {
69                 Con_DPrintf("CDAudio: no music tracks\n");
70                 return -1;
71         }
72
73         return tochdr.cdth_trk1;
74 }
75
76
77 int CDAudio_SysPlay (qbyte track)
78 {
79         struct cdrom_tocentry entry;
80         struct cdrom_ti ti;
81
82         if (cdfile == -1)
83                 return -1;
84
85         // don't try to play a non-audio track
86         entry.cdte_track = track;
87         entry.cdte_format = CDROM_MSF;
88         if (ioctl(cdfile, CDROMREADTOCENTRY, &entry) == -1)
89         {
90                 Con_DPrintf("ioctl CDROMREADTOCENTRY failed\n");
91                 return -1;
92         }
93         if (entry.cdte_ctrl == CDROM_DATA_TRACK)
94         {
95                 Con_Printf("CDAudio: track %i is not audio\n", track);
96                 return -1;
97         }
98
99         if (cdPlaying)
100                 CDAudio_Stop();
101
102         ti.cdti_trk0 = track;
103         ti.cdti_trk1 = track;
104         ti.cdti_ind0 = 1;
105         ti.cdti_ind1 = 99;
106
107         if (ioctl(cdfile, CDROMPLAYTRKIND, &ti) == -1)
108         {
109                 Con_DPrintf("ioctl CDROMPLAYTRKIND failed\n");
110                 return -1;
111         }
112
113         if (ioctl(cdfile, CDROMRESUME) == -1)
114         {
115                 Con_DPrintf("ioctl CDROMRESUME failed\n");
116                 return -1;
117         }
118
119         return 0;
120 }
121
122
123 int CDAudio_SysStop (void)
124 {
125         if (cdfile == -1)
126                 return -1;
127
128         if (ioctl(cdfile, CDROMSTOP) == -1)
129         {
130                 Con_DPrintf("ioctl CDROMSTOP failed (%d)\n", errno);
131                 return -1;
132         }
133
134         return 0;
135 }
136
137 int CDAudio_SysPause (void)
138 {
139         if (cdfile == -1)
140                 return -1;
141
142         if (ioctl(cdfile, CDROMPAUSE) == -1)
143         {
144                 Con_DPrintf("ioctl CDROMPAUSE failed\n");
145                 return -1;
146         }
147
148         return 0;
149 }
150
151
152 int CDAudio_SysResume (void)
153 {
154         if (cdfile == -1)
155                 return -1;
156
157         if (ioctl(cdfile, CDROMRESUME) == -1)
158                 Con_DPrintf("ioctl CDROMRESUME failed\n");
159
160         return 0;
161 }
162
163 int CDAudio_SysUpdate (void)
164 {
165         struct cdrom_subchnl subchnl;
166         static time_t lastchk = 0;
167
168         if (cdPlaying && lastchk < time(NULL))
169         {
170                 lastchk = time(NULL) + 2; //two seconds between chks
171                 subchnl.cdsc_format = CDROM_MSF;
172                 if (ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1)
173                 {
174                         Con_DPrintf("ioctl CDROMSUBCHNL failed\n");
175                         cdPlaying = false;
176                         return -1;
177                 }
178                 if (subchnl.cdsc_audiostatus != CDROM_AUDIO_PLAY &&
179                         subchnl.cdsc_audiostatus != CDROM_AUDIO_PAUSED)
180                 {
181                         cdPlaying = false;
182                         if (cdPlayLooping)
183                                 CDAudio_Play(cdPlayTrack, true);
184                 }
185                 else
186                         cdPlayTrack = subchnl.cdsc_trk;
187         }
188
189         return 0;
190 }
191
192 void CDAudio_SysInit (void)
193 {
194         int i;
195
196         if ((i = COM_CheckParm("-cddev")) != 0 && i < com_argc - 1)
197                 strlcpy(cd_dev, com_argv[i + 1], sizeof(cd_dev));
198 }
199
200 int CDAudio_SysStartup (void)
201 {
202         if ((cdfile = open(cd_dev, O_RDONLY)) == -1)
203         {
204                 Con_DPrintf("CDAudio_SysStartup: open of \"%s\" failed (%i)\n",
205                                         cd_dev, errno);
206                 cdfile = -1;
207                 return -1;
208         }
209
210         return 0;
211 }
212
213 void CDAudio_SysShutdown (void)
214 {
215         close(cdfile);
216         cdfile = -1;
217 }