~pygame/pygame/trunk

« back to all changes in this revision

Viewing changes to src/joystick.c

  • Committer: pygame
  • Date: 2017-01-10 00:31:42 UTC
  • Revision ID: git-v1:2eea4f299a2e791f884608d7ed601558634af73c
commit 1639c41a8cb3433046882ede92c80ce69d59016b
Author: Thomas Kluyver <takowl@gmail.com>
Date:   Sun Jan 8 18:46:46 2017 +0000

    Build newer versions of libogg and libvorbis into Linux base images

    Closes #317
    Closes #323

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
#include "pgcompat.h"
 
26
#include "doc/joystick_doc.h"
 
27
 
 
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)
 
33
 
 
34
static void
 
35
joy_autoquit (void)
 
36
{
 
37
    int loop;
 
38
    for (loop = 0; loop < JOYSTICK_MAXSTICKS; ++loop) {
 
39
        if (joystick_stickdata[loop]) {
 
40
            SDL_JoystickClose (joystick_stickdata[loop]);
 
41
            joystick_stickdata[loop] = NULL;
 
42
        }
 
43
    }
 
44
 
 
45
    if (SDL_WasInit (SDL_INIT_JOYSTICK)) {
 
46
        SDL_JoystickEventState (SDL_ENABLE);
 
47
        SDL_QuitSubSystem (SDL_INIT_JOYSTICK);
 
48
    }
 
49
}
 
50
 
 
51
static PyObject*
 
52
joy_autoinit (PyObject* self)
 
53
{
 
54
    if (!SDL_WasInit (SDL_INIT_JOYSTICK)) {
 
55
        if (SDL_InitSubSystem (SDL_INIT_JOYSTICK)) {
 
56
            return PyInt_FromLong (0);
 
57
        }
 
58
        SDL_JoystickEventState (SDL_ENABLE);
 
59
        PyGame_RegisterQuit (joy_autoquit);
 
60
    }
 
61
    return PyInt_FromLong (1);
 
62
}
 
63
 
 
64
static PyObject*
 
65
quit (PyObject* self)
 
66
{
 
67
    joy_autoquit ();
 
68
    Py_RETURN_NONE;
 
69
}
 
70
 
 
71
static PyObject*
 
72
init (PyObject* self)
 
73
{
 
74
    PyObject* result;
 
75
    int istrue;
 
76
 
 
77
    result = joy_autoinit (self);
 
78
    istrue = PyObject_IsTrue (result);
 
79
    Py_DECREF (result);
 
80
    if (!istrue) {
 
81
        return RAISE (PyExc_SDLError, SDL_GetError ());
 
82
    }
 
83
    Py_RETURN_NONE;
 
84
}
 
85
 
 
86
static PyObject*
 
87
get_init (PyObject* self)
 
88
{
 
89
    return PyInt_FromLong (SDL_WasInit (SDL_INIT_JOYSTICK) != 0);
 
90
}
 
91
 
 
92
/*joystick object funcs*/
 
93
static void
 
94
joy_dealloc (PyObject* self)
 
95
{
 
96
    PyObject_DEL (self);
 
97
}
 
98
 
 
99
static PyObject*
 
100
Joystick (PyObject* self, PyObject* args)
 
101
{
 
102
    int id;
 
103
    if (!PyArg_ParseTuple (args, "i", &id)) {
 
104
        return NULL;
 
105
    }
 
106
 
 
107
    JOYSTICK_INIT_CHECK ();
 
108
 
 
109
    return PyJoystick_New (id);
 
110
}
 
111
 
 
112
static PyObject*
 
113
get_count (PyObject* self)
 
114
{
 
115
    JOYSTICK_INIT_CHECK ();
 
116
    return PyInt_FromLong (SDL_NumJoysticks ());
 
117
}
 
118
 
 
119
static PyObject*
 
120
joy_init (PyObject* self)
 
121
{
 
122
    int joy_id = PyJoystick_AsID (self);
 
123
 
 
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 ());
 
129
        }
 
130
    }
 
131
    Py_RETURN_NONE;
 
132
}
 
133
 
 
134
static PyObject*
 
135
joy_quit (PyObject* self)
 
136
{
 
137
    int joy_id = PyJoystick_AsID (self);
 
138
 
 
139
    JOYSTICK_INIT_CHECK ();
 
140
 
 
141
    if (joystick_stickdata[joy_id]) {
 
142
        SDL_JoystickClose (joystick_stickdata[joy_id]);
 
143
        joystick_stickdata[joy_id] = NULL;
 
144
    }
 
145
    Py_RETURN_NONE;
 
146
}
 
147
 
 
148
static PyObject*
 
149
joy_get_init (PyObject* self)
 
150
{
 
151
    int joy_id = PyJoystick_AsID (self);
 
152
    return PyInt_FromLong (joystick_stickdata[joy_id] != NULL);
 
153
}
 
154
 
 
155
static PyObject*
 
156
joy_get_id (PyObject* self)
 
157
{
 
158
    int joy_id = PyJoystick_AsID (self);
 
159
    return PyInt_FromLong (joy_id);
 
160
}
 
161
 
 
162
static PyObject*
 
163
joy_get_name (PyObject* self)
 
164
{
 
165
    int joy_id = PyJoystick_AsID (self);
 
166
    JOYSTICK_INIT_CHECK ();
 
167
    return Text_FromUTF8 (SDL_JoystickName (joy_id));
 
168
}
 
169
 
 
170
static PyObject*
 
171
joy_get_numaxes (PyObject* self)
 
172
{
 
173
    int joy_id = PyJoystick_AsID (self);
 
174
    SDL_Joystick* joy = joystick_stickdata[joy_id];
 
175
 
 
176
    JOYSTICK_INIT_CHECK ();
 
177
    if (!joy) {
 
178
        return RAISE (PyExc_SDLError, "Joystick not initialized");
 
179
    }
 
180
 
 
181
    return PyInt_FromLong (SDL_JoystickNumAxes (joy));
 
182
}
 
183
 
 
184
static PyObject*
 
185
joy_get_axis (PyObject* self, PyObject* args)
 
186
{
 
187
    int joy_id = PyJoystick_AsID (self);
 
188
    SDL_Joystick* joy = joystick_stickdata[joy_id];
 
189
    int axis, value;
 
190
 
 
191
    if (!PyArg_ParseTuple (args, "i", &axis)) {
 
192
        return NULL;
 
193
    }
 
194
 
 
195
    JOYSTICK_INIT_CHECK ();
 
196
    if (!joy) {
 
197
        return RAISE (PyExc_SDLError, "Joystick not initialized");
 
198
    }
 
199
    if (axis < 0 || axis >= SDL_JoystickNumAxes (joy)) {
 
200
        return RAISE (PyExc_SDLError, "Invalid joystick axis");
 
201
    }
 
202
 
 
203
    value = SDL_JoystickGetAxis (joy, axis);
 
204
#ifdef DEBUG
 
205
    /*printf("SDL_JoystickGetAxis value:%d:\n", value);*/
 
206
#endif
 
207
 
 
208
    return PyFloat_FromDouble (value / 32768.0);
 
209
}
 
210
 
 
211
static PyObject*
 
212
joy_get_numbuttons (PyObject* self)
 
213
{
 
214
    int joy_id = PyJoystick_AsID (self);
 
215
    SDL_Joystick* joy = joystick_stickdata[joy_id];
 
216
 
 
217
    JOYSTICK_INIT_CHECK ();
 
218
    if (!joy) {
 
219
        return RAISE (PyExc_SDLError, "Joystick not initialized");
 
220
    }
 
221
 
 
222
    return PyInt_FromLong (SDL_JoystickNumButtons (joy));
 
223
}
 
224
 
 
225
static PyObject*
 
226
joy_get_button (PyObject* self, PyObject* args)
 
227
{
 
228
    int joy_id = PyJoystick_AsID (self);
 
229
    SDL_Joystick* joy = joystick_stickdata[joy_id];
 
230
    int _index, value;
 
231
 
 
232
    if (!PyArg_ParseTuple (args, "i", &_index)) {
 
233
        return NULL;
 
234
    }
 
235
 
 
236
    JOYSTICK_INIT_CHECK ();
 
237
    if (!joy) {
 
238
        return RAISE (PyExc_SDLError, "Joystick not initialized");
 
239
    }
 
240
    if (_index < 0 || _index >= SDL_JoystickNumButtons (joy)) {
 
241
        return RAISE (PyExc_SDLError, "Invalid joystick button");
 
242
    }
 
243
 
 
244
    value = SDL_JoystickGetButton (joy, _index);
 
245
#ifdef DEBUG
 
246
    /*printf("SDL_JoystickGetButton value:%d:\n", value);*/
 
247
#endif
 
248
    return PyInt_FromLong (value);
 
249
}
 
250
 
 
251
static PyObject*
 
252
joy_get_numballs (PyObject* self)
 
253
{
 
254
    int joy_id = PyJoystick_AsID (self);
 
255
    SDL_Joystick* joy = joystick_stickdata[joy_id];
 
256
 
 
257
    JOYSTICK_INIT_CHECK ();
 
258
    if (!joy) {
 
259
        return RAISE (PyExc_SDLError, "Joystick not initialized");
 
260
    }
 
261
 
 
262
    return PyInt_FromLong (SDL_JoystickNumBalls (joy));
 
263
}
 
