4
** Copyright (c) 1993-2001 by Hans-Ulrich Kiel & Joerg Czeranski
5
** All rights reserved.
7
** Redistribution and use in source and binary forms, with or without
8
** modification, are permitted provided that the following conditions are
11
** 1. Redistributions of source code must retain the above copyright
12
** notice, this list of conditions and the following disclaimer.
13
** 2. Redistributions in binary form must reproduce the above copyright
14
** notice, this list of conditions and the following disclaimer in the
15
** documentation and/or other materials provided with the distribution.
16
** 3. The name of the authors may not be used to endorse or promote
17
** products derived from this software without specific prior written
19
** 4. The name ``iMaze'' may not be used for products derived from this
20
** software unless a prefix or a suffix is added to the name.
22
** THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
23
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25
** DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
26
** INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30
** STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31
** IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
** POSSIBILITY OF SUCH DAMAGE.
35
** Datei: linux_joystick.c
38
** Wertet Events vom Joystick auf Linux-Systemen aus
48
#include <linux/joystick.h>
59
static char sccsid[] = "@(#)linux_joystick.c 3.7 12/3/01";
62
#ifndef DEFAULT_JOYSTICK_DEVICE
63
#define DEFAULT_JOYSTICK_DEVICE "/dev/js0"
67
static int want_joystick = 0;
68
static char *set_device_name = NULL;
69
struct arg_option joystick_opts[] =
71
{ Arg_Simple, "j", &want_joystick, "enable joystick" },
72
{ Arg_String, "J", &set_device_name, "set joystick device", "device" },
77
static int joystick; /* Deskriptor fuer Joystick-Device */
78
static int joystick_abfragen; /* Flag: Joystick benutzen? */
79
static int joystick_gelesen; /* Flag: joystick_xmax, ... initialisiert? */
80
static int joystick_xmin; /* minimaler x-Wert der Joystick-Position */
81
static int joystick_xmax; /* maximaler x-Wert der Joystick-Position */
82
static int joystick_ymin; /* minimaler y-Wert der Joystick-Position */
83
static int joystick_ymax; /* maximaler y-Wert der Joystick-Position */
86
/* bis hier lokaler Teil */
87
/***********************************************/
88
/* ab hier globaler Teil */
91
void joystick_trigger(void)
93
/* Joystick benutzen? */
94
if (joystick_abfragen)
96
int laenge; /* Laenge der gelesenen Daten */
97
int j_x, j_y, j_button; /* gelesene Daten */
98
struct JS_DATA_TYPE joystick_daten; /* Position des Joysticks */
100
/* Joystick abfragen */
101
laenge = read(joystick, &joystick_daten, sizeof joystick_daten);
104
if (laenge < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
106
static char *meldung[] = { "iMaze - Joystick Error", "",
107
"Can't read from joystick device:", NULL, NULL };
109
meldung[3] = fehler_text();
110
uebler_fehler(meldung, NULL);
113
/* stimmt die Laenge der gelesenen Daten? */
114
if (laenge == sizeof joystick_daten)
116
j_x = joystick_daten.x;
117
j_y = joystick_daten.y;
119
j_button = joystick_daten.buttons != 0;
121
/* xmin, ... schon initialisiert? */
122
if (joystick_gelesen)
124
joystick_xmin = min(j_x, joystick_xmin);
125
joystick_xmax = max(j_x, joystick_xmax);
126
joystick_ymin = min(j_y, joystick_ymin);
127
joystick_ymax = max(j_y, joystick_ymax);
131
joystick_xmin = joystick_xmax = j_x;
132
joystick_ymin = joystick_ymax = j_y;
134
joystick_gelesen = 1;
137
/* Joystick-Buttons abfragen */
138
joystick_event(SCHUSSSIGNAL, j_button);
140
/* x-Richtung abfragen */
141
joystick_event(LINKSSIGNAL,
142
3 * j_x < 2 * joystick_xmin + joystick_xmax);
143
joystick_event(RECHTSSIGNAL,
144
3 * j_x > joystick_xmin + 2 * joystick_xmax);
146
/* y-Richtung abfragen */
147
joystick_event(VORSIGNAL,
148
3 * j_y < 2 * joystick_ymin + joystick_ymax);
149
joystick_event(ZURUECKSIGNAL,
150
3 * j_y > joystick_ymin + 2 * joystick_ymax);
156
int joystick_init(void)
158
char *joystick_device; /* Name des Joystick-Devices */
161
joystick_abfragen = want_joystick || set_device_name != NULL;
162
joystick_device = set_device_name != NULL ? set_device_name :
163
DEFAULT_JOYSTICK_DEVICE;
165
/* Joystick nicht benutzen? */
166
if (!joystick_abfragen)
169
/* Joystick-Device oeffnen Erfolg oder Abbruch */
172
static char *meldung[] = { "iMaze - Joystick Error",
173
"", NULL, NULL, NULL };
175
/* Joystick-Device oeffnen */
176
if ((joystick = open(joystick_device, O_RDONLY | O_NDELAY, 0)) >= 0)
179
/* Fehlertext erzeugen */
180
speicher_belegen((void **)&meldung[2],
181
strlen(joystick_device) + 20);
182
sprintf(meldung[2], "Can't open %s:", joystick_device);
183
meldung[3] = fehler_text();
185
/* sonstiger Fehler? */
187
if (rueckfrage(meldung, "No Joystick", NULL))
189
joystick_abfragen = 0;
190
speicher_freigeben((void **)&meldung[2]);
196
speicher_freigeben((void **)&meldung[2]);
200
/* Device gerade in Benutzung? */
201
else if (!rueckfrage(meldung, "Try again", "No Joystick"))
203
joystick_abfragen = 0;
204
speicher_freigeben((void **)&meldung[2]);
209
speicher_freigeben((void **)&meldung[2]);
212
/* Joystick-Device in nicht blockierenden Modus schalten */
213
if (fcntl(joystick, F_SETFL, O_NONBLOCK))
215
static char *meldung[] = { "iMaze - Joystick Error", "",
216
"Can't switch joystick device to non-blocking mode:",
219
meldung[3] = fehler_text();
220
uebler_fehler(meldung, NULL);
223
/* xmin, xmax, ... noch nicht initialisiert */
224
joystick_gelesen = 0;
225
return 0; /* Erfolg */
229
void joystick_close(void)
231
/* Joystick schliessen */
232
if (joystick_abfragen && close(joystick))
234
static char *meldung[] = { "iMaze - Joystick Error", "",
235
"Can't close joystick device:", NULL, NULL };
237
meldung[3] = fehler_text();
238
uebler_fehler(meldung, NULL);
241
/* Joystick nicht mehr abfragen */
242
joystick_abfragen = 0;