annotate input/joystick.c @ 31508:effd6503b84c

reindent after last commit
author siretart
date Sun, 27 Jun 2010 19:56:49 +0000
parents 0f1b5b68af32
children 277ec491a8a7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28112
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
1 /*
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
2 * This file is part of MPlayer.
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
3 *
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
5 * it under the terms of the GNU General Public License as published by
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
7 * (at your option) any later version.
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
8 *
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
12 * GNU General Public License for more details.
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
13 *
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
14 * You should have received a copy of the GNU General Public License along
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
142c53391eb7 Add standard GPL license header.
diego
parents: 26759
diff changeset
17 */
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
18
16860
a68ede010f66 Unify include paths, -I.. is in CFLAGS.
diego
parents: 16855
diff changeset
19 #include "config.h"
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
20
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
21 #include "joystick.h"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
22 #include "input.h"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
23
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
24 #include <stdlib.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
25 #include <stdio.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
26 #include <string.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
27 #include <unistd.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
28 #include <sys/types.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
29 #include <sys/stat.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
30 #include <fcntl.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
31 #include <errno.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
32
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
33 #include "mp_msg.h"
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
34 #include "help_mp.h"
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
35
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
36 #ifndef JOY_AXIS_DELTA
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
37 #define JOY_AXIS_DELTA 500
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
38 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
39
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
40 #ifndef JS_DEV
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
41 #define JS_DEV "/dev/input/js0"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
42 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
43
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
44 #include <linux/joystick.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
45
18005
1aed2b2c2983 prevent overflow.. wtf?! from irc:
rfelker
parents: 16860
diff changeset
46 int axis[256];
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
47 int btns = 0;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
48
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
49 int mp_input_joystick_init(char* dev) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
50 int fd,l=0;
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 23663
diff changeset
51 int initialized = 0;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
52 struct js_event ev;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28112
diff changeset
53
20185
b6eed21e0535 slight overall verbosity reduction
diego
parents: 19801
diff changeset
54 mp_msg(MSGT_INPUT,MSGL_V,MSGTR_INPUT_JOYSTICK_Opening,dev ? dev : JS_DEV);
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
55
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
56 fd = open( dev ? dev : JS_DEV , O_RDONLY | O_NONBLOCK );
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
57 if(fd < 0) {
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
58 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_CantOpen,dev ? dev : JS_DEV,strerror(errno));
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
59 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
60 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28112
diff changeset
61
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 23663
diff changeset
62 while(! initialized) {
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
63 l = 0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
64 while((unsigned int)l < sizeof(struct js_event)) {
6156
f961a25a9257 10L bugfix
albeu
parents: 4589
diff changeset
65 int r = read(fd,((char*)&ev)+l,sizeof(struct js_event)-l);
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
66 if(r < 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
67 if(errno == EINTR)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
68 continue;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
69 else if(errno == EAGAIN) {
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 23663
diff changeset
70 initialized = 1;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
71 break;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
72 }
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
73 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_ErrReading,strerror(errno));
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
74 close(fd);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
75 return -1;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28112
diff changeset
76 }
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
77 l += r;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
78 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
79 if((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
80 if(l > 0)
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28112
diff changeset
81 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_LoosingBytes,l);
4524
01a0cf736e0d Fix the bugs the previous version should fix (and those introduced
albeu
parents: 4518
diff changeset
82 break;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
83 }
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
84 if(ev.type == JS_EVENT_BUTTON)
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
85 btns |= (ev.value << ev.number);
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
86 if(ev.type == JS_EVENT_AXIS)
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
87 axis[ev.number] = ev.value;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
88 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28112
diff changeset
89
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
90 return fd;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
91 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
92
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
93 int mp_input_joystick_read(int fd) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
94 struct js_event ev;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
95 int l=0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
96
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
97 while((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
98 int r = read(fd,&ev+l,sizeof(struct js_event)-l);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
99 if(r <= 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
100 if(errno == EINTR)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
101 continue;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
102 else if(errno == EAGAIN)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
103 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
104 if( r < 0)
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
105 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_ErrReading,strerror(errno));
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
106 else
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28112
diff changeset
107 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_JOYSTICK_ErrReading,"EOF");
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
108 return MP_INPUT_DEAD;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28112
diff changeset
109 }
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
110 l += r;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
111 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
112
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
113 if((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
114 if(l > 0)
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
115 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_LoosingBytes,l);
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
116 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
117 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
118
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
119 if(ev.type & JS_EVENT_INIT) {
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28112
diff changeset
120 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_WarnLostSync);
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
121 ev.type &= ~JS_EVENT_INIT;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
122 if(ev.type == JS_EVENT_BUTTON) {
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
123 int s = (btns >> ev.number) & 1;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
124 if(s == ev.value) // State is the same : ignore
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
125 return MP_INPUT_NOTHING;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
126 }
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
127 if(ev.type == JS_EVENT_AXIS) {
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
128 if( ( axis[ev.number] == 1 && ev.value > JOY_AXIS_DELTA) ||
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
129 (axis[ev.number] == -1 && ev.value < -JOY_AXIS_DELTA) ||
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
130 (axis[ev.number] == 0 && ev.value >= -JOY_AXIS_DELTA && ev.value <= JOY_AXIS_DELTA)
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
131 ) // State is the same : ignore
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
132 return MP_INPUT_NOTHING;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28112
diff changeset
133 }
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
134 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28112
diff changeset
135
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
136 if(ev.type & JS_EVENT_BUTTON) {
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
137 btns &= ~(1 << ev.number);
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
138 btns |= (ev.value << ev.number);
4524
01a0cf736e0d Fix the bugs the previous version should fix (and those introduced
albeu
parents: 4518
diff changeset
139 if(ev.value == 1)
26759
8eff880f638c cosmetics: Remove useless parentheses from return statements.
diego
parents: 26080
diff changeset
140 return (JOY_BTN0 + ev.number) | MP_KEY_DOWN;
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
141 else
26759
8eff880f638c cosmetics: Remove useless parentheses from return statements.
diego
parents: 26080
diff changeset
142 return JOY_BTN0 + ev.number;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
143 } else if(ev.type & JS_EVENT_AXIS) {
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
144 if(ev.value < -JOY_AXIS_DELTA && axis[ev.number] != -1) {
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
145 axis[ev.number] = -1;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
146 return (JOY_AXIS0_MINUS+(2*ev.number)) | MP_KEY_DOWN;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
147 } else if(ev.value > JOY_AXIS_DELTA && axis[ev.number] != 1) {
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
148 axis[ev.number] = 1;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
149 return (JOY_AXIS0_PLUS+(2*ev.number)) | MP_KEY_DOWN;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
150 } else if(ev.value <= JOY_AXIS_DELTA && ev.value >= -JOY_AXIS_DELTA && axis[ev.number] != 0) {
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
151 int r = axis[ev.number] == 1 ? JOY_AXIS0_PLUS+(2*ev.number) : JOY_AXIS0_MINUS+(2*ev.number);
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
152 axis[ev.number] = 0;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
153 return r;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
154 } else
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
155 return MP_INPUT_NOTHING;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
156 } else {
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28112
diff changeset
157 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_INPUT_JOYSTICK_WarnUnknownEvent,ev.type);
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
158 return MP_INPUT_ERROR;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
159 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
160
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
161 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
162 }