home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mmtest.zip / MMTEST.C < prev    next >
Text File  |  1994-04-22  |  9KB  |  247 lines

  1. // mmtest.cpp -- test program for multimedia
  2.  
  3. // Copyright (C) 1994 Joel Barnum, Descriptor Systems
  4. //
  5. // Compuserve: 70047,442
  6. // Internet:   jbarnum@ins.infonet.net
  7.  
  8. // This program attempts to load the MMPM/2 DLL and
  9. // if successful, run-time links to a couple of APIs.
  10. // If not successful, then multimedia services are
  11. // not available.
  12. //
  13. // To test the program, compile and link it with
  14. // the provided makefile. Copy a WAV file to the
  15. // current directory and name it TEST.WAV. Then run
  16. // the program and choose Play from the Sound menu.
  17.  
  18. // Note: the program should also ensure that there
  19. // is a wave audio device installed, but I haven't
  20. // yet figured out how to do that!
  21.  
  22. #define  INCL_WIN
  23. #define  INCL_GPI
  24. #define  INCL_DOS
  25. #include <os2.h>
  26. #include <os2me.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include "mmtest.h"
  30.  
  31. // global variables
  32. // define pointers to APIs that we'll call
  33. ULONG (* APIENTRY pmciSendString)   (PSZ      pszCommandBuf,
  34.                                  PSZ      pszReturnString,
  35.                                  USHORT   wReturnLength,
  36.                                  HWND     hwndCallBack,
  37.                                  USHORT   usUserParm );
  38. ULONG (* APIENTRY pmciGetErrorString)(ULONG   ulError,
  39.                                  PSZ     pszBuffer,
  40.                                  USHORT  usLength);
  41.  
  42. HAB  hab;
  43.  
  44. // Internal function prototypes
  45.  
  46. MRESULT EXPENTRY MyWindowProc( HWND hwnd, ULONG msg
  47.                    , MPARAM mp1, MPARAM mp2 );
  48. void error ( ULONG dwError, int nLine );
  49.  
  50.  
  51. int main ( int argc, char *argv[]  )
  52. {
  53.     HMQ  hmq;               // Message queue handle
  54.     HWND hwndFrame;         // Frame window handle
  55.     HWND hwndClient;        // Client window handle
  56.     QMSG qmsg;              // Message from queue
  57.     ULONG flCreate;         // Window creation flags
  58.     BOOL  fSuccess;         // return from API
  59.  
  60.     hab = WinInitialize ( 0 );
  61.     hmq = WinCreateMsgQueue ( hab, 0 );
  62.  
  63.     fSuccess = WinRegisterClass ( hab, "mmtest"
  64.                 , MyWindowProc, 0, 0 );
  65.  
  66.     flCreate = FCF_SYSMENU | FCF_SIZEBORDER | FCF_TITLEBAR |
  67.                FCF_MINMAX  | FCF_SHELLPOSITION | FCF_TASKLIST |
  68.                FCF_ICON    | FCF_MENU;
  69.  
  70.     hwndFrame = WinCreateStdWindow ( HWND_DESKTOP, WS_VISIBLE
  71.                 , &flCreate, "mmtest"
  72.                 , "Test MMPM/2", 0L, 0, IDF_MAIN
  73.                 , &hwndClient );
  74.  
  75.     while ( WinGetMsg ( hab, &qmsg, NULLHANDLE, 0, 0 ) )
  76.         WinDispatchMsg  ( hab, &qmsg );
  77.  
  78.     fSuccess = WinDestroyWindow ( hwndFrame );
  79.     fSuccess = WinDestroyMsgQueue ( hmq );
  80.     fSuccess = WinTerminate ( hab );
  81.     return 0;
  82. }
  83. //************************************************************
  84. MRESULT EXPENTRY MyWindowProc ( HWND hwnd, ULONG msg
  85.                               , MPARAM mp1, MPARAM mp2 )
  86. {
  87.  
  88.     switch( msg )
  89.     {
  90.         case WM_CREATE:
  91.             {
  92.                 APIRET  rc;
  93.                 HWND    hwndMenu;
  94.                 CHAR    ach[255];       // for DosLoadModule
  95.                 HMODULE hmodMMPM;       // multimedia PM DLL
  96.  
  97.               // attempt to load the MMPM DLL
  98.                 rc = DosLoadModule ( ach, sizeof ( ach )
  99.                         , "MDM", &hmodMMPM );
  100.                 if ( rc != 0 )
  101.                 {
  102.                     WinMessageBox ( HWND_DESKTOP
  103.                             , hwnd
  104.                             , "Multimedia PM not installed.\n"
  105.                               "No sound will be played"
  106.                             , "Warning"
  107.                             , 0
  108.                             , MB_OK | MB_WARNING | MB_MOVEABLE );
  109.  
  110.                   // disable menu item
  111.                     hwndMenu = WinWindowFromID (
  112.                             WinQueryWindow ( hwnd, QW_PARENT ), FID_MENU );
  113.                     WinEnableMenuItem ( hwndMenu, IDM_PLAY, FALSE );
  114.                 }
  115.                 else
  116.                 {
  117.                   // find MMPM API address
  118.                     rc = DosQueryProcAddr ( hmodMMPM, 0, "mciSendString"
  119.                                 , (PFN *)&pmciSendString );
  120.                     if ( rc != 0 )
  121.                     {
  122.                         WinMessageBox ( HWND_DESKTOP
  123.                                 , hwnd
  124.                                 , "Unable to query proc addr.\n"
  125.                                   "No sound will be played"
  126.                                 , "Warning"
  127.                                 , 0
  128.                                 , MB_OK | MB_WARNING | MB_MOVEABLE );
  129.  
  130.                       // disable menu item
  131.                         hwndMenu = WinWindowFromID (
  132.                                 WinQueryWindow ( hwnd, QW_PARENT ), FID_MENU );
  133.                         WinEnableMenuItem ( hwndMenu, IDM_PLAY, FALSE );
  134.                     }
  135.  
  136.                   // find MMPM API address
  137.                     rc = DosQueryProcAddr ( hmodMMPM, 0, "mciGetErrorString"
  138.                                 , (PFN *)&pmciGetErrorString );
  139.                     if ( rc != 0 )
  140.                     {
  141.                         WinMessageBox ( HWND_DESKTOP
  142.                                 , hwnd
  143.                                 , "Unable to query proc addr.\n"
  144.                                   "No sound will be played"
  145.                                 , "Warning"
  146.                                 , 0
  147.                                 , MB_OK | MB_WARNING | MB_MOVEABLE );
  148.  
  149.                       // disable menu item
  150.                         hwndMenu = WinWindowFromID (
  151.                                 WinQueryWindow ( hwnd, QW_PARENT ), FID_MENU );
  152.                         WinEnableMenuItem ( hwndMenu, IDM_PLAY, FALSE );
  153.                     }
  154.                 }
  155.             }
  156.             return 0;
  157.  
  158.         case WM_COMMAND:
  159.             switch ( SHORT1FROMMP (mp1) )
  160.             {
  161.                 ULONG   dwError;            // return from API
  162.  
  163.                 case IDM_PLAY:
  164.                   // open the TEST.WAV file
  165.                     dwError = pmciSendString (
  166.                           "open test.wav alias test shareable wait"
  167.                         , NULL
  168.                         , 0
  169.                         , 0
  170.                         , 0 );
  171.                     if ( dwError != 0 )
  172.                         error ( dwError, __LINE__ - 2 );
  173.  
  174.                   // play it
  175.                     dwError = pmciSendString (
  176.                         "play test from 1 notify", NULL, 0, hwnd, 0 );
  177.                     if ( dwError != 0 )
  178.                         error ( dwError, __LINE__ - 2 );
  179.                     break;
  180.             }
  181.             return 0L;
  182.  
  183.         case MM_MCINOTIFY:
  184.           // MMPM notifies us when the play is finished or
  185.           // if there was an error
  186.             if ( SHORT1FROMMP (mp1) == MCI_NOTIFY_SUCCESSFUL )
  187.             {
  188.                 ULONG   dwError;
  189.                 dwError = pmciSendString (
  190.                         "close test wait", NULL, 0, 0, 0 );
  191.                 if ( dwError != 0 )
  192.                     error ( dwError, __LINE__ - 2 );
  193.                 else
  194.                     WinMessageBox ( HWND_DESKTOP, hwnd, "All done"
  195.                         , "Info", 0, MB_OK | MB_MOVEABLE );
  196.             }
  197.             else
  198.             {
  199.                 char    sz[255];        // temp string
  200.                 switch ( SHORT1FROMMP ( mp1 ) )
  201.                 {
  202.                     case MCI_NOTIFY_ABORTED:
  203.                         strcpy ( sz, "MCI_NOTIFY_ABORTED" );
  204.                         break;
  205.  
  206.                     default:
  207.                         strcpy ( sz, "Unkown error" );
  208.                         break;
  209.                 }
  210.  
  211.                 WinMessageBox ( HWND_DESKTOP, hwnd, sz, "Info", 0, MB_OK );
  212.             }
  213.             return 0L;
  214.  
  215.         case WM_CLOSE:
  216.             WinPostMsg ( hwnd, WM_QUIT, 0L, 0L );
  217.             return (MRESULT)NULL;
  218.  
  219.         case WM_PAINT:
  220.             {
  221.                 HPS hps = WinBeginPaint ( hwnd, NULLHANDLE, NULL );
  222.                 GpiErase ( hps );
  223.                 WinEndPaint ( hps );
  224.             }
  225.             return (MRESULT)NULL;
  226.  
  227.         default:
  228.             return WinDefWindowProc ( hwnd, msg, mp1, mp2 );
  229.     }
  230. }
  231.  
  232. //******************************************************************
  233. void error ( ULONG dwError, int nLine )
  234. {
  235.         char    szErr[255];         // error string
  236.         char    szTitle[255];       // title string
  237.         char    sz2[25];            // temp string
  238.  
  239.  
  240.     pmciGetErrorString ( dwError, szErr, sizeof (szErr) );
  241.     strcpy ( szTitle, "Error in line " );
  242.     _itoa ( nLine, sz2, 10 );
  243.     strcat ( szTitle, sz2 );
  244.     WinMessageBox ( HWND_DESKTOP, HWND_DESKTOP, szErr, szTitle, 0, MB_OK );
  245.  
  246. }
  247.