| 1 | n/a | # This script generates a Python interface for an Apple Macintosh Manager. |
|---|
| 2 | n/a | # It uses the "bgen" package to generate C code. |
|---|
| 3 | n/a | # The function specifications are generated by scanning the mamager's header file, |
|---|
| 4 | n/a | # using the "scantools" package (customized for this particular manager). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | import string |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | # Declarations that change for each manager |
|---|
| 9 | n/a | MACHEADERFILE = 'QDOffscreen.h' # The Apple header file |
|---|
| 10 | n/a | MODNAME = '_Qdoffs' # The name of the module |
|---|
| 11 | n/a | OBJECTNAME = 'GWorld' # The basic name of the objects used here |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | # The following is *usually* unchanged but may still require tuning |
|---|
| 14 | n/a | MODPREFIX = 'Qdoffs' # The prefix for module-wide routines |
|---|
| 15 | n/a | OBJECTTYPE = OBJECTNAME + 'Ptr' # The C type used to represent them |
|---|
| 16 | n/a | OBJECTPREFIX = OBJECTNAME + 'Obj' # The prefix for object methods |
|---|
| 17 | n/a | INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner |
|---|
| 18 | n/a | #EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions |
|---|
| 19 | n/a | OUTPUTFILE = MODNAME + "module.c" # The file generated by this program |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | from macsupport import * |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | # Create the type objects |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | GWorldPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) |
|---|
| 26 | n/a | GWorldFlags = Type("GWorldFlags", "l") |
|---|
| 27 | n/a | GDHandle = OpaqueByValueType("GDHandle", "ResObj") |
|---|
| 28 | n/a | OptGDHandle = OpaqueByValueType("GDHandle", "OptResObj") |
|---|
| 29 | n/a | CTabHandle = OpaqueByValueType("CTabHandle", "OptResObj") |
|---|
| 30 | n/a | PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj") |
|---|
| 31 | n/a | PixMapHandle = OpaqueByValueType("PixMapHandle", "ResObj") |
|---|
| 32 | n/a | CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj") |
|---|
| 33 | n/a | GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj") |
|---|
| 34 | n/a | QDErr = OSErrType("QDErr", 'h') |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | includestuff = includestuff + """ |
|---|
| 37 | n/a | #include <Carbon/Carbon.h> |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | #ifdef USE_TOOLBOX_OBJECT_GLUE |
|---|
| 40 | n/a | extern PyObject *_GWorldObj_New(GWorldPtr); |
|---|
| 41 | n/a | extern int _GWorldObj_Convert(PyObject *, GWorldPtr *); |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | #define GWorldObj_New _GWorldObj_New |
|---|
| 44 | n/a | #define GWorldObj_Convert _GWorldObj_Convert |
|---|
| 45 | n/a | #endif |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | #define as_GrafPtr(gworld) ((GrafPtr)(gworld)) |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | """ |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | initstuff = initstuff + """ |
|---|
| 52 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(GWorldPtr, GWorldObj_New); |
|---|
| 53 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(GWorldPtr, GWorldObj_Convert); |
|---|
| 54 | n/a | """ |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 57 | n/a | # XXXX Should inherit from GrafPtr? |
|---|
| 58 | n/a | def outputCheckNewArg(self): |
|---|
| 59 | n/a | Output("if (itself == NULL) return PyMac_Error(resNotFound);") |
|---|
| 60 | n/a | ## def outputInitStructMembers(self): |
|---|
| 61 | n/a | ## GlobalObjectDefinition.outputInitStructMembers(self) |
|---|
| 62 | n/a | ## Output("SetWRefCon(itself, (long)it);") |
|---|
| 63 | n/a | ## def outputCheckConvertArg(self): |
|---|
| 64 | n/a | ## OutLbrace("if (DlgObj_Check(v))") |
|---|
| 65 | n/a | ## Output("*p_itself = ((WindowObject *)v)->ob_itself;") |
|---|
| 66 | n/a | ## Output("return 1;") |
|---|
| 67 | n/a | ## OutRbrace() |
|---|
| 68 | n/a | ## Out(""" |
|---|
| 69 | n/a | ## if (v == Py_None) { *p_itself = NULL; return 1; } |
|---|
| 70 | n/a | ## if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; } |
|---|
| 71 | n/a | ## """) |
|---|
| 72 | n/a | def outputFreeIt(self, itselfname): |
|---|
| 73 | n/a | Output("DisposeGWorld(%s);", itselfname) |
|---|
| 74 | n/a | # From here on it's basically all boiler plate... |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | # Create the generator groups and link them |
|---|
| 77 | n/a | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) |
|---|
| 78 | n/a | object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
|---|
| 79 | n/a | module.addobject(object) |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | # Create the generator classes used to populate the lists |
|---|
| 83 | n/a | Function = OSErrWeakLinkFunctionGenerator |
|---|
| 84 | n/a | Method = OSErrWeakLinkMethodGenerator |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | # Create and populate the lists |
|---|
| 87 | n/a | functions = [] |
|---|
| 88 | n/a | methods = [] |
|---|
| 89 | n/a | execfile(INPUTFILE) |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | # A method to convert a GWorldPtr to a GrafPtr |
|---|
| 92 | n/a | f = Method(GrafPtr, 'as_GrafPtr', (GWorldPtr, 'p', InMode)) |
|---|
| 93 | n/a | methods.append(f) |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | # |
|---|
| 96 | n/a | # Manual generator: get data out of a pixmap |
|---|
| 97 | n/a | pixmapgetbytes_body = """ |
|---|
| 98 | n/a | PixMapHandle pm; |
|---|
| 99 | n/a | int from, length; |
|---|
| 100 | n/a | char *cp; |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | if ( !PyArg_ParseTuple(_args, "O&ii", ResObj_Convert, &pm, &from, &length) ) |
|---|
| 103 | n/a | return NULL; |
|---|
| 104 | n/a | cp = GetPixBaseAddr(pm)+from; |
|---|
| 105 | n/a | _res = PyString_FromStringAndSize(cp, length); |
|---|
| 106 | n/a | return _res; |
|---|
| 107 | n/a | """ |
|---|
| 108 | n/a | f = ManualGenerator("GetPixMapBytes", pixmapgetbytes_body) |
|---|
| 109 | n/a | f.docstring = lambda: """(pixmap, int start, int size) -> string. Return bytes from the pixmap""" |
|---|
| 110 | n/a | functions.append(f) |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | # Manual generator: store data in a pixmap |
|---|
| 113 | n/a | pixmapputbytes_body = """ |
|---|
| 114 | n/a | PixMapHandle pm; |
|---|
| 115 | n/a | int from, length; |
|---|
| 116 | n/a | char *cp, *icp; |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | if ( !PyArg_ParseTuple(_args, "O&is#", ResObj_Convert, &pm, &from, &icp, &length) ) |
|---|
| 119 | n/a | return NULL; |
|---|
| 120 | n/a | cp = GetPixBaseAddr(pm)+from; |
|---|
| 121 | n/a | memcpy(cp, icp, length); |
|---|
| 122 | n/a | Py_INCREF(Py_None); |
|---|
| 123 | n/a | _res = Py_None; |
|---|
| 124 | n/a | return _res; |
|---|
| 125 | n/a | """ |
|---|
| 126 | n/a | f = ManualGenerator("PutPixMapBytes", pixmapputbytes_body) |
|---|
| 127 | n/a | f.docstring = lambda: """(pixmap, int start, string data). Store bytes into the pixmap""" |
|---|
| 128 | n/a | functions.append(f) |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | # add the populated lists to the generator groups |
|---|
| 131 | n/a | # (in a different wordl the scan program would generate this) |
|---|
| 132 | n/a | for f in functions: module.add(f) |
|---|
| 133 | n/a | for f in methods: object.add(f) |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | # generate output (open the output file as late as possible) |
|---|
| 138 | n/a | SetOutputFileName(OUTPUTFILE) |
|---|
| 139 | n/a | module.generate() |
|---|