/Mac/Modules/evt/evtsupport.py

http://unladen-swallow.googlecode.com/ · Python · 92 lines · 53 code · 18 blank · 21 comment · 1 complexity · d5d5c7fd854e88b3d1da8225796f6297 MD5 · raw file

  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).
  5. import string
  6. # Declarations that change for each manager
  7. MACHEADERFILE = 'Events.h' # The Apple header file
  8. MODNAME = '_Evt' # The name of the module
  9. OBJECTNAME = 'Event' # The basic name of the objects used here
  10. KIND = 'Record' # Usually 'Ptr' or 'Handle'
  11. # The following is *usually* unchanged but may still require tuning
  12. MODPREFIX = 'Evt' # The prefix for module-wide routines
  13. OBJECTTYPE = OBJECTNAME + KIND # The C type used to represent them
  14. OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
  15. INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
  16. OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
  17. from macsupport import *
  18. # Create the type objects
  19. #WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX)
  20. RgnHandle = FakeType("(RgnHandle)0")
  21. # XXXX Should be next, but this will break a lot of code...
  22. # RgnHandle = OpaqueByValueType("RgnHandle", "OptResObj")
  23. KeyMap = ArrayOutputBufferType("KeyMap")
  24. ##MacOSEventKind = Type("MacOSEventKind", "h") # Old-style
  25. ##MacOSEventMask = Type("MacOSEventMask", "h") # Old-style
  26. EventMask = Type("EventMask", "H")
  27. EventKind = Type("EventKind", "H")
  28. includestuff = includestuff + """
  29. #include <Carbon/Carbon.h>
  30. """
  31. # From here on it's basically all boiler plate...
  32. # Create the generator groups and link them
  33. module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
  34. # Create the generator classes used to populate the lists
  35. Function = OSErrWeakLinkFunctionGenerator
  36. ##Method = OSErrWeakLinkMethodGenerator
  37. # Create and populate the lists
  38. functions = []
  39. execfile(INPUTFILE)
  40. # Move TickCount here, for convenience
  41. f = Function(UInt32, 'TickCount',
  42. )
  43. functions.append(f)
  44. # add the populated lists to the generator groups
  45. # (in a different wordl the scan program would generate this)
  46. for f in functions: module.add(f)
  47. WaitNextEvent_body = """
  48. Boolean _rv;
  49. EventMask eventMask;
  50. EventRecord theEvent;
  51. UInt32 sleep;
  52. Handle mouseregion = (Handle)0;
  53. if (!PyArg_ParseTuple(_args, "Hl|O&",
  54. &eventMask,
  55. &sleep,
  56. OptResObj_Convert, &mouseregion))
  57. return NULL;
  58. _rv = WaitNextEvent(eventMask,
  59. &theEvent,
  60. sleep,
  61. (RgnHandle)mouseregion);
  62. _res = Py_BuildValue("bO&",
  63. _rv,
  64. PyMac_BuildEventRecord, &theEvent);
  65. return _res;
  66. """
  67. f = ManualGenerator("WaitNextEvent", WaitNextEvent_body);
  68. f.docstring = lambda: "(EventMask eventMask, UInt32 sleep [,RegionHandle]) -> (Boolean _rv, EventRecord theEvent)"
  69. module.add(f)
  70. # generate output (open the output file as late as possible)
  71. SetOutputFileName(OUTPUTFILE)
  72. module.generate()