Source

pygame / src / joystick.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
    PyGame - Python Game Library
    Copyright (C) 2000  Pete Shinners

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Pete Shinners
    pete@shinners.org
*/

#define PYGAMEAPI_JOYSTICK_INTERNAL
#include "pygame.h"




staticforward PyTypeObject Joystick_Type;
static PyObject* PyJoystick_New(SDL_Joystick*);
#define PyJoystick_Check(x) ((x)->ob_type == &PyCD_Type)



static void joy_autoquit()
{
	if(SDL_WasInit(SDL_INIT_JOYSTICK))
	{
		SDL_JoystickEventState(SDL_DISABLE);
		SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
	}
}

static PyObject* joy_autoinit(PyObject* self, PyObject* arg)
{
	if(!PyArg_ParseTuple(arg, ""))
		return NULL;

	if(!SDL_WasInit(SDL_INIT_JOYSTICK))
	{
		if(SDL_InitSubSystem(SDL_INIT_JOYSTICK))
			return PyInt_FromLong(0);
		SDL_JoystickEventState(SDL_ENABLE);
		PyGame_RegisterQuit(cdrom_autoquit);
	}
	return PyInt_FromLong(1);
}


    /*DOC*/ static char doc_joy_quit[] =
    /*DOC*/    "pygame.joystick.quit() -> None\n"
    /*DOC*/    "uninitialize cdrom module\n"
    /*DOC*/    "\n"
    /*DOC*/    "Uninitialize the joystick module manually\n"
    /*DOC*/ ;

static PyObject* joy_quit(PyObject* self, PyObject* arg)
{
	if(!PyArg_ParseTuple(arg, ""))
		return NULL;

	joy_autoquit();

	RETURN_NONE
}

    /*DOC*/ static char doc_joy_init[] =
    /*DOC*/    "pygame.joystick.init() -> None\n"
    /*DOC*/    "initialize cdrom module\n"
    /*DOC*/    "\n"
    /*DOC*/    "Initialize the joystick module manually\n"
    /*DOC*/ ;

static PyObject* joy_init(PyObject* self, PyObject* arg)
{
	PyObject* result;
	int istrue;

	if(!PyArg_ParseTuple(arg, ""))
		return NULL;

	result = joy_autoinit(self, arg);
	istrue = PyObject_IsTrue(result);
	Py_DECREF(result);
	if(!istrue)
		return RAISE(PyExc_SDLError, SDL_GetError());

	RETURN_NONE
}



    /*DOC*/ static char doc_get_init[] =
    /*DOC*/    "pygame.joystick.get_init() -> bool\n"
    /*DOC*/    "query initialization of joystick module\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns true when the joystick module is\n"
    /*DOC*/    "initialized.\n"
    /*DOC*/ ;

static PyObject* get_init(PyObject* self, PyObject* arg)
{
	if(!PyArg_ParseTuple(arg, ""))
		return NULL;

	return PyInt_FromLong(SDL_WasInit(SDL_INIT_JOYSTICK)!=0);
}



/*joystick object funcs*/


