chiark / gitweb /
debian/control: Add Build-Depends for `dh-python'.
[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   """
36   SelTimer(WHEN, [timerproc = None])
37
38   Arrange to perform some action at time WHEN.
39   """
40   cdef sel_timer t
41   cdef int _activep
42   cdef readonly double time
43   cdef _timer
44   def __cinit__(me, double when, timerproc = None, *hunoz, **hukairz):
45     cdef timeval tv
46     _floattotv(&tv, when)
47     sel_addtimer(&_sel, &me.t, &tv, _timerfunc, <void *>me)
48     me._activep = 1
49     me.time = when
50     me._timer = _checkcallable(timerproc, 'timer proc')
51   def __dealloc__(me):
52     if me._activep:
53       sel_rmtimer(&me.t)
54   property activep:
55     """ST.activep -> BOOL: is the timer active?"""
56     def __get__(me):
57       return _tobool(me._activep)
58   property timerproc:
59     """ST.timerproc -> FUNC: call FUNC() when the timer pops"""
60     def __get__(me):
61       return me._timer
62     def __set__(me, proc):
63       me._timer = _checkcallable(proc, 'timer proc')
64     def __del__(me):
65       me._timer = None
66   def kill(me):
67     """ST.kill(): deactivate timer permanently"""
68     if not me._activep:
69       raise ValueError, 'already dead'
70     sel_rmtimer(&me.t)
71     me._dead()
72     return me
73   cdef _dead(me):
74     me._activep = 0
75     me.dead()
76   def dead(me):
77     """ST.dead(): called when timer is deactivated"""
78     pass
79   def timer(me, now):
80     """ST.timer(NOW): called when the timer pops"""
81     return _maybecall(me._timer, ())
82
83 cdef void _timerfunc(timeval *now, void *arg):
84   cdef SelTimer st
85   st = <SelTimer>arg
86   st._dead()
87   st.timer(_tvtofloat(now))
88
89 ###----- That's all, folks --------------------------------------------------