annotate input/joystick.c @ 4457:49dcbd03436d

Optimize DirectShow decoding with vidix
author nick
date Fri, 01 Feb 2002 10:01:56 +0000
parents 8141d2c399e4
children 83128eed25f1
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
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
2 #include "../config.h"
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
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
18 #ifndef JOY_AXIS_DELTA
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
19 #define JOY_AXIS_DELTA 500
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
20 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
21
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
22 #ifndef JS_DEV
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
23 #define JS_DEV "/dev/input/js0"
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
24 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
25
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
26 #ifdef TARGET_LINUX
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
27
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
28 #include <linux/joystick.h>
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
29
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
30 static int buttons = 0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
31 static int* axis;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
32
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
33 int mp_input_joystick_init(char* dev) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
34 int fd,l=0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
35 int inited = 0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
36 struct js_event ev;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
37 char n_axis;
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 printf("Opening joystick device %s\n",dev ? dev : JS_DEV);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
40
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
41 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
42 if(fd < 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
43 printf("Can't open joystick device %s : %s\n",dev ? dev : JS_DEV,strerror(errno));
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
44 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
45 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
46
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
47 if(ioctl(fd, JSIOCGAXES, &n_axis) < 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
48 printf("Joystick : can't get number of axis, %s\n",strerror(errno));
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
49 close(fd);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
50 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
51 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
52
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
53 axis = (int*)malloc(n_axis*sizeof(int));
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
54
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 while(! inited) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
57 l = 0;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
58 while((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
59 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
60 if(r < 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
61 if(errno == EINTR)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
62 continue;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
63 else if(errno == EAGAIN) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
64 inited = 1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
65 break;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
66 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
67 printf("Error while reading joystick device : %s\n",strerror(errno));
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
68 close(fd);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
69 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
70 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
71 l += r;
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 if((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
74 if(l > 0)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
75 printf("Joystick : we loose %d bytes of data\n",l);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
76 return fd;
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 if(ev.type & JS_EVENT_INIT) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
79 ev.type &= ~JS_EVENT_INIT;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
80 if(ev.type & JS_EVENT_BUTTON)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
81 buttons |= (ev.value << ev.number);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
82 else if(ev.type & JS_EVENT_AXIS)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
83 axis[ev.number] = ev.value;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
84 } else
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
85 printf("Joystick : Warning non-init event during init :-o\n");
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
86 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
87
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
88 return fd;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
89 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
90
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
91 int mp_input_joystick_read(int fd) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
92 struct js_event ev;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
93 int l=0;
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 while((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
96 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
97 if(r <= 0) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
98 if(errno == EINTR)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
99 continue;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
100 else if(errno == EAGAIN)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
101 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
102 if( r < 0)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
103 printf("Joystick error while reading joystick device : %s\n",strerror(errno));
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
104 else
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
105 printf("Joystick error while reading joystick device : EOF\n");
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
106 return MP_INPUT_DEAD;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
107 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
108 l += r;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
109 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
110
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
111 if((unsigned int)l < sizeof(struct js_event)) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
112 if(l > 0)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
113 printf("Joystick : we loose %d bytes of data\n",l);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
114 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
115 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
116
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
117 if(ev.type & JS_EVENT_INIT) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
118 printf("Joystick : warning reinit (Can happend more than on time)\n");
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
119 ev.type &= ~JS_EVENT_INIT;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
120 if(ev.type & JS_EVENT_BUTTON)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
121 buttons |= (ev.value << ev.number);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
122 else if(ev.type & JS_EVENT_AXIS)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
123 axis[ev.number] = ev.value;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
124 else
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
125 printf("Joystick warning unknow event type %d\n",ev.type);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
126 return mp_input_joystick_read(fd);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
127 }
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
128
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
129 ev.type &= ~JS_EVENT_INIT;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
130
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
131 if(ev.type & JS_EVENT_BUTTON) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
132 int b = buttons | (ev.value << ev.number);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
133 if(b != buttons)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
134 return JOY_BTN0+ev.number;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
135 } else if(ev.type & JS_EVENT_AXIS) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
136 if(ev.value - axis[ev.number] > JOY_AXIS_DELTA)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
137 return ev.number == 0 ? JOY_UP : JOY_LEFT;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
138 else if(axis[ev.number] - ev.value > JOY_AXIS_DELTA)
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
139 return ev.number == 0 ? JOY_DOWN : JOY_RIGHT;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
140 } else {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
141 printf("Joystick warning unknow event type %d\n",ev.type);
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
142 return MP_INPUT_ERROR;
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
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
145 return MP_INPUT_NOTHING;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
146 }
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 #else
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
149
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
150 // dummy function
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 int mp_input_joystick_init(char* dev) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
153 return -1;
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
154 }
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_read(int fd) {
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
157
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
158 return MP_INPUT_NOTHING;
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 #endif
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
162
8141d2c399e4 A new configurable input system and joystick support for this system
albeu
parents:
diff changeset
163 #endif