/*
 *   Copyright (c) 1998-1999 David Parsons. All rights reserved.
 *   
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *  3. All advertising materials mentioning features or use of this
 *     software must display the following acknowledgement:
 *     
 *   This product includes software developed by David Parsons
 *   (orc@pell.chi.il.us)
 *
 *  4. My name may not be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *     
 *  THIS SOFTWARE IS PROVIDED BY DAVID PARSONS ``AS IS'' AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 *  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
 *  PARSONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 *  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 *  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 *  THE POSSIBILITY OF SUCH DAMAGE.
 */

static const char rcsid[] = "$Id: s.mousedetect.c 1.3 99/11/21 00:06:49-00:00 orc $";

/*
 * Mousedetect() attempts to detect vermin.  All it knows about is PS/2
 * mice and Microsoft mice, because that's all I've been able to find
 * programming information on.
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
#include "util.h"


/*
 * look for a serial mouse on the given port
 *
 * Microsoft mice will echo M if you toggle DTR on the mouseport.
 * Logitech mice will echo M, plus some long identification string
 * that appears to begin with 033h, 08h, 01h.  Other mice will just
 * sit there and sulk.
 */
int
detectSerialMouse(char *port)
{
    int retval = NO_MOUSE;
    int fd;
    char bfr[4];
    int siz;
    int rc;
    struct termios tty;
    struct timeval tick;
    fd_set readers;

    static char logitech_ident[] = { 'M', 0x33, 0x08, 0x01 };

    if ((fd = open(port, O_RDWR|O_NDELAY)) < 0)
	return NO_MOUSE;

    tcgetattr(fd, &tty);
    cfsetispeed(&tty, B1200);
    cfsetospeed(&tty, B1200);

    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS7;

    tcsetattr(fd, TCSANOW, &tty);
    usleep(100);
    tcflush(fd, TCIOFLUSH);

    ioctl(fd, TIOCMBIC, TIOCM_DTR);
    usleep(100);
    ioctl(fd, TIOCMBIS, TIOCM_DTR);
    usleep(100);

    tick.tv_sec = 1;
    tick.tv_usec = 0;
    FD_ZERO(&readers);
    FD_SET(fd, &readers);

    rc = select(fd+1, &readers, 0, 0, &tick);

    if (rc > 0 && read(fd, &bfr, 1) > 0 && bfr[0] == 'M') {
	/* it's an MS or Logitech mouse.  If it's a logitech
	 * mouse, it will spit out an identification string
	 * that APPEARS to be prefixed with 0x33, 0x08, 0x01
	 */
	 retval = MS_MOUSE;
	 usleep(300000);
	 tick.tv_sec = 0;
	 tick.tv_usec = 0;	/* 1/10th second; enough time to clock 3
				 * characters in? */

	 FD_ZERO(&readers);
	 FD_SET(fd, &readers);

	 rc = select(fd+1, &readers, 0, 0, &tick);

	 if (rc > 0 && (siz = read(fd, bfr+1, 3)) == 3
		    && memcmp(bfr, logitech_ident, 4) == 0)
	    retval = LOGITECH_MOUSE;
    }
    close(fd);

    return retval;
} /* detectSerialMouse */


/*
 * detectPS2Mouse() attempts to open /dev/psaux, in hopes that some vermin
 * will be there.
 */
#define PS2PORT	"/dev/psaux"

char *
detectPS2Mouse()
{
    int fd = open(PS2PORT, O_RDONLY);

    if (fd >= 0) {
	close(fd);
	return PS2PORT;
    }
    return 0;
} /* detectPS2Mouse */


#ifdef TEST
main()
{
    static char *Vermin[3] = { "Nada", "Microsoft", "Logitech" };
    int rc;

    if (detectPS2Mouse())
	puts("PS/2 mouse");

    if ((rc = detectSerialMouse("/dev/ttyS0")) != NO_MOUSE)
	printf("%s mouse detected on COM1:\n", Vermin[rc]);
    else if ((rc = detectSerialMouse("/dev/ttyS1")) != NO_MOUSE)
	printf("%s mouse detected on COM2:\n", Vermin[rc]);
    else if ((rc = detectSerialMouse("/dev/ttyS2")) != NO_MOUSE)
	printf("%s mouse detected on COM3:\n", Vermin[rc]);
    else if ((rc = detectSerialMouse("/dev/ttyS3")) != NO_MOUSE)
	printf("%s mouse detected on COM4:\n", Vermin[rc]);
}
#endif
