Back to index

gcompris  8.2.2
py-mod-timer.c
Go to the documentation of this file.
00001 #include <Python.h>
00002 #include <pygobject.h>
00003 #include "gcompris/gcompris.h"
00004 #include "py-mod-utils.h"
00005 #include "py-gcompris-board.h"
00006 
00007 /* All functions provided by this python module
00008  * wraps a gcompris function. Each "py_*" function wraps the
00009  * "*" C function.
00010  */
00011 
00012 /* typedef void (*GcomprisTimerEnd) (); */
00013 static PyObject* pyTimerCallBackFunc = NULL;
00014 
00015 void pyTimerCallBack(){
00016   PyObject* result;
00017   if(pyTimerCallBackFunc==NULL) return;
00018 
00019   /* Build arguments */
00020   result = PyObject_CallObject(pyTimerCallBackFunc, NULL);
00021   if(result==NULL){
00022     PyErr_Print();
00023   } else {
00024     Py_DECREF(result);
00025   }
00026 }
00027 
00028 
00029 /* void gc_timer_display
00030         (int x, int y, TimerList type, int second, GcomprisTimerEnd gcomprisTimerEnd); */
00031 static PyObject*
00032 py_gc_timer_display(PyObject* self, PyObject* args)
00033 {
00034   int x,y;
00035   int type;
00036   int second;
00037   PyObject* pyCallback;
00038 
00039   /* Parse arguments */
00040   if(!PyArg_ParseTuple(args, "iiiiO:gc_timer_display", &x, &y, &type, &second, &pyCallback))
00041     return NULL;
00042   if(!PyCallable_Check(pyCallback)) return NULL;
00043   pyTimerCallBackFunc = pyCallback;
00044 
00045   /* Call the corresponding C function */
00046   gc_timer_display(x, y, type, second, pyTimerCallBack );
00047 
00048   /* Create and return the result */
00049   Py_INCREF(Py_None);
00050   return Py_None;
00051 }
00052 
00053 
00054 /* void gc_timer_add(int second); */
00055 static PyObject*
00056 py_gc_timer_add(PyObject* self, PyObject* args)
00057 {
00058   int second;
00059 
00060   /* Parse arguments */
00061   if(!PyArg_ParseTuple(args, "i:gc_timer_add", &second))
00062     return NULL;
00063 
00064   /* Call the corresponding C function */
00065   gc_timer_add(second);
00066 
00067   /* Create and return the result */
00068   Py_INCREF(Py_None);
00069   return Py_None;
00070 }
00071 
00072 
00073 /*void gc_timer_end(void); */
00074 static PyObject*
00075 py_gc_timer_end(PyObject* self, PyObject* args)
00076 {
00077   /* Parse arguments */
00078   if(!PyArg_ParseTuple(args, ":gc_timer_end"))
00079     return NULL;
00080 
00081   /* Call the corresponding C function */
00082   gc_timer_end();
00083 
00084   /* Create and return the result */
00085   Py_INCREF(Py_None);
00086   return Py_None;
00087 }
00088 
00089 
00090 /* guint gc_timer_get_remaining(); */
00091 static PyObject*
00092 py_gc_timer_get_remaining(PyObject* self, PyObject* args)
00093 {
00094   guint result;
00095 
00096   /* Parse arguments */
00097   if(!PyArg_ParseTuple(args, ":gc_timer_get_remaining"))
00098     return NULL;
00099 
00100   /* Call the corresponding C function */
00101   result = gc_timer_get_remaining();
00102 
00103   /* Create and return the result */
00104   return Py_BuildValue("i", result);  
00105 }
00106 
00107 
00108 /* void        gc_timer_pause(gboolean pause); */
00109 static PyObject*
00110 py_gc_timer_pause(PyObject* self, PyObject* args)
00111 {
00112   gboolean pause;
00113 
00114   /* Parse arguments */
00115   if(!PyArg_ParseTuple(args, "i:gc_timer_pause", &pause))
00116     return NULL;
00117 
00118   /* Call the corresponding C function */
00119   gc_timer_pause(pause);
00120 
00121   /* Create and return the result */
00122   Py_INCREF(Py_None);
00123   return Py_None;
00124 }
00125 
00126 
00127 static PyMethodDef PythonGcomprisTimerModule[] = {
00128   { "display",  py_gc_timer_display, METH_VARARGS, "gc_timer_display" },
00129   { "add",  py_gc_timer_add, METH_VARARGS, "gc_timer_add" },
00130   { "end",  py_gc_timer_end, METH_VARARGS, "gc_timer_end" },
00131   { "get_remaining",  py_gc_timer_get_remaining, METH_VARARGS, "gc_timer_get_remaining" },
00132   { "pause",  py_gc_timer_pause, METH_VARARGS, "gc_timer_pause" },
00133   { NULL, NULL, 0, NULL}
00134 };
00135 
00136 void python_gc_timer_module_init(void)
00137 {
00138   PyObject* module;
00139   module = Py_InitModule("_gcompris_timer", PythonGcomprisTimerModule);
00140 
00141   /* TimerList constants */
00142   PyModule_AddIntConstant(module, "TEXT", GCOMPRIS_TIMER_TEXT );
00143   PyModule_AddIntConstant(module, "SAND", GCOMPRIS_TIMER_SAND );
00144   PyModule_AddIntConstant(module, "BALLOON", GCOMPRIS_TIMER_BALLOON );
00145   PyModule_AddIntConstant(module, "CLOCK", GCOMPRIS_TIMER_CLOCK );
00146 }
00147 
00148 /* Some usefull code parts ... */
00149 /*
00150 static PyObject*
00151 py_gcompris_(PyObject* self, PyObject* args)
00152 {
00153 */  /* Parse arguments */
00154 /*  if(!PyArg_ParseTuple(args, ":gcompris_"))
00155     return NULL;
00156 */
00157   /* Call the corresponding C function */
00158 /*  gcompris_();
00159 */
00160   /* Create and return the result */
00161 /*  Py_INCREF(Py_None);
00162   return Py_None;
00163 }
00164 */
00165 /*
00166   { "",  py_gcompris_, METH_VARARGS, "gcompris_" },
00167 */