2 + D Programming Language "bindings" to libev
3 + <http://software.schmorp.de/pkg/libev.html>
5 + Written by Leandro Lucarella (2008).
7 + Placed under BOLA license <http://auriga.wearlab.de/~alb/bola/> which is
8 + basically public domain.
25 PERIODIC = EV_PERIODIC,
41 FORKCHECK = EVFLAG_FORKCHECK,
42 SELECT = EVBACKEND_SELECT,
43 POLL = EVBACKEND_POLL,
44 EPOLL = EVBACKEND_EPOLL,
45 KQUEUE = EVBACKEND_KQUEUE,
46 DEVPOLL = EVBACKEND_DEVPOLL,
47 PORT = EVBACKEND_PORT,
52 NONBLOCK = EVLOOP_NONBLOCK,
53 ONESHOT = EVLOOP_ONESHOT,
58 CANCEL = EVUNLOOP_CANCEL,
63 alias ev_tstamp tstamp;
65 alias ev_statdata statdata;
69 return ev_version_major();
74 return ev_version_minor();
77 uint supported_backends()
79 return ev_supported_backends();
82 uint recommended_backends()
84 return ev_recommended_backends();
87 uint embeddable_backends()
89 return ev_embeddable_backends();
97 void sleep(tstamp delay)
99 void ev_sleep(tstamp delay);
102 private extern(C) void* allocator_thunk(alias Fn)(void* ptr, int size)
104 return Fn(ptr, size);
107 // Fn is void* function(void* ptr, int size)
108 void set_allocator(alias Fn)()
110 ev_set_allocator(&allocator_thunk!(Fn));
113 debug (ev_d_set_allocator)
118 static void* alloc(void* ptr, int size)
120 writefln("alloc(", ptr, ", ", size, ")");
123 set_allocator!(alloc)();
128 private extern(C) void syserr_thunk(alias Fn)(char* msg)
130 string m = toString(msg);
134 // Fn is void function(string msg)
135 void set_syserr_cb(alias Fn)()
137 ev_set_syserr_cb(&syserr_thunk!(Fn));
142 static void syserr(string msg)
145 set_syserr_cb!(syserr)();
149 private alias extern (C) void function(int, void*) once_callback_t;
154 alias void delegate(ILoop, int, int) OnceCallback;
166 void loop(int flags = 0);
168 void unloop(Unloop how = Unloop.ONE);
170 void ioCollectInterval(tstamp interval);
172 void timeoutCollectInterval(tstamp interval);
178 void once(int fd, int events, tstamp timeout,
179 once_callback_t cb, void* arg = null);
181 void once(int fd, int events, tstamp timeout, OnceCallback cb);
183 void once(int fd, int events, OnceCallback cb);
185 void once(tstamp timeout, OnceCallback cb);
187 void feedFdEvent(int fd, int revents);
189 void feedSignalEvent(int signum);
193 private struct OnceData
197 ILoop.OnceCallback cb;
200 private extern(C) void once_thunk(int revents, void* arg)
202 auto d = cast (OnceData*) arg;
203 d.cb(d.loop, d.fd, revents);
221 return ev_backend(ptr);
226 return ev_loop_count(ptr);
229 void loop(int flags = 0)
234 void unloop(Unloop how = Unloop.ONE)
239 void ioCollectInterval(tstamp interval)
241 ev_set_io_collect_interval(ptr, interval);
244 void timeoutCollectInterval(tstamp interval)
246 ev_set_timeout_collect_interval(ptr, interval);
259 void once(int fd, int events, tstamp timeout,
260 once_callback_t cb, void* arg = null)
262 ev_once(ptr, fd, events, timeout, cb, arg);
265 void once(int fd, int events, tstamp timeout, OnceCallback cb)
267 auto d = new OnceData;
271 once(fd, events, timeout, &once_thunk, d);
274 void once(int fd, int events, OnceCallback cb)
276 once(fd, events, -1.0, cb);
279 void once(tstamp timeout, OnceCallback cb)
281 once(-1, NONE, timeout, cb);
284 void feedFdEvent(int fd, int revents)
286 return ev_feed_fd_event(ptr, fd, revents);
289 void feedSignalEvent(int signum)
291 return ev_feed_signal_event(ptr, signum);
294 private ev_loop_t* _ptr;
303 this(uint flags = AUTO)
305 _ptr = ev_loop_new(flags);
310 ev_loop_destroy(ptr);
320 auto loop = new Loop;
321 assert (loop.count == 0);
327 private class DefaultLoop: ILoop
332 this(uint flags = AUTO)
334 _ptr = ev_default_loop(flags);
339 ev_default_destroy();
353 debug (ev_d_DefaultLoop) import std.stdio;
356 debug (ev_d_DefaultLoop) writefln("BEGIN UNITTEST");
357 auto loop = new DefaultLoop;
358 debug (ev_d_DefaultLoop) writefln("loop.count = ", loop.count);
362 void ev(ILoop loop, int fd, int revents)
364 debug (ev_d_DefaultLoop)
367 if (revents & READ) writefln("\tREAD");
368 if (revents & TIMEOUT) writefln("\tTIMEOUT");
373 loop.once(0, READ, 2.0, &c.ev);
375 debug (ev_d_DefaultLoop) writefln("END UNITTEST");
380 private DefaultLoop _loop;
382 DefaultLoop loop(uint flags = AUTO)
384 if (!_loop) _loop = new DefaultLoop(flags);
407 void feed(int revents);
417 string property(string type, string proxy, string name, bool access = RW)()
419 string s = "\n\t" ~ type ~ " " ~ name ~ "() {\n"
420 "\t\treturn cast (" ~ type ~ ") " ~ proxy ~ "." ~ name ~ ";\n"
424 s ~= "\n\tvoid " ~ name ~ "(" ~ type ~ " " ~ name ~ ") {\n"
425 "\t\t" ~ proxy ~ "." ~ name ~ " = cast (" ~ type ~ ") " ~ name ~ ";\n"
431 string wproperty(string type, string name, bool access = RW)()
433 debug (ev_d_property)
434 pragma(msg, "" ~ property!(type, "ptr", name, access));
435 return property!(type, "ptr", name, access);
438 string dproperty(string type, string name, bool access = RW)()
440 debug (ev_d_property)
441 pragma(msg, "" ~ property!(type, "data", name, access));
442 return property!(type, "data", name, access);
445 template MWatcher(DWatcher, CWatcher, alias StartFunc, alias StopFunc,
446 bool DefineData = true)
449 static if (DefineData) private struct WatcherData
455 alias void delegate(DWatcher, int revents) Callback;
457 private extern(C) static void watcher_thunk(ev_loop_t* loop,
458 CWatcher* watcher, int revents)
460 auto d = cast (WatcherData*) watcher.data;
461 d.cb(d.watcher, revents);
464 private void init(ILoop loop, Callback cb)
466 ev_init(ptr, &watcher_thunk);
467 auto d = new WatcherData;
481 return ev_is_pending(ptr);
486 return ev_is_active(ptr);
491 return ev_priority(ptr);
494 void priority(int prio)
496 ev_set_priority(ptr, prio);
509 void loop(ILoop loop)
519 private CWatcher _ptr;
523 StartFunc(loop.ptr, ptr);
528 StopFunc(loop.ptr, ptr);
531 void feed(int revents)
533 ev_feed_event(loop.ptr, ptr, revents);
536 private mixin (wproperty!("WatcherData*", "data"));
538 mixin (dproperty!("Callback", "cb"));
545 mixin MWatcher!(Io, ev_io, ev_io_start, ev_io_stop);
547 this(int fd, int events, Callback cb, ILoop loop = ev.d.loop)
550 ev_io_set(ptr, fd, events);
553 mixin (wproperty!("int", "fd", RO));
555 mixin (wproperty!("int", "events", RO));
561 import std.c.unix.unix;
564 auto w = new Io(0, READ, (Io w, int revents)
567 writefln("io callback: revents=", revents);
569 int r = read(w.fd, buff.ptr, buff.length);
571 writefln("\tread %d bytes: %s", r, buff);
579 interface ITimer: IWatcher
586 template MTimer(alias AgainFunc)
591 AgainFunc(loop.ptr, ptr);
599 mixin MWatcher!(Timer, ev_timer, ev_timer_start, ev_timer_stop);
601 mixin MTimer!(ev_timer_again);
603 this(tstamp after, tstamp repeat, Callback cb, ILoop loop = ev.d.loop)
606 ev_timer_set(ptr, after, repeat);
609 this(tstamp after, Callback cb, ILoop loop = ev.d.loop)
611 this(after, 0.0, cb, loop);
614 mixin (wproperty!("tstamp", "repeat"));
619 import std.c.unix.unix;
623 auto w = new Timer(1.0, (Timer w, int revents)
626 writefln("timeout callback: revents=", revents);
634 class Periodic: ITimer
637 mixin MWatcher!(Periodic, ev_periodic, ev_periodic_start,
638 ev_periodic_stop, false);
640 mixin MTimer!(ev_periodic_again);
642 alias tstamp delegate(Periodic, tstamp) RescheduleCallback;
644 private struct WatcherData
646 void delegate(Periodic, int) cb;
647 RescheduleCallback reschedulecb;
651 private extern(C) static tstamp reschedule_thunk(ev_periodic* watcher,
654 auto d = cast (WatcherData*) watcher.data;
655 return d.reschedulecb(d.watcher, now);
658 this(tstamp at, tstamp interval, RescheduleCallback reschedulecb,
659 Callback cb, ILoop loop = ev.d.loop)
662 ev_periodic_set(ptr, at, interval, null);
663 this.reschedulecb = reschedulecb;
666 void reschedulecb(RescheduleCallback reschedulecb)
668 if (reschedulecb is null) {
669 ptr.reschedule_cb = null;
670 data.reschedulecb = null;
673 ptr.reschedule_cb = &reschedule_thunk;
674 data.reschedulecb = reschedulecb;
678 mixin (dproperty!("RescheduleCallback", "reschedulecb", RO));
680 mixin (wproperty!("tstamp", "offset"));
682 mixin (wproperty!("tstamp", "interval"));
684 mixin (wproperty!("tstamp", "at", RO));
691 this(tstamp time, Callback cb, ILoop loop = ev.d.loop)
693 super(time, 0.0, null, cb, loop);
701 this(tstamp offset, tstamp interval, Callback cb,
702 ILoop loop = ev.d.loop)
704 assert (interval > 0);
707 super(offset, interval, null, cb, loop);
712 class ManualCron: Periodic
714 this(RescheduleCallback reschedulecb, Callback cb,
715 ILoop loop = ev.d.loop)
717 super(0.0, 0.0, reschedulecb, cb, loop);
721 class Signal: IWatcher
724 mixin MWatcher!(Signal, ev_signal, ev_signal_start, ev_signal_stop);
726 this(int signum, Callback cb, ILoop loop = ev.d.loop)
729 ev_signal_set(ptr, signum);
732 mixin (wproperty!("int", "signum", RO));
736 class Child: IWatcher
739 mixin MWatcher!(Child, ev_child, ev_child_start, ev_child_stop);
741 this(int pid, bool trace, Callback cb, ILoop loop = ev.d.loop)
744 ev_child_set(ptr, pid, trace);
747 this(int pid, Callback cb, ILoop loop = ev.d.loop)
749 this(pid, 0, cb, loop);
752 this(Callback cb, ILoop loop = ev.d.loop)
754 this(0, 0, cb, loop);
757 mixin (wproperty!("int", "pid", RO));
759 mixin (wproperty!("int", "rpid"));
761 mixin (wproperty!("int", "rstatus"));
768 import std.string: toString, toStringz;
770 mixin MWatcher!(Stat, ev_stat, ev_stat_start, ev_stat_stop);
772 this(string path, tstamp interval, Callback cb, ILoop loop = ev.d.loop)
775 ev_stat_set(ptr, toStringz(path.dup), interval);
778 this(string path, Callback cb, ILoop loop = ev.d.loop)
780 this(path, 0.0, cb, loop);
785 ev_stat_stat(loop.ptr, ptr);
788 mixin (wproperty!("statdata", "attr", RO));
790 mixin (wproperty!("statdata", "prev", RO));
792 mixin (wproperty!("tstamp", "interval", RO));
795 void path(string path)
797 ptr.path = toStringz(path.dup);
803 return toString(ptr.path).dup;
811 mixin MWatcher!(Idle, ev_idle, ev_idle_start, ev_idle_stop);
813 this(Callback cb, ILoop loop = ev.d.loop)
821 class Prepare: IWatcher
824 mixin MWatcher!(Prepare, ev_prepare, ev_prepare_start, ev_prepare_stop);
826 this(Callback cb, ILoop loop = ev.d.loop)
834 class Check: IWatcher
837 mixin MWatcher!(Check, ev_check, ev_check_start, ev_check_stop);
839 this(Callback cb, ILoop loop = ev.d.loop)
847 class Embed: IWatcher
850 mixin MWatcher!(Embed, ev_embed, ev_embed_start, ev_embed_stop, false);
852 private struct WatcherData
854 void delegate(Embed, int) cb;
859 this(ILoop embeddedloop, Callback cb, ILoop loop = ev.d.loop)
862 ev_embed_set(ptr, embeddedloop.ptr);
863 this.other = embeddedloop;
868 ev_embed_sweep(loop.ptr, ptr);
871 void other(ILoop other)
874 ev_embed_set(ptr, other.ptr);
877 mixin (dproperty!("ILoop", "other", RO));
884 mixin MWatcher!(Fork, ev_fork, ev_fork_start, ev_fork_stop);
886 this(Callback cb, ILoop loop = ev.d.loop)