2 * Copyright (c) 2017-2019 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 /* get the current value */
74 if (S_callback
!= NULL
) {
75 (*S_callback
)(S_prefs
);
83 * kInterfaceNamerControlManangedPrefsID
84 * - identifies the location of the managed preferences file
86 #define kManagedPrefsDirStr "/Library/Managed Preferences/mobile/"
87 #define kInterfaceNamerControlManagedPrefsID CFSTR(kManagedPrefsDirStr \
88 kInterfaceNamerControlPrefsIDStr)
89 static SCPreferencesRef S_managed_prefs
;
91 static SCPreferencesRef
92 InterfaceNamerControlManagedPrefsGet(void)
94 if (S_managed_prefs
== NULL
) {
95 CFMutableDictionaryRef options
;
97 options
= CFDictionaryCreateMutable(NULL
,
99 &kCFTypeDictionaryKeyCallBacks
,
100 &kCFTypeDictionaryValueCallBacks
);
101 CFDictionarySetValue(options
, kSCPreferencesOptionRemoveWhenEmpty
, kCFBooleanTrue
);
102 S_managed_prefs
= SCPreferencesCreateWithOptions(NULL
,
103 CFSTR("InterfaceNamerControlPrefs"),
104 kInterfaceNamerControlManagedPrefsID
,
109 return (S_managed_prefs
);
113 enable_prefs_observer(CFRunLoopRef runloop
)
115 CFRunLoopSourceContext context
;
116 dispatch_queue_t queue
;
117 CFRunLoopSourceRef source
;
119 memset(&context
, 0, sizeof(context
));
120 context
.perform
= prefs_changed
;
121 source
= CFRunLoopSourceCreate(kCFAllocatorDefault
, 0, &context
);
122 CFRunLoopAddSource(runloop
, source
, kCFRunLoopCommonModes
);
123 queue
= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0);
124 _scprefs_observer_watch(scprefs_observer_type_global
,
125 kInterfaceNamerControlPrefsIDStr
,
128 if (source
!= NULL
) {
129 CFRunLoopSourceSignal(source
);
130 if (runloop
!= NULL
) {
131 CFRunLoopWakeUp(runloop
);
138 #else /* TARGET_OS_IPHONE */
141 enable_prefs_observer(CFRunLoopRef runloop
)
143 #pragma unused(runloop)
147 #endif /* TARGET_OS_IPHONE */
150 InterfaceNamerControlPrefsChanged(SCPreferencesRef prefs
,
151 SCPreferencesNotification type
,
154 #pragma unused(prefs)
161 __private_extern__ SCPreferencesRef
162 InterfaceNamerControlPrefsInit(CFRunLoopRef runloop
,
163 InterfaceNamerControlPrefsCallBack callback
)
165 CFMutableDictionaryRef options
;
167 options
= CFDictionaryCreateMutable(NULL
,
169 &kCFTypeDictionaryKeyCallBacks
,
170 &kCFTypeDictionaryValueCallBacks
);
171 CFDictionarySetValue(options
, kSCPreferencesOptionRemoveWhenEmpty
, kCFBooleanTrue
);
172 S_prefs
= SCPreferencesCreateWithOptions(NULL
,
173 CFSTR("InterfaceNamerControlPrefs"),
174 kInterfaceNamerControlPrefsID
,
179 if ((runloop
!= NULL
) && (callback
!= NULL
)) {
180 S_callback
= callback
;
181 if (!SCPreferencesSetCallback(S_prefs
, InterfaceNamerControlPrefsChanged
, NULL
)) {
182 SC_log(LOG_NOTICE
, "SCPreferencesSetCallBack() failed: %s", SCErrorString(SCError()));
186 if (!SCPreferencesScheduleWithRunLoop(S_prefs
, runloop
, kCFRunLoopCommonModes
)) {
187 SC_log(LOG_NOTICE
, "SCPreferencesScheduleWithRunLoop() failed: %s", SCErrorString(SCError()));
188 (void) SCPreferencesSetCallback(S_prefs
, NULL
, NULL
);
191 enable_prefs_observer(runloop
);
199 InterfaceNamerControlPrefsSave(void)
201 Boolean saved
= FALSE
;
203 if (S_prefs
!= NULL
) {
204 saved
= SCPreferencesCommitChanges(S_prefs
);
205 SCPreferencesSynchronize(S_prefs
);
211 prefs_get_boolean(CFStringRef key
)
213 CFBooleanRef b
= NULL
;
214 SCPreferencesRef prefs
;
217 prefs
= InterfaceNamerControlManagedPrefsGet();
219 b
= SCPreferencesGetValue(prefs
, key
);
220 b
= isA_CFBoolean(b
);
225 #endif /* TARGET_OS_IPHONE */
227 prefs
= InterfaceNamerControlPrefsGet();
229 b
= SCPreferencesGetValue(prefs
, key
);
230 b
= isA_CFBoolean(b
);
236 prefs_set_boolean(CFStringRef key
, CFBooleanRef b
)
238 SCPreferencesRef prefs
;
240 prefs
= InterfaceNamerControlPrefsGet();
242 if (isA_CFBoolean(b
) == NULL
) {
243 SCPreferencesRemoveValue(prefs
, key
);
246 SCPreferencesSetValue(prefs
, key
, b
);
253 InterfaceNamerControlPrefsRefresh(void)
255 if (S_prefs
!= NULL
) {
256 SCPreferencesSynchronize(S_prefs
);
259 if (S_managed_prefs
!= NULL
) {
260 SCPreferencesSynchronize(S_managed_prefs
);
262 #endif /* TARGET_OS_IPHONE */
269 __private_extern__ Boolean
270 InterfaceNamerControlPrefsAllowNewInterfaces(void)
273 Boolean allow
= FALSE
;
275 b
= prefs_get_boolean(kAllowNewInterfaces
);
277 allow
= CFBooleanGetValue(b
);
279 /* flush the backing store */
280 InterfaceNamerControlPrefsRefresh();
287 __private_extern__ Boolean
288 InterfaceNamerControlPrefsSetAllowNewInterfaces(Boolean allow
)
291 prefs_set_boolean(kAllowNewInterfaces
, kCFBooleanTrue
);
293 prefs_set_boolean(kAllowNewInterfaces
, NULL
);
295 return (InterfaceNamerControlPrefsSave());