From 22cf0531e1b5e6da6b6b8debeae46c3d5ddf0b2e Mon Sep 17 00:00:00 2001
From: nehalmistry <>
Date: Fri, 6 Dec 2002 07:24:31 +0000
Subject: [PATCH] more updates for CDDA mode

---
 src/include/sound_server.h | 13 ++++++--
 src/sound/Module.make      |  4 +--
 src/sound/cdda.cpp         | 65 ++++++++++++++++++++++++++++++++++++++
 src/sound/music.cpp        | 54 ++++++++++++++-----------------
 src/stratagus/mainloop.cpp |  6 ++--
 5 files changed, 103 insertions(+), 39 deletions(-)
 create mode 100644 src/sound/cdda.cpp

diff --git a/src/include/sound_server.h b/src/include/sound_server.h
index 2ba84bc67..2b9ef8f6c 100644
--- a/src/include/sound_server.h
+++ b/src/include/sound_server.h
@@ -47,9 +47,9 @@ extern sem_t SoundThreadChannelSemaphore;
 #elif defined(USE_LIBCDA)
 #include "libcda.h"
 #elif defined(USE_CDDA)
-#include <cdda_interface.h>
-#include <cdda_paranoia.h>
-#include <utils.h>
+#include <linux/cdrom.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
 #endif
 
 /*----------------------------------------------------------------------------
@@ -249,6 +249,10 @@ extern int NumCDTracks;
 #elif defined(USE_CDDA)
 // FIXME: fill up
 extern int NumCDTracks;
+extern int CDDrive;
+extern struct cdrom_tochdr CDchdr;
+extern struct cdrom_tocentry CDtocentry[64];
+extern struct cdrom_read_audio CDdata;
 #endif
 
 extern Sample* MusicSample;		/// Music samples
@@ -261,6 +265,9 @@ extern Sample* LoadFlac(const char* name,int flags);	/// Load a flac file
 extern Sample* LoadWav(const char* name,int flags);	/// Load a wav file
 extern Sample* LoadOgg(const char* name,int flags);	/// Load an ogg file
 extern Sample* LoadMp3(const char* name,int flags);	/// Load a mp3 file
+#ifdef USE_CDDA
+extern Sample* LoadCD(const char* name,int flags);	/// Load a cd track
+#endif
 
     ///	Register a sound (can be a simple sound or a group)
 extern SoundId RegisterSound(char* file[],unsigned number);
diff --git a/src/sound/Module.make b/src/sound/Module.make
index 41404654c..f3049ca16 100644
--- a/src/sound/Module.make
+++ b/src/sound/Module.make
@@ -1,3 +1,3 @@
-SRC += src/sound/arts_audio.c src/sound/ccl_sound.c src/sound/flac.c src/sound/libcda.c src/sound/mad.c src/sound/music.c src/sound/ogg.c src/sound/oss_audio.c src/sound/sdl_audio.c src/sound/sound.c src/sound/sound_id.c src/sound/sound_server.c src/sound/unitsound.c src/sound/wav.c 
+SRC += src/sound/arts_audio.c src/sound/ccl_sound.c src/sound/flac.c src/sound/libcda.c src/sound/mad.c src/sound/music.c src/sound/ogg.c src/sound/oss_audio.c src/sound/sdl_audio.c src/sound/sound.c src/sound/sound_id.c src/sound/sound_server.c src/sound/unitsound.c src/sound/wav.c src/sound/cdda.c
 HDRS +=
-OBJ += src/sound/$(OBJDIR)/arts_audio.o src/sound/$(OBJDIR)/ccl_sound.o src/sound/$(OBJDIR)/flac.o src/sound/$(OBJDIR)/libcda.o src/sound/$(OBJDIR)/mad.o src/sound/$(OBJDIR)/music.o src/sound/$(OBJDIR)/ogg.o src/sound/$(OBJDIR)/oss_audio.o src/sound/$(OBJDIR)/sdl_audio.o src/sound/$(OBJDIR)/sound.o src/sound/$(OBJDIR)/sound_id.o src/sound/$(OBJDIR)/sound_server.o src/sound/$(OBJDIR)/unitsound.o src/sound/$(OBJDIR)/wav.o 
+OBJ += src/sound/$(OBJDIR)/arts_audio.o src/sound/$(OBJDIR)/ccl_sound.o src/sound/$(OBJDIR)/flac.o src/sound/$(OBJDIR)/libcda.o src/sound/$(OBJDIR)/mad.o src/sound/$(OBJDIR)/music.o src/sound/$(OBJDIR)/ogg.o src/sound/$(OBJDIR)/oss_audio.o src/sound/$(OBJDIR)/sdl_audio.o src/sound/$(OBJDIR)/sound.o src/sound/$(OBJDIR)/sound_id.o src/sound/$(OBJDIR)/sound_server.o src/sound/$(OBJDIR)/unitsound.o src/sound/$(OBJDIR)/wav.o src/sound/$(OBJDIR)/cdda.c
diff --git a/src/sound/cdda.cpp b/src/sound/cdda.cpp
new file mode 100644
index 000000000..35d87e108
--- /dev/null
+++ b/src/sound/cdda.cpp
@@ -0,0 +1,65 @@
+//   ___________		     _________		      _____  __
+//   \_	  _____/______   ____   ____ \_   ___ \____________ _/ ____\/  |_
+//    |    __) \_  __ \_/ __ \_/ __ \/    \  \/\_  __ \__  \\   __\\   __\ 
+//    |     \   |  | \/\  ___/\  ___/\     \____|  | \// __ \|  |   |  |
+//    \___  /   |__|    \___  >\___  >\______  /|__|  (____  /__|   |__|
+//	  \/		    \/	   \/	     \/		   \/
+//  ______________________                           ______________________
+//			  T H E   W A R   B E G I N S
+//	   FreeCraft - A free fantasy real time strategy game engine
+//
+/**@name wav.c			-	wav support */
+//
+//	(c) Copyright 2002 by Lutz Sammer and Fabrice Rossi
+//
+//	FreeCraft 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; only version 2 of the License.
+//
+//	FreeCraft 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.
+//
+//	$Id: wav.c,v 1.7 2002/07/20 00:09:05 johns Exp $
+
+//@{
+
+/*----------------------------------------------------------------------------
+--	Includes
+----------------------------------------------------------------------------*/
+
+#include "freecraft.h"
+
+#if defined(WITH_SOUND) // {
+
+#include "sound_server.h"
+#include <stdlib.h>
+
+/*----------------------------------------------------------------------------
+--	Declaration
+----------------------------------------------------------------------------*/
+
+/*----------------------------------------------------------------------------
+--	Functions
+----------------------------------------------------------------------------*/
+
+/**
+**	Load CD.
+**
+**	@param name	Unused.
+**	@param flags	Track number.
+**
+**	@return		Returns the loaded sample.
+**
+*/
+global Sample* LoadCD(const char* name, int flags)
+{
+    Sample* sample = NULL;
+    
+    return sample;
+}
+
+#endif	// } WITH_SOUND
+
+//@}
diff --git a/src/sound/music.cpp b/src/sound/music.cpp
index a191f7913..96ef4d6eb 100644
--- a/src/sound/music.cpp
+++ b/src/sound/music.cpp
@@ -66,7 +66,7 @@ global Sample* MusicSample;		/// Music samples
 
 #if defined(USE_SDLCD) || defined(USE_LIBCDA) || defined(USE_CDDA)
 global char *CDMode = ":off";	/// cd play mode, ":off" ":random" or ":all"
