blob: f3de71946b66ea83cb369859d39aa251a75ddb68 [file] [log] [blame]
Jack Jansen52e90452001-11-05 14:44:23 +00001# IBCarbonsupport.py
2
3from macsupport import *
4
Jack Jansen9c2b5142001-11-05 16:21:45 +00005from CarbonEvtscan import RefObjectTypes
Jack Jansen52e90452001-11-05 14:44:23 +00006
7# where should this go? macsupport.py?
8CFStringRef = OpaqueByValueType('CFStringRef')
9
10for typ in RefObjectTypes:
11 execstr = "%(name)s = OpaqueByValueType('%(name)s')" % {"name": typ}
12 exec execstr
13
Just van Rossumf8d64732001-12-12 20:48:53 +000014if 0:
15 # these types will have no methods and will merely be opaque blobs
16 # should write getattr and setattr for them?
Jack Jansen52e90452001-11-05 14:44:23 +000017
Just van Rossumf8d64732001-12-12 20:48:53 +000018 StructObjectTypes = ["EventTypeSpec",
19 "HIPoint",
20 "HICommand",
21 "EventHotKeyID",
22 ]
Jack Jansen52e90452001-11-05 14:44:23 +000023
Just van Rossumf8d64732001-12-12 20:48:53 +000024 for typ in StructObjectTypes:
25 execstr = "%(name)s = OpaqueType('%(name)s')" % {"name": typ}
26 exec execstr
Jack Jansen52e90452001-11-05 14:44:23 +000027
Just van Rossumf8d64732001-12-12 20:48:53 +000028EventHotKeyID = OpaqueByValueType("EventHotKeyID", "EventHotKeyID")
Just van Rossumc3baa0e2001-12-11 21:52:02 +000029EventTypeSpec_ptr = OpaqueType("EventTypeSpec", "EventTypeSpec")
Jack Jansen52e90452001-11-05 14:44:23 +000030
31# is this the right type for the void * in GetEventParameter
32#void_ptr = FixedInputBufferType(1024)
33void_ptr = stringptr
34# here are some types that are really other types
35
36EventTime = double
37EventTimeout = EventTime
38EventTimerInterval = EventTime
39EventAttributes = UInt32
40EventParamName = OSType
41EventParamType = OSType
42EventPriority = SInt16
43EventMask = UInt16
44
45EventComparatorUPP = FakeType("(EventComparatorUPP)0")
46EventLoopTimerUPP = FakeType("(EventLoopTimerUPP)0")
47EventHandlerUPP = FakeType("(EventHandlerUPP)0")
48EventHandlerUPP = FakeType("(EventHandlerUPP)0")
49EventComparatorProcPtr = FakeType("(EventComparatorProcPtr)0")
50EventLoopTimerProcPtr = FakeType("(EventLoopTimerProcPtr)0")
51EventHandlerProcPtr = FakeType("(EventHandlerProcPtr)0")
52
53CarbonEventsFunction = OSErrFunctionGenerator
54CarbonEventsMethod = OSErrMethodGenerator
55
Just van Rossumf8d64732001-12-12 20:48:53 +000056includestuff = r"""
Just van Rossumc3baa0e2001-12-11 21:52:02 +000057#ifdef WITHOUT_FRAMEWORKS
58#include <CarbonEvents.h>
59#else
Jack Jansen52e90452001-11-05 14:44:23 +000060#include <Carbon/Carbon.h>
Just van Rossumc3baa0e2001-12-11 21:52:02 +000061#endif
62
Jack Jansen52e90452001-11-05 14:44:23 +000063#include "macglue.h"
64
Just van Rossumf8d64732001-12-12 20:48:53 +000065/* Macro to test whether a weak-loaded CFM function exists */
66#define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\
67 PyErr_SetString(PyExc_NotImplementedError, \
68 "Not available in this shared library/OS version"); \
69 return; \
70 }} while(0)
71
72
73#define USE_MAC_MP_MULTITHREADING 0
Jack Jansen52e90452001-11-05 14:44:23 +000074
75#if USE_MAC_MP_MULTITHREADING
76static PyThreadState *_save;
77static MPCriticalRegionID reentrantLock;
78#endif /* USE_MAC_MP_MULTITHREADING */
79
80extern int CFStringRef_New(CFStringRef *);
81
82extern int CFStringRef_Convert(PyObject *, CFStringRef *);
83extern int CFBundleRef_Convert(PyObject *, CFBundleRef *);
84
85int EventTargetRef_Convert(PyObject *, EventTargetRef *);
86PyObject *EventHandlerCallRef_New(EventHandlerCallRef itself);
87PyObject *EventRef_New(EventRef itself);
88
89/********** EventTypeSpec *******/
90static PyObject*
91EventTypeSpec_New(EventTypeSpec *in)
92{
93 return Py_BuildValue("ll", in->eventClass, in->eventKind);
94}
95
96static int
97EventTypeSpec_Convert(PyObject *v, EventTypeSpec *out)
98{
Just van Rossumc3baa0e2001-12-11 21:52:02 +000099 if (PyArg_Parse(v, "(O&l)", PyMac_GetOSType, &(out->eventClass), &(out->eventKind)))
Jack Jansen52e90452001-11-05 14:44:23 +0000100 return 1;
101 return NULL;
102}
103
104/********** end EventTypeSpec *******/
105
106/********** HIPoint *******/
107
Just van Rossumc3baa0e2001-12-11 21:52:02 +0000108#if 0 /* XXX doesn't compile */
Jack Jansen52e90452001-11-05 14:44:23 +0000109static PyObject*
110HIPoint_New(HIPoint *in)
111{
112 return Py_BuildValue("ff", in->x, in->y);
113}
114
115static int
116HIPoint_Convert(PyObject *v, HIPoint *out)
117{
118 if (PyArg_ParseTuple(v, "ff", &(out->x), &(out->y)))
119 return 1;
120 return NULL;
121}
Just van Rossumc3baa0e2001-12-11 21:52:02 +0000122#endif
Jack Jansen52e90452001-11-05 14:44:23 +0000123
124/********** end HIPoint *******/
125
126/********** EventHotKeyID *******/
127
128static PyObject*
129EventHotKeyID_New(EventHotKeyID *in)
130{
131 return Py_BuildValue("ll", in->signature, in->id);
132}
133
134static int
135EventHotKeyID_Convert(PyObject *v, EventHotKeyID *out)
136{
137 if (PyArg_ParseTuple(v, "ll", &out->signature, &out->id))
138 return 1;
139 return NULL;
140}
141
142/********** end EventHotKeyID *******/
143
Just van Rossumf8d64732001-12-12 20:48:53 +0000144/******** myEventHandler ***********/
Jack Jansen52e90452001-11-05 14:44:23 +0000145
Just van Rossumf8d64732001-12-12 20:48:53 +0000146static EventHandlerUPP myEventHandlerUPP;
Just van Rossumc3baa0e2001-12-11 21:52:02 +0000147
Just van Rossumf8d64732001-12-12 20:48:53 +0000148pascal OSStatus myEventHandler(EventHandlerCallRef handlerRef, EventRef event, void *outPyObject) {
Jack Jansen52e90452001-11-05 14:44:23 +0000149 PyObject *retValue;
150 int status;
151
152#if USE_MAC_MP_MULTITHREADING
153 MPEnterCriticalRegion(reentrantLock, kDurationForever);
154 PyEval_RestoreThread(_save);
155#endif /* USE_MAC_MP_MULTITHREADING */
156
Jack Jansen34cbe712001-11-05 16:15:45 +0000157 retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHandlerCallRef_New, handlerRef, EventRef_New, event);
Jack Jansen52e90452001-11-05 14:44:23 +0000158 status = PyInt_AsLong(retValue);
159
160#if USE_MAC_MP_MULTITHREADING
161 _save = PyEval_SaveThread();
162 MPExitCriticalRegion(reentrantLock);
163#endif /* USE_MAC_MP_MULTITHREADING */
164
165 return status;
166}
167
Just van Rossumf8d64732001-12-12 20:48:53 +0000168/******** end myEventHandler ***********/
Jack Jansen52e90452001-11-05 14:44:23 +0000169
170"""
171
Just van Rossumc3baa0e2001-12-11 21:52:02 +0000172initstuff = initstuff + """
Just van Rossumf8d64732001-12-12 20:48:53 +0000173PyMac_PRECHECK(NewEventHandlerUPP); /* This can fail if CarbonLib is too old */
174myEventHandlerUPP = NewEventHandlerUPP(myEventHandler);
Just van Rossumc3baa0e2001-12-11 21:52:02 +0000175"""
176module = MacModule('_CarbonEvt', 'CarbonEvents', includestuff, finalstuff, initstuff)
Jack Jansen52e90452001-11-05 14:44:23 +0000177
178#class CFReleaserObj(GlobalObjectDefinition):
179# def outputFreeIt(self, name):
180# Output("CFRelease(%s);" % name)
181
182for typ in RefObjectTypes:
183 execstr = typ + 'object = GlobalObjectDefinition(typ)'
184 exec execstr
185 module.addobject(eval(typ + 'object'))
186
187functions = []
Jack Jansen34cbe712001-11-05 16:15:45 +0000188for typ in RefObjectTypes: ## go thru all ObjectTypes as defined in CarbonEventsscan.py
Jack Jansen52e90452001-11-05 14:44:23 +0000189 # initialize the lists for carbongen to fill
190 execstr = typ + 'methods = []'
191 exec execstr
192
193execfile('CarbonEventsgen.py')
194
Jack Jansen34cbe712001-11-05 16:15:45 +0000195for f in functions: module.add(f) # add all the functions carboneventsgen put in the list
Jack Jansen52e90452001-11-05 14:44:23 +0000196
Jack Jansen34cbe712001-11-05 16:15:45 +0000197for typ in RefObjectTypes: ## go thru all ObjectTypes as defined in CarbonEventsscan.py
Jack Jansen9c2b5142001-11-05 16:21:45 +0000198 methods = eval(typ + 'methods') ## get a reference to the method list from the main namespace
Jack Jansen34cbe712001-11-05 16:15:45 +0000199 obj = eval(typ + 'object') ## get a reference to the object
200 for m in methods: obj.add(m) ## add each method in the list to the object
Jack Jansen52e90452001-11-05 14:44:23 +0000201
202installeventhandler = """
203EventTypeSpec inSpec;
204PyObject *callback;
205EventHandlerRef outRef;
206OSStatus _err;
Jack Jansen52e90452001-11-05 14:44:23 +0000207
208if (!PyArg_ParseTuple(_args, "O&O", EventTypeSpec_Convert, &inSpec, &callback))
209 return NULL;
210
Just van Rossumf8d64732001-12-12 20:48:53 +0000211_err = InstallEventHandler(_self->ob_itself, myEventHandlerUPP, 1, &inSpec, (void *)callback, &outRef);
Jack Jansen52e90452001-11-05 14:44:23 +0000212if (_err != noErr) return PyMac_Error(_err);
213
Just van Rossumc3baa0e2001-12-11 21:52:02 +0000214return Py_BuildValue("O&", EventHandlerRef_New, outRef);"""
Jack Jansen52e90452001-11-05 14:44:23 +0000215
216f = ManualGenerator("InstallEventHandler", installeventhandler);
Jack Jansen34cbe712001-11-05 16:15:45 +0000217f.docstring = lambda: "(EventTargetRef inTarget, EventTypeSpec inSpec, Method callback) -> (EventHandlerRef outRef)"
Jack Jansen52e90452001-11-05 14:44:23 +0000218EventTargetRefobject.add(f)
219
220runappeventloop = """
221#if USE_MAC_MP_MULTITHREADING
222if (MPCreateCriticalRegion(&reentrantLock) != noErr) {
Just van Rossumf8d64732001-12-12 20:48:53 +0000223 PySys_WriteStderr("lock failure\\n");
Jack Jansen52e90452001-11-05 14:44:23 +0000224 return NULL;
225}
226_save = PyEval_SaveThread();
227#endif /* USE_MAC_MP_MULTITHREADING */
228
229RunApplicationEventLoop();
230
231#if USE_MAC_MP_MULTITHREADING
232PyEval_RestoreThread(_save);
233
234MPDeleteCriticalRegion(reentrantLock);
235#endif /* USE_MAC_MP_MULTITHREADING */
236
237Py_INCREF(Py_None);
238
239return Py_None;
240"""
241
242f = ManualGenerator("RunApplicationEventLoop", runappeventloop);
243f.docstring = lambda: "() -> ()"
244module.add(f)
245
Just van Rossumc3baa0e2001-12-11 21:52:02 +0000246SetOutputFileName('_CarbonEvtmodule.c')
Jack Jansen52e90452001-11-05 14:44:23 +0000247module.generate()
248
Jack Jansen9c2b5142001-11-05 16:21:45 +0000249##import os
250##os.system("python setup.py build")