~ubuntu-branches/ubuntu/gutsy/pygame/gutsy

« back to all changes in this revision

Viewing changes to src/joystick.c

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2002-02-20 06:39:24 UTC
  • Revision ID: james.westby@ubuntu.com-20020220063924-amlzj7tqkeods4eq
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    pygame - Python Game Library
 
3
    Copyright (C) 2000-2001  Pete Shinners
 
4
 
 
5
    This library is free software; you can redistribute it and/or
 
6
    modify it under the terms of the GNU Library General Public
 
7
    License as published by the Free Software Foundation; either
 
8
    version 2 of the License, or (at your option) any later version.
 
9
 
 
10
    This library is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
    Library General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU Library General Public
 
16
    License along with this library; if not, write to the Free
 
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
 
19
    Pete Shinners
 
20
    pete@shinners.org
 
21
*/
 
22
 
 
23
#define PYGAMEAPI_JOYSTICK_INTERNAL
 
24
#include "pygame.h"
 
25
 
 
26
 
 
27
#define JOYSTICK_MAXSTICKS 32
 
28
static SDL_Joystick* joystick_stickdata[JOYSTICK_MAXSTICKS] = {NULL};
 
29
 
 
30
 
 
31
staticforward PyTypeObject PyJoystick_Type;
 
32
static PyObject* PyJoystick_New(int);
 
33
#define PyJoystick_Check(x) ((x)->ob_type == &PyJoystick_Type)
 
34
 
 
35
 
 
36
static void joy_autoquit(void)
 
37
{
 
38
        int loop;
 
39
        for(loop = 0; loop < JOYSTICK_MAXSTICKS; ++loop)
 
40
        {
 
41
                if(joystick_stickdata[loop])
 
42
                {
 
43
                        SDL_JoystickClose(joystick_stickdata[loop]);
 
44
                        joystick_stickdata[loop] = NULL;
 
45
                }
 
46
        }
 
47
 
 
48
        if(SDL_WasInit(SDL_INIT_JOYSTICK))
 
49
        {
 
50
                SDL_JoystickEventState(SDL_ENABLE);
 
51
                SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
 
52
        }
 
53
}
 
54
 
 
55
static PyObject* joy_autoinit(PyObject* self, PyObject* arg)
 
56
{
 
57
        if(!PyArg_ParseTuple(arg, ""))
 
58
                return NULL;
 
59
 
 
60
        if(!SDL_WasInit(SDL_INIT_JOYSTICK))
 
61
        {
 
62
                if(SDL_InitSubSystem(SDL_INIT_JOYSTICK))
 
63
                        return PyInt_FromLong(0);
 
64
                SDL_JoystickEventState(SDL_ENABLE);
 
65
                PyGame_RegisterQuit(joy_autoquit);
 
66
        }
 
67
        return PyInt_FromLong(1);
 
68
}
 
69
 
 
70
 
 
71
    /*DOC*/ static char doc_quit[] =
 
72
    /*DOC*/    "pygame.joystick.quit() -> None\n"
 
73
    /*DOC*/    "uninitialize joystick module\n"
 
74
    /*DOC*/    "\n"
 
75
    /*DOC*/    "Uninitialize the joystick module manually\n"
 
76
    /*DOC*/ ;
 
77
 
 
78
static PyObject* quit(PyObject* self, PyObject* arg)
 
79
{
 
80
        if(!PyArg_ParseTuple(arg, ""))
 
81
                return NULL;
 
82
 
 
83
        joy_autoquit();
 
84
 
 
85
        RETURN_NONE
 
86
}
 
87
 
 
88
    /*DOC*/ static char doc_init[] =
 
89
    /*DOC*/    "pygame.joystick.init() -> None\n"
 
90
    /*DOC*/    "initialize joystick module\n"
 
91
    /*DOC*/    "\n"
 
92
    /*DOC*/    "Initialize the joystick module manually\n"
 
93
    /*DOC*/ ;
 
