Overhaul formatting.
[mLib-python] / sel-timer.pyx
1 ### -*-pyrex-*-
2 ###
3 ### Timer selectors
4 ###
5 ### (c) 2005 Straylight/Edgeware
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the Python interface to mLib.
11 ###
12 ### mLib/Python is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
16 ###
17 ### mLib/Python is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with mLib/Python; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 cdef double _tvtofloat(timeval *tv):
27 return tv.tv_sec + (tv.tv_usec / 1000000)
28 cdef void _floattotv(timeval *tv, double t):
29 cdef double s, us
30 us = modf(t, &s)
31 tv.tv_sec = <int>s
32 tv.tv_usec = <int>(us * 1000000)
33
34 cdef class SelTimer:
35 cdef sel_timer t
36 cdef int _activep
37 cdef readonly double time
38 cdef _timer
39 def __cinit__(me, double when, timerproc = None, *hunoz, **hukairz):
40 cdef timeval tv
41 _floattotv(&tv, when)
42 sel_addtimer(&_sel, &me.t, &tv, _timerfunc, <void *>me)
43 me._activep = 1
44 me.time = when
45 me._timer = _checkcallable(timerproc, 'timer proc')
46 def __dealloc__(me):
47 if me._activep:
48 sel_rmtimer(&me.t)
49 property activep:
50 def __get__(me):
51 return _tobool(me._activep)
52 property timerproc:
53 def __get__(me):
54 return me._timer
55 def __set__(me, proc):
56 me._timer = _checkcallable(proc, 'timer proc')
57 def __del__(me):
58 me._timer = None
59 def kill(me):
60 if not me._activep:
61 raise ValueError, 'already dead'
62 sel_rmtimer(&me.t)
63 me._dead()
64 return me
65 cdef _dead(me):
66 me._activep = 0
67 me.dead()
68 def dead(me):
69 pass
70 def timer(me, now):
71 return _maybecall(me._timer, ())
72
73 cdef void _timerfunc(timeval *now, void *arg):
74 cdef SelTimer st
75 st = <SelTimer>arg
76 st._dead()
77 st.timer(_tvtofloat(now))
78
79 ###----- That's all, folks --------------------------------------------------