Source

pygame / src / openal / openalmod.c

Full commit
  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
/*
  pygame - Python Game Library
  Copyright (C) 2010 Marcus von Appen

  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

*/
#define PYGAME_OPENALBASE_INTERNAL

#include "pymacros.h"
#include "openalmod.h"
#include "pgbase.h"
#include "pgopenal.h"
/*#include "openalbase_doc.h"*/

static PyObject* _openal_init (PyObject *self);
static PyObject* _openal_quit (PyObject *self);
static PyObject* _openal_geterror (PyObject *self);
static PyObject* _openal_algetstring (PyObject *self, PyObject *args);
static PyObject* _openal_isextensionpresent (PyObject *self, PyObject *args);
static PyObject* _openal_setcurrentcontext (PyObject *self, PyObject *args);
static PyObject* _openal_listoutputdevices (PyObject *self);
static PyObject* _openal_listcapturedevices (PyObject *self);
static PyObject* _openal_getdefaultoutputdevicename (PyObject *self);
static PyObject* _openal_getdefaultcapturedevicename (PyObject *self);

static PyMethodDef _openal_methods[] = {
    { "init", (PyCFunction)_openal_init, METH_NOARGS, ""/*DOC_BASE_INIT*/ },
    { "quit", (PyCFunction)_openal_quit, METH_NOARGS, ""/*DOC_BASE_QUIT*/ },
    { "get_error", (PyCFunction)_openal_geterror, METH_NOARGS,
      ""/*DOC_BASE_GETERROR*/ },
    { "is_extension_present", _openal_isextensionpresent, METH_VARARGS, "" },
    { "list_output_devices", (PyCFunction)_openal_listoutputdevices,
      METH_NOARGS, "" },
    { "list_capture_devices", (PyCFunction)_openal_listcapturedevices,
      METH_NOARGS, "" },
    { "al_get_string", (PyCFunction)_openal_algetstring, METH_O, "" },
    { "set_current_context", (PyCFunction)_openal_setcurrentcontext,
      METH_O, "" },
    { "get_default_output_device_name",
      (PyCFunction) _openal_getdefaultoutputdevicename, METH_NOARGS, ""},
    { "get_default_capture_device_name",
      (PyCFunction) _openal_getdefaultcapturedevicename, METH_NOARGS, ""},
   
    { NULL, NULL, 0, NULL },
};

static PyObject*
_openal_init (PyObject *self)
{
    Py_RETURN_NONE;
}

static PyObject*
_openal_quit (PyObject *self)
{
    Py_RETURN_NONE;
}

static PyObject*
_openal_geterror (PyObject *self)
{
    ALenum error = alGetError ();
    switch (error)
    {
    case AL_INVALID_ENUM:
        return Text_FromUTF8 ("invalid enumeration value");
    case AL_INVALID_VALUE:
        return Text_FromUTF8 ("invalid value");
    case AL_INVALID_OPERATION:
        return Text_FromUTF8 ("invalid operation request");
    case AL_OUT_OF_MEMORY:
        return Text_FromUTF8 ("insufficient memory");
    default:
        Py_RETURN_NONE;
    }
}

static PyObject*
_openal_algetstring (PyObject *self, PyObject *args)
{
    ALenum val;
    const ALchar* retval;
    if (!IntFromObj (args, (int*) &val))
        return NULL;
    CLEAR_ERROR_STATE ();

    if (alcGetCurrentContext () == NULL)
    {
        PyErr_SetString (PyExc_PyGameError, "no context is currently used");
        return NULL;
    }

    retval = alGetString (val);
    if (!retval)
    {
        SetALErrorException (alGetError ());
        return NULL;
    }
    return Text_FromUTF8 ((const char*)retval);
}

static PyObject*
_openal_isextensionpresent (PyObject *self, PyObject *args)
{
    char *extname = NULL;
    PyObject *device = NULL;
    ALCboolean present;
    
    if(!PyArg_ParseTuple (args, "s|O", &extname, &device))
        return NULL;
    if (device && !PyDevice_Check (device))
    {
        PyErr_SetString (PyExc_TypeError, "device must be a Device");
        return NULL;
    }
    
    CLEAR_ERROR_STATE ();
    if (device)
        present = alcIsExtensionPresent (PyDevice_AsDevice (device),
            (const ALchar*) extname);
    else
        present = alIsExtensionPresent ((const ALchar*) extname);
    if (SetALErrorException (alGetError ()))
        return NULL;
    if (present == ALC_FALSE)
        Py_RETURN_FALSE;
    Py_RETURN_TRUE;
}