static void joy_dealloc(PyObject* self)
{
	PyJoystickObject* joy_ref = (PyJoystickObject*)self;

	if(SDL_WasInit(SDL_INIT_JOYSTICK)
		SDL_JoystickClose(joy_ref->joy);

	PyMem_DEL(self);
}


    /*DOC*/ static char doc_joy_get_id[] =
    /*DOC*/    "Joystick.get_id() -> id\n"
    /*DOC*/    "query id of joystick\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the joystick id number for the Joystick\n"
    /*DOC*/ ;

static PyObject* joy_get_id(PyObject* self, PyObject* args)
{
	PyJoystickObject* joy_ref = (PyJoystickObject*)self;

	if(!PyArg_ParseTuple(args, ""))
		return NULL;

	return PyInt_FromLong(SDL_JoystickIndex(joy_ref->joy));
}



    /*DOC*/ static char doc_joy_get_axes[] =
    /*DOC*/    "Joystick.get_axes() -> count\n"
    /*DOC*/    "query number of axis\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the number of axis on this Joystick\n"
    /*DOC*/ ;

static PyObject* joy_get_axes(PyObject* self, PyObject* args)
{
	PyJoystickObject* joy_ref = (PyJoystickObject*)self;

	if(!PyArg_ParseTuple(args, ""))
		return NULL;

	return PyInt_FromLong(SDL_JoystickNumAxes(joy_ref->joy));
}



    /*DOC*/ static char doc_joy_get_balls[] =
    /*DOC*/    "Joystick.get_balls() -> count\n"
    /*DOC*/    "query number of balls\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns number of trackballs on this Joystick\n"
    /*DOC*/ ;

static PyObject* joy_get_balls(PyObject* self, PyObject* args)
{
	PyJoystickObject* joy_ref = (PyJoystickObject*)self;

	if(!PyArg_ParseTuple(args, ""))
		return NULL;

	return PyInt_FromLong(SDL_JoystickNumBalls(joy_ref->joy));
}



    /*DOC*/ static char doc_joy_get_hats[] =
    /*DOC*/    "Joystick.get_hats() -> count\n"
    /*DOC*/    "query number of hats\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns number of directional hats on this\n"
    /*DOC*/    "Joystick\n"
    /*DOC*/ ;

static PyObject* joy_get_hats(PyObject* self, PyObject* args)
{
	PyJoystickObject* joy_ref = (PyJoystickObject*)self;

	if(!PyArg_ParseTuple(args, ""))
		return NULL;

	return PyInt_FromLong(SDL_JoystickNumHats(joy_ref->joy));
}



    /*DOC*/ static char doc_joy_get_buttons[] =
    /*DOC*/    "Joystick.get_buttons() -> count\n"
    /*DOC*/    "query number of buttons\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns number of pushable buttons on this\n"
    /*DOC*/    "Joystick\n"
    /*DOC*/ ;

static PyObject* joy_get_buttons(PyObject* self, PyObject* args)
{
	PyJoystickObject* joy_ref = (PyJoystickObject*)self;

	if(!PyArg_ParseTuple(args, ""))
		return NULL;

	return PyInt_FromLong(SDL_JoystickNumButtons(joy_ref->joy));
}



    /*DOC*/ static char doc_joy_get_axis[] =
    /*DOC*/    "Joystick.get_axis(axis) -> position\n"
    /*DOC*/    "query axis of a joystick\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the current position of an axis control on\n"
    /*DOC*/    "the Joystick. Value is in the range -1.0 to 1.0.\n"
    /*DOC*/ ;

static PyObject* joy_get_axis(PyObject* self, PyObject* args)
{
	PyJoystickObject* joy_ref = (PyJoystickObject*)self;
	int axis;

	if(!PyArg_ParseTuple(args, "i", &axis))
		return NULL;

	return PyFloat_FromDouble(SDL_JoystickGetAxis(joy_ref->joy, axis)/32767.0);
}



    /*DOC*/ static char doc_joy_get_hat[] =
    /*DOC*/    "Joystick.get_hat(pov_hat) -> state\n"
    /*DOC*/    "query position of hat\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the current position of a directional hat\n"
    /*DOC*/    "on the Joystick. Value in a position on the\n"
    /*DOC*/    "following compass. (think 1 is up, and goes around\n"
    /*DOC*/    "clockwise)\n"
    /*DOC*/    "8 1 2\n"
    /*DOC*/    "7 0 3\n"
    /*DOC*/    "6 5 4\n"
    /*DOC*/ ;

static PyObject* joy_get_hat(PyObject* self, PyObject* args)
{
	PyJoystickObject* joy_ref = (PyJoystickObject*)self;
	int hat;

	if(!PyArg_ParseTuple(args, "i", &hat))
		return NULL;

	return PyInt_FromLong(SDL_JoystickGetHat(joy_ref->joy, hat));
}



    /*DOC*/ static char doc_joy_get_button[] =
    /*DOC*/    "Joystick.get_button(button) -> bool\n"
    /*DOC*/    "query state of button\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns true if the given Joystick button is being\n"
    /*DOC*/    "pressed.\n"
    /*DOC*/ ;

static PyObject* joy_get_button(PyObject* self, PyObject* args)
{
	PyJoystickObject* joy_ref = (PyJoystickObject*)self;
	int button;

	if(!PyArg_ParseTuple(args, "i", &button))
		return NULL;

	return PyInt_FromLong(SDL_JoystickGetButton(joy_ref->joy, button));
}



/*joystick module funcs*/

static PyObject* PyJoystick_New(SDL_Joystick* joy)
{
	PyJoystickObject* joyobj;

	if(!joy)
		return RAISE(PyExc_SDLError, SDL_GetError());

	joyobj = PyObject_NEW(PyJoystickObject, &Joystick_Type);
	if(!joyobj)
		return NULL;

	joyobj->joy = joy;

	return (PyObject*)joyobj;
}



    /*DOC*/ static char doc_joy_open[] =
    /*DOC*/    "pygame.joystick.open(id) -> Joystick\n"
    /*DOC*/    "return new joystick object\n"
    /*DOC*/    "\n"
    /*DOC*/    "Creates a new joystick object for the given\n"
    /*DOC*/    "joystick id. Once a joystick has been opened, it\n"
    /*DOC*/    "will start receiving joystick events on the event\n"
    /*DOC*/    "queue.\n"
    /*DOC*/ ;

static PyObject* joy_open(PyObject* self, PyObject* args)
{
	int id;

	if(!PyArg_ParseTuple(args, "i", &id))
		return NULL;

	JOY_INIT_CHECK

	return PyJoystick_New(SDL_JoystickOpen(id));
}



    /*DOC*/ static char doc_joy_get_count[] =
    /*DOC*/    "pygame.joystick.get_count() -> int\n"
    /*DOC*/    "number of joysticks in system\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the number of joysticks available on the\n"
    /*DOC*/    "system. Will be 0 if there are no joysticks.\n"
    /*DOC*/ ;

static PyObject* joy_get_count(PyObject* self, PyObject* args)
{
	if(!PyArg_ParseTuple(args, ""))
		return NULL;

	JOY_INIT_CHECK

	return PyInt_FromLong(SDL_NumJoysticks());
}



    /*DOC*/ static char doc_joy_get_name[] =
    /*DOC*/    "pygame.joystick.get_name(id) -> string\n"
    /*DOC*/    "system name for joystick\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns a readable name for the joystick device,\n"
    /*DOC*/    "given by the system.\n"
    /*DOC*/ ;

static PyObject* joy_get_name(PyObject* self, PyObject* args)
{
	int id;

	if(!PyArg_ParseTuple(args, "i", &id))
		return NULL;

	JOY_INIT_CHECK

	return PyString_FromString(SDL_JoystickName(id));
}



    /*DOC*/ static char doc_joy_is_opened[] =
    /*DOC*/    "pygame.joystick.is_opened(id) -> bool\n"
    /*DOC*/    "query opened joystick\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns true if the given joystick id has been\n"
    /*DOC*/    "previously opened.\n"
    /*DOC*/ ;

static PyObject* joy_is_opened(PyObject* self, PyObject* args)
{
	int id;
	
	if(!PyArg_ParseTuple(args, "i", &id))
		return NULL;

	JOY_INIT_CHECK

	return PyInt_FromLong(SDL_JoystickOpened(id));
}




static PyMethodDef joy__builtins__[] =
{
	{ "get_id", joy_get_id, 1, joy_get_id },
	{ "get_axes", joy_get_axes, 1, joy_get_axes },
	{ "get_balls", joy_get_balls, 1, joy_get_balls },
	{ "get_hats", joy_get_hats, 1, joy_get_hats },
	{ "get_buttons", joy_get_buttons, 1, joy_get_buttons },
	{ "get_axis", joy_get_axis, 1, joy_get_axis },
	{ "get_hat", joy_get_hat, 1, joy_get_hat },
	{ "get_button", joy_get_button, 1, joy_get_button },
	{ NULL, NULL }
};

static PyObject* joy_getattr(PyObject* self, char* attrname)
{
	if(_pysdl_sdl_is_initialized && _pysdl_joy_is_initialized)
		return Py_FindMethod(joy__builtins__, self, attrname);

	PyErr_SetString(PyExc_NameError,	attrname);
	return NULL;
}


    /*DOC*/ static char doc_Joystick_MODULE[] =
    /*DOC*/    "Thin object wrapper around the SDL joystick\n"
    /*DOC*/    "interface. Likely to be changed.\n"
    /*DOC*/ ;

static PyTypeObject Joystick_Type =
{
	PyObject_HEAD_INIT(NULL)
	0,
	"Joystick",
	sizeof(PyJoystickObject),
	0,
	joy_dealloc,
	0,
	joy_getattr
};


static PyMethodDef joystick_builtins[] =
{
	{ "__PYGAMEinit__", joy_autoinit, 1, joy_init_doc },
	{ "init", joy_init, 1, joy_init_doc },
	{ "quit", joy_quit, 1, joy_quit_doc },
	{ "get_count", joy_get_count, 1, joy_get_count },
	{ "get_name", joy_get_name, 1, joy_get_name },
	{ "open", joy_open, 1, joy_open },
	{ "is_opened", joy_is_opened, 1, joy_is_opened },
	{ "update", joy_update, 1, joy_update },
	{ "event_state", joy_event_state, 1, joy_event_state },
	{ NULL, NULL }
};


    /*DOC*/ static char doc_pygame_joystick_MODULE[] =
    /*DOC*/    "Thin wrapper around the SDL joystick interface.\n"
    /*DOC*/    "Likely to be changed.\n"
    /*DOC*/ ;

void initcdrom()
{
	PyObject *module, *dict, *apiobj;
	static void* c_api[PYGAMEAPI_CDROM_NUMSLOTS];

	PyType_Init(PyCD_Type);


    /* create the module */
	module = Py_InitModule3("joystick", joystick_builtins, doc_pygame_joystick_MODULE);
	dict = PyModule_GetDict(module);

	/* export the c api */
	c_api[0] = &PyJoystick_Type;
	c_api[1] = PyJoystick_New;
	apiobj = PyCObject_FromVoidPtr(c_api, NULL);
	PyDict_SetItemString(dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);

	/*imported needed apis*/
	import_pygame_base();
}