9
9
#define HANS_INPUT_MODE (1 << 0 )
10
10
#define HANT_INPUT_MODE (1 << 1 )
11
11
12
+ #define DEFAULT_INPUT_MODE HANS_INPUT_MODE
13
+
14
+ int GetEnabledInputModes (void );
15
+
12
16
void RegisterInputSource (void ) {
17
+ int enabled_input_modes = GetEnabledInputModes ();
18
+ if (enabled_input_modes) {
19
+ // Already registered.
20
+ return ;
21
+ }
13
22
CFURLRef installedLocationURL = CFURLCreateFromFileSystemRepresentation (
14
23
NULL , (UInt8 *)kInstallLocation , (CFIndex )strlen (kInstallLocation ), false );
15
24
if (installedLocationURL) {
@@ -19,7 +28,14 @@ void RegisterInputSource(void) {
19
28
}
20
29
}
21
30
22
- void ActivateInputSource (int enabled_modes) {
31
+ void EnableInputSource (void ) {
32
+ int enabled_input_modes = GetEnabledInputModes ();
33
+ if (enabled_input_modes) {
34
+ // keep user's manually enabled input modes.
35
+ return ;
36
+ }
37
+ // neither is enabled, enable the default input mode.
38
+ int input_modes_to_enable = DEFAULT_INPUT_MODE;
23
39
CFArrayRef sourceList = TISCreateInputSourceList (NULL , true );
24
40
for (CFIndex i = 0 ; i < CFArrayGetCount (sourceList); ++i) {
25
41
TISInputSourceRef inputSource =
@@ -28,23 +44,61 @@ void ActivateInputSource(int enabled_modes) {
28
44
inputSource, kTISPropertyInputSourceID );
29
45
// NSLog(@"Examining input source: %@", sourceID);
30
46
if ((!CFStringCompare (sourceID, kHansInputModeID , 0 ) &&
31
- ((enabled_modes & HANS_INPUT_MODE) != 0 )) ||
47
+ ((input_modes_to_enable & HANS_INPUT_MODE) != 0 )) ||
32
48
(!CFStringCompare (sourceID, kHantInputModeID , 0 ) &&
33
- ((enabled_modes & HANT_INPUT_MODE) != 0 ))) {
34
- TISEnableInputSource (inputSource);
35
- NSLog (@" Enabled input source: %@ " , sourceID);
49
+ ((input_modes_to_enable & HANT_INPUT_MODE) != 0 ))) {
50
+ CFBooleanRef isEnabled = (CFBooleanRef )TISGetInputSourceProperty (
51
+ inputSource, kTISPropertyInputSourceIsEnabled );
52
+ if (!CFBooleanGetValue (isEnabled)) {
53
+ TISEnableInputSource (inputSource);
54
+ NSLog (@" Enabled input source: %@ " , sourceID);
55
+ }
56
+ }
57
+ }
58
+ CFRelease (sourceList);
59
+ }
60
+
61
+ void SelectInputSource (void ) {
62
+ int enabled_input_modes = GetEnabledInputModes ();
63
+ int input_modes_to_select =
64
+ ((enabled_input_modes & DEFAULT_INPUT_MODE) != 0 )
65
+ ? DEFAULT_INPUT_MODE : enabled_input_modes;
66
+ if (!input_modes_to_select) {
67
+ NSLog (@" No enabled input sources." );
68
+ return ;
69
+ }
70
+ CFArrayRef sourceList = TISCreateInputSourceList (NULL , true );
71
+ for (CFIndex i = 0 ; i < CFArrayGetCount (sourceList); ++i) {
72
+ TISInputSourceRef inputSource =
73
+ (TISInputSourceRef)CFArrayGetValueAtIndex (sourceList, i);
74
+ CFStringRef sourceID = (CFStringRef )TISGetInputSourceProperty (
75
+ inputSource, kTISPropertyInputSourceID );
76
+ // NSLog(@"Examining input source: %@", sourceID);
77
+ if ((!CFStringCompare (sourceID, kHansInputModeID , 0 ) &&
78
+ ((input_modes_to_select & HANS_INPUT_MODE) != 0 )) ||
79
+ (!CFStringCompare (sourceID, kHantInputModeID , 0 ) &&
80
+ ((input_modes_to_select & HANT_INPUT_MODE) != 0 ))) {
81
+ // select the first enabled input mode in Squirrel.
82
+ CFBooleanRef isEnabled = (CFBooleanRef )TISGetInputSourceProperty (
83
+ inputSource, kTISPropertyInputSourceIsEnabled );
84
+ if (!CFBooleanGetValue (isEnabled)) {
85
+ continue ;
86
+ }
36
87
CFBooleanRef isSelectable = (CFBooleanRef )TISGetInputSourceProperty (
37
88
inputSource, kTISPropertyInputSourceIsSelectCapable );
38
- if (CFBooleanGetValue (isSelectable)) {
89
+ CFBooleanRef isSelected = (CFBooleanRef )TISGetInputSourceProperty (
90
+ inputSource, kTISPropertyInputSourceIsSelected );
91
+ if (!CFBooleanGetValue (isSelected) && CFBooleanGetValue (isSelectable)) {
39
92
TISSelectInputSource (inputSource);
40
93
NSLog (@" Selected input source: %@ " , sourceID);
41
94
}
95
+ break ;
42
96
}
43
97
}
44
98
CFRelease (sourceList);
45
99
}
46
100
47
- void DeactivateInputSource (void ) {
101
+ void DisableInputSource (void ) {
48
102
CFArrayRef sourceList = TISCreateInputSourceList (NULL , true );
49
103
for (CFIndex i = CFArrayGetCount (sourceList); i > 0 ; --i) {
50
104
TISInputSourceRef inputSource =
0 commit comments