ms.c

来自「linux 下svgalib编的一个界面程序示例」· C语言 代码 · 共 1,183 行 · 第 1/3 页

C
1,183
字号
#ifdef DEBUG_ACCEL	  printf("m_accel_powertable:\n");#endif          for (i = 1; i < m_accel_thresh; i++) {            m_accel_powertable[i] = pow((double)(i-1)/(m_accel_thresh-1), m_accel_power)              * (m_accel_mult - m_accel_offset) + m_accel_offset;#ifdef DEBUG_ACCEL	    printf("  %ld => %f (%f)\n", (long)i, (double)m_accel_powertable[i] * i,	  			     (double)m_accel_powertable[i]);#endif          }        }      }    }#ifdef DEBUG_ACCEL    printf("m_accel_type: %ld\n",	(long)m_accel_type);    printf("m_force: %ld\n",		(long)m_force);    printf("m_accel_thresh: %ld\n",	(long)m_accel_thresh);    printf("m_accel_offset: %ld\n",	(long)m_accel_offset);    printf("m_accel_mult: %f\n",	(double)m_accel_mult);    printf("m_accel_power: %f\n",	(double)m_accel_power);    printf("m_maxdelta: %ld\n",		(long)m_maxdelta);    printf("m_accel_maxdelta: %ld\n",	(long)m_accel_maxdelta);#endif    /*  Ensure that the open will get a file descriptor greater     *  than 2, else problems can occur with stdio functions     *  under certain strange conditions:  */    if (fcntl(0,F_GETFD) < 0) open("/dev/null", O_RDONLY);    if (fcntl(1,F_GETFD) < 0) open("/dev/null", O_WRONLY);    if (fcntl(2,F_GETFD) < 0) open("/dev/null", O_WRONLY);    /* Set the proper wheel delta */    if(m_wheel_steps)        m_wheel_delta = (360 / m_wheel_steps);    else        m_wheel_delta = 0;    /* Added O_NDELAY here. *//*    if(__svgalib_mouse_fd!=-1)close(__svgalib_mouse_fd);*/    if ((__svgalib_mouse_fd = open(m_dev, O_RDWR | O_NDELAY )) < 0)	return -1;    if (m_type == MOUSE_BUSMOUSE || m_type == MOUSE_PS2     		|| m_type == MOUSE_IMPS2 || m_type == MOUSE_GPM || m_type == MOUSE_DRMOUSE4DS)	m_modem_ctl = 0;    /* If no signal will change there is no need to restore       or safe original settings. */    if (!m_modem_ctl)	m_modem_info_valid = 0;    else {	/* Get current modem signals; keep silent on errors.. */	m_modem_info_valid = !ioctl(__svgalib_mouse_fd,				   TIOCMGET, &m_old_modem_info);	if (m_modem_info_valid) {	    unsigned long param = m_old_modem_info;	    /* Prepare new stat: */	    /*set DTR as ordered.. */	    if (m_modem_ctl & MOUSE_CHG_DTR) {		param &= ~TIOCM_DTR;		if (m_modem_ctl & MOUSE_DTR_HIGH)		    param |= TIOCM_DTR;	    }	    /*set RTS as ordered.. */	    if (m_modem_ctl & MOUSE_CHG_RTS) {		param &= ~TIOCM_RTS;		if (m_modem_ctl & MOUSE_RTS_HIGH)		    param |= TIOCM_RTS;	    }	    if (ioctl(__svgalib_mouse_fd, TIOCMSET, &param))		m_modem_info_valid = 0;	/* No try to restore if this failed */	}    }    if (m_type == MOUSE_SPACEBALL) {      m_baud = 9600;      ms_setspeed(1200, m_baud, cflag[m_type]);    } else if (m_type == MOUSE_LOGIMAN) {	ms_setspeed(9600, 1200, cflag[m_type]);	ms_setspeed(4800, 1200, cflag[m_type]);	ms_setspeed(2400, 1200, cflag[m_type]);	ms_setspeed(1200, 1200, cflag[m_type]);	write(__svgalib_mouse_fd, "*X", 2);	ms_setspeed(1200, m_baud, cflag[m_type]);    } else if (m_type == MOUSE_WACOM_GRAPHIRE) {    	m_baud = 9600;    	ms_setspeed(1200, m_baud, cflag[m_type]);    	/* Reset baud rate */    	write(__svgalib_mouse_fd, "\r$", 2);    	usleep(250000);    	/* Reset tablet */    	write(__svgalib_mouse_fd, "\r#", 2);    	usleep(75000);    	/* Set hardware filtering */    	write(__svgalib_mouse_fd, "\rSU3", 4);    	usleep(75000);    	/* Start sending coordinates */    	write(__svgalib_mouse_fd, "\rST\r", 4);    } else if (m_type == MOUSE_IMPS2 || m_type == MOUSE_DRMOUSE4DS) {	/* Initialize the mouse into wheel mode */	write(__svgalib_mouse_fd, "\363\310\363\144\363\120", 6);    } else if (m_type == MOUSE_PNP) {    	/* Need to do this termios stuff here, by hand, ms_setspeed won't 	   work with pnp */	struct termios tty; 	m_baud = 1200;	tcgetattr(__svgalib_mouse_fd, &tty);   	tty.c_iflag = IGNBRK | IGNPAR;	tty.c_oflag = 0;	tty.c_lflag = 0;	tty.c_line = 0;	tty.c_cc[VTIME] = 0;	tty.c_cc[VMIN] = 1;	tty.c_cflag = cflag[m_type] | B1200;	tcsetattr(__svgalib_mouse_fd, TCSAFLUSH, &tty);    } else if (m_type != MOUSE_BUSMOUSE && m_type != MOUSE_PS2) {	ms_setspeed(9600, m_baud, cflag[m_type]);	ms_setspeed(4800, m_baud, cflag[m_type]);	ms_setspeed(2400, m_baud, cflag[m_type]);	ms_setspeed(1200, m_baud, cflag[m_type]);	if (m_type == MOUSE_LOGITECH) {	    write(__svgalib_mouse_fd, "S", 1);	    ms_setspeed(m_baud, m_baud, cflag[MOUSE_MMSERIES]);	}	if (m_sample <= 0)	    write(__svgalib_mouse_fd, "O", 1);	else if (m_sample <= 15)	    write(__svgalib_mouse_fd, "J", 1);	else if (m_sample <= 27)	    write(__svgalib_mouse_fd, "K", 1);	else if (m_sample <= 42)	    write(__svgalib_mouse_fd, "L", 1);	else if (m_sample <= 60)	    write(__svgalib_mouse_fd, "R", 1);	else if (m_sample <= 85)	    write(__svgalib_mouse_fd, "M", 1);	else if (m_sample <= 125)	    write(__svgalib_mouse_fd, "Q", 1);	else	    write(__svgalib_mouse_fd, "N", 1);    }    /* Set the mouse caps */    mouse_caps.key = MOUSE_GOTCAPS;    /* Zero out first to be sure... */    mouse_caps.buttons = mouse_caps.axes = mouse_caps.info =        mouse_caps.reserved0 = mouse_caps.reserved1 = 0;    switch(m_type) {    case MOUSE_SPACEBALL:        /* 6 axes, many buttons */        mouse_caps.buttons =            MOUSE_LEFTBUTTON | MOUSE_MIDDLEBUTTON | MOUSE_RIGHTBUTTON |            MOUSE_FOURTHBUTTON | MOUSE_FIFTHBUTTON | MOUSE_SIXTHBUTTON |            MOUSE_RESETBUTTON;        mouse_caps.axes = MOUSE_6DIM;        break;        case MOUSE_DRMOUSE4DS:    	/* X, Y, RX and RY (two wheels), 3 buttons */	mouse_caps.axes = MOUSE_2DIM | MOUSE_RXDIM | MOUSE_RYDIM;	mouse_caps.buttons = MOUSE_LEFTBUTTON | MOUSE_RIGHTBUTTON | MOUSE_MIDDLEBUTTON;	mouse_caps.info = MOUSE_INFO_WHEEL;	break;	    case MOUSE_INTELLIMOUSE:    case MOUSE_IMPS2:    case MOUSE_WACOM_GRAPHIRE:        /* X, Y, RX (wheel), 3 buttons, wheel */        mouse_caps.axes = MOUSE_RXDIM;        mouse_caps.info = MOUSE_INFO_WHEEL;    case MOUSE_LOGIMAN:		/* Some TrackMen have 4 buttons */	mouse_caps.buttons = MOUSE_FOURTHBUTTON;    case MOUSE_MOUSESYSTEMS:    case MOUSE_MMSERIES:    case MOUSE_LOGITECH:    case MOUSE_BUSMOUSE:    case MOUSE_PS2:    case MOUSE_GPM:        /* Any of these _can_ have 3 buttons, but may not */        mouse_caps.buttons |= MOUSE_MIDDLEBUTTON;    case MOUSE_MICROSOFT: /* Two buttons only */        mouse_caps.buttons |= MOUSE_LEFTBUTTON | MOUSE_RIGHTBUTTON;        mouse_caps.axes |= MOUSE_2DIM;        break;    }        return 0;}/* Scooped from X driver. */static inline void ms_close(void){    if (m_accel_powertable)        free(m_accel_powertable);    if (__svgalib_mouse_fd == -1)	return;    if (m_type == MOUSE_LOGITECH) {	write(__svgalib_mouse_fd, "U", 1);	ms_setspeed(m_baud, 1200, cflag[MOUSE_LOGITECH]);    }    /* Try to restore modem signals if we could get them. */    if (m_modem_info_valid)	ioctl(__svgalib_mouse_fd, TIOCMSET, &m_old_modem_info);    close(__svgalib_mouse_fd);    __svgalib_mouse_fd = -1;}#define MOUSEBUFFERSIZE 256static int get_ms_event(int wait) {/*   Changed to process multiple packets.   wait value:   0    Process any mouse events, and return status.   1    Wait for mouse event, then return.   Status indicates whether an event was processed. */    static unsigned char buf[MOUSEBUFFERSIZE];    static int nu_bytes = 0;    int nu_packets = 0;    char event_handled = 0;    int bytesread;    int i, wheel;/*  int but; */    static int but = 0;		/* static is hack for MouseMan */    static int mouse_orientation = 0;    int dx=0, dy=0, dz=0, drx=0, dry=0, drz=0;    int ax=0, ay=0;    static int oldax=0, olday=0, nodev = 0;    int j;    char SpaceWare[] = "SpaceWare!";    if (__svgalib_mouse_fd == -1)	return -1;  again:    if (m_fdmode == 1) {	/* We don't want to wait, set NDELAY mode. */	fcntl(__svgalib_mouse_fd, F_SETFL, O_RDONLY | O_NDELAY );	m_fdmode = 0;    }#ifdef BACKGROUND    /* No need to read mouse while background. */    if (vga_oktowrite()) {#endif        bytesread = read(__svgalib_mouse_fd,		     &buf[nu_bytes], MOUSEBUFFERSIZE - nu_bytes);#ifdef BACKGROUND       }     else       bytesread=0;#endif    i = 0;    if (bytesread >= 1)	nu_bytes += bytesread;#ifdef DEBUG    printf("#bytes in buffer: %d\n", nu_bytes);#endif  handle_packets:    /* Handle packets in buffer. */#ifdef DEBUG    printf("Bytes left in buffer: %d at %d, packet is %d bytes\n",	   nu_bytes - i, i, proto[m_type][4]);    if (nu_bytes - i > 0 )      printf("Header byte: %c %d\n", (buf[i] & 0177), buf[i]);#endif    if ((m_type == MOUSE_LOGIMAN) &&	((nu_bytes - i) >= 1) &&	((buf[i] & proto[m_type][0]) != proto[m_type][1]) &&	((char) (buf[i] & ~0x33) == 0)) { /* s/23/33/, for 4-but trackman */	/* Hack-o-matic, stolen from xf86_Mouse.c */	but = ((buf[i] & 0x20) >> 4) | ((buf[i] & 0x10) >> 1) | (but & 0x05);	__svgalib_mouse_eventhandler(but, 0, 0, 0, 0, 0, 0);	event_handled++;	i++;    }    if ((m_type == MOUSE_SPACEBALL)) {      j=i;      while ((nu_bytes - j > 0) && (buf[j]!=13))        j++;      nu_packets=(buf[j]==13);    } else {      nu_packets=1;    }    if ((nu_packets==0)||(nu_bytes - i < proto[m_type][4])) {	/* No full packet available. */	if (wait == 0 || (wait == 1 && event_handled)) {	    if (i >= nu_bytes) {		nu_bytes = 0;		i = 0;	    } else {		/* Move partial packet to front of buffer. */		for (j = i; j < nu_bytes; j++)		    buf[j - i] = buf[j];		nu_bytes -= i;	    }	    return event_handled;	} else {		/* (wait == 1 && !event_handled) */	    if (i >= nu_bytes) {		nu_bytes = 0;		i = 0;	    }	    /* Wait mode, we'll sleep on reads. */	    fcntl(__svgalib_mouse_fd, F_SETFL, O_RDONLY);	    m_fdmode = 1;#ifdef BACKGROUND            /* Waits while foreground again. No reading in background. */	    while(!vga_oktowrite()) sleep(0);#endif	    read(__svgalib_mouse_fd, &buf[nu_bytes], 1);            if ((m_type == MOUSE_SPACEBALL)) {              nu_packets=(buf[nu_bytes]==13);            } else {              nu_packets=1;            }	    nu_bytes++;	    if ((nu_packets==0)||(nu_bytes - i < proto[m_type][4]))		/* Not a complete packet. */		goto again;	}    }    /* Check header byte. */    if ((buf[i] & proto[m_type][0]) != proto[m_type][1]) {	/* Not a header byte. */#ifdef DEBUG    printf("Bad header byte: %c %d\n", (buf[i] & 0177), buf[i]);#endif	i++;	goto handle_packets;    }    /* Check whether it's a valid data packet. */    if ((m_type != MOUSE_PS2)&&(m_type != MOUSE_IMPS2)&&(m_type != MOUSE_SPACEBALL)    	&&(m_type != MOUSE_WACOM_GRAPHIRE) && (m_type != MOUSE_DRMOUSE4DS) )	for (j = 1; j < proto[m_type][4]; j++)	    if ((buf[i + j] & proto[m_type][2]) != proto[m_type][3]		|| buf[i + j] == 0x80) {		i = i + j + 1;		goto handle_packets;	    }    /* Construct the event. */    switch (m_type) {    case MOUSE_MICROSOFT:	/* Microsoft */    case MOUSE_LOGIMAN:	/* MouseMan / TrackMan */    case MOUSE_PNP:    default:	but = (but & 0x0A) | ((buf[i] & 0x20) >> 3) | ((buf[i] & 0x10) >> 4);	dx = (char) (((buf[i] & 0x03) << 6) | (buf[i + 1] & 0x3F));	dy = (char) (((buf[i] & 0x0C) << 4) | (buf[i + 2] & 0x3F));        break;    case MOUSE_WACOM_GRAPHIRE: /* Wacom Graphire Tablet */        if (!(buf[i] & 0x40)) { /* no device on tablet */        	nodev = 1;        	break;        }        but =   (buf[i+3] & 0x08) ? MOUSE_LEFTBUTTON : 0 |        	(buf[i+3] & 0x10) ? MOUSE_RIGHTBUTTON : 0 |        	(buf[i+3] & 0x20) ? MOUSE_MIDDLEBUTTON : 0;        /* The absolute position is returned, not the change in position, so           we convert it. */        ax = ((buf[i+0] & 0x03) << 14) | (buf[i+1] << 7) | buf[i+2];        ay = ((buf[i+3] & 0x03) << 14) | (buf[i+4] << 7) | buf[i+5];        if (nodev) {        	oldax = ax;        	olday = ay;        	nodev = 0;        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?