annotate input/joystick.c @ 27574:f51671d8bdc3

Make -heartbeat-cmd and -stop-xscreensaver sections reference each other.
author reimar
date Sun, 14 Sep 2008 10:17:30 +0000
parents 8eff880f638c
children 142c53391eb7
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 #include "joystick.h"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
5 #include "input.h"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
6
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
7 #include <stdlib.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
8 #include <stdio.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
9 #include <string.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
10 #include <unistd.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
11 #include <sys/types.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
12 #include <sys/stat.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
13 #include <fcntl.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
14 #include <errno.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
15
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
16 #include "mp_msg.h"
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
17 #include "help_mp.h"
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
18
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
19 #ifndef JOY_AXIS_DELTA
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
20 #define JOY_AXIS_DELTA 500
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
21 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
22
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
23 #ifndef JS_DEV
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
24 #define JS_DEV "/dev/input/js0"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
25 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
26
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
27 #include <linux/joystick.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
28
18005
1aed2b2c2983 prevent overflow.. wtf?! from irc:
rfelker
parents: 16860
diff changeset
29 int axis[256];
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
30 int btns = 0;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
31
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
32 int mp_input_joystick_init(char* dev) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
33 int fd,l=0;
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 23663
diff changeset
34 int initialized = 0;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
35 struct js_event ev;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
36
20185
b6eed21e0535 slight overall verbosity reduction
diego
parents: 19801
diff changeset
37 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
38
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
39 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
40 if(fd < 0) {
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
41 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
42 return -1;
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
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 23663
diff changeset
45 while(! initialized) {
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
46 l = 0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
47 while((unsigned int)l < sizeof(struct js_event)) {
6156
f961a25a9257 10L bugfix
albeu
parents: 4589
diff changeset
48 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
49 if(r < 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
50 if(errno == EINTR)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
51 continue;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
52 else if(errno == EAGAIN) {
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 23663
diff changeset
53 initialized = 1;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
54 break;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
55 }
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
56 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
57 close(fd);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
58 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
59 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
60 l += r;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
61 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
62 if((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
63 if(l > 0)
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
64 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
65 break;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
66 }
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
67 if(ev.type == JS_EVENT_BUTTON)
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
68 btns |= (ev.value << ev.number);
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
69 if(ev.type == JS_EVENT_AXIS)
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
70 axis[ev.number] = ev.value;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
71 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
72
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
73 return fd;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
74 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
75
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
76 int mp_input_joystick_read(int fd) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
77 struct js_event ev;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
78 int l=0;
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 while((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
81 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
82 if(r <= 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
83 if(errno == EINTR)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
84 continue;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
85 else if(errno == EAGAIN)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
86 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
87 if( r < 0)
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
88 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
89 else
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
90 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
91 return MP_INPUT_DEAD;
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 l += r;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
94 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
95
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
96 if((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
97 if(l > 0)
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
98 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
99 return MP_INPUT_NOTHING;
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
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
102 if(ev.type & JS_EVENT_INIT) {
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
103 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
104 ev.type &= ~JS_EVENT_INIT;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
105 if(ev.type == JS_EVENT_BUTTON) {
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
106 int s = (btns >> ev.number) & 1;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
107 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
108 return MP_INPUT_NOTHING;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
109 }
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
110 if(ev.type == JS_EVENT_AXIS) {
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
111 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
112 (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
113 (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
114 ) // State is the same : ignore
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
115 return MP_INPUT_NOTHING;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
116 }
4418
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_BUTTON) {
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
120 btns &= ~(1 << ev.number);
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
121 btns |= (ev.value << ev.number);
4524
01a0cf736e0d Fix the bugs the previous version should fix (and those introduced
albeu
parents: 4518
diff changeset
122 if(ev.value == 1)
26759
8eff880f638c cosmetics: Remove useless parentheses from return statements.
diego
parents: 26080
diff changeset
123 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
124 else
26759
8eff880f638c cosmetics: Remove useless parentheses from return statements.
diego
parents: 26080
diff changeset
125 return JOY_BTN0 + ev.number;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
126 } else if(ev.type & JS_EVENT_AXIS) {
4589
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
127 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
128 axis[ev.number] = -1;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
129 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
130 } 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
131 axis[ev.number] = 1;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
132 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
133 } 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
134 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
135 axis[ev.number] = 0;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
136 return r;
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
137 } else
4ce20c55a18a Added support for key combination and mouse buttons key code
albeu
parents: 4524
diff changeset
138 return MP_INPUT_NOTHING;
4418
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
139 } else {
16855
5f83daf94eb2 printf to mp_msg
reynaldo
parents: 10397
diff changeset
140 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
141 return MP_INPUT_ERROR;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
142 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
143
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
144 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
145 }