94
 
 
95
static PyObject* init(PyObject* self, PyObject* arg)
 
96
{
 
97
        PyObject* result;
 
98
        int istrue;
 
99
 
 
100
        if(!PyArg_ParseTuple(arg, ""))
 
101
                return NULL;
 
102
 
 
103
        result = joy_autoinit(self, arg);
 
104
        istrue = PyObject_IsTrue(result);
 
105
        Py_DECREF(result);
 
106
        if(!istrue)
 
107
                return RAISE(PyExc_SDLError, SDL_GetError());
 
108
 
 
109
        RETURN_NONE
 
110
}
 
111
 
 
112
 
 
113
 
 
114
    /*DOC*/ static char doc_get_init[] =
 
115
    /*DOC*/    "pygame.joystick.get_init() -> bool\n"
 
116
    /*DOC*/    "query initialization of joystick module\n"
 
117
    /*DOC*/    "\n"
 
118
    /*DOC*/    "Returns true when the joystick module is initialized.\n"
 
119
    /*DOC*/ ;
 
120
 
 
121
static PyObject* get_init(PyObject* self, PyObject* arg)
 
122
{
 
123
        if(!PyArg_ParseTuple(arg, ""))
 
124
                return NULL;
 
125
 
 
126
        return PyInt_FromLong(SDL_WasInit(SDL_INIT_JOYSTICK)!=0);
 
127
}
 
128
 
 
129
 
 
130
 
 
131
/*joystick object funcs*/
 
132
 
 
133
 
 
134
static void joy_dealloc(PyObject* self)
 
135
{
 
136
        PyObject_DEL(self);
 
137
}
 
138
 
 
139
 
 
140
    /*DOC*/ static char doc_Joystick[] =
 
141
    /*DOC*/    "pygame.joystick.Joystick(id) -> Joystick\n"
 
142
    /*DOC*/    "create new joystick object\n"
 
143
    /*DOC*/    "\n"
 
144
    /*DOC*/    "Creates a new joystick object for the given device id. The given id\n"
 
145
    /*DOC*/    "must be less than the value from pygame.joystick.get_count().\n"
 
146
    /*DOC*/ ;
 
147
 
 
148
static PyObject* Joystick(PyObject* self, PyObject* args)
 
149
{
 
150
        int id; 
 
151
        if(!PyArg_ParseTuple(args, "i", &id))
 
152
                return NULL;
 
153
 
 
154
        JOYSTICK_INIT_CHECK();
 
155
 
 
156
        return PyJoystick_New(id);
 
157
}
 
158
 
 
159
 
 
160
 
 
161
    /*DOC*/ static char doc_get_count[] =
 
162
    /*DOC*/    "pygame.joystick.get_count() -> int\n"
 
163
    /*DOC*/    "query number of joysticks on system\n"
 
164
    /*DOC*/    "\n"
 
165
    /*DOC*/    "Returns the number of joysticks devices available on\n"
 
166
    /*DOC*/    "the system.\n"
 
167
    /*DOC*/ ;
 
168
 
 
169
static PyObject* get_count(PyObject* self, PyObject* args)
 
170
{
 
171
        if(!PyArg_ParseTuple(args, ""))
 
172
                return NULL;
 
173
 
 
174
        JOYSTICK_INIT_CHECK();
 
175
 
 
176
        return PyInt_FromLong(SDL_NumJoysticks());
 
177
}
 
178
 
 
179
 
 
180
 
 
181
 
 
182
    /*DOC*/ static char doc_joy_init[] =
 
183
    /*DOC*/    "Joystick.init() -> None\n"
 
184
    /*DOC*/    "initialize a joystick device for use\n"
 
185
    /*DOC*/    "\n"
 
186
    /*DOC*/    "In order to call most members in the Joystick object, the\n"
 
187
    /*DOC*/    "Joystick must be initialized. You can initialzie the Joystick object\n"
 
188
    /*DOC*/    "at anytime, and it is ok to initialize more than once.\n"
 
189
    /*DOC*/ ;
 
