Anonymous avatar Anonymous committed a2e7c0f

Added openal.Device skeleton.

Comments (0)

Files changed (6)

         depends = [ 'openal' ]),
     
     Module ("openal.base",
-        sources = [ "src/openal/basemod.c" ],
+        sources = [ "src/openal/basemod.c",
+                    "src/openal/device.c" ],
         depends = [ 'openal' ]),
     ]
 

src/openal/basemod.c

 */
 #define PYGAME_OPENALBASE_INTERNAL
 
-#ifdef IS_MSYS
-#include <al.h>
-#else
-#include <AL/al.h>
-#endif
-
+#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_isextensionpresent (PyObject *self, PyObject *args);
 
 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, "" },
     { NULL, NULL, 0, NULL },
 };
 
     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_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;
+    }
+    
+    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;
+}
+
+/* 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;
+    }
+}
+
 #ifdef IS_PYTHON_3
 PyMODINIT_FUNC PyInit_base (void)
 #else
     if (!mod)
         goto fail;
         
-    /* PyDevice_Type.tp_new = PyType_GenericNew; */
-    /* if (PyType_Ready (&PyDevice_Type) < 0) */
-    /*     MODINIT_RETURN(NULL); */
-
-    /*PyModule_AddObject (mod, "Device", (PyObject *) &PyDevice_Type);*/
-    
     if (import_pygame2_base () < 0)
         goto fail;
+    
+    PyDevice_Type.tp_new = PyType_GenericNew;
+    if (PyType_Ready (&PyDevice_Type) < 0)
+        goto fail;
+
+    PyModule_AddObject (mod, "Device", (PyObject *) &PyDevice_Type);
+    
     MODINIT_RETURN(mod);
 fail:
     Py_XDECREF (mod);

src/openal/device.c

+/*
+  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_OPENALDEVICE_INTERNAL
+
+#include "openalmod.h"
+#include "pgopenal.h"
+
+static int _device_init (PyObject *self, PyObject *args, PyObject *kwds);
+static void _device_dealloc (PyDevice *self);
+static PyObject* _device_repr (PyObject *self);
+
+static PyObject* _device_hasextension (PyObject *self, PyObject *args);
+
+static PyObject* _device_getname (PyObject* self, void *closure);
+/**
+ */
+static PyMethodDef _device_methods[] = {
+    { "has_extension", _device_hasextension, METH_VARARGS, NULL },
+    { NULL, NULL, 0, NULL }
+};
+
+/**
+ */
+static PyGetSetDef _device_getsets[] = {
+    { "name", _device_getname, NULL, NULL/*DOC_BASEDEVICE_NAME*/, NULL },
+    { NULL, NULL, NULL, NULL, NULL }
+};
+
+/**
+ */
+PyTypeObject PyDevice_Type =
+{
+    TYPE_HEAD(NULL, 0)
+    "base.Device",              /* tp_name */
+    sizeof (PyDevice),          /* tp_basicsize */
+    0,                          /* tp_itemsize */
+    (destructor) _device_dealloc, /* tp_dealloc */
+    0,                          /* tp_print */
+    0,                          /* tp_getattr */
+    0,                          /* tp_setattr */
+    0,                          /* tp_compare */
+    (reprfunc)_device_repr,     /* tp_repr */
+    0,                          /* tp_as_number */
+    0,                          /* tp_as_sequence */
+    0,                          /* tp_as_mapping */
+    0,                          /* tp_hash */
+    0,                          /* tp_call */
+    0,                          /* tp_str */
+    0,                          /* tp_getattro */
+    0,                          /* tp_setattro */
+    0,                          /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+    0/*DOC_BASE_DEVICE*/,
+    0,                          /* tp_traverse */
+    0,                          /* tp_clear */
+    0,                          /* tp_richcompare */
+    0,                          /* tp_weaklistoffset */
+    0,                          /* tp_iter */
+    0,                          /* tp_iternext */
+    _device_methods,            /* tp_methods */
+    0,                          /* tp_members */
+    _device_getsets,            /* tp_getset */
+    0,                          /* tp_base */
+    0,                          /* tp_dict */
+    0,                          /* tp_descr_get */
+    0,                          /* tp_descr_set */
+    0,                          /* tp_dictoffset */
+    (initproc) _device_init,    /* tp_init */
+    0,                          /* tp_alloc */
+    0,                          /* tp_new */
+    0,                          /* tp_free */
+    0,                          /* tp_is_gc */
+    0,                          /* tp_bases */
+    0,                          /* tp_mro */
+    0,                          /* tp_cache */
+    0,                          /* tp_subclasses */
+    0,                          /* tp_weaklist */
+    0,                          /* tp_del */
+#if PY_VERSION_HEX >= 0x02060000
+    0                           /* tp_version_tag */
+#endif
+};
+
+
+static void
+_device_dealloc (PyDevice *self)
+{
+    if (self->device)
+        alcCloseDevice (self->device);
+    self->device = NULL;
+    ((PyObject*)self)->ob_type->tp_free ((PyObject *) self);
+}
+
+static int
+_device_init (PyObject *self, PyObject *args, PyObject *kwds)
+{
+    char *name = NULL;
+    ALCdevice *device = NULL;
+    
+    if (!PyArg_ParseTuple (args, "|s", &name))
+        return -1;
+    
+    device = alcOpenDevice (name);
+    if (!device)
+    {
+        SetALErrorException (alGetError ());
+        return -1;
+    }
+    return 0;
+}
+
+static PyObject*
+_device_repr (PyObject *self)
+{
+    return Text_FromUTF8 ("<alcDevice>");
+}
+
+/* Device getters/setters */
+static PyObject*
+_device_getname (PyObject* self, void *closure)
+{
+    const ALCchar *name = alcGetString (PyDevice_AsDevice (self),
+        ALC_DEVICE_SPECIFIER);
+    if (!name)
+    {
+        SetALErrorException (alGetError ());
+        return NULL;
+    }
+    return Text_FromUTF8 ((const char*)name);
+}
+
+/* Device methods */
+static PyObject*
+_device_hasextension (PyObject *self, PyObject *args)
+{
+    char *extname = NULL;
+    ALCboolean present;
+    
+    if(!PyArg_ParseTuple (args, "s:has_extension", &extname))
+        return NULL;
+    
+    present = alcIsExtensionPresent (PyDevice_AsDevice (self),
+        (const ALchar*)extname);
+    if (SetALErrorException (alGetError ()))
+        return NULL;
+    if (present == ALC_FALSE)
+        Py_RETURN_FALSE;
+    Py_RETURN_TRUE;
+}
+
+/* C API */
+PyObject*
+PyDevice_New (const char* name)
+{
+    ALCdevice *dev;
+    PyObject *device = PyDevice_Type.tp_new (&PyDevice_Type, NULL, NULL);
+
+    if (!device)
+        return NULL;
+
+    dev = alcOpenDevice (name);
+    if (!dev)
+    {
+        SetALErrorException (alGetError ());
+        Py_DECREF (device);
+        return NULL;
+    }
+    ((PyDevice*)device)->device = dev;
+    return device;
+}
+
+void
+device_export_capi (void **capi)
+{
+    capi[PYGAME_OPENALDEVICE_FIRSTSLOT] = &PyDevice_Type;
+    capi[PYGAME_OPENALDEVICE_FIRSTSLOT+1] = PyDevice_New;
+}

