PageRenderTime 1380ms CodeModel.GetById 160ms app.highlight 585ms RepoModel.GetById 622ms app.codeStats 1ms

/Mac/Modules/ae/_AEmodule.c

http://unladen-swallow.googlecode.com/
C | 1459 lines | 1347 code | 105 blank | 7 comment | 173 complexity | 23acde0705d78c73740fd71901d15e76 MD5 | raw file
   1
   2/* =========================== Module _AE =========================== */
   3
   4#include "Python.h"
   5
   6
   7
   8#include "pymactoolbox.h"
   9
  10#ifndef HAVE_OSX105_SDK
  11typedef SInt32 SRefCon;
  12#endif
  13
  14/* Macro to test whether a weak-loaded CFM function exists */
  15#define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL )  {\
  16        PyErr_SetString(PyExc_NotImplementedError, \
  17        "Not available in this shared library/OS version"); \
  18        return NULL; \
  19    }} while(0)
  20
  21
  22#include <Carbon/Carbon.h>
  23
  24#ifdef USE_TOOLBOX_OBJECT_GLUE
  25extern PyObject *_AEDesc_New(AEDesc *);
  26extern int _AEDesc_Convert(PyObject *, AEDesc *);
  27
  28#define AEDesc_New _AEDesc_New
  29#define AEDesc_NewBorrowed _AEDesc_NewBorrowed
  30#define AEDesc_Convert _AEDesc_Convert
  31#endif
  32
  33typedef long refcontype;
  34
  35static pascal OSErr GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon); /* Forward */
  36
  37AEEventHandlerUPP upp_GenericEventHandler;
  38
  39static pascal Boolean AEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn)
  40{
  41        if ( PyOS_InterruptOccurred() )
  42                return 1;
  43        return 0;
  44}
  45
  46AEIdleUPP upp_AEIdleProc;
  47
  48static PyObject *AE_Error;
  49
  50/* ----------------------- Object type AEDesc ----------------------- */
  51
  52PyTypeObject AEDesc_Type;
  53
  54#define AEDesc_Check(x) ((x)->ob_type == &AEDesc_Type || PyObject_TypeCheck((x), &AEDesc_Type))
  55
  56typedef struct AEDescObject {
  57	PyObject_HEAD
  58	AEDesc ob_itself;
  59	int ob_owned;
  60} AEDescObject;
  61
  62PyObject *AEDesc_New(AEDesc *itself)
  63{
  64	AEDescObject *it;
  65	it = PyObject_NEW(AEDescObject, &AEDesc_Type);
  66	if (it == NULL) return NULL;
  67	it->ob_itself = *itself;
  68	it->ob_owned = 1;
  69	return (PyObject *)it;
  70}
  71
  72int AEDesc_Convert(PyObject *v, AEDesc *p_itself)
  73{
  74	if (!AEDesc_Check(v))
  75	{
  76		PyErr_SetString(PyExc_TypeError, "AEDesc required");
  77		return 0;
  78	}
  79	*p_itself = ((AEDescObject *)v)->ob_itself;
  80	return 1;
  81}
  82
  83static void AEDesc_dealloc(AEDescObject *self)
  84{
  85	if (self->ob_owned) AEDisposeDesc(&self->ob_itself);
  86	self->ob_type->tp_free((PyObject *)self);
  87}
  88
  89static PyObject *AEDesc_AECoerceDesc(AEDescObject *_self, PyObject *_args)
  90{
  91	PyObject *_res = NULL;
  92	OSErr _err;
  93	DescType toType;
  94	AEDesc result;
  95#ifndef AECoerceDesc
  96	PyMac_PRECHECK(AECoerceDesc);
  97#endif
  98	if (!PyArg_ParseTuple(_args, "O&",
  99	                      PyMac_GetOSType, &toType))
 100		return NULL;
 101	_err = AECoerceDesc(&_self->ob_itself,
 102	                    toType,
 103	                    &result);
 104	if (_err != noErr) return PyMac_Error(_err);
 105	_res = Py_BuildValue("O&",
 106	                     AEDesc_New, &result);
 107	return _res;
 108}
 109
 110static PyObject *AEDesc_AEDuplicateDesc(AEDescObject *_self, PyObject *_args)
 111{
 112	PyObject *_res = NULL;
 113	OSErr _err;
 114	AEDesc result;
 115#ifndef AEDuplicateDesc
 116	PyMac_PRECHECK(AEDuplicateDesc);
 117#endif
 118	if (!PyArg_ParseTuple(_args, ""))
 119		return NULL;
 120	_err = AEDuplicateDesc(&_self->ob_itself,
 121	                       &result);
 122	if (_err != noErr) return PyMac_Error(_err);
 123	_res = Py_BuildValue("O&",
 124	                     AEDesc_New, &result);
 125	return _res;
 126}
 127
 128static PyObject *AEDesc_AECountItems(AEDescObject *_self, PyObject *_args)
 129{
 130	PyObject *_res = NULL;
 131	OSErr _err;
 132	long theCount;
 133#ifndef AECountItems
 134	PyMac_PRECHECK(AECountItems);
 135#endif
 136	if (!PyArg_ParseTuple(_args, ""))
 137		return NULL;
 138	_err = AECountItems(&_self->ob_itself,
 139	                    &theCount);
 140	if (_err != noErr) return PyMac_Error(_err);
 141	_res = Py_BuildValue("l",
 142	                     theCount);
 143	return _res;
 144}
 145
 146static PyObject *AEDesc_AEPutPtr(AEDescObject *_self, PyObject *_args)
 147{
 148	PyObject *_res = NULL;
 149	OSErr _err;
 150	long index;
 151	DescType typeCode;
 152	char *dataPtr__in__;
 153	long dataPtr__len__;
 154	int dataPtr__in_len__;
 155#ifndef AEPutPtr
 156	PyMac_PRECHECK(AEPutPtr);
 157#endif
 158	if (!PyArg_ParseTuple(_args, "lO&s#",
 159	                      &index,
 160	                      PyMac_GetOSType, &typeCode,
 161	                      &dataPtr__in__, &dataPtr__in_len__))
 162		return NULL;
 163	dataPtr__len__ = dataPtr__in_len__;
 164	_err = AEPutPtr(&_self->ob_itself,
 165	                index,
 166	                typeCode,
 167	                dataPtr__in__, dataPtr__len__);
 168	if (_err != noErr) return PyMac_Error(_err);
 169	Py_INCREF(Py_None);
 170	_res = Py_None;
 171	return _res;
 172}
 173
 174static PyObject *AEDesc_AEPutDesc(AEDescObject *_self, PyObject *_args)
 175{
 176	PyObject *_res = NULL;
 177	OSErr _err;
 178	long index;
 179	AEDesc theAEDesc;
 180#ifndef AEPutDesc
 181	PyMac_PRECHECK(AEPutDesc);
 182#endif
 183	if (!PyArg_ParseTuple(_args, "lO&",
 184	                      &index,
 185	                      AEDesc_Convert, &theAEDesc))
 186		return NULL;
 187	_err = AEPutDesc(&_self->ob_itself,
 188	                 index,
 189	                 &theAEDesc);
 190	if (_err != noErr) return PyMac_Error(_err);
 191	Py_INCREF(Py_None);
 192	_res = Py_None;
 193	return _res;
 194}
 195
 196static PyObject *AEDesc_AEGetNthPtr(AEDescObject *_self, PyObject *_args)
 197{
 198	PyObject *_res = NULL;
 199	OSErr _err;
 200	long index;
 201	DescType desiredType;
 202	AEKeyword theAEKeyword;
 203	DescType typeCode;
 204	char *dataPtr__out__;
 205	long dataPtr__len__;
 206	int dataPtr__in_len__;
 207#ifndef AEGetNthPtr
 208	PyMac_PRECHECK(AEGetNthPtr);
 209#endif
 210	if (!PyArg_ParseTuple(_args, "lO&i",
 211	                      &index,
 212	                      PyMac_GetOSType, &desiredType,
 213	                      &dataPtr__in_len__))
 214		return NULL;
 215	if ((dataPtr__out__ = malloc(dataPtr__in_len__)) == NULL)
 216	{
 217		PyErr_NoMemory();
 218		goto dataPtr__error__;
 219	}
 220	dataPtr__len__ = dataPtr__in_len__;
 221	_err = AEGetNthPtr(&_self->ob_itself,
 222	                   index,
 223	                   desiredType,
 224	                   &theAEKeyword,
 225	                   &typeCode,
 226	                   dataPtr__out__, dataPtr__len__, &dataPtr__len__);
 227	if (_err != noErr) return PyMac_Error(_err);
 228	_res = Py_BuildValue("O&O&s#",
 229	                     PyMac_BuildOSType, theAEKeyword,
 230	                     PyMac_BuildOSType, typeCode,
 231	                     dataPtr__out__, (int)dataPtr__len__);
 232	free(dataPtr__out__);
 233 dataPtr__error__: ;
 234	return _res;
 235}
 236
 237static PyObject *AEDesc_AEGetNthDesc(AEDescObject *_self, PyObject *_args)
 238{
 239	PyObject *_res = NULL;
 240	OSErr _err;
 241	long index;
 242	DescType desiredType;
 243	AEKeyword theAEKeyword;
 244	AEDesc result;
 245#ifndef AEGetNthDesc
 246	PyMac_PRECHECK(AEGetNthDesc);
 247#endif
 248	if (!PyArg_ParseTuple(_args, "lO&",
 249	                      &index,
 250	                      PyMac_GetOSType, &desiredType))
 251		return NULL;
 252	_err = AEGetNthDesc(&_self->ob_itself,
 253	                    index,
 254	                    desiredType,
 255	                    &theAEKeyword,
 256	                    &result);
 257	if (_err != noErr) return PyMac_Error(_err);
 258	_res = Py_BuildValue("O&O&",
 259	                     PyMac_BuildOSType, theAEKeyword,
 260	                     AEDesc_New, &result);
 261	return _res;
 262}
 263
 264static PyObject *AEDesc_AESizeOfNthItem(AEDescObject *_self, PyObject *_args)
 265{
 266	PyObject *_res = NULL;
 267	OSErr _err;
 268	long index;
 269	DescType typeCode;
 270	Size dataSize;
 271#ifndef AESizeOfNthItem
 272	PyMac_PRECHECK(AESizeOfNthItem);
 273#endif
 274	if (!PyArg_ParseTuple(_args, "l",
 275	                      &index))
 276		return NULL;
 277	_err = AESizeOfNthItem(&_self->ob_itself,
 278	                       index,
 279	                       &typeCode,
 280	                       &dataSize);
 281	if (_err != noErr) return PyMac_Error(_err);
 282	_res = Py_BuildValue("O&l",
 283	                     PyMac_BuildOSType, typeCode,
 284	                     dataSize);
 285	return _res;
 286}
 287
 288static PyObject *AEDesc_AEDeleteItem(AEDescObject *_self, PyObject *_args)
 289{
 290	PyObject *_res = NULL;
 291	OSErr _err;
 292	long index;
 293#ifndef AEDeleteItem
 294	PyMac_PRECHECK(AEDeleteItem);
 295#endif
 296	if (!PyArg_ParseTuple(_args, "l",
 297	                      &index))
 298		return NULL;
 299	_err = AEDeleteItem(&_self->ob_itself,
 300	                    index);
 301	if (_err != noErr) return PyMac_Error(_err);
 302	Py_INCREF(Py_None);
 303	_res = Py_None;
 304	return _res;
 305}
 306
 307static PyObject *AEDesc_AEPutParamPtr(AEDescObject *_self, PyObject *_args)
 308{
 309	PyObject *_res = NULL;
 310	OSErr _err;
 311	AEKeyword theAEKeyword;
 312	DescType typeCode;
 313	char *dataPtr__in__;
 314	long dataPtr__len__;
 315	int dataPtr__in_len__;
 316#ifndef AEPutParamPtr
 317	PyMac_PRECHECK(AEPutParamPtr);
 318#endif
 319	if (!PyArg_ParseTuple(_args, "O&O&s#",
 320	                      PyMac_GetOSType, &theAEKeyword,
 321	                      PyMac_GetOSType, &typeCode,
 322	                      &dataPtr__in__, &dataPtr__in_len__))
 323		return NULL;
 324	dataPtr__len__ = dataPtr__in_len__;
 325	_err = AEPutParamPtr(&_self->ob_itself,
 326	                     theAEKeyword,
 327	                     typeCode,
 328	                     dataPtr__in__, dataPtr__len__);
 329	if (_err != noErr) return PyMac_Error(_err);
 330	Py_INCREF(Py_None);
 331	_res = Py_None;
 332	return _res;
 333}
 334
 335static PyObject *AEDesc_AEPutParamDesc(AEDescObject *_self, PyObject *_args)
 336{
 337	PyObject *_res = NULL;
 338	OSErr _err;
 339	AEKeyword theAEKeyword;
 340	AEDesc theAEDesc;
 341#ifndef AEPutParamDesc
 342	PyMac_PRECHECK(AEPutParamDesc);
 343#endif
 344	if (!PyArg_ParseTuple(_args, "O&O&",
 345	                      PyMac_GetOSType, &theAEKeyword,
 346	                      AEDesc_Convert, &theAEDesc))
 347		return NULL;
 348	_err = AEPutParamDesc(&_self->ob_itself,
 349	                      theAEKeyword,
 350	                      &theAEDesc);
 351	if (_err != noErr) return PyMac_Error(_err);
 352	Py_INCREF(Py_None);
 353	_res = Py_None;
 354	return _res;
 355}
 356
 357static PyObject *AEDesc_AEGetParamPtr(AEDescObject *_self, PyObject *_args)
 358{
 359	PyObject *_res = NULL;
 360	OSErr _err;
 361	AEKeyword theAEKeyword;
 362	DescType desiredType;
 363	DescType typeCode;
 364	char *dataPtr__out__;
 365	long dataPtr__len__;
 366	int dataPtr__in_len__;
 367#ifndef AEGetParamPtr
 368	PyMac_PRECHECK(AEGetParamPtr);
 369#endif
 370	if (!PyArg_ParseTuple(_args, "O&O&i",
 371	                      PyMac_GetOSType, &theAEKeyword,
 372	                      PyMac_GetOSType, &desiredType,
 373	                      &dataPtr__in_len__))
 374		return NULL;
 375	if ((dataPtr__out__ = malloc(dataPtr__in_len__)) == NULL)
 376	{
 377		PyErr_NoMemory();
 378		goto dataPtr__error__;
 379	}
 380	dataPtr__len__ = dataPtr__in_len__;
 381	_err = AEGetParamPtr(&_self->ob_itself,
 382	                     theAEKeyword,
 383	                     desiredType,
 384	                     &typeCode,
 385	                     dataPtr__out__, dataPtr__len__, &dataPtr__len__);
 386	if (_err != noErr) return PyMac_Error(_err);
 387	_res = Py_BuildValue("O&s#",
 388	                     PyMac_BuildOSType, typeCode,
 389	                     dataPtr__out__, (int)dataPtr__len__);
 390	free(dataPtr__out__);
 391 dataPtr__error__: ;
 392	return _res;
 393}
 394
 395static PyObject *AEDesc_AEGetParamDesc(AEDescObject *_self, PyObject *_args)
 396{
 397	PyObject *_res = NULL;
 398	OSErr _err;
 399	AEKeyword theAEKeyword;
 400	DescType desiredType;
 401	AEDesc result;
 402#ifndef AEGetParamDesc
 403	PyMac_PRECHECK(AEGetParamDesc);
 404#endif
 405	if (!PyArg_ParseTuple(_args, "O&O&",
 406	                      PyMac_GetOSType, &theAEKeyword,
 407	                      PyMac_GetOSType, &desiredType))
 408		return NULL;
 409	_err = AEGetParamDesc(&_self->ob_itself,
 410	                      theAEKeyword,
 411	                      desiredType,
 412	                      &result);
 413	if (_err != noErr) return PyMac_Error(_err);
 414	_res = Py_BuildValue("O&",
 415	                     AEDesc_New, &result);
 416	return _res;
 417}
 418
 419static PyObject *AEDesc_AESizeOfParam(AEDescObject *_self, PyObject *_args)
 420{
 421	PyObject *_res = NULL;
 422	OSErr _err;
 423	AEKeyword theAEKeyword;
 424	DescType typeCode;
 425	Size dataSize;
 426#ifndef AESizeOfParam
 427	PyMac_PRECHECK(AESizeOfParam);
 428#endif
 429	if (!PyArg_ParseTuple(_args, "O&",
 430	                      PyMac_GetOSType, &theAEKeyword))
 431		return NULL;
 432	_err = AESizeOfParam(&_self->ob_itself,
 433	                     theAEKeyword,
 434	                     &typeCode,
 435	                     &dataSize);
 436	if (_err != noErr) return PyMac_Error(_err);
 437	_res = Py_BuildValue("O&l",
 438	                     PyMac_BuildOSType, typeCode,
 439	                     dataSize);
 440	return _res;
 441}
 442
 443static PyObject *AEDesc_AEDeleteParam(AEDescObject *_self, PyObject *_args)
 444{
 445	PyObject *_res = NULL;
 446	OSErr _err;
 447	AEKeyword theAEKeyword;
 448#ifndef AEDeleteParam
 449	PyMac_PRECHECK(AEDeleteParam);
 450#endif
 451	if (!PyArg_ParseTuple(_args, "O&",
 452	                      PyMac_GetOSType, &theAEKeyword))
 453		return NULL;
 454	_err = AEDeleteParam(&_self->ob_itself,
 455	                     theAEKeyword);
 456	if (_err != noErr) return PyMac_Error(_err);
 457	Py_INCREF(Py_None);
 458	_res = Py_None;
 459	return _res;
 460}
 461
 462static PyObject *AEDesc_AEGetAttributePtr(AEDescObject *_self, PyObject *_args)
 463{
 464	PyObject *_res = NULL;
 465	OSErr _err;
 466	AEKeyword theAEKeyword;
 467	DescType desiredType;
 468	DescType typeCode;
 469	char *dataPtr__out__;
 470	long dataPtr__len__;
 471	int dataPtr__in_len__;
 472#ifndef AEGetAttributePtr
 473	PyMac_PRECHECK(AEGetAttributePtr);
 474#endif
 475	if (!PyArg_ParseTuple(_args, "O&O&i",
 476	                      PyMac_GetOSType, &theAEKeyword,
 477	                      PyMac_GetOSType, &desiredType,
 478	                      &dataPtr__in_len__))
 479		return NULL;
 480	if ((dataPtr__out__ = malloc(dataPtr__in_len__)) == NULL)
 481	{
 482		PyErr_NoMemory();
 483		goto dataPtr__error__;
 484	}
 485	dataPtr__len__ = dataPtr__in_len__;
 486	_err = AEGetAttributePtr(&_self->ob_itself,
 487	                         theAEKeyword,
 488	                         desiredType,
 489	                         &typeCode,
 490	                         dataPtr__out__, dataPtr__len__, &dataPtr__len__);
 491	if (_err != noErr) return PyMac_Error(_err);
 492	_res = Py_BuildValue("O&s#",
 493	                     PyMac_BuildOSType, typeCode,
 494	                     dataPtr__out__, (int)dataPtr__len__);
 495	free(dataPtr__out__);
 496 dataPtr__error__: ;
 497	return _res;
 498}
 499
 500static PyObject *AEDesc_AEGetAttributeDesc(AEDescObject *_self, PyObject *_args)
 501{
 502	PyObject *_res = NULL;
 503	OSErr _err;
 504	AEKeyword theAEKeyword;
 505	DescType desiredType;
 506	AEDesc result;
 507#ifndef AEGetAttributeDesc
 508	PyMac_PRECHECK(AEGetAttributeDesc);
 509#endif
 510	if (!PyArg_ParseTuple(_args, "O&O&",
 511	                      PyMac_GetOSType, &theAEKeyword,
 512	                      PyMac_GetOSType, &desiredType))
 513		return NULL;
 514	_err = AEGetAttributeDesc(&_self->ob_itself,
 515	                          theAEKeyword,
 516	                          desiredType,
 517	                          &result);
 518	if (_err != noErr) return PyMac_Error(_err);
 519	_res = Py_BuildValue("O&",
 520	                     AEDesc_New, &result);
 521	return _res;
 522}
 523
 524static PyObject *AEDesc_AESizeOfAttribute(AEDescObject *_self, PyObject *_args)
 525{
 526	PyObject *_res = NULL;
 527	OSErr _err;
 528	AEKeyword theAEKeyword;
 529	DescType typeCode;
 530	Size dataSize;
 531#ifndef AESizeOfAttribute
 532	PyMac_PRECHECK(AESizeOfAttribute);
 533#endif
 534	if (!PyArg_ParseTuple(_args, "O&",
 535	                      PyMac_GetOSType, &theAEKeyword))
 536		return NULL;
 537	_err = AESizeOfAttribute(&_self->ob_itself,
 538	                         theAEKeyword,
 539	                         &typeCode,
 540	                         &dataSize);
 541	if (_err != noErr) return PyMac_Error(_err);
 542	_res = Py_BuildValue("O&l",
 543	                     PyMac_BuildOSType, typeCode,
 544	                     dataSize);
 545	return _res;
 546}
 547
 548static PyObject *AEDesc_AEPutAttributePtr(AEDescObject *_self, PyObject *_args)
 549{
 550	PyObject *_res = NULL;
 551	OSErr _err;
 552	AEKeyword theAEKeyword;
 553	DescType typeCode;
 554	char *dataPtr__in__;
 555	long dataPtr__len__;
 556	int dataPtr__in_len__;
 557#ifndef AEPutAttributePtr
 558	PyMac_PRECHECK(AEPutAttributePtr);
 559#endif
 560	if (!PyArg_ParseTuple(_args, "O&O&s#",
 561	                      PyMac_GetOSType, &theAEKeyword,
 562	                      PyMac_GetOSType, &typeCode,
 563	                      &dataPtr__in__, &dataPtr__in_len__))
 564		return NULL;
 565	dataPtr__len__ = dataPtr__in_len__;
 566	_err = AEPutAttributePtr(&_self->ob_itself,
 567	                         theAEKeyword,
 568	                         typeCode,
 569	                         dataPtr__in__, dataPtr__len__);
 570	if (_err != noErr) return PyMac_Error(_err);
 571	Py_INCREF(Py_None);
 572	_res = Py_None;
 573	return _res;
 574}
 575
 576static PyObject *AEDesc_AEPutAttributeDesc(AEDescObject *_self, PyObject *_args)
 577{
 578	PyObject *_res = NULL;
 579	OSErr _err;
 580	AEKeyword theAEKeyword;
 581	AEDesc theAEDesc;
 582#ifndef AEPutAttributeDesc
 583	PyMac_PRECHECK(AEPutAttributeDesc);
 584#endif
 585	if (!PyArg_ParseTuple(_args, "O&O&",
 586	                      PyMac_GetOSType, &theAEKeyword,
 587	                      AEDesc_Convert, &theAEDesc))
 588		return NULL;
 589	_err = AEPutAttributeDesc(&_self->ob_itself,
 590	                          theAEKeyword,
 591	                          &theAEDesc);
 592	if (_err != noErr) return PyMac_Error(_err);
 593	Py_INCREF(Py_None);
 594	_res = Py_None;
 595	return _res;
 596}
 597
 598static PyObject *AEDesc_AEGetDescDataSize(AEDescObject *_self, PyObject *_args)
 599{
 600	PyObject *_res = NULL;
 601	Size _rv;
 602#ifndef AEGetDescDataSize
 603	PyMac_PRECHECK(AEGetDescDataSize);
 604#endif
 605	if (!PyArg_ParseTuple(_args, ""))
 606		return NULL;
 607	_rv = AEGetDescDataSize(&_self->ob_itself);
 608	_res = Py_BuildValue("l",
 609	                     _rv);
 610	return _res;
 611}
 612
 613static PyObject *AEDesc_AESend(AEDescObject *_self, PyObject *_args)
 614{
 615	PyObject *_res = NULL;
 616	OSErr _err;
 617	AppleEvent reply;
 618	AESendMode sendMode;
 619	AESendPriority sendPriority;
 620	long timeOutInTicks;
 621#ifndef AESend
 622	PyMac_PRECHECK(AESend);
 623#endif
 624	if (!PyArg_ParseTuple(_args, "lhl",
 625	                      &sendMode,
 626	                      &sendPriority,
 627	                      &timeOutInTicks))
 628		return NULL;
 629	_err = AESend(&_self->ob_itself,
 630	              &reply,
 631	              sendMode,
 632	              sendPriority,
 633	              timeOutInTicks,
 634	              upp_AEIdleProc,
 635	              (AEFilterUPP)0);
 636	if (_err != noErr) return PyMac_Error(_err);
 637	_res = Py_BuildValue("O&",
 638	                     AEDesc_New, &reply);
 639	return _res;
 640}
 641
 642static PyObject *AEDesc_AEResetTimer(AEDescObject *_self, PyObject *_args)
 643{
 644	PyObject *_res = NULL;
 645	OSErr _err;
 646#ifndef AEResetTimer
 647	PyMac_PRECHECK(AEResetTimer);
 648#endif
 649	if (!PyArg_ParseTuple(_args, ""))
 650		return NULL;
 651	_err = AEResetTimer(&_self->ob_itself);
 652	if (_err != noErr) return PyMac_Error(_err);
 653	Py_INCREF(Py_None);
 654	_res = Py_None;
 655	return _res;
 656}
 657
 658static PyObject *AEDesc_AESuspendTheCurrentEvent(AEDescObject *_self, PyObject *_args)
 659{
 660	PyObject *_res = NULL;
 661	OSErr _err;
 662#ifndef AESuspendTheCurrentEvent
 663	PyMac_PRECHECK(AESuspendTheCurrentEvent);
 664#endif
 665	if (!PyArg_ParseTuple(_args, ""))
 666		return NULL;
 667	_err = AESuspendTheCurrentEvent(&_self->ob_itself);
 668	if (_err != noErr) return PyMac_Error(_err);
 669	Py_INCREF(Py_None);
 670	_res = Py_None;
 671	return _res;
 672}
 673
 674static PyObject *AEDesc_AEResumeTheCurrentEvent(AEDescObject *_self, PyObject *_args)
 675{
 676	PyObject *_res = NULL;
 677	OSErr _err;
 678	AppleEvent reply;
 679	AEEventHandlerUPP dispatcher__proc__ = upp_GenericEventHandler;
 680	PyObject *dispatcher;
 681#ifndef AEResumeTheCurrentEvent
 682	PyMac_PRECHECK(AEResumeTheCurrentEvent);
 683#endif
 684	if (!PyArg_ParseTuple(_args, "O&O",
 685	                      AEDesc_Convert, &reply,
 686	                      &dispatcher))
 687		return NULL;
 688	_err = AEResumeTheCurrentEvent(&_self->ob_itself,
 689	                               &reply,
 690	                               dispatcher__proc__, 
 691				       (SRefCon)dispatcher);
 692	if (_err != noErr) return PyMac_Error(_err);
 693	Py_INCREF(Py_None);
 694	_res = Py_None;
 695	Py_INCREF(dispatcher); /* XXX leak, but needed */
 696	return _res;
 697}
 698
 699static PyObject *AEDesc_AEGetTheCurrentEvent(AEDescObject *_self, PyObject *_args)
 700{
 701	PyObject *_res = NULL;
 702	OSErr _err;
 703#ifndef AEGetTheCurrentEvent
 704	PyMac_PRECHECK(AEGetTheCurrentEvent);
 705#endif
 706	if (!PyArg_ParseTuple(_args, ""))
 707		return NULL;
 708	_err = AEGetTheCurrentEvent(&_self->ob_itself);
 709	if (_err != noErr) return PyMac_Error(_err);
 710	Py_INCREF(Py_None);
 711	_res = Py_None;
 712	return _res;
 713}
 714
 715static PyObject *AEDesc_AESetTheCurrentEvent(AEDescObject *_self, PyObject *_args)
 716{
 717	PyObject *_res = NULL;
 718	OSErr _err;
 719#ifndef AESetTheCurrentEvent
 720	PyMac_PRECHECK(AESetTheCurrentEvent);
 721#endif
 722	if (!PyArg_ParseTuple(_args, ""))
 723		return NULL;
 724	_err = AESetTheCurrentEvent(&_self->ob_itself);
 725	if (_err != noErr) return PyMac_Error(_err);
 726	Py_INCREF(Py_None);
 727	_res = Py_None;
 728	return _res;
 729}
 730
 731static PyObject *AEDesc_AEResolve(AEDescObject *_self, PyObject *_args)
 732{
 733	PyObject *_res = NULL;
 734	OSErr _err;
 735	short callbackFlags;
 736	AEDesc theToken;
 737#ifndef AEResolve
 738	PyMac_PRECHECK(AEResolve);
 739#endif
 740	if (!PyArg_ParseTuple(_args, "h",
 741	                      &callbackFlags))
 742		return NULL;
 743	_err = AEResolve(&_self->ob_itself,
 744	                 callbackFlags,
 745	                 &theToken);
 746	if (_err != noErr) return PyMac_Error(_err);
 747	_res = Py_BuildValue("O&",
 748	                     AEDesc_New, &theToken);
 749	return _res;
 750}
 751
 752static PyObject *AEDesc_AutoDispose(AEDescObject *_self, PyObject *_args)
 753{
 754	PyObject *_res = NULL;
 755
 756	int onoff, old;
 757	if (!PyArg_ParseTuple(_args, "i", &onoff))
 758	        return NULL;
 759	old = _self->ob_owned;
 760	_self->ob_owned = onoff;
 761	_res = Py_BuildValue("i", old);
 762	return _res;
 763
 764}
 765
 766static PyMethodDef AEDesc_methods[] = {
 767	{"AECoerceDesc", (PyCFunction)AEDesc_AECoerceDesc, 1,
 768	 PyDoc_STR("(DescType toType) -> (AEDesc result)")},
 769	{"AEDuplicateDesc", (PyCFunction)AEDesc_AEDuplicateDesc, 1,
 770	 PyDoc_STR("() -> (AEDesc result)")},
 771	{"AECountItems", (PyCFunction)AEDesc_AECountItems, 1,
 772	 PyDoc_STR("() -> (long theCount)")},
 773	{"AEPutPtr", (PyCFunction)AEDesc_AEPutPtr, 1,
 774	 PyDoc_STR("(long index, DescType typeCode, Buffer dataPtr) -> None")},
 775	{"AEPutDesc", (PyCFunction)AEDesc_AEPutDesc, 1,
 776	 PyDoc_STR("(long index, AEDesc theAEDesc) -> None")},
 777	{"AEGetNthPtr", (PyCFunction)AEDesc_AEGetNthPtr, 1,
 778	 PyDoc_STR("(long index, DescType desiredType, Buffer dataPtr) -> (AEKeyword theAEKeyword, DescType typeCode, Buffer dataPtr)")},
 779	{"AEGetNthDesc", (PyCFunction)AEDesc_AEGetNthDesc, 1,
 780	 PyDoc_STR("(long index, DescType desiredType) -> (AEKeyword theAEKeyword, AEDesc result)")},
 781	{"AESizeOfNthItem", (PyCFunction)AEDesc_AESizeOfNthItem, 1,
 782	 PyDoc_STR("(long index) -> (DescType typeCode, Size dataSize)")},
 783	{"AEDeleteItem", (PyCFunction)AEDesc_AEDeleteItem, 1,
 784	 PyDoc_STR("(long index) -> None")},
 785	{"AEPutParamPtr", (PyCFunction)AEDesc_AEPutParamPtr, 1,
 786	 PyDoc_STR("(AEKeyword theAEKeyword, DescType typeCode, Buffer dataPtr) -> None")},
 787	{"AEPutParamDesc", (PyCFunction)AEDesc_AEPutParamDesc, 1,
 788	 PyDoc_STR("(AEKeyword theAEKeyword, AEDesc theAEDesc) -> None")},
 789	{"AEGetParamPtr", (PyCFunction)AEDesc_AEGetParamPtr, 1,
 790	 PyDoc_STR("(AEKeyword theAEKeyword, DescType desiredType, Buffer dataPtr) -> (DescType typeCode, Buffer dataPtr)")},
 791	{"AEGetParamDesc", (PyCFunction)AEDesc_AEGetParamDesc, 1,
 792	 PyDoc_STR("(AEKeyword theAEKeyword, DescType desiredType) -> (AEDesc result)")},
 793	{"AESizeOfParam", (PyCFunction)AEDesc_AESizeOfParam, 1,
 794	 PyDoc_STR("(AEKeyword theAEKeyword) -> (DescType typeCode, Size dataSize)")},
 795	{"AEDeleteParam", (PyCFunction)AEDesc_AEDeleteParam, 1,
 796	 PyDoc_STR("(AEKeyword theAEKeyword) -> None")},
 797	{"AEGetAttributePtr", (PyCFunction)AEDesc_AEGetAttributePtr, 1,
 798	 PyDoc_STR("(AEKeyword theAEKeyword, DescType desiredType, Buffer dataPtr) -> (DescType typeCode, Buffer dataPtr)")},
 799	{"AEGetAttributeDesc", (PyCFunction)AEDesc_AEGetAttributeDesc, 1,
 800	 PyDoc_STR("(AEKeyword theAEKeyword, DescType desiredType) -> (AEDesc result)")},
 801	{"AESizeOfAttribute", (PyCFunction)AEDesc_AESizeOfAttribute, 1,
 802	 PyDoc_STR("(AEKeyword theAEKeyword) -> (DescType typeCode, Size dataSize)")},
 803	{"AEPutAttributePtr", (PyCFunction)AEDesc_AEPutAttributePtr, 1,
 804	 PyDoc_STR("(AEKeyword theAEKeyword, DescType typeCode, Buffer dataPtr) -> None")},
 805	{"AEPutAttributeDesc", (PyCFunction)AEDesc_AEPutAttributeDesc, 1,
 806	 PyDoc_STR("(AEKeyword theAEKeyword, AEDesc theAEDesc) -> None")},
 807	{"AEGetDescDataSize", (PyCFunction)AEDesc_AEGetDescDataSize, 1,
 808	 PyDoc_STR("() -> (Size _rv)")},
 809	{"AESend", (PyCFunction)AEDesc_AESend, 1,
 810	 PyDoc_STR("(AESendMode sendMode, AESendPriority sendPriority, long timeOutInTicks) -> (AppleEvent reply)")},
 811	{"AEResetTimer", (PyCFunction)AEDesc_AEResetTimer, 1,
 812	 PyDoc_STR("() -> None")},
 813	{"AESuspendTheCurrentEvent", (PyCFunction)AEDesc_AESuspendTheCurrentEvent, 1,
 814	 PyDoc_STR("() -> None")},
 815	{"AEResumeTheCurrentEvent", (PyCFunction)AEDesc_AEResumeTheCurrentEvent, 1,
 816	 PyDoc_STR("(AppleEvent reply, EventHandler dispatcher) -> None")},
 817	{"AEGetTheCurrentEvent", (PyCFunction)AEDesc_AEGetTheCurrentEvent, 1,
 818	 PyDoc_STR("() -> None")},
 819	{"AESetTheCurrentEvent", (PyCFunction)AEDesc_AESetTheCurrentEvent, 1,
 820	 PyDoc_STR("() -> None")},
 821	{"AEResolve", (PyCFunction)AEDesc_AEResolve, 1,
 822	 PyDoc_STR("(short callbackFlags) -> (AEDesc theToken)")},
 823	{"AutoDispose", (PyCFunction)AEDesc_AutoDispose, 1,
 824	 PyDoc_STR("(int)->int. Automatically AEDisposeDesc the object on Python object cleanup")},
 825	{NULL, NULL, 0}
 826};
 827
 828static PyObject *AEDesc_get_type(AEDescObject *self, void *closure)
 829{
 830	return PyMac_BuildOSType(self->ob_itself.descriptorType);
 831}
 832
 833#define AEDesc_set_type NULL
 834
 835static PyObject *AEDesc_get_data(AEDescObject *self, void *closure)
 836{
 837	PyObject *res;
 838	Size size;
 839	char *ptr;
 840	OSErr err;
 841
 842	size = AEGetDescDataSize(&self->ob_itself);
 843	if ( (res = PyString_FromStringAndSize(NULL, size)) == NULL )
 844		return NULL;
 845	if ( (ptr = PyString_AsString(res)) == NULL )
 846		return NULL;
 847	if ( (err=AEGetDescData(&self->ob_itself, ptr, size)) < 0 )
 848		return PyMac_Error(err);
 849	return res;
 850}
 851
 852#define AEDesc_set_data NULL
 853
 854static PyGetSetDef AEDesc_getsetlist[] = {
 855	{"type", (getter)AEDesc_get_type, (setter)AEDesc_set_type, "Type of this AEDesc"},
 856	{"data", (getter)AEDesc_get_data, (setter)AEDesc_set_data, "The raw data in this AEDesc"},
 857	{NULL, NULL, NULL, NULL},
 858};
 859
 860
 861#define AEDesc_compare NULL
 862
 863#define AEDesc_repr NULL
 864
 865#define AEDesc_hash NULL
 866#define AEDesc_tp_init 0
 867
 868#define AEDesc_tp_alloc PyType_GenericAlloc
 869
 870static PyObject *AEDesc_tp_new(PyTypeObject *type, PyObject *_args, PyObject *_kwds)
 871{
 872	PyObject *_self;
 873	AEDesc itself;
 874	char *kw[] = {"itself", 0};
 875
 876	if (!PyArg_ParseTupleAndKeywords(_args, _kwds, "O&", kw, AEDesc_Convert, &itself)) return NULL;
 877	if ((_self = type->tp_alloc(type, 0)) == NULL) return NULL;
 878	((AEDescObject *)_self)->ob_itself = itself;
 879	return _self;
 880}
 881
 882#define AEDesc_tp_free PyObject_Del
 883
 884
 885PyTypeObject AEDesc_Type = {
 886	PyObject_HEAD_INIT(NULL)
 887	0, /*ob_size*/
 888	"_AE.AEDesc", /*tp_name*/
 889	sizeof(AEDescObject), /*tp_basicsize*/
 890	0, /*tp_itemsize*/
 891	/* methods */
 892	(destructor) AEDesc_dealloc, /*tp_dealloc*/
 893	0, /*tp_print*/
 894	(getattrfunc)0, /*tp_getattr*/
 895	(setattrfunc)0, /*tp_setattr*/
 896	(cmpfunc) AEDesc_compare, /*tp_compare*/
 897	(reprfunc) AEDesc_repr, /*tp_repr*/
 898	(PyNumberMethods *)0, /* tp_as_number */
 899	(PySequenceMethods *)0, /* tp_as_sequence */
 900	(PyMappingMethods *)0, /* tp_as_mapping */
 901	(hashfunc) AEDesc_hash, /*tp_hash*/
 902	0, /*tp_call*/
 903	0, /*tp_str*/
 904	PyObject_GenericGetAttr, /*tp_getattro*/
 905	PyObject_GenericSetAttr, /*tp_setattro */
 906	0, /*tp_as_buffer*/
 907	Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
 908	0, /*tp_doc*/
 909	0, /*tp_traverse*/
 910	0, /*tp_clear*/
 911	0, /*tp_richcompare*/
 912	0, /*tp_weaklistoffset*/
 913	0, /*tp_iter*/
 914	0, /*tp_iternext*/
 915	AEDesc_methods, /* tp_methods */
 916	0, /*tp_members*/
 917	AEDesc_getsetlist, /*tp_getset*/
 918	0, /*tp_base*/
 919	0, /*tp_dict*/
 920	0, /*tp_descr_get*/
 921	0, /*tp_descr_set*/
 922	0, /*tp_dictoffset*/
 923	AEDesc_tp_init, /* tp_init */
 924	AEDesc_tp_alloc, /* tp_alloc */
 925	AEDesc_tp_new, /* tp_new */
 926	AEDesc_tp_free, /* tp_free */
 927};
 928
 929/* --------------------- End object type AEDesc --------------------- */
 930
 931
 932static PyObject *AE_AECoercePtr(PyObject *_self, PyObject *_args)
 933{
 934	PyObject *_res = NULL;
 935	OSErr _err;
 936	DescType typeCode;
 937	char *dataPtr__in__;
 938	long dataPtr__len__;
 939	int dataPtr__in_len__;
 940	DescType toType;
 941	AEDesc result;
 942#ifndef AECoercePtr
 943	PyMac_PRECHECK(AECoercePtr);
 944#endif
 945	if (!PyArg_ParseTuple(_args, "O&s#O&",
 946	                      PyMac_GetOSType, &typeCode,
 947	                      &dataPtr__in__, &dataPtr__in_len__,
 948	                      PyMac_GetOSType, &toType))
 949		return NULL;
 950	dataPtr__len__ = dataPtr__in_len__;
 951	_err = AECoercePtr(typeCode,
 952	                   dataPtr__in__, dataPtr__len__,
 953	                   toType,
 954	                   &result);
 955	if (_err != noErr) return PyMac_Error(_err);
 956	_res = Py_BuildValue("O&",
 957	                     AEDesc_New, &result);
 958	return _res;
 959}
 960
 961static PyObject *AE_AECreateDesc(PyObject *_self, PyObject *_args)
 962{
 963	PyObject *_res = NULL;
 964	OSErr _err;
 965	DescType typeCode;
 966	char *dataPtr__in__;
 967	long dataPtr__len__;
 968	int dataPtr__in_len__;
 969	AEDesc result;
 970#ifndef AECreateDesc
 971	PyMac_PRECHECK(AECreateDesc);
 972#endif
 973	if (!PyArg_ParseTuple(_args, "O&s#",
 974	                      PyMac_GetOSType, &typeCode,
 975	                      &dataPtr__in__, &dataPtr__in_len__))
 976		return NULL;
 977	dataPtr__len__ = dataPtr__in_len__;
 978	_err = AECreateDesc(typeCode,
 979	                    dataPtr__in__, dataPtr__len__,
 980	                    &result);
 981	if (_err != noErr) return PyMac_Error(_err);
 982	_res = Py_BuildValue("O&",
 983	                     AEDesc_New, &result);
 984	return _res;
 985}
 986
 987static PyObject *AE_AECreateList(PyObject *_self, PyObject *_args)
 988{
 989	PyObject *_res = NULL;
 990	OSErr _err;
 991	char *factoringPtr__in__;
 992	long factoringPtr__len__;
 993	int factoringPtr__in_len__;
 994	Boolean isRecord;
 995	AEDescList resultList;
 996#ifndef AECreateList
 997	PyMac_PRECHECK(AECreateList);
 998#endif
 999	if (!PyArg_ParseTuple(_args, "s#b",
1000	                      &factoringPtr__in__, &factoringPtr__in_len__,
1001	                      &isRecord))
1002		return NULL;
1003	factoringPtr__len__ = factoringPtr__in_len__;
1004	_err = AECreateList(factoringPtr__in__, factoringPtr__len__,
1005	                    isRecord,
1006	                    &resultList);
1007	if (_err != noErr) return PyMac_Error(_err);
1008	_res = Py_BuildValue("O&",
1009	                     AEDesc_New, &resultList);
1010	return _res;
1011}
1012
1013static PyObject *AE_AECreateAppleEvent(PyObject *_self, PyObject *_args)
1014{
1015	PyObject *_res = NULL;
1016	OSErr _err;
1017	AEEventClass theAEEventClass;
1018	AEEventID theAEEventID;
1019	AEAddressDesc target;
1020	AEReturnID returnID;
1021	AETransactionID transactionID;
1022	AppleEvent result;
1023#ifndef AECreateAppleEvent
1024	PyMac_PRECHECK(AECreateAppleEvent);
1025#endif
1026	if (!PyArg_ParseTuple(_args, "O&O&O&hl",
1027	                      PyMac_GetOSType, &theAEEventClass,
1028	                      PyMac_GetOSType, &theAEEventID,
1029	                      AEDesc_Convert, &target,
1030	                      &returnID,
1031	                      &transactionID))
1032		return NULL;
1033	_err = AECreateAppleEvent(theAEEventClass,
1034	                          theAEEventID,
1035	                          &target,
1036	                          returnID,
1037	                          transactionID,
1038	                          &result);
1039	if (_err != noErr) return PyMac_Error(_err);
1040	_res = Py_BuildValue("O&",
1041	                     AEDesc_New, &result);
1042	return _res;
1043}
1044
1045static PyObject *AE_AEReplaceDescData(PyObject *_self, PyObject *_args)
1046{
1047	PyObject *_res = NULL;
1048	OSErr _err;
1049	DescType typeCode;
1050	char *dataPtr__in__;
1051	long dataPtr__len__;
1052	int dataPtr__in_len__;
1053	AEDesc theAEDesc;
1054#ifndef AEReplaceDescData
1055	PyMac_PRECHECK(AEReplaceDescData);
1056#endif
1057	if (!PyArg_ParseTuple(_args, "O&s#",
1058	                      PyMac_GetOSType, &typeCode,
1059	                      &dataPtr__in__, &dataPtr__in_len__))
1060		return NULL;
1061	dataPtr__len__ = dataPtr__in_len__;
1062	_err = AEReplaceDescData(typeCode,
1063	                         dataPtr__in__, dataPtr__len__,
1064	                         &theAEDesc);
1065	if (_err != noErr) return PyMac_Error(_err);
1066	_res = Py_BuildValue("O&",
1067	                     AEDesc_New, &theAEDesc);
1068	return _res;
1069}
1070
1071static PyObject *AE_AEProcessAppleEvent(PyObject *_self, PyObject *_args)
1072{
1073	PyObject *_res = NULL;
1074	OSErr _err;
1075	EventRecord theEventRecord;
1076#ifndef AEProcessAppleEvent
1077	PyMac_PRECHECK(AEProcessAppleEvent);
1078#endif
1079	if (!PyArg_ParseTuple(_args, "O&",
1080	                      PyMac_GetEventRecord, &theEventRecord))
1081		return NULL;
1082	_err = AEProcessAppleEvent(&theEventRecord);
1083	if (_err != noErr) return PyMac_Error(_err);
1084	Py_INCREF(Py_None);
1085	_res = Py_None;
1086	return _res;
1087}
1088
1089static PyObject *AE_AEGetInteractionAllowed(PyObject *_self, PyObject *_args)
1090{
1091	PyObject *_res = NULL;
1092	OSErr _err;
1093	AEInteractAllowed level;
1094#ifndef AEGetInteractionAllowed
1095	PyMac_PRECHECK(AEGetInteractionAllowed);
1096#endif
1097	if (!PyArg_ParseTuple(_args, ""))
1098		return NULL;
1099	_err = AEGetInteractionAllowed(&level);
1100	if (_err != noErr) return PyMac_Error(_err);
1101	_res = Py_BuildValue("b",
1102	                     level);
1103	return _res;
1104}
1105
1106static PyObject *AE_AESetInteractionAllowed(PyObject *_self, PyObject *_args)
1107{
1108	PyObject *_res = NULL;
1109	OSErr _err;
1110	AEInteractAllowed level;
1111#ifndef AESetInteractionAllowed
1112	PyMac_PRECHECK(AESetInteractionAllowed);
1113#endif
1114	if (!PyArg_ParseTuple(_args, "b",
1115	                      &level))
1116		return NULL;
1117	_err = AESetInteractionAllowed(level);
1118	if (_err != noErr) return PyMac_Error(_err);
1119	Py_INCREF(Py_None);
1120	_res = Py_None;
1121	return _res;
1122}
1123
1124static PyObject *AE_AEInteractWithUser(PyObject *_self, PyObject *_args)
1125{
1126	PyObject *_res = NULL;
1127	OSErr _err;
1128	long timeOutInTicks;
1129#ifndef AEInteractWithUser
1130	PyMac_PRECHECK(AEInteractWithUser);
1131#endif
1132	if (!PyArg_ParseTuple(_args, "l",
1133	                      &timeOutInTicks))
1134		return NULL;
1135	_err = AEInteractWithUser(timeOutInTicks,
1136	                          (NMRecPtr)0,
1137	                          upp_AEIdleProc);
1138	if (_err != noErr) return PyMac_Error(_err);
1139	Py_INCREF(Py_None);
1140	_res = Py_None;
1141	return _res;
1142}
1143
1144static PyObject *AE_AEInstallEventHandler(PyObject *_self, PyObject *_args)
1145{
1146	PyObject *_res = NULL;
1147	OSErr _err;
1148	AEEventClass theAEEventClass;
1149	AEEventID theAEEventID;
1150	AEEventHandlerUPP handler__proc__ = upp_GenericEventHandler;
1151	PyObject *handler;
1152#ifndef AEInstallEventHandler
1153	PyMac_PRECHECK(AEInstallEventHandler);
1154#endif
1155	if (!PyArg_ParseTuple(_args, "O&O&O",
1156	                      PyMac_GetOSType, &theAEEventClass,
1157	                      PyMac_GetOSType, &theAEEventID,
1158	                      &handler))
1159		return NULL;
1160	_err = AEInstallEventHandler(theAEEventClass,
1161	                             theAEEventID,
1162	                             handler__proc__, (SRefCon)handler,
1163	                             0);
1164	if (_err != noErr) return PyMac_Error(_err);
1165	Py_INCREF(Py_None);
1166	_res = Py_None;
1167	Py_INCREF(handler); /* XXX leak, but needed */
1168	return _res;
1169}
1170
1171static PyObject *AE_AERemoveEventHandler(PyObject *_self, PyObject *_args)
1172{
1173	PyObject *_res = NULL;
1174	OSErr _err;
1175	AEEventClass theAEEventClass;
1176	AEEventID theAEEventID;
1177#ifndef AERemoveEventHandler
1178	PyMac_PRECHECK(AERemoveEventHandler);
1179#endif
1180	if (!PyArg_ParseTuple(_args, "O&O&",
1181	                      PyMac_GetOSType, &theAEEventClass,
1182	                      PyMac_GetOSType, &theAEEventID))
1183		return NULL;
1184	_err = AERemoveEventHandler(theAEEventClass,
1185	                            theAEEventID,
1186	                            upp_GenericEventHandler,
1187	                            0);
1188	if (_err != noErr) return PyMac_Error(_err);
1189	Py_INCREF(Py_None);
1190	_res = Py_None;
1191	return _res;
1192}
1193
1194static PyObject *AE_AEGetEventHandler(PyObject *_self, PyObject *_args)
1195{
1196	PyObject *_res = NULL;
1197	OSErr _err;
1198	AEEventClass theAEEventClass;
1199	AEEventID theAEEventID;
1200	AEEventHandlerUPP handler__proc__ = upp_GenericEventHandler;
1201	PyObject *handler;
1202#ifndef AEGetEventHandler
1203	PyMac_PRECHECK(AEGetEventHandler);
1204#endif
1205	if (!PyArg_ParseTuple(_args, "O&O&",
1206	                      PyMac_GetOSType, &theAEEventClass,
1207	                      PyMac_GetOSType, &theAEEventID))
1208		return NULL;
1209	_err = AEGetEventHandler(theAEEventClass,
1210	                         theAEEventID,
1211	                         &handler__proc__, (SRefCon *)&handler,
1212	                         0);
1213	if (_err != noErr) return PyMac_Error(_err);
1214	_res = Py_BuildValue("O",
1215	                     handler);
1216	Py_INCREF(handler); /* XXX leak, but needed */
1217	return _res;
1218}
1219
1220static PyObject *AE_AEInstallSpecialHandler(PyObject *_self, PyObject *_args)
1221{
1222	PyObject *_res = NULL;
1223	OSErr _err;
1224	AEKeyword functionClass;
1225#ifndef AEInstallSpecialHandler
1226	PyMac_PRECHECK(AEInstallSpecialHandler);
1227#endif
1228	if (!PyArg_ParseTuple(_args, "O&",
1229	                      PyMac_GetOSType, &functionClass))
1230		return NULL;
1231	_err = AEInstallSpecialHandler(functionClass,
1232	                               upp_GenericEventHandler,
1233	                               0);
1234	if (_err != noErr) return PyMac_Error(_err);
1235	Py_INCREF(Py_None);
1236	_res = Py_None;
1237	return _res;
1238}
1239
1240static PyObject *AE_AERemoveSpecialHandler(PyObject *_self, PyObject *_args)
1241{
1242	PyObject *_res = NULL;
1243	OSErr _err;
1244	AEKeyword functionClass;
1245#ifndef AERemoveSpecialHandler
1246	PyMac_PRECHECK(AERemoveSpecialHandler);
1247#endif
1248	if (!PyArg_ParseTuple(_args, "O&",
1249	                      PyMac_GetOSType, &functionClass))
1250		return NULL;
1251	_err = AERemoveSpecialHandler(functionClass,
1252	                              upp_GenericEventHandler,
1253	                              0);
1254	if (_err != noErr) return PyMac_Error(_err);
1255	Py_INCREF(Py_None);
1256	_res = Py_None;
1257	return _res;
1258}
1259
1260static PyObject *AE_AEManagerInfo(PyObject *_self, PyObject *_args)
1261{
1262	PyObject *_res = NULL;
1263	OSErr _err;
1264	AEKeyword keyWord;
1265	long result;
1266#ifndef AEManagerInfo
1267	PyMac_PRECHECK(AEManagerInfo);
1268#endif
1269	if (!PyArg_ParseTuple(_args, "O&",
1270	                      PyMac_GetOSType, &keyWord))
1271		return NULL;
1272	_err = AEManagerInfo(keyWord,
1273	                     &result);
1274	if (_err != noErr) return PyMac_Error(_err);
1275	_res = Py_BuildValue("l",
1276	                     result);
1277	return _res;
1278}
1279
1280static PyObject *AE_AEObjectInit(PyObject *_self, PyObject *_args)
1281{
1282	PyObject *_res = NULL;
1283	OSErr _err;
1284#ifndef AEObjectInit
1285	PyMac_PRECHECK(AEObjectInit);
1286#endif
1287	if (!PyArg_ParseTuple(_args, ""))
1288		return NULL;
1289	_err = AEObjectInit();
1290	if (_err != noErr) return PyMac_Error(_err);
1291	Py_INCREF(Py_None);
1292	_res = Py_None;
1293	return _res;
1294}
1295
1296static PyObject *AE_AEDisposeToken(PyObject *_self, PyObject *_args)
1297{
1298	PyObject *_res = NULL;
1299	OSErr _err;
1300	AEDesc theToken;
1301#ifndef AEDisposeToken
1302	PyMac_PRECHECK(AEDisposeToken);
1303#endif
1304	if (!PyArg_ParseTuple(_args, ""))
1305		return NULL;
1306	_err = AEDisposeToken(&theToken);
1307	if (_err != noErr) return PyMac_Error(_err);
1308	_res = Py_BuildValue("O&",
1309	                     AEDesc_New, &theToken);
1310	return _res;
1311}
1312
1313static PyObject *AE_AECallObjectAccessor(PyObject *_self, PyObject *_args)
1314{
1315	PyObject *_res = NULL;
1316	OSErr _err;
1317	DescType desiredClass;
1318	AEDesc containerToken;
1319	DescType containerClass;
1320	DescType keyForm;
1321	AEDesc keyData;
1322	AEDesc token;
1323#ifndef AECallObjectAccessor
1324	PyMac_PRECHECK(AECallObjectAccessor);
1325#endif
1326	if (!PyArg_ParseTuple(_args, "O&O&O&O&O&",
1327	                      PyMac_GetOSType, &desiredClass,
1328	                      AEDesc_Convert, &containerToken,
1329	                      PyMac_GetOSType, &containerClass,
1330	                      PyMac_GetOSType, &keyForm,
1331	                      AEDesc_Convert, &keyData))
1332		return NULL;
1333	_err = AECallObjectAccessor(desiredClass,
1334	                            &containerToken,
1335	                            containerClass,
1336	                            keyForm,
1337	                            &keyData,
1338	                            &token);
1339	if (_err != noErr) return PyMac_Error(_err);
1340	_res = Py_BuildValue("O&",
1341	                     AEDesc_New, &token);
1342	return _res;
1343}
1344
1345static PyMethodDef AE_methods[] = {
1346	{"AECoercePtr", (PyCFunction)AE_AECoercePtr, 1,
1347	 PyDoc_STR("(DescType typeCode, Buffer dataPtr, DescType toType) -> (AEDesc result)")},
1348	{"AECreateDesc", (PyCFunction)AE_AECreateDesc, 1,
1349	 PyDoc_STR("(DescType typeCode, Buffer dataPtr) -> (AEDesc result)")},
1350	{"AECreateList", (PyCFunction)AE_AECreateList, 1,
1351	 PyDoc_STR("(Buffer factoringPtr, Boolean isRecord) -> (AEDescList resultList)")},
1352	{"AECreateAppleEvent", (PyCFunction)AE_AECreateAppleEvent, 1,
1353	 PyDoc_STR("(AEEventClass theAEEventClass, AEEventID theAEEventID, AEAddressDesc target, AEReturnID returnID, AETransactionID transactionID) -> (AppleEvent result)")},
1354	{"AEReplaceDescData", (PyCFunction)AE_AEReplaceDescData, 1,
1355	 PyDoc_STR("(DescType typeCode, Buffer dataPtr) -> (AEDesc theAEDesc)")},
1356	{"AEProcessAppleEvent", (PyCFunction)AE_AEProcessAppleEvent, 1,
1357	 PyDoc_STR("(EventRecord theEventRecord) -> None")},
1358	{"AEGetInteractionAllowed", (PyCFunction)AE_AEGetInteractionAllowed, 1,
1359	 PyDoc_STR("() -> (AEInteractAllowed level)")},
1360	{"AESetInteractionAllowed", (PyCFunction)AE_AESetInteractionAllowed, 1,
1361	 PyDoc_STR("(AEInteractAllowed level) -> None")},
1362	{"AEInteractWithUser", (PyCFunction)AE_AEInteractWithUser, 1,
1363	 PyDoc_STR("(long timeOutInTicks) -> None")},
1364	{"AEInstallEventHandler", (PyCFunction)AE_AEInstallEventHandler, 1,
1365	 PyDoc_STR("(AEEventClass theAEEventClass, AEEventID theAEEventID, EventHandler handler) -> None")},
1366	{"AERemoveEventHandler", (PyCFunction)AE_AERemoveEventHandler, 1,
1367	 PyDoc_STR("(AEEventClass theAEEventClass, AEEventID theAEEventID) -> None")},
1368	{"AEGetEventHandler", (PyCFunction)AE_AEGetEventHandler, 1,
1369	 PyDoc_STR("(AEEventClass theAEEventClass, AEEventID theAEEventID) -> (EventHandler handler)")},
1370	{"AEInstallSpecialHandler", (PyCFunction)AE_AEInstallSpecialHandler, 1,
1371	 PyDoc_STR("(AEKeyword functionClass) -> None")},
1372	{"AERemoveSpecialHandler", (PyCFunction)AE_AERemoveSpecialHandler, 1,
1373	 PyDoc_STR("(AEKeyword functionClass) -> None")},
1374	{"AEManagerInfo", (PyCFunction)AE_AEManagerInfo, 1,
1375	 PyDoc_STR("(AEKeyword keyWord) -> (long result)")},
1376	{"AEObjectInit", (PyCFunction)AE_AEObjectInit, 1,
1377	 PyDoc_STR("() -> None")},
1378	{"AEDisposeToken", (PyCFunction)AE_AEDisposeToken, 1,
1379	 PyDoc_STR("() -> (AEDesc theToken)")},
1380	{"AECallObjectAccessor", (PyCFunction)AE_AECallObjectAccessor, 1,
1381	 PyDoc_STR("(DescType desiredClass, AEDesc containerToken, DescType containerClass, DescType keyForm, AEDesc keyData) -> (AEDesc token)")},
1382	{NULL, NULL, 0}
1383};
1384
1385
1386
1387static pascal OSErr
1388GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon)
1389{
1390        PyObject *handler = (PyObject *)refcon;
1391        AEDescObject *requestObject, *replyObject;
1392        PyObject *args, *res;
1393        if ((requestObject = (AEDescObject *)AEDesc_New((AppleEvent *)request)) == NULL) {
1394                return -1;
1395        }
1396        if ((replyObject = (AEDescObject *)AEDesc_New(reply)) == NULL) {
1397                Py_DECREF(requestObject);
1398                return -1;
1399        }
1400        if ((args = Py_BuildValue("OO", requestObject, replyObject)) == NULL) {
1401                Py_DECREF(requestObject);
1402                Py_DECREF(replyObject);
1403                return -1;
1404        }
1405        res = PyEval_CallObject(handler, args);
1406        requestObject->ob_itself.descriptorType = 'null';
1407        requestObject->ob_itself.dataHandle = NULL;
1408        replyObject->ob_itself.descriptorType = 'null';
1409        replyObject->ob_itself.dataHandle = NULL;
1410        Py_DECREF(args);
1411        if (res == NULL) {
1412                PySys_WriteStderr("Exception in AE event handler function\n");
1413                PyErr_Print();
1414                return -1;
1415        }
1416        Py_DECREF(res);
1417        return noErr;
1418}
1419
1420PyObject *AEDesc_NewBorrowed(AEDesc *itself)
1421{
1422        PyObject *it;
1423
1424        it = AEDesc_New(itself);
1425        if (it)
1426                ((AEDescObject *)it)->ob_owned = 0;
1427        return (PyObject *)it;
1428}
1429
1430
1431
1432void init_AE(void)
1433{
1434	PyObject *m;
1435	PyObject *d;
1436
1437        upp_AEIdleProc = NewAEIdleUPP(AEIdleProc);
1438        upp_GenericEventHandler = NewAEEventHandlerUPP(GenericEventHandler);
1439        PyMac_INIT_TOOLBOX_OBJECT_NEW(AEDesc *, AEDesc_New);
1440        PyMac_INIT_TOOLBOX_OBJECT_NEW(AEDesc *, AEDesc_NewBorrowed);
1441        PyMac_INIT_TOOLBOX_OBJECT_CONVERT(AEDesc, AEDesc_Convert);
1442
1443	m = Py_InitModule("_AE", AE_methods);
1444	d = PyModule_GetDict(m);
1445	AE_Error = PyMac_GetOSErrException();
1446	if (AE_Error == NULL ||
1447	    PyDict_SetItemString(d, "Error", AE_Error) != 0)
1448		return;
1449	AEDesc_Type.ob_type = &PyType_Type;
1450	if (PyType_Ready(&AEDesc_Type) < 0) return;
1451	Py_INCREF(&AEDesc_Type);
1452	PyModule_AddObject(m, "AEDesc", (PyObject *)&AEDesc_Type);
1453	/* Backward-compatible name */
1454	Py_INCREF(&AEDesc_Type);
1455	PyModule_AddObject(m, "AEDescType", (PyObject *)&AEDesc_Type);
1456}
1457
1458/* ========================= End module _AE ========================= */
1459