190
 
 
191
static PyObject* joy_init(PyObject* self, PyObject* args)
 
192
{
 
193
        int joy_id = PyJoystick_AsID(self);
 
194
 
 
195
        if(!PyArg_ParseTuple(args, ""))
 
196
                return NULL;
 
197
 
 
198
        JOYSTICK_INIT_CHECK();
 
199
 
 
200
        if(!joystick_stickdata[joy_id])
 
201
        {
 
202
                joystick_stickdata[joy_id] = SDL_JoystickOpen(joy_id);
 
203
                if(!joystick_stickdata[joy_id])
 
204
                        return RAISE(PyExc_SDLError, SDL_GetError());
 
205
        }
 
206
        RETURN_NONE
 
207
}
 
208
 
 
209
 
 
210
    /*DOC*/ static char doc_joy_quit[] =
 
211
    /*DOC*/    "Joystick.quit() -> None\n"
 
212
    /*DOC*/    "uninitialize a joystick device for use\n"
 
213
    /*DOC*/    "\n"
 
214
    /*DOC*/    "After you are completely finished with a joystick device, you\n"
 
215
    /*DOC*/    "can use this quit() function to free access to the drive.\n"
 
216
    /*DOC*/    "This will be cleaned up automatically when the joystick module is.\n"
 
217
    /*DOC*/    "uninitialized. It is safe to call this function on an uninitialized Joystick.\n"
 
218
    /*DOC*/ ;
 
219
 
 
220
static PyObject* joy_quit(PyObject* self, PyObject* args)
 
221
{
 
222
        int joy_id = PyJoystick_AsID(self);
 
223
 
 
224
        if(!PyArg_ParseTuple(args, ""))
 
225
                return NULL;
 
226
 
 
227
        JOYSTICK_INIT_CHECK();
 
228
 
 
229
        if(joystick_stickdata[joy_id])
 
230
        {
 
231
                SDL_JoystickClose(joystick_stickdata[joy_id]);
 
232
                joystick_stickdata[joy_id] = NULL;
 
233
        }
 
234
        RETURN_NONE
 
235
}
 
236
 
 
237
 
 
238
 
 
239
    /*DOC*/ static char doc_joy_get_init[] =
 
240
    /*DOC*/    "Joystick.get_init() -> bool\n"
 
241
    /*DOC*/    "check if joystick is initialized\n"
 
242
    /*DOC*/    "\n"
 
243
    /*DOC*/    "Returns a true value if the Joystick is initialized.\n"
 
244
    /*DOC*/ ;
 
245
 
 
246
static PyObject* joy_get_init(PyObject* self, PyObject* args)
 
247
{
 
248
        int joy_id = PyJoystick_AsID(self);
 
249
 
 
250
        if(!PyArg_ParseTuple(args, ""))
 
251
                return NULL;
 
252
 
 
253
        return PyInt_FromLong(joystick_stickdata[joy_id] != NULL);
 
254
}
 
255
 
 
256
 
 
257
 
 
258
    /*DOC*/ static char doc_joy_get_id[] =
 
259
    /*DOC*/    "Joystick.get_id() -> idnum\n"
 
260
    /*DOC*/    "get device id number for joystick\n"
 
261
    /*DOC*/    "\n"
 
262
    /*DOC*/    "Returns the device id number for this Joystick. This is the\n"
 
263
    /*DOC*/    "same number used in the call to pygame.joystick.Joystick() to create\n"
 
264
    /*DOC*/    "the object. The Joystick does not need to be initialized for this\n"
 
265
    /*DOC*/    "function to work.\n"
 
266
    /*DOC*/ ;
 
267
 
 
268
static PyObject* joy_get_id(PyObject* self, PyObject* args)
 
269
{
 
270
        int joy_id = PyJoystick_AsID(self);
 
271
 
 
272
        if(!PyArg_ParseTuple(args, ""))
 
273
                return NULL;
 
274
        return PyInt_FromLong(joy_id);
 
275
}
 
