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>&runLoopTimerCallBack, &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
40
cdef class PyCFRunLoop:
43
def __new__(self, runLoop=None):
44
cdef CFTypeRef _runLoop
45
cdef EventLoopRef _evtLoop
47
_runLoop = CFRunLoopGetCurrent()
48
elif isinstance(runLoop, _CarbonEvt.EventLoopRef):
49
# XXX - I don't know how to get access to this
51
#if EventLoopRef_Convert(runLoop, &_evtLoop) == 0:
53
_evtLoop = GetCurrentEventLoop()
54
_runLoop = GetCFRunLoopFromEventLoop(_evtLoop)
56
if CFObj_Convert(runLoop, &_runLoop) == 0:
59
self.cf = CFObj_New(CFRetain(_runLoop))
65
cdef CFTypeRef _runLoop
66
if CFObj_Convert(self.cf, &_runLoop) == 0:
67
raise ValueError, "CFRunLoopReference is invalid"
68
CFRunLoopStop(_runLoop)
70
def currentMode(self):
71
cdef CFTypeRef _currentMode
72
cdef CFTypeRef _runLoop
73
if CFObj_Convert(self.cf, &_runLoop) == 0:
74
raise ValueError, "CFRunLoopReference is invalid"
75
_currentMode = CFRunLoopCopyCurrentMode(_runLoop)
76
if _currentMode == NULL:
78
return CFObj_New(_currentMode)
80
def addSocket(self, PyCFSocket socket not None):
81
cdef CFTypeRef _runLoop
82
if CFObj_Convert(self.cf, &_runLoop) == 0:
83
raise ValueError, "CFRunLoopReference is invalid"
84
CFRunLoopAddSource(_runLoop, socket.source, kCFRunLoopCommonModes)
86
def removeSocket(self, PyCFSocket socket not None):
87
cdef CFTypeRef _runLoop
88
if CFObj_Convert(self.cf, &_runLoop) == 0:
89
raise ValueError, "CFRunLoopReference is invalid"
90
CFRunLoopRemoveSource(_runLoop, socket.source, kCFRunLoopCommonModes)
92
def addTimer(self, PyCFRunLoopTimer timer not None):
93
cdef CFTypeRef _runLoop
94
if CFObj_Convert(self.cf, &_runLoop) == 0:
95
raise ValueError, "CFRunLoopReference is invalid"
96
CFRunLoopAddTimer(_runLoop, timer.cf, kCFRunLoopCommonModes)
98
def removeTimer(self, PyCFRunLoopTimer timer not None):
99
cdef CFTypeRef _runLoop
100
if CFObj_Convert(self.cf, &_runLoop) == 0:
101
raise ValueError, "CFRunLoopReference is invalid"
102
CFRunLoopRemoveTimer(_runLoop, timer.cf, kCFRunLoopCommonModes)