Back to index

gcompris  8.2.2
py-mod-sound.c
Go to the documentation of this file.
00001 #include <Python.h>
00002 #include <pygobject.h>
00003 #include <stdarg.h>
00004 #include "gcompris/gcompris.h"
00005 #include "py-mod-sound.h"
00006 #include "py-gcompris-board.h"
00007 
00008 /* All functions provided by this python module
00009  * wraps a gcompris function. Each "py_*" function wraps the
00010  * "*" C function.
00011  */
00012 
00013 /*void gc_sound_play_ogg_list( GList* files ); */
00014 static PyObject*
00015 py_gc_sound_play_ogg_list(PyObject* self, PyObject* args)
00016 {
00017   GList* list;
00018   PyObject* pylist;
00019   /* Parse arguments */
00020   if(!PyArg_ParseTuple(args, "O:gc_sound_play_ogg_list", &pylist))
00021     return NULL;
00022   list = (GList*) pygobject_get(pylist);
00023 
00024   /* Call the corresponding C function */
00025   gc_sound_play_ogg_list(list);
00026 
00027   /* Create and return the result */
00028   Py_INCREF(Py_None);
00029   return Py_None;
00030 }
00031 
00032 
00033 
00034 /* void gc_sound_play_ogg(char *, ...);
00035    The method: gc_sound_play_ogg_list( GList* ) is used
00036    to perform the core call. (Because there's no way to construct
00037    a variable argument function call.
00038 */
00039 
00040 static PyObject*
00041 py_gc_sound_play_ogg(PyObject* self, PyObject* args)
00042 {
00043   PyObject* item;
00044   GList* list = NULL;
00045   int i, length;
00046 
00047   /* Parse arguments */
00048   length = PyTuple_Size(args);
00049   if(length<1) return NULL;
00050   for(i=0; i<length; i++){
00051     item = PyTuple_GetItem(args, i);
00052     list = g_list_append(list, PyString_AsString(item));
00053   }
00054   
00055   /* Call the corresponding C function */
00056   gc_sound_play_ogg_list(list);
00057   g_list_free(list);
00058     
00059   /* Create and return the result */
00060   Py_INCREF(Py_None);
00061   return Py_None;
00062 }
00063 
00064 
00065 
00066 static PyObject*
00067 py_gc_sound_reopen(PyObject* self, PyObject* args)
00068 {
00069   /* Parse arguments */
00070   if(!PyArg_ParseTuple(args, ":gcompris.reopen_sound"))
00071     return NULL;
00072 
00073   /* Call the corresponding C function */
00074   gc_sound_reopen();
00075 
00076   /* Create and return the result */
00077   Py_INCREF(Py_None);
00078   return Py_None;
00079 }
00080 
00081 static PyObject*
00082 py_gc_sound_close(PyObject* self, PyObject* args)
00083 {
00084   /* Parse arguments */
00085   if(!PyArg_ParseTuple(args, ":gcompris.close_sound"))
00086     return NULL;
00087 
00088   /* Call the corresponding C function */
00089   gc_sound_close();
00090 
00091   /* Create and return the result */
00092   Py_INCREF(Py_None);
00093   return Py_None;
00094 }
00095 
00096 static PyObject*
00097 py_gc_sound_pause(PyObject* self, PyObject* args)
00098 {
00099   /* Parse arguments */
00100   if(!PyArg_ParseTuple(args, ":gcompris.pause_sound"))
00101     return NULL;
00102 
00103   /* Call the corresponding C function */
00104   gc_sound_pause();
00105 
00106   /* Create and return the result */
00107   Py_INCREF(Py_None);
00108   return Py_None;
00109 }
00110 
00111 static PyObject*
00112 py_gc_sound_resume(PyObject* self, PyObject* args)
00113 {
00114   /* Parse arguments */
00115   if(!PyArg_ParseTuple(args, ":gcompris.sound.resume"))
00116     return NULL;
00117 
00118   /* Call the corresponding C function */
00119   gc_sound_resume();
00120 
00121   /* Create and return the result */
00122   Py_INCREF(Py_None);
00123   return Py_None;
00124 }
00125 
00126 /********************************************/
00127 /* sound callback                           */
00128 static GHashTable *py_sound_callbacks=NULL;
00129 
00130 void pyGcomprisSoundCallback(gchar *file){
00131   PyObject* result;
00132   PyObject* py_cb;
00133 
00134   g_warning("python sound callback : %s", file);
00135 
00136   PyGILState_STATE gil;
00137 
00138   g_assert ( py_sound_callbacks != NULL);
00139 
00140   py_cb = g_hash_table_lookup(py_sound_callbacks, file);
00141 
00142   //g_hash_table_remove(py_sound_callbacks, file);
00143 
00144   if(py_cb==NULL) return;
00145 
00146   if (!Py_IsInitialized())
00147     return;
00148 
00149   gil = pyg_gil_state_ensure();
00150 
00151   result = PyObject_CallFunction(py_cb, "O", PyString_FromString(file));
00152 
00153   // This callback can be called multiple time ?
00154   
00155   Py_DECREF(py_cb);
00156 
00157   if(result==NULL){
00158     PyErr_Print();
00159   } else {
00160     Py_DECREF(result);
00161   }
00162 
00163   pyg_gil_state_release(gil);
00164 
00165 }
00166 
00167 
00168 static PyObject*
00169 py_gc_sound_play_ogg_cb(PyObject* self, PyObject* args)
00170 {
00171   gchar *file;
00172   PyObject* pyCallback;
00173   
00174   /* Parse arguments */
00175   if(!PyArg_ParseTuple(args,
00176                      "sO:gc_sound_play_ogg_cb",
00177                      &file,
00178                      &pyCallback))
00179     return NULL;
00180 
00181   if(!PyCallable_Check(pyCallback))
00182     {
00183       PyErr_SetString(PyExc_TypeError,
00184                     "gc_sound_play_ogg_cb second argument must be callable");
00185       return NULL;
00186     }
00187 
00188   if (!py_sound_callbacks)
00189     py_sound_callbacks = g_hash_table_new_full (g_str_hash,
00190                                        g_str_equal,
00191                                        g_free,
00192                                        NULL);
00193 
00194   g_hash_table_replace (py_sound_callbacks,
00195                      g_strdup(file),
00196                      pyCallback);
00197   Py_INCREF(pyCallback);
00198 
00199   g_warning("py_gc_sound_play_ogg_cb %s", file);
00200 
00201   gc_sound_play_ogg_cb( file,
00202                      (GcomprisSoundCallback) pyGcomprisSoundCallback);
00203 
00204   /* Create and return the result */
00205   Py_INCREF(Py_None);
00206   return Py_None;
00207 
00208 }
00209 
00210 /* void gc_sound_policy_set(guint); */
00211 static PyObject*
00212 py_gc_sound_policy_set(PyObject* self, PyObject* args)
00213 {
00214   guint policy;
00215   /* Parse arguments */
00216   if(!PyArg_ParseTuple(args, "i:gc_sound_policy_set",&policy))
00217     return NULL;
00218 
00219   /* Call the corresponding C function */
00220   gc_sound_policy_set(policy);
00221 
00222   /* Create and return the result */
00223   Py_INCREF(Py_None);
00224   return Py_None;
00225 }
00226 
00227 /* int  gc_sound_policy_get(void); */
00228 static PyObject*
00229 py_gc_sound_policy_get(PyObject* self, PyObject* args)
00230 {
00231   guint policy;
00232   /* Parse arguments */
00233   if(!PyArg_ParseTuple(args, ":gc_sound_policy_set"))
00234     return NULL;
00235 
00236   /* Call the corresponding C function */
00237   policy = gc_sound_policy_get();
00238 
00239   /* Create and return the result */
00240   return Py_BuildValue("i", policy);
00241 }
00242 
00243 
00244 static PyMethodDef PythonGcomprisSoundModule[] = {
00245   { "play_ogg_list",  py_gc_sound_play_ogg_list, METH_VARARGS, "gc_sound_play_ogg_list" },
00246   { "play_ogg",  py_gc_sound_play_ogg, METH_VARARGS, "gc_sound_play_ogg" },
00247   { "reopen",  py_gc_sound_reopen, METH_VARARGS, "gc_sound_reopen" },
00248   { "close",  py_gc_sound_close, METH_VARARGS, "gc_sound_close" },
00249   { "pause",  py_gc_sound_pause, METH_VARARGS, "gc_sound_pause" },
00250   { "resume",  py_gc_sound_resume, METH_VARARGS, "gc_sound_resume" },
00251   { "play_ogg_cb",  py_gc_sound_play_ogg_cb, METH_VARARGS, "gc_sound_play_ogg_cb" },
00252   { "policy_get",  py_gc_sound_policy_get, METH_VARARGS, "gc_sound_policy_get" },
00253   { "policy_set",  py_gc_sound_policy_set, METH_VARARGS, "gc_sound_policy_set" },
00254   { NULL, NULL, 0, NULL}
00255 };
00256 
00257 void python_gcompris_sound_module_init(void)
00258 {
00259   PyObject* module;
00260   module = Py_InitModule("_gcompris_sound", PythonGcomprisSoundModule);
00261 
00262   PyModule_AddIntConstant(module, "PLAY_ONLY_IF_IDLE", PLAY_ONLY_IF_IDLE );
00263   PyModule_AddIntConstant(module, "PLAY_AFTER_CURRENT", PLAY_AFTER_CURRENT );
00264   PyModule_AddIntConstant(module, "PLAY_AND_INTERRUPT", PLAY_AND_INTERRUPT );
00265 }
00266 
00267 /* Some usefull code parts ... */
00268 /*
00269 static PyObject*
00270 py_gcompris_(PyObject* self, PyObject* args)
00271 {
00272 */  /* Parse arguments */
00273 /*  if(!PyArg_ParseTuple(args, ":gcompris_"))
00274     return NULL;
00275 */
00276   /* Call the corresponding C function */
00277 /*  gcompris_();
00278 */
00279   /* Create and return the result */
00280 /*  Py_INCREF(Py_None);
00281   return Py_None;
00282 }
00283 */
00284 /*
00285   { "",  py_gcompris_, METH_VARARGS, "gcompris_" },
00286 */