home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 10 / mycd10.iso / progs / tun / dos / emul / emul.arc / MOUSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  3.2 KB  |  183 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <termio.h>
  4.  
  5. #define    MOUSE_MOVE    0x01
  6. #define    LEFT_PRESS    0x02
  7. #define    LEFT_RELEASE    0x04
  8. #define    RIGHT_PRESS    0x08
  9. #define    RIGHT_RELEASE    0x10
  10. #define    CENTER_PRESS    0x20
  11. #define    CENTER_RELEASE    0x40
  12. #define    DOUBLE_CLICK    0x80
  13.  
  14. #define    EVENTS_MOVE    0x01
  15. #define    EVENTS_ALL    0x02
  16. #define    XY_PHYSICAL    0x04
  17. #define    XY_RELATIVE    0x08
  18.  
  19. #define    NOESC        0
  20. #define    ESC1        1
  21. #define    ESC2        2
  22. #define    STATUS        3
  23. #define    COLUMN        4
  24. #define    ROW        5
  25.  
  26. #define    CLEAR        "\033[=14F\033[=1G\033[2J"
  27. #define    START        "\033\033Me%dX"
  28. #define    INIT        "\033\033Mi%d;%d;%dX"
  29. #define    FORMAT        "\033\033Mf'%s'"
  30. #define    CLOSE        "\033\033Mc"
  31.  
  32. static struct    termio    sv_las;    
  33. static         int    sv = 0;
  34.  
  35. iniio()
  36.  
  37. {    struct    termio    B;    
  38.  
  39.     if(!sv)    ioctl(0,TCGETA,(char*)&sv_las);
  40.     sv = 1;
  41.     ioctl(0,TCGETA,(char*)&B);
  42.     B.c_lflag &= ~ICANON;
  43.     B.c_lflag &= ~ECHO;
  44.     B.c_iflag &= ~ICRNL;
  45.     B.c_cc[VMIN] = 1;
  46.     B.c_cc[VINTR] = 0xff;
  47.     ioctl(0,TCSETAW,&B);
  48. }
  49.  
  50. endio()
  51.  
  52. {    if(sv)    ioctl(0,TCSETAW,&sv_las);
  53. }
  54.  
  55. MouseInit()
  56.  
  57. {    char    buf[128], format[128];
  58.     int    mode;
  59.  
  60.     /* Clear screen */
  61.     write(1,CLEAR,strlen(CLEAR));
  62.  
  63.     /* Message */
  64.     write(1,"Hit $ to exit",13);
  65.  
  66.     /* Init Mouse */
  67.     mode = LEFT_PRESS|LEFT_RELEASE|RIGHT_PRESS|RIGHT_RELEASE|MOUSE_MOVE;
  68.     sprintf(buf,INIT,mode,1,5);
  69.     write(1,buf,strlen(buf));
  70.  
  71.     /* Set Event format */
  72.     strcpy(format,"\033\033E%d;%d;%dX");
  73.     sprintf(buf,FORMAT,format);
  74.     write(1,buf,strlen(buf));
  75. }
  76.  
  77. MouseStart()
  78.  
  79. {    char    buf[128];
  80.  
  81.     /* Start mouse */
  82.     sprintf(buf,START,EVENTS_MOVE|XY_PHYSICAL);
  83.     write(1,buf,strlen(buf));
  84. }
  85.  
  86. MouseClose()
  87.  
  88. {    char    buf[128];
  89.  
  90.     /* Close mouse */
  91.     write(1,CLOSE,strlen(CLOSE));
  92. }
  93.  
  94. main()
  95.  
  96. {    unsigned char    car, buf[32];
  97.     int    event, line, col, status;
  98.  
  99.     /* IO Init */
  100.     iniio();
  101.     
  102.     /* Mouse Init */
  103.     MouseInit();
  104.  
  105.     /* Mouse Start */
  106.     MouseStart();
  107.  
  108.     status = NOESC;
  109.     /* Read mouse */
  110.     while(read(0,&car,1))
  111.     {    if(car == '$')    break;
  112.         switch(status)
  113.         {    case    NOESC:
  114.             if(car == '\033')
  115.             {      status = ESC1;
  116.                 event = line = col = 0;
  117.                 break;
  118.             }
  119.             write(1,&car,1);
  120.             break;
  121.         
  122.             case    ESC1:
  123.             if(car == '\033')    status = ESC2;
  124.             else            status = NOESC;
  125.             break;
  126.  
  127.             case    ESC2:
  128.             if(car == 'E')        status = STATUS;
  129.             else            status = NOESC;
  130.             break;
  131.  
  132.             case    STATUS:
  133.             if(isdigit(car))
  134.             {    event = event * 10 + car -'0';
  135.                 break;
  136.             }
  137.             if(car == ';')        status = COLUMN;
  138.             else            status = NOESC;
  139.             break;
  140.  
  141.             case    COLUMN:
  142.             if(isdigit(car))
  143.             {    col = col * 10 + car -'0';
  144.                 break;
  145.             }
  146.             if(car == ';')        status = ROW;
  147.             else            status = NOESC;
  148.             break;
  149.  
  150.             case    ROW:
  151.             if(isdigit(car))
  152.             {    line = line * 10 + car - '0';
  153.                 break;
  154.             }
  155.             if(car == 'X')
  156.             {    status = NOESC;
  157.                 *buf = 0;
  158.                 if(event & MOUSE_MOVE)
  159.                   sprintf(buf,"\033[%d;%dHm",line+1,col+1);
  160.                 if(event & LEFT_PRESS)
  161.                   sprintf(buf,"\033[%d;%dHl",line+1,col+1);
  162.                 if(event & RIGHT_PRESS)
  163.                   sprintf(buf,"\033[%d;%dHr",line+1,col+1);
  164.                 if(event & DOUBLE_CLICK)
  165.                   sprintf(buf,"\033[%d;%dHd",line+1,col+1);
  166.                 if(*buf)    write(1,buf,strlen(buf));
  167.                 break;
  168.             }
  169.             status = NOESC;
  170.             break;
  171.         }
  172.     }
  173.  
  174.     /* Close mouse */
  175.     MouseClose();
  176.  
  177.     /* IO reset */
  178.     endio();
  179.  
  180.     /* Clear screen */
  181.     write(1,CLEAR,strlen(CLEAR));
  182. }
  183.