home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <ctype.h>
- #include <termio.h>
-
- #define MOUSE_MOVE 0x01
- #define LEFT_PRESS 0x02
- #define LEFT_RELEASE 0x04
- #define RIGHT_PRESS 0x08
- #define RIGHT_RELEASE 0x10
- #define CENTER_PRESS 0x20
- #define CENTER_RELEASE 0x40
- #define DOUBLE_CLICK 0x80
-
- #define EVENTS_MOVE 0x01
- #define EVENTS_ALL 0x02
- #define XY_PHYSICAL 0x04
- #define XY_RELATIVE 0x08
-
- #define NOESC 0
- #define ESC1 1
- #define ESC2 2
- #define STATUS 3
- #define COLUMN 4
- #define ROW 5
-
- #define CLEAR "\033[=14F\033[=1G\033[2J"
- #define START "\033\033Me%dX"
- #define INIT "\033\033Mi%d;%d;%dX"
- #define FORMAT "\033\033Mf'%s'"
- #define CLOSE "\033\033Mc"
-
- static struct termio sv_las;
- static int sv = 0;
-
- iniio()
-
- { struct termio B;
-
- if(!sv) ioctl(0,TCGETA,(char*)&sv_las);
- sv = 1;
- ioctl(0,TCGETA,(char*)&B);
- B.c_lflag &= ~ICANON;
- B.c_lflag &= ~ECHO;
- B.c_iflag &= ~ICRNL;
- B.c_cc[VMIN] = 1;
- B.c_cc[VINTR] = 0xff;
- ioctl(0,TCSETAW,&B);
- }
-
- endio()
-
- { if(sv) ioctl(0,TCSETAW,&sv_las);
- }
-
- MouseInit()
-
- { char buf[128], format[128];
- int mode;
-
- /* Clear screen */
- write(1,CLEAR,strlen(CLEAR));
-
- /* Message */
- write(1,"Hit $ to exit",13);
-
- /* Init Mouse */
- mode = LEFT_PRESS|LEFT_RELEASE|RIGHT_PRESS|RIGHT_RELEASE|MOUSE_MOVE;
- sprintf(buf,INIT,mode,1,5);
- write(1,buf,strlen(buf));
-
- /* Set Event format */
- strcpy(format,"\033\033E%d;%d;%dX");
- sprintf(buf,FORMAT,format);
- write(1,buf,strlen(buf));
- }
-
- MouseStart()
-
- { char buf[128];
-
- /* Start mouse */
- sprintf(buf,START,EVENTS_MOVE|XY_PHYSICAL);
- write(1,buf,strlen(buf));
- }
-
- MouseClose()
-
- { char buf[128];
-
- /* Close mouse */
- write(1,CLOSE,strlen(CLOSE));
- }
-
- main()
-
- { unsigned char car, buf[32];
- int event, line, col, status;
-
- /* IO Init */
- iniio();
-
- /* Mouse Init */
- MouseInit();
-
- /* Mouse Start */
- MouseStart();
-
- status = NOESC;
- /* Read mouse */
- while(read(0,&car,1))
- { if(car == '$') break;
- switch(status)
- { case NOESC:
- if(car == '\033')
- { status = ESC1;
- event = line = col = 0;
- break;
- }
- write(1,&car,1);
- break;
-
- case ESC1:
- if(car == '\033') status = ESC2;
- else status = NOESC;
- break;
-
- case ESC2:
- if(car == 'E') status = STATUS;
- else status = NOESC;
- break;
-
- case STATUS:
- if(isdigit(car))
- { event = event * 10 + car -'0';
- break;
- }
- if(car == ';') status = COLUMN;
- else status = NOESC;
- break;
-
- case COLUMN:
- if(isdigit(car))
- { col = col * 10 + car -'0';
- break;
- }
- if(car == ';') status = ROW;
- else status = NOESC;
- break;
-
- case ROW:
- if(isdigit(car))
- { line = line * 10 + car - '0';
- break;
- }
- if(car == 'X')
- { status = NOESC;
- *buf = 0;
- if(event & MOUSE_MOVE)
- sprintf(buf,"\033[%d;%dHm",line+1,col+1);
- if(event & LEFT_PRESS)
- sprintf(buf,"\033[%d;%dHl",line+1,col+1);
- if(event & RIGHT_PRESS)
- sprintf(buf,"\033[%d;%dHr",line+1,col+1);
- if(event & DOUBLE_CLICK)
- sprintf(buf,"\033[%d;%dHd",line+1,col+1);
- if(*buf) write(1,buf,strlen(buf));
- break;
- }
- status = NOESC;
- break;
- }
- }
-
- /* Close mouse */
- MouseClose();
-
- /* IO reset */
- endio();
-
- /* Clear screen */
- write(1,CLEAR,strlen(CLEAR));
- }
-