2 /* =========================== Module _AE =========================== */
9 #include "pymactoolbox.h"
11 /* Macro to test whether a weak-loaded CFM function exists */
12 #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\
13 PyErr_SetString(PyExc_NotImplementedError, \
14 "Not available in this shared library/OS version"); \
19 #ifdef WITHOUT_FRAMEWORKS
20 #include <AppleEvents.h>
21 #include <AEObjects.h>
23 #include <Carbon/Carbon.h>
26 #ifdef USE_TOOLBOX_OBJECT_GLUE
27 extern PyObject
*_AEDesc_New(AEDesc
*);
28 extern int _AEDesc_Convert(PyObject
*, AEDesc
*);
30 #define AEDesc_New _AEDesc_New
31 #define AEDesc_Convert _AEDesc_Convert
34 static pascal OSErr
GenericEventHandler(); /* Forward */
36 AEEventHandlerUPP upp_GenericEventHandler
;
38 static pascal Boolean
AEIdleProc(EventRecord
*theEvent
, long *sleepTime
, RgnHandle
*mouseRgn
)
40 if ( PyOS_InterruptOccurred() )
42 #if !TARGET_API_MAC_OSX
43 if ( PyMac_HandleEvent(theEvent
) < 0 ) {
44 PySys_WriteStderr("Exception in user event handler during AE processing\n");
51 AEIdleUPP upp_AEIdleProc
;
53 static PyObject
*AE_Error
;
55 /* ----------------------- Object type AEDesc ----------------------- */
57 PyTypeObject AEDesc_Type
;
59 #define AEDesc_Check(x) ((x)->ob_type == &AEDesc_Type)
61 typedef struct AEDescObject
{
66 PyObject
*AEDesc_New(AEDesc
*itself
)
69 it
= PyObject_NEW(AEDescObject
, &AEDesc_Type
);
70 if (it
== NULL
) return NULL
;
71 it
->ob_itself
= *itself
;
72 return (PyObject
*)it
;
74 int AEDesc_Convert(PyObject
*v
, AEDesc
*p_itself
)
78 PyErr_SetString(PyExc_TypeError
, "AEDesc required");
81 *p_itself
= ((AEDescObject
*)v
)->ob_itself
;
85 static void AEDesc_dealloc(AEDescObject
*self
)
87 AEDisposeDesc(&self
->ob_itself
);
91 static PyObject
*AEDesc_AECoerceDesc(AEDescObject
*_self
, PyObject
*_args
)
93 PyObject
*_res
= NULL
;
97 if (!PyArg_ParseTuple(_args
, "O&",
98 PyMac_GetOSType
, &toType
))
100 _err
= AECoerceDesc(&_self
->ob_itself
,
103 if (_err
!= noErr
) return PyMac_Error(_err
);
104 _res
= Py_BuildValue("O&",
105 AEDesc_New
, &result
);
109 static PyObject
*AEDesc_AEDuplicateDesc(AEDescObject
*_self
, PyObject
*_args
)
111 PyObject
*_res
= NULL
;
114 if (!PyArg_ParseTuple(_args
, ""))
116 _err
= AEDuplicateDesc(&_self
->ob_itself
,
118 if (_err
!= noErr
) return PyMac_Error(_err
);
119 _res
= Py_BuildValue("O&",
120 AEDesc_New
, &result
);
124 static PyObject
*AEDesc_AECountItems(AEDescObject
*_self
, PyObject
*_args
)
126 PyObject
*_res
= NULL
;
129 if (!PyArg_ParseTuple(_args
, ""))
131 _err
= AECountItems(&_self
->ob_itself
,
133 if (_err
!= noErr
) return PyMac_Error(_err
);
134 _res
= Py_BuildValue("l",
139 static PyObject
*AEDesc_AEPutPtr(AEDescObject
*_self
, PyObject
*_args
)
141 PyObject
*_res
= NULL
;
147 int dataPtr__in_len__
;
148 if (!PyArg_ParseTuple(_args
, "lO&s#",
150 PyMac_GetOSType
, &typeCode
,
151 &dataPtr__in__
, &dataPtr__in_len__
))
153 dataPtr__len__
= dataPtr__in_len__
;
154 _err
= AEPutPtr(&_self
->ob_itself
,
157 dataPtr__in__
, dataPtr__len__
);
158 if (_err
!= noErr
) return PyMac_Error(_err
);
164 static PyObject
*AEDesc_AEPutDesc(AEDescObject
*_self
, PyObject
*_args
)
166 PyObject
*_res
= NULL
;
170 if (!PyArg_ParseTuple(_args
, "lO&",
172 AEDesc_Convert
, &theAEDesc
))
174 _err
= AEPutDesc(&_self
->ob_itself
,
177 if (_err
!= noErr
) return PyMac_Error(_err
);
183 static PyObject
*AEDesc_AEGetNthPtr(AEDescObject
*_self
, PyObject
*_args
)
185 PyObject
*_res
= NULL
;
188 DescType desiredType
;
189 AEKeyword theAEKeyword
;
191 char *dataPtr__out__
;
193 int dataPtr__in_len__
;
194 if (!PyArg_ParseTuple(_args
, "lO&i",
196 PyMac_GetOSType
, &desiredType
,
199 if ((dataPtr__out__
= malloc(dataPtr__in_len__
)) == NULL
)
202 goto dataPtr__error__
;
204 dataPtr__len__
= dataPtr__in_len__
;
205 _err
= AEGetNthPtr(&_self
->ob_itself
,
210 dataPtr__out__
, dataPtr__len__
, &dataPtr__len__
);
211 if (_err
!= noErr
) return PyMac_Error(_err
);
212 _res
= Py_BuildValue("O&O&s#",
213 PyMac_BuildOSType
, theAEKeyword
,
214 PyMac_BuildOSType
, typeCode
,
215 dataPtr__out__
, (int)dataPtr__len__
);
216 free(dataPtr__out__
);
221 static PyObject
*AEDesc_AEGetNthDesc(AEDescObject
*_self
, PyObject
*_args
)
223 PyObject
*_res
= NULL
;
226 DescType desiredType
;
227 AEKeyword theAEKeyword
;
229 if (!PyArg_ParseTuple(_args
, "lO&",
231 PyMac_GetOSType
, &desiredType
))
233 _err
= AEGetNthDesc(&_self
->ob_itself
,
238 if (_err
!= noErr
) return PyMac_Error(_err
);
239 _res
= Py_BuildValue("O&O&",
240 PyMac_BuildOSType
, theAEKeyword
,
241 AEDesc_New
, &result
);
245 static PyObject
*AEDesc_AESizeOfNthItem(AEDescObject
*_self
, PyObject
*_args
)
247 PyObject
*_res
= NULL
;
252 if (!PyArg_ParseTuple(_args
, "l",
255 _err
= AESizeOfNthItem(&_self
->ob_itself
,
259 if (_err
!= noErr
) return PyMac_Error(_err
);
260 _res
= Py_BuildValue("O&l",
261 PyMac_BuildOSType
, typeCode
,
266 static PyObject
*AEDesc_AEDeleteItem(AEDescObject
*_self
, PyObject
*_args
)
268 PyObject
*_res
= NULL
;
271 if (!PyArg_ParseTuple(_args
, "l",
274 _err
= AEDeleteItem(&_self
->ob_itself
,
276 if (_err
!= noErr
) return PyMac_Error(_err
);
282 static PyObject
*AEDesc_AEPutParamPtr(AEDescObject
*_self
, PyObject
*_args
)
284 PyObject
*_res
= NULL
;
286 AEKeyword theAEKeyword
;
290 int dataPtr__in_len__
;
291 if (!PyArg_ParseTuple(_args
, "O&O&s#",
292 PyMac_GetOSType
, &theAEKeyword
,
293 PyMac_GetOSType
, &typeCode
,
294 &dataPtr__in__
, &dataPtr__in_len__
))
296 dataPtr__len__
= dataPtr__in_len__
;
297 _err
= AEPutParamPtr(&_self
->ob_itself
,
300 dataPtr__in__
, dataPtr__len__
);
301 if (_err
!= noErr
) return PyMac_Error(_err
);
307 static PyObject
*AEDesc_AEPutParamDesc(AEDescObject
*_self
, PyObject
*_args
)
309 PyObject
*_res
= NULL
;
311 AEKeyword theAEKeyword
;
313 if (!PyArg_ParseTuple(_args
, "O&O&",
314 PyMac_GetOSType
, &theAEKeyword
,
315 AEDesc_Convert
, &theAEDesc
))
317 _err
= AEPutParamDesc(&_self
->ob_itself
,
320 if (_err
!= noErr
) return PyMac_Error(_err
);
326 static PyObject
*AEDesc_AEGetParamPtr(AEDescObject
*_self
, PyObject
*_args
)
328 PyObject
*_res
= NULL
;
330 AEKeyword theAEKeyword
;
331 DescType desiredType
;
333 char *dataPtr__out__
;
335 int dataPtr__in_len__
;
336 if (!PyArg_ParseTuple(_args
, "O&O&i",
337 PyMac_GetOSType
, &theAEKeyword
,
338 PyMac_GetOSType
, &desiredType
,
341 if ((dataPtr__out__
= malloc(dataPtr__in_len__
)) == NULL
)
344 goto dataPtr__error__
;
346 dataPtr__len__
= dataPtr__in_len__
;
347 _err
= AEGetParamPtr(&_self
->ob_itself
,
351 dataPtr__out__
, dataPtr__len__
, &dataPtr__len__
);
352 if (_err
!= noErr
) return PyMac_Error(_err
);
353 _res
= Py_BuildValue("O&s#",
354 PyMac_BuildOSType
, typeCode
,
355 dataPtr__out__
, (int)dataPtr__len__
);
356 free(dataPtr__out__
);
361 static PyObject
*AEDesc_AEGetParamDesc(AEDescObject
*_self
, PyObject
*_args
)
363 PyObject
*_res
= NULL
;
365 AEKeyword theAEKeyword
;
366 DescType desiredType
;
368 if (!PyArg_ParseTuple(_args
, "O&O&",
369 PyMac_GetOSType
, &theAEKeyword
,
370 PyMac_GetOSType
, &desiredType
))
372 _err
= AEGetParamDesc(&_self
->ob_itself
,
376 if (_err
!= noErr
) return PyMac_Error(_err
);
377 _res
= Py_BuildValue("O&",
378 AEDesc_New
, &result
);
382 static PyObject
*AEDesc_AESizeOfParam(AEDescObject
*_self
, PyObject
*_args
)
384 PyObject
*_res
= NULL
;
386 AEKeyword theAEKeyword
;
389 if (!PyArg_ParseTuple(_args
, "O&",
390 PyMac_GetOSType
, &theAEKeyword
))
392 _err
= AESizeOfParam(&_self
->ob_itself
,
396 if (_err
!= noErr
) return PyMac_Error(_err
);
397 _res
= Py_BuildValue("O&l",
398 PyMac_BuildOSType
, typeCode
,
403 static PyObject
*AEDesc_AEDeleteParam(AEDescObject
*_self
, PyObject
*_args
)
405 PyObject
*_res
= NULL
;
407 AEKeyword theAEKeyword
;
408 if (!PyArg_ParseTuple(_args
, "O&",
409 PyMac_GetOSType
, &theAEKeyword
))
411 _err
= AEDeleteParam(&_self
->ob_itself
,
413 if (_err
!= noErr
) return PyMac_Error(_err
);
419 static PyObject
*AEDesc_AEGetAttributePtr(AEDescObject
*_self
, PyObject
*_args
)
421 PyObject
*_res
= NULL
;
423 AEKeyword theAEKeyword
;
424 DescType desiredType
;
426 char *dataPtr__out__
;
428 int dataPtr__in_len__
;
429 if (!PyArg_ParseTuple(_args
, "O&O&i",
430 PyMac_GetOSType
, &theAEKeyword
,
431 PyMac_GetOSType
, &desiredType
,
434 if ((dataPtr__out__
= malloc(dataPtr__in_len__
)) == NULL
)
437 goto dataPtr__error__
;
439 dataPtr__len__
= dataPtr__in_len__
;
440 _err
= AEGetAttributePtr(&_self
->ob_itself
,
444 dataPtr__out__
, dataPtr__len__
, &dataPtr__len__
);
445 if (_err
!= noErr
) return PyMac_Error(_err
);
446 _res
= Py_BuildValue("O&s#",
447 PyMac_BuildOSType
, typeCode
,
448 dataPtr__out__
, (int)dataPtr__len__
);
449 free(dataPtr__out__
);
454 static PyObject
*AEDesc_AEGetAttributeDesc(AEDescObject
*_self
, PyObject
*_args
)
456 PyObject
*_res
= NULL
;
458 AEKeyword theAEKeyword
;
459 DescType desiredType
;
461 if (!PyArg_ParseTuple(_args
, "O&O&",
462 PyMac_GetOSType
, &theAEKeyword
,
463 PyMac_GetOSType
, &desiredType
))
465 _err
= AEGetAttributeDesc(&_self
->ob_itself
,
469 if (_err
!= noErr
) return PyMac_Error(_err
);
470 _res
= Py_BuildValue("O&",
471 AEDesc_New
, &result
);
475 static PyObject
*AEDesc_AESizeOfAttribute(AEDescObject
*_self
, PyObject
*_args
)
477 PyObject
*_res
= NULL
;
479 AEKeyword theAEKeyword
;
482 if (!PyArg_ParseTuple(_args
, "O&",
483 PyMac_GetOSType
, &theAEKeyword
))
485 _err
= AESizeOfAttribute(&_self
->ob_itself
,
489 if (_err
!= noErr
) return PyMac_Error(_err
);
490 _res
= Py_BuildValue("O&l",
491 PyMac_BuildOSType
, typeCode
,
496 static PyObject
*AEDesc_AEPutAttributePtr(AEDescObject
*_self
, PyObject
*_args
)
498 PyObject
*_res
= NULL
;
500 AEKeyword theAEKeyword
;
504 int dataPtr__in_len__
;
505 if (!PyArg_ParseTuple(_args
, "O&O&s#",
506 PyMac_GetOSType
, &theAEKeyword
,
507 PyMac_GetOSType
, &typeCode
,
508 &dataPtr__in__
, &dataPtr__in_len__
))
510 dataPtr__len__
= dataPtr__in_len__
;
511 _err
= AEPutAttributePtr(&_self
->ob_itself
,
514 dataPtr__in__
, dataPtr__len__
);
515 if (_err
!= noErr
) return PyMac_Error(_err
);
521 static PyObject
*AEDesc_AEPutAttributeDesc(AEDescObject
*_self
, PyObject
*_args
)
523 PyObject
*_res
= NULL
;
525 AEKeyword theAEKeyword
;
527 if (!PyArg_ParseTuple(_args
, "O&O&",
528 PyMac_GetOSType
, &theAEKeyword
,
529 AEDesc_Convert
, &theAEDesc
))
531 _err
= AEPutAttributeDesc(&_self
->ob_itself
,
534 if (_err
!= noErr
) return PyMac_Error(_err
);
540 #if TARGET_API_MAC_CARBON
542 static PyObject
*AEDesc_AEGetDescDataSize(AEDescObject
*_self
, PyObject
*_args
)
544 PyObject
*_res
= NULL
;
546 if (!PyArg_ParseTuple(_args
, ""))
548 _rv
= AEGetDescDataSize(&_self
->ob_itself
);
549 _res
= Py_BuildValue("l",
555 static PyObject
*AEDesc_AESend(AEDescObject
*_self
, PyObject
*_args
)
557 PyObject
*_res
= NULL
;
561 AESendPriority sendPriority
;
563 if (!PyArg_ParseTuple(_args
, "lhl",
568 _err
= AESend(&_self
->ob_itself
,
575 if (_err
!= noErr
) return PyMac_Error(_err
);
576 _res
= Py_BuildValue("O&",
581 static PyObject
*AEDesc_AEResetTimer(AEDescObject
*_self
, PyObject
*_args
)
583 PyObject
*_res
= NULL
;
585 if (!PyArg_ParseTuple(_args
, ""))
587 _err
= AEResetTimer(&_self
->ob_itself
);
588 if (_err
!= noErr
) return PyMac_Error(_err
);
594 static PyObject
*AEDesc_AESuspendTheCurrentEvent(AEDescObject
*_self
, PyObject
*_args
)
596 PyObject
*_res
= NULL
;
598 if (!PyArg_ParseTuple(_args
, ""))
600 _err
= AESuspendTheCurrentEvent(&_self
->ob_itself
);
601 if (_err
!= noErr
) return PyMac_Error(_err
);
607 static PyObject
*AEDesc_AEResumeTheCurrentEvent(AEDescObject
*_self
, PyObject
*_args
)
609 PyObject
*_res
= NULL
;
612 AEEventHandlerUPP dispatcher__proc__
= upp_GenericEventHandler
;
613 PyObject
*dispatcher
;
614 if (!PyArg_ParseTuple(_args
, "O&O",
615 AEDesc_Convert
, &reply
,
618 _err
= AEResumeTheCurrentEvent(&_self
->ob_itself
,
620 dispatcher__proc__
, (long)dispatcher
);
621 if (_err
!= noErr
) return PyMac_Error(_err
);
624 Py_INCREF(dispatcher
); /* XXX leak, but needed */
628 static PyObject
*AEDesc_AEGetTheCurrentEvent(AEDescObject
*_self
, PyObject
*_args
)
630 PyObject
*_res
= NULL
;
632 if (!PyArg_ParseTuple(_args
, ""))
634 _err
= AEGetTheCurrentEvent(&_self
->ob_itself
);
635 if (_err
!= noErr
) return PyMac_Error(_err
);
641 static PyObject
*AEDesc_AESetTheCurrentEvent(AEDescObject
*_self
, PyObject
*_args
)
643 PyObject
*_res
= NULL
;
645 if (!PyArg_ParseTuple(_args
, ""))
647 _err
= AESetTheCurrentEvent(&_self
->ob_itself
);
648 if (_err
!= noErr
) return PyMac_Error(_err
);
654 static PyObject
*AEDesc_AEResolve(AEDescObject
*_self
, PyObject
*_args
)
656 PyObject
*_res
= NULL
;
660 if (!PyArg_ParseTuple(_args
, "h",
663 _err
= AEResolve(&_self
->ob_itself
,
666 if (_err
!= noErr
) return PyMac_Error(_err
);
667 _res
= Py_BuildValue("O&",
668 AEDesc_New
, &theToken
);
672 static PyMethodDef AEDesc_methods
[] = {
673 {"AECoerceDesc", (PyCFunction
)AEDesc_AECoerceDesc
, 1,
674 "(DescType toType) -> (AEDesc result)"},
675 {"AEDuplicateDesc", (PyCFunction
)AEDesc_AEDuplicateDesc
, 1,
676 "() -> (AEDesc result)"},
677 {"AECountItems", (PyCFunction
)AEDesc_AECountItems
, 1,
678 "() -> (long theCount)"},
679 {"AEPutPtr", (PyCFunction
)AEDesc_AEPutPtr
, 1,
680 "(long index, DescType typeCode, Buffer dataPtr) -> None"},
681 {"AEPutDesc", (PyCFunction
)AEDesc_AEPutDesc
, 1,
682 "(long index, AEDesc theAEDesc) -> None"},
683 {"AEGetNthPtr", (PyCFunction
)AEDesc_AEGetNthPtr
, 1,
684 "(long index, DescType desiredType, Buffer dataPtr) -> (AEKeyword theAEKeyword, DescType typeCode, Buffer dataPtr)"},
685 {"AEGetNthDesc", (PyCFunction
)AEDesc_AEGetNthDesc
, 1,
686 "(long index, DescType desiredType) -> (AEKeyword theAEKeyword, AEDesc result)"},
687 {"AESizeOfNthItem", (PyCFunction
)AEDesc_AESizeOfNthItem
, 1,
688 "(long index) -> (DescType typeCode, Size dataSize)"},
689 {"AEDeleteItem", (PyCFunction
)AEDesc_AEDeleteItem
, 1,
690 "(long index) -> None"},
691 {"AEPutParamPtr", (PyCFunction
)AEDesc_AEPutParamPtr
, 1,
692 "(AEKeyword theAEKeyword, DescType typeCode, Buffer dataPtr) -> None"},
693 {"AEPutParamDesc", (PyCFunction
)AEDesc_AEPutParamDesc
, 1,
694 "(AEKeyword theAEKeyword, AEDesc theAEDesc) -> None"},
695 {"AEGetParamPtr", (PyCFunction
)AEDesc_AEGetParamPtr
, 1,
696 "(AEKeyword theAEKeyword, DescType desiredType, Buffer dataPtr) -> (DescType typeCode, Buffer dataPtr)"},
697 {"AEGetParamDesc", (PyCFunction
)AEDesc_AEGetParamDesc
, 1,
698 "(AEKeyword theAEKeyword, DescType desiredType) -> (AEDesc result)"},
699 {"AESizeOfParam", (PyCFunction
)AEDesc_AESizeOfParam
, 1,
700 "(AEKeyword theAEKeyword) -> (DescType typeCode, Size dataSize)"},
701 {"AEDeleteParam", (PyCFunction
)AEDesc_AEDeleteParam
, 1,
702 "(AEKeyword theAEKeyword) -> None"},
703 {"AEGetAttributePtr", (PyCFunction
)AEDesc_AEGetAttributePtr
, 1,
704 "(AEKeyword theAEKeyword, DescType desiredType, Buffer dataPtr) -> (DescType typeCode, Buffer dataPtr)"},
705 {"AEGetAttributeDesc", (PyCFunction
)AEDesc_AEGetAttributeDesc
, 1,
706 "(AEKeyword theAEKeyword, DescType desiredType) -> (AEDesc result)"},
707 {"AESizeOfAttribute", (PyCFunction
)AEDesc_AESizeOfAttribute
, 1,
708 "(AEKeyword theAEKeyword) -> (DescType typeCode, Size dataSize)"},
709 {"AEPutAttributePtr", (PyCFunction
)AEDesc_AEPutAttributePtr
, 1,
710 "(AEKeyword theAEKeyword, DescType typeCode, Buffer dataPtr) -> None"},
711 {"AEPutAttributeDesc", (PyCFunction
)AEDesc_AEPutAttributeDesc
, 1,
712 "(AEKeyword theAEKeyword, AEDesc theAEDesc) -> None"},
714 #if TARGET_API_MAC_CARBON
715 {"AEGetDescDataSize", (PyCFunction
)AEDesc_AEGetDescDataSize
, 1,
718 {"AESend", (PyCFunction
)AEDesc_AESend
, 1,
719 "(AESendMode sendMode, AESendPriority sendPriority, long timeOutInTicks) -> (AppleEvent reply)"},
720 {"AEResetTimer", (PyCFunction
)AEDesc_AEResetTimer
, 1,
722 {"AESuspendTheCurrentEvent", (PyCFunction
)AEDesc_AESuspendTheCurrentEvent
, 1,
724 {"AEResumeTheCurrentEvent", (PyCFunction
)AEDesc_AEResumeTheCurrentEvent
, 1,
725 "(AppleEvent reply, EventHandler dispatcher) -> None"},
726 {"AEGetTheCurrentEvent", (PyCFunction
)AEDesc_AEGetTheCurrentEvent
, 1,
728 {"AESetTheCurrentEvent", (PyCFunction
)AEDesc_AESetTheCurrentEvent
, 1,
730 {"AEResolve", (PyCFunction
)AEDesc_AEResolve
, 1,
731 "(short callbackFlags) -> (AEDesc theToken)"},
735 PyMethodChain AEDesc_chain
= { AEDesc_methods
, NULL
};
737 static PyObject
*AEDesc_getattr(AEDescObject
*self
, char *name
)
740 if (strcmp(name
, "type") == 0)
741 return PyMac_BuildOSType(self
->ob_itself
.descriptorType
);
742 if (strcmp(name
, "data") == 0) {
744 #if !TARGET_API_MAC_CARBON
746 state
= HGetState(self
->ob_itself
.dataHandle
);
747 HLock(self
->ob_itself
.dataHandle
);
748 res
= PyString_FromStringAndSize(
749 *self
->ob_itself
.dataHandle
,
750 GetHandleSize(self
->ob_itself
.dataHandle
));
751 HUnlock(self
->ob_itself
.dataHandle
);
752 HSetState(self
->ob_itself
.dataHandle
, state
);
758 size
= AEGetDescDataSize(&self
->ob_itself
);
759 if ( (res
= PyString_FromStringAndSize(NULL
, size
)) == NULL
)
761 if ( (ptr
= PyString_AsString(res
)) == NULL
)
763 if ( (err
=AEGetDescData(&self
->ob_itself
, ptr
, size
)) < 0 )
764 return PyMac_Error(err
);
768 if (strcmp(name
, "__members__") == 0)
769 return Py_BuildValue("[ss]", "data", "type");
771 return Py_FindMethodInChain(&AEDesc_chain
, (PyObject
*)self
, name
);
774 #define AEDesc_setattr NULL
776 #define AEDesc_compare NULL
778 #define AEDesc_repr NULL
780 #define AEDesc_hash NULL
782 PyTypeObject AEDesc_Type
= {
783 PyObject_HEAD_INIT(NULL
)
785 "AEDesc", /*tp_name*/
786 sizeof(AEDescObject
), /*tp_basicsize*/
789 (destructor
) AEDesc_dealloc
, /*tp_dealloc*/
791 (getattrfunc
) AEDesc_getattr
, /*tp_getattr*/
792 (setattrfunc
) AEDesc_setattr
, /*tp_setattr*/
793 (cmpfunc
) AEDesc_compare
, /*tp_compare*/
794 (reprfunc
) AEDesc_repr
, /*tp_repr*/
795 (PyNumberMethods
*)0, /* tp_as_number */
796 (PySequenceMethods
*)0, /* tp_as_sequence */
797 (PyMappingMethods
*)0, /* tp_as_mapping */
798 (hashfunc
) AEDesc_hash
, /*tp_hash*/
801 /* --------------------- End object type AEDesc --------------------- */
804 static PyObject
*AE_AECoercePtr(PyObject
*_self
, PyObject
*_args
)
806 PyObject
*_res
= NULL
;
811 int dataPtr__in_len__
;
814 if (!PyArg_ParseTuple(_args
, "O&s#O&",
815 PyMac_GetOSType
, &typeCode
,
816 &dataPtr__in__
, &dataPtr__in_len__
,
817 PyMac_GetOSType
, &toType
))
819 dataPtr__len__
= dataPtr__in_len__
;
820 _err
= AECoercePtr(typeCode
,
821 dataPtr__in__
, dataPtr__len__
,
824 if (_err
!= noErr
) return PyMac_Error(_err
);
825 _res
= Py_BuildValue("O&",
826 AEDesc_New
, &result
);
830 static PyObject
*AE_AECreateDesc(PyObject
*_self
, PyObject
*_args
)
832 PyObject
*_res
= NULL
;
837 int dataPtr__in_len__
;
839 if (!PyArg_ParseTuple(_args
, "O&s#",
840 PyMac_GetOSType
, &typeCode
,
841 &dataPtr__in__
, &dataPtr__in_len__
))
843 dataPtr__len__
= dataPtr__in_len__
;
844 _err
= AECreateDesc(typeCode
,
845 dataPtr__in__
, dataPtr__len__
,
847 if (_err
!= noErr
) return PyMac_Error(_err
);
848 _res
= Py_BuildValue("O&",
849 AEDesc_New
, &result
);
853 static PyObject
*AE_AECreateList(PyObject
*_self
, PyObject
*_args
)
855 PyObject
*_res
= NULL
;
857 char *factoringPtr__in__
;
858 long factoringPtr__len__
;
859 int factoringPtr__in_len__
;
861 AEDescList resultList
;
862 if (!PyArg_ParseTuple(_args
, "s#b",
863 &factoringPtr__in__
, &factoringPtr__in_len__
,
866 factoringPtr__len__
= factoringPtr__in_len__
;
867 _err
= AECreateList(factoringPtr__in__
, factoringPtr__len__
,
870 if (_err
!= noErr
) return PyMac_Error(_err
);
871 _res
= Py_BuildValue("O&",
872 AEDesc_New
, &resultList
);
876 static PyObject
*AE_AECreateAppleEvent(PyObject
*_self
, PyObject
*_args
)
878 PyObject
*_res
= NULL
;
880 AEEventClass theAEEventClass
;
881 AEEventID theAEEventID
;
882 AEAddressDesc target
;
884 AETransactionID transactionID
;
886 if (!PyArg_ParseTuple(_args
, "O&O&O&hl",
887 PyMac_GetOSType
, &theAEEventClass
,
888 PyMac_GetOSType
, &theAEEventID
,
889 AEDesc_Convert
, &target
,
893 _err
= AECreateAppleEvent(theAEEventClass
,
899 if (_err
!= noErr
) return PyMac_Error(_err
);
900 _res
= Py_BuildValue("O&",
901 AEDesc_New
, &result
);
905 #if TARGET_API_MAC_CARBON
907 static PyObject
*AE_AEReplaceDescData(PyObject
*_self
, PyObject
*_args
)
909 PyObject
*_res
= NULL
;
914 int dataPtr__in_len__
;
916 if (!PyArg_ParseTuple(_args
, "O&s#",
917 PyMac_GetOSType
, &typeCode
,
918 &dataPtr__in__
, &dataPtr__in_len__
))
920 dataPtr__len__
= dataPtr__in_len__
;
921 _err
= AEReplaceDescData(typeCode
,
922 dataPtr__in__
, dataPtr__len__
,
924 if (_err
!= noErr
) return PyMac_Error(_err
);
925 _res
= Py_BuildValue("O&",
926 AEDesc_New
, &theAEDesc
);
931 static PyObject
*AE_AEProcessAppleEvent(PyObject
*_self
, PyObject
*_args
)
933 PyObject
*_res
= NULL
;
935 EventRecord theEventRecord
;
936 if (!PyArg_ParseTuple(_args
, "O&",
937 PyMac_GetEventRecord
, &theEventRecord
))
939 _err
= AEProcessAppleEvent(&theEventRecord
);
940 if (_err
!= noErr
) return PyMac_Error(_err
);
946 static PyObject
*AE_AEGetInteractionAllowed(PyObject
*_self
, PyObject
*_args
)
948 PyObject
*_res
= NULL
;
950 AEInteractAllowed level
;
951 if (!PyArg_ParseTuple(_args
, ""))
953 _err
= AEGetInteractionAllowed(&level
);
954 if (_err
!= noErr
) return PyMac_Error(_err
);
955 _res
= Py_BuildValue("b",
960 static PyObject
*AE_AESetInteractionAllowed(PyObject
*_self
, PyObject
*_args
)
962 PyObject
*_res
= NULL
;
964 AEInteractAllowed level
;
965 if (!PyArg_ParseTuple(_args
, "b",
968 _err
= AESetInteractionAllowed(level
);
969 if (_err
!= noErr
) return PyMac_Error(_err
);
975 static PyObject
*AE_AEInteractWithUser(PyObject
*_self
, PyObject
*_args
)
977 PyObject
*_res
= NULL
;
980 if (!PyArg_ParseTuple(_args
, "l",
983 _err
= AEInteractWithUser(timeOutInTicks
,
986 if (_err
!= noErr
) return PyMac_Error(_err
);
992 static PyObject
*AE_AEInstallEventHandler(PyObject
*_self
, PyObject
*_args
)
994 PyObject
*_res
= NULL
;
996 AEEventClass theAEEventClass
;
997 AEEventID theAEEventID
;
998 AEEventHandlerUPP handler__proc__
= upp_GenericEventHandler
;
1000 if (!PyArg_ParseTuple(_args
, "O&O&O",
1001 PyMac_GetOSType
, &theAEEventClass
,
1002 PyMac_GetOSType
, &theAEEventID
,
1005 _err
= AEInstallEventHandler(theAEEventClass
,
1007 handler__proc__
, (long)handler
,
1009 if (_err
!= noErr
) return PyMac_Error(_err
);
1012 Py_INCREF(handler
); /* XXX leak, but needed */
1016 static PyObject
*AE_AERemoveEventHandler(PyObject
*_self
, PyObject
*_args
)
1018 PyObject
*_res
= NULL
;
1020 AEEventClass theAEEventClass
;
1021 AEEventID theAEEventID
;
1022 if (!PyArg_ParseTuple(_args
, "O&O&",
1023 PyMac_GetOSType
, &theAEEventClass
,
1024 PyMac_GetOSType
, &theAEEventID
))
1026 _err
= AERemoveEventHandler(theAEEventClass
,
1028 upp_GenericEventHandler
,
1030 if (_err
!= noErr
) return PyMac_Error(_err
);
1036 static PyObject
*AE_AEGetEventHandler(PyObject
*_self
, PyObject
*_args
)
1038 PyObject
*_res
= NULL
;
1040 AEEventClass theAEEventClass
;
1041 AEEventID theAEEventID
;
1042 AEEventHandlerUPP handler__proc__
= upp_GenericEventHandler
;
1044 if (!PyArg_ParseTuple(_args
, "O&O&",
1045 PyMac_GetOSType
, &theAEEventClass
,
1046 PyMac_GetOSType
, &theAEEventID
))
1048 _err
= AEGetEventHandler(theAEEventClass
,
1050 &handler__proc__
, (long *)&handler
,
1052 if (_err
!= noErr
) return PyMac_Error(_err
);
1053 _res
= Py_BuildValue("O",
1055 Py_INCREF(handler
); /* XXX leak, but needed */
1059 static PyObject
*AE_AEInstallSpecialHandler(PyObject
*_self
, PyObject
*_args
)
1061 PyObject
*_res
= NULL
;
1063 AEKeyword functionClass
;
1064 if (!PyArg_ParseTuple(_args
, "O&",
1065 PyMac_GetOSType
, &functionClass
))
1067 _err
= AEInstallSpecialHandler(functionClass
,
1068 upp_GenericEventHandler
,
1070 if (_err
!= noErr
) return PyMac_Error(_err
);
1076 static PyObject
*AE_AERemoveSpecialHandler(PyObject
*_self
, PyObject
*_args
)
1078 PyObject
*_res
= NULL
;
1080 AEKeyword functionClass
;
1081 if (!PyArg_ParseTuple(_args
, "O&",
1082 PyMac_GetOSType
, &functionClass
))
1084 _err
= AERemoveSpecialHandler(functionClass
,
1085 upp_GenericEventHandler
,
1087 if (_err
!= noErr
) return PyMac_Error(_err
);
1093 static PyObject
*AE_AEManagerInfo(PyObject
*_self
, PyObject
*_args
)
1095 PyObject
*_res
= NULL
;
1099 if (!PyArg_ParseTuple(_args
, "O&",
1100 PyMac_GetOSType
, &keyWord
))
1102 _err
= AEManagerInfo(keyWord
,
1104 if (_err
!= noErr
) return PyMac_Error(_err
);
1105 _res
= Py_BuildValue("l",
1110 static PyObject
*AE_AEObjectInit(PyObject
*_self
, PyObject
*_args
)
1112 PyObject
*_res
= NULL
;
1114 if (!PyArg_ParseTuple(_args
, ""))
1116 _err
= AEObjectInit();
1117 if (_err
!= noErr
) return PyMac_Error(_err
);
1123 static PyObject
*AE_AEDisposeToken(PyObject
*_self
, PyObject
*_args
)
1125 PyObject
*_res
= NULL
;
1128 if (!PyArg_ParseTuple(_args
, ""))
1130 _err
= AEDisposeToken(&theToken
);
1131 if (_err
!= noErr
) return PyMac_Error(_err
);
1132 _res
= Py_BuildValue("O&",
1133 AEDesc_New
, &theToken
);
1137 static PyObject
*AE_AECallObjectAccessor(PyObject
*_self
, PyObject
*_args
)
1139 PyObject
*_res
= NULL
;
1141 DescType desiredClass
;
1142 AEDesc containerToken
;
1143 DescType containerClass
;
1147 if (!PyArg_ParseTuple(_args
, "O&O&O&O&O&",
1148 PyMac_GetOSType
, &desiredClass
,
1149 AEDesc_Convert
, &containerToken
,
1150 PyMac_GetOSType
, &containerClass
,
1151 PyMac_GetOSType
, &keyForm
,
1152 AEDesc_Convert
, &keyData
))
1154 _err
= AECallObjectAccessor(desiredClass
,
1160 if (_err
!= noErr
) return PyMac_Error(_err
);
1161 _res
= Py_BuildValue("O&",
1162 AEDesc_New
, &token
);
1166 static PyMethodDef AE_methods
[] = {
1167 {"AECoercePtr", (PyCFunction
)AE_AECoercePtr
, 1,
1168 "(DescType typeCode, Buffer dataPtr, DescType toType) -> (AEDesc result)"},
1169 {"AECreateDesc", (PyCFunction
)AE_AECreateDesc
, 1,
1170 "(DescType typeCode, Buffer dataPtr) -> (AEDesc result)"},
1171 {"AECreateList", (PyCFunction
)AE_AECreateList
, 1,
1172 "(Buffer factoringPtr, Boolean isRecord) -> (AEDescList resultList)"},
1173 {"AECreateAppleEvent", (PyCFunction
)AE_AECreateAppleEvent
, 1,
1174 "(AEEventClass theAEEventClass, AEEventID theAEEventID, AEAddressDesc target, AEReturnID returnID, AETransactionID transactionID) -> (AppleEvent result)"},
1176 #if TARGET_API_MAC_CARBON
1177 {"AEReplaceDescData", (PyCFunction
)AE_AEReplaceDescData
, 1,
1178 "(DescType typeCode, Buffer dataPtr) -> (AEDesc theAEDesc)"},
1180 {"AEProcessAppleEvent", (PyCFunction
)AE_AEProcessAppleEvent
, 1,
1181 "(EventRecord theEventRecord) -> None"},
1182 {"AEGetInteractionAllowed", (PyCFunction
)AE_AEGetInteractionAllowed
, 1,
1183 "() -> (AEInteractAllowed level)"},
1184 {"AESetInteractionAllowed", (PyCFunction
)AE_AESetInteractionAllowed
, 1,
1185 "(AEInteractAllowed level) -> None"},
1186 {"AEInteractWithUser", (PyCFunction
)AE_AEInteractWithUser
, 1,
1187 "(long timeOutInTicks) -> None"},
1188 {"AEInstallEventHandler", (PyCFunction
)AE_AEInstallEventHandler
, 1,
1189 "(AEEventClass theAEEventClass, AEEventID theAEEventID, EventHandler handler) -> None"},
1190 {"AERemoveEventHandler", (PyCFunction
)AE_AERemoveEventHandler
, 1,
1191 "(AEEventClass theAEEventClass, AEEventID theAEEventID) -> None"},
1192 {"AEGetEventHandler", (PyCFunction
)AE_AEGetEventHandler
, 1,
1193 "(AEEventClass theAEEventClass, AEEventID theAEEventID) -> (EventHandler handler)"},
1194 {"AEInstallSpecialHandler", (PyCFunction
)AE_AEInstallSpecialHandler
, 1,
1195 "(AEKeyword functionClass) -> None"},
1196 {"AERemoveSpecialHandler", (PyCFunction
)AE_AERemoveSpecialHandler
, 1,
1197 "(AEKeyword functionClass) -> None"},
1198 {"AEManagerInfo", (PyCFunction
)AE_AEManagerInfo
, 1,
1199 "(AEKeyword keyWord) -> (long result)"},
1200 {"AEObjectInit", (PyCFunction
)AE_AEObjectInit
, 1,
1202 {"AEDisposeToken", (PyCFunction
)AE_AEDisposeToken
, 1,
1203 "() -> (AEDesc theToken)"},
1204 {"AECallObjectAccessor", (PyCFunction
)AE_AECallObjectAccessor
, 1,
1205 "(DescType desiredClass, AEDesc containerToken, DescType containerClass, DescType keyForm, AEDesc keyData) -> (AEDesc token)"},
1211 #if UNIVERSAL_INTERFACES_VERSION >= 0x0340
1212 typedef long refcontype
;
1214 typedef unsigned long refcontype
;
1218 GenericEventHandler(const AppleEvent
*request
, AppleEvent
*reply
, refcontype refcon
)
1220 PyObject
*handler
= (PyObject
*)refcon
;
1221 AEDescObject
*requestObject
, *replyObject
;
1222 PyObject
*args
, *res
;
1223 if ((requestObject
= (AEDescObject
*)AEDesc_New((AppleEvent
*)request
)) == NULL
) {
1226 if ((replyObject
= (AEDescObject
*)AEDesc_New(reply
)) == NULL
) {
1227 Py_DECREF(requestObject
);
1230 if ((args
= Py_BuildValue("OO", requestObject
, replyObject
)) == NULL
) {
1231 Py_DECREF(requestObject
);
1232 Py_DECREF(replyObject
);
1235 res
= PyEval_CallObject(handler
, args
);
1236 requestObject
->ob_itself
.descriptorType
= 'null';
1237 requestObject
->ob_itself
.dataHandle
= NULL
;
1238 replyObject
->ob_itself
.descriptorType
= 'null';
1239 replyObject
->ob_itself
.dataHandle
= NULL
;
1255 upp_AEIdleProc
= NewAEIdleUPP(AEIdleProc
);
1256 #if UNIVERSAL_INTERFACES_VERSION >= 0x03400
1257 upp_GenericEventHandler
= NewAEEventHandlerUPP(&GenericEventHandler
);
1259 upp_GenericEventHandler
= NewAEEventHandlerUPP(GenericEventHandler
);
1261 PyMac_INIT_TOOLBOX_OBJECT_NEW(AEDesc
*, AEDesc_New
);
1262 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(AEDesc
, AEDesc_Convert
);
1265 m
= Py_InitModule("_AE", AE_methods
);
1266 d
= PyModule_GetDict(m
);
1267 AE_Error
= PyMac_GetOSErrException();
1268 if (AE_Error
== NULL
||
1269 PyDict_SetItemString(d
, "Error", AE_Error
) != 0)
1271 AEDesc_Type
.ob_type
= &PyType_Type
;
1272 Py_INCREF(&AEDesc_Type
);
1273 if (PyDict_SetItemString(d
, "AEDescType", (PyObject
*)&AEDesc_Type
) != 0)
1274 Py_FatalError("can't initialize AEDescType");
1277 /* ========================= End module _AE ========================= */