| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | # IBCarbonsupport.py | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from macsupport import * | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-05 16:21:45 +00:00
										 |  |  | from CarbonEvtscan import RefObjectTypes | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # where should this go? macsupport.py? | 
					
						
							|  |  |  | CFStringRef = OpaqueByValueType('CFStringRef') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | for typ in RefObjectTypes: | 
					
						
							|  |  |  | 	execstr = "%(name)s = OpaqueByValueType('%(name)s')" % {"name": typ} | 
					
						
							|  |  |  | 	exec execstr | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | if 0: | 
					
						
							|  |  |  | 	# these types will have no methods and will merely be opaque blobs | 
					
						
							|  |  |  | 	# should write getattr and setattr for them? | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | 	StructObjectTypes = ["EventTypeSpec", | 
					
						
							|  |  |  | 						"HIPoint", | 
					
						
							|  |  |  | 						"HICommand", | 
					
						
							|  |  |  | 						"EventHotKeyID", | 
					
						
							|  |  |  | 						] | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | 	for typ in StructObjectTypes: | 
					
						
							|  |  |  | 		execstr = "%(name)s = OpaqueType('%(name)s')" % {"name": typ} | 
					
						
							|  |  |  | 		exec execstr | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | EventHotKeyID = OpaqueByValueType("EventHotKeyID", "EventHotKeyID") | 
					
						
							| 
									
										
										
										
											2001-12-11 21:52:02 +00:00
										 |  |  | EventTypeSpec_ptr = OpaqueType("EventTypeSpec", "EventTypeSpec") | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | includestuff = r"""
 | 
					
						
							| 
									
										
										
										
											2001-12-11 21:52:02 +00:00
										 |  |  | #ifdef WITHOUT_FRAMEWORKS | 
					
						
							|  |  |  | #include <CarbonEvents.h> | 
					
						
							|  |  |  | #else | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | #include <Carbon/Carbon.h> | 
					
						
							| 
									
										
										
										
											2001-12-11 21:52:02 +00:00
										 |  |  | #endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | #include "macglue.h" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | /* Macro to test whether a weak-loaded CFM function exists */ | 
					
						
							|  |  |  | #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL )  {\ | 
					
						
							| 
									
										
										
										
											2001-12-12 21:48:00 +00:00
										 |  |  | 		PyErr_SetString(PyExc_NotImplementedError, \ | 
					
						
							|  |  |  | 		"Not available in this shared library/OS version"); \ | 
					
						
							|  |  |  | 		return; \ | 
					
						
							|  |  |  | 	}} while(0) | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define USE_MAC_MP_MULTITHREADING 0 | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-12-11 21:52:02 +00:00
										 |  |  | 	if (PyArg_Parse(v, "(O&l)", PyMac_GetOSType, &(out->eventClass), &(out->eventKind))) | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 		return 1; | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********** end EventTypeSpec *******/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********** HIPoint *******/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-11 21:52:02 +00:00
										 |  |  | #if 0  /* XXX doesn't compile */ | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 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; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2001-12-11 21:52:02 +00:00
										 |  |  | #endif | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /********** 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 *******/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | /******** myEventHandler ***********/ | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | static EventHandlerUPP myEventHandlerUPP; | 
					
						
							| 
									
										
										
										
											2001-12-11 21:52:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 21:48:00 +00:00
										 |  |  | static pascal OSStatus | 
					
						
							|  |  |  | myEventHandler(EventHandlerCallRef handlerRef, EventRef event, void *outPyObject) { | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 	PyObject *retValue; | 
					
						
							|  |  |  | 	int status; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if USE_MAC_MP_MULTITHREADING | 
					
						
							| 
									
										
										
										
											2001-12-12 21:48:00 +00:00
										 |  |  | 	MPEnterCriticalRegion(reentrantLock, kDurationForever); | 
					
						
							|  |  |  | 	PyEval_RestoreThread(_save); | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | #endif /* USE_MAC_MP_MULTITHREADING */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 21:48:00 +00:00
										 |  |  | 	retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHandlerCallRef_New, handlerRef, EventRef_New, event); | 
					
						
							|  |  |  | 	if (retValue == NULL) { | 
					
						
							|  |  |  | 		PySys_WriteStderr("Error in event handler callback:\n"); | 
					
						
							|  |  |  | 		PyErr_Print();  /* this also clears the error */ | 
					
						
							|  |  |  | 		status = noErr; /* complain? how? */ | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if (retValue == Py_None) | 
					
						
							|  |  |  | 			status = noErr; | 
					
						
							|  |  |  | 		else if (PyInt_Check(retValue)) { | 
					
						
							|  |  |  | 			status = PyInt_AsLong(retValue); | 
					
						
							|  |  |  | 		} else | 
					
						
							|  |  |  | 			status = noErr; /* wrong object type, complain? */ | 
					
						
							|  |  |  | 		Py_DECREF(retValue); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if USE_MAC_MP_MULTITHREADING | 
					
						
							| 
									
										
										
										
											2001-12-12 21:48:00 +00:00
										 |  |  | 	_save = PyEval_SaveThread(); | 
					
						
							|  |  |  | 	MPExitCriticalRegion(reentrantLock); | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | #endif /* USE_MAC_MP_MULTITHREADING */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 21:48:00 +00:00
										 |  |  | 	return status; | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | /******** end myEventHandler ***********/ | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-11 21:52:02 +00:00
										 |  |  | initstuff = initstuff + """
 | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | PyMac_PRECHECK(NewEventHandlerUPP); /* This can fail if CarbonLib is too old */ | 
					
						
							|  |  |  | myEventHandlerUPP = NewEventHandlerUPP(myEventHandler); | 
					
						
							| 
									
										
										
										
											2001-12-11 21:52:02 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | module = MacModule('_CarbonEvt', 'CarbonEvents', includestuff, finalstuff, initstuff) | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #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 = [] | 
					
						
							| 
									
										
										
										
											2001-11-05 16:15:45 +00:00
										 |  |  | for typ in RefObjectTypes: ## go thru all ObjectTypes as defined in CarbonEventsscan.py | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 	# initialize the lists for carbongen to fill | 
					
						
							|  |  |  | 	execstr = typ + 'methods = []' | 
					
						
							|  |  |  | 	exec execstr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | execfile('CarbonEventsgen.py') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-05 16:15:45 +00:00
										 |  |  | for f in functions: module.add(f)	# add all the functions carboneventsgen put in the list | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-05 16:15:45 +00:00
										 |  |  | for typ in RefObjectTypes:				 ## go thru all ObjectTypes as defined in CarbonEventsscan.py | 
					
						
							| 
									
										
										
										
											2001-11-05 16:21:45 +00:00
										 |  |  | 	methods = eval(typ + 'methods')  ## get a reference to the method list from the main namespace | 
					
						
							| 
									
										
										
										
											2001-11-05 16:15:45 +00:00
										 |  |  | 	obj = eval(typ + 'object')		  ## get a reference to the object | 
					
						
							|  |  |  | 	for m in methods: obj.add(m)	## add each method in the list to the object | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | installeventhandler = """
 | 
					
						
							|  |  |  | EventTypeSpec inSpec; | 
					
						
							|  |  |  | PyObject *callback; | 
					
						
							|  |  |  | EventHandlerRef outRef; | 
					
						
							|  |  |  | OSStatus _err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (!PyArg_ParseTuple(_args, "O&O", EventTypeSpec_Convert, &inSpec, &callback)) | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | _err = InstallEventHandler(_self->ob_itself, myEventHandlerUPP, 1, &inSpec, (void *)callback, &outRef); | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | if (_err != noErr) return PyMac_Error(_err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-11 21:52:02 +00:00
										 |  |  | return Py_BuildValue("O&", EventHandlerRef_New, outRef);"""
 | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | f = ManualGenerator("InstallEventHandler", installeventhandler); | 
					
						
							| 
									
										
										
										
											2001-11-05 16:15:45 +00:00
										 |  |  | f.docstring = lambda: "(EventTargetRef inTarget, EventTypeSpec inSpec, Method callback) -> (EventHandlerRef outRef)" | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | EventTargetRefobject.add(f) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | runappeventloop = """
 | 
					
						
							|  |  |  | #if USE_MAC_MP_MULTITHREADING | 
					
						
							|  |  |  | if (MPCreateCriticalRegion(&reentrantLock) != noErr) { | 
					
						
							| 
									
										
										
										
											2001-12-12 20:48:53 +00:00
										 |  |  | 	PySys_WriteStderr("lock failure\\n"); | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | 	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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-11 21:52:02 +00:00
										 |  |  | SetOutputFileName('_CarbonEvtmodule.c') | 
					
						
							| 
									
										
										
										
											2001-11-05 14:44:23 +00:00
										 |  |  | module.generate() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-05 16:21:45 +00:00
										 |  |  | ##import os | 
					
						
							|  |  |  | ##os.system("python setup.py build") |