summaryrefslogtreecommitdiff
path: root/ivycpy.i
blob: 3dcc4491d7c612d92f9a31ba6cb5fe736fc27106 (plain)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
// 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 <ivyloop.h>
#include <ivychannel.h>
#include <signal.h>
#include <tcl.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 _clnt_lst *IvyClientPtr;
typedef enum { IvyApplicationConnected, IvyApplicationDisconnected } 
	IvyApplicationEvent;

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 ;  */

typedef MsgRcvPtr *MsgRcvArray ;
typedef TimerId   *TimerIdArray ;

/* 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;

#ifdef __DEBUG__	
  printf("-pytkIvyApplicationCallback\n");
#endif  
  
  ENTER_PYTHON
  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 */

  arglist = Py_BuildValue("(i)", (int) event);
  result = PyEval_CallObject(func,arglist);
  Py_DECREF(arglist);
  LEAVE_PYTHON
}
static void
pyIvyApplicationCallback(IvyClientPtr app, void *user_data,
						 IvyApplicationEvent event)
{
  PyObject *func, *arglist;
  PyObject *result;

#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 */

  arglist = Py_BuildValue("(i)", (int) event);
  result = PyEval_CallObject(func,arglist);
  Py_DECREF(arglist);
}
static void
pytkIvyDieCallback(IvyClientPtr app, void *user_data, int id)
{
  PyObject *func, *arglist;
  PyObject *result;

#ifdef __DEBUG__	
  printf("-pytkIvyApplicationCallback\n");
#endif  


  ENTER_PYTHON
  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 */

  arglist = Py_BuildValue("(i)", id);
  result = PyEval_CallObject(func,arglist);
  Py_DECREF(arglist);
  LEAVE_PYTHON
}
static void
pyIvyDieCallback(IvyClientPtr app, void *user_data, int id)
{
  PyObject *func, *arglist;
  PyObject *result;

#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 */

  arglist = Py_BuildValue("(i)", id );
  result = PyEval_CallObject(func,arglist); 
  Py_DECREF(arglist);
}

/* callback fixe (ce sera tjs le même) appellé par pyIvyBindMsg */
static void
pytkMsgCallback( IvyClientPtr app, void *user_data, int argc, char **argv )
{
  PyObject *func, *arglist;
  PyObject *result;
  PyObject *pyargv ;
  int i ;
	
#ifdef __DEBUG__	
  printf("-pytkMsgCallback\n");
#endif  
  
  ENTER_PYTHON
  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 */

  pyargv = PyTuple_New(argc);
  for (i = 0; i < argc; i++) {
     PyTuple_SetItem(pyargv,i,PyString_FromString(argv[i]));
  }
  /*  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) ;
  LEAVE_PYTHON

} 
 
static void
pyMsgCallback( IvyClientPtr app, void *user_data, int argc, char **argv )
{
  PyObject *func, *arglist;
  PyObject *result;
  PyObject *pyargv ;
  int i ;
  
#ifdef __DEBUG__	
  printf("-pyMsgCallback\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 */

  pyargv = PyTuple_New(argc);
  for (i = 0; i < argc; i++) {
     PyTuple_SetItem(pyargv,i,PyString_FromString(argv[i]));
  }
  /*  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) ;
}

/* 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;
  PyObject *result;
	
#ifdef __DEBUG__	
  printf("-pytkMsgDirectCallback\n");
#endif  
  
  ENTER_PYTHON
  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 */

  result = PyEval_CallObject(func, PyString_FromString(msg)) ;

  LEAVE_PYTHON
} 
  
static void
pyMsgDirectCallback( IvyClientPtr app, void *user_data, int id, char *msg )
{
  PyObject *func;
  PyObject *result;
  
#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 */
  result = PyEval_CallObject(func, PyString_FromString(msg)) ;

}

/* 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) {
	printf("-IvyUnbindMsg %x \n" , (int) rcvid) ;
	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  

  ENTER_PYTHON
  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);
  LEAVE_PYTHON
}

/* 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) {
	printf("-modifying timer %x \n", (int) timer);
	TimerModify(timer, (long) time);
  }
}
static void
pyIvyTimerRemove( int timerid)
{
  TimerId timer ;
  timer = getIvyTimer(timerid);
  if (timer != NULL) {
	printf("-removing timer %x \n", (int) timer);
	TimerRemove(timer);
	delIvyTimer(timerid);
  }
}

/* IvyMainLoop */
static void
pyIvyMainLoop()
{
  IvyMainLoop(0);          /* pyHookCallback, (void *) PyFunc ); */
  Py_INCREF(Py_None) ;
}

/* code repris de ivytcl.h ecrit par FR. Colin et S. Chatty */
struct _channel {
	HANDLE fd;
	void *data;
	ChannelHandleDelete handle_delete;
	ChannelHandleRead handle_read;
	};
static int channel_initialized = 0;

static void pyIvyChannelInit(void)
{
#ifdef __DEBUG__	
  printf("-pyIvyChannelInit\n");
#endif
  if ( channel_initialized ) return;
	/* pour eviter les plantages quand les autres applis font core-dump */
#ifndef WIN32
  signal( SIGPIPE, SIG_IGN);
#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;

#ifdef __DEBUG__	
  printf("-pyIvyChannelSetUp\n");
#endif  
  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 */

  return channel;

}

static void pyIvyChannelClose( Channel channel )
{
#ifdef __DEBUG__	
  printf("-pyIvyChannelClose\n");
#endif  

  if ( channel->handle_delete )
	(*channel->handle_delete)( channel->data );
  Tcl_DeleteFileHandler(channel->fd);
	
  ckfree((char *) channel);

}
 
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;
}


// 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
IvyStop ();
int
pyIvySendMsg(const char *message);
void
pyIvyMainLoop();
char *
IvyGetApplicationList();
IvyClientPtr
IvyGetApplication( char *name );

char *
IvyGetApplicationName( IvyClientPtr app );
char *IvyGetApplicationHost( 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); 

// for emacs 
// Local Variables:
// mode: C
// indent-tabs-mode: t
// tab-width: 4
// End: