annotate input/joystick.c @ 18053:d0f651424409

French fixes, by Bounec
author gpoirier
date Fri, 07 Apr 2006 21:47:38 +0000
parents 1aed2b2c2983
children 133f4d2f84c5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
1
16860
a68ede010f66 Unify include paths, -I.. is in CFLAGS.
diego
parents: 16855
diff changeset
2 #include "config.h"
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
3
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
4 #ifdef HAVE_JOYSTICK
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
5
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
6 #include "joystick.h"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
7 #include "input.h"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
8
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
9 #include <stdlib.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
10 #include <stdio.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
11 #include <string.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
12 #include <unistd.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
13 #include <sys/types.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
14 #include <sys/stat.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
15 #include <fcntl.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
16 #include <errno.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
17
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
18 #include "mp_msg.h"
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
19 #include "help_mp.h"
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
20
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
21 #ifndef JOY_AXIS_DELTA
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
22 #define JOY_AXIS_DELTA 500
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
23 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
24
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
25 #ifndef JS_DEV
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
26 #define JS_DEV "/dev/input/js0"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
27 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
28
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
29 #ifdef TARGET_LINUX
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
30
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
31 #include <linux/joystick.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
32
18005
1aed2b2c2983 prevent overflow.. wtf?! from irc:
rfelker
parents: 16860
diff changeset
33 int axis[256];
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
34 int btns = 0;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
35
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
36 int mp_input_joystick_init(char* dev) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
37 int fd,l=0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
38 int inited = 0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
39 struct js_event ev;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
40
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
41 mp_msg(MSGT_INPUT,MSGL_INFO,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
42
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
43 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
44 if(fd < 0) {
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
45 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
46 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
47 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
48
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
49 while(! inited) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
50 l = 0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
51 while((unsigned int)l < sizeof(struct js_event)) {
6156
f961a25a9257 10L bugfix
albeu
parents: 4589
diff changeset
52 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
53 if(r < 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
54 if(errno == EINTR)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
55 continue;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
56 else if(errno == EAGAIN) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
57 inited = 1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
58 break;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
59 }
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
60 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
61 close(fd);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
62 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
63 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
64 l += r;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
65 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
66 if((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
67 if(l > 0)
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
68 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
69 break;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
70 }
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
71 ev.type &= ~JS_EVENT_INIT;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
72 if(ev.type == JS_EVENT_BUTTON)
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
73 btns |= (ev.value << ev.number);
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
74 if(ev.type == JS_EVENT_AXIS)
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
75 axis[ev.number] = ev.value;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
76 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
77
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
78 return fd;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
79 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
80
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
81 int mp_input_joystick_read(int fd) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
82 struct js_event ev;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
83 int l=0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
84
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
85 while((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
86 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
87 if(r <= 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
88 if(errno == EINTR)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
89 continue;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
90 else if(errno == EAGAIN)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
91 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
92 if( r < 0)
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
93 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
94 else
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
95 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
96 return MP_INPUT_DEAD;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
97 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
98 l += r;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
99 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
100
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
101 if((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
102 if(l > 0)
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
103 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
104 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
105 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
106
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
107 if(ev.type & JS_EVENT_INIT) {
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
108 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
109 ev.type &= ~JS_EVENT_INIT;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
110 if(ev.type == JS_EVENT_BUTTON) {
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
111 int s = (btns >> ev.number) & 1;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
112 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
113 return MP_INPUT_NOTHING;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
114 }
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
115 if(ev.type == JS_EVENT_AXIS) {
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
116 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
117 (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
118 (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
119 ) // State is the same : ignore
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
120 return MP_INPUT_NOTHING;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
121 }
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
122 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
123
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
124 if(ev.type & JS_EVENT_BUTTON) {
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
125 btns &= ~(1 << ev.number);
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
126 btns |= (ev.value << ev.number);
4524
01a0cf736e0d Fix the bugs the previous version should fix (and those introduced
albeu
parents: 4518
diff changeset
127 if(ev.value == 1)
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
128 return ((JOY_BTN0+ev.number) | MP_KEY_DOWN);
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
129 else
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
130 return (JOY_BTN0+ev.number);
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
131 } else if(ev.type & JS_EVENT_AXIS) {
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
132 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
133 axis[ev.number] = -1;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
134 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
135 } 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
136 axis[ev.number] = 1;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
137 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
138 } 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
139 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
140 axis[ev.number] = 0;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
141 return r;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
142 } else
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
143 return MP_INPUT_NOTHING;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
144 } else {
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
145 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
146 return MP_INPUT_ERROR;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
147 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
148
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
149 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
150 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
151
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
152 #else
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
153
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
154 // dummy function
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
155
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
156 int mp_input_joystick_init(char* dev) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
157 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
158 }
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 int mp_input_joystick_read(int fd) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
161
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
162 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
163 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
164
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
165 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
166
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
167 #endif