aboutsummaryrefslogtreecommitdiff
path: root/civyloop.c
blob: 083b104ee15506b3e2f7b9e0f037e820433c136e (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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <getopt.h>
#include <Ivy/ivy.h>
#include <Ivy/ivyloop.h>
#include <Ivy/timer.h>
#include <Ivy/version.h>
#include <caml/mlvalues.h>
#include <caml/fail.h>
#include <caml/callback.h>
#include <caml/memory.h>
#include <caml/alloc.h>

value ivy_mainLoop(value unit)
{
#if IVYMINOR_VERSION == 8
  IvyMainLoop (NULL,NULL);
#else
  IvyMainLoop ();
#endif
  return Val_unit;
}

void timer_cb(TimerId id, void *data, unsigned long delta)
{
  value closure = *(value*)data;
  callback(closure, Val_long(id));
}

value ivy_timerRepeatafter(value nb_ticks,value delay, value closure_name)
{
  value * closure = caml_named_value(String_val(closure_name));
  TimerId id = TimerRepeatAfter(Int_val(nb_ticks), Int_val(delay), timer_cb, (void*)closure);
  return Val_int(id);
}

/* Data associated to Channel callbacks is the couple of delete and
read closures */

extern void cb_delete_channel(void *delete_read);
extern void cb_read_channel(Channel ch, IVY_HANDLE fd, void *closure);
extern void cb_write_channel(Channel ch, IVY_HANDLE fd, void *closure);


value ivy_channelSetUp(value fd, value closure_name)
{
  Channel c;
  value * closure = caml_named_value(String_val(closure_name));

#if IVYMINOR_VERSION == 8
  c = IvyChannelAdd((IVY_HANDLE)Int_val(fd), (void*)closure, cb_delete_channel, cb_read_channel);
#else
  c = IvyChannelAdd((IVY_HANDLE)Int_val(fd), (void*)closure, cb_delete_channel, cb_read_channel, cb_write_channel);
#endif
  return Val_int(c);
}

value ivy_timerRemove(value t)
{
  TimerRemove((TimerId)Long_val(t));
  return Val_unit;
}


value ivy_channelClose(value ch)
{
  IvyChannelRemove((Channel)Long_val(ch));
  return Val_unit;
}