static PyObject*
_openal_setcurrentcontext (PyObject *self, PyObject *args)
{
    if (PyContext_Check (args))
    {
        PyErr_SetString (PyExc_TypeError, "argument must be a Context");
        return NULL;
    }
    if (alcMakeContextCurrent (PyContext_AsContext (args)) == AL_TRUE)
        Py_RETURN_TRUE;
    Py_RETURN_FALSE;
}

static PyObject*
_openal_listoutputdevices (PyObject *self)
{
    PyObject *list, *item;
    const ALCchar *dptr;
    const ALCchar *devices;

    CLEAR_ERROR_STATE ();
    if (alcIsExtensionPresent (NULL, "ALC_ENUMERATION_EXT") == AL_FALSE)
    {
        PyErr_SetString (PyExc_PyGameError, "device listing not supported");
        return NULL;
    }

    if (alcIsExtensionPresent (NULL, "ALC_ENUMERATE_ALL_EXT") == AL_TRUE)
        devices = alcGetString (NULL, ALC_ALL_DEVICES_SPECIFIER);
    else
        devices = alcGetString (NULL, ALC_DEVICE_SPECIFIER);

    if (!devices)
    {
        SetALCErrorException (alcGetError (NULL));
        return NULL;
    }

    list = PyList_New (0);
    if (!list)
        return NULL;
    dptr = devices;
    /* According to the docs, devices will be a list of strings, seperated
     * by NULL, with two consequtive NULL bytes to mark the end */
    while (devices && *dptr)
    {
        item = Text_FromUTF8 ((const char*) dptr);
        if (!item)
        {
            Py_DECREF (list);
            return NULL;
        }
        if (PyList_Append (list, item) == -1)
        {
            Py_DECREF (item);
            Py_DECREF (list);
            return NULL;
        }
        Py_DECREF (item);
        dptr += strlen (dptr) + 1;
    }
    return list;
}

