2 * Copyright (c) 2017 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@
25 * InterfaceNamerControlPrefs.c
26 * - definitions for accessing InterfaceNamer control preferences
30 * Modification History
32 * January 12, 2017 Allan Nathanson (ajn@apple.com)
36 #include <TargetConditionals.h>
37 #include <SystemConfiguration/SystemConfiguration.h>
38 #include <SystemConfiguration/SCPrivate.h>
39 #include <SystemConfiguration/scprefs_observer.h>
40 #include "InterfaceNamerControlPrefs.h"
43 * kInterfaceNamerControlPrefsID
44 * - identifies the InterfaceNamer preferences file that contains 'AllowNewInterfaces'
46 #define kInterfaceNamerControlPrefsIDStr "com.apple.InterfaceNamer.control.plist"
47 #define kInterfaceNamerControlPrefsID CFSTR(kInterfaceNamerControlPrefsIDStr)
51 * - indicates whether InterfaceNamer is allowed to create new interfaces
52 * while the screen is locked or not
54 #define kAllowNewInterfaces CFSTR("AllowNewInterfaces")
56 static SCPreferencesRef S_prefs
;
57 static InterfaceNamerControlPrefsCallBack S_callback
;
59 static SCPreferencesRef
60 InterfaceNamerControlPrefsGet(void)
62 if (S_prefs
== NULL
) {
63 InterfaceNamerControlPrefsInit(NULL
, NULL
);
70 prefs_changed(void * arg
)
73 os_activity_t activity
;
75 activity
= os_activity_create("processing InterfaceNamer preference change",
77 OS_ACTIVITY_FLAG_DEFAULT
);
78 os_activity_scope(activity
);
80 /* get the current value */
81 if (S_callback
!= NULL
) {
82 (*S_callback
)(S_prefs
);
92 * kInterfaceNamerControlManangedPrefsID
93 * - identifies the location of the managed preferences file
95 #define kManagedPrefsDirStr "/Library/Managed Preferences/mobile/"
96 #define kInterfaceNamerControlManagedPrefsID CFSTR(kManagedPrefsDirStr \
97 kInterfaceNamerControlPrefsIDStr)
98 static SCPreferencesRef S_managed_prefs
;
100 static SCPreferencesRef
101 InterfaceNamerControlManagedPrefsGet(void)
103 if (S_managed_prefs
== NULL
) {
104 CFMutableDictionaryRef options
;
106 options
= CFDictionaryCreateMutable(NULL
,
108 &kCFTypeDictionaryKeyCallBacks
,
109 &kCFTypeDictionaryValueCallBacks
);
110 CFDictionarySetValue(options
, kSCPreferencesOptionRemoveWhenEmpty
, kCFBooleanTrue
);
111 S_managed_prefs
= SCPreferencesCreateWithOptions(NULL
,
112 CFSTR("InterfaceNamerControlPrefs"),
113 kInterfaceNamerControlManagedPrefsID
,
118 return (S_managed_prefs
);
122 enable_prefs_observer(CFRunLoopRef runloop
)
124 CFRunLoopSourceContext context
;
125 dispatch_queue_t queue
;
126 CFRunLoopSourceRef source
;
128 bzero(&context
, sizeof(context
));
129 context
.perform
= prefs_changed
;
130 source
= CFRunLoopSourceCreate(kCFAllocatorDefault
, 0, &context
);
131 CFRunLoopAddSource(runloop
, source
, kCFRunLoopCommonModes
);
132 queue
= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0);
133 _scprefs_observer_watch(scprefs_observer_type_global
,
134 kInterfaceNamerControlPrefsIDStr
,
137 if (source
!= NULL
) {
138 CFRunLoopSourceSignal(source
);
139 if (runloop
!= NULL
) {
140 CFRunLoopWakeUp(runloop
);
147 #else /* TARGET_OS_IPHONE */
150 enable_prefs_observer(CFRunLoopRef runloop
)
152 #pragma unused(runloop)
156 #endif /* TARGET_OS_IPHONE */
159 InterfaceNamerControlPrefsChanged(SCPreferencesRef prefs
,
160 SCPreferencesNotification type
,
163 #pragma unused(prefs)
170 __private_extern__ SCPreferencesRef
171 InterfaceNamerControlPrefsInit(CFRunLoopRef runloop
,
172 InterfaceNamerControlPrefsCallBack callback
)
174 CFMutableDictionaryRef options
;
176 options
= CFDictionaryCreateMutable(NULL
,
178 &kCFTypeDictionaryKeyCallBacks
,
179 &kCFTypeDictionaryValueCallBacks
);
180 CFDictionarySetValue(options
, kSCPreferencesOptionRemoveWhenEmpty
, kCFBooleanTrue
);
181 S_prefs
= SCPreferencesCreateWithOptions(NULL
,
182 CFSTR("InterfaceNamerControlPrefs"),
183 kInterfaceNamerControlPrefsID
,
188 if ((runloop
!= NULL
) && (callback
!= NULL
)) {
189 S_callback
= callback
;
190 if (!SCPreferencesSetCallback(S_prefs
, InterfaceNamerControlPrefsChanged
, NULL
)) {
191 SC_log(LOG_NOTICE
, "SCPreferencesSetCallBack() failed: %s", SCErrorString(SCError()));
195 if (!SCPreferencesScheduleWithRunLoop(S_prefs
, runloop
, kCFRunLoopCommonModes
)) {
196 SC_log(LOG_NOTICE
, "SCPreferencesScheduleWithRunLoop() failed: %s", SCErrorString(SCError()));
197 (void) SCPreferencesSetCallback(S_prefs
, NULL
, NULL
);
200 enable_prefs_observer(runloop
);
208 InterfaceNamerControlPrefsSave(void)
210 Boolean saved
= FALSE
;
212 if (S_prefs
!= NULL
) {
213 saved
= SCPreferencesCommitChanges(S_prefs
);
214 SCPreferencesSynchronize(S_prefs
);
220 prefs_get_boolean(CFStringRef key
)
222 CFBooleanRef b
= NULL
;
223 SCPreferencesRef prefs
;
226 prefs
= InterfaceNamerControlManagedPrefsGet();
228 b
= SCPreferencesGetValue(prefs
, key
);
229 b
= isA_CFBoolean(b
);
234 #endif /* TARGET_OS_IPHONE */
236 prefs
= InterfaceNamerControlPrefsGet();
238 b
= SCPreferencesGetValue(prefs
, key
);
239 b
= isA_CFBoolean(b
);
245 prefs_set_boolean(CFStringRef key
, CFBooleanRef b
)
247 SCPreferencesRef prefs
;
249 prefs
= InterfaceNamerControlPrefsGet();
251 if (isA_CFBoolean(b
) == NULL
) {
252 SCPreferencesRemoveValue(prefs
, key
);
255 SCPreferencesSetValue(prefs
, key
, b
);
262 InterfaceNamerControlPrefsRefresh(void)
264 if (S_prefs
!= NULL
) {
265 SCPreferencesSynchronize(S_prefs
);
268 if (S_managed_prefs
!= NULL
) {
269 SCPreferencesSynchronize(S_managed_prefs
);
271 #endif /* TARGET_OS_IPHONE */
278 __private_extern__ Boolean
279 InterfaceNamerControlPrefsAllowNewInterfaces(void)
282 Boolean allow
= FALSE
;
284 b
= prefs_get_boolean(kAllowNewInterfaces
);
286 allow
= CFBooleanGetValue(b
);
288 /* flush the backing store */
289 InterfaceNamerControlPrefsRefresh();
296 __private_extern__ Boolean
297 InterfaceNamerControlPrefsSetAllowNewInterfaces(Boolean allow
)
300 prefs_set_boolean(kAllowNewInterfaces
, kCFBooleanTrue
);
302 prefs_set_boolean(kAllowNewInterfaces
, NULL
);
304 return (InterfaceNamerControlPrefsSave());