Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / input / joystick.c
blob312cd90f4dca0e82369457ed204768b4bf527851
2 #include "config.h"
4 #include "joystick.h"
5 #include "input.h"
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <errno.h>
16 #include "mp_msg.h"
17 #include "help_mp.h"
19 #ifndef JOY_AXIS_DELTA
20 #define JOY_AXIS_DELTA 500
21 #endif
23 #ifndef JS_DEV
24 #define JS_DEV "/dev/input/js0"
25 #endif
27 #ifdef TARGET_LINUX
29 #include <linux/joystick.h>
31 int axis[256];
32 int btns = 0;
34 int mp_input_joystick_init(char* dev) {
35 int fd,l=0;
36 int inited = 0;
37 struct js_event ev;
39 mp_msg(MSGT_INPUT,MSGL_V,MSGTR_INPUT_JOYSTICK_Opening,dev ? dev : JS_DEV);
41 fd = open( dev ? dev : JS_DEV , O_RDONLY | O_NONBLOCK );
42 if(fd < 0) {
43 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_CantOpen,dev ? dev : JS_DEV,strerror(errno));
44 return -1;
47 while(! inited) {
48 l = 0;
49 while((unsigned int)l < sizeof(struct js_event)) {
50 int r = read(fd,((char*)&ev)+l,sizeof(struct js_event)-l);
51 if(r < 0) {
52 if(errno == EINTR)
53 continue;
54 else if(errno == EAGAIN) {
55 inited = 1;
56 break;
58 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_ErrReading,strerror(errno));
59 close(fd);
60 return -1;
62 l += r;
64 if((unsigned int)l < sizeof(struct js_event)) {
65 if(l > 0)
66 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_LoosingBytes,l);
67 break;
69 if(ev.type == JS_EVENT_BUTTON)
70 btns |= (ev.value << ev.number);
71 if(ev.type == JS_EVENT_AXIS)
72 axis[ev.number] = ev.value;
75 return fd;
78 int mp_input_joystick_read(int fd) {
79 struct js_event ev;
80 int l=0;
82 while((unsigned int)l < sizeof(struct js_event)) {
83 int r = read(fd,&ev+l,sizeof(struct js_event)-l);
84 if(r <= 0) {
85 if(errno == EINTR)
86 continue;
87 else if(errno == EAGAIN)
88 return MP_INPUT_NOTHING;
89 if( r < 0)
90 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_ErrReading,strerror(errno));
91 else
92 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_ErrReading,"EOF");
93 return MP_INPUT_DEAD;
95 l += r;
98 if((unsigned int)l < sizeof(struct js_event)) {
99 if(l > 0)
100 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_LoosingBytes,l);
101 return MP_INPUT_NOTHING;
104 if(ev.type & JS_EVENT_INIT) {
105 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_WarnLostSync);
106 ev.type &= ~JS_EVENT_INIT;
107 if(ev.type == JS_EVENT_BUTTON) {
108 int s = (btns >> ev.number) & 1;
109 if(s == ev.value) // State is the same : ignore
110 return MP_INPUT_NOTHING;
112 if(ev.type == JS_EVENT_AXIS) {
113 if( ( axis[ev.number] == 1 && ev.value > JOY_AXIS_DELTA) ||
114 (axis[ev.number] == -1 && ev.value < -JOY_AXIS_DELTA) ||
115 (axis[ev.number] == 0 && ev.value >= -JOY_AXIS_DELTA && ev.value <= JOY_AXIS_DELTA)
116 ) // State is the same : ignore
117 return MP_INPUT_NOTHING;
121 if(ev.type & JS_EVENT_BUTTON) {
122 btns &= ~(1 << ev.number);
123 btns |= (ev.value << ev.number);
124 if(ev.value == 1)
125 return ((JOY_BTN0+ev.number) | MP_KEY_DOWN);
126 else
127 return (JOY_BTN0+ev.number);
128 } else if(ev.type & JS_EVENT_AXIS) {
129 if(ev.value < -JOY_AXIS_DELTA && axis[ev.number] != -1) {
130 axis[ev.number] = -1;
131 return (JOY_AXIS0_MINUS+(2*ev.number)) | MP_KEY_DOWN;
132 } else if(ev.value > JOY_AXIS_DELTA && axis[ev.number] != 1) {
133 axis[ev.number] = 1;
134 return (JOY_AXIS0_PLUS+(2*ev.number)) | MP_KEY_DOWN;
135 } else if(ev.value <= JOY_AXIS_DELTA && ev.value >= -JOY_AXIS_DELTA && axis[ev.number] != 0) {
136 int r = axis[ev.number] == 1 ? JOY_AXIS0_PLUS+(2*ev.number) : JOY_AXIS0_MINUS+(2*ev.number);
137 axis[ev.number] = 0;
138 return r;
139 } else
140 return MP_INPUT_NOTHING;
141 } else {
142 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_WarnUnknownEvent,ev.type);
143 return MP_INPUT_ERROR;
146 return MP_INPUT_NOTHING;
149 #else /* TARGET_LINUX */
151 // dummy function
153 int mp_input_joystick_init(char* dev) {
154 return -1;
157 int mp_input_joystick_read(int fd) {
159 return MP_INPUT_NOTHING;
162 #endif /* TARGET_LINUX */