[comp.sources.misc] Microsoft serial mouse routines update

allbery@ncoast.UUCP (08/19/87)

Enclosed is a shell archive containing my modifications to the serial mouse
routines written by John Chapman (john@bby-bc.UUCP) 

I added error checking along with some modularization and efficiency
changes. Also included is a small program called "track" to test these
routines.

These routines were tested with a Microsoft compatible PC mouse and
Microport UNIX 2.2. 

-----
Anthony J. Starks		...{ihnp4 | seismo}!iuvax!hpuinda!mdri!ajs
Merrell Dow Research Institute
P.O. Box 68470 
Indianapolis, IN 46268



#------------------cut here------------------------------------------------
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	Makefile
#	smouse.h
#	smouse.c
#	track.c
# This archive created: Fri Aug  7 13:13:33 1987
export PATH; PATH=/bin:$PATH
if test -f 'Makefile'
then
	echo shar: will not over-write existing file "'Makefile'"
else
cat << \SHAR_EOF > 'Makefile'
#
#  Makefile for smouse routines
#
track:		track.o smouse.o
		cc $(CFLAGS) track.o smouse.o -o track

track.o:	track.c smouse.h
		cc $(CFLAGS) -c track.c

smouse.o:	smouse.c smouse.h
		cc $(CFLAGS) -c smouse.c

shar:		
		shar Makefile smouse.h smouse.c track.c >smouse.shar
clean:		
		rm -f track.o
SHAR_EOF
fi # end of overwriting check
if test -f 'smouse.h'
then
	echo shar: will not over-write existing file "'smouse.h'"
else
cat << \SHAR_EOF > 'smouse.h'
/*
 *  Constants and definitions for the Microsoft serial mouse routines
 */

#define MOUSE_DEV	"/dev/tty1"
#define NCOORD		4
#define NBUTTON		3
#define SYSERR		(-1)
#define SUCCESS		(0)

typedef struct {
	int dx;
	int dy;
	int button[NBUTTON];
} Mouse;


SHAR_EOF
fi # end of overwriting check
if test -f 'smouse.c'
then
	echo shar: will not over-write existing file "'smouse.c'"
else
cat << \SHAR_EOF > 'smouse.c'
/*
 *  Mouse routines for the Microsoft serial mouse and compatibles.
 *
 *  Adapted from routines posted by john@bby-bc.UUCP in <146@bby-bc.UUCP>.
 *
 *  Anthony J. Starks (ajs@mdri)
 */
#include	<fcntl.h>
#include	<errno.h>
#include	<termio.h>
#include	"smouse.h"

static int mfd;

MouseInit()
{
	struct termio t;
	
	errno = 0;
	if ((mfd = open(MOUSE_DEV, O_RDONLY)) < 0) 
	{
		perror("mouse open");
		return SYSERR;
	}

	if (ioctl(mfd,TCGETA, &t) < 0)
	{
		perror("mouse get parameter");
		return SYSERR;
	}
		
	t.c_iflag &= ~(IXON|IXOFF|ISTRIP|INLCR|IGNCR|ICRNL|IUCLC|INPCK);
	t.c_cflag = B1200 | CS8 | CLOCAL | CREAD ;
	t.c_lflag = 0;

	if (ioctl(mfd,TCSETA, &t) < 0)
	{
		perror("mouse set parameter");
		return SYSERR;
	}
	
	return SUCCESS;
}

MouseRead(m)
Mouse *m;
{
	unsigned char	b;
	char		coord[NCOORD];

	/*
	 *  Synchronize on a button byte
	 */
	for (b=0; (b&0370) != 0200 ; ) 
		if (read(mfd, &b, 1) < 0) return SYSERR;
	
	/*
	 *  Read the coordinates
	 */
	if (read(mfd, coord, NCOORD) < 0) return SYSERR;
	
	/*
	 *  Read button bits
	 */

	m->button[2] = (~b)  & 1;
	m->button[1] = (~(b>>1)) & 1;
	m->button[0] = (~(b>>2)) & 1;
	
	/*
	 *  Pack up the coordinate half-words
	 */
	m->dx  =  (int)(coord[0] + coord[2]);
	m->dy  =  (int)(coord[1] + coord[3]);
	
	return SUCCESS;

}

MouseClose()
{
	close(mfd);
}
SHAR_EOF
fi # end of overwriting check
if test -f 'track.c'
then
	echo shar: will not over-write existing file "'track.c'"
else
cat << \SHAR_EOF > 'track.c'
/*
 *  track -- track the mouse using the serial mouse routines.
 */

#include "smouse.h"

main()
{
	Mouse m;
	
	if (MouseInit() < 0) exit(1);
	while (!m.button[2])
	{
		if (MouseRead(&m) < 0) break;

		printf("dx = %4d dy = %4d b1 = %1d b2 = %1d b3 = %1d\n",
			m.dx, m.dy, m.button[0], m.button[1], m.button[2]);
		
	}
	MouseClose();
}
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0