1 // -*- mode: C; indent-tabs-mode: nil; c-basic-offset: 2; -*-
5 #include "timerqueue.h"
7 PyObject
*OtkTimer_New(OtkTimeoutHandler handler
, OtkTimeoutData data
)
9 OtkTimer
*self
= PyObject_New(OtkTimer
, &OtkTimer_Type
);
11 assert(handler
); assert(data
);
12 self
->handler
= handler
;
14 self
->recur
= self
->timing
= False
;
16 return (PyObject
*)self
;
19 void OtkTimer_Start(OtkTimer
*self
)
21 gettimeofday(&(self
->start
), 0);
23 self
->end
.tv_sec
= self
->start
.tv_sec
+ self
->timeout
/ 1000;
24 self
->end
.tv_usec
= self
->start
.tv_usec
+ self
->timeout
% 1000 * 1000;
28 OtkTimerQueue_Add(self
);
32 void OtkTimer_Stop(OtkTimer
*self
)
36 OtkTimerQueue_Remove(self
);
44 static void otktimer_dealloc(OtkTimer
* self
)
47 // when this is called, the color has already been cleaned out of the cache
48 PyObject_Del((PyObject
*)self
);
51 static int otktimer_compare(OtkTimer
*t1
, OtkTimer
*t2
)
53 if (t1
->end
.tv_sec
== t2
->end
.tv_sec
&& t1
->end
.tv_usec
== t2
->end
.tv_usec
)
55 else if ((t1
->end
.tv_sec
< t2
->end
.tv_sec
) ||
56 (t1
->end
.tv_sec
== t2
->end
.tv_sec
&&
57 t1
->end
.tv_usec
< t2
->end
.tv_usec
))
63 PyTypeObject OtkTimer_Type
= {
64 PyObject_HEAD_INIT(NULL
)
69 (destructor
)otktimer_dealloc
, /*tp_dealloc*/
73 (cmpfunc
)otktimer_compare
, /*tp_compare*/