/*
**	Command & Conquer Red Alert(tm)
**	Copyright 2025 Electronic Arts 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 3 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, see <http://www.gnu.org/licenses/>.
*/

/***************************************************************************
 **   C O N F I D E N T I A L --- W E S T W O O D   A S S O C I A T E S   **
 ***************************************************************************
 *                                                                         *
 *                 Project Name : WWLIB												*
 *                                                                         *
 *                    File Name : GETCD.CPP											*
 *                                                                         *
 *                   Programmer : STEVE WETHERILL BASED ON JOE BOSTIC CODE *
 *                                                                         *
 *                   Start Date : 5/13/94												*
 *                                                                         *
 *                  Last Update : June 4, 1994   [SW]                      *
 *                                                                         *
 *-------------------------------------------------------------------------*
 *-------------------------------------------------------------------------*
 * Functions:                                                              *
 *		GetCDClass::GetCDClass	--	default constructor                       *
 *		GetCDClass::~GetCDClass	--	destructor                                *
 *		GetCDClass::GetCDDrive	--	returns the logical CD drive              *
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>

#include "wwstd.h"
#include "playcd.h"
#include "wwmem.h"


/***************************************************************************
 * GetCDClass -- default constructor													*
 *                                                                         *
 *                                                                         *
 *                                                                         *
 * INPUT:                                                                  *
 *       none 																					*
 * OUTPUT:                                                                 *
 *			none                          												*
 * WARNINGS:                                                               *
 *                                                                         *
 * HISTORY:                                                                *
 *   05/26/1994 SW   : Created.                                            *
 *=========================================================================*/

GetCDClass::GetCDClass(VOID)
{
	memset ( this , 0 , sizeof ( GetCDClass ) ) ;

	if	(DPMI_real_alloc(2, &cdDrive_addrp, &largestp))
		exit(1);

	CDCount = 0;
	CDIndex = 0;

	/*
	** Set all CD drive placeholders to empty
	*/
	memset (CDDrives, NO_CD_DRIVE, MAX_CD_DRIVES);

	/*
	** Dos will only currently support one cd drive so just
	** set the first entry to it.
	*/
	GetCDDrives();
}

/***************************************************************************
 * GetCDClass -- destructor                                                *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 * INPUT:                                                                  *
 *    	none																					*
 * OUTPUT:                                                                 *
 *			none																					*
 * WARNINGS:                                                               *
 *                                                                         *
 * HISTORY:                                                                *
 *   05/26/1994 SW: Created.                                          	   *
 *=========================================================================*/

GetCDClass::~GetCDClass(VOID)
{
	if(cdDrive_addrp.seg)
		DPMI_real_free(cdDrive_addrp);		// free up those conventional buffers
}

/***************************************************************************
 * GetCDDrive -- returns the logical CD drive                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 * INPUT:                                                                  *
 *			none																					*
 * OUTPUT:                                                                 *
 *			WORD logical_drive																*
 * WARNINGS:                                                               *
 *                                                                         *
 * HISTORY:                                                                *
 *   05/26/1994 SW : Created.                                              *
 *=========================================================================*/

void  GetCDClass::GetCDDrives(VOID)

{
	for (int lp = 0; lp < 26; lp++ ) {
		/*
		** This call determines if the current specifed drive is a
		** CD ROM drive.
		** Input:
		** AX = 150Bh
		** CX = CD rom drive letter to check (A = 0, B = 1)
		** Output:
		** AX = non zero if drive is a CD ROM, zero if it is not.
		** BX = Signature word (ADADh if CD rom extension are installed)
		*/
		sregs . es 	= cdDrive_addrp . seg ;
		regs . x . ebx  = 0;
		regs . x . eax  = 0x150B;
		regs . x . ecx  = lp;
		DPMI_real_intr ( 0x2F , & regs , & sregs ) ;

		if (regs.x.ebx == 0xADAD && regs.x.eax != 0) {
			CDDrives[CDCount++] = lp;
		}
	}
}
/* ==================================================================== */

