]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cd_shared.c
fix a number of char types that should be int, to make them immune to
[xonotic/darkplaces.git] / cd_shared.c
index e56fa388911f8c616c0a4ad2d410855e8d8aac82..9ab0210a531fdd4ffa692a32e0fadc4e677edc02 100644 (file)
@@ -32,7 +32,7 @@ extern void CDAudio_SysCloseDoor (void);
 extern int CDAudio_SysGetAudioDiskInfo (void);
 extern float CDAudio_SysGetVolume (void);
 extern void CDAudio_SysSetVolume (float volume);
-extern int CDAudio_SysPlay (unsigned char track);
+extern int CDAudio_SysPlay (int track);
 extern int CDAudio_SysStop (void);
 extern int CDAudio_SysPause (void);
 extern int CDAudio_SysResume (void);
@@ -43,12 +43,14 @@ extern void CDAudio_SysShutdown (void);
 
 // used by menu to ghost CD audio slider
 cvar_t cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0","indicates if CD Audio system is active"};
+cvar_t cdaudio = {CVAR_SAVE,"cdaudio","1","CD playing mode (0 = never access CD drive, 1 = play CD tracks if no replacement available, 2 = play fake tracks if no CD track available, 3 = play only real CD tracks, 4 = play real CD tracks even instead of named fake tracks)"};
 
 static qboolean wasPlaying = false;
 static qboolean initialized = false;
 static qboolean enabled = false;
 static float cdvolume;
-static unsigned char remap[MAXTRACKS];
+typedef char filename_t[MAX_QPATH];
+static filename_t remap[MAXTRACKS];
 static unsigned char maxTrack;
 static int faketrack = -1;
 
@@ -66,6 +68,9 @@ static void CDAudio_Eject (void)
 {
        if (!enabled)
                return;
+       
+       if(cdaudio.integer == 0)
+               return;
 
        CDAudio_SysEject();
 }
@@ -76,6 +81,9 @@ static void CDAudio_CloseDoor (void)
        if (!enabled)
                return;
 
+       if(cdaudio.integer == 0)
+               return;
+
        CDAudio_SysCloseDoor();
 }
 
@@ -85,6 +93,9 @@ static int CDAudio_GetAudioDiskInfo (void)
 
        cdValid = false;
 
+       if(cdaudio.integer == 0)
+               return -1;
+
        ret = CDAudio_SysGetAudioDiskInfo();
        if (ret < 1)
                return -1;
@@ -95,10 +106,45 @@ static int CDAudio_GetAudioDiskInfo (void)
        return 0;
 }
 
