11 #include <sys/types.h>
19 #ifndef JOY_AXIS_DELTA
20 #define JOY_AXIS_DELTA 500
24 #define JS_DEV "/dev/input/js0"
29 #include <linux/joystick.h>
34 int mp_input_joystick_init(char* dev
) {
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
);
43 mp_msg(MSGT_INPUT
,MSGL_ERR
,MSGTR_INPUT_JOYSTICK_CantOpen
,dev
? dev
: JS_DEV
,strerror(errno
));
49 while((unsigned int)l
< sizeof(struct js_event
)) {
50 int r
= read(fd
,((char*)&ev
)+l
,sizeof(struct js_event
)-l
);
54 else if(errno
== EAGAIN
) {
58 mp_msg(MSGT_INPUT
,MSGL_ERR
,MSGTR_INPUT_JOYSTICK_ErrReading
,strerror(errno
));
64 if((unsigned int)l
< sizeof(struct js_event
)) {
66 mp_msg(MSGT_INPUT
,MSGL_WARN
,MSGTR_INPUT_JOYSTICK_LoosingBytes
,l
);
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
;
78 int mp_input_joystick_read(int fd
) {
82 while((unsigned int)l
< sizeof(struct js_event
)) {
83 int r
= read(fd
,&ev
+l
,sizeof(struct js_event
)-l
);
87 else if(errno
== EAGAIN
)
88 return MP_INPUT_NOTHING
;
90 mp_msg(MSGT_INPUT
,MSGL_ERR
,MSGTR_INPUT_JOYSTICK_ErrReading
,strerror(errno
));
92 mp_msg(MSGT_INPUT
,MSGL_ERR
,MSGTR_INPUT_JOYSTICK_ErrReading
,"EOF");
98 if((unsigned int)l
< sizeof(struct js_event
)) {
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
);
125 return ((JOY_BTN0
+ev
.number
) | MP_KEY_DOWN
);
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) {
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
);
140 return MP_INPUT_NOTHING
;
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 */
153 int mp_input_joystick_init(char* dev
) {
157 int mp_input_joystick_read(int fd
) {
159 return MP_INPUT_NOTHING
;
162 #endif /* TARGET_LINUX */