Skip to content

Commit

Permalink
fix: install one input mode or keep previous ones
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Feb 4, 2023
1 parent fbd430d commit 3bc6c2c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
16 changes: 3 additions & 13 deletions Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>TISInputSourceID</key>
<string>im.rime.inputmethod.Squirrel</string>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
Expand Down Expand Up @@ -37,7 +35,7 @@
<key>tsInputModeCharacterRepertoireKey</key>
<array>
<string>Hans</string>
<string>Han</string>
<string>Hant</string>
</array>
<key>tsInputModeDefaultStateKey</key>
<true/>
Expand Down Expand Up @@ -65,10 +63,10 @@
<key>tsInputModeCharacterRepertoireKey</key>
<array>
<string>Hant</string>
<string>Han</string>
<string>Hans</string>
</array>
<key>tsInputModeDefaultStateKey</key>
<true/>
<false/>
<key>tsInputModeIsVisibleKey</key>
<true/>
<key>tsInputModeKeyEquivalentModifiersKey</key>
Expand Down Expand Up @@ -111,13 +109,5 @@
<string>dsa_pub.pem</string>
<key>TICapsLockLanguageSwitchCapable</key>
<true/>
<key>tsInputMethodCharacterRepertoireKey</key>
<array>
<string>Hans</string>
<string>Hant</string>
<string>Han</string>
</array>
<key>tsInputMethodIconFileKey</key>
<string>rime.pdf</string>
</dict>
</plist>
48 changes: 30 additions & 18 deletions input_source.m
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -48,34 +53,41 @@ 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(
sourceList, i));
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;
}
9 changes: 6 additions & 3 deletions main.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#import <string.h>

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.
Expand All @@ -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;
}

Expand Down

0 comments on commit 3bc6c2c

Please sign in to comment.