66static const CFStringRef kHantInputModeID =
77 CFSTR (" im.rime.inputmethod.Squirrel.Hant" );
88
9- typedef NS_OPTIONS (int , RimeInputMode) {
10- DEFAULT_INPUT_MODE = 1 << 0 ,
11- HANS_INPUT_MODE = 1 << 0 ,
12- HANT_INPUT_MODE = 1 << 1
13- };
9+ #define HANS_INPUT_MODE (1 << 0 )
10+ #define HANT_INPUT_MODE (1 << 1 )
11+
12+ #define DEFAULT_INPUT_MODE HANS_INPUT_MODE
13+
14+ int GetEnabledInputModes (void );
1415
1516void RegisterInputSource (void ) {
17+ int enabled_input_modes = GetEnabledInputModes ();
18+ if (enabled_input_modes) {
19+ // Already registered.
20+ return ;
21+ }
1622 CFURLRef installedLocationURL = CFURLCreateFromFileSystemRepresentation (
1723 NULL , (UInt8*)kInstallLocation , (CFIndex)strlen (kInstallLocation ), false );
1824 if (installedLocationURL) {
@@ -22,7 +28,14 @@ void RegisterInputSource(void) {
2228 }
2329}
2430
25- 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;
2639 CFArrayRef sourceList = TISCreateInputSourceList (NULL , true );
2740 for (CFIndex i = 0 ; i < CFArrayGetCount (sourceList); ++i) {
2841 TISInputSourceRef inputSource =
@@ -31,23 +44,61 @@ void ActivateInputSource(int enabled_modes) {
3144 inputSource, kTISPropertyInputSourceID );
3245 // NSLog(@"Examining input source: %@", sourceID);
3346 if ((!CFStringCompare (sourceID, kHansInputModeID , 0 ) &&
34- ((enabled_modes & HANS_INPUT_MODE) != 0 )) ||
47+ ((input_modes_to_enable & HANS_INPUT_MODE) != 0 )) ||
3548 (!CFStringCompare (sourceID, kHantInputModeID , 0 ) &&
36- ((enabled_modes & HANT_INPUT_MODE) != 0 ))) {
37- TISEnableInputSource (inputSource);
38- 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 = ((enabled_input_modes & DEFAULT_INPUT_MODE) != 0 )
64+ ? DEFAULT_INPUT_MODE
65+ : 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+ }
3987 CFBooleanRef isSelectable = (CFBooleanRef)TISGetInputSourceProperty (
4088 inputSource, kTISPropertyInputSourceIsSelectCapable );
41- if (CFBooleanGetValue (isSelectable)) {
89+ CFBooleanRef isSelected = (CFBooleanRef)TISGetInputSourceProperty (
90+ inputSource, kTISPropertyInputSourceIsSelected );
91+ if (!CFBooleanGetValue (isSelected) && CFBooleanGetValue (isSelectable)) {
4292 TISSelectInputSource (inputSource);
4393 NSLog (@" Selected input source: %@ " , sourceID);
4494 }
95+ break ;
4596 }
4697 }
4798 CFRelease (sourceList);
4899}
49100
50- void DeactivateInputSource (void ) {
101+ void DisableInputSource (void ) {
51102 CFArrayRef sourceList = TISCreateInputSourceList (NULL , true );
52103 for (CFIndex i = CFArrayGetCount (sourceList); i > 0 ; --i) {
53104 TISInputSourceRef inputSource =
@@ -68,8 +119,8 @@ void DeactivateInputSource(void) {
68119 CFRelease (sourceList);
69120}
70121
71- RimeInputMode GetEnabledInputModes (void ) {
72- RimeInputMode input_modes = 0 ;
122+ int GetEnabledInputModes (void ) {
123+ int input_modes = 0 ;
73124 CFArrayRef sourceList = TISCreateInputSourceList (NULL , true );
74125 for (CFIndex i = 0 ; i < CFArrayGetCount (sourceList); ++i) {
75126 TISInputSourceRef inputSource =
0 commit comments