Commit | Line | Data |
---|---|---|
5b1830f3 MW |
1 | ### -*-pyrex-*- |
2 | ### | |
3 | ### Timer selectors | |
4 | ### | |
5 | ### (c) 2005 Straylight/Edgeware | |
6 | ### | |
579d0169 | 7 | |
5b1830f3 MW |
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. | |
579d0169 | 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) | |
c34ff3c5 MW |
31 | tv.tv_sec = <int>s |
32 | tv.tv_usec = <int>(us * 1000000) | |
579d0169 | 33 | |
34 | cdef class SelTimer: | |
addc0c37 MW |
35 | """ |
36 | SelTimer(WHEN, [timerproc = None]) | |
37 | ||
38 | Arrange to perform some action at time WHEN. | |
39 | """ | |
579d0169 | 40 | cdef sel_timer t |
41 | cdef int _activep | |
42 | cdef readonly double time | |
43 | cdef _timer | |
376ad06d | 44 | def __cinit__(me, double when, timerproc = None, *hunoz, **hukairz): |
579d0169 | 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: | |
addc0c37 | 55 | """ST.activep -> BOOL: is the timer active?""" |
579d0169 | 56 | def __get__(me): |
57 | return _tobool(me._activep) | |
58 | property timerproc: | |
addc0c37 | 59 | """ST.timerproc -> FUNC: call FUNC() when the timer pops""" |
579d0169 | 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): | |
addc0c37 | 67 | """ST.kill(): deactivate timer permanently""" |
579d0169 | 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): | |
addc0c37 | 77 | """ST.dead(): called when timer is deactivated""" |
579d0169 | 78 | pass |
79 | def timer(me, now): | |
addc0c37 | 80 | """ST.timer(NOW): called when the timer pops""" |
579d0169 | 81 | return _maybecall(me._timer, ()) |
82 | ||
b51b6cf0 | 83 | cdef void _timerfunc(timeval *now, void *arg): |
579d0169 | 84 | cdef SelTimer st |
85 | st = <SelTimer>arg | |
86 | st._dead() | |
87 | st.timer(_tvtofloat(now)) | |
88 | ||
5b1830f3 | 89 | ###----- That's all, folks -------------------------------------------------- |