/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga

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

    This library 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
    Library General Public License for more details.

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

    Sam Lantinga
    slouken@devolution.com
*/

/* Functions for system-level CD-ROM audio control */

/* RISC OS version by Alan Buckley 13th May 2003 */

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

#include "kernel.h"

#include "SDL_error.h"
#include "SDL_cdrom.h"
#include "SDL_syscdrom.h"

/* CD Rom switches */
#ifndef CDFS_GetNumberOfDrives
#define CDFS_GetNumberOfDrives    0x41E84
#define CDFS_ConvertDriveToDevice 0x41E80
#define CD_DriveStatus            0x41243
#define CD_AudioStatus            0x41254
#define CD_EnquireAddress         0x41249
#define CD_EnquireTrack           0x4124E
#define CD_ConvertToMSF           0x41265
#define CD_PlayAudio              0x4124B
#define CD_AudioPause             0x4124D
#define CD_StopDisc               0x41252
#define CD_OpenDrawer             0x41247
#define CD_DiscUsed               0x41253

#endif

typedef struct
{
   int block[5];
} CDCB;

static CDCB **cdCB = NULL;
static char **SDL_cdlist = NULL;

/* The system-dependent CD control functions */
static const char *SDL_SYS_CDName(int drive);
static int SDL_SYS_CDOpen(int drive);
static int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
static int SDL_SYS_CDPause(SDL_CD *cdrom);
static int SDL_SYS_CDResume(SDL_CD *cdrom);
static int SDL_SYS_CDStop(SDL_CD *cdrom);
static int SDL_SYS_CDEject(SDL_CD *cdrom);
static void SDL_SYS_CDClose(SDL_CD *cdrom);

int  SDL_SYS_CDInit(void)
{
  _kernel_swi_regs regs;
  int j;
  char driveName[8];

  if (_kernel_swi(CDFS_GetNumberOfDrives, &regs, &regs) != NULL)
  {
     return -1;
  }

  SDL_numcds = regs.r[0];
  if (SDL_numcds == 0) return -1;

  cdCB = malloc(sizeof(CDCB *) * SDL_numcds);
  SDL_cdlist = malloc(sizeof(char *) * SDL_numcds);
  if (cdCB == NULL || SDL_cdlist == NULL)
  {
     SDL_OutOfMemory();
     return -1;
  }

  for (j = 0; j < SDL_numcds; j++)
  {
     cdCB[j] = 0;
     sprintf(driveName, "%d", j);
     SDL_cdlist[j] = strdup(driveName);
     if (SDL_cdlist[j] == NULL)
     {
         SDL_OutOfMemory();
         return -1;
     }
  }


	/* Fill in our driver capabilities */
	SDL_CDcaps.Name = SDL_SYS_CDName;
	SDL_CDcaps.Open = SDL_SYS_CDOpen;
	SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
	SDL_CDcaps.Status = SDL_SYS_CDStatus;
	SDL_CDcaps.Play = SDL_SYS_CDPlay;
	SDL_CDcaps.Pause = SDL_SYS_CDPause;
	SDL_CDcaps.Resume = SDL_SYS_CDResume;
	SDL_CDcaps.Stop = SDL_SYS_CDStop;
	SDL_CDcaps.Eject = SDL_SYS_CDEject;
	SDL_CDcaps.Close = SDL_SYS_CDClose;

	return(0);
}

static const char *SDL_SYS_CDName(int drive)
{
	return(SDL_cdlist[drive]);
}

static int SDL_SYS_CDOpen(int drive)
{
   _kernel_swi_regs regs;
   int device;

   if (cdCB[drive] != NULL) return drive; /* Already open */

   regs.r[0] = drive;
   if (_kernel_swi(CDFS_ConvertDriveToDevice, &regs, &regs) != NULL) return -1;
   cdCB[drive] = malloc(sizeof(CDCB));
   if (cdCB[drive] == NULL)
   {
      SDL_OutOfMemory();
      return -1;
   }

   device = regs.r[1];

   cdCB[drive]->block[0] = device & 7;
   cdCB[drive]->block[1] = (device & 0x18) >> 3;
   cdCB[drive]->block[2] = (device & 0xE0) >> 5;
   cdCB[drive]->block[3] = (device & 0xFF00) >> 8;
   cdCB[drive]->block[4] = (device & 0xFFFF0000) >> 16;

   return(drive);
}


/* Get CD-ROM status */
static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
{
   CDstatus status = CD_ERROR;
   _kernel_swi_regs regs;

    regs.r[7] = (int)cdCB[cdrom->id];
    if (_kernel_swi(CD_DriveStatus, &regs, &regs) != NULL)
    {
       status = CD_ERROR;
    } else
    {
      if (regs.r[0] == 1)
      {
         /* CD in drive */
         if (_kernel_swi(CD_AudioStatus, &regs, &regs) != NULL)
         {
            status = CD_ERROR;
         } else
         {
            switch(regs.r[0])
            {
                case 0: status = CD_PLAYING; break;
                case 1: status = CD_PAUSED; break;
                case 3: /* 3 and 5 need to be treated the same */
                case 5:
                      status = CD_STOPPED;
                      break;
            }
         }
       } else
          status = CD_TRAYEMPTY;

       if (position)
       {
          if (status == CD_PLAYING || status == CD_PAUSED)
          {
             regs.r[0] = 1; /* Red book addressing */
             regs.r[7] = (int)cdCB[cdrom->id];
             if (_kernel_swi(CD_EnquireAddress, &regs, &regs) == NULL)
             {
                int rbAddress = regs.r[0];
                int ff = rbAddress & 0xFF;
                int ss = (rbAddress & 0xFF00) >> 8;
                int mm = (rbAddress & 0xFF0000) >> 16;
                *position = MSF_TO_FRAMES(mm,ss,ff);
             } else
                *position =0;
          } else
             *position = 0;
       }
    }

	return(status);
}

