/***********************************************************************
***
*** "RepeatTrack"  Repeats a CD audio track.
*** v1.1 Adam Keith Levin  Commodore Applications and Technical Support
*** Based upon Carl's CDTV CD-Audio Player 1.0
***
*** Free to Distribute amoung CDTV developers.
***
*** Pass it a track number from the CLI; it will repeat that track.
*** If no track is given, it repeats DEFAULT_TRACK (#defined below).
*** If you press CTRL-C it will stop repeating once the track finishes.
***
***********************************************************************/

#include <exec/types.h>
#include <exec/io.h>
#include <libraries/dos.h>
#include <stdio.h>
#include "cdtv.h"

extern struct   IOStdReq *CreateStdIO();
extern struct   MsgPort  *CreatePort();
struct IOStdReq *IORequest = NULL;
struct MsgPort  *IOPort = NULL;

#define TOC_entries 100
struct CDTOC    Toc[TOC_entries];

#define CTRL_C     (SetSignal(0L, 0L) & SIGBREAKF_CTRL_C)

#define DEFAULT_TRACK 1


main(int argc, char *argv[])
{
int track = DEFAULT_TRACK, last;

        /* Parse argument (if any). */
        if (argc > 1)
        {
                track = atoi(argv[1]);
                if (!track)
                        track = DEFAULT_TRACK;
        }

        IOPort = CreatePort(0,0);
        if (NULL == IOPort)
                printf("Unable to create port.\n");
        else
        {
                IORequest = CreateStdIO(IOPort);
                if (NULL == IORequest)
                        printf("Unable to create I/O request.\n");
                else
                {
                        if (OpenDevice(CD_NAME,0,IORequest,0))
                                printf("Unable to open the CDTV device.\n");
                        else
                        {
                                /* Obtain TOC and examine number of tracks on disc. */
                                IORequest->io_Command = CD_TOCMSF;
                                IORequest->io_Offset = 0;
                                IORequest->io_Length = TOC_entries;
                                IORequest->io_Data   = &Toc[0];
                                if (DoIO(IORequest))
                                        printf("Unable to examine disc's Table of Contents.\n");
                                else
                                {
                                        /* Insure that disc has enough tracks. */
                                        last = Toc[0].LastTrack;
                                        if (track > last)
                                                printf("Not that many tracks (last track is %d).\n", last);
                                        else
                                        {
                                                /* Keep playing the track until CTRL-C is sensed.
                                                   (To abort play during a track use CD_STOPPLAY.)
                                                */
                                                while (! CTRL_C)
                                                {
                                                        IORequest->io_Command = CD_PLAYTRACK;
                                                        IORequest->io_Offset = track;
                                                        IORequest->io_Length = 0;
                                                        IORequest->io_Data   = NULL;
                                                        if (DoIO(IORequest))
                                                                printf("Unable to play track %d.\n", track);
                                                }
                                        }
                                }
                        CloseDevice(IORequest);
                        }
                DeleteStdIO(IORequest);
                }
        DeletePort(IOPort);
        }
}