-global int CDTrack = 1;			/// Current cd track
+global int CDTrack = 0;			/// Current cd track
 #endif
 
 #if defined(USE_SDLCD)
@@ -76,6 +76,10 @@ global int NumCDTracks;			/// Number of tracks on the cd
 #elif defined(USE_CDDA)
 // FIXME: fill up
 global int NumCDTracks;
+global int CDDrive;
+global struct cdrom_tochdr CDchdr;
+global struct cdrom_tocentry CDtocentry[64];
+global struct cdrom_read_audio CDdata;
 #endif
 
 /*----------------------------------------------------------------------------
@@ -375,19 +379,22 @@ local int PlayCDRom(const char* name)
 */
 local int PlayCDRom(const char* name)
 {
-    cdrom_drive *CddaDrive = NULL;
-    void *buf;
-//    cdrom_paranoia *Cdda;
+    int i;
+    Sample *sample;
 
     if (!strcmp(CDMode, ":off")) {
 	if (!strncmp(name, ":", 1)) {
-	    CddaDrive = cdda_find_a_cdrom(0, NULL);
-	    cdda_open(CddaDrive);
-//	    Cdda = paranoia_init(CddaDrive);
+	    CDDrive = open("/dev/cdrom", O_RDONLY | O_NONBLOCK);
+	    ioctl(CDDrive, CDROMREADTOCHDR, &CDchdr);
 
-	    NumCDTracks = cdda_tracks(CddaDrive);
+	    for (i = CDchdr.cdth_trk0; i < CDchdr.cdth_trk1; ++i){
+		CDtocentry[i].cdte_format = CDROM_LBA;
+		CDtocentry[i].cdte_track = i + 1;
+		ioctl(CDDrive, CDROMREADTOCENTRY, &CDtocentry[i]);
+	    }
+	    NumCDTracks = i + 1;
 
-	    if (NumCDTracks == -1) {
+	    if (NumCDTracks == 0) {
 		CDMode = ":off";
 		return 1;
 	    }
@@ -405,19 +412,8 @@ local int PlayCDRom(const char* name)
 	    
 	    do {
 		if (CDTrack > NumCDTracks)
-		    CDTrack = 1;
-	    } while (cdda_track_audiop(CddaDrive, ++CDTrack) == 0);
-	    
-	    // temporary
-	    fprintf(stderr, "AAAAAAAAAAa %d\n",CDTrack);
-	    CDTrack = 3;
-	    
-	    buf = malloc(512*100);
-	    cdda_read(CddaDrive, buf, cdda_track_firstsector(CddaDrive, CDTrack), 100);
-
-	    free(buf);
-
-	    return 1;
+		    CDTrack = 0;
+	    } while (CDtocentry[++CDTrack].cdte_ctrl&CDROM_DATA_TRACK);
 	}
 	// if mode is play random tracks
 	if (!strcmp(name, ":random")) {
@@ -425,15 +421,13 @@ local int PlayCDRom(const char* name)
 
 	    do {
 		CDTrack = MyRand() % NumCDTracks;
-	    } while (cdda_track_audiop(CddaDrive, ++CDTrack) == 0);
-
-	    buf = malloc(512*100);
-	    cdda_read(CddaDrive, buf, cdda_track_firstsector(CddaDrive, CDTrack), 100);
-
-	    free(buf);
-
-	    return 1;
+	    } while (CDtocentry[++CDTrack].cdte_ctrl&CDROM_DATA_TRACK);
 	}
+
+	sample = LoadCD(NULL, CDTrack);
+	StopMusic();
+	MusicSample = sample;
+	PlayingMusic = 1;
 	return 1;
     }
     // FIXME: no cdrom, must stop it now!
diff --git a/src/stratagus/mainloop.cpp b/src/stratagus/mainloop.cpp
index 66cdc47ec..40f7ee804 100644
--- a/src/stratagus/mainloop.cpp
+++ b/src/stratagus/mainloop.cpp
@@ -699,12 +699,10 @@ global void GameMainLoop(void)
 	    //
 	    switch( GameCycle% ((CYCLES_PER_SECOND*VideoSyncSpeed/100)+1) ) {
 		case 0:				// Check cd-rom
-#ifdef USE_SDLCD
+#if defined(USE_SDLCD)
 		    if ( !(GameCycle%2) )	// every 2nd second
 			SDL_CreateThread(CDRomCheck, NULL);
-#endif
-
-#ifdef USE_LIBCDA
+#elif defined(USE_LIBCDA) || defined(USE_CDDA)
 		    CDRomCheck(NULL);
 #endif
 		    break;