diff --git a/Info.plist b/Info.plist index 5fb01b358..0929d4c2e 100644 --- a/Info.plist +++ b/Info.plist @@ -2,8 +2,6 @@ - TISInputSourceID - im.rime.inputmethod.Squirrel CFBundleDevelopmentRegion English CFBundleExecutable @@ -37,7 +35,7 @@ tsInputModeCharacterRepertoireKey Hans - Han + Hant tsInputModeDefaultStateKey @@ -65,10 +63,10 @@ tsInputModeCharacterRepertoireKey Hant - Han + Hans tsInputModeDefaultStateKey - + tsInputModeIsVisibleKey tsInputModeKeyEquivalentModifiersKey @@ -111,13 +109,5 @@ dsa_pub.pem TICapsLockLanguageSwitchCapable - tsInputMethodCharacterRepertoireKey - - Hans - Hant - Han - - tsInputMethodIconFileKey - rime.pdf diff --git a/input_source.m b/input_source.m index be983a142..35b1abf2f 100644 --- a/input_source.m +++ b/input_source.m @@ -2,10 +2,13 @@ static const unsigned char kInstallLocation[] = "/Library/Input Methods/Squirrel.app"; -static NSString *const kSourceID = - @"im.rime.inputmethod.Squirrel"; -static NSString *const kInputModeID = +static NSString *const kHansInputModeID = @"im.rime.inputmethod.Squirrel.Hans"; +static NSString *const kHantInputModeID = + @"im.rime.inputmethod.Squirrel.Hant"; + +#define HANS_INPUT_MODE (1 << 0) +#define HANT_INPUT_MODE (1 << 1) void RegisterInputSource() { CFURLRef installedLocationURL = CFURLCreateFromFileSystemRepresentation( @@ -17,16 +20,18 @@ void RegisterInputSource() { } } -void ActivateInputSource() { +void ActivateInputSource(int enabled_modes) { CFArrayRef sourceList = TISCreateInputSourceList(NULL, true); for (CFIndex i = 0; i < CFArrayGetCount(sourceList); ++i) { TISInputSourceRef inputSource = (TISInputSourceRef)(CFArrayGetValueAtIndex( sourceList, i)); NSString *sourceID = (__bridge NSString *)(TISGetInputSourceProperty( inputSource, kTISPropertyInputSourceID)); - //NSLog(@"examining input source '%@", sourceID); - if ([sourceID isEqualToString:kSourceID] || - [sourceID isEqualToString:kInputModeID]) { + //NSLog(@"Examining input source: %@", sourceID); + if ([sourceID isEqualToString:kHansInputModeID] && + ((enabled_modes & HANS_INPUT_MODE) != 0) || + [sourceID isEqualToString:kHantInputModeID] && + ((enabled_modes & HANT_INPUT_MODE) != 0)) { TISEnableInputSource(inputSource); NSLog(@"Enabled input source: %@", sourceID); CFBooleanRef isSelectable = (CFBooleanRef)TISGetInputSourceProperty( @@ -48,17 +53,21 @@ void DeactivateInputSource() { NSString *sourceID = (__bridge NSString *)(TISGetInputSourceProperty( inputSource, kTISPropertyInputSourceID)); //NSLog(@"Examining input source: %@", sourceID); - if ([sourceID isEqualToString:kSourceID] || - [sourceID isEqualToString:kInputModeID]) { - TISDisableInputSource(inputSource); - NSLog(@"Disabled input source: %@", sourceID); + if ([sourceID isEqualToString:kHansInputModeID] || + [sourceID isEqualToString:kHantInputModeID]) { + CFBooleanRef isEnabled = (CFBooleanRef)(TISGetInputSourceProperty( + inputSource, kTISPropertyInputSourceIsEnabled)); + if (CFBooleanGetValue(isEnabled)) { + TISDisableInputSource(inputSource); + NSLog(@"Disabled input source: %@", sourceID); + } } } CFRelease(sourceList); } -BOOL IsInputSourceActive() { - int active = 0; +int GetEnabledInputModes() { + int input_modes = 0; CFArrayRef sourceList = TISCreateInputSourceList(NULL, true); for (CFIndex i = 0; i < CFArrayGetCount(sourceList); ++i) { TISInputSourceRef inputSource = (TISInputSourceRef)(CFArrayGetValueAtIndex( @@ -66,16 +75,19 @@ BOOL IsInputSourceActive() { NSString *sourceID = (__bridge NSString *)(TISGetInputSourceProperty( inputSource, kTISPropertyInputSourceID)); //NSLog(@"Examining input source: %@", sourceID); - if ([sourceID isEqualToString:kSourceID] || - [sourceID isEqualToString:kInputModeID]) { + if ([sourceID isEqualToString:kHansInputModeID] || + [sourceID isEqualToString:kHantInputModeID]) { CFBooleanRef isEnabled = (CFBooleanRef)(TISGetInputSourceProperty( inputSource, kTISPropertyInputSourceIsEnabled)); if (CFBooleanGetValue(isEnabled)) { - ++active; + if ([sourceID isEqualToString:kHansInputModeID]) + input_modes |= HANS_INPUT_MODE; + else if ([sourceID isEqualToString:kHantInputModeID]) + input_modes |= HANT_INPUT_MODE; } } } CFRelease(sourceList); - //NSLog(@"IsInputSourceActive: %d", active); - return active == 2; // 1 active input method + 1 active input mode + NSLog(@"EnabledInputModes: %d", input_modes); + return input_modes; } diff --git a/main.m b/main.m index a98f1c76d..f4f95d6f7 100644 --- a/main.m +++ b/main.m @@ -6,9 +6,11 @@ #import void RegisterInputSource(); -void ActivateInputSource(); +int GetEnabledInputModes(); void DeactivateInputSource(); -// BOOL IsInputSourceActive(); +void ActivateInputSource(int input_modes); + +#define DEFAULT_INPUT_MODE 1 // Each input method needs a unique connection name. // Note that periods and spaces are not allowed in the connection name. @@ -35,8 +37,9 @@ int main(int argc, char *argv[]) { if (argc > 1 && !strcmp("--install", argv[1])) { // register and enable Squirrel RegisterInputSource(); + int input_modes = GetEnabledInputModes(); DeactivateInputSource(); - ActivateInputSource(); + ActivateInputSource(input_modes ? input_modes : DEFAULT_INPUT_MODE); return 0; }