OSStatus AEMSendMessageThreadSafe( AppleEvent * eventPtr, AppleEvent * replyPtr, AESendMode sendMode, long timeOutInTicks ) // See comment in header. { OSStatus err = noErr; mach_port_t replyPort; assert(eventPtr != NULL); assert(replyPtr != NULL); if (sendMode && kAEWaitReply) { replyPort = MACH_PORT_NULL; // Set up the reply port if necessary. err = BindReplyMachPortToThread(&replyPort); if ( (err == noErr) && (replyPort != MACH_PORT_NULL) ) { err = AEPutAttributePtr(eventPtr, keyReplyPortAttr, typeMachPort, &replyPort, sizeof(replyPort)); } } // Call through to AESendMessage. if (err == noErr) { err = AESendMessage(eventPtr, replyPtr, sendMode, timeOutInTicks); } return err; }
static PyObject *AEDesc_AEPutAttributePtr(AEDescObject *_self, PyObject *_args) { PyObject *_res = NULL; OSErr _err; AEKeyword theAEKeyword; DescType typeCode; char *dataPtr__in__; long dataPtr__len__; int dataPtr__in_len__; #ifndef AEPutAttributePtr PyMac_PRECHECK(AEPutAttributePtr); #endif if (!PyArg_ParseTuple(_args, "O&O&s#", PyMac_GetOSType, &theAEKeyword, PyMac_GetOSType, &typeCode, &dataPtr__in__, &dataPtr__in_len__)) return NULL; dataPtr__len__ = dataPtr__in_len__; _err = AEPutAttributePtr(&_self->ob_itself, theAEKeyword, typeCode, dataPtr__in__, dataPtr__len__); if (_err != noErr) return PyMac_Error(_err); Py_INCREF(Py_None); _res = Py_None; return _res; }
static PyObject *AEDesc_AESendMach(AEDescObject *_self, PyObject *_args) { PyObject *_res = NULL; OSErr _err; AppleEvent reply; AESendMode sendMode; AESendPriority sendPriority; long timeOutInTicks; mach_port_t replyPort; #ifndef AESend PyMac_PRECHECK(AESend); #endif if (!PyArg_ParseTuple(_args, "lhl", &sendMode, &sendPriority, &timeOutInTicks)) return NULL; if (sendMode == kAEWaitReply) { if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &replyPort) != KERN_SUCCESS) { PyErr_SetString(PyExc_SystemError, "Failed to allocate Mach port!"); return NULL; } _err = AEPutAttributePtr(&_self->ob_itself, keyReplyPortAttr, typeMachPort, &replyPort, sizeof(replyPort)); if (_err != noErr) goto cleanup; } _err = AESendMessage(&_self->ob_itself, &reply, sendMode, timeOutInTicks); if (_err != noErr) goto cleanup; _res = Py_BuildValue("O&", AEDesc_New, &reply); if (sendMode == kAEWaitReply) { mach_port_destroy(mach_task_self(), replyPort); } return _res; cleanup: if (sendMode == kAEWaitReply) { mach_port_destroy(mach_task_self(), replyPort); } return PyMac_Error(_err); }
static VALUE rbosa_app_send_event (VALUE self, VALUE event_class, VALUE event_id, VALUE params, VALUE need_retval) { OSErr error; AppleEvent ae; AppleEvent reply; VALUE rb_timeout; SInt32 timeout; VALUE rb_reply; unsigned has_direct_param; error = AECreateAppleEvent (RVAL2FOURCHAR (event_class), RVAL2FOURCHAR (event_id), rbosa_element_aedesc (self), kAutoGenerateReturnID, kAnyTransactionID, &ae); if (error != noErr) rb_raise (rb_eArgError, "Cannot create Apple Event '%s%s' : %s (%d)", RVAL2CSTR (event_class), RVAL2CSTR (event_id), error_code_to_string (error), error); has_direct_param = 0; if (!NIL_P (params)) { unsigned i; for (i = 0; i < RARRAY (params)->len; i++) { VALUE ary; VALUE type; VALUE element; FourCharCode code; ary = RARRAY (params)->ptr[i]; if (NIL_P (ary) || RARRAY (ary)->len != 2) continue; type = RARRAY (ary)->ptr[0]; element = RARRAY (ary)->ptr[1]; code = RVAL2FOURCHAR (type); if (code == '----') has_direct_param = 1; error = AEPutParamDesc (&ae, RVAL2FOURCHAR (type), rbosa_element_aedesc (element)); if (error != noErr) { AEDisposeDesc (&ae); rb_raise (rb_eArgError, "Cannot add Apple Event parameter '%s' : %s (%d)", RVAL2CSTR (type), error_code_to_string (error), error); } } } rb_timeout = rb_iv_get (mOSA, "@timeout"); timeout = NIL_P (rb_timeout) ? kAEDefaultTimeout : NUM2INT (rb_timeout); if (has_direct_param == 0) AEPutAttributePtr (&ae, 'subj', typeNull, NULL, 0); error = AESend (&ae, &reply, (RVAL2CBOOL(need_retval) ? kAEWaitReply : kAENoReply) | kAECanInteract | kAECanSwitchLayer, kAENormalPriority, timeout, NULL, NULL); AEDisposeDesc (&ae); if (error != noErr) rb_raise (rb_eRuntimeError, "Cannot send Apple Event '%s%s' : %s (%d)", RVAL2CSTR (event_class), RVAL2CSTR (event_id), error_code_to_string (error), error); __rbosa_raise_potential_app_error (&reply); if (RTEST (need_retval)) { AEDesc replyObject; AEGetParamDesc (&reply, keyDirectObject, typeWildCard, &replyObject); rb_reply = rbosa_element_make (cOSAElement, &replyObject, self); } else { rb_reply = Qnil; } AEDisposeDesc (&reply); return rb_reply; }