home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / SprocketExamples / SprocketInvaders / Source / RedbookHandler.c < prev    next >
Encoding:
Text File  |  1998-07-14  |  3.7 KB  |  141 lines  |  [TEXT/CWIE]

  1. //•    ------------------------------------------------------------------------------------------    •
  2. //•
  3. //•    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  4. //•
  5. //•
  6. //•        You may incorporate this sample code into your applications without
  7. //•        restriction, though the sample code has been provided "AS IS" and the
  8. //•        responsibility for its operation is 100% yours.  However, what you are
  9. //•        not permitted to do is to redistribute the source as "DSC Sample Code"
  10. //•        after having made changes. If you're going to re-distribute the source,
  11. //•        we require that you make it clear in the source that the code was
  12. //•        descended from Apple Sample Code, but that you've made changes.
  13. //•
  14. //•        Authors:
  15. //•            Chris De Salvo
  16. //•
  17. //•    ------------------------------------------------------------------------------------------    •
  18.  
  19. //•    ------------------------------    Includes
  20.  
  21. #include <Devices.h>
  22.  
  23. #include <string.h>
  24.  
  25. #include "ErrorHandler.h"
  26. #include "RedbookHandler.h"
  27.  
  28. //•    ------------------------------    Private Definitions
  29. //•    ------------------------------    Private Types
  30. //•    ------------------------------    Private Variables
  31.  
  32. static SInt16 gDriverRef = -1;
  33.  
  34. //•    ------------------------------    Private Functions
  35.  
  36. static Boolean RedbookIsCDInserted(void);
  37.  
  38. //•    ------------------------------    Public Variables
  39.  
  40. Boolean gCDAudio = true;
  41.  
  42. //•    --------------------    RedbookHandlerInit
  43.  
  44. void
  45. RedbookHandlerInit(void)
  46. {
  47. OSErr    theErr;
  48.  
  49.     //•    Attempt to open the CD-ROM driver so we can call it later
  50.     theErr = OpenDriver("\p.AppleCD", &gDriverRef);
  51.  
  52.     if (theErr)
  53.         gDriverRef = -1;
  54. }
  55.  
  56. //•    --------------------    RedbookIsCDInserted
  57.  
  58. static Boolean
  59. RedbookIsCDInserted(void)
  60. {
  61. ParamBlockRec    params;
  62. OSErr            theErr;
  63.  
  64.     if (gDriverRef == -1)
  65.         return (false);
  66.     
  67.     memset(¶ms, 0, sizeof (ParamBlockRec));
  68.  
  69.     //•    On return from a drive status call the csParam fields have the same info
  70.     //•    as the regular disk driver DrvSts structure.  The fourth byte in that
  71.     //•    structure is true or false depending on whether or not a disk is in the drive.
  72.  
  73.     params.cntrlParam.ioCompletion = nil;
  74.     params.cntrlParam.ioVRefNum = 1;
  75.     params.cntrlParam.ioCRefNum = gDriverRef;
  76.     params.cntrlParam.csCode = 8;                    //•    Drive Status
  77.     
  78.     theErr = PBStatusSync((ParmBlkPtr) ¶ms);
  79.     if (theErr)
  80.         return (false);
  81.     
  82.     return (params.cntrlParam.csParam[1] & 0xFF);
  83. }
  84.  
  85. //•    --------------------    RedbookGetNumTracks
  86.  
  87. short
  88. RedbookGetNumTracks(void)
  89. {
  90. ParamBlockRec    params;
  91. OSErr            theErr;
  92. UInt8            lastTrack;
  93.  
  94.     if (gDriverRef == -1)
  95.         return (0);
  96.     
  97.     memset(¶ms, 0, sizeof (ParamBlockRec));
  98.  
  99.     params.cntrlParam.ioVRefNum = 1;
  100.     params.cntrlParam.ioCRefNum = gDriverRef;
  101.     params.cntrlParam.csCode = 100;                    //•    Read TOC
  102.     params.cntrlParam.csParam[0] = 1;                //•    Type 1 TOC lookup marker
  103.  
  104.     theErr = PBControlSync(¶ms);
  105.     if (theErr)
  106.         return (0);
  107.  
  108.     lastTrack = params.cntrlParam.csParam[0] & 0xFF;
  109.  
  110.     //•    Return the number of tracks in decimal instead of BCD form.
  111.     return ((((lastTrack >> 4) & 0x0F) * 10) + (lastTrack & 0x0F));
  112. }
  113.  
  114. //•    --------------------    RedbookPlayTrackNum
  115.  
  116. void
  117. RedbookPlayTrackNum(UInt16 trackNum)
  118. {
  119. ParamBlockRec    params;
  120.  
  121.     if (gDriverRef == -1)
  122.         return;
  123.  
  124.     if (false == gCDAudio)
  125.         return;
  126.  
  127.     memset(¶ms, 0, sizeof (ParamBlockRec));
  128.  
  129.     params.cntrlParam.ioCRefNum = gDriverRef;
  130.     params.cntrlParam.csCode = 104;                    //•    Audio Play
  131.     params.cntrlParam.csParam[0] = 0x0002;            //•    Use a BCD Track Number
  132.     
  133.     //•    Params 1 and 2 are the 32-bit value (in BCD) of the track to play.
  134.     //•    Seems odd to me that they used 2 bytes for this since the highest legal
  135.     //•    track number you can have is 99 and only needs 1 byte.
  136.     params.cntrlParam.csParam[2] = ((trackNum / 10) << 4) + (trackNum % 10);
  137.     params.cntrlParam.csParam[4] = 0x0009;            //•    Play in normal stereo mode
  138.  
  139.     PBControlSync(¶ms);
  140. }
  141.