276
 
 
277
 
 
278
    /*DOC*/ static char doc_joy_get_name[] =
 
279
    /*DOC*/    "Joystick.get_name(id) -> string\n"
 
280
    /*DOC*/    "query name of joystick drive\n"
 
281
    /*DOC*/    "\n"
 
282
    /*DOC*/    "Returns the name of the Joystick device, given by the\n"
 
283
    /*DOC*/    "system. This function can be called before the Joystick\n"
 
284
    /*DOC*/    "is initialized.\n"
 
285
    /*DOC*/ ;
 
286
 
 
287
static PyObject* joy_get_name(PyObject* self, PyObject* args)
 
288
{
 
289
        int joy_id = PyJoystick_AsID(self);
 
290
        
 
291
        if(!PyArg_ParseTuple(args, ""))
 
292
                return NULL;
 
293
 
 
294
        JOYSTICK_INIT_CHECK();
 
295
 
 
296
        return PyString_FromString(SDL_JoystickName(joy_id));
 
297
}
 
298
 
 
299
 
 
300
 
 
301
    /*DOC*/ static char doc_joy_get_numaxes[] =
 
302
    /*DOC*/    "Joystick.get_numaxes() -> int\n"
 
303
    /*DOC*/    "get number of axes on a joystick\n"
 
304
    /*DOC*/    "\n"
 
305
    /*DOC*/    "Returns the number of available axes on the Joystick.\n"
 
306
    /*DOC*/ ;
 
307
 
 
308
static PyObject* joy_get_numaxes(PyObject* self, PyObject* args)
 
309
{
 
310
        int joy_id = PyJoystick_AsID(self);
 
311
        SDL_Joystick* joy = joystick_stickdata[joy_id];
 
312
 
 
313
        if(!PyArg_ParseTuple(args, ""))
 
314
                return NULL;
 
315
 
 
316
        JOYSTICK_INIT_CHECK();
 
317
        if(!joy)
 
318
                return RAISE(PyExc_SDLError, "Joystick not initialized");
 
319
 
 
320
        return PyInt_FromLong(SDL_JoystickNumAxes(joy));
 
321
}
 
322
 
 
323
 
 
324
 
 
325
    /*DOC*/ static char doc_joy_get_axis[] =
 
326
    /*DOC*/    "Joystick.get_axis(axis) -> float\n"
 
327
    /*DOC*/    "get the position of a joystick axis\n"
 
328
    /*DOC*/    "\n"
 
329
    /*DOC*/    "Returns the current position of a joystick axis. The value\n"
 
330
    /*DOC*/    "will range from -1 to 1 with a value of 0 being centered. You\n"
 
331
    /*DOC*/    "may want to take into account some tolerance to handle jitter,\n"
 
332
    /*DOC*/    "and joystick drift may keep the joystick from centering at 0 or\n"
 
333
    /*DOC*/    "using the full range of position values.\n"
 
334
    /*DOC*/ ;
 
335
 
 
336
static PyObject* joy_get_axis(PyObject* self, PyObject* args)
 
337
{
 
338
        int joy_id = PyJoystick_AsID(self);
 
339
        SDL_Joystick* joy = joystick_stickdata[joy_id];
 
340
        int axis, value;
 
341
        
 
342
        if(!PyArg_ParseTuple(args, "i", &axis))
 
343
                return NULL;
 
344
 
 
345
        JOYSTICK_INIT_CHECK();
 
346
        if(!joy)
 
347
                return RAISE(PyExc_SDLError, "Joystick not initialized");
 
348
        if(axis < 0 || axis >= SDL_JoystickNumAxes(joy))
 
349
                return RAISE(PyExc_SDLError, "Invalid joystick axis");
 
350
 
 
351
        value = SDL_JoystickGetAxis(joy, axis);
 
352
        return PyFloat_FromDouble(value / 32768.0);
 
353
}
 
354
 
 
355
 
 
356
    /*DOC*/ static char doc_joy_get_numbuttons[] =
 
