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 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
;
79 int mp_input_joystick_read(int fd
) {
83 while((unsigned int)l
< sizeof(struct js_event
)) {
84 int r
= read(fd
,&ev
+l
,sizeof(struct js_event
)-l
);
88 else if(errno
== EAGAIN
)
89 return MP_INPUT_NOTHING
;
91 mp_msg(MSGT_INPUT
,MSGL_ERR
,MSGTR_INPUT_JOYSTICK_ErrReading
,strerror(errno
));
93 mp_msg(MSGT_INPUT
,MSGL_ERR
,MSGTR_INPUT_JOYSTICK_ErrReading
,"EOF");
99 if((unsigned int)l
< sizeof(struct js_event
)) {
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
);
126 return ((JOY_BTN0
+ev
.number
) | MP_KEY_DOWN
);
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) {
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
);
141 return MP_INPUT_NOTHING
;
143 mp_msg(MSGT_INPUT
,MSGL_WARN
,MSGTR_INPUT_JOYSTICK_WarnUnknownEvent
,ev
.type
);
144 return MP_INPUT_ERROR
;
147 return MP_INPUT_NOTHING
;
154 int mp_input_joystick_init(char* dev
) {
158 int mp_input_joystick_read(int fd
) {
160 return MP_INPUT_NOTHING
;