diff options
Diffstat (limited to 'ivycpy.i')
-rw-r--r-- | ivycpy.i | 1149 |
1 files changed, 0 insertions, 1149 deletions
diff --git a/ivycpy.i b/ivycpy.i deleted file mode 100644 index 4d34a90..0000000 --- a/ivycpy.i +++ /dev/null @@ -1,1149 +0,0 @@ -// SWIG Module for making a python wrapper to C ivy library -// -#- c-mode -#- -// -// warning avoid any phrase starting with "pyIvy -// or ending with MsgAsS" -// or ending with ErrorAsS" -// cause these patterns are patched afterwards in order to neat -// Ivycpy library naming -// - -%module ivycpy - -// part 1 on place ici entre %{%} les inclusions, pre-declarations importantes, -// fonctions locales qui vont etre associées au wrapper ce code sera conservé -// intact par SWIG - -%{ -#include <stdio.h> -#include <string.h> -#include <ivy.h> -#include <timer.h> -#include <ivychannel.h> -#include <signal.h> -#include <tcl.h> -#include <sys/types.h> -#include "list.h" - -/* #define MYAPP "ivycpy" */ -/*#include <_tkinter.h> */ - - /* #include <ivytcl.h> */ -%} -// part 1 on place ici les types de donnees faisant partie des elements -// à wrapper - -/* define */ -/* numero par default du bus */ -/* #define DEFAULT_BUS 2010 */ - -/* typedef : debut */ -typedef struct { - IvyClientPtr next; - Client client; - MsgSndPtr msg_send; - char *app_name; - unsigned short app_port; -} *IvyClientPtr; - - -typedef enum { IvyApplicationConnected, IvyApplicationDisconnected } - IvyApplicationEvent; - -typedef enum { IvyAddBind, IvyRemoveBind } IvyBindEvent; - -typedef struct _timer *TimerId; - -/* a voir si a laisser ici */ -/* extern void */ -/* IvyDefaultApplicationCallback( IvyClientPtr app, void *user_data, IvyApplicationEvent event ) ; */ - -/* pour ces callbacks le clientdata sert à ... */ -/* callback appele sur connexion deconnexion d'une appli */ -/* typedef void (*IvyApplicationCallback)( */ -/* IvyClientPtr app, void *user_data, IvyApplicationEvent event); */ - -/* callback appele sur reception de die */ -/* typedef void (*IvyDieCallback)( */ -/* IvyClientPtr app, void *user_data, int id) ; */ - -/* callback appele sur reception de messages normaux */ -/* typedef void (*MsgCallback)( */ -/* IvyClientPtr app, void *user_data, int argc, char **argv ) ; */ - -/* callback appele sur reception de messages directs */ -/* typedef void (*MsgDirectCallback)( IvyClientPtr app, void *user_data, int id, char *msg ) ; */ - -/* identifiant d'une expression reguliere ( Bind/Unbind ) */ -/* typedef struct _msg_rcv *MsgRcvPtr; */ - -/* typedef : fin */ - -/* les fonctions proprement dite : debut */ - -/* filtrage des regexps */ -/* void IvyClasses( int argc, const char **argv); */ - - -/* query sur les applications connectees */ -/* char *IvyGetApplicationName( IvyClientPtr app ); */ -/* char *IvyGetApplicationHost( IvyClientPtr app ); */ -/* IvyClientPtr IvyGetApplication( char *name ); */ -/* char *IvyGetApplicationList(); */ -/* char **IvyGetApplicationMessages( IvyClientPtr app); demande de reception d'un message */ - -/* void *mcb_as_data, cette donnee doit etre passee dans le user_data */ - -/* void IvyUnbindMsg( MsgRcvPtr id ); */ - -/* emission d'un message d'erreur */ -/* void IvySendError( IvyClientPtr app, int id, const char *message); */ - -/* emmission d'un message die pour terminer l'application */ -/* void IvySendDieMsg( IvyClientPtr app ); */ - -/* emission d'un message retourne le nb effectivement emis */ - -/* int IvySendMsg(const char *message); */ - -/* Message Direct Inter-application */ - -/* void IvyBindDirectMsg( MsgDirectCallback callback, void *user_data); */ -/* void IvySendDirectMsg( IvyClientPtr app, int id, char *msg ); */ - -/* qq donnees privees necessaires */ - -/* il faudra ici traiter les pbs de callback de facon propre -voir trash1 ou les exemples sur les callbacks en Python */ - - -// ---------------------------------------------------------------- -// Python helper functions for adding callbacks -// ---------------------------------------------------------------- - -%{ -/* typedef struct { */ -/* PyObject *func, */ -/* void *true_userdata */ -/* } _Userdata4cb , *_PUserdata4cb ; */ -struct _channel { - Channel next; - HANDLE fd; - void *data; - int tobedeleted; - ChannelHandleDelete handle_delete; - ChannelHandleRead handle_read; -}; - - -static Channel channels_list = NULL; - -static int channel_initialized = 0; - -static fd_set open_fds; -static int MainLoop = 1; - - typedef MsgRcvPtr *MsgRcvArray ; - typedef TimerId *TimerIdArray ; - typedef char *Message; - typedef Message *MessagesArray; -/* donnees et fonctions de gestion des infos MsgRcvPtr retournés par - IvyBindMsg */ -static MsgRcvArray msgrcvarray ; -static int bindingid_number = 0 ; - -/* donnees et fonctions de gestion des timer retournés par - TimerRepeatAfter */ -static TimerIdArray timerarray ; -static int timerid_number = 0 ; - -static int lloopmode ; /* 0 if Ivyloop else TkLoop */ - -static int -addRcvMsg(MsgRcvPtr rcvptr) -{ - int i, bindingid ; - for(i=0;i<bindingid_number;++i) { - if (msgrcvarray[i] == NULL) { /* a slot is free */ - msgrcvarray[i] = rcvptr ; - return(i); - } - } - /* no free slot was found */ - bindingid = bindingid_number ; - bindingid_number ++ ; - - msgrcvarray = (MsgRcvPtr *) realloc( - (void *) msgrcvarray , - sizeof(MsgRcvArray) * (bindingid_number) ); - /* testing realloc */ - if (!msgrcvarray) { - fprintf(stderr,"realloc msgrcvarray return NULL\n"); - exit(1); - }; - msgrcvarray[bindingid] = rcvptr ; - /* memcpy((char *) &msgrcvarray[bindingid] , - (char *) rcvptr, sizeof(MsgRcvPtr) ); */ - return bindingid ; -} -static MsgRcvPtr -getRcvMsg(int binding_id) -{ - if (binding_id >=0 && binding_id < bindingid_number) - return msgrcvarray[binding_id] ; - else - return NULL ; -} -static void -delRcvMsg(int binding_id) -{ - if (binding_id >=0 && binding_id < bindingid_number) - msgrcvarray[binding_id] = NULL ; -} -static void -printmsgrcvarray() -{ - int i; - for(i=0;i<bindingid_number;++i) { - printf("rcvarray %i : %x \n", i, (int) msgrcvarray[i]); - } -} -static int -addIvyTimer(TimerId timer) -{ - int i, timerid ; - for(i=0;i<timerid_number;++i) { - if (timerarray[i] == NULL) { /* a slot is free */ - timerarray[i] = timer ; - return(i); - } - } - /* no free slot was found */ - timerid = timerid_number ; - timerid_number ++ ; - - timerarray = (TimerId *) realloc( - (void *) timerarray, - sizeof(TimerIdArray) * (timerid_number) ); - /* testing realloc */ - if (!timerarray) { - fprintf(stderr,"realloc timerarray return NULL\n"); - exit(1); - }; - timerarray[timerid] = timer ; - return timerid ; -} -static TimerId -getIvyTimer(int timerid) -{ - if (timerid >=0 && timerid < timerid_number) - return timerarray[timerid] ; - else - return NULL ; -} -static void -delIvyTimer(int timerid) -{ - if (timerid >=0 && timerid < timerid_number) - timerarray[timerid] = NULL ; -} -static void -printtimerarray() -{ - int i; - for(i=0;i<timerid_number;++i) { - printf("timerarray %i : %x \n", i, (int) timerarray[i]); - } -} - - -/* This function matches the prototype of a normal C callback - function for our widget. However, the clientdata pointer - actually refers to a Python callable object. */ -static void -pytkIvyApplicationCallback(IvyClientPtr app, void *user_data, - IvyApplicationEvent event) -{ - PyObject *func, *arglist; - PyObject *result, *eventobj; - PyObject *resultobj; - static void *descr = 0; -#ifdef __DEBUG__ - printf("-pytkIvyApplicationCallback\n"); -#endif - - func = (PyObject *) user_data; - /* cette verif est inutile ; elle a deja ete faite lors de - l'enregistrement du callback */ - if (!PyCallable_Check(func)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return ; - }; - /* user_data->func ; */ - /* la signature python doit etre proc(n) elle ne retourne rien */ - - descr = SWIG_TypeQuery("IvyClientPtr"); - resultobj = SWIG_NewPointerObj((void *) app, descr, 0); - eventobj = Py_BuildValue("i", (int) event); - arglist = PyTuple_New(2); - PyTuple_SetItem(arglist,0,resultobj); - PyTuple_SetItem(arglist,1,eventobj); - - result = PyEval_CallObject(func,arglist); - Py_DECREF(arglist); - -} -static void -pyIvyApplicationCallback(IvyClientPtr app, void *user_data, - IvyApplicationEvent event) -{ - PyObject *func, *arglist; - PyObject *result, *eventobj; - PyObject *resultobj; - static void *descr = 0; - -#ifdef __DEBUG__ - printf("-pyIvyApplicationCallback\n"); -#endif - func = (PyObject *) user_data; - /* cette verif est inutile ; elle a deja ete faite lors de - l'enregistrement du callback */ - if (!PyCallable_Check(func)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return ; - }; - /* user_data->func ; */ - /* la signature python doit etre proc(n) elle ne retourne rien */ - descr = SWIG_TypeQuery("IvyClientPtr"); - resultobj = SWIG_NewPointerObj((void *) app, descr, 0); - eventobj = Py_BuildValue("i", (int) event); - arglist = PyTuple_New(2); - PyTuple_SetItem(arglist,0,resultobj); - PyTuple_SetItem(arglist,1,eventobj); - - result = PyEval_CallObject(func,arglist); - Py_DECREF(arglist); -} - -static void -pyIvyBindCallback(IvyClientPtr app, void *user_data, - int id, char* regexp, IvyBindEvent event) -{ - PyObject *func, *arglist; - PyObject *result, *eventobj, *idobj, *regexpobj; - PyObject *resultobj; - static void *descr = 0; -#ifdef __DEBUG__ - printf("-pyIvyBindCallback\n"); -#endif - func = (PyObject *) user_data; - /* cette verif est inutile ; elle a deja ete faite lors de - l'enregistrement du callback */ - if (!PyCallable_Check(func)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return ; - }; - /* user_data->func ; */ - /* la signature python doit etre proc(n) elle ne retourne rien */ - descr = SWIG_TypeQuery("IvyClientPtr"); - resultobj = SWIG_NewPointerObj((void *) app, descr, 0); - - idobj = Py_BuildValue("i", (int) id); - regexpobj=PyString_FromString(regexp); - eventobj = Py_BuildValue("i", (int) event); - arglist = PyTuple_New(4); - - PyTuple_SetItem(arglist,0,resultobj); - PyTuple_SetItem(arglist,1,idobj); - PyTuple_SetItem(arglist,2,regexpobj); - PyTuple_SetItem(arglist,3,eventobj); - - result = PyEval_CallObject(func,arglist); - Py_DECREF(arglist); -} - -static void -pyIvySetBindCallback(PyObject *PyFunc) /* void *user_data, */ -{ - - IvySetBindCallback(pyIvyBindCallback, (void *) PyFunc); - Py_INCREF(PyFunc); -} - -static void -pyIvyDelBindCallback() /* void *user_data, */ -{ - - IvyDelBindCallback(); -} - -static void -pytkIvyDieCallback(IvyClientPtr app, void *user_data, int id) -{ - PyObject *func, *arglist; - PyObject *result, *idobj; - PyObject *resultobj; - static void *descr = 0; - -#ifdef __DEBUG__ - printf("-pytkIvyApplicationCallback\n"); -#endif - - - func = (PyObject *) user_data; - /* cette verif est inutile ; elle a deja ete faite lors de - l'enregistrement du callback */ - if (!PyCallable_Check(func)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return ; - }; - /* user_data->func ; */ - /* la signature python doit etre proc(n) elle ne retourne rien */ - - descr = SWIG_TypeQuery("IvyClientPtr"); - resultobj = SWIG_NewPointerObj((void *) app, descr, 0); - idobj = Py_BuildValue("i", (int) id); - - arglist = PyTuple_New(2); - PyTuple_SetItem(arglist,0,resultobj); - PyTuple_SetItem(arglist,1,idobj); - - result = PyEval_CallObject(func,arglist); - Py_DECREF(arglist); - -} -static void -pyIvyDieCallback(IvyClientPtr app, void *user_data, int id) -{ - PyObject *func, *arglist; - PyObject *result, *idobj; - PyObject *resultobj; - static void *descr = 0; - -#ifdef __DEBUG__ - printf("-pyIvyApplicationCallback\n"); -#endif - func = (PyObject *) user_data; - /* cette verif est inutile ; elle a deja ete faite lors de - l'enregistrement du callback */ - if (!PyCallable_Check(func)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return ; - }; - /* user_data->func ; */ - /* la signature python doit etre proc(n) elle ne retourne rien */ - - descr = SWIG_TypeQuery("IvyClientPtr"); - resultobj = SWIG_NewPointerObj((void *) app, descr, 0); - idobj = Py_BuildValue("i", (int) id); - - arglist = PyTuple_New(2); - PyTuple_SetItem(arglist,0,resultobj); - PyTuple_SetItem(arglist,1,idobj); - - result = PyEval_CallObject(func,arglist); - Py_DECREF(arglist); -} - - -static void -pytkMsgCallback( IvyClientPtr app, void *user_data, int argc, char **argv ) -{ - PyObject *pyargv ; - int i ; - PyObject *func; - PyObject *result; - PyObject *resultobj; - static void *descr = 0; - -#ifdef __DEBUG__ - printf("-pytkMsgCallback\n"); -#endif - - func = (PyObject *) user_data; - - /* cette verif est inutile ; elle a deja ete faite lors de - l'enregistrement du callback */ - if (!PyCallable_Check(func)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return ; - }; - /* user_data->func ; */ - /* la signature python doit etre proc(*arg)) - elle ne retourne rien */ - descr = SWIG_TypeQuery("IvyClientPtr"); - resultobj = SWIG_NewPointerObj((void *) app, descr, 0); - pyargv = PyTuple_New(argc+1); - PyTuple_SetItem(pyargv,0,resultobj); - for (i = 1; i < argc+1; i++) { - PyTuple_SetItem(pyargv,i,PyString_FromString(argv[i-1])); - } - /* this codes is useless; the tuple (pyargv) is directly passed - to the Python call back - arglist = Py_BuildValue("(iO)", argc, pyargv); - result = PyEval_CallObject(func,arglist); - Py_DECREF(arglist); */ - - result = PyEval_CallObject(func, pyargv) ; - Py_DECREF(pyargv); -} - -static void -pyMsgCallback( IvyClientPtr app, void *user_data, int argc, char **argv ) -{ - PyObject *pyargv ; - int i ; - PyObject *func; - PyObject *result; - PyObject *resultobj; - PyObject *chaine; - static void *descr = 0; -#ifdef __DEBUG__ - printf("-pytkMsgCallback\n"); -#endif - - func = (PyObject *) user_data; - - /* cette verif est inutile ; elle a deja ete faite lors de - l'enregistrement du callback */ - if (!PyCallable_Check(func)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return ; - }; - /* user_data->func ; */ - /* la signature python doit etre proc(*arg)) - elle ne retourne rien */ - descr = SWIG_TypeQuery("IvyClientPtr"); - resultobj = SWIG_NewPointerObj((void *) app, descr, 0); - pyargv = PyTuple_New(argc+1); - PyTuple_SetItem(pyargv,0,resultobj); - for (i = 1; i < argc+1; i++) { - if (argv[i-1]){ - chaine=PyString_FromString(argv[i-1]); - } - else{ - chaine=PyString_FromString(""); - } - if(PyString_Check(chaine)){ - PyTuple_SetItem(pyargv,i,chaine); - } - else{ - printf("-pytkMsgCallback Error %i\n",i); - } - } - /* this codes is useless; the tuple (pyargv) is directly passed - to the Python call back - */ - result = PyEval_CallObject(func, pyargv) ; - Py_DECREF(pyargv); - /*Py_DECREF(resultobj);*/ -} - -/* jeu de 2 callback fixes (mode tk ou sans (ce sera tjs le même) appellé par - pyIvyBindDirectMsg */ -static void -pytkMsgDirectCallback( IvyClientPtr app, void *user_data, int id, char *msg ) -{ - PyObject *func, *arglist; - PyObject *result; - PyObject *resultobj; - static void *descr = 0; - -#ifdef __DEBUG__ - printf("-pytkMsgDirectCallback\n"); -#endif - - func = (PyObject *) user_data; - - /* cette verif est inutile ; elle a deja ete faite lors de - l'enregistrement du callback */ - if (!PyCallable_Check(func)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return ; - }; - /* user_data->func ; */ - /* la signature python doit etre proc(arg)) - elle ne retourne rien */ - descr = SWIG_TypeQuery("IvyClientPtr"); - resultobj= SWIG_NewPointerObj((void *) app, descr, 0); - arglist = PyTuple_New(2); - PyTuple_SetItem(arglist,0,resultobj); - PyTuple_SetItem(arglist,1,PyString_FromString(msg)); - - result = PyEval_CallObject(func,arglist); - Py_DECREF(arglist); - Py_DECREF(resultobj); -} - -static void -pyMsgDirectCallback( IvyClientPtr app, void *user_data, int id, char *msg ) -{ - PyObject *func, *arglist; - PyObject *result; - PyObject *resultobj; - static void *descr = 0; - -#ifdef __DEBUG__ - printf("-pyMsgDirectCallback\n"); -#endif - func = (PyObject *) user_data; - - /* cette verif est inutile ; elle a deja ete faite lors de - l'enregistrement du callback */ - if (!PyCallable_Check(func)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return ; - }; - /* user_data->func ; */ - /* la signature python doit etre proc(arg)) - elle ne retourne rien */ - descr = SWIG_TypeQuery("IvyClientPtr"); - resultobj= SWIG_NewPointerObj((void *) app, descr, 0); - arglist = PyTuple_New(2); - PyTuple_SetItem(arglist,0,resultobj); - PyTuple_SetItem(arglist,1,PyString_FromString(msg)); - - result = PyEval_CallObject(func,arglist); - Py_DECREF(arglist); - Py_DECREF(resultobj); -} - -/* facade to IvyBindMsg avoiding vararg problem */ -static int -pyIvyBindMsg (PyObject *PyFunc, const char *msg) /* void *user_data, */ -{ - MsgRcvPtr result ; - int binding_id = -1 ; - - /* le user_data est le pointeur PyFunc */ - if (lloopmode) { - result = IvyBindMsg(pytkMsgCallback, (void *) PyFunc , msg); - } - else { - result = IvyBindMsg(pyMsgCallback, (void *) PyFunc , msg); - } - /* on conserve une trace de result sous la forme d'une table - indexé i -> (result) ; cet indice sera incrémenté et retourné - comme valeur de retour */ -#ifdef __DEBUG__ - printf("-IvyBindMsg retourne %x \n", (int) result); -#endif - binding_id = addRcvMsg(result) ; -#ifdef __DEBUG__ - printmsgrcvarray(); -#endif - Py_INCREF(PyFunc); - Py_INCREF(Py_None) ; - return binding_id ; -} -static void -pyIvyUnBindMsg (int binding_id) -{ /* on passe à pyIvyUnBindMsg l'indexe du MsgRcvPtr retourné par - pyIvyBindMsg */ - MsgRcvPtr rcvid ; - rcvid = getRcvMsg(binding_id) ; - if (rcvid != NULL) { - IvyUnbindMsg(rcvid); - delRcvMsg(binding_id); - }; -#ifdef __DEBUG__ - printmsgrcvarray() ; -#endif -} - -/* facade to IvyBindDirectMsg avoiding vararg problem */ -static void -pyIvyBindDirectMsg (PyObject *PyFunc) /* void *user_data, */ -{ - /* le user_data est le pointeur PyFunc */ - if (lloopmode) { - IvyBindDirectMsg(pytkMsgDirectCallback, (void *) PyFunc); - } - else { - IvyBindDirectMsg(pyMsgDirectCallback, (void *) PyFunc); - } - /* on conserve une trace de result sous la forme d'une table - indexé i -> (result) ; cet indice sera incrémenté et retourné - comme valeur de retour */ -#ifdef __DEBUG__ - printmsgrcvarray(); -#endif - Py_INCREF(PyFunc); - Py_INCREF(Py_None) ; -} - -/* facade to IvySendMsg avoiding vararg problem */ -int -pyIvySendMsg(const char *message) -{ - return(IvySendMsg(message)); -} -void -pyIvySendError( IvyClientPtr app, int id, const char *message) -{ - IvySendError(app,id,message); -} - -/* for ivy timer */ - -static void -pyTimerCallback(TimerId id , void *user_data, unsigned long delta ) -{ - PyObject *func ; - PyObject *result; - -#ifdef __DEBUG__ - printf("-pyTimerCallback\n"); -#endif - func = (PyObject *) user_data; - - /* cette verif est inutile ; elle a deja ete faite lors de - l'enregistrement du callback */ - if (!PyCallable_Check(func)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return ; - }; - /* la signature de la fonction python a appellé doit etre - proc() */ - result = PyEval_CallObject(func,NULL); -} -static void -pytkTimerCallback(TimerId id , void *user_data, unsigned long delta ) -{ - PyObject *func ; - PyObject *result; - -#ifdef __DEBUG__ - printf("-pytkTimerCallback\n"); -#endif - - func = (PyObject *) user_data; - - /* cette verif est inutile ; elle a deja ete faite lors de - l'enregistrement du callback */ - if (!PyCallable_Check(func)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return ; - }; - /* la signature de la fonction python a appellé doit etre - proc() */ - result = PyEval_CallObject(func,NULL); -} - -/* add an ivy timer */ -/* retourne l'indice du timer pour identification ultérieure */ -static int -pyIvyTimerRepeatAfter(int count, int time, PyObject *PyFunc) -{ - TimerId timer ; - int timerid ; -#ifdef __DEBUG__ - printf("-pyIvyTimerRepeatAfter\n"); -#endif - if (lloopmode) { - timer = TimerRepeatAfter(count, (long) time, pytkTimerCallback, - (void *) PyFunc); - } - else { - timer = TimerRepeatAfter(count, time, pyTimerCallback, - (void *) PyFunc); - } - timerid = addIvyTimer(timer); -#ifdef __DEBUG__ - printtimerarray(); -#endif - Py_INCREF(PyFunc); - Py_INCREF(Py_None) ; - return timerid ; -} -static void -pyIvyTimerModify( int timerid, int time ) -{ - TimerId timer ; - timer = getIvyTimer(timerid); - if (timer != NULL) { - TimerModify(timer, (long) time); - } -} -static void -pyIvyTimerRemove( int timerid) -{ - TimerId timer ; - timer = getIvyTimer(timerid); - if (timer != NULL) { - TimerRemove(timer); - delIvyTimer(timerid); - } -} - -static void -pyIvyStop (void) -{ - MainLoop = 0; -} - -static void -pyIvyChannelHandleRead (fd_set *current) -{ - Channel channel, next; - - IVY_LIST_EACH_SAFE (channels_list, channel, next) { - if (FD_ISSET (channel->fd, current)) { - (*channel->handle_read)(channel,channel->fd,channel->data); - } - } -} - -static void -pyIvyChannelHandleExcpt (fd_set *current) -{ - Channel channel,next; - IVY_LIST_EACH_SAFE (channels_list, channel, next) { - if (FD_ISSET (channel->fd, current)) { - if (channel->handle_delete) - (*channel->handle_delete)(channel->data); -/* IvyChannelClose (channel); */ - } - } -} - -static void -pyIvyChannelDelete (Channel channel) -{ - if (channel->handle_delete) - (*channel->handle_delete) (channel->data); - - FD_CLR (channel->fd, &open_fds); - IVY_LIST_REMOVE (channels_list, channel); -} -static void -pyChannelDefferedDelete () -{ - Channel channel, next; - IVY_LIST_EACH_SAFE (channels_list, channel,next) { - if (channel->tobedeleted ) { - pyIvyChannelDelete (channel); - } - } -} - -/* IvyMainLoop */ -static void -pyIvyMainLoop() -{ - fd_set rdset; - fd_set exset; - int ready; - - while (MainLoop) { - pyChannelDefferedDelete(); - rdset = open_fds; - exset = open_fds; - Py_BEGIN_ALLOW_THREADS - ready = select(64, &rdset, 0, &exset, TimerGetSmallestTimeout()); - Py_END_ALLOW_THREADS - if (ready < 0 && (errno != EINTR)) { - fprintf (stderr, "select error %d\n",errno); - perror("select"); - return; - } - TimerScan(); - if (ready > 0) { - pyIvyChannelHandleExcpt(&exset); - pyIvyChannelHandleRead(&rdset); - continue; - } - } - Py_INCREF(Py_None) ; -} - -/* code repris de ivytcl.h ecrit par FR. Colin et S. Chatty */ - -static void pyIvyChannelInit(void) -{ -#ifdef __DEBUG__ - printf("-pyIvyChannelInit\n"); -#endif -#ifdef WIN32 - int error; -#else - /* pour eviter les plantages quand les autres applis font core-dump */ - signal (SIGPIPE, SIG_IGN); -#endif - if (channel_initialized) return; - - if (!lloopmode){ - FD_ZERO (&open_fds); - } - -#ifdef WIN32 - error = WSAStartup (0x0101, &WsaData); - if (error == SOCKET_ERROR) { - printf ("WSAStartup failed.\n"); - } -#endif - channel_initialized = 1; -} - - -static void -IvyHandleFd(ClientData cd, - int mask) -{ - Channel channel = (Channel)cd; - - /*printf("-handle event %d\n", mask);*/ - if (mask == TCL_READABLE) { - (*channel->handle_read)(channel,channel->fd,channel->data); - } - else if (mask == TCL_EXCEPTION) { - (*channel->handle_delete)(channel->data); - } -} - -static Channel pyIvyChannelSetUp( - HANDLE fd, - void *data, - ChannelHandleDelete handle_delete, - ChannelHandleRead handle_read) -{ - Channel channel; - if (lloopmode) { - channel = (Channel)ckalloc( sizeof (struct _channel) ); /* ckalloc */ - if ( !channel ) { - fprintf(stderr,"NOK Memory Alloc Error\n"); - exit(0); - } - - channel->handle_delete = handle_delete; - channel->handle_read = handle_read; - channel->data = data; - channel->fd = fd; - - /*printf("-Create handle fd %d\n", fd);*/ - /* Py_BEGIN_ALLOW_THREADS*/ - /* ENTER_TCL */ - Tcl_CreateFileHandler(fd, TCL_READABLE|TCL_EXCEPTION, IvyHandleFd, - (ClientData) channel); - /* LEAVE_TCL */ - /* Py_END_ALLOW_THREADS */ - } - else{ - IVY_LIST_ADD (channels_list, channel); - if (!channel) { - fprintf(stderr,"NOK Memory Alloc Error\n"); - exit(0); - } - channel->fd = fd; - channel->tobedeleted = 0; - channel->handle_delete = handle_delete; - channel->handle_read = handle_read; - channel->data = data; - - FD_SET (channel->fd, &open_fds); - } - return channel; -} - -static void pyIvyChannelClose( Channel channel ) -{ -#ifdef __DEBUG__ - printf("-pyIvyChannelClose\n"); -#endif - if (lloopmode) { - if ( channel->handle_delete ) - (*channel->handle_delete)( channel->data ); - Tcl_DeleteFileHandler(channel->fd); - ckfree((char *) channel); - } - else{ - - channel->tobedeleted = 1; - } -} -ChannelInit channel_init = pyIvyChannelInit; -ChannelSetUp channel_setup = pyIvyChannelSetUp; -ChannelClose channel_close = pyIvyChannelClose; - -static void -pyIvyInit( - const char *AppName, /* nom de l'application */ - const char *ready, /* ready Message peut etre NULL */ - int loopmode, /* 1 = mode Tk , 0 = mode Ivyloop */ - PyObject *PyFuncOnCx, /* callback appele sur connection deconnection - d'une appli */ - /* void *data, user data passe au callback */ - PyObject *PyFuncOnDie) /* last change callback before die - void *die_data ) user data */ -{ -#ifdef __DEBUG__ - printf("-pyIvyInit\n"); -#endif - - lloopmode = loopmode ; - if (loopmode == 1) { -/* channel_init = pyIvyChannelInit ; */ -/* channel_setup = pyIvyChannelSetUp; */ -/* channel_close = pyIvyChannelClose; */ - IvyInit(AppName,ready, - pytkIvyApplicationCallback, (void *) PyFuncOnCx, - pytkIvyDieCallback, (void *) PyFuncOnDie); - } - else { - IvyInit(AppName,ready, - pyIvyApplicationCallback, (void *) PyFuncOnCx, - pyIvyDieCallback, (void *) PyFuncOnDie); - } - - Py_INCREF(PyFuncOnCx); - Py_INCREF(PyFuncOnDie); - -} - - -%} - -// part 2 -// on place ici les declarations complementaires : typemap, etc - -// ------------------------------------------------------------------- -// SWIG typemap allowing us to grab a Python callable object -// ------------------------------------------------------------------- - -/* %typemap(python,in) PyObject *PyFunc { */ -/* if (!PyCallable_Check($source)) { */ -/* PyErr_SetString(PyExc_TypeError, "Need a callable object!"); */ -/* return NULL; */ -/* } */ -/* $target = $source; */ -/* } */ - -/* %typemap(python,in) PyObject *PyFuncOnCx { */ -/* if (!PyCallable_Check($source)) { */ -/* PyErr_SetString(PyExc_TypeError, "Need a callable object!"); */ -/* return NULL; */ -/* } */ -/* $target = $source; */ -/* } */ - -/* %typemap(python,in) PyObject *PyFuncOnDie { */ -/* if (!PyCallable_Check($source)) { */ -/* PyErr_SetString(PyExc_TypeError, "Need a callable object!"); */ -/* return NULL; */ -/* } */ -/* $target = $source; */ -/* } */ - -// nouvelle syntaxe SWIG 1.3.x : $target -> $1 et $source -> $input -%typemap(python,in) PyObject *PyFunc { - if (!PyCallable_Check($input)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return NULL; - } - $1 = $input; -} - -%typemap(python,in) PyObject *PyFuncOnCx { - if (!PyCallable_Check($input)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return NULL; - } - $1 = $input; -} - -%typemap(python,in) PyObject *PyFuncOnDie { - if (!PyCallable_Check($input)) { - PyErr_SetString(PyExc_TypeError, "Need a callable object!"); - return NULL; - } - $1 = $input; -} - -%typemap(python,out) char** { - int size=0, i; - char **msgList; - msgList=$1; - while (*msgList ) - { - /*printf("Message to '%s'\n",*msgList++);*/ - *msgList++; - size++; - } - resultobj=PyTuple_New(size); - msgList=$1; - for (i = 0; i < size; i++) { - PyTuple_SetItem(resultobj,i,PyString_FromString(*msgList++)); - } - $result=resultobj; -} - -// part 3 -// on place ici les donnees, fonctions a wrapper - -void -pyIvyInit( - const char *AppName, /* nom de l'application */ - const char *ready, /* ready Message peut etre NULL */ - int loopmode, - PyObject *PyFuncOnCx, /* callback appele sur connection deconnection - d'une appli */ - /*void *data, user data passe au callback */ - PyObject *PyFuncOnDie); /* last change callback before die */ -/*void *die_data ); user data */ - -int -pyIvyBindMsg (PyObject *PyFunc, const char *msg); -void -pyIvyUnBindMsg (int binding_id); -void -IvyStart (const char*); -void -pyIvyStop (); -int -pyIvySendMsg(const char *message); -void -pyIvyMainLoop(); -char * -IvyGetApplicationList(); -IvyClientPtr -IvyGetApplication( char *name ); - -char * -IvyGetApplicationName( IvyClientPtr app ); -char *IvyGetApplicationHost( IvyClientPtr app ); -char **IvyGetApplicationMessages( IvyClientPtr app); -/* emission d'un message d'erreur */ -void -pyIvySendError( IvyClientPtr app, int id, const char *message); - -/* emission d'un message die pour terminer l'application */ -void -IvySendDieMsg( IvyClientPtr app ); - -/* send and bind direct */ -void -IvySendDirectMsg( IvyClientPtr app, int id, char *msg ); - -/* binddirect a venir */ -void -pyIvyBindDirectMsg (PyObject *PyFunc); - -/* fonction a wrapper concernant les timer */ -int -pyIvyTimerRepeatAfter(int count, int time, PyObject *PyFunc); -void -pyIvyTimerModify( int timerid, int time ); -void -pyIvyTimerRemove( int timerid); - -static void -pyIvySetBindCallback(PyObject *PyFunc); -static void -pyIvyDelBindCallback(); - -// for emacs -// Local Variables: -// mode: C -// indent-tabs-mode: t -// tab-width: 4 -// End: |