Instantly share code, notes, and snippets.

Embed
What would you like to do?
CFUserNotificationRef CFUserNotificationCreate(CFAllocatorRef allocator, CFTimeInterval timeout, CFOptionFlags flags, SInt32 *error, CFDictionaryRef dictionary) {
CHECK_FOR_FORK();
CFUserNotificationRef userNotification = NULL;
SInt32 retval = ERR_SUCCESS;
static uint16_t tokenCounter = 0;
SInt32 token = ((getpid() << 16) | (tokenCounter++));
CFStringRef sessionID = (dictionary ? CFDictionaryGetValue(dictionary, kCFUserNotificationSessionIDKey) : NULL);
mach_port_t replyPort = MACH_PORT_NULL;
if (!allocator) allocator = __CFGetDefaultAllocator();
retval = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &replyPort);
if (ERR_SUCCESS == retval && MACH_PORT_NULL != replyPort) retval = _CFUserNotificationSendRequest(allocator, sessionID, replyPort, token, timeout, flags, dictionary);
if (ERR_SUCCESS == retval) {
userNotification = (CFUserNotificationRef)_CFRuntimeCreateInstance(allocator, CFUserNotificationGetTypeID(), sizeof(struct __CFUserNotification) - sizeof(CFRuntimeBase), NULL);
if (userNotification) {
userNotification->_replyPort = replyPort;
userNotification->_token = token;
userNotification->_timeout = timeout;
userNotification->_requestFlags = flags;
userNotification->_responseFlags = 0;
userNotification->_sessionID = NULL;
userNotification->_responseDictionary = NULL;
userNotification->_machPort = NULL;
userNotification->_callout = NULL;
if (sessionID) userNotification->_sessionID = CFStringCreateCopy(allocator, sessionID);
} else {
retval = unix_err(ENOMEM);
}
} else {
if (dictionary) CFUserNotificationLog(CFDictionaryGetValue(dictionary, kCFUserNotificationAlertHeaderKey), CFDictionaryGetValue(dictionary, kCFUserNotificationAlertMessageKey));
}
if (ERR_SUCCESS != retval && MACH_PORT_NULL != replyPort) mach_port_destroy(mach_task_self(), replyPort);
if (error) *error = retval;
return userNotification;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment