Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52e9045

Browse files
committed
Carbon Event Manager module donated by Donovan Preston. Checked in as I received them (except for namechange), these will not work as-is, that'll be fixed in a later checkin.
1 parent 537a69f commit 52e9045

2 files changed

Lines changed: 332 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# IBCarbonscan.py
2+
3+
import sys
4+
import os
5+
import string
6+
import MacOS
7+
8+
BGENDIR= '/Users/dsp/Documents/python/dist/src/Tools/bgen/bgen'
9+
sys.path.append(BGENDIR)
10+
11+
from bgenlocations import TOOLBOXDIR
12+
13+
from scantools import Scanner, Scanner_OSX
14+
15+
def main():
16+
print "---Scanning CarbonEvents.h---"
17+
input = ["CarbonEvents.h"]
18+
output = "CarbonEventsgen.py"
19+
defsoutput = TOOLBOXDIR + "CarbonEvents.py"
20+
scanner = CarbonEvents_Scanner(input, output, defsoutput)
21+
scanner.scan()
22+
scanner.close()
23+
print "--done scanning, importing--"
24+
import CarbonEventssupport
25+
print "done"
26+
27+
RefObjectTypes = ["EventRef",
28+
"EventQueueRef",
29+
"EventLoopRef",
30+
"EventLoopTimerRef",
31+
"EventHandlerRef",
32+
"EventHandlerCallRef",
33+
"EventTargetRef",
34+
"EventHotKeyRef",
35+
]
36+
37+
class CarbonEvents_Scanner(Scanner):
38+
def destination(self, type, name, arglist):
39+
classname = "CarbonEventsFunction"
40+
listname = "functions"
41+
if arglist:
42+
t, n, m = arglist[0]
43+
print "*********", t,
44+
if t in RefObjectTypes and m == "InMode":
45+
print "method"
46+
classname = "CarbonEventsMethod"
47+
listname = t + "methods"
48+
else:
49+
print "not method"
50+
return classname, listname
51+
52+
def makeblacklistnames(self):
53+
return [
54+
"MacCreateEvent",
55+
"TrackMouseLocationWithOptions",
56+
"TrackMouseLocation",
57+
"TrackMouseRegion",
58+
"RegisterToolboxObjectClass",
59+
"UnregisterToolboxObjectClass",
60+
"ProcessHICommand",
61+
"GetCFRunLoopFromEventLoop",
62+
63+
"InvokeEventHandlerUPP",
64+
"InvokeEventComparatorUPP",
65+
"InvokeEventLoopTimerUPP",
66+
67+
# Wrote by hand
68+
"InstallEventHandler",
69+
"RunApplicationEventLoop",
70+
71+
# Write by hand?
72+
"GetEventParameter",
73+
"FlushSpecificEventsFromQueue",
74+
"FindSpecificEventInQueue",
75+
"InstallEventLoopTimer",
76+
77+
# Don't do these because they require a CFRelease
78+
"CreateTypeStringWithOSType",
79+
"CopyEvent",
80+
]
81+
82+
# def makeblacklisttypes(self):
83+
# return ["EventComparatorUPP",
84+
# "EventLoopTimerUPP",
85+
# #"EventHandlerUPP",
86+
# "EventComparatorProcPtr",
87+
# "EventLoopTimerProcPtr",
88+
# "EventHandlerProcPtr",
89+
# ]
90+
if __name__ == "__main__":
91+
main()
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
# IBCarbonsupport.py
2+
3+
from macsupport import *
4+
5+
from CarbonEventsscan import RefObjectTypes
6+
7+
# where should this go? macsupport.py?
8+
CFStringRef = OpaqueByValueType('CFStringRef')
9+
10+
for typ in RefObjectTypes:
11+
execstr = "%(name)s = OpaqueByValueType('%(name)s')" % {"name": typ}
12+
exec execstr
13+
14+
# these types will have no methods and will merely be opaque blobs
15+
# should write getattr and setattr for them?
16+
17+
StructObjectTypes = ["EventTypeSpec",
18+
"HIPoint",
19+
"HICommand",
20+
"EventHotKeyID",
21+
]
22+
23+
for typ in StructObjectTypes:
24+
execstr = "%(name)s = OpaqueType('%(name)s')" % {"name": typ}
25+
exec execstr
26+
27+
EventTypeSpec_ptr = OpaqueType("EventTypeSpec *", "EventTypeSpec")
28+
29+
# is this the right type for the void * in GetEventParameter
30+
#void_ptr = FixedInputBufferType(1024)
31+
void_ptr = stringptr
32+
# here are some types that are really other types
33+
34+
EventTime = double
35+
EventTimeout = EventTime
36+
EventTimerInterval = EventTime
37+
EventAttributes = UInt32
38+
EventParamName = OSType
39+
EventParamType = OSType
40+
EventPriority = SInt16
41+
EventMask = UInt16
42+
43+
EventComparatorUPP = FakeType("(EventComparatorUPP)0")
44+
EventLoopTimerUPP = FakeType("(EventLoopTimerUPP)0")
45+
EventHandlerUPP = FakeType("(EventHandlerUPP)0")
46+
EventHandlerUPP = FakeType("(EventHandlerUPP)0")
47+
EventComparatorProcPtr = FakeType("(EventComparatorProcPtr)0")
48+
EventLoopTimerProcPtr = FakeType("(EventLoopTimerProcPtr)0")
49+
EventHandlerProcPtr = FakeType("(EventHandlerProcPtr)0")
50+
51+
CarbonEventsFunction = OSErrFunctionGenerator
52+
CarbonEventsMethod = OSErrMethodGenerator
53+
54+
includestuff = """
55+
#include <Carbon/Carbon.h>
56+
#include "macglue.h"
57+
58+
#define USE_MAC_MP_MULTITHREADING 1
59+
60+
#if USE_MAC_MP_MULTITHREADING
61+
static PyThreadState *_save;
62+
static MPCriticalRegionID reentrantLock;
63+
#endif /* USE_MAC_MP_MULTITHREADING */
64+
65+
extern int CFStringRef_New(CFStringRef *);
66+
67+
extern int CFStringRef_Convert(PyObject *, CFStringRef *);
68+
extern int CFBundleRef_Convert(PyObject *, CFBundleRef *);
69+
70+
int EventTargetRef_Convert(PyObject *, EventTargetRef *);
71+
PyObject *EventHandlerCallRef_New(EventHandlerCallRef itself);
72+
PyObject *EventRef_New(EventRef itself);
73+
74+
/********** EventTypeSpec *******/
75+
static PyObject*
76+
EventTypeSpec_New(EventTypeSpec *in)
77+
{
78+
return Py_BuildValue("ll", in->eventClass, in->eventKind);
79+
}
80+
81+
static int
82+
EventTypeSpec_Convert(PyObject *v, EventTypeSpec *out)
83+
{
84+
if (PyArg_ParseTuple(v, "ll", &(out->eventClass), &(out->eventKind)))
85+
return 1;
86+
return NULL;
87+
}
88+
89+
/********** end EventTypeSpec *******/
90+
91+
/********** HIPoint *******/
92+
93+
static PyObject*
94+
HIPoint_New(HIPoint *in)
95+
{
96+
return Py_BuildValue("ff", in->x, in->y);
97+
}
98+
99+
static int
100+
HIPoint_Convert(PyObject *v, HIPoint *out)
101+
{
102+
if (PyArg_ParseTuple(v, "ff", &(out->x), &(out->y)))
103+
return 1;
104+
return NULL;
105+
}
106+
107+
/********** end HIPoint *******/
108+
109+
/********** EventHotKeyID *******/
110+
111+
static PyObject*
112+
EventHotKeyID_New(EventHotKeyID *in)
113+
{
114+
return Py_BuildValue("ll", in->signature, in->id);
115+
}
116+
117+
static int
118+
EventHotKeyID_Convert(PyObject *v, EventHotKeyID *out)
119+
{
120+
if (PyArg_ParseTuple(v, "ll", &out->signature, &out->id))
121+
return 1;
122+
return NULL;
123+
}
124+
125+
/********** end EventHotKeyID *******/
126+
127+
/******** handlecommand ***********/
128+
129+
pascal OSStatus CarbonEvents_HandleCommand(EventHandlerCallRef handlerRef, Even
130+
tRef event, void *outPyObject) {
131+
PyObject *retValue;
132+
int status;
133+
134+
#if USE_MAC_MP_MULTITHREADING
135+
MPEnterCriticalRegion(reentrantLock, kDurationForever);
136+
PyEval_RestoreThread(_save);
137+
#endif /* USE_MAC_MP_MULTITHREADING */
138+
139+
retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHand
140+
lerCallRef_New, handlerRef, EventRef_New, event);
141+
status = PyInt_AsLong(retValue);
142+
143+
#if USE_MAC_MP_MULTITHREADING
144+
_save = PyEval_SaveThread();
145+
MPExitCriticalRegion(reentrantLock);
146+
#endif /* USE_MAC_MP_MULTITHREADING */
147+
148+
return status;
149+
}
150+
151+
/******** end handlecommand ***********/
152+
153+
"""
154+
155+
module = MacModule('CarbonEvents', 'CarbonEvents', includestuff, finalstuff, in
156+
itstuff)
157+
158+
#class CFReleaserObj(GlobalObjectDefinition):
159+
# def outputFreeIt(self, name):
160+
# Output("CFRelease(%s);" % name)
161+
162+
for typ in RefObjectTypes:
163+
execstr = typ + 'object = GlobalObjectDefinition(typ)'
164+
exec execstr
165+
module.addobject(eval(typ + 'object'))
166+
167+
functions = []
168+
for typ in RefObjectTypes: ## go thru all ObjectTypes as defined in CarbonEvent
169+
sscan.py
170+
# initialize the lists for carbongen to fill
171+
execstr = typ + 'methods = []'
172+
exec execstr
173+
174+
execfile('CarbonEventsgen.py')
175+
176+
for f in functions: module.add(f) # add all the functions carboneventsgen
177+
put in the list
178+
179+
for typ in RefObjectTypes: ## go thru all ObjectT
180+
ypes as defined in CarbonEventsscan.py
181+
methods = eval(typ + 'methods') ## get a reference to the method list
182+
from the main namespace
183+
obj = eval(typ + 'object') ## get a reference to the obj
184+
ect
185+
for m in methods: obj.add(m) ## add each method in the list to the o
186+
bject
187+
188+
installeventhandler = """
189+
EventTypeSpec inSpec;
190+
PyObject *callback;
191+
EventHandlerRef outRef;
192+
OSStatus _err;
193+
EventHandlerUPP event;
194+
195+
if (!PyArg_ParseTuple(_args, "O&O", EventTypeSpec_Convert, &inSpec, &callback))
196+
return NULL;
197+
198+
event = NewEventHandlerUPP(CarbonEvents_HandleCommand);
199+
_err = InstallEventHandler(_self->ob_itself, event, 1, &inSpec, (void *)callbac
200+
k, &outRef);
201+
if (_err != noErr) return PyMac_Error(_err);
202+
203+
return Py_BuildValue("l", outRef);
204+
"""
205+
206+
f = ManualGenerator("InstallEventHandler", installeventhandler);
207+
f.docstring = lambda: "(EventTargetRef inTarget, EventTypeSpec inSpec, Method c
208+
allback) -> (EventHandlerRef outRef)"
209+
EventTargetRefobject.add(f)
210+
211+
runappeventloop = """
212+
#if USE_MAC_MP_MULTITHREADING
213+
if (MPCreateCriticalRegion(&reentrantLock) != noErr) {
214+
printf("lock failure\n");
215+
return NULL;
216+
}
217+
_save = PyEval_SaveThread();
218+
#endif /* USE_MAC_MP_MULTITHREADING */
219+
220+
RunApplicationEventLoop();
221+
222+
#if USE_MAC_MP_MULTITHREADING
223+
PyEval_RestoreThread(_save);
224+
225+
MPDeleteCriticalRegion(reentrantLock);
226+
#endif /* USE_MAC_MP_MULTITHREADING */
227+
228+
Py_INCREF(Py_None);
229+
230+
return Py_None;
231+
"""
232+
233+
f = ManualGenerator("RunApplicationEventLoop", runappeventloop);
234+
f.docstring = lambda: "() -> ()"
235+
module.add(f)
236+
237+
SetOutputFileName('_CarbonEvents.c')
238+
module.generate()
239+
240+
import os
241+
os.system("python setup.py build")

0 commit comments

Comments
 (0)