2
pygame - Python Game Library
3
Copyright (C) 2000-2001 Pete Shinners
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.
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.
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
23
#define PYGAMEAPI_JOYSTICK_INTERNAL
26
#include "doc/joystick_doc.h"
28
#define JOYSTICK_MAXSTICKS 32
29
static SDL_Joystick* joystick_stickdata[JOYSTICK_MAXSTICKS] = {NULL};
30
static PyTypeObject PyJoystick_Type;
31
static PyObject* PyJoystick_New (int);
32
#define PyJoystick_Check(x) ((x)->ob_type == &PyJoystick_Type)
38
for (loop = 0; loop < JOYSTICK_MAXSTICKS; ++loop) {
39
if (joystick_stickdata[loop]) {
40
SDL_JoystickClose (joystick_stickdata[loop]);
41
joystick_stickdata[loop] = NULL;
45
if (SDL_WasInit (SDL_INIT_JOYSTICK)) {
46
SDL_JoystickEventState (SDL_ENABLE);
47
SDL_QuitSubSystem (SDL_INIT_JOYSTICK);
52
joy_autoinit (PyObject* self)
54
if (!SDL_WasInit (SDL_INIT_JOYSTICK)) {
55
if (SDL_InitSubSystem (SDL_INIT_JOYSTICK)) {
56
return PyInt_FromLong (0);
58
SDL_JoystickEventState (SDL_ENABLE);
59
PyGame_RegisterQuit (joy_autoquit);
61
return PyInt_FromLong (1);
77
result = joy_autoinit (self);
78
istrue = PyObject_IsTrue (result);
81
return RAISE (PyExc_SDLError, SDL_GetError ());
87
get_init (PyObject* self)
89
return PyInt_FromLong (SDL_WasInit (SDL_INIT_JOYSTICK) != 0);
92
/*joystick object funcs*/
94
joy_dealloc (PyObject* self)
100
Joystick (PyObject* self, PyObject* args)
103
if (!PyArg_ParseTuple (args, "i", &id)) {
107
JOYSTICK_INIT_CHECK ();
109
return PyJoystick_New (id);
113
get_count (PyObject* self)
115
JOYSTICK_INIT_CHECK ();
116
return PyInt_FromLong (SDL_NumJoysticks ());
120
joy_init (PyObject* self)
122
int joy_id = PyJoystick_AsID (self);
124
JOYSTICK_INIT_CHECK ();
125
if (!joystick_stickdata[joy_id]) {
126
joystick_stickdata[joy_id] = SDL_JoystickOpen (joy_id);
127
if (!joystick_stickdata[joy_id]) {
128
return RAISE (PyExc_SDLError, SDL_GetError ());
135
joy_quit (PyObject* self)
137
int joy_id = PyJoystick_AsID (self);
139
JOYSTICK_INIT_CHECK ();
141
if (joystick_stickdata[joy_id]) {
142
SDL_JoystickClose (joystick_stickdata[joy_id]);
143
joystick_stickdata[joy_id] = NULL;
149
joy_get_init (PyObject* self)
151
int joy_id = PyJoystick_AsID (self);
152
return PyInt_FromLong (joystick_stickdata[joy_id] != NULL);
156
joy_get_id (PyObject* self)
158
int joy_id = PyJoystick_AsID (self);
159
return PyInt_FromLong (joy_id);
163
joy_get_name (PyObject* self)
165
int joy_id = PyJoystick_AsID (self);
166
JOYSTICK_INIT_CHECK ();
167
return Text_FromUTF8 (SDL_JoystickName (joy_id));
171
joy_get_numaxes (PyObject* self)
173
int joy_id = PyJoystick_AsID (self);
174
SDL_Joystick* joy = joystick_stickdata[joy_id];
176
JOYSTICK_INIT_CHECK ();
178
return RAISE (PyExc_SDLError, "Joystick not initialized");
181
return PyInt_FromLong (SDL_JoystickNumAxes (joy));
185
joy_get_axis (PyObject* self, PyObject* args)
187
int joy_id = PyJoystick_AsID (self);
188
SDL_Joystick* joy = joystick_stickdata[joy_id];
191
if (!PyArg_ParseTuple (args, "i", &axis)) {
195
JOYSTICK_INIT_CHECK ();
197
return RAISE (PyExc_SDLError, "Joystick not initialized");
199
if (axis < 0 || axis >= SDL_JoystickNumAxes (joy)) {
200
return RAISE (PyExc_SDLError, "Invalid joystick axis");
203
value = SDL_JoystickGetAxis (joy, axis);
205
/*printf("SDL_JoystickGetAxis value:%d:\n", value);*/
208
return PyFloat_FromDouble (value / 32768.0);
212
joy_get_numbuttons (PyObject* self)
214
int joy_id = PyJoystick_AsID (self);
215
SDL_Joystick* joy = joystick_stickdata[joy_id];
217
JOYSTICK_INIT_CHECK ();
219
return RAISE (PyExc_SDLError, "Joystick not initialized");
222
return PyInt_FromLong (SDL_JoystickNumButtons (joy));
226
joy_get_button (PyObject* self, PyObject* args)
228
int joy_id = PyJoystick_AsID (self);
229
SDL_Joystick* joy = joystick_stickdata[joy_id];
232
if (!PyArg_ParseTuple (args, "i", &_index)) {
236
JOYSTICK_INIT_CHECK ();
238
return RAISE (PyExc_SDLError, "Joystick not initialized");
240
if (_index < 0 || _index >= SDL_JoystickNumButtons (joy)) {
241
return RAISE (PyExc_SDLError, "Invalid joystick button");
244
value = SDL_JoystickGetButton (joy, _index);
246
/*printf("SDL_JoystickGetButton value:%d:\n", value);*/
248
return PyInt_FromLong (value);
252
joy_get_numballs (PyObject* self)
254
int joy_id = PyJoystick_AsID (self);
255
SDL_Joystick* joy = joystick_stickdata[joy_id];
257
JOYSTICK_INIT_CHECK ();
259
return RAISE (PyExc_SDLError, "Joystick not initialized");
262
return PyInt_FromLong (SDL_JoystickNumBalls (joy));
266
joy_get_ball (PyObject* self, PyObject* args)
268
int joy_id = PyJoystick_AsID (self);
269
SDL_Joystick* joy = joystick_stickdata[joy_id];
273
if (!PyArg_ParseTuple (args, "i", &_index)) {
277
JOYSTICK_INIT_CHECK ();
279
return RAISE (PyExc_SDLError, "Joystick not initialized");
281
value = SDL_JoystickNumBalls (joy);
283
/*printf("SDL_JoystickNumBalls value:%d:\n", value);*/
285
if (_index < 0 || _index >= value) {
286
return RAISE (PyExc_SDLError, "Invalid joystick trackball");
289
SDL_JoystickGetBall (joy, _index, &dx, &dy);
290
return Py_BuildValue ("(ii)", dx, dy);
294
joy_get_numhats (PyObject* self)
296
int joy_id = PyJoystick_AsID (self);
298
SDL_Joystick* joy = joystick_stickdata[joy_id];
300
JOYSTICK_INIT_CHECK ();
302
return RAISE (PyExc_SDLError, "Joystick not initialized");
305
value = SDL_JoystickNumHats (joy);
307
/*printf("SDL_JoystickNumHats value:%d:\n", value);*/
309
return PyInt_FromLong (value);
313
joy_get_hat (PyObject* self, PyObject* args)
315
int joy_id = PyJoystick_AsID (self);
316
SDL_Joystick* joy = joystick_stickdata[joy_id];
320
if (!PyArg_ParseTuple (args, "i", &_index)) {
324
JOYSTICK_INIT_CHECK ();
326
return RAISE (PyExc_SDLError, "Joystick not initialized");
328
if (_index < 0 || _index >= SDL_JoystickNumHats (joy)) {
329
return RAISE(PyExc_SDLError, "Invalid joystick hat");
333
value = SDL_JoystickGetHat (joy, _index);
335
/*printf("SDL_JoystickGetHat value:%d:\n", value);*/
337
if (value & SDL_HAT_UP) {
340
else if (value & SDL_HAT_DOWN) {
343
if (value & SDL_HAT_RIGHT) {
346
else if (value & SDL_HAT_LEFT) {
350
return Py_BuildValue ("(ii)", px, py);
353
static PyMethodDef joy_methods[] =
355
{ "init", (PyCFunction) joy_init, METH_NOARGS, DOC_JOYSTICKINIT },
356
{ "quit", (PyCFunction) joy_quit, METH_NOARGS, DOC_JOYSTICKQUIT },
357
{ "get_init", (PyCFunction) joy_get_init, METH_NOARGS,
358
DOC_JOYSTICKGETINIT },
360
{ "get_id", (PyCFunction) joy_get_id, METH_NOARGS, DOC_JOYSTICKGETID },
361
{ "get_name", (PyCFunction) joy_get_name, METH_NOARGS,
362
DOC_JOYSTICKGETNAME },
364
{ "get_numaxes", (PyCFunction) joy_get_numaxes, METH_NOARGS,
365
DOC_JOYSTICKGETNUMAXES },
366
{ "get_axis", joy_get_axis, METH_VARARGS, DOC_JOYSTICKGETAXIS },
367
{ "get_numbuttons", (PyCFunction) joy_get_numbuttons, METH_NOARGS,
368
DOC_JOYSTICKGETNUMBUTTONS },
369
{ "get_button", joy_get_button, METH_VARARGS, DOC_JOYSTICKGETBUTTON },
370
{ "get_numballs", (PyCFunction) joy_get_numballs, METH_NOARGS,
371
DOC_JOYSTICKGETNUMBALLS },
372
{ "get_ball", joy_get_ball, METH_VARARGS, DOC_JOYSTICKGETBALL },
373
{ "get_numhats", (PyCFunction) joy_get_numhats, METH_NOARGS,
374
DOC_JOYSTICKGETNUMHATS },
375
{ "get_hat", joy_get_hat, METH_VARARGS, DOC_JOYSTICKGETHAT },
377
{ NULL, NULL, 0, NULL }
380
static PyTypeObject PyJoystick_Type =
383
"Joystick", /* name */
384
sizeof(PyJoystickObject), /* basic size */
386
joy_dealloc, /* dealloc */
400
0, /* tp_as_buffer */
402
DOC_PYGAMEJOYSTICKJOYSTICK, /* Documentation string */
405
0, /* tp_richcompare */
406
0, /* tp_weaklistoffset */
409
joy_methods, /* tp_methods */
414
0, /* tp_descr_get */
415
0, /* tp_descr_set */
416
0, /* tp_dictoffset */
423
PyJoystick_New (int id)
425
PyJoystickObject* joy;
427
if (id < 0 || id >= JOYSTICK_MAXSTICKS || id >= SDL_NumJoysticks ()) {
428
return RAISE (PyExc_SDLError, "Invalid joystick device number");
431
joy = PyObject_NEW (PyJoystickObject, &PyJoystick_Type);
438
return (PyObject*)joy;
441
static PyMethodDef _joystick_methods[] =
443
{ "__PYGAMEinit__", (PyCFunction) joy_autoinit, METH_NOARGS,
444
"auto initialize function for joystick" },
445
{ "init", (PyCFunction) init, METH_NOARGS, DOC_PYGAMEJOYSTICKINIT },
446
{ "quit", (PyCFunction) quit, METH_NOARGS, DOC_PYGAMEJOYSTICKQUIT },
447
{ "get_init", (PyCFunction) get_init, METH_NOARGS,
448
DOC_PYGAMEJOYSTICKGETINIT },
449
{ "get_count", (PyCFunction) get_count, METH_NOARGS,
450
DOC_PYGAMEJOYSTICKGETCOUNT },
451
{ "Joystick", Joystick, METH_VARARGS, DOC_PYGAMEJOYSTICKJOYSTICK },
452
{ NULL, NULL, 0, NULL }
455
MODINIT_DEFINE (joystick)
457
PyObject *module, *dict, *apiobj;
459
static void* c_api[PYGAMEAPI_JOYSTICK_NUMSLOTS];
462
static struct PyModuleDef _module = {
463
PyModuleDef_HEAD_INIT,
468
NULL, NULL, NULL, NULL
472
/* imported needed apis; Do this first so if there is an error
473
the module is not loaded.
475
import_pygame_base ();
476
if (PyErr_Occurred ()) {
480
/* type preparation */
481
if (PyType_Ready (&PyJoystick_Type) == -1) {
485
/* create the module */
487
module = PyModule_Create (&_module);
489
module = Py_InitModule3 ("joystick", _joystick_methods, DOC_PYGAMEJOYSTICK);
491
if (module == NULL) {
494
dict = PyModule_GetDict (module);
496
if (PyDict_SetItemString (dict, "JoystickType",
497
(PyObject *)&PyJoystick_Type) == -1) {
502
/* export the c api */
503
c_api[0] = &PyJoystick_Type;
504
c_api[1] = PyJoystick_New;
505
apiobj = encapsulate_api (c_api, "joystick");
506
if (apiobj == NULL) {
510
ecode = PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
516
MODINIT_RETURN (module);