home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / videotlk.zip / SAMPLES / EX2 / EX2.C next >
Text File  |  1999-01-26  |  16KB  |  468 lines

  1.  
  2. /*------------------------------------------------------------------*
  3.  *                                                                  *
  4.  *  Video Toolkit For OS/2 Version 2.0                              *
  5.  *  Example PM Application No. 2.                                   *
  6.  *  Date : 22/02/95.                                                *
  7.  *  Copyright (c) Abbotsbury Software Ltd., United Kingdom. 1995-98 *
  8.  *                                                                  *
  9.  *  Filename : ex2.c                                                *
  10.  *                                                                  *
  11.  *------------------------------------------------------------------*/
  12.  
  13. #define     INCL_PM
  14. #define     INCL_OS2MM
  15. #define     INCL_DOS
  16. #define     INCL_SW
  17.  
  18. #include    <os2.h>
  19. #include    <os2me.h>
  20. #include    <stdlib.h>
  21. #include    <stdio.h>
  22. #include    <string.h>
  23. #include    <vcadd.h>
  24. #include    <vcai.h>
  25. #include    "ex2.h"
  26. #include    "pmtv2rem.h"
  27. #include    "helpfunc.h"
  28. #include    "region.h"
  29.  
  30. extern  BOOL    RemOpen;
  31. extern  MRESULT EXPENTRY TuneDlgProc (HWND hwnd, ULONG msg,
  32.                                       MPARAM mp1, MPARAM mp2);
  33.  
  34. HAB     Hab;
  35. HWND    HwndFrame;
  36. DRIVERHANDLE    DevHandle;
  37. USHORT  DevInstance;
  38. CHAR    DevType[20];
  39. LONG    ColourKey;
  40. LONG    Colours[256];
  41. LONG    NumColours;
  42.  
  43. static  HWND    HwndClient, HwndRemote;
  44. static  VCADEVINFO  DevInfo;
  45. static  PSZ     RemoteName = "RemoteDlgProc";
  46. static  PFN     RemoteAddr;
  47. static  HMODULE HmodRemote;
  48. static  CHAR    txt[256];
  49. static  UCHAR   *Copyright =
  50.   "Copyright (c) Abbotsbury Software Ltd., UK. 1994-1998. All Rights Reserved";
  51.  
  52. int     main (int argc, char **argv);
  53. void    msg_box (UCHAR *txt, UCHAR *title, ULONG options);
  54. void    err_msg (UCHAR *mess);
  55. LONG    get_colour (HPS hps);
  56. MRESULT EXPENTRY ClientWndProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  57. MRESULT EXPENTRY QuitDlgProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  58. MRESULT EXPENTRY AboutDlgProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  59.  
  60. static  BOOL    init_window (void);
  61. static  void    init_colour (void);
  62. static  void    map_pallete (HPS hps);
  63.  
  64. int main (int argc, char **argv)
  65. {
  66.     HMQ     hmq;
  67.     QMSG    qmsg;
  68.     CHAR    mod_txt[256];
  69.     PSZ     pmmbase, env = "MMBASE";
  70.     LONG    len;
  71.     APIRET  rc;
  72.  
  73.         if (argc < 3)
  74.             DosExit (EXIT_PROCESS, 1);
  75.  
  76.         Hab = WinInitialize (0);
  77.         if (!Hab)
  78.             DosExit (EXIT_PROCESS, 1);
  79.  
  80.         hmq = WinCreateMsgQueue (Hab, 0);
  81.         if (!hmq)
  82.             DosExit (EXIT_PROCESS, 1);
  83.  
  84.         strcpy (DevType, argv[1]);
  85.         DevInstance = (USHORT)atoi (argv[2]);
  86.         DevHandle = VcaiDeviceOpen (DevType, DevInstance);
  87.         if (DevHandle < 0)
  88.         {
  89.             sprintf (txt, "Cannot Open Device %u !", atoi (argv[2]));
  90.             msg_box (txt, "Error", MB_OK | MB_ERROR);
  91.             DosExit (EXIT_PROCESS, 1);
  92.         }
  93.  
  94.         rc = DosScanEnv (env, &pmmbase);
  95.         if (rc != 0)
  96.             DosExit (EXIT_PROCESS, 1);
  97.         len = strlen (pmmbase);
  98.         if (pmmbase[len-1] == ';')
  99.             pmmbase[len-1] = '\0';
  100.         sprintf (mod_txt, "%s\\DLL\\PMTV2REM.DLL", pmmbase);
  101.         rc = DosLoadModule (txt, sizeof (txt), mod_txt, &HmodRemote);
  102.         if (rc != 0)
  103.             DosExit (EXIT_PROCESS, 1);
  104.         rc = DosQueryProcAddr (HmodRemote, 0L, RemoteName, &RemoteAddr);
  105.         if (rc != 0)
  106.             DosExit (EXIT_PROCESS, 1);
  107.  
  108.         if (!init_window ())
  109.             DosExit (EXIT_PROCESS, 1);
  110.  
  111.         while (WinGetMsg (Hab, &qmsg, NULLHANDLE, 0, 0))
  112.             WinDispatchMsg (Hab, &qmsg);
  113.  
  114.         DestroyHelpInstance ();
  115.         WinDestroyWindow (HwndFrame);
  116.         WinDestroyMsgQueue (hmq);
  117.         WinTerminate (Hab);
  118.         DosExit (EXIT_PROCESS, 0);
  119. }
  120.  
  121. void msg_box (UCHAR *txt, UCHAR *title, ULONG options)
  122. {
  123.         WinMessageBox (HWND_DESKTOP,
  124.                        HWND_DESKTOP,
  125.                        txt,
  126.                        title,
  127.                        IDD_MSG,
  128.                        options);
  129. }
  130.  
  131. void err_msg (UCHAR *mess)
  132. {
  133.     CHAR    txt[256];
  134.     ERRORID err;
  135.  
  136.         err = WinGetLastError (Hab);
  137.         sprintf (txt, "%s - %lx", mess,  err & 0xffff);
  138.         msg_box (txt, "Error", MB_OK | MB_ERROR);
  139. }
  140.  
  141.                         // The colour value for the Video Colour Key
  142.                         // is a physical palette value.
  143.                         // For systems with 256 or less colours in the
  144.                         // palette, you MUST map the physical palette
  145.                         // to PM logical palette first.
  146.                         // If there are more than 256 colours this
  147.                         // step should be ignored.
  148. LONG get_colour (HPS hps)
  149. {
  150.     LONG index;
  151.  
  152.         map_pallete (hps);
  153.  
  154.         index = VcaiColourkeyGet();
  155.         if (index > (ULONG)NumColours)
  156.         {
  157.             index = COLOUR_KEY_DEFAULT;
  158.             VcaiColourkeySet (index);
  159.         }
  160.         return (LONG)index;
  161. }
  162.  
  163. MRESULT EXPENTRY ClientWndProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  164. {
  165.     static  HWND        hwnd_menu;
  166.     static  HPOINTER    hptr_rem;
  167.     static  BOOL        minimize = FALSE;
  168.     HPS     hps;
  169.     RECTL   rcl;
  170.     LONG    bx, by, cy, x, y, width, height;
  171.     VCADEVAUDIO ad;
  172.     MOUSEPOS mousepos;
  173.     PSWP    pswp;
  174.  
  175.         switch(msg)
  176.         {
  177.             case WM_CREATE :
  178.                 RemOpen = FALSE;
  179.                 VcaiOverlay (TRUE);
  180.                 VcaiFreeze (FALSE);
  181.                 hwnd_menu = WinLoadMenu (hwnd, NULLHANDLE, IDR_MAIN);
  182.                 hptr_rem = WinLoadPointer (HWND_DESKTOP, NULLHANDLE,
  183.                                            PTR_REMOTE);
  184.                 init_colour ();
  185.                 break;
  186.             case WM_PAINT :
  187.                 hps = WinBeginPaint (hwnd, NULLHANDLE, &rcl);
  188.                 WinQueryWindowRect (hwnd, &rcl);
  189.                 ColourKey = get_colour (hps);
  190.                 if ((ULONG)NumColours <= 256)
  191.                     WinFillRect (hps, &rcl, Colours[ColourKey]);
  192.                 else
  193.                     WinFillRect (hps, &rcl, ColourKey);
  194.                 WinEndPaint (hps);
  195.                 break;
  196.             case MSG_SIZE_TV :
  197.             case WM_SIZE :
  198.             case WM_MOVE :
  199.             case WM_WINDOWPOSCHANGED :
  200.                 if (minimize)
  201.                     break;
  202.                 if (!WinQueryWindowRect (hwnd, &rcl))
  203.                     return 0;
  204.                 WinMapWindowPoints (hwnd, HWND_DESKTOP, (PPOINTL)&rcl, 2);
  205.                 bx = WinQuerySysValue (HWND_DESKTOP, SV_CXSIZEBORDER);
  206.                 by = WinQuerySysValue (HWND_DESKTOP, SV_CYSIZEBORDER);
  207.                 cy = WinQuerySysValue (HWND_DESKTOP, SV_CYSCREEN);
  208.                 x = rcl.xLeft + bx;
  209.                 y = cy - rcl.yTop - by;
  210.                 width = rcl.xRight - rcl.xLeft;
  211.                 height = rcl.yTop - rcl.yBottom;
  212.                 VcaiVideoRectValidate (0, 0, 0, 0, (USHORT)x, (USHORT)y,
  213.                                        (USHORT)width, (USHORT)height);
  214.                 if (RemOpen)
  215.                     WinSetActiveWindow (HWND_DESKTOP, HwndRemote);
  216.                 break;
  217.             case WM_MINMAXFRAME :
  218.                 pswp = (PSWP)PVOIDFROMMP (mp1);
  219.                 if (!(pswp->fl & SWP_MINIMIZE))
  220.                 {
  221.                     minimize = FALSE;
  222.                     break;
  223.                 }
  224.                 minimize = TRUE;
  225.                 VcaiVideoRectValidate (0, 0, 0, 0, 0, 0, 0, 0);
  226.                 break;
  227.             case WM_BUTTON1CLICK :
  228.                 if (RemOpen)
  229.                     return 0;
  230.                 WinQueryWindowRect (hwnd, & rcl);
  231.                 WinMapWindowPoints (hwnd, HWND_DESKTOP, (PPOINTL)&rcl, 2);
  232.                 mousepos.mp_x = MOUSEMSG (&msg)->x + rcl.xLeft;
  233.                 mousepos.mp_y = MOUSEMSG (&msg)->y + rcl.yBottom;
  234.                 init_audio ();
  235.                 init_video ();
  236.                 HwndRemote = WinLoadDlg (HWND_DESKTOP,
  237.                                          HWND_DESKTOP,
  238.                                          (PFNWP)RemoteAddr,
  239.                                          HmodRemote,
  240.                                          IDD_REM,
  241.                                          (PVOID)&mousepos);
  242.                 return 0;
  243.             case WM_BUTTON2CLICK :
  244.                 WinPopupMenu (hwnd, hwnd, hwnd_menu,
  245.                               MOUSEMSG (&msg)->x, MOUSEMSG (&msg)->y, 0L,
  246.                               PU_HCONSTRAIN | PU_VCONSTRAIN |
  247.                               PU_NONE | PU_MOUSEBUTTON1 |
  248.                               PU_KEYBOARD);
  249.                 return 0;
  250.             case WM_MOUSEMOVE :
  251.                 WinSetPointer (HWND_DESKTOP, hptr_rem);
  252.                 return MRFROMSHORT (1);
  253.  
  254.             case WM_CLOSE :
  255.                 VcaiDeviceClose (DevHandle);
  256.                 break;
  257.  
  258.             case WM_COMMAND :
  259.                 switch (SHORT1FROMMP (mp1))
  260.                 {
  261.                     case IDM_FILE_QUIT :
  262.                         WinDlgBox (HWND_DESKTOP,
  263.                                    HwndFrame,
  264.                                    (PFNWP)QuitDlgProc,
  265.                                    (HMODULE)NULLHANDLE,
  266.                                    IDD_QUIT,
  267.                                    NULL);
  268.                         WinInvalidateRegion (HwndFrame, NULLHANDLE, FALSE);
  269.                         break;
  270.                     case IDM_TUNE :
  271.                         WinDlgBox (HWND_DESKTOP,
  272.                                    HWND_DESKTOP,
  273.                                    (PFNWP)TuneDlgProc,
  274.                                    (HMODULE)NULLHANDLE,
  275.                                    IDD_TUNE,
  276.                                    NULL);
  277.                         break;
  278.                     case IDM_HELPCONTENTS :
  279.                         HelpContents ();
  280.                         break;
  281.                     case IDM_HELPINDEX :
  282.                         HelpIndex ();
  283.                         break;
  284.                     case IDM_KEYSHELP :
  285.                         HelpKeys ();
  286.                         break;
  287.                     case IDM_GENERALHELP :
  288.                         HelpHelpForHelp ();
  289.                         break;
  290.                     case IDM_HELPPRODUCTINFO :
  291.                         HelpAbout ();
  292.                         break;
  293.                     default :
  294.                         break;
  295.                 }
  296.                 break;
  297.             case HM_QUERY_KEYS_HELP :
  298.                 return ((MRESULT)PANEL_KEYSHELP);
  299.             case MSG_UPDATE_WINDOW :
  300.                 WinQueryWindowRect (hwnd, &rcl);
  301.                 WinInvalidateRect (hwnd, &rcl, TRUE);
  302.                 WinPostMsg (hwnd, MSG_SIZE_TV, NULL, NULL);
  303.             default :
  304.                 break;
  305.         }
  306.         return WinDefWindowProc (hwnd, msg, mp1, mp2);
  307. }
  308.  
  309. MRESULT EXPENTRY QuitDlgProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  310. {
  311.         switch (msg)
  312.         {
  313.             case WM_COMMAND :
  314.                 switch (SHORT1FROMMP (mp1))
  315.                 {
  316.                     case DID_OK :
  317.                         WinPostMsg (HwndFrame, WM_CLOSE, NULL, NULL);
  318.                         WinDismissDlg (hwnd, TRUE);
  319.                         break;
  320.                     case DID_CANCEL :
  321.                         WinDismissDlg (hwnd, TRUE);
  322.                         break;
  323.                     default :
  324.                         break;
  325.                 }
  326.                 break;
  327.             default :
  328.                 break;
  329.         }
  330.         return WinDefDlgProc (hwnd, msg, mp1, mp2);
  331. }
  332.  
  333. MRESULT EXPENTRY AboutDlgProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  334. {
  335.     HPS     hps;
  336.     RECTL   rcl;
  337.     LONG    x, y;
  338.  
  339.         switch (msg)
  340.         {
  341.             case WM_PAINT :
  342.                 hps = WinBeginPaint (hwnd, NULLHANDLE, &rcl);
  343.                 WinQueryWindowRect (hwnd, &rcl);
  344.                 WinFillRect (hps, &rcl, CLR_PALEGRAY);
  345.                 sprintf (txt, "%30.30s", DevInfo.ProdInfo);
  346.                 WinSetDlgItemText (hwnd, IDD_ABOUT_PROD_INFO, txt);
  347.                 sprintf (txt, "%30.30s", DevInfo.ManInfo);
  348.                 WinSetDlgItemText (hwnd, IDD_ABOUT_MAN_INFO, txt);
  349.                 sprintf (txt, "%10.10s", DevInfo.Version);
  350.                 WinSetDlgItemText (hwnd, IDD_ABOUT_VERSION, txt);
  351.                 WinDrawBorder (hps, &rcl, x, y, 0L, 0L, DB_AREAATTRS |
  352.                                DB_DLGBORDER);
  353.                 WinEndPaint (hps);
  354.                 break;
  355.             case WM_COMMAND :
  356.                 switch (SHORT1FROMMP (mp1))
  357.                 {
  358.                     case DID_OK :
  359.                         WinDismissDlg (hwnd, TRUE);
  360.                         break;
  361.                     default :
  362.                         break;
  363.                 }
  364.                 break;
  365.             default :
  366.                 break;
  367.         }
  368.         return WinDefDlgProc (hwnd, msg, mp1, mp2);
  369. }
  370.  
  371. static BOOL init_window ()
  372. {
  373.     CHAR    szClient[] = "Client";
  374.     ULONG   flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU | FCF_ICON |
  375.                            FCF_SIZEBORDER | FCF_MINMAX | FCF_ACCELTABLE |
  376.                            FCF_SHELLPOSITION | FCF_TASKLIST;
  377.  
  378.         if (!WinRegisterClass (Hab, szClient, ClientWndProc,
  379.                                CS_SIZEREDRAW | CS_MOVENOTIFY, 0))
  380.             return (FALSE);
  381.  
  382.         if (!WinRegisterGraphicButton ())
  383.             return (FALSE);
  384.  
  385.         DevInfo.Length = sizeof (VCADEVINFO);
  386.         VcaiDeviceInfoGet (&DevInfo);
  387.  
  388.         sprintf (txt, "Example 2 - %s %u", DevType, DevInstance);
  389.  
  390.         HwndFrame = WinCreateStdWindow (HWND_DESKTOP,
  391.                                         WS_VISIBLE,
  392.                                         &flFrameFlags,
  393.                                         szClient,
  394.                                         txt,
  395.                                         0L,
  396.                                         (HMODULE)NULLHANDLE,
  397.                                         IDR_MAIN,
  398.                                         &HwndClient);
  399.         if (!HwndFrame)
  400.             return (FALSE);
  401.  
  402.         HelpInit (HwndFrame);
  403.  
  404.         if (!init_video ())
  405.             return (FALSE);
  406.  
  407.         if (!init_audio ())
  408.             return (FALSE);
  409.  
  410.         return (TRUE);
  411. }
  412.  
  413. static void init_colour ()
  414. {
  415.     HDC     hdc;
  416.     DEVOPENSTRUC dop = {NULL, "DISPLAY", NULL, NULL, NULL, NULL, NULL, NULL,
  417.                         NULL};
  418.  
  419.         hdc = DevOpenDC (Hab, OD_INFO, "*", 5L, (PDEVOPENDATA)&dop, NULLHANDLE);
  420.         DevQueryCaps (hdc, CAPS_COLORS, 1L, &NumColours);
  421.         DevCloseDC (hdc);
  422. }
  423.  
  424.  
  425. static void map_pallete (HPS hps)
  426. {
  427.     LONG    rc;
  428.     VCASCREENINFO vs;
  429.     USHORT cx, cy, i;
  430.  
  431.         if ((NumColours <= 256) && (NumColours > 0))
  432.         {
  433.             rc = GpiQueryRealColors (hps, 0L, 0L, NumColours, Colours);
  434.             if (rc == GPI_ALTERROR)
  435.             {
  436.                 err_msg ("GpiQueryRealColors");
  437.                 return;
  438.             }
  439.         }
  440.         cx = WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN);
  441.         cy = WinQuerySysValue (HWND_DESKTOP, SV_CYSCREEN);
  442.         vs.ulLength = sizeof (VCASCREENINFO);
  443.         vs.ul_RESV01 = 0L;
  444.         vs.ulWidth = (ULONG)cx;
  445.         vs.ulHeight = (ULONG)cy;
  446.         vs.ulNumColours = (ULONG)NumColours;
  447.         vs.ulHFreq = 0L;
  448.         vs.ulVFreq = 0L;
  449.         if ((ULONG)NumColours <= 256)
  450.         {
  451.             for (i = 0; i < NumColours; i++)
  452.                 vs.ulRGB[i] = (ULONG)Colours[i];
  453.         }
  454.         else
  455.         {
  456.             for (i = 0; i < 256; i++)
  457.                 vs.ulRGB[i] = 0L;
  458.         }
  459.         VcaiScreenInfoSet (&vs);
  460.         rc = GpiCreateLogColorTable (hps, LCOL_RESET, LCOLF_RGB,
  461.                                      0L, 0L, (PLONG)0L);
  462.         if (!rc)
  463.         {
  464.             err_msg ("GpiCreateLogColorTable");
  465.             return;
  466.         }
  467. }
  468.