File: js.c

package info (click to toggle)
joystick 0.8.0-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 80 kB
  • ctags: 68
  • sloc: ansic: 436; makefile: 83; sh: 18
file content (62 lines) | stat: -rw-r--r-- 1,242 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
   simple test program for joystick driver

   usage: js 0       (to test first joystick)
   usage: js 1       (to test second joystick)
*/

#include <linux/joystick.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main (int argc, char **argv)
{
	int fd, status;
	char *fname;
	struct JS_DATA_TYPE js;

	/* should be one argument, and it should be "0" or "1" */
	if (argc != 2 || (strcmp (argv[1], "0") && strcmp (argv[1], "1"))) {
		fprintf (stderr, "usage: js 0|1\n");
		exit (1);
	}

	/* pick appropriate device file */
	if (!strcmp (argv[1], "0"))
		fname = "/dev/js0";
	else if (!strcmp (argv[1], "1"))
		fname = "/dev/js1";
	else
		fname = NULL;

	/* open device file */
	fd = open (fname, O_RDONLY);
	if (fd < 0) {
		perror ("js");
		exit (1);
	}

	printf ("Joystick test program (interrupt to exit)\n");

	while (1) {
		status = read (fd, &js, JS_RETURN);
		if (status != JS_RETURN) {
			perror ("js");
			exit (1);
		}

		fprintf (stdout, "button 0: %s  button 1: %s  X position: %4d  Y position: %4d\r",
			 (js.buttons & 1) ? "on " : "off",
			 (js.buttons & 2) ? "on " : "off",
			 js.x,
			 js.y);
		fflush (stdout);

		/* give other processes a chance */
		usleep (100);
	}

	exit (0);
}