264
 
 
265
static PyObject*
 
266
joy_get_ball (PyObject* self, PyObject* args)
 
267
{
 
268
    int joy_id = PyJoystick_AsID (self);
 
269
    SDL_Joystick* joy = joystick_stickdata[joy_id];
 
270
    int _index, dx, dy;
 
271
    Uint32 value;
 
272
 
 
273
    if (!PyArg_ParseTuple (args, "i", &_index)) {
 
274
        return NULL;
 
275
    }
 
276
 
 
277
    JOYSTICK_INIT_CHECK ();
 
278
    if (!joy) {
 
279
        return RAISE (PyExc_SDLError, "Joystick not initialized");
 
280
    }
 
281
    value = SDL_JoystickNumBalls (joy);
 
282
#ifdef DEBUG
 
283
    /*printf("SDL_JoystickNumBalls value:%d:\n", value);*/
 
284
#endif
 
285
    if (_index < 0 || _index >= value) {
 
286
        return RAISE (PyExc_SDLError, "Invalid joystick trackball");
 
287
    }
 
288
 
 
289
    SDL_JoystickGetBall (joy, _index, &dx, &dy);
 
290
    return Py_BuildValue ("(ii)", dx, dy);
 
291
}
 
292
 
 
293
static PyObject*
 
294
joy_get_numhats (PyObject* self)
 
295
{
 
296
    int joy_id = PyJoystick_AsID (self);
 
297
    Uint32 value;
 
298
    SDL_Joystick* joy = joystick_stickdata[joy_id];
 
299
 
 
300
    JOYSTICK_INIT_CHECK ();
 
301
    if (!joy) {
 
302
        return RAISE (PyExc_SDLError, "Joystick not initialized");
 
303
    }
 
304
 
 
305
    value = SDL_JoystickNumHats (joy);
 
306
#ifdef DEBUG
 
307
    /*printf("SDL_JoystickNumHats value:%d:\n", value);*/
 
308
#endif
 
309
    return PyInt_FromLong (value);
 
310
}
 
311
 
 
312
static PyObject*
 
313
joy_get_hat (PyObject* self, PyObject* args)
 
314
{
 
315
    int joy_id = PyJoystick_AsID (self);
 
316
    SDL_Joystick* joy = joystick_stickdata[joy_id];
 
317
    int _index, px, py;
 
318
    Uint32 value;
 
319
 
 
320
    if (!PyArg_ParseTuple (args, "i", &_index)) {
 
321
        return NULL;
 
322
    }
 
323
 
 
324
    JOYSTICK_INIT_CHECK ();
 
325
    if (!joy) {
 
326
        return RAISE (PyExc_SDLError, "Joystick not initialized");
 
327
    }
 
328
    if (_index < 0 || _index >= SDL_JoystickNumHats (joy)) {
 
329
        return RAISE(PyExc_SDLError, "Invalid joystick hat");
 
330
    }
 
331
 
 
332
    px = py = 0;
 
333
    value = SDL_JoystickGetHat (joy, _index);
 
334
#ifdef DEBUG
 
335
    /*printf("SDL_JoystickGetHat value:%d:\n", value);*/
 
336
#endif
 
337
    if (value & SDL_HAT_UP) {
 
338
        py = 1;
 
339
    }
 
340
    else if (value & SDL_HAT_DOWN) {
 
341
        py = -1;
 
342
    }
 
343
    if (value & SDL_HAT_RIGHT) {
 
344
        px = 1;
 
345
    }
 
346
    else if (value & SDL_HAT_LEFT) {
 
347
        px = -1;
 
348
    }
 
349
 
 
350
    return Py_BuildValue ("(ii)", px, py);
 
351
}
 
352
 
 
353
static PyMethodDef joy_methods[] =
 
354
{
 
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 },
 
359
 
 
360
    { "get_id", (PyCFunction) joy_get_id, METH_NOARGS, DOC_JOYSTICKGETID },
 
361
    { "get_name", (PyCFunction) joy_get_name, METH_NOARGS,
 
362
      DOC_JOYSTICKGETNAME },
 
363
 
 
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 },
 
376
 
 
377
    { NULL, NULL, 0, NULL }
 
378
};
 
379
 
 
380
static PyTypeObject PyJoystick_Type =
 
