home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 May / cica_0595_4.zip / cica_0595_4 / UTIL / MSWSRC35 / DEVWIND.CPP < prev    next >
C/C++ Source or Header  |  1993-10-06  |  10KB  |  461 lines

  1. #include "allwind.h"
  2.  
  3. int ComId = -1;
  4.  
  5. NODE *lmouseon(NODE *args)
  6.    {
  7.    char lbuttondown[MAX_BUFFER_SIZE];
  8.    char lbuttonup[MAX_BUFFER_SIZE];
  9.    char rbuttondown[MAX_BUFFER_SIZE];
  10.    char rbuttonup[MAX_BUFFER_SIZE];
  11.    char mousemove[MAX_BUFFER_SIZE];
  12.    
  13.    // get args
  14.    
  15.    cnv_strnode_string(lbuttondown, args);
  16.    cnv_strnode_string(lbuttonup, args=cdr(args));
  17.    cnv_strnode_string(rbuttondown, args=cdr(args));
  18.    cnv_strnode_string(rbuttonup, args=cdr(args));
  19.    cnv_strnode_string(mousemove, args=cdr(args));
  20.    
  21.    // most of mouse code is in DEFWNDPROC when this flag is on
  22.    
  23.    mouse_on = 1;
  24.    
  25.    if (mouse_lbuttondown == NULL)
  26.      {
  27.      mouse_lbuttondown = (char *)malloc(MAX_BUFFER_SIZE);
  28.      mouse_lbuttonup   = (char *)malloc(MAX_BUFFER_SIZE);
  29.      mouse_rbuttondown = (char *)malloc(MAX_BUFFER_SIZE);
  30.      mouse_rbuttonup   = (char *)malloc(MAX_BUFFER_SIZE);
  31.      mouse_mousemove   = (char *)malloc(MAX_BUFFER_SIZE);
  32.      }
  33.  
  34.    // these really should be NODEs but not yet
  35.    
  36.    strcpy(mouse_lbuttondown,lbuttondown);
  37.    strcpy(mouse_lbuttonup,lbuttonup);
  38.    strcpy(mouse_rbuttondown,rbuttondown);
  39.    strcpy(mouse_rbuttonup,rbuttonup);
  40.    strcpy(mouse_mousemove,mousemove);
  41.    
  42.    return (UNBOUND);
  43.    }
  44.  
  45. NODE *lmouseoff(void)
  46.    {
  47.  
  48.    // tell handler not to do anything with messages for mouse
  49.  
  50.    mouse_on = 0;
  51.    
  52.    return (UNBOUND);
  53.    }
  54.  
  55. NODE *lkeyboardon(NODE *args)
  56.    {
  57.    char keyboarddown[MAX_BUFFER_SIZE];
  58.    char keyboardup[MAX_BUFFER_SIZE];
  59.    
  60.    if (keyboard_keyup == NULL)
  61.      {
  62.      keyboard_keyup = (char *)malloc(MAX_BUFFER_SIZE);
  63.      keyboard_keydown = (char *)malloc(MAX_BUFFER_SIZE);
  64.      }
  65.  
  66.    // get args
  67.    
  68.    if (cdr(args) == NIL)
  69.       {
  70.       cnv_strnode_string(keyboardup, args);
  71.       
  72.       // most keyboard processing is done in DEFWNDPROC
  73.       
  74.       keyboard_on = 1;
  75.       
  76.       strcpy(keyboard_keyup,keyboardup);
  77.       }
  78.    else
  79.       {
  80.       
  81.       cnv_strnode_string(keyboarddown, args);
  82.       cnv_strnode_string(keyboardup, cdr(args));
  83.       
  84.       // most keyboard processing is done in DEFWNDPROC
  85.       
  86.       keyboard_on = 2;
  87.       
  88.       strcpy(keyboard_keydown,keyboarddown);
  89.       strcpy(keyboard_keyup,keyboardup);
  90.       }
  91.    
  92.    return (UNBOUND);
  93.    }
  94.  
  95. NODE *lkeyboardoff(void)
  96.    {
  97.  
  98.    // tell handler not to do anything with messages for keyboard
  99.  
  100.    keyboard_on = 0;
  101.    
  102.    return (UNBOUND);
  103.    }
  104.  
  105. NODE *lmousepos()
  106.    {
  107.  
  108.    // return current mouse position
  109.  
  110.    return(
  111.    cons(make_intnode( mouse_posx+((TMyWindow *)MainWindowx)->Scroller->XPos*the_zoom-xoffset),
  112.    cons(make_intnode(-(mouse_posy+((TMyWindow *)MainWindowx)->Scroller->YPos*the_zoom-yoffset)), NIL))
  113.    );
  114.    }
  115.  
  116. NODE *lkeyboardvalue()
  117.    {
  118.  
  119.    // return current keyboard value
  120.  
  121.    return(make_intnode(keyboard_value));
  122.    }
  123.  
  124. int min(int a, int b)
  125.    {
  126.    if (a < b) return (a); else return(b);
  127.    }
  128.  
  129. NODE *lportclose(void)
  130.    {
  131.    
  132.    // if port closed output error else close it
  133.  
  134.    if (ComId < 0)
  135.       {
  136.       MessageBox(CmdHWindow, "Could not CLOSE port", "Error", MB_OK);
  137.       }
  138.    else
  139.       {
  140.       CloseComm(ComId);
  141.       ComId = -1;
  142.       }
  143.    
  144.    return (UNBOUND);
  145.    }
  146.  
  147. NODE *lportopen(NODE *args)
  148.    {
  149.    char comport[MAX_BUFFER_SIZE];
  150.    
  151.    cnv_strnode_string(comport, args);
  152.    
  153.    // if port open output error else open it
  154.  
  155.    if (ComId >= 0)
  156.       {
  157.       MessageBox(CmdHWindow, "PORT already open", "Error", MB_OK);
  158.       }
  159.    else
  160.       {
  161.       
  162.       ComId = OpenComm(comport, 1024, 1024);
  163.       
  164.       if (ComId < 0)
  165.          {
  166.          MessageBox(CmdHWindow, "Could not open PORT", "Error", MB_OK);
  167.          }
  168.       }   
  169.    
  170.    return (UNBOUND);
  171.    }
  172.  
  173. NODE *lportflush(NODE *args)
  174.    {
  175.    
  176.    int que;
  177.    int err;
  178.    
  179.    que = getint(pos_int_arg(args));
  180.    
  181.    if (ComId < 0)
  182.       {
  183.       MessageBox(CmdHWindow, "PORT not open", "Error", MB_OK);
  184.       }
  185.    else
  186.       {
  187.       err = FlushComm(ComId,que);
  188.       
  189.       if (err < 0)
  190.          {
  191.          MessageBox(CmdHWindow, "Could not flush PORT", "Error", MB_OK);
  192.          }
  193.       
  194.       }
  195.    
  196.    return (UNBOUND);
  197.    }
  198.  
  199. NODE *lportmode(NODE *args)
  200.    {
  201.    char commode[MAX_BUFFER_SIZE];
  202.    
  203.    DCB dcb;
  204.    
  205.    int err;
  206.    
  207.    cnv_strnode_string(commode, args);
  208.    
  209.    // if closed output error else set mode
  210.  
  211.    if (ComId < 0)
  212.       {
  213.       MessageBox(CmdHWindow, "PORT not open", "Error", MB_OK);
  214.       }
  215.    else
  216.       {
  217.       
  218.       // build dcb, if no error continue
  219.  
  220.       err = BuildCommDCB(commode, &dcb);
  221.       
  222.       if (err < 0)
  223.          {
  224.          MessageBox(CmdHWindow, "Could not build dcb on PORT", "Error", MB_OK);
  225.          }
  226.       else
  227.          {
  228.          // now set Id in dcb and set the state
  229.  
  230.          dcb.Id = ComId;
  231.          
  232.          err = SetCommState(&dcb);
  233.          
  234.          if (err < 0)
  235.             {
  236.             MessageBox(CmdHWindow, "Could not set PORT", "Error", MB_OK);
  237.             }
  238.          }   
  239.       }
  240.    
  241.    return (UNBOUND);
  242.    }
  243.  
  244. NODE *lportwritearray(NODE *args)
  245.    {
  246.    char txbuffer[MAX_BUFFER_SIZE]; 
  247.    
  248.    int i;
  249.    int count;
  250.    int status;  
  251.    
  252.    NODE *obj;
  253.    NODE *val;
  254.    NODE *item;
  255.    
  256.    val = pos_int_arg(args);
  257.    obj = cadr(args);
  258.    
  259.    while ((obj == NIL || obj == Null_Word) && NOT_THROWING)
  260.       {
  261.       setcar(cdr(args), err_logo(BAD_DATA, obj));
  262.       obj = cadr(args);
  263.       }
  264.    
  265.    if (NOT_THROWING)
  266.       {
  267.       if (nodetype(obj) == ARRAY)
  268.          {
  269.          
  270.          // if closed the error, else continue
  271.  
  272.          if (ComId < 0)
  273.             {
  274.             MessageBox(CmdHWindow, "PORT not open", "Error", MB_OK);
  275.             }
  276.          else
  277.             {
  278.             
  279.             // get min of max array and the array
  280.  
  281.             count = min(min(getint(val),getarrdim(obj)),sizeof(txbuffer));
  282.             
  283.             // fill buffer with elements of the array
  284.  
  285.             for (i=0;i<count;i++)
  286.                {
  287.                item = litem(cons(make_intnode(i+getarrorg(obj)),cons(obj,NIL)));
  288.                txbuffer[i] = getint(cnv_node_to_numnode(item));
  289.                }
  290.             
  291.             // now write buffer
  292.  
  293.             status = WriteComm(ComId, txbuffer, count);
  294.             
  295.             // if problem GetComError will Put up Message box
  296.  
  297.             if (status < 0) GetCommError(ComId, NULL);
  298.             
  299.             // return byte count sent
  300.  
  301.             return(make_intnode(status));
  302.             }   
  303.          }
  304.       else
  305.          {
  306.          MessageBox(CmdHWindow, "First arg must be an array", "Error", MB_OK);
  307.          }
  308.       }
  309.    
  310.    return(make_intnode(0));
  311.    }
  312.  
  313. NODE *lportreadarray(NODE *args)
  314.    {
  315.    char rxbuffer[MAX_BUFFER_SIZE];
  316.    
  317.    int count;
  318.    int i;
  319.    
  320.    NODE *val;
  321.    NODE *obj;
  322.    
  323.    COMSTAT Stat;  
  324.    
  325.    val = pos_int_arg(args);
  326.    obj = cadr(args);
  327.    
  328.    while ((obj == NIL || obj == Null_Word) && NOT_THROWING)
  329.       {
  330.       setcar(cdr(args), err_logo(BAD_DATA, obj));
  331.       obj = cadr(args);
  332.       }
  333.    
  334.    if (NOT_THROWING)
  335.       {
  336.       if (nodetype(obj) == ARRAY)
  337.          {
  338.          
  339.          // if closed the error, else continue
  340.  
  341.          if (ComId < 0)
  342.             {
  343.             MessageBox(CmdHWindow, "PORT not open", "Error", MB_OK);
  344.             }
  345.          else
  346.             {
  347.  
  348.             // get status on port (returns pending bytes)
  349.  
  350.             GetCommError(ComId, &Stat);
  351.             
  352.             // if something pending continue
  353.  
  354.             if (Stat.cbInQue)
  355.                {
  356.                
  357.                // don't overflow buffer
  358.  
  359.                count = min(min(min(getarrdim(obj),getint(val)),Stat.cbInQue), sizeof(rxbuffer));
  360.                
  361.                // do the read
  362.  
  363.                ReadComm(ComId, rxbuffer, count);
  364.                
  365.                // now fill in the array
  366.  
  367.                for (i=0;i<count;i++)
  368.                   {
  369.                   lsetitem(
  370.                   cons(make_intnode(i+getarrorg(obj)),
  371.                   cons(obj,
  372.                   cons(make_intnode(rxbuffer[i]),
  373.                   NIL))));
  374.                   }
  375.                
  376.                // return actual transfered
  377.  
  378.                return(make_intnode(count));
  379.                }
  380.             else
  381.                {
  382.                return(make_intnode(0));
  383.                }
  384.             }
  385.          }
  386.       }   
  387.    
  388.    return(make_intnode(0));
  389.    
  390.    }
  391.  
  392. NODE *lportwritechar(NODE *args)
  393.    {
  394.    char txchar[1];  
  395.    
  396.    int status;   
  397.    
  398.    // get arg
  399.  
  400.    txchar[0] = getint(pos_int_arg(args));
  401.    
  402.    // if not open output error, else continue
  403.  
  404.    if (ComId < 0)
  405.       {
  406.       MessageBox(CmdHWindow, "PORT not open", "Error", MB_OK);
  407.       }
  408.    else
  409.       {     
  410.  
  411.       // write the 1 byte
  412.  
  413.       status = WriteComm(ComId, txchar, 1);
  414.       
  415.       // if problem GetComError will Put up Message box
  416.  
  417.       if (status < 0) GetCommError(ComId, NULL);
  418.       
  419.       // return byte count sent
  420.  
  421.       return(make_intnode(status));
  422.       }   
  423.    
  424.    return(make_intnode(0));
  425.    }
  426.  
  427. NODE *lportreadchar(void)
  428.    {
  429.    char rxchar[1];
  430.    
  431.    COMSTAT Stat;
  432.    
  433.    // if closed output error, else continue
  434.  
  435.    if (ComId < 0)
  436.       {
  437.       MessageBox(CmdHWindow, "PORT not open", "Error", MB_OK);
  438.       }
  439.    else
  440.       {
  441.       // get status on port (returns pending bytes)
  442.  
  443.       GetCommError(ComId, &Stat);
  444.       
  445.       // if a byte the read it and return it
  446.  
  447.       if (Stat.cbInQue)
  448.          {
  449.          ReadComm(ComId, rxchar, min(1,Stat.cbInQue));
  450.          
  451.          return(make_intnode(rxchar[0]));
  452.          }
  453.       else
  454.          {
  455.          return(make_intnode(-1));
  456.          }
  457.       }   
  458.    
  459.    return(make_intnode(-1));
  460.    }
  461.