home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Drivers / joystick-0.0-I / joystick.next / test / js.c next >
Encoding:
C/C++ Source or Header  |  1997-11-03  |  1.4 KB  |  69 lines

  1. /*
  2.    simple test program for joystick driver
  3.  
  4.    usage: js 0       (to test first joystick)
  5.    usage: js 1       (to test second joystick)
  6. */
  7.  
  8. // #include <linux/joystick.h>
  9. #include "joystick.h"
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <errno.h>
  15.  
  16. int main (int argc, char **argv)
  17. {
  18.     int fd, status;
  19.     char *fname;
  20.     struct JS_DATA_TYPE js;
  21.  
  22.     /* should be one argument, and it should be "0" or "1" */
  23.     if (argc != 2 || (strcmp (argv[1], "0") && strcmp (argv[1], "1"))) {
  24.         fprintf (stderr, "usage: js 0|1\n");
  25.         exit (1);
  26.     }
  27.  
  28.     /* pick appropriate device file */
  29.     if (!strcmp (argv[1], "0"))
  30.         fname = "/dev/js0";
  31.     else if (!strcmp (argv[1], "1"))
  32.         fname = "/dev/js1";
  33.     else
  34.         fname = NULL;
  35.  
  36.     /* open device file */
  37.     fd = open (fname, O_RDONLY);
  38.     if (fd < 0) {
  39.         perror (fname);
  40.         exit (1);
  41.     }
  42.  
  43.     printf ("Joystick test program (interrupt to exit)\n");
  44.  
  45.     while (1) {
  46.         status = read (fd, &js, JS_RETURN);
  47.         if (status != JS_RETURN)
  48.         {
  49.             int temperrno = errno;
  50.             perror ("js read");
  51.             printf ("js - status = %d, errno = %d\n",
  52.                 status, temperrno);
  53.             exit (1);
  54.         }
  55.  
  56.         fprintf (stdout, "button 0: %s  button 1: %s  X position: %4d  Y position: %4d\r",
  57.              (js.buttons & 1) ? "on " : "off",
  58.              (js.buttons & 2) ? "on " : "off",
  59.              js.x,
  60.              js.y);
  61.         fflush (stdout);
  62.  
  63.         /* give other processes a chance */
  64.         usleep (100);
  65.     }
  66.  
  67.     exit (0);
  68. }
  69.