home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / MIDIMON / CALLBACK.C_ / CALLBACK.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  4.0 KB  |  134 lines

  1. /* callback.c - Contains the low-level MIDI input callback function for
  2.  *      MIDIMon.  This module also contains the LibMain() and WEP() 
  3.  *      DLL routines, and other functions accessed by the callback.
  4.  *
  5.  *      Because this module contains a low-level callback function,
  6.  *      this entire module must reside in a FIXED code segment in a DLL.
  7.  *      The data segment must be FIXED as well, since it accessed by
  8.  *      the callback.
  9.  */
  10.  
  11. #include <windows.h>
  12. #include <mmsystem.h>
  13. #include "midimon.h"
  14. #include "circbuf.h"
  15. #include "instdata.h"
  16. #include "callback.h"
  17.  
  18. static EVENT event;
  19.     
  20. /*
  21.  *  LibMain - Generic for a DLL.  Just initializes a little local memory.
  22.  */
  23. int FAR PASCAL LibMain (hInstance, wDataSeg, wHeapSize, lpCmdLine)
  24. HANDLE hInstance ;
  25. WORD   wDataSeg, wHeapSize ;
  26. LPSTR  lpCmdLine ;
  27. {
  28.     // Nothing to do - SDK Libentry does the LocalInit
  29.  
  30.     return TRUE;    
  31. }
  32.  
  33. /*
  34.  *  WEP - Generic for a DLL.  Doesn't do a whole lot.
  35.  */
  36. void FAR PASCAL _WEP(wParam)
  37. WORD wParam;
  38. {
  39. }
  40.  
  41.  
  42. /* midiInputHandler - Low-level callback function to handle MIDI input.
  43.  *      Installed by midiInOpen().  The input handler takes incoming
  44.  *      MIDI events and places them in the circular input buffer.  It then
  45.  *      notifies the application by posting a MM_MIDIINPUT message.
  46.  *
  47.  *      This function is accessed at interrupt time, so it should be as 
  48.  *      fast and efficient as possible.  You can't make any
  49.  *      Windows calls here, except PostMessage().  The only Multimedia
  50.  *      Windows call you can make are timeGetSystemTime(), midiOutShortMsg().
  51.  *      
  52.  *
  53.  * Param:   hMidiIn - Handle for the associated input device.
  54.  *          wMsg - One of the MIM_***** messages.
  55.  *          dwInstance - Points to CALLBACKINSTANCEDATA structure.
  56.  *          dwParam1 - MIDI data.
  57.  *          dwParam2 - Timestamp (in milliseconds)
  58.  *
  59.  * Return:  void
  60.  */     
  61. void FAR PASCAL midiInputHandler(
  62. HMIDIIN hMidiIn, 
  63. WORD wMsg, 
  64. DWORD dwInstance, 
  65. DWORD dwParam1, 
  66. DWORD dwParam2)
  67. {
  68.     switch(wMsg)
  69.     {
  70.         case MIM_OPEN:
  71.             break;
  72.  
  73.         /* The only error possible is invalid MIDI data, so just pass
  74.          * the invalid data on so we'll see it.
  75.          */
  76.         case MIM_ERROR:
  77.         case MIM_DATA:
  78.             event.dwDevice = ((LPCALLBACKINSTANCEDATA)dwInstance)->dwDevice;
  79.             event.data = dwParam1;
  80.             event.timestamp = dwParam2;
  81.             
  82.             /* Send the MIDI event to the MIDI Mapper, put it in the
  83.              * circular input buffer, and notify the application that
  84.              * data was received.
  85.              */
  86.             if(((LPCALLBACKINSTANCEDATA)dwInstance)->hMapper)
  87.                 midiOutShortMsg( 
  88.                             ((LPCALLBACKINSTANCEDATA)dwInstance)->hMapper, 
  89.                               dwParam1);
  90.  
  91.             PutEvent(((LPCALLBACKINSTANCEDATA)dwInstance)->lpBuf,
  92.                        (LPEVENT) &event); 
  93.  
  94.             PostMessage(((LPCALLBACKINSTANCEDATA)dwInstance)->hWnd,
  95.                           MM_MIDIINPUT, 0, 0L);
  96.  
  97.             break;
  98.  
  99.         default:
  100.             break;
  101.     }
  102. }
  103.  
  104. /* PutEvent - Puts an EVENT in a CIRCULARBUFFER.  If the buffer is full, 
  105.  *      it sets the wError element of the CIRCULARBUFFER structure 
  106.  *      to be non-zero.
  107.  *
  108.  * Params:  lpBuf - Points to the CIRCULARBUFFER.
  109.  *          lpEvent - Points to the EVENT.
  110.  *
  111.  * Return:  void
  112. */
  113. void FAR PASCAL PutEvent(LPCIRCULARBUFFER lpBuf, LPEVENT lpEvent)
  114. {
  115.     /* If the buffer is full, set an error and return. 
  116.      */
  117.     if(lpBuf->dwCount >= lpBuf->dwSize){
  118.         lpBuf->wError = 1;
  119.         return;
  120.     }
  121.     
  122.     /* Put the event in the buffer, bump the head pointer and the byte count.
  123.      */
  124.     *lpBuf->lpHead = *lpEvent;
  125.     
  126.     ++lpBuf->lpHead;
  127.     ++lpBuf->dwCount;
  128.  
  129.     /* Wrap the head pointer, if necessary.
  130.      */
  131.     if(lpBuf->lpHead >= lpBuf->lpEnd)
  132.         lpBuf->lpHead = lpBuf->lpStart;
  133. }
  134.