annotate input/joystick.c @ 4524:01a0cf736e0d

Fix the bugs the previous version should fix (and those introduced by the previous version ;) )
author albeu
date Mon, 04 Feb 2002 14:19:54 +0000
parents 83128eed25f1
children 4ce20c55a18a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
1
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
2 #include "../config.h"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
3
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
4 #ifdef HAVE_JOYSTICK
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
5
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
6 #include "joystick.h"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
7 #include "input.h"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
8
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
9 #include <stdlib.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
10 #include <stdio.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
11 #include <string.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
12 #include <unistd.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
13 #include <sys/types.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
14 #include <sys/stat.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
15 #include <fcntl.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
16 #include <errno.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
17
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
18 #ifndef JOY_AXIS_DELTA
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
19 #define JOY_AXIS_DELTA 500
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
20 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
21
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
22 #ifndef JS_DEV
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
23 #define JS_DEV "/dev/input/js0"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
24 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
25
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
26 #ifdef TARGET_LINUX
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
27
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
28 #include <linux/joystick.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
29
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
30 int mp_input_joystick_init(char* dev) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
31 int fd,l=0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
32 int inited = 0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
33 struct js_event ev;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
34
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
35 printf("Opening joystick device %s\n",dev ? dev : JS_DEV);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
36
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
37 fd = open( dev ? dev : JS_DEV , O_RDONLY | O_NONBLOCK );
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
38 if(fd < 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
39 printf("Can't open joystick device %s : %s\n",dev ? dev : JS_DEV,strerror(errno));
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
40 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
41 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
42
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
43 while(! inited) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
44 l = 0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
45 while((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
46 int r = read(fd,&ev+l,sizeof(struct js_event)-l);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
47 if(r < 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
48 if(errno == EINTR)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
49 continue;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
50 else if(errno == EAGAIN) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
51 inited = 1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
52 break;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
53 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
54 printf("Error while reading joystick device : %s\n",strerror(errno));
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
55 close(fd);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
56 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
57 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
58 l += r;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
59 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
60 if((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
61 if(l > 0)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
62 printf("Joystick : we loose %d bytes of data\n",l);
4524
01a0cf736e0d Fix the bugs the previous version should fix (and those introduced
albeu
parents: 4518
diff changeset
63 break;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
64 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
65 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
66
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
67 return fd;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
68 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
69
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
70 int mp_input_joystick_read(int fd) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
71 struct js_event ev;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
72 int l=0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
73
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
74 while((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
75 int r = read(fd,&ev+l,sizeof(struct js_event)-l);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
76 if(r <= 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
77 if(errno == EINTR)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
78 continue;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
79 else if(errno == EAGAIN)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
80 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
81 if( r < 0)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
82 printf("Joystick error while reading joystick device : %s\n",strerror(errno));
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
83 else
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
84 printf("Joystick error while reading joystick device : EOF\n");
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
85 return MP_INPUT_DEAD;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
86 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
87 l += r;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
88 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
89
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
90 if((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
91 if(l > 0)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
92 printf("Joystick : we loose %d bytes of data\n",l);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
93 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
94 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
95
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
96 if(ev.type & JS_EVENT_INIT) {
4524
01a0cf736e0d Fix the bugs the previous version should fix (and those introduced
albeu
parents: 4518
diff changeset
97 printf("Joystick : warning init event, we have lost sync with driver\n");
01a0cf736e0d Fix the bugs the previous version should fix (and those introduced
albeu
parents: 4518
diff changeset
98 return mp_input_joystick_read(fd);
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
99 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
100
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
101 if(ev.type & JS_EVENT_BUTTON) {
4524
01a0cf736e0d Fix the bugs the previous version should fix (and those introduced
albeu
parents: 4518
diff changeset
102 if(ev.value == 1)
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
103 return JOY_BTN0+ev.number;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
104 } else if(ev.type & JS_EVENT_AXIS) {
4524
01a0cf736e0d Fix the bugs the previous version should fix (and those introduced
albeu
parents: 4518
diff changeset
105 if(-ev.value > JOY_AXIS_DELTA)
4518
83128eed25f1 Corrected the quit bug and added support for up to 10 axis
albeu
parents: 4418
diff changeset
106 return JOY_AXIS0_MINUS+(2*ev.number);
4524
01a0cf736e0d Fix the bugs the previous version should fix (and those introduced
albeu
parents: 4518
diff changeset
107 else if(ev.value > JOY_AXIS_DELTA)
4518
83128eed25f1 Corrected the quit bug and added support for up to 10 axis
albeu
parents: 4418
diff changeset
108 return JOY_AXIS0_PLUS+(2*ev.number);
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
109 } else {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
110 printf("Joystick warning unknow event type %d\n",ev.type);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
111 return MP_INPUT_ERROR;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
112 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
113
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
114 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
115 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
116
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
117 #else
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
118
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
119 // dummy function
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
120
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
121 int mp_input_joystick_init(char* dev) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
122 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
123 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
124
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
125 int mp_input_joystick_read(int fd) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
126
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
127 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
128 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
129
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
130 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
131
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
132 #endif