Allocate struct demuxer with talloc
[mplayer.git] / input / joystick.c
blobb49214c95c51bf819d11865c964e1b0cfc47e2b4
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include "joystick.h"
22 #include "input.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <errno.h>
33 #include "mp_msg.h"
34 #include "help_mp.h"
36 #ifndef JOY_AXIS_DELTA
37 #define JOY_AXIS_DELTA 500
38 #endif
40 #ifndef JS_DEV
41 #define JS_DEV "/dev/input/js0"
42 #endif
44 #include <linux/joystick.h>
46 int axis[256];
47 int btns = 0;
49 int mp_input_joystick_init(char* dev) {
50 int fd,l=0;
51 int initialized = 0;
52 struct js_event ev;
54 mp_msg(MSGT_INPUT,MSGL_V,MSGTR_INPUT_JOYSTICK_Opening,dev ? dev : JS_DEV);
56 fd = open( dev ? dev : JS_DEV , O_RDONLY | O_NONBLOCK );
57 if(fd < 0) {
58 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_CantOpen,dev ? dev : JS_DEV,strerror(errno));
59 return -1;
62 while(! initialized) {
63 l = 0;
64 while((unsigned int)l < sizeof(struct js_event)) {
65 int r = read(fd,((char*)&ev)+l,sizeof(struct js_event)-l);
66 if(r < 0) {
67 if(errno == EINTR)
68 continue;
69 else if(errno == EAGAIN) {
70 initialized = 1;
71 break;
73 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_ErrReading,strerror(errno));
74 close(fd);
75 return -1;
77 l += r;
79 if((unsigned int)l < sizeof(struct js_event)) {
80 if(l > 0)
81 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_LoosingBytes,l);
82 break;
84 if(ev.type == JS_EVENT_BUTTON)
85 btns |= (ev.value << ev.number);
86 if(ev.type == JS_EVENT_AXIS)
87 axis[ev.number] = ev.value;
90 return fd;
93 int mp_input_joystick_read(void *ctx, int fd) {
94 struct js_event ev;
95 int l=0;
97 while((unsigned int)l < sizeof(struct js_event)) {
98 int r = read(fd,&ev+l,sizeof(struct js_event)-l);
99 if(r <= 0) {
100 if(errno == EINTR)
101 continue;
102 else if(errno == EAGAIN)
103 return MP_INPUT_NOTHING;
104 if( r < 0)
105 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_ErrReading,strerror(errno));
106 else
107 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_ErrReading,"EOF");
108 return MP_INPUT_DEAD;
110 l += r;
113 if((unsigned int)l < sizeof(struct js_event)) {
114 if(l > 0)
115 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_LoosingBytes,l);
116 return MP_INPUT_NOTHING;
119 if(ev.type & JS_EVENT_INIT) {
120 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_WarnLostSync);
121 ev.type &= ~JS_EVENT_INIT;
122 if(ev.type == JS_EVENT_BUTTON) {
123 int s = (btns >> ev.number) & 1;
124 if(s == ev.value) // State is the same : ignore
125 return MP_INPUT_NOTHING;
127 if(ev.type == JS_EVENT_AXIS) {
128 if( ( axis[ev.number] == 1 && ev.value > JOY_AXIS_DELTA) ||
129 (axis[ev.number] == -1 && ev.value < -JOY_AXIS_DELTA) ||
130 (axis[ev.number] == 0 && ev.value >= -JOY_AXIS_DELTA && ev.value <= JOY_AXIS_DELTA)
131 ) // State is the same : ignore
132 return MP_INPUT_NOTHING;
136 if(ev.type & JS_EVENT_BUTTON) {
137 btns &= ~(1 << ev.number);
138 btns |= (ev.value << ev.number);
139 if(ev.value == 1)
140 return (JOY_BTN0 + ev.number) | MP_KEY_DOWN;
141 else
142 return JOY_BTN0 + ev.number;
143 } else if(ev.type & JS_EVENT_AXIS) {
144 if(ev.value < -JOY_AXIS_DELTA && axis[ev.number] != -1) {
145 axis[ev.number] = -1;
146 return (JOY_AXIS0_MINUS+(2*ev.number)) | MP_KEY_DOWN;
147 } else if(ev.value > JOY_AXIS_DELTA && axis[ev.number] != 1) {
148 axis[ev.number] = 1;
149 return (JOY_AXIS0_PLUS+(2*ev.number)) | MP_KEY_DOWN;
150 } else if(ev.value <= JOY_AXIS_DELTA && ev.value >= -JOY_AXIS_DELTA && axis[ev.number] != 0) {
151 int r = axis[ev.number] == 1 ? JOY_AXIS0_PLUS+(2*ev.number) : JOY_AXIS0_MINUS+(2*ev.number);
152 axis[ev.number] = 0;
153 return r;
154 } else
155 return MP_INPUT_NOTHING;
156 } else {
157 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_WarnUnknownEvent,ev.type);
158 return MP_INPUT_ERROR;
161 return MP_INPUT_NOTHING;