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