home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Drivers / joystick-0.0-I / linux / js.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-07  |  1.2 KB  |  64 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.  
  15. int main (int argc, char **argv)
  16. {
  17.     int fd, status;
  18.     char *fname;
  19.     struct JS_DATA_TYPE js;
  20.  
  21.     /* should be one argument, and it should be "0" or "1" */
  22.     if (argc != 2 || (strcmp (argv[1], "0") && strcmp (argv[1], "1"))) {
  23.         fprintf (stderr, "usage: js 0|1\n");
  24.         exit (1);
  25.     }
  26.  
  27.     /* pick appropriate device file */
  28.     if (!strcmp (argv[1], "0"))
  29.         fname = "/dev/js0";
  30.     else if (!strcmp (argv[1], "1"))
  31.         fname = "/dev/js1";
  32.     else
  33.         fname = NULL;
  34.  
  35.     /* open device file */
  36.     fd = open (fname, O_RDONLY);
  37.     if (fd < 0) {
  38.         perror ("js");
  39.         exit (1);
  40.     }
  41.  
  42.     printf ("Joystick test program (interrupt to exit)\n");
  43.  
  44.     while (1) {
  45.         status = read (fd, &js, JS_RETURN);
  46.         if (status != JS_RETURN) {
  47.             perror ("js");
  48.             exit (1);
  49.         }
  50.  
  51.         fprintf (stdout, "button 0: %s  button 1: %s  X position: %4d  Y position: %4d\r",
  52.              (js.buttons & 1) ? "on " : "off",
  53.              (js.buttons & 2) ? "on " : "off",
  54.              js.x,
  55.              js.y);
  56.         fflush (stdout);
  57.  
  58.         /* give other processes a chance */
  59.         usleep (100);
  60.     }
  61.  
  62.     exit (0);
  63. }
  64.