357
    /*DOC*/    "Joystick.get_numbuttons() -> int\n"
 
358
    /*DOC*/    "get number of buttons on a joystick\n"
 
359
    /*DOC*/    "\n"
 
360
    /*DOC*/    "Returns the number of available buttons on the Joystick.\n"
 
361
    /*DOC*/ ;
 
362
 
 
363
static PyObject* joy_get_numbuttons(PyObject* self, PyObject* args)
 
364
{
 
365
        int joy_id = PyJoystick_AsID(self);
 
366
        SDL_Joystick* joy = joystick_stickdata[joy_id];
 
367
 
 
368
        if(!PyArg_ParseTuple(args, ""))
 
369
                return NULL;
 
370
 
 
371
        JOYSTICK_INIT_CHECK();
 
372
        if(!joy)
 
373
                return RAISE(PyExc_SDLError, "Joystick not initialized");
 
374
 
 
375
        return PyInt_FromLong(SDL_JoystickNumButtons(joy));
 
376
}
 
377
 
 
378
 
 
379
 
 
380
    /*DOC*/ static char doc_joy_get_button[] =
 
381
    /*DOC*/    "Joystick.get_button(button) -> bool\n"
 
382
    /*DOC*/    "get the position of a joystick button\n"
 
383
    /*DOC*/    "\n"
 
384
    /*DOC*/    "Returns the current state of a joystick button.\n"
 
385
    /*DOC*/ ;
 
386
 
 
387
static PyObject* joy_get_button(PyObject* self, PyObject* args)
 
388
{
 
389
        int joy_id = PyJoystick_AsID(self);
 
390
        SDL_Joystick* joy = joystick_stickdata[joy_id];
 
391
        int index, value;
 
392
        
 
393
        if(!PyArg_ParseTuple(args, "i", &index))
 
394
                return NULL;
 
395
 
 
396
        JOYSTICK_INIT_CHECK();
 
397
        if(!joy)
 
398
                return RAISE(PyExc_SDLError, "Joystick not initialized");
 
399
        if(index < 0 || index >= SDL_JoystickNumButtons(joy))
 
400
                return RAISE(PyExc_SDLError, "Invalid joystick button");
 
401
 
 
402
        value = SDL_JoystickGetButton(joy, index);
 
403
        return PyInt_FromLong(value);
 
404
}
 
405
 
 
406
 
 
407
    /*DOC*/ static char doc_joy_get_numballs[] =
 
408
    /*DOC*/    "Joystick.get_numballs() -> int\n"
 
409
    /*DOC*/    "get number of trackballs on a joystick\n"
 
410
    /*DOC*/    "\n"
 
411
    /*DOC*/    "Returns the number of available trackballs on the Joystick.\n"
 
412
    /*DOC*/ ;
 
413
 
 
414
static PyObject* joy_get_numballs(PyObject* self, PyObject* args)
 
415
{
 
416
        int joy_id = PyJoystick_AsID(self);
 
417
        SDL_Joystick* joy = joystick_stickdata[joy_id];
 
418
 
 
419
        if(!PyArg_ParseTuple(args, ""))
 
420
                return NULL;
 
421
 
 
422
        JOYSTICK_INIT_CHECK();
 
423
        if(!joy)
 
424
                return RAISE(PyExc_SDLError, "Joystick not initialized");
 
425
 
 
426
        return PyInt_FromLong(SDL_JoystickNumBalls(joy));
 
427
}
 
428
 
 
429
 
 
430
 
 
431
    /*DOC*/ static char doc_joy_get_ball[] =
 
432
    /*DOC*/    "Joystick.get_ball(button) -> x, y\n"
 
433
    /*DOC*/    "get the movement of a joystick trackball\n"
 
434
    /*DOC*/    "\n"
 
435
    /*DOC*/    "Returns the relative movement of a joystick button. The\n"
 
436
    /*DOC*/    "value is a x, y pair holding the relative movement since the\n"
 