static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
{
   _kernel_swi_regs regs;
   unsigned char buffer[5];
   int firstTrack, lastTrack;
   int trackPos;
   int j;
   int mins, secs, frames;

   regs.r[7] = (int)cdCB[cdrom->id];
   regs.r[0] = 0;
   regs.r[1] = (int)buffer;
   if (_kernel_swi(CD_EnquireTrack, &regs, &regs) != NULL) return -1;
   firstTrack = buffer[0];
   lastTrack  = buffer[1];

   cdrom->numtracks = lastTrack - firstTrack + 1;
   if ( cdrom->numtracks > SDL_MAX_TRACKS ) cdrom->numtracks = SDL_MAX_TRACKS;

   for (j = 0;j < cdrom->numtracks; j++)
   {
       regs.r[0] = firstTrack + j;
       regs.r[1] = (int)buffer;
       regs.r[7] = (int)cdCB[cdrom->id];
       if (_kernel_swi(CD_EnquireTrack, &regs, &regs) != NULL) return -1;
       trackPos = *(int *)buffer;
       if ((buffer[4] & 1) == 0)
       {       
          cdrom->track[j].type = SDL_AUDIO_TRACK;
       } else
       {
          cdrom->track[j].type = SDL_DATA_TRACK;
       }

       regs.r[0] = 0;
       regs.r[1] = trackPos;
       if (_kernel_swi(CD_ConvertToMSF, &regs, &regs) != NULL) return -1;

       mins = (regs.r[1] & 0xFF0000) >> 16;
       secs = (regs.r[1] & 0xFF00) >> 8;
       frames = (regs.r[1] & 0xFF);

       cdrom->track[j].offset = MSF_TO_FRAMES(mins, secs, frames);
       cdrom->track[j].length = 0;
       if ( j > 0 )
       {
           cdrom->track[j-1].length = cdrom->track[j].offset - cdrom->track[j-1].offset;
       }
   }
   if (j == cdrom->numtracks)
   {
      unsigned char block[8];
      regs.r[0] = 1; /* Return in red book format */
      regs.r[1] = (int)block;
      _kernel_swi(CD_DiscUsed, &regs, &regs);
      cdrom->track[j].offset = MSF_TO_FRAMES(block[2], block[1], block[0]) - 151;
      /* Apparently reply above include 151 unreadable sectors */
      cdrom->track[j-1].length = cdrom->track[j].offset - cdrom->track[j-1].offset;      
    }

    return 0;
}

/* Start play */
static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
{
   _kernel_swi_regs regs;
    int m, s, f;

   regs.r[0] = 1; /* Redbook addressing */
   regs.r[7] = (int)cdCB[cdrom->id];

   FRAMES_TO_MSF(start, &m, &s, &f);
   regs.r[1] = (m << 16) + (s << 8) + f;
   FRAMES_TO_MSF(start+length, &m, &s, &f);
   regs.r[2] = (m << 16) + (s << 8) + f;

   if (_kernel_swi(CD_PlayAudio, &regs, &regs) == NULL)
      return 0;
   else
      return -1;
}

/* Pause play */
static int SDL_SYS_CDPause(SDL_CD *cdrom)
{
   _kernel_swi_regs regs;

   regs.r[0] = 1; /* Pause */
   regs.r[7] = (int)cdCB[cdrom->id];
   if (_kernel_swi(CD_AudioPause, &regs, &regs) == NULL)
      return 0;
   else
      return -1;
}

/* Resume play */
static int SDL_SYS_CDResume(SDL_CD *cdrom)
{
   _kernel_swi_regs regs;

   regs.r[0] = 0; /* Unpause */
   regs.r[7] = (int)cdCB[cdrom->id];
   if (_kernel_swi(CD_AudioPause, &regs, &regs) == NULL)
      return 0;
   else
      return -1;
}

/* Stop play */
static int SDL_SYS_CDStop(SDL_CD *cdrom)
{
   _kernel_swi_regs regs;

   regs.r[7] = (int)cdCB[cdrom->id];
   if (_kernel_swi(CD_StopDisc, &regs, &regs) == NULL)
      return 0;
   else
      return -1;
}

/* Eject the CD-ROM */
static int SDL_SYS_CDEject(SDL_CD *cdrom)
{
   _kernel_swi_regs regs;

   regs.r[7] = (int)cdCB[cdrom->id];
   if (_kernel_swi(CD_OpenDrawer, &regs, &regs) == NULL)
      return 0;
   else
      return -1;
}

/* Close the CD-ROM handle */
static void SDL_SYS_CDClose(SDL_CD *cdrom)
{
   /* I don't think I need to do anything here */
}

void SDL_SYS_CDQuit(void)
{
	int i;

	if ( SDL_numcds > 0 )
    {
		for ( i=0; i<SDL_numcds; ++i )
		{
			free(SDL_cdlist[i]);
			if (cdCB[i] != NULL) free(cdCB[i]);
		}
		SDL_numcds = 0;
		free(SDL_cdlist);
		free(cdCB);
	}
}