381
{
 
382
    TYPE_HEAD (NULL, 0)
 
383
    "Joystick",                 /* name */
 
384
    sizeof(PyJoystickObject),   /* basic size */
 
385
    0,                          /* itemsize */
 
386
    joy_dealloc,                /* dealloc */
 
387
    0,                          /* print */
 
388
    0,                          /* getattr */
 
389
    0,                          /* setattr */
 
390
    0,                          /* compare */
 
391
    0,                          /* repr */
 
392
    0,                          /* as_number */
 
393
    0,                          /* as_sequence */
 
394
    0,                          /* as_mapping */
 
395
    0,                          /* hash */
 
396
    0,                          /* call */
 
397
    0,                          /* str */
 
398
    0,                          /* tp_getattro */
 
399
    0,                          /* tp_setattro */
 
400
    0,                          /* tp_as_buffer */
 
401
    0,                          /* flags */
 
402
    DOC_PYGAMEJOYSTICKJOYSTICK, /* Documentation string */
 
403
    0,                          /* tp_traverse */
 
404
    0,                          /* tp_clear */
 
405
    0,                          /* tp_richcompare */
 
406
    0,                          /* tp_weaklistoffset */
 
407
    0,                          /* tp_iter */
 
408
    0,                          /* tp_iternext */
 
409
    joy_methods,                /* tp_methods */
 
410
    0,                          /* tp_members */
 
411
    0,                          /* tp_getset */
 
412
    0,                          /* tp_base */
 
413
    0,                          /* tp_dict */
 
414
    0,                          /* tp_descr_get */
 
415
    0,                          /* tp_descr_set */
 
416
    0,                          /* tp_dictoffset */
 
417
    0,                          /* tp_init */
 
418
    0,                          /* tp_alloc */
 
419
    0,                          /* tp_new */
 
420
};
 
421
 
 
422
static PyObject*
 
423
PyJoystick_New (int id)
 
424
{
 
425
    PyJoystickObject* joy;
 
426
 
 
427
    if (id < 0 || id >= JOYSTICK_MAXSTICKS || id >= SDL_NumJoysticks ()) {
 
428
        return RAISE (PyExc_SDLError, "Invalid joystick device number");
 
429
    }
 
430
 
 
431
    joy = PyObject_NEW (PyJoystickObject, &PyJoystick_Type);
 
432
    if (!joy) {
 
433
        return NULL;
 
434
    }
 
435
 
 
436
    joy->id = id;
 
437
 
 
438
    return (PyObject*)joy;
 
439
}
 
440
 
 
441
static PyMethodDef _joystick_methods[] =
 
442
{
 
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 }
 
453
};
 
454
 
 
455
MODINIT_DEFINE (joystick)
 
456
{
 
457
    PyObject *module, *dict, *apiobj;
 
458
    int ecode;
 
459
    static void* c_api[PYGAMEAPI_JOYSTICK_NUMSLOTS];
 
460
 
 
461
#if PY3
 
462
    static struct PyModuleDef _module = {
 
463
        PyModuleDef_HEAD_INIT,
 
464
        "joystick",
 
465
        DOC_PYGAMEJOYSTICK,
 
466
        -1,
 
467
        _joystick_methods,
 
468
        NULL, NULL, NULL, NULL
 
469
    };
 
470
#endif
 
471
 
 
472
    /* imported needed apis; Do this first so if there is an error
 
473
       the module is not loaded.
 
474
    */
 
475
    import_pygame_base ();
 
476
    if (PyErr_Occurred ()) {
 
477
        MODINIT_ERROR;
 
478
    }
 
479
 
 
480
    /* type preparation */
 
481
    if (PyType_Ready (&PyJoystick_Type) == -1) {
 
482
        MODINIT_ERROR;
 
483
    }
 
484
 
 
485
    /* create the module */
 
486
#if PY3
 
487
    module = PyModule_Create (&_module);
 
488
#else
 
489
    module = Py_InitModule3 ("joystick", _joystick_methods, DOC_PYGAMEJOYSTICK);
 
490
#endif
 
491
    if (module == NULL) {
 
492
        MODINIT_ERROR;
 
493
    }
 
494
    dict = PyModule_GetDict (module);
 
495
 
 
496
    if (PyDict_SetItemString (dict, "JoystickType",
 
497
                              (PyObject *)&PyJoystick_Type) == -1) {
 
498
        DECREF_MOD (module);
 
499
        MODINIT_ERROR;
 
500
    }
 
501
 
 
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) {
 
507
        DECREF_MOD (module);
 
508
        MODINIT_ERROR;
 
509
    }
 
510
    ecode = PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
 
511
    Py_DECREF (apiobj);
 
512
    if (ecode == -1) {
 
513
        DECREF_MOD (module);
 
514
        MODINIT_ERROR;
 
515
    }
 
516
    MODINIT_RETURN (module);
 
517
}