2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* CFUserNotification.c
24 Copyright (c) 2000-2007 Apple Inc. All rights reserved.
25 Responsibility: Doug Davidson
28 #include <CoreFoundation/CFUserNotification.h>
29 #include <CoreFoundation/CFPropertyList.h>
30 #include <CoreFoundation/CFNumber.h>
31 #include <CoreFoundation/CFRunLoop.h>
32 #include "CFInternal.h"
33 #include <CoreFoundation/CFMachPort.h>
37 #include <mach/mach.h>
38 #include <mach/error.h>
39 #include <bootstrap_priv.h>
45 #define CFUserNotificationLog(alertHeader, alertMessage) CFLog(3, CFSTR("%@: %@"), alertHeader, alertMessage);
48 kCFUserNotificationCancelFlag
= (1 << 3),
49 kCFUserNotificationUpdateFlag
= (1 << 4)
52 CONST_STRING_DECL(kCFUserNotificationTokenKey
, "Token")
53 CONST_STRING_DECL(kCFUserNotificationTimeoutKey
, "Timeout")
54 CONST_STRING_DECL(kCFUserNotificationFlagsKey
, "Flags")
55 CONST_STRING_DECL(kCFUserNotificationIconPathKey
, "IconPath")
56 CONST_STRING_DECL(kCFUserNotificationSoundPathKey
, "SoundPath")
57 CONST_STRING_DECL(kCFUserNotificationLocalizationPathKey
, "LocalizationPath")
58 CONST_STRING_DECL(kCFUserNotificationAlertSourceKey
, "AlertSource")
59 CONST_STRING_DECL(kCFUserNotificationTextFieldLabelsKey
, "TextFieldTitles")
60 CONST_STRING_DECL(kCFUserNotificationCheckBoxLabelsKey
, "CheckBoxTitles")
61 CONST_STRING_DECL(kCFUserNotificationIconURLKey
, "IconURL")
62 CONST_STRING_DECL(kCFUserNotificationSoundURLKey
, "SoundURL")
63 CONST_STRING_DECL(kCFUserNotificationLocalizationURLKey
, "LocalizationURL")
64 CONST_STRING_DECL(kCFUserNotificationAlertHeaderKey
, "AlertHeader")
65 CONST_STRING_DECL(kCFUserNotificationAlertMessageKey
, "AlertMessage")
66 CONST_STRING_DECL(kCFUserNotificationDefaultButtonTitleKey
, "DefaultButtonTitle")
67 CONST_STRING_DECL(kCFUserNotificationAlternateButtonTitleKey
, "AlternateButtonTitle")
68 CONST_STRING_DECL(kCFUserNotificationOtherButtonTitleKey
, "OtherButtonTitle")
69 CONST_STRING_DECL(kCFUserNotificationProgressIndicatorValueKey
, "ProgressIndicatorValue")
70 CONST_STRING_DECL(kCFUserNotificationSessionIDKey
, "SessionID")
71 CONST_STRING_DECL(kCFUserNotificationPopUpTitlesKey
, "PopUpTitles")
72 CONST_STRING_DECL(kCFUserNotificationTextFieldTitlesKey
, "TextFieldTitles")
73 CONST_STRING_DECL(kCFUserNotificationCheckBoxTitlesKey
, "CheckBoxTitles")
74 CONST_STRING_DECL(kCFUserNotificationTextFieldValuesKey
, "TextFieldValues")
75 CONST_STRING_DECL(kCFUserNotificationPopUpSelectionKey
, "PopUpSelection")
76 CONST_STRING_DECL(kCFUserNotificationKeyboardTypesKey
, "KeyboardTypes")
78 static CFTypeID __kCFUserNotificationTypeID
= _kCFRuntimeNotATypeID
;
80 struct __CFUserNotification
{
84 CFTimeInterval _timeout
;
85 CFOptionFlags _requestFlags
;
86 CFOptionFlags _responseFlags
;
87 CFStringRef _sessionID
;
88 CFDictionaryRef _responseDictionary
;
89 CFMachPortRef _machPort
;
90 CFUserNotificationCallBack _callout
;
93 static CFStringRef
__CFUserNotificationCopyDescription(CFTypeRef cf
) {
94 CFMutableStringRef result
;
95 result
= CFStringCreateMutable(CFGetAllocator(cf
), 0);
96 CFStringAppendFormat(result
, NULL
, CFSTR("<CFUserNotification %p>"), cf
);
100 #define MAX_STRING_LENGTH PATH_MAX
101 #define MAX_STRING_COUNT 16
102 #define MAX_PORT_NAME_LENGTH 63
103 #define NOTIFICATION_PORT_NAME_SUFFIX ".session."
104 #define MESSAGE_TIMEOUT 100
105 #if DEPLOYMENT_TARGET_MACOSX
106 #define NOTIFICATION_PORT_NAME "com.apple.UNCUserNotification"
108 #define NOTIFICATION_PORT_NAME "com.apple.SBUserNotification"
110 #error Unknown or unspecified DEPLOYMENT_TARGET
114 static void __CFUserNotificationDeallocate(CFTypeRef cf
);
116 static const CFRuntimeClass __CFUserNotificationClass
= {
118 "CFUserNotification",
121 __CFUserNotificationDeallocate
,
125 __CFUserNotificationCopyDescription
128 __private_extern__
void __CFUserNotificationInitialize(void) {
129 __kCFUserNotificationTypeID
= _CFRuntimeRegisterClass(&__CFUserNotificationClass
);
132 CFTypeID
CFUserNotificationGetTypeID(void) {
133 if (_kCFRuntimeNotATypeID
== __kCFUserNotificationTypeID
) __CFUserNotificationInitialize();
134 return __kCFUserNotificationTypeID
;
137 static void __CFUserNotificationDeallocate(CFTypeRef cf
) {
138 CFUserNotificationRef userNotification
= (CFUserNotificationRef
)cf
;
139 if (userNotification
->_machPort
) {
140 CFMachPortInvalidate(userNotification
->_machPort
);
141 CFRelease(userNotification
->_machPort
);
142 } else if (MACH_PORT_NULL
!= userNotification
->_replyPort
) {
143 mach_port_destroy(mach_task_self(), userNotification
->_replyPort
);
145 if (userNotification
->_sessionID
) CFRelease(userNotification
->_sessionID
);
146 if (userNotification
->_responseDictionary
) CFRelease(userNotification
->_responseDictionary
);
149 static void _CFUserNotificationAddToDictionary(const void *key
, const void *value
, void *context
) {
150 if (CFGetTypeID(key
) == CFStringGetTypeID()) CFDictionarySetValue((CFMutableDictionaryRef
)context
, key
, value
);
153 static CFDictionaryRef
_CFUserNotificationModifiedDictionary(CFAllocatorRef allocator
, CFDictionaryRef dictionary
, SInt32 token
, SInt32 timeout
, CFStringRef source
) {
154 CFMutableDictionaryRef md
= CFDictionaryCreateMutable(allocator
, 0, &kCFCopyStringDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
155 CFNumberRef tokenNumber
= CFNumberCreate(allocator
, kCFNumberSInt32Type
, &token
);
156 CFNumberRef timeoutNumber
= CFNumberCreate(allocator
, kCFNumberSInt32Type
, &timeout
);
158 CFStringRef path
= NULL
;
160 if (dictionary
) CFDictionaryApplyFunction(dictionary
, _CFUserNotificationAddToDictionary
, md
);
161 if (source
) CFDictionaryAddValue(md
, kCFUserNotificationAlertSourceKey
, source
);
163 CFDictionaryAddValue(md
, kCFUserNotificationTokenKey
, tokenNumber
);
164 CFRelease(tokenNumber
);
167 CFDictionaryAddValue(md
, kCFUserNotificationTimeoutKey
, timeoutNumber
);
168 CFRelease(timeoutNumber
);
171 url
= CFDictionaryGetValue(md
, kCFUserNotificationIconURLKey
);
172 if (url
&& CFGetTypeID((CFTypeRef
)url
) == CFURLGetTypeID()) {
173 url
= CFURLCopyAbsoluteURL(url
);
174 CFDictionaryRemoveValue(md
, kCFUserNotificationIconURLKey
);
175 path
= CFURLCopyFileSystemPath(url
, kCFURLPOSIXPathStyle
);
176 CFDictionaryAddValue(md
, kCFUserNotificationIconPathKey
, path
);
180 url
= CFDictionaryGetValue(md
, kCFUserNotificationSoundURLKey
);
181 if (url
&& CFGetTypeID((CFTypeRef
)url
) == CFURLGetTypeID()) {
182 url
= CFURLCopyAbsoluteURL(url
);
183 CFDictionaryRemoveValue(md
, kCFUserNotificationSoundURLKey
);
184 path
= CFURLCopyFileSystemPath(url
, kCFURLPOSIXPathStyle
);
185 CFDictionaryAddValue(md
, kCFUserNotificationSoundPathKey
, path
);
189 url
= CFDictionaryGetValue(md
, kCFUserNotificationLocalizationURLKey
);
190 if (url
&& CFGetTypeID((CFTypeRef
)url
) == CFURLGetTypeID()) {
191 url
= CFURLCopyAbsoluteURL(url
);
192 CFDictionaryRemoveValue(md
, kCFUserNotificationLocalizationURLKey
);
193 path
= CFURLCopyFileSystemPath(url
, kCFURLPOSIXPathStyle
);
194 CFDictionaryAddValue(md
, kCFUserNotificationLocalizationPathKey
, path
);
201 static SInt32
_CFUserNotificationSendRequest(CFAllocatorRef allocator
, CFStringRef sessionID
, mach_port_t replyPort
, SInt32 token
, CFTimeInterval timeout
, CFOptionFlags flags
, CFDictionaryRef dictionary
) {
202 CFDictionaryRef modifiedDictionary
= NULL
;
203 SInt32 retval
= ERR_SUCCESS
, itimeout
= (timeout
> 0.0 && timeout
< INT_MAX
) ? (SInt32
)timeout
: 0;
205 mach_msg_base_t
*msg
= NULL
;
206 mach_port_t bootstrapPort
= MACH_PORT_NULL
, serverPort
= MACH_PORT_NULL
;
208 char namebuffer
[MAX_PORT_NAME_LENGTH
+ 1];
209 strlcpy(namebuffer
, NOTIFICATION_PORT_NAME
, sizeof(namebuffer
));
211 char sessionid
[MAX_PORT_NAME_LENGTH
+ 1];
212 CFIndex len
= MAX_PORT_NAME_LENGTH
- sizeof(NOTIFICATION_PORT_NAME
) - sizeof(NOTIFICATION_PORT_NAME_SUFFIX
);
213 CFStringGetBytes(sessionID
, CFRangeMake(0, CFStringGetLength(sessionID
)), kCFStringEncodingUTF8
, 0, false, (uint8_t *)sessionid
, len
, &size
);
214 sessionid
[len
- 1] = '\0';
215 strlcat(namebuffer
, NOTIFICATION_PORT_NAME_SUFFIX
, sizeof(namebuffer
));
216 strlcat(namebuffer
, sessionid
, sizeof(namebuffer
));
219 retval
= task_get_bootstrap_port(mach_task_self(), &bootstrapPort
);
220 if (ERR_SUCCESS
== retval
&& MACH_PORT_NULL
!= bootstrapPort
) retval
= bootstrap_look_up2(bootstrapPort
, namebuffer
, &serverPort
, 0, 0);
221 if (ERR_SUCCESS
== retval
&& MACH_PORT_NULL
!= serverPort
) {
222 modifiedDictionary
= _CFUserNotificationModifiedDictionary(allocator
, dictionary
, token
, itimeout
, _CFProcessNameString());
223 if (modifiedDictionary
) {
224 data
= CFPropertyListCreateXMLData(allocator
, modifiedDictionary
);
226 size
= sizeof(mach_msg_base_t
) + ((CFDataGetLength(data
) + 3) & (~0x3));
227 msg
= (mach_msg_base_t
*)CFAllocatorAllocate(allocator
, size
, 0);
228 if (__CFOASafe
) __CFSetLastAllocationEventName(msg
, "CFUserNotification (temp)");
230 memset(msg
, 0, size
);
231 msg
->header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, MACH_MSG_TYPE_MAKE_SEND_ONCE
);
232 msg
->header
.msgh_size
= size
;
233 msg
->header
.msgh_remote_port
= serverPort
;
234 msg
->header
.msgh_local_port
= replyPort
;
235 msg
->header
.msgh_id
= flags
;
236 msg
->body
.msgh_descriptor_count
= 0;
237 CFDataGetBytes(data
, CFRangeMake(0, CFDataGetLength(data
)), (uint8_t *)msg
+ sizeof(mach_msg_base_t
));
238 //CFShow(CFStringCreateWithBytes(kCFAllocatorSystemDefault, (UInt8 *)msg + sizeof(mach_msg_base_t), CFDataGetLength(data), kCFStringEncodingUTF8, false));
239 retval
= mach_msg((mach_msg_header_t
*)msg
, MACH_SEND_MSG
|MACH_SEND_TIMEOUT
, size
, 0, MACH_PORT_NULL
, MESSAGE_TIMEOUT
, MACH_PORT_NULL
);
240 CFAllocatorDeallocate(allocator
, msg
);
242 retval
= unix_err(ENOMEM
);
246 retval
= unix_err(ENOMEM
);
248 CFRelease(modifiedDictionary
);
250 retval
= unix_err(ENOMEM
);
256 CFUserNotificationRef
CFUserNotificationCreate(CFAllocatorRef allocator
, CFTimeInterval timeout
, CFOptionFlags flags
, SInt32
*error
, CFDictionaryRef dictionary
) {
258 CFUserNotificationRef userNotification
= NULL
;
259 SInt32 retval
= ERR_SUCCESS
;
260 static uint16_t tokenCounter
= 0;
261 SInt32 token
= ((getpid()<<16) | (tokenCounter
++));
262 CFStringRef sessionID
= (dictionary
? CFDictionaryGetValue(dictionary
, kCFUserNotificationSessionIDKey
) : NULL
);
263 mach_port_t replyPort
= MACH_PORT_NULL
;
265 if (!allocator
) allocator
= __CFGetDefaultAllocator();
267 retval
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &replyPort
);
268 if (ERR_SUCCESS
== retval
&& MACH_PORT_NULL
!= replyPort
) retval
= _CFUserNotificationSendRequest(allocator
, sessionID
, replyPort
, token
, timeout
, flags
, dictionary
);
269 if (ERR_SUCCESS
== retval
) {
270 userNotification
= (CFUserNotificationRef
)_CFRuntimeCreateInstance(allocator
, CFUserNotificationGetTypeID(), sizeof(struct __CFUserNotification
) - sizeof(CFRuntimeBase
), NULL
);
271 if (userNotification
) {
272 userNotification
->_replyPort
= replyPort
;
273 userNotification
->_token
= token
;
274 userNotification
->_timeout
= timeout
;
275 userNotification
->_requestFlags
= flags
;
276 userNotification
->_responseFlags
= 0;
277 userNotification
->_sessionID
= NULL
;
278 userNotification
->_responseDictionary
= NULL
;
279 userNotification
->_machPort
= NULL
;
280 userNotification
->_callout
= NULL
;
281 if (sessionID
) userNotification
->_sessionID
= CFStringCreateCopy(allocator
, sessionID
);
283 retval
= unix_err(ENOMEM
);
286 if (dictionary
) CFUserNotificationLog(CFDictionaryGetValue(dictionary
, kCFUserNotificationAlertHeaderKey
), CFDictionaryGetValue(dictionary
, kCFUserNotificationAlertMessageKey
));
288 if (ERR_SUCCESS
!= retval
&& MACH_PORT_NULL
!= replyPort
) mach_port_destroy(mach_task_self(), replyPort
);
289 if (error
) *error
= retval
;
290 return userNotification
;
293 static void _CFUserNotificationMachPortCallBack(CFMachPortRef port
, void *m
, CFIndex size
, void *info
) {
294 CFUserNotificationRef userNotification
= (CFUserNotificationRef
)info
;
295 mach_msg_base_t
*msg
= (mach_msg_base_t
*)m
;
296 CFOptionFlags responseFlags
= msg
->header
.msgh_id
;
297 if (msg
->header
.msgh_size
> sizeof(mach_msg_base_t
)) {
298 CFDataRef responseData
= CFDataCreate(kCFAllocatorSystemDefault
, (uint8_t *)msg
+ sizeof(mach_msg_base_t
), msg
->header
.msgh_size
- sizeof(mach_msg_base_t
));
300 userNotification
->_responseDictionary
= CFPropertyListCreateFromXMLData(kCFAllocatorSystemDefault
, responseData
, kCFPropertyListImmutable
, NULL
);
301 CFRelease(responseData
);
304 CFMachPortInvalidate(userNotification
->_machPort
);
305 CFRelease(userNotification
->_machPort
);
306 userNotification
->_machPort
= NULL
;
307 mach_port_destroy(mach_task_self(), userNotification
->_replyPort
);
308 userNotification
->_replyPort
= MACH_PORT_NULL
;
309 userNotification
->_callout(userNotification
, responseFlags
);
312 SInt32
CFUserNotificationReceiveResponse(CFUserNotificationRef userNotification
, CFTimeInterval timeout
, CFOptionFlags
*responseFlags
) {
314 SInt32 retval
= ERR_SUCCESS
;
315 mach_msg_timeout_t msgtime
= (timeout
> 0.0 && 1000.0 * timeout
< INT_MAX
) ? (mach_msg_timeout_t
)(1000.0 * timeout
) : 0;
316 mach_msg_base_t
*msg
= NULL
;
317 CFIndex size
= MAX_STRING_COUNT
* MAX_STRING_LENGTH
;
318 CFDataRef responseData
;
320 if (userNotification
&& MACH_PORT_NULL
!= userNotification
->_replyPort
) {
321 msg
= (mach_msg_base_t
*)CFAllocatorAllocate(CFGetAllocator(userNotification
), size
, 0);
322 if (__CFOASafe
) __CFSetLastAllocationEventName(msg
, "CFUserNotification (temp)");
324 memset(msg
, 0, size
);
325 msg
->header
.msgh_size
= size
;
327 retval
= mach_msg((mach_msg_header_t
*)msg
, MACH_RCV_MSG
|MACH_RCV_TIMEOUT
, 0, size
, userNotification
->_replyPort
, msgtime
, MACH_PORT_NULL
);
329 retval
= mach_msg((mach_msg_header_t
*)msg
, MACH_RCV_MSG
, 0, size
, userNotification
->_replyPort
, 0, MACH_PORT_NULL
);
331 if (ERR_SUCCESS
== retval
) {
332 if (responseFlags
) *responseFlags
= msg
->header
.msgh_id
;
333 if (msg
->header
.msgh_size
> sizeof(mach_msg_base_t
)) {
334 responseData
= CFDataCreate(kCFAllocatorSystemDefault
, (uint8_t *)msg
+ sizeof(mach_msg_base_t
), msg
->header
.msgh_size
- sizeof(mach_msg_base_t
));
336 userNotification
->_responseDictionary
= CFPropertyListCreateFromXMLData(kCFAllocatorSystemDefault
, responseData
, kCFPropertyListImmutable
, NULL
);
337 CFRelease(responseData
);
340 if (userNotification
->_machPort
) {
341 CFMachPortInvalidate(userNotification
->_machPort
);
342 CFRelease(userNotification
->_machPort
);
343 userNotification
->_machPort
= NULL
;
345 mach_port_destroy(mach_task_self(), userNotification
->_replyPort
);
346 userNotification
->_replyPort
= MACH_PORT_NULL
;
348 CFAllocatorDeallocate(CFGetAllocator(userNotification
), msg
);
350 retval
= unix_err(ENOMEM
);
356 CFStringRef
CFUserNotificationGetResponseValue(CFUserNotificationRef userNotification
, CFStringRef key
, CFIndex idx
) {
358 CFStringRef retval
= NULL
;
359 CFTypeRef value
= NULL
;
360 if (userNotification
&& userNotification
->_responseDictionary
) {
361 value
= CFDictionaryGetValue(userNotification
->_responseDictionary
, key
);
362 if (CFGetTypeID(value
) == CFStringGetTypeID()) {
364 retval
= (CFStringRef
)value
;
366 } else if (CFGetTypeID(value
) == CFArrayGetTypeID()) {
367 if (0 <= idx
&& idx
< CFArrayGetCount((CFArrayRef
)value
)) {
368 retval
= (CFStringRef
)CFArrayGetValueAtIndex((CFArrayRef
)value
, idx
);
375 CFDictionaryRef
CFUserNotificationGetResponseDictionary(CFUserNotificationRef userNotification
) {
377 return userNotification
? userNotification
->_responseDictionary
: NULL
;
380 SInt32
CFUserNotificationUpdate(CFUserNotificationRef userNotification
, CFTimeInterval timeout
, CFOptionFlags flags
, CFDictionaryRef dictionary
) {
382 SInt32 retval
= ERR_SUCCESS
;
383 if (userNotification
&& MACH_PORT_NULL
!= userNotification
->_replyPort
) {
384 retval
= _CFUserNotificationSendRequest(CFGetAllocator(userNotification
), userNotification
->_sessionID
, userNotification
->_replyPort
, userNotification
->_token
, timeout
, flags
|kCFUserNotificationUpdateFlag
, dictionary
);
389 SInt32
CFUserNotificationCancel(CFUserNotificationRef userNotification
) {
391 SInt32 retval
= ERR_SUCCESS
;
392 if (userNotification
&& MACH_PORT_NULL
!= userNotification
->_replyPort
) {
393 retval
= _CFUserNotificationSendRequest(CFGetAllocator(userNotification
), userNotification
->_sessionID
, userNotification
->_replyPort
, userNotification
->_token
, 0, kCFUserNotificationCancelFlag
, NULL
);
398 CFRunLoopSourceRef
CFUserNotificationCreateRunLoopSource(CFAllocatorRef allocator
, CFUserNotificationRef userNotification
, CFUserNotificationCallBack callout
, CFIndex order
) {
400 CFRunLoopSourceRef source
= NULL
;
401 if (userNotification
&& callout
&& !userNotification
->_machPort
&& MACH_PORT_NULL
!= userNotification
->_replyPort
) {
402 CFMachPortContext context
= {0, userNotification
, NULL
, NULL
, NULL
};
403 userNotification
->_machPort
= CFMachPortCreateWithPort(CFGetAllocator(userNotification
), (mach_port_t
)userNotification
->_replyPort
, _CFUserNotificationMachPortCallBack
, &context
, false);
405 if (userNotification
&& userNotification
->_machPort
) {
406 source
= CFMachPortCreateRunLoopSource(allocator
, userNotification
->_machPort
, order
);
407 userNotification
->_callout
= callout
;
412 SInt32
CFUserNotificationDisplayNotice(CFTimeInterval timeout
, CFOptionFlags flags
, CFURLRef iconURL
, CFURLRef soundURL
, CFURLRef localizationURL
, CFStringRef alertHeader
, CFStringRef alertMessage
, CFStringRef defaultButtonTitle
) {
414 CFUserNotificationRef userNotification
;
415 SInt32 retval
= ERR_SUCCESS
;
416 CFMutableDictionaryRef dict
= CFDictionaryCreateMutable(kCFAllocatorSystemDefault
, 0, &kCFCopyStringDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
417 if (iconURL
) CFDictionaryAddValue(dict
, kCFUserNotificationIconURLKey
, iconURL
);
418 if (soundURL
) CFDictionaryAddValue(dict
, kCFUserNotificationSoundURLKey
, soundURL
);
419 if (localizationURL
) CFDictionaryAddValue(dict
, kCFUserNotificationLocalizationURLKey
, localizationURL
);
420 if (alertHeader
) CFDictionaryAddValue(dict
, kCFUserNotificationAlertHeaderKey
, alertHeader
);
421 if (alertMessage
) CFDictionaryAddValue(dict
, kCFUserNotificationAlertMessageKey
, alertMessage
);
422 if (defaultButtonTitle
) CFDictionaryAddValue(dict
, kCFUserNotificationDefaultButtonTitleKey
, defaultButtonTitle
);
423 userNotification
= CFUserNotificationCreate(kCFAllocatorSystemDefault
, timeout
, flags
, &retval
, dict
);
424 if (userNotification
) CFRelease(userNotification
);
430 CF_EXPORT SInt32
CFUserNotificationDisplayAlert(CFTimeInterval timeout
, CFOptionFlags flags
, CFURLRef iconURL
, CFURLRef soundURL
, CFURLRef localizationURL
, CFStringRef alertHeader
, CFStringRef alertMessage
, CFStringRef defaultButtonTitle
, CFStringRef alternateButtonTitle
, CFStringRef otherButtonTitle
, CFOptionFlags
*responseFlags
) {
432 CFUserNotificationRef userNotification
;
433 SInt32 retval
= ERR_SUCCESS
;
434 CFMutableDictionaryRef dict
= CFDictionaryCreateMutable(kCFAllocatorSystemDefault
, 0, &kCFCopyStringDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
435 if (iconURL
) CFDictionaryAddValue(dict
, kCFUserNotificationIconURLKey
, iconURL
);
436 if (soundURL
) CFDictionaryAddValue(dict
, kCFUserNotificationSoundURLKey
, soundURL
);
437 if (localizationURL
) CFDictionaryAddValue(dict
, kCFUserNotificationLocalizationURLKey
, localizationURL
);
438 if (alertHeader
) CFDictionaryAddValue(dict
, kCFUserNotificationAlertHeaderKey
, alertHeader
);
439 if (alertMessage
) CFDictionaryAddValue(dict
, kCFUserNotificationAlertMessageKey
, alertMessage
);
440 if (defaultButtonTitle
) CFDictionaryAddValue(dict
, kCFUserNotificationDefaultButtonTitleKey
, defaultButtonTitle
);
441 if (alternateButtonTitle
) CFDictionaryAddValue(dict
, kCFUserNotificationAlternateButtonTitleKey
, alternateButtonTitle
);
442 if (otherButtonTitle
) CFDictionaryAddValue(dict
, kCFUserNotificationOtherButtonTitleKey
, otherButtonTitle
);
443 userNotification
= CFUserNotificationCreate(kCFAllocatorSystemDefault
, timeout
, flags
, &retval
, dict
);
444 if (userNotification
) {
445 retval
= CFUserNotificationReceiveResponse(userNotification
, timeout
, responseFlags
);
446 if (MACH_RCV_TIMED_OUT
== retval
) {
447 retval
= CFUserNotificationCancel(userNotification
);
448 if (responseFlags
) *responseFlags
= kCFUserNotificationCancelResponse
;
450 CFRelease(userNotification
);
456 #undef MAX_STRING_LENGTH
457 #undef MAX_STRING_COUNT
458 #undef NOTIFICATION_PORT_NAME
459 #undef MESSAGE_TIMEOUT
460 #undef MAX_PORT_NAME_LENGTH
461 #undef NOTIFICATION_PORT_NAME_SUFFIX