1 # This script generates a Python interface for an Apple Macintosh Manager.
2 # It uses the "bgen" package to generate C code.
3 # The function specifications are generated by scanning the mamager's header file,
4 # using the "scantools" package (customized for this particular manager).
8 # Declarations that change for each manager
9 MACHEADERFILE
= 'Events.h' # The Apple header file
10 MODNAME
= 'Evt' # The name of the module
11 OBJECTNAME
= 'Event' # The basic name of the objects used here
12 KIND
= 'Record' # Usually 'Ptr' or 'Handle'
14 # The following is *usually* unchanged but may still require tuning
15 MODPREFIX
= MODNAME
# The prefix for module-wide routines
16 OBJECTTYPE
= OBJECTNAME
+ KIND
# The C type used to represent them
17 OBJECTPREFIX
= MODPREFIX
+ 'Obj' # The prefix for object methods
18 INPUTFILE
= string
.lower(MODPREFIX
) + 'gen.py' # The file generated by the scanner
19 OUTPUTFILE
= MODNAME
+ "module.c" # The file generated by this program
21 from macsupport
import *
23 # Create the type objects
25 #WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX)
27 RgnHandle
= FakeType("(RgnHandle)0")
28 # XXXX Should be next, but this will break a lot of code...
29 # RgnHandle = OpaqueByValueType("RgnHandle", "OptResObj")
31 KeyMap
= ArrayOutputBufferType("KeyMap")
32 MacOSEventKind
= Type("MacOSEventKind", "h") # Old-style
33 MacOSEventMask
= Type("MacOSEventMask", "h") # Old-style
34 EventMask
= Type("EventMask", "h")
35 EventKind
= Type("EventKind", "h")
37 includestuff
= includestuff
+ """
38 #include <%s>""" % MACHEADERFILE
+ """
40 #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
43 class MyObjectDefinition(GlobalObjectDefinition
):
44 def outputCheckNewArg(self
):
45 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
46 def outputCheckConvertArg(self
):
47 OutLbrace("if (DlgObj_Check(v))")
48 Output("*p_itself = ((WindowObject *)v)->ob_itself;")
52 if (v == Py_None) { *p_itself = NULL; return 1; }
53 if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
56 # From here on it's basically all boiler plate...
58 # Create the generator groups and link them
59 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
)
60 ##object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
61 ##module.addobject(object)
63 # Create the generator classes used to populate the lists
64 Function
= OSErrFunctionGenerator
65 ##Method = OSErrMethodGenerator
67 # Create and populate the lists
72 # add the populated lists to the generator groups
73 # (in a different wordl the scan program would generate this)
74 for f
in functions
: module
.add(f
)
75 ##for f in methods: object.add(f)
77 WaitNextEvent_body
= """
82 Handle mouseregion = (Handle)0;
84 if (!PyArg_ParseTuple(_args, "hl|O&",
87 OptResObj_Convert, &mouseregion))
89 _rv = WaitNextEvent(eventMask,
92 (RgnHandle)mouseregion);
93 _res = Py_BuildValue("bO&",
95 PyMac_BuildEventRecord, &theEvent);
98 f
= ManualGenerator("WaitNextEvent", WaitNextEvent_body
);
99 f
.docstring
= lambda: "(EventMask eventMask, UInt32 sleep [,RegionHandle]) -> (Boolean _rv, EventRecord theEvent)"
102 # generate output (open the output file as late as possible)
103 SetOutputFileName(OUTPUTFILE
)