1dc61d50dSmrg//
2dc61d50dSmrg//  NSUserDefaults+XQuartzDefaults.m
3dc61d50dSmrg//  XQuartz
4dc61d50dSmrg//
5dc61d50dSmrg//  Created by Jeremy Huddleston Sequoia on 2021.02.19.
6dc61d50dSmrg//  Copyright (c) 2021 Apple Inc. All rights reserved.
7dc61d50dSmrg//
8dc61d50dSmrg
9dc61d50dSmrg#import "NSUserDefaults+XQuartzDefaults.h"
10dc61d50dSmrg#import <dispatch/dispatch.h>
11dc61d50dSmrg
12dc61d50dSmrgNSString * const XQuartzPrefKeyAppsMenu = @"apps_menu";
13dc61d50dSmrgNSString * const XQuartzPrefKeyFakeButtons = @"enable_fake_buttons";
14dc61d50dSmrgNSString * const XQuartzPrefKeyFakeButton2 = @"fake_button2";
15dc61d50dSmrgNSString * const XQuartzPrefKeyFakeButton3 = @"fake_button3";
16dc61d50dSmrgNSString * const XQuartzPrefKeyKeyEquivs = @"enable_key_equivalents";
17dc61d50dSmrgNSString * const XQuartzPrefKeyFullscreenHotkeys = @"fullscreen_hotkeys";
18dc61d50dSmrgNSString * const XQuartzPrefKeyFullscreenMenu = @"fullscreen_menu";
19dc61d50dSmrgNSString * const XQuartzPrefKeySyncKeymap = @"sync_keymap";
20dc61d50dSmrgNSString * const XQuartzPrefKeyDepth = @"depth";
21dc61d50dSmrgNSString * const XQuartzPrefKeyNoAuth = @"no_auth";
22dc61d50dSmrgNSString * const XQuartzPrefKeyNoTCP = @"nolisten_tcp";
23dc61d50dSmrgNSString * const XQuartzPrefKeyDoneXinitCheck = @"done_xinit_check";
24dc61d50dSmrgNSString * const XQuartzPrefKeyNoQuitAlert = @"no_quit_alert";
25dc61d50dSmrgNSString * const XQuartzPrefKeyNoRANDRAlert = @"no_randr_alert";
26dc61d50dSmrgNSString * const XQuartzPrefKeyOptionSendsAlt = @"option_sends_alt";
27dc61d50dSmrgNSString * const XQuartzPrefKeyAppKitModifiers = @"appkit_modifiers";
28dc61d50dSmrgNSString * const XQuartzPrefKeyWindowItemModifiers = @"window_item_modifiers";
29dc61d50dSmrgNSString * const XQuartzPrefKeyRootless = @"rootless";
30dc61d50dSmrgNSString * const XQuartzPrefKeyRENDERExtension = @"enable_render_extension";
31dc61d50dSmrgNSString * const XQuartzPrefKeyTESTExtension = @"enable_test_extensions";
32dc61d50dSmrgNSString * const XQuartzPrefKeyLoginShell = @"login_shell";
33dc61d50dSmrgNSString * const XQuartzPrefKeyUpdateFeed = @"update_feed";
34dc61d50dSmrgNSString * const XQuartzPrefKeyClickThrough = @"wm_click_through";
35dc61d50dSmrgNSString * const XQuartzPrefKeyFocusFollowsMouse = @"wm_ffm";
36dc61d50dSmrgNSString * const XQuartzPrefKeyFocusOnNewWindow = @"wm_focus_on_new_window";
37dc61d50dSmrg
38dc61d50dSmrgNSString * const XQuartzPrefKeyScrollInDeviceDirection = @"scroll_in_device_direction";
39dc61d50dSmrgNSString * const XQuartzPrefKeySyncPasteboard = @"sync_pasteboard";
40dc61d50dSmrgNSString * const XQuartzPrefKeySyncPasteboardToClipboard = @"sync_pasteboard_to_clipboard";
41dc61d50dSmrgNSString * const XQuartzPrefKeySyncPasteboardToPrimary = @"sync_pasteboard_to_primary";
42dc61d50dSmrgNSString * const XQuartzPrefKeySyncClipboardToPasteBoard = @"sync_clipboard_to_pasteboard";
43dc61d50dSmrgNSString * const XQuartzPrefKeySyncPrimaryOnSelect = @"sync_primary_on_select";
44dc61d50dSmrg
45dc61d50dSmrg@implementation NSUserDefaults (XQuartzDefaults)
46dc61d50dSmrg
47dc61d50dSmrg+ (NSUserDefaults *)globalDefaults
48dc61d50dSmrg{
49dc61d50dSmrg    static dispatch_once_t once;
50dc61d50dSmrg    static NSUserDefaults *defaults;
51dc61d50dSmrg
52dc61d50dSmrg    dispatch_once(&once, ^{
53dc61d50dSmrg        NSString * const defaultsDomain = @".GlobalPreferences";
54dc61d50dSmrg        defaults = [[[NSUserDefaults alloc] initWithSuiteName:defaultsDomain] retain];
55dc61d50dSmrg
56dc61d50dSmrg        NSDictionary<NSString *, id> * const defaultDefaultsDict = @{
57dc61d50dSmrg            @"AppleSpacesSwitchOnActivate" : @(YES),
58dc61d50dSmrg        };
59dc61d50dSmrg
60dc61d50dSmrg        [defaults registerDefaults:defaultDefaultsDict];
61dc61d50dSmrg    });
62dc61d50dSmrg
63dc61d50dSmrg    return defaults;
64dc61d50dSmrg}
65dc61d50dSmrg
66dc61d50dSmrg+ (NSUserDefaults *)dockDefaults
67dc61d50dSmrg{
68dc61d50dSmrg    static dispatch_once_t once;
69dc61d50dSmrg    static NSUserDefaults *defaults;
70dc61d50dSmrg
71dc61d50dSmrg    dispatch_once(&once, ^{
72dc61d50dSmrg        NSString * const defaultsDomain = @"com.apple.dock";
73dc61d50dSmrg        defaults = [[[NSUserDefaults alloc] initWithSuiteName:defaultsDomain] retain];
74dc61d50dSmrg
75dc61d50dSmrg        NSDictionary<NSString *, id> * const defaultDefaultsDict = @{
76dc61d50dSmrg            @"workspaces" : @(NO),
77dc61d50dSmrg        };
78dc61d50dSmrg
79dc61d50dSmrg        [defaults registerDefaults:defaultDefaultsDict];
80dc61d50dSmrg    });
81dc61d50dSmrg
82dc61d50dSmrg    return defaults;
83dc61d50dSmrg}
84dc61d50dSmrg
85dc61d50dSmrg+ (NSUserDefaults *)xquartzDefaults
86dc61d50dSmrg{
87dc61d50dSmrg    static dispatch_once_t once;
88dc61d50dSmrg    static NSUserDefaults *defaults;
89dc61d50dSmrg
90dc61d50dSmrg    dispatch_once(&once, ^{
91dc61d50dSmrg        NSString * const defaultsDomain = @(BUNDLE_ID_PREFIX ".X11");
92dc61d50dSmrg        NSString * const defaultDefaultsDomain = NSBundle.mainBundle.bundleIdentifier;
93dc61d50dSmrg        if ([defaultsDomain isEqualToString:defaultDefaultsDomain]) {
94dc61d50dSmrg            defaults = [NSUserDefaults.standardUserDefaults retain];
95dc61d50dSmrg        } else {
96dc61d50dSmrg            defaults = [[[NSUserDefaults alloc] initWithSuiteName:defaultsDomain] retain];
97dc61d50dSmrg        }
98dc61d50dSmrg
99dc61d50dSmrg        NSString *defaultWindowItemModifiers = @"command";
100dc61d50dSmrg        NSString * const defaultWindowItemModifiersLocalized = NSLocalizedString(@"window item modifiers", @"window item modifiers");
101dc61d50dSmrg        if (![defaultWindowItemModifiersLocalized isEqualToString:@"window item modifiers"]) {
102dc61d50dSmrg            defaultWindowItemModifiers = defaultWindowItemModifiersLocalized;
103dc61d50dSmrg        }
104dc61d50dSmrg
105dc61d50dSmrg        NSDictionary<NSString *, id> * const defaultDefaultsDict = @{
106dc61d50dSmrg            XQuartzPrefKeyFakeButtons : @(NO),
107dc61d50dSmrg            // XQuartzPrefKeyFakeButton2 nil default
108dc61d50dSmrg            // XQuartzPrefKeyFakeButton3 nil default
109dc61d50dSmrg            XQuartzPrefKeyKeyEquivs : @(YES),
110dc61d50dSmrg            XQuartzPrefKeyFullscreenHotkeys : @(NO),
111dc61d50dSmrg            XQuartzPrefKeyFullscreenMenu : @(NO),
112dc61d50dSmrg            XQuartzPrefKeySyncKeymap : @(NO),
113dc61d50dSmrg            XQuartzPrefKeyDepth : @(-1),
114dc61d50dSmrg            XQuartzPrefKeyNoAuth : @(NO),
115dc61d50dSmrg            XQuartzPrefKeyNoTCP : @(NO),
116dc61d50dSmrg            XQuartzPrefKeyDoneXinitCheck : @(NO),
117dc61d50dSmrg            XQuartzPrefKeyNoQuitAlert : @(NO),
118dc61d50dSmrg            XQuartzPrefKeyNoRANDRAlert : @(NO),
119dc61d50dSmrg            XQuartzPrefKeyOptionSendsAlt : @(NO),
120dc61d50dSmrg            // XQuartzPrefKeyAppKitModifiers nil default
121dc61d50dSmrg            XQuartzPrefKeyWindowItemModifiers : defaultWindowItemModifiers,
122dc61d50dSmrg            XQuartzPrefKeyRootless : @(YES),
123dc61d50dSmrg            XQuartzPrefKeyRENDERExtension : @(YES),
124dc61d50dSmrg            XQuartzPrefKeyTESTExtension : @(NO),
125dc61d50dSmrg            XQuartzPrefKeyLoginShell : @"/bin/sh",
126dc61d50dSmrg            XQuartzPrefKeyClickThrough : @(NO),
127dc61d50dSmrg            XQuartzPrefKeyFocusFollowsMouse : @(NO),
128dc61d50dSmrg            XQuartzPrefKeyFocusOnNewWindow : @(YES),
129dc61d50dSmrg
130dc61d50dSmrg            XQuartzPrefKeyScrollInDeviceDirection : @(NO),
131dc61d50dSmrg            XQuartzPrefKeySyncPasteboard : @(YES),
132dc61d50dSmrg            XQuartzPrefKeySyncPasteboardToClipboard : @(YES),
133dc61d50dSmrg            XQuartzPrefKeySyncPasteboardToPrimary : @(YES),
134dc61d50dSmrg            XQuartzPrefKeySyncClipboardToPasteBoard : @(YES),
135dc61d50dSmrg            XQuartzPrefKeySyncPrimaryOnSelect : @(NO),
136dc61d50dSmrg        };
137dc61d50dSmrg
138dc61d50dSmrg        [defaults registerDefaults:defaultDefaultsDict];
139c82838c1Smrg
140c82838c1Smrg        NSString * const systemDefaultsPlistPath = [@(XQUARTZ_DATA_DIR) stringByAppendingPathComponent:@"defaults.plist"];
141c82838c1Smrg        NSDictionary <NSString *, id> * const systemDefaultsDict = [NSDictionary dictionaryWithContentsOfFile:systemDefaultsPlistPath];
142c82838c1Smrg        [defaults registerDefaults:systemDefaultsDict];
143dc61d50dSmrg    });
144dc61d50dSmrg
145dc61d50dSmrg    return defaults;
146dc61d50dSmrg}
147dc61d50dSmrg
148dc61d50dSmrg@end
149