synced with r22428
[mplayer/greg.git] / input / joystick.c
blobc432bf7cf4297585b576909892a216e5a505eee3
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 ev.type &= ~JS_EVENT_INIT;
70 if(ev.type == JS_EVENT_BUTTON)
71 btns |= (ev.value << ev.number);
72 if(ev.type == JS_EVENT_AXIS)
73 axis[ev.number] = ev.value;
76 return fd;
79 int mp_input_joystick_read(int fd) {
80 struct js_event ev;
81 int l=0;
83 while((unsigned int)l < sizeof(struct js_event)) {
84 int r = read(fd,&ev+l,sizeof(struct js_event)-l);
85 if(r <= 0) {
86 if(errno == EINTR)
87 continue;
88 else if(errno == EAGAIN)
89 return MP_INPUT_NOTHING;
90 if( r < 0)
91 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_ErrReading,strerror(errno));
92 else
93 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_ErrReading,"EOF");
94 return MP_INPUT_DEAD;
96 l += r;
99 if((unsigned int)l < sizeof(struct js_event)) {
100 if(l > 0)
101 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_LoosingBytes,l);
102 return MP_INPUT_NOTHING;
105 if(ev.type & JS_EVENT_INIT) {
106 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_WarnLostSync);
107 ev.type &= ~JS_EVENT_INIT;
108 if(ev.type == JS_EVENT_BUTTON) {
109 int s = (btns >> ev.number) & 1;
110 if(s == ev.value) // State is the same : ignore
111 return MP_INPUT_NOTHING;
113 if(ev.type == JS_EVENT_AXIS) {
114 if( ( axis[ev.number] == 1 && ev.value > JOY_AXIS_DELTA) ||
115 (axis[ev.number] == -1 && ev.value < -JOY_AXIS_DELTA) ||
116 (axis[ev.number] == 0 && ev.value >= -JOY_AXIS_DELTA && ev.value <= JOY_AXIS_DELTA)
117 ) // State is the same : ignore
118 return MP_INPUT_NOTHING;
122 if(ev.type & JS_EVENT_BUTTON) {
123 btns &= ~(1 << ev.number);
124 btns |= (ev.value << ev.number);
125 if(ev.value == 1)
126 return ((JOY_BTN0+ev.number) | MP_KEY_DOWN);
127 else
128 return (JOY_BTN0+ev.number);
129 } else if(ev.type & JS_EVENT_AXIS) {
130 if(ev.value < -JOY_AXIS_DELTA && axis[ev.number] != -1) {
131 axis[ev.number] = -1;
132 return (JOY_AXIS0_MINUS+(2*ev.number)) | MP_KEY_DOWN;
133 } else if(ev.value > JOY_AXIS_DELTA && axis[ev.number] != 1) {
134 axis[ev.number] = 1;
135 return (JOY_AXIS0_PLUS+(2*ev.number)) | MP_KEY_DOWN;
136 } else if(ev.value <= JOY_AXIS_DELTA && ev.value >= -JOY_AXIS_DELTA && axis[ev.number] != 0) {
137 int r = axis[ev.number] == 1 ? JOY_AXIS0_PLUS+(2*ev.number) : JOY_AXIS0_MINUS+(2*ev.number);
138 axis[ev.number] = 0;
139 return r;
140 } else
141 return MP_INPUT_NOTHING;
142 } else {
143 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_WarnUnknownEvent,ev.type);
144 return MP_INPUT_ERROR;
147 return MP_INPUT_NOTHING;
150 #else
152 // dummy function
154 int mp_input_joystick_init(char* dev) {
155 return -1;
158 int mp_input_joystick_read(int fd) {
160 return MP_INPUT_NOTHING;
163 #endif