~ubuntu-branches/ubuntu/vivid/imaze/vivid

« back to all changes in this revision

Viewing changes to source/linux_joystick.c

  • Committer: Bazaar Package Importer
  • Author(s): Hans Freitag
  • Date: 2002-11-28 13:24:12 UTC
  • Revision ID: james.westby@ubuntu.com-20021128132412-lw82xl9oq1j36g8b
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
** - - -  iMaze  - - -
 
3
**
 
4
** Copyright (c) 1993-2001 by Hans-Ulrich Kiel & Joerg Czeranski
 
5
** All rights reserved.
 
6
**
 
7
** Redistribution and use in source and binary forms, with or without
 
8
** modification, are permitted provided that the following conditions are
 
9
** met:
 
10
**
 
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
 
18
**    permission.
 
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.
 
21
**
 
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.
 
33
**
 
34
**
 
35
** Datei: linux_joystick.c
 
36
**
 
37
** Kommentar:
 
38
**  Wertet Events vom Joystick auf Linux-Systemen aus
 
39
*/
 
40
 
 
41
 
 
42
#include <stdio.h>
 
43
#include <string.h>
 
44
#include <errno.h>
 
45
#include <fcntl.h>
 
46
#include <unistd.h>
 
47
#include <sys/time.h>
 
48
#include <linux/joystick.h>
 
49
 
 
50
#include "argv.h"
 
51
#include "global.h"
 
52
#include "fehler.h"
 
53
#include "speicher.h"
 
54
#include "system.h"
 
55
#include "signale.h"
 
56
#include "grafik.h"
 
57
#include "joystick.h"
 
58
 
 
59
static char sccsid[] = "@(#)linux_joystick.c    3.7 12/3/01";
 
60
 
 
61
 
 
62
#ifndef DEFAULT_JOYSTICK_DEVICE
 
63
#define DEFAULT_JOYSTICK_DEVICE "/dev/js0"
 
64
#endif
 
65
 
 
66
 
 
67
static int want_joystick = 0;
 
68
static char *set_device_name = NULL;
 
69
struct arg_option joystick_opts[] =
 
70
{
 
71
        { Arg_Simple, "j", &want_joystick, "enable joystick" },
 
72
        { Arg_String, "J", &set_device_name, "set joystick device", "device" },
 
73
        { Arg_End }
 
74
};
 
75
 
 
76
 
 
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 */
 
84
 
 
85
 
 
86
/* bis hier lokaler Teil                       */
 
87
/***********************************************/
 
88
/* ab hier globaler Teil                       */
 
89
 
 
90
 
 
91
void joystick_trigger(void)
 
92
{
 
93
        /* Joystick benutzen? */
 
94
        if (joystick_abfragen)
 
95
        {
 
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 */
 
99
 
 
100
                /* Joystick abfragen */
 
101
                laenge = read(joystick, &joystick_daten, sizeof joystick_daten);
 
102
 
 
103
                /* Fehler? */
 
104
                if (laenge < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
 
105
                {
 
106
                        static char *meldung[] = { "iMaze - Joystick Error", "",
 
107
                                "Can't read from joystick device:", NULL, NULL };
 
108
 
 
109
                        meldung[3] = fehler_text();
 
110
                        uebler_fehler(meldung, NULL);
 
111
                }
 
112
 
 
113
                /* stimmt die Laenge der gelesenen Daten? */
 
114
                if (laenge == sizeof joystick_daten)
 
115
                {
 
116
                        j_x = joystick_daten.x;
 
117
                        j_y = joystick_daten.y;
 
118
 
 
119
                        j_button = joystick_daten.buttons != 0;
 
120
 
 
121
                        /* xmin, ... schon initialisiert? */
 
122
                        if (joystick_gelesen)
 
123
                        {
 
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);
 
128
                        }
 
129
                        else
 
130
                        {
 
131
                                joystick_xmin = joystick_xmax = j_x;
 
132
                                joystick_ymin = joystick_ymax = j_y;
 
133
 
 
134
                                joystick_gelesen = 1;
 
135
                        }
 
136
 
 
137
                        /* Joystick-Buttons abfragen */
 
138
                        joystick_event(SCHUSSSIGNAL, j_button);
 
139
 
 
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);
 
145
 
 
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);
 
151
                }
 
152
        }
 
153
}
 
154
 
 
155
 
 
156
int joystick_init(void)
 
157
{
 
158
        char *joystick_device; /* Name des Joystick-Devices */
 
159
 
 
160
        /* initialisieren */
 
161
        joystick_abfragen = want_joystick || set_device_name != NULL;
 
162
        joystick_device = set_device_name != NULL ? set_device_name :
 
163
                DEFAULT_JOYSTICK_DEVICE;
 
164
 
 
165
        /* Joystick nicht benutzen? */
 
166
        if (!joystick_abfragen)
 
167
                return 0;
 
168
 
 
169
        /* Joystick-Device oeffnen Erfolg oder Abbruch */
 
170
        for (;;)
 
171
        {
 
172
                static char *meldung[] = { "iMaze - Joystick Error",
 
173
                        "", NULL, NULL, NULL };
 
174
 
 
175
                /* Joystick-Device oeffnen */
 
176
                if ((joystick = open(joystick_device, O_RDONLY | O_NDELAY, 0)) >= 0)
 
177
                        break;
 
178
 
 
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();
 
184
 
 
185
                /* sonstiger Fehler? */
 
186
                if (errno != EBUSY)
 
187
                        if (rueckfrage(meldung, "No Joystick", NULL))
 
188
                        {
 
189
                                joystick_abfragen = 0;
 
190
                                speicher_freigeben((void **)&meldung[2]);
 
191
 
 
192
                                return 0;
 
193
                        }
 
194
                        else
 
195
                        {
 
196
                                speicher_freigeben((void **)&meldung[2]);
 
197
 
 
198
                                return 1;
 
199
                        }
 
200
                /* Device gerade in Benutzung? */
 
201
                else if (!rueckfrage(meldung, "Try again", "No Joystick"))
 
202
                {
 
203
                        joystick_abfragen = 0;
 
204
                        speicher_freigeben((void **)&meldung[2]);
 
205
 
 
206
                        return 0;
 
207
                }
 
208
 
 
209
                speicher_freigeben((void **)&meldung[2]);
 
210
        }
 
211
 
 
212
        /* Joystick-Device in nicht blockierenden Modus schalten */
 
213
        if (fcntl(joystick, F_SETFL, O_NONBLOCK))
 
214
        {
 
215
                static char *meldung[] = { "iMaze - Joystick Error", "",
 
216
                        "Can't switch joystick device to non-blocking mode:",
 
217
                        NULL, NULL };
 
218
 
 
219
                meldung[3] = fehler_text();
 
220
                uebler_fehler(meldung, NULL);
 
221
        }
 
222
 
 
223
        /* xmin, xmax, ... noch nicht initialisiert */
 
224
        joystick_gelesen = 0;
 
225
        return 0; /* Erfolg */
 
226
}
 
227
 
 
228
 
 
229
void joystick_close(void)
 
230
{
 
231
        /* Joystick schliessen */
 
232
        if (joystick_abfragen && close(joystick))
 
233
        {
 
234
                static char *meldung[] = { "iMaze - Joystick Error", "",
 
235
                        "Can't close joystick device:", NULL, NULL };
 
236
 
 
237
                meldung[3] = fehler_text();
 
238
                uebler_fehler(meldung, NULL);
 
239
        }
 
240
 
 
241
        /* Joystick nicht mehr abfragen */
 
242
        joystick_abfragen = 0;
 
243
}