File: cd_linux.c

package info (click to toggle)
darkplaces 0~20180412~beta1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,200 kB
  • sloc: ansic: 176,886; makefile: 485; pascal: 455; perl: 372; objc: 245; sh: 102
file content (256 lines) | stat: -rw-r--r-- 4,836 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
// Quake is a trademark of Id Software, Inc., (c) 1996 Id Software, Inc. All
// rights reserved.

// suggested by Zero_Dogg to fix a compile problem on Mandriva Linux
#include "quakedef.h"

#include <linux/cdrom.h>
#include <sys/ioctl.h>

#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>

#include "cdaudio.h"


static int cdfile = -1;
static char cd_dev[64] = "/dev/cdrom";


void CDAudio_SysEject (void)
{
	if (cdfile == -1)
		return;

	if (ioctl(cdfile, CDROMEJECT) == -1)
		Con_Print("ioctl CDROMEJECT failed\n");
}


void CDAudio_SysCloseDoor (void)
{
	if (cdfile == -1)
		return;

	if (ioctl(cdfile, CDROMCLOSETRAY) == -1)
		Con_Print("ioctl CDROMCLOSETRAY failed\n");
}

int CDAudio_SysGetAudioDiskInfo (void)
{
	struct cdrom_tochdr tochdr;

	if (cdfile == -1)
		return -1;

	if (ioctl(cdfile, CDROMREADTOCHDR, &tochdr) == -1)
	{
		Con_Print("ioctl CDROMREADTOCHDR failed\n");
		return -1;
	}

	if (tochdr.cdth_trk0 < 1)
	{
		Con_Print("CDAudio: no music tracks\n");
		return -1;
	}

	return tochdr.cdth_trk1;
}


float CDAudio_SysGetVolume (void)
{
	struct cdrom_volctrl vol;

	if (cdfile == -1)
		return -1.0f;

	if (ioctl (cdfile, CDROMVOLREAD, &vol) == -1)
	{
		Con_Print("ioctl CDROMVOLREAD failed\n");
		return -1.0f;
	}

	return (vol.channel0 + vol.channel1) / 2.0f / 255.0f;
}


void CDAudio_SysSetVolume (float volume)
{
	struct cdrom_volctrl vol;

	if (cdfile == -1)
		return;

	vol.channel0 = vol.channel1 = (__u8)(volume * 255);
	vol.channel2 = vol.channel3 = 0;

	if (ioctl (cdfile, CDROMVOLCTRL, &vol) == -1)
		Con_Print("ioctl CDROMVOLCTRL failed\n");
}


int CDAudio_SysPlay (int track)
{
	struct cdrom_tocentry entry;
	struct cdrom_ti ti;

	if (cdfile == -1)
		return -1;

	// don't try to play a non-audio track
	entry.cdte_track = track;
	entry.cdte_format = CDROM_MSF;
	if (ioctl(cdfile, CDROMREADTOCENTRY, &entry) == -1)
	{
		Con_Print("ioctl CDROMREADTOCENTRY failed\n");
		return -1;
	}
	if (entry.cdte_ctrl == CDROM_DATA_TRACK)
	{
		Con_Printf("CDAudio: track %i is not audio\n", track);
		return -1;
	}

	if (cdPlaying)
		CDAudio_Stop();

	ti.cdti_trk0 = track;
	ti.cdti_trk1 = track;
	ti.cdti_ind0 = 1;
	ti.cdti_ind1 = 99;

	if (ioctl(cdfile, CDROMPLAYTRKIND, &ti) == -1)
	{
		Con_Print("ioctl CDROMPLAYTRKIND failed\n");
		return -1;
	}

	if (ioctl(cdfile, CDROMRESUME) == -1)
	{
		Con_Print("ioctl CDROMRESUME failed\n");
		return -1;
	}

	return 0;
}


int CDAudio_SysStop (void)
{
	if (cdfile == -1)
		return -1;

	if (ioctl(cdfile, CDROMSTOP) == -1)
	{
		Con_Printf("ioctl CDROMSTOP failed (%d)\n", errno);
		return -1;
	}

	return 0;
}

int CDAudio_SysPause (void)
{
	if (cdfile == -1)
		return -1;

	if (ioctl(cdfile, CDROMPAUSE) == -1)
	{
		Con_Print("ioctl CDROMPAUSE failed\n");
		return -1;
	}

	return 0;
}


int CDAudio_SysResume (void)
{
	if (cdfile == -1)
		return -1;

	if (ioctl(cdfile, CDROMRESUME) == -1)
		Con_Print("ioctl CDROMRESUME failed\n");

	return 0;
}

int CDAudio_SysUpdate (void)
{
	struct cdrom_subchnl subchnl;
	static time_t lastchk = 0;

	if (cdPlaying && lastchk < time(NULL) && cdfile != -1)
	{
		lastchk = time(NULL) + 2; //two seconds between chks
		subchnl.cdsc_format = CDROM_MSF;
		if (ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1)
		{
			Con_Print("ioctl CDROMSUBCHNL failed\n");
			cdPlaying = false;
			return -1;
		}
		if (subchnl.cdsc_audiostatus != CDROM_AUDIO_PLAY &&
			subchnl.cdsc_audiostatus != CDROM_AUDIO_PAUSED)
		{
			cdPlaying = false;
			if (cdPlayLooping)
				CDAudio_Play(cdPlayTrack, true);
		}
		else
			cdPlayTrack = subchnl.cdsc_trk;
	}

	return 0;
}

void CDAudio_SysInit (void)
{
	int i;

// COMMANDLINEOPTION: Linux Sound: -cddev <devicepath> chooses which CD drive to use
	if ((i = COM_CheckParm("-cddev")) != 0 && i < com_argc - 1)
		strlcpy(cd_dev, com_argv[i + 1], sizeof(cd_dev));
}

int CDAudio_SysStartup (void)
{
	if ((cdfile = open(cd_dev, O_RDONLY | O_NONBLOCK)) == -1)
	{
		Con_Printf("CDAudio_SysStartup: open of \"%s\" failed (%i)\n",
					cd_dev, errno);
		cdfile = -1;
		return -1;
	}

	return 0;
}

void CDAudio_SysShutdown (void)
{
	close(cdfile);
	cdfile = -1;
}