home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / cdrom / cybersound / cdplayer / source / wbinterface.c < prev   
C/C++ Source or Header  |  1977-12-31  |  12KB  |  515 lines

  1.  
  2. /********************************************
  3.  * CD Audio Player using 14bit sound driver *
  4.  *   for Toshiba 4101 CDRom and compatible  *
  5.  *      (c) 1995 by Christian Buchner       *
  6.  ********************************************
  7.  *
  8.  * WBInterface.c
  9.  */
  10.  
  11. /* Note: TAB SIZE = 4 */
  12.  
  13.  
  14. /* Includes */
  15.  
  16. #include <proto/dos.h>
  17. #include <proto/exec.h>
  18. #include <proto/intuition.h>
  19. #include <proto/icon.h>
  20. #include <libraries/dos.h>
  21. #include <utility/tagitem.h>
  22. #include <gadgets/tapedeck.h>
  23. #include <intuition/gadgetclass.h>
  24. #include <intuition/screens.h>
  25. #include <intuition/icclass.h>
  26. #include <workbench/startup.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30.  
  31.  
  32. /* Prototypes */
  33.  
  34. #include "CDPlayer_Protos.h"
  35. void GUI(void);
  36. extern void __saveds ProgramCallback(void);
  37. extern BOOL ReadDefaults(void);
  38. extern void FreeDefaults(void);
  39. BOOL OpenLibs(void);
  40. void CloseLibs(void);
  41. void ReadIcon(void);
  42. void FreeIcon(void);
  43. void CheckString(UBYTE *Name, UBYTE **Storage);
  44. void CheckLong(UBYTE *Name, ULONG **Pointer, ULONG *Storage);
  45. void CheckBoolean(UBYTE *Name, ULONG *Storage);
  46.  
  47.  
  48. struct ArgArray
  49. {
  50.     ULONG **aa_Tracks;
  51.     UBYTE *aa_SCSIName;
  52.     ULONG *aa_SCSIUnit;
  53.     UBYTE *aa_SoundDriver;
  54.     ULONG aa_Repeat;
  55.     ULONG *aa_Frequency;
  56.     ULONG *aa_Quality;
  57.     ULONG *aa_Buffers;
  58.     ULONG aa_HighPri;
  59.     UBYTE *aa_PubScreen;
  60.     ULONG aa_GUI;
  61. };
  62.  
  63.  
  64. /* Externals */
  65.  
  66. extern struct DosLibrary *DOSBase;            /* From CDPlayer.c */
  67. extern struct IntuitionBase *IntuitionBase;
  68. extern struct GfxBase *GfxBase;
  69. extern struct Process *MyProc;                /* From CLIInterface.c */
  70. extern struct ArgArray AA;
  71. extern UBYTE LineBuffer[256];
  72. extern UBYTE ErrorBuffer[100];
  73. extern struct RDArgs *RDArgs;
  74. extern struct RDArgs *RDArgsSuccess;
  75.  
  76.  
  77. /* Default parameters */
  78.  
  79. extern UBYTE DefaultDriverName[];            /* From CLIInterface.c */
  80. extern UBYTE DefaultSCSIName[];
  81. extern ULONG DefaultSCSIUnit;
  82. extern ULONG DefaultFrequency;
  83. extern ULONG DefaultQuality;
  84. extern ULONG DefaultBuffers;
  85.  
  86.  
  87. /* Variables */
  88.  
  89. struct WBStartup *WBenchMsg;
  90. struct Library *TapeDeckBase;
  91. struct Library *IconBase;
  92. struct DiskObject *DiskObject;
  93. ULONG TT_SCSIUnit;
  94. ULONG TT_Frequency;
  95. ULONG TT_Buffers;
  96. ULONG TT_Quality;
  97. UBYTE Titlebuffer[40];
  98. BOOL LibsOpen;
  99.  
  100.  
  101. /*******************************************************************************/
  102.  
  103. /* Workbench interface routine */
  104.  
  105. void WBInterface(void)
  106. {
  107.     WaitPort(&MyProc->pr_MsgPort);
  108.     WBenchMsg=(struct WBStartup*)GetMsg(&MyProc->pr_MsgPort);
  109.     
  110.     /* Open DOS library */
  111.     if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37))
  112.     {
  113.         /* Open other libraries */
  114.         if (OpenLibs())
  115.         {
  116.             /* Read global defaults */
  117.             if (ReadDefaults())
  118.             {
  119.                 /* Read the tooltypes from the icon */
  120.                 ReadIcon();
  121.                 
  122.                 /* Set defaults for the arguments */
  123.                 if (!AA.aa_SCSIName)     AA.aa_SCSIName=        DefaultSCSIName;
  124.                 if (!AA.aa_SCSIUnit)     AA.aa_SCSIUnit=        &DefaultSCSIUnit;
  125.                 if (!AA.aa_SoundDriver) AA.aa_SoundDriver=    DefaultDriverName;
  126.                 if (!AA.aa_Frequency)    AA.aa_Frequency=    &DefaultFrequency;
  127.                 if (!AA.aa_Quality)        AA.aa_Quality=        &DefaultQuality;
  128.                 if (!AA.aa_Buffers)        AA.aa_Buffers=        &DefaultBuffers;
  129.                 
  130.                 GUI();
  131.                 
  132.                 FreeIcon();
  133.                 FreeDefaults();
  134.             }
  135.             CloseLibs();
  136.         }
  137.         CloseLibrary((struct Library*)DOSBase);
  138.     }
  139.     Forbid();
  140.     ReplyMsg((struct Message*)WBenchMsg);
  141. }
  142.  
  143.  
  144. void GUI(void)
  145. {
  146.     BOOL CloseFlag;
  147.     struct Screen *Screen;
  148.     struct Gadget *tdeck;
  149.     struct Window *Window;
  150.     ULONG GWidth=201, GHeight=15;
  151.     struct IntuiMessage *MyMsg;
  152.     ULONG Cl;
  153.     UWORD Co;
  154.     struct Gadget *IA;
  155.     ULONG Mode,OldMode=99;
  156.     ULONG Paused,OldPaused=BUT_PLAY;
  157.     ULONG track, oldtrack=0;
  158.     ULONG position, oldposition=0;
  159.     
  160.     /* Open Libraries */
  161.     CloseFlag=OpenLibs();
  162.     
  163.     /* Lock specified public screen */
  164.     if (!(Screen=LockPubScreen(AA.aa_PubScreen)))
  165.     {
  166.         /* or workbench if this failed */
  167.         Screen=LockPubScreen(NULL);
  168.     }
  169.     if (Screen)
  170.     {
  171.         /* Create TapeDeck gadget */
  172.         if (tdeck=NewObject(NULL,"tapedeck.gadget",
  173.                     TDECK_Tape, TRUE,
  174.                     GA_Top, Screen->WBorTop+Screen->RastPort.TxHeight+1,
  175.                     GA_Left, Screen->WBorLeft,
  176.                     TAG_DONE))
  177.         {
  178.             /* Setting a custom size currently does not work */
  179.             SetAttrs(tdeck,GA_Width, GWidth, TAG_DONE);
  180.             SetAttrs(tdeck,GA_Height,GHeight,TAG_DONE);
  181.             
  182.             /* Start with the play button pressed */
  183.             SetAttrs(tdeck,TDECK_Mode,BUT_PLAY,TAG_DONE);
  184.             
  185.             /* Open the window */
  186.             if (Window=OpenWindowTags(NULL,
  187.                             WA_Title,"CDPlayer",
  188.                             WA_DragBar,TRUE,
  189.                             WA_CloseGadget,TRUE,
  190.                             WA_DepthGadget,TRUE,
  191.                             WA_Gadgets,tdeck,
  192.                             WA_Left,Screen->MouseX-(GWidth+Screen->WBorLeft+Screen->WBorRight)/2,
  193.                             WA_Top,Screen->MouseY-(GHeight+Screen->WBorTop+Screen->RastPort.TxHeight+1+Screen->WBorBottom)/2,
  194.                             WA_InnerWidth,GWidth,
  195.                             WA_InnerHeight,GHeight,
  196.                             WA_IDCMP,IDCMP_CLOSEWINDOW,
  197.                             WA_PubScreen, Screen,
  198.                             TAG_DONE))
  199.             {
  200.                 if (InitDriver(AA.aa_SoundDriver,*AA.aa_Buffers))
  201.                 {
  202.                     if (OpenSCSI(AA.aa_SCSIName,*AA.aa_SCSIUnit,(BOOL)AA.aa_HighPri))
  203.                     {
  204.                         /* Create a program for replay */
  205.                         if (!CD_SetProgram(AA.aa_Tracks, ProgramCallback))
  206.                         {
  207.                             Message("CD or specified track list contain no audio tracks.");
  208.                         }
  209.                         else
  210.                         {
  211.                             CD_Frequency(*AA.aa_Frequency);
  212.                             CD_Quality  (*AA.aa_Quality);
  213.                             
  214.                             /* Start reading/playing */
  215.                             CD_Start();
  216.                             
  217.                 /* Note: This uses a delay loop instead of      */ 
  218.                 /* waiting for signals because the notification */
  219.                 /* mechanism via icclass doesn't work with      */
  220.                 /* tapedeck.gadget                              */
  221.                             
  222.                             while(!CheckSignal(SIGBREAKF_CTRL_C))
  223.                             {
  224.                                 if (MyMsg=(struct IntuiMessage*)GetMsg(Window->UserPort))
  225.                                 {
  226.                                     /* When we are here we got an IntuiMsg */
  227.                                     Cl=MyMsg->Class;
  228.                                     Co=MyMsg->Code;
  229.                                     IA=(struct Gadget*)MyMsg->IAddress;
  230.                                     ReplyMsg((struct Message*)MyMsg);
  231.                                     
  232.                                     /* Window was closed? */
  233.                                     if (Cl==IDCMP_CLOSEWINDOW)
  234.                                     {
  235.                                         break;
  236.                                     }
  237.                                 }
  238.                                 else
  239.                                 {
  240.                                     /* Update track/position */
  241.                                     track=CD_CurrentTrack();
  242.                                     position=CD_CurrentPosition()/75;
  243.                                     if ((track!=oldtrack) || (position!=oldposition))
  244.                                     {
  245.                                         sprintf(Titlebuffer,"CDPlayer %2ld %2ld:%02ld",track,position/60,position%60);
  246.                                         SetWindowTitles(Window,Titlebuffer,(UBYTE*)-1);
  247.                                         oldtrack=track;
  248.                                         oldposition=position;
  249.                                     }
  250.                                     
  251.                                     /* Check the play mode */
  252.                                     GetAttr(TDECK_Mode,tdeck,&Mode);
  253.                                     if (Mode!=OldMode)
  254.                                     {
  255.                                         switch(Mode)
  256.                                         {
  257.                                             case BUT_PLAY:
  258.                                             {
  259.                                                 CD_Start();
  260.                                                 OldMode=Mode;
  261.                                             }
  262.                                             break;
  263.                                             
  264.                                             case BUT_STOP:
  265.                                             {
  266.                                                 CD_Abort();
  267.                                                 CD_ToIndex(CD_CurrentIndex());
  268.                                                 OldMode=Mode;
  269.                                             }
  270.                                             break;
  271.                                             
  272.                                             case BUT_REWIND:
  273.                                             {
  274.                                                 Delay(10);
  275.                                                 SetGadgetAttrs(tdeck,Window,NULL,TDECK_Mode,OldMode,TAG_DONE);
  276.                                                 if (!CD_PrevIndex())
  277.                                                 {
  278.                                                     DisplayBeep(NULL);
  279.                                                 }
  280.                                             }
  281.                                             break;
  282.                                             
  283.                                             case BUT_FORWARD:
  284.                                             {
  285.                                                 Delay(10);
  286.                                                 SetGadgetAttrs(tdeck,Window,NULL,TDECK_Mode,OldMode,TAG_DONE);
  287.                                                 if (!CD_NextIndex())
  288.                                                 {
  289.                                                     DisplayBeep(NULL);
  290.                                                 }
  291.                                             }
  292.                                             break;
  293.                                         }
  294.                                     }
  295.                                     
  296.                                     /* Check pause flag */
  297.                                     GetAttr(TDECK_Paused,tdeck,&Paused);
  298.                                     if (Paused!=OldPaused)
  299.                                     {
  300.                                         if (Paused)
  301.                                         {
  302.                                             CD_Pause();
  303.                                         }
  304.                                         else
  305.                                         {
  306.                                             CD_Resume();
  307.                                         }
  308.                                         OldPaused=Paused;
  309.                                     }
  310.                                     
  311.                                     /* End of program signalled? */
  312.                                     if (MyProc->pr_Task.tc_SigRecvd & SIGBREAKF_CTRL_F)
  313.                                     {
  314.                                         /* Clear that signal */
  315.                                         SetSignal(0,SIGBREAKF_CTRL_F);
  316.                                         
  317.                                         /* Press STOP button */
  318.                                         SetGadgetAttrs(tdeck,Window,NULL,TDECK_Mode,BUT_STOP,TAG_DONE);
  319.                                     }
  320.                                     
  321.                                     /* Delay 1/10 of a second */
  322.                                     Delay(5);
  323.                                 }
  324.                             }
  325.                             
  326.                             CD_Abort();
  327.                         }
  328.                         CloseSCSI();
  329.                     }
  330.                     DeInitDriver();
  331.                 }
  332.                 CloseWindow(Window);
  333.             }
  334.             DisposeObject(tdeck);
  335.         }
  336.         UnlockPubScreen(NULL,Screen);
  337.     }
  338.     if (CloseFlag) CloseLibs();
  339. }
  340.  
  341.  
  342. BOOL OpenLibs(void)
  343. {
  344.     BOOL Success=FALSE;
  345.     
  346.     if (!LibsOpen)
  347.     {
  348.         if (IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",37))
  349.         {
  350.             if (GfxBase=(struct GfxBase*)OpenLibrary("graphics.library",37))
  351.             {
  352.                 if (IconBase=OpenLibrary("icon.library",37))
  353.                 {
  354.                     if (!(TapeDeckBase=OpenLibrary("gadgets/tapedeck.gadget",39)))
  355.                     {
  356.                         Message("Cannot open V39 tapedeck.gadget boopsi class!");
  357.                     }
  358.                     else
  359.                     {
  360.                         LibsOpen=TRUE;
  361.                         Success=TRUE;
  362.                     }
  363.                 }
  364.             }
  365.         }
  366.         if (!Success)
  367.         {
  368.             CloseLibs();
  369.         }
  370.     }
  371.     return(Success);
  372. }
  373.  
  374.  
  375. void CloseLibs(void)
  376. {
  377.     if (TapeDeckBase)
  378.     {
  379.         CloseLibrary(TapeDeckBase);
  380.         TapeDeckBase=NULL;
  381.     }
  382.     if (IconBase)
  383.     {
  384.         CloseLibrary(IconBase);
  385.         IconBase=NULL;
  386.     }
  387.     if (GfxBase)
  388.     {
  389.         CloseLibrary((struct Library*)GfxBase);
  390.         GfxBase=NULL;
  391.     }
  392.     if (IntuitionBase)
  393.     {
  394.         CloseLibrary((struct Library*)IntuitionBase);
  395.         IntuitionBase=NULL;
  396.     }
  397.     LibsOpen=FALSE;
  398. }
  399.  
  400.  
  401. void ReadIcon(void)
  402. {
  403.     UBYTE *String;
  404.     
  405.     CurrentDir(WBenchMsg->sm_ArgList->wa_Lock);
  406.     
  407.     if (DiskObject=GetDiskObjectNew(WBenchMsg->sm_ArgList->wa_Name))
  408.     {
  409.         if (String=FindToolType(DiskObject->do_ToolTypes,"TRACKS"))
  410.         {
  411.             strncpy(LineBuffer,String,sizeof(LineBuffer)-1);
  412.             strcat(LineBuffer,"\n");
  413.             
  414.             if (RDArgs=(struct RDArgs*)AllocDosObject(DOS_RDARGS,TAG_DONE))
  415.             {
  416.                 RDArgs->RDA_Source.CS_Buffer=LineBuffer;
  417.                 RDArgs->RDA_Source.CS_Length=strlen(LineBuffer);
  418.                 RDArgs->RDA_Source.CS_CurChr=0;
  419.                 RDArgs->RDA_DAList=NULL;
  420.                 RDArgs->RDA_Buffer=NULL;
  421.                 RDArgs->RDA_BufSiz=NULL;
  422.                 RDArgs->RDA_ExtHelp=NULL;
  423.                 RDArgs->RDA_Flags=RDAF_NOPROMPT;
  424.                 
  425.                 if (!(RDArgsSuccess=ReadArgs("TRACKS/N/M", (LONG *)&AA.aa_Tracks, RDArgs)))
  426.                 {
  427.                     Fault(IoErr(),NULL, ErrorBuffer, sizeof(ErrorBuffer));
  428.                     Message("Error parsing TRACKS tooltype:\n%s\n%s",LineBuffer,ErrorBuffer);
  429.                 }
  430.             }
  431.         }
  432.         CheckString("DEVICE", &AA.aa_SCSIName);
  433.         CheckLong("UNIT", &AA.aa_SCSIUnit, &TT_SCSIUnit);
  434.         CheckString("SOUNDDRIVER", &AA.aa_SoundDriver);
  435.         CheckBoolean("REPEAT",&AA.aa_Repeat);
  436.         CheckLong("FREQUENCY", &AA.aa_Frequency, &TT_Frequency);
  437.         CheckLong("QUALITY", &AA.aa_Quality, &TT_Quality);
  438.         CheckLong("BUFFERS", &AA.aa_Buffers, &TT_Buffers);
  439.         CheckBoolean("HIGHPRI",&AA.aa_HighPri);
  440.         CheckString("PUBSCREEN", &AA.aa_PubScreen);
  441.     }
  442. }
  443.  
  444.  
  445. void FreeIcon(void)
  446. {
  447.     if (RDArgsSuccess)
  448.     {
  449.         FreeArgs(RDArgsSuccess);
  450.         RDArgsSuccess=NULL;
  451.     }
  452.     if (RDArgs)
  453.     {
  454.         FreeDosObject(DOS_RDARGS, RDArgs);
  455.         RDArgs=NULL;
  456.     }
  457.     if (DiskObject)
  458.     {
  459.         FreeDiskObject(DiskObject);
  460.         DiskObject=NULL;
  461.     }
  462. }
  463.  
  464.  
  465. void CheckString(UBYTE *Name, UBYTE **Storage)
  466. {
  467.     UBYTE *String;
  468.     
  469.     if (String=FindToolType(DiskObject->do_ToolTypes,Name))
  470.     {
  471.         *Storage=String;
  472.     }
  473. }
  474.  
  475.  
  476. void CheckLong(UBYTE *Name, ULONG **Pointer, ULONG *Storage)
  477. {
  478.     UBYTE *String;
  479.     
  480.     if (String=FindToolType(DiskObject->do_ToolTypes,Name))
  481.     {
  482.         *Pointer=Storage;
  483.         *Storage=atol(String);
  484.     }
  485. }
  486.  
  487.  
  488. void CheckBoolean(UBYTE *Name, ULONG *Storage)
  489. {
  490.     UBYTE *String;
  491.     
  492.     if (String=FindToolType(DiskObject->do_ToolTypes,Name))
  493.     {
  494.         if ((strlen(String)==0) ||
  495.             MatchToolValue(String,"TRUE") ||
  496.             MatchToolValue(String,"YES"))
  497.         {
  498.             *Storage=TRUE;
  499.         }
  500.         else
  501.         {
  502.             if (MatchToolValue(String,"FALSE") ||
  503.                 MatchToolValue(String,"NO"))
  504.             {
  505.                 *Storage=FALSE;
  506.             }
  507.             else
  508.             {
  509.                 Message("Illegal parameter for %s tooltype\n"
  510.                         "Use TRUE/YES, FALSE/NO or nothing.\n",Name);
  511.             }
  512.         }
  513.     }
  514. }
  515.