rpm: Remove MEncoder from rpm packaging
[mplayer/glamo.git] / input / joystick.c
blob40a1b22b7fb98c024f9e962e9e63c91c80078664
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"
35 #ifndef JOY_AXIS_DELTA
36 #define JOY_AXIS_DELTA 500
37 #endif
39 #ifndef JS_DEV
40 #define JS_DEV "/dev/input/js0"
41 #endif
43 #include <linux/joystick.h>
45 int axis[256];
46 int btns = 0;
48 int mp_input_joystick_init(char* dev) {
49 int fd,l=0;
50 int initialized = 0;
51 struct js_event ev;
53 mp_tmsg(MSGT_INPUT,MSGL_V,"Opening joystick device %s\n",dev ? dev : JS_DEV);
55 fd = open( dev ? dev : JS_DEV , O_RDONLY | O_NONBLOCK );
56 if(fd < 0) {
57 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't open joystick device %s: %s\n",dev ? dev : JS_DEV,strerror(errno));
58 return -1;
61 while(! initialized) {
62 l = 0;
63 while((unsigned int)l < sizeof(struct js_event)) {
64 int r = read(fd,((char*)&ev)+l,sizeof(struct js_event)-l);
65 if(r < 0) {
66 if(errno == EINTR)
67 continue;
68 else if(errno == EAGAIN) {
69 initialized = 1;
70 break;
72 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading joystick device: %s\n",strerror(errno));
73 close(fd);
74 return -1;
76 l += r;
78 if((unsigned int)l < sizeof(struct js_event)) {
79 if(l > 0)
80 mp_tmsg(MSGT_INPUT,MSGL_WARN,"Joystick: We lose %d bytes of data\n",l);
81 break;
83 if(ev.type == JS_EVENT_BUTTON)
84 btns |= (ev.value << ev.number);
85 if(ev.type == JS_EVENT_AXIS)
86 axis[ev.number] = ev.value;
89 return fd;
92 int mp_input_joystick_read(void *ctx, int fd) {
93 struct js_event ev;
94 int l=0;
96 while((unsigned int)l < sizeof(struct js_event)) {
97 int r = read(fd,&ev+l,sizeof(struct js_event)-l);
98 if(r <= 0) {
99 if(errno == EINTR)
100 continue;
101 else if(errno == EAGAIN)
102 return MP_INPUT_NOTHING;
103 if( r < 0)
104 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading joystick device: %s\n",strerror(errno));
105 else
106 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading joystick device: %s\n","EOF");
107 return MP_INPUT_DEAD;
109 l += r;
112 if((unsigned int)l < sizeof(struct js_event)) {
113 if(l > 0)
114 mp_tmsg(MSGT_INPUT,MSGL_WARN,"Joystick: We lose %d bytes of data\n",l);
115 return MP_INPUT_NOTHING;
118 if(ev.type & JS_EVENT_INIT) {
119 mp_tmsg(MSGT_INPUT,MSGL_WARN,"Joystick: warning init event, we have lost sync with driver.\n");
120 ev.type &= ~JS_EVENT_INIT;
121 if(ev.type == JS_EVENT_BUTTON) {
122 int s = (btns >> ev.number) & 1;
123 if(s == ev.value) // State is the same : ignore
124 return MP_INPUT_NOTHING;
126 if(ev.type == JS_EVENT_AXIS) {
127 if( ( axis[ev.number] == 1 && ev.value > JOY_AXIS_DELTA) ||
128 (axis[ev.number] == -1 && ev.value < -JOY_AXIS_DELTA) ||
129 (axis[ev.number] == 0 && ev.value >= -JOY_AXIS_DELTA && ev.value <= JOY_AXIS_DELTA)
130 ) // State is the same : ignore
131 return MP_INPUT_NOTHING;
135 if(ev.type & JS_EVENT_BUTTON) {
136 btns &= ~(1 << ev.number);
137 btns |= (ev.value << ev.number);
138 if(ev.value == 1)
139 return (JOY_BTN0 + ev.number) | MP_KEY_DOWN;
140 else
141 return JOY_BTN0 + ev.number;
142 } else if(ev.type & JS_EVENT_AXIS) {
143 if(ev.value < -JOY_AXIS_DELTA && axis[ev.number] != -1) {
144 axis[ev.number] = -1;
145 return (JOY_AXIS0_MINUS+(2*ev.number)) | MP_KEY_DOWN;
146 } else if(ev.value > JOY_AXIS_DELTA && axis[ev.number] != 1) {
147 axis[ev.number] = 1;
148 return (JOY_AXIS0_PLUS+(2*ev.number)) | MP_KEY_DOWN;
149 } else if(ev.value <= JOY_AXIS_DELTA && ev.value >= -JOY_AXIS_DELTA && axis[ev.number] != 0) {
150 int r = axis[ev.number] == 1 ? JOY_AXIS0_PLUS+(2*ev.number) : JOY_AXIS0_MINUS+(2*ev.number);
151 axis[ev.number] = 0;
152 return r;
153 } else
154 return MP_INPUT_NOTHING;
155 } else {
156 mp_tmsg(MSGT_INPUT,MSGL_WARN,"Joystick warning unknown event type %d\n",ev.type);
157 return MP_INPUT_ERROR;
160 return MP_INPUT_NOTHING;