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
27
#define JOYSTICK_MAXSTICKS 32
28
static SDL_Joystick* joystick_stickdata[JOYSTICK_MAXSTICKS] = {NULL};
31
staticforward PyTypeObject PyJoystick_Type;
32
static PyObject* PyJoystick_New(int);
33
#define PyJoystick_Check(x) ((x)->ob_type == &PyJoystick_Type)
36
static void joy_autoquit(void)
39
for(loop = 0; loop < JOYSTICK_MAXSTICKS; ++loop)
41
if(joystick_stickdata[loop])
43
SDL_JoystickClose(joystick_stickdata[loop]);
44
joystick_stickdata[loop] = NULL;
48
if(SDL_WasInit(SDL_INIT_JOYSTICK))
50
SDL_JoystickEventState(SDL_ENABLE);
51
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
55
static PyObject* joy_autoinit(PyObject* self, PyObject* arg)
57
if(!PyArg_ParseTuple(arg, ""))
60
if(!SDL_WasInit(SDL_INIT_JOYSTICK))
62
if(SDL_InitSubSystem(SDL_INIT_JOYSTICK))
63
return PyInt_FromLong(0);
64
SDL_JoystickEventState(SDL_ENABLE);
65
PyGame_RegisterQuit(joy_autoquit);
67
return PyInt_FromLong(1);
71
/*DOC*/ static char doc_quit[] =
72
/*DOC*/ "pygame.joystick.quit() -> None\n"
73
/*DOC*/ "uninitialize joystick module\n"
75
/*DOC*/ "Uninitialize the joystick module manually\n"
78
static PyObject* quit(PyObject* self, PyObject* arg)
80
if(!PyArg_ParseTuple(arg, ""))
88
/*DOC*/ static char doc_init[] =
89
/*DOC*/ "pygame.joystick.init() -> None\n"
90
/*DOC*/ "initialize joystick module\n"
92
/*DOC*/ "Initialize the joystick module manually\n"
95
static PyObject* init(PyObject* self, PyObject* arg)
100
if(!PyArg_ParseTuple(arg, ""))
103
result = joy_autoinit(self, arg);
104
istrue = PyObject_IsTrue(result);
107
return RAISE(PyExc_SDLError, SDL_GetError());
114
/*DOC*/ static char doc_get_init[] =
115
/*DOC*/ "pygame.joystick.get_init() -> bool\n"
116
/*DOC*/ "query initialization of joystick module\n"
118
/*DOC*/ "Returns true when the joystick module is initialized.\n"
121
static PyObject* get_init(PyObject* self, PyObject* arg)
123
if(!PyArg_ParseTuple(arg, ""))
126
return PyInt_FromLong(SDL_WasInit(SDL_INIT_JOYSTICK)!=0);
131
/*joystick object funcs*/
134
static void joy_dealloc(PyObject* self)
140
/*DOC*/ static char doc_Joystick[] =
141
/*DOC*/ "pygame.joystick.Joystick(id) -> Joystick\n"
142
/*DOC*/ "create new joystick object\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"
148
static PyObject* Joystick(PyObject* self, PyObject* args)
151
if(!PyArg_ParseTuple(args, "i", &id))
154
JOYSTICK_INIT_CHECK();
156
return PyJoystick_New(id);
161
/*DOC*/ static char doc_get_count[] =
162
/*DOC*/ "pygame.joystick.get_count() -> int\n"
163
/*DOC*/ "query number of joysticks on system\n"
165
/*DOC*/ "Returns the number of joysticks devices available on\n"
166
/*DOC*/ "the system.\n"
169
static PyObject* get_count(PyObject* self, PyObject* args)
171
if(!PyArg_ParseTuple(args, ""))
174
JOYSTICK_INIT_CHECK();
176
return PyInt_FromLong(SDL_NumJoysticks());
182
/*DOC*/ static char doc_joy_init[] =
183
/*DOC*/ "Joystick.init() -> None\n"
184
/*DOC*/ "initialize a joystick device for use\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"
191
static PyObject* joy_init(PyObject* self, PyObject* args)
193
int joy_id = PyJoystick_AsID(self);
195
if(!PyArg_ParseTuple(args, ""))
198
JOYSTICK_INIT_CHECK();
200
if(!joystick_stickdata[joy_id])
202
joystick_stickdata[joy_id] = SDL_JoystickOpen(joy_id);
203
if(!joystick_stickdata[joy_id])
204
return RAISE(PyExc_SDLError, SDL_GetError());
210
/*DOC*/ static char doc_joy_quit[] =
211
/*DOC*/ "Joystick.quit() -> None\n"
212
/*DOC*/ "uninitialize a joystick device for use\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"
220
static PyObject* joy_quit(PyObject* self, PyObject* args)
222
int joy_id = PyJoystick_AsID(self);
224
if(!PyArg_ParseTuple(args, ""))
227
JOYSTICK_INIT_CHECK();
229
if(joystick_stickdata[joy_id])
231
SDL_JoystickClose(joystick_stickdata[joy_id]);
232
joystick_stickdata[joy_id] = NULL;
239
/*DOC*/ static char doc_joy_get_init[] =
240
/*DOC*/ "Joystick.get_init() -> bool\n"
241
/*DOC*/ "check if joystick is initialized\n"
243
/*DOC*/ "Returns a true value if the Joystick is initialized.\n"
246
static PyObject* joy_get_init(PyObject* self, PyObject* args)
248
int joy_id = PyJoystick_AsID(self);
250
if(!PyArg_ParseTuple(args, ""))
253
return PyInt_FromLong(joystick_stickdata[joy_id] != NULL);
258
/*DOC*/ static char doc_joy_get_id[] =
259
/*DOC*/ "Joystick.get_id() -> idnum\n"
260
/*DOC*/ "get device id number for joystick\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"
268
static PyObject* joy_get_id(PyObject* self, PyObject* args)
270
int joy_id = PyJoystick_AsID(self);
272
if(!PyArg_ParseTuple(args, ""))
274
return PyInt_FromLong(joy_id);
278
/*DOC*/ static char doc_joy_get_name[] =
279
/*DOC*/ "Joystick.get_name(id) -> string\n"
280
/*DOC*/ "query name of joystick drive\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"
287
static PyObject* joy_get_name(PyObject* self, PyObject* args)
289
int joy_id = PyJoystick_AsID(self);
291
if(!PyArg_ParseTuple(args, ""))
294
JOYSTICK_INIT_CHECK();
296
return PyString_FromString(SDL_JoystickName(joy_id));
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"
305
/*DOC*/ "Returns the number of available axes on the Joystick.\n"
308
static PyObject* joy_get_numaxes(PyObject* self, PyObject* args)
310
int joy_id = PyJoystick_AsID(self);
311
SDL_Joystick* joy = joystick_stickdata[joy_id];
313
if(!PyArg_ParseTuple(args, ""))
316
JOYSTICK_INIT_CHECK();
318
return RAISE(PyExc_SDLError, "Joystick not initialized");
320
return PyInt_FromLong(SDL_JoystickNumAxes(joy));
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"
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"
336
static PyObject* joy_get_axis(PyObject* self, PyObject* args)
338
int joy_id = PyJoystick_AsID(self);
339
SDL_Joystick* joy = joystick_stickdata[joy_id];
342
if(!PyArg_ParseTuple(args, "i", &axis))
345
JOYSTICK_INIT_CHECK();
347
return RAISE(PyExc_SDLError, "Joystick not initialized");
348
if(axis < 0 || axis >= SDL_JoystickNumAxes(joy))
349
return RAISE(PyExc_SDLError, "Invalid joystick axis");
351
value = SDL_JoystickGetAxis(joy, axis);
352
return PyFloat_FromDouble(value / 32768.0);
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"
360
/*DOC*/ "Returns the number of available buttons on the Joystick.\n"
363
static PyObject* joy_get_numbuttons(PyObject* self, PyObject* args)
365
int joy_id = PyJoystick_AsID(self);
366
SDL_Joystick* joy = joystick_stickdata[joy_id];
368
if(!PyArg_ParseTuple(args, ""))
371
JOYSTICK_INIT_CHECK();
373
return RAISE(PyExc_SDLError, "Joystick not initialized");
375
return PyInt_FromLong(SDL_JoystickNumButtons(joy));
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"
384
/*DOC*/ "Returns the current state of a joystick button.\n"
387
static PyObject* joy_get_button(PyObject* self, PyObject* args)
389
int joy_id = PyJoystick_AsID(self);
390
SDL_Joystick* joy = joystick_stickdata[joy_id];
393
if(!PyArg_ParseTuple(args, "i", &index))
396
JOYSTICK_INIT_CHECK();
398
return RAISE(PyExc_SDLError, "Joystick not initialized");
399
if(index < 0 || index >= SDL_JoystickNumButtons(joy))
400
return RAISE(PyExc_SDLError, "Invalid joystick button");
402
value = SDL_JoystickGetButton(joy, index);
403
return PyInt_FromLong(value);
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"
411
/*DOC*/ "Returns the number of available trackballs on the Joystick.\n"
414
static PyObject* joy_get_numballs(PyObject* self, PyObject* args)
416
int joy_id = PyJoystick_AsID(self);
417
SDL_Joystick* joy = joystick_stickdata[joy_id];
419
if(!PyArg_ParseTuple(args, ""))
422
JOYSTICK_INIT_CHECK();
424
return RAISE(PyExc_SDLError, "Joystick not initialized");
426
return PyInt_FromLong(SDL_JoystickNumBalls(joy));
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"
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"
440
static PyObject* joy_get_ball(PyObject* self, PyObject* args)
442
int joy_id = PyJoystick_AsID(self);
443
SDL_Joystick* joy = joystick_stickdata[joy_id];
446
if(!PyArg_ParseTuple(args, "i", &index))
449
JOYSTICK_INIT_CHECK();
451
return RAISE(PyExc_SDLError, "Joystick not initialized");
452
if(index < 0 || index >= SDL_JoystickNumBalls(joy))
453
return RAISE(PyExc_SDLError, "Invalid joystick trackball");
455
SDL_JoystickGetBall(joy, index, &dx, &dy);
456
return Py_BuildValue("(ii)", dx, dy);
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"
464
/*DOC*/ "Returns the number of available directional hats on the Joystick.\n"
467
static PyObject* joy_get_numhats(PyObject* self, PyObject* args)
469
int joy_id = PyJoystick_AsID(self);
470
SDL_Joystick* joy = joystick_stickdata[joy_id];
472
if(!PyArg_ParseTuple(args, ""))
475
JOYSTICK_INIT_CHECK();
477
return RAISE(PyExc_SDLError, "Joystick not initialized");
479
return PyInt_FromLong(SDL_JoystickNumHats(joy));
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"
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"
494
static PyObject* joy_get_hat(PyObject* self, PyObject* args)
496
int joy_id = PyJoystick_AsID(self);
497
SDL_Joystick* joy = joystick_stickdata[joy_id];
501
if(!PyArg_ParseTuple(args, "i", &index))
504
JOYSTICK_INIT_CHECK();
506
return RAISE(PyExc_SDLError, "Joystick not initialized");
507
if(index < 0 || index >= SDL_JoystickNumHats(joy))
508
return RAISE(PyExc_SDLError, "Invalid joystick hat");
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;
517
return Py_BuildValue("(ii)", px, py);
522
static PyMethodDef joy_builtins[] =
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 },
528
{ "get_id", joy_get_id, 1, doc_joy_get_id },
529
{ "get_name", joy_get_name, 1, doc_joy_get_name },
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 },
543
static PyObject* joy_getattr(PyObject* self, char* attrname)
545
return Py_FindMethod(joy_builtins, self, attrname);
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"
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"
560
/*DOC*/ "Be sure to understand there is a difference between the joystick module\n"
561
/*DOC*/ "and the Joystick objects.\n"
565
static PyTypeObject PyJoystick_Type =
567
PyObject_HEAD_INIT(NULL)
570
sizeof(PyJoystickObject),
585
doc_Joystick_MODULE /* Documentation string */
590
static PyObject* PyJoystick_New(int id)
592
PyJoystickObject* joy;
594
if(id < 0 || id >= JOYSTICK_MAXSTICKS || id >= SDL_NumJoysticks())
595
return RAISE(PyExc_SDLError, "Invalid joystick device number");
597
joy = PyObject_NEW(PyJoystickObject, &PyJoystick_Type);
598
if(!joy) return NULL;
602
return (PyObject*)joy;
609
static PyMethodDef joystick_builtins[] =
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 },
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"
635
/*DOC*/ "You can call the Joystick.get_name() and Joystick.get_id() functions\n"
636
/*DOC*/ "without initializing the Joystick object.\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"
643
/*DOC*/ "Be sure to understand there is a difference between the joystick module\n"
644
/*DOC*/ "and the Joystick objects.\n"
648
void initjoystick(void)
650
PyObject *module, *dict, *apiobj;
651
static void* c_api[PYGAMEAPI_JOYSTICK_NUMSLOTS];
653
PyType_Init(PyJoystick_Type);
656
/* create the module */
657
module = Py_InitModule3("joystick", joystick_builtins, doc_pygame_joystick_MODULE);
658
dict = PyModule_GetDict(module);
660
PyDict_SetItemString(dict, "JoystickType", (PyObject *)&PyJoystick_Type);
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);
669
/*imported needed apis*/
670
import_pygame_base();