437
    /*DOC*/    "last call to get_ball()\n"
 
438
    /*DOC*/ ;
 
439
 
 
440
static PyObject* joy_get_ball(PyObject* self, PyObject* args)
 
441
{
 
442
        int joy_id = PyJoystick_AsID(self);
 
443
        SDL_Joystick* joy = joystick_stickdata[joy_id];
 
444
        int index, dx, dy;
 
445
        
 
446
        if(!PyArg_ParseTuple(args, "i", &index))
 
447
                return NULL;
 
448
 
 
449
        JOYSTICK_INIT_CHECK();
 
450
        if(!joy)
 
451
                return RAISE(PyExc_SDLError, "Joystick not initialized");
 
452
        if(index < 0 || index >= SDL_JoystickNumBalls(joy))
 
453
                return RAISE(PyExc_SDLError, "Invalid joystick trackball");
 
454
 
 
455
        SDL_JoystickGetBall(joy, index, &dx, &dy);
 
456
        return Py_BuildValue("(ii)", dx, dy);
 
457
}
 
458
 
 
459
 
 
460
    /*DOC*/ static char doc_joy_get_numhats[] =
 
461
    /*DOC*/    "Joystick.get_numhats() -> int\n"
 
462
    /*DOC*/    "get number of hats on a joystick\n"
 
463
    /*DOC*/    "\n"
 
464
    /*DOC*/    "Returns the number of available directional hats on the Joystick.\n"
 
465
    /*DOC*/ ;
 
466
 
 
467
static PyObject* joy_get_numhats(PyObject* self, PyObject* args)
 
468
{
 
469
        int joy_id = PyJoystick_AsID(self);
 
470
        SDL_Joystick* joy = joystick_stickdata[joy_id];
 
471
 
 
472
        if(!PyArg_ParseTuple(args, ""))
 
473
                return NULL;
 
474
 
 
475
        JOYSTICK_INIT_CHECK();
 
476
        if(!joy)
 
477
                return RAISE(PyExc_SDLError, "Joystick not initialized");
 
478
 
 
479
        return PyInt_FromLong(SDL_JoystickNumHats(joy));
 
480
}
 
481
 
 
482
 
 
483
 
 
484
    /*DOC*/ static char doc_joy_get_hat[] =
 
485
    /*DOC*/    "Joystick.get_hat(button) -> x, y\n"
 
486
    /*DOC*/    "get the position of a joystick hat\n"
 
487
    /*DOC*/    "\n"
 
488
    /*DOC*/    "Returns the current position of a position hat. The position\n"
 
489
    /*DOC*/    "is given as two values representing the X and Y position for the\n"
 
490
    /*DOC*/    "hat. (0, 0) means centered. A value of -1 means left/down a value\n"
 
491
    /*DOC*/    "of one means right/up\n"
 
492
    /*DOC*/ ;
 
493
 
 
494
static PyObject* joy_get_hat(PyObject* self, PyObject* args)
 
495
{
 
496
        int joy_id = PyJoystick_AsID(self);
 
497
        SDL_Joystick* joy = joystick_stickdata[joy_id];
 
498
        int index, px, py;
 
499
        Uint8 value;
 
500
 
 
501
        if(!PyArg_ParseTuple(args, "i", &index))
 
502
                return NULL;
 
503
 
 
504
        JOYSTICK_INIT_CHECK();
 
505
        if(!joy)
 
506
                return RAISE(PyExc_SDLError, "Joystick not initialized");
 
507
        if(index < 0 || index >= SDL_JoystickNumHats(joy))
 
508
                return RAISE(PyExc_SDLError, "Invalid joystick hat");
 
509
 
 
510
        px = py = 0;
 
511
        value = SDL_JoystickGetHat(joy, index);
 
512
        if(value&SDL_HAT_UP) py = 1;
 
513
        else if(value&SDL_HAT_DOWN) py = -1;
 
514
        if(value&SDL_HAT_RIGHT) px = 1;
 
515
        else if(value&SDL_HAT_LEFT) px = -1;
 
516
        
 
517
        return Py_BuildValue("(ii)", px, py);
 
518
}
 
