]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """\\r |
2 | Augment the "bgen" package with definitions that are useful on the Apple Macintosh.\r | |
3 | \r | |
4 | Intended usage is "from macsupport import *" -- this implies all bgen's goodies.\r | |
5 | """\r | |
6 | \r | |
7 | \r | |
8 | # Import everything from bgen (for ourselves as well as for re-export)\r | |
9 | from bgen import *\r | |
10 | \r | |
11 | \r | |
12 | # Simple types\r | |
13 | Boolean = Type("Boolean", "b")\r | |
14 | SignedByte = Type("SignedByte", "b")\r | |
15 | Size = Type("Size", "l")\r | |
16 | Style = Type("Style", "b")\r | |
17 | StyleParameter = Type("StyleParameter", "h")\r | |
18 | CharParameter = Type("CharParameter", "h")\r | |
19 | TextEncoding = Type("TextEncoding", "l")\r | |
20 | ByteCount = Type("ByteCount", "l")\r | |
21 | Duration = Type("Duration", "l")\r | |
22 | ByteOffset = Type("ByteOffset", "l")\r | |
23 | OptionBits = Type("OptionBits", "l")\r | |
24 | ItemCount = Type("ItemCount", "l")\r | |
25 | PBVersion = Type("PBVersion", "l")\r | |
26 | ScriptCode = Type("ScriptCode", "h")\r | |
27 | LangCode = Type("LangCode", "h")\r | |
28 | RegionCode = Type("RegionCode", "h")\r | |
29 | \r | |
30 | UInt8 = Type("UInt8", "b")\r | |
31 | SInt8 = Type("SInt8", "b")\r | |
32 | UInt16 = Type("UInt16", "H")\r | |
33 | SInt16 = Type("SInt16", "h")\r | |
34 | UInt32 = Type("UInt32", "l")\r | |
35 | SInt32 = Type("SInt32", "l")\r | |
36 | Float32 = Type("Float32", "f")\r | |
37 | \r | |
38 | wide = OpaqueByValueType("wide", "PyMac_Buildwide", "PyMac_Getwide")\r | |
39 | wide_ptr = OpaqueType("wide", "PyMac_Buildwide", "PyMac_Getwide")\r | |
40 | \r | |
41 | # Pascal strings\r | |
42 | ConstStr255Param = OpaqueArrayType("Str255", "PyMac_BuildStr255", "PyMac_GetStr255")\r | |
43 | Str255 = OpaqueArrayType("Str255", "PyMac_BuildStr255", "PyMac_GetStr255")\r | |
44 | StringPtr = OpaqueByValueType("StringPtr", "PyMac_BuildStr255", "PyMac_GetStr255")\r | |
45 | ConstStringPtr = StringPtr\r | |
46 | \r | |
47 | # File System Specifications\r | |
48 | FSSpec_ptr = OpaqueType("FSSpec", "PyMac_BuildFSSpec", "PyMac_GetFSSpec")\r | |
49 | FSSpec = OpaqueByValueStructType("FSSpec", "PyMac_BuildFSSpec", "PyMac_GetFSSpec")\r | |
50 | FSRef_ptr = OpaqueType("FSRef", "PyMac_BuildFSRef", "PyMac_GetFSRef")\r | |
51 | FSRef = OpaqueByValueStructType("FSRef", "PyMac_BuildFSRef", "PyMac_GetFSRef")\r | |
52 | \r | |
53 | # OSType and ResType: 4-byte character strings\r | |
54 | def OSTypeType(typename):\r | |
55 | return OpaqueByValueType(typename, "PyMac_BuildOSType", "PyMac_GetOSType")\r | |
56 | OSType = OSTypeType("OSType")\r | |
57 | ResType = OSTypeType("ResType")\r | |
58 | FourCharCode = OSTypeType("FourCharCode")\r | |
59 | \r | |
60 | # Version numbers\r | |
61 | NumVersion = OpaqueByValueType("NumVersion", "PyMac_BuildNumVersion", "BUG")\r | |
62 | \r | |
63 | # Handles (always resources in our case)\r | |
64 | Handle = OpaqueByValueType("Handle", "ResObj")\r | |
65 | MenuHandle = OpaqueByValueType("MenuHandle", "MenuObj")\r | |
66 | MenuRef = MenuHandle\r | |
67 | ControlHandle = OpaqueByValueType("ControlHandle", "CtlObj")\r | |
68 | ControlRef = ControlHandle\r | |
69 | \r | |
70 | # Windows and Dialogs\r | |
71 | WindowPtr = OpaqueByValueType("WindowPtr", "WinObj")\r | |
72 | WindowRef = WindowPtr\r | |
73 | DialogPtr = OpaqueByValueType("DialogPtr", "DlgObj")\r | |
74 | DialogRef = DialogPtr\r | |
75 | ExistingWindowPtr = OpaqueByValueType("WindowPtr", "WinObj_WhichWindow", "BUG")\r | |
76 | ExistingDialogPtr = OpaqueByValueType("DialogPtr", "DlgObj_WhichDialog", "BUG")\r | |
77 | \r | |
78 | # NULL pointer passed in as optional storage -- not present in Python version\r | |
79 | NullStorage = FakeType("(void *)0")\r | |
80 | \r | |
81 | # More standard datatypes\r | |
82 | Fixed = OpaqueByValueType("Fixed", "PyMac_BuildFixed", "PyMac_GetFixed")\r | |
83 | \r | |
84 | # Quickdraw data types\r | |
85 | Rect = Rect_ptr = OpaqueType("Rect", "PyMac_BuildRect", "PyMac_GetRect")\r | |
86 | Point = OpaqueByValueType("Point", "PyMac_BuildPoint", "PyMac_GetPoint")\r | |
87 | Point_ptr = OpaqueType("Point", "PyMac_BuildPoint", "PyMac_GetPoint")\r | |
88 | \r | |
89 | # Event records\r | |
90 | EventRecord = OpaqueType("EventRecord", "PyMac_BuildEventRecord", "PyMac_GetEventRecord")\r | |
91 | EventRecord_ptr = EventRecord\r | |
92 | \r | |
93 | # CoreFoundation datatypes\r | |
94 | CFTypeRef = OpaqueByValueType("CFTypeRef", "CFTypeRefObj")\r | |
95 | CFStringRef = OpaqueByValueType("CFStringRef", "CFStringRefObj")\r | |
96 | CFMutableStringRef = OpaqueByValueType("CFMutableStringRef", "CFMutableStringRefObj")\r | |
97 | CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")\r | |
98 | CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")\r | |
99 | CFDictionaryRef = OpaqueByValueType("CFDictionaryRef", "CFDictionaryRefObj")\r | |
100 | CFMutableDictionaryRef = OpaqueByValueType("CFMutableDictionaryRef", "CFMutableDictionaryRefObj")\r | |
101 | CFURLRef = OpaqueByValueType("CFURLRef", "CFURLRefObj")\r | |
102 | OptionalCFURLRef = OpaqueByValueType("CFURLRef", "OptionalCFURLRefObj")\r | |
103 | \r | |
104 | # OSErr is special because it is turned into an exception\r | |
105 | # (Could do this with less code using a variant of mkvalue("O&")?)\r | |
106 | class OSErrType(Type):\r | |
107 | def errorCheck(self, name):\r | |
108 | Output("if (%s != noErr) return PyMac_Error(%s);", name, name)\r | |
109 | self.used = 1\r | |
110 | OSErr = OSErrType("OSErr", 'h')\r | |
111 | OSStatus = OSErrType("OSStatus", 'l')\r | |
112 | \r | |
113 | \r | |
114 | # Various buffer types\r | |
115 | \r | |
116 | InBuffer = VarInputBufferType('char', 'long', 'l') # (buf, len)\r | |
117 | UcharInBuffer = VarInputBufferType('unsigned char', 'long', 'l') # (buf, len)\r | |
118 | OptionalInBuffer = OptionalVarInputBufferType('char', 'long', 'l') # (buf, len)\r | |
119 | \r | |
120 | InOutBuffer = HeapInputOutputBufferType('char', 'long', 'l') # (inbuf, outbuf, len)\r | |
121 | VarInOutBuffer = VarHeapInputOutputBufferType('char', 'long', 'l') # (inbuf, outbuf, &len)\r | |
122 | \r | |
123 | OutBuffer = HeapOutputBufferType('char', 'long', 'l') # (buf, len)\r | |
124 | VarOutBuffer = VarHeapOutputBufferType('char', 'long', 'l') # (buf, &len)\r | |
125 | VarVarOutBuffer = VarVarHeapOutputBufferType('char', 'long', 'l') # (buf, len, &len)\r | |
126 | \r | |
127 | # Unicode arguments sometimes have reversed len, buffer (don't understand why Apple did this...)\r | |
128 | class VarUnicodeInputBufferType(VarInputBufferType):\r | |
129 | \r | |
130 | def getargsFormat(self):\r | |
131 | return "u#"\r | |
132 | \r | |
133 | class VarUnicodeReverseInputBufferType(ReverseInputBufferMixin, VarUnicodeInputBufferType):\r | |
134 | pass\r | |
135 | \r | |
136 | UnicodeInBuffer = VarUnicodeInputBufferType('UniChar', 'UniCharCount', 'l')\r | |
137 | UnicodeReverseInBuffer = VarUnicodeReverseInputBufferType('UniChar', 'UniCharCount', 'l')\r | |
138 | UniChar_ptr = InputOnlyType("UniCharPtr", "u")\r | |
139 | \r | |
140 | \r | |
141 | # Predefine various pieces of program text to be passed to Module() later:\r | |
142 | \r | |
143 | # Stuff added immediately after the system include files\r | |
144 | includestuff = """\r | |
145 | #include "pymactoolbox.h"\r | |
146 | \r | |
147 | /* Macro to test whether a weak-loaded CFM function exists */\r | |
148 | #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\\\r | |
149 | PyErr_SetString(PyExc_NotImplementedError, \\\r | |
150 | "Not available in this shared library/OS version"); \\\r | |
151 | return NULL; \\\r | |
152 | }} while(0)\r | |
153 | \r | |
154 | """\r | |
155 | \r | |
156 | # Stuff added just before the module's init function\r | |
157 | finalstuff = """\r | |
158 | """\r | |
159 | \r | |
160 | # Stuff added inside the module's init function\r | |
161 | initstuff = """\r | |
162 | """\r | |
163 | \r | |
164 | \r | |
165 | # Generator classes with a twist -- if the function returns OSErr,\r | |
166 | # its mode is manipulated so that it turns into an exception or disappears\r | |
167 | # (and its name is changed to _err, for documentation purposes).\r | |
168 | # This requires that the OSErr type (defined above) has a non-trivial\r | |
169 | # errorCheck method.\r | |
170 | class OSErrMixIn:\r | |
171 | "Mix-in class to treat OSErr/OSStatus return values special"\r | |
172 | def makereturnvar(self):\r | |
173 | if self.returntype.__class__ == OSErrType:\r | |
174 | return Variable(self.returntype, "_err", ErrorMode)\r | |
175 | else:\r | |
176 | return Variable(self.returntype, "_rv", OutMode)\r | |
177 | \r | |
178 | class OSErrFunctionGenerator(OSErrMixIn, FunctionGenerator): pass\r | |
179 | class OSErrMethodGenerator(OSErrMixIn, MethodGenerator): pass\r | |
180 | \r | |
181 | class WeakLinkMixIn:\r | |
182 | "Mix-in to test the function actually exists (!= NULL) before calling"\r | |
183 | \r | |
184 | def precheck(self):\r | |
185 | Output('#ifndef %s', self.name)\r | |
186 | Output('PyMac_PRECHECK(%s);', self.name)\r | |
187 | Output('#endif')\r | |
188 | \r | |
189 | class WeakLinkFunctionGenerator(WeakLinkMixIn, FunctionGenerator): pass\r | |
190 | class WeakLinkMethodGenerator(WeakLinkMixIn, MethodGenerator): pass\r | |
191 | class OSErrWeakLinkFunctionGenerator(OSErrMixIn, WeakLinkMixIn, FunctionGenerator): pass\r | |
192 | class OSErrWeakLinkMethodGenerator(OSErrMixIn, WeakLinkMixIn, MethodGenerator): pass\r | |
193 | \r | |
194 | class MacModule(Module):\r | |
195 | "Subclass which gets the exception initializer from macglue.c"\r | |
196 | def exceptionInitializer(self):\r | |
197 | return "PyMac_GetOSErrException()"\r |