keep the asserts for !debug builds..
[mikachu/openbox.git] / otk_c / timer.c
1 // -*- mode: C; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "../config.h"
4 #include "timer.h"
5 #include "timerqueue.h"
6
7 PyObject *OtkTimer_New(OtkTimeoutHandler handler, OtkTimeoutData data)
8 {
9   OtkTimer *self = PyObject_New(OtkTimer, &OtkTimer_Type);
10
11   assert(handler); assert(data);
12   self->handler = handler;
13   self->data = data;
14   self->recur = self->timing = False;
15
16   return (PyObject*)self;
17 }
18
19 void OtkTimer_Start(OtkTimer *self)
20 {
21   gettimeofday(&(self->start), 0);
22
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;
25
26   if (! self->timing) {
27     self->timing = True;
28     OtkTimerQueue_Add(self);
29   }
30 }
31
32 void OtkTimer_Stop(OtkTimer *self)
33 {
34   if (self->timing) {
35     self->timing = False;
36     OtkTimerQueue_Remove(self);
37   }
38 }
39
40
41
42
43
44 static void otktimer_dealloc(OtkTimer* self)
45 {
46   OtkTimer_Stop(self);  
47   // when this is called, the color has already been cleaned out of the cache
48   PyObject_Del((PyObject*)self);
49 }
50
51 static int otktimer_compare(OtkTimer *t1, OtkTimer *t2)
52 {
53   if (t1->end.tv_sec == t2->end.tv_sec && t1->end.tv_usec == t2->end.tv_usec)
54     return 0;
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))
58     return -1;
59   else
60     return 1;
61 }
62
63 PyTypeObject OtkTimer_Type = {
64   PyObject_HEAD_INIT(NULL)
65   0,
66   "OtkTimer",
67   sizeof(OtkTimer),
68   0,
69   (destructor)otktimer_dealloc, /*tp_dealloc*/
70   0,                            /*tp_print*/
71   0,                            /*tp_getattr*/
72   0,                            /*tp_setattr*/
73   (cmpfunc)otktimer_compare,    /*tp_compare*/
74   0,                            /*tp_repr*/
75   0,                            /*tp_as_number*/
76   0,                            /*tp_as_sequence*/
77   0,                            /*tp_as_mapping*/
78   0,                            /*tp_hash */
79 };