519
 
 
520
 
 
521
 
 
522
static PyMethodDef joy_builtins[] =
 
523
{
 
524
        { "init", joy_init, 1, doc_joy_init },
 
525
        { "quit", joy_quit, 1, doc_joy_quit },
 
526
        { "get_init", joy_get_init, 1, doc_joy_get_init },
 
527
 
 
528
        { "get_id", joy_get_id, 1, doc_joy_get_id },
 
529
        { "get_name", joy_get_name, 1, doc_joy_get_name },
 
530
 
 
531
        { "get_numaxes", joy_get_numaxes, 1, doc_joy_get_numaxes },
 
532
        { "get_axis", joy_get_axis, 1, doc_joy_get_axis },
 
533
        { "get_numbuttons", joy_get_numbuttons, 1, doc_joy_get_numbuttons },
 
534
        { "get_button", joy_get_button, 1, doc_joy_get_button },
 
535
        { "get_numballs", joy_get_numballs, 1, doc_joy_get_numballs },
 
536
        { "get_ball", joy_get_ball, 1, doc_joy_get_ball },
 
537
        { "get_numhats", joy_get_numhats, 1, doc_joy_get_numhats },
 
538
        { "get_hat", joy_get_hat, 1, doc_joy_get_hat },
 
539
 
 
540
        { NULL, NULL }
 
541
};
 
542
 
 
543
static PyObject* joy_getattr(PyObject* self, char* attrname)
 
544
{
 
545
        return Py_FindMethod(joy_builtins, self, attrname);
 
546
}
 
547
 
 
548
 
 
549
    /*DOC*/ static char doc_Joystick_MODULE[] =
 
550
    /*DOC*/    "The Joystick object represents a joystick device and allows you to\n"
 
551
    /*DOC*/    "access the controls on that joystick. All functions (except get_name()\n"
 
552
    /*DOC*/    "and get_id()) require the Joystick object to be initialized. This is done\n"
 
553
    /*DOC*/    "with the Joystick.init() function.\n"
 
554
    /*DOC*/    "\n"
 
555
    /*DOC*/    "Joystick control values are only updated during the calls to the event\n"
 
556
    /*DOC*/    "queue. Call pygame.event.pump() if you are not using the event queue for\n"
 
557
    /*DOC*/    "any input handling. Once a joystick object has been initialized, it will\n"
 
558
    /*DOC*/    "start to send joystick events to the input queue.\n"
 
559
    /*DOC*/    "\n"
 
560
    /*DOC*/    "Be sure to understand there is a difference between the joystick module\n"
 
561
    /*DOC*/    "and the Joystick objects.\n"
 
562
    /*DOC*/ ;
 
563
 
 
564
 
 
565
static PyTypeObject PyJoystick_Type =
 
566
{
 
567
        PyObject_HEAD_INIT(NULL)
 
568
        0,
 
569
        "Joystick",
 
570
        sizeof(PyJoystickObject),
 
571
        0,
 
572
        joy_dealloc,
 
573
        0,
 
574
        joy_getattr,
 
575
        0,
 
576
        0,
 
577
        0,
 
578
        0,
 
579
        NULL,
 
580
        0, 
 
581
        (hashfunc)NULL,
 
582
        (ternaryfunc)NULL,
 
583
        (reprfunc)NULL,
 
584
        0L,0L,0L,0L,
 
585
        doc_Joystick_MODULE /* Documentation string */
 
586
};
 
587
 
 
588
 
 
589
 
 
590
static PyObject* PyJoystick_New(int id)
 
591
{
 
592
        PyJoystickObject* joy;
 
593
 
 
594
        if(id < 0 || id >= JOYSTICK_MAXSTICKS || id >= SDL_NumJoysticks())
 
595
                return RAISE(PyExc_SDLError, "Invalid joystick device number");
 
596
        
 
597
        joy = PyObject_NEW(PyJoystickObject, &PyJoystick_Type);
 
598
        if(!joy) return NULL;
 
599
 
 
600
        joy->id = id;
 
601
 
 
602
        return (PyObject*)joy;
 
603
}
 
