home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / mm / tuner / tuner.c next >
Text File  |  1999-05-11  |  34KB  |  907 lines

  1.  
  2.  /***************************************************************************
  3.  *                                                                          *
  4.  * File Name        : TUNER.C                                               *
  5.  *                                                                          *
  6.  * Description      : This program provides sample code for using the       *
  7.  *                    Multimedia API's related to overlay cards that        *
  8.  *                    contain tuners.                                       *
  9.  *                                                                          *
  10.  *                                                                          *
  11.  * Concepts         :                                                       *
  12.  *                                                                          *
  13.  * Entry Points     : CheckArg()                                            *
  14.  *                    GetOverlayColor()                                     *
  15.  *                    CloseTv()                                             *
  16.  *                    InitTv()                                              *
  17.  *                    WmPaint()                                             *
  18.  *                    SizeClient()                                          *
  19.  *                    MainDialogProc()                                      *
  20.  *                    main()                                                *
  21.  *                                                                          *
  22.  * Copyright        : COPYRIGHT IBM CORPORATION, 1991, 1992, 1993           *
  23.  *                                                                          *
  24.  *       DISCLAIMER OF WARRANTIES.  The following [enclosed] code is        *
  25.  *       sample code created by IBM Corporation. This sample code is not    *
  26.  *       part of any standard or IBM product and is provided to you solely  *
  27.  *       for  the purpose of assisting you in the development of your       *
  28.  *       applications.  The code is provided "AS IS", without               *
  29.  *       warranty of any kind.  IBM shall not be liable for any damages     *
  30.  *       arising out of your use of the sample code, even if they have been *
  31.  *       advised of the possibility of such damages.                        *
  32.  *                                                                          *
  33.  ****************************************************************************/
  34.  
  35. #define  INCL_DOS                   /* required to use Dos APIs.           */
  36. #define  INCL_WIN                   /* required to use Win APIs.           */
  37. #define  INCL_PM                    /* required to use PM APIs.            */
  38. #define  INCL_OS2MM                 /* required for MCI and MMIO headers   */
  39.  
  40. #include <os2.h>
  41. #include <os2me.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45.  
  46. #include "tuner.h"                  /* Dialog Ids, Resource Ids etc        */
  47.  
  48.  
  49. /****************************************************************************
  50.  *                                                                          *
  51.  * Name          : CheckArg                                                 *
  52.  *                                                                          *
  53.  * Description   : Reads the command line for an alternate digital video    *
  54.  *                 device.                                                  *
  55.  *                                                                          *
  56.  *                                                                          *
  57.  * Parameters    : hwnd    -  Client window handle                          *
  58.  *                 pad     -  Application data                              *
  59.  *                                                                          *
  60.  * Return        : ULONG   - 0 = Success, 1 = Fail                          *
  61.  *                                                                          *
  62.  ****************************************************************************/
  63. void CheckArg( PSZ      pszArg,
  64.                 PAPPDATA pad )
  65. {
  66.  
  67.     /*
  68.      * The Device Name has been passed ( eg DigitalVideo02 )
  69.      */
  70.     if ( !strncmp( pszArg, "/d=", 3 )
  71.       || !strncmp( pszArg, "/D=", 3 ) )
  72.         strcpy( pad->szDevice, &pszArg[3] );
  73.  
  74. } /* endfunc CheckArg */
  75.  
  76.  
  77.  
  78.  
  79. /****************************************************************************
  80.  *                                                                          *
  81.  * Name          : GetOverlayColor                                          *
  82.  *                                                                          *
  83.  * Description   : Gets the current overlay color from the device.          *
  84.  *                                                                          *
  85.  * Concept       : The overlay color is what the tuner card uses as a key   *
  86.  *                 to determine where to overlay its video output. This     *
  87.  *                 color can be changed from the Multimedia Setup program.  *
  88.  *                                                                          *
  89.  *                                                                          *
  90.  * Parameters    : hwnd    -  Client window handle                          *
  91.  *                 pad     -  Application data                              *
  92.  *                                                                          *
  93.  * Return        : Value of overlay color                                   *
  94.  *                                                                          *
  95.  ****************************************************************************/
  96. LONG  GetOverlayColor( HWND hwnd,
  97.                        PSZ  pszDevice )
  98. {
  99.  
  100.     USHORT      usUserParm       = (USHORT) NULL;
  101.     ULONG       ulError;
  102.     CHAR        szCommand[100],
  103.                 szReturn[100],
  104.                 szError[100];
  105.     USHORT      usReservedLength = 100;
  106.     HDC         hdcWin;
  107.     HPS         hpsWin;
  108.     LONG        aulColors[256];
  109.     LONG        lTransColor     = 5,
  110.                 lIndexColor     = 5,
  111.                 lSysColors      = 256,
  112.                 lIndex;
  113.  
  114.  
  115.     /*
  116.      * Query the device overlay Color and match the physical color table
  117.      * entry against logical color entry. This value is returned and stored
  118.      * in the gloabal overlay color var.
  119.      */
  120.     sprintf( szCommand, "status tv1 transparent color wait" );
  121.     ulError = mciSendString( szCommand,
  122.                              szReturn,
  123.                              usReservedLength,
  124.                              hwnd,
  125.                              usUserParm );
  126.     if ( ulError )
  127.     {
  128.         sprintf( szError,
  129.                  "No Overlay Color was returned for the device selected\n"
  130.                  "Return Code = %d",
  131.                  ulError );
  132.  
  133.         WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
  134.                        szError, "MCI Error",
  135.                        ID_MSGBOX, MB_OK | MB_ERROR | MB_MOVEABLE);
  136.  
  137.     } /* endif */
  138.     else
  139.     {
  140.         /* Convert the MCI Physical Color Table value */
  141.         lIndex = atol(szReturn);
  142.  
  143.         /*
  144.          * Calculate the Logical Color Table entry to be used as the
  145.          * GPI overlay Color
  146.          */
  147.         hdcWin = WinOpenWindowDC( hwnd );
  148.         DevQueryCaps( hdcWin, CAPS_COLORS, 1L, &lSysColors );
  149.  
  150.         hpsWin = WinGetPS( hwnd );
  151.  
  152.         if ( lSysColors <= 256 )
  153.             GpiQueryRealColors( hpsWin,
  154.                                 0,        /* LCOLOPT_INDEX */
  155.                                 0,
  156.                                 lSysColors,
  157.                                 ( LONG * )aulColors );
  158.  
  159.         if ( lIndex >= lSysColors )
  160.            lTransColor = lSysColors;
  161.  
  162.         switch ( lSysColors )
  163.         {
  164.              case 16:
  165.              case 256:
  166.                 lTransColor = aulColors[lIndex];
  167.                 break;
  168.  
  169.              case 65536:
  170.                 lTransColor = ( ( (lIndex&0xf800) << 8 ) +
  171.                                  ( (lIndex&0x07e0) << 5 ) +
  172.                                  ( (lIndex&0x001f) << 3 ) );
  173.                 break;
  174.  
  175.              case 16777216:
  176.                 lTransColor = lIndex;
  177.                 break;
  178.         } /* endswitch */
  179.  
  180.         /*
  181.          * Query the Index version on the RGB derived
  182.          */
  183.         lIndexColor = GpiQueryColorIndex( hpsWin, 0L, lTransColor );
  184.  
  185.         WinReleasePS( hpsWin );
  186.     } /* endelse */
  187.  
  188.     return( lIndexColor );
  189.  
  190. } /* endfunc GetOverlayColor */
  191.  
  192.  
  193. /****************************************************************************
  194.  *                                                                          *
  195.  * Name          : CloseTv                                                  *
  196.  *                                                                          *
  197.  * Description   : Closes the digital video device.                         *
  198.  *                                                                          *
  199.  *                                                                          *
  200.  * Parameters    : hwnd    -  Client window handle                          *
  201.  *                 pad     -  Application data                              *
  202.  *                                                                          *
  203.  * Return        : None.                                                    *
  204.  *                                                                          *
  205.  ****************************************************************************/
  206. VOID CloseTv( HWND     hwnd,
  207.               PAPPDATA pad )
  208. {
  209.     CHAR        szCommand[100],
  210.                 szReturn[100];
  211.     USHORT      usReserved = 100,
  212.                 usUserParm = 0;
  213.  
  214.  
  215.     sprintf( szCommand, "close tv1 wait" );
  216.     mciSendString( szCommand,
  217.                    szReturn,
  218.                    usReserved,
  219.                    hwnd,
  220.                    usUserParm );
  221.  
  222. } /* endfunc CloseTv */
  223.  
  224.  
  225. /****************************************************************************
  226.  *                                                                          *
  227.  * Name          : InitTv                                                   *
  228.  *                                                                          *
  229.  * Description   : Opens the appropriate digital video device.              *
  230.  *                                                                          *
  231.  * Concepts      : The appropriate digitalvideo device is opened and the    *
  232.  *                 monitor ( output ) window is set to the application      *
  233.  *                 window.                                                  *
  234.  *                                                                          *
  235.  * Parameters    : hwnd    -  Client window handle                          *
  236.  *                 pad     -  Application data                              *
  237.  *                                                                          *
  238.  * Return        : ULONG   - 0 = success, 1 = fail                          *
  239.  *                                                                          *
  240.  ****************************************************************************/
  241.  
  242. ULONG InitTv( HWND     hwnd,
  243.               PAPPDATA pad )
  244. {
  245.    CHAR        szCommand[100],
  246.                szReturn[100],
  247.                szEntry[100],
  248.                szError[100];
  249.    USHORT      usReserved = 100,
  250.                usUserParm = 0;
  251.    ULONG       ulError;
  252.  
  253.  
  254.     /*
  255.      * Open the required DigitalVideo Device
  256.      */
  257.     sprintf( szCommand,
  258.              "open %s alias tv1 shareable wait", pad->szDevice );
  259.     ulError = mciSendString( szCommand,
  260.                              szReturn,
  261.                              usReserved,
  262.                              hwnd,
  263.                              usUserParm );
  264.     if ( ulError )
  265.     {
  266.         sprintf( szError,
  267.                  "Could not open device: %s\nReturn Code = %u",
  268.                  pad->szDevice, ulError );
  269.  
  270.         WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
  271.                        szError, "MCI Error",
  272.                        ID_MSGBOX, MB_OK | MB_ERROR | MB_MOVEABLE );
  273.  
  274.         return( 1 );
  275.     } /* endif */
  276.  
  277.  
  278.    /*
  279.     * Make sure that the device has a tuner.
  280.     * Note: Some drivers don't allow sharing of the tuner even
  281.     *       when the device was opened in sharable mode. In such
  282.     *       a case, the second instance of the device will open
  283.     *       with no tuner capablility.
  284.     */
  285.  
  286.     sprintf( szCommand,
  287.              "capability tv1 has tuner wait" );
  288.  
  289.     ulError = mciSendString( szCommand,
  290.                              szReturn,
  291.                              usReserved,
  292.                              hwnd,
  293.                              usUserParm );
  294.  
  295.  
  296.     if ( strcmp( szReturn, "TRUE" ) != 0 )
  297.     {
  298.         sprintf( szError,
  299.                  "Could not find a tuner \nfor device: %s",
  300.                  pad->szDevice );
  301.  
  302.         WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
  303.                        szError, "MCI Error",
  304.                        ID_MSGBOX, MB_OK | MB_ERROR | MB_MOVEABLE );
  305.  
  306.         CloseTv( hwnd, pad );
  307.  
  308.         return( 1 );
  309.     } /* endif */
  310.  
  311.  
  312.     /* Query tuner device for highest tv channel.
  313.      */
  314.     sprintf( szCommand,
  315.              "status tv1 tuner high tv channel wait" );
  316.  
  317.     mciSendString( szCommand,
  318.                    szReturn,
  319.                    usReserved,
  320.                    hwnd,
  321.                    usUserParm );
  322.  
  323.     pad->ulHighChannel = atol( szReturn );
  324.  
  325.     /* Query tuner device for lowest tv channel
  326.      */
  327.     sprintf( szCommand,
  328.              "status tv1 tuner low tv channel wait" );
  329.  
  330.     mciSendString( szCommand,
  331.                    szReturn,
  332.                    usReserved,
  333.                    hwnd,
  334.                    usUserParm );
  335.  
  336.     pad->ulLowChannel = atol( szReturn );
  337.  
  338.     /* Query tuner device for current tv channel
  339.      */
  340.     sprintf( szCommand,
  341.              "status tv1 tuner tv channel wait" );
  342.  
  343.     mciSendString( szCommand,
  344.                    szReturn,
  345.                    usReserved,
  346.                    hwnd,
  347.                    usUserParm );
  348.  
  349.     pad->ulChannel = atol( szReturn );
  350.  
  351.     /* Set the text entry field to the current tv channel
  352.      */
  353.     WinSetDlgItemText ( hwnd, IDC_ENTRY, szReturn );
  354.  
  355.  
  356.     /* Query tuner device for highest tv channel.
  357.      */
  358.     sprintf( szCommand,
  359.              "status tv1 tuner high tv channel wait" );
  360.  
  361.     mciSendString( szCommand,
  362.                    szReturn,
  363.                    usReserved,
  364.                    hwnd,
  365.                    usUserParm );
  366.  
  367.     pad->ulHighChannel = atol( szReturn );
  368.  
  369.     /* Query tuner device for lowest tv channel
  370.      */
  371.     sprintf( szCommand,
  372.              "status tv1 tuner low tv channel wait" );
  373.  
  374.     mciSendString( szCommand,
  375.                    szReturn,
  376.                    usReserved,
  377.                    hwnd,
  378.                    usUserParm );
  379.  
  380.     pad->ulLowChannel = atol( szReturn );
  381.  
  382.     /* Query tuner device for current tv channel
  383.      */
  384.     sprintf( szCommand,
  385.              "status tv1 tuner tv channel wait" );
  386.  
  387.     mciSendString( szCommand,
  388.                    szReturn,
  389.                    usReserved,
  390.                    hwnd,
  391.                    usUserParm );
  392.  
  393.     pad->ulChannel = atol( szReturn );
  394.  
  395.     /* Set the text entry field to the current tv channel
  396.      */
  397.     WinSetDlgItemText ( hwnd, IDC_ENTRY, szReturn );
  398.  
  399.  
  400.     /*
  401.      * Set the client window as the monitor window
  402.     */
  403.     sprintf( szCommand,
  404.              "window tv1 monitor handle %u wait",
  405.              hwnd );
  406.     ulError = mciSendString( szCommand,
  407.                              szReturn,
  408.                              usReserved,
  409.                              hwnd,
  410.                              usUserParm );
  411.  
  412.     /*
  413.      * Turn on the monitor facility
  414.      */
  415.     sprintf( szCommand,
  416.              "set tv1 monitor on wait" );
  417.     ulError = mciSendString( szCommand,
  418.                              szReturn,
  419.                              usReserved,
  420.                              hwnd,
  421.                              usUserParm );
  422.  
  423.     /*
  424.      * Get the overlay color for this device to use as the color to
  425.      * paint the application window.
  426.      */
  427.     pad->ulOverlayColor = GetOverlayColor( hwnd, pad->szDevice );
  428.  
  429.     return( 0 );
  430. } /* endfunc InitTv */
  431.  
  432.  
  433.  
  434. /****************************************************************************
  435.  *                                                                          *
  436.  * Name          : WmPaint                                                  *
  437.  *                                                                          *
  438.  * Description   : Called when the window needs to be repainted.            *
  439.  *                                                                          *
  440.  * Concepts      : When the window is resized or the visible region         *                      *
  441.  *                 changes in any way, it needs to be repainted with        *
  442.  *                 the appropriate overlay color.                           *
  443.  *                                                                          *
  444.  * Parameters    : hwnd    -  Client window handle                          *
  445.  *                 pad     -  Application data                              *
  446.  *                                                                          *
  447.  * Return        : None.                                                    *
  448.  *                                                                          *
  449.  ****************************************************************************/
  450. VOID WmPaint ( PAPPDATA pad )
  451.  
  452. {
  453.     RECTL   rectl;
  454.     HPS     hps;
  455.  
  456.     /* Acquire a presentation space for the client window */
  457.     hps = WinGetPS( pad->hwndMovie );
  458.  
  459.     /* Get the visible rectangles for the client window */
  460.     WinQueryWindowRect( pad->hwndMovie, &rectl );
  461.     WinFillRect( hps, &rectl, pad->ulOverlayColor );
  462.  
  463. }  /* endfunc WmPaint */
  464.  
  465.  
  466.  
  467. /****************************************************************************
  468.  *                                                                          *
  469.  * Name          : SizeClient                                               *
  470.  *                                                                          *
  471.  *                                                                          *
  472.  * Description   : The first order of buisness is to make sure that the     *
  473.  *                 new size for the frame window leaves at least enough     *
  474.  *                 room for the control windows.                            *
  475.  *                                                                          *
  476.  *                 To account for the size of the button bar, it is         *
  477.  *                 assumed that the two buttons and the text entry field    *
  478.  *                 are of the same size and that they are aranged three     *
  479.  *                 accross and one high.                                    *
  480.  *                                                                          *
  481.  *                 Once the size of all of the control windows is accounted *
  482.  *                 for, the client/monitor window is resized to fit the     *
  483.  *                 remaining top part of the frame window. The client       *
  484.  *                 window is then painted with the overlay key color.       *
  485.  *                                                                          *
  486.  *                 Another potential problem is the fact that overlay       *
  487.  *                 cards cant handle negative numbers for screen            *
  488.  *                 coordinates. The way this program solves this is to      *
  489.  *                 prevent the frame window from moving past the left edge  *
  490.  *                 of the screen.                                           *
  491.  *                                                                          *
  492.  *                                                                          *
  493.  *                                                                          *
  494.  * Parameters    : hwnd    -  Client window handle                          *
  495.  *                 pad     -  Application data                              *
  496.  *                                                                          *
  497.  * Return        : None.                                                    *
  498.  *                                                                          *
  499.  ****************************************************************************/
  500.  
  501. VOID SizeClient( HWND hwnd, PAPPDATA pad )
  502. {
  503.    SWP               swp;                 /* Standard window rect       */
  504.    static LONG       cxWidthBorder,       /* Size of top border         */
  505.                      cyWidthBorder,       /* Size of side borders       */
  506.                      cyTitleBar;          /* Size of title bar          */
  507.    HWND              hwndButton;          /* handle for button window   */
  508.    SWP               swpButton;           /* button window position     */
  509.    ULONG             ulControlWindowCy;   /* Height of all control windows */
  510.    ULONG             ulControlWindowCx;   /* Width of all control windows  */
  511.  
  512.  
  513.    cxWidthBorder = WinQuerySysValue(HWND_DESKTOP, SV_CXSIZEBORDER);
  514.    cyWidthBorder = WinQuerySysValue(HWND_DESKTOP, SV_CYSIZEBORDER);
  515.    cyTitleBar    = WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR);
  516.  
  517.    /* Get handle for one of the button windows */
  518.    hwndButton = WinWindowFromID( hwnd,          /* handle of parent  */
  519.                                  IDC_UP );    /* ID of window      */
  520.  
  521.    /* Get the size of one of the button windows */
  522.    WinQueryWindowPos( hwndButton, &swpButton ); /* Get button position */
  523.  
  524.    ulControlWindowCx = swpButton.x + swpButton.cx;
  525.    ulControlWindowCy = swpButton.cy + 2 * cyWidthBorder + cyTitleBar;
  526.  
  527.    /* Get the size of the frame window */
  528.    WinQueryWindowPos( hwnd, &swp );
  529.  
  530.    /* Make sure that the frame window is not too narow */
  531.    if( swp.cy <= ulControlWindowCy )
  532.       swp.cy = ulControlWindowCy + 1;
  533.  
  534.    /* Make sure that the frame window is not to short */
  535.    if( swp.cx <= ulControlWindowCx )
  536.       swp.cx = ulControlWindowCx + 1;
  537.  
  538.    /* Set the frame window size */
  539.    WinSetWindowPos( hwnd,
  540.                     HWND_TOP,
  541.                     swp.x,
  542.                     swp.y,
  543.                     swp.cx,
  544.                     swp.cy,
  545.                     SWP_SIZE );
  546.  
  547.    /* Set the client window size and position to fit the top part of
  548.     * the frame window
  549.     */
  550.    WinSetWindowPos(
  551.       pad->hwndMovie,
  552.       HWND_TOP,
  553.       cxWidthBorder,
  554.       cyWidthBorder + swpButton.cy,
  555.       swp.cx - 2 * cxWidthBorder,
  556.       swp.cy - 2 * cyWidthBorder - swpButton.cy - cyTitleBar,
  557.       SWP_MOVE | SWP_SIZE );
  558.  
  559. } /* end SizeClient */
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566. /*******************************************************************************
  567.  * Name        : MainDialogProc
  568.  *
  569.  * Description : This function controls the main dialog box.  It will handle
  570.  *               received messages such as pushbutton notifications, and
  571.  *               entry field messages.
  572.  *
  573.  * Return      :
  574.  *
  575.  ******************************************************************************/
  576. MRESULT EXPENTRY MainDialogProc( HWND hwnd,
  577.                                  ULONG msg,
  578.                                  MPARAM mp1,
  579.                                  MPARAM mp2)
  580.  
  581. {
  582.    static PAPPDATA   pad;                       /* App data structure      */
  583.    CHAR              szCommand[100],            /* sent to MciSendString   */
  584.                      szReturn[100],             /* returned from MciSend.. */
  585.                      szEntry[10];               /* Entry field text        */
  586.    USHORT            usUserParm = (USHORT) NULL,
  587.                      usReservedLength = 100;
  588.    static ULONG      ulHeight,                  /* Size of desktop         */
  589.                      ulWidth;
  590.    PSWP              pswp;                      /* Standard window pos     */
  591.  
  592.    switch (msg)
  593.    {
  594.       case WM_INITDLG:
  595.          /* Get the pointer to application defined data area
  596.           */
  597.          pad = ( PAPPDATA )mp2;
  598.  
  599.          ulHeight = WinQuerySysValue( HWND_DESKTOP,
  600.                                       SV_CYSCREEN );
  601.  
  602.          ulWidth = WinQuerySysValue ( HWND_DESKTOP,
  603.                                       SV_CXSCREEN );
  604.  
  605.          break;
  606.  
  607.  
  608.       case WM_CONTROL:
  609.          /*
  610.           * If the channel text field has been manually changed
  611.           * then update the tuner channel to match.
  612.           */
  613.          if(  SHORT1FROMMP( mp1 ) == IDC_ENTRY )
  614.          {
  615.             /* Get text from entry field */
  616.             WinQueryWindowText( WinWindowFromID( hwnd, IDC_ENTRY ),
  617.                                  sizeof( szEntry ),
  618.                                  szEntry );
  619.  
  620.              pad->ulChannel = atol( szEntry );
  621.  
  622.             /* Make sure the selected channel is in range. If the
  623.              * Channel is too low, it is assumed that the user has
  624.              * not finished entering the channel and the program
  625.              * will wait for the next digit.
  626.              */
  627.             if( pad->ulChannel < pad->ulLowChannel )
  628.                break;
  629.  
  630.             /* If the selected channel is too high, the channel
  631.              * is set to the highest supported channel number
  632.              */
  633.             if( pad->ulChannel > pad->ulHighChannel )
  634.             {
  635.                pad->ulChannel = pad->ulHighChannel;
  636.                sprintf( szCommand,
  637.                         "%d",
  638.                         pad->ulChannel );
  639.  
  640.                WinSetDlgItemText ( hwnd, IDC_ENTRY, szCommand );
  641.             }
  642.  
  643.             sprintf( szCommand,
  644.                      "settuner tv1 tv channel %d wait",
  645.                      pad->ulChannel );
  646.  
  647.             mciSendString( szCommand,
  648.                            szReturn,
  649.                            usReservedLength,
  650.                            hwnd,
  651.                            usUserParm );
  652.  
  653.          } /* end if */
  654.          return 0;
  655.  
  656.       case WM_COMMAND:
  657.          switch ( SHORT1FROMMP ( mp1 ) )
  658.          {
  659.             /* The user has selected the down button so
  660.              * decrement the current channel.
  661.              */
  662.             case IDC_DOWN:
  663.                pad->ulChannel--;
  664.                break;
  665.  
  666.             /* The user has selected the up button so
  667.              * increment the current channel
  668.              */
  669.             case IDC_UP:
  670.                pad->ulChannel++;
  671.                break;
  672.  
  673.          } /* end WM_COMMAND switch */
  674.  
  675.          /*
  676.           * Make sure that the currently selected channel
  677.           * is within the valid range of channels.
  678.           */
  679.          if( pad->ulChannel < pad->ulLowChannel )
  680.             pad->ulChannel = pad->ulLowChannel;
  681.  
  682.          if( pad->ulChannel > pad->ulHighChannel )
  683.             pad->ulChannel = pad->ulHighChannel;
  684.  
  685.          /* Set the tuner to the selected channel
  686.           */
  687.          sprintf ( szCommand,
  688.                   "settuner tv1 tv channel %d wait",
  689.                   pad->ulChannel );
  690.  
  691.          mciSendString ( szCommand,
  692.                         szReturn,
  693.                         usReservedLength,
  694.                         hwnd,
  695.                         usUserParm );
  696.  
  697.          /* Update the text entry field with the updated channel.
  698.           */
  699.          sprintf ( szReturn,
  700.                   "%d",
  701.                   pad->ulChannel );
  702.  
  703.          WinSetDlgItemText ( hwnd, IDC_ENTRY, szReturn );
  704.  
  705.          return 0;
  706.  
  707.       case WM_ADJUSTFRAMEPOS:
  708.          /*
  709.           * Most overlay cards do not handle clipping for a window that
  710.           * is moved beyond the edges of the screen. This program handles
  711.           * this limitation by not allowing the frame window to be moved
  712.           * outside of the desktop. Another aproach is to resize the client
  713.           * window so that it contained by the desktop, even if the frame
  714.           * window is not. This would provide the illusion of allowing the
  715.           * window to be moved outside of the desktop.
  716.           */
  717.  
  718.          pswp = ( PSWP )mp1;
  719.  
  720.          /* Make sure that the window has not move past the left edge */
  721.          if( pswp->x < 0 )
  722.             pswp->x = 0;
  723.  
  724.          /* Make sure that the window has not move past the bottom edge */
  725.          if( pswp->y < 0 )
  726.             pswp->y = 0;
  727.  
  728.  
  729.          /* Make sure that the window has not moved past the top edge */
  730.          if( ( pswp->y + pswp->cy ) > ulHeight )
  731.             pswp->y = ulHeight - pswp->cy;
  732.  
  733.          /* Make sure that the window has not moved past the right edge */
  734.          if( ( pswp->x + pswp->cx) > ulWidth )
  735.             pswp->x = ulWidth - pswp->cx;
  736.          break;
  737.  
  738.  
  739.       case WM_PAINT:
  740.          /*
  741.           * Call the procedure to resize the client window. The actual
  742.           * painting of the client window is done later to allow the
  743.           * frame window time to paint all of the control windows.
  744.           */
  745.          if( pad->hwndMovie != 0 )
  746.             {
  747.             SizeClient( hwnd, pad );
  748.             WinPostMsg( hwnd, WM_PAINTMOVIE, 0L, 0L );
  749.             }
  750.          break;
  751.  
  752.       case WM_PAINTMOVIE:
  753.          /*
  754.           * Call the procedure to fill the client window with the
  755.           * overlay card's key color.
  756.           */
  757.          WmPaint( pad );
  758.          return 0;
  759.  
  760.       case WM_CLOSE:
  761.  
  762.          WinPostMsg( hwnd, WM_QUIT, 0L, 0L );
  763.          return 0;
  764.  
  765.    } /* end switch */
  766.  
  767.    /* Pass messages on to the frame window */
  768.    return ( WinDefDlgProc( hwnd, msg, mp1, mp2 ) );
  769.  
  770. }
  771.  
  772.  
  773.  /******************************************************************************
  774.  * Name         : main
  775.  *
  776.  * Description  : This function calls the Intialize procedure to prepare
  777.  *                everything for the program's operation, enters the
  778.  *                message loop, then call Finalize to shut everything down
  779.  *                when the program is terminated.
  780.  *
  781.  * Concepts     : None.
  782.  *
  783.  * MMPM/2 API's : None.
  784.  *
  785.  * Parameters   : argc - Number of parameters passed into the program.
  786.  *                argv - Command line parameters.
  787.  *
  788.  * Return       : None.
  789.  *
  790.  *************************************************************************/
  791. void main(int argc, char *argv[])
  792. {
  793.    HAB         hab;
  794.    HWND        hwndFrame;
  795.    HMQ         hmq;
  796.    QMSG        qmsg;
  797.    CHAR        szCommand[20];
  798.    ULONG       ulAllocFlags = PAG_COMMIT
  799.                             | PAG_READ
  800.                             | PAG_WRITE;
  801.    PAPPDATA    pad;
  802.    CHAR        chKey;
  803.  
  804.    /*
  805.     * Set up the window specific data and set some defaults.
  806.     * Note: The defaults set relate to the Hauppauge Celebrity card.
  807.     */
  808.    DosAllocMem ( ( PPVOID )&pad,
  809.                  sizeof( APPDATA ),
  810.                  ulAllocFlags );
  811.  
  812.    pad->ulConnector = 2;
  813.    strcpy( pad->szDevice, "digitalvideo02" );
  814.    pad->ulOverlayColor = 5;
  815.    pad->hwndMovie = 0;
  816.  
  817.  
  818.    /* Check if alternate device ordinal was passed from command line
  819.     */
  820.    if( argc > 1 )
  821.       CheckArg( ( PSZ )argv[1], pad );
  822.  
  823.  
  824.    /* Initialize PM */
  825.    hab = WinInitialize(0);
  826.  
  827.    /* create a message queue for the window */
  828.    hmq = WinCreateMsgQueue(hab,0);
  829.  
  830.    /* Load the Dialog - This will return the Handle to the Frame */
  831.    hwndFrame = WinLoadDlg( HWND_DESKTOP,
  832.                            HWND_DESKTOP,
  833.                            ( PFNWP )MainDialogProc,
  834.                            NULLHANDLE,
  835.                            ID_DIALOG,
  836.                            ( PVOID )pad);
  837.  
  838.    /* Create a client window that we can associate with the
  839.     * digitalvideo device. This will be the output window for
  840.     * the video overlay
  841.     */
  842.    WinRegisterClass( hab,
  843.                      "MovieWindow",
  844.                      (PFNWP) NULL,
  845.                      CS_SIZEREDRAW | CS_MOVENOTIFY,
  846.                      0 );
  847.  
  848.    pad->hwndMovie = WinCreateWindow( hwndFrame,
  849.                                 "MovieWindow",
  850.                                 NULL,
  851.                                 WS_VISIBLE,
  852.                                 0,
  853.                                 0,
  854.                                 0,
  855.                                 0,
  856.                                 hwndFrame,
  857.                                 HWND_TOP,
  858.                                 ID_MOVIEWINDOW,
  859.                                 NULL,
  860.                                 NULL );
  861.  
  862.    /* Update and size the client window
  863.     */
  864.    SizeClient( hwndFrame, pad );
  865.  
  866.    /* Initialise the overlay card.
  867.     */
  868.    if( InitTv ( pad->hwndMovie, pad ) == 0 )
  869.    {
  870.       /* Set dialog text field with startup value
  871.        */
  872.       sprintf ( szCommand,
  873.                "%d",
  874.                pad->ulChannel );
  875.  
  876.       WinSetDlgItemText ( hwndFrame, IDC_ENTRY, szCommand );
  877.  
  878.       /* Handle the messages: */
  879.       while( ( WinGetMsg( hab, &qmsg, (HWND) NULL, 0, 0 ) != FALSE )
  880.                && ( qmsg.msg != WM_QUIT ) )
  881.       {
  882.          /* Intercept WM_CHAR messages to filter non numeric characters
  883.           * This is done to keep non numeric characters from being passed
  884.           * to the text entry field
  885.           */
  886.          if( qmsg.msg == WM_CHAR )
  887.          {
  888.             chKey = SHORT1FROMMP( qmsg.mp2 );
  889.             if(  ( ( chKey > 57 ) || ( chKey < 48 ) ) &&
  890.                    ( SHORT2FROMMP( qmsg.mp2 ) != VK_BACKSPACE ) )
  891.                continue;
  892.          } /* end if */
  893.  
  894.          WinDispatchMsg( hab, &qmsg );
  895.  
  896.       }/* end while */
  897.  
  898.    }/* end if */
  899.  
  900.    CloseTv( hwndFrame, pad );
  901.    WinDestroyWindow( pad->hwndMovie );
  902.    WinDismissDlg( hwndFrame, TRUE );
  903.    WinDestroyMsgQueue( hmq );
  904.    WinTerminate( hab );
  905.  
  906. }/* end main */
  907.