summaryrefslogtreecommitdiffstats
path: root/Mac/Modules/carbonevt
diff options
context:
space:
mode:
authorJack Jansen <jack.jansen@cwi.nl>2001-11-05 14:44:23 (GMT)
committerJack Jansen <jack.jansen@cwi.nl>2001-11-05 14:44:23 (GMT)
commit52e9045da6d2a57cec6823a8dc5c64a5c410ef13 (patch)
tree9bcdba242dd2c5d6e7c70994ffd0ea1248b2ae89 /Mac/Modules/carbonevt
parent537a69fe66b10614b11b129d07da146f30801717 (diff)
downloadcpython-52e9045da6d2a57cec6823a8dc5c64a5c410ef13.zip
cpython-52e9045da6d2a57cec6823a8dc5c64a5c410ef13.tar.gz
cpython-52e9045da6d2a57cec6823a8dc5c64a5c410ef13.tar.bz2
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.
Diffstat (limited to 'Mac/Modules/carbonevt')
-rw-r--r--Mac/Modules/carbonevt/CarbonEvtscan.py91
-rw-r--r--Mac/Modules/carbonevt/CarbonEvtsupport.py241
2 files changed, 332 insertions, 0 deletions
diff --git a/Mac/Modules/carbonevt/CarbonEvtscan.py b/Mac/Modules/carbonevt/CarbonEvtscan.py
new file mode 100644
index 0000000..96f9b55
--- /dev/null
+++ b/Mac/Modules/carbonevt/CarbonEvtscan.py
@@ -0,0 +1,91 @@
+# IBCarbonscan.py
+
+import sys
+import os
+import string
+import MacOS
+
+BGENDIR= '/Users/dsp/Documents/python/dist/src/Tools/bgen/bgen'
+sys.path.append(BGENDIR)
+
+from bgenlocations import TOOLBOXDIR
+
+from scantools import Scanner, Scanner_OSX
+
+def main():
+ print "---Scanning CarbonEvents.h---"
+ input = ["CarbonEvents.h"]
+ output = "CarbonEventsgen.py"
+ defsoutput = TOOLBOXDIR + "CarbonEvents.py"
+ scanner = CarbonEvents_Scanner(input, output, defsoutput)
+ scanner.scan()
+ scanner.close()
+ print "--done scanning, importing--"
+ import CarbonEventssupport
+ print "done"
+
+RefObjectTypes = ["EventRef",
+ "EventQueueRef",
+ "EventLoopRef",
+ "EventLoopTimerRef",
+ "EventHandlerRef",
+ "EventHandlerCallRef",
+ "EventTargetRef",
+ "EventHotKeyRef",
+ ]
+
+class CarbonEvents_Scanner(Scanner):
+ def destination(self, type, name, arglist):
+ classname = "CarbonEventsFunction"
+ listname = "functions"
+ if arglist:
+ t, n, m = arglist[0]
+ print "*********", t,
+ if t in RefObjectTypes and m == "InMode":
+ print "method"
+ classname = "CarbonEventsMethod"
+ listname = t + "methods"
+ else:
+ print "not method"
+ return classname, listname
+
+ def makeblacklistnames(self):
+ return [
+ "MacCreateEvent",
+ "TrackMouseLocationWithOptions",
+ "TrackMouseLocation",
+ "TrackMouseRegion",
+ "RegisterToolboxObjectClass",
+ "UnregisterToolboxObjectClass",
+ "ProcessHICommand",
+ "GetCFRunLoopFromEventLoop",
+
+ "InvokeEventHandlerUPP",
+ "InvokeEventComparatorUPP",
+ "InvokeEventLoopTimerUPP",
+
+ # Wrote by hand
+ "InstallEventHandler",
+ "RunApplicationEventLoop",
+
+ # Write by hand?
+ "GetEventParameter",
+ "FlushSpecificEventsFromQueue",
+ "FindSpecificEventInQueue",
+ "InstallEventLoopTimer",
+
+ # Don't do these because they require a CFRelease
+ "CreateTypeStringWithOSType",
+ "CopyEvent",
+ ]
+
+# def makeblacklisttypes(self):
+# return ["EventComparatorUPP",
+# "EventLoopTimerUPP",
+# #"EventHandlerUPP",
+# "EventComparatorProcPtr",
+# "EventLoopTimerProcPtr",
+# "EventHandlerProcPtr",
+# ]
+if __name__ == "__main__":
+ main()
diff --git a/Mac/Modules/carbonevt/CarbonEvtsupport.py b/Mac/Modules/carbonevt/CarbonEvtsupport.py
new file mode 100644
index 0000000..b56d20c
--- /dev/null
+++ b/Mac/Modules/carbonevt/CarbonEvtsupport.py
@@ -0,0 +1,241 @@
+# IBCarbonsupport.py
+
+from macsupport import *
+
+from CarbonEventsscan import RefObjectTypes
+
+# where should this go? macsupport.py?
+CFStringRef = OpaqueByValueType('CFStringRef')
+
+for typ in RefObjectTypes:
+ execstr = "%(name)s = OpaqueByValueType('%(name)s')" % {"name": typ}
+ exec execstr
+
+# these types will have no methods and will merely be opaque blobs
+# should write getattr and setattr for them?
+
+StructObjectTypes = ["EventTypeSpec",
+ "HIPoint",
+ "HICommand",
+ "EventHotKeyID",
+ ]
+
+for typ in StructObjectTypes:
+ execstr = "%(name)s = OpaqueType('%(name)s')" % {"name": typ}
+ exec execstr
+
+EventTypeSpec_ptr = OpaqueType("EventTypeSpec *", "EventTypeSpec")
+
+# is this the right type for the void * in GetEventParameter
+#void_ptr = FixedInputBufferType(1024)
+void_ptr = stringptr
+# here are some types that are really other types
+
+EventTime = double
+EventTimeout = EventTime
+EventTimerInterval = EventTime
+EventAttributes = UInt32
+EventParamName = OSType
+EventParamType = OSType
+EventPriority = SInt16
+EventMask = UInt16
+
+EventComparatorUPP = FakeType("(EventComparatorUPP)0")
+EventLoopTimerUPP = FakeType("(EventLoopTimerUPP)0")
+EventHandlerUPP = FakeType("(EventHandlerUPP)0")
+EventHandlerUPP = FakeType("(EventHandlerUPP)0")
+EventComparatorProcPtr = FakeType("(EventComparatorProcPtr)0")
+EventLoopTimerProcPtr = FakeType("(EventLoopTimerProcPtr)0")
+EventHandlerProcPtr = FakeType("(EventHandlerProcPtr)0")
+
+CarbonEventsFunction = OSErrFunctionGenerator
+CarbonEventsMethod = OSErrMethodGenerator
+
+includestuff = """
+#include <Carbon/Carbon.h>
+#include "macglue.h"
+
+#define USE_MAC_MP_MULTITHREADING 1
+
+#if USE_MAC_MP_MULTITHREADING
+static PyThreadState *_save;
+static MPCriticalRegionID reentrantLock;
+#endif /* USE_MAC_MP_MULTITHREADING */
+
+extern int CFStringRef_New(CFStringRef *);
+
+extern int CFStringRef_Convert(PyObject *, CFStringRef *);
+extern int CFBundleRef_Convert(PyObject *, CFBundleRef *);
+
+int EventTargetRef_Convert(PyObject *, EventTargetRef *);
+PyObject *EventHandlerCallRef_New(EventHandlerCallRef itself);
+PyObject *EventRef_New(EventRef itself);
+
+/********** EventTypeSpec *******/
+static PyObject*
+EventTypeSpec_New(EventTypeSpec *in)
+{
+ return Py_BuildValue("ll", in->eventClass, in->eventKind);
+}
+
+static int
+EventTypeSpec_Convert(PyObject *v, EventTypeSpec *out)
+{
+ if (PyArg_ParseTuple(v, "ll", &(out->eventClass), &(out->eventKind)))
+ return 1;
+ return NULL;
+}
+
+/********** end EventTypeSpec *******/
+
+/********** HIPoint *******/
+
+static PyObject*
+HIPoint_New(HIPoint *in)
+{
+ return Py_BuildValue("ff", in->x, in->y);
+}
+
+static int
+HIPoint_Convert(PyObject *v, HIPoint *out)
+{
+ if (PyArg_ParseTuple(v, "ff", &(out->x), &(out->y)))
+ return 1;
+ return NULL;
+}
+
+/********** end HIPoint *******/
+
+/********** EventHotKeyID *******/
+
+static PyObject*
+EventHotKeyID_New(EventHotKeyID *in)
+{
+ return Py_BuildValue("ll", in->signature, in->id);
+}
+
+static int
+EventHotKeyID_Convert(PyObject *v, EventHotKeyID *out)
+{
+ if (PyArg_ParseTuple(v, "ll", &out->signature, &out->id))
+ return 1;
+ return NULL;
+}
+
+/********** end EventHotKeyID *******/
+
+/******** handlecommand ***********/
+
+pascal OSStatus CarbonEvents_HandleCommand(EventHandlerCallRef handlerRef, Even
+tRef event, void *outPyObject) {
+ PyObject *retValue;
+ int status;
+
+#if USE_MAC_MP_MULTITHREADING
+ MPEnterCriticalRegion(reentrantLock, kDurationForever);
+ PyEval_RestoreThread(_save);
+#endif /* USE_MAC_MP_MULTITHREADING */
+
+ retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHand
+lerCallRef_New, handlerRef, EventRef_New, event);
+ status = PyInt_AsLong(retValue);
+
+#if USE_MAC_MP_MULTITHREADING
+ _save = PyEval_SaveThread();
+ MPExitCriticalRegion(reentrantLock);
+#endif /* USE_MAC_MP_MULTITHREADING */
+
+ return status;
+}
+
+/******** end handlecommand ***********/
+
+"""
+
+module = MacModule('CarbonEvents', 'CarbonEvents', includestuff, finalstuff, in
+itstuff)
+
+#class CFReleaserObj(GlobalObjectDefinition):
+# def outputFreeIt(self, name):
+# Output("CFRelease(%s);" % name)
+
+for typ in RefObjectTypes:
+ execstr = typ + 'object = GlobalObjectDefinition(typ)'
+ exec execstr
+ module.addobject(eval(typ + 'object'))
+
+functions = []
+for typ in RefObjectTypes: ## go thru all ObjectTypes as defined in CarbonEvent
+sscan.py
+ # initialize the lists for carbongen to fill
+ execstr = typ + 'methods = []'
+ exec execstr
+
+execfile('CarbonEventsgen.py')
+
+for f in functions: module.add(f) # add all the functions carboneventsgen
+ put in the list
+
+for typ in RefObjectTypes: ## go thru all ObjectT
+ypes as defined in CarbonEventsscan.py
+ methods = eval(typ + 'methods') ## get a reference to the method list
+from the main namespace
+ obj = eval(typ + 'object') ## get a reference to the obj
+ect
+ for m in methods: obj.add(m) ## add each method in the list to the o
+bject
+
+installeventhandler = """
+EventTypeSpec inSpec;
+PyObject *callback;
+EventHandlerRef outRef;
+OSStatus _err;
+EventHandlerUPP event;
+
+if (!PyArg_ParseTuple(_args, "O&O", EventTypeSpec_Convert, &inSpec, &callback))
+ return NULL;
+
+event = NewEventHandlerUPP(CarbonEvents_HandleCommand);
+_err = InstallEventHandler(_self->ob_itself, event, 1, &inSpec, (void *)callbac
+k, &outRef);
+if (_err != noErr) return PyMac_Error(_err);
+
+return Py_BuildValue("l", outRef);
+"""
+
+f = ManualGenerator("InstallEventHandler", installeventhandler);
+f.docstring = lambda: "(EventTargetRef inTarget, EventTypeSpec inSpec, Method c
+allback) -> (EventHandlerRef outRef)"
+EventTargetRefobject.add(f)
+
+runappeventloop = """
+#if USE_MAC_MP_MULTITHREADING
+if (MPCreateCriticalRegion(&reentrantLock) != noErr) {
+ printf("lock failure\n");
+ return NULL;
+}
+_save = PyEval_SaveThread();
+#endif /* USE_MAC_MP_MULTITHREADING */
+
+RunApplicationEventLoop();
+
+#if USE_MAC_MP_MULTITHREADING
+PyEval_RestoreThread(_save);
+
+MPDeleteCriticalRegion(reentrantLock);
+#endif /* USE_MAC_MP_MULTITHREADING */
+
+Py_INCREF(Py_None);
+
+return Py_None;
+"""
+
+f = ManualGenerator("RunApplicationEventLoop", runappeventloop);
+f.docstring = lambda: "() -> ()"
+module.add(f)
+
+SetOutputFileName('_CarbonEvents.c')
+module.generate()
+
+import os
+os.system("python setup.py build")