home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / CKPM5X_S.ZIP / CKOPM1.C < prev    next >
C/C++ Source or Header  |  1990-05-21  |  18KB  |  509 lines

  1. /******************************************************************************
  2. File name:  pminfn.c     Rev: 01  Date: 24-Nov-89 Programmer: C.P.Armstrong
  3.  
  4. File title: Provides an interface to the Presentation Manager for programs
  5.             which were originally designed as MS-DOS command line like
  6.             applications.  This file contains code which initializes and
  7.             starts a thread which processes Presentation Manager message queue
  8.             events.  An AVIO presentation space is created for the programs I/O.
  9.             This has the advantage of being fixed pitch and directly addressable
  10.             by the program.  Unfortunately it would appear that a number of the
  11.             Vio functions don't work with PM presentation spaces when used
  12.             outside of the main PM thread.  This means that all screen I/O
  13.             must be performed directly.
  14.  
  15. Contents:   
  16.  
  17. Modification History:
  18.     01  24-Nov-89 C.P.Armstrong created
  19.         27-Nov-89 C.P.Armstrong A VioWrtTTY look-alike which is compatible with
  20.                                 multi-threaded AVIO PM programs.  Frills such as
  21.                                 cursor hiding, colour matching etc.
  22.         05-Feb-90 C.P.Armstrong PM thread termination does not shut down whole 
  23.                                 process.
  24. ******************************************************************************/
  25. #define INCL_DOS
  26. #define INCL_WIN
  27. #define INCL_GPI    
  28. #define INCL_VIO
  29. #define INCL_AVIO
  30. #include <OS2.h>
  31. #include <malloc.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <process.h>
  35. #include "ckopm.h"
  36. #include "ckorc.h"     /* Resource defines */
  37.  
  38. #define VIOTITLETEXT  "C-Kermit Command Mode"
  39.  
  40. #define WINDPROCSTACKSIZE   9000  /* Last value was 10000 */
  41. #define YES                    1
  42. #define NO                     0
  43. #define DEFAULTCOLOUR       0x1E
  44. #define MAXPC               1024
  45. #define CURBLINK               1
  46.  
  47. /* Window handles, flags, semaphores etc.. */
  48. HAB     hab;
  49. HWND    hwndTFrame;        /* Frame handle of the main window */
  50. HWND    hwndGFrame;        /* Frame handle of the child window */
  51. HWND    hwndText;          /* Handle to the Vio text window */
  52. HWND    hwndGraph;         /* Handle to the Gpi graph window */
  53. HDC     hdc;
  54. HPS     gpi_hps;           /* Handle to the Gpi window PS */
  55. ULONG far wind_sem=0;        /* Set when video buffer is allocated */
  56. ULONG far vio_sem=0;         /* Used by the Vio routines when they are done */
  57. ULONG far kbd_sem=0;         /* Cleared when a character is put */
  58.                              /* into the buffer */
  59. ULONG far paint_sem=0;          /* Set when repaint desired */
  60. ULONG far stop_painting_sem=0;  /* Set when about to clear */
  61. ULONG far pm_die_sem=0;         /* Set by main thread, cleared by PM thread */
  62.                                 /* before dying */
  63.  
  64.  
  65. /* Screen variables */
  66. char far * dispbuf="C.P.A. 1989";/* Pointer to string to be output to screen */
  67. char far * ulVideoBuffer;    /* Pointer to the video buffer */
  68. USHORT  usVideoLength;       /* Length of the video buffer */
  69. HVPS    hvps;                /* Handle to video presenation space */
  70.                              /* used by VioShowBuf */
  71. int far scrwidth=80;         /* Screen size in characters - used by vWrtchar()*/
  72. int far scrheight=25;
  73. int     Vwinheight;          /* Size of Vio window in pels (including the     */
  74. int     Vwinwidth;           /* border etc..                                  */
  75. extern char defaultattribute;    /* Colour attribute used for the Vio screen */
  76. COLOR   pmbgcol;                 /* PM colours - defaultattribute is */
  77.                                  /* converted into */
  78. COLOR   pmfgcol;                 /* these for PM colouring.                  */
  79. char    cursor_displayed=0;      /* Record of whether cursor should be on    */
  80. USHORT  curblink;                /* id of cursor timer */
  81.  
  82.  
  83. /* Misc definitions */
  84. char debugmsg=0;
  85. char Vio_visible=1;             /* Is Vio screen visible */  
  86. SWP pc_swp;                     /* Data used by the repaint thread */
  87. char minied;                    /* Indicates window is minimized */
  88.  
  89. MPARAM dum1;
  90. MPARAM dum2;
  91.  
  92. /* Function definitions */
  93. MRESULT EXPENTRY GpiWndProc(HWND,USHORT,MPARAM,MPARAM);
  94. MRESULT EXPENTRY VioWndProc(HWND,USHORT,MPARAM,MPARAM);
  95. void dbprintf(const char *,...);
  96. int  DecodePMChar(MPARAM,MPARAM);
  97. void Put_cursor_onscreen(HWND,HVPS);
  98. void TitleText(MPARAM,MPARAM);
  99. int  DoPMTekGin(HWND,USHORT*);
  100. int  numlock_toggle();
  101. int  numlock_status();
  102. void avio_dispatch(struct avio_cellstr *, HVPS);
  103.  
  104. int do_mark(HWND,MPARAM,MPARAM,HVPS);
  105. int do_copy(int,HVPS,HAB);
  106. int end_mark(HWND,MPARAM,MPARAM,HVPS);
  107. int start_mark(HWND,MPARAM,MPARAM,HVPS);
  108. int quit_mark(HWND,MPARAM,MPARAM,HVPS,HAB);
  109.  
  110. // void far pc_paint_thread();
  111. /******************************************************************************
  112. Function:       PM_init()
  113.  
  114. Description:    Initializes the PM message queue thread, client window routine
  115.                 etc..
  116.  
  117. Syntax:         int PM_init()
  118.  
  119. Returns:        0 if successful
  120.                 -1 if the thread create fails
  121.  
  122. Mods:           24-Nov-89 C.P.Armstrong created
  123.  
  124. ******************************************************************************/
  125. int PM_init(title)
  126.     char far * title;
  127.     {
  128.     extern HVPS hvps;   
  129.     extern USHORT usVideoLength;
  130.     extern char far * ulVideoBuffer;
  131.     extern ULONG wind_sem;
  132.     int* wind_proc_stack;
  133.     TID   wind_proc_id;
  134.  
  135.     int c;
  136.     char buf[90];
  137.  
  138.     /* allocate stack for window thread */
  139.     if((wind_proc_stack = (int*) malloc(WINDPROCSTACKSIZE))==NULL)
  140.         {
  141.         dbprintf("Can't allocate window thread stack\n");
  142.         DosExit(EXIT_PROCESS,1);
  143.         }
  144.  
  145.     /* Set the semaphore so it can be cleared when video buffer is alloc'd */
  146.     if(DosSemSet(&wind_sem)!=0)
  147.         {
  148.         dbprintf("Can't set window semaphore\n");
  149.         DosExit(EXIT_PROCESS,1);
  150.         }
  151.  
  152.     _beginthread(window_thread,wind_proc_stack,WINDPROCSTACKSIZE,title);
  153.  
  154.     /* Wait for semaphore to be set indicating that the window has been */
  155.     /* created. If an error has occurred then the program will be ended */
  156.     /* where the error occurs so an indefinite wait should be okay.     */
  157.     DosSemWait(&wind_sem,SEM_INDEFINITE_WAIT);
  158.  
  159.     return(0);
  160.     }
  161.  
  162. /******************************************************************************
  163. Function:       window_thread()
  164.  
  165. Description:    The presentation manager message queue thread.
  166.  
  167. Syntax:         void window_thread(title)
  168.                     char far * title;  Pointer to main window title
  169.  
  170. Returns:        nothing
  171.  
  172. Mods:           07-Dec-89 C.P.Armstrong
  173.                 05-Feb-90 C.P.Armstrong  Termination of this thread does not 
  174.                                          cause termination of the whole process.
  175.                 25-Feb-90 C.P.Armstrong  Function clears a semaphore just prior
  176.                                          to dying.
  177.  
  178. ******************************************************************************/
  179. void far window_thread(title)
  180.     char far * title;
  181.     {
  182.     extern HWND hwndTFrame,hwndGFrame,hwndText,hwndGraph;
  183.     extern HDC hdc;
  184.     extern Vwinheight,Vwinwidth;
  185.     extern ULONG wind_sem;
  186.     extern ULONG pm_die_sem;
  187.     extern USHORT  curblink; 
  188.     static CHAR szClientClass[]="Vio window";
  189.     static CHAR szGraphClass[]="Graph window";
  190.     static ULONG flTFrameFlags = FCF_TITLEBAR
  191.                                 | FCF_SIZEBORDER
  192.                                 | FCF_MINMAX;
  193.     static ULONG flGFrameFlags = FCF_TITLEBAR | FCF_SIZEBORDER 
  194.                                 | FCF_SHELLPOSITION 
  195.                                 | FCF_ICON
  196.                                 | FCF_SYSMENU
  197.                                 | FCF_MENU
  198.                                 | FCF_MINMAX ;
  199.     HMQ hmq;
  200.     QMSG qmsg;
  201.     SIZEL sizl;
  202.     RECTL rcl;
  203.     SWP swp;
  204.     SWCNTRL swctl;
  205.     HSWITCH hswitch;
  206.     PID pid;
  207.     LONG bord_title;
  208.  
  209.  
  210.     hab = WinInitialize(0);
  211.     hmq=WinCreateMsgQueue(hab,0);
  212.     
  213.     /* Register the Vio window */
  214.     WinRegisterClass(hab, szClientClass, VioWndProc,CS_SIZEREDRAW,0);
  215.     
  216.     /* Register the graphics window */
  217.     WinRegisterClass(hab, szGraphClass, GpiWndProc,CS_SIZEREDRAW,0);
  218.  
  219.  
  220.     /* Create the main (Gpi) window */
  221.     hwndGFrame = WinCreateStdWindow(HWND_DESKTOP,WS_VISIBLE,
  222.                       &flGFrameFlags,
  223.                       szGraphClass, 
  224.                       title, WS_CLIPCHILDREN, (HMODULE)NULL, 
  225.                       ID_KERMIT, &hwndGraph);
  226.  
  227.     /* Create the child (Vio) window */
  228.     hwndTFrame = WinCreateStdWindow(hwndGraph,WS_VISIBLE,&flTFrameFlags,
  229.                           szClientClass, VIOTITLETEXT, 0L, (HMODULE)NULL, 
  230.                           0, &hwndText);
  231.  
  232.     /* Determine size of Vio window */
  233.     DevQueryCaps(hdc, CAPS_CHAR_WIDTH, 2L, (PLONG) &sizl);
  234.     sizl.cx *= (long) scrwidth;
  235.     sizl.cx += (LONG)(2*WinQuerySysValue(HWND_DESKTOP,SV_CXSIZEBORDER));
  236.     Vwinwidth = (int) sizl.cx;
  237.  
  238.     sizl.cy *= (long) scrheight;
  239.     bord_title = (LONG) (WinQuerySysValue(HWND_DESKTOP,SV_CYTITLEBAR)+
  240.                     (2*WinQuerySysValue(HWND_DESKTOP,SV_CYSIZEBORDER)));
  241.     sizl.cy += bord_title;
  242.     Vwinheight = (int) sizl.cy;
  243.  
  244.     /* Make initial com line window just fit the GPI window */
  245.     WinQueryWindowRect(hwndGFrame,&rcl);     /* Get GPI frame size */
  246.     WinCalcFrameRect(hwndGFrame,&rcl,TRUE);  /* Convert to client window size */
  247.  
  248.     /* Use Gpi client size as Vio Frame size */
  249.     WinSetWindowPos(hwndTFrame,HWND_TOP,(SHORT)0,(SHORT)0,
  250.             (SHORT) (rcl.xRight-rcl.xLeft), (SHORT)(rcl.yTop-rcl.yBottom) ,
  251.             SWP_SIZE|SWP_ZORDER|SWP_SHOW|SWP_MOVE|SWP_ACTIVATE);
  252.  
  253.     WinSendMsg(hwndTFrame,WM_SETICON,
  254.               WinQuerySysPointer(HWND_DESKTOP,SPTR_APPICON,FALSE),
  255.               NULL);
  256.  
  257.     /* Add program title to the task switch list */
  258.     if((hswitch = AddToSwitch(hwndGFrame,NULL,title))==NULL )
  259.         {
  260.         pm_err("Failed to add program to switch list");
  261.         }
  262.               
  263.     /* The window should be up and running now so tell the main thread */
  264.     DosSemClear(&wind_sem);
  265.  
  266.     /* Start the timer for the blinking cursor - using the system */
  267.     /* value for the rate */
  268.     WinStartTimer(hab,hwndText,CURBLINK,(USHORT)WinQuerySysValue(HWND_DESKTOP,
  269.                     SV_CURSORRATE)/2 );
  270.  
  271.  
  272.     while(WinGetMsg(hab,&qmsg,NULL,0,0))
  273.         WinDispatchMsg(hab,&qmsg);
  274.         
  275.     WinStopTimer(hab,hwndText,CURBLINK);
  276.     WinDestroyWindow(hwndTFrame);
  277.     WinDestroyWindow(hwndGFrame);
  278.     WinDestroyMsgQueue(hmq);
  279.     WinRemoveSwitchEntry(hswitch);
  280.     WinTerminate(hab);
  281.     if(DosSemWait(&pm_die_sem,SEM_IMMEDIATE_RETURN)!=0)
  282.         {
  283.         DosEnterCritSec();
  284.         DosSemClear(&pm_die_sem);
  285.         dbprintf("Exiting window thread\n");
  286.         DosExit(EXIT_THREAD,0);
  287.         }
  288.     dbprintf("Window thread killing whole process\n");
  289.     DosExit(EXIT_PROCESS,0);
  290.     }
  291.  
  292. /******************************************************************************
  293. Function:       VioWndProc()
  294.  
  295. Description:    Handles the Vio window text input and output
  296.  
  297. Syntax:         
  298.  
  299. Returns:        
  300.  
  301. Mods:           07-Dec-89 C.P.Armstrong
  302.  
  303. ******************************************************************************/
  304. MRESULT EXPENTRY VioWndProc(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
  305.     {
  306.     extern USHORT  curblink;                /* id of cursor timer */
  307.     extern HVPS hvps;
  308.     extern USHORT usVideoLength;
  309.     extern char far * ulVideoBuffer;
  310.     extern COLOR pmbgcol,pmfgcol;
  311.     extern char  defaultattribute;
  312.     extern int scrheight, scrwidth;
  313.     extern int Vwinheight,Vwinwidth;
  314.     extern ULONG wind_sem;
  315.     extern ULONG vio_sem;
  316.     extern char Vio_visible;
  317.     extern int marking;
  318.  
  319.     static HPS hps;  /* PS handle used by Gpi calls */
  320.     extern HDC hdc;
  321.     RECTL rcl;
  322.     SIZEL sizl;
  323.     long rgbcol;
  324.     PSWP pswp;
  325.     QMSG qmsg;
  326.     
  327.     int i,j,t,x,y;
  328.     struct avio_cellstr * pac;
  329.     
  330.     switch(msg)
  331.         {
  332.         case WM_CREATE:
  333.             hdc = WinOpenWindowDC(hwnd);
  334.             sizl.cx = sizl.cy = 0;
  335.             hps = GpiCreatePS(hab,hdc,&sizl, PU_PELS | GPIF_DEFAULT | 
  336.                                              GPIT_MICRO | GPIA_ASSOC);
  337.             VioCreatePS(&hvps,scrheight,scrwidth,0,1,(HVPS)NULL);
  338.             VioAssociate(hdc,hvps);
  339.             
  340.             VioGetBuf((PULONG)&ulVideoBuffer,&usVideoLength,hvps);
  341.             VioWrtNAttr(&defaultattribute, usVideoLength,0,0,hvps);
  342.             
  343.             return(0L);
  344.             
  345.        case WM_TIMER:
  346.             if(SHORT1FROMMP(mp1)==CURBLINK)
  347.                 {
  348.                 flash_cursor(hvps);
  349.                 return(0L);
  350.                 }
  351.              break;
  352.  
  353.        case WM_ADJUSTWINDOWPOS:   // about to size - remove cursor
  354.             show_cursor(NO,hvps);
  355.             return(0L);
  356.  
  357.        case WM_SIZE:
  358.             quit_mark(hwnd,mp1,mp2,hvps,hab);            
  359.             /* Prevent the size from being made bigger than the video buffer */
  360.             /* First calculate the extra dimensions of the frame window */
  361.  
  362.             /* Calculate Frame window size - */
  363.             /* this allows for menus, scroll bars etc. */
  364.             rcl.xRight  = SHORT1FROMMP(mp2);
  365.             rcl.xLeft   = 0;
  366.             rcl.yTop    = SHORT2FROMMP(mp2);
  367.             rcl.yBottom = 0;
  368.             WinCalcFrameRect(hwndTFrame,&rcl,FALSE);  /* Get frame size */
  369.             i =  (int) (rcl.xRight-rcl.xLeft);
  370.             j = (int) (rcl.yTop-rcl.yBottom);
  371.  
  372.             t=0;
  373.             
  374.             /* Do the check */
  375.             if(i>(Vwinwidth))
  376.                 {
  377.                 i = Vwinwidth;
  378.                 t = 1;
  379.                 }
  380.             else if(j>(Vwinheight))
  381.                 {   
  382.                 j = Vwinheight;
  383.                 t = 1;
  384.                 }
  385.  
  386.             if(t==1)
  387.                 {
  388.                 WinSetWindowPos(hwndTFrame,0,0,0,i,j,SWP_SIZE);
  389.                 MPFROM2SHORT(i,j);
  390.                 return(FALSE);
  391.                 }
  392.                 
  393.             WinDefAVioWindowProc(hwnd,msg,mp1,mp2);
  394.             show_cursor(YES,hvps);
  395.             Put_cursor_onscreen(hwnd,hvps);
  396.             return(0L);
  397.             
  398.        case WM_SETSELECTION:
  399.            if(SHORT1FROMMP(mp1))
  400.                {                
  401.                /* We are being selected */
  402.                show_cursor(YES,hvps);
  403.                }
  404.             else
  405.                {
  406.                /* We are being deselected */
  407.                show_cursor(NO,hvps);
  408.                }
  409.             
  410.         case WM_PAINT:
  411.             WinBeginPaint(hwnd,hps,NULL);
  412.             WinQueryWindowRect(hwnd,&rcl);
  413.             pmbgcol = VioToRgbColor((BYTE)(defaultattribute>>((char)4)));
  414.             pmfgcol = VioToRgbColor(defaultattribute);
  415.  
  416.             WinFillRect(hps,&rcl,pmbgcol);
  417.             
  418.             VioShowPS(scrheight,scrwidth,0,hvps);
  419.             WinEndPaint(hps);
  420.             return(0L);
  421.             
  422.         case WM_HELP:
  423.             pm_msg("Help", "Try typing ? at the command line prompt.\n");
  424.             return(0L);
  425.  
  426.         case WM_CHAR:
  427.             /* There seems to be no other way of getting keyboard input to */
  428.             /* the parent window.                                          */
  429.             quit_mark(hwnd,mp1,mp2,hvps,hab);            
  430.  
  431.             if(TestPMGinMode())
  432.                 {
  433.                 WinSendMsg(hwndGraph,WM_CHAR,mp1,mp2);
  434.                 return((MRESULT)1);
  435.                 }
  436.  
  437.  
  438.             if(CHARMSG(&msg)->fs & KC_KEYUP)            /* Ignore key ups */
  439.                 {
  440.                 return(0L);
  441.                 }
  442.  
  443.             DecodePMChar(mp1,mp2);
  444.             
  445. //          break;
  446.             return(0L);
  447.  
  448.         case WM_BUTTON1DOWN:
  449.             start_mark(hwnd,mp1,mp2,hvps);
  450.             break;
  451.  
  452.             
  453.         case WM_BUTTON1UP:
  454.             end_mark(hwnd,mp1,mp2,hvps);
  455.             break;
  456.  
  457.         case WM_MOUSEMOVE:
  458.             do_mark(hwnd,mp1,mp2,hvps);
  459.             break;
  460.  
  461.  
  462.  
  463.         case WM_HIDE:
  464.             if(mp1==(MPARAM)1)
  465.                 {
  466.                 WinShowWindow(hwndTFrame,FALSE);
  467.                 Vio_visible=0;
  468.                 }
  469.             else
  470.                 {
  471.                 WinShowWindow(hwndTFrame,TRUE);
  472.                 Vio_visible=1;
  473.                 }
  474.  
  475.             return(0L);
  476.  
  477.         case WM_KERAVIO:
  478.             quit_mark(hwnd,mp1,mp2,hvps,hab);            
  479.  
  480.             pac = (struct avio_cellstr *) PVOIDFROMMP(mp1);
  481.  
  482.             /* Do the avio command */
  483.             process_avio(pac,hvps);
  484.               
  485.             DosSemClear(&vio_sem);
  486.             return(0L);
  487.  
  488.         case WM_TITLETEXT:
  489.             TitleText(mp1,mp2);
  490.             return(0L);
  491.  
  492.         case WM_CURCHECK:
  493.             quit_mark(hwnd,mp1,mp2,hvps,hab);            
  494.             Put_cursor_onscreen(hwnd,hvps);
  495.             return(0L);
  496.  
  497.         case WM_DESTROY:
  498.             VioAssociate(NULL,hvps);
  499.             VioDestroyPS(hvps);
  500.             GpiDestroyPS(hps);
  501.             return(0L);
  502.         }
  503.     
  504.     return(WinDefWindowProc(hwnd,msg,mp1,mp2));
  505.     }
  506.  
  507.    
  508.    
  509.