2010-02-15 23:26:55 +00:00
|
|
|
/*
|
2010-02-16 12:51:25 +00:00
|
|
|
cd_sdl.c
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
Taken from the Twilight project with modifications
|
|
|
|
to make it work with Hexen II: Hammer of Thyrion.
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
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.
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
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.
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
See the GNU General Public License for more details.
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
51 Franklin St, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301 USA
|
2010-02-15 23:26:55 +00:00
|
|
|
*/
|
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
|
2012-08-16 04:51:41 +00:00
|
|
|
#if defined(SDL_FRAMEWORK) || defined(NO_SDL_CONFIG)
|
|
|
|
#include <SDL/SDL.h>
|
|
|
|
#else
|
2010-02-16 12:51:25 +00:00
|
|
|
#include "SDL.h"
|
2012-08-16 04:51:41 +00:00
|
|
|
#endif
|
2010-02-16 12:51:25 +00:00
|
|
|
|
|
|
|
#ifndef SDL_INIT_CDROM
|
|
|
|
|
|
|
|
/* SDL dropped support for
|
|
|
|
cd audio since v1.3.0 */
|
|
|
|
#warning SDL CDAudio support disabled
|
|
|
|
#include "cd_null.c"
|
|
|
|
|
|
|
|
#else /* SDL_INIT_CDROM */
|
|
|
|
|
2010-02-15 23:26:55 +00:00
|
|
|
#include "quakedef.h"
|
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
static qboolean cdValid = false;
|
|
|
|
static qboolean playing = false;
|
2014-08-02 07:51:24 +00:00
|
|
|
static qboolean active = false;
|
2010-02-16 12:51:25 +00:00
|
|
|
static qboolean wasPlaying = false;
|
|
|
|
static qboolean enabled = true;
|
|
|
|
static qboolean playLooping = false;
|
|
|
|
static byte remap[100];
|
|
|
|
static byte playTrack;
|
2011-10-30 16:56:03 +00:00
|
|
|
static double endOfTrack = -1.0, pausetime = -1.0;
|
2010-02-16 12:51:25 +00:00
|
|
|
static SDL_CD *cd_handle;
|
|
|
|
static int cd_dev = -1;
|
|
|
|
static float old_cdvolume;
|
|
|
|
static qboolean hw_vol_works = true;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
|
|
|
|
static void CDAudio_Eject(void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-16 12:51:25 +00:00
|
|
|
if (!cd_handle || !enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (SDL_CDEject(cd_handle) == -1)
|
|
|
|
Con_Printf ("Unable to eject CD-ROM: %s\n", SDL_GetError ());
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
static int CDAudio_GetAudioDiskInfo(void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-16 12:51:25 +00:00
|
|
|
cdValid = false;
|
|
|
|
|
|
|
|
if (!cd_handle)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ( ! CD_INDRIVE(SDL_CDStatus(cd_handle)) )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
cdValid = true;
|
|
|
|
|
|
|
|
return 0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2011-01-02 18:33:21 +00:00
|
|
|
int CDAudio_Play(byte track, qboolean looping)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2011-10-30 16:56:03 +00:00
|
|
|
int len_m, len_s, len_f;
|
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
if (!cd_handle || !enabled)
|
2011-01-02 18:33:21 +00:00
|
|
|
return -1;
|
2010-02-16 12:51:25 +00:00
|
|
|
|
|
|
|
if (!cdValid)
|
|
|
|
{
|
|
|
|
CDAudio_GetAudioDiskInfo();
|
|
|
|
if (!cdValid)
|
2011-01-02 18:33:21 +00:00
|
|
|
return -1;
|
2010-02-16 12:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
track = remap[track];
|
|
|
|
|
|
|
|
if (track < 1 || track > cd_handle->numtracks)
|
|
|
|
{
|
|
|
|
Con_Printf ("CDAudio_Play: Bad track number %d.\n", track);
|
2011-01-02 18:33:21 +00:00
|
|
|
return -1;
|
2010-02-16 12:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cd_handle->track[track-1].type == SDL_DATA_TRACK)
|
|
|
|
{
|
|
|
|
Con_Printf ("CDAudio_Play: track %d is not audio\n", track);
|
2011-01-02 18:33:21 +00:00
|
|
|
return -1;
|
2010-02-16 12:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (playing)
|
|
|
|
{
|
|
|
|
if (playTrack == track)
|
2011-01-02 18:33:21 +00:00
|
|
|
return 0;
|
2010-12-30 14:55:25 +00:00
|
|
|
CDAudio_Stop();
|
2010-02-16 12:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SDL_CDPlay(cd_handle, cd_handle->track[track-1].offset, cd_handle->track[track-1].length) == -1)
|
|
|
|
{
|
2011-10-30 16:56:03 +00:00
|
|
|
Con_Printf ("CDAudio_Play: Unable to play track %d: %s\n", track, SDL_GetError ());
|
2011-01-02 18:33:21 +00:00
|
|
|
return -1;
|
2010-02-16 12:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
playLooping = looping;
|
|
|
|
playTrack = track;
|
|
|
|
playing = true;
|
2014-08-02 07:51:24 +00:00
|
|
|
active = true;
|
2010-02-16 12:51:25 +00:00
|
|
|
|
2011-10-30 16:56:03 +00:00
|
|
|
FRAMES_TO_MSF(cd_handle->track[track-1].length, &len_m, &len_s, &len_f);
|
|
|
|
endOfTrack = realtime + ((double)len_m * 60.0) + (double)len_s + (double)len_f / (double)CD_FPS;
|
|
|
|
|
|
|
|
/* Add the pregap for the next track. This means that disc-at-once CDs
|
|
|
|
* won't loop smoothly, but they wouldn't anyway so it doesn't really
|
|
|
|
* matter. SDL doesn't give us pregap information anyway, so you'll
|
|
|
|
* just have to live with it. */
|
|
|
|
endOfTrack += 2.0;
|
|
|
|
pausetime = -1.0;
|
|
|
|
|
2010-12-30 14:55:25 +00:00
|
|
|
if (bgmvolume.value == 0) /* don't bother advancing */
|
2010-02-16 12:51:25 +00:00
|
|
|
CDAudio_Pause ();
|
2011-01-02 18:33:21 +00:00
|
|
|
|
|
|
|
return 0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 07:51:24 +00:00
|
|
|
/* We have two types of stop
|
|
|
|
*
|
|
|
|
* On many devices, CDROMSTOP
|
|
|
|
* ioctl causes any ioctls following immediately to
|
|
|
|
* fail for a considerable time.
|
|
|
|
* So avoid dead stops if playback may be resume shortly */
|
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
void CDAudio_Stop(void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-16 12:51:25 +00:00
|
|
|
if (!cd_handle || !enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!playing)
|
|
|
|
return;
|
|
|
|
|
2014-08-02 07:51:24 +00:00
|
|
|
|
|
|
|
if (SDL_CDPause(cd_handle) == -1)
|
|
|
|
Con_Printf ("CDAudio_Stop: Unable to pause CD-ROM (%s)\n", SDL_GetError());
|
|
|
|
|
|
|
|
wasPlaying = false;
|
|
|
|
playing = false;
|
|
|
|
pausetime = -1.0;
|
|
|
|
endOfTrack = -1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDAudio_ReallyStop(void)
|
|
|
|
{
|
|
|
|
if (!cd_handle || !enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!active)
|
|
|
|
return;
|
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
if (SDL_CDStop(cd_handle) == -1)
|
2014-08-02 07:51:24 +00:00
|
|
|
Con_Printf ("CDAudio_ReallyStop: Unable to stop CD-ROM (%s)\n", SDL_GetError());
|
2010-02-16 12:51:25 +00:00
|
|
|
|
|
|
|
wasPlaying = false;
|
|
|
|
playing = false;
|
2014-08-02 07:51:24 +00:00
|
|
|
active = false;
|
2011-10-30 16:56:03 +00:00
|
|
|
pausetime = -1.0;
|
|
|
|
endOfTrack = -1.0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
void CDAudio_Pause(void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-16 12:51:25 +00:00
|
|
|
if (!cd_handle || !enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!playing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (SDL_CDPause(cd_handle) == -1)
|
|
|
|
Con_Printf ("Unable to pause CD-ROM: %s\n", SDL_GetError());
|
|
|
|
|
|
|
|
wasPlaying = playing;
|
|
|
|
playing = false;
|
2011-10-30 16:56:03 +00:00
|
|
|
pausetime = realtime;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
void CDAudio_Resume(void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-16 12:51:25 +00:00
|
|
|
if (!cd_handle || !enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!cdValid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!wasPlaying)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (SDL_CDResume(cd_handle) == -1)
|
|
|
|
Con_Printf ("Unable to resume CD-ROM: %s\n", SDL_GetError());
|
|
|
|
playing = true;
|
2014-08-02 07:51:24 +00:00
|
|
|
active = true;
|
2011-10-30 16:56:03 +00:00
|
|
|
endOfTrack += realtime - pausetime;
|
|
|
|
pausetime = -1.0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
static void CD_f (void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2011-01-12 10:02:16 +00:00
|
|
|
const char *command;
|
2010-02-16 12:51:25 +00:00
|
|
|
int ret, n;
|
|
|
|
|
|
|
|
if (Cmd_Argc() < 2)
|
|
|
|
{
|
2011-01-12 10:02:16 +00:00
|
|
|
Con_Printf("commands:");
|
|
|
|
Con_Printf("on, off, reset, remap, \n");
|
|
|
|
Con_Printf("play, stop, loop, pause, resume\n");
|
|
|
|
Con_Printf("eject, info\n");
|
2010-02-16 12:51:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
command = Cmd_Argv (1);
|
|
|
|
|
2013-09-25 17:01:40 +00:00
|
|
|
if (q_strcasecmp(command, "on") == 0)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
|
|
|
enabled = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:01:40 +00:00
|
|
|
if (q_strcasecmp(command, "off") == 0)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
|
|
|
if (playing)
|
2014-08-02 07:51:24 +00:00
|
|
|
CDAudio_ReallyStop();
|
2010-02-16 12:51:25 +00:00
|
|
|
enabled = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:01:40 +00:00
|
|
|
if (q_strcasecmp(command, "reset") == 0)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
|
|
|
enabled = true;
|
|
|
|
if (playing)
|
2014-08-02 07:51:24 +00:00
|
|
|
CDAudio_ReallyStop();
|
2010-02-16 12:51:25 +00:00
|
|
|
for (n = 0; n < 100; n++)
|
|
|
|
remap[n] = n;
|
|
|
|
CDAudio_GetAudioDiskInfo();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:01:40 +00:00
|
|
|
if (q_strcasecmp(command, "remap") == 0)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
2010-12-30 14:55:25 +00:00
|
|
|
ret = Cmd_Argc() - 2;
|
2010-02-16 12:51:25 +00:00
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
for (n = 1; n < 100; n++)
|
|
|
|
if (remap[n] != n)
|
2010-12-30 14:55:25 +00:00
|
|
|
Con_Printf(" %u -> %u\n", n, remap[n]);
|
2010-02-16 12:51:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (n = 1; n <= ret; n++)
|
|
|
|
remap[n] = atoi(Cmd_Argv (n + 1));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cdValid)
|
|
|
|
{
|
2010-12-30 14:55:25 +00:00
|
|
|
CDAudio_GetAudioDiskInfo();
|
2010-02-16 12:51:25 +00:00
|
|
|
if (!cdValid)
|
|
|
|
{
|
|
|
|
Con_Printf("No CD in player.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:01:40 +00:00
|
|
|
if (q_strcasecmp(command, "play") == 0)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
2011-01-12 10:02:16 +00:00
|
|
|
CDAudio_Play((byte)atoi(Cmd_Argv (2)), false);
|
2010-02-16 12:51:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:01:40 +00:00
|
|
|
if (q_strcasecmp(command, "loop") == 0)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
2011-01-12 10:02:16 +00:00
|
|
|
CDAudio_Play((byte)atoi(Cmd_Argv (2)), true);
|
2010-02-16 12:51:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:01:40 +00:00
|
|
|
if (q_strcasecmp(command, "stop") == 0)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
|
|
|
CDAudio_Stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:01:40 +00:00
|
|
|
if (q_strcasecmp(command, "pause") == 0)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
|
|
|
CDAudio_Pause();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:01:40 +00:00
|
|
|
if (q_strcasecmp(command, "resume") == 0)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
|
|
|
CDAudio_Resume();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:01:40 +00:00
|
|
|
if (q_strcasecmp(command, "eject") == 0)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
2014-08-02 07:51:24 +00:00
|
|
|
if (playing || active)
|
|
|
|
CDAudio_ReallyStop();
|
2010-02-16 12:51:25 +00:00
|
|
|
CDAudio_Eject();
|
|
|
|
cdValid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:01:40 +00:00
|
|
|
if (q_strcasecmp(command, "info") == 0)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
|
|
|
int current_min, current_sec, current_frame;
|
|
|
|
int length_min, length_sec, length_frame;
|
|
|
|
|
|
|
|
Con_Printf ("%u tracks\n", cd_handle->numtracks);
|
|
|
|
|
|
|
|
if (playing)
|
2010-12-30 14:55:25 +00:00
|
|
|
Con_Printf("Currently %s track %u\n", playLooping ? "looping" : "playing", playTrack);
|
2010-02-16 12:51:25 +00:00
|
|
|
else if (wasPlaying)
|
2010-12-30 14:55:25 +00:00
|
|
|
Con_Printf("Paused %s track %u\n", playLooping ? "looping" : "playing", playTrack);
|
2010-02-16 12:51:25 +00:00
|
|
|
|
|
|
|
if (playing || wasPlaying)
|
|
|
|
{
|
|
|
|
SDL_CDStatus(cd_handle);
|
|
|
|
FRAMES_TO_MSF(cd_handle->cur_frame, ¤t_min, ¤t_sec, ¤t_frame);
|
|
|
|
FRAMES_TO_MSF(cd_handle->track[playTrack-1].length, &length_min, &length_sec, &length_frame);
|
|
|
|
|
|
|
|
Con_Printf ("Current position: %d:%02d.%02d (of %d:%02d.%02d)\n",
|
|
|
|
current_min, current_sec, current_frame * 60 / CD_FPS,
|
|
|
|
length_min, length_sec, length_frame * 60 / CD_FPS);
|
|
|
|
}
|
2010-12-30 14:55:25 +00:00
|
|
|
Con_Printf("Volume is %f\n", bgmvolume.value);
|
2010-02-16 12:51:25 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2014-08-02 07:51:24 +00:00
|
|
|
|
|
|
|
Con_Printf("cd: \"%s\" unknown command.\n",command);
|
|
|
|
return;
|
2010-02-16 12:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static qboolean CD_GetVolume (void *unused)
|
|
|
|
{
|
|
|
|
/* FIXME: write proper code in here when SDL
|
|
|
|
supports cdrom volume control some day. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static qboolean CD_SetVolume (void *unused)
|
|
|
|
{
|
|
|
|
/* FIXME: write proper code in here when SDL
|
|
|
|
supports cdrom volume control some day. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-30 14:55:25 +00:00
|
|
|
static qboolean CDAudio_SetVolume (float value)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
|
|
|
if (!cd_handle || !enabled)
|
|
|
|
return false;
|
|
|
|
|
2010-12-30 14:55:25 +00:00
|
|
|
old_cdvolume = value;
|
|
|
|
|
|
|
|
if (value == 0.0f)
|
|
|
|
CDAudio_Pause ();
|
|
|
|
else
|
|
|
|
CDAudio_Resume();
|
|
|
|
|
|
|
|
if (!hw_vol_works)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
2010-12-30 14:55:25 +00:00
|
|
|
return false;
|
2010-02-16 12:51:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-30 14:55:25 +00:00
|
|
|
/* FIXME: write proper code in here when SDL
|
|
|
|
supports cdrom volume control some day. */
|
|
|
|
return CD_SetVolume (NULL);
|
2010-02-16 12:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDAudio_Update(void)
|
|
|
|
{
|
|
|
|
CDstatus curstat;
|
2011-10-30 16:56:03 +00:00
|
|
|
/* static double lastchk;*/
|
2010-02-16 12:51:25 +00:00
|
|
|
|
|
|
|
if (!cd_handle || !enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (old_cdvolume != bgmvolume.value)
|
2010-12-30 14:55:25 +00:00
|
|
|
CDAudio_SetVolume (bgmvolume.value);
|
2010-02-16 12:51:25 +00:00
|
|
|
|
2011-10-30 16:56:03 +00:00
|
|
|
/* if (playing && realtime > lastchk)*/
|
|
|
|
if (playing && realtime > endOfTrack)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
2011-10-30 16:56:03 +00:00
|
|
|
/* lastchk = realtime + 2;*/ /* two seconds between chks */
|
2010-02-16 12:51:25 +00:00
|
|
|
curstat = SDL_CDStatus(cd_handle);
|
|
|
|
if (curstat != CD_PLAYING && curstat != CD_PAUSED)
|
|
|
|
{
|
2011-01-12 10:02:16 +00:00
|
|
|
playing = false;
|
2011-10-30 16:56:03 +00:00
|
|
|
endOfTrack = -1.0;
|
2011-01-12 10:02:16 +00:00
|
|
|
if (playLooping)
|
2010-02-16 12:51:25 +00:00
|
|
|
CDAudio_Play(playTrack, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-23 14:05:32 +00:00
|
|
|
static const char *get_cddev_arg (const char *arg)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
/* arg should be like "D:\", make sure it is so,
|
|
|
|
* but tolerate args like "D" or "D:", as well. */
|
|
|
|
static char drive[4];
|
|
|
|
if (!arg || ! *arg)
|
|
|
|
return NULL;
|
|
|
|
if (arg[1] != '\0')
|
|
|
|
{
|
|
|
|
if (arg[1] != ':')
|
|
|
|
return NULL;
|
|
|
|
if (arg[2] != '\0')
|
|
|
|
{
|
|
|
|
if (arg[2] != '\\' &&
|
|
|
|
arg[2] != '/')
|
|
|
|
return NULL;
|
|
|
|
if (arg[3] != '\0')
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*arg >= 'A' && *arg <= 'Z')
|
|
|
|
{
|
|
|
|
drive[0] = *arg;
|
|
|
|
drive[1] = ':';
|
|
|
|
drive[2] = '\\';
|
|
|
|
drive[3] = '\0';
|
|
|
|
return drive;
|
|
|
|
}
|
|
|
|
else if (*arg >= 'a' && *arg <= 'z')
|
|
|
|
{
|
|
|
|
/* make it uppercase for SDL */
|
2010-08-23 15:55:54 +00:00
|
|
|
drive[0] = *arg - ('a' - 'A');
|
2010-08-23 14:05:32 +00:00
|
|
|
drive[1] = ':';
|
|
|
|
drive[2] = '\\';
|
|
|
|
drive[3] = '\0';
|
|
|
|
return drive;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
#else
|
2010-08-23 14:32:34 +00:00
|
|
|
if (!arg || ! *arg)
|
|
|
|
return NULL;
|
2010-08-23 14:05:32 +00:00
|
|
|
return arg;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void export_cddev_arg (void)
|
|
|
|
{
|
|
|
|
/* Bad ugly hack to workaround SDL's cdrom device detection.
|
|
|
|
* not needed for windows due to the way SDL_cdrom works. */
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
int i = COM_CheckParm("-cddev");
|
|
|
|
if (i != 0 && i < com_argc - 1 && com_argv[i+1][0] != '\0')
|
|
|
|
{
|
2010-08-23 23:46:16 +00:00
|
|
|
static char arg[64];
|
2010-08-23 23:20:16 +00:00
|
|
|
q_snprintf(arg, sizeof(arg), "SDL_CDROM=%s", com_argv[i+1]);
|
2010-08-23 14:05:32 +00:00
|
|
|
putenv(arg);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
int CDAudio_Init(void)
|
|
|
|
{
|
2010-08-23 14:05:32 +00:00
|
|
|
int i, sdl_num_drives;
|
2010-02-16 12:51:25 +00:00
|
|
|
|
2010-08-29 12:55:41 +00:00
|
|
|
if (safemode || COM_CheckParm("-nocdaudio"))
|
2010-02-16 12:51:25 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-08-23 17:23:39 +00:00
|
|
|
export_cddev_arg();
|
2010-08-23 04:12:19 +00:00
|
|
|
|
2010-02-16 12:51:25 +00:00
|
|
|
if (SDL_InitSubSystem(SDL_INIT_CDROM) == -1)
|
|
|
|
{
|
|
|
|
Con_Printf("Couldn't init SDL cdrom: %s\n", SDL_GetError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sdl_num_drives = SDL_CDNumDrives ();
|
|
|
|
Con_Printf ("SDL detected %d CD-ROM drive%c\n", sdl_num_drives,
|
|
|
|
sdl_num_drives == 1 ? ' ' : 's');
|
|
|
|
|
|
|
|
if (sdl_num_drives < 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((i = COM_CheckParm("-cddev")) != 0 && i < com_argc - 1)
|
|
|
|
{
|
2010-08-23 14:05:32 +00:00
|
|
|
const char *userdev = get_cddev_arg(com_argv[i+1]);
|
|
|
|
if (!userdev)
|
|
|
|
{
|
|
|
|
Con_Printf("Invalid argument to -cddev\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < sdl_num_drives; i++)
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
2013-09-25 17:01:40 +00:00
|
|
|
if (!q_strcasecmp(SDL_CDName(i), userdev))
|
2010-02-16 12:51:25 +00:00
|
|
|
{
|
2010-08-23 14:05:32 +00:00
|
|
|
cd_dev = i;
|
2010-02-16 12:51:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cd_dev == -1)
|
|
|
|
{
|
2010-08-23 14:05:32 +00:00
|
|
|
Con_Printf("SDL couldn't find cdrom device %s\n", userdev);
|
2010-02-16 12:51:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cd_dev == -1)
|
2010-12-30 14:55:25 +00:00
|
|
|
cd_dev = 0; /* default drive */
|
2010-02-16 12:51:25 +00:00
|
|
|
|
|
|
|
cd_handle = SDL_CDOpen(cd_dev);
|
|
|
|
if (!cd_handle)
|
|
|
|
{
|
|
|
|
Con_Printf ("CDAudio_Init: Unable to open CD-ROM drive %s (%s)\n",
|
|
|
|
SDL_CDName(cd_dev), SDL_GetError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 100; i++)
|
|
|
|
remap[i] = i;
|
|
|
|
enabled = true;
|
|
|
|
old_cdvolume = bgmvolume.value;
|
|
|
|
|
|
|
|
Con_Printf("CDAudio initialized (SDL, using %s)\n", SDL_CDName(cd_dev));
|
|
|
|
|
|
|
|
if (CDAudio_GetAudioDiskInfo())
|
|
|
|
{
|
|
|
|
Con_Printf("CDAudio_Init: No CD in drive\n");
|
|
|
|
cdValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cmd_AddCommand ("cd", CD_f);
|
|
|
|
|
2010-12-30 14:55:25 +00:00
|
|
|
hw_vol_works = CD_GetVolume (NULL); /* no SDL support at present. */
|
2010-02-16 12:51:25 +00:00
|
|
|
if (hw_vol_works)
|
2010-12-30 14:55:25 +00:00
|
|
|
hw_vol_works = CDAudio_SetVolume (bgmvolume.value);
|
2010-02-16 12:51:25 +00:00
|
|
|
|
|
|
|
return 0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
2010-02-16 12:51:25 +00:00
|
|
|
|
|
|
|
void CDAudio_Shutdown(void)
|
|
|
|
{
|
|
|
|
if (!cd_handle)
|
|
|
|
return;
|
2014-08-02 07:51:24 +00:00
|
|
|
CDAudio_ReallyStop();
|
2010-12-30 14:55:25 +00:00
|
|
|
if (hw_vol_works)
|
|
|
|
CD_SetVolume (NULL); /* no SDL support at present. */
|
2010-02-16 12:51:25 +00:00
|
|
|
SDL_CDClose(cd_handle);
|
|
|
|
cd_handle = NULL;
|
|
|
|
cd_dev = -1;
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_CDROM);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* SDL_INIT_CDROM */
|
|
|
|
|