src/openal/openalmod.h

+/*
+  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
+
+*/
+#ifndef _PYGAME_OPENALMOD_H_
+#define _PYGAME_OPENALMOD_H_
+
+#define PYGAME_OPENAL_INTERNAL
+#define PYGAME_OPENALDEVICE_INTERNAL
+
+#ifdef IS_MSYS
+#include <al.h>
+#else
+#include <AL/al.h>
+#endif
+
+#include "pgcompat.h"
+
+extern PyTypeObject PyDevice_Type;
+#define PyDevice_Check(x) (PyObject_TypeCheck (x, &PyDevice_Type))
+PyObject* PyDevice_New (const char *name);
+
+int SetALErrorException (ALenum error);
+
+void device_export_capi (void **capi);
+
+#endif /* _PYGAME_OPENALMOD_H_ */

src/openal/pgopenal.h

 #ifndef _PYGAME_OPENAL_H_
 #define _PYGAME_OPENAL_H_
 
+#ifdef IS_MSYS
+#include <al.h>
+#include <alc.h>
+#include <alext.h>
+#else
+#include <AL/al.h>
+#include <AL/alc.h>
+#include <AL/alext.h>
+#endif
+
 #include "pgcompat.h"
 #include "pgdefines.h"
 
 #ifndef PYGAME_OPENAL_INTERNAL
 #endif /* PYGAME_OPENAL_INTERNAL */
 
+typedef struct
+{
+    PyObject_HEAD
+    ALCdevice *device;
+} PyDevice;
+#define PyDevice_AsDevice(x) (((PyDevice*)x)->device)
+#define PYGAME_OPENALDEVICE_FIRSTSLOT \
+    (PYGAME_OPENAL_FIRSTSLOT + PYGAME_OPENAL_NUMSLOTS)
+#define PYGAME_OPENALDEVICE_NUMSLOTS 2
+#ifndef PYGAME_OPENALDEVICE_INTERNAL
+#define PyDevice_Type                                             \
+    (*(PyTypeObject*)PyGameOpenAL_C_API[PYGAME_OPENALDEVICE_FIRSTSLOT+0])
+#define PyDevice_Check(x)                                                 \
+    (PyObject_TypeCheck(x,                                              \
+        (PyTypeObject*)PyGameOpenAL_C_API[PYGAME_OPENALDEVICE_FIRSTSLOT+0]))
+#define PyDevice_New                                                      \
+    (*(PyObject*(*)(const char*))PyGameOpenAL_C_API[PYGAME_OPENALDEVICE_FIRSTSLOT+1])
+#endif /* PYGAME_OPENALDEVICE_INTERNAL */
+
 /**
  * C API export.
  */

test/sdl_video_test.py

         # 
         # Gets the name of the video driver or None, if the video system has
         # not been initialised or it could not be determined.
+        self.assert_ (video.get_drivername () == None)
         video.init ()
         self.assert_ (video.get_drivername () != None)
         video.quit ()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.