2 * Copyright (c) 2007 Apple Computer, 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@
25 * Modification History
27 * October 24, 2007 Allan Nathanson <ajn@apple.com>
34 #include <CoreFoundation/CoreFoundation.h>
35 #include <SystemConfiguration/SystemConfiguration.h>
36 #include <SystemConfiguration/SCPrivate.h>
37 #include <ApplicationServices/ApplicationServices.h>
38 #include "UserEventAgentInterface.h"
40 #define MY_BUNDLE_ID CFSTR("com.apple.SystemConfiguration.SCMonitor")
41 #define MY_ICON_PATH "/System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns"
43 #define NETWORK_PREF_APP "/System/Library/PreferencePanes/Network.prefPane"
44 #define NETWORK_PREF_CMD "New Interface"
47 UserEventAgentInterfaceStruct
*_UserEventAgentInterface
;
51 CFRunLoopSourceRef monitorRls
;
53 CFMutableSetRef knownInterfaces
;
55 CFMutableArrayRef userInterfaces
;
56 CFUserNotificationRef userNotification
;
57 CFRunLoopSourceRef userRls
;
60 static CFMutableDictionaryRef notify_to_instance
= NULL
;
64 #pragma mark Watch for new [network] interfaces
68 open_NetworkPrefPane(void)
70 AEDesc aeDesc
= { typeNull
, NULL
};
73 LSLaunchURLSpec prefSpec
;
76 prefURL
= CFURLCreateWithFileSystemPath(kCFAllocatorDefault
,
77 CFSTR(NETWORK_PREF_APP
),
80 prefArray
= CFArrayCreate(NULL
, (const void **)&prefURL
, 1, &kCFTypeArrayCallBacks
);
83 status
= AECreateDesc('ptru',
84 (const void *)NETWORK_PREF_CMD
,
85 strlen(NETWORK_PREF_CMD
),
87 if (status
!= noErr
) {
88 SCLog(TRUE
, LOG_ERR
, CFSTR("SCMonitor: AECreateDesc() failed: %d"), status
);
91 prefSpec
.appURL
= NULL
;
92 prefSpec
.itemURLs
= prefArray
;
93 prefSpec
.passThruParams
= &aeDesc
;
94 prefSpec
.launchFlags
= kLSLaunchAsync
| kLSLaunchDontAddToRecents
;
95 prefSpec
.asyncRefCon
= NULL
;
97 status
= LSOpenFromURLSpec(&prefSpec
, NULL
);
98 if (status
!= noErr
) {
99 SCLog(TRUE
, LOG_ERR
, CFSTR("SCMonitor: LSOpenFromURLSpec() failed: %d"), status
);
102 CFRelease(prefArray
);
103 if (aeDesc
.descriptorType
!= typeNull
) AEDisposeDesc(&aeDesc
);
109 notify_remove(MyType
*myInstance
, Boolean cancel
)
111 if (myInstance
->userInterfaces
!= NULL
) {
112 CFRelease(myInstance
->userInterfaces
);
113 myInstance
->userInterfaces
= NULL
;
116 if (myInstance
->userRls
!= NULL
) {
117 CFRunLoopSourceInvalidate(myInstance
->userRls
);
118 CFRelease(myInstance
->userRls
);
119 myInstance
->userRls
= NULL
;
122 if (myInstance
->userNotification
!= NULL
) {
126 status
= CFUserNotificationCancel(myInstance
->userNotification
);
129 CFSTR("SCMonitor: CFUserNotificationCancel() failed, status=%d"),
133 CFRelease(myInstance
->userNotification
);
134 myInstance
->userNotification
= NULL
;
142 notify_reply(CFUserNotificationRef userNotification
, CFOptionFlags response_flags
)
144 MyType
*myInstance
= NULL
;
146 // get instance for notification
147 if (notify_to_instance
!= NULL
) {
148 myInstance
= (MyType
*)CFDictionaryGetValue(notify_to_instance
, userNotification
);
149 if (myInstance
!= NULL
) {
150 CFDictionaryRemoveValue(notify_to_instance
, userNotification
);
151 if (CFDictionaryGetCount(notify_to_instance
) == 0) {
152 CFRelease(notify_to_instance
);
153 notify_to_instance
= NULL
;
157 if (myInstance
== NULL
) {
158 SCLog(TRUE
, LOG_ERR
, CFSTR("SCMonitor: can't find user notification"));
163 switch (response_flags
& 0x3) {
164 case kCFUserNotificationDefaultResponse
:
165 // user asked to configure interface
166 open_NetworkPrefPane();
173 notify_remove(myInstance
, FALSE
);
178 notify_add(MyType
*myInstance
)
181 CFMutableDictionaryRef dict
= NULL
;
184 CFMutableArrayRef message
;
185 CFIndex n
= CFArrayGetCount(myInstance
->userInterfaces
);
188 if (myInstance
->userNotification
!= NULL
) {
189 CFMutableArrayRef save
= NULL
;
192 CFRetain(myInstance
->userInterfaces
);
193 save
= myInstance
->userInterfaces
;
195 notify_remove(myInstance
, TRUE
);
196 myInstance
->userInterfaces
= save
;
202 dict
= CFDictionaryCreateMutable(NULL
,
204 &kCFTypeDictionaryKeyCallBacks
,
205 &kCFTypeDictionaryValueCallBacks
);
207 // set localization URL
208 bundle
= CFBundleGetBundleWithIdentifier(MY_BUNDLE_ID
);
209 if (bundle
!= NULL
) {
210 url
= CFBundleCopyBundleURL(bundle
);
214 CFDictionarySetValue(dict
, kCFUserNotificationLocalizationURLKey
, url
);
217 SCLog(TRUE
, LOG_NOTICE
, CFSTR("SCMonitor: can't find bundle"));
222 url
= CFURLCreateFromFileSystemRepresentation(NULL
,
223 (const UInt8
*)MY_ICON_PATH
,
224 strlen(MY_ICON_PATH
),
227 CFDictionarySetValue(dict
, kCFUserNotificationIconURLKey
, url
);
232 CFDictionarySetValue(dict
,
233 kCFUserNotificationAlertHeaderKey
,
234 (n
== 1) ? CFSTR("HEADER_1") : CFSTR("HEADER_N"));
237 message
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
238 CFArrayAppendValue(message
,
239 (n
== 1) ? CFSTR("MESSAGE_S1") : CFSTR("MESSAGE_SN"));
240 for (i
= 0; i
< n
; i
++) {
241 SCNetworkInterfaceRef interface
;
244 interface
= CFArrayGetValueAtIndex(myInstance
->userInterfaces
, i
);
245 name
= SCNetworkInterfaceGetLocalizedDisplayName(interface
);
247 CFArrayAppendValue(message
, name
);
251 str
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("\r\t%@"), name
);
252 CFArrayAppendValue(message
, str
);
256 CFArrayAppendValue(message
,
257 (n
== 1) ? CFSTR("MESSAGE_E1") : CFSTR("MESSAGE_EN"));
258 CFDictionarySetValue(dict
, kCFUserNotificationAlertMessageKey
, message
);
262 CFDictionaryAddValue(dict
, kCFUserNotificationDefaultButtonTitleKey
, CFSTR("OPEN_NP"));
263 CFDictionaryAddValue(dict
, kCFUserNotificationAlternateButtonTitleKey
, CFSTR("CANCEL"));
265 // create and post notification
266 myInstance
->userNotification
= CFUserNotificationCreate(NULL
,
268 kCFUserNotificationNoteAlertLevel
,
271 if (myInstance
->userNotification
== NULL
) {
272 SCLog(TRUE
, LOG_ERR
, CFSTR("SCMonitor: CFUserNotificationCreate() failed, %d"), error
);
276 // establish callback
277 myInstance
->userRls
= CFUserNotificationCreateRunLoopSource(NULL
,
278 myInstance
->userNotification
,
281 if (myInstance
->userRls
== NULL
) {
282 SCLog(TRUE
, LOG_ERR
, CFSTR("SCMonitor: CFUserNotificationCreateRunLoopSource() failed"));
283 CFRelease(myInstance
->userNotification
);
284 myInstance
->userNotification
= NULL
;
287 CFRunLoopAddSource(CFRunLoopGetCurrent(), myInstance
->userRls
, kCFRunLoopDefaultMode
);
289 // add instance for notification
290 if (notify_to_instance
== NULL
) {
291 notify_to_instance
= CFDictionaryCreateMutable(NULL
,
293 &kCFTypeDictionaryKeyCallBacks
,
294 NULL
); // no retain/release/... for values
296 CFDictionarySetValue(notify_to_instance
, myInstance
->userNotification
, myInstance
);
300 if (dict
!= NULL
) CFRelease(dict
);
306 updateInterfaceList(SCDynamicStoreRef store
, CFArrayRef changes
, void * arg
)
309 CFArrayRef interfaces
;
310 MyType
*myInstance
= (MyType
*)arg
;
312 SCPreferencesRef prefs
;
313 CFMutableSetRef previouslyKnown
= NULL
;
314 SCNetworkSetRef set
= NULL
;
316 prefs
= SCPreferencesCreate(NULL
, CFSTR("SCMonitor"), NULL
);
321 set
= SCNetworkSetCopyCurrent(prefs
);
323 set
= SCNetworkSetCreate(prefs
);
329 previouslyKnown
= CFSetCreateMutableCopy(NULL
, 0, myInstance
->knownInterfaces
);
331 interfaces
= SCNetworkInterfaceCopyAll();
332 if (interfaces
!= NULL
) {
334 n
= CFArrayGetCount(interfaces
);
335 for (i
= 0; i
< n
; i
++) {
337 SCNetworkInterfaceRef interface
;
340 interface
= CFArrayGetValueAtIndex(interfaces
, i
);
341 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
342 if (bsdName
== NULL
) {
347 CFSetRemoveValue(previouslyKnown
, bsdName
);
349 if (CFSetContainsValue(myInstance
->knownInterfaces
, bsdName
)) {
350 // if known interface
354 CFSetAddValue(myInstance
->knownInterfaces
, bsdName
);
356 ok
= SCNetworkSetEstablishDefaultInterfaceConfiguration(set
, interface
);
358 // this is a *new* interface
359 if (myInstance
->userInterfaces
== NULL
) {
360 myInstance
->userInterfaces
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
362 CFArrayAppendValue(myInstance
->userInterfaces
, interface
);
366 CFRelease(interfaces
);
369 n
= CFSetGetCount(previouslyKnown
);
371 const void * names_q
[32];
372 const void ** names
= names_q
;
374 if (n
> (CFIndex
)(sizeof(names_q
) / sizeof(CFTypeRef
)))
375 names
= CFAllocatorAllocate(NULL
, n
* sizeof(CFTypeRef
), 0);
376 CFSetGetValues(previouslyKnown
, names
);
377 for (i
= 0; i
< n
; i
++) {
378 if (myInstance
->userInterfaces
!= NULL
) {
381 j
= CFArrayGetCount(myInstance
->userInterfaces
);
384 SCNetworkInterfaceRef interface
;
386 interface
= CFArrayGetValueAtIndex(myInstance
->userInterfaces
, j
);
387 bsdName
= SCNetworkInterfaceGetBSDName(interface
);
388 if (CFEqual(bsdName
, names
[i
])) {
389 // if we have previously posted a notification
390 // for this no-longer-present interface
391 CFArrayRemoveValueAtIndex(myInstance
->userInterfaces
, j
);
396 CFSetRemoveValue(myInstance
->knownInterfaces
, names
[i
]);
398 if (names
!= names_q
) CFAllocatorDeallocate(NULL
, names
);
404 if (myInstance
->userInterfaces
!= NULL
) {
405 notify_add(myInstance
);
408 if (set
!= NULL
) CFRelease(set
);
415 watcher_remove(MyType
*myInstance
)
417 if (myInstance
->monitorRls
!= NULL
) {
418 CFRunLoopSourceInvalidate(myInstance
->monitorRls
);
419 CFRelease(myInstance
->monitorRls
);
420 myInstance
->monitorRls
= NULL
;
423 if (myInstance
->knownInterfaces
!= NULL
) {
424 CFRelease(myInstance
->knownInterfaces
);
425 myInstance
->knownInterfaces
= NULL
;
433 watcher_add(MyType
*myInstance
)
435 SCDynamicStoreContext context
= { 0, (void *)myInstance
, NULL
, NULL
, NULL
};
436 CFDictionaryRef dict
;
439 SCDynamicStoreRef store
;
441 store
= SCDynamicStoreCreate(NULL
, CFSTR("SCMonitor"), updateInterfaceList
, &context
);
444 CFSTR("SCMonitor: SCDynamicStoreCreate() failed: %s"),
445 SCErrorString(SCError()));
449 key
= SCDynamicStoreKeyCreateNetworkInterface(NULL
, kSCDynamicStoreDomainState
);
451 // watch for changes to the list of network interfaces
452 keys
= CFArrayCreate(NULL
, (const void **)&key
, 1, &kCFTypeArrayCallBacks
);
453 SCDynamicStoreSetNotificationKeys(store
, NULL
, keys
);
455 myInstance
->monitorRls
= SCDynamicStoreCreateRunLoopSource(NULL
, store
, 0);
456 CFRunLoopAddSource(CFRunLoopGetCurrent(),
457 myInstance
->monitorRls
,
458 kCFRunLoopDefaultMode
);
460 // initialize the list of known interfaces
461 myInstance
->knownInterfaces
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
462 dict
= SCDynamicStoreCopyValue(store
, key
);
464 if (isA_CFDictionary(dict
)) {
466 CFArrayRef interfaces
;
469 interfaces
= CFDictionaryGetValue(dict
, kSCPropNetInterfaces
);
470 n
= isA_CFArray(interfaces
) ? CFArrayGetCount(interfaces
) : 0;
471 for (i
= 0; i
< n
; i
++) {
474 bsdName
= CFArrayGetValueAtIndex(interfaces
, i
);
475 if (isA_CFString(bsdName
)) {
476 CFSetAddValue(myInstance
->knownInterfaces
, bsdName
);
491 #pragma mark UserEventAgent stubs
495 myQueryInterface(void *myInstance
, REFIID iid
, LPVOID
*ppv
)
497 CFUUIDRef interfaceID
= CFUUIDCreateFromUUIDBytes(NULL
, iid
);
499 // Test the requested ID against the valid interfaces.
500 if (CFEqual(interfaceID
, kUserEventAgentInterfaceID
)) {
501 ((MyType
*) myInstance
)->_UserEventAgentInterface
->AddRef(myInstance
);
503 CFRelease(interfaceID
);
507 if (CFEqual(interfaceID
, IUnknownUUID
)) {
508 ((MyType
*) myInstance
)->_UserEventAgentInterface
->AddRef(myInstance
);
510 CFRelease(interfaceID
);
514 // Requested interface unknown, bail with error.
516 CFRelease(interfaceID
);
517 return E_NOINTERFACE
;
522 myAddRef(void *myInstance
)
524 ((MyType
*) myInstance
)->_refCount
++;
525 return ((MyType
*) myInstance
)->_refCount
;
530 myRelease(void *myInstance
)
532 ((MyType
*) myInstance
)->_refCount
--;
533 if (((MyType
*) myInstance
)->_refCount
== 0) {
534 CFUUIDRef factoryID
= ((MyType
*) myInstance
)->_factoryID
;
536 if (factoryID
!= NULL
) {
537 CFPlugInRemoveInstanceForFactory(factoryID
);
538 CFRelease(factoryID
);
540 watcher_remove((MyType
*)myInstance
);
541 notify_remove((MyType
*)myInstance
, TRUE
);
547 return ((MyType
*) myInstance
)->_refCount
;
552 myInstall(void *myInstance
)
554 watcher_add((MyType
*)myInstance
);
559 static UserEventAgentInterfaceStruct UserEventAgentInterfaceFtbl
= {
560 NULL
, // Required padding for COM
561 myQueryInterface
, // These three are the required COM functions
564 myInstall
// Interface implementation
569 UserEventAgentFactory(CFAllocatorRef allocator
, CFUUIDRef typeID
)
571 MyType
*newOne
= NULL
;
573 if (CFEqual(typeID
, kUserEventAgentTypeID
)) {
574 newOne
= (MyType
*)malloc(sizeof(MyType
));
575 bzero(newOne
, sizeof(*newOne
));
576 newOne
->_UserEventAgentInterface
= &UserEventAgentInterfaceFtbl
;
577 newOne
->_factoryID
= (CFUUIDRef
)CFRetain(kUserEventAgentFactoryID
);
578 CFPlugInAddInstanceForFactory(kUserEventAgentFactoryID
);
579 newOne
->_refCount
= 1;
588 main(int argc
, char **argv
)
590 MyType
*newOne
= (MyType
*)malloc(sizeof(MyType
));
593 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
595 bzero(newOne
, sizeof(*newOne
));