home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / pmtermsr.lzh / thread2.c < prev   
C/C++ Source or Header  |  1995-10-13  |  5KB  |  197 lines

  1. /*
  2.  * This file contains functions for the 2nd thread,  which reads
  3.  * from the serial port.
  4.  */
  5.  
  6. #include "pmterm.h"
  7.  
  8.  
  9. static char **lines = NULL;
  10. static int curline = 0;
  11. static int curcol  = 0;
  12.  
  13. /*
  14.  * PostMsg() -- just like WinPostMsg(),  but checks to see
  15.  * if the queue was full
  16.  */
  17. static void PostMsg(HWND h, ULONG msg, MPARAM mp1, MPARAM mp2)
  18. {
  19.     while(!WinPostMsg(h, msg, mp1, mp2)){
  20.         DosSleep(1);
  21.     }
  22. }
  23.  
  24. char *format(long recnum, void *user)
  25. {
  26.     if(!lines)
  27.     return NULL;
  28.     return lines[recnum];
  29. }
  30.  
  31.  
  32. /* Scroll window so that current line will be visible */
  33. static void Adjust(HWND hwnd, SCROLL *scrl, int line1, int line2)
  34. {
  35.     int y;
  36.     int n;  // number of lines to scroll.
  37.  
  38.     y = scrl->yClient - scrl->yChar * (line2 + 1 - scrl->vScrollPos) + scrl->yDesc;
  39.     if(y < 0){  // off bottom?
  40.         n = y/scrl->yChar - 1;      // n will be negative
  41.         WinSendMsg(hwnd,
  42.                    WM_VSCROLL,
  43.                    (MPARAM)FID_VERTSCROLL,
  44.                    MPFROM2SHORT(scrl->vScrollPos - n,
  45.                         SB_SLIDERPOSITION));
  46.     }
  47.     else{
  48.         y = scrl->yClient - scrl->yChar * (line1 + 1 - scrl->vScrollPos) + scrl->yDesc;
  49.         if(y > scrl->yClient){  // beyond top?
  50.             n = 1 + (y - scrl->yClient)/scrl->yChar;
  51.  
  52.             WinSendMsg(hwnd,
  53.                WM_VSCROLL,
  54.                (MPARAM)FID_VERTSCROLL,
  55.                        MPFROM2SHORT(scrl->vScrollPos - n,
  56.                 SB_SLIDERPOSITION));
  57.  
  58.     }
  59.     }
  60. }
  61.  
  62. void PaintLines(HWND hwnd, int startline, int endline)
  63. {
  64.     int line;
  65.     POINTL pt;                  /* String screen coordinates      */
  66.     HPS hps;
  67.     int ofs;
  68.     char *str;
  69.     int cols;
  70.     SCROLL *scrl = WinQueryWindowPtr(hwnd, 0);
  71.  
  72.     if(startline < 0)
  73.         startline = 0;
  74.     Adjust(hwnd, scrl, startline, endline);  // make sure new lines will be visible
  75.  
  76.     hps = WinGetPS(hwnd);
  77.     pt.x = 0L;
  78.     ofs = scrl->hScrollPos;
  79.     for(line=startline; line<=endline; line++){
  80.     pt.y = scrl->yClient - scrl->yChar * (line + 1 - scrl->vScrollPos) + scrl->yDesc;
  81.     str = (*scrl->func)(line, scrl->user);
  82.     if(str){
  83.         if(strlen(str) > ofs){
  84.         cols = strlen(str + ofs);
  85.         if(cols)
  86.             GpiCharStringAt( hps, &pt,
  87.             (ULONG) ((cols > (scrl->cols+5)) ? (scrl->cols+5) : cols),
  88.             str + ofs);
  89.         }
  90.     }
  91.     }
  92.     WinReleasePS(hps);
  93. }
  94.  
  95. void Cls(HWND hwnd)
  96. {
  97.     int j;
  98.     for(j=0; j<MAXLINES; j++)
  99.     lines[j][0] = 0;
  100.     curline = curcol = 0;
  101.     WinInvalidateRect(hwnd, NULL, FALSE);
  102. }
  103.  
  104.  
  105. /* shift1() -- scroll top line into the bit bucket */
  106. static void shift1(void)
  107. {
  108.     int i;
  109.  
  110.     for(i=0; i<MAXLINES-1; i++)
  111.         lines[i] = lines[i+1];
  112.     lines[i] = lines[0];
  113.  
  114. }
  115.  
  116. static void print(PDATA *pdata, char *lbuf, ULONG len)
  117. {
  118.     int i;
  119.     int startline = curline;
  120.     int pre_scroll = 0;
  121.  
  122.     for(i=0; i<len; i++){
  123.     switch(lbuf[i]){
  124.         case '\r':
  125.         break;
  126.         case '\n':
  127.                 lines[curline][curcol] = 0;
  128.         curcol = 0;
  129.         curline++;
  130.         if(curline == MAXLINES){
  131.                     shift1();
  132.                     curline--;
  133.                     pre_scroll++;
  134.         }
  135.         break;
  136.         case '\f':      // page eject == ascii 12 == cls
  137.                 PostMsg(pdata->hwndClient, WMU_CLS, 0, 0);
  138.         break;
  139.         default:
  140.         lines[curline][curcol++] = lbuf[i];
  141.         if(curcol == MAXLINELEN){
  142.             lines[curline][curcol] = 0;
  143.             curcol = 0;
  144.             curline++;
  145.                     if(curline == MAXLINES){
  146.                         shift1();
  147.                         curline--;
  148.                         pre_scroll++;
  149.                     }
  150.         }
  151.         break;
  152.     }
  153.     }
  154.     lines[curline][curcol  ] = 0;
  155.  
  156.     if(pre_scroll)
  157.         PostMsg(pdata->hwndClient, WMU_PRESCROLL, (MPARAM)pre_scroll, 0);
  158.  
  159.     PostMsg(pdata->hwndClient,
  160.             WMU_PAINTLINES,
  161.             (MPARAM)(startline - pre_scroll),
  162.             (MPARAM)curline);
  163. }
  164.  
  165. void AsyncReadThread(void *arg)
  166. {
  167.     PDATA *pdata = arg;
  168.     APIRET rc;
  169.     char buf[256];
  170.     ULONG br;        // bytes read
  171.     int i;
  172.  
  173.     if(!lines){
  174.         lines = (char **)calloc(MAXLINES , sizeof(char *));
  175.         for(i=0; i<MAXLINES; i++){
  176.             lines[i] = (char *)malloc(MAXLINELEN+1);
  177.             *lines[i] = 0;
  178.         }
  179.     }
  180.  
  181.     for(;;){
  182.     rc = DosRead(pdata->hf, buf, sizeof(buf), &br);
  183.     if(rc)
  184.         break;
  185.     if(br){
  186.         print(pdata, buf, br);
  187.     }
  188.     else { // else, no bytes were read (5 second time out).
  189.         // nice place for a carrier check
  190.             DosSleep(1L);
  191.     }
  192.     }
  193.     printf("Unexpected error %d from DosRead()\n", rc);
  194.     _endthread();
  195. }
  196.  
  197.