CACFRemoteMessagePort::CACFRemoteMessagePort(CFStringRef inName, CFMessagePortInvalidationCallBack inInvalidationCallBack) : mMessagePort(NULL), mRunLoopSource(NULL) { // create the CFMessagePort mMessagePort = CFMessagePortCreateRemote(NULL, inName); if(mMessagePort != NULL) { // failure to create a remote port does not need to throw an exception // because it isn't really an error since the port in question may not // exist and this fact requires a more complex response than an excpeption // provides for. // add the invalidation callback, if any if(inInvalidationCallBack != NULL) { CFMessagePortSetInvalidationCallBack(mMessagePort, inInvalidationCallBack); } // get the run loop source mRunLoopSource = CFMessagePortCreateRunLoopSource(NULL, mMessagePort, NULL); if(mRunLoopSource == NULL) { CFRelease(mMessagePort); mMessagePort = NULL; DebugMessage("CACFRemoteMessagePort::CACFRemoteMessagePort: couldn't create the CFRunLoopSource"); throw CAException('what'); } } }
extern CFDataRef GPDuplexClient_Send(GPDuplexClientRef client, SInt32 type, CFDataRef data, Boolean expectsReturn) { CFMessagePortRef serverPort; if (client != NULL) serverPort = client->serverPort; else { serverPort = CFMessagePortCreateRemote(NULL, CFSTR("hk.kennytm.GriP.server")); if (serverPort == NULL) { CFShow(CFSTR("GPDuplexClient_Send(): Cannot create server port. Is GriP running?")); return NULL; } } if (expectsReturn) { CFDataRef retData = NULL; SInt32 errorCode = CFMessagePortSendRequest(serverPort, type, data, 4, 1, kCFRunLoopDefaultMode, &retData); if (client == NULL) CFRelease(serverPort); if (errorCode != kCFMessagePortSuccess) { CFLog(4, CFSTR("GPDuplexClient_Send(): Cannot send data %@ of type %d to server. Returning NULL. Error code = %d"), data, type, errorCode); if (retData != NULL) { CFRelease(retData); retData = NULL; } } return retData; } else { SInt32 errorCode = CFMessagePortSendRequest(serverPort, type, data, 4, 0, NULL, NULL); if (client == NULL) CFRelease(serverPort); if (errorCode != kCFMessagePortSuccess) { CFLog(4, CFSTR("GPDuplexClient_Send(): Cannot send data %@ of type %d to server. Error code = %d"), data, type, errorCode); } return NULL; } }
SC_MachMessagePort::SC_MachMessagePort(struct World *inWorld, CFStringRef serverPortName, CFStringRef replyPortName) : SC_CmdPort(inWorld), mServerPort(NULL), mReplyPort(NULL) { CFMessagePortContext context = { 0, this, NULL, NULL, NULL }; mServerPort = CFMessagePortCreateLocal(NULL, serverPortName, &messagePortCallBack, &context, NULL); if (replyPortName) mReplyPort = CFMessagePortCreateRemote(NULL, replyPortName); Start(); }
Zirk2PortClient::Zirk2PortClient() : Zirk2Port(), mRemoteMessagePort(NULL), mData(NULL) { CFMessagePortSetName(mMessagePort, CFSTR("Zirk2PortClient")); mRemoteMessagePort = CFMessagePortCreateRemote(NULL, CFSTR("Zirk2PortServer")); if (!mRemoteMessagePort) { CFShow(CFSTR("Could not create remote message port")); mRemoteMessagePort = NULL; } mData = CFDataCreateMutable(NULL, 0); }
static void SBA_refresh(void){ // still valid if (springBoardAccessMessagePort && !CFMessagePortIsValid(springBoardAccessMessagePort)){ CFRelease(springBoardAccessMessagePort); springBoardAccessMessagePort = NULL; } // create new one if (!springBoardAccessMessagePort) { springBoardAccessMessagePort = CFMessagePortCreateRemote(NULL, CFSTR(SBA_MessagePortName)); } }
static OSStatus BIMSendMessageToUIServer( BasicMessageID inMessageID ) { OSStatus result; CFDataRef sendData; CFDataRef replyData; CFMessagePortRef serverPortRef; BasicMessageHeader header; result = noErr; sendData = NULL; replyData = NULL; serverPortRef = NULL; // Create a reference to the remote message port. We identify the port using a unique, // system-wide name, in this case defined by kBasicServerPortName. serverPortRef = CFMessagePortCreateRemote( NULL, CFSTR( kBasicServerPortName ) ); if( serverPortRef == NULL ) result = -1; // Create our message header. if( result == noErr ) result = GetCurrentProcess( &header.fProcessSerialNumber ); if( result == noErr ) { sendData = CFDataCreate( NULL, (UInt8 *) &header, sizeof( BasicMessageHeader ) ); if (sendData == NULL) result = memFullErr; } // Send the message specified in iMessageID to the server. We send the message header // as data. if( result == noErr ) { if( CFMessagePortSendRequest( serverPortRef, inMessageID, sendData, 10, 10, NULL, &replyData ) != kCFMessagePortSuccess ) result = -2; } // Release everything. if( sendData ) CFRelease( sendData ); if( replyData ) CFRelease( replyData ); if( serverPortRef ) CFRelease( serverPortRef ); // Return an error code to the caller. return result; }
extern void GPStartWhenGriPIsReady(void(*initializer)()) { CFMessagePortRef serverPort = CFMessagePortCreateRemote(NULL, CFSTR("hk.kennytm.GriP.server")); if (serverPort != NULL) { // GriP is already running. call the initializer directly. initializer(); CFRelease(serverPort); } else { // GriP is not running. register for the notification and set the initializer as callback. CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), initializer, (CFNotificationCallback)&GPGriPIsReadyCallback, CFSTR("hk.kennytm.GriP.ready"), NULL, CFNotificationSuspensionBehaviorCoalesce); } }
extern GPDuplexClientRef GPDuplexClient_Create() { CFRunLoopRef runLoop = CFRunLoopGetCurrent(); GPDuplexClientRef client = calloc(1, sizeof(GPDuplexClientRef)); client->refcount = 1; client->observers = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); // (1) Obtain the server port. client->serverPort = CFMessagePortCreateRemote(NULL, CFSTR("hk.kennytm.GriP.server")); if (client->serverPort == NULL) { CFShow(CFSTR("GPDuplexClient_Init(): Cannot create server port. Is GriP running?")); GPDuplexClient_Release(client); return NULL; } // (2) ask the server port for a unique ID. CFDataRef pidData = NULL; SInt32 errorCode = CFMessagePortSendRequest(client->serverPort, GPMessage_GetClientPortID, NULL, 1, 1, kCFRunLoopDefaultMode, &pidData); if (errorCode != kCFMessagePortSuccess || pidData == NULL) { CFLog(4, CFSTR("GPDuplexClient_Init(): Cannot obtain a unique client port ID from server. Error code = %d and pidData = %@."), errorCode, pidData); if (pidData != NULL) CFRelease(pidData); GPDuplexClient_Release(client); return NULL; } // (3) Create client port from UID. const char* clientPortCString = (const char*)CFDataGetBytePtr(pidData); CFStringRef clientPortName = CFStringCreateWithCString(NULL, clientPortCString, kCFStringEncodingUTF8); CFMessagePortContext clientContext = {0, client, NULL, NULL, NULL}; Boolean shouldFreeInfo = false; client->clientPort = CFMessagePortCreateLocal(NULL, clientPortName, &GPClientCallback, &clientContext, &shouldFreeInfo); if (shouldFreeInfo || client->clientPort == NULL) { CFLog(4, CFSTR("GPDuplexClient_Init(): Cannot create client port with port name %@."), clientPortName); CFRelease(clientPortName); CFRelease(pidData); GPDuplexClient_Release(client); return NULL; } CFRelease(clientPortName); // (4) Add client port to run loop. client->clientSource = CFMessagePortCreateRunLoopSource(NULL, client->clientPort, 0); CFRunLoopAddSource(runLoop, client->clientSource, kCFRunLoopDefaultMode); CFRelease(pidData); CFLog(4, CFSTR("create client = %@"), client->clientPort); return client; }
/* * Call from CoreMIDI IO threaded callback, * we can't call Wine debug channels, critical section or anything using NtCurrentTeb here. */ void MIDIIn_SendMessage(MIDIMessage msg) { CFDataRef data; CFMessagePortRef messagePort; messagePort = CFMessagePortCreateRemote(kCFAllocatorDefault, MIDIInThreadPortName); data = CFDataCreate(kCFAllocatorDefault, (UInt8 *) &msg, sizeof(msg)); if (data) { CFMessagePortSendRequest(messagePort, 0, data, 0.0, 0.0, NULL, NULL); CFRelease(data); CFRelease(messagePort); } }
UniqueApplication::UniqueApplication( const char* id ) : m_id( id ), m_alreadyRunning( false ) { #ifdef WIN32 m_hwnd = ::FindWindow( L"QWidget", (wchar_t*)windowTitle().utf16() ); m_alreadyRunning = m_hwnd; #endif #ifdef Q_WS_MAC CFStringRef cfid = CFStringCreateWithCString( NULL, id, kCFStringEncodingISOLatin1 ); m_port = CFMessagePortCreateRemote( kCFAllocatorDefault, cfid ); m_alreadyRunning = m_port; CFRelease( cfid ); #endif #ifdef Q_WS_X11 qWarning() << "Single application instance code still unwritten!"; #endif }
LONG CoreAudio_MIDIRelease(void) { TRACE("\n"); if (MIDIIn_NumDevs > 0) { CFMessagePortRef messagePort; /* Stop CFRunLoop in MIDIIn_MessageThread */ messagePort = CFMessagePortCreateRemote(kCFAllocatorDefault, MIDIInThreadPortName); CFMessagePortSendRequest(messagePort, 1, NULL, 0.0, 0.0, NULL, NULL); CFRelease(messagePort); DeleteCriticalSection(&midiInLock); } if (wineMIDIClient) MIDIClientDispose(wineMIDIClient); /* MIDIClientDispose will close all ports */ HeapFree(GetProcessHeap(), 0, sources); HeapFree(GetProcessHeap(), 0, destinations); return DRV_SUCCESS; }
CACFRemoteMessagePort::CACFRemoteMessagePort(CFStringRef inName, CFMessagePortInvalidationCallBack inInvalidationCallBack) : mMessagePort(NULL), mRunLoopSource(NULL), mDispatchQueue(NULL) { // create the CFMessagePort mMessagePort = CFMessagePortCreateRemote(NULL, inName); if(mMessagePort != NULL) { // failure to create a remote port does not need to throw an exception // because it isn't really an error since the port in question may not // exist and this fact requires a more complex response than an excpeption // provides for. // add the invalidation callback, if any if(inInvalidationCallBack != NULL) { CFMessagePortSetInvalidationCallBack(mMessagePort, inInvalidationCallBack); } } }
int main(int argc, char * argv[]) { signal(SIGINT, sigint_handler); bool israw = false; bool readstdin = false; char* code = NULL; bool usecolors = true; int ch; while ((ch = getopt(argc, argv, "nirc:t:sh")) != -1) { switch (ch) { case 'n': usecolors = false; break; case 'i': break; case 'r': israw = true; break; case 'c': code = optarg; break; case 't': recvTimeout = strtod(optarg, NULL); break; case 's': readstdin = true; break; case 'h': case '?': default: usage(argv[0]); } } if (optind != argc) usage(argv[0]); argc -= optind; argv += optind; CFMessagePortRef port = CFMessagePortCreateRemote(NULL, CFSTR("Hammerspoon")); if (!port) { fprintf(stderr, "error: can't access Hammerspoon; is it running with the ipc module loaded?\n"); return 1; } CFMutableStringRef str = CFStringCreateMutable(NULL, 0); if (readstdin) { target_setprefix(str, israw); char buffer[BUFSIZ]; while (fgets(buffer, BUFSIZ, stdin)) CFStringAppendCString(str, buffer, kCFStringEncodingUTF8); if (ferror(stdin)) { perror("error reading from stdin."); exit(3); } target_send(port, str); } else if (code) { target_setprefix(str, israw); CFStringAppendCString(str, code, kCFStringEncodingUTF8); target_send(port, str); } else { if (usecolors) setupcolors(); printf("%sHammerspoon interactive prompt.%s\n", COLOR_INITIAL, COLOR_RESET); while (1) { printf("\n%s", COLOR_INPUT); char* input = readline("> "); printf("%s", COLOR_RESET); if (!input) { printf("\n") ; exit(0); } add_history(input); if (!CFMessagePortIsValid(port)) { fprintf(stderr, "%sMessage port has become invalid. Attempting to re-establish.%s\n", COLOR_INITIAL, COLOR_RESET); port = CFMessagePortCreateRemote(NULL, CFSTR("Hammerspoon")); if (!port) { fprintf(stderr, "error: can't access Hammerspoon; is it running?\n"); exit(1); } } target_setprefix(str, israw); CFStringAppendCString(str, input, kCFStringEncodingUTF8); target_send(port, str); CFStringDelete(str, CFRangeMake(0, CFStringGetLength(str))); free(input); } } return 0; }