604
 
 
605
 
 
606
 
 
607
 
 
608
 
 
609
static PyMethodDef joystick_builtins[] =
 
610
{
 
611
        { "__PYGAMEinit__", joy_autoinit, 1, doc_joy_init },
 
612
        { "init", init, 1, doc_init },
 
613
        { "quit", quit, 1, doc_quit },
 
614
        { "get_init", get_init, 1, doc_get_init },
 
615
        { "get_count", get_count, 1, doc_get_count },
 
616
        { "Joystick", Joystick, 1, doc_Joystick },
 
617
        { NULL, NULL }
 
618
};
 
619
 
 
620
 
 
621
 
 
622
 
 
623
    /*DOC*/ static char doc_pygame_joystick_MODULE[] =
 
624
    /*DOC*/    "The joystick module provides a few functions to initialize\n"
 
625
    /*DOC*/    "the joystick subsystem and to manage the Joystick objects. These\n"
 
626
    /*DOC*/    "objects are created with the pygame.joystick.Joystick() function.\n"
 
627
    /*DOC*/    "This function needs a joystick device number to work on. All\n"
 
628
    /*DOC*/    "joystick devices on the system are enumerated for use as a Joystick\n"
 
629
    /*DOC*/    "object. To access most of the Joystick functions, you'll need to\n"
 
630
    /*DOC*/    "init() the Joystick. (note that the joystick module will already\n"
 
631
    /*DOC*/    "be initialized). When multiple Joysticks objects are created for the\n"
 
632
    /*DOC*/    "same joystick device, the state and values for those Joystick objects\n"
 
633
    /*DOC*/    "will be shared.\n"
 
634
    /*DOC*/    "\n"
 
635
    /*DOC*/    "You can call the Joystick.get_name() and Joystick.get_id() functions\n"
 
636
    /*DOC*/    "without initializing the Joystick object.\n"
 
637
    /*DOC*/    "\n"
 
638
    /*DOC*/    "Joystick control values are only updated during the calls to the event\n"
 
639
    /*DOC*/    "queue. Call pygame.event.pump() if you are not using the event queue for\n"
 
640
    /*DOC*/    "any input handling. Once a joystick object has been initialized, it will\n"
 
641
    /*DOC*/    "start to send joystick events to the input queue.\n"
 
642
    /*DOC*/    "\n"
 
643
    /*DOC*/    "Be sure to understand there is a difference between the joystick module\n"
 
644
    /*DOC*/    "and the Joystick objects.\n"
 
645
    /*DOC*/ ;
 
646
 
 
647
PYGAME_EXPORT
 
648
void initjoystick(void)
 
649
{
 
650
        PyObject *module, *dict, *apiobj;
 
651
        static void* c_api[PYGAMEAPI_JOYSTICK_NUMSLOTS];
 
652
 
 
653
        PyType_Init(PyJoystick_Type);
 
654
 
 
655
 
 
656
    /* create the module */
 
657
        module = Py_InitModule3("joystick", joystick_builtins, doc_pygame_joystick_MODULE);
 
658
        dict = PyModule_GetDict(module);
 
659
 
 
660
        PyDict_SetItemString(dict, "JoystickType", (PyObject *)&PyJoystick_Type);
 
661
 
 
662
        /* export the c api */
 
663
        c_api[0] = &PyJoystick_Type;
 
664
        c_api[1] = PyJoystick_New;
 
665
        apiobj = PyCObject_FromVoidPtr(c_api, NULL);
 
666
        PyDict_SetItemString(dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
 
667
        Py_DECREF(apiobj);
 
668
 
 
669
        /*imported needed apis*/
 
670
        import_pygame_base();
 
671
}
 
672
 
 
673
 
 
674