/* 
 * pentest.c - written by Rene K. Mueller <kiwi@the-labs.com>
 *
 * Version 0.10 - Jul 27, 2001
 *    http://the-labs.com/Stylistic/1000.html
 *
 * Infos extracted from
 *    xf86fpit.c as available at http://www.linuxslate.org/
 *
 * Compiles under FreeBSD + Linux, other UNIXes not tested
 *
 * License: BSD License (see with google.com for most recent BSD License)
 */
 
#include <stdio.h>
#include <errno.h>
#include <termios.h>
#include <fcntl.h>
#include <ctype.h>

/* 
 * pen-modes:
 *    "A" send config
 *    "F" abs mode
 *    "E" relative mode
 *    "b" uper-left coord
 *    "B" prompt-mode
 *    "@" stream-mode
 *    "I" set increment
 *   "zb" binary reporting
 */

main(int argc, char *argv[]) {
   struct termios tty;
   unsigned char buff[5]; int i = 0; int fd;

   if(argc!=2)
      fprintf(stderr,"USAGE: <device> (e.g. /dev/ttyS3 under Linux)\n"), exit(1);
   fd = open(argv[1],O_RDWR|O_NDELAY,0);
   if(fd<0)
      fprintf(stderr,"opening device failed %s\n",argv[1]), exit(0);
   if(tcgetattr(fd,&tty)<0) 
      fprintf(stderr,"couldn't probe defaults of %s\n",argv[1]), exit(0);

   tty.c_iflag = 0;
   tty.c_cflag = B19200|CRTSCTS|CS8|CLOCAL|CREAD;
   tty.c_lflag = 0;
   tty.c_cc[VINTR] = 0;
   tty.c_cc[VQUIT] = 0;
   tty.c_cc[VKILL] = 0;
   tty.c_cc[VEOF] = 0;
   tty.c_cc[VEOL] = 0;
   tty.c_cc[VMIN] = 1;
   tty.c_cc[VTIME] = 0;
#ifdef VWERASE
    tty.c_cc[VWERASE] = 0;
#endif
#ifdef VREPRINT
    tty.c_cc[VREPRINT] = 0;
#endif

#ifdef VEOL2
    tty.c_cc[VEOL2] = 0;
#endif
    tty.c_cc[VSUSP] = 0;
#ifdef VDISCARD
    tty.c_cc[VDISCARD] = 0;
#endif
#ifdef VLNEXT
    tty.c_cc[VLNEXT] = 0; 
#endif
   if(tcsetattr(fd,TCSANOW,&tty)<0)
      fprintf(stderr,"pen settings failed.\n"), exit(1);

   /* init pen */
   write(fd,"\0",1);
   usleep(400000);
   tcflush(fd,TCIFLUSH);
   fprintf(stderr,"init done.\n");

   /* set streaming mode */
   write(fd,"zb@",3);
   usleep(400000);
   tcflush(fd,TCIFLUSH);
   while(1) {
      if(read(fd,buff+i,1)==1) {
         //fprintf(stderr,"%d (%dth byte)\n",buff[i],i+1);

/* Format of 5 bytes data packet for FPIT Tablets
       Byte 1
       bit 7  Phasing bit always 1
       bit 6  Switch status change
       bit 5  Proximity
       bit 4  Always 0
       bit 3  Test data
       bit 2  Sw3 (2nd side sw) 
       bit 1  Sw2 (1st side sw) 
       bit 0  Sw1 (Pen tip sw) 

       Byte 2
       bit 7  Always 0
       bits 6-0 = X6 - X0

       Byte 3
       bit 7  Always 0
       bits 6-0 = X13 - X7

       Byte 4
       bit 7  Always 0
       bits 6-0 = Y6 - Y0

       Byte 5
       bit 7  Always 0
       bits 6-0 = Y13 - Y7
*/
         if(i==4) {
            int x = buff[1]|(buff[2]<<7);
            int y = buff[3]|(buff[4]<<7);
            fprintf(stderr,"coord %5d,%5d  sw3=%s sw2=%s sw=%s (bits: ",
               x,y,
               buff[0]&0x04?"on ":"off",
               buff[0]&0x02?"on ":"off",
               buff[0]&0x01?"on ":"off");
            for(i=7; i>=0; i--)
               fprintf(stderr,"%d",buff[0]&(1<<i)?1:0);
            fprintf(stderr,")\n");
            i = 0;
         } else {
            /* see if it's sync bit */
            if(i&&buff[i]&0x80) 
               fprintf(stderr,"lost sync, found it again!\n"), i = 1;
            else 
               i++;
         }
      } else {
         //fprintf(stderr,"...\n");
      }
   }
}
 
