home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / AsyncSysBeep / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-25  |  3.7 KB  |  221 lines  |  [TEXT/KAHL]

  1. #include <Traps.h>
  2. #include <Sound.h>
  3. #include "main.h"
  4.  
  5.  
  6.  
  7.  
  8.  
  9. long            gRealSysBeep;
  10. long            gRealSystemTask;
  11. short            gSysBeepID;
  12. Handle            gSndHandle;
  13. SndChannelPtr    gSndChannel;
  14. short            gSndLevel;
  15.  
  16.  
  17.  
  18.  
  19.  
  20. void main(void)
  21. {
  22.     asm
  23.     {
  24.         move.l    A4,-(SP)            ;save A4
  25.         lea        main,A4                ;setup globals
  26.         move.l    A4,A0                ;put ptr to code into A0
  27.         dc.w    _RecoverHandle        ;recover handle to my code
  28.         move.l    A0,-(SP)            ;put handle to code onto stack
  29.         move.l    A0,-(SP)            ;put handle to code onto stack
  30.         dc.w    _DetachResource        ;detach my handle to code
  31.         move.l    (SP)+,A0            ;put handle to code into A0
  32.         dc.w    _HLock                ;lock my handle to code
  33.     }
  34.     
  35.     
  36.     gSndChannel = 0L;
  37.     gSndHandle = 0L;
  38.     gSysBeepID = 0;
  39.     gSndLevel = 0;
  40.     
  41.     gRealSysBeep = NGetTrapAddress(_SysBeep,GetTrapType(_SysBeep));
  42.     NSetTrapAddress((long)MySysBeep,_SysBeep,GetTrapType(_SysBeep));
  43.     gRealSystemTask = NGetTrapAddress(_SystemTask,GetTrapType(_SystemTask));
  44.     NSetTrapAddress((long)MySystemTask,_SystemTask,GetTrapType(_SystemTask));
  45.     
  46.     
  47.     asm
  48.     {
  49.         move.l    (SP)+,A4            ;restore A4
  50.     }
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57. pascal void MySysBeep(short duration)
  58. {
  59.     asm
  60.     {
  61.         move.l    A4,-(SP)            ;save A4
  62.         lea        main,A4                ;restore globals
  63.         
  64.         move.b    sdVolume,-(SP)        ;save speaker volume
  65.         move.b    WWExist,-(SP)        ;save InitWindows flag
  66.         clr.b    sdVolume            ;set speaker volume to zero
  67.         move.b    #0xFF,WWExist        ;set InitWindows flag to non-inited
  68.         
  69.         move.w    duration,-(SP)        ;put duration on the stack
  70.         move.l    gRealSysBeep,A0        ;put real SysBeep address in A0
  71.         jsr        (A0)                ;jump to real SysBeep for compatibility
  72.         
  73.         move.b    (SP)+,WWExist        ;restore InitWindows flag
  74.         move.b    (SP)+,sdVolume        ;restore speaker volume
  75.         
  76.         move.l    ExpandMem,A0        ;put address of some system global ptr into A0
  77.         movea.l    272(A0),A0            ;put address of some other system global ptr into A0
  78.         tst.w    24(A0)                ;test SysBeep enable flag
  79.         beq.s    @end
  80.         
  81.         clr.w    -(SP)                ;put arg for DoSysBeep on stack
  82.         movea.l    A7,A0                ;put address in A0 for result of ReadXPram
  83.         move.l    #0x0002007C,D0        ;setup ReadXPram call for SysBeep ID
  84.         dc.w    _ReadXPRam            ;read SysBeep ID from extended pram
  85.         
  86.         jsr        DoSysBeep            ;do the beeping
  87.         addq    #2,SP                ;clean up stack
  88.         
  89. @end    move.l    (SP)+,A4            ;restore A4
  90.     }
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97. void DoSysBeep(short beepRsrcID)
  98. {
  99.     SndCommand    cmd;
  100.     
  101.     
  102.     if (!gSndHandle || !gSndChannel || (beepRsrcID != gSysBeepID))
  103.         LoadNewSysBeep(beepRsrcID);
  104.     
  105.     if (gSndHandle&&gSndChannel)
  106.         if (!SndPlay(gSndChannel,gSndHandle,true))
  107.         {
  108.             gSndLevel++;
  109.             
  110.             cmd.cmd = callBackCmd;
  111.             if (SndDoCommand(gSndChannel,&cmd,false))
  112.                 gSndLevel--;
  113.         }
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120. void LoadNewSysBeep(short beepRsrcID)
  121. {
  122.     Byte    resLoadSave;
  123.     THz        zoneSave;
  124.     short    err;
  125.     
  126.     
  127.     gSysBeepID = beepRsrcID;
  128.     
  129.     zoneSave = GetZone();
  130.     SetZone(SystemZone());
  131.     
  132.     resLoadSave = GetResLoad();
  133.     SetResLoad(true);
  134.     
  135.     
  136.     JunkOldSysBeep();
  137.     gSndLevel = 0;
  138.     
  139.     err = SndNewChannel(&gSndChannel,0,0,(ProcPtr)SndPlayCompletion);
  140.     if (err)
  141.         gSndChannel = 0L;
  142.     else
  143.     {
  144.         gSndHandle = RGetResource('snd ',beepRsrcID);
  145.         if (gSndHandle)
  146.             HLock(gSndHandle);
  147.     }
  148.     
  149.     
  150.     SetResLoad(resLoadSave);
  151.     SetZone(zoneSave);
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158. void JunkOldSysBeep(void)
  159. {
  160.     if (gSndChannel)
  161.     {
  162.         SndDisposeChannel(gSndChannel,true);
  163.         gSndChannel = 0L;
  164.     }
  165.     
  166.     if (gSndHandle)
  167.     {
  168.         HUnlock(gSndHandle);
  169.         ReleaseResource(gSndHandle);
  170.         gSndHandle = 0L;
  171.     }
  172. }
  173.  
  174.  
  175.  
  176.  
  177.  
  178. pascal void SndPlayCompletion(SndChannelPtr chan,SndCommand cmd)
  179. {
  180.     asm
  181.     {
  182.         move.l    A4,-(SP)            ;save A4
  183.         lea        main,A4                ;restore globals
  184.     }
  185.         
  186.     
  187.     
  188.     if (gSndLevel)
  189.         gSndLevel--;
  190.     
  191.     
  192.     asm
  193.     {
  194.         move.l    (SP)+,A4            ;restore A4
  195.     }
  196. }
  197.  
  198.  
  199.  
  200.  
  201.  
  202. pascal void MySystemTask(void)
  203. {
  204.     asm
  205.     {
  206.         clr.l    -(SP)                    ;reserve space for real SystemTask return address
  207.         move.l    A4,-(SP)                ;save A4
  208.         lea        main,A4                    ;restore globals
  209.         move.l    gRealSystemTask,4(A7)    ;put address of real SystemTask in reserved space
  210.     }
  211.         
  212.     
  213.     if (gSndLevel == 0)
  214.         JunkOldSysBeep();
  215.     
  216.     
  217.     asm
  218.     {
  219.         move.l    (SP)+,A4                ;restore A4
  220.     }
  221. }