+qboolean CDAudio_Play_real (int track, qboolean looping, qboolean complain)
+{
+       if(track < 1)
+       {
+               if(complain)
+                       Con_Print("Could not load BGM track.\n");
+               return false;
+       }
+
+       if (!cdValid)
+       {
+               CDAudio_GetAudioDiskInfo();
+               if (!cdValid)
+               {
+                       if(complain)
+                               Con_Print ("No CD in player.\n");
+                       return false;
+               }
+       }
+
+       if (track > maxTrack)
+       {
+               if(complain)
+                       Con_Printf("CDAudio: Bad track number %u.\n", track);
+               return false;
+       }
+
+       if (CDAudio_SysPlay(track) == -1)
+               return false;
+
+       if(cdaudio.integer != 3 || developer.integer)
+               Con_Printf ("CD track %u playing...\n", track);
+
+       return true;
+}
 
 void CDAudio_Play_byName (const char *trackname, qboolean looping)
 {
-       unsigned char track;
+       unsigned int track;
        sfx_t* sfx;
 
        Host_StartVideo();
@@ -109,7 +155,14 @@ void CDAudio_Play_byName (const char *trackname, qboolean looping)
        if(strspn(trackname, "0123456789") == strlen(trackname))
        {
                track = (unsigned char) atoi(trackname);
-               track = remap[track];
+               if(track > 0 && track < MAXTRACKS)
+                               if(*remap[track])
+                                       trackname = remap[track];
+       }
+
+       if(strspn(trackname, "0123456789") == strlen(trackname))
+       {
+               track = (unsigned char) atoi(trackname);
                if (track < 1)
                {
                        Con_Printf("CDAudio: Bad track number %u.\n", track);
@@ -123,6 +176,40 @@ void CDAudio_Play_byName (const char *trackname, qboolean looping)
                return;
        CDAudio_Stop ();
 
+       if(track >= 1)
+       {
+               if(cdaudio.integer == 3) // only play real CD tracks at all
+               {
+                       if(CDAudio_Play_real(track, looping, true))
+                               goto success;
+                       return;
+               }
+
+               if(cdaudio.integer == 2) // prefer real CD track over fake
+               {
+                       if(CDAudio_Play_real(track, looping, false))
+                               goto success;
+               }
+       }
+
+       if(cdaudio.integer == 4) // only play real CD tracks, EVEN instead of fake tracks!
+       {
+               if(CDAudio_Play_real(track, looping, false))
+                       goto success;
+               
+               if(cdValid && maxTrack > 0)
+               {
+                       track = 1 + (rand() % maxTrack);
+                       if(CDAudio_Play_real(track, looping, true))
+                               goto success;
+               }
+               else
+               {
+                       Con_Print ("No CD in player.\n");
+               }
+               return;
+       }
+
        // Try playing a fake track (sound file) first
        if(track >= 1)
        {
@@ -139,6 +226,10 @@ void CDAudio_Play_byName (const char *trackname, qboolean looping)
                sfx = S_PrecacheSound (va("cdtracks/%s.wav", trackname), false, false);
                if (sfx == NULL || !S_IsSoundPrecached (sfx))
                        sfx = S_PrecacheSound (va("cdtracks/%s", trackname), false, false);
+               if (sfx == NULL || !S_IsSoundPrecached (sfx))
+                       sfx = S_PrecacheSound (va("%s.wav", trackname), false, false);
+               if (sfx == NULL || !S_IsSoundPrecached (sfx))
+                       sfx = S_PrecacheSound (va("%s", trackname), false, false);
        }
        if (sfx != NULL)
        {
@@ -149,7 +240,10 @@ void CDAudio_Play_byName (const char *trackname, qboolean looping)
                                S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
                        S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
                        if(track >= 1)
-                               Con_DPrintf ("Fake CD track %u playing...\n", track);
+                       {
+                               if(cdaudio.integer != 0 || developer.integer) // we don't need these messages if only fake tracks can be played anyway
+                                       Con_Printf ("Fake CD track %u playing...\n", track);
+                       }
                        else
                                Con_DPrintf ("BGM track %s playing...\n", trackname);
                }
@@ -158,32 +252,19 @@ void CDAudio_Play_byName (const char *trackname, qboolean looping)
        // If we can't play a fake CD track, try the real one
        if (faketrack == -1)
        {
-               if(track < 1)
+               if(cdaudio.integer == 0 || track < 1)
                {
                        Con_Print("Could not load BGM track.\n");
                        return;
                }
-       
-               if (!cdValid)
+               else
                {
-                       CDAudio_GetAudioDiskInfo();
-                       if (!cdValid)
-                       {
-                               Con_Print ("No CD in player.\n");
+                       if(!CDAudio_Play_real(track, looping, true))
                                return;
-                       }
-               }
-
-               if (track > maxTrack)
-               {
-                       Con_Printf("CDAudio: Bad track number %u.\n", track);
-                       return;
                }
-
-               if (CDAudio_SysPlay(track) == -1)
-                       return;
        }
 
+success:
        cdPlayLooping = looping;
        cdPlayTrack = track;
        cdPlaying = true;
@@ -192,7 +273,7 @@ void CDAudio_Play_byName (const char *trackname, qboolean looping)
                CDAudio_Pause ();
 }
 
-void CDAudio_Play (unsigned char track, qboolean looping)
+void CDAudio_Play (int track, qboolean looping)
 {
        char buf[20];
        dpsnprintf(buf, sizeof(buf), "%d", (int) track);
@@ -201,15 +282,15 @@ void CDAudio_Play (unsigned char track, qboolean looping)
 
 void CDAudio_Stop (void)
 {
-       if (!enabled || !cdPlaying)
+       if (!enabled)
                return;
 
        if (faketrack != -1)
        {
-               S_StopChannel (faketrack);
+               S_StopChannel (faketrack, true);
                faketrack = -1;
        }
-       else if (CDAudio_SysStop() == -1)
+       else if (cdPlaying && (CDAudio_SysStop() == -1))
                return;
 
        wasPlaying = false;
@@ -249,13 +330,14 @@ static void CD_f (void)
        int ret;
        int n;
 
-       Host_StartVideo();
-
        if (Cmd_Argc() < 2)
                return;
 
        command = Cmd_Argv (1);
 
+       if (strcasecmp(command, "remap") != 0)
+               Host_StartVideo();
+
        if (strcasecmp(command, "on") == 0)
        {
                enabled = true;
@@ -276,7 +358,7 @@ static void CD_f (void)
                if (cdPlaying)
                        CDAudio_Stop();
                for (n = 0; n < MAXTRACKS; n++)
-                       remap[n] = n;
+                       *remap[n] = 0; // empty string, that is, unremapped
                CDAudio_GetAudioDiskInfo();
                return;
        }
@@ -287,12 +369,12 @@ static void CD_f (void)
                if (ret <= 0)
                {
                        for (n = 1; n < MAXTRACKS; n++)
-                               if (remap[n] != n)
-                                       Con_Printf("  %u -> %u\n", n, remap[n]);
+                               if (*remap[n])
+                                       Con_Printf("  %u -> %s\n", n, remap[n]);
                        return;
                }
                for (n = 1; n <= ret; n++)
-                       remap[n] = atoi(Cmd_Argv (n+1));
+                       strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
                return;
        }
 
@@ -388,7 +470,8 @@ void CDAudio_SetVolume (float newvol)
 
                if (faketrack != -1)
                        S_SetChannelVolume (faketrack, newvol);
-               CDAudio_SysSetVolume (newvol);
+               else
+                       CDAudio_SysSetVolume (newvol);
        }
 
        cdvolume = newvol;
@@ -401,7 +484,7 @@ void CDAudio_Update (void)
 
        CDAudio_SetVolume (bgmvolume.value);
 
-       if (faketrack == -1)
+       if (faketrack == -1 && cdaudio.integer != 0)
                CDAudio_SysUpdate();
 }
 
@@ -419,8 +502,9 @@ int CDAudio_Init (void)
        CDAudio_SysInit();
 
        for (i = 0; i < MAXTRACKS; i++)
-               remap[i] = i;
+               *remap[i] = 0;
 
+       Cvar_RegisterVariable(&cdaudio);
        Cvar_RegisterVariable(&cdaudioinitialized);
        Cvar_SetValueQuick(&cdaudioinitialized, true);
        enabled = true;