3
# possibly figure out how to subclass these or something
5
cdef class PyCFRunLoopTimer:
6
cdef CFRunLoopTimerRef cf
7
cdef public object callout
8
cdef CFRunLoopTimerContext context
10
def __new__(self, double fireDate, double interval, callout):
11
self.callout = callout
12
self.context.version = 0
13
self.context.info = <void *>self
14
self.context.retain = NULL
15
self.context.release = NULL
16
self.context.copyDescription = NULL
17
self.cf = CFRunLoopTimerCreate(kCFAllocatorDefault, fireDate, interval, 0, 0, <CFRunLoopTimerCallBack>&gilRunLoopTimerCallBack, &self.context)
19
raise ValueError("Invalid Socket")
21
def getNextFireDate(self):
22
return CFRunLoopTimerGetNextFireDate(self.cf)
24
def setNextFireDate(self, double fireDate):
25
CFRunLoopTimerSetNextFireDate(self.cf, fireDate)
28
CFRunLoopTimerInvalidate(self.cf)
30
def __dealloc__(self):
34
cdef void runLoopTimerCallBack(CFRunLoopTimerRef timer, void *info):
35
cdef PyCFRunLoopTimer obj
36
obj = <PyCFRunLoopTimer>info
43
cdef void gilRunLoopTimerCallBack(CFRunLoopTimerRef timer, void *info):
44
cdef PyGILState_STATE gil
45
gil = PyGILState_Ensure()
46
runLoopTimerCallBack(timer, info)
47
PyGILState_Release(gil)
49
cdef class PyCFRunLoop:
52
def __new__(self, runLoop=None):
53
cdef CFTypeRef _runLoop
55
_runLoop = CFRunLoopGetCurrent()
57
if CFObj_Convert(runLoop, &_runLoop) == 0:
60
# this is going to leak a reference
61
self.cf = CFObj_New(CFRetain(_runLoop))
67
cdef CFTypeRef _runLoop
68
if CFObj_Convert(self.cf, &_runLoop) == 0:
69
raise ValueError, "CFRunLoopReference is invalid"
70
CFRunLoopStop(_runLoop)
72
def currentMode(self):
73
cdef CFTypeRef _currentMode
74
cdef CFTypeRef _runLoop
75
if CFObj_Convert(self.cf, &_runLoop) == 0:
76
raise ValueError, "CFRunLoopReference is invalid"
77
_currentMode = CFRunLoopCopyCurrentMode(_runLoop)
78
if _currentMode == NULL:
80
return CFObj_New(_currentMode)
82
def addSocket(self, PyCFSocket socket not None):
83
cdef CFTypeRef _runLoop
84
if CFObj_Convert(self.cf, &_runLoop) == 0:
85
raise ValueError, "CFRunLoopReference is invalid"
86
CFRunLoopAddSource(_runLoop, socket.source, kCFRunLoopCommonModes)
88
def removeSocket(self, PyCFSocket socket not None):
89
cdef CFTypeRef _runLoop
90
if CFObj_Convert(self.cf, &_runLoop) == 0:
91
raise ValueError, "CFRunLoopReference is invalid"
92
CFRunLoopRemoveSource(_runLoop, socket.source, kCFRunLoopCommonModes)
94
def addTimer(self, PyCFRunLoopTimer timer not None):
95
cdef CFTypeRef _runLoop
96
if CFObj_Convert(self.cf, &_runLoop) == 0:
97
raise ValueError, "CFRunLoopReference is invalid"
98
CFRunLoopAddTimer(_runLoop, timer.cf, kCFRunLoopCommonModes)
100
def removeTimer(self, PyCFRunLoopTimer timer not None):
101
cdef CFTypeRef _runLoop
102
if CFObj_Convert(self.cf, &_runLoop) == 0:
103
raise ValueError, "CFRunLoopReference is invalid"
104
CFRunLoopRemoveTimer(_runLoop, timer.cf, kCFRunLoopCommonModes)