static PyObject*
_openal_listcapturedevices (PyObject *self)
{
    PyObject *list, *item;
    const ALCchar *dptr;
    const ALCchar *devices;

    CLEAR_ERROR_STATE ();
    if (alcIsExtensionPresent (NULL, "ALC_ENUMERATION_EXT") == AL_FALSE)
    {
        PyErr_SetString (PyExc_PyGameError, "device listing not supported");
        return NULL;
    }

    devices = alcGetString (NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
    if (!devices)
    {
        SetALCErrorException (alcGetError (NULL));
        return NULL;
    }
    list = PyList_New (0);
    if (!list)
        return NULL;
    dptr = devices;
    /* According to the docs, devices will be a list of strings, seperated
     * by NULL, with two consequtive NULL bytes to mark the end */
    while (devices && *dptr)
    {
        item = Text_FromUTF8 ((const char*) dptr);
        if (!item)
        {
            Py_DECREF (list);
            return NULL;
        }
        if (PyList_Append (list, item) == -1)
        {
            Py_DECREF (item);
            Py_DECREF (list);
            return NULL;
        }
        Py_DECREF (item);
        dptr += strlen (dptr) + 1;
    }
    return list;
}

static PyObject*
_openal_getdefaultoutputdevicename (PyObject *self)
{
    const ALCchar *name;
    CLEAR_ERROR_STATE ();
    name = alcGetString (NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
    if (name)
        return Text_FromUTF8 ((const char*)name);
    SetALCErrorException (alcGetError (NULL));
    return NULL;
}

static PyObject*
_openal_getdefaultcapturedevicename (PyObject *self)
{
    const ALCchar *name;
    CLEAR_ERROR_STATE ();
    name = alcGetString (NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER);
    if (name)
        return Text_FromUTF8 ((const char*)name);
    SetALCErrorException (alcGetError (NULL));
    return NULL;
}

/* C API */
int
SetALErrorException (ALenum error)
{
    switch (error)
    {
    case AL_INVALID_ENUM:
        PyErr_SetString (PyExc_PyGameError, "invalid enumeration value");
        return 1;
    case AL_INVALID_VALUE:
        PyErr_SetString (PyExc_PyGameError, "invalid value");
        return 1;
    case AL_INVALID_OPERATION:
        PyErr_SetString (PyExc_PyGameError, "invalid operation request");
        return 1;
    case AL_OUT_OF_MEMORY:
        PyErr_SetString (PyExc_PyGameError, "insufficient memory");
        return 1;
    default:
        return 0;
    }
}

int
SetALCErrorException (ALCenum error)
{
    switch (error)
    {
    case ALC_INVALID_ENUM:
        PyErr_SetString (PyExc_PyGameError, "invalid enumeration value");
        return 1;
    case ALC_INVALID_VALUE:
        PyErr_SetString (PyExc_PyGameError, "invalid value");
        return 1;
    case ALC_OUT_OF_MEMORY:
        PyErr_SetString (PyExc_PyGameError, "insufficient memory");
        return 1;
    case ALC_INVALID_DEVICE:
        PyErr_SetString (PyExc_PyGameError, "invalid device");
        return 1;
    case ALC_INVALID_CONTEXT:
        PyErr_SetString (PyExc_PyGameError, "invalid context");
        return 1;
    default:
        return 0;
    }
}

PropType
GetPropTypeFromStr (char *name)
{
    size_t len;
    if (!name)
        return INVALID;

    len = strlen (name);

    if (len == 1)
    {
        if (name[0] == 'i')
            return INT;
        else if (name[0] == 'f')
            return FLOAT;
        return INVALID;
    }

    if (len == 2)
    {
        if (name[0] == 'i')
        {
            if (name[1] == 'a')
                return INTARRAY;
            if (name[1] == '3')
                return INT3;
        }
        else if (name[0] == 'f')
        {
            if (name[1] == 'a')
                return FLOATARRAY;
            if (name[1] == '3')
                return FLOAT3;
        }
    }
    return INVALID;
}


#ifdef IS_PYTHON_3
PyMODINIT_FUNC PyInit_base (void)
#else
PyMODINIT_FUNC initbase (void)
#endif
{
    PyObject *mod;
    PyObject *c_api_obj;
    static void *c_api[PYGAME_OPENAL_SLOTS];

#ifdef IS_PYTHON_3
    static struct PyModuleDef _module = {
        PyModuleDef_HEAD_INIT,
        "base",
        ""/*DOC_BASE*/,
        -1,
        _openal_methods,
        NULL, NULL, NULL, NULL
    };
    mod = PyModule_Create (&_module);
#else
    mod = Py_InitModule3 ("base", _openal_methods, ""/*DOC_BASE*/);
#endif
    if (!mod)
        goto fail;
        
    if (import_pygame2_base () < 0)
        goto fail;
    
    PyDevice_Type.tp_new = PyType_GenericNew;
    if (PyType_Ready (&PyDevice_Type) < 0)
        goto fail;
    PyCaptureDevice_Type.tp_base = &PyDevice_Type;
    if (PyType_Ready (&PyCaptureDevice_Type) < 0)
        goto fail;
    PyContext_Type.tp_new = PyType_GenericNew;
    if (PyType_Ready (&PyContext_Type) < 0)
        goto fail;
    PyBuffers_Type.tp_new = PyType_GenericNew;
    if (PyType_Ready (&PyBuffers_Type) < 0)
        goto fail;
    PySources_Type.tp_new = PyType_GenericNew;
    if (PyType_Ready (&PySources_Type) < 0)
        goto fail;
    PyListener_Type.tp_new = PyType_GenericNew;
    if (PyType_Ready (&PyListener_Type) < 0)
        goto fail;
    
    ADD_OBJ_OR_FAIL (mod, "Device", PyDevice_Type, fail);
    ADD_OBJ_OR_FAIL (mod, "CaptureDevice", PyCaptureDevice_Type, fail);
    ADD_OBJ_OR_FAIL (mod, "Context", PyContext_Type, fail);
    ADD_OBJ_OR_FAIL (mod, "Buffers", PyBuffers_Type, fail);
    ADD_OBJ_OR_FAIL (mod, "Sources", PySources_Type, fail);
    ADD_OBJ_OR_FAIL (mod, "Listener", PyListener_Type, fail);

    device_export_capi (c_api);
    capturedevice_export_capi (c_api);
    context_export_capi (c_api);
    buffers_export_capi (c_api);
    sources_export_capi (c_api);
    listener_export_capi (c_api);

    c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
    if (c_api_obj)
    {
        if (PyModule_AddObject (mod, PYGAME_OPENAL_ENTRY, c_api_obj) == -1)
        {
            Py_DECREF (c_api_obj);
            goto fail;
        }
    }

    MODINIT_RETURN(mod);
fail:
    Py_XDECREF (mod);
    MODINIT